torcert.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* Copyright (c) 2014, The Tor Project, Inc. */
  2. /* See LICENSE for licensing information */
  3. #include "crypto.h"
  4. #include "torcert.h"
  5. #include "ed25519_cert.h"
  6. #include "torlog.h"
  7. #include "util.h"
  8. #include "compat.h"
  9. #include "link_handshake.h"
  10. /** Helper for tor_cert_create(): signs any 32 bytes, not just an ed25519
  11. * key.
  12. */
  13. static tor_cert_t *
  14. tor_cert_sign_impl(const ed25519_keypair_t *signing_key,
  15. uint8_t cert_type,
  16. uint8_t signed_key_type,
  17. const uint8_t signed_key_info[32],
  18. time_t now, time_t lifetime,
  19. uint32_t flags)
  20. {
  21. tor_cert_t *torcert = NULL;
  22. ed25519_cert_t *cert = ed25519_cert_new();
  23. cert->cert_type = cert_type;
  24. cert->exp_field = (uint32_t) CEIL_DIV(now + lifetime, 3600);
  25. cert->cert_key_type = signed_key_type;
  26. memcpy(cert->certified_key, signed_key_info, 32);
  27. if (flags & CERT_FLAG_INCLUDE_SIGNING_KEY) {
  28. ed25519_cert_extension_t *ext = ed25519_cert_extension_new();
  29. ext->ext_type = CERTEXT_SIGNED_WITH_KEY;
  30. memcpy(ext->un_signing_key, signing_key->pubkey.pubkey, 32);
  31. ed25519_cert_add_ext(cert, ext);
  32. ++cert->n_extensions;
  33. }
  34. const ssize_t alloc_len = ed25519_cert_encoded_len(cert);
  35. tor_assert(alloc_len > 0);
  36. uint8_t *encoded = tor_malloc(alloc_len);
  37. const ssize_t real_len = ed25519_cert_encode(encoded, alloc_len, cert);
  38. if (real_len < 0)
  39. goto err;
  40. tor_assert(real_len == alloc_len);
  41. tor_assert(real_len > ED25519_SIG_LEN);
  42. uint8_t *sig = encoded + (real_len - ED25519_SIG_LEN);
  43. tor_assert(tor_mem_is_zero((char*)sig, ED25519_SIG_LEN));
  44. ed25519_signature_t signature;
  45. if (ed25519_sign(&signature, encoded,
  46. real_len-ED25519_SIG_LEN, signing_key)<0) {
  47. log_warn(LD_BUG, "Can't sign certificate");
  48. goto err;
  49. }
  50. memcpy(sig, signature.sig, ED25519_SIG_LEN);
  51. torcert = tor_cert_parse(encoded, real_len);
  52. if (! torcert) {
  53. log_warn(LD_BUG, "Generated a certificate we cannot parse");
  54. goto err;
  55. }
  56. if (tor_cert_checksig(torcert, &signing_key->pubkey, now) < 0) {
  57. log_warn(LD_BUG, "Generated a certificate whose signature we can't check");
  58. goto err;
  59. }
  60. tor_free(encoded);
  61. return torcert;
  62. err:
  63. tor_cert_free(torcert);
  64. ed25519_cert_free(cert);
  65. tor_free(encoded);
  66. return NULL;
  67. }
  68. /**
  69. * Create and return a new new certificate of type <b>cert_type</b> to
  70. * authenticate <b>signed_key</b> using the key <b>signing_key</b>. The
  71. * certificate should remain valid for at least <b>lifetime</b> seconds after
  72. * <b>now</b>.
  73. *
  74. * If CERT_FLAG_INCLUDE_SIGNING_KEY is set in <b>flags</b>, embed
  75. * the public part of <b>signing_key</b> in the certificate.
  76. */
  77. tor_cert_t *
  78. tor_cert_create(const ed25519_keypair_t *signing_key,
  79. uint8_t cert_type,
  80. const ed25519_public_key_t *signed_key,
  81. time_t now, time_t lifetime,
  82. uint32_t flags)
  83. {
  84. return tor_cert_sign_impl(signing_key, cert_type,
  85. SIGNED_KEY_TYPE_ED25519, signed_key->pubkey,
  86. now, lifetime, flags);
  87. }
  88. /** Release all storage held for <b>cert</>. */
  89. void
  90. tor_cert_free(tor_cert_t *cert)
  91. {
  92. if (! cert)
  93. return;
  94. if (cert->encoded)
  95. memwipe(cert->encoded, 0, cert->encoded_len);
  96. tor_free(cert->encoded);
  97. memwipe(cert, 0, sizeof(tor_cert_t));
  98. tor_free(cert);
  99. }
  100. /** Parse a certificate encoded with <b>len</b> bytes in <b>encoded</b>. */
  101. tor_cert_t *
  102. tor_cert_parse(const uint8_t *encoded, const size_t len)
  103. {
  104. tor_cert_t *cert = NULL;
  105. ed25519_cert_t *parsed = NULL;
  106. ssize_t got_len = ed25519_cert_parse(&parsed, encoded, len);
  107. if (got_len < 0 || (size_t) got_len != len)
  108. goto err;
  109. cert = tor_malloc_zero(sizeof(tor_cert_t));
  110. cert->encoded = tor_memdup(encoded, len);
  111. cert->encoded_len = len;
  112. memcpy(cert->signed_key.pubkey, parsed->certified_key, 32);
  113. cert->valid_until = parsed->exp_field * 3600;
  114. cert->cert_type = parsed->cert_type;
  115. for (unsigned i = 0; i < ed25519_cert_getlen_ext(parsed); ++i) {
  116. ed25519_cert_extension_t *ext = ed25519_cert_get_ext(parsed, i);
  117. if (ext->ext_type == CERTEXT_SIGNED_WITH_KEY) {
  118. if (cert->signing_key_included)
  119. goto err;
  120. cert->signing_key_included = 1;
  121. memcpy(cert->signing_key.pubkey, ext->un_signing_key, 32);
  122. } else if (ext->ext_flags & CERTEXT_FLAG_AFFECTS_VALIDATION) {
  123. /* Unrecognized extension with affects_validation set */
  124. goto err;
  125. }
  126. }
  127. return cert;
  128. err:
  129. ed25519_cert_free(parsed);
  130. tor_cert_free(cert);
  131. return NULL;
  132. }
  133. /** Fill in <b>checkable_out</b> with the information needed to check
  134. * the signature on <b>cert</b> with <b>pubkey</b>. */
  135. int
  136. tor_cert_get_checkable_sig(ed25519_checkable_t *checkable_out,
  137. const tor_cert_t *cert,
  138. const ed25519_public_key_t *pubkey)
  139. {
  140. if (! pubkey) {
  141. if (cert->signing_key_included)
  142. pubkey = &cert->signing_key;
  143. else
  144. return -1;
  145. }
  146. checkable_out->msg = cert->encoded;
  147. checkable_out->pubkey = pubkey;
  148. tor_assert(cert->encoded_len > ED25519_SIG_LEN);
  149. const size_t signed_len = cert->encoded_len - ED25519_SIG_LEN;
  150. checkable_out->len = signed_len;
  151. memcpy(checkable_out->signature.sig,
  152. cert->encoded + signed_len, ED25519_SIG_LEN);
  153. return 0;
  154. }
  155. /** Validates the signature on <b>cert</b> with <b>pubkey</b> relative to
  156. * the current time <b>now</b>. Return 0 on success, -1 on failure.
  157. * Sets flags in <b>cert</b> as appropriate.
  158. */
  159. int
  160. tor_cert_checksig(tor_cert_t *cert,
  161. const ed25519_public_key_t *pubkey, time_t now)
  162. {
  163. ed25519_checkable_t checkable;
  164. int okay;
  165. if (now > cert->valid_until) {
  166. cert->cert_expired = 1;
  167. return -1;
  168. }
  169. if (tor_cert_get_checkable_sig(&checkable, cert, pubkey) < 0)
  170. return -1;
  171. if (ed25519_checksig_batch(&okay, &checkable, 1) < 0) {
  172. cert->sig_bad = 1;
  173. return -1;
  174. } else {
  175. cert->sig_ok = 1;
  176. memcpy(cert->signing_key.pubkey, checkable.pubkey->pubkey, 32);
  177. cert->cert_valid = 1;
  178. return 0;
  179. }
  180. }
  181. /** Return a new copy of <b>cert</b> */
  182. tor_cert_t *
  183. tor_cert_dup(const tor_cert_t *cert)
  184. {
  185. tor_cert_t *newcert = tor_memdup(cert, sizeof(tor_cert_t));
  186. if (cert->encoded)
  187. newcert->encoded = tor_memdup(cert->encoded, cert->encoded_len);
  188. return newcert;
  189. }
  190. /** Return true iff cert1 and cert2 are the same cert. */
  191. int
  192. tor_cert_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  193. {
  194. tor_assert(cert1);
  195. tor_assert(cert2);
  196. return cert1->encoded_len == cert2->encoded_len &&
  197. tor_memeq(cert1->encoded, cert2->encoded, cert1->encoded_len);
  198. }
  199. /** Return true iff cert1 and cert2 are the same cert, or if they are both
  200. * NULL. */
  201. int
  202. tor_cert_opt_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  203. {
  204. if (cert1 == NULL && cert2 == NULL)
  205. return 1;
  206. if (!cert1 || !cert2)
  207. return 0;
  208. return tor_cert_eq(cert1, cert2);
  209. }
  210. /** Create new cross-certification object to certify <b>ed_key</b> as the
  211. * master ed25519 identity key for the RSA identity key <b>rsa_key</b>.
  212. * Allocates and stores the encoded certificate in *<b>cert</b>, and returns
  213. * the number of bytes stored. Returns negative on error.*/
  214. ssize_t
  215. tor_make_rsa_ed25519_crosscert(const ed25519_public_key_t *ed_key,
  216. const crypto_pk_t *rsa_key,
  217. time_t expires,
  218. uint8_t **cert)
  219. {
  220. uint8_t *res;
  221. rsa_ed_crosscert_t *cc = rsa_ed_crosscert_new();
  222. memcpy(cc->ed_key, ed_key->pubkey, ED25519_PUBKEY_LEN);
  223. cc->expiration = (uint32_t) CEIL_DIV(expires, 3600);
  224. cc->sig_len = crypto_pk_keysize(rsa_key);
  225. rsa_ed_crosscert_setlen_sig(cc, crypto_pk_keysize(rsa_key));
  226. ssize_t alloc_sz = rsa_ed_crosscert_encoded_len(cc);
  227. tor_assert(alloc_sz > 0);
  228. res = tor_malloc_zero(alloc_sz);
  229. ssize_t sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  230. tor_assert(sz > 0 && sz <= alloc_sz);
  231. const int signed_part_len = 32 + 4;
  232. int siglen = crypto_pk_private_sign(rsa_key,
  233. (char*)rsa_ed_crosscert_getarray_sig(cc),
  234. rsa_ed_crosscert_getlen_sig(cc),
  235. (char*)res, signed_part_len);
  236. tor_assert(siglen > 0 && siglen <= (int)crypto_pk_keysize(rsa_key));
  237. tor_assert(siglen <= UINT8_MAX);
  238. cc->sig_len = siglen;
  239. rsa_ed_crosscert_setlen_sig(cc, siglen);
  240. sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  241. rsa_ed_crosscert_free(cc);
  242. *cert = res;
  243. return sz;
  244. }