torcert.c 23 KB

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