torcert.c 25 KB

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