s2n.h 149 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392
  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 client certificate chain and places it in the `der_cert_chain_out` buffer. `cert_chain_len` is updated
  1941. * to match the size the chain buffer.
  1942. *
  1943. * @warning The buffers share a lifetime with the s2n_connection object.
  1944. *
  1945. * @param conn A pointer to the s2n_connection object
  1946. * @param der_cert_chain_out A uint8_t pointer. This will be updated to point to the client certificate chain.
  1947. * @param cert_chain_len A pointer to a uint32_t. This will be updated to match the size of the buffer `der_cert_chain_out` points to.
  1948. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1949. */
  1950. 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);
  1951. /**
  1952. * Sets the initial number of session tickets to send after a >=TLS1.3 handshake. The default value is one ticket.
  1953. *
  1954. * @param config A pointer to the config object.
  1955. * @param num The number of session tickets that will be sent.
  1956. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1957. */
  1958. S2N_API extern int s2n_config_set_initial_ticket_count(struct s2n_config *config, uint8_t num);
  1959. /**
  1960. * Increases the number of session tickets to send after a >=TLS1.3 handshake.
  1961. *
  1962. * @param conn A pointer to the connection object.
  1963. * @param num The number of additional session tickets to send.
  1964. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1965. */
  1966. S2N_API extern int s2n_connection_add_new_tickets_to_send(struct s2n_connection *conn, uint8_t num);
  1967. /**
  1968. * Returns the number of session tickets issued by the server.
  1969. *
  1970. * In TLS1.3, this number can be up to the limit configured by s2n_config_set_initial_ticket_count
  1971. * and s2n_connection_add_new_tickets_to_send. In earlier versions of TLS, this number will be either 0 or 1.
  1972. *
  1973. * This method only works for server connections.
  1974. *
  1975. * @param conn A pointer to the connection object.
  1976. * @param num The number of additional session tickets sent.
  1977. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1978. */
  1979. S2N_API extern int s2n_connection_get_tickets_sent(struct s2n_connection *conn, uint16_t *num);
  1980. /**
  1981. * Sets the keying material lifetime for >=TLS1.3 session tickets so that one session doesn't get re-used ad infinitum.
  1982. * The default value is one week.
  1983. *
  1984. * @param conn A pointer to the connection object.
  1985. * @param lifetime_in_secs Lifetime of keying material in seconds.
  1986. * @returns S2N_SUCCESS on success. S2N_FAILURE on failure
  1987. */
  1988. S2N_API extern int s2n_connection_set_server_keying_material_lifetime(struct s2n_connection *conn, uint32_t lifetime_in_secs);
  1989. struct s2n_session_ticket;
  1990. /**
  1991. * Callback function for receiving a session ticket.
  1992. *
  1993. * This function will be called each time a session ticket is received, which may be multiple times for TLS1.3.
  1994. *
  1995. * # Safety
  1996. *
  1997. * `ctx` is a void pointer and the caller is responsible for ensuring it is cast to the correct type.
  1998. * `ticket` is valid only within the scope of this callback.
  1999. *
  2000. * @param conn A pointer to the connection object.
  2001. * @param ctx Context for the session ticket callback function.
  2002. * @param ticket Pointer to the received session ticket object.
  2003. */
  2004. typedef int (*s2n_session_ticket_fn)(struct s2n_connection *conn, void *ctx, struct s2n_session_ticket *ticket);
  2005. /**
  2006. * Sets a session ticket callback to be called when a client receives a new session ticket.
  2007. *
  2008. * # Safety
  2009. *
  2010. * `callback` MUST cast `ctx` into the same type of pointer that was originally created.
  2011. * `ctx` MUST be valid for the lifetime of the config, or until a different context is set.
  2012. *
  2013. * @param config A pointer to the config object.
  2014. * @param callback The function that should be called when the callback is triggered.
  2015. * @param ctx The context to be passed when the callback is called.
  2016. */
  2017. S2N_API extern int s2n_config_set_session_ticket_cb(struct s2n_config *config, s2n_session_ticket_fn callback, void *ctx);
  2018. /**
  2019. * Gets the length of the session ticket from a session ticket object.
  2020. *
  2021. * @param ticket Pointer to the session ticket object.
  2022. * @param data_len Pointer to be set to the length of the session ticket on success.
  2023. */
  2024. S2N_API extern int s2n_session_ticket_get_data_len(struct s2n_session_ticket *ticket, size_t *data_len);
  2025. /**
  2026. * Gets the session ticket data from a session ticket object.
  2027. *
  2028. * # Safety
  2029. * The entire session ticket will be copied into `data` on success. Therefore, `data` MUST have enough
  2030. * memory to store the session ticket data.
  2031. *
  2032. * @param ticket Pointer to the session ticket object.
  2033. * @param max_data_len Maximum length of data that can be written to the 'data' pointer.
  2034. * @param data Pointer to where the session ticket data will be stored.
  2035. */
  2036. S2N_API extern int s2n_session_ticket_get_data(struct s2n_session_ticket *ticket, size_t max_data_len, uint8_t *data);
  2037. /**
  2038. * Gets the lifetime in seconds of the session ticket from a session ticket object.
  2039. *
  2040. * @param ticket Pointer to the session ticket object.
  2041. * @param session_lifetime Pointer to a variable where the lifetime of the session ticket will be stored.
  2042. */
  2043. S2N_API extern int s2n_session_ticket_get_lifetime(struct s2n_session_ticket *ticket, uint32_t *session_lifetime);
  2044. /**
  2045. * De-serializes the session state and updates the connection accordingly.
  2046. *
  2047. * If this method fails, the connection should not be affected: calling s2n_negotiate
  2048. * with the connection should simply result in a full handshake.
  2049. *
  2050. * @param conn A pointer to the s2n_connection object
  2051. * @param session A pointer to a buffer of size `length`
  2052. * @param length The size of the `session` buffer
  2053. *
  2054. * @returns The number of copied bytes
  2055. */
  2056. S2N_API extern int s2n_connection_set_session(struct s2n_connection *conn, const uint8_t *session, size_t length);
  2057. /**
  2058. * Serializes the session state from connection and copies into the `session` buffer and returns the number of copied bytes
  2059. *
  2060. * @note This function is not recommended for > TLS 1.2 because in TLS1.3
  2061. * servers can send multiple session tickets and this function will only
  2062. * return the most recently received ticket.
  2063. *
  2064. * @param conn A pointer to the s2n_connection object
  2065. * @param session A pointer to a buffer of size `max_length`
  2066. * @param max_length The size of the `session` buffer
  2067. *
  2068. * @returns The number of copied bytes
  2069. */
  2070. S2N_API extern int s2n_connection_get_session(struct s2n_connection *conn, uint8_t *session, size_t max_length);
  2071. /**
  2072. * Retrieves a hint from the server indicating how long this ticket's lifetime is.
  2073. *
  2074. * @note This function is not recommended for > TLS 1.2 because in TLS1.3
  2075. * servers can send multiple session tickets and this function will only
  2076. * return the most recently received ticket lifetime hint.
  2077. *
  2078. * @param conn A pointer to the s2n_connection object
  2079. *
  2080. * @returns The session ticket lifetime hint in seconds from the server or -1 when session ticket was not used for resumption.
  2081. */
  2082. S2N_API extern int s2n_connection_get_session_ticket_lifetime_hint(struct s2n_connection *conn);
  2083. /**
  2084. * Use this to query the serialized session state size before copying it into a buffer.
  2085. *
  2086. * @param conn A pointer to the s2n_connection object
  2087. *
  2088. * @returns number of bytes needed to store serialized session state
  2089. */
  2090. S2N_API extern int s2n_connection_get_session_length(struct s2n_connection *conn);
  2091. /**
  2092. * Gets the latest session id's length from the connection.
  2093. *
  2094. * Use this to query the session id size before copying it into a buffer.
  2095. *
  2096. * @param conn A pointer to the s2n_connection object
  2097. *
  2098. * @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.
  2099. */
  2100. S2N_API extern int s2n_connection_get_session_id_length(struct s2n_connection *conn);
  2101. /**
  2102. * Gets the latest session id from the connection, copies it into the `session_id` buffer, and returns the number of copied bytes.
  2103. *
  2104. * The session id may change between s2n receiving the ClientHello and sending the ServerHello, but this function will always describe the latest session id.
  2105. *
  2106. * See s2n_client_hello_get_session_id() to get the session id as it was sent by the client in the ClientHello message.
  2107. *
  2108. * @param conn A pointer to the s2n_connection object
  2109. * @param session_id A pointer to a buffer of size `max_length`
  2110. * @param max_length The size of the `session_id` buffer
  2111. *
  2112. * @returns The number of copied bytes.
  2113. */
  2114. S2N_API extern int s2n_connection_get_session_id(struct s2n_connection *conn, uint8_t *session_id, size_t max_length);
  2115. /**
  2116. * Check if the connection was resumed from an earlier handshake.
  2117. *
  2118. * @param conn A pointer to the s2n_connection object
  2119. *
  2120. * @returns returns 1 if the handshake was abbreviated, otherwise returns 0
  2121. */
  2122. S2N_API extern int s2n_connection_is_session_resumed(struct s2n_connection *conn);
  2123. /**
  2124. * Check if the connection is OCSP stapled.
  2125. *
  2126. * @param conn A pointer to the s2n_connection object
  2127. *
  2128. * @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.
  2129. */
  2130. S2N_API extern int s2n_connection_is_ocsp_stapled(struct s2n_connection *conn);
  2131. /**
  2132. * TLS Signature Algorithms - RFC 5246 7.4.1.4.1
  2133. * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-16
  2134. */
  2135. typedef enum {
  2136. S2N_TLS_SIGNATURE_ANONYMOUS = 0,
  2137. S2N_TLS_SIGNATURE_RSA = 1,
  2138. S2N_TLS_SIGNATURE_ECDSA = 3,
  2139. /* Use Private Range for RSA PSS since it's not defined there */
  2140. S2N_TLS_SIGNATURE_RSA_PSS_RSAE = 224,
  2141. S2N_TLS_SIGNATURE_RSA_PSS_PSS
  2142. } s2n_tls_signature_algorithm;
  2143. /** TLS Hash Algorithms - https://tools.ietf.org/html/rfc5246#section-7.4.1.4.1
  2144. * https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#tls-parameters-18
  2145. */
  2146. typedef enum {
  2147. S2N_TLS_HASH_NONE = 0,
  2148. S2N_TLS_HASH_MD5 = 1,
  2149. S2N_TLS_HASH_SHA1 = 2,
  2150. S2N_TLS_HASH_SHA224 = 3,
  2151. S2N_TLS_HASH_SHA256 = 4,
  2152. S2N_TLS_HASH_SHA384 = 5,
  2153. S2N_TLS_HASH_SHA512 = 6,
  2154. /* Use Private Range for MD5_SHA1 */
  2155. S2N_TLS_HASH_MD5_SHA1 = 224
  2156. } s2n_tls_hash_algorithm;
  2157. /**
  2158. * Get the connection's selected signature algorithm.
  2159. *
  2160. * @param conn A pointer to the s2n_connection object
  2161. * @param chosen_alg A pointer to a s2n_tls_signature_algorithm object. This is an output parameter.
  2162. *
  2163. * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received.
  2164. */
  2165. S2N_API extern int s2n_connection_get_selected_signature_algorithm(struct s2n_connection *conn, s2n_tls_signature_algorithm *chosen_alg);
  2166. /**
  2167. * Get the connection's selected digest algorithm.
  2168. *
  2169. * @param conn A pointer to the s2n_connection object
  2170. * @param chosen_alg A pointer to a s2n_tls_hash_algorithm object. This is an output parameter.
  2171. *
  2172. * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received.
  2173. */
  2174. S2N_API extern int s2n_connection_get_selected_digest_algorithm(struct s2n_connection *conn, s2n_tls_hash_algorithm *chosen_alg);
  2175. /**
  2176. * Get the client certificate's signature algorithm.
  2177. *
  2178. * @param conn A pointer to the s2n_connection object
  2179. * @param chosen_alg A pointer to a s2n_tls_signature_algorithm object. This is an output parameter.
  2180. *
  2181. * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received.
  2182. */
  2183. S2N_API extern int s2n_connection_get_selected_client_cert_signature_algorithm(struct s2n_connection *conn, s2n_tls_signature_algorithm *chosen_alg);
  2184. /**
  2185. * Get the client certificate's digest algorithm.
  2186. *
  2187. * @param conn A pointer to the s2n_connection object
  2188. * @param chosen_alg A pointer to a s2n_tls_hash_algorithm object. This is an output parameter.
  2189. *
  2190. * @returns S2N_SUCCESS on success. S2N_FAILURE if bad parameters are received.
  2191. */
  2192. S2N_API extern int s2n_connection_get_selected_client_cert_digest_algorithm(struct s2n_connection *conn, s2n_tls_hash_algorithm *chosen_alg);
  2193. /**
  2194. * Get the certificate used during the TLS handshake
  2195. *
  2196. * - If `conn` is a server connection, the certificate selected will depend on the
  2197. * ServerName sent by the client and supported ciphers.
  2198. * - If `conn` is a client connection, the certificate sent in response to a CertificateRequest
  2199. * message is returned. Currently s2n-tls supports loading only one certificate in client mode. Note that
  2200. * not all TLS endpoints will request a certificate.
  2201. *
  2202. * @param conn A pointer to the s2n_connection object
  2203. *
  2204. * @returns NULL if the certificate selection phase of the handshake has not completed or if a certificate was not requested by the peer
  2205. */
  2206. S2N_API extern struct s2n_cert_chain_and_key *s2n_connection_get_selected_cert(struct s2n_connection *conn);
  2207. /**
  2208. * @param chain_and_key A pointer to the s2n_cert_chain_and_key object being read.
  2209. * @param cert_length This return value represents the length of the s2n certificate chain `chain_and_key`.
  2210. * @returns the length of the s2n certificate chain `chain_and_key`.
  2211. */
  2212. S2N_API extern int s2n_cert_chain_get_length(const struct s2n_cert_chain_and_key *chain_and_key, uint32_t *cert_length);
  2213. /**
  2214. * Returns the certificate `out_cert` present at the index `cert_idx` of the certificate chain `chain_and_key`.
  2215. *
  2216. * Note that the index of the leaf certificate is zero. If the certificate chain `chain_and_key` is NULL or the
  2217. * certificate index value is not in the acceptable range for the input certificate chain, an error is returned.
  2218. *
  2219. * # Safety
  2220. *
  2221. * There is no memory allocation required for `out_cert` buffer prior to calling the `s2n_cert_chain_get_cert` API.
  2222. * The `out_cert` will contain the pointer to the s2n_cert initialized within the input s2n_cert_chain_and_key `chain_and_key`.
  2223. * The pointer to the output s2n certificate `out_cert` is valid until `chain_and_key` is freed up.
  2224. * If a caller wishes to persist the `out_cert` beyond the lifetime of `chain_and_key`, the contents would need to be
  2225. * copied prior to freeing `chain_and_key`.
  2226. *
  2227. * @param chain_and_key A pointer to the s2n_cert_chain_and_key object being read.
  2228. * @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`.
  2229. * @param cert_idx The certificate index for the requested certificate within the s2n certificate chain.
  2230. */
  2231. 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);
  2232. /**
  2233. * Returns the s2n certificate in DER format along with its length.
  2234. *
  2235. * The API gets the s2n certificate `cert` in DER format. The certificate is returned in the `out_cert_der` buffer.
  2236. * Here, `cert_len` represents the length of the certificate.
  2237. *
  2238. * A caller can use certificate parsing tools such as the ones provided by OpenSSL to parse the DER encoded certificate chain returned.
  2239. *
  2240. * # Safety
  2241. *
  2242. * The memory for the `out_cert_der` buffer is allocated and owned by s2n-tls.
  2243. * Since the size of the certificate can potentially be very large, a pointer to internal connection data is returned instead of
  2244. * copying the contents into a caller-provided buffer.
  2245. *
  2246. * The pointer to the output buffer `out_cert_der` is valid only while the connection exists.
  2247. * The `s2n_connection_free` API frees the memory associated with the out_cert_der buffer and after the `s2n_connection_wipe` API is
  2248. * called the memory pointed by out_cert_der is invalid.
  2249. *
  2250. * If a caller wishes to persist the `out_cert_der` beyond the lifetime of the connection, the contents would need to be
  2251. * copied prior to the connection termination.
  2252. *
  2253. * @param cert A pointer to the s2n_cert object being read.
  2254. * @param out_cert_der A pointer to the output buffer which will hold the s2n certificate `cert` in DER format.
  2255. * @param cert_length This return value represents the length of the certificate.
  2256. */
  2257. S2N_API extern int s2n_cert_get_der(const struct s2n_cert *cert, const uint8_t **out_cert_der, uint32_t *cert_length);
  2258. /**
  2259. * Returns the validated peer certificate chain as a `s2n_cert_chain_and_key` opaque object.
  2260. *
  2261. * The `s2n_cert_chain_and_key` parameter must be allocated by the caller using the `s2n_cert_chain_and_key_new` API
  2262. * prior to this function call and must be empty. To free the memory associated with the `s2n_cert_chain_and_key` object use the
  2263. * `s2n_cert_chain_and_key_free` API.
  2264. *
  2265. * @param conn A pointer to the s2n_connection object being read.
  2266. * @param cert_chain The returned validated peer certificate chain `cert_chain` retrieved from the s2n connection.
  2267. */
  2268. S2N_API extern int s2n_connection_get_peer_cert_chain(const struct s2n_connection *conn, struct s2n_cert_chain_and_key *cert_chain);
  2269. /**
  2270. * Returns the length of the DER encoded extension value of the ASN.1 X.509 certificate extension.
  2271. *
  2272. * @param cert A pointer to the s2n_cert object being read.
  2273. * @param oid A null-terminated cstring that contains the OID of the X.509 certificate extension to be read.
  2274. * @param ext_value_len This return value contains the length of DER encoded extension value of the ASN.1 X.509 certificate extension.
  2275. */
  2276. S2N_API extern int s2n_cert_get_x509_extension_value_length(struct s2n_cert *cert, const uint8_t *oid, uint32_t *ext_value_len);
  2277. /**
  2278. * Returns the DER encoding of an ASN.1 X.509 certificate extension value, it's length and a boolean critical.
  2279. *
  2280. * @param cert A pointer to the s2n_cert object being read.
  2281. * @param oid A null-terminated cstring that contains the OID of the X.509 certificate extension to be read.
  2282. * @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.
  2283. * @param ext_value_len This value is both an input and output parameter and represents the length of the output buffer `ext_value`.
  2284. * When used as an input parameter, the caller must use this parameter to convey the maximum length of `ext_value`.
  2285. * 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.
  2286. * @param critical This return value contains the boolean value for `critical`.
  2287. */
  2288. 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);
  2289. /**
  2290. * Returns the UTF8 String length of the ASN.1 X.509 certificate extension data.
  2291. *
  2292. * @param extension_data A pointer to the DER encoded ASN.1 X.509 certificate extension value being read.
  2293. * @param extension_len represents the length of the input buffer `extension_data`.
  2294. * @param utf8_str_len This return value contains the UTF8 String length of the ASN.1 X.509 certificate extension data.
  2295. */
  2296. 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);
  2297. /**
  2298. * Returns the UTF8 String representation of the DER encoded ASN.1 X.509 certificate extension data.
  2299. *
  2300. * @param extension_data A pointer to the DER encoded ASN.1 X.509 certificate extension value being read.
  2301. * @param extension_len represents the length of the input buffer `extension_data`.
  2302. * @param out_data A pointer to the output buffer which will hold the UTF8 String representation of the DER encoded ASN.1 X.509
  2303. * certificate extension data returned.
  2304. * @param out_len This value is both an input and output parameter and represents the length of the output buffer `out_data`.
  2305. * When used as an input parameter, the caller must use this parameter to convey the maximum length of `out_data`.
  2306. * When used as an output parameter, `out_len` holds the actual length of UTF8 String returned.
  2307. */
  2308. 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);
  2309. /**
  2310. * Pre-shared key (PSK) Hash Algorithm - RFC 8446 Section-2.2
  2311. */
  2312. typedef enum {
  2313. S2N_PSK_HMAC_SHA256,
  2314. S2N_PSK_HMAC_SHA384,
  2315. } s2n_psk_hmac;
  2316. /**
  2317. * Opaque pre shared key handle
  2318. */
  2319. struct s2n_psk;
  2320. /**
  2321. * Creates a new s2n external pre-shared key (PSK) object with `S2N_PSK_HMAC_SHA256` as the default
  2322. * PSK hash algorithm. An external PSK is a key established outside of TLS using a secure mutually agreed upon mechanism.
  2323. *
  2324. * Use `s2n_psk_free` to free the memory allocated to the s2n external PSK object created by this API.
  2325. *
  2326. * @returns struct s2n_psk* Returns a pointer to the newly created external PSK object.
  2327. */
  2328. S2N_API struct s2n_psk *s2n_external_psk_new(void);
  2329. /**
  2330. * Frees the memory associated with the external PSK object.
  2331. *
  2332. * @param psk Pointer to the PSK object to be freed.
  2333. */
  2334. S2N_API int s2n_psk_free(struct s2n_psk **psk);
  2335. /**
  2336. * Sets the identity for a given external PSK object.
  2337. * The identity is a unique identifier for the pre-shared secret.
  2338. * It is a non-secret value represented by raw bytes.
  2339. *
  2340. * # Safety
  2341. *
  2342. * The identity is transmitted over the network unencrypted and is a non-secret value.
  2343. * Do not include confidential information in the identity.
  2344. *
  2345. * Note that the identity is copied into s2n-tls memory and the caller is responsible for
  2346. * freeing the memory associated with the identity input.
  2347. *
  2348. * @param psk A pointer to a PSK object to be updated with the identity.
  2349. * @param identity The identity in raw bytes format to be copied.
  2350. * @param identity_size The length of the PSK identity being set.
  2351. */
  2352. S2N_API int s2n_psk_set_identity(struct s2n_psk *psk, const uint8_t *identity, uint16_t identity_size);
  2353. /**
  2354. * Sets the out-of-band/externally provisioned secret for a given external PSK object.
  2355. *
  2356. * # Safety
  2357. *
  2358. * Note that the secret is copied into s2n-tls memory and the caller is responsible for
  2359. * freeing the memory associated with the `secret` input.
  2360. *
  2361. * Deriving a shared secret from a password or other low-entropy source
  2362. * is not secure and is subject to dictionary attacks.
  2363. * See https://tools.ietf.org/rfc/rfc8446#section-2.2 for more information.
  2364. *
  2365. * @param psk A pointer to a PSK object to be updated with the secret.
  2366. * @param secret The secret in raw bytes format to be copied.
  2367. * @param secret_size The length of the pre-shared secret being set.
  2368. */
  2369. S2N_API int s2n_psk_set_secret(struct s2n_psk *psk, const uint8_t *secret, uint16_t secret_size);
  2370. /**
  2371. * Sets the hash algorithm for a given external PSK object. The supported PSK hash
  2372. * algorithms are as listed in the enum `s2n_psk_hmac` above.
  2373. *
  2374. * @param psk A pointer to the external PSK object to be updated with the PSK hash algorithm.
  2375. * @param hmac The PSK hash algorithm being set.
  2376. */
  2377. S2N_API int s2n_psk_set_hmac(struct s2n_psk *psk, s2n_psk_hmac hmac);
  2378. /**
  2379. * Appends a PSK object to the list of PSKs supported by the s2n connection.
  2380. * If a PSK with a duplicate identity is found, an error is returned and the PSK is not added to the list.
  2381. * Note that a copy of `psk` is stored on the connection. The user is still responsible for freeing the
  2382. * memory associated with `psk`.
  2383. *
  2384. * @param conn A pointer to the s2n_connection object that contains the list of PSKs supported.
  2385. * @param psk A pointer to the `s2n_psk` object to be appended to the list of PSKs on the s2n connection.
  2386. */
  2387. S2N_API int s2n_connection_append_psk(struct s2n_connection *conn, struct s2n_psk *psk);
  2388. /**
  2389. * The list of PSK modes supported by s2n-tls for TLS versions >= TLS1.3.
  2390. * Currently s2n-tls supports two modes - `S2N_PSK_MODE_RESUMPTION`, which represents the PSKs established
  2391. * using the previous connection via session resumption, and `S2N_PSK_MODE_EXTERNAL`, which represents PSKs
  2392. * established out-of-band/externally using a secure mutually agreed upon mechanism.
  2393. */
  2394. typedef enum {
  2395. S2N_PSK_MODE_RESUMPTION,
  2396. S2N_PSK_MODE_EXTERNAL
  2397. } s2n_psk_mode;
  2398. /**
  2399. * Sets the PSK mode on the s2n config object.
  2400. * The supported PSK modes are listed in the enum `s2n_psk_mode` above.
  2401. *
  2402. * @param config A pointer to the s2n_config object being updated.
  2403. * @param mode The PSK mode to be set.
  2404. */
  2405. S2N_API int s2n_config_set_psk_mode(struct s2n_config *config, s2n_psk_mode mode);
  2406. /**
  2407. * Sets the PSK mode on the s2n connection object.
  2408. * The supported PSK modes are listed in the enum `s2n_psk_mode` above.
  2409. * This API overrides the PSK mode set on config for this connection.
  2410. *
  2411. * @param conn A pointer to the s2n_connection object being updated.
  2412. * @param mode The PSK mode to be set.
  2413. */
  2414. S2N_API int s2n_connection_set_psk_mode(struct s2n_connection *conn, s2n_psk_mode mode);
  2415. /**
  2416. * Gets the negotiated PSK identity length from the s2n connection object. The negotiated PSK
  2417. * refers to the chosen PSK by the server to be used for the connection.
  2418. *
  2419. * This API can be used to determine if the negotiated PSK exists. If negotiated PSK exists a
  2420. * call to this API returns a value greater than zero. If the negotiated PSK does not exist, the
  2421. * value `0` is returned.
  2422. *
  2423. * @param conn A pointer to the s2n_connection object that successfully negotiated a PSK connection.
  2424. * @param identity_length The length of the negotiated PSK identity.
  2425. */
  2426. S2N_API int s2n_connection_get_negotiated_psk_identity_length(struct s2n_connection *conn, uint16_t *identity_length);
  2427. /**
  2428. * Gets the negotiated PSK identity from the s2n connection object.
  2429. * If the negotiated PSK does not exist, the PSK identity will not be obtained and no error will be returned.
  2430. * Prior to this API call, use `s2n_connection_get_negotiated_psk_identity_length` to determine if a
  2431. * negotiated PSK exists or not.
  2432. *
  2433. * # Safety
  2434. *
  2435. * The negotiated PSK identity will be copied into the identity buffer on success.
  2436. * Therefore, the identity buffer must have enough memory to fit the identity length.
  2437. *
  2438. * @param conn A pointer to the s2n_connection object.
  2439. * @param identity The negotiated PSK identity obtained from the s2n_connection object.
  2440. * @param max_identity_length The maximum length for the PSK identity. If the negotiated psk_identity length is
  2441. * greater than this `max_identity_length` value an error will be returned.
  2442. */
  2443. S2N_API int s2n_connection_get_negotiated_psk_identity(struct s2n_connection *conn, uint8_t *identity, uint16_t max_identity_length);
  2444. struct s2n_offered_psk;
  2445. /**
  2446. * Creates a new s2n offered PSK object.
  2447. * An offered PSK object represents a single PSK sent by the client.
  2448. *
  2449. * # Safety
  2450. *
  2451. * Use `s2n_offered_psk_free` to free the memory allocated to the s2n offered PSK object created by this API.
  2452. *
  2453. * @returns struct s2n_offered_psk* Returns a pointer to the newly created offered PSK object.
  2454. */
  2455. S2N_API struct s2n_offered_psk *s2n_offered_psk_new(void);
  2456. /**
  2457. * Frees the memory associated with the `s2n_offered_psk` object.
  2458. *
  2459. * @param psk A pointer to the `s2n_offered_psk` object to be freed.
  2460. */
  2461. S2N_API int s2n_offered_psk_free(struct s2n_offered_psk **psk);
  2462. /**
  2463. * Gets the PSK identity and PSK identity length for a given offered PSK object.
  2464. *
  2465. * @param psk A pointer to the offered PSK object being read.
  2466. * @param identity The PSK identity being obtained.
  2467. * @param size The length of the PSK identity being obtained.
  2468. */
  2469. S2N_API int s2n_offered_psk_get_identity(struct s2n_offered_psk *psk, uint8_t **identity, uint16_t *size);
  2470. struct s2n_offered_psk_list;
  2471. /**
  2472. * Checks whether the offered PSK list has an offered psk object next in line in the list.
  2473. * An offered PSK list contains all the PSKs offered by the client for the server to select.
  2474. *
  2475. * # Safety
  2476. *
  2477. * This API returns a pointer to the s2n-tls internal memory with limited lifetime.
  2478. * After the completion of `s2n_psk_selection_callback` this pointer is invalid.
  2479. *
  2480. * @param psk_list A pointer to the offered PSK list being read.
  2481. * @returns bool A boolean value representing whether an offered psk object is present next in line in the offered PSK list.
  2482. */
  2483. S2N_API bool s2n_offered_psk_list_has_next(struct s2n_offered_psk_list *psk_list);
  2484. /**
  2485. * Obtains the next offered PSK object from the list of offered PSKs. Use `s2n_offered_psk_list_has_next`
  2486. * prior to this API call to ensure we have not reached the end of the list.
  2487. *
  2488. * @param psk_list A pointer to the offered PSK list being read.
  2489. * @param psk A pointer to the next offered PSK object being obtained.
  2490. */
  2491. S2N_API int s2n_offered_psk_list_next(struct s2n_offered_psk_list *psk_list, struct s2n_offered_psk *psk);
  2492. /**
  2493. * Returns the offered PSK list to its original read state.
  2494. *
  2495. * When `s2n_offered_psk_list_reread` is called, `s2n_offered_psk_list_next` will return the first PSK
  2496. * in the offered PSK list.
  2497. *
  2498. * @param psk_list A pointer to the offered PSK list being reread.
  2499. */
  2500. S2N_API int s2n_offered_psk_list_reread(struct s2n_offered_psk_list *psk_list);
  2501. /**
  2502. * Chooses a PSK from the offered PSK list to be used for the connection.
  2503. * This API matches the PSK identity received from the client against the server's known PSK identities
  2504. * list, in order to choose the PSK to be used for the connection. If the PSK identity sent from the client
  2505. * is NULL, no PSK is chosen for the connection. If the client offered PSK identity has no matching PSK identity
  2506. * with the server, an error will be returned. Use this API along with the `s2n_psk_selection_callback` callback
  2507. * to select a PSK identity.
  2508. *
  2509. * @param psk_list A pointer to the server's known PSK list used to compare for a matching PSK with the client.
  2510. * @param psk A pointer to the client's PSK object used to compare with the server's known PSK identities.
  2511. */
  2512. S2N_API int s2n_offered_psk_list_choose_psk(struct s2n_offered_psk_list *psk_list, struct s2n_offered_psk *psk);
  2513. /**
  2514. * Callback function to select a PSK from a list of offered PSKs.
  2515. * Use this callback to implement custom PSK selection logic. The s2n-tls default PSK selection logic
  2516. * chooses the first matching PSK from the list of offered PSKs sent by the client.
  2517. *
  2518. * # Safety
  2519. *
  2520. * `context` is a void pointer and the caller is responsible for ensuring it is cast to the correct type.
  2521. * After the completion of this callback, the pointer to `psk_list` is invalid.
  2522. *
  2523. * @param conn A pointer to the s2n_connection object.
  2524. * @param context A pointer to a context for the caller to pass state to the callback, if needed.
  2525. * @param psk_list A pointer to the offered PSK list being read.
  2526. */
  2527. typedef int (*s2n_psk_selection_callback)(struct s2n_connection *conn, void *context,
  2528. struct s2n_offered_psk_list *psk_list);
  2529. /**
  2530. * Sets the callback to select the matching PSK.
  2531. * If this callback is not set s2n-tls uses a default PSK selection logic that selects the first matching
  2532. * server PSK.
  2533. *
  2534. * @param config A pointer to the s2n_config object.
  2535. * @param cb The function that should be called when the callback is triggered.
  2536. * @param context A pointer to a context for the caller to pass state to the callback, if needed.
  2537. */
  2538. S2N_API int s2n_config_set_psk_selection_callback(struct s2n_config *config, s2n_psk_selection_callback cb, void *context);
  2539. /**
  2540. * Get the number of bytes the connection has received.
  2541. *
  2542. * @param conn A pointer to the connection
  2543. * @returns return the number of bytes received by s2n-tls "on the wire"
  2544. */
  2545. S2N_API extern uint64_t s2n_connection_get_wire_bytes_in(struct s2n_connection *conn);
  2546. /**
  2547. * Get the number of bytes the connection has transmitted out.
  2548. *
  2549. * @param conn A pointer to the connection
  2550. * @returns return the number of bytes transmitted out by s2n-tls "on the wire"
  2551. */
  2552. S2N_API extern uint64_t s2n_connection_get_wire_bytes_out(struct s2n_connection *conn);
  2553. /**
  2554. * Access the protocol version supported by the client.
  2555. *
  2556. * @note The return value corresponds to the macros defined as S2N_SSLv2,
  2557. * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13.
  2558. *
  2559. * @param conn A pointer to the connection
  2560. * @returns returns the highest protocol version supported by the client
  2561. */
  2562. S2N_API extern int s2n_connection_get_client_protocol_version(struct s2n_connection *conn);
  2563. /**
  2564. * Access the protocol version supported by the server.
  2565. *
  2566. * @note The return value corresponds to the macros defined as S2N_SSLv2,
  2567. * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13.
  2568. *
  2569. * @param conn A pointer to the connection
  2570. * @returns Returns the highest protocol version supported by the server
  2571. */
  2572. S2N_API extern int s2n_connection_get_server_protocol_version(struct s2n_connection *conn);
  2573. /**
  2574. * Access the protocol version selected for the connection.
  2575. *
  2576. * @note The return value corresponds to the macros defined as S2N_SSLv2,
  2577. * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13.
  2578. *
  2579. * @param conn A pointer to the connection
  2580. * @returns The protocol version actually negotiated by the handshake
  2581. */
  2582. S2N_API extern int s2n_connection_get_actual_protocol_version(struct s2n_connection *conn);
  2583. /**
  2584. * Access the client hello protocol version for the connection.
  2585. *
  2586. * @note The return value corresponds to the macros defined as S2N_SSLv2,
  2587. * S2N_SSLv3, S2N_TLS10, S2N_TLS11, S2N_TLS12, and S2N_TLS13.
  2588. *
  2589. * @param conn A pointer to the connection
  2590. * @returns The protocol version used to send the initial client hello message.
  2591. */
  2592. S2N_API extern int s2n_connection_get_client_hello_version(struct s2n_connection *conn);
  2593. /**
  2594. * Check if Client Auth was used for a connection.
  2595. *
  2596. * @param conn A pointer to the connection
  2597. * @returns 1 if the handshake completed and Client Auth was negotiated during then
  2598. * handshake.
  2599. */
  2600. S2N_API extern int s2n_connection_client_cert_used(struct s2n_connection *conn);
  2601. /**
  2602. * A function that provides a human readable string of the cipher suite that was chosen
  2603. * for a connection.
  2604. *
  2605. * @warning The string "TLS_NULL_WITH_NULL_NULL" is returned before the TLS handshake has been performed.
  2606. * This does not mean that the ciphersuite "TLS_NULL_WITH_NULL_NULL" will be used by the connection,
  2607. * it is merely being used as a placeholder.
  2608. *
  2609. * @note This function is only accurate after the TLS handshake.
  2610. *
  2611. * @param conn A pointer to the connection
  2612. * @returns A string indicating the cipher suite negotiated by s2n in OpenSSL format.
  2613. */
  2614. S2N_API extern const char *s2n_connection_get_cipher(struct s2n_connection *conn);
  2615. /**
  2616. * Provides access to the TLS-Exporter functionality.
  2617. *
  2618. * See https://datatracker.ietf.org/doc/html/rfc5705 and https://www.rfc-editor.org/rfc/rfc8446.
  2619. *
  2620. * @note This is currently only available with TLS 1.3 connections which have finished a handshake.
  2621. *
  2622. * @param conn A pointer to the connection
  2623. * @returns A POSIX error signal. If an error was returned, the value contained in `output` should be considered invalid.
  2624. */
  2625. S2N_API extern int s2n_connection_tls_exporter(struct s2n_connection *conn,
  2626. const uint8_t *label, uint32_t label_length, const uint8_t *context, uint32_t context_length,
  2627. uint8_t *output, uint32_t output_length);
  2628. /**
  2629. * Returns the IANA value for the connection's negotiated cipher suite.
  2630. *
  2631. * The value is returned in the form of `first,second`, in order to closely match
  2632. * the values defined in the [IANA Registry](https://www.iana.org/assignments/tls-parameters/tls-parameters.xhtml#table-tls-parameters-4).
  2633. * For example if the connection's negotiated cipher suite is `TLS_AES_128_GCM_SHA256`,
  2634. * which is registered as `0x13,0x01`, then `first = 0x13` and `second = 0x01`.
  2635. *
  2636. * This method will only succeed after the cipher suite has been negotiated with the peer.
  2637. *
  2638. * @param conn A pointer to the connection being read
  2639. * @param first A pointer to a single byte, which will be updated with the first byte in the registered IANA value.
  2640. * @param second A pointer to a single byte, which will be updated with the second byte in the registered IANA value.
  2641. * @returns A POSIX error signal. If an error was returned, the values contained in `first` and `second` should be considered invalid.
  2642. */
  2643. S2N_API extern int s2n_connection_get_cipher_iana_value(struct s2n_connection *conn, uint8_t *first, uint8_t *second);
  2644. /**
  2645. * Function to check if the cipher used by current connection is supported by the current
  2646. * cipher preferences.
  2647. * @param conn A pointer to the s2n connection
  2648. * @param version A string representing the security policy to check against.
  2649. * @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.
  2650. */
  2651. S2N_API extern int s2n_connection_is_valid_for_cipher_preferences(struct s2n_connection *conn, const char *version);
  2652. /**
  2653. * Function to get the human readable elliptic curve name for the connection.
  2654. *
  2655. * @param conn A pointer to the s2n connection
  2656. * @returns A string indicating the elliptic curve used during ECDHE key exchange. The string "NONE" is returned if no curve was used.
  2657. */
  2658. S2N_API extern const char *s2n_connection_get_curve(struct s2n_connection *conn);
  2659. /**
  2660. * Function to get the human readable KEM name for the connection.
  2661. *
  2662. * @param conn A pointer to the s2n connection
  2663. * @returns A human readable string for the KEM group. If there is no KEM configured returns "NONE"
  2664. */
  2665. S2N_API extern const char *s2n_connection_get_kem_name(struct s2n_connection *conn);
  2666. /**
  2667. * Function to get the human readable KEM group name for the connection.
  2668. *
  2669. * @param conn A pointer to the s2n connection
  2670. * @returns A human readable string for the KEM group. If the connection is < TLS1.3 or there is no KEM group configured returns "NONE"
  2671. */
  2672. S2N_API extern const char *s2n_connection_get_kem_group_name(struct s2n_connection *conn);
  2673. /**
  2674. * Function to get the alert that caused a connection to close. s2n-tls considers all
  2675. * TLS alerts fatal and shuts down a connection whenever one is received.
  2676. *
  2677. * @param conn A pointer to the s2n connection
  2678. * @returns The TLS alert code that caused a connection to be shut down
  2679. */
  2680. S2N_API extern int s2n_connection_get_alert(struct s2n_connection *conn);
  2681. /**
  2682. * Function to return the last TLS handshake type that was processed. The returned format is a human readable string.
  2683. *
  2684. * @param conn A pointer to the s2n connection
  2685. * @returns A human-readable handshake type name, e.g. "NEGOTIATED|FULL_HANDSHAKE|PERFECT_FORWARD_SECRECY"
  2686. */
  2687. S2N_API extern const char *s2n_connection_get_handshake_type_name(struct s2n_connection *conn);
  2688. /**
  2689. * Function to return the last TLS message that was processed. The returned format is a human readable string.
  2690. * @param conn A pointer to the s2n connection
  2691. * @returns The last message name in the TLS state machine, e.g. "SERVER_HELLO", "APPLICATION_DATA".
  2692. */
  2693. S2N_API extern const char *s2n_connection_get_last_message_name(struct s2n_connection *conn);
  2694. /**
  2695. * Opaque async private key operation handle
  2696. */
  2697. struct s2n_async_pkey_op;
  2698. /**
  2699. * Sets whether or not a connection should enforce strict signature validation during the
  2700. * `s2n_async_pkey_op_apply` call.
  2701. *
  2702. * `mode` can take the following values:
  2703. * - `S2N_ASYNC_PKEY_VALIDATION_FAST` - default behavior: s2n-tls will perform only the minimum validation required for safe use of the asyn pkey operation.
  2704. * - `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.
  2705. */
  2706. typedef enum {
  2707. S2N_ASYNC_PKEY_VALIDATION_FAST,
  2708. S2N_ASYNC_PKEY_VALIDATION_STRICT
  2709. } s2n_async_pkey_validation_mode;
  2710. /**
  2711. * The type of private key operation
  2712. */
  2713. typedef enum {
  2714. S2N_ASYNC_DECRYPT,
  2715. S2N_ASYNC_SIGN
  2716. } s2n_async_pkey_op_type;
  2717. /**
  2718. * Callback function for handling private key operations
  2719. *
  2720. * Invoked every time an operation requiring the private key is encountered
  2721. * during the handshake.
  2722. *
  2723. * # Safety
  2724. * * `op` is owned by the application and MUST be freed.
  2725. *
  2726. * @param conn Connection which triggered the callback
  2727. * @param op An opaque object representing the private key operation
  2728. */
  2729. typedef int (*s2n_async_pkey_fn)(struct s2n_connection *conn, struct s2n_async_pkey_op *op);
  2730. /**
  2731. * Sets up the callback to invoke when private key operations occur.
  2732. *
  2733. * @param config Config to set the callback
  2734. * @param fn The function that should be called for each private key operation
  2735. */
  2736. S2N_API extern int s2n_config_set_async_pkey_callback(struct s2n_config *config, s2n_async_pkey_fn fn);
  2737. /**
  2738. * Performs a private key operation using the given private key.
  2739. *
  2740. * # Safety
  2741. * * Can only be called once. Any subsequent calls will produce a `S2N_ERR_T_USAGE` error.
  2742. * * Safe to call from inside s2n_async_pkey_fn
  2743. * * Safe to call from a different thread, as long as no other thread is operating on `op`.
  2744. *
  2745. * @param op An opaque object representing the private key operation
  2746. * @param key The private key used for the operation. It can be extracted from
  2747. * `conn` through the `s2n_connection_get_selected_cert` and `s2n_cert_chain_and_key_get_private_key` calls
  2748. */
  2749. S2N_API extern int s2n_async_pkey_op_perform(struct s2n_async_pkey_op *op, s2n_cert_private_key *key);
  2750. /**
  2751. * Finalizes a private key operation and unblocks the connection.
  2752. *
  2753. * # Safety
  2754. * * `conn` must match the connection that originally triggered the callback.
  2755. * * Must be called after the operation is performed.
  2756. * * Can only be called once. Any subsequent calls will produce a `S2N_ERR_T_USAGE` error.
  2757. * * Safe to call from inside s2n_async_pkey_fn
  2758. * * Safe to call from a different thread, as long as no other thread is operating on `op`.
  2759. *
  2760. * @param op An opaque object representing the private key operation
  2761. * @param conn The connection associated with the operation that should be unblocked
  2762. */
  2763. S2N_API extern int s2n_async_pkey_op_apply(struct s2n_async_pkey_op *op, struct s2n_connection *conn);
  2764. /**
  2765. * Frees the opaque structure representing a private key operation.
  2766. *
  2767. * # Safety
  2768. * * MUST be called for every operation passed to s2n_async_pkey_fn
  2769. * * Safe to call before or after the connection that created the operation is freed
  2770. *
  2771. * @param op An opaque object representing the private key operation
  2772. */
  2773. S2N_API extern int s2n_async_pkey_op_free(struct s2n_async_pkey_op *op);
  2774. /**
  2775. * Configures whether or not s2n-tls will perform potentially expensive validation of
  2776. * the results of a private key operation.
  2777. *
  2778. * @param config Config to set the validation mode for
  2779. * @param mode What level of validation to perform
  2780. */
  2781. S2N_API extern int s2n_config_set_async_pkey_validation_mode(struct s2n_config *config, s2n_async_pkey_validation_mode mode);
  2782. /**
  2783. * Returns the type of the private key operation.
  2784. *
  2785. * @param op An opaque object representing the private key operation
  2786. * @param type A pointer to be set to the type
  2787. */
  2788. S2N_API extern int s2n_async_pkey_op_get_op_type(struct s2n_async_pkey_op *op, s2n_async_pkey_op_type *type);
  2789. /**
  2790. * Returns the size of the input to the private key operation.
  2791. *
  2792. * @param op An opaque object representing the private key operation
  2793. * @param data_len A pointer to be set to the size
  2794. */
  2795. S2N_API extern int s2n_async_pkey_op_get_input_size(struct s2n_async_pkey_op *op, uint32_t *data_len);
  2796. /**
  2797. * Returns the input to the private key operation.
  2798. *
  2799. * When signing, the input is the digest to sign.
  2800. * When decrypting, the input is the data to decrypt.
  2801. *
  2802. * # Safety
  2803. * * `data` must be sufficiently large to contain the input.
  2804. * `s2n_async_pkey_op_get_input_size` can be called to determine how much memory is required.
  2805. * * s2n-tls does not take ownership of `data`.
  2806. * The application still owns the memory and must free it if necessary.
  2807. *
  2808. * @param op An opaque object representing the private key operation
  2809. * @param data A pointer to a buffer to copy the input into
  2810. * @param data_len The maximum size of the `data` buffer
  2811. */
  2812. S2N_API extern int s2n_async_pkey_op_get_input(struct s2n_async_pkey_op *op, uint8_t *data, uint32_t data_len);
  2813. /**
  2814. * Sets the output of the private key operation.
  2815. *
  2816. * # Safety
  2817. * * s2n-tls does not take ownership of `data`.
  2818. * The application still owns the memory and must free it if necessary.
  2819. *
  2820. * @param op An opaque object representing the private key operation
  2821. * @param data A pointer to a buffer containing the output
  2822. * @param data_len The size of the `data` buffer
  2823. */
  2824. S2N_API extern int s2n_async_pkey_op_set_output(struct s2n_async_pkey_op *op, const uint8_t *data, uint32_t data_len);
  2825. /**
  2826. * Callback function for handling key log events
  2827. *
  2828. * THIS SHOULD BE USED FOR DEBUGGING PURPOSES ONLY!
  2829. *
  2830. * Each log line is formatted with the
  2831. * [NSS Key Log Format](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format)
  2832. * without a newline.
  2833. *
  2834. * # Safety
  2835. *
  2836. * * `ctx` MUST be cast into the same type of pointer that was originally created
  2837. * * `logline` bytes MUST be copied or discarded before this function returns
  2838. *
  2839. * @param ctx Context for the callback
  2840. * @param conn Connection for which the log line is being emitted
  2841. * @param logline Pointer to the log line data
  2842. * @param len Length of the log line data
  2843. */
  2844. typedef int (*s2n_key_log_fn)(void *ctx, struct s2n_connection *conn, uint8_t *logline, size_t len);
  2845. /**
  2846. * Sets a key logging callback on the provided config
  2847. *
  2848. * THIS SHOULD BE USED FOR DEBUGGING PURPOSES ONLY!
  2849. *
  2850. * Setting this function enables configurations to emit secrets in the
  2851. * [NSS Key Log Format](https://developer.mozilla.org/en-US/docs/Mozilla/Projects/NSS/Key_Log_Format)
  2852. *
  2853. * # Safety
  2854. *
  2855. * * `callback` MUST cast `ctx` into the same type of pointer that was originally created
  2856. * * `ctx` MUST live for at least as long as it is set on the config
  2857. *
  2858. * @param config Config to set the callback
  2859. * @param callback The function that should be called for each secret log entry
  2860. * @param ctx The context to be passed when the callback is called
  2861. */
  2862. S2N_API extern int s2n_config_set_key_log_cb(struct s2n_config *config, s2n_key_log_fn callback, void *ctx);
  2863. /**
  2864. * s2n_config_enable_cert_req_dss_legacy_compat adds a dss cert type in the server certificate request when being called.
  2865. * It only sends the dss cert type in the cert request but does not succeed the handshake if a dss cert is received.
  2866. * Please DO NOT call this api unless you know you actually need legacy DSS certificate type compatibility
  2867. * @param config Config to enable legacy DSS certificates for
  2868. */
  2869. S2N_API extern int s2n_config_enable_cert_req_dss_legacy_compat(struct s2n_config *config);
  2870. /**
  2871. * Sets the maximum bytes of early data the server will accept.
  2872. *
  2873. * The default maximum is 0. If the maximum is 0, the server rejects all early data requests.
  2874. * The config maximum can be overridden by the connection maximum or the maximum on an external pre-shared key.
  2875. *
  2876. * @param config A pointer to the config
  2877. * @param max_early_data_size The maximum early data that the server will accept
  2878. * @returns A POSIX error signal. If successful, the maximum early data size was updated.
  2879. */
  2880. S2N_API int s2n_config_set_server_max_early_data_size(struct s2n_config *config, uint32_t max_early_data_size);
  2881. /**
  2882. * Sets the maximum bytes of early data the server will accept.
  2883. *
  2884. * The default maximum is 0. If the maximum is 0, the server rejects all early data requests.
  2885. * The connection maximum can be overridden by the maximum on an external pre-shared key.
  2886. *
  2887. * @param conn A pointer to the connection
  2888. * @param max_early_data_size The maximum early data the server will accept
  2889. * @returns A POSIX error signal. If successful, the maximum early data size was updated.
  2890. */
  2891. S2N_API int s2n_connection_set_server_max_early_data_size(struct s2n_connection *conn, uint32_t max_early_data_size);
  2892. /**
  2893. * Sets the user context associated with early data on a server.
  2894. *
  2895. * This context is passed to the `s2n_early_data_cb` callback to help decide whether to accept or reject early data.
  2896. *
  2897. * Unlike most contexts, the early data context is a byte buffer instead of a void pointer.
  2898. * This is because we need to serialize the context into session tickets.
  2899. *
  2900. * This API is intended for use with session resumption, and will not affect pre-shared keys.
  2901. *
  2902. * @param conn A pointer to the connection
  2903. * @param context A pointer to the user context data. This data will be copied.
  2904. * @param context_size The size of the data to read from the `context` pointer.
  2905. * @returns A POSIX error signal. If successful, the context was updated.
  2906. */
  2907. S2N_API int s2n_connection_set_server_early_data_context(struct s2n_connection *conn, const uint8_t *context, uint16_t context_size);
  2908. /**
  2909. * Configures a particular pre-shared key to allow early data.
  2910. *
  2911. * `max_early_data_size` must be set to the maximum early data accepted by the server.
  2912. *
  2913. * In order to use early data, the cipher suite set on the pre-shared key must match the cipher suite
  2914. * ultimately negotiated by the TLS handshake. Additionally, the cipher suite must have the same
  2915. * hmac algorithm as the pre-shared key.
  2916. *
  2917. * @param psk A pointer to the pre-shared key, created with `s2n_external_psk_new`.
  2918. * @param max_early_data_size The maximum early data that can be sent or received using this key.
  2919. * @param cipher_suite_first_byte The first byte in the registered IANA value of the associated cipher suite.
  2920. * @param cipher_suite_second_byte The second byte in the registered IANA value of the associated cipher suite.
  2921. * @returns A POSIX error signal. If successful, `psk` was updated.
  2922. */
  2923. S2N_API int s2n_psk_configure_early_data(struct s2n_psk *psk, uint32_t max_early_data_size,
  2924. uint8_t cipher_suite_first_byte, uint8_t cipher_suite_second_byte);
  2925. /**
  2926. * Sets the optional `application_protocol` associated with the given pre-shared key.
  2927. *
  2928. * In order to use early data, the `application_protocol` set on the pre-shared key must match
  2929. * the `application_protocol` ultimately negotiated by the TLS handshake.
  2930. *
  2931. * @param psk A pointer to the pre-shared key, created with `s2n_external_psk_new`.
  2932. * @param application_protocol A pointer to the associated application protocol data. This data will be copied.
  2933. * @param size The size of the data to read from the `application_protocol` pointer.
  2934. * @returns A POSIX error signal. If successful, the application protocol was set.
  2935. */
  2936. S2N_API int s2n_psk_set_application_protocol(struct s2n_psk *psk, const uint8_t *application_protocol, uint8_t size);
  2937. /**
  2938. * Sets the optional user early data context associated with the given pre-shared key.
  2939. *
  2940. * The early data context is passed to the `s2n_early_data_cb` callback to help decide whether
  2941. * to accept or reject early data.
  2942. *
  2943. * @param psk A pointer to the pre-shared key, created with `s2n_external_psk_new`.
  2944. * @param context A pointer to the associated user context data. This data will be copied.
  2945. * @param size The size of the data to read from the `context` pointer.
  2946. * @returns A POSIX error signal. If successful, the context was set.
  2947. */
  2948. S2N_API int s2n_psk_set_early_data_context(struct s2n_psk *psk, const uint8_t *context, uint16_t size);
  2949. /**
  2950. * The status of early data on a connection.
  2951. *
  2952. * S2N_EARLY_DATA_STATUS_OK: Early data is in progress.
  2953. * S2N_EARLY_DATA_STATUS_NOT_REQUESTED: The client did not request early data, so none was sent or received.
  2954. * S2N_EARLY_DATA_STATUS_REJECTED: The client requested early data, but the server rejected the request.
  2955. * Early data may have been sent, but was not received.
  2956. * S2N_EARLY_DATA_STATUS_END: All early data was successfully sent and received.
  2957. */
  2958. typedef enum {
  2959. S2N_EARLY_DATA_STATUS_OK,
  2960. S2N_EARLY_DATA_STATUS_NOT_REQUESTED,
  2961. S2N_EARLY_DATA_STATUS_REJECTED,
  2962. S2N_EARLY_DATA_STATUS_END,
  2963. } s2n_early_data_status_t;
  2964. /**
  2965. * Reports the current state of early data for a connection.
  2966. *
  2967. * See `s2n_early_data_status_t` for all possible states.
  2968. *
  2969. * @param conn A pointer to the connection
  2970. * @param status A pointer which will be set to the current early data status
  2971. * @returns A POSIX error signal.
  2972. */
  2973. S2N_API int s2n_connection_get_early_data_status(struct s2n_connection *conn, s2n_early_data_status_t *status);
  2974. /**
  2975. * Reports the remaining size of the early data allowed by a connection.
  2976. *
  2977. * If early data was rejected or not requested, the remaining early data size is 0.
  2978. * Otherwise, the remaining early data size is the maximum early data allowed by the connection,
  2979. * minus the early data sent or received so far.
  2980. *
  2981. * @param conn A pointer to the connection
  2982. * @param allowed_early_data_size A pointer which will be set to the remaining early data currently allowed by `conn`
  2983. * @returns A POSIX error signal.
  2984. */
  2985. S2N_API int s2n_connection_get_remaining_early_data_size(struct s2n_connection *conn, uint32_t *allowed_early_data_size);
  2986. /**
  2987. * Reports the maximum size of the early data allowed by a connection.
  2988. *
  2989. * This is the maximum amount of early data that can ever be sent and received for a connection.
  2990. * It is not affected by the actual status of the early data, so can be non-zero even if early data
  2991. * is rejected or not requested.
  2992. *
  2993. * @param conn A pointer to the connection
  2994. * @param max_early_data_size A pointer which will be set to the maximum early data allowed by `conn`
  2995. * @returns A POSIX error signal.
  2996. */
  2997. S2N_API int s2n_connection_get_max_early_data_size(struct s2n_connection *conn, uint32_t *max_early_data_size);
  2998. /**
  2999. * Called by the client to begin negotiation and send early data.
  3000. *
  3001. * See https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md#using-early-data--0rtt
  3002. * for usage and examples. DO NOT USE unless you have considered the security issues and
  3003. * implemented mitigation for anti-replay attacks.
  3004. *
  3005. * @param conn A pointer to the connection
  3006. * @param data A pointer to the early data to be sent
  3007. * @param data_len The size of the early data to send
  3008. * @param data_sent A pointer which will be set to the size of the early data sent
  3009. * @param blocked A pointer which will be set to the blocked status, as in `s2n_negotiate`.
  3010. * @returns A POSIX error signal. The error should be handled as in `s2n_negotiate`.
  3011. */
  3012. S2N_API int s2n_send_early_data(struct s2n_connection *conn, const uint8_t *data, ssize_t data_len,
  3013. ssize_t *data_sent, s2n_blocked_status *blocked);
  3014. /**
  3015. * Called by the server to begin negotiation and accept any early data the client sends.
  3016. *
  3017. * See https://github.com/aws/s2n-tls/blob/main/docs/USAGE-GUIDE.md#using-early-data--0rtt
  3018. * for usage and examples. DO NOT USE unless you have considered the security issues and
  3019. * implemented mitigation for anti-replay attacks.
  3020. *
  3021. * @param conn A pointer to the connection
  3022. * @param data A pointer to a buffer to store the early data received
  3023. * @param max_data_len The size of the early data buffer
  3024. * @param data_received A pointer which will be set to the size of the early data received
  3025. * @param blocked A pointer which will be set to the blocked status, as in `s2n_negotiate`.
  3026. * @returns A POSIX error signal. The error should be handled as in `s2n_negotiate`.
  3027. */
  3028. S2N_API int s2n_recv_early_data(struct s2n_connection *conn, uint8_t *data, ssize_t max_data_len,
  3029. ssize_t *data_received, s2n_blocked_status *blocked);
  3030. struct s2n_offered_early_data;
  3031. /**
  3032. * A callback which can be implemented to accept or reject early data.
  3033. *
  3034. * This callback is triggered only after the server has determined early data is otherwise acceptable according
  3035. * to the TLS early data specification. Implementations therefore only need to cover application-specific checks,
  3036. * not the standard TLS early data validation.
  3037. *
  3038. * This callback can be synchronous or asynchronous. For asynchronous behavior, return success without
  3039. * calling `s2n_offered_early_data_reject` or `s2n_offered_early_data_accept`. `early_data` will
  3040. * still be a valid reference, and the connection will block until `s2n_offered_early_data_reject` or
  3041. * `s2n_offered_early_data_accept` is called.
  3042. *
  3043. * @param conn A pointer to the connection
  3044. * @param early_data A pointer which can be used to access information about the proposed early data
  3045. * and then accept or reject it.
  3046. * @returns A POSIX error signal. If unsuccessful, the connection will be closed with an error.
  3047. */
  3048. typedef int (*s2n_early_data_cb)(struct s2n_connection *conn, struct s2n_offered_early_data *early_data);
  3049. /**
  3050. * Set a callback to accept or reject early data.
  3051. *
  3052. * @param config A pointer to the connection config
  3053. * @param cb A pointer to the implementation of the callback.
  3054. * @returns A POSIX error signal. If successful, the callback was set.
  3055. */
  3056. S2N_API int s2n_config_set_early_data_cb(struct s2n_config *config, s2n_early_data_cb cb);
  3057. /**
  3058. * Get the length of the early data context set by the user.
  3059. *
  3060. * @param early_data A pointer to the early data information
  3061. * @param context_len The length of the user context
  3062. * @returns A POSIX error signal.
  3063. */
  3064. S2N_API int s2n_offered_early_data_get_context_length(struct s2n_offered_early_data *early_data, uint16_t *context_len);
  3065. /**
  3066. * Get the early data context set by the user.
  3067. *
  3068. * @param early_data A pointer to the early data information
  3069. * @param context A byte buffer to copy the user context into
  3070. * @param max_len The size of `context`. Must be >= to the result of `s2n_offered_early_data_get_context_length`.
  3071. * @returns A POSIX error signal.
  3072. */
  3073. S2N_API int s2n_offered_early_data_get_context(struct s2n_offered_early_data *early_data, uint8_t *context, uint16_t max_len);
  3074. /**
  3075. * Reject early data offered by the client.
  3076. *
  3077. * @param early_data A pointer to the early data information
  3078. * @returns A POSIX error signal. If success, the client's early data will be rejected.
  3079. */
  3080. S2N_API int s2n_offered_early_data_reject(struct s2n_offered_early_data *early_data);
  3081. /**
  3082. * Accept early data offered by the client.
  3083. *
  3084. * @param early_data A pointer to the early data information
  3085. * @returns A POSIX error signal. If success, the client's early data will be accepted.
  3086. */
  3087. S2N_API int s2n_offered_early_data_accept(struct s2n_offered_early_data *early_data);
  3088. #ifdef __cplusplus
  3089. }
  3090. #endif