torcert.c 24 KB

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