2
0

curl_darwinssl.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2012, Nick Zitzmann, <nickzman@gmail.com>.
  9. * Copyright (C) 2012, 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 sslgen.c should ever call or use these functions.
  26. */
  27. #include "setup.h"
  28. #ifdef USE_DARWINSSL
  29. #ifdef HAVE_LIMITS_H
  30. #include <limits.h>
  31. #endif
  32. #ifdef HAVE_SYS_SOCKET_H
  33. #include <sys/socket.h>
  34. #endif
  35. #include <Security/Security.h>
  36. #include <Security/SecureTransport.h>
  37. #include <CoreFoundation/CoreFoundation.h>
  38. #include <CommonCrypto/CommonDigest.h>
  39. #include "urldata.h"
  40. #include "sendf.h"
  41. #include "inet_pton.h"
  42. #include "connect.h"
  43. #include "select.h"
  44. #include "sslgen.h"
  45. #include "curl_darwinssl.h"
  46. #define _MPRINTF_REPLACE /* use our functions only */
  47. #include <curl/mprintf.h>
  48. #include "curl_memory.h"
  49. /* The last #include file should be: */
  50. #include "memdebug.h"
  51. /* From MacTypes.h (which we can't include because it isn't present in iOS: */
  52. #define ioErr -36
  53. /* In Mountain Lion and iOS 5, Apple made some changes to the API. They
  54. added TLS 1.1 and 1.2 support, and deprecated and replaced some
  55. functions. You need to build against the Mountain Lion or iOS 5 SDK
  56. or later to get TLS 1.1 or 1.2 support working in cURL. We'll weak-link
  57. to the newer functions and use them if present in the user's OS.
  58. Builders: If you want TLS 1.1 and 1.2 but still want to retain support
  59. for older cats, don't forget to set the MACOSX_DEPLOYMENT_TARGET
  60. environmental variable prior to building cURL. */
  61. /* The following two functions were ripped from Apple sample code,
  62. * with some modifications: */
  63. static OSStatus SocketRead(SSLConnectionRef connection,
  64. void *data, /* owned by
  65. * caller, data
  66. * RETURNED */
  67. size_t *dataLength) /* IN/OUT */
  68. {
  69. UInt32 bytesToGo = *dataLength;
  70. UInt32 initLen = bytesToGo;
  71. UInt8 *currData = (UInt8 *)data;
  72. /*int sock = *(int *)connection;*/
  73. struct ssl_connect_data *connssl = (struct ssl_connect_data *)connection;
  74. int sock = connssl->ssl_sockfd;
  75. OSStatus rtn = noErr;
  76. UInt32 bytesRead;
  77. int rrtn;
  78. int theErr;
  79. *dataLength = 0;
  80. for(;;) {
  81. bytesRead = 0;
  82. rrtn = read(sock, currData, bytesToGo);
  83. if(rrtn <= 0) {
  84. /* this is guesswork... */
  85. theErr = errno;
  86. if((rrtn == 0) && (theErr == 0)) {
  87. /* try fix for iSync */
  88. rtn = errSSLClosedGraceful;
  89. }
  90. else /* do the switch */
  91. switch(theErr) {
  92. case ENOENT:
  93. /* connection closed */
  94. rtn = errSSLClosedGraceful;
  95. break;
  96. case ECONNRESET:
  97. rtn = errSSLClosedAbort;
  98. break;
  99. case EAGAIN:
  100. rtn = errSSLWouldBlock;
  101. connssl->ssl_direction = false;
  102. break;
  103. default:
  104. rtn = ioErr;
  105. break;
  106. }
  107. break;
  108. }
  109. else {
  110. bytesRead = rrtn;
  111. }
  112. bytesToGo -= bytesRead;
  113. currData += bytesRead;
  114. if(bytesToGo == 0) {
  115. /* filled buffer with incoming data, done */
  116. break;
  117. }
  118. }
  119. *dataLength = initLen - bytesToGo;
  120. return rtn;
  121. }
  122. static OSStatus SocketWrite(SSLConnectionRef connection,
  123. const void *data,
  124. size_t *dataLength) /* IN/OUT */
  125. {
  126. UInt32 bytesSent = 0;
  127. /*int sock = *(int *)connection;*/
  128. struct ssl_connect_data *connssl = (struct ssl_connect_data *)connection;
  129. int sock = connssl->ssl_sockfd;
  130. int length;
  131. UInt32 dataLen = *dataLength;
  132. const UInt8 *dataPtr = (UInt8 *)data;
  133. OSStatus ortn;
  134. int theErr;
  135. *dataLength = 0;
  136. do {
  137. length = write(sock,
  138. (char*)dataPtr + bytesSent,
  139. dataLen - bytesSent);
  140. } while((length > 0) &&
  141. ( (bytesSent += length) < dataLen) );
  142. if(length <= 0) {
  143. theErr = errno;
  144. if(theErr == EAGAIN) {
  145. ortn = errSSLWouldBlock;
  146. connssl->ssl_direction = true;
  147. }
  148. else {
  149. ortn = ioErr;
  150. }
  151. }
  152. else {
  153. ortn = noErr;
  154. }
  155. *dataLength = bytesSent;
  156. return ortn;
  157. }
  158. CF_INLINE const char *SSLCipherNameForNumber(SSLCipherSuite cipher) {
  159. switch (cipher) {
  160. /* SSL version 3.0 */
  161. case SSL_RSA_WITH_NULL_MD5:
  162. return "SSL_RSA_WITH_NULL_MD5";
  163. break;
  164. case SSL_RSA_WITH_NULL_SHA:
  165. return "SSL_RSA_WITH_NULL_SHA";
  166. break;
  167. case SSL_RSA_EXPORT_WITH_RC4_40_MD5:
  168. return "SSL_RSA_EXPORT_WITH_RC4_40_MD5";
  169. break;
  170. case SSL_RSA_WITH_RC4_128_MD5:
  171. return "SSL_RSA_WITH_RC4_128_MD5";
  172. break;
  173. case SSL_RSA_WITH_RC4_128_SHA:
  174. return "SSL_RSA_WITH_RC4_128_SHA";
  175. break;
  176. case SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5:
  177. return "SSL_RSA_EXPORT_WITH_RC2_CBC_40_MD5";
  178. break;
  179. case SSL_RSA_WITH_IDEA_CBC_SHA:
  180. return "SSL_RSA_WITH_IDEA_CBC_SHA";
  181. break;
  182. case SSL_RSA_EXPORT_WITH_DES40_CBC_SHA:
  183. return "SSL_RSA_EXPORT_WITH_DES40_CBC_SHA";
  184. break;
  185. case SSL_RSA_WITH_DES_CBC_SHA:
  186. return "SSL_RSA_WITH_DES_CBC_SHA";
  187. break;
  188. case SSL_RSA_WITH_3DES_EDE_CBC_SHA:
  189. return "SSL_RSA_WITH_3DES_EDE_CBC_SHA";
  190. break;
  191. case SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA:
  192. return "SSL_DH_DSS_EXPORT_WITH_DES40_CBC_SHA";
  193. break;
  194. case SSL_DH_DSS_WITH_DES_CBC_SHA:
  195. return "SSL_DH_DSS_WITH_DES_CBC_SHA";
  196. break;
  197. case SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA:
  198. return "SSL_DH_DSS_WITH_3DES_EDE_CBC_SHA";
  199. break;
  200. case SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA:
  201. return "SSL_DH_RSA_EXPORT_WITH_DES40_CBC_SHA";
  202. break;
  203. case SSL_DH_RSA_WITH_DES_CBC_SHA:
  204. return "SSL_DH_RSA_WITH_DES_CBC_SHA";
  205. break;
  206. case SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA:
  207. return "SSL_DH_RSA_WITH_3DES_EDE_CBC_SHA";
  208. break;
  209. case SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA:
  210. return "SSL_DHE_DSS_EXPORT_WITH_DES40_CBC_SHA";
  211. break;
  212. case SSL_DHE_DSS_WITH_DES_CBC_SHA:
  213. return "SSL_DHE_DSS_WITH_DES_CBC_SHA";
  214. break;
  215. case SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
  216. return "SSL_DHE_DSS_WITH_3DES_EDE_CBC_SHA";
  217. break;
  218. case SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA:
  219. return "SSL_DHE_RSA_EXPORT_WITH_DES40_CBC_SHA";
  220. break;
  221. case SSL_DHE_RSA_WITH_DES_CBC_SHA:
  222. return "SSL_DHE_RSA_WITH_DES_CBC_SHA";
  223. break;
  224. case SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
  225. return "SSL_DHE_RSA_WITH_3DES_EDE_CBC_SHA";
  226. break;
  227. case SSL_DH_anon_EXPORT_WITH_RC4_40_MD5:
  228. return "SSL_DH_anon_EXPORT_WITH_RC4_40_MD5";
  229. break;
  230. case SSL_DH_anon_WITH_RC4_128_MD5:
  231. return "SSL_DH_anon_WITH_RC4_128_MD5";
  232. break;
  233. case SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA:
  234. return "SSL_DH_anon_EXPORT_WITH_DES40_CBC_SHA";
  235. break;
  236. case SSL_DH_anon_WITH_DES_CBC_SHA:
  237. return "SSL_DH_anon_WITH_DES_CBC_SHA";
  238. break;
  239. case SSL_DH_anon_WITH_3DES_EDE_CBC_SHA:
  240. return "SSL_DH_anon_WITH_3DES_EDE_CBC_SHA";
  241. break;
  242. case SSL_FORTEZZA_DMS_WITH_NULL_SHA:
  243. return "SSL_FORTEZZA_DMS_WITH_NULL_SHA";
  244. break;
  245. case SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA:
  246. return "SSL_FORTEZZA_DMS_WITH_FORTEZZA_CBC_SHA";
  247. break;
  248. /* TLS 1.0 with AES (RFC 3268)
  249. (Apparently these are used in SSLv3 implementations as well.) */
  250. case TLS_RSA_WITH_AES_128_CBC_SHA:
  251. return "TLS_RSA_WITH_AES_128_CBC_SHA";
  252. break;
  253. case TLS_DH_DSS_WITH_AES_128_CBC_SHA:
  254. return "TLS_DH_DSS_WITH_AES_128_CBC_SHA";
  255. break;
  256. case TLS_DH_RSA_WITH_AES_128_CBC_SHA:
  257. return "TLS_DH_RSA_WITH_AES_128_CBC_SHA";
  258. break;
  259. case TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
  260. return "TLS_DHE_DSS_WITH_AES_128_CBC_SHA";
  261. break;
  262. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
  263. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA";
  264. break;
  265. case TLS_DH_anon_WITH_AES_128_CBC_SHA:
  266. return "TLS_DH_anon_WITH_AES_128_CBC_SHA";
  267. break;
  268. case TLS_RSA_WITH_AES_256_CBC_SHA:
  269. return "TLS_RSA_WITH_AES_256_CBC_SHA";
  270. break;
  271. case TLS_DH_DSS_WITH_AES_256_CBC_SHA:
  272. return "TLS_DH_DSS_WITH_AES_256_CBC_SHA";
  273. break;
  274. case TLS_DH_RSA_WITH_AES_256_CBC_SHA:
  275. return "TLS_DH_RSA_WITH_AES_256_CBC_SHA";
  276. break;
  277. case TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
  278. return "TLS_DHE_DSS_WITH_AES_256_CBC_SHA";
  279. break;
  280. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
  281. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA";
  282. break;
  283. case TLS_DH_anon_WITH_AES_256_CBC_SHA:
  284. return "TLS_DH_anon_WITH_AES_256_CBC_SHA";
  285. break;
  286. /* SSL version 2.0 */
  287. case SSL_RSA_WITH_RC2_CBC_MD5:
  288. return "SSL_RSA_WITH_RC2_CBC_MD5";
  289. break;
  290. case SSL_RSA_WITH_IDEA_CBC_MD5:
  291. return "SSL_RSA_WITH_IDEA_CBC_MD5";
  292. break;
  293. case SSL_RSA_WITH_DES_CBC_MD5:
  294. return "SSL_RSA_WITH_DES_CBC_MD5";
  295. break;
  296. case SSL_RSA_WITH_3DES_EDE_CBC_MD5:
  297. return "SSL_RSA_WITH_3DES_EDE_CBC_MD5";
  298. break;
  299. }
  300. return "SSL_NULL_WITH_NULL_NULL";
  301. }
  302. CF_INLINE const char *TLSCipherNameForNumber(SSLCipherSuite cipher) {
  303. switch(cipher) {
  304. /* TLS 1.0 with AES (RFC 3268) */
  305. case TLS_RSA_WITH_AES_128_CBC_SHA:
  306. return "TLS_RSA_WITH_AES_128_CBC_SHA";
  307. break;
  308. case TLS_DH_DSS_WITH_AES_128_CBC_SHA:
  309. return "TLS_DH_DSS_WITH_AES_128_CBC_SHA";
  310. break;
  311. case TLS_DH_RSA_WITH_AES_128_CBC_SHA:
  312. return "TLS_DH_RSA_WITH_AES_128_CBC_SHA";
  313. break;
  314. case TLS_DHE_DSS_WITH_AES_128_CBC_SHA:
  315. return "TLS_DHE_DSS_WITH_AES_128_CBC_SHA";
  316. break;
  317. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA:
  318. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA";
  319. break;
  320. case TLS_DH_anon_WITH_AES_128_CBC_SHA:
  321. return "TLS_DH_anon_WITH_AES_128_CBC_SHA";
  322. break;
  323. case TLS_RSA_WITH_AES_256_CBC_SHA:
  324. return "TLS_RSA_WITH_AES_256_CBC_SHA";
  325. break;
  326. case TLS_DH_DSS_WITH_AES_256_CBC_SHA:
  327. return "TLS_DH_DSS_WITH_AES_256_CBC_SHA";
  328. break;
  329. case TLS_DH_RSA_WITH_AES_256_CBC_SHA:
  330. return "TLS_DH_RSA_WITH_AES_256_CBC_SHA";
  331. break;
  332. case TLS_DHE_DSS_WITH_AES_256_CBC_SHA:
  333. return "TLS_DHE_DSS_WITH_AES_256_CBC_SHA";
  334. break;
  335. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA:
  336. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA";
  337. break;
  338. case TLS_DH_anon_WITH_AES_256_CBC_SHA:
  339. return "TLS_DH_anon_WITH_AES_256_CBC_SHA";
  340. break;
  341. /* TLS 1.0 with ECDSA (RFC 4492) */
  342. case TLS_ECDH_ECDSA_WITH_NULL_SHA:
  343. return "TLS_ECDH_ECDSA_WITH_NULL_SHA";
  344. break;
  345. case TLS_ECDH_ECDSA_WITH_RC4_128_SHA:
  346. return "TLS_ECDH_ECDSA_WITH_RC4_128_SHA";
  347. break;
  348. case TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA:
  349. return "TLS_ECDH_ECDSA_WITH_3DES_EDE_CBC_SHA";
  350. break;
  351. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA:
  352. return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA";
  353. break;
  354. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA:
  355. return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA";
  356. break;
  357. case TLS_ECDHE_ECDSA_WITH_NULL_SHA:
  358. return "TLS_ECDHE_ECDSA_WITH_NULL_SHA";
  359. break;
  360. case TLS_ECDHE_ECDSA_WITH_RC4_128_SHA:
  361. return "TLS_ECDHE_ECDSA_WITH_RC4_128_SHA";
  362. break;
  363. case TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA:
  364. return "TLS_ECDHE_ECDSA_WITH_3DES_EDE_CBC_SHA";
  365. break;
  366. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA:
  367. return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA";
  368. break;
  369. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA:
  370. return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA";
  371. break;
  372. case TLS_ECDH_RSA_WITH_NULL_SHA:
  373. return "TLS_ECDH_RSA_WITH_NULL_SHA";
  374. break;
  375. case TLS_ECDH_RSA_WITH_RC4_128_SHA:
  376. return "TLS_ECDH_RSA_WITH_RC4_128_SHA";
  377. break;
  378. case TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA:
  379. return "TLS_ECDH_RSA_WITH_3DES_EDE_CBC_SHA";
  380. break;
  381. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA:
  382. return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA";
  383. break;
  384. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA:
  385. return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA";
  386. break;
  387. case TLS_ECDHE_RSA_WITH_NULL_SHA:
  388. return "TLS_ECDHE_RSA_WITH_NULL_SHA";
  389. break;
  390. case TLS_ECDHE_RSA_WITH_RC4_128_SHA:
  391. return "TLS_ECDHE_RSA_WITH_RC4_128_SHA";
  392. break;
  393. case TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA:
  394. return "TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA";
  395. break;
  396. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA:
  397. return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA";
  398. break;
  399. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA:
  400. return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA";
  401. break;
  402. case TLS_ECDH_anon_WITH_NULL_SHA:
  403. return "TLS_ECDH_anon_WITH_NULL_SHA";
  404. break;
  405. case TLS_ECDH_anon_WITH_RC4_128_SHA:
  406. return "TLS_ECDH_anon_WITH_RC4_128_SHA";
  407. break;
  408. case TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA:
  409. return "TLS_ECDH_anon_WITH_3DES_EDE_CBC_SHA";
  410. break;
  411. case TLS_ECDH_anon_WITH_AES_128_CBC_SHA:
  412. return "TLS_ECDH_anon_WITH_AES_128_CBC_SHA";
  413. break;
  414. case TLS_ECDH_anon_WITH_AES_256_CBC_SHA:
  415. return "TLS_ECDH_anon_WITH_AES_256_CBC_SHA";
  416. break;
  417. #if defined(__MAC_10_8) || defined(__IPHONE_5_0)
  418. /* TLS 1.2 (RFC 5246) */
  419. case TLS_RSA_WITH_NULL_MD5:
  420. return "TLS_RSA_WITH_NULL_MD5";
  421. break;
  422. case TLS_RSA_WITH_NULL_SHA:
  423. return "TLS_RSA_WITH_NULL_SHA";
  424. break;
  425. case TLS_RSA_WITH_RC4_128_MD5:
  426. return "TLS_RSA_WITH_RC4_128_MD5";
  427. break;
  428. case TLS_RSA_WITH_RC4_128_SHA:
  429. return "TLS_RSA_WITH_RC4_128_SHA";
  430. break;
  431. case TLS_RSA_WITH_3DES_EDE_CBC_SHA:
  432. return "TLS_RSA_WITH_3DES_EDE_CBC_SHA";
  433. break;
  434. case TLS_RSA_WITH_NULL_SHA256:
  435. return "TLS_RSA_WITH_NULL_SHA256";
  436. break;
  437. case TLS_RSA_WITH_AES_128_CBC_SHA256:
  438. return "TLS_RSA_WITH_AES_128_CBC_SHA256";
  439. break;
  440. case TLS_RSA_WITH_AES_256_CBC_SHA256:
  441. return "TLS_RSA_WITH_AES_256_CBC_SHA256";
  442. break;
  443. case TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA:
  444. return "TLS_DH_DSS_WITH_3DES_EDE_CBC_SHA";
  445. break;
  446. case TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA:
  447. return "TLS_DH_RSA_WITH_3DES_EDE_CBC_SHA";
  448. break;
  449. case TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA:
  450. return "TLS_DHE_DSS_WITH_3DES_EDE_CBC_SHA";
  451. break;
  452. case TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA:
  453. return "TLS_DHE_RSA_WITH_3DES_EDE_CBC_SHA";
  454. break;
  455. case TLS_DH_DSS_WITH_AES_128_CBC_SHA256:
  456. return "TLS_DH_DSS_WITH_AES_128_CBC_SHA256";
  457. break;
  458. case TLS_DH_RSA_WITH_AES_128_CBC_SHA256:
  459. return "TLS_DH_RSA_WITH_AES_128_CBC_SHA256";
  460. break;
  461. case TLS_DHE_DSS_WITH_AES_128_CBC_SHA256:
  462. return "TLS_DHE_DSS_WITH_AES_128_CBC_SHA256";
  463. break;
  464. case TLS_DHE_RSA_WITH_AES_128_CBC_SHA256:
  465. return "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256";
  466. break;
  467. case TLS_DH_DSS_WITH_AES_256_CBC_SHA256:
  468. return "TLS_DH_DSS_WITH_AES_256_CBC_SHA256";
  469. break;
  470. case TLS_DH_RSA_WITH_AES_256_CBC_SHA256:
  471. return "TLS_DH_RSA_WITH_AES_256_CBC_SHA256";
  472. break;
  473. case TLS_DHE_DSS_WITH_AES_256_CBC_SHA256:
  474. return "TLS_DHE_DSS_WITH_AES_256_CBC_SHA256";
  475. break;
  476. case TLS_DHE_RSA_WITH_AES_256_CBC_SHA256:
  477. return "TLS_DHE_RSA_WITH_AES_256_CBC_SHA256";
  478. break;
  479. case TLS_DH_anon_WITH_RC4_128_MD5:
  480. return "TLS_DH_anon_WITH_RC4_128_MD5";
  481. break;
  482. case TLS_DH_anon_WITH_3DES_EDE_CBC_SHA:
  483. return "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA";
  484. break;
  485. case TLS_DH_anon_WITH_AES_128_CBC_SHA256:
  486. return "TLS_DH_anon_WITH_AES_128_CBC_SHA256";
  487. break;
  488. case TLS_DH_anon_WITH_AES_256_CBC_SHA256:
  489. return "TLS_DH_anon_WITH_AES_256_CBC_SHA256";
  490. break;
  491. /* TLS 1.2 with AES GCM (RFC 5288) */
  492. case TLS_RSA_WITH_AES_128_GCM_SHA256:
  493. return "TLS_RSA_WITH_AES_128_GCM_SHA256";
  494. break;
  495. case TLS_RSA_WITH_AES_256_GCM_SHA384:
  496. return "TLS_RSA_WITH_AES_256_GCM_SHA384";
  497. break;
  498. case TLS_DHE_RSA_WITH_AES_128_GCM_SHA256:
  499. return "TLS_DHE_RSA_WITH_AES_128_GCM_SHA256";
  500. break;
  501. case TLS_DHE_RSA_WITH_AES_256_GCM_SHA384:
  502. return "TLS_DHE_RSA_WITH_AES_256_GCM_SHA384";
  503. break;
  504. case TLS_DH_RSA_WITH_AES_128_GCM_SHA256:
  505. return "TLS_DH_RSA_WITH_AES_128_GCM_SHA256";
  506. break;
  507. case TLS_DH_RSA_WITH_AES_256_GCM_SHA384:
  508. return "TLS_DH_RSA_WITH_AES_256_GCM_SHA384";
  509. break;
  510. case TLS_DHE_DSS_WITH_AES_128_GCM_SHA256:
  511. return "TLS_DHE_DSS_WITH_AES_128_GCM_SHA256";
  512. break;
  513. case TLS_DHE_DSS_WITH_AES_256_GCM_SHA384:
  514. return "TLS_DHE_DSS_WITH_AES_256_GCM_SHA384";
  515. break;
  516. case TLS_DH_DSS_WITH_AES_128_GCM_SHA256:
  517. return "TLS_DH_DSS_WITH_AES_128_GCM_SHA256";
  518. break;
  519. case TLS_DH_DSS_WITH_AES_256_GCM_SHA384:
  520. return "TLS_DH_DSS_WITH_AES_256_GCM_SHA384";
  521. break;
  522. case TLS_DH_anon_WITH_AES_128_GCM_SHA256:
  523. return "TLS_DH_anon_WITH_AES_128_GCM_SHA256";
  524. break;
  525. case TLS_DH_anon_WITH_AES_256_GCM_SHA384:
  526. return "TLS_DH_anon_WITH_AES_256_GCM_SHA384";
  527. break;
  528. /* TLS 1.2 with elliptic curve ciphers (RFC 5289) */
  529. case TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256:
  530. return "TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256";
  531. break;
  532. case TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384:
  533. return "TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA384";
  534. break;
  535. case TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256:
  536. return "TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256";
  537. break;
  538. case TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384:
  539. return "TLS_ECDH_ECDSA_WITH_AES_256_CBC_SHA384";
  540. break;
  541. case TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256:
  542. return "TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256";
  543. break;
  544. case TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384:
  545. return "TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA384";
  546. break;
  547. case TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256:
  548. return "TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256";
  549. break;
  550. case TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384:
  551. return "TLS_ECDH_RSA_WITH_AES_256_CBC_SHA384";
  552. break;
  553. case TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256:
  554. return "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256";
  555. break;
  556. case TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384:
  557. return "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384";
  558. break;
  559. case TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256:
  560. return "TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256";
  561. break;
  562. case TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384:
  563. return "TLS_ECDH_ECDSA_WITH_AES_256_GCM_SHA384";
  564. break;
  565. case TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256:
  566. return "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256";
  567. break;
  568. case TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384:
  569. return "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384";
  570. break;
  571. case TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256:
  572. return "TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256";
  573. break;
  574. case TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384:
  575. return "TLS_ECDH_RSA_WITH_AES_256_GCM_SHA384";
  576. break;
  577. case TLS_EMPTY_RENEGOTIATION_INFO_SCSV:
  578. return "TLS_EMPTY_RENEGOTIATION_INFO_SCSV";
  579. break;
  580. #else
  581. case SSL_RSA_WITH_NULL_MD5:
  582. return "TLS_RSA_WITH_NULL_MD5";
  583. break;
  584. case SSL_RSA_WITH_NULL_SHA:
  585. return "TLS_RSA_WITH_NULL_SHA";
  586. break;
  587. case SSL_RSA_WITH_RC4_128_MD5:
  588. return "TLS_RSA_WITH_RC4_128_MD5";
  589. break;
  590. case SSL_RSA_WITH_RC4_128_SHA:
  591. return "TLS_RSA_WITH_RC4_128_SHA";
  592. break;
  593. case SSL_RSA_WITH_3DES_EDE_CBC_SHA:
  594. return "TLS_RSA_WITH_3DES_EDE_CBC_SHA";
  595. break;
  596. case SSL_DH_anon_WITH_RC4_128_MD5:
  597. return "TLS_DH_anon_WITH_RC4_128_MD5";
  598. break;
  599. case SSL_DH_anon_WITH_3DES_EDE_CBC_SHA:
  600. return "TLS_DH_anon_WITH_3DES_EDE_CBC_SHA";
  601. break;
  602. #endif /* defined(__MAC_10_8) || defined(__IPHONE_5_0) */
  603. }
  604. return "TLS_NULL_WITH_NULL_NULL";
  605. }
  606. static CURLcode darwinssl_connect_step1(struct connectdata *conn,
  607. int sockindex)
  608. {
  609. struct SessionHandle *data = conn->data;
  610. curl_socket_t sockfd = conn->sock[sockindex];
  611. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  612. bool sni = true;
  613. #ifdef ENABLE_IPV6
  614. struct in6_addr addr;
  615. #else
  616. struct in_addr addr;
  617. #endif
  618. /*SSLConnectionRef ssl_connection;*/
  619. OSStatus err = noErr;
  620. #if defined(__MAC_10_8) || defined(__IPHONE_5_0)
  621. if(SSLCreateContext != NULL) { /* use the newer API if avaialble */
  622. if(connssl->ssl_ctx)
  623. CFRelease(connssl->ssl_ctx);
  624. connssl->ssl_ctx = SSLCreateContext(NULL, kSSLClientSide, kSSLStreamType);
  625. if(!connssl->ssl_ctx) {
  626. failf(data, "SSL: couldn't create a context!");
  627. return CURLE_OUT_OF_MEMORY;
  628. }
  629. }
  630. else {
  631. /* The old ST API does not exist under iOS, so don't compile it: */
  632. #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
  633. if(connssl->ssl_ctx)
  634. (void)SSLDisposeContext(connssl->ssl_ctx);
  635. err = SSLNewContext(false, &(connssl->ssl_ctx));
  636. if(err != noErr) {
  637. failf(data, "SSL: couldn't create a context: OSStatus %d", err);
  638. return CURLE_OUT_OF_MEMORY;
  639. }
  640. #endif /* (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) */
  641. }
  642. #else
  643. if(connssl->ssl_ctx)
  644. (void)SSLDisposeContext(connssl->ssl_ctx);
  645. err = SSLNewContext(false, &(connssl->ssl_ctx));
  646. if(err != noErr) {
  647. failf(data, "SSL: couldn't create a context: OSStatus %d", err);
  648. return CURLE_OUT_OF_MEMORY;
  649. }
  650. #endif /* defined(__MAC_10_8) || defined(__IPHONE_5_0) */
  651. /* check to see if we've been told to use an explicit SSL/TLS version */
  652. #if defined(__MAC_10_8) || defined(__IPHONE_5_0)
  653. if(SSLSetProtocolVersionMax != NULL) {
  654. switch(data->set.ssl.version) {
  655. case CURL_SSLVERSION_DEFAULT: default:
  656. (void)SSLSetProtocolVersionMin(connssl->ssl_ctx, kSSLProtocol3);
  657. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kTLSProtocol12);
  658. break;
  659. case CURL_SSLVERSION_TLSv1:
  660. (void)SSLSetProtocolVersionMin(connssl->ssl_ctx, kTLSProtocol1);
  661. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kTLSProtocol12);
  662. break;
  663. case CURL_SSLVERSION_SSLv3:
  664. (void)SSLSetProtocolVersionMin(connssl->ssl_ctx, kSSLProtocol3);
  665. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kSSLProtocol3);
  666. break;
  667. case CURL_SSLVERSION_SSLv2:
  668. (void)SSLSetProtocolVersionMin(connssl->ssl_ctx, kSSLProtocol2);
  669. (void)SSLSetProtocolVersionMax(connssl->ssl_ctx, kSSLProtocol2);
  670. }
  671. }
  672. else {
  673. #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
  674. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  675. kSSLProtocolAll,
  676. false);
  677. switch (data->set.ssl.version) {
  678. case CURL_SSLVERSION_DEFAULT: default:
  679. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  680. kSSLProtocol3,
  681. true);
  682. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  683. kTLSProtocol1,
  684. true);
  685. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  686. kTLSProtocol11,
  687. true);
  688. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  689. kTLSProtocol12,
  690. true);
  691. break;
  692. case CURL_SSLVERSION_TLSv1:
  693. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  694. kTLSProtocol1,
  695. true);
  696. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  697. kTLSProtocol11,
  698. true);
  699. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  700. kTLSProtocol12,
  701. true);
  702. break;
  703. case CURL_SSLVERSION_SSLv3:
  704. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  705. kSSLProtocol3,
  706. true);
  707. break;
  708. case CURL_SSLVERSION_SSLv2:
  709. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  710. kSSLProtocol2,
  711. true);
  712. break;
  713. }
  714. #endif /* (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) */
  715. }
  716. #else
  717. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx, kSSLProtocolAll, false);
  718. switch(data->set.ssl.version) {
  719. default:
  720. case CURL_SSLVERSION_DEFAULT:
  721. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  722. kSSLProtocol3,
  723. true);
  724. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  725. kTLSProtocol1,
  726. true);
  727. break;
  728. case CURL_SSLVERSION_TLSv1:
  729. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  730. kTLSProtocol1,
  731. true);
  732. break;
  733. case CURL_SSLVERSION_SSLv2:
  734. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  735. kSSLProtocol2,
  736. true);
  737. break;
  738. case CURL_SSLVERSION_SSLv3:
  739. (void)SSLSetProtocolVersionEnabled(connssl->ssl_ctx,
  740. kSSLProtocol3,
  741. true);
  742. break;
  743. }
  744. #endif /* defined(__MAC_10_8) || defined(__IPHONE_5_0) */
  745. /* No need to load certificates here. SecureTransport uses the Keychain
  746. * (which is also part of the Security framework) to evaluate trust. */
  747. /* SSL always tries to verify the peer, this only says whether it should
  748. * fail to connect if the verification fails, or if it should continue
  749. * anyway. In the latter case the result of the verification is checked with
  750. * SSL_get_verify_result() below. */
  751. #if defined(__MAC_10_6) || defined(__IPHONE_5_0)
  752. if(SSLSetSessionOption != NULL) {
  753. err = SSLSetSessionOption(connssl->ssl_ctx,
  754. kSSLSessionOptionBreakOnServerAuth,
  755. data->set.ssl.verifypeer?false:true);
  756. if(err != noErr) {
  757. failf(data, "SSL: SSLSetSessionOption() failed: OSStatus %d", err);
  758. return CURLE_SSL_CONNECT_ERROR;
  759. }
  760. }
  761. else {
  762. #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
  763. err = SSLSetEnableCertVerify(connssl->ssl_ctx,
  764. data->set.ssl.verifypeer?true:false);
  765. if(err != noErr) {
  766. failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err);
  767. return CURLE_SSL_CONNECT_ERROR;
  768. }
  769. #endif /* (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) */
  770. }
  771. #else
  772. err = SSLSetEnableCertVerify(connssl->ssl_ctx,
  773. data->set.ssl.verifypeer?true:false);
  774. if(err != noErr) {
  775. failf(data, "SSL: SSLSetEnableCertVerify() failed: OSStatus %d", err);
  776. return CURLE_SSL_CONNECT_ERROR;
  777. }
  778. #endif /* defined(__MAC_10_6) || defined(__IPHONE_5_0) */
  779. /* If this is a domain name and not an IP address, then configure SNI: */
  780. if((0 == Curl_inet_pton(AF_INET, conn->host.name, &addr)) &&
  781. #ifdef ENABLE_IPV6
  782. (0 == Curl_inet_pton(AF_INET6, conn->host.name, &addr)) &&
  783. #endif
  784. sni) {
  785. err = SSLSetPeerDomainName(connssl->ssl_ctx, conn->host.name,
  786. strlen(conn->host.name));
  787. if(err != noErr) {
  788. infof(data, "WARNING: SSL: SSLSetPeerDomainName() failed: OSStatus %d",
  789. err);
  790. }
  791. }
  792. err = SSLSetIOFuncs(connssl->ssl_ctx, SocketRead, SocketWrite);
  793. if(err != noErr) {
  794. failf(data, "SSL: SSLSetIOFuncs() failed: OSStatus %d", err);
  795. return CURLE_SSL_CONNECT_ERROR;
  796. }
  797. /* pass the raw socket into the SSL layers */
  798. /* We need to store the FD in a constant memory address, because
  799. * SSLSetConnection() will not copy that address. I've found that
  800. * conn->sock[sockindex] may change on its own. */
  801. connssl->ssl_sockfd = sockfd;
  802. /*ssl_connection = &(connssl->ssl_sockfd);
  803. err = SSLSetConnection(connssl->ssl_ctx, ssl_connection);*/
  804. err = SSLSetConnection(connssl->ssl_ctx, connssl);
  805. if(err != noErr) {
  806. failf(data, "SSL: SSLSetConnection() failed: %d", err);
  807. return CURLE_SSL_CONNECT_ERROR;
  808. }
  809. connssl->connecting_state = ssl_connect_2;
  810. return CURLE_OK;
  811. }
  812. static CURLcode
  813. darwinssl_connect_step2(struct connectdata *conn, int sockindex)
  814. {
  815. struct SessionHandle *data = conn->data;
  816. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  817. OSStatus err;
  818. SSLCipherSuite cipher;
  819. SSLProtocol protocol = 0;
  820. DEBUGASSERT(ssl_connect_2 == connssl->connecting_state
  821. || ssl_connect_2_reading == connssl->connecting_state
  822. || ssl_connect_2_writing == connssl->connecting_state);
  823. /* Here goes nothing: */
  824. err = SSLHandshake(connssl->ssl_ctx);
  825. if(err != noErr) {
  826. switch (err) {
  827. case errSSLWouldBlock: /* they're not done with us yet */
  828. connssl->connecting_state = connssl->ssl_direction ?
  829. ssl_connect_2_writing : ssl_connect_2_reading;
  830. return CURLE_OK;
  831. break;
  832. case errSSLServerAuthCompleted:
  833. /* the documentation says we need to call SSLHandshake() again */
  834. return darwinssl_connect_step2(conn, sockindex);
  835. case errSSLXCertChainInvalid:
  836. case errSSLUnknownRootCert:
  837. case errSSLNoRootCert:
  838. case errSSLCertExpired:
  839. failf(data, "SSL certificate problem: OSStatus %d", err);
  840. return CURLE_SSL_CACERT;
  841. break;
  842. default:
  843. failf(data, "Unknown SSL protocol error in connection to %s:%d",
  844. conn->host.name, err);
  845. return CURLE_SSL_CONNECT_ERROR;
  846. break;
  847. }
  848. }
  849. else {
  850. /* we have been connected fine, we're not waiting for anything else. */
  851. connssl->connecting_state = ssl_connect_3;
  852. /* Informational message */
  853. (void)SSLGetNegotiatedCipher(connssl->ssl_ctx, &cipher);
  854. (void)SSLGetNegotiatedProtocolVersion(connssl->ssl_ctx, &protocol);
  855. switch (protocol) {
  856. case kSSLProtocol2:
  857. infof(data, "SSL 2.0 connection using %s\n",
  858. SSLCipherNameForNumber(cipher));
  859. break;
  860. case kSSLProtocol3:
  861. infof(data, "SSL 3.0 connection using %s\n",
  862. SSLCipherNameForNumber(cipher));
  863. break;
  864. case kTLSProtocol1:
  865. infof(data, "TLS 1.0 connection using %s\n",
  866. TLSCipherNameForNumber(cipher));
  867. break;
  868. #if defined(__MAC_10_8) || defined(__IPHONE_5_0)
  869. case kTLSProtocol11:
  870. infof(data, "TLS 1.1 connection using %s\n",
  871. TLSCipherNameForNumber(cipher));
  872. break;
  873. case kTLSProtocol12:
  874. infof(data, "TLS 1.2 connection using %s\n",
  875. TLSCipherNameForNumber(cipher));
  876. break;
  877. #endif
  878. default:
  879. infof(data, "Unknown protocol connection\n");
  880. break;
  881. }
  882. return CURLE_OK;
  883. }
  884. }
  885. static CURLcode
  886. darwinssl_connect_step3(struct connectdata *conn,
  887. int sockindex)
  888. {
  889. struct SessionHandle *data = conn->data;
  890. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  891. CFStringRef server_cert_summary;
  892. char server_cert_summary_c[128];
  893. CFArrayRef server_certs;
  894. SecCertificateRef server_cert;
  895. OSStatus err;
  896. CFIndex i, count;
  897. SecTrustRef trust;
  898. /* There is no step 3!
  899. * Well, okay, if verbose mode is on, let's print the details of the
  900. * server certificates. */
  901. #if defined(__MAC_10_7) || defined(__IPHONE_5_0)
  902. #if (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)
  903. #pragma unused(server_certs)
  904. err = SSLCopyPeerTrust(connssl->ssl_ctx, &trust);
  905. if(err == noErr) {
  906. count = SecTrustGetCertificateCount(trust);
  907. for(i = 0L ; i < count ; i++) {
  908. server_cert = SecTrustGetCertificateAtIndex(trust, i);
  909. server_cert_summary = SecCertificateCopySubjectSummary(server_cert);
  910. memset(server_cert_summary_c, 0, 128);
  911. if(CFStringGetCString(server_cert_summary,
  912. server_cert_summary_c,
  913. 128,
  914. kCFStringEncodingUTF8)) {
  915. infof(data, "Server certificate: %s\n", server_cert_summary_c);
  916. }
  917. CFRelease(server_cert_summary);
  918. }
  919. CFRelease(trust);
  920. }
  921. #else
  922. /* SSLCopyPeerCertificates() is deprecated as of Mountain Lion.
  923. The function SecTrustGetCertificateAtIndex() is officially present
  924. in Lion, but it is unfortunately also present in Snow Leopard as
  925. private API and doesn't work as expected. So we have to look for
  926. a different symbol to make sure this code is only executed under
  927. Lion or later. */
  928. if(SecTrustEvaluateAsync != NULL) {
  929. #pragma unused(server_certs)
  930. err = SSLCopyPeerTrust(connssl->ssl_ctx, &trust);
  931. if(err == noErr) {
  932. count = SecTrustGetCertificateCount(trust);
  933. for(i = 0L ; i < count ; i++) {
  934. server_cert = SecTrustGetCertificateAtIndex(trust, i);
  935. server_cert_summary =
  936. SecCertificateCopyLongDescription(NULL, server_cert, NULL);
  937. memset(server_cert_summary_c, 0, 128);
  938. if(CFStringGetCString(server_cert_summary,
  939. server_cert_summary_c,
  940. 128,
  941. kCFStringEncodingUTF8)) {
  942. infof(data, "Server certificate: %s\n", server_cert_summary_c);
  943. }
  944. CFRelease(server_cert_summary);
  945. }
  946. CFRelease(trust);
  947. }
  948. }
  949. else {
  950. err = SSLCopyPeerCertificates(connssl->ssl_ctx, &server_certs);
  951. if(err == noErr) {
  952. count = CFArrayGetCount(server_certs);
  953. for(i = 0L ; i < count ; i++) {
  954. server_cert = (SecCertificateRef)CFArrayGetValueAtIndex(server_certs,
  955. i);
  956. server_cert_summary = SecCertificateCopySubjectSummary(server_cert);
  957. memset(server_cert_summary_c, 0, 128);
  958. if(CFStringGetCString(server_cert_summary,
  959. server_cert_summary_c,
  960. 128,
  961. kCFStringEncodingUTF8)) {
  962. infof(data, "Server certificate: %s\n", server_cert_summary_c);
  963. }
  964. CFRelease(server_cert_summary);
  965. }
  966. CFRelease(server_certs);
  967. }
  968. }
  969. #endif /* (TARGET_OS_EMBEDDED || TARGET_OS_IPHONE) */
  970. #else
  971. #pragma unused(trust)
  972. err = SSLCopyPeerCertificates(connssl->ssl_ctx, &server_certs);
  973. if(err == noErr) {
  974. count = CFArrayGetCount(server_certs);
  975. for(i = 0L ; i < count ; i++) {
  976. server_cert = (SecCertificateRef)CFArrayGetValueAtIndex(server_certs, i);
  977. server_cert_summary = SecCertificateCopySubjectSummary(server_cert);
  978. memset(server_cert_summary_c, 0, 128);
  979. if(CFStringGetCString(server_cert_summary,
  980. server_cert_summary_c,
  981. 128,
  982. kCFStringEncodingUTF8)) {
  983. infof(data, "Server certificate: %s\n", server_cert_summary_c);
  984. }
  985. CFRelease(server_cert_summary);
  986. }
  987. CFRelease(server_certs);
  988. }
  989. #endif /* defined(__MAC_10_7) || defined(__IPHONE_5_0) */
  990. connssl->connecting_state = ssl_connect_done;
  991. return CURLE_OK;
  992. }
  993. static Curl_recv darwinssl_recv;
  994. static Curl_send darwinssl_send;
  995. static CURLcode
  996. darwinssl_connect_common(struct connectdata *conn,
  997. int sockindex,
  998. bool nonblocking,
  999. bool *done)
  1000. {
  1001. CURLcode retcode;
  1002. struct SessionHandle *data = conn->data;
  1003. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1004. curl_socket_t sockfd = conn->sock[sockindex];
  1005. long timeout_ms;
  1006. int what;
  1007. /* check if the connection has already been established */
  1008. if(ssl_connection_complete == connssl->state) {
  1009. *done = TRUE;
  1010. return CURLE_OK;
  1011. }
  1012. if(ssl_connect_1==connssl->connecting_state) {
  1013. /* Find out how much more time we're allowed */
  1014. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1015. if(timeout_ms < 0) {
  1016. /* no need to continue if time already is up */
  1017. failf(data, "SSL connection timeout");
  1018. return CURLE_OPERATION_TIMEDOUT;
  1019. }
  1020. retcode = darwinssl_connect_step1(conn, sockindex);
  1021. if(retcode)
  1022. return retcode;
  1023. }
  1024. while(ssl_connect_2 == connssl->connecting_state ||
  1025. ssl_connect_2_reading == connssl->connecting_state ||
  1026. ssl_connect_2_writing == connssl->connecting_state) {
  1027. /* check allowed time left */
  1028. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1029. if(timeout_ms < 0) {
  1030. /* no need to continue if time already is up */
  1031. failf(data, "SSL connection timeout");
  1032. return CURLE_OPERATION_TIMEDOUT;
  1033. }
  1034. /* if ssl is expecting something, check if it's available. */
  1035. if(connssl->connecting_state == ssl_connect_2_reading
  1036. || connssl->connecting_state == ssl_connect_2_writing) {
  1037. curl_socket_t writefd = ssl_connect_2_writing ==
  1038. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  1039. curl_socket_t readfd = ssl_connect_2_reading ==
  1040. connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
  1041. what = Curl_socket_ready(readfd, writefd, nonblocking?0:timeout_ms);
  1042. if(what < 0) {
  1043. /* fatal error */
  1044. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1045. return CURLE_SSL_CONNECT_ERROR;
  1046. }
  1047. else if(0 == what) {
  1048. if(nonblocking) {
  1049. *done = FALSE;
  1050. return CURLE_OK;
  1051. }
  1052. else {
  1053. /* timeout */
  1054. failf(data, "SSL connection timeout");
  1055. return CURLE_OPERATION_TIMEDOUT;
  1056. }
  1057. }
  1058. /* socket is readable or writable */
  1059. }
  1060. /* Run transaction, and return to the caller if it failed or if this
  1061. * connection is done nonblocking and this loop would execute again. This
  1062. * permits the owner of a multi handle to abort a connection attempt
  1063. * before step2 has completed while ensuring that a client using select()
  1064. * or epoll() will always have a valid fdset to wait on.
  1065. */
  1066. retcode = darwinssl_connect_step2(conn, sockindex);
  1067. if(retcode || (nonblocking &&
  1068. (ssl_connect_2 == connssl->connecting_state ||
  1069. ssl_connect_2_reading == connssl->connecting_state ||
  1070. ssl_connect_2_writing == connssl->connecting_state)))
  1071. return retcode;
  1072. } /* repeat step2 until all transactions are done. */
  1073. if(ssl_connect_3==connssl->connecting_state) {
  1074. retcode = darwinssl_connect_step3(conn, sockindex);
  1075. if(retcode)
  1076. return retcode;
  1077. }
  1078. if(ssl_connect_done==connssl->connecting_state) {
  1079. connssl->state = ssl_connection_complete;
  1080. conn->recv[sockindex] = darwinssl_recv;
  1081. conn->send[sockindex] = darwinssl_send;
  1082. *done = TRUE;
  1083. }
  1084. else
  1085. *done = FALSE;
  1086. /* Reset our connect state machine */
  1087. connssl->connecting_state = ssl_connect_1;
  1088. return CURLE_OK;
  1089. }
  1090. CURLcode
  1091. Curl_darwinssl_connect_nonblocking(struct connectdata *conn,
  1092. int sockindex,
  1093. bool *done)
  1094. {
  1095. return darwinssl_connect_common(conn, sockindex, TRUE, done);
  1096. }
  1097. CURLcode
  1098. Curl_darwinssl_connect(struct connectdata *conn,
  1099. int sockindex)
  1100. {
  1101. CURLcode retcode;
  1102. bool done = FALSE;
  1103. retcode = darwinssl_connect_common(conn, sockindex, FALSE, &done);
  1104. if(retcode)
  1105. return retcode;
  1106. DEBUGASSERT(done);
  1107. return CURLE_OK;
  1108. }
  1109. void Curl_darwinssl_close(struct connectdata *conn, int sockindex)
  1110. {
  1111. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1112. if(connssl->ssl_ctx) {
  1113. (void)SSLClose(connssl->ssl_ctx);
  1114. #if defined(__MAC_10_8) || defined(__IPHONE_5_0)
  1115. if(SSLCreateContext != NULL)
  1116. CFRelease(connssl->ssl_ctx);
  1117. #if (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE))
  1118. else
  1119. (void)SSLDisposeContext(connssl->ssl_ctx);
  1120. #endif /* (TARGET_OS_MAC && !(TARGET_OS_EMBEDDED || TARGET_OS_IPHONE)) */
  1121. #else
  1122. (void)SSLDisposeContext(connssl->ssl_ctx);
  1123. #endif /* defined(__MAC_10_8) || defined(__IPHONE_5_0) */
  1124. connssl->ssl_ctx = NULL;
  1125. }
  1126. connssl->ssl_sockfd = 0;
  1127. }
  1128. void Curl_darwinssl_close_all(struct SessionHandle *data)
  1129. {
  1130. /* SecureTransport doesn't separate sessions from contexts, so... */
  1131. (void)data;
  1132. }
  1133. int Curl_darwinssl_shutdown(struct connectdata *conn, int sockindex)
  1134. {
  1135. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1136. struct SessionHandle *data = conn->data;
  1137. ssize_t nread;
  1138. int what;
  1139. int rc;
  1140. char buf[120];
  1141. if(!connssl->ssl_ctx)
  1142. return 0;
  1143. if(data->set.ftp_ccc != CURLFTPSSL_CCC_ACTIVE)
  1144. return 0;
  1145. Curl_darwinssl_close(conn, sockindex);
  1146. rc = 0;
  1147. what = Curl_socket_ready(conn->sock[sockindex],
  1148. CURL_SOCKET_BAD, SSL_SHUTDOWN_TIMEOUT);
  1149. for(;;) {
  1150. if(what < 0) {
  1151. /* anything that gets here is fatally bad */
  1152. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1153. rc = -1;
  1154. break;
  1155. }
  1156. if(!what) { /* timeout */
  1157. failf(data, "SSL shutdown timeout");
  1158. break;
  1159. }
  1160. /* Something to read, let's do it and hope that it is the close
  1161. notify alert from the server. No way to SSL_Read now, so use read(). */
  1162. nread = read(conn->sock[sockindex], buf, sizeof(buf));
  1163. if(nread < 0) {
  1164. failf(data, "read: %s", strerror(errno));
  1165. rc = -1;
  1166. }
  1167. if(nread <= 0)
  1168. break;
  1169. what = Curl_socket_ready(conn->sock[sockindex], CURL_SOCKET_BAD, 0);
  1170. }
  1171. return rc;
  1172. }
  1173. size_t Curl_darwinssl_version(char *buffer, size_t size)
  1174. {
  1175. return snprintf(buffer, size, "SecureTransport");
  1176. }
  1177. /*
  1178. * This function uses SSLGetSessionState to determine connection status.
  1179. *
  1180. * Return codes:
  1181. * 1 means the connection is still in place
  1182. * 0 means the connection has been closed
  1183. * -1 means the connection status is unknown
  1184. */
  1185. int Curl_darwinssl_check_cxn(struct connectdata *conn)
  1186. {
  1187. struct ssl_connect_data *connssl = &conn->ssl[FIRSTSOCKET];
  1188. OSStatus err;
  1189. SSLSessionState state;
  1190. if(connssl->ssl_ctx) {
  1191. err = SSLGetSessionState(connssl->ssl_ctx, &state);
  1192. if(err == noErr)
  1193. return state == kSSLConnected || state == kSSLHandshake;
  1194. return -1;
  1195. }
  1196. return 0;
  1197. }
  1198. bool Curl_darwinssl_data_pending(const struct connectdata *conn,
  1199. int connindex)
  1200. {
  1201. const struct ssl_connect_data *connssl = &conn->ssl[connindex];
  1202. OSStatus err;
  1203. size_t buffer;
  1204. if(connssl->ssl_ctx) { /* SSL is in use */
  1205. err = SSLGetBufferedReadSize(connssl->ssl_ctx, &buffer);
  1206. if(err == noErr)
  1207. return buffer > 0UL;
  1208. return false;
  1209. }
  1210. else
  1211. return false;
  1212. }
  1213. void Curl_darwinssl_random(struct SessionHandle *data,
  1214. unsigned char *entropy,
  1215. size_t length)
  1216. {
  1217. /* arc4random_buf() isn't available on cats older than Lion, so let's
  1218. do this manually for the benefit of the older cats. */
  1219. size_t i;
  1220. u_int32_t random_number = 0;
  1221. for(i = 0 ; i < length ; i++) {
  1222. if(i % sizeof(u_int32_t) == 0)
  1223. random_number = arc4random();
  1224. entropy[i] = random_number & 0xFF;
  1225. random_number >>= 8;
  1226. }
  1227. i = random_number = 0;
  1228. (void)data;
  1229. }
  1230. void Curl_darwinssl_md5sum(unsigned char *tmp, /* input */
  1231. size_t tmplen,
  1232. unsigned char *md5sum, /* output */
  1233. size_t md5len)
  1234. {
  1235. (void)md5len;
  1236. (void)CC_MD5(tmp, tmplen, md5sum);
  1237. }
  1238. static ssize_t darwinssl_send(struct connectdata *conn,
  1239. int sockindex,
  1240. const void *mem,
  1241. size_t len,
  1242. CURLcode *curlcode)
  1243. {
  1244. /*struct SessionHandle *data = conn->data;*/
  1245. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  1246. size_t processed;
  1247. OSStatus err = SSLWrite(connssl->ssl_ctx, mem, len, &processed);
  1248. if(err != noErr) {
  1249. switch (err) {
  1250. case errSSLWouldBlock: /* we're not done yet; keep sending */
  1251. *curlcode = CURLE_AGAIN;
  1252. return -1;
  1253. break;
  1254. default:
  1255. failf(conn->data, "SSLWrite() return error %d", err);
  1256. *curlcode = CURLE_SEND_ERROR;
  1257. return -1;
  1258. break;
  1259. }
  1260. }
  1261. return (ssize_t)processed;
  1262. }
  1263. static ssize_t darwinssl_recv(struct connectdata *conn,
  1264. int num,
  1265. char *buf,
  1266. size_t buffersize,
  1267. CURLcode *curlcode)
  1268. {
  1269. /*struct SessionHandle *data = conn->data;*/
  1270. struct ssl_connect_data *connssl = &conn->ssl[num];
  1271. size_t processed;
  1272. OSStatus err = SSLRead(connssl->ssl_ctx, buf, buffersize, &processed);
  1273. if(err != noErr) {
  1274. switch (err) {
  1275. case errSSLWouldBlock: /* we're not done yet; keep reading */
  1276. *curlcode = CURLE_AGAIN;
  1277. return -1;
  1278. break;
  1279. case errSSLClosedGraceful: /* they're done; fail gracefully */
  1280. *curlcode = CURLE_OK;
  1281. return -1;
  1282. break;
  1283. default:
  1284. failf(conn->data, "SSLRead() return error %d", err);
  1285. *curlcode = CURLE_RECV_ERROR;
  1286. return -1;
  1287. break;
  1288. }
  1289. }
  1290. return (ssize_t)processed;
  1291. }
  1292. #endif /* USE_DARWINSSL */