Coverage Report

Created: 2026-05-11 06:45

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/boringssl/crypto/x509/t_x509.cc
Line
Count
Source
1
// Copyright 1995-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 <assert.h>
16
17
#include <openssl/asn1.h>
18
#include <openssl/bio.h>
19
#include <openssl/digest.h>
20
#include <openssl/err.h>
21
#include <openssl/evp.h>
22
#include <openssl/mem.h>
23
#include <openssl/obj.h>
24
#include <openssl/x509.h>
25
26
#include "../internal.h"
27
#include "internal.h"
28
29
30
using namespace bssl;
31
32
int X509_print_ex_fp(FILE *fp, const X509 *x, unsigned long nmflag,
33
0
                     unsigned long cflag) {
34
0
  BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
35
0
  if (b == nullptr) {
36
0
    OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
37
0
    return 0;
38
0
  }
39
0
  int ret = X509_print_ex(b, x, nmflag, cflag);
40
0
  BIO_free(b);
41
0
  return ret;
42
0
}
43
44
0
int X509_print_fp(FILE *fp, const X509 *x) {
45
0
  return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
46
0
}
47
48
3.18k
int X509_print(BIO *bp, const X509 *x) {
49
3.18k
  return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT);
50
3.18k
}
51
52
int X509_print_ex(BIO *bp, const X509 *x, unsigned long nmflags,
53
3.18k
                  unsigned long cflag) {
54
3.18k
  auto *impl = FromOpaque(x);
55
3.18k
  char mlch = ' ';
56
3.18k
  int nmindent = 0;
57
3.18k
  if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) {
58
0
    mlch = '\n';
59
0
    nmindent = 12;
60
0
  }
61
62
3.18k
  if (nmflags == X509_FLAG_COMPAT) {
63
3.18k
    nmindent = 16;
64
3.18k
  }
65
66
3.18k
  if (!(cflag & X509_FLAG_NO_HEADER)) {
67
3.18k
    if (BIO_write(bp, "Certificate:\n", 13) <= 0) {
68
0
      return 0;
69
0
    }
70
3.18k
    if (BIO_write(bp, "    Data:\n", 10) <= 0) {
71
0
      return 0;
72
0
    }
73
3.18k
  }
74
3.18k
  if (!(cflag & X509_FLAG_NO_VERSION)) {
75
3.18k
    long l = X509_get_version(x);
76
3.18k
    assert(X509_VERSION_1 <= l && l <= X509_VERSION_3);
77
3.18k
    if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1,
78
3.18k
                   (unsigned long)l) <= 0) {
79
0
      return 0;
80
0
    }
81
3.18k
  }
82
3.18k
  if (!(cflag & X509_FLAG_NO_SERIAL)) {
83
3.18k
    if (BIO_write(bp, "        Serial Number:", 22) <= 0) {
84
0
      return 0;
85
0
    }
86
87
3.18k
    const ASN1_INTEGER *serial = X509_get0_serialNumber(x);
88
3.18k
    uint64_t serial_u64;
89
3.18k
    if (ASN1_INTEGER_get_uint64(&serial_u64, serial)) {
90
929
      assert(serial->type != V_ASN1_NEG_INTEGER);
91
929
      if (BIO_printf(bp, " %" PRIu64 " (0x%" PRIx64 ")\n", serial_u64,
92
929
                     serial_u64) <= 0) {
93
0
        return 0;
94
0
      }
95
2.25k
    } else {
96
2.25k
      ERR_clear_error();  // Clear |ASN1_INTEGER_get_uint64|'s error.
97
2.25k
      const char *neg =
98
2.25k
          (serial->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : "";
99
2.25k
      if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0) {
100
0
        return 0;
101
0
      }
102
103
30.5k
      for (int i = 0; i < serial->length; i++) {
104
28.3k
        if (BIO_printf(bp, "%02x%c", serial->data[i],
105
28.3k
                       ((i + 1 == serial->length) ? '\n' : ':')) <= 0) {
106
0
          return 0;
107
0
        }
108
28.3k
      }
109
2.25k
    }
110
3.18k
  }
111
112
3.18k
  if (!(cflag & X509_FLAG_NO_SIGNAME)) {
113
3.18k
    if (X509_signature_print(bp, &impl->tbs_sig_alg, nullptr) <= 0) {
114
0
      return 0;
115
0
    }
116
3.18k
  }
117
118
3.18k
  if (!(cflag & X509_FLAG_NO_ISSUER)) {
119
3.18k
    if (BIO_printf(bp, "        Issuer:%c", mlch) <= 0) {
120
0
      return 0;
121
0
    }
122
3.18k
    if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags) <
123
3.18k
        0) {
124
0
      return 0;
125
0
    }
126
3.18k
    if (BIO_write(bp, "\n", 1) <= 0) {
127
0
      return 0;
128
0
    }
129
3.18k
  }
130
3.18k
  if (!(cflag & X509_FLAG_NO_VALIDITY)) {
131
3.18k
    if (BIO_write(bp, "        Validity\n", 17) <= 0) {
132
0
      return 0;
133
0
    }
134
3.18k
    if (BIO_write(bp, "            Not Before: ", 24) <= 0) {
135
0
      return 0;
136
0
    }
137
3.18k
    if (!ASN1_TIME_print(bp, X509_get0_notBefore(x))) {
138
0
      return 0;
139
0
    }
140
3.18k
    if (BIO_write(bp, "\n            Not After : ", 25) <= 0) {
141
0
      return 0;
142
0
    }
143
3.18k
    if (!ASN1_TIME_print(bp, X509_get0_notAfter(x))) {
144
0
      return 0;
145
0
    }
146
3.18k
    if (BIO_write(bp, "\n", 1) <= 0) {
147
0
      return 0;
148
0
    }
149
3.18k
  }
150
3.18k
  if (!(cflag & X509_FLAG_NO_SUBJECT)) {
151
3.18k
    if (BIO_printf(bp, "        Subject:%c", mlch) <= 0) {
152
0
      return 0;
153
0
    }
154
3.18k
    if (X509_NAME_print_ex(bp, X509_get_subject_name(x), nmindent, nmflags) <
155
3.18k
        0) {
156
0
      return 0;
157
0
    }
158
3.18k
    if (BIO_write(bp, "\n", 1) <= 0) {
159
0
      return 0;
160
0
    }
161
3.18k
  }
162
3.18k
  if (!(cflag & X509_FLAG_NO_PUBKEY)) {
163
3.18k
    if (BIO_write(bp, "        Subject Public Key Info:\n", 33) <= 0) {
164
0
      return 0;
165
0
    }
166
3.18k
    if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0) {
167
0
      return 0;
168
0
    }
169
3.18k
    if (i2a_ASN1_OBJECT(bp, impl->key.algor.algorithm) <= 0) {
170
0
      return 0;
171
0
    }
172
3.18k
    if (BIO_puts(bp, "\n") <= 0) {
173
0
      return 0;
174
0
    }
175
176
3.18k
    const EVP_PKEY *pkey = X509_get0_pubkey(x);
177
3.18k
    if (pkey == nullptr) {
178
2.61k
      BIO_printf(bp, "%12sUnable to load Public Key\n", "");
179
2.61k
      ERR_print_errors(bp);
180
2.61k
    } else {
181
572
      EVP_PKEY_print_public(bp, pkey, 16, nullptr);
182
572
    }
183
3.18k
  }
184
185
3.18k
  if (!(cflag & X509_FLAG_NO_IDS)) {
186
3.18k
    if (impl->issuerUID) {
187
94
      if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0) {
188
0
        return 0;
189
0
      }
190
94
      if (!X509_signature_dump(bp, impl->issuerUID, 12)) {
191
0
        return 0;
192
0
      }
193
94
    }
194
3.18k
    if (impl->subjectUID) {
195
62
      if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0) {
196
0
        return 0;
197
0
      }
198
62
      if (!X509_signature_dump(bp, impl->subjectUID, 12)) {
199
0
        return 0;
200
0
      }
201
62
    }
202
3.18k
  }
203
204
3.18k
  if (!(cflag & X509_FLAG_NO_EXTENSIONS)) {
205
3.18k
    X509V3_extensions_print(bp, "X509v3 extensions", impl->extensions, cflag,
206
3.18k
                            8);
207
3.18k
  }
208
209
3.18k
  if (!(cflag & X509_FLAG_NO_SIGDUMP)) {
210
3.18k
    if (X509_signature_print(bp, &impl->sig_alg, &impl->signature) <= 0) {
211
0
      return 0;
212
0
    }
213
3.18k
  }
214
3.18k
  if (!(cflag & X509_FLAG_NO_AUX)) {
215
3.18k
    if (!X509_CERT_AUX_print(bp, impl->aux, 0)) {
216
0
      return 0;
217
0
    }
218
3.18k
  }
219
220
3.18k
  return 1;
221
3.18k
}
222
223
int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg,
224
6.37k
                         const ASN1_STRING *sig) {
225
6.37k
  if (BIO_puts(bp, "    Signature Algorithm: ") <= 0) {
226
0
    return 0;
227
0
  }
228
6.37k
  if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) {
229
0
    return 0;
230
0
  }
231
232
  // RSA-PSS signatures have parameters to print.
233
6.37k
  int sig_nid = OBJ_obj2nid(sigalg->algorithm);
234
6.37k
  if (sig_nid == NID_rsassaPss &&
235
228
      !x509_print_rsa_pss_params(bp, sigalg, 9, nullptr)) {
236
0
    return 0;
237
0
  }
238
239
6.37k
  if (sig) {
240
3.18k
    return X509_signature_dump(bp, sig, 9);
241
3.18k
  } else if (BIO_puts(bp, "\n") <= 0) {
242
0
    return 0;
243
0
  }
244
3.18k
  return 1;
245
6.37k
}
246
247
6.37k
int X509_NAME_print(BIO *bp, const X509_NAME *name, int obase) {
248
6.37k
  char *s, *c, *b;
249
6.37k
  int ret = 0, i;
250
251
6.37k
  b = X509_NAME_oneline(name, nullptr, 0);
252
6.37k
  if (!b) {
253
0
    return 0;
254
0
  }
255
6.37k
  if (!*b) {
256
668
    OPENSSL_free(b);
257
668
    return 1;
258
668
  }
259
5.70k
  s = b + 1;  // skip the first slash
260
261
5.70k
  c = s;
262
512k
  for (;;) {
263
512k
    if (((*s == '/') && ((s[1] >= 'A') && (s[1] <= 'Z') &&
264
3.60k
                         ((s[2] == '=') || ((s[2] >= 'A') && (s[2] <= 'Z') &&
265
2.44k
                                            (s[3] == '='))))) ||
266
509k
        (*s == '\0')) {
267
8.60k
      i = s - c;
268
8.60k
      if (BIO_write(bp, c, i) != i) {
269
0
        goto err;
270
0
      }
271
8.60k
      c = s + 1;  // skip following slash
272
8.60k
      if (*s != '\0') {
273
2.90k
        if (BIO_write(bp, ", ", 2) != 2) {
274
0
          goto err;
275
0
        }
276
2.90k
      }
277
8.60k
    }
278
512k
    if (*s == '\0') {
279
5.70k
      break;
280
5.70k
    }
281
506k
    s++;
282
506k
  }
283
284
5.70k
  ret = 1;
285
5.70k
  if (0) {
286
0
  err:
287
0
    OPENSSL_PUT_ERROR(X509, ERR_R_BUF_LIB);
288
0
  }
289
5.70k
  OPENSSL_free(b);
290
5.70k
  return ret;
291
5.70k
}