record.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. * Copyright 1995-2020 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. /*****************************************************************************
  10. * *
  11. * These structures should be considered PRIVATE to the record layer. No *
  12. * non-record layer code should be using these structures in any way. *
  13. * *
  14. *****************************************************************************/
  15. typedef struct ssl3_buffer_st {
  16. /* at least SSL3_RT_MAX_PACKET_SIZE bytes, see ssl3_setup_buffers() */
  17. unsigned char *buf;
  18. /* default buffer size (or 0 if no default set) */
  19. size_t default_len;
  20. /* buffer size */
  21. size_t len;
  22. /* where to 'copy from' */
  23. size_t offset;
  24. /* how many bytes left */
  25. size_t left;
  26. /* 'buf' is from application for KTLS */
  27. int app_buffer;
  28. } SSL3_BUFFER;
  29. #define SEQ_NUM_SIZE 8
  30. typedef struct ssl3_record_st {
  31. /* Record layer version */
  32. /* r */
  33. int rec_version;
  34. /* type of record */
  35. /* r */
  36. int type;
  37. /* How many bytes available */
  38. /* rw */
  39. size_t length;
  40. /*
  41. * How many bytes were available before padding was removed? This is used
  42. * to implement the MAC check in constant time for CBC records.
  43. */
  44. /* rw */
  45. size_t orig_len;
  46. /* read/write offset into 'buf' */
  47. /* r */
  48. size_t off;
  49. /* pointer to the record data */
  50. /* rw */
  51. unsigned char *data;
  52. /* where the decode bytes are */
  53. /* rw */
  54. unsigned char *input;
  55. /* only used with decompression - malloc()ed */
  56. /* r */
  57. unsigned char *comp;
  58. /* Whether the data from this record has already been read or not */
  59. /* r */
  60. unsigned int read;
  61. /* epoch number, needed by DTLS1 */
  62. /* r */
  63. unsigned long epoch;
  64. /* sequence number, needed by DTLS1 */
  65. /* r */
  66. unsigned char seq_num[SEQ_NUM_SIZE];
  67. } SSL3_RECORD;
  68. typedef struct dtls1_bitmap_st {
  69. /* Track 32 packets on 32-bit systems and 64 - on 64-bit systems */
  70. unsigned long map;
  71. /* Max record number seen so far, 64-bit value in big-endian encoding */
  72. unsigned char max_seq_num[SEQ_NUM_SIZE];
  73. } DTLS1_BITMAP;
  74. typedef struct record_pqueue_st {
  75. unsigned short epoch;
  76. struct pqueue_st *q;
  77. } record_pqueue;
  78. typedef struct dtls1_record_data_st {
  79. unsigned char *packet;
  80. size_t packet_length;
  81. SSL3_BUFFER rbuf;
  82. SSL3_RECORD rrec;
  83. #ifndef OPENSSL_NO_SCTP
  84. struct bio_dgram_sctp_rcvinfo recordinfo;
  85. #endif
  86. } DTLS1_RECORD_DATA;
  87. typedef struct dtls_record_layer_st {
  88. /*
  89. * The current data and handshake epoch. This is initially
  90. * undefined, and starts at zero once the initial handshake is
  91. * completed
  92. */
  93. unsigned short r_epoch;
  94. unsigned short w_epoch;
  95. /* records being received in the current epoch */
  96. DTLS1_BITMAP bitmap;
  97. /* renegotiation starts a new set of sequence numbers */
  98. DTLS1_BITMAP next_bitmap;
  99. /* Received handshake records (processed and unprocessed) */
  100. record_pqueue unprocessed_rcds;
  101. record_pqueue processed_rcds;
  102. /*
  103. * Buffered application records. Only for records between CCS and
  104. * Finished to prevent either protocol violation or unnecessary message
  105. * loss.
  106. */
  107. record_pqueue buffered_app_data;
  108. /* save last and current sequence numbers for retransmissions */
  109. unsigned char last_write_sequence[8];
  110. unsigned char curr_write_sequence[8];
  111. } DTLS_RECORD_LAYER;
  112. /*****************************************************************************
  113. * *
  114. * This structure should be considered "opaque" to anything outside of the *
  115. * record layer. No non-record layer code should be accessing the members of *
  116. * this structure. *
  117. * *
  118. *****************************************************************************/
  119. typedef struct record_layer_st {
  120. /* The parent SSL structure */
  121. SSL *s;
  122. /*
  123. * Read as many input bytes as possible (for
  124. * non-blocking reads)
  125. */
  126. int read_ahead;
  127. /* where we are when reading */
  128. int rstate;
  129. /* How many pipelines can be used to read data */
  130. size_t numrpipes;
  131. /* How many pipelines can be used to write data */
  132. size_t numwpipes;
  133. /* read IO goes into here */
  134. SSL3_BUFFER rbuf;
  135. /* write IO goes into here */
  136. SSL3_BUFFER wbuf[SSL_MAX_PIPELINES];
  137. /* each decoded record goes in here */
  138. SSL3_RECORD rrec[SSL_MAX_PIPELINES];
  139. /* used internally to point at a raw packet */
  140. unsigned char *packet;
  141. size_t packet_length;
  142. /* number of bytes sent so far */
  143. size_t wnum;
  144. unsigned char handshake_fragment[4];
  145. size_t handshake_fragment_len;
  146. /* The number of consecutive empty records we have received */
  147. size_t empty_record_count;
  148. /* partial write - check the numbers match */
  149. /* number bytes written */
  150. size_t wpend_tot;
  151. int wpend_type;
  152. /* number of bytes submitted */
  153. size_t wpend_ret;
  154. const unsigned char *wpend_buf;
  155. unsigned char read_sequence[SEQ_NUM_SIZE];
  156. unsigned char write_sequence[SEQ_NUM_SIZE];
  157. /* Set to true if this is the first record in a connection */
  158. unsigned int is_first_record;
  159. /* Count of the number of consecutive warning alerts received */
  160. unsigned int alert_count;
  161. DTLS_RECORD_LAYER *d;
  162. } RECORD_LAYER;
  163. /*****************************************************************************
  164. * *
  165. * The following macros/functions represent the libssl internal API to the *
  166. * record layer. Any libssl code may call these functions/macros *
  167. * *
  168. *****************************************************************************/
  169. struct ssl_mac_buf_st {
  170. unsigned char *mac;
  171. int alloced;
  172. };
  173. typedef struct ssl_mac_buf_st SSL_MAC_BUF;
  174. #define MIN_SSL2_RECORD_LEN 9
  175. #define RECORD_LAYER_set_read_ahead(rl, ra) ((rl)->read_ahead = (ra))
  176. #define RECORD_LAYER_get_read_ahead(rl) ((rl)->read_ahead)
  177. #define RECORD_LAYER_get_packet(rl) ((rl)->packet)
  178. #define RECORD_LAYER_get_packet_length(rl) ((rl)->packet_length)
  179. #define RECORD_LAYER_add_packet_length(rl, inc) ((rl)->packet_length += (inc))
  180. #define DTLS_RECORD_LAYER_get_w_epoch(rl) ((rl)->d->w_epoch)
  181. #define DTLS_RECORD_LAYER_get_processed_rcds(rl) \
  182. ((rl)->d->processed_rcds)
  183. #define DTLS_RECORD_LAYER_get_unprocessed_rcds(rl) \
  184. ((rl)->d->unprocessed_rcds)
  185. #define RECORD_LAYER_get_rbuf(rl) (&(rl)->rbuf)
  186. #define RECORD_LAYER_get_wbuf(rl) ((rl)->wbuf)
  187. void RECORD_LAYER_init(RECORD_LAYER *rl, SSL *s);
  188. void RECORD_LAYER_clear(RECORD_LAYER *rl);
  189. void RECORD_LAYER_release(RECORD_LAYER *rl);
  190. int RECORD_LAYER_read_pending(const RECORD_LAYER *rl);
  191. int RECORD_LAYER_processed_read_pending(const RECORD_LAYER *rl);
  192. int RECORD_LAYER_write_pending(const RECORD_LAYER *rl);
  193. void RECORD_LAYER_reset_read_sequence(RECORD_LAYER *rl);
  194. void RECORD_LAYER_reset_write_sequence(RECORD_LAYER *rl);
  195. int RECORD_LAYER_is_sslv2_record(RECORD_LAYER *rl);
  196. size_t RECORD_LAYER_get_rrec_length(RECORD_LAYER *rl);
  197. __owur size_t ssl3_pending(const SSL *s);
  198. __owur int ssl3_write_bytes(SSL *s, int type, const void *buf, size_t len,
  199. size_t *written);
  200. int do_ssl3_write(SSL *s, int type, const unsigned char *buf,
  201. size_t *pipelens, size_t numpipes,
  202. int create_empty_fragment, size_t *written);
  203. __owur int ssl3_read_bytes(SSL *s, int type, int *recvd_type,
  204. unsigned char *buf, size_t len, int peek,
  205. size_t *readbytes);
  206. __owur int ssl3_setup_buffers(SSL *s);
  207. __owur int ssl3_enc(SSL *s, SSL3_RECORD *inrecs, size_t n_recs, int send,
  208. SSL_MAC_BUF *mac, size_t macsize);
  209. __owur int n_ssl3_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
  210. __owur int ssl3_write_pending(SSL *s, int type, const unsigned char *buf, size_t len,
  211. size_t *written);
  212. __owur int tls1_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int sending,
  213. SSL_MAC_BUF *mac, size_t macsize);
  214. __owur int tls1_mac(SSL *ssl, SSL3_RECORD *rec, unsigned char *md, int send);
  215. __owur int tls13_enc(SSL *s, SSL3_RECORD *recs, size_t n_recs, int send,
  216. SSL_MAC_BUF *mac, size_t macsize);
  217. int DTLS_RECORD_LAYER_new(RECORD_LAYER *rl);
  218. void DTLS_RECORD_LAYER_free(RECORD_LAYER *rl);
  219. void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
  220. void DTLS_RECORD_LAYER_set_saved_w_epoch(RECORD_LAYER *rl, unsigned short e);
  221. void DTLS_RECORD_LAYER_clear(RECORD_LAYER *rl);
  222. void DTLS_RECORD_LAYER_set_write_sequence(RECORD_LAYER *rl, unsigned char *seq);
  223. __owur int dtls1_read_bytes(SSL *s, int type, int *recvd_type,
  224. unsigned char *buf, size_t len, int peek,
  225. size_t *readbytes);
  226. __owur int dtls1_write_bytes(SSL *s, int type, const void *buf, size_t len,
  227. size_t *written);
  228. int do_dtls1_write(SSL *s, int type, const unsigned char *buf,
  229. size_t len, int create_empty_fragment, size_t *written);
  230. void dtls1_reset_seq_numbers(SSL *s, int rw);
  231. int dtls_buffer_listen_record(SSL *s, size_t len, unsigned char *seq,
  232. size_t off);