tor-gencert.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /* Copyright 2007 Roger Dingledine, Nick Mathewson */
  2. /* See LICENSE for licensing information */
  3. /* $Id: /tor/trunk/src/tools/tor-resolve.c 12481 2007-04-21T17:27:19.371353Z nickm $ */
  4. #include "orconfig.h"
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <openssl/evp.h>
  12. #include <openssl/pem.h>
  13. #include <openssl/objects.h>
  14. #include <openssl/obj_mac.h>
  15. #include <openssl/err.h>
  16. #include <errno.h>
  17. #if 0
  18. #include <stdlib.h>
  19. #include <stdarg.h>
  20. #include <assert.h>
  21. #endif
  22. #define CRYPTO_PRIVATE
  23. #include "compat.h"
  24. #include "util.h"
  25. #include "log.h"
  26. #include "crypto.h"
  27. #define IDENTITY_KEY_BITS 3072
  28. #define SIGNING_KEY_BITS 1024
  29. #define DEFAULT_LIFETIME 12
  30. char *identity_key_file = NULL;
  31. char *signing_key_file = NULL;
  32. char *certificate_file = NULL;
  33. int verbose = 0;
  34. int make_new_id = 0;
  35. int months_lifetime = DEFAULT_LIFETIME;
  36. EVP_PKEY *identity_key = NULL;
  37. EVP_PKEY *signing_key = NULL;
  38. /* DOCDOC */
  39. static void
  40. show_help(void)
  41. {
  42. fprintf(stderr, "Syntax:\n"
  43. "tor-gencert [-h|--help] [-v] [--create-identity-key] "
  44. "[-i identity_key_file]\n"
  45. " [-s signing_key_file] [-c certificate_file]\n");
  46. }
  47. /* XXXX copied from crypto.c */
  48. static void
  49. crypto_log_errors(int severity, const char *doing)
  50. {
  51. unsigned int err;
  52. const char *msg, *lib, *func;
  53. while ((err = ERR_get_error()) != 0) {
  54. msg = (const char*)ERR_reason_error_string(err);
  55. lib = (const char*)ERR_lib_error_string(err);
  56. func = (const char*)ERR_func_error_string(err);
  57. if (!msg) msg = "(null)";
  58. if (!lib) lib = "(null)";
  59. if (!func) func = "(null)";
  60. if (doing) {
  61. log(severity, LD_CRYPTO, "crypto error while %s: %s (in %s:%s)",
  62. doing, msg, lib, func);
  63. } else {
  64. log(severity, LD_CRYPTO, "crypto error: %s (in %s:%s)", msg, lib, func);
  65. }
  66. }
  67. }
  68. /** DOCDOC */
  69. static int
  70. parse_commandline(int argc, char **argv)
  71. {
  72. int i;
  73. for (i = 1; i < argc; ++i) {
  74. if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
  75. show_help();
  76. return 1;
  77. } else if (!strcmp(argv[i], "-i")) {
  78. if (i+1>=argc) {
  79. fprintf(stderr, "No argument to -i\n");
  80. return 1;
  81. }
  82. identity_key_file = tor_strdup(argv[++i]);
  83. } else if (!strcmp(argv[i], "-c")) {
  84. if (i+1>=argc) {
  85. fprintf(stderr, "No argument to -c\n");
  86. return 1;
  87. }
  88. certificate_file = tor_strdup(argv[++i]);
  89. } else if (!strcmp(argv[i], "-m")) {
  90. if (i+1>=argc) {
  91. fprintf(stderr, "No argument to -m\n");
  92. return 1;
  93. }
  94. months_lifetime = atoi(argv[++i]);
  95. if (months_lifetime > 24 || months_lifetime < 0) {
  96. fprintf(stderr, "Lifetime (in months) was out of range.");
  97. return 1;
  98. }
  99. } else if (!strcmp(argv[i], "-v")) {
  100. verbose = 1;
  101. } else if (!strcmp(argv[i], "--create-identity-key")) {
  102. make_new_id = 1;
  103. } else {
  104. fprintf(stderr, "Unrecognized option %s\n", argv[i]);
  105. return 1;
  106. }
  107. }
  108. if (verbose) {
  109. add_stream_log(LOG_INFO, LOG_ERR, "<stderr>", stderr);
  110. } else {
  111. add_stream_log(LOG_NOTICE, LOG_ERR, "<stderr>", stderr);
  112. }
  113. if (!identity_key_file) {
  114. identity_key_file = tor_strdup("./authority_identity_key");
  115. log_info(LD_GENERAL, "No identity key file given; defaulting to %s",
  116. identity_key_file);
  117. }
  118. if (!signing_key_file) {
  119. signing_key_file = tor_strdup("./authority_signing_key");
  120. log_info(LD_GENERAL, "No signing key file given; defaulting to %s",
  121. signing_key_file);
  122. }
  123. if (!certificate_file) {
  124. certificate_file = tor_strdup("./authority_certificate");
  125. log_info(LD_GENERAL, "No signing key file given; defaulting to %s",
  126. signing_key_file);
  127. }
  128. return 0;
  129. }
  130. /** DOCDOC */
  131. static int
  132. load_identity_key(void)
  133. {
  134. file_status_t status = file_status(identity_key_file);
  135. FILE *f;
  136. if (make_new_id) {
  137. int fd;
  138. RSA *key;
  139. if (status != FN_NOENT) {
  140. log_err(LD_GENERAL, "--create-identity-key was specified, but %s "
  141. "already exists.", identity_key_file);
  142. return 1;
  143. }
  144. log_notice(LD_GENERAL, "Generating %d-bit RSA identity key.",
  145. IDENTITY_KEY_BITS);
  146. if (!(key = RSA_generate_key(IDENTITY_KEY_BITS, 65537, NULL, NULL))) {
  147. log_err(LD_GENERAL, "Couldn't generate identity key.");
  148. crypto_log_errors(LOG_ERR, "Generating identity key");
  149. return 1;
  150. }
  151. identity_key = EVP_PKEY_new();
  152. if (!(EVP_PKEY_assign_RSA(identity_key, key))) {
  153. log_err(LD_GENERAL, "Couldn't assign identity key.");
  154. return 1;
  155. }
  156. if ((fd = open(identity_key_file, O_CREAT|O_EXCL|O_WRONLY, 0400))<0) {
  157. log_err(LD_GENERAL, "Couldn't fdopen %s for writing: %s",
  158. identity_key_file, strerror(errno));
  159. return 1;
  160. }
  161. if (!(f = fdopen(fd, "w"))) {
  162. close(fd);
  163. log_err(LD_GENERAL, "Couldn't fdopen %s for writing: %s",
  164. identity_key_file, strerror(errno));
  165. return 1;
  166. }
  167. if (!PEM_write_PKCS8PrivateKey_nid(f, identity_key,
  168. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  169. NULL, 0, /* no password here. */
  170. NULL, NULL)) {
  171. log_err(LD_GENERAL, "Couldn't write identity key to %s",
  172. identity_key_file);
  173. crypto_log_errors(LOG_ERR, "Writing identity key");
  174. return 1;
  175. }
  176. fclose(f);
  177. } else {
  178. if (status != FN_FILE) {
  179. log_err(LD_GENERAL,
  180. "No identity key found in %s. To specify a location "
  181. "for an identity key, use -i. To generate a new identity key, "
  182. "use --create-identity-key.", identity_key_file);
  183. return 1;
  184. }
  185. if (!(f = fopen(identity_key_file, "r"))) {
  186. log_err(LD_GENERAL, "Couldn't open %s for reading: %s",
  187. identity_key_file, strerror(errno));
  188. return 1;
  189. }
  190. identity_key = PEM_read_PrivateKey(f, NULL, NULL, NULL);
  191. if (!identity_key) {
  192. log_err(LD_GENERAL, "Couldn't read identity key from %s",
  193. identity_key_file);
  194. return 1;
  195. }
  196. fclose(f);
  197. }
  198. return 0;
  199. }
  200. /** DOCDOC */
  201. static int
  202. generate_signing_key(void)
  203. {
  204. int fd;
  205. FILE *f;
  206. RSA *key;
  207. log_notice(LD_GENERAL, "Generating %d-bit RSA signing key.",
  208. SIGNING_KEY_BITS);
  209. if (!(key = RSA_generate_key(SIGNING_KEY_BITS, 65537, NULL, NULL))) {
  210. log_err(LD_GENERAL, "Couldn't generate signing key.");
  211. crypto_log_errors(LOG_ERR, "Generating signing key");
  212. return 1;
  213. }
  214. signing_key = EVP_PKEY_new();
  215. if (!(EVP_PKEY_assign_RSA(signing_key, key))) {
  216. log_err(LD_GENERAL, "Couldn't assign signing key.");
  217. return 1;
  218. }
  219. if ((fd = open(signing_key_file, O_CREAT|O_EXCL|O_WRONLY, 0600))<0) {
  220. log_err(LD_GENERAL, "Couldn't open %s for writing: %s",
  221. signing_key_file, strerror(errno));
  222. return 1;
  223. }
  224. if (!(f = fdopen(fd, "w"))) {
  225. close(fd);
  226. log_err(LD_GENERAL, "Couldn't open %s for writing: %s",
  227. signing_key_file, strerror(errno));
  228. return 1;
  229. }
  230. /* Write signing key with no encryption. */
  231. if (!PEM_write_RSAPrivateKey(f, key, NULL, NULL, 0, NULL, NULL)) {
  232. crypto_log_errors(LOG_WARN, "writing signing key");
  233. return 1;
  234. }
  235. fclose(f);
  236. return 0;
  237. }
  238. static char *
  239. key_to_string(EVP_PKEY *key)
  240. {
  241. BUF_MEM *buf;
  242. BIO *b;
  243. RSA *rsa = EVP_PKEY_get1_RSA(key);
  244. char *result;
  245. if (!rsa)
  246. return NULL;
  247. b = BIO_new(BIO_s_mem());
  248. if (!PEM_write_bio_RSAPublicKey(b, rsa)) {
  249. crypto_log_errors(LOG_WARN, "writing public key to string");
  250. return NULL;
  251. }
  252. BIO_get_mem_ptr(b, &buf);
  253. (void) BIO_set_close(b, BIO_NOCLOSE);
  254. BIO_free(b);
  255. result = tor_malloc(buf->length + 1);
  256. memcpy(result, buf->data, buf->length);
  257. result[buf->length] = 0;
  258. BUF_MEM_free(buf);
  259. return result;
  260. }
  261. static int
  262. get_fingerprint(EVP_PKEY *pkey, char *out)
  263. {
  264. int r = 1;
  265. crypto_pk_env_t *pk = _crypto_new_pk_env_rsa(EVP_PKEY_get1_RSA(pkey));
  266. if (pk) {
  267. r = crypto_pk_get_fingerprint(pk, out, 0);
  268. crypto_free_pk_env(pk);
  269. }
  270. return r;
  271. }
  272. static int
  273. generate_certificate(void)
  274. {
  275. char buf[8192];
  276. time_t now = time(NULL);
  277. struct tm tm;
  278. char published[ISO_TIME_LEN+1];
  279. char expires[ISO_TIME_LEN+1];
  280. char fingerprint[FINGERPRINT_LEN+1];
  281. char *ident = key_to_string(identity_key);
  282. char *signing = key_to_string(signing_key);
  283. FILE *f;
  284. size_t signed_len;
  285. char digest[DIGEST_LEN];
  286. char signature[1024]; /* handles up to 8192-bit keys. */
  287. int r;
  288. get_fingerprint(identity_key, fingerprint);
  289. tor_localtime_r(&now, &tm);
  290. tm.tm_mon += months_lifetime;
  291. format_iso_time(published, now);
  292. format_iso_time(expires, mktime(&tm));
  293. tor_snprintf(buf, sizeof(buf),
  294. "dir-key-certificate-version 3\n"
  295. "fingerprint %s\n"
  296. "dir-key-published %s\n"
  297. "dir-key-expires %s\n"
  298. "dir-identity-key\n%s"
  299. "dir-signing-key\n%s"
  300. "dir-key-certification\n",
  301. fingerprint, published, expires, ident, signing);
  302. signed_len = strlen(buf);
  303. SHA1((const unsigned char*)buf,signed_len,(unsigned char*)digest);
  304. r = RSA_private_encrypt(DIGEST_LEN, (unsigned char*)digest,
  305. (unsigned char*)signature,
  306. EVP_PKEY_get1_RSA(identity_key),
  307. RSA_PKCS1_PADDING);
  308. strlcat(buf, "-----BEGIN SIGNATURE-----\n", sizeof(buf));
  309. signed_len = strlen(buf);
  310. base64_encode(buf+signed_len, sizeof(buf)-signed_len, signature, r);
  311. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  312. if (!(f = fopen(certificate_file, "w"))) {
  313. log_err(LD_GENERAL, "Couldn't open %s for writing: %s",
  314. certificate_file, strerror(errno));
  315. return 1;
  316. }
  317. fputs(buf, f);
  318. fclose(f);
  319. return 0;
  320. }
  321. /** Entry point to tor-gencert */
  322. int
  323. main(int argc, char **argv)
  324. {
  325. int r = 1;
  326. /* Don't bother using acceleration. */
  327. if (crypto_global_init(0)) {
  328. fprintf(stderr, "Couldn't initialize crypto library.\n");
  329. return 1;
  330. }
  331. if (crypto_seed_rng()) {
  332. fprintf(stderr, "Couldn't seed RNG.\n");
  333. goto done;
  334. }
  335. /* Make sure that files are made private. */
  336. umask(0077);
  337. if (parse_commandline(argc, argv))
  338. goto done;
  339. if (load_identity_key())
  340. goto done;
  341. if (generate_signing_key())
  342. goto done;
  343. if (generate_certificate())
  344. goto done;
  345. r = 0;
  346. done:
  347. if (identity_key)
  348. EVP_PKEY_free(identity_key);
  349. if (signing_key)
  350. EVP_PKEY_free(signing_key);
  351. crypto_global_cleanup();
  352. return r;
  353. }