x509.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /* Copyright (c) 2003, Roger Dingledine.
  2. * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
  3. * Copyright (c) 2007-2018, The Tor Project, Inc. */
  4. /* See LICENSE for licensing information */
  5. /**
  6. * \file x509_openssl.c
  7. * \brief Wrapper functions to present a consistent interface to
  8. * X.509 functions.
  9. **/
  10. #define TOR_X509_PRIVATE
  11. #include "lib/tls/x509.h"
  12. #include "lib/tls/x509_internal.h"
  13. #include "lib/log/util_bug.h"
  14. #include "lib/crypt_ops/crypto_rand.h"
  15. #include "lib/crypt_ops/crypto_util.h"
  16. /** Choose the start and end times for a certificate */
  17. void
  18. tor_tls_pick_certificate_lifetime(time_t now,
  19. unsigned int cert_lifetime,
  20. time_t *start_time_out,
  21. time_t *end_time_out)
  22. {
  23. time_t start_time, end_time;
  24. /* Make sure we're part-way through the certificate lifetime, rather
  25. * than having it start right now. Don't choose quite uniformly, since
  26. * then we might pick a time where we're about to expire. Lastly, be
  27. * sure to start on a day boundary. */
  28. /* Our certificate lifetime will be cert_lifetime no matter what, but if we
  29. * start cert_lifetime in the past, we'll have 0 real lifetime. instead we
  30. * start up to (cert_lifetime - min_real_lifetime - start_granularity) in
  31. * the past. */
  32. const time_t min_real_lifetime = 24*3600;
  33. const time_t start_granularity = 24*3600;
  34. time_t earliest_start_time;
  35. /* Don't actually start in the future! */
  36. if (cert_lifetime <= min_real_lifetime + start_granularity) {
  37. earliest_start_time = now - 1;
  38. } else {
  39. earliest_start_time = now + min_real_lifetime + start_granularity
  40. - cert_lifetime;
  41. }
  42. start_time = crypto_rand_time_range(earliest_start_time, now);
  43. /* Round the start time back to the start of a day. */
  44. start_time -= start_time % start_granularity;
  45. end_time = start_time + cert_lifetime;
  46. *start_time_out = start_time;
  47. *end_time_out = end_time;
  48. }
  49. /** Return a set of digests for the public key in <b>cert</b>, or NULL if this
  50. * cert's public key is not one we know how to take the digest of. */
  51. const common_digests_t *
  52. tor_x509_cert_get_id_digests(const tor_x509_cert_t *cert)
  53. {
  54. if (cert->pkey_digests_set)
  55. return &cert->pkey_digests;
  56. else
  57. return NULL;
  58. }
  59. /** Return a set of digests for the public key in <b>cert</b>. */
  60. const common_digests_t *
  61. tor_x509_cert_get_cert_digests(const tor_x509_cert_t *cert)
  62. {
  63. return &cert->cert_digests;
  64. }
  65. /** Free all storage held in <b>cert</b> */
  66. void
  67. tor_x509_cert_free_(tor_x509_cert_t *cert)
  68. {
  69. if (! cert)
  70. return;
  71. if (cert->cert)
  72. tor_x509_cert_impl_free_(cert->cert);
  73. #ifdef ENABLE_OPENSSL
  74. tor_free(cert->encoded);
  75. #endif
  76. memwipe(cert, 0x03, sizeof(*cert));
  77. /* LCOV_EXCL_BR_START since cert will never be NULL here */
  78. tor_free(cert);
  79. /* LCOV_EXCL_BR_STOP */
  80. }
  81. /**
  82. * Allocate a new tor_x509_cert_t to hold the certificate "x509_cert".
  83. *
  84. * Steals a reference to x509_cert.
  85. */
  86. MOCK_IMPL(tor_x509_cert_t *,
  87. tor_x509_cert_new,(tor_x509_cert_impl_t *x509_cert))
  88. {
  89. tor_x509_cert_t *cert;
  90. if (!x509_cert)
  91. return NULL;
  92. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  93. cert->cert = x509_cert;
  94. if (tor_x509_cert_set_cached_der_encoding(cert) < 0)
  95. goto err;
  96. {
  97. const uint8_t *encoded=NULL;
  98. size_t encoded_len=0;
  99. tor_x509_cert_get_der(cert, &encoded, &encoded_len);
  100. tor_assert(encoded);
  101. crypto_common_digests(&cert->cert_digests, (char *)encoded, encoded_len);
  102. }
  103. {
  104. crypto_pk_t *pk = tor_tls_cert_get_key(cert);
  105. if (pk) {
  106. if (crypto_pk_get_common_digests(pk, &cert->pkey_digests) < 0) {
  107. log_warn(LD_CRYPTO, "unable to compute digests of certificate key");
  108. crypto_pk_free(pk);
  109. goto err;
  110. }
  111. }
  112. cert->pkey_digests_set = 1;
  113. crypto_pk_free(pk);
  114. }
  115. return cert;
  116. err:
  117. tor_free(cert);
  118. log_err(LD_CRYPTO, "Couldn't wrap encoded X509 certificate.");
  119. tor_x509_cert_impl_free_(x509_cert);
  120. return NULL;
  121. }
  122. /** Return a new copy of <b>cert</b>. */
  123. tor_x509_cert_t *
  124. tor_x509_cert_dup(const tor_x509_cert_t *cert)
  125. {
  126. tor_assert(cert);
  127. tor_assert(cert->cert);
  128. return tor_x509_cert_new(tor_x509_cert_impl_dup_(cert->cert));
  129. }