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