record.h 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /*
  2. * Copyright 1995-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/core_dispatch.h>
  10. #include "internal/recordmethod.h"
  11. /*****************************************************************************
  12. * *
  13. * These structures should be considered PRIVATE to the record layer. No *
  14. * non-record layer code should be using these structures in any way. *
  15. * *
  16. *****************************************************************************/
  17. #define SEQ_NUM_SIZE 8
  18. typedef struct tls_record_st {
  19. void *rechandle;
  20. int version;
  21. uint8_t type;
  22. /* The data buffer containing bytes from the record */
  23. const unsigned char *data;
  24. /*
  25. * Buffer that we allocated to store data. If non NULL always the same as
  26. * data (but non-const)
  27. */
  28. unsigned char *allocdata;
  29. /* Number of remaining to be read in the data buffer */
  30. size_t length;
  31. /* Offset into the data buffer where to start reading */
  32. size_t off;
  33. /* epoch number. DTLS only */
  34. uint16_t epoch;
  35. /* sequence number. DTLS only */
  36. unsigned char seq_num[SEQ_NUM_SIZE];
  37. #ifndef OPENSSL_NO_SCTP
  38. struct bio_dgram_sctp_rcvinfo recordinfo;
  39. #endif
  40. } TLS_RECORD;
  41. typedef struct record_pqueue_st {
  42. uint16_t epoch;
  43. struct pqueue_st *q;
  44. } record_pqueue;
  45. typedef struct dtls_record_layer_st {
  46. /*
  47. * The current data and handshake epoch. This is initially
  48. * undefined, and starts at zero once the initial handshake is
  49. * completed
  50. */
  51. uint16_t r_epoch;
  52. uint16_t w_epoch;
  53. /*
  54. * Buffered application records. Only for records between CCS and
  55. * Finished to prevent either protocol violation or unnecessary message
  56. * loss.
  57. */
  58. record_pqueue buffered_app_data;
  59. } DTLS_RECORD_LAYER;
  60. /*****************************************************************************
  61. * *
  62. * This structure should be considered "opaque" to anything outside of the *
  63. * record layer. No non-record layer code should be accessing the members of *
  64. * this structure. *
  65. * *
  66. *****************************************************************************/
  67. typedef struct record_layer_st {
  68. /* The parent SSL_CONNECTION structure */
  69. SSL_CONNECTION *s;
  70. /* Custom record layer: always selected if set */
  71. const OSSL_RECORD_METHOD *custom_rlmethod;
  72. /* Record layer specific argument */
  73. void *rlarg;
  74. /* Method to use for the read record layer*/
  75. const OSSL_RECORD_METHOD *rrlmethod;
  76. /* Method to use for the write record layer*/
  77. const OSSL_RECORD_METHOD *wrlmethod;
  78. /* The read record layer object itself */
  79. OSSL_RECORD_LAYER *rrl;
  80. /* The write record layer object itself */
  81. OSSL_RECORD_LAYER *wrl;
  82. /* BIO to store data destined for the next read record layer epoch */
  83. BIO *rrlnext;
  84. /* Default read buffer length to be passed to the record layer */
  85. size_t default_read_buf_len;
  86. /*
  87. * Read as many input bytes as possible (for
  88. * non-blocking reads)
  89. */
  90. int read_ahead;
  91. /* number of bytes sent so far */
  92. size_t wnum;
  93. unsigned char handshake_fragment[4];
  94. size_t handshake_fragment_len;
  95. /* partial write - check the numbers match */
  96. /* number bytes written */
  97. size_t wpend_tot;
  98. uint8_t wpend_type;
  99. /* number of bytes submitted */
  100. size_t wpend_ret;
  101. const unsigned char *wpend_buf;
  102. /* Count of the number of consecutive warning alerts received */
  103. unsigned int alert_count;
  104. DTLS_RECORD_LAYER *d;
  105. /* TLS1.3 padding callback */
  106. size_t (*record_padding_cb)(SSL *s, int type, size_t len, void *arg);
  107. void *record_padding_arg;
  108. size_t block_padding;
  109. /* How many records we have read from the record layer */
  110. size_t num_recs;
  111. /* The next record from the record layer that we need to process */
  112. size_t curr_rec;
  113. /* Record layer data to be processed */
  114. TLS_RECORD tlsrecs[SSL_MAX_PIPELINES];
  115. } RECORD_LAYER;
  116. /*****************************************************************************
  117. * *
  118. * The following macros/functions represent the libssl internal API to the *
  119. * record layer. Any libssl code may call these functions/macros *
  120. * *
  121. *****************************************************************************/
  122. #define RECORD_LAYER_set_read_ahead(rl, ra) ((rl)->read_ahead = (ra))
  123. #define RECORD_LAYER_get_read_ahead(rl) ((rl)->read_ahead)
  124. #define DTLS_RECORD_LAYER_get_w_epoch(rl) ((rl)->d->w_epoch)
  125. void RECORD_LAYER_init(RECORD_LAYER *rl, SSL_CONNECTION *s);
  126. int RECORD_LAYER_clear(RECORD_LAYER *rl);
  127. int RECORD_LAYER_reset(RECORD_LAYER *rl);
  128. int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
  129. int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
  130. int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
  131. int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
  132. __owur size_t ssl3_pending(const SSL *s);
  133. __owur int ssl3_write_bytes(SSL *s, uint8_t type, const void *buf, size_t len,
  134. size_t *written);
  135. __owur int ssl3_read_bytes(SSL *s, uint8_t type, uint8_t *recvd_type,
  136. unsigned char *buf, size_t len, int peek,
  137. size_t *readbytes);
  138. int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
  139. void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
  140. void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
  141. __owur int dtls1_read_bytes(SSL *s, uint8_t type, uint8_t *recvd_type,
  142. unsigned char *buf, size_t len, int peek,
  143. size_t *readbytes);
  144. __owur int dtls1_write_bytes(SSL_CONNECTION *s, uint8_t type, const void *buf,
  145. size_t len, size_t *written);
  146. int do_dtls1_write(SSL_CONNECTION *s, uint8_t type, const unsigned char *buf,
  147. size_t len, size_t *written);
  148. void dtls1_increment_epoch(SSL_CONNECTION *s, int rw);
  149. int ssl_release_record(SSL_CONNECTION *s, TLS_RECORD *rr, size_t length);
  150. # define HANDLE_RLAYER_READ_RETURN(s, ret) \
  151. ossl_tls_handle_rlayer_return(s, 0, ret, OPENSSL_FILE, OPENSSL_LINE)
  152. # define HANDLE_RLAYER_WRITE_RETURN(s, ret) \
  153. ossl_tls_handle_rlayer_return(s, 1, ret, OPENSSL_FILE, OPENSSL_LINE)
  154. int ossl_tls_handle_rlayer_return(SSL_CONNECTION *s, int writing, int ret,
  155. char *file, int line);
  156. int ssl_set_new_record_layer(SSL_CONNECTION *s, int version,
  157. int direction, int level,
  158. unsigned char *secret, size_t secretlen,
  159. unsigned char *key, size_t keylen,
  160. unsigned char *iv, size_t ivlen,
  161. unsigned char *mackey, size_t mackeylen,
  162. const EVP_CIPHER *ciph, size_t taglen,
  163. int mactype, const EVP_MD *md,
  164. const SSL_COMP *comp, const EVP_MD *kdfdigest);
  165. int ssl_set_record_protocol_version(SSL_CONNECTION *s, int vers);
  166. # define OSSL_FUNC_RLAYER_SKIP_EARLY_DATA 1
  167. OSSL_CORE_MAKE_FUNC(int, rlayer_skip_early_data, (void *cbarg))
  168. # define OSSL_FUNC_RLAYER_MSG_CALLBACK 2
  169. OSSL_CORE_MAKE_FUNC(void, rlayer_msg_callback, (int write_p, int version,
  170. int content_type,
  171. const void *buf, size_t len,
  172. void *cbarg))
  173. # define OSSL_FUNC_RLAYER_SECURITY 3
  174. OSSL_CORE_MAKE_FUNC(int, rlayer_security, (void *cbarg, int op, int bits,
  175. int nid, void *other))
  176. # define OSSL_FUNC_RLAYER_PADDING 4
  177. OSSL_CORE_MAKE_FUNC(size_t, rlayer_padding, (void *cbarg, int type, size_t len))