ts_rsp_verify.c 18 KB

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