tor-gencert.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* Copyright (c) 2007 The Tor Project, Inc. */
  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 reuse_signing_key = 0;
  34. int verbose = 0;
  35. int make_new_id = 0;
  36. int months_lifetime = DEFAULT_LIFETIME;
  37. char *address = NULL;
  38. EVP_PKEY *identity_key = NULL;
  39. EVP_PKEY *signing_key = NULL;
  40. /* DOCDOC */
  41. static void
  42. show_help(void)
  43. {
  44. fprintf(stderr, "Syntax:\n"
  45. "tor-gencert [-h|--help] [-v] [--create-identity-key] "
  46. "[-i identity_key_file]\n"
  47. " [-s signing_key_file] [-c certificate_file] "
  48. "[--reuse-signing-key]\n");
  49. }
  50. /* XXXX copied from crypto.c */
  51. static void
  52. crypto_log_errors(int severity, const char *doing)
  53. {
  54. unsigned int err;
  55. const char *msg, *lib, *func;
  56. while ((err = ERR_get_error()) != 0) {
  57. msg = (const char*)ERR_reason_error_string(err);
  58. lib = (const char*)ERR_lib_error_string(err);
  59. func = (const char*)ERR_func_error_string(err);
  60. if (!msg) msg = "(null)";
  61. if (!lib) lib = "(null)";
  62. if (!func) func = "(null)";
  63. if (doing) {
  64. log(severity, LD_CRYPTO, "crypto error while %s: %s (in %s:%s)",
  65. doing, msg, lib, func);
  66. } else {
  67. log(severity, LD_CRYPTO, "crypto error: %s (in %s:%s)", msg, lib, func);
  68. }
  69. }
  70. }
  71. /** DOCDOC */
  72. static int
  73. parse_commandline(int argc, char **argv)
  74. {
  75. int i;
  76. for (i = 1; i < argc; ++i) {
  77. if (!strcmp(argv[i], "--help") || !strcmp(argv[i], "-h")) {
  78. show_help();
  79. return 1;
  80. } else if (!strcmp(argv[i], "-i")) {
  81. if (i+1>=argc) {
  82. fprintf(stderr, "No argument to -i\n");
  83. return 1;
  84. }
  85. identity_key_file = tor_strdup(argv[++i]);
  86. } else if (!strcmp(argv[i], "-s")) {
  87. if (i+1>=argc) {
  88. fprintf(stderr, "No argument to -s\n");
  89. return 1;
  90. }
  91. signing_key_file = tor_strdup(argv[++i]);
  92. } else if (!strcmp(argv[i], "-c")) {
  93. if (i+1>=argc) {
  94. fprintf(stderr, "No argument to -c\n");
  95. return 1;
  96. }
  97. certificate_file = tor_strdup(argv[++i]);
  98. } else if (!strcmp(argv[i], "-m")) {
  99. if (i+1>=argc) {
  100. fprintf(stderr, "No argument to -m\n");
  101. return 1;
  102. }
  103. months_lifetime = atoi(argv[++i]);
  104. if (months_lifetime > 24 || months_lifetime < 0) {
  105. fprintf(stderr, "Lifetime (in months) was out of range.");
  106. return 1;
  107. }
  108. } else if (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--reuse")) {
  109. reuse_signing_key = 1;
  110. } else if (!strcmp(argv[i], "-v")) {
  111. verbose = 1;
  112. } else if (!strcmp(argv[i], "-a")) {
  113. uint32_t addr;
  114. uint16_t port;
  115. char b[INET_NTOA_BUF_LEN];
  116. struct in_addr in;
  117. if (i+1>=argc) {
  118. fprintf(stderr, "No argument to -a\n");
  119. return 1;
  120. }
  121. if (parse_addr_port(LOG_ERR, argv[++i], NULL, &addr, &port)<0)
  122. return 1;
  123. in.s_addr = htonl(addr);
  124. tor_inet_ntoa(&in, b, sizeof(b));
  125. address = tor_malloc(INET_NTOA_BUF_LEN+32);
  126. tor_snprintf(address, INET_NTOA_BUF_LEN+32, "%s:%d", b, (int)port);
  127. } else if (!strcmp(argv[i], "--create-identity-key")) {
  128. make_new_id = 1;
  129. } else {
  130. fprintf(stderr, "Unrecognized option %s\n", argv[i]);
  131. return 1;
  132. }
  133. }
  134. if (verbose) {
  135. add_stream_log(LOG_INFO, LOG_ERR, "<stderr>", stderr);
  136. } else {
  137. add_stream_log(LOG_NOTICE, LOG_ERR, "<stderr>", stderr);
  138. }
  139. if (!identity_key_file) {
  140. identity_key_file = tor_strdup("./authority_identity_key");
  141. log_info(LD_GENERAL, "No identity key file given; defaulting to %s",
  142. identity_key_file);
  143. }
  144. if (!signing_key_file) {
  145. signing_key_file = tor_strdup("./authority_signing_key");
  146. log_info(LD_GENERAL, "No signing key file given; defaulting to %s",
  147. signing_key_file);
  148. }
  149. if (!certificate_file) {
  150. certificate_file = tor_strdup("./authority_certificate");
  151. log_info(LD_GENERAL, "No signing key file given; defaulting to %s",
  152. certificate_file);
  153. }
  154. return 0;
  155. }
  156. /** DOCDOC */
  157. static int
  158. load_identity_key(void)
  159. {
  160. file_status_t status = file_status(identity_key_file);
  161. FILE *f;
  162. if (make_new_id) {
  163. open_file_t *open_file = NULL;
  164. RSA *key;
  165. if (status != FN_NOENT) {
  166. log_err(LD_GENERAL, "--create-identity-key was specified, but %s "
  167. "already exists.", identity_key_file);
  168. return 1;
  169. }
  170. log_notice(LD_GENERAL, "Generating %d-bit RSA identity key.",
  171. IDENTITY_KEY_BITS);
  172. if (!(key = RSA_generate_key(IDENTITY_KEY_BITS, 65537, NULL, NULL))) {
  173. log_err(LD_GENERAL, "Couldn't generate identity key.");
  174. crypto_log_errors(LOG_ERR, "Generating identity key");
  175. return 1;
  176. }
  177. identity_key = EVP_PKEY_new();
  178. if (!(EVP_PKEY_assign_RSA(identity_key, key))) {
  179. log_err(LD_GENERAL, "Couldn't assign identity key.");
  180. return 1;
  181. }
  182. if (!(f = start_writing_to_stdio_file(identity_key_file,
  183. OPEN_FLAGS_REPLACE, 0400,
  184. &open_file)))
  185. return 1;
  186. if (!PEM_write_PKCS8PrivateKey_nid(f, identity_key,
  187. NID_pbe_WithSHA1And3_Key_TripleDES_CBC,
  188. NULL, 0, /* no password here. */
  189. NULL, NULL)) {
  190. log_err(LD_GENERAL, "Couldn't write identity key to %s",
  191. identity_key_file);
  192. crypto_log_errors(LOG_ERR, "Writing identity key");
  193. abort_writing_to_file(open_file);
  194. return 1;
  195. }
  196. finish_writing_to_file(open_file);
  197. } else {
  198. if (status != FN_FILE) {
  199. log_err(LD_GENERAL,
  200. "No identity key found in %s. To specify a location "
  201. "for an identity key, use -i. To generate a new identity key, "
  202. "use --create-identity-key.", identity_key_file);
  203. return 1;
  204. }
  205. if (!(f = fopen(identity_key_file, "r"))) {
  206. log_err(LD_GENERAL, "Couldn't open %s for reading: %s",
  207. identity_key_file, strerror(errno));
  208. return 1;
  209. }
  210. identity_key = PEM_read_PrivateKey(f, NULL, NULL, NULL);
  211. if (!identity_key) {
  212. log_err(LD_GENERAL, "Couldn't read identity key from %s",
  213. identity_key_file);
  214. return 1;
  215. }
  216. fclose(f);
  217. }
  218. return 0;
  219. }
  220. /** DOCDOC */
  221. static int
  222. load_signing_key(void)
  223. {
  224. FILE *f;
  225. if (!(f = fopen(signing_key_file, "r"))) {
  226. log_err(LD_GENERAL, "Couldn't open %s for reading: %s",
  227. signing_key_file, strerror(errno));
  228. return 1;
  229. }
  230. if (!(signing_key = PEM_read_PrivateKey(f, NULL, NULL, NULL))) {
  231. log_err(LD_GENERAL, "Couldn't read siging key from %s", signing_key_file);
  232. return 1;
  233. }
  234. fclose(f);
  235. return 0;
  236. }
  237. /** DOCDOC */
  238. static int
  239. generate_signing_key(void)
  240. {
  241. open_file_t *open_file;
  242. FILE *f;
  243. RSA *key;
  244. log_notice(LD_GENERAL, "Generating %d-bit RSA signing key.",
  245. SIGNING_KEY_BITS);
  246. if (!(key = RSA_generate_key(SIGNING_KEY_BITS, 65537, NULL, NULL))) {
  247. log_err(LD_GENERAL, "Couldn't generate signing key.");
  248. crypto_log_errors(LOG_ERR, "Generating signing key");
  249. return 1;
  250. }
  251. signing_key = EVP_PKEY_new();
  252. if (!(EVP_PKEY_assign_RSA(signing_key, key))) {
  253. log_err(LD_GENERAL, "Couldn't assign signing key.");
  254. return 1;
  255. }
  256. if (!(f = start_writing_to_stdio_file(signing_key_file,
  257. OPEN_FLAGS_REPLACE, 0600,
  258. &open_file)))
  259. return 1;
  260. /* Write signing key with no encryption. */
  261. if (!PEM_write_RSAPrivateKey(f, key, NULL, NULL, 0, NULL, NULL)) {
  262. crypto_log_errors(LOG_WARN, "writing signing key");
  263. abort_writing_to_file(open_file);
  264. return 1;
  265. }
  266. finish_writing_to_file(open_file);
  267. return 0;
  268. }
  269. static char *
  270. key_to_string(EVP_PKEY *key)
  271. {
  272. BUF_MEM *buf;
  273. BIO *b;
  274. RSA *rsa = EVP_PKEY_get1_RSA(key);
  275. char *result;
  276. if (!rsa)
  277. return NULL;
  278. b = BIO_new(BIO_s_mem());
  279. if (!PEM_write_bio_RSAPublicKey(b, rsa)) {
  280. crypto_log_errors(LOG_WARN, "writing public key to string");
  281. return NULL;
  282. }
  283. BIO_get_mem_ptr(b, &buf);
  284. (void) BIO_set_close(b, BIO_NOCLOSE);
  285. BIO_free(b);
  286. result = tor_malloc(buf->length + 1);
  287. memcpy(result, buf->data, buf->length);
  288. result[buf->length] = 0;
  289. BUF_MEM_free(buf);
  290. return result;
  291. }
  292. static int
  293. get_fingerprint(EVP_PKEY *pkey, char *out)
  294. {
  295. int r = 1;
  296. crypto_pk_env_t *pk = _crypto_new_pk_env_rsa(EVP_PKEY_get1_RSA(pkey));
  297. if (pk) {
  298. r = crypto_pk_get_fingerprint(pk, out, 0);
  299. crypto_free_pk_env(pk);
  300. }
  301. return r;
  302. }
  303. static int
  304. generate_certificate(void)
  305. {
  306. char buf[8192];
  307. time_t now = time(NULL);
  308. struct tm tm;
  309. char published[ISO_TIME_LEN+1];
  310. char expires[ISO_TIME_LEN+1];
  311. char fingerprint[FINGERPRINT_LEN+1];
  312. char *ident = key_to_string(identity_key);
  313. char *signing = key_to_string(signing_key);
  314. FILE *f;
  315. size_t signed_len;
  316. char digest[DIGEST_LEN];
  317. char signature[1024]; /* handles up to 8192-bit keys. */
  318. int r;
  319. get_fingerprint(identity_key, fingerprint);
  320. tor_localtime_r(&now, &tm);
  321. tm.tm_mon += months_lifetime;
  322. format_iso_time(published, now);
  323. format_iso_time(expires, mktime(&tm));
  324. tor_snprintf(buf, sizeof(buf),
  325. "dir-key-certificate-version 3"
  326. "%s%s"
  327. "\nfingerprint %s\n"
  328. "dir-key-published %s\n"
  329. "dir-key-expires %s\n"
  330. "dir-identity-key\n%s"
  331. "dir-signing-key\n%s"
  332. "dir-key-certification\n",
  333. address?"\ndir-address ":"", address?address:"",
  334. fingerprint, published, expires, ident, signing);
  335. signed_len = strlen(buf);
  336. SHA1((const unsigned char*)buf,signed_len,(unsigned char*)digest);
  337. r = RSA_private_encrypt(DIGEST_LEN, (unsigned char*)digest,
  338. (unsigned char*)signature,
  339. EVP_PKEY_get1_RSA(identity_key),
  340. RSA_PKCS1_PADDING);
  341. strlcat(buf, "-----BEGIN SIGNATURE-----\n", sizeof(buf));
  342. signed_len = strlen(buf);
  343. base64_encode(buf+signed_len, sizeof(buf)-signed_len, signature, r);
  344. strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
  345. if (!(f = fopen(certificate_file, "w"))) {
  346. log_err(LD_GENERAL, "Couldn't open %s for writing: %s",
  347. certificate_file, strerror(errno));
  348. return 1;
  349. }
  350. fputs(buf, f);
  351. fclose(f);
  352. return 0;
  353. }
  354. /** Entry point to tor-gencert */
  355. int
  356. main(int argc, char **argv)
  357. {
  358. int r = 1;
  359. init_logging();
  360. /* Don't bother using acceleration. */
  361. if (crypto_global_init(0)) {
  362. fprintf(stderr, "Couldn't initialize crypto library.\n");
  363. return 1;
  364. }
  365. if (crypto_seed_rng()) {
  366. fprintf(stderr, "Couldn't seed RNG.\n");
  367. goto done;
  368. }
  369. /* Make sure that files are made private. */
  370. umask(0077);
  371. if (parse_commandline(argc, argv))
  372. goto done;
  373. if (load_identity_key())
  374. goto done;
  375. if (reuse_signing_key) {
  376. if (load_signing_key())
  377. goto done;
  378. } else {
  379. if (generate_signing_key())
  380. goto done;
  381. }
  382. if (generate_certificate())
  383. goto done;
  384. r = 0;
  385. done:
  386. if (identity_key)
  387. EVP_PKEY_free(identity_key);
  388. if (signing_key)
  389. EVP_PKEY_free(signing_key);
  390. tor_free(address);
  391. crypto_global_cleanup();
  392. return r;
  393. }