sectransp.c 125 KB

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