ssl.h 150 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350
  1. /**
  2. * \file ssl.h
  3. *
  4. * \brief SSL/TLS functions.
  5. */
  6. /*
  7. * Copyright The Mbed TLS Contributors
  8. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  9. *
  10. * This file is provided under the Apache License 2.0, or the
  11. * GNU General Public License v2.0 or later.
  12. *
  13. * **********
  14. * Apache License 2.0:
  15. *
  16. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  17. * not use this file except in compliance with the License.
  18. * You may obtain a copy of the License at
  19. *
  20. * http://www.apache.org/licenses/LICENSE-2.0
  21. *
  22. * Unless required by applicable law or agreed to in writing, software
  23. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  24. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  25. * See the License for the specific language governing permissions and
  26. * limitations under the License.
  27. *
  28. * **********
  29. *
  30. * **********
  31. * GNU General Public License v2.0 or later:
  32. *
  33. * This program is free software; you can redistribute it and/or modify
  34. * it under the terms of the GNU General Public License as published by
  35. * the Free Software Foundation; either version 2 of the License, or
  36. * (at your option) any later version.
  37. *
  38. * This program is distributed in the hope that it will be useful,
  39. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  40. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  41. * GNU General Public License for more details.
  42. *
  43. * You should have received a copy of the GNU General Public License along
  44. * with this program; if not, write to the Free Software Foundation, Inc.,
  45. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  46. *
  47. * **********
  48. */
  49. #ifndef MBEDTLS_SSL_H
  50. #define MBEDTLS_SSL_H
  51. #if !defined(MBEDTLS_CONFIG_FILE)
  52. #include "config.h"
  53. #else
  54. #include MBEDTLS_CONFIG_FILE
  55. #endif
  56. #include "bignum.h"
  57. #include "ecp.h"
  58. #include "ssl_ciphersuites.h"
  59. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  60. #include "x509_crt.h"
  61. #include "x509_crl.h"
  62. #endif
  63. #if defined(MBEDTLS_DHM_C)
  64. #include "dhm.h"
  65. #endif
  66. #if defined(MBEDTLS_ECDH_C)
  67. #include "ecdh.h"
  68. #endif
  69. #if defined(MBEDTLS_ZLIB_SUPPORT)
  70. #if defined(MBEDTLS_DEPRECATED_WARNING)
  71. #warning "Record compression support via MBEDTLS_ZLIB_SUPPORT is deprecated and will be removed in the next major revision of the library"
  72. #endif
  73. #if defined(MBEDTLS_DEPRECATED_REMOVED)
  74. #error "Record compression support via MBEDTLS_ZLIB_SUPPORT is deprecated and cannot be used if MBEDTLS_DEPRECATED_REMOVED is set"
  75. #endif
  76. #include "zlib.h"
  77. #endif
  78. #if defined(MBEDTLS_HAVE_TIME)
  79. #include "platform_time.h"
  80. #endif
  81. /*
  82. * SSL Error codes
  83. */
  84. /** The requested feature is not available. */
  85. #define MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE -0x7080
  86. /** Bad input parameters to function. */
  87. #define MBEDTLS_ERR_SSL_BAD_INPUT_DATA -0x7100
  88. /** Verification of the message MAC failed. */
  89. #define MBEDTLS_ERR_SSL_INVALID_MAC -0x7180
  90. /** An invalid SSL record was received. */
  91. #define MBEDTLS_ERR_SSL_INVALID_RECORD -0x7200
  92. /** The connection indicated an EOF. */
  93. #define MBEDTLS_ERR_SSL_CONN_EOF -0x7280
  94. /** An unknown cipher was received. */
  95. #define MBEDTLS_ERR_SSL_UNKNOWN_CIPHER -0x7300
  96. /** The server has no ciphersuites in common with the client. */
  97. #define MBEDTLS_ERR_SSL_NO_CIPHER_CHOSEN -0x7380
  98. /** No RNG was provided to the SSL module. */
  99. #define MBEDTLS_ERR_SSL_NO_RNG -0x7400
  100. /** No client certification received from the client, but required by the authentication mode. */
  101. #define MBEDTLS_ERR_SSL_NO_CLIENT_CERTIFICATE -0x7480
  102. /** Our own certificate(s) is/are too large to send in an SSL message. */
  103. #define MBEDTLS_ERR_SSL_CERTIFICATE_TOO_LARGE -0x7500
  104. /** The own certificate is not set, but needed by the server. */
  105. #define MBEDTLS_ERR_SSL_CERTIFICATE_REQUIRED -0x7580
  106. /** The own private key or pre-shared key is not set, but needed. */
  107. #define MBEDTLS_ERR_SSL_PRIVATE_KEY_REQUIRED -0x7600
  108. /** No CA Chain is set, but required to operate. */
  109. #define MBEDTLS_ERR_SSL_CA_CHAIN_REQUIRED -0x7680
  110. /** An unexpected message was received from our peer. */
  111. #define MBEDTLS_ERR_SSL_UNEXPECTED_MESSAGE -0x7700
  112. /** A fatal alert message was received from our peer. */
  113. #define MBEDTLS_ERR_SSL_FATAL_ALERT_MESSAGE -0x7780
  114. /** Verification of our peer failed. */
  115. #define MBEDTLS_ERR_SSL_PEER_VERIFY_FAILED -0x7800
  116. /** The peer notified us that the connection is going to be closed. */
  117. #define MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY -0x7880
  118. /** Processing of the ClientHello handshake message failed. */
  119. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_HELLO -0x7900
  120. /** Processing of the ServerHello handshake message failed. */
  121. #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO -0x7980
  122. /** Processing of the Certificate handshake message failed. */
  123. #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE -0x7A00
  124. /** Processing of the CertificateRequest handshake message failed. */
  125. #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_REQUEST -0x7A80
  126. /** Processing of the ServerKeyExchange handshake message failed. */
  127. #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_KEY_EXCHANGE -0x7B00
  128. /** Processing of the ServerHelloDone handshake message failed. */
  129. #define MBEDTLS_ERR_SSL_BAD_HS_SERVER_HELLO_DONE -0x7B80
  130. /** Processing of the ClientKeyExchange handshake message failed. */
  131. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE -0x7C00
  132. /** Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Read Public. */
  133. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_RP -0x7C80
  134. /** Processing of the ClientKeyExchange handshake message failed in DHM / ECDH Calculate Secret. */
  135. #define MBEDTLS_ERR_SSL_BAD_HS_CLIENT_KEY_EXCHANGE_CS -0x7D00
  136. /** Processing of the CertificateVerify handshake message failed. */
  137. #define MBEDTLS_ERR_SSL_BAD_HS_CERTIFICATE_VERIFY -0x7D80
  138. /** Processing of the ChangeCipherSpec handshake message failed. */
  139. #define MBEDTLS_ERR_SSL_BAD_HS_CHANGE_CIPHER_SPEC -0x7E00
  140. /** Processing of the Finished handshake message failed. */
  141. #define MBEDTLS_ERR_SSL_BAD_HS_FINISHED -0x7E80
  142. /** Memory allocation failed */
  143. #define MBEDTLS_ERR_SSL_ALLOC_FAILED -0x7F00
  144. /** Hardware acceleration function returned with error */
  145. #define MBEDTLS_ERR_SSL_HW_ACCEL_FAILED -0x7F80
  146. /** Hardware acceleration function skipped / left alone data */
  147. #define MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH -0x6F80
  148. /** Processing of the compression / decompression failed */
  149. #define MBEDTLS_ERR_SSL_COMPRESSION_FAILED -0x6F00
  150. /** Handshake protocol not within min/max boundaries */
  151. #define MBEDTLS_ERR_SSL_BAD_HS_PROTOCOL_VERSION -0x6E80
  152. /** Processing of the NewSessionTicket handshake message failed. */
  153. #define MBEDTLS_ERR_SSL_BAD_HS_NEW_SESSION_TICKET -0x6E00
  154. /** Session ticket has expired. */
  155. #define MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED -0x6D80
  156. /** Public key type mismatch (eg, asked for RSA key exchange and presented EC key) */
  157. #define MBEDTLS_ERR_SSL_PK_TYPE_MISMATCH -0x6D00
  158. /** Unknown identity received (eg, PSK identity) */
  159. #define MBEDTLS_ERR_SSL_UNKNOWN_IDENTITY -0x6C80
  160. /** Internal error (eg, unexpected failure in lower-level module) */
  161. #define MBEDTLS_ERR_SSL_INTERNAL_ERROR -0x6C00
  162. /** A counter would wrap (eg, too many messages exchanged). */
  163. #define MBEDTLS_ERR_SSL_COUNTER_WRAPPING -0x6B80
  164. /** Unexpected message at ServerHello in renegotiation. */
  165. #define MBEDTLS_ERR_SSL_WAITING_SERVER_HELLO_RENEGO -0x6B00
  166. /** DTLS client must retry for hello verification */
  167. #define MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED -0x6A80
  168. /** A buffer is too small to receive or write a message */
  169. #define MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL -0x6A00
  170. /** None of the common ciphersuites is usable (eg, no suitable certificate, see debug messages). */
  171. #define MBEDTLS_ERR_SSL_NO_USABLE_CIPHERSUITE -0x6980
  172. /** No data of requested type currently available on underlying transport. */
  173. #define MBEDTLS_ERR_SSL_WANT_READ -0x6900
  174. /** Connection requires a write call. */
  175. #define MBEDTLS_ERR_SSL_WANT_WRITE -0x6880
  176. /** The operation timed out. */
  177. #define MBEDTLS_ERR_SSL_TIMEOUT -0x6800
  178. /** The client initiated a reconnect from the same port. */
  179. #define MBEDTLS_ERR_SSL_CLIENT_RECONNECT -0x6780
  180. /** Record header looks valid but is not expected. */
  181. #define MBEDTLS_ERR_SSL_UNEXPECTED_RECORD -0x6700
  182. /** The alert message received indicates a non-fatal error. */
  183. #define MBEDTLS_ERR_SSL_NON_FATAL -0x6680
  184. /** Couldn't set the hash for verifying CertificateVerify */
  185. #define MBEDTLS_ERR_SSL_INVALID_VERIFY_HASH -0x6600
  186. /** Internal-only message signaling that further message-processing should be done */
  187. #define MBEDTLS_ERR_SSL_CONTINUE_PROCESSING -0x6580
  188. /** The asynchronous operation is not completed yet. */
  189. #define MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS -0x6500
  190. /** Internal-only message signaling that a message arrived early. */
  191. #define MBEDTLS_ERR_SSL_EARLY_MESSAGE -0x6480
  192. /** A cryptographic operation is in progress. Try again later. */
  193. #define MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS -0x7000
  194. /** Invalid value in SSL config */
  195. #define MBEDTLS_ERR_SSL_BAD_CONFIG -0x5E80
  196. /*
  197. * Various constants
  198. */
  199. #define MBEDTLS_SSL_MAJOR_VERSION_3 3
  200. #define MBEDTLS_SSL_MINOR_VERSION_0 0 /*!< SSL v3.0 */
  201. #define MBEDTLS_SSL_MINOR_VERSION_1 1 /*!< TLS v1.0 */
  202. #define MBEDTLS_SSL_MINOR_VERSION_2 2 /*!< TLS v1.1 */
  203. #define MBEDTLS_SSL_MINOR_VERSION_3 3 /*!< TLS v1.2 */
  204. #define MBEDTLS_SSL_TRANSPORT_STREAM 0 /*!< TLS */
  205. #define MBEDTLS_SSL_TRANSPORT_DATAGRAM 1 /*!< DTLS */
  206. #define MBEDTLS_SSL_MAX_HOST_NAME_LEN 255 /*!< Maximum host name defined in RFC 1035 */
  207. #define MBEDTLS_SSL_MAX_ALPN_NAME_LEN 255 /*!< Maximum size in bytes of a protocol name in alpn ext., RFC 7301 */
  208. #define MBEDTLS_SSL_MAX_ALPN_LIST_LEN 65535 /*!< Maximum size in bytes of list in alpn ext., RFC 7301 */
  209. /* RFC 6066 section 4, see also mfl_code_to_length in ssl_tls.c
  210. * NONE must be zero so that memset()ing structure to zero works */
  211. #define MBEDTLS_SSL_MAX_FRAG_LEN_NONE 0 /*!< don't use this extension */
  212. #define MBEDTLS_SSL_MAX_FRAG_LEN_512 1 /*!< MaxFragmentLength 2^9 */
  213. #define MBEDTLS_SSL_MAX_FRAG_LEN_1024 2 /*!< MaxFragmentLength 2^10 */
  214. #define MBEDTLS_SSL_MAX_FRAG_LEN_2048 3 /*!< MaxFragmentLength 2^11 */
  215. #define MBEDTLS_SSL_MAX_FRAG_LEN_4096 4 /*!< MaxFragmentLength 2^12 */
  216. #define MBEDTLS_SSL_MAX_FRAG_LEN_INVALID 5 /*!< first invalid value */
  217. #define MBEDTLS_SSL_IS_CLIENT 0
  218. #define MBEDTLS_SSL_IS_SERVER 1
  219. #define MBEDTLS_SSL_IS_NOT_FALLBACK 0
  220. #define MBEDTLS_SSL_IS_FALLBACK 1
  221. #define MBEDTLS_SSL_EXTENDED_MS_DISABLED 0
  222. #define MBEDTLS_SSL_EXTENDED_MS_ENABLED 1
  223. #define MBEDTLS_SSL_ETM_DISABLED 0
  224. #define MBEDTLS_SSL_ETM_ENABLED 1
  225. #define MBEDTLS_SSL_COMPRESS_NULL 0
  226. #define MBEDTLS_SSL_COMPRESS_DEFLATE 1
  227. #define MBEDTLS_SSL_VERIFY_NONE 0
  228. #define MBEDTLS_SSL_VERIFY_OPTIONAL 1
  229. #define MBEDTLS_SSL_VERIFY_REQUIRED 2
  230. #define MBEDTLS_SSL_VERIFY_UNSET 3 /* Used only for sni_authmode */
  231. #define MBEDTLS_SSL_LEGACY_RENEGOTIATION 0
  232. #define MBEDTLS_SSL_SECURE_RENEGOTIATION 1
  233. #define MBEDTLS_SSL_RENEGOTIATION_DISABLED 0
  234. #define MBEDTLS_SSL_RENEGOTIATION_ENABLED 1
  235. #define MBEDTLS_SSL_ANTI_REPLAY_DISABLED 0
  236. #define MBEDTLS_SSL_ANTI_REPLAY_ENABLED 1
  237. #define MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED -1
  238. #define MBEDTLS_SSL_RENEGO_MAX_RECORDS_DEFAULT 16
  239. #define MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION 0
  240. #define MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION 1
  241. #define MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE 2
  242. #define MBEDTLS_SSL_TRUNC_HMAC_DISABLED 0
  243. #define MBEDTLS_SSL_TRUNC_HMAC_ENABLED 1
  244. #define MBEDTLS_SSL_TRUNCATED_HMAC_LEN 10 /* 80 bits, rfc 6066 section 7 */
  245. #define MBEDTLS_SSL_SESSION_TICKETS_DISABLED 0
  246. #define MBEDTLS_SSL_SESSION_TICKETS_ENABLED 1
  247. #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED 0
  248. #define MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED 1
  249. #define MBEDTLS_SSL_ARC4_ENABLED 0
  250. #define MBEDTLS_SSL_ARC4_DISABLED 1
  251. #define MBEDTLS_SSL_PRESET_DEFAULT 0
  252. #define MBEDTLS_SSL_PRESET_SUITEB 2
  253. #define MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED 1
  254. #define MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED 0
  255. /*
  256. * Default range for DTLS retransmission timer value, in milliseconds.
  257. * RFC 6347 4.2.4.1 says from 1 second to 60 seconds.
  258. */
  259. #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MIN 1000
  260. #define MBEDTLS_SSL_DTLS_TIMEOUT_DFL_MAX 60000
  261. /**
  262. * \name SECTION: Module settings
  263. *
  264. * The configuration options you can set for this module are in this section.
  265. * Either change them in config.h or define them on the compiler command line.
  266. * \{
  267. */
  268. #if !defined(MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME)
  269. #define MBEDTLS_SSL_DEFAULT_TICKET_LIFETIME 86400 /**< Lifetime of session tickets (if enabled) */
  270. #endif
  271. /*
  272. * Maximum fragment length in bytes,
  273. * determines the size of each of the two internal I/O buffers.
  274. *
  275. * Note: the RFC defines the default size of SSL / TLS messages. If you
  276. * change the value here, other clients / servers may not be able to
  277. * communicate with you anymore. Only change this value if you control
  278. * both sides of the connection and have it reduced at both sides, or
  279. * if you're using the Max Fragment Length extension and you know all your
  280. * peers are using it too!
  281. */
  282. #if !defined(MBEDTLS_SSL_MAX_CONTENT_LEN)
  283. #define MBEDTLS_SSL_MAX_CONTENT_LEN 16384 /**< Size of the input / output buffer */
  284. #endif
  285. #if !defined(MBEDTLS_SSL_IN_CONTENT_LEN)
  286. #define MBEDTLS_SSL_IN_CONTENT_LEN MBEDTLS_SSL_MAX_CONTENT_LEN
  287. #endif
  288. #if !defined(MBEDTLS_SSL_OUT_CONTENT_LEN)
  289. #define MBEDTLS_SSL_OUT_CONTENT_LEN MBEDTLS_SSL_MAX_CONTENT_LEN
  290. #endif
  291. /*
  292. * Maximum number of heap-allocated bytes for the purpose of
  293. * DTLS handshake message reassembly and future message buffering.
  294. */
  295. #if !defined(MBEDTLS_SSL_DTLS_MAX_BUFFERING)
  296. #define MBEDTLS_SSL_DTLS_MAX_BUFFERING 32768
  297. #endif
  298. /* \} name SECTION: Module settings */
  299. /*
  300. * Length of the verify data for secure renegotiation
  301. */
  302. #if defined(MBEDTLS_SSL_PROTO_SSL3)
  303. #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 36
  304. #else
  305. #define MBEDTLS_SSL_VERIFY_DATA_MAX_LEN 12
  306. #endif
  307. /*
  308. * Signaling ciphersuite values (SCSV)
  309. */
  310. #define MBEDTLS_SSL_EMPTY_RENEGOTIATION_INFO 0xFF /**< renegotiation info ext */
  311. #define MBEDTLS_SSL_FALLBACK_SCSV_VALUE 0x5600 /**< RFC 7507 section 2 */
  312. /*
  313. * Supported Signature and Hash algorithms (For TLS 1.2)
  314. * RFC 5246 section 7.4.1.4.1
  315. */
  316. #define MBEDTLS_SSL_HASH_NONE 0
  317. #define MBEDTLS_SSL_HASH_MD5 1
  318. #define MBEDTLS_SSL_HASH_SHA1 2
  319. #define MBEDTLS_SSL_HASH_SHA224 3
  320. #define MBEDTLS_SSL_HASH_SHA256 4
  321. #define MBEDTLS_SSL_HASH_SHA384 5
  322. #define MBEDTLS_SSL_HASH_SHA512 6
  323. #define MBEDTLS_SSL_SIG_ANON 0
  324. #define MBEDTLS_SSL_SIG_RSA 1
  325. #define MBEDTLS_SSL_SIG_ECDSA 3
  326. /*
  327. * Client Certificate Types
  328. * RFC 5246 section 7.4.4 plus RFC 4492 section 5.5
  329. */
  330. #define MBEDTLS_SSL_CERT_TYPE_RSA_SIGN 1
  331. #define MBEDTLS_SSL_CERT_TYPE_ECDSA_SIGN 64
  332. /*
  333. * Message, alert and handshake types
  334. */
  335. #define MBEDTLS_SSL_MSG_CHANGE_CIPHER_SPEC 20
  336. #define MBEDTLS_SSL_MSG_ALERT 21
  337. #define MBEDTLS_SSL_MSG_HANDSHAKE 22
  338. #define MBEDTLS_SSL_MSG_APPLICATION_DATA 23
  339. #define MBEDTLS_SSL_ALERT_LEVEL_WARNING 1
  340. #define MBEDTLS_SSL_ALERT_LEVEL_FATAL 2
  341. #define MBEDTLS_SSL_ALERT_MSG_CLOSE_NOTIFY 0 /* 0x00 */
  342. #define MBEDTLS_SSL_ALERT_MSG_UNEXPECTED_MESSAGE 10 /* 0x0A */
  343. #define MBEDTLS_SSL_ALERT_MSG_BAD_RECORD_MAC 20 /* 0x14 */
  344. #define MBEDTLS_SSL_ALERT_MSG_DECRYPTION_FAILED 21 /* 0x15 */
  345. #define MBEDTLS_SSL_ALERT_MSG_RECORD_OVERFLOW 22 /* 0x16 */
  346. #define MBEDTLS_SSL_ALERT_MSG_DECOMPRESSION_FAILURE 30 /* 0x1E */
  347. #define MBEDTLS_SSL_ALERT_MSG_HANDSHAKE_FAILURE 40 /* 0x28 */
  348. #define MBEDTLS_SSL_ALERT_MSG_NO_CERT 41 /* 0x29 */
  349. #define MBEDTLS_SSL_ALERT_MSG_BAD_CERT 42 /* 0x2A */
  350. #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_CERT 43 /* 0x2B */
  351. #define MBEDTLS_SSL_ALERT_MSG_CERT_REVOKED 44 /* 0x2C */
  352. #define MBEDTLS_SSL_ALERT_MSG_CERT_EXPIRED 45 /* 0x2D */
  353. #define MBEDTLS_SSL_ALERT_MSG_CERT_UNKNOWN 46 /* 0x2E */
  354. #define MBEDTLS_SSL_ALERT_MSG_ILLEGAL_PARAMETER 47 /* 0x2F */
  355. #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_CA 48 /* 0x30 */
  356. #define MBEDTLS_SSL_ALERT_MSG_ACCESS_DENIED 49 /* 0x31 */
  357. #define MBEDTLS_SSL_ALERT_MSG_DECODE_ERROR 50 /* 0x32 */
  358. #define MBEDTLS_SSL_ALERT_MSG_DECRYPT_ERROR 51 /* 0x33 */
  359. #define MBEDTLS_SSL_ALERT_MSG_EXPORT_RESTRICTION 60 /* 0x3C */
  360. #define MBEDTLS_SSL_ALERT_MSG_PROTOCOL_VERSION 70 /* 0x46 */
  361. #define MBEDTLS_SSL_ALERT_MSG_INSUFFICIENT_SECURITY 71 /* 0x47 */
  362. #define MBEDTLS_SSL_ALERT_MSG_INTERNAL_ERROR 80 /* 0x50 */
  363. #define MBEDTLS_SSL_ALERT_MSG_INAPROPRIATE_FALLBACK 86 /* 0x56 */
  364. #define MBEDTLS_SSL_ALERT_MSG_USER_CANCELED 90 /* 0x5A */
  365. #define MBEDTLS_SSL_ALERT_MSG_NO_RENEGOTIATION 100 /* 0x64 */
  366. #define MBEDTLS_SSL_ALERT_MSG_UNSUPPORTED_EXT 110 /* 0x6E */
  367. #define MBEDTLS_SSL_ALERT_MSG_UNRECOGNIZED_NAME 112 /* 0x70 */
  368. #define MBEDTLS_SSL_ALERT_MSG_UNKNOWN_PSK_IDENTITY 115 /* 0x73 */
  369. #define MBEDTLS_SSL_ALERT_MSG_NO_APPLICATION_PROTOCOL 120 /* 0x78 */
  370. #define MBEDTLS_SSL_HS_HELLO_REQUEST 0
  371. #define MBEDTLS_SSL_HS_CLIENT_HELLO 1
  372. #define MBEDTLS_SSL_HS_SERVER_HELLO 2
  373. #define MBEDTLS_SSL_HS_HELLO_VERIFY_REQUEST 3
  374. #define MBEDTLS_SSL_HS_NEW_SESSION_TICKET 4
  375. #define MBEDTLS_SSL_HS_CERTIFICATE 11
  376. #define MBEDTLS_SSL_HS_SERVER_KEY_EXCHANGE 12
  377. #define MBEDTLS_SSL_HS_CERTIFICATE_REQUEST 13
  378. #define MBEDTLS_SSL_HS_SERVER_HELLO_DONE 14
  379. #define MBEDTLS_SSL_HS_CERTIFICATE_VERIFY 15
  380. #define MBEDTLS_SSL_HS_CLIENT_KEY_EXCHANGE 16
  381. #define MBEDTLS_SSL_HS_FINISHED 20
  382. /*
  383. * TLS extensions
  384. */
  385. #define MBEDTLS_TLS_EXT_SERVERNAME 0
  386. #define MBEDTLS_TLS_EXT_SERVERNAME_HOSTNAME 0
  387. #define MBEDTLS_TLS_EXT_MAX_FRAGMENT_LENGTH 1
  388. #define MBEDTLS_TLS_EXT_TRUNCATED_HMAC 4
  389. #define MBEDTLS_TLS_EXT_SUPPORTED_ELLIPTIC_CURVES 10
  390. #define MBEDTLS_TLS_EXT_SUPPORTED_POINT_FORMATS 11
  391. #define MBEDTLS_TLS_EXT_SIG_ALG 13
  392. #define MBEDTLS_TLS_EXT_ALPN 16
  393. #define MBEDTLS_TLS_EXT_ENCRYPT_THEN_MAC 22 /* 0x16 */
  394. #define MBEDTLS_TLS_EXT_EXTENDED_MASTER_SECRET 0x0017 /* 23 */
  395. #define MBEDTLS_TLS_EXT_SESSION_TICKET 35
  396. #define MBEDTLS_TLS_EXT_ECJPAKE_KKPP 256 /* experimental */
  397. #define MBEDTLS_TLS_EXT_RENEGOTIATION_INFO 0xFF01
  398. /*
  399. * Size defines
  400. */
  401. #if !defined(MBEDTLS_PSK_MAX_LEN)
  402. #define MBEDTLS_PSK_MAX_LEN 32 /* 256 bits */
  403. #endif
  404. /* Dummy type used only for its size */
  405. union mbedtls_ssl_premaster_secret
  406. {
  407. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_ENABLED)
  408. unsigned char _pms_rsa[48]; /* RFC 5246 8.1.1 */
  409. #endif
  410. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_RSA_ENABLED)
  411. unsigned char _pms_dhm[MBEDTLS_MPI_MAX_SIZE]; /* RFC 5246 8.1.2 */
  412. #endif
  413. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_RSA_ENABLED) || \
  414. defined(MBEDTLS_KEY_EXCHANGE_ECDHE_ECDSA_ENABLED) || \
  415. defined(MBEDTLS_KEY_EXCHANGE_ECDH_RSA_ENABLED) || \
  416. defined(MBEDTLS_KEY_EXCHANGE_ECDH_ECDSA_ENABLED)
  417. unsigned char _pms_ecdh[MBEDTLS_ECP_MAX_BYTES]; /* RFC 4492 5.10 */
  418. #endif
  419. #if defined(MBEDTLS_KEY_EXCHANGE_PSK_ENABLED)
  420. unsigned char _pms_psk[4 + 2 * MBEDTLS_PSK_MAX_LEN]; /* RFC 4279 2 */
  421. #endif
  422. #if defined(MBEDTLS_KEY_EXCHANGE_DHE_PSK_ENABLED)
  423. unsigned char _pms_dhe_psk[4 + MBEDTLS_MPI_MAX_SIZE
  424. + MBEDTLS_PSK_MAX_LEN]; /* RFC 4279 3 */
  425. #endif
  426. #if defined(MBEDTLS_KEY_EXCHANGE_RSA_PSK_ENABLED)
  427. unsigned char _pms_rsa_psk[52 + MBEDTLS_PSK_MAX_LEN]; /* RFC 4279 4 */
  428. #endif
  429. #if defined(MBEDTLS_KEY_EXCHANGE_ECDHE_PSK_ENABLED)
  430. unsigned char _pms_ecdhe_psk[4 + MBEDTLS_ECP_MAX_BYTES
  431. + MBEDTLS_PSK_MAX_LEN]; /* RFC 5489 2 */
  432. #endif
  433. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  434. unsigned char _pms_ecjpake[32]; /* Thread spec: SHA-256 output */
  435. #endif
  436. };
  437. #define MBEDTLS_PREMASTER_SIZE sizeof( union mbedtls_ssl_premaster_secret )
  438. #ifdef __cplusplus
  439. extern "C" {
  440. #endif
  441. /*
  442. * SSL state machine
  443. */
  444. typedef enum
  445. {
  446. MBEDTLS_SSL_HELLO_REQUEST,
  447. MBEDTLS_SSL_CLIENT_HELLO,
  448. MBEDTLS_SSL_SERVER_HELLO,
  449. MBEDTLS_SSL_SERVER_CERTIFICATE,
  450. MBEDTLS_SSL_SERVER_KEY_EXCHANGE,
  451. MBEDTLS_SSL_CERTIFICATE_REQUEST,
  452. MBEDTLS_SSL_SERVER_HELLO_DONE,
  453. MBEDTLS_SSL_CLIENT_CERTIFICATE,
  454. MBEDTLS_SSL_CLIENT_KEY_EXCHANGE,
  455. MBEDTLS_SSL_CERTIFICATE_VERIFY,
  456. MBEDTLS_SSL_CLIENT_CHANGE_CIPHER_SPEC,
  457. MBEDTLS_SSL_CLIENT_FINISHED,
  458. MBEDTLS_SSL_SERVER_CHANGE_CIPHER_SPEC,
  459. MBEDTLS_SSL_SERVER_FINISHED,
  460. MBEDTLS_SSL_FLUSH_BUFFERS,
  461. MBEDTLS_SSL_HANDSHAKE_WRAPUP,
  462. MBEDTLS_SSL_HANDSHAKE_OVER,
  463. MBEDTLS_SSL_SERVER_NEW_SESSION_TICKET,
  464. MBEDTLS_SSL_SERVER_HELLO_VERIFY_REQUEST_SENT,
  465. }
  466. mbedtls_ssl_states;
  467. /**
  468. * \brief Callback type: send data on the network.
  469. *
  470. * \note That callback may be either blocking or non-blocking.
  471. *
  472. * \param ctx Context for the send callback (typically a file descriptor)
  473. * \param buf Buffer holding the data to send
  474. * \param len Length of the data to send
  475. *
  476. * \return The callback must return the number of bytes sent if any,
  477. * or a non-zero error code.
  478. * If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_WRITE
  479. * must be returned when the operation would block.
  480. *
  481. * \note The callback is allowed to send fewer bytes than requested.
  482. * It must always return the number of bytes actually sent.
  483. */
  484. typedef int mbedtls_ssl_send_t( void *ctx,
  485. const unsigned char *buf,
  486. size_t len );
  487. /**
  488. * \brief Callback type: receive data from the network.
  489. *
  490. * \note That callback may be either blocking or non-blocking.
  491. *
  492. * \param ctx Context for the receive callback (typically a file
  493. * descriptor)
  494. * \param buf Buffer to write the received data to
  495. * \param len Length of the receive buffer
  496. *
  497. * \returns If data has been received, the positive number of bytes received.
  498. * \returns \c 0 if the connection has been closed.
  499. * \returns If performing non-blocking I/O, \c MBEDTLS_ERR_SSL_WANT_READ
  500. * must be returned when the operation would block.
  501. * \returns Another negative error code on other kinds of failures.
  502. *
  503. * \note The callback may receive fewer bytes than the length of the
  504. * buffer. It must always return the number of bytes actually
  505. * received and written to the buffer.
  506. */
  507. typedef int mbedtls_ssl_recv_t( void *ctx,
  508. unsigned char *buf,
  509. size_t len );
  510. /**
  511. * \brief Callback type: receive data from the network, with timeout
  512. *
  513. * \note That callback must block until data is received, or the
  514. * timeout delay expires, or the operation is interrupted by a
  515. * signal.
  516. *
  517. * \param ctx Context for the receive callback (typically a file descriptor)
  518. * \param buf Buffer to write the received data to
  519. * \param len Length of the receive buffer
  520. * \param timeout Maximum nomber of millisecondes to wait for data
  521. * 0 means no timeout (potentially waiting forever)
  522. *
  523. * \return The callback must return the number of bytes received,
  524. * or a non-zero error code:
  525. * \c MBEDTLS_ERR_SSL_TIMEOUT if the operation timed out,
  526. * \c MBEDTLS_ERR_SSL_WANT_READ if interrupted by a signal.
  527. *
  528. * \note The callback may receive fewer bytes than the length of the
  529. * buffer. It must always return the number of bytes actually
  530. * received and written to the buffer.
  531. */
  532. typedef int mbedtls_ssl_recv_timeout_t( void *ctx,
  533. unsigned char *buf,
  534. size_t len,
  535. uint32_t timeout );
  536. /**
  537. * \brief Callback type: set a pair of timers/delays to watch
  538. *
  539. * \param ctx Context pointer
  540. * \param int_ms Intermediate delay in milliseconds
  541. * \param fin_ms Final delay in milliseconds
  542. * 0 cancels the current timer.
  543. *
  544. * \note This callback must at least store the necessary information
  545. * for the associated \c mbedtls_ssl_get_timer_t callback to
  546. * return correct information.
  547. *
  548. * \note If using a event-driven style of programming, an event must
  549. * be generated when the final delay is passed. The event must
  550. * cause a call to \c mbedtls_ssl_handshake() with the proper
  551. * SSL context to be scheduled. Care must be taken to ensure
  552. * that at most one such call happens at a time.
  553. *
  554. * \note Only one timer at a time must be running. Calling this
  555. * function while a timer is running must cancel it. Cancelled
  556. * timers must not generate any event.
  557. */
  558. typedef void mbedtls_ssl_set_timer_t( void * ctx,
  559. uint32_t int_ms,
  560. uint32_t fin_ms );
  561. /**
  562. * \brief Callback type: get status of timers/delays
  563. *
  564. * \param ctx Context pointer
  565. *
  566. * \return This callback must return:
  567. * -1 if cancelled (fin_ms == 0),
  568. * 0 if none of the delays have passed,
  569. * 1 if only the intermediate delay has passed,
  570. * 2 if the final delay has passed.
  571. */
  572. typedef int mbedtls_ssl_get_timer_t( void * ctx );
  573. /* Defined below */
  574. typedef struct mbedtls_ssl_session mbedtls_ssl_session;
  575. typedef struct mbedtls_ssl_context mbedtls_ssl_context;
  576. typedef struct mbedtls_ssl_config mbedtls_ssl_config;
  577. /* Defined in ssl_internal.h */
  578. typedef struct mbedtls_ssl_transform mbedtls_ssl_transform;
  579. typedef struct mbedtls_ssl_handshake_params mbedtls_ssl_handshake_params;
  580. typedef struct mbedtls_ssl_sig_hash_set_t mbedtls_ssl_sig_hash_set_t;
  581. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  582. typedef struct mbedtls_ssl_key_cert mbedtls_ssl_key_cert;
  583. #endif
  584. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  585. typedef struct mbedtls_ssl_flight_item mbedtls_ssl_flight_item;
  586. #endif
  587. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  588. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  589. /**
  590. * \brief Callback type: start external signature operation.
  591. *
  592. * This callback is called during an SSL handshake to start
  593. * a signature decryption operation using an
  594. * external processor. The parameter \p cert contains
  595. * the public key; it is up to the callback function to
  596. * determine how to access the associated private key.
  597. *
  598. * This function typically sends or enqueues a request, and
  599. * does not wait for the operation to complete. This allows
  600. * the handshake step to be non-blocking.
  601. *
  602. * The parameters \p ssl and \p cert are guaranteed to remain
  603. * valid throughout the handshake. On the other hand, this
  604. * function must save the contents of \p hash if the value
  605. * is needed for later processing, because the \p hash buffer
  606. * is no longer valid after this function returns.
  607. *
  608. * This function may call mbedtls_ssl_set_async_operation_data()
  609. * to store an operation context for later retrieval
  610. * by the resume or cancel callback.
  611. *
  612. * \note For RSA signatures, this function must produce output
  613. * that is consistent with PKCS#1 v1.5 in the same way as
  614. * mbedtls_rsa_pkcs1_sign(). Before the private key operation,
  615. * apply the padding steps described in RFC 8017, section 9.2
  616. * "EMSA-PKCS1-v1_5" as follows.
  617. * - If \p md_alg is #MBEDTLS_MD_NONE, apply the PKCS#1 v1.5
  618. * encoding, treating \p hash as the DigestInfo to be
  619. * padded. In other words, apply EMSA-PKCS1-v1_5 starting
  620. * from step 3, with `T = hash` and `tLen = hash_len`.
  621. * - If `md_alg != MBEDTLS_MD_NONE`, apply the PKCS#1 v1.5
  622. * encoding, treating \p hash as the hash to be encoded and
  623. * padded. In other words, apply EMSA-PKCS1-v1_5 starting
  624. * from step 2, with `digestAlgorithm` obtained by calling
  625. * mbedtls_oid_get_oid_by_md() on \p md_alg.
  626. *
  627. * \note For ECDSA signatures, the output format is the DER encoding
  628. * `Ecdsa-Sig-Value` defined in
  629. * [RFC 4492 section 5.4](https://tools.ietf.org/html/rfc4492#section-5.4).
  630. *
  631. * \param ssl The SSL connection instance. It should not be
  632. * modified other than via
  633. * mbedtls_ssl_set_async_operation_data().
  634. * \param cert Certificate containing the public key.
  635. * In simple cases, this is one of the pointers passed to
  636. * mbedtls_ssl_conf_own_cert() when configuring the SSL
  637. * connection. However, if other callbacks are used, this
  638. * property may not hold. For example, if an SNI callback
  639. * is registered with mbedtls_ssl_conf_sni(), then
  640. * this callback determines what certificate is used.
  641. * \param md_alg Hash algorithm.
  642. * \param hash Buffer containing the hash. This buffer is
  643. * no longer valid when the function returns.
  644. * \param hash_len Size of the \c hash buffer in bytes.
  645. *
  646. * \return 0 if the operation was started successfully and the SSL
  647. * stack should call the resume callback immediately.
  648. * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if the operation
  649. * was started successfully and the SSL stack should return
  650. * immediately without calling the resume callback yet.
  651. * \return #MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH if the external
  652. * processor does not support this key. The SSL stack will
  653. * use the private key object instead.
  654. * \return Any other error indicates a fatal failure and is
  655. * propagated up the call chain. The callback should
  656. * use \c MBEDTLS_ERR_PK_xxx error codes, and <b>must not</b>
  657. * use \c MBEDTLS_ERR_SSL_xxx error codes except as
  658. * directed in the documentation of this callback.
  659. */
  660. typedef int mbedtls_ssl_async_sign_t( mbedtls_ssl_context *ssl,
  661. mbedtls_x509_crt *cert,
  662. mbedtls_md_type_t md_alg,
  663. const unsigned char *hash,
  664. size_t hash_len );
  665. /**
  666. * \brief Callback type: start external decryption operation.
  667. *
  668. * This callback is called during an SSL handshake to start
  669. * an RSA decryption operation using an
  670. * external processor. The parameter \p cert contains
  671. * the public key; it is up to the callback function to
  672. * determine how to access the associated private key.
  673. *
  674. * This function typically sends or enqueues a request, and
  675. * does not wait for the operation to complete. This allows
  676. * the handshake step to be non-blocking.
  677. *
  678. * The parameters \p ssl and \p cert are guaranteed to remain
  679. * valid throughout the handshake. On the other hand, this
  680. * function must save the contents of \p input if the value
  681. * is needed for later processing, because the \p input buffer
  682. * is no longer valid after this function returns.
  683. *
  684. * This function may call mbedtls_ssl_set_async_operation_data()
  685. * to store an operation context for later retrieval
  686. * by the resume or cancel callback.
  687. *
  688. * \warning RSA decryption as used in TLS is subject to a potential
  689. * timing side channel attack first discovered by Bleichenbacher
  690. * in 1998. This attack can be remotely exploitable
  691. * in practice. To avoid this attack, you must ensure that
  692. * if the callback performs an RSA decryption, the time it
  693. * takes to execute and return the result does not depend
  694. * on whether the RSA decryption succeeded or reported
  695. * invalid padding.
  696. *
  697. * \param ssl The SSL connection instance. It should not be
  698. * modified other than via
  699. * mbedtls_ssl_set_async_operation_data().
  700. * \param cert Certificate containing the public key.
  701. * In simple cases, this is one of the pointers passed to
  702. * mbedtls_ssl_conf_own_cert() when configuring the SSL
  703. * connection. However, if other callbacks are used, this
  704. * property may not hold. For example, if an SNI callback
  705. * is registered with mbedtls_ssl_conf_sni(), then
  706. * this callback determines what certificate is used.
  707. * \param input Buffer containing the input ciphertext. This buffer
  708. * is no longer valid when the function returns.
  709. * \param input_len Size of the \p input buffer in bytes.
  710. *
  711. * \return 0 if the operation was started successfully and the SSL
  712. * stack should call the resume callback immediately.
  713. * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if the operation
  714. * was started successfully and the SSL stack should return
  715. * immediately without calling the resume callback yet.
  716. * \return #MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH if the external
  717. * processor does not support this key. The SSL stack will
  718. * use the private key object instead.
  719. * \return Any other error indicates a fatal failure and is
  720. * propagated up the call chain. The callback should
  721. * use \c MBEDTLS_ERR_PK_xxx error codes, and <b>must not</b>
  722. * use \c MBEDTLS_ERR_SSL_xxx error codes except as
  723. * directed in the documentation of this callback.
  724. */
  725. typedef int mbedtls_ssl_async_decrypt_t( mbedtls_ssl_context *ssl,
  726. mbedtls_x509_crt *cert,
  727. const unsigned char *input,
  728. size_t input_len );
  729. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  730. /**
  731. * \brief Callback type: resume external operation.
  732. *
  733. * This callback is called during an SSL handshake to resume
  734. * an external operation started by the
  735. * ::mbedtls_ssl_async_sign_t or
  736. * ::mbedtls_ssl_async_decrypt_t callback.
  737. *
  738. * This function typically checks the status of a pending
  739. * request or causes the request queue to make progress, and
  740. * does not wait for the operation to complete. This allows
  741. * the handshake step to be non-blocking.
  742. *
  743. * This function may call mbedtls_ssl_get_async_operation_data()
  744. * to retrieve an operation context set by the start callback.
  745. * It may call mbedtls_ssl_set_async_operation_data() to modify
  746. * this context.
  747. *
  748. * Note that when this function returns a status other than
  749. * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS, it must free any
  750. * resources associated with the operation.
  751. *
  752. * \param ssl The SSL connection instance. It should not be
  753. * modified other than via
  754. * mbedtls_ssl_set_async_operation_data().
  755. * \param output Buffer containing the output (signature or decrypted
  756. * data) on success.
  757. * \param output_len On success, number of bytes written to \p output.
  758. * \param output_size Size of the \p output buffer in bytes.
  759. *
  760. * \return 0 if output of the operation is available in the
  761. * \p output buffer.
  762. * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if the operation
  763. * is still in progress. Subsequent requests for progress
  764. * on the SSL connection will call the resume callback
  765. * again.
  766. * \return Any other error means that the operation is aborted.
  767. * The SSL handshake is aborted. The callback should
  768. * use \c MBEDTLS_ERR_PK_xxx error codes, and <b>must not</b>
  769. * use \c MBEDTLS_ERR_SSL_xxx error codes except as
  770. * directed in the documentation of this callback.
  771. */
  772. typedef int mbedtls_ssl_async_resume_t( mbedtls_ssl_context *ssl,
  773. unsigned char *output,
  774. size_t *output_len,
  775. size_t output_size );
  776. /**
  777. * \brief Callback type: cancel external operation.
  778. *
  779. * This callback is called if an SSL connection is closed
  780. * while an asynchronous operation is in progress. Note that
  781. * this callback is not called if the
  782. * ::mbedtls_ssl_async_resume_t callback has run and has
  783. * returned a value other than
  784. * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS, since in that case
  785. * the asynchronous operation has already completed.
  786. *
  787. * This function may call mbedtls_ssl_get_async_operation_data()
  788. * to retrieve an operation context set by the start callback.
  789. *
  790. * \param ssl The SSL connection instance. It should not be
  791. * modified.
  792. */
  793. typedef void mbedtls_ssl_async_cancel_t( mbedtls_ssl_context *ssl );
  794. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  795. /*
  796. * This structure is used for storing current session data.
  797. */
  798. struct mbedtls_ssl_session
  799. {
  800. #if defined(MBEDTLS_HAVE_TIME)
  801. mbedtls_time_t start; /*!< starting time */
  802. #endif
  803. int ciphersuite; /*!< chosen ciphersuite */
  804. int compression; /*!< chosen compression */
  805. size_t id_len; /*!< session id length */
  806. unsigned char id[32]; /*!< session identifier */
  807. unsigned char master[48]; /*!< the master secret */
  808. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  809. mbedtls_x509_crt *peer_cert; /*!< peer X.509 cert chain */
  810. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  811. uint32_t verify_result; /*!< verification result */
  812. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  813. unsigned char *ticket; /*!< RFC 5077 session ticket */
  814. size_t ticket_len; /*!< session ticket length */
  815. uint32_t ticket_lifetime; /*!< ticket lifetime hint */
  816. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  817. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  818. unsigned char mfl_code; /*!< MaxFragmentLength negotiated by peer */
  819. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  820. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  821. int trunc_hmac; /*!< flag for truncated hmac activation */
  822. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  823. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  824. int encrypt_then_mac; /*!< flag for EtM activation */
  825. #endif
  826. };
  827. /**
  828. * SSL/TLS configuration to be shared between mbedtls_ssl_context structures.
  829. */
  830. struct mbedtls_ssl_config
  831. {
  832. /* Group items by size (largest first) to minimize padding overhead */
  833. /*
  834. * Pointers
  835. */
  836. const int *ciphersuite_list[4]; /*!< allowed ciphersuites per version */
  837. /** Callback for printing debug output */
  838. void (*f_dbg)(void *, int, const char *, int, const char *);
  839. void *p_dbg; /*!< context for the debug function */
  840. /** Callback for getting (pseudo-)random numbers */
  841. int (*f_rng)(void *, unsigned char *, size_t);
  842. void *p_rng; /*!< context for the RNG function */
  843. /** Callback to retrieve a session from the cache */
  844. int (*f_get_cache)(void *, mbedtls_ssl_session *);
  845. /** Callback to store a session into the cache */
  846. int (*f_set_cache)(void *, const mbedtls_ssl_session *);
  847. void *p_cache; /*!< context for cache callbacks */
  848. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  849. /** Callback for setting cert according to SNI extension */
  850. int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *, size_t);
  851. void *p_sni; /*!< context for SNI callback */
  852. #endif
  853. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  854. /** Callback to customize X.509 certificate chain verification */
  855. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *);
  856. void *p_vrfy; /*!< context for X.509 verify calllback */
  857. #endif
  858. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  859. /** Callback to retrieve PSK key from identity */
  860. int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *, size_t);
  861. void *p_psk; /*!< context for PSK callback */
  862. #endif
  863. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  864. /** Callback to create & write a cookie for ClientHello veirifcation */
  865. int (*f_cookie_write)( void *, unsigned char **, unsigned char *,
  866. const unsigned char *, size_t );
  867. /** Callback to verify validity of a ClientHello cookie */
  868. int (*f_cookie_check)( void *, const unsigned char *, size_t,
  869. const unsigned char *, size_t );
  870. void *p_cookie; /*!< context for the cookie callbacks */
  871. #endif
  872. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
  873. /** Callback to create & write a session ticket */
  874. int (*f_ticket_write)( void *, const mbedtls_ssl_session *,
  875. unsigned char *, const unsigned char *, size_t *, uint32_t * );
  876. /** Callback to parse a session ticket into a session structure */
  877. int (*f_ticket_parse)( void *, mbedtls_ssl_session *, unsigned char *, size_t);
  878. void *p_ticket; /*!< context for the ticket callbacks */
  879. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
  880. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  881. /** Callback to export key block and master secret */
  882. int (*f_export_keys)( void *, const unsigned char *,
  883. const unsigned char *, size_t, size_t, size_t );
  884. void *p_export_keys; /*!< context for key export callback */
  885. #endif
  886. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  887. const mbedtls_x509_crt_profile *cert_profile; /*!< verification profile */
  888. mbedtls_ssl_key_cert *key_cert; /*!< own certificate/key pair(s) */
  889. mbedtls_x509_crt *ca_chain; /*!< trusted CAs */
  890. mbedtls_x509_crl *ca_crl; /*!< trusted CAs CRLs */
  891. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  892. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  893. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  894. mbedtls_ssl_async_sign_t *f_async_sign_start; /*!< start asynchronous signature operation */
  895. mbedtls_ssl_async_decrypt_t *f_async_decrypt_start; /*!< start asynchronous decryption operation */
  896. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  897. mbedtls_ssl_async_resume_t *f_async_resume; /*!< resume asynchronous operation */
  898. mbedtls_ssl_async_cancel_t *f_async_cancel; /*!< cancel asynchronous operation */
  899. void *p_async_config_data; /*!< Configuration data set by mbedtls_ssl_conf_async_private_cb(). */
  900. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  901. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  902. const int *sig_hashes; /*!< allowed signature hashes */
  903. #endif
  904. #if defined(MBEDTLS_ECP_C)
  905. const mbedtls_ecp_group_id *curve_list; /*!< allowed curves */
  906. #endif
  907. #if defined(MBEDTLS_DHM_C)
  908. mbedtls_mpi dhm_P; /*!< prime modulus for DHM */
  909. mbedtls_mpi dhm_G; /*!< generator for DHM */
  910. #endif
  911. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  912. unsigned char *psk; /*!< pre-shared key. This field should
  913. only be set via
  914. mbedtls_ssl_conf_psk() */
  915. size_t psk_len; /*!< length of the pre-shared key. This
  916. field should only be set via
  917. mbedtls_ssl_conf_psk() */
  918. unsigned char *psk_identity; /*!< identity for PSK negotiation. This
  919. field should only be set via
  920. mbedtls_ssl_conf_psk() */
  921. size_t psk_identity_len;/*!< length of identity. This field should
  922. only be set via
  923. mbedtls_ssl_conf_psk() */
  924. #endif
  925. #if defined(MBEDTLS_SSL_ALPN)
  926. const char **alpn_list; /*!< ordered list of protocols */
  927. #endif
  928. /*
  929. * Numerical settings (int then char)
  930. */
  931. uint32_t read_timeout; /*!< timeout for mbedtls_ssl_read (ms) */
  932. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  933. uint32_t hs_timeout_min; /*!< initial value of the handshake
  934. retransmission timeout (ms) */
  935. uint32_t hs_timeout_max; /*!< maximum value of the handshake
  936. retransmission timeout (ms) */
  937. #endif
  938. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  939. int renego_max_records; /*!< grace period for renegotiation */
  940. unsigned char renego_period[8]; /*!< value of the record counters
  941. that triggers renegotiation */
  942. #endif
  943. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  944. unsigned int badmac_limit; /*!< limit of records with a bad MAC */
  945. #endif
  946. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
  947. unsigned int dhm_min_bitlen; /*!< min. bit length of the DHM prime */
  948. #endif
  949. unsigned char max_major_ver; /*!< max. major version used */
  950. unsigned char max_minor_ver; /*!< max. minor version used */
  951. unsigned char min_major_ver; /*!< min. major version used */
  952. unsigned char min_minor_ver; /*!< min. minor version used */
  953. /*
  954. * Flags (bitfields)
  955. */
  956. unsigned int endpoint : 1; /*!< 0: client, 1: server */
  957. unsigned int transport : 1; /*!< stream (TLS) or datagram (DTLS) */
  958. unsigned int authmode : 2; /*!< MBEDTLS_SSL_VERIFY_XXX */
  959. /* needed even with renego disabled for LEGACY_BREAK_HANDSHAKE */
  960. unsigned int allow_legacy_renegotiation : 2 ; /*!< MBEDTLS_LEGACY_XXX */
  961. #if defined(MBEDTLS_ARC4_C)
  962. unsigned int arc4_disabled : 1; /*!< blacklist RC4 ciphersuites? */
  963. #endif
  964. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  965. unsigned int mfl_code : 3; /*!< desired fragment length */
  966. #endif
  967. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  968. unsigned int encrypt_then_mac : 1 ; /*!< negotiate encrypt-then-mac? */
  969. #endif
  970. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  971. unsigned int extended_ms : 1; /*!< negotiate extended master secret? */
  972. #endif
  973. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  974. unsigned int anti_replay : 1; /*!< detect and prevent replay? */
  975. #endif
  976. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  977. unsigned int cbc_record_splitting : 1; /*!< do cbc record splitting */
  978. #endif
  979. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  980. unsigned int disable_renegotiation : 1; /*!< disable renegotiation? */
  981. #endif
  982. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  983. unsigned int trunc_hmac : 1; /*!< negotiate truncated hmac? */
  984. #endif
  985. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  986. unsigned int session_tickets : 1; /*!< use session tickets? */
  987. #endif
  988. #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
  989. unsigned int fallback : 1; /*!< is this a fallback? */
  990. #endif
  991. #if defined(MBEDTLS_SSL_SRV_C)
  992. unsigned int cert_req_ca_list : 1; /*!< enable sending CA list in
  993. Certificate Request messages? */
  994. #endif
  995. };
  996. struct mbedtls_ssl_context
  997. {
  998. const mbedtls_ssl_config *conf; /*!< configuration information */
  999. /*
  1000. * Miscellaneous
  1001. */
  1002. int state; /*!< SSL handshake: current state */
  1003. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1004. int renego_status; /*!< Initial, in progress, pending? */
  1005. int renego_records_seen; /*!< Records since renego request, or with DTLS,
  1006. number of retransmissions of request if
  1007. renego_max_records is < 0 */
  1008. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1009. int major_ver; /*!< equal to MBEDTLS_SSL_MAJOR_VERSION_3 */
  1010. int minor_ver; /*!< either 0 (SSL3) or 1 (TLS1.0) */
  1011. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  1012. unsigned badmac_seen; /*!< records with a bad MAC received */
  1013. #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
  1014. mbedtls_ssl_send_t *f_send; /*!< Callback for network send */
  1015. mbedtls_ssl_recv_t *f_recv; /*!< Callback for network receive */
  1016. mbedtls_ssl_recv_timeout_t *f_recv_timeout;
  1017. /*!< Callback for network receive with timeout */
  1018. void *p_bio; /*!< context for I/O operations */
  1019. /*
  1020. * Session layer
  1021. */
  1022. mbedtls_ssl_session *session_in; /*!< current session data (in) */
  1023. mbedtls_ssl_session *session_out; /*!< current session data (out) */
  1024. mbedtls_ssl_session *session; /*!< negotiated session data */
  1025. mbedtls_ssl_session *session_negotiate; /*!< session data in negotiation */
  1026. mbedtls_ssl_handshake_params *handshake; /*!< params required only during
  1027. the handshake process */
  1028. /*
  1029. * Record layer transformations
  1030. */
  1031. mbedtls_ssl_transform *transform_in; /*!< current transform params (in) */
  1032. mbedtls_ssl_transform *transform_out; /*!< current transform params (in) */
  1033. mbedtls_ssl_transform *transform; /*!< negotiated transform params */
  1034. mbedtls_ssl_transform *transform_negotiate; /*!< transform params in negotiation */
  1035. /*
  1036. * Timers
  1037. */
  1038. void *p_timer; /*!< context for the timer callbacks */
  1039. mbedtls_ssl_set_timer_t *f_set_timer; /*!< set timer callback */
  1040. mbedtls_ssl_get_timer_t *f_get_timer; /*!< get timer callback */
  1041. /*
  1042. * Record layer (incoming data)
  1043. */
  1044. unsigned char *in_buf; /*!< input buffer */
  1045. unsigned char *in_ctr; /*!< 64-bit incoming message counter
  1046. TLS: maintained by us
  1047. DTLS: read from peer */
  1048. unsigned char *in_hdr; /*!< start of record header */
  1049. unsigned char *in_len; /*!< two-bytes message length field */
  1050. unsigned char *in_iv; /*!< ivlen-byte IV */
  1051. unsigned char *in_msg; /*!< message contents (in_iv+ivlen) */
  1052. unsigned char *in_offt; /*!< read offset in application data */
  1053. int in_msgtype; /*!< record header: message type */
  1054. size_t in_msglen; /*!< record header: message length */
  1055. size_t in_left; /*!< amount of data read so far */
  1056. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1057. uint16_t in_epoch; /*!< DTLS epoch for incoming records */
  1058. size_t next_record_offset; /*!< offset of the next record in datagram
  1059. (equal to in_left if none) */
  1060. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1061. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1062. uint64_t in_window_top; /*!< last validated record seq_num */
  1063. uint64_t in_window; /*!< bitmask for replay detection */
  1064. #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  1065. size_t in_hslen; /*!< current handshake message length,
  1066. including the handshake header */
  1067. int nb_zero; /*!< # of 0-length encrypted messages */
  1068. int keep_current_message; /*!< drop or reuse current message
  1069. on next call to record layer? */
  1070. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1071. uint8_t disable_datagram_packing; /*!< Disable packing multiple records
  1072. * within a single datagram. */
  1073. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1074. /*
  1075. * Record layer (outgoing data)
  1076. */
  1077. unsigned char *out_buf; /*!< output buffer */
  1078. unsigned char *out_ctr; /*!< 64-bit outgoing message counter */
  1079. unsigned char *out_hdr; /*!< start of record header */
  1080. unsigned char *out_len; /*!< two-bytes message length field */
  1081. unsigned char *out_iv; /*!< ivlen-byte IV */
  1082. unsigned char *out_msg; /*!< message contents (out_iv+ivlen) */
  1083. int out_msgtype; /*!< record header: message type */
  1084. size_t out_msglen; /*!< record header: message length */
  1085. size_t out_left; /*!< amount of data not yet written */
  1086. unsigned char cur_out_ctr[8]; /*!< Outgoing record sequence number. */
  1087. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1088. uint16_t mtu; /*!< path mtu, used to fragment outgoing messages */
  1089. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1090. #if defined(MBEDTLS_ZLIB_SUPPORT)
  1091. unsigned char *compress_buf; /*!< zlib data buffer */
  1092. #endif /* MBEDTLS_ZLIB_SUPPORT */
  1093. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  1094. signed char split_done; /*!< current record already splitted? */
  1095. #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
  1096. /*
  1097. * PKI layer
  1098. */
  1099. int client_auth; /*!< flag for client auth. */
  1100. /*
  1101. * User settings
  1102. */
  1103. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1104. char *hostname; /*!< expected peer CN for verification
  1105. (and SNI if available) */
  1106. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1107. #if defined(MBEDTLS_SSL_ALPN)
  1108. const char *alpn_chosen; /*!< negotiated protocol */
  1109. #endif /* MBEDTLS_SSL_ALPN */
  1110. /*
  1111. * Information for DTLS hello verify
  1112. */
  1113. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  1114. unsigned char *cli_id; /*!< transport-level ID of the client */
  1115. size_t cli_id_len; /*!< length of cli_id */
  1116. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
  1117. /*
  1118. * Secure renegotiation
  1119. */
  1120. /* needed to know when to send extension on server */
  1121. int secure_renegotiation; /*!< does peer support legacy or
  1122. secure renegotiation */
  1123. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1124. size_t verify_data_len; /*!< length of verify data stored */
  1125. char own_verify_data[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN]; /*!< previous handshake verify data */
  1126. char peer_verify_data[MBEDTLS_SSL_VERIFY_DATA_MAX_LEN]; /*!< previous handshake verify data */
  1127. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1128. };
  1129. #if defined(MBEDTLS_SSL_HW_RECORD_ACCEL)
  1130. #define MBEDTLS_SSL_CHANNEL_OUTBOUND 0
  1131. #define MBEDTLS_SSL_CHANNEL_INBOUND 1
  1132. extern int (*mbedtls_ssl_hw_record_init)(mbedtls_ssl_context *ssl,
  1133. const unsigned char *key_enc, const unsigned char *key_dec,
  1134. size_t keylen,
  1135. const unsigned char *iv_enc, const unsigned char *iv_dec,
  1136. size_t ivlen,
  1137. const unsigned char *mac_enc, const unsigned char *mac_dec,
  1138. size_t maclen);
  1139. extern int (*mbedtls_ssl_hw_record_activate)(mbedtls_ssl_context *ssl, int direction);
  1140. extern int (*mbedtls_ssl_hw_record_reset)(mbedtls_ssl_context *ssl);
  1141. extern int (*mbedtls_ssl_hw_record_write)(mbedtls_ssl_context *ssl);
  1142. extern int (*mbedtls_ssl_hw_record_read)(mbedtls_ssl_context *ssl);
  1143. extern int (*mbedtls_ssl_hw_record_finish)(mbedtls_ssl_context *ssl);
  1144. #endif /* MBEDTLS_SSL_HW_RECORD_ACCEL */
  1145. /**
  1146. * \brief Return the name of the ciphersuite associated with the
  1147. * given ID
  1148. *
  1149. * \param ciphersuite_id SSL ciphersuite ID
  1150. *
  1151. * \return a string containing the ciphersuite name
  1152. */
  1153. const char *mbedtls_ssl_get_ciphersuite_name( const int ciphersuite_id );
  1154. /**
  1155. * \brief Return the ID of the ciphersuite associated with the
  1156. * given name
  1157. *
  1158. * \param ciphersuite_name SSL ciphersuite name
  1159. *
  1160. * \return the ID with the ciphersuite or 0 if not found
  1161. */
  1162. int mbedtls_ssl_get_ciphersuite_id( const char *ciphersuite_name );
  1163. /**
  1164. * \brief Initialize an SSL context
  1165. * Just makes the context ready for mbedtls_ssl_setup() or
  1166. * mbedtls_ssl_free()
  1167. *
  1168. * \param ssl SSL context
  1169. */
  1170. void mbedtls_ssl_init( mbedtls_ssl_context *ssl );
  1171. /**
  1172. * \brief Set up an SSL context for use
  1173. *
  1174. * \note No copy of the configuration context is made, it can be
  1175. * shared by many mbedtls_ssl_context structures.
  1176. *
  1177. * \warning The conf structure will be accessed during the session.
  1178. * It must not be modified or freed as long as the session
  1179. * is active.
  1180. *
  1181. * \warning This function must be called exactly once per context.
  1182. * Calling mbedtls_ssl_setup again is not supported, even
  1183. * if no session is active.
  1184. *
  1185. * \param ssl SSL context
  1186. * \param conf SSL configuration to use
  1187. *
  1188. * \return 0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED if
  1189. * memory allocation failed
  1190. */
  1191. int mbedtls_ssl_setup( mbedtls_ssl_context *ssl,
  1192. const mbedtls_ssl_config *conf );
  1193. /**
  1194. * \brief Reset an already initialized SSL context for re-use
  1195. * while retaining application-set variables, function
  1196. * pointers and data.
  1197. *
  1198. * \param ssl SSL context
  1199. * \return 0 if successful, or MBEDTLS_ERR_SSL_ALLOC_FAILED,
  1200. MBEDTLS_ERR_SSL_HW_ACCEL_FAILED or
  1201. * MBEDTLS_ERR_SSL_COMPRESSION_FAILED
  1202. */
  1203. int mbedtls_ssl_session_reset( mbedtls_ssl_context *ssl );
  1204. /**
  1205. * \brief Set the current endpoint type
  1206. *
  1207. * \param conf SSL configuration
  1208. * \param endpoint must be MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
  1209. */
  1210. void mbedtls_ssl_conf_endpoint( mbedtls_ssl_config *conf, int endpoint );
  1211. /**
  1212. * \brief Set the transport type (TLS or DTLS).
  1213. * Default: TLS
  1214. *
  1215. * \note For DTLS, you must either provide a recv callback that
  1216. * doesn't block, or one that handles timeouts, see
  1217. * \c mbedtls_ssl_set_bio(). You also need to provide timer
  1218. * callbacks with \c mbedtls_ssl_set_timer_cb().
  1219. *
  1220. * \param conf SSL configuration
  1221. * \param transport transport type:
  1222. * MBEDTLS_SSL_TRANSPORT_STREAM for TLS,
  1223. * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS.
  1224. */
  1225. void mbedtls_ssl_conf_transport( mbedtls_ssl_config *conf, int transport );
  1226. /**
  1227. * \brief Set the certificate verification mode
  1228. * Default: NONE on server, REQUIRED on client
  1229. *
  1230. * \param conf SSL configuration
  1231. * \param authmode can be:
  1232. *
  1233. * MBEDTLS_SSL_VERIFY_NONE: peer certificate is not checked
  1234. * (default on server)
  1235. * (insecure on client)
  1236. *
  1237. * MBEDTLS_SSL_VERIFY_OPTIONAL: peer certificate is checked, however the
  1238. * handshake continues even if verification failed;
  1239. * mbedtls_ssl_get_verify_result() can be called after the
  1240. * handshake is complete.
  1241. *
  1242. * MBEDTLS_SSL_VERIFY_REQUIRED: peer *must* present a valid certificate,
  1243. * handshake is aborted if verification failed.
  1244. * (default on client)
  1245. *
  1246. * \note On client, MBEDTLS_SSL_VERIFY_REQUIRED is the recommended mode.
  1247. * With MBEDTLS_SSL_VERIFY_OPTIONAL, the user needs to call mbedtls_ssl_get_verify_result() at
  1248. * the right time(s), which may not be obvious, while REQUIRED always perform
  1249. * the verification as soon as possible. For example, REQUIRED was protecting
  1250. * against the "triple handshake" attack even before it was found.
  1251. */
  1252. void mbedtls_ssl_conf_authmode( mbedtls_ssl_config *conf, int authmode );
  1253. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1254. /**
  1255. * \brief Set the verification callback (Optional).
  1256. *
  1257. * If set, the verify callback is called for each
  1258. * certificate in the chain. For implementation
  1259. * information, please see \c mbedtls_x509_crt_verify()
  1260. *
  1261. * \param conf SSL configuration
  1262. * \param f_vrfy verification function
  1263. * \param p_vrfy verification parameter
  1264. */
  1265. void mbedtls_ssl_conf_verify( mbedtls_ssl_config *conf,
  1266. int (*f_vrfy)(void *, mbedtls_x509_crt *, int, uint32_t *),
  1267. void *p_vrfy );
  1268. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1269. /**
  1270. * \brief Set the random number generator callback
  1271. *
  1272. * \param conf SSL configuration
  1273. * \param f_rng RNG function
  1274. * \param p_rng RNG parameter
  1275. */
  1276. void mbedtls_ssl_conf_rng( mbedtls_ssl_config *conf,
  1277. int (*f_rng)(void *, unsigned char *, size_t),
  1278. void *p_rng );
  1279. /**
  1280. * \brief Set the debug callback
  1281. *
  1282. * The callback has the following argument:
  1283. * void * opaque context for the callback
  1284. * int debug level
  1285. * const char * file name
  1286. * int line number
  1287. * const char * message
  1288. *
  1289. * \param conf SSL configuration
  1290. * \param f_dbg debug function
  1291. * \param p_dbg debug parameter
  1292. */
  1293. void mbedtls_ssl_conf_dbg( mbedtls_ssl_config *conf,
  1294. void (*f_dbg)(void *, int, const char *, int, const char *),
  1295. void *p_dbg );
  1296. /**
  1297. * \brief Set the underlying BIO callbacks for write, read and
  1298. * read-with-timeout.
  1299. *
  1300. * \param ssl SSL context
  1301. * \param p_bio parameter (context) shared by BIO callbacks
  1302. * \param f_send write callback
  1303. * \param f_recv read callback
  1304. * \param f_recv_timeout blocking read callback with timeout.
  1305. *
  1306. * \note One of f_recv or f_recv_timeout can be NULL, in which case
  1307. * the other is used. If both are non-NULL, f_recv_timeout is
  1308. * used and f_recv is ignored (as if it were NULL).
  1309. *
  1310. * \note The two most common use cases are:
  1311. * - non-blocking I/O, f_recv != NULL, f_recv_timeout == NULL
  1312. * - blocking I/O, f_recv == NULL, f_recv_timout != NULL
  1313. *
  1314. * \note For DTLS, you need to provide either a non-NULL
  1315. * f_recv_timeout callback, or a f_recv that doesn't block.
  1316. *
  1317. * \note See the documentations of \c mbedtls_ssl_send_t,
  1318. * \c mbedtls_ssl_recv_t and \c mbedtls_ssl_recv_timeout_t for
  1319. * the conventions those callbacks must follow.
  1320. *
  1321. * \note On some platforms, net_sockets.c provides
  1322. * \c mbedtls_net_send(), \c mbedtls_net_recv() and
  1323. * \c mbedtls_net_recv_timeout() that are suitable to be used
  1324. * here.
  1325. */
  1326. void mbedtls_ssl_set_bio( mbedtls_ssl_context *ssl,
  1327. void *p_bio,
  1328. mbedtls_ssl_send_t *f_send,
  1329. mbedtls_ssl_recv_t *f_recv,
  1330. mbedtls_ssl_recv_timeout_t *f_recv_timeout );
  1331. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1332. /**
  1333. * \brief Set the Maximum Tranport Unit (MTU).
  1334. * Special value: 0 means unset (no limit).
  1335. * This represents the maximum size of a datagram payload
  1336. * handled by the transport layer (usually UDP) as determined
  1337. * by the network link and stack. In practice, this controls
  1338. * the maximum size datagram the DTLS layer will pass to the
  1339. * \c f_send() callback set using \c mbedtls_ssl_set_bio().
  1340. *
  1341. * \note The limit on datagram size is converted to a limit on
  1342. * record payload by subtracting the current overhead of
  1343. * encapsulation and encryption/authentication if any.
  1344. *
  1345. * \note This can be called at any point during the connection, for
  1346. * example when a Path Maximum Transfer Unit (PMTU)
  1347. * estimate becomes available from other sources,
  1348. * such as lower (or higher) protocol layers.
  1349. *
  1350. * \note This setting only controls the size of the packets we send,
  1351. * and does not restrict the size of the datagrams we're
  1352. * willing to receive. Client-side, you can request the
  1353. * server to use smaller records with \c
  1354. * mbedtls_ssl_conf_max_frag_len().
  1355. *
  1356. * \note If both a MTU and a maximum fragment length have been
  1357. * configured (or negotiated with the peer), the resulting
  1358. * lower limit on record payload (see first note) is used.
  1359. *
  1360. * \note This can only be used to decrease the maximum size
  1361. * of datagrams (hence records, see first note) sent. It
  1362. * cannot be used to increase the maximum size of records over
  1363. * the limit set by #MBEDTLS_SSL_OUT_CONTENT_LEN.
  1364. *
  1365. * \note Values lower than the current record layer expansion will
  1366. * result in an error when trying to send data.
  1367. *
  1368. * \note Using record compression together with a non-zero MTU value
  1369. * will result in an error when trying to send data.
  1370. *
  1371. * \param ssl SSL context
  1372. * \param mtu Value of the path MTU in bytes
  1373. */
  1374. void mbedtls_ssl_set_mtu( mbedtls_ssl_context *ssl, uint16_t mtu );
  1375. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1376. /**
  1377. * \brief Set the timeout period for mbedtls_ssl_read()
  1378. * (Default: no timeout.)
  1379. *
  1380. * \param conf SSL configuration context
  1381. * \param timeout Timeout value in milliseconds.
  1382. * Use 0 for no timeout (default).
  1383. *
  1384. * \note With blocking I/O, this will only work if a non-NULL
  1385. * \c f_recv_timeout was set with \c mbedtls_ssl_set_bio().
  1386. * With non-blocking I/O, this will only work if timer
  1387. * callbacks were set with \c mbedtls_ssl_set_timer_cb().
  1388. *
  1389. * \note With non-blocking I/O, you may also skip this function
  1390. * altogether and handle timeouts at the application layer.
  1391. */
  1392. void mbedtls_ssl_conf_read_timeout( mbedtls_ssl_config *conf, uint32_t timeout );
  1393. /**
  1394. * \brief Set the timer callbacks (Mandatory for DTLS.)
  1395. *
  1396. * \param ssl SSL context
  1397. * \param p_timer parameter (context) shared by timer callbacks
  1398. * \param f_set_timer set timer callback
  1399. * \param f_get_timer get timer callback. Must return:
  1400. *
  1401. * \note See the documentation of \c mbedtls_ssl_set_timer_t and
  1402. * \c mbedtls_ssl_get_timer_t for the conventions this pair of
  1403. * callbacks must follow.
  1404. *
  1405. * \note On some platforms, timing.c provides
  1406. * \c mbedtls_timing_set_delay() and
  1407. * \c mbedtls_timing_get_delay() that are suitable for using
  1408. * here, except if using an event-driven style.
  1409. *
  1410. * \note See also the "DTLS tutorial" article in our knowledge base.
  1411. * https://tls.mbed.org/kb/how-to/dtls-tutorial
  1412. */
  1413. void mbedtls_ssl_set_timer_cb( mbedtls_ssl_context *ssl,
  1414. void *p_timer,
  1415. mbedtls_ssl_set_timer_t *f_set_timer,
  1416. mbedtls_ssl_get_timer_t *f_get_timer );
  1417. /**
  1418. * \brief Callback type: generate and write session ticket
  1419. *
  1420. * \note This describes what a callback implementation should do.
  1421. * This callback should generate an encrypted and
  1422. * authenticated ticket for the session and write it to the
  1423. * output buffer. Here, ticket means the opaque ticket part
  1424. * of the NewSessionTicket structure of RFC 5077.
  1425. *
  1426. * \param p_ticket Context for the callback
  1427. * \param session SSL session to be written in the ticket
  1428. * \param start Start of the output buffer
  1429. * \param end End of the output buffer
  1430. * \param tlen On exit, holds the length written
  1431. * \param lifetime On exit, holds the lifetime of the ticket in seconds
  1432. *
  1433. * \return 0 if successful, or
  1434. * a specific MBEDTLS_ERR_XXX code.
  1435. */
  1436. typedef int mbedtls_ssl_ticket_write_t( void *p_ticket,
  1437. const mbedtls_ssl_session *session,
  1438. unsigned char *start,
  1439. const unsigned char *end,
  1440. size_t *tlen,
  1441. uint32_t *lifetime );
  1442. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  1443. /**
  1444. * \brief Callback type: Export key block and master secret
  1445. *
  1446. * \note This is required for certain uses of TLS, e.g. EAP-TLS
  1447. * (RFC 5216) and Thread. The key pointers are ephemeral and
  1448. * therefore must not be stored. The master secret and keys
  1449. * should not be used directly except as an input to a key
  1450. * derivation function.
  1451. *
  1452. * \param p_expkey Context for the callback
  1453. * \param ms Pointer to master secret (fixed length: 48 bytes)
  1454. * \param kb Pointer to key block, see RFC 5246 section 6.3
  1455. * (variable length: 2 * maclen + 2 * keylen + 2 * ivlen).
  1456. * \param maclen MAC length
  1457. * \param keylen Key length
  1458. * \param ivlen IV length
  1459. *
  1460. * \return 0 if successful, or
  1461. * a specific MBEDTLS_ERR_XXX code.
  1462. */
  1463. typedef int mbedtls_ssl_export_keys_t( void *p_expkey,
  1464. const unsigned char *ms,
  1465. const unsigned char *kb,
  1466. size_t maclen,
  1467. size_t keylen,
  1468. size_t ivlen );
  1469. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  1470. /**
  1471. * \brief Callback type: parse and load session ticket
  1472. *
  1473. * \note This describes what a callback implementation should do.
  1474. * This callback should parse a session ticket as generated
  1475. * by the corresponding mbedtls_ssl_ticket_write_t function,
  1476. * and, if the ticket is authentic and valid, load the
  1477. * session.
  1478. *
  1479. * \note The implementation is allowed to modify the first len
  1480. * bytes of the input buffer, eg to use it as a temporary
  1481. * area for the decrypted ticket contents.
  1482. *
  1483. * \param p_ticket Context for the callback
  1484. * \param session SSL session to be loaded
  1485. * \param buf Start of the buffer containing the ticket
  1486. * \param len Length of the ticket.
  1487. *
  1488. * \return 0 if successful, or
  1489. * MBEDTLS_ERR_SSL_INVALID_MAC if not authentic, or
  1490. * MBEDTLS_ERR_SSL_SESSION_TICKET_EXPIRED if expired, or
  1491. * any other non-zero code for other failures.
  1492. */
  1493. typedef int mbedtls_ssl_ticket_parse_t( void *p_ticket,
  1494. mbedtls_ssl_session *session,
  1495. unsigned char *buf,
  1496. size_t len );
  1497. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_SRV_C)
  1498. /**
  1499. * \brief Configure SSL session ticket callbacks (server only).
  1500. * (Default: none.)
  1501. *
  1502. * \note On server, session tickets are enabled by providing
  1503. * non-NULL callbacks.
  1504. *
  1505. * \note On client, use \c mbedtls_ssl_conf_session_tickets().
  1506. *
  1507. * \param conf SSL configuration context
  1508. * \param f_ticket_write Callback for writing a ticket
  1509. * \param f_ticket_parse Callback for parsing a ticket
  1510. * \param p_ticket Context shared by the two callbacks
  1511. */
  1512. void mbedtls_ssl_conf_session_tickets_cb( mbedtls_ssl_config *conf,
  1513. mbedtls_ssl_ticket_write_t *f_ticket_write,
  1514. mbedtls_ssl_ticket_parse_t *f_ticket_parse,
  1515. void *p_ticket );
  1516. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_SRV_C */
  1517. #if defined(MBEDTLS_SSL_EXPORT_KEYS)
  1518. /**
  1519. * \brief Configure key export callback.
  1520. * (Default: none.)
  1521. *
  1522. * \note See \c mbedtls_ssl_export_keys_t.
  1523. *
  1524. * \param conf SSL configuration context
  1525. * \param f_export_keys Callback for exporting keys
  1526. * \param p_export_keys Context for the callback
  1527. */
  1528. void mbedtls_ssl_conf_export_keys_cb( mbedtls_ssl_config *conf,
  1529. mbedtls_ssl_export_keys_t *f_export_keys,
  1530. void *p_export_keys );
  1531. #endif /* MBEDTLS_SSL_EXPORT_KEYS */
  1532. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1533. /**
  1534. * \brief Configure asynchronous private key operation callbacks.
  1535. *
  1536. * \param conf SSL configuration context
  1537. * \param f_async_sign Callback to start a signature operation. See
  1538. * the description of ::mbedtls_ssl_async_sign_t
  1539. * for more information. This may be \c NULL if the
  1540. * external processor does not support any signature
  1541. * operation; in this case the private key object
  1542. * associated with the certificate will be used.
  1543. * \param f_async_decrypt Callback to start a decryption operation. See
  1544. * the description of ::mbedtls_ssl_async_decrypt_t
  1545. * for more information. This may be \c NULL if the
  1546. * external processor does not support any decryption
  1547. * operation; in this case the private key object
  1548. * associated with the certificate will be used.
  1549. * \param f_async_resume Callback to resume an asynchronous operation. See
  1550. * the description of ::mbedtls_ssl_async_resume_t
  1551. * for more information. This may not be \c NULL unless
  1552. * \p f_async_sign and \p f_async_decrypt are both
  1553. * \c NULL.
  1554. * \param f_async_cancel Callback to cancel an asynchronous operation. See
  1555. * the description of ::mbedtls_ssl_async_cancel_t
  1556. * for more information. This may be \c NULL if
  1557. * no cleanup is needed.
  1558. * \param config_data A pointer to configuration data which can be
  1559. * retrieved with
  1560. * mbedtls_ssl_conf_get_async_config_data(). The
  1561. * library stores this value without dereferencing it.
  1562. */
  1563. void mbedtls_ssl_conf_async_private_cb( mbedtls_ssl_config *conf,
  1564. mbedtls_ssl_async_sign_t *f_async_sign,
  1565. mbedtls_ssl_async_decrypt_t *f_async_decrypt,
  1566. mbedtls_ssl_async_resume_t *f_async_resume,
  1567. mbedtls_ssl_async_cancel_t *f_async_cancel,
  1568. void *config_data );
  1569. /**
  1570. * \brief Retrieve the configuration data set by
  1571. * mbedtls_ssl_conf_async_private_cb().
  1572. *
  1573. * \param conf SSL configuration context
  1574. * \return The configuration data set by
  1575. * mbedtls_ssl_conf_async_private_cb().
  1576. */
  1577. void *mbedtls_ssl_conf_get_async_config_data( const mbedtls_ssl_config *conf );
  1578. /**
  1579. * \brief Retrieve the asynchronous operation user context.
  1580. *
  1581. * \note This function may only be called while a handshake
  1582. * is in progress.
  1583. *
  1584. * \param ssl The SSL context to access.
  1585. *
  1586. * \return The asynchronous operation user context that was last
  1587. * set during the current handshake. If
  1588. * mbedtls_ssl_set_async_operation_data() has not yet been
  1589. * called during the current handshake, this function returns
  1590. * \c NULL.
  1591. */
  1592. void *mbedtls_ssl_get_async_operation_data( const mbedtls_ssl_context *ssl );
  1593. /**
  1594. * \brief Retrieve the asynchronous operation user context.
  1595. *
  1596. * \note This function may only be called while a handshake
  1597. * is in progress.
  1598. *
  1599. * \param ssl The SSL context to access.
  1600. * \param ctx The new value of the asynchronous operation user context.
  1601. * Call mbedtls_ssl_get_async_operation_data() later during the
  1602. * same handshake to retrieve this value.
  1603. */
  1604. void mbedtls_ssl_set_async_operation_data( mbedtls_ssl_context *ssl,
  1605. void *ctx );
  1606. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1607. /**
  1608. * \brief Callback type: generate a cookie
  1609. *
  1610. * \param ctx Context for the callback
  1611. * \param p Buffer to write to,
  1612. * must be updated to point right after the cookie
  1613. * \param end Pointer to one past the end of the output buffer
  1614. * \param info Client ID info that was passed to
  1615. * \c mbedtls_ssl_set_client_transport_id()
  1616. * \param ilen Length of info in bytes
  1617. *
  1618. * \return The callback must return 0 on success,
  1619. * or a negative error code.
  1620. */
  1621. typedef int mbedtls_ssl_cookie_write_t( void *ctx,
  1622. unsigned char **p, unsigned char *end,
  1623. const unsigned char *info, size_t ilen );
  1624. /**
  1625. * \brief Callback type: verify a cookie
  1626. *
  1627. * \param ctx Context for the callback
  1628. * \param cookie Cookie to verify
  1629. * \param clen Length of cookie
  1630. * \param info Client ID info that was passed to
  1631. * \c mbedtls_ssl_set_client_transport_id()
  1632. * \param ilen Length of info in bytes
  1633. *
  1634. * \return The callback must return 0 if cookie is valid,
  1635. * or a negative error code.
  1636. */
  1637. typedef int mbedtls_ssl_cookie_check_t( void *ctx,
  1638. const unsigned char *cookie, size_t clen,
  1639. const unsigned char *info, size_t ilen );
  1640. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY) && defined(MBEDTLS_SSL_SRV_C)
  1641. /**
  1642. * \brief Register callbacks for DTLS cookies
  1643. * (Server only. DTLS only.)
  1644. *
  1645. * Default: dummy callbacks that fail, in order to force you to
  1646. * register working callbacks (and initialize their context).
  1647. *
  1648. * To disable HelloVerifyRequest, register NULL callbacks.
  1649. *
  1650. * \warning Disabling hello verification allows your server to be used
  1651. * for amplification in DoS attacks against other hosts.
  1652. * Only disable if you known this can't happen in your
  1653. * particular environment.
  1654. *
  1655. * \note See comments on \c mbedtls_ssl_handshake() about handling
  1656. * the MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED that is expected
  1657. * on the first handshake attempt when this is enabled.
  1658. *
  1659. * \note This is also necessary to handle client reconnection from
  1660. * the same port as described in RFC 6347 section 4.2.8 (only
  1661. * the variant with cookies is supported currently). See
  1662. * comments on \c mbedtls_ssl_read() for details.
  1663. *
  1664. * \param conf SSL configuration
  1665. * \param f_cookie_write Cookie write callback
  1666. * \param f_cookie_check Cookie check callback
  1667. * \param p_cookie Context for both callbacks
  1668. */
  1669. void mbedtls_ssl_conf_dtls_cookies( mbedtls_ssl_config *conf,
  1670. mbedtls_ssl_cookie_write_t *f_cookie_write,
  1671. mbedtls_ssl_cookie_check_t *f_cookie_check,
  1672. void *p_cookie );
  1673. /**
  1674. * \brief Set client's transport-level identification info.
  1675. * (Server only. DTLS only.)
  1676. *
  1677. * This is usually the IP address (and port), but could be
  1678. * anything identify the client depending on the underlying
  1679. * network stack. Used for HelloVerifyRequest with DTLS.
  1680. * This is *not* used to route the actual packets.
  1681. *
  1682. * \param ssl SSL context
  1683. * \param info Transport-level info identifying the client (eg IP + port)
  1684. * \param ilen Length of info in bytes
  1685. *
  1686. * \note An internal copy is made, so the info buffer can be reused.
  1687. *
  1688. * \return 0 on success,
  1689. * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used on client,
  1690. * MBEDTLS_ERR_SSL_ALLOC_FAILED if out of memory.
  1691. */
  1692. int mbedtls_ssl_set_client_transport_id( mbedtls_ssl_context *ssl,
  1693. const unsigned char *info,
  1694. size_t ilen );
  1695. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY && MBEDTLS_SSL_SRV_C */
  1696. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  1697. /**
  1698. * \brief Enable or disable anti-replay protection for DTLS.
  1699. * (DTLS only, no effect on TLS.)
  1700. * Default: enabled.
  1701. *
  1702. * \param conf SSL configuration
  1703. * \param mode MBEDTLS_SSL_ANTI_REPLAY_ENABLED or MBEDTLS_SSL_ANTI_REPLAY_DISABLED.
  1704. *
  1705. * \warning Disabling this is a security risk unless the application
  1706. * protocol handles duplicated packets in a safe way. You
  1707. * should not disable this without careful consideration.
  1708. * However, if your application already detects duplicated
  1709. * packets and needs information about them to adjust its
  1710. * transmission strategy, then you'll want to disable this.
  1711. */
  1712. void mbedtls_ssl_conf_dtls_anti_replay( mbedtls_ssl_config *conf, char mode );
  1713. #endif /* MBEDTLS_SSL_DTLS_ANTI_REPLAY */
  1714. #if defined(MBEDTLS_SSL_DTLS_BADMAC_LIMIT)
  1715. /**
  1716. * \brief Set a limit on the number of records with a bad MAC
  1717. * before terminating the connection.
  1718. * (DTLS only, no effect on TLS.)
  1719. * Default: 0 (disabled).
  1720. *
  1721. * \param conf SSL configuration
  1722. * \param limit Limit, or 0 to disable.
  1723. *
  1724. * \note If the limit is N, then the connection is terminated when
  1725. * the Nth non-authentic record is seen.
  1726. *
  1727. * \note Records with an invalid header are not counted, only the
  1728. * ones going through the authentication-decryption phase.
  1729. *
  1730. * \note This is a security trade-off related to the fact that it's
  1731. * often relatively easy for an active attacker ot inject UDP
  1732. * datagrams. On one hand, setting a low limit here makes it
  1733. * easier for such an attacker to forcibly terminated a
  1734. * connection. On the other hand, a high limit or no limit
  1735. * might make us waste resources checking authentication on
  1736. * many bogus packets.
  1737. */
  1738. void mbedtls_ssl_conf_dtls_badmac_limit( mbedtls_ssl_config *conf, unsigned limit );
  1739. #endif /* MBEDTLS_SSL_DTLS_BADMAC_LIMIT */
  1740. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1741. /**
  1742. * \brief Allow or disallow packing of multiple handshake records
  1743. * within a single datagram.
  1744. *
  1745. * \param ssl The SSL context to configure.
  1746. * \param allow_packing This determines whether datagram packing may
  1747. * be used or not. A value of \c 0 means that every
  1748. * record will be sent in a separate datagram; a
  1749. * value of \c 1 means that, if space permits,
  1750. * multiple handshake messages (including CCS) belonging to
  1751. * a single flight may be packed within a single datagram.
  1752. *
  1753. * \note This is enabled by default and should only be disabled
  1754. * for test purposes, or if datagram packing causes
  1755. * interoperability issues with peers that don't support it.
  1756. *
  1757. * \note Allowing datagram packing reduces the network load since
  1758. * there's less overhead if multiple messages share the same
  1759. * datagram. Also, it increases the handshake efficiency
  1760. * since messages belonging to a single datagram will not
  1761. * be reordered in transit, and so future message buffering
  1762. * or flight retransmission (if no buffering is used) as
  1763. * means to deal with reordering are needed less frequently.
  1764. *
  1765. * \note Application records are not affected by this option and
  1766. * are currently always sent in separate datagrams.
  1767. *
  1768. */
  1769. void mbedtls_ssl_set_datagram_packing( mbedtls_ssl_context *ssl,
  1770. unsigned allow_packing );
  1771. /**
  1772. * \brief Set retransmit timeout values for the DTLS handshake.
  1773. * (DTLS only, no effect on TLS.)
  1774. *
  1775. * \param conf SSL configuration
  1776. * \param min Initial timeout value in milliseconds.
  1777. * Default: 1000 (1 second).
  1778. * \param max Maximum timeout value in milliseconds.
  1779. * Default: 60000 (60 seconds).
  1780. *
  1781. * \note Default values are from RFC 6347 section 4.2.4.1.
  1782. *
  1783. * \note The 'min' value should typically be slightly above the
  1784. * expected round-trip time to your peer, plus whatever time
  1785. * it takes for the peer to process the message. For example,
  1786. * if your RTT is about 600ms and you peer needs up to 1s to
  1787. * do the cryptographic operations in the handshake, then you
  1788. * should set 'min' slightly above 1600. Lower values of 'min'
  1789. * might cause spurious resends which waste network resources,
  1790. * while larger value of 'min' will increase overall latency
  1791. * on unreliable network links.
  1792. *
  1793. * \note The more unreliable your network connection is, the larger
  1794. * your max / min ratio needs to be in order to achieve
  1795. * reliable handshakes.
  1796. *
  1797. * \note Messages are retransmitted up to log2(ceil(max/min)) times.
  1798. * For example, if min = 1s and max = 5s, the retransmit plan
  1799. * goes: send ... 1s -> resend ... 2s -> resend ... 4s ->
  1800. * resend ... 5s -> give up and return a timeout error.
  1801. */
  1802. void mbedtls_ssl_conf_handshake_timeout( mbedtls_ssl_config *conf, uint32_t min, uint32_t max );
  1803. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1804. #if defined(MBEDTLS_SSL_SRV_C)
  1805. /**
  1806. * \brief Set the session cache callbacks (server-side only)
  1807. * If not set, no session resuming is done (except if session
  1808. * tickets are enabled too).
  1809. *
  1810. * The session cache has the responsibility to check for stale
  1811. * entries based on timeout. See RFC 5246 for recommendations.
  1812. *
  1813. * Warning: session.peer_cert is cleared by the SSL/TLS layer on
  1814. * connection shutdown, so do not cache the pointer! Either set
  1815. * it to NULL or make a full copy of the certificate.
  1816. *
  1817. * The get callback is called once during the initial handshake
  1818. * to enable session resuming. The get function has the
  1819. * following parameters: (void *parameter, mbedtls_ssl_session *session)
  1820. * If a valid entry is found, it should fill the master of
  1821. * the session object with the cached values and return 0,
  1822. * return 1 otherwise. Optionally peer_cert can be set as well
  1823. * if it is properly present in cache entry.
  1824. *
  1825. * The set callback is called once during the initial handshake
  1826. * to enable session resuming after the entire handshake has
  1827. * been finished. The set function has the following parameters:
  1828. * (void *parameter, const mbedtls_ssl_session *session). The function
  1829. * should create a cache entry for future retrieval based on
  1830. * the data in the session structure and should keep in mind
  1831. * that the mbedtls_ssl_session object presented (and all its referenced
  1832. * data) is cleared by the SSL/TLS layer when the connection is
  1833. * terminated. It is recommended to add metadata to determine if
  1834. * an entry is still valid in the future. Return 0 if
  1835. * successfully cached, return 1 otherwise.
  1836. *
  1837. * \param conf SSL configuration
  1838. * \param p_cache parmater (context) for both callbacks
  1839. * \param f_get_cache session get callback
  1840. * \param f_set_cache session set callback
  1841. */
  1842. void mbedtls_ssl_conf_session_cache( mbedtls_ssl_config *conf,
  1843. void *p_cache,
  1844. int (*f_get_cache)(void *, mbedtls_ssl_session *),
  1845. int (*f_set_cache)(void *, const mbedtls_ssl_session *) );
  1846. #endif /* MBEDTLS_SSL_SRV_C */
  1847. #if defined(MBEDTLS_SSL_CLI_C)
  1848. /**
  1849. * \brief Request resumption of session (client-side only)
  1850. * Session data is copied from presented session structure.
  1851. *
  1852. * \param ssl SSL context
  1853. * \param session session context
  1854. *
  1855. * \return 0 if successful,
  1856. * MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
  1857. * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
  1858. * arguments are otherwise invalid
  1859. *
  1860. * \sa mbedtls_ssl_get_session()
  1861. */
  1862. int mbedtls_ssl_set_session( mbedtls_ssl_context *ssl, const mbedtls_ssl_session *session );
  1863. #endif /* MBEDTLS_SSL_CLI_C */
  1864. /**
  1865. * \brief Set the list of allowed ciphersuites and the preference
  1866. * order. First in the list has the highest preference.
  1867. * (Overrides all version-specific lists)
  1868. *
  1869. * The ciphersuites array is not copied, and must remain
  1870. * valid for the lifetime of the ssl_config.
  1871. *
  1872. * Note: The server uses its own preferences
  1873. * over the preference of the client unless
  1874. * MBEDTLS_SSL_SRV_RESPECT_CLIENT_PREFERENCE is defined!
  1875. *
  1876. * \param conf SSL configuration
  1877. * \param ciphersuites 0-terminated list of allowed ciphersuites
  1878. */
  1879. void mbedtls_ssl_conf_ciphersuites( mbedtls_ssl_config *conf,
  1880. const int *ciphersuites );
  1881. /**
  1882. * \brief Set the list of allowed ciphersuites and the
  1883. * preference order for a specific version of the protocol.
  1884. * (Only useful on the server side)
  1885. *
  1886. * The ciphersuites array is not copied, and must remain
  1887. * valid for the lifetime of the ssl_config.
  1888. *
  1889. * \param conf SSL configuration
  1890. * \param ciphersuites 0-terminated list of allowed ciphersuites
  1891. * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3
  1892. * supported)
  1893. * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
  1894. * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
  1895. * MBEDTLS_SSL_MINOR_VERSION_3 supported)
  1896. *
  1897. * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0
  1898. * and MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
  1899. */
  1900. void mbedtls_ssl_conf_ciphersuites_for_version( mbedtls_ssl_config *conf,
  1901. const int *ciphersuites,
  1902. int major, int minor );
  1903. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1904. /**
  1905. * \brief Set the X.509 security profile used for verification
  1906. *
  1907. * \note The restrictions are enforced for all certificates in the
  1908. * chain. However, signatures in the handshake are not covered
  1909. * by this setting but by \b mbedtls_ssl_conf_sig_hashes().
  1910. *
  1911. * \param conf SSL configuration
  1912. * \param profile Profile to use
  1913. */
  1914. void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf,
  1915. const mbedtls_x509_crt_profile *profile );
  1916. /**
  1917. * \brief Set the data required to verify peer certificate
  1918. *
  1919. * \note See \c mbedtls_x509_crt_verify() for notes regarding the
  1920. * parameters ca_chain (maps to trust_ca for that function)
  1921. * and ca_crl.
  1922. *
  1923. * \param conf SSL configuration
  1924. * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
  1925. * \param ca_crl trusted CA CRLs
  1926. */
  1927. void mbedtls_ssl_conf_ca_chain( mbedtls_ssl_config *conf,
  1928. mbedtls_x509_crt *ca_chain,
  1929. mbedtls_x509_crl *ca_crl );
  1930. /**
  1931. * \brief Set own certificate chain and private key
  1932. *
  1933. * \note own_cert should contain in order from the bottom up your
  1934. * certificate chain. The top certificate (self-signed)
  1935. * can be omitted.
  1936. *
  1937. * \note On server, this function can be called multiple times to
  1938. * provision more than one cert/key pair (eg one ECDSA, one
  1939. * RSA with SHA-256, one RSA with SHA-1). An adequate
  1940. * certificate will be selected according to the client's
  1941. * advertised capabilities. In case multiple certificates are
  1942. * adequate, preference is given to the one set by the first
  1943. * call to this function, then second, etc.
  1944. *
  1945. * \note On client, only the first call has any effect. That is,
  1946. * only one client certificate can be provisioned. The
  1947. * server's preferences in its CertficateRequest message will
  1948. * be ignored and our only cert will be sent regardless of
  1949. * whether it matches those preferences - the server can then
  1950. * decide what it wants to do with it.
  1951. *
  1952. * \note The provided \p pk_key needs to match the public key in the
  1953. * first certificate in \p own_cert, or all handshakes using
  1954. * that certificate will fail. It is your responsibility
  1955. * to ensure that; this function will not perform any check.
  1956. * You may use mbedtls_pk_check_pair() in order to perform
  1957. * this check yourself, but be aware that this function can
  1958. * be computationally expensive on some key types.
  1959. *
  1960. * \param conf SSL configuration
  1961. * \param own_cert own public certificate chain
  1962. * \param pk_key own private key
  1963. *
  1964. * \return 0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
  1965. */
  1966. int mbedtls_ssl_conf_own_cert( mbedtls_ssl_config *conf,
  1967. mbedtls_x509_crt *own_cert,
  1968. mbedtls_pk_context *pk_key );
  1969. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1970. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1971. /**
  1972. * \brief Set the Pre Shared Key (PSK) and the expected identity name
  1973. *
  1974. * \note This is mainly useful for clients. Servers will usually
  1975. * want to use \c mbedtls_ssl_conf_psk_cb() instead.
  1976. *
  1977. * \note Currently clients can only register one pre-shared key.
  1978. * In other words, the servers' identity hint is ignored.
  1979. * Support for setting multiple PSKs on clients and selecting
  1980. * one based on the identity hint is not a planned feature but
  1981. * feedback is welcomed.
  1982. *
  1983. * \param conf SSL configuration
  1984. * \param psk pointer to the pre-shared key
  1985. * \param psk_len pre-shared key length
  1986. * \param psk_identity pointer to the pre-shared key identity
  1987. * \param psk_identity_len identity key length
  1988. *
  1989. * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
  1990. */
  1991. int mbedtls_ssl_conf_psk( mbedtls_ssl_config *conf,
  1992. const unsigned char *psk, size_t psk_len,
  1993. const unsigned char *psk_identity, size_t psk_identity_len );
  1994. /**
  1995. * \brief Set the Pre Shared Key (PSK) for the current handshake
  1996. *
  1997. * \note This should only be called inside the PSK callback,
  1998. * ie the function passed to \c mbedtls_ssl_conf_psk_cb().
  1999. *
  2000. * \param ssl SSL context
  2001. * \param psk pointer to the pre-shared key
  2002. * \param psk_len pre-shared key length
  2003. *
  2004. * \return 0 if successful or MBEDTLS_ERR_SSL_ALLOC_FAILED
  2005. */
  2006. int mbedtls_ssl_set_hs_psk( mbedtls_ssl_context *ssl,
  2007. const unsigned char *psk, size_t psk_len );
  2008. /**
  2009. * \brief Set the PSK callback (server-side only).
  2010. *
  2011. * If set, the PSK callback is called for each
  2012. * handshake where a PSK ciphersuite was negotiated.
  2013. * The caller provides the identity received and wants to
  2014. * receive the actual PSK data and length.
  2015. *
  2016. * The callback has the following parameters: (void *parameter,
  2017. * mbedtls_ssl_context *ssl, const unsigned char *psk_identity,
  2018. * size_t identity_len)
  2019. * If a valid PSK identity is found, the callback should use
  2020. * \c mbedtls_ssl_set_hs_psk() on the ssl context to set the
  2021. * correct PSK and return 0.
  2022. * Any other return value will result in a denied PSK identity.
  2023. *
  2024. * \note If you set a PSK callback using this function, then you
  2025. * don't need to set a PSK key and identity using
  2026. * \c mbedtls_ssl_conf_psk().
  2027. *
  2028. * \param conf SSL configuration
  2029. * \param f_psk PSK identity function
  2030. * \param p_psk PSK identity parameter
  2031. */
  2032. void mbedtls_ssl_conf_psk_cb( mbedtls_ssl_config *conf,
  2033. int (*f_psk)(void *, mbedtls_ssl_context *, const unsigned char *,
  2034. size_t),
  2035. void *p_psk );
  2036. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  2037. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_SRV_C)
  2038. #if !defined(MBEDTLS_DEPRECATED_REMOVED)
  2039. #if defined(MBEDTLS_DEPRECATED_WARNING)
  2040. #define MBEDTLS_DEPRECATED __attribute__((deprecated))
  2041. #else
  2042. #define MBEDTLS_DEPRECATED
  2043. #endif
  2044. /**
  2045. * \brief Set the Diffie-Hellman public P and G values,
  2046. * read as hexadecimal strings (server-side only)
  2047. * (Default values: MBEDTLS_DHM_RFC3526_MODP_2048_[PG])
  2048. *
  2049. * \param conf SSL configuration
  2050. * \param dhm_P Diffie-Hellman-Merkle modulus
  2051. * \param dhm_G Diffie-Hellman-Merkle generator
  2052. *
  2053. * \deprecated Superseded by \c mbedtls_ssl_conf_dh_param_bin.
  2054. *
  2055. * \return 0 if successful
  2056. */
  2057. MBEDTLS_DEPRECATED int mbedtls_ssl_conf_dh_param( mbedtls_ssl_config *conf,
  2058. const char *dhm_P,
  2059. const char *dhm_G );
  2060. #endif /* MBEDTLS_DEPRECATED_REMOVED */
  2061. /**
  2062. * \brief Set the Diffie-Hellman public P and G values
  2063. * from big-endian binary presentations.
  2064. * (Default values: MBEDTLS_DHM_RFC3526_MODP_2048_[PG]_BIN)
  2065. *
  2066. * \param conf SSL configuration
  2067. * \param dhm_P Diffie-Hellman-Merkle modulus in big-endian binary form
  2068. * \param P_len Length of DHM modulus
  2069. * \param dhm_G Diffie-Hellman-Merkle generator in big-endian binary form
  2070. * \param G_len Length of DHM generator
  2071. *
  2072. * \return 0 if successful
  2073. */
  2074. int mbedtls_ssl_conf_dh_param_bin( mbedtls_ssl_config *conf,
  2075. const unsigned char *dhm_P, size_t P_len,
  2076. const unsigned char *dhm_G, size_t G_len );
  2077. /**
  2078. * \brief Set the Diffie-Hellman public P and G values,
  2079. * read from existing context (server-side only)
  2080. *
  2081. * \param conf SSL configuration
  2082. * \param dhm_ctx Diffie-Hellman-Merkle context
  2083. *
  2084. * \return 0 if successful
  2085. */
  2086. int mbedtls_ssl_conf_dh_param_ctx( mbedtls_ssl_config *conf, mbedtls_dhm_context *dhm_ctx );
  2087. #endif /* MBEDTLS_DHM_C && defined(MBEDTLS_SSL_SRV_C) */
  2088. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_SSL_CLI_C)
  2089. /**
  2090. * \brief Set the minimum length for Diffie-Hellman parameters.
  2091. * (Client-side only.)
  2092. * (Default: 1024 bits.)
  2093. *
  2094. * \param conf SSL configuration
  2095. * \param bitlen Minimum bit length of the DHM prime
  2096. */
  2097. void mbedtls_ssl_conf_dhm_min_bitlen( mbedtls_ssl_config *conf,
  2098. unsigned int bitlen );
  2099. #endif /* MBEDTLS_DHM_C && MBEDTLS_SSL_CLI_C */
  2100. #if defined(MBEDTLS_ECP_C)
  2101. /**
  2102. * \brief Set the allowed curves in order of preference.
  2103. * (Default: all defined curves in order of decreasing size.)
  2104. *
  2105. * On server: this only affects selection of the ECDHE curve;
  2106. * the curves used for ECDH and ECDSA are determined by the
  2107. * list of available certificates instead.
  2108. *
  2109. * On client: this affects the list of curves offered for any
  2110. * use. The server can override our preference order.
  2111. *
  2112. * Both sides: limits the set of curves accepted for use in
  2113. * ECDHE and in the peer's end-entity certificate.
  2114. *
  2115. * \note This has no influence on which curves are allowed inside the
  2116. * certificate chains, see \c mbedtls_ssl_conf_cert_profile()
  2117. * for that. For the end-entity certificate however, the key
  2118. * will be accepted only if it is allowed both by this list
  2119. * and by the cert profile.
  2120. *
  2121. * \note This list should be ordered by decreasing preference
  2122. * (preferred curve first).
  2123. *
  2124. * \param conf SSL configuration
  2125. * \param curves Ordered list of allowed curves,
  2126. * terminated by MBEDTLS_ECP_DP_NONE.
  2127. */
  2128. void mbedtls_ssl_conf_curves( mbedtls_ssl_config *conf,
  2129. const mbedtls_ecp_group_id *curves );
  2130. #endif /* MBEDTLS_ECP_C */
  2131. #if defined(MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED)
  2132. /**
  2133. * \brief Set the allowed hashes for signatures during the handshake.
  2134. * (Default: all SHA-2 hashes, largest first. Also SHA-1 if
  2135. * the compile-time option
  2136. * `MBEDTLS_TLS_DEFAULT_ALLOW_SHA1_IN_KEY_EXCHANGE` is enabled.)
  2137. *
  2138. * \note This only affects which hashes are offered and can be used
  2139. * for signatures during the handshake. Hashes for message
  2140. * authentication and the TLS PRF are controlled by the
  2141. * ciphersuite, see \c mbedtls_ssl_conf_ciphersuites(). Hashes
  2142. * used for certificate signature are controlled by the
  2143. * verification profile, see \c mbedtls_ssl_conf_cert_profile().
  2144. *
  2145. * \note This list should be ordered by decreasing preference
  2146. * (preferred hash first).
  2147. *
  2148. * \param conf SSL configuration
  2149. * \param hashes Ordered list of allowed signature hashes,
  2150. * terminated by \c MBEDTLS_MD_NONE.
  2151. */
  2152. void mbedtls_ssl_conf_sig_hashes( mbedtls_ssl_config *conf,
  2153. const int *hashes );
  2154. #endif /* MBEDTLS_KEY_EXCHANGE__WITH_CERT__ENABLED */
  2155. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2156. /**
  2157. * \brief Set or reset the hostname to check against the received
  2158. * server certificate. It sets the ServerName TLS extension,
  2159. * too, if that extension is enabled. (client-side only)
  2160. *
  2161. * \param ssl SSL context
  2162. * \param hostname the server hostname, may be NULL to clear hostname
  2163. * \note Maximum hostname length MBEDTLS_SSL_MAX_HOST_NAME_LEN.
  2164. *
  2165. * \return 0 if successful, MBEDTLS_ERR_SSL_ALLOC_FAILED on
  2166. * allocation failure, MBEDTLS_ERR_SSL_BAD_INPUT_DATA on
  2167. * too long input hostname.
  2168. *
  2169. * Hostname set to the one provided on success (cleared
  2170. * when NULL). On allocation failure hostname is cleared.
  2171. * On too long input failure, old hostname is unchanged.
  2172. */
  2173. int mbedtls_ssl_set_hostname( mbedtls_ssl_context *ssl, const char *hostname );
  2174. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2175. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION)
  2176. /**
  2177. * \brief Set own certificate and key for the current handshake
  2178. *
  2179. * \note Same as \c mbedtls_ssl_conf_own_cert() but for use within
  2180. * the SNI callback.
  2181. *
  2182. * \param ssl SSL context
  2183. * \param own_cert own public certificate chain
  2184. * \param pk_key own private key
  2185. *
  2186. * \return 0 on success or MBEDTLS_ERR_SSL_ALLOC_FAILED
  2187. */
  2188. int mbedtls_ssl_set_hs_own_cert( mbedtls_ssl_context *ssl,
  2189. mbedtls_x509_crt *own_cert,
  2190. mbedtls_pk_context *pk_key );
  2191. /**
  2192. * \brief Set the data required to verify peer certificate for the
  2193. * current handshake
  2194. *
  2195. * \note Same as \c mbedtls_ssl_conf_ca_chain() but for use within
  2196. * the SNI callback.
  2197. *
  2198. * \param ssl SSL context
  2199. * \param ca_chain trusted CA chain (meaning all fully trusted top-level CAs)
  2200. * \param ca_crl trusted CA CRLs
  2201. */
  2202. void mbedtls_ssl_set_hs_ca_chain( mbedtls_ssl_context *ssl,
  2203. mbedtls_x509_crt *ca_chain,
  2204. mbedtls_x509_crl *ca_crl );
  2205. /**
  2206. * \brief Set authmode for the current handshake.
  2207. *
  2208. * \note Same as \c mbedtls_ssl_conf_authmode() but for use within
  2209. * the SNI callback.
  2210. *
  2211. * \param ssl SSL context
  2212. * \param authmode MBEDTLS_SSL_VERIFY_NONE, MBEDTLS_SSL_VERIFY_OPTIONAL or
  2213. * MBEDTLS_SSL_VERIFY_REQUIRED
  2214. */
  2215. void mbedtls_ssl_set_hs_authmode( mbedtls_ssl_context *ssl,
  2216. int authmode );
  2217. /**
  2218. * \brief Set server side ServerName TLS extension callback
  2219. * (optional, server-side only).
  2220. *
  2221. * If set, the ServerName callback is called whenever the
  2222. * server receives a ServerName TLS extension from the client
  2223. * during a handshake. The ServerName callback has the
  2224. * following parameters: (void *parameter, mbedtls_ssl_context *ssl,
  2225. * const unsigned char *hostname, size_t len). If a suitable
  2226. * certificate is found, the callback must set the
  2227. * certificate(s) and key(s) to use with \c
  2228. * mbedtls_ssl_set_hs_own_cert() (can be called repeatedly),
  2229. * and may optionally adjust the CA and associated CRL with \c
  2230. * mbedtls_ssl_set_hs_ca_chain() as well as the client
  2231. * authentication mode with \c mbedtls_ssl_set_hs_authmode(),
  2232. * then must return 0. If no matching name is found, the
  2233. * callback must either set a default cert, or
  2234. * return non-zero to abort the handshake at this point.
  2235. *
  2236. * \param conf SSL configuration
  2237. * \param f_sni verification function
  2238. * \param p_sni verification parameter
  2239. */
  2240. void mbedtls_ssl_conf_sni( mbedtls_ssl_config *conf,
  2241. int (*f_sni)(void *, mbedtls_ssl_context *, const unsigned char *,
  2242. size_t),
  2243. void *p_sni );
  2244. #endif /* MBEDTLS_SSL_SERVER_NAME_INDICATION */
  2245. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2246. /**
  2247. * \brief Set the EC J-PAKE password for current handshake.
  2248. *
  2249. * \note An internal copy is made, and destroyed as soon as the
  2250. * handshake is completed, or when the SSL context is reset or
  2251. * freed.
  2252. *
  2253. * \note The SSL context needs to be already set up. The right place
  2254. * to call this function is between \c mbedtls_ssl_setup() or
  2255. * \c mbedtls_ssl_reset() and \c mbedtls_ssl_handshake().
  2256. *
  2257. * \param ssl SSL context
  2258. * \param pw EC J-PAKE password (pre-shared secret)
  2259. * \param pw_len length of pw in bytes
  2260. *
  2261. * \return 0 on success, or a negative error code.
  2262. */
  2263. int mbedtls_ssl_set_hs_ecjpake_password( mbedtls_ssl_context *ssl,
  2264. const unsigned char *pw,
  2265. size_t pw_len );
  2266. #endif /*MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED */
  2267. #if defined(MBEDTLS_SSL_ALPN)
  2268. /**
  2269. * \brief Set the supported Application Layer Protocols.
  2270. *
  2271. * \param conf SSL configuration
  2272. * \param protos Pointer to a NULL-terminated list of supported protocols,
  2273. * in decreasing preference order. The pointer to the list is
  2274. * recorded by the library for later reference as required, so
  2275. * the lifetime of the table must be atleast as long as the
  2276. * lifetime of the SSL configuration structure.
  2277. *
  2278. * \return 0 on success, or MBEDTLS_ERR_SSL_BAD_INPUT_DATA.
  2279. */
  2280. int mbedtls_ssl_conf_alpn_protocols( mbedtls_ssl_config *conf, const char **protos );
  2281. /**
  2282. * \brief Get the name of the negotiated Application Layer Protocol.
  2283. * This function should be called after the handshake is
  2284. * completed.
  2285. *
  2286. * \param ssl SSL context
  2287. *
  2288. * \return Protcol name, or NULL if no protocol was negotiated.
  2289. */
  2290. const char *mbedtls_ssl_get_alpn_protocol( const mbedtls_ssl_context *ssl );
  2291. #endif /* MBEDTLS_SSL_ALPN */
  2292. /**
  2293. * \brief Set the maximum supported version sent from the client side
  2294. * and/or accepted at the server side
  2295. * (Default: MBEDTLS_SSL_MAX_MAJOR_VERSION, MBEDTLS_SSL_MAX_MINOR_VERSION)
  2296. *
  2297. * \note This ignores ciphersuites from higher versions.
  2298. *
  2299. * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
  2300. * MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
  2301. *
  2302. * \param conf SSL configuration
  2303. * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
  2304. * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
  2305. * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
  2306. * MBEDTLS_SSL_MINOR_VERSION_3 supported)
  2307. */
  2308. void mbedtls_ssl_conf_max_version( mbedtls_ssl_config *conf, int major, int minor );
  2309. /**
  2310. * \brief Set the minimum accepted SSL/TLS protocol version
  2311. * (Default: TLS 1.0)
  2312. *
  2313. * \note Input outside of the SSL_MAX_XXXXX_VERSION and
  2314. * SSL_MIN_XXXXX_VERSION range is ignored.
  2315. *
  2316. * \note MBEDTLS_SSL_MINOR_VERSION_0 (SSL v3) should be avoided.
  2317. *
  2318. * \note With DTLS, use MBEDTLS_SSL_MINOR_VERSION_2 for DTLS 1.0 and
  2319. * MBEDTLS_SSL_MINOR_VERSION_3 for DTLS 1.2
  2320. *
  2321. * \param conf SSL configuration
  2322. * \param major Major version number (only MBEDTLS_SSL_MAJOR_VERSION_3 supported)
  2323. * \param minor Minor version number (MBEDTLS_SSL_MINOR_VERSION_0,
  2324. * MBEDTLS_SSL_MINOR_VERSION_1 and MBEDTLS_SSL_MINOR_VERSION_2,
  2325. * MBEDTLS_SSL_MINOR_VERSION_3 supported)
  2326. */
  2327. void mbedtls_ssl_conf_min_version( mbedtls_ssl_config *conf, int major, int minor );
  2328. #if defined(MBEDTLS_SSL_FALLBACK_SCSV) && defined(MBEDTLS_SSL_CLI_C)
  2329. /**
  2330. * \brief Set the fallback flag (client-side only).
  2331. * (Default: MBEDTLS_SSL_IS_NOT_FALLBACK).
  2332. *
  2333. * \note Set to MBEDTLS_SSL_IS_FALLBACK when preparing a fallback
  2334. * connection, that is a connection with max_version set to a
  2335. * lower value than the value you're willing to use. Such
  2336. * fallback connections are not recommended but are sometimes
  2337. * necessary to interoperate with buggy (version-intolerant)
  2338. * servers.
  2339. *
  2340. * \warning You should NOT set this to MBEDTLS_SSL_IS_FALLBACK for
  2341. * non-fallback connections! This would appear to work for a
  2342. * while, then cause failures when the server is upgraded to
  2343. * support a newer TLS version.
  2344. *
  2345. * \param conf SSL configuration
  2346. * \param fallback MBEDTLS_SSL_IS_NOT_FALLBACK or MBEDTLS_SSL_IS_FALLBACK
  2347. */
  2348. void mbedtls_ssl_conf_fallback( mbedtls_ssl_config *conf, char fallback );
  2349. #endif /* MBEDTLS_SSL_FALLBACK_SCSV && MBEDTLS_SSL_CLI_C */
  2350. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  2351. /**
  2352. * \brief Enable or disable Encrypt-then-MAC
  2353. * (Default: MBEDTLS_SSL_ETM_ENABLED)
  2354. *
  2355. * \note This should always be enabled, it is a security
  2356. * improvement, and should not cause any interoperability
  2357. * issue (used only if the peer supports it too).
  2358. *
  2359. * \param conf SSL configuration
  2360. * \param etm MBEDTLS_SSL_ETM_ENABLED or MBEDTLS_SSL_ETM_DISABLED
  2361. */
  2362. void mbedtls_ssl_conf_encrypt_then_mac( mbedtls_ssl_config *conf, char etm );
  2363. #endif /* MBEDTLS_SSL_ENCRYPT_THEN_MAC */
  2364. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  2365. /**
  2366. * \brief Enable or disable Extended Master Secret negotiation.
  2367. * (Default: MBEDTLS_SSL_EXTENDED_MS_ENABLED)
  2368. *
  2369. * \note This should always be enabled, it is a security fix to the
  2370. * protocol, and should not cause any interoperability issue
  2371. * (used only if the peer supports it too).
  2372. *
  2373. * \param conf SSL configuration
  2374. * \param ems MBEDTLS_SSL_EXTENDED_MS_ENABLED or MBEDTLS_SSL_EXTENDED_MS_DISABLED
  2375. */
  2376. void mbedtls_ssl_conf_extended_master_secret( mbedtls_ssl_config *conf, char ems );
  2377. #endif /* MBEDTLS_SSL_EXTENDED_MASTER_SECRET */
  2378. #if defined(MBEDTLS_ARC4_C)
  2379. /**
  2380. * \brief Disable or enable support for RC4
  2381. * (Default: MBEDTLS_SSL_ARC4_DISABLED)
  2382. *
  2383. * \warning Use of RC4 in DTLS/TLS has been prohibited by RFC 7465
  2384. * for security reasons. Use at your own risk.
  2385. *
  2386. * \note This function is deprecated and will likely be removed in
  2387. * a future version of the library.
  2388. * RC4 is disabled by default at compile time and needs to be
  2389. * actively enabled for use with legacy systems.
  2390. *
  2391. * \param conf SSL configuration
  2392. * \param arc4 MBEDTLS_SSL_ARC4_ENABLED or MBEDTLS_SSL_ARC4_DISABLED
  2393. */
  2394. void mbedtls_ssl_conf_arc4_support( mbedtls_ssl_config *conf, char arc4 );
  2395. #endif /* MBEDTLS_ARC4_C */
  2396. #if defined(MBEDTLS_SSL_SRV_C)
  2397. /**
  2398. * \brief Whether to send a list of acceptable CAs in
  2399. * CertificateRequest messages.
  2400. * (Default: do send)
  2401. *
  2402. * \param conf SSL configuration
  2403. * \param cert_req_ca_list MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED or
  2404. * MBEDTLS_SSL_CERT_REQ_CA_LIST_DISABLED
  2405. */
  2406. void mbedtls_ssl_conf_cert_req_ca_list( mbedtls_ssl_config *conf,
  2407. char cert_req_ca_list );
  2408. #endif /* MBEDTLS_SSL_SRV_C */
  2409. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2410. /**
  2411. * \brief Set the maximum fragment length to emit and/or negotiate.
  2412. * (Typical: the smaller of #MBEDTLS_SSL_IN_CONTENT_LEN and
  2413. * #MBEDTLS_SSL_OUT_CONTENT_LEN, usually `2^14` bytes)
  2414. * (Server: set maximum fragment length to emit,
  2415. * usually negotiated by the client during handshake)
  2416. * (Client: set maximum fragment length to emit *and*
  2417. * negotiate with the server during handshake)
  2418. * (Default: #MBEDTLS_SSL_MAX_FRAG_LEN_NONE)
  2419. *
  2420. * \note On the client side, the maximum fragment length extension
  2421. * *will not* be used, unless the maximum fragment length has
  2422. * been set via this function to a value different than
  2423. * #MBEDTLS_SSL_MAX_FRAG_LEN_NONE.
  2424. *
  2425. * \note This sets the maximum length for a record's payload,
  2426. * excluding record overhead that will be added to it, see
  2427. * \c mbedtls_ssl_get_record_expansion().
  2428. *
  2429. * \note With TLS, this currently only affects ApplicationData (sent
  2430. * with \c mbedtls_ssl_read()), not handshake messages.
  2431. * With DTLS, this affects both ApplicationData and handshake.
  2432. *
  2433. * \note For DTLS, it is also possible to set a limit for the total
  2434. * size of daragrams passed to the transport layer, including
  2435. * record overhead, see \c mbedtls_ssl_set_mtu().
  2436. *
  2437. * \param conf SSL configuration
  2438. * \param mfl_code Code for maximum fragment length (allowed values:
  2439. * MBEDTLS_SSL_MAX_FRAG_LEN_512, MBEDTLS_SSL_MAX_FRAG_LEN_1024,
  2440. * MBEDTLS_SSL_MAX_FRAG_LEN_2048, MBEDTLS_SSL_MAX_FRAG_LEN_4096)
  2441. *
  2442. * \return 0 if successful or MBEDTLS_ERR_SSL_BAD_INPUT_DATA
  2443. */
  2444. int mbedtls_ssl_conf_max_frag_len( mbedtls_ssl_config *conf, unsigned char mfl_code );
  2445. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  2446. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  2447. /**
  2448. * \brief Activate negotiation of truncated HMAC
  2449. * (Default: MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
  2450. *
  2451. * \param conf SSL configuration
  2452. * \param truncate Enable or disable (MBEDTLS_SSL_TRUNC_HMAC_ENABLED or
  2453. * MBEDTLS_SSL_TRUNC_HMAC_DISABLED)
  2454. */
  2455. void mbedtls_ssl_conf_truncated_hmac( mbedtls_ssl_config *conf, int truncate );
  2456. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  2457. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  2458. /**
  2459. * \brief Enable / Disable 1/n-1 record splitting
  2460. * (Default: MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED)
  2461. *
  2462. * \note Only affects SSLv3 and TLS 1.0, not higher versions.
  2463. * Does not affect non-CBC ciphersuites in any version.
  2464. *
  2465. * \param conf SSL configuration
  2466. * \param split MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED or
  2467. * MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED
  2468. */
  2469. void mbedtls_ssl_conf_cbc_record_splitting( mbedtls_ssl_config *conf, char split );
  2470. #endif /* MBEDTLS_SSL_CBC_RECORD_SPLITTING */
  2471. #if defined(MBEDTLS_SSL_SESSION_TICKETS) && defined(MBEDTLS_SSL_CLI_C)
  2472. /**
  2473. * \brief Enable / Disable session tickets (client only).
  2474. * (Default: MBEDTLS_SSL_SESSION_TICKETS_ENABLED.)
  2475. *
  2476. * \note On server, use \c mbedtls_ssl_conf_session_tickets_cb().
  2477. *
  2478. * \param conf SSL configuration
  2479. * \param use_tickets Enable or disable (MBEDTLS_SSL_SESSION_TICKETS_ENABLED or
  2480. * MBEDTLS_SSL_SESSION_TICKETS_DISABLED)
  2481. */
  2482. void mbedtls_ssl_conf_session_tickets( mbedtls_ssl_config *conf, int use_tickets );
  2483. #endif /* MBEDTLS_SSL_SESSION_TICKETS && MBEDTLS_SSL_CLI_C */
  2484. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2485. /**
  2486. * \brief Enable / Disable renegotiation support for connection when
  2487. * initiated by peer
  2488. * (Default: MBEDTLS_SSL_RENEGOTIATION_DISABLED)
  2489. *
  2490. * \warning It is recommended to always disable renegotation unless you
  2491. * know you need it and you know what you're doing. In the
  2492. * past, there have been several issues associated with
  2493. * renegotiation or a poor understanding of its properties.
  2494. *
  2495. * \note Server-side, enabling renegotiation also makes the server
  2496. * susceptible to a resource DoS by a malicious client.
  2497. *
  2498. * \param conf SSL configuration
  2499. * \param renegotiation Enable or disable (MBEDTLS_SSL_RENEGOTIATION_ENABLED or
  2500. * MBEDTLS_SSL_RENEGOTIATION_DISABLED)
  2501. */
  2502. void mbedtls_ssl_conf_renegotiation( mbedtls_ssl_config *conf, int renegotiation );
  2503. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  2504. /**
  2505. * \brief Prevent or allow legacy renegotiation.
  2506. * (Default: MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION)
  2507. *
  2508. * MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION allows connections to
  2509. * be established even if the peer does not support
  2510. * secure renegotiation, but does not allow renegotiation
  2511. * to take place if not secure.
  2512. * (Interoperable and secure option)
  2513. *
  2514. * MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION allows renegotiations
  2515. * with non-upgraded peers. Allowing legacy renegotiation
  2516. * makes the connection vulnerable to specific man in the
  2517. * middle attacks. (See RFC 5746)
  2518. * (Most interoperable and least secure option)
  2519. *
  2520. * MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE breaks off connections
  2521. * if peer does not support secure renegotiation. Results
  2522. * in interoperability issues with non-upgraded peers
  2523. * that do not support renegotiation altogether.
  2524. * (Most secure option, interoperability issues)
  2525. *
  2526. * \param conf SSL configuration
  2527. * \param allow_legacy Prevent or allow (SSL_NO_LEGACY_RENEGOTIATION,
  2528. * SSL_ALLOW_LEGACY_RENEGOTIATION or
  2529. * MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE)
  2530. */
  2531. void mbedtls_ssl_conf_legacy_renegotiation( mbedtls_ssl_config *conf, int allow_legacy );
  2532. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2533. /**
  2534. * \brief Enforce renegotiation requests.
  2535. * (Default: enforced, max_records = 16)
  2536. *
  2537. * When we request a renegotiation, the peer can comply or
  2538. * ignore the request. This function allows us to decide
  2539. * whether to enforce our renegotiation requests by closing
  2540. * the connection if the peer doesn't comply.
  2541. *
  2542. * However, records could already be in transit from the peer
  2543. * when the request is emitted. In order to increase
  2544. * reliability, we can accept a number of records before the
  2545. * expected handshake records.
  2546. *
  2547. * The optimal value is highly dependent on the specific usage
  2548. * scenario.
  2549. *
  2550. * \note With DTLS and server-initiated renegotiation, the
  2551. * HelloRequest is retransmited every time mbedtls_ssl_read() times
  2552. * out or receives Application Data, until:
  2553. * - max_records records have beens seen, if it is >= 0, or
  2554. * - the number of retransmits that would happen during an
  2555. * actual handshake has been reached.
  2556. * Please remember the request might be lost a few times
  2557. * if you consider setting max_records to a really low value.
  2558. *
  2559. * \warning On client, the grace period can only happen during
  2560. * mbedtls_ssl_read(), as opposed to mbedtls_ssl_write() and mbedtls_ssl_renegotiate()
  2561. * which always behave as if max_record was 0. The reason is,
  2562. * if we receive application data from the server, we need a
  2563. * place to write it, which only happens during mbedtls_ssl_read().
  2564. *
  2565. * \param conf SSL configuration
  2566. * \param max_records Use MBEDTLS_SSL_RENEGOTIATION_NOT_ENFORCED if you don't want to
  2567. * enforce renegotiation, or a non-negative value to enforce
  2568. * it but allow for a grace period of max_records records.
  2569. */
  2570. void mbedtls_ssl_conf_renegotiation_enforced( mbedtls_ssl_config *conf, int max_records );
  2571. /**
  2572. * \brief Set record counter threshold for periodic renegotiation.
  2573. * (Default: 2^48 - 1)
  2574. *
  2575. * Renegotiation is automatically triggered when a record
  2576. * counter (outgoing or ingoing) crosses the defined
  2577. * threshold. The default value is meant to prevent the
  2578. * connection from being closed when the counter is about to
  2579. * reached its maximal value (it is not allowed to wrap).
  2580. *
  2581. * Lower values can be used to enforce policies such as "keys
  2582. * must be refreshed every N packets with cipher X".
  2583. *
  2584. * The renegotiation period can be disabled by setting
  2585. * conf->disable_renegotiation to
  2586. * MBEDTLS_SSL_RENEGOTIATION_DISABLED.
  2587. *
  2588. * \note When the configured transport is
  2589. * MBEDTLS_SSL_TRANSPORT_DATAGRAM the maximum renegotiation
  2590. * period is 2^48 - 1, and for MBEDTLS_SSL_TRANSPORT_STREAM,
  2591. * the maximum renegotiation period is 2^64 - 1.
  2592. *
  2593. * \param conf SSL configuration
  2594. * \param period The threshold value: a big-endian 64-bit number.
  2595. */
  2596. void mbedtls_ssl_conf_renegotiation_period( mbedtls_ssl_config *conf,
  2597. const unsigned char period[8] );
  2598. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  2599. /**
  2600. * \brief Check if there is data already read from the
  2601. * underlying transport but not yet processed.
  2602. *
  2603. * \param ssl SSL context
  2604. *
  2605. * \return 0 if nothing's pending, 1 otherwise.
  2606. *
  2607. * \note This is different in purpose and behaviour from
  2608. * \c mbedtls_ssl_get_bytes_avail in that it considers
  2609. * any kind of unprocessed data, not only unread
  2610. * application data. If \c mbedtls_ssl_get_bytes
  2611. * returns a non-zero value, this function will
  2612. * also signal pending data, but the converse does
  2613. * not hold. For example, in DTLS there might be
  2614. * further records waiting to be processed from
  2615. * the current underlying transport's datagram.
  2616. *
  2617. * \note If this function returns 1 (data pending), this
  2618. * does not imply that a subsequent call to
  2619. * \c mbedtls_ssl_read will provide any data;
  2620. * e.g., the unprocessed data might turn out
  2621. * to be an alert or a handshake message.
  2622. *
  2623. * \note This function is useful in the following situation:
  2624. * If the SSL/TLS module successfully returns from an
  2625. * operation - e.g. a handshake or an application record
  2626. * read - and you're awaiting incoming data next, you
  2627. * must not immediately idle on the underlying transport
  2628. * to have data ready, but you need to check the value
  2629. * of this function first. The reason is that the desired
  2630. * data might already be read but not yet processed.
  2631. * If, in contrast, a previous call to the SSL/TLS module
  2632. * returned MBEDTLS_ERR_SSL_WANT_READ, it is not necessary
  2633. * to call this function, as the latter error code entails
  2634. * that all internal data has been processed.
  2635. *
  2636. */
  2637. int mbedtls_ssl_check_pending( const mbedtls_ssl_context *ssl );
  2638. /**
  2639. * \brief Return the number of application data bytes
  2640. * remaining to be read from the current record.
  2641. *
  2642. * \param ssl SSL context
  2643. *
  2644. * \return How many bytes are available in the application
  2645. * data record read buffer.
  2646. *
  2647. * \note When working over a datagram transport, this is
  2648. * useful to detect the current datagram's boundary
  2649. * in case \c mbedtls_ssl_read has written the maximal
  2650. * amount of data fitting into the input buffer.
  2651. *
  2652. */
  2653. size_t mbedtls_ssl_get_bytes_avail( const mbedtls_ssl_context *ssl );
  2654. /**
  2655. * \brief Return the result of the certificate verification
  2656. *
  2657. * \param ssl The SSL context to use.
  2658. *
  2659. * \return \c 0 if the certificate verification was successful.
  2660. * \return \c -1u if the result is not available. This may happen
  2661. * e.g. if the handshake aborts early, or a verification
  2662. * callback returned a fatal error.
  2663. * \return A bitwise combination of \c MBEDTLS_X509_BADCERT_XXX
  2664. * and \c MBEDTLS_X509_BADCRL_XXX failure flags; see x509.h.
  2665. */
  2666. uint32_t mbedtls_ssl_get_verify_result( const mbedtls_ssl_context *ssl );
  2667. /**
  2668. * \brief Return the name of the current ciphersuite
  2669. *
  2670. * \param ssl SSL context
  2671. *
  2672. * \return a string containing the ciphersuite name
  2673. */
  2674. const char *mbedtls_ssl_get_ciphersuite( const mbedtls_ssl_context *ssl );
  2675. /**
  2676. * \brief Return the current SSL version (SSLv3/TLSv1/etc)
  2677. *
  2678. * \param ssl SSL context
  2679. *
  2680. * \return a string containing the SSL version
  2681. */
  2682. const char *mbedtls_ssl_get_version( const mbedtls_ssl_context *ssl );
  2683. /**
  2684. * \brief Return the (maximum) number of bytes added by the record
  2685. * layer: header + encryption/MAC overhead (inc. padding)
  2686. *
  2687. * \note This function is not available (always returns an error)
  2688. * when record compression is enabled.
  2689. *
  2690. * \param ssl SSL context
  2691. *
  2692. * \return Current maximum record expansion in bytes, or
  2693. * MBEDTLS_ERR_SSL_FEATURE_UNAVAILABLE if compression is
  2694. * enabled, which makes expansion much less predictable
  2695. */
  2696. int mbedtls_ssl_get_record_expansion( const mbedtls_ssl_context *ssl );
  2697. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2698. /**
  2699. * \brief Return the maximum fragment length (payload, in bytes).
  2700. * This is the value negotiated with peer if any,
  2701. * or the locally configured value.
  2702. *
  2703. * \sa mbedtls_ssl_conf_max_frag_len()
  2704. * \sa mbedtls_ssl_get_max_record_payload()
  2705. *
  2706. * \param ssl SSL context
  2707. *
  2708. * \return Current maximum fragment length.
  2709. */
  2710. size_t mbedtls_ssl_get_max_frag_len( const mbedtls_ssl_context *ssl );
  2711. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  2712. /**
  2713. * \brief Return the current maximum outgoing record payload in bytes.
  2714. * This takes into account the config.h setting \c
  2715. * MBEDTLS_SSL_OUT_CONTENT_LEN, the configured and negotiated
  2716. * max fragment length extension if used, and for DTLS the
  2717. * path MTU as configured and current record expansion.
  2718. *
  2719. * \note With DTLS, \c mbedtls_ssl_write() will return an error if
  2720. * called with a larger length value.
  2721. * With TLS, \c mbedtls_ssl_write() will fragment the input if
  2722. * necessary and return the number of bytes written; it is up
  2723. * to the caller to call \c mbedtls_ssl_write() again in
  2724. * order to send the remaining bytes if any.
  2725. *
  2726. * \note This function is not available (always returns an error)
  2727. * when record compression is enabled.
  2728. *
  2729. * \sa mbedtls_ssl_set_mtu()
  2730. * \sa mbedtls_ssl_get_max_frag_len()
  2731. * \sa mbedtls_ssl_get_record_expansion()
  2732. *
  2733. * \param ssl SSL context
  2734. *
  2735. * \return Current maximum payload for an outgoing record,
  2736. * or a negative error code.
  2737. */
  2738. int mbedtls_ssl_get_max_out_record_payload( const mbedtls_ssl_context *ssl );
  2739. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2740. /**
  2741. * \brief Return the peer certificate from the current connection
  2742. *
  2743. * Note: Can be NULL in case no certificate was sent during
  2744. * the handshake. Different calls for the same connection can
  2745. * return the same or different pointers for the same
  2746. * certificate and even a different certificate altogether.
  2747. * The peer cert CAN change in a single connection if
  2748. * renegotiation is performed.
  2749. *
  2750. * \param ssl SSL context
  2751. *
  2752. * \return the current peer certificate
  2753. */
  2754. const mbedtls_x509_crt *mbedtls_ssl_get_peer_cert( const mbedtls_ssl_context *ssl );
  2755. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2756. #if defined(MBEDTLS_SSL_CLI_C)
  2757. /**
  2758. * \brief Save session in order to resume it later (client-side only)
  2759. * Session data is copied to presented session structure.
  2760. *
  2761. *
  2762. * \param ssl SSL context
  2763. * \param session session context
  2764. *
  2765. * \return 0 if successful,
  2766. * MBEDTLS_ERR_SSL_ALLOC_FAILED if memory allocation failed,
  2767. * MBEDTLS_ERR_SSL_BAD_INPUT_DATA if used server-side or
  2768. * arguments are otherwise invalid.
  2769. *
  2770. * \note Only the server certificate is copied, and not the full chain,
  2771. * so you should not attempt to validate the certificate again
  2772. * by calling \c mbedtls_x509_crt_verify() on it.
  2773. * Instead, you should use the results from the verification
  2774. * in the original handshake by calling \c mbedtls_ssl_get_verify_result()
  2775. * after loading the session again into a new SSL context
  2776. * using \c mbedtls_ssl_set_session().
  2777. *
  2778. * \note Once the session object is not needed anymore, you should
  2779. * free it by calling \c mbedtls_ssl_session_free().
  2780. *
  2781. * \sa mbedtls_ssl_set_session()
  2782. */
  2783. int mbedtls_ssl_get_session( const mbedtls_ssl_context *ssl, mbedtls_ssl_session *session );
  2784. #endif /* MBEDTLS_SSL_CLI_C */
  2785. /**
  2786. * \brief Perform the SSL handshake
  2787. *
  2788. * \param ssl SSL context
  2789. *
  2790. * \return \c 0 if successful.
  2791. * \return #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE
  2792. * if the handshake is incomplete and waiting for data to
  2793. * be available for reading from or writing to the underlying
  2794. * transport - in this case you must call this function again
  2795. * when the underlying transport is ready for the operation.
  2796. * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if an asynchronous
  2797. * operation is in progress (see
  2798. * mbedtls_ssl_conf_async_private_cb()) - in this case you
  2799. * must call this function again when the operation is ready.
  2800. * \return #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS if a cryptographic
  2801. * operation is in progress (see mbedtls_ecp_set_max_ops()) -
  2802. * in this case you must call this function again to complete
  2803. * the handshake when you're done attending other tasks.
  2804. * \return #MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED if DTLS is in use
  2805. * and the client did not demonstrate reachability yet - in
  2806. * this case you must stop using the context (see below).
  2807. * \return Another SSL error code - in this case you must stop using
  2808. * the context (see below).
  2809. *
  2810. * \warning If this function returns something other than
  2811. * \c 0,
  2812. * #MBEDTLS_ERR_SSL_WANT_READ,
  2813. * #MBEDTLS_ERR_SSL_WANT_WRITE,
  2814. * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
  2815. * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS,
  2816. * you must stop using the SSL context for reading or writing,
  2817. * and either free it or call \c mbedtls_ssl_session_reset()
  2818. * on it before re-using it for a new connection; the current
  2819. * connection must be closed.
  2820. *
  2821. * \note If DTLS is in use, then you may choose to handle
  2822. * #MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED specially for logging
  2823. * purposes, as it is an expected return value rather than an
  2824. * actual error, but you still need to reset/free the context.
  2825. *
  2826. * \note Remarks regarding event-driven DTLS:
  2827. * If the function returns #MBEDTLS_ERR_SSL_WANT_READ, no datagram
  2828. * from the underlying transport layer is currently being processed,
  2829. * and it is safe to idle until the timer or the underlying transport
  2830. * signal a new event. This is not true for a successful handshake,
  2831. * in which case the datagram of the underlying transport that is
  2832. * currently being processed might or might not contain further
  2833. * DTLS records.
  2834. */
  2835. int mbedtls_ssl_handshake( mbedtls_ssl_context *ssl );
  2836. /**
  2837. * \brief Perform a single step of the SSL handshake
  2838. *
  2839. * \note The state of the context (ssl->state) will be at
  2840. * the next state after this function returns \c 0. Do not
  2841. * call this function if state is MBEDTLS_SSL_HANDSHAKE_OVER.
  2842. *
  2843. * \param ssl SSL context
  2844. *
  2845. * \return See mbedtls_ssl_handshake().
  2846. *
  2847. * \warning If this function returns something other than \c 0,
  2848. * #MBEDTLS_ERR_SSL_WANT_READ, #MBEDTLS_ERR_SSL_WANT_WRITE,
  2849. * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
  2850. * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS, you must stop using
  2851. * the SSL context for reading or writing, and either free it
  2852. * or call \c mbedtls_ssl_session_reset() on it before
  2853. * re-using it for a new connection; the current connection
  2854. * must be closed.
  2855. */
  2856. int mbedtls_ssl_handshake_step( mbedtls_ssl_context *ssl );
  2857. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2858. /**
  2859. * \brief Initiate an SSL renegotiation on the running connection.
  2860. * Client: perform the renegotiation right now.
  2861. * Server: request renegotiation, which will be performed
  2862. * during the next call to mbedtls_ssl_read() if honored by
  2863. * client.
  2864. *
  2865. * \param ssl SSL context
  2866. *
  2867. * \return 0 if successful, or any mbedtls_ssl_handshake() return
  2868. * value except #MBEDTLS_ERR_SSL_CLIENT_RECONNECT that can't
  2869. * happen during a renegotiation.
  2870. *
  2871. * \warning If this function returns something other than \c 0,
  2872. * #MBEDTLS_ERR_SSL_WANT_READ, #MBEDTLS_ERR_SSL_WANT_WRITE,
  2873. * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
  2874. * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS, you must stop using
  2875. * the SSL context for reading or writing, and either free it
  2876. * or call \c mbedtls_ssl_session_reset() on it before
  2877. * re-using it for a new connection; the current connection
  2878. * must be closed.
  2879. *
  2880. */
  2881. int mbedtls_ssl_renegotiate( mbedtls_ssl_context *ssl );
  2882. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  2883. /**
  2884. * \brief Read at most 'len' application data bytes
  2885. *
  2886. * \param ssl SSL context
  2887. * \param buf buffer that will hold the data
  2888. * \param len maximum number of bytes to read
  2889. *
  2890. * \return The (positive) number of bytes read if successful.
  2891. * \return \c 0 if the read end of the underlying transport was closed
  2892. * - in this case you must stop using the context (see below).
  2893. * \return #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE
  2894. * if the handshake is incomplete and waiting for data to
  2895. * be available for reading from or writing to the underlying
  2896. * transport - in this case you must call this function again
  2897. * when the underlying transport is ready for the operation.
  2898. * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if an asynchronous
  2899. * operation is in progress (see
  2900. * mbedtls_ssl_conf_async_private_cb()) - in this case you
  2901. * must call this function again when the operation is ready.
  2902. * \return #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS if a cryptographic
  2903. * operation is in progress (see mbedtls_ecp_set_max_ops()) -
  2904. * in this case you must call this function again to complete
  2905. * the handshake when you're done attending other tasks.
  2906. * \return #MBEDTLS_ERR_SSL_CLIENT_RECONNECT if we're at the server
  2907. * side of a DTLS connection and the client is initiating a
  2908. * new connection using the same source port. See below.
  2909. * \return Another SSL error code - in this case you must stop using
  2910. * the context (see below).
  2911. *
  2912. * \warning If this function returns something other than
  2913. * a positive value,
  2914. * #MBEDTLS_ERR_SSL_WANT_READ,
  2915. * #MBEDTLS_ERR_SSL_WANT_WRITE,
  2916. * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS,
  2917. * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS or
  2918. * #MBEDTLS_ERR_SSL_CLIENT_RECONNECT,
  2919. * you must stop using the SSL context for reading or writing,
  2920. * and either free it or call \c mbedtls_ssl_session_reset()
  2921. * on it before re-using it for a new connection; the current
  2922. * connection must be closed.
  2923. *
  2924. * \note When this function returns #MBEDTLS_ERR_SSL_CLIENT_RECONNECT
  2925. * (which can only happen server-side), it means that a client
  2926. * is initiating a new connection using the same source port.
  2927. * You can either treat that as a connection close and wait
  2928. * for the client to resend a ClientHello, or directly
  2929. * continue with \c mbedtls_ssl_handshake() with the same
  2930. * context (as it has been reset internally). Either way, you
  2931. * must make sure this is seen by the application as a new
  2932. * connection: application state, if any, should be reset, and
  2933. * most importantly the identity of the client must be checked
  2934. * again. WARNING: not validating the identity of the client
  2935. * again, or not transmitting the new identity to the
  2936. * application layer, would allow authentication bypass!
  2937. *
  2938. * \note Remarks regarding event-driven DTLS:
  2939. * - If the function returns #MBEDTLS_ERR_SSL_WANT_READ, no datagram
  2940. * from the underlying transport layer is currently being processed,
  2941. * and it is safe to idle until the timer or the underlying transport
  2942. * signal a new event.
  2943. * - This function may return MBEDTLS_ERR_SSL_WANT_READ even if data was
  2944. * initially available on the underlying transport, as this data may have
  2945. * been only e.g. duplicated messages or a renegotiation request.
  2946. * Therefore, you must be prepared to receive MBEDTLS_ERR_SSL_WANT_READ even
  2947. * when reacting to an incoming-data event from the underlying transport.
  2948. * - On success, the datagram of the underlying transport that is currently
  2949. * being processed may contain further DTLS records. You should call
  2950. * \c mbedtls_ssl_check_pending to check for remaining records.
  2951. *
  2952. */
  2953. int mbedtls_ssl_read( mbedtls_ssl_context *ssl, unsigned char *buf, size_t len );
  2954. /**
  2955. * \brief Try to write exactly 'len' application data bytes
  2956. *
  2957. * \warning This function will do partial writes in some cases. If the
  2958. * return value is non-negative but less than length, the
  2959. * function must be called again with updated arguments:
  2960. * buf + ret, len - ret (if ret is the return value) until
  2961. * it returns a value equal to the last 'len' argument.
  2962. *
  2963. * \param ssl SSL context
  2964. * \param buf buffer holding the data
  2965. * \param len how many bytes must be written
  2966. *
  2967. * \return The (non-negative) number of bytes actually written if
  2968. * successful (may be less than \p len).
  2969. * \return #MBEDTLS_ERR_SSL_WANT_READ or #MBEDTLS_ERR_SSL_WANT_WRITE
  2970. * if the handshake is incomplete and waiting for data to
  2971. * be available for reading from or writing to the underlying
  2972. * transport - in this case you must call this function again
  2973. * when the underlying transport is ready for the operation.
  2974. * \return #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS if an asynchronous
  2975. * operation is in progress (see
  2976. * mbedtls_ssl_conf_async_private_cb()) - in this case you
  2977. * must call this function again when the operation is ready.
  2978. * \return #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS if a cryptographic
  2979. * operation is in progress (see mbedtls_ecp_set_max_ops()) -
  2980. * in this case you must call this function again to complete
  2981. * the handshake when you're done attending other tasks.
  2982. * \return Another SSL error code - in this case you must stop using
  2983. * the context (see below).
  2984. *
  2985. * \warning If this function returns something other than
  2986. * a non-negative value,
  2987. * #MBEDTLS_ERR_SSL_WANT_READ,
  2988. * #MBEDTLS_ERR_SSL_WANT_WRITE,
  2989. * #MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS or
  2990. * #MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS,
  2991. * you must stop using the SSL context for reading or writing,
  2992. * and either free it or call \c mbedtls_ssl_session_reset()
  2993. * on it before re-using it for a new connection; the current
  2994. * connection must be closed.
  2995. *
  2996. * \note When this function returns #MBEDTLS_ERR_SSL_WANT_WRITE/READ,
  2997. * it must be called later with the *same* arguments,
  2998. * until it returns a value greater that or equal to 0. When
  2999. * the function returns #MBEDTLS_ERR_SSL_WANT_WRITE there may be
  3000. * some partial data in the output buffer, however this is not
  3001. * yet sent.
  3002. *
  3003. * \note If the requested length is greater than the maximum
  3004. * fragment length (either the built-in limit or the one set
  3005. * or negotiated with the peer), then:
  3006. * - with TLS, less bytes than requested are written.
  3007. * - with DTLS, MBEDTLS_ERR_SSL_BAD_INPUT_DATA is returned.
  3008. * \c mbedtls_ssl_get_max_frag_len() may be used to query the
  3009. * active maximum fragment length.
  3010. *
  3011. * \note Attempting to write 0 bytes will result in an empty TLS
  3012. * application record being sent.
  3013. */
  3014. int mbedtls_ssl_write( mbedtls_ssl_context *ssl, const unsigned char *buf, size_t len );
  3015. /**
  3016. * \brief Send an alert message
  3017. *
  3018. * \param ssl SSL context
  3019. * \param level The alert level of the message
  3020. * (MBEDTLS_SSL_ALERT_LEVEL_WARNING or MBEDTLS_SSL_ALERT_LEVEL_FATAL)
  3021. * \param message The alert message (SSL_ALERT_MSG_*)
  3022. *
  3023. * \return 0 if successful, or a specific SSL error code.
  3024. *
  3025. * \note If this function returns something other than 0 or
  3026. * MBEDTLS_ERR_SSL_WANT_READ/WRITE, you must stop using
  3027. * the SSL context for reading or writing, and either free it or
  3028. * call \c mbedtls_ssl_session_reset() on it before re-using it
  3029. * for a new connection; the current connection must be closed.
  3030. */
  3031. int mbedtls_ssl_send_alert_message( mbedtls_ssl_context *ssl,
  3032. unsigned char level,
  3033. unsigned char message );
  3034. /**
  3035. * \brief Notify the peer that the connection is being closed
  3036. *
  3037. * \param ssl SSL context
  3038. *
  3039. * \return 0 if successful, or a specific SSL error code.
  3040. *
  3041. * \note If this function returns something other than 0 or
  3042. * MBEDTLS_ERR_SSL_WANT_READ/WRITE, you must stop using
  3043. * the SSL context for reading or writing, and either free it or
  3044. * call \c mbedtls_ssl_session_reset() on it before re-using it
  3045. * for a new connection; the current connection must be closed.
  3046. */
  3047. int mbedtls_ssl_close_notify( mbedtls_ssl_context *ssl );
  3048. /**
  3049. * \brief Free referenced items in an SSL context and clear memory
  3050. *
  3051. * \param ssl SSL context
  3052. */
  3053. void mbedtls_ssl_free( mbedtls_ssl_context *ssl );
  3054. /**
  3055. * \brief Initialize an SSL configuration context
  3056. * Just makes the context ready for
  3057. * mbedtls_ssl_config_defaults() or mbedtls_ssl_config_free().
  3058. *
  3059. * \note You need to call mbedtls_ssl_config_defaults() unless you
  3060. * manually set all of the relevant fields yourself.
  3061. *
  3062. * \param conf SSL configuration context
  3063. */
  3064. void mbedtls_ssl_config_init( mbedtls_ssl_config *conf );
  3065. /**
  3066. * \brief Load reasonnable default SSL configuration values.
  3067. * (You need to call mbedtls_ssl_config_init() first.)
  3068. *
  3069. * \param conf SSL configuration context
  3070. * \param endpoint MBEDTLS_SSL_IS_CLIENT or MBEDTLS_SSL_IS_SERVER
  3071. * \param transport MBEDTLS_SSL_TRANSPORT_STREAM for TLS, or
  3072. * MBEDTLS_SSL_TRANSPORT_DATAGRAM for DTLS
  3073. * \param preset a MBEDTLS_SSL_PRESET_XXX value
  3074. *
  3075. * \note See \c mbedtls_ssl_conf_transport() for notes on DTLS.
  3076. *
  3077. * \return 0 if successful, or
  3078. * MBEDTLS_ERR_XXX_ALLOC_FAILED on memory allocation error.
  3079. */
  3080. int mbedtls_ssl_config_defaults( mbedtls_ssl_config *conf,
  3081. int endpoint, int transport, int preset );
  3082. /**
  3083. * \brief Free an SSL configuration context
  3084. *
  3085. * \param conf SSL configuration context
  3086. */
  3087. void mbedtls_ssl_config_free( mbedtls_ssl_config *conf );
  3088. /**
  3089. * \brief Initialize SSL session structure
  3090. *
  3091. * \param session SSL session
  3092. */
  3093. void mbedtls_ssl_session_init( mbedtls_ssl_session *session );
  3094. /**
  3095. * \brief Free referenced items in an SSL session including the
  3096. * peer certificate and clear memory
  3097. *
  3098. * \note A session object can be freed even if the SSL context
  3099. * that was used to retrieve the session is still in use.
  3100. *
  3101. * \param session SSL session
  3102. */
  3103. void mbedtls_ssl_session_free( mbedtls_ssl_session *session );
  3104. #ifdef __cplusplus
  3105. }
  3106. #endif
  3107. #endif /* ssl.h */