ssl3_meth.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 "internal/ssl3_cbc.h"
  12. #include "../../ssl_local.h"
  13. #include "../record_local.h"
  14. #include "recmethod_local.h"
  15. static int ssl3_set_crypto_state(OSSL_RECORD_LAYER *rl, int level,
  16. unsigned char *key, size_t keylen,
  17. unsigned char *iv, size_t ivlen,
  18. unsigned char *mackey, size_t mackeylen,
  19. const EVP_CIPHER *ciph,
  20. size_t taglen,
  21. int mactype,
  22. const EVP_MD *md,
  23. COMP_METHOD *comp)
  24. {
  25. EVP_CIPHER_CTX *ciph_ctx;
  26. int enc = (rl->direction == OSSL_RECORD_DIRECTION_WRITE) ? 1 : 0;
  27. if (md == NULL) {
  28. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  29. return OSSL_RECORD_RETURN_FATAL;
  30. }
  31. if ((rl->enc_ctx = EVP_CIPHER_CTX_new()) == NULL) {
  32. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  33. return OSSL_RECORD_RETURN_FATAL;
  34. }
  35. ciph_ctx = rl->enc_ctx;
  36. rl->md_ctx = EVP_MD_CTX_new();
  37. if (rl->md_ctx == NULL) {
  38. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  39. return OSSL_RECORD_RETURN_FATAL;
  40. }
  41. if ((md != NULL && EVP_DigestInit_ex(rl->md_ctx, md, NULL) <= 0)) {
  42. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  43. return OSSL_RECORD_RETURN_FATAL;
  44. }
  45. #ifndef OPENSSL_NO_COMP
  46. if (comp != NULL) {
  47. rl->compctx = COMP_CTX_new(comp);
  48. if (rl->compctx == NULL) {
  49. ERR_raise(ERR_LIB_SSL, SSL_R_COMPRESSION_LIBRARY_ERROR);
  50. return OSSL_RECORD_RETURN_FATAL;
  51. }
  52. }
  53. #endif
  54. if (!EVP_CipherInit_ex(ciph_ctx, ciph, NULL, key, iv, enc)) {
  55. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  56. return OSSL_RECORD_RETURN_FATAL;
  57. }
  58. if (EVP_CIPHER_get0_provider(ciph) != NULL
  59. && !ossl_set_tls_provider_parameters(rl, ciph_ctx, ciph, md)) {
  60. /* ERR_raise already called */
  61. return OSSL_RECORD_RETURN_FATAL;
  62. }
  63. if (mackeylen > sizeof(rl->mac_secret)) {
  64. ERR_raise(ERR_LIB_SSL, ERR_R_INTERNAL_ERROR);
  65. return OSSL_RECORD_RETURN_FATAL;
  66. }
  67. memcpy(rl->mac_secret, mackey, mackeylen);
  68. return OSSL_RECORD_RETURN_SUCCESS;
  69. }
  70. /*
  71. * ssl3_cipher encrypts/decrypts |n_recs| records in |inrecs|. Calls RLAYERfatal
  72. * on internal error, but not otherwise. It is the responsibility of the caller
  73. * to report a bad_record_mac
  74. *
  75. * Returns:
  76. * 0: if the record is publicly invalid, or an internal error
  77. * 1: Success or Mac-then-encrypt decryption failed (MAC will be randomised)
  78. */
  79. static int ssl3_cipher(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *inrecs,
  80. size_t n_recs, int sending, SSL_MAC_BUF *mac,
  81. size_t macsize)
  82. {
  83. TLS_RL_RECORD *rec;
  84. EVP_CIPHER_CTX *ds;
  85. size_t l, i;
  86. size_t bs;
  87. const EVP_CIPHER *enc;
  88. int provided;
  89. rec = inrecs;
  90. /*
  91. * We shouldn't ever be called with more than one record in the SSLv3 case
  92. */
  93. if (n_recs != 1)
  94. return 0;
  95. ds = rl->enc_ctx;
  96. if (ds == NULL || (enc = EVP_CIPHER_CTX_get0_cipher(ds)) == NULL)
  97. return 0;
  98. provided = (EVP_CIPHER_get0_provider(enc) != NULL);
  99. l = rec->length;
  100. bs = EVP_CIPHER_CTX_get_block_size(ds);
  101. /* COMPRESS */
  102. if ((bs != 1) && sending && !provided) {
  103. /*
  104. * We only do this for legacy ciphers. Provided ciphers add the
  105. * padding on the provider side.
  106. */
  107. i = bs - (l % bs);
  108. /* we need to add 'i-1' padding bytes */
  109. l += i;
  110. /*
  111. * the last of these zero bytes will be overwritten with the
  112. * padding length.
  113. */
  114. memset(&rec->input[rec->length], 0, i);
  115. rec->length += i;
  116. rec->input[l - 1] = (unsigned char)(i - 1);
  117. }
  118. if (!sending) {
  119. if (l == 0 || l % bs != 0) {
  120. /* Publicly invalid */
  121. return 0;
  122. }
  123. /* otherwise, rec->length >= bs */
  124. }
  125. if (provided) {
  126. int outlen;
  127. if (!EVP_CipherUpdate(ds, rec->data, &outlen, rec->input,
  128. (unsigned int)l))
  129. return 0;
  130. rec->length = outlen;
  131. if (!sending && mac != NULL) {
  132. /* Now get a pointer to the MAC */
  133. OSSL_PARAM params[2], *p = params;
  134. /* Get the MAC */
  135. mac->alloced = 0;
  136. *p++ = OSSL_PARAM_construct_octet_ptr(OSSL_CIPHER_PARAM_TLS_MAC,
  137. (void **)&mac->mac,
  138. macsize);
  139. *p = OSSL_PARAM_construct_end();
  140. if (!EVP_CIPHER_CTX_get_params(ds, params)) {
  141. /* Shouldn't normally happen */
  142. RLAYERfatal(rl, SSL_AD_INTERNAL_ERROR, ERR_R_INTERNAL_ERROR);
  143. return 0;
  144. }
  145. }
  146. } else {
  147. if (EVP_Cipher(ds, rec->data, rec->input, (unsigned int)l) < 1) {
  148. /* Shouldn't happen */
  149. RLAYERfatal(rl, SSL_AD_BAD_RECORD_MAC, ERR_R_INTERNAL_ERROR);
  150. return 0;
  151. }
  152. if (!sending)
  153. return ssl3_cbc_remove_padding_and_mac(&rec->length,
  154. rec->orig_len,
  155. rec->data,
  156. (mac != NULL) ? &mac->mac : NULL,
  157. (mac != NULL) ? &mac->alloced : NULL,
  158. bs,
  159. macsize,
  160. rl->libctx);
  161. }
  162. return 1;
  163. }
  164. static const unsigned char ssl3_pad_1[48] = {
  165. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  166. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  167. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  168. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  169. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36,
  170. 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36, 0x36
  171. };
  172. static const unsigned char ssl3_pad_2[48] = {
  173. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  174. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  175. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  176. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  177. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c,
  178. 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c, 0x5c
  179. };
  180. static int ssl3_mac(OSSL_RECORD_LAYER *rl, TLS_RL_RECORD *rec, unsigned char *md,
  181. int sending)
  182. {
  183. unsigned char *mac_sec, *seq = rl->sequence;
  184. const EVP_MD_CTX *hash;
  185. unsigned char *p, rec_char;
  186. size_t md_size;
  187. size_t npad;
  188. int t;
  189. mac_sec = &(rl->mac_secret[0]);
  190. hash = rl->md_ctx;
  191. t = EVP_MD_CTX_get_size(hash);
  192. if (t <= 0)
  193. return 0;
  194. md_size = t;
  195. npad = (48 / md_size) * md_size;
  196. if (!sending
  197. && EVP_CIPHER_CTX_get_mode(rl->enc_ctx) == EVP_CIPH_CBC_MODE
  198. && ssl3_cbc_record_digest_supported(hash)) {
  199. #ifdef OPENSSL_NO_DEPRECATED_3_0
  200. return 0;
  201. #else
  202. /*
  203. * This is a CBC-encrypted record. We must avoid leaking any
  204. * timing-side channel information about how many blocks of data we
  205. * are hashing because that gives an attacker a timing-oracle.
  206. */
  207. /*-
  208. * npad is, at most, 48 bytes and that's with MD5:
  209. * 16 + 48 + 8 (sequence bytes) + 1 + 2 = 75.
  210. *
  211. * With SHA-1 (the largest hash speced for SSLv3) the hash size
  212. * goes up 4, but npad goes down by 8, resulting in a smaller
  213. * total size.
  214. */
  215. unsigned char header[75];
  216. size_t j = 0;
  217. memcpy(header + j, mac_sec, md_size);
  218. j += md_size;
  219. memcpy(header + j, ssl3_pad_1, npad);
  220. j += npad;
  221. memcpy(header + j, seq, 8);
  222. j += 8;
  223. header[j++] = rec->type;
  224. header[j++] = (unsigned char)(rec->length >> 8);
  225. header[j++] = (unsigned char)(rec->length & 0xff);
  226. /* Final param == is SSLv3 */
  227. if (ssl3_cbc_digest_record(EVP_MD_CTX_get0_md(hash),
  228. md, &md_size,
  229. header, rec->input,
  230. rec->length, rec->orig_len,
  231. mac_sec, md_size, 1) <= 0)
  232. return 0;
  233. #endif
  234. } else {
  235. unsigned int md_size_u;
  236. /* Chop the digest off the end :-) */
  237. EVP_MD_CTX *md_ctx = EVP_MD_CTX_new();
  238. if (md_ctx == NULL)
  239. return 0;
  240. rec_char = rec->type;
  241. p = md;
  242. s2n(rec->length, p);
  243. if (EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
  244. || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
  245. || EVP_DigestUpdate(md_ctx, ssl3_pad_1, npad) <= 0
  246. || EVP_DigestUpdate(md_ctx, seq, 8) <= 0
  247. || EVP_DigestUpdate(md_ctx, &rec_char, 1) <= 0
  248. || EVP_DigestUpdate(md_ctx, md, 2) <= 0
  249. || EVP_DigestUpdate(md_ctx, rec->input, rec->length) <= 0
  250. || EVP_DigestFinal_ex(md_ctx, md, NULL) <= 0
  251. || EVP_MD_CTX_copy_ex(md_ctx, hash) <= 0
  252. || EVP_DigestUpdate(md_ctx, mac_sec, md_size) <= 0
  253. || EVP_DigestUpdate(md_ctx, ssl3_pad_2, npad) <= 0
  254. || EVP_DigestUpdate(md_ctx, md, md_size) <= 0
  255. || EVP_DigestFinal_ex(md_ctx, md, &md_size_u) <= 0) {
  256. EVP_MD_CTX_free(md_ctx);
  257. return 0;
  258. }
  259. EVP_MD_CTX_free(md_ctx);
  260. }
  261. if (!tls_increment_sequence_ctr(rl))
  262. return 0;
  263. return 1;
  264. }
  265. struct record_functions_st ssl_3_0_funcs = {
  266. ssl3_set_crypto_state,
  267. ssl3_cipher,
  268. ssl3_mac,
  269. tls_default_set_protocol_version,
  270. tls_default_read_n,
  271. tls_get_more_records,
  272. tls_default_validate_record_header,
  273. tls_default_post_process_record,
  274. tls_get_max_records_default,
  275. tls_write_records_default,
  276. /* These 2 functions are defined in tls1_meth.c */
  277. tls1_allocate_write_buffers,
  278. tls1_initialise_write_packets,
  279. NULL,
  280. tls_prepare_record_header_default,
  281. NULL,
  282. tls_prepare_for_encryption_default,
  283. tls_post_encryption_processing_default,
  284. NULL
  285. };