torcert.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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. cert->valid_until = parsed->exp_field * 3600;
  124. cert->cert_type = parsed->cert_type;
  125. for (unsigned i = 0; i < ed25519_cert_getlen_ext(parsed); ++i) {
  126. ed25519_cert_extension_t *ext = ed25519_cert_get_ext(parsed, i);
  127. if (ext->ext_type == CERTEXT_SIGNED_WITH_KEY) {
  128. if (cert->signing_key_included)
  129. goto err;
  130. cert->signing_key_included = 1;
  131. memcpy(cert->signing_key.pubkey, ext->un_signing_key, 32);
  132. } else if (ext->ext_flags & CERTEXT_FLAG_AFFECTS_VALIDATION) {
  133. /* Unrecognized extension with affects_validation set */
  134. goto err;
  135. }
  136. }
  137. goto done;
  138. err:
  139. tor_cert_free(cert);
  140. cert = NULL;
  141. done:
  142. ed25519_cert_free(parsed);
  143. return cert;
  144. }
  145. /** Fill in <b>checkable_out</b> with the information needed to check
  146. * the signature on <b>cert</b> with <b>pubkey</b>.
  147. *
  148. * On success, if <b>expiration_out</b> is provided, and it is some time
  149. * _after_ the expiration time of this certificate, set it to the
  150. * expiration time of this certificate.
  151. */
  152. int
  153. tor_cert_get_checkable_sig(ed25519_checkable_t *checkable_out,
  154. const tor_cert_t *cert,
  155. const ed25519_public_key_t *pubkey,
  156. time_t *expiration_out)
  157. {
  158. if (! pubkey) {
  159. if (cert->signing_key_included)
  160. pubkey = &cert->signing_key;
  161. else
  162. return -1;
  163. }
  164. checkable_out->msg = cert->encoded;
  165. checkable_out->pubkey = pubkey;
  166. tor_assert(cert->encoded_len > ED25519_SIG_LEN);
  167. const size_t signed_len = cert->encoded_len - ED25519_SIG_LEN;
  168. checkable_out->len = signed_len;
  169. memcpy(checkable_out->signature.sig,
  170. cert->encoded + signed_len, ED25519_SIG_LEN);
  171. if (expiration_out) {
  172. *expiration_out = MIN(*expiration_out, cert->valid_until);
  173. }
  174. return 0;
  175. }
  176. /** Validates the signature on <b>cert</b> with <b>pubkey</b> relative to the
  177. * current time <b>now</b>. (If <b>now</b> is 0, do not check the expiration
  178. * time.) Return 0 on success, -1 on failure. Sets flags in <b>cert</b> as
  179. * appropriate.
  180. */
  181. int
  182. tor_cert_checksig(tor_cert_t *cert,
  183. const ed25519_public_key_t *pubkey, time_t now)
  184. {
  185. ed25519_checkable_t checkable;
  186. int okay;
  187. time_t expires = TIME_MAX;
  188. if (tor_cert_get_checkable_sig(&checkable, cert, pubkey, &expires) < 0)
  189. return -1;
  190. if (now && now > expires) {
  191. cert->cert_expired = 1;
  192. return -1;
  193. }
  194. if (ed25519_checksig_batch(&okay, &checkable, 1) < 0) {
  195. cert->sig_bad = 1;
  196. return -1;
  197. } else {
  198. cert->sig_ok = 1;
  199. /* Only copy the checkable public key when it is different from the signing
  200. * key of the certificate to avoid undefined behavior. */
  201. if (cert->signing_key.pubkey != checkable.pubkey->pubkey) {
  202. memcpy(cert->signing_key.pubkey, checkable.pubkey->pubkey, 32);
  203. }
  204. cert->cert_valid = 1;
  205. return 0;
  206. }
  207. }
  208. /** Return a new copy of <b>cert</b> */
  209. tor_cert_t *
  210. tor_cert_dup(const tor_cert_t *cert)
  211. {
  212. tor_cert_t *newcert = tor_memdup(cert, sizeof(tor_cert_t));
  213. if (cert->encoded)
  214. newcert->encoded = tor_memdup(cert->encoded, cert->encoded_len);
  215. return newcert;
  216. }
  217. /** Return true iff cert1 and cert2 are the same cert. */
  218. int
  219. tor_cert_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  220. {
  221. tor_assert(cert1);
  222. tor_assert(cert2);
  223. return cert1->encoded_len == cert2->encoded_len &&
  224. tor_memeq(cert1->encoded, cert2->encoded, cert1->encoded_len);
  225. }
  226. /** Return true iff cert1 and cert2 are the same cert, or if they are both
  227. * NULL. */
  228. int
  229. tor_cert_opt_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  230. {
  231. if (cert1 == NULL && cert2 == NULL)
  232. return 1;
  233. if (!cert1 || !cert2)
  234. return 0;
  235. return tor_cert_eq(cert1, cert2);
  236. }
  237. #define RSA_ED_CROSSCERT_PREFIX "Tor TLS RSA/Ed25519 cross-certificate"
  238. /** Create new cross-certification object to certify <b>ed_key</b> as the
  239. * master ed25519 identity key for the RSA identity key <b>rsa_key</b>.
  240. * Allocates and stores the encoded certificate in *<b>cert</b>, and returns
  241. * the number of bytes stored. Returns negative on error.*/
  242. ssize_t
  243. tor_make_rsa_ed25519_crosscert(const ed25519_public_key_t *ed_key,
  244. const crypto_pk_t *rsa_key,
  245. time_t expires,
  246. uint8_t **cert)
  247. {
  248. uint8_t *res;
  249. rsa_ed_crosscert_t *cc = rsa_ed_crosscert_new();
  250. memcpy(cc->ed_key, ed_key->pubkey, ED25519_PUBKEY_LEN);
  251. cc->expiration = (uint32_t) CEIL_DIV(expires, 3600);
  252. cc->sig_len = crypto_pk_keysize(rsa_key);
  253. rsa_ed_crosscert_setlen_sig(cc, crypto_pk_keysize(rsa_key));
  254. ssize_t alloc_sz = rsa_ed_crosscert_encoded_len(cc);
  255. tor_assert(alloc_sz > 0);
  256. res = tor_malloc_zero(alloc_sz);
  257. ssize_t sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  258. tor_assert(sz > 0 && sz <= alloc_sz);
  259. crypto_digest_t *d = crypto_digest256_new(DIGEST_SHA256);
  260. crypto_digest_add_bytes(d, RSA_ED_CROSSCERT_PREFIX,
  261. strlen(RSA_ED_CROSSCERT_PREFIX));
  262. const int signed_part_len = 32 + 4;
  263. crypto_digest_add_bytes(d, (char*)res, signed_part_len);
  264. uint8_t digest[DIGEST256_LEN];
  265. crypto_digest_get_digest(d, (char*)digest, sizeof(digest));
  266. crypto_digest_free(d);
  267. int siglen = crypto_pk_private_sign(rsa_key,
  268. (char*)rsa_ed_crosscert_getarray_sig(cc),
  269. rsa_ed_crosscert_getlen_sig(cc),
  270. (char*)digest, sizeof(digest));
  271. tor_assert(siglen > 0 && siglen <= (int)crypto_pk_keysize(rsa_key));
  272. tor_assert(siglen <= UINT8_MAX);
  273. cc->sig_len = siglen;
  274. rsa_ed_crosscert_setlen_sig(cc, siglen);
  275. sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  276. rsa_ed_crosscert_free(cc);
  277. *cert = res;
  278. return sz;
  279. }
  280. /**
  281. * Check whether the <b>crosscert_len</b> byte certificate in <b>crosscert</b>
  282. * is in fact a correct cross-certification of <b>master_key</b> using
  283. * the RSA key <b>rsa_id_key</b>.
  284. *
  285. * Also reject the certificate if it expired before
  286. * <b>reject_if_expired_before</b>.
  287. *
  288. * Return 0 on success, negative on failure.
  289. */
  290. int
  291. rsa_ed25519_crosscert_check(const uint8_t *crosscert,
  292. const size_t crosscert_len,
  293. const crypto_pk_t *rsa_id_key,
  294. const ed25519_public_key_t *master_key,
  295. const time_t reject_if_expired_before)
  296. {
  297. rsa_ed_crosscert_t *cc = NULL;
  298. int rv;
  299. #define ERR(code, s) \
  300. do { \
  301. log_fn(LOG_PROTOCOL_WARN, LD_PROTOCOL, \
  302. "Received a bad RSA->Ed25519 crosscert: %s", \
  303. (s)); \
  304. rv = (code); \
  305. goto err; \
  306. } while (0)
  307. if (BUG(crypto_pk_keysize(rsa_id_key) > PK_BYTES))
  308. return -1;
  309. if (BUG(!crosscert))
  310. return -1;
  311. ssize_t parsed_len = rsa_ed_crosscert_parse(&cc, crosscert, crosscert_len);
  312. if (parsed_len < 0 || crosscert_len != (size_t)parsed_len) {
  313. ERR(-2, "Unparseable or overlong crosscert");
  314. }
  315. if (tor_memneq(rsa_ed_crosscert_getarray_ed_key(cc),
  316. master_key->pubkey,
  317. ED25519_PUBKEY_LEN)) {
  318. ERR(-3, "Crosscert did not match Ed25519 key");
  319. }
  320. const uint32_t expiration_date = rsa_ed_crosscert_get_expiration(cc);
  321. const uint64_t expiration_time = expiration_date * 3600;
  322. if (reject_if_expired_before < 0 ||
  323. expiration_time < (uint64_t)reject_if_expired_before) {
  324. ERR(-4, "Crosscert is expired");
  325. }
  326. const uint8_t *eos = rsa_ed_crosscert_get_end_of_signed(cc);
  327. const uint8_t *sig = rsa_ed_crosscert_getarray_sig(cc);
  328. const uint8_t siglen = rsa_ed_crosscert_get_sig_len(cc);
  329. tor_assert(eos >= crosscert);
  330. tor_assert((size_t)(eos - crosscert) <= crosscert_len);
  331. tor_assert(siglen == rsa_ed_crosscert_getlen_sig(cc));
  332. /* Compute the digest */
  333. uint8_t digest[DIGEST256_LEN];
  334. crypto_digest_t *d = crypto_digest256_new(DIGEST_SHA256);
  335. crypto_digest_add_bytes(d, RSA_ED_CROSSCERT_PREFIX,
  336. strlen(RSA_ED_CROSSCERT_PREFIX));
  337. crypto_digest_add_bytes(d, (char*)crosscert, eos-crosscert);
  338. crypto_digest_get_digest(d, (char*)digest, sizeof(digest));
  339. crypto_digest_free(d);
  340. /* Now check the signature */
  341. uint8_t signed_[PK_BYTES];
  342. int signed_len = crypto_pk_public_checksig(rsa_id_key,
  343. (char*)signed_, sizeof(signed_),
  344. (char*)sig, siglen);
  345. if (signed_len < DIGEST256_LEN) {
  346. ERR(-5, "Bad signature, or length of signed data not as expected");
  347. }
  348. if (tor_memneq(digest, signed_, DIGEST256_LEN)) {
  349. ERR(-6, "The signature was good, but it didn't match the data");
  350. }
  351. rv = 0;
  352. err:
  353. rsa_ed_crosscert_free(cc);
  354. return rv;
  355. }
  356. /** Construct and return a new empty or_handshake_certs object */
  357. or_handshake_certs_t *
  358. or_handshake_certs_new(void)
  359. {
  360. return tor_malloc_zero(sizeof(or_handshake_certs_t));
  361. }
  362. /** DODCDOC */
  363. void
  364. or_handshake_certs_free(or_handshake_certs_t *certs)
  365. {
  366. if (!certs)
  367. return;
  368. tor_x509_cert_free(certs->auth_cert);
  369. tor_x509_cert_free(certs->id_cert);
  370. memwipe(certs, 0xBD, sizeof(*certs));
  371. tor_free(certs);
  372. }
  373. #undef ERR
  374. #define ERR(s) \
  375. do { \
  376. log_fn(severity, LD_PROTOCOL, \
  377. "Received a bad CERTS cell: %s", \
  378. (s)); \
  379. return 0; \
  380. } while (0)
  381. int
  382. or_handshake_certs_rsa_ok(int severity,
  383. or_handshake_certs_t *certs,
  384. tor_tls_t *tls,
  385. time_t now)
  386. {
  387. tor_x509_cert_t *link_cert = certs->link_cert;
  388. tor_x509_cert_t *auth_cert = certs->auth_cert;
  389. tor_x509_cert_t *id_cert = certs->id_cert;
  390. if (certs->started_here) {
  391. if (! (id_cert && link_cert))
  392. ERR("The certs we wanted were missing");
  393. if (! tor_tls_cert_matches_key(tls, link_cert))
  394. ERR("The link certificate didn't match the TLS public key");
  395. if (! tor_tls_cert_is_valid(severity, link_cert, id_cert, now, 0))
  396. ERR("The link certificate was not valid");
  397. if (! tor_tls_cert_is_valid(severity, id_cert, id_cert, now, 1))
  398. ERR("The ID certificate was not valid");
  399. } else {
  400. if (! (id_cert && auth_cert))
  401. ERR("The certs we wanted were missing");
  402. /* Remember these certificates so we can check an AUTHENTICATE cell
  403. * XXXX make sure we do that
  404. */
  405. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, auth_cert, id_cert, now, 1))
  406. ERR("The authentication certificate was not valid");
  407. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, id_cert, id_cert, now, 1))
  408. ERR("The ID certificate was not valid");
  409. }
  410. return 1;
  411. }
  412. int
  413. or_handshake_certs_ed25519_ok(or_handshake_certs_t *certs)
  414. {
  415. (void) certs;
  416. return 0;
  417. }