ssl3_meth.c 11 KB

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