sectransp.c 125 KB

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