2 * Copyright 2007-2023 The OpenSSL Project Authors. All Rights Reserved.
3 * Copyright Nokia 2007-2020
4 * Copyright Siemens AG 2015-2020
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
12 /* CMP functions for PKIMessage checking */
14 #include "cmp_local.h"
15 #include <openssl/cmp_util.h>
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>
24 /* Verify a message protected by signature according to RFC section 5.1.3.3 */
25 static int verify_signature(const OSSL_CMP_CTX
*cmp_ctx
,
26 const OSSL_CMP_MSG
*msg
, X509
*cert
)
28 OSSL_CMP_PROTECTEDPART prot_part
;
29 EVP_PKEY
*pubkey
= NULL
;
33 if (!ossl_assert(cmp_ctx
!= NULL
&& msg
!= NULL
&& cert
!= NULL
))
36 bio
= BIO_new(BIO_s_mem()); /* may be NULL */
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) {
42 ERR_raise(ERR_LIB_CMP
, CMP_R_MISSING_KEY_USAGE_DIGITALSIGNATURE
);
46 pubkey
= X509_get_pubkey(cert
);
48 ERR_raise(ERR_LIB_CMP
, CMP_R_FAILED_EXTRACTING_PUBKEY
);
52 prot_part
.header
= msg
->header
;
53 prot_part
.body
= msg
->body
;
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) {
64 res
= ossl_x509_print_ex_brief(bio
, cert
, X509_FLAG_NO_EXTENSIONS
);
65 ERR_raise(ERR_LIB_CMP
, CMP_R_ERROR_VALIDATING_SIGNATURE
);
67 ERR_add_error_mem_bio("\n", bio
);
71 EVP_PKEY_free(pubkey
);
77 /* Verify a message protected with PBMAC */
78 static int verify_PBMAC(OSSL_CMP_CTX
*ctx
, const OSSL_CMP_MSG
*msg
)
80 ASN1_BIT_STRING
*protection
= NULL
;
83 /* generate expected protection for the message */
84 if ((protection
= ossl_cmp_calc_protection(ctx
, msg
)) == NULL
)
85 return 0; /* failed to generate protection string! */
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
);
94 ERR_raise(ERR_LIB_CMP
, CMP_R_WRONG_PBM_VALUE
);
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.
104 * Returns 1 on successful validation and 0 otherwise.
106 int OSSL_CMP_validate_cert_path(const OSSL_CMP_CTX
*ctx
,
107 X509_STORE
*trusted_store
, X509
*cert
)
110 X509_STORE_CTX
*csc
= NULL
;
113 if (ctx
== NULL
|| cert
== NULL
) {
114 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
118 if (trusted_store
== NULL
) {
119 ERR_raise(ERR_LIB_CMP
, CMP_R_MISSING_TRUST_STORE
);
123 if ((csc
= X509_STORE_CTX_new_ex(ctx
->libctx
, ctx
->propq
)) == NULL
124 || !X509_STORE_CTX_init(csc
, trusted_store
,
125 cert
, ctx
->untrusted
))
128 valid
= X509_verify_cert(csc
) > 0;
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
)
133 ERR_raise(ERR_LIB_CMP
, CMP_R_POTENTIALLY_INVALID_CERTIFICATE
);
136 /* directly output any fresh errors, needed for check_msg_find_cert() */
137 OSSL_CMP_CTX_print_errors(ctx
);
138 X509_STORE_CTX_free(csc
);
142 /* Return 0 if expect_name != NULL and there is no matching actual_name */
143 static int check_name(const OSSL_CMP_CTX
*ctx
, int log_success
,
144 const char *actual_desc
, const X509_NAME
*actual_name
,
145 const char *expect_desc
, const X509_NAME
*expect_name
)
149 if (expect_name
== NULL
)
150 return 1; /* no expectation, thus trivially fulfilled */
152 /* make sure that a matching name is there */
153 if (actual_name
== NULL
) {
154 ossl_cmp_log1(WARN
, ctx
, "missing %s", actual_desc
);
157 str
= X509_NAME_oneline(actual_name
, NULL
, 0);
158 if (X509_NAME_cmp(actual_name
, expect_name
) == 0) {
159 if (log_success
&& str
!= NULL
)
160 ossl_cmp_log2(INFO
, ctx
, " subject matches %s: %s", expect_desc
,
167 ossl_cmp_log2(INFO
, ctx
, " actual name in %s = %s", actual_desc
, str
);
169 if ((str
= X509_NAME_oneline(expect_name
, NULL
, 0)) != NULL
)
170 ossl_cmp_log2(INFO
, ctx
, " does not match %s = %s", expect_desc
, str
);
175 /* Return 0 if skid != NULL and there is no matching subject key ID in cert */
176 static int check_kid(const OSSL_CMP_CTX
*ctx
,
177 const ASN1_OCTET_STRING
*ckid
,
178 const ASN1_OCTET_STRING
*skid
)
183 return 1; /* no expectation, thus trivially fulfilled */
185 /* make sure that the expected subject key identifier is there */
187 ossl_cmp_warn(ctx
, "missing Subject Key Identifier in certificate");
190 str
= i2s_ASN1_OCTET_STRING(NULL
, ckid
);
191 if (ASN1_OCTET_STRING_cmp(ckid
, skid
) == 0) {
193 ossl_cmp_log1(INFO
, ctx
, " subjectKID matches senderKID: %s", str
);
199 ossl_cmp_log1(INFO
, ctx
, " cert Subject Key Identifier = %s", str
);
201 if ((str
= i2s_ASN1_OCTET_STRING(NULL
, skid
)) != NULL
)
202 ossl_cmp_log1(INFO
, ctx
, " does not match senderKID = %s", str
);
207 static int already_checked(const X509
*cert
,
208 const STACK_OF(X509
) *already_checked
)
212 for (i
= sk_X509_num(already_checked
/* may be NULL */); i
> 0; i
--)
213 if (X509_cmp(sk_X509_value(already_checked
, i
- 1), cert
) == 0)
219 * Check if the given cert is acceptable as sender cert of the given message.
220 * The subject DN must match, the subject key ID as well if present in the msg,
221 * and the cert must be current (checked if ctx->trusted is not NULL).
222 * Note that cert revocation etc. is checked by OSSL_CMP_validate_cert_path().
224 * Returns 0 on error or not acceptable, else 1.
226 static int cert_acceptable(const OSSL_CMP_CTX
*ctx
,
227 const char *desc1
, const char *desc2
, X509
*cert
,
228 const STACK_OF(X509
) *already_checked1
,
229 const STACK_OF(X509
) *already_checked2
,
230 const OSSL_CMP_MSG
*msg
)
232 X509_STORE
*ts
= ctx
->trusted
;
233 int self_issued
= X509_check_issued(cert
, cert
) == X509_V_OK
;
235 X509_VERIFY_PARAM
*vpm
= ts
!= NULL
? X509_STORE_get0_param(ts
) : NULL
;
238 ossl_cmp_log3(INFO
, ctx
, " considering %s%s %s with..",
239 self_issued
? "self-issued ": "", desc1
, desc2
);
240 if ((str
= X509_NAME_oneline(X509_get_subject_name(cert
), NULL
, 0)) != NULL
)
241 ossl_cmp_log1(INFO
, ctx
, " subject = %s", str
);
244 str
= X509_NAME_oneline(X509_get_issuer_name(cert
), NULL
, 0);
246 ossl_cmp_log1(INFO
, ctx
, " issuer = %s", str
);
250 if (already_checked(cert
, already_checked1
)
251 || already_checked(cert
, already_checked2
)) {
252 ossl_cmp_info(ctx
, " cert has already been checked");
256 time_cmp
= X509_cmp_timeframe(vpm
, X509_get0_notBefore(cert
),
257 X509_get0_notAfter(cert
));
259 ossl_cmp_warn(ctx
, time_cmp
> 0 ? "cert has expired"
260 : "cert is not yet valid");
264 if (!check_name(ctx
, 1,
265 "cert subject", X509_get_subject_name(cert
),
266 "sender field", msg
->header
->sender
->d
.directoryName
))
269 if (!check_kid(ctx
, X509_get0_subject_key_id(cert
), msg
->header
->senderKID
))
271 /* prevent misleading error later in case x509v3_cache_extensions() fails */
272 if (!ossl_x509v3_cache_extensions(cert
)) {
273 ossl_cmp_warn(ctx
, "cert appears to be invalid");
276 if (!verify_signature(ctx
, msg
, cert
)) {
277 ossl_cmp_warn(ctx
, "msg signature verification failed");
280 /* acceptable also if there is no senderKID in msg header */
281 ossl_cmp_info(ctx
, " cert seems acceptable");
285 static int check_cert_path(const OSSL_CMP_CTX
*ctx
, X509_STORE
*store
,
288 if (OSSL_CMP_validate_cert_path(ctx
, store
, scrt
))
292 "msg signature validates but cert path validation failed");
297 * Exceptional handling for 3GPP TS 33.310 [3G/LTE Network Domain Security
298 * (NDS); Authentication Framework (AF)], only to use for IP messages
299 * and if the ctx option is explicitly set: use self-issued certificates
300 * from extraCerts as trust anchor to validate sender cert -
301 * provided it also can validate the newly enrolled certificate
303 static int check_cert_path_3gpp(const OSSL_CMP_CTX
*ctx
,
304 const OSSL_CMP_MSG
*msg
, X509
*scrt
)
309 if (!ctx
->permitTAInExtraCertsForIR
)
312 if ((store
= X509_STORE_new()) == NULL
313 || !ossl_cmp_X509_STORE_add1_certs(store
, msg
->extraCerts
,
314 1 /* self-issued only */))
317 /* store does not include CRLs */
318 valid
= OSSL_CMP_validate_cert_path(ctx
, store
, scrt
);
321 "also exceptional 3GPP mode cert path validation failed");
324 * verify that the newly enrolled certificate (which assumed rid ==
325 * OSSL_CMP_CERTREQID) can also be validated with the same trusted store
327 OSSL_CMP_CERTRESPONSE
*crep
=
328 ossl_cmp_certrepmessage_get0_certresponse(msg
->body
->value
.ip
,
330 X509
*newcrt
= ossl_cmp_certresponse_get1_cert(ctx
, crep
);
333 * maybe better use get_cert_status() from cmp_client.c, which catches
336 valid
= OSSL_CMP_validate_cert_path(ctx
, store
, newcrt
);
341 X509_STORE_free(store
);
345 static int check_msg_given_cert(const OSSL_CMP_CTX
*ctx
, X509
*cert
,
346 const OSSL_CMP_MSG
*msg
)
348 return cert_acceptable(ctx
, "previously validated", "sender cert",
349 cert
, NULL
, NULL
, msg
)
350 && (check_cert_path(ctx
, ctx
->trusted
, cert
)
351 || check_cert_path_3gpp(ctx
, msg
, cert
));
355 * Try all certs in given list for verifying msg, normally or in 3GPP mode.
356 * If already_checked1 == NULL then certs are assumed to be the msg->extraCerts.
357 * On success cache the found cert using ossl_cmp_ctx_set1_validatedSrvCert().
359 static int check_msg_with_certs(OSSL_CMP_CTX
*ctx
, const STACK_OF(X509
) *certs
,
361 const STACK_OF(X509
) *already_checked1
,
362 const STACK_OF(X509
) *already_checked2
,
363 const OSSL_CMP_MSG
*msg
, int mode_3gpp
)
365 int in_extraCerts
= already_checked1
== NULL
;
366 int n_acceptable_certs
= 0;
369 if (sk_X509_num(certs
) <= 0) {
370 ossl_cmp_log1(WARN
, ctx
, "no %s", desc
);
374 for (i
= 0; i
< sk_X509_num(certs
); i
++) { /* certs may be NULL */
375 X509
*cert
= sk_X509_value(certs
, i
);
377 if (!ossl_assert(cert
!= NULL
))
379 if (!cert_acceptable(ctx
, "cert from", desc
, cert
,
380 already_checked1
, already_checked2
, msg
))
382 n_acceptable_certs
++;
383 if (mode_3gpp
? check_cert_path_3gpp(ctx
, msg
, cert
)
384 : check_cert_path(ctx
, ctx
->trusted
, cert
)) {
385 /* store successful sender cert for further msgs in transaction */
386 return ossl_cmp_ctx_set1_validatedSrvCert(ctx
, cert
);
389 if (in_extraCerts
&& n_acceptable_certs
== 0)
390 ossl_cmp_warn(ctx
, "no acceptable cert in extraCerts");
395 * Verify msg trying first ctx->untrusted, which should include extraCerts
396 * at its front, then trying the trusted certs in truststore (if any) of ctx.
397 * On success cache the found cert using ossl_cmp_ctx_set1_validatedSrvCert().
399 static int check_msg_all_certs(OSSL_CMP_CTX
*ctx
, const OSSL_CMP_MSG
*msg
,
405 && ((!ctx
->permitTAInExtraCertsForIR
406 || OSSL_CMP_MSG_get_bodytype(msg
) != OSSL_CMP_PKIBODY_IP
)))
410 mode_3gpp
? "normal mode failed; trying now 3GPP mode trusting extraCerts"
411 : "trying first normal mode using trust store");
412 if (check_msg_with_certs(ctx
, msg
->extraCerts
, "extraCerts",
413 NULL
, NULL
, msg
, mode_3gpp
))
415 if (check_msg_with_certs(ctx
, ctx
->untrusted
, "untrusted certs",
416 msg
->extraCerts
, NULL
, msg
, mode_3gpp
))
419 if (ctx
->trusted
== NULL
) {
420 ossl_cmp_warn(ctx
, mode_3gpp
? "no self-issued extraCerts"
421 : "no trusted store");
423 STACK_OF(X509
) *trusted
= X509_STORE_get1_all_certs(ctx
->trusted
);
425 ret
= check_msg_with_certs(ctx
, trusted
,
426 mode_3gpp
? "self-issued extraCerts"
427 : "certs in trusted store",
428 msg
->extraCerts
, ctx
->untrusted
,
430 OSSL_STACK_OF_X509_free(trusted
);
435 static int no_log_cb(const char *func
, const char *file
, int line
,
436 OSSL_CMP_severity level
, const char *msg
)
442 * Verify message signature with any acceptable and valid candidate cert.
443 * On success cache the found cert using ossl_cmp_ctx_set1_validatedSrvCert().
445 static int check_msg_find_cert(OSSL_CMP_CTX
*ctx
, const OSSL_CMP_MSG
*msg
)
447 X509
*scrt
= ctx
->validatedSrvCert
; /* previous successful sender cert */
448 GENERAL_NAME
*sender
= msg
->header
->sender
;
450 char *skid_str
= NULL
;
451 const ASN1_OCTET_STRING
*skid
= msg
->header
->senderKID
;
452 OSSL_CMP_log_cb_t backup_log_cb
= ctx
->log_cb
;
455 if (sender
== NULL
|| msg
->body
== NULL
)
456 return 0; /* other NULL cases already have been checked */
457 if (sender
->type
!= GEN_DIRNAME
) {
458 /* So far, only X509_NAME is supported */
459 ERR_raise(ERR_LIB_CMP
, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED
);
463 /* dump any hitherto errors to avoid confusion when printing further ones */
464 OSSL_CMP_CTX_print_errors(ctx
);
466 /* enable clearing irrelevant errors in attempts to validate sender certs */
467 (void)ERR_set_mark();
468 ctx
->log_cb
= no_log_cb
; /* temporarily disable logging */
471 * try first cached scrt, used successfully earlier in same transaction,
472 * for validating this and any further msgs where extraCerts may be left out
475 if (check_msg_given_cert(ctx
, scrt
, msg
)) {
476 ctx
->log_cb
= backup_log_cb
;
477 (void)ERR_pop_to_mark();
480 /* cached sender cert has shown to be no more successfully usable */
481 (void)ossl_cmp_ctx_set1_validatedSrvCert(ctx
, NULL
);
482 /* re-do the above check (just) for adding diagnostic information */
484 "trying to verify msg signature with previously validated cert");
485 (void)check_msg_given_cert(ctx
, scrt
, msg
);
488 res
= check_msg_all_certs(ctx
, msg
, 0 /* using ctx->trusted */)
489 || check_msg_all_certs(ctx
, msg
, 1 /* 3gpp */);
490 ctx
->log_cb
= backup_log_cb
;
492 /* discard any diagnostic information on trying to use certs */
493 (void)ERR_pop_to_mark();
496 /* failed finding a sender cert that verifies the message signature */
497 (void)ERR_clear_last_mark();
499 sname
= X509_NAME_oneline(sender
->d
.directoryName
, NULL
, 0);
500 skid_str
= skid
== NULL
? NULL
: i2s_ASN1_OCTET_STRING(NULL
, skid
);
501 if (ctx
->log_cb
!= NULL
) {
502 ossl_cmp_info(ctx
, "trying to verify msg signature with a valid cert that..");
504 ossl_cmp_log1(INFO
, ctx
, "matches msg sender = %s", sname
);
505 if (skid_str
!= NULL
)
506 ossl_cmp_log1(INFO
, ctx
, "matches msg senderKID = %s", skid_str
);
508 ossl_cmp_info(ctx
, "while msg header does not contain senderKID");
509 /* re-do the above checks (just) for adding diagnostic information */
510 (void)check_msg_all_certs(ctx
, msg
, 0 /* using ctx->trusted */);
511 (void)check_msg_all_certs(ctx
, msg
, 1 /* 3gpp */);
514 ERR_raise(ERR_LIB_CMP
, CMP_R_NO_SUITABLE_SENDER_CERT
);
516 ERR_add_error_txt(NULL
, "for msg sender name = ");
517 ERR_add_error_txt(NULL
, sname
);
519 if (skid_str
!= NULL
) {
520 ERR_add_error_txt(" and ", "for msg senderKID = ");
521 ERR_add_error_txt(NULL
, skid_str
);
526 OPENSSL_free(skid_str
);
531 * Validate the protection of the given PKIMessage using either password-
532 * based mac (PBM) or a signature algorithm. In the case of signature algorithm,
533 * the sender certificate can have been pinned by providing it in ctx->srvCert,
534 * else it is searched in msg->extraCerts, ctx->untrusted, in ctx->trusted
535 * (in this order) and is path is validated against ctx->trusted.
536 * On success cache the found cert using ossl_cmp_ctx_set1_validatedSrvCert().
538 * If ctx->permitTAInExtraCertsForIR is true and when validating a CMP IP msg,
539 * the trust anchor for validating the IP msg may be taken from msg->extraCerts
540 * if a self-issued certificate is found there that can be used to
541 * validate the enrolled certificate returned in the IP.
542 * This is according to the need given in 3GPP TS 33.310.
544 * Returns 1 on success, 0 on error or validation failed.
546 int OSSL_CMP_validate_msg(OSSL_CMP_CTX
*ctx
, const OSSL_CMP_MSG
*msg
)
550 ossl_cmp_debug(ctx
, "validating CMP message");
551 if (ctx
== NULL
|| msg
== NULL
552 || msg
->header
== NULL
|| msg
->body
== NULL
) {
553 ERR_raise(ERR_LIB_CMP
, CMP_R_NULL_ARGUMENT
);
557 if (msg
->header
->protectionAlg
== NULL
/* unprotected message */
558 || msg
->protection
== NULL
|| msg
->protection
->data
== NULL
) {
559 ERR_raise(ERR_LIB_CMP
, CMP_R_MISSING_PROTECTION
);
563 switch (ossl_cmp_hdr_get_protection_nid(msg
->header
)) {
564 /* 5.1.3.1. Shared Secret Information */
565 case NID_id_PasswordBasedMAC
:
566 if (ctx
->secretValue
== NULL
) {
567 ossl_cmp_info(ctx
, "no secret available for verifying PBM-based CMP message protection");
568 ERR_raise(ERR_LIB_CMP
, CMP_R_MISSING_SECRET
);
571 if (verify_PBMAC(ctx
, msg
)) {
573 * RFC 4210, 5.3.2: 'Note that if the PKI Message Protection is
574 * "shared secret information", then any certificate transported in
575 * the caPubs field may be directly trusted as a root CA
576 * certificate by the initiator.'
578 switch (OSSL_CMP_MSG_get_bodytype(msg
)) {
581 case OSSL_CMP_PKIBODY_IP
:
582 case OSSL_CMP_PKIBODY_CP
:
583 case OSSL_CMP_PKIBODY_KUP
:
584 case OSSL_CMP_PKIBODY_CCP
:
585 if (ctx
->trusted
!= NULL
) {
586 STACK_OF(X509
) *certs
= msg
->body
->value
.ip
->caPubs
;
587 /* value.ip is same for cp, kup, and ccp */
589 if (!ossl_cmp_X509_STORE_add1_certs(ctx
->trusted
, certs
, 0))
590 /* adds both self-issued and not self-issued certs */
598 "successfully validated PBM-based CMP message protection");
601 ossl_cmp_warn(ctx
, "verifying PBM-based CMP message protection failed");
605 * 5.1.3.2 DH Key Pairs
608 case NID_id_DHBasedMac
:
609 ERR_raise(ERR_LIB_CMP
, CMP_R_UNSUPPORTED_PROTECTION_ALG_DHBASEDMAC
);
618 if (ctx
->trusted
== NULL
) {
619 ossl_cmp_info(ctx
, "no trust store nor pinned server cert available for verifying signature-based CMP message protection");
620 ERR_raise(ERR_LIB_CMP
, CMP_R_MISSING_TRUST_ANCHOR
);
623 if (check_msg_find_cert(ctx
, msg
)) {
625 "successfully validated signature-based CMP message protection using trust store");
628 } else { /* use pinned sender cert */
629 /* use ctx->srvCert for signature check even if not acceptable */
630 if (verify_signature(ctx
, msg
, scrt
)) {
632 "successfully validated signature-based CMP message protection using pinned server cert");
633 return ossl_cmp_ctx_set1_validatedSrvCert(ctx
, scrt
);
635 ossl_cmp_warn(ctx
, "CMP message signature verification failed");
636 ERR_raise(ERR_LIB_CMP
, CMP_R_SRVCERT_DOES_NOT_VALIDATE_MSG
);
643 static int check_transactionID_or_nonce(ASN1_OCTET_STRING
*expected
,
644 ASN1_OCTET_STRING
*actual
, int reason
)
647 && (actual
== NULL
|| ASN1_OCTET_STRING_cmp(expected
, actual
) != 0)) {
648 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
649 char *expected_str
, *actual_str
;
651 expected_str
= i2s_ASN1_OCTET_STRING(NULL
, expected
);
652 actual_str
= actual
== NULL
? NULL
: i2s_ASN1_OCTET_STRING(NULL
, actual
);
653 ERR_raise_data(ERR_LIB_CMP
, reason
,
654 "expected = %s, actual = %s",
655 expected_str
== NULL
? "?" : expected_str
,
656 actual
== NULL
? "(none)" :
657 actual_str
== NULL
? "?" : actual_str
);
658 OPENSSL_free(expected_str
);
659 OPENSSL_free(actual_str
);
667 * Check received message (i.e., response by server or request from client)
668 * Any msg->extraCerts are prepended to ctx->untrusted.
671 * its sender is of appropriate type (currently only X509_NAME) and
672 * matches any expected sender or srvCert subject given in the ctx
673 * it has a valid body type
674 * its protection is valid (or invalid/absent, but only if a callback function
675 * is present and yields a positive result using also the supplied argument)
676 * its transaction ID matches the previous transaction ID stored in ctx (if any)
677 * its recipNonce matches the previous senderNonce stored in the ctx (if any)
679 * If everything is fine:
680 * learns the senderNonce from the received message,
681 * learns the transaction ID if it is not yet in ctx,
682 * and makes any certs in caPubs directly trusted.
684 * Returns 1 on success, 0 on error.
686 int ossl_cmp_msg_check_update(OSSL_CMP_CTX
*ctx
, const OSSL_CMP_MSG
*msg
,
687 ossl_cmp_allow_unprotected_cb_t cb
, int cb_arg
)
689 OSSL_CMP_PKIHEADER
*hdr
;
690 const X509_NAME
*expected_sender
;
692 if (!ossl_assert(ctx
!= NULL
&& msg
!= NULL
&& msg
->header
!= NULL
))
694 hdr
= OSSL_CMP_MSG_get0_header(msg
);
696 /* validate sender name of received msg */
697 if (hdr
->sender
->type
!= GEN_DIRNAME
) {
698 ERR_raise(ERR_LIB_CMP
, CMP_R_SENDER_GENERALNAME_TYPE_NOT_SUPPORTED
);
702 * Compare actual sender name of response with expected sender name.
703 * Mitigates risk to accept misused PBM secret
704 * or misused certificate of an unauthorized entity of a trusted hierarchy.
706 expected_sender
= ctx
->expected_sender
;
707 if (expected_sender
== NULL
&& ctx
->srvCert
!= NULL
)
708 expected_sender
= X509_get_subject_name(ctx
->srvCert
);
709 if (!check_name(ctx
, 0, "sender DN field", hdr
->sender
->d
.directoryName
,
710 "expected sender", expected_sender
))
712 /* Note: if recipient was NULL-DN it could be learned here if needed */
714 if (sk_X509_num(msg
->extraCerts
) > 10)
716 "received CMP message contains more than 10 extraCerts");
718 * Store any provided extraCerts in ctx for use in OSSL_CMP_validate_msg()
719 * and for future use, 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 * Note that it does not help validating the message before storing the
722 * extraCerts because they do not belong to the protected msg part anyway.
723 * For efficiency, the extraCerts are prepended so they get used first.
725 if (!X509_add_certs(ctx
->untrusted
, msg
->extraCerts
,
726 /* this allows self-signed certs */
727 X509_ADD_FLAG_UP_REF
| X509_ADD_FLAG_NO_DUP
728 | X509_ADD_FLAG_PREPEND
))
731 /* validate message protection */
732 if (hdr
->protectionAlg
!= NULL
) {
733 /* detect explicitly permitted exceptions for invalid protection */
734 if (!OSSL_CMP_validate_msg(ctx
, msg
)
735 && (cb
== NULL
|| (*cb
)(ctx
, msg
, 1, cb_arg
) <= 0)) {
736 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
737 ERR_raise(ERR_LIB_CMP
, CMP_R_ERROR_VALIDATING_PROTECTION
);
742 /* detect explicitly permitted exceptions for missing protection */
743 if (cb
== NULL
|| (*cb
)(ctx
, msg
, 0, cb_arg
) <= 0) {
744 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
745 ERR_raise(ERR_LIB_CMP
, CMP_R_MISSING_PROTECTION
);
751 /* check CMP version number in header */
752 if (ossl_cmp_hdr_get_pvno(hdr
) != OSSL_CMP_PVNO_2
753 && ossl_cmp_hdr_get_pvno(hdr
) != OSSL_CMP_PVNO_3
) {
754 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
755 ERR_raise(ERR_LIB_CMP
, CMP_R_UNEXPECTED_PVNO
);
760 if (OSSL_CMP_MSG_get_bodytype(msg
) < 0) {
761 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
762 ERR_raise(ERR_LIB_CMP
, CMP_R_PKIBODY_ERROR
);
767 /* compare received transactionID with the expected one in previous msg */
768 if (!check_transactionID_or_nonce(ctx
->transactionID
, hdr
->transactionID
,
769 CMP_R_TRANSACTIONID_UNMATCHED
))
772 /* compare received nonce with the one we sent */
773 if (!check_transactionID_or_nonce(ctx
->senderNonce
, hdr
->recipNonce
,
774 CMP_R_RECIPNONCE_UNMATCHED
))
777 /* if not yet present, learn transactionID */
778 if (ctx
->transactionID
== NULL
779 && !OSSL_CMP_CTX_set1_transactionID(ctx
, hdr
->transactionID
))
783 * RFC 4210 section 5.1.1 states: the recipNonce is copied from
784 * the senderNonce of the previous message in the transaction.
785 * --> Store for setting in next message
787 if (!ossl_cmp_ctx_set1_recipNonce(ctx
, hdr
->senderNonce
))
791 * Store any provided extraCerts in ctx for future use,
792 * such that they are available to ctx->certConf_cb and
793 * the peer does not need to send them again in the same transaction.
794 * For efficiency, the extraCerts are prepended so they get used first.
796 if (!X509_add_certs(ctx
->untrusted
, msg
->extraCerts
,
797 /* this allows self-signed certs */
798 X509_ADD_FLAG_UP_REF
| X509_ADD_FLAG_NO_DUP
799 | X509_ADD_FLAG_PREPEND
))
802 if (ossl_cmp_hdr_get_protection_nid(hdr
) == NID_id_PasswordBasedMAC
) {
804 * RFC 4210, 5.3.2: 'Note that if the PKI Message Protection is
805 * "shared secret information", then any certificate transported in
806 * the caPubs field may be directly trusted as a root CA
807 * certificate by the initiator.'
809 switch (OSSL_CMP_MSG_get_bodytype(msg
)) {
810 case OSSL_CMP_PKIBODY_IP
:
811 case OSSL_CMP_PKIBODY_CP
:
812 case OSSL_CMP_PKIBODY_KUP
:
813 case OSSL_CMP_PKIBODY_CCP
:
814 if (ctx
->trusted
!= NULL
) {
815 STACK_OF(X509
) *certs
= msg
->body
->value
.ip
->caPubs
;
816 /* value.ip is same for cp, kup, and ccp */
818 if (!ossl_cmp_X509_STORE_add1_certs(ctx
->trusted
, certs
, 0))
819 /* adds both self-issued and not self-issued certs */
830 int ossl_cmp_verify_popo(const OSSL_CMP_CTX
*ctx
,
831 const OSSL_CMP_MSG
*msg
, int acceptRAVerified
)
833 if (!ossl_assert(msg
!= NULL
&& msg
->body
!= NULL
))
835 switch (msg
->body
->type
) {
836 case OSSL_CMP_PKIBODY_P10CR
:
838 X509_REQ
*req
= msg
->body
->value
.p10cr
;
840 if (X509_REQ_verify_ex(req
, X509_REQ_get0_pubkey(req
), ctx
->libctx
,
842 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
843 ERR_raise(ERR_LIB_CMP
, CMP_R_REQUEST_NOT_ACCEPTED
);
849 case OSSL_CMP_PKIBODY_IR
:
850 case OSSL_CMP_PKIBODY_CR
:
851 case OSSL_CMP_PKIBODY_KUR
:
852 if (!OSSL_CRMF_MSGS_verify_popo(msg
->body
->value
.ir
, OSSL_CMP_CERTREQID
,
854 ctx
->libctx
, ctx
->propq
)) {
855 #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
861 ERR_raise(ERR_LIB_CMP
, CMP_R_PKIBODY_ERROR
);