dsa_pmeth.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. /*
  2. * Copyright 2006-2016 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. return EVP_PKEY_CTX_ctrl(ctx, EVP_PKEY_DSA, EVP_PKEY_OP_PARAMGEN,
  165. EVP_PKEY_CTRL_DSA_PARAMGEN_MD, 0,
  166. (void *)EVP_get_digestbyname(value));
  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;
  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. ret = dsa_builtin_paramgen(dsa, dctx->nbits, dctx->qbits, dctx->pmd,
  189. NULL, 0, NULL, NULL, NULL, pcb);
  190. BN_GENCB_free(pcb);
  191. if (ret)
  192. EVP_PKEY_assign_DSA(pkey, dsa);
  193. else
  194. DSA_free(dsa);
  195. return ret;
  196. }
  197. static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  198. {
  199. DSA *dsa = NULL;
  200. if (ctx->pkey == NULL) {
  201. DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET);
  202. return 0;
  203. }
  204. dsa = DSA_new();
  205. if (dsa == NULL)
  206. return 0;
  207. EVP_PKEY_assign_DSA(pkey, dsa);
  208. /* Note: if error return, pkey is freed by parent routine */
  209. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  210. return 0;
  211. return DSA_generate_key(pkey->pkey.dsa);
  212. }
  213. const EVP_PKEY_METHOD dsa_pkey_meth = {
  214. EVP_PKEY_DSA,
  215. EVP_PKEY_FLAG_AUTOARGLEN,
  216. pkey_dsa_init,
  217. pkey_dsa_copy,
  218. pkey_dsa_cleanup,
  219. 0,
  220. pkey_dsa_paramgen,
  221. 0,
  222. pkey_dsa_keygen,
  223. 0,
  224. pkey_dsa_sign,
  225. 0,
  226. pkey_dsa_verify,
  227. 0, 0,
  228. 0, 0, 0, 0,
  229. 0, 0,
  230. 0, 0,
  231. 0, 0,
  232. pkey_dsa_ctrl,
  233. pkey_dsa_ctrl_str
  234. };