/src/openssl31/crypto/x509/t_x509.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 1995-2022 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 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 | return ret; |
219 | 27.5k | } |
220 | | |
221 | | int X509_ocspid_print(BIO *bp, X509 *x) |
222 | 0 | { |
223 | 0 | unsigned char *der = NULL; |
224 | 0 | unsigned char *dertmp; |
225 | 0 | int derlen; |
226 | 0 | int i; |
227 | 0 | unsigned char SHA1md[SHA_DIGEST_LENGTH]; |
228 | 0 | ASN1_BIT_STRING *keybstr; |
229 | 0 | const X509_NAME *subj; |
230 | 0 | EVP_MD *md = NULL; |
231 | |
|
232 | 0 | if (x == NULL || bp == NULL) |
233 | 0 | return 0; |
234 | | /* |
235 | | * display the hash of the subject as it would appear in OCSP requests |
236 | | */ |
237 | 0 | if (BIO_printf(bp, " Subject OCSP hash: ") <= 0) |
238 | 0 | goto err; |
239 | 0 | subj = X509_get_subject_name(x); |
240 | 0 | derlen = i2d_X509_NAME(subj, NULL); |
241 | 0 | if (derlen <= 0) |
242 | 0 | goto err; |
243 | 0 | if ((der = dertmp = OPENSSL_malloc(derlen)) == NULL) |
244 | 0 | goto err; |
245 | 0 | i2d_X509_NAME(subj, &dertmp); |
246 | |
|
247 | 0 | md = EVP_MD_fetch(x->libctx, SN_sha1, x->propq); |
248 | 0 | if (md == NULL) |
249 | 0 | goto err; |
250 | 0 | if (!EVP_Digest(der, derlen, SHA1md, NULL, md, NULL)) |
251 | 0 | goto err; |
252 | 0 | for (i = 0; i < SHA_DIGEST_LENGTH; i++) { |
253 | 0 | if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0) |
254 | 0 | goto err; |
255 | 0 | } |
256 | 0 | OPENSSL_free(der); |
257 | 0 | der = NULL; |
258 | | |
259 | | /* |
260 | | * display the hash of the public key as it would appear in OCSP requests |
261 | | */ |
262 | 0 | if (BIO_printf(bp, "\n Public key OCSP hash: ") <= 0) |
263 | 0 | goto err; |
264 | | |
265 | 0 | keybstr = X509_get0_pubkey_bitstr(x); |
266 | |
|
267 | 0 | if (keybstr == NULL) |
268 | 0 | goto err; |
269 | | |
270 | 0 | if (!EVP_Digest(ASN1_STRING_get0_data(keybstr), |
271 | 0 | ASN1_STRING_length(keybstr), SHA1md, NULL, md, NULL)) |
272 | 0 | goto err; |
273 | 0 | for (i = 0; i < SHA_DIGEST_LENGTH; i++) { |
274 | 0 | if (BIO_printf(bp, "%02X", SHA1md[i]) <= 0) |
275 | 0 | goto err; |
276 | 0 | } |
277 | 0 | BIO_printf(bp, "\n"); |
278 | 0 | EVP_MD_free(md); |
279 | |
|
280 | 0 | return 1; |
281 | 0 | err: |
282 | 0 | OPENSSL_free(der); |
283 | 0 | EVP_MD_free(md); |
284 | 0 | return 0; |
285 | 0 | } |
286 | | |
287 | | int X509_signature_dump(BIO *bp, const ASN1_STRING *sig, int indent) |
288 | 60.5k | { |
289 | 60.5k | const unsigned char *s; |
290 | 60.5k | int i, n; |
291 | | |
292 | 60.5k | n = sig->length; |
293 | 60.5k | s = sig->data; |
294 | 81.5M | for (i = 0; i < n; i++) { |
295 | 81.5M | if ((i % 18) == 0) { |
296 | 4.53M | if (i > 0 && BIO_write(bp, "\n", 1) <= 0) |
297 | 0 | return 0; |
298 | 4.53M | if (BIO_indent(bp, indent, indent) <= 0) |
299 | 0 | return 0; |
300 | 4.53M | } |
301 | 81.5M | if (BIO_printf(bp, "%02x%s", s[i], ((i + 1) == n) ? "" : ":") <= 0) |
302 | 0 | return 0; |
303 | 81.5M | } |
304 | 60.5k | if (BIO_write(bp, "\n", 1) != 1) |
305 | 0 | return 0; |
306 | | |
307 | 60.5k | return 1; |
308 | 60.5k | } |
309 | | |
310 | | int X509_signature_print(BIO *bp, const X509_ALGOR *sigalg, |
311 | | const ASN1_STRING *sig) |
312 | 119k | { |
313 | 119k | int sig_nid; |
314 | 119k | int indent = 4; |
315 | 119k | if (BIO_printf(bp, "%*sSignature Algorithm: ", indent, "") <= 0) |
316 | 0 | return 0; |
317 | 119k | if (i2a_ASN1_OBJECT(bp, sigalg->algorithm) <= 0) |
318 | 0 | return 0; |
319 | | |
320 | 119k | if (sig && BIO_printf(bp, "\n%*sSignature Value:", indent, "") <= 0) |
321 | 0 | return 0; |
322 | 119k | sig_nid = OBJ_obj2nid(sigalg->algorithm); |
323 | 119k | if (sig_nid != NID_undef) { |
324 | 31.6k | int pkey_nid, dig_nid; |
325 | 31.6k | const EVP_PKEY_ASN1_METHOD *ameth; |
326 | 31.6k | if (OBJ_find_sigid_algs(sig_nid, &dig_nid, &pkey_nid)) { |
327 | 13.5k | ameth = EVP_PKEY_asn1_find(NULL, pkey_nid); |
328 | 13.5k | if (ameth && ameth->sig_print) |
329 | 12.8k | return ameth->sig_print(bp, sigalg, sig, indent + 4, 0); |
330 | 13.5k | } |
331 | 31.6k | } |
332 | 106k | if (BIO_write(bp, "\n", 1) != 1) |
333 | 0 | return 0; |
334 | 106k | if (sig) |
335 | 54.2k | return X509_signature_dump(bp, sig, indent + 4); |
336 | 51.9k | return 1; |
337 | 106k | } |
338 | | |
339 | | int X509_aux_print(BIO *out, X509 *x, int indent) |
340 | 29.2k | { |
341 | 29.2k | char oidstr[80], first; |
342 | 29.2k | STACK_OF(ASN1_OBJECT) *trust, *reject; |
343 | 29.2k | const unsigned char *alias, *keyid; |
344 | 29.2k | int keyidlen; |
345 | 29.2k | int i; |
346 | 29.2k | if (X509_trusted(x) == 0) |
347 | 29.2k | return 1; |
348 | 0 | trust = X509_get0_trust_objects(x); |
349 | 0 | reject = X509_get0_reject_objects(x); |
350 | 0 | if (trust) { |
351 | 0 | first = 1; |
352 | 0 | BIO_printf(out, "%*sTrusted Uses:\n%*s", indent, "", indent + 2, ""); |
353 | 0 | for (i = 0; i < sk_ASN1_OBJECT_num(trust); i++) { |
354 | 0 | if (!first) |
355 | 0 | BIO_puts(out, ", "); |
356 | 0 | else |
357 | 0 | first = 0; |
358 | 0 | OBJ_obj2txt(oidstr, sizeof(oidstr), |
359 | 0 | sk_ASN1_OBJECT_value(trust, i), 0); |
360 | 0 | BIO_puts(out, oidstr); |
361 | 0 | } |
362 | 0 | BIO_puts(out, "\n"); |
363 | 0 | } else |
364 | 0 | BIO_printf(out, "%*sNo Trusted Uses.\n", indent, ""); |
365 | 0 | if (reject) { |
366 | 0 | first = 1; |
367 | 0 | BIO_printf(out, "%*sRejected Uses:\n%*s", indent, "", indent + 2, ""); |
368 | 0 | for (i = 0; i < sk_ASN1_OBJECT_num(reject); i++) { |
369 | 0 | if (!first) |
370 | 0 | BIO_puts(out, ", "); |
371 | 0 | else |
372 | 0 | first = 0; |
373 | 0 | OBJ_obj2txt(oidstr, sizeof(oidstr), |
374 | 0 | sk_ASN1_OBJECT_value(reject, i), 0); |
375 | 0 | BIO_puts(out, oidstr); |
376 | 0 | } |
377 | 0 | BIO_puts(out, "\n"); |
378 | 0 | } else |
379 | 0 | BIO_printf(out, "%*sNo Rejected Uses.\n", indent, ""); |
380 | 0 | alias = X509_alias_get0(x, &i); |
381 | 0 | if (alias) |
382 | 0 | BIO_printf(out, "%*sAlias: %.*s\n", indent, "", i, alias); |
383 | 0 | keyid = X509_keyid_get0(x, &keyidlen); |
384 | 0 | if (keyid) { |
385 | 0 | BIO_printf(out, "%*sKey Id: ", indent, ""); |
386 | 0 | for (i = 0; i < keyidlen; i++) |
387 | 0 | BIO_printf(out, "%s%02X", i ? ":" : "", keyid[i]); |
388 | 0 | BIO_write(out, "\n", 1); |
389 | 0 | } |
390 | 0 | return 1; |
391 | 29.2k | } |
392 | | |
393 | | /* |
394 | | * Helper functions for improving certificate verification error diagnostics |
395 | | */ |
396 | | |
397 | | int ossl_x509_print_ex_brief(BIO *bio, X509 *cert, unsigned long neg_cflags) |
398 | 2.17k | { |
399 | 2.17k | unsigned long flags = ASN1_STRFLGS_RFC2253 | ASN1_STRFLGS_ESC_QUOTE | |
400 | 2.17k | XN_FLAG_SEP_CPLUS_SPC | XN_FLAG_FN_SN; |
401 | | |
402 | 2.17k | if (cert == NULL) |
403 | 0 | return BIO_printf(bio, " (no certificate)\n") > 0; |
404 | 2.17k | if (BIO_printf(bio, " certificate\n") <= 0 |
405 | 2.17k | || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_SUBJECT)) |
406 | 0 | return 0; |
407 | 2.17k | if (X509_check_issued((X509 *)cert, cert) == X509_V_OK) { |
408 | 91 | if (BIO_printf(bio, " self-issued\n") <= 0) |
409 | 0 | return 0; |
410 | 2.08k | } else { |
411 | 2.08k | if (BIO_printf(bio, " ") <= 0 |
412 | 2.08k | || !X509_print_ex(bio, cert, flags, ~X509_FLAG_NO_ISSUER)) |
413 | 0 | return 0; |
414 | 2.08k | } |
415 | 2.17k | if (!X509_print_ex(bio, cert, flags, |
416 | 2.17k | ~(X509_FLAG_NO_SERIAL | X509_FLAG_NO_VALIDITY))) |
417 | 0 | return 0; |
418 | 2.17k | if (X509_cmp_current_time(X509_get0_notBefore(cert)) > 0) |
419 | 73 | if (BIO_printf(bio, " not yet valid\n") <= 0) |
420 | 0 | return 0; |
421 | 2.17k | if (X509_cmp_current_time(X509_get0_notAfter(cert)) < 0) |
422 | 159 | if (BIO_printf(bio, " no more valid\n") <= 0) |
423 | 0 | return 0; |
424 | 2.17k | return X509_print_ex(bio, cert, flags, |
425 | 2.17k | ~neg_cflags & ~X509_FLAG_EXTENSIONS_ONLY_KID); |
426 | 2.17k | } |
427 | | |
428 | | static int print_certs(BIO *bio, const STACK_OF(X509) *certs) |
429 | 0 | { |
430 | 0 | int i; |
431 | |
|
432 | 0 | if (certs == NULL || sk_X509_num(certs) <= 0) |
433 | 0 | return BIO_printf(bio, " (no certificates)\n") >= 0; |
434 | | |
435 | 0 | for (i = 0; i < sk_X509_num(certs); i++) { |
436 | 0 | X509 *cert = sk_X509_value(certs, i); |
437 | |
|
438 | 0 | if (cert != NULL) { |
439 | 0 | if (!ossl_x509_print_ex_brief(bio, cert, 0)) |
440 | 0 | return 0; |
441 | 0 | if (!X509V3_extensions_print(bio, NULL, |
442 | 0 | X509_get0_extensions(cert), |
443 | 0 | X509_FLAG_EXTENSIONS_ONLY_KID, 8)) |
444 | 0 | return 0; |
445 | 0 | } |
446 | 0 | } |
447 | 0 | return 1; |
448 | 0 | } |
449 | | |
450 | | static int print_store_certs(BIO *bio, X509_STORE *store) |
451 | 0 | { |
452 | 0 | if (store != NULL) { |
453 | 0 | STACK_OF(X509) *certs = X509_STORE_get1_all_certs(store); |
454 | 0 | int ret = print_certs(bio, certs); |
455 | |
|
456 | 0 | sk_X509_pop_free(certs, X509_free); |
457 | 0 | return ret; |
458 | 0 | } else { |
459 | 0 | return BIO_printf(bio, " (no trusted store)\n") >= 0; |
460 | 0 | } |
461 | 0 | } |
462 | | |
463 | | /* Extend the error queue with details on a failed cert verification */ |
464 | | int X509_STORE_CTX_print_verify_cb(int ok, X509_STORE_CTX *ctx) |
465 | 0 | { |
466 | 0 | if (ok == 0 && ctx != NULL) { |
467 | 0 | int cert_error = X509_STORE_CTX_get_error(ctx); |
468 | 0 | BIO *bio = BIO_new(BIO_s_mem()); /* may be NULL */ |
469 | |
|
470 | 0 | BIO_printf(bio, "%s at depth = %d error = %d (%s)\n", |
471 | 0 | X509_STORE_CTX_get0_parent_ctx(ctx) != NULL |
472 | 0 | ? "CRL path validation" |
473 | 0 | : "Certificate verification", |
474 | 0 | X509_STORE_CTX_get_error_depth(ctx), |
475 | 0 | cert_error, X509_verify_cert_error_string(cert_error)); |
476 | 0 | { |
477 | 0 | X509_STORE *ts = X509_STORE_CTX_get0_store(ctx); |
478 | 0 | X509_VERIFY_PARAM *vpm = X509_STORE_get0_param(ts); |
479 | 0 | char *str; |
480 | 0 | int idx = 0; |
481 | |
|
482 | 0 | switch (cert_error) { |
483 | 0 | case X509_V_ERR_HOSTNAME_MISMATCH: |
484 | 0 | BIO_printf(bio, "Expected hostname(s) = "); |
485 | 0 | while ((str = X509_VERIFY_PARAM_get0_host(vpm, idx++)) != NULL) |
486 | 0 | BIO_printf(bio, "%s%s", idx == 1 ? "" : ", ", str); |
487 | 0 | BIO_printf(bio, "\n"); |
488 | 0 | break; |
489 | 0 | case X509_V_ERR_EMAIL_MISMATCH: |
490 | 0 | str = X509_VERIFY_PARAM_get0_email(vpm); |
491 | 0 | if (str != NULL) |
492 | 0 | BIO_printf(bio, "Expected email address = %s\n", str); |
493 | 0 | break; |
494 | 0 | case X509_V_ERR_IP_ADDRESS_MISMATCH: |
495 | 0 | str = X509_VERIFY_PARAM_get1_ip_asc(vpm); |
496 | 0 | if (str != NULL) |
497 | 0 | BIO_printf(bio, "Expected IP address = %s\n", str); |
498 | 0 | OPENSSL_free(str); |
499 | 0 | break; |
500 | 0 | default: |
501 | 0 | break; |
502 | 0 | } |
503 | 0 | } |
504 | | |
505 | 0 | BIO_printf(bio, "Failure for:\n"); |
506 | 0 | ossl_x509_print_ex_brief(bio, X509_STORE_CTX_get_current_cert(ctx), |
507 | 0 | X509_FLAG_NO_EXTENSIONS); |
508 | 0 | if (cert_error == X509_V_ERR_CERT_UNTRUSTED |
509 | 0 | || cert_error == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT |
510 | 0 | || cert_error == X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN |
511 | 0 | || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT |
512 | 0 | || cert_error == X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY |
513 | 0 | || cert_error == X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER |
514 | 0 | || cert_error == X509_V_ERR_STORE_LOOKUP) { |
515 | 0 | BIO_printf(bio, "Non-trusted certs:\n"); |
516 | 0 | print_certs(bio, X509_STORE_CTX_get0_untrusted(ctx)); |
517 | 0 | BIO_printf(bio, "Certs in trust store:\n"); |
518 | 0 | print_store_certs(bio, X509_STORE_CTX_get0_store(ctx)); |
519 | 0 | } |
520 | 0 | ERR_raise(ERR_LIB_X509, X509_R_CERTIFICATE_VERIFICATION_FAILED); |
521 | 0 | ERR_add_error_mem_bio("\n", bio); |
522 | 0 | BIO_free(bio); |
523 | 0 | } |
524 | | |
525 | 0 | return ok; |
526 | 0 | } |