test_x509.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /* Copyright (c) 2010-2018, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #define TOR_X509_PRIVATE
  4. #include "orconfig.h"
  5. #ifdef _WIN32
  6. #include <winsock2.h>
  7. #endif
  8. #include <math.h>
  9. #include <stddef.h>
  10. #include "lib/cc/compat_compiler.h"
  11. #include "core/or/or.h"
  12. #include "lib/log/log.h"
  13. #include "app/config/config.h"
  14. #include "lib/tls/x509.h"
  15. #include "lib/tls/x509_internal.h"
  16. #include "app/config/or_state_st.h"
  17. #include "test/test.h"
  18. #include "test/log_test_helpers.h"
  19. #include "tinytest.h"
  20. /* A mock replacement for crypto_digest that always fails. */
  21. static int
  22. mock_failing_digest(char *digest, const char *m, size_t len)
  23. {
  24. (void)digest;
  25. (void)m;
  26. (void)len;
  27. return -1;
  28. }
  29. static void
  30. test_x509_cert_new_failing_digest(void *arg)
  31. {
  32. (void)arg;
  33. crypto_pk_t *pk1=NULL, *pk2=NULL;
  34. tor_x509_cert_impl_t *impl = NULL;
  35. tor_x509_cert_t *cert = NULL;
  36. pk1 = pk_generate(0);
  37. pk2 = pk_generate(1);
  38. impl = tor_tls_create_certificate(pk1, pk2, "hello", "world", 86400*100);
  39. tt_assert(impl);
  40. MOCK(crypto_digest, mock_failing_digest);
  41. setup_full_capture_of_logs(LOG_WARN);
  42. cert = tor_x509_cert_new(impl);
  43. tt_assert(!cert);
  44. expect_log_msg_containing("Couldn't wrap encoded X509 certificate");
  45. expect_log_msg_containing("unable to compute digests of certificate key");
  46. done:
  47. crypto_pk_free(pk1);
  48. crypto_pk_free(pk2);
  49. UNMOCK(crypto_digest);
  50. teardown_capture_of_logs();
  51. }
  52. static tor_x509_cert_t *
  53. cert_from_der64(const char *der64)
  54. {
  55. size_t der64len = strlen(der64);
  56. unsigned char *der = tor_malloc_zero(der64len);
  57. int derlen;
  58. tor_x509_cert_t *cert = NULL;
  59. derlen = base64_decode((char*)der, der64len,
  60. der64, der64len);
  61. if (derlen >= 0)
  62. cert = tor_x509_cert_decode(der, derlen);
  63. tor_free(der);
  64. return cert;
  65. }
  66. static void
  67. test_x509_consume_ec_cert(void *arg)
  68. {
  69. (void)arg;
  70. /* This is a small self-signed EC certificate. */
  71. const char certificate[] =
  72. "MIIBEzCBugIJAIdl5svgOZ0OMAoGCCqGSM49BAMCMBIxEDAOBgNVBAMMB1Rlc3Rp\n"
  73. "bmcwHhcNMTgwODIzMTcyMzI1WhcNMTkwODIzMTcyMzI1WjASMRAwDgYDVQQDDAdU\n"
  74. "ZXN0aW5nMFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAExMDpnRc0Btic3tIyCKNE\n"
  75. "iNY4j4gzcaYzS2sTYRoVK3RAukG29Qg6/c8e8XcnsSquU4fItYxDRbi/3nhYk4CP\n"
  76. "GDAKBggqhkjOPQQDAgNIADBFAiA0h1q03C2xlONUgAOonJLrlV1SUtMeKDxNsxsU\n"
  77. "+FSPvQIhAM7kY9Tlt0ELmyMnORPp1VJieXn/qhL5VoxGxSedTbny\n";
  78. const time_t now = 1535045321; /* when I'm writing this test. */
  79. tor_x509_cert_t *cert = cert_from_der64(certificate);
  80. crypto_pk_t *key = NULL;
  81. tt_assert(cert);
  82. key = tor_tls_cert_get_key(cert);
  83. tt_ptr_op(NULL, OP_EQ, key); // Can't get an RSA key out of an EC cert.
  84. /* It's a self-signed cert -- make sure it signed itself. */
  85. tt_assert(tor_tls_cert_is_valid(LOG_ERR, cert, cert, now, 0));
  86. /* Make sure we detect its key as non-RSA1024 */
  87. setup_capture_of_logs(LOG_INFO);
  88. tt_assert(! tor_tls_cert_is_valid(LOG_INFO, cert, cert, now, 1));
  89. expect_log_msg_containing("Key is not RSA1024");
  90. done:
  91. tor_x509_cert_free(cert);
  92. crypto_pk_free(key);
  93. teardown_capture_of_logs();
  94. }
  95. static void
  96. test_x509_reject_tiny_keys(void *arg)
  97. {
  98. (void)arg;
  99. const char *certificates[] = {
  100. /* Self-signed RSA512 */
  101. "MIIBXDCCAQYCCQDKikjJYZI5uDANBgkqhkiG9w0BAQsFADA1MRUwEwYDVQQHDAxE\n"
  102. "ZWZhdWx0IENpdHkxHDAaBgNVBAoME0RlZmF1bHQgQ29tcGFueSBMdGQwHhcNMTgw\n"
  103. "ODIzMTczNjQ4WhcNMTkwODIzMTczNjQ4WjA1MRUwEwYDVQQHDAxEZWZhdWx0IENp\n"
  104. "dHkxHDAaBgNVBAoME0RlZmF1bHQgQ29tcGFueSBMdGQwXDANBgkqhkiG9w0BAQEF\n"
  105. "AANLADBIAkEAqOvVKzrSpmKOTNqDzBG/iZrUdhCrMRsymFXyIScJcdsyn7jB8RMy\n"
  106. "fbHqG8EqB8HHLU/eqt/+zhh2w08Lx3+5QwIDAQABMA0GCSqGSIb3DQEBCwUAA0EA\n"
  107. "RSCq0sNbD9uWfcBqF0U4MtfFjU5x+RQQCeBVtAzwC9bggSILKZfB9XUvtGh6vqig\n",
  108. /* Self-signed secp112r2 */
  109. "MIIBLTCB+QIJAI0LtN9uWxy3MAoGCCqGSM49BAMCMEUxCzAJBgNVBAYTAkFVMRMw\n"
  110. "EQYDVQQIDApTb21lLVN0YXRlMSEwHwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0\n"
  111. "eSBMdGQwHhcNMTgwODIzMTc0MTQ4WhcNMTkwODIzMTc0MTQ4WjBFMQswCQYDVQQG\n"
  112. "EwJBVTETMBEGA1UECAwKU29tZS1TdGF0ZTEhMB8GA1UECgwYSW50ZXJuZXQgV2lk\n"
  113. "Z2l0cyBQdHkgTHRkMDIwEAYHKoZIzj0CAQYFK4EEAAcDHgAEf7dFHo7xhCtIcgyo\n"
  114. "Px+IDcUUlntZCtar6V4O0zAKBggqhkjOPQQDAgMjADAgAg4yhBJMEmpkNbZU95Zf\n"
  115. "uwIOJAan4J1ETxUII1RrGmw=\n"
  116. };
  117. const time_t now = 1535046182;
  118. tor_x509_cert_t *cert = NULL;
  119. unsigned i;
  120. for (i = 0; i < ARRAY_LENGTH(certificates); ++i) {
  121. cert = cert_from_der64(certificates[i]);
  122. /* It might parse okay, depending on our version of NSS or OpenSSL. */
  123. if (cert == NULL)
  124. continue;
  125. /* But it should not validate. */
  126. tt_assert(! tor_tls_cert_is_valid(LOG_INFO, cert, cert, now, 0));
  127. tor_x509_cert_free(cert);
  128. }
  129. done:
  130. tor_x509_cert_free(cert);
  131. }
  132. static void
  133. test_x509_expiration(void *arg)
  134. {
  135. (void)arg;
  136. /* a 365-day RSA2048 cert, created between 0 and 60 minutes before "now" */
  137. const char certificate[] =
  138. "MIICzjCCAbYCCQDxIONWIQ9OGDANBgkqhkiG9w0BAQsFADApMQswCQYDVQQGEwJV\n"
  139. "UzEaMBgGA1UEAwwRSW50ZXJlc3RpbmcgdGltZXMwHhcNMTgwODIzMTc1NTE4WhcN\n"
  140. "MTkwODIzMTc1NTE4WjApMQswCQYDVQQGEwJVUzEaMBgGA1UEAwwRSW50ZXJlc3Rp\n"
  141. "bmcgdGltZXMwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQD0Blz1fBii\n"
  142. "OffpFlzMrmfPah/vkPcNrwoyx5YiosbHErYUpqdCtfNb7rbBM5xcac1LmF9kjnOQ\n"
  143. "uAw1jsCNE82QHwWMlXOqaZCEJsnttNo0Y7yaSR/ChbGJ54XCp+Lx2acyTeH9cBWU\n"
  144. "de8/sKAQ4NqpbEP01pBH4+1mPu2MYWjVWVicUxmw0mJ3cfkJCWUzt0nC4ls8+Itk\n"
  145. "7XliKb216Z9uQXu/zD/JGkxAljnFs1jXCX4NyWz46xnJFzXbYCeyQnBz0tUbAvgg\n"
  146. "uRdryYtHzD46hd8LTXH6oK2gV64ILAhDnRb1aBjnCXxbex24XoW3hjSrKGTdNsXA\n"
  147. "RMWU/8QZaoiBAgMBAAEwDQYJKoZIhvcNAQELBQADggEBAFIYDBcbit2kOMrHECZK\n"
  148. "ctem40A3s+0ZifzZ2KLhW8dTr/2Zb6DnlqVm2iUOV4cG/o1RAn/HzkQQuWEq+oBG\n"
  149. "yOPVHudvCyGs+2ZQWudgAv9xq8N7KtZwJhnn42c2YSoreqRXDQgJqGFatyr+XdR7\n"
  150. "gdQapLI4BFbZToeXp49Nl+q9330hKaSmIYmWEZ7R/33R64PU2el7X9/apYEcuZQT\n"
  151. "+FjEqcO1lJ8/dTwM/2C1BJZqUeFTAu+ac1M+4//qyJRUUc6xSJLhiens8atWaxwL\n"
  152. "eBCT8fCY8oPOwA1eImc/yWWmWXpv8bBWVe8OeLCMKM/OZoIdFqQpqSdcyGoh/kIW\n"
  153. "Dws=\n";
  154. const time_t now = 1535046996;
  155. tor_x509_cert_t *cert = cert_from_der64(certificate);
  156. tt_assert(cert);
  157. tt_assert(tor_tls_cert_is_valid(LOG_ERR, cert, cert, now, 0));
  158. tt_assert(tor_tls_cert_is_valid(LOG_ERR, cert, cert,
  159. now-TOR_X509_FUTURE_SLOP, 0));
  160. tt_assert(tor_tls_cert_is_valid(LOG_ERR, cert, cert,
  161. now+365*86400+TOR_X509_PAST_SLOP - 3600, 0));
  162. tt_assert(! tor_tls_cert_is_valid(LOG_INFO, cert, cert,
  163. now-TOR_X509_FUTURE_SLOP - 3600, 0));
  164. tt_assert(! tor_tls_cert_is_valid(LOG_INFO, cert, cert,
  165. now+365*86400+TOR_X509_FUTURE_SLOP, 0));
  166. done:
  167. tor_x509_cert_free(cert);
  168. }
  169. #define TEST(name) { #name, test_x509_ ## name, TT_FORK, 0, NULL }
  170. struct testcase_t x509_tests[] = {
  171. TEST(cert_new_failing_digest),
  172. TEST(consume_ec_cert),
  173. TEST(reject_tiny_keys),
  174. TEST(expiration),
  175. END_OF_TESTCASES
  176. };