1 /* ====================================================================
2 * Copyright (c) 2006 The OpenSSL Project. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in
13 * the documentation and/or other materials provided with the
14 * distribution.
15 *
16 * 3. All advertising materials mentioning features or use of this
17 * software must display the following acknowledgment:
18 * "This product includes software developed by the OpenSSL Project
19 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
20 *
21 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
22 * endorse or promote products derived from this software without
23 * prior written permission. For written permission, please contact
24 * licensing@OpenSSL.org.
25 *
26 * 5. Products derived from this software may not be called "OpenSSL"
27 * nor may "OpenSSL" appear in their names without prior written
28 * permission of the OpenSSL Project.
29 *
30 * 6. Redistributions of any form whatsoever must retain the following
31 * acknowledgment:
32 * "This product includes software developed by the OpenSSL Project
33 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
36 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
38 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
41 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
42 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
43 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
44 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
46 * OF THE POSSIBILITY OF SUCH DAMAGE.
47 * ====================================================================
48 *
49 * This product includes cryptographic software written by Eric Young
50 * (eay@cryptsoft.com). This product includes software written by Tim
51 * Hudson (tjh@cryptsoft.com). */
52
53 #include <openssl/evp.h>
54
55 #include <openssl/bio.h>
56 #include <openssl/bn.h>
57 #include <openssl/dsa.h>
58 #include <openssl/ec.h>
59 #include <openssl/ec_key.h>
60 #include <openssl/mem.h>
61 #include <openssl/rsa.h>
62
63 #include "../internal.h"
64 #include "../fipsmodule/rsa/internal.h"
65
66
print_hex(BIO * bp,const uint8_t * data,size_t len,int off)67 static int print_hex(BIO *bp, const uint8_t *data, size_t len, int off) {
68 for (size_t i = 0; i < len; i++) {
69 if ((i % 15) == 0) {
70 if (BIO_puts(bp, "\n") <= 0 || //
71 !BIO_indent(bp, off + 4, 128)) {
72 return 0;
73 }
74 }
75 if (BIO_printf(bp, "%02x%s", data[i], (i + 1 == len) ? "" : ":") <= 0) {
76 return 0;
77 }
78 }
79 if (BIO_write(bp, "\n", 1) <= 0) {
80 return 0;
81 }
82 return 1;
83 }
84
bn_print(BIO * bp,const char * name,const BIGNUM * num,int off)85 static int bn_print(BIO *bp, const char *name, const BIGNUM *num, int off) {
86 if (num == NULL) {
87 return 1;
88 }
89
90 if (!BIO_indent(bp, off, 128)) {
91 return 0;
92 }
93 if (BN_is_zero(num)) {
94 if (BIO_printf(bp, "%s 0\n", name) <= 0) {
95 return 0;
96 }
97 return 1;
98 }
99
100 uint64_t u64;
101 if (BN_get_u64(num, &u64)) {
102 const char *neg = BN_is_negative(num) ? "-" : "";
103 return BIO_printf(bp, "%s %s%" PRIu64 " (%s0x%" PRIx64 ")\n", name, neg,
104 u64, neg, u64) > 0;
105 }
106
107 if (BIO_printf(bp, "%s%s", name,
108 (BN_is_negative(num)) ? " (Negative)" : "") <= 0) {
109 return 0;
110 }
111
112 // Print |num| in hex, adding a leading zero, as in ASN.1, if the high bit
113 // is set.
114 //
115 // TODO(davidben): Do we need to do this? We already print "(Negative)" above
116 // and negative values are never valid in keys anyway.
117 size_t len = BN_num_bytes(num);
118 uint8_t *buf = OPENSSL_malloc(len + 1);
119 if (buf == NULL) {
120 return 0;
121 }
122
123 buf[0] = 0;
124 BN_bn2bin(num, buf + 1);
125 int ret;
126 if (len > 0 && (buf[1] & 0x80) != 0) {
127 // Print the whole buffer.
128 ret = print_hex(bp, buf, len + 1, off);
129 } else {
130 // Skip the leading zero.
131 ret = print_hex(bp, buf + 1, len, off);
132 }
133 OPENSSL_free(buf);
134 return ret;
135 }
136
137 // RSA keys.
138
do_rsa_print(BIO * out,const RSA * rsa,int off,int include_private)139 static int do_rsa_print(BIO *out, const RSA *rsa, int off,
140 int include_private) {
141 int mod_len = 0;
142 if (rsa->n != NULL) {
143 mod_len = BN_num_bits(rsa->n);
144 }
145
146 if (!BIO_indent(out, off, 128)) {
147 return 0;
148 }
149
150 const char *s, *str;
151 if (include_private && rsa->d) {
152 if (BIO_printf(out, "Private-Key: (%d bit)\n", mod_len) <= 0) {
153 return 0;
154 }
155 str = "modulus:";
156 s = "publicExponent:";
157 } else {
158 if (BIO_printf(out, "Public-Key: (%d bit)\n", mod_len) <= 0) {
159 return 0;
160 }
161 str = "Modulus:";
162 s = "Exponent:";
163 }
164 if (!bn_print(out, str, rsa->n, off) ||
165 !bn_print(out, s, rsa->e, off)) {
166 return 0;
167 }
168
169 if (include_private) {
170 if (!bn_print(out, "privateExponent:", rsa->d, off) ||
171 !bn_print(out, "prime1:", rsa->p, off) ||
172 !bn_print(out, "prime2:", rsa->q, off) ||
173 !bn_print(out, "exponent1:", rsa->dmp1, off) ||
174 !bn_print(out, "exponent2:", rsa->dmq1, off) ||
175 !bn_print(out, "coefficient:", rsa->iqmp, off)) {
176 return 0;
177 }
178 }
179
180 return 1;
181 }
182
rsa_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent)183 static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
184 return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 0);
185 }
186
rsa_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent)187 static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
188 return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 1);
189 }
190
191
192 // DSA keys.
193
do_dsa_print(BIO * bp,const DSA * x,int off,int ptype)194 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
195 const BIGNUM *priv_key = NULL;
196 if (ptype == 2) {
197 priv_key = x->priv_key;
198 }
199
200 const BIGNUM *pub_key = NULL;
201 if (ptype > 0) {
202 pub_key = x->pub_key;
203 }
204
205 const char *ktype = "DSA-Parameters";
206 if (ptype == 2) {
207 ktype = "Private-Key";
208 } else if (ptype == 1) {
209 ktype = "Public-Key";
210 }
211
212 if (!BIO_indent(bp, off, 128) ||
213 BIO_printf(bp, "%s: (%u bit)\n", ktype, BN_num_bits(x->p)) <= 0 ||
214 // |priv_key| and |pub_key| may be NULL, in which case |bn_print| will
215 // silently skip them.
216 !bn_print(bp, "priv:", priv_key, off) ||
217 !bn_print(bp, "pub:", pub_key, off) ||
218 !bn_print(bp, "P:", x->p, off) ||
219 !bn_print(bp, "Q:", x->q, off) ||
220 !bn_print(bp, "G:", x->g, off)) {
221 return 0;
222 }
223
224 return 1;
225 }
226
dsa_param_print(BIO * bp,const EVP_PKEY * pkey,int indent)227 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
228 return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 0);
229 }
230
dsa_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent)231 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
232 return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 1);
233 }
234
dsa_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent)235 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
236 return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 2);
237 }
238
239
240 // EC keys.
241
do_EC_KEY_print(BIO * bp,const EC_KEY * x,int off,int ktype)242 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
243 const EC_GROUP *group;
244 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
245 OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
246 return 0;
247 }
248
249 const char *ecstr;
250 if (ktype == 2) {
251 ecstr = "Private-Key";
252 } else if (ktype == 1) {
253 ecstr = "Public-Key";
254 } else {
255 ecstr = "ECDSA-Parameters";
256 }
257
258 if (!BIO_indent(bp, off, 128)) {
259 return 0;
260 }
261 int curve_name = EC_GROUP_get_curve_name(group);
262 if (BIO_printf(bp, "%s: (%s)\n", ecstr,
263 curve_name == NID_undef
264 ? "unknown curve"
265 : EC_curve_nid2nist(curve_name)) <= 0) {
266 return 0;
267 }
268
269 if (ktype == 2) {
270 const BIGNUM *priv_key = EC_KEY_get0_private_key(x);
271 if (priv_key != NULL && //
272 !bn_print(bp, "priv:", priv_key, off)) {
273 return 0;
274 }
275 }
276
277 if (ktype > 0 && EC_KEY_get0_public_key(x) != NULL) {
278 uint8_t *pub = NULL;
279 size_t pub_len = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
280 if (pub_len == 0) {
281 return 0;
282 }
283 int ret = BIO_indent(bp, off, 128) && //
284 BIO_puts(bp, "pub:") > 0 && //
285 print_hex(bp, pub, pub_len, off);
286 OPENSSL_free(pub);
287 if (!ret) {
288 return 0;
289 }
290 }
291
292 return 1;
293 }
294
eckey_param_print(BIO * bp,const EVP_PKEY * pkey,int indent)295 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
296 return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 0);
297 }
298
eckey_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent)299 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
300 return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 1);
301 }
302
303
eckey_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent)304 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
305 return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 2);
306 }
307
308
309 typedef struct {
310 int type;
311 int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent);
312 int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent);
313 int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent);
314 } EVP_PKEY_PRINT_METHOD;
315
316 static EVP_PKEY_PRINT_METHOD kPrintMethods[] = {
317 {
318 EVP_PKEY_RSA,
319 rsa_pub_print,
320 rsa_priv_print,
321 NULL /* param_print */,
322 },
323 {
324 EVP_PKEY_DSA,
325 dsa_pub_print,
326 dsa_priv_print,
327 dsa_param_print,
328 },
329 {
330 EVP_PKEY_EC,
331 eckey_pub_print,
332 eckey_priv_print,
333 eckey_param_print,
334 },
335 };
336
337 static size_t kPrintMethodsLen = OPENSSL_ARRAY_SIZE(kPrintMethods);
338
find_method(int type)339 static EVP_PKEY_PRINT_METHOD *find_method(int type) {
340 for (size_t i = 0; i < kPrintMethodsLen; i++) {
341 if (kPrintMethods[i].type == type) {
342 return &kPrintMethods[i];
343 }
344 }
345 return NULL;
346 }
347
print_unsupported(BIO * out,const EVP_PKEY * pkey,int indent,const char * kstr)348 static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
349 const char *kstr) {
350 BIO_indent(out, indent, 128);
351 BIO_printf(out, "%s algorithm unsupported\n", kstr);
352 return 1;
353 }
354
EVP_PKEY_print_public(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)355 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
356 ASN1_PCTX *pctx) {
357 EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
358 if (method != NULL && method->pub_print != NULL) {
359 return method->pub_print(out, pkey, indent);
360 }
361 return print_unsupported(out, pkey, indent, "Public Key");
362 }
363
EVP_PKEY_print_private(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)364 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
365 ASN1_PCTX *pctx) {
366 EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
367 if (method != NULL && method->priv_print != NULL) {
368 return method->priv_print(out, pkey, indent);
369 }
370 return print_unsupported(out, pkey, indent, "Private Key");
371 }
372
EVP_PKEY_print_params(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)373 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
374 ASN1_PCTX *pctx) {
375 EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
376 if (method != NULL && method->param_print != NULL) {
377 return method->param_print(out, pkey, indent);
378 }
379 return print_unsupported(out, pkey, indent, "Parameters");
380 }
381