1
0

recordmethod.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright 2022-2025 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. #ifndef OSSL_INTERNAL_RECORDMETHOD_H
  10. # define OSSL_INTERNAL_RECORDMETHOD_H
  11. # pragma once
  12. # include <openssl/ssl.h>
  13. /*
  14. * We use the term "record" here to refer to a packet of data. Records are
  15. * typically protected via a cipher and MAC, or an AEAD cipher (although not
  16. * always). This usage of the term record is consistent with the TLS concept.
  17. * In QUIC the term "record" is not used but it is analogous to the QUIC term
  18. * "packet". The interface in this file applies to all protocols that protect
  19. * records/packets of data, i.e. (D)TLS and QUIC. The term record is used to
  20. * refer to both contexts.
  21. */
  22. /*
  23. * An OSSL_RECORD_METHOD is a protocol specific method which provides the
  24. * functions for reading and writing records for that protocol. Which
  25. * OSSL_RECORD_METHOD to use for a given protocol is defined by the SSL_METHOD.
  26. */
  27. typedef struct ossl_record_method_st OSSL_RECORD_METHOD;
  28. /*
  29. * An OSSL_RECORD_LAYER is just an externally defined opaque pointer created by
  30. * the method
  31. */
  32. typedef struct ossl_record_layer_st OSSL_RECORD_LAYER;
  33. # define OSSL_RECORD_ROLE_CLIENT 0
  34. # define OSSL_RECORD_ROLE_SERVER 1
  35. # define OSSL_RECORD_DIRECTION_READ 0
  36. # define OSSL_RECORD_DIRECTION_WRITE 1
  37. # define OSSL_RECORD_RETURN_SUCCESS 1
  38. # define OSSL_RECORD_RETURN_RETRY 0
  39. # define OSSL_RECORD_RETURN_NON_FATAL_ERR -1
  40. # define OSSL_RECORD_RETURN_FATAL -2
  41. # define OSSL_RECORD_RETURN_EOF -3
  42. /*
  43. * Template for creating a record. A record consists of the |type| of data it
  44. * will contain (e.g. alert, handshake, application data, etc) along with a
  45. * buffer of payload data in |buf| of length |buflen|.
  46. */
  47. struct ossl_record_template_st {
  48. unsigned char type;
  49. unsigned int version;
  50. const unsigned char *buf;
  51. size_t buflen;
  52. };
  53. typedef struct ossl_record_template_st OSSL_RECORD_TEMPLATE;
  54. /*
  55. * Rather than a "method" approach, we could make this fetchable - Should we?
  56. * There could be some complexity in finding suitable record layer implementations
  57. * e.g. we need to find one that matches the negotiated protocol, cipher,
  58. * extensions, etc. The selection_cb approach given above doesn't work so well
  59. * if unknown third party providers with OSSL_RECORD_METHOD implementations are
  60. * loaded.
  61. */
  62. /*
  63. * If this becomes public API then we will need functions to create and
  64. * free an OSSL_RECORD_METHOD, as well as functions to get/set the various
  65. * function pointers....unless we make it fetchable.
  66. */
  67. struct ossl_record_method_st {
  68. /*
  69. * Create a new OSSL_RECORD_LAYER object for handling the protocol version
  70. * set by |vers|. |role| is 0 for client and 1 for server. |direction|
  71. * indicates either read or write. |level| is the protection level as
  72. * described above. |settings| are mandatory settings that will cause the
  73. * new() call to fail if they are not understood (for example to require
  74. * Encrypt-Then-Mac support). |options| are optional settings that will not
  75. * cause the new() call to fail if they are not understood (for example
  76. * whether to use "read ahead" or not).
  77. *
  78. * The BIO in |transport| is the BIO for the underlying transport layer.
  79. * Where the direction is "read", then this BIO will only ever be used for
  80. * reading data. Where the direction is "write", then this BIO will only
  81. * every be used for writing data.
  82. *
  83. * An SSL object will always have at least 2 OSSL_RECORD_LAYER objects in
  84. * force at any one time (one for reading and one for writing). In some
  85. * protocols more than 2 might be used (e.g. in DTLS for retransmitting
  86. * messages from an earlier epoch).
  87. *
  88. * The created OSSL_RECORD_LAYER object is stored in *ret on success (or
  89. * NULL otherwise). The return value will be one of
  90. * OSSL_RECORD_RETURN_SUCCESS, OSSL_RECORD_RETURN_FATAL or
  91. * OSSL_RECORD_RETURN_NON_FATAL. A non-fatal return means that creation of
  92. * the record layer has failed because it is unsuitable, but an alternative
  93. * record layer can be tried instead.
  94. */
  95. /*
  96. * If we eventually make this fetchable then we will need to use something
  97. * other than EVP_CIPHER. Also mactype would not be a NID, but a string. For
  98. * now though, this works.
  99. */
  100. int (*new_record_layer)(OSSL_LIB_CTX *libctx,
  101. const char *propq, int vers,
  102. int role, int direction,
  103. int level,
  104. uint16_t epoch,
  105. unsigned char *secret,
  106. size_t secretlen,
  107. unsigned char *key,
  108. size_t keylen,
  109. unsigned char *iv,
  110. size_t ivlen,
  111. unsigned char *mackey,
  112. size_t mackeylen,
  113. const EVP_CIPHER *ciph,
  114. size_t taglen,
  115. int mactype,
  116. const EVP_MD *md,
  117. COMP_METHOD *comp,
  118. const EVP_MD *kdfdigest,
  119. BIO *prev,
  120. BIO *transport,
  121. BIO *next,
  122. BIO_ADDR *local,
  123. BIO_ADDR *peer,
  124. const OSSL_PARAM *settings,
  125. const OSSL_PARAM *options,
  126. const OSSL_DISPATCH *fns,
  127. void *cbarg,
  128. void *rlarg,
  129. OSSL_RECORD_LAYER **ret);
  130. int (*free)(OSSL_RECORD_LAYER *rl);
  131. /* Returns 1 if we have unprocessed data buffered or 0 otherwise */
  132. int (*unprocessed_read_pending)(OSSL_RECORD_LAYER *rl);
  133. /*
  134. * Returns 1 if we have processed data buffered that can be read or 0 otherwise
  135. * - not necessarily app data
  136. */
  137. int (*processed_read_pending)(OSSL_RECORD_LAYER *rl);
  138. /*
  139. * The amount of processed app data that is internally buffered and
  140. * available to read
  141. */
  142. size_t (*app_data_pending)(OSSL_RECORD_LAYER *rl);
  143. /*
  144. * Find out the maximum number of records that the record layer is prepared
  145. * to process in a single call to write_records. It is the caller's
  146. * responsibility to ensure that no call to write_records exceeds this
  147. * number of records. |type| is the type of the records that the caller
  148. * wants to write, and |len| is the total amount of data that it wants
  149. * to send. |maxfrag| is the maximum allowed fragment size based on user
  150. * configuration, or TLS parameter negotiation. |*preffrag| contains on
  151. * entry the default fragment size that will actually be used based on user
  152. * configuration. This will always be less than or equal to |maxfrag|. On
  153. * exit the record layer may update this to an alternative fragment size to
  154. * be used. This must always be less than or equal to |maxfrag|.
  155. */
  156. size_t (*get_max_records)(OSSL_RECORD_LAYER *rl, uint8_t type, size_t len,
  157. size_t maxfrag, size_t *preffrag);
  158. /*
  159. * Write |numtempl| records from the array of record templates pointed to
  160. * by |templates|. Each record should be no longer than the value returned
  161. * by get_max_record_len(), and there should be no more records than the
  162. * value returned by get_max_records().
  163. * Where possible the caller will attempt to ensure that all records are the
  164. * same length, except the last record. This may not always be possible so
  165. * the record method implementation should not rely on this being the case.
  166. * In the event of a retry the caller should call retry_write_records()
  167. * to try again. No more calls to write_records() should be attempted until
  168. * retry_write_records() returns success.
  169. * Buffers allocated for the record templates can be freed immediately after
  170. * write_records() returns - even in the case a retry.
  171. * The record templates represent the plaintext payload. The encrypted
  172. * output is written to the |transport| BIO.
  173. * Returns:
  174. * 1 on success
  175. * 0 on retry
  176. * -1 on failure
  177. */
  178. int (*write_records)(OSSL_RECORD_LAYER *rl, OSSL_RECORD_TEMPLATE *templates,
  179. size_t numtempl);
  180. /*
  181. * Retry a previous call to write_records. The caller should continue to
  182. * call this until the function returns with success or failure. After
  183. * each retry more of the data may have been incrementally sent.
  184. * Returns:
  185. * 1 on success
  186. * 0 on retry
  187. * -1 on failure
  188. */
  189. int (*retry_write_records)(OSSL_RECORD_LAYER *rl);
  190. /*
  191. * Read a record and return the record layer version and record type in
  192. * the |rversion| and |type| parameters. |*data| is set to point to a
  193. * record layer buffer containing the record payload data and |*datalen|
  194. * is filled in with the length of that data. The |epoch| and |seq_num|
  195. * values are only used if DTLS has been negotiated. In that case they are
  196. * filled in with the epoch and sequence number from the record.
  197. * An opaque record layer handle for the record is returned in |*rechandle|
  198. * which is used in a subsequent call to |release_record|. The buffer must
  199. * remain available until all the bytes from record are released via one or
  200. * more release_record calls.
  201. *
  202. * Internally the OSSL_RECORD_METHOD implementation may read/process
  203. * multiple records in one go and buffer them.
  204. */
  205. int (*read_record)(OSSL_RECORD_LAYER *rl, void **rechandle, int *rversion,
  206. uint8_t *type, const unsigned char **data, size_t *datalen,
  207. uint16_t *epoch, unsigned char *seq_num);
  208. /*
  209. * Release length bytes from a buffer associated with a record previously
  210. * read with read_record. Once all the bytes from a record are released, the
  211. * whole record and its associated buffer is released. Records are
  212. * guaranteed to be released in the order that they are read.
  213. */
  214. int (*release_record)(OSSL_RECORD_LAYER *rl, void *rechandle, size_t length);
  215. /*
  216. * In the event that a fatal error is returned from the functions above then
  217. * get_alert_code() can be called to obtain a more details identifier for
  218. * the error. In (D)TLS this is the alert description code.
  219. */
  220. int (*get_alert_code)(OSSL_RECORD_LAYER *rl);
  221. /*
  222. * Update the transport BIO from the one originally set in the
  223. * new_record_layer call
  224. */
  225. int (*set1_bio)(OSSL_RECORD_LAYER *rl, BIO *bio);
  226. /* Called when protocol negotiation selects a protocol version to use */
  227. int (*set_protocol_version)(OSSL_RECORD_LAYER *rl, int version);
  228. /*
  229. * Whether we are allowed to receive unencrypted alerts, even if we might
  230. * otherwise expect encrypted records. Ignored by protocol versions where
  231. * this isn't relevant
  232. */
  233. void (*set_plain_alerts)(OSSL_RECORD_LAYER *rl, int allow);
  234. /*
  235. * Called immediately after creation of the record layer if we are in a
  236. * first handshake. Also called at the end of the first handshake
  237. */
  238. void (*set_first_handshake)(OSSL_RECORD_LAYER *rl, int first);
  239. /*
  240. * Set the maximum number of pipelines that the record layer should process.
  241. * The default is 1.
  242. */
  243. void (*set_max_pipelines)(OSSL_RECORD_LAYER *rl, size_t max_pipelines);
  244. /*
  245. * Called to tell the record layer whether we are currently "in init" or
  246. * not. Default at creation of the record layer is "yes".
  247. */
  248. void (*set_in_init)(OSSL_RECORD_LAYER *rl, int in_init);
  249. /*
  250. * Get a short or long human readable description of the record layer state
  251. */
  252. void (*get_state)(OSSL_RECORD_LAYER *rl, const char **shortstr,
  253. const char **longstr);
  254. /*
  255. * Set new options or modify ones that were originally specified in the
  256. * new_record_layer call.
  257. */
  258. int (*set_options)(OSSL_RECORD_LAYER *rl, const OSSL_PARAM *options);
  259. const COMP_METHOD *(*get_compression)(OSSL_RECORD_LAYER *rl);
  260. /*
  261. * Set the maximum fragment length to be used for the record layer. This
  262. * will override any previous value supplied for the "max_frag_len"
  263. * setting during construction of the record layer.
  264. */
  265. void (*set_max_frag_len)(OSSL_RECORD_LAYER *rl, size_t max_frag_len);
  266. /*
  267. * The maximum expansion in bytes that the record layer might add while
  268. * writing a record
  269. */
  270. size_t (*get_max_record_overhead)(OSSL_RECORD_LAYER *rl);
  271. /*
  272. * Increment the record sequence number
  273. */
  274. int (*increment_sequence_ctr)(OSSL_RECORD_LAYER *rl);
  275. /*
  276. * Allocate read or write buffers. Does nothing if already allocated.
  277. * Assumes default buffer length and 1 pipeline.
  278. */
  279. int (*alloc_buffers)(OSSL_RECORD_LAYER *rl);
  280. /*
  281. * Free read or write buffers. Fails if there is pending read or write
  282. * data. Buffers are automatically reallocated on next read/write.
  283. */
  284. int (*free_buffers)(OSSL_RECORD_LAYER *rl);
  285. };
  286. /* Standard built-in record methods */
  287. extern const OSSL_RECORD_METHOD ossl_tls_record_method;
  288. # ifndef OPENSSL_NO_KTLS
  289. extern const OSSL_RECORD_METHOD ossl_ktls_record_method;
  290. # endif
  291. extern const OSSL_RECORD_METHOD ossl_dtls_record_method;
  292. #endif /* !defined(OSSL_INTERNAL_RECORDMETHOD_H) */