torcert.c 20 KB

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