A local copy of OpenSSL from GitHub
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

853 lines
31 KiB

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