record.h 9.2 KB

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