1 /* $OpenBSD: eck_prn.c,v 1.17 2021/04/20 17:12:43 tb 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 int
ECPKParameters_print_fp(FILE * fp,const EC_GROUP * x,int off)73 ECPKParameters_print_fp(FILE * fp, const EC_GROUP * x, int off)
74 {
75 BIO *b;
76 int ret;
77
78 if ((b = BIO_new(BIO_s_file())) == NULL) {
79 ECerror(ERR_R_BUF_LIB);
80 return (0);
81 }
82 BIO_set_fp(b, fp, BIO_NOCLOSE);
83 ret = ECPKParameters_print(b, x, off);
84 BIO_free(b);
85 return (ret);
86 }
87
88 int
EC_KEY_print_fp(FILE * fp,const EC_KEY * x,int off)89 EC_KEY_print_fp(FILE * fp, const EC_KEY * x, int off)
90 {
91 BIO *b;
92 int ret;
93
94 if ((b = BIO_new(BIO_s_file())) == NULL) {
95 ECerror(ERR_R_BIO_LIB);
96 return (0);
97 }
98 BIO_set_fp(b, fp, BIO_NOCLOSE);
99 ret = EC_KEY_print(b, x, off);
100 BIO_free(b);
101 return (ret);
102 }
103
104 int
ECParameters_print_fp(FILE * fp,const EC_KEY * x)105 ECParameters_print_fp(FILE * fp, const EC_KEY * x)
106 {
107 BIO *b;
108 int ret;
109
110 if ((b = BIO_new(BIO_s_file())) == NULL) {
111 ECerror(ERR_R_BIO_LIB);
112 return (0);
113 }
114 BIO_set_fp(b, fp, BIO_NOCLOSE);
115 ret = ECParameters_print(b, x);
116 BIO_free(b);
117 return (ret);
118 }
119
120 int
EC_KEY_print(BIO * bp,const EC_KEY * x,int off)121 EC_KEY_print(BIO * bp, const EC_KEY * x, int off)
122 {
123 EVP_PKEY *pk;
124 int ret;
125 pk = EVP_PKEY_new();
126 if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *) x))
127 return 0;
128 ret = EVP_PKEY_print_private(bp, pk, off, NULL);
129 EVP_PKEY_free(pk);
130 return ret;
131 }
132
133 int
ECParameters_print(BIO * bp,const EC_KEY * x)134 ECParameters_print(BIO * bp, const EC_KEY * x)
135 {
136 EVP_PKEY *pk;
137 int ret;
138 pk = EVP_PKEY_new();
139 if (!pk || !EVP_PKEY_set1_EC_KEY(pk, (EC_KEY *) x))
140 return 0;
141 ret = EVP_PKEY_print_params(bp, pk, 4, NULL);
142 EVP_PKEY_free(pk);
143 return ret;
144 }
145
146 static int
147 print_bin(BIO * fp, const char *str, const unsigned char *num,
148 size_t len, int off);
149
150 int
ECPKParameters_print(BIO * bp,const EC_GROUP * x,int off)151 ECPKParameters_print(BIO * bp, const EC_GROUP * x, int off)
152 {
153 unsigned char *buffer = NULL;
154 size_t buf_len = 0, i;
155 int ret = 0, reason = ERR_R_BIO_LIB;
156 BN_CTX *ctx = NULL;
157 const EC_POINT *point = NULL;
158 BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL, *order = NULL,
159 *cofactor = NULL;
160 const unsigned char *seed;
161 size_t seed_len = 0;
162 const char *nname;
163
164 static const char *gen_compressed = "Generator (compressed):";
165 static const char *gen_uncompressed = "Generator (uncompressed):";
166 static const char *gen_hybrid = "Generator (hybrid):";
167
168 if (!x) {
169 reason = ERR_R_PASSED_NULL_PARAMETER;
170 goto err;
171 }
172 ctx = BN_CTX_new();
173 if (ctx == NULL) {
174 reason = ERR_R_MALLOC_FAILURE;
175 goto err;
176 }
177 if (EC_GROUP_get_asn1_flag(x)) {
178 /* the curve parameter are given by an asn1 OID */
179 int nid;
180
181 if (!BIO_indent(bp, off, 128))
182 goto err;
183
184 nid = EC_GROUP_get_curve_name(x);
185 if (nid == 0)
186 goto err;
187
188 if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0)
189 goto err;
190 if (BIO_printf(bp, "\n") <= 0)
191 goto err;
192
193 nname = EC_curve_nid2nist(nid);
194 if (nname) {
195 if (!BIO_indent(bp, off, 128))
196 goto err;
197 if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0)
198 goto err;
199 }
200 } else {
201 /* explicit parameters */
202 int is_char_two = 0;
203 point_conversion_form_t form;
204 int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x));
205
206 if (tmp_nid == NID_X9_62_characteristic_two_field)
207 is_char_two = 1;
208
209 if ((p = BN_new()) == NULL || (a = BN_new()) == NULL ||
210 (b = BN_new()) == NULL || (order = BN_new()) == NULL ||
211 (cofactor = BN_new()) == NULL) {
212 reason = ERR_R_MALLOC_FAILURE;
213 goto err;
214 }
215 if (!EC_GROUP_get_curve(x, p, a, b, ctx)) {
216 reason = ERR_R_EC_LIB;
217 goto err;
218 }
219
220 if ((point = EC_GROUP_get0_generator(x)) == NULL) {
221 reason = ERR_R_EC_LIB;
222 goto err;
223 }
224 if (!EC_GROUP_get_order(x, order, NULL) ||
225 !EC_GROUP_get_cofactor(x, cofactor, NULL)) {
226 reason = ERR_R_EC_LIB;
227 goto err;
228 }
229 form = EC_GROUP_get_point_conversion_form(x);
230
231 if ((gen = EC_POINT_point2bn(x, point,
232 form, NULL, ctx)) == NULL) {
233 reason = ERR_R_EC_LIB;
234 goto err;
235 }
236 buf_len = (size_t) BN_num_bytes(p);
237 if (buf_len < (i = (size_t) BN_num_bytes(a)))
238 buf_len = i;
239 if (buf_len < (i = (size_t) BN_num_bytes(b)))
240 buf_len = i;
241 if (buf_len < (i = (size_t) BN_num_bytes(gen)))
242 buf_len = i;
243 if (buf_len < (i = (size_t) BN_num_bytes(order)))
244 buf_len = i;
245 if (buf_len < (i = (size_t) BN_num_bytes(cofactor)))
246 buf_len = i;
247
248 if ((seed = EC_GROUP_get0_seed(x)) != NULL)
249 seed_len = EC_GROUP_get_seed_len(x);
250
251 buf_len += 10;
252 if ((buffer = malloc(buf_len)) == NULL) {
253 reason = ERR_R_MALLOC_FAILURE;
254 goto err;
255 }
256 if (!BIO_indent(bp, off, 128))
257 goto err;
258
259 /* print the 'short name' of the field type */
260 if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid))
261 <= 0)
262 goto err;
263
264 if (is_char_two) {
265 /* print the 'short name' of the base type OID */
266 int basis_type = EC_GROUP_get_basis_type(x);
267 if (basis_type == 0)
268 goto err;
269
270 if (!BIO_indent(bp, off, 128))
271 goto err;
272
273 if (BIO_printf(bp, "Basis Type: %s\n",
274 OBJ_nid2sn(basis_type)) <= 0)
275 goto err;
276
277 /* print the polynomial */
278 if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, buffer,
279 off))
280 goto err;
281 } else {
282 if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, buffer, off))
283 goto err;
284 }
285 if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, buffer, off))
286 goto err;
287 if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, buffer, off))
288 goto err;
289 if (form == POINT_CONVERSION_COMPRESSED) {
290 if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen,
291 buffer, off))
292 goto err;
293 } else if (form == POINT_CONVERSION_UNCOMPRESSED) {
294 if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen,
295 buffer, off))
296 goto err;
297 } else { /* form == POINT_CONVERSION_HYBRID */
298 if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen,
299 buffer, off))
300 goto err;
301 }
302 if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order,
303 buffer, off))
304 goto err;
305 if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor,
306 buffer, off))
307 goto err;
308 if (seed && !print_bin(bp, "Seed:", seed, seed_len, off))
309 goto err;
310 }
311 ret = 1;
312 err:
313 if (!ret)
314 ECerror(reason);
315 BN_free(p);
316 BN_free(a);
317 BN_free(b);
318 BN_free(gen);
319 BN_free(order);
320 BN_free(cofactor);
321 BN_CTX_free(ctx);
322 free(buffer);
323 return (ret);
324 }
325
326 static int
print_bin(BIO * fp,const char * name,const unsigned char * buf,size_t len,int off)327 print_bin(BIO * fp, const char *name, const unsigned char *buf,
328 size_t len, int off)
329 {
330 size_t i;
331 char str[128];
332
333 if (buf == NULL)
334 return 1;
335 if (off) {
336 if (off > 128)
337 off = 128;
338 memset(str, ' ', off);
339 if (BIO_write(fp, str, off) <= 0)
340 return 0;
341 }
342 if (BIO_printf(fp, "%s", name) <= 0)
343 return 0;
344
345 for (i = 0; i < len; i++) {
346 if ((i % 15) == 0) {
347 str[0] = '\n';
348 memset(&(str[1]), ' ', off + 4);
349 if (BIO_write(fp, str, off + 1 + 4) <= 0)
350 return 0;
351 }
352 if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <= 0)
353 return 0;
354 }
355 if (BIO_write(fp, "\n", 1) <= 0)
356 return 0;
357
358 return 1;
359 }
360