tls1_meth.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. /*
  2. * Copyright 2022-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <openssl/evp.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/rand.h>
  12. #include <openssl/ssl.h>
  13. #include "internal/ssl3_cbc.h"
  14. #include "../../ssl_local.h"
  15. #include "../record_local.h"
  16. #include "recmethod_local.h"
  17. static int tls1_set_crypto_state(OSSL_RECORD_LAYER *rl, int level,
  18. unsigned char *key, size_t keylen,
  19. unsigned char *iv, size_t ivlen,
  20. unsigned char *mackey, size_t mackeylen,
  21. const EVP_CIPHER *ciph,
  22. size_t taglen,
  23. int mactype,
  24. const EVP_MD *md,
  25. COMP_METHOD *comp)
  26. {
  27. EVP_CIPHER_CTX *ciph_ctx;
  28. EVP_PKEY *mac_key;
  29. int enc = (rl->direction == OSSL_RECORD_DIRECTION_WRITE) ? 1 : 0;
  30. if (level != OSSL_RECORD_PROTECTION_LEVEL_APPLICATION)
  31. return OSSL_RECORD_RETURN_FATAL;
  32. if ((rl->enc_ctx = EVP_CIPHER_CTX_new()) == NULL) {
  33. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_EVP_LIB);
  34. return OSSL_RECORD_RETURN_FATAL;
  35. }
  36. ciph_ctx = rl->enc_ctx;
  37. rl->md_ctx = EVP_MD_CTX_new();
  38. if (rl->md_ctx == NULL) {
  39. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  40. return OSSL_RECORD_RETURN_FATAL;
  41. }
  42. #ifndef OPENSSL_NO_COMP
  43. if (comp != NULL) {
  44. rl->compctx = COMP_CTX_new(comp);
  45. if (rl->compctx == NULL) {
  46. ERR_raise(ERR_LIB_SSL, SSL_R_COMPRESSION_LIBRARY_ERROR);
  47. return OSSL_RECORD_RETURN_FATAL;
  48. }
  49. }
  50. #endif
  51. /*
  52. * If we have an AEAD Cipher, then there is no separate MAC, so we can skip
  53. * setting up the MAC key.
  54. */
  55. if ((EVP_CIPHER_get_flags(ciph) & EVP_CIPH_FLAG_AEAD_CIPHER) == 0) {
  56. if (mactype == EVP_PKEY_HMAC) {
  57. mac_key = EVP_PKEY_new_raw_private_key_ex(rl->libctx, "HMAC",
  58. rl->propq, mackey,
  59. mackeylen);
  60. } else {
  61. /*
  62. * If its not HMAC then the only other types of MAC we support are
  63. * the GOST MACs, so we need to use the old style way of creating
  64. * a MAC key.
  65. */
  66. mac_key = EVP_PKEY_new_mac_key(mactype, NULL, mackey,
  67. (int)mackeylen);
  68. }
  69. if (mac_key == NULL
  70. || EVP_DigestSignInit_ex(rl->md_ctx, NULL, EVP_MD_get0_name(md),
  71. rl->libctx, rl->propq, mac_key,
  72. NULL) <= 0) {
  73. EVP_PKEY_free(mac_key);
  74. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  75. return OSSL_RECORD_RETURN_FATAL;
  76. }
  77. EVP_PKEY_free(mac_key);
  78. }
  79. if (EVP_CIPHER_get_mode(ciph) == EVP_CIPH_GCM_MODE) {
  80. if (!EVP_CipherInit_ex(ciph_ctx, ciph, NULL, key, NULL, enc)
  81. || EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_GCM_SET_IV_FIXED,
  82. (int)ivlen, iv) <= 0) {
  83. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  84. return OSSL_RECORD_RETURN_FATAL;
  85. }
  86. } else if (EVP_CIPHER_get_mode(ciph) == EVP_CIPH_CCM_MODE) {
  87. if (!EVP_CipherInit_ex(ciph_ctx, ciph, NULL, NULL, NULL, enc)
  88. || EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_IVLEN, 12,
  89. NULL) <= 0
  90. || EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_TAG,
  91. (int)taglen, NULL) <= 0
  92. || EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_CCM_SET_IV_FIXED,
  93. (int)ivlen, iv) <= 0
  94. || !EVP_CipherInit_ex(ciph_ctx, NULL, NULL, key, NULL, enc)) {
  95. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  96. return OSSL_RECORD_RETURN_FATAL;
  97. }
  98. } else {
  99. if (!EVP_CipherInit_ex(ciph_ctx, ciph, NULL, key, iv, enc)) {
  100. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  101. return OSSL_RECORD_RETURN_FATAL;
  102. }
  103. }
  104. /* Needed for "composite" AEADs, such as RC4-HMAC-MD5 */
  105. if ((EVP_CIPHER_get_flags(ciph) & EVP_CIPH_FLAG_AEAD_CIPHER) != 0
  106. && mackeylen != 0
  107. && EVP_CIPHER_CTX_ctrl(ciph_ctx, EVP_CTRL_AEAD_SET_MAC_KEY,
  108. (int)mackeylen, mackey) <= 0) {
  109. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  110. return OSSL_RECORD_RETURN_FATAL;
  111. }
  112. /*
  113. * The cipher we actually ended up using in the EVP_CIPHER_CTX may be
  114. * different to that in ciph if we have an ENGINE in use
  115. */
  116. if (EVP_CIPHER_get0_provider(EVP_CIPHER_CTX_get0_cipher(ciph_ctx)) != NULL
  117. && !ossl_set_tls_provider_parameters(rl, ciph_ctx, ciph, md)) {
  118. /* ERR_raise already called */
  119. return OSSL_RECORD_RETURN_FATAL;
  120. }
  121. /* Calculate the explicit IV length */
  122. if (RLAYER_USE_EXPLICIT_IV(rl)) {
  123. int mode = EVP_CIPHER_CTX_get_mode(ciph_ctx);
  124. int eivlen = 0;
  125. if (mode == EVP_CIPH_CBC_MODE) {
  126. eivlen = EVP_CIPHER_CTX_get_iv_length(ciph_ctx);
  127. if (eivlen < 0) {
  128. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_LIBRARY_BUG);
  129. return OSSL_RECORD_RETURN_FATAL;
  130. }
  131. if (eivlen <= 1)
  132. eivlen = 0;
  133. } else if (mode == EVP_CIPH_GCM_MODE) {
  134. /* Need explicit part of IV for GCM mode */
  135. eivlen = EVP_GCM_TLS_EXPLICIT_IV_LEN;
  136. } else if (mode == EVP_CIPH_CCM_MODE) {
  137. eivlen = EVP_CCM_TLS_EXPLICIT_IV_LEN;
  138. }
  139. rl->eivlen = (size_t)eivlen;
  140. }
  141. return OSSL_RECORD_RETURN_SUCCESS;
  142. }
  143. #define MAX_PADDING 256
  144. /*-
  145. * tls1_cipher encrypts/decrypts |n_recs| in |recs|. Calls RLAYERfatal on
  146. * internal error, but not otherwise. It is the responsibility of the caller to
  147. * report a bad_record_mac - if appropriate (DTLS just drops the record).
  148. *
  149. * Returns:
  150. * 0: if the record is publicly invalid, or an internal error, or AEAD
  151. * decryption failed, or Encrypt-then-mac decryption failed.
  152. * 1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
  153. */
  154. static int tls1_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *recs,
  155. size_t n_recs, int sending, SSL_MAC_BUF *macs,
  156. size_t macsize)
  157. {
  158. EVP_CIPHER_CTX *ds;
  159. size_t reclen[SSL_MAX_PIPELINES];
  160. unsigned char buf[SSL_MAX_PIPELINES][EVP_AEAD_TLS1_AAD_LEN];
  161. unsigned char *data[SSL_MAX_PIPELINES];
  162. int pad = 0, tmpr, provided;
  163. size_t bs, ctr, padnum, loop;
  164. unsigned char padval;
  165. const EVP_CIPHER *enc;
  166. if (n_recs == 0) {
  167. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  168. return 0;
  169. }
  170. if (EVP_MD_CTX_get0_md(rl->md_ctx)) {
  171. int n = EVP_MD_CTX_get_size(rl->md_ctx);
  172. if (!ossl_assert(n >= 0)) {
  173. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  174. return 0;
  175. }
  176. }
  177. ds = rl->enc_ctx;
  178. if (!ossl_assert(rl->enc_ctx != NULL)) {
  179. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  180. return 0;
  181. }
  182. enc = EVP_CIPHER_CTX_get0_cipher(rl->enc_ctx);
  183. if (sending) {
  184. int ivlen;
  185. /* For TLSv1.1 and later explicit IV */
  186. if (RLAYER_USE_EXPLICIT_IV(rl)
  187. && EVP_CIPHER_get_mode(enc) == EVP_CIPH_CBC_MODE)
  188. ivlen = EVP_CIPHER_get_iv_length(enc);
  189. else
  190. ivlen = 0;
  191. if (ivlen > 1) {
  192. for (ctr = 0; ctr < n_recs; ctr++) {
  193. if (recs[ctr].data != recs[ctr].input) {
  194. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  195. return 0;
  196. } else if (RAND_bytes_ex(rl->libctx, recs[ctr].input,
  197. ivlen, 0) <= 0) {
  198. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  199. return 0;
  200. }
  201. }
  202. }
  203. }
  204. if (!ossl_assert(enc != NULL)) {
  205. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  206. return 0;
  207. }
  208. provided = (EVP_CIPHER_get0_provider(enc) != NULL);
  209. bs = EVP_CIPHER_get_block_size(EVP_CIPHER_CTX_get0_cipher(ds));
  210. if (n_recs > 1) {
  211. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
  212. & EVP_CIPH_FLAG_PIPELINE) == 0) {
  213. /*
  214. * We shouldn't have been called with pipeline data if the
  215. * cipher doesn't support pipelining
  216. */
  217. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
  218. return 0;
  219. }
  220. }
  221. for (ctr = 0; ctr < n_recs; ctr++) {
  222. reclen[ctr] = recs[ctr].length;
  223. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
  224. & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) {
  225. unsigned char *seq;
  226. seq = rl->sequence;
  227. if (rl->isdtls) {
  228. unsigned char dtlsseq[8], *p = dtlsseq;
  229. s2n(rl->epoch, p);
  230. memcpy(p, &seq[2], 6);
  231. memcpy(buf[ctr], dtlsseq, 8);
  232. } else {
  233. memcpy(buf[ctr], seq, 8);
  234. if (!tls_increment_sequence_ctr(rl)) {
  235. /* RLAYERfatal already called */
  236. return 0;
  237. }
  238. }
  239. buf[ctr][8] = recs[ctr].type;
  240. buf[ctr][9] = (unsigned char)(rl->version >> 8);
  241. buf[ctr][10] = (unsigned char)(rl->version);
  242. buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
  243. buf[ctr][12] = (unsigned char)(recs[ctr].length & 0xff);
  244. pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
  245. EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
  246. if (pad <= 0) {
  247. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  248. return 0;
  249. }
  250. if (sending) {
  251. reclen[ctr] += pad;
  252. recs[ctr].length += pad;
  253. }
  254. } else if ((bs != 1) && sending && !provided) {
  255. /*
  256. * We only do this for legacy ciphers. Provided ciphers add the
  257. * padding on the provider side.
  258. */
  259. padnum = bs - (reclen[ctr] % bs);
  260. /* Add weird padding of up to 256 bytes */
  261. if (padnum > MAX_PADDING) {
  262. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  263. return 0;
  264. }
  265. /* we need to add 'padnum' padding bytes of value padval */
  266. padval = (unsigned char)(padnum - 1);
  267. for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
  268. recs[ctr].input[loop] = padval;
  269. reclen[ctr] += padnum;
  270. recs[ctr].length += padnum;
  271. }
  272. if (!sending) {
  273. if (reclen[ctr] == 0 || reclen[ctr] % bs != 0) {
  274. /* Publicly invalid */
  275. return 0;
  276. }
  277. }
  278. }
  279. if (n_recs > 1) {
  280. /* Set the output buffers */
  281. for (ctr = 0; ctr < n_recs; ctr++)
  282. data[ctr] = recs[ctr].data;
  283. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
  284. (int)n_recs, data) <= 0) {
  285. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
  286. return 0;
  287. }
  288. /* Set the input buffers */
  289. for (ctr = 0; ctr < n_recs; ctr++)
  290. data[ctr] = recs[ctr].input;
  291. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
  292. (int)n_recs, data) <= 0
  293. || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
  294. (int)n_recs, reclen) <= 0) {
  295. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
  296. return 0;
  297. }
  298. }
  299. if (!rl->isdtls && rl->tlstree) {
  300. int decrement_seq = 0;
  301. /*
  302. * When sending, seq is incremented after MAC calculation.
  303. * So if we are in ETM mode, we use seq 'as is' in the ctrl-function.
  304. * Otherwise we have to decrease it in the implementation
  305. */
  306. if (sending && !rl->use_etm)
  307. decrement_seq = 1;
  308. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_TLSTREE, decrement_seq,
  309. rl->sequence) <= 0) {
  310. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  311. return 0;
  312. }
  313. }
  314. if (provided) {
  315. int outlen;
  316. /* Provided cipher - we do not support pipelining on this path */
  317. if (n_recs > 1) {
  318. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  319. return 0;
  320. }
  321. if (!EVP_CipherUpdate(ds, recs[0].data, &outlen, recs[0].input,
  322. (unsigned int)reclen[0]))
  323. return 0;
  324. recs[0].length = outlen;
  325. /*
  326. * The length returned from EVP_CipherUpdate above is the actual
  327. * payload length. We need to adjust the data/input ptr to skip over
  328. * any explicit IV
  329. */
  330. if (!sending) {
  331. if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_GCM_MODE) {
  332. recs[0].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  333. recs[0].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  334. } else if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_CCM_MODE) {
  335. recs[0].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  336. recs[0].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  337. } else if (bs != 1 && RLAYER_USE_EXPLICIT_IV(rl)) {
  338. recs[0].data += bs;
  339. recs[0].input += bs;
  340. recs[0].orig_len -= bs;
  341. }
  342. /* Now get a pointer to the MAC (if applicable) */
  343. if (macs != NULL) {
  344. OSSL_PARAM params[2], *p = params;
  345. /* Get the MAC */
  346. macs[0].alloced = 0;
  347. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
  348. (void **)&macs[0].mac,
  349. macsize);
  350. *p = OSSL_PARAM_construct_end();
  351. if (!EVP_CIPHER_CTX_get_params(ds, params)) {
  352. /* Shouldn't normally happen */
  353. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR,
  354. ERR_R_INTERNAL_ERROR);
  355. return 0;
  356. }
  357. }
  358. }
  359. } else {
  360. /* Legacy cipher */
  361. tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
  362. (unsigned int)reclen[0]);
  363. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
  364. & EVP_CIPH_FLAG_CUSTOM_CIPHER) != 0
  365. ? (tmpr < 0)
  366. : (tmpr == 0)) {
  367. /* AEAD can fail to verify MAC */
  368. return 0;
  369. }
  370. if (!sending) {
  371. for (ctr = 0; ctr < n_recs; ctr++) {
  372. /* Adjust the record to remove the explicit IV/MAC/Tag */
  373. if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_GCM_MODE) {
  374. recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  375. recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  376. recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  377. } else if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_CCM_MODE) {
  378. recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  379. recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  380. recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
  381. } else if (bs != 1 && RLAYER_USE_EXPLICIT_IV(rl)) {
  382. if (recs[ctr].length < bs)
  383. return 0;
  384. recs[ctr].data += bs;
  385. recs[ctr].input += bs;
  386. recs[ctr].length -= bs;
  387. recs[ctr].orig_len -= bs;
  388. }
  389. /*
  390. * If using Mac-then-encrypt, then this will succeed but
  391. * with a random MAC if padding is invalid
  392. */
  393. if (!tls1_cbc_remove_padding_and_mac(&recs[ctr].length,
  394. recs[ctr].orig_len,
  395. recs[ctr].data,
  396. (macs != NULL) ? &macs[ctr].mac : NULL,
  397. (macs != NULL) ? &macs[ctr].alloced
  398. : NULL,
  399. bs,
  400. pad ? (size_t)pad : macsize,
  401. (EVP_CIPHER_get_flags(enc)
  402. & EVP_CIPH_FLAG_AEAD_CIPHER) != 0,
  403. rl->libctx))
  404. return 0;
  405. }
  406. }
  407. }
  408. return 1;
  409. }
  410. static int tls1_mac(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec, unsigned char *md,
  411. int sending)
  412. {
  413. unsigned char *seq = rl->sequence;
  414. EVP_MD_CTX *hash;
  415. size_t md_size;
  416. EVP_MD_CTX *hmac = NULL, *mac_ctx;
  417. unsigned char header[13];
  418. int t;
  419. int ret = 0;
  420. hash = rl->md_ctx;
  421. t = EVP_MD_CTX_get_size(hash);
  422. if (!ossl_assert(t >= 0))
  423. return 0;
  424. md_size = t;
  425. if (rl->stream_mac) {
  426. mac_ctx = hash;
  427. } else {
  428. hmac = EVP_MD_CTX_new();
  429. if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash)) {
  430. goto end;
  431. }
  432. mac_ctx = hmac;
  433. }
  434. if (!rl->isdtls
  435. && rl->tlstree
  436. && EVP_MD_CTX_ctrl(mac_ctx, EVP_MD_CTRL_TLSTREE, 0, seq) <= 0)
  437. goto end;
  438. if (rl->isdtls) {
  439. unsigned char dtlsseq[8], *p = dtlsseq;
  440. s2n(rl->epoch, p);
  441. memcpy(p, &seq[2], 6);
  442. memcpy(header, dtlsseq, 8);
  443. } else {
  444. memcpy(header, seq, 8);
  445. }
  446. header[8] = rec->type;
  447. header[9] = (unsigned char)(rl->version >> 8);
  448. header[10] = (unsigned char)(rl->version);
  449. header[11] = (unsigned char)(rec->length >> 8);
  450. header[12] = (unsigned char)(rec->length & 0xff);
  451. if (!sending && !rl->use_etm
  452. && EVP_CIPHER_CTX_get_mode(rl->enc_ctx) == EVP_CIPH_CBC_MODE
  453. && ssl3_cbc_record_digest_supported(mac_ctx)) {
  454. OSSL_PARAM tls_hmac_params[2], *p = tls_hmac_params;
  455. *p++ = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE,
  456. &rec->orig_len);
  457. *p++ = OSSL_PARAM_construct_end();
  458. if (!EVP_PKEY_CTX_set_params(EVP_MD_CTX_get_pkey_ctx(mac_ctx),
  459. tls_hmac_params))
  460. goto end;
  461. }
  462. if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
  463. || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
  464. || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0)
  465. goto end;
  466. OSSL_TRACE_BEGIN(TLS) {
  467. BIO_printf(trc_out, "seq:\n");
  468. BIO_dump_indent(trc_out, seq, 8, 4);
  469. BIO_printf(trc_out, "rec:\n");
  470. BIO_dump_indent(trc_out, rec->data, rec->length, 4);
  471. } OSSL_TRACE_END(TLS);
  472. if (!rl->isdtls && !tls_increment_sequence_ctr(rl)) {
  473. /* RLAYERfatal already called */
  474. goto end;
  475. }
  476. OSSL_TRACE_BEGIN(TLS) {
  477. BIO_printf(trc_out, "md:\n");
  478. BIO_dump_indent(trc_out, md, md_size, 4);
  479. } OSSL_TRACE_END(TLS);
  480. ret = 1;
  481. end:
  482. EVP_MD_CTX_free(hmac);
  483. return ret;
  484. }
  485. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
  486. # ifndef OPENSSL_NO_COMP
  487. # define MAX_PREFIX_LEN ((SSL3_ALIGN_PAYLOAD - 1) \
  488. + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD \
  489. + SSL3_RT_HEADER_LENGTH \
  490. + SSL3_RT_MAX_COMPRESSED_OVERHEAD)
  491. # else
  492. # define MAX_PREFIX_LEN ((SSL3_ALIGN_PAYLOAD - 1) \
  493. + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD \
  494. + SSL3_RT_HEADER_LENGTH)
  495. # endif /* OPENSSL_NO_COMP */
  496. #else
  497. # ifndef OPENSSL_NO_COMP
  498. # define MAX_PREFIX_LEN (SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD \
  499. + SSL3_RT_HEADER_LENGTH \
  500. + SSL3_RT_MAX_COMPRESSED_OVERHEAD)
  501. # else
  502. # define MAX_PREFIX_LEN (SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD \
  503. + SSL3_RT_HEADER_LENGTH)
  504. # endif /* OPENSSL_NO_COMP */
  505. #endif
  506. /* This function is also used by the SSLv3 implementation */
  507. int tls1_allocate_write_buffers(OSSL_RECORD_LAYER *rl,
  508. OSSL_RECORD_TEMPLATE *templates,
  509. size_t numtempl, size_t *prefix)
  510. {
  511. /* Do we need to add an empty record prefix? */
  512. *prefix = rl->need_empty_fragments
  513. && templates[0].type == SSL3_RT_APPLICATION_DATA;
  514. /*
  515. * In the prefix case we can allocate a much smaller buffer. Otherwise we
  516. * just allocate the default buffer size
  517. */
  518. if (!tls_setup_write_buffer(rl, numtempl + *prefix,
  519. *prefix ? MAX_PREFIX_LEN : 0, 0)) {
  520. /* RLAYERfatal() already called */
  521. return 0;
  522. }
  523. return 1;
  524. }
  525. /* This function is also used by the SSLv3 implementation */
  526. int tls1_initialise_write_packets(OSSL_RECORD_LAYER *rl,
  527. OSSL_RECORD_TEMPLATE *templates,
  528. size_t numtempl,
  529. OSSL_RECORD_TEMPLATE *prefixtempl,
  530. WPACKET *pkt,
  531. TLS_BUFFER *bufs,
  532. size_t *wpinited)
  533. {
  534. size_t align = 0;
  535. TLS_BUFFER *wb;
  536. size_t prefix;
  537. /* Do we need to add an empty record prefix? */
  538. prefix = rl->need_empty_fragments
  539. && templates[0].type == SSL3_RT_APPLICATION_DATA;
  540. if (prefix) {
  541. /*
  542. * countermeasure against known-IV weakness in CBC ciphersuites (see
  543. * http://www.openssl.org/~bodo/tls-cbc.txt)
  544. */
  545. prefixtempl->buf = NULL;
  546. prefixtempl->version = templates[0].version;
  547. prefixtempl->buflen = 0;
  548. prefixtempl->type = SSL3_RT_APPLICATION_DATA;
  549. wb = &bufs[0];
  550. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
  551. align = (size_t)TLS_BUFFER_get_buf(wb) + SSL3_RT_HEADER_LENGTH;
  552. align = SSL3_ALIGN_PAYLOAD - 1
  553. - ((align - 1) % SSL3_ALIGN_PAYLOAD);
  554. #endif
  555. TLS_BUFFER_set_offset(wb, align);
  556. if (!WPACKET_init_static_len(&pkt[0], TLS_BUFFER_get_buf(wb),
  557. TLS_BUFFER_get_len(wb), 0)) {
  558. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  559. return 0;
  560. }
  561. *wpinited = 1;
  562. if (!WPACKET_allocate_bytes(&pkt[0], align, NULL)) {
  563. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  564. return 0;
  565. }
  566. }
  567. return tls_initialise_write_packets_default(rl, templates, numtempl,
  568. NULL,
  569. pkt + prefix, bufs + prefix,
  570. wpinited);
  571. }
  572. /* TLSv1.0, TLSv1.1 and TLSv1.2 all use the same funcs */
  573. struct record_functions_st tls_1_funcs = {
  574. tls1_set_crypto_state,
  575. tls1_cipher,
  576. tls1_mac,
  577. tls_default_set_protocol_version,
  578. tls_default_read_n,
  579. tls_get_more_records,
  580. tls_default_validate_record_header,
  581. tls_default_post_process_record,
  582. tls_get_max_records_multiblock,
  583. tls_write_records_multiblock, /* Defined in tls_multib.c */
  584. tls1_allocate_write_buffers,
  585. tls1_initialise_write_packets,
  586. NULL,
  587. tls_prepare_record_header_default,
  588. NULL,
  589. tls_prepare_for_encryption_default,
  590. tls_post_encryption_processing_default,
  591. NULL
  592. };
  593. struct record_functions_st dtls_1_funcs = {
  594. tls1_set_crypto_state,
  595. tls1_cipher,
  596. tls1_mac,
  597. tls_default_set_protocol_version,
  598. tls_default_read_n,
  599. dtls_get_more_records,
  600. NULL,
  601. NULL,
  602. NULL,
  603. tls_write_records_default,
  604. /*
  605. * Don't use tls1_allocate_write_buffers since that handles empty fragment
  606. * records which aren't needed in DTLS. We just use the default allocation
  607. * instead.
  608. */
  609. tls_allocate_write_buffers_default,
  610. /* Don't use tls1_initialise_write_packets for same reason as above */
  611. tls_initialise_write_packets_default,
  612. NULL,
  613. dtls_prepare_record_header,
  614. NULL,
  615. tls_prepare_for_encryption_default,
  616. dtls_post_encryption_processing,
  617. NULL
  618. };