1 /* $OpenBSD: eck_prn.c,v 1.28 2023/07/07 13:54:45 beck 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/bn.h> 68 #include <openssl/ec.h> 69 #include <openssl/err.h> 70 #include <openssl/evp.h> 71 72 #include "ec_local.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 ECerror(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 LCRYPTO_ALIAS(ECPKParameters_print_fp); 90 91 int 92 EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off) 93 { 94 BIO *b; 95 int ret; 96 97 if ((b = BIO_new(BIO_s_file())) == NULL) { 98 ECerror(ERR_R_BIO_LIB); 99 return (0); 100 } 101 BIO_set_fp(b, fp, BIO_NOCLOSE); 102 ret = EC_KEY_print(b, x, off); 103 BIO_free(b); 104 return (ret); 105 } 106 LCRYPTO_ALIAS(EC_KEY_print_fp); 107 108 int 109 ECParameters_print_fp(FILE *fp, const EC_KEY *x) 110 { 111 BIO *b; 112 int ret; 113 114 if ((b = BIO_new(BIO_s_file())) == NULL) { 115 ECerror(ERR_R_BIO_LIB); 116 return (0); 117 } 118 BIO_set_fp(b, fp, BIO_NOCLOSE); 119 ret = ECParameters_print(b, x); 120 BIO_free(b); 121 return (ret); 122 } 123 LCRYPTO_ALIAS(ECParameters_print_fp); 124 125 int 126 EC_KEY_print(BIO *bp, const EC_KEY *x, int off) 127 { 128 EVP_PKEY *pk; 129 int ret = 0; 130 131 if ((pk = EVP_PKEY_new()) == NULL) 132 goto err; 133 134 if (!EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *) x)) 135 goto err; 136 137 ret = EVP_PKEY_print_private(bp, pk, off, NULL); 138 err: 139 EVP_PKEY_free(pk); 140 return ret; 141 } 142 LCRYPTO_ALIAS(EC_KEY_print); 143 144 int 145 ECParameters_print(BIO *bp, const EC_KEY *x) 146 { 147 EVP_PKEY *pk; 148 int ret = 0; 149 150 if ((pk = EVP_PKEY_new()) == NULL) 151 goto err; 152 153 if (!EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *) x)) 154 goto err; 155 156 ret = EVP_PKEY_print_params(bp, pk, 4, NULL); 157 err: 158 EVP_PKEY_free(pk); 159 return ret; 160 } 161 LCRYPTO_ALIAS(ECParameters_print); 162 163 static int 164 print_bin(BIO *fp, const char *str, const unsigned char *num, 165 size_t len, int off); 166 167 static int 168 ecpk_print_asn1_parameters(BIO *bp, const EC_GROUP *group, int off) 169 { 170 const char *nist_name; 171 int nid; 172 int ret = 0; 173 174 if (!BIO_indent(bp, off, 128)) { 175 ECerror(ERR_R_BIO_LIB); 176 goto err; 177 } 178 179 if ((nid = EC_GROUP_get_curve_name(group)) == NID_undef) { 180 ECerror(ERR_R_INTERNAL_ERROR); 181 goto err; 182 } 183 184 if (BIO_printf(bp, "ASN1 OID: %s\n", OBJ_nid2sn(nid)) <= 0) { 185 ECerror(ERR_R_BIO_LIB); 186 goto err; 187 } 188 189 if ((nist_name = EC_curve_nid2nist(nid)) != NULL) { 190 if (!BIO_indent(bp, off, 128)) { 191 ECerror(ERR_R_BIO_LIB); 192 goto err; 193 } 194 if (BIO_printf(bp, "NIST CURVE: %s\n", nist_name) <= 0) { 195 ECerror(ERR_R_BIO_LIB); 196 goto err; 197 } 198 } 199 200 ret = 1; 201 err: 202 203 return ret; 204 } 205 206 static int 207 ecpk_print_explicit_parameters(BIO *bp, const EC_GROUP *group, int off) 208 { 209 BN_CTX *ctx = NULL; 210 const BIGNUM *order; 211 BIGNUM *p, *a, *b, *cofactor; 212 BIGNUM *gen = NULL; 213 const EC_POINT *generator; 214 const char *conversion_form; 215 const unsigned char *seed; 216 size_t seed_len; 217 point_conversion_form_t form; 218 int nid; 219 int ret = 0; 220 221 if ((ctx = BN_CTX_new()) == NULL) { 222 ECerror(ERR_R_MALLOC_FAILURE); 223 goto err; 224 } 225 226 BN_CTX_start(ctx); 227 228 if ((p = BN_CTX_get(ctx)) == NULL) 229 goto err; 230 if ((a = BN_CTX_get(ctx)) == NULL) 231 goto err; 232 if ((b = BN_CTX_get(ctx)) == NULL) 233 goto err; 234 if ((cofactor = BN_CTX_get(ctx)) == NULL) 235 goto err; 236 if ((gen = BN_CTX_get(ctx)) == NULL) 237 goto err; 238 239 if (!EC_GROUP_get_curve(group, p, a, b, ctx)) { 240 ECerror(ERR_R_EC_LIB); 241 goto err; 242 } 243 if ((order = EC_GROUP_get0_order(group)) == NULL) { 244 ECerror(ERR_R_EC_LIB); 245 goto err; 246 } 247 if (!EC_GROUP_get_cofactor(group, cofactor, NULL)) { 248 ECerror(ERR_R_EC_LIB); 249 goto err; 250 } 251 252 if ((generator = EC_GROUP_get0_generator(group)) == NULL) { 253 ECerror(ERR_R_EC_LIB); 254 goto err; 255 } 256 form = EC_GROUP_get_point_conversion_form(group); 257 if (EC_POINT_point2bn(group, generator, form, gen, ctx) == NULL) { 258 ECerror(ERR_R_EC_LIB); 259 goto err; 260 } 261 262 if (!BIO_indent(bp, off, 128)) 263 goto err; 264 265 nid = EC_METHOD_get_field_type(EC_GROUP_method_of(group)); 266 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(nid)) <= 0) 267 goto err; 268 269 if (!bn_printf(bp, p, off, "Prime:")) 270 goto err; 271 if (!bn_printf(bp, a, off, "A: ")) 272 goto err; 273 if (!bn_printf(bp, b, off, "B: ")) 274 goto err; 275 276 if (form == POINT_CONVERSION_COMPRESSED) 277 conversion_form = "compressed"; 278 else if (form == POINT_CONVERSION_UNCOMPRESSED) 279 conversion_form = "uncompressed"; 280 else if (form == POINT_CONVERSION_HYBRID) 281 conversion_form = "hybrid"; 282 else 283 conversion_form = "unknown"; 284 if (!bn_printf(bp, gen, off, "Generator (%s):", conversion_form)) 285 goto err; 286 287 if (!bn_printf(bp, order, off, "Order: ")) 288 goto err; 289 if (!bn_printf(bp, cofactor, off, "Cofactor: ")) 290 goto err; 291 if ((seed = EC_GROUP_get0_seed(group)) != NULL) { 292 seed_len = EC_GROUP_get_seed_len(group); 293 if (!print_bin(bp, "Seed:", seed, seed_len, off)) 294 goto err; 295 } 296 297 ret = 1; 298 err: 299 BN_CTX_end(ctx); 300 BN_CTX_free(ctx); 301 302 return ret; 303 } 304 305 int 306 ECPKParameters_print(BIO *bp, const EC_GROUP *group, int off) 307 { 308 if (group == NULL) { 309 ECerror(ERR_R_PASSED_NULL_PARAMETER); 310 return 0; 311 } 312 313 if (EC_GROUP_get_asn1_flag(group)) 314 return ecpk_print_asn1_parameters(bp, group, off); 315 316 return ecpk_print_explicit_parameters(bp, group, off); 317 } 318 LCRYPTO_ALIAS(ECPKParameters_print); 319 320 static int 321 print_bin(BIO *fp, const char *name, const unsigned char *buf, 322 size_t len, int off) 323 { 324 size_t i; 325 char str[128]; 326 327 if (buf == NULL) 328 return 1; 329 if (off) { 330 if (off > 128) 331 off = 128; 332 memset(str, ' ', off); 333 if (BIO_write(fp, str, off) <= 0) 334 return 0; 335 } 336 if (BIO_printf(fp, "%s", name) <= 0) 337 return 0; 338 339 for (i = 0; i < len; i++) { 340 if ((i % 15) == 0) { 341 str[0] = '\n'; 342 memset(&(str[1]), ' ', off + 4); 343 if (BIO_write(fp, str, off + 1 + 4) <= 0) 344 return 0; 345 } 346 if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <= 0) 347 return 0; 348 } 349 if (BIO_write(fp, "\n", 1) <= 0) 350 return 0; 351 352 return 1; 353 } 354