t_pkey.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815
  1. /* crypto/asn1/t_pkey.c */
  2. /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
  3. * All rights reserved.
  4. *
  5. * This package is an SSL implementation written
  6. * by Eric Young (eay@cryptsoft.com).
  7. * The implementation was written so as to conform with Netscapes SSL.
  8. *
  9. * This library is free for commercial and non-commercial use as long as
  10. * the following conditions are aheared to. The following conditions
  11. * apply to all code found in this distribution, be it the RC4, RSA,
  12. * lhash, DES, etc., code; not just the SSL code. The SSL documentation
  13. * included with this distribution is covered by the same copyright terms
  14. * except that the holder is Tim Hudson (tjh@cryptsoft.com).
  15. *
  16. * Copyright remains Eric Young's, and as such any Copyright notices in
  17. * the code are not to be removed.
  18. * If this package is used in a product, Eric Young should be given attribution
  19. * as the author of the parts of the library used.
  20. * This can be in the form of a textual message at program startup or
  21. * in documentation (online or textual) provided with the package.
  22. *
  23. * Redistribution and use in source and binary forms, with or without
  24. * modification, are permitted provided that the following conditions
  25. * are met:
  26. * 1. Redistributions of source code must retain the copyright
  27. * notice, this list of conditions and the following disclaimer.
  28. * 2. Redistributions in binary form must reproduce the above copyright
  29. * notice, this list of conditions and the following disclaimer in the
  30. * documentation and/or other materials provided with the distribution.
  31. * 3. All advertising materials mentioning features or use of this software
  32. * must display the following acknowledgement:
  33. * "This product includes cryptographic software written by
  34. * Eric Young (eay@cryptsoft.com)"
  35. * The word 'cryptographic' can be left out if the rouines from the library
  36. * being used are not cryptographic related :-).
  37. * 4. If you include any Windows specific code (or a derivative thereof) from
  38. * the apps directory (application code) you must include an acknowledgement:
  39. * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
  40. *
  41. * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
  42. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  43. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  44. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  45. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  46. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  47. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  48. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  49. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  50. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  51. * SUCH DAMAGE.
  52. *
  53. * The licence and distribution terms for any publically available version or
  54. * derivative of this code cannot be changed. i.e. this code cannot simply be
  55. * copied and put under another distribution licence
  56. * [including the GNU Public Licence.]
  57. */
  58. /* ====================================================================
  59. * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
  60. * Binary polynomial ECC support in OpenSSL originally developed by
  61. * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
  62. */
  63. #include <stdio.h>
  64. #include "cryptlib.h"
  65. #include <openssl/objects.h>
  66. #include <openssl/buffer.h>
  67. #include <openssl/bn.h>
  68. #ifndef OPENSSL_NO_RSA
  69. #include <openssl/rsa.h>
  70. #endif
  71. #ifndef OPENSSL_NO_DH
  72. #include <openssl/dh.h>
  73. #endif
  74. #ifndef OPENSSL_NO_DSA
  75. #include <openssl/dsa.h>
  76. #endif
  77. #ifndef OPENSSL_NO_EC
  78. #include <openssl/ec.h>
  79. #endif
  80. static int print(BIO *fp,const char *str, const BIGNUM *num,
  81. unsigned char *buf,int off);
  82. #ifndef OPENSSL_NO_EC
  83. static int print_bin(BIO *fp, const char *str, const unsigned char *num,
  84. size_t len, int off);
  85. #endif
  86. #ifndef OPENSSL_NO_RSA
  87. #ifndef OPENSSL_NO_FP_API
  88. int RSA_print_fp(FILE *fp, const RSA *x, int off)
  89. {
  90. BIO *b;
  91. int ret;
  92. if ((b=BIO_new(BIO_s_file())) == NULL)
  93. {
  94. RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
  95. return(0);
  96. }
  97. BIO_set_fp(b,fp,BIO_NOCLOSE);
  98. ret=RSA_print(b,x,off);
  99. BIO_free(b);
  100. return(ret);
  101. }
  102. #endif
  103. int RSA_print(BIO *bp, const RSA *x, int off)
  104. {
  105. char str[128];
  106. const char *s;
  107. unsigned char *m=NULL;
  108. int ret=0;
  109. size_t buf_len=0, i;
  110. if (x->n)
  111. buf_len = (size_t)BN_num_bytes(x->n);
  112. if (x->e)
  113. if (buf_len < (i = (size_t)BN_num_bytes(x->e)))
  114. buf_len = i;
  115. if (x->d)
  116. if (buf_len < (i = (size_t)BN_num_bytes(x->d)))
  117. buf_len = i;
  118. if (x->p)
  119. if (buf_len < (i = (size_t)BN_num_bytes(x->p)))
  120. buf_len = i;
  121. if (x->q)
  122. if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
  123. buf_len = i;
  124. if (x->dmp1)
  125. if (buf_len < (i = (size_t)BN_num_bytes(x->dmp1)))
  126. buf_len = i;
  127. if (x->dmq1)
  128. if (buf_len < (i = (size_t)BN_num_bytes(x->dmq1)))
  129. buf_len = i;
  130. if (x->iqmp)
  131. if (buf_len < (i = (size_t)BN_num_bytes(x->iqmp)))
  132. buf_len = i;
  133. m=(unsigned char *)OPENSSL_malloc(buf_len+10);
  134. if (m == NULL)
  135. {
  136. RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
  137. goto err;
  138. }
  139. if (x->d != NULL)
  140. {
  141. if(!BIO_indent(bp,off,128))
  142. goto err;
  143. if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n))
  144. <= 0) goto err;
  145. }
  146. if (x->d == NULL)
  147. BIO_snprintf(str,sizeof str,"Modulus (%d bit):",BN_num_bits(x->n));
  148. else
  149. BUF_strlcpy(str,"modulus:",sizeof str);
  150. if (!print(bp,str,x->n,m,off)) goto err;
  151. s=(x->d == NULL)?"Exponent:":"publicExponent:";
  152. if (!print(bp,s,x->e,m,off)) goto err;
  153. if (!print(bp,"privateExponent:",x->d,m,off)) goto err;
  154. if (!print(bp,"prime1:",x->p,m,off)) goto err;
  155. if (!print(bp,"prime2:",x->q,m,off)) goto err;
  156. if (!print(bp,"exponent1:",x->dmp1,m,off)) goto err;
  157. if (!print(bp,"exponent2:",x->dmq1,m,off)) goto err;
  158. if (!print(bp,"coefficient:",x->iqmp,m,off)) goto err;
  159. ret=1;
  160. err:
  161. if (m != NULL) OPENSSL_free(m);
  162. return(ret);
  163. }
  164. #endif /* OPENSSL_NO_RSA */
  165. #ifndef OPENSSL_NO_DSA
  166. #ifndef OPENSSL_NO_FP_API
  167. int DSA_print_fp(FILE *fp, const DSA *x, int off)
  168. {
  169. BIO *b;
  170. int ret;
  171. if ((b=BIO_new(BIO_s_file())) == NULL)
  172. {
  173. DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB);
  174. return(0);
  175. }
  176. BIO_set_fp(b,fp,BIO_NOCLOSE);
  177. ret=DSA_print(b,x,off);
  178. BIO_free(b);
  179. return(ret);
  180. }
  181. #endif
  182. int DSA_print(BIO *bp, const DSA *x, int off)
  183. {
  184. unsigned char *m=NULL;
  185. int ret=0;
  186. size_t buf_len=0,i;
  187. if (x->p)
  188. buf_len = (size_t)BN_num_bytes(x->p);
  189. if (x->q)
  190. if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
  191. buf_len = i;
  192. if (x->g)
  193. if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
  194. buf_len = i;
  195. if (x->priv_key)
  196. if (buf_len < (i = (size_t)BN_num_bytes(x->priv_key)))
  197. buf_len = i;
  198. if (x->pub_key)
  199. if (buf_len < (i = (size_t)BN_num_bytes(x->pub_key)))
  200. buf_len = i;
  201. m=(unsigned char *)OPENSSL_malloc(buf_len+10);
  202. if (m == NULL)
  203. {
  204. DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);
  205. goto err;
  206. }
  207. if (x->priv_key != NULL)
  208. {
  209. if(!BIO_indent(bp,off,128))
  210. goto err;
  211. if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p))
  212. <= 0) goto err;
  213. }
  214. if ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off))
  215. goto err;
  216. if ((x->pub_key != NULL) && !print(bp,"pub: ",x->pub_key,m,off))
  217. goto err;
  218. if ((x->p != NULL) && !print(bp,"P: ",x->p,m,off)) goto err;
  219. if ((x->q != NULL) && !print(bp,"Q: ",x->q,m,off)) goto err;
  220. if ((x->g != NULL) && !print(bp,"G: ",x->g,m,off)) goto err;
  221. ret=1;
  222. err:
  223. if (m != NULL) OPENSSL_free(m);
  224. return(ret);
  225. }
  226. #endif /* !OPENSSL_NO_DSA */
  227. #ifndef OPENSSL_NO_EC
  228. #ifndef OPENSSL_NO_FP_API
  229. int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
  230. {
  231. BIO *b;
  232. int ret;
  233. if ((b=BIO_new(BIO_s_file())) == NULL)
  234. {
  235. ECerr(EC_F_ECPKPARAMETERS_PRINT_FP,ERR_R_BUF_LIB);
  236. return(0);
  237. }
  238. BIO_set_fp(b, fp, BIO_NOCLOSE);
  239. ret = ECPKParameters_print(b, x, off);
  240. BIO_free(b);
  241. return(ret);
  242. }
  243. int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
  244. {
  245. BIO *b;
  246. int ret;
  247. if ((b=BIO_new(BIO_s_file())) == NULL)
  248. {
  249. ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
  250. return(0);
  251. }
  252. BIO_set_fp(b, fp, BIO_NOCLOSE);
  253. ret = EC_KEY_print(b, x, off);
  254. BIO_free(b);
  255. return(ret);
  256. }
  257. #endif
  258. int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
  259. {
  260. unsigned char *buffer=NULL;
  261. size_t buf_len=0, i;
  262. int ret=0, reason=ERR_R_BIO_LIB;
  263. BN_CTX *ctx=NULL;
  264. const EC_POINT *point=NULL;
  265. BIGNUM *p=NULL, *a=NULL, *b=NULL, *gen=NULL,
  266. *order=NULL, *cofactor=NULL;
  267. const unsigned char *seed;
  268. size_t seed_len=0;
  269. static const char *gen_compressed = "Generator (compressed):";
  270. static const char *gen_uncompressed = "Generator (uncompressed):";
  271. static const char *gen_hybrid = "Generator (hybrid):";
  272. if (!x)
  273. {
  274. reason = ERR_R_PASSED_NULL_PARAMETER;
  275. goto err;
  276. }
  277. if (EC_GROUP_get_asn1_flag(x))
  278. {
  279. /* the curve parameter are given by an asn1 OID */
  280. int nid;
  281. if (!BIO_indent(bp, off, 128))
  282. goto err;
  283. nid = EC_GROUP_get_curve_name(x);
  284. if (nid == 0)
  285. goto err;
  286. if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
  287. goto err;
  288. if (BIO_printf(bp, "\n") <= 0)
  289. goto err;
  290. }
  291. else
  292. {
  293. /* explicit parameters */
  294. int is_char_two = 0;
  295. point_conversion_form_t form;
  296. int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
  297. if (tmp_nid == NID_X9_62_characteristic_two_field)
  298. is_char_two = 1;
  299. if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
  300. (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
  301. (cofactor = BN_new()) == NULL)
  302. {
  303. reason = ERR_R_MALLOC_FAILURE;
  304. goto err;
  305. }
  306. if (is_char_two)
  307. {
  308. if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx))
  309. {
  310. reason = ERR_R_EC_LIB;
  311. goto err;
  312. }
  313. }
  314. else /* prime field */
  315. {
  316. if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx))
  317. {
  318. reason = ERR_R_EC_LIB;
  319. goto err;
  320. }
  321. }
  322. if ((point = EC_GROUP_get0_generator(x)) == NULL)
  323. {
  324. reason = ERR_R_EC_LIB;
  325. goto err;
  326. }
  327. if (!EC_GROUP_get_order(x, order, NULL) ||
  328. !EC_GROUP_get_cofactor(x, cofactor, NULL))
  329. {
  330. reason = ERR_R_EC_LIB;
  331. goto err;
  332. }
  333. form = EC_GROUP_get_point_conversion_form(x);
  334. if ((gen = EC_POINT_point2bn(x, point,
  335. form, NULL, ctx)) == NULL)
  336. {
  337. reason = ERR_R_EC_LIB;
  338. goto err;
  339. }
  340. buf_len = (size_t)BN_num_bytes(p);
  341. if (buf_len < (i = (size_t)BN_num_bytes(a)))
  342. buf_len = i;
  343. if (buf_len < (i = (size_t)BN_num_bytes(b)))
  344. buf_len = i;
  345. if (buf_len < (i = (size_t)BN_num_bytes(gen)))
  346. buf_len = i;
  347. if (buf_len < (i = (size_t)BN_num_bytes(order)))
  348. buf_len = i;
  349. if (buf_len < (i = (size_t)BN_num_bytes(cofactor)))
  350. buf_len = i;
  351. if ((seed = EC_GROUP_get0_seed(x)) != NULL)
  352. seed_len = EC_GROUP_get_seed_len(x);
  353. buf_len += 10;
  354. if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
  355. {
  356. reason = ERR_R_MALLOC_FAILURE;
  357. goto err;
  358. }
  359. if (!BIO_indent(bp, off, 128))
  360. goto err;
  361. /* print the 'short name' of the field type */
  362. if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
  363. <= 0)
  364. goto err;
  365. if (is_char_two)
  366. {
  367. /* print the 'short name' of the base type OID */
  368. int basis_type = EC_GROUP_get_basis_type(x);
  369. if (basis_type == 0)
  370. goto err;
  371. if (!BIO_indent(bp, off, 128))
  372. goto err;
  373. if (BIO_printf(bp, "Basis Type: %s\n",
  374. OBJ_nid2sn(basis_type)) <= 0)
  375. goto err;
  376. /* print the polynomial */
  377. if ((p != NULL) && !print(bp, "Polynomial:", p, buffer,
  378. off))
  379. goto err;
  380. }
  381. else
  382. {
  383. if ((p != NULL) && !print(bp, "Prime:", p, buffer,off))
  384. goto err;
  385. }
  386. if ((a != NULL) && !print(bp, "A: ", a, buffer, off))
  387. goto err;
  388. if ((b != NULL) && !print(bp, "B: ", b, buffer, off))
  389. goto err;
  390. if (form == POINT_CONVERSION_COMPRESSED)
  391. {
  392. if ((gen != NULL) && !print(bp, gen_compressed, gen,
  393. buffer, off))
  394. goto err;
  395. }
  396. else if (form == POINT_CONVERSION_UNCOMPRESSED)
  397. {
  398. if ((gen != NULL) && !print(bp, gen_uncompressed, gen,
  399. buffer, off))
  400. goto err;
  401. }
  402. else /* form == POINT_CONVERSION_HYBRID */
  403. {
  404. if ((gen != NULL) && !print(bp, gen_hybrid, gen,
  405. buffer, off))
  406. goto err;
  407. }
  408. if ((order != NULL) && !print(bp, "Order: ", order,
  409. buffer, off)) goto err;
  410. if ((cofactor != NULL) && !print(bp, "Cofactor: ", cofactor,
  411. buffer, off)) goto err;
  412. if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
  413. goto err;
  414. }
  415. ret=1;
  416. err:
  417. if (!ret)
  418. ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
  419. if (p)
  420. BN_free(p);
  421. if (a)
  422. BN_free(a);
  423. if (b)
  424. BN_free(b);
  425. if (gen)
  426. BN_free(gen);
  427. if (order)
  428. BN_free(order);
  429. if (cofactor)
  430. BN_free(cofactor);
  431. if (ctx)
  432. BN_CTX_free(ctx);
  433. if (buffer != NULL)
  434. OPENSSL_free(buffer);
  435. return(ret);
  436. }
  437. int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
  438. {
  439. unsigned char *buffer=NULL;
  440. size_t buf_len=0, i;
  441. int ret=0, reason=ERR_R_BIO_LIB;
  442. BIGNUM *pub_key=NULL, *order=NULL;
  443. BN_CTX *ctx=NULL;
  444. const EC_GROUP *group;
  445. const EC_POINT *public_key;
  446. const BIGNUM *priv_key;
  447. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL)
  448. {
  449. reason = ERR_R_PASSED_NULL_PARAMETER;
  450. goto err;
  451. }
  452. public_key = EC_KEY_get0_public_key(x);
  453. if ((pub_key = EC_POINT_point2bn(group, public_key,
  454. EC_KEY_get_conv_form(x), NULL, ctx)) == NULL)
  455. {
  456. reason = ERR_R_EC_LIB;
  457. goto err;
  458. }
  459. buf_len = (size_t)BN_num_bytes(pub_key);
  460. priv_key = EC_KEY_get0_private_key(x);
  461. if (priv_key != NULL)
  462. {
  463. if ((i = (size_t)BN_num_bytes(priv_key)) > buf_len)
  464. buf_len = i;
  465. }
  466. buf_len += 10;
  467. if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
  468. {
  469. reason = ERR_R_MALLOC_FAILURE;
  470. goto err;
  471. }
  472. if (priv_key != NULL)
  473. {
  474. if (!BIO_indent(bp, off, 128))
  475. goto err;
  476. if ((order = BN_new()) == NULL)
  477. goto err;
  478. if (!EC_GROUP_get_order(group, order, NULL))
  479. goto err;
  480. if (BIO_printf(bp, "Private-Key: (%d bit)\n",
  481. BN_num_bits(order)) <= 0) goto err;
  482. }
  483. if ((priv_key != NULL) && !print(bp, "priv:", priv_key,
  484. buffer, off))
  485. goto err;
  486. if ((pub_key != NULL) && !print(bp, "pub: ", pub_key,
  487. buffer, off))
  488. goto err;
  489. if (!ECPKParameters_print(bp, group, off))
  490. goto err;
  491. ret=1;
  492. err:
  493. if (!ret)
  494. ECerr(EC_F_EC_KEY_PRINT, reason);
  495. if (pub_key)
  496. BN_free(pub_key);
  497. if (order)
  498. BN_free(order);
  499. if (ctx)
  500. BN_CTX_free(ctx);
  501. if (buffer != NULL)
  502. OPENSSL_free(buffer);
  503. return(ret);
  504. }
  505. #endif /* OPENSSL_NO_EC */
  506. static int print(BIO *bp, const char *number, const BIGNUM *num, unsigned char *buf,
  507. int off)
  508. {
  509. int n,i;
  510. const char *neg;
  511. if (num == NULL) return(1);
  512. neg = (BN_is_negative(num))?"-":"";
  513. if(!BIO_indent(bp,off,128))
  514. return 0;
  515. if (BN_is_zero(num))
  516. {
  517. if (BIO_printf(bp, "%s 0\n", number) <= 0)
  518. return 0;
  519. return 1;
  520. }
  521. if (BN_num_bytes(num) <= BN_BYTES)
  522. {
  523. if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg,
  524. (unsigned long)num->d[0],neg,(unsigned long)num->d[0])
  525. <= 0) return(0);
  526. }
  527. else
  528. {
  529. buf[0]=0;
  530. if (BIO_printf(bp,"%s%s",number,
  531. (neg[0] == '-')?" (Negative)":"") <= 0)
  532. return(0);
  533. n=BN_bn2bin(num,&buf[1]);
  534. if (buf[1] & 0x80)
  535. n++;
  536. else buf++;
  537. for (i=0; i<n; i++)
  538. {
  539. if ((i%15) == 0)
  540. {
  541. if(BIO_puts(bp,"\n") <= 0
  542. || !BIO_indent(bp,off+4,128))
  543. return 0;
  544. }
  545. if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
  546. <= 0) return(0);
  547. }
  548. if (BIO_write(bp,"\n",1) <= 0) return(0);
  549. }
  550. return(1);
  551. }
  552. #ifndef OPENSSL_NO_EC
  553. static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
  554. size_t len, int off)
  555. {
  556. size_t i;
  557. char str[128];
  558. if (buf == NULL)
  559. return 1;
  560. if (off)
  561. {
  562. if (off > 128)
  563. off=128;
  564. memset(str,' ',off);
  565. if (BIO_write(fp, str, off) <= 0)
  566. return 0;
  567. }
  568. if (BIO_printf(fp,"%s", name) <= 0)
  569. return 0;
  570. for (i=0; i<len; i++)
  571. {
  572. if ((i%15) == 0)
  573. {
  574. str[0]='\n';
  575. memset(&(str[1]),' ',off+4);
  576. if (BIO_write(fp, str, off+1+4) <= 0)
  577. return 0;
  578. }
  579. if (BIO_printf(fp,"%02x%s",buf[i],((i+1) == len)?"":":") <= 0)
  580. return 0;
  581. }
  582. if (BIO_write(fp,"\n",1) <= 0)
  583. return 0;
  584. return 1;
  585. }
  586. #endif
  587. #ifndef OPENSSL_NO_DH
  588. #ifndef OPENSSL_NO_FP_API
  589. int DHparams_print_fp(FILE *fp, const DH *x)
  590. {
  591. BIO *b;
  592. int ret;
  593. if ((b=BIO_new(BIO_s_file())) == NULL)
  594. {
  595. DHerr(DH_F_DHPARAMS_PRINT_FP,ERR_R_BUF_LIB);
  596. return(0);
  597. }
  598. BIO_set_fp(b,fp,BIO_NOCLOSE);
  599. ret=DHparams_print(b, x);
  600. BIO_free(b);
  601. return(ret);
  602. }
  603. #endif
  604. int DHparams_print(BIO *bp, const DH *x)
  605. {
  606. unsigned char *m=NULL;
  607. int reason=ERR_R_BUF_LIB,ret=0;
  608. size_t buf_len=0, i;
  609. if (x->p)
  610. buf_len = (size_t)BN_num_bytes(x->p);
  611. if (x->g)
  612. if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
  613. buf_len = i;
  614. m=(unsigned char *)OPENSSL_malloc(buf_len+10);
  615. if (m == NULL)
  616. {
  617. reason=ERR_R_MALLOC_FAILURE;
  618. goto err;
  619. }
  620. if (BIO_printf(bp,"Diffie-Hellman-Parameters: (%d bit)\n",
  621. BN_num_bits(x->p)) <= 0)
  622. goto err;
  623. if (!print(bp,"prime:",x->p,m,4)) goto err;
  624. if (!print(bp,"generator:",x->g,m,4)) goto err;
  625. if (x->length != 0)
  626. {
  627. if (BIO_printf(bp," recommended-private-length: %d bits\n",
  628. (int)x->length) <= 0) goto err;
  629. }
  630. ret=1;
  631. if (0)
  632. {
  633. err:
  634. DHerr(DH_F_DHPARAMS_PRINT,reason);
  635. }
  636. if (m != NULL) OPENSSL_free(m);
  637. return(ret);
  638. }
  639. #endif
  640. #ifndef OPENSSL_NO_DSA
  641. #ifndef OPENSSL_NO_FP_API
  642. int DSAparams_print_fp(FILE *fp, const DSA *x)
  643. {
  644. BIO *b;
  645. int ret;
  646. if ((b=BIO_new(BIO_s_file())) == NULL)
  647. {
  648. DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB);
  649. return(0);
  650. }
  651. BIO_set_fp(b,fp,BIO_NOCLOSE);
  652. ret=DSAparams_print(b, x);
  653. BIO_free(b);
  654. return(ret);
  655. }
  656. #endif
  657. int DSAparams_print(BIO *bp, const DSA *x)
  658. {
  659. unsigned char *m=NULL;
  660. int reason=ERR_R_BUF_LIB,ret=0;
  661. size_t buf_len=0,i;
  662. if (x->p)
  663. buf_len = (size_t)BN_num_bytes(x->p);
  664. if (x->q)
  665. if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
  666. buf_len = i;
  667. if (x->g)
  668. if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
  669. buf_len = i;
  670. m=(unsigned char *)OPENSSL_malloc(buf_len+10);
  671. if (m == NULL)
  672. {
  673. reason=ERR_R_MALLOC_FAILURE;
  674. goto err;
  675. }
  676. if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n",
  677. BN_num_bits(x->p)) <= 0)
  678. goto err;
  679. if (!print(bp,"p:",x->p,m,4)) goto err;
  680. if (!print(bp,"q:",x->q,m,4)) goto err;
  681. if (!print(bp,"g:",x->g,m,4)) goto err;
  682. ret=1;
  683. err:
  684. if (m != NULL) OPENSSL_free(m);
  685. DSAerr(DSA_F_DSAPARAMS_PRINT,reason);
  686. return(ret);
  687. }
  688. #endif /* !OPENSSL_NO_DSA */
  689. #ifndef OPENSSL_NO_EC
  690. #ifndef OPENSSL_NO_FP_API
  691. int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
  692. {
  693. BIO *b;
  694. int ret;
  695. if ((b=BIO_new(BIO_s_file())) == NULL)
  696. {
  697. ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
  698. return(0);
  699. }
  700. BIO_set_fp(b, fp, BIO_NOCLOSE);
  701. ret = ECParameters_print(b, x);
  702. BIO_free(b);
  703. return(ret);
  704. }
  705. #endif
  706. int ECParameters_print(BIO *bp, const EC_KEY *x)
  707. {
  708. int reason=ERR_R_EC_LIB, ret=0;
  709. BIGNUM *order=NULL;
  710. const EC_GROUP *group;
  711. if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL)
  712. {
  713. reason = ERR_R_PASSED_NULL_PARAMETER;;
  714. goto err;
  715. }
  716. if ((order = BN_new()) == NULL)
  717. {
  718. reason = ERR_R_MALLOC_FAILURE;
  719. goto err;
  720. }
  721. if (!EC_GROUP_get_order(group, order, NULL))
  722. {
  723. reason = ERR_R_EC_LIB;
  724. goto err;
  725. }
  726. if (BIO_printf(bp, "ECDSA-Parameters: (%d bit)\n",
  727. BN_num_bits(order)) <= 0)
  728. goto err;
  729. if (!ECPKParameters_print(bp, group, 4))
  730. goto err;
  731. ret=1;
  732. err:
  733. if (order)
  734. BN_free(order);
  735. ECerr(EC_F_ECPARAMETERS_PRINT, reason);
  736. return(ret);
  737. }
  738. #endif