dsa_pmeth.c 7.2 KB

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