Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/evp_extra/print.c

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