Coverage Report

Created: 2023-06-07 07:11

/src/boringssl/crypto/evp/print.c
Line
Count
Source (jump to first uncovered line)
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
67
425
static int print_hex(BIO *bp, const uint8_t *data, size_t len, int off) {
68
47.0k
  for (size_t i = 0; i < len; i++) {
69
46.6k
    if ((i % 15) == 0) {
70
3.35k
      if (BIO_puts(bp, "\n") <= 0 ||  //
71
3.35k
          !BIO_indent(bp, off + 4, 128)) {
72
0
        return 0;
73
0
      }
74
3.35k
    }
75
46.6k
    if (BIO_printf(bp, "%02x%s", data[i], (i + 1 == len) ? "" : ":") <= 0) {
76
0
      return 0;
77
0
    }
78
46.6k
  }
79
425
  if (BIO_write(bp, "\n", 1) <= 0) {
80
0
    return 0;
81
0
  }
82
425
  return 1;
83
425
}
84
85
735
static int bn_print(BIO *bp, const char *name, const BIGNUM *num, int off) {
86
735
  if (num == NULL) {
87
31
    return 1;
88
31
  }
89
90
704
  if (!BIO_indent(bp, off, 128)) {
91
0
    return 0;
92
0
  }
93
704
  if (BN_is_zero(num)) {
94
0
    if (BIO_printf(bp, "%s 0\n", name) <= 0) {
95
0
      return 0;
96
0
    }
97
0
    return 1;
98
0
  }
99
100
704
  uint64_t u64;
101
704
  if (BN_get_u64(num, &u64)) {
102
290
    const char *neg = BN_is_negative(num) ? "-" : "";
103
290
    return BIO_printf(bp, "%s %s%" PRIu64 " (%s0x%" PRIx64 ")\n", name, neg,
104
290
                      u64, neg, u64) > 0;
105
290
  }
106
107
414
  if (BIO_printf(bp, "%s%s", name,
108
414
                  (BN_is_negative(num)) ? " (Negative)" : "") <= 0) {
109
0
    return 0;
110
0
  }
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
414
  size_t len = BN_num_bytes(num);
118
414
  uint8_t *buf = OPENSSL_malloc(len + 1);
119
414
  if (buf == NULL) {
120
0
    return 0;
121
0
  }
122
123
414
  buf[0] = 0;
124
414
  BN_bn2bin(num, buf + 1);
125
414
  int ret;
126
414
  if (len > 0 && (buf[1] & 0x80) != 0) {
127
    // Print the whole buffer.
128
314
    ret = print_hex(bp, buf, len + 1, off);
129
314
  } else {
130
    // Skip the leading zero.
131
100
    ret = print_hex(bp, buf + 1, len, off);
132
100
  }
133
414
  OPENSSL_free(buf);
134
414
  return ret;
135
414
}
136
137
// RSA keys.
138
139
static int do_rsa_print(BIO *out, const RSA *rsa, int off,
140
290
                        int include_private) {
141
290
  int mod_len = 0;
142
290
  if (rsa->n != NULL) {
143
290
    mod_len = BN_num_bits(rsa->n);
144
290
  }
145
146
290
  if (!BIO_indent(out, off, 128)) {
147
0
    return 0;
148
0
  }
149
150
290
  const char *s, *str;
151
290
  if (include_private && rsa->d) {
152
0
    if (BIO_printf(out, "Private-Key: (%d bit)\n", mod_len) <= 0) {
153
0
      return 0;
154
0
    }
155
0
    str = "modulus:";
156
0
    s = "publicExponent:";
157
290
  } else {
158
290
    if (BIO_printf(out, "Public-Key: (%d bit)\n", mod_len) <= 0) {
159
0
      return 0;
160
0
    }
161
290
    str = "Modulus:";
162
290
    s = "Exponent:";
163
290
  }
164
290
  if (!bn_print(out, str, rsa->n, off) ||
165
290
      !bn_print(out, s, rsa->e, off)) {
166
0
    return 0;
167
0
  }
168
169
290
  if (include_private) {
170
0
    if (!bn_print(out, "privateExponent:", rsa->d, off) ||
171
0
        !bn_print(out, "prime1:", rsa->p, off) ||
172
0
        !bn_print(out, "prime2:", rsa->q, off) ||
173
0
        !bn_print(out, "exponent1:", rsa->dmp1, off) ||
174
0
        !bn_print(out, "exponent2:", rsa->dmq1, off) ||
175
0
        !bn_print(out, "coefficient:", rsa->iqmp, off)) {
176
0
      return 0;
177
0
    }
178
0
  }
179
180
290
  return 1;
181
290
}
182
183
290
static int rsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
184
290
  return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 0);
185
290
}
186
187
0
static int rsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
188
0
  return do_rsa_print(bp, EVP_PKEY_get0_RSA(pkey), indent, 1);
189
0
}
190
191
192
// DSA keys.
193
194
31
static int do_dsa_print(BIO *bp, const DSA *x, int off, int ptype) {
195
31
  const BIGNUM *priv_key = NULL;
196
31
  if (ptype == 2) {
197
0
    priv_key = DSA_get0_priv_key(x);
198
0
  }
199
200
31
  const BIGNUM *pub_key = NULL;
201
31
  if (ptype > 0) {
202
31
    pub_key = DSA_get0_pub_key(x);
203
31
  }
204
205
31
  const char *ktype = "DSA-Parameters";
206
31
  if (ptype == 2) {
207
0
    ktype = "Private-Key";
208
31
  } else if (ptype == 1) {
209
31
    ktype = "Public-Key";
210
31
  }
211
212
31
  if (!BIO_indent(bp, off, 128) ||
213
31
      BIO_printf(bp, "%s: (%u bit)\n", ktype, BN_num_bits(DSA_get0_p(x))) <=
214
31
          0 ||
215
      // |priv_key| and |pub_key| may be NULL, in which case |bn_print| will
216
      // silently skip them.
217
31
      !bn_print(bp, "priv:", priv_key, off) ||
218
31
      !bn_print(bp, "pub:", pub_key, off) ||
219
31
      !bn_print(bp, "P:", DSA_get0_p(x), off) ||
220
31
      !bn_print(bp, "Q:", DSA_get0_q(x), off) ||
221
31
      !bn_print(bp, "G:", DSA_get0_g(x), off)) {
222
0
    return 0;
223
0
  }
224
225
31
  return 1;
226
31
}
227
228
0
static int dsa_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
229
0
  return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 0);
230
0
}
231
232
31
static int dsa_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
233
31
  return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 1);
234
31
}
235
236
0
static int dsa_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
237
0
  return do_dsa_print(bp, EVP_PKEY_get0_DSA(pkey), indent, 2);
238
0
}
239
240
241
// EC keys.
242
243
11
static int do_EC_KEY_print(BIO *bp, const EC_KEY *x, int off, int ktype) {
244
11
  const EC_GROUP *group;
245
11
  if (x == NULL || (group = EC_KEY_get0_group(x)) == NULL) {
246
0
    OPENSSL_PUT_ERROR(EVP, ERR_R_PASSED_NULL_PARAMETER);
247
0
    return 0;
248
0
  }
249
250
11
  const char *ecstr;
251
11
  if (ktype == 2) {
252
0
    ecstr = "Private-Key";
253
11
  } else if (ktype == 1) {
254
11
    ecstr = "Public-Key";
255
11
  } else {
256
0
    ecstr = "ECDSA-Parameters";
257
0
  }
258
259
11
  if (!BIO_indent(bp, off, 128)) {
260
0
    return 0;
261
0
  }
262
11
  int curve_name = EC_GROUP_get_curve_name(group);
263
11
  if (BIO_printf(bp, "%s: (%s)\n", ecstr,
264
11
                 curve_name == NID_undef
265
11
                     ? "unknown curve"
266
11
                     : EC_curve_nid2nist(curve_name)) <= 0) {
267
0
    return 0;
268
0
  }
269
270
11
  if (ktype == 2) {
271
0
    const BIGNUM *priv_key = EC_KEY_get0_private_key(x);
272
0
    if (priv_key != NULL &&  //
273
0
        !bn_print(bp, "priv:", priv_key, off)) {
274
0
      return 0;
275
0
    }
276
0
  }
277
278
11
  if (ktype > 0 && EC_KEY_get0_public_key(x) != NULL) {
279
11
    uint8_t *pub = NULL;
280
11
    size_t pub_len = EC_KEY_key2buf(x, EC_KEY_get_conv_form(x), &pub, NULL);
281
11
    if (pub_len == 0) {
282
0
      return 0;
283
0
    }
284
11
    int ret = BIO_indent(bp, off, 128) &&  //
285
11
              BIO_puts(bp, "pub:") > 0 &&  //
286
11
              print_hex(bp, pub, pub_len, off);
287
11
    OPENSSL_free(pub);
288
11
    if (!ret) {
289
0
      return 0;
290
0
    }
291
11
  }
292
293
11
  return 1;
294
11
}
295
296
0
static int eckey_param_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
297
0
  return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 0);
298
0
}
299
300
11
static int eckey_pub_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
301
11
  return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 1);
302
11
}
303
304
305
0
static int eckey_priv_print(BIO *bp, const EVP_PKEY *pkey, int indent) {
306
0
  return do_EC_KEY_print(bp, EVP_PKEY_get0_EC_KEY(pkey), indent, 2);
307
0
}
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
340
332
static EVP_PKEY_PRINT_METHOD *find_method(int type) {
341
385
  for (size_t i = 0; i < kPrintMethodsLen; i++) {
342
385
    if (kPrintMethods[i].type == type) {
343
332
      return &kPrintMethods[i];
344
332
    }
345
385
  }
346
0
  return NULL;
347
332
}
348
349
static int print_unsupported(BIO *out, const EVP_PKEY *pkey, int indent,
350
0
                             const char *kstr) {
351
0
  BIO_indent(out, indent, 128);
352
0
  BIO_printf(out, "%s algorithm unsupported\n", kstr);
353
0
  return 1;
354
0
}
355
356
int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey, int indent,
357
332
                          ASN1_PCTX *pctx) {
358
332
  EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
359
332
  if (method != NULL && method->pub_print != NULL) {
360
332
    return method->pub_print(out, pkey, indent);
361
332
  }
362
0
  return print_unsupported(out, pkey, indent, "Public Key");
363
332
}
364
365
int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey, int indent,
366
0
                           ASN1_PCTX *pctx) {
367
0
  EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
368
0
  if (method != NULL && method->priv_print != NULL) {
369
0
    return method->priv_print(out, pkey, indent);
370
0
  }
371
0
  return print_unsupported(out, pkey, indent, "Private Key");
372
0
}
373
374
int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey, int indent,
375
0
                          ASN1_PCTX *pctx) {
376
0
  EVP_PKEY_PRINT_METHOD *method = find_method(EVP_PKEY_id(pkey));
377
0
  if (method != NULL && method->param_print != NULL) {
378
0
    return method->param_print(out, pkey, indent);
379
0
  }
380
0
  return print_unsupported(out, pkey, indent, "Parameters");
381
0
}