/src/openssl111/crypto/ec/eck_prn.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2006-2018 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved |
4 | | * |
5 | | * Licensed under the OpenSSL license (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 |
9 | | */ |
10 | | |
11 | | #include <stdio.h> |
12 | | #include "internal/cryptlib.h" |
13 | | #include <openssl/evp.h> |
14 | | #include <openssl/ec.h> |
15 | | #include <openssl/bn.h> |
16 | | |
17 | | #ifndef OPENSSL_NO_STDIO |
18 | | int ECPKParameters_print_fp(FILE *fp, const EC_GROUP *x, int off) |
19 | 0 | { |
20 | 0 | BIO *b; |
21 | 0 | int ret; |
22 | |
|
23 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
24 | 0 | ECerr(EC_F_ECPKPARAMETERS_PRINT_FP, ERR_R_BUF_LIB); |
25 | 0 | return 0; |
26 | 0 | } |
27 | 0 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
28 | 0 | ret = ECPKParameters_print(b, x, off); |
29 | 0 | BIO_free(b); |
30 | 0 | return ret; |
31 | 0 | } |
32 | | |
33 | | int EC_KEY_print_fp(FILE *fp, const EC_KEY *x, int off) |
34 | 0 | { |
35 | 0 | BIO *b; |
36 | 0 | int ret; |
37 | |
|
38 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
39 | 0 | ECerr(EC_F_EC_KEY_PRINT_FP, ERR_R_BIO_LIB); |
40 | 0 | return 0; |
41 | 0 | } |
42 | 0 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
43 | 0 | ret = EC_KEY_print(b, x, off); |
44 | 0 | BIO_free(b); |
45 | 0 | return ret; |
46 | 0 | } |
47 | | |
48 | | int ECParameters_print_fp(FILE *fp, const EC_KEY *x) |
49 | 0 | { |
50 | 0 | BIO *b; |
51 | 0 | int ret; |
52 | |
|
53 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
54 | 0 | ECerr(EC_F_ECPARAMETERS_PRINT_FP, ERR_R_BIO_LIB); |
55 | 0 | return 0; |
56 | 0 | } |
57 | 0 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
58 | 0 | ret = ECParameters_print(b, x); |
59 | 0 | BIO_free(b); |
60 | 0 | return ret; |
61 | 0 | } |
62 | | #endif |
63 | | |
64 | | static int print_bin(BIO *fp, const char *str, const unsigned char *num, |
65 | | size_t len, int off); |
66 | | |
67 | | int ECPKParameters_print(BIO *bp, const EC_GROUP *x, int off) |
68 | 0 | { |
69 | 0 | int ret = 0, reason = ERR_R_BIO_LIB; |
70 | 0 | BN_CTX *ctx = NULL; |
71 | 0 | const EC_POINT *point = NULL; |
72 | 0 | BIGNUM *p = NULL, *a = NULL, *b = NULL, *gen = NULL; |
73 | 0 | const BIGNUM *order = NULL, *cofactor = NULL; |
74 | 0 | const unsigned char *seed; |
75 | 0 | size_t seed_len = 0; |
76 | |
|
77 | 0 | static const char *gen_compressed = "Generator (compressed):"; |
78 | 0 | static const char *gen_uncompressed = "Generator (uncompressed):"; |
79 | 0 | static const char *gen_hybrid = "Generator (hybrid):"; |
80 | |
|
81 | 0 | if (!x) { |
82 | 0 | reason = ERR_R_PASSED_NULL_PARAMETER; |
83 | 0 | goto err; |
84 | 0 | } |
85 | | |
86 | 0 | ctx = BN_CTX_new(); |
87 | 0 | if (ctx == NULL) { |
88 | 0 | reason = ERR_R_MALLOC_FAILURE; |
89 | 0 | goto err; |
90 | 0 | } |
91 | | |
92 | 0 | if (EC_GROUP_get_asn1_flag(x)) { |
93 | | /* the curve parameter are given by an asn1 OID */ |
94 | 0 | int nid; |
95 | 0 | const char *nname; |
96 | |
|
97 | 0 | if (!BIO_indent(bp, off, 128)) |
98 | 0 | goto err; |
99 | | |
100 | 0 | nid = EC_GROUP_get_curve_name(x); |
101 | 0 | if (nid == 0) |
102 | 0 | goto err; |
103 | 0 | if (BIO_printf(bp, "ASN1 OID: %s", OBJ_nid2sn(nid)) <= 0) |
104 | 0 | goto err; |
105 | 0 | if (BIO_printf(bp, "\n") <= 0) |
106 | 0 | goto err; |
107 | 0 | nname = EC_curve_nid2nist(nid); |
108 | 0 | if (nname) { |
109 | 0 | if (!BIO_indent(bp, off, 128)) |
110 | 0 | goto err; |
111 | 0 | if (BIO_printf(bp, "NIST CURVE: %s\n", nname) <= 0) |
112 | 0 | goto err; |
113 | 0 | } |
114 | 0 | } else { |
115 | | /* explicit parameters */ |
116 | 0 | int is_char_two = 0; |
117 | 0 | point_conversion_form_t form; |
118 | 0 | int tmp_nid = EC_METHOD_get_field_type(EC_GROUP_method_of(x)); |
119 | |
|
120 | 0 | if (tmp_nid == NID_X9_62_characteristic_two_field) |
121 | 0 | is_char_two = 1; |
122 | |
|
123 | 0 | if ((p = BN_new()) == NULL || (a = BN_new()) == NULL || |
124 | 0 | (b = BN_new()) == NULL) { |
125 | 0 | reason = ERR_R_MALLOC_FAILURE; |
126 | 0 | goto err; |
127 | 0 | } |
128 | | |
129 | 0 | if (!EC_GROUP_get_curve(x, p, a, b, ctx)) { |
130 | 0 | reason = ERR_R_EC_LIB; |
131 | 0 | goto err; |
132 | 0 | } |
133 | | |
134 | 0 | if ((point = EC_GROUP_get0_generator(x)) == NULL) { |
135 | 0 | reason = ERR_R_EC_LIB; |
136 | 0 | goto err; |
137 | 0 | } |
138 | 0 | order = EC_GROUP_get0_order(x); |
139 | 0 | cofactor = EC_GROUP_get0_cofactor(x); |
140 | 0 | if (order == NULL) { |
141 | 0 | reason = ERR_R_EC_LIB; |
142 | 0 | goto err; |
143 | 0 | } |
144 | | |
145 | 0 | form = EC_GROUP_get_point_conversion_form(x); |
146 | |
|
147 | 0 | if ((gen = EC_POINT_point2bn(x, point, form, NULL, ctx)) == NULL) { |
148 | 0 | reason = ERR_R_EC_LIB; |
149 | 0 | goto err; |
150 | 0 | } |
151 | | |
152 | 0 | if ((seed = EC_GROUP_get0_seed(x)) != NULL) |
153 | 0 | seed_len = EC_GROUP_get_seed_len(x); |
154 | |
|
155 | 0 | if (!BIO_indent(bp, off, 128)) |
156 | 0 | goto err; |
157 | | |
158 | | /* print the 'short name' of the field type */ |
159 | 0 | if (BIO_printf(bp, "Field Type: %s\n", OBJ_nid2sn(tmp_nid)) |
160 | 0 | <= 0) |
161 | 0 | goto err; |
162 | | |
163 | 0 | if (is_char_two) { |
164 | | /* print the 'short name' of the base type OID */ |
165 | 0 | int basis_type = EC_GROUP_get_basis_type(x); |
166 | 0 | if (basis_type == 0) |
167 | 0 | goto err; |
168 | | |
169 | 0 | if (!BIO_indent(bp, off, 128)) |
170 | 0 | goto err; |
171 | | |
172 | 0 | if (BIO_printf(bp, "Basis Type: %s\n", |
173 | 0 | OBJ_nid2sn(basis_type)) <= 0) |
174 | 0 | goto err; |
175 | | |
176 | | /* print the polynomial */ |
177 | 0 | if ((p != NULL) && !ASN1_bn_print(bp, "Polynomial:", p, NULL, |
178 | 0 | off)) |
179 | 0 | goto err; |
180 | 0 | } else { |
181 | 0 | if ((p != NULL) && !ASN1_bn_print(bp, "Prime:", p, NULL, off)) |
182 | 0 | goto err; |
183 | 0 | } |
184 | 0 | if ((a != NULL) && !ASN1_bn_print(bp, "A: ", a, NULL, off)) |
185 | 0 | goto err; |
186 | 0 | if ((b != NULL) && !ASN1_bn_print(bp, "B: ", b, NULL, off)) |
187 | 0 | goto err; |
188 | 0 | if (form == POINT_CONVERSION_COMPRESSED) { |
189 | 0 | if ((gen != NULL) && !ASN1_bn_print(bp, gen_compressed, gen, |
190 | 0 | NULL, off)) |
191 | 0 | goto err; |
192 | 0 | } else if (form == POINT_CONVERSION_UNCOMPRESSED) { |
193 | 0 | if ((gen != NULL) && !ASN1_bn_print(bp, gen_uncompressed, gen, |
194 | 0 | NULL, off)) |
195 | 0 | goto err; |
196 | 0 | } else { /* form == POINT_CONVERSION_HYBRID */ |
197 | |
|
198 | 0 | if ((gen != NULL) && !ASN1_bn_print(bp, gen_hybrid, gen, |
199 | 0 | NULL, off)) |
200 | 0 | goto err; |
201 | 0 | } |
202 | 0 | if ((order != NULL) && !ASN1_bn_print(bp, "Order: ", order, |
203 | 0 | NULL, off)) |
204 | 0 | goto err; |
205 | 0 | if ((cofactor != NULL) && !ASN1_bn_print(bp, "Cofactor: ", cofactor, |
206 | 0 | NULL, off)) |
207 | 0 | goto err; |
208 | 0 | if (seed && !print_bin(bp, "Seed:", seed, seed_len, off)) |
209 | 0 | goto err; |
210 | 0 | } |
211 | 0 | ret = 1; |
212 | 0 | err: |
213 | 0 | if (!ret) |
214 | 0 | ECerr(EC_F_ECPKPARAMETERS_PRINT, reason); |
215 | 0 | BN_free(p); |
216 | 0 | BN_free(a); |
217 | 0 | BN_free(b); |
218 | 0 | BN_free(gen); |
219 | 0 | BN_CTX_free(ctx); |
220 | 0 | return ret; |
221 | 0 | } |
222 | | |
223 | | static int print_bin(BIO *fp, const char *name, const unsigned char *buf, |
224 | | size_t len, int off) |
225 | 0 | { |
226 | 0 | size_t i; |
227 | 0 | char str[128 + 1 + 4]; |
228 | |
|
229 | 0 | if (buf == NULL) |
230 | 0 | return 1; |
231 | 0 | if (off > 0) { |
232 | 0 | if (off > 128) |
233 | 0 | off = 128; |
234 | 0 | memset(str, ' ', off); |
235 | 0 | if (BIO_write(fp, str, off) <= 0) |
236 | 0 | return 0; |
237 | 0 | } else { |
238 | 0 | off = 0; |
239 | 0 | } |
240 | | |
241 | 0 | if (BIO_printf(fp, "%s", name) <= 0) |
242 | 0 | return 0; |
243 | | |
244 | 0 | for (i = 0; i < len; i++) { |
245 | 0 | if ((i % 15) == 0) { |
246 | 0 | str[0] = '\n'; |
247 | 0 | memset(&(str[1]), ' ', off + 4); |
248 | 0 | if (BIO_write(fp, str, off + 1 + 4) <= 0) |
249 | 0 | return 0; |
250 | 0 | } |
251 | 0 | if (BIO_printf(fp, "%02x%s", buf[i], ((i + 1) == len) ? "" : ":") <= |
252 | 0 | 0) |
253 | 0 | return 0; |
254 | 0 | } |
255 | 0 | if (BIO_write(fp, "\n", 1) <= 0) |
256 | 0 | return 0; |
257 | | |
258 | 0 | return 1; |
259 | 0 | } |