recordmethod.h 14 KB

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