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