1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/asn1/t_pkey.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,855 @@
1.4 +/* crypto/asn1/t_pkey.c */
1.5 +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1.6 + * All rights reserved.
1.7 + *
1.8 + * This package is an SSL implementation written
1.9 + * by Eric Young (eay@cryptsoft.com).
1.10 + * The implementation was written so as to conform with Netscapes SSL.
1.11 + *
1.12 + * This library is free for commercial and non-commercial use as long as
1.13 + * the following conditions are aheared to. The following conditions
1.14 + * apply to all code found in this distribution, be it the RC4, RSA,
1.15 + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1.16 + * included with this distribution is covered by the same copyright terms
1.17 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1.18 + *
1.19 + * Copyright remains Eric Young's, and as such any Copyright notices in
1.20 + * the code are not to be removed.
1.21 + * If this package is used in a product, Eric Young should be given attribution
1.22 + * as the author of the parts of the library used.
1.23 + * This can be in the form of a textual message at program startup or
1.24 + * in documentation (online or textual) provided with the package.
1.25 + *
1.26 + * Redistribution and use in source and binary forms, with or without
1.27 + * modification, are permitted provided that the following conditions
1.28 + * are met:
1.29 + * 1. Redistributions of source code must retain the copyright
1.30 + * notice, this list of conditions and the following disclaimer.
1.31 + * 2. Redistributions in binary form must reproduce the above copyright
1.32 + * notice, this list of conditions and the following disclaimer in the
1.33 + * documentation and/or other materials provided with the distribution.
1.34 + * 3. All advertising materials mentioning features or use of this software
1.35 + * must display the following acknowledgement:
1.36 + * "This product includes cryptographic software written by
1.37 + * Eric Young (eay@cryptsoft.com)"
1.38 + * The word 'cryptographic' can be left out if the rouines from the library
1.39 + * being used are not cryptographic related :-).
1.40 + * 4. If you include any Windows specific code (or a derivative thereof) from
1.41 + * the apps directory (application code) you must include an acknowledgement:
1.42 + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1.43 + *
1.44 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1.45 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.46 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.47 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.48 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.49 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.50 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.51 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.52 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.53 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.54 + * SUCH DAMAGE.
1.55 + *
1.56 + * The licence and distribution terms for any publically available version or
1.57 + * derivative of this code cannot be changed. i.e. this code cannot simply be
1.58 + * copied and put under another distribution licence
1.59 + * [including the GNU Public Licence.]
1.60 + */
1.61 +/* ====================================================================
1.62 + * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED.
1.63 + * Binary polynomial ECC support in OpenSSL originally developed by
1.64 + * SUN MICROSYSTEMS, INC., and contributed to the OpenSSL project.
1.65 + */
1.66 +/*
1.67 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.68 + */
1.69 +#include <stdio.h>
1.70 +#include "cryptlib.h"
1.71 +#include <openssl/objects.h>
1.72 +#include <openssl/buffer.h>
1.73 +#include <openssl/bn.h>
1.74 +#ifndef OPENSSL_NO_RSA
1.75 +#include <openssl/rsa.h>
1.76 +#endif
1.77 +#ifndef OPENSSL_NO_DH
1.78 +#include <openssl/dh.h>
1.79 +#endif
1.80 +#ifndef OPENSSL_NO_DSA
1.81 +#include <openssl/dsa.h>
1.82 +#endif
1.83 +#ifndef OPENSSL_NO_EC
1.84 +#include <openssl/ec.h>
1.85 +#endif
1.86 +
1.87 +static int print(BIO *fp,const char *str, const BIGNUM *num,
1.88 + unsigned char *buf,int off);
1.89 +#ifndef OPENSSL_NO_EC
1.90 +static int print_bin(BIO *fp, const char *str, const unsigned char *num,
1.91 + size_t len, int off);
1.92 +#endif
1.93 +#ifndef OPENSSL_NO_RSA
1.94 +#ifndef OPENSSL_NO_FP_API
1.95 +EXPORT_C int RSA_print_fp(FILE *fp, const RSA *x, int off)
1.96 + {
1.97 + BIO *b;
1.98 + int ret;
1.99 +
1.100 + if ((b=BIO_new(BIO_s_file())) == NULL)
1.101 + {
1.102 + RSAerr(RSA_F_RSA_PRINT_FP,ERR_R_BUF_LIB);
1.103 + return(0);
1.104 + }
1.105 + BIO_set_fp(b,fp,BIO_NOCLOSE);
1.106 + ret=RSA_print(b,x,off);
1.107 + BIO_free(b);
1.108 + return(ret);
1.109 + }
1.110 +#endif
1.111 +
1.112 +EXPORT_C int RSA_print(BIO *bp, const RSA *x, int off)
1.113 + {
1.114 +#ifndef SYMBAIN
1.115 + char str[128];
1.116 +#else
1.117 + char str[50];
1.118 +#endif
1.119 + const char *s;
1.120 + unsigned char *m=NULL;
1.121 + int ret=0, mod_len = 0;
1.122 + size_t buf_len=0, i;
1.123 +
1.124 + if (x->n)
1.125 + buf_len = (size_t)BN_num_bytes(x->n);
1.126 + if (x->e)
1.127 + if (buf_len < (i = (size_t)BN_num_bytes(x->e)))
1.128 + buf_len = i;
1.129 + if (x->d)
1.130 + if (buf_len < (i = (size_t)BN_num_bytes(x->d)))
1.131 + buf_len = i;
1.132 + if (x->p)
1.133 + if (buf_len < (i = (size_t)BN_num_bytes(x->p)))
1.134 + buf_len = i;
1.135 + if (x->q)
1.136 + if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
1.137 + buf_len = i;
1.138 + if (x->dmp1)
1.139 + if (buf_len < (i = (size_t)BN_num_bytes(x->dmp1)))
1.140 + buf_len = i;
1.141 + if (x->dmq1)
1.142 + if (buf_len < (i = (size_t)BN_num_bytes(x->dmq1)))
1.143 + buf_len = i;
1.144 + if (x->iqmp)
1.145 + if (buf_len < (i = (size_t)BN_num_bytes(x->iqmp)))
1.146 + buf_len = i;
1.147 +
1.148 + m=(unsigned char *)OPENSSL_malloc(buf_len+10);
1.149 + if (m == NULL)
1.150 + {
1.151 + RSAerr(RSA_F_RSA_PRINT,ERR_R_MALLOC_FAILURE);
1.152 + goto err;
1.153 + }
1.154 +
1.155 + if (x->n != NULL)
1.156 + mod_len = BN_num_bits(x->n);
1.157 +
1.158 + if (x->d != NULL)
1.159 + {
1.160 + if(!BIO_indent(bp,off,128))
1.161 + goto err;
1.162 + if (BIO_printf(bp,"Private-Key: (%d bit)\n", mod_len)
1.163 + <= 0) goto err;
1.164 + }
1.165 +
1.166 + if (x->d == NULL)
1.167 + BIO_snprintf(str,sizeof str,"Modulus (%d bit):", mod_len);
1.168 + else
1.169 + BUF_strlcpy(str,"modulus:",sizeof str);
1.170 + if (!print(bp,str,x->n,m,off)) goto err;
1.171 + s=(x->d == NULL)?"Exponent:":"publicExponent:";
1.172 + if ((x->e != NULL) && !print(bp,s,x->e,m,off))
1.173 + goto err;
1.174 + if ((x->d != NULL) && !print(bp,"privateExponent:",x->d,m,off))
1.175 + goto err;
1.176 + if ((x->p != NULL) && !print(bp,"prime1:",x->p,m,off))
1.177 + goto err;
1.178 + if ((x->q != NULL) && !print(bp,"prime2:",x->q,m,off))
1.179 + goto err;
1.180 + if ((x->dmp1 != NULL) && !print(bp,"exponent1:",x->dmp1,m,off))
1.181 + goto err;
1.182 + if ((x->dmq1 != NULL) && !print(bp,"exponent2:",x->dmq1,m,off))
1.183 + goto err;
1.184 + if ((x->iqmp != NULL) && !print(bp,"coefficient:",x->iqmp,m,off))
1.185 + goto err;
1.186 + ret=1;
1.187 +err:
1.188 + if (m != NULL) OPENSSL_free(m);
1.189 + return(ret);
1.190 +
1.191 + }
1.192 +#endif /* OPENSSL_NO_RSA */
1.193 +
1.194 +#ifndef OPENSSL_NO_DSA
1.195 +#ifndef OPENSSL_NO_FP_API
1.196 +EXPORT_C int DSA_print_fp(FILE *fp, const DSA *x, int off)
1.197 + {
1.198 + BIO *b;
1.199 + int ret;
1.200 +
1.201 + if ((b=BIO_new(BIO_s_file())) == NULL)
1.202 + {
1.203 + DSAerr(DSA_F_DSA_PRINT_FP,ERR_R_BUF_LIB);
1.204 + return(0);
1.205 + }
1.206 + BIO_set_fp(b,fp,BIO_NOCLOSE);
1.207 + ret=DSA_print(b,x,off);
1.208 + BIO_free(b);
1.209 + return(ret);
1.210 + }
1.211 +#endif
1.212 +
1.213 +EXPORT_C int DSA_print(BIO *bp, const DSA *x, int off)
1.214 + {
1.215 + unsigned char *m=NULL;
1.216 + int ret=0;
1.217 + size_t buf_len=0,i;
1.218 +
1.219 + if (x->p)
1.220 + buf_len = (size_t)BN_num_bytes(x->p);
1.221 + else
1.222 + {
1.223 + DSAerr(DSA_F_DSA_PRINT,DSA_R_MISSING_PARAMETERS);
1.224 + goto err;
1.225 + }
1.226 + if (x->q)
1.227 + if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
1.228 + buf_len = i;
1.229 + if (x->g)
1.230 + if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
1.231 + buf_len = i;
1.232 + if (x->priv_key)
1.233 + if (buf_len < (i = (size_t)BN_num_bytes(x->priv_key)))
1.234 + buf_len = i;
1.235 + if (x->pub_key)
1.236 + if (buf_len < (i = (size_t)BN_num_bytes(x->pub_key)))
1.237 + buf_len = i;
1.238 +
1.239 + m=(unsigned char *)OPENSSL_malloc(buf_len+10);
1.240 + if (m == NULL)
1.241 + {
1.242 + DSAerr(DSA_F_DSA_PRINT,ERR_R_MALLOC_FAILURE);
1.243 + goto err;
1.244 + }
1.245 +
1.246 + if (x->priv_key != NULL)
1.247 + {
1.248 + if(!BIO_indent(bp,off,128))
1.249 + goto err;
1.250 + if (BIO_printf(bp,"Private-Key: (%d bit)\n",BN_num_bits(x->p))
1.251 + <= 0) goto err;
1.252 + }
1.253 +
1.254 + if ((x->priv_key != NULL) && !print(bp,"priv:",x->priv_key,m,off))
1.255 + goto err;
1.256 + if ((x->pub_key != NULL) && !print(bp,"pub: ",x->pub_key,m,off))
1.257 + goto err;
1.258 + if ((x->p != NULL) && !print(bp,"P: ",x->p,m,off)) goto err;
1.259 + if ((x->q != NULL) && !print(bp,"Q: ",x->q,m,off)) goto err;
1.260 + if ((x->g != NULL) && !print(bp,"G: ",x->g,m,off)) goto err;
1.261 + ret=1;
1.262 +err:
1.263 + if (m != NULL) OPENSSL_free(m);
1.264 + return(ret);
1.265 + }
1.266 +#endif /* !OPENSSL_NO_DSA */
1.267 +
1.268 +#ifndef OPENSSL_NO_EC
1.269 +#ifndef OPENSSL_NO_FP_API
1.270 +EXPORT_C int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
1.271 + {
1.272 + BIO *b;
1.273 + int ret;
1.274 +
1.275 + if ((b=BIO_new(BIO_s_file())) == NULL)
1.276 + {
1.277 + ECerr(EC_F_ECPKPARAMETERS_PRINT_FP,ERR_R_BUF_LIB);
1.278 + return(0);
1.279 + }
1.280 + BIO_set_fp(b, fp, BIO_NOCLOSE);
1.281 + ret = ECPKParameters_print(b, x, off);
1.282 + BIO_free(b);
1.283 + return(ret);
1.284 + }
1.285 +
1.286 +EXPORT_C int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
1.287 + {
1.288 + BIO *b;
1.289 + int ret;
1.290 +
1.291 + if ((b=BIO_new(BIO_s_file())) == NULL)
1.292 + {
1.293 + ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB);
1.294 + return(0);
1.295 + }
1.296 + BIO_set_fp(b, fp, BIO_NOCLOSE);
1.297 + ret = EC_KEY_print(b, x, off);
1.298 + BIO_free(b);
1.299 + return(ret);
1.300 + }
1.301 +#endif
1.302 +
1.303 +EXPORT_C int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
1.304 + {
1.305 + unsigned char *buffer=NULL;
1.306 + size_t buf_len=0, i;
1.307 + int ret=0, reason=ERR_R_BIO_LIB;
1.308 + BN_CTX *ctx=NULL;
1.309 + const EC_POINT *point=NULL;
1.310 + BIGNUM *p=NULL, *a=NULL, *b=NULL, *gen=NULL,
1.311 + *order=NULL, *cofactor=NULL;
1.312 + const unsigned char *seed;
1.313 + size_t seed_len=0;
1.314 +
1.315 + static const char *gen_compressed = "Generator (compressed):";
1.316 + static const char *gen_uncompressed = "Generator (uncompressed):";
1.317 + static const char *gen_hybrid = "Generator (hybrid):";
1.318 +
1.319 + if (!x)
1.320 + {
1.321 + reason = ERR_R_PASSED_NULL_PARAMETER;
1.322 + goto err;
1.323 + }
1.324 +
1.325 + if (EC_GROUP_get_asn1_flag(x))
1.326 + {
1.327 + /* the curve parameter are given by an asn1 OID */
1.328 + int nid;
1.329 +
1.330 + if (!BIO_indent(bp, off, 128))
1.331 + goto err;
1.332 +
1.333 + nid = EC_GROUP_get_curve_name(x);
1.334 + if (nid == 0)
1.335 + goto err;
1.336 +
1.337 + if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
1.338 + goto err;
1.339 + if (BIO_printf(bp, "\n") <= 0)
1.340 + goto err;
1.341 + }
1.342 + else
1.343 + {
1.344 + /* explicit parameters */
1.345 + int is_char_two = 0;
1.346 + point_conversion_form_t form;
1.347 + int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
1.348 +
1.349 + if (tmp_nid == NID_X9_62_characteristic_two_field)
1.350 + is_char_two = 1;
1.351 +
1.352 + if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
1.353 + (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
1.354 + (cofactor = BN_new()) == NULL)
1.355 + {
1.356 + reason = ERR_R_MALLOC_FAILURE;
1.357 + goto err;
1.358 + }
1.359 +
1.360 + if (is_char_two)
1.361 + {
1.362 + if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx))
1.363 + {
1.364 + reason = ERR_R_EC_LIB;
1.365 + goto err;
1.366 + }
1.367 + }
1.368 + else /* prime field */
1.369 + {
1.370 + if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx))
1.371 + {
1.372 + reason = ERR_R_EC_LIB;
1.373 + goto err;
1.374 + }
1.375 + }
1.376 +
1.377 + if ((point = EC_GROUP_get0_generator(x)) == NULL)
1.378 + {
1.379 + reason = ERR_R_EC_LIB;
1.380 + goto err;
1.381 + }
1.382 + if (!EC_GROUP_get_order(x, order, NULL) ||
1.383 + !EC_GROUP_get_cofactor(x, cofactor, NULL))
1.384 + {
1.385 + reason = ERR_R_EC_LIB;
1.386 + goto err;
1.387 + }
1.388 +
1.389 + form = EC_GROUP_get_point_conversion_form(x);
1.390 +
1.391 + if ((gen = EC_POINT_point2bn(x, point,
1.392 + form, NULL, ctx)) == NULL)
1.393 + {
1.394 + reason = ERR_R_EC_LIB;
1.395 + goto err;
1.396 + }
1.397 +
1.398 + buf_len = (size_t)BN_num_bytes(p);
1.399 + if (buf_len < (i = (size_t)BN_num_bytes(a)))
1.400 + buf_len = i;
1.401 + if (buf_len < (i = (size_t)BN_num_bytes(b)))
1.402 + buf_len = i;
1.403 + if (buf_len < (i = (size_t)BN_num_bytes(gen)))
1.404 + buf_len = i;
1.405 + if (buf_len < (i = (size_t)BN_num_bytes(order)))
1.406 + buf_len = i;
1.407 + if (buf_len < (i = (size_t)BN_num_bytes(cofactor)))
1.408 + buf_len = i;
1.409 +
1.410 + if ((seed = EC_GROUP_get0_seed(x)) != NULL)
1.411 + seed_len = EC_GROUP_get_seed_len(x);
1.412 +
1.413 + buf_len += 10;
1.414 + if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
1.415 + {
1.416 + reason = ERR_R_MALLOC_FAILURE;
1.417 + goto err;
1.418 + }
1.419 +
1.420 + if (!BIO_indent(bp, off, 128))
1.421 + goto err;
1.422 +
1.423 + /* print the 'short name' of the field type */
1.424 + if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
1.425 + <= 0)
1.426 + goto err;
1.427 +
1.428 + if (is_char_two)
1.429 + {
1.430 + /* print the 'short name' of the base type OID */
1.431 + int basis_type = EC_GROUP_get_basis_type(x);
1.432 + if (basis_type == 0)
1.433 + goto err;
1.434 +
1.435 + if (!BIO_indent(bp, off, 128))
1.436 + goto err;
1.437 +
1.438 + if (BIO_printf(bp, "Basis Type: %s\n",
1.439 + OBJ_nid2sn(basis_type)) <= 0)
1.440 + goto err;
1.441 +
1.442 + /* print the polynomial */
1.443 + if ((p != NULL) && !print(bp, "Polynomial:", p, buffer,
1.444 + off))
1.445 + goto err;
1.446 + }
1.447 + else
1.448 + {
1.449 + if ((p != NULL) && !print(bp, "Prime:", p, buffer,off))
1.450 + goto err;
1.451 + }
1.452 + if ((a != NULL) && !print(bp, "A: ", a, buffer, off))
1.453 + goto err;
1.454 + if ((b != NULL) && !print(bp, "B: ", b, buffer, off))
1.455 + goto err;
1.456 + if (form == POINT_CONVERSION_COMPRESSED)
1.457 + {
1.458 + if ((gen != NULL) && !print(bp, gen_compressed, gen,
1.459 + buffer, off))
1.460 + goto err;
1.461 + }
1.462 + else if (form == POINT_CONVERSION_UNCOMPRESSED)
1.463 + {
1.464 + if ((gen != NULL) && !print(bp, gen_uncompressed, gen,
1.465 + buffer, off))
1.466 + goto err;
1.467 + }
1.468 + else /* form == POINT_CONVERSION_HYBRID */
1.469 + {
1.470 + if ((gen != NULL) && !print(bp, gen_hybrid, gen,
1.471 + buffer, off))
1.472 + goto err;
1.473 + }
1.474 + if ((order != NULL) && !print(bp, "Order: ", order,
1.475 + buffer, off)) goto err;
1.476 + if ((cofactor != NULL) && !print(bp, "Cofactor: ", cofactor,
1.477 + buffer, off)) goto err;
1.478 + if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
1.479 + goto err;
1.480 + }
1.481 + ret=1;
1.482 +err:
1.483 + if (!ret)
1.484 + ECerr(EC_F_ECPKPARAMETERS_PRINT, reason);
1.485 + if (p)
1.486 + BN_free(p);
1.487 + if (a)
1.488 + BN_free(a);
1.489 + if (b)
1.490 + BN_free(b);
1.491 + if (gen)
1.492 + BN_free(gen);
1.493 + if (order)
1.494 + BN_free(order);
1.495 + if (cofactor)
1.496 + BN_free(cofactor);
1.497 + if (ctx)
1.498 + BN_CTX_free(ctx);
1.499 + if (buffer != NULL)
1.500 + OPENSSL_free(buffer);
1.501 + return(ret);
1.502 + }
1.503 +
1.504 +EXPORT_C int EC_KEY_print(BIO *bp, const EC_KEY *x, int off)
1.505 + {
1.506 + unsigned char *buffer=NULL;
1.507 + size_t buf_len=0, i;
1.508 + int ret=0, reason=ERR_R_BIO_LIB;
1.509 + BIGNUM *pub_key=NULL, *order=NULL;
1.510 + BN_CTX *ctx=NULL;
1.511 + const EC_GROUP *group;
1.512 + const EC_POINT *public_key;
1.513 + const BIGNUM *priv_key;
1.514 +
1.515 + if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL)
1.516 + {
1.517 + reason = ERR_R_PASSED_NULL_PARAMETER;
1.518 + goto err;
1.519 + }
1.520 +
1.521 + public_key = EC_KEY_get0_public_key(x);
1.522 + if ((pub_key = EC_POINT_point2bn(group, public_key,
1.523 + EC_KEY_get_conv_form(x), NULL, ctx)) == NULL)
1.524 + {
1.525 + reason = ERR_R_EC_LIB;
1.526 + goto err;
1.527 + }
1.528 +
1.529 + buf_len = (size_t)BN_num_bytes(pub_key);
1.530 + priv_key = EC_KEY_get0_private_key(x);
1.531 + if (priv_key != NULL)
1.532 + {
1.533 + if ((i = (size_t)BN_num_bytes(priv_key)) > buf_len)
1.534 + buf_len = i;
1.535 + }
1.536 +
1.537 + buf_len += 10;
1.538 + if ((buffer = OPENSSL_malloc(buf_len)) == NULL)
1.539 + {
1.540 + reason = ERR_R_MALLOC_FAILURE;
1.541 + goto err;
1.542 + }
1.543 +
1.544 + if (priv_key != NULL)
1.545 + {
1.546 + if (!BIO_indent(bp, off, 128))
1.547 + goto err;
1.548 + if ((order = BN_new()) == NULL)
1.549 + goto err;
1.550 + if (!EC_GROUP_get_order(group, order, NULL))
1.551 + goto err;
1.552 + if (BIO_printf(bp, "Private-Key: (%d bit)\n",
1.553 + BN_num_bits(order)) <= 0) goto err;
1.554 + }
1.555 +
1.556 + if ((priv_key != NULL) && !print(bp, "priv:", priv_key,
1.557 + buffer, off))
1.558 + goto err;
1.559 + if ((pub_key != NULL) && !print(bp, "pub: ", pub_key,
1.560 + buffer, off))
1.561 + goto err;
1.562 + if (!ECPKParameters_print(bp, group, off))
1.563 + goto err;
1.564 + ret=1;
1.565 +err:
1.566 + if (!ret)
1.567 + ECerr(EC_F_EC_KEY_PRINT, reason);
1.568 + if (pub_key)
1.569 + BN_free(pub_key);
1.570 + if (order)
1.571 + BN_free(order);
1.572 + if (ctx)
1.573 + BN_CTX_free(ctx);
1.574 + if (buffer != NULL)
1.575 + OPENSSL_free(buffer);
1.576 + return(ret);
1.577 + }
1.578 +#endif /* OPENSSL_NO_EC */
1.579 +
1.580 +static int print(BIO *bp, const char *number, const BIGNUM *num, unsigned char *buf,
1.581 + int off)
1.582 + {
1.583 + int n,i;
1.584 + const char *neg;
1.585 +
1.586 + if (num == NULL) return(1);
1.587 + neg = (BN_is_negative(num))?"-":"";
1.588 + if(!BIO_indent(bp,off,128))
1.589 + return 0;
1.590 + if (BN_is_zero(num))
1.591 + {
1.592 + if (BIO_printf(bp, "%s 0\n", number) <= 0)
1.593 + return 0;
1.594 + return 1;
1.595 + }
1.596 +
1.597 + if (BN_num_bytes(num) <= BN_BYTES)
1.598 + {
1.599 + if (BIO_printf(bp,"%s %s%lu (%s0x%lx)\n",number,neg,
1.600 + (unsigned long)num->d[0],neg,(unsigned long)num->d[0])
1.601 + <= 0) return(0);
1.602 + }
1.603 + else
1.604 + {
1.605 + buf[0]=0;
1.606 + if (BIO_printf(bp,"%s%s",number,
1.607 + (neg[0] == '-')?" (Negative)":"") <= 0)
1.608 + return(0);
1.609 + n=BN_bn2bin(num,&buf[1]);
1.610 +
1.611 + if (buf[1] & 0x80)
1.612 + n++;
1.613 + else buf++;
1.614 +
1.615 + for (i=0; i<n; i++)
1.616 + {
1.617 + if ((i%15) == 0)
1.618 + {
1.619 + if(BIO_puts(bp,"\n") <= 0
1.620 + || !BIO_indent(bp,off+4,128))
1.621 + return 0;
1.622 + }
1.623 + if (BIO_printf(bp,"%02x%s",buf[i],((i+1) == n)?"":":")
1.624 + <= 0) return(0);
1.625 + }
1.626 + if (BIO_write(bp,"\n",1) <= 0) return(0);
1.627 + }
1.628 + return(1);
1.629 + }
1.630 +
1.631 +#ifndef OPENSSL_NO_EC
1.632 +static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
1.633 + size_t len, int off)
1.634 + {
1.635 + size_t i;
1.636 +#ifndef SYMBIAN
1.637 + char str[128];
1.638 +#else
1.639 + char str[80];
1.640 +#endif
1.641 +
1.642 + if (buf == NULL)
1.643 + return 1;
1.644 + if (off)
1.645 + {
1.646 +#ifndef SYMBIAN
1.647 + if (off > 128)
1.648 + off=128;
1.649 +#else
1.650 +if (off > 80)
1.651 + off=80;
1.652 +#endif
1.653 + memset(str,' ',off);
1.654 + if (BIO_write(fp, str, off) <= 0)
1.655 + return 0;
1.656 + }
1.657 +
1.658 + if (BIO_printf(fp,"%s", name) <= 0)
1.659 + return 0;
1.660 +
1.661 + for (i=0; i<len; i++)
1.662 + {
1.663 + if ((i%15) == 0)
1.664 + {
1.665 + str[0]='\n';
1.666 + memset(&(str[1]),' ',off+4);
1.667 + if (BIO_write(fp, str, off+1+4) <= 0)
1.668 + return 0;
1.669 + }
1.670 + if (BIO_printf(fp,"%02x%s",buf[i],((i+1) == len)?"":":") <= 0)
1.671 + return 0;
1.672 + }
1.673 + if (BIO_write(fp,"\n",1) <= 0)
1.674 + return 0;
1.675 +
1.676 + return 1;
1.677 + }
1.678 +#endif
1.679 +
1.680 +#ifndef OPENSSL_NO_DH
1.681 +#ifndef OPENSSL_NO_FP_API
1.682 +EXPORT_C int DHparams_print_fp(FILE *fp, const DH *x)
1.683 + {
1.684 + BIO *b;
1.685 + int ret;
1.686 +
1.687 + if ((b=BIO_new(BIO_s_file())) == NULL)
1.688 + {
1.689 + DHerr(DH_F_DHPARAMS_PRINT_FP,ERR_R_BUF_LIB);
1.690 + return(0);
1.691 + }
1.692 + BIO_set_fp(b,fp,BIO_NOCLOSE);
1.693 + ret=DHparams_print(b, x);
1.694 + BIO_free(b);
1.695 + return(ret);
1.696 + }
1.697 +#endif
1.698 +
1.699 +EXPORT_C int DHparams_print(BIO *bp, const DH *x)
1.700 + {
1.701 + unsigned char *m=NULL;
1.702 + int reason=ERR_R_BUF_LIB,ret=0;
1.703 + size_t buf_len=0, i;
1.704 +
1.705 + if (x->p)
1.706 + buf_len = (size_t)BN_num_bytes(x->p);
1.707 + else
1.708 + {
1.709 + reason = ERR_R_PASSED_NULL_PARAMETER;
1.710 + goto err;
1.711 + }
1.712 + if (x->g)
1.713 + if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
1.714 + buf_len = i;
1.715 + m=(unsigned char *)OPENSSL_malloc(buf_len+10);
1.716 + if (m == NULL)
1.717 + {
1.718 + reason=ERR_R_MALLOC_FAILURE;
1.719 + goto err;
1.720 + }
1.721 +
1.722 + if (BIO_printf(bp,"Diffie-Hellman-Parameters: (%d bit)\n",
1.723 + BN_num_bits(x->p)) <= 0)
1.724 + goto err;
1.725 + if (!print(bp,"prime:",x->p,m,4)) goto err;
1.726 + if (!print(bp,"generator:",x->g,m,4)) goto err;
1.727 + if (x->length != 0)
1.728 + {
1.729 + if (BIO_printf(bp," recommended-private-length: %d bits\n",
1.730 + (int)x->length) <= 0) goto err;
1.731 + }
1.732 + ret=1;
1.733 + if (0)
1.734 + {
1.735 +err:
1.736 + DHerr(DH_F_DHPARAMS_PRINT,reason);
1.737 + }
1.738 + if (m != NULL) OPENSSL_free(m);
1.739 + return(ret);
1.740 + }
1.741 +#endif
1.742 +
1.743 +#ifndef OPENSSL_NO_DSA
1.744 +#ifndef OPENSSL_NO_FP_API
1.745 +EXPORT_C int DSAparams_print_fp(FILE *fp, const DSA *x)
1.746 + {
1.747 + BIO *b;
1.748 + int ret;
1.749 +
1.750 + if ((b=BIO_new(BIO_s_file())) == NULL)
1.751 + {
1.752 + DSAerr(DSA_F_DSAPARAMS_PRINT_FP,ERR_R_BUF_LIB);
1.753 + return(0);
1.754 + }
1.755 + BIO_set_fp(b,fp,BIO_NOCLOSE);
1.756 + ret=DSAparams_print(b, x);
1.757 + BIO_free(b);
1.758 + return(ret);
1.759 + }
1.760 +#endif
1.761 +
1.762 +EXPORT_C int DSAparams_print(BIO *bp, const DSA *x)
1.763 + {
1.764 + unsigned char *m=NULL;
1.765 + int ret=0;
1.766 + size_t buf_len=0,i;
1.767 +
1.768 + if (x->p)
1.769 + buf_len = (size_t)BN_num_bytes(x->p);
1.770 + else
1.771 + {
1.772 + DSAerr(DSA_F_DSAPARAMS_PRINT,DSA_R_MISSING_PARAMETERS);
1.773 + goto err;
1.774 + }
1.775 + if (x->q)
1.776 + if (buf_len < (i = (size_t)BN_num_bytes(x->q)))
1.777 + buf_len = i;
1.778 + if (x->g)
1.779 + if (buf_len < (i = (size_t)BN_num_bytes(x->g)))
1.780 + buf_len = i;
1.781 + m=(unsigned char *)OPENSSL_malloc(buf_len+10);
1.782 + if (m == NULL)
1.783 + {
1.784 + DSAerr(DSA_F_DSAPARAMS_PRINT,ERR_R_MALLOC_FAILURE);
1.785 + goto err;
1.786 + }
1.787 +
1.788 + if (BIO_printf(bp,"DSA-Parameters: (%d bit)\n",
1.789 + BN_num_bits(x->p)) <= 0)
1.790 + goto err;
1.791 + if (!print(bp,"p:",x->p,m,4)) goto err;
1.792 + if ((x->q != NULL) && !print(bp,"q:",x->q,m,4)) goto err;
1.793 + if ((x->g != NULL) && !print(bp,"g:",x->g,m,4)) goto err;
1.794 + ret=1;
1.795 +err:
1.796 + if (m != NULL) OPENSSL_free(m);
1.797 + return(ret);
1.798 + }
1.799 +
1.800 +#endif /* !OPENSSL_NO_DSA */
1.801 +
1.802 +#ifndef OPENSSL_NO_EC
1.803 +#ifndef OPENSSL_NO_FP_API
1.804 +EXPORT_C int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
1.805 + {
1.806 + BIO *b;
1.807 + int ret;
1.808 +
1.809 + if ((b=BIO_new(BIO_s_file())) == NULL)
1.810 + {
1.811 + ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB);
1.812 + return(0);
1.813 + }
1.814 + BIO_set_fp(b, fp, BIO_NOCLOSE);
1.815 + ret = ECParameters_print(b, x);
1.816 + BIO_free(b);
1.817 + return(ret);
1.818 + }
1.819 +#endif
1.820 +
1.821 +EXPORT_C int ECParameters_print(BIO *bp, const EC_KEY *x)
1.822 + {
1.823 + int reason=ERR_R_EC_LIB, ret=0;
1.824 + BIGNUM *order=NULL;
1.825 + const EC_GROUP *group;
1.826 +
1.827 + if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL)
1.828 + {
1.829 + reason = ERR_R_PASSED_NULL_PARAMETER;;
1.830 + goto err;
1.831 + }
1.832 +
1.833 + if ((order = BN_new()) == NULL)
1.834 + {
1.835 + reason = ERR_R_MALLOC_FAILURE;
1.836 + goto err;
1.837 + }
1.838 +
1.839 + if (!EC_GROUP_get_order(group, order, NULL))
1.840 + {
1.841 + reason = ERR_R_EC_LIB;
1.842 + goto err;
1.843 + }
1.844 +
1.845 + if (BIO_printf(bp, "ECDSA-Parameters: (%d bit)\n",
1.846 + BN_num_bits(order)) <= 0)
1.847 + goto err;
1.848 + if (!ECPKParameters_print(bp, group, 4))
1.849 + goto err;
1.850 + ret=1;
1.851 +err:
1.852 + if (order)
1.853 + BN_free(order);
1.854 + ECerr(EC_F_ECPARAMETERS_PRINT, reason);
1.855 + return(ret);
1.856 + }
1.857 +
1.858 +#endif