dsa_pmeth.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Written by Dr Stephen N Henson (shenson@bigfoot.com) for the OpenSSL
  2. * project 2006.
  3. */
  4. /* ====================================================================
  5. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. All advertising materials mentioning features or use of this
  20. * software must display the following acknowledgment:
  21. * "This product includes software developed by the OpenSSL Project
  22. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  23. *
  24. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  25. * endorse or promote products derived from this software without
  26. * prior written permission. For written permission, please contact
  27. * licensing@OpenSSL.org.
  28. *
  29. * 5. Products derived from this software may not be called "OpenSSL"
  30. * nor may "OpenSSL" appear in their names without prior written
  31. * permission of the OpenSSL Project.
  32. *
  33. * 6. Redistributions of any form whatsoever must retain the following
  34. * acknowledgment:
  35. * "This product includes software developed by the OpenSSL Project
  36. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  39. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  41. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  42. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  43. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  44. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  45. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  46. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  47. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  48. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  49. * OF THE POSSIBILITY OF SUCH DAMAGE.
  50. * ====================================================================
  51. *
  52. * This product includes cryptographic software written by Eric Young
  53. * (eay@cryptsoft.com). This product includes software written by Tim
  54. * Hudson (tjh@cryptsoft.com).
  55. *
  56. */
  57. #include <stdio.h>
  58. #include "cryptlib.h"
  59. #include <openssl/asn1t.h>
  60. #include <openssl/x509.h>
  61. #include <openssl/rsa.h>
  62. #include <openssl/evp.h>
  63. #include "evp_locl.h"
  64. /* DSA pkey context structure */
  65. typedef struct
  66. {
  67. /* Parameter gen parameters */
  68. int nbits;
  69. /* Keygen callback info */
  70. int gentmp[2];
  71. /* message digest */
  72. const EVP_MD *md;
  73. } DSA_PKEY_CTX;
  74. static int pkey_dsa_init(EVP_PKEY_CTX *ctx)
  75. {
  76. DSA_PKEY_CTX *dctx;
  77. dctx = OPENSSL_malloc(sizeof(DSA_PKEY_CTX));
  78. if (!dctx)
  79. return 0;
  80. dctx->nbits = 1024;
  81. dctx->md = NULL;
  82. ctx->data = dctx;
  83. ctx->keygen_info = dctx->gentmp;
  84. ctx->keygen_info_count = 2;
  85. return 1;
  86. }
  87. static void pkey_dsa_cleanup(EVP_PKEY_CTX *ctx)
  88. {
  89. DSA_PKEY_CTX *dctx = ctx->data;
  90. if (dctx)
  91. OPENSSL_free(dctx);
  92. }
  93. static int pkey_dsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, int *siglen,
  94. const unsigned char *tbs, int tbslen)
  95. {
  96. int ret, type;
  97. unsigned int sltmp;
  98. DSA_PKEY_CTX *dctx = ctx->data;
  99. DSA *dsa = ctx->pkey->pkey.dsa;
  100. if (dctx->md)
  101. type = EVP_MD_type(dctx->md);
  102. else
  103. type = NID_sha1;
  104. ret = DSA_sign(type, tbs, tbslen, sig, &sltmp, dsa);
  105. if (ret < 0)
  106. return ret;
  107. *siglen = sltmp;
  108. return 1;
  109. }
  110. static int pkey_dsa_verify(EVP_PKEY_CTX *ctx,
  111. const unsigned char *sig, int siglen,
  112. const unsigned char *tbs, int tbslen)
  113. {
  114. int ret, type;
  115. DSA_PKEY_CTX *dctx = ctx->data;
  116. DSA *dsa = ctx->pkey->pkey.dsa;
  117. if (dctx->md)
  118. type = EVP_MD_type(dctx->md);
  119. else
  120. type = NID_sha1;
  121. ret = DSA_verify(type, tbs, tbslen, sig, siglen, dsa);
  122. return ret;
  123. }
  124. static int pkey_dsa_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  125. {
  126. DSA_PKEY_CTX *dctx = ctx->data;
  127. switch (type)
  128. {
  129. case EVP_PKEY_CTRL_DSA_PARAMGEN_BITS:
  130. if (p1 < 256)
  131. return -2;
  132. dctx->nbits = p1;
  133. return 1;
  134. case EVP_PKEY_CTRL_MD:
  135. if (EVP_MD_type((const EVP_MD *)p2) != NID_sha1)
  136. {
  137. DSAerr(DSA_F_PKEY_DSA_CTRL, DSA_R_INVALID_DIGEST_TYPE);
  138. return 0;
  139. }
  140. dctx->md = p2;
  141. return 1;
  142. default:
  143. return -2;
  144. }
  145. }
  146. static int pkey_dsa_ctrl_str(EVP_PKEY_CTX *ctx,
  147. const char *type, const char *value)
  148. {
  149. if (!strcmp(type, "dsa_paramgen_bits"))
  150. {
  151. int nbits;
  152. nbits = atoi(value);
  153. return EVP_PKEY_CTX_set_dsa_paramgen_bits(ctx, nbits);
  154. }
  155. return -2;
  156. }
  157. static int pkey_dsa_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  158. {
  159. DSA *dsa = NULL;
  160. DSA_PKEY_CTX *dctx = ctx->data;
  161. BN_GENCB *pcb, cb;
  162. int ret;
  163. if (ctx->pkey_gencb)
  164. {
  165. pcb = &cb;
  166. evp_pkey_set_cb_translate(pcb, ctx);
  167. }
  168. else
  169. pcb = NULL;
  170. dsa = DSA_new();
  171. if (!dsa)
  172. return 0;
  173. ret = DSA_generate_parameters_ex(dsa, dctx->nbits, NULL, 0, NULL, NULL,
  174. pcb);
  175. if (ret)
  176. EVP_PKEY_assign_DSA(pkey, dsa);
  177. else
  178. DSA_free(dsa);
  179. return ret;
  180. }
  181. static int pkey_dsa_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  182. {
  183. DSA *dsa = NULL;
  184. if (ctx->pkey == NULL)
  185. {
  186. DSAerr(DSA_F_PKEY_DSA_KEYGEN, DSA_R_NO_PARAMETERS_SET);
  187. return 0;
  188. }
  189. dsa = DSA_new();
  190. if (!dsa)
  191. return 0;
  192. EVP_PKEY_assign_DSA(pkey, dsa);
  193. /* Note: if error return, pkey is freed by parent routine */
  194. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  195. return 0;
  196. return DSA_generate_key(pkey->pkey.dsa);
  197. }
  198. const EVP_PKEY_METHOD dsa_pkey_meth =
  199. {
  200. EVP_PKEY_DSA,
  201. EVP_PKEY_FLAG_AUTOARGLEN,
  202. pkey_dsa_init,
  203. pkey_dsa_cleanup,
  204. 0,
  205. pkey_dsa_paramgen,
  206. 0,
  207. pkey_dsa_keygen,
  208. 0,
  209. pkey_dsa_sign,
  210. 0,
  211. pkey_dsa_verify,
  212. 0,0,
  213. 0,0,0,0,
  214. 0,0,
  215. 0,0,
  216. 0,0,
  217. pkey_dsa_ctrl,
  218. pkey_dsa_ctrl_str
  219. };