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