curl_darwinssl.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012 - 2014, Nick Zitzmann, <nickzman@gmail.com>.
  9. * Copyright (C) 2012 - 2014, 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 http://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 Mac OS X 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 SessionHandle definition */
  29. #include "curl_base64.h"
  30. #ifdef USE_DARWINSSL
  31. #ifdef HAVE_LIMITS_H
  32. #include <limits.h>
  33. #endif
  34. #include <Security/Security.h>
  35. #include <Security/SecureTransport.h>
  36. #include <CoreFoundation/CoreFoundation.h>
  37. #include <CommonCrypto/CommonDigest.h>
  38. /* The Security framework has changed greatly between iOS and different OS X
  39. versions, and we will try to support as many of them as we can (back to
  40. Leopard and iOS 5) by using macros and weak-linking.
  41. IMPORTANT: If TLS 1.1 and 1.2 support are important for you on OS X, then
  42. you must build this project against the 10.8 SDK or later. */
  43. #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
  44. #if MAC_OS_X_VERSION_MAX_ALLOWED < 1050
  45. #error "The darwinssl back-end requires Leopard or later."
  46. #endif /* MAC_OS_X_VERSION_MAX_ALLOWED < 1050 */
  47. #define CURL_BUILD_IOS 0
  48. #define CURL_BUILD_IOS_7 0
  49. #define CURL_BUILD_MAC 1
  50. /* This is the maximum API level we are allowed to use when building: */
  51. #define CURL_BUILD_MAC_10_5 MAC_OS_X_VERSION_MAX_ALLOWED >= 1050
  52. #define CURL_BUILD_MAC_10_6 MAC_OS_X_VERSION_MAX_ALLOWED >= 1060
  53. #define CURL_BUILD_MAC_10_7 MAC_OS_X_VERSION_MAX_ALLOWED >= 1070
  54. #define CURL_BUILD_MAC_10_8 MAC_OS_X_VERSION_MAX_ALLOWED >= 1080
  55. #define CURL_BUILD_MAC_10_9 MAC_OS_X_VERSION_MAX_ALLOWED >= 1090
  56. /* These macros mean "the following code is present to allow runtime backward
  57. compatibility with at least this cat or earlier":
  58. (You set this at build-time by setting the MACOSX_DEPLOYMENT_TARGET
  59. environmental variable.) */
  60. #define CURL_SUPPORT_MAC_10_5 MAC_OS_X_VERSION_MIN_REQUIRED <= 1050
  61. #define CURL_SUPPORT_MAC_10_6 MAC_OS_X_VERSION_MIN_REQUIRED <= 1060
  62. #define CURL_SUPPORT_MAC_10_7 MAC_OS_X_VERSION_MIN_REQUIRED <= 1070
  63. #define CURL_SUPPORT_MAC_10_8 MAC_OS_X_VERSION_MIN_REQUIRED <= 1080
  64. #define CURL_SUPPORT_MAC_10_9 MAC_OS_X_VERSION_MIN_REQUIRED <= 1090
  65. #elif TARGET_OS_EMBEDDED || TARGET_OS_IPHONE
  66. #define CURL_BUILD_IOS 1
  67. #define CURL_BUILD_IOS_7 __IPHONE_OS_VERSION_MAX_ALLOWED >= 70000
  68. #define CURL_BUILD_MAC 0
  69. #define CURL_BUILD_MAC_10_5 0
  70. #define CURL_BUILD_MAC_10_6 0
  71. #define CURL_BUILD_MAC_10_7 0
  72. #define CURL_BUILD_MAC_10_8 0
  73. #define CURL_SUPPORT_MAC_10_5 0
  74. #define CURL_SUPPORT_MAC_10_6 0
  75. #define CURL_SUPPORT_MAC_10_7 0
  76. #define CURL_SUPPORT_MAC_10_8 0
  77. #else
  78. #error "The darwinssl back-end requires iOS or OS X."
  79. #endif /* (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) */
  80. #if CURL_BUILD_MAC
  81. #include <sys/sysctl.h>
  82. #endif /* CURL_BUILD_MAC */
  83. #include "urldata.h"
  84. #include "sendf.h"
  85. #include "inet_pton.h"
  86. #include "connect.h"
  87. #include "select.h"
  88. #include "vtls.h"
  89. #include "curl_darwinssl.h"
  90. #define _MPRINTF_REPLACE /* use our functions only */
  91. #include <curl/mprintf.h>
  92. #include "curl_memory.h"
  93. /* The last #include file should be: */
  94. #include "memdebug.h"
  95. /* From MacTypes.h (which we can't include because it isn't present in iOS: */
  96. #define ioErr -36
  97. #define paramErr -50
  98. /* The following two functions were ripped from Apple sample code,
  99. * with some modifications: */
  100. static OSStatus SocketRead(SSLConnectionRef connection,
  101. void *data, /* owned by
  102. * caller, data
  103. * RETURNED */
  104. size_t *dataLength) /* IN/OUT */
  105. {
  106. size_t bytesToGo = *dataLength;
  107. size_t initLen = bytesToGo;
  108. UInt8 *currData = (UInt8 *)data;
  109. /*int sock = *(int *)connection;*/
  110. struct ssl_connect_data *connssl = (struct ssl_connect_data *)connection;
  111. int sock = connssl->ssl_sockfd;
  112. OSStatus rtn = noErr;
  113. size_t bytesRead;
  114. ssize_t rrtn;
  115. int theErr;
  116. *dataLength = 0;
  117. for(;;) {
  118. bytesRead = 0;
  119. rrtn = read(sock, currData, bytesToGo);
  120. if(rrtn <= 0) {
  121. /* this is guesswork... */
  122. theErr = errno;
  123. if(rrtn == 0) { /* EOF = server hung up */
  124. /* the framework will turn this into errSSLClosedNoNotify */
  125. rtn = errSSLClosedGraceful;
  126. }
  127. else /* do the switch */
  128. switch(theErr) {
  129. case ENOENT:
  130. /* connection closed */
  131. rtn = errSSLClosedGraceful;
  132. break;
  133. case ECONNRESET:
  134. rtn = errSSLClosedAbort;
  135. break;
  136. case EAGAIN:
  137. rtn = errSSLWouldBlock;
  138. connssl->ssl_direction = false;
  139. break;
  140. default:
  141. rtn = ioErr;
  142. break;
  143. }
  144. break;
  145. }
  146. else {
  147. bytesRead = rrtn;
  148. }
  149. bytesToGo -= bytesRead;
  150. currData += bytesRead;
  151. if(bytesToGo == 0) {
  152. /* filled buffer with incoming data, done */
  153. break;
  154. }
  155. }
  156. *dataLength = initLen - bytesToGo;
  157. return rtn;
  158. }
  159. static OSStatus SocketWrite(SSLConnectionRef connection,
  160. const void *data,
  161. size_t *dataLength) /* IN/OUT */
  162. {
  163. size_t bytesSent = 0;
  164. /*int sock = *(int *)connection;*/
  165. struct ssl_connect_data *connssl = (struct ssl_connect_data *)connection;
  166. int sock = connssl->ssl_sockfd;
  167. ssize_t length;
  168. size_t dataLen = *dataLength;
  169. const UInt8 *dataPtr = (UInt8 *)data;
  170. OSStatus ortn;
  171. int theErr;
  172. *dataLength = 0;
  173. do {
  174. length = write(sock,
  175. (char*)dataPtr + bytesSent,
  176. dataLen - bytesSent);
  177. } while((length > 0) &&
  178. ( (bytesSent += length) < dataLen) );
  179. if(length <= 0) {
  180. theErr = errno;
  181. if(theErr == EAGAIN) {
  182. ortn = errSSLWouldBlock;
  183. connssl->ssl_direction = true;
  184. }
  185. else {
  186. ortn = ioErr;
  187. }
  188. }
  189. else {
  190. ortn = noErr;
  191. }
  192. *dataLength = bytesSent;
  193. return ortn;
  194. }
  195. CF_INLINE const char *SSLCipherNameForNumber(SSLCipherSuite cipher) {
  196. switch (cipher) {
  197. /* SSL version 3.0 */
  198. case SSL_RSA_WITH_NULL_MD5:
  199. return "SSL_RSA_WITH_NULL_MD5";
  200. break;
  201. case SSL_RSA_WITH_NULL_SHA:
  202. return "SSL_RSA_WITH_NULL_SHA";
  203. break;
  204. case SSL_RSA_EXPORT_WITH_RC4_40_MD5:
  205. return "SSL_RSA_EXPORT_WITH_RC4_40_MD5";
  206. break;
  207. case SSL_RSA_WITH_RC4_128_MD5:
  208. return "SSL_RSA_WITH_RC4_128_MD5";
  209. break;
  210. case SSL_RSA_WITH_RC4_128_SHA:
  211. return "SSL_RSA_WITH_RC4_128_SHA";
  212. break;
  213. case SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5:
  214. return "SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5";
  215. break;
  216. case SSL_RSA_WITH_IDEA_CBC_SHA:
  217. return "SSL_RSA_WITH_IDEA_CBC_SHA";
  218. break;
  219. case SSL_RSA_EXPORT_WITH_DES40_CBC_SHA:
  220. return "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA";
  221. break;
  222. case SSL_RSA_WITH_DES_CBC_SHA:
  223. return "SSL_RSA_WITH_DES_CBC_SHA";
  224. break;
  225. case SSL_RSA_WITH_3DES_EDE_CBC_SHA:
  226. return "SSL_RSA_WITH_3DES_EDE_CBC_SHA";
  227. break;
  228. case SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA:
  229. return "SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA";
  230. break;
  231. case SSL_DH_DSS_WITH_DES_CBC_SHA:
  232. return "SSL_DH_DSS_WITH_DES_CBC_SHA";
  233. break;
  234. case SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA:
  235. return "SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA";
  236. break;
  237. case SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA:
  238. return "SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA";
  239. break;
  240. case SSL_DH_RSA_WITH_DES_CBC_SHA:
  241. return "SSL_DH_RSA_WITH_DES_CBC_SHA";
  242. break;
  243. case SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA:
  244. return "SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA";
  245. break;
  246. case SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA:
  247. return "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA";
  248. break;
  249. case SSL_DHE_DSS_WITH_DES_CBC_SHA:
  250. return "SSL_DHE_DSS_WITH_DES_CBC_SHA";
  251. break;
  252. case SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
  253. return "SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA";
  254. break;
  255. case SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA:
  256. return "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA";
  257. break;
  258. case SSL_DHE_RSA_WITH_DES_CBC_SHA:
  259. return "SSL_DHE_RSA_WITH_DES_CBC_SHA";
  260. break;
  261. case SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
  262. return "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA";
  263. break;
  264. case SSL_DH_anon_EXPORT_WITH_RC4_40_MD5:
  265. return "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5";
  266. break;
  267. case SSL_DH_anon_WITH_RC4_128_MD5:
  268. return "SSL_DH_anon_WITH_RC4_128_MD5";
  269. break;
  270. case SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA:
  271. return "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA";
  272. break;
  273. case SSL_DH_anon_WITH_DES_CBC_SHA:
  274. return "SSL_DH_anon_WITH_DES_CBC_SHA";
  275. break;
  276. case SSL_DH_anon_WITH_3DES_EDE_CBC_SHA:
  277. return "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA";
  278. break;
  279. case SSL_FORTEZZA_DMS_WITH_NULL_SHA:
  280. return "SSL_FORTEZZA_DMS_WITH_NULL_SHA";
  281. break;
  282. case SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA:
  283. return "SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA";
  284. break;
  285. /* TLS 1.0 with AES (RFC 3268)
  286. (Apparently these are used in SSLv3 implementations as well.) */
  287. case TLS_RSA_WITH_AES_128_CBC_SHA:
  288. return "TLS_RSA_WITH_AES_128_CBC_SHA";
  289. break;
  290. case TLS_DH_DSS_WITH_AES_128_CBC_SHA:
  291. return "TLS_DH_DSS_WITH_AES_128_CBC_SHA";
  292. break;
  293. case TLS_DH_RSA_WITH_AES_128_CBC_SHA:
  294. return "TLS_DH_RSA_WITH_AES_128_CBC_SHA";
  295. break;
  296. case TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
  297. return "TLS_DHE_DSS_WITH_AES_128_CBC_SHA";
  298. break;
  299. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
  300. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA";
  301. break;
  302. case TLS_DH_anon_WITH_AES_128_CBC_SHA:
  303. return "TLS_DH_anon_WITH_AES_128_CBC_SHA";
  304. break;
  305. case TLS_RSA_WITH_AES_256_CBC_SHA:
  306. return "TLS_RSA_WITH_AES_256_CBC_SHA";
  307. break;
  308. case TLS_DH_DSS_WITH_AES_256_CBC_SHA:
  309. return "TLS_DH_DSS_WITH_AES_256_CBC_SHA";
  310. break;
  311. case TLS_DH_RSA_WITH_AES_256_CBC_SHA:
  312. return "TLS_DH_RSA_WITH_AES_256_CBC_SHA";
  313. break;
  314. case TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
  315. return "TLS_DHE_DSS_WITH_AES_256_CBC_SHA";
  316. break;
  317. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
  318. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA";
  319. break;
  320. case TLS_DH_anon_WITH_AES_256_CBC_SHA:
  321. return "TLS_DH_anon_WITH_AES_256_CBC_SHA";
  322. break;
  323. /* SSL version 2.0 */
  324. case SSL_RSA_WITH_RC2_CBC_MD5:
  325. return "SSL_RSA_WITH_RC2_CBC_MD5";
  326. break;
  327. case SSL_RSA_WITH_IDEA_CBC_MD5:
  328. return "SSL_RSA_WITH_IDEA_CBC_MD5";
  329. break;
  330. case SSL_RSA_WITH_DES_CBC_MD5:
  331. return "SSL_RSA_WITH_DES_CBC_MD5";
  332. break;
  333. case SSL_RSA_WITH_3DES_EDE_CBC_MD5:
  334. return "SSL_RSA_WITH_3DES_EDE_CBC_MD5";
  335. break;
  336. }
  337. return "SSL_NULL_WITH_NULL_NULL";
  338. }
  339. CF_INLINE const char *TLSCipherNameForNumber(SSLCipherSuite cipher) {
  340. switch(cipher) {
  341. /* TLS 1.0 with AES (RFC 3268) */
  342. case TLS_RSA_WITH_AES_128_CBC_SHA:
  343. return "TLS_RSA_WITH_AES_128_CBC_SHA";
  344. break;
  345. case TLS_DH_DSS_WITH_AES_128_CBC_SHA:
  346. return "TLS_DH_DSS_WITH_AES_128_CBC_SHA";
  347. break;
  348. case TLS_DH_RSA_WITH_AES_128_CBC_SHA:
  349. return "TLS_DH_RSA_WITH_AES_128_CBC_SHA";
  350. break;
  351. case TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
  352. return "TLS_DHE_DSS_WITH_AES_128_CBC_SHA";
  353. break;
  354. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
  355. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA";
  356. break;
  357. case TLS_DH_anon_WITH_AES_128_CBC_SHA:
  358. return "TLS_DH_anon_WITH_AES_128_CBC_SHA";
  359. break;
  360. case TLS_RSA_WITH_AES_256_CBC_SHA:
  361. return "TLS_RSA_WITH_AES_256_CBC_SHA";
  362. break;
  363. case TLS_DH_DSS_WITH_AES_256_CBC_SHA:
  364. return "TLS_DH_DSS_WITH_AES_256_CBC_SHA";
  365. break;
  366. case TLS_DH_RSA_WITH_AES_256_CBC_SHA:
  367. return "TLS_DH_RSA_WITH_AES_256_CBC_SHA";
  368. break;
  369. case TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
  370. return "TLS_DHE_DSS_WITH_AES_256_CBC_SHA";
  371. break;
  372. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
  373. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA";
  374. break;
  375. case TLS_DH_anon_WITH_AES_256_CBC_SHA:
  376. return "TLS_DH_anon_WITH_AES_256_CBC_SHA";
  377. break;
  378. #if CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS
  379. /* TLS 1.0 with ECDSA (RFC 4492) */
  380. case TLS_ECDH_ECDSA_WITH_NULL_SHA:
  381. return "TLS_ECDH_ECDSA_WITH_NULL_SHA";
  382. break;
  383. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  384. return "TLS_ECDH_ECDSA_WITH_RC4_128_SHA";
  385. break;
  386. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  387. return "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA";
  388. break;
  389. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  390. return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA";
  391. break;
  392. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  393. return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA";
  394. break;
  395. case TLS_ECDHE_ECDSA_WITH_NULL_SHA:
  396. return "TLS_ECDHE_ECDSA_WITH_NULL_SHA";
  397. break;
  398. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  399. return "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA";
  400. break;
  401. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  402. return "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA";
  403. break;
  404. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  405. return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA";
  406. break;
  407. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  408. return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA";
  409. break;
  410. case TLS_ECDH_RSA_WITH_NULL_SHA:
  411. return "TLS_ECDH_RSA_WITH_NULL_SHA";
  412. break;
  413. case TLS_ECDH_RSA_WITH_RC4_128_SHA:
  414. return "TLS_ECDH_RSA_WITH_RC4_128_SHA";
  415. break;
  416. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  417. return "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA";
  418. break;
  419. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  420. return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA";
  421. break;
  422. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  423. return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA";
  424. break;
  425. case TLS_ECDHE_RSA_WITH_NULL_SHA:
  426. return "TLS_ECDHE_RSA_WITH_NULL_SHA";
  427. break;
  428. case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  429. return "TLS_ECDHE_RSA_WITH_RC4_128_SHA";
  430. break;
  431. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  432. return "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA";
  433. break;
  434. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  435. return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA";
  436. break;
  437. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  438. return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA";
  439. break;
  440. case TLS_ECDH_anon_WITH_NULL_SHA:
  441. return "TLS_ECDH_anon_WITH_NULL_SHA";
  442. break;
  443. case TLS_ECDH_anon_WITH_RC4_128_SHA:
  444. return "TLS_ECDH_anon_WITH_RC4_128_SHA";
  445. break;
  446. case TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA:
  447. return "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA";
  448. break;
  449. case TLS_ECDH_anon_WITH_AES_128_CBC_SHA:
  450. return "TLS_ECDH_anon_WITH_AES_128_CBC_SHA";
  451. break;
  452. case TLS_ECDH_anon_WITH_AES_256_CBC_SHA:
  453. return "TLS_ECDH_anon_WITH_AES_256_CBC_SHA";
  454. break;
  455. #endif /* CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS */
  456. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  457. /* TLS 1.2 (RFC 5246) */
  458. case TLS_RSA_WITH_NULL_MD5:
  459. return "TLS_RSA_WITH_NULL_MD5";
  460. break;
  461. case TLS_RSA_WITH_NULL_SHA:
  462. return "TLS_RSA_WITH_NULL_SHA";
  463. break;
  464. case TLS_RSA_WITH_RC4_128_MD5:
  465. return "TLS_RSA_WITH_RC4_128_MD5";
  466. break;
  467. case TLS_RSA_WITH_RC4_128_SHA:
  468. return "TLS_RSA_WITH_RC4_128_SHA";
  469. break;
  470. case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
  471. return "TLS_RSA_WITH_3DES_EDE_CBC_SHA";
  472. break;
  473. case TLS_RSA_WITH_NULL_SHA256:
  474. return "TLS_RSA_WITH_NULL_SHA256";
  475. break;
  476. case TLS_RSA_WITH_AES_128_CBC_SHA256:
  477. return "TLS_RSA_WITH_AES_128_CBC_SHA256";
  478. break;
  479. case TLS_RSA_WITH_AES_256_CBC_SHA256:
  480. return "TLS_RSA_WITH_AES_256_CBC_SHA256";
  481. break;
  482. case TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA:
  483. return "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA";
  484. break;
  485. case TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA:
  486. return "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA";
  487. break;
  488. case TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
  489. return "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA";
  490. break;
  491. case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
  492. return "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA";
  493. break;
  494. case TLS_DH_DSS_WITH_AES_128_CBC_SHA256:
  495. return "TLS_DH_DSS_WITH_AES_128_CBC_SHA256";
  496. break;
  497. case TLS_DH_RSA_WITH_AES_128_CBC_SHA256:
  498. return "TLS_DH_RSA_WITH_AES_128_CBC_SHA256";
  499. break;
  500. case TLS_DHE_DSS_WITH_AES_128_CBC_SHA256:
  501. return "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256";
  502. break;
  503. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
  504. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256";
  505. break;
  506. case TLS_DH_DSS_WITH_AES_256_CBC_SHA256:
  507. return "TLS_DH_DSS_WITH_AES_256_CBC_SHA256";
  508. break;
  509. case TLS_DH_RSA_WITH_AES_256_CBC_SHA256:
  510. return "TLS_DH_RSA_WITH_AES_256_CBC_SHA256";
  511. break;
  512. case TLS_DHE_DSS_WITH_AES_256_CBC_SHA256:
  513. return "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256";
  514. break;
  515. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
  516. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256";
  517. break;
  518. case TLS_DH_anon_WITH_RC4_128_MD5:
  519. return "TLS_DH_anon_WITH_RC4_128_MD5";
  520. break;
  521. case TLS_DH_anon_WITH_3DES_EDE_CBC_SHA:
  522. return "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA";
  523. break;
  524. case TLS_DH_anon_WITH_AES_128_CBC_SHA256:
  525. return "TLS_DH_anon_WITH_AES_128_CBC_SHA256";
  526. break;
  527. case TLS_DH_anon_WITH_AES_256_CBC_SHA256:
  528. return "TLS_DH_anon_WITH_AES_256_CBC_SHA256";
  529. break;
  530. /* TLS 1.2 with AES GCM (RFC 5288) */
  531. case TLS_RSA_WITH_AES_128_GCM_SHA256:
  532. return "TLS_RSA_WITH_AES_128_GCM_SHA256";
  533. break;
  534. case TLS_RSA_WITH_AES_256_GCM_SHA384:
  535. return "TLS_RSA_WITH_AES_256_GCM_SHA384";
  536. break;
  537. case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256:
  538. return "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256";
  539. break;
  540. case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384:
  541. return "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384";
  542. break;
  543. case TLS_DH_RSA_WITH_AES_128_GCM_SHA256:
  544. return "TLS_DH_RSA_WITH_AES_128_GCM_SHA256";
  545. break;
  546. case TLS_DH_RSA_WITH_AES_256_GCM_SHA384:
  547. return "TLS_DH_RSA_WITH_AES_256_GCM_SHA384";
  548. break;
  549. case TLS_DHE_DSS_WITH_AES_128_GCM_SHA256:
  550. return "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256";
  551. break;
  552. case TLS_DHE_DSS_WITH_AES_256_GCM_SHA384:
  553. return "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384";
  554. break;
  555. case TLS_DH_DSS_WITH_AES_128_GCM_SHA256:
  556. return "TLS_DH_DSS_WITH_AES_128_GCM_SHA256";
  557. break;
  558. case TLS_DH_DSS_WITH_AES_256_GCM_SHA384:
  559. return "TLS_DH_DSS_WITH_AES_256_GCM_SHA384";
  560. break;
  561. case TLS_DH_anon_WITH_AES_128_GCM_SHA256:
  562. return "TLS_DH_anon_WITH_AES_128_GCM_SHA256";
  563. break;
  564. case TLS_DH_anon_WITH_AES_256_GCM_SHA384:
  565. return "TLS_DH_anon_WITH_AES_256_GCM_SHA384";
  566. break;
  567. /* TLS 1.2 with elliptic curve ciphers (RFC 5289) */
  568. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  569. return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256";
  570. break;
  571. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  572. return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384";
  573. break;
  574. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  575. return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256";
  576. break;
  577. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  578. return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384";
  579. break;
  580. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  581. return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256";
  582. break;
  583. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  584. return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384";
  585. break;
  586. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  587. return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256";
  588. break;
  589. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  590. return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384";
  591. break;
  592. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  593. return "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256";
  594. break;
  595. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  596. return "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384";
  597. break;
  598. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  599. return "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256";
  600. break;
  601. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  602. return "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384";
  603. break;
  604. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  605. return "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256";
  606. break;
  607. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  608. return "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384";
  609. break;
  610. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  611. return "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256";
  612. break;
  613. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  614. return "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384";
  615. break;
  616. case TLS_EMPTY_RENEGOTIATION_INFO_SCSV:
  617. return "TLS_EMPTY_RENEGOTIATION_INFO_SCSV";
  618. break;
  619. #else
  620. case SSL_RSA_WITH_NULL_MD5:
  621. return "TLS_RSA_WITH_NULL_MD5";
  622. break;
  623. case SSL_RSA_WITH_NULL_SHA:
  624. return "TLS_RSA_WITH_NULL_SHA";
  625. break;
  626. case SSL_RSA_WITH_RC4_128_MD5:
  627. return "TLS_RSA_WITH_RC4_128_MD5";
  628. break;
  629. case SSL_RSA_WITH_RC4_128_SHA:
  630. return "TLS_RSA_WITH_RC4_128_SHA";
  631. break;
  632. case SSL_RSA_WITH_3DES_EDE_CBC_SHA:
  633. return "TLS_RSA_WITH_3DES_EDE_CBC_SHA";
  634. break;
  635. case SSL_DH_anon_WITH_RC4_128_MD5:
  636. return "TLS_DH_anon_WITH_RC4_128_MD5";
  637. break;
  638. case SSL_DH_anon_WITH_3DES_EDE_CBC_SHA:
  639. return "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA";
  640. break;
  641. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  642. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  643. /* TLS PSK (RFC 4279): */
  644. case TLS_PSK_WITH_RC4_128_SHA:
  645. return "TLS_PSK_WITH_RC4_128_SHA";
  646. break;
  647. case TLS_PSK_WITH_3DES_EDE_CBC_SHA:
  648. return "TLS_PSK_WITH_3DES_EDE_CBC_SHA";
  649. break;
  650. case TLS_PSK_WITH_AES_128_CBC_SHA:
  651. return "TLS_PSK_WITH_AES_128_CBC_SHA";
  652. break;
  653. case TLS_PSK_WITH_AES_256_CBC_SHA:
  654. return "TLS_PSK_WITH_AES_256_CBC_SHA";
  655. break;
  656. case TLS_DHE_PSK_WITH_RC4_128_SHA:
  657. return "TLS_DHE_PSK_WITH_RC4_128_SHA";
  658. break;
  659. case TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA:
  660. return "TLS_DHE_PSK_WITH_3DES_EDE_CBC_SHA";
  661. break;
  662. case TLS_DHE_PSK_WITH_AES_128_CBC_SHA:
  663. return "TLS_DHE_PSK_WITH_AES_128_CBC_SHA";
  664. break;
  665. case TLS_DHE_PSK_WITH_AES_256_CBC_SHA:
  666. return "TLS_DHE_PSK_WITH_AES_256_CBC_SHA";
  667. break;
  668. case TLS_RSA_PSK_WITH_RC4_128_SHA:
  669. return "TLS_RSA_PSK_WITH_RC4_128_SHA";
  670. break;
  671. case TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA:
  672. return "TLS_RSA_PSK_WITH_3DES_EDE_CBC_SHA";
  673. break;
  674. case TLS_RSA_PSK_WITH_AES_128_CBC_SHA:
  675. return "TLS_RSA_PSK_WITH_AES_128_CBC_SHA";
  676. break;
  677. case TLS_RSA_PSK_WITH_AES_256_CBC_SHA:
  678. return "TLS_RSA_PSK_WITH_AES_256_CBC_SHA";
  679. break;
  680. /* More TLS PSK (RFC 4785): */
  681. case TLS_PSK_WITH_NULL_SHA:
  682. return "TLS_PSK_WITH_NULL_SHA";
  683. break;
  684. case TLS_DHE_PSK_WITH_NULL_SHA:
  685. return "TLS_DHE_PSK_WITH_NULL_SHA";
  686. break;
  687. case TLS_RSA_PSK_WITH_NULL_SHA:
  688. return "TLS_RSA_PSK_WITH_NULL_SHA";
  689. break;
  690. /* Even more TLS PSK (RFC 5487): */
  691. case TLS_PSK_WITH_AES_128_GCM_SHA256:
  692. return "TLS_PSK_WITH_AES_128_GCM_SHA256";
  693. break;
  694. case TLS_PSK_WITH_AES_256_GCM_SHA384:
  695. return "TLS_PSK_WITH_AES_256_GCM_SHA384";
  696. break;
  697. case TLS_DHE_PSK_WITH_AES_128_GCM_SHA256:
  698. return "TLS_DHE_PSK_WITH_AES_128_GCM_SHA256";
  699. break;
  700. case TLS_DHE_PSK_WITH_AES_256_GCM_SHA384:
  701. return "TLS_DHE_PSK_WITH_AES_256_GCM_SHA384";
  702. break;
  703. case TLS_RSA_PSK_WITH_AES_128_GCM_SHA256:
  704. return "TLS_RSA_PSK_WITH_AES_128_GCM_SHA256";
  705. break;
  706. case TLS_RSA_PSK_WITH_AES_256_GCM_SHA384:
  707. return "TLS_PSK_WITH_AES_256_GCM_SHA384";
  708. break;
  709. case TLS_PSK_WITH_AES_128_CBC_SHA256:
  710. return "TLS_PSK_WITH_AES_128_CBC_SHA256";
  711. break;
  712. case TLS_PSK_WITH_AES_256_CBC_SHA384:
  713. return "TLS_PSK_WITH_AES_256_CBC_SHA384";
  714. break;
  715. case TLS_PSK_WITH_NULL_SHA256:
  716. return "TLS_PSK_WITH_NULL_SHA256";
  717. break;
  718. case TLS_PSK_WITH_NULL_SHA384:
  719. return "TLS_PSK_WITH_NULL_SHA384";
  720. break;
  721. case TLS_DHE_PSK_WITH_AES_128_CBC_SHA256:
  722. return "TLS_DHE_PSK_WITH_AES_128_CBC_SHA256";
  723. break;
  724. case TLS_DHE_PSK_WITH_AES_256_CBC_SHA384:
  725. return "TLS_DHE_PSK_WITH_AES_256_CBC_SHA384";
  726. break;
  727. case TLS_DHE_PSK_WITH_NULL_SHA256:
  728. return "TLS_DHE_PSK_WITH_NULL_SHA256";
  729. break;
  730. case TLS_DHE_PSK_WITH_NULL_SHA384:
  731. return "TLS_RSA_PSK_WITH_NULL_SHA384";
  732. break;
  733. case TLS_RSA_PSK_WITH_AES_128_CBC_SHA256:
  734. return "TLS_RSA_PSK_WITH_AES_128_CBC_SHA256";
  735. break;
  736. case TLS_RSA_PSK_WITH_AES_256_CBC_SHA384:
  737. return "TLS_RSA_PSK_WITH_AES_256_CBC_SHA384";
  738. break;
  739. case TLS_RSA_PSK_WITH_NULL_SHA256:
  740. return "TLS_RSA_PSK_WITH_NULL_SHA256";
  741. break;
  742. case TLS_RSA_PSK_WITH_NULL_SHA384:
  743. return "TLS_RSA_PSK_WITH_NULL_SHA384";
  744. break;
  745. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  746. }
  747. return "TLS_NULL_WITH_NULL_NULL";
  748. }
  749. #if CURL_BUILD_MAC
  750. CF_INLINE void GetDarwinVersionNumber(int *major, int *minor)
  751. {
  752. int mib[2];
  753. char *os_version;
  754. size_t os_version_len;
  755. char *os_version_major, *os_version_minor/*, *os_version_point*/;
  756. /* Get the Darwin kernel version from the kernel using sysctl(): */
  757. mib[0] = CTL_KERN;
  758. mib[1] = KERN_OSRELEASE;
  759. if(sysctl(mib, 2, NULL, &os_version_len, NULL, 0) == -1)
  760. return;
  761. os_version = malloc(os_version_len*sizeof(char));
  762. if(!os_version)
  763. return;
  764. if(sysctl(mib, 2, os_version, &os_version_len, NULL, 0) == -1) {
  765. free(os_version);
  766. return;
  767. }
  768. /* Parse the version: */
  769. os_version_major = strtok(os_version, ".");
  770. os_version_minor = strtok(NULL, ".");
  771. /*os_version_point = strtok(NULL, ".");*/
  772. *major = atoi(os_version_major);
  773. *minor = atoi(os_version_minor);
  774. free(os_version);
  775. }
  776. #endif /* CURL_BUILD_MAC */
  777. /* Apple provides a myriad of ways of getting information about a certificate
  778. into a string. Some aren't available under iOS or newer cats. So here's
  779. a unified function for getting a string describing the certificate that
  780. ought to work in all cats starting with Leopard. */
  781. CF_INLINE CFStringRef CopyCertSubject(SecCertificateRef cert)
  782. {
  783. CFStringRef server_cert_summary = CFSTR("(null)");
  784. #if CURL_BUILD_IOS
  785. /* iOS: There's only one way to do this. */
  786. server_cert_summary = SecCertificateCopySubjectSummary(cert);
  787. #else
  788. #if CURL_BUILD_MAC_10_7
  789. /* Lion & later: Get the long description if we can. */
  790. if(SecCertificateCopyLongDescription != NULL)
  791. server_cert_summary =
  792. SecCertificateCopyLongDescription(NULL, cert, NULL);
  793. else
  794. #endif /* CURL_BUILD_MAC_10_7 */
  795. #if CURL_BUILD_MAC_10_6
  796. /* Snow Leopard: Get the certificate summary. */
  797. if(SecCertificateCopySubjectSummary != NULL)
  798. server_cert_summary = SecCertificateCopySubjectSummary(cert);
  799. else
  800. #endif /* CURL_BUILD_MAC_10_6 */
  801. /* Leopard is as far back as we go... */
  802. (void)SecCertificateCopyCommonName(cert, &server_cert_summary);
  803. #endif /* CURL_BUILD_IOS */
  804. return server_cert_summary;
  805. }
  806. #if CURL_SUPPORT_MAC_10_6
  807. /* The SecKeychainSearch API was deprecated in Lion, and using it will raise
  808. deprecation warnings, so let's not compile this unless it's necessary: */
  809. static OSStatus CopyIdentityWithLabelOldSchool(char *label,
  810. SecIdentityRef *out_c_a_k)
  811. {
  812. OSStatus status = errSecItemNotFound;
  813. SecKeychainAttributeList attr_list;
  814. SecKeychainAttribute attr;
  815. SecKeychainSearchRef search = NULL;
  816. SecCertificateRef cert = NULL;
  817. /* Set up the attribute list: */
  818. attr_list.count = 1L;
  819. attr_list.attr = &attr;
  820. /* Set up our lone search criterion: */
  821. attr.tag = kSecLabelItemAttr;
  822. attr.data = label;
  823. attr.length = (UInt32)strlen(label);
  824. /* Start searching: */
  825. status = SecKeychainSearchCreateFromAttributes(NULL,
  826. kSecCertificateItemClass,
  827. &attr_list,
  828. &search);
  829. if(status == noErr) {
  830. status = SecKeychainSearchCopyNext(search,
  831. (SecKeychainItemRef *)&cert);
  832. if(status == noErr && cert) {
  833. /* If we found a certificate, does it have a private key? */
  834. status = SecIdentityCreateWithCertificate(NULL, cert, out_c_a_k);
  835. CFRelease(cert);
  836. }
  837. }
  838. if(search)
  839. CFRelease(search);
  840. return status;
  841. }
  842. #endif /* CURL_SUPPORT_MAC_10_6 */
  843. static OSStatus CopyIdentityWithLabel(char *label,
  844. SecIdentityRef *out_cert_and_key)
  845. {
  846. OSStatus status = errSecItemNotFound;
  847. #if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS
  848. /* SecItemCopyMatching() was introduced in iOS and Snow Leopard.
  849. kSecClassIdentity was introduced in Lion. If both exist, let's use them
  850. to find the certificate. */
  851. if(SecItemCopyMatching != NULL && kSecClassIdentity != NULL) {
  852. CFTypeRef keys[4];
  853. CFTypeRef values[4];
  854. CFDictionaryRef query_dict;
  855. CFStringRef label_cf = CFStringCreateWithCString(NULL, label,
  856. kCFStringEncodingUTF8);
  857. /* Set up our search criteria and expected results: */
  858. values[0] = kSecClassIdentity; /* we want a certificate and a key */
  859. keys[0] = kSecClass;
  860. values[1] = kCFBooleanTrue; /* we want a reference */
  861. keys[1] = kSecReturnRef;
  862. values[2] = kSecMatchLimitOne; /* one is enough, thanks */
  863. keys[2] = kSecMatchLimit;
  864. /* identity searches need a SecPolicyRef in order to work */
  865. values[3] = SecPolicyCreateSSL(false, label_cf);
  866. keys[3] = kSecMatchPolicy;
  867. query_dict = CFDictionaryCreate(NULL, (const void **)keys,
  868. (const void **)values, 4L,
  869. &kCFCopyStringDictionaryKeyCallBacks,
  870. &kCFTypeDictionaryValueCallBacks);
  871. CFRelease(values[3]);
  872. CFRelease(label_cf);
  873. /* Do we have a match? */
  874. status = SecItemCopyMatching(query_dict, (CFTypeRef *)out_cert_and_key);
  875. CFRelease(query_dict);
  876. }
  877. else {
  878. #if CURL_SUPPORT_MAC_10_6
  879. /* On Leopard and Snow Leopard, fall back to SecKeychainSearch. */
  880. status = CopyIdentityWithLabelOldSchool(label, out_cert_and_key);
  881. #endif /* CURL_SUPPORT_MAC_10_7 */
  882. }
  883. #elif CURL_SUPPORT_MAC_10_6
  884. /* For developers building on older cats, we have no choice but to fall back
  885. to SecKeychainSearch. */
  886. status = CopyIdentityWithLabelOldSchool(label, out_cert_and_key);
  887. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  888. return status;
  889. }
  890. static OSStatus CopyIdentityFromPKCS12File(const char *cPath,
  891. const char *cPassword,
  892. SecIdentityRef *out_cert_and_key)
  893. {
  894. OSStatus status = errSecItemNotFound;
  895. CFURLRef pkcs_url = CFURLCreateFromFileSystemRepresentation(NULL,
  896. (const UInt8 *)cPath, strlen(cPath), false);
  897. CFStringRef password = cPassword ? CFStringCreateWithCString(NULL,
  898. cPassword, kCFStringEncodingUTF8) : NULL;
  899. CFDataRef pkcs_data = NULL;
  900. /* We can import P12 files on iOS or OS X 10.7 or later: */
  901. /* These constants are documented as having first appeared in 10.6 but they
  902. raise linker errors when used on that cat for some reason. */
  903. #if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS
  904. if(CFURLCreateDataAndPropertiesFromResource(NULL, pkcs_url, &pkcs_data,
  905. NULL, NULL, &status)) {
  906. const void *cKeys[] = {kSecImportExportPassphrase};
  907. const void *cValues[] = {password};
  908. CFDictionaryRef options = CFDictionaryCreate(NULL, cKeys, cValues,
  909. password ? 1L : 0L, NULL, NULL);
  910. CFArrayRef items = NULL;
  911. /* Here we go: */
  912. status = SecPKCS12Import(pkcs_data, options, &items);
  913. if(status == noErr && items && CFArrayGetCount(items)) {
  914. CFDictionaryRef identity_and_trust = CFArrayGetValueAtIndex(items, 0L);
  915. const void *temp_identity = CFDictionaryGetValue(identity_and_trust,
  916. kSecImportItemIdentity);
  917. /* Retain the identity; we don't care about any other data... */
  918. CFRetain(temp_identity);
  919. *out_cert_and_key = (SecIdentityRef)temp_identity;
  920. }
  921. if(items)
  922. CFRelease(items);
  923. CFRelease(options);
  924. CFRelease(pkcs_data);
  925. }
  926. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  927. if(password)
  928. CFRelease(password);
  929. CFRelease(pkcs_url);
  930. return status;
  931. }
  932. /* This code was borrowed from nss.c, with some modifications:
  933. * Determine whether the nickname passed in is a filename that needs to
  934. * be loaded as a PEM or a regular NSS nickname.
  935. *
  936. * returns 1 for a file
  937. * returns 0 for not a file
  938. */
  939. CF_INLINE bool is_file(const char *filename)
  940. {
  941. struct_stat st;
  942. if(filename == NULL)
  943. return false;
  944. if(stat(filename, &st) == 0)
  945. return S_ISREG(st.st_mode);
  946. return false;
  947. }
  948. static CURLcode darwinssl_connect_step1(struct connectdata *conn,
  949. int sockindex)
  950. {
  951. struct SessionHandle *data = conn->data;
  952. curl_socket_t sockfd = conn->sock[sockindex];
  953. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  954. #ifdef ENABLE_IPV6
  955. struct in6_addr addr;
  956. #else
  957. struct in_addr addr;
  958. #endif /* ENABLE_IPV6 */
  959. size_t all_ciphers_count = 0UL, allowed_ciphers_count = 0UL, i;
  960. SSLCipherSuite *all_ciphers = NULL, *allowed_ciphers = NULL;
  961. char *ssl_sessionid;
  962. size_t ssl_sessionid_len;
  963. OSStatus err = noErr;
  964. #if CURL_BUILD_MAC
  965. int darwinver_maj = 0, darwinver_min = 0;
  966. GetDarwinVersionNumber(&darwinver_maj, &darwinver_min);
  967. #endif /* CURL_BUILD_MAC */
  968. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  969. if(SSLCreateContext != NULL) { /* use the newer API if avaialble */
  970. if(connssl->ssl_ctx)
  971. CFRelease(connssl->ssl_ctx);
  972. connssl->ssl_ctx = SSLCreateContext(NULL, kSSLClientSide, kSSLStreamType);
  973. if(!connssl->ssl_ctx) {
  974. failf(data, "SSL: couldn't create a context!");
  975. return CURLE_OUT_OF_MEMORY;
  976. }
  977. }
  978. else {
  979. /* The old ST API does not exist under iOS, so don't compile it: */
  980. #if CURL_SUPPORT_MAC_10_8
  981. if(connssl->ssl_ctx)
  982. (void)SSLDisposeContext(connssl->ssl_ctx);
  983. err = SSLNewContext(false, &(connssl->ssl_ctx));
  984. if(err != noErr) {
  985. failf(data, "SSL: couldn't create a context: OSStatus %d", err);
  986. return CURLE_OUT_OF_MEMORY;
  987. }
  988. #endif /* CURL_SUPPORT_MAC_10_8 */
  989. }
  990. #else
  991. if(connssl->ssl_ctx)
  992. (void)SSLDisposeContext(connssl->ssl_ctx);
  993. err = SSLNewContext(false, &(connssl->ssl_ctx));
  994. if(err != noErr) {
  995. failf(data, "SSL: couldn't create a context: OSStatus %d", err);
  996. return CURLE_OUT_OF_MEMORY;
  997. }
  998. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  999. connssl->ssl_write_buffered_length = 0UL; /* reset buffered write length */
  1000. /* check to see if we've been told to use an explicit SSL/TLS version */
  1001. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1002. if(SSLSetProtocolVersionMax != NULL) {
  1003. switch(data->set.ssl.version) {
  1004. case CURL_SSLVERSION_DEFAULT: default:
  1005. (void)SSLSetProtocolVersionMin(connssl->ssl_ctx, kSSLProtocol3);
  1006. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kTLSProtocol12);
  1007. break;
  1008. case CURL_SSLVERSION_TLSv1:
  1009. (void)SSLSetProtocolVersionMin(connssl->ssl_ctx, kTLSProtocol1);
  1010. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kTLSProtocol12);
  1011. break;
  1012. case CURL_SSLVERSION_TLSv1_0:
  1013. (void)SSLSetProtocolVersionMin(connssl->ssl_ctx, kTLSProtocol1);
  1014. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kTLSProtocol1);
  1015. break;
  1016. case CURL_SSLVERSION_TLSv1_1:
  1017. (void)SSLSetProtocolVersionMin(connssl->ssl_ctx, kTLSProtocol11);
  1018. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kTLSProtocol11);
  1019. break;
  1020. case CURL_SSLVERSION_TLSv1_2:
  1021. (void)SSLSetProtocolVersionMin(connssl->ssl_ctx, kTLSProtocol12);
  1022. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kTLSProtocol12);
  1023. break;
  1024. case CURL_SSLVERSION_SSLv3:
  1025. (void)SSLSetProtocolVersionMin(connssl->ssl_ctx, kSSLProtocol3);
  1026. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kSSLProtocol3);
  1027. break;
  1028. case CURL_SSLVERSION_SSLv2:
  1029. err = SSLSetProtocolVersionMin(connssl->ssl_ctx, kSSLProtocol2);
  1030. if(err != noErr) {
  1031. failf(data, "Your version of the OS does not support SSLv2");
  1032. return CURLE_SSL_CONNECT_ERROR;
  1033. }
  1034. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kSSLProtocol2);
  1035. }
  1036. }
  1037. else {
  1038. #if CURL_SUPPORT_MAC_10_8
  1039. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1040. kSSLProtocolAll,
  1041. false);
  1042. switch (data->set.ssl.version) {
  1043. case CURL_SSLVERSION_DEFAULT: default:
  1044. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1045. kSSLProtocol3,
  1046. true);
  1047. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1048. kTLSProtocol1,
  1049. true);
  1050. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1051. kTLSProtocol11,
  1052. true);
  1053. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1054. kTLSProtocol12,
  1055. true);
  1056. break;
  1057. case CURL_SSLVERSION_TLSv1:
  1058. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1059. kTLSProtocol1,
  1060. true);
  1061. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1062. kTLSProtocol11,
  1063. true);
  1064. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1065. kTLSProtocol12,
  1066. true);
  1067. break;
  1068. case CURL_SSLVERSION_TLSv1_0:
  1069. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1070. kTLSProtocol1,
  1071. true);
  1072. break;
  1073. case CURL_SSLVERSION_TLSv1_1:
  1074. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1075. kTLSProtocol11,
  1076. true);
  1077. break;
  1078. case CURL_SSLVERSION_TLSv1_2:
  1079. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1080. kTLSProtocol12,
  1081. true);
  1082. break;
  1083. case CURL_SSLVERSION_SSLv3:
  1084. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1085. kSSLProtocol3,
  1086. true);
  1087. break;
  1088. case CURL_SSLVERSION_SSLv2:
  1089. err = SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1090. kSSLProtocol2,
  1091. true);
  1092. if(err != noErr) {
  1093. failf(data, "Your version of the OS does not support SSLv2");
  1094. return CURLE_SSL_CONNECT_ERROR;
  1095. }
  1096. break;
  1097. }
  1098. #endif /* CURL_SUPPORT_MAC_10_8 */
  1099. }
  1100. #else
  1101. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx, kSSLProtocolAll, false);
  1102. switch(data->set.ssl.version) {
  1103. default:
  1104. case CURL_SSLVERSION_DEFAULT:
  1105. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1106. kSSLProtocol3,
  1107. true);
  1108. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1109. kTLSProtocol1,
  1110. true);
  1111. break;
  1112. case CURL_SSLVERSION_TLSv1:
  1113. case CURL_SSLVERSION_TLSv1_0:
  1114. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1115. kTLSProtocol1,
  1116. true);
  1117. break;
  1118. case CURL_SSLVERSION_TLSv1_1:
  1119. failf(data, "Your version of the OS does not support TLSv1.1");
  1120. return CURLE_SSL_CONNECT_ERROR;
  1121. case CURL_SSLVERSION_TLSv1_2:
  1122. failf(data, "Your version of the OS does not support TLSv1.2");
  1123. return CURLE_SSL_CONNECT_ERROR;
  1124. case CURL_SSLVERSION_SSLv2:
  1125. err = SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1126. kSSLProtocol2,
  1127. true);
  1128. if(err != noErr) {
  1129. failf(data, "Your version of the OS does not support SSLv2");
  1130. return CURLE_SSL_CONNECT_ERROR;
  1131. }
  1132. break;
  1133. case CURL_SSLVERSION_SSLv3:
  1134. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  1135. kSSLProtocol3,
  1136. true);
  1137. break;
  1138. }
  1139. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  1140. if(data->set.str[STRING_KEY]) {
  1141. infof(data, "WARNING: SSL: CURLOPT_SSLKEY is ignored by Secure "
  1142. "Transport. The private key must be in the Keychain.\n");
  1143. }
  1144. if(data->set.str[STRING_CERT]) {
  1145. SecIdentityRef cert_and_key = NULL;
  1146. bool is_cert_file = is_file(data->set.str[STRING_CERT]);
  1147. /* User wants to authenticate with a client cert. Look for it:
  1148. If we detect that this is a file on disk, then let's load it.
  1149. Otherwise, assume that the user wants to use an identity loaded
  1150. from the Keychain. */
  1151. if(is_cert_file) {
  1152. if(!data->set.str[STRING_CERT_TYPE])
  1153. infof(data, "WARNING: SSL: Certificate type not set, assuming "
  1154. "PKCS#12 format.\n");
  1155. else if(strncmp(data->set.str[STRING_CERT_TYPE], "P12",
  1156. strlen(data->set.str[STRING_CERT_TYPE])) != 0)
  1157. infof(data, "WARNING: SSL: The Security framework only supports "
  1158. "loading identities that are in PKCS#12 format.\n");
  1159. err = CopyIdentityFromPKCS12File(data->set.str[STRING_CERT],
  1160. data->set.str[STRING_KEY_PASSWD], &cert_and_key);
  1161. }
  1162. else
  1163. err = CopyIdentityWithLabel(data->set.str[STRING_CERT], &cert_and_key);
  1164. if(err == noErr) {
  1165. SecCertificateRef cert = NULL;
  1166. CFTypeRef certs_c[1];
  1167. CFArrayRef certs;
  1168. /* If we found one, print it out: */
  1169. err = SecIdentityCopyCertificate(cert_and_key, &cert);
  1170. if(err == noErr) {
  1171. CFStringRef cert_summary = CopyCertSubject(cert);
  1172. char cert_summary_c[128];
  1173. if(cert_summary) {
  1174. memset(cert_summary_c, 0, 128);
  1175. if(CFStringGetCString(cert_summary,
  1176. cert_summary_c,
  1177. 128,
  1178. kCFStringEncodingUTF8)) {
  1179. infof(data, "Client certificate: %s\n", cert_summary_c);
  1180. }
  1181. CFRelease(cert_summary);
  1182. CFRelease(cert);
  1183. }
  1184. }
  1185. certs_c[0] = cert_and_key;
  1186. certs = CFArrayCreate(NULL, (const void **)certs_c, 1L,
  1187. &kCFTypeArrayCallBacks);
  1188. err = SSLSetCertificate(connssl->ssl_ctx, certs);
  1189. if(certs)
  1190. CFRelease(certs);
  1191. if(err != noErr) {
  1192. failf(data, "SSL: SSLSetCertificate() failed: OSStatus %d", err);
  1193. return CURLE_SSL_CERTPROBLEM;
  1194. }
  1195. CFRelease(cert_and_key);
  1196. }
  1197. else {
  1198. switch(err) {
  1199. case errSecAuthFailed: case -25264: /* errSecPkcs12VerifyFailure */
  1200. failf(data, "SSL: Incorrect password for the certificate \"%s\" "
  1201. "and its private key.", data->set.str[STRING_CERT]);
  1202. break;
  1203. case -26275: /* errSecDecode */ case -25257: /* errSecUnknownFormat */
  1204. failf(data, "SSL: Couldn't make sense of the data in the "
  1205. "certificate \"%s\" and its private key.",
  1206. data->set.str[STRING_CERT]);
  1207. break;
  1208. case -25260: /* errSecPassphraseRequired */
  1209. failf(data, "SSL The certificate \"%s\" requires a password.",
  1210. data->set.str[STRING_CERT]);
  1211. break;
  1212. case errSecItemNotFound:
  1213. failf(data, "SSL: Can't find the certificate \"%s\" and its private "
  1214. "key in the Keychain.", data->set.str[STRING_CERT]);
  1215. break;
  1216. default:
  1217. failf(data, "SSL: Can't load the certificate \"%s\" and its private "
  1218. "key: OSStatus %d", data->set.str[STRING_CERT], err);
  1219. break;
  1220. }
  1221. return CURLE_SSL_CERTPROBLEM;
  1222. }
  1223. }
  1224. /* SSL always tries to verify the peer, this only says whether it should
  1225. * fail to connect if the verification fails, or if it should continue
  1226. * anyway. In the latter case the result of the verification is checked with
  1227. * SSL_get_verify_result() below. */
  1228. #if CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS
  1229. /* Snow Leopard introduced the SSLSetSessionOption() function, but due to
  1230. a library bug with the way the kSSLSessionOptionBreakOnServerAuth flag
  1231. works, it doesn't work as expected under Snow Leopard or Lion.
  1232. So we need to call SSLSetEnableCertVerify() on those older cats in order
  1233. to disable certificate validation if the user turned that off.
  1234. (SecureTransport will always validate the certificate chain by
  1235. default.) */
  1236. /* (Note: Darwin 12.x.x is Mountain Lion.) */
  1237. #if CURL_BUILD_MAC
  1238. if(SSLSetSessionOption != NULL && darwinver_maj >= 12) {
  1239. #else
  1240. if(SSLSetSessionOption != NULL) {
  1241. #endif /* CURL_BUILD_MAC */
  1242. bool break_on_auth = !data->set.ssl.verifypeer ||
  1243. data->set.str[STRING_SSL_CAFILE];
  1244. err = SSLSetSessionOption(connssl->ssl_ctx,
  1245. kSSLSessionOptionBreakOnServerAuth,
  1246. break_on_auth);
  1247. if(err != noErr) {
  1248. failf(data, "SSL: SSLSetSessionOption() failed: OSStatus %d", err);
  1249. return CURLE_SSL_CONNECT_ERROR;
  1250. }
  1251. }
  1252. else {
  1253. #if CURL_SUPPORT_MAC_10_8
  1254. err = SSLSetEnableCertVerify(connssl->ssl_ctx,
  1255. data->set.ssl.verifypeer?true:false);
  1256. if(err != noErr) {
  1257. failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err);
  1258. return CURLE_SSL_CONNECT_ERROR;
  1259. }
  1260. #endif /* CURL_SUPPORT_MAC_10_8 */
  1261. }
  1262. #else
  1263. err = SSLSetEnableCertVerify(connssl->ssl_ctx,
  1264. data->set.ssl.verifypeer?true:false);
  1265. if(err != noErr) {
  1266. failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err);
  1267. return CURLE_SSL_CONNECT_ERROR;
  1268. }
  1269. #endif /* CURL_BUILD_MAC_10_6 || CURL_BUILD_IOS */
  1270. if(data->set.str[STRING_SSL_CAFILE]) {
  1271. bool is_cert_file = is_file(data->set.str[STRING_SSL_CAFILE]);
  1272. if(!is_cert_file) {
  1273. failf(data, "SSL: can't load CA certificate file %s",
  1274. data->set.str[STRING_SSL_CAFILE]);
  1275. return CURLE_SSL_CACERT_BADFILE;
  1276. }
  1277. if(!data->set.ssl.verifypeer) {
  1278. failf(data, "SSL: CA certificate set, but certificate verification "
  1279. "is disabled");
  1280. return CURLE_SSL_CONNECT_ERROR;
  1281. }
  1282. }
  1283. /* Configure hostname check. SNI is used if available.
  1284. * Both hostname check and SNI require SSLSetPeerDomainName().
  1285. * Also: the verifyhost setting influences SNI usage */
  1286. if(data->set.ssl.verifyhost) {
  1287. err = SSLSetPeerDomainName(connssl->ssl_ctx, conn->host.name,
  1288. strlen(conn->host.name));
  1289. if(err != noErr) {
  1290. infof(data, "WARNING: SSL: SSLSetPeerDomainName() failed: OSStatus %d\n",
  1291. err);
  1292. }
  1293. if((Curl_inet_pton(AF_INET, conn->host.name, &addr))
  1294. #ifdef ENABLE_IPV6
  1295. || (Curl_inet_pton(AF_INET6, conn->host.name, &addr))
  1296. #endif
  1297. ) {
  1298. infof(data, "WARNING: using IP address, SNI is being disabled by "
  1299. "the OS.\n");
  1300. }
  1301. }
  1302. /* Disable cipher suites that ST supports but are not safe. These ciphers
  1303. are unlikely to be used in any case since ST gives other ciphers a much
  1304. higher priority, but it's probably better that we not connect at all than
  1305. to give the user a false sense of security if the server only supports
  1306. insecure ciphers. (Note: We don't care about SSLv2-only ciphers.) */
  1307. (void)SSLGetNumberSupportedCiphers(connssl->ssl_ctx, &all_ciphers_count);
  1308. all_ciphers = malloc(all_ciphers_count*sizeof(SSLCipherSuite));
  1309. allowed_ciphers = malloc(all_ciphers_count*sizeof(SSLCipherSuite));
  1310. if(all_ciphers && allowed_ciphers &&
  1311. SSLGetSupportedCiphers(connssl->ssl_ctx, all_ciphers,
  1312. &all_ciphers_count) == noErr) {
  1313. for(i = 0UL ; i < all_ciphers_count ; i++) {
  1314. #if CURL_BUILD_MAC
  1315. /* There's a known bug in early versions of Mountain Lion where ST's ECC
  1316. ciphers (cipher suite 0xC001 through 0xC032) simply do not work.
  1317. Work around the problem here by disabling those ciphers if we are
  1318. running in an affected version of OS X. */
  1319. if(darwinver_maj == 12 && darwinver_min <= 3 &&
  1320. all_ciphers[i] >= 0xC001 && all_ciphers[i] <= 0xC032) {
  1321. continue;
  1322. }
  1323. #endif /* CURL_BUILD_MAC */
  1324. switch(all_ciphers[i]) {
  1325. /* Disable NULL ciphersuites: */
  1326. case SSL_NULL_WITH_NULL_NULL:
  1327. case SSL_RSA_WITH_NULL_MD5:
  1328. case SSL_RSA_WITH_NULL_SHA:
  1329. case 0x003B: /* TLS_RSA_WITH_NULL_SHA256 */
  1330. case SSL_FORTEZZA_DMS_WITH_NULL_SHA:
  1331. case 0xC001: /* TLS_ECDH_ECDSA_WITH_NULL_SHA */
  1332. case 0xC006: /* TLS_ECDHE_ECDSA_WITH_NULL_SHA */
  1333. case 0xC00B: /* TLS_ECDH_RSA_WITH_NULL_SHA */
  1334. case 0xC010: /* TLS_ECDHE_RSA_WITH_NULL_SHA */
  1335. case 0x002C: /* TLS_PSK_WITH_NULL_SHA */
  1336. case 0x002D: /* TLS_DHE_PSK_WITH_NULL_SHA */
  1337. case 0x002E: /* TLS_RSA_PSK_WITH_NULL_SHA */
  1338. case 0x00B0: /* TLS_PSK_WITH_NULL_SHA256 */
  1339. case 0x00B1: /* TLS_PSK_WITH_NULL_SHA384 */
  1340. case 0x00B4: /* TLS_DHE_PSK_WITH_NULL_SHA256 */
  1341. case 0x00B5: /* TLS_DHE_PSK_WITH_NULL_SHA384 */
  1342. case 0x00B8: /* TLS_RSA_PSK_WITH_NULL_SHA256 */
  1343. case 0x00B9: /* TLS_RSA_PSK_WITH_NULL_SHA384 */
  1344. /* Disable anonymous ciphersuites: */
  1345. case SSL_DH_anon_EXPORT_WITH_RC4_40_MD5:
  1346. case SSL_DH_anon_WITH_RC4_128_MD5:
  1347. case SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA:
  1348. case SSL_DH_anon_WITH_DES_CBC_SHA:
  1349. case SSL_DH_anon_WITH_3DES_EDE_CBC_SHA:
  1350. case TLS_DH_anon_WITH_AES_128_CBC_SHA:
  1351. case TLS_DH_anon_WITH_AES_256_CBC_SHA:
  1352. case 0xC015: /* TLS_ECDH_anon_WITH_NULL_SHA */
  1353. case 0xC016: /* TLS_ECDH_anon_WITH_RC4_128_SHA */
  1354. case 0xC017: /* TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA */
  1355. case 0xC018: /* TLS_ECDH_anon_WITH_AES_128_CBC_SHA */
  1356. case 0xC019: /* TLS_ECDH_anon_WITH_AES_256_CBC_SHA */
  1357. case 0x006C: /* TLS_DH_anon_WITH_AES_128_CBC_SHA256 */
  1358. case 0x006D: /* TLS_DH_anon_WITH_AES_256_CBC_SHA256 */
  1359. case 0x00A6: /* TLS_DH_anon_WITH_AES_128_GCM_SHA256 */
  1360. case 0x00A7: /* TLS_DH_anon_WITH_AES_256_GCM_SHA384 */
  1361. /* Disable weak key ciphersuites: */
  1362. case SSL_RSA_EXPORT_WITH_RC4_40_MD5:
  1363. case SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5:
  1364. case SSL_RSA_EXPORT_WITH_DES40_CBC_SHA:
  1365. case SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA:
  1366. case SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA:
  1367. case SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA:
  1368. case SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA:
  1369. case SSL_RSA_WITH_DES_CBC_SHA:
  1370. case SSL_DH_DSS_WITH_DES_CBC_SHA:
  1371. case SSL_DH_RSA_WITH_DES_CBC_SHA:
  1372. case SSL_DHE_DSS_WITH_DES_CBC_SHA:
  1373. case SSL_DHE_RSA_WITH_DES_CBC_SHA:
  1374. /* Disable IDEA: */
  1375. case SSL_RSA_WITH_IDEA_CBC_SHA:
  1376. case SSL_RSA_WITH_IDEA_CBC_MD5:
  1377. break;
  1378. default: /* enable everything else */
  1379. allowed_ciphers[allowed_ciphers_count++] = all_ciphers[i];
  1380. break;
  1381. }
  1382. }
  1383. err = SSLSetEnabledCiphers(connssl->ssl_ctx, allowed_ciphers,
  1384. allowed_ciphers_count);
  1385. if(err != noErr) {
  1386. failf(data, "SSL: SSLSetEnabledCiphers() failed: OSStatus %d", err);
  1387. return CURLE_SSL_CONNECT_ERROR;
  1388. }
  1389. }
  1390. else {
  1391. Curl_safefree(all_ciphers);
  1392. Curl_safefree(allowed_ciphers);
  1393. failf(data, "SSL: Failed to allocate memory for allowed ciphers");
  1394. return CURLE_OUT_OF_MEMORY;
  1395. }
  1396. Curl_safefree(all_ciphers);
  1397. Curl_safefree(allowed_ciphers);
  1398. #if CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7
  1399. /* We want to enable 1/n-1 when using a CBC cipher unless the user
  1400. specifically doesn't want us doing that: */
  1401. if(SSLSetSessionOption != NULL)
  1402. SSLSetSessionOption(connssl->ssl_ctx, kSSLSessionOptionSendOneByteRecord,
  1403. !data->set.ssl_enable_beast);
  1404. #endif /* CURL_BUILD_MAC_10_9 || CURL_BUILD_IOS_7 */
  1405. /* Check if there's a cached ID we can/should use here! */
  1406. if(!Curl_ssl_getsessionid(conn, (void **)&ssl_sessionid,
  1407. &ssl_sessionid_len)) {
  1408. /* we got a session id, use it! */
  1409. err = SSLSetPeerID(connssl->ssl_ctx, ssl_sessionid, ssl_sessionid_len);
  1410. if(err != noErr) {
  1411. failf(data, "SSL: SSLSetPeerID() failed: OSStatus %d", err);
  1412. return CURLE_SSL_CONNECT_ERROR;
  1413. }
  1414. /* Informational message */
  1415. infof(data, "SSL re-using session ID\n");
  1416. }
  1417. /* If there isn't one, then let's make one up! This has to be done prior
  1418. to starting the handshake. */
  1419. else {
  1420. CURLcode retcode;
  1421. ssl_sessionid = malloc(256*sizeof(char));
  1422. ssl_sessionid_len = snprintf(ssl_sessionid, 256, "curl:%s:%hu",
  1423. conn->host.name, conn->remote_port);
  1424. err = SSLSetPeerID(connssl->ssl_ctx, ssl_sessionid, ssl_sessionid_len);
  1425. if(err != noErr) {
  1426. failf(data, "SSL: SSLSetPeerID() failed: OSStatus %d", err);
  1427. return CURLE_SSL_CONNECT_ERROR;
  1428. }
  1429. retcode = Curl_ssl_addsessionid(conn, ssl_sessionid, ssl_sessionid_len);
  1430. if(retcode!= CURLE_OK) {
  1431. failf(data, "failed to store ssl session");
  1432. return retcode;
  1433. }
  1434. }
  1435. err = SSLSetIOFuncs(connssl->ssl_ctx, SocketRead, SocketWrite);
  1436. if(err != noErr) {
  1437. failf(data, "SSL: SSLSetIOFuncs() failed: OSStatus %d", err);
  1438. return CURLE_SSL_CONNECT_ERROR;
  1439. }
  1440. /* pass the raw socket into the SSL layers */
  1441. /* We need to store the FD in a constant memory address, because
  1442. * SSLSetConnection() will not copy that address. I've found that
  1443. * conn->sock[sockindex] may change on its own. */
  1444. connssl->ssl_sockfd = sockfd;
  1445. err = SSLSetConnection(connssl->ssl_ctx, connssl);
  1446. if(err != noErr) {
  1447. failf(data, "SSL: SSLSetConnection() failed: %d", err);
  1448. return CURLE_SSL_CONNECT_ERROR;
  1449. }
  1450. connssl->connecting_state = ssl_connect_2;
  1451. return CURLE_OK;
  1452. }
  1453. static int pem_to_der(const char *in, unsigned char **out, size_t *outlen)
  1454. {
  1455. char *sep, *start, *end;
  1456. size_t i, j, err;
  1457. size_t len;
  1458. unsigned char *b64;
  1459. /* Jump through the separators in the first line. */
  1460. sep = strstr(in, "-----");
  1461. if(sep == NULL)
  1462. return -1;
  1463. sep = strstr(sep + 1, "-----");
  1464. if(sep == NULL)
  1465. return -1;
  1466. start = sep + 5;
  1467. /* Find beginning of last line separator. */
  1468. end = strstr(start, "-----");
  1469. if(end == NULL)
  1470. return -1;
  1471. len = end - start;
  1472. *out = malloc(len);
  1473. if(!*out)
  1474. return -1;
  1475. b64 = malloc(len + 1);
  1476. if(!b64) {
  1477. free(*out);
  1478. return -1;
  1479. }
  1480. /* Create base64 string without linefeeds. */
  1481. for(i = 0, j = 0; i < len; i++) {
  1482. if(start[i] != '\r' && start[i] != '\n')
  1483. b64[j++] = start[i];
  1484. }
  1485. b64[j] = '\0';
  1486. err = Curl_base64_decode((const char *)b64, out, outlen);
  1487. free(b64);
  1488. if(err) {
  1489. free(*out);
  1490. return -1;
  1491. }
  1492. return 0;
  1493. }
  1494. static int read_cert(const char *file, unsigned char **out, size_t *outlen)
  1495. {
  1496. int fd;
  1497. ssize_t n, len = 0, cap = 512;
  1498. size_t derlen;
  1499. unsigned char buf[cap], *data, *der;
  1500. fd = open(file, 0);
  1501. if(fd < 0)
  1502. return -1;
  1503. data = malloc(cap);
  1504. if(!data) {
  1505. close(fd);
  1506. return -1;
  1507. }
  1508. for(;;) {
  1509. n = read(fd, buf, sizeof(buf));
  1510. if(n < 0) {
  1511. close(fd);
  1512. free(data);
  1513. return -1;
  1514. }
  1515. else if(n == 0) {
  1516. close(fd);
  1517. break;
  1518. }
  1519. if(len + n >= cap) {
  1520. cap *= 2;
  1521. data = realloc(data, cap);
  1522. if(!data) {
  1523. close(fd);
  1524. return -1;
  1525. }
  1526. }
  1527. memcpy(data + len, buf, n);
  1528. len += n;
  1529. }
  1530. data[len] = '\0';
  1531. /*
  1532. * Check if the certificate is in PEM format, and convert it to DER. If this
  1533. * fails, we assume the certificate is in DER format.
  1534. */
  1535. if(pem_to_der((const char *)data, &der, &derlen) == 0) {
  1536. free(data);
  1537. data = der;
  1538. len = derlen;
  1539. }
  1540. *out = data;
  1541. *outlen = len;
  1542. return 0;
  1543. }
  1544. static int sslerr_to_curlerr(struct SessionHandle *data, int err)
  1545. {
  1546. switch(err) {
  1547. case errSSLXCertChainInvalid:
  1548. failf(data, "SSL certificate problem: Invalid certificate chain");
  1549. return CURLE_SSL_CACERT;
  1550. case errSSLUnknownRootCert:
  1551. failf(data, "SSL certificate problem: Untrusted root certificate");
  1552. return CURLE_SSL_CACERT;
  1553. case errSSLNoRootCert:
  1554. failf(data, "SSL certificate problem: No root certificate");
  1555. return CURLE_SSL_CACERT;
  1556. case errSSLCertExpired:
  1557. failf(data, "SSL certificate problem: Certificate chain had an "
  1558. "expired certificate");
  1559. return CURLE_SSL_CACERT;
  1560. case errSSLBadCert:
  1561. failf(data, "SSL certificate problem: Couldn't understand the server "
  1562. "certificate format");
  1563. return CURLE_SSL_CONNECT_ERROR;
  1564. case errSSLHostNameMismatch:
  1565. failf(data, "SSL certificate peer hostname mismatch");
  1566. return CURLE_PEER_FAILED_VERIFICATION;
  1567. default:
  1568. failf(data, "SSL unexpected certificate error %d", err);
  1569. return CURLE_SSL_CACERT;
  1570. }
  1571. }
  1572. static int verify_cert(const char *cafile, struct SessionHandle *data,
  1573. SSLContextRef ctx)
  1574. {
  1575. unsigned char *certbuf;
  1576. size_t buflen;
  1577. if(read_cert(cafile, &certbuf, &buflen) < 0) {
  1578. failf(data, "SSL: failed to read or invalid CA certificate");
  1579. return CURLE_SSL_CACERT;
  1580. }
  1581. CFDataRef certdata = CFDataCreate(kCFAllocatorDefault, certbuf, buflen);
  1582. free(certbuf);
  1583. if(!certdata) {
  1584. failf(data, "SSL: failed to allocate array for CA certificate");
  1585. return CURLE_OUT_OF_MEMORY;
  1586. }
  1587. SecCertificateRef cacert = SecCertificateCreateWithData(kCFAllocatorDefault,
  1588. certdata);
  1589. CFRelease(certdata);
  1590. if(!cacert) {
  1591. failf(data, "SSL: failed to create SecCertificate from CA certificate");
  1592. return CURLE_SSL_CACERT;
  1593. }
  1594. SecTrustRef trust;
  1595. OSStatus ret = SSLCopyPeerTrust(ctx, &trust);
  1596. if(trust == NULL) {
  1597. failf(data, "SSL: error getting certificate chain");
  1598. return CURLE_OUT_OF_MEMORY;
  1599. }
  1600. else if(ret != noErr) {
  1601. return sslerr_to_curlerr(data, ret);
  1602. }
  1603. CFMutableArrayRef array = CFArrayCreateMutable(kCFAllocatorDefault, 0,
  1604. &kCFTypeArrayCallBacks);
  1605. CFArrayAppendValue(array, cacert);
  1606. CFRelease(cacert);
  1607. ret = SecTrustSetAnchorCertificates(trust, array);
  1608. if(ret != noErr) {
  1609. CFRelease(trust);
  1610. return sslerr_to_curlerr(data, ret);
  1611. }
  1612. SecTrustResultType trust_eval = 0;
  1613. ret = SecTrustEvaluate(trust, &trust_eval);
  1614. CFRelease(array);
  1615. CFRelease(trust);
  1616. if(ret != noErr) {
  1617. return sslerr_to_curlerr(data, ret);
  1618. }
  1619. switch (trust_eval) {
  1620. case kSecTrustResultUnspecified:
  1621. case kSecTrustResultProceed:
  1622. infof(data, "SSL: certificate verification succeeded (result: %d)",
  1623. trust_eval);
  1624. return CURLE_OK;
  1625. case kSecTrustResultRecoverableTrustFailure:
  1626. case kSecTrustResultDeny:
  1627. default:
  1628. failf(data, "SSL: certificate verification failed (result: %d)",
  1629. trust_eval);
  1630. return CURLE_PEER_FAILED_VERIFICATION;
  1631. }
  1632. }
  1633. static CURLcode
  1634. darwinssl_connect_step2(struct connectdata *conn, int sockindex)
  1635. {
  1636. struct SessionHandle *data = conn->data;
  1637. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1638. OSStatus err;
  1639. SSLCipherSuite cipher;
  1640. SSLProtocol protocol = 0;
  1641. DEBUGASSERT(ssl_connect_2 == connssl->connecting_state
  1642. || ssl_connect_2_reading == connssl->connecting_state
  1643. || ssl_connect_2_writing == connssl->connecting_state);
  1644. /* Here goes nothing: */
  1645. err = SSLHandshake(connssl->ssl_ctx);
  1646. if(err != noErr) {
  1647. switch (err) {
  1648. case errSSLWouldBlock: /* they're not done with us yet */
  1649. connssl->connecting_state = connssl->ssl_direction ?
  1650. ssl_connect_2_writing : ssl_connect_2_reading;
  1651. return CURLE_OK;
  1652. /* The below is errSSLServerAuthCompleted; it's not defined in
  1653. Leopard's headers */
  1654. case -9841:
  1655. if(data->set.str[STRING_SSL_CAFILE]) {
  1656. int res = verify_cert(data->set.str[STRING_SSL_CAFILE], data,
  1657. connssl->ssl_ctx);
  1658. if(res != CURLE_OK)
  1659. return res;
  1660. }
  1661. /* the documentation says we need to call SSLHandshake() again */
  1662. return darwinssl_connect_step2(conn, sockindex);
  1663. /* These are all certificate problems with the server: */
  1664. case errSSLXCertChainInvalid:
  1665. failf(data, "SSL certificate problem: Invalid certificate chain");
  1666. return CURLE_SSL_CACERT;
  1667. case errSSLUnknownRootCert:
  1668. failf(data, "SSL certificate problem: Untrusted root certificate");
  1669. return CURLE_SSL_CACERT;
  1670. case errSSLNoRootCert:
  1671. failf(data, "SSL certificate problem: No root certificate");
  1672. return CURLE_SSL_CACERT;
  1673. case errSSLCertExpired:
  1674. failf(data, "SSL certificate problem: Certificate chain had an "
  1675. "expired certificate");
  1676. return CURLE_SSL_CACERT;
  1677. case errSSLBadCert:
  1678. failf(data, "SSL certificate problem: Couldn't understand the server "
  1679. "certificate format");
  1680. return CURLE_SSL_CONNECT_ERROR;
  1681. /* These are all certificate problems with the client: */
  1682. case errSecAuthFailed:
  1683. failf(data, "SSL authentication failed");
  1684. return CURLE_SSL_CONNECT_ERROR;
  1685. case errSSLPeerHandshakeFail:
  1686. failf(data, "SSL peer handshake failed, the server most likely "
  1687. "requires a client certificate to connect");
  1688. return CURLE_SSL_CONNECT_ERROR;
  1689. case errSSLPeerUnknownCA:
  1690. failf(data, "SSL server rejected the client certificate due to "
  1691. "the certificate being signed by an unknown certificate "
  1692. "authority");
  1693. return CURLE_SSL_CONNECT_ERROR;
  1694. /* This error is raised if the server's cert didn't match the server's
  1695. host name: */
  1696. case errSSLHostNameMismatch:
  1697. failf(data, "SSL certificate peer verification failed, the "
  1698. "certificate did not match \"%s\"\n", conn->host.dispname);
  1699. return CURLE_PEER_FAILED_VERIFICATION;
  1700. /* Generic handshake errors: */
  1701. case errSSLConnectionRefused:
  1702. failf(data, "Server dropped the connection during the SSL handshake");
  1703. return CURLE_SSL_CONNECT_ERROR;
  1704. case errSSLClosedAbort:
  1705. failf(data, "Server aborted the SSL handshake");
  1706. return CURLE_SSL_CONNECT_ERROR;
  1707. case errSSLNegotiation:
  1708. failf(data, "Could not negotiate an SSL cipher suite with the server");
  1709. return CURLE_SSL_CONNECT_ERROR;
  1710. /* Sometimes paramErr happens with buggy ciphers: */
  1711. case paramErr: case errSSLInternal:
  1712. failf(data, "Internal SSL engine error encountered during the "
  1713. "SSL handshake");
  1714. return CURLE_SSL_CONNECT_ERROR;
  1715. case errSSLFatalAlert:
  1716. failf(data, "Fatal SSL engine error encountered during the SSL "
  1717. "handshake");
  1718. return CURLE_SSL_CONNECT_ERROR;
  1719. default:
  1720. failf(data, "Unknown SSL protocol error in connection to %s:%d",
  1721. conn->host.name, err);
  1722. return CURLE_SSL_CONNECT_ERROR;
  1723. }
  1724. }
  1725. else {
  1726. /* we have been connected fine, we're not waiting for anything else. */
  1727. connssl->connecting_state = ssl_connect_3;
  1728. /* Informational message */
  1729. (void)SSLGetNegotiatedCipher(connssl->ssl_ctx, &cipher);
  1730. (void)SSLGetNegotiatedProtocolVersion(connssl->ssl_ctx, &protocol);
  1731. switch (protocol) {
  1732. case kSSLProtocol2:
  1733. infof(data, "SSL 2.0 connection using %s\n",
  1734. SSLCipherNameForNumber(cipher));
  1735. break;
  1736. case kSSLProtocol3:
  1737. infof(data, "SSL 3.0 connection using %s\n",
  1738. SSLCipherNameForNumber(cipher));
  1739. break;
  1740. case kTLSProtocol1:
  1741. infof(data, "TLS 1.0 connection using %s\n",
  1742. TLSCipherNameForNumber(cipher));
  1743. break;
  1744. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1745. case kTLSProtocol11:
  1746. infof(data, "TLS 1.1 connection using %s\n",
  1747. TLSCipherNameForNumber(cipher));
  1748. break;
  1749. case kTLSProtocol12:
  1750. infof(data, "TLS 1.2 connection using %s\n",
  1751. TLSCipherNameForNumber(cipher));
  1752. break;
  1753. #endif
  1754. default:
  1755. infof(data, "Unknown protocol connection\n");
  1756. break;
  1757. }
  1758. return CURLE_OK;
  1759. }
  1760. }
  1761. static CURLcode
  1762. darwinssl_connect_step3(struct connectdata *conn,
  1763. int sockindex)
  1764. {
  1765. struct SessionHandle *data = conn->data;
  1766. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1767. CFStringRef server_cert_summary;
  1768. char server_cert_summary_c[128];
  1769. CFArrayRef server_certs = NULL;
  1770. SecCertificateRef server_cert;
  1771. OSStatus err;
  1772. CFIndex i, count;
  1773. SecTrustRef trust = NULL;
  1774. /* There is no step 3!
  1775. * Well, okay, if verbose mode is on, let's print the details of the
  1776. * server certificates. */
  1777. #if CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS
  1778. #if CURL_BUILD_IOS
  1779. #pragma unused(server_certs)
  1780. err = SSLCopyPeerTrust(connssl->ssl_ctx, &trust);
  1781. /* For some reason, SSLCopyPeerTrust() can return noErr and yet return
  1782. a null trust, so be on guard for that: */
  1783. if(err == noErr && trust) {
  1784. count = SecTrustGetCertificateCount(trust);
  1785. for(i = 0L ; i < count ; i++) {
  1786. server_cert = SecTrustGetCertificateAtIndex(trust, i);
  1787. server_cert_summary = CopyCertSubject(server_cert);
  1788. memset(server_cert_summary_c, 0, 128);
  1789. if(CFStringGetCString(server_cert_summary,
  1790. server_cert_summary_c,
  1791. 128,
  1792. kCFStringEncodingUTF8)) {
  1793. infof(data, "Server certificate: %s\n", server_cert_summary_c);
  1794. }
  1795. CFRelease(server_cert_summary);
  1796. }
  1797. CFRelease(trust);
  1798. }
  1799. #else
  1800. /* SSLCopyPeerCertificates() is deprecated as of Mountain Lion.
  1801. The function SecTrustGetCertificateAtIndex() is officially present
  1802. in Lion, but it is unfortunately also present in Snow Leopard as
  1803. private API and doesn't work as expected. So we have to look for
  1804. a different symbol to make sure this code is only executed under
  1805. Lion or later. */
  1806. if(SecTrustEvaluateAsync != NULL) {
  1807. #pragma unused(server_certs)
  1808. err = SSLCopyPeerTrust(connssl->ssl_ctx, &trust);
  1809. /* For some reason, SSLCopyPeerTrust() can return noErr and yet return
  1810. a null trust, so be on guard for that: */
  1811. if(err == noErr && trust) {
  1812. count = SecTrustGetCertificateCount(trust);
  1813. for(i = 0L ; i < count ; i++) {
  1814. server_cert = SecTrustGetCertificateAtIndex(trust, i);
  1815. server_cert_summary = CopyCertSubject(server_cert);
  1816. memset(server_cert_summary_c, 0, 128);
  1817. if(CFStringGetCString(server_cert_summary,
  1818. server_cert_summary_c,
  1819. 128,
  1820. kCFStringEncodingUTF8)) {
  1821. infof(data, "Server certificate: %s\n", server_cert_summary_c);
  1822. }
  1823. CFRelease(server_cert_summary);
  1824. }
  1825. CFRelease(trust);
  1826. }
  1827. }
  1828. else {
  1829. #if CURL_SUPPORT_MAC_10_8
  1830. err = SSLCopyPeerCertificates(connssl->ssl_ctx, &server_certs);
  1831. /* Just in case SSLCopyPeerCertificates() returns null too... */
  1832. if(err == noErr && server_certs) {
  1833. count = CFArrayGetCount(server_certs);
  1834. for(i = 0L ; i < count ; i++) {
  1835. server_cert = (SecCertificateRef)CFArrayGetValueAtIndex(server_certs,
  1836. i);
  1837. server_cert_summary = CopyCertSubject(server_cert);
  1838. memset(server_cert_summary_c, 0, 128);
  1839. if(CFStringGetCString(server_cert_summary,
  1840. server_cert_summary_c,
  1841. 128,
  1842. kCFStringEncodingUTF8)) {
  1843. infof(data, "Server certificate: %s\n", server_cert_summary_c);
  1844. }
  1845. CFRelease(server_cert_summary);
  1846. }
  1847. CFRelease(server_certs);
  1848. }
  1849. #endif /* CURL_SUPPORT_MAC_10_8 */
  1850. }
  1851. #endif /* CURL_BUILD_IOS */
  1852. #else
  1853. #pragma unused(trust)
  1854. err = SSLCopyPeerCertificates(connssl->ssl_ctx, &server_certs);
  1855. if(err == noErr) {
  1856. count = CFArrayGetCount(server_certs);
  1857. for(i = 0L ; i < count ; i++) {
  1858. server_cert = (SecCertificateRef)CFArrayGetValueAtIndex(server_certs, i);
  1859. server_cert_summary = CopyCertSubject(server_cert);
  1860. memset(server_cert_summary_c, 0, 128);
  1861. if(CFStringGetCString(server_cert_summary,
  1862. server_cert_summary_c,
  1863. 128,
  1864. kCFStringEncodingUTF8)) {
  1865. infof(data, "Server certificate: %s\n", server_cert_summary_c);
  1866. }
  1867. CFRelease(server_cert_summary);
  1868. }
  1869. CFRelease(server_certs);
  1870. }
  1871. #endif /* CURL_BUILD_MAC_10_7 || CURL_BUILD_IOS */
  1872. connssl->connecting_state = ssl_connect_done;
  1873. return CURLE_OK;
  1874. }
  1875. static Curl_recv darwinssl_recv;
  1876. static Curl_send darwinssl_send;
  1877. static CURLcode
  1878. darwinssl_connect_common(struct connectdata *conn,
  1879. int sockindex,
  1880. bool nonblocking,
  1881. bool *done)
  1882. {
  1883. CURLcode retcode;
  1884. struct SessionHandle *data = conn->data;
  1885. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1886. curl_socket_t sockfd = conn->sock[sockindex];
  1887. long timeout_ms;
  1888. int what;
  1889. /* check if the connection has already been established */
  1890. if(ssl_connection_complete == connssl->state) {
  1891. *done = TRUE;
  1892. return CURLE_OK;
  1893. }
  1894. if(ssl_connect_1==connssl->connecting_state) {
  1895. /* Find out how much more time we're allowed */
  1896. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1897. if(timeout_ms < 0) {
  1898. /* no need to continue if time already is up */
  1899. failf(data, "SSL connection timeout");
  1900. return CURLE_OPERATION_TIMEDOUT;
  1901. }
  1902. retcode = darwinssl_connect_step1(conn, sockindex);
  1903. if(retcode)
  1904. return retcode;
  1905. }
  1906. while(ssl_connect_2 == connssl->connecting_state ||
  1907. ssl_connect_2_reading == connssl->connecting_state ||
  1908. ssl_connect_2_writing == connssl->connecting_state) {
  1909. /* check allowed time left */
  1910. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1911. if(timeout_ms < 0) {
  1912. /* no need to continue if time already is up */
  1913. failf(data, "SSL connection timeout");
  1914. return CURLE_OPERATION_TIMEDOUT;
  1915. }
  1916. /* if ssl is expecting something, check if it's available. */
  1917. if(connssl->connecting_state == ssl_connect_2_reading
  1918. || connssl->connecting_state == ssl_connect_2_writing) {
  1919. curl_socket_t writefd = ssl_connect_2_writing ==
  1920. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  1921. curl_socket_t readfd = ssl_connect_2_reading ==
  1922. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  1923. what = Curl_socket_ready(readfd, writefd, nonblocking?0:timeout_ms);
  1924. if(what < 0) {
  1925. /* fatal error */
  1926. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1927. return CURLE_SSL_CONNECT_ERROR;
  1928. }
  1929. else if(0 == what) {
  1930. if(nonblocking) {
  1931. *done = FALSE;
  1932. return CURLE_OK;
  1933. }
  1934. else {
  1935. /* timeout */
  1936. failf(data, "SSL connection timeout");
  1937. return CURLE_OPERATION_TIMEDOUT;
  1938. }
  1939. }
  1940. /* socket is readable or writable */
  1941. }
  1942. /* Run transaction, and return to the caller if it failed or if this
  1943. * connection is done nonblocking and this loop would execute again. This
  1944. * permits the owner of a multi handle to abort a connection attempt
  1945. * before step2 has completed while ensuring that a client using select()
  1946. * or epoll() will always have a valid fdset to wait on.
  1947. */
  1948. retcode = darwinssl_connect_step2(conn, sockindex);
  1949. if(retcode || (nonblocking &&
  1950. (ssl_connect_2 == connssl->connecting_state ||
  1951. ssl_connect_2_reading == connssl->connecting_state ||
  1952. ssl_connect_2_writing == connssl->connecting_state)))
  1953. return retcode;
  1954. } /* repeat step2 until all transactions are done. */
  1955. if(ssl_connect_3==connssl->connecting_state) {
  1956. retcode = darwinssl_connect_step3(conn, sockindex);
  1957. if(retcode)
  1958. return retcode;
  1959. }
  1960. if(ssl_connect_done==connssl->connecting_state) {
  1961. connssl->state = ssl_connection_complete;
  1962. conn->recv[sockindex] = darwinssl_recv;
  1963. conn->send[sockindex] = darwinssl_send;
  1964. *done = TRUE;
  1965. }
  1966. else
  1967. *done = FALSE;
  1968. /* Reset our connect state machine */
  1969. connssl->connecting_state = ssl_connect_1;
  1970. return CURLE_OK;
  1971. }
  1972. CURLcode
  1973. Curl_darwinssl_connect_nonblocking(struct connectdata *conn,
  1974. int sockindex,
  1975. bool *done)
  1976. {
  1977. return darwinssl_connect_common(conn, sockindex, TRUE, done);
  1978. }
  1979. CURLcode
  1980. Curl_darwinssl_connect(struct connectdata *conn,
  1981. int sockindex)
  1982. {
  1983. CURLcode retcode;
  1984. bool done = FALSE;
  1985. retcode = darwinssl_connect_common(conn, sockindex, FALSE, &done);
  1986. if(retcode)
  1987. return retcode;
  1988. DEBUGASSERT(done);
  1989. return CURLE_OK;
  1990. }
  1991. void Curl_darwinssl_close(struct connectdata *conn, int sockindex)
  1992. {
  1993. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1994. if(connssl->ssl_ctx) {
  1995. (void)SSLClose(connssl->ssl_ctx);
  1996. #if CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS
  1997. if(SSLCreateContext != NULL)
  1998. CFRelease(connssl->ssl_ctx);
  1999. #if CURL_SUPPORT_MAC_10_8
  2000. else
  2001. (void)SSLDisposeContext(connssl->ssl_ctx);
  2002. #endif /* CURL_SUPPORT_MAC_10_8 */
  2003. #else
  2004. (void)SSLDisposeContext(connssl->ssl_ctx);
  2005. #endif /* CURL_BUILD_MAC_10_8 || CURL_BUILD_IOS */
  2006. connssl->ssl_ctx = NULL;
  2007. }
  2008. connssl->ssl_sockfd = 0;
  2009. }
  2010. void Curl_darwinssl_close_all(struct SessionHandle *data)
  2011. {
  2012. /* SecureTransport doesn't separate sessions from contexts, so... */
  2013. (void)data;
  2014. }
  2015. int Curl_darwinssl_shutdown(struct connectdata *conn, int sockindex)
  2016. {
  2017. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  2018. struct SessionHandle *data = conn->data;
  2019. ssize_t nread;
  2020. int what;
  2021. int rc;
  2022. char buf[120];
  2023. if(!connssl->ssl_ctx)
  2024. return 0;
  2025. if(data->set.ftp_ccc != CURLFTPSSL_CCC_ACTIVE)
  2026. return 0;
  2027. Curl_darwinssl_close(conn, sockindex);
  2028. rc = 0;
  2029. what = Curl_socket_ready(conn->sock[sockindex],
  2030. CURL_SOCKET_BAD, SSL_SHUTDOWN_TIMEOUT);
  2031. for(;;) {
  2032. if(what < 0) {
  2033. /* anything that gets here is fatally bad */
  2034. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  2035. rc = -1;
  2036. break;
  2037. }
  2038. if(!what) { /* timeout */
  2039. failf(data, "SSL shutdown timeout");
  2040. break;
  2041. }
  2042. /* Something to read, let's do it and hope that it is the close
  2043. notify alert from the server. No way to SSL_Read now, so use read(). */
  2044. nread = read(conn->sock[sockindex], buf, sizeof(buf));
  2045. if(nread < 0) {
  2046. failf(data, "read: %s", strerror(errno));
  2047. rc = -1;
  2048. }
  2049. if(nread <= 0)
  2050. break;
  2051. what = Curl_socket_ready(conn->sock[sockindex], CURL_SOCKET_BAD, 0);
  2052. }
  2053. return rc;
  2054. }
  2055. void Curl_darwinssl_session_free(void *ptr)
  2056. {
  2057. /* ST, as of iOS 5 and Mountain Lion, has no public method of deleting a
  2058. cached session ID inside the Security framework. There is a private
  2059. function that does this, but I don't want to have to explain to you why I
  2060. got your application rejected from the App Store due to the use of a
  2061. private API, so the best we can do is free up our own char array that we
  2062. created way back in darwinssl_connect_step1... */
  2063. Curl_safefree(ptr);
  2064. }
  2065. size_t Curl_darwinssl_version(char *buffer, size_t size)
  2066. {
  2067. return snprintf(buffer, size, "SecureTransport");
  2068. }
  2069. /*
  2070. * This function uses SSLGetSessionState to determine connection status.
  2071. *
  2072. * Return codes:
  2073. * 1 means the connection is still in place
  2074. * 0 means the connection has been closed
  2075. * -1 means the connection status is unknown
  2076. */
  2077. int Curl_darwinssl_check_cxn(struct connectdata *conn)
  2078. {
  2079. struct ssl_connect_data *connssl = &conn->ssl[FIRSTSOCKET];
  2080. OSStatus err;
  2081. SSLSessionState state;
  2082. if(connssl->ssl_ctx) {
  2083. err = SSLGetSessionState(connssl->ssl_ctx, &state);
  2084. if(err == noErr)
  2085. return state == kSSLConnected || state == kSSLHandshake;
  2086. return -1;
  2087. }
  2088. return 0;
  2089. }
  2090. bool Curl_darwinssl_data_pending(const struct connectdata *conn,
  2091. int connindex)
  2092. {
  2093. const struct ssl_connect_data *connssl = &conn->ssl[connindex];
  2094. OSStatus err;
  2095. size_t buffer;
  2096. if(connssl->ssl_ctx) { /* SSL is in use */
  2097. err = SSLGetBufferedReadSize(connssl->ssl_ctx, &buffer);
  2098. if(err == noErr)
  2099. return buffer > 0UL;
  2100. return false;
  2101. }
  2102. else
  2103. return false;
  2104. }
  2105. void Curl_darwinssl_random(struct SessionHandle *data,
  2106. unsigned char *entropy,
  2107. size_t length)
  2108. {
  2109. /* arc4random_buf() isn't available on cats older than Lion, so let's
  2110. do this manually for the benefit of the older cats. */
  2111. size_t i;
  2112. u_int32_t random_number = 0;
  2113. for(i = 0 ; i < length ; i++) {
  2114. if(i % sizeof(u_int32_t) == 0)
  2115. random_number = arc4random();
  2116. entropy[i] = random_number & 0xFF;
  2117. random_number >>= 8;
  2118. }
  2119. i = random_number = 0;
  2120. (void)data;
  2121. }
  2122. void Curl_darwinssl_md5sum(unsigned char *tmp, /* input */
  2123. size_t tmplen,
  2124. unsigned char *md5sum, /* output */
  2125. size_t md5len)
  2126. {
  2127. (void)md5len;
  2128. (void)CC_MD5(tmp, (CC_LONG)tmplen, md5sum);
  2129. }
  2130. static ssize_t darwinssl_send(struct connectdata *conn,
  2131. int sockindex,
  2132. const void *mem,
  2133. size_t len,
  2134. CURLcode *curlcode)
  2135. {
  2136. /*struct SessionHandle *data = conn->data;*/
  2137. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  2138. size_t processed = 0UL;
  2139. OSStatus err;
  2140. /* The SSLWrite() function works a little differently than expected. The
  2141. fourth argument (processed) is currently documented in Apple's
  2142. documentation as: "On return, the length, in bytes, of the data actually
  2143. written."
  2144. Now, one could interpret that as "written to the socket," but actually,
  2145. it returns the amount of data that was written to a buffer internal to
  2146. the SSLContextRef instead. So it's possible for SSLWrite() to return
  2147. errSSLWouldBlock and a number of bytes "written" because those bytes were
  2148. encrypted and written to a buffer, not to the socket.
  2149. So if this happens, then we need to keep calling SSLWrite() over and
  2150. over again with no new data until it quits returning errSSLWouldBlock. */
  2151. /* Do we have buffered data to write from the last time we were called? */
  2152. if(connssl->ssl_write_buffered_length) {
  2153. /* Write the buffered data: */
  2154. err = SSLWrite(connssl->ssl_ctx, NULL, 0UL, &processed);
  2155. switch (err) {
  2156. case noErr:
  2157. /* processed is always going to be 0 because we didn't write to
  2158. the buffer, so return how much was written to the socket */
  2159. processed = connssl->ssl_write_buffered_length;
  2160. connssl->ssl_write_buffered_length = 0UL;
  2161. break;
  2162. case errSSLWouldBlock: /* argh, try again */
  2163. *curlcode = CURLE_AGAIN;
  2164. return -1L;
  2165. default:
  2166. failf(conn->data, "SSLWrite() returned error %d", err);
  2167. *curlcode = CURLE_SEND_ERROR;
  2168. return -1L;
  2169. }
  2170. }
  2171. else {
  2172. /* We've got new data to write: */
  2173. err = SSLWrite(connssl->ssl_ctx, mem, len, &processed);
  2174. if(err != noErr) {
  2175. switch (err) {
  2176. case errSSLWouldBlock:
  2177. /* Data was buffered but not sent, we have to tell the caller
  2178. to try sending again, and remember how much was buffered */
  2179. connssl->ssl_write_buffered_length = len;
  2180. *curlcode = CURLE_AGAIN;
  2181. return -1L;
  2182. default:
  2183. failf(conn->data, "SSLWrite() returned error %d", err);
  2184. *curlcode = CURLE_SEND_ERROR;
  2185. return -1L;
  2186. }
  2187. }
  2188. }
  2189. return (ssize_t)processed;
  2190. }
  2191. static ssize_t darwinssl_recv(struct connectdata *conn,
  2192. int num,
  2193. char *buf,
  2194. size_t buffersize,
  2195. CURLcode *curlcode)
  2196. {
  2197. /*struct SessionHandle *data = conn->data;*/
  2198. struct ssl_connect_data *connssl = &conn->ssl[num];
  2199. size_t processed = 0UL;
  2200. OSStatus err = SSLRead(connssl->ssl_ctx, buf, buffersize, &processed);
  2201. if(err != noErr) {
  2202. switch (err) {
  2203. case errSSLWouldBlock: /* return how much we read (if anything) */
  2204. if(processed)
  2205. return (ssize_t)processed;
  2206. *curlcode = CURLE_AGAIN;
  2207. return -1L;
  2208. break;
  2209. /* errSSLClosedGraceful - server gracefully shut down the SSL session
  2210. errSSLClosedNoNotify - server hung up on us instead of sending a
  2211. closure alert notice, read() is returning 0
  2212. Either way, inform the caller that the server disconnected. */
  2213. case errSSLClosedGraceful:
  2214. case errSSLClosedNoNotify:
  2215. *curlcode = CURLE_OK;
  2216. return -1L;
  2217. break;
  2218. default:
  2219. failf(conn->data, "SSLRead() return error %d", err);
  2220. *curlcode = CURLE_RECV_ERROR;
  2221. return -1L;
  2222. break;
  2223. }
  2224. }
  2225. return (ssize_t)processed;
  2226. }
  2227. #endif /* USE_DARWINSSL */