sectransp.c 126 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012 - 2022, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. * Copyright (C) 2012 - 2017, Nick Zitzmann, <nickzman@gmail.com>.
  10. *
  11. * This software is licensed as described in the file COPYING, which
  12. * you should have received as part of this distribution. The terms
  13. * are also available at https://curl.se/docs/copyright.html.
  14. *
  15. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  16. * copies of the Software, and permit persons to whom the Software is
  17. * furnished to do so, under the terms of the COPYING file.
  18. *
  19. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  20. * KIND, either express or implied.
  21. *
  22. * SPDX-License-Identifier: curl
  23. *
  24. ***************************************************************************/
  25. /*
  26. * Source file for all iOS and macOS SecureTransport-specific code for the
  27. * TLS/SSL layer. No code but vtls.c should ever call or use these functions.
  28. */
  29. #include "curl_setup.h"
  30. #include "urldata.h" /* for the Curl_easy definition */
  31. #include "curl_base64.h"
  32. #include "strtok.h"
  33. #include "multiif.h"
  34. #include "strcase.h"
  35. #include "x509asn1.h"
  36. #include "strerror.h"
  37. #ifdef USE_SECTRANSP
  38. #ifdef __clang__
  39. #pragma clang diagnostic push
  40. #pragma clang diagnostic ignored "-Wtautological-pointer-compare"
  41. #endif /* __clang__ */
  42. #include <limits.h>
  43. #include <Security/Security.h>
  44. /* For some reason, when building for iOS, the omnibus header above does
  45. * not include SecureTransport.h as of iOS SDK 5.1. */
  46. #include <Security/SecureTransport.h>
  47. #include <CoreFoundation/CoreFoundation.h>
  48. #include <CommonCrypto/CommonDigest.h>
  49. /* The Security framework has changed greatly between iOS and different macOS
  50. versions, and we will try to support as many of them as we can (back to
  51. Leopard and iOS 5) by using macros and weak-linking.
  52. In general, you want to build this using the most recent OS SDK, since some
  53. features require curl to be built against the latest SDK. TLS 1.1 and 1.2
  54. support, for instance, require the macOS 10.8 SDK or later. TLS 1.3
  55. requires the macOS 10.13 or iOS 11 SDK or later. */
  56. #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
  57. #if MAC_OS_X_VERSION_MAX_ALLOWED < 1050
  58. #error "The Secure Transport back-end requires Leopard or later."
  59. #endif /* MAC_OS_X_VERSION_MAX_ALLOWED < 1050 */
  60. #define CURL_BUILD_IOS 0
  61. #define CURL_BUILD_IOS_7 0
  62. #define CURL_BUILD_IOS_9 0
  63. #define CURL_BUILD_IOS_11 0
  64. #define CURL_BUILD_IOS_13 0
  65. #define CURL_BUILD_MAC 1
  66. /* This is the maximum API level we are allowed to use when building: */
  67. #define CURL_BUILD_MAC_10_5 MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
  68. #define CURL_BUILD_MAC_10_6 MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
  69. #define CURL_BUILD_MAC_10_7 MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  70. #define CURL_BUILD_MAC_10_8 MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
  71. #define CURL_BUILD_MAC_10_9 MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
  72. #define CURL_BUILD_MAC_10_11 MAC_OS_X_VERSION_MAX_ALLOWED >= 101100
  73. #define CURL_BUILD_MAC_10_13 MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
  74. #define CURL_BUILD_MAC_10_15 MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
  75. /* These macros mean "the following code is present to allow runtime backward
  76. compatibility with at least this cat or earlier":
  77. (You set this at build-time using the compiler command line option
  78. "-mmacosx-version-min.") */
  79. #define CURL_SUPPORT_MAC_10_5 MAC_OS_X_VERSION_MIN_REQUIRED <= 1050
  80. #define CURL_SUPPORT_MAC_10_6 MAC_OS_X_VERSION_MIN_REQUIRED <= 1060
  81. #define CURL_SUPPORT_MAC_10_7 MAC_OS_X_VERSION_MIN_REQUIRED <= 1070
  82. #define CURL_SUPPORT_MAC_10_8 MAC_OS_X_VERSION_MIN_REQUIRED <= 1080
  83. #define CURL_SUPPORT_MAC_10_9 MAC_OS_X_VERSION_MIN_REQUIRED <= 1090
  84. #elif TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
  85. #define CURL_BUILD_IOS 1
  86. #define CURL_BUILD_IOS_7 __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
  87. #define CURL_BUILD_IOS_9 __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
  88. #define CURL_BUILD_IOS_11 __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  89. #define CURL_BUILD_IOS_13 __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  90. #define CURL_BUILD_MAC 0
  91. #define CURL_BUILD_MAC_10_5 0
  92. #define CURL_BUILD_MAC_10_6 0
  93. #define CURL_BUILD_MAC_10_7 0
  94. #define CURL_BUILD_MAC_10_8 0
  95. #define CURL_BUILD_MAC_10_9 0
  96. #define CURL_BUILD_MAC_10_11 0
  97. #define CURL_BUILD_MAC_10_13 0
  98. #define CURL_BUILD_MAC_10_15 0
  99. #define CURL_SUPPORT_MAC_10_5 0
  100. #define CURL_SUPPORT_MAC_10_6 0
  101. #define CURL_SUPPORT_MAC_10_7 0
  102. #define CURL_SUPPORT_MAC_10_8 0
  103. #define CURL_SUPPORT_MAC_10_9 0
  104. #else
  105. #error "The Secure Transport back-end requires iOS or macOS."
  106. #endif /* (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) */
  107. #if CURL_BUILD_MAC
  108. #include <sys/sysctl.h>
  109. #endif /* CURL_BUILD_MAC */
  110. #include "sendf.h"
  111. #include "inet_pton.h"
  112. #include "connect.h"
  113. #include "select.h"
  114. #include "vtls.h"
  115. #include "sectransp.h"
  116. #include "curl_printf.h"
  117. #include "strdup.h"
  118. #include "curl_memory.h"
  119. /* The last #include file should be: */
  120. #include "memdebug.h"
  121. /* From MacTypes.h (which we can't include because it isn't present in iOS: */
  122. #define ioErr -36
  123. #define paramErr -50
  124. struct ssl_backend_data {
  125. SSLContextRef ssl_ctx;
  126. curl_socket_t ssl_sockfd;
  127. bool ssl_direction; /* true if writing, false if reading */
  128. size_t ssl_write_buffered_length;
  129. };
  130. struct st_cipher {
  131. const char *name; /* Cipher suite IANA name. It starts with "TLS_" prefix */
  132. const char *alias_name; /* Alias name is the same as OpenSSL cipher name */
  133. SSLCipherSuite num; /* Cipher suite code/number defined in IANA registry */
  134. bool weak; /* Flag to mark cipher as weak based on previous implementation
  135. of Secure Transport back-end by CURL */
  136. };
  137. /* Macro to initialize st_cipher data structure: stringify id to name, cipher
  138. number/id, 'weak' suite flag
  139. */
  140. #define CIPHER_DEF(num, alias, weak) \
  141. { #num, alias, num, weak }
  142. /*
  143. Macro to initialize st_cipher data structure with name, code (IANA cipher
  144. number/id value), and 'weak' suite flag. The first 28 cipher suite numbers
  145. have the same IANA code for both SSL and TLS standards: numbers 0x0000 to
  146. 0x001B. They have different names though. The first 4 letters of the cipher
  147. suite name are the protocol name: "SSL_" or "TLS_", rest of the IANA name is
  148. the same for both SSL and TLS cipher suite name.
  149. The second part of the problem is that macOS/iOS SDKs don't define all TLS
  150. codes but only 12 of them. The SDK defines all SSL codes though, i.e. SSL_NUM
  151. constant is always defined for those 28 ciphers while TLS_NUM is defined only
  152. for 12 of the first 28 ciphers. Those 12 TLS cipher codes match to
  153. corresponding SSL enum value and represent the same cipher suite. Therefore
  154. we'll use the SSL enum value for those cipher suites because it is defined
  155. for all 28 of them.
  156. We make internal data consistent and based on TLS names, i.e. all st_cipher
  157. item names start with the "TLS_" prefix.
  158. Summarizing all the above, those 28 first ciphers are presented in our table
  159. with both TLS and SSL names. Their cipher numbers are assigned based on the
  160. SDK enum value for the SSL cipher, which matches to IANA TLS number.
  161. */
  162. #define CIPHER_DEF_SSLTLS(num_wo_prefix, alias, weak) \
  163. { "TLS_" #num_wo_prefix, alias, SSL_##num_wo_prefix, weak }
  164. /*
  165. Cipher suites were marked as weak based on the following:
  166. RC4 encryption - rfc7465, the document contains a list of deprecated ciphers.
  167. Marked in the code below as weak.
  168. RC2 encryption - many mentions, was found vulnerable to a relatively easy
  169. attack https://link.springer.com/chapter/10.1007%2F3-540-69710-1_14
  170. Marked in the code below as weak.
  171. DES and IDEA encryption - rfc5469, has a list of deprecated ciphers.
  172. Marked in the code below as weak.
  173. Anonymous Diffie-Hellman authentication and anonymous elliptic curve
  174. Diffie-Hellman - vulnerable to a man-in-the-middle attack. Deprecated by
  175. RFC 4346 aka TLS 1.1 (section A.5, page 60)
  176. Null bulk encryption suites - not encrypted communication
  177. Export ciphers, i.e. ciphers with restrictions to be used outside the US for
  178. software exported to some countries, they were excluded from TLS 1.1
  179. version. More precisely, they were noted as ciphers which MUST NOT be
  180. negotiated in RFC 4346 aka TLS 1.1 (section A.5, pages 60 and 61).
  181. All of those filters were considered weak because they contain a weak
  182. algorithm like DES, RC2 or RC4, and already considered weak by other
  183. criteria.
  184. 3DES - NIST deprecated it and is going to retire it by 2023
  185. https://csrc.nist.gov/News/2017/Update-to-Current-Use-and-Deprecation-of-TDEA
  186. OpenSSL https://www.openssl.org/blog/blog/2016/08/24/sweet32/ also
  187. deprecated those ciphers. Some other libraries also consider it
  188. vulnerable or at least not strong enough.
  189. CBC ciphers are vulnerable with SSL3.0 and TLS1.0:
  190. https://www.cisco.com/c/en/us/support/docs/security/email-security-appliance
  191. /118518-technote-esa-00.html
  192. We don't take care of this issue because it is resolved by later TLS
  193. versions and for us, it requires more complicated checks, we need to
  194. check a protocol version also. Vulnerability doesn't look very critical
  195. and we do not filter out those cipher suites.
  196. */
  197. #define CIPHER_WEAK_NOT_ENCRYPTED TRUE
  198. #define CIPHER_WEAK_RC_ENCRYPTION TRUE
  199. #define CIPHER_WEAK_DES_ENCRYPTION TRUE
  200. #define CIPHER_WEAK_IDEA_ENCRYPTION TRUE
  201. #define CIPHER_WEAK_ANON_AUTH TRUE
  202. #define CIPHER_WEAK_3DES_ENCRYPTION TRUE
  203. #define CIPHER_STRONG_ENOUGH FALSE
  204. /* Please do not change the order of the first ciphers available for SSL.
  205. Do not insert and do not delete any of them. Code below
  206. depends on their order and continuity.
  207. If you add a new cipher, please maintain order by number, i.e.
  208. insert in between existing items to appropriate place based on
  209. cipher suite IANA number
  210. */
  211. const static struct st_cipher ciphertable[] = {
  212. /* SSL version 3.0 and initial TLS 1.0 cipher suites.
  213. Defined since SDK 10.2.8 */
  214. CIPHER_DEF_SSLTLS(NULL_WITH_NULL_NULL, /* 0x0000 */
  215. NULL,
  216. CIPHER_WEAK_NOT_ENCRYPTED),
  217. CIPHER_DEF_SSLTLS(RSA_WITH_NULL_MD5, /* 0x0001 */
  218. "NULL-MD5",
  219. CIPHER_WEAK_NOT_ENCRYPTED),
  220. CIPHER_DEF_SSLTLS(RSA_WITH_NULL_SHA, /* 0x0002 */
  221. "NULL-SHA",
  222. CIPHER_WEAK_NOT_ENCRYPTED),
  223. CIPHER_DEF_SSLTLS(RSA_EXPORT_WITH_RC4_40_MD5, /* 0x0003 */
  224. "EXP-RC4-MD5",
  225. CIPHER_WEAK_RC_ENCRYPTION),
  226. CIPHER_DEF_SSLTLS(RSA_WITH_RC4_128_MD5, /* 0x0004 */
  227. "RC4-MD5",
  228. CIPHER_WEAK_RC_ENCRYPTION),
  229. CIPHER_DEF_SSLTLS(RSA_WITH_RC4_128_SHA, /* 0x0005 */
  230. "RC4-SHA",
  231. CIPHER_WEAK_RC_ENCRYPTION),
  232. CIPHER_DEF_SSLTLS(RSA_EXPORT_WITH_RC2_CBC_40_MD5, /* 0x0006 */
  233. "EXP-RC2-CBC-MD5",
  234. CIPHER_WEAK_RC_ENCRYPTION),
  235. CIPHER_DEF_SSLTLS(RSA_WITH_IDEA_CBC_SHA, /* 0x0007 */
  236. "IDEA-CBC-SHA",
  237. CIPHER_WEAK_IDEA_ENCRYPTION),
  238. CIPHER_DEF_SSLTLS(RSA_EXPORT_WITH_DES40_CBC_SHA, /* 0x0008 */
  239. "EXP-DES-CBC-SHA",
  240. CIPHER_WEAK_DES_ENCRYPTION),
  241. CIPHER_DEF_SSLTLS(RSA_WITH_DES_CBC_SHA, /* 0x0009 */
  242. "DES-CBC-SHA",
  243. CIPHER_WEAK_DES_ENCRYPTION),
  244. CIPHER_DEF_SSLTLS(RSA_WITH_3DES_EDE_CBC_SHA, /* 0x000A */
  245. "DES-CBC3-SHA",
  246. CIPHER_WEAK_3DES_ENCRYPTION),
  247. CIPHER_DEF_SSLTLS(DH_DSS_EXPORT_WITH_DES40_CBC_SHA, /* 0x000B */
  248. "EXP-DH-DSS-DES-CBC-SHA",
  249. CIPHER_WEAK_DES_ENCRYPTION),
  250. CIPHER_DEF_SSLTLS(DH_DSS_WITH_DES_CBC_SHA, /* 0x000C */
  251. "DH-DSS-DES-CBC-SHA",
  252. CIPHER_WEAK_DES_ENCRYPTION),
  253. CIPHER_DEF_SSLTLS(DH_DSS_WITH_3DES_EDE_CBC_SHA, /* 0x000D */
  254. "DH-DSS-DES-CBC3-SHA",
  255. CIPHER_WEAK_3DES_ENCRYPTION),
  256. CIPHER_DEF_SSLTLS(DH_RSA_EXPORT_WITH_DES40_CBC_SHA, /* 0x000E */
  257. "EXP-DH-RSA-DES-CBC-SHA",
  258. CIPHER_WEAK_DES_ENCRYPTION),
  259. CIPHER_DEF_SSLTLS(DH_RSA_WITH_DES_CBC_SHA, /* 0x000F */
  260. "DH-RSA-DES-CBC-SHA",
  261. CIPHER_WEAK_DES_ENCRYPTION),
  262. CIPHER_DEF_SSLTLS(DH_RSA_WITH_3DES_EDE_CBC_SHA, /* 0x0010 */
  263. "DH-RSA-DES-CBC3-SHA",
  264. CIPHER_WEAK_3DES_ENCRYPTION),
  265. CIPHER_DEF_SSLTLS(DHE_DSS_EXPORT_WITH_DES40_CBC_SHA, /* 0x0011 */
  266. "EXP-EDH-DSS-DES-CBC-SHA",
  267. CIPHER_WEAK_DES_ENCRYPTION),
  268. CIPHER_DEF_SSLTLS(DHE_DSS_WITH_DES_CBC_SHA, /* 0x0012 */
  269. "EDH-DSS-CBC-SHA",
  270. CIPHER_WEAK_DES_ENCRYPTION),
  271. CIPHER_DEF_SSLTLS(DHE_DSS_WITH_3DES_EDE_CBC_SHA, /* 0x0013 */
  272. "DHE-DSS-DES-CBC3-SHA",
  273. CIPHER_WEAK_3DES_ENCRYPTION),
  274. CIPHER_DEF_SSLTLS(DHE_RSA_EXPORT_WITH_DES40_CBC_SHA, /* 0x0014 */
  275. "EXP-EDH-RSA-DES-CBC-SHA",
  276. CIPHER_WEAK_DES_ENCRYPTION),
  277. CIPHER_DEF_SSLTLS(DHE_RSA_WITH_DES_CBC_SHA, /* 0x0015 */
  278. "EDH-RSA-DES-CBC-SHA",
  279. CIPHER_WEAK_DES_ENCRYPTION),
  280. CIPHER_DEF_SSLTLS(DHE_RSA_WITH_3DES_EDE_CBC_SHA, /* 0x0016 */
  281. "DHE-RSA-DES-CBC3-SHA",
  282. CIPHER_WEAK_3DES_ENCRYPTION),
  283. CIPHER_DEF_SSLTLS(DH_anon_EXPORT_WITH_RC4_40_MD5, /* 0x0017 */
  284. "EXP-ADH-RC4-MD5",
  285. CIPHER_WEAK_ANON_AUTH),
  286. CIPHER_DEF_SSLTLS(DH_anon_WITH_RC4_128_MD5, /* 0x0018 */
  287. "ADH-RC4-MD5",
  288. CIPHER_WEAK_ANON_AUTH),
  289. CIPHER_DEF_SSLTLS(DH_anon_EXPORT_WITH_DES40_CBC_SHA, /* 0x0019 */
  290. "EXP-ADH-DES-CBC-SHA",
  291. CIPHER_WEAK_ANON_AUTH),
  292. CIPHER_DEF_SSLTLS(DH_anon_WITH_DES_CBC_SHA, /* 0x001A */
  293. "ADH-DES-CBC-SHA",
  294. CIPHER_WEAK_ANON_AUTH),
  295. CIPHER_DEF_SSLTLS(DH_anon_WITH_3DES_EDE_CBC_SHA, /* 0x001B */
  296. "ADH-DES-CBC3-SHA",
  297. CIPHER_WEAK_3DES_ENCRYPTION),
  298. CIPHER_DEF(SSL_FORTEZZA_DMS_WITH_NULL_SHA, /* 0x001C */
  299. NULL,
  300. CIPHER_WEAK_NOT_ENCRYPTED),
  301. CIPHER_DEF(SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA, /* 0x001D */
  302. NULL,
  303. CIPHER_STRONG_ENOUGH),
  304. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  305. /* RFC 4785 - Pre-Shared Key (PSK) Ciphersuites with NULL Encryption */
  306. CIPHER_DEF(TLS_PSK_WITH_NULL_SHA, /* 0x002C */
  307. "PSK-NULL-SHA",
  308. CIPHER_WEAK_NOT_ENCRYPTED),
  309. CIPHER_DEF(TLS_DHE_PSK_WITH_NULL_SHA, /* 0x002D */
  310. "DHE-PSK-NULL-SHA",
  311. CIPHER_WEAK_NOT_ENCRYPTED),
  312. CIPHER_DEF(TLS_RSA_PSK_WITH_NULL_SHA, /* 0x002E */
  313. "RSA-PSK-NULL-SHA",
  314. CIPHER_WEAK_NOT_ENCRYPTED),
  315. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  316. /* TLS addenda using AES, per RFC 3268. Defined since SDK 10.4u */
  317. CIPHER_DEF(TLS_RSA_WITH_AES_128_CBC_SHA, /* 0x002F */
  318. "AES128-SHA",
  319. CIPHER_STRONG_ENOUGH),
  320. CIPHER_DEF(TLS_DH_DSS_WITH_AES_128_CBC_SHA, /* 0x0030 */
  321. "DH-DSS-AES128-SHA",
  322. CIPHER_STRONG_ENOUGH),
  323. CIPHER_DEF(TLS_DH_RSA_WITH_AES_128_CBC_SHA, /* 0x0031 */
  324. "DH-RSA-AES128-SHA",
  325. CIPHER_STRONG_ENOUGH),
  326. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_128_CBC_SHA, /* 0x0032 */
  327. "DHE-DSS-AES128-SHA",
  328. CIPHER_STRONG_ENOUGH),
  329. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_128_CBC_SHA, /* 0x0033 */
  330. "DHE-RSA-AES128-SHA",
  331. CIPHER_STRONG_ENOUGH),
  332. CIPHER_DEF(TLS_DH_anon_WITH_AES_128_CBC_SHA, /* 0x0034 */
  333. "ADH-AES128-SHA",
  334. CIPHER_WEAK_ANON_AUTH),
  335. CIPHER_DEF(TLS_RSA_WITH_AES_256_CBC_SHA, /* 0x0035 */
  336. "AES256-SHA",
  337. CIPHER_STRONG_ENOUGH),
  338. CIPHER_DEF(TLS_DH_DSS_WITH_AES_256_CBC_SHA, /* 0x0036 */
  339. "DH-DSS-AES256-SHA",
  340. CIPHER_STRONG_ENOUGH),
  341. CIPHER_DEF(TLS_DH_RSA_WITH_AES_256_CBC_SHA, /* 0x0037 */
  342. "DH-RSA-AES256-SHA",
  343. CIPHER_STRONG_ENOUGH),
  344. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_256_CBC_SHA, /* 0x0038 */
  345. "DHE-DSS-AES256-SHA",
  346. CIPHER_STRONG_ENOUGH),
  347. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_256_CBC_SHA, /* 0x0039 */
  348. "DHE-RSA-AES256-SHA",
  349. CIPHER_STRONG_ENOUGH),
  350. CIPHER_DEF(TLS_DH_anon_WITH_AES_256_CBC_SHA, /* 0x003A */
  351. "ADH-AES256-SHA",
  352. CIPHER_WEAK_ANON_AUTH),
  353. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  354. /* TLS 1.2 addenda, RFC 5246 */
  355. /* Server provided RSA certificate for key exchange. */
  356. CIPHER_DEF(TLS_RSA_WITH_NULL_SHA256, /* 0x003B */
  357. "NULL-SHA256",
  358. CIPHER_WEAK_NOT_ENCRYPTED),
  359. CIPHER_DEF(TLS_RSA_WITH_AES_128_CBC_SHA256, /* 0x003C */
  360. "AES128-SHA256",
  361. CIPHER_STRONG_ENOUGH),
  362. CIPHER_DEF(TLS_RSA_WITH_AES_256_CBC_SHA256, /* 0x003D */
  363. "AES256-SHA256",
  364. CIPHER_STRONG_ENOUGH),
  365. /* Server-authenticated (and optionally client-authenticated)
  366. Diffie-Hellman. */
  367. CIPHER_DEF(TLS_DH_DSS_WITH_AES_128_CBC_SHA256, /* 0x003E */
  368. "DH-DSS-AES128-SHA256",
  369. CIPHER_STRONG_ENOUGH),
  370. CIPHER_DEF(TLS_DH_RSA_WITH_AES_128_CBC_SHA256, /* 0x003F */
  371. "DH-RSA-AES128-SHA256",
  372. CIPHER_STRONG_ENOUGH),
  373. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, /* 0x0040 */
  374. "DHE-DSS-AES128-SHA256",
  375. CIPHER_STRONG_ENOUGH),
  376. /* TLS 1.2 addenda, RFC 5246 */
  377. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, /* 0x0067 */
  378. "DHE-RSA-AES128-SHA256",
  379. CIPHER_STRONG_ENOUGH),
  380. CIPHER_DEF(TLS_DH_DSS_WITH_AES_256_CBC_SHA256, /* 0x0068 */
  381. "DH-DSS-AES256-SHA256",
  382. CIPHER_STRONG_ENOUGH),
  383. CIPHER_DEF(TLS_DH_RSA_WITH_AES_256_CBC_SHA256, /* 0x0069 */
  384. "DH-RSA-AES256-SHA256",
  385. CIPHER_STRONG_ENOUGH),
  386. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_256_CBC_SHA256, /* 0x006A */
  387. "DHE-DSS-AES256-SHA256",
  388. CIPHER_STRONG_ENOUGH),
  389. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, /* 0x006B */
  390. "DHE-RSA-AES256-SHA256",
  391. CIPHER_STRONG_ENOUGH),
  392. CIPHER_DEF(TLS_DH_anon_WITH_AES_128_CBC_SHA256, /* 0x006C */
  393. "ADH-AES128-SHA256",
  394. CIPHER_WEAK_ANON_AUTH),
  395. CIPHER_DEF(TLS_DH_anon_WITH_AES_256_CBC_SHA256, /* 0x006D */
  396. "ADH-AES256-SHA256",
  397. CIPHER_WEAK_ANON_AUTH),
  398. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  399. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  400. /* Addendum from RFC 4279, TLS PSK */
  401. CIPHER_DEF(TLS_PSK_WITH_RC4_128_SHA, /* 0x008A */
  402. "PSK-RC4-SHA",
  403. CIPHER_WEAK_RC_ENCRYPTION),
  404. CIPHER_DEF(TLS_PSK_WITH_3DES_EDE_CBC_SHA, /* 0x008B */
  405. "PSK-3DES-EDE-CBC-SHA",
  406. CIPHER_WEAK_3DES_ENCRYPTION),
  407. CIPHER_DEF(TLS_PSK_WITH_AES_128_CBC_SHA, /* 0x008C */
  408. "PSK-AES128-CBC-SHA",
  409. CIPHER_STRONG_ENOUGH),
  410. CIPHER_DEF(TLS_PSK_WITH_AES_256_CBC_SHA, /* 0x008D */
  411. "PSK-AES256-CBC-SHA",
  412. CIPHER_STRONG_ENOUGH),
  413. CIPHER_DEF(TLS_DHE_PSK_WITH_RC4_128_SHA, /* 0x008E */
  414. "DHE-PSK-RC4-SHA",
  415. CIPHER_WEAK_RC_ENCRYPTION),
  416. CIPHER_DEF(TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA, /* 0x008F */
  417. "DHE-PSK-3DES-EDE-CBC-SHA",
  418. CIPHER_WEAK_3DES_ENCRYPTION),
  419. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_128_CBC_SHA, /* 0x0090 */
  420. "DHE-PSK-AES128-CBC-SHA",
  421. CIPHER_STRONG_ENOUGH),
  422. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_256_CBC_SHA, /* 0x0091 */
  423. "DHE-PSK-AES256-CBC-SHA",
  424. CIPHER_STRONG_ENOUGH),
  425. CIPHER_DEF(TLS_RSA_PSK_WITH_RC4_128_SHA, /* 0x0092 */
  426. "RSA-PSK-RC4-SHA",
  427. CIPHER_WEAK_RC_ENCRYPTION),
  428. CIPHER_DEF(TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA, /* 0x0093 */
  429. "RSA-PSK-3DES-EDE-CBC-SHA",
  430. CIPHER_WEAK_3DES_ENCRYPTION),
  431. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_128_CBC_SHA, /* 0x0094 */
  432. "RSA-PSK-AES128-CBC-SHA",
  433. CIPHER_STRONG_ENOUGH),
  434. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_256_CBC_SHA, /* 0x0095 */
  435. "RSA-PSK-AES256-CBC-SHA",
  436. CIPHER_STRONG_ENOUGH),
  437. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  438. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  439. /* Addenda from rfc 5288 AES Galois Counter Mode (GCM) Cipher Suites
  440. for TLS. */
  441. CIPHER_DEF(TLS_RSA_WITH_AES_128_GCM_SHA256, /* 0x009C */
  442. "AES128-GCM-SHA256",
  443. CIPHER_STRONG_ENOUGH),
  444. CIPHER_DEF(TLS_RSA_WITH_AES_256_GCM_SHA384, /* 0x009D */
  445. "AES256-GCM-SHA384",
  446. CIPHER_STRONG_ENOUGH),
  447. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, /* 0x009E */
  448. "DHE-RSA-AES128-GCM-SHA256",
  449. CIPHER_STRONG_ENOUGH),
  450. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, /* 0x009F */
  451. "DHE-RSA-AES256-GCM-SHA384",
  452. CIPHER_STRONG_ENOUGH),
  453. CIPHER_DEF(TLS_DH_RSA_WITH_AES_128_GCM_SHA256, /* 0x00A0 */
  454. "DH-RSA-AES128-GCM-SHA256",
  455. CIPHER_STRONG_ENOUGH),
  456. CIPHER_DEF(TLS_DH_RSA_WITH_AES_256_GCM_SHA384, /* 0x00A1 */
  457. "DH-RSA-AES256-GCM-SHA384",
  458. CIPHER_STRONG_ENOUGH),
  459. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, /* 0x00A2 */
  460. "DHE-DSS-AES128-GCM-SHA256",
  461. CIPHER_STRONG_ENOUGH),
  462. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_256_GCM_SHA384, /* 0x00A3 */
  463. "DHE-DSS-AES256-GCM-SHA384",
  464. CIPHER_STRONG_ENOUGH),
  465. CIPHER_DEF(TLS_DH_DSS_WITH_AES_128_GCM_SHA256, /* 0x00A4 */
  466. "DH-DSS-AES128-GCM-SHA256",
  467. CIPHER_STRONG_ENOUGH),
  468. CIPHER_DEF(TLS_DH_DSS_WITH_AES_256_GCM_SHA384, /* 0x00A5 */
  469. "DH-DSS-AES256-GCM-SHA384",
  470. CIPHER_STRONG_ENOUGH),
  471. CIPHER_DEF(TLS_DH_anon_WITH_AES_128_GCM_SHA256, /* 0x00A6 */
  472. "ADH-AES128-GCM-SHA256",
  473. CIPHER_WEAK_ANON_AUTH),
  474. CIPHER_DEF(TLS_DH_anon_WITH_AES_256_GCM_SHA384, /* 0x00A7 */
  475. "ADH-AES256-GCM-SHA384",
  476. CIPHER_WEAK_ANON_AUTH),
  477. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  478. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  479. /* RFC 5487 - PSK with SHA-256/384 and AES GCM */
  480. CIPHER_DEF(TLS_PSK_WITH_AES_128_GCM_SHA256, /* 0x00A8 */
  481. "PSK-AES128-GCM-SHA256",
  482. CIPHER_STRONG_ENOUGH),
  483. CIPHER_DEF(TLS_PSK_WITH_AES_256_GCM_SHA384, /* 0x00A9 */
  484. "PSK-AES256-GCM-SHA384",
  485. CIPHER_STRONG_ENOUGH),
  486. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_128_GCM_SHA256, /* 0x00AA */
  487. "DHE-PSK-AES128-GCM-SHA256",
  488. CIPHER_STRONG_ENOUGH),
  489. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_256_GCM_SHA384, /* 0x00AB */
  490. "DHE-PSK-AES256-GCM-SHA384",
  491. CIPHER_STRONG_ENOUGH),
  492. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_128_GCM_SHA256, /* 0x00AC */
  493. "RSA-PSK-AES128-GCM-SHA256",
  494. CIPHER_STRONG_ENOUGH),
  495. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_256_GCM_SHA384, /* 0x00AD */
  496. "RSA-PSK-AES256-GCM-SHA384",
  497. CIPHER_STRONG_ENOUGH),
  498. CIPHER_DEF(TLS_PSK_WITH_AES_128_CBC_SHA256, /* 0x00AE */
  499. "PSK-AES128-CBC-SHA256",
  500. CIPHER_STRONG_ENOUGH),
  501. CIPHER_DEF(TLS_PSK_WITH_AES_256_CBC_SHA384, /* 0x00AF */
  502. "PSK-AES256-CBC-SHA384",
  503. CIPHER_STRONG_ENOUGH),
  504. CIPHER_DEF(TLS_PSK_WITH_NULL_SHA256, /* 0x00B0 */
  505. "PSK-NULL-SHA256",
  506. CIPHER_WEAK_NOT_ENCRYPTED),
  507. CIPHER_DEF(TLS_PSK_WITH_NULL_SHA384, /* 0x00B1 */
  508. "PSK-NULL-SHA384",
  509. CIPHER_WEAK_NOT_ENCRYPTED),
  510. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_128_CBC_SHA256, /* 0x00B2 */
  511. "DHE-PSK-AES128-CBC-SHA256",
  512. CIPHER_STRONG_ENOUGH),
  513. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_256_CBC_SHA384, /* 0x00B3 */
  514. "DHE-PSK-AES256-CBC-SHA384",
  515. CIPHER_STRONG_ENOUGH),
  516. CIPHER_DEF(TLS_DHE_PSK_WITH_NULL_SHA256, /* 0x00B4 */
  517. "DHE-PSK-NULL-SHA256",
  518. CIPHER_WEAK_NOT_ENCRYPTED),
  519. CIPHER_DEF(TLS_DHE_PSK_WITH_NULL_SHA384, /* 0x00B5 */
  520. "DHE-PSK-NULL-SHA384",
  521. CIPHER_WEAK_NOT_ENCRYPTED),
  522. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_128_CBC_SHA256, /* 0x00B6 */
  523. "RSA-PSK-AES128-CBC-SHA256",
  524. CIPHER_STRONG_ENOUGH),
  525. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_256_CBC_SHA384, /* 0x00B7 */
  526. "RSA-PSK-AES256-CBC-SHA384",
  527. CIPHER_STRONG_ENOUGH),
  528. CIPHER_DEF(TLS_RSA_PSK_WITH_NULL_SHA256, /* 0x00B8 */
  529. "RSA-PSK-NULL-SHA256",
  530. CIPHER_WEAK_NOT_ENCRYPTED),
  531. CIPHER_DEF(TLS_RSA_PSK_WITH_NULL_SHA384, /* 0x00B9 */
  532. "RSA-PSK-NULL-SHA384",
  533. CIPHER_WEAK_NOT_ENCRYPTED),
  534. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  535. /* RFC 5746 - Secure Renegotiation. This is not a real suite,
  536. it is a response to initiate negotiation again */
  537. CIPHER_DEF(TLS_EMPTY_RENEGOTIATION_INFO_SCSV, /* 0x00FF */
  538. NULL,
  539. CIPHER_STRONG_ENOUGH),
  540. #if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11
  541. /* TLS 1.3 standard cipher suites for ChaCha20+Poly1305.
  542. Note: TLS 1.3 ciphersuites do not specify the key exchange
  543. algorithm -- they only specify the symmetric ciphers.
  544. Cipher alias name matches to OpenSSL cipher name, and for
  545. TLS 1.3 ciphers */
  546. CIPHER_DEF(TLS_AES_128_GCM_SHA256, /* 0x1301 */
  547. NULL, /* The OpenSSL cipher name matches to the IANA name */
  548. CIPHER_STRONG_ENOUGH),
  549. CIPHER_DEF(TLS_AES_256_GCM_SHA384, /* 0x1302 */
  550. NULL, /* The OpenSSL cipher name matches to the IANA name */
  551. CIPHER_STRONG_ENOUGH),
  552. CIPHER_DEF(TLS_CHACHA20_POLY1305_SHA256, /* 0x1303 */
  553. NULL, /* The OpenSSL cipher name matches to the IANA name */
  554. CIPHER_STRONG_ENOUGH),
  555. CIPHER_DEF(TLS_AES_128_CCM_SHA256, /* 0x1304 */
  556. NULL, /* The OpenSSL cipher name matches to the IANA name */
  557. CIPHER_STRONG_ENOUGH),
  558. CIPHER_DEF(TLS_AES_128_CCM_8_SHA256, /* 0x1305 */
  559. NULL, /* The OpenSSL cipher name matches to the IANA name */
  560. CIPHER_STRONG_ENOUGH),
  561. #endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */
  562. #if CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS
  563. /* ECDSA addenda, RFC 4492 */
  564. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_NULL_SHA, /* 0xC001 */
  565. "ECDH-ECDSA-NULL-SHA",
  566. CIPHER_WEAK_NOT_ENCRYPTED),
  567. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_RC4_128_SHA, /* 0xC002 */
  568. "ECDH-ECDSA-RC4-SHA",
  569. CIPHER_WEAK_RC_ENCRYPTION),
  570. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, /* 0xC003 */
  571. "ECDH-ECDSA-DES-CBC3-SHA",
  572. CIPHER_WEAK_3DES_ENCRYPTION),
  573. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, /* 0xC004 */
  574. "ECDH-ECDSA-AES128-SHA",
  575. CIPHER_STRONG_ENOUGH),
  576. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, /* 0xC005 */
  577. "ECDH-ECDSA-AES256-SHA",
  578. CIPHER_STRONG_ENOUGH),
  579. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_NULL_SHA, /* 0xC006 */
  580. "ECDHE-ECDSA-NULL-SHA",
  581. CIPHER_WEAK_NOT_ENCRYPTED),
  582. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, /* 0xC007 */
  583. "ECDHE-ECDSA-RC4-SHA",
  584. CIPHER_WEAK_RC_ENCRYPTION),
  585. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, /* 0xC008 */
  586. "ECDHE-ECDSA-DES-CBC3-SHA",
  587. CIPHER_WEAK_3DES_ENCRYPTION),
  588. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, /* 0xC009 */
  589. "ECDHE-ECDSA-AES128-SHA",
  590. CIPHER_STRONG_ENOUGH),
  591. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, /* 0xC00A */
  592. "ECDHE-ECDSA-AES256-SHA",
  593. CIPHER_STRONG_ENOUGH),
  594. CIPHER_DEF(TLS_ECDH_RSA_WITH_NULL_SHA, /* 0xC00B */
  595. "ECDH-RSA-NULL-SHA",
  596. CIPHER_WEAK_NOT_ENCRYPTED),
  597. CIPHER_DEF(TLS_ECDH_RSA_WITH_RC4_128_SHA, /* 0xC00C */
  598. "ECDH-RSA-RC4-SHA",
  599. CIPHER_WEAK_RC_ENCRYPTION),
  600. CIPHER_DEF(TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, /* 0xC00D */
  601. "ECDH-RSA-DES-CBC3-SHA",
  602. CIPHER_WEAK_3DES_ENCRYPTION),
  603. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, /* 0xC00E */
  604. "ECDH-RSA-AES128-SHA",
  605. CIPHER_STRONG_ENOUGH),
  606. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, /* 0xC00F */
  607. "ECDH-RSA-AES256-SHA",
  608. CIPHER_STRONG_ENOUGH),
  609. CIPHER_DEF(TLS_ECDHE_RSA_WITH_NULL_SHA, /* 0xC010 */
  610. "ECDHE-RSA-NULL-SHA",
  611. CIPHER_WEAK_NOT_ENCRYPTED),
  612. CIPHER_DEF(TLS_ECDHE_RSA_WITH_RC4_128_SHA, /* 0xC011 */
  613. "ECDHE-RSA-RC4-SHA",
  614. CIPHER_WEAK_RC_ENCRYPTION),
  615. CIPHER_DEF(TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, /* 0xC012 */
  616. "ECDHE-RSA-DES-CBC3-SHA",
  617. CIPHER_WEAK_3DES_ENCRYPTION),
  618. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, /* 0xC013 */
  619. "ECDHE-RSA-AES128-SHA",
  620. CIPHER_STRONG_ENOUGH),
  621. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, /* 0xC014 */
  622. "ECDHE-RSA-AES256-SHA",
  623. CIPHER_STRONG_ENOUGH),
  624. CIPHER_DEF(TLS_ECDH_anon_WITH_NULL_SHA, /* 0xC015 */
  625. "AECDH-NULL-SHA",
  626. CIPHER_WEAK_ANON_AUTH),
  627. CIPHER_DEF(TLS_ECDH_anon_WITH_RC4_128_SHA, /* 0xC016 */
  628. "AECDH-RC4-SHA",
  629. CIPHER_WEAK_ANON_AUTH),
  630. CIPHER_DEF(TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA, /* 0xC017 */
  631. "AECDH-DES-CBC3-SHA",
  632. CIPHER_WEAK_3DES_ENCRYPTION),
  633. CIPHER_DEF(TLS_ECDH_anon_WITH_AES_128_CBC_SHA, /* 0xC018 */
  634. "AECDH-AES128-SHA",
  635. CIPHER_WEAK_ANON_AUTH),
  636. CIPHER_DEF(TLS_ECDH_anon_WITH_AES_256_CBC_SHA, /* 0xC019 */
  637. "AECDH-AES256-SHA",
  638. CIPHER_WEAK_ANON_AUTH),
  639. #endif /* CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS */
  640. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  641. /* Addenda from rfc 5289 Elliptic Curve Cipher Suites with
  642. HMAC SHA-256/384. */
  643. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, /* 0xC023 */
  644. "ECDHE-ECDSA-AES128-SHA256",
  645. CIPHER_STRONG_ENOUGH),
  646. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, /* 0xC024 */
  647. "ECDHE-ECDSA-AES256-SHA384",
  648. CIPHER_STRONG_ENOUGH),
  649. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, /* 0xC025 */
  650. "ECDH-ECDSA-AES128-SHA256",
  651. CIPHER_STRONG_ENOUGH),
  652. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, /* 0xC026 */
  653. "ECDH-ECDSA-AES256-SHA384",
  654. CIPHER_STRONG_ENOUGH),
  655. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, /* 0xC027 */
  656. "ECDHE-RSA-AES128-SHA256",
  657. CIPHER_STRONG_ENOUGH),
  658. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, /* 0xC028 */
  659. "ECDHE-RSA-AES256-SHA384",
  660. CIPHER_STRONG_ENOUGH),
  661. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, /* 0xC029 */
  662. "ECDH-RSA-AES128-SHA256",
  663. CIPHER_STRONG_ENOUGH),
  664. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, /* 0xC02A */
  665. "ECDH-RSA-AES256-SHA384",
  666. CIPHER_STRONG_ENOUGH),
  667. /* Addenda from rfc 5289 Elliptic Curve Cipher Suites with
  668. SHA-256/384 and AES Galois Counter Mode (GCM) */
  669. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, /* 0xC02B */
  670. "ECDHE-ECDSA-AES128-GCM-SHA256",
  671. CIPHER_STRONG_ENOUGH),
  672. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, /* 0xC02C */
  673. "ECDHE-ECDSA-AES256-GCM-SHA384",
  674. CIPHER_STRONG_ENOUGH),
  675. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, /* 0xC02D */
  676. "ECDH-ECDSA-AES128-GCM-SHA256",
  677. CIPHER_STRONG_ENOUGH),
  678. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, /* 0xC02E */
  679. "ECDH-ECDSA-AES256-GCM-SHA384",
  680. CIPHER_STRONG_ENOUGH),
  681. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* 0xC02F */
  682. "ECDHE-RSA-AES128-GCM-SHA256",
  683. CIPHER_STRONG_ENOUGH),
  684. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, /* 0xC030 */
  685. "ECDHE-RSA-AES256-GCM-SHA384",
  686. CIPHER_STRONG_ENOUGH),
  687. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, /* 0xC031 */
  688. "ECDH-RSA-AES128-GCM-SHA256",
  689. CIPHER_STRONG_ENOUGH),
  690. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, /* 0xC032 */
  691. "ECDH-RSA-AES256-GCM-SHA384",
  692. CIPHER_STRONG_ENOUGH),
  693. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  694. #if CURL_BUILD_MAC_10_15 || CURL_BUILD_IOS_13
  695. /* ECDHE_PSK Cipher Suites for Transport Layer Security (TLS), RFC 5489 */
  696. CIPHER_DEF(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA, /* 0xC035 */
  697. "ECDHE-PSK-AES128-CBC-SHA",
  698. CIPHER_STRONG_ENOUGH),
  699. CIPHER_DEF(TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA, /* 0xC036 */
  700. "ECDHE-PSK-AES256-CBC-SHA",
  701. CIPHER_STRONG_ENOUGH),
  702. #endif /* CURL_BUILD_MAC_10_15 || CURL_BUILD_IOS_13 */
  703. #if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11
  704. /* Addenda from rfc 7905 ChaCha20-Poly1305 Cipher Suites for
  705. Transport Layer Security (TLS). */
  706. CIPHER_DEF(TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA8 */
  707. "ECDHE-RSA-CHACHA20-POLY1305",
  708. CIPHER_STRONG_ENOUGH),
  709. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA9 */
  710. "ECDHE-ECDSA-CHACHA20-POLY1305",
  711. CIPHER_STRONG_ENOUGH),
  712. #endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */
  713. #if CURL_BUILD_MAC_10_15 || CURL_BUILD_IOS_13
  714. /* ChaCha20-Poly1305 Cipher Suites for Transport Layer Security (TLS),
  715. RFC 7905 */
  716. CIPHER_DEF(TLS_PSK_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCAB */
  717. "PSK-CHACHA20-POLY1305",
  718. CIPHER_STRONG_ENOUGH),
  719. #endif /* CURL_BUILD_MAC_10_15 || CURL_BUILD_IOS_13 */
  720. /* Tags for SSL 2 cipher kinds which are not specified for SSL 3.
  721. Defined since SDK 10.2.8 */
  722. CIPHER_DEF(SSL_RSA_WITH_RC2_CBC_MD5, /* 0xFF80 */
  723. NULL,
  724. CIPHER_WEAK_RC_ENCRYPTION),
  725. CIPHER_DEF(SSL_RSA_WITH_IDEA_CBC_MD5, /* 0xFF81 */
  726. NULL,
  727. CIPHER_WEAK_IDEA_ENCRYPTION),
  728. CIPHER_DEF(SSL_RSA_WITH_DES_CBC_MD5, /* 0xFF82 */
  729. NULL,
  730. CIPHER_WEAK_DES_ENCRYPTION),
  731. CIPHER_DEF(SSL_RSA_WITH_3DES_EDE_CBC_MD5, /* 0xFF83 */
  732. NULL,
  733. CIPHER_WEAK_3DES_ENCRYPTION),
  734. };
  735. #define NUM_OF_CIPHERS sizeof(ciphertable)/sizeof(ciphertable[0])
  736. /* pinned public key support tests */
  737. /* version 1 supports macOS 10.12+ and iOS 10+ */
  738. #if ((TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || \
  739. (!TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200))
  740. #define SECTRANSP_PINNEDPUBKEY_V1 1
  741. #endif
  742. /* version 2 supports MacOSX 10.7+ */
  743. #if (!TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)
  744. #define SECTRANSP_PINNEDPUBKEY_V2 1
  745. #endif
  746. #if defined(SECTRANSP_PINNEDPUBKEY_V1) || defined(SECTRANSP_PINNEDPUBKEY_V2)
  747. /* this backend supports CURLOPT_PINNEDPUBLICKEY */
  748. #define SECTRANSP_PINNEDPUBKEY 1
  749. #endif /* SECTRANSP_PINNEDPUBKEY */
  750. #ifdef SECTRANSP_PINNEDPUBKEY
  751. /* both new and old APIs return rsa keys missing the spki header (not DER) */
  752. static const unsigned char rsa4096SpkiHeader[] = {
  753. 0x30, 0x82, 0x02, 0x22, 0x30, 0x0d,
  754. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  755. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
  756. 0x00, 0x03, 0x82, 0x02, 0x0f, 0x00};
  757. static const unsigned char rsa2048SpkiHeader[] = {
  758. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d,
  759. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  760. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
  761. 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00};
  762. #ifdef SECTRANSP_PINNEDPUBKEY_V1
  763. /* the *new* version doesn't return DER encoded ecdsa certs like the old... */
  764. static const unsigned char ecDsaSecp256r1SpkiHeader[] = {
  765. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07,
  766. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  767. 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48,
  768. 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  769. 0x42, 0x00};
  770. static const unsigned char ecDsaSecp384r1SpkiHeader[] = {
  771. 0x30, 0x76, 0x30, 0x10, 0x06, 0x07,
  772. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  773. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04,
  774. 0x00, 0x22, 0x03, 0x62, 0x00};
  775. #endif /* SECTRANSP_PINNEDPUBKEY_V1 */
  776. #endif /* SECTRANSP_PINNEDPUBKEY */
  777. /* The following two functions were ripped from Apple sample code,
  778. * with some modifications: */
  779. static OSStatus SocketRead(SSLConnectionRef connection,
  780. void *data, /* owned by
  781. * caller, data
  782. * RETURNED */
  783. size_t *dataLength) /* IN/OUT */
  784. {
  785. size_t bytesToGo = *dataLength;
  786. size_t initLen = bytesToGo;
  787. UInt8 *currData = (UInt8 *)data;
  788. struct ssl_connect_data *connssl = (struct ssl_connect_data *)connection;
  789. struct ssl_backend_data *backend = connssl->backend;
  790. int sock;
  791. OSStatus rtn = noErr;
  792. size_t bytesRead;
  793. ssize_t rrtn;
  794. int theErr;
  795. DEBUGASSERT(backend);
  796. sock = backend->ssl_sockfd;
  797. *dataLength = 0;
  798. for(;;) {
  799. bytesRead = 0;
  800. rrtn = read(sock, currData, bytesToGo);
  801. if(rrtn <= 0) {
  802. /* this is guesswork... */
  803. theErr = errno;
  804. if(rrtn == 0) { /* EOF = server hung up */
  805. /* the framework will turn this into errSSLClosedNoNotify */
  806. rtn = errSSLClosedGraceful;
  807. }
  808. else /* do the switch */
  809. switch(theErr) {
  810. case ENOENT:
  811. /* connection closed */
  812. rtn = errSSLClosedGraceful;
  813. break;
  814. case ECONNRESET:
  815. rtn = errSSLClosedAbort;
  816. break;
  817. case EAGAIN:
  818. rtn = errSSLWouldBlock;
  819. backend->ssl_direction = false;
  820. break;
  821. default:
  822. rtn = ioErr;
  823. break;
  824. }
  825. break;
  826. }
  827. else {
  828. bytesRead = rrtn;
  829. }
  830. bytesToGo -= bytesRead;
  831. currData += bytesRead;
  832. if(bytesToGo == 0) {
  833. /* filled buffer with incoming data, done */
  834. break;
  835. }
  836. }
  837. *dataLength = initLen - bytesToGo;
  838. return rtn;
  839. }
  840. static OSStatus SocketWrite(SSLConnectionRef connection,
  841. const void *data,
  842. size_t *dataLength) /* IN/OUT */
  843. {
  844. size_t bytesSent = 0;
  845. struct ssl_connect_data *connssl = (struct ssl_connect_data *)connection;
  846. struct ssl_backend_data *backend = connssl->backend;
  847. int sock;
  848. ssize_t length;
  849. size_t dataLen = *dataLength;
  850. const UInt8 *dataPtr = (UInt8 *)data;
  851. OSStatus ortn;
  852. int theErr;
  853. DEBUGASSERT(backend);
  854. sock = backend->ssl_sockfd;
  855. *dataLength = 0;
  856. do {
  857. length = write(sock,
  858. (char *)dataPtr + bytesSent,
  859. dataLen - bytesSent);
  860. } while((length > 0) &&
  861. ( (bytesSent += length) < dataLen) );
  862. if(length <= 0) {
  863. theErr = errno;
  864. if(theErr == EAGAIN) {
  865. ortn = errSSLWouldBlock;
  866. backend->ssl_direction = true;
  867. }
  868. else {
  869. ortn = ioErr;
  870. }
  871. }
  872. else {
  873. ortn = noErr;
  874. }
  875. *dataLength = bytesSent;
  876. return ortn;
  877. }
  878. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  879. CF_INLINE const char *TLSCipherNameForNumber(SSLCipherSuite cipher)
  880. {
  881. /* The first ciphers in the ciphertable are continuous. Here we do small
  882. optimization and instead of loop directly get SSL name by cipher number.
  883. */
  884. if(cipher <= SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA) {
  885. return ciphertable[cipher].name;
  886. }
  887. /* Iterate through the rest of the ciphers */
  888. for(size_t i = SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA + 1;
  889. i < NUM_OF_CIPHERS;
  890. ++i) {
  891. if(ciphertable[i].num == cipher) {
  892. return ciphertable[i].name;
  893. }
  894. }
  895. return ciphertable[SSL_NULL_WITH_NULL_NULL].name;
  896. }
  897. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  898. #if CURL_BUILD_MAC
  899. CF_INLINE void GetDarwinVersionNumber(int *major, int *minor)
  900. {
  901. int mib[2];
  902. char *os_version;
  903. size_t os_version_len;
  904. char *os_version_major, *os_version_minor;
  905. char *tok_buf;
  906. /* Get the Darwin kernel version from the kernel using sysctl(): */
  907. mib[0] = CTL_KERN;
  908. mib[1] = KERN_OSRELEASE;
  909. if(sysctl(mib, 2, NULL, &os_version_len, NULL, 0) == -1)
  910. return;
  911. os_version = malloc(os_version_len*sizeof(char));
  912. if(!os_version)
  913. return;
  914. if(sysctl(mib, 2, os_version, &os_version_len, NULL, 0) == -1) {
  915. free(os_version);
  916. return;
  917. }
  918. /* Parse the version: */
  919. os_version_major = strtok_r(os_version, ".", &tok_buf);
  920. os_version_minor = strtok_r(NULL, ".", &tok_buf);
  921. *major = atoi(os_version_major);
  922. *minor = atoi(os_version_minor);
  923. free(os_version);
  924. }
  925. #endif /* CURL_BUILD_MAC */
  926. /* Apple provides a myriad of ways of getting information about a certificate
  927. into a string. Some aren't available under iOS or newer cats. So here's
  928. a unified function for getting a string describing the certificate that
  929. ought to work in all cats starting with Leopard. */
  930. CF_INLINE CFStringRef getsubject(SecCertificateRef cert)
  931. {
  932. CFStringRef server_cert_summary = CFSTR("(null)");
  933. #if CURL_BUILD_IOS
  934. /* iOS: There's only one way to do this. */
  935. server_cert_summary = SecCertificateCopySubjectSummary(cert);
  936. #else
  937. #if CURL_BUILD_MAC_10_7
  938. /* Lion & later: Get the long description if we can. */
  939. if(SecCertificateCopyLongDescription)
  940. server_cert_summary =
  941. SecCertificateCopyLongDescription(NULL, cert, NULL);
  942. else
  943. #endif /* CURL_BUILD_MAC_10_7 */
  944. #if CURL_BUILD_MAC_10_6
  945. /* Snow Leopard: Get the certificate summary. */
  946. if(SecCertificateCopySubjectSummary)
  947. server_cert_summary = SecCertificateCopySubjectSummary(cert);
  948. else
  949. #endif /* CURL_BUILD_MAC_10_6 */
  950. /* Leopard is as far back as we go... */
  951. (void)SecCertificateCopyCommonName(cert, &server_cert_summary);
  952. #endif /* CURL_BUILD_IOS */
  953. return server_cert_summary;
  954. }
  955. static CURLcode CopyCertSubject(struct Curl_easy *data,
  956. SecCertificateRef cert, char **certp)
  957. {
  958. CFStringRef c = getsubject(cert);
  959. CURLcode result = CURLE_OK;
  960. const char *direct;
  961. char *cbuf = NULL;
  962. *certp = NULL;
  963. if(!c) {
  964. failf(data, "SSL: invalid CA certificate subject");
  965. return CURLE_PEER_FAILED_VERIFICATION;
  966. }
  967. /* If the subject is already available as UTF-8 encoded (ie 'direct') then
  968. use that, else convert it. */
  969. direct = CFStringGetCStringPtr(c, kCFStringEncodingUTF8);
  970. if(direct) {
  971. *certp = strdup(direct);
  972. if(!*certp) {
  973. failf(data, "SSL: out of memory");
  974. result = CURLE_OUT_OF_MEMORY;
  975. }
  976. }
  977. else {
  978. size_t cbuf_size = ((size_t)CFStringGetLength(c) * 4) + 1;
  979. cbuf = calloc(cbuf_size, 1);
  980. if(cbuf) {
  981. if(!CFStringGetCString(c, cbuf, cbuf_size,
  982. kCFStringEncodingUTF8)) {
  983. failf(data, "SSL: invalid CA certificate subject");
  984. result = CURLE_PEER_FAILED_VERIFICATION;
  985. }
  986. else
  987. /* pass back the buffer */
  988. *certp = cbuf;
  989. }
  990. else {
  991. failf(data, "SSL: couldn't allocate %zu bytes of memory", cbuf_size);
  992. result = CURLE_OUT_OF_MEMORY;
  993. }
  994. }
  995. if(result)
  996. free(cbuf);
  997. CFRelease(c);
  998. return result;
  999. }
  1000. #if CURL_SUPPORT_MAC_10_6
  1001. /* The SecKeychainSearch API was deprecated in Lion, and using it will raise
  1002. deprecation warnings, so let's not compile this unless it's necessary: */
  1003. static OSStatus CopyIdentityWithLabelOldSchool(char *label,
  1004. SecIdentityRef *out_c_a_k)
  1005. {
  1006. OSStatus status = errSecItemNotFound;
  1007. SecKeychainAttributeList attr_list;
  1008. SecKeychainAttribute attr;
  1009. SecKeychainSearchRef search = NULL;
  1010. SecCertificateRef cert = NULL;
  1011. /* Set up the attribute list: */
  1012. attr_list.count = 1L;
  1013. attr_list.attr = &attr;
  1014. /* Set up our lone search criterion: */
  1015. attr.tag = kSecLabelItemAttr;
  1016. attr.data = label;
  1017. attr.length = (UInt32)strlen(label);
  1018. /* Start searching: */
  1019. status = SecKeychainSearchCreateFromAttributes(NULL,
  1020. kSecCertificateItemClass,
  1021. &attr_list,
  1022. &search);
  1023. if(status == noErr) {
  1024. status = SecKeychainSearchCopyNext(search,
  1025. (SecKeychainItemRef *)&cert);
  1026. if(status == noErr && cert) {
  1027. /* If we found a certificate, does it have a private key? */
  1028. status = SecIdentityCreateWithCertificate(NULL, cert, out_c_a_k);
  1029. CFRelease(cert);
  1030. }
  1031. }
  1032. if(search)
  1033. CFRelease(search);
  1034. return status;
  1035. }
  1036. #endif /* CURL_SUPPORT_MAC_10_6 */
  1037. static OSStatus CopyIdentityWithLabel(char *label,
  1038. SecIdentityRef *out_cert_and_key)
  1039. {
  1040. OSStatus status = errSecItemNotFound;
  1041. #if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS
  1042. CFArrayRef keys_list;
  1043. CFIndex keys_list_count;
  1044. CFIndex i;
  1045. CFStringRef common_name;
  1046. /* SecItemCopyMatching() was introduced in iOS and Snow Leopard.
  1047. kSecClassIdentity was introduced in Lion. If both exist, let's use them
  1048. to find the certificate. */
  1049. if(SecItemCopyMatching && kSecClassIdentity) {
  1050. CFTypeRef keys[5];
  1051. CFTypeRef values[5];
  1052. CFDictionaryRef query_dict;
  1053. CFStringRef label_cf = CFStringCreateWithCString(NULL, label,
  1054. kCFStringEncodingUTF8);
  1055. /* Set up our search criteria and expected results: */
  1056. values[0] = kSecClassIdentity; /* we want a certificate and a key */
  1057. keys[0] = kSecClass;
  1058. values[1] = kCFBooleanTrue; /* we want a reference */
  1059. keys[1] = kSecReturnRef;
  1060. values[2] = kSecMatchLimitAll; /* kSecMatchLimitOne would be better if the
  1061. * label matching below worked correctly */
  1062. keys[2] = kSecMatchLimit;
  1063. /* identity searches need a SecPolicyRef in order to work */
  1064. values[3] = SecPolicyCreateSSL(false, NULL);
  1065. keys[3] = kSecMatchPolicy;
  1066. /* match the name of the certificate (doesn't work in macOS 10.12.1) */
  1067. values[4] = label_cf;
  1068. keys[4] = kSecAttrLabel;
  1069. query_dict = CFDictionaryCreate(NULL, (const void **)keys,
  1070. (const void **)values, 5L,
  1071. &kCFCopyStringDictionaryKeyCallBacks,
  1072. &kCFTypeDictionaryValueCallBacks);
  1073. CFRelease(values[3]);
  1074. /* Do we have a match? */
  1075. status = SecItemCopyMatching(query_dict, (CFTypeRef *) &keys_list);
  1076. /* Because kSecAttrLabel matching doesn't work with kSecClassIdentity,
  1077. * we need to find the correct identity ourselves */
  1078. if(status == noErr) {
  1079. keys_list_count = CFArrayGetCount(keys_list);
  1080. *out_cert_and_key = NULL;
  1081. status = 1;
  1082. for(i = 0; i<keys_list_count; i++) {
  1083. OSStatus err = noErr;
  1084. SecCertificateRef cert = NULL;
  1085. SecIdentityRef identity =
  1086. (SecIdentityRef) CFArrayGetValueAtIndex(keys_list, i);
  1087. err = SecIdentityCopyCertificate(identity, &cert);
  1088. if(err == noErr) {
  1089. OSStatus copy_status = noErr;
  1090. #if CURL_BUILD_IOS
  1091. common_name = SecCertificateCopySubjectSummary(cert);
  1092. #elif CURL_BUILD_MAC_10_7
  1093. copy_status = SecCertificateCopyCommonName(cert, &common_name);
  1094. #endif
  1095. if(copy_status == noErr &&
  1096. CFStringCompare(common_name, label_cf, 0) == kCFCompareEqualTo) {
  1097. CFRelease(cert);
  1098. CFRelease(common_name);
  1099. CFRetain(identity);
  1100. *out_cert_and_key = identity;
  1101. status = noErr;
  1102. break;
  1103. }
  1104. CFRelease(common_name);
  1105. }
  1106. CFRelease(cert);
  1107. }
  1108. }
  1109. if(keys_list)
  1110. CFRelease(keys_list);
  1111. CFRelease(query_dict);
  1112. CFRelease(label_cf);
  1113. }
  1114. else {
  1115. #if CURL_SUPPORT_MAC_10_6
  1116. /* On Leopard and Snow Leopard, fall back to SecKeychainSearch. */
  1117. status = CopyIdentityWithLabelOldSchool(label, out_cert_and_key);
  1118. #endif /* CURL_SUPPORT_MAC_10_6 */
  1119. }
  1120. #elif CURL_SUPPORT_MAC_10_6
  1121. /* For developers building on older cats, we have no choice but to fall back
  1122. to SecKeychainSearch. */
  1123. status = CopyIdentityWithLabelOldSchool(label, out_cert_and_key);
  1124. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  1125. return status;
  1126. }
  1127. static OSStatus CopyIdentityFromPKCS12File(const char *cPath,
  1128. const struct curl_blob *blob,
  1129. const char *cPassword,
  1130. SecIdentityRef *out_cert_and_key)
  1131. {
  1132. OSStatus status = errSecItemNotFound;
  1133. CFURLRef pkcs_url = NULL;
  1134. CFStringRef password = cPassword ? CFStringCreateWithCString(NULL,
  1135. cPassword, kCFStringEncodingUTF8) : NULL;
  1136. CFDataRef pkcs_data = NULL;
  1137. /* We can import P12 files on iOS or OS X 10.7 or later: */
  1138. /* These constants are documented as having first appeared in 10.6 but they
  1139. raise linker errors when used on that cat for some reason. */
  1140. #if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS
  1141. bool resource_imported;
  1142. if(blob) {
  1143. pkcs_data = CFDataCreate(kCFAllocatorDefault,
  1144. (const unsigned char *)blob->data, blob->len);
  1145. status = (pkcs_data != NULL) ? errSecSuccess : errSecAllocate;
  1146. resource_imported = (pkcs_data != NULL);
  1147. }
  1148. else {
  1149. pkcs_url =
  1150. CFURLCreateFromFileSystemRepresentation(NULL,
  1151. (const UInt8 *)cPath,
  1152. strlen(cPath), false);
  1153. resource_imported =
  1154. CFURLCreateDataAndPropertiesFromResource(NULL,
  1155. pkcs_url, &pkcs_data,
  1156. NULL, NULL, &status);
  1157. }
  1158. if(resource_imported) {
  1159. CFArrayRef items = NULL;
  1160. /* On iOS SecPKCS12Import will never add the client certificate to the
  1161. * Keychain.
  1162. *
  1163. * It gives us back a SecIdentityRef that we can use directly. */
  1164. #if CURL_BUILD_IOS
  1165. const void *cKeys[] = {kSecImportExportPassphrase};
  1166. const void *cValues[] = {password};
  1167. CFDictionaryRef options = CFDictionaryCreate(NULL, cKeys, cValues,
  1168. password ? 1L : 0L, NULL, NULL);
  1169. if(options) {
  1170. status = SecPKCS12Import(pkcs_data, options, &items);
  1171. CFRelease(options);
  1172. }
  1173. /* On macOS SecPKCS12Import will always add the client certificate to
  1174. * the Keychain.
  1175. *
  1176. * As this doesn't match iOS, and apps may not want to see their client
  1177. * certificate saved in the user's keychain, we use SecItemImport
  1178. * with a NULL keychain to avoid importing it.
  1179. *
  1180. * This returns a SecCertificateRef from which we can construct a
  1181. * SecIdentityRef.
  1182. */
  1183. #elif CURL_BUILD_MAC_10_7
  1184. SecItemImportExportKeyParameters keyParams;
  1185. SecExternalFormat inputFormat = kSecFormatPKCS12;
  1186. SecExternalItemType inputType = kSecItemTypeCertificate;
  1187. memset(&keyParams, 0x00, sizeof(keyParams));
  1188. keyParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
  1189. keyParams.passphrase = password;
  1190. status = SecItemImport(pkcs_data, NULL, &inputFormat, &inputType,
  1191. 0, &keyParams, NULL, &items);
  1192. #endif
  1193. /* Extract the SecIdentityRef */
  1194. if(status == errSecSuccess && items && CFArrayGetCount(items)) {
  1195. CFIndex i, count;
  1196. count = CFArrayGetCount(items);
  1197. for(i = 0; i < count; i++) {
  1198. CFTypeRef item = (CFTypeRef) CFArrayGetValueAtIndex(items, i);
  1199. CFTypeID itemID = CFGetTypeID(item);
  1200. if(itemID == CFDictionaryGetTypeID()) {
  1201. CFTypeRef identity = (CFTypeRef) CFDictionaryGetValue(
  1202. (CFDictionaryRef) item,
  1203. kSecImportItemIdentity);
  1204. CFRetain(identity);
  1205. *out_cert_and_key = (SecIdentityRef) identity;
  1206. break;
  1207. }
  1208. #if CURL_BUILD_MAC_10_7
  1209. else if(itemID == SecCertificateGetTypeID()) {
  1210. status = SecIdentityCreateWithCertificate(NULL,
  1211. (SecCertificateRef) item,
  1212. out_cert_and_key);
  1213. break;
  1214. }
  1215. #endif
  1216. }
  1217. }
  1218. if(items)
  1219. CFRelease(items);
  1220. CFRelease(pkcs_data);
  1221. }
  1222. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  1223. if(password)
  1224. CFRelease(password);
  1225. if(pkcs_url)
  1226. CFRelease(pkcs_url);
  1227. return status;
  1228. }
  1229. /* This code was borrowed from nss.c, with some modifications:
  1230. * Determine whether the nickname passed in is a filename that needs to
  1231. * be loaded as a PEM or a regular NSS nickname.
  1232. *
  1233. * returns 1 for a file
  1234. * returns 0 for not a file
  1235. */
  1236. CF_INLINE bool is_file(const char *filename)
  1237. {
  1238. struct_stat st;
  1239. if(!filename)
  1240. return false;
  1241. if(stat(filename, &st) == 0)
  1242. return S_ISREG(st.st_mode);
  1243. return false;
  1244. }
  1245. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1246. static CURLcode sectransp_version_from_curl(SSLProtocol *darwinver,
  1247. long ssl_version)
  1248. {
  1249. switch(ssl_version) {
  1250. case CURL_SSLVERSION_TLSv1_0:
  1251. *darwinver = kTLSProtocol1;
  1252. return CURLE_OK;
  1253. case CURL_SSLVERSION_TLSv1_1:
  1254. *darwinver = kTLSProtocol11;
  1255. return CURLE_OK;
  1256. case CURL_SSLVERSION_TLSv1_2:
  1257. *darwinver = kTLSProtocol12;
  1258. return CURLE_OK;
  1259. case CURL_SSLVERSION_TLSv1_3:
  1260. /* TLS 1.3 support first appeared in iOS 11 and macOS 10.13 */
  1261. #if (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && HAVE_BUILTIN_AVAILABLE == 1
  1262. if(__builtin_available(macOS 10.13, iOS 11.0, *)) {
  1263. *darwinver = kTLSProtocol13;
  1264. return CURLE_OK;
  1265. }
  1266. #endif /* (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) &&
  1267. HAVE_BUILTIN_AVAILABLE == 1 */
  1268. break;
  1269. }
  1270. return CURLE_SSL_CONNECT_ERROR;
  1271. }
  1272. #endif
  1273. static CURLcode
  1274. set_ssl_version_min_max(struct Curl_easy *data, struct connectdata *conn,
  1275. int sockindex)
  1276. {
  1277. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1278. struct ssl_backend_data *backend = connssl->backend;
  1279. long ssl_version = SSL_CONN_CONFIG(version);
  1280. long ssl_version_max = SSL_CONN_CONFIG(version_max);
  1281. long max_supported_version_by_os;
  1282. DEBUGASSERT(backend);
  1283. /* macOS 10.5-10.7 supported TLS 1.0 only.
  1284. macOS 10.8 and later, and iOS 5 and later, added TLS 1.1 and 1.2.
  1285. macOS 10.13 and later, and iOS 11 and later, added TLS 1.3. */
  1286. #if (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && HAVE_BUILTIN_AVAILABLE == 1
  1287. if(__builtin_available(macOS 10.13, iOS 11.0, *)) {
  1288. max_supported_version_by_os = CURL_SSLVERSION_MAX_TLSv1_3;
  1289. }
  1290. else {
  1291. max_supported_version_by_os = CURL_SSLVERSION_MAX_TLSv1_2;
  1292. }
  1293. #else
  1294. max_supported_version_by_os = CURL_SSLVERSION_MAX_TLSv1_2;
  1295. #endif /* (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) &&
  1296. HAVE_BUILTIN_AVAILABLE == 1 */
  1297. switch(ssl_version) {
  1298. case CURL_SSLVERSION_DEFAULT:
  1299. case CURL_SSLVERSION_TLSv1:
  1300. ssl_version = CURL_SSLVERSION_TLSv1_0;
  1301. break;
  1302. }
  1303. switch(ssl_version_max) {
  1304. case CURL_SSLVERSION_MAX_NONE:
  1305. case CURL_SSLVERSION_MAX_DEFAULT:
  1306. ssl_version_max = max_supported_version_by_os;
  1307. break;
  1308. }
  1309. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1310. if(SSLSetProtocolVersionMax) {
  1311. SSLProtocol darwin_ver_min = kTLSProtocol1;
  1312. SSLProtocol darwin_ver_max = kTLSProtocol1;
  1313. CURLcode result = sectransp_version_from_curl(&darwin_ver_min,
  1314. ssl_version);
  1315. if(result) {
  1316. failf(data, "unsupported min version passed via CURLOPT_SSLVERSION");
  1317. return result;
  1318. }
  1319. result = sectransp_version_from_curl(&darwin_ver_max,
  1320. ssl_version_max >> 16);
  1321. if(result) {
  1322. failf(data, "unsupported max version passed via CURLOPT_SSLVERSION");
  1323. return result;
  1324. }
  1325. (void)SSLSetProtocolVersionMin(backend->ssl_ctx, darwin_ver_min);
  1326. (void)SSLSetProtocolVersionMax(backend->ssl_ctx, darwin_ver_max);
  1327. return result;
  1328. }
  1329. else {
  1330. #if CURL_SUPPORT_MAC_10_8
  1331. long i = ssl_version;
  1332. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1333. kSSLProtocolAll,
  1334. false);
  1335. for(; i <= (ssl_version_max >> 16); i++) {
  1336. switch(i) {
  1337. case CURL_SSLVERSION_TLSv1_0:
  1338. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1339. kTLSProtocol1,
  1340. true);
  1341. break;
  1342. case CURL_SSLVERSION_TLSv1_1:
  1343. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1344. kTLSProtocol11,
  1345. true);
  1346. break;
  1347. case CURL_SSLVERSION_TLSv1_2:
  1348. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1349. kTLSProtocol12,
  1350. true);
  1351. break;
  1352. case CURL_SSLVERSION_TLSv1_3:
  1353. failf(data, "Your version of the OS does not support TLSv1.3");
  1354. return CURLE_SSL_CONNECT_ERROR;
  1355. }
  1356. }
  1357. return CURLE_OK;
  1358. #endif /* CURL_SUPPORT_MAC_10_8 */
  1359. }
  1360. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  1361. failf(data, "Secure Transport: cannot set SSL protocol");
  1362. return CURLE_SSL_CONNECT_ERROR;
  1363. }
  1364. static bool is_cipher_suite_strong(SSLCipherSuite suite_num)
  1365. {
  1366. for(size_t i = 0; i < NUM_OF_CIPHERS; ++i) {
  1367. if(ciphertable[i].num == suite_num) {
  1368. return !ciphertable[i].weak;
  1369. }
  1370. }
  1371. /* If the cipher is not in our list, assume it is a new one
  1372. and therefore strong. Previous implementation was the same,
  1373. if cipher suite is not in the list, it was considered strong enough */
  1374. return true;
  1375. }
  1376. static bool is_separator(char c)
  1377. {
  1378. /* Return whether character is a cipher list separator. */
  1379. switch(c) {
  1380. case ' ':
  1381. case '\t':
  1382. case ':':
  1383. case ',':
  1384. case ';':
  1385. return true;
  1386. }
  1387. return false;
  1388. }
  1389. static CURLcode sectransp_set_default_ciphers(struct Curl_easy *data,
  1390. SSLContextRef ssl_ctx)
  1391. {
  1392. size_t all_ciphers_count = 0UL, allowed_ciphers_count = 0UL, i;
  1393. SSLCipherSuite *all_ciphers = NULL, *allowed_ciphers = NULL;
  1394. OSStatus err = noErr;
  1395. #if CURL_BUILD_MAC
  1396. int darwinver_maj = 0, darwinver_min = 0;
  1397. GetDarwinVersionNumber(&darwinver_maj, &darwinver_min);
  1398. #endif /* CURL_BUILD_MAC */
  1399. /* Disable cipher suites that ST supports but are not safe. These ciphers
  1400. are unlikely to be used in any case since ST gives other ciphers a much
  1401. higher priority, but it's probably better that we not connect at all than
  1402. to give the user a false sense of security if the server only supports
  1403. insecure ciphers. (Note: We don't care about SSLv2-only ciphers.) */
  1404. err = SSLGetNumberSupportedCiphers(ssl_ctx, &all_ciphers_count);
  1405. if(err != noErr) {
  1406. failf(data, "SSL: SSLGetNumberSupportedCiphers() failed: OSStatus %d",
  1407. err);
  1408. return CURLE_SSL_CIPHER;
  1409. }
  1410. all_ciphers = malloc(all_ciphers_count*sizeof(SSLCipherSuite));
  1411. if(!all_ciphers) {
  1412. failf(data, "SSL: Failed to allocate memory for all ciphers");
  1413. return CURLE_OUT_OF_MEMORY;
  1414. }
  1415. allowed_ciphers = malloc(all_ciphers_count*sizeof(SSLCipherSuite));
  1416. if(!allowed_ciphers) {
  1417. Curl_safefree(all_ciphers);
  1418. failf(data, "SSL: Failed to allocate memory for allowed ciphers");
  1419. return CURLE_OUT_OF_MEMORY;
  1420. }
  1421. err = SSLGetSupportedCiphers(ssl_ctx, all_ciphers,
  1422. &all_ciphers_count);
  1423. if(err != noErr) {
  1424. Curl_safefree(all_ciphers);
  1425. Curl_safefree(allowed_ciphers);
  1426. return CURLE_SSL_CIPHER;
  1427. }
  1428. for(i = 0UL ; i < all_ciphers_count ; i++) {
  1429. #if CURL_BUILD_MAC
  1430. /* There's a known bug in early versions of Mountain Lion where ST's ECC
  1431. ciphers (cipher suite 0xC001 through 0xC032) simply do not work.
  1432. Work around the problem here by disabling those ciphers if we are
  1433. running in an affected version of OS X. */
  1434. if(darwinver_maj == 12 && darwinver_min <= 3 &&
  1435. all_ciphers[i] >= 0xC001 && all_ciphers[i] <= 0xC032) {
  1436. continue;
  1437. }
  1438. #endif /* CURL_BUILD_MAC */
  1439. if(is_cipher_suite_strong(all_ciphers[i])) {
  1440. allowed_ciphers[allowed_ciphers_count++] = all_ciphers[i];
  1441. }
  1442. }
  1443. err = SSLSetEnabledCiphers(ssl_ctx, allowed_ciphers,
  1444. allowed_ciphers_count);
  1445. Curl_safefree(all_ciphers);
  1446. Curl_safefree(allowed_ciphers);
  1447. if(err != noErr) {
  1448. failf(data, "SSL: SSLSetEnabledCiphers() failed: OSStatus %d", err);
  1449. return CURLE_SSL_CIPHER;
  1450. }
  1451. return CURLE_OK;
  1452. }
  1453. static CURLcode sectransp_set_selected_ciphers(struct Curl_easy *data,
  1454. SSLContextRef ssl_ctx,
  1455. const char *ciphers)
  1456. {
  1457. size_t ciphers_count = 0;
  1458. const char *cipher_start = ciphers;
  1459. OSStatus err = noErr;
  1460. SSLCipherSuite selected_ciphers[NUM_OF_CIPHERS];
  1461. if(!ciphers)
  1462. return CURLE_OK;
  1463. while(is_separator(*ciphers)) /* Skip initial separators. */
  1464. ciphers++;
  1465. if(!*ciphers)
  1466. return CURLE_OK;
  1467. cipher_start = ciphers;
  1468. while(*cipher_start && ciphers_count < NUM_OF_CIPHERS) {
  1469. bool cipher_found = FALSE;
  1470. size_t cipher_len = 0;
  1471. const char *cipher_end = NULL;
  1472. bool tls_name = FALSE;
  1473. /* Skip separators */
  1474. while(is_separator(*cipher_start))
  1475. cipher_start++;
  1476. if(*cipher_start == '\0') {
  1477. break;
  1478. }
  1479. /* Find last position of a cipher in the ciphers string */
  1480. cipher_end = cipher_start;
  1481. while (*cipher_end != '\0' && !is_separator(*cipher_end)) {
  1482. ++cipher_end;
  1483. }
  1484. /* IANA cipher names start with the TLS_ or SSL_ prefix.
  1485. If the 4th symbol of the cipher is '_' we look for a cipher in the
  1486. table by its (TLS) name.
  1487. Otherwise, we try to match cipher by an alias. */
  1488. if(cipher_start[3] == '_') {
  1489. tls_name = TRUE;
  1490. }
  1491. /* Iterate through the cipher table and look for the cipher, starting
  1492. the cipher number 0x01 because the 0x00 is not the real cipher */
  1493. cipher_len = cipher_end - cipher_start;
  1494. for(size_t i = 1; i < NUM_OF_CIPHERS; ++i) {
  1495. const char *table_cipher_name = NULL;
  1496. if(tls_name) {
  1497. table_cipher_name = ciphertable[i].name;
  1498. }
  1499. else if(ciphertable[i].alias_name) {
  1500. table_cipher_name = ciphertable[i].alias_name;
  1501. }
  1502. else {
  1503. continue;
  1504. }
  1505. /* Compare a part of the string between separators with a cipher name
  1506. in the table and make sure we matched the whole cipher name */
  1507. if(strncmp(cipher_start, table_cipher_name, cipher_len) == 0
  1508. && table_cipher_name[cipher_len] == '\0') {
  1509. selected_ciphers[ciphers_count] = ciphertable[i].num;
  1510. ++ciphers_count;
  1511. cipher_found = TRUE;
  1512. break;
  1513. }
  1514. }
  1515. if(!cipher_found) {
  1516. /* It would be more human-readable if we print the wrong cipher name
  1517. but we don't want to allocate any additional memory and copy the name
  1518. into it, then add it into logs.
  1519. Also, we do not modify an original cipher list string. We just point
  1520. to positions where cipher starts and ends in the cipher list string.
  1521. The message is a bit cryptic and longer than necessary but can be
  1522. understood by humans. */
  1523. failf(data, "SSL: cipher string \"%s\" contains unsupported cipher name"
  1524. " starting position %d and ending position %d",
  1525. ciphers,
  1526. cipher_start - ciphers,
  1527. cipher_end - ciphers);
  1528. return CURLE_SSL_CIPHER;
  1529. }
  1530. if(*cipher_end) {
  1531. cipher_start = cipher_end + 1;
  1532. }
  1533. else {
  1534. break;
  1535. }
  1536. }
  1537. /* All cipher suites in the list are found. Report to logs as-is */
  1538. infof(data, "SSL: Setting cipher suites list \"%s\"", ciphers);
  1539. err = SSLSetEnabledCiphers(ssl_ctx, selected_ciphers, ciphers_count);
  1540. if(err != noErr) {
  1541. failf(data, "SSL: SSLSetEnabledCiphers() failed: OSStatus %d", err);
  1542. return CURLE_SSL_CIPHER;
  1543. }
  1544. return CURLE_OK;
  1545. }
  1546. static CURLcode sectransp_connect_step1(struct Curl_easy *data,
  1547. struct connectdata *conn,
  1548. int sockindex)
  1549. {
  1550. curl_socket_t sockfd = conn->sock[sockindex];
  1551. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1552. struct ssl_backend_data *backend = connssl->backend;
  1553. const struct curl_blob *ssl_cablob = SSL_CONN_CONFIG(ca_info_blob);
  1554. const char * const ssl_cafile =
  1555. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  1556. (ssl_cablob ? NULL : SSL_CONN_CONFIG(CAfile));
  1557. const bool verifypeer = SSL_CONN_CONFIG(verifypeer);
  1558. char * const ssl_cert = SSL_SET_OPTION(primary.clientcert);
  1559. const struct curl_blob *ssl_cert_blob = SSL_SET_OPTION(primary.cert_blob);
  1560. bool isproxy = SSL_IS_PROXY();
  1561. const char * const hostname = SSL_HOST_NAME();
  1562. const long int port = SSL_HOST_PORT();
  1563. #ifdef ENABLE_IPV6
  1564. struct in6_addr addr;
  1565. #else
  1566. struct in_addr addr;
  1567. #endif /* ENABLE_IPV6 */
  1568. char *ciphers;
  1569. OSStatus err = noErr;
  1570. #if CURL_BUILD_MAC
  1571. int darwinver_maj = 0, darwinver_min = 0;
  1572. DEBUGASSERT(backend);
  1573. GetDarwinVersionNumber(&darwinver_maj, &darwinver_min);
  1574. #endif /* CURL_BUILD_MAC */
  1575. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1576. if(SSLCreateContext) { /* use the newer API if available */
  1577. if(backend->ssl_ctx)
  1578. CFRelease(backend->ssl_ctx);
  1579. backend->ssl_ctx = SSLCreateContext(NULL, kSSLClientSide, kSSLStreamType);
  1580. if(!backend->ssl_ctx) {
  1581. failf(data, "SSL: couldn't create a context");
  1582. return CURLE_OUT_OF_MEMORY;
  1583. }
  1584. }
  1585. else {
  1586. /* The old ST API does not exist under iOS, so don't compile it: */
  1587. #if CURL_SUPPORT_MAC_10_8
  1588. if(backend->ssl_ctx)
  1589. (void)SSLDisposeContext(backend->ssl_ctx);
  1590. err = SSLNewContext(false, &(backend->ssl_ctx));
  1591. if(err != noErr) {
  1592. failf(data, "SSL: couldn't create a context: OSStatus %d", err);
  1593. return CURLE_OUT_OF_MEMORY;
  1594. }
  1595. #endif /* CURL_SUPPORT_MAC_10_8 */
  1596. }
  1597. #else
  1598. if(backend->ssl_ctx)
  1599. (void)SSLDisposeContext(backend->ssl_ctx);
  1600. err = SSLNewContext(false, &(backend->ssl_ctx));
  1601. if(err != noErr) {
  1602. failf(data, "SSL: couldn't create a context: OSStatus %d", err);
  1603. return CURLE_OUT_OF_MEMORY;
  1604. }
  1605. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  1606. backend->ssl_write_buffered_length = 0UL; /* reset buffered write length */
  1607. /* check to see if we've been told to use an explicit SSL/TLS version */
  1608. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1609. if(SSLSetProtocolVersionMax) {
  1610. switch(conn->ssl_config.version) {
  1611. case CURL_SSLVERSION_TLSv1:
  1612. (void)SSLSetProtocolVersionMin(backend->ssl_ctx, kTLSProtocol1);
  1613. #if (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && HAVE_BUILTIN_AVAILABLE == 1
  1614. if(__builtin_available(macOS 10.13, iOS 11.0, *)) {
  1615. (void)SSLSetProtocolVersionMax(backend->ssl_ctx, kTLSProtocol13);
  1616. }
  1617. else {
  1618. (void)SSLSetProtocolVersionMax(backend->ssl_ctx, kTLSProtocol12);
  1619. }
  1620. #else
  1621. (void)SSLSetProtocolVersionMax(backend->ssl_ctx, kTLSProtocol12);
  1622. #endif /* (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) &&
  1623. HAVE_BUILTIN_AVAILABLE == 1 */
  1624. break;
  1625. case CURL_SSLVERSION_DEFAULT:
  1626. case CURL_SSLVERSION_TLSv1_0:
  1627. case CURL_SSLVERSION_TLSv1_1:
  1628. case CURL_SSLVERSION_TLSv1_2:
  1629. case CURL_SSLVERSION_TLSv1_3:
  1630. {
  1631. CURLcode result = set_ssl_version_min_max(data, conn, sockindex);
  1632. if(result != CURLE_OK)
  1633. return result;
  1634. break;
  1635. }
  1636. case CURL_SSLVERSION_SSLv3:
  1637. case CURL_SSLVERSION_SSLv2:
  1638. failf(data, "SSL versions not supported");
  1639. return CURLE_NOT_BUILT_IN;
  1640. default:
  1641. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  1642. return CURLE_SSL_CONNECT_ERROR;
  1643. }
  1644. }
  1645. else {
  1646. #if CURL_SUPPORT_MAC_10_8
  1647. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1648. kSSLProtocolAll,
  1649. false);
  1650. switch(conn->ssl_config.version) {
  1651. case CURL_SSLVERSION_DEFAULT:
  1652. case CURL_SSLVERSION_TLSv1:
  1653. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1654. kTLSProtocol1,
  1655. true);
  1656. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1657. kTLSProtocol11,
  1658. true);
  1659. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1660. kTLSProtocol12,
  1661. true);
  1662. break;
  1663. case CURL_SSLVERSION_TLSv1_0:
  1664. case CURL_SSLVERSION_TLSv1_1:
  1665. case CURL_SSLVERSION_TLSv1_2:
  1666. case CURL_SSLVERSION_TLSv1_3:
  1667. {
  1668. CURLcode result = set_ssl_version_min_max(data, conn, sockindex);
  1669. if(result != CURLE_OK)
  1670. return result;
  1671. break;
  1672. }
  1673. case CURL_SSLVERSION_SSLv3:
  1674. case CURL_SSLVERSION_SSLv2:
  1675. failf(data, "SSL versions not supported");
  1676. return CURLE_NOT_BUILT_IN;
  1677. default:
  1678. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  1679. return CURLE_SSL_CONNECT_ERROR;
  1680. }
  1681. #endif /* CURL_SUPPORT_MAC_10_8 */
  1682. }
  1683. #else
  1684. if(conn->ssl_config.version_max != CURL_SSLVERSION_MAX_NONE) {
  1685. failf(data, "Your version of the OS does not support to set maximum"
  1686. " SSL/TLS version");
  1687. return CURLE_SSL_CONNECT_ERROR;
  1688. }
  1689. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx, kSSLProtocolAll, false);
  1690. switch(conn->ssl_config.version) {
  1691. case CURL_SSLVERSION_DEFAULT:
  1692. case CURL_SSLVERSION_TLSv1:
  1693. case CURL_SSLVERSION_TLSv1_0:
  1694. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1695. kTLSProtocol1,
  1696. true);
  1697. break;
  1698. case CURL_SSLVERSION_TLSv1_1:
  1699. failf(data, "Your version of the OS does not support TLSv1.1");
  1700. return CURLE_SSL_CONNECT_ERROR;
  1701. case CURL_SSLVERSION_TLSv1_2:
  1702. failf(data, "Your version of the OS does not support TLSv1.2");
  1703. return CURLE_SSL_CONNECT_ERROR;
  1704. case CURL_SSLVERSION_TLSv1_3:
  1705. failf(data, "Your version of the OS does not support TLSv1.3");
  1706. return CURLE_SSL_CONNECT_ERROR;
  1707. case CURL_SSLVERSION_SSLv2:
  1708. case CURL_SSLVERSION_SSLv3:
  1709. failf(data, "SSL versions not supported");
  1710. return CURLE_NOT_BUILT_IN;
  1711. default:
  1712. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  1713. return CURLE_SSL_CONNECT_ERROR;
  1714. }
  1715. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  1716. #if (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && HAVE_BUILTIN_AVAILABLE == 1
  1717. if(conn->bits.tls_enable_alpn) {
  1718. if(__builtin_available(macOS 10.13.4, iOS 11, tvOS 11, *)) {
  1719. CFMutableArrayRef alpnArr = CFArrayCreateMutable(NULL, 0,
  1720. &kCFTypeArrayCallBacks);
  1721. #ifdef USE_HTTP2
  1722. if(data->state.httpwant >= CURL_HTTP_VERSION_2
  1723. #ifndef CURL_DISABLE_PROXY
  1724. && (!isproxy || !conn->bits.tunnel_proxy)
  1725. #endif
  1726. ) {
  1727. CFArrayAppendValue(alpnArr, CFSTR(ALPN_H2));
  1728. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, ALPN_H2);
  1729. }
  1730. #endif
  1731. CFArrayAppendValue(alpnArr, CFSTR(ALPN_HTTP_1_1));
  1732. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, ALPN_HTTP_1_1);
  1733. /* expects length prefixed preference ordered list of protocols in wire
  1734. * format
  1735. */
  1736. err = SSLSetALPNProtocols(backend->ssl_ctx, alpnArr);
  1737. if(err != noErr)
  1738. infof(data, "WARNING: failed to set ALPN protocols; OSStatus %d",
  1739. err);
  1740. CFRelease(alpnArr);
  1741. }
  1742. }
  1743. #endif
  1744. if(SSL_SET_OPTION(key)) {
  1745. infof(data, "WARNING: SSL: CURLOPT_SSLKEY is ignored by Secure "
  1746. "Transport. The private key must be in the Keychain.");
  1747. }
  1748. if(ssl_cert || ssl_cert_blob) {
  1749. bool is_cert_data = ssl_cert_blob != NULL;
  1750. bool is_cert_file = (!is_cert_data) && is_file(ssl_cert);
  1751. SecIdentityRef cert_and_key = NULL;
  1752. /* User wants to authenticate with a client cert. Look for it. Assume that
  1753. the user wants to use an identity loaded from the Keychain. If not, try
  1754. it as a file on disk */
  1755. if(!is_cert_data)
  1756. err = CopyIdentityWithLabel(ssl_cert, &cert_and_key);
  1757. else
  1758. err = !noErr;
  1759. if((err != noErr) && (is_cert_file || is_cert_data)) {
  1760. if(!SSL_SET_OPTION(cert_type))
  1761. infof(data, "SSL: Certificate type not set, assuming "
  1762. "PKCS#12 format.");
  1763. else if(!strcasecompare(SSL_SET_OPTION(cert_type), "P12")) {
  1764. failf(data, "SSL: The Security framework only supports "
  1765. "loading identities that are in PKCS#12 format.");
  1766. return CURLE_SSL_CERTPROBLEM;
  1767. }
  1768. err = CopyIdentityFromPKCS12File(ssl_cert, ssl_cert_blob,
  1769. SSL_SET_OPTION(key_passwd),
  1770. &cert_and_key);
  1771. }
  1772. if(err == noErr && cert_and_key) {
  1773. SecCertificateRef cert = NULL;
  1774. CFTypeRef certs_c[1];
  1775. CFArrayRef certs;
  1776. /* If we found one, print it out: */
  1777. err = SecIdentityCopyCertificate(cert_and_key, &cert);
  1778. if(err == noErr) {
  1779. char *certp;
  1780. CURLcode result = CopyCertSubject(data, cert, &certp);
  1781. if(!result) {
  1782. infof(data, "Client certificate: %s", certp);
  1783. free(certp);
  1784. }
  1785. CFRelease(cert);
  1786. if(result == CURLE_PEER_FAILED_VERIFICATION)
  1787. return CURLE_SSL_CERTPROBLEM;
  1788. if(result)
  1789. return result;
  1790. }
  1791. certs_c[0] = cert_and_key;
  1792. certs = CFArrayCreate(NULL, (const void **)certs_c, 1L,
  1793. &kCFTypeArrayCallBacks);
  1794. err = SSLSetCertificate(backend->ssl_ctx, certs);
  1795. if(certs)
  1796. CFRelease(certs);
  1797. if(err != noErr) {
  1798. failf(data, "SSL: SSLSetCertificate() failed: OSStatus %d", err);
  1799. return CURLE_SSL_CERTPROBLEM;
  1800. }
  1801. CFRelease(cert_and_key);
  1802. }
  1803. else {
  1804. const char *cert_showfilename_error =
  1805. is_cert_data ? "(memory blob)" : ssl_cert;
  1806. switch(err) {
  1807. case errSecAuthFailed: case -25264: /* errSecPkcs12VerifyFailure */
  1808. failf(data, "SSL: Incorrect password for the certificate \"%s\" "
  1809. "and its private key.", cert_showfilename_error);
  1810. break;
  1811. case -26275: /* errSecDecode */ case -25257: /* errSecUnknownFormat */
  1812. failf(data, "SSL: Couldn't make sense of the data in the "
  1813. "certificate \"%s\" and its private key.",
  1814. cert_showfilename_error);
  1815. break;
  1816. case -25260: /* errSecPassphraseRequired */
  1817. failf(data, "SSL The certificate \"%s\" requires a password.",
  1818. cert_showfilename_error);
  1819. break;
  1820. case errSecItemNotFound:
  1821. failf(data, "SSL: Can't find the certificate \"%s\" and its private "
  1822. "key in the Keychain.", cert_showfilename_error);
  1823. break;
  1824. default:
  1825. failf(data, "SSL: Can't load the certificate \"%s\" and its private "
  1826. "key: OSStatus %d", cert_showfilename_error, err);
  1827. break;
  1828. }
  1829. return CURLE_SSL_CERTPROBLEM;
  1830. }
  1831. }
  1832. /* SSL always tries to verify the peer, this only says whether it should
  1833. * fail to connect if the verification fails, or if it should continue
  1834. * anyway. In the latter case the result of the verification is checked with
  1835. * SSL_get_verify_result() below. */
  1836. #if CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS
  1837. /* Snow Leopard introduced the SSLSetSessionOption() function, but due to
  1838. a library bug with the way the kSSLSessionOptionBreakOnServerAuth flag
  1839. works, it doesn't work as expected under Snow Leopard, Lion or
  1840. Mountain Lion.
  1841. So we need to call SSLSetEnableCertVerify() on those older cats in order
  1842. to disable certificate validation if the user turned that off.
  1843. (SecureTransport will always validate the certificate chain by
  1844. default.)
  1845. Note:
  1846. Darwin 11.x.x is Lion (10.7)
  1847. Darwin 12.x.x is Mountain Lion (10.8)
  1848. Darwin 13.x.x is Mavericks (10.9)
  1849. Darwin 14.x.x is Yosemite (10.10)
  1850. Darwin 15.x.x is El Capitan (10.11)
  1851. */
  1852. #if CURL_BUILD_MAC
  1853. if(SSLSetSessionOption && darwinver_maj >= 13) {
  1854. #else
  1855. if(SSLSetSessionOption) {
  1856. #endif /* CURL_BUILD_MAC */
  1857. bool break_on_auth = !conn->ssl_config.verifypeer ||
  1858. ssl_cafile || ssl_cablob;
  1859. err = SSLSetSessionOption(backend->ssl_ctx,
  1860. kSSLSessionOptionBreakOnServerAuth,
  1861. break_on_auth);
  1862. if(err != noErr) {
  1863. failf(data, "SSL: SSLSetSessionOption() failed: OSStatus %d", err);
  1864. return CURLE_SSL_CONNECT_ERROR;
  1865. }
  1866. }
  1867. else {
  1868. #if CURL_SUPPORT_MAC_10_8
  1869. err = SSLSetEnableCertVerify(backend->ssl_ctx,
  1870. conn->ssl_config.verifypeer?true:false);
  1871. if(err != noErr) {
  1872. failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err);
  1873. return CURLE_SSL_CONNECT_ERROR;
  1874. }
  1875. #endif /* CURL_SUPPORT_MAC_10_8 */
  1876. }
  1877. #else
  1878. err = SSLSetEnableCertVerify(backend->ssl_ctx,
  1879. conn->ssl_config.verifypeer?true:false);
  1880. if(err != noErr) {
  1881. failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err);
  1882. return CURLE_SSL_CONNECT_ERROR;
  1883. }
  1884. #endif /* CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS */
  1885. if((ssl_cafile || ssl_cablob) && verifypeer) {
  1886. bool is_cert_data = ssl_cablob != NULL;
  1887. bool is_cert_file = (!is_cert_data) && is_file(ssl_cafile);
  1888. if(!(is_cert_file || is_cert_data)) {
  1889. failf(data, "SSL: can't load CA certificate file %s",
  1890. ssl_cafile ? ssl_cafile : "(blob memory)");
  1891. return CURLE_SSL_CACERT_BADFILE;
  1892. }
  1893. }
  1894. /* Configure hostname check. SNI is used if available.
  1895. * Both hostname check and SNI require SSLSetPeerDomainName().
  1896. * Also: the verifyhost setting influences SNI usage */
  1897. if(conn->ssl_config.verifyhost) {
  1898. size_t snilen;
  1899. char *snihost = Curl_ssl_snihost(data, hostname, &snilen);
  1900. if(!snihost) {
  1901. failf(data, "Failed to set SNI");
  1902. return CURLE_SSL_CONNECT_ERROR;
  1903. }
  1904. err = SSLSetPeerDomainName(backend->ssl_ctx, snihost, snilen);
  1905. if(err != noErr) {
  1906. failf(data, "SSL: SSLSetPeerDomainName() failed: OSStatus %d",
  1907. err);
  1908. return CURLE_SSL_CONNECT_ERROR;
  1909. }
  1910. if((Curl_inet_pton(AF_INET, hostname, &addr))
  1911. #ifdef ENABLE_IPV6
  1912. || (Curl_inet_pton(AF_INET6, hostname, &addr))
  1913. #endif
  1914. ) {
  1915. infof(data, "WARNING: using IP address, SNI is being disabled by "
  1916. "the OS.");
  1917. }
  1918. }
  1919. else {
  1920. infof(data, "WARNING: disabling hostname validation also disables SNI.");
  1921. }
  1922. ciphers = SSL_CONN_CONFIG(cipher_list);
  1923. if(ciphers) {
  1924. err = sectransp_set_selected_ciphers(data, backend->ssl_ctx, ciphers);
  1925. }
  1926. else {
  1927. err = sectransp_set_default_ciphers(data, backend->ssl_ctx);
  1928. }
  1929. if(err != noErr) {
  1930. failf(data, "SSL: Unable to set ciphers for SSL/TLS handshake. "
  1931. "Error code: %d", err);
  1932. return CURLE_SSL_CIPHER;
  1933. }
  1934. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  1935. /* We want to enable 1/n-1 when using a CBC cipher unless the user
  1936. specifically doesn't want us doing that: */
  1937. if(SSLSetSessionOption) {
  1938. SSLSetSessionOption(backend->ssl_ctx, kSSLSessionOptionSendOneByteRecord,
  1939. !SSL_SET_OPTION(enable_beast));
  1940. SSLSetSessionOption(backend->ssl_ctx, kSSLSessionOptionFalseStart,
  1941. data->set.ssl.falsestart); /* false start support */
  1942. }
  1943. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  1944. /* Check if there's a cached ID we can/should use here! */
  1945. if(SSL_SET_OPTION(primary.sessionid)) {
  1946. char *ssl_sessionid;
  1947. size_t ssl_sessionid_len;
  1948. Curl_ssl_sessionid_lock(data);
  1949. if(!Curl_ssl_getsessionid(data, conn, isproxy, (void **)&ssl_sessionid,
  1950. &ssl_sessionid_len, sockindex)) {
  1951. /* we got a session id, use it! */
  1952. err = SSLSetPeerID(backend->ssl_ctx, ssl_sessionid, ssl_sessionid_len);
  1953. Curl_ssl_sessionid_unlock(data);
  1954. if(err != noErr) {
  1955. failf(data, "SSL: SSLSetPeerID() failed: OSStatus %d", err);
  1956. return CURLE_SSL_CONNECT_ERROR;
  1957. }
  1958. /* Informational message */
  1959. infof(data, "SSL re-using session ID");
  1960. }
  1961. /* If there isn't one, then let's make one up! This has to be done prior
  1962. to starting the handshake. */
  1963. else {
  1964. CURLcode result;
  1965. ssl_sessionid =
  1966. aprintf("%s:%d:%d:%s:%ld",
  1967. ssl_cafile ? ssl_cafile : "(blob memory)",
  1968. verifypeer, SSL_CONN_CONFIG(verifyhost), hostname, port);
  1969. ssl_sessionid_len = strlen(ssl_sessionid);
  1970. err = SSLSetPeerID(backend->ssl_ctx, ssl_sessionid, ssl_sessionid_len);
  1971. if(err != noErr) {
  1972. Curl_ssl_sessionid_unlock(data);
  1973. failf(data, "SSL: SSLSetPeerID() failed: OSStatus %d", err);
  1974. return CURLE_SSL_CONNECT_ERROR;
  1975. }
  1976. result = Curl_ssl_addsessionid(data, conn, isproxy, ssl_sessionid,
  1977. ssl_sessionid_len, sockindex, NULL);
  1978. Curl_ssl_sessionid_unlock(data);
  1979. if(result) {
  1980. failf(data, "failed to store ssl session");
  1981. return result;
  1982. }
  1983. }
  1984. }
  1985. err = SSLSetIOFuncs(backend->ssl_ctx, SocketRead, SocketWrite);
  1986. if(err != noErr) {
  1987. failf(data, "SSL: SSLSetIOFuncs() failed: OSStatus %d", err);
  1988. return CURLE_SSL_CONNECT_ERROR;
  1989. }
  1990. /* pass the raw socket into the SSL layers */
  1991. /* We need to store the FD in a constant memory address, because
  1992. * SSLSetConnection() will not copy that address. I've found that
  1993. * conn->sock[sockindex] may change on its own. */
  1994. backend->ssl_sockfd = sockfd;
  1995. err = SSLSetConnection(backend->ssl_ctx, connssl);
  1996. if(err != noErr) {
  1997. failf(data, "SSL: SSLSetConnection() failed: %d", err);
  1998. return CURLE_SSL_CONNECT_ERROR;
  1999. }
  2000. connssl->connecting_state = ssl_connect_2;
  2001. return CURLE_OK;
  2002. }
  2003. static long pem_to_der(const char *in, unsigned char **out, size_t *outlen)
  2004. {
  2005. char *sep_start, *sep_end, *cert_start, *cert_end;
  2006. size_t i, j, err;
  2007. size_t len;
  2008. unsigned char *b64;
  2009. /* Jump through the separators at the beginning of the certificate. */
  2010. sep_start = strstr(in, "-----");
  2011. if(!sep_start)
  2012. return 0;
  2013. cert_start = strstr(sep_start + 1, "-----");
  2014. if(!cert_start)
  2015. return -1;
  2016. cert_start += 5;
  2017. /* Find separator after the end of the certificate. */
  2018. cert_end = strstr(cert_start, "-----");
  2019. if(!cert_end)
  2020. return -1;
  2021. sep_end = strstr(cert_end + 1, "-----");
  2022. if(!sep_end)
  2023. return -1;
  2024. sep_end += 5;
  2025. len = cert_end - cert_start;
  2026. b64 = malloc(len + 1);
  2027. if(!b64)
  2028. return -1;
  2029. /* Create base64 string without linefeeds. */
  2030. for(i = 0, j = 0; i < len; i++) {
  2031. if(cert_start[i] != '\r' && cert_start[i] != '\n')
  2032. b64[j++] = cert_start[i];
  2033. }
  2034. b64[j] = '\0';
  2035. err = Curl_base64_decode((const char *)b64, out, outlen);
  2036. free(b64);
  2037. if(err) {
  2038. free(*out);
  2039. return -1;
  2040. }
  2041. return sep_end - in;
  2042. }
  2043. static int read_cert(const char *file, unsigned char **out, size_t *outlen)
  2044. {
  2045. int fd;
  2046. ssize_t n, len = 0, cap = 512;
  2047. unsigned char buf[512], *data;
  2048. fd = open(file, 0);
  2049. if(fd < 0)
  2050. return -1;
  2051. data = malloc(cap);
  2052. if(!data) {
  2053. close(fd);
  2054. return -1;
  2055. }
  2056. for(;;) {
  2057. n = read(fd, buf, sizeof(buf));
  2058. if(n < 0) {
  2059. close(fd);
  2060. free(data);
  2061. return -1;
  2062. }
  2063. else if(n == 0) {
  2064. close(fd);
  2065. break;
  2066. }
  2067. if(len + n >= cap) {
  2068. cap *= 2;
  2069. data = Curl_saferealloc(data, cap);
  2070. if(!data) {
  2071. close(fd);
  2072. return -1;
  2073. }
  2074. }
  2075. memcpy(data + len, buf, n);
  2076. len += n;
  2077. }
  2078. data[len] = '\0';
  2079. *out = data;
  2080. *outlen = len;
  2081. return 0;
  2082. }
  2083. static int append_cert_to_array(struct Curl_easy *data,
  2084. const unsigned char *buf, size_t buflen,
  2085. CFMutableArrayRef array)
  2086. {
  2087. CFDataRef certdata = CFDataCreate(kCFAllocatorDefault, buf, buflen);
  2088. char *certp;
  2089. CURLcode result;
  2090. if(!certdata) {
  2091. failf(data, "SSL: failed to allocate array for CA certificate");
  2092. return CURLE_OUT_OF_MEMORY;
  2093. }
  2094. SecCertificateRef cacert =
  2095. SecCertificateCreateWithData(kCFAllocatorDefault, certdata);
  2096. CFRelease(certdata);
  2097. if(!cacert) {
  2098. failf(data, "SSL: failed to create SecCertificate from CA certificate");
  2099. return CURLE_SSL_CACERT_BADFILE;
  2100. }
  2101. /* Check if cacert is valid. */
  2102. result = CopyCertSubject(data, cacert, &certp);
  2103. switch(result) {
  2104. case CURLE_OK:
  2105. break;
  2106. case CURLE_PEER_FAILED_VERIFICATION:
  2107. return CURLE_SSL_CACERT_BADFILE;
  2108. case CURLE_OUT_OF_MEMORY:
  2109. default:
  2110. return result;
  2111. }
  2112. free(certp);
  2113. CFArrayAppendValue(array, cacert);
  2114. CFRelease(cacert);
  2115. return CURLE_OK;
  2116. }
  2117. static CURLcode verify_cert_buf(struct Curl_easy *data,
  2118. const unsigned char *certbuf, size_t buflen,
  2119. SSLContextRef ctx)
  2120. {
  2121. int n = 0, rc;
  2122. long res;
  2123. unsigned char *der;
  2124. size_t derlen, offset = 0;
  2125. /*
  2126. * Certbuf now contains the contents of the certificate file, which can be
  2127. * - a single DER certificate,
  2128. * - a single PEM certificate or
  2129. * - a bunch of PEM certificates (certificate bundle).
  2130. *
  2131. * Go through certbuf, and convert any PEM certificate in it into DER
  2132. * format.
  2133. */
  2134. CFMutableArrayRef array = CFArrayCreateMutable(kCFAllocatorDefault, 0,
  2135. &kCFTypeArrayCallBacks);
  2136. if(!array) {
  2137. failf(data, "SSL: out of memory creating CA certificate array");
  2138. return CURLE_OUT_OF_MEMORY;
  2139. }
  2140. while(offset < buflen) {
  2141. n++;
  2142. /*
  2143. * Check if the certificate is in PEM format, and convert it to DER. If
  2144. * this fails, we assume the certificate is in DER format.
  2145. */
  2146. res = pem_to_der((const char *)certbuf + offset, &der, &derlen);
  2147. if(res < 0) {
  2148. CFRelease(array);
  2149. failf(data, "SSL: invalid CA certificate #%d (offset %zu) in bundle",
  2150. n, offset);
  2151. return CURLE_SSL_CACERT_BADFILE;
  2152. }
  2153. offset += res;
  2154. if(res == 0 && offset == 0) {
  2155. /* This is not a PEM file, probably a certificate in DER format. */
  2156. rc = append_cert_to_array(data, certbuf, buflen, array);
  2157. if(rc != CURLE_OK) {
  2158. CFRelease(array);
  2159. return rc;
  2160. }
  2161. break;
  2162. }
  2163. else if(res == 0) {
  2164. /* No more certificates in the bundle. */
  2165. break;
  2166. }
  2167. rc = append_cert_to_array(data, der, derlen, array);
  2168. free(der);
  2169. if(rc != CURLE_OK) {
  2170. CFRelease(array);
  2171. return rc;
  2172. }
  2173. }
  2174. SecTrustRef trust;
  2175. OSStatus ret = SSLCopyPeerTrust(ctx, &trust);
  2176. if(!trust) {
  2177. failf(data, "SSL: error getting certificate chain");
  2178. CFRelease(array);
  2179. return CURLE_PEER_FAILED_VERIFICATION;
  2180. }
  2181. else if(ret != noErr) {
  2182. CFRelease(array);
  2183. failf(data, "SSLCopyPeerTrust() returned error %d", ret);
  2184. return CURLE_PEER_FAILED_VERIFICATION;
  2185. }
  2186. ret = SecTrustSetAnchorCertificates(trust, array);
  2187. if(ret != noErr) {
  2188. CFRelease(array);
  2189. CFRelease(trust);
  2190. failf(data, "SecTrustSetAnchorCertificates() returned error %d", ret);
  2191. return CURLE_PEER_FAILED_VERIFICATION;
  2192. }
  2193. ret = SecTrustSetAnchorCertificatesOnly(trust, true);
  2194. if(ret != noErr) {
  2195. CFRelease(array);
  2196. CFRelease(trust);
  2197. failf(data, "SecTrustSetAnchorCertificatesOnly() returned error %d", ret);
  2198. return CURLE_PEER_FAILED_VERIFICATION;
  2199. }
  2200. SecTrustResultType trust_eval = 0;
  2201. ret = SecTrustEvaluate(trust, &trust_eval);
  2202. CFRelease(array);
  2203. CFRelease(trust);
  2204. if(ret != noErr) {
  2205. failf(data, "SecTrustEvaluate() returned error %d", ret);
  2206. return CURLE_PEER_FAILED_VERIFICATION;
  2207. }
  2208. switch(trust_eval) {
  2209. case kSecTrustResultUnspecified:
  2210. case kSecTrustResultProceed:
  2211. return CURLE_OK;
  2212. case kSecTrustResultRecoverableTrustFailure:
  2213. case kSecTrustResultDeny:
  2214. default:
  2215. failf(data, "SSL: certificate verification failed (result: %d)",
  2216. trust_eval);
  2217. return CURLE_PEER_FAILED_VERIFICATION;
  2218. }
  2219. }
  2220. static CURLcode verify_cert(struct Curl_easy *data, const char *cafile,
  2221. const struct curl_blob *ca_info_blob,
  2222. SSLContextRef ctx)
  2223. {
  2224. int result;
  2225. unsigned char *certbuf;
  2226. size_t buflen;
  2227. if(ca_info_blob) {
  2228. certbuf = (unsigned char *)malloc(ca_info_blob->len + 1);
  2229. if(!certbuf) {
  2230. return CURLE_OUT_OF_MEMORY;
  2231. }
  2232. buflen = ca_info_blob->len;
  2233. memcpy(certbuf, ca_info_blob->data, ca_info_blob->len);
  2234. certbuf[ca_info_blob->len]='\0';
  2235. }
  2236. else if(cafile) {
  2237. if(read_cert(cafile, &certbuf, &buflen) < 0) {
  2238. failf(data, "SSL: failed to read or invalid CA certificate");
  2239. return CURLE_SSL_CACERT_BADFILE;
  2240. }
  2241. }
  2242. else
  2243. return CURLE_SSL_CACERT_BADFILE;
  2244. result = verify_cert_buf(data, certbuf, buflen, ctx);
  2245. free(certbuf);
  2246. return result;
  2247. }
  2248. #ifdef SECTRANSP_PINNEDPUBKEY
  2249. static CURLcode pkp_pin_peer_pubkey(struct Curl_easy *data,
  2250. SSLContextRef ctx,
  2251. const char *pinnedpubkey)
  2252. { /* Scratch */
  2253. size_t pubkeylen, realpubkeylen, spkiHeaderLength = 24;
  2254. unsigned char *pubkey = NULL, *realpubkey = NULL;
  2255. const unsigned char *spkiHeader = NULL;
  2256. CFDataRef publicKeyBits = NULL;
  2257. /* Result is returned to caller */
  2258. CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  2259. /* if a path wasn't specified, don't pin */
  2260. if(!pinnedpubkey)
  2261. return CURLE_OK;
  2262. if(!ctx)
  2263. return result;
  2264. do {
  2265. SecTrustRef trust;
  2266. OSStatus ret = SSLCopyPeerTrust(ctx, &trust);
  2267. if(ret != noErr || !trust)
  2268. break;
  2269. SecKeyRef keyRef = SecTrustCopyPublicKey(trust);
  2270. CFRelease(trust);
  2271. if(!keyRef)
  2272. break;
  2273. #ifdef SECTRANSP_PINNEDPUBKEY_V1
  2274. publicKeyBits = SecKeyCopyExternalRepresentation(keyRef, NULL);
  2275. CFRelease(keyRef);
  2276. if(!publicKeyBits)
  2277. break;
  2278. #elif SECTRANSP_PINNEDPUBKEY_V2
  2279. OSStatus success = SecItemExport(keyRef, kSecFormatOpenSSL, 0, NULL,
  2280. &publicKeyBits);
  2281. CFRelease(keyRef);
  2282. if(success != errSecSuccess || !publicKeyBits)
  2283. break;
  2284. #endif /* SECTRANSP_PINNEDPUBKEY_V2 */
  2285. pubkeylen = CFDataGetLength(publicKeyBits);
  2286. pubkey = (unsigned char *)CFDataGetBytePtr(publicKeyBits);
  2287. switch(pubkeylen) {
  2288. case 526:
  2289. /* 4096 bit RSA pubkeylen == 526 */
  2290. spkiHeader = rsa4096SpkiHeader;
  2291. break;
  2292. case 270:
  2293. /* 2048 bit RSA pubkeylen == 270 */
  2294. spkiHeader = rsa2048SpkiHeader;
  2295. break;
  2296. #ifdef SECTRANSP_PINNEDPUBKEY_V1
  2297. case 65:
  2298. /* ecDSA secp256r1 pubkeylen == 65 */
  2299. spkiHeader = ecDsaSecp256r1SpkiHeader;
  2300. spkiHeaderLength = 26;
  2301. break;
  2302. case 97:
  2303. /* ecDSA secp384r1 pubkeylen == 97 */
  2304. spkiHeader = ecDsaSecp384r1SpkiHeader;
  2305. spkiHeaderLength = 23;
  2306. break;
  2307. default:
  2308. infof(data, "SSL: unhandled public key length: %d", pubkeylen);
  2309. #elif SECTRANSP_PINNEDPUBKEY_V2
  2310. default:
  2311. /* ecDSA secp256r1 pubkeylen == 91 header already included?
  2312. * ecDSA secp384r1 header already included too
  2313. * we assume rest of algorithms do same, so do nothing
  2314. */
  2315. result = Curl_pin_peer_pubkey(data, pinnedpubkey, pubkey,
  2316. pubkeylen);
  2317. #endif /* SECTRANSP_PINNEDPUBKEY_V2 */
  2318. continue; /* break from loop */
  2319. }
  2320. realpubkeylen = pubkeylen + spkiHeaderLength;
  2321. realpubkey = malloc(realpubkeylen);
  2322. if(!realpubkey)
  2323. break;
  2324. memcpy(realpubkey, spkiHeader, spkiHeaderLength);
  2325. memcpy(realpubkey + spkiHeaderLength, pubkey, pubkeylen);
  2326. result = Curl_pin_peer_pubkey(data, pinnedpubkey, realpubkey,
  2327. realpubkeylen);
  2328. } while(0);
  2329. Curl_safefree(realpubkey);
  2330. if(publicKeyBits)
  2331. CFRelease(publicKeyBits);
  2332. return result;
  2333. }
  2334. #endif /* SECTRANSP_PINNEDPUBKEY */
  2335. static CURLcode
  2336. sectransp_connect_step2(struct Curl_easy *data, struct connectdata *conn,
  2337. int sockindex)
  2338. {
  2339. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  2340. struct ssl_backend_data *backend = connssl->backend;
  2341. OSStatus err;
  2342. SSLCipherSuite cipher;
  2343. SSLProtocol protocol = 0;
  2344. const char * const hostname = SSL_HOST_NAME();
  2345. DEBUGASSERT(ssl_connect_2 == connssl->connecting_state
  2346. || ssl_connect_2_reading == connssl->connecting_state
  2347. || ssl_connect_2_writing == connssl->connecting_state);
  2348. DEBUGASSERT(backend);
  2349. /* Here goes nothing: */
  2350. err = SSLHandshake(backend->ssl_ctx);
  2351. if(err != noErr) {
  2352. switch(err) {
  2353. case errSSLWouldBlock: /* they're not done with us yet */
  2354. connssl->connecting_state = backend->ssl_direction ?
  2355. ssl_connect_2_writing : ssl_connect_2_reading;
  2356. return CURLE_OK;
  2357. /* The below is errSSLServerAuthCompleted; it's not defined in
  2358. Leopard's headers */
  2359. case -9841:
  2360. if((SSL_CONN_CONFIG(CAfile) || SSL_CONN_CONFIG(ca_info_blob)) &&
  2361. SSL_CONN_CONFIG(verifypeer)) {
  2362. CURLcode result = verify_cert(data, SSL_CONN_CONFIG(CAfile),
  2363. SSL_CONN_CONFIG(ca_info_blob),
  2364. backend->ssl_ctx);
  2365. if(result)
  2366. return result;
  2367. }
  2368. /* the documentation says we need to call SSLHandshake() again */
  2369. return sectransp_connect_step2(data, conn, sockindex);
  2370. /* Problem with encrypt / decrypt */
  2371. case errSSLPeerDecodeError:
  2372. failf(data, "Decode failed");
  2373. break;
  2374. case errSSLDecryptionFail:
  2375. case errSSLPeerDecryptionFail:
  2376. failf(data, "Decryption failed");
  2377. break;
  2378. case errSSLPeerDecryptError:
  2379. failf(data, "A decryption error occurred");
  2380. break;
  2381. case errSSLBadCipherSuite:
  2382. failf(data, "A bad SSL cipher suite was encountered");
  2383. break;
  2384. case errSSLCrypto:
  2385. failf(data, "An underlying cryptographic error was encountered");
  2386. break;
  2387. #if CURL_BUILD_MAC_10_11 || CURL_BUILD_IOS_9
  2388. case errSSLWeakPeerEphemeralDHKey:
  2389. failf(data, "Indicates a weak ephemeral Diffie-Hellman key");
  2390. break;
  2391. #endif
  2392. /* Problem with the message record validation */
  2393. case errSSLBadRecordMac:
  2394. case errSSLPeerBadRecordMac:
  2395. failf(data, "A record with a bad message authentication code (MAC) "
  2396. "was encountered");
  2397. break;
  2398. case errSSLRecordOverflow:
  2399. case errSSLPeerRecordOverflow:
  2400. failf(data, "A record overflow occurred");
  2401. break;
  2402. /* Problem with zlib decompression */
  2403. case errSSLPeerDecompressFail:
  2404. failf(data, "Decompression failed");
  2405. break;
  2406. /* Problem with access */
  2407. case errSSLPeerAccessDenied:
  2408. failf(data, "Access was denied");
  2409. break;
  2410. case errSSLPeerInsufficientSecurity:
  2411. failf(data, "There is insufficient security for this operation");
  2412. break;
  2413. /* These are all certificate problems with the server: */
  2414. case errSSLXCertChainInvalid:
  2415. failf(data, "SSL certificate problem: Invalid certificate chain");
  2416. return CURLE_PEER_FAILED_VERIFICATION;
  2417. case errSSLUnknownRootCert:
  2418. failf(data, "SSL certificate problem: Untrusted root certificate");
  2419. return CURLE_PEER_FAILED_VERIFICATION;
  2420. case errSSLNoRootCert:
  2421. failf(data, "SSL certificate problem: No root certificate");
  2422. return CURLE_PEER_FAILED_VERIFICATION;
  2423. case errSSLCertNotYetValid:
  2424. failf(data, "SSL certificate problem: The certificate chain had a "
  2425. "certificate that is not yet valid");
  2426. return CURLE_PEER_FAILED_VERIFICATION;
  2427. case errSSLCertExpired:
  2428. case errSSLPeerCertExpired:
  2429. failf(data, "SSL certificate problem: Certificate chain had an "
  2430. "expired certificate");
  2431. return CURLE_PEER_FAILED_VERIFICATION;
  2432. case errSSLBadCert:
  2433. case errSSLPeerBadCert:
  2434. failf(data, "SSL certificate problem: Couldn't understand the server "
  2435. "certificate format");
  2436. return CURLE_PEER_FAILED_VERIFICATION;
  2437. case errSSLPeerUnsupportedCert:
  2438. failf(data, "SSL certificate problem: An unsupported certificate "
  2439. "format was encountered");
  2440. return CURLE_PEER_FAILED_VERIFICATION;
  2441. case errSSLPeerCertRevoked:
  2442. failf(data, "SSL certificate problem: The certificate was revoked");
  2443. return CURLE_PEER_FAILED_VERIFICATION;
  2444. case errSSLPeerCertUnknown:
  2445. failf(data, "SSL certificate problem: The certificate is unknown");
  2446. return CURLE_PEER_FAILED_VERIFICATION;
  2447. /* These are all certificate problems with the client: */
  2448. case errSecAuthFailed:
  2449. failf(data, "SSL authentication failed");
  2450. break;
  2451. case errSSLPeerHandshakeFail:
  2452. failf(data, "SSL peer handshake failed, the server most likely "
  2453. "requires a client certificate to connect");
  2454. break;
  2455. case errSSLPeerUnknownCA:
  2456. failf(data, "SSL server rejected the client certificate due to "
  2457. "the certificate being signed by an unknown certificate "
  2458. "authority");
  2459. break;
  2460. /* This error is raised if the server's cert didn't match the server's
  2461. host name: */
  2462. case errSSLHostNameMismatch:
  2463. failf(data, "SSL certificate peer verification failed, the "
  2464. "certificate did not match \"%s\"\n", conn->host.dispname);
  2465. return CURLE_PEER_FAILED_VERIFICATION;
  2466. /* Problem with SSL / TLS negotiation */
  2467. case errSSLNegotiation:
  2468. failf(data, "Could not negotiate an SSL cipher suite with the server");
  2469. break;
  2470. case errSSLBadConfiguration:
  2471. failf(data, "A configuration error occurred");
  2472. break;
  2473. case errSSLProtocol:
  2474. failf(data, "SSL protocol error");
  2475. break;
  2476. case errSSLPeerProtocolVersion:
  2477. failf(data, "A bad protocol version was encountered");
  2478. break;
  2479. case errSSLPeerNoRenegotiation:
  2480. failf(data, "No renegotiation is allowed");
  2481. break;
  2482. /* Generic handshake errors: */
  2483. case errSSLConnectionRefused:
  2484. failf(data, "Server dropped the connection during the SSL handshake");
  2485. break;
  2486. case errSSLClosedAbort:
  2487. failf(data, "Server aborted the SSL handshake");
  2488. break;
  2489. case errSSLClosedGraceful:
  2490. failf(data, "The connection closed gracefully");
  2491. break;
  2492. case errSSLClosedNoNotify:
  2493. failf(data, "The server closed the session with no notification");
  2494. break;
  2495. /* Sometimes paramErr happens with buggy ciphers: */
  2496. case paramErr:
  2497. case errSSLInternal:
  2498. case errSSLPeerInternalError:
  2499. failf(data, "Internal SSL engine error encountered during the "
  2500. "SSL handshake");
  2501. break;
  2502. case errSSLFatalAlert:
  2503. failf(data, "Fatal SSL engine error encountered during the SSL "
  2504. "handshake");
  2505. break;
  2506. /* Unclassified error */
  2507. case errSSLBufferOverflow:
  2508. failf(data, "An insufficient buffer was provided");
  2509. break;
  2510. case errSSLIllegalParam:
  2511. failf(data, "An illegal parameter was encountered");
  2512. break;
  2513. case errSSLModuleAttach:
  2514. failf(data, "Module attach failure");
  2515. break;
  2516. case errSSLSessionNotFound:
  2517. failf(data, "An attempt to restore an unknown session failed");
  2518. break;
  2519. case errSSLPeerExportRestriction:
  2520. failf(data, "An export restriction occurred");
  2521. break;
  2522. case errSSLPeerUserCancelled:
  2523. failf(data, "The user canceled the operation");
  2524. break;
  2525. case errSSLPeerUnexpectedMsg:
  2526. failf(data, "Peer rejected unexpected message");
  2527. break;
  2528. #if CURL_BUILD_MAC_10_11 || CURL_BUILD_IOS_9
  2529. /* Treaing non-fatal error as fatal like before */
  2530. case errSSLClientHelloReceived:
  2531. failf(data, "A non-fatal result for providing a server name "
  2532. "indication");
  2533. break;
  2534. #endif
  2535. /* Error codes defined in the enum but should never be returned.
  2536. We list them here just in case. */
  2537. #if CURL_BUILD_MAC_10_6
  2538. /* Only returned when kSSLSessionOptionBreakOnCertRequested is set */
  2539. case errSSLClientCertRequested:
  2540. failf(data, "Server requested a client certificate during the "
  2541. "handshake");
  2542. return CURLE_SSL_CLIENTCERT;
  2543. #endif
  2544. #if CURL_BUILD_MAC_10_9
  2545. /* Alias for errSSLLast, end of error range */
  2546. case errSSLUnexpectedRecord:
  2547. failf(data, "Unexpected (skipped) record in DTLS");
  2548. break;
  2549. #endif
  2550. default:
  2551. /* May also return codes listed in Security Framework Result Codes */
  2552. failf(data, "Unknown SSL protocol error in connection to %s:%d",
  2553. hostname, err);
  2554. break;
  2555. }
  2556. return CURLE_SSL_CONNECT_ERROR;
  2557. }
  2558. else {
  2559. /* we have been connected fine, we're not waiting for anything else. */
  2560. connssl->connecting_state = ssl_connect_3;
  2561. #ifdef SECTRANSP_PINNEDPUBKEY
  2562. if(data->set.str[STRING_SSL_PINNEDPUBLICKEY]) {
  2563. CURLcode result =
  2564. pkp_pin_peer_pubkey(data, backend->ssl_ctx,
  2565. data->set.str[STRING_SSL_PINNEDPUBLICKEY]);
  2566. if(result) {
  2567. failf(data, "SSL: public key does not match pinned public key");
  2568. return result;
  2569. }
  2570. }
  2571. #endif /* SECTRANSP_PINNEDPUBKEY */
  2572. /* Informational message */
  2573. (void)SSLGetNegotiatedCipher(backend->ssl_ctx, &cipher);
  2574. (void)SSLGetNegotiatedProtocolVersion(backend->ssl_ctx, &protocol);
  2575. switch(protocol) {
  2576. case kSSLProtocol2:
  2577. infof(data, "SSL 2.0 connection using %s",
  2578. TLSCipherNameForNumber(cipher));
  2579. break;
  2580. case kSSLProtocol3:
  2581. infof(data, "SSL 3.0 connection using %s",
  2582. TLSCipherNameForNumber(cipher));
  2583. break;
  2584. case kTLSProtocol1:
  2585. infof(data, "TLS 1.0 connection using %s",
  2586. TLSCipherNameForNumber(cipher));
  2587. break;
  2588. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  2589. case kTLSProtocol11:
  2590. infof(data, "TLS 1.1 connection using %s",
  2591. TLSCipherNameForNumber(cipher));
  2592. break;
  2593. case kTLSProtocol12:
  2594. infof(data, "TLS 1.2 connection using %s",
  2595. TLSCipherNameForNumber(cipher));
  2596. break;
  2597. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  2598. #if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11
  2599. case kTLSProtocol13:
  2600. infof(data, "TLS 1.3 connection using %s",
  2601. TLSCipherNameForNumber(cipher));
  2602. break;
  2603. #endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */
  2604. default:
  2605. infof(data, "Unknown protocol connection");
  2606. break;
  2607. }
  2608. #if(CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && HAVE_BUILTIN_AVAILABLE == 1
  2609. if(conn->bits.tls_enable_alpn) {
  2610. if(__builtin_available(macOS 10.13.4, iOS 11, tvOS 11, *)) {
  2611. CFArrayRef alpnArr = NULL;
  2612. CFStringRef chosenProtocol = NULL;
  2613. err = SSLCopyALPNProtocols(backend->ssl_ctx, &alpnArr);
  2614. if(err == noErr && alpnArr && CFArrayGetCount(alpnArr) >= 1)
  2615. chosenProtocol = CFArrayGetValueAtIndex(alpnArr, 0);
  2616. #ifdef USE_HTTP2
  2617. if(chosenProtocol &&
  2618. !CFStringCompare(chosenProtocol, CFSTR(ALPN_H2), 0)) {
  2619. conn->alpn = CURL_HTTP_VERSION_2;
  2620. }
  2621. else
  2622. #endif
  2623. if(chosenProtocol &&
  2624. !CFStringCompare(chosenProtocol, CFSTR(ALPN_HTTP_1_1), 0)) {
  2625. conn->alpn = CURL_HTTP_VERSION_1_1;
  2626. }
  2627. else
  2628. infof(data, VTLS_INFOF_NO_ALPN);
  2629. Curl_multiuse_state(data, conn->alpn == CURL_HTTP_VERSION_2 ?
  2630. BUNDLE_MULTIPLEX : BUNDLE_NO_MULTIUSE);
  2631. /* chosenProtocol is a reference to the string within alpnArr
  2632. and doesn't need to be freed separately */
  2633. if(alpnArr)
  2634. CFRelease(alpnArr);
  2635. }
  2636. }
  2637. #endif
  2638. return CURLE_OK;
  2639. }
  2640. }
  2641. static CURLcode
  2642. add_cert_to_certinfo(struct Curl_easy *data,
  2643. SecCertificateRef server_cert,
  2644. int idx)
  2645. {
  2646. CURLcode result = CURLE_OK;
  2647. const char *beg;
  2648. const char *end;
  2649. CFDataRef cert_data = SecCertificateCopyData(server_cert);
  2650. if(!cert_data)
  2651. return CURLE_PEER_FAILED_VERIFICATION;
  2652. beg = (const char *)CFDataGetBytePtr(cert_data);
  2653. end = beg + CFDataGetLength(cert_data);
  2654. result = Curl_extract_certinfo(data, idx, beg, end);
  2655. CFRelease(cert_data);
  2656. return result;
  2657. }
  2658. static CURLcode
  2659. collect_server_cert_single(struct Curl_easy *data,
  2660. SecCertificateRef server_cert,
  2661. CFIndex idx)
  2662. {
  2663. CURLcode result = CURLE_OK;
  2664. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  2665. if(data->set.verbose) {
  2666. char *certp;
  2667. result = CopyCertSubject(data, server_cert, &certp);
  2668. if(!result) {
  2669. infof(data, "Server certificate: %s", certp);
  2670. free(certp);
  2671. }
  2672. }
  2673. #endif
  2674. if(data->set.ssl.certinfo)
  2675. result = add_cert_to_certinfo(data, server_cert, (int)idx);
  2676. return result;
  2677. }
  2678. /* This should be called during step3 of the connection at the earliest */
  2679. static CURLcode
  2680. collect_server_cert(struct Curl_easy *data,
  2681. struct connectdata *conn,
  2682. int sockindex)
  2683. {
  2684. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  2685. const bool show_verbose_server_cert = data->set.verbose;
  2686. #else
  2687. const bool show_verbose_server_cert = false;
  2688. #endif
  2689. CURLcode result = data->set.ssl.certinfo ?
  2690. CURLE_PEER_FAILED_VERIFICATION : CURLE_OK;
  2691. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  2692. struct ssl_backend_data *backend = connssl->backend;
  2693. CFArrayRef server_certs = NULL;
  2694. SecCertificateRef server_cert;
  2695. OSStatus err;
  2696. CFIndex i, count;
  2697. SecTrustRef trust = NULL;
  2698. DEBUGASSERT(backend);
  2699. if(!show_verbose_server_cert && !data->set.ssl.certinfo)
  2700. return CURLE_OK;
  2701. if(!backend->ssl_ctx)
  2702. return result;
  2703. #if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS
  2704. #if CURL_BUILD_IOS
  2705. #pragma unused(server_certs)
  2706. err = SSLCopyPeerTrust(backend->ssl_ctx, &trust);
  2707. /* For some reason, SSLCopyPeerTrust() can return noErr and yet return
  2708. a null trust, so be on guard for that: */
  2709. if(err == noErr && trust) {
  2710. count = SecTrustGetCertificateCount(trust);
  2711. if(data->set.ssl.certinfo)
  2712. result = Curl_ssl_init_certinfo(data, (int)count);
  2713. for(i = 0L ; !result && (i < count) ; i++) {
  2714. server_cert = SecTrustGetCertificateAtIndex(trust, i);
  2715. result = collect_server_cert_single(data, server_cert, i);
  2716. }
  2717. CFRelease(trust);
  2718. }
  2719. #else
  2720. /* SSLCopyPeerCertificates() is deprecated as of Mountain Lion.
  2721. The function SecTrustGetCertificateAtIndex() is officially present
  2722. in Lion, but it is unfortunately also present in Snow Leopard as
  2723. private API and doesn't work as expected. So we have to look for
  2724. a different symbol to make sure this code is only executed under
  2725. Lion or later. */
  2726. if(SecTrustCopyPublicKey) {
  2727. #pragma unused(server_certs)
  2728. err = SSLCopyPeerTrust(backend->ssl_ctx, &trust);
  2729. /* For some reason, SSLCopyPeerTrust() can return noErr and yet return
  2730. a null trust, so be on guard for that: */
  2731. if(err == noErr && trust) {
  2732. count = SecTrustGetCertificateCount(trust);
  2733. if(data->set.ssl.certinfo)
  2734. result = Curl_ssl_init_certinfo(data, (int)count);
  2735. for(i = 0L ; !result && (i < count) ; i++) {
  2736. server_cert = SecTrustGetCertificateAtIndex(trust, i);
  2737. result = collect_server_cert_single(data, server_cert, i);
  2738. }
  2739. CFRelease(trust);
  2740. }
  2741. }
  2742. else {
  2743. #if CURL_SUPPORT_MAC_10_8
  2744. err = SSLCopyPeerCertificates(backend->ssl_ctx, &server_certs);
  2745. /* Just in case SSLCopyPeerCertificates() returns null too... */
  2746. if(err == noErr && server_certs) {
  2747. count = CFArrayGetCount(server_certs);
  2748. if(data->set.ssl.certinfo)
  2749. result = Curl_ssl_init_certinfo(data, (int)count);
  2750. for(i = 0L ; !result && (i < count) ; i++) {
  2751. server_cert = (SecCertificateRef)CFArrayGetValueAtIndex(server_certs,
  2752. i);
  2753. result = collect_server_cert_single(data, server_cert, i);
  2754. }
  2755. CFRelease(server_certs);
  2756. }
  2757. #endif /* CURL_SUPPORT_MAC_10_8 */
  2758. }
  2759. #endif /* CURL_BUILD_IOS */
  2760. #else
  2761. #pragma unused(trust)
  2762. err = SSLCopyPeerCertificates(backend->ssl_ctx, &server_certs);
  2763. if(err == noErr) {
  2764. count = CFArrayGetCount(server_certs);
  2765. if(data->set.ssl.certinfo)
  2766. result = Curl_ssl_init_certinfo(data, (int)count);
  2767. for(i = 0L ; !result && (i < count) ; i++) {
  2768. server_cert = (SecCertificateRef)CFArrayGetValueAtIndex(server_certs, i);
  2769. result = collect_server_cert_single(data, server_cert, i);
  2770. }
  2771. CFRelease(server_certs);
  2772. }
  2773. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  2774. return result;
  2775. }
  2776. static CURLcode
  2777. sectransp_connect_step3(struct Curl_easy *data, struct connectdata *conn,
  2778. int sockindex)
  2779. {
  2780. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  2781. /* There is no step 3!
  2782. * Well, okay, let's collect server certificates, and if verbose mode is on,
  2783. * let's print the details of the server certificates. */
  2784. const CURLcode result = collect_server_cert(data, conn, sockindex);
  2785. if(result)
  2786. return result;
  2787. connssl->connecting_state = ssl_connect_done;
  2788. return CURLE_OK;
  2789. }
  2790. static Curl_recv sectransp_recv;
  2791. static Curl_send sectransp_send;
  2792. static CURLcode
  2793. sectransp_connect_common(struct Curl_easy *data,
  2794. struct connectdata *conn,
  2795. int sockindex,
  2796. bool nonblocking,
  2797. bool *done)
  2798. {
  2799. CURLcode result;
  2800. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  2801. curl_socket_t sockfd = conn->sock[sockindex];
  2802. int what;
  2803. /* check if the connection has already been established */
  2804. if(ssl_connection_complete == connssl->state) {
  2805. *done = TRUE;
  2806. return CURLE_OK;
  2807. }
  2808. if(ssl_connect_1 == connssl->connecting_state) {
  2809. /* Find out how much more time we're allowed */
  2810. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  2811. if(timeout_ms < 0) {
  2812. /* no need to continue if time already is up */
  2813. failf(data, "SSL connection timeout");
  2814. return CURLE_OPERATION_TIMEDOUT;
  2815. }
  2816. result = sectransp_connect_step1(data, conn, sockindex);
  2817. if(result)
  2818. return result;
  2819. }
  2820. while(ssl_connect_2 == connssl->connecting_state ||
  2821. ssl_connect_2_reading == connssl->connecting_state ||
  2822. ssl_connect_2_writing == connssl->connecting_state) {
  2823. /* check allowed time left */
  2824. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  2825. if(timeout_ms < 0) {
  2826. /* no need to continue if time already is up */
  2827. failf(data, "SSL connection timeout");
  2828. return CURLE_OPERATION_TIMEDOUT;
  2829. }
  2830. /* if ssl is expecting something, check if it's available. */
  2831. if(connssl->connecting_state == ssl_connect_2_reading ||
  2832. connssl->connecting_state == ssl_connect_2_writing) {
  2833. curl_socket_t writefd = ssl_connect_2_writing ==
  2834. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  2835. curl_socket_t readfd = ssl_connect_2_reading ==
  2836. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  2837. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  2838. nonblocking ? 0 : timeout_ms);
  2839. if(what < 0) {
  2840. /* fatal error */
  2841. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  2842. return CURLE_SSL_CONNECT_ERROR;
  2843. }
  2844. else if(0 == what) {
  2845. if(nonblocking) {
  2846. *done = FALSE;
  2847. return CURLE_OK;
  2848. }
  2849. else {
  2850. /* timeout */
  2851. failf(data, "SSL connection timeout");
  2852. return CURLE_OPERATION_TIMEDOUT;
  2853. }
  2854. }
  2855. /* socket is readable or writable */
  2856. }
  2857. /* Run transaction, and return to the caller if it failed or if this
  2858. * connection is done nonblocking and this loop would execute again. This
  2859. * permits the owner of a multi handle to abort a connection attempt
  2860. * before step2 has completed while ensuring that a client using select()
  2861. * or epoll() will always have a valid fdset to wait on.
  2862. */
  2863. result = sectransp_connect_step2(data, conn, sockindex);
  2864. if(result || (nonblocking &&
  2865. (ssl_connect_2 == connssl->connecting_state ||
  2866. ssl_connect_2_reading == connssl->connecting_state ||
  2867. ssl_connect_2_writing == connssl->connecting_state)))
  2868. return result;
  2869. } /* repeat step2 until all transactions are done. */
  2870. if(ssl_connect_3 == connssl->connecting_state) {
  2871. result = sectransp_connect_step3(data, conn, sockindex);
  2872. if(result)
  2873. return result;
  2874. }
  2875. if(ssl_connect_done == connssl->connecting_state) {
  2876. connssl->state = ssl_connection_complete;
  2877. conn->recv[sockindex] = sectransp_recv;
  2878. conn->send[sockindex] = sectransp_send;
  2879. *done = TRUE;
  2880. }
  2881. else
  2882. *done = FALSE;
  2883. /* Reset our connect state machine */
  2884. connssl->connecting_state = ssl_connect_1;
  2885. return CURLE_OK;
  2886. }
  2887. static CURLcode sectransp_connect_nonblocking(struct Curl_easy *data,
  2888. struct connectdata *conn,
  2889. int sockindex, bool *done)
  2890. {
  2891. return sectransp_connect_common(data, conn, sockindex, TRUE, done);
  2892. }
  2893. static CURLcode sectransp_connect(struct Curl_easy *data,
  2894. struct connectdata *conn, int sockindex)
  2895. {
  2896. CURLcode result;
  2897. bool done = FALSE;
  2898. result = sectransp_connect_common(data, conn, sockindex, FALSE, &done);
  2899. if(result)
  2900. return result;
  2901. DEBUGASSERT(done);
  2902. return CURLE_OK;
  2903. }
  2904. static void sectransp_close(struct Curl_easy *data, struct connectdata *conn,
  2905. int sockindex)
  2906. {
  2907. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  2908. struct ssl_backend_data *backend = connssl->backend;
  2909. (void) data;
  2910. DEBUGASSERT(backend);
  2911. if(backend->ssl_ctx) {
  2912. (void)SSLClose(backend->ssl_ctx);
  2913. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  2914. if(SSLCreateContext)
  2915. CFRelease(backend->ssl_ctx);
  2916. #if CURL_SUPPORT_MAC_10_8
  2917. else
  2918. (void)SSLDisposeContext(backend->ssl_ctx);
  2919. #endif /* CURL_SUPPORT_MAC_10_8 */
  2920. #else
  2921. (void)SSLDisposeContext(backend->ssl_ctx);
  2922. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  2923. backend->ssl_ctx = NULL;
  2924. }
  2925. backend->ssl_sockfd = 0;
  2926. }
  2927. static int sectransp_shutdown(struct Curl_easy *data,
  2928. struct connectdata *conn, int sockindex)
  2929. {
  2930. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  2931. struct ssl_backend_data *backend = connssl->backend;
  2932. ssize_t nread;
  2933. int what;
  2934. int rc;
  2935. char buf[120];
  2936. int loop = 10; /* avoid getting stuck */
  2937. DEBUGASSERT(backend);
  2938. if(!backend->ssl_ctx)
  2939. return 0;
  2940. #ifndef CURL_DISABLE_FTP
  2941. if(data->set.ftp_ccc != CURLFTPSSL_CCC_ACTIVE)
  2942. return 0;
  2943. #endif
  2944. sectransp_close(data, conn, sockindex);
  2945. rc = 0;
  2946. what = SOCKET_READABLE(conn->sock[sockindex], SSL_SHUTDOWN_TIMEOUT);
  2947. while(loop--) {
  2948. if(what < 0) {
  2949. /* anything that gets here is fatally bad */
  2950. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  2951. rc = -1;
  2952. break;
  2953. }
  2954. if(!what) { /* timeout */
  2955. failf(data, "SSL shutdown timeout");
  2956. break;
  2957. }
  2958. /* Something to read, let's do it and hope that it is the close
  2959. notify alert from the server. No way to SSL_Read now, so use read(). */
  2960. nread = read(conn->sock[sockindex], buf, sizeof(buf));
  2961. if(nread < 0) {
  2962. char buffer[STRERROR_LEN];
  2963. failf(data, "read: %s",
  2964. Curl_strerror(errno, buffer, sizeof(buffer)));
  2965. rc = -1;
  2966. }
  2967. if(nread <= 0)
  2968. break;
  2969. what = SOCKET_READABLE(conn->sock[sockindex], 0);
  2970. }
  2971. return rc;
  2972. }
  2973. static void sectransp_session_free(void *ptr)
  2974. {
  2975. /* ST, as of iOS 5 and Mountain Lion, has no public method of deleting a
  2976. cached session ID inside the Security framework. There is a private
  2977. function that does this, but I don't want to have to explain to you why I
  2978. got your application rejected from the App Store due to the use of a
  2979. private API, so the best we can do is free up our own char array that we
  2980. created way back in sectransp_connect_step1... */
  2981. Curl_safefree(ptr);
  2982. }
  2983. static size_t sectransp_version(char *buffer, size_t size)
  2984. {
  2985. return msnprintf(buffer, size, "SecureTransport");
  2986. }
  2987. /*
  2988. * This function uses SSLGetSessionState to determine connection status.
  2989. *
  2990. * Return codes:
  2991. * 1 means the connection is still in place
  2992. * 0 means the connection has been closed
  2993. * -1 means the connection status is unknown
  2994. */
  2995. static int sectransp_check_cxn(struct connectdata *conn)
  2996. {
  2997. struct ssl_connect_data *connssl = &conn->ssl[FIRSTSOCKET];
  2998. struct ssl_backend_data *backend = connssl->backend;
  2999. OSStatus err;
  3000. SSLSessionState state;
  3001. DEBUGASSERT(backend);
  3002. if(backend->ssl_ctx) {
  3003. err = SSLGetSessionState(backend->ssl_ctx, &state);
  3004. if(err == noErr)
  3005. return state == kSSLConnected || state == kSSLHandshake;
  3006. return -1;
  3007. }
  3008. return 0;
  3009. }
  3010. static bool sectransp_data_pending(const struct connectdata *conn,
  3011. int connindex)
  3012. {
  3013. const struct ssl_connect_data *connssl = &conn->ssl[connindex];
  3014. struct ssl_backend_data *backend = connssl->backend;
  3015. OSStatus err;
  3016. size_t buffer;
  3017. DEBUGASSERT(backend);
  3018. if(backend->ssl_ctx) { /* SSL is in use */
  3019. err = SSLGetBufferedReadSize(backend->ssl_ctx, &buffer);
  3020. if(err == noErr)
  3021. return buffer > 0UL;
  3022. return false;
  3023. }
  3024. else
  3025. return false;
  3026. }
  3027. static CURLcode sectransp_random(struct Curl_easy *data UNUSED_PARAM,
  3028. unsigned char *entropy, size_t length)
  3029. {
  3030. /* arc4random_buf() isn't available on cats older than Lion, so let's
  3031. do this manually for the benefit of the older cats. */
  3032. size_t i;
  3033. u_int32_t random_number = 0;
  3034. (void)data;
  3035. for(i = 0 ; i < length ; i++) {
  3036. if(i % sizeof(u_int32_t) == 0)
  3037. random_number = arc4random();
  3038. entropy[i] = random_number & 0xFF;
  3039. random_number >>= 8;
  3040. }
  3041. i = random_number = 0;
  3042. return CURLE_OK;
  3043. }
  3044. static CURLcode sectransp_sha256sum(const unsigned char *tmp, /* input */
  3045. size_t tmplen,
  3046. unsigned char *sha256sum, /* output */
  3047. size_t sha256len)
  3048. {
  3049. assert(sha256len >= CURL_SHA256_DIGEST_LENGTH);
  3050. (void)CC_SHA256(tmp, (CC_LONG)tmplen, sha256sum);
  3051. return CURLE_OK;
  3052. }
  3053. static bool sectransp_false_start(void)
  3054. {
  3055. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  3056. if(SSLSetSessionOption)
  3057. return TRUE;
  3058. #endif
  3059. return FALSE;
  3060. }
  3061. static ssize_t sectransp_send(struct Curl_easy *data,
  3062. int sockindex,
  3063. const void *mem,
  3064. size_t len,
  3065. CURLcode *curlcode)
  3066. {
  3067. struct connectdata *conn = data->conn;
  3068. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  3069. struct ssl_backend_data *backend = connssl->backend;
  3070. size_t processed = 0UL;
  3071. OSStatus err;
  3072. DEBUGASSERT(backend);
  3073. /* The SSLWrite() function works a little differently than expected. The
  3074. fourth argument (processed) is currently documented in Apple's
  3075. documentation as: "On return, the length, in bytes, of the data actually
  3076. written."
  3077. Now, one could interpret that as "written to the socket," but actually,
  3078. it returns the amount of data that was written to a buffer internal to
  3079. the SSLContextRef instead. So it's possible for SSLWrite() to return
  3080. errSSLWouldBlock and a number of bytes "written" because those bytes were
  3081. encrypted and written to a buffer, not to the socket.
  3082. So if this happens, then we need to keep calling SSLWrite() over and
  3083. over again with no new data until it quits returning errSSLWouldBlock. */
  3084. /* Do we have buffered data to write from the last time we were called? */
  3085. if(backend->ssl_write_buffered_length) {
  3086. /* Write the buffered data: */
  3087. err = SSLWrite(backend->ssl_ctx, NULL, 0UL, &processed);
  3088. switch(err) {
  3089. case noErr:
  3090. /* processed is always going to be 0 because we didn't write to
  3091. the buffer, so return how much was written to the socket */
  3092. processed = backend->ssl_write_buffered_length;
  3093. backend->ssl_write_buffered_length = 0UL;
  3094. break;
  3095. case errSSLWouldBlock: /* argh, try again */
  3096. *curlcode = CURLE_AGAIN;
  3097. return -1L;
  3098. default:
  3099. failf(data, "SSLWrite() returned error %d", err);
  3100. *curlcode = CURLE_SEND_ERROR;
  3101. return -1L;
  3102. }
  3103. }
  3104. else {
  3105. /* We've got new data to write: */
  3106. err = SSLWrite(backend->ssl_ctx, mem, len, &processed);
  3107. if(err != noErr) {
  3108. switch(err) {
  3109. case errSSLWouldBlock:
  3110. /* Data was buffered but not sent, we have to tell the caller
  3111. to try sending again, and remember how much was buffered */
  3112. backend->ssl_write_buffered_length = len;
  3113. *curlcode = CURLE_AGAIN;
  3114. return -1L;
  3115. default:
  3116. failf(data, "SSLWrite() returned error %d", err);
  3117. *curlcode = CURLE_SEND_ERROR;
  3118. return -1L;
  3119. }
  3120. }
  3121. }
  3122. return (ssize_t)processed;
  3123. }
  3124. static ssize_t sectransp_recv(struct Curl_easy *data,
  3125. int num,
  3126. char *buf,
  3127. size_t buffersize,
  3128. CURLcode *curlcode)
  3129. {
  3130. struct connectdata *conn = data->conn;
  3131. struct ssl_connect_data *connssl = &conn->ssl[num];
  3132. struct ssl_backend_data *backend = connssl->backend;
  3133. size_t processed = 0UL;
  3134. OSStatus err;
  3135. DEBUGASSERT(backend);
  3136. again:
  3137. err = SSLRead(backend->ssl_ctx, buf, buffersize, &processed);
  3138. if(err != noErr) {
  3139. switch(err) {
  3140. case errSSLWouldBlock: /* return how much we read (if anything) */
  3141. if(processed)
  3142. return (ssize_t)processed;
  3143. *curlcode = CURLE_AGAIN;
  3144. return -1L;
  3145. break;
  3146. /* errSSLClosedGraceful - server gracefully shut down the SSL session
  3147. errSSLClosedNoNotify - server hung up on us instead of sending a
  3148. closure alert notice, read() is returning 0
  3149. Either way, inform the caller that the server disconnected. */
  3150. case errSSLClosedGraceful:
  3151. case errSSLClosedNoNotify:
  3152. *curlcode = CURLE_OK;
  3153. return -1L;
  3154. break;
  3155. /* The below is errSSLPeerAuthCompleted; it's not defined in
  3156. Leopard's headers */
  3157. case -9841:
  3158. if((SSL_CONN_CONFIG(CAfile) || SSL_CONN_CONFIG(ca_info_blob)) &&
  3159. SSL_CONN_CONFIG(verifypeer)) {
  3160. CURLcode result = verify_cert(data, SSL_CONN_CONFIG(CAfile),
  3161. SSL_CONN_CONFIG(ca_info_blob),
  3162. backend->ssl_ctx);
  3163. if(result)
  3164. return result;
  3165. }
  3166. goto again;
  3167. default:
  3168. failf(data, "SSLRead() return error %d", err);
  3169. *curlcode = CURLE_RECV_ERROR;
  3170. return -1L;
  3171. break;
  3172. }
  3173. }
  3174. return (ssize_t)processed;
  3175. }
  3176. static void *sectransp_get_internals(struct ssl_connect_data *connssl,
  3177. CURLINFO info UNUSED_PARAM)
  3178. {
  3179. struct ssl_backend_data *backend = connssl->backend;
  3180. (void)info;
  3181. DEBUGASSERT(backend);
  3182. return backend->ssl_ctx;
  3183. }
  3184. const struct Curl_ssl Curl_ssl_sectransp = {
  3185. { CURLSSLBACKEND_SECURETRANSPORT, "secure-transport" }, /* info */
  3186. SSLSUPP_CAINFO_BLOB |
  3187. SSLSUPP_CERTINFO |
  3188. #ifdef SECTRANSP_PINNEDPUBKEY
  3189. SSLSUPP_PINNEDPUBKEY,
  3190. #else
  3191. 0,
  3192. #endif /* SECTRANSP_PINNEDPUBKEY */
  3193. sizeof(struct ssl_backend_data),
  3194. Curl_none_init, /* init */
  3195. Curl_none_cleanup, /* cleanup */
  3196. sectransp_version, /* version */
  3197. sectransp_check_cxn, /* check_cxn */
  3198. sectransp_shutdown, /* shutdown */
  3199. sectransp_data_pending, /* data_pending */
  3200. sectransp_random, /* random */
  3201. Curl_none_cert_status_request, /* cert_status_request */
  3202. sectransp_connect, /* connect */
  3203. sectransp_connect_nonblocking, /* connect_nonblocking */
  3204. Curl_ssl_getsock, /* getsock */
  3205. sectransp_get_internals, /* get_internals */
  3206. sectransp_close, /* close_one */
  3207. Curl_none_close_all, /* close_all */
  3208. sectransp_session_free, /* session_free */
  3209. Curl_none_set_engine, /* set_engine */
  3210. Curl_none_set_engine_default, /* set_engine_default */
  3211. Curl_none_engines_list, /* engines_list */
  3212. sectransp_false_start, /* false_start */
  3213. sectransp_sha256sum, /* sha256sum */
  3214. NULL, /* associate_connection */
  3215. NULL, /* disassociate_connection */
  3216. NULL /* free_multi_ssl_backend_data */
  3217. };
  3218. #ifdef __clang__
  3219. #pragma clang diagnostic pop
  3220. #endif
  3221. #endif /* USE_SECTRANSP */