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