dh_pmeth.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. /* Written by Dr Stephen N Henson (steve@openssl.org) 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/evp.h>
  62. #include <openssl/dh.h>
  63. #include <openssl/bn.h>
  64. #include "evp_locl.h"
  65. /* DH pkey context structure */
  66. typedef struct
  67. {
  68. /* Parameter gen parameters */
  69. int prime_len;
  70. int generator;
  71. int use_dsa;
  72. /* Keygen callback info */
  73. int gentmp[2];
  74. /* message digest */
  75. } DH_PKEY_CTX;
  76. static int pkey_dh_init(EVP_PKEY_CTX *ctx)
  77. {
  78. DH_PKEY_CTX *dctx;
  79. dctx = OPENSSL_malloc(sizeof(DH_PKEY_CTX));
  80. if (!dctx)
  81. return 0;
  82. dctx->prime_len = 1024;
  83. dctx->generator = 2;
  84. dctx->use_dsa = 0;
  85. ctx->data = dctx;
  86. ctx->keygen_info = dctx->gentmp;
  87. ctx->keygen_info_count = 2;
  88. return 1;
  89. }
  90. static int pkey_dh_copy(EVP_PKEY_CTX *dst, EVP_PKEY_CTX *src)
  91. {
  92. DH_PKEY_CTX *dctx, *sctx;
  93. if (!pkey_dh_init(dst))
  94. return 0;
  95. sctx = src->data;
  96. dctx = dst->data;
  97. dctx->prime_len = sctx->prime_len;
  98. dctx->generator = sctx->generator;
  99. dctx->use_dsa = sctx->use_dsa;
  100. return 1;
  101. }
  102. static void pkey_dh_cleanup(EVP_PKEY_CTX *ctx)
  103. {
  104. DH_PKEY_CTX *dctx = ctx->data;
  105. if (dctx)
  106. OPENSSL_free(dctx);
  107. }
  108. static int pkey_dh_ctrl(EVP_PKEY_CTX *ctx, int type, int p1, void *p2)
  109. {
  110. DH_PKEY_CTX *dctx = ctx->data;
  111. switch (type)
  112. {
  113. case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
  114. if (p1 < 256)
  115. return -2;
  116. dctx->prime_len = p1;
  117. return 1;
  118. case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
  119. dctx->generator = p1;
  120. return 1;
  121. case EVP_PKEY_CTRL_PEER_KEY:
  122. /* Default behaviour is OK */
  123. return 1;
  124. default:
  125. return -2;
  126. }
  127. }
  128. static int pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx,
  129. const char *type, const char *value)
  130. {
  131. if (!strcmp(type, "dh_paramgen_prime_len"))
  132. {
  133. int len;
  134. len = atoi(value);
  135. return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
  136. }
  137. if (!strcmp(type, "dh_paramgen_generator"))
  138. {
  139. int len;
  140. len = atoi(value);
  141. return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
  142. }
  143. return -2;
  144. }
  145. static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  146. {
  147. DH *dh = NULL;
  148. DH_PKEY_CTX *dctx = ctx->data;
  149. BN_GENCB *pcb, cb;
  150. int ret;
  151. if (ctx->pkey_gencb)
  152. {
  153. pcb = &cb;
  154. evp_pkey_set_cb_translate(pcb, ctx);
  155. }
  156. else
  157. pcb = NULL;
  158. dh = DH_new();
  159. if (!dh)
  160. return 0;
  161. ret = DH_generate_parameters_ex(dh,
  162. dctx->prime_len, dctx->generator, pcb);
  163. if (ret)
  164. EVP_PKEY_assign_DH(pkey, dh);
  165. else
  166. DH_free(dh);
  167. return ret;
  168. }
  169. static int pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  170. {
  171. DH *dh = NULL;
  172. if (ctx->pkey == NULL)
  173. {
  174. DHerr(DH_F_PKEY_DH_KEYGEN, DH_R_NO_PARAMETERS_SET);
  175. return 0;
  176. }
  177. dh = DH_new();
  178. if (!dh)
  179. return 0;
  180. EVP_PKEY_assign_DH(pkey, dh);
  181. /* Note: if error return, pkey is freed by parent routine */
  182. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  183. return 0;
  184. return DH_generate_key(pkey->pkey.dh);
  185. }
  186. static int pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key, size_t *keylen)
  187. {
  188. int ret;
  189. if (!ctx->pkey || !ctx->peerkey)
  190. {
  191. DHerr(DH_F_PKEY_DH_DERIVE, DH_R_KEYS_NOT_SET);
  192. return 0;
  193. }
  194. ret = DH_compute_key(key, ctx->peerkey->pkey.dh->pub_key,
  195. ctx->pkey->pkey.dh);
  196. if (ret < 0)
  197. return ret;
  198. *keylen = ret;
  199. return 1;
  200. }
  201. const EVP_PKEY_METHOD dh_pkey_meth =
  202. {
  203. EVP_PKEY_DH,
  204. EVP_PKEY_FLAG_AUTOARGLEN,
  205. pkey_dh_init,
  206. pkey_dh_copy,
  207. pkey_dh_cleanup,
  208. 0,
  209. pkey_dh_paramgen,
  210. 0,
  211. pkey_dh_keygen,
  212. 0,
  213. 0,
  214. 0,
  215. 0,
  216. 0,0,
  217. 0,0,0,0,
  218. 0,0,
  219. 0,0,
  220. 0,
  221. pkey_dh_derive,
  222. pkey_dh_ctrl,
  223. pkey_dh_ctrl_str
  224. };