2
0

dh_pmeth.c 6.0 KB

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