1 /* $OpenBSD: eck_prn.c,v 1.10 2014/07/12 16:03:37 miod Exp $ */ 2 /* 3 * Written by Nils Larsch for the OpenSSL project. 4 */ 5 /* ==================================================================== 6 * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 12 * 1. Redistributions of source code must retain the above copyright 13 * notice, this list of conditions and the following disclaimer. 14 * 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in 17 * the documentation and/or other materials provided with the 18 * distribution. 19 * 20 * 3. All advertising materials mentioning features or use of this 21 * software must display the following acknowledgment: 22 * "This product includes software developed by the OpenSSL Project 23 * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" 24 * 25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 26 * endorse or promote products derived from this software without 27 * prior written permission. For written permission, please contact 28 * openssl-core@openssl.org. 29 * 30 * 5. Products derived from this software may not be called "OpenSSL" 31 * nor may "OpenSSL" appear in their names without prior written 32 * permission of the OpenSSL Project. 33 * 34 * 6. Redistributions of any form whatsoever must retain the following 35 * acknowledgment: 36 * "This product includes software developed by the OpenSSL Project 37 * for use in the OpenSSL Toolkit (http://www.openssl.org/)" 38 * 39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 50 * OF THE POSSIBILITY OF SUCH DAMAGE. 51 * ==================================================================== 52 * 53 * This product includes cryptographic software written by Eric Young 54 * (eay@cryptsoft.com). This product includes software written by Tim 55 * Hudson (tjh@cryptsoft.com). 56 * 57 */ 58 /* ==================================================================== 59 * Copyright 2002 Sun Microsystems, Inc. ALL RIGHTS RESERVED. 60 * Portions originally developed by SUN MICROSYSTEMS, INC., and 61 * contributed to the OpenSSL project. 62 */ 63 64 #include <stdio.h> 65 #include <string.h> 66 67 #include <openssl/opensslconf.h> 68 69 #include <openssl/bn.h> 70 #include <openssl/ec.h> 71 #include <openssl/err.h> 72 #include <openssl/evp.h> 73 74 int 75 ECPKParameters_print_fp(FILE * fp, const EC_GROUP * x, int off) 76 { 77 BIO *b; 78 int ret; 79 80 if ((b = BIO_new(BIO_s_file())) == NULL) { 81 ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB); 82 return (0); 83 } 84 BIO_set_fp(b, fp, BIO_NOCLOSE); 85 ret = ECPKParameters_print(b, x, off); 86 BIO_free(b); 87 return (ret); 88 } 89 90 int 91 EC_KEY_print_fp(FILE * fp, const EC_KEY * x, int off) 92 { 93 BIO *b; 94 int ret; 95 96 if ((b = BIO_new(BIO_s_file())) == NULL) { 97 ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB); 98 return (0); 99 } 100 BIO_set_fp(b, fp, BIO_NOCLOSE); 101 ret = EC_KEY_print(b, x, off); 102 BIO_free(b); 103 return (ret); 104 } 105 106 int 107 ECParameters_print_fp(FILE * fp, const EC_KEY * x) 108 { 109 BIO *b; 110 int ret; 111 112 if ((b = BIO_new(BIO_s_file())) == NULL) { 113 ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB); 114 return (0); 115 } 116 BIO_set_fp(b, fp, BIO_NOCLOSE); 117 ret = ECParameters_print(b, x); 118 BIO_free(b); 119 return (ret); 120 } 121 122 int 123 EC_KEY_print(BIO * bp, const EC_KEY * x, int off) 124 { 125 EVP_PKEY *pk; 126 int ret; 127 pk = EVP_PKEY_new(); 128 if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *) x)) 129 return 0; 130 ret = EVP_PKEY_print_private(bp, pk, off, NULL); 131 EVP_PKEY_free(pk); 132 return ret; 133 } 134 135 int 136 ECParameters_print(BIO * bp, const EC_KEY * x) 137 { 138 EVP_PKEY *pk; 139 int ret; 140 pk = EVP_PKEY_new(); 141 if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *) x)) 142 return 0; 143 ret = EVP_PKEY_print_params(bp, pk, 4, NULL); 144 EVP_PKEY_free(pk); 145 return ret; 146 } 147 148 static int 149 print_bin(BIO * fp, const char *str, const unsigned char *num, 150 size_t len, int off); 151 152 int 153 ECPKParameters_print(BIO * bp, const EC_GROUP * x, int off) 154 { 155 unsigned char *buffer = NULL; 156 size_t buf_len = 0, i; 157 int ret = 0, reason = ERR_R_BIO_LIB; 158 BN_CTX *ctx = NULL; 159 const EC_POINT *point = NULL; 160 BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL, *order = NULL, 161 *cofactor = NULL; 162 const unsigned char *seed; 163 size_t seed_len = 0; 164 165 static const char *gen_compressed = "Generator (compressed):"; 166 static const char *gen_uncompressed = "Generator (uncompressed):"; 167 static const char *gen_hybrid = "Generator (hybrid):"; 168 169 if (!x) { 170 reason = ERR_R_PASSED_NULL_PARAMETER; 171 goto err; 172 } 173 ctx = BN_CTX_new(); 174 if (ctx == NULL) { 175 reason = ERR_R_MALLOC_FAILURE; 176 goto err; 177 } 178 if (EC_GROUP_get_asn1_flag(x)) { 179 /* the curve parameter are given by an asn1 OID */ 180 int nid; 181 182 if (!BIO_indent(bp, off, 128)) 183 goto err; 184 185 nid = EC_GROUP_get_curve_name(x); 186 if (nid == 0) 187 goto err; 188 189 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0) 190 goto err; 191 if (BIO_printf(bp, "\n") <= 0) 192 goto err; 193 } else { 194 /* explicit parameters */ 195 int is_char_two = 0; 196 point_conversion_form_t form; 197 int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x)); 198 199 if (tmp_nid == NID_X9_62_characteristic_two_field) 200 is_char_two = 1; 201 202 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL || 203 (b = BN_new()) == NULL || (order = BN_new()) == NULL || 204 (cofactor = BN_new()) == NULL) { 205 reason = ERR_R_MALLOC_FAILURE; 206 goto err; 207 } 208 #ifndef OPENSSL_NO_EC2M 209 if (is_char_two) { 210 if (!EC_GROUP_get_curve_GF2m(x, p, a, b, ctx)) { 211 reason = ERR_R_EC_LIB; 212 goto err; 213 } 214 } else /* prime field */ 215 #endif 216 { 217 if (!EC_GROUP_get_curve_GFp(x, p, a, b, ctx)) { 218 reason = ERR_R_EC_LIB; 219 goto err; 220 } 221 } 222 223 if ((point = EC_GROUP_get0_generator(x)) == NULL) { 224 reason = ERR_R_EC_LIB; 225 goto err; 226 } 227 if (!EC_GROUP_get_order(x, order, NULL) || 228 !EC_GROUP_get_cofactor(x, cofactor, NULL)) { 229 reason = ERR_R_EC_LIB; 230 goto err; 231 } 232 form = EC_GROUP_get_point_conversion_form(x); 233 234 if ((gen = EC_POINT_point2bn(x, point, 235 form, NULL, ctx)) == NULL) { 236 reason = ERR_R_EC_LIB; 237 goto err; 238 } 239 buf_len = (size_t) BN_num_bytes(p); 240 if (buf_len < (i = (size_t) BN_num_bytes(a))) 241 buf_len = i; 242 if (buf_len < (i = (size_t) BN_num_bytes(b))) 243 buf_len = i; 244 if (buf_len < (i = (size_t) BN_num_bytes(gen))) 245 buf_len = i; 246 if (buf_len < (i = (size_t) BN_num_bytes(order))) 247 buf_len = i; 248 if (buf_len < (i = (size_t) BN_num_bytes(cofactor))) 249 buf_len = i; 250 251 if ((seed = EC_GROUP_get0_seed(x)) != NULL) 252 seed_len = EC_GROUP_get_seed_len(x); 253 254 buf_len += 10; 255 if ((buffer = malloc(buf_len)) == NULL) { 256 reason = ERR_R_MALLOC_FAILURE; 257 goto err; 258 } 259 if (!BIO_indent(bp, off, 128)) 260 goto err; 261 262 /* print the 'short name' of the field type */ 263 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) 264 <= 0) 265 goto err; 266 267 if (is_char_two) { 268 /* print the 'short name' of the base type OID */ 269 int basis_type = EC_GROUP_get_basis_type(x); 270 if (basis_type == 0) 271 goto err; 272 273 if (!BIO_indent(bp, off, 128)) 274 goto err; 275 276 if (BIO_printf(bp, "Basis Type: %s\n", 277 OBJ_nid2sn(basis_type)) <= 0) 278 goto err; 279 280 /* print the polynomial */ 281 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer, 282 off)) 283 goto err; 284 } else { 285 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer, off)) 286 goto err; 287 } 288 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off)) 289 goto err; 290 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off)) 291 goto err; 292 if (form == POINT_CONVERSION_COMPRESSED) { 293 if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen, 294 buffer, off)) 295 goto err; 296 } else if (form == POINT_CONVERSION_UNCOMPRESSED) { 297 if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen, 298 buffer, off)) 299 goto err; 300 } else { /* form == POINT_CONVERSION_HYBRID */ 301 if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen, 302 buffer, off)) 303 goto err; 304 } 305 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, 306 buffer, off)) 307 goto err; 308 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor, 309 buffer, off)) 310 goto err; 311 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off)) 312 goto err; 313 } 314 ret = 1; 315 err: 316 if (!ret) 317 ECerr(EC_F_ECPKPARAMETERS_PRINT, reason); 318 BN_free(p); 319 BN_free(a); 320 BN_free(b); 321 BN_free(gen); 322 BN_free(order); 323 BN_free(cofactor); 324 BN_CTX_free(ctx); 325 free(buffer); 326 return (ret); 327 } 328 329 static int 330 print_bin(BIO * fp, const char *name, const unsigned char *buf, 331 size_t len, int off) 332 { 333 size_t i; 334 char str[128]; 335 336 if (buf == NULL) 337 return 1; 338 if (off) { 339 if (off > 128) 340 off = 128; 341 memset(str, ' ', off); 342 if (BIO_write(fp, str, off) <= 0) 343 return 0; 344 } 345 if (BIO_printf(fp, "%s", name) <= 0) 346 return 0; 347 348 for (i = 0; i < len; i++) { 349 if ((i % 15) == 0) { 350 str[0] = '\n'; 351 memset(&(str[1]), ' ', off + 4); 352 if (BIO_write(fp, str, off + 1 + 4) <= 0) 353 return 0; 354 } 355 if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <= 0) 356 return 0; 357 } 358 if (BIO_write(fp, "\n", 1) <= 0) 359 return 0; 360 361 return 1; 362 } 363