228
246
#endif /* !OPENSSL_NO_DSA */
230
static int print(BIO *bp, const char *number, BIGNUM *num, unsigned char *buf,
248
#ifndef OPENSSL_NO_EC
249
#ifndef OPENSSL_NO_FP_API
250
int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
255
if ((b=BIO_new(BIO_s_file())) == NULL)
257
ECerr(EC_F_ECPKPARAMETERS_PRINT_FP,ERR_R_BUF_LIB);
260
BIO_set_fp(b, fp, BIO_NOCLOSE);
261
ret = ECPKParameters_print(b, x, off);
266
int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
271
if ((b=BIO_new(BIO_s_file())) == NULL)
273
ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
276
BIO_set_fp(b, fp, BIO_NOCLOSE);
277
ret = EC_KEY_print(b, x, off);
283
int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
285
unsigned char *buffer=NULL;
287
int ret=0, reason=ERR_R_BIO_LIB;
289
const EC_POINT *point=NULL;
290
BIGNUM *p=NULL, *a=NULL, *b=NULL, *gen=NULL,
291
*order=NULL, *cofactor=NULL;
292
const unsigned char *seed;
295
static const char *gen_compressed = "Generator (compressed):";
296
static const char *gen_uncompressed = "Generator (uncompressed):";
297
static const char *gen_hybrid = "Generator (hybrid):";
301
reason = ERR_R_PASSED_NULL_PARAMETER;
305
if (EC_GROUP_get_asn1_flag(x))
307
/* the curve parameter are given by an asn1 OID */
310
if (!BIO_indent(bp, off, 128))
313
nid = EC_GROUP_get_curve_name(x);
317
if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
319
if (BIO_printf(bp, "\n") <= 0)
324
/* explicit parameters */
326
point_conversion_form_t form;
327
int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
329
if (tmp_nid == NID_X9_62_characteristic_two_field)
332
if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
333
(b = BN_new()) == NULL || (order = BN_new()) == NULL ||
334
(cofactor = BN_new()) == NULL)
336
reason = ERR_R_MALLOC_FAILURE;
342
if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx))
344
reason = ERR_R_EC_LIB;
348
else /* prime field */
350
if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx))
352
reason = ERR_R_EC_LIB;
357
if ((point = EC_GROUP_get0_generator(x)) == NULL)
359
reason = ERR_R_EC_LIB;
362
if (!EC_GROUP_get_order(x, order, NULL) ||
363
!EC_GROUP_get_cofactor(x, cofactor, NULL))
365
reason = ERR_R_EC_LIB;
369
form = EC_GROUP_get_point_conversion_form(x);
371
if ((gen = EC_POINT_point2bn(x, point,
372
form, NULL, ctx)) == NULL)
374
reason = ERR_R_EC_LIB;
378
buf_len = (size_t)BN_num_bytes(p);
379
if (buf_len < (i = (size_t)BN_num_bytes(a)))
381
if (buf_len < (i = (size_t)BN_num_bytes(b)))
383
if (buf_len < (i = (size_t)BN_num_bytes(gen)))
385
if (buf_len < (i = (size_t)BN_num_bytes(order)))
387
if (buf_len < (i = (size_t)BN_num_bytes(cofactor)))
390
if ((seed = EC_GROUP_get0_seed(x)) != NULL)
391
seed_len = EC_GROUP_get_seed_len(x);
394
if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
396
reason = ERR_R_MALLOC_FAILURE;
400
if (!BIO_indent(bp, off, 128))
403
/* print the 'short name' of the field type */
404
if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
410
/* print the 'short name' of the base type OID */
411
int basis_type = EC_GROUP_get_basis_type(x);
415
if (!BIO_indent(bp, off, 128))
418
if (BIO_printf(bp, "Basis Type: %s\n",
419
OBJ_nid2sn(basis_type)) <= 0)
422
/* print the polynomial */
423
if ((p != NULL) && !print(bp, "Polynomial:", p, buffer,
429
if ((p != NULL) && !print(bp, "Prime:", p, buffer,off))
432
if ((a != NULL) && !print(bp, "A: ", a, buffer, off))
434
if ((b != NULL) && !print(bp, "B: ", b, buffer, off))
436
if (form == POINT_CONVERSION_COMPRESSED)
438
if ((gen != NULL) && !print(bp, gen_compressed, gen,
442
else if (form == POINT_CONVERSION_UNCOMPRESSED)
444
if ((gen != NULL) && !print(bp, gen_uncompressed, gen,
448
else /* form == POINT_CONVERSION_HYBRID */
450
if ((gen != NULL) && !print(bp, gen_hybrid, gen,
454
if ((order != NULL) && !print(bp, "Order: ", order,
455
buffer, off)) goto err;
456
if ((cofactor != NULL) && !print(bp, "Cofactor: ", cofactor,
457
buffer, off)) goto err;
458
if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
464
ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
480
OPENSSL_free(buffer);
484
int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
486
unsigned char *buffer=NULL;
488
int ret=0, reason=ERR_R_BIO_LIB;
489
BIGNUM *pub_key=NULL, *order=NULL;
491
const EC_GROUP *group;
492
const EC_POINT *public_key;
493
const BIGNUM *priv_key;
495
if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL)
497
reason = ERR_R_PASSED_NULL_PARAMETER;
501
public_key = EC_KEY_get0_public_key(x);
502
if ((pub_key = EC_POINT_point2bn(group, public_key,
503
EC_KEY_get_conv_form(x), NULL, ctx)) == NULL)
505
reason = ERR_R_EC_LIB;
509
buf_len = (size_t)BN_num_bytes(pub_key);
510
priv_key = EC_KEY_get0_private_key(x);
511
if (priv_key != NULL)
513
if ((i = (size_t)BN_num_bytes(priv_key)) > buf_len)
518
if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
520
reason = ERR_R_MALLOC_FAILURE;
524
if (priv_key != NULL)
526
if (!BIO_indent(bp, off, 128))
528
if ((order = BN_new()) == NULL)
530
if (!EC_GROUP_get_order(group, order, NULL))
532
if (BIO_printf(bp, "Private-Key: (%d bit)\n",
533
BN_num_bits(order)) <= 0) goto err;
536
if ((priv_key != NULL) && !print(bp, "priv:", priv_key,
539
if ((pub_key != NULL) && !print(bp, "pub: ", pub_key,
542
if (!ECPKParameters_print(bp, group, off))
547
ECerr(EC_F_EC_KEY_PRINT, reason);
555
OPENSSL_free(buffer);
558
#endif /* OPENSSL_NO_EC */
560
static int print(BIO *bp, const char *number, const BIGNUM *num, unsigned char *buf,
236
566
if (num == NULL) return(1);
237
neg=(num->neg)?"-":"";
567
neg = (BN_is_negative(num))?"-":"";
238
568
if(!BIO_indent(bp,off,128))
572
if (BIO_printf(bp, "%s 0\n", number) <= 0)
241
577
if (BN_num_bytes(num) <= BN_BYTES)