torcert.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649
  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 "or.h"
  27. #include "config.h"
  28. #include "crypto.h"
  29. #include "torcert.h"
  30. #include "ed25519_cert.h"
  31. #include "torlog.h"
  32. #include "util.h"
  33. #include "compat.h"
  34. #include "link_handshake.h"
  35. /** Helper for tor_cert_create(): signs any 32 bytes, not just an ed25519
  36. * key.
  37. */
  38. static tor_cert_t *
  39. tor_cert_sign_impl(const ed25519_keypair_t *signing_key,
  40. uint8_t cert_type,
  41. uint8_t signed_key_type,
  42. const uint8_t signed_key_info[32],
  43. time_t now, time_t lifetime,
  44. uint32_t flags)
  45. {
  46. tor_cert_t *torcert = NULL;
  47. ed25519_cert_t *cert = ed25519_cert_new();
  48. cert->cert_type = cert_type;
  49. cert->exp_field = (uint32_t) CEIL_DIV(now + lifetime, 3600);
  50. cert->cert_key_type = signed_key_type;
  51. memcpy(cert->certified_key, signed_key_info, 32);
  52. if (flags & CERT_FLAG_INCLUDE_SIGNING_KEY) {
  53. ed25519_cert_extension_t *ext = ed25519_cert_extension_new();
  54. ext->ext_type = CERTEXT_SIGNED_WITH_KEY;
  55. memcpy(ext->un_signing_key, signing_key->pubkey.pubkey, 32);
  56. ed25519_cert_add_ext(cert, ext);
  57. ++cert->n_extensions;
  58. }
  59. const ssize_t alloc_len = ed25519_cert_encoded_len(cert);
  60. tor_assert(alloc_len > 0);
  61. uint8_t *encoded = tor_malloc(alloc_len);
  62. const ssize_t real_len = ed25519_cert_encode(encoded, alloc_len, cert);
  63. if (real_len < 0)
  64. goto err;
  65. tor_assert(real_len == alloc_len);
  66. tor_assert(real_len > ED25519_SIG_LEN);
  67. uint8_t *sig = encoded + (real_len - ED25519_SIG_LEN);
  68. tor_assert(tor_mem_is_zero((char*)sig, ED25519_SIG_LEN));
  69. ed25519_signature_t signature;
  70. if (ed25519_sign(&signature, encoded,
  71. real_len-ED25519_SIG_LEN, signing_key)<0) {
  72. log_warn(LD_BUG, "Can't sign certificate");
  73. goto err;
  74. }
  75. memcpy(sig, signature.sig, ED25519_SIG_LEN);
  76. torcert = tor_cert_parse(encoded, real_len);
  77. if (! torcert) {
  78. log_warn(LD_BUG, "Generated a certificate we cannot parse");
  79. goto err;
  80. }
  81. if (tor_cert_checksig(torcert, &signing_key->pubkey, now) < 0) {
  82. log_warn(LD_BUG, "Generated a certificate whose signature we can't check");
  83. goto err;
  84. }
  85. tor_free(encoded);
  86. goto done;
  87. err:
  88. tor_cert_free(torcert);
  89. torcert = NULL;
  90. done:
  91. ed25519_cert_free(cert);
  92. tor_free(encoded);
  93. return torcert;
  94. }
  95. /**
  96. * Create and return a new new certificate of type <b>cert_type</b> to
  97. * authenticate <b>signed_key</b> using the key <b>signing_key</b>. The
  98. * certificate should remain valid for at least <b>lifetime</b> seconds after
  99. * <b>now</b>.
  100. *
  101. * If CERT_FLAG_INCLUDE_SIGNING_KEY is set in <b>flags</b>, embed
  102. * the public part of <b>signing_key</b> in the certificate.
  103. */
  104. tor_cert_t *
  105. tor_cert_create(const ed25519_keypair_t *signing_key,
  106. uint8_t cert_type,
  107. const ed25519_public_key_t *signed_key,
  108. time_t now, time_t lifetime,
  109. uint32_t flags)
  110. {
  111. return tor_cert_sign_impl(signing_key, cert_type,
  112. SIGNED_KEY_TYPE_ED25519, signed_key->pubkey,
  113. now, lifetime, flags);
  114. }
  115. /** Release all storage held for <b>cert</b>. */
  116. void
  117. tor_cert_free(tor_cert_t *cert)
  118. {
  119. if (! cert)
  120. return;
  121. if (cert->encoded)
  122. memwipe(cert->encoded, 0, cert->encoded_len);
  123. tor_free(cert->encoded);
  124. memwipe(cert, 0, sizeof(tor_cert_t));
  125. tor_free(cert);
  126. }
  127. /** Parse a certificate encoded with <b>len</b> bytes in <b>encoded</b>. */
  128. tor_cert_t *
  129. tor_cert_parse(const uint8_t *encoded, const size_t len)
  130. {
  131. tor_cert_t *cert = NULL;
  132. ed25519_cert_t *parsed = NULL;
  133. ssize_t got_len = ed25519_cert_parse(&parsed, encoded, len);
  134. if (got_len < 0 || (size_t) got_len != len)
  135. goto err;
  136. cert = tor_malloc_zero(sizeof(tor_cert_t));
  137. cert->encoded = tor_memdup(encoded, len);
  138. cert->encoded_len = len;
  139. memcpy(cert->signed_key.pubkey, parsed->certified_key, 32);
  140. const int64_t valid_until_64 = ((int64_t)parsed->exp_field) * 3600;
  141. if (valid_until_64 > TIME_MAX)
  142. cert->valid_until = TIME_MAX - 1;
  143. else
  144. cert->valid_until = (time_t) valid_until_64;
  145. cert->cert_type = parsed->cert_type;
  146. for (unsigned i = 0; i < ed25519_cert_getlen_ext(parsed); ++i) {
  147. ed25519_cert_extension_t *ext = ed25519_cert_get_ext(parsed, i);
  148. if (ext->ext_type == CERTEXT_SIGNED_WITH_KEY) {
  149. if (cert->signing_key_included)
  150. goto err;
  151. cert->signing_key_included = 1;
  152. memcpy(cert->signing_key.pubkey, ext->un_signing_key, 32);
  153. } else if (ext->ext_flags & CERTEXT_FLAG_AFFECTS_VALIDATION) {
  154. /* Unrecognized extension with affects_validation set */
  155. goto err;
  156. }
  157. }
  158. goto done;
  159. err:
  160. tor_cert_free(cert);
  161. cert = NULL;
  162. done:
  163. ed25519_cert_free(parsed);
  164. return cert;
  165. }
  166. /** Fill in <b>checkable_out</b> with the information needed to check
  167. * the signature on <b>cert</b> with <b>pubkey</b>.
  168. *
  169. * On success, if <b>expiration_out</b> is provided, and it is some time
  170. * _after_ the expiration time of this certificate, set it to the
  171. * expiration time of this certificate.
  172. */
  173. int
  174. tor_cert_get_checkable_sig(ed25519_checkable_t *checkable_out,
  175. const tor_cert_t *cert,
  176. const ed25519_public_key_t *pubkey,
  177. time_t *expiration_out)
  178. {
  179. if (! pubkey) {
  180. if (cert->signing_key_included)
  181. pubkey = &cert->signing_key;
  182. else
  183. return -1;
  184. }
  185. checkable_out->msg = cert->encoded;
  186. checkable_out->pubkey = pubkey;
  187. tor_assert(cert->encoded_len > ED25519_SIG_LEN);
  188. const size_t signed_len = cert->encoded_len - ED25519_SIG_LEN;
  189. checkable_out->len = signed_len;
  190. memcpy(checkable_out->signature.sig,
  191. cert->encoded + signed_len, ED25519_SIG_LEN);
  192. if (expiration_out) {
  193. *expiration_out = MIN(*expiration_out, cert->valid_until);
  194. }
  195. return 0;
  196. }
  197. /** Validates the signature on <b>cert</b> with <b>pubkey</b> relative to the
  198. * current time <b>now</b>. (If <b>now</b> is 0, do not check the expiration
  199. * time.) Return 0 on success, -1 on failure. Sets flags in <b>cert</b> as
  200. * appropriate.
  201. */
  202. int
  203. tor_cert_checksig(tor_cert_t *cert,
  204. const ed25519_public_key_t *pubkey, time_t now)
  205. {
  206. ed25519_checkable_t checkable;
  207. int okay;
  208. time_t expires = TIME_MAX;
  209. if (tor_cert_get_checkable_sig(&checkable, cert, pubkey, &expires) < 0)
  210. return -1;
  211. if (now && now > expires) {
  212. cert->cert_expired = 1;
  213. return -1;
  214. }
  215. if (ed25519_checksig_batch(&okay, &checkable, 1) < 0) {
  216. cert->sig_bad = 1;
  217. return -1;
  218. } else {
  219. cert->sig_ok = 1;
  220. /* Only copy the checkable public key when it is different from the signing
  221. * key of the certificate to avoid undefined behavior. */
  222. if (cert->signing_key.pubkey != checkable.pubkey->pubkey) {
  223. memcpy(cert->signing_key.pubkey, checkable.pubkey->pubkey, 32);
  224. }
  225. cert->cert_valid = 1;
  226. return 0;
  227. }
  228. }
  229. /** Return a new copy of <b>cert</b> */
  230. tor_cert_t *
  231. tor_cert_dup(const tor_cert_t *cert)
  232. {
  233. tor_cert_t *newcert = tor_memdup(cert, sizeof(tor_cert_t));
  234. if (cert->encoded)
  235. newcert->encoded = tor_memdup(cert->encoded, cert->encoded_len);
  236. return newcert;
  237. }
  238. /** Return true iff cert1 and cert2 are the same cert. */
  239. int
  240. tor_cert_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  241. {
  242. tor_assert(cert1);
  243. tor_assert(cert2);
  244. return cert1->encoded_len == cert2->encoded_len &&
  245. tor_memeq(cert1->encoded, cert2->encoded, cert1->encoded_len);
  246. }
  247. /** Return true iff cert1 and cert2 are the same cert, or if they are both
  248. * NULL. */
  249. int
  250. tor_cert_opt_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  251. {
  252. if (cert1 == NULL && cert2 == NULL)
  253. return 1;
  254. if (!cert1 || !cert2)
  255. return 0;
  256. return tor_cert_eq(cert1, cert2);
  257. }
  258. #define RSA_ED_CROSSCERT_PREFIX "Tor TLS RSA/Ed25519 cross-certificate"
  259. /** Create new cross-certification object to certify <b>ed_key</b> as the
  260. * master ed25519 identity key for the RSA identity key <b>rsa_key</b>.
  261. * Allocates and stores the encoded certificate in *<b>cert</b>, and returns
  262. * the number of bytes stored. Returns negative on error.*/
  263. ssize_t
  264. tor_make_rsa_ed25519_crosscert(const ed25519_public_key_t *ed_key,
  265. const crypto_pk_t *rsa_key,
  266. time_t expires,
  267. uint8_t **cert)
  268. {
  269. uint8_t *res;
  270. rsa_ed_crosscert_t *cc = rsa_ed_crosscert_new();
  271. memcpy(cc->ed_key, ed_key->pubkey, ED25519_PUBKEY_LEN);
  272. cc->expiration = (uint32_t) CEIL_DIV(expires, 3600);
  273. cc->sig_len = crypto_pk_keysize(rsa_key);
  274. rsa_ed_crosscert_setlen_sig(cc, crypto_pk_keysize(rsa_key));
  275. ssize_t alloc_sz = rsa_ed_crosscert_encoded_len(cc);
  276. tor_assert(alloc_sz > 0);
  277. res = tor_malloc_zero(alloc_sz);
  278. ssize_t sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  279. tor_assert(sz > 0 && sz <= alloc_sz);
  280. crypto_digest_t *d = crypto_digest256_new(DIGEST_SHA256);
  281. crypto_digest_add_bytes(d, RSA_ED_CROSSCERT_PREFIX,
  282. strlen(RSA_ED_CROSSCERT_PREFIX));
  283. const int signed_part_len = 32 + 4;
  284. crypto_digest_add_bytes(d, (char*)res, signed_part_len);
  285. uint8_t digest[DIGEST256_LEN];
  286. crypto_digest_get_digest(d, (char*)digest, sizeof(digest));
  287. crypto_digest_free(d);
  288. int siglen = crypto_pk_private_sign(rsa_key,
  289. (char*)rsa_ed_crosscert_getarray_sig(cc),
  290. rsa_ed_crosscert_getlen_sig(cc),
  291. (char*)digest, sizeof(digest));
  292. tor_assert(siglen > 0 && siglen <= (int)crypto_pk_keysize(rsa_key));
  293. tor_assert(siglen <= UINT8_MAX);
  294. cc->sig_len = siglen;
  295. rsa_ed_crosscert_setlen_sig(cc, siglen);
  296. sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  297. rsa_ed_crosscert_free(cc);
  298. *cert = res;
  299. return sz;
  300. }
  301. /**
  302. * Check whether the <b>crosscert_len</b> byte certificate in <b>crosscert</b>
  303. * is in fact a correct cross-certification of <b>master_key</b> using
  304. * the RSA key <b>rsa_id_key</b>.
  305. *
  306. * Also reject the certificate if it expired before
  307. * <b>reject_if_expired_before</b>.
  308. *
  309. * Return 0 on success, negative on failure.
  310. */
  311. int
  312. rsa_ed25519_crosscert_check(const uint8_t *crosscert,
  313. const size_t crosscert_len,
  314. const crypto_pk_t *rsa_id_key,
  315. const ed25519_public_key_t *master_key,
  316. const time_t reject_if_expired_before)
  317. {
  318. rsa_ed_crosscert_t *cc = NULL;
  319. int rv;
  320. #define ERR(code, s) \
  321. do { \
  322. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  323. "Received a bad RSA->Ed25519 crosscert: %s", \
  324. (s)); \
  325. rv = (code); \
  326. goto err; \
  327. } while (0)
  328. if (BUG(crypto_pk_keysize(rsa_id_key) > PK_BYTES))
  329. return -1;
  330. if (BUG(!crosscert))
  331. return -1;
  332. ssize_t parsed_len = rsa_ed_crosscert_parse(&cc, crosscert, crosscert_len);
  333. if (parsed_len < 0 || crosscert_len != (size_t)parsed_len) {
  334. ERR(-2, "Unparseable or overlong crosscert");
  335. }
  336. if (tor_memneq(rsa_ed_crosscert_getarray_ed_key(cc),
  337. master_key->pubkey,
  338. ED25519_PUBKEY_LEN)) {
  339. ERR(-3, "Crosscert did not match Ed25519 key");
  340. }
  341. const uint32_t expiration_date = rsa_ed_crosscert_get_expiration(cc);
  342. const uint64_t expiration_time = expiration_date * 3600;
  343. if (reject_if_expired_before < 0 ||
  344. expiration_time < (uint64_t)reject_if_expired_before) {
  345. ERR(-4, "Crosscert is expired");
  346. }
  347. const uint8_t *eos = rsa_ed_crosscert_get_end_of_signed(cc);
  348. const uint8_t *sig = rsa_ed_crosscert_getarray_sig(cc);
  349. const uint8_t siglen = rsa_ed_crosscert_get_sig_len(cc);
  350. tor_assert(eos >= crosscert);
  351. tor_assert((size_t)(eos - crosscert) <= crosscert_len);
  352. tor_assert(siglen == rsa_ed_crosscert_getlen_sig(cc));
  353. /* Compute the digest */
  354. uint8_t digest[DIGEST256_LEN];
  355. crypto_digest_t *d = crypto_digest256_new(DIGEST_SHA256);
  356. crypto_digest_add_bytes(d, RSA_ED_CROSSCERT_PREFIX,
  357. strlen(RSA_ED_CROSSCERT_PREFIX));
  358. crypto_digest_add_bytes(d, (char*)crosscert, eos-crosscert);
  359. crypto_digest_get_digest(d, (char*)digest, sizeof(digest));
  360. crypto_digest_free(d);
  361. /* Now check the signature */
  362. uint8_t signed_[PK_BYTES];
  363. int signed_len = crypto_pk_public_checksig(rsa_id_key,
  364. (char*)signed_, sizeof(signed_),
  365. (char*)sig, siglen);
  366. if (signed_len < DIGEST256_LEN) {
  367. ERR(-5, "Bad signature, or length of signed data not as expected");
  368. }
  369. if (tor_memneq(digest, signed_, DIGEST256_LEN)) {
  370. ERR(-6, "The signature was good, but it didn't match the data");
  371. }
  372. rv = 0;
  373. err:
  374. rsa_ed_crosscert_free(cc);
  375. return rv;
  376. }
  377. /** Construct and return a new empty or_handshake_certs object */
  378. or_handshake_certs_t *
  379. or_handshake_certs_new(void)
  380. {
  381. return tor_malloc_zero(sizeof(or_handshake_certs_t));
  382. }
  383. /** Release all storage held in <b>certs</b> */
  384. void
  385. or_handshake_certs_free(or_handshake_certs_t *certs)
  386. {
  387. if (!certs)
  388. return;
  389. tor_x509_cert_free(certs->auth_cert);
  390. tor_x509_cert_free(certs->link_cert);
  391. tor_x509_cert_free(certs->id_cert);
  392. tor_cert_free(certs->ed_id_sign);
  393. tor_cert_free(certs->ed_sign_link);
  394. tor_cert_free(certs->ed_sign_auth);
  395. tor_free(certs->ed_rsa_crosscert);
  396. memwipe(certs, 0xBD, sizeof(*certs));
  397. tor_free(certs);
  398. }
  399. #undef ERR
  400. #define ERR(s) \
  401. do { \
  402. log_fn(severity, LD_PROTOCOL, \
  403. "Received a bad CERTS cell: %s", \
  404. (s)); \
  405. return 0; \
  406. } while (0)
  407. int
  408. or_handshake_certs_rsa_ok(int severity,
  409. or_handshake_certs_t *certs,
  410. tor_tls_t *tls,
  411. time_t now)
  412. {
  413. tor_x509_cert_t *link_cert = certs->link_cert;
  414. tor_x509_cert_t *auth_cert = certs->auth_cert;
  415. tor_x509_cert_t *id_cert = certs->id_cert;
  416. if (certs->started_here) {
  417. if (! (id_cert && link_cert))
  418. ERR("The certs we wanted (ID, Link) were missing");
  419. if (! tor_tls_cert_matches_key(tls, link_cert))
  420. ERR("The link certificate didn't match the TLS public key");
  421. if (! tor_tls_cert_is_valid(severity, link_cert, id_cert, now, 0))
  422. ERR("The link certificate was not valid");
  423. if (! tor_tls_cert_is_valid(severity, id_cert, id_cert, now, 1))
  424. ERR("The ID certificate was not valid");
  425. } else {
  426. if (! (id_cert && auth_cert))
  427. ERR("The certs we wanted (ID, Auth) were missing");
  428. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, auth_cert, id_cert, now, 1))
  429. ERR("The authentication certificate was not valid");
  430. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, id_cert, id_cert, now, 1))
  431. ERR("The ID certificate was not valid");
  432. }
  433. return 1;
  434. }
  435. /** Check all the ed25519 certificates in <b>certs</b> against each other, and
  436. * against the peer certificate in <b>tls</b> if appropriate. On success,
  437. * return 0; on failure, return a negative value and warn at level
  438. * <b>severity</b> */
  439. int
  440. or_handshake_certs_ed25519_ok(int severity,
  441. or_handshake_certs_t *certs,
  442. tor_tls_t *tls,
  443. time_t now)
  444. {
  445. ed25519_checkable_t check[10];
  446. unsigned n_checkable = 0;
  447. time_t expiration = TIME_MAX;
  448. #define ADDCERT(cert, pk) \
  449. do { \
  450. tor_assert(n_checkable < ARRAY_LENGTH(check)); \
  451. if (tor_cert_get_checkable_sig(&check[n_checkable++], cert, pk, \
  452. &expiration) < 0) \
  453. ERR("Could not get checkable cert."); \
  454. } while (0)
  455. if (! certs->ed_id_sign || !certs->ed_id_sign->signing_key_included) {
  456. ERR("No Ed25519 signing key");
  457. }
  458. ADDCERT(certs->ed_id_sign, NULL);
  459. if (certs->started_here) {
  460. if (! certs->ed_sign_link)
  461. ERR("No Ed25519 link key");
  462. {
  463. /* check for a match with the TLS cert. */
  464. tor_x509_cert_t *peer_cert = tor_tls_get_peer_cert(tls);
  465. if (BUG(!peer_cert)) {
  466. /* This is a bug, because if we got to this point, we are a connection
  467. * that was initiated here, and we completed a TLS handshake. The
  468. * other side *must* have given us a certificate! */
  469. ERR("No x509 peer cert"); // LCOV_EXCL_LINE
  470. }
  471. const common_digests_t *peer_cert_digests =
  472. tor_x509_cert_get_cert_digests(peer_cert);
  473. int okay = tor_memeq(peer_cert_digests->d[DIGEST_SHA256],
  474. certs->ed_sign_link->signed_key.pubkey,
  475. DIGEST256_LEN);
  476. tor_x509_cert_free(peer_cert);
  477. if (!okay)
  478. ERR("Link certificate does not match TLS certificate");
  479. }
  480. ADDCERT(certs->ed_sign_link, &certs->ed_id_sign->signed_key);
  481. } else {
  482. if (! certs->ed_sign_auth)
  483. ERR("No Ed25519 link authentication key");
  484. ADDCERT(certs->ed_sign_auth, &certs->ed_id_sign->signed_key);
  485. }
  486. if (expiration < now) {
  487. ERR("At least one certificate expired.");
  488. }
  489. /* Okay, we've gotten ready to check all the Ed25519 certificates.
  490. * Now, we are going to check the RSA certificate's cross-certification
  491. * with the ED certificates.
  492. *
  493. * FFFF In the future, we might want to make this optional.
  494. */
  495. tor_x509_cert_t *rsa_id_cert = certs->id_cert;
  496. if (!rsa_id_cert) {
  497. ERR("Missing legacy RSA ID certificate");
  498. }
  499. if (! tor_tls_cert_is_valid(severity, rsa_id_cert, rsa_id_cert, now, 1)) {
  500. ERR("The legacy RSA ID certificate was not valid");
  501. }
  502. if (! certs->ed_rsa_crosscert) {
  503. ERR("Missing RSA->Ed25519 crosscert");
  504. }
  505. crypto_pk_t *rsa_id_key = tor_tls_cert_get_key(rsa_id_cert);
  506. if (!rsa_id_key) {
  507. ERR("RSA ID cert had no RSA key");
  508. }
  509. if (rsa_ed25519_crosscert_check(certs->ed_rsa_crosscert,
  510. certs->ed_rsa_crosscert_len,
  511. rsa_id_key,
  512. &certs->ed_id_sign->signing_key,
  513. now) < 0) {
  514. crypto_pk_free(rsa_id_key);
  515. ERR("Invalid RSA->Ed25519 crosscert");
  516. }
  517. crypto_pk_free(rsa_id_key);
  518. rsa_id_key = NULL;
  519. /* FFFF We could save a little time in the client case by queueing
  520. * this batch to check it later, along with the signature from the
  521. * AUTHENTICATE cell. That will change our data flow a bit, though,
  522. * so I say "postpone". */
  523. if (ed25519_checksig_batch(NULL, check, n_checkable) < 0) {
  524. ERR("At least one Ed25519 certificate was badly signed");
  525. }
  526. return 1;
  527. }
  528. /**
  529. * Check the Ed certificates and/or the RSA certificates, as appropriate. If
  530. * we obtained an Ed25519 identity, set *ed_id_out. If we obtained an RSA
  531. * identity, set *rs_id_out. Otherwise, set them both to NULL.
  532. */
  533. void
  534. or_handshake_certs_check_both(int severity,
  535. or_handshake_certs_t *certs,
  536. tor_tls_t *tls,
  537. time_t now,
  538. const ed25519_public_key_t **ed_id_out,
  539. const common_digests_t **rsa_id_out)
  540. {
  541. tor_assert(ed_id_out);
  542. tor_assert(rsa_id_out);
  543. *ed_id_out = NULL;
  544. *rsa_id_out = NULL;
  545. if (certs->ed_id_sign) {
  546. if (or_handshake_certs_ed25519_ok(severity, certs, tls, now)) {
  547. tor_assert(certs->ed_id_sign);
  548. tor_assert(certs->id_cert);
  549. *ed_id_out = &certs->ed_id_sign->signing_key;
  550. *rsa_id_out = tor_x509_cert_get_id_digests(certs->id_cert);
  551. /* If we reached this point, we did not look at any of the
  552. * subsidiary RSA certificates, so we'd better just remove them.
  553. */
  554. tor_x509_cert_free(certs->link_cert);
  555. tor_x509_cert_free(certs->auth_cert);
  556. certs->link_cert = certs->auth_cert = NULL;
  557. }
  558. /* We do _not_ fall through here. If you provided us Ed25519
  559. * certificates, we expect to verify them! */
  560. } else {
  561. /* No ed25519 keys given in the CERTS cell */
  562. if (or_handshake_certs_rsa_ok(severity, certs, tls, now)) {
  563. *rsa_id_out = tor_x509_cert_get_id_digests(certs->id_cert);
  564. }
  565. }
  566. }