sectransp.c 125 KB

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