crypto.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664
  1. /* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
  2. /* See LICENSE for licensing information */
  3. /* $Id$ */
  4. #include <string.h>
  5. #include <openssl/err.h>
  6. #include <openssl/rsa.h>
  7. #include <openssl/pem.h>
  8. #include <openssl/evp.h>
  9. #include <openssl/rand.h>
  10. #include <openssl/opensslv.h>
  11. #include <stdlib.h>
  12. #include <assert.h>
  13. #include <stdio.h>
  14. #include "crypto.h"
  15. #include "../or/or.h"
  16. #include "log.h"
  17. #if OPENSSL_VERSION_NUMBER < 0x00905000l
  18. #error "We require openssl >= 0.9.5"
  19. #elif OPENSSL_VERSION_NUMBER < 0x00906000l
  20. #define OPENSSL_095
  21. #endif
  22. /*
  23. * Certain functions that return a success code in OpenSSL 0.9.6 return void
  24. * (and don't indicate errors) in OpenSSL version 0.9.5.
  25. *
  26. * [OpenSSL 0.9.5 matters, because it ships with Redhat 6.2.]
  27. */
  28. #ifdef OPENSSL_095
  29. #define RETURN_SSL_OUTCOME(exp) (exp); return 0
  30. #else
  31. #define RETURN_SSL_OUTCOME(exp) return !(exp)
  32. #endif
  33. static inline int
  34. crypto_cipher_iv_length(int type) {
  35. switch(type)
  36. {
  37. case CRYPTO_CIPHER_IDENTITY: return 0;
  38. case CRYPTO_CIPHER_DES: return 8;
  39. case CRYPTO_CIPHER_RC4: return 16;
  40. case CRYPTO_CIPHER_3DES: return 8;
  41. default: assert(0); return -1;
  42. }
  43. }
  44. static inline int
  45. crypto_cipher_key_length(int type) {
  46. switch(type)
  47. {
  48. case CRYPTO_CIPHER_IDENTITY: return 0;
  49. case CRYPTO_CIPHER_DES: return 8;
  50. case CRYPTO_CIPHER_RC4: return 16;
  51. case CRYPTO_CIPHER_3DES: return 16;
  52. default: assert(0); return -1;
  53. }
  54. }
  55. static inline EVP_CIPHER *
  56. crypto_cipher_evp_cipher(int type, int enc) {
  57. switch(type)
  58. {
  59. case CRYPTO_CIPHER_IDENTITY: return EVP_enc_null();
  60. case CRYPTO_CIPHER_DES: return EVP_des_ofb();
  61. case CRYPTO_CIPHER_RC4: return EVP_rc4();
  62. case CRYPTO_CIPHER_3DES: return EVP_des_ede_ofb();
  63. default: return NULL;
  64. }
  65. }
  66. int crypto_global_init()
  67. {
  68. ERR_load_crypto_strings();
  69. return 0;
  70. }
  71. int crypto_global_cleanup()
  72. {
  73. ERR_free_strings();
  74. return 0;
  75. }
  76. crypto_pk_env_t *crypto_new_pk_env(int type)
  77. {
  78. crypto_pk_env_t *env;
  79. env = (crypto_pk_env_t *)malloc(sizeof(crypto_pk_env_t));
  80. if (!env)
  81. return 0;
  82. env->type = type;
  83. env->refs = 1;
  84. env->key = NULL;
  85. env->aux = NULL;
  86. switch(type) {
  87. case CRYPTO_PK_RSA:
  88. env->key = (unsigned char *)RSA_new();
  89. if (!env->key) {
  90. free((void *)env);
  91. return NULL;
  92. }
  93. break;
  94. default:
  95. free((void *)env);
  96. return NULL;
  97. break;
  98. }
  99. return env;
  100. }
  101. void crypto_free_pk_env(crypto_pk_env_t *env)
  102. {
  103. assert(env);
  104. if(--env->refs > 0)
  105. return;
  106. switch(env->type) {
  107. case CRYPTO_PK_RSA:
  108. if (env->key)
  109. RSA_free((RSA *)env->key);
  110. break;
  111. default:
  112. break;
  113. }
  114. free((void *)env);
  115. return;
  116. }
  117. /* Create a new crypto_cipher_env_t for a given onion cipher type, key,
  118. * iv, and encryption flag (1=encrypt, 0=decrypt). Return the crypto object
  119. * on success; NULL on failure.
  120. */
  121. crypto_cipher_env_t *
  122. crypto_create_init_cipher(int cipher_type, char *key, char *iv, int encrypt_mode)
  123. {
  124. int r;
  125. crypto_cipher_env_t *crypto = NULL;
  126. if (! (crypto = crypto_new_cipher_env(cipher_type))) {
  127. log(LOG_ERR, "Unable to allocate crypto object");
  128. return NULL;
  129. }
  130. if (crypto_cipher_set_key(crypto, key)) {
  131. log(LOG_ERR, "Unable to set key: %s", crypto_perror());
  132. goto error;
  133. }
  134. if (crypto_cipher_set_iv(crypto, iv)) {
  135. log(LOG_ERR, "Unable to set iv: %s", crypto_perror());
  136. goto error;
  137. }
  138. if (encrypt_mode)
  139. r = crypto_cipher_encrypt_init_cipher(crypto);
  140. else
  141. r = crypto_cipher_decrypt_init_cipher(crypto);
  142. if (r) {
  143. log(LOG_ERR, "Unabble to initialize cipher: %s", crypto_perror());
  144. goto error;
  145. }
  146. return crypto;
  147. error:
  148. if (crypto)
  149. crypto_free_cipher_env(crypto);
  150. return NULL;
  151. }
  152. crypto_cipher_env_t *crypto_new_cipher_env(int type)
  153. {
  154. crypto_cipher_env_t *env;
  155. int iv_len, key_len;
  156. env = (crypto_cipher_env_t *)malloc(sizeof(crypto_cipher_env_t));
  157. if (!env)
  158. return NULL;
  159. env->type = type;
  160. env->key = NULL;
  161. env->iv = NULL;
  162. env->aux = NULL;
  163. iv_len = crypto_cipher_iv_length(type);
  164. key_len = crypto_cipher_key_length(type);
  165. if (! crypto_cipher_evp_cipher(type,0))
  166. /* This is not an openssl cipher */
  167. goto err;
  168. else {
  169. env->aux = (unsigned char *)malloc(sizeof(EVP_CIPHER_CTX));
  170. EVP_CIPHER_CTX_init((EVP_CIPHER_CTX *)env->aux);
  171. }
  172. if (iv_len && !(env->iv = (unsigned char *)malloc(iv_len)))
  173. goto err;
  174. if (key_len && !(env->key = (unsigned char *)malloc(key_len)))
  175. goto err;
  176. return env;
  177. err:
  178. if (env->key)
  179. free(env->key);
  180. if (env->iv)
  181. free(env->iv);
  182. if (env->aux)
  183. free(env->aux);
  184. if (env)
  185. free(env);
  186. return NULL;
  187. }
  188. void crypto_free_cipher_env(crypto_cipher_env_t *env)
  189. {
  190. assert(env);
  191. if (crypto_cipher_evp_cipher(env->type,0)) {
  192. /* This is an openssl cipher */
  193. assert(env->aux);
  194. EVP_CIPHER_CTX_cleanup((EVP_CIPHER_CTX *)env->aux);
  195. }
  196. if (env->aux)
  197. free((void *)env->aux);
  198. if (env->iv)
  199. free((void *)env->iv);
  200. if (env->key)
  201. free((void *)env->key);
  202. free((void *)env);
  203. }
  204. /* public key crypto */
  205. int crypto_pk_generate_key(crypto_pk_env_t *env)
  206. {
  207. assert(env);
  208. switch(env->type) {
  209. case CRYPTO_PK_RSA:
  210. if (env->key)
  211. RSA_free((RSA *)env->key);
  212. env->key = (unsigned char *)RSA_generate_key(1024,65537, NULL, NULL);
  213. if (!env->key)
  214. return -1;
  215. break;
  216. default:
  217. return -1;
  218. }
  219. return 0;
  220. }
  221. int crypto_pk_read_private_key_from_file(crypto_pk_env_t *env, FILE *src)
  222. {
  223. assert(env && src);
  224. switch(env->type) {
  225. case CRYPTO_PK_RSA:
  226. if (env->key)
  227. RSA_free((RSA *)env->key);
  228. env->key = (unsigned char *)PEM_read_RSAPrivateKey(src, NULL, NULL, NULL);
  229. if (!env->key)
  230. return -1;
  231. break;
  232. default :
  233. return -1;
  234. }
  235. return 0;
  236. }
  237. int crypto_pk_read_private_key_from_filename(crypto_pk_env_t *env, unsigned char *keyfile)
  238. {
  239. FILE *f_pr;
  240. int retval = 0;
  241. assert(env && keyfile);
  242. if (strspn(keyfile,CONFIG_LEGAL_FILENAME_CHARACTERS) == strlen(keyfile)) /* filename contains legal characters only */
  243. {
  244. /* open the keyfile */
  245. f_pr=fopen(keyfile,"rb");
  246. if (!f_pr)
  247. return -1;
  248. /* read the private key */
  249. retval = crypto_pk_read_private_key_from_file(env, f_pr);
  250. fclose(f_pr);
  251. if (retval == -1)
  252. {
  253. log(LOG_ERR,"Error reading private key : %s",crypto_perror());
  254. return -1;
  255. }
  256. /* check the private key */
  257. retval = crypto_pk_check_key(env);
  258. if (retval == 0)
  259. {
  260. log(LOG_ERR,"Private key read but is invalid : %s.", crypto_perror());
  261. return -1;
  262. }
  263. else if (retval == -1)
  264. {
  265. log(LOG_ERR,"Private key read but validity checking failed : %s",crypto_perror());
  266. return -1;
  267. }
  268. else if (retval == 1)
  269. {
  270. return 0;
  271. }
  272. } /* filename contains legal characters only */
  273. return -1; /* report error */
  274. }
  275. int crypto_pk_read_public_key_from_file(crypto_pk_env_t *env, FILE *src)
  276. {
  277. assert(env && src);
  278. switch(env->type) {
  279. case CRYPTO_PK_RSA:
  280. if(env->key)
  281. RSA_free((RSA *)env->key);
  282. env->key = (unsigned char *)PEM_read_RSAPublicKey(src, NULL, NULL, NULL);
  283. if (!env->key)
  284. return -1;
  285. break;
  286. default :
  287. return -1;
  288. }
  289. return 0;
  290. }
  291. int crypto_pk_write_public_key_to_string(crypto_pk_env_t *env, char **dest, int *len) {
  292. BUF_MEM *buf;
  293. BIO *b;
  294. assert(env && env->key && dest);
  295. switch(env->type) {
  296. case CRYPTO_PK_RSA:
  297. b = BIO_new(BIO_s_mem()); /* Create a memory BIO */
  298. /* Now you can treat b as if it were a file. Just use the
  299. * * PEM_*_bio_* functions instead of the non-bio variants.
  300. * */
  301. if(!PEM_write_bio_RSAPublicKey(b, (RSA *)env->key))
  302. return -1;
  303. BIO_get_mem_ptr(b, &buf);
  304. BIO_set_close(b, BIO_NOCLOSE); /* so BIO_free doesn't free buf */
  305. BIO_free(b);
  306. *dest = malloc(buf->length+1);
  307. if(!*dest)
  308. return -1;
  309. memcpy(*dest, buf->data, buf->length);
  310. (*dest)[buf->length] = 0; /* null terminate it */
  311. *len = buf->length;
  312. BUF_MEM_free(buf);
  313. break;
  314. default:
  315. return -1;
  316. }
  317. return 0;
  318. }
  319. int crypto_pk_read_public_key_from_string(crypto_pk_env_t *env, char *src, int len) {
  320. BIO *b;
  321. assert(env && src);
  322. switch(env->type) {
  323. case CRYPTO_PK_RSA:
  324. b = BIO_new(BIO_s_mem()); /* Create a memory BIO */
  325. BIO_write(b, src, len);
  326. RSA_free((RSA *)env->key);
  327. env->key = (unsigned char *)PEM_read_bio_RSAPublicKey(b, NULL, NULL, NULL);
  328. if(!env->key)
  329. return -1;
  330. BIO_free(b);
  331. break;
  332. default:
  333. return -1;
  334. }
  335. return 0;
  336. }
  337. int crypto_pk_write_private_key_to_file(crypto_pk_env_t *env, FILE *dest)
  338. {
  339. assert(env && dest);
  340. switch(env->type) {
  341. case CRYPTO_PK_RSA:
  342. if (!env->key)
  343. return -1;
  344. if (PEM_write_RSAPrivateKey(dest, (RSA *)env->key, NULL, NULL, 0,0, NULL) == 0)
  345. return -1;
  346. break;
  347. default :
  348. return -1;
  349. }
  350. return 0;
  351. }
  352. int crypto_pk_write_public_key_to_file(crypto_pk_env_t *env, FILE *dest)
  353. {
  354. assert(env && dest);
  355. switch(env->type) {
  356. case CRYPTO_PK_RSA:
  357. if (!env->key)
  358. return -1;
  359. if (PEM_write_RSAPublicKey(dest, (RSA *)env->key) == 0)
  360. return -1;
  361. break;
  362. default :
  363. return -1;
  364. }
  365. return 0;
  366. }
  367. int crypto_pk_check_key(crypto_pk_env_t *env)
  368. {
  369. assert(env);
  370. switch(env->type) {
  371. case CRYPTO_PK_RSA:
  372. return RSA_check_key((RSA *)env->key);
  373. default:
  374. return -1;
  375. }
  376. }
  377. int crypto_pk_set_key(crypto_pk_env_t *env, unsigned char *key)
  378. {
  379. assert(env && key);
  380. switch(env->type) {
  381. case CRYPTO_PK_RSA:
  382. if (!env->key)
  383. return -1;
  384. /* XXX BUG XXX you can't memcpy an RSA, it's got a bunch of subpointers */
  385. assert(0);
  386. memcpy((void *)env->key, (void *)key, sizeof(RSA));
  387. break;
  388. default :
  389. return -1;
  390. }
  391. return 0;
  392. }
  393. int crypto_pk_cmp_keys(crypto_pk_env_t *a, crypto_pk_env_t *b) {
  394. int result;
  395. if (!a || !b)
  396. return -1;
  397. if (!a->key || !b->key)
  398. return -1;
  399. if (a->type != b->type)
  400. return -1;
  401. switch(a->type) {
  402. case CRYPTO_PK_RSA:
  403. assert(((RSA *)a->key)->n && ((RSA *)a->key)->e && ((RSA *)b->key)->n && ((RSA *)b->key)->e);
  404. result = BN_cmp(((RSA *)a->key)->n, ((RSA *)b->key)->n);
  405. if (result)
  406. return result;
  407. return BN_cmp(((RSA *)a->key)->e, ((RSA *)b->key)->e);
  408. default:
  409. return -1;
  410. }
  411. }
  412. int crypto_pk_keysize(crypto_pk_env_t *env)
  413. {
  414. assert(env && env->key);
  415. return RSA_size((RSA *)env->key);
  416. }
  417. crypto_pk_env_t *crypto_pk_dup_key(crypto_pk_env_t *env) {
  418. assert(env && env->key);
  419. switch(env->type) {
  420. case CRYPTO_PK_RSA:
  421. env->refs++;
  422. break;
  423. default:
  424. return NULL;
  425. }
  426. return env;
  427. }
  428. int crypto_pk_public_encrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding)
  429. {
  430. assert(env && from && to);
  431. switch(env->type) {
  432. case CRYPTO_PK_RSA:
  433. return RSA_public_encrypt(fromlen, from, to, (RSA *)env->key, padding);
  434. default:
  435. return -1;
  436. }
  437. }
  438. int crypto_pk_private_decrypt(crypto_pk_env_t *env, unsigned char *from, int fromlen, unsigned char *to, int padding)
  439. {
  440. assert(env && from && to);
  441. switch(env->type) {
  442. case CRYPTO_PK_RSA:
  443. if (!(((RSA*)env->key)->p))
  444. return -1;
  445. return RSA_private_decrypt(fromlen, from, to, (RSA *)env->key, padding);
  446. default:
  447. return -1;
  448. }
  449. }
  450. /* symmetric crypto */
  451. int crypto_cipher_generate_key(crypto_cipher_env_t *env)
  452. {
  453. int key_len;
  454. assert(env);
  455. key_len = crypto_cipher_key_length(env->type);
  456. if (key_len > 0)
  457. return crypto_rand(key_len, env->key);
  458. else if (key_len == 0)
  459. return 0;
  460. else
  461. return -1;
  462. }
  463. int crypto_cipher_set_iv(crypto_cipher_env_t *env, unsigned char *iv)
  464. {
  465. int iv_len;
  466. assert(env && iv);
  467. iv_len = crypto_cipher_iv_length(env->type);
  468. if (!iv_len)
  469. return 0;
  470. if (!env->iv)
  471. return -1;
  472. memcpy((void*)env->iv, (void*)iv, iv_len);
  473. return 0;
  474. }
  475. int crypto_cipher_set_key(crypto_cipher_env_t *env, unsigned char *key)
  476. {
  477. int key_len;
  478. assert(env && key);
  479. key_len = crypto_cipher_key_length(env->type);
  480. if (!key_len)
  481. return 0;
  482. if (!env->key)
  483. return -1;
  484. memcpy((void*)env->key, (void*)key, key_len);
  485. return 0;
  486. }
  487. int crypto_cipher_encrypt_init_cipher(crypto_cipher_env_t *env)
  488. {
  489. assert(env);
  490. if (crypto_cipher_evp_cipher(env->type, 1)) {
  491. RETURN_SSL_OUTCOME(EVP_EncryptInit((EVP_CIPHER_CTX *)env->aux,
  492. crypto_cipher_evp_cipher(env->type, 1),
  493. env->key, env->iv));
  494. } else {
  495. return -1;
  496. }
  497. }
  498. int crypto_cipher_decrypt_init_cipher(crypto_cipher_env_t *env)
  499. {
  500. assert(env);
  501. if (crypto_cipher_evp_cipher(env->type, 0)) {
  502. RETURN_SSL_OUTCOME(EVP_EncryptInit((EVP_CIPHER_CTX *)env->aux,
  503. crypto_cipher_evp_cipher(env->type, 0),
  504. env->key, env->iv));
  505. } else {
  506. return -1;
  507. }
  508. }
  509. int crypto_cipher_encrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to)
  510. {
  511. int tolen;
  512. assert(env && from && to);
  513. RETURN_SSL_OUTCOME(EVP_EncryptUpdate((EVP_CIPHER_CTX *)env->aux, to, &tolen, from, fromlen));
  514. }
  515. int crypto_cipher_decrypt(crypto_cipher_env_t *env, unsigned char *from, unsigned int fromlen, unsigned char *to)
  516. {
  517. int tolen;
  518. assert(env && from && to);
  519. RETURN_SSL_OUTCOME(EVP_DecryptUpdate((EVP_CIPHER_CTX *)env->aux, to, &tolen, from, fromlen));
  520. }
  521. /* SHA-1 */
  522. int crypto_SHA_digest(unsigned char *m, int len, unsigned char *digest)
  523. {
  524. assert(m && digest);
  525. return (SHA1(m,len,digest) == NULL);
  526. }
  527. /* random numbers */
  528. int crypto_rand(unsigned int n, unsigned char *to)
  529. {
  530. assert(to);
  531. return (RAND_bytes(to, n) != 1);
  532. }
  533. int crypto_pseudo_rand(unsigned int n, unsigned char *to)
  534. {
  535. assert(to);
  536. return (RAND_pseudo_bytes(to, n) == -1);
  537. }
  538. /* errors */
  539. char *crypto_perror()
  540. {
  541. return (char *)ERR_reason_error_string(ERR_get_error());
  542. }