/src/openssl/crypto/cmp/cmp_vfy.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2007-2025 The OpenSSL Project Authors. All Rights Reserved. |
3 | | * Copyright Nokia 2007-2020 |
4 | | * Copyright Siemens AG 2015-2020 |
5 | | * |
6 | | * Licensed under the Apache License 2.0 (the "License"). You may not use |
7 | | * this file except in compliance with the License. You can obtain a copy |
8 | | * in the file LICENSE in the source distribution or at |
9 | | * https://www.openssl.org/source/license.html |
10 | | */ |
11 | | |
12 | | /* CMP functions for PKIMessage checking */ |
13 | | |
14 | | #include "cmp_local.h" |
15 | | |
16 | | /* Verify a message protected by signature according to RFC section 5.1.3.3 */ |
17 | | static int verify_signature(const OSSL_CMP_CTX *cmp_ctx, |
18 | | const OSSL_CMP_MSG *msg, X509 *cert) |
19 | 3.80k | { |
20 | 3.80k | OSSL_CMP_PROTECTEDPART prot_part; |
21 | 3.80k | EVP_PKEY *pubkey = NULL; |
22 | 3.80k | BIO *bio; |
23 | 3.80k | int res = 0; |
24 | | |
25 | 3.80k | if (!ossl_assert(cmp_ctx != NULL && msg != NULL && cert != NULL)) |
26 | 0 | return 0; |
27 | | |
28 | 3.80k | bio = BIO_new(BIO_s_mem()); /* may be NULL */ |
29 | 3.80k | if (bio == NULL) |
30 | 0 | return 0; |
31 | | /* verify that keyUsage, if present, contains digitalSignature */ |
32 | 3.80k | if (!cmp_ctx->ignore_keyusage |
33 | 3.80k | && (X509_get_key_usage(cert) & X509v3_KU_DIGITAL_SIGNATURE) == 0) { |
34 | 512 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_KEY_USAGE_DIGITALSIGNATURE); |
35 | 512 | goto sig_err; |
36 | 512 | } |
37 | | |
38 | 3.29k | pubkey = X509_get_pubkey(cert); |
39 | 3.29k | if (pubkey == NULL) { |
40 | 1.43k | ERR_raise(ERR_LIB_CMP, CMP_R_FAILED_EXTRACTING_PUBKEY); |
41 | 1.43k | goto sig_err; |
42 | 1.43k | } |
43 | | |
44 | 1.86k | prot_part.header = msg->header; |
45 | 1.86k | prot_part.body = msg->body; |
46 | | |
47 | 1.86k | if (ASN1_item_verify_ex(ASN1_ITEM_rptr(OSSL_CMP_PROTECTEDPART), |
48 | 1.86k | msg->header->protectionAlg, msg->protection, |
49 | 1.86k | &prot_part, NULL, pubkey, cmp_ctx->libctx, |
50 | 1.86k | cmp_ctx->propq) |
51 | 1.86k | > 0) { |
52 | 59 | res = 1; |
53 | 59 | goto end; |
54 | 59 | } |
55 | | |
56 | 3.74k | sig_err: |
57 | 3.74k | res = ossl_x509_print_ex_brief(bio, cert, X509_FLAG_NO_EXTENSIONS); |
58 | 3.74k | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_VALIDATING_SIGNATURE); |
59 | 3.74k | if (res) |
60 | 3.74k | ERR_add_error_mem_bio("\n", bio); |
61 | 3.74k | res = 0; |
62 | | |
63 | 3.80k | end: |
64 | 3.80k | EVP_PKEY_free(pubkey); |
65 | 3.80k | BIO_free(bio); |
66 | | |
67 | 3.80k | return res; |
68 | 3.74k | } |
69 | | |
70 | | /* Verify a message protected with PBMAC */ |
71 | | static int verify_PBMAC(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg) |
72 | 1.72k | { |
73 | 1.72k | ASN1_BIT_STRING *protection = NULL; |
74 | 1.72k | int valid = 0; |
75 | | |
76 | | /* generate expected protection for the message */ |
77 | 1.72k | if ((protection = ossl_cmp_calc_protection(ctx, msg)) == NULL) |
78 | 1.20k | return 0; /* failed to generate protection string! */ |
79 | | |
80 | 516 | valid = msg->protection != NULL && msg->protection->length >= 0 |
81 | 516 | && msg->protection->type == protection->type |
82 | 516 | && msg->protection->length == protection->length |
83 | 370 | && CRYPTO_memcmp(msg->protection->data, protection->data, |
84 | 370 | protection->length) |
85 | 370 | == 0; |
86 | 516 | ASN1_BIT_STRING_free(protection); |
87 | 516 | if (!valid) |
88 | 516 | ERR_raise(ERR_LIB_CMP, CMP_R_WRONG_PBM_VALUE); |
89 | | |
90 | 516 | return valid; |
91 | 1.72k | } |
92 | | |
93 | | /*- |
94 | | * Attempt to validate certificate and path using any given store with trusted |
95 | | * certs (possibly including CRLs and a cert verification callback function) |
96 | | * and non-trusted intermediate certs from the given ctx. |
97 | | * |
98 | | * Returns 1 on successful validation and 0 otherwise. |
99 | | */ |
100 | | int OSSL_CMP_validate_cert_path(const OSSL_CMP_CTX *ctx, |
101 | | X509_STORE *trusted_store, X509 *cert) |
102 | 65 | { |
103 | 65 | int valid = 0; |
104 | 65 | X509_STORE_CTX *csc = NULL; |
105 | 65 | int err; |
106 | | |
107 | 65 | if (ctx == NULL || cert == NULL) { |
108 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
109 | 0 | return 0; |
110 | 0 | } |
111 | | |
112 | 65 | if (trusted_store == NULL) { |
113 | 65 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_TRUST_STORE); |
114 | 65 | return 0; |
115 | 65 | } |
116 | | |
117 | 0 | if ((csc = X509_STORE_CTX_new_ex(ctx->libctx, ctx->propq)) == NULL |
118 | 0 | || !X509_STORE_CTX_init(csc, trusted_store, |
119 | 0 | cert, ctx->untrusted)) |
120 | 0 | goto err; |
121 | | |
122 | 0 | valid = X509_verify_cert(csc) > 0; |
123 | | |
124 | | /* make sure suitable error is queued even if callback did not do */ |
125 | 0 | err = ERR_peek_last_error(); |
126 | 0 | if (!valid && ERR_GET_REASON(err) != CMP_R_POTENTIALLY_INVALID_CERTIFICATE) |
127 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_POTENTIALLY_INVALID_CERTIFICATE); |
128 | |
|
129 | 0 | err: |
130 | | /* directly output any fresh errors, needed for check_msg_find_cert() */ |
131 | 0 | OSSL_CMP_CTX_print_errors(ctx); |
132 | 0 | X509_STORE_CTX_free(csc); |
133 | 0 | return valid; |
134 | 0 | } |
135 | | |
136 | | static int verify_cb_cert(X509_STORE *ts, X509 *cert, int err) |
137 | 4.44k | { |
138 | 4.44k | X509_STORE_CTX_verify_cb verify_cb; |
139 | 4.44k | X509_STORE_CTX *csc; |
140 | 4.44k | int ok = 0; |
141 | | |
142 | 4.44k | if (ts == NULL || (verify_cb = X509_STORE_get_verify_cb(ts)) == NULL) |
143 | 4.44k | return ok; |
144 | 0 | if ((csc = X509_STORE_CTX_new()) != NULL |
145 | 0 | && X509_STORE_CTX_init(csc, ts, cert, NULL)) { |
146 | 0 | X509_STORE_CTX_set_error(csc, err); |
147 | 0 | X509_STORE_CTX_set_current_cert(csc, cert); |
148 | 0 | ok = (*verify_cb)(0, csc); |
149 | 0 | } |
150 | 0 | X509_STORE_CTX_free(csc); |
151 | 0 | return ok; |
152 | 4.44k | } |
153 | | |
154 | | /* Return 0 if expect_name != NULL and there is no matching actual_name */ |
155 | | static int check_name(const OSSL_CMP_CTX *ctx, int log_success, |
156 | | const char *actual_desc, const X509_NAME *actual_name, |
157 | | const char *expect_desc, const X509_NAME *expect_name) |
158 | 16.5k | { |
159 | 16.5k | char *str; |
160 | | |
161 | 16.5k | if (expect_name == NULL) |
162 | 5.15k | return 1; /* no expectation, thus trivially fulfilled */ |
163 | | |
164 | | /* make sure that a matching name is there */ |
165 | 11.4k | if (actual_name == NULL) { |
166 | 0 | ossl_cmp_log1(WARN, ctx, "missing %s", actual_desc); |
167 | 0 | return 0; |
168 | 0 | } |
169 | 11.4k | str = X509_NAME_oneline(actual_name, NULL, 0); |
170 | 11.4k | if (X509_NAME_cmp(actual_name, expect_name) == 0) { |
171 | 5.05k | if (log_success && str != NULL) |
172 | 5.05k | ossl_cmp_log3(INFO, ctx, " %s matches %s: %s", |
173 | 5.05k | actual_desc, expect_desc, str); |
174 | 5.05k | OPENSSL_free(str); |
175 | 5.05k | return 1; |
176 | 5.05k | } |
177 | | |
178 | 6.36k | if (str != NULL) |
179 | 6.36k | ossl_cmp_log2(INFO, ctx, " actual name in %s = %s", actual_desc, str); |
180 | 6.36k | OPENSSL_free(str); |
181 | 6.36k | if ((str = X509_NAME_oneline(expect_name, NULL, 0)) != NULL) |
182 | 6.36k | ossl_cmp_log2(INFO, ctx, " does not match %s = %s", expect_desc, str); |
183 | 6.36k | OPENSSL_free(str); |
184 | 6.36k | return 0; |
185 | 11.4k | } |
186 | | |
187 | | /* Return 0 if skid != NULL and there is no matching subject key ID in cert */ |
188 | | static int check_kid(const OSSL_CMP_CTX *ctx, |
189 | | const ASN1_OCTET_STRING *ckid, |
190 | | const ASN1_OCTET_STRING *skid) |
191 | 5.05k | { |
192 | 5.05k | char *str; |
193 | | |
194 | 5.05k | if (skid == NULL) |
195 | 1.85k | return 1; /* no expectation, thus trivially fulfilled */ |
196 | | |
197 | | /* make sure that the expected subject key identifier is there */ |
198 | 3.19k | if (ckid == NULL) { |
199 | 297 | ossl_cmp_warn(ctx, "missing Subject Key Identifier in certificate"); |
200 | 297 | return 0; |
201 | 297 | } |
202 | 2.90k | str = i2s_ASN1_OCTET_STRING(NULL, ckid); |
203 | 2.90k | if (ASN1_OCTET_STRING_cmp(ckid, skid) == 0) { |
204 | 2.74k | if (str != NULL) |
205 | 2.74k | ossl_cmp_log1(INFO, ctx, " subjectKID matches senderKID: %s", str); |
206 | 2.74k | OPENSSL_free(str); |
207 | 2.74k | return 1; |
208 | 2.74k | } |
209 | | |
210 | 161 | if (str != NULL) |
211 | 161 | ossl_cmp_log1(INFO, ctx, " cert Subject Key Identifier = %s", str); |
212 | 161 | OPENSSL_free(str); |
213 | 161 | if ((str = i2s_ASN1_OCTET_STRING(NULL, skid)) != NULL) |
214 | 161 | ossl_cmp_log1(INFO, ctx, " does not match senderKID = %s", str); |
215 | 161 | OPENSSL_free(str); |
216 | 161 | return 0; |
217 | 2.90k | } |
218 | | |
219 | | static int already_checked(const X509 *cert, |
220 | | const STACK_OF(X509) *already_checked) |
221 | 50.5k | { |
222 | 50.5k | int i; |
223 | | |
224 | 76.3k | for (i = sk_X509_num(already_checked /* may be NULL */); i > 0; i--) |
225 | 42.6k | if (X509_cmp(sk_X509_value(already_checked, i - 1), cert) == 0) |
226 | 16.8k | return 1; |
227 | 33.6k | return 0; |
228 | 50.5k | } |
229 | | |
230 | | /*- |
231 | | * Check if the given cert is acceptable as sender cert of the given message. |
232 | | * The subject DN must match, the subject key ID as well if present in the msg, |
233 | | * and the cert must be current (checked if ctx->trusted is not NULL). |
234 | | * Note that cert revocation etc. is checked by OSSL_CMP_validate_cert_path(). |
235 | | * |
236 | | * Returns 0 on error or not acceptable, else 1. |
237 | | */ |
238 | | static int cert_acceptable(const OSSL_CMP_CTX *ctx, |
239 | | const char *desc1, const char *desc2, X509 *cert, |
240 | | const STACK_OF(X509) *already_checked1, |
241 | | const STACK_OF(X509) *already_checked2, |
242 | | const OSSL_CMP_MSG *msg) |
243 | 5.15k | { |
244 | 5.15k | X509_STORE *ts = ctx->trusted; |
245 | 5.15k | int self_issued = X509_check_issued(cert, cert) == X509_V_OK; |
246 | 5.15k | char *str; |
247 | 5.15k | X509_VERIFY_PARAM *vpm = ts != NULL ? X509_STORE_get0_param(ts) : NULL; |
248 | 5.15k | int err; |
249 | | |
250 | 5.15k | ossl_cmp_log3(INFO, ctx, " considering %s%s %s with..", |
251 | 5.15k | self_issued ? "self-issued " : "", desc1, desc2); |
252 | 5.15k | if ((str = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0)) != NULL) |
253 | 5.15k | ossl_cmp_log1(INFO, ctx, " subject = %s", str); |
254 | 5.15k | OPENSSL_free(str); |
255 | 5.15k | if (!self_issued) { |
256 | 4.63k | str = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0); |
257 | 4.63k | if (str != NULL) |
258 | 4.63k | ossl_cmp_log1(INFO, ctx, " issuer = %s", str); |
259 | 4.63k | OPENSSL_free(str); |
260 | 4.63k | } |
261 | | |
262 | 5.15k | if (already_checked(cert, already_checked1) |
263 | 2.57k | || already_checked(cert, already_checked2)) { |
264 | 2.57k | ossl_cmp_info(ctx, " cert has already been checked"); |
265 | 2.57k | return 0; |
266 | 2.57k | } |
267 | | |
268 | 2.57k | if (!X509_check_certificate_times(vpm, cert, &err)) { |
269 | 2.31k | const char *message; |
270 | | |
271 | 2.31k | switch (err) { |
272 | 52 | case X509_V_ERR_CERT_NOT_YET_VALID: |
273 | 52 | message = "cert is not yet valid"; |
274 | 52 | break; |
275 | 874 | case X509_V_ERR_CERT_HAS_EXPIRED: |
276 | 874 | message = "cert has expired"; |
277 | 874 | break; |
278 | 950 | case X509_V_ERR_ERROR_IN_CERT_NOT_BEFORE_FIELD: |
279 | 950 | message = "cert has an invalid not before field"; |
280 | 950 | break; |
281 | 442 | case X509_V_ERR_ERROR_IN_CERT_NOT_AFTER_FIELD: |
282 | 442 | message = "cert has an invalid not after field"; |
283 | 442 | break; |
284 | 0 | default: |
285 | 0 | message = "cert is invalid for an unspecfied reason"; |
286 | 0 | break; |
287 | 2.31k | } |
288 | | |
289 | 2.31k | ossl_cmp_warn(ctx, message); |
290 | 2.31k | if (ctx->log_cb != NULL /* logging not temporarily disabled */ |
291 | 1.15k | && verify_cb_cert(ts, cert, err) <= 0) |
292 | 1.15k | return 0; |
293 | 2.31k | } |
294 | | |
295 | 1.41k | if (!check_name(ctx, 1, |
296 | 1.41k | "cert subject", X509_get_subject_name(cert), |
297 | 1.41k | "sender field", msg->header->sender->d.directoryName)) |
298 | 852 | return 0; |
299 | | |
300 | 567 | if (!check_kid(ctx, X509_get0_subject_key_id(cert), msg->header->senderKID)) |
301 | 40 | return 0; |
302 | | /* prevent misleading error later in case x509v3_cache_extensions() fails */ |
303 | 527 | if (!ossl_x509v3_cache_extensions(cert)) { |
304 | 27 | ossl_cmp_warn(ctx, "cert appears to be invalid"); |
305 | 27 | return 0; |
306 | 27 | } |
307 | 500 | if (!verify_signature(ctx, msg, cert)) { |
308 | 490 | ossl_cmp_warn(ctx, "msg signature verification failed"); |
309 | 490 | return 0; |
310 | 490 | } |
311 | | /* acceptable also if there is no senderKID in msg header */ |
312 | 10 | ossl_cmp_info(ctx, " cert seems acceptable"); |
313 | 10 | return 1; |
314 | 500 | } |
315 | | |
316 | | static int check_cert_path(const OSSL_CMP_CTX *ctx, X509_STORE *store, |
317 | | X509 *scrt) |
318 | 65 | { |
319 | 65 | if (OSSL_CMP_validate_cert_path(ctx, store, scrt)) |
320 | 0 | return 1; |
321 | | |
322 | 65 | ossl_cmp_warn(ctx, |
323 | 65 | "msg signature validates but cert path validation failed"); |
324 | 65 | return 0; |
325 | 65 | } |
326 | | |
327 | | /* |
328 | | * Exceptional handling for 3GPP TS 33.310 [3G/LTE Network Domain Security |
329 | | * (NDS); Authentication Framework (AF)], only to use for IP messages |
330 | | * and if the ctx option is explicitly set: use self-issued certificates from |
331 | | * extraCerts as trust anchors when validating the CMP message protection cert |
332 | | * in this and any subsequent responses from the server in the same transaction, |
333 | | * but only if these extraCerts can also be used as trust anchors for validating |
334 | | * the newly enrolled certificate received in the IP message. |
335 | | */ |
336 | | static int check_cert_path_3gpp(const OSSL_CMP_CTX *ctx, |
337 | | const OSSL_CMP_MSG *msg, X509 *scrt) |
338 | 0 | { |
339 | 0 | int valid = 0; |
340 | 0 | X509_STORE *store; |
341 | 0 | STACK_OF(X509) *extraCerts; |
342 | |
|
343 | 0 | if (!ctx->permitTAInExtraCertsForIR) |
344 | 0 | return 0; |
345 | | |
346 | | /* |
347 | | * Initially, use extraCerts from the IP message. |
348 | | * For subsequent msgs (pollRep or PKIConf) in the same transaction, |
349 | | * use extraCertsIn remembered from earlier message (typically, the IP message). |
350 | | * The extraCertsIn field will be cleared by OSSL_CMP_CTX_reinit(). |
351 | | */ |
352 | 0 | extraCerts = ctx->extraCertsIn == NULL ? msg->extraCerts : ctx->extraCertsIn; |
353 | 0 | if ((store = X509_STORE_new()) == NULL |
354 | 0 | || !ossl_cmp_X509_STORE_add1_certs(store, extraCerts, |
355 | 0 | 1 /* self-issued only */)) |
356 | 0 | goto err; |
357 | | |
358 | | /* store does not include CRLs */ |
359 | 0 | valid = OSSL_CMP_validate_cert_path(ctx, store, scrt); |
360 | 0 | if (!valid) { |
361 | 0 | ossl_cmp_warn(ctx, |
362 | 0 | "also exceptional 3GPP mode cert path validation failed"); |
363 | 0 | } else if (OSSL_CMP_MSG_get_bodytype(msg) == OSSL_CMP_PKIBODY_IP) { |
364 | | /* |
365 | | * verify that the newly enrolled certificate (which assumed rid == |
366 | | * OSSL_CMP_CERTREQID) can also be validated with the same trusted store |
367 | | */ |
368 | 0 | OSSL_CMP_CERTRESPONSE *crep = ossl_cmp_certrepmessage_get0_certresponse(msg->body->value.ip, |
369 | 0 | OSSL_CMP_CERTREQID); |
370 | 0 | X509 *newcrt = NULL; |
371 | |
|
372 | 0 | valid = crep != NULL |
373 | 0 | && (newcrt = ossl_cmp_certresponse_get1_cert(ctx, crep)) != NULL |
374 | 0 | && OSSL_CMP_validate_cert_path(ctx, store, newcrt); |
375 | 0 | X509_free(newcrt); |
376 | 0 | } |
377 | |
|
378 | 0 | err: |
379 | 0 | X509_STORE_free(store); |
380 | 0 | return valid; |
381 | 0 | } |
382 | | |
383 | | static int check_msg_given_cert(const OSSL_CMP_CTX *ctx, X509 *cert, |
384 | | const OSSL_CMP_MSG *msg) |
385 | 0 | { |
386 | 0 | return cert_acceptable(ctx, "previously validated", "sender cert", |
387 | 0 | cert, NULL, NULL, msg) |
388 | 0 | && (check_cert_path(ctx, ctx->trusted, cert) |
389 | 0 | || check_cert_path_3gpp(ctx, msg, cert)); |
390 | 0 | } |
391 | | |
392 | | /*- |
393 | | * Try all certs in given list for verifying msg, normally or in 3GPP mode. |
394 | | * If already_checked1 == NULL then certs are assumed to be the msg->extraCerts. |
395 | | * On success cache the found cert using ossl_cmp_ctx_set1_validatedSrvCert(). |
396 | | */ |
397 | | static int check_msg_with_certs(OSSL_CMP_CTX *ctx, const STACK_OF(X509) *certs, |
398 | | const char *desc, |
399 | | const STACK_OF(X509) *already_checked1, |
400 | | const STACK_OF(X509) *already_checked2, |
401 | | const OSSL_CMP_MSG *msg, int mode_3gpp) |
402 | 15.6k | { |
403 | 15.6k | int in_extraCerts = already_checked1 == NULL; |
404 | 15.6k | int n_acceptable_certs = 0; |
405 | 15.6k | int i; |
406 | | |
407 | 15.6k | if (sk_X509_num(certs) <= 0) { |
408 | 2.39k | ossl_cmp_log1(WARN, ctx, "no %s", desc); |
409 | 2.39k | return 0; |
410 | 2.39k | } |
411 | | |
412 | 42.5k | for (i = 0; i < sk_X509_num(certs); i++) { /* certs may be NULL */ |
413 | 29.2k | X509 *cert = sk_X509_value(certs, i); |
414 | | |
415 | 29.2k | if (!ossl_assert(cert != NULL)) |
416 | 0 | return 0; |
417 | 29.2k | if (!cert_acceptable(ctx, "cert from", desc, cert, |
418 | 29.2k | already_checked1, already_checked2, msg)) |
419 | 29.2k | continue; |
420 | 59 | n_acceptable_certs++; |
421 | 59 | if (mode_3gpp ? check_cert_path_3gpp(ctx, msg, cert) |
422 | 59 | : check_cert_path(ctx, ctx->trusted, cert)) { |
423 | | /* store successful sender cert for further msgs in transaction */ |
424 | 0 | return ossl_cmp_ctx_set1_validatedSrvCert(ctx, cert); |
425 | 0 | } |
426 | 59 | } |
427 | 13.2k | if (in_extraCerts && n_acceptable_certs == 0) |
428 | 13.2k | ossl_cmp_warn(ctx, "no acceptable cert in extraCerts"); |
429 | 13.2k | return 0; |
430 | 13.2k | } |
431 | | |
432 | | /*- |
433 | | * Verify msg trying first ctx->untrusted, which should include extraCerts |
434 | | * at its front, then trying the trusted certs in truststore (if any) of ctx. |
435 | | * On success cache the found cert using ossl_cmp_ctx_set1_validatedSrvCert(). |
436 | | */ |
437 | | static int check_msg_all_certs(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg, |
438 | | int mode_3gpp) |
439 | 15.6k | { |
440 | 15.6k | int ret = 0; |
441 | | |
442 | 15.6k | if (ctx->permitTAInExtraCertsForIR |
443 | 0 | && OSSL_CMP_MSG_get_bodytype(msg) == OSSL_CMP_PKIBODY_IP) |
444 | 15.6k | ossl_cmp_info(ctx, mode_3gpp ? "normal mode failed; trying now 3GPP mode trusting extraCerts" : "trying first normal mode using trust store"); |
445 | 15.6k | else if (mode_3gpp) |
446 | 7.83k | return 0; |
447 | | |
448 | 7.83k | if (check_msg_with_certs(ctx, msg->extraCerts, "extraCerts", |
449 | 7.83k | NULL, NULL, msg, mode_3gpp)) |
450 | 0 | return 1; |
451 | 7.83k | if (check_msg_with_certs(ctx, ctx->untrusted, "untrusted certs", |
452 | 7.83k | msg->extraCerts, NULL, msg, mode_3gpp)) |
453 | 0 | return 1; |
454 | | |
455 | 7.83k | if (ctx->trusted == NULL) { |
456 | 7.83k | ossl_cmp_warn(ctx, mode_3gpp ? "no self-issued extraCerts" : "no trusted store"); |
457 | 7.83k | } else { |
458 | 0 | STACK_OF(X509) *trusted = X509_STORE_get1_all_certs(ctx->trusted); |
459 | |
|
460 | 0 | ret = check_msg_with_certs(ctx, trusted, |
461 | 0 | mode_3gpp ? "self-issued extraCerts" |
462 | 0 | : "certs in trusted store", |
463 | 0 | msg->extraCerts, ctx->untrusted, |
464 | 0 | msg, mode_3gpp); |
465 | 0 | OSSL_STACK_OF_X509_free(trusted); |
466 | 0 | } |
467 | 7.83k | return ret; |
468 | 7.83k | } |
469 | | |
470 | | /*- |
471 | | * Verify message signature with any acceptable and valid candidate cert. |
472 | | * On success cache the found cert using ossl_cmp_ctx_set1_validatedSrvCert(). |
473 | | */ |
474 | | static int check_msg_find_cert(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg) |
475 | 4.43k | { |
476 | 4.43k | X509 *scrt = ctx->validatedSrvCert; /* previous successful sender cert */ |
477 | 4.43k | GENERAL_NAME *sender = msg->header->sender; |
478 | 4.43k | char *sname = NULL; |
479 | 4.43k | char *skid_str = NULL; |
480 | 4.43k | const ASN1_OCTET_STRING *skid = msg->header->senderKID; |
481 | 4.43k | OSSL_CMP_log_cb_t backup_log_cb = ctx->log_cb; |
482 | 4.43k | int res = 0; |
483 | | |
484 | 4.43k | if (sender == NULL || msg->body == NULL) |
485 | 0 | return 0; /* other NULL cases already have been checked */ |
486 | 4.43k | if (sender->type != GEN_DIRNAME) { |
487 | | /* So far, only X509_NAME is supported */ |
488 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED); |
489 | 0 | return 0; |
490 | 0 | } |
491 | | |
492 | | /* dump any hitherto errors to avoid confusion when printing further ones */ |
493 | 4.43k | OSSL_CMP_CTX_print_errors(ctx); |
494 | | |
495 | | /* enable clearing irrelevant errors in attempts to validate sender certs */ |
496 | 4.43k | (void)ERR_set_mark(); |
497 | 4.43k | ctx->log_cb = NULL; /* temporarily disable logging */ |
498 | | |
499 | | /* |
500 | | * try first cached scrt, used successfully earlier in same transaction, |
501 | | * for validating this and any further msgs where extraCerts may be left out |
502 | | */ |
503 | 4.43k | if (scrt != NULL) { |
504 | 0 | if (check_msg_given_cert(ctx, scrt, msg)) { |
505 | 0 | ctx->log_cb = backup_log_cb; |
506 | 0 | (void)ERR_pop_to_mark(); |
507 | 0 | return 1; |
508 | 0 | } |
509 | | /* cached sender cert has shown to be no more successfully usable */ |
510 | 0 | (void)ossl_cmp_ctx_set1_validatedSrvCert(ctx, NULL); |
511 | | /* re-do the above check (just) for adding diagnostic information */ |
512 | 0 | ossl_cmp_info(ctx, |
513 | 0 | "trying to verify msg signature with previously validated cert"); |
514 | 0 | (void)check_msg_given_cert(ctx, scrt, msg); |
515 | 0 | } |
516 | | |
517 | 4.43k | res = check_msg_all_certs(ctx, msg, 0 /* using ctx->trusted */) |
518 | 4.43k | || check_msg_all_certs(ctx, msg, 1 /* 3gpp */); |
519 | 4.43k | ctx->log_cb = backup_log_cb; |
520 | 4.43k | if (res) { |
521 | | /* discard any diagnostic information on trying to use certs */ |
522 | 0 | (void)ERR_pop_to_mark(); |
523 | 0 | goto end; |
524 | 0 | } |
525 | | /* failed finding a sender cert that verifies the message signature */ |
526 | 4.43k | (void)ERR_clear_last_mark(); |
527 | | |
528 | 4.43k | sname = X509_NAME_oneline(sender->d.directoryName, NULL, 0); |
529 | 4.43k | skid_str = skid == NULL ? NULL : i2s_ASN1_OCTET_STRING(NULL, skid); |
530 | 4.43k | if (ctx->log_cb != NULL) { |
531 | 4.43k | ossl_cmp_info(ctx, "trying to verify msg signature with a valid cert that.."); |
532 | 4.43k | if (sname != NULL) |
533 | 4.43k | ossl_cmp_log1(INFO, ctx, "matches msg sender = %s", sname); |
534 | 4.43k | if (skid_str != NULL) |
535 | 2.60k | ossl_cmp_log1(INFO, ctx, "matches msg senderKID = %s", skid_str); |
536 | 1.83k | else |
537 | 4.43k | ossl_cmp_info(ctx, "while msg header does not contain senderKID"); |
538 | | /* re-do the above checks (just) for adding diagnostic information */ |
539 | 4.43k | (void)check_msg_all_certs(ctx, msg, 0 /* using ctx->trusted */); |
540 | 4.43k | (void)check_msg_all_certs(ctx, msg, 1 /* 3gpp */); |
541 | 4.43k | } |
542 | | |
543 | 4.43k | ERR_raise(ERR_LIB_CMP, CMP_R_NO_SUITABLE_SENDER_CERT); |
544 | 4.43k | if (sname != NULL) { |
545 | 4.43k | ERR_add_error_txt(NULL, "for msg sender name = "); |
546 | 4.43k | ERR_add_error_txt(NULL, sname); |
547 | 4.43k | } |
548 | 4.43k | if (skid_str != NULL) { |
549 | 2.60k | ERR_add_error_txt(" and ", "for msg senderKID = "); |
550 | 2.60k | ERR_add_error_txt(NULL, skid_str); |
551 | 2.60k | } |
552 | | |
553 | 4.43k | end: |
554 | 4.43k | OPENSSL_free(sname); |
555 | 4.43k | OPENSSL_free(skid_str); |
556 | 4.43k | return res; |
557 | 4.43k | } |
558 | | |
559 | | /*- |
560 | | * Validate the protection of the given PKIMessage using either password- |
561 | | * based mac (PBM) or a signature algorithm. In the case of signature algorithm, |
562 | | * the sender certificate can have been pinned by providing it in ctx->srvCert, |
563 | | * else it is searched in msg->extraCerts, ctx->untrusted, in ctx->trusted |
564 | | * (in this order) and is path is validated against ctx->trusted. |
565 | | * On success cache the found cert using ossl_cmp_ctx_set1_validatedSrvCert(). |
566 | | * |
567 | | * If ctx->permitTAInExtraCertsForIR is true, when validating a CMP IP message, |
568 | | * trust anchors for validating the IP message (and any subsequent responses |
569 | | * by the server in the same transaction) may be taken from msg->extraCerts |
570 | | * if self-issued certificates are found there that can also be used |
571 | | * to validate the newly enrolled certificate returned in the IP msg. |
572 | | * This is according to the need given in 3GPP TS 33.310. |
573 | | * |
574 | | * Returns 1 on success, 0 on error or validation failed. |
575 | | */ |
576 | | int OSSL_CMP_validate_msg(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg) |
577 | 8.08k | { |
578 | 8.08k | X509 *scrt; |
579 | | |
580 | 8.08k | ossl_cmp_debug(ctx, "validating CMP message"); |
581 | 8.08k | if (ctx == NULL || msg == NULL |
582 | 8.08k | || msg->header == NULL || msg->body == NULL) { |
583 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_NULL_ARGUMENT); |
584 | 0 | return 0; |
585 | 0 | } |
586 | | |
587 | 8.08k | if (msg->header->protectionAlg == NULL /* unprotected message */ |
588 | 8.08k | || msg->protection == NULL || msg->protection->data == NULL) { |
589 | 2.36k | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PROTECTION); |
590 | 2.36k | return 0; |
591 | 2.36k | } |
592 | | |
593 | 5.72k | switch (ossl_cmp_hdr_get_protection_nid(msg->header)) { |
594 | | /* 5.1.3.1. Shared Secret Information */ |
595 | 1.61k | case NID_id_PasswordBasedMAC: |
596 | 1.61k | if (ctx->secretValue == NULL) { |
597 | 748 | ossl_cmp_info(ctx, "no secret available for verifying PBM-based CMP message protection"); |
598 | 748 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_SECRET); |
599 | 748 | return 0; |
600 | 748 | } |
601 | 868 | if (verify_PBMAC(ctx, msg)) { |
602 | | /* |
603 | | * RFC 9810, 5.3.2: 'Note that if the PKI message protection is |
604 | | * "shared secret information", then any certificate transported in |
605 | | * the caPubs field may be directly trusted as a root CA |
606 | | * certificate by the initiator.' |
607 | | */ |
608 | 6 | switch (OSSL_CMP_MSG_get_bodytype(msg)) { |
609 | 0 | case -1: |
610 | 0 | return 0; |
611 | 0 | case OSSL_CMP_PKIBODY_IP: |
612 | 0 | case OSSL_CMP_PKIBODY_CP: |
613 | 0 | case OSSL_CMP_PKIBODY_KUP: |
614 | 3 | case OSSL_CMP_PKIBODY_CCP: |
615 | 3 | if (ctx->trusted != NULL) { |
616 | 0 | STACK_OF(X509) *certs = msg->body->value.ip->caPubs; |
617 | | /* value.ip is same for cp, kup, and ccp */ |
618 | |
|
619 | 0 | if (!ossl_cmp_X509_STORE_add1_certs(ctx->trusted, certs, 0)) |
620 | | /* adds both self-issued and not self-issued certs */ |
621 | 0 | return 0; |
622 | 0 | } |
623 | 3 | break; |
624 | 3 | default: |
625 | 3 | break; |
626 | 6 | } |
627 | 6 | ossl_cmp_debug(ctx, |
628 | 6 | "successfully validated PBM-based CMP message protection"); |
629 | 6 | return 1; |
630 | 6 | } |
631 | 862 | ossl_cmp_warn(ctx, "verifying PBM-based CMP message protection failed"); |
632 | 862 | break; |
633 | | |
634 | | /* |
635 | | * 5.1.3.2 DH Key Pairs |
636 | | * Not yet supported |
637 | | */ |
638 | 24 | case NID_id_DHBasedMac: |
639 | 24 | ERR_raise(ERR_LIB_CMP, CMP_R_UNSUPPORTED_PROTECTION_ALG_DHBASEDMAC); |
640 | 24 | break; |
641 | | |
642 | | /* |
643 | | * 5.1.3.3. Signature |
644 | | */ |
645 | 4.08k | default: |
646 | 4.08k | scrt = ctx->srvCert; |
647 | 4.08k | if (scrt == NULL) { |
648 | 4.08k | if (ctx->trusted == NULL && ctx->secretValue != NULL) { |
649 | 1.65k | ossl_cmp_info(ctx, "no trust store nor pinned server cert available for verifying signature-based CMP message protection"); |
650 | 1.65k | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_TRUST_ANCHOR); |
651 | 1.65k | return 0; |
652 | 1.65k | } |
653 | 2.43k | if (check_msg_find_cert(ctx, msg)) { |
654 | 0 | ossl_cmp_log1(DEBUG, ctx, |
655 | 0 | "successfully validated signature-based CMP message protection using trust store%s", |
656 | 0 | ctx->permitTAInExtraCertsForIR ? " or 3GPP mode" : ""); |
657 | 0 | return 1; |
658 | 0 | } |
659 | 2.43k | } else { /* use pinned sender cert */ |
660 | | /* use ctx->srvCert for signature check even if not acceptable */ |
661 | 0 | if (verify_signature(ctx, msg, scrt)) { |
662 | 0 | ossl_cmp_debug(ctx, |
663 | 0 | "successfully validated signature-based CMP message protection using pinned server cert"); |
664 | 0 | return ossl_cmp_ctx_set1_validatedSrvCert(ctx, scrt); |
665 | 0 | } |
666 | 0 | ossl_cmp_warn(ctx, "CMP message signature verification failed"); |
667 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_SRVCERT_DOES_NOT_VALIDATE_MSG); |
668 | 0 | } |
669 | 2.43k | break; |
670 | 5.72k | } |
671 | 3.31k | return 0; |
672 | 5.72k | } |
673 | | |
674 | | static int check_transactionID_or_nonce(ASN1_OCTET_STRING *expected, |
675 | | ASN1_OCTET_STRING *actual, int reason) |
676 | 81.4k | { |
677 | 81.4k | if (expected != NULL |
678 | 0 | && (actual == NULL || ASN1_OCTET_STRING_cmp(expected, actual) != 0)) { |
679 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
680 | | char *expected_str, *actual_str; |
681 | | |
682 | | expected_str = i2s_ASN1_OCTET_STRING(NULL, expected); |
683 | | actual_str = actual == NULL ? NULL : i2s_ASN1_OCTET_STRING(NULL, actual); |
684 | | ERR_raise_data(ERR_LIB_CMP, reason, |
685 | | "expected = %s, actual = %s", |
686 | | expected_str == NULL ? "?" : expected_str, |
687 | | actual == NULL ? "(none)" : actual_str == NULL ? "?" |
688 | | : actual_str); |
689 | | OPENSSL_free(expected_str); |
690 | | OPENSSL_free(actual_str); |
691 | | return 0; |
692 | | #endif |
693 | 0 | } |
694 | 81.4k | return 1; |
695 | 81.4k | } |
696 | | |
697 | | /*- |
698 | | * Check received message (i.e., response by server or request from client) |
699 | | * Any msg->extraCerts are prepended to ctx->untrusted. |
700 | | * |
701 | | * Ensures that: |
702 | | * its sender is of appropriate type (currently only X509_NAME) and |
703 | | * matches any expected sender or srvCert subject given in the ctx |
704 | | * it has a valid body type |
705 | | * its protection is valid (or invalid/absent, but only if a callback function |
706 | | * is present and yields a positive result using also the supplied argument) |
707 | | * its transaction ID matches the previous transaction ID stored in ctx (if any) |
708 | | * its recipNonce matches the previous senderNonce stored in the ctx (if any) |
709 | | * |
710 | | * If everything is fine: |
711 | | * learns the senderNonce from the received message, |
712 | | * learns the transaction ID if it is not yet in ctx, |
713 | | * and makes any certs in caPubs directly trusted. |
714 | | * |
715 | | * Returns 1 on success, 0 on error. |
716 | | */ |
717 | | int ossl_cmp_msg_check_update(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg, |
718 | | ossl_cmp_allow_unprotected_cb_t cb, int cb_arg) |
719 | 8.16k | { |
720 | 8.16k | OSSL_CMP_PKIHEADER *hdr; |
721 | 8.16k | const X509_NAME *expected_sender; |
722 | 8.16k | int num_untrusted, num_added, res; |
723 | | |
724 | 8.16k | if (!ossl_assert(ctx != NULL && msg != NULL && msg->header != NULL)) |
725 | 0 | return 0; |
726 | 8.16k | hdr = OSSL_CMP_MSG_get0_header(msg); |
727 | | |
728 | | /* If expected_sender is given, validate sender name of received msg */ |
729 | 8.16k | expected_sender = ctx->expected_sender; |
730 | 8.16k | if (expected_sender == NULL && ctx->srvCert != NULL) |
731 | 0 | expected_sender = X509_get_subject_name(ctx->srvCert); |
732 | 8.16k | if (expected_sender != NULL) { |
733 | 0 | const X509_NAME *actual_sender; |
734 | 0 | char *str; |
735 | |
|
736 | 0 | if (hdr->sender == NULL) { |
737 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_SENDER_IDENTIFICATION); |
738 | 0 | return 0; |
739 | 0 | } |
740 | | |
741 | 0 | if (hdr->sender->type != GEN_DIRNAME) { |
742 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED); |
743 | 0 | return 0; |
744 | 0 | } |
745 | 0 | actual_sender = hdr->sender->d.directoryName; |
746 | | /* |
747 | | * Compare actual sender name of response with expected sender name. |
748 | | * Mitigates risk of accepting misused PBM secret or |
749 | | * misused certificate of an unauthorized entity of a trusted hierarchy. |
750 | | */ |
751 | 0 | if (!check_name(ctx, 0, "sender DN field", actual_sender, |
752 | 0 | "expected sender", expected_sender)) { |
753 | 0 | str = X509_NAME_oneline(actual_sender, NULL, 0); |
754 | 0 | ERR_raise_data(ERR_LIB_CMP, CMP_R_UNEXPECTED_SENDER, |
755 | 0 | str != NULL ? str : "<unknown>"); |
756 | 0 | OPENSSL_free(str); |
757 | 0 | return 0; |
758 | 0 | } |
759 | 0 | } |
760 | | /* Note: if recipient was NULL-DN it could be learned here if needed */ |
761 | | |
762 | 8.16k | num_added = sk_X509_num(msg->extraCerts); |
763 | 8.16k | if (num_added > 10) |
764 | 0 | ossl_cmp_log1(WARN, ctx, "received CMP message contains %d extraCerts", |
765 | 8.16k | num_added); |
766 | | /* |
767 | | * Store any provided extraCerts in ctx for use in OSSL_CMP_validate_msg() |
768 | | * and for future use, such that they are available to ctx->certConf_cb and |
769 | | * the peer does not need to send them again in the same transaction. |
770 | | * Note that it does not help validating the message before storing the |
771 | | * extraCerts because they do not belong to the protected msg part anyway. |
772 | | * The extraCerts are prepended. Allows simple removal if they shall not be |
773 | | * cached. Also they get used first, which is likely good for efficiency. |
774 | | */ |
775 | 8.16k | num_untrusted = ctx->untrusted == NULL ? 0 : sk_X509_num(ctx->untrusted); |
776 | 8.16k | res = ossl_x509_add_certs_new(&ctx->untrusted, msg->extraCerts, |
777 | | /* this allows self-signed certs */ |
778 | 8.16k | X509_ADD_FLAG_UP_REF | X509_ADD_FLAG_NO_DUP |
779 | 8.16k | | X509_ADD_FLAG_PREPEND); |
780 | 8.16k | num_added = (ctx->untrusted == NULL ? 0 : sk_X509_num(ctx->untrusted)) |
781 | 8.16k | - num_untrusted; |
782 | 8.16k | if (!res) { |
783 | 0 | while (num_added-- > 0) |
784 | 0 | X509_free(sk_X509_shift(ctx->untrusted)); |
785 | 0 | return 0; |
786 | 0 | } |
787 | | |
788 | 8.16k | if (hdr->protectionAlg != NULL) |
789 | 1.35k | res = OSSL_CMP_validate_msg(ctx, msg) |
790 | | /* explicitly permitted exceptions for invalid protection: */ |
791 | 1.35k | || (cb != NULL && (*cb)(ctx, msg, 1, cb_arg) > 0); |
792 | 6.80k | else |
793 | | /* explicitly permitted exceptions for missing protection: */ |
794 | 6.80k | res = cb != NULL && (*cb)(ctx, msg, 0, cb_arg) > 0; |
795 | 8.16k | #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
796 | 8.16k | res = 1; /* support more aggressive fuzzing by letting invalid msg pass */ |
797 | 8.16k | #endif |
798 | | |
799 | | /* remove extraCerts again if not caching */ |
800 | 8.16k | if (ctx->noCacheExtraCerts) |
801 | 0 | while (num_added-- > 0) |
802 | 0 | X509_free(sk_X509_shift(ctx->untrusted)); |
803 | | |
804 | 8.16k | if (!res) { |
805 | 0 | if (hdr->protectionAlg != NULL) |
806 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_VALIDATING_PROTECTION); |
807 | 0 | else |
808 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_MISSING_PROTECTION); |
809 | 0 | return 0; |
810 | 0 | } |
811 | | |
812 | | /* check CMP version number in header */ |
813 | 8.16k | if (ossl_cmp_hdr_get_pvno(hdr) != OSSL_CMP_PVNO_2 |
814 | 6.95k | && ossl_cmp_hdr_get_pvno(hdr) != OSSL_CMP_PVNO_3) { |
815 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
816 | | ERR_raise(ERR_LIB_CMP, CMP_R_UNEXPECTED_PVNO); |
817 | | return 0; |
818 | | #endif |
819 | 6.95k | } |
820 | | |
821 | 8.16k | if (OSSL_CMP_MSG_get_bodytype(msg) < 0) { |
822 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
823 | | ERR_raise(ERR_LIB_CMP, CMP_R_PKIBODY_ERROR); |
824 | | return 0; |
825 | | #endif |
826 | 0 | } |
827 | | |
828 | | /* compare received transactionID with the expected one in previous msg */ |
829 | 8.16k | if (!check_transactionID_or_nonce(ctx->transactionID, hdr->transactionID, |
830 | 8.16k | CMP_R_TRANSACTIONID_UNMATCHED)) |
831 | 0 | return 0; |
832 | | |
833 | | /* |
834 | | * enable clearing irrelevant errors |
835 | | * in attempts to validate recipient nonce in case of delayed delivery. |
836 | | */ |
837 | 8.16k | (void)ERR_set_mark(); |
838 | | /* compare received nonce with the one we sent */ |
839 | 8.16k | if (!check_transactionID_or_nonce(ctx->senderNonce, hdr->recipNonce, |
840 | 8.16k | CMP_R_RECIPNONCE_UNMATCHED)) { |
841 | | /* check if we are polling and received final response */ |
842 | 0 | if (ctx->first_senderNonce == NULL |
843 | 0 | || OSSL_CMP_MSG_get_bodytype(msg) == OSSL_CMP_PKIBODY_POLLREP |
844 | | /* compare received nonce with our sender nonce at poll start */ |
845 | 0 | || !check_transactionID_or_nonce(ctx->first_senderNonce, |
846 | 0 | hdr->recipNonce, |
847 | 0 | CMP_R_RECIPNONCE_UNMATCHED)) { |
848 | 0 | (void)ERR_clear_last_mark(); |
849 | 0 | return 0; |
850 | 0 | } |
851 | 0 | } |
852 | 8.16k | (void)ERR_pop_to_mark(); |
853 | | |
854 | | /* if not yet present, learn transactionID */ |
855 | 8.16k | if (ctx->transactionID == NULL |
856 | 8.16k | && !OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID)) |
857 | 0 | return 0; |
858 | | |
859 | | /* |
860 | | * RFC 9810 section 5.1.1 states: the recipNonce is copied from |
861 | | * the senderNonce of the previous message in the transaction. |
862 | | * --> Store for setting in next message |
863 | | */ |
864 | 8.16k | if (!ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce)) |
865 | 0 | return 0; |
866 | | |
867 | 8.16k | if (ossl_cmp_hdr_get_protection_nid(hdr) == NID_id_PasswordBasedMAC) { |
868 | | /* |
869 | | * RFC 9810, 5.3.2: 'Note that if the PKI message protection is |
870 | | * "shared secret information", then any certificate transported in |
871 | | * the caPubs field may be directly trusted as a root CA |
872 | | * certificate by the initiator.' |
873 | | */ |
874 | 453 | switch (OSSL_CMP_MSG_get_bodytype(msg)) { |
875 | 2 | case OSSL_CMP_PKIBODY_IP: |
876 | 6 | case OSSL_CMP_PKIBODY_CP: |
877 | 7 | case OSSL_CMP_PKIBODY_KUP: |
878 | 13 | case OSSL_CMP_PKIBODY_CCP: |
879 | 13 | if (ctx->trusted != NULL) { |
880 | 0 | STACK_OF(X509) *certs = msg->body->value.ip->caPubs; |
881 | | /* value.ip is same for cp, kup, and ccp */ |
882 | |
|
883 | 0 | if (!ossl_cmp_X509_STORE_add1_certs(ctx->trusted, certs, 0)) |
884 | | /* adds both self-issued and not self-issued certs */ |
885 | 0 | return 0; |
886 | 0 | } |
887 | 13 | break; |
888 | 440 | default: |
889 | 440 | break; |
890 | 453 | } |
891 | 453 | } |
892 | 8.16k | return 1; |
893 | 8.16k | } |
894 | | |
895 | | int ossl_cmp_verify_popo(const OSSL_CMP_CTX *ctx, |
896 | | const OSSL_CMP_MSG *msg, int acceptRAVerified) |
897 | 6.45k | { |
898 | 6.45k | if (!ossl_assert(msg != NULL && msg->body != NULL)) |
899 | 0 | return 0; |
900 | 6.45k | switch (msg->body->type) { |
901 | 3.81k | case OSSL_CMP_PKIBODY_P10CR: { |
902 | 3.81k | X509_REQ *req = msg->body->value.p10cr; |
903 | | |
904 | 3.81k | if (X509_REQ_verify_ex(req, X509_REQ_get0_pubkey(req), ctx->libctx, |
905 | 3.81k | ctx->propq) |
906 | 3.81k | <= 0) { |
907 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
908 | | ERR_raise(ERR_LIB_CMP, CMP_R_REQUEST_NOT_ACCEPTED); |
909 | | return 0; |
910 | | #endif |
911 | 3.81k | } |
912 | 3.81k | } break; |
913 | 1.42k | case OSSL_CMP_PKIBODY_IR: |
914 | 2.45k | case OSSL_CMP_PKIBODY_CR: |
915 | 2.63k | case OSSL_CMP_PKIBODY_KUR: |
916 | 2.63k | if (!OSSL_CRMF_MSGS_verify_popo(msg->body->value.ir, OSSL_CMP_CERTREQID, |
917 | 2.63k | acceptRAVerified, |
918 | 2.63k | ctx->libctx, ctx->propq)) { |
919 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
920 | | return 0; |
921 | | #endif |
922 | 2.53k | } |
923 | 2.63k | break; |
924 | 0 | default: |
925 | 0 | ERR_raise(ERR_LIB_CMP, CMP_R_PKIBODY_ERROR); |
926 | 0 | return 0; |
927 | 6.45k | } |
928 | 6.45k | return 1; |
929 | 6.45k | } |