/src/boringssl/crypto/x509/x509_vfy.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 <ctype.h> |
16 | | #include <limits.h> |
17 | | #include <string.h> |
18 | | #include <time.h> |
19 | | |
20 | | #include <openssl/asn1.h> |
21 | | #include <openssl/err.h> |
22 | | #include <openssl/evp.h> |
23 | | #include <openssl/mem.h> |
24 | | #include <openssl/obj.h> |
25 | | #include <openssl/x509.h> |
26 | | |
27 | | #include "../internal.h" |
28 | | #include "../mem_internal.h" |
29 | | #include "internal.h" |
30 | | |
31 | | |
32 | | using namespace bssl; |
33 | | |
34 | | static ExDataClass g_ex_data_class(/*with_app_data=*/true); |
35 | | |
36 | | // CRL score values |
37 | | |
38 | | // No unhandled critical extensions |
39 | 0 | #define CRL_SCORE_NOCRITICAL 0x100 |
40 | | |
41 | | // certificate is within CRL scope |
42 | 0 | #define CRL_SCORE_SCOPE 0x080 |
43 | | |
44 | | // CRL times valid |
45 | 0 | #define CRL_SCORE_TIME 0x040 |
46 | | |
47 | | // Issuer name matches certificate |
48 | 0 | #define CRL_SCORE_ISSUER_NAME 0x020 |
49 | | |
50 | | // If this score or above CRL is probably valid |
51 | | #define CRL_SCORE_VALID \ |
52 | 0 | (CRL_SCORE_NOCRITICAL | CRL_SCORE_TIME | CRL_SCORE_SCOPE) |
53 | | |
54 | | // CRL issuer is certificate issuer |
55 | 0 | #define CRL_SCORE_ISSUER_CERT 0x018 |
56 | | |
57 | | // CRL issuer is on certificate path |
58 | 0 | #define CRL_SCORE_SAME_PATH 0x008 |
59 | | |
60 | | // CRL issuer matches CRL AKID |
61 | 0 | #define CRL_SCORE_AKID 0x004 |
62 | | |
63 | | static int null_callback(int ok, X509_STORE_CTX *e); |
64 | | static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x); |
65 | | static int check_chain_extensions(X509_STORE_CTX *ctx); |
66 | | static int check_name_constraints(X509_STORE_CTX *ctx); |
67 | | static int check_id(X509_STORE_CTX *ctx); |
68 | | static int check_trust(X509_STORE_CTX *ctx); |
69 | | static int check_revocation(X509_STORE_CTX *ctx); |
70 | | static int check_cert(X509_STORE_CTX *ctx); |
71 | | static int check_policy(X509_STORE_CTX *ctx); |
72 | | |
73 | | static X509 *get_trusted_issuer(X509_STORE_CTX *ctx, X509 *x); |
74 | | static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl, |
75 | | X509 *x); |
76 | | static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x); |
77 | | static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer, |
78 | | int *pcrl_score); |
79 | | static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score); |
80 | | static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl); |
81 | | static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x); |
82 | | |
83 | | static int internal_verify(X509_STORE_CTX *ctx); |
84 | | |
85 | 0 | static int null_callback(int ok, X509_STORE_CTX *e) { return ok; } |
86 | | |
87 | | // cert_self_signed checks if `x` is self-signed. If `x` is valid, it returns |
88 | | // one and sets `*out_is_self_signed` to the result. If `x` is invalid, it |
89 | | // returns zero. |
90 | 0 | static int cert_self_signed(X509 *x, int *out_is_self_signed) { |
91 | 0 | if (!x509v3_cache_extensions(x)) { |
92 | 0 | return 0; |
93 | 0 | } |
94 | 0 | auto *impl = FromOpaque(x); |
95 | 0 | *out_is_self_signed = (impl->ex_flags & EXFLAG_SS) != 0; |
96 | 0 | return 1; |
97 | 0 | } |
98 | | |
99 | 0 | static int call_verify_cb(int ok, X509_STORE_CTX *ctx) { |
100 | 0 | ok = ctx->verify_cb(ok, ctx); |
101 | | // Historically, callbacks returning values like -1 would be treated as a mix |
102 | | // of success or failure. Insert that callers check correctly. |
103 | | // |
104 | | // TODO(davidben): Also use this wrapper to constrain which errors may be |
105 | | // suppressed, and ensure all `verify_cb` calls remember to fill in an error. |
106 | 0 | BSSL_CHECK(ok == 0 || ok == 1); |
107 | 0 | return ok; |
108 | 0 | } |
109 | | |
110 | | // Given a certificate try and find an exact match in the store |
111 | 0 | static X509 *lookup_cert_match(X509_STORE_CTX *ctx, X509 *x) { |
112 | 0 | STACK_OF(X509) *certs; |
113 | 0 | X509 *xtmp = nullptr; |
114 | 0 | size_t i; |
115 | | // Lookup all certs with matching subject name |
116 | 0 | certs = X509_STORE_CTX_get1_certs(ctx, X509_get_subject_name(x)); |
117 | 0 | if (certs == nullptr) { |
118 | 0 | return nullptr; |
119 | 0 | } |
120 | | // Look for exact match |
121 | 0 | for (i = 0; i < sk_X509_num(certs); i++) { |
122 | 0 | xtmp = sk_X509_value(certs, i); |
123 | 0 | if (!X509_cmp(xtmp, x)) { |
124 | 0 | break; |
125 | 0 | } |
126 | 0 | } |
127 | 0 | if (i < sk_X509_num(certs)) { |
128 | 0 | X509_up_ref(xtmp); |
129 | 0 | } else { |
130 | 0 | xtmp = nullptr; |
131 | 0 | } |
132 | 0 | sk_X509_pop_free(certs, X509_free); |
133 | 0 | return xtmp; |
134 | 0 | } |
135 | | |
136 | 0 | int X509_verify_cert(X509_STORE_CTX *ctx) { |
137 | 0 | X509 *chain_ss = nullptr; |
138 | 0 | int bad_chain = 0; |
139 | 0 | X509_VERIFY_PARAM *param = ctx->param; |
140 | 0 | int i, ok = 0; |
141 | 0 | int trust; |
142 | 0 | STACK_OF(X509) *sktmp = nullptr; |
143 | |
|
144 | 0 | { |
145 | 0 | if (ctx->cert == nullptr) { |
146 | 0 | OPENSSL_PUT_ERROR(X509, X509_R_NO_CERT_SET_FOR_US_TO_VERIFY); |
147 | 0 | ctx->error = X509_V_ERR_INVALID_CALL; |
148 | 0 | return 0; |
149 | 0 | } |
150 | | |
151 | 0 | if (ctx->chain != nullptr) { |
152 | | // This X509_STORE_CTX has already been used to verify a cert. We |
153 | | // cannot do another one. |
154 | 0 | OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
155 | 0 | ctx->error = X509_V_ERR_INVALID_CALL; |
156 | 0 | return 0; |
157 | 0 | } |
158 | | |
159 | 0 | if (ctx->param->flags & |
160 | 0 | (X509_V_FLAG_EXTENDED_CRL_SUPPORT | X509_V_FLAG_USE_DELTAS)) { |
161 | | // We do not support indirect or delta CRLs. The flags still exist for |
162 | | // compatibility with bindings libraries, but to ensure we do not |
163 | | // inadvertently skip a CRL check that the caller expects, fail closed. |
164 | 0 | OPENSSL_PUT_ERROR(X509, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); |
165 | 0 | ctx->error = X509_V_ERR_INVALID_CALL; |
166 | 0 | return 0; |
167 | 0 | } |
168 | | |
169 | | // first we make sure the chain we are going to build is present and that |
170 | | // the first entry is in place |
171 | 0 | ctx->chain = sk_X509_new_null(); |
172 | 0 | if (ctx->chain == nullptr || !sk_X509_push(ctx->chain, ctx->cert)) { |
173 | 0 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
174 | 0 | goto end; |
175 | 0 | } |
176 | 0 | X509_up_ref(ctx->cert); |
177 | 0 | ctx->last_untrusted = 1; |
178 | | |
179 | | // We use a temporary STACK so we can chop and hack at it. |
180 | 0 | if (ctx->untrusted != nullptr && |
181 | 0 | (sktmp = sk_X509_dup(ctx->untrusted)) == nullptr) { |
182 | 0 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
183 | 0 | goto end; |
184 | 0 | } |
185 | | |
186 | 0 | int num = (int)sk_X509_num(ctx->chain); |
187 | 0 | X509 *x = sk_X509_value(ctx->chain, num - 1); |
188 | | // `param->depth` does not include the leaf certificate or the trust anchor, |
189 | | // so the maximum size is 2 more. |
190 | 0 | int max_chain = param->depth >= INT_MAX - 2 ? INT_MAX : param->depth + 2; |
191 | |
|
192 | 0 | for (;;) { |
193 | 0 | if (num >= max_chain) { |
194 | | // FIXME: If this happens, we should take note of it and, if |
195 | | // appropriate, use the X509_V_ERR_CERT_CHAIN_TOO_LONG error code later. |
196 | 0 | break; |
197 | 0 | } |
198 | | |
199 | 0 | int is_self_signed; |
200 | 0 | if (!cert_self_signed(x, &is_self_signed)) { |
201 | 0 | ctx->error = X509_V_ERR_INVALID_EXTENSION; |
202 | 0 | goto end; |
203 | 0 | } |
204 | | |
205 | | // If we are self signed, we break |
206 | 0 | if (is_self_signed) { |
207 | 0 | break; |
208 | 0 | } |
209 | | // See if we can find issuer in trusted store first |
210 | 0 | X509 *issuer = get_trusted_issuer(ctx, x); |
211 | 0 | if (issuer != nullptr) { |
212 | | // Free the certificate. It will be picked up again later. |
213 | 0 | X509_free(issuer); |
214 | 0 | break; |
215 | 0 | } |
216 | | |
217 | | // If we were passed a cert chain, use it first |
218 | 0 | if (sktmp != nullptr) { |
219 | 0 | issuer = find_issuer(ctx, sktmp, x); |
220 | 0 | if (issuer != nullptr) { |
221 | 0 | if (!sk_X509_push(ctx->chain, issuer)) { |
222 | 0 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
223 | 0 | goto end; |
224 | 0 | } |
225 | 0 | X509_up_ref(issuer); |
226 | 0 | (void)sk_X509_delete_ptr(sktmp, issuer); |
227 | 0 | ctx->last_untrusted++; |
228 | 0 | x = issuer; |
229 | 0 | num++; |
230 | | // reparse the full chain for the next one |
231 | 0 | continue; |
232 | 0 | } |
233 | 0 | } |
234 | 0 | break; |
235 | 0 | } |
236 | | |
237 | | // At this point, chain should contain a list of untrusted certificates. |
238 | | // We now need to add at least one trusted one, if possible, otherwise we |
239 | | // complain. |
240 | | |
241 | | // Examine last certificate in chain and see if it is self signed. |
242 | 0 | i = (int)sk_X509_num(ctx->chain); |
243 | 0 | x = sk_X509_value(ctx->chain, i - 1); |
244 | |
|
245 | 0 | int is_self_signed; |
246 | 0 | if (!cert_self_signed(x, &is_self_signed)) { |
247 | 0 | ctx->error = X509_V_ERR_INVALID_EXTENSION; |
248 | 0 | goto end; |
249 | 0 | } |
250 | | |
251 | 0 | if (is_self_signed) { |
252 | | // we have a self signed certificate |
253 | 0 | if (sk_X509_num(ctx->chain) == 1) { |
254 | | // We have a single self signed certificate: see if we can |
255 | | // find it in the store. We must have an exact match to avoid |
256 | | // possible impersonation. |
257 | 0 | X509 *issuer = get_trusted_issuer(ctx, x); |
258 | 0 | if (issuer == nullptr || X509_cmp(x, issuer) != 0) { |
259 | 0 | X509_free(issuer); |
260 | 0 | ctx->error = X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT; |
261 | 0 | ctx->current_cert = x; |
262 | 0 | ctx->error_depth = i - 1; |
263 | 0 | bad_chain = 1; |
264 | 0 | if (!call_verify_cb(0, ctx)) { |
265 | 0 | goto end; |
266 | 0 | } |
267 | 0 | } else { |
268 | | // We have a match: replace certificate with store |
269 | | // version so we get any trust settings. |
270 | 0 | X509_free(x); |
271 | 0 | x = issuer; |
272 | 0 | (void)sk_X509_set(ctx->chain, i - 1, x); |
273 | 0 | ctx->last_untrusted = 0; |
274 | 0 | } |
275 | 0 | } else { |
276 | | // extract and save self signed certificate for later use |
277 | 0 | chain_ss = sk_X509_pop(ctx->chain); |
278 | 0 | ctx->last_untrusted--; |
279 | 0 | num--; |
280 | 0 | x = sk_X509_value(ctx->chain, num - 1); |
281 | 0 | } |
282 | 0 | } |
283 | | // We now lookup certs from the certificate store |
284 | 0 | for (;;) { |
285 | 0 | if (num >= max_chain) { |
286 | | // FIXME: If this happens, we should take note of it and, if |
287 | | // appropriate, use the X509_V_ERR_CERT_CHAIN_TOO_LONG error code |
288 | | // later. |
289 | 0 | break; |
290 | 0 | } |
291 | 0 | if (!cert_self_signed(x, &is_self_signed)) { |
292 | 0 | ctx->error = X509_V_ERR_INVALID_EXTENSION; |
293 | 0 | goto end; |
294 | 0 | } |
295 | | // If we are self signed, we break |
296 | 0 | if (is_self_signed) { |
297 | 0 | break; |
298 | 0 | } |
299 | 0 | X509 *issuer = get_trusted_issuer(ctx, x); |
300 | 0 | if (issuer == nullptr) { |
301 | 0 | break; |
302 | 0 | } |
303 | 0 | x = issuer; |
304 | 0 | if (!sk_X509_push(ctx->chain, x)) { |
305 | 0 | X509_free(issuer); |
306 | 0 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
307 | 0 | goto end; |
308 | 0 | } |
309 | 0 | num++; |
310 | 0 | } |
311 | | |
312 | | // we now have our chain, lets check it... |
313 | 0 | trust = check_trust(ctx); |
314 | | |
315 | | // If explicitly rejected error |
316 | 0 | if (trust == X509_TRUST_REJECTED) { |
317 | 0 | goto end; |
318 | 0 | } |
319 | | |
320 | | // If not explicitly trusted then indicate error unless it's a single |
321 | | // self signed certificate in which case we've indicated an error already |
322 | | // and set bad_chain == 1 |
323 | 0 | if (trust != X509_TRUST_TRUSTED && !bad_chain) { |
324 | 0 | if (chain_ss == nullptr || |
325 | 0 | !x509_check_issued_with_callback(ctx, x, chain_ss)) { |
326 | 0 | if (ctx->last_untrusted >= num) { |
327 | 0 | ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY; |
328 | 0 | } else { |
329 | 0 | ctx->error = X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT; |
330 | 0 | } |
331 | 0 | ctx->current_cert = x; |
332 | 0 | } else { |
333 | 0 | if (!sk_X509_push(ctx->chain, chain_ss)) { |
334 | 0 | ctx->error = X509_V_ERR_OUT_OF_MEM; |
335 | 0 | goto end; |
336 | 0 | } |
337 | 0 | num++; |
338 | 0 | ctx->last_untrusted = num; |
339 | 0 | ctx->current_cert = chain_ss; |
340 | 0 | ctx->error = X509_V_ERR_SELF_SIGNED_CERT_IN_CHAIN; |
341 | 0 | chain_ss = nullptr; |
342 | 0 | } |
343 | | |
344 | 0 | ctx->error_depth = num - 1; |
345 | 0 | bad_chain = 1; |
346 | 0 | if (!call_verify_cb(0, ctx)) { |
347 | 0 | goto end; |
348 | 0 | } |
349 | 0 | } |
350 | | |
351 | | // We have the chain complete: now we need to check its purpose |
352 | 0 | if (!check_chain_extensions(ctx) || // |
353 | 0 | !check_id(ctx) || |
354 | | // We check revocation status after copying parameters because they may |
355 | | // be needed for CRL signature verification. |
356 | 0 | !check_revocation(ctx) || // |
357 | 0 | !internal_verify(ctx) || // |
358 | 0 | !check_name_constraints(ctx) || |
359 | | // TODO(davidben): Does `check_policy` still need to be conditioned on |
360 | | // |!bad_chain|? DoS concerns have been resolved. |
361 | 0 | (!bad_chain && !check_policy(ctx))) { |
362 | 0 | goto end; |
363 | 0 | } |
364 | | |
365 | 0 | ok = 1; |
366 | 0 | } |
367 | | |
368 | 0 | end: |
369 | 0 | sk_X509_free(sktmp); |
370 | 0 | X509_free(chain_ss); |
371 | | |
372 | | // Safety net, error returns must set ctx->error |
373 | 0 | if (!ok && ctx->error == X509_V_OK) { |
374 | 0 | ctx->error = X509_V_ERR_UNSPECIFIED; |
375 | 0 | } |
376 | 0 | return ok; |
377 | 0 | } |
378 | | |
379 | | // Given a STACK_OF(X509) find the issuer of cert (if any) |
380 | | |
381 | 0 | static X509 *find_issuer(X509_STORE_CTX *ctx, STACK_OF(X509) *sk, X509 *x) { |
382 | 0 | size_t i; |
383 | 0 | X509 *issuer; |
384 | 0 | for (i = 0; i < sk_X509_num(sk); i++) { |
385 | 0 | issuer = sk_X509_value(sk, i); |
386 | 0 | if (x509_check_issued_with_callback(ctx, x, issuer)) { |
387 | 0 | return issuer; |
388 | 0 | } |
389 | 0 | } |
390 | 0 | return nullptr; |
391 | 0 | } |
392 | | |
393 | | // Given a possible certificate and issuer check them |
394 | | |
395 | | int bssl::x509_check_issued_with_callback(X509_STORE_CTX *ctx, const X509 *x, |
396 | 0 | const X509 *issuer) { |
397 | 0 | int ret; |
398 | 0 | ret = X509_check_issued(issuer, x); |
399 | 0 | if (ret == X509_V_OK) { |
400 | 0 | return 1; |
401 | 0 | } |
402 | | // If we haven't asked for issuer errors don't set ctx |
403 | 0 | if (!(ctx->param->flags & X509_V_FLAG_CB_ISSUER_CHECK)) { |
404 | 0 | return 0; |
405 | 0 | } |
406 | | |
407 | 0 | ctx->error = ret; |
408 | 0 | ctx->current_cert = const_cast<X509 *>(x); |
409 | 0 | return call_verify_cb(0, ctx); |
410 | 0 | } |
411 | | |
412 | 0 | static X509 *get_trusted_issuer(X509_STORE_CTX *ctx, X509 *x) { |
413 | 0 | X509 *issuer; |
414 | 0 | if (ctx->trusted_stack != nullptr) { |
415 | | // Ignore the store and use the configured stack instead. |
416 | 0 | issuer = find_issuer(ctx, ctx->trusted_stack, x); |
417 | 0 | if (issuer != nullptr) { |
418 | 0 | X509_up_ref(issuer); |
419 | 0 | } |
420 | 0 | return issuer; |
421 | 0 | } |
422 | | |
423 | 0 | if (!X509_STORE_CTX_get1_issuer(&issuer, ctx, x)) { |
424 | 0 | return nullptr; |
425 | 0 | } |
426 | 0 | return issuer; |
427 | 0 | } |
428 | | |
429 | | // Check a certificate chains extensions for consistency with the supplied |
430 | | // purpose |
431 | | |
432 | 0 | static int check_chain_extensions(X509_STORE_CTX *ctx) { |
433 | 0 | int plen = 0; |
434 | 0 | int purpose = ctx->param->purpose; |
435 | | |
436 | | // Check all untrusted certificates |
437 | 0 | for (int i = 0; i < ctx->last_untrusted; i++) { |
438 | 0 | X509Impl *x = FromOpaque(sk_X509_value(ctx->chain, i)); |
439 | 0 | if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) && |
440 | 0 | (x->ex_flags & EXFLAG_CRITICAL)) { |
441 | 0 | ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_EXTENSION; |
442 | 0 | ctx->error_depth = i; |
443 | 0 | ctx->current_cert = x; |
444 | 0 | if (!call_verify_cb(0, ctx)) { |
445 | 0 | return 0; |
446 | 0 | } |
447 | 0 | } |
448 | | |
449 | 0 | int must_be_ca = i > 0; |
450 | 0 | if (must_be_ca && !X509_check_ca(x)) { |
451 | 0 | ctx->error = X509_V_ERR_INVALID_CA; |
452 | 0 | ctx->error_depth = i; |
453 | 0 | ctx->current_cert = x; |
454 | 0 | if (!call_verify_cb(0, ctx)) { |
455 | 0 | return 0; |
456 | 0 | } |
457 | 0 | } |
458 | 0 | if (ctx->param->purpose > 0 && |
459 | 0 | X509_check_purpose(x, purpose, must_be_ca) != 1) { |
460 | 0 | ctx->error = X509_V_ERR_INVALID_PURPOSE; |
461 | 0 | ctx->error_depth = i; |
462 | 0 | ctx->current_cert = x; |
463 | 0 | if (!call_verify_cb(0, ctx)) { |
464 | 0 | return 0; |
465 | 0 | } |
466 | 0 | } |
467 | | // Check path length constraints. See steps (l) and (m) of RFC 5280, |
468 | | // section 6.1.4. Note the spec is structured differently from this |
469 | | // logic. Section 6.1.4 runs from root to leaf and does not run on |
470 | | // the leaf. `plen` counts the number of times step (l) would have |
471 | | // run. The constraint is violated if some `x->ex_pathlen`, read in |
472 | | // step (m), is too low to be decremented `plen` times. |
473 | | // |
474 | | // Note that path lengths of self-issued certificates still have to be |
475 | | // considered - they are just not counted as part of the path length! |
476 | 0 | if (i > 1 && x->ex_pathlen != -1 && plen > x->ex_pathlen + 1) { |
477 | 0 | ctx->error = X509_V_ERR_PATH_LENGTH_EXCEEDED; |
478 | 0 | ctx->error_depth = i; |
479 | 0 | ctx->current_cert = x; |
480 | 0 | if (!call_verify_cb(0, ctx)) { |
481 | 0 | return 0; |
482 | 0 | } |
483 | 0 | } |
484 | | // Increment path length if not self issued. As only self-issued |
485 | | // _intermediates_ are skipped in (l) of RFC 5280 (simply because it |
486 | | // operates on certificate chain _edges_), always increment for the first |
487 | | // (the leaf) in the chain. |
488 | 0 | if (i == 0 || !(x->ex_flags & EXFLAG_SI)) { |
489 | 0 | plen++; |
490 | 0 | } |
491 | 0 | } |
492 | | |
493 | 0 | return 1; |
494 | 0 | } |
495 | | |
496 | 0 | static int reject_dns_name_in_common_name(X509 *x509) { |
497 | 0 | const X509_NAME *name = X509_get_subject_name(x509); |
498 | 0 | int i = -1; |
499 | 0 | for (;;) { |
500 | 0 | i = X509_NAME_get_index_by_NID(name, NID_commonName, i); |
501 | 0 | if (i == -1) { |
502 | 0 | return X509_V_OK; |
503 | 0 | } |
504 | | |
505 | 0 | const X509_NAME_ENTRY *entry = X509_NAME_get_entry(name, i); |
506 | 0 | const ASN1_STRING *common_name = X509_NAME_ENTRY_get_data(entry); |
507 | 0 | unsigned char *idval; |
508 | 0 | int idlen = ASN1_STRING_to_UTF8(&idval, common_name); |
509 | 0 | if (idlen < 0) { |
510 | 0 | return X509_V_ERR_OUT_OF_MEM; |
511 | 0 | } |
512 | | // Only process attributes that look like host names. Note it is |
513 | | // important that this check be mirrored in `X509_check_host`. |
514 | 0 | int looks_like_dns = x509v3_looks_like_dns_name(idval, (size_t)idlen); |
515 | 0 | OPENSSL_free(idval); |
516 | 0 | if (looks_like_dns) { |
517 | 0 | return X509_V_ERR_NAME_CONSTRAINTS_WITHOUT_SANS; |
518 | 0 | } |
519 | 0 | } |
520 | 0 | } |
521 | | |
522 | 0 | static int check_name_constraints(X509_STORE_CTX *ctx) { |
523 | 0 | int i, j, rv; |
524 | 0 | int has_name_constraints = 0; |
525 | | // Check name constraints for all certificates |
526 | 0 | for (i = (int)sk_X509_num(ctx->chain) - 1; i >= 0; i--) { |
527 | 0 | X509Impl *x = FromOpaque(sk_X509_value(ctx->chain, i)); |
528 | | // Ignore self issued certs unless last in chain |
529 | 0 | if (i && (x->ex_flags & EXFLAG_SI)) { |
530 | 0 | continue; |
531 | 0 | } |
532 | | // Check against constraints for all certificates higher in chain |
533 | | // including trust anchor. Trust anchor not strictly speaking needed |
534 | | // but if it includes constraints it is to be assumed it expects them |
535 | | // to be obeyed. |
536 | 0 | for (j = (int)sk_X509_num(ctx->chain) - 1; j > i; j--) { |
537 | 0 | NAME_CONSTRAINTS *nc = FromOpaque(sk_X509_value(ctx->chain, j))->nc.get(); |
538 | 0 | if (nc) { |
539 | 0 | has_name_constraints = 1; |
540 | 0 | rv = NAME_CONSTRAINTS_check(x, nc); |
541 | 0 | switch (rv) { |
542 | 0 | case X509_V_OK: |
543 | 0 | continue; |
544 | 0 | case X509_V_ERR_OUT_OF_MEM: |
545 | 0 | ctx->error = rv; |
546 | 0 | return 0; |
547 | 0 | default: |
548 | 0 | ctx->error = rv; |
549 | 0 | ctx->error_depth = i; |
550 | 0 | ctx->current_cert = x; |
551 | 0 | if (!call_verify_cb(0, ctx)) { |
552 | 0 | return 0; |
553 | 0 | } |
554 | 0 | break; |
555 | 0 | } |
556 | 0 | } |
557 | 0 | } |
558 | 0 | } |
559 | | |
560 | | // Name constraints do not match against the common name, but |
561 | | // `X509_check_host` still implements the legacy behavior where, on |
562 | | // certificates lacking a SAN list, DNS-like names in the common name are |
563 | | // checked instead. |
564 | | // |
565 | | // While we could apply the name constraints to the common name, name |
566 | | // constraints are rare enough that can hold such certificates to a higher |
567 | | // standard. Note this does not make "DNS-like" heuristic failures any |
568 | | // worse. A decorative common-name misidentified as a DNS name would fail |
569 | | // the name constraint anyway. |
570 | 0 | X509Impl *leaf = FromOpaque(sk_X509_value(ctx->chain, 0)); |
571 | 0 | if (has_name_constraints && leaf->altname == nullptr) { |
572 | 0 | rv = reject_dns_name_in_common_name(leaf); |
573 | 0 | switch (rv) { |
574 | 0 | case X509_V_OK: |
575 | 0 | break; |
576 | 0 | case X509_V_ERR_OUT_OF_MEM: |
577 | 0 | ctx->error = rv; |
578 | 0 | return 0; |
579 | 0 | default: |
580 | 0 | ctx->error = rv; |
581 | 0 | ctx->error_depth = 0; |
582 | 0 | ctx->current_cert = leaf; |
583 | 0 | if (!call_verify_cb(0, ctx)) { |
584 | 0 | return 0; |
585 | 0 | } |
586 | 0 | break; |
587 | 0 | } |
588 | 0 | } |
589 | | |
590 | 0 | return 1; |
591 | 0 | } |
592 | | |
593 | 0 | static int check_id_error(X509_STORE_CTX *ctx, int errcode) { |
594 | 0 | ctx->error = errcode; |
595 | 0 | ctx->current_cert = ctx->cert; |
596 | 0 | ctx->error_depth = 0; |
597 | 0 | return call_verify_cb(0, ctx); |
598 | 0 | } |
599 | | |
600 | 0 | static int check_hosts(X509 *x, X509_VERIFY_PARAM *param) { |
601 | 0 | size_t i; |
602 | 0 | size_t n = sk_OPENSSL_STRING_num(param->hosts); |
603 | 0 | char *name; |
604 | |
|
605 | 0 | for (i = 0; i < n; ++i) { |
606 | 0 | name = sk_OPENSSL_STRING_value(param->hosts, i); |
607 | 0 | if (X509_check_host(x, name, strlen(name), param->hostflags, nullptr) > 0) { |
608 | 0 | return 1; |
609 | 0 | } |
610 | 0 | } |
611 | 0 | return n == 0; |
612 | 0 | } |
613 | | |
614 | 0 | static int check_id(X509_STORE_CTX *ctx) { |
615 | 0 | X509_VERIFY_PARAM *vpm = ctx->param; |
616 | 0 | X509 *x = ctx->cert; |
617 | 0 | if (vpm->poison) { |
618 | 0 | if (!check_id_error(ctx, X509_V_ERR_INVALID_CALL)) { |
619 | 0 | return 0; |
620 | 0 | } |
621 | 0 | } |
622 | 0 | if (vpm->hosts && check_hosts(x, vpm) <= 0) { |
623 | 0 | if (!check_id_error(ctx, X509_V_ERR_HOSTNAME_MISMATCH)) { |
624 | 0 | return 0; |
625 | 0 | } |
626 | 0 | } |
627 | 0 | if (vpm->email && X509_check_email(x, vpm->email, vpm->emaillen, 0) <= 0) { |
628 | 0 | if (!check_id_error(ctx, X509_V_ERR_EMAIL_MISMATCH)) { |
629 | 0 | return 0; |
630 | 0 | } |
631 | 0 | } |
632 | 0 | if (vpm->ip && X509_check_ip(x, vpm->ip, vpm->iplen, 0) <= 0) { |
633 | 0 | if (!check_id_error(ctx, X509_V_ERR_IP_ADDRESS_MISMATCH)) { |
634 | 0 | return 0; |
635 | 0 | } |
636 | 0 | } |
637 | 0 | return 1; |
638 | 0 | } |
639 | | |
640 | 0 | static int check_trust(X509_STORE_CTX *ctx) { |
641 | 0 | X509 *x = nullptr; |
642 | | // Check all trusted certificates in chain |
643 | 0 | for (size_t i = ctx->last_untrusted; i < sk_X509_num(ctx->chain); i++) { |
644 | 0 | x = sk_X509_value(ctx->chain, i); |
645 | 0 | int trust = X509_check_trust(x, ctx->param->trust, 0); |
646 | | // If explicitly trusted return trusted |
647 | 0 | if (trust == X509_TRUST_TRUSTED) { |
648 | 0 | return X509_TRUST_TRUSTED; |
649 | 0 | } |
650 | | // If explicitly rejected notify callback and reject if not |
651 | | // overridden. |
652 | 0 | if (trust == X509_TRUST_REJECTED) { |
653 | 0 | ctx->error_depth = (int)i; |
654 | 0 | ctx->current_cert = x; |
655 | 0 | ctx->error = X509_V_ERR_CERT_REJECTED; |
656 | 0 | if (!call_verify_cb(0, ctx)) { |
657 | 0 | return X509_TRUST_REJECTED; |
658 | 0 | } |
659 | 0 | } |
660 | 0 | } |
661 | | // If we accept partial chains and have at least one trusted certificate |
662 | | // return success. |
663 | 0 | if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { |
664 | 0 | X509 *mx; |
665 | 0 | if (ctx->last_untrusted < (int)sk_X509_num(ctx->chain)) { |
666 | 0 | return X509_TRUST_TRUSTED; |
667 | 0 | } |
668 | 0 | x = sk_X509_value(ctx->chain, 0); |
669 | 0 | mx = lookup_cert_match(ctx, x); |
670 | 0 | if (mx) { |
671 | 0 | (void)sk_X509_set(ctx->chain, 0, mx); |
672 | 0 | X509_free(x); |
673 | 0 | ctx->last_untrusted = 0; |
674 | 0 | return X509_TRUST_TRUSTED; |
675 | 0 | } |
676 | 0 | } |
677 | | |
678 | | // If no trusted certs in chain at all return untrusted and allow |
679 | | // standard (no issuer cert) etc errors to be indicated. |
680 | 0 | return X509_TRUST_UNTRUSTED; |
681 | 0 | } |
682 | | |
683 | 0 | static int check_revocation(X509_STORE_CTX *ctx) { |
684 | 0 | if (!(ctx->param->flags & X509_V_FLAG_CRL_CHECK)) { |
685 | 0 | return 1; |
686 | 0 | } |
687 | 0 | int last; |
688 | 0 | if (ctx->param->flags & X509_V_FLAG_CRL_CHECK_ALL) { |
689 | 0 | last = (int)sk_X509_num(ctx->chain) - 1; |
690 | 0 | } else { |
691 | 0 | last = 0; |
692 | 0 | } |
693 | 0 | for (int i = 0; i <= last; i++) { |
694 | 0 | ctx->error_depth = i; |
695 | 0 | if (!check_cert(ctx)) { |
696 | 0 | return 0; |
697 | 0 | } |
698 | 0 | } |
699 | 0 | return 1; |
700 | 0 | } |
701 | | |
702 | 0 | static int check_cert(X509_STORE_CTX *ctx) { |
703 | 0 | X509_CRL *crl = nullptr; |
704 | 0 | int ok = 0, cnum = ctx->error_depth; |
705 | 0 | X509 *x = sk_X509_value(ctx->chain, cnum); |
706 | 0 | ctx->current_cert = x; |
707 | 0 | ctx->current_crl_issuer = nullptr; |
708 | 0 | ctx->current_crl_score = 0; |
709 | | |
710 | | // Try to retrieve the relevant CRL. Note that `get_crl` sets |
711 | | // `current_crl_issuer` and `current_crl_score`, which `check_crl` then reads. |
712 | | // |
713 | | // TODO(davidben): The awkward internal calling convention is a historical |
714 | | // artifact of when these functions were user-overridable callbacks, even |
715 | | // though there was no way to set them correctly. These callbacks have since |
716 | | // been removed, so we can pass input and output parameters more directly. |
717 | 0 | if (!get_crl(ctx, &crl, x)) { |
718 | 0 | ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL; |
719 | 0 | ok = call_verify_cb(0, ctx); |
720 | 0 | goto err; |
721 | 0 | } |
722 | | |
723 | 0 | ctx->current_crl = crl; |
724 | 0 | if (!check_crl(ctx, crl) || // |
725 | 0 | !cert_crl(ctx, crl, x)) { |
726 | 0 | goto err; |
727 | 0 | } |
728 | | |
729 | 0 | ok = 1; |
730 | |
|
731 | 0 | err: |
732 | 0 | X509_CRL_free(crl); |
733 | 0 | ctx->current_crl = nullptr; |
734 | 0 | return ok; |
735 | 0 | } |
736 | | |
737 | | // Check CRL times against values in X509_STORE_CTX |
738 | 0 | static int check_crl_time(X509_STORE_CTX *ctx, X509_CRL *crl, int notify) { |
739 | 0 | if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) { |
740 | 0 | return 1; |
741 | 0 | } |
742 | | |
743 | 0 | if (notify) { |
744 | 0 | ctx->current_crl = crl; |
745 | 0 | } |
746 | 0 | int64_t ptime; |
747 | 0 | if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) { |
748 | 0 | ptime = ctx->param->check_time; |
749 | 0 | } else { |
750 | 0 | ptime = time(nullptr); |
751 | 0 | } |
752 | |
|
753 | 0 | int i = X509_cmp_time_posix(X509_CRL_get0_lastUpdate(crl), ptime); |
754 | 0 | if (i == 0) { |
755 | 0 | if (!notify) { |
756 | 0 | return 0; |
757 | 0 | } |
758 | 0 | ctx->error = X509_V_ERR_ERROR_IN_CRL_LAST_UPDATE_FIELD; |
759 | 0 | if (!call_verify_cb(0, ctx)) { |
760 | 0 | return 0; |
761 | 0 | } |
762 | 0 | } |
763 | | |
764 | 0 | if (i > 0) { |
765 | 0 | if (!notify) { |
766 | 0 | return 0; |
767 | 0 | } |
768 | 0 | ctx->error = X509_V_ERR_CRL_NOT_YET_VALID; |
769 | 0 | if (!call_verify_cb(0, ctx)) { |
770 | 0 | return 0; |
771 | 0 | } |
772 | 0 | } |
773 | | |
774 | 0 | if (X509_CRL_get0_nextUpdate(crl)) { |
775 | 0 | i = X509_cmp_time_posix(X509_CRL_get0_nextUpdate(crl), ptime); |
776 | |
|
777 | 0 | if (i == 0) { |
778 | 0 | if (!notify) { |
779 | 0 | return 0; |
780 | 0 | } |
781 | 0 | ctx->error = X509_V_ERR_ERROR_IN_CRL_NEXT_UPDATE_FIELD; |
782 | 0 | if (!call_verify_cb(0, ctx)) { |
783 | 0 | return 0; |
784 | 0 | } |
785 | 0 | } |
786 | 0 | if (i < 0) { |
787 | 0 | if (!notify) { |
788 | 0 | return 0; |
789 | 0 | } |
790 | 0 | ctx->error = X509_V_ERR_CRL_HAS_EXPIRED; |
791 | 0 | if (!call_verify_cb(0, ctx)) { |
792 | 0 | return 0; |
793 | 0 | } |
794 | 0 | } |
795 | 0 | } |
796 | | |
797 | 0 | if (notify) { |
798 | 0 | ctx->current_crl = nullptr; |
799 | 0 | } |
800 | |
|
801 | 0 | return 1; |
802 | 0 | } |
803 | | |
804 | | static int get_crl_sk(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 **pissuer, |
805 | 0 | int *pscore, STACK_OF(X509_CRL) *crls) { |
806 | 0 | int crl_score, best_score = *pscore; |
807 | 0 | X509 *x = ctx->current_cert; |
808 | 0 | X509_CRL *best_crl = nullptr; |
809 | 0 | X509 *crl_issuer = nullptr, *best_crl_issuer = nullptr; |
810 | |
|
811 | 0 | for (size_t i = 0; i < sk_X509_CRL_num(crls); i++) { |
812 | 0 | X509_CRL *crl = sk_X509_CRL_value(crls, i); |
813 | 0 | crl_score = get_crl_score(ctx, &crl_issuer, crl, x); |
814 | 0 | if (crl_score < best_score || crl_score == 0) { |
815 | 0 | continue; |
816 | 0 | } |
817 | | // If current CRL is equivalent use it if it is newer |
818 | 0 | if (crl_score == best_score && best_crl != nullptr) { |
819 | 0 | int day, sec; |
820 | 0 | if (ASN1_TIME_diff(&day, &sec, X509_CRL_get0_lastUpdate(best_crl), |
821 | 0 | X509_CRL_get0_lastUpdate(crl)) == 0) { |
822 | 0 | continue; |
823 | 0 | } |
824 | | // ASN1_TIME_diff never returns inconsistent signs for `day` |
825 | | // and `sec`. |
826 | 0 | if (day <= 0 && sec <= 0) { |
827 | 0 | continue; |
828 | 0 | } |
829 | 0 | } |
830 | 0 | best_crl = crl; |
831 | 0 | best_crl_issuer = crl_issuer; |
832 | 0 | best_score = crl_score; |
833 | 0 | } |
834 | |
|
835 | 0 | if (best_crl) { |
836 | 0 | if (*pcrl) { |
837 | 0 | X509_CRL_free(*pcrl); |
838 | 0 | } |
839 | 0 | *pcrl = best_crl; |
840 | 0 | *pissuer = best_crl_issuer; |
841 | 0 | *pscore = best_score; |
842 | 0 | X509_CRL_up_ref(best_crl); |
843 | 0 | } |
844 | |
|
845 | 0 | if (best_score >= CRL_SCORE_VALID) { |
846 | 0 | return 1; |
847 | 0 | } |
848 | | |
849 | 0 | return 0; |
850 | 0 | } |
851 | | |
852 | | // For a given CRL return how suitable it is for the supplied certificate |
853 | | // 'x'. The return value is a mask of several criteria. If the issuer is not |
854 | | // the certificate issuer this is returned in *pissuer. |
855 | | static int get_crl_score(X509_STORE_CTX *ctx, X509 **pissuer, X509_CRL *crl, |
856 | 0 | X509 *x) { |
857 | 0 | int crl_score = 0; |
858 | | |
859 | | // First see if we can reject CRL straight away |
860 | | |
861 | | // Invalid IDP cannot be processed |
862 | 0 | if (crl->idp_flags & IDP_INVALID) { |
863 | 0 | return 0; |
864 | 0 | } |
865 | | // Reason codes and indirect CRLs are not supported. |
866 | 0 | if (crl->idp_flags & (IDP_INDIRECT | IDP_REASONS)) { |
867 | 0 | return 0; |
868 | 0 | } |
869 | | // We do not support indirect CRLs, so the issuer names must match. |
870 | 0 | if (X509_NAME_cmp(X509_get_issuer_name(x), X509_CRL_get_issuer(crl))) { |
871 | 0 | return 0; |
872 | 0 | } |
873 | 0 | crl_score |= CRL_SCORE_ISSUER_NAME; |
874 | |
|
875 | 0 | if (!(crl->flags & EXFLAG_CRITICAL)) { |
876 | 0 | crl_score |= CRL_SCORE_NOCRITICAL; |
877 | 0 | } |
878 | | |
879 | | // Check expiry |
880 | 0 | if (check_crl_time(ctx, crl, 0)) { |
881 | 0 | crl_score |= CRL_SCORE_TIME; |
882 | 0 | } |
883 | | |
884 | | // Check authority key ID and locate certificate issuer |
885 | 0 | if (!crl_akid_check(ctx, crl, pissuer, &crl_score)) { |
886 | | // If we can't locate certificate issuer at this point forget it |
887 | 0 | return 0; |
888 | 0 | } |
889 | | |
890 | | // Check cert for matching CRL distribution points |
891 | 0 | if (crl_crldp_check(x, crl, crl_score)) { |
892 | 0 | crl_score |= CRL_SCORE_SCOPE; |
893 | 0 | } |
894 | |
|
895 | 0 | return crl_score; |
896 | 0 | } |
897 | | |
898 | | static int crl_akid_check(X509_STORE_CTX *ctx, X509_CRL *crl, X509 **pissuer, |
899 | 0 | int *pcrl_score) { |
900 | 0 | X509 *crl_issuer = nullptr; |
901 | 0 | X509_NAME *cnm = X509_CRL_get_issuer(crl); |
902 | 0 | int cidx = ctx->error_depth; |
903 | |
|
904 | 0 | if ((size_t)cidx != sk_X509_num(ctx->chain) - 1) { |
905 | 0 | cidx++; |
906 | 0 | } |
907 | |
|
908 | 0 | crl_issuer = sk_X509_value(ctx->chain, cidx); |
909 | |
|
910 | 0 | if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { |
911 | 0 | *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_ISSUER_CERT; |
912 | 0 | *pissuer = crl_issuer; |
913 | 0 | return 1; |
914 | 0 | } |
915 | | |
916 | 0 | for (cidx++; cidx < (int)sk_X509_num(ctx->chain); cidx++) { |
917 | 0 | crl_issuer = sk_X509_value(ctx->chain, cidx); |
918 | 0 | if (X509_NAME_cmp(X509_get_subject_name(crl_issuer), cnm)) { |
919 | 0 | continue; |
920 | 0 | } |
921 | 0 | if (X509_check_akid(crl_issuer, crl->akid) == X509_V_OK) { |
922 | 0 | *pcrl_score |= CRL_SCORE_AKID | CRL_SCORE_SAME_PATH; |
923 | 0 | *pissuer = crl_issuer; |
924 | 0 | return 1; |
925 | 0 | } |
926 | 0 | } |
927 | | |
928 | 0 | return 0; |
929 | 0 | } |
930 | | |
931 | | // Check for match between two dist point names: three separate cases. 1. |
932 | | // Both are relative names and compare X509_NAME types. 2. One full, one |
933 | | // relative. Compare X509_NAME to GENERAL_NAMES. 3. Both are full names and |
934 | | // compare two GENERAL_NAMES. 4. One is NULL: automatic match. |
935 | 0 | static int idp_check_dp(DIST_POINT_NAME *a, DIST_POINT_NAME *b) { |
936 | 0 | X509_NAME *nm = nullptr; |
937 | 0 | GENERAL_NAMES *gens = nullptr; |
938 | 0 | GENERAL_NAME *gena, *genb; |
939 | 0 | size_t i, j; |
940 | 0 | if (!a || !b) { |
941 | 0 | return 1; |
942 | 0 | } |
943 | 0 | if (a->type == 1) { |
944 | 0 | if (!a->dpname) { |
945 | 0 | return 0; |
946 | 0 | } |
947 | | // Case 1: two X509_NAME |
948 | 0 | if (b->type == 1) { |
949 | 0 | if (!b->dpname) { |
950 | 0 | return 0; |
951 | 0 | } |
952 | 0 | if (!X509_NAME_cmp(a->dpname, b->dpname)) { |
953 | 0 | return 1; |
954 | 0 | } else { |
955 | 0 | return 0; |
956 | 0 | } |
957 | 0 | } |
958 | | // Case 2: set name and GENERAL_NAMES appropriately |
959 | 0 | nm = a->dpname; |
960 | 0 | gens = b->name.fullname; |
961 | 0 | } else if (b->type == 1) { |
962 | 0 | if (!b->dpname) { |
963 | 0 | return 0; |
964 | 0 | } |
965 | | // Case 2: set name and GENERAL_NAMES appropriately |
966 | 0 | gens = a->name.fullname; |
967 | 0 | nm = b->dpname; |
968 | 0 | } |
969 | | |
970 | | // Handle case 2 with one GENERAL_NAMES and one X509_NAME |
971 | 0 | if (nm) { |
972 | 0 | for (i = 0; i < sk_GENERAL_NAME_num(gens); i++) { |
973 | 0 | gena = sk_GENERAL_NAME_value(gens, i); |
974 | 0 | if (gena->type != GEN_DIRNAME) { |
975 | 0 | continue; |
976 | 0 | } |
977 | 0 | if (!X509_NAME_cmp(nm, gena->d.directoryName)) { |
978 | 0 | return 1; |
979 | 0 | } |
980 | 0 | } |
981 | 0 | return 0; |
982 | 0 | } |
983 | | |
984 | | // Else case 3: two GENERAL_NAMES |
985 | | |
986 | 0 | for (i = 0; i < sk_GENERAL_NAME_num(a->name.fullname); i++) { |
987 | 0 | gena = sk_GENERAL_NAME_value(a->name.fullname, i); |
988 | 0 | for (j = 0; j < sk_GENERAL_NAME_num(b->name.fullname); j++) { |
989 | 0 | genb = sk_GENERAL_NAME_value(b->name.fullname, j); |
990 | 0 | if (!GENERAL_NAME_cmp(gena, genb)) { |
991 | 0 | return 1; |
992 | 0 | } |
993 | 0 | } |
994 | 0 | } |
995 | | |
996 | 0 | return 0; |
997 | 0 | } |
998 | | |
999 | | // Check CRLDP and IDP |
1000 | 0 | static int crl_crldp_check(X509 *x, X509_CRL *crl, int crl_score) { |
1001 | 0 | auto *impl = FromOpaque(x); |
1002 | | // TODO(bbe): crbug.com/409778435 Make tests for the corner cases we hit |
1003 | | // here so that we stay correct for RFC 5280 6.3.3 steps b.1 and b.2 |
1004 | 0 | if (crl->idp_flags & IDP_ONLYATTR) { |
1005 | 0 | return 0; |
1006 | 0 | } |
1007 | 0 | if (impl->ex_flags & EXFLAG_CA) { |
1008 | 0 | if (crl->idp_flags & IDP_ONLYUSER) { |
1009 | 0 | return 0; |
1010 | 0 | } |
1011 | 0 | } else { |
1012 | 0 | if (crl->idp_flags & IDP_ONLYCA) { |
1013 | 0 | return 0; |
1014 | 0 | } |
1015 | 0 | } |
1016 | 0 | for (size_t i = 0; i < sk_DIST_POINT_num(impl->crldp.get()); i++) { |
1017 | 0 | DIST_POINT *dp = sk_DIST_POINT_value(impl->crldp.get(), i); |
1018 | | // Skip distribution points with a reasons field or a CRL issuer: |
1019 | | // |
1020 | | // We do not support CRLs partitioned by reason code. RFC 5280 requires CAs |
1021 | | // include at least one DistributionPoint that covers all reasons. |
1022 | | // |
1023 | | // We also do not support indirect CRLs, and a CRL issuer can only match |
1024 | | // indirect CRLs (RFC 5280, section 6.3.3, step b.1). |
1025 | 0 | if (dp->reasons != nullptr || dp->CRLissuer != nullptr) { |
1026 | 0 | continue; |
1027 | 0 | } |
1028 | | // At this point we have already checked that the CRL issuer matches |
1029 | | // the certificate issuer (and set CRL_SCORE_ISSUER_NAME); |
1030 | | |
1031 | | // RFC 5280 Section 6.3.3 step b.2 |
1032 | 0 | if (!crl->idp || idp_check_dp(dp->distpoint, crl->idp->distpoint)){ |
1033 | 0 | return 1; |
1034 | 0 | } |
1035 | 0 | } |
1036 | | |
1037 | | // If the CRL does not specify an issuing distribution point, allow it to |
1038 | | // match anything. |
1039 | | // |
1040 | | // TODO(davidben): Does this match RFC 5280? It's hard to follow because RFC |
1041 | | // 5280 starts from distribution points, while this starts from CRLs. |
1042 | 0 | return !crl->idp || !crl->idp->distpoint; |
1043 | 0 | } |
1044 | | |
1045 | | // Retrieve CRL corresponding to current certificate. |
1046 | 0 | static int get_crl(X509_STORE_CTX *ctx, X509_CRL **pcrl, X509 *x) { |
1047 | 0 | X509 *issuer = nullptr; |
1048 | 0 | int crl_score = 0; |
1049 | 0 | X509_CRL *crl = nullptr; |
1050 | 0 | STACK_OF(X509_CRL) *skcrl = nullptr; |
1051 | 0 | if (get_crl_sk(ctx, &crl, &issuer, &crl_score, ctx->crls)) { |
1052 | 0 | goto done; |
1053 | 0 | } |
1054 | | |
1055 | | // Lookup CRLs from store |
1056 | 0 | skcrl = X509_STORE_CTX_get1_crls(ctx, X509_get_issuer_name(x)); |
1057 | | |
1058 | | // If no CRLs found and a near match from get_crl_sk use that |
1059 | 0 | if (!skcrl && crl) { |
1060 | 0 | goto done; |
1061 | 0 | } |
1062 | | |
1063 | 0 | get_crl_sk(ctx, &crl, &issuer, &crl_score, skcrl); |
1064 | |
|
1065 | 0 | sk_X509_CRL_pop_free(skcrl, X509_CRL_free); |
1066 | |
|
1067 | 0 | done: |
1068 | | // If we got any kind of CRL use it and return success |
1069 | 0 | if (crl) { |
1070 | 0 | ctx->current_crl_issuer = issuer; |
1071 | 0 | ctx->current_crl_score = crl_score; |
1072 | 0 | *pcrl = crl; |
1073 | 0 | return 1; |
1074 | 0 | } |
1075 | | |
1076 | 0 | return 0; |
1077 | 0 | } |
1078 | | |
1079 | | // Check CRL validity |
1080 | 0 | static int check_crl(X509_STORE_CTX *ctx, X509_CRL *crl) { |
1081 | 0 | X509Impl *issuer = nullptr; |
1082 | 0 | int cnum = ctx->error_depth; |
1083 | 0 | int chnum = (int)sk_X509_num(ctx->chain) - 1; |
1084 | | // If we have an alternative CRL issuer cert use that. Otherwise, it is the |
1085 | | // issuer of the current certificate. |
1086 | 0 | if (ctx->current_crl_issuer) { |
1087 | 0 | issuer = FromOpaque(ctx->current_crl_issuer); |
1088 | 0 | } else if (cnum < chnum) { |
1089 | 0 | issuer = FromOpaque(sk_X509_value(ctx->chain, cnum + 1)); |
1090 | 0 | } else { |
1091 | 0 | issuer = FromOpaque(sk_X509_value(ctx->chain, chnum)); |
1092 | | // If not self signed, can't check signature |
1093 | 0 | if (!x509_check_issued_with_callback(ctx, issuer, issuer)) { |
1094 | 0 | ctx->error = X509_V_ERR_UNABLE_TO_GET_CRL_ISSUER; |
1095 | 0 | if (!call_verify_cb(0, ctx)) { |
1096 | 0 | return 0; |
1097 | 0 | } |
1098 | 0 | } |
1099 | 0 | } |
1100 | | |
1101 | 0 | if (issuer) { |
1102 | | // Check for cRLSign bit if keyUsage present |
1103 | 0 | if ((issuer->ex_flags & EXFLAG_KUSAGE) && |
1104 | 0 | !(issuer->ex_kusage & X509v3_KU_CRL_SIGN)) { |
1105 | 0 | ctx->error = X509_V_ERR_KEYUSAGE_NO_CRL_SIGN; |
1106 | 0 | if (!call_verify_cb(0, ctx)) { |
1107 | 0 | return 0; |
1108 | 0 | } |
1109 | 0 | } |
1110 | | |
1111 | 0 | if (!(ctx->current_crl_score & CRL_SCORE_SCOPE)) { |
1112 | 0 | ctx->error = X509_V_ERR_DIFFERENT_CRL_SCOPE; |
1113 | 0 | if (!call_verify_cb(0, ctx)) { |
1114 | 0 | return 0; |
1115 | 0 | } |
1116 | 0 | } |
1117 | | |
1118 | 0 | if (crl->idp_flags & IDP_INVALID) { |
1119 | 0 | ctx->error = X509_V_ERR_INVALID_EXTENSION; |
1120 | 0 | if (!call_verify_cb(0, ctx)) { |
1121 | 0 | return 0; |
1122 | 0 | } |
1123 | 0 | } |
1124 | | |
1125 | 0 | if (!(ctx->current_crl_score & CRL_SCORE_TIME)) { |
1126 | 0 | if (!check_crl_time(ctx, crl, 1)) { |
1127 | 0 | return 0; |
1128 | 0 | } |
1129 | 0 | } |
1130 | | |
1131 | | // Attempt to get issuer certificate public key |
1132 | 0 | EVP_PKEY *ikey = X509_get0_pubkey(issuer); |
1133 | 0 | if (!ikey) { |
1134 | 0 | ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; |
1135 | 0 | if (!call_verify_cb(0, ctx)) { |
1136 | 0 | return 0; |
1137 | 0 | } |
1138 | 0 | } else { |
1139 | | // Verify CRL signature |
1140 | 0 | if (X509_CRL_verify(crl, ikey) <= 0) { |
1141 | 0 | ctx->error = X509_V_ERR_CRL_SIGNATURE_FAILURE; |
1142 | 0 | if (!call_verify_cb(0, ctx)) { |
1143 | 0 | return 0; |
1144 | 0 | } |
1145 | 0 | } |
1146 | 0 | } |
1147 | 0 | } |
1148 | | |
1149 | 0 | return 1; |
1150 | 0 | } |
1151 | | |
1152 | | // Check certificate against CRL |
1153 | 0 | static int cert_crl(X509_STORE_CTX *ctx, X509_CRL *crl, X509 *x) { |
1154 | | // The rules changed for this... previously if a CRL contained unhandled |
1155 | | // critical extensions it could still be used to indicate a certificate |
1156 | | // was revoked. This has since been changed since critical extension can |
1157 | | // change the meaning of CRL entries. |
1158 | 0 | if (!(ctx->param->flags & X509_V_FLAG_IGNORE_CRITICAL) && |
1159 | 0 | (crl->flags & EXFLAG_CRITICAL)) { |
1160 | 0 | ctx->error = X509_V_ERR_UNHANDLED_CRITICAL_CRL_EXTENSION; |
1161 | 0 | if (!call_verify_cb(0, ctx)) { |
1162 | 0 | return 0; |
1163 | 0 | } |
1164 | 0 | } |
1165 | | // Look for serial number of certificate in CRL. |
1166 | 0 | X509_REVOKED *rev; |
1167 | 0 | if (X509_CRL_get0_by_cert(crl, &rev, x)) { |
1168 | 0 | ctx->error = X509_V_ERR_CERT_REVOKED; |
1169 | 0 | if (!call_verify_cb(0, ctx)) { |
1170 | 0 | return 0; |
1171 | 0 | } |
1172 | 0 | } |
1173 | | |
1174 | 0 | return 1; |
1175 | 0 | } |
1176 | | |
1177 | 0 | static int check_policy(X509_STORE_CTX *ctx) { |
1178 | 0 | X509 *current_cert = nullptr; |
1179 | 0 | int ret = X509_policy_check(ctx->chain, ctx->param->policies, |
1180 | 0 | ctx->param->flags, ¤t_cert); |
1181 | 0 | if (ret != X509_V_OK) { |
1182 | 0 | ctx->current_cert = current_cert; |
1183 | 0 | ctx->error = ret; |
1184 | 0 | if (ret == X509_V_ERR_OUT_OF_MEM) { |
1185 | 0 | return 0; |
1186 | 0 | } |
1187 | 0 | return call_verify_cb(0, ctx); |
1188 | 0 | } |
1189 | | |
1190 | 0 | return 1; |
1191 | 0 | } |
1192 | | |
1193 | 0 | static int check_cert_time(X509_STORE_CTX *ctx, X509 *x) { |
1194 | 0 | if (ctx->param->flags & X509_V_FLAG_NO_CHECK_TIME) { |
1195 | 0 | return 1; |
1196 | 0 | } |
1197 | | |
1198 | 0 | int64_t ptime; |
1199 | 0 | if (ctx->param->flags & X509_V_FLAG_USE_CHECK_TIME) { |
1200 | 0 | ptime = ctx->param->check_time; |
1201 | 0 | } else { |
1202 | 0 | ptime = time(nullptr); |
1203 | 0 | } |
1204 | |
|
1205 | 0 | int i = (ctx->param->flags & X509_V_FLAG_ALLOW_TIMEZONE_OFFSET) |
1206 | 0 | ? X509_cmp_time_posix_nonstandard(X509_get_notBefore(x), ptime) |
1207 | 0 | : X509_cmp_time_posix(X509_get_notBefore(x), ptime); |
1208 | 0 | if (i == 0) { |
1209 | 0 | ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD; |
1210 | 0 | ctx->current_cert = x; |
1211 | 0 | if (!call_verify_cb(0, ctx)) { |
1212 | 0 | return 0; |
1213 | 0 | } |
1214 | 0 | } |
1215 | | |
1216 | 0 | if (i > 0) { |
1217 | 0 | ctx->error = X509_V_ERR_CERT_NOT_YET_VALID; |
1218 | 0 | ctx->current_cert = x; |
1219 | 0 | if (!call_verify_cb(0, ctx)) { |
1220 | 0 | return 0; |
1221 | 0 | } |
1222 | 0 | } |
1223 | | |
1224 | 0 | i = (ctx->param->flags & X509_V_FLAG_ALLOW_TIMEZONE_OFFSET) |
1225 | 0 | ? X509_cmp_time_posix_nonstandard(X509_get_notAfter(x), ptime) |
1226 | 0 | : X509_cmp_time_posix(X509_get_notAfter(x), ptime); |
1227 | 0 | if (i == 0) { |
1228 | 0 | ctx->error = X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD; |
1229 | 0 | ctx->current_cert = x; |
1230 | 0 | if (!call_verify_cb(0, ctx)) { |
1231 | 0 | return 0; |
1232 | 0 | } |
1233 | 0 | } |
1234 | | |
1235 | 0 | if (i < 0) { |
1236 | 0 | ctx->error = X509_V_ERR_CERT_HAS_EXPIRED; |
1237 | 0 | ctx->current_cert = x; |
1238 | 0 | if (!call_verify_cb(0, ctx)) { |
1239 | 0 | return 0; |
1240 | 0 | } |
1241 | 0 | } |
1242 | | |
1243 | 0 | return 1; |
1244 | 0 | } |
1245 | | |
1246 | | static int verify_signature(X509_STORE_CTX *ctx, const X509 *x509, |
1247 | 0 | const X509 *issuer, EVP_PKEY *pkey) { |
1248 | 0 | int ret; |
1249 | 0 | if ((ctx->param->flags & X509_V_FLAG_USE_MTC_DRAFT_PLANTS_05) && |
1250 | 0 | x509_is_merkle_tree_ca(issuer)) { |
1251 | 0 | ret = x509_verify_mtc(x509, pkey, issuer); |
1252 | 0 | } else { |
1253 | 0 | ret = X509_verify(x509, pkey); |
1254 | 0 | } |
1255 | 0 | return ret ? X509_V_OK : X509_V_ERR_CERT_SIGNATURE_FAILURE; |
1256 | 0 | } |
1257 | | |
1258 | 0 | static int internal_verify(X509_STORE_CTX *ctx) { |
1259 | | // TODO(davidben): This logic is incredibly confusing. Rewrite this: |
1260 | | // |
1261 | | // First, don't allow the verify callback to suppress |
1262 | | // X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY, which will simplify the |
1263 | | // signature check. Then replace jumping into the middle of the loop. It's |
1264 | | // trying to ensure that all certificates see `check_cert_time`, then checking |
1265 | | // the root's self signature when requested, but not breaking partial chains |
1266 | | // in the process. |
1267 | 0 | int n = (int)sk_X509_num(ctx->chain); |
1268 | 0 | ctx->error_depth = n - 1; |
1269 | 0 | n--; |
1270 | 0 | X509 *xi = sk_X509_value(ctx->chain, n); |
1271 | 0 | X509 *xs; |
1272 | 0 | if (x509_check_issued_with_callback(ctx, xi, xi)) { |
1273 | 0 | xs = xi; |
1274 | 0 | } else { |
1275 | 0 | if (ctx->param->flags & X509_V_FLAG_PARTIAL_CHAIN) { |
1276 | 0 | xs = xi; |
1277 | 0 | goto check_cert; |
1278 | 0 | } |
1279 | 0 | if (n <= 0) { |
1280 | 0 | ctx->error = X509_V_ERR_UNABLE_TO_VERIFY_LEAF_SIGNATURE; |
1281 | 0 | ctx->current_cert = xi; |
1282 | 0 | return call_verify_cb(0, ctx); |
1283 | 0 | } |
1284 | 0 | n--; |
1285 | 0 | ctx->error_depth = n; |
1286 | 0 | xs = sk_X509_value(ctx->chain, n); |
1287 | 0 | } |
1288 | | |
1289 | | // ctx->error=0; not needed |
1290 | 0 | while (n >= 0) { |
1291 | 0 | ctx->error_depth = n; |
1292 | | |
1293 | | // Skip signature check for self signed certificates unless |
1294 | | // explicitly asked for. It doesn't add any security and just wastes |
1295 | | // time. |
1296 | 0 | if (xs != xi || (ctx->param->flags & X509_V_FLAG_CHECK_SS_SIGNATURE)) { |
1297 | 0 | EVP_PKEY *pkey = X509_get0_pubkey(xi); |
1298 | 0 | if (pkey == nullptr) { |
1299 | 0 | ctx->error = X509_V_ERR_UNABLE_TO_DECODE_ISSUER_PUBLIC_KEY; |
1300 | 0 | ctx->current_cert = xi; |
1301 | 0 | if (!call_verify_cb(0, ctx)) { |
1302 | 0 | return 0; |
1303 | 0 | } |
1304 | 0 | } else if (int err = verify_signature(ctx, xs, xi, pkey); |
1305 | 0 | err != X509_V_OK) { |
1306 | 0 | ctx->error = err; |
1307 | 0 | ctx->current_cert = xs; |
1308 | 0 | if (!call_verify_cb(0, ctx)) { |
1309 | 0 | return 0; |
1310 | 0 | } |
1311 | 0 | } |
1312 | 0 | } |
1313 | | |
1314 | 0 | check_cert: |
1315 | 0 | if (!check_cert_time(ctx, xs)) { |
1316 | 0 | return 0; |
1317 | 0 | } |
1318 | | |
1319 | | // The last error (if any) is still in the error value |
1320 | 0 | ctx->current_cert = xs; |
1321 | 0 | if (!call_verify_cb(1, ctx)) { |
1322 | 0 | return 0; |
1323 | 0 | } |
1324 | | |
1325 | 0 | n--; |
1326 | 0 | if (n >= 0) { |
1327 | 0 | xi = xs; |
1328 | 0 | xs = sk_X509_value(ctx->chain, n); |
1329 | 0 | } |
1330 | 0 | } |
1331 | | |
1332 | 0 | return 1; |
1333 | 0 | } |
1334 | | |
1335 | 0 | int X509_cmp_current_time(const ASN1_TIME *ctm) { |
1336 | 0 | return X509_cmp_time_posix(ctm, time(nullptr)); |
1337 | 0 | } |
1338 | | |
1339 | 0 | int X509_cmp_time(const ASN1_TIME *ctm, const time_t *cmp_time) { |
1340 | 0 | int64_t compare_time = (cmp_time == nullptr) ? time(nullptr) : *cmp_time; |
1341 | 0 | return X509_cmp_time_posix(ctm, compare_time); |
1342 | 0 | } |
1343 | | |
1344 | 0 | int X509_cmp_time_posix(const ASN1_TIME *ctm, int64_t cmp_time) { |
1345 | 0 | int64_t ctm_time; |
1346 | 0 | if (!ASN1_TIME_to_posix(ctm, &ctm_time)) { |
1347 | 0 | return 0; |
1348 | 0 | } |
1349 | | // The return value 0 is reserved for errors. |
1350 | 0 | return (ctm_time - cmp_time <= 0) ? -1 : 1; |
1351 | 0 | } |
1352 | | |
1353 | 0 | int X509_cmp_time_posix_nonstandard(const ASN1_TIME *ctm, int64_t cmp_time) { |
1354 | 0 | int64_t ctm_time; |
1355 | 0 | if (!ASN1_TIME_to_posix_nonstandard(ctm, &ctm_time)) { |
1356 | 0 | return 0; |
1357 | 0 | } |
1358 | | // The return value 0 is reserved for errors. |
1359 | 0 | return (ctm_time - cmp_time <= 0) ? -1 : 1; |
1360 | 0 | } |
1361 | | |
1362 | 0 | ASN1_TIME *X509_gmtime_adj(ASN1_TIME *s, long offset_sec) { |
1363 | 0 | return X509_time_adj(s, offset_sec, nullptr); |
1364 | 0 | } |
1365 | | |
1366 | 0 | ASN1_TIME *X509_time_adj(ASN1_TIME *s, long offset_sec, const time_t *in_tm) { |
1367 | 0 | return X509_time_adj_ex(s, 0, offset_sec, in_tm); |
1368 | 0 | } |
1369 | | |
1370 | | ASN1_TIME *X509_time_adj_ex(ASN1_TIME *s, int offset_day, long offset_sec, |
1371 | 0 | const time_t *in_tm) { |
1372 | 0 | int64_t t = 0; |
1373 | |
|
1374 | 0 | if (in_tm) { |
1375 | 0 | t = *in_tm; |
1376 | 0 | } else { |
1377 | 0 | t = time(nullptr); |
1378 | 0 | } |
1379 | |
|
1380 | 0 | return ASN1_TIME_adj(s, t, offset_day, offset_sec); |
1381 | 0 | } |
1382 | | |
1383 | | int X509_STORE_CTX_get_ex_new_index(long argl, void *argp, |
1384 | | CRYPTO_EX_unused *unused, |
1385 | | CRYPTO_EX_dup *dup_unused, |
1386 | 0 | CRYPTO_EX_free *free_func) { |
1387 | 0 | return CRYPTO_get_ex_new_index_ex(&g_ex_data_class, argl, argp, free_func); |
1388 | 0 | } |
1389 | | |
1390 | 8.02k | int X509_STORE_CTX_set_ex_data(X509_STORE_CTX *ctx, int idx, void *data) { |
1391 | 8.02k | return CRYPTO_set_ex_data(&ctx->ex_data, idx, data); |
1392 | 8.02k | } |
1393 | | |
1394 | 0 | void *X509_STORE_CTX_get_ex_data(X509_STORE_CTX *ctx, int idx) { |
1395 | 0 | return CRYPTO_get_ex_data(&ctx->ex_data, idx); |
1396 | 0 | } |
1397 | | |
1398 | 8.02k | int X509_STORE_CTX_get_error(const X509_STORE_CTX *ctx) { return ctx->error; } |
1399 | | |
1400 | 0 | void X509_STORE_CTX_set_error(X509_STORE_CTX *ctx, int err) { |
1401 | 0 | ctx->error = err; |
1402 | 0 | } |
1403 | | |
1404 | 0 | int X509_STORE_CTX_get_error_depth(const X509_STORE_CTX *ctx) { |
1405 | 0 | return ctx->error_depth; |
1406 | 0 | } |
1407 | | |
1408 | 0 | X509 *X509_STORE_CTX_get_current_cert(const X509_STORE_CTX *ctx) { |
1409 | 0 | return ctx->current_cert; |
1410 | 0 | } |
1411 | | |
1412 | 0 | STACK_OF(X509) *X509_STORE_CTX_get_chain(const X509_STORE_CTX *ctx) { |
1413 | 0 | return ctx->chain; |
1414 | 0 | } |
1415 | | |
1416 | 0 | STACK_OF(X509) *X509_STORE_CTX_get0_chain(const X509_STORE_CTX *ctx) { |
1417 | 0 | return ctx->chain; |
1418 | 0 | } |
1419 | | |
1420 | 0 | STACK_OF(X509) *X509_STORE_CTX_get1_chain(const X509_STORE_CTX *ctx) { |
1421 | 0 | if (!ctx->chain) { |
1422 | 0 | return nullptr; |
1423 | 0 | } |
1424 | 0 | return X509_chain_up_ref(ctx->chain); |
1425 | 0 | } |
1426 | | |
1427 | 0 | X509_CRL *X509_STORE_CTX_get0_current_crl(const X509_STORE_CTX *ctx) { |
1428 | 0 | return ctx->current_crl; |
1429 | 0 | } |
1430 | | |
1431 | 0 | X509_STORE_CTX *X509_STORE_CTX_get0_parent_ctx(const X509_STORE_CTX *ctx) { |
1432 | | // In OpenSSL, an `X509_STORE_CTX` sometimes has a parent context during CRL |
1433 | | // path validation for indirect CRLs. We require the CRL to be issued |
1434 | | // somewhere along the certificate path, so this is always NULL. |
1435 | 0 | return nullptr; |
1436 | 0 | } |
1437 | | |
1438 | 0 | void X509_STORE_CTX_set_chain(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) { |
1439 | 0 | ctx->untrusted = sk; |
1440 | 0 | } |
1441 | | |
1442 | 0 | STACK_OF(X509) *X509_STORE_CTX_get0_untrusted(const X509_STORE_CTX *ctx) { |
1443 | 0 | return ctx->untrusted; |
1444 | 0 | } |
1445 | | |
1446 | 0 | void X509_STORE_CTX_set0_crls(X509_STORE_CTX *ctx, STACK_OF(X509_CRL) *sk) { |
1447 | 0 | ctx->crls = sk; |
1448 | 0 | } |
1449 | | |
1450 | 0 | int X509_STORE_CTX_set_purpose(X509_STORE_CTX *ctx, int purpose) { |
1451 | | // If `purpose` is zero, this function historically silently did nothing. |
1452 | 0 | if (purpose == 0) { |
1453 | 0 | return 1; |
1454 | 0 | } |
1455 | | |
1456 | 0 | const X509_PURPOSE *pobj = X509_PURPOSE_get0(purpose); |
1457 | 0 | if (pobj == nullptr) { |
1458 | 0 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_PURPOSE_ID); |
1459 | 0 | return 0; |
1460 | 0 | } |
1461 | | |
1462 | 0 | int trust = X509_PURPOSE_get_trust(pobj); |
1463 | 0 | if (!X509_STORE_CTX_set_trust(ctx, trust)) { |
1464 | 0 | return 0; |
1465 | 0 | } |
1466 | | |
1467 | 0 | if (ctx->param->purpose == 0) { |
1468 | 0 | ctx->param->purpose = purpose; |
1469 | 0 | } |
1470 | 0 | return 1; |
1471 | 0 | } |
1472 | | |
1473 | 0 | int X509_STORE_CTX_set_trust(X509_STORE_CTX *ctx, int trust) { |
1474 | | // If `trust` is zero, this function historically silently did nothing. |
1475 | 0 | if (trust == 0) { |
1476 | 0 | return 1; |
1477 | 0 | } |
1478 | | |
1479 | 0 | if (!X509_is_valid_trust_id(trust)) { |
1480 | 0 | OPENSSL_PUT_ERROR(X509, X509_R_UNKNOWN_TRUST_ID); |
1481 | 0 | return 0; |
1482 | 0 | } |
1483 | | |
1484 | 0 | if (ctx->param->trust == 0) { |
1485 | 0 | ctx->param->trust = trust; |
1486 | 0 | } |
1487 | 0 | return 1; |
1488 | 0 | } |
1489 | | |
1490 | 8.02k | X509_STORE_CTX *X509_STORE_CTX_new() { return New<X509_STORE_CTX>(); } |
1491 | | |
1492 | 8.02k | void X509_STORE_CTX_free(X509_STORE_CTX *ctx) { |
1493 | 8.02k | if (ctx == nullptr) { |
1494 | 0 | return; |
1495 | 0 | } |
1496 | 8.02k | X509_STORE_CTX_cleanup(ctx); |
1497 | 8.02k | Delete(ctx); |
1498 | 8.02k | } |
1499 | | |
1500 | | int X509_STORE_CTX_init(X509_STORE_CTX *ctx, X509_STORE *store, X509 *x509, |
1501 | 8.02k | STACK_OF(X509) *chain) { |
1502 | 8.02k | X509_STORE_CTX_cleanup(ctx); |
1503 | | |
1504 | 8.02k | ctx->ctx = store; |
1505 | 8.02k | ctx->cert = x509; |
1506 | 8.02k | ctx->untrusted = chain; |
1507 | | |
1508 | 8.02k | CRYPTO_new_ex_data(&ctx->ex_data); |
1509 | | |
1510 | 8.02k | if (store == nullptr) { |
1511 | 0 | OPENSSL_PUT_ERROR(X509, ERR_R_PASSED_NULL_PARAMETER); |
1512 | 0 | goto err; |
1513 | 0 | } |
1514 | | |
1515 | 8.02k | ctx->param = X509_VERIFY_PARAM_new(); |
1516 | 8.02k | if (!ctx->param) { |
1517 | 0 | goto err; |
1518 | 0 | } |
1519 | | |
1520 | 8.02k | { |
1521 | | // Inherit callbacks and flags from X509_STORE. |
1522 | | |
1523 | 8.02k | auto *store_impl = FromOpaque(store); |
1524 | 8.02k | ctx->verify_cb = store_impl->verify_cb; |
1525 | | |
1526 | 8.02k | if (!X509_VERIFY_PARAM_inherit(ctx->param, store_impl->param.get()) || |
1527 | 8.02k | !X509_VERIFY_PARAM_inherit(ctx->param, |
1528 | 8.02k | X509_VERIFY_PARAM_lookup("default"))) { |
1529 | 0 | goto err; |
1530 | 0 | } |
1531 | | |
1532 | 8.02k | if (store_impl->verify_cb) { |
1533 | 0 | ctx->verify_cb = store_impl->verify_cb; |
1534 | 8.02k | } else { |
1535 | 8.02k | ctx->verify_cb = null_callback; |
1536 | 8.02k | } |
1537 | 8.02k | } |
1538 | | |
1539 | 0 | return 1; |
1540 | | |
1541 | 0 | err: |
1542 | 0 | X509_STORE_CTX_cleanup(ctx); |
1543 | 0 | return 0; |
1544 | 8.02k | } |
1545 | | |
1546 | | // Set alternative lookup method: just a STACK of trusted certificates. This |
1547 | | // avoids X509_STORE nastiness where it isn't needed. |
1548 | | |
1549 | | void X509_STORE_CTX_set0_trusted_stack(X509_STORE_CTX *ctx, |
1550 | 0 | STACK_OF(X509) *sk) { |
1551 | 0 | ctx->trusted_stack = sk; |
1552 | 0 | } |
1553 | | |
1554 | 0 | void X509_STORE_CTX_trusted_stack(X509_STORE_CTX *ctx, STACK_OF(X509) *sk) { |
1555 | 0 | X509_STORE_CTX_set0_trusted_stack(ctx, sk); |
1556 | 0 | } |
1557 | | |
1558 | 16.0k | void X509_STORE_CTX_cleanup(X509_STORE_CTX *ctx) { |
1559 | 16.0k | CRYPTO_free_ex_data(&g_ex_data_class, &ctx->ex_data); |
1560 | 16.0k | X509_VERIFY_PARAM_free(ctx->param); |
1561 | 16.0k | sk_X509_pop_free(ctx->chain, X509_free); |
1562 | 16.0k | OPENSSL_memset(ctx, 0, sizeof(X509_STORE_CTX)); |
1563 | 16.0k | } |
1564 | | |
1565 | 0 | void X509_STORE_CTX_set_depth(X509_STORE_CTX *ctx, int depth) { |
1566 | 0 | X509_VERIFY_PARAM_set_depth(ctx->param, depth); |
1567 | 0 | } |
1568 | | |
1569 | 0 | void X509_STORE_CTX_set_flags(X509_STORE_CTX *ctx, unsigned long flags) { |
1570 | 0 | X509_VERIFY_PARAM_set_flags(ctx->param, flags); |
1571 | 0 | } |
1572 | | |
1573 | | void X509_STORE_CTX_set_time_posix(X509_STORE_CTX *ctx, unsigned long flags, |
1574 | 0 | int64_t t) { |
1575 | 0 | X509_VERIFY_PARAM_set_time_posix(ctx->param, t); |
1576 | 0 | } |
1577 | | |
1578 | | void X509_STORE_CTX_set_time(X509_STORE_CTX *ctx, unsigned long flags, |
1579 | 0 | time_t t) { |
1580 | 0 | X509_STORE_CTX_set_time_posix(ctx, flags, t); |
1581 | 0 | } |
1582 | | |
1583 | 0 | X509 *X509_STORE_CTX_get0_cert(const X509_STORE_CTX *ctx) { return ctx->cert; } |
1584 | | |
1585 | | void X509_STORE_CTX_set_verify_cb(X509_STORE_CTX *ctx, |
1586 | 0 | int (*verify_cb)(int, X509_STORE_CTX *)) { |
1587 | 0 | ctx->verify_cb = verify_cb; |
1588 | 0 | } |
1589 | | |
1590 | 8.02k | int X509_STORE_CTX_set_default(X509_STORE_CTX *ctx, const char *name) { |
1591 | 8.02k | const X509_VERIFY_PARAM *param = X509_VERIFY_PARAM_lookup(name); |
1592 | 8.02k | if (!param) { |
1593 | 0 | return 0; |
1594 | 0 | } |
1595 | 8.02k | return X509_VERIFY_PARAM_inherit(ctx->param, param); |
1596 | 8.02k | } |
1597 | | |
1598 | 8.02k | X509_VERIFY_PARAM *X509_STORE_CTX_get0_param(X509_STORE_CTX *ctx) { |
1599 | 8.02k | return ctx->param; |
1600 | 8.02k | } |
1601 | | |
1602 | 0 | void X509_STORE_CTX_set0_param(X509_STORE_CTX *ctx, X509_VERIFY_PARAM *param) { |
1603 | 0 | if (ctx->param) { |
1604 | 0 | X509_VERIFY_PARAM_free(ctx->param); |
1605 | 0 | } |
1606 | 0 | ctx->param = param; |
1607 | 0 | } |