tls1_meth.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  1. /*
  2. * Copyright 2022-2024 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 (bs == 0) {
  211. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_BAD_CIPHER);
  212. return 0;
  213. }
  214. if (n_recs > 1) {
  215. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
  216. & EVP_CIPH_FLAG_PIPELINE) == 0) {
  217. /*
  218. * We shouldn't have been called with pipeline data if the
  219. * cipher doesn't support pipelining
  220. */
  221. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
  222. return 0;
  223. }
  224. }
  225. for (ctr = 0; ctr < n_recs; ctr++) {
  226. reclen[ctr] = recs[ctr].length;
  227. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
  228. & EVP_CIPH_FLAG_AEAD_CIPHER) != 0) {
  229. unsigned char *seq;
  230. seq = rl->sequence;
  231. if (rl->isdtls) {
  232. unsigned char dtlsseq[8], *p = dtlsseq;
  233. s2n(rl->epoch, p);
  234. memcpy(p, &seq[2], 6);
  235. memcpy(buf[ctr], dtlsseq, 8);
  236. } else {
  237. memcpy(buf[ctr], seq, 8);
  238. if (!tls_increment_sequence_ctr(rl)) {
  239. /* RLAYERfatal already called */
  240. return 0;
  241. }
  242. }
  243. buf[ctr][8] = recs[ctr].type;
  244. buf[ctr][9] = (unsigned char)(rl->version >> 8);
  245. buf[ctr][10] = (unsigned char)(rl->version);
  246. buf[ctr][11] = (unsigned char)(recs[ctr].length >> 8);
  247. buf[ctr][12] = (unsigned char)(recs[ctr].length & 0xff);
  248. pad = EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_AEAD_TLS1_AAD,
  249. EVP_AEAD_TLS1_AAD_LEN, buf[ctr]);
  250. if (pad <= 0) {
  251. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  252. return 0;
  253. }
  254. if (sending) {
  255. reclen[ctr] += pad;
  256. recs[ctr].length += pad;
  257. }
  258. } else if ((bs != 1) && sending && !provided) {
  259. /*
  260. * We only do this for legacy ciphers. Provided ciphers add the
  261. * padding on the provider side.
  262. */
  263. padnum = bs - (reclen[ctr] % bs);
  264. /* Add weird padding of up to 256 bytes */
  265. if (padnum > MAX_PADDING) {
  266. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  267. return 0;
  268. }
  269. /* we need to add 'padnum' padding bytes of value padval */
  270. padval = (unsigned char)(padnum - 1);
  271. for (loop = reclen[ctr]; loop < reclen[ctr] + padnum; loop++)
  272. recs[ctr].input[loop] = padval;
  273. reclen[ctr] += padnum;
  274. recs[ctr].length += padnum;
  275. }
  276. if (!sending) {
  277. if (reclen[ctr] == 0 || reclen[ctr] % bs != 0) {
  278. /* Publicly invalid */
  279. return 0;
  280. }
  281. }
  282. }
  283. if (n_recs > 1) {
  284. /* Set the output buffers */
  285. for (ctr = 0; ctr < n_recs; ctr++)
  286. data[ctr] = recs[ctr].data;
  287. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_OUTPUT_BUFS,
  288. (int)n_recs, data) <= 0) {
  289. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
  290. return 0;
  291. }
  292. /* Set the input buffers */
  293. for (ctr = 0; ctr < n_recs; ctr++)
  294. data[ctr] = recs[ctr].input;
  295. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_BUFS,
  296. (int)n_recs, data) <= 0
  297. || EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_SET_PIPELINE_INPUT_LENS,
  298. (int)n_recs, reclen) <= 0) {
  299. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, SSL_R_PIPELINE_FAILURE);
  300. return 0;
  301. }
  302. }
  303. if (!rl->isdtls && rl->tlstree) {
  304. int decrement_seq = 0;
  305. /*
  306. * When sending, seq is incremented after MAC calculation.
  307. * So if we are in ETM mode, we use seq 'as is' in the ctrl-function.
  308. * Otherwise we have to decrease it in the implementation
  309. */
  310. if (sending && !rl->use_etm)
  311. decrement_seq = 1;
  312. if (EVP_CIPHER_CTX_ctrl(ds, EVP_CTRL_TLSTREE, decrement_seq,
  313. rl->sequence) <= 0) {
  314. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  315. return 0;
  316. }
  317. }
  318. if (provided) {
  319. int outlen;
  320. /* Provided cipher - we do not support pipelining on this path */
  321. if (n_recs > 1) {
  322. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  323. return 0;
  324. }
  325. if (!EVP_CipherUpdate(ds, recs[0].data, &outlen, recs[0].input,
  326. (unsigned int)reclen[0]))
  327. return 0;
  328. recs[0].length = outlen;
  329. /*
  330. * The length returned from EVP_CipherUpdate above is the actual
  331. * payload length. We need to adjust the data/input ptr to skip over
  332. * any explicit IV
  333. */
  334. if (!sending) {
  335. if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_GCM_MODE) {
  336. recs[0].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  337. recs[0].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  338. } else if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_CCM_MODE) {
  339. recs[0].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  340. recs[0].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  341. } else if (bs != 1 && RLAYER_USE_EXPLICIT_IV(rl)) {
  342. recs[0].data += bs;
  343. recs[0].input += bs;
  344. recs[0].orig_len -= bs;
  345. }
  346. /* Now get a pointer to the MAC (if applicable) */
  347. if (macs != NULL) {
  348. OSSL_PARAM params[2], *p = params;
  349. /* Get the MAC */
  350. macs[0].alloced = 0;
  351. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
  352. (void **)&macs[0].mac,
  353. macsize);
  354. *p = OSSL_PARAM_construct_end();
  355. if (!EVP_CIPHER_CTX_get_params(ds, params)) {
  356. /* Shouldn't normally happen */
  357. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR,
  358. ERR_R_INTERNAL_ERROR);
  359. return 0;
  360. }
  361. }
  362. }
  363. } else {
  364. /* Legacy cipher */
  365. tmpr = EVP_Cipher(ds, recs[0].data, recs[0].input,
  366. (unsigned int)reclen[0]);
  367. if ((EVP_CIPHER_get_flags(EVP_CIPHER_CTX_get0_cipher(ds))
  368. & EVP_CIPH_FLAG_CUSTOM_CIPHER) != 0
  369. ? (tmpr < 0)
  370. : (tmpr == 0)) {
  371. /* AEAD can fail to verify MAC */
  372. return 0;
  373. }
  374. if (!sending) {
  375. for (ctr = 0; ctr < n_recs; ctr++) {
  376. /* Adjust the record to remove the explicit IV/MAC/Tag */
  377. if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_GCM_MODE) {
  378. recs[ctr].data += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  379. recs[ctr].input += EVP_GCM_TLS_EXPLICIT_IV_LEN;
  380. recs[ctr].length -= EVP_GCM_TLS_EXPLICIT_IV_LEN;
  381. } else if (EVP_CIPHER_get_mode(enc) == EVP_CIPH_CCM_MODE) {
  382. recs[ctr].data += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  383. recs[ctr].input += EVP_CCM_TLS_EXPLICIT_IV_LEN;
  384. recs[ctr].length -= EVP_CCM_TLS_EXPLICIT_IV_LEN;
  385. } else if (bs != 1 && RLAYER_USE_EXPLICIT_IV(rl)) {
  386. if (recs[ctr].length < bs)
  387. return 0;
  388. recs[ctr].data += bs;
  389. recs[ctr].input += bs;
  390. recs[ctr].length -= bs;
  391. recs[ctr].orig_len -= bs;
  392. }
  393. /*
  394. * If using Mac-then-encrypt, then this will succeed but
  395. * with a random MAC if padding is invalid
  396. */
  397. if (!tls1_cbc_remove_padding_and_mac(&recs[ctr].length,
  398. recs[ctr].orig_len,
  399. recs[ctr].data,
  400. (macs != NULL) ? &macs[ctr].mac : NULL,
  401. (macs != NULL) ? &macs[ctr].alloced
  402. : NULL,
  403. bs,
  404. pad ? (size_t)pad : macsize,
  405. (EVP_CIPHER_get_flags(enc)
  406. & EVP_CIPH_FLAG_AEAD_CIPHER) != 0,
  407. rl->libctx))
  408. return 0;
  409. }
  410. }
  411. }
  412. return 1;
  413. }
  414. static int tls1_mac(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec, unsigned char *md,
  415. int sending)
  416. {
  417. unsigned char *seq = rl->sequence;
  418. EVP_MD_CTX *hash;
  419. size_t md_size;
  420. EVP_MD_CTX *hmac = NULL, *mac_ctx;
  421. unsigned char header[13];
  422. int t;
  423. int ret = 0;
  424. hash = rl->md_ctx;
  425. t = EVP_MD_CTX_get_size(hash);
  426. if (!ossl_assert(t >= 0))
  427. return 0;
  428. md_size = t;
  429. if (rl->stream_mac) {
  430. mac_ctx = hash;
  431. } else {
  432. hmac = EVP_MD_CTX_new();
  433. if (hmac == NULL || !EVP_MD_CTX_copy(hmac, hash)) {
  434. goto end;
  435. }
  436. mac_ctx = hmac;
  437. }
  438. if (!rl->isdtls
  439. && rl->tlstree
  440. && EVP_MD_CTX_ctrl(mac_ctx, EVP_MD_CTRL_TLSTREE, 0, seq) <= 0)
  441. goto end;
  442. if (rl->isdtls) {
  443. unsigned char dtlsseq[8], *p = dtlsseq;
  444. s2n(rl->epoch, p);
  445. memcpy(p, &seq[2], 6);
  446. memcpy(header, dtlsseq, 8);
  447. } else {
  448. memcpy(header, seq, 8);
  449. }
  450. header[8] = rec->type;
  451. header[9] = (unsigned char)(rl->version >> 8);
  452. header[10] = (unsigned char)(rl->version);
  453. header[11] = (unsigned char)(rec->length >> 8);
  454. header[12] = (unsigned char)(rec->length & 0xff);
  455. if (!sending && !rl->use_etm
  456. && EVP_CIPHER_CTX_get_mode(rl->enc_ctx) == EVP_CIPH_CBC_MODE
  457. && ssl3_cbc_record_digest_supported(mac_ctx)) {
  458. OSSL_PARAM tls_hmac_params[2], *p = tls_hmac_params;
  459. *p++ = OSSL_PARAM_construct_size_t(OSSL_MAC_PARAM_TLS_DATA_SIZE,
  460. &rec->orig_len);
  461. *p++ = OSSL_PARAM_construct_end();
  462. if (!EVP_PKEY_CTX_set_params(EVP_MD_CTX_get_pkey_ctx(mac_ctx),
  463. tls_hmac_params))
  464. goto end;
  465. }
  466. if (EVP_DigestSignUpdate(mac_ctx, header, sizeof(header)) <= 0
  467. || EVP_DigestSignUpdate(mac_ctx, rec->input, rec->length) <= 0
  468. || EVP_DigestSignFinal(mac_ctx, md, &md_size) <= 0)
  469. goto end;
  470. OSSL_TRACE_BEGIN(TLS) {
  471. BIO_printf(trc_out, "seq:\n");
  472. BIO_dump_indent(trc_out, seq, 8, 4);
  473. BIO_printf(trc_out, "rec:\n");
  474. BIO_dump_indent(trc_out, rec->data, rec->length, 4);
  475. } OSSL_TRACE_END(TLS);
  476. if (!rl->isdtls && !tls_increment_sequence_ctr(rl)) {
  477. /* RLAYERfatal already called */
  478. goto end;
  479. }
  480. OSSL_TRACE_BEGIN(TLS) {
  481. BIO_printf(trc_out, "md:\n");
  482. BIO_dump_indent(trc_out, md, md_size, 4);
  483. } OSSL_TRACE_END(TLS);
  484. ret = 1;
  485. end:
  486. EVP_MD_CTX_free(hmac);
  487. return ret;
  488. }
  489. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
  490. # ifndef OPENSSL_NO_COMP
  491. # define MAX_PREFIX_LEN ((SSL3_ALIGN_PAYLOAD - 1) \
  492. + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD \
  493. + SSL3_RT_HEADER_LENGTH \
  494. + SSL3_RT_MAX_COMPRESSED_OVERHEAD)
  495. # else
  496. # define MAX_PREFIX_LEN ((SSL3_ALIGN_PAYLOAD - 1) \
  497. + SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD \
  498. + SSL3_RT_HEADER_LENGTH)
  499. # endif /* OPENSSL_NO_COMP */
  500. #else
  501. # ifndef OPENSSL_NO_COMP
  502. # define MAX_PREFIX_LEN (SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD \
  503. + SSL3_RT_HEADER_LENGTH \
  504. + SSL3_RT_MAX_COMPRESSED_OVERHEAD)
  505. # else
  506. # define MAX_PREFIX_LEN (SSL3_RT_SEND_MAX_ENCRYPTED_OVERHEAD \
  507. + SSL3_RT_HEADER_LENGTH)
  508. # endif /* OPENSSL_NO_COMP */
  509. #endif
  510. /* This function is also used by the SSLv3 implementation */
  511. int tls1_allocate_write_buffers(OSSL_RECORD_LAYER *rl,
  512. OSSL_RECORD_TEMPLATE *templates,
  513. size_t numtempl, size_t *prefix)
  514. {
  515. /* Do we need to add an empty record prefix? */
  516. *prefix = rl->need_empty_fragments
  517. && templates[0].type == SSL3_RT_APPLICATION_DATA;
  518. /*
  519. * In the prefix case we can allocate a much smaller buffer. Otherwise we
  520. * just allocate the default buffer size
  521. */
  522. if (!tls_setup_write_buffer(rl, numtempl + *prefix,
  523. *prefix ? MAX_PREFIX_LEN : 0, 0)) {
  524. /* RLAYERfatal() already called */
  525. return 0;
  526. }
  527. return 1;
  528. }
  529. /* This function is also used by the SSLv3 implementation */
  530. int tls1_initialise_write_packets(OSSL_RECORD_LAYER *rl,
  531. OSSL_RECORD_TEMPLATE *templates,
  532. size_t numtempl,
  533. OSSL_RECORD_TEMPLATE *prefixtempl,
  534. WPACKET *pkt,
  535. TLS_BUFFER *bufs,
  536. size_t *wpinited)
  537. {
  538. size_t align = 0;
  539. TLS_BUFFER *wb;
  540. size_t prefix;
  541. /* Do we need to add an empty record prefix? */
  542. prefix = rl->need_empty_fragments
  543. && templates[0].type == SSL3_RT_APPLICATION_DATA;
  544. if (prefix) {
  545. /*
  546. * countermeasure against known-IV weakness in CBC ciphersuites (see
  547. * http://www.openssl.org/~bodo/tls-cbc.txt)
  548. */
  549. prefixtempl->buf = NULL;
  550. prefixtempl->version = templates[0].version;
  551. prefixtempl->buflen = 0;
  552. prefixtempl->type = SSL3_RT_APPLICATION_DATA;
  553. wb = &bufs[0];
  554. #if defined(SSL3_ALIGN_PAYLOAD) && SSL3_ALIGN_PAYLOAD != 0
  555. align = (size_t)TLS_BUFFER_get_buf(wb) + SSL3_RT_HEADER_LENGTH;
  556. align = SSL3_ALIGN_PAYLOAD - 1
  557. - ((align - 1) % SSL3_ALIGN_PAYLOAD);
  558. #endif
  559. TLS_BUFFER_set_offset(wb, align);
  560. if (!WPACKET_init_static_len(&pkt[0], TLS_BUFFER_get_buf(wb),
  561. TLS_BUFFER_get_len(wb), 0)) {
  562. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  563. return 0;
  564. }
  565. *wpinited = 1;
  566. if (!WPACKET_allocate_bytes(&pkt[0], align, NULL)) {
  567. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  568. return 0;
  569. }
  570. }
  571. return tls_initialise_write_packets_default(rl, templates, numtempl,
  572. NULL,
  573. pkt + prefix, bufs + prefix,
  574. wpinited);
  575. }
  576. /* TLSv1.0, TLSv1.1 and TLSv1.2 all use the same funcs */
  577. const struct record_functions_st tls_1_funcs = {
  578. tls1_set_crypto_state,
  579. tls1_cipher,
  580. tls1_mac,
  581. tls_default_set_protocol_version,
  582. tls_default_read_n,
  583. tls_get_more_records,
  584. tls_default_validate_record_header,
  585. tls_default_post_process_record,
  586. tls_get_max_records_multiblock,
  587. tls_write_records_multiblock, /* Defined in tls_multib.c */
  588. tls1_allocate_write_buffers,
  589. tls1_initialise_write_packets,
  590. NULL,
  591. tls_prepare_record_header_default,
  592. NULL,
  593. tls_prepare_for_encryption_default,
  594. tls_post_encryption_processing_default,
  595. NULL
  596. };
  597. const struct record_functions_st dtls_1_funcs = {
  598. tls1_set_crypto_state,
  599. tls1_cipher,
  600. tls1_mac,
  601. tls_default_set_protocol_version,
  602. tls_default_read_n,
  603. dtls_get_more_records,
  604. NULL,
  605. NULL,
  606. NULL,
  607. tls_write_records_default,
  608. /*
  609. * Don't use tls1_allocate_write_buffers since that handles empty fragment
  610. * records which aren't needed in DTLS. We just use the default allocation
  611. * instead.
  612. */
  613. tls_allocate_write_buffers_default,
  614. /* Don't use tls1_initialise_write_packets for same reason as above */
  615. tls_initialise_write_packets_default,
  616. NULL,
  617. dtls_prepare_record_header,
  618. NULL,
  619. tls_prepare_for_encryption_default,
  620. dtls_post_encryption_processing,
  621. NULL
  622. };