crypto.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. #include <openssl/evp.h>
  2. #include <openssl/dh.h>
  3. #include <openssl/bn.h>
  4. #include <openssl/err.h>
  5. #include <openssl/rand.h>
  6. #include <openssl/ssl.h>
  7. #include "crypto.h"
  8. #include "flow.h"
  9. #include "slitheen.h"
  10. int update_finish_hash(flow *f, uint8_t *hs){
  11. //find handshake length
  12. const struct handshake_header *hs_hdr;
  13. uint8_t *p = hs;
  14. hs_hdr = (struct handshake_header*) p;
  15. uint32_t hs_len = HANDSHAKE_MESSAGE_LEN(hs_hdr);
  16. EVP_DigestUpdate(f->finish_md_ctx, hs, hs_len+4);
  17. return 1;
  18. }
  19. int extract_parameters(flow *f, uint8_t *hs){
  20. DH *dh;
  21. uint8_t *p;
  22. long i;
  23. p = hs + HANDSHAKE_HEADER_LEN;
  24. if((dh = DH_new()) == NULL){
  25. return 1;
  26. }
  27. /* Extract prime modulus */
  28. n2s(p,i);
  29. if(!(dh->p = BN_bin2bn(p,i,NULL))){
  30. return 1;
  31. }
  32. p += i;
  33. /* Extract generator */
  34. n2s(p,i);
  35. if(!(dh->g = BN_bin2bn(p,i,NULL))){
  36. return 1;
  37. }
  38. p += i;
  39. /* Extract server public value */
  40. n2s(p,i);
  41. if(!(dh->pub_key = BN_bin2bn(p,i,NULL))){
  42. return 1;
  43. }
  44. f->dh = dh;
  45. return 0;
  46. }
  47. /** MAC a message
  48. * TODO: look at tls1_mac in t1_enc.c
  49. * For now, only goes one way (macs message to be written)
  50. */
  51. int32_t mac(flow *f, uint8_t *input, uint8_t *output, int32_t len, int32_t incoming, int32_t type, int32_t enc){
  52. uint8_t *seq;
  53. uint8_t header[13];
  54. int32_t md_size;
  55. //TODO: update this is I need more than two
  56. seq = (incoming) ? f->read_seq : f->write_seq;
  57. header[8] = type;
  58. header[9] = 0x03;//TODO: update for different versions
  59. header[10] = 0x03;
  60. header[11] = (len) >> 8;
  61. header[12] = (len) & 0xff;
  62. EVP_DigestSignUpdate(f->read_mac_ctx, header, sizeof(header));
  63. EVP_DigestSignUpdate(f->read_mac_ctx, input, len);
  64. int32_t t = EVP_DigestSignFinal(f->read_mac_ctx, output, &md_size);
  65. return md_size;
  66. }
  67. /* Encrypt/decrypt message
  68. *
  69. */
  70. int encrypt(flow *f, uint8_t *input, uint8_t *output, int32_t len, int32_t incoming, int32_t type, int32_t enc){
  71. uint8_t *p = input;
  72. EVP_CIPHER_CTX *ds = (incoming) ? ((enc) ? f->srvr_write_ctx : f->clnt_read_ctx) : ((enc) ? f->clnt_write_ctx : f->srvr_read_ctx) ;
  73. if(ds == NULL){
  74. printf("FAIL\n");
  75. return 1;
  76. }
  77. //TODO: wrap my mind around this, might need 2 more
  78. uint8_t *seq;
  79. seq = (incoming) ? f->read_seq : f->write_seq;
  80. if(f->application && (ds->iv[EVP_GCM_TLS_FIXED_IV_LEN] == 0)){
  81. //fill in rest of iv
  82. for(int i = EVP_GCM_TLS_FIXED_IV_LEN; i< ds->cipher->iv_len; i++){
  83. ds->iv[i] = p[i- EVP_GCM_TLS_FIXED_IV_LEN];
  84. }
  85. }
  86. // printf("\t\tiv: ");
  87. // for(int i=0; i<ds->cipher->iv_len; i++){
  88. // printf("%02X ", ds->iv[i]);
  89. // }
  90. // printf("\n");
  91. int32_t bs = EVP_CIPHER_block_size(ds->cipher);
  92. printf("bs = %d\n", bs);
  93. //padding stuff? TODO: understand this
  94. uint8_t buf[13];
  95. memcpy(buf, seq, 8);
  96. for(int i=7; i>=0; i--){
  97. ++seq[i];
  98. if(seq[i] != 0)
  99. break;
  100. }
  101. buf[8] = type;
  102. buf[9] = 0x03;
  103. buf[10] = 0x03;
  104. buf[11] = len >> 8; //len >> 8;
  105. buf[12] = len & 0xff;//len *0xff;
  106. int32_t pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
  107. 13, buf); // = int32_t pad?
  108. printf("buf (%s): ", incoming ? "read" : "write");
  109. for(int i=0; i<13; i++){
  110. printf("%02x ", buf[i]);
  111. }
  112. printf("\n");
  113. if(enc)
  114. len += pad;
  115. /* printf("Decrypting: ");
  116. for(int i=0; i<len; i++){
  117. printf("%02x ", p[i]);
  118. }
  119. printf("\n");*/
  120. int32_t n = EVP_Cipher(ds, p, p, len); //decrypt in place
  121. if(n<0) return 0;
  122. printf("%s %d bytes\n", (enc) ? "Encrypted" : "Decrypted", n);
  123. printf("seq after (%s): ", incoming ? "read" : "write");
  124. for(int i=0; i<8; i++){
  125. printf("%02x ", seq[i]);
  126. }
  127. printf("\n");
  128. /* printf("Decrypted: ");
  129. for(int i=0; i<n; i++){
  130. printf("%02x ", p[i]);
  131. }
  132. printf("\n");
  133. printf("decrypted %d/%d bytes.\n", n, len);*/
  134. if(!enc)
  135. p[EVP_GCM_TLS_EXPLICIT_IV_LEN+n] = '\0';
  136. return n;
  137. }
  138. int verify_finish_hash(flow *f, uint8_t *p, int32_t incoming){
  139. EVP_MD_CTX ctx;
  140. uint8_t hash[EVP_MAX_MD_SIZE];
  141. int32_t hash_len;
  142. EVP_MD_CTX_init(&ctx);
  143. //get header length
  144. struct handshake_header *hs_hdr;
  145. hs_hdr = (struct handshake_header*) p;
  146. uint32_t fin_length = HANDSHAKE_MESSAGE_LEN(hs_hdr);
  147. p += HANDSHAKE_HEADER_LEN;
  148. //finalize hash of handshake msgs
  149. EVP_MD_CTX_copy_ex(&ctx, f->finish_md_ctx);
  150. EVP_DigestFinal_ex(&ctx, hash, &hash_len);
  151. //now use pseudorandom function
  152. uint8_t *output = calloc(1, fin_length);
  153. if(incoming){
  154. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE, TLS_MD_SERVER_FINISH_CONST, TLS_MD_SERVER_FINISH_CONST_SIZE , hash, hash_len, NULL, 0, NULL, 0, output, fin_length);
  155. } else {
  156. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE, TLS_MD_CLIENT_FINISH_CONST, TLS_MD_CLIENT_FINISH_CONST_SIZE , hash, hash_len, NULL, 0, NULL, 0, output, fin_length);
  157. }
  158. //now compare
  159. if(CRYPTO_memcmp(p, output, fin_length) != 0){
  160. printf("VERIFY FAILED\n");
  161. return 0;
  162. } else {
  163. printf("VERIFY PASSED\n");
  164. }
  165. return 1;
  166. }
  167. int compute_master_secret(flow *f){
  168. DH *dh_srvr = NULL;
  169. DH *dh_clnt = NULL;
  170. BN_CTX *ctx;
  171. BN_MONT_CTX *mont = NULL;
  172. BIGNUM *pub_key = NULL, *priv_key = NULL;
  173. ctx = BN_CTX_new();
  174. dh_srvr = f->dh;
  175. dh_clnt = DHparams_dup(dh_srvr);
  176. uint32_t l = dh_clnt->length ? dh_clnt->length : BN_num_bits(dh_clnt->p) - 1;
  177. int32_t bytes = (l+7) / 8;
  178. uint8_t *buf = (uint8_t *)OPENSSL_malloc(bytes);
  179. if (buf == NULL){
  180. BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
  181. return 1;
  182. }
  183. pub_key = BN_new();
  184. priv_key = BN_new();
  185. for(int i=0; i<bytes; i++){
  186. buf[i] = f->key[i%16];
  187. }
  188. if (!BN_bin2bn(buf, bytes, priv_key))
  189. return 1;
  190. {
  191. BIGNUM *prk;
  192. prk = priv_key;
  193. if (!dh_clnt->meth->bn_mod_exp(dh_clnt, pub_key, dh_clnt->g, prk, dh_clnt->p, ctx, mont)){
  194. printf("FAIL\n");
  195. return 1;
  196. }
  197. }
  198. dh_clnt->pub_key = pub_key;
  199. dh_clnt->priv_key = priv_key;
  200. // Compute master key
  201. uint8_t *pre_master_secret = calloc(1, 256);//TODO: find right length
  202. DH_compute_key(pre_master_secret, dh_srvr->pub_key, dh_clnt);
  203. PRF(pre_master_secret, PRE_MASTER_LEN, TLS_MD_MASTER_SECRET_CONST, TLS_MD_MASTER_SECRET_CONST_SIZE, f->client_random, SSL3_RANDOM_SIZE, f->server_random, SSL3_RANDOM_SIZE, NULL, 0, f->master_secret, SSL3_MASTER_SECRET_SIZE);
  204. //remove pre_master_secret from memory
  205. memset(pre_master_secret, 0, PRE_MASTER_LEN);
  206. /*printf("master secret:\n");
  207. for(int i=0; i< 48; i++){
  208. printf("%02x ", f->master_secret[i]);
  209. }
  210. printf("\n");*/
  211. free(pre_master_secret);
  212. DH_free(dh_srvr);
  213. DH_free(dh_clnt);
  214. return 0;
  215. }
  216. void extract_server_random(flow *f, uint8_t *hs){
  217. uint8_t *p;
  218. p = hs + HANDSHAKE_HEADER_LEN;
  219. p+=2; //skip version
  220. memcpy(f->server_random, p, SSL3_RANDOM_SIZE);
  221. }
  222. /* PRF using sha384, as defined in RFC 5246 */
  223. int PRF(uint8_t *secret, int32_t secret_len,
  224. uint8_t *seed1, int32_t seed1_len,
  225. uint8_t *seed2, int32_t seed2_len,
  226. uint8_t *seed3, int32_t seed3_len,
  227. uint8_t *seed4, int32_t seed4_len,
  228. uint8_t *output, int32_t output_len){
  229. EVP_MD_CTX ctx, ctx_tmp, ctx_init;
  230. EVP_PKEY *mac_key;
  231. const EVP_MD *md = EVP_sha384();
  232. uint8_t A[EVP_MAX_MD_SIZE];
  233. size_t len, A_len;
  234. int chunk = EVP_MD_size(md);
  235. int remaining = output_len;
  236. uint8_t *out = output;
  237. EVP_MD_CTX_init(&ctx);
  238. EVP_MD_CTX_init(&ctx_tmp);
  239. EVP_MD_CTX_init(&ctx_init);
  240. EVP_MD_CTX_set_flags(&ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  241. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret, secret_len);
  242. /* Calculate first A value */
  243. EVP_DigestSignInit(&ctx_init, NULL, md, NULL, mac_key);
  244. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  245. if(seed1 != NULL && seed1_len > 0){
  246. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  247. }
  248. if(seed2 != NULL && seed2_len > 0){
  249. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  250. }
  251. if(seed3 != NULL && seed3_len > 0){
  252. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  253. }
  254. if(seed4 != NULL && seed4_len > 0){
  255. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  256. }
  257. EVP_DigestSignFinal(&ctx, A, &A_len);
  258. //iterate until desired length is achieved
  259. while(remaining > 0){
  260. /* Now compute SHA384(secret, A+seed) */
  261. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  262. EVP_DigestSignUpdate(&ctx, A, A_len);
  263. EVP_MD_CTX_copy_ex(&ctx_tmp, &ctx);
  264. if(seed1 != NULL && seed1_len > 0){
  265. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  266. }
  267. if(seed2 != NULL && seed2_len > 0){
  268. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  269. }
  270. if(seed3 != NULL && seed3_len > 0){
  271. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  272. }
  273. if(seed4 != NULL && seed4_len > 0){
  274. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  275. }
  276. if(remaining > chunk){
  277. EVP_DigestSignFinal(&ctx, out, &len);
  278. out += len;
  279. remaining -= len;
  280. /* Next A value */
  281. EVP_DigestSignFinal(&ctx_tmp, A, &A_len);
  282. } else {
  283. EVP_DigestSignFinal(&ctx, A, &A_len);
  284. memcpy(out, A, remaining);
  285. remaining -= remaining;
  286. }
  287. }
  288. return 1;
  289. }
  290. /* After receiving change cipher spec, calculate keys from master secret */
  291. int init_ciphers(flow *f){
  292. EVP_CIPHER_CTX *r_ctx;
  293. EVP_CIPHER_CTX *w_ctx;
  294. EVP_CIPHER_CTX *w_ctx_srvr;
  295. EVP_CIPHER_CTX *r_ctx_srvr;
  296. const EVP_CIPHER *c = EVP_aes_256_gcm();
  297. EVP_MD_CTX *read_mac_ctx;
  298. EVP_MD_CTX *write_mac_ctx;
  299. EVP_PKEY *read_mac_key;
  300. EVP_PKEY *write_mac_key;
  301. /* Generate Keys */
  302. uint8_t *write_mac, *write_key, *write_iv;
  303. uint8_t *read_mac, *read_key, *read_iv;
  304. int32_t mac_len, key_len, iv_len;
  305. key_len = EVP_CIPHER_key_length(c);
  306. iv_len = EVP_CIPHER_iv_length(c); //EVP_GCM_TLS_FIXED_IV_LEN;
  307. mac_len = EVP_MD_size(EVP_sha384());
  308. int32_t total_len = key_len + iv_len + mac_len;
  309. total_len *= 2;
  310. uint8_t *key_block = calloc(1, total_len);
  311. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE,
  312. TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
  313. f->server_random, SSL3_RANDOM_SIZE,
  314. f->client_random, SSL3_RANDOM_SIZE,
  315. NULL, 0,
  316. key_block, total_len);
  317. /*printf("keyblock:\n");
  318. for(int i=0; i< total_len; i++){
  319. printf("%02x ", key_block[i]);
  320. }
  321. printf("\n");*/
  322. iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
  323. write_key = key_block;
  324. read_key = key_block + key_len;
  325. write_iv = key_block + 2*key_len;
  326. read_iv = key_block + 2*key_len + iv_len;
  327. write_mac = key_block + 2*key_len + 2*iv_len;
  328. read_mac = key_block + 2*key_len + 2*iv_len + mac_len;
  329. /*
  330. printf("read_mac: \n");
  331. for(int i=0; i< mac_len; i++){
  332. printf("%02x ", read_mac[i]);
  333. }
  334. printf("write_mac: \n");
  335. for(int i=0; i< mac_len; i++){
  336. printf("%02x ", write_mac[i]);
  337. }*/
  338. /* Initialize Cipher Contexts */
  339. r_ctx = EVP_CIPHER_CTX_new();
  340. w_ctx = EVP_CIPHER_CTX_new();
  341. EVP_CIPHER_CTX_init(r_ctx);
  342. EVP_CIPHER_CTX_init(w_ctx);
  343. w_ctx_srvr = EVP_CIPHER_CTX_new();
  344. r_ctx_srvr = EVP_CIPHER_CTX_new();
  345. EVP_CIPHER_CTX_init(w_ctx_srvr);
  346. EVP_CIPHER_CTX_init(r_ctx_srvr);
  347. /* Initialize MACs */
  348. read_mac_ctx = EVP_MD_CTX_create();
  349. write_mac_ctx = EVP_MD_CTX_create();
  350. /*read_mac_key =EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, read_mac, mac_len);
  351. write_mac_key =EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, write_mac, mac_len);
  352. EVP_DigestSignInit(read_mac_ctx, NULL, EVP_sha384(), NULL, read_mac_key);
  353. EVP_DigestSignInit(write_mac_ctx, NULL, EVP_sha384(), NULL, write_mac_key);
  354. EVP_PKEY_free(read_mac_key);
  355. EVP_PKEY_free(write_mac_key);*/
  356. #ifdef KSSL_DEBUG
  357. {
  358. int i;
  359. fprintf(stderr, "EVP_CipherInit_ex(r_ctx,c,key=,iv=,which)\n");
  360. fprintf(stderr, "\tkey= ");
  361. for (i = 0; i < c->key_len; i++)
  362. fprintf(stderr, "%02x", read_key[i]);
  363. fprintf(stderr, "\n");
  364. fprintf(stderr, "\t iv= ");
  365. for (i = 0; i < c->iv_len; i++)
  366. fprintf(stderr, "%02x", read_iv[i]);
  367. fprintf(stderr, "\n");
  368. }
  369. #endif KSSL_DEBUG_
  370. {
  371. int i;
  372. fprintf(stderr, "EVP_CipherInit_ex(w_ctx,c,key=,iv=,which)\n");
  373. fprintf(stderr, "\tkey= ");
  374. for (i = 0; i < c->key_len; i++)
  375. fprintf(stderr, "%02x", write_key[i]);
  376. fprintf(stderr, "\n");
  377. fprintf(stderr, "\t iv= ");
  378. for (i = 0; i < c->iv_len; i++)
  379. fprintf(stderr, "%02x", write_iv[i]);
  380. fprintf(stderr, "\n");
  381. }
  382. //#endif KSSL_DEBUG */
  383. EVP_CipherInit_ex(r_ctx, c, NULL, read_key, NULL, 0);
  384. EVP_CipherInit_ex(w_ctx, c, NULL, write_key, NULL, 1);
  385. EVP_CipherInit_ex(w_ctx_srvr, c, NULL, read_key, NULL, 1);
  386. EVP_CipherInit_ex(r_ctx_srvr, c, NULL, write_key, NULL, 0);
  387. EVP_CIPHER_CTX_ctrl(r_ctx, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, read_iv);
  388. EVP_CIPHER_CTX_ctrl(w_ctx, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, write_iv);
  389. EVP_CIPHER_CTX_ctrl(w_ctx_srvr, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, read_iv);
  390. EVP_CIPHER_CTX_ctrl(r_ctx_srvr, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, write_iv);
  391. f->clnt_read_ctx = r_ctx;
  392. f->clnt_write_ctx = w_ctx;
  393. f->srvr_read_ctx = r_ctx_srvr;
  394. f->srvr_write_ctx = w_ctx_srvr;
  395. f->read_mac_ctx = read_mac_ctx;
  396. f->write_mac_ctx = write_mac_ctx;
  397. return 0;
  398. }