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