torcert.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "crypto.h"
  11. #include "torcert.h"
  12. #include "ed25519_cert.h"
  13. #include "torlog.h"
  14. #include "util.h"
  15. #include "compat.h"
  16. #include "link_handshake.h"
  17. /** Helper for tor_cert_create(): signs any 32 bytes, not just an ed25519
  18. * key.
  19. */
  20. static tor_cert_t *
  21. tor_cert_sign_impl(const ed25519_keypair_t *signing_key,
  22. uint8_t cert_type,
  23. uint8_t signed_key_type,
  24. const uint8_t signed_key_info[32],
  25. time_t now, time_t lifetime,
  26. uint32_t flags)
  27. {
  28. tor_cert_t *torcert = NULL;
  29. ed25519_cert_t *cert = ed25519_cert_new();
  30. cert->cert_type = cert_type;
  31. cert->exp_field = (uint32_t) CEIL_DIV(now + lifetime, 3600);
  32. cert->cert_key_type = signed_key_type;
  33. memcpy(cert->certified_key, signed_key_info, 32);
  34. if (flags & CERT_FLAG_INCLUDE_SIGNING_KEY) {
  35. ed25519_cert_extension_t *ext = ed25519_cert_extension_new();
  36. ext->ext_type = CERTEXT_SIGNED_WITH_KEY;
  37. memcpy(ext->un_signing_key, signing_key->pubkey.pubkey, 32);
  38. ed25519_cert_add_ext(cert, ext);
  39. ++cert->n_extensions;
  40. }
  41. const ssize_t alloc_len = ed25519_cert_encoded_len(cert);
  42. tor_assert(alloc_len > 0);
  43. uint8_t *encoded = tor_malloc(alloc_len);
  44. const ssize_t real_len = ed25519_cert_encode(encoded, alloc_len, cert);
  45. if (real_len < 0)
  46. goto err;
  47. tor_assert(real_len == alloc_len);
  48. tor_assert(real_len > ED25519_SIG_LEN);
  49. uint8_t *sig = encoded + (real_len - ED25519_SIG_LEN);
  50. tor_assert(tor_mem_is_zero((char*)sig, ED25519_SIG_LEN));
  51. ed25519_signature_t signature;
  52. if (ed25519_sign(&signature, encoded,
  53. real_len-ED25519_SIG_LEN, signing_key)<0) {
  54. log_warn(LD_BUG, "Can't sign certificate");
  55. goto err;
  56. }
  57. memcpy(sig, signature.sig, ED25519_SIG_LEN);
  58. torcert = tor_cert_parse(encoded, real_len);
  59. if (! torcert) {
  60. log_warn(LD_BUG, "Generated a certificate we cannot parse");
  61. goto err;
  62. }
  63. if (tor_cert_checksig(torcert, &signing_key->pubkey, now) < 0) {
  64. log_warn(LD_BUG, "Generated a certificate whose signature we can't check");
  65. goto err;
  66. }
  67. tor_free(encoded);
  68. goto done;
  69. err:
  70. tor_cert_free(torcert);
  71. torcert = NULL;
  72. done:
  73. ed25519_cert_free(cert);
  74. tor_free(encoded);
  75. return torcert;
  76. }
  77. /**
  78. * Create and return a new new certificate of type <b>cert_type</b> to
  79. * authenticate <b>signed_key</b> using the key <b>signing_key</b>. The
  80. * certificate should remain valid for at least <b>lifetime</b> seconds after
  81. * <b>now</b>.
  82. *
  83. * If CERT_FLAG_INCLUDE_SIGNING_KEY is set in <b>flags</b>, embed
  84. * the public part of <b>signing_key</b> in the certificate.
  85. */
  86. tor_cert_t *
  87. tor_cert_create(const ed25519_keypair_t *signing_key,
  88. uint8_t cert_type,
  89. const ed25519_public_key_t *signed_key,
  90. time_t now, time_t lifetime,
  91. uint32_t flags)
  92. {
  93. return tor_cert_sign_impl(signing_key, cert_type,
  94. SIGNED_KEY_TYPE_ED25519, signed_key->pubkey,
  95. now, lifetime, flags);
  96. }
  97. /** Release all storage held for <b>cert</b>. */
  98. void
  99. tor_cert_free(tor_cert_t *cert)
  100. {
  101. if (! cert)
  102. return;
  103. if (cert->encoded)
  104. memwipe(cert->encoded, 0, cert->encoded_len);
  105. tor_free(cert->encoded);
  106. memwipe(cert, 0, sizeof(tor_cert_t));
  107. tor_free(cert);
  108. }
  109. /** Parse a certificate encoded with <b>len</b> bytes in <b>encoded</b>. */
  110. tor_cert_t *
  111. tor_cert_parse(const uint8_t *encoded, const size_t len)
  112. {
  113. tor_cert_t *cert = NULL;
  114. ed25519_cert_t *parsed = NULL;
  115. ssize_t got_len = ed25519_cert_parse(&parsed, encoded, len);
  116. if (got_len < 0 || (size_t) got_len != len)
  117. goto err;
  118. cert = tor_malloc_zero(sizeof(tor_cert_t));
  119. cert->encoded = tor_memdup(encoded, len);
  120. cert->encoded_len = len;
  121. memcpy(cert->signed_key.pubkey, parsed->certified_key, 32);
  122. cert->valid_until = parsed->exp_field * 3600;
  123. cert->cert_type = parsed->cert_type;
  124. for (unsigned i = 0; i < ed25519_cert_getlen_ext(parsed); ++i) {
  125. ed25519_cert_extension_t *ext = ed25519_cert_get_ext(parsed, i);
  126. if (ext->ext_type == CERTEXT_SIGNED_WITH_KEY) {
  127. if (cert->signing_key_included)
  128. goto err;
  129. cert->signing_key_included = 1;
  130. memcpy(cert->signing_key.pubkey, ext->un_signing_key, 32);
  131. } else if (ext->ext_flags & CERTEXT_FLAG_AFFECTS_VALIDATION) {
  132. /* Unrecognized extension with affects_validation set */
  133. goto err;
  134. }
  135. }
  136. goto done;
  137. err:
  138. tor_cert_free(cert);
  139. cert = NULL;
  140. done:
  141. ed25519_cert_free(parsed);
  142. return cert;
  143. }
  144. /** Fill in <b>checkable_out</b> with the information needed to check
  145. * the signature on <b>cert</b> with <b>pubkey</b>. */
  146. int
  147. tor_cert_get_checkable_sig(ed25519_checkable_t *checkable_out,
  148. const tor_cert_t *cert,
  149. const ed25519_public_key_t *pubkey)
  150. {
  151. if (! pubkey) {
  152. if (cert->signing_key_included)
  153. pubkey = &cert->signing_key;
  154. else
  155. return -1;
  156. }
  157. checkable_out->msg = cert->encoded;
  158. checkable_out->pubkey = pubkey;
  159. tor_assert(cert->encoded_len > ED25519_SIG_LEN);
  160. const size_t signed_len = cert->encoded_len - ED25519_SIG_LEN;
  161. checkable_out->len = signed_len;
  162. memcpy(checkable_out->signature.sig,
  163. cert->encoded + signed_len, ED25519_SIG_LEN);
  164. return 0;
  165. }
  166. /** Validates the signature on <b>cert</b> with <b>pubkey</b> relative to the
  167. * current time <b>now</b>. (If <b>now</b> is 0, do not check the expiration
  168. * time.) Return 0 on success, -1 on failure. Sets flags in <b>cert</b> as
  169. * appropriate.
  170. */
  171. int
  172. tor_cert_checksig(tor_cert_t *cert,
  173. const ed25519_public_key_t *pubkey, time_t now)
  174. {
  175. ed25519_checkable_t checkable;
  176. int okay;
  177. if (now && now > cert->valid_until) {
  178. cert->cert_expired = 1;
  179. return -1;
  180. }
  181. if (tor_cert_get_checkable_sig(&checkable, cert, pubkey) < 0)
  182. return -1;
  183. if (ed25519_checksig_batch(&okay, &checkable, 1) < 0) {
  184. cert->sig_bad = 1;
  185. return -1;
  186. } else {
  187. cert->sig_ok = 1;
  188. /* Only copy the checkable public key when it is different from the signing
  189. * key of the certificate to avoid undefined behavior. */
  190. if (cert->signing_key.pubkey != checkable.pubkey->pubkey) {
  191. memcpy(cert->signing_key.pubkey, checkable.pubkey->pubkey, 32);
  192. }
  193. cert->cert_valid = 1;
  194. return 0;
  195. }
  196. }
  197. /** Return a new copy of <b>cert</b> */
  198. tor_cert_t *
  199. tor_cert_dup(const tor_cert_t *cert)
  200. {
  201. tor_cert_t *newcert = tor_memdup(cert, sizeof(tor_cert_t));
  202. if (cert->encoded)
  203. newcert->encoded = tor_memdup(cert->encoded, cert->encoded_len);
  204. return newcert;
  205. }
  206. /** Return true iff cert1 and cert2 are the same cert. */
  207. int
  208. tor_cert_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  209. {
  210. tor_assert(cert1);
  211. tor_assert(cert2);
  212. return cert1->encoded_len == cert2->encoded_len &&
  213. tor_memeq(cert1->encoded, cert2->encoded, cert1->encoded_len);
  214. }
  215. /** Return true iff cert1 and cert2 are the same cert, or if they are both
  216. * NULL. */
  217. int
  218. tor_cert_opt_eq(const tor_cert_t *cert1, const tor_cert_t *cert2)
  219. {
  220. if (cert1 == NULL && cert2 == NULL)
  221. return 1;
  222. if (!cert1 || !cert2)
  223. return 0;
  224. return tor_cert_eq(cert1, cert2);
  225. }
  226. /** Create new cross-certification object to certify <b>ed_key</b> as the
  227. * master ed25519 identity key for the RSA identity key <b>rsa_key</b>.
  228. * Allocates and stores the encoded certificate in *<b>cert</b>, and returns
  229. * the number of bytes stored. Returns negative on error.*/
  230. ssize_t
  231. tor_make_rsa_ed25519_crosscert(const ed25519_public_key_t *ed_key,
  232. const crypto_pk_t *rsa_key,
  233. time_t expires,
  234. uint8_t **cert)
  235. {
  236. uint8_t *res;
  237. rsa_ed_crosscert_t *cc = rsa_ed_crosscert_new();
  238. memcpy(cc->ed_key, ed_key->pubkey, ED25519_PUBKEY_LEN);
  239. cc->expiration = (uint32_t) CEIL_DIV(expires, 3600);
  240. cc->sig_len = crypto_pk_keysize(rsa_key);
  241. rsa_ed_crosscert_setlen_sig(cc, crypto_pk_keysize(rsa_key));
  242. ssize_t alloc_sz = rsa_ed_crosscert_encoded_len(cc);
  243. tor_assert(alloc_sz > 0);
  244. res = tor_malloc_zero(alloc_sz);
  245. ssize_t sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  246. tor_assert(sz > 0 && sz <= alloc_sz);
  247. const int signed_part_len = 32 + 4;
  248. int siglen = crypto_pk_private_sign(rsa_key,
  249. (char*)rsa_ed_crosscert_getarray_sig(cc),
  250. rsa_ed_crosscert_getlen_sig(cc),
  251. (char*)res, signed_part_len);
  252. tor_assert(siglen > 0 && siglen <= (int)crypto_pk_keysize(rsa_key));
  253. tor_assert(siglen <= UINT8_MAX);
  254. cc->sig_len = siglen;
  255. rsa_ed_crosscert_setlen_sig(cc, siglen);
  256. sz = rsa_ed_crosscert_encode(res, alloc_sz, cc);
  257. rsa_ed_crosscert_free(cc);
  258. *cert = res;
  259. return sz;
  260. }
  261. or_handshake_certs_t *
  262. or_handshake_certs_new(void)
  263. {
  264. return tor_malloc_zero(sizeof(or_handshake_certs_t));
  265. }
  266. /** DODCDOC */
  267. void
  268. or_handshake_certs_free(or_handshake_certs_t *certs)
  269. {
  270. if (!certs)
  271. return;
  272. tor_x509_cert_free(certs->auth_cert);
  273. tor_x509_cert_free(certs->id_cert);
  274. memwipe(certs, 0xBD, sizeof(*certs));
  275. tor_free(certs);
  276. }