ts_rsp_verify.c 21 KB

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