record.h 8.2 KB

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