openssl/ssl/record
John Baldwin 074a6e86e6 Use a flag in SSL3_BUFFER to track when an application buffer is reused.
With KTLS, writes to an SSL connection store the application buffer
pointer directly in the 'buf' member instead of allocating a separate
buffer to hold the encrypted data.  As a result,
ssl3_release_write_buffer() has to avoid freeing these 'buf' pointers.

Previously, ssl3_release_write_buffer() checked for KTLS being enabled
on the write BIO to determine if a buffer should be freed.  However, a
buffer can outlive a BIO.  For example, 'openssl s_time' creates new
write BIOs when reusing sessions.  Since the new BIO did not have KTLS
enabled at the start of a connection, ssl3_release_write_buffer()
would incorrectly try to free the 'buf' pointer from the previous KTLS
connection.  To fix, track the state of 'buf' explicitly in
SSL3_BUFFER to determine if the 'buf' should be freed or simply
cleared.

Reviewed-by: Matt Caswell <matt@openssl.org>
Reviewed-by: Tomas Mraz <tmraz@fedoraproject.org>
(Merged from https://github.com/openssl/openssl/pull/10489)
2020-03-16 10:41:51 +01:00
..
dtls1_bitmap.c Reorganize local header files 2019-09-28 20:26:35 +02:00
README Reorganize local header files 2019-09-28 20:26:35 +02:00
rec_layer_d1.c Reorganize local header files 2019-09-28 20:26:35 +02:00
rec_layer_s3.c Use a flag in SSL3_BUFFER to track when an application buffer is reused. 2020-03-16 10:41:51 +01:00
record_local.h Use a flag in SSL3_BUFFER to track when an application buffer is reused. 2020-03-16 10:41:51 +01:00
record.h Use a flag in SSL3_BUFFER to track when an application buffer is reused. 2020-03-16 10:41:51 +01:00
ssl3_buffer.c Use a flag in SSL3_BUFFER to track when an application buffer is reused. 2020-03-16 10:41:51 +01:00
ssl3_record_tls13.c Fix some typos 2019-12-11 19:04:01 +01:00
ssl3_record.c Handle max_fragment_length overflow for DTLS 2020-02-19 09:21:10 +01:00

Record Layer Design
===================

This file provides some guidance on the thinking behind the design of the
record layer code to aid future maintenance.

The record layer is divided into a number of components. At the time of writing
there are four: SSL3_RECORD, SSL3_BUFFER, DLTS1_BITMAP and RECORD_LAYER. Each
of these components is defined by:
1) A struct definition of the same name as the component
2) A set of source files that define the functions for that component
3) A set of accessor macros

All struct definitions are in record.h. The functions and macros are either
defined in record.h or record_local.h dependent on whether they are intended to
be private to the record layer, or whether they form part of the API to the rest
of libssl.

The source files map to components as follows:

dtls1_bitmap.c                                   -> DTLS1_BITMAP component
ssl3_buffer.c                                    -> SSL3_BUFFER component
ssl3_record.c                                    -> SSL3_RECORD component
rec_layer_s3.c, rec_layer_d1.c                   -> RECORD_LAYER component

The RECORD_LAYER component is a facade pattern, i.e. it provides a simplified
interface to the record layer for the rest of libssl. The other 3 components are
entirely private to the record layer and therefore should never be accessed
directly by libssl.

Any component can directly access its own members - they are private to that
component, e.g. ssl3_buffer.c can access members of the SSL3_BUFFER struct
without using a macro. No component can directly access the members of another
component, e.g. ssl3_buffer cannot reach inside the RECORD_LAYER component to
directly access its members. Instead components use accessor macros, so if code
in ssl3_buffer.c wants to access the members of the RECORD_LAYER it uses the
RECORD_LAYER_* macros.

Conceptually it looks like this:

                        libssl
                           |
---------------------------|-----record.h--------------------------------------
                           |
                    _______V______________
                   |                      |
                   |    RECORD_LAYER      |
                   |                      |
                   |    rec_layer_s3.c    |
                   |          ^           |
                   | _________|__________ |
                   ||                    ||
                   || DTLS1_RECORD_LAYER ||
                   ||                    ||
                   || rec_layer_d1.c     ||
                   ||____________________||
                   |______________________|
        record_local.h     ^   ^   ^
         _________________|   |   |_________________
        |                     |                     |
   _____V_________      ______V________      _______V________
  |               |    |               |    |                |
  | SSL3_BUFFER   |    | SSL3_RECORD   |    | DTLS1_BITMAP   |
  |               |--->|               |    |                |
  | ssl3_buffer.c |    | ssl3_record.c |    | dtls1_bitmap.c |
  |_______________|    |_______________|    |________________|


The two RECORD_LAYER source files build on each other, i.e.
the main one is rec_layer_s3.c which provides the core SSL/TLS layer. The second
one is rec_layer_d1.c which builds off of the SSL/TLS code to provide DTLS
specific capabilities. It uses some DTLS specific RECORD_LAYER component members
which should only be accessed from rec_layer_d1.c. These are held in the
DTLS1_RECORD_LAYER struct.