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