2 * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved
5 * Licensed under the Apache License 2.0 (the "License"). You may not use
6 * this file except in compliance with the License. You can obtain a copy
7 * in the file LICENSE in the source distribution or at
8 * https://www.openssl.org/source/license.html
11 #include "internal/deprecated.h"
14 #include "internal/cryptlib.h"
15 #include <openssl/evp.h>
16 #include <openssl/ec.h>
17 #include <openssl/bn.h>
19 #ifndef OPENSSL_NO_DEPRECATED_3_0
20 # ifndef OPENSSL_NO_STDIO
21 int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off)
26 if ((b = BIO_new(BIO_s_file())) == NULL) {
27 ERR_raise(ERR_LIB_EC, ERR_R_BUF_LIB);
30 BIO_set_fp(b, fp, BIO_NOCLOSE);
31 ret = ECPKParameters_print(b, x, off);
36 int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off)
41 if ((b = BIO_new(BIO_s_file())) == NULL) {
42 ERR_raise(ERR_LIB_EC, ERR_R_BIO_LIB);
45 BIO_set_fp(b, fp, BIO_NOCLOSE);
46 ret = EC_KEY_print(b, x, off);
51 int ECParameters_print_fp(FILE *fp, const EC_KEY *x)
56 if ((b = BIO_new(BIO_s_file())) == NULL) {
57 ERR_raise(ERR_LIB_EC, ERR_R_BIO_LIB);
60 BIO_set_fp(b, fp, BIO_NOCLOSE);
61 ret = ECParameters_print(b, x);
65 #endif /* OPENSSL_NO_STDIO */
67 static int print_bin(BIO *fp, const char *str, const unsigned char *num,
70 int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off)
72 int ret = 0, reason = ERR_R_BIO_LIB;
74 const EC_POINT *point = NULL;
75 BIGNUM *p = NULL, *a = NULL, *b = NULL;
76 unsigned char *gen_buf = NULL;
77 const BIGNUM *order = NULL, *cofactor = NULL;
78 const unsigned char *seed;
79 size_t seed_len = 0, gen_buf_len = 0;
81 static const char *gen_compressed = "Generator (compressed):";
82 static const char *gen_uncompressed = "Generator (uncompressed):";
83 static const char *gen_hybrid = "Generator (hybrid):";
86 reason = ERR_R_PASSED_NULL_PARAMETER;
92 reason = ERR_R_BN_LIB;
96 if (EC_GROUP_get_asn1_flag(x)) {
97 /* the curve parameter are given by an asn1 OID */
101 if (!BIO_indent(bp, off, 128))
104 nid = EC_GROUP_get_curve_name(x);
107 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
109 if (BIO_printf(bp, "\n") <= 0)
111 nname = EC_curve_nid2nist(nid);
113 if (!BIO_indent(bp, off, 128))
115 if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
119 const char *form_str;
120 /* explicit parameters */
122 point_conversion_form_t form;
123 int tmp_nid = EC_GROUP_get_field_type(x);
125 if (tmp_nid == NID_X9_62_characteristic_two_field)
128 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
129 (b = BN_new()) == NULL) {
130 reason = ERR_R_BN_LIB;
134 if (!EC_GROUP_get_curve(x, p, a, b, ctx)) {
135 reason = ERR_R_EC_LIB;
139 if ((point = EC_GROUP_get0_generator(x)) == NULL) {
140 reason = ERR_R_EC_LIB;
143 order = EC_GROUP_get0_order(x);
144 cofactor = EC_GROUP_get0_cofactor(x);
146 reason = ERR_R_EC_LIB;
150 form = EC_GROUP_get_point_conversion_form(x);
152 gen_buf_len = EC_POINT_point2buf(x, point, form, &gen_buf, ctx);
153 if (gen_buf_len == 0) {
154 reason = ERR_R_EC_LIB;
158 if ((seed = EC_GROUP_get0_seed(x)) != NULL)
159 seed_len = EC_GROUP_get_seed_len(x);
161 if (!BIO_indent(bp, off, 128))
164 /* print the 'short name' of the field type */
165 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
170 /* print the 'short name' of the base type OID */
171 int basis_type = EC_GROUP_get_basis_type(x);
175 if (!BIO_indent(bp, off, 128))
178 if (BIO_printf(bp, "Basis Type: %s\n",
179 OBJ_nid2sn(basis_type)) <= 0)
182 /* print the polynomial */
183 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL,
187 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off))
190 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, NULL, off))
192 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, NULL, off))
195 if (form == POINT_CONVERSION_COMPRESSED)
196 form_str = gen_compressed;
197 else if (form == POINT_CONVERSION_UNCOMPRESSED)
198 form_str = gen_uncompressed;
200 form_str = gen_hybrid;
202 && !print_bin(bp, form_str, gen_buf, gen_buf_len, off))
205 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, NULL, off))
207 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
210 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
216 ERR_raise(ERR_LIB_EC, reason);
220 OPENSSL_clear_free(gen_buf, gen_buf_len);
225 static int print_bin(BIO *fp, const char *name, const unsigned char *buf,
229 char str[128 + 1 + 4];
236 memset(str, ' ', off);
237 if (BIO_write(fp, str, off) <= 0)
243 if (BIO_printf(fp, "%s", name) <= 0)
246 for (i = 0; i < len; i++) {
249 memset(&(str[1]), ' ', off + 4);
250 if (BIO_write(fp, str, off + 1 + 4) <= 0)
253 if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <=
257 if (BIO_write(fp, "\n", 1) <= 0)
262 #endif /* OPENSSL_NO_DEPRECATED_3_0 */