dsa_pmeth.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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;
  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 && tbslen != (size_t)EVP_MD_get_size(dctx->md))
  79. return 0;
  80. ret = DSA_sign(0, tbs, tbslen, sig, &sltmp, dsa);
  81. if (ret <= 0)
  82. return ret;
  83. *siglen = sltmp;
  84. return 1;
  85. }
  86. static int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
  87. const unsigned char *sig, size_t siglen,
  88. const unsigned char *tbs, size_t tbslen)
  89. {
  90. int ret;
  91. DSA_PKEY_CTX *dctx = ctx->data;
  92. /*
  93. * Discard const. Its marked as const because this may be a cached copy of
  94. * the "real" key. These calls don't make any modifications that need to
  95. * be reflected back in the "original" key.
  96. */
  97. DSA *dsa = (DSA *)EVP_PKEY_get0_DSA(ctx->pkey);
  98. if (dctx->md != NULL && tbslen != (size_t)EVP_MD_get_size(dctx->md))
  99. return 0;
  100. ret = DSA_verify(0, tbs, tbslen, sig, siglen, dsa);
  101. return ret;
  102. }
  103. static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  104. {
  105. DSA_PKEY_CTX *dctx = ctx->data;
  106. switch (type) {
  107. case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
  108. if (p1 < 256)
  109. return -2;
  110. dctx->nbits = p1;
  111. return 1;
  112. case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
  113. if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
  114. return -2;
  115. dctx->qbits = p1;
  116. return 1;
  117. case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
  118. if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
  119. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
  120. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256) {
  121. ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
  122. return 0;
  123. }
  124. dctx->pmd = p2;
  125. return 1;
  126. case EVP_PKEY_CTRL_MD:
  127. if (EVP_MD_get_type((const EVP_MD *)p2) != NID_sha1 &&
  128. EVP_MD_get_type((const EVP_MD *)p2) != NID_dsa &&
  129. EVP_MD_get_type((const EVP_MD *)p2) != NID_dsaWithSHA &&
  130. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha224 &&
  131. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha256 &&
  132. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha384 &&
  133. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha512 &&
  134. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_224 &&
  135. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_256 &&
  136. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_384 &&
  137. EVP_MD_get_type((const EVP_MD *)p2) != NID_sha3_512) {
  138. ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
  139. return 0;
  140. }
  141. dctx->md = p2;
  142. return 1;
  143. case EVP_PKEY_CTRL_GET_MD:
  144. *(const EVP_MD **)p2 = dctx->md;
  145. return 1;
  146. case EVP_PKEY_CTRL_DIGESTINIT:
  147. case EVP_PKEY_CTRL_PKCS7_SIGN:
  148. case EVP_PKEY_CTRL_CMS_SIGN:
  149. return 1;
  150. case EVP_PKEY_CTRL_PEER_KEY:
  151. ERR_raise(ERR_LIB_DSA, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  152. return -2;
  153. default:
  154. return -2;
  155. }
  156. }
  157. static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
  158. const char *type, const char *value)
  159. {
  160. if (strcmp(type, "dsa_paramgen_bits") == 0) {
  161. int nbits;
  162. nbits = atoi(value);
  163. return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
  164. }
  165. if (strcmp(type, "dsa_paramgen_q_bits") == 0) {
  166. int qbits = atoi(value);
  167. return EVP_PKEY_CTX_set_dsa_paramgen_q_bits(ctx, qbits);
  168. }
  169. if (strcmp(type, "dsa_paramgen_md") == 0) {
  170. const EVP_MD *md = EVP_get_digestbyname(value);
  171. if (md == NULL) {
  172. ERR_raise(ERR_LIB_DSA, DSA_R_INVALID_DIGEST_TYPE);
  173. return 0;
  174. }
  175. return EVP_PKEY_CTX_set_dsa_paramgen_md(ctx, md);
  176. }
  177. return -2;
  178. }
  179. static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  180. {
  181. DSA *dsa = NULL;
  182. DSA_PKEY_CTX *dctx = ctx->data;
  183. BN_GENCB *pcb;
  184. int ret, res;
  185. if (ctx->pkey_gencb) {
  186. pcb = BN_GENCB_new();
  187. if (pcb == NULL)
  188. return 0;
  189. evp_pkey_set_cb_translate(pcb, ctx);
  190. } else
  191. pcb = NULL;
  192. dsa = DSA_new();
  193. if (dsa == NULL) {
  194. BN_GENCB_free(pcb);
  195. return 0;
  196. }
  197. if (dctx->md != NULL)
  198. ossl_ffc_set_digest(&dsa->params, EVP_MD_get0_name(dctx->md), NULL);
  199. ret = ossl_ffc_params_FIPS186_4_generate(NULL, &dsa->params,
  200. FFC_PARAM_TYPE_DSA, dctx->nbits,
  201. dctx->qbits, &res, pcb);
  202. BN_GENCB_free(pcb);
  203. if (ret > 0)
  204. EVP_PKEY_assign_DSA(pkey, dsa);
  205. else
  206. DSA_free(dsa);
  207. return ret;
  208. }
  209. static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  210. {
  211. DSA *dsa = NULL;
  212. if (ctx->pkey == NULL) {
  213. ERR_raise(ERR_LIB_DSA, DSA_R_NO_PARAMETERS_SET);
  214. return 0;
  215. }
  216. dsa = DSA_new();
  217. if (dsa == NULL)
  218. return 0;
  219. EVP_PKEY_assign_DSA(pkey, dsa);
  220. /* Note: if error return, pkey is freed by parent routine */
  221. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  222. return 0;
  223. return DSA_generate_key((DSA *)EVP_PKEY_get0_DSA(pkey));
  224. }
  225. static const EVP_PKEY_METHOD dsa_pkey_meth = {
  226. EVP_PKEY_DSA,
  227. EVP_PKEY_FLAG_AUTOARGLEN,
  228. pkey_dsa_init,
  229. pkey_dsa_copy,
  230. pkey_dsa_cleanup,
  231. 0,
  232. pkey_dsa_paramgen,
  233. 0,
  234. pkey_dsa_keygen,
  235. 0,
  236. pkey_dsa_sign,
  237. 0,
  238. pkey_dsa_verify,
  239. 0, 0,
  240. 0, 0, 0, 0,
  241. 0, 0,
  242. 0, 0,
  243. 0, 0,
  244. pkey_dsa_ctrl,
  245. pkey_dsa_ctrl_str
  246. };
  247. const EVP_PKEY_METHOD *ossl_dsa_pkey_method(void)
  248. {
  249. return &dsa_pkey_meth;
  250. }