sectransp.c 108 KB

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