ts_verify_ctx.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 "internal/cryptlib.h"
  10. #include <openssl/objects.h>
  11. #include <openssl/ts.h>
  12. #include "ts_local.h"
  13. DEFINE_STACK_OF(X509)
  14. TS_VERIFY_CTX *TS_VERIFY_CTX_new(void)
  15. {
  16. TS_VERIFY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx));
  17. if (ctx == NULL)
  18. TSerr(TS_F_TS_VERIFY_CTX_NEW, ERR_R_MALLOC_FAILURE);
  19. return ctx;
  20. }
  21. void TS_VERIFY_CTX_init(TS_VERIFY_CTX *ctx)
  22. {
  23. OPENSSL_assert(ctx != NULL);
  24. memset(ctx, 0, sizeof(*ctx));
  25. }
  26. void TS_VERIFY_CTX_free(TS_VERIFY_CTX *ctx)
  27. {
  28. if (!ctx)
  29. return;
  30. TS_VERIFY_CTX_cleanup(ctx);
  31. OPENSSL_free(ctx);
  32. }
  33. int TS_VERIFY_CTX_add_flags(TS_VERIFY_CTX *ctx, int f)
  34. {
  35. ctx->flags |= f;
  36. return ctx->flags;
  37. }
  38. int TS_VERIFY_CTX_set_flags(TS_VERIFY_CTX *ctx, int f)
  39. {
  40. ctx->flags = f;
  41. return ctx->flags;
  42. }
  43. BIO *TS_VERIFY_CTX_set_data(TS_VERIFY_CTX *ctx, BIO *b)
  44. {
  45. ctx->data = b;
  46. return ctx->data;
  47. }
  48. X509_STORE *TS_VERIFY_CTX_set_store(TS_VERIFY_CTX *ctx, X509_STORE *s)
  49. {
  50. ctx->store = s;
  51. return ctx->store;
  52. }
  53. STACK_OF(X509) *TS_VERIFY_CTX_set_certs(TS_VERIFY_CTX *ctx,
  54. STACK_OF(X509) *certs)
  55. {
  56. ctx->certs = certs;
  57. return ctx->certs;
  58. }
  59. unsigned char *TS_VERIFY_CTX_set_imprint(TS_VERIFY_CTX *ctx,
  60. unsigned char *hexstr, long len)
  61. {
  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 = OPENSSL_malloc(ret->imprint_len)) == NULL)
  106. goto err;
  107. memcpy(ret->imprint, ASN1_STRING_get0_data(msg), ret->imprint_len);
  108. if ((nonce = req->nonce) != NULL) {
  109. if ((ret->nonce = ASN1_INTEGER_dup(nonce)) == NULL)
  110. goto err;
  111. } else
  112. ret->flags &= ~TS_VFY_NONCE;
  113. return ret;
  114. err:
  115. if (ctx)
  116. TS_VERIFY_CTX_cleanup(ctx);
  117. else
  118. TS_VERIFY_CTX_free(ret);
  119. return NULL;
  120. }