2
0

sectransp.c 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492
  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 push
  44. #pragma GCC diagnostic ignored "-Waddress"
  45. #pragma GCC diagnostic ignored "-Wundef"
  46. #pragma GCC diagnostic ignored "-Wunreachable-code"
  47. #endif
  48. #include <limits.h>
  49. #include <Security/Security.h>
  50. /* For some reason, when building for iOS, the omnibus header above does
  51. * not include SecureTransport.h as of iOS SDK 5.1. */
  52. #include <Security/SecureTransport.h>
  53. #include <CoreFoundation/CoreFoundation.h>
  54. #include <CommonCrypto/CommonDigest.h>
  55. /* The Security framework has changed greatly between iOS and different macOS
  56. versions, and we will try to support as many of them as we can (back to
  57. Leopard and iOS 5) by using macros and weak-linking.
  58. In general, you want to build this using the most recent OS SDK, since some
  59. features require curl to be built against the latest SDK. TLS 1.1 and 1.2
  60. support, for instance, require the macOS 10.8 SDK or later. TLS 1.3
  61. requires the macOS 10.13 or iOS 11 SDK or later. */
  62. #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
  63. #if MAC_OS_X_VERSION_MAX_ALLOWED < 1050
  64. #error "The Secure Transport back-end requires Leopard or later."
  65. #endif /* MAC_OS_X_VERSION_MAX_ALLOWED < 1050 */
  66. #define CURL_BUILD_IOS 0
  67. #define CURL_BUILD_IOS_7 0
  68. #define CURL_BUILD_IOS_9 0
  69. #define CURL_BUILD_IOS_11 0
  70. #define CURL_BUILD_IOS_13 0
  71. #define CURL_BUILD_MAC 1
  72. /* This is the maximum API level we are allowed to use when building: */
  73. #define CURL_BUILD_MAC_10_5 MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
  74. #define CURL_BUILD_MAC_10_6 MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
  75. #define CURL_BUILD_MAC_10_7 MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  76. #define CURL_BUILD_MAC_10_8 MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
  77. #define CURL_BUILD_MAC_10_9 MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
  78. #define CURL_BUILD_MAC_10_11 MAC_OS_X_VERSION_MAX_ALLOWED >= 101100
  79. #define CURL_BUILD_MAC_10_13 MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
  80. #define CURL_BUILD_MAC_10_15 MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
  81. /* These macros mean "the following code is present to allow runtime backward
  82. compatibility with at least this cat or earlier":
  83. (You set this at build-time using the compiler command line option
  84. "-mmacosx-version-min.") */
  85. #define CURL_SUPPORT_MAC_10_5 MAC_OS_X_VERSION_MIN_REQUIRED <= 1050
  86. #define CURL_SUPPORT_MAC_10_6 MAC_OS_X_VERSION_MIN_REQUIRED <= 1060
  87. #define CURL_SUPPORT_MAC_10_7 MAC_OS_X_VERSION_MIN_REQUIRED <= 1070
  88. #define CURL_SUPPORT_MAC_10_8 MAC_OS_X_VERSION_MIN_REQUIRED <= 1080
  89. #define CURL_SUPPORT_MAC_10_9 MAC_OS_X_VERSION_MIN_REQUIRED <= 1090
  90. #elif TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
  91. #define CURL_BUILD_IOS 1
  92. #define CURL_BUILD_IOS_7 __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
  93. #define CURL_BUILD_IOS_9 __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
  94. #define CURL_BUILD_IOS_11 __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  95. #define CURL_BUILD_IOS_13 __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  96. #define CURL_BUILD_MAC 0
  97. #define CURL_BUILD_MAC_10_5 0
  98. #define CURL_BUILD_MAC_10_6 0
  99. #define CURL_BUILD_MAC_10_7 0
  100. #define CURL_BUILD_MAC_10_8 0
  101. #define CURL_BUILD_MAC_10_9 0
  102. #define CURL_BUILD_MAC_10_11 0
  103. #define CURL_BUILD_MAC_10_13 0
  104. #define CURL_BUILD_MAC_10_15 0
  105. #define CURL_SUPPORT_MAC_10_5 0
  106. #define CURL_SUPPORT_MAC_10_6 0
  107. #define CURL_SUPPORT_MAC_10_7 0
  108. #define CURL_SUPPORT_MAC_10_8 0
  109. #define CURL_SUPPORT_MAC_10_9 0
  110. #else
  111. #error "The Secure Transport back-end requires iOS or macOS."
  112. #endif /* (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) */
  113. #if CURL_BUILD_MAC
  114. #include <sys/sysctl.h>
  115. #endif /* CURL_BUILD_MAC */
  116. #include "sendf.h"
  117. #include "inet_pton.h"
  118. #include "connect.h"
  119. #include "select.h"
  120. #include "vtls.h"
  121. #include "vtls_int.h"
  122. #include "sectransp.h"
  123. #include "curl_printf.h"
  124. #include "strdup.h"
  125. #include "curl_memory.h"
  126. /* The last #include file should be: */
  127. #include "memdebug.h"
  128. /* From MacTypes.h (which we can't include because it isn't present in iOS: */
  129. #define ioErr -36
  130. #define paramErr -50
  131. struct st_ssl_backend_data {
  132. SSLContextRef ssl_ctx;
  133. bool ssl_direction; /* true if writing, false if reading */
  134. size_t ssl_write_buffered_length;
  135. };
  136. struct st_cipher {
  137. const char *name; /* Cipher suite IANA name. It starts with "TLS_" prefix */
  138. const char *alias_name; /* Alias name is the same as OpenSSL cipher name */
  139. SSLCipherSuite num; /* Cipher suite code/number defined in IANA registry */
  140. bool weak; /* Flag to mark cipher as weak based on previous implementation
  141. of Secure Transport back-end by CURL */
  142. };
  143. /* Macro to initialize st_cipher data structure: stringify id to name, cipher
  144. number/id, 'weak' suite flag
  145. */
  146. #define CIPHER_DEF(num, alias, weak) \
  147. { #num, alias, num, weak }
  148. /*
  149. Macro to initialize st_cipher data structure with name, code (IANA cipher
  150. number/id value), and 'weak' suite flag. The first 28 cipher suite numbers
  151. have the same IANA code for both SSL and TLS standards: numbers 0x0000 to
  152. 0x001B. They have different names though. The first 4 letters of the cipher
  153. suite name are the protocol name: "SSL_" or "TLS_", rest of the IANA name is
  154. the same for both SSL and TLS cipher suite name.
  155. The second part of the problem is that macOS/iOS SDKs don't define all TLS
  156. codes but only 12 of them. The SDK defines all SSL codes though, i.e. SSL_NUM
  157. constant is always defined for those 28 ciphers while TLS_NUM is defined only
  158. for 12 of the first 28 ciphers. Those 12 TLS cipher codes match to
  159. corresponding SSL enum value and represent the same cipher suite. Therefore
  160. we'll use the SSL enum value for those cipher suites because it is defined
  161. for all 28 of them.
  162. We make internal data consistent and based on TLS names, i.e. all st_cipher
  163. item names start with the "TLS_" prefix.
  164. Summarizing all the above, those 28 first ciphers are presented in our table
  165. with both TLS and SSL names. Their cipher numbers are assigned based on the
  166. SDK enum value for the SSL cipher, which matches to IANA TLS number.
  167. */
  168. #define CIPHER_DEF_SSLTLS(num_wo_prefix, alias, weak) \
  169. { "TLS_" #num_wo_prefix, alias, SSL_##num_wo_prefix, weak }
  170. /*
  171. Cipher suites were marked as weak based on the following:
  172. RC4 encryption - rfc7465, the document contains a list of deprecated ciphers.
  173. Marked in the code below as weak.
  174. RC2 encryption - many mentions, was found vulnerable to a relatively easy
  175. attack https://link.springer.com/chapter/10.1007%2F3-540-69710-1_14
  176. Marked in the code below as weak.
  177. DES and IDEA encryption - rfc5469, has a list of deprecated ciphers.
  178. Marked in the code below as weak.
  179. Anonymous Diffie-Hellman authentication and anonymous elliptic curve
  180. Diffie-Hellman - vulnerable to a man-in-the-middle attack. Deprecated by
  181. RFC 4346 aka TLS 1.1 (section A.5, page 60)
  182. Null bulk encryption suites - not encrypted communication
  183. Export ciphers, i.e. ciphers with restrictions to be used outside the US for
  184. software exported to some countries, they were excluded from TLS 1.1
  185. version. More precisely, they were noted as ciphers which MUST NOT be
  186. negotiated in RFC 4346 aka TLS 1.1 (section A.5, pages 60 and 61).
  187. All of those filters were considered weak because they contain a weak
  188. algorithm like DES, RC2 or RC4, and already considered weak by other
  189. criteria.
  190. 3DES - NIST deprecated it and is going to retire it by 2023
  191. https://csrc.nist.gov/News/2017/Update-to-Current-Use-and-Deprecation-of-TDEA
  192. OpenSSL https://www.openssl.org/blog/blog/2016/08/24/sweet32/ also
  193. deprecated those ciphers. Some other libraries also consider it
  194. vulnerable or at least not strong enough.
  195. CBC ciphers are vulnerable with SSL3.0 and TLS1.0:
  196. https://www.cisco.com/c/en/us/support/docs/security/email-security-appliance
  197. /118518-technote-esa-00.html
  198. We don't take care of this issue because it is resolved by later TLS
  199. versions and for us, it requires more complicated checks, we need to
  200. check a protocol version also. Vulnerability doesn't look very critical
  201. and we do not filter out those cipher suites.
  202. */
  203. #define CIPHER_WEAK_NOT_ENCRYPTED TRUE
  204. #define CIPHER_WEAK_RC_ENCRYPTION TRUE
  205. #define CIPHER_WEAK_DES_ENCRYPTION TRUE
  206. #define CIPHER_WEAK_IDEA_ENCRYPTION TRUE
  207. #define CIPHER_WEAK_ANON_AUTH TRUE
  208. #define CIPHER_WEAK_3DES_ENCRYPTION TRUE
  209. #define CIPHER_STRONG_ENOUGH FALSE
  210. /* Please do not change the order of the first ciphers available for SSL.
  211. Do not insert and do not delete any of them. Code below
  212. depends on their order and continuity.
  213. If you add a new cipher, please maintain order by number, i.e.
  214. insert in between existing items to appropriate place based on
  215. cipher suite IANA number
  216. */
  217. static const struct st_cipher ciphertable[] = {
  218. /* SSL version 3.0 and initial TLS 1.0 cipher suites.
  219. Defined since SDK 10.2.8 */
  220. CIPHER_DEF_SSLTLS(NULL_WITH_NULL_NULL, /* 0x0000 */
  221. NULL,
  222. CIPHER_WEAK_NOT_ENCRYPTED),
  223. CIPHER_DEF_SSLTLS(RSA_WITH_NULL_MD5, /* 0x0001 */
  224. "NULL-MD5",
  225. CIPHER_WEAK_NOT_ENCRYPTED),
  226. CIPHER_DEF_SSLTLS(RSA_WITH_NULL_SHA, /* 0x0002 */
  227. "NULL-SHA",
  228. CIPHER_WEAK_NOT_ENCRYPTED),
  229. CIPHER_DEF_SSLTLS(RSA_EXPORT_WITH_RC4_40_MD5, /* 0x0003 */
  230. "EXP-RC4-MD5",
  231. CIPHER_WEAK_RC_ENCRYPTION),
  232. CIPHER_DEF_SSLTLS(RSA_WITH_RC4_128_MD5, /* 0x0004 */
  233. "RC4-MD5",
  234. CIPHER_WEAK_RC_ENCRYPTION),
  235. CIPHER_DEF_SSLTLS(RSA_WITH_RC4_128_SHA, /* 0x0005 */
  236. "RC4-SHA",
  237. CIPHER_WEAK_RC_ENCRYPTION),
  238. CIPHER_DEF_SSLTLS(RSA_EXPORT_WITH_RC2_CBC_40_MD5, /* 0x0006 */
  239. "EXP-RC2-CBC-MD5",
  240. CIPHER_WEAK_RC_ENCRYPTION),
  241. CIPHER_DEF_SSLTLS(RSA_WITH_IDEA_CBC_SHA, /* 0x0007 */
  242. "IDEA-CBC-SHA",
  243. CIPHER_WEAK_IDEA_ENCRYPTION),
  244. CIPHER_DEF_SSLTLS(RSA_EXPORT_WITH_DES40_CBC_SHA, /* 0x0008 */
  245. "EXP-DES-CBC-SHA",
  246. CIPHER_WEAK_DES_ENCRYPTION),
  247. CIPHER_DEF_SSLTLS(RSA_WITH_DES_CBC_SHA, /* 0x0009 */
  248. "DES-CBC-SHA",
  249. CIPHER_WEAK_DES_ENCRYPTION),
  250. CIPHER_DEF_SSLTLS(RSA_WITH_3DES_EDE_CBC_SHA, /* 0x000A */
  251. "DES-CBC3-SHA",
  252. CIPHER_WEAK_3DES_ENCRYPTION),
  253. CIPHER_DEF_SSLTLS(DH_DSS_EXPORT_WITH_DES40_CBC_SHA, /* 0x000B */
  254. "EXP-DH-DSS-DES-CBC-SHA",
  255. CIPHER_WEAK_DES_ENCRYPTION),
  256. CIPHER_DEF_SSLTLS(DH_DSS_WITH_DES_CBC_SHA, /* 0x000C */
  257. "DH-DSS-DES-CBC-SHA",
  258. CIPHER_WEAK_DES_ENCRYPTION),
  259. CIPHER_DEF_SSLTLS(DH_DSS_WITH_3DES_EDE_CBC_SHA, /* 0x000D */
  260. "DH-DSS-DES-CBC3-SHA",
  261. CIPHER_WEAK_3DES_ENCRYPTION),
  262. CIPHER_DEF_SSLTLS(DH_RSA_EXPORT_WITH_DES40_CBC_SHA, /* 0x000E */
  263. "EXP-DH-RSA-DES-CBC-SHA",
  264. CIPHER_WEAK_DES_ENCRYPTION),
  265. CIPHER_DEF_SSLTLS(DH_RSA_WITH_DES_CBC_SHA, /* 0x000F */
  266. "DH-RSA-DES-CBC-SHA",
  267. CIPHER_WEAK_DES_ENCRYPTION),
  268. CIPHER_DEF_SSLTLS(DH_RSA_WITH_3DES_EDE_CBC_SHA, /* 0x0010 */
  269. "DH-RSA-DES-CBC3-SHA",
  270. CIPHER_WEAK_3DES_ENCRYPTION),
  271. CIPHER_DEF_SSLTLS(DHE_DSS_EXPORT_WITH_DES40_CBC_SHA, /* 0x0011 */
  272. "EXP-EDH-DSS-DES-CBC-SHA",
  273. CIPHER_WEAK_DES_ENCRYPTION),
  274. CIPHER_DEF_SSLTLS(DHE_DSS_WITH_DES_CBC_SHA, /* 0x0012 */
  275. "EDH-DSS-CBC-SHA",
  276. CIPHER_WEAK_DES_ENCRYPTION),
  277. CIPHER_DEF_SSLTLS(DHE_DSS_WITH_3DES_EDE_CBC_SHA, /* 0x0013 */
  278. "DHE-DSS-DES-CBC3-SHA",
  279. CIPHER_WEAK_3DES_ENCRYPTION),
  280. CIPHER_DEF_SSLTLS(DHE_RSA_EXPORT_WITH_DES40_CBC_SHA, /* 0x0014 */
  281. "EXP-EDH-RSA-DES-CBC-SHA",
  282. CIPHER_WEAK_DES_ENCRYPTION),
  283. CIPHER_DEF_SSLTLS(DHE_RSA_WITH_DES_CBC_SHA, /* 0x0015 */
  284. "EDH-RSA-DES-CBC-SHA",
  285. CIPHER_WEAK_DES_ENCRYPTION),
  286. CIPHER_DEF_SSLTLS(DHE_RSA_WITH_3DES_EDE_CBC_SHA, /* 0x0016 */
  287. "DHE-RSA-DES-CBC3-SHA",
  288. CIPHER_WEAK_3DES_ENCRYPTION),
  289. CIPHER_DEF_SSLTLS(DH_anon_EXPORT_WITH_RC4_40_MD5, /* 0x0017 */
  290. "EXP-ADH-RC4-MD5",
  291. CIPHER_WEAK_ANON_AUTH),
  292. CIPHER_DEF_SSLTLS(DH_anon_WITH_RC4_128_MD5, /* 0x0018 */
  293. "ADH-RC4-MD5",
  294. CIPHER_WEAK_ANON_AUTH),
  295. CIPHER_DEF_SSLTLS(DH_anon_EXPORT_WITH_DES40_CBC_SHA, /* 0x0019 */
  296. "EXP-ADH-DES-CBC-SHA",
  297. CIPHER_WEAK_ANON_AUTH),
  298. CIPHER_DEF_SSLTLS(DH_anon_WITH_DES_CBC_SHA, /* 0x001A */
  299. "ADH-DES-CBC-SHA",
  300. CIPHER_WEAK_ANON_AUTH),
  301. CIPHER_DEF_SSLTLS(DH_anon_WITH_3DES_EDE_CBC_SHA, /* 0x001B */
  302. "ADH-DES-CBC3-SHA",
  303. CIPHER_WEAK_3DES_ENCRYPTION),
  304. CIPHER_DEF(SSL_FORTEZZA_DMS_WITH_NULL_SHA, /* 0x001C */
  305. NULL,
  306. CIPHER_WEAK_NOT_ENCRYPTED),
  307. CIPHER_DEF(SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA, /* 0x001D */
  308. NULL,
  309. CIPHER_STRONG_ENOUGH),
  310. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  311. /* RFC 4785 - Pre-Shared Key (PSK) Ciphersuites with NULL Encryption */
  312. CIPHER_DEF(TLS_PSK_WITH_NULL_SHA, /* 0x002C */
  313. "PSK-NULL-SHA",
  314. CIPHER_WEAK_NOT_ENCRYPTED),
  315. CIPHER_DEF(TLS_DHE_PSK_WITH_NULL_SHA, /* 0x002D */
  316. "DHE-PSK-NULL-SHA",
  317. CIPHER_WEAK_NOT_ENCRYPTED),
  318. CIPHER_DEF(TLS_RSA_PSK_WITH_NULL_SHA, /* 0x002E */
  319. "RSA-PSK-NULL-SHA",
  320. CIPHER_WEAK_NOT_ENCRYPTED),
  321. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  322. /* TLS addenda using AES, per RFC 3268. Defined since SDK 10.4u */
  323. CIPHER_DEF(TLS_RSA_WITH_AES_128_CBC_SHA, /* 0x002F */
  324. "AES128-SHA",
  325. CIPHER_STRONG_ENOUGH),
  326. CIPHER_DEF(TLS_DH_DSS_WITH_AES_128_CBC_SHA, /* 0x0030 */
  327. "DH-DSS-AES128-SHA",
  328. CIPHER_STRONG_ENOUGH),
  329. CIPHER_DEF(TLS_DH_RSA_WITH_AES_128_CBC_SHA, /* 0x0031 */
  330. "DH-RSA-AES128-SHA",
  331. CIPHER_STRONG_ENOUGH),
  332. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_128_CBC_SHA, /* 0x0032 */
  333. "DHE-DSS-AES128-SHA",
  334. CIPHER_STRONG_ENOUGH),
  335. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_128_CBC_SHA, /* 0x0033 */
  336. "DHE-RSA-AES128-SHA",
  337. CIPHER_STRONG_ENOUGH),
  338. CIPHER_DEF(TLS_DH_anon_WITH_AES_128_CBC_SHA, /* 0x0034 */
  339. "ADH-AES128-SHA",
  340. CIPHER_WEAK_ANON_AUTH),
  341. CIPHER_DEF(TLS_RSA_WITH_AES_256_CBC_SHA, /* 0x0035 */
  342. "AES256-SHA",
  343. CIPHER_STRONG_ENOUGH),
  344. CIPHER_DEF(TLS_DH_DSS_WITH_AES_256_CBC_SHA, /* 0x0036 */
  345. "DH-DSS-AES256-SHA",
  346. CIPHER_STRONG_ENOUGH),
  347. CIPHER_DEF(TLS_DH_RSA_WITH_AES_256_CBC_SHA, /* 0x0037 */
  348. "DH-RSA-AES256-SHA",
  349. CIPHER_STRONG_ENOUGH),
  350. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_256_CBC_SHA, /* 0x0038 */
  351. "DHE-DSS-AES256-SHA",
  352. CIPHER_STRONG_ENOUGH),
  353. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_256_CBC_SHA, /* 0x0039 */
  354. "DHE-RSA-AES256-SHA",
  355. CIPHER_STRONG_ENOUGH),
  356. CIPHER_DEF(TLS_DH_anon_WITH_AES_256_CBC_SHA, /* 0x003A */
  357. "ADH-AES256-SHA",
  358. CIPHER_WEAK_ANON_AUTH),
  359. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  360. /* TLS 1.2 addenda, RFC 5246 */
  361. /* Server provided RSA certificate for key exchange. */
  362. CIPHER_DEF(TLS_RSA_WITH_NULL_SHA256, /* 0x003B */
  363. "NULL-SHA256",
  364. CIPHER_WEAK_NOT_ENCRYPTED),
  365. CIPHER_DEF(TLS_RSA_WITH_AES_128_CBC_SHA256, /* 0x003C */
  366. "AES128-SHA256",
  367. CIPHER_STRONG_ENOUGH),
  368. CIPHER_DEF(TLS_RSA_WITH_AES_256_CBC_SHA256, /* 0x003D */
  369. "AES256-SHA256",
  370. CIPHER_STRONG_ENOUGH),
  371. /* Server-authenticated (and optionally client-authenticated)
  372. Diffie-Hellman. */
  373. CIPHER_DEF(TLS_DH_DSS_WITH_AES_128_CBC_SHA256, /* 0x003E */
  374. "DH-DSS-AES128-SHA256",
  375. CIPHER_STRONG_ENOUGH),
  376. CIPHER_DEF(TLS_DH_RSA_WITH_AES_128_CBC_SHA256, /* 0x003F */
  377. "DH-RSA-AES128-SHA256",
  378. CIPHER_STRONG_ENOUGH),
  379. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_128_CBC_SHA256, /* 0x0040 */
  380. "DHE-DSS-AES128-SHA256",
  381. CIPHER_STRONG_ENOUGH),
  382. /* TLS 1.2 addenda, RFC 5246 */
  383. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, /* 0x0067 */
  384. "DHE-RSA-AES128-SHA256",
  385. CIPHER_STRONG_ENOUGH),
  386. CIPHER_DEF(TLS_DH_DSS_WITH_AES_256_CBC_SHA256, /* 0x0068 */
  387. "DH-DSS-AES256-SHA256",
  388. CIPHER_STRONG_ENOUGH),
  389. CIPHER_DEF(TLS_DH_RSA_WITH_AES_256_CBC_SHA256, /* 0x0069 */
  390. "DH-RSA-AES256-SHA256",
  391. CIPHER_STRONG_ENOUGH),
  392. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_256_CBC_SHA256, /* 0x006A */
  393. "DHE-DSS-AES256-SHA256",
  394. CIPHER_STRONG_ENOUGH),
  395. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, /* 0x006B */
  396. "DHE-RSA-AES256-SHA256",
  397. CIPHER_STRONG_ENOUGH),
  398. CIPHER_DEF(TLS_DH_anon_WITH_AES_128_CBC_SHA256, /* 0x006C */
  399. "ADH-AES128-SHA256",
  400. CIPHER_WEAK_ANON_AUTH),
  401. CIPHER_DEF(TLS_DH_anon_WITH_AES_256_CBC_SHA256, /* 0x006D */
  402. "ADH-AES256-SHA256",
  403. CIPHER_WEAK_ANON_AUTH),
  404. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  405. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  406. /* Addendum from RFC 4279, TLS PSK */
  407. CIPHER_DEF(TLS_PSK_WITH_RC4_128_SHA, /* 0x008A */
  408. "PSK-RC4-SHA",
  409. CIPHER_WEAK_RC_ENCRYPTION),
  410. CIPHER_DEF(TLS_PSK_WITH_3DES_EDE_CBC_SHA, /* 0x008B */
  411. "PSK-3DES-EDE-CBC-SHA",
  412. CIPHER_WEAK_3DES_ENCRYPTION),
  413. CIPHER_DEF(TLS_PSK_WITH_AES_128_CBC_SHA, /* 0x008C */
  414. "PSK-AES128-CBC-SHA",
  415. CIPHER_STRONG_ENOUGH),
  416. CIPHER_DEF(TLS_PSK_WITH_AES_256_CBC_SHA, /* 0x008D */
  417. "PSK-AES256-CBC-SHA",
  418. CIPHER_STRONG_ENOUGH),
  419. CIPHER_DEF(TLS_DHE_PSK_WITH_RC4_128_SHA, /* 0x008E */
  420. "DHE-PSK-RC4-SHA",
  421. CIPHER_WEAK_RC_ENCRYPTION),
  422. CIPHER_DEF(TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA, /* 0x008F */
  423. "DHE-PSK-3DES-EDE-CBC-SHA",
  424. CIPHER_WEAK_3DES_ENCRYPTION),
  425. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_128_CBC_SHA, /* 0x0090 */
  426. "DHE-PSK-AES128-CBC-SHA",
  427. CIPHER_STRONG_ENOUGH),
  428. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_256_CBC_SHA, /* 0x0091 */
  429. "DHE-PSK-AES256-CBC-SHA",
  430. CIPHER_STRONG_ENOUGH),
  431. CIPHER_DEF(TLS_RSA_PSK_WITH_RC4_128_SHA, /* 0x0092 */
  432. "RSA-PSK-RC4-SHA",
  433. CIPHER_WEAK_RC_ENCRYPTION),
  434. CIPHER_DEF(TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA, /* 0x0093 */
  435. "RSA-PSK-3DES-EDE-CBC-SHA",
  436. CIPHER_WEAK_3DES_ENCRYPTION),
  437. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_128_CBC_SHA, /* 0x0094 */
  438. "RSA-PSK-AES128-CBC-SHA",
  439. CIPHER_STRONG_ENOUGH),
  440. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_256_CBC_SHA, /* 0x0095 */
  441. "RSA-PSK-AES256-CBC-SHA",
  442. CIPHER_STRONG_ENOUGH),
  443. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  444. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  445. /* Addenda from rfc 5288 AES Galois Counter Mode (GCM) Cipher Suites
  446. for TLS. */
  447. CIPHER_DEF(TLS_RSA_WITH_AES_128_GCM_SHA256, /* 0x009C */
  448. "AES128-GCM-SHA256",
  449. CIPHER_STRONG_ENOUGH),
  450. CIPHER_DEF(TLS_RSA_WITH_AES_256_GCM_SHA384, /* 0x009D */
  451. "AES256-GCM-SHA384",
  452. CIPHER_STRONG_ENOUGH),
  453. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, /* 0x009E */
  454. "DHE-RSA-AES128-GCM-SHA256",
  455. CIPHER_STRONG_ENOUGH),
  456. CIPHER_DEF(TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, /* 0x009F */
  457. "DHE-RSA-AES256-GCM-SHA384",
  458. CIPHER_STRONG_ENOUGH),
  459. CIPHER_DEF(TLS_DH_RSA_WITH_AES_128_GCM_SHA256, /* 0x00A0 */
  460. "DH-RSA-AES128-GCM-SHA256",
  461. CIPHER_STRONG_ENOUGH),
  462. CIPHER_DEF(TLS_DH_RSA_WITH_AES_256_GCM_SHA384, /* 0x00A1 */
  463. "DH-RSA-AES256-GCM-SHA384",
  464. CIPHER_STRONG_ENOUGH),
  465. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_128_GCM_SHA256, /* 0x00A2 */
  466. "DHE-DSS-AES128-GCM-SHA256",
  467. CIPHER_STRONG_ENOUGH),
  468. CIPHER_DEF(TLS_DHE_DSS_WITH_AES_256_GCM_SHA384, /* 0x00A3 */
  469. "DHE-DSS-AES256-GCM-SHA384",
  470. CIPHER_STRONG_ENOUGH),
  471. CIPHER_DEF(TLS_DH_DSS_WITH_AES_128_GCM_SHA256, /* 0x00A4 */
  472. "DH-DSS-AES128-GCM-SHA256",
  473. CIPHER_STRONG_ENOUGH),
  474. CIPHER_DEF(TLS_DH_DSS_WITH_AES_256_GCM_SHA384, /* 0x00A5 */
  475. "DH-DSS-AES256-GCM-SHA384",
  476. CIPHER_STRONG_ENOUGH),
  477. CIPHER_DEF(TLS_DH_anon_WITH_AES_128_GCM_SHA256, /* 0x00A6 */
  478. "ADH-AES128-GCM-SHA256",
  479. CIPHER_WEAK_ANON_AUTH),
  480. CIPHER_DEF(TLS_DH_anon_WITH_AES_256_GCM_SHA384, /* 0x00A7 */
  481. "ADH-AES256-GCM-SHA384",
  482. CIPHER_WEAK_ANON_AUTH),
  483. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  484. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  485. /* RFC 5487 - PSK with SHA-256/384 and AES GCM */
  486. CIPHER_DEF(TLS_PSK_WITH_AES_128_GCM_SHA256, /* 0x00A8 */
  487. "PSK-AES128-GCM-SHA256",
  488. CIPHER_STRONG_ENOUGH),
  489. CIPHER_DEF(TLS_PSK_WITH_AES_256_GCM_SHA384, /* 0x00A9 */
  490. "PSK-AES256-GCM-SHA384",
  491. CIPHER_STRONG_ENOUGH),
  492. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_128_GCM_SHA256, /* 0x00AA */
  493. "DHE-PSK-AES128-GCM-SHA256",
  494. CIPHER_STRONG_ENOUGH),
  495. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_256_GCM_SHA384, /* 0x00AB */
  496. "DHE-PSK-AES256-GCM-SHA384",
  497. CIPHER_STRONG_ENOUGH),
  498. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_128_GCM_SHA256, /* 0x00AC */
  499. "RSA-PSK-AES128-GCM-SHA256",
  500. CIPHER_STRONG_ENOUGH),
  501. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_256_GCM_SHA384, /* 0x00AD */
  502. "RSA-PSK-AES256-GCM-SHA384",
  503. CIPHER_STRONG_ENOUGH),
  504. CIPHER_DEF(TLS_PSK_WITH_AES_128_CBC_SHA256, /* 0x00AE */
  505. "PSK-AES128-CBC-SHA256",
  506. CIPHER_STRONG_ENOUGH),
  507. CIPHER_DEF(TLS_PSK_WITH_AES_256_CBC_SHA384, /* 0x00AF */
  508. "PSK-AES256-CBC-SHA384",
  509. CIPHER_STRONG_ENOUGH),
  510. CIPHER_DEF(TLS_PSK_WITH_NULL_SHA256, /* 0x00B0 */
  511. "PSK-NULL-SHA256",
  512. CIPHER_WEAK_NOT_ENCRYPTED),
  513. CIPHER_DEF(TLS_PSK_WITH_NULL_SHA384, /* 0x00B1 */
  514. "PSK-NULL-SHA384",
  515. CIPHER_WEAK_NOT_ENCRYPTED),
  516. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_128_CBC_SHA256, /* 0x00B2 */
  517. "DHE-PSK-AES128-CBC-SHA256",
  518. CIPHER_STRONG_ENOUGH),
  519. CIPHER_DEF(TLS_DHE_PSK_WITH_AES_256_CBC_SHA384, /* 0x00B3 */
  520. "DHE-PSK-AES256-CBC-SHA384",
  521. CIPHER_STRONG_ENOUGH),
  522. CIPHER_DEF(TLS_DHE_PSK_WITH_NULL_SHA256, /* 0x00B4 */
  523. "DHE-PSK-NULL-SHA256",
  524. CIPHER_WEAK_NOT_ENCRYPTED),
  525. CIPHER_DEF(TLS_DHE_PSK_WITH_NULL_SHA384, /* 0x00B5 */
  526. "DHE-PSK-NULL-SHA384",
  527. CIPHER_WEAK_NOT_ENCRYPTED),
  528. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_128_CBC_SHA256, /* 0x00B6 */
  529. "RSA-PSK-AES128-CBC-SHA256",
  530. CIPHER_STRONG_ENOUGH),
  531. CIPHER_DEF(TLS_RSA_PSK_WITH_AES_256_CBC_SHA384, /* 0x00B7 */
  532. "RSA-PSK-AES256-CBC-SHA384",
  533. CIPHER_STRONG_ENOUGH),
  534. CIPHER_DEF(TLS_RSA_PSK_WITH_NULL_SHA256, /* 0x00B8 */
  535. "RSA-PSK-NULL-SHA256",
  536. CIPHER_WEAK_NOT_ENCRYPTED),
  537. CIPHER_DEF(TLS_RSA_PSK_WITH_NULL_SHA384, /* 0x00B9 */
  538. "RSA-PSK-NULL-SHA384",
  539. CIPHER_WEAK_NOT_ENCRYPTED),
  540. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  541. /* RFC 5746 - Secure Renegotiation. This is not a real suite,
  542. it is a response to initiate negotiation again */
  543. CIPHER_DEF(TLS_EMPTY_RENEGOTIATION_INFO_SCSV, /* 0x00FF */
  544. NULL,
  545. CIPHER_STRONG_ENOUGH),
  546. #if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11
  547. /* TLS 1.3 standard cipher suites for ChaCha20+Poly1305.
  548. Note: TLS 1.3 ciphersuites do not specify the key exchange
  549. algorithm -- they only specify the symmetric ciphers.
  550. Cipher alias name matches to OpenSSL cipher name, and for
  551. TLS 1.3 ciphers */
  552. CIPHER_DEF(TLS_AES_128_GCM_SHA256, /* 0x1301 */
  553. NULL, /* The OpenSSL cipher name matches to the IANA name */
  554. CIPHER_STRONG_ENOUGH),
  555. CIPHER_DEF(TLS_AES_256_GCM_SHA384, /* 0x1302 */
  556. NULL, /* The OpenSSL cipher name matches to the IANA name */
  557. CIPHER_STRONG_ENOUGH),
  558. CIPHER_DEF(TLS_CHACHA20_POLY1305_SHA256, /* 0x1303 */
  559. NULL, /* The OpenSSL cipher name matches to the IANA name */
  560. CIPHER_STRONG_ENOUGH),
  561. CIPHER_DEF(TLS_AES_128_CCM_SHA256, /* 0x1304 */
  562. NULL, /* The OpenSSL cipher name matches to the IANA name */
  563. CIPHER_STRONG_ENOUGH),
  564. CIPHER_DEF(TLS_AES_128_CCM_8_SHA256, /* 0x1305 */
  565. NULL, /* The OpenSSL cipher name matches to the IANA name */
  566. CIPHER_STRONG_ENOUGH),
  567. #endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */
  568. #if CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS
  569. /* ECDSA addenda, RFC 4492 */
  570. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_NULL_SHA, /* 0xC001 */
  571. "ECDH-ECDSA-NULL-SHA",
  572. CIPHER_WEAK_NOT_ENCRYPTED),
  573. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_RC4_128_SHA, /* 0xC002 */
  574. "ECDH-ECDSA-RC4-SHA",
  575. CIPHER_WEAK_RC_ENCRYPTION),
  576. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA, /* 0xC003 */
  577. "ECDH-ECDSA-DES-CBC3-SHA",
  578. CIPHER_WEAK_3DES_ENCRYPTION),
  579. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, /* 0xC004 */
  580. "ECDH-ECDSA-AES128-SHA",
  581. CIPHER_STRONG_ENOUGH),
  582. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA, /* 0xC005 */
  583. "ECDH-ECDSA-AES256-SHA",
  584. CIPHER_STRONG_ENOUGH),
  585. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_NULL_SHA, /* 0xC006 */
  586. "ECDHE-ECDSA-NULL-SHA",
  587. CIPHER_WEAK_NOT_ENCRYPTED),
  588. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_RC4_128_SHA, /* 0xC007 */
  589. "ECDHE-ECDSA-RC4-SHA",
  590. CIPHER_WEAK_RC_ENCRYPTION),
  591. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA, /* 0xC008 */
  592. "ECDHE-ECDSA-DES-CBC3-SHA",
  593. CIPHER_WEAK_3DES_ENCRYPTION),
  594. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, /* 0xC009 */
  595. "ECDHE-ECDSA-AES128-SHA",
  596. CIPHER_STRONG_ENOUGH),
  597. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, /* 0xC00A */
  598. "ECDHE-ECDSA-AES256-SHA",
  599. CIPHER_STRONG_ENOUGH),
  600. CIPHER_DEF(TLS_ECDH_RSA_WITH_NULL_SHA, /* 0xC00B */
  601. "ECDH-RSA-NULL-SHA",
  602. CIPHER_WEAK_NOT_ENCRYPTED),
  603. CIPHER_DEF(TLS_ECDH_RSA_WITH_RC4_128_SHA, /* 0xC00C */
  604. "ECDH-RSA-RC4-SHA",
  605. CIPHER_WEAK_RC_ENCRYPTION),
  606. CIPHER_DEF(TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA, /* 0xC00D */
  607. "ECDH-RSA-DES-CBC3-SHA",
  608. CIPHER_WEAK_3DES_ENCRYPTION),
  609. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, /* 0xC00E */
  610. "ECDH-RSA-AES128-SHA",
  611. CIPHER_STRONG_ENOUGH),
  612. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA, /* 0xC00F */
  613. "ECDH-RSA-AES256-SHA",
  614. CIPHER_STRONG_ENOUGH),
  615. CIPHER_DEF(TLS_ECDHE_RSA_WITH_NULL_SHA, /* 0xC010 */
  616. "ECDHE-RSA-NULL-SHA",
  617. CIPHER_WEAK_NOT_ENCRYPTED),
  618. CIPHER_DEF(TLS_ECDHE_RSA_WITH_RC4_128_SHA, /* 0xC011 */
  619. "ECDHE-RSA-RC4-SHA",
  620. CIPHER_WEAK_RC_ENCRYPTION),
  621. CIPHER_DEF(TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA, /* 0xC012 */
  622. "ECDHE-RSA-DES-CBC3-SHA",
  623. CIPHER_WEAK_3DES_ENCRYPTION),
  624. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, /* 0xC013 */
  625. "ECDHE-RSA-AES128-SHA",
  626. CIPHER_STRONG_ENOUGH),
  627. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, /* 0xC014 */
  628. "ECDHE-RSA-AES256-SHA",
  629. CIPHER_STRONG_ENOUGH),
  630. CIPHER_DEF(TLS_ECDH_anon_WITH_NULL_SHA, /* 0xC015 */
  631. "AECDH-NULL-SHA",
  632. CIPHER_WEAK_ANON_AUTH),
  633. CIPHER_DEF(TLS_ECDH_anon_WITH_RC4_128_SHA, /* 0xC016 */
  634. "AECDH-RC4-SHA",
  635. CIPHER_WEAK_ANON_AUTH),
  636. CIPHER_DEF(TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA, /* 0xC017 */
  637. "AECDH-DES-CBC3-SHA",
  638. CIPHER_WEAK_3DES_ENCRYPTION),
  639. CIPHER_DEF(TLS_ECDH_anon_WITH_AES_128_CBC_SHA, /* 0xC018 */
  640. "AECDH-AES128-SHA",
  641. CIPHER_WEAK_ANON_AUTH),
  642. CIPHER_DEF(TLS_ECDH_anon_WITH_AES_256_CBC_SHA, /* 0xC019 */
  643. "AECDH-AES256-SHA",
  644. CIPHER_WEAK_ANON_AUTH),
  645. #endif /* CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS */
  646. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  647. /* Addenda from rfc 5289 Elliptic Curve Cipher Suites with
  648. HMAC SHA-256/384. */
  649. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, /* 0xC023 */
  650. "ECDHE-ECDSA-AES128-SHA256",
  651. CIPHER_STRONG_ENOUGH),
  652. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, /* 0xC024 */
  653. "ECDHE-ECDSA-AES256-SHA384",
  654. CIPHER_STRONG_ENOUGH),
  655. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, /* 0xC025 */
  656. "ECDH-ECDSA-AES128-SHA256",
  657. CIPHER_STRONG_ENOUGH),
  658. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384, /* 0xC026 */
  659. "ECDH-ECDSA-AES256-SHA384",
  660. CIPHER_STRONG_ENOUGH),
  661. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, /* 0xC027 */
  662. "ECDHE-RSA-AES128-SHA256",
  663. CIPHER_STRONG_ENOUGH),
  664. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, /* 0xC028 */
  665. "ECDHE-RSA-AES256-SHA384",
  666. CIPHER_STRONG_ENOUGH),
  667. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, /* 0xC029 */
  668. "ECDH-RSA-AES128-SHA256",
  669. CIPHER_STRONG_ENOUGH),
  670. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384, /* 0xC02A */
  671. "ECDH-RSA-AES256-SHA384",
  672. CIPHER_STRONG_ENOUGH),
  673. /* Addenda from rfc 5289 Elliptic Curve Cipher Suites with
  674. SHA-256/384 and AES Galois Counter Mode (GCM) */
  675. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, /* 0xC02B */
  676. "ECDHE-ECDSA-AES128-GCM-SHA256",
  677. CIPHER_STRONG_ENOUGH),
  678. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, /* 0xC02C */
  679. "ECDHE-ECDSA-AES256-GCM-SHA384",
  680. CIPHER_STRONG_ENOUGH),
  681. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, /* 0xC02D */
  682. "ECDH-ECDSA-AES128-GCM-SHA256",
  683. CIPHER_STRONG_ENOUGH),
  684. CIPHER_DEF(TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384, /* 0xC02E */
  685. "ECDH-ECDSA-AES256-GCM-SHA384",
  686. CIPHER_STRONG_ENOUGH),
  687. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* 0xC02F */
  688. "ECDHE-RSA-AES128-GCM-SHA256",
  689. CIPHER_STRONG_ENOUGH),
  690. CIPHER_DEF(TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, /* 0xC030 */
  691. "ECDHE-RSA-AES256-GCM-SHA384",
  692. CIPHER_STRONG_ENOUGH),
  693. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, /* 0xC031 */
  694. "ECDH-RSA-AES128-GCM-SHA256",
  695. CIPHER_STRONG_ENOUGH),
  696. CIPHER_DEF(TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384, /* 0xC032 */
  697. "ECDH-RSA-AES256-GCM-SHA384",
  698. CIPHER_STRONG_ENOUGH),
  699. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  700. #if CURL_BUILD_MAC_10_15 || CURL_BUILD_IOS_13
  701. /* ECDHE_PSK Cipher Suites for Transport Layer Security (TLS), RFC 5489 */
  702. CIPHER_DEF(TLS_ECDHE_PSK_WITH_AES_128_CBC_SHA, /* 0xC035 */
  703. "ECDHE-PSK-AES128-CBC-SHA",
  704. CIPHER_STRONG_ENOUGH),
  705. CIPHER_DEF(TLS_ECDHE_PSK_WITH_AES_256_CBC_SHA, /* 0xC036 */
  706. "ECDHE-PSK-AES256-CBC-SHA",
  707. CIPHER_STRONG_ENOUGH),
  708. #endif /* CURL_BUILD_MAC_10_15 || CURL_BUILD_IOS_13 */
  709. #if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11
  710. /* Addenda from rfc 7905 ChaCha20-Poly1305 Cipher Suites for
  711. Transport Layer Security (TLS). */
  712. CIPHER_DEF(TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA8 */
  713. "ECDHE-RSA-CHACHA20-POLY1305",
  714. CIPHER_STRONG_ENOUGH),
  715. CIPHER_DEF(TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA9 */
  716. "ECDHE-ECDSA-CHACHA20-POLY1305",
  717. CIPHER_STRONG_ENOUGH),
  718. #endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */
  719. #if CURL_BUILD_MAC_10_15 || CURL_BUILD_IOS_13
  720. /* ChaCha20-Poly1305 Cipher Suites for Transport Layer Security (TLS),
  721. RFC 7905 */
  722. CIPHER_DEF(TLS_PSK_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCAB */
  723. "PSK-CHACHA20-POLY1305",
  724. CIPHER_STRONG_ENOUGH),
  725. #endif /* CURL_BUILD_MAC_10_15 || CURL_BUILD_IOS_13 */
  726. /* Tags for SSL 2 cipher kinds which are not specified for SSL 3.
  727. Defined since SDK 10.2.8 */
  728. CIPHER_DEF(SSL_RSA_WITH_RC2_CBC_MD5, /* 0xFF80 */
  729. NULL,
  730. CIPHER_WEAK_RC_ENCRYPTION),
  731. CIPHER_DEF(SSL_RSA_WITH_IDEA_CBC_MD5, /* 0xFF81 */
  732. NULL,
  733. CIPHER_WEAK_IDEA_ENCRYPTION),
  734. CIPHER_DEF(SSL_RSA_WITH_DES_CBC_MD5, /* 0xFF82 */
  735. NULL,
  736. CIPHER_WEAK_DES_ENCRYPTION),
  737. CIPHER_DEF(SSL_RSA_WITH_3DES_EDE_CBC_MD5, /* 0xFF83 */
  738. NULL,
  739. CIPHER_WEAK_3DES_ENCRYPTION),
  740. };
  741. #define NUM_OF_CIPHERS sizeof(ciphertable)/sizeof(ciphertable[0])
  742. /* pinned public key support tests */
  743. /* version 1 supports macOS 10.12+ and iOS 10+ */
  744. #if ((TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || \
  745. (!TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200))
  746. #define SECTRANSP_PINNEDPUBKEY_V1 1
  747. #endif
  748. /* version 2 supports MacOSX 10.7+ */
  749. #if (!TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)
  750. #define SECTRANSP_PINNEDPUBKEY_V2 1
  751. #endif
  752. #if defined(SECTRANSP_PINNEDPUBKEY_V1) || defined(SECTRANSP_PINNEDPUBKEY_V2)
  753. /* this backend supports CURLOPT_PINNEDPUBLICKEY */
  754. #define SECTRANSP_PINNEDPUBKEY 1
  755. #endif /* SECTRANSP_PINNEDPUBKEY */
  756. #ifdef SECTRANSP_PINNEDPUBKEY
  757. /* both new and old APIs return rsa keys missing the spki header (not DER) */
  758. static const unsigned char rsa4096SpkiHeader[] = {
  759. 0x30, 0x82, 0x02, 0x22, 0x30, 0x0d,
  760. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  761. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
  762. 0x00, 0x03, 0x82, 0x02, 0x0f, 0x00};
  763. static const unsigned char rsa2048SpkiHeader[] = {
  764. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d,
  765. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  766. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
  767. 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00};
  768. #ifdef SECTRANSP_PINNEDPUBKEY_V1
  769. /* the *new* version doesn't return DER encoded ecdsa certs like the old... */
  770. static const unsigned char ecDsaSecp256r1SpkiHeader[] = {
  771. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07,
  772. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  773. 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48,
  774. 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  775. 0x42, 0x00};
  776. static const unsigned char ecDsaSecp384r1SpkiHeader[] = {
  777. 0x30, 0x76, 0x30, 0x10, 0x06, 0x07,
  778. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  779. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04,
  780. 0x00, 0x22, 0x03, 0x62, 0x00};
  781. #endif /* SECTRANSP_PINNEDPUBKEY_V1 */
  782. #endif /* SECTRANSP_PINNEDPUBKEY */
  783. static OSStatus sectransp_bio_cf_in_read(SSLConnectionRef connection,
  784. void *buf,
  785. size_t *dataLength) /* IN/OUT */
  786. {
  787. struct Curl_cfilter *cf = (struct Curl_cfilter *)connection;
  788. struct ssl_connect_data *connssl = cf->ctx;
  789. struct st_ssl_backend_data *backend =
  790. (struct st_ssl_backend_data *)connssl->backend;
  791. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  792. ssize_t nread;
  793. CURLcode result;
  794. OSStatus rtn = noErr;
  795. DEBUGASSERT(data);
  796. nread = Curl_conn_cf_recv(cf->next, data, buf, *dataLength, &result);
  797. CURL_TRC_CF(data, cf, "bio_read(len=%zu) -> %zd, result=%d",
  798. *dataLength, nread, result);
  799. if(nread < 0) {
  800. switch(result) {
  801. case CURLE_OK:
  802. case CURLE_AGAIN:
  803. rtn = errSSLWouldBlock;
  804. backend->ssl_direction = false;
  805. break;
  806. default:
  807. rtn = ioErr;
  808. break;
  809. }
  810. nread = 0;
  811. }
  812. else if(nread == 0) {
  813. rtn = errSSLClosedGraceful;
  814. }
  815. else if((size_t)nread < *dataLength) {
  816. rtn = errSSLWouldBlock;
  817. }
  818. *dataLength = nread;
  819. return rtn;
  820. }
  821. static OSStatus sectransp_bio_cf_out_write(SSLConnectionRef connection,
  822. const void *buf,
  823. size_t *dataLength) /* IN/OUT */
  824. {
  825. struct Curl_cfilter *cf = (struct Curl_cfilter *)connection;
  826. struct ssl_connect_data *connssl = cf->ctx;
  827. struct st_ssl_backend_data *backend =
  828. (struct st_ssl_backend_data *)connssl->backend;
  829. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  830. ssize_t nwritten;
  831. CURLcode result;
  832. OSStatus rtn = noErr;
  833. DEBUGASSERT(data);
  834. nwritten = Curl_conn_cf_send(cf->next, data, buf, *dataLength, &result);
  835. CURL_TRC_CF(data, cf, "bio_send(len=%zu) -> %zd, result=%d",
  836. *dataLength, nwritten, result);
  837. if(nwritten <= 0) {
  838. if(result == CURLE_AGAIN) {
  839. rtn = errSSLWouldBlock;
  840. backend->ssl_direction = true;
  841. }
  842. else {
  843. rtn = ioErr;
  844. }
  845. nwritten = 0;
  846. }
  847. else if((size_t)nwritten < *dataLength) {
  848. rtn = errSSLWouldBlock;
  849. }
  850. *dataLength = nwritten;
  851. return rtn;
  852. }
  853. CF_INLINE const char *TLSCipherNameForNumber(SSLCipherSuite cipher)
  854. {
  855. /* The first ciphers in the ciphertable are continuous. Here we do small
  856. optimization and instead of loop directly get SSL name by cipher number.
  857. */
  858. size_t i;
  859. if(cipher <= SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA) {
  860. return ciphertable[cipher].name;
  861. }
  862. /* Iterate through the rest of the ciphers */
  863. for(i = SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA + 1; i < NUM_OF_CIPHERS;
  864. ++i) {
  865. if(ciphertable[i].num == cipher) {
  866. return ciphertable[i].name;
  867. }
  868. }
  869. return ciphertable[SSL_NULL_WITH_NULL_NULL].name;
  870. }
  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(1, cbuf_size);
  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. /* SecItemCopyMatching() was introduced in iOS and Snow Leopard.
  1019. kSecClassIdentity was introduced in Lion. If both exist, let's use them
  1020. to find the certificate. */
  1021. if(SecItemCopyMatching && kSecClassIdentity) {
  1022. CFTypeRef keys[5];
  1023. CFTypeRef values[5];
  1024. CFDictionaryRef query_dict;
  1025. CFStringRef label_cf = CFStringCreateWithCString(NULL, label,
  1026. kCFStringEncodingUTF8);
  1027. /* Set up our search criteria and expected results: */
  1028. values[0] = kSecClassIdentity; /* we want a certificate and a key */
  1029. keys[0] = kSecClass;
  1030. values[1] = kCFBooleanTrue; /* we want a reference */
  1031. keys[1] = kSecReturnRef;
  1032. values[2] = kSecMatchLimitAll; /* kSecMatchLimitOne would be better if the
  1033. * label matching below worked correctly */
  1034. keys[2] = kSecMatchLimit;
  1035. /* identity searches need a SecPolicyRef in order to work */
  1036. values[3] = SecPolicyCreateSSL(false, NULL);
  1037. keys[3] = kSecMatchPolicy;
  1038. /* match the name of the certificate (doesn't work in macOS 10.12.1) */
  1039. values[4] = label_cf;
  1040. keys[4] = kSecAttrLabel;
  1041. query_dict = CFDictionaryCreate(NULL, (const void **)keys,
  1042. (const void **)values, 5L,
  1043. &kCFCopyStringDictionaryKeyCallBacks,
  1044. &kCFTypeDictionaryValueCallBacks);
  1045. CFRelease(values[3]);
  1046. /* Do we have a match? */
  1047. status = SecItemCopyMatching(query_dict, (CFTypeRef *) &keys_list);
  1048. /* Because kSecAttrLabel matching doesn't work with kSecClassIdentity,
  1049. * we need to find the correct identity ourselves */
  1050. if(status == noErr) {
  1051. keys_list_count = CFArrayGetCount(keys_list);
  1052. *out_cert_and_key = NULL;
  1053. status = 1;
  1054. for(i = 0; i<keys_list_count; i++) {
  1055. OSStatus err = noErr;
  1056. SecCertificateRef cert = NULL;
  1057. SecIdentityRef identity =
  1058. (SecIdentityRef) CFArrayGetValueAtIndex(keys_list, i);
  1059. err = SecIdentityCopyCertificate(identity, &cert);
  1060. if(err == noErr) {
  1061. CFStringRef common_name = NULL;
  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. if(common_name)
  1078. CFRelease(common_name);
  1079. }
  1080. CFRelease(cert);
  1081. }
  1082. }
  1083. if(keys_list)
  1084. CFRelease(keys_list);
  1085. CFRelease(query_dict);
  1086. CFRelease(label_cf);
  1087. }
  1088. else {
  1089. #if CURL_SUPPORT_MAC_10_6
  1090. /* On Leopard and Snow Leopard, fall back to SecKeychainSearch. */
  1091. status = CopyIdentityWithLabelOldSchool(label, out_cert_and_key);
  1092. #endif /* CURL_SUPPORT_MAC_10_6 */
  1093. }
  1094. #elif CURL_SUPPORT_MAC_10_6
  1095. /* For developers building on older cats, we have no choice but to fall back
  1096. to SecKeychainSearch. */
  1097. status = CopyIdentityWithLabelOldSchool(label, out_cert_and_key);
  1098. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  1099. return status;
  1100. }
  1101. static OSStatus CopyIdentityFromPKCS12File(const char *cPath,
  1102. const struct curl_blob *blob,
  1103. const char *cPassword,
  1104. SecIdentityRef *out_cert_and_key)
  1105. {
  1106. OSStatus status = errSecItemNotFound;
  1107. CFURLRef pkcs_url = NULL;
  1108. CFStringRef password = cPassword ? CFStringCreateWithCString(NULL,
  1109. cPassword, kCFStringEncodingUTF8) : NULL;
  1110. CFDataRef pkcs_data = NULL;
  1111. /* We can import P12 files on iOS or OS X 10.7 or later: */
  1112. /* These constants are documented as having first appeared in 10.6 but they
  1113. raise linker errors when used on that cat for some reason. */
  1114. #if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS
  1115. bool resource_imported;
  1116. if(blob) {
  1117. pkcs_data = CFDataCreate(kCFAllocatorDefault,
  1118. (const unsigned char *)blob->data, blob->len);
  1119. status = (pkcs_data != NULL) ? errSecSuccess : errSecAllocate;
  1120. resource_imported = (pkcs_data != NULL);
  1121. }
  1122. else {
  1123. pkcs_url =
  1124. CFURLCreateFromFileSystemRepresentation(NULL,
  1125. (const UInt8 *)cPath,
  1126. strlen(cPath), false);
  1127. resource_imported =
  1128. CFURLCreateDataAndPropertiesFromResource(NULL,
  1129. pkcs_url, &pkcs_data,
  1130. NULL, NULL, &status);
  1131. }
  1132. if(resource_imported) {
  1133. CFArrayRef items = NULL;
  1134. /* On iOS SecPKCS12Import will never add the client certificate to the
  1135. * Keychain.
  1136. *
  1137. * It gives us back a SecIdentityRef that we can use directly. */
  1138. #if CURL_BUILD_IOS
  1139. const void *cKeys[] = {kSecImportExportPassphrase};
  1140. const void *cValues[] = {password};
  1141. CFDictionaryRef options = CFDictionaryCreate(NULL, cKeys, cValues,
  1142. password ? 1L : 0L, NULL, NULL);
  1143. if(options) {
  1144. status = SecPKCS12Import(pkcs_data, options, &items);
  1145. CFRelease(options);
  1146. }
  1147. /* On macOS SecPKCS12Import will always add the client certificate to
  1148. * the Keychain.
  1149. *
  1150. * As this doesn't match iOS, and apps may not want to see their client
  1151. * certificate saved in the user's keychain, we use SecItemImport
  1152. * with a NULL keychain to avoid importing it.
  1153. *
  1154. * This returns a SecCertificateRef from which we can construct a
  1155. * SecIdentityRef.
  1156. */
  1157. #elif CURL_BUILD_MAC_10_7
  1158. SecItemImportExportKeyParameters keyParams;
  1159. SecExternalFormat inputFormat = kSecFormatPKCS12;
  1160. SecExternalItemType inputType = kSecItemTypeCertificate;
  1161. memset(&keyParams, 0x00, sizeof(keyParams));
  1162. keyParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
  1163. keyParams.passphrase = password;
  1164. status = SecItemImport(pkcs_data, NULL, &inputFormat, &inputType,
  1165. 0, &keyParams, NULL, &items);
  1166. #endif
  1167. /* Extract the SecIdentityRef */
  1168. if(status == errSecSuccess && items && CFArrayGetCount(items)) {
  1169. CFIndex i, count;
  1170. count = CFArrayGetCount(items);
  1171. for(i = 0; i < count; i++) {
  1172. CFTypeRef item = (CFTypeRef) CFArrayGetValueAtIndex(items, i);
  1173. CFTypeID itemID = CFGetTypeID(item);
  1174. if(itemID == CFDictionaryGetTypeID()) {
  1175. CFTypeRef identity = (CFTypeRef) CFDictionaryGetValue(
  1176. (CFDictionaryRef) item,
  1177. kSecImportItemIdentity);
  1178. CFRetain(identity);
  1179. *out_cert_and_key = (SecIdentityRef) identity;
  1180. break;
  1181. }
  1182. #if CURL_BUILD_MAC_10_7
  1183. else if(itemID == SecCertificateGetTypeID()) {
  1184. status = SecIdentityCreateWithCertificate(NULL,
  1185. (SecCertificateRef) item,
  1186. out_cert_and_key);
  1187. break;
  1188. }
  1189. #endif
  1190. }
  1191. }
  1192. if(items)
  1193. CFRelease(items);
  1194. CFRelease(pkcs_data);
  1195. }
  1196. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  1197. if(password)
  1198. CFRelease(password);
  1199. if(pkcs_url)
  1200. CFRelease(pkcs_url);
  1201. return status;
  1202. }
  1203. /* This code was borrowed from nss.c, with some modifications:
  1204. * Determine whether the nickname passed in is a filename that needs to
  1205. * be loaded as a PEM or a nickname.
  1206. *
  1207. * returns 1 for a file
  1208. * returns 0 for not a file
  1209. */
  1210. CF_INLINE bool is_file(const char *filename)
  1211. {
  1212. struct_stat st;
  1213. if(!filename)
  1214. return false;
  1215. if(stat(filename, &st) == 0)
  1216. return S_ISREG(st.st_mode);
  1217. return false;
  1218. }
  1219. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1220. static CURLcode sectransp_version_from_curl(SSLProtocol *darwinver,
  1221. long ssl_version)
  1222. {
  1223. switch(ssl_version) {
  1224. case CURL_SSLVERSION_TLSv1_0:
  1225. *darwinver = kTLSProtocol1;
  1226. return CURLE_OK;
  1227. case CURL_SSLVERSION_TLSv1_1:
  1228. *darwinver = kTLSProtocol11;
  1229. return CURLE_OK;
  1230. case CURL_SSLVERSION_TLSv1_2:
  1231. *darwinver = kTLSProtocol12;
  1232. return CURLE_OK;
  1233. case CURL_SSLVERSION_TLSv1_3:
  1234. /* TLS 1.3 support first appeared in iOS 11 and macOS 10.13 */
  1235. #if (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && HAVE_BUILTIN_AVAILABLE == 1
  1236. if(__builtin_available(macOS 10.13, iOS 11.0, *)) {
  1237. *darwinver = kTLSProtocol13;
  1238. return CURLE_OK;
  1239. }
  1240. #endif /* (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) &&
  1241. HAVE_BUILTIN_AVAILABLE == 1 */
  1242. break;
  1243. }
  1244. return CURLE_SSL_CONNECT_ERROR;
  1245. }
  1246. #endif
  1247. static CURLcode set_ssl_version_min_max(struct Curl_cfilter *cf,
  1248. struct Curl_easy *data)
  1249. {
  1250. struct ssl_connect_data *connssl = cf->ctx;
  1251. struct st_ssl_backend_data *backend =
  1252. (struct st_ssl_backend_data *)connssl->backend;
  1253. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  1254. long ssl_version = conn_config->version;
  1255. long ssl_version_max = conn_config->version_max;
  1256. long max_supported_version_by_os;
  1257. DEBUGASSERT(backend);
  1258. /* macOS 10.5-10.7 supported TLS 1.0 only.
  1259. macOS 10.8 and later, and iOS 5 and later, added TLS 1.1 and 1.2.
  1260. macOS 10.13 and later, and iOS 11 and later, added TLS 1.3. */
  1261. #if (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && HAVE_BUILTIN_AVAILABLE == 1
  1262. if(__builtin_available(macOS 10.13, iOS 11.0, *)) {
  1263. max_supported_version_by_os = CURL_SSLVERSION_MAX_TLSv1_3;
  1264. }
  1265. else {
  1266. max_supported_version_by_os = CURL_SSLVERSION_MAX_TLSv1_2;
  1267. }
  1268. #else
  1269. max_supported_version_by_os = CURL_SSLVERSION_MAX_TLSv1_2;
  1270. #endif /* (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) &&
  1271. HAVE_BUILTIN_AVAILABLE == 1 */
  1272. switch(ssl_version) {
  1273. case CURL_SSLVERSION_DEFAULT:
  1274. case CURL_SSLVERSION_TLSv1:
  1275. ssl_version = CURL_SSLVERSION_TLSv1_0;
  1276. break;
  1277. }
  1278. switch(ssl_version_max) {
  1279. case CURL_SSLVERSION_MAX_NONE:
  1280. case CURL_SSLVERSION_MAX_DEFAULT:
  1281. ssl_version_max = max_supported_version_by_os;
  1282. break;
  1283. }
  1284. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1285. if(SSLSetProtocolVersionMax) {
  1286. SSLProtocol darwin_ver_min = kTLSProtocol1;
  1287. SSLProtocol darwin_ver_max = kTLSProtocol1;
  1288. CURLcode result = sectransp_version_from_curl(&darwin_ver_min,
  1289. ssl_version);
  1290. if(result) {
  1291. failf(data, "unsupported min version passed via CURLOPT_SSLVERSION");
  1292. return result;
  1293. }
  1294. result = sectransp_version_from_curl(&darwin_ver_max,
  1295. ssl_version_max >> 16);
  1296. if(result) {
  1297. failf(data, "unsupported max version passed via CURLOPT_SSLVERSION");
  1298. return result;
  1299. }
  1300. (void)SSLSetProtocolVersionMin(backend->ssl_ctx, darwin_ver_min);
  1301. (void)SSLSetProtocolVersionMax(backend->ssl_ctx, darwin_ver_max);
  1302. return result;
  1303. }
  1304. else {
  1305. #if CURL_SUPPORT_MAC_10_8
  1306. long i = ssl_version;
  1307. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1308. kSSLProtocolAll,
  1309. false);
  1310. for(; i <= (ssl_version_max >> 16); i++) {
  1311. switch(i) {
  1312. case CURL_SSLVERSION_TLSv1_0:
  1313. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1314. kTLSProtocol1,
  1315. true);
  1316. break;
  1317. case CURL_SSLVERSION_TLSv1_1:
  1318. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1319. kTLSProtocol11,
  1320. true);
  1321. break;
  1322. case CURL_SSLVERSION_TLSv1_2:
  1323. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1324. kTLSProtocol12,
  1325. true);
  1326. break;
  1327. case CURL_SSLVERSION_TLSv1_3:
  1328. failf(data, "Your version of the OS does not support TLSv1.3");
  1329. return CURLE_SSL_CONNECT_ERROR;
  1330. }
  1331. }
  1332. return CURLE_OK;
  1333. #endif /* CURL_SUPPORT_MAC_10_8 */
  1334. }
  1335. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  1336. failf(data, "Secure Transport: cannot set SSL protocol");
  1337. return CURLE_SSL_CONNECT_ERROR;
  1338. }
  1339. static bool is_cipher_suite_strong(SSLCipherSuite suite_num)
  1340. {
  1341. size_t i;
  1342. for(i = 0; i < NUM_OF_CIPHERS; ++i) {
  1343. if(ciphertable[i].num == suite_num) {
  1344. return !ciphertable[i].weak;
  1345. }
  1346. }
  1347. /* If the cipher is not in our list, assume it is a new one
  1348. and therefore strong. Previous implementation was the same,
  1349. if cipher suite is not in the list, it was considered strong enough */
  1350. return true;
  1351. }
  1352. static bool sectransp_is_separator(char c)
  1353. {
  1354. /* Return whether character is a cipher list separator. */
  1355. switch(c) {
  1356. case ' ':
  1357. case '\t':
  1358. case ':':
  1359. case ',':
  1360. case ';':
  1361. return true;
  1362. }
  1363. return false;
  1364. }
  1365. static CURLcode sectransp_set_default_ciphers(struct Curl_easy *data,
  1366. SSLContextRef ssl_ctx)
  1367. {
  1368. size_t all_ciphers_count = 0UL, allowed_ciphers_count = 0UL, i;
  1369. SSLCipherSuite *all_ciphers = NULL, *allowed_ciphers = NULL;
  1370. OSStatus err = noErr;
  1371. #if CURL_BUILD_MAC
  1372. int darwinver_maj = 0, darwinver_min = 0;
  1373. GetDarwinVersionNumber(&darwinver_maj, &darwinver_min);
  1374. #endif /* CURL_BUILD_MAC */
  1375. /* Disable cipher suites that ST supports but are not safe. These ciphers
  1376. are unlikely to be used in any case since ST gives other ciphers a much
  1377. higher priority, but it's probably better that we not connect at all than
  1378. to give the user a false sense of security if the server only supports
  1379. insecure ciphers. (Note: We don't care about SSLv2-only ciphers.) */
  1380. err = SSLGetNumberSupportedCiphers(ssl_ctx, &all_ciphers_count);
  1381. if(err != noErr) {
  1382. failf(data, "SSL: SSLGetNumberSupportedCiphers() failed: OSStatus %d",
  1383. err);
  1384. return CURLE_SSL_CIPHER;
  1385. }
  1386. all_ciphers = malloc(all_ciphers_count*sizeof(SSLCipherSuite));
  1387. if(!all_ciphers) {
  1388. failf(data, "SSL: Failed to allocate memory for all ciphers");
  1389. return CURLE_OUT_OF_MEMORY;
  1390. }
  1391. allowed_ciphers = malloc(all_ciphers_count*sizeof(SSLCipherSuite));
  1392. if(!allowed_ciphers) {
  1393. Curl_safefree(all_ciphers);
  1394. failf(data, "SSL: Failed to allocate memory for allowed ciphers");
  1395. return CURLE_OUT_OF_MEMORY;
  1396. }
  1397. err = SSLGetSupportedCiphers(ssl_ctx, all_ciphers,
  1398. &all_ciphers_count);
  1399. if(err != noErr) {
  1400. Curl_safefree(all_ciphers);
  1401. Curl_safefree(allowed_ciphers);
  1402. return CURLE_SSL_CIPHER;
  1403. }
  1404. for(i = 0UL ; i < all_ciphers_count ; i++) {
  1405. #if CURL_BUILD_MAC
  1406. /* There's a known bug in early versions of Mountain Lion where ST's ECC
  1407. ciphers (cipher suite 0xC001 through 0xC032) simply do not work.
  1408. Work around the problem here by disabling those ciphers if we are
  1409. running in an affected version of OS X. */
  1410. if(darwinver_maj == 12 && darwinver_min <= 3 &&
  1411. all_ciphers[i] >= 0xC001 && all_ciphers[i] <= 0xC032) {
  1412. continue;
  1413. }
  1414. #endif /* CURL_BUILD_MAC */
  1415. if(is_cipher_suite_strong(all_ciphers[i])) {
  1416. allowed_ciphers[allowed_ciphers_count++] = all_ciphers[i];
  1417. }
  1418. }
  1419. err = SSLSetEnabledCiphers(ssl_ctx, allowed_ciphers,
  1420. allowed_ciphers_count);
  1421. Curl_safefree(all_ciphers);
  1422. Curl_safefree(allowed_ciphers);
  1423. if(err != noErr) {
  1424. failf(data, "SSL: SSLSetEnabledCiphers() failed: OSStatus %d", err);
  1425. return CURLE_SSL_CIPHER;
  1426. }
  1427. return CURLE_OK;
  1428. }
  1429. static CURLcode sectransp_set_selected_ciphers(struct Curl_easy *data,
  1430. SSLContextRef ssl_ctx,
  1431. const char *ciphers)
  1432. {
  1433. size_t ciphers_count = 0;
  1434. const char *cipher_start = ciphers;
  1435. OSStatus err = noErr;
  1436. SSLCipherSuite selected_ciphers[NUM_OF_CIPHERS];
  1437. if(!ciphers)
  1438. return CURLE_OK;
  1439. while(sectransp_is_separator(*ciphers)) /* Skip initial separators. */
  1440. ciphers++;
  1441. if(!*ciphers)
  1442. return CURLE_OK;
  1443. cipher_start = ciphers;
  1444. while(*cipher_start && ciphers_count < NUM_OF_CIPHERS) {
  1445. bool cipher_found = FALSE;
  1446. size_t cipher_len = 0;
  1447. const char *cipher_end = NULL;
  1448. bool tls_name = FALSE;
  1449. size_t i;
  1450. /* Skip separators */
  1451. while(sectransp_is_separator(*cipher_start))
  1452. cipher_start++;
  1453. if(*cipher_start == '\0') {
  1454. break;
  1455. }
  1456. /* Find last position of a cipher in the ciphers string */
  1457. cipher_end = cipher_start;
  1458. while(*cipher_end != '\0' && !sectransp_is_separator(*cipher_end)) {
  1459. ++cipher_end;
  1460. }
  1461. /* IANA cipher names start with the TLS_ or SSL_ prefix.
  1462. If the 4th symbol of the cipher is '_' we look for a cipher in the
  1463. table by its (TLS) name.
  1464. Otherwise, we try to match cipher by an alias. */
  1465. if(cipher_start[3] == '_') {
  1466. tls_name = TRUE;
  1467. }
  1468. /* Iterate through the cipher table and look for the cipher, starting
  1469. the cipher number 0x01 because the 0x00 is not the real cipher */
  1470. cipher_len = cipher_end - cipher_start;
  1471. for(i = 1; i < NUM_OF_CIPHERS; ++i) {
  1472. const char *table_cipher_name = NULL;
  1473. if(tls_name) {
  1474. table_cipher_name = ciphertable[i].name;
  1475. }
  1476. else if(ciphertable[i].alias_name) {
  1477. table_cipher_name = ciphertable[i].alias_name;
  1478. }
  1479. else {
  1480. continue;
  1481. }
  1482. /* Compare a part of the string between separators with a cipher name
  1483. in the table and make sure we matched the whole cipher name */
  1484. if(strncmp(cipher_start, table_cipher_name, cipher_len) == 0
  1485. && table_cipher_name[cipher_len] == '\0') {
  1486. selected_ciphers[ciphers_count] = ciphertable[i].num;
  1487. ++ciphers_count;
  1488. cipher_found = TRUE;
  1489. break;
  1490. }
  1491. }
  1492. if(!cipher_found) {
  1493. /* It would be more human-readable if we print the wrong cipher name
  1494. but we don't want to allocate any additional memory and copy the name
  1495. into it, then add it into logs.
  1496. Also, we do not modify an original cipher list string. We just point
  1497. to positions where cipher starts and ends in the cipher list string.
  1498. The message is a bit cryptic and longer than necessary but can be
  1499. understood by humans. */
  1500. failf(data, "SSL: cipher string \"%s\" contains unsupported cipher name"
  1501. " starting position %zd and ending position %zd",
  1502. ciphers,
  1503. cipher_start - ciphers,
  1504. cipher_end - ciphers);
  1505. return CURLE_SSL_CIPHER;
  1506. }
  1507. if(*cipher_end) {
  1508. cipher_start = cipher_end + 1;
  1509. }
  1510. else {
  1511. break;
  1512. }
  1513. }
  1514. /* All cipher suites in the list are found. Report to logs as-is */
  1515. infof(data, "SSL: Setting cipher suites list \"%s\"", ciphers);
  1516. err = SSLSetEnabledCiphers(ssl_ctx, selected_ciphers, ciphers_count);
  1517. if(err != noErr) {
  1518. failf(data, "SSL: SSLSetEnabledCiphers() failed: OSStatus %d", err);
  1519. return CURLE_SSL_CIPHER;
  1520. }
  1521. return CURLE_OK;
  1522. }
  1523. static void sectransp_session_free(void *sessionid, size_t idsize)
  1524. {
  1525. /* ST, as of iOS 5 and Mountain Lion, has no public method of deleting a
  1526. cached session ID inside the Security framework. There is a private
  1527. function that does this, but I don't want to have to explain to you why I
  1528. got your application rejected from the App Store due to the use of a
  1529. private API, so the best we can do is free up our own char array that we
  1530. created way back in sectransp_connect_step1... */
  1531. (void)idsize;
  1532. Curl_safefree(sessionid);
  1533. }
  1534. static CURLcode sectransp_connect_step1(struct Curl_cfilter *cf,
  1535. struct Curl_easy *data)
  1536. {
  1537. struct ssl_connect_data *connssl = cf->ctx;
  1538. struct st_ssl_backend_data *backend =
  1539. (struct st_ssl_backend_data *)connssl->backend;
  1540. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  1541. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  1542. const struct curl_blob *ssl_cablob = conn_config->ca_info_blob;
  1543. const char * const ssl_cafile =
  1544. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  1545. (ssl_cablob ? NULL : conn_config->CAfile);
  1546. const bool verifypeer = conn_config->verifypeer;
  1547. char * const ssl_cert = ssl_config->primary.clientcert;
  1548. const struct curl_blob *ssl_cert_blob = ssl_config->primary.cert_blob;
  1549. char *ciphers;
  1550. OSStatus err = noErr;
  1551. #if CURL_BUILD_MAC
  1552. int darwinver_maj = 0, darwinver_min = 0;
  1553. DEBUGASSERT(backend);
  1554. CURL_TRC_CF(data, cf, "connect_step1");
  1555. GetDarwinVersionNumber(&darwinver_maj, &darwinver_min);
  1556. #endif /* CURL_BUILD_MAC */
  1557. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1558. if(SSLCreateContext) { /* use the newer API if available */
  1559. if(backend->ssl_ctx)
  1560. CFRelease(backend->ssl_ctx);
  1561. backend->ssl_ctx = SSLCreateContext(NULL, kSSLClientSide, kSSLStreamType);
  1562. if(!backend->ssl_ctx) {
  1563. failf(data, "SSL: couldn't create a context");
  1564. return CURLE_OUT_OF_MEMORY;
  1565. }
  1566. }
  1567. else {
  1568. /* The old ST API does not exist under iOS, so don't compile it: */
  1569. #if CURL_SUPPORT_MAC_10_8
  1570. if(backend->ssl_ctx)
  1571. (void)SSLDisposeContext(backend->ssl_ctx);
  1572. err = SSLNewContext(false, &(backend->ssl_ctx));
  1573. if(err != noErr) {
  1574. failf(data, "SSL: couldn't create a context: OSStatus %d", err);
  1575. return CURLE_OUT_OF_MEMORY;
  1576. }
  1577. #endif /* CURL_SUPPORT_MAC_10_8 */
  1578. }
  1579. #else
  1580. if(backend->ssl_ctx)
  1581. (void)SSLDisposeContext(backend->ssl_ctx);
  1582. err = SSLNewContext(false, &(backend->ssl_ctx));
  1583. if(err != noErr) {
  1584. failf(data, "SSL: couldn't create a context: OSStatus %d", err);
  1585. return CURLE_OUT_OF_MEMORY;
  1586. }
  1587. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  1588. backend->ssl_write_buffered_length = 0UL; /* reset buffered write length */
  1589. /* check to see if we've been told to use an explicit SSL/TLS version */
  1590. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1591. if(SSLSetProtocolVersionMax) {
  1592. switch(conn_config->version) {
  1593. case CURL_SSLVERSION_TLSv1:
  1594. (void)SSLSetProtocolVersionMin(backend->ssl_ctx, kTLSProtocol1);
  1595. #if (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && HAVE_BUILTIN_AVAILABLE == 1
  1596. if(__builtin_available(macOS 10.13, iOS 11.0, *)) {
  1597. (void)SSLSetProtocolVersionMax(backend->ssl_ctx, kTLSProtocol13);
  1598. }
  1599. else {
  1600. (void)SSLSetProtocolVersionMax(backend->ssl_ctx, kTLSProtocol12);
  1601. }
  1602. #else
  1603. (void)SSLSetProtocolVersionMax(backend->ssl_ctx, kTLSProtocol12);
  1604. #endif /* (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) &&
  1605. HAVE_BUILTIN_AVAILABLE == 1 */
  1606. break;
  1607. case CURL_SSLVERSION_DEFAULT:
  1608. case CURL_SSLVERSION_TLSv1_0:
  1609. case CURL_SSLVERSION_TLSv1_1:
  1610. case CURL_SSLVERSION_TLSv1_2:
  1611. case CURL_SSLVERSION_TLSv1_3:
  1612. {
  1613. CURLcode result = set_ssl_version_min_max(cf, data);
  1614. if(result != CURLE_OK)
  1615. return result;
  1616. break;
  1617. }
  1618. case CURL_SSLVERSION_SSLv3:
  1619. case CURL_SSLVERSION_SSLv2:
  1620. failf(data, "SSL versions not supported");
  1621. return CURLE_NOT_BUILT_IN;
  1622. default:
  1623. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  1624. return CURLE_SSL_CONNECT_ERROR;
  1625. }
  1626. }
  1627. else {
  1628. #if CURL_SUPPORT_MAC_10_8
  1629. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1630. kSSLProtocolAll,
  1631. false);
  1632. switch(conn_config->version) {
  1633. case CURL_SSLVERSION_DEFAULT:
  1634. case CURL_SSLVERSION_TLSv1:
  1635. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1636. kTLSProtocol1,
  1637. true);
  1638. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1639. kTLSProtocol11,
  1640. true);
  1641. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1642. kTLSProtocol12,
  1643. true);
  1644. break;
  1645. case CURL_SSLVERSION_TLSv1_0:
  1646. case CURL_SSLVERSION_TLSv1_1:
  1647. case CURL_SSLVERSION_TLSv1_2:
  1648. case CURL_SSLVERSION_TLSv1_3:
  1649. {
  1650. CURLcode result = set_ssl_version_min_max(cf, data);
  1651. if(result != CURLE_OK)
  1652. return result;
  1653. break;
  1654. }
  1655. case CURL_SSLVERSION_SSLv3:
  1656. case CURL_SSLVERSION_SSLv2:
  1657. failf(data, "SSL versions not supported");
  1658. return CURLE_NOT_BUILT_IN;
  1659. default:
  1660. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  1661. return CURLE_SSL_CONNECT_ERROR;
  1662. }
  1663. #endif /* CURL_SUPPORT_MAC_10_8 */
  1664. }
  1665. #else
  1666. if(conn_config->version_max != CURL_SSLVERSION_MAX_NONE) {
  1667. failf(data, "Your version of the OS does not support to set maximum"
  1668. " SSL/TLS version");
  1669. return CURLE_SSL_CONNECT_ERROR;
  1670. }
  1671. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx, kSSLProtocolAll, false);
  1672. switch(conn_config->version) {
  1673. case CURL_SSLVERSION_DEFAULT:
  1674. case CURL_SSLVERSION_TLSv1:
  1675. case CURL_SSLVERSION_TLSv1_0:
  1676. (void)SSLSetProtocolVersionEnabled(backend->ssl_ctx,
  1677. kTLSProtocol1,
  1678. true);
  1679. break;
  1680. case CURL_SSLVERSION_TLSv1_1:
  1681. failf(data, "Your version of the OS does not support TLSv1.1");
  1682. return CURLE_SSL_CONNECT_ERROR;
  1683. case CURL_SSLVERSION_TLSv1_2:
  1684. failf(data, "Your version of the OS does not support TLSv1.2");
  1685. return CURLE_SSL_CONNECT_ERROR;
  1686. case CURL_SSLVERSION_TLSv1_3:
  1687. failf(data, "Your version of the OS does not support TLSv1.3");
  1688. return CURLE_SSL_CONNECT_ERROR;
  1689. case CURL_SSLVERSION_SSLv2:
  1690. case CURL_SSLVERSION_SSLv3:
  1691. failf(data, "SSL versions not supported");
  1692. return CURLE_NOT_BUILT_IN;
  1693. default:
  1694. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  1695. return CURLE_SSL_CONNECT_ERROR;
  1696. }
  1697. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  1698. #if (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && HAVE_BUILTIN_AVAILABLE == 1
  1699. if(connssl->alpn) {
  1700. if(__builtin_available(macOS 10.13.4, iOS 11, tvOS 11, *)) {
  1701. struct alpn_proto_buf proto;
  1702. size_t i;
  1703. CFStringRef cstr;
  1704. CFMutableArrayRef alpnArr = CFArrayCreateMutable(NULL, 0,
  1705. &kCFTypeArrayCallBacks);
  1706. for(i = 0; i < connssl->alpn->count; ++i) {
  1707. cstr = CFStringCreateWithCString(NULL, connssl->alpn->entries[i],
  1708. kCFStringEncodingUTF8);
  1709. if(!cstr)
  1710. return CURLE_OUT_OF_MEMORY;
  1711. CFArrayAppendValue(alpnArr, cstr);
  1712. CFRelease(cstr);
  1713. }
  1714. err = SSLSetALPNProtocols(backend->ssl_ctx, alpnArr);
  1715. if(err != noErr)
  1716. infof(data, "WARNING: failed to set ALPN protocols; OSStatus %d",
  1717. err);
  1718. CFRelease(alpnArr);
  1719. Curl_alpn_to_proto_str(&proto, connssl->alpn);
  1720. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  1721. }
  1722. }
  1723. #endif
  1724. if(ssl_config->key) {
  1725. infof(data, "WARNING: SSL: CURLOPT_SSLKEY is ignored by Secure "
  1726. "Transport. The private key must be in the Keychain.");
  1727. }
  1728. if(ssl_cert || ssl_cert_blob) {
  1729. bool is_cert_data = ssl_cert_blob != NULL;
  1730. bool is_cert_file = (!is_cert_data) && is_file(ssl_cert);
  1731. SecIdentityRef cert_and_key = NULL;
  1732. /* User wants to authenticate with a client cert. Look for it. Assume that
  1733. the user wants to use an identity loaded from the Keychain. If not, try
  1734. it as a file on disk */
  1735. if(!is_cert_data)
  1736. err = CopyIdentityWithLabel(ssl_cert, &cert_and_key);
  1737. else
  1738. err = !noErr;
  1739. if((err != noErr) && (is_cert_file || is_cert_data)) {
  1740. if(!ssl_config->cert_type)
  1741. infof(data, "SSL: Certificate type not set, assuming "
  1742. "PKCS#12 format.");
  1743. else if(!strcasecompare(ssl_config->cert_type, "P12")) {
  1744. failf(data, "SSL: The Security framework only supports "
  1745. "loading identities that are in PKCS#12 format.");
  1746. return CURLE_SSL_CERTPROBLEM;
  1747. }
  1748. err = CopyIdentityFromPKCS12File(ssl_cert, ssl_cert_blob,
  1749. ssl_config->key_passwd,
  1750. &cert_and_key);
  1751. }
  1752. if(err == noErr && cert_and_key) {
  1753. SecCertificateRef cert = NULL;
  1754. CFTypeRef certs_c[1];
  1755. CFArrayRef certs;
  1756. /* If we found one, print it out: */
  1757. err = SecIdentityCopyCertificate(cert_and_key, &cert);
  1758. if(err == noErr) {
  1759. char *certp;
  1760. CURLcode result = CopyCertSubject(data, cert, &certp);
  1761. if(!result) {
  1762. infof(data, "Client certificate: %s", certp);
  1763. free(certp);
  1764. }
  1765. CFRelease(cert);
  1766. if(result == CURLE_PEER_FAILED_VERIFICATION)
  1767. return CURLE_SSL_CERTPROBLEM;
  1768. if(result)
  1769. return result;
  1770. }
  1771. certs_c[0] = cert_and_key;
  1772. certs = CFArrayCreate(NULL, (const void **)certs_c, 1L,
  1773. &kCFTypeArrayCallBacks);
  1774. err = SSLSetCertificate(backend->ssl_ctx, certs);
  1775. if(certs)
  1776. CFRelease(certs);
  1777. if(err != noErr) {
  1778. failf(data, "SSL: SSLSetCertificate() failed: OSStatus %d", err);
  1779. return CURLE_SSL_CERTPROBLEM;
  1780. }
  1781. CFRelease(cert_and_key);
  1782. }
  1783. else {
  1784. const char *cert_showfilename_error =
  1785. is_cert_data ? "(memory blob)" : ssl_cert;
  1786. switch(err) {
  1787. case errSecAuthFailed: case -25264: /* errSecPkcs12VerifyFailure */
  1788. failf(data, "SSL: Incorrect password for the certificate \"%s\" "
  1789. "and its private key.", cert_showfilename_error);
  1790. break;
  1791. case -26275: /* errSecDecode */ case -25257: /* errSecUnknownFormat */
  1792. failf(data, "SSL: Couldn't make sense of the data in the "
  1793. "certificate \"%s\" and its private key.",
  1794. cert_showfilename_error);
  1795. break;
  1796. case -25260: /* errSecPassphraseRequired */
  1797. failf(data, "SSL The certificate \"%s\" requires a password.",
  1798. cert_showfilename_error);
  1799. break;
  1800. case errSecItemNotFound:
  1801. failf(data, "SSL: Can't find the certificate \"%s\" and its private "
  1802. "key in the Keychain.", cert_showfilename_error);
  1803. break;
  1804. default:
  1805. failf(data, "SSL: Can't load the certificate \"%s\" and its private "
  1806. "key: OSStatus %d", cert_showfilename_error, err);
  1807. break;
  1808. }
  1809. return CURLE_SSL_CERTPROBLEM;
  1810. }
  1811. }
  1812. /* SSL always tries to verify the peer, this only says whether it should
  1813. * fail to connect if the verification fails, or if it should continue
  1814. * anyway. In the latter case the result of the verification is checked with
  1815. * SSL_get_verify_result() below. */
  1816. #if CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS
  1817. /* Snow Leopard introduced the SSLSetSessionOption() function, but due to
  1818. a library bug with the way the kSSLSessionOptionBreakOnServerAuth flag
  1819. works, it doesn't work as expected under Snow Leopard, Lion or
  1820. Mountain Lion.
  1821. So we need to call SSLSetEnableCertVerify() on those older cats in order
  1822. to disable certificate validation if the user turned that off.
  1823. (SecureTransport will always validate the certificate chain by
  1824. default.)
  1825. Note:
  1826. Darwin 11.x.x is Lion (10.7)
  1827. Darwin 12.x.x is Mountain Lion (10.8)
  1828. Darwin 13.x.x is Mavericks (10.9)
  1829. Darwin 14.x.x is Yosemite (10.10)
  1830. Darwin 15.x.x is El Capitan (10.11)
  1831. */
  1832. #if CURL_BUILD_MAC
  1833. if(SSLSetSessionOption && darwinver_maj >= 13) {
  1834. #else
  1835. if(SSLSetSessionOption) {
  1836. #endif /* CURL_BUILD_MAC */
  1837. bool break_on_auth = !conn_config->verifypeer ||
  1838. ssl_cafile || ssl_cablob;
  1839. err = SSLSetSessionOption(backend->ssl_ctx,
  1840. kSSLSessionOptionBreakOnServerAuth,
  1841. break_on_auth);
  1842. if(err != noErr) {
  1843. failf(data, "SSL: SSLSetSessionOption() failed: OSStatus %d", err);
  1844. return CURLE_SSL_CONNECT_ERROR;
  1845. }
  1846. }
  1847. else {
  1848. #if CURL_SUPPORT_MAC_10_8
  1849. err = SSLSetEnableCertVerify(backend->ssl_ctx,
  1850. conn_config->verifypeer?true:false);
  1851. if(err != noErr) {
  1852. failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err);
  1853. return CURLE_SSL_CONNECT_ERROR;
  1854. }
  1855. #endif /* CURL_SUPPORT_MAC_10_8 */
  1856. }
  1857. #else
  1858. err = SSLSetEnableCertVerify(backend->ssl_ctx,
  1859. conn_config->verifypeer?true:false);
  1860. if(err != noErr) {
  1861. failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err);
  1862. return CURLE_SSL_CONNECT_ERROR;
  1863. }
  1864. #endif /* CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS */
  1865. if((ssl_cafile || ssl_cablob) && verifypeer) {
  1866. bool is_cert_data = ssl_cablob != NULL;
  1867. bool is_cert_file = (!is_cert_data) && is_file(ssl_cafile);
  1868. if(!(is_cert_file || is_cert_data)) {
  1869. failf(data, "SSL: can't load CA certificate file %s",
  1870. ssl_cafile ? ssl_cafile : "(blob memory)");
  1871. return CURLE_SSL_CACERT_BADFILE;
  1872. }
  1873. }
  1874. /* Configure hostname check. SNI is used if available.
  1875. * Both hostname check and SNI require SSLSetPeerDomainName().
  1876. * Also: the verifyhost setting influences SNI usage */
  1877. if(conn_config->verifyhost) {
  1878. char *server = connssl->peer.sni?
  1879. connssl->peer.sni : connssl->peer.hostname;
  1880. err = SSLSetPeerDomainName(backend->ssl_ctx, server, strlen(server));
  1881. if(err != noErr) {
  1882. failf(data, "SSL: SSLSetPeerDomainName() failed: OSStatus %d",
  1883. err);
  1884. return CURLE_SSL_CONNECT_ERROR;
  1885. }
  1886. if(connssl->peer.type != CURL_SSL_PEER_DNS) {
  1887. infof(data, "WARNING: using IP address, SNI is being disabled by "
  1888. "the OS.");
  1889. }
  1890. }
  1891. else {
  1892. infof(data, "WARNING: disabling hostname validation also disables SNI.");
  1893. }
  1894. ciphers = conn_config->cipher_list;
  1895. if(ciphers) {
  1896. err = sectransp_set_selected_ciphers(data, backend->ssl_ctx, ciphers);
  1897. }
  1898. else {
  1899. err = sectransp_set_default_ciphers(data, backend->ssl_ctx);
  1900. }
  1901. if(err != noErr) {
  1902. failf(data, "SSL: Unable to set ciphers for SSL/TLS handshake. "
  1903. "Error code: %d", err);
  1904. return CURLE_SSL_CIPHER;
  1905. }
  1906. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  1907. /* We want to enable 1/n-1 when using a CBC cipher unless the user
  1908. specifically doesn't want us doing that: */
  1909. if(SSLSetSessionOption) {
  1910. SSLSetSessionOption(backend->ssl_ctx, kSSLSessionOptionSendOneByteRecord,
  1911. !ssl_config->enable_beast);
  1912. SSLSetSessionOption(backend->ssl_ctx, kSSLSessionOptionFalseStart,
  1913. ssl_config->falsestart); /* false start support */
  1914. }
  1915. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  1916. /* Check if there's a cached ID we can/should use here! */
  1917. if(ssl_config->primary.sessionid) {
  1918. char *ssl_sessionid;
  1919. size_t ssl_sessionid_len;
  1920. Curl_ssl_sessionid_lock(data);
  1921. if(!Curl_ssl_getsessionid(cf, data, &connssl->peer,
  1922. (void **)&ssl_sessionid, &ssl_sessionid_len)) {
  1923. /* we got a session id, use it! */
  1924. err = SSLSetPeerID(backend->ssl_ctx, ssl_sessionid, ssl_sessionid_len);
  1925. Curl_ssl_sessionid_unlock(data);
  1926. if(err != noErr) {
  1927. failf(data, "SSL: SSLSetPeerID() failed: OSStatus %d", err);
  1928. return CURLE_SSL_CONNECT_ERROR;
  1929. }
  1930. /* Informational message */
  1931. infof(data, "SSL reusing session ID");
  1932. }
  1933. /* If there isn't one, then let's make one up! This has to be done prior
  1934. to starting the handshake. */
  1935. else {
  1936. CURLcode result;
  1937. ssl_sessionid =
  1938. aprintf("%s:%d:%d:%s:%d",
  1939. ssl_cafile ? ssl_cafile : "(blob memory)",
  1940. verifypeer, conn_config->verifyhost, connssl->peer.hostname,
  1941. connssl->peer.port);
  1942. ssl_sessionid_len = strlen(ssl_sessionid);
  1943. err = SSLSetPeerID(backend->ssl_ctx, ssl_sessionid, ssl_sessionid_len);
  1944. if(err != noErr) {
  1945. Curl_ssl_sessionid_unlock(data);
  1946. failf(data, "SSL: SSLSetPeerID() failed: OSStatus %d", err);
  1947. return CURLE_SSL_CONNECT_ERROR;
  1948. }
  1949. result = Curl_ssl_addsessionid(cf, data, &connssl->peer, ssl_sessionid,
  1950. ssl_sessionid_len,
  1951. sectransp_session_free);
  1952. Curl_ssl_sessionid_unlock(data);
  1953. if(result)
  1954. return result;
  1955. }
  1956. }
  1957. err = SSLSetIOFuncs(backend->ssl_ctx,
  1958. sectransp_bio_cf_in_read,
  1959. sectransp_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. CURL_TRC_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. CURL_TRC_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. CURL_TRC_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. CURL_TRC_CF(data, cf, "trust result: Unspecified");
  2173. result = CURLE_OK;
  2174. goto out;
  2175. case kSecTrustResultProceed:
  2176. CURL_TRC_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. CURLcode result;
  2202. unsigned char *certbuf;
  2203. size_t buflen;
  2204. bool free_certbuf = FALSE;
  2205. if(ca_info_blob) {
  2206. CURL_TRC_CF(data, cf, "verify_peer, CA from config blob");
  2207. certbuf = ca_info_blob->data;
  2208. buflen = ca_info_blob->len;
  2209. }
  2210. else if(cafile) {
  2211. CURL_TRC_CF(data, cf, "verify_peer, CA from file '%s'", cafile);
  2212. if(read_cert(cafile, &certbuf, &buflen) < 0) {
  2213. failf(data, "SSL: failed to read or invalid CA certificate");
  2214. return CURLE_SSL_CACERT_BADFILE;
  2215. }
  2216. free_certbuf = TRUE;
  2217. }
  2218. else
  2219. return CURLE_SSL_CACERT_BADFILE;
  2220. result = verify_cert_buf(cf, data, certbuf, buflen, ctx);
  2221. if(free_certbuf)
  2222. free(certbuf);
  2223. return result;
  2224. }
  2225. #ifdef SECTRANSP_PINNEDPUBKEY
  2226. static CURLcode pkp_pin_peer_pubkey(struct Curl_easy *data,
  2227. SSLContextRef ctx,
  2228. const char *pinnedpubkey)
  2229. { /* Scratch */
  2230. size_t pubkeylen, realpubkeylen, spkiHeaderLength = 24;
  2231. unsigned char *pubkey = NULL, *realpubkey = NULL;
  2232. const unsigned char *spkiHeader = NULL;
  2233. CFDataRef publicKeyBits = NULL;
  2234. /* Result is returned to caller */
  2235. CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  2236. /* if a path wasn't specified, don't pin */
  2237. if(!pinnedpubkey)
  2238. return CURLE_OK;
  2239. if(!ctx)
  2240. return result;
  2241. do {
  2242. SecTrustRef trust;
  2243. OSStatus ret;
  2244. SecKeyRef keyRef;
  2245. ret = SSLCopyPeerTrust(ctx, &trust);
  2246. if(ret != noErr || !trust)
  2247. break;
  2248. keyRef = SecTrustCopyPublicKey(trust);
  2249. CFRelease(trust);
  2250. if(!keyRef)
  2251. break;
  2252. #ifdef SECTRANSP_PINNEDPUBKEY_V1
  2253. publicKeyBits = SecKeyCopyExternalRepresentation(keyRef, NULL);
  2254. CFRelease(keyRef);
  2255. if(!publicKeyBits)
  2256. break;
  2257. #elif SECTRANSP_PINNEDPUBKEY_V2
  2258. {
  2259. OSStatus success;
  2260. success = SecItemExport(keyRef, kSecFormatOpenSSL, 0, NULL,
  2261. &publicKeyBits);
  2262. CFRelease(keyRef);
  2263. if(success != errSecSuccess || !publicKeyBits)
  2264. break;
  2265. }
  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: %zu", 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. CURL_TRC_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->peer.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->peer.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. CURL_TRC_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. CURL_TRC_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. CURL_TRC_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. CURL_TRC_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 size_t sectransp_version(char *buffer, size_t size)
  2957. {
  2958. return msnprintf(buffer, size, "SecureTransport");
  2959. }
  2960. static bool sectransp_data_pending(struct Curl_cfilter *cf,
  2961. const struct Curl_easy *data)
  2962. {
  2963. const struct ssl_connect_data *connssl = cf->ctx;
  2964. struct st_ssl_backend_data *backend =
  2965. (struct st_ssl_backend_data *)connssl->backend;
  2966. OSStatus err;
  2967. size_t buffer;
  2968. (void)data;
  2969. DEBUGASSERT(backend);
  2970. if(backend->ssl_ctx) { /* SSL is in use */
  2971. CURL_TRC_CF((struct Curl_easy *)data, cf, "data_pending");
  2972. err = SSLGetBufferedReadSize(backend->ssl_ctx, &buffer);
  2973. if(err == noErr)
  2974. return buffer > 0UL;
  2975. return false;
  2976. }
  2977. else
  2978. return false;
  2979. }
  2980. static CURLcode sectransp_random(struct Curl_easy *data UNUSED_PARAM,
  2981. unsigned char *entropy, size_t length)
  2982. {
  2983. /* arc4random_buf() isn't available on cats older than Lion, so let's
  2984. do this manually for the benefit of the older cats. */
  2985. size_t i;
  2986. u_int32_t random_number = 0;
  2987. (void)data;
  2988. for(i = 0 ; i < length ; i++) {
  2989. if(i % sizeof(u_int32_t) == 0)
  2990. random_number = arc4random();
  2991. entropy[i] = random_number & 0xFF;
  2992. random_number >>= 8;
  2993. }
  2994. i = random_number = 0;
  2995. return CURLE_OK;
  2996. }
  2997. static CURLcode sectransp_sha256sum(const unsigned char *tmp, /* input */
  2998. size_t tmplen,
  2999. unsigned char *sha256sum, /* output */
  3000. size_t sha256len)
  3001. {
  3002. (void)sha256len;
  3003. assert(sha256len >= CURL_SHA256_DIGEST_LENGTH);
  3004. (void)CC_SHA256(tmp, (CC_LONG)tmplen, sha256sum);
  3005. return CURLE_OK;
  3006. }
  3007. static bool sectransp_false_start(void)
  3008. {
  3009. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  3010. if(SSLSetSessionOption)
  3011. return TRUE;
  3012. #endif
  3013. return FALSE;
  3014. }
  3015. static ssize_t sectransp_send(struct Curl_cfilter *cf,
  3016. struct Curl_easy *data,
  3017. const void *mem,
  3018. size_t len,
  3019. CURLcode *curlcode)
  3020. {
  3021. struct ssl_connect_data *connssl = cf->ctx;
  3022. struct st_ssl_backend_data *backend =
  3023. (struct st_ssl_backend_data *)connssl->backend;
  3024. size_t processed = 0UL;
  3025. OSStatus err;
  3026. DEBUGASSERT(backend);
  3027. /* The SSLWrite() function works a little differently than expected. The
  3028. fourth argument (processed) is currently documented in Apple's
  3029. documentation as: "On return, the length, in bytes, of the data actually
  3030. written."
  3031. Now, one could interpret that as "written to the socket," but actually,
  3032. it returns the amount of data that was written to a buffer internal to
  3033. the SSLContextRef instead. So it's possible for SSLWrite() to return
  3034. errSSLWouldBlock and a number of bytes "written" because those bytes were
  3035. encrypted and written to a buffer, not to the socket.
  3036. So if this happens, then we need to keep calling SSLWrite() over and
  3037. over again with no new data until it quits returning errSSLWouldBlock. */
  3038. /* Do we have buffered data to write from the last time we were called? */
  3039. if(backend->ssl_write_buffered_length) {
  3040. /* Write the buffered data: */
  3041. err = SSLWrite(backend->ssl_ctx, NULL, 0UL, &processed);
  3042. switch(err) {
  3043. case noErr:
  3044. /* processed is always going to be 0 because we didn't write to
  3045. the buffer, so return how much was written to the socket */
  3046. processed = backend->ssl_write_buffered_length;
  3047. backend->ssl_write_buffered_length = 0UL;
  3048. break;
  3049. case errSSLWouldBlock: /* argh, try again */
  3050. *curlcode = CURLE_AGAIN;
  3051. return -1L;
  3052. default:
  3053. failf(data, "SSLWrite() returned error %d", err);
  3054. *curlcode = CURLE_SEND_ERROR;
  3055. return -1L;
  3056. }
  3057. }
  3058. else {
  3059. /* We've got new data to write: */
  3060. err = SSLWrite(backend->ssl_ctx, mem, len, &processed);
  3061. if(err != noErr) {
  3062. switch(err) {
  3063. case errSSLWouldBlock:
  3064. /* Data was buffered but not sent, we have to tell the caller
  3065. to try sending again, and remember how much was buffered */
  3066. backend->ssl_write_buffered_length = len;
  3067. *curlcode = CURLE_AGAIN;
  3068. return -1L;
  3069. default:
  3070. failf(data, "SSLWrite() returned error %d", err);
  3071. *curlcode = CURLE_SEND_ERROR;
  3072. return -1L;
  3073. }
  3074. }
  3075. }
  3076. return (ssize_t)processed;
  3077. }
  3078. static ssize_t sectransp_recv(struct Curl_cfilter *cf,
  3079. struct Curl_easy *data,
  3080. char *buf,
  3081. size_t buffersize,
  3082. CURLcode *curlcode)
  3083. {
  3084. struct ssl_connect_data *connssl = cf->ctx;
  3085. struct st_ssl_backend_data *backend =
  3086. (struct st_ssl_backend_data *)connssl->backend;
  3087. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  3088. size_t processed = 0UL;
  3089. OSStatus err;
  3090. DEBUGASSERT(backend);
  3091. again:
  3092. *curlcode = CURLE_OK;
  3093. err = SSLRead(backend->ssl_ctx, buf, buffersize, &processed);
  3094. if(err != noErr) {
  3095. switch(err) {
  3096. case errSSLWouldBlock: /* return how much we read (if anything) */
  3097. if(processed) {
  3098. return (ssize_t)processed;
  3099. }
  3100. *curlcode = CURLE_AGAIN;
  3101. return -1L;
  3102. /* errSSLClosedGraceful - server gracefully shut down the SSL session
  3103. errSSLClosedNoNotify - server hung up on us instead of sending a
  3104. closure alert notice, read() is returning 0
  3105. Either way, inform the caller that the server disconnected. */
  3106. case errSSLClosedGraceful:
  3107. case errSSLClosedNoNotify:
  3108. *curlcode = CURLE_OK;
  3109. return 0;
  3110. /* The below is errSSLPeerAuthCompleted; it's not defined in
  3111. Leopard's headers */
  3112. case -9841:
  3113. if((conn_config->CAfile || conn_config->ca_info_blob) &&
  3114. conn_config->verifypeer) {
  3115. CURLcode result = verify_cert(cf, data, conn_config->CAfile,
  3116. conn_config->ca_info_blob,
  3117. backend->ssl_ctx);
  3118. if(result) {
  3119. *curlcode = result;
  3120. return -1;
  3121. }
  3122. }
  3123. goto again;
  3124. default:
  3125. failf(data, "SSLRead() return error %d", err);
  3126. *curlcode = CURLE_RECV_ERROR;
  3127. return -1L;
  3128. }
  3129. }
  3130. return (ssize_t)processed;
  3131. }
  3132. static void *sectransp_get_internals(struct ssl_connect_data *connssl,
  3133. CURLINFO info UNUSED_PARAM)
  3134. {
  3135. struct st_ssl_backend_data *backend =
  3136. (struct st_ssl_backend_data *)connssl->backend;
  3137. (void)info;
  3138. DEBUGASSERT(backend);
  3139. return backend->ssl_ctx;
  3140. }
  3141. const struct Curl_ssl Curl_ssl_sectransp = {
  3142. { CURLSSLBACKEND_SECURETRANSPORT, "secure-transport" }, /* info */
  3143. SSLSUPP_CAINFO_BLOB |
  3144. SSLSUPP_CERTINFO |
  3145. #ifdef SECTRANSP_PINNEDPUBKEY
  3146. SSLSUPP_PINNEDPUBKEY |
  3147. #endif /* SECTRANSP_PINNEDPUBKEY */
  3148. SSLSUPP_HTTPS_PROXY,
  3149. sizeof(struct st_ssl_backend_data),
  3150. Curl_none_init, /* init */
  3151. Curl_none_cleanup, /* cleanup */
  3152. sectransp_version, /* version */
  3153. Curl_none_check_cxn, /* check_cxn */
  3154. sectransp_shutdown, /* shutdown */
  3155. sectransp_data_pending, /* data_pending */
  3156. sectransp_random, /* random */
  3157. Curl_none_cert_status_request, /* cert_status_request */
  3158. sectransp_connect, /* connect */
  3159. sectransp_connect_nonblocking, /* connect_nonblocking */
  3160. Curl_ssl_adjust_pollset, /* adjust_pollset */
  3161. sectransp_get_internals, /* get_internals */
  3162. sectransp_close, /* close_one */
  3163. Curl_none_close_all, /* close_all */
  3164. Curl_none_set_engine, /* set_engine */
  3165. Curl_none_set_engine_default, /* set_engine_default */
  3166. Curl_none_engines_list, /* engines_list */
  3167. sectransp_false_start, /* false_start */
  3168. sectransp_sha256sum, /* sha256sum */
  3169. NULL, /* associate_connection */
  3170. NULL, /* disassociate_connection */
  3171. NULL, /* free_multi_ssl_backend_data */
  3172. sectransp_recv, /* recv decrypted data */
  3173. sectransp_send, /* send data to encrypt */
  3174. };
  3175. #ifdef __GNUC__
  3176. #pragma GCC diagnostic pop
  3177. #endif
  3178. #ifdef __clang__
  3179. #pragma clang diagnostic pop
  3180. #endif
  3181. #endif /* USE_SECTRANSP */