]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/asn1/t_pkey.c
Add ECDH support.
[thirdparty/openssl.git] / crypto / asn1 / t_pkey.c
CommitLineData
d02b48c6 1/* crypto/asn1/t_pkey.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
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 */
e172d60d
BM
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 */
d02b48c6
RE
63
64#include <stdio.h>
65#include "cryptlib.h"
5f3d6f70 66#include <openssl/objects.h>
ec577822
BM
67#include <openssl/buffer.h>
68#include <openssl/bn.h>
cf1b7d96 69#ifndef OPENSSL_NO_RSA
ec577822 70#include <openssl/rsa.h>
d02b48c6 71#endif
cf1b7d96 72#ifndef OPENSSL_NO_DH
ec577822 73#include <openssl/dh.h>
d02b48c6 74#endif
cf1b7d96 75#ifndef OPENSSL_NO_DSA
ec577822 76#include <openssl/dsa.h>
d02b48c6 77#endif
14a7cfb3
BM
78#ifndef OPENSSL_NO_EC
79#include <openssl/ec.h>
4d94ae00 80#endif
d02b48c6 81
e778802f 82static int print(BIO *fp,const char *str,BIGNUM *num,
d02b48c6 83 unsigned char *buf,int off);
cf1b7d96
RL
84#ifndef OPENSSL_NO_RSA
85#ifndef OPENSSL_NO_FP_API
7081f3bd 86int RSA_print_fp(FILE *fp, const RSA *x, int off)
4d94ae00
BM
87 {
88 BIO *b;
89 int ret;
d02b48c6 90
4d94ae00 91 if ((b=BIO_new(BIO_s_file())) == NULL)
d02b48c6
RE
92 {
93 RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
4d94ae00 94 return(0);
d02b48c6 95 }
4d94ae00
BM
96 BIO_set_fp(b,fp,BIO_NOCLOSE);
97 ret=RSA_print(b,x,off);
98 BIO_free(b);
99 return(ret);
100 }
d02b48c6
RE
101#endif
102
7081f3bd 103int RSA_print(BIO *bp, const RSA *x, int off)
d02b48c6 104 {
e778802f
BL
105 char str[128];
106 const char *s;
d02b48c6 107 unsigned char *m=NULL;
9ae08a9c
BM
108 int ret=0;
109 size_t buf_len=0, i;
d02b48c6 110
9ae08a9c
BM
111 if (x->n)
112 buf_len = (size_t)BN_num_bytes(x->n);
113 if (x->e)
114 if (buf_len < (i = (size_t)BN_num_bytes(x->e)))
115 buf_len = i;
116 if (x->d)
117 if (buf_len < (i = (size_t)BN_num_bytes(x->d)))
118 buf_len = i;
119 if (x->p)
120 if (buf_len < (i = (size_t)BN_num_bytes(x->p)))
121 buf_len = i;
122 if (x->q)
123 if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
124 buf_len = i;
125 if (x->dmp1)
126 if (buf_len < (i = (size_t)BN_num_bytes(x->dmp1)))
127 buf_len = i;
128 if (x->dmq1)
129 if (buf_len < (i = (size_t)BN_num_bytes(x->dmq1)))
130 buf_len = i;
131 if (x->iqmp)
132 if (buf_len < (i = (size_t)BN_num_bytes(x->iqmp)))
133 buf_len = i;
134
135 m=(unsigned char *)OPENSSL_malloc(buf_len+10);
d02b48c6
RE
136 if (m == NULL)
137 {
138 RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
139 goto err;
140 }
141
142 if (off)
143 {
144 if (off > 128) off=128;
145 memset(str,' ',off);
146 }
147 if (x->d != NULL)
148 {
149 if (off && (BIO_write(bp,str,off) <= 0)) goto err;
150 if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->n))
151 <= 0) goto err;
152 }
153
154 if (x->d == NULL)
155 sprintf(str,"Modulus (%d bit):",BN_num_bits(x->n));
156 else
157 strcpy(str,"modulus:");
158 if (!print(bp,str,x->n,m,off)) goto err;
159 s=(x->d == NULL)?"Exponent:":"publicExponent:";
160 if (!print(bp,s,x->e,m,off)) goto err;
161 if (!print(bp,"privateExponent:",x->d,m,off)) goto err;
162 if (!print(bp,"prime1:",x->p,m,off)) goto err;
163 if (!print(bp,"prime2:",x->q,m,off)) goto err;
164 if (!print(bp,"exponent1:",x->dmp1,m,off)) goto err;
165 if (!print(bp,"exponent2:",x->dmq1,m,off)) goto err;
166 if (!print(bp,"coefficient:",x->iqmp,m,off)) goto err;
167 ret=1;
168err:
26a3a48d 169 if (m != NULL) OPENSSL_free(m);
d02b48c6
RE
170 return(ret);
171 }
cf1b7d96 172#endif /* OPENSSL_NO_RSA */
d02b48c6 173
cf1b7d96
RL
174#ifndef OPENSSL_NO_DSA
175#ifndef OPENSSL_NO_FP_API
a4aba800 176int DSA_print_fp(FILE *fp, const DSA *x, int off)
d02b48c6
RE
177 {
178 BIO *b;
179 int ret;
180
181 if ((b=BIO_new(BIO_s_file())) == NULL)
182 {
183 DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB);
184 return(0);
185 }
186 BIO_set_fp(b,fp,BIO_NOCLOSE);
187 ret=DSA_print(b,x,off);
188 BIO_free(b);
189 return(ret);
190 }
191#endif
192
a4aba800 193int DSA_print(BIO *bp, const DSA *x, int off)
d02b48c6
RE
194 {
195 char str[128];
196 unsigned char *m=NULL;
9ae08a9c
BM
197 int ret=0;
198 size_t buf_len=0,i;
199
200 if (x->p)
201 buf_len = (size_t)BN_num_bytes(x->p);
202 if (x->q)
203 if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
204 buf_len = i;
205 if (x->g)
206 if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
207 buf_len = i;
208 if (x->priv_key)
209 if (buf_len < (i = (size_t)BN_num_bytes(x->priv_key)))
210 buf_len = i;
211 if (x->pub_key)
212 if (buf_len < (i = (size_t)BN_num_bytes(x->pub_key)))
213 buf_len = i;
214
215 m=(unsigned char *)OPENSSL_malloc(buf_len+10);
d02b48c6
RE
216 if (m == NULL)
217 {
218 DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);
219 goto err;
220 }
221
222 if (off)
223 {
224 if (off > 128) off=128;
225 memset(str,' ',off);
226 }
227 if (x->priv_key != NULL)
228 {
229 if (off && (BIO_write(bp,str,off) <= 0)) goto err;
230 if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p))
231 <= 0) goto err;
232 }
233
234 if ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off))
235 goto err;
236 if ((x->pub_key != NULL) && !print(bp,"pub: ",x->pub_key,m,off))
237 goto err;
238 if ((x->p != NULL) && !print(bp,"P: ",x->p,m,off)) goto err;
239 if ((x->q != NULL) && !print(bp,"Q: ",x->q,m,off)) goto err;
240 if ((x->g != NULL) && !print(bp,"G: ",x->g,m,off)) goto err;
241 ret=1;
242err:
26a3a48d 243 if (m != NULL) OPENSSL_free(m);
d02b48c6
RE
244 return(ret);
245 }
cf1b7d96 246#endif /* !OPENSSL_NO_DSA */
d02b48c6 247
5f3d6f70
BM
248#ifndef OPENSSL_NO_EC
249#ifndef OPENSSL_NO_FP_API
250int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
251 {
252 BIO *b;
253 int ret;
254
255 if ((b=BIO_new(BIO_s_file())) == NULL)
256 {
257 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP,ERR_R_BUF_LIB);
258 return(0);
259 }
260 BIO_set_fp(b, fp, BIO_NOCLOSE);
261 ret = ECPKParameters_print(b, x, off);
262 BIO_free(b);
263 return(ret);
264 }
14a7cfb3
BM
265
266int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
267 {
268 BIO *b;
269 int ret;
270
271 if ((b=BIO_new(BIO_s_file())) == NULL)
272 {
273 ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
274 return(0);
275 }
276 BIO_set_fp(b, fp, BIO_NOCLOSE);
277 ret = EC_KEY_print(b, x, off);
278 BIO_free(b);
279 return(ret);
280 }
5f3d6f70
BM
281#endif
282
283int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
284 {
285 char str[128];
286 unsigned char *buffer=NULL;
287 size_t buf_len=0, i;
288 int ret=0, reason=ERR_R_BIO_LIB;
289 BN_CTX *ctx=NULL;
290 EC_POINT *point=NULL;
291 BIGNUM *p=NULL, *a=NULL, *b=NULL, *gen=NULL,
292 *order=NULL, *cofactor=NULL, *seed=NULL;
293
294 static const char *gen_compressed = "Generator (compressed):";
295 static const char *gen_uncompressed = "Generator (uncompressed):";
296 static const char *gen_hybrid = "Generator (hybrid):";
297
298 if (!x)
299 {
300 reason = ERR_R_PASSED_NULL_PARAMETER;
301 goto err;
302 }
303
304 if (EC_GROUP_get_asn1_flag(x))
305 {
306 /* the curve parameter are given by an asn1 OID */
307 int nid;
308
309 if (off)
310 {
311 if (off > 128)
312 off=128;
313 memset(str, ' ', off);
314 if (BIO_write(bp, str, off) <= 0)
315 goto err;
316 }
317
318 nid = EC_GROUP_get_nid(x);
319 if (nid == 0)
320 goto err;
321
322 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
323 goto err;
324 if (BIO_printf(bp, "\n") <= 0)
325 goto err;
326 }
327 else
328 {
329 /* explicit parameters */
330 /* TODO */
331 point_conversion_form_t form;
332
333 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
334 (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
335 (cofactor = BN_new()) == NULL)
336 {
337 reason = ERR_R_MALLOC_FAILURE;
338 goto err;
339 }
340
e172d60d 341 if (EC_METHOD_get_field_type(EC_GROUP_method_of(x)) == NID_X9_62_prime_field)
5f3d6f70 342 {
e172d60d
BM
343 if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx))
344 {
345 reason = ERR_R_EC_LIB;
346 goto err;
347 }
348 }
349 else
350 {
351 if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx))
352 {
353 reason = ERR_R_EC_LIB;
354 goto err;
355 }
5f3d6f70
BM
356 }
357
358 if ((point = EC_GROUP_get0_generator(x)) == NULL)
359 {
360 reason = ERR_R_EC_LIB;
361 goto err;
362 }
363 if (!EC_GROUP_get_order(x, order, NULL) ||
364 !EC_GROUP_get_cofactor(x, cofactor, NULL))
365 {
366 reason = ERR_R_EC_LIB;
367 goto err;
368 }
369
370 form = EC_GROUP_get_point_conversion_form(x);
371
372 if ((gen = EC_POINT_point2bn(x, point,
373 form, NULL, ctx)) == NULL)
374 {
375 reason = ERR_R_EC_LIB;
376 goto err;
377 }
378
379 buf_len = (size_t)BN_num_bytes(p);
380 if (buf_len < (i = (size_t)BN_num_bytes(a)))
381 buf_len = i;
382 if (buf_len < (i = (size_t)BN_num_bytes(b)))
383 buf_len = i;
384 if (buf_len < (i = (size_t)BN_num_bytes(gen)))
385 buf_len = i;
386 if (buf_len < (i = (size_t)BN_num_bytes(order)))
387 buf_len = i;
388 if (buf_len < (i = (size_t)BN_num_bytes(cofactor)))
389 buf_len = i;
390
391 if (EC_GROUP_get0_seed(x))
392 {
393 seed = BN_bin2bn(EC_GROUP_get0_seed(x),
394 EC_GROUP_get_seed_len(x), NULL);
395 if (seed == NULL)
396 {
397 reason = ERR_R_BN_LIB;
398 goto err;
399 }
400 if (buf_len < (i = (size_t)BN_num_bytes(seed)))
401 buf_len = i;
402 }
403
404 buf_len += 10;
405 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
406 {
407 reason = ERR_R_MALLOC_FAILURE;
408 goto err;
409 }
410 if (off)
411 {
412 if (off > 128) off=128;
413 memset(str,' ',off);
414 }
415
416 if ((p != NULL) && !print(bp, "P: ", p, buffer, off))
417 goto err;
418 if ((a != NULL) && !print(bp, "A: ", a, buffer, off))
419 goto err;
420 if ((b != NULL) && !print(bp, "B: ", b, buffer, off))
421 goto err;
422 if (form == POINT_CONVERSION_COMPRESSED)
423 {
424 if ((gen != NULL) && !print(bp, gen_compressed, gen,
425 buffer, off))
426 goto err;
427 }
428 else if (form == POINT_CONVERSION_UNCOMPRESSED)
429 {
430 if ((gen != NULL) && !print(bp, gen_uncompressed, gen,
431 buffer, off))
432 goto err;
433 }
434 else /* form == POINT_CONVERSION_HYBRID */
435 {
436 if ((gen != NULL) && !print(bp, gen_hybrid, gen,
437 buffer, off))
438 goto err;
439 }
440 if ((order != NULL) && !print(bp, "Order: ", order,
441 buffer, off)) goto err;
442 if ((cofactor != NULL) && !print(bp, "Cofactor: ", cofactor,
443 buffer, off)) goto err;
444 if ((seed != NULL) && !print(bp, "Seed:", seed,
445 buffer, off)) goto err;
446 }
447 ret=1;
448err:
449 if (!ret)
450 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
451 if (p)
452 BN_free(p);
453 if (a)
454 BN_free(a);
455 if (b)
456 BN_free(b);
457 if (gen)
458 BN_free(gen);
459 if (order)
460 BN_free(order);
461 if (cofactor)
462 BN_free(cofactor);
463 if (seed)
464 BN_free(seed);
465 if (ctx)
466 BN_CTX_free(ctx);
467 if (buffer != NULL)
468 OPENSSL_free(buffer);
469 return(ret);
470 }
4d94ae00 471
14a7cfb3 472int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
4d94ae00
BM
473 {
474 char str[128];
475 unsigned char *buffer=NULL;
5f3d6f70
BM
476 size_t buf_len=0, i;
477 int ret=0, reason=ERR_R_BIO_LIB;
478 BIGNUM *pub_key=NULL;
4d94ae00 479 BN_CTX *ctx=NULL;
4d94ae00 480
4d94ae00
BM
481 if (!x || !x->group)
482 {
5f3d6f70 483 reason = ERR_R_PASSED_NULL_PARAMETER;
d0561b5c
BM
484 goto err;
485 }
5f3d6f70
BM
486
487 if ((pub_key = EC_POINT_point2bn(x->group, x->pub_key,
14a7cfb3 488 x->conv_form, NULL, ctx)) == NULL)
4d94ae00
BM
489 {
490 reason = ERR_R_EC_LIB;
491 goto err;
492 }
5f3d6f70
BM
493
494 buf_len = (size_t)BN_num_bytes(pub_key);
495 if (x->priv_key)
4d94ae00 496 {
5f3d6f70
BM
497 if ((i = (size_t)BN_num_bytes(x->priv_key)) > buf_len)
498 buf_len = i;
4d94ae00 499 }
6cbe6382 500
6cbe6382
BM
501 buf_len += 10;
502 if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
4d94ae00
BM
503 {
504 reason = ERR_R_MALLOC_FAILURE;
4d94ae00
BM
505 goto err;
506 }
507 if (off)
508 {
509 if (off > 128) off=128;
510 memset(str,' ',off);
511 }
512 if (x->priv_key != NULL)
513 {
514 if (off && (BIO_write(bp, str, off) <= 0)) goto err;
5f3d6f70
BM
515 if (BIO_printf(bp, "Private-Key: (%d bit)\n",
516 BN_num_bits(x->priv_key)) <= 0) goto err;
4d94ae00
BM
517 }
518
5f3d6f70
BM
519 if ((x->priv_key != NULL) && !print(bp, "priv:", x->priv_key,
520 buffer, off))
521 goto err;
522 if ((pub_key != NULL) && !print(bp, "pub: ", pub_key,
523 buffer, off))
524 goto err;
525 if (!ECPKParameters_print(bp, x->group, off))
526 goto err;
4d94ae00
BM
527 ret=1;
528err:
529 if (!ret)
14a7cfb3 530 ECerr(EC_F_EC_KEY_PRINT, reason);
5f3d6f70
BM
531 if (pub_key)
532 BN_free(pub_key);
533 if (ctx)
534 BN_CTX_free(ctx);
535 if (buffer != NULL)
536 OPENSSL_free(buffer);
4d94ae00
BM
537 return(ret);
538 }
14a7cfb3 539#endif /* OPENSSL_NO_EC */
4d94ae00 540
6b691a5c
UM
541static int print(BIO *bp, const char *number, BIGNUM *num, unsigned char *buf,
542 int off)
d02b48c6
RE
543 {
544 int n,i;
e778802f
BL
545 char str[128];
546 const char *neg;
d02b48c6
RE
547
548 if (num == NULL) return(1);
549 neg=(num->neg)?"-":"";
550 if (off)
551 {
552 if (off > 128) off=128;
553 memset(str,' ',off);
554 if (BIO_write(bp,str,off) <= 0) return(0);
555 }
556
557 if (BN_num_bytes(num) <= BN_BYTES)
558 {
559 if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg,
560 (unsigned long)num->d[0],neg,(unsigned long)num->d[0])
561 <= 0) return(0);
562 }
563 else
564 {
565 buf[0]=0;
566 if (BIO_printf(bp,"%s%s",number,
567 (neg[0] == '-')?" (Negative)":"") <= 0)
568 return(0);
569 n=BN_bn2bin(num,&buf[1]);
570
571 if (buf[1] & 0x80)
572 n++;
573 else buf++;
574
575 for (i=0; i<n; i++)
576 {
577 if ((i%15) == 0)
578 {
579 str[0]='\n';
580 memset(&(str[1]),' ',off+4);
581 if (BIO_write(bp,str,off+1+4) <= 0) return(0);
582 }
583 if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
584 <= 0) return(0);
585 }
586 if (BIO_write(bp,"\n",1) <= 0) return(0);
587 }
588 return(1);
589 }
590
cf1b7d96
RL
591#ifndef OPENSSL_NO_DH
592#ifndef OPENSSL_NO_FP_API
f971ccb2 593int DHparams_print_fp(FILE *fp, const DH *x)
4d94ae00
BM
594 {
595 BIO *b;
596 int ret;
d02b48c6 597
4d94ae00 598 if ((b=BIO_new(BIO_s_file())) == NULL)
d02b48c6
RE
599 {
600 DHerr(DH_F_DHPARAMS_PRINT_FP,ERR_R_BUF_LIB);
4d94ae00 601 return(0);
d02b48c6 602 }
4d94ae00
BM
603 BIO_set_fp(b,fp,BIO_NOCLOSE);
604 ret=DHparams_print(b, x);
605 BIO_free(b);
606 return(ret);
607 }
d02b48c6
RE
608#endif
609
f971ccb2 610int DHparams_print(BIO *bp, const DH *x)
d02b48c6
RE
611 {
612 unsigned char *m=NULL;
9ae08a9c
BM
613 int reason=ERR_R_BUF_LIB,ret=0;
614 size_t buf_len=0, i;
d02b48c6 615
9ae08a9c
BM
616 if (x->p)
617 buf_len = (size_t)BN_num_bytes(x->p);
618 if (x->g)
619 if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
620 buf_len = i;
621 m=(unsigned char *)OPENSSL_malloc(buf_len+10);
d02b48c6
RE
622 if (m == NULL)
623 {
624 reason=ERR_R_MALLOC_FAILURE;
625 goto err;
626 }
627
628 if (BIO_printf(bp,"Diffie-Hellman-Parameters: (%d bit)\n",
629 BN_num_bits(x->p)) <= 0)
630 goto err;
631 if (!print(bp,"prime:",x->p,m,4)) goto err;
632 if (!print(bp,"generator:",x->g,m,4)) goto err;
633 if (x->length != 0)
634 {
657e60fa 635 if (BIO_printf(bp," recommended-private-length: %d bits\n",
d02b48c6
RE
636 (int)x->length) <= 0) goto err;
637 }
638 ret=1;
58964a49
RE
639 if (0)
640 {
d02b48c6 641err:
58964a49
RE
642 DHerr(DH_F_DHPARAMS_PRINT,reason);
643 }
26a3a48d 644 if (m != NULL) OPENSSL_free(m);
d02b48c6
RE
645 return(ret);
646 }
647#endif
648
cf1b7d96
RL
649#ifndef OPENSSL_NO_DSA
650#ifndef OPENSSL_NO_FP_API
a4aba800 651int DSAparams_print_fp(FILE *fp, const DSA *x)
4d94ae00
BM
652 {
653 BIO *b;
654 int ret;
d02b48c6 655
4d94ae00 656 if ((b=BIO_new(BIO_s_file())) == NULL)
d02b48c6
RE
657 {
658 DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB);
4d94ae00 659 return(0);
d02b48c6 660 }
4d94ae00
BM
661 BIO_set_fp(b,fp,BIO_NOCLOSE);
662 ret=DSAparams_print(b, x);
663 BIO_free(b);
664 return(ret);
665 }
d02b48c6
RE
666#endif
667
a4aba800 668int DSAparams_print(BIO *bp, const DSA *x)
d02b48c6
RE
669 {
670 unsigned char *m=NULL;
9ae08a9c
BM
671 int reason=ERR_R_BUF_LIB,ret=0;
672 size_t buf_len=0,i;
d02b48c6 673
9ae08a9c
BM
674 if (x->p)
675 buf_len = (size_t)BN_num_bytes(x->p);
676 if (x->q)
677 if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
678 buf_len = i;
679 if (x->g)
680 if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
681 buf_len = i;
682 m=(unsigned char *)OPENSSL_malloc(buf_len+10);
d02b48c6
RE
683 if (m == NULL)
684 {
685 reason=ERR_R_MALLOC_FAILURE;
686 goto err;
687 }
688
689 if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n",
690 BN_num_bits(x->p)) <= 0)
691 goto err;
692 if (!print(bp,"p:",x->p,m,4)) goto err;
693 if (!print(bp,"q:",x->q,m,4)) goto err;
694 if (!print(bp,"g:",x->g,m,4)) goto err;
695 ret=1;
696err:
26a3a48d 697 if (m != NULL) OPENSSL_free(m);
d02b48c6
RE
698 DSAerr(DSA_F_DSAPARAMS_PRINT,reason);
699 return(ret);
700 }
701
cf1b7d96 702#endif /* !OPENSSL_NO_DSA */
d02b48c6 703
14a7cfb3 704#ifndef OPENSSL_NO_EC
4d94ae00 705#ifndef OPENSSL_NO_FP_API
14a7cfb3 706int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
4d94ae00
BM
707 {
708 BIO *b;
709 int ret;
710
711 if ((b=BIO_new(BIO_s_file())) == NULL)
14a7cfb3
BM
712 {
713 ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
4d94ae00 714 return(0);
14a7cfb3 715 }
4d94ae00 716 BIO_set_fp(b, fp, BIO_NOCLOSE);
14a7cfb3 717 ret = ECParameters_print(b, x);
4d94ae00
BM
718 BIO_free(b);
719 return(ret);
720 }
721#endif
722
14a7cfb3 723int ECParameters_print(BIO *bp, const EC_KEY *x)
5f3d6f70
BM
724 {
725 int reason=ERR_R_EC_LIB, ret=0;
726 BIGNUM *order=NULL;
4d94ae00 727
5f3d6f70
BM
728 if (!x || !x->group)
729 {
730 reason = ERR_R_PASSED_NULL_PARAMETER;;
4d94ae00 731 goto err;
5f3d6f70
BM
732 }
733
734 if ((order = BN_new()) == NULL)
735 {
4d94ae00
BM
736 reason = ERR_R_MALLOC_FAILURE;
737 goto err;
5f3d6f70 738 }
6cbe6382 739
5f3d6f70 740 if (!EC_GROUP_get_order(x->group, order, NULL))
6cbe6382 741 {
4d94ae00
BM
742 reason = ERR_R_EC_LIB;
743 goto err;
6cbe6382 744 }
4d94ae00 745
5f3d6f70
BM
746 if (BIO_printf(bp, "ECDSA-Parameters: (%d bit)\n",
747 BN_num_bits(order)) <= 0)
748 goto err;
749 if (!ECPKParameters_print(bp, x->group, 4))
750 goto err;
4d94ae00
BM
751 ret=1;
752err:
5f3d6f70
BM
753 if (order)
754 BN_free(order);
14a7cfb3 755 ECerr(EC_F_ECPARAMETERS_PRINT, reason);
4d94ae00
BM
756 return(ret);
757 }
758
759#endif