dh_ameth.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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/x509.h>
  60. #include <openssl/asn1.h>
  61. #include <openssl/dh.h>
  62. #include "asn1_locl.h"
  63. static void int_dh_free(EVP_PKEY *pkey)
  64. {
  65. DH_free(pkey->pkey.dh);
  66. }
  67. static int dh_pub_decode(EVP_PKEY *pkey, X509_PUBKEY *pubkey)
  68. {
  69. const unsigned char *p, *pm;
  70. int pklen, pmlen;
  71. int ptype;
  72. void *pval;
  73. ASN1_STRING *pstr;
  74. X509_ALGOR *palg;
  75. ASN1_INTEGER *public_key = NULL;
  76. DH *dh = NULL;
  77. if (!X509_PUBKEY_get0_param(NULL, &p, &pklen, &palg, pubkey))
  78. return 0;
  79. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  80. if (ptype != V_ASN1_SEQUENCE)
  81. {
  82. DHerr(DH_F_DH_PUB_DECODE, DH_R_PARAMETER_ENCODING_ERROR);
  83. goto err;
  84. }
  85. pstr = pval;
  86. pm = pstr->data;
  87. pmlen = pstr->length;
  88. if (!(dh = d2i_DHparams(NULL, &pm, pmlen)))
  89. {
  90. DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
  91. goto err;
  92. }
  93. if (!(public_key=d2i_ASN1_INTEGER(NULL, &p, pklen)))
  94. {
  95. DHerr(DH_F_DH_PUB_DECODE, DH_R_DECODE_ERROR);
  96. goto err;
  97. }
  98. /* We have parameters now set public key */
  99. if (!(dh->pub_key = ASN1_INTEGER_to_BN(public_key, NULL)))
  100. {
  101. DHerr(DH_F_DH_PUB_DECODE, DH_R_BN_DECODE_ERROR);
  102. goto err;
  103. }
  104. ASN1_INTEGER_free(public_key);
  105. EVP_PKEY_assign_DH(pkey, dh);
  106. return 1;
  107. err:
  108. if (pubkey)
  109. ASN1_INTEGER_free(public_key);
  110. if (dh)
  111. DH_free(dh);
  112. return 0;
  113. }
  114. static int dh_pub_encode(X509_PUBKEY *pk, const EVP_PKEY *pkey)
  115. {
  116. DH *dh;
  117. void *pval = NULL;
  118. int ptype;
  119. unsigned char *penc = NULL;
  120. int penclen;
  121. ASN1_STRING *str;
  122. ASN1_INTEGER *pub_key = NULL;
  123. dh=pkey->pkey.dh;
  124. str = ASN1_STRING_new();
  125. str->length = i2d_DHparams(dh, &str->data);
  126. if (str->length <= 0)
  127. {
  128. DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  129. goto err;
  130. }
  131. pval = str;
  132. ptype = V_ASN1_SEQUENCE;
  133. pub_key = BN_to_ASN1_INTEGER(dh->pub_key, NULL);
  134. if (!pub_key)
  135. goto err;
  136. penclen = i2d_ASN1_INTEGER(pub_key, &penc);
  137. ASN1_INTEGER_free(pub_key);
  138. if (penclen <= 0)
  139. {
  140. DHerr(DH_F_DH_PUB_ENCODE, ERR_R_MALLOC_FAILURE);
  141. goto err;
  142. }
  143. if (X509_PUBKEY_set0_param(pk, OBJ_nid2obj(EVP_PKEY_DH),
  144. ptype, pval, penc, penclen))
  145. return 1;
  146. err:
  147. if (penc)
  148. OPENSSL_free(penc);
  149. if (pval)
  150. ASN1_STRING_free(pval);
  151. return 0;
  152. }
  153. /* PKCS#8 DH is defined in PKCS#11 of all places. It is similar to DH in
  154. * that the AlgorithmIdentifier contains the paramaters, the private key
  155. * is explcitly included and the pubkey must be recalculated.
  156. */
  157. static int dh_priv_decode(EVP_PKEY *pkey, PKCS8_PRIV_KEY_INFO *p8)
  158. {
  159. const unsigned char *p, *pm;
  160. int pklen, pmlen;
  161. int ptype;
  162. void *pval;
  163. ASN1_STRING *pstr;
  164. X509_ALGOR *palg;
  165. ASN1_INTEGER *privkey = NULL;
  166. DH *dh = NULL;
  167. if (!PKCS8_pkey_get0(NULL, &p, &pklen, &palg, p8))
  168. return 0;
  169. X509_ALGOR_get0(NULL, &ptype, &pval, palg);
  170. if (ptype != V_ASN1_SEQUENCE)
  171. goto decerr;
  172. if (!(privkey=d2i_ASN1_INTEGER(NULL, &p, pklen)))
  173. goto decerr;
  174. pstr = pval;
  175. pm = pstr->data;
  176. pmlen = pstr->length;
  177. if (!(dh = d2i_DHparams(NULL, &pm, pmlen)))
  178. goto decerr;
  179. /* We have parameters now set private key */
  180. if (!(dh->priv_key = ASN1_INTEGER_to_BN(privkey, NULL)))
  181. {
  182. DHerr(DH_F_DH_PRIV_DECODE,DH_R_BN_ERROR);
  183. goto dherr;
  184. }
  185. /* Calculate public key */
  186. if (!DH_generate_key(dh))
  187. goto dherr;
  188. EVP_PKEY_assign_DH(pkey, dh);
  189. ASN1_INTEGER_free(privkey);
  190. return 1;
  191. decerr:
  192. DHerr(DH_F_DH_PRIV_DECODE, EVP_R_DECODE_ERROR);
  193. dherr:
  194. DH_free(dh);
  195. return 0;
  196. }
  197. static int dh_priv_encode(PKCS8_PRIV_KEY_INFO *p8, const EVP_PKEY *pkey)
  198. {
  199. ASN1_STRING *params = NULL;
  200. ASN1_INTEGER *prkey = NULL;
  201. unsigned char *dp = NULL;
  202. int dplen;
  203. params = ASN1_STRING_new();
  204. if (!params)
  205. {
  206. DHerr(DH_F_DH_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
  207. goto err;
  208. }
  209. params->length = i2d_DHparams(pkey->pkey.dh, &params->data);
  210. if (params->length <= 0)
  211. {
  212. DHerr(DH_F_DH_PRIV_ENCODE,ERR_R_MALLOC_FAILURE);
  213. goto err;
  214. }
  215. params->type = V_ASN1_SEQUENCE;
  216. /* Get private key into integer */
  217. prkey = BN_to_ASN1_INTEGER(pkey->pkey.dh->priv_key, NULL);
  218. if (!prkey)
  219. {
  220. DHerr(DH_F_DH_PRIV_ENCODE,DH_R_BN_ERROR);
  221. goto err;
  222. }
  223. dplen = i2d_ASN1_INTEGER(prkey, &dp);
  224. ASN1_INTEGER_free(prkey);
  225. if (!PKCS8_pkey_set0(p8, OBJ_nid2obj(NID_dhKeyAgreement), 0,
  226. V_ASN1_SEQUENCE, params, dp, dplen))
  227. goto err;
  228. return 1;
  229. err:
  230. if (dp != NULL)
  231. OPENSSL_free(dp);
  232. if (params != NULL)
  233. ASN1_STRING_free(params);
  234. if (prkey != NULL)
  235. ASN1_INTEGER_free(prkey);
  236. return 0;
  237. }
  238. static void update_buflen(const BIGNUM *b, size_t *pbuflen)
  239. {
  240. size_t i;
  241. if (!b)
  242. return;
  243. if (*pbuflen < (i = (size_t)BN_num_bytes(b)))
  244. *pbuflen = i;
  245. }
  246. static int dh_param_decode(EVP_PKEY *pkey,
  247. const unsigned char **pder, int derlen)
  248. {
  249. DH *dh;
  250. if (!(dh = d2i_DHparams(NULL, pder, derlen)))
  251. {
  252. DHerr(DH_F_DH_PARAM_DECODE, ERR_R_DH_LIB);
  253. return 0;
  254. }
  255. EVP_PKEY_assign_DH(pkey, dh);
  256. return 1;
  257. }
  258. static int dh_param_encode(const EVP_PKEY *pkey, unsigned char **pder)
  259. {
  260. return i2d_DHparams(pkey->pkey.dh, pder);
  261. }
  262. static int do_dh_print(BIO *bp, const DH *x, int indent,
  263. ASN1_PCTX *ctx, int ptype)
  264. {
  265. unsigned char *m=NULL;
  266. int reason=ERR_R_BUF_LIB,ret=0;
  267. size_t buf_len=0;
  268. const char *ktype = NULL;
  269. BIGNUM *priv_key, *pub_key;
  270. if (ptype == 2)
  271. priv_key = x->priv_key;
  272. else
  273. priv_key = NULL;
  274. if (ptype > 0)
  275. pub_key = x->pub_key;
  276. else
  277. pub_key = NULL;
  278. update_buflen(x->p, &buf_len);
  279. if (buf_len == 0)
  280. {
  281. reason = ERR_R_PASSED_NULL_PARAMETER;
  282. goto err;
  283. }
  284. update_buflen(x->g, &buf_len);
  285. update_buflen(pub_key, &buf_len);
  286. update_buflen(priv_key, &buf_len);
  287. if (ptype == 2)
  288. ktype = "PKCS#3 DH Private-Key";
  289. else if (ptype == 1)
  290. ktype = "PKCS#3 DH Public-Key";
  291. else
  292. ktype = "PKCS#3 DH Parameters";
  293. m= OPENSSL_malloc(buf_len+10);
  294. if (m == NULL)
  295. {
  296. reason=ERR_R_MALLOC_FAILURE;
  297. goto err;
  298. }
  299. BIO_indent(bp, indent, 128);
  300. if (BIO_printf(bp,"%s: (%d bit)\n", ktype, BN_num_bits(x->p)) <= 0)
  301. goto err;
  302. indent += 4;
  303. if (!ASN1_bn_print(bp,"private-key:",priv_key,m,indent)) goto err;
  304. if (!ASN1_bn_print(bp,"public-key:",pub_key,m,indent)) goto err;
  305. if (!ASN1_bn_print(bp,"prime:",x->p,m,indent)) goto err;
  306. if (!ASN1_bn_print(bp,"generator:",x->g,m,indent)) goto err;
  307. if (x->length != 0)
  308. {
  309. BIO_indent(bp, indent, 128);
  310. if (BIO_printf(bp,"recommended-private-length: %d bits\n",
  311. (int)x->length) <= 0) goto err;
  312. }
  313. ret=1;
  314. if (0)
  315. {
  316. err:
  317. DHerr(DH_F_DHPARAMS_PRINT,reason);
  318. }
  319. if (m != NULL) OPENSSL_free(m);
  320. return(ret);
  321. }
  322. static int int_dh_size(const EVP_PKEY *pkey)
  323. {
  324. return(DH_size(pkey->pkey.dh));
  325. }
  326. static int dh_bits(const EVP_PKEY *pkey)
  327. {
  328. return BN_num_bits(pkey->pkey.dh->p);
  329. }
  330. static int dh_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  331. {
  332. if ( BN_cmp(a->pkey.dh->p,b->pkey.dh->p) ||
  333. BN_cmp(a->pkey.dh->g,b->pkey.dh->g))
  334. return 0;
  335. else
  336. return 1;
  337. }
  338. static int dh_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  339. {
  340. BIGNUM *a;
  341. if ((a=BN_dup(from->pkey.dh->p)) == NULL)
  342. return 0;
  343. if (to->pkey.dh->p != NULL)
  344. BN_free(to->pkey.dh->p);
  345. to->pkey.dh->p=a;
  346. if ((a=BN_dup(from->pkey.dh->g)) == NULL)
  347. return 0;
  348. if (to->pkey.dh->g != NULL)
  349. BN_free(to->pkey.dh->g);
  350. to->pkey.dh->g=a;
  351. return 1;
  352. }
  353. static int dh_missing_parameters(const EVP_PKEY *a)
  354. {
  355. if (!a->pkey.dh->p || !a->pkey.dh->g)
  356. return 1;
  357. return 0;
  358. }
  359. static int dh_pub_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  360. {
  361. if (dh_cmp_parameters(a, b) == 0)
  362. return 0;
  363. if (BN_cmp(b->pkey.dh->pub_key,a->pkey.dh->pub_key) != 0)
  364. return 0;
  365. else
  366. return 1;
  367. }
  368. static int dh_param_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  369. ASN1_PCTX *ctx)
  370. {
  371. return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 0);
  372. }
  373. static int dh_public_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  374. ASN1_PCTX *ctx)
  375. {
  376. return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 1);
  377. }
  378. static int dh_private_print(BIO *bp, const EVP_PKEY *pkey, int indent,
  379. ASN1_PCTX *ctx)
  380. {
  381. return do_dh_print(bp, pkey->pkey.dh, indent, ctx, 2);
  382. }
  383. int DHparams_print(BIO *bp, const DH *x)
  384. {
  385. return do_dh_print(bp, x, 4, NULL, 0);
  386. }
  387. const EVP_PKEY_ASN1_METHOD dh_asn1_meth =
  388. {
  389. EVP_PKEY_DH,
  390. EVP_PKEY_DH,
  391. 0,
  392. "DH",
  393. "OpenSSL PKCS#3 DH method",
  394. dh_pub_decode,
  395. dh_pub_encode,
  396. dh_pub_cmp,
  397. dh_public_print,
  398. dh_priv_decode,
  399. dh_priv_encode,
  400. dh_private_print,
  401. int_dh_size,
  402. dh_bits,
  403. dh_param_decode,
  404. dh_param_encode,
  405. dh_missing_parameters,
  406. dh_copy_parameters,
  407. dh_cmp_parameters,
  408. dh_param_print,
  409. int_dh_free,
  410. 0
  411. };