crypto.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. /* Decrypt the TLS FINISHED message
  48. * Verify that the data is:
  49. * PRF(master_secret, finished_label, Hash(handshake_messages))*/
  50. int encrypt(flow *f, uint8_t *input, uint8_t *output, int32_t len, int32_t incoming, int32_t type){
  51. uint8_t *p = input;
  52. EVP_CIPHER_CTX *ds = (incoming) ? f->read_ctx : f->write_ctx;
  53. if(ds == NULL){
  54. printf("FAIL\n");
  55. return 1;
  56. }
  57. uint8_t *seq = (incoming) ? f->read_seq : f->write_seq;
  58. if(f->application && (ds->iv[EVP_GCM_TLS_FIXED_IV_LEN] == 0)){
  59. //fill in rest of iv
  60. for(int i = EVP_GCM_TLS_FIXED_IV_LEN; i< ds->cipher->iv_len; i++){
  61. ds->iv[i] = p[i- EVP_GCM_TLS_FIXED_IV_LEN];
  62. }
  63. }
  64. // printf("\t\tiv: ");
  65. // for(int i=0; i<ds->cipher->iv_len; i++){
  66. // printf("%02X ", ds->iv[i]);
  67. // }
  68. // printf("\n");
  69. int32_t bs = EVP_CIPHER_block_size(ds->cipher);
  70. //padding stuff? TODO: understand this
  71. uint8_t buf[13];
  72. memcpy(buf, seq, 8);
  73. for(int i=7; i>=0; i--){
  74. ++seq[i];
  75. if(seq[i] != 0)
  76. break;
  77. }
  78. buf[8] = type;
  79. buf[9] = 0x03;
  80. buf[10] = 0x03;
  81. buf[11] = len >> 8; //len >> 8;
  82. buf[12] = len & 0xff;//len *0xff;
  83. int32_t pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
  84. 13, buf);
  85. //printf("buf: ");
  86. //for(int i=0; i<13; i++){
  87. // printf("%02x ", buf[i]);
  88. // }
  89. // printf("\n");
  90. /* printf("Decrypting: ");
  91. for(int i=0; i<len; i++){
  92. printf("%02x ", p[i]);
  93. }
  94. printf("\n");*/
  95. int32_t n = EVP_Cipher(ds, p, p, len); //decrypt in place
  96. if(n<0) return 0;
  97. /* printf("Decrypted: ");
  98. for(int i=0; i<n; i++){
  99. printf("%02x ", p[i]);
  100. }
  101. printf("\n");
  102. printf("decrypted %d/%d bytes.\n", n, len);*/
  103. p[EVP_GCM_TLS_EXPLICIT_IV_LEN+n] = '\0';
  104. return 1;
  105. }
  106. int verify_finish_hash(flow *f, uint8_t *p, int32_t incoming){
  107. EVP_MD_CTX ctx;
  108. uint8_t hash[EVP_MAX_MD_SIZE];
  109. int32_t hash_len;
  110. EVP_MD_CTX_init(&ctx);
  111. //get header length
  112. struct handshake_header *hs_hdr;
  113. hs_hdr = (struct handshake_header*) p;
  114. uint32_t fin_length = HANDSHAKE_MESSAGE_LEN(hs_hdr);
  115. p += HANDSHAKE_HEADER_LEN;
  116. //finalize hash of handshake msgs
  117. EVP_MD_CTX_copy_ex(&ctx, f->finish_md_ctx);
  118. EVP_DigestFinal_ex(&ctx, hash, &hash_len);
  119. //now use pseudorandom function
  120. uint8_t *output = calloc(1, fin_length);
  121. if(incoming){
  122. 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);
  123. } else {
  124. 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);
  125. }
  126. //now compare
  127. if(CRYPTO_memcmp(p, output, fin_length) != 0){
  128. printf("VERIFY FAILED\n");
  129. return 0;
  130. } else {
  131. printf("VERIFY PASSED\n");
  132. }
  133. return 1;
  134. }
  135. int compute_master_secret(flow *f){
  136. DH *dh_srvr = NULL;
  137. DH *dh_clnt = NULL;
  138. BN_CTX *ctx;
  139. BN_MONT_CTX *mont = NULL;
  140. BIGNUM *pub_key = NULL, *priv_key = NULL;
  141. ctx = BN_CTX_new();
  142. dh_srvr = f->dh;
  143. dh_clnt = DHparams_dup(dh_srvr);
  144. uint32_t l = dh_clnt->length ? dh_clnt->length : BN_num_bits(dh_clnt->p) - 1;
  145. int32_t bytes = (l+7) / 8;
  146. uint8_t *buf = (uint8_t *)OPENSSL_malloc(bytes);
  147. if (buf == NULL){
  148. BNerr(BN_F_BNRAND, ERR_R_MALLOC_FAILURE);
  149. return 1;
  150. }
  151. pub_key = BN_new();
  152. priv_key = BN_new();
  153. for(int i=0; i<bytes; i++){
  154. buf[i] = f->key[i%16];
  155. }
  156. if (!BN_bin2bn(buf, bytes, priv_key))
  157. return 1;
  158. {
  159. BIGNUM *prk;
  160. prk = priv_key;
  161. if (!dh_clnt->meth->bn_mod_exp(dh_clnt, pub_key, dh_clnt->g, prk, dh_clnt->p, ctx, mont)){
  162. printf("FAIL\n");
  163. return 1;
  164. }
  165. }
  166. dh_clnt->pub_key = pub_key;
  167. dh_clnt->priv_key = priv_key;
  168. // Compute master key
  169. uint8_t *pre_master_secret = calloc(1, 256);//TODO: find right length
  170. DH_compute_key(pre_master_secret, dh_srvr->pub_key, dh_clnt);
  171. 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);
  172. //remove pre_master_secret from memory
  173. memset(pre_master_secret, 0, PRE_MASTER_LEN);
  174. /*printf("master secret:\n");
  175. for(int i=0; i< 48; i++){
  176. printf("%02x ", f->master_secret[i]);
  177. }
  178. printf("\n");*/
  179. free(pre_master_secret);
  180. DH_free(dh_srvr);
  181. DH_free(dh_clnt);
  182. return 0;
  183. }
  184. void extract_server_random(flow *f, uint8_t *hs){
  185. uint8_t *p;
  186. p = hs + HANDSHAKE_HEADER_LEN;
  187. p+=2; //skip version
  188. memcpy(f->server_random, p, SSL3_RANDOM_SIZE);
  189. }
  190. /* PRF using sha384, as defined in RFC 5246 */
  191. int PRF(uint8_t *secret, int32_t secret_len,
  192. uint8_t *seed1, int32_t seed1_len,
  193. uint8_t *seed2, int32_t seed2_len,
  194. uint8_t *seed3, int32_t seed3_len,
  195. uint8_t *seed4, int32_t seed4_len,
  196. uint8_t *output, int32_t output_len){
  197. EVP_MD_CTX ctx, ctx_tmp, ctx_init;
  198. EVP_PKEY *mac_key;
  199. const EVP_MD *md = EVP_sha384();
  200. uint8_t A[EVP_MAX_MD_SIZE];
  201. size_t len, A_len;
  202. int chunk = EVP_MD_size(md);
  203. int remaining = output_len;
  204. uint8_t *out = output;
  205. EVP_MD_CTX_init(&ctx);
  206. EVP_MD_CTX_init(&ctx_tmp);
  207. EVP_MD_CTX_init(&ctx_init);
  208. EVP_MD_CTX_set_flags(&ctx_init, EVP_MD_CTX_FLAG_NON_FIPS_ALLOW);
  209. mac_key = EVP_PKEY_new_mac_key(EVP_PKEY_HMAC, NULL, secret, secret_len);
  210. /* Calculate first A value */
  211. EVP_DigestSignInit(&ctx_init, NULL, md, NULL, mac_key);
  212. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  213. if(seed1 != NULL && seed1_len > 0){
  214. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  215. }
  216. if(seed2 != NULL && seed2_len > 0){
  217. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  218. }
  219. if(seed3 != NULL && seed3_len > 0){
  220. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  221. }
  222. if(seed4 != NULL && seed4_len > 0){
  223. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  224. }
  225. EVP_DigestSignFinal(&ctx, A, &A_len);
  226. //iterate until desired length is achieved
  227. while(remaining > 0){
  228. /* Now compute SHA384(secret, A+seed) */
  229. EVP_MD_CTX_copy_ex(&ctx, &ctx_init);
  230. EVP_DigestSignUpdate(&ctx, A, A_len);
  231. EVP_MD_CTX_copy_ex(&ctx_tmp, &ctx);
  232. if(seed1 != NULL && seed1_len > 0){
  233. EVP_DigestSignUpdate(&ctx, seed1, seed1_len);
  234. }
  235. if(seed2 != NULL && seed2_len > 0){
  236. EVP_DigestSignUpdate(&ctx, seed2, seed2_len);
  237. }
  238. if(seed3 != NULL && seed3_len > 0){
  239. EVP_DigestSignUpdate(&ctx, seed3, seed3_len);
  240. }
  241. if(seed4 != NULL && seed4_len > 0){
  242. EVP_DigestSignUpdate(&ctx, seed4, seed4_len);
  243. }
  244. if(remaining > chunk){
  245. EVP_DigestSignFinal(&ctx, out, &len);
  246. out += len;
  247. remaining -= len;
  248. /* Next A value */
  249. EVP_DigestSignFinal(&ctx_tmp, A, &A_len);
  250. } else {
  251. EVP_DigestSignFinal(&ctx, A, &A_len);
  252. memcpy(out, A, remaining);
  253. remaining -= remaining;
  254. }
  255. }
  256. return 1;
  257. }
  258. /* After receiving change cipher spec, calculate keys from master secret */
  259. int init_ciphers(flow *f){
  260. EVP_CIPHER_CTX *r_ctx;
  261. EVP_CIPHER_CTX *w_ctx;
  262. const EVP_CIPHER *c = EVP_aes_256_gcm();
  263. /* Generate Keys */
  264. uint8_t *write_mac, *write_key, *write_iv;
  265. uint8_t *read_mac, *read_key, *read_iv;
  266. int32_t mac_len, key_len, iv_len;
  267. key_len = EVP_CIPHER_key_length(c);
  268. iv_len = EVP_CIPHER_iv_length(c); //EVP_GCM_TLS_FIXED_IV_LEN;
  269. mac_len = EVP_MD_size(EVP_get_digestbyname(SN_sha384));
  270. int32_t total_len = key_len + iv_len + mac_len;
  271. total_len *= 2;
  272. uint8_t *key_block = calloc(1, total_len);
  273. PRF(f->master_secret, SSL3_MASTER_SECRET_SIZE,
  274. TLS_MD_KEY_EXPANSION_CONST, TLS_MD_KEY_EXPANSION_CONST_SIZE,
  275. f->server_random, SSL3_RANDOM_SIZE,
  276. f->client_random, SSL3_RANDOM_SIZE,
  277. NULL, 0,
  278. key_block, total_len);
  279. /*printf("keyblock:\n");
  280. for(int i=0; i< total_len; i++){
  281. printf("%02x ", key_block[i]);
  282. }
  283. printf("\n");*/
  284. iv_len = EVP_GCM_TLS_FIXED_IV_LEN;
  285. write_key = key_block;
  286. read_key = key_block + key_len;
  287. write_iv = key_block + 2*key_len;
  288. read_iv = key_block + 2*key_len + iv_len;
  289. write_mac = key_block + 2*key_len + 2*iv_len;
  290. read_mac = key_block + 2*key_len + 2*iv_len + mac_len;
  291. /* Initialize Cipher Contexts */
  292. r_ctx = EVP_CIPHER_CTX_new();
  293. w_ctx = EVP_CIPHER_CTX_new();
  294. EVP_CIPHER_CTX_init(r_ctx);
  295. EVP_CIPHER_CTX_init(w_ctx);
  296. /*#ifdef KSSL_DEBUG
  297. {
  298. int i;
  299. fprintf(stderr, "EVP_CipherInit_ex(r_ctx,c,key=,iv=,which)\n");
  300. fprintf(stderr, "\tkey= ");
  301. for (i = 0; i < c->key_len; i++)
  302. fprintf(stderr, "%02x", read_key[i]);
  303. fprintf(stderr, "\n");
  304. fprintf(stderr, "\t iv= ");
  305. for (i = 0; i < c->iv_len; i++)
  306. fprintf(stderr, "%02x", read_iv[i]);
  307. fprintf(stderr, "\n");
  308. }
  309. //#endif KSSL_DEBUG_
  310. {
  311. int i;
  312. fprintf(stderr, "EVP_CipherInit_ex(w_ctx,c,key=,iv=,which)\n");
  313. fprintf(stderr, "\tkey= ");
  314. for (i = 0; i < c->key_len; i++)
  315. fprintf(stderr, "%02x", write_key[i]);
  316. fprintf(stderr, "\n");
  317. fprintf(stderr, "\t iv= ");
  318. for (i = 0; i < c->iv_len; i++)
  319. fprintf(stderr, "%02x", write_iv[i]);
  320. fprintf(stderr, "\n");
  321. }
  322. //#endif KSSL_DEBUG */
  323. EVP_CipherInit_ex(r_ctx, c, NULL, read_key, NULL, 0);
  324. EVP_CipherInit_ex(w_ctx, c, NULL, write_key, NULL, 0);
  325. EVP_CIPHER_CTX_ctrl(r_ctx, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, read_iv);
  326. EVP_CIPHER_CTX_ctrl(w_ctx, EVP_CTRL_GCM_SET_IV_FIXED, EVP_GCM_TLS_FIXED_IV_LEN, write_iv);
  327. f->read_ctx = r_ctx;
  328. f->write_ctx = w_ctx;
  329. return 0;
  330. }