tls1_meth.c 24 KB

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