2
0

ct_policy.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. * Copyright 2016-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. #ifdef OPENSSL_NO_CT
  10. # error "CT is disabled"
  11. #endif
  12. #include <openssl/ct.h>
  13. #include <openssl/err.h>
  14. #include <time.h>
  15. #include "ct_local.h"
  16. /*
  17. * Number of seconds in the future that an SCT timestamp can be, by default,
  18. * without being considered invalid. This is added to time() when setting a
  19. * default value for CT_POLICY_EVAL_CTX.epoch_time_in_ms.
  20. * It can be overridden by calling CT_POLICY_EVAL_CTX_set_time().
  21. */
  22. static const time_t SCT_CLOCK_DRIFT_TOLERANCE = 300;
  23. CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new_ex(OSSL_LIB_CTX *libctx,
  24. const char *propq)
  25. {
  26. CT_POLICY_EVAL_CTX *ctx = OPENSSL_zalloc(sizeof(CT_POLICY_EVAL_CTX));
  27. if (ctx == NULL) {
  28. ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
  29. return NULL;
  30. }
  31. ctx->libctx = libctx;
  32. if (propq != NULL) {
  33. ctx->propq = OPENSSL_strdup(propq);
  34. if (ctx->propq == NULL) {
  35. ERR_raise(ERR_LIB_CT, ERR_R_MALLOC_FAILURE);
  36. OPENSSL_free(ctx);
  37. return NULL;
  38. }
  39. }
  40. /* time(NULL) shouldn't ever fail, so don't bother checking for -1. */
  41. ctx->epoch_time_in_ms = (uint64_t)(time(NULL) + SCT_CLOCK_DRIFT_TOLERANCE) *
  42. 1000;
  43. return ctx;
  44. }
  45. CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void)
  46. {
  47. return CT_POLICY_EVAL_CTX_new_ex(NULL, NULL);
  48. }
  49. void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx)
  50. {
  51. if (ctx == NULL)
  52. return;
  53. X509_free(ctx->cert);
  54. X509_free(ctx->issuer);
  55. OPENSSL_free(ctx->propq);
  56. OPENSSL_free(ctx);
  57. }
  58. int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert)
  59. {
  60. if (!X509_up_ref(cert))
  61. return 0;
  62. ctx->cert = cert;
  63. return 1;
  64. }
  65. int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer)
  66. {
  67. if (!X509_up_ref(issuer))
  68. return 0;
  69. ctx->issuer = issuer;
  70. return 1;
  71. }
  72. void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx,
  73. CTLOG_STORE *log_store)
  74. {
  75. ctx->log_store = log_store;
  76. }
  77. void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms)
  78. {
  79. ctx->epoch_time_in_ms = time_in_ms;
  80. }
  81. X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx)
  82. {
  83. return ctx->cert;
  84. }
  85. X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx)
  86. {
  87. return ctx->issuer;
  88. }
  89. const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx)
  90. {
  91. return ctx->log_store;
  92. }
  93. uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx)
  94. {
  95. return ctx->epoch_time_in_ms;
  96. }