2022-04-07 19:30:30 +08:00
|
|
|
/*
|
|
|
|
* Copyright 2022 The OpenSSL Project Authors. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License 2.0 (the "License"). You may not use
|
|
|
|
* this file except in compliance with the License. You can obtain a copy
|
|
|
|
* in the file LICENSE in the source distribution or at
|
|
|
|
* https://www.openssl.org/source/license.html
|
|
|
|
*/
|
|
|
|
|
2022-05-06 22:10:00 +08:00
|
|
|
#ifndef OSSL_INTERNAL_RECORDMETHOD_H
|
|
|
|
# define OSSL_INTERNAL_RECORDMETHOD_H
|
|
|
|
# pragma once
|
|
|
|
|
|
|
|
# include <openssl/ssl.h>
|
2022-04-07 19:30:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We use the term "record" here to refer to a packet of data. Records are
|
|
|
|
* typically protected via a cipher and MAC, or an AEAD cipher (although not
|
|
|
|
* always). This usage of the term record is consistent with the TLS concept.
|
|
|
|
* In QUIC the term "record" is not used but it is analogous to the QUIC term
|
|
|
|
* "packet". The interface in this file applies to all protocols that protect
|
|
|
|
* records/packets of data, i.e. (D)TLS and QUIC. The term record is used to
|
|
|
|
* refer to both contexts.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An OSSL_RECORD_METHOD is a protcol specific method which provides the
|
|
|
|
* functions for reading and writing records for that protocol. Which
|
|
|
|
* OSSL_RECORD_METHOD to use for a given protocol is defined by the SSL_METHOD.
|
|
|
|
*/
|
|
|
|
typedef struct ossl_record_method_st OSSL_RECORD_METHOD;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* An OSSL_RECORD_LAYER is just an externally defined opaque pointer created by
|
|
|
|
* the method
|
|
|
|
*/
|
|
|
|
typedef struct ossl_record_layer_st OSSL_RECORD_LAYER;
|
|
|
|
|
|
|
|
|
2022-05-06 22:10:00 +08:00
|
|
|
# define OSSL_RECORD_ROLE_CLIENT 0
|
|
|
|
# define OSSL_RECORD_ROLE_SERVER 1
|
2022-04-07 19:30:30 +08:00
|
|
|
|
2022-05-06 22:10:00 +08:00
|
|
|
# define OSSL_RECORD_DIRECTION_READ 0
|
|
|
|
# define OSSL_RECORD_DIRECTION_WRITE 1
|
2022-04-07 19:30:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Protection level. For <= TLSv1.2 only "NONE" and "APPLICATION" are used.
|
|
|
|
*/
|
2022-05-06 22:10:00 +08:00
|
|
|
# define OSSL_RECORD_PROTECTION_LEVEL_NONE 0
|
|
|
|
# define OSSL_RECORD_PROTECTION_LEVEL_EARLY 1
|
|
|
|
# define OSSL_RECORD_PROTECTION_LEVEL_HANDSHAKE 2
|
|
|
|
# define OSSL_RECORD_PROTECTION_LEVEL_APPLICATION 3
|
2022-04-07 19:30:30 +08:00
|
|
|
|
2022-05-06 22:10:00 +08:00
|
|
|
# define OSSL_RECORD_RETURN_SUCCESS 1
|
|
|
|
# define OSSL_RECORD_RETURN_RETRY 0
|
|
|
|
# define OSSL_RECORD_RETURN_NON_FATAL_ERR -1
|
|
|
|
# define OSSL_RECORD_RETURN_FATAL -2
|
|
|
|
# define OSSL_RECORD_RETURN_EOF -3
|
2022-04-08 00:35:36 +08:00
|
|
|
|
2022-04-07 19:30:30 +08:00
|
|
|
/*
|
|
|
|
* Template for creating a record. A record consists of the |type| of data it
|
2022-08-12 16:51:51 +08:00
|
|
|
* will contain (e.g. alert, handshake, application data, etc) along with a
|
|
|
|
* buffer of payload data in |buf| of length |buflen|.
|
2022-04-07 19:30:30 +08:00
|
|
|
*/
|
|
|
|
struct ossl_record_template_st {
|
|
|
|
int type;
|
2022-08-27 00:34:40 +08:00
|
|
|
unsigned int version;
|
2022-08-12 16:51:51 +08:00
|
|
|
const unsigned char *buf;
|
|
|
|
size_t buflen;
|
2022-04-07 19:30:30 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct ossl_record_template_st OSSL_RECORD_TEMPLATE;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Rather than a "method" approach, we could make this fetchable - Should we?
|
|
|
|
* There could be some complexity in finding suitable record layer implementations
|
|
|
|
* e.g. we need to find one that matches the negotiated protocol, cipher,
|
|
|
|
* extensions, etc. The selection_cb approach given above doesn't work so well
|
|
|
|
* if unknown third party providers with OSSL_RECORD_METHOD implementations are
|
|
|
|
* loaded.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If this becomes public API then we will need functions to create and
|
|
|
|
* free an OSSL_RECORD_METHOD, as well as functions to get/set the various
|
|
|
|
* function pointers....unless we make it fetchable.
|
|
|
|
*/
|
|
|
|
struct ossl_record_method_st {
|
|
|
|
/*
|
|
|
|
* Create a new OSSL_RECORD_LAYER object for handling the protocol version
|
|
|
|
* set by |vers|. |role| is 0 for client and 1 for server. |direction|
|
|
|
|
* indicates either read or write. |level| is the protection level as
|
|
|
|
* described above. |settings| are mandatory settings that will cause the
|
|
|
|
* new() call to fail if they are not understood (for example to require
|
|
|
|
* Encrypt-Then-Mac support). |options| are optional settings that will not
|
|
|
|
* cause the new() call to fail if they are not understood (for example
|
|
|
|
* whether to use "read ahead" or not).
|
|
|
|
*
|
|
|
|
* The BIO in |transport| is the BIO for the underlying transport layer.
|
|
|
|
* Where the direction is "read", then this BIO will only ever be used for
|
|
|
|
* reading data. Where the direction is "write", then this BIO will only
|
|
|
|
* every be used for writing data.
|
|
|
|
*
|
|
|
|
* An SSL object will always have at least 2 OSSL_RECORD_LAYER objects in
|
|
|
|
* force at any one time (one for reading and one for writing). In some
|
|
|
|
* protocols more than 2 might be used (e.g. in DTLS for retransmitting
|
|
|
|
* messages from an earlier epoch).
|
2022-05-12 23:35:52 +08:00
|
|
|
*
|
|
|
|
* The created OSSL_RECORD_LAYER object is stored in *ret on success (or
|
|
|
|
* NULL otherwise). The return value will be one of
|
|
|
|
* OSSL_RECORD_RETURN_SUCCESS, OSSL_RECORD_RETURN_FATAL or
|
|
|
|
* OSSL_RECORD_RETURN_NON_FATAL. A non-fatal return means that creation of
|
|
|
|
* the record layer has failed because it is unsuitable, but an alternative
|
|
|
|
* record layer can be tried instead.
|
2022-04-07 19:30:30 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2022-07-20 21:49:53 +08:00
|
|
|
* If we eventually make this fetchable then we will need to use something
|
|
|
|
* other than EVP_CIPHER. Also mactype would not be a NID, but a string. For
|
|
|
|
* now though, this works.
|
2022-04-07 19:30:30 +08:00
|
|
|
*/
|
2022-05-12 23:35:52 +08:00
|
|
|
int (*new_record_layer)(OSSL_LIB_CTX *libctx,
|
|
|
|
const char *propq, int vers,
|
|
|
|
int role, int direction,
|
2022-06-08 21:52:44 +08:00
|
|
|
int level,
|
2022-07-27 21:37:27 +08:00
|
|
|
uint16_t epoch,
|
2022-06-08 21:52:44 +08:00
|
|
|
unsigned char *key,
|
2022-05-12 23:35:52 +08:00
|
|
|
size_t keylen,
|
|
|
|
unsigned char *iv,
|
|
|
|
size_t ivlen,
|
|
|
|
unsigned char *mackey,
|
|
|
|
size_t mackeylen,
|
|
|
|
const EVP_CIPHER *ciph,
|
|
|
|
size_t taglen,
|
|
|
|
int mactype,
|
|
|
|
const EVP_MD *md,
|
2022-09-15 23:03:02 +08:00
|
|
|
COMP_METHOD *comp,
|
2022-05-17 23:16:40 +08:00
|
|
|
BIO *prev,
|
|
|
|
BIO *transport,
|
|
|
|
BIO *next,
|
|
|
|
BIO_ADDR *local,
|
2022-05-12 23:35:52 +08:00
|
|
|
BIO_ADDR *peer,
|
|
|
|
const OSSL_PARAM *settings,
|
|
|
|
const OSSL_PARAM *options,
|
2022-05-25 22:16:48 +08:00
|
|
|
const OSSL_DISPATCH *fns,
|
|
|
|
void *cbarg,
|
2022-05-26 00:30:33 +08:00
|
|
|
OSSL_RECORD_LAYER **ret);
|
2022-05-17 23:16:40 +08:00
|
|
|
int (*free)(OSSL_RECORD_LAYER *rl);
|
2022-04-07 19:30:30 +08:00
|
|
|
|
2022-04-07 19:38:13 +08:00
|
|
|
int (*reset)(OSSL_RECORD_LAYER *rl); /* Is this needed? */
|
2022-04-07 19:30:30 +08:00
|
|
|
|
|
|
|
/* Returns 1 if we have unprocessed data buffered or 0 otherwise */
|
2022-04-07 19:38:13 +08:00
|
|
|
int (*unprocessed_read_pending)(OSSL_RECORD_LAYER *rl);
|
2022-07-27 21:20:23 +08:00
|
|
|
|
2022-04-07 19:30:30 +08:00
|
|
|
/*
|
|
|
|
* Returns 1 if we have processed data buffered that can be read or 0 otherwise
|
|
|
|
* - not necessarily app data
|
|
|
|
*/
|
2022-04-07 19:38:13 +08:00
|
|
|
int (*processed_read_pending)(OSSL_RECORD_LAYER *rl);
|
2022-04-07 19:30:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The amount of processed app data that is internally bufferred and
|
|
|
|
* available to read
|
|
|
|
*/
|
2022-04-07 19:38:13 +08:00
|
|
|
size_t (*app_data_pending)(OSSL_RECORD_LAYER *rl);
|
2022-04-07 19:30:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Find out the maximum number of records that the record layer is prepared
|
|
|
|
* to process in a single call to write_records. It is the caller's
|
|
|
|
* responsibility to ensure that no call to write_records exceeds this
|
2022-09-01 04:03:22 +08:00
|
|
|
* number of records. |type| is the type of the records that the caller
|
2022-09-09 20:26:50 +08:00
|
|
|
* wants to write, and |len| is the total amount of data that it wants
|
2022-09-01 04:03:22 +08:00
|
|
|
* to send. |maxfrag| is the maximum allowed fragment size based on user
|
|
|
|
* configuration, or TLS parameter negotiation. |*preffrag| contains on
|
|
|
|
* entry the default fragment size that will actually be used based on user
|
|
|
|
* configuration. This will always be less than or equal to |maxfrag|. On
|
|
|
|
* exit the record layer may update this to an alternative fragment size to
|
|
|
|
* be used. This must always be less than or equal to |maxfrag|.
|
2022-04-07 19:30:30 +08:00
|
|
|
*/
|
2022-09-09 20:26:50 +08:00
|
|
|
size_t (*get_max_records)(OSSL_RECORD_LAYER *rl, int type, size_t len,
|
2022-09-01 04:03:22 +08:00
|
|
|
size_t maxfrag, size_t *preffrag);
|
2022-04-07 19:30:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Write |numtempl| records from the array of record templates pointed to
|
|
|
|
* by |templates|. Each record should be no longer than the value returned
|
|
|
|
* by get_max_record_len(), and there should be no more records than the
|
|
|
|
* value returned by get_max_records().
|
|
|
|
* Where possible the caller will attempt to ensure that all records are the
|
|
|
|
* same length, except the last record. This may not always be possible so
|
|
|
|
* the record method implementation should not rely on this being the case.
|
|
|
|
* In the event of a retry the caller should call retry_write_records()
|
|
|
|
* to try again. No more calls to write_records() should be attempted until
|
|
|
|
* retry_write_records() returns success.
|
|
|
|
* Buffers allocated for the record templates can be freed immediately after
|
|
|
|
* write_records() returns - even in the case a retry.
|
|
|
|
* The record templates represent the plaintext payload. The encrypted
|
|
|
|
* output is written to the |transport| BIO.
|
|
|
|
* Returns:
|
|
|
|
* 1 on success
|
|
|
|
* 0 on retry
|
|
|
|
* -1 on failure
|
|
|
|
*/
|
2022-08-19 23:54:09 +08:00
|
|
|
int (*write_records)(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
|
|
|
|
size_t numtempl);
|
2022-04-07 19:30:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Retry a previous call to write_records. The caller should continue to
|
|
|
|
* call this until the function returns with success or failure. After
|
2022-08-19 23:54:09 +08:00
|
|
|
* each retry more of the data may have been incrementally sent.
|
2022-04-07 19:30:30 +08:00
|
|
|
* Returns:
|
|
|
|
* 1 on success
|
|
|
|
* 0 on retry
|
|
|
|
* -1 on failure
|
|
|
|
*/
|
2022-08-19 23:54:09 +08:00
|
|
|
int (*retry_write_records)(OSSL_RECORD_LAYER *rl);
|
2022-04-07 19:30:30 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a record and return the record layer version and record type in
|
|
|
|
* the |rversion| and |type| parameters. |*data| is set to point to a
|
|
|
|
* record layer buffer containing the record payload data and |*datalen|
|
|
|
|
* is filled in with the length of that data. The |epoch| and |seq_num|
|
|
|
|
* values are only used if DTLS has been negotiated. In that case they are
|
|
|
|
* filled in with the epoch and sequence number from the record.
|
|
|
|
* An opaque record layer handle for the record is returned in |*rechandle|
|
|
|
|
* which is used in a subsequent call to |release_record|. The buffer must
|
|
|
|
* remain available until release_record is called.
|
|
|
|
*
|
|
|
|
* Internally the the OSSL_RECORD_METHOD the implementation may read/process
|
|
|
|
* multiple records in one go and buffer them.
|
|
|
|
*/
|
2022-04-07 19:38:13 +08:00
|
|
|
int (*read_record)(OSSL_RECORD_LAYER *rl, void **rechandle, int *rversion,
|
|
|
|
int *type, unsigned char **data, size_t *datalen,
|
2022-05-26 00:30:33 +08:00
|
|
|
uint16_t *epoch, unsigned char *seq_num);
|
2022-04-07 19:30:30 +08:00
|
|
|
/*
|
|
|
|
* Release a buffer associated with a record previously read with
|
|
|
|
* read_record. Records are guaranteed to be released in the order that they
|
|
|
|
* are read.
|
|
|
|
*/
|
2022-04-12 21:50:28 +08:00
|
|
|
int (*release_record)(OSSL_RECORD_LAYER *rl, void *rechandle);
|
2022-04-07 19:30:30 +08:00
|
|
|
|
2022-04-08 00:35:36 +08:00
|
|
|
/*
|
|
|
|
* In the event that a fatal error is returned from the functions above then
|
|
|
|
* get_alert_code() can be called to obtain a more details identifier for
|
|
|
|
* the error. In (D)TLS this is the alert description code.
|
|
|
|
*/
|
|
|
|
int (*get_alert_code)(OSSL_RECORD_LAYER *rl);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Update the transport BIO from the one originally set in the
|
|
|
|
* new_record_layer call
|
|
|
|
*/
|
|
|
|
int (*set1_bio)(OSSL_RECORD_LAYER *rl, BIO *bio);
|
|
|
|
|
2022-05-13 00:21:25 +08:00
|
|
|
/* Called when protocol negotiation selects a protocol version to use */
|
|
|
|
int (*set_protocol_version)(OSSL_RECORD_LAYER *rl, int version);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Whether we are allowed to receive unencrypted alerts, even if we might
|
|
|
|
* otherwise expect encrypted records. Ignored by protocol versions where
|
|
|
|
* this isn't relevant
|
|
|
|
*/
|
|
|
|
void (*set_plain_alerts)(OSSL_RECORD_LAYER *rl, int allow);
|
|
|
|
|
|
|
|
/*
|
2022-06-24 23:45:14 +08:00
|
|
|
* Called immediately after creation of the record layer if we are in a
|
2022-05-13 00:21:25 +08:00
|
|
|
* first handshake. Also called at the end of the first handshake
|
|
|
|
*/
|
|
|
|
void (*set_first_handshake)(OSSL_RECORD_LAYER *rl, int first);
|
|
|
|
|
2022-05-26 00:30:33 +08:00
|
|
|
/*
|
|
|
|
* Set the maximum number of pipelines that the record layer should process.
|
|
|
|
* The default is 1.
|
|
|
|
*/
|
|
|
|
void (*set_max_pipelines)(OSSL_RECORD_LAYER *rl, size_t max_pipelines);
|
|
|
|
|
2022-06-24 23:45:14 +08:00
|
|
|
/*
|
|
|
|
* Called to tell the record layer whether we are currently "in init" or
|
|
|
|
* not. Default at creation of the record layer is "yes".
|
|
|
|
*/
|
|
|
|
void (*set_in_init)(OSSL_RECORD_LAYER *rl, int in_init);
|
2022-07-22 22:38:26 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Get a short or long human readable description of the record layer state
|
|
|
|
*/
|
|
|
|
void (*get_state)(OSSL_RECORD_LAYER *rl, const char **shortstr,
|
|
|
|
const char **longstr);
|
2022-07-26 21:34:38 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set new options or modify ones that were originaly specified in the
|
|
|
|
* new_record_layer call.
|
|
|
|
*/
|
|
|
|
int (*set_options)(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options);
|
2022-09-15 23:03:02 +08:00
|
|
|
|
|
|
|
const COMP_METHOD *(*get_compression)(OSSL_RECORD_LAYER *rl);
|
2022-09-23 23:53:23 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the maximum fragment length to be used for the record layer. This
|
|
|
|
* will override any previous value supplied for the "max_frag_len"
|
|
|
|
* setting during construction of the record layer.
|
|
|
|
*/
|
|
|
|
void (*set_max_frag_len)(OSSL_RECORD_LAYER *rl, size_t max_frag_len);
|
2022-10-14 22:30:55 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* The maximum expansion in bytes that the record layer might add while
|
|
|
|
* writing a record
|
|
|
|
*/
|
|
|
|
size_t (*get_max_record_overhead)(OSSL_RECORD_LAYER *rl);
|
2022-10-17 23:33:40 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Increment the record sequence number
|
|
|
|
*/
|
|
|
|
int (*increment_sequence_ctr)(OSSL_RECORD_LAYER *rl);
|
2022-10-21 23:12:31 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate read or write buffers. Does nothing if already allocated.
|
|
|
|
* Assumes default buffer length and 1 pipeline.
|
|
|
|
*/
|
|
|
|
int (*alloc_buffers)(OSSL_RECORD_LAYER *rl);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Free read or write buffers. Fails if there is pending read or write
|
|
|
|
* data. Buffers are automatically reallocated on next read/write.
|
|
|
|
*/
|
|
|
|
int (*free_buffers)(OSSL_RECORD_LAYER *rl);
|
2022-04-07 19:30:30 +08:00
|
|
|
};
|
2022-04-08 00:35:36 +08:00
|
|
|
|
|
|
|
|
|
|
|
/* Standard built-in record methods */
|
|
|
|
extern const OSSL_RECORD_METHOD ossl_tls_record_method;
|
2022-05-11 01:50:00 +08:00
|
|
|
# ifndef OPENSSL_NO_KTLS
|
|
|
|
extern const OSSL_RECORD_METHOD ossl_ktls_record_method;
|
|
|
|
# endif
|
2022-05-06 22:10:00 +08:00
|
|
|
extern const OSSL_RECORD_METHOD ossl_dtls_record_method;
|
|
|
|
|
|
|
|
#endif /* !defined(OSSL_INTERNAL_RECORDMETHOD_H) */
|