ts_rsp_verify.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707
  1. /*
  2. * Copyright 2006-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include "internal/cryptlib.h"
  11. #include <openssl/objects.h>
  12. #include <openssl/ts.h>
  13. #include <openssl/pkcs7.h>
  14. #include "ts_local.h"
  15. static int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,
  16. X509 *signer, STACK_OF(X509) **chain);
  17. static int ts_check_signing_certs(PKCS7_SIGNER_INFO *si,
  18. STACK_OF(X509) *chain);
  19. static ESS_SIGNING_CERT *ess_get_signing_cert(PKCS7_SIGNER_INFO *si);
  20. static int ts_find_cert(STACK_OF(ESS_CERT_ID) *cert_ids, X509 *cert);
  21. static int ts_issuer_serial_cmp(ESS_ISSUER_SERIAL *is, X509 *cert);
  22. static int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx,
  23. PKCS7 *token, TS_TST_INFO *tst_info);
  24. static int ts_check_status_info(TS_RESP *response);
  25. static char *ts_get_status_text(STACK_OF(ASN1_UTF8STRING) *text);
  26. static int ts_check_policy(const ASN1_OBJECT *req_oid,
  27. const TS_TST_INFO *tst_info);
  28. static int ts_compute_imprint(BIO *data, TS_TST_INFO *tst_info,
  29. X509_ALGOR **md_alg,
  30. unsigned char **imprint, unsigned *imprint_len);
  31. static int ts_check_imprints(X509_ALGOR *algor_a,
  32. const unsigned char *imprint_a, unsigned len_a,
  33. TS_TST_INFO *tst_info);
  34. static int ts_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info);
  35. static int ts_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer);
  36. static int ts_find_name(STACK_OF(GENERAL_NAME) *gen_names,
  37. GENERAL_NAME *name);
  38. static int ts_find_cert_v2(STACK_OF(ESS_CERT_ID_V2) *cert_ids, X509 *cert);
  39. static ESS_SIGNING_CERT_V2 *ess_get_signing_cert_v2(PKCS7_SIGNER_INFO *si);
  40. /*
  41. * This must be large enough to hold all values in ts_status_text (with
  42. * comma separator) or all text fields in ts_failure_info (also with comma).
  43. */
  44. #define TS_STATUS_BUF_SIZE 256
  45. /*
  46. * Local mapping between response codes and descriptions.
  47. */
  48. static const char *ts_status_text[] = {
  49. "granted",
  50. "grantedWithMods",
  51. "rejection",
  52. "waiting",
  53. "revocationWarning",
  54. "revocationNotification"
  55. };
  56. #define TS_STATUS_TEXT_SIZE OSSL_NELEM(ts_status_text)
  57. static struct {
  58. int code;
  59. const char *text;
  60. } ts_failure_info[] = {
  61. {TS_INFO_BAD_ALG, "badAlg"},
  62. {TS_INFO_BAD_REQUEST, "badRequest"},
  63. {TS_INFO_BAD_DATA_FORMAT, "badDataFormat"},
  64. {TS_INFO_TIME_NOT_AVAILABLE, "timeNotAvailable"},
  65. {TS_INFO_UNACCEPTED_POLICY, "unacceptedPolicy"},
  66. {TS_INFO_UNACCEPTED_EXTENSION, "unacceptedExtension"},
  67. {TS_INFO_ADD_INFO_NOT_AVAILABLE, "addInfoNotAvailable"},
  68. {TS_INFO_SYSTEM_FAILURE, "systemFailure"}
  69. };
  70. /*-
  71. * This function carries out the following tasks:
  72. * - Checks if there is one and only one signer.
  73. * - Search for the signing certificate in 'certs' and in the response.
  74. * - Check the extended key usage and key usage fields of the signer
  75. * certificate (done by the path validation).
  76. * - Build and validate the certificate path.
  77. * - Check if the certificate path meets the requirements of the
  78. * SigningCertificate ESS signed attribute.
  79. * - Verify the signature value.
  80. * - Returns the signer certificate in 'signer', if 'signer' is not NULL.
  81. */
  82. int TS_RESP_verify_signature(PKCS7 *token, STACK_OF(X509) *certs,
  83. X509_STORE *store, X509 **signer_out)
  84. {
  85. STACK_OF(PKCS7_SIGNER_INFO) *sinfos = NULL;
  86. PKCS7_SIGNER_INFO *si;
  87. STACK_OF(X509) *signers = NULL;
  88. X509 *signer;
  89. STACK_OF(X509) *chain = NULL;
  90. char buf[4096];
  91. int i, j = 0, ret = 0;
  92. BIO *p7bio = NULL;
  93. /* Some sanity checks first. */
  94. if (!token) {
  95. TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_INVALID_NULL_POINTER);
  96. goto err;
  97. }
  98. if (!PKCS7_type_is_signed(token)) {
  99. TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_WRONG_CONTENT_TYPE);
  100. goto err;
  101. }
  102. sinfos = PKCS7_get_signer_info(token);
  103. if (!sinfos || sk_PKCS7_SIGNER_INFO_num(sinfos) != 1) {
  104. TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_THERE_MUST_BE_ONE_SIGNER);
  105. goto err;
  106. }
  107. si = sk_PKCS7_SIGNER_INFO_value(sinfos, 0);
  108. if (PKCS7_get_detached(token)) {
  109. TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_NO_CONTENT);
  110. goto err;
  111. }
  112. /*
  113. * Get hold of the signer certificate, search only internal certificates
  114. * if it was requested.
  115. */
  116. signers = PKCS7_get0_signers(token, certs, 0);
  117. if (!signers || sk_X509_num(signers) != 1)
  118. goto err;
  119. signer = sk_X509_value(signers, 0);
  120. if (!ts_verify_cert(store, certs, signer, &chain))
  121. goto err;
  122. if (!ts_check_signing_certs(si, chain))
  123. goto err;
  124. p7bio = PKCS7_dataInit(token, NULL);
  125. /* We now have to 'read' from p7bio to calculate digests etc. */
  126. while ((i = BIO_read(p7bio, buf, sizeof(buf))) > 0)
  127. continue;
  128. j = PKCS7_signatureVerify(p7bio, token, si, signer);
  129. if (j <= 0) {
  130. TSerr(TS_F_TS_RESP_VERIFY_SIGNATURE, TS_R_SIGNATURE_FAILURE);
  131. goto err;
  132. }
  133. if (signer_out) {
  134. *signer_out = signer;
  135. X509_up_ref(signer);
  136. }
  137. ret = 1;
  138. err:
  139. BIO_free_all(p7bio);
  140. sk_X509_pop_free(chain, X509_free);
  141. sk_X509_free(signers);
  142. return ret;
  143. }
  144. /*
  145. * The certificate chain is returned in chain. Caller is responsible for
  146. * freeing the vector.
  147. */
  148. static int ts_verify_cert(X509_STORE *store, STACK_OF(X509) *untrusted,
  149. X509 *signer, STACK_OF(X509) **chain)
  150. {
  151. X509_STORE_CTX *cert_ctx = NULL;
  152. int i;
  153. int ret = 0;
  154. *chain = NULL;
  155. cert_ctx = X509_STORE_CTX_new();
  156. if (cert_ctx == NULL) {
  157. TSerr(TS_F_TS_VERIFY_CERT, ERR_R_MALLOC_FAILURE);
  158. goto err;
  159. }
  160. if (!X509_STORE_CTX_init(cert_ctx, store, signer, untrusted))
  161. goto end;
  162. X509_STORE_CTX_set_purpose(cert_ctx, X509_PURPOSE_TIMESTAMP_SIGN);
  163. i = X509_verify_cert(cert_ctx);
  164. if (i <= 0) {
  165. int j = X509_STORE_CTX_get_error(cert_ctx);
  166. TSerr(TS_F_TS_VERIFY_CERT, TS_R_CERTIFICATE_VERIFY_ERROR);
  167. ERR_add_error_data(2, "Verify error:",
  168. X509_verify_cert_error_string(j));
  169. goto err;
  170. }
  171. *chain = X509_STORE_CTX_get1_chain(cert_ctx);
  172. ret = 1;
  173. goto end;
  174. err:
  175. ret = 0;
  176. end:
  177. X509_STORE_CTX_free(cert_ctx);
  178. return ret;
  179. }
  180. static int ts_check_signing_certs(PKCS7_SIGNER_INFO *si,
  181. STACK_OF(X509) *chain)
  182. {
  183. ESS_SIGNING_CERT *ss = ess_get_signing_cert(si);
  184. STACK_OF(ESS_CERT_ID) *cert_ids = NULL;
  185. ESS_SIGNING_CERT_V2 *ssv2 = ess_get_signing_cert_v2(si);
  186. STACK_OF(ESS_CERT_ID_V2) *cert_ids_v2 = NULL;
  187. X509 *cert;
  188. int i = 0;
  189. int ret = 0;
  190. if (ss != NULL) {
  191. cert_ids = ss->cert_ids;
  192. cert = sk_X509_value(chain, 0);
  193. if (ts_find_cert(cert_ids, cert) != 0)
  194. goto err;
  195. /*
  196. * Check the other certificates of the chain if there are more than one
  197. * certificate ids in cert_ids.
  198. */
  199. if (sk_ESS_CERT_ID_num(cert_ids) > 1) {
  200. for (i = 1; i < sk_X509_num(chain); ++i) {
  201. cert = sk_X509_value(chain, i);
  202. if (ts_find_cert(cert_ids, cert) < 0)
  203. goto err;
  204. }
  205. }
  206. } else if (ssv2 != NULL) {
  207. cert_ids_v2 = ssv2->cert_ids;
  208. cert = sk_X509_value(chain, 0);
  209. if (ts_find_cert_v2(cert_ids_v2, cert) != 0)
  210. goto err;
  211. /*
  212. * Check the other certificates of the chain if there are more than one
  213. * certificate ids in cert_ids.
  214. */
  215. if (sk_ESS_CERT_ID_V2_num(cert_ids_v2) > 1) {
  216. for (i = 1; i < sk_X509_num(chain); ++i) {
  217. cert = sk_X509_value(chain, i);
  218. if (ts_find_cert_v2(cert_ids_v2, cert) < 0)
  219. goto err;
  220. }
  221. }
  222. } else {
  223. goto err;
  224. }
  225. ret = 1;
  226. err:
  227. if (!ret)
  228. TSerr(TS_F_TS_CHECK_SIGNING_CERTS,
  229. TS_R_ESS_SIGNING_CERTIFICATE_ERROR);
  230. ESS_SIGNING_CERT_free(ss);
  231. ESS_SIGNING_CERT_V2_free(ssv2);
  232. return ret;
  233. }
  234. static ESS_SIGNING_CERT *ess_get_signing_cert(PKCS7_SIGNER_INFO *si)
  235. {
  236. ASN1_TYPE *attr;
  237. const unsigned char *p;
  238. attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificate);
  239. if (!attr)
  240. return NULL;
  241. p = attr->value.sequence->data;
  242. return d2i_ESS_SIGNING_CERT(NULL, &p, attr->value.sequence->length);
  243. }
  244. static ESS_SIGNING_CERT_V2 *ess_get_signing_cert_v2(PKCS7_SIGNER_INFO *si)
  245. {
  246. ASN1_TYPE *attr;
  247. const unsigned char *p;
  248. attr = PKCS7_get_signed_attribute(si, NID_id_smime_aa_signingCertificateV2);
  249. if (attr == NULL)
  250. return NULL;
  251. p = attr->value.sequence->data;
  252. return d2i_ESS_SIGNING_CERT_V2(NULL, &p, attr->value.sequence->length);
  253. }
  254. /* Returns < 0 if certificate is not found, certificate index otherwise. */
  255. static int ts_find_cert(STACK_OF(ESS_CERT_ID) *cert_ids, X509 *cert)
  256. {
  257. int i;
  258. unsigned char cert_sha1[SHA_DIGEST_LENGTH];
  259. if (!cert_ids || !cert)
  260. return -1;
  261. /* Recompute SHA1 hash of certificate if necessary (side effect). */
  262. X509_check_purpose(cert, -1, 0);
  263. if (!X509_digest(cert, EVP_sha1(), cert_sha1, NULL))
  264. return -1;
  265. /* Look for cert in the cert_ids vector. */
  266. for (i = 0; i < sk_ESS_CERT_ID_num(cert_ids); ++i) {
  267. ESS_CERT_ID *cid = sk_ESS_CERT_ID_value(cert_ids, i);
  268. if (cid->hash->length == SHA_DIGEST_LENGTH
  269. && memcmp(cid->hash->data, cert_sha1, SHA_DIGEST_LENGTH) == 0) {
  270. ESS_ISSUER_SERIAL *is = cid->issuer_serial;
  271. if (!is || !ts_issuer_serial_cmp(is, cert))
  272. return i;
  273. }
  274. }
  275. return -1;
  276. }
  277. /* Returns < 0 if certificate is not found, certificate index otherwise. */
  278. static int ts_find_cert_v2(STACK_OF(ESS_CERT_ID_V2) *cert_ids, X509 *cert)
  279. {
  280. int i;
  281. unsigned char cert_digest[EVP_MAX_MD_SIZE];
  282. unsigned int len;
  283. /* Look for cert in the cert_ids vector. */
  284. for (i = 0; i < sk_ESS_CERT_ID_V2_num(cert_ids); ++i) {
  285. ESS_CERT_ID_V2 *cid = sk_ESS_CERT_ID_V2_value(cert_ids, i);
  286. const EVP_MD *md;
  287. if (cid->hash_alg != NULL)
  288. md = EVP_get_digestbyobj(cid->hash_alg->algorithm);
  289. else
  290. md = EVP_sha256();
  291. if (!X509_digest(cert, md, cert_digest, &len))
  292. return -1;
  293. if (cid->hash->length != (int)len)
  294. return -1;
  295. if (memcmp(cid->hash->data, cert_digest, cid->hash->length) == 0) {
  296. ESS_ISSUER_SERIAL *is = cid->issuer_serial;
  297. if (is == NULL || !ts_issuer_serial_cmp(is, cert))
  298. return i;
  299. }
  300. }
  301. return -1;
  302. }
  303. static int ts_issuer_serial_cmp(ESS_ISSUER_SERIAL *is, X509 *cert)
  304. {
  305. GENERAL_NAME *issuer;
  306. if (!is || !cert || sk_GENERAL_NAME_num(is->issuer) != 1)
  307. return -1;
  308. issuer = sk_GENERAL_NAME_value(is->issuer, 0);
  309. if (issuer->type != GEN_DIRNAME
  310. || X509_NAME_cmp(issuer->d.dirn, X509_get_issuer_name(cert)))
  311. return -1;
  312. if (ASN1_INTEGER_cmp(is->serial, X509_get_serialNumber(cert)))
  313. return -1;
  314. return 0;
  315. }
  316. /*-
  317. * Verifies whether 'response' contains a valid response with regards
  318. * to the settings of the context:
  319. * - Gives an error message if the TS_TST_INFO is not present.
  320. * - Calls _TS_RESP_verify_token to verify the token content.
  321. */
  322. int TS_RESP_verify_response(TS_VERIFY_CTX *ctx, TS_RESP *response)
  323. {
  324. PKCS7 *token = response->token;
  325. TS_TST_INFO *tst_info = response->tst_info;
  326. int ret = 0;
  327. if (!ts_check_status_info(response))
  328. goto err;
  329. if (!int_ts_RESP_verify_token(ctx, token, tst_info))
  330. goto err;
  331. ret = 1;
  332. err:
  333. return ret;
  334. }
  335. /*
  336. * Tries to extract a TS_TST_INFO structure from the PKCS7 token and
  337. * calls the internal int_TS_RESP_verify_token function for verifying it.
  338. */
  339. int TS_RESP_verify_token(TS_VERIFY_CTX *ctx, PKCS7 *token)
  340. {
  341. TS_TST_INFO *tst_info = PKCS7_to_TS_TST_INFO(token);
  342. int ret = 0;
  343. if (tst_info) {
  344. ret = int_ts_RESP_verify_token(ctx, token, tst_info);
  345. TS_TST_INFO_free(tst_info);
  346. }
  347. return ret;
  348. }
  349. /*-
  350. * Verifies whether the 'token' contains a valid time stamp token
  351. * with regards to the settings of the context. Only those checks are
  352. * carried out that are specified in the context:
  353. * - Verifies the signature of the TS_TST_INFO.
  354. * - Checks the version number of the response.
  355. * - Check if the requested and returned policies math.
  356. * - Check if the message imprints are the same.
  357. * - Check if the nonces are the same.
  358. * - Check if the TSA name matches the signer.
  359. * - Check if the TSA name is the expected TSA.
  360. */
  361. static int int_ts_RESP_verify_token(TS_VERIFY_CTX *ctx,
  362. PKCS7 *token, TS_TST_INFO *tst_info)
  363. {
  364. X509 *signer = NULL;
  365. GENERAL_NAME *tsa_name = tst_info->tsa;
  366. X509_ALGOR *md_alg = NULL;
  367. unsigned char *imprint = NULL;
  368. unsigned imprint_len = 0;
  369. int ret = 0;
  370. int flags = ctx->flags;
  371. /* Some options require us to also check the signature */
  372. if (((flags & TS_VFY_SIGNER) && tsa_name != NULL)
  373. || (flags & TS_VFY_TSA_NAME)) {
  374. flags |= TS_VFY_SIGNATURE;
  375. }
  376. if ((flags & TS_VFY_SIGNATURE)
  377. && !TS_RESP_verify_signature(token, ctx->certs, ctx->store, &signer))
  378. goto err;
  379. if ((flags & TS_VFY_VERSION)
  380. && TS_TST_INFO_get_version(tst_info) != 1) {
  381. TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_UNSUPPORTED_VERSION);
  382. goto err;
  383. }
  384. if ((flags & TS_VFY_POLICY)
  385. && !ts_check_policy(ctx->policy, tst_info))
  386. goto err;
  387. if ((flags & TS_VFY_IMPRINT)
  388. && !ts_check_imprints(ctx->md_alg, ctx->imprint, ctx->imprint_len,
  389. tst_info))
  390. goto err;
  391. if ((flags & TS_VFY_DATA)
  392. && (!ts_compute_imprint(ctx->data, tst_info,
  393. &md_alg, &imprint, &imprint_len)
  394. || !ts_check_imprints(md_alg, imprint, imprint_len, tst_info)))
  395. goto err;
  396. if ((flags & TS_VFY_NONCE)
  397. && !ts_check_nonces(ctx->nonce, tst_info))
  398. goto err;
  399. if ((flags & TS_VFY_SIGNER)
  400. && tsa_name && !ts_check_signer_name(tsa_name, signer)) {
  401. TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_NAME_MISMATCH);
  402. goto err;
  403. }
  404. if ((flags & TS_VFY_TSA_NAME)
  405. && !ts_check_signer_name(ctx->tsa_name, signer)) {
  406. TSerr(TS_F_INT_TS_RESP_VERIFY_TOKEN, TS_R_TSA_UNTRUSTED);
  407. goto err;
  408. }
  409. ret = 1;
  410. err:
  411. X509_free(signer);
  412. X509_ALGOR_free(md_alg);
  413. OPENSSL_free(imprint);
  414. return ret;
  415. }
  416. static int ts_check_status_info(TS_RESP *response)
  417. {
  418. TS_STATUS_INFO *info = response->status_info;
  419. long status = ASN1_INTEGER_get(info->status);
  420. const char *status_text = NULL;
  421. char *embedded_status_text = NULL;
  422. char failure_text[TS_STATUS_BUF_SIZE] = "";
  423. if (status == 0 || status == 1)
  424. return 1;
  425. /* There was an error, get the description in status_text. */
  426. if (0 <= status && status < (long) OSSL_NELEM(ts_status_text))
  427. status_text = ts_status_text[status];
  428. else
  429. status_text = "unknown code";
  430. if (sk_ASN1_UTF8STRING_num(info->text) > 0
  431. && (embedded_status_text = ts_get_status_text(info->text)) == NULL)
  432. return 0;
  433. /* Fill in failure_text with the failure information. */
  434. if (info->failure_info) {
  435. int i;
  436. int first = 1;
  437. for (i = 0; i < (int)OSSL_NELEM(ts_failure_info); ++i) {
  438. if (ASN1_BIT_STRING_get_bit(info->failure_info,
  439. ts_failure_info[i].code)) {
  440. if (!first)
  441. strcat(failure_text, ",");
  442. else
  443. first = 0;
  444. strcat(failure_text, ts_failure_info[i].text);
  445. }
  446. }
  447. }
  448. if (failure_text[0] == '\0')
  449. strcpy(failure_text, "unspecified");
  450. TSerr(TS_F_TS_CHECK_STATUS_INFO, TS_R_NO_TIME_STAMP_TOKEN);
  451. ERR_add_error_data(6,
  452. "status code: ", status_text,
  453. ", status text: ", embedded_status_text ?
  454. embedded_status_text : "unspecified",
  455. ", failure codes: ", failure_text);
  456. OPENSSL_free(embedded_status_text);
  457. return 0;
  458. }
  459. static char *ts_get_status_text(STACK_OF(ASN1_UTF8STRING) *text)
  460. {
  461. int i;
  462. int length = 0;
  463. char *result = NULL;
  464. char *p;
  465. for (i = 0; i < sk_ASN1_UTF8STRING_num(text); ++i) {
  466. ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
  467. if (ASN1_STRING_length(current) > TS_MAX_STATUS_LENGTH - length - 1)
  468. return NULL;
  469. length += ASN1_STRING_length(current);
  470. length += 1; /* separator character */
  471. }
  472. if ((result = OPENSSL_malloc(length)) == NULL) {
  473. TSerr(TS_F_TS_GET_STATUS_TEXT, ERR_R_MALLOC_FAILURE);
  474. return NULL;
  475. }
  476. for (i = 0, p = result; i < sk_ASN1_UTF8STRING_num(text); ++i) {
  477. ASN1_UTF8STRING *current = sk_ASN1_UTF8STRING_value(text, i);
  478. length = ASN1_STRING_length(current);
  479. if (i > 0)
  480. *p++ = '/';
  481. strncpy(p, (const char *)ASN1_STRING_get0_data(current), length);
  482. p += length;
  483. }
  484. *p = '\0';
  485. return result;
  486. }
  487. static int ts_check_policy(const ASN1_OBJECT *req_oid,
  488. const TS_TST_INFO *tst_info)
  489. {
  490. const ASN1_OBJECT *resp_oid = tst_info->policy_id;
  491. if (OBJ_cmp(req_oid, resp_oid) != 0) {
  492. TSerr(TS_F_TS_CHECK_POLICY, TS_R_POLICY_MISMATCH);
  493. return 0;
  494. }
  495. return 1;
  496. }
  497. static int ts_compute_imprint(BIO *data, TS_TST_INFO *tst_info,
  498. X509_ALGOR **md_alg,
  499. unsigned char **imprint, unsigned *imprint_len)
  500. {
  501. TS_MSG_IMPRINT *msg_imprint = tst_info->msg_imprint;
  502. X509_ALGOR *md_alg_resp = msg_imprint->hash_algo;
  503. const EVP_MD *md;
  504. EVP_MD_CTX *md_ctx = NULL;
  505. unsigned char buffer[4096];
  506. int length;
  507. *md_alg = NULL;
  508. *imprint = NULL;
  509. if ((*md_alg = X509_ALGOR_dup(md_alg_resp)) == NULL)
  510. goto err;
  511. if ((md = EVP_get_digestbyobj((*md_alg)->algorithm)) == NULL) {
  512. TSerr(TS_F_TS_COMPUTE_IMPRINT, TS_R_UNSUPPORTED_MD_ALGORITHM);
  513. goto err;
  514. }
  515. length = EVP_MD_size(md);
  516. if (length < 0)
  517. goto err;
  518. *imprint_len = length;
  519. if ((*imprint = OPENSSL_malloc(*imprint_len)) == NULL) {
  520. TSerr(TS_F_TS_COMPUTE_IMPRINT, ERR_R_MALLOC_FAILURE);
  521. goto err;
  522. }
  523. md_ctx = EVP_MD_CTX_new();
  524. if (md_ctx == NULL) {
  525. TSerr(TS_F_TS_COMPUTE_IMPRINT, ERR_R_MALLOC_FAILURE);
  526. goto err;
  527. }
  528. if (!EVP_DigestInit(md_ctx, md))
  529. goto err;
  530. while ((length = BIO_read(data, buffer, sizeof(buffer))) > 0) {
  531. if (!EVP_DigestUpdate(md_ctx, buffer, length))
  532. goto err;
  533. }
  534. if (!EVP_DigestFinal(md_ctx, *imprint, NULL))
  535. goto err;
  536. EVP_MD_CTX_free(md_ctx);
  537. return 1;
  538. err:
  539. EVP_MD_CTX_free(md_ctx);
  540. X509_ALGOR_free(*md_alg);
  541. *md_alg = NULL;
  542. OPENSSL_free(*imprint);
  543. *imprint_len = 0;
  544. *imprint = 0;
  545. return 0;
  546. }
  547. static int ts_check_imprints(X509_ALGOR *algor_a,
  548. const unsigned char *imprint_a, unsigned len_a,
  549. TS_TST_INFO *tst_info)
  550. {
  551. TS_MSG_IMPRINT *b = tst_info->msg_imprint;
  552. X509_ALGOR *algor_b = b->hash_algo;
  553. int ret = 0;
  554. if (algor_a) {
  555. if (OBJ_cmp(algor_a->algorithm, algor_b->algorithm))
  556. goto err;
  557. /* The parameter must be NULL in both. */
  558. if ((algor_a->parameter
  559. && ASN1_TYPE_get(algor_a->parameter) != V_ASN1_NULL)
  560. || (algor_b->parameter
  561. && ASN1_TYPE_get(algor_b->parameter) != V_ASN1_NULL))
  562. goto err;
  563. }
  564. ret = len_a == (unsigned)ASN1_STRING_length(b->hashed_msg) &&
  565. memcmp(imprint_a, ASN1_STRING_get0_data(b->hashed_msg), len_a) == 0;
  566. err:
  567. if (!ret)
  568. TSerr(TS_F_TS_CHECK_IMPRINTS, TS_R_MESSAGE_IMPRINT_MISMATCH);
  569. return ret;
  570. }
  571. static int ts_check_nonces(const ASN1_INTEGER *a, TS_TST_INFO *tst_info)
  572. {
  573. const ASN1_INTEGER *b = tst_info->nonce;
  574. if (!b) {
  575. TSerr(TS_F_TS_CHECK_NONCES, TS_R_NONCE_NOT_RETURNED);
  576. return 0;
  577. }
  578. /* No error if a nonce is returned without being requested. */
  579. if (ASN1_INTEGER_cmp(a, b) != 0) {
  580. TSerr(TS_F_TS_CHECK_NONCES, TS_R_NONCE_MISMATCH);
  581. return 0;
  582. }
  583. return 1;
  584. }
  585. /*
  586. * Check if the specified TSA name matches either the subject or one of the
  587. * subject alternative names of the TSA certificate.
  588. */
  589. static int ts_check_signer_name(GENERAL_NAME *tsa_name, X509 *signer)
  590. {
  591. STACK_OF(GENERAL_NAME) *gen_names = NULL;
  592. int idx = -1;
  593. int found = 0;
  594. if (tsa_name->type == GEN_DIRNAME
  595. && X509_name_cmp(tsa_name->d.dirn, X509_get_subject_name(signer)) == 0)
  596. return 1;
  597. gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name, NULL, &idx);
  598. while (gen_names != NULL) {
  599. found = ts_find_name(gen_names, tsa_name) >= 0;
  600. if (found)
  601. break;
  602. /*
  603. * Get the next subject alternative name, although there should be no
  604. * more than one.
  605. */
  606. GENERAL_NAMES_free(gen_names);
  607. gen_names = X509_get_ext_d2i(signer, NID_subject_alt_name, NULL, &idx);
  608. }
  609. GENERAL_NAMES_free(gen_names);
  610. return found;
  611. }
  612. /* Returns 1 if name is in gen_names, 0 otherwise. */
  613. static int ts_find_name(STACK_OF(GENERAL_NAME) *gen_names, GENERAL_NAME *name)
  614. {
  615. int i, found;
  616. for (i = 0, found = 0; !found && i < sk_GENERAL_NAME_num(gen_names); ++i) {
  617. GENERAL_NAME *current = sk_GENERAL_NAME_value(gen_names, i);
  618. found = GENERAL_NAME_cmp(current, name) == 0;
  619. }
  620. return found ? i - 1 : -1;
  621. }