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 * [email protected].
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 * ([email protected]). This product includes software written by Tim
51 * Hudson ([email protected]). */
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 = DSA_get0_priv_key(x);
198 }
199
200 const BIGNUM *pub_key = NULL;
201 if (ptype > 0) {
202 pub_key = DSA_get0_pub_key(x);
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(DSA_get0_p(x))) <=
214 0 ||
215 // |priv_key| and |pub_key| may be NULL, in which case |bn_print| will
216 // silently skip them.
217 !bn_print(bp, "priv:", priv_key, off) ||
218 !bn_print(bp, "pub:", pub_key, off) ||
219 !bn_print(bp, "P:", DSA_get0_p(x), off) ||
220 !bn_print(bp, "Q:", DSA_get0_q(x), off) ||
221 !bn_print(bp, "G:", DSA_get0_g(x), off)) {
222 return 0;
223 }
224
225 return 1;
226 }
227
dsa_param_print(BIO * bp,const EVP_PKEY * pkey,int indent)228 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
229 return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 0);
230 }
231
dsa_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent)232 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
233 return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 1);
234 }
235
dsa_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent)236 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
237 return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 2);
238 }
239
240
241 // EC keys.
242
do_EC_KEY_print(BIO * bp,const EC_KEY * x,int off,int ktype)243 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
244 const EC_GROUP *group;
245 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
246 OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
247 return 0;
248 }
249
250 const char *ecstr;
251 if (ktype == 2) {
252 ecstr = "Private-Key";
253 } else if (ktype == 1) {
254 ecstr = "Public-Key";
255 } else {
256 ecstr = "ECDSA-Parameters";
257 }
258
259 if (!BIO_indent(bp, off, 128)) {
260 return 0;
261 }
262 int curve_name = EC_GROUP_get_curve_name(group);
263 if (BIO_printf(bp, "%s: (%s)\n", ecstr,
264 curve_name == NID_undef
265 ? "unknown curve"
266 : EC_curve_nid2nist(curve_name)) <= 0) {
267 return 0;
268 }
269
270 if (ktype == 2) {
271 const BIGNUM *priv_key = EC_KEY_get0_private_key(x);
272 if (priv_key != NULL && //
273 !bn_print(bp, "priv:", priv_key, off)) {
274 return 0;
275 }
276 }
277
278 if (ktype > 0 && EC_KEY_get0_public_key(x) != NULL) {
279 uint8_t *pub = NULL;
280 size_t pub_len = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
281 if (pub_len == 0) {
282 return 0;
283 }
284 int ret = BIO_indent(bp, off, 128) && //
285 BIO_puts(bp, "pub:") > 0 && //
286 print_hex(bp, pub, pub_len, off);
287 OPENSSL_free(pub);
288 if (!ret) {
289 return 0;
290 }
291 }
292
293 return 1;
294 }
295
eckey_param_print(BIO * bp,const EVP_PKEY * pkey,int indent)296 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
297 return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 0);
298 }
299
eckey_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent)300 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
301 return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 1);
302 }
303
304
eckey_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent)305 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
306 return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 2);
307 }
308
309
310 typedef struct {
311 int type;
312 int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent);
313 int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent);
314 int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent);
315 } EVP_PKEY_PRINT_METHOD;
316
317 static EVP_PKEY_PRINT_METHOD kPrintMethods[] = {
318 {
319 EVP_PKEY_RSA,
320 rsa_pub_print,
321 rsa_priv_print,
322 NULL /* param_print */,
323 },
324 {
325 EVP_PKEY_DSA,
326 dsa_pub_print,
327 dsa_priv_print,
328 dsa_param_print,
329 },
330 {
331 EVP_PKEY_EC,
332 eckey_pub_print,
333 eckey_priv_print,
334 eckey_param_print,
335 },
336 };
337
338 static size_t kPrintMethodsLen = OPENSSL_ARRAY_SIZE(kPrintMethods);
339
find_method(int type)340 static EVP_PKEY_PRINT_METHOD *find_method(int type) {
341 for (size_t i = 0; i < kPrintMethodsLen; i++) {
342 if (kPrintMethods[i].type == type) {
343 return &kPrintMethods[i];
344 }
345 }
346 return NULL;
347 }
348
print_unsupported(BIO * out,const EVP_PKEY * pkey,int indent,const char * kstr)349 static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
350 const char *kstr) {
351 BIO_indent(out, indent, 128);
352 BIO_printf(out, "%s algorithm unsupported\n", kstr);
353 return 1;
354 }
355
EVP_PKEY_print_public(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)356 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
357 ASN1_PCTX *pctx) {
358 EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
359 if (method != NULL && method->pub_print != NULL) {
360 return method->pub_print(out, pkey, indent);
361 }
362 return print_unsupported(out, pkey, indent, "Public Key");
363 }
364
EVP_PKEY_print_private(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)365 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
366 ASN1_PCTX *pctx) {
367 EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
368 if (method != NULL && method->priv_print != NULL) {
369 return method->priv_print(out, pkey, indent);
370 }
371 return print_unsupported(out, pkey, indent, "Private Key");
372 }
373
EVP_PKEY_print_params(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)374 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
375 ASN1_PCTX *pctx) {
376 EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
377 if (method != NULL && method->param_print != NULL) {
378 return method->param_print(out, pkey, indent);
379 }
380 return print_unsupported(out, pkey, indent, "Parameters");
381 }
382