torcert.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /* Copyright (c) 2014, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "torcert.h"
  4. #include "crypto.h"
  5. #include "ed25519_cert.h"
  6. #include "torlog.h"
  7. #include "util.h"
  8. #include "compat.h"
  9. /** Helper for tor_cert_create(): signs any 32 bytes, not just an ed25519
  10. * key.
  11. */
  12. static tor_cert_t *
  13. tor_cert_sign_impl(const ed25519_keypair_t *signing_key,
  14. uint8_t cert_type,
  15. uint8_t signed_key_type,
  16. const uint8_t signed_key_info[32],
  17. time_t now, time_t lifetime,
  18. uint32_t flags)
  19. {
  20. tor_cert_t *torcert = NULL;
  21. ed25519_cert_t *cert = ed25519_cert_new();
  22. cert->cert_type = cert_type;
  23. cert->exp_field = (uint32_t) CEIL_DIV(now + lifetime, 3600);
  24. cert->cert_key_type = signed_key_type;
  25. memcpy(cert->certified_key, signed_key_info, 32);
  26. if (flags & CERT_FLAG_INCLUDE_SIGNING_KEY) {
  27. ed25519_cert_extension_t *ext = ed25519_cert_extension_new();
  28. ext->ext_type = CERTEXT_SIGNED_WITH_KEY;
  29. memcpy(ext->un_signing_key, signing_key->pubkey.pubkey, 32);
  30. ed25519_cert_add_ext(cert, ext);
  31. ++cert->n_extensions;
  32. }
  33. const ssize_t alloc_len = ed25519_cert_encoded_len(cert);
  34. tor_assert(alloc_len > 0);
  35. uint8_t *encoded = tor_malloc(alloc_len);
  36. const ssize_t real_len = ed25519_cert_encode(encoded, alloc_len, cert);
  37. if (real_len < 0)
  38. goto err;
  39. tor_assert(real_len == alloc_len);
  40. tor_assert(real_len > ED25519_SIG_LEN);
  41. uint8_t *sig = encoded + (real_len - ED25519_SIG_LEN);
  42. tor_assert(tor_mem_is_zero((char*)sig, ED25519_SIG_LEN));
  43. ed25519_signature_t signature;
  44. if (ed25519_sign(&signature, encoded,
  45. real_len-ED25519_SIG_LEN, signing_key)<0) {
  46. log_warn(LD_BUG, "Can't sign certificate");
  47. goto err;
  48. }
  49. memcpy(sig, signature.sig, ED25519_SIG_LEN);
  50. torcert = tor_cert_parse(encoded, real_len);
  51. if (! torcert) {
  52. log_warn(LD_BUG, "Generated a certificate we cannot parse");
  53. goto err;
  54. }
  55. if (tor_cert_checksig(torcert, &signing_key->pubkey, now) < 0) {
  56. log_warn(LD_BUG, "Generated a certificate whose signature we can't check");
  57. goto err;
  58. }
  59. tor_free(encoded);
  60. return torcert;
  61. err:
  62. tor_cert_free(torcert);
  63. ed25519_cert_free(cert);
  64. tor_free(encoded);
  65. return NULL;
  66. }
  67. /**
  68. * Create and return a new new certificate of type <b>cert_type</b> to
  69. * authenticate <b>signed_key</b> using the key <b>signing_key</b>. The
  70. * certificate should remain valid for at least <b>lifetime</b> seconds after
  71. * <b>now</b>.
  72. *
  73. * If CERT_FLAG_INCLUDE_SIGNING_KEY is set in <b>flags</b>, embed
  74. * the public part of <b>signing_key</b> in the certificate.
  75. */
  76. tor_cert_t *
  77. tor_cert_create(const ed25519_keypair_t *signing_key,
  78. uint8_t cert_type,
  79. const ed25519_public_key_t *signed_key,
  80. time_t now, time_t lifetime,
  81. uint32_t flags)
  82. {
  83. return tor_cert_sign_impl(signing_key, cert_type,
  84. SIGNED_KEY_TYPE_ED25519, signed_key->pubkey,
  85. now, lifetime, flags);
  86. }
  87. /** Release all storage held for <b>cert</>. */
  88. void
  89. tor_cert_free(tor_cert_t *cert)
  90. {
  91. if (! cert)
  92. return;
  93. if (cert->encoded)
  94. memwipe(cert->encoded, 0, cert->encoded_len);
  95. tor_free(cert->encoded);
  96. memwipe(cert, 0, sizeof(tor_cert_t));
  97. tor_free(cert);
  98. }
  99. /** Parse a certificate encoded with <b>len</b> bytes in <b>encoded</b>. */
  100. tor_cert_t *
  101. tor_cert_parse(const uint8_t *encoded, const size_t len)
  102. {
  103. tor_cert_t *cert = NULL;
  104. ed25519_cert_t *parsed = NULL;
  105. ssize_t got_len = ed25519_cert_parse(&parsed, encoded, len);
  106. if (got_len < 0 || (size_t) got_len != len)
  107. goto err;
  108. cert = tor_malloc_zero(sizeof(tor_cert_t));
  109. cert->encoded = tor_memdup(encoded, len);
  110. cert->encoded_len = len;
  111. memcpy(cert->signed_key.pubkey, parsed->certified_key, 32);
  112. cert->valid_until = parsed->exp_field * 3600;
  113. cert->cert_type = parsed->cert_type;
  114. for (unsigned i = 0; i < ed25519_cert_getlen_ext(parsed); ++i) {
  115. ed25519_cert_extension_t *ext = ed25519_cert_get_ext(parsed, i);
  116. if (ext->ext_type == CERTEXT_SIGNED_WITH_KEY) {
  117. if (cert->signing_key_included)
  118. goto err;
  119. cert->signing_key_included = 1;
  120. memcpy(cert->signing_key.pubkey, ext->un_signing_key, 32);
  121. } else if (ext->ext_flags & CERTEXT_FLAG_AFFECTS_VALIDATION) {
  122. /* Unrecognized extension with affects_validation set */
  123. goto err;
  124. }
  125. }
  126. return cert;
  127. err:
  128. ed25519_cert_free(parsed);
  129. tor_cert_free(cert);
  130. return NULL;
  131. }
  132. /** Fill in <b>checkable_out</b> with the information needed to check
  133. * the signature on <b>cert</b> with <b>pubkey</b>. */
  134. int
  135. tor_cert_get_checkable_sig(ed25519_checkable_t *checkable_out,
  136. const tor_cert_t *cert,
  137. const ed25519_public_key_t *pubkey)
  138. {
  139. if (! pubkey) {
  140. if (cert->signing_key_included)
  141. pubkey = &cert->signing_key;
  142. else
  143. return -1;
  144. }
  145. checkable_out->msg = cert->encoded;
  146. checkable_out->pubkey = pubkey;
  147. tor_assert(cert->encoded_len > ED25519_SIG_LEN);
  148. const size_t signed_len = cert->encoded_len - ED25519_SIG_LEN;
  149. checkable_out->len = signed_len;
  150. memcpy(checkable_out->signature.sig,
  151. cert->encoded + signed_len, ED25519_SIG_LEN);
  152. return 0;
  153. }
  154. /** Validates the signature on <b>cert</b> with <b>pubkey</b> relative to
  155. * the current time <b>now</b>. Return 0 on success, -1 on failure.
  156. * Sets flags in <b>cert</b> as appropriate.
  157. */
  158. int
  159. tor_cert_checksig(tor_cert_t *cert,
  160. const ed25519_public_key_t *pubkey, time_t now)
  161. {
  162. ed25519_checkable_t checkable;
  163. int okay;
  164. if (now > cert->valid_until) {
  165. cert->cert_expired = 1;
  166. return -1;
  167. }
  168. if (tor_cert_get_checkable_sig(&checkable, cert, pubkey) < 0)
  169. return -1;
  170. if (ed25519_checksig_batch(&okay, &checkable, 1) < 0) {
  171. cert->sig_bad = 1;
  172. return -1;
  173. } else {
  174. cert->sig_ok = 1;
  175. memcpy(cert->signing_key.pubkey, checkable.pubkey->pubkey, 32);
  176. cert->cert_valid = 1;
  177. return 0;
  178. }
  179. }