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