sectransp.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790
  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 Secure Transport-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. #ifdef USE_SECTRANSP
  31. #include "urldata.h" /* for the Curl_easy definition */
  32. #include "curl_base64.h"
  33. #include "strtok.h"
  34. #include "multiif.h"
  35. #include "strcase.h"
  36. #include "x509asn1.h"
  37. #include "strerror.h"
  38. #include "cipher_suite.h"
  39. #ifdef __clang__
  40. #pragma clang diagnostic push
  41. #pragma clang diagnostic ignored "-Wunreachable-code"
  42. #endif /* __clang__ */
  43. #ifdef __GNUC__
  44. #pragma GCC diagnostic push
  45. #pragma GCC diagnostic ignored "-Waddress"
  46. #endif
  47. #if defined(__GNUC__) && defined(__APPLE__)
  48. #pragma GCC diagnostic push
  49. #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
  50. #endif
  51. #include <limits.h>
  52. #include <Security/Security.h>
  53. /* For some reason, when building for iOS, the omnibus header above does
  54. * not include SecureTransport.h as of iOS SDK 5.1. */
  55. #include <Security/SecureTransport.h>
  56. #include <CoreFoundation/CoreFoundation.h>
  57. #include <CommonCrypto/CommonDigest.h>
  58. /* The Security framework has changed greatly between iOS and different macOS
  59. versions, and we will try to support as many of them as we can (back to
  60. Leopard and iOS 5) by using macros and weak-linking.
  61. In general, you want to build this using the most recent OS SDK, since some
  62. features require curl to be built against the latest SDK. TLS 1.1 and 1.2
  63. support, for instance, require the macOS 10.8 SDK or later. TLS 1.3
  64. requires the macOS 10.13 or iOS 11 SDK or later. */
  65. #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
  66. #if MAC_OS_X_VERSION_MAX_ALLOWED < 1050
  67. #error "The Secure Transport backend requires Leopard or later."
  68. #endif /* MAC_OS_X_VERSION_MAX_ALLOWED < 1050 */
  69. #define CURL_BUILD_IOS 0
  70. #define CURL_BUILD_IOS_7 0
  71. #define CURL_BUILD_IOS_9 0
  72. #define CURL_BUILD_IOS_11 0
  73. #define CURL_BUILD_IOS_13 0
  74. #define CURL_BUILD_MAC 1
  75. /* This is the maximum API level we are allowed to use when building: */
  76. #define CURL_BUILD_MAC_10_5 MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
  77. #define CURL_BUILD_MAC_10_6 MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
  78. #define CURL_BUILD_MAC_10_7 MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  79. #define CURL_BUILD_MAC_10_8 MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
  80. #define CURL_BUILD_MAC_10_9 MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
  81. #define CURL_BUILD_MAC_10_11 MAC_OS_X_VERSION_MAX_ALLOWED >= 101100
  82. #define CURL_BUILD_MAC_10_13 MAC_OS_X_VERSION_MAX_ALLOWED >= 101300
  83. #define CURL_BUILD_MAC_10_15 MAC_OS_X_VERSION_MAX_ALLOWED >= 101500
  84. /* These macros mean "the following code is present to allow runtime backward
  85. compatibility with at least this cat or earlier":
  86. (You set this at build-time using the compiler command line option
  87. "-mmacosx-version-min.") */
  88. #define CURL_SUPPORT_MAC_10_5 MAC_OS_X_VERSION_MIN_REQUIRED <= 1050
  89. #define CURL_SUPPORT_MAC_10_6 MAC_OS_X_VERSION_MIN_REQUIRED <= 1060
  90. #define CURL_SUPPORT_MAC_10_7 MAC_OS_X_VERSION_MIN_REQUIRED <= 1070
  91. #define CURL_SUPPORT_MAC_10_8 MAC_OS_X_VERSION_MIN_REQUIRED <= 1080
  92. #define CURL_SUPPORT_MAC_10_9 MAC_OS_X_VERSION_MIN_REQUIRED <= 1090
  93. #elif TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
  94. #define CURL_BUILD_IOS 1
  95. #define CURL_BUILD_IOS_7 __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
  96. #define CURL_BUILD_IOS_9 __IPHONE_OS_VERSION_MAX_ALLOWED >= 90000
  97. #define CURL_BUILD_IOS_11 __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  98. #define CURL_BUILD_IOS_13 __IPHONE_OS_VERSION_MAX_ALLOWED >= 130000
  99. #define CURL_BUILD_MAC 0
  100. #define CURL_BUILD_MAC_10_5 0
  101. #define CURL_BUILD_MAC_10_6 0
  102. #define CURL_BUILD_MAC_10_7 0
  103. #define CURL_BUILD_MAC_10_8 0
  104. #define CURL_BUILD_MAC_10_9 0
  105. #define CURL_BUILD_MAC_10_11 0
  106. #define CURL_BUILD_MAC_10_13 0
  107. #define CURL_BUILD_MAC_10_15 0
  108. #define CURL_SUPPORT_MAC_10_5 0
  109. #define CURL_SUPPORT_MAC_10_6 0
  110. #define CURL_SUPPORT_MAC_10_7 0
  111. #define CURL_SUPPORT_MAC_10_8 0
  112. #define CURL_SUPPORT_MAC_10_9 0
  113. #else
  114. #error "The Secure Transport backend requires iOS or macOS."
  115. #endif /* (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) */
  116. #if CURL_BUILD_MAC
  117. #include <sys/sysctl.h>
  118. #endif /* CURL_BUILD_MAC */
  119. #include "sendf.h"
  120. #include "inet_pton.h"
  121. #include "connect.h"
  122. #include "select.h"
  123. #include "vtls.h"
  124. #include "vtls_int.h"
  125. #include "sectransp.h"
  126. #include "curl_printf.h"
  127. #include "strdup.h"
  128. #include "curl_memory.h"
  129. /* The last #include file should be: */
  130. #include "memdebug.h"
  131. /* From MacTypes.h (which we cannot include because it is not present in
  132. iOS: */
  133. #define ioErr -36
  134. #define paramErr -50
  135. struct st_ssl_backend_data {
  136. SSLContextRef ssl_ctx;
  137. bool ssl_direction; /* true if writing, false if reading */
  138. size_t ssl_write_buffered_length;
  139. BIT(sent_shutdown);
  140. };
  141. /* Create the list of default ciphers to use by making an intersection of the
  142. * ciphers supported by Secure Transport and the list below, using the order
  143. * of the former.
  144. * This list is based on TLS recommendations by Mozilla, balancing between
  145. * security and wide compatibility: "Most ciphers that are not clearly broken
  146. * and dangerous to use are supported"
  147. */
  148. static const uint16_t default_ciphers[] = {
  149. TLS_RSA_WITH_3DES_EDE_CBC_SHA, /* 0x000A */
  150. TLS_RSA_WITH_AES_128_CBC_SHA, /* 0x002F */
  151. TLS_RSA_WITH_AES_256_CBC_SHA, /* 0x0035 */
  152. #if CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS
  153. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, /* 0xC009 */
  154. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, /* 0xC00A */
  155. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, /* 0xC013 */
  156. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, /* 0xC014 */
  157. #endif /* CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS */
  158. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  159. TLS_RSA_WITH_AES_128_CBC_SHA256, /* 0x003C */
  160. TLS_RSA_WITH_AES_256_CBC_SHA256, /* 0x003D */
  161. TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, /* 0x0067 */
  162. TLS_DHE_RSA_WITH_AES_256_CBC_SHA256, /* 0x006B */
  163. TLS_RSA_WITH_AES_128_GCM_SHA256, /* 0x009C */
  164. TLS_RSA_WITH_AES_256_GCM_SHA384, /* 0x009D */
  165. TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, /* 0x009E */
  166. TLS_DHE_RSA_WITH_AES_256_GCM_SHA384, /* 0x009F */
  167. TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, /* 0xC023 */
  168. TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384, /* 0xC024 */
  169. TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, /* 0xC027 */
  170. TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384, /* 0xC028 */
  171. TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, /* 0xC02B */
  172. TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, /* 0xC02C */
  173. TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, /* 0xC02F */
  174. TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, /* 0xC030 */
  175. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  176. #if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11
  177. TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA8 */
  178. TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305_SHA256, /* 0xCCA9 */
  179. /* TLSv1.3 is not supported by Secure Transport, but there is also other
  180. * code referencing TLSv1.3, like: kTLSProtocol13 ? */
  181. TLS_AES_128_GCM_SHA256, /* 0x1301 */
  182. TLS_AES_256_GCM_SHA384, /* 0x1302 */
  183. TLS_CHACHA20_POLY1305_SHA256, /* 0x1303 */
  184. #endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */
  185. };
  186. #define DEFAULT_CIPHERS_LEN sizeof(default_ciphers)/sizeof(default_ciphers[0])
  187. /* pinned public key support tests */
  188. /* version 1 supports macOS 10.12+ and iOS 10+ */
  189. #if ((TARGET_OS_IPHONE && __IPHONE_OS_VERSION_MIN_REQUIRED >= 100000) || \
  190. (!TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 101200))
  191. #define SECTRANSP_PINNEDPUBKEY_V1 1
  192. #endif
  193. /* version 2 supports macOS 10.7+ */
  194. #if (!TARGET_OS_IPHONE && __MAC_OS_X_VERSION_MIN_REQUIRED >= 1070)
  195. #define SECTRANSP_PINNEDPUBKEY_V2 1
  196. #endif
  197. #if defined(SECTRANSP_PINNEDPUBKEY_V1) || defined(SECTRANSP_PINNEDPUBKEY_V2)
  198. /* this backend supports CURLOPT_PINNEDPUBLICKEY */
  199. #define SECTRANSP_PINNEDPUBKEY 1
  200. #endif /* SECTRANSP_PINNEDPUBKEY */
  201. #ifdef SECTRANSP_PINNEDPUBKEY
  202. /* both new and old APIs return rsa keys missing the spki header (not DER) */
  203. static const unsigned char rsa4096SpkiHeader[] = {
  204. 0x30, 0x82, 0x02, 0x22, 0x30, 0x0d,
  205. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  206. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
  207. 0x00, 0x03, 0x82, 0x02, 0x0f, 0x00};
  208. static const unsigned char rsa2048SpkiHeader[] = {
  209. 0x30, 0x82, 0x01, 0x22, 0x30, 0x0d,
  210. 0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
  211. 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05,
  212. 0x00, 0x03, 0x82, 0x01, 0x0f, 0x00};
  213. #ifdef SECTRANSP_PINNEDPUBKEY_V1
  214. /* the *new* version does not return DER encoded ecdsa certs like the old... */
  215. static const unsigned char ecDsaSecp256r1SpkiHeader[] = {
  216. 0x30, 0x59, 0x30, 0x13, 0x06, 0x07,
  217. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  218. 0x01, 0x06, 0x08, 0x2a, 0x86, 0x48,
  219. 0xce, 0x3d, 0x03, 0x01, 0x07, 0x03,
  220. 0x42, 0x00};
  221. static const unsigned char ecDsaSecp384r1SpkiHeader[] = {
  222. 0x30, 0x76, 0x30, 0x10, 0x06, 0x07,
  223. 0x2a, 0x86, 0x48, 0xce, 0x3d, 0x02,
  224. 0x01, 0x06, 0x05, 0x2b, 0x81, 0x04,
  225. 0x00, 0x22, 0x03, 0x62, 0x00};
  226. #endif /* SECTRANSP_PINNEDPUBKEY_V1 */
  227. #endif /* SECTRANSP_PINNEDPUBKEY */
  228. static OSStatus sectransp_bio_cf_in_read(SSLConnectionRef connection,
  229. void *buf,
  230. size_t *dataLength) /* IN/OUT */
  231. {
  232. struct Curl_cfilter *cf = (struct Curl_cfilter *)connection;
  233. struct ssl_connect_data *connssl = cf->ctx;
  234. struct st_ssl_backend_data *backend =
  235. (struct st_ssl_backend_data *)connssl->backend;
  236. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  237. ssize_t nread;
  238. CURLcode result;
  239. OSStatus rtn = noErr;
  240. DEBUGASSERT(data);
  241. nread = Curl_conn_cf_recv(cf->next, data, buf, *dataLength, &result);
  242. CURL_TRC_CF(data, cf, "bio_read(len=%zu) -> %zd, result=%d",
  243. *dataLength, nread, result);
  244. if(nread < 0) {
  245. switch(result) {
  246. case CURLE_OK:
  247. case CURLE_AGAIN:
  248. rtn = errSSLWouldBlock;
  249. backend->ssl_direction = FALSE;
  250. break;
  251. default:
  252. rtn = ioErr;
  253. break;
  254. }
  255. nread = 0;
  256. }
  257. else if(nread == 0) {
  258. rtn = errSSLClosedGraceful;
  259. }
  260. else if((size_t)nread < *dataLength) {
  261. rtn = errSSLWouldBlock;
  262. }
  263. *dataLength = nread;
  264. return rtn;
  265. }
  266. static OSStatus sectransp_bio_cf_out_write(SSLConnectionRef connection,
  267. const void *buf,
  268. size_t *dataLength) /* IN/OUT */
  269. {
  270. struct Curl_cfilter *cf = (struct Curl_cfilter *)connection;
  271. struct ssl_connect_data *connssl = cf->ctx;
  272. struct st_ssl_backend_data *backend =
  273. (struct st_ssl_backend_data *)connssl->backend;
  274. struct Curl_easy *data = CF_DATA_CURRENT(cf);
  275. ssize_t nwritten;
  276. CURLcode result;
  277. OSStatus rtn = noErr;
  278. DEBUGASSERT(data);
  279. nwritten = Curl_conn_cf_send(cf->next, data, buf, *dataLength, FALSE,
  280. &result);
  281. CURL_TRC_CF(data, cf, "bio_send(len=%zu) -> %zd, result=%d",
  282. *dataLength, nwritten, result);
  283. if(nwritten <= 0) {
  284. if(result == CURLE_AGAIN) {
  285. rtn = errSSLWouldBlock;
  286. backend->ssl_direction = TRUE;
  287. }
  288. else {
  289. rtn = ioErr;
  290. }
  291. nwritten = 0;
  292. }
  293. else if((size_t)nwritten < *dataLength) {
  294. rtn = errSSLWouldBlock;
  295. }
  296. *dataLength = nwritten;
  297. return rtn;
  298. }
  299. #if CURL_BUILD_MAC
  300. CF_INLINE void GetDarwinVersionNumber(int *major, int *minor)
  301. {
  302. int mib[2];
  303. char *os_version;
  304. size_t os_version_len;
  305. char *os_version_major, *os_version_minor;
  306. char *tok_buf;
  307. /* Get the Darwin kernel version from the kernel using sysctl(): */
  308. mib[0] = CTL_KERN;
  309. mib[1] = KERN_OSRELEASE;
  310. if(sysctl(mib, 2, NULL, &os_version_len, NULL, 0) == -1)
  311. return;
  312. os_version = malloc(os_version_len*sizeof(char));
  313. if(!os_version)
  314. return;
  315. if(sysctl(mib, 2, os_version, &os_version_len, NULL, 0) == -1) {
  316. free(os_version);
  317. return;
  318. }
  319. /* Parse the version: */
  320. os_version_major = Curl_strtok_r(os_version, ".", &tok_buf);
  321. os_version_minor = Curl_strtok_r(NULL, ".", &tok_buf);
  322. *major = atoi(os_version_major);
  323. *minor = atoi(os_version_minor);
  324. free(os_version);
  325. }
  326. #endif /* CURL_BUILD_MAC */
  327. /* Apple provides a myriad of ways of getting information about a certificate
  328. into a string. Some are not available under iOS or newer cats. Here's a
  329. unified function for getting a string describing the certificate that ought
  330. to work in all cats starting with Leopard. */
  331. CF_INLINE CFStringRef getsubject(SecCertificateRef cert)
  332. {
  333. CFStringRef server_cert_summary = CFSTR("(null)");
  334. #if CURL_BUILD_IOS
  335. /* iOS: There is only one way to do this. */
  336. server_cert_summary = SecCertificateCopySubjectSummary(cert);
  337. #else
  338. #if CURL_BUILD_MAC_10_7
  339. /* Lion & later: Get the long description if we can. */
  340. if(&SecCertificateCopyLongDescription)
  341. server_cert_summary =
  342. SecCertificateCopyLongDescription(NULL, cert, NULL);
  343. else
  344. #endif /* CURL_BUILD_MAC_10_7 */
  345. #if CURL_BUILD_MAC_10_6
  346. /* Snow Leopard: Get the certificate summary. */
  347. if(&SecCertificateCopySubjectSummary)
  348. server_cert_summary = SecCertificateCopySubjectSummary(cert);
  349. else
  350. #endif /* CURL_BUILD_MAC_10_6 */
  351. /* Leopard is as far back as we go... */
  352. (void)SecCertificateCopyCommonName(cert, &server_cert_summary);
  353. #endif /* CURL_BUILD_IOS */
  354. return server_cert_summary;
  355. }
  356. static CURLcode CopyCertSubject(struct Curl_easy *data,
  357. SecCertificateRef cert, char **certp)
  358. {
  359. CFStringRef c = getsubject(cert);
  360. CURLcode result = CURLE_OK;
  361. const char *direct;
  362. char *cbuf = NULL;
  363. *certp = NULL;
  364. if(!c) {
  365. failf(data, "SSL: invalid CA certificate subject");
  366. return CURLE_PEER_FAILED_VERIFICATION;
  367. }
  368. /* If the subject is already available as UTF-8 encoded (ie 'direct') then
  369. use that, else convert it. */
  370. direct = CFStringGetCStringPtr(c, kCFStringEncodingUTF8);
  371. if(direct) {
  372. *certp = strdup(direct);
  373. if(!*certp) {
  374. failf(data, "SSL: out of memory");
  375. result = CURLE_OUT_OF_MEMORY;
  376. }
  377. }
  378. else {
  379. size_t cbuf_size = ((size_t)CFStringGetLength(c) * 4) + 1;
  380. cbuf = calloc(1, cbuf_size);
  381. if(cbuf) {
  382. if(!CFStringGetCString(c, cbuf, (CFIndex)cbuf_size,
  383. kCFStringEncodingUTF8)) {
  384. failf(data, "SSL: invalid CA certificate subject");
  385. result = CURLE_PEER_FAILED_VERIFICATION;
  386. }
  387. else
  388. /* pass back the buffer */
  389. *certp = cbuf;
  390. }
  391. else {
  392. failf(data, "SSL: could not allocate %zu bytes of memory", cbuf_size);
  393. result = CURLE_OUT_OF_MEMORY;
  394. }
  395. }
  396. if(result)
  397. free(cbuf);
  398. CFRelease(c);
  399. return result;
  400. }
  401. #if CURL_SUPPORT_MAC_10_6
  402. /* The SecKeychainSearch API was deprecated in Lion, and using it will raise
  403. deprecation warnings, so let's not compile this unless it is necessary: */
  404. static OSStatus CopyIdentityWithLabelOldSchool(char *label,
  405. SecIdentityRef *out_c_a_k)
  406. {
  407. OSStatus status = errSecItemNotFound;
  408. SecKeychainAttributeList attr_list;
  409. SecKeychainAttribute attr;
  410. SecKeychainSearchRef search = NULL;
  411. SecCertificateRef cert = NULL;
  412. /* Set up the attribute list: */
  413. attr_list.count = 1L;
  414. attr_list.attr = &attr;
  415. /* Set up our lone search criterion: */
  416. attr.tag = kSecLabelItemAttr;
  417. attr.data = label;
  418. attr.length = (UInt32)strlen(label);
  419. /* Start searching: */
  420. status = SecKeychainSearchCreateFromAttributes(NULL,
  421. kSecCertificateItemClass,
  422. &attr_list,
  423. &search);
  424. if(status == noErr) {
  425. status = SecKeychainSearchCopyNext(search,
  426. (SecKeychainItemRef *)&cert);
  427. if(status == noErr && cert) {
  428. /* If we found a certificate, does it have a private key? */
  429. status = SecIdentityCreateWithCertificate(NULL, cert, out_c_a_k);
  430. CFRelease(cert);
  431. }
  432. }
  433. if(search)
  434. CFRelease(search);
  435. return status;
  436. }
  437. #endif /* CURL_SUPPORT_MAC_10_6 */
  438. static OSStatus CopyIdentityWithLabel(char *label,
  439. SecIdentityRef *out_cert_and_key)
  440. {
  441. OSStatus status = errSecItemNotFound;
  442. #if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS
  443. CFArrayRef keys_list;
  444. CFIndex keys_list_count;
  445. CFIndex i;
  446. /* SecItemCopyMatching() was introduced in iOS and Snow Leopard.
  447. kSecClassIdentity was introduced in Lion. If both exist, let's use them
  448. to find the certificate. */
  449. if(&SecItemCopyMatching && kSecClassIdentity) {
  450. CFTypeRef keys[5];
  451. CFTypeRef values[5];
  452. CFDictionaryRef query_dict;
  453. CFStringRef label_cf = CFStringCreateWithCString(NULL, label,
  454. kCFStringEncodingUTF8);
  455. /* Set up our search criteria and expected results: */
  456. values[0] = kSecClassIdentity; /* we want a certificate and a key */
  457. keys[0] = kSecClass;
  458. values[1] = kCFBooleanTrue; /* we want a reference */
  459. keys[1] = kSecReturnRef;
  460. values[2] = kSecMatchLimitAll; /* kSecMatchLimitOne would be better if the
  461. * label matching below worked correctly */
  462. keys[2] = kSecMatchLimit;
  463. /* identity searches need a SecPolicyRef in order to work */
  464. values[3] = SecPolicyCreateSSL(FALSE, NULL);
  465. keys[3] = kSecMatchPolicy;
  466. /* match the name of the certificate (does not work in macOS 10.12.1) */
  467. values[4] = label_cf;
  468. keys[4] = kSecAttrLabel;
  469. query_dict = CFDictionaryCreate(NULL, (const void **)keys,
  470. (const void **)values, 5L,
  471. &kCFCopyStringDictionaryKeyCallBacks,
  472. &kCFTypeDictionaryValueCallBacks);
  473. CFRelease(values[3]);
  474. /* Do we have a match? */
  475. status = SecItemCopyMatching(query_dict, (CFTypeRef *) &keys_list);
  476. /* Because kSecAttrLabel matching does not work with kSecClassIdentity,
  477. * we need to find the correct identity ourselves */
  478. if(status == noErr) {
  479. keys_list_count = CFArrayGetCount(keys_list);
  480. *out_cert_and_key = NULL;
  481. status = 1;
  482. for(i = 0; i < keys_list_count; i++) {
  483. OSStatus err = noErr;
  484. SecCertificateRef cert = NULL;
  485. SecIdentityRef identity =
  486. (SecIdentityRef) CFArrayGetValueAtIndex(keys_list, i);
  487. err = SecIdentityCopyCertificate(identity, &cert);
  488. if(err == noErr) {
  489. CFStringRef common_name = NULL;
  490. OSStatus copy_status = noErr;
  491. #if CURL_BUILD_IOS
  492. common_name = SecCertificateCopySubjectSummary(cert);
  493. #elif CURL_BUILD_MAC_10_7
  494. copy_status = SecCertificateCopyCommonName(cert, &common_name);
  495. #endif
  496. if(copy_status == noErr &&
  497. CFStringCompare(common_name, label_cf, 0) == kCFCompareEqualTo) {
  498. CFRelease(cert);
  499. CFRelease(common_name);
  500. CFRetain(identity);
  501. *out_cert_and_key = identity;
  502. status = noErr;
  503. break;
  504. }
  505. if(common_name)
  506. CFRelease(common_name);
  507. }
  508. CFRelease(cert);
  509. }
  510. }
  511. if(keys_list)
  512. CFRelease(keys_list);
  513. CFRelease(query_dict);
  514. CFRelease(label_cf);
  515. }
  516. else {
  517. #if CURL_SUPPORT_MAC_10_6
  518. /* On Leopard and Snow Leopard, fall back to SecKeychainSearch. */
  519. status = CopyIdentityWithLabelOldSchool(label, out_cert_and_key);
  520. #endif /* CURL_SUPPORT_MAC_10_6 */
  521. }
  522. #elif CURL_SUPPORT_MAC_10_6
  523. /* For developers building on older cats, we have no choice but to fall back
  524. to SecKeychainSearch. */
  525. status = CopyIdentityWithLabelOldSchool(label, out_cert_and_key);
  526. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  527. return status;
  528. }
  529. static OSStatus CopyIdentityFromPKCS12File(const char *cPath,
  530. const struct curl_blob *blob,
  531. const char *cPassword,
  532. SecIdentityRef *out_cert_and_key)
  533. {
  534. OSStatus status = errSecItemNotFound;
  535. CFURLRef pkcs_url = NULL;
  536. CFStringRef password = cPassword ? CFStringCreateWithCString(NULL,
  537. cPassword, kCFStringEncodingUTF8) : NULL;
  538. CFDataRef pkcs_data = NULL;
  539. /* We can import P12 files on iOS or macOS 10.7 or later: */
  540. /* These constants are documented as having first appeared in 10.6 but they
  541. raise linker errors when used on that cat for some reason. */
  542. #if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS
  543. bool resource_imported;
  544. if(blob) {
  545. pkcs_data = CFDataCreate(kCFAllocatorDefault,
  546. (const unsigned char *)blob->data,
  547. (CFIndex)blob->len);
  548. status = (pkcs_data != NULL) ? errSecSuccess : errSecAllocate;
  549. resource_imported = (pkcs_data != NULL);
  550. }
  551. else {
  552. pkcs_url =
  553. CFURLCreateFromFileSystemRepresentation(NULL,
  554. (const UInt8 *)cPath,
  555. (CFIndex)strlen(cPath), FALSE);
  556. resource_imported =
  557. CFURLCreateDataAndPropertiesFromResource(NULL,
  558. pkcs_url, &pkcs_data,
  559. NULL, NULL, &status);
  560. }
  561. if(resource_imported) {
  562. CFArrayRef items = NULL;
  563. /* On iOS SecPKCS12Import will never add the client certificate to the
  564. * Keychain.
  565. *
  566. * It gives us back a SecIdentityRef that we can use directly. */
  567. #if CURL_BUILD_IOS
  568. const void *cKeys[] = {kSecImportExportPassphrase};
  569. const void *cValues[] = {password};
  570. CFDictionaryRef options = CFDictionaryCreate(NULL, cKeys, cValues,
  571. password ? 1L : 0L, NULL, NULL);
  572. if(options) {
  573. status = SecPKCS12Import(pkcs_data, options, &items);
  574. CFRelease(options);
  575. }
  576. /* On macOS SecPKCS12Import will always add the client certificate to
  577. * the Keychain.
  578. *
  579. * As this does not match iOS, and apps may not want to see their client
  580. * certificate saved in the user's keychain, we use SecItemImport
  581. * with a NULL keychain to avoid importing it.
  582. *
  583. * This returns a SecCertificateRef from which we can construct a
  584. * SecIdentityRef.
  585. */
  586. #elif CURL_BUILD_MAC_10_7
  587. SecItemImportExportKeyParameters keyParams;
  588. SecExternalFormat inputFormat = kSecFormatPKCS12;
  589. SecExternalItemType inputType = kSecItemTypeCertificate;
  590. memset(&keyParams, 0x00, sizeof(keyParams));
  591. keyParams.version = SEC_KEY_IMPORT_EXPORT_PARAMS_VERSION;
  592. keyParams.passphrase = password;
  593. status = SecItemImport(pkcs_data, NULL, &inputFormat, &inputType,
  594. 0, &keyParams, NULL, &items);
  595. #endif
  596. /* Extract the SecIdentityRef */
  597. if(status == errSecSuccess && items && CFArrayGetCount(items)) {
  598. CFIndex i, count;
  599. count = CFArrayGetCount(items);
  600. for(i = 0; i < count; i++) {
  601. CFTypeRef item = (CFTypeRef) CFArrayGetValueAtIndex(items, i);
  602. CFTypeID itemID = CFGetTypeID(item);
  603. if(itemID == CFDictionaryGetTypeID()) {
  604. CFTypeRef identity = (CFTypeRef) CFDictionaryGetValue(
  605. (CFDictionaryRef) item,
  606. kSecImportItemIdentity);
  607. CFRetain(identity);
  608. *out_cert_and_key = (SecIdentityRef) identity;
  609. break;
  610. }
  611. #if CURL_BUILD_MAC_10_7
  612. else if(itemID == SecCertificateGetTypeID()) {
  613. status = SecIdentityCreateWithCertificate(NULL,
  614. (SecCertificateRef) item,
  615. out_cert_and_key);
  616. break;
  617. }
  618. #endif
  619. }
  620. }
  621. if(items)
  622. CFRelease(items);
  623. CFRelease(pkcs_data);
  624. }
  625. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  626. if(password)
  627. CFRelease(password);
  628. if(pkcs_url)
  629. CFRelease(pkcs_url);
  630. return status;
  631. }
  632. /* This code was borrowed from nss.c, with some modifications:
  633. * Determine whether the nickname passed in is a filename that needs to
  634. * be loaded as a PEM or a nickname.
  635. *
  636. * returns 1 for a file
  637. * returns 0 for not a file
  638. */
  639. CF_INLINE bool is_file(const char *filename)
  640. {
  641. struct_stat st;
  642. if(!filename)
  643. return FALSE;
  644. if(stat(filename, &st) == 0)
  645. return S_ISREG(st.st_mode);
  646. return FALSE;
  647. }
  648. static CURLcode
  649. sectransp_set_ssl_version_min_max(struct Curl_easy *data,
  650. struct st_ssl_backend_data *backend,
  651. struct ssl_primary_config *conn_config)
  652. {
  653. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  654. OSStatus err;
  655. SSLProtocol ver_min;
  656. SSLProtocol ver_max;
  657. #if CURL_SUPPORT_MAC_10_7
  658. if(!&SSLSetProtocolVersionMax)
  659. goto legacy;
  660. #endif
  661. switch(conn_config->version) {
  662. case CURL_SSLVERSION_DEFAULT:
  663. case CURL_SSLVERSION_TLSv1:
  664. case CURL_SSLVERSION_TLSv1_0:
  665. ver_min = kTLSProtocol1;
  666. break;
  667. case CURL_SSLVERSION_TLSv1_1:
  668. ver_min = kTLSProtocol11;
  669. break;
  670. case CURL_SSLVERSION_TLSv1_2:
  671. ver_min = kTLSProtocol12;
  672. break;
  673. case CURL_SSLVERSION_TLSv1_3:
  674. default:
  675. failf(data, "SSL: unsupported minimum TLS version value");
  676. return CURLE_SSL_CONNECT_ERROR;
  677. }
  678. switch(conn_config->version_max) {
  679. case CURL_SSLVERSION_MAX_DEFAULT:
  680. case CURL_SSLVERSION_MAX_NONE:
  681. case CURL_SSLVERSION_MAX_TLSv1_3:
  682. case CURL_SSLVERSION_MAX_TLSv1_2:
  683. ver_max = kTLSProtocol12;
  684. break;
  685. case CURL_SSLVERSION_MAX_TLSv1_1:
  686. ver_max = kTLSProtocol11;
  687. break;
  688. case CURL_SSLVERSION_MAX_TLSv1_0:
  689. ver_max = kTLSProtocol1;
  690. break;
  691. default:
  692. failf(data, "SSL: unsupported maximum TLS version value");
  693. return CURLE_SSL_CONNECT_ERROR;
  694. }
  695. err = SSLSetProtocolVersionMin(backend->ssl_ctx, ver_min);
  696. if(err != noErr) {
  697. failf(data, "SSL: failed to set minimum TLS version");
  698. return CURLE_SSL_CONNECT_ERROR;
  699. }
  700. err = SSLSetProtocolVersionMax(backend->ssl_ctx, ver_max);
  701. if(err != noErr) {
  702. failf(data, "SSL: failed to set maximum TLS version");
  703. return CURLE_SSL_CONNECT_ERROR;
  704. }
  705. return CURLE_OK;
  706. #endif
  707. #if CURL_SUPPORT_MAC_10_7
  708. goto legacy;
  709. legacy:
  710. switch(conn_config->version) {
  711. case CURL_SSLVERSION_DEFAULT:
  712. case CURL_SSLVERSION_TLSv1:
  713. case CURL_SSLVERSION_TLSv1_0:
  714. break;
  715. default:
  716. failf(data, "SSL: unsupported minimum TLS version value");
  717. return CURLE_SSL_CONNECT_ERROR;
  718. }
  719. /* only TLS 1.0 is supported, disable SSL 3.0 and SSL 2.0 */
  720. SSLSetProtocolVersionEnabled(backend->ssl_ctx, kSSLProtocolAll, FALSE);
  721. SSLSetProtocolVersionEnabled(backend->ssl_ctx, kTLSProtocol1, TRUE);
  722. return CURLE_OK;
  723. #endif
  724. }
  725. static int sectransp_cipher_suite_get_str(uint16_t id, char *buf,
  726. size_t buf_size, bool prefer_rfc)
  727. {
  728. /* are these fortezza suites even supported ? */
  729. if(id == SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA)
  730. msnprintf(buf, buf_size, "%s", "SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA");
  731. else if(id == SSL_FORTEZZA_DMS_WITH_NULL_SHA)
  732. msnprintf(buf, buf_size, "%s", "SSL_FORTEZZA_DMS_WITH_NULL_SHA");
  733. /* can TLS_EMPTY_RENEGOTIATION_INFO_SCSV even be set ? */
  734. else if(id == TLS_EMPTY_RENEGOTIATION_INFO_SCSV)
  735. msnprintf(buf, buf_size, "%s", "TLS_EMPTY_RENEGOTIATION_INFO_SCSV");
  736. /* do we still need to support these SSL2-only ciphers ? */
  737. else if(id == SSL_RSA_WITH_RC2_CBC_MD5)
  738. msnprintf(buf, buf_size, "%s", "SSL_RSA_WITH_RC2_CBC_MD5");
  739. else if(id == SSL_RSA_WITH_IDEA_CBC_MD5)
  740. msnprintf(buf, buf_size, "%s", "SSL_RSA_WITH_IDEA_CBC_MD5");
  741. else if(id == SSL_RSA_WITH_DES_CBC_MD5)
  742. msnprintf(buf, buf_size, "%s", "SSL_RSA_WITH_DES_CBC_MD5");
  743. else if(id == SSL_RSA_WITH_3DES_EDE_CBC_MD5)
  744. msnprintf(buf, buf_size, "%s", "SSL_RSA_WITH_3DES_EDE_CBC_MD5");
  745. else
  746. return Curl_cipher_suite_get_str(id, buf, buf_size, prefer_rfc);
  747. return 0;
  748. }
  749. static uint16_t sectransp_cipher_suite_walk_str(const char **str,
  750. const char **end)
  751. {
  752. uint16_t id = Curl_cipher_suite_walk_str(str, end);
  753. size_t len = *end - *str;
  754. if(!id) {
  755. /* are these fortezza suites even supported ? */
  756. if(strncasecompare("SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA", *str, len))
  757. id = SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA;
  758. else if(strncasecompare("SSL_FORTEZZA_DMS_WITH_NULL_SHA", *str, len))
  759. id = SSL_FORTEZZA_DMS_WITH_NULL_SHA;
  760. /* can TLS_EMPTY_RENEGOTIATION_INFO_SCSV even be set ? */
  761. else if(strncasecompare("TLS_EMPTY_RENEGOTIATION_INFO_SCSV", *str, len))
  762. id = TLS_EMPTY_RENEGOTIATION_INFO_SCSV;
  763. /* do we still need to support these SSL2-only ciphers ? */
  764. else if(strncasecompare("SSL_RSA_WITH_RC2_CBC_MD5", *str, len))
  765. id = SSL_RSA_WITH_RC2_CBC_MD5;
  766. else if(strncasecompare("SSL_RSA_WITH_IDEA_CBC_MD5", *str, len))
  767. id = SSL_RSA_WITH_IDEA_CBC_MD5;
  768. else if(strncasecompare("SSL_RSA_WITH_DES_CBC_MD5", *str, len))
  769. id = SSL_RSA_WITH_DES_CBC_MD5;
  770. else if(strncasecompare("SSL_RSA_WITH_3DES_EDE_CBC_MD5", *str, len))
  771. id = SSL_RSA_WITH_3DES_EDE_CBC_MD5;
  772. }
  773. return id;
  774. }
  775. /* allocated memory must be freed */
  776. static SSLCipherSuite * sectransp_get_supported_ciphers(SSLContextRef ssl_ctx,
  777. size_t *len)
  778. {
  779. SSLCipherSuite *ciphers = NULL;
  780. OSStatus err = noErr;
  781. *len = 0;
  782. err = SSLGetNumberSupportedCiphers(ssl_ctx, len);
  783. if(err != noErr)
  784. goto failed;
  785. ciphers = malloc(*len * sizeof(SSLCipherSuite));
  786. if(!ciphers)
  787. goto failed;
  788. err = SSLGetSupportedCiphers(ssl_ctx, ciphers, len);
  789. if(err != noErr)
  790. goto failed;
  791. #if CURL_BUILD_MAC
  792. {
  793. int maj = 0, min = 0;
  794. GetDarwinVersionNumber(&maj, &min);
  795. /* There is a known bug in early versions of Mountain Lion where ST's ECC
  796. ciphers (cipher suite 0xC001 through 0xC032) simply do not work.
  797. Work around the problem here by disabling those ciphers if we are
  798. running in an affected version of macOS. */
  799. if(maj == 12 && min <= 3) {
  800. size_t i = 0, j = 0;
  801. for(; i < *len; i++) {
  802. if(ciphers[i] >= 0xC001 && ciphers[i] <= 0xC032)
  803. continue;
  804. ciphers[j++] = ciphers[i];
  805. }
  806. *len = j;
  807. }
  808. }
  809. #endif
  810. return ciphers;
  811. failed:
  812. *len = 0;
  813. Curl_safefree(ciphers);
  814. return NULL;
  815. }
  816. static CURLcode sectransp_set_default_ciphers(struct Curl_easy *data,
  817. SSLContextRef ssl_ctx)
  818. {
  819. CURLcode ret = CURLE_SSL_CIPHER;
  820. size_t count = 0, i, j;
  821. OSStatus err;
  822. size_t supported_len;
  823. SSLCipherSuite *ciphers = NULL;
  824. ciphers = sectransp_get_supported_ciphers(ssl_ctx, &supported_len);
  825. if(!ciphers) {
  826. failf(data, "SSL: Failed to get supported ciphers");
  827. goto failed;
  828. }
  829. /* Intersect the ciphers supported by Secure Transport with the default
  830. * ciphers, using the order of the former. */
  831. for(i = 0; i < supported_len; i++) {
  832. for(j = 0; j < DEFAULT_CIPHERS_LEN; j++) {
  833. if(default_ciphers[j] == ciphers[i]) {
  834. ciphers[count++] = ciphers[i];
  835. break;
  836. }
  837. }
  838. }
  839. if(count == 0) {
  840. failf(data, "SSL: no supported default ciphers");
  841. goto failed;
  842. }
  843. err = SSLSetEnabledCiphers(ssl_ctx, ciphers, count);
  844. if(err != noErr) {
  845. failf(data, "SSL: SSLSetEnabledCiphers() failed: OSStatus %d", err);
  846. goto failed;
  847. }
  848. ret = CURLE_OK;
  849. failed:
  850. Curl_safefree(ciphers);
  851. return ret;
  852. }
  853. static CURLcode sectransp_set_selected_ciphers(struct Curl_easy *data,
  854. SSLContextRef ssl_ctx,
  855. const char *ciphers)
  856. {
  857. CURLcode ret = CURLE_SSL_CIPHER;
  858. size_t count = 0, i;
  859. const char *ptr, *end;
  860. OSStatus err;
  861. size_t supported_len;
  862. SSLCipherSuite *supported = NULL;
  863. SSLCipherSuite *selected = NULL;
  864. supported = sectransp_get_supported_ciphers(ssl_ctx, &supported_len);
  865. if(!supported) {
  866. failf(data, "SSL: Failed to get supported ciphers");
  867. goto failed;
  868. }
  869. selected = malloc(supported_len * sizeof(SSLCipherSuite));
  870. if(!selected) {
  871. failf(data, "SSL: Failed to allocate memory");
  872. goto failed;
  873. }
  874. for(ptr = ciphers; ptr[0] != '\0' && count < supported_len; ptr = end) {
  875. uint16_t id = sectransp_cipher_suite_walk_str(&ptr, &end);
  876. /* Check if cipher is supported */
  877. if(id) {
  878. for(i = 0; i < supported_len && supported[i] != id; i++);
  879. if(i == supported_len)
  880. id = 0;
  881. }
  882. if(!id) {
  883. if(ptr[0] != '\0')
  884. infof(data, "SSL: unknown cipher in list: \"%.*s\"", (int) (end - ptr),
  885. ptr);
  886. continue;
  887. }
  888. /* No duplicates allowed (so selected cannot overflow) */
  889. for(i = 0; i < count && selected[i] != id; i++);
  890. if(i < count) {
  891. infof(data, "SSL: duplicate cipher in list: \"%.*s\"", (int) (end - ptr),
  892. ptr);
  893. continue;
  894. }
  895. selected[count++] = id;
  896. }
  897. if(count == 0) {
  898. failf(data, "SSL: no supported cipher in list");
  899. goto failed;
  900. }
  901. err = SSLSetEnabledCiphers(ssl_ctx, selected, count);
  902. if(err != noErr) {
  903. failf(data, "SSL: SSLSetEnabledCiphers() failed: OSStatus %d", err);
  904. goto failed;
  905. }
  906. ret = CURLE_OK;
  907. failed:
  908. Curl_safefree(supported);
  909. Curl_safefree(selected);
  910. return ret;
  911. }
  912. static void sectransp_session_free(void *sessionid, size_t idsize)
  913. {
  914. /* ST, as of iOS 5 and Mountain Lion, has no public method of deleting a
  915. cached session ID inside the Security framework. There is a private
  916. function that does this, but I do not want to have to explain to you why I
  917. got your application rejected from the App Store due to the use of a
  918. private API, so the best we can do is free up our own char array that we
  919. created way back in sectransp_connect_step1... */
  920. (void)idsize;
  921. Curl_safefree(sessionid);
  922. }
  923. static CURLcode sectransp_connect_step1(struct Curl_cfilter *cf,
  924. struct Curl_easy *data)
  925. {
  926. struct ssl_connect_data *connssl = cf->ctx;
  927. struct st_ssl_backend_data *backend =
  928. (struct st_ssl_backend_data *)connssl->backend;
  929. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  930. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  931. const struct curl_blob *ssl_cablob = conn_config->ca_info_blob;
  932. const char * const ssl_cafile =
  933. /* CURLOPT_CAINFO_BLOB overrides CURLOPT_CAINFO */
  934. (ssl_cablob ? NULL : conn_config->CAfile);
  935. const bool verifypeer = conn_config->verifypeer;
  936. char * const ssl_cert = ssl_config->primary.clientcert;
  937. const struct curl_blob *ssl_cert_blob = ssl_config->primary.cert_blob;
  938. char *ciphers;
  939. OSStatus err = noErr;
  940. CURLcode result;
  941. #if CURL_BUILD_MAC
  942. int darwinver_maj = 0, darwinver_min = 0;
  943. DEBUGASSERT(backend);
  944. CURL_TRC_CF(data, cf, "connect_step1");
  945. GetDarwinVersionNumber(&darwinver_maj, &darwinver_min);
  946. #endif /* CURL_BUILD_MAC */
  947. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  948. if(&SSLCreateContext) { /* use the newer API if available */
  949. if(backend->ssl_ctx)
  950. CFRelease(backend->ssl_ctx);
  951. backend->ssl_ctx = SSLCreateContext(NULL, kSSLClientSide, kSSLStreamType);
  952. if(!backend->ssl_ctx) {
  953. failf(data, "SSL: could not create a context");
  954. return CURLE_OUT_OF_MEMORY;
  955. }
  956. }
  957. else {
  958. /* The old ST API does not exist under iOS, so do not compile it: */
  959. #if CURL_SUPPORT_MAC_10_8
  960. if(backend->ssl_ctx)
  961. (void)SSLDisposeContext(backend->ssl_ctx);
  962. err = SSLNewContext(FALSE, &(backend->ssl_ctx));
  963. if(err != noErr) {
  964. failf(data, "SSL: could not create a context: OSStatus %d", err);
  965. return CURLE_OUT_OF_MEMORY;
  966. }
  967. #endif /* CURL_SUPPORT_MAC_10_8 */
  968. }
  969. #else
  970. if(backend->ssl_ctx)
  971. (void)SSLDisposeContext(backend->ssl_ctx);
  972. err = SSLNewContext(FALSE, &(backend->ssl_ctx));
  973. if(err != noErr) {
  974. failf(data, "SSL: could not create a context: OSStatus %d", err);
  975. return CURLE_OUT_OF_MEMORY;
  976. }
  977. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  978. backend->ssl_write_buffered_length = 0UL; /* reset buffered write length */
  979. result = sectransp_set_ssl_version_min_max(data, backend, conn_config);
  980. if(result != CURLE_OK)
  981. return result;
  982. #if (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && \
  983. defined(HAVE_BUILTIN_AVAILABLE)
  984. if(connssl->alpn) {
  985. if(__builtin_available(macOS 10.13.4, iOS 11, tvOS 11, *)) {
  986. struct alpn_proto_buf proto;
  987. size_t i;
  988. CFStringRef cstr;
  989. CFMutableArrayRef alpnArr = CFArrayCreateMutable(NULL, 0,
  990. &kCFTypeArrayCallBacks);
  991. for(i = 0; i < connssl->alpn->count; ++i) {
  992. cstr = CFStringCreateWithCString(NULL, connssl->alpn->entries[i],
  993. kCFStringEncodingUTF8);
  994. if(!cstr)
  995. return CURLE_OUT_OF_MEMORY;
  996. CFArrayAppendValue(alpnArr, cstr);
  997. CFRelease(cstr);
  998. }
  999. err = SSLSetALPNProtocols(backend->ssl_ctx, alpnArr);
  1000. if(err != noErr)
  1001. infof(data, "WARNING: failed to set ALPN protocols; OSStatus %d",
  1002. err);
  1003. CFRelease(alpnArr);
  1004. Curl_alpn_to_proto_str(&proto, connssl->alpn);
  1005. infof(data, VTLS_INFOF_ALPN_OFFER_1STR, proto.data);
  1006. }
  1007. }
  1008. #endif
  1009. if(ssl_config->key) {
  1010. infof(data, "WARNING: SSL: CURLOPT_SSLKEY is ignored by Secure "
  1011. "Transport. The private key must be in the Keychain.");
  1012. }
  1013. if(ssl_cert || ssl_cert_blob) {
  1014. bool is_cert_data = ssl_cert_blob != NULL;
  1015. bool is_cert_file = (!is_cert_data) && is_file(ssl_cert);
  1016. SecIdentityRef cert_and_key = NULL;
  1017. /* User wants to authenticate with a client cert. Look for it. Assume that
  1018. the user wants to use an identity loaded from the Keychain. If not, try
  1019. it as a file on disk */
  1020. if(!is_cert_data)
  1021. err = CopyIdentityWithLabel(ssl_cert, &cert_and_key);
  1022. else
  1023. err = !noErr;
  1024. if((err != noErr) && (is_cert_file || is_cert_data)) {
  1025. if(!ssl_config->cert_type)
  1026. infof(data, "SSL: Certificate type not set, assuming "
  1027. "PKCS#12 format.");
  1028. else if(!strcasecompare(ssl_config->cert_type, "P12")) {
  1029. failf(data, "SSL: The Security framework only supports "
  1030. "loading identities that are in PKCS#12 format.");
  1031. return CURLE_SSL_CERTPROBLEM;
  1032. }
  1033. err = CopyIdentityFromPKCS12File(ssl_cert, ssl_cert_blob,
  1034. ssl_config->key_passwd,
  1035. &cert_and_key);
  1036. }
  1037. if(err == noErr && cert_and_key) {
  1038. SecCertificateRef cert = NULL;
  1039. CFTypeRef certs_c[1];
  1040. CFArrayRef certs;
  1041. /* If we found one, print it out: */
  1042. err = SecIdentityCopyCertificate(cert_and_key, &cert);
  1043. if(err == noErr) {
  1044. char *certp;
  1045. result = CopyCertSubject(data, cert, &certp);
  1046. if(!result) {
  1047. infof(data, "Client certificate: %s", certp);
  1048. free(certp);
  1049. }
  1050. CFRelease(cert);
  1051. if(result == CURLE_PEER_FAILED_VERIFICATION)
  1052. return CURLE_SSL_CERTPROBLEM;
  1053. if(result)
  1054. return result;
  1055. }
  1056. certs_c[0] = cert_and_key;
  1057. certs = CFArrayCreate(NULL, (const void **)certs_c, 1L,
  1058. &kCFTypeArrayCallBacks);
  1059. err = SSLSetCertificate(backend->ssl_ctx, certs);
  1060. if(certs)
  1061. CFRelease(certs);
  1062. if(err != noErr) {
  1063. failf(data, "SSL: SSLSetCertificate() failed: OSStatus %d", err);
  1064. return CURLE_SSL_CERTPROBLEM;
  1065. }
  1066. CFRelease(cert_and_key);
  1067. }
  1068. else {
  1069. const char *cert_showfilename_error =
  1070. is_cert_data ? "(memory blob)" : ssl_cert;
  1071. switch(err) {
  1072. case errSecAuthFailed: case -25264: /* errSecPkcs12VerifyFailure */
  1073. failf(data, "SSL: Incorrect password for the certificate \"%s\" "
  1074. "and its private key.", cert_showfilename_error);
  1075. break;
  1076. case -26275: /* errSecDecode */ case -25257: /* errSecUnknownFormat */
  1077. failf(data, "SSL: Couldn't make sense of the data in the "
  1078. "certificate \"%s\" and its private key.",
  1079. cert_showfilename_error);
  1080. break;
  1081. case -25260: /* errSecPassphraseRequired */
  1082. failf(data, "SSL The certificate \"%s\" requires a password.",
  1083. cert_showfilename_error);
  1084. break;
  1085. case errSecItemNotFound:
  1086. failf(data, "SSL: cannot find the certificate \"%s\" and its private "
  1087. "key in the Keychain.", cert_showfilename_error);
  1088. break;
  1089. default:
  1090. failf(data, "SSL: cannot load the certificate \"%s\" and its private "
  1091. "key: OSStatus %d", cert_showfilename_error, err);
  1092. break;
  1093. }
  1094. return CURLE_SSL_CERTPROBLEM;
  1095. }
  1096. }
  1097. /* SSL always tries to verify the peer, this only says whether it should
  1098. * fail to connect if the verification fails, or if it should continue
  1099. * anyway. In the latter case the result of the verification is checked with
  1100. * SSL_get_verify_result() below. */
  1101. #if CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS
  1102. /* Snow Leopard introduced the SSLSetSessionOption() function, but due to
  1103. a library bug with the way the kSSLSessionOptionBreakOnServerAuth flag
  1104. works, it does not work as expected under Snow Leopard, Lion or
  1105. Mountain Lion.
  1106. So we need to call SSLSetEnableCertVerify() on those older cats in order
  1107. to disable certificate validation if the user turned that off.
  1108. (Secure Transport always validates the certificate chain by default.)
  1109. Note:
  1110. Darwin 11.x.x is Lion (10.7)
  1111. Darwin 12.x.x is Mountain Lion (10.8)
  1112. Darwin 13.x.x is Mavericks (10.9)
  1113. Darwin 14.x.x is Yosemite (10.10)
  1114. Darwin 15.x.x is El Capitan (10.11)
  1115. */
  1116. #if CURL_BUILD_MAC
  1117. if(&SSLSetSessionOption && darwinver_maj >= 13) {
  1118. #else
  1119. if(&SSLSetSessionOption) {
  1120. #endif /* CURL_BUILD_MAC */
  1121. bool break_on_auth = !conn_config->verifypeer ||
  1122. ssl_cafile || ssl_cablob;
  1123. err = SSLSetSessionOption(backend->ssl_ctx,
  1124. kSSLSessionOptionBreakOnServerAuth,
  1125. break_on_auth);
  1126. if(err != noErr) {
  1127. failf(data, "SSL: SSLSetSessionOption() failed: OSStatus %d", err);
  1128. return CURLE_SSL_CONNECT_ERROR;
  1129. }
  1130. }
  1131. else {
  1132. #if CURL_SUPPORT_MAC_10_8
  1133. err = SSLSetEnableCertVerify(backend->ssl_ctx,
  1134. conn_config->verifypeer ? true : FALSE);
  1135. if(err != noErr) {
  1136. failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err);
  1137. return CURLE_SSL_CONNECT_ERROR;
  1138. }
  1139. #endif /* CURL_SUPPORT_MAC_10_8 */
  1140. }
  1141. #else
  1142. err = SSLSetEnableCertVerify(backend->ssl_ctx,
  1143. conn_config->verifypeer ? true : FALSE);
  1144. if(err != noErr) {
  1145. failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err);
  1146. return CURLE_SSL_CONNECT_ERROR;
  1147. }
  1148. #endif /* CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS */
  1149. if((ssl_cafile || ssl_cablob) && verifypeer) {
  1150. bool is_cert_data = ssl_cablob != NULL;
  1151. bool is_cert_file = (!is_cert_data) && is_file(ssl_cafile);
  1152. if(!(is_cert_file || is_cert_data)) {
  1153. failf(data, "SSL: cannot load CA certificate file %s",
  1154. ssl_cafile ? ssl_cafile : "(blob memory)");
  1155. return CURLE_SSL_CACERT_BADFILE;
  1156. }
  1157. }
  1158. /* Configure hostname check. SNI is used if available.
  1159. * Both hostname check and SNI require SSLSetPeerDomainName().
  1160. * Also: the verifyhost setting influences SNI usage */
  1161. if(conn_config->verifyhost) {
  1162. char *server = connssl->peer.sni ?
  1163. connssl->peer.sni : connssl->peer.hostname;
  1164. err = SSLSetPeerDomainName(backend->ssl_ctx, server, strlen(server));
  1165. if(err != noErr) {
  1166. failf(data, "SSL: SSLSetPeerDomainName() failed: OSStatus %d",
  1167. err);
  1168. return CURLE_SSL_CONNECT_ERROR;
  1169. }
  1170. if(connssl->peer.type != CURL_SSL_PEER_DNS) {
  1171. infof(data, "WARNING: using IP address, SNI is being disabled by "
  1172. "the OS.");
  1173. }
  1174. }
  1175. else {
  1176. infof(data, "WARNING: disabling hostname validation also disables SNI.");
  1177. }
  1178. ciphers = conn_config->cipher_list;
  1179. if(ciphers) {
  1180. result = sectransp_set_selected_ciphers(data, backend->ssl_ctx, ciphers);
  1181. }
  1182. else {
  1183. result = sectransp_set_default_ciphers(data, backend->ssl_ctx);
  1184. }
  1185. if(result != CURLE_OK) {
  1186. failf(data, "SSL: Unable to set ciphers for SSL/TLS handshake. "
  1187. "Error code: %d", (int)result);
  1188. return CURLE_SSL_CIPHER;
  1189. }
  1190. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  1191. /* We want to enable 1/n-1 when using a CBC cipher unless the user
  1192. specifically does not want us doing that: */
  1193. if(&SSLSetSessionOption) {
  1194. SSLSetSessionOption(backend->ssl_ctx, kSSLSessionOptionSendOneByteRecord,
  1195. !ssl_config->enable_beast);
  1196. SSLSetSessionOption(backend->ssl_ctx, kSSLSessionOptionFalseStart,
  1197. ssl_config->falsestart); /* false start support */
  1198. }
  1199. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  1200. /* Check if there is a cached ID we can/should use here! */
  1201. if(ssl_config->primary.cache_session) {
  1202. char *ssl_sessionid;
  1203. size_t ssl_sessionid_len;
  1204. Curl_ssl_sessionid_lock(data);
  1205. if(!Curl_ssl_getsessionid(cf, data, &connssl->peer,
  1206. (void **)&ssl_sessionid, &ssl_sessionid_len,
  1207. NULL)) {
  1208. /* we got a session id, use it! */
  1209. err = SSLSetPeerID(backend->ssl_ctx, ssl_sessionid, ssl_sessionid_len);
  1210. Curl_ssl_sessionid_unlock(data);
  1211. if(err != noErr) {
  1212. failf(data, "SSL: SSLSetPeerID() failed: OSStatus %d", err);
  1213. return CURLE_SSL_CONNECT_ERROR;
  1214. }
  1215. /* Informational message */
  1216. infof(data, "SSL reusing session ID");
  1217. }
  1218. /* If there is not one, then let's make one up! This has to be done prior
  1219. to starting the handshake. */
  1220. else {
  1221. ssl_sessionid =
  1222. aprintf("%s:%d:%d:%s:%d",
  1223. ssl_cafile ? ssl_cafile : "(blob memory)",
  1224. verifypeer, conn_config->verifyhost, connssl->peer.hostname,
  1225. connssl->peer.port);
  1226. ssl_sessionid_len = strlen(ssl_sessionid);
  1227. err = SSLSetPeerID(backend->ssl_ctx, ssl_sessionid, ssl_sessionid_len);
  1228. if(err != noErr) {
  1229. Curl_ssl_sessionid_unlock(data);
  1230. failf(data, "SSL: SSLSetPeerID() failed: OSStatus %d", err);
  1231. return CURLE_SSL_CONNECT_ERROR;
  1232. }
  1233. result = Curl_ssl_set_sessionid(cf, data, &connssl->peer, NULL,
  1234. ssl_sessionid, ssl_sessionid_len,
  1235. sectransp_session_free);
  1236. Curl_ssl_sessionid_unlock(data);
  1237. if(result)
  1238. return result;
  1239. }
  1240. }
  1241. err = SSLSetIOFuncs(backend->ssl_ctx,
  1242. sectransp_bio_cf_in_read,
  1243. sectransp_bio_cf_out_write);
  1244. if(err != noErr) {
  1245. failf(data, "SSL: SSLSetIOFuncs() failed: OSStatus %d", err);
  1246. return CURLE_SSL_CONNECT_ERROR;
  1247. }
  1248. err = SSLSetConnection(backend->ssl_ctx, cf);
  1249. if(err != noErr) {
  1250. failf(data, "SSL: SSLSetConnection() failed: %d", err);
  1251. return CURLE_SSL_CONNECT_ERROR;
  1252. }
  1253. connssl->connecting_state = ssl_connect_2;
  1254. return CURLE_OK;
  1255. }
  1256. static long pem_to_der(const char *in, unsigned char **out, size_t *outlen)
  1257. {
  1258. char *sep_start, *sep_end, *cert_start, *cert_end;
  1259. size_t i, j, err;
  1260. size_t len;
  1261. char *b64;
  1262. /* Jump through the separators at the beginning of the certificate. */
  1263. sep_start = strstr(in, "-----");
  1264. if(!sep_start)
  1265. return 0;
  1266. cert_start = strstr(sep_start + 1, "-----");
  1267. if(!cert_start)
  1268. return -1;
  1269. cert_start += 5;
  1270. /* Find separator after the end of the certificate. */
  1271. cert_end = strstr(cert_start, "-----");
  1272. if(!cert_end)
  1273. return -1;
  1274. sep_end = strstr(cert_end + 1, "-----");
  1275. if(!sep_end)
  1276. return -1;
  1277. sep_end += 5;
  1278. len = cert_end - cert_start;
  1279. b64 = malloc(len + 1);
  1280. if(!b64)
  1281. return -1;
  1282. /* Create base64 string without linefeeds. */
  1283. for(i = 0, j = 0; i < len; i++) {
  1284. if(cert_start[i] != '\r' && cert_start[i] != '\n')
  1285. b64[j++] = cert_start[i];
  1286. }
  1287. b64[j] = '\0';
  1288. err = Curl_base64_decode((const char *)b64, out, outlen);
  1289. free(b64);
  1290. if(err) {
  1291. free(*out);
  1292. return -1;
  1293. }
  1294. return sep_end - in;
  1295. }
  1296. #define MAX_CERTS_SIZE (50*1024*1024) /* arbitrary - to catch mistakes */
  1297. static int read_cert(const char *file, unsigned char **out, size_t *outlen)
  1298. {
  1299. int fd;
  1300. ssize_t n;
  1301. unsigned char buf[512];
  1302. struct dynbuf certs;
  1303. Curl_dyn_init(&certs, MAX_CERTS_SIZE);
  1304. fd = open(file, 0);
  1305. if(fd < 0)
  1306. return -1;
  1307. for(;;) {
  1308. n = read(fd, buf, sizeof(buf));
  1309. if(!n)
  1310. break;
  1311. if(n < 0) {
  1312. close(fd);
  1313. Curl_dyn_free(&certs);
  1314. return -1;
  1315. }
  1316. if(Curl_dyn_addn(&certs, buf, n)) {
  1317. close(fd);
  1318. return -1;
  1319. }
  1320. }
  1321. close(fd);
  1322. *out = Curl_dyn_uptr(&certs);
  1323. *outlen = Curl_dyn_len(&certs);
  1324. return 0;
  1325. }
  1326. static CURLcode append_cert_to_array(struct Curl_easy *data,
  1327. const unsigned char *buf, size_t buflen,
  1328. CFMutableArrayRef array)
  1329. {
  1330. char *certp;
  1331. CURLcode result;
  1332. SecCertificateRef cacert;
  1333. CFDataRef certdata;
  1334. certdata = CFDataCreate(kCFAllocatorDefault, buf, (CFIndex)buflen);
  1335. if(!certdata) {
  1336. failf(data, "SSL: failed to allocate array for CA certificate");
  1337. return CURLE_OUT_OF_MEMORY;
  1338. }
  1339. cacert = SecCertificateCreateWithData(kCFAllocatorDefault, certdata);
  1340. CFRelease(certdata);
  1341. if(!cacert) {
  1342. failf(data, "SSL: failed to create SecCertificate from CA certificate");
  1343. return CURLE_SSL_CACERT_BADFILE;
  1344. }
  1345. /* Check if cacert is valid. */
  1346. result = CopyCertSubject(data, cacert, &certp);
  1347. switch(result) {
  1348. case CURLE_OK:
  1349. break;
  1350. case CURLE_PEER_FAILED_VERIFICATION:
  1351. CFRelease(cacert);
  1352. return CURLE_SSL_CACERT_BADFILE;
  1353. case CURLE_OUT_OF_MEMORY:
  1354. default:
  1355. CFRelease(cacert);
  1356. return result;
  1357. }
  1358. free(certp);
  1359. CFArrayAppendValue(array, cacert);
  1360. CFRelease(cacert);
  1361. return CURLE_OK;
  1362. }
  1363. static CURLcode verify_cert_buf(struct Curl_cfilter *cf,
  1364. struct Curl_easy *data,
  1365. const unsigned char *certbuf, size_t buflen,
  1366. SSLContextRef ctx)
  1367. {
  1368. int n = 0;
  1369. CURLcode rc;
  1370. long res;
  1371. unsigned char *der;
  1372. size_t derlen, offset = 0;
  1373. OSStatus ret;
  1374. SecTrustResultType trust_eval;
  1375. CFMutableArrayRef array = NULL;
  1376. SecTrustRef trust = NULL;
  1377. CURLcode result = CURLE_PEER_FAILED_VERIFICATION;
  1378. (void)cf;
  1379. /*
  1380. * Certbuf now contains the contents of the certificate file, which can be
  1381. * - a single DER certificate,
  1382. * - a single PEM certificate or
  1383. * - a bunch of PEM certificates (certificate bundle).
  1384. *
  1385. * Go through certbuf, and convert any PEM certificate in it into DER
  1386. * format.
  1387. */
  1388. array = CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks);
  1389. if(!array) {
  1390. failf(data, "SSL: out of memory creating CA certificate array");
  1391. result = CURLE_OUT_OF_MEMORY;
  1392. goto out;
  1393. }
  1394. while(offset < buflen) {
  1395. n++;
  1396. /*
  1397. * Check if the certificate is in PEM format, and convert it to DER. If
  1398. * this fails, we assume the certificate is in DER format.
  1399. */
  1400. res = pem_to_der((const char *)certbuf + offset, &der, &derlen);
  1401. if(res < 0) {
  1402. failf(data, "SSL: invalid CA certificate #%d (offset %zu) in bundle",
  1403. n, offset);
  1404. result = CURLE_SSL_CACERT_BADFILE;
  1405. goto out;
  1406. }
  1407. offset += res;
  1408. if(res == 0 && offset == 0) {
  1409. /* This is not a PEM file, probably a certificate in DER format. */
  1410. rc = append_cert_to_array(data, certbuf, buflen, array);
  1411. if(rc != CURLE_OK) {
  1412. CURL_TRC_CF(data, cf, "append_cert for CA failed");
  1413. result = rc;
  1414. goto out;
  1415. }
  1416. break;
  1417. }
  1418. else if(res == 0) {
  1419. /* No more certificates in the bundle. */
  1420. break;
  1421. }
  1422. rc = append_cert_to_array(data, der, derlen, array);
  1423. free(der);
  1424. if(rc != CURLE_OK) {
  1425. CURL_TRC_CF(data, cf, "append_cert for CA failed");
  1426. result = rc;
  1427. goto out;
  1428. }
  1429. }
  1430. ret = SSLCopyPeerTrust(ctx, &trust);
  1431. if(!trust) {
  1432. failf(data, "SSL: error getting certificate chain");
  1433. goto out;
  1434. }
  1435. else if(ret != noErr) {
  1436. failf(data, "SSLCopyPeerTrust() returned error %d", ret);
  1437. goto out;
  1438. }
  1439. CURL_TRC_CF(data, cf, "setting %d trust anchors", n);
  1440. ret = SecTrustSetAnchorCertificates(trust, array);
  1441. if(ret != noErr) {
  1442. failf(data, "SecTrustSetAnchorCertificates() returned error %d", ret);
  1443. goto out;
  1444. }
  1445. ret = SecTrustSetAnchorCertificatesOnly(trust, TRUE);
  1446. if(ret != noErr) {
  1447. failf(data, "SecTrustSetAnchorCertificatesOnly() returned error %d", ret);
  1448. goto out;
  1449. }
  1450. trust_eval = 0;
  1451. ret = SecTrustEvaluate(trust, &trust_eval);
  1452. if(ret != noErr) {
  1453. failf(data, "SecTrustEvaluate() returned error %d", ret);
  1454. goto out;
  1455. }
  1456. switch(trust_eval) {
  1457. case kSecTrustResultUnspecified:
  1458. /* what does this really mean? */
  1459. CURL_TRC_CF(data, cf, "trust result: Unspecified");
  1460. result = CURLE_OK;
  1461. goto out;
  1462. case kSecTrustResultProceed:
  1463. CURL_TRC_CF(data, cf, "trust result: Proceed");
  1464. result = CURLE_OK;
  1465. goto out;
  1466. case kSecTrustResultRecoverableTrustFailure:
  1467. failf(data, "SSL: peer not verified: RecoverableTrustFailure");
  1468. goto out;
  1469. case kSecTrustResultDeny:
  1470. failf(data, "SSL: peer not verified: Deny");
  1471. goto out;
  1472. default:
  1473. failf(data, "SSL: perr not verified: result=%d", trust_eval);
  1474. goto out;
  1475. }
  1476. out:
  1477. if(trust)
  1478. CFRelease(trust);
  1479. if(array)
  1480. CFRelease(array);
  1481. return result;
  1482. }
  1483. static CURLcode verify_cert(struct Curl_cfilter *cf,
  1484. struct Curl_easy *data, const char *cafile,
  1485. const struct curl_blob *ca_info_blob,
  1486. SSLContextRef ctx)
  1487. {
  1488. CURLcode result;
  1489. unsigned char *certbuf;
  1490. size_t buflen;
  1491. bool free_certbuf = FALSE;
  1492. if(ca_info_blob) {
  1493. CURL_TRC_CF(data, cf, "verify_peer, CA from config blob");
  1494. certbuf = ca_info_blob->data;
  1495. buflen = ca_info_blob->len;
  1496. }
  1497. else if(cafile) {
  1498. CURL_TRC_CF(data, cf, "verify_peer, CA from file '%s'", cafile);
  1499. if(read_cert(cafile, &certbuf, &buflen) < 0) {
  1500. failf(data, "SSL: failed to read or invalid CA certificate");
  1501. return CURLE_SSL_CACERT_BADFILE;
  1502. }
  1503. free_certbuf = TRUE;
  1504. }
  1505. else
  1506. return CURLE_SSL_CACERT_BADFILE;
  1507. result = verify_cert_buf(cf, data, certbuf, buflen, ctx);
  1508. if(free_certbuf)
  1509. free(certbuf);
  1510. return result;
  1511. }
  1512. #ifdef SECTRANSP_PINNEDPUBKEY
  1513. static CURLcode pkp_pin_peer_pubkey(struct Curl_easy *data,
  1514. SSLContextRef ctx,
  1515. const char *pinnedpubkey)
  1516. { /* Scratch */
  1517. size_t pubkeylen, realpubkeylen, spkiHeaderLength = 24;
  1518. unsigned char *pubkey = NULL, *realpubkey = NULL;
  1519. const unsigned char *spkiHeader = NULL;
  1520. CFDataRef publicKeyBits = NULL;
  1521. /* Result is returned to caller */
  1522. CURLcode result = CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  1523. /* if a path was not specified, do not pin */
  1524. if(!pinnedpubkey)
  1525. return CURLE_OK;
  1526. if(!ctx)
  1527. return result;
  1528. do {
  1529. SecTrustRef trust;
  1530. OSStatus ret;
  1531. SecKeyRef keyRef;
  1532. ret = SSLCopyPeerTrust(ctx, &trust);
  1533. if(ret != noErr || !trust)
  1534. break;
  1535. keyRef = SecTrustCopyPublicKey(trust);
  1536. CFRelease(trust);
  1537. if(!keyRef)
  1538. break;
  1539. #ifdef SECTRANSP_PINNEDPUBKEY_V1
  1540. publicKeyBits = SecKeyCopyExternalRepresentation(keyRef, NULL);
  1541. CFRelease(keyRef);
  1542. if(!publicKeyBits)
  1543. break;
  1544. #elif SECTRANSP_PINNEDPUBKEY_V2
  1545. {
  1546. OSStatus success;
  1547. success = SecItemExport(keyRef, kSecFormatOpenSSL, 0, NULL,
  1548. &publicKeyBits);
  1549. CFRelease(keyRef);
  1550. if(success != errSecSuccess || !publicKeyBits)
  1551. break;
  1552. }
  1553. #endif /* SECTRANSP_PINNEDPUBKEY_V2 */
  1554. pubkeylen = (size_t)CFDataGetLength(publicKeyBits);
  1555. pubkey = (unsigned char *)CFDataGetBytePtr(publicKeyBits);
  1556. switch(pubkeylen) {
  1557. case 526:
  1558. /* 4096 bit RSA pubkeylen == 526 */
  1559. spkiHeader = rsa4096SpkiHeader;
  1560. break;
  1561. case 270:
  1562. /* 2048 bit RSA pubkeylen == 270 */
  1563. spkiHeader = rsa2048SpkiHeader;
  1564. break;
  1565. #ifdef SECTRANSP_PINNEDPUBKEY_V1
  1566. case 65:
  1567. /* ecDSA secp256r1 pubkeylen == 65 */
  1568. spkiHeader = ecDsaSecp256r1SpkiHeader;
  1569. spkiHeaderLength = 26;
  1570. break;
  1571. case 97:
  1572. /* ecDSA secp384r1 pubkeylen == 97 */
  1573. spkiHeader = ecDsaSecp384r1SpkiHeader;
  1574. spkiHeaderLength = 23;
  1575. break;
  1576. default:
  1577. infof(data, "SSL: unhandled public key length: %zu", pubkeylen);
  1578. #elif SECTRANSP_PINNEDPUBKEY_V2
  1579. default:
  1580. /* ecDSA secp256r1 pubkeylen == 91 header already included?
  1581. * ecDSA secp384r1 header already included too
  1582. * we assume rest of algorithms do same, so do nothing
  1583. */
  1584. result = Curl_pin_peer_pubkey(data, pinnedpubkey, pubkey,
  1585. pubkeylen);
  1586. #endif /* SECTRANSP_PINNEDPUBKEY_V2 */
  1587. continue; /* break from loop */
  1588. }
  1589. realpubkeylen = pubkeylen + spkiHeaderLength;
  1590. realpubkey = malloc(realpubkeylen);
  1591. if(!realpubkey)
  1592. break;
  1593. memcpy(realpubkey, spkiHeader, spkiHeaderLength);
  1594. memcpy(realpubkey + spkiHeaderLength, pubkey, pubkeylen);
  1595. result = Curl_pin_peer_pubkey(data, pinnedpubkey, realpubkey,
  1596. realpubkeylen);
  1597. } while(0);
  1598. Curl_safefree(realpubkey);
  1599. if(publicKeyBits)
  1600. CFRelease(publicKeyBits);
  1601. return result;
  1602. }
  1603. #endif /* SECTRANSP_PINNEDPUBKEY */
  1604. static CURLcode sectransp_connect_step2(struct Curl_cfilter *cf,
  1605. struct Curl_easy *data)
  1606. {
  1607. struct ssl_connect_data *connssl = cf->ctx;
  1608. struct st_ssl_backend_data *backend =
  1609. (struct st_ssl_backend_data *)connssl->backend;
  1610. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  1611. OSStatus err;
  1612. SSLCipherSuite cipher;
  1613. SSLProtocol protocol = 0;
  1614. DEBUGASSERT(ssl_connect_2 == connssl->connecting_state);
  1615. DEBUGASSERT(backend);
  1616. CURL_TRC_CF(data, cf, "connect_step2");
  1617. /* Here goes nothing: */
  1618. check_handshake:
  1619. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  1620. err = SSLHandshake(backend->ssl_ctx);
  1621. if(err != noErr) {
  1622. switch(err) {
  1623. case errSSLWouldBlock: /* they are not done with us yet */
  1624. connssl->io_need = backend->ssl_direction ?
  1625. CURL_SSL_IO_NEED_SEND : CURL_SSL_IO_NEED_RECV;
  1626. return CURLE_OK;
  1627. /* The below is errSSLServerAuthCompleted; it is not defined in
  1628. Leopard's headers */
  1629. case -9841:
  1630. if((conn_config->CAfile || conn_config->ca_info_blob) &&
  1631. conn_config->verifypeer) {
  1632. CURLcode result = verify_cert(cf, data, conn_config->CAfile,
  1633. conn_config->ca_info_blob,
  1634. backend->ssl_ctx);
  1635. if(result)
  1636. return result;
  1637. }
  1638. /* the documentation says we need to call SSLHandshake() again */
  1639. goto check_handshake;
  1640. /* Problem with encrypt / decrypt */
  1641. case errSSLPeerDecodeError:
  1642. failf(data, "Decode failed");
  1643. break;
  1644. case errSSLDecryptionFail:
  1645. case errSSLPeerDecryptionFail:
  1646. failf(data, "Decryption failed");
  1647. break;
  1648. case errSSLPeerDecryptError:
  1649. failf(data, "A decryption error occurred");
  1650. break;
  1651. case errSSLBadCipherSuite:
  1652. failf(data, "A bad SSL cipher suite was encountered");
  1653. break;
  1654. case errSSLCrypto:
  1655. failf(data, "An underlying cryptographic error was encountered");
  1656. break;
  1657. #if CURL_BUILD_MAC_10_11 || CURL_BUILD_IOS_9
  1658. case errSSLWeakPeerEphemeralDHKey:
  1659. failf(data, "Indicates a weak ephemeral Diffie-Hellman key");
  1660. break;
  1661. #endif
  1662. /* Problem with the message record validation */
  1663. case errSSLBadRecordMac:
  1664. case errSSLPeerBadRecordMac:
  1665. failf(data, "A record with a bad message authentication code (MAC) "
  1666. "was encountered");
  1667. break;
  1668. case errSSLRecordOverflow:
  1669. case errSSLPeerRecordOverflow:
  1670. failf(data, "A record overflow occurred");
  1671. break;
  1672. /* Problem with zlib decompression */
  1673. case errSSLPeerDecompressFail:
  1674. failf(data, "Decompression failed");
  1675. break;
  1676. /* Problem with access */
  1677. case errSSLPeerAccessDenied:
  1678. failf(data, "Access was denied");
  1679. break;
  1680. case errSSLPeerInsufficientSecurity:
  1681. failf(data, "There is insufficient security for this operation");
  1682. break;
  1683. /* These are all certificate problems with the server: */
  1684. case errSSLXCertChainInvalid:
  1685. failf(data, "SSL certificate problem: Invalid certificate chain");
  1686. return CURLE_PEER_FAILED_VERIFICATION;
  1687. case errSSLUnknownRootCert:
  1688. failf(data, "SSL certificate problem: Untrusted root certificate");
  1689. return CURLE_PEER_FAILED_VERIFICATION;
  1690. case errSSLNoRootCert:
  1691. failf(data, "SSL certificate problem: No root certificate");
  1692. return CURLE_PEER_FAILED_VERIFICATION;
  1693. case errSSLCertNotYetValid:
  1694. failf(data, "SSL certificate problem: The certificate chain had a "
  1695. "certificate that is not yet valid");
  1696. return CURLE_PEER_FAILED_VERIFICATION;
  1697. case errSSLCertExpired:
  1698. case errSSLPeerCertExpired:
  1699. failf(data, "SSL certificate problem: Certificate chain had an "
  1700. "expired certificate");
  1701. return CURLE_PEER_FAILED_VERIFICATION;
  1702. case errSSLBadCert:
  1703. case errSSLPeerBadCert:
  1704. failf(data, "SSL certificate problem: Couldn't understand the server "
  1705. "certificate format");
  1706. return CURLE_PEER_FAILED_VERIFICATION;
  1707. case errSSLPeerUnsupportedCert:
  1708. failf(data, "SSL certificate problem: An unsupported certificate "
  1709. "format was encountered");
  1710. return CURLE_PEER_FAILED_VERIFICATION;
  1711. case errSSLPeerCertRevoked:
  1712. failf(data, "SSL certificate problem: The certificate was revoked");
  1713. return CURLE_PEER_FAILED_VERIFICATION;
  1714. case errSSLPeerCertUnknown:
  1715. failf(data, "SSL certificate problem: The certificate is unknown");
  1716. return CURLE_PEER_FAILED_VERIFICATION;
  1717. /* These are all certificate problems with the client: */
  1718. case errSecAuthFailed:
  1719. failf(data, "SSL authentication failed");
  1720. break;
  1721. case errSSLPeerHandshakeFail:
  1722. failf(data, "SSL peer handshake failed, the server most likely "
  1723. "requires a client certificate to connect");
  1724. break;
  1725. case errSSLPeerUnknownCA:
  1726. failf(data, "SSL server rejected the client certificate due to "
  1727. "the certificate being signed by an unknown certificate "
  1728. "authority");
  1729. break;
  1730. /* This error is raised if the server's cert did not match the server's
  1731. hostname: */
  1732. case errSSLHostNameMismatch:
  1733. failf(data, "SSL certificate peer verification failed, the "
  1734. "certificate did not match \"%s\"\n", connssl->peer.dispname);
  1735. return CURLE_PEER_FAILED_VERIFICATION;
  1736. /* Problem with SSL / TLS negotiation */
  1737. case errSSLNegotiation:
  1738. failf(data, "Could not negotiate an SSL cipher suite with the server");
  1739. break;
  1740. case errSSLBadConfiguration:
  1741. failf(data, "A configuration error occurred");
  1742. break;
  1743. case errSSLProtocol:
  1744. failf(data, "SSL protocol error");
  1745. break;
  1746. case errSSLPeerProtocolVersion:
  1747. failf(data, "A bad protocol version was encountered");
  1748. break;
  1749. case errSSLPeerNoRenegotiation:
  1750. failf(data, "No renegotiation is allowed");
  1751. break;
  1752. /* Generic handshake errors: */
  1753. case errSSLConnectionRefused:
  1754. failf(data, "Server dropped the connection during the SSL handshake");
  1755. break;
  1756. case errSSLClosedAbort:
  1757. failf(data, "Server aborted the SSL handshake");
  1758. break;
  1759. case errSSLClosedGraceful:
  1760. failf(data, "The connection closed gracefully");
  1761. break;
  1762. case errSSLClosedNoNotify:
  1763. failf(data, "The server closed the session with no notification");
  1764. break;
  1765. /* Sometimes paramErr happens with buggy ciphers: */
  1766. case paramErr:
  1767. case errSSLInternal:
  1768. case errSSLPeerInternalError:
  1769. failf(data, "Internal SSL engine error encountered during the "
  1770. "SSL handshake");
  1771. break;
  1772. case errSSLFatalAlert:
  1773. failf(data, "Fatal SSL engine error encountered during the SSL "
  1774. "handshake");
  1775. break;
  1776. /* Unclassified error */
  1777. case errSSLBufferOverflow:
  1778. failf(data, "An insufficient buffer was provided");
  1779. break;
  1780. case errSSLIllegalParam:
  1781. failf(data, "An illegal parameter was encountered");
  1782. break;
  1783. case errSSLModuleAttach:
  1784. failf(data, "Module attach failure");
  1785. break;
  1786. case errSSLSessionNotFound:
  1787. failf(data, "An attempt to restore an unknown session failed");
  1788. break;
  1789. case errSSLPeerExportRestriction:
  1790. failf(data, "An export restriction occurred");
  1791. break;
  1792. case errSSLPeerUserCancelled:
  1793. failf(data, "The user canceled the operation");
  1794. break;
  1795. case errSSLPeerUnexpectedMsg:
  1796. failf(data, "Peer rejected unexpected message");
  1797. break;
  1798. #if CURL_BUILD_MAC_10_11 || CURL_BUILD_IOS_9
  1799. /* Treating non-fatal error as fatal like before */
  1800. case errSSLClientHelloReceived:
  1801. failf(data, "A non-fatal result for providing a server name "
  1802. "indication");
  1803. break;
  1804. #endif
  1805. /* Error codes defined in the enum but should never be returned.
  1806. We list them here just in case. */
  1807. #if CURL_BUILD_MAC_10_6
  1808. /* Only returned when kSSLSessionOptionBreakOnCertRequested is set */
  1809. case errSSLClientCertRequested:
  1810. failf(data, "Server requested a client certificate during the "
  1811. "handshake");
  1812. return CURLE_SSL_CLIENTCERT;
  1813. #endif
  1814. #if CURL_BUILD_MAC_10_9
  1815. /* Alias for errSSLLast, end of error range */
  1816. case errSSLUnexpectedRecord:
  1817. failf(data, "Unexpected (skipped) record in DTLS");
  1818. break;
  1819. #endif
  1820. default:
  1821. /* May also return codes listed in Security Framework Result Codes */
  1822. failf(data, "Unknown SSL protocol error in connection to %s:%d",
  1823. connssl->peer.hostname, err);
  1824. break;
  1825. }
  1826. return CURLE_SSL_CONNECT_ERROR;
  1827. }
  1828. else {
  1829. char cipher_str[64];
  1830. /* we have been connected fine, we are not waiting for anything else. */
  1831. connssl->connecting_state = ssl_connect_3;
  1832. #ifdef SECTRANSP_PINNEDPUBKEY
  1833. if(data->set.str[STRING_SSL_PINNEDPUBLICKEY]) {
  1834. CURLcode result =
  1835. pkp_pin_peer_pubkey(data, backend->ssl_ctx,
  1836. data->set.str[STRING_SSL_PINNEDPUBLICKEY]);
  1837. if(result) {
  1838. failf(data, "SSL: public key does not match pinned public key");
  1839. return result;
  1840. }
  1841. }
  1842. #endif /* SECTRANSP_PINNEDPUBKEY */
  1843. /* Informational message */
  1844. (void)SSLGetNegotiatedCipher(backend->ssl_ctx, &cipher);
  1845. (void)SSLGetNegotiatedProtocolVersion(backend->ssl_ctx, &protocol);
  1846. sectransp_cipher_suite_get_str((uint16_t) cipher, cipher_str,
  1847. sizeof(cipher_str), TRUE);
  1848. switch(protocol) {
  1849. case kSSLProtocol2:
  1850. infof(data, "SSL 2.0 connection using %s", cipher_str);
  1851. break;
  1852. case kSSLProtocol3:
  1853. infof(data, "SSL 3.0 connection using %s", cipher_str);
  1854. break;
  1855. case kTLSProtocol1:
  1856. infof(data, "TLS 1.0 connection using %s", cipher_str);
  1857. break;
  1858. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1859. case kTLSProtocol11:
  1860. infof(data, "TLS 1.1 connection using %s", cipher_str);
  1861. break;
  1862. case kTLSProtocol12:
  1863. infof(data, "TLS 1.2 connection using %s", cipher_str);
  1864. break;
  1865. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  1866. #if CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11
  1867. case kTLSProtocol13:
  1868. infof(data, "TLS 1.3 connection using %s", cipher_str);
  1869. break;
  1870. #endif /* CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11 */
  1871. default:
  1872. infof(data, "Unknown protocol connection");
  1873. break;
  1874. }
  1875. #if (CURL_BUILD_MAC_10_13 || CURL_BUILD_IOS_11) && \
  1876. defined(HAVE_BUILTIN_AVAILABLE)
  1877. if(connssl->alpn) {
  1878. if(__builtin_available(macOS 10.13.4, iOS 11, tvOS 11, *)) {
  1879. CFArrayRef alpnArr = NULL;
  1880. CFStringRef chosenProtocol = NULL;
  1881. err = SSLCopyALPNProtocols(backend->ssl_ctx, &alpnArr);
  1882. if(err == noErr && alpnArr && CFArrayGetCount(alpnArr) >= 1)
  1883. chosenProtocol = CFArrayGetValueAtIndex(alpnArr, 0);
  1884. #ifdef USE_HTTP2
  1885. if(chosenProtocol &&
  1886. !CFStringCompare(chosenProtocol, CFSTR(ALPN_H2), 0)) {
  1887. cf->conn->alpn = CURL_HTTP_VERSION_2;
  1888. }
  1889. else
  1890. #endif
  1891. if(chosenProtocol &&
  1892. !CFStringCompare(chosenProtocol, CFSTR(ALPN_HTTP_1_1), 0)) {
  1893. cf->conn->alpn = CURL_HTTP_VERSION_1_1;
  1894. }
  1895. else
  1896. infof(data, VTLS_INFOF_NO_ALPN);
  1897. /* chosenProtocol is a reference to the string within alpnArr
  1898. and does not need to be freed separately */
  1899. if(alpnArr)
  1900. CFRelease(alpnArr);
  1901. }
  1902. }
  1903. #endif
  1904. return CURLE_OK;
  1905. }
  1906. }
  1907. static CURLcode
  1908. add_cert_to_certinfo(struct Curl_easy *data,
  1909. SecCertificateRef server_cert,
  1910. int idx)
  1911. {
  1912. CURLcode result = CURLE_OK;
  1913. const char *beg;
  1914. const char *end;
  1915. CFDataRef cert_data = SecCertificateCopyData(server_cert);
  1916. if(!cert_data)
  1917. return CURLE_PEER_FAILED_VERIFICATION;
  1918. beg = (const char *)CFDataGetBytePtr(cert_data);
  1919. end = beg + CFDataGetLength(cert_data);
  1920. result = Curl_extract_certinfo(data, idx, beg, end);
  1921. CFRelease(cert_data);
  1922. return result;
  1923. }
  1924. static CURLcode
  1925. collect_server_cert_single(struct Curl_cfilter *cf, struct Curl_easy *data,
  1926. SecCertificateRef server_cert,
  1927. CFIndex idx)
  1928. {
  1929. CURLcode result = CURLE_OK;
  1930. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  1931. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1932. if(data->set.verbose) {
  1933. char *certp;
  1934. result = CopyCertSubject(data, server_cert, &certp);
  1935. if(!result) {
  1936. infof(data, "Server certificate: %s", certp);
  1937. free(certp);
  1938. }
  1939. }
  1940. #endif
  1941. if(ssl_config->certinfo)
  1942. result = add_cert_to_certinfo(data, server_cert, (int)idx);
  1943. return result;
  1944. }
  1945. /* This should be called during step3 of the connection at the earliest */
  1946. static CURLcode collect_server_cert(struct Curl_cfilter *cf,
  1947. struct Curl_easy *data)
  1948. {
  1949. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1950. const bool show_verbose_server_cert = data->set.verbose;
  1951. #else
  1952. const bool show_verbose_server_cert = FALSE;
  1953. #endif
  1954. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  1955. CURLcode result = ssl_config->certinfo ?
  1956. CURLE_PEER_FAILED_VERIFICATION : CURLE_OK;
  1957. struct ssl_connect_data *connssl = cf->ctx;
  1958. struct st_ssl_backend_data *backend =
  1959. (struct st_ssl_backend_data *)connssl->backend;
  1960. CFArrayRef server_certs = NULL;
  1961. SecCertificateRef server_cert;
  1962. OSStatus err;
  1963. CFIndex i, count;
  1964. SecTrustRef trust = NULL;
  1965. DEBUGASSERT(backend);
  1966. if(!show_verbose_server_cert && !ssl_config->certinfo)
  1967. return CURLE_OK;
  1968. if(!backend->ssl_ctx)
  1969. return result;
  1970. #if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS
  1971. #if CURL_BUILD_IOS
  1972. #pragma unused(server_certs)
  1973. err = SSLCopyPeerTrust(backend->ssl_ctx, &trust);
  1974. /* For some reason, SSLCopyPeerTrust() can return noErr and yet return
  1975. a null trust, so be on guard for that: */
  1976. if(err == noErr && trust) {
  1977. count = SecTrustGetCertificateCount(trust);
  1978. if(ssl_config->certinfo)
  1979. result = Curl_ssl_init_certinfo(data, (int)count);
  1980. for(i = 0L ; !result && (i < count) ; i++) {
  1981. server_cert = SecTrustGetCertificateAtIndex(trust, i);
  1982. result = collect_server_cert_single(cf, data, server_cert, i);
  1983. }
  1984. CFRelease(trust);
  1985. }
  1986. #else
  1987. /* SSLCopyPeerCertificates() is deprecated as of Mountain Lion.
  1988. The function SecTrustGetCertificateAtIndex() is officially present
  1989. in Lion, but it is unfortunately also present in Snow Leopard as
  1990. private API and does not work as expected. So we have to look for
  1991. a different symbol to make sure this code is only executed under
  1992. Lion or later. */
  1993. if(&SecTrustCopyPublicKey) {
  1994. #pragma unused(server_certs)
  1995. err = SSLCopyPeerTrust(backend->ssl_ctx, &trust);
  1996. /* For some reason, SSLCopyPeerTrust() can return noErr and yet return
  1997. a null trust, so be on guard for that: */
  1998. if(err == noErr && trust) {
  1999. count = SecTrustGetCertificateCount(trust);
  2000. if(ssl_config->certinfo)
  2001. result = Curl_ssl_init_certinfo(data, (int)count);
  2002. for(i = 0L ; !result && (i < count) ; i++) {
  2003. server_cert = SecTrustGetCertificateAtIndex(trust, i);
  2004. result = collect_server_cert_single(cf, data, server_cert, i);
  2005. }
  2006. CFRelease(trust);
  2007. }
  2008. }
  2009. else {
  2010. #if CURL_SUPPORT_MAC_10_8
  2011. err = SSLCopyPeerCertificates(backend->ssl_ctx, &server_certs);
  2012. /* Just in case SSLCopyPeerCertificates() returns null too... */
  2013. if(err == noErr && server_certs) {
  2014. count = CFArrayGetCount(server_certs);
  2015. if(ssl_config->certinfo)
  2016. result = Curl_ssl_init_certinfo(data, (int)count);
  2017. for(i = 0L ; !result && (i < count) ; i++) {
  2018. server_cert = (SecCertificateRef)CFArrayGetValueAtIndex(server_certs,
  2019. i);
  2020. result = collect_server_cert_single(cf, data, server_cert, i);
  2021. }
  2022. CFRelease(server_certs);
  2023. }
  2024. #endif /* CURL_SUPPORT_MAC_10_8 */
  2025. }
  2026. #endif /* CURL_BUILD_IOS */
  2027. #else
  2028. #pragma unused(trust)
  2029. err = SSLCopyPeerCertificates(backend->ssl_ctx, &server_certs);
  2030. if(err == noErr) {
  2031. count = CFArrayGetCount(server_certs);
  2032. if(ssl_config->certinfo)
  2033. result = Curl_ssl_init_certinfo(data, (int)count);
  2034. for(i = 0L ; !result && (i < count) ; i++) {
  2035. server_cert = (SecCertificateRef)CFArrayGetValueAtIndex(server_certs, i);
  2036. result = collect_server_cert_single(cf, data, server_cert, i);
  2037. }
  2038. CFRelease(server_certs);
  2039. }
  2040. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  2041. return result;
  2042. }
  2043. static CURLcode sectransp_connect_step3(struct Curl_cfilter *cf,
  2044. struct Curl_easy *data)
  2045. {
  2046. struct ssl_connect_data *connssl = cf->ctx;
  2047. CURLcode result;
  2048. CURL_TRC_CF(data, cf, "connect_step3");
  2049. /* There is no step 3!
  2050. * Well, okay, let's collect server certificates, and if verbose mode is on,
  2051. * let's print the details of the server certificates. */
  2052. result = collect_server_cert(cf, data);
  2053. if(result)
  2054. return result;
  2055. connssl->connecting_state = ssl_connect_done;
  2056. return CURLE_OK;
  2057. }
  2058. static CURLcode
  2059. sectransp_connect_common(struct Curl_cfilter *cf, struct Curl_easy *data,
  2060. bool nonblocking,
  2061. bool *done)
  2062. {
  2063. CURLcode result;
  2064. struct ssl_connect_data *connssl = cf->ctx;
  2065. curl_socket_t sockfd = Curl_conn_cf_get_socket(cf, data);
  2066. int what;
  2067. /* check if the connection has already been established */
  2068. if(ssl_connection_complete == connssl->state) {
  2069. *done = TRUE;
  2070. return CURLE_OK;
  2071. }
  2072. if(ssl_connect_1 == connssl->connecting_state) {
  2073. /* Find out how much more time we are allowed */
  2074. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  2075. if(timeout_ms < 0) {
  2076. /* no need to continue if time already is up */
  2077. failf(data, "SSL connection timeout");
  2078. return CURLE_OPERATION_TIMEDOUT;
  2079. }
  2080. result = sectransp_connect_step1(cf, data);
  2081. if(result)
  2082. return result;
  2083. }
  2084. while(ssl_connect_2 == connssl->connecting_state) {
  2085. /* check allowed time left */
  2086. const timediff_t timeout_ms = Curl_timeleft(data, NULL, TRUE);
  2087. if(timeout_ms < 0) {
  2088. /* no need to continue if time already is up */
  2089. failf(data, "SSL connection timeout");
  2090. return CURLE_OPERATION_TIMEDOUT;
  2091. }
  2092. /* if ssl is expecting something, check if it is available. */
  2093. if(connssl->io_need) {
  2094. curl_socket_t writefd = (connssl->io_need & CURL_SSL_IO_NEED_SEND) ?
  2095. sockfd : CURL_SOCKET_BAD;
  2096. curl_socket_t readfd = (connssl->io_need & CURL_SSL_IO_NEED_RECV) ?
  2097. sockfd : CURL_SOCKET_BAD;
  2098. what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
  2099. nonblocking ? 0 : timeout_ms);
  2100. if(what < 0) {
  2101. /* fatal error */
  2102. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  2103. return CURLE_SSL_CONNECT_ERROR;
  2104. }
  2105. else if(0 == what) {
  2106. if(nonblocking) {
  2107. *done = FALSE;
  2108. return CURLE_OK;
  2109. }
  2110. else {
  2111. /* timeout */
  2112. failf(data, "SSL connection timeout");
  2113. return CURLE_OPERATION_TIMEDOUT;
  2114. }
  2115. }
  2116. /* socket is readable or writable */
  2117. }
  2118. /* Run transaction, and return to the caller if it failed or if this
  2119. * connection is done nonblocking and this loop would execute again. This
  2120. * permits the owner of a multi handle to abort a connection attempt
  2121. * before step2 has completed while ensuring that a client using select()
  2122. * or epoll() will always have a valid fdset to wait on.
  2123. */
  2124. result = sectransp_connect_step2(cf, data);
  2125. if(result || (nonblocking && (ssl_connect_2 == connssl->connecting_state)))
  2126. return result;
  2127. } /* repeat step2 until all transactions are done. */
  2128. if(ssl_connect_3 == connssl->connecting_state) {
  2129. result = sectransp_connect_step3(cf, data);
  2130. if(result)
  2131. return result;
  2132. }
  2133. if(ssl_connect_done == connssl->connecting_state) {
  2134. CURL_TRC_CF(data, cf, "connected");
  2135. connssl->state = ssl_connection_complete;
  2136. *done = TRUE;
  2137. }
  2138. else
  2139. *done = FALSE;
  2140. /* Reset our connect state machine */
  2141. connssl->connecting_state = ssl_connect_1;
  2142. return CURLE_OK;
  2143. }
  2144. static CURLcode sectransp_connect_nonblocking(struct Curl_cfilter *cf,
  2145. struct Curl_easy *data,
  2146. bool *done)
  2147. {
  2148. return sectransp_connect_common(cf, data, TRUE, done);
  2149. }
  2150. static CURLcode sectransp_connect(struct Curl_cfilter *cf,
  2151. struct Curl_easy *data)
  2152. {
  2153. CURLcode result;
  2154. bool done = FALSE;
  2155. result = sectransp_connect_common(cf, data, FALSE, &done);
  2156. if(result)
  2157. return result;
  2158. DEBUGASSERT(done);
  2159. return CURLE_OK;
  2160. }
  2161. static ssize_t sectransp_recv(struct Curl_cfilter *cf,
  2162. struct Curl_easy *data,
  2163. char *buf,
  2164. size_t buffersize,
  2165. CURLcode *curlcode);
  2166. static CURLcode sectransp_shutdown(struct Curl_cfilter *cf,
  2167. struct Curl_easy *data,
  2168. bool send_shutdown, bool *done)
  2169. {
  2170. struct ssl_connect_data *connssl = cf->ctx;
  2171. struct st_ssl_backend_data *backend =
  2172. (struct st_ssl_backend_data *)connssl->backend;
  2173. CURLcode result = CURLE_OK;
  2174. ssize_t nread = 0;
  2175. char buf[1024];
  2176. size_t i;
  2177. DEBUGASSERT(backend);
  2178. if(!backend->ssl_ctx || cf->shutdown) {
  2179. *done = TRUE;
  2180. goto out;
  2181. }
  2182. connssl->io_need = CURL_SSL_IO_NEED_NONE;
  2183. *done = FALSE;
  2184. if(send_shutdown && !backend->sent_shutdown) {
  2185. OSStatus err;
  2186. CURL_TRC_CF(data, cf, "shutdown, send close notify");
  2187. err = SSLClose(backend->ssl_ctx);
  2188. switch(err) {
  2189. case noErr:
  2190. backend->sent_shutdown = TRUE;
  2191. break;
  2192. case errSSLWouldBlock:
  2193. connssl->io_need = CURL_SSL_IO_NEED_SEND;
  2194. result = CURLE_OK;
  2195. goto out;
  2196. default:
  2197. CURL_TRC_CF(data, cf, "shutdown, error: %d", (int)err);
  2198. result = CURLE_SEND_ERROR;
  2199. goto out;
  2200. }
  2201. }
  2202. for(i = 0; i < 10; ++i) {
  2203. if(!backend->sent_shutdown) {
  2204. nread = sectransp_recv(cf, data, buf, (int)sizeof(buf), &result);
  2205. }
  2206. else {
  2207. /* We would like to read the close notify from the server using
  2208. * Secure Transport, however SSLRead() no longer works after we
  2209. * sent the notify from our side. So, we just read from the
  2210. * underlying filter and hope it will end. */
  2211. nread = Curl_conn_cf_recv(cf->next, data, buf, sizeof(buf), &result);
  2212. }
  2213. CURL_TRC_CF(data, cf, "shutdown read -> %zd, %d", nread, result);
  2214. if(nread <= 0)
  2215. break;
  2216. }
  2217. if(nread > 0) {
  2218. /* still data coming in? */
  2219. connssl->io_need = CURL_SSL_IO_NEED_RECV;
  2220. }
  2221. else if(nread == 0) {
  2222. /* We got the close notify alert and are done. */
  2223. CURL_TRC_CF(data, cf, "shutdown done");
  2224. *done = TRUE;
  2225. }
  2226. else if(result == CURLE_AGAIN) {
  2227. connssl->io_need = CURL_SSL_IO_NEED_RECV;
  2228. result = CURLE_OK;
  2229. }
  2230. else {
  2231. DEBUGASSERT(result);
  2232. CURL_TRC_CF(data, cf, "shutdown, error: %d", result);
  2233. }
  2234. out:
  2235. cf->shutdown = (result || *done);
  2236. return result;
  2237. }
  2238. static void sectransp_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  2239. {
  2240. struct ssl_connect_data *connssl = cf->ctx;
  2241. struct st_ssl_backend_data *backend =
  2242. (struct st_ssl_backend_data *)connssl->backend;
  2243. (void) data;
  2244. DEBUGASSERT(backend);
  2245. if(backend->ssl_ctx) {
  2246. CURL_TRC_CF(data, cf, "close");
  2247. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  2248. if(&SSLCreateContext)
  2249. CFRelease(backend->ssl_ctx);
  2250. #if CURL_SUPPORT_MAC_10_8
  2251. else
  2252. (void)SSLDisposeContext(backend->ssl_ctx);
  2253. #endif /* CURL_SUPPORT_MAC_10_8 */
  2254. #else
  2255. (void)SSLDisposeContext(backend->ssl_ctx);
  2256. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  2257. backend->ssl_ctx = NULL;
  2258. }
  2259. }
  2260. static size_t sectransp_version(char *buffer, size_t size)
  2261. {
  2262. return msnprintf(buffer, size, "SecureTransport");
  2263. }
  2264. static bool sectransp_data_pending(struct Curl_cfilter *cf,
  2265. const struct Curl_easy *data)
  2266. {
  2267. const struct ssl_connect_data *connssl = cf->ctx;
  2268. struct st_ssl_backend_data *backend =
  2269. (struct st_ssl_backend_data *)connssl->backend;
  2270. OSStatus err;
  2271. size_t buffer;
  2272. (void)data;
  2273. DEBUGASSERT(backend);
  2274. if(backend->ssl_ctx) { /* SSL is in use */
  2275. CURL_TRC_CF((struct Curl_easy *)data, cf, "data_pending");
  2276. err = SSLGetBufferedReadSize(backend->ssl_ctx, &buffer);
  2277. if(err == noErr)
  2278. return buffer > 0UL;
  2279. return FALSE;
  2280. }
  2281. else
  2282. return FALSE;
  2283. }
  2284. static CURLcode sectransp_random(struct Curl_easy *data UNUSED_PARAM,
  2285. unsigned char *entropy, size_t length)
  2286. {
  2287. /* arc4random_buf() is not available on cats older than Lion, so let's
  2288. do this manually for the benefit of the older cats. */
  2289. size_t i;
  2290. u_int32_t random_number = 0;
  2291. (void)data;
  2292. for(i = 0 ; i < length ; i++) {
  2293. if(i % sizeof(u_int32_t) == 0)
  2294. random_number = arc4random();
  2295. entropy[i] = random_number & 0xFF;
  2296. random_number >>= 8;
  2297. }
  2298. i = random_number = 0;
  2299. return CURLE_OK;
  2300. }
  2301. static CURLcode sectransp_sha256sum(const unsigned char *tmp, /* input */
  2302. size_t tmplen,
  2303. unsigned char *sha256sum, /* output */
  2304. size_t sha256len)
  2305. {
  2306. (void)sha256len;
  2307. assert(sha256len >= CURL_SHA256_DIGEST_LENGTH);
  2308. (void)CC_SHA256(tmp, (CC_LONG)tmplen, sha256sum);
  2309. return CURLE_OK;
  2310. }
  2311. static bool sectransp_false_start(void)
  2312. {
  2313. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  2314. if(&SSLSetSessionOption)
  2315. return TRUE;
  2316. #endif
  2317. return FALSE;
  2318. }
  2319. static ssize_t sectransp_send(struct Curl_cfilter *cf,
  2320. struct Curl_easy *data,
  2321. const void *mem,
  2322. size_t len,
  2323. CURLcode *curlcode)
  2324. {
  2325. struct ssl_connect_data *connssl = cf->ctx;
  2326. struct st_ssl_backend_data *backend =
  2327. (struct st_ssl_backend_data *)connssl->backend;
  2328. size_t processed = 0UL;
  2329. OSStatus err;
  2330. DEBUGASSERT(backend);
  2331. /* The SSLWrite() function works a little differently than expected. The
  2332. fourth argument (processed) is currently documented in Apple's
  2333. documentation as: "On return, the length, in bytes, of the data actually
  2334. written."
  2335. Now, one could interpret that as "written to the socket," but actually,
  2336. it returns the amount of data that was written to a buffer internal to
  2337. the SSLContextRef instead. So it is possible for SSLWrite() to return
  2338. errSSLWouldBlock and a number of bytes "written" because those bytes were
  2339. encrypted and written to a buffer, not to the socket.
  2340. So if this happens, then we need to keep calling SSLWrite() over and
  2341. over again with no new data until it quits returning errSSLWouldBlock. */
  2342. /* Do we have buffered data to write from the last time we were called? */
  2343. if(backend->ssl_write_buffered_length) {
  2344. /* Write the buffered data: */
  2345. err = SSLWrite(backend->ssl_ctx, NULL, 0UL, &processed);
  2346. switch(err) {
  2347. case noErr:
  2348. /* processed is always going to be 0 because we did not write to
  2349. the buffer, so return how much was written to the socket */
  2350. processed = backend->ssl_write_buffered_length;
  2351. backend->ssl_write_buffered_length = 0UL;
  2352. break;
  2353. case errSSLWouldBlock: /* argh, try again */
  2354. *curlcode = CURLE_AGAIN;
  2355. return -1L;
  2356. default:
  2357. failf(data, "SSLWrite() returned error %d", err);
  2358. *curlcode = CURLE_SEND_ERROR;
  2359. return -1L;
  2360. }
  2361. }
  2362. else {
  2363. /* We have got new data to write: */
  2364. err = SSLWrite(backend->ssl_ctx, mem, len, &processed);
  2365. if(err != noErr) {
  2366. switch(err) {
  2367. case errSSLWouldBlock:
  2368. /* Data was buffered but not sent, we have to tell the caller
  2369. to try sending again, and remember how much was buffered */
  2370. backend->ssl_write_buffered_length = len;
  2371. *curlcode = CURLE_AGAIN;
  2372. return -1L;
  2373. default:
  2374. failf(data, "SSLWrite() returned error %d", err);
  2375. *curlcode = CURLE_SEND_ERROR;
  2376. return -1L;
  2377. }
  2378. }
  2379. }
  2380. return (ssize_t)processed;
  2381. }
  2382. static ssize_t sectransp_recv(struct Curl_cfilter *cf,
  2383. struct Curl_easy *data,
  2384. char *buf,
  2385. size_t buffersize,
  2386. CURLcode *curlcode)
  2387. {
  2388. struct ssl_connect_data *connssl = cf->ctx;
  2389. struct st_ssl_backend_data *backend =
  2390. (struct st_ssl_backend_data *)connssl->backend;
  2391. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  2392. size_t processed = 0UL;
  2393. OSStatus err;
  2394. DEBUGASSERT(backend);
  2395. again:
  2396. *curlcode = CURLE_OK;
  2397. err = SSLRead(backend->ssl_ctx, buf, buffersize, &processed);
  2398. if(err != noErr) {
  2399. switch(err) {
  2400. case errSSLWouldBlock: /* return how much we read (if anything) */
  2401. if(processed) {
  2402. return (ssize_t)processed;
  2403. }
  2404. *curlcode = CURLE_AGAIN;
  2405. return -1L;
  2406. /* errSSLClosedGraceful - server gracefully shut down the SSL session
  2407. errSSLClosedNoNotify - server hung up on us instead of sending a
  2408. closure alert notice, read() is returning 0
  2409. Either way, inform the caller that the server disconnected. */
  2410. case errSSLClosedGraceful:
  2411. case errSSLClosedNoNotify:
  2412. *curlcode = CURLE_OK;
  2413. return 0;
  2414. /* The below is errSSLPeerAuthCompleted; it is not defined in
  2415. Leopard's headers */
  2416. case -9841:
  2417. if((conn_config->CAfile || conn_config->ca_info_blob) &&
  2418. conn_config->verifypeer) {
  2419. CURLcode result = verify_cert(cf, data, conn_config->CAfile,
  2420. conn_config->ca_info_blob,
  2421. backend->ssl_ctx);
  2422. if(result) {
  2423. *curlcode = result;
  2424. return -1;
  2425. }
  2426. }
  2427. goto again;
  2428. default:
  2429. failf(data, "SSLRead() return error %d", err);
  2430. *curlcode = CURLE_RECV_ERROR;
  2431. return -1L;
  2432. }
  2433. }
  2434. return (ssize_t)processed;
  2435. }
  2436. static void *sectransp_get_internals(struct ssl_connect_data *connssl,
  2437. CURLINFO info UNUSED_PARAM)
  2438. {
  2439. struct st_ssl_backend_data *backend =
  2440. (struct st_ssl_backend_data *)connssl->backend;
  2441. (void)info;
  2442. DEBUGASSERT(backend);
  2443. return backend->ssl_ctx;
  2444. }
  2445. const struct Curl_ssl Curl_ssl_sectransp = {
  2446. { CURLSSLBACKEND_SECURETRANSPORT, "secure-transport" }, /* info */
  2447. SSLSUPP_CAINFO_BLOB |
  2448. SSLSUPP_CERTINFO |
  2449. #ifdef SECTRANSP_PINNEDPUBKEY
  2450. SSLSUPP_PINNEDPUBKEY |
  2451. #endif /* SECTRANSP_PINNEDPUBKEY */
  2452. SSLSUPP_HTTPS_PROXY |
  2453. SSLSUPP_CIPHER_LIST,
  2454. sizeof(struct st_ssl_backend_data),
  2455. Curl_none_init, /* init */
  2456. Curl_none_cleanup, /* cleanup */
  2457. sectransp_version, /* version */
  2458. Curl_none_check_cxn, /* check_cxn */
  2459. sectransp_shutdown, /* shutdown */
  2460. sectransp_data_pending, /* data_pending */
  2461. sectransp_random, /* random */
  2462. Curl_none_cert_status_request, /* cert_status_request */
  2463. sectransp_connect, /* connect */
  2464. sectransp_connect_nonblocking, /* connect_nonblocking */
  2465. Curl_ssl_adjust_pollset, /* adjust_pollset */
  2466. sectransp_get_internals, /* get_internals */
  2467. sectransp_close, /* close_one */
  2468. Curl_none_close_all, /* close_all */
  2469. Curl_none_set_engine, /* set_engine */
  2470. Curl_none_set_engine_default, /* set_engine_default */
  2471. Curl_none_engines_list, /* engines_list */
  2472. sectransp_false_start, /* false_start */
  2473. sectransp_sha256sum, /* sha256sum */
  2474. NULL, /* associate_connection */
  2475. NULL, /* disassociate_connection */
  2476. sectransp_recv, /* recv decrypted data */
  2477. sectransp_send, /* send data to encrypt */
  2478. NULL, /* get_channel_binding */
  2479. };
  2480. #if defined(__GNUC__) && defined(__APPLE__)
  2481. #pragma GCC diagnostic pop
  2482. #endif
  2483. #ifdef __GNUC__
  2484. #pragma GCC diagnostic pop
  2485. #endif
  2486. #ifdef __clang__
  2487. #pragma clang diagnostic pop
  2488. #endif
  2489. #endif /* USE_SECTRANSP */