torcert.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. int
  148. tor_cert_get_checkable_sig(ed25519_checkable_t *checkable_out,
  149. const tor_cert_t *cert,
  150. const ed25519_public_key_t *pubkey)
  151. {
  152. if (! pubkey) {
  153. if (cert->signing_key_included)
  154. pubkey = &cert->signing_key;
  155. else
  156. return -1;
  157. }
  158. checkable_out->msg = cert->encoded;
  159. checkable_out->pubkey = pubkey;
  160. tor_assert(cert->encoded_len > ED25519_SIG_LEN);
  161. const size_t signed_len = cert->encoded_len - ED25519_SIG_LEN;
  162. checkable_out->len = signed_len;
  163. memcpy(checkable_out->signature.sig,
  164. cert->encoded + signed_len, ED25519_SIG_LEN);
  165. return 0;
  166. }
  167. /** Validates the signature on <b>cert</b> with <b>pubkey</b> relative to the
  168. * current time <b>now</b>. (If <b>now</b> is 0, do not check the expiration
  169. * time.) Return 0 on success, -1 on failure. Sets flags in <b>cert</b> as
  170. * appropriate.
  171. */
  172. int
  173. tor_cert_checksig(tor_cert_t *cert,
  174. const ed25519_public_key_t *pubkey, time_t now)
  175. {
  176. ed25519_checkable_t checkable;
  177. int okay;
  178. if (now && now > cert->valid_until) {
  179. cert->cert_expired = 1;
  180. return -1;
  181. }
  182. if (tor_cert_get_checkable_sig(&checkable, cert, pubkey) < 0)
  183. return -1;
  184. if (ed25519_checksig_batch(&okay, &checkable, 1) < 0) {
  185. cert->sig_bad = 1;
  186. return -1;
  187. } else {
  188. cert->sig_ok = 1;
  189. /* Only copy the checkable public key when it is different from the signing
  190. * key of the certificate to avoid undefined behavior. */
  191. if (cert->signing_key.pubkey != checkable.pubkey->pubkey) {
  192. memcpy(cert->signing_key.pubkey, checkable.pubkey->pubkey, 32);
  193. }
  194. cert->cert_valid = 1;
  195. return 0;
  196. }
  197. }
  198. /** Return a new copy of <b>cert</b> */
  199. tor_cert_t *
  200. tor_cert_dup(const tor_cert_t *cert)
  201. {
  202. tor_cert_t *newcert = tor_memdup(cert, sizeof(tor_cert_t));
  203. if (cert->encoded)
  204. newcert->encoded = tor_memdup(cert->encoded, cert->encoded_len);
  205. return newcert;
  206. }
  207. /** Return true iff cert1 and cert2 are the same cert. */
  208. int
  209. tor_cert_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  210. {
  211. tor_assert(cert1);
  212. tor_assert(cert2);
  213. return cert1->encoded_len == cert2->encoded_len &&
  214. tor_memeq(cert1->encoded, cert2->encoded, cert1->encoded_len);
  215. }
  216. /** Return true iff cert1 and cert2 are the same cert, or if they are both
  217. * NULL. */
  218. int
  219. tor_cert_opt_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  220. {
  221. if (cert1 == NULL && cert2 == NULL)
  222. return 1;
  223. if (!cert1 || !cert2)
  224. return 0;
  225. return tor_cert_eq(cert1, cert2);
  226. }
  227. /** Create new cross-certification object to certify <b>ed_key</b> as the
  228. * master ed25519 identity key for the RSA identity key <b>rsa_key</b>.
  229. * Allocates and stores the encoded certificate in *<b>cert</b>, and returns
  230. * the number of bytes stored. Returns negative on error.*/
  231. ssize_t
  232. tor_make_rsa_ed25519_crosscert(const ed25519_public_key_t *ed_key,
  233. const crypto_pk_t *rsa_key,
  234. time_t expires,
  235. uint8_t **cert)
  236. {
  237. uint8_t *res;
  238. rsa_ed_crosscert_t *cc = rsa_ed_crosscert_new();
  239. memcpy(cc->ed_key, ed_key->pubkey, ED25519_PUBKEY_LEN);
  240. cc->expiration = (uint32_t) CEIL_DIV(expires, 3600);
  241. cc->sig_len = crypto_pk_keysize(rsa_key);
  242. rsa_ed_crosscert_setlen_sig(cc, crypto_pk_keysize(rsa_key));
  243. ssize_t alloc_sz = rsa_ed_crosscert_encoded_len(cc);
  244. tor_assert(alloc_sz > 0);
  245. res = tor_malloc_zero(alloc_sz);
  246. ssize_t sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  247. tor_assert(sz > 0 && sz <= alloc_sz);
  248. const int signed_part_len = 32 + 4;
  249. int siglen = crypto_pk_private_sign(rsa_key,
  250. (char*)rsa_ed_crosscert_getarray_sig(cc),
  251. rsa_ed_crosscert_getlen_sig(cc),
  252. (char*)res, signed_part_len);
  253. tor_assert(siglen > 0 && siglen <= (int)crypto_pk_keysize(rsa_key));
  254. tor_assert(siglen <= UINT8_MAX);
  255. cc->sig_len = siglen;
  256. rsa_ed_crosscert_setlen_sig(cc, siglen);
  257. sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  258. rsa_ed_crosscert_free(cc);
  259. *cert = res;
  260. return sz;
  261. }
  262. or_handshake_certs_t *
  263. or_handshake_certs_new(void)
  264. {
  265. return tor_malloc_zero(sizeof(or_handshake_certs_t));
  266. }
  267. /** DODCDOC */
  268. void
  269. or_handshake_certs_free(or_handshake_certs_t *certs)
  270. {
  271. if (!certs)
  272. return;
  273. tor_x509_cert_free(certs->auth_cert);
  274. tor_x509_cert_free(certs->id_cert);
  275. memwipe(certs, 0xBD, sizeof(*certs));
  276. tor_free(certs);
  277. }
  278. #define ERR(s) \
  279. do { \
  280. log_fn(severity, LD_PROTOCOL, \
  281. "Received a bad CERTS cell: %s", \
  282. (s)); \
  283. return 0; \
  284. } while (0)
  285. int
  286. or_handshake_certs_rsa_ok(int severity,
  287. or_handshake_certs_t *certs,
  288. tor_tls_t *tls)
  289. {
  290. tor_x509_cert_t *link_cert = certs->link_cert;
  291. tor_x509_cert_t *auth_cert = certs->auth_cert;
  292. tor_x509_cert_t *id_cert = certs->id_cert;
  293. if (certs->started_here) {
  294. if (! (id_cert && link_cert))
  295. ERR("The certs we wanted were missing");
  296. if (! tor_tls_cert_matches_key(tls, link_cert))
  297. ERR("The link certificate didn't match the TLS public key");
  298. if (! tor_tls_cert_is_valid(severity, link_cert, id_cert, 0))
  299. ERR("The link certificate was not valid");
  300. if (! tor_tls_cert_is_valid(severity, id_cert, id_cert, 1))
  301. ERR("The ID certificate was not valid");
  302. } else {
  303. if (! (id_cert && auth_cert))
  304. ERR("The certs we wanted were missing");
  305. /* Remember these certificates so we can check an AUTHENTICATE cell */
  306. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, auth_cert, id_cert, 1))
  307. ERR("The authentication certificate was not valid");
  308. if (! tor_tls_cert_is_valid(LOG_PROTOCOL_WARN, id_cert, id_cert, 1))
  309. ERR("The ID certificate was not valid");
  310. }
  311. return 1;
  312. }
  313. int
  314. or_handshake_certs_ed25519_ok(or_handshake_certs_t *certs)
  315. {
  316. (void) certs;
  317. return 0;
  318. }