torcert.c 9.7 KB

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