/src/openssl30/crypto/x509/t_x509.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * |
4 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
5 | | * this file except in compliance with the License. You can obtain a copy |
6 | | * in the file LICENSE in the source distribution or at |
7 | | * https://www.openssl.org/source/license.html |
8 | | */ |
9 | | |
10 | | #include <stdio.h> |
11 | | #include "internal/cryptlib.h" |
12 | | #include <openssl/buffer.h> |
13 | | #include <openssl/bn.h> |
14 | | #include <openssl/objects.h> |
15 | | #include <openssl/x509.h> |
16 | | #include <openssl/x509v3.h> |
17 | | #include "crypto/asn1.h" |
18 | | #include "crypto/x509.h" |
19 | | |
20 | | #ifndef OPENSSL_NO_STDIO |
21 | | int X509_print_fp(FILE *fp, X509 *x) |
22 | 0 | { |
23 | 0 | return X509_print_ex_fp(fp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT); |
24 | 0 | } |
25 | | |
26 | | int X509_print_ex_fp(FILE *fp, X509 *x, unsigned long nmflag, |
27 | | unsigned long cflag) |
28 | 0 | { |
29 | 0 | BIO *b; |
30 | 0 | int ret; |
31 | |
|
32 | 0 | if ((b = BIO_new(BIO_s_file())) == NULL) { |
33 | 0 | ERR_raise(ERR_LIB_X509, ERR_R_BUF_LIB); |
34 | 0 | return 0; |
35 | 0 | } |
36 | 0 | BIO_set_fp(b, fp, BIO_NOCLOSE); |
37 | 0 | ret = X509_print_ex(b, x, nmflag, cflag); |
38 | 0 | BIO_free(b); |
39 | 0 | return ret; |
40 | 0 | } |
41 | | #endif |
42 | | |
43 | | int X509_print(BIO *bp, X509 *x) |
44 | 29.3k | { |
45 | 29.3k | return X509_print_ex(bp, x, XN_FLAG_COMPAT, X509_FLAG_COMPAT); |
46 | 29.3k | } |
47 | | |
48 | | int X509_print_ex(BIO *bp, X509 *x, unsigned long nmflags, |
49 | | unsigned long cflag) |
50 | 27.5k | { |
51 | 27.5k | long l; |
52 | 27.5k | int ret = 0, i; |
53 | 27.5k | char *m = NULL, mlch = ' '; |
54 | 27.5k | int nmindent = 0, printok = 0; |
55 | 27.5k | EVP_PKEY *pkey = NULL; |
56 | 27.5k | const char *neg; |
57 | | |
58 | 27.5k | if ((nmflags & XN_FLAG_SEP_MASK) == XN_FLAG_SEP_MULTILINE) { |
59 | 0 | mlch = '\n'; |
60 | 0 | nmindent = 12; |
61 | 0 | } |
62 | | |
63 | 27.5k | if (nmflags == XN_FLAG_COMPAT) |
64 | 21.9k | printok = 1; |
65 | | |
66 | 27.5k | if (!(cflag & X509_FLAG_NO_HEADER)) { |
67 | 21.9k | if (BIO_write(bp, "Certificate:\n", 13) <= 0) |
68 | 0 | goto err; |
69 | 21.9k | if (BIO_write(bp, " Data:\n", 10) <= 0) |
70 | 0 | goto err; |
71 | 21.9k | } |
72 | 27.5k | if (!(cflag & X509_FLAG_NO_VERSION)) { |
73 | 21.9k | l = X509_get_version(x); |
74 | 21.9k | if (l >= X509_VERSION_1 && l <= X509_VERSION_3) { |
75 | 20.7k | if (BIO_printf(bp, "%8sVersion: %ld (0x%lx)\n", "", l + 1, (unsigned long)l) <= 0) |
76 | 0 | goto err; |
77 | 20.7k | } else { |
78 | 1.21k | if (BIO_printf(bp, "%8sVersion: Unknown (%ld)\n", "", l) <= 0) |
79 | 0 | goto err; |
80 | 1.21k | } |
81 | 21.9k | } |
82 | 27.5k | if (!(cflag & X509_FLAG_NO_SERIAL)) { |
83 | 23.3k | const ASN1_INTEGER *bs = X509_get0_serialNumber(x); |
84 | | |
85 | 23.3k | if (BIO_write(bp, " Serial Number:", 22) <= 0) |
86 | 0 | goto err; |
87 | | |
88 | 23.3k | if (bs->length <= (int)sizeof(long)) { |
89 | 22.6k | ERR_set_mark(); |
90 | 22.6k | l = ASN1_INTEGER_get(bs); |
91 | 22.6k | ERR_pop_to_mark(); |
92 | 22.6k | } else { |
93 | 713 | l = -1; |
94 | 713 | } |
95 | 23.3k | if (l != -1) { |
96 | 20.4k | unsigned long ul; |
97 | 20.4k | if (bs->type == V_ASN1_NEG_INTEGER) { |
98 | 7.21k | ul = 0 - (unsigned long)l; |
99 | 7.21k | neg = "-"; |
100 | 13.2k | } else { |
101 | 13.2k | ul = l; |
102 | 13.2k | neg = ""; |
103 | 13.2k | } |
104 | 20.4k | if (BIO_printf(bp, " %s%lu (%s0x%lx)\n", neg, ul, neg, ul) <= 0) |
105 | 0 | goto err; |
106 | 20.4k | } else { |
107 | 2.92k | neg = (bs->type == V_ASN1_NEG_INTEGER) ? " (Negative)" : ""; |
108 | 2.92k | if (BIO_printf(bp, "\n%12s%s", "", neg) <= 0) |
109 | 0 | goto err; |
110 | | |
111 | 79.2k | for (i = 0; i < bs->length; i++) { |
112 | 76.2k | if (BIO_printf(bp, "%02x%c", bs->data[i], |
113 | 76.2k | ((i + 1 == bs->length) ? '\n' : ':')) <= 0) |
114 | 0 | goto err; |
115 | 76.2k | } |
116 | 2.92k | } |
117 | | |
118 | 23.3k | } |
119 | | |
120 | 27.5k | if (!(cflag & X509_FLAG_NO_SIGNAME)) { |
121 | 21.9k | const X509_ALGOR *tsig_alg = X509_get0_tbs_sigalg(x); |
122 | | |
123 | 21.9k | if (BIO_puts(bp, " ") <= 0) |
124 | 0 | goto err; |
125 | 21.9k | if (X509_signature_print(bp, tsig_alg, NULL) <= 0) |
126 | 0 | goto err; |
127 | 21.9k | } |
128 | | |
129 | 27.5k | if (!(cflag & X509_FLAG_NO_ISSUER)) { |
130 | 23.3k | if (BIO_printf(bp, " Issuer:%c", mlch) <= 0) |
131 | 0 | goto err; |
132 | 23.3k | if (X509_NAME_print_ex(bp, X509_get_issuer_name(x), nmindent, nmflags) |
133 | 23.3k | < printok) |
134 | 71 | goto err; |
135 | 23.2k | if (BIO_write(bp, "\n", 1) <= 0) |
136 | 0 | goto err; |
137 | 23.2k | } |
138 | 27.5k | if (!(cflag & X509_FLAG_NO_VALIDITY)) { |
139 | 23.2k | if (BIO_write(bp, " Validity\n", 17) <= 0) |
140 | 0 | goto err; |
141 | 23.2k | if (BIO_write(bp, " Not Before: ", 24) <= 0) |
142 | 0 | goto err; |
143 | 23.2k | if (ossl_asn1_time_print_ex(bp, X509_get0_notBefore(x), ASN1_DTFLGS_RFC822) == 0) |
144 | 0 | goto err; |
145 | 23.2k | if (BIO_write(bp, "\n Not After : ", 25) <= 0) |
146 | 0 | goto err; |
147 | 23.2k | if (ossl_asn1_time_print_ex(bp, X509_get0_notAfter(x), ASN1_DTFLGS_RFC822) == 0) |
148 | 0 | goto err; |
149 | 23.2k | if (BIO_write(bp, "\n", 1) <= 0) |
150 | 0 | goto err; |
151 | 23.2k | } |
152 | 27.5k | if (!(cflag & X509_FLAG_NO_SUBJECT)) { |
153 | 23.2k | if (BIO_printf(bp, " Subject:%c", mlch) <= 0) |
154 | 0 | goto err; |
155 | 23.2k | if (X509_NAME_print_ex |
156 | 23.2k | (bp, X509_get_subject_name(x), nmindent, nmflags) < printok) |
157 | 7 | goto err; |
158 | 23.2k | if (BIO_write(bp, "\n", 1) <= 0) |
159 | 0 | goto err; |
160 | 23.2k | } |
161 | 27.5k | if (!(cflag & X509_FLAG_NO_PUBKEY)) { |
162 | 21.8k | X509_PUBKEY *xpkey = X509_get_X509_PUBKEY(x); |
163 | 21.8k | ASN1_OBJECT *xpoid; |
164 | 21.8k | X509_PUBKEY_get0_param(&xpoid, NULL, NULL, NULL, xpkey); |
165 | 21.8k | if (BIO_write(bp, " Subject Public Key Info:\n", 33) <= 0) |
166 | 0 | goto err; |
167 | 21.8k | if (BIO_printf(bp, "%12sPublic Key Algorithm: ", "") <= 0) |
168 | 0 | goto err; |
169 | 21.8k | if (i2a_ASN1_OBJECT(bp, xpoid) <= 0) |
170 | 0 | goto err; |
171 | 21.8k | if (BIO_puts(bp, "\n") <= 0) |
172 | 0 | goto err; |
173 | | |
174 | 21.8k | pkey = X509_get0_pubkey(x); |
175 | 21.8k | if (pkey == NULL) { |
176 | 16.7k | BIO_printf(bp, "%12sUnable to load Public Key\n", ""); |
177 | 16.7k | ERR_print_errors(bp); |
178 | 16.7k | } else { |
179 | 5.13k | EVP_PKEY_print_public(bp, pkey, 16, NULL); |
180 | 5.13k | } |
181 | 21.8k | } |
182 | | |
183 | 27.5k | if (!(cflag & X509_FLAG_NO_IDS)) { |
184 | 21.8k | const ASN1_BIT_STRING *iuid, *suid; |
185 | 21.8k | X509_get0_uids(x, &iuid, &suid); |
186 | 21.8k | if (iuid != NULL) { |
187 | 26 | if (BIO_printf(bp, "%8sIssuer Unique ID: ", "") <= 0) |
188 | 0 | goto err; |
189 | 26 | if (!X509_signature_dump(bp, iuid, 12)) |
190 | 0 | goto err; |
191 | 26 | } |
192 | 21.8k | if (suid != NULL) { |
193 | 43 | if (BIO_printf(bp, "%8sSubject Unique ID: ", "") <= 0) |
194 | 0 | goto err; |
195 | 43 | if (!X509_signature_dump(bp, suid, 12)) |
196 | 0 | goto err; |
197 | 43 | } |
198 | 21.8k | } |
199 | | |
200 | 27.5k | if (!(cflag & X509_FLAG_NO_EXTENSIONS) |
201 | 27.5k | && !X509V3_extensions_print(bp, "X509v3 extensions", |
202 | 23.2k | X509_get0_extensions(x), cflag, 8)) |
203 | 0 | goto err; |
204 | | |
205 | 27.5k | if (!(cflag & X509_FLAG_NO_SIGDUMP)) { |
206 | 21.8k | const X509_ALGOR *sig_alg; |
207 | 21.8k | const ASN1_BIT_STRING *sig; |
208 | 21.8k | X509_get0_signature(&sig, &sig_alg, x); |
209 | 21.8k | if (X509_signature_print(bp, sig_alg, sig) <= 0) |
210 | 0 | goto err; |
211 | 21.8k | } |
212 | 27.5k | if (!(cflag & X509_FLAG_NO_AUX)) { |
213 | 21.8k | if (!X509_aux_print(bp, x, 0)) |
214 | 0 | goto err; |
215 | 21.8k | } |
216 | 27.5k | ret = 1; |
217 | 27.5k | err: |
218 | 27.5k | OPENSSL_free(m); |
219 | 27.5k | return ret; |
220 | 27.5k | } |
221 | | |
222 | | int X509_ocspid_print(BIO *bp, X509 *x) |
223 | 0 | { |
224 | 0 | unsigned char *der = NULL; |
225 | 0 | unsigned char *dertmp; |
226 | 0 | int derlen; |
227 | 0 | int i; |
228 | 0 | unsigned char SHA1md[SHA_DIGEST_LENGTH]; |
229 | 0 | ASN1_BIT_STRING *keybstr; |
230 | 0 | const X509_NAME *subj; |
231 | 0 | EVP_MD *md = NULL; |
232 | |
|
233 | 0 | if (x == NULL || bp == NULL) |
234 | 0 | return 0; |
235 | | /* |
236 | | * display the hash of the subject as it would appear in OCSP requests |
237 | | */ |
238 | 0 | if (BIO_printf(bp, " Subject OCSP hash: ") <= 0) |
239 | 0 | goto err; |
240 | 0 | subj = X509_get_subject_name(x); |
241 | 0 | derlen = i2d_X509_NAME(subj, NULL); |
242 | 0 | if (derlen <= 0) |
243 | 0 | goto err; |
244 | 0 | if ((der = dertmp = OPENSSL_malloc(derlen)) == NULL) |
245 | 0 | goto err; |
246 | 0 | i2d_X509_NAME(subj, &dertmp); |
247 | |
|
248 | 0 | md = EVP_MD_fetch(x->libctx, SN_sha1, x->propq); |
249 | 0 | if (md == NULL) |
250 | 0 | goto err; |
251 | 0 | if (!EVP_Digest(der, derlen, SHA1md, NULL, md, NULL)) |
252 | 0 | goto err; |
253 | 0 | for (i = 0; i < SHA_DIGEST_LENGTH; i++) { |
254 | 0 | if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0) |
255 | 0 | goto err; |
256 | 0 | } |
257 | 0 | OPENSSL_free(der); |
258 | 0 | der = NULL; |
259 | | |
260 | | /* |
261 | | * display the hash of the public key as it would appear in OCSP requests |
262 | | */ |
263 | 0 | if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0) |
264 | 0 | goto err; |
265 | | |
266 | 0 | keybstr = X509_get0_pubkey_bitstr(x); |
267 | |
|
268 | 0 | if (keybstr == NULL) |
269 | 0 | goto err; |
270 | | |
271 | 0 | if (!EVP_Digest(ASN1_STRING_get0_data(keybstr), |
272 | 0 | ASN1_STRING_length(keybstr), SHA1md, NULL, md, NULL)) |
273 | 0 | goto err; |
274 | 0 | for (i = 0; i < SHA_DIGEST_LENGTH; i++) { |
275 | 0 | if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0) |
276 | 0 | goto err; |
277 | 0 | } |
278 | 0 | BIO_printf(bp, "\n"); |
279 | 0 | EVP_MD_free(md); |
280 | |
|
281 | 0 | return 1; |
282 | 0 | err: |
283 | 0 | OPENSSL_free(der); |
284 | 0 | EVP_MD_free(md); |
285 | 0 | return 0; |
286 | 0 | } |
287 | | |
288 | | int X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent) |
289 | 60.5k | { |
290 | 60.5k | const unsigned char *s; |
291 | 60.5k | int i, n; |
292 | | |
293 | 60.5k | n = sig->length; |
294 | 60.5k | s = sig->data; |
295 | 81.5M | for (i = 0; i < n; i++) { |
296 | 81.5M | if ((i % 18) == 0) { |
297 | 4.53M | if (i > 0 && BIO_write(bp, "\n", 1) <= 0) |
298 | 0 | return 0; |
299 | 4.53M | if (BIO_indent(bp, indent, indent) <= 0) |
300 | 0 | return 0; |
301 | 4.53M | } |
302 | 81.5M | if (BIO_printf(bp, "%02x%s", s[i], ((i + 1) == n) ? "" : ":") <= 0) |
303 | 0 | return 0; |
304 | 81.5M | } |
305 | 60.5k | if (BIO_write(bp, "\n", 1) != 1) |
306 | 0 | return 0; |
307 | | |
308 | 60.5k | return 1; |
309 | 60.5k | } |
310 | | |
311 | | int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg, |
312 | | const ASN1_STRING *sig) |
313 | 119k | { |
314 | 119k | int sig_nid; |
315 | 119k | int indent = 4; |
316 | 119k | if (BIO_printf(bp, "%*sSignature Algorithm: ", indent, "") <= 0) |
317 | 0 | return 0; |
318 | 119k | if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) |
319 | 0 | return 0; |
320 | | |
321 | 119k | if (sig && BIO_printf(bp, "\n%*sSignature Value:", indent, "") <= 0) |
322 | 0 | return 0; |
323 | 119k | sig_nid = OBJ_obj2nid(sigalg->algorithm); |
324 | 119k | if (sig_nid != NID_undef) { |
325 | 31.6k | int pkey_nid, dig_nid; |
326 | 31.6k | const EVP_PKEY_ASN1_METHOD *ameth; |
327 | 31.6k | if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) { |
328 | 13.5k | ameth = EVP_PKEY_asn1_find(NULL, pkey_nid); |
329 | 13.5k | if (ameth && ameth->sig_print) |
330 | 12.8k | return ameth->sig_print(bp, sigalg, sig, indent + 4, 0); |
331 | 13.5k | } |
332 | 31.6k | } |
333 | 106k | if (BIO_write(bp, "\n", 1) != 1) |
334 | 0 | return 0; |
335 | 106k | if (sig) |
336 | 54.2k | return X509_signature_dump(bp, sig, indent + 4); |
337 | 51.9k | return 1; |
338 | 106k | } |
339 | | |
340 | | int X509_aux_print(BIO *out, X509 *x, int indent) |
341 | 29.2k | { |
342 | 29.2k | char oidstr[80], first; |
343 | 29.2k | STACK_OF(ASN1_OBJECT) *trust, *reject; |
344 | 29.2k | const unsigned char *alias, *keyid; |
345 | 29.2k | int keyidlen; |
346 | 29.2k | int i; |
347 | 29.2k | if (X509_trusted(x) == 0) |
348 | 29.2k | return 1; |
349 | 0 | trust = X509_get0_trust_objects(x); |
350 | 0 | reject = X509_get0_reject_objects(x); |
351 | 0 | if (trust) { |
352 | 0 | first = 1; |
353 | 0 | BIO_printf(out, "%*sTrusted Uses:\n%*s", indent, "", indent + 2, ""); |
354 | 0 | for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) { |
355 | 0 | if (!first) |
356 | 0 | BIO_puts(out, ", "); |
357 | 0 | else |
358 | 0 | first = 0; |
359 | 0 | OBJ_obj2txt(oidstr, sizeof(oidstr), |
360 | 0 | sk_ASN1_OBJECT_value(trust, i), 0); |
361 | 0 | BIO_puts(out, oidstr); |
362 | 0 | } |
363 | 0 | BIO_puts(out, "\n"); |
364 | 0 | } else |
365 | 0 | BIO_printf(out, "%*sNo Trusted Uses.\n", indent, ""); |
366 | 0 | if (reject) { |
367 | 0 | first = 1; |
368 | 0 | BIO_printf(out, "%*sRejected Uses:\n%*s", indent, "", indent + 2, ""); |
369 | 0 | for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) { |
370 | 0 | if (!first) |
371 | 0 | BIO_puts(out, ", "); |
372 | 0 | else |
373 | 0 | first = 0; |
374 | 0 | OBJ_obj2txt(oidstr, sizeof(oidstr), |
375 | 0 | sk_ASN1_OBJECT_value(reject, i), 0); |
376 | 0 | BIO_puts(out, oidstr); |
377 | 0 | } |
378 | 0 | BIO_puts(out, "\n"); |
379 | 0 | } else |
380 | 0 | BIO_printf(out, "%*sNo Rejected Uses.\n", indent, ""); |
381 | 0 | alias = X509_alias_get0(x, &i); |
382 | 0 | if (alias) |
383 | 0 | BIO_printf(out, "%*sAlias: %.*s\n", indent, "", i, alias); |
384 | 0 | keyid = X509_keyid_get0(x, &keyidlen); |
385 | 0 | if (keyid) { |
386 | 0 | BIO_printf(out, "%*sKey Id: ", indent, ""); |
387 | 0 | for (i = 0; i < keyidlen; i++) |
388 | 0 | BIO_printf(out, "%s%02X", i ? ":" : "", keyid[i]); |
389 | 0 | BIO_write(out, "\n", 1); |
390 | 0 | } |
391 | 0 | return 1; |
392 | 29.2k | } |
393 | | |
394 | | /* |
395 | | * Helper functions for improving certificate verification error diagnostics |
396 | | */ |
397 | | |
398 | | int ossl_x509_print_ex_brief(BIO *bio, X509 *cert, unsigned long neg_cflags) |
399 | 2.17k | { |
400 | 2.17k | unsigned long flags = ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE | |
401 | 2.17k | XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN; |
402 | | |
403 | 2.17k | if (cert == NULL) |
404 | 0 | return BIO_printf(bio, " (no certificate)\n") > 0; |
405 | 2.17k | if (BIO_printf(bio, " certificate\n") <= 0 |
406 | 2.17k | || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_SUBJECT)) |
407 | 0 | return 0; |
408 | 2.17k | if (X509_check_issued((X509 *)cert, cert) == X509_V_OK) { |
409 | 91 | if (BIO_printf(bio, " self-issued\n") <= 0) |
410 | 0 | return 0; |
411 | 2.08k | } else { |
412 | 2.08k | if (BIO_printf(bio, " ") <= 0 |
413 | 2.08k | || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_ISSUER)) |
414 | 0 | return 0; |
415 | 2.08k | } |
416 | 2.17k | if (!X509_print_ex(bio, cert, flags, |
417 | 2.17k | ~(X509_FLAG_NO_SERIAL | X509_FLAG_NO_VALIDITY))) |
418 | 0 | return 0; |
419 | 2.17k | if (X509_cmp_current_time(X509_get0_notBefore(cert)) > 0) |
420 | 73 | if (BIO_printf(bio, " not yet valid\n") <= 0) |
421 | 0 | return 0; |
422 | 2.17k | if (X509_cmp_current_time(X509_get0_notAfter(cert)) < 0) |
423 | 159 | if (BIO_printf(bio, " no more valid\n") <= 0) |
424 | 0 | return 0; |
425 | 2.17k | return X509_print_ex(bio, cert, flags, |
426 | 2.17k | ~neg_cflags & ~X509_FLAG_EXTENSIONS_ONLY_KID); |
427 | 2.17k | } |
428 | | |
429 | | static int print_certs(BIO *bio, const STACK_OF(X509) *certs) |
430 | 0 | { |
431 | 0 | int i; |
432 | |
|
433 | 0 | if (certs == NULL || sk_X509_num(certs) <= 0) |
434 | 0 | return BIO_printf(bio, " (no certificates)\n") >= 0; |
435 | | |
436 | 0 | for (i = 0; i < sk_X509_num(certs); i++) { |
437 | 0 | X509 *cert = sk_X509_value(certs, i); |
438 | |
|
439 | 0 | if (cert != NULL) { |
440 | 0 | if (!ossl_x509_print_ex_brief(bio, cert, 0)) |
441 | 0 | return 0; |
442 | 0 | if (!X509V3_extensions_print(bio, NULL, |
443 | 0 | X509_get0_extensions(cert), |
444 | 0 | X509_FLAG_EXTENSIONS_ONLY_KID, 8)) |
445 | 0 | return 0; |
446 | 0 | } |
447 | 0 | } |
448 | 0 | return 1; |
449 | 0 | } |
450 | | |
451 | | static int print_store_certs(BIO *bio, X509_STORE *store) |
452 | 0 | { |
453 | 0 | if (store != NULL) { |
454 | 0 | STACK_OF(X509) *certs = X509_STORE_get1_all_certs(store); |
455 | 0 | int ret = print_certs(bio, certs); |
456 | |
|
457 | 0 | sk_X509_pop_free(certs, X509_free); |
458 | 0 | return ret; |
459 | 0 | } else { |
460 | 0 | return BIO_printf(bio, " (no trusted store)\n") >= 0; |
461 | 0 | } |
462 | 0 | } |
463 | | |
464 | | /* Extend the error queue with details on a failed cert verification */ |
465 | | int X509_STORE_CTX_print_verify_cb(int ok, X509_STORE_CTX *ctx) |
466 | 0 | { |
467 | 0 | if (ok == 0 && ctx != NULL) { |
468 | 0 | int cert_error = X509_STORE_CTX_get_error(ctx); |
469 | 0 | BIO *bio = BIO_new(BIO_s_mem()); /* may be NULL */ |
470 | |
|
471 | 0 | BIO_printf(bio, "%s at depth = %d error = %d (%s)\n", |
472 | 0 | X509_STORE_CTX_get0_parent_ctx(ctx) != NULL |
473 | 0 | ? "CRL path validation" |
474 | 0 | : "Certificate verification", |
475 | 0 | X509_STORE_CTX_get_error_depth(ctx), |
476 | 0 | cert_error, X509_verify_cert_error_string(cert_error)); |
477 | 0 | { |
478 | 0 | X509_STORE *ts = X509_STORE_CTX_get0_store(ctx); |
479 | 0 | X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts); |
480 | 0 | char *str; |
481 | 0 | int idx = 0; |
482 | |
|
483 | 0 | switch (cert_error) { |
484 | 0 | case X509_V_ERR_HOSTNAME_MISMATCH: |
485 | 0 | BIO_printf(bio, "Expected hostname(s) = "); |
486 | 0 | while ((str = X509_VERIFY_PARAM_get0_host(vpm, idx++)) != NULL) |
487 | 0 | BIO_printf(bio, "%s%s", idx == 1 ? "" : ", ", str); |
488 | 0 | BIO_printf(bio, "\n"); |
489 | 0 | break; |
490 | 0 | case X509_V_ERR_EMAIL_MISMATCH: |
491 | 0 | str = X509_VERIFY_PARAM_get0_email(vpm); |
492 | 0 | if (str != NULL) |
493 | 0 | BIO_printf(bio, "Expected email address = %s\n", str); |
494 | 0 | break; |
495 | 0 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
496 | 0 | str = X509_VERIFY_PARAM_get1_ip_asc(vpm); |
497 | 0 | if (str != NULL) |
498 | 0 | BIO_printf(bio, "Expected IP address = %s\n", str); |
499 | 0 | OPENSSL_free(str); |
500 | 0 | break; |
501 | 0 | default: |
502 | 0 | break; |
503 | 0 | } |
504 | 0 | } |
505 | | |
506 | 0 | BIO_printf(bio, "Failure for:\n"); |
507 | 0 | ossl_x509_print_ex_brief(bio, X509_STORE_CTX_get_current_cert(ctx), |
508 | 0 | X509_FLAG_NO_EXTENSIONS); |
509 | 0 | if (cert_error == X509_V_ERR_CERT_UNTRUSTED |
510 | 0 | || cert_error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT |
511 | 0 | || cert_error == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN |
512 | 0 | || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT |
513 | 0 | || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY |
514 | 0 | || cert_error == X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER |
515 | 0 | || cert_error == X509_V_ERR_STORE_LOOKUP) { |
516 | 0 | BIO_printf(bio, "Non-trusted certs:\n"); |
517 | 0 | print_certs(bio, X509_STORE_CTX_get0_untrusted(ctx)); |
518 | 0 | BIO_printf(bio, "Certs in trust store:\n"); |
519 | 0 | print_store_certs(bio, X509_STORE_CTX_get0_store(ctx)); |
520 | 0 | } |
521 | 0 | ERR_raise(ERR_LIB_X509, X509_R_CERTIFICATE_VERIFICATION_FAILED); |
522 | 0 | ERR_add_error_mem_bio("\n", bio); |
523 | 0 | BIO_free(bio); |
524 | 0 | } |
525 | | |
526 | 0 | return ok; |
527 | 0 | } |