Coverage Report

Created: 2025-06-11 06:40

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