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 the underlying implementation for <b>cert</b> */
  50. const tor_x509_cert_impl_t *
  51. tor_x509_cert_get_impl(const tor_x509_cert_t *cert)
  52. {
  53. tor_assert(cert);
  54. return cert->cert;
  55. }
  56. /** Return a set of digests for the public key in <b>cert</b>, or NULL if this
  57. * cert's public key is not one we know how to take the digest of. */
  58. const common_digests_t *
  59. tor_x509_cert_get_id_digests(const tor_x509_cert_t *cert)
  60. {
  61. if (cert->pkey_digests_set)
  62. return &cert->pkey_digests;
  63. else
  64. return NULL;
  65. }
  66. /** Return a set of digests for the public key in <b>cert</b>. */
  67. const common_digests_t *
  68. tor_x509_cert_get_cert_digests(const tor_x509_cert_t *cert)
  69. {
  70. return &cert->cert_digests;
  71. }
  72. /** Free all storage held in <b>cert</b> */
  73. void
  74. tor_x509_cert_free_(tor_x509_cert_t *cert)
  75. {
  76. if (! cert)
  77. return;
  78. if (cert->cert)
  79. tor_x509_cert_impl_free_(cert->cert);
  80. #ifdef ENABLE_OPENSSL
  81. tor_free(cert->encoded);
  82. #endif
  83. memwipe(cert, 0x03, sizeof(*cert));
  84. /* LCOV_EXCL_BR_START since cert will never be NULL here */
  85. tor_free(cert);
  86. /* LCOV_EXCL_BR_STOP */
  87. }
  88. /**
  89. * Allocate a new tor_x509_cert_t to hold the certificate "x509_cert".
  90. *
  91. * Steals a reference to x509_cert.
  92. */
  93. MOCK_IMPL(tor_x509_cert_t *,
  94. tor_x509_cert_new,(tor_x509_cert_impl_t *x509_cert))
  95. {
  96. tor_x509_cert_t *cert;
  97. if (!x509_cert)
  98. return NULL;
  99. cert = tor_malloc_zero(sizeof(tor_x509_cert_t));
  100. cert->cert = x509_cert;
  101. if (tor_x509_cert_set_cached_der_encoding(cert) < 0)
  102. goto err;
  103. {
  104. const uint8_t *encoded=NULL;
  105. size_t encoded_len=0;
  106. tor_x509_cert_get_der(cert, &encoded, &encoded_len);
  107. tor_assert(encoded);
  108. crypto_common_digests(&cert->cert_digests, (char *)encoded, encoded_len);
  109. }
  110. {
  111. crypto_pk_t *pk = tor_tls_cert_get_key(cert);
  112. if (pk) {
  113. if (crypto_pk_get_common_digests(pk, &cert->pkey_digests) < 0) {
  114. crypto_pk_free(pk);
  115. goto err;
  116. }
  117. }
  118. cert->pkey_digests_set = 1;
  119. crypto_pk_free(pk);
  120. }
  121. return cert;
  122. err:
  123. /* LCOV_EXCL_START for the same reason as the exclusion above */
  124. tor_free(cert);
  125. log_err(LD_CRYPTO, "Couldn't wrap encoded X509 certificate.");
  126. tor_x509_cert_impl_free_(x509_cert);
  127. return NULL;
  128. /* LCOV_EXCL_STOP */
  129. }