dh_pmeth.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. * Written by Dr Stephen N Henson ([email protected]) for the OpenSSL project
  3. * 2006.
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * [email protected].
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * ([email protected]). This product includes software written by Tim
  55. * Hudson ([email protected]).
  56. *
  57. */
  58. #include <stdio.h>
  59. #include "cryptlib.h"
  60. #include <openssl/asn1t.h>
  61. #include <openssl/x509.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. /* 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. case EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN:
  113. if (p1 < 256)
  114. return -2;
  115. dctx->prime_len = p1;
  116. return 1;
  117. case EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR:
  118. dctx->generator = p1;
  119. return 1;
  120. case EVP_PKEY_CTRL_PEER_KEY:
  121. /* Default behaviour is OK */
  122. return 1;
  123. default:
  124. return -2;
  125. }
  126. }
  127. static int pkey_dh_ctrl_str(EVP_PKEY_CTX *ctx,
  128. const char *type, const char *value)
  129. {
  130. if (!strcmp(type, "dh_paramgen_prime_len")) {
  131. int len;
  132. len = atoi(value);
  133. return EVP_PKEY_CTX_set_dh_paramgen_prime_len(ctx, len);
  134. }
  135. if (!strcmp(type, "dh_paramgen_generator")) {
  136. int len;
  137. len = atoi(value);
  138. return EVP_PKEY_CTX_set_dh_paramgen_generator(ctx, len);
  139. }
  140. return -2;
  141. }
  142. static int pkey_dh_paramgen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  143. {
  144. DH *dh = NULL;
  145. DH_PKEY_CTX *dctx = ctx->data;
  146. BN_GENCB *pcb, cb;
  147. int ret;
  148. if (ctx->pkey_gencb) {
  149. pcb = &cb;
  150. evp_pkey_set_cb_translate(pcb, ctx);
  151. } else
  152. pcb = NULL;
  153. dh = DH_new();
  154. if (!dh)
  155. return 0;
  156. ret = DH_generate_parameters_ex(dh,
  157. dctx->prime_len, dctx->generator, pcb);
  158. if (ret)
  159. EVP_PKEY_assign_DH(pkey, dh);
  160. else
  161. DH_free(dh);
  162. return ret;
  163. }
  164. static int pkey_dh_keygen(EVP_PKEY_CTX *ctx, EVP_PKEY *pkey)
  165. {
  166. DH *dh = NULL;
  167. if (ctx->pkey == NULL) {
  168. DHerr(DH_F_PKEY_DH_KEYGEN, DH_R_NO_PARAMETERS_SET);
  169. return 0;
  170. }
  171. dh = DH_new();
  172. if (!dh)
  173. return 0;
  174. EVP_PKEY_assign_DH(pkey, dh);
  175. /* Note: if error return, pkey is freed by parent routine */
  176. if (!EVP_PKEY_copy_parameters(pkey, ctx->pkey))
  177. return 0;
  178. return DH_generate_key(pkey->pkey.dh);
  179. }
  180. static int pkey_dh_derive(EVP_PKEY_CTX *ctx, unsigned char *key,
  181. size_t *keylen)
  182. {
  183. int ret;
  184. if (!ctx->pkey || !ctx->peerkey) {
  185. DHerr(DH_F_PKEY_DH_DERIVE, DH_R_KEYS_NOT_SET);
  186. return 0;
  187. }
  188. ret = DH_compute_key(key, ctx->peerkey->pkey.dh->pub_key,
  189. ctx->pkey->pkey.dh);
  190. if (ret < 0)
  191. return ret;
  192. *keylen = ret;
  193. return 1;
  194. }
  195. const EVP_PKEY_METHOD dh_pkey_meth = {
  196. EVP_PKEY_DH,
  197. EVP_PKEY_FLAG_AUTOARGLEN,
  198. pkey_dh_init,
  199. pkey_dh_copy,
  200. pkey_dh_cleanup,
  201. 0,
  202. pkey_dh_paramgen,
  203. 0,
  204. pkey_dh_keygen,
  205. 0,
  206. 0,
  207. 0,
  208. 0,
  209. 0, 0,
  210. 0, 0, 0, 0,
  211. 0, 0,
  212. 0, 0,
  213. 0,
  214. pkey_dh_derive,
  215. pkey_dh_ctrl,
  216. pkey_dh_ctrl_str
  217. };