torcert.c 8.8 KB

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