ts_verify_ctx.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 "internal/cryptlib.h"
  10. #include <openssl/objects.h>
  11. #include <openssl/ts.h>
  12. #include "ts_local.h"
  13. TS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
  14. {
  15. TS_VERIFY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  16. if (ctx == NULL)
  17. TSerr(TS_F_TS_VERIFY_CTX_NEW, ERR_R_MALLOC_FAILURE);
  18. return ctx;
  19. }
  20. void TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
  21. {
  22. OPENSSL_assert(ctx != NULL);
  23. memset(ctx, 0, sizeof(*ctx));
  24. }
  25. void TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
  26. {
  27. if (!ctx)
  28. return;
  29. TS_VERIFY_CTX_cleanup(ctx);
  30. OPENSSL_free(ctx);
  31. }
  32. int TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX *ctx, int f)
  33. {
  34. ctx->flags |= f;
  35. return ctx->flags;
  36. }
  37. int TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX *ctx, int f)
  38. {
  39. ctx->flags = f;
  40. return ctx->flags;
  41. }
  42. BIO *TS_VERIFY_CTX_set_data(TS_VERIFY_CTX *ctx, BIO *b)
  43. {
  44. ctx->data = b;
  45. return ctx->data;
  46. }
  47. X509_STORE *TS_VERIFY_CTX_set_store(TS_VERIFY_CTX *ctx, X509_STORE *s)
  48. {
  49. ctx->store = s;
  50. return ctx->store;
  51. }
  52. STACK_OF(X509) *TS_VERIFY_CTS_set_certs(TS_VERIFY_CTX *ctx,
  53. STACK_OF(X509) *certs)
  54. {
  55. ctx->certs = certs;
  56. return ctx->certs;
  57. }
  58. unsigned char *TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX *ctx,
  59. unsigned char *hexstr, long len)
  60. {
  61. OPENSSL_free(ctx->imprint);
  62. ctx->imprint = hexstr;
  63. ctx->imprint_len = len;
  64. return ctx->imprint;
  65. }
  66. void TS_VERIFY_CTX_cleanup(TS_VERIFY_CTX *ctx)
  67. {
  68. if (!ctx)
  69. return;
  70. X509_STORE_free(ctx->store);
  71. sk_X509_pop_free(ctx->certs, X509_free);
  72. ASN1_OBJECT_free(ctx->policy);
  73. X509_ALGOR_free(ctx->md_alg);
  74. OPENSSL_free(ctx->imprint);
  75. BIO_free_all(ctx->data);
  76. ASN1_INTEGER_free(ctx->nonce);
  77. GENERAL_NAME_free(ctx->tsa_name);
  78. TS_VERIFY_CTX_init(ctx);
  79. }
  80. TS_VERIFY_CTX *TS_REQ_to_TS_VERIFY_CTX(TS_REQ *req, TS_VERIFY_CTX *ctx)
  81. {
  82. TS_VERIFY_CTX *ret = ctx;
  83. ASN1_OBJECT *policy;
  84. TS_MSG_IMPRINT *imprint;
  85. X509_ALGOR *md_alg;
  86. ASN1_OCTET_STRING *msg;
  87. const ASN1_INTEGER *nonce;
  88. OPENSSL_assert(req != NULL);
  89. if (ret)
  90. TS_VERIFY_CTX_cleanup(ret);
  91. else if ((ret = TS_VERIFY_CTX_new()) == NULL)
  92. return NULL;
  93. ret->flags = TS_VFY_ALL_IMPRINT & ~(TS_VFY_TSA_NAME | TS_VFY_SIGNATURE);
  94. if ((policy = req->policy_id) != NULL) {
  95. if ((ret->policy = OBJ_dup(policy)) == NULL)
  96. goto err;
  97. } else
  98. ret->flags &= ~TS_VFY_POLICY;
  99. imprint = req->msg_imprint;
  100. md_alg = imprint->hash_algo;
  101. if ((ret->md_alg = X509_ALGOR_dup(md_alg)) == NULL)
  102. goto err;
  103. msg = imprint->hashed_msg;
  104. ret->imprint_len = ASN1_STRING_length(msg);
  105. if (ret->imprint_len <= 0)
  106. goto err;
  107. if ((ret->imprint = OPENSSL_malloc(ret->imprint_len)) == NULL)
  108. goto err;
  109. memcpy(ret->imprint, ASN1_STRING_get0_data(msg), ret->imprint_len);
  110. if ((nonce = req->nonce) != NULL) {
  111. if ((ret->nonce = ASN1_INTEGER_dup(nonce)) == NULL)
  112. goto err;
  113. } else
  114. ret->flags &= ~TS_VFY_NONCE;
  115. return ret;
  116. err:
  117. if (ctx)
  118. TS_VERIFY_CTX_cleanup(ctx);
  119. else
  120. TS_VERIFY_CTX_free(ret);
  121. return NULL;
  122. }