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