cmp_vfy.c 31 KB

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