dsa_pmeth.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <stdio.h>
  15. #include "internal/cryptlib.h"
  16. #include <openssl/asn1t.h>
  17. #include <openssl/x509.h>
  18. #include <openssl/evp.h>
  19. #include <openssl/bn.h>
  20. #include "crypto/evp.h"
  21. #include "dsa_local.h"
  22. /* DSA pkey context structure */
  23. typedef struct {
  24. /* Parameter gen parameters */
  25. int nbits; /* size of p in bits (default: 2048) */
  26. int qbits; /* size of q in bits (default: 224) */
  27. const EVP_MD *pmd; /* MD for parameter generation */
  28. /* Keygen callback info */
  29. int gentmp[2];
  30. /* message digest */
  31. const EVP_MD *md; /* MD for the signature */
  32. } DSA_PKEY_CTX;
  33. static int pkey_dsa_init(EVP_PKEY_CTX *ctx)
  34. {
  35. DSA_PKEY_CTX *dctx = OPENSSL_malloc(sizeof(*dctx));
  36. if (dctx == NULL)
  37. return 0;
  38. dctx->nbits = 2048;
  39. dctx->qbits = 224;
  40. dctx->pmd = NULL;
  41. dctx->md = NULL;
  42. ctx->data = dctx;
  43. ctx->keygen_info = dctx->gentmp;
  44. ctx->keygen_info_count = 2;
  45. return 1;
  46. }
  47. static int pkey_dsa_copy(EVP_PKEY_CTX *dst, const EVP_PKEY_CTX *src)
  48. {
  49. DSA_PKEY_CTX *dctx, *sctx;
  50. if (!pkey_dsa_init(dst))
  51. return 0;
  52. sctx = src->data;
  53. dctx = dst->data;
  54. dctx->nbits = sctx->nbits;
  55. dctx->qbits = sctx->qbits;
  56. dctx->pmd = sctx->pmd;
  57. dctx->md = sctx->md;
  58. return 1;
  59. }
  60. static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
  61. {
  62. DSA_PKEY_CTX *dctx = ctx->data;
  63. OPENSSL_free(dctx);
  64. }
  65. static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
  66. size_t *siglen, const unsigned char *tbs,
  67. size_t tbslen)
  68. {
  69. int ret, md_size;
  70. unsigned int sltmp;
  71. DSA_PKEY_CTX *dctx = ctx->data;
  72. /*
  73. * Discard const. Its marked as const because this may be a cached copy of
  74. * the "real" key. These calls don't make any modifications that need to
  75. * be reflected back in the "original" key.
  76. */
  77. DSA *dsa = (DSA *)EVP_PKEY_get0_DSA(ctx->pkey);
  78. if (dctx->md != NULL) {
  79. md_size = EVP_MD_get_size(dctx->md);
  80. if (md_size <= 0)
  81. return 0;
  82. if (tbslen != (size_t)md_size)
  83. return 0;
  84. }
  85. ret = DSA_sign(0, tbs, tbslen, sig, &sltmp, dsa);
  86. if (ret <= 0)
  87. return ret;
  88. *siglen = sltmp;
  89. return 1;
  90. }
  91. static int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
  92. const unsigned char *sig, size_t siglen,
  93. const unsigned char *tbs, size_t tbslen)
  94. {
  95. int ret, md_size;
  96. DSA_PKEY_CTX *dctx = ctx->data;
  97. /*
  98. * Discard const. Its marked as const because this may be a cached copy of
  99. * the "real" key. These calls don't make any modifications that need to
  100. * be reflected back in the "original" key.
  101. */
  102. DSA *dsa = (DSA *)EVP_PKEY_get0_DSA(ctx->pkey);
  103. if (dctx->md != NULL) {
  104. md_size = EVP_MD_get_size(dctx->md);
  105. if (md_size <= 0)
  106. return 0;
  107. if (tbslen != (size_t)md_size)
  108. return 0;
  109. }
  110. ret = DSA_verify(0, tbs, tbslen, sig, siglen, dsa);
  111. return ret;
  112. }
  113. static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  114. {
  115. DSA_PKEY_CTX *dctx = ctx->data;
  116. switch (type) {
  117. case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
  118. if (p1 < 256)
  119. return -2;
  120. dctx->nbits = p1;
  121. return 1;
  122. case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
  123. if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
  124. return -2;
  125. dctx->qbits = p1;
  126. return 1;
  127. case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
  128. if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
  129. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
  130. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256) {
  131. ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
  132. return 0;
  133. }
  134. dctx->pmd = p2;
  135. return 1;
  136. case EVP_PKEY_CTRL_MD:
  137. if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
  138. EVP_MD_get_type((const EVP_MD *)p2) != NID_dsa &&
  139. EVP_MD_get_type((const EVP_MD *)p2) != NID_dsaWithSHA &&
  140. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
  141. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256 &&
  142. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha384 &&
  143. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha512 &&
  144. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_224 &&
  145. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_256 &&
  146. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_384 &&
  147. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_512) {
  148. ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
  149. return 0;
  150. }
  151. dctx->md = p2;
  152. return 1;
  153. case EVP_PKEY_CTRL_GET_MD:
  154. *(const EVP_MD **)p2 = dctx->md;
  155. return 1;
  156. case EVP_PKEY_CTRL_DIGESTINIT:
  157. case EVP_PKEY_CTRL_PKCS7_SIGN:
  158. case EVP_PKEY_CTRL_CMS_SIGN:
  159. return 1;
  160. case EVP_PKEY_CTRL_PEER_KEY:
  161. ERR_raise(ERR_LIB_DSA, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  162. return -2;
  163. default:
  164. return -2;
  165. }
  166. }
  167. static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
  168. const char *type, const char *value)
  169. {
  170. if (strcmp(type, "dsa_paramgen_bits") == 0) {
  171. int nbits;
  172. nbits = atoi(value);
  173. return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
  174. }
  175. if (strcmp(type, "dsa_paramgen_q_bits") == 0) {
  176. int qbits = atoi(value);
  177. return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, qbits);
  178. }
  179. if (strcmp(type, "dsa_paramgen_md") == 0) {
  180. const EVP_MD *md = EVP_get_digestbyname(value);
  181. if (md == NULL) {
  182. ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
  183. return 0;
  184. }
  185. return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, md);
  186. }
  187. return -2;
  188. }
  189. static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  190. {
  191. DSA *dsa = NULL;
  192. DSA_PKEY_CTX *dctx = ctx->data;
  193. BN_GENCB *pcb;
  194. int ret, res;
  195. if (ctx->pkey_gencb) {
  196. pcb = BN_GENCB_new();
  197. if (pcb == NULL)
  198. return 0;
  199. evp_pkey_set_cb_translate(pcb, ctx);
  200. } else
  201. pcb = NULL;
  202. dsa = DSA_new();
  203. if (dsa == NULL) {
  204. BN_GENCB_free(pcb);
  205. return 0;
  206. }
  207. if (dctx->md != NULL)
  208. ossl_ffc_set_digest(&dsa->params, EVP_MD_get0_name(dctx->md), NULL);
  209. ret = ossl_ffc_params_FIPS186_4_generate(NULL, &dsa->params,
  210. FFC_PARAM_TYPE_DSA, dctx->nbits,
  211. dctx->qbits, &res, pcb);
  212. BN_GENCB_free(pcb);
  213. if (ret > 0)
  214. EVP_PKEY_assign_DSA(pkey, dsa);
  215. else
  216. DSA_free(dsa);
  217. return ret;
  218. }
  219. static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  220. {
  221. DSA *dsa = NULL;
  222. if (ctx->pkey == NULL) {
  223. ERR_raise(ERR_LIB_DSA, DSA_R_NO_PARAMETERS_SET);
  224. return 0;
  225. }
  226. dsa = DSA_new();
  227. if (dsa == NULL)
  228. return 0;
  229. EVP_PKEY_assign_DSA(pkey, dsa);
  230. /* Note: if error return, pkey is freed by parent routine */
  231. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  232. return 0;
  233. return DSA_generate_key((DSA *)EVP_PKEY_get0_DSA(pkey));
  234. }
  235. static const EVP_PKEY_METHOD dsa_pkey_meth = {
  236. EVP_PKEY_DSA,
  237. EVP_PKEY_FLAG_AUTOARGLEN,
  238. pkey_dsa_init,
  239. pkey_dsa_copy,
  240. pkey_dsa_cleanup,
  241. 0,
  242. pkey_dsa_paramgen,
  243. 0,
  244. pkey_dsa_keygen,
  245. 0,
  246. pkey_dsa_sign,
  247. 0,
  248. pkey_dsa_verify,
  249. 0, 0,
  250. 0, 0, 0, 0,
  251. 0, 0,
  252. 0, 0,
  253. 0, 0,
  254. pkey_dsa_ctrl,
  255. pkey_dsa_ctrl_str
  256. };
  257. const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void)
  258. {
  259. return &dsa_pkey_meth;
  260. }