1 /*
2 * Copyright 2006-2016 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 #include <openssl/evp.h>
11
12 #include <openssl/bio.h>
13 #include <openssl/bn.h>
14 #include <openssl/dsa.h>
15 #include <openssl/ec.h>
16 #include <openssl/ec_key.h>
17 #include <openssl/mem.h>
18 #include <openssl/rsa.h>
19
20 #include "../fipsmodule/rsa/internal.h"
21 #include "../internal.h"
22
23
print_hex(BIO * bp,const uint8_t * data,size_t len,int off)24 static int print_hex(BIO *bp, const uint8_t *data, size_t len, int off) {
25 for (size_t i = 0; i < len; i++) {
26 if ((i % 15) == 0) {
27 if (BIO_puts(bp, "\n") <= 0 || //
28 !BIO_indent(bp, off + 4, 128)) {
29 return 0;
30 }
31 }
32 if (BIO_printf(bp, "%02x%s", data[i], (i + 1 == len) ? "" : ":") <= 0) {
33 return 0;
34 }
35 }
36 if (BIO_write(bp, "\n", 1) <= 0) {
37 return 0;
38 }
39 return 1;
40 }
41
bn_print(BIO * bp,const char * name,const BIGNUM * num,int off)42 static int bn_print(BIO *bp, const char *name, const BIGNUM *num, int off) {
43 if (num == NULL) {
44 return 1;
45 }
46
47 if (!BIO_indent(bp, off, 128)) {
48 return 0;
49 }
50 if (BN_is_zero(num)) {
51 if (BIO_printf(bp, "%s 0\n", name) <= 0) {
52 return 0;
53 }
54 return 1;
55 }
56
57 uint64_t u64;
58 if (BN_get_u64(num, &u64)) {
59 const char *neg = BN_is_negative(num) ? "-" : "";
60 return BIO_printf(bp, "%s %s%" PRIu64 " (%s0x%" PRIx64 ")\n", name, neg,
61 u64, neg, u64) > 0;
62 }
63
64 if (BIO_printf(bp, "%s%s", name,
65 (BN_is_negative(num)) ? " (Negative)" : "") <= 0) {
66 return 0;
67 }
68
69 // Print |num| in hex, adding a leading zero, as in ASN.1, if the high bit
70 // is set.
71 //
72 // TODO(davidben): Do we need to do this? We already print "(Negative)" above
73 // and negative values are never valid in keys anyway.
74 size_t len = BN_num_bytes(num);
75 uint8_t *buf = reinterpret_cast<uint8_t *>(OPENSSL_malloc(len + 1));
76 if (buf == NULL) {
77 return 0;
78 }
79
80 buf[0] = 0;
81 BN_bn2bin(num, buf + 1);
82 int ret;
83 if (len > 0 && (buf[1] & 0x80) != 0) {
84 // Print the whole buffer.
85 ret = print_hex(bp, buf, len + 1, off);
86 } else {
87 // Skip the leading zero.
88 ret = print_hex(bp, buf + 1, len, off);
89 }
90 OPENSSL_free(buf);
91 return ret;
92 }
93
94 // RSA keys.
95
do_rsa_print(BIO * out,const RSA * rsa,int off,int include_private)96 static int do_rsa_print(BIO *out, const RSA *rsa, int off,
97 int include_private) {
98 int mod_len = 0;
99 if (rsa->n != NULL) {
100 mod_len = BN_num_bits(rsa->n);
101 }
102
103 if (!BIO_indent(out, off, 128)) {
104 return 0;
105 }
106
107 const char *s, *str;
108 if (include_private && rsa->d) {
109 if (BIO_printf(out, "Private-Key: (%d bit)\n", mod_len) <= 0) {
110 return 0;
111 }
112 str = "modulus:";
113 s = "publicExponent:";
114 } else {
115 if (BIO_printf(out, "Public-Key: (%d bit)\n", mod_len) <= 0) {
116 return 0;
117 }
118 str = "Modulus:";
119 s = "Exponent:";
120 }
121 if (!bn_print(out, str, rsa->n, off) || !bn_print(out, s, rsa->e, off)) {
122 return 0;
123 }
124
125 if (include_private) {
126 if (!bn_print(out, "privateExponent:", rsa->d, off) ||
127 !bn_print(out, "prime1:", rsa->p, off) ||
128 !bn_print(out, "prime2:", rsa->q, off) ||
129 !bn_print(out, "exponent1:", rsa->dmp1, off) ||
130 !bn_print(out, "exponent2:", rsa->dmq1, off) ||
131 !bn_print(out, "coefficient:", rsa->iqmp, off)) {
132 return 0;
133 }
134 }
135
136 return 1;
137 }
138
rsa_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent)139 static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
140 return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 0);
141 }
142
rsa_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent)143 static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
144 return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 1);
145 }
146
147
148 // DSA keys.
149
do_dsa_print(BIO * bp,const DSA * x,int off,int ptype)150 static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
151 const BIGNUM *priv_key = NULL;
152 if (ptype == 2) {
153 priv_key = DSA_get0_priv_key(x);
154 }
155
156 const BIGNUM *pub_key = NULL;
157 if (ptype > 0) {
158 pub_key = DSA_get0_pub_key(x);
159 }
160
161 const char *ktype = "DSA-Parameters";
162 if (ptype == 2) {
163 ktype = "Private-Key";
164 } else if (ptype == 1) {
165 ktype = "Public-Key";
166 }
167
168 if (!BIO_indent(bp, off, 128) ||
169 BIO_printf(bp, "%s: (%u bit)\n", ktype, BN_num_bits(DSA_get0_p(x))) <=
170 0 ||
171 // |priv_key| and |pub_key| may be NULL, in which case |bn_print| will
172 // silently skip them.
173 !bn_print(bp, "priv:", priv_key, off) ||
174 !bn_print(bp, "pub:", pub_key, off) ||
175 !bn_print(bp, "P:", DSA_get0_p(x), off) ||
176 !bn_print(bp, "Q:", DSA_get0_q(x), off) ||
177 !bn_print(bp, "G:", DSA_get0_g(x), off)) {
178 return 0;
179 }
180
181 return 1;
182 }
183
dsa_param_print(BIO * bp,const EVP_PKEY * pkey,int indent)184 static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
185 return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 0);
186 }
187
dsa_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent)188 static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
189 return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 1);
190 }
191
dsa_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent)192 static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
193 return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 2);
194 }
195
196
197 // EC keys.
198
do_EC_KEY_print(BIO * bp,const EC_KEY * x,int off,int ktype)199 static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
200 const EC_GROUP *group;
201 if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
202 OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
203 return 0;
204 }
205
206 const char *ecstr;
207 if (ktype == 2) {
208 ecstr = "Private-Key";
209 } else if (ktype == 1) {
210 ecstr = "Public-Key";
211 } else {
212 ecstr = "ECDSA-Parameters";
213 }
214
215 if (!BIO_indent(bp, off, 128)) {
216 return 0;
217 }
218 int curve_name = EC_GROUP_get_curve_name(group);
219 if (BIO_printf(bp, "%s: (%s)\n", ecstr,
220 curve_name == NID_undef
221 ? "unknown curve"
222 : EC_curve_nid2nist(curve_name)) <= 0) {
223 return 0;
224 }
225
226 if (ktype == 2) {
227 const BIGNUM *priv_key = EC_KEY_get0_private_key(x);
228 if (priv_key != NULL && //
229 !bn_print(bp, "priv:", priv_key, off)) {
230 return 0;
231 }
232 }
233
234 if (ktype > 0 && EC_KEY_get0_public_key(x) != NULL) {
235 uint8_t *pub = NULL;
236 size_t pub_len = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
237 if (pub_len == 0) {
238 return 0;
239 }
240 int ret = BIO_indent(bp, off, 128) && //
241 BIO_puts(bp, "pub:") > 0 && //
242 print_hex(bp, pub, pub_len, off);
243 OPENSSL_free(pub);
244 if (!ret) {
245 return 0;
246 }
247 }
248
249 return 1;
250 }
251
eckey_param_print(BIO * bp,const EVP_PKEY * pkey,int indent)252 static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
253 return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 0);
254 }
255
eckey_pub_print(BIO * bp,const EVP_PKEY * pkey,int indent)256 static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
257 return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 1);
258 }
259
260
eckey_priv_print(BIO * bp,const EVP_PKEY * pkey,int indent)261 static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
262 return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 2);
263 }
264
265
266 typedef struct {
267 int type;
268 int (*pub_print)(BIO *out, const EVP_PKEY *pkey, int indent);
269 int (*priv_print)(BIO *out, const EVP_PKEY *pkey, int indent);
270 int (*param_print)(BIO *out, const EVP_PKEY *pkey, int indent);
271 } EVP_PKEY_PRINT_METHOD;
272
273 static EVP_PKEY_PRINT_METHOD kPrintMethods[] = {
274 {
275 EVP_PKEY_RSA,
276 rsa_pub_print,
277 rsa_priv_print,
278 NULL /* param_print */,
279 },
280 {
281 EVP_PKEY_DSA,
282 dsa_pub_print,
283 dsa_priv_print,
284 dsa_param_print,
285 },
286 {
287 EVP_PKEY_EC,
288 eckey_pub_print,
289 eckey_priv_print,
290 eckey_param_print,
291 },
292 };
293
294 static size_t kPrintMethodsLen = OPENSSL_ARRAY_SIZE(kPrintMethods);
295
find_method(int type)296 static EVP_PKEY_PRINT_METHOD *find_method(int type) {
297 for (size_t i = 0; i < kPrintMethodsLen; i++) {
298 if (kPrintMethods[i].type == type) {
299 return &kPrintMethods[i];
300 }
301 }
302 return NULL;
303 }
304
print_unsupported(BIO * out,const EVP_PKEY * pkey,int indent,const char * kstr)305 static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
306 const char *kstr) {
307 BIO_indent(out, indent, 128);
308 BIO_printf(out, "%s algorithm unsupported\n", kstr);
309 return 1;
310 }
311
EVP_PKEY_print_public(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)312 int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
313 ASN1_PCTX *pctx) {
314 EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
315 if (method != NULL && method->pub_print != NULL) {
316 return method->pub_print(out, pkey, indent);
317 }
318 return print_unsupported(out, pkey, indent, "Public Key");
319 }
320
EVP_PKEY_print_private(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)321 int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
322 ASN1_PCTX *pctx) {
323 EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
324 if (method != NULL && method->priv_print != NULL) {
325 return method->priv_print(out, pkey, indent);
326 }
327 return print_unsupported(out, pkey, indent, "Private Key");
328 }
329
EVP_PKEY_print_params(BIO * out,const EVP_PKEY * pkey,int indent,ASN1_PCTX * pctx)330 int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
331 ASN1_PCTX *pctx) {
332 EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
333 if (method != NULL && method->param_print != NULL) {
334 return method->param_print(out, pkey, indent);
335 }
336 return print_unsupported(out, pkey, indent, "Parameters");
337 }
338