s2n.h 150 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396
  1. /*
  2. * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License").
  5. * You may not use this file except in compliance with the License.
  6. * A copy of the License is located at
  7. *
  8. * http://aws.amazon.com/apache2.0
  9. *
  10. * or in the "license" file accompanying this file. This file is distributed
  11. * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
  12. * express or implied. See the License for the specific language governing
  13. * permissions and limitations under the License.
  14. */
  15. /**
  16. * @file s2n.h
  17. * s2n-tls is a C99 implementation of the TLS/SSL protocols that is designed to
  18. * be simple, small, fast, and with security as a priority. <br> It is released and
  19. * licensed under the Apache License 2.0.
  20. */
  21. #pragma once
  22. #if ((__GNUC__ >= 4) || defined(__clang__)) && defined(S2N_EXPORTS)
  23. /**
  24. * Marks a function as belonging to the public s2n API.
  25. */
  26. #define S2N_API __attribute__((visibility("default")))
  27. #else
  28. /**
  29. * Marks a function as belonging to the public s2n API.
  30. */
  31. #define S2N_API
  32. #endif /* __GNUC__ >= 4 || defined(__clang__) */
  33. #ifdef __cplusplus
  34. extern "C" {
  35. #endif
  36. #include <stdbool.h>
  37. #include <stdint.h>
  38. #include <stdio.h>
  39. #include <sys/types.h>
  40. #include <sys/uio.h>
  41. /**
  42. * Function return code
  43. */
  44. #define S2N_SUCCESS 0
  45. /**
  46. * Function return code
  47. */
  48. #define S2N_FAILURE -1
  49. /**
  50. * Callback return code
  51. */
  52. #define S2N_CALLBACK_BLOCKED -2
  53. /**
  54. * s2n minimum supported TLS record major version
  55. */
  56. #define S2N_MINIMUM_SUPPORTED_TLS_RECORD_MAJOR_VERSION 2
  57. /**
  58. * s2n maximum supported TLS record major version
  59. */
  60. #define S2N_MAXIMUM_SUPPORTED_TLS_RECORD_MAJOR_VERSION 3
  61. /**
  62. * s2n SSL 2.0 Version Constant
  63. */
  64. #define S2N_SSLv2 20
  65. /**
  66. * s2n SSL 3.0 Version Constant
  67. */
  68. #define S2N_SSLv3 30
  69. /**
  70. * s2n TLS 1.0 Version Constant
  71. */
  72. #define S2N_TLS10 31
  73. /**
  74. * s2n TLS 1.1 Version Constant
  75. */
  76. #define S2N_TLS11 32
  77. /**
  78. * s2n TLS 1.2 Version Constant
  79. */
  80. #define S2N_TLS12 33
  81. /**
  82. * s2n TLS 1.3 Version Constant
  83. */
  84. #define S2N_TLS13 34
  85. /**
  86. * s2n Unknown TLS Version
  87. */
  88. #define S2N_UNKNOWN_PROTOCOL_VERSION 0
  89. /**
  90. * s2n-tls functions that return 'int' return 0 to indicate success and -1 to indicate
  91. * failure.
  92. *
  93. * s2n-tls functions that return pointer types return NULL in the case of
  94. * failure.
  95. *
  96. * When an s2n-tls function returns a failure, s2n_errno will be set to a value
  97. * corresponding to the error. This error value can be translated into a string
  98. * explaining the error in English by calling s2n_strerror(s2n_errno, "EN").
  99. * A string containing human readable error name; can be generated with `s2n_strerror_name`.
  100. * A string containing internal debug information, including filename and line number, can be generated with `s2n_strerror_debug`.
  101. * A string containing only the filename and line number can be generated with `s2n_strerror_source`.
  102. * This string is useful to include when reporting issues to the s2n-tls development team.
  103. *
  104. * @warning To avoid possible confusion, s2n_errno should be cleared after processing an error: `s2n_errno = S2N_ERR_T_OK`
  105. */
  106. S2N_API extern __thread int s2n_errno;
  107. /**
  108. * This function can be used instead of trying to resolve `s2n_errno` directly
  109. * in runtimes where thread-local variables may not be easily accessible.
  110. *
  111. * @returns The address of the thread-local `s2n_errno` variable
  112. */
  113. S2N_API extern int *s2n_errno_location(void);
  114. /**
  115. * Used to help applications determine why an s2n-tls function failed.
  116. *
  117. * This enum is optimized for use in C switch statements. Each value in the enum represents
  118. * an error "category".
  119. *
  120. * s2n-tls organizes errors into different "types" to allow applications to handle error
  121. * values without catching all possibilities. Applications using non-blocking I/O should check
  122. * the error type to determine if the I/O operation failed because it would block or for some other
  123. * error. To retrieve the type for a given error use `s2n_error_get_type()`. Applications should
  124. * perform any error handling logic using these high level types.
  125. *
  126. * See the [Error Handling](https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md#error-handling) section for how the errors should be interpreted.
  127. */
  128. typedef enum {
  129. /** No error */
  130. S2N_ERR_T_OK = 0,
  131. /** Underlying I/O operation failed, check system errno */
  132. S2N_ERR_T_IO,
  133. /** EOF */
  134. S2N_ERR_T_CLOSED,
  135. /** Underlying I/O operation would block */
  136. S2N_ERR_T_BLOCKED,
  137. /** Incoming Alert */
  138. S2N_ERR_T_ALERT,
  139. /** Failure in some part of the TLS protocol. Ex: CBC verification failure */
  140. S2N_ERR_T_PROTO,
  141. /** Error internal to s2n-tls. A precondition could have failed. */
  142. S2N_ERR_T_INTERNAL,
  143. /** User input error. Ex: Providing an invalid cipher preference version */
  144. S2N_ERR_T_USAGE
  145. } s2n_error_type;
  146. /**
  147. * Gets the category of error from an error.
  148. *
  149. * s2n-tls organizes errors into different "types" to allow applications to do logic on error values without catching all possibilities.
  150. * Applications using non-blocking I/O should check error type to determine if the I/O operation failed because
  151. * it would block or for some other error.
  152. *
  153. * @param error The error from s2n. Usually this is `s2n_errno`.
  154. * @returns An s2n_error_type
  155. */
  156. S2N_API extern int s2n_error_get_type(int error);
  157. /**
  158. * An opaque configuration object, used by clients and servers for holding cryptographic certificates, keys and preferences.
  159. */
  160. struct s2n_config;
  161. /**
  162. * An opaque connection. Used to track each s2n connection.
  163. */
  164. struct s2n_connection;
  165. /**
  166. * Prevents S2N from calling `OPENSSL_init_crypto`/`OPENSSL_cleanup`/`EVP_cleanup` on OpenSSL versions
  167. * prior to 1.1.x. This allows applications or languages that also init OpenSSL to interoperate
  168. * with S2N.
  169. *
  170. * @warning This function must be called BEFORE s2n_init() to have any effect. It will return an error
  171. * if s2n is already initialized.
  172. *
  173. * @note If you disable this and are using a version of OpenSSL/libcrypto < 1.1.x, you will
  174. * be responsible for library init and cleanup (specifically `OPENSSL_add_all_algorithms()`
  175. * or `OPENSSL_init_crypto()`, and EVP_* APIs will not be usable unless the library is initialized.
  176. *
  177. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  178. */
  179. S2N_API extern int s2n_crypto_disable_init(void);
  180. /**
  181. * Prevents S2N from installing an atexit handler, which allows safe shutdown of S2N from within a
  182. * re-entrant shared library
  183. *
  184. * @warning This function must be called BEFORE s2n_init() to have any effect. It will return an error
  185. * if s2n is already initialized.
  186. *
  187. * @note This will cause `s2n_cleanup` to do complete cleanup of s2n-tls when called from the main
  188. * thread (the thread `s2n_init` was called from).
  189. *
  190. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  191. */
  192. S2N_API extern int s2n_disable_atexit(void);
  193. /**
  194. * Fetches the OpenSSL version s2n-tls was compiled with. This can be used by applications to validate at runtime
  195. * that the versions of s2n-tls and Openssl that they have loaded are correct.
  196. *
  197. * @returns the version number of OpenSSL that s2n-tls was compiled with
  198. */
  199. S2N_API extern unsigned long s2n_get_openssl_version(void);
  200. /**
  201. * Initializes the s2n-tls library and should be called once in your application, before any other s2n-tls
  202. * functions are called. Failure to call s2n_init() will result in errors from other s2n-tls functions.
  203. *
  204. * @warning This function is not thread safe and should only be called once.
  205. *
  206. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  207. */
  208. S2N_API extern int s2n_init(void);
  209. /**
  210. * Cleans up any internal resources used by s2n-tls. This function should be called from each thread or process
  211. * that is created subsequent to calling `s2n_init` when that thread or process is done calling other s2n-tls functions.
  212. *
  213. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  214. */
  215. S2N_API extern int s2n_cleanup(void);
  216. /**
  217. * Creates a new s2n_config object. This object can (and should) be associated with many connection
  218. * objects.
  219. *
  220. * The returned config will be initialized with default system certificates in its trust store.
  221. *
  222. * The returned config should be freed with `s2n_config_free()` after it's no longer in use by any
  223. * connection.
  224. *
  225. * @returns A new configuration object suitable for configuring connections and associating certs
  226. * and keys.
  227. */
  228. S2N_API extern struct s2n_config *s2n_config_new(void);
  229. /**
  230. * Creates a new s2n_config object with minimal default options.
  231. *
  232. * This function has better performance than `s2n_config_new()` because it does not load default
  233. * system certificates into the trust store by default. To add system certificates to this config,
  234. * call `s2n_config_load_system_certs()`.
  235. *
  236. * The returned config should be freed with `s2n_config_free()` after it's no longer in use by any
  237. * connection.
  238. *
  239. * @returns A new configuration object suitable for configuring connections and associating certs
  240. * and keys.
  241. */
  242. S2N_API extern struct s2n_config *s2n_config_new_minimal(void);
  243. /**
  244. * Frees the memory associated with an `s2n_config` object.
  245. *
  246. * @param config The configuration object being freed
  247. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  248. */
  249. S2N_API extern int s2n_config_free(struct s2n_config *config);
  250. /**
  251. * Frees the DH params associated with an `s2n_config` object.
  252. *
  253. * @param config The configuration object with DH params being freed
  254. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  255. */
  256. S2N_API extern int s2n_config_free_dhparams(struct s2n_config *config);
  257. /**
  258. * Frees the certificate chain and key associated with an `s2n_config` object.
  259. *
  260. * @param config The configuration object with DH params being freed
  261. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  262. */
  263. S2N_API extern int s2n_config_free_cert_chain_and_key(struct s2n_config *config);
  264. /**
  265. * Callback function type used to get the system time.
  266. *
  267. * @param void* A pointer to arbitrary data for use within the callback
  268. * @param uint64_t* A pointer that the callback will set to the time in nanoseconds
  269. * The function should return 0 on success and -1 on failure.
  270. */
  271. typedef int (*s2n_clock_time_nanoseconds)(void *, uint64_t *);
  272. /**
  273. * Cache callback function that allows the caller to retrieve SSL session data
  274. * from a cache.
  275. *
  276. * The callback function takes six arguments:
  277. * a pointer to the s2n_connection object,
  278. * a pointer to arbitrary data for use within the callback,
  279. * a pointer to a key which can be used to retrieve the cached entry,
  280. * a 64 bit unsigned integer specifying the size of this key,
  281. * a pointer to a memory location where the value should be stored,
  282. * and a pointer to a 64 bit unsigned integer specifying the size of this value.
  283. *
  284. * Initially *value_size will be set to the amount of space allocated for the value,
  285. * the callback should set *value_size to the actual size of the data returned.
  286. * If there is insufficient space, -1 should be returned.
  287. * If the cache is not ready to provide data for the request,
  288. * S2N_CALLBACK_BLOCKED should be returned.
  289. *
  290. * This will cause s2n_negotiate() to return S2N_BLOCKED_ON_APPLICATION_INPUT.
  291. */
  292. typedef int (*s2n_cache_retrieve_callback)(struct s2n_connection *conn, void *, const void *key, uint64_t key_size, void *value, uint64_t *value_size);
  293. /**
  294. * Cache callback function that allows the caller to store SSL session data in a
  295. * cache.
  296. *
  297. * The callback function takes seven arguments:
  298. * a pointer to the s2n_connection object,
  299. * a pointer to arbitrary data for use within the callback,
  300. * a 64-bit unsigned integer specifying the number of seconds the session data may be stored for,
  301. * a pointer to a key which can be used to retrieve the cached entry,
  302. * a 64 bit unsigned integer specifying the size of this key,
  303. * a pointer to a value which should be stored,
  304. * and a 64 bit unsigned integer specified the size of this value.
  305. */
  306. typedef int (*s2n_cache_store_callback)(struct s2n_connection *conn, void *, uint64_t ttl_in_seconds, const void *key, uint64_t key_size, const void *value, uint64_t value_size);
  307. /**
  308. * Cache callback function that allows the caller to set a callback function
  309. * that will be used to delete SSL session data from a cache.
  310. *
  311. * The callback function takes four arguments:
  312. * a pointer to s2n_connection object,
  313. * a pointer to arbitrary data for use within the callback,
  314. * a pointer to a key which can be used to delete the cached entry,
  315. * and a 64 bit unsigned integer specifying the size of this key.
  316. */
  317. typedef int (*s2n_cache_delete_callback)(struct s2n_connection *conn, void *, const void *key, uint64_t key_size);
  318. /**
  319. * Allows the caller to set a callback function that will be used to get the
  320. * system time. The time returned should be the number of nanoseconds since the
  321. * Unix epoch (Midnight, January 1st, 1970).
  322. *
  323. * s2n-tls uses this clock for timestamps.
  324. *
  325. * @param config The configuration object being updated
  326. * @param clock_fn The wall clock time callback function
  327. * @param ctx An opaque pointer that the callback will be invoked with
  328. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  329. */
  330. S2N_API extern int s2n_config_set_wall_clock(struct s2n_config *config, s2n_clock_time_nanoseconds clock_fn, void *ctx);
  331. /**
  332. * Allows the caller to set a callback function that will be used to get
  333. * monotonic time. The monotonic time is the time since an arbitrary, unspecified
  334. * point. Unlike wall clock time, it MUST never move backwards.
  335. *
  336. * s2n-tls uses this clock for timers.
  337. *
  338. * @param config The configuration object being updated
  339. * @param clock_fn The monotonic time callback function
  340. * @param ctx An opaque pointer that the callback will be invoked with
  341. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  342. */
  343. S2N_API extern int s2n_config_set_monotonic_clock(struct s2n_config *config, s2n_clock_time_nanoseconds clock_fn, void *ctx);
  344. /**
  345. * Translates an s2n_error code to a human readable string explaining the error.
  346. *
  347. * @param error The error code to explain. Usually this is s2n_errno
  348. * @param lang The language to explain the error code. Pass "EN" or NULL for English.
  349. * @returns The error string
  350. */
  351. S2N_API extern const char *s2n_strerror(int error, const char *lang);
  352. /**
  353. * Translates an s2n_error code to a human readable string containing internal debug
  354. * information, including file name and line number. This function is useful when
  355. * reporting issues to the s2n-tls development team.
  356. *
  357. * @param error The error code to explain. Usually this is s2n_errno
  358. * @param lang The language to explain the error code. Pass "EN" or NULL for English.
  359. * @returns The error string
  360. */
  361. S2N_API extern const char *s2n_strerror_debug(int error, const char *lang);
  362. /**
  363. * Translates an s2n_error code to a human readable string.
  364. *
  365. * @param error The error code to explain. Usually this is s2n_errno
  366. * @returns The error string
  367. */
  368. S2N_API extern const char *s2n_strerror_name(int error);
  369. /**
  370. * Translates an s2n_error code to a filename and line number.
  371. *
  372. * @param error The error code to explain. Usually this is s2n_errno.
  373. * @returns The error string.
  374. */
  375. S2N_API extern const char *s2n_strerror_source(int error);
  376. /**
  377. * Opaque stack trace structure.
  378. */
  379. struct s2n_stacktrace;
  380. /**
  381. * Checks if s2n stack trace captures are enabled.
  382. *
  383. * @returns True if stack traces are enabled. False if they are disabled.
  384. */
  385. S2N_API extern bool s2n_stack_traces_enabled(void);
  386. /**
  387. * Configures the s2n stack trace captures option.
  388. *
  389. * @param newval Boolean to determine if stack traces should be enabled. True to enable them. False to disable them.
  390. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  391. */
  392. S2N_API extern int s2n_stack_traces_enabled_set(bool newval);
  393. /**
  394. * Calculates the s2n stack trace.
  395. *
  396. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  397. */
  398. S2N_API extern int s2n_calculate_stacktrace(void);
  399. /**
  400. * Prints the s2n stack trace to a file. The file descriptor is expected to be
  401. * open and ready for writing.
  402. *
  403. * @param fptr A pointer to the file s2n-tls should write the stack trace to.
  404. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  405. */
  406. S2N_API extern int s2n_print_stacktrace(FILE *fptr);
  407. /**
  408. * Clean up the memory used to contain the stack trace.
  409. *
  410. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  411. */
  412. S2N_API extern int s2n_free_stacktrace(void);
  413. /**
  414. * Export the s2n_stacktrace.
  415. *
  416. * @param trace A pointer to the s2n_stacktrace to fill.
  417. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  418. */
  419. S2N_API extern int s2n_get_stacktrace(struct s2n_stacktrace *trace);
  420. /**
  421. * Allows the caller to set a callback function that will be used to store SSL
  422. * session data in a cache.
  423. *
  424. * @param config The configuration object being updated
  425. * @param cache_store_callback The cache store callback function.
  426. * @param data An opaque context pointer that the callback will be invoked with.
  427. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  428. */
  429. S2N_API extern int s2n_config_set_cache_store_callback(struct s2n_config *config, s2n_cache_store_callback cache_store_callback, void *data);
  430. /**
  431. * Allows the caller to set a callback function that will be used to retrieve SSL
  432. * session data from a cache.
  433. *
  434. * @param config The configuration object being updated
  435. * @param cache_retrieve_callback The cache retrieve callback function.
  436. * @param data An opaque context pointer that the callback will be invoked with.
  437. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  438. */
  439. S2N_API extern int s2n_config_set_cache_retrieve_callback(struct s2n_config *config, s2n_cache_retrieve_callback cache_retrieve_callback, void *data);
  440. /**
  441. * Allows the caller to set a callback function that will be used to delete SSL
  442. * session data from a cache.
  443. *
  444. * @param config The configuration object being updated
  445. * @param cache_delete_callback The cache delete callback function.
  446. * @param data An opaque context pointer that the callback will be invoked with.
  447. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  448. */
  449. S2N_API extern int s2n_config_set_cache_delete_callback(struct s2n_config *config, s2n_cache_delete_callback cache_delete_callback, void *data);
  450. /**
  451. * Called when `s2n_init` is executed.
  452. */
  453. typedef int (*s2n_mem_init_callback)(void);
  454. /**
  455. * Will be called when `s2n_cleanup` is executed.
  456. */
  457. typedef int (*s2n_mem_cleanup_callback)(void);
  458. /**
  459. * A function that can allocate at least `requested` bytes of memory.
  460. *
  461. * It stores the location of that memory in **\*ptr** and the size of the allocated
  462. * data in **\*allocated**. The function may choose to allocate more memory
  463. * than was requested. s2n-tls will consider all allocated memory available for
  464. * use, and will attempt to free all allocated memory when able.
  465. */
  466. typedef int (*s2n_mem_malloc_callback)(void **ptr, uint32_t requested, uint32_t *allocated);
  467. /**
  468. * Frees memory allocated by s2n_mem_malloc_callback.
  469. */
  470. typedef int (*s2n_mem_free_callback)(void *ptr, uint32_t size);
  471. /**
  472. * Allows the caller to override s2n-tls's internal memory handling functions.
  473. *
  474. * @warning This function must be called before s2n_init().
  475. *
  476. * @param mem_init_callback The s2n_mem_init_callback
  477. * @param mem_cleanup_callback The s2n_mem_cleanup_callback
  478. * @param mem_malloc_callback The s2n_mem_malloc_callback
  479. * @param mem_free_callback The s2n_mem_free_callback
  480. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  481. */
  482. S2N_API extern int s2n_mem_set_callbacks(s2n_mem_init_callback mem_init_callback, s2n_mem_cleanup_callback mem_cleanup_callback,
  483. s2n_mem_malloc_callback mem_malloc_callback, s2n_mem_free_callback mem_free_callback);
  484. /**
  485. * A callback function that will be called when s2n-tls is initialized.
  486. */
  487. typedef int (*s2n_rand_init_callback)(void);
  488. /**
  489. * A callback function that will be called when `s2n_cleanup` is executed.
  490. */
  491. typedef int (*s2n_rand_cleanup_callback)(void);
  492. /**
  493. * A callback function that will be used to provide entropy to the s2n-tls
  494. * random number generators.
  495. */
  496. typedef int (*s2n_rand_seed_callback)(void *data, uint32_t size);
  497. /**
  498. * A callback function that will be used to mix in entropy every time the RNG
  499. * is invoked.
  500. */
  501. typedef int (*s2n_rand_mix_callback)(void *data, uint32_t size);
  502. /**
  503. * Allows the caller to override s2n-tls's entropy functions.
  504. *
  505. * @warning This function must be called before s2n_init().
  506. *
  507. * @note The overriden random callbacks will not be used when s2n-tls is operating in FIPS mode.
  508. *
  509. * @param rand_init_callback The s2n_rand_init_callback
  510. * @param rand_cleanup_callback The s2n_rand_cleanup_callback
  511. * @param rand_seed_callback The s2n_rand_seed_callback
  512. * @param rand_mix_callback The s2n_rand_mix_callback
  513. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  514. */
  515. S2N_API extern int s2n_rand_set_callbacks(s2n_rand_init_callback rand_init_callback, s2n_rand_cleanup_callback rand_cleanup_callback,
  516. s2n_rand_seed_callback rand_seed_callback, s2n_rand_mix_callback rand_mix_callback);
  517. /**
  518. * TLS extensions supported by s2n-tls
  519. */
  520. typedef enum {
  521. S2N_EXTENSION_SERVER_NAME = 0,
  522. S2N_EXTENSION_MAX_FRAG_LEN = 1,
  523. S2N_EXTENSION_OCSP_STAPLING = 5,
  524. S2N_EXTENSION_SUPPORTED_GROUPS = 10,
  525. S2N_EXTENSION_EC_POINT_FORMATS = 11,
  526. S2N_EXTENSION_SIGNATURE_ALGORITHMS = 13,
  527. S2N_EXTENSION_ALPN = 16,
  528. S2N_EXTENSION_CERTIFICATE_TRANSPARENCY = 18,
  529. S2N_EXTENSION_SUPPORTED_VERSIONS = 43,
  530. S2N_EXTENSION_RENEGOTIATION_INFO = 65281,
  531. } s2n_tls_extension_type;
  532. /**
  533. * MFL configurations from https://datatracker.ietf.org/doc/html/rfc6066#section-4.
  534. */
  535. typedef enum {
  536. S2N_TLS_MAX_FRAG_LEN_512 = 1,
  537. S2N_TLS_MAX_FRAG_LEN_1024 = 2,
  538. S2N_TLS_MAX_FRAG_LEN_2048 = 3,
  539. S2N_TLS_MAX_FRAG_LEN_4096 = 4,
  540. } s2n_max_frag_len;
  541. /**
  542. * Opaque certificate type.
  543. */
  544. struct s2n_cert;
  545. /**
  546. * Opaque certificate chain and key type.
  547. */
  548. struct s2n_cert_chain_and_key;
  549. /**
  550. * Opaque key type.
  551. */
  552. struct s2n_pkey;
  553. /**
  554. * Opaque public key type.
  555. */
  556. typedef struct s2n_pkey s2n_cert_public_key;
  557. /**
  558. * Opaque private key type.
  559. */
  560. typedef struct s2n_pkey s2n_cert_private_key;
  561. /**
  562. * Creates a new s2n_cert_chain_and_key object. This object can be associated
  563. * with many config objects. It is used to represent a certificate and key pair.
  564. *
  565. * @returns A new object used to represent a certificate-chain/key pair
  566. */
  567. S2N_API extern struct s2n_cert_chain_and_key *s2n_cert_chain_and_key_new(void);
  568. /**
  569. * Associates a certificate chain and private key with an `s2n_cert_chain_and_key` object.
  570. *
  571. * `cert_chain_pem` should be a PEM encoded certificate chain, with the first
  572. * certificate in the chain being your leaf certificate. `private_key_pem`
  573. * should be a PEM encoded private key corresponding to the leaf certificate.
  574. *
  575. * @note Prefer using s2n_cert_chain_and_key_load_pem_bytes.
  576. *
  577. * @param chain_and_key The certificate chain and private key handle
  578. * @param chain_pem A byte array of a PEM encoded certificate chain.
  579. * @param private_key_pem A byte array of a PEM encoded key.
  580. *
  581. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  582. */
  583. S2N_API extern int s2n_cert_chain_and_key_load_pem(struct s2n_cert_chain_and_key *chain_and_key, const char *chain_pem, const char *private_key_pem);
  584. /**
  585. * Associates a certificate chain and private key with an `s2n_cert_chain_and_key` object.
  586. *
  587. * `cert_chain_pem` should be a PEM encoded certificate chain, with the first
  588. * certificate in the chain being your leaf certificate. `private_key_pem`
  589. * should be a PEM encoded private key corresponding to the leaf certificate.
  590. *
  591. * @param chain_and_key The certificate chain and private key handle
  592. * @param chain_pem A byte array of a PEM encoded certificate chain.
  593. * @param chain_pem_len Size of `chain_pem`
  594. * @param private_key_pem A byte array of a PEM encoded key.
  595. * @param private_key_pem_len Size of `private_key_pem`
  596. *
  597. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  598. */
  599. S2N_API extern int s2n_cert_chain_and_key_load_pem_bytes(struct s2n_cert_chain_and_key *chain_and_key, uint8_t *chain_pem, uint32_t chain_pem_len, uint8_t *private_key_pem, uint32_t private_key_pem_len);
  600. /**
  601. * Associates a public certificate chain with a `s2n_cert_chain_and_key` object. It does
  602. * NOT set a private key, so the connection will need to be configured to
  603. * [offload private key operations](https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md#offloading-asynchronous-private-key-operations).
  604. *
  605. * @param chain_and_key The certificate chain and private key handle
  606. * @param chain_pem A byte array of a PEM encoded certificate chain.
  607. * @param chain_pem_len Size of `chain_pem`
  608. *
  609. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  610. */
  611. S2N_API extern int s2n_cert_chain_and_key_load_public_pem_bytes(struct s2n_cert_chain_and_key *chain_and_key, uint8_t *chain_pem, uint32_t chain_pem_len);
  612. /**
  613. * Frees the memory associated with an `s2n_cert_chain_and_key` object.
  614. *
  615. * @param cert_and_key The certificate chain and private key handle
  616. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  617. */
  618. S2N_API extern int s2n_cert_chain_and_key_free(struct s2n_cert_chain_and_key *cert_and_key);
  619. /**
  620. * Adds a context to the `s2n_cert_chain_and_key` object.
  621. *
  622. * @param cert_and_key The certificate chain and private key handle
  623. * @param ctx An opaque pointer to user supplied data.
  624. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  625. */
  626. S2N_API extern int s2n_cert_chain_and_key_set_ctx(struct s2n_cert_chain_and_key *cert_and_key, void *ctx);
  627. /**
  628. * Get the user supplied context from the `s2n_cert_chain_and_key` object.
  629. *
  630. * @param cert_and_key The certificate chain and private key handle
  631. * @returns The user supplied pointer from s2n_cert_chain_and_key_set_ctx()
  632. */
  633. S2N_API extern void *s2n_cert_chain_and_key_get_ctx(struct s2n_cert_chain_and_key *cert_and_key);
  634. /**
  635. * Get the private key from the `s2n_cert_chain_and_key` object.
  636. *
  637. * @param cert_and_key The certificate chain and private key handle
  638. * @returns A pointer to the `s2n_cert_private_key`
  639. */
  640. S2N_API extern s2n_cert_private_key *s2n_cert_chain_and_key_get_private_key(struct s2n_cert_chain_and_key *cert_and_key);
  641. /**
  642. * Set the raw OCSP stapling data for a certificate chain.
  643. *
  644. * @param chain_and_key The certificate chain handle
  645. * @param data A pointer to the raw OCSP stapling data bytes. The data will be copied.
  646. * @param length The length of the data bytes.
  647. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  648. */
  649. S2N_API extern int s2n_cert_chain_and_key_set_ocsp_data(struct s2n_cert_chain_and_key *chain_and_key, const uint8_t *data, uint32_t length);
  650. /**
  651. * Set the signed certificate timestamp (SCT) for a certificate chain.
  652. * This is used for Certificate Transparency.
  653. *
  654. * @param chain_and_key The certificate chain handle
  655. * @param data A pointer to the SCT data. The data will be copied.
  656. * @param length The length of the data bytes.
  657. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  658. */
  659. S2N_API extern int s2n_cert_chain_and_key_set_sct_list(struct s2n_cert_chain_and_key *chain_and_key, const uint8_t *data, uint32_t length);
  660. /**
  661. * A callback function that is invoked if s2n-tls cannot resolve a conflict between
  662. * two certificates with the same domain name. This function is invoked while certificates
  663. * are added to an `s2n_config`.
  664. *
  665. * Currently, the only builtin resolution for domain name conflicts is certificate type(RSA,
  666. * ECDSA, etc). The callback should return a pointer to the `s2n_cert_chain_and_key` that
  667. * should be used for dns name `name`.
  668. *
  669. * If NULL is returned, the first certificate will be used. Typically an application
  670. * will use properties like trust and expiry to implement tiebreaking.
  671. */
  672. typedef struct s2n_cert_chain_and_key *(*s2n_cert_tiebreak_callback)(struct s2n_cert_chain_and_key *cert1, struct s2n_cert_chain_and_key *cert2, uint8_t *name, uint32_t name_len);
  673. /**
  674. * Sets the `s2n_cert_tiebreak_callback` for resolving domain name conflicts.
  675. * If no callback is set, the first certificate added for a domain name will always be preferred.
  676. *
  677. * @param config The configuration object being updated
  678. * @param cert_tiebreak_cb The pointer to the certificate tiebreak function
  679. *
  680. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  681. */
  682. S2N_API extern int s2n_config_set_cert_tiebreak_callback(struct s2n_config *config, s2n_cert_tiebreak_callback cert_tiebreak_cb);
  683. /**
  684. * Associates a certificate chain and private key with an `s2n_config` object.
  685. * Using this API, only one cert chain of each type (like ECDSA or RSA) may be associated with a config.
  686. * `cert_chain_pem` should be a PEM encoded certificate chain, with the first certificate
  687. * in the chain being your server's certificate. `private_key_pem` should be a
  688. * PEM encoded private key corresponding to the server certificate.
  689. *
  690. * @deprecated Use s2n_config_add_cert_chain_and_key_to_store instead.
  691. *
  692. * @param config The configuration object being updated
  693. * @param cert_chain_pem A byte array of a PEM encoded certificate chain.
  694. * @param private_key_pem A byte array of a PEM encoded key.
  695. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure.
  696. */
  697. S2N_API extern int s2n_config_add_cert_chain_and_key(struct s2n_config *config, const char *cert_chain_pem, const char *private_key_pem);
  698. /**
  699. * The preferred method of associating a certificate chain and private key pair with an `s2n_config` object.
  700. * This method may be called multiple times to support multiple key types(RSA, ECDSA) and multiple domains.
  701. * On the server side, the certificate selected will be based on the incoming SNI value and the
  702. * client's capabilities(supported ciphers).
  703. *
  704. * In the case of no certificate matching the client's SNI extension or if no SNI extension was sent by
  705. * the client, the certificate from the `first` call to `s2n_config_add_cert_chain_and_key_to_store`
  706. * will be selected.
  707. *
  708. * @warning It is not recommended to free or modify the `cert_key_pair` as any subsequent changes will be
  709. * reflected in the config.
  710. *
  711. * @param config The configuration object being updated
  712. * @param cert_key_pair The certificate chain and private key handle
  713. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  714. */
  715. S2N_API extern int s2n_config_add_cert_chain_and_key_to_store(struct s2n_config *config, struct s2n_cert_chain_and_key *cert_key_pair);
  716. /**
  717. * Explicitly sets certificate chain and private key pairs to be used as defaults for each auth
  718. * method (key type). A "default" certificate is used when there is not an SNI match with any other
  719. * configured certificate.
  720. *
  721. * Only one certificate can be set as the default per auth method (one RSA default, one ECDSA default,
  722. * etc.). All previous default certificates will be cleared and re-set when this API is called.
  723. *
  724. * This API is called for a specific `s2n_config` object. s2n-tls will attempt to automatically choose
  725. * default certificates for each auth method (key type) based on the order that `s2n_cert_chain_and_key`
  726. * are added to the `s2n_config` using one of the APIs listed above.
  727. * `s2n_config_set_cert_chain_and_key_defaults` can be called at any time; s2n-tls will clear defaults
  728. * and no longer attempt to automatically choose any default certificates.
  729. *
  730. * @param config The configuration object being updated
  731. * @param cert_key_pairs An array of certificate chain and private key handles
  732. * @param num_cert_key_pairs The amount of handles in cert_key_pairs
  733. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  734. */
  735. S2N_API extern int s2n_config_set_cert_chain_and_key_defaults(struct s2n_config *config,
  736. struct s2n_cert_chain_and_key **cert_key_pairs, uint32_t num_cert_key_pairs);
  737. /**
  738. * Adds to the trust store from a CA file or directory containing trusted certificates.
  739. *
  740. * When configs are created with `s2n_config_new()`, the trust store is initialized with default
  741. * system certificates. To completely override these certificates, call
  742. * `s2n_config_wipe_trust_store()` before calling this function.
  743. *
  744. * @note The trust store will be initialized with the common locations for the host
  745. * operating system by default.
  746. * @param config The configuration object being updated
  747. * @param ca_pem_filename A string for the file path of the CA PEM file.
  748. * @param ca_dir A string for the directory of the CA PEM files.
  749. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  750. */
  751. S2N_API extern int s2n_config_set_verification_ca_location(struct s2n_config *config, const char *ca_pem_filename, const char *ca_dir);
  752. /**
  753. * Adds a PEM to the trust store. This will allocate memory, and load `pem` into the trust store.
  754. *
  755. * When configs are created with `s2n_config_new()`, the trust store is initialized with default
  756. * system certificates. To completely override these certificates, call
  757. * `s2n_config_wipe_trust_store()` before calling this function.
  758. *
  759. * @param config The configuration object being updated
  760. * @param pem The string value of the PEM certificate.
  761. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  762. */
  763. S2N_API extern int s2n_config_add_pem_to_trust_store(struct s2n_config *config, const char *pem);
  764. /**
  765. * Clears the trust store of all certificates.
  766. *
  767. * When configs are created with `s2n_config_new()`, the trust store is initialized with default
  768. * system certificates. To completely override these certificates, call this function before
  769. * functions like `s2n_config_set_verification_ca_location()` or
  770. * `s2n_config_add_pem_to_trust_store()`.
  771. *
  772. * @param config The configuration object being updated
  773. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  774. */
  775. S2N_API extern int s2n_config_wipe_trust_store(struct s2n_config *config);
  776. /**
  777. * Loads default system certificates into the trust store.
  778. *
  779. * `s2n_config_new_minimal()` doesn't load default system certificates into the config's trust
  780. * store by default. If `config` was created with `s2n_config_new_minimal`, this function can be
  781. * used to load system certificates into the trust store.
  782. *
  783. * @note This API will error if called on a config that has already loaded system certificates
  784. * into its trust store, which includes all configs created with `s2n_config_new()`.
  785. *
  786. * @param config The configuration object being updated
  787. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  788. */
  789. S2N_API extern int s2n_config_load_system_certs(struct s2n_config *config);
  790. typedef enum {
  791. S2N_VERIFY_AFTER_SIGN_DISABLED,
  792. S2N_VERIFY_AFTER_SIGN_ENABLED
  793. } s2n_verify_after_sign;
  794. /**
  795. * Toggle whether generated signatures are verified before being sent.
  796. *
  797. * Although signatures produced by the underlying libcrypto should always be valid,
  798. * hardware faults, bugs in the signing implementation, or other uncommon factors
  799. * can cause unexpected mistakes in the final signatures. Because these mistakes
  800. * can leak information about the private key, applications with low trust in their
  801. * hardware or libcrypto may want to verify signatures before sending them.
  802. *
  803. * However, this feature will significantly impact handshake latency.
  804. * Additionally, most libcrypto implementations already check for common errors in signatures.
  805. */
  806. S2N_API extern int s2n_config_set_verify_after_sign(struct s2n_config *config, s2n_verify_after_sign mode);
  807. /**
  808. * Set a custom send buffer size.
  809. *
  810. * This buffer is used to stage records for sending. By default,
  811. * enough memory is allocated to hold a single record of the maximum
  812. * size configured for the connection. With the default fragment size,
  813. * that is about 8K bytes.
  814. *
  815. * Less memory can be allocated for the send buffer, but this will result in
  816. * smaller, more fragmented records and increased overhead. While the absolute
  817. * minimum size required is 1034 bytes, at least 2K bytes is recommended for
  818. * reasonable record sizes.
  819. *
  820. * More memory can be allocated for the send buffer. This will result in s2n-tls
  821. * buffering multiple records before sending them, reducing system write calls.
  822. * At least 17K bytes is recommended for this use case, or at least 35K bytes
  823. * if larger fragment sizes are used via `s2n_connection_prefer_throughput()`.
  824. *
  825. * @param config The configuration object being updated
  826. * @param size The desired custom buffer size.
  827. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  828. */
  829. S2N_API extern int s2n_config_set_send_buffer_size(struct s2n_config *config, uint32_t size);
  830. /**
  831. * Enable or disable receiving of multiple TLS records in a single s2n_recv call
  832. *
  833. * By default, s2n-tls returns from s2n_recv() after reading a single TLS record.
  834. * Enabling receiving of multiple records will instead cause s2n_recv() to attempt
  835. * to read until the application-provided output buffer is full. This may be more
  836. * efficient, especially if larger receive buffers are used.
  837. *
  838. * @note If this option is enabled with blocking IO, the call to s2n_recv() will
  839. * not return until either the application-provided output buffer is full or the
  840. * peer closes the connection. This may lead to unintentionally long waits if the
  841. * peer does not send enough data.
  842. *
  843. * @param config The configuration object being updated
  844. * @param enabled Set to `true` if multiple record receive is to be enabled; `false` to disable.
  845. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  846. */
  847. S2N_API extern int s2n_config_set_recv_multi_record(struct s2n_config *config, bool enabled);
  848. /**
  849. * A callback function invoked (usually multiple times) during X.509 validation for each
  850. * name encountered in the leaf certificate.
  851. *
  852. * Return 1 to trust that hostname or 0 to not trust the hostname.
  853. *
  854. * If this function returns 1, then the certificate is considered trusted and that portion
  855. * of the X.509 validation will succeed.
  856. *
  857. * If no hostname results in a 1 being returned, the certificate will be untrusted and the
  858. * validation will terminate immediately.
  859. *
  860. * Data is a opaque user context set in s2n_config_set_verify_host_callback() or s2n_connection_set_verify_host_callback().
  861. */
  862. typedef uint8_t (*s2n_verify_host_fn)(const char *host_name, size_t host_name_len, void *data);
  863. /**
  864. * Sets the callback to use for verifying that a hostname from an X.509 certificate is trusted.
  865. *
  866. * The default behavior is to require that the hostname match the server name set with s2n_set_server_name().
  867. * This will likely lead to all client certificates being rejected, so the callback will need to be overriden when using
  868. * client authentication.
  869. *
  870. * This change will be inherited by s2n_connections using this config. If a separate callback for different connections
  871. * using the same config is desired, see s2n_connection_set_verify_host_callback().
  872. *
  873. * @param config The configuration object being updated
  874. * @param data A user supplied opaque context to pass back to the callback
  875. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  876. */
  877. S2N_API extern int s2n_config_set_verify_host_callback(struct s2n_config *config, s2n_verify_host_fn, void *data);
  878. /**
  879. * Toggles whether or not to validate stapled OCSP responses.
  880. *
  881. * 1 means OCSP responses will be validated when they are encountered, while 0 means this step will
  882. * be skipped.
  883. *
  884. * The default value is 1 if the underlying libCrypto implementation supports OCSP.
  885. *
  886. * @param config The configuration object being updated
  887. * @param check_ocsp The desired OCSP response check configuration
  888. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  889. */
  890. S2N_API extern int s2n_config_set_check_stapled_ocsp_response(struct s2n_config *config, uint8_t check_ocsp);
  891. /**
  892. * Disables timestamp validation for received certificates.
  893. *
  894. * By default, s2n-tls checks the notBefore and notAfter fields on the certificates it receives
  895. * during the handshake. If the current date is not within the range of these fields for any
  896. * certificate in the chain of trust, `s2n_negotiate()` will error. This validation is in
  897. * accordance with RFC 5280, section 6.1.3 a.2:
  898. * https://datatracker.ietf.org/doc/html/rfc5280#section-6.1.3.
  899. *
  900. * This API will disable this timestamp validation, permitting negotiation with peers that send
  901. * expired certificates, or certificates that are not yet considered valid.
  902. *
  903. * @warning Applications calling this API should seriously consider the security implications of
  904. * disabling this validation. The validity period of a certificate corresponds to the range of time
  905. * in which the CA is guaranteed to maintain information regarding the certificate's revocation
  906. * status. As such, it may not be possible to obtain accurate revocation information for
  907. * certificates with invalid timestamps. Applications disabling this validation MUST implement
  908. * some external method for limiting certificate lifetime.
  909. *
  910. * @param config The associated connection config.
  911. * @returns S2N_SUCCESS on success, S2N_FAILURE on failure.
  912. */
  913. S2N_API extern int s2n_config_disable_x509_time_verification(struct s2n_config *config);
  914. /**
  915. * Turns off all X.509 validation during the negotiation phase of the connection. This should only
  916. * be used for testing or debugging purposes.
  917. *
  918. * @param config The configuration object being updated
  919. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  920. */
  921. S2N_API extern int s2n_config_disable_x509_verification(struct s2n_config *config);
  922. /**
  923. * Sets the maximum allowed depth of a cert chain used for X509 validation. The default value is
  924. * 7. If this limit is exceeded, validation will fail if s2n_config_disable_x509_verification()
  925. * has not been called. 0 is an illegal value and will return an error.
  926. * 1 means only a root certificate will be used.
  927. *
  928. * @param config The configuration object being updated
  929. * @param max_depth The number of allowed certificates in the certificate chain
  930. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  931. */
  932. S2N_API extern int s2n_config_set_max_cert_chain_depth(struct s2n_config *config, uint16_t max_depth);
  933. /**
  934. * Associates a set of Diffie-Hellman parameters with an `s2n_config` object.
  935. * @note `dhparams_pem` should be PEM encoded DH parameters.
  936. *
  937. * @param config The configuration object being updated
  938. * @param dhparams_pem A string containing the PEM encoded DH parameters.
  939. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  940. */
  941. S2N_API extern int s2n_config_add_dhparams(struct s2n_config *config, const char *dhparams_pem);
  942. /**
  943. * Sets the security policy that includes the cipher/kem/signature/ecc preferences and
  944. * protocol version.
  945. *
  946. * See the [USAGE-GUIDE.md](https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md) for how to use security policies.
  947. */
  948. S2N_API extern int s2n_config_set_cipher_preferences(struct s2n_config *config, const char *version);
  949. /**
  950. * Appends the provided application protocol to the preference list
  951. *
  952. * The data provided in `protocol` parameter will be copied into an internal buffer
  953. *
  954. * @param config The configuration object being updated
  955. * @param protocol A pointer to a byte array value
  956. * @param protocol_len The length of bytes that should be read from `protocol`. Note: this value cannot be 0, otherwise an error will be returned.
  957. */
  958. S2N_API extern int s2n_config_append_protocol_preference(struct s2n_config *config, const uint8_t *protocol, uint8_t protocol_len);
  959. /**
  960. * Sets the application protocol preferences on an `s2n_config` object.
  961. * `protocols` is a list in order of preference, with most preferred protocol first, and of
  962. * length `protocol_count`.
  963. *
  964. * When acting as an `S2N_CLIENT` the protocol list is included in the Client Hello message
  965. * as the ALPN extension.
  966. *
  967. * As an `S2N_SERVER`, the list is used to negotiate a mutual application protocol with the
  968. * client. After the negotiation for the connection has completed, the agreed upon protocol
  969. * can be retrieved with s2n_get_application_protocol()
  970. *
  971. * @param config The configuration object being updated
  972. * @param protocols The list of preferred protocols, in order of preference
  973. * @param protocol_count The size of the protocols list
  974. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  975. */
  976. S2N_API extern int s2n_config_set_protocol_preferences(struct s2n_config *config, const char *const *protocols, int protocol_count);
  977. /**
  978. * Enum used to define the type, if any, of certificate status request
  979. * a connection should make during the handshake. The only supported status request type is
  980. * OCSP, `S2N_STATUS_REQUEST_OCSP`.
  981. */
  982. typedef enum {
  983. S2N_STATUS_REQUEST_NONE = 0,
  984. S2N_STATUS_REQUEST_OCSP = 1
  985. } s2n_status_request_type;
  986. /**
  987. * Sets up a connection to request the certificate status of a peer during an SSL handshake. If set
  988. * to S2N_STATUS_REQUEST_NONE, no status request is made.
  989. *
  990. * @param config The configuration object being updated
  991. * @param type The desired request status type
  992. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  993. */
  994. S2N_API extern int s2n_config_set_status_request_type(struct s2n_config *config, s2n_status_request_type type);
  995. /**
  996. * Enum to set Certificate Transparency Support level.
  997. */
  998. typedef enum {
  999. S2N_CT_SUPPORT_NONE = 0,
  1000. S2N_CT_SUPPORT_REQUEST = 1
  1001. } s2n_ct_support_level;
  1002. /**
  1003. * Set the Certificate Transparency Support level.
  1004. *
  1005. * @param config The configuration object being updated
  1006. * @param level The desired Certificate Transparency Support configuration
  1007. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1008. */
  1009. S2N_API extern int s2n_config_set_ct_support_level(struct s2n_config *config, s2n_ct_support_level level);
  1010. /**
  1011. * Sets whether or not a connection should terminate on receiving a WARNING alert from its peer.
  1012. *
  1013. * `alert_behavior` can take the following values:
  1014. * - `S2N_ALERT_FAIL_ON_WARNINGS` default behavior: s2n-tls will terminate the connection if its peer sends a WARNING alert.
  1015. * - `S2N_ALERT_IGNORE_WARNINGS` - with the exception of `close_notify` s2n-tls will ignore all WARNING alerts and keep communicating with its peer. This setting is ignored in TLS1.3
  1016. *
  1017. * @note TLS1.3 terminates a connection for all alerts except user_canceled.
  1018. * @warning S2N_ALERT_FAIL_ON_WARNINGS is the recommended behavior. Past TLS protocol vulnerabilities have involved downgrading alerts to warnings.
  1019. */
  1020. typedef enum {
  1021. S2N_ALERT_FAIL_ON_WARNINGS = 0,
  1022. S2N_ALERT_IGNORE_WARNINGS = 1
  1023. } s2n_alert_behavior;
  1024. /**
  1025. * Sets the config's alert behavior based on the `s2n_alert_behavior` enum.
  1026. *
  1027. * @param config The configuration object being updated
  1028. * @param alert_behavior The desired alert behavior.
  1029. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1030. */
  1031. S2N_API extern int s2n_config_set_alert_behavior(struct s2n_config *config, s2n_alert_behavior alert_behavior);
  1032. /**
  1033. * Sets the extension data in the `s2n_config` object for the specified extension.
  1034. * This method will clear any existing data that is set. If the data and length
  1035. * parameters are set to NULL, no new data is set in the `s2n_config` object,
  1036. * effectively clearing existing data.
  1037. *
  1038. * @deprecated Use s2n_cert_chain_and_key_set_ocsp_data and s2n_cert_chain_and_key_set_sct_list instead.
  1039. *
  1040. * @param config The configuration object being updated
  1041. * @param type The extension type
  1042. * @param data Data for the extension
  1043. * @param length Length of the `data` buffer
  1044. */
  1045. S2N_API extern int s2n_config_set_extension_data(struct s2n_config *config, s2n_tls_extension_type type, const uint8_t *data, uint32_t length);
  1046. /**
  1047. * Allows the caller to set a TLS Maximum Fragment Length extension that will be used
  1048. * to fragment outgoing messages. s2n-tls currently does not reject fragments larger
  1049. * than the configured maximum when in server mode. The TLS negotiated maximum fragment
  1050. * length overrides the preference set by the `s2n_connection_prefer_throughput` and
  1051. * `s2n_connection_prefer_low_latency`.
  1052. *
  1053. * @note Some TLS implementations do not respect their peer's max fragment length extension.
  1054. *
  1055. * @param config The configuration object being updated
  1056. * @param mfl_code The selected MFL size
  1057. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1058. */
  1059. S2N_API extern int s2n_config_send_max_fragment_length(struct s2n_config *config, s2n_max_frag_len mfl_code);
  1060. /**
  1061. * Allows the server to opt-in to accept client's TLS maximum fragment length extension
  1062. * requests. If this API is not called, and client requests the extension, server will ignore
  1063. * the request and continue TLS handshake with default maximum fragment length of 8k bytes
  1064. *
  1065. * @note Some TLS implementations do not respect their peer's max fragment length extension.
  1066. *
  1067. * @param config The configuration object being updated
  1068. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1069. */
  1070. S2N_API extern int s2n_config_accept_max_fragment_length(struct s2n_config *config);
  1071. /**
  1072. * Sets the lifetime of the cached session state. The default value is 15 hours.
  1073. *
  1074. * @param config The configuration object being updated
  1075. * @param lifetime_in_secs The desired lifetime of the session state in seconds
  1076. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1077. */
  1078. S2N_API extern int s2n_config_set_session_state_lifetime(struct s2n_config *config, uint64_t lifetime_in_secs);
  1079. /**
  1080. * Enable or disable session resumption using session ticket.
  1081. *
  1082. * @param config The configuration object being updated
  1083. * @param enabled The configuration object being updated. Set to 1 to enable. Set to 0 to disable.
  1084. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1085. */
  1086. S2N_API extern int s2n_config_set_session_tickets_onoff(struct s2n_config *config, uint8_t enabled);
  1087. /**
  1088. * Enable or disable session caching.
  1089. *
  1090. * @note Session caching will not be turned on unless all three session cache callbacks are set
  1091. * prior to calling this function.
  1092. *
  1093. * @param config The configuration object being updated
  1094. * @param enabled The configuration object being updated. Set to 1 to enable. Set to 0 to disable.
  1095. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1096. */
  1097. S2N_API extern int s2n_config_set_session_cache_onoff(struct s2n_config *config, uint8_t enabled);
  1098. /**
  1099. * Sets how long a session ticket key will be in a state where it can be used for both encryption
  1100. * and decryption of tickets on the server side.
  1101. *
  1102. * @note The default value is 2 hours.
  1103. * @param config The configuration object being updated
  1104. * @param lifetime_in_secs The desired lifetime of decrypting and encrypting tickets in seconds
  1105. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1106. */
  1107. S2N_API extern int s2n_config_set_ticket_encrypt_decrypt_key_lifetime(struct s2n_config *config, uint64_t lifetime_in_secs);
  1108. /**
  1109. * Sets how long a session ticket key will be in a state where it can used just for decryption of
  1110. * already assigned tickets on the server side. Once decrypted, the session will resume and the
  1111. * server will issue a new session ticket encrypted using a key in encrypt-decrypt state.
  1112. *
  1113. * @note The default value is 13 hours.
  1114. * @param config The configuration object being updated
  1115. * @param lifetime_in_secs The desired lifetime of decrypting and encrypting tickets in seconds
  1116. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1117. */
  1118. S2N_API extern int s2n_config_set_ticket_decrypt_key_lifetime(struct s2n_config *config, uint64_t lifetime_in_secs);
  1119. /**
  1120. * Adds session ticket key on the server side. It would be ideal to add new keys after every
  1121. * (encrypt_decrypt_key_lifetime_in_nanos/2) nanos because this will allow for gradual and
  1122. * linear transition of a key from encrypt-decrypt state to decrypt-only state.
  1123. *
  1124. * @param config The configuration object being updated
  1125. * @param name Name of the session ticket key that should be randomly generated to avoid collisions
  1126. * @param name_len Length of session ticket key name
  1127. * @param key Key used to perform encryption/decryption of session ticket
  1128. * @param key_len Length of the session ticket key
  1129. * @param intro_time_in_seconds_from_epoch Time at which the session ticket key is introduced. If this is 0, then intro_time_in_seconds_from_epoch is set to now.
  1130. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1131. */
  1132. S2N_API extern int s2n_config_add_ticket_crypto_key(struct s2n_config *config, const uint8_t *name, uint32_t name_len,
  1133. uint8_t *key, uint32_t key_len, uint64_t intro_time_in_seconds_from_epoch);
  1134. /**
  1135. * Sets user defined context on the `s2n_config` object.
  1136. *
  1137. * @param config The configuration object being updated
  1138. * @param ctx A pointer to the user defined ctx.
  1139. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1140. */
  1141. S2N_API extern int s2n_config_set_ctx(struct s2n_config *config, void *ctx);
  1142. /**
  1143. * Gets the user defined context from the `s2n_config` object.
  1144. * The context is set by calling s2n_config_set_ctx()
  1145. *
  1146. * @param config The configuration object being accessed
  1147. * @param ctx A pointer to the user defined ctx.
  1148. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1149. */
  1150. S2N_API extern int s2n_config_get_ctx(struct s2n_config *config, void **ctx);
  1151. /**
  1152. * Used to declare connections as server or client type, respectively.
  1153. */
  1154. typedef enum {
  1155. S2N_SERVER,
  1156. S2N_CLIENT
  1157. } s2n_mode;
  1158. /**
  1159. * Creates a new connection object. Each s2n-tls SSL/TLS connection uses
  1160. * one of these objects. These connection objects can be operated on by up
  1161. * to two threads at a time, one sender and one receiver, but neither sending
  1162. * nor receiving are atomic, so if these objects are being called by multiple
  1163. * sender or receiver threads, you must perform your own locking to ensure
  1164. * that only one sender or receiver is active at a time.
  1165. *
  1166. * The `mode` parameters specifies if the caller is a server, or is a client.
  1167. * Connections objects are re-usable across many connections, and should be
  1168. * re-used (to avoid deallocating and allocating memory). You should wipe
  1169. * connections immediately after use.
  1170. *
  1171. * @param mode The desired connection type
  1172. * @returns A s2n_connection handle
  1173. */
  1174. S2N_API extern struct s2n_connection *s2n_connection_new(s2n_mode mode);
  1175. /**
  1176. * Associates a configuration object with a connection.
  1177. *
  1178. * @param conn The connection object being associated
  1179. * @param config The configuration object being associated
  1180. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1181. */
  1182. S2N_API extern int s2n_connection_set_config(struct s2n_connection *conn, struct s2n_config *config);
  1183. /**
  1184. * Sets user defined context in `s2n_connection` object.
  1185. *
  1186. * @param conn The connection object being updated
  1187. * @param ctx A pointer to the user defined context
  1188. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1189. */
  1190. S2N_API extern int s2n_connection_set_ctx(struct s2n_connection *conn, void *ctx);
  1191. /**
  1192. * Gets user defined context from a `s2n_connection` object.
  1193. *
  1194. * @param conn The connection object that contains the desired context
  1195. */
  1196. S2N_API extern void *s2n_connection_get_ctx(struct s2n_connection *conn);
  1197. /**
  1198. * The callback function takes a s2n-tls connection as input, which receives the ClientHello
  1199. * and the context previously provided in `s2n_config_set_client_hello_cb`. The callback can
  1200. * access any ClientHello information from the connection and use the `s2n_connection_set_config`
  1201. * call to change the config of the connection.
  1202. */
  1203. typedef int s2n_client_hello_fn(struct s2n_connection *conn, void *ctx);
  1204. /**
  1205. * Client Hello callback modes
  1206. * - `S2N_CLIENT_HELLO_CB_BLOCKING` (default):
  1207. * - In this mode s2n-tls expects the callback to complete its work and return the appropriate response code before the handshake continues. If any of the connection properties were changed based on the server_name extension the callback must either return a value greater than 0 or invoke `s2n_connection_server_name_extension_used`, otherwise the callback returns 0 to continue the handshake.
  1208. * - `S2N_CLIENT_HELLO_CB_NONBLOCKING`:
  1209. * - In non-blocking mode, s2n-tls expects the callback to not complete its work. If the callback returns a response code of 0, s2n-tls will return `S2N_FAILURE` with `S2N_ERR_T_BLOCKED` error type and `s2n_blocked_status` set to `S2N_BLOCKED_ON_APPLICATION_INPUT`. The handshake is paused and further calls to `s2n_negotiate` will continue to return the same error until `s2n_client_hello_cb_done` is invoked for the `s2n_connection` to resume the handshake. If any of the connection properties were changed on the basis of the server_name extension then `s2n_connection_server_name_extension_used` must be invoked before marking the callback done.
  1210. */
  1211. typedef enum {
  1212. S2N_CLIENT_HELLO_CB_BLOCKING,
  1213. S2N_CLIENT_HELLO_CB_NONBLOCKING
  1214. } s2n_client_hello_cb_mode;
  1215. /**
  1216. * Allows the caller to set a callback function that will be called after ClientHello was parsed.
  1217. *
  1218. * @param config The configuration object being updated
  1219. * @param client_hello_callback The client hello callback function
  1220. * @param ctx A pointer to a user defined context that the Client Hello callback will be invoked with.
  1221. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1222. */
  1223. S2N_API extern int s2n_config_set_client_hello_cb(struct s2n_config *config, s2n_client_hello_fn client_hello_callback, void *ctx);
  1224. /**
  1225. * Sets the callback execution mode.
  1226. *
  1227. * See s2n_client_hello_cb_mode for each mode's behavior.
  1228. *
  1229. * @param config The configuration object being updated
  1230. * @param cb_mode The desired callback mode
  1231. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1232. */
  1233. S2N_API extern int s2n_config_set_client_hello_cb_mode(struct s2n_config *config, s2n_client_hello_cb_mode cb_mode);
  1234. /**
  1235. * Marks the non-blocking callback as complete. Can be invoked from within the callback when
  1236. * operating in non-blocking mode to continue the handshake.
  1237. *
  1238. * @param conn The connection object being updated
  1239. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1240. */
  1241. S2N_API extern int s2n_client_hello_cb_done(struct s2n_connection *conn);
  1242. /**
  1243. * Must be invoked if any of the connection properties were changed on the basis of the server_name
  1244. * extension. This must be invoked before marking the Client Hello callback done.
  1245. *
  1246. * @param conn The connection object being updated
  1247. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1248. */
  1249. S2N_API extern int s2n_connection_server_name_extension_used(struct s2n_connection *conn);
  1250. /**
  1251. * Opaque client hello handle
  1252. */
  1253. struct s2n_client_hello;
  1254. /**
  1255. * Get the Client Hello from a s2n_connection.
  1256. *
  1257. * Earliest point during the handshake when this structure is available for use is in the
  1258. * client_hello_callback (see s2n_config_set_client_hello_cb()).
  1259. *
  1260. * @param conn The connection object containing the client hello
  1261. * @returns A handle to the s2n_client_hello structure holding the client hello message sent by the client during the handshake. NULL is returned if a Client Hello has not yet been received and parsed.
  1262. */
  1263. S2N_API extern struct s2n_client_hello *s2n_connection_get_client_hello(struct s2n_connection *conn);
  1264. /**
  1265. * Function to determine the size of the raw Client Hello buffer.
  1266. *
  1267. * Can be used to determine the necessary size of the `out` buffer for
  1268. * s2n_client_hello_get_raw_message()
  1269. *
  1270. * @param ch The Client Hello handle
  1271. * @returns The size of the ClientHello message received by the server
  1272. */
  1273. S2N_API extern ssize_t s2n_client_hello_get_raw_message_length(struct s2n_client_hello *ch);
  1274. /**
  1275. * Copies `max_length` bytes of the ClientHello message into the `out` buffer.
  1276. * The ClientHello instrumented using this function will have the Random bytes
  1277. * zero-ed out.
  1278. *
  1279. * Note: SSLv2 ClientHello messages follow a different structure than more modern
  1280. * ClientHello messages. See [RFC5246](https://tools.ietf.org/html/rfc5246#appendix-E.2).
  1281. * In addition, due to how s2n-tls parses SSLv2 ClientHellos, the raw message is
  1282. * missing the first three bytes (the msg_type and version) and instead begins with
  1283. * the cipher_specs. To determine whether a ClientHello is an SSLv2 ClientHello,
  1284. * you will need to use s2n_connection_get_client_hello_version(). To get the
  1285. * protocol version advertised in the SSLv2 ClientHello (which may be higher
  1286. * than SSLv2), you will need to use s2n_connection_get_client_protocol_version().
  1287. *
  1288. * @param ch The Client Hello handle
  1289. * @param out The destination buffer for the raw Client Hello
  1290. * @param max_length The size of out in bytes
  1291. * @returns The number of copied bytes
  1292. */
  1293. S2N_API extern ssize_t s2n_client_hello_get_raw_message(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length);
  1294. /**
  1295. * Function to determine the size of the Client Hello cipher suites.
  1296. * This can be used to allocate the `out` buffer for s2n_client_hello_get_cipher_suites().
  1297. *
  1298. * @param ch The Client Hello handle
  1299. * @returns the number of bytes the cipher_suites takes on the ClientHello message received by the server
  1300. */
  1301. S2N_API extern ssize_t s2n_client_hello_get_cipher_suites_length(struct s2n_client_hello *ch);
  1302. /**
  1303. * Copies into the `out` buffer `max_length` bytes of the cipher_suites on the ClientHello.
  1304. *
  1305. * Note: SSLv2 ClientHello cipher suites follow a different structure than modern
  1306. * ClientHello messages. See [RFC5246](https://tools.ietf.org/html/rfc5246#appendix-E.2).
  1307. * To determine whether a ClientHello is an SSLv2 ClientHello,
  1308. * you will need to use s2n_connection_get_client_hello_version().
  1309. *
  1310. * @param ch The Client Hello handle
  1311. * @param out The destination buffer for the raw Client Hello cipher suites
  1312. * @param max_length The size of out in bytes
  1313. * @returns The number of copied bytes
  1314. */
  1315. S2N_API extern ssize_t s2n_client_hello_get_cipher_suites(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length);
  1316. /**
  1317. * Function to determine the size of the Client Hello extensions.
  1318. * This can be used to allocate the `out` buffer for s2n_client_hello_get_extensions().
  1319. *
  1320. * @param ch The Client Hello handle
  1321. * @returns the number of bytes the extensions take in the ClientHello message received by the server
  1322. */
  1323. S2N_API extern ssize_t s2n_client_hello_get_extensions_length(struct s2n_client_hello *ch);
  1324. /**
  1325. * Copies into the `out` buffer `max_length` bytes of the extensions in the ClientHello.
  1326. *
  1327. * @param ch The Client Hello handle
  1328. * @param out The destination buffer for the raw Client Hello extensions
  1329. * @param max_length The size of out in bytes
  1330. * @returns The number of copied bytes
  1331. */
  1332. S2N_API extern ssize_t s2n_client_hello_get_extensions(struct s2n_client_hello *ch, uint8_t *out, uint32_t max_length);
  1333. /**
  1334. * Query the ClientHello message received by the server. Use this function to allocate the `out` buffer for
  1335. * other client hello extension functions.
  1336. *
  1337. * @param ch A pointer to the Client Hello
  1338. * @param extension_type Indicates the desired extension
  1339. * @returns The number of bytes the given extension type takes
  1340. */
  1341. S2N_API extern ssize_t s2n_client_hello_get_extension_length(struct s2n_client_hello *ch, s2n_tls_extension_type extension_type);
  1342. /**
  1343. * Copies into the `out` buffer `max_length` bytes of a given extension type on the ClientHello
  1344. *
  1345. * `ch` is a pointer to the `s2n_client_hello` of the `s2n_connection` which can be obtained using s2n_connection_get_client_hello().
  1346. *
  1347. * @param ch A pointer to the Client Hello
  1348. * @param extension_type Indicates the desired extension
  1349. * @param out A pointer to the buffer that s2n will write the client session id to. This buffer MUST be the size of `max_length`
  1350. * @param max_length The size of `out`.
  1351. * @returns The number of copied bytes
  1352. */
  1353. S2N_API extern ssize_t s2n_client_hello_get_extension_by_id(struct s2n_client_hello *ch, s2n_tls_extension_type extension_type, uint8_t *out, uint32_t max_length);
  1354. /**
  1355. * Used to check if a particular extension exists in the client hello.
  1356. *
  1357. * `ch` is a pointer to the `s2n_client_hello` of the `s2n_connection` which can be obtained using s2n_connection_get_client_hello().
  1358. *
  1359. * @param ch A pointer to the client hello object
  1360. * @param extension_iana The iana value of the extension
  1361. * @param exists A pointer that will be set to whether or not the extension exists
  1362. */
  1363. S2N_API extern int s2n_client_hello_has_extension(struct s2n_client_hello *ch, uint16_t extension_iana, bool *exists);
  1364. /**
  1365. * Get the the ClientHello session id length in bytes
  1366. *
  1367. * `ch` is a pointer to the `s2n_client_hello` of the `s2n_connection` which can be obtained using s2n_connection_get_client_hello().
  1368. *
  1369. * @param ch A pointer to the Client Hello
  1370. * @param out_length An out pointer. s2n will set it's value to the size of the session_id in bytes.
  1371. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1372. */
  1373. S2N_API extern int s2n_client_hello_get_session_id_length(struct s2n_client_hello *ch, uint32_t *out_length);
  1374. /**
  1375. * Copies up to `max_length` bytes of the ClientHello session_id into the `out` buffer and stores the number of copied bytes in `out_length`.
  1376. *
  1377. * Retrieve the session id as sent by the client in the ClientHello message. The session id on the `s2n_connection` may change later
  1378. * when the server sends the ServerHello; see `s2n_connection_get_session_id` for how to get the final session id used for future session resumption.
  1379. *
  1380. * Use s2n_client_hello_get_session_id_length() to get the the ClientHello session id length in bytes. `ch` is a pointer to the `s2n_client_hello`
  1381. * of the `s2n_connection` which can be obtained using s2n_connection_get_client_hello().
  1382. *
  1383. * @param ch A pointer to the Client Hello
  1384. * @param out A pointer to the buffer that s2n will write the client session id to. This buffer MUST be the size of `max_length`
  1385. * @param out_length An out pointer. s2n will set it's value to the size of the session_id in bytes.
  1386. * @param max_length The size of `out`.
  1387. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1388. */
  1389. S2N_API extern int s2n_client_hello_get_session_id(struct s2n_client_hello *ch, uint8_t *out, uint32_t *out_length, uint32_t max_length);
  1390. /**
  1391. * Retrieves the supported groups received from the client in the supported groups extension.
  1392. *
  1393. * IANA values for each of the received supported groups are written to the provided `groups`
  1394. * array, and `groups_count` is set to the number of received supported groups.
  1395. *
  1396. * `groups_count_max` should be set to the maximum capacity of the `groups` array. If
  1397. * `groups_count_max` is less than the number of received supported groups, this function will
  1398. * error. To determine how large `groups` should be in advance, use
  1399. * `s2n_client_hello_get_extension_length()` with the S2N_EXTENSION_SUPPORTED_GROUPS extension
  1400. * type, and divide the value by 2.
  1401. *
  1402. * If no supported groups extension was received from the peer, or the received supported groups
  1403. * extension is malformed, this function will error.
  1404. *
  1405. * @param ch A pointer to the ClientHello. Can be retrieved from a connection via
  1406. * `s2n_connection_get_client_hello()`.
  1407. * @param groups The array to populate with the received supported groups.
  1408. * @param groups_count_max The maximum number of supported groups that can fit in the `groups` array.
  1409. * @param groups_count Returns the number of received supported groups.
  1410. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure.
  1411. */
  1412. S2N_API extern int s2n_client_hello_get_supported_groups(struct s2n_client_hello *ch, uint16_t *groups,
  1413. uint16_t groups_count_max, uint16_t *groups_count);
  1414. /**
  1415. * Sets the file descriptor for a s2n connection.
  1416. *
  1417. * @warning If the read end of the pipe is closed unexpectedly, writing to the pipe will raise a SIGPIPE signal.
  1418. * **s2n-tls does NOT handle SIGPIPE.** A SIGPIPE signal will cause the process to terminate unless it is handled
  1419. * or ignored by the application.
  1420. * @note This file-descriptor should be active and connected
  1421. * @param conn A pointer to the s2n connection
  1422. * @param fd The new file descriptor
  1423. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1424. */
  1425. S2N_API extern int s2n_connection_set_fd(struct s2n_connection *conn, int fd);
  1426. /**
  1427. * Sets the file descriptor for the read channel of an s2n connection.
  1428. *
  1429. * @warning If the read end of the pipe is closed unexpectedly, writing to the pipe will raise a SIGPIPE signal.
  1430. * **s2n-tls does NOT handle SIGPIPE.** A SIGPIPE signal will cause the process to terminate unless it is handled
  1431. * or ignored by the application.
  1432. * @note This file-descriptor should be active and connected
  1433. * @param conn A pointer to the s2n connection
  1434. * @param readfd The new read file descriptor
  1435. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1436. */
  1437. S2N_API extern int s2n_connection_set_read_fd(struct s2n_connection *conn, int readfd);
  1438. /**
  1439. * Sets the assigned file descriptor for the write channel of an s2n connection.
  1440. *
  1441. * @note This file-descriptor should be active and connected
  1442. * @param conn A pointer to the s2n connection
  1443. * @param writefd The new write file descriptor
  1444. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1445. */
  1446. S2N_API extern int s2n_connection_set_write_fd(struct s2n_connection *conn, int writefd);
  1447. /**
  1448. * Gets the assigned file descriptor for the read channel of an s2n connection.
  1449. *
  1450. * @param conn A pointer to the s2n connection
  1451. * @param readfd pointer to place the used file descriptor.
  1452. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1453. */
  1454. S2N_API extern int s2n_connection_get_read_fd(struct s2n_connection *conn, int *readfd);
  1455. /**
  1456. * Gets the assigned file descriptor for the write channel of an s2n connection.
  1457. *
  1458. * @param conn A pointer to the s2n connection
  1459. * @param writefd pointer to place the used file descriptor.
  1460. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1461. */
  1462. S2N_API extern int s2n_connection_get_write_fd(struct s2n_connection *conn, int *writefd);
  1463. /**
  1464. * Indicates to s2n that the connection is using corked IO.
  1465. *
  1466. * @warning This API should only be used when using managed send IO.
  1467. *
  1468. * @param conn The connection object being updated
  1469. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1470. */
  1471. S2N_API extern int s2n_connection_use_corked_io(struct s2n_connection *conn);
  1472. /**
  1473. * Function pointer for a user provided send callback.
  1474. */
  1475. typedef int s2n_recv_fn(void *io_context, uint8_t *buf, uint32_t len);
  1476. /**
  1477. * Function pointer for a user provided send callback.
  1478. */
  1479. typedef int s2n_send_fn(void *io_context, const uint8_t *buf, uint32_t len);
  1480. /**
  1481. * Set a context containing anything needed in the recv callback function (for example,
  1482. * a file descriptor), the buffer holding data to be sent or received, and the length of the buffer.
  1483. *
  1484. * @note The `io_context` passed to the callbacks may be set separately using `s2n_connection_set_recv_ctx` and `s2n_connection_set_send_ctx`.
  1485. *
  1486. * @param conn The connection object being updated
  1487. * @param ctx A user provided context that the callback will be invoked with
  1488. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1489. */
  1490. S2N_API extern int s2n_connection_set_recv_ctx(struct s2n_connection *conn, void *ctx);
  1491. /**
  1492. * Set a context containing anything needed in the send callback function (for example,
  1493. * a file descriptor), the buffer holding data to be sent or received, and the length of the buffer.
  1494. *
  1495. * @note The `io_context` passed to the callbacks may be set separately using `s2n_connection_set_recv_ctx` and `s2n_connection_set_send_ctx`.
  1496. *
  1497. * @param conn The connection object being updated
  1498. * @param ctx A user provided context that the callback will be invoked with
  1499. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1500. */
  1501. S2N_API extern int s2n_connection_set_send_ctx(struct s2n_connection *conn, void *ctx);
  1502. /**
  1503. * Configure a connection to use a recv callback to receive data.
  1504. *
  1505. * @note This callback may be blocking or nonblocking.
  1506. * @note The callback may receive less than the requested length. The function should return the number
  1507. * of bytes received, or set errno and return an error code < 0.
  1508. *
  1509. * @param conn The connection object being updated
  1510. * @param recv A recv callback function pointer
  1511. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1512. */
  1513. S2N_API extern int s2n_connection_set_recv_cb(struct s2n_connection *conn, s2n_recv_fn recv);
  1514. /**
  1515. * Configure a connection to use a send callback to send data.
  1516. *
  1517. * @note This callback may be blocking or nonblocking.
  1518. * @note The callback may send less than the requested length. The function should return the
  1519. * number of bytes sent or set errno and return an error code < 0.
  1520. *
  1521. * @param conn The connection object being updated
  1522. * @param send A send callback function pointer
  1523. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1524. */
  1525. S2N_API extern int s2n_connection_set_send_cb(struct s2n_connection *conn, s2n_send_fn send);
  1526. /**
  1527. * Change the behavior of s2n-tls when sending data to prefer high throughput.
  1528. *
  1529. * Connections preferring throughput will use
  1530. * large record sizes that minimize overhead.
  1531. *
  1532. * @param conn The connection object being updated
  1533. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1534. */
  1535. S2N_API extern int s2n_connection_prefer_throughput(struct s2n_connection *conn);
  1536. /**
  1537. * Change the behavior of s2n-tls when sending data to prefer low latency.
  1538. *
  1539. * Connections preferring low latency will be encrypted
  1540. * using small record sizes that can be decrypted sooner by the recipient.
  1541. *
  1542. * @param conn The connection object being updated
  1543. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1544. */
  1545. S2N_API extern int s2n_connection_prefer_low_latency(struct s2n_connection *conn);
  1546. /**
  1547. * Configure the connection to free IO buffers when they are not currently in use.
  1548. *
  1549. * This configuration can be used to minimize connection memory footprint size, at the cost
  1550. * of more calls to alloc and free. Some of these costs can be mitigated by configuring s2n-tls
  1551. * to use an allocator that includes thread-local caches or lock-free allocation patterns.
  1552. *
  1553. * @param conn The connection object being update
  1554. * @param enabled Set to `true` if dynamic buffers are enabled; `false` if disabled
  1555. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1556. */
  1557. S2N_API extern int s2n_connection_set_dynamic_buffers(struct s2n_connection *conn, bool enabled);
  1558. /**
  1559. * Changes the behavior of s2n-tls when sending data to initially prefer records
  1560. * small enough to fit in single ethernet frames.
  1561. *
  1562. * When dynamic record sizing is active, the connection sends records small enough
  1563. * to fit in a single standard 1500 byte ethernet frame. Otherwise, the connection
  1564. * chooses record sizes according to the configured maximum fragment length.
  1565. *
  1566. * Dynamic record sizing is active for the first resize_threshold bytes of a connection,
  1567. * and is reactivated whenever timeout_threshold seconds pass without sending data.
  1568. *
  1569. * @param conn The connection object being updated
  1570. * @param resize_threshold The number of bytes to send before changing the record size. Maximum 8MiB.
  1571. * @param timeout_threshold Reset record size back to a single segment after threshold seconds of inactivity
  1572. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1573. */
  1574. S2N_API extern int s2n_connection_set_dynamic_record_threshold(struct s2n_connection *conn, uint32_t resize_threshold, uint16_t timeout_threshold);
  1575. /**
  1576. * Sets the callback to use for verifying that a hostname from an X.509 certificate is trusted.
  1577. *
  1578. * The default behavior is to require that the hostname match the server name set with s2n_set_server_name(). This will
  1579. * likely lead to all client certificates being rejected, so the callback will need to be overriden when using client authentication.
  1580. *
  1581. * If a single callback for different connections using the same config is desired, see s2n_config_set_verify_host_callback().
  1582. *
  1583. * @param conn A pointer to a s2n_connection object
  1584. * @param host_fn A pointer to a callback function that s2n will invoke in order to verify the hostname of an X.509 certificate
  1585. * @param data Opaque pointer to data that the verify host function will be invoked with
  1586. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1587. */
  1588. S2N_API extern int s2n_connection_set_verify_host_callback(struct s2n_connection *conn, s2n_verify_host_fn host_fn, void *data);
  1589. /**
  1590. * Used to opt-out of s2n-tls's built-in blinding. Blinding is a
  1591. * mitigation against timing side-channels which in some cases can leak information
  1592. * about encrypted data. By default s2n-tls will cause a thread to sleep between 10 and
  1593. * 30 seconds whenever tampering is detected.
  1594. *
  1595. * Setting the S2N_SELF_SERVICE_BLINDING option with s2n_connection_set_blinding()
  1596. * turns off this behavior. This is useful for applications that are handling many connections
  1597. * in a single thread. In that case, if s2n_recv() or s2n_negotiate() return an error,
  1598. * self-service applications should call s2n_connection_get_delay() and pause
  1599. * activity on the connection for the specified number of nanoseconds before calling
  1600. * close() or shutdown().
  1601. */
  1602. typedef enum {
  1603. S2N_BUILT_IN_BLINDING,
  1604. S2N_SELF_SERVICE_BLINDING
  1605. } s2n_blinding;
  1606. /**
  1607. * Used to configure s2n-tls to either use built-in blinding (set blinding to S2N_BUILT_IN_BLINDING) or
  1608. * self-service blinding (set blinding to S2N_SELF_SERVICE_BLINDING).
  1609. *
  1610. * @param conn The connection object being updated
  1611. * @param blinding The desired blinding mode for the connection
  1612. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1613. */
  1614. S2N_API extern int s2n_connection_set_blinding(struct s2n_connection *conn, s2n_blinding blinding);
  1615. /**
  1616. * Query the connection object for the configured blinding delay.
  1617. * @param conn The connection object being updated
  1618. * @returns the number of nanoseconds an application using self-service blinding should pause before calling close() or shutdown().
  1619. */
  1620. S2N_API extern uint64_t s2n_connection_get_delay(struct s2n_connection *conn);
  1621. /**
  1622. * Sets the cipher preference override for the s2n_connection. Calling this function is not necessary
  1623. * unless you want to set the cipher preferences on the connection to something different than what is in the s2n_config.
  1624. *
  1625. * @param conn The connection object being updated
  1626. * @param version The human readable string representation of the security policy version.
  1627. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1628. */
  1629. S2N_API extern int s2n_connection_set_cipher_preferences(struct s2n_connection *conn, const char *version);
  1630. /**
  1631. * Appends the provided application protocol to the preference list
  1632. *
  1633. * The data provided in `protocol` parameter will be copied into an internal buffer
  1634. *
  1635. * @param conn The connection object being updated
  1636. * @param protocol A pointer to a slice of bytes
  1637. * @param protocol_len The length of bytes that should be read from `protocol`. Note: this value cannot be 0, otherwise an error will be returned.
  1638. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1639. */
  1640. S2N_API extern int s2n_connection_append_protocol_preference(struct s2n_connection *conn, const uint8_t *protocol, uint8_t protocol_len);
  1641. /**
  1642. * Sets the protocol preference override for the s2n_connection. Calling this function is not necessary unless you want
  1643. * to set the protocol preferences on the connection to something different than what is in the s2n_config.
  1644. *
  1645. * @param conn The connection object being updated
  1646. * @param protocols A pointer to an array of protocol strings
  1647. * @param protocol_count The number of protocols contained in protocols
  1648. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1649. */
  1650. S2N_API extern int s2n_connection_set_protocol_preferences(struct s2n_connection *conn, const char *const *protocols, int protocol_count);
  1651. /**
  1652. * Sets the server name for the connection.
  1653. *
  1654. * The provided server name will be sent by the client to the server in the
  1655. * server_name ClientHello extension. It may be desirable for clients
  1656. * to provide this information to facilitate secure connections to
  1657. * servers that host multiple 'virtual' servers at a single underlying
  1658. * network address.
  1659. *
  1660. * s2n-tls does not place any restrictions on the provided server name. However,
  1661. * other TLS implementations might. Specifically, the TLS specification for the
  1662. * server_name extension requires that it be an ASCII-encoded DNS name without a
  1663. * trailing dot, and explicitly forbids literal IPv4 or IPv6 addresses.
  1664. *
  1665. * @param conn The connection object being queried
  1666. * @param server_name A pointer to a string containing the desired server name
  1667. * @warning `server_name` must be a NULL terminated string.
  1668. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1669. */
  1670. S2N_API extern int s2n_set_server_name(struct s2n_connection *conn, const char *server_name);
  1671. /**
  1672. * Query the connection for the selected server name.
  1673. *
  1674. * This can be used by a server to determine which server name the client is using. This function returns the first ServerName entry
  1675. * in the ServerNameList sent by the client. Subsequent entries are not returned.
  1676. *
  1677. * @param conn The connection object being queried
  1678. * @returns The server name associated with a connection, or NULL if none is found.
  1679. */
  1680. S2N_API extern const char *s2n_get_server_name(struct s2n_connection *conn);
  1681. /**
  1682. * Query the connection for the selected application protocol.
  1683. *
  1684. * @param conn The connection object being queried
  1685. * @returns The negotiated application protocol for a `s2n_connection`. In the event of no protocol being negotiated, NULL is returned.
  1686. */
  1687. S2N_API extern const char *s2n_get_application_protocol(struct s2n_connection *conn);
  1688. /**
  1689. * Query the connection for a buffer containing the OCSP response.
  1690. *
  1691. * @param conn The connection object being queried
  1692. * @param length A pointer that is set to the certificate transparency response buffer's size
  1693. * @returns A pointer to the OCSP response sent by a server during the handshake. If no status response is received, NULL is returned.
  1694. */
  1695. S2N_API extern const uint8_t *s2n_connection_get_ocsp_response(struct s2n_connection *conn, uint32_t *length);
  1696. /**
  1697. * Query the connection for a buffer containing the Certificate Transparency response.
  1698. *
  1699. * @param conn The connection object being queried
  1700. * @param length A pointer that is set to the certificate transparency response buffer's size
  1701. * @returns A pointer to the certificate transparency response buffer.
  1702. */
  1703. S2N_API extern const uint8_t *s2n_connection_get_sct_list(struct s2n_connection *conn, uint32_t *length);
  1704. /**
  1705. * Used in non-blocking mode to indicate in which direction s2n-tls became blocked on I/O before it
  1706. * returned control to the caller. This allows an application to avoid retrying s2n-tls operations
  1707. * until I/O is possible in that direction.
  1708. */
  1709. typedef enum {
  1710. S2N_NOT_BLOCKED = 0,
  1711. S2N_BLOCKED_ON_READ,
  1712. S2N_BLOCKED_ON_WRITE,
  1713. S2N_BLOCKED_ON_APPLICATION_INPUT,
  1714. S2N_BLOCKED_ON_EARLY_DATA,
  1715. } s2n_blocked_status;
  1716. /**
  1717. * Performs the initial "handshake" phase of a TLS connection and must be called before any s2n_recv() or s2n_send() calls.
  1718. *
  1719. * @note When using client authentication with TLS1.3, s2n_negotiate() will report a successful
  1720. * handshake to clients before the server validates the client certificate. If the server then
  1721. * rejects the client certificate, the client may later receive an alert while calling s2n_recv,
  1722. * potentially after already having sent application data with s2n_send.
  1723. *
  1724. * See the following example for guidance on calling `s2n_negotiate()`:
  1725. * https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_negotiate.c
  1726. *
  1727. * @param conn A pointer to the s2n_connection object
  1728. * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned.
  1729. * @returns S2N_SUCCESS if the handshake completed. S2N_FAILURE if the handshake encountered an error or is blocked.
  1730. */
  1731. S2N_API extern int s2n_negotiate(struct s2n_connection *conn, s2n_blocked_status *blocked);
  1732. /**
  1733. * Writes and encrypts `size` of `buf` data to the associated connection. s2n_send() will return the number of bytes
  1734. * written, and may indicate a partial write.
  1735. *
  1736. * @note Partial writes are possible not just for non-blocking I/O, but also for connections aborted while active.
  1737. * @note Unlike OpenSSL, repeated calls to s2n_send() should not duplicate the original parameters, but should
  1738. * update `buf` and `size` per the indication of size written.
  1739. *
  1740. * See the following example for guidance on calling `s2n_send()`:
  1741. * https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_send.c
  1742. *
  1743. * @param conn A pointer to the s2n_connection object
  1744. * @param buf A pointer to a buffer that s2n will write data from
  1745. * @param size The size of buf
  1746. * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned.
  1747. * @returns The number of bytes written, and may indicate a partial write
  1748. */
  1749. S2N_API extern ssize_t s2n_send(struct s2n_connection *conn, const void *buf, ssize_t size, s2n_blocked_status *blocked);
  1750. /**
  1751. * Works in the same way as s2n_sendv_with_offset() but with the `offs` parameter implicitly assumed to be 0.
  1752. * Therefore in the partial write case, the caller would have to make sure that the `bufs` and `count` fields are modified in a way that takes
  1753. * the partial writes into account.
  1754. *
  1755. * @param conn A pointer to the s2n_connection object
  1756. * @param bufs A pointer to a vector of buffers that s2n will write data from.
  1757. * @param count The number of buffers in `bufs`
  1758. * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned.
  1759. * @returns The number of bytes written, and may indicate a partial write.
  1760. */
  1761. S2N_API extern ssize_t s2n_sendv(struct s2n_connection *conn, const struct iovec *bufs, ssize_t count, s2n_blocked_status *blocked);
  1762. /**
  1763. * Works in the same way as s2n_send() except that it accepts vectorized buffers. Will return the number of bytes written, and may indicate a partial write. Partial writes are possible not just for non-blocking I/O, but also for connections aborted while active.
  1764. *
  1765. * @note Partial writes are possible not just for non-blocking I/O, but also for connections aborted while active.
  1766. *
  1767. * @note Unlike OpenSSL, repeated calls to s2n_sendv_with_offset() should not duplicate the original parameters, but should update `bufs` and `count` per the indication of size written.
  1768. *
  1769. * See the following example for guidance on calling `s2n_sendv_with_offset()`:
  1770. * https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_send.c
  1771. *
  1772. * @param conn A pointer to the s2n_connection object
  1773. * @param bufs A pointer to a vector of buffers that s2n will write data from.
  1774. * @param count The number of buffers in `bufs`
  1775. * @param offs The write cursor offset. This should be updated as data is written. See the example code.
  1776. * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned.
  1777. * @returns The number of bytes written, and may indicate a partial write.
  1778. */
  1779. S2N_API extern ssize_t s2n_sendv_with_offset(struct s2n_connection *conn, const struct iovec *bufs, ssize_t count, ssize_t offs, s2n_blocked_status *blocked);
  1780. /**
  1781. * Decrypts and reads **size* to `buf` data from the associated
  1782. * connection.
  1783. *
  1784. * @note Unlike OpenSSL, repeated calls to `s2n_recv` should not duplicate the original parameters, but should update `buf` and `size` per the indication of size read.
  1785. *
  1786. * See the following example for guidance on calling `s2n_recv()`:
  1787. * https://github.com/aws/s2n-tls/blob/main/docs/examples/s2n_recv.c
  1788. *
  1789. * @param conn A pointer to the s2n_connection object
  1790. * @param buf A pointer to a buffer that s2n will place read data into.
  1791. * @param size Size of `buf`
  1792. * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned.
  1793. * @returns number of bytes read. 0 if the connection was shutdown by peer.
  1794. */
  1795. S2N_API extern ssize_t s2n_recv(struct s2n_connection *conn, void *buf, ssize_t size, s2n_blocked_status *blocked);
  1796. /**
  1797. * Allows users of s2n-tls to peek inside the data buffer of an s2n-tls connection to see if there more data to be read without actually reading it.
  1798. *
  1799. * This is useful when using select() on the underlying s2n-tls file descriptor with a message based application layer protocol. As a single call
  1800. * to s2n_recv may read all data off the underlying file descriptor, select() will be unable to tell you there if there is more application data
  1801. * ready for processing already loaded into the s2n-tls buffer.
  1802. *
  1803. * @note can then be used to determine if s2n_recv() needs to be called before more data comes in on the raw fd
  1804. * @param conn A pointer to the s2n_connection object
  1805. * @returns The number of bytes that can be read from the connection
  1806. */
  1807. S2N_API extern uint32_t s2n_peek(struct s2n_connection *conn);
  1808. /**
  1809. * Wipes and releases buffers and memory allocated during the TLS handshake.
  1810. *
  1811. * @note This function should be called after the handshake is successfully negotiated and logging or recording of handshake data is complete.
  1812. *
  1813. * @param conn A pointer to the s2n_connection object
  1814. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1815. */
  1816. S2N_API extern int s2n_connection_free_handshake(struct s2n_connection *conn);
  1817. /**
  1818. * Wipes and free the `in` and `out` buffers associated with a connection.
  1819. *
  1820. * @note This function may be called when a connection is
  1821. * in keep-alive or idle state to reduce memory overhead of long lived connections.
  1822. *
  1823. * @param conn A pointer to the s2n_connection object
  1824. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1825. */
  1826. S2N_API extern int s2n_connection_release_buffers(struct s2n_connection *conn);
  1827. /**
  1828. * Wipes an existing connection and allows it to be reused. Erases all data associated with a connection including
  1829. * pending reads.
  1830. *
  1831. * @note This function should be called after all I/O is completed and s2n_shutdown has been called.
  1832. * @note Reusing the same connection handle(s) is more performant than repeatedly calling s2n_connection_new() and s2n_connection_free().
  1833. *
  1834. * @param conn A pointer to the s2n_connection object
  1835. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1836. */
  1837. S2N_API extern int s2n_connection_wipe(struct s2n_connection *conn);
  1838. /**
  1839. * Frees the memory associated with an s2n_connection
  1840. * handle. The handle is considered invalid after `s2n_connection_free` is used.
  1841. * s2n_connection_wipe() does not need to be called prior to this function. `s2n_connection_free` performs its own wipe
  1842. * of sensitive data.
  1843. *
  1844. * @param conn A pointer to the s2n_connection object
  1845. * @returns 0 on success. -1 on failure
  1846. */
  1847. S2N_API extern int s2n_connection_free(struct s2n_connection *conn);
  1848. /**
  1849. * Attempts a closure at the TLS layer. Does not close the underlying transport. This call may block in either direction.
  1850. *
  1851. * Unlike other TLS implementations, `s2n_shutdown` attempts a graceful shutdown by default. It will not return with success unless a close_notify alert is successfully
  1852. * sent and received. As a result, `s2n_shutdown` may fail when interacting with a non-conformant TLS implementation.
  1853. *
  1854. * Once `s2n_shutdown` is complete:
  1855. * * The s2n_connection handle cannot be used for reading for writing.
  1856. * * The underlying transport can be closed. Most likely via `shutdown()` or `close()`.
  1857. * * The s2n_connection handle can be freed via s2n_connection_free() or reused via s2n_connection_wipe()
  1858. *
  1859. * @param conn A pointer to the s2n_connection object
  1860. * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned.
  1861. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1862. */
  1863. S2N_API extern int s2n_shutdown(struct s2n_connection *conn, s2n_blocked_status *blocked);
  1864. /**
  1865. * Attempts to close the write side of the TLS connection.
  1866. *
  1867. * TLS1.3 supports closing the write side of a TLS connection while leaving the read
  1868. * side unaffected. This feature is usually referred to as "half-close". We send
  1869. * a close_notify alert, but do not wait for the peer to respond.
  1870. *
  1871. * Like `s2n_shutdown()`, this method does not affect the underlying transport.
  1872. *
  1873. * `s2n_shutdown_send()` may still be called for earlier TLS versions, but most
  1874. * TLS implementations will react by immediately discarding any pending writes and
  1875. * closing the connection.
  1876. *
  1877. * Once `s2n_shutdown_send()` is complete:
  1878. * * The s2n_connection handle CANNOT be used for writing.
  1879. * * The s2n_connection handle CAN be used for reading.
  1880. * * The write side of the underlying transport can be closed. Most likely via `shutdown()`.
  1881. *
  1882. * The application should still call `s2n_shutdown()` or wait for `s2n_recv()` to
  1883. * return 0 to indicate end-of-data before cleaning up the connection or closing
  1884. * the read side of the underlying transport.
  1885. *
  1886. * @param conn A pointer to the s2n_connection object
  1887. * @param blocked A pointer which will be set to the blocked status if an `S2N_ERR_T_BLOCKED` error is returned.
  1888. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1889. */
  1890. S2N_API extern int s2n_shutdown_send(struct s2n_connection *conn, s2n_blocked_status *blocked);
  1891. /**
  1892. * Used to declare what type of client certificate authentication to use.
  1893. *
  1894. * Currently the default for s2n-tls is for neither the server side or the client side to use Client (aka Mutual) authentication.
  1895. */
  1896. typedef enum {
  1897. S2N_CERT_AUTH_NONE,
  1898. S2N_CERT_AUTH_REQUIRED,
  1899. S2N_CERT_AUTH_OPTIONAL
  1900. } s2n_cert_auth_type;
  1901. /**
  1902. * Gets Client Certificate authentication method the s2n_config object is using.
  1903. *
  1904. * @param config A pointer to a s2n_config object
  1905. * @param client_auth_type A pointer to a client auth policy. This will be updated to the s2n_config value.
  1906. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1907. */
  1908. S2N_API extern int s2n_config_get_client_auth_type(struct s2n_config *config, s2n_cert_auth_type *client_auth_type);
  1909. /**
  1910. * Sets whether or not a Client Certificate should be required to complete the TLS Connection.
  1911. *
  1912. * If this is set to `S2N_CERT_AUTH_OPTIONAL` the server will request a client certificate but allow the client to not provide one.
  1913. * Rejecting a client certificate when using `S2N_CERT_AUTH_OPTIONAL` will terminate the handshake.
  1914. *
  1915. * @param config A pointer to a s2n_config object
  1916. * @param client_auth_type The client auth policy for the connection
  1917. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1918. */
  1919. S2N_API extern int s2n_config_set_client_auth_type(struct s2n_config *config, s2n_cert_auth_type client_auth_type);
  1920. /**
  1921. * Gets Client Certificate authentication method the s2n_connection object is using.
  1922. *
  1923. * @param conn A pointer to the s2n_connection object
  1924. * @param client_auth_type A pointer to a client auth policy. This will be updated to the s2n_connection value.
  1925. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1926. */
  1927. S2N_API extern int s2n_connection_get_client_auth_type(struct s2n_connection *conn, s2n_cert_auth_type *client_auth_type);
  1928. /**
  1929. * Sets whether or not a Client Certificate should be required to complete the TLS Connection.
  1930. *
  1931. * If this is set to `S2N_CERT_AUTH_OPTIONAL` the server will request a client certificate but allow the client to not provide one.
  1932. * Rejecting a client certificate when using `S2N_CERT_AUTH_OPTIONAL` will terminate the handshake.
  1933. *
  1934. * @param conn A pointer to the s2n_connection object
  1935. * @param client_auth_type The client auth policy for the connection
  1936. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1937. */
  1938. S2N_API extern int s2n_connection_set_client_auth_type(struct s2n_connection *conn, s2n_cert_auth_type client_auth_type);
  1939. /**
  1940. * Gets the raw certificate chain received from the client.
  1941. *
  1942. * The retrieved certificate chain has the format described by the TLS 1.2 RFC:
  1943. * https://datatracker.ietf.org/doc/html/rfc5246#section-7.4.2. Each certificate is a DER-encoded ASN.1 X.509,
  1944. * prepended by a 3 byte network-endian length value. Note that this format is used regardless of the connection's
  1945. * protocol version.
  1946. *
  1947. * @warning The buffer pointed to by `cert_chain_out` shares its lifetime with the s2n_connection object.
  1948. *
  1949. * @param conn A pointer to the s2n_connection object
  1950. * @param cert_chain_out A pointer that's set to the client certificate chain.
  1951. * @param cert_chain_len A pointer that's set to the size of the `cert_chain_out` buffer.
  1952. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1953. */
  1954. S2N_API extern int s2n_connection_get_client_cert_chain(struct s2n_connection *conn, uint8_t **der_cert_chain_out, uint32_t *cert_chain_len);
  1955. /**
  1956. * Sets the initial number of session tickets to send after a >=TLS1.3 handshake. The default value is one ticket.
  1957. *
  1958. * @param config A pointer to the config object.
  1959. * @param num The number of session tickets that will be sent.
  1960. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1961. */
  1962. S2N_API extern int s2n_config_set_initial_ticket_count(struct s2n_config *config, uint8_t num);
  1963. /**
  1964. * Increases the number of session tickets to send after a >=TLS1.3 handshake.
  1965. *
  1966. * @param conn A pointer to the connection object.
  1967. * @param num The number of additional session tickets to send.
  1968. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1969. */
  1970. S2N_API extern int s2n_connection_add_new_tickets_to_send(struct s2n_connection *conn, uint8_t num);
  1971. /**
  1972. * Returns the number of session tickets issued by the server.
  1973. *
  1974. * In TLS1.3, this number can be up to the limit configured by s2n_config_set_initial_ticket_count
  1975. * and s2n_connection_add_new_tickets_to_send. In earlier versions of TLS, this number will be either 0 or 1.
  1976. *
  1977. * This method only works for server connections.
  1978. *
  1979. * @param conn A pointer to the connection object.
  1980. * @param num The number of additional session tickets sent.
  1981. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1982. */
  1983. S2N_API extern int s2n_connection_get_tickets_sent(struct s2n_connection *conn, uint16_t *num);
  1984. /**
  1985. * Sets the keying material lifetime for >=TLS1.3 session tickets so that one session doesn't get re-used ad infinitum.
  1986. * The default value is one week.
  1987. *
  1988. * @param conn A pointer to the connection object.
  1989. * @param lifetime_in_secs Lifetime of keying material in seconds.
  1990. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1991. */
  1992. S2N_API extern int s2n_connection_set_server_keying_material_lifetime(struct s2n_connection *conn, uint32_t lifetime_in_secs);
  1993. struct s2n_session_ticket;
  1994. /**
  1995. * Callback function for receiving a session ticket.
  1996. *
  1997. * This function will be called each time a session ticket is received, which may be multiple times for TLS1.3.
  1998. *
  1999. * # Safety
  2000. *
  2001. * `ctx` is a void pointer and the caller is responsible for ensuring it is cast to the correct type.
  2002. * `ticket` is valid only within the scope of this callback.
  2003. *
  2004. * @param conn A pointer to the connection object.
  2005. * @param ctx Context for the session ticket callback function.
  2006. * @param ticket Pointer to the received session ticket object.
  2007. */
  2008. typedef int (*s2n_session_ticket_fn)(struct s2n_connection *conn, void *ctx, struct s2n_session_ticket *ticket);
  2009. /**
  2010. * Sets a session ticket callback to be called when a client receives a new session ticket.
  2011. *
  2012. * # Safety
  2013. *
  2014. * `callback` MUST cast `ctx` into the same type of pointer that was originally created.
  2015. * `ctx` MUST be valid for the lifetime of the config, or until a different context is set.
  2016. *
  2017. * @param config A pointer to the config object.
  2018. * @param callback The function that should be called when the callback is triggered.
  2019. * @param ctx The context to be passed when the callback is called.
  2020. */
  2021. S2N_API extern int s2n_config_set_session_ticket_cb(struct s2n_config *config, s2n_session_ticket_fn callback, void *ctx);
  2022. /**
  2023. * Gets the length of the session ticket from a session ticket object.
  2024. *
  2025. * @param ticket Pointer to the session ticket object.
  2026. * @param data_len Pointer to be set to the length of the session ticket on success.
  2027. */
  2028. S2N_API extern int s2n_session_ticket_get_data_len(struct s2n_session_ticket *ticket, size_t *data_len);
  2029. /**
  2030. * Gets the session ticket data from a session ticket object.
  2031. *
  2032. * # Safety
  2033. * The entire session ticket will be copied into `data` on success. Therefore, `data` MUST have enough
  2034. * memory to store the session ticket data.
  2035. *
  2036. * @param ticket Pointer to the session ticket object.
  2037. * @param max_data_len Maximum length of data that can be written to the 'data' pointer.
  2038. * @param data Pointer to where the session ticket data will be stored.
  2039. */
  2040. S2N_API extern int s2n_session_ticket_get_data(struct s2n_session_ticket *ticket, size_t max_data_len, uint8_t *data);
  2041. /**
  2042. * Gets the lifetime in seconds of the session ticket from a session ticket object.
  2043. *
  2044. * @param ticket Pointer to the session ticket object.
  2045. * @param session_lifetime Pointer to a variable where the lifetime of the session ticket will be stored.
  2046. */
  2047. S2N_API extern int s2n_session_ticket_get_lifetime(struct s2n_session_ticket *ticket, uint32_t *session_lifetime);
  2048. /**
  2049. * De-serializes the session state and updates the connection accordingly.
  2050. *
  2051. * If this method fails, the connection should not be affected: calling s2n_negotiate
  2052. * with the connection should simply result in a full handshake.
  2053. *
  2054. * @param conn A pointer to the s2n_connection object
  2055. * @param session A pointer to a buffer of size `length`
  2056. * @param length The size of the `session` buffer
  2057. *
  2058. * @returns The number of copied bytes
  2059. */
  2060. S2N_API extern int s2n_connection_set_session(struct s2n_connection *conn, const uint8_t *session, size_t length);
  2061. /**
  2062. * Serializes the session state from connection and copies into the `session` buffer and returns the number of copied bytes
  2063. *
  2064. * @note This function is not recommended for > TLS 1.2 because in TLS1.3
  2065. * servers can send multiple session tickets and this function will only
  2066. * return the most recently received ticket.
  2067. *
  2068. * @param conn A pointer to the s2n_connection object
  2069. * @param session A pointer to a buffer of size `max_length`
  2070. * @param max_length The size of the `session` buffer
  2071. *
  2072. * @returns The number of copied bytes
  2073. */
  2074. S2N_API extern int s2n_connection_get_session(struct s2n_connection *conn, uint8_t *session, size_t max_length);
  2075. /**
  2076. * Retrieves a hint from the server indicating how long this ticket's lifetime is.
  2077. *
  2078. * @note This function is not recommended for > TLS 1.2 because in TLS1.3
  2079. * servers can send multiple session tickets and this function will only
  2080. * return the most recently received ticket lifetime hint.
  2081. *
  2082. * @param conn A pointer to the s2n_connection object
  2083. *
  2084. * @returns The session ticket lifetime hint in seconds from the server or -1 when session ticket was not used for resumption.
  2085. */
  2086. S2N_API extern int s2n_connection_get_session_ticket_lifetime_hint(struct s2n_connection *conn);
  2087. /**
  2088. * Use this to query the serialized session state size before copying it into a buffer.
  2089. *
  2090. * @param conn A pointer to the s2n_connection object
  2091. *
  2092. * @returns number of bytes needed to store serialized session state
  2093. */
  2094. S2N_API extern int s2n_connection_get_session_length(struct s2n_connection *conn);
  2095. /**
  2096. * Gets the latest session id's length from the connection.
  2097. *
  2098. * Use this to query the session id size before copying it into a buffer.
  2099. *
  2100. * @param conn A pointer to the s2n_connection object
  2101. *
  2102. * @returns The latest session id length from the connection. Session id length will be 0 for TLS versions >= TLS1.3 as stateful session resumption has not yet been implemented in TLS1.3.
  2103. */
  2104. S2N_API extern int s2n_connection_get_session_id_length(struct s2n_connection *conn);
  2105. /**
  2106. * Gets the latest session id from the connection, copies it into the `session_id` buffer, and returns the number of copied bytes.
  2107. *
  2108. * The session id may change between s2n receiving the ClientHello and sending the ServerHello, but this function will always describe the latest session id.
  2109. *
  2110. * See s2n_client_hello_get_session_id() to get the session id as it was sent by the client in the ClientHello message.
  2111. *
  2112. * @param conn A pointer to the s2n_connection object
  2113. * @param session_id A pointer to a buffer of size `max_length`
  2114. * @param max_length The size of the `session_id` buffer
  2115. *
  2116. * @returns The number of copied bytes.
  2117. */
  2118. S2N_API extern int s2n_connection_get_session_id(struct s2n_connection *conn, uint8_t *session_id, size_t max_length);
  2119. /**
  2120. * Check if the connection was resumed from an earlier handshake.
  2121. *
  2122. * @param conn A pointer to the s2n_connection object
  2123. *
  2124. * @returns returns 1 if the handshake was abbreviated, otherwise returns 0
  2125. */
  2126. S2N_API extern int s2n_connection_is_session_resumed(struct s2n_connection *conn);
  2127. /**
  2128. * Check if the connection is OCSP stapled.
  2129. *
  2130. * @param conn A pointer to the s2n_connection object
  2131. *
  2132. * @returns 1 if OCSP response was sent (if connection is in S2N_SERVER mode) or received (if connection is in S2N_CLIENT mode) during handshake, otherwise it returns 0.
  2133. */
  2134. S2N_API extern int s2n_connection_is_ocsp_stapled(struct s2n_connection *conn);
  2135. /**
  2136. * TLS Signature Algorithms - RFC 5246 7.4.1.4.1
  2137. * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-16
  2138. */
  2139. typedef enum {
  2140. S2N_TLS_SIGNATURE_ANONYMOUS = 0,
  2141. S2N_TLS_SIGNATURE_RSA = 1,
  2142. S2N_TLS_SIGNATURE_ECDSA = 3,
  2143. /* Use Private Range for RSA PSS since it's not defined there */
  2144. S2N_TLS_SIGNATURE_RSA_PSS_RSAE = 224,
  2145. S2N_TLS_SIGNATURE_RSA_PSS_PSS
  2146. } s2n_tls_signature_algorithm;
  2147. /** TLS Hash Algorithms - https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  2148. * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-18
  2149. */
  2150. typedef enum {
  2151. S2N_TLS_HASH_NONE = 0,
  2152. S2N_TLS_HASH_MD5 = 1,
  2153. S2N_TLS_HASH_SHA1 = 2,
  2154. S2N_TLS_HASH_SHA224 = 3,
  2155. S2N_TLS_HASH_SHA256 = 4,
  2156. S2N_TLS_HASH_SHA384 = 5,
  2157. S2N_TLS_HASH_SHA512 = 6,
  2158. /* Use Private Range for MD5_SHA1 */
  2159. S2N_TLS_HASH_MD5_SHA1 = 224
  2160. } s2n_tls_hash_algorithm;
  2161. /**
  2162. * Get the connection's selected signature algorithm.
  2163. *
  2164. * @param conn A pointer to the s2n_connection object
  2165. * @param chosen_alg A pointer to a s2n_tls_signature_algorithm object. This is an output parameter.
  2166. *
  2167. * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received.
  2168. */
  2169. S2N_API extern int s2n_connection_get_selected_signature_algorithm(struct s2n_connection *conn, s2n_tls_signature_algorithm *chosen_alg);
  2170. /**
  2171. * Get the connection's selected digest algorithm.
  2172. *
  2173. * @param conn A pointer to the s2n_connection object
  2174. * @param chosen_alg A pointer to a s2n_tls_hash_algorithm object. This is an output parameter.
  2175. *
  2176. * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received.
  2177. */
  2178. S2N_API extern int s2n_connection_get_selected_digest_algorithm(struct s2n_connection *conn, s2n_tls_hash_algorithm *chosen_alg);
  2179. /**
  2180. * Get the client certificate's signature algorithm.
  2181. *
  2182. * @param conn A pointer to the s2n_connection object
  2183. * @param chosen_alg A pointer to a s2n_tls_signature_algorithm object. This is an output parameter.
  2184. *
  2185. * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received.
  2186. */
  2187. S2N_API extern int s2n_connection_get_selected_client_cert_signature_algorithm(struct s2n_connection *conn, s2n_tls_signature_algorithm *chosen_alg);
  2188. /**
  2189. * Get the client certificate's digest algorithm.
  2190. *
  2191. * @param conn A pointer to the s2n_connection object
  2192. * @param chosen_alg A pointer to a s2n_tls_hash_algorithm object. This is an output parameter.
  2193. *
  2194. * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received.
  2195. */
  2196. S2N_API extern int s2n_connection_get_selected_client_cert_digest_algorithm(struct s2n_connection *conn, s2n_tls_hash_algorithm *chosen_alg);
  2197. /**
  2198. * Get the certificate used during the TLS handshake
  2199. *
  2200. * - If `conn` is a server connection, the certificate selected will depend on the
  2201. * ServerName sent by the client and supported ciphers.
  2202. * - If `conn` is a client connection, the certificate sent in response to a CertificateRequest
  2203. * message is returned. Currently s2n-tls supports loading only one certificate in client mode. Note that
  2204. * not all TLS endpoints will request a certificate.
  2205. *
  2206. * @param conn A pointer to the s2n_connection object
  2207. *
  2208. * @returns NULL if the certificate selection phase of the handshake has not completed or if a certificate was not requested by the peer
  2209. */
  2210. S2N_API extern struct s2n_cert_chain_and_key *s2n_connection_get_selected_cert(struct s2n_connection *conn);
  2211. /**
  2212. * @param chain_and_key A pointer to the s2n_cert_chain_and_key object being read.
  2213. * @param cert_length This return value represents the length of the s2n certificate chain `chain_and_key`.
  2214. * @returns the length of the s2n certificate chain `chain_and_key`.
  2215. */
  2216. S2N_API extern int s2n_cert_chain_get_length(const struct s2n_cert_chain_and_key *chain_and_key, uint32_t *cert_length);
  2217. /**
  2218. * Returns the certificate `out_cert` present at the index `cert_idx` of the certificate chain `chain_and_key`.
  2219. *
  2220. * Note that the index of the leaf certificate is zero. If the certificate chain `chain_and_key` is NULL or the
  2221. * certificate index value is not in the acceptable range for the input certificate chain, an error is returned.
  2222. *
  2223. * # Safety
  2224. *
  2225. * There is no memory allocation required for `out_cert` buffer prior to calling the `s2n_cert_chain_get_cert` API.
  2226. * The `out_cert` will contain the pointer to the s2n_cert initialized within the input s2n_cert_chain_and_key `chain_and_key`.
  2227. * The pointer to the output s2n certificate `out_cert` is valid until `chain_and_key` is freed up.
  2228. * If a caller wishes to persist the `out_cert` beyond the lifetime of `chain_and_key`, the contents would need to be
  2229. * copied prior to freeing `chain_and_key`.
  2230. *
  2231. * @param chain_and_key A pointer to the s2n_cert_chain_and_key object being read.
  2232. * @param out_cert A pointer to the output s2n_cert `out_cert` present at the index `cert_idx` of the certificate chain `chain_and_key`.
  2233. * @param cert_idx The certificate index for the requested certificate within the s2n certificate chain.
  2234. */
  2235. S2N_API extern int s2n_cert_chain_get_cert(const struct s2n_cert_chain_and_key *chain_and_key, struct s2n_cert **out_cert, const uint32_t cert_idx);
  2236. /**
  2237. * Returns the s2n certificate in DER format along with its length.
  2238. *
  2239. * The API gets the s2n certificate `cert` in DER format. The certificate is returned in the `out_cert_der` buffer.
  2240. * Here, `cert_len` represents the length of the certificate.
  2241. *
  2242. * A caller can use certificate parsing tools such as the ones provided by OpenSSL to parse the DER encoded certificate chain returned.
  2243. *
  2244. * # Safety
  2245. *
  2246. * The memory for the `out_cert_der` buffer is allocated and owned by s2n-tls.
  2247. * Since the size of the certificate can potentially be very large, a pointer to internal connection data is returned instead of
  2248. * copying the contents into a caller-provided buffer.
  2249. *
  2250. * The pointer to the output buffer `out_cert_der` is valid only while the connection exists.
  2251. * The `s2n_connection_free` API frees the memory associated with the out_cert_der buffer and after the `s2n_connection_wipe` API is
  2252. * called the memory pointed by out_cert_der is invalid.
  2253. *
  2254. * If a caller wishes to persist the `out_cert_der` beyond the lifetime of the connection, the contents would need to be
  2255. * copied prior to the connection termination.
  2256. *
  2257. * @param cert A pointer to the s2n_cert object being read.
  2258. * @param out_cert_der A pointer to the output buffer which will hold the s2n certificate `cert` in DER format.
  2259. * @param cert_length This return value represents the length of the certificate.
  2260. */
  2261. S2N_API extern int s2n_cert_get_der(const struct s2n_cert *cert, const uint8_t **out_cert_der, uint32_t *cert_length);
  2262. /**
  2263. * Returns the validated peer certificate chain as a `s2n_cert_chain_and_key` opaque object.
  2264. *
  2265. * The `s2n_cert_chain_and_key` parameter must be allocated by the caller using the `s2n_cert_chain_and_key_new` API
  2266. * prior to this function call and must be empty. To free the memory associated with the `s2n_cert_chain_and_key` object use the
  2267. * `s2n_cert_chain_and_key_free` API.
  2268. *
  2269. * @param conn A pointer to the s2n_connection object being read.
  2270. * @param cert_chain The returned validated peer certificate chain `cert_chain` retrieved from the s2n connection.
  2271. */
  2272. S2N_API extern int s2n_connection_get_peer_cert_chain(const struct s2n_connection *conn, struct s2n_cert_chain_and_key *cert_chain);
  2273. /**
  2274. * Returns the length of the DER encoded extension value of the ASN.1 X.509 certificate extension.
  2275. *
  2276. * @param cert A pointer to the s2n_cert object being read.
  2277. * @param oid A null-terminated cstring that contains the OID of the X.509 certificate extension to be read.
  2278. * @param ext_value_len This return value contains the length of DER encoded extension value of the ASN.1 X.509 certificate extension.
  2279. */
  2280. S2N_API extern int s2n_cert_get_x509_extension_value_length(struct s2n_cert *cert, const uint8_t *oid, uint32_t *ext_value_len);
  2281. /**
  2282. * Returns the DER encoding of an ASN.1 X.509 certificate extension value, it's length and a boolean critical.
  2283. *
  2284. * @param cert A pointer to the s2n_cert object being read.
  2285. * @param oid A null-terminated cstring that contains the OID of the X.509 certificate extension to be read.
  2286. * @param ext_value A pointer to the output buffer which will hold the DER encoding of an ASN.1 X.509 certificate extension value returned.
  2287. * @param ext_value_len This value is both an input and output parameter and represents the length of the output buffer `ext_value`.
  2288. * When used as an input parameter, the caller must use this parameter to convey the maximum length of `ext_value`.
  2289. * When used as an output parameter, `ext_value_len` holds the actual length of the DER encoding of the ASN.1 X.509 certificate extension value returned.
  2290. * @param critical This return value contains the boolean value for `critical`.
  2291. */
  2292. S2N_API extern int s2n_cert_get_x509_extension_value(struct s2n_cert *cert, const uint8_t *oid, uint8_t *ext_value, uint32_t *ext_value_len, bool *critical);
  2293. /**
  2294. * Returns the UTF8 String length of the ASN.1 X.509 certificate extension data.
  2295. *
  2296. * @param extension_data A pointer to the DER encoded ASN.1 X.509 certificate extension value being read.
  2297. * @param extension_len represents the length of the input buffer `extension_data`.
  2298. * @param utf8_str_len This return value contains the UTF8 String length of the ASN.1 X.509 certificate extension data.
  2299. */
  2300. S2N_API extern int s2n_cert_get_utf8_string_from_extension_data_length(const uint8_t *extension_data, uint32_t extension_len, uint32_t *utf8_str_len);
  2301. /**
  2302. * Returns the UTF8 String representation of the DER encoded ASN.1 X.509 certificate extension data.
  2303. *
  2304. * @param extension_data A pointer to the DER encoded ASN.1 X.509 certificate extension value being read.
  2305. * @param extension_len represents the length of the input buffer `extension_data`.
  2306. * @param out_data A pointer to the output buffer which will hold the UTF8 String representation of the DER encoded ASN.1 X.509
  2307. * certificate extension data returned.
  2308. * @param out_len This value is both an input and output parameter and represents the length of the output buffer `out_data`.
  2309. * When used as an input parameter, the caller must use this parameter to convey the maximum length of `out_data`.
  2310. * When used as an output parameter, `out_len` holds the actual length of UTF8 String returned.
  2311. */
  2312. S2N_API extern int s2n_cert_get_utf8_string_from_extension_data(const uint8_t *extension_data, uint32_t extension_len, uint8_t *out_data, uint32_t *out_len);
  2313. /**
  2314. * Pre-shared key (PSK) Hash Algorithm - RFC 8446 Section-2.2
  2315. */
  2316. typedef enum {
  2317. S2N_PSK_HMAC_SHA256,
  2318. S2N_PSK_HMAC_SHA384,
  2319. } s2n_psk_hmac;
  2320. /**
  2321. * Opaque pre shared key handle
  2322. */
  2323. struct s2n_psk;
  2324. /**
  2325. * Creates a new s2n external pre-shared key (PSK) object with `S2N_PSK_HMAC_SHA256` as the default
  2326. * PSK hash algorithm. An external PSK is a key established outside of TLS using a secure mutually agreed upon mechanism.
  2327. *
  2328. * Use `s2n_psk_free` to free the memory allocated to the s2n external PSK object created by this API.
  2329. *
  2330. * @returns struct s2n_psk* Returns a pointer to the newly created external PSK object.
  2331. */
  2332. S2N_API struct s2n_psk *s2n_external_psk_new(void);
  2333. /**
  2334. * Frees the memory associated with the external PSK object.
  2335. *
  2336. * @param psk Pointer to the PSK object to be freed.
  2337. */
  2338. S2N_API int s2n_psk_free(struct s2n_psk **psk);
  2339. /**
  2340. * Sets the identity for a given external PSK object.
  2341. * The identity is a unique identifier for the pre-shared secret.
  2342. * It is a non-secret value represented by raw bytes.
  2343. *
  2344. * # Safety
  2345. *
  2346. * The identity is transmitted over the network unencrypted and is a non-secret value.
  2347. * Do not include confidential information in the identity.
  2348. *
  2349. * Note that the identity is copied into s2n-tls memory and the caller is responsible for
  2350. * freeing the memory associated with the identity input.
  2351. *
  2352. * @param psk A pointer to a PSK object to be updated with the identity.
  2353. * @param identity The identity in raw bytes format to be copied.
  2354. * @param identity_size The length of the PSK identity being set.
  2355. */
  2356. S2N_API int s2n_psk_set_identity(struct s2n_psk *psk, const uint8_t *identity, uint16_t identity_size);
  2357. /**
  2358. * Sets the out-of-band/externally provisioned secret for a given external PSK object.
  2359. *
  2360. * # Safety
  2361. *
  2362. * Note that the secret is copied into s2n-tls memory and the caller is responsible for
  2363. * freeing the memory associated with the `secret` input.
  2364. *
  2365. * Deriving a shared secret from a password or other low-entropy source
  2366. * is not secure and is subject to dictionary attacks.
  2367. * See https://tools.ietf.org/rfc/rfc8446#section-2.2 for more information.
  2368. *
  2369. * @param psk A pointer to a PSK object to be updated with the secret.
  2370. * @param secret The secret in raw bytes format to be copied.
  2371. * @param secret_size The length of the pre-shared secret being set.
  2372. */
  2373. S2N_API int s2n_psk_set_secret(struct s2n_psk *psk, const uint8_t *secret, uint16_t secret_size);
  2374. /**
  2375. * Sets the hash algorithm for a given external PSK object. The supported PSK hash
  2376. * algorithms are as listed in the enum `s2n_psk_hmac` above.
  2377. *
  2378. * @param psk A pointer to the external PSK object to be updated with the PSK hash algorithm.
  2379. * @param hmac The PSK hash algorithm being set.
  2380. */
  2381. S2N_API int s2n_psk_set_hmac(struct s2n_psk *psk, s2n_psk_hmac hmac);
  2382. /**
  2383. * Appends a PSK object to the list of PSKs supported by the s2n connection.
  2384. * If a PSK with a duplicate identity is found, an error is returned and the PSK is not added to the list.
  2385. * Note that a copy of `psk` is stored on the connection. The user is still responsible for freeing the
  2386. * memory associated with `psk`.
  2387. *
  2388. * @param conn A pointer to the s2n_connection object that contains the list of PSKs supported.
  2389. * @param psk A pointer to the `s2n_psk` object to be appended to the list of PSKs on the s2n connection.
  2390. */
  2391. S2N_API int s2n_connection_append_psk(struct s2n_connection *conn, struct s2n_psk *psk);
  2392. /**
  2393. * The list of PSK modes supported by s2n-tls for TLS versions >= TLS1.3.
  2394. * Currently s2n-tls supports two modes - `S2N_PSK_MODE_RESUMPTION`, which represents the PSKs established
  2395. * using the previous connection via session resumption, and `S2N_PSK_MODE_EXTERNAL`, which represents PSKs
  2396. * established out-of-band/externally using a secure mutually agreed upon mechanism.
  2397. */
  2398. typedef enum {
  2399. S2N_PSK_MODE_RESUMPTION,
  2400. S2N_PSK_MODE_EXTERNAL
  2401. } s2n_psk_mode;
  2402. /**
  2403. * Sets the PSK mode on the s2n config object.
  2404. * The supported PSK modes are listed in the enum `s2n_psk_mode` above.
  2405. *
  2406. * @param config A pointer to the s2n_config object being updated.
  2407. * @param mode The PSK mode to be set.
  2408. */
  2409. S2N_API int s2n_config_set_psk_mode(struct s2n_config *config, s2n_psk_mode mode);
  2410. /**
  2411. * Sets the PSK mode on the s2n connection object.
  2412. * The supported PSK modes are listed in the enum `s2n_psk_mode` above.
  2413. * This API overrides the PSK mode set on config for this connection.
  2414. *
  2415. * @param conn A pointer to the s2n_connection object being updated.
  2416. * @param mode The PSK mode to be set.
  2417. */
  2418. S2N_API int s2n_connection_set_psk_mode(struct s2n_connection *conn, s2n_psk_mode mode);
  2419. /**
  2420. * Gets the negotiated PSK identity length from the s2n connection object. The negotiated PSK
  2421. * refers to the chosen PSK by the server to be used for the connection.
  2422. *
  2423. * This API can be used to determine if the negotiated PSK exists. If negotiated PSK exists a
  2424. * call to this API returns a value greater than zero. If the negotiated PSK does not exist, the
  2425. * value `0` is returned.
  2426. *
  2427. * @param conn A pointer to the s2n_connection object that successfully negotiated a PSK connection.
  2428. * @param identity_length The length of the negotiated PSK identity.
  2429. */
  2430. S2N_API int s2n_connection_get_negotiated_psk_identity_length(struct s2n_connection *conn, uint16_t *identity_length);
  2431. /**
  2432. * Gets the negotiated PSK identity from the s2n connection object.
  2433. * If the negotiated PSK does not exist, the PSK identity will not be obtained and no error will be returned.
  2434. * Prior to this API call, use `s2n_connection_get_negotiated_psk_identity_length` to determine if a
  2435. * negotiated PSK exists or not.
  2436. *
  2437. * # Safety
  2438. *
  2439. * The negotiated PSK identity will be copied into the identity buffer on success.
  2440. * Therefore, the identity buffer must have enough memory to fit the identity length.
  2441. *
  2442. * @param conn A pointer to the s2n_connection object.
  2443. * @param identity The negotiated PSK identity obtained from the s2n_connection object.
  2444. * @param max_identity_length The maximum length for the PSK identity. If the negotiated psk_identity length is
  2445. * greater than this `max_identity_length` value an error will be returned.
  2446. */
  2447. S2N_API int s2n_connection_get_negotiated_psk_identity(struct s2n_connection *conn, uint8_t *identity, uint16_t max_identity_length);
  2448. struct s2n_offered_psk;
  2449. /**
  2450. * Creates a new s2n offered PSK object.
  2451. * An offered PSK object represents a single PSK sent by the client.
  2452. *
  2453. * # Safety
  2454. *
  2455. * Use `s2n_offered_psk_free` to free the memory allocated to the s2n offered PSK object created by this API.
  2456. *
  2457. * @returns struct s2n_offered_psk* Returns a pointer to the newly created offered PSK object.
  2458. */
  2459. S2N_API struct s2n_offered_psk *s2n_offered_psk_new(void);
  2460. /**
  2461. * Frees the memory associated with the `s2n_offered_psk` object.
  2462. *
  2463. * @param psk A pointer to the `s2n_offered_psk` object to be freed.
  2464. */
  2465. S2N_API int s2n_offered_psk_free(struct s2n_offered_psk **psk);
  2466. /**
  2467. * Gets the PSK identity and PSK identity length for a given offered PSK object.
  2468. *
  2469. * @param psk A pointer to the offered PSK object being read.
  2470. * @param identity The PSK identity being obtained.
  2471. * @param size The length of the PSK identity being obtained.
  2472. */
  2473. S2N_API int s2n_offered_psk_get_identity(struct s2n_offered_psk *psk, uint8_t **identity, uint16_t *size);
  2474. struct s2n_offered_psk_list;
  2475. /**
  2476. * Checks whether the offered PSK list has an offered psk object next in line in the list.
  2477. * An offered PSK list contains all the PSKs offered by the client for the server to select.
  2478. *
  2479. * # Safety
  2480. *
  2481. * This API returns a pointer to the s2n-tls internal memory with limited lifetime.
  2482. * After the completion of `s2n_psk_selection_callback` this pointer is invalid.
  2483. *
  2484. * @param psk_list A pointer to the offered PSK list being read.
  2485. * @returns bool A boolean value representing whether an offered psk object is present next in line in the offered PSK list.
  2486. */
  2487. S2N_API bool s2n_offered_psk_list_has_next(struct s2n_offered_psk_list *psk_list);
  2488. /**
  2489. * Obtains the next offered PSK object from the list of offered PSKs. Use `s2n_offered_psk_list_has_next`
  2490. * prior to this API call to ensure we have not reached the end of the list.
  2491. *
  2492. * @param psk_list A pointer to the offered PSK list being read.
  2493. * @param psk A pointer to the next offered PSK object being obtained.
  2494. */
  2495. S2N_API int s2n_offered_psk_list_next(struct s2n_offered_psk_list *psk_list, struct s2n_offered_psk *psk);
  2496. /**
  2497. * Returns the offered PSK list to its original read state.
  2498. *
  2499. * When `s2n_offered_psk_list_reread` is called, `s2n_offered_psk_list_next` will return the first PSK
  2500. * in the offered PSK list.
  2501. *
  2502. * @param psk_list A pointer to the offered PSK list being reread.
  2503. */
  2504. S2N_API int s2n_offered_psk_list_reread(struct s2n_offered_psk_list *psk_list);
  2505. /**
  2506. * Chooses a PSK from the offered PSK list to be used for the connection.
  2507. * This API matches the PSK identity received from the client against the server's known PSK identities
  2508. * list, in order to choose the PSK to be used for the connection. If the PSK identity sent from the client
  2509. * is NULL, no PSK is chosen for the connection. If the client offered PSK identity has no matching PSK identity
  2510. * with the server, an error will be returned. Use this API along with the `s2n_psk_selection_callback` callback
  2511. * to select a PSK identity.
  2512. *
  2513. * @param psk_list A pointer to the server's known PSK list used to compare for a matching PSK with the client.
  2514. * @param psk A pointer to the client's PSK object used to compare with the server's known PSK identities.
  2515. */
  2516. S2N_API int s2n_offered_psk_list_choose_psk(struct s2n_offered_psk_list *psk_list, struct s2n_offered_psk *psk);
  2517. /**
  2518. * Callback function to select a PSK from a list of offered PSKs.
  2519. * Use this callback to implement custom PSK selection logic. The s2n-tls default PSK selection logic
  2520. * chooses the first matching PSK from the list of offered PSKs sent by the client.
  2521. *
  2522. * # Safety
  2523. *
  2524. * `context` is a void pointer and the caller is responsible for ensuring it is cast to the correct type.
  2525. * After the completion of this callback, the pointer to `psk_list` is invalid.
  2526. *
  2527. * @param conn A pointer to the s2n_connection object.
  2528. * @param context A pointer to a context for the caller to pass state to the callback, if needed.
  2529. * @param psk_list A pointer to the offered PSK list being read.
  2530. */
  2531. typedef int (*s2n_psk_selection_callback)(struct s2n_connection *conn, void *context,
  2532. struct s2n_offered_psk_list *psk_list);
  2533. /**
  2534. * Sets the callback to select the matching PSK.
  2535. * If this callback is not set s2n-tls uses a default PSK selection logic that selects the first matching
  2536. * server PSK.
  2537. *
  2538. * @param config A pointer to the s2n_config object.
  2539. * @param cb The function that should be called when the callback is triggered.
  2540. * @param context A pointer to a context for the caller to pass state to the callback, if needed.
  2541. */
  2542. S2N_API int s2n_config_set_psk_selection_callback(struct s2n_config *config, s2n_psk_selection_callback cb, void *context);
  2543. /**
  2544. * Get the number of bytes the connection has received.
  2545. *
  2546. * @param conn A pointer to the connection
  2547. * @returns return the number of bytes received by s2n-tls "on the wire"
  2548. */
  2549. S2N_API extern uint64_t s2n_connection_get_wire_bytes_in(struct s2n_connection *conn);
  2550. /**
  2551. * Get the number of bytes the connection has transmitted out.
  2552. *
  2553. * @param conn A pointer to the connection
  2554. * @returns return the number of bytes transmitted out by s2n-tls "on the wire"
  2555. */
  2556. S2N_API extern uint64_t s2n_connection_get_wire_bytes_out(struct s2n_connection *conn);
  2557. /**
  2558. * Access the protocol version supported by the client.
  2559. *
  2560. * @note The return value corresponds to the macros defined as S2N_SSLv2,
  2561. * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13.
  2562. *
  2563. * @param conn A pointer to the connection
  2564. * @returns returns the highest protocol version supported by the client
  2565. */
  2566. S2N_API extern int s2n_connection_get_client_protocol_version(struct s2n_connection *conn);
  2567. /**
  2568. * Access the protocol version supported by the server.
  2569. *
  2570. * @note The return value corresponds to the macros defined as S2N_SSLv2,
  2571. * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13.
  2572. *
  2573. * @param conn A pointer to the connection
  2574. * @returns Returns the highest protocol version supported by the server
  2575. */
  2576. S2N_API extern int s2n_connection_get_server_protocol_version(struct s2n_connection *conn);
  2577. /**
  2578. * Access the protocol version selected for the connection.
  2579. *
  2580. * @note The return value corresponds to the macros defined as S2N_SSLv2,
  2581. * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13.
  2582. *
  2583. * @param conn A pointer to the connection
  2584. * @returns The protocol version actually negotiated by the handshake
  2585. */
  2586. S2N_API extern int s2n_connection_get_actual_protocol_version(struct s2n_connection *conn);
  2587. /**
  2588. * Access the client hello protocol version for the connection.
  2589. *
  2590. * @note The return value corresponds to the macros defined as S2N_SSLv2,
  2591. * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13.
  2592. *
  2593. * @param conn A pointer to the connection
  2594. * @returns The protocol version used to send the initial client hello message.
  2595. */
  2596. S2N_API extern int s2n_connection_get_client_hello_version(struct s2n_connection *conn);
  2597. /**
  2598. * Check if Client Auth was used for a connection.
  2599. *
  2600. * @param conn A pointer to the connection
  2601. * @returns 1 if the handshake completed and Client Auth was negotiated during then
  2602. * handshake.
  2603. */
  2604. S2N_API extern int s2n_connection_client_cert_used(struct s2n_connection *conn);
  2605. /**
  2606. * A function that provides a human readable string of the cipher suite that was chosen
  2607. * for a connection.
  2608. *
  2609. * @warning The string "TLS_NULL_WITH_NULL_NULL" is returned before the TLS handshake has been performed.
  2610. * This does not mean that the ciphersuite "TLS_NULL_WITH_NULL_NULL" will be used by the connection,
  2611. * it is merely being used as a placeholder.
  2612. *
  2613. * @note This function is only accurate after the TLS handshake.
  2614. *
  2615. * @param conn A pointer to the connection
  2616. * @returns A string indicating the cipher suite negotiated by s2n in OpenSSL format.
  2617. */
  2618. S2N_API extern const char *s2n_connection_get_cipher(struct s2n_connection *conn);
  2619. /**
  2620. * Provides access to the TLS-Exporter functionality.
  2621. *
  2622. * See https://datatracker.ietf.org/doc/html/rfc5705 and https://www.rfc-editor.org/rfc/rfc8446.
  2623. *
  2624. * @note This is currently only available with TLS 1.3 connections which have finished a handshake.
  2625. *
  2626. * @param conn A pointer to the connection
  2627. * @returns A POSIX error signal. If an error was returned, the value contained in `output` should be considered invalid.
  2628. */
  2629. S2N_API extern int s2n_connection_tls_exporter(struct s2n_connection *conn,
  2630. const uint8_t *label, uint32_t label_length, const uint8_t *context, uint32_t context_length,
  2631. uint8_t *output, uint32_t output_length);
  2632. /**
  2633. * Returns the IANA value for the connection's negotiated cipher suite.
  2634. *
  2635. * The value is returned in the form of `first,second`, in order to closely match
  2636. * the values defined in the [IANA Registry](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#table-tls-parameters-4).
  2637. * For example if the connection's negotiated cipher suite is `TLS_AES_128_GCM_SHA256`,
  2638. * which is registered as `0x13,0x01`, then `first = 0x13` and `second = 0x01`.
  2639. *
  2640. * This method will only succeed after the cipher suite has been negotiated with the peer.
  2641. *
  2642. * @param conn A pointer to the connection being read
  2643. * @param first A pointer to a single byte, which will be updated with the first byte in the registered IANA value.
  2644. * @param second A pointer to a single byte, which will be updated with the second byte in the registered IANA value.
  2645. * @returns A POSIX error signal. If an error was returned, the values contained in `first` and `second` should be considered invalid.
  2646. */
  2647. S2N_API extern int s2n_connection_get_cipher_iana_value(struct s2n_connection *conn, uint8_t *first, uint8_t *second);
  2648. /**
  2649. * Function to check if the cipher used by current connection is supported by the current
  2650. * cipher preferences.
  2651. * @param conn A pointer to the s2n connection
  2652. * @param version A string representing the security policy to check against.
  2653. * @returns 1 if the connection satisfies the cipher suite. 0 if the connection does not satisfy the cipher suite. -1 if there is an error.
  2654. */
  2655. S2N_API extern int s2n_connection_is_valid_for_cipher_preferences(struct s2n_connection *conn, const char *version);
  2656. /**
  2657. * Function to get the human readable elliptic curve name for the connection.
  2658. *
  2659. * @param conn A pointer to the s2n connection
  2660. * @returns A string indicating the elliptic curve used during ECDHE key exchange. The string "NONE" is returned if no curve was used.
  2661. */
  2662. S2N_API extern const char *s2n_connection_get_curve(struct s2n_connection *conn);
  2663. /**
  2664. * Function to get the human readable KEM name for the connection.
  2665. *
  2666. * @param conn A pointer to the s2n connection
  2667. * @returns A human readable string for the KEM group. If there is no KEM configured returns "NONE"
  2668. */
  2669. S2N_API extern const char *s2n_connection_get_kem_name(struct s2n_connection *conn);
  2670. /**
  2671. * Function to get the human readable KEM group name for the connection.
  2672. *
  2673. * @param conn A pointer to the s2n connection
  2674. * @returns A human readable string for the KEM group. If the connection is < TLS1.3 or there is no KEM group configured returns "NONE"
  2675. */
  2676. S2N_API extern const char *s2n_connection_get_kem_group_name(struct s2n_connection *conn);
  2677. /**
  2678. * Function to get the alert that caused a connection to close. s2n-tls considers all
  2679. * TLS alerts fatal and shuts down a connection whenever one is received.
  2680. *
  2681. * @param conn A pointer to the s2n connection
  2682. * @returns The TLS alert code that caused a connection to be shut down
  2683. */
  2684. S2N_API extern int s2n_connection_get_alert(struct s2n_connection *conn);
  2685. /**
  2686. * Function to return the last TLS handshake type that was processed. The returned format is a human readable string.
  2687. *
  2688. * @param conn A pointer to the s2n connection
  2689. * @returns A human-readable handshake type name, e.g. "NEGOTIATED|FULL_HANDSHAKE|PERFECT_FORWARD_SECRECY"
  2690. */
  2691. S2N_API extern const char *s2n_connection_get_handshake_type_name(struct s2n_connection *conn);
  2692. /**
  2693. * Function to return the last TLS message that was processed. The returned format is a human readable string.
  2694. * @param conn A pointer to the s2n connection
  2695. * @returns The last message name in the TLS state machine, e.g. "SERVER_HELLO", "APPLICATION_DATA".
  2696. */
  2697. S2N_API extern const char *s2n_connection_get_last_message_name(struct s2n_connection *conn);
  2698. /**
  2699. * Opaque async private key operation handle
  2700. */
  2701. struct s2n_async_pkey_op;
  2702. /**
  2703. * Sets whether or not a connection should enforce strict signature validation during the
  2704. * `s2n_async_pkey_op_apply` call.
  2705. *
  2706. * `mode` can take the following values:
  2707. * - `S2N_ASYNC_PKEY_VALIDATION_FAST` - default behavior: s2n-tls will perform only the minimum validation required for safe use of the asyn pkey operation.
  2708. * - `S2N_ASYNC_PKEY_VALIDATION_STRICT` - in addition to the previous checks, s2n-tls will also ensure that the signature created as a result of the async private key sign operation matches the public key on the connection.
  2709. */
  2710. typedef enum {
  2711. S2N_ASYNC_PKEY_VALIDATION_FAST,
  2712. S2N_ASYNC_PKEY_VALIDATION_STRICT
  2713. } s2n_async_pkey_validation_mode;
  2714. /**
  2715. * The type of private key operation
  2716. */
  2717. typedef enum {
  2718. S2N_ASYNC_DECRYPT,
  2719. S2N_ASYNC_SIGN
  2720. } s2n_async_pkey_op_type;
  2721. /**
  2722. * Callback function for handling private key operations
  2723. *
  2724. * Invoked every time an operation requiring the private key is encountered
  2725. * during the handshake.
  2726. *
  2727. * # Safety
  2728. * * `op` is owned by the application and MUST be freed.
  2729. *
  2730. * @param conn Connection which triggered the callback
  2731. * @param op An opaque object representing the private key operation
  2732. */
  2733. typedef int (*s2n_async_pkey_fn)(struct s2n_connection *conn, struct s2n_async_pkey_op *op);
  2734. /**
  2735. * Sets up the callback to invoke when private key operations occur.
  2736. *
  2737. * @param config Config to set the callback
  2738. * @param fn The function that should be called for each private key operation
  2739. */
  2740. S2N_API extern int s2n_config_set_async_pkey_callback(struct s2n_config *config, s2n_async_pkey_fn fn);
  2741. /**
  2742. * Performs a private key operation using the given private key.
  2743. *
  2744. * # Safety
  2745. * * Can only be called once. Any subsequent calls will produce a `S2N_ERR_T_USAGE` error.
  2746. * * Safe to call from inside s2n_async_pkey_fn
  2747. * * Safe to call from a different thread, as long as no other thread is operating on `op`.
  2748. *
  2749. * @param op An opaque object representing the private key operation
  2750. * @param key The private key used for the operation. It can be extracted from
  2751. * `conn` through the `s2n_connection_get_selected_cert` and `s2n_cert_chain_and_key_get_private_key` calls
  2752. */
  2753. S2N_API extern int s2n_async_pkey_op_perform(struct s2n_async_pkey_op *op, s2n_cert_private_key *key);
  2754. /**
  2755. * Finalizes a private key operation and unblocks the connection.
  2756. *
  2757. * # Safety
  2758. * * `conn` must match the connection that originally triggered the callback.
  2759. * * Must be called after the operation is performed.
  2760. * * Can only be called once. Any subsequent calls will produce a `S2N_ERR_T_USAGE` error.
  2761. * * Safe to call from inside s2n_async_pkey_fn
  2762. * * Safe to call from a different thread, as long as no other thread is operating on `op`.
  2763. *
  2764. * @param op An opaque object representing the private key operation
  2765. * @param conn The connection associated with the operation that should be unblocked
  2766. */
  2767. S2N_API extern int s2n_async_pkey_op_apply(struct s2n_async_pkey_op *op, struct s2n_connection *conn);
  2768. /**
  2769. * Frees the opaque structure representing a private key operation.
  2770. *
  2771. * # Safety
  2772. * * MUST be called for every operation passed to s2n_async_pkey_fn
  2773. * * Safe to call before or after the connection that created the operation is freed
  2774. *
  2775. * @param op An opaque object representing the private key operation
  2776. */
  2777. S2N_API extern int s2n_async_pkey_op_free(struct s2n_async_pkey_op *op);
  2778. /**
  2779. * Configures whether or not s2n-tls will perform potentially expensive validation of
  2780. * the results of a private key operation.
  2781. *
  2782. * @param config Config to set the validation mode for
  2783. * @param mode What level of validation to perform
  2784. */
  2785. S2N_API extern int s2n_config_set_async_pkey_validation_mode(struct s2n_config *config, s2n_async_pkey_validation_mode mode);
  2786. /**
  2787. * Returns the type of the private key operation.
  2788. *
  2789. * @param op An opaque object representing the private key operation
  2790. * @param type A pointer to be set to the type
  2791. */
  2792. S2N_API extern int s2n_async_pkey_op_get_op_type(struct s2n_async_pkey_op *op, s2n_async_pkey_op_type *type);
  2793. /**
  2794. * Returns the size of the input to the private key operation.
  2795. *
  2796. * @param op An opaque object representing the private key operation
  2797. * @param data_len A pointer to be set to the size
  2798. */
  2799. S2N_API extern int s2n_async_pkey_op_get_input_size(struct s2n_async_pkey_op *op, uint32_t *data_len);
  2800. /**
  2801. * Returns the input to the private key operation.
  2802. *
  2803. * When signing, the input is the digest to sign.
  2804. * When decrypting, the input is the data to decrypt.
  2805. *
  2806. * # Safety
  2807. * * `data` must be sufficiently large to contain the input.
  2808. * `s2n_async_pkey_op_get_input_size` can be called to determine how much memory is required.
  2809. * * s2n-tls does not take ownership of `data`.
  2810. * The application still owns the memory and must free it if necessary.
  2811. *
  2812. * @param op An opaque object representing the private key operation
  2813. * @param data A pointer to a buffer to copy the input into
  2814. * @param data_len The maximum size of the `data` buffer
  2815. */
  2816. S2N_API extern int s2n_async_pkey_op_get_input(struct s2n_async_pkey_op *op, uint8_t *data, uint32_t data_len);
  2817. /**
  2818. * Sets the output of the private key operation.
  2819. *
  2820. * # Safety
  2821. * * s2n-tls does not take ownership of `data`.
  2822. * The application still owns the memory and must free it if necessary.
  2823. *
  2824. * @param op An opaque object representing the private key operation
  2825. * @param data A pointer to a buffer containing the output
  2826. * @param data_len The size of the `data` buffer
  2827. */
  2828. S2N_API extern int s2n_async_pkey_op_set_output(struct s2n_async_pkey_op *op, const uint8_t *data, uint32_t data_len);
  2829. /**
  2830. * Callback function for handling key log events
  2831. *
  2832. * THIS SHOULD BE USED FOR DEBUGGING PURPOSES ONLY!
  2833. *
  2834. * Each log line is formatted with the
  2835. * [NSS Key Log Format](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format)
  2836. * without a newline.
  2837. *
  2838. * # Safety
  2839. *
  2840. * * `ctx` MUST be cast into the same type of pointer that was originally created
  2841. * * `logline` bytes MUST be copied or discarded before this function returns
  2842. *
  2843. * @param ctx Context for the callback
  2844. * @param conn Connection for which the log line is being emitted
  2845. * @param logline Pointer to the log line data
  2846. * @param len Length of the log line data
  2847. */
  2848. typedef int (*s2n_key_log_fn)(void *ctx, struct s2n_connection *conn, uint8_t *logline, size_t len);
  2849. /**
  2850. * Sets a key logging callback on the provided config
  2851. *
  2852. * THIS SHOULD BE USED FOR DEBUGGING PURPOSES ONLY!
  2853. *
  2854. * Setting this function enables configurations to emit secrets in the
  2855. * [NSS Key Log Format](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format)
  2856. *
  2857. * # Safety
  2858. *
  2859. * * `callback` MUST cast `ctx` into the same type of pointer that was originally created
  2860. * * `ctx` MUST live for at least as long as it is set on the config
  2861. *
  2862. * @param config Config to set the callback
  2863. * @param callback The function that should be called for each secret log entry
  2864. * @param ctx The context to be passed when the callback is called
  2865. */
  2866. S2N_API extern int s2n_config_set_key_log_cb(struct s2n_config *config, s2n_key_log_fn callback, void *ctx);
  2867. /**
  2868. * s2n_config_enable_cert_req_dss_legacy_compat adds a dss cert type in the server certificate request when being called.
  2869. * It only sends the dss cert type in the cert request but does not succeed the handshake if a dss cert is received.
  2870. * Please DO NOT call this api unless you know you actually need legacy DSS certificate type compatibility
  2871. * @param config Config to enable legacy DSS certificates for
  2872. */
  2873. S2N_API extern int s2n_config_enable_cert_req_dss_legacy_compat(struct s2n_config *config);
  2874. /**
  2875. * Sets the maximum bytes of early data the server will accept.
  2876. *
  2877. * The default maximum is 0. If the maximum is 0, the server rejects all early data requests.
  2878. * The config maximum can be overridden by the connection maximum or the maximum on an external pre-shared key.
  2879. *
  2880. * @param config A pointer to the config
  2881. * @param max_early_data_size The maximum early data that the server will accept
  2882. * @returns A POSIX error signal. If successful, the maximum early data size was updated.
  2883. */
  2884. S2N_API int s2n_config_set_server_max_early_data_size(struct s2n_config *config, uint32_t max_early_data_size);
  2885. /**
  2886. * Sets the maximum bytes of early data the server will accept.
  2887. *
  2888. * The default maximum is 0. If the maximum is 0, the server rejects all early data requests.
  2889. * The connection maximum can be overridden by the maximum on an external pre-shared key.
  2890. *
  2891. * @param conn A pointer to the connection
  2892. * @param max_early_data_size The maximum early data the server will accept
  2893. * @returns A POSIX error signal. If successful, the maximum early data size was updated.
  2894. */
  2895. S2N_API int s2n_connection_set_server_max_early_data_size(struct s2n_connection *conn, uint32_t max_early_data_size);
  2896. /**
  2897. * Sets the user context associated with early data on a server.
  2898. *
  2899. * This context is passed to the `s2n_early_data_cb` callback to help decide whether to accept or reject early data.
  2900. *
  2901. * Unlike most contexts, the early data context is a byte buffer instead of a void pointer.
  2902. * This is because we need to serialize the context into session tickets.
  2903. *
  2904. * This API is intended for use with session resumption, and will not affect pre-shared keys.
  2905. *
  2906. * @param conn A pointer to the connection
  2907. * @param context A pointer to the user context data. This data will be copied.
  2908. * @param context_size The size of the data to read from the `context` pointer.
  2909. * @returns A POSIX error signal. If successful, the context was updated.
  2910. */
  2911. S2N_API int s2n_connection_set_server_early_data_context(struct s2n_connection *conn, const uint8_t *context, uint16_t context_size);
  2912. /**
  2913. * Configures a particular pre-shared key to allow early data.
  2914. *
  2915. * `max_early_data_size` must be set to the maximum early data accepted by the server.
  2916. *
  2917. * In order to use early data, the cipher suite set on the pre-shared key must match the cipher suite
  2918. * ultimately negotiated by the TLS handshake. Additionally, the cipher suite must have the same
  2919. * hmac algorithm as the pre-shared key.
  2920. *
  2921. * @param psk A pointer to the pre-shared key, created with `s2n_external_psk_new`.
  2922. * @param max_early_data_size The maximum early data that can be sent or received using this key.
  2923. * @param cipher_suite_first_byte The first byte in the registered IANA value of the associated cipher suite.
  2924. * @param cipher_suite_second_byte The second byte in the registered IANA value of the associated cipher suite.
  2925. * @returns A POSIX error signal. If successful, `psk` was updated.
  2926. */
  2927. S2N_API int s2n_psk_configure_early_data(struct s2n_psk *psk, uint32_t max_early_data_size,
  2928. uint8_t cipher_suite_first_byte, uint8_t cipher_suite_second_byte);
  2929. /**
  2930. * Sets the optional `application_protocol` associated with the given pre-shared key.
  2931. *
  2932. * In order to use early data, the `application_protocol` set on the pre-shared key must match
  2933. * the `application_protocol` ultimately negotiated by the TLS handshake.
  2934. *
  2935. * @param psk A pointer to the pre-shared key, created with `s2n_external_psk_new`.
  2936. * @param application_protocol A pointer to the associated application protocol data. This data will be copied.
  2937. * @param size The size of the data to read from the `application_protocol` pointer.
  2938. * @returns A POSIX error signal. If successful, the application protocol was set.
  2939. */
  2940. S2N_API int s2n_psk_set_application_protocol(struct s2n_psk *psk, const uint8_t *application_protocol, uint8_t size);
  2941. /**
  2942. * Sets the optional user early data context associated with the given pre-shared key.
  2943. *
  2944. * The early data context is passed to the `s2n_early_data_cb` callback to help decide whether
  2945. * to accept or reject early data.
  2946. *
  2947. * @param psk A pointer to the pre-shared key, created with `s2n_external_psk_new`.
  2948. * @param context A pointer to the associated user context data. This data will be copied.
  2949. * @param size The size of the data to read from the `context` pointer.
  2950. * @returns A POSIX error signal. If successful, the context was set.
  2951. */
  2952. S2N_API int s2n_psk_set_early_data_context(struct s2n_psk *psk, const uint8_t *context, uint16_t size);
  2953. /**
  2954. * The status of early data on a connection.
  2955. *
  2956. * S2N_EARLY_DATA_STATUS_OK: Early data is in progress.
  2957. * S2N_EARLY_DATA_STATUS_NOT_REQUESTED: The client did not request early data, so none was sent or received.
  2958. * S2N_EARLY_DATA_STATUS_REJECTED: The client requested early data, but the server rejected the request.
  2959. * Early data may have been sent, but was not received.
  2960. * S2N_EARLY_DATA_STATUS_END: All early data was successfully sent and received.
  2961. */
  2962. typedef enum {
  2963. S2N_EARLY_DATA_STATUS_OK,
  2964. S2N_EARLY_DATA_STATUS_NOT_REQUESTED,
  2965. S2N_EARLY_DATA_STATUS_REJECTED,
  2966. S2N_EARLY_DATA_STATUS_END,
  2967. } s2n_early_data_status_t;
  2968. /**
  2969. * Reports the current state of early data for a connection.
  2970. *
  2971. * See `s2n_early_data_status_t` for all possible states.
  2972. *
  2973. * @param conn A pointer to the connection
  2974. * @param status A pointer which will be set to the current early data status
  2975. * @returns A POSIX error signal.
  2976. */
  2977. S2N_API int s2n_connection_get_early_data_status(struct s2n_connection *conn, s2n_early_data_status_t *status);
  2978. /**
  2979. * Reports the remaining size of the early data allowed by a connection.
  2980. *
  2981. * If early data was rejected or not requested, the remaining early data size is 0.
  2982. * Otherwise, the remaining early data size is the maximum early data allowed by the connection,
  2983. * minus the early data sent or received so far.
  2984. *
  2985. * @param conn A pointer to the connection
  2986. * @param allowed_early_data_size A pointer which will be set to the remaining early data currently allowed by `conn`
  2987. * @returns A POSIX error signal.
  2988. */
  2989. S2N_API int s2n_connection_get_remaining_early_data_size(struct s2n_connection *conn, uint32_t *allowed_early_data_size);
  2990. /**
  2991. * Reports the maximum size of the early data allowed by a connection.
  2992. *
  2993. * This is the maximum amount of early data that can ever be sent and received for a connection.
  2994. * It is not affected by the actual status of the early data, so can be non-zero even if early data
  2995. * is rejected or not requested.
  2996. *
  2997. * @param conn A pointer to the connection
  2998. * @param max_early_data_size A pointer which will be set to the maximum early data allowed by `conn`
  2999. * @returns A POSIX error signal.
  3000. */
  3001. S2N_API int s2n_connection_get_max_early_data_size(struct s2n_connection *conn, uint32_t *max_early_data_size);
  3002. /**
  3003. * Called by the client to begin negotiation and send early data.
  3004. *
  3005. * See https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md#using-early-data--0rtt
  3006. * for usage and examples. DO NOT USE unless you have considered the security issues and
  3007. * implemented mitigation for anti-replay attacks.
  3008. *
  3009. * @param conn A pointer to the connection
  3010. * @param data A pointer to the early data to be sent
  3011. * @param data_len The size of the early data to send
  3012. * @param data_sent A pointer which will be set to the size of the early data sent
  3013. * @param blocked A pointer which will be set to the blocked status, as in `s2n_negotiate`.
  3014. * @returns A POSIX error signal. The error should be handled as in `s2n_negotiate`.
  3015. */
  3016. S2N_API int s2n_send_early_data(struct s2n_connection *conn, const uint8_t *data, ssize_t data_len,
  3017. ssize_t *data_sent, s2n_blocked_status *blocked);
  3018. /**
  3019. * Called by the server to begin negotiation and accept any early data the client sends.
  3020. *
  3021. * See https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md#using-early-data--0rtt
  3022. * for usage and examples. DO NOT USE unless you have considered the security issues and
  3023. * implemented mitigation for anti-replay attacks.
  3024. *
  3025. * @param conn A pointer to the connection
  3026. * @param data A pointer to a buffer to store the early data received
  3027. * @param max_data_len The size of the early data buffer
  3028. * @param data_received A pointer which will be set to the size of the early data received
  3029. * @param blocked A pointer which will be set to the blocked status, as in `s2n_negotiate`.
  3030. * @returns A POSIX error signal. The error should be handled as in `s2n_negotiate`.
  3031. */
  3032. S2N_API int s2n_recv_early_data(struct s2n_connection *conn, uint8_t *data, ssize_t max_data_len,
  3033. ssize_t *data_received, s2n_blocked_status *blocked);
  3034. struct s2n_offered_early_data;
  3035. /**
  3036. * A callback which can be implemented to accept or reject early data.
  3037. *
  3038. * This callback is triggered only after the server has determined early data is otherwise acceptable according
  3039. * to the TLS early data specification. Implementations therefore only need to cover application-specific checks,
  3040. * not the standard TLS early data validation.
  3041. *
  3042. * This callback can be synchronous or asynchronous. For asynchronous behavior, return success without
  3043. * calling `s2n_offered_early_data_reject` or `s2n_offered_early_data_accept`. `early_data` will
  3044. * still be a valid reference, and the connection will block until `s2n_offered_early_data_reject` or
  3045. * `s2n_offered_early_data_accept` is called.
  3046. *
  3047. * @param conn A pointer to the connection
  3048. * @param early_data A pointer which can be used to access information about the proposed early data
  3049. * and then accept or reject it.
  3050. * @returns A POSIX error signal. If unsuccessful, the connection will be closed with an error.
  3051. */
  3052. typedef int (*s2n_early_data_cb)(struct s2n_connection *conn, struct s2n_offered_early_data *early_data);
  3053. /**
  3054. * Set a callback to accept or reject early data.
  3055. *
  3056. * @param config A pointer to the connection config
  3057. * @param cb A pointer to the implementation of the callback.
  3058. * @returns A POSIX error signal. If successful, the callback was set.
  3059. */
  3060. S2N_API int s2n_config_set_early_data_cb(struct s2n_config *config, s2n_early_data_cb cb);
  3061. /**
  3062. * Get the length of the early data context set by the user.
  3063. *
  3064. * @param early_data A pointer to the early data information
  3065. * @param context_len The length of the user context
  3066. * @returns A POSIX error signal.
  3067. */
  3068. S2N_API int s2n_offered_early_data_get_context_length(struct s2n_offered_early_data *early_data, uint16_t *context_len);
  3069. /**
  3070. * Get the early data context set by the user.
  3071. *
  3072. * @param early_data A pointer to the early data information
  3073. * @param context A byte buffer to copy the user context into
  3074. * @param max_len The size of `context`. Must be >= to the result of `s2n_offered_early_data_get_context_length`.
  3075. * @returns A POSIX error signal.
  3076. */
  3077. S2N_API int s2n_offered_early_data_get_context(struct s2n_offered_early_data *early_data, uint8_t *context, uint16_t max_len);
  3078. /**
  3079. * Reject early data offered by the client.
  3080. *
  3081. * @param early_data A pointer to the early data information
  3082. * @returns A POSIX error signal. If success, the client's early data will be rejected.
  3083. */
  3084. S2N_API int s2n_offered_early_data_reject(struct s2n_offered_early_data *early_data);
  3085. /**
  3086. * Accept early data offered by the client.
  3087. *
  3088. * @param early_data A pointer to the early data information
  3089. * @returns A POSIX error signal. If success, the client's early data will be accepted.
  3090. */
  3091. S2N_API int s2n_offered_early_data_accept(struct s2n_offered_early_data *early_data);
  3092. #ifdef __cplusplus
  3093. }
  3094. #endif