sectransp.c 109 KB

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