tor-gencert.c 11 KB

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