torcert.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  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 "or.h"
  10. #include "config.h"
  11. #include "crypto.h"
  12. #include "torcert.h"
  13. #include "ed25519_cert.h"
  14. #include "torlog.h"
  15. #include "util.h"
  16. #include "compat.h"
  17. #include "link_handshake.h"
  18. /** Helper for tor_cert_create(): signs any 32 bytes, not just an ed25519
  19. * key.
  20. */
  21. static tor_cert_t *
  22. tor_cert_sign_impl(const ed25519_keypair_t *signing_key,
  23. uint8_t cert_type,
  24. uint8_t signed_key_type,
  25. const uint8_t signed_key_info[32],
  26. time_t now, time_t lifetime,
  27. uint32_t flags)
  28. {
  29. tor_cert_t *torcert = NULL;
  30. ed25519_cert_t *cert = ed25519_cert_new();
  31. cert->cert_type = cert_type;
  32. cert->exp_field = (uint32_t) CEIL_DIV(now + lifetime, 3600);
  33. cert->cert_key_type = signed_key_type;
  34. memcpy(cert->certified_key, signed_key_info, 32);
  35. if (flags & CERT_FLAG_INCLUDE_SIGNING_KEY) {
  36. ed25519_cert_extension_t *ext = ed25519_cert_extension_new();
  37. ext->ext_type = CERTEXT_SIGNED_WITH_KEY;
  38. memcpy(ext->un_signing_key, signing_key->pubkey.pubkey, 32);
  39. ed25519_cert_add_ext(cert, ext);
  40. ++cert->n_extensions;
  41. }
  42. const ssize_t alloc_len = ed25519_cert_encoded_len(cert);
  43. tor_assert(alloc_len > 0);
  44. uint8_t *encoded = tor_malloc(alloc_len);
  45. const ssize_t real_len = ed25519_cert_encode(encoded, alloc_len, cert);
  46. if (real_len < 0)
  47. goto err;
  48. tor_assert(real_len == alloc_len);
  49. tor_assert(real_len > ED25519_SIG_LEN);
  50. uint8_t *sig = encoded + (real_len - ED25519_SIG_LEN);
  51. tor_assert(tor_mem_is_zero((char*)sig, ED25519_SIG_LEN));
  52. ed25519_signature_t signature;
  53. if (ed25519_sign(&signature, encoded,
  54. real_len-ED25519_SIG_LEN, signing_key)<0) {
  55. log_warn(LD_BUG, "Can't sign certificate");
  56. goto err;
  57. }
  58. memcpy(sig, signature.sig, ED25519_SIG_LEN);
  59. torcert = tor_cert_parse(encoded, real_len);
  60. if (! torcert) {
  61. log_warn(LD_BUG, "Generated a certificate we cannot parse");
  62. goto err;
  63. }
  64. if (tor_cert_checksig(torcert, &signing_key->pubkey, now) < 0) {
  65. log_warn(LD_BUG, "Generated a certificate whose signature we can't check");
  66. goto err;
  67. }
  68. tor_free(encoded);
  69. goto done;
  70. err:
  71. tor_cert_free(torcert);
  72. torcert = NULL;
  73. done:
  74. ed25519_cert_free(cert);
  75. tor_free(encoded);
  76. return torcert;
  77. }
  78. /**
  79. * Create and return a new new certificate of type <b>cert_type</b> to
  80. * authenticate <b>signed_key</b> using the key <b>signing_key</b>. The
  81. * certificate should remain valid for at least <b>lifetime</b> seconds after
  82. * <b>now</b>.
  83. *
  84. * If CERT_FLAG_INCLUDE_SIGNING_KEY is set in <b>flags</b>, embed
  85. * the public part of <b>signing_key</b> in the certificate.
  86. */
  87. tor_cert_t *
  88. tor_cert_create(const ed25519_keypair_t *signing_key,
  89. uint8_t cert_type,
  90. const ed25519_public_key_t *signed_key,
  91. time_t now, time_t lifetime,
  92. uint32_t flags)
  93. {
  94. return tor_cert_sign_impl(signing_key, cert_type,
  95. SIGNED_KEY_TYPE_ED25519, signed_key->pubkey,
  96. now, lifetime, flags);
  97. }
  98. /** Release all storage held for <b>cert</b>. */
  99. void
  100. tor_cert_free(tor_cert_t *cert)
  101. {
  102. if (! cert)
  103. return;
  104. if (cert->encoded)
  105. memwipe(cert->encoded, 0, cert->encoded_len);
  106. tor_free(cert->encoded);
  107. memwipe(cert, 0, sizeof(tor_cert_t));
  108. tor_free(cert);
  109. }
  110. /** Parse a certificate encoded with <b>len</b> bytes in <b>encoded</b>. */
  111. tor_cert_t *
  112. tor_cert_parse(const uint8_t *encoded, const size_t len)
  113. {
  114. tor_cert_t *cert = NULL;
  115. ed25519_cert_t *parsed = NULL;
  116. ssize_t got_len = ed25519_cert_parse(&parsed, encoded, len);
  117. if (got_len < 0 || (size_t) got_len != len)
  118. goto err;
  119. cert = tor_malloc_zero(sizeof(tor_cert_t));
  120. cert->encoded = tor_memdup(encoded, len);
  121. cert->encoded_len = len;
  122. memcpy(cert->signed_key.pubkey, parsed->certified_key, 32);
  123. cert->valid_until = parsed->exp_field * 3600;
  124. cert->cert_type = parsed->cert_type;
  125. for (unsigned i = 0; i < ed25519_cert_getlen_ext(parsed); ++i) {
  126. ed25519_cert_extension_t *ext = ed25519_cert_get_ext(parsed, i);
  127. if (ext->ext_type == CERTEXT_SIGNED_WITH_KEY) {
  128. if (cert->signing_key_included)
  129. goto err;
  130. cert->signing_key_included = 1;
  131. memcpy(cert->signing_key.pubkey, ext->un_signing_key, 32);
  132. } else if (ext->ext_flags & CERTEXT_FLAG_AFFECTS_VALIDATION) {
  133. /* Unrecognized extension with affects_validation set */
  134. goto err;
  135. }
  136. }
  137. goto done;
  138. err:
  139. tor_cert_free(cert);
  140. cert = NULL;
  141. done:
  142. ed25519_cert_free(parsed);
  143. return cert;
  144. }
  145. /** Fill in <b>checkable_out</b> with the information needed to check
  146. * the signature on <b>cert</b> with <b>pubkey</b>. */
  147. int
  148. tor_cert_get_checkable_sig(ed25519_checkable_t *checkable_out,
  149. const tor_cert_t *cert,
  150. const ed25519_public_key_t *pubkey)
  151. {
  152. if (! pubkey) {
  153. if (cert->signing_key_included)
  154. pubkey = &cert->signing_key;
  155. else
  156. return -1;
  157. }
  158. checkable_out->msg = cert->encoded;
  159. checkable_out->pubkey = pubkey;
  160. tor_assert(cert->encoded_len > ED25519_SIG_LEN);
  161. const size_t signed_len = cert->encoded_len - ED25519_SIG_LEN;
  162. checkable_out->len = signed_len;
  163. memcpy(checkable_out->signature.sig,
  164. cert->encoded + signed_len, ED25519_SIG_LEN);
  165. return 0;
  166. }
  167. /** Validates the signature on <b>cert</b> with <b>pubkey</b> relative to the
  168. * current time <b>now</b>. (If <b>now</b> is 0, do not check the expiration
  169. * time.) Return 0 on success, -1 on failure. Sets flags in <b>cert</b> as
  170. * appropriate.
  171. */
  172. int
  173. tor_cert_checksig(tor_cert_t *cert,
  174. const ed25519_public_key_t *pubkey, time_t now)
  175. {
  176. ed25519_checkable_t checkable;
  177. int okay;
  178. if (now && now > cert->valid_until) {
  179. cert->cert_expired = 1;
  180. return -1;
  181. }
  182. if (tor_cert_get_checkable_sig(&checkable, cert, pubkey) < 0)
  183. return -1;
  184. if (ed25519_checksig_batch(&okay, &checkable, 1) < 0) {
  185. cert->sig_bad = 1;
  186. return -1;
  187. } else {
  188. cert->sig_ok = 1;
  189. /* Only copy the checkable public key when it is different from the signing
  190. * key of the certificate to avoid undefined behavior. */
  191. if (cert->signing_key.pubkey != checkable.pubkey->pubkey) {
  192. memcpy(cert->signing_key.pubkey, checkable.pubkey->pubkey, 32);
  193. }
  194. cert->cert_valid = 1;
  195. return 0;
  196. }
  197. }
  198. /** Return a new copy of <b>cert</b> */
  199. tor_cert_t *
  200. tor_cert_dup(const tor_cert_t *cert)
  201. {
  202. tor_cert_t *newcert = tor_memdup(cert, sizeof(tor_cert_t));
  203. if (cert->encoded)
  204. newcert->encoded = tor_memdup(cert->encoded, cert->encoded_len);
  205. return newcert;
  206. }
  207. /** Return true iff cert1 and cert2 are the same cert. */
  208. int
  209. tor_cert_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  210. {
  211. tor_assert(cert1);
  212. tor_assert(cert2);
  213. return cert1->encoded_len == cert2->encoded_len &&
  214. tor_memeq(cert1->encoded, cert2->encoded, cert1->encoded_len);
  215. }
  216. /** Return true iff cert1 and cert2 are the same cert, or if they are both
  217. * NULL. */
  218. int
  219. tor_cert_opt_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  220. {
  221. if (cert1 == NULL && cert2 == NULL)
  222. return 1;
  223. if (!cert1 || !cert2)
  224. return 0;
  225. return tor_cert_eq(cert1, cert2);
  226. }
  227. #define RSA_ED_CROSSCERT_PREFIX "Tor TLS RSA/Ed25519 cross-certificate"
  228. /** Create new cross-certification object to certify <b>ed_key</b> as the
  229. * master ed25519 identity key for the RSA identity key <b>rsa_key</b>.
  230. * Allocates and stores the encoded certificate in *<b>cert</b>, and returns
  231. * the number of bytes stored. Returns negative on error.*/
  232. ssize_t
  233. tor_make_rsa_ed25519_crosscert(const ed25519_public_key_t *ed_key,
  234. const crypto_pk_t *rsa_key,
  235. time_t expires,
  236. uint8_t **cert)
  237. {
  238. uint8_t *res;
  239. rsa_ed_crosscert_t *cc = rsa_ed_crosscert_new();
  240. memcpy(cc->ed_key, ed_key->pubkey, ED25519_PUBKEY_LEN);
  241. cc->expiration = (uint32_t) CEIL_DIV(expires, 3600);
  242. cc->sig_len = crypto_pk_keysize(rsa_key);
  243. rsa_ed_crosscert_setlen_sig(cc, crypto_pk_keysize(rsa_key));
  244. ssize_t alloc_sz = rsa_ed_crosscert_encoded_len(cc);
  245. tor_assert(alloc_sz > 0);
  246. res = tor_malloc_zero(alloc_sz);
  247. ssize_t sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  248. tor_assert(sz > 0 && sz <= alloc_sz);
  249. crypto_digest_t *d = crypto_digest256_new(DIGEST_SHA256);
  250. crypto_digest_add_bytes(d, RSA_ED_CROSSCERT_PREFIX,
  251. strlen(RSA_ED_CROSSCERT_PREFIX));
  252. const int signed_part_len = 32 + 4;
  253. crypto_digest_add_bytes(d, (char*)res, signed_part_len);
  254. uint8_t digest[DIGEST256_LEN];
  255. crypto_digest_get_digest(d, (char*)digest, sizeof(digest));
  256. crypto_digest_free(d);
  257. int siglen = crypto_pk_private_sign(rsa_key,
  258. (char*)rsa_ed_crosscert_getarray_sig(cc),
  259. rsa_ed_crosscert_getlen_sig(cc),
  260. (char*)digest, sizeof(digest));
  261. tor_assert(siglen > 0 && siglen <= (int)crypto_pk_keysize(rsa_key));
  262. tor_assert(siglen <= UINT8_MAX);
  263. cc->sig_len = siglen;
  264. rsa_ed_crosscert_setlen_sig(cc, siglen);
  265. sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  266. rsa_ed_crosscert_free(cc);
  267. *cert = res;
  268. return sz;
  269. }
  270. /**
  271. * Check whether the <b>crosscert_len</b> byte certificate in <b>crosscert</b>
  272. * is in fact a correct cross-certification of <b>master_key</b> using
  273. * the RSA key <b>rsa_id_key</b>.
  274. *
  275. * Also reject the certificate if it expired before
  276. * <b>reject_if_expired_before</b>.
  277. *
  278. * Return 0 on success, negative on failure.
  279. */
  280. int
  281. rsa_ed25519_crosscert_check(const uint8_t *crosscert,
  282. const size_t crosscert_len,
  283. const crypto_pk_t *rsa_id_key,
  284. const ed25519_public_key_t *master_key,
  285. const time_t reject_if_expired_before)
  286. {
  287. rsa_ed_crosscert_t *cc = NULL;
  288. int rv;
  289. #define ERR(code, s) \
  290. do { \
  291. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  292. "Received a bad RSA->Ed25519 crosscert: %s", \
  293. (s)); \
  294. rv = (code); \
  295. goto err; \
  296. } while (0)
  297. if (BUG(crypto_pk_keysize(rsa_id_key) > PK_BYTES))
  298. return -1;
  299. if (BUG(!crosscert))
  300. return -1;
  301. ssize_t parsed_len = rsa_ed_crosscert_parse(&cc, crosscert, crosscert_len);
  302. if (parsed_len < 0 || crosscert_len != (size_t)parsed_len) {
  303. ERR(-2, "Unparseable or overlong crosscert");
  304. }
  305. if (tor_memneq(rsa_ed_crosscert_getarray_ed_key(cc),
  306. master_key->pubkey,
  307. ED25519_PUBKEY_LEN)) {
  308. ERR(-3, "Crosscert did not match Ed25519 key");
  309. }
  310. const uint32_t expiration_date = rsa_ed_crosscert_get_expiration(cc);
  311. const uint64_t expiration_time = expiration_date * 3600;
  312. if (reject_if_expired_before < 0 ||
  313. expiration_time < (uint64_t)reject_if_expired_before) {
  314. ERR(-4, "Crosscert is expired");
  315. }
  316. const uint8_t *eos = rsa_ed_crosscert_get_end_of_signed(cc);
  317. const uint8_t *sig = rsa_ed_crosscert_getarray_sig(cc);
  318. const uint8_t siglen = rsa_ed_crosscert_get_sig_len(cc);
  319. tor_assert(eos >= crosscert);
  320. tor_assert((size_t)(eos - crosscert) <= crosscert_len);
  321. tor_assert(siglen == rsa_ed_crosscert_getlen_sig(cc));
  322. /* Compute the digest */
  323. uint8_t digest[DIGEST256_LEN];
  324. crypto_digest_t *d = crypto_digest256_new(DIGEST_SHA256);
  325. crypto_digest_add_bytes(d, RSA_ED_CROSSCERT_PREFIX,
  326. strlen(RSA_ED_CROSSCERT_PREFIX));
  327. crypto_digest_add_bytes(d, (char*)crosscert, eos-crosscert);
  328. crypto_digest_get_digest(d, (char*)digest, sizeof(digest));
  329. crypto_digest_free(d);
  330. /* Now check the signature */
  331. uint8_t signed_[PK_BYTES];
  332. int signed_len = crypto_pk_public_checksig(rsa_id_key,
  333. (char*)signed_, sizeof(signed_),
  334. (char*)sig, siglen);
  335. if (signed_len < DIGEST256_LEN) {
  336. ERR(-5, "Bad signature, or length of signed data not as expected");
  337. }
  338. if (tor_memneq(digest, signed_, DIGEST256_LEN)) {
  339. ERR(-6, "The signature was good, but it didn't match the data");
  340. }
  341. rv = 0;
  342. err:
  343. rsa_ed_crosscert_free(cc);
  344. return rv;
  345. }
  346. /** Construct and return a new empty or_handshake_certs object */
  347. or_handshake_certs_t *
  348. or_handshake_certs_new(void)
  349. {
  350. return tor_malloc_zero(sizeof(or_handshake_certs_t));
  351. }
  352. /** DODCDOC */
  353. void
  354. or_handshake_certs_free(or_handshake_certs_t *certs)
  355. {
  356. if (!certs)
  357. return;
  358. tor_x509_cert_free(certs->auth_cert);
  359. tor_x509_cert_free(certs->id_cert);
  360. memwipe(certs, 0xBD, sizeof(*certs));
  361. tor_free(certs);
  362. }
  363. #undef ERR
  364. #define ERR(s) \
  365. do { \
  366. log_fn(severity, LD_PROTOCOL, \
  367. "Received a bad CERTS cell: %s", \
  368. (s)); \
  369. return 0; \
  370. } while (0)
  371. int
  372. or_handshake_certs_rsa_ok(int severity,
  373. or_handshake_certs_t *certs,
  374. tor_tls_t *tls)
  375. {
  376. tor_x509_cert_t *link_cert = certs->link_cert;
  377. tor_x509_cert_t *auth_cert = certs->auth_cert;
  378. tor_x509_cert_t *id_cert = certs->id_cert;
  379. if (certs->started_here) {
  380. if (! (id_cert && link_cert))
  381. ERR("The certs we wanted were missing");
  382. if (! tor_tls_cert_matches_key(tls, link_cert))
  383. ERR("The link certificate didn't match the TLS public key");
  384. if (! tor_tls_cert_is_valid(severity, link_cert, id_cert, 0))
  385. ERR("The link certificate was not valid");
  386. if (! tor_tls_cert_is_valid(severity, id_cert, id_cert, 1))
  387. ERR("The ID certificate was not valid");
  388. } else {
  389. if (! (id_cert && auth_cert))
  390. ERR("The certs we wanted were missing");
  391. /* Remember these certificates so we can check an AUTHENTICATE cell */
  392. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, auth_cert, id_cert, 1))
  393. ERR("The authentication certificate was not valid");
  394. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, id_cert, id_cert, 1))
  395. ERR("The ID certificate was not valid");
  396. }
  397. return 1;
  398. }
  399. int
  400. or_handshake_certs_ed25519_ok(or_handshake_certs_t *certs)
  401. {
  402. (void) certs;
  403. return 0;
  404. }