dsa_pmeth.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (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. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/asn1t.h>
  12. #include <openssl/x509.h>
  13. #include <openssl/evp.h>
  14. #include <openssl/bn.h>
  15. #include "internal/evp_int.h"
  16. #include "dsa_locl.h"
  17. /* DSA pkey context structure */
  18. typedef struct {
  19. /* Parameter gen parameters */
  20. int nbits; /* size of p in bits (default: 1024) */
  21. int qbits; /* size of q in bits (default: 160) */
  22. const EVP_MD *pmd; /* MD for parameter generation */
  23. /* Keygen callback info */
  24. int gentmp[2];
  25. /* message digest */
  26. const EVP_MD *md; /* MD for the signature */
  27. } DSA_PKEY_CTX;
  28. static int pkey_dsa_init(EVP_PKEY_CTX *ctx)
  29. {
  30. DSA_PKEY_CTX *dctx = OPENSSL_malloc(sizeof(*dctx));
  31. if (dctx == NULL)
  32. return 0;
  33. dctx->nbits = 1024;
  34. dctx->qbits = 160;
  35. dctx->pmd = NULL;
  36. dctx->md = NULL;
  37. ctx->data = dctx;
  38. ctx->keygen_info = dctx->gentmp;
  39. ctx->keygen_info_count = 2;
  40. return 1;
  41. }
  42. static int pkey_dsa_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  43. {
  44. DSA_PKEY_CTX *dctx, *sctx;
  45. if (!pkey_dsa_init(dst))
  46. return 0;
  47. sctx = src->data;
  48. dctx = dst->data;
  49. dctx->nbits = sctx->nbits;
  50. dctx->qbits = sctx->qbits;
  51. dctx->pmd = sctx->pmd;
  52. dctx->md = sctx->md;
  53. return 1;
  54. }
  55. static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
  56. {
  57. DSA_PKEY_CTX *dctx = ctx->data;
  58. OPENSSL_free(dctx);
  59. }
  60. static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig,
  61. size_t *siglen, const unsigned char *tbs,
  62. size_t tbslen)
  63. {
  64. int ret;
  65. unsigned int sltmp;
  66. DSA_PKEY_CTX *dctx = ctx->data;
  67. DSA *dsa = ctx->pkey->pkey.dsa;
  68. if (dctx->md) {
  69. if (tbslen != (size_t)EVP_MD_size(dctx->md))
  70. return 0;
  71. } else {
  72. if (tbslen != SHA_DIGEST_LENGTH)
  73. return 0;
  74. }
  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) {
  89. if (tbslen != (size_t)EVP_MD_size(dctx->md))
  90. return 0;
  91. } else {
  92. if (tbslen != SHA_DIGEST_LENGTH)
  93. return 0;
  94. }
  95. ret = DSA_verify(0, tbs, tbslen, sig, siglen, dsa);
  96. return ret;
  97. }
  98. static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  99. {
  100. DSA_PKEY_CTX *dctx = ctx->data;
  101. switch (type) {
  102. case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
  103. if (p1 < 256)
  104. return -2;
  105. dctx->nbits = p1;
  106. return 1;
  107. case EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS:
  108. if (p1 != 160 && p1 != 224 && p1 && p1 != 256)
  109. return -2;
  110. dctx->qbits = p1;
  111. return 1;
  112. case EVP_PKEY_CTRL_DSA_PARAMGEN_MD:
  113. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
  114. EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
  115. EVP_MD_type((const EVP_MD *)p2) != NID_sha256) {
  116. DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
  117. return 0;
  118. }
  119. dctx->pmd = p2;
  120. return 1;
  121. case EVP_PKEY_CTRL_MD:
  122. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1 &&
  123. EVP_MD_type((const EVP_MD *)p2) != NID_dsa &&
  124. EVP_MD_type((const EVP_MD *)p2) != NID_dsaWithSHA &&
  125. EVP_MD_type((const EVP_MD *)p2) != NID_sha224 &&
  126. EVP_MD_type((const EVP_MD *)p2) != NID_sha256 &&
  127. EVP_MD_type((const EVP_MD *)p2) != NID_sha384 &&
  128. EVP_MD_type((const EVP_MD *)p2) != NID_sha512) {
  129. DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
  130. return 0;
  131. }
  132. dctx->md = p2;
  133. return 1;
  134. case EVP_PKEY_CTRL_GET_MD:
  135. *(const EVP_MD **)p2 = dctx->md;
  136. return 1;
  137. case EVP_PKEY_CTRL_DIGESTINIT:
  138. case EVP_PKEY_CTRL_PKCS7_SIGN:
  139. case EVP_PKEY_CTRL_CMS_SIGN:
  140. return 1;
  141. case EVP_PKEY_CTRL_PEER_KEY:
  142. DSAerr(DSA_F_PKEY_DSA_CTRL,
  143. EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  144. return -2;
  145. default:
  146. return -2;
  147. }
  148. }
  149. static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
  150. const char *type, const char *value)
  151. {
  152. if (strcmp(type, "dsa_paramgen_bits") == 0) {
  153. int nbits;
  154. nbits = atoi(value);
  155. return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
  156. }
  157. if (strcmp(type, "dsa_paramgen_q_bits") == 0) {
  158. int qbits = atoi(value);
  159. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
  160. EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, qbits,
  161. NULL);
  162. }
  163. if (strcmp(type, "dsa_paramgen_md") == 0) {
  164. const EVP_MD *md = EVP_get_digestbyname(value);
  165. if (md == NULL) {
  166. DSAerr(DSA_F_PKEY_DSA_CTRL_STR, DSA_R_INVALID_DIGEST_TYPE);
  167. return 0;
  168. }
  169. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
  170. EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0,
  171. (void *)md);
  172. }
  173. return -2;
  174. }
  175. static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  176. {
  177. DSA *dsa = NULL;
  178. DSA_PKEY_CTX *dctx = ctx->data;
  179. BN_GENCB *pcb;
  180. int ret;
  181. if (ctx->pkey_gencb) {
  182. pcb = BN_GENCB_new();
  183. if (pcb == NULL)
  184. return 0;
  185. evp_pkey_set_cb_translate(pcb, ctx);
  186. } else
  187. pcb = NULL;
  188. dsa = DSA_new();
  189. if (dsa == NULL) {
  190. BN_GENCB_free(pcb);
  191. return 0;
  192. }
  193. ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd,
  194. NULL, 0, NULL, NULL, NULL, pcb);
  195. BN_GENCB_free(pcb);
  196. if (ret)
  197. EVP_PKEY_assign_DSA(pkey, dsa);
  198. else
  199. DSA_free(dsa);
  200. return ret;
  201. }
  202. static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  203. {
  204. DSA *dsa = NULL;
  205. if (ctx->pkey == NULL) {
  206. DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET);
  207. return 0;
  208. }
  209. dsa = DSA_new();
  210. if (dsa == NULL)
  211. return 0;
  212. EVP_PKEY_assign_DSA(pkey, dsa);
  213. /* Note: if error return, pkey is freed by parent routine */
  214. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  215. return 0;
  216. return DSA_generate_key(pkey->pkey.dsa);
  217. }
  218. const EVP_PKEY_METHOD dsa_pkey_meth = {
  219. EVP_PKEY_DSA,
  220. EVP_PKEY_FLAG_AUTOARGLEN,
  221. pkey_dsa_init,
  222. pkey_dsa_copy,
  223. pkey_dsa_cleanup,
  224. 0,
  225. pkey_dsa_paramgen,
  226. 0,
  227. pkey_dsa_keygen,
  228. 0,
  229. pkey_dsa_sign,
  230. 0,
  231. pkey_dsa_verify,
  232. 0, 0,
  233. 0, 0, 0, 0,
  234. 0, 0,
  235. 0, 0,
  236. 0, 0,
  237. pkey_dsa_ctrl,
  238. pkey_dsa_ctrl_str
  239. };