ct_policy.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 "internal/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. OSSL_TIME now;
  28. if (ctx == NULL)
  29. return NULL;
  30. ctx->libctx = libctx;
  31. if (propq != NULL) {
  32. ctx->propq = OPENSSL_strdup(propq);
  33. if (ctx->propq == NULL) {
  34. OPENSSL_free(ctx);
  35. return NULL;
  36. }
  37. }
  38. now = ossl_time_add(ossl_time_now(),
  39. ossl_seconds2time(SCT_CLOCK_DRIFT_TOLERANCE));
  40. ctx->epoch_time_in_ms = ossl_time2ms(now);
  41. return ctx;
  42. }
  43. CT_POLICY_EVAL_CTX *CT_POLICY_EVAL_CTX_new(void)
  44. {
  45. return CT_POLICY_EVAL_CTX_new_ex(NULL, NULL);
  46. }
  47. void CT_POLICY_EVAL_CTX_free(CT_POLICY_EVAL_CTX *ctx)
  48. {
  49. if (ctx == NULL)
  50. return;
  51. X509_free(ctx->cert);
  52. X509_free(ctx->issuer);
  53. OPENSSL_free(ctx->propq);
  54. OPENSSL_free(ctx);
  55. }
  56. int CT_POLICY_EVAL_CTX_set1_cert(CT_POLICY_EVAL_CTX *ctx, X509 *cert)
  57. {
  58. if (!X509_up_ref(cert))
  59. return 0;
  60. ctx->cert = cert;
  61. return 1;
  62. }
  63. int CT_POLICY_EVAL_CTX_set1_issuer(CT_POLICY_EVAL_CTX *ctx, X509 *issuer)
  64. {
  65. if (!X509_up_ref(issuer))
  66. return 0;
  67. ctx->issuer = issuer;
  68. return 1;
  69. }
  70. void CT_POLICY_EVAL_CTX_set_shared_CTLOG_STORE(CT_POLICY_EVAL_CTX *ctx,
  71. CTLOG_STORE *log_store)
  72. {
  73. ctx->log_store = log_store;
  74. }
  75. void CT_POLICY_EVAL_CTX_set_time(CT_POLICY_EVAL_CTX *ctx, uint64_t time_in_ms)
  76. {
  77. ctx->epoch_time_in_ms = time_in_ms;
  78. }
  79. X509* CT_POLICY_EVAL_CTX_get0_cert(const CT_POLICY_EVAL_CTX *ctx)
  80. {
  81. return ctx->cert;
  82. }
  83. X509* CT_POLICY_EVAL_CTX_get0_issuer(const CT_POLICY_EVAL_CTX *ctx)
  84. {
  85. return ctx->issuer;
  86. }
  87. const CTLOG_STORE *CT_POLICY_EVAL_CTX_get0_log_store(const CT_POLICY_EVAL_CTX *ctx)
  88. {
  89. return ctx->log_store;
  90. }
  91. uint64_t CT_POLICY_EVAL_CTX_get_time(const CT_POLICY_EVAL_CTX *ctx)
  92. {
  93. return ctx->epoch_time_in_ms;
  94. }