gskit.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifdef USE_GSKIT
  26. #include <gskssl.h>
  27. #include <qsoasync.h>
  28. #undef HAVE_SOCKETPAIR /* because the native one isn't good enough */
  29. #include "socketpair.h"
  30. #include "strerror.h"
  31. /* Some symbols are undefined/unsupported on OS400 versions < V7R1. */
  32. #ifndef GSK_SSL_EXTN_SERVERNAME_REQUEST
  33. #define GSK_SSL_EXTN_SERVERNAME_REQUEST 230
  34. #endif
  35. #ifndef GSK_TLSV10_CIPHER_SPECS
  36. #define GSK_TLSV10_CIPHER_SPECS 236
  37. #endif
  38. #ifndef GSK_TLSV11_CIPHER_SPECS
  39. #define GSK_TLSV11_CIPHER_SPECS 237
  40. #endif
  41. #ifndef GSK_TLSV12_CIPHER_SPECS
  42. #define GSK_TLSV12_CIPHER_SPECS 238
  43. #endif
  44. #ifndef GSK_PROTOCOL_TLSV11
  45. #define GSK_PROTOCOL_TLSV11 437
  46. #endif
  47. #ifndef GSK_PROTOCOL_TLSV12
  48. #define GSK_PROTOCOL_TLSV12 438
  49. #endif
  50. #ifndef GSK_FALSE
  51. #define GSK_FALSE 0
  52. #endif
  53. #ifndef GSK_TRUE
  54. #define GSK_TRUE 1
  55. #endif
  56. #include <limits.h>
  57. #include <curl/curl.h>
  58. #include "urldata.h"
  59. #include "sendf.h"
  60. #include "gskit.h"
  61. #include "vtls.h"
  62. #include "vtls_int.h"
  63. #include "connect.h" /* for the connect timeout */
  64. #include "select.h"
  65. #include "strcase.h"
  66. #include "timediff.h"
  67. #include "x509asn1.h"
  68. #include "curl_printf.h"
  69. #include "curl_memory.h"
  70. /* The last #include file should be: */
  71. #include "memdebug.h"
  72. /* Directions. */
  73. #define SOS_READ 0x01
  74. #define SOS_WRITE 0x02
  75. /* SSL version flags. */
  76. #define CURL_GSKPROTO_SSLV2 0
  77. #define CURL_GSKPROTO_SSLV2_MASK (1 << CURL_GSKPROTO_SSLV2)
  78. #define CURL_GSKPROTO_SSLV3 1
  79. #define CURL_GSKPROTO_SSLV3_MASK (1 << CURL_GSKPROTO_SSLV3)
  80. #define CURL_GSKPROTO_TLSV10 2
  81. #define CURL_GSKPROTO_TLSV10_MASK (1 << CURL_GSKPROTO_TLSV10)
  82. #define CURL_GSKPROTO_TLSV11 3
  83. #define CURL_GSKPROTO_TLSV11_MASK (1 << CURL_GSKPROTO_TLSV11)
  84. #define CURL_GSKPROTO_TLSV12 4
  85. #define CURL_GSKPROTO_TLSV12_MASK (1 << CURL_GSKPROTO_TLSV12)
  86. #define CURL_GSKPROTO_LAST 5
  87. struct ssl_backend_data {
  88. gsk_handle handle;
  89. int iocport;
  90. int localfd;
  91. int remotefd;
  92. };
  93. #define BACKEND connssl->backend
  94. /* Supported ciphers. */
  95. struct gskit_cipher {
  96. const char *name; /* Cipher name. */
  97. const char *gsktoken; /* Corresponding token for GSKit String. */
  98. unsigned int versions; /* SSL version flags. */
  99. };
  100. static const struct gskit_cipher ciphertable[] = {
  101. { "null-md5", "01",
  102. CURL_GSKPROTO_SSLV3_MASK | CURL_GSKPROTO_TLSV10_MASK |
  103. CURL_GSKPROTO_TLSV11_MASK | CURL_GSKPROTO_TLSV12_MASK },
  104. { "null-sha", "02",
  105. CURL_GSKPROTO_SSLV3_MASK | CURL_GSKPROTO_TLSV10_MASK |
  106. CURL_GSKPROTO_TLSV11_MASK | CURL_GSKPROTO_TLSV12_MASK },
  107. { "exp-rc4-md5", "03",
  108. CURL_GSKPROTO_SSLV3_MASK | CURL_GSKPROTO_TLSV10_MASK },
  109. { "rc4-md5", "04",
  110. CURL_GSKPROTO_SSLV3_MASK | CURL_GSKPROTO_TLSV10_MASK |
  111. CURL_GSKPROTO_TLSV11_MASK | CURL_GSKPROTO_TLSV12_MASK },
  112. { "rc4-sha", "05",
  113. CURL_GSKPROTO_SSLV3_MASK | CURL_GSKPROTO_TLSV10_MASK |
  114. CURL_GSKPROTO_TLSV11_MASK | CURL_GSKPROTO_TLSV12_MASK },
  115. { "exp-rc2-cbc-md5", "06",
  116. CURL_GSKPROTO_SSLV3_MASK | CURL_GSKPROTO_TLSV10_MASK },
  117. { "exp-des-cbc-sha", "09",
  118. CURL_GSKPROTO_SSLV3_MASK | CURL_GSKPROTO_TLSV10_MASK |
  119. CURL_GSKPROTO_TLSV11_MASK },
  120. { "des-cbc3-sha", "0A",
  121. CURL_GSKPROTO_SSLV3_MASK | CURL_GSKPROTO_TLSV10_MASK |
  122. CURL_GSKPROTO_TLSV11_MASK | CURL_GSKPROTO_TLSV12_MASK },
  123. { "aes128-sha", "2F",
  124. CURL_GSKPROTO_TLSV10_MASK | CURL_GSKPROTO_TLSV11_MASK |
  125. CURL_GSKPROTO_TLSV12_MASK },
  126. { "aes256-sha", "35",
  127. CURL_GSKPROTO_TLSV10_MASK | CURL_GSKPROTO_TLSV11_MASK |
  128. CURL_GSKPROTO_TLSV12_MASK },
  129. { "null-sha256", "3B", CURL_GSKPROTO_TLSV12_MASK },
  130. { "aes128-sha256", "3C", CURL_GSKPROTO_TLSV12_MASK },
  131. { "aes256-sha256", "3D", CURL_GSKPROTO_TLSV12_MASK },
  132. { "aes128-gcm-sha256",
  133. "9C", CURL_GSKPROTO_TLSV12_MASK },
  134. { "aes256-gcm-sha384",
  135. "9D", CURL_GSKPROTO_TLSV12_MASK },
  136. { "rc4-md5", "1", CURL_GSKPROTO_SSLV2_MASK },
  137. { "exp-rc4-md5", "2", CURL_GSKPROTO_SSLV2_MASK },
  138. { "rc2-md5", "3", CURL_GSKPROTO_SSLV2_MASK },
  139. { "exp-rc2-md5", "4", CURL_GSKPROTO_SSLV2_MASK },
  140. { "des-cbc-md5", "6", CURL_GSKPROTO_SSLV2_MASK },
  141. { "des-cbc3-md5", "7", CURL_GSKPROTO_SSLV2_MASK },
  142. { (const char *) NULL, (const char *) NULL, 0 }
  143. };
  144. static bool is_separator(char c)
  145. {
  146. /* Return whether character is a cipher list separator. */
  147. switch(c) {
  148. case ' ':
  149. case '\t':
  150. case ':':
  151. case ',':
  152. case ';':
  153. return true;
  154. }
  155. return false;
  156. }
  157. static CURLcode gskit_status(struct Curl_easy *data, int rc,
  158. const char *procname, CURLcode defcode)
  159. {
  160. char buffer[STRERROR_LEN];
  161. /* Process GSKit status and map it to a CURLcode. */
  162. switch(rc) {
  163. case GSK_OK:
  164. case GSK_OS400_ASYNCHRONOUS_SOC_INIT:
  165. return CURLE_OK;
  166. case GSK_KEYRING_OPEN_ERROR:
  167. case GSK_OS400_ERROR_NO_ACCESS:
  168. return CURLE_SSL_CACERT_BADFILE;
  169. case GSK_INSUFFICIENT_STORAGE:
  170. return CURLE_OUT_OF_MEMORY;
  171. case GSK_ERROR_BAD_V2_CIPHER:
  172. case GSK_ERROR_BAD_V3_CIPHER:
  173. case GSK_ERROR_NO_CIPHERS:
  174. return CURLE_SSL_CIPHER;
  175. case GSK_OS400_ERROR_NOT_TRUSTED_ROOT:
  176. case GSK_ERROR_CERT_VALIDATION:
  177. return CURLE_PEER_FAILED_VERIFICATION;
  178. case GSK_OS400_ERROR_TIMED_OUT:
  179. return CURLE_OPERATION_TIMEDOUT;
  180. case GSK_WOULD_BLOCK:
  181. return CURLE_AGAIN;
  182. case GSK_OS400_ERROR_NOT_REGISTERED:
  183. break;
  184. case GSK_ERROR_IO:
  185. switch(errno) {
  186. case ENOMEM:
  187. return CURLE_OUT_OF_MEMORY;
  188. default:
  189. failf(data, "%s I/O error: %s", procname,
  190. Curl_strerror(errno, buffer, sizeof(buffer)));
  191. break;
  192. }
  193. break;
  194. default:
  195. failf(data, "%s: %s", procname, gsk_strerror(rc));
  196. break;
  197. }
  198. return defcode;
  199. }
  200. static CURLcode set_enum(struct Curl_easy *data, gsk_handle h,
  201. GSK_ENUM_ID id, GSK_ENUM_VALUE value, bool unsupported_ok)
  202. {
  203. char buffer[STRERROR_LEN];
  204. int rc = gsk_attribute_set_enum(h, id, value);
  205. switch(rc) {
  206. case GSK_OK:
  207. return CURLE_OK;
  208. case GSK_ERROR_IO:
  209. failf(data, "gsk_attribute_set_enum() I/O error: %s",
  210. Curl_strerror(errno, buffer, sizeof(buffer)));
  211. break;
  212. case GSK_ATTRIBUTE_INVALID_ID:
  213. if(unsupported_ok)
  214. return CURLE_UNSUPPORTED_PROTOCOL;
  215. default:
  216. failf(data, "gsk_attribute_set_enum(): %s", gsk_strerror(rc));
  217. break;
  218. }
  219. return CURLE_SSL_CONNECT_ERROR;
  220. }
  221. static CURLcode set_buffer(struct Curl_easy *data, gsk_handle h,
  222. GSK_BUF_ID id, const char *buf, bool unsupported_ok)
  223. {
  224. char buffer[STRERROR_LEN];
  225. int rc = gsk_attribute_set_buffer(h, id, buf, 0);
  226. switch(rc) {
  227. case GSK_OK:
  228. return CURLE_OK;
  229. case GSK_ERROR_IO:
  230. failf(data, "gsk_attribute_set_buffer() I/O error: %s",
  231. Curl_strerror(errno, buffer, sizeof(buffer)));
  232. break;
  233. case GSK_ATTRIBUTE_INVALID_ID:
  234. if(unsupported_ok)
  235. return CURLE_UNSUPPORTED_PROTOCOL;
  236. default:
  237. failf(data, "gsk_attribute_set_buffer(): %s", gsk_strerror(rc));
  238. break;
  239. }
  240. return CURLE_SSL_CONNECT_ERROR;
  241. }
  242. static CURLcode set_numeric(struct Curl_easy *data,
  243. gsk_handle h, GSK_NUM_ID id, int value)
  244. {
  245. char buffer[STRERROR_LEN];
  246. int rc = gsk_attribute_set_numeric_value(h, id, value);
  247. switch(rc) {
  248. case GSK_OK:
  249. return CURLE_OK;
  250. case GSK_ERROR_IO:
  251. failf(data, "gsk_attribute_set_numeric_value() I/O error: %s",
  252. Curl_strerror(errno, buffer, sizeof(buffer)));
  253. break;
  254. default:
  255. failf(data, "gsk_attribute_set_numeric_value(): %s", gsk_strerror(rc));
  256. break;
  257. }
  258. return CURLE_SSL_CONNECT_ERROR;
  259. }
  260. static CURLcode set_ciphers(struct Curl_cfilter *cf, struct Curl_easy *data,
  261. gsk_handle h, unsigned int *protoflags)
  262. {
  263. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  264. struct connectdata *conn = data->conn;
  265. const char *cipherlist = conn_config->cipher_list;
  266. const char *clp;
  267. const struct gskit_cipher *ctp;
  268. int i;
  269. int l;
  270. bool unsupported;
  271. CURLcode result;
  272. struct {
  273. char *buf;
  274. char *ptr;
  275. } ciphers[CURL_GSKPROTO_LAST];
  276. /* Compile cipher list into GSKit-compatible cipher lists. */
  277. if(!cipherlist)
  278. return CURLE_OK;
  279. while(is_separator(*cipherlist)) /* Skip initial separators. */
  280. cipherlist++;
  281. if(!*cipherlist)
  282. return CURLE_OK;
  283. /* We allocate GSKit buffers of the same size as the input string: since
  284. GSKit tokens are always shorter than their cipher names, allocated buffers
  285. will always be large enough to accommodate the result. */
  286. l = strlen(cipherlist) + 1;
  287. memset(ciphers, 0, sizeof(ciphers));
  288. for(i = 0; i < CURL_GSKPROTO_LAST; i++) {
  289. ciphers[i].buf = malloc(l);
  290. if(!ciphers[i].buf) {
  291. while(i--)
  292. free(ciphers[i].buf);
  293. return CURLE_OUT_OF_MEMORY;
  294. }
  295. ciphers[i].ptr = ciphers[i].buf;
  296. *ciphers[i].ptr = '\0';
  297. }
  298. /* Process each cipher in input string. */
  299. unsupported = FALSE;
  300. result = CURLE_OK;
  301. for(;;) {
  302. for(clp = cipherlist; *cipherlist && !is_separator(*cipherlist);)
  303. cipherlist++;
  304. l = cipherlist - clp;
  305. if(!l)
  306. break;
  307. /* Search the cipher in our table. */
  308. for(ctp = ciphertable; ctp->name; ctp++)
  309. if(strncasecompare(ctp->name, clp, l) && !ctp->name[l])
  310. break;
  311. if(!ctp->name) {
  312. failf(data, "Unknown cipher %.*s", l, clp);
  313. result = CURLE_SSL_CIPHER;
  314. }
  315. else {
  316. unsupported |= !(ctp->versions & (CURL_GSKPROTO_SSLV2_MASK |
  317. CURL_GSKPROTO_SSLV3_MASK | CURL_GSKPROTO_TLSV10_MASK));
  318. for(i = 0; i < CURL_GSKPROTO_LAST; i++) {
  319. if(ctp->versions & (1 << i)) {
  320. strcpy(ciphers[i].ptr, ctp->gsktoken);
  321. ciphers[i].ptr += strlen(ctp->gsktoken);
  322. }
  323. }
  324. }
  325. /* Advance to next cipher name or end of string. */
  326. while(is_separator(*cipherlist))
  327. cipherlist++;
  328. }
  329. /* Disable protocols with empty cipher lists. */
  330. for(i = 0; i < CURL_GSKPROTO_LAST; i++) {
  331. if(!(*protoflags & (1 << i)) || !ciphers[i].buf[0]) {
  332. *protoflags &= ~(1 << i);
  333. ciphers[i].buf[0] = '\0';
  334. }
  335. }
  336. /* Try to set-up TLSv1.1 and TLSv2.1 ciphers. */
  337. if(*protoflags & CURL_GSKPROTO_TLSV11_MASK) {
  338. result = set_buffer(data, h, GSK_TLSV11_CIPHER_SPECS,
  339. ciphers[CURL_GSKPROTO_TLSV11].buf, TRUE);
  340. if(result == CURLE_UNSUPPORTED_PROTOCOL) {
  341. result = CURLE_OK;
  342. if(unsupported) {
  343. failf(data, "TLSv1.1-only ciphers are not yet supported");
  344. result = CURLE_SSL_CIPHER;
  345. }
  346. }
  347. }
  348. if(!result && (*protoflags & CURL_GSKPROTO_TLSV12_MASK)) {
  349. result = set_buffer(data, h, GSK_TLSV12_CIPHER_SPECS,
  350. ciphers[CURL_GSKPROTO_TLSV12].buf, TRUE);
  351. if(result == CURLE_UNSUPPORTED_PROTOCOL) {
  352. result = CURLE_OK;
  353. if(unsupported) {
  354. failf(data, "TLSv1.2-only ciphers are not yet supported");
  355. result = CURLE_SSL_CIPHER;
  356. }
  357. }
  358. }
  359. /* Try to set-up TLSv1.0 ciphers. If not successful, concatenate them to
  360. the SSLv3 ciphers. OS/400 prior to version 7.1 will understand it. */
  361. if(!result && (*protoflags & CURL_GSKPROTO_TLSV10_MASK)) {
  362. result = set_buffer(data, h, GSK_TLSV10_CIPHER_SPECS,
  363. ciphers[CURL_GSKPROTO_TLSV10].buf, TRUE);
  364. if(result == CURLE_UNSUPPORTED_PROTOCOL) {
  365. result = CURLE_OK;
  366. strcpy(ciphers[CURL_GSKPROTO_SSLV3].ptr,
  367. ciphers[CURL_GSKPROTO_TLSV10].ptr);
  368. }
  369. }
  370. /* Set-up other ciphers. */
  371. if(!result && (*protoflags & CURL_GSKPROTO_SSLV3_MASK))
  372. result = set_buffer(data, h, GSK_V3_CIPHER_SPECS,
  373. ciphers[CURL_GSKPROTO_SSLV3].buf, FALSE);
  374. if(!result && (*protoflags & CURL_GSKPROTO_SSLV2_MASK))
  375. result = set_buffer(data, h, GSK_V2_CIPHER_SPECS,
  376. ciphers[CURL_GSKPROTO_SSLV2].buf, FALSE);
  377. /* Clean-up. */
  378. for(i = 0; i < CURL_GSKPROTO_LAST; i++)
  379. free(ciphers[i].buf);
  380. return result;
  381. }
  382. static int gskit_init(void)
  383. {
  384. /* No initialization needed. */
  385. return 1;
  386. }
  387. static void gskit_cleanup(void)
  388. {
  389. /* Nothing to do. */
  390. }
  391. static CURLcode init_environment(struct Curl_easy *data,
  392. gsk_handle *envir, const char *appid,
  393. const char *file, const char *label,
  394. const char *password)
  395. {
  396. int rc;
  397. CURLcode result;
  398. gsk_handle h;
  399. /* Creates the GSKit environment. */
  400. rc = gsk_environment_open(&h);
  401. switch(rc) {
  402. case GSK_OK:
  403. break;
  404. case GSK_INSUFFICIENT_STORAGE:
  405. return CURLE_OUT_OF_MEMORY;
  406. default:
  407. failf(data, "gsk_environment_open(): %s", gsk_strerror(rc));
  408. return CURLE_SSL_CONNECT_ERROR;
  409. }
  410. result = set_enum(data, h, GSK_SESSION_TYPE, GSK_CLIENT_SESSION, FALSE);
  411. if(!result && appid)
  412. result = set_buffer(data, h, GSK_OS400_APPLICATION_ID, appid, FALSE);
  413. if(!result && file)
  414. result = set_buffer(data, h, GSK_KEYRING_FILE, file, FALSE);
  415. if(!result && label)
  416. result = set_buffer(data, h, GSK_KEYRING_LABEL, label, FALSE);
  417. if(!result && password)
  418. result = set_buffer(data, h, GSK_KEYRING_PW, password, FALSE);
  419. if(!result) {
  420. /* Locate CAs, Client certificate and key according to our settings.
  421. Note: this call may be blocking for some tenths of seconds. */
  422. result = gskit_status(data, gsk_environment_init(h),
  423. "gsk_environment_init()", CURLE_SSL_CERTPROBLEM);
  424. if(!result) {
  425. *envir = h;
  426. return result;
  427. }
  428. }
  429. /* Error: rollback. */
  430. gsk_environment_close(&h);
  431. return result;
  432. }
  433. static void cancel_async_handshake(struct Curl_cfilter *cf,
  434. struct Curl_easy *data)
  435. {
  436. struct ssl_connect_data *connssl = cf->ctx;
  437. Qso_OverlappedIO_t cstat;
  438. (void)data;
  439. DEBUGASSERT(BACKEND);
  440. if(QsoCancelOperation(Curl_conn_cf_get_socket(cf, data), 0) > 0)
  441. QsoWaitForIOCompletion(BACKEND->iocport, &cstat, (struct timeval *) NULL);
  442. }
  443. static void close_async_handshake(struct ssl_connect_data *connssl)
  444. {
  445. DEBUGASSERT(BACKEND);
  446. QsoDestroyIOCompletionPort(BACKEND->iocport);
  447. BACKEND->iocport = -1;
  448. }
  449. static int pipe_ssloverssl(struct Curl_cfilter *cf, struct Curl_easy *data,
  450. int directions)
  451. {
  452. struct ssl_connect_data *connssl = cf->ctx;
  453. struct Curl_cfilter *cf_ssl_next = Curl_ssl_cf_get_ssl(cf->next);
  454. struct ssl_connect_data *connssl_next = cf_ssl_next?
  455. cf_ssl_next->ctx : NULL;
  456. struct pollfd fds[2];
  457. int n;
  458. int m;
  459. int i;
  460. int ret = 0;
  461. char buf[CURL_MAX_WRITE_SIZE];
  462. DEBUGASSERT(BACKEND);
  463. if(!connssl_next)
  464. return 0; /* No SSL over SSL: OK. */
  465. DEBUGASSERT(connssl_next->backend);
  466. n = 1;
  467. fds[0].fd = BACKEND->remotefd;
  468. fds[1].fd = Curl_conn_cf_get_socket(cf, data);
  469. if(directions & SOS_READ) {
  470. fds[0].events |= POLLOUT;
  471. }
  472. if(directions & SOS_WRITE) {
  473. n = 2;
  474. fds[0].events |= POLLIN;
  475. fds[1].events |= POLLOUT;
  476. }
  477. i = Curl_poll(fds, n, 0);
  478. if(i < 0)
  479. return -1; /* Select error. */
  480. if(fds[0].revents & POLLOUT) {
  481. /* Try getting data from HTTPS proxy and pipe it upstream. */
  482. n = 0;
  483. i = gsk_secure_soc_read(connssl_next->backend->handle,
  484. buf, sizeof(buf), &n);
  485. switch(i) {
  486. case GSK_OK:
  487. if(n) {
  488. i = write(BACKEND->remotefd, buf, n);
  489. if(i < 0)
  490. return -1;
  491. ret = 1;
  492. }
  493. break;
  494. case GSK_OS400_ERROR_TIMED_OUT:
  495. case GSK_WOULD_BLOCK:
  496. break;
  497. default:
  498. return -1;
  499. }
  500. }
  501. if((fds[0].revents & POLLIN) && (fds[1].revents & POLLOUT)) {
  502. /* Pipe data to HTTPS proxy. */
  503. n = read(BACKEND->remotefd, buf, sizeof(buf));
  504. if(n < 0)
  505. return -1;
  506. if(n) {
  507. i = gsk_secure_soc_write(connssl_next->backend->handle, buf, n, &m);
  508. if(i != GSK_OK || n != m)
  509. return -1;
  510. ret = 1;
  511. }
  512. }
  513. return ret; /* OK */
  514. }
  515. static void close_one(struct Curl_cfilter *cf, struct Curl_easy *data)
  516. {
  517. struct ssl_connect_data *connssl = cf->ctx;
  518. DEBUGASSERT(BACKEND);
  519. if(BACKEND->handle) {
  520. gskit_status(data, gsk_secure_soc_close(&BACKEND->handle),
  521. "gsk_secure_soc_close()", 0);
  522. /* Last chance to drain output. */
  523. while(pipe_ssloverssl(cf, data, SOS_WRITE) > 0)
  524. ;
  525. BACKEND->handle = (gsk_handle) NULL;
  526. if(BACKEND->localfd >= 0) {
  527. close(BACKEND->localfd);
  528. BACKEND->localfd = -1;
  529. }
  530. if(BACKEND->remotefd >= 0) {
  531. close(BACKEND->remotefd);
  532. BACKEND->remotefd = -1;
  533. }
  534. }
  535. if(BACKEND->iocport >= 0)
  536. close_async_handshake(connssl);
  537. }
  538. static ssize_t gskit_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  539. const void *mem, size_t len, CURLcode *curlcode)
  540. {
  541. struct connectdata *conn = cf->conn;
  542. struct ssl_connect_data *connssl = cf->ctx;
  543. CURLcode cc = CURLE_SEND_ERROR;
  544. int written;
  545. DEBUGASSERT(BACKEND);
  546. if(pipe_ssloverssl(cf, data, SOS_WRITE) >= 0) {
  547. cc = gskit_status(data,
  548. gsk_secure_soc_write(BACKEND->handle,
  549. (char *) mem, (int) len, &written),
  550. "gsk_secure_soc_write()", CURLE_SEND_ERROR);
  551. if(cc == CURLE_OK)
  552. if(pipe_ssloverssl(cf, data, SOS_WRITE) < 0)
  553. cc = CURLE_SEND_ERROR;
  554. }
  555. if(cc != CURLE_OK) {
  556. *curlcode = cc;
  557. written = -1;
  558. }
  559. return (ssize_t) written; /* number of bytes */
  560. }
  561. static ssize_t gskit_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  562. char *buf, size_t buffersize, CURLcode *curlcode)
  563. {
  564. struct connectdata *conn = cf->conn;
  565. struct ssl_connect_data *connssl = cf->ctx;
  566. int nread;
  567. CURLcode cc = CURLE_RECV_ERROR;
  568. (void)data;
  569. DEBUGASSERT(BACKEND);
  570. if(pipe_ssloverssl(cf, data, SOS_READ) >= 0) {
  571. int buffsize = buffersize > (size_t) INT_MAX? INT_MAX: (int) buffersize;
  572. cc = gskit_status(data, gsk_secure_soc_read(BACKEND->handle,
  573. buf, buffsize, &nread),
  574. "gsk_secure_soc_read()", CURLE_RECV_ERROR);
  575. }
  576. switch(cc) {
  577. case CURLE_OK:
  578. break;
  579. case CURLE_OPERATION_TIMEDOUT:
  580. cc = CURLE_AGAIN;
  581. default:
  582. *curlcode = cc;
  583. nread = -1;
  584. break;
  585. }
  586. return (ssize_t) nread;
  587. }
  588. static CURLcode
  589. set_ssl_version_min_max(unsigned int *protoflags,
  590. struct Curl_cfilter *cf,
  591. struct Curl_easy *data)
  592. {
  593. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  594. struct connectdata *conn = data->conn;
  595. long ssl_version = conn_config->version;
  596. long ssl_version_max = conn_config->version_max;
  597. long i = ssl_version;
  598. switch(ssl_version_max) {
  599. case CURL_SSLVERSION_MAX_NONE:
  600. case CURL_SSLVERSION_MAX_DEFAULT:
  601. ssl_version_max = CURL_SSLVERSION_TLSv1_2;
  602. break;
  603. }
  604. for(; i <= (ssl_version_max >> 16); ++i) {
  605. switch(i) {
  606. case CURL_SSLVERSION_TLSv1_0:
  607. *protoflags |= CURL_GSKPROTO_TLSV10_MASK;
  608. break;
  609. case CURL_SSLVERSION_TLSv1_1:
  610. *protoflags |= CURL_GSKPROTO_TLSV11_MASK;
  611. break;
  612. case CURL_SSLVERSION_TLSv1_2:
  613. *protoflags |= CURL_GSKPROTO_TLSV11_MASK;
  614. break;
  615. case CURL_SSLVERSION_TLSv1_3:
  616. failf(data, "GSKit: TLS 1.3 is not yet supported");
  617. return CURLE_SSL_CONNECT_ERROR;
  618. }
  619. }
  620. return CURLE_OK;
  621. }
  622. static CURLcode gskit_connect_step1(struct Curl_cfilter *cf,
  623. struct Curl_easy *data)
  624. {
  625. struct ssl_connect_data *connssl = cf->ctx;
  626. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  627. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  628. struct Curl_cfilter *cf_ssl_next = Curl_ssl_cf_get_ssl(cf->next);
  629. struct ssl_connect_data *connssl_next = cf_ssl_next?
  630. cf_ssl_next->ctx : NULL;
  631. gsk_handle envir;
  632. CURLcode result;
  633. const char * const keyringfile = conn_config->CAfile;
  634. const char * const keyringpwd = ssl_config->key_passwd;
  635. const char * const keyringlabel = ssl_config->primary.clientcert;
  636. const long int ssl_version = conn_config->version;
  637. const bool verifypeer = conn_config->verifypeer;
  638. const char *hostname = connssl->hostname;
  639. const char *sni;
  640. unsigned int protoflags = 0;
  641. Qso_OverlappedIO_t commarea;
  642. int sockpair[2];
  643. static const int sobufsize = CURL_MAX_WRITE_SIZE;
  644. /* Create SSL environment, start (preferably asynchronous) handshake. */
  645. DEBUGASSERT(BACKEND);
  646. BACKEND->handle = (gsk_handle) NULL;
  647. BACKEND->iocport = -1;
  648. BACKEND->localfd = -1;
  649. BACKEND->remotefd = -1;
  650. /* GSKit supports two ways of specifying an SSL context: either by
  651. * application identifier (that should have been defined at the system
  652. * level) or by keyring file, password and certificate label.
  653. * Local certificate name (CURLOPT_SSLCERT) is used to hold either the
  654. * application identifier of the certificate label.
  655. * Key password (CURLOPT_KEYPASSWD) holds the keyring password.
  656. * It is not possible to have different keyrings for the CAs and the
  657. * local certificate. We thus use the CA file (CURLOPT_CAINFO) to identify
  658. * the keyring file.
  659. * If no key password is given and the keyring is the system keyring,
  660. * application identifier mode is tried first, as recommended in IBM doc.
  661. */
  662. envir = (gsk_handle) NULL;
  663. if(keyringlabel && *keyringlabel && !keyringpwd &&
  664. !strcmp(keyringfile, CURL_CA_BUNDLE)) {
  665. /* Try application identifier mode. */
  666. init_environment(data, &envir, keyringlabel, (const char *) NULL,
  667. (const char *) NULL, (const char *) NULL);
  668. }
  669. if(!envir) {
  670. /* Use keyring mode. */
  671. result = init_environment(data, &envir, (const char *) NULL,
  672. keyringfile, keyringlabel, keyringpwd);
  673. if(result)
  674. return result;
  675. }
  676. /* Create secure session. */
  677. result = gskit_status(data, gsk_secure_soc_open(envir, &BACKEND->handle),
  678. "gsk_secure_soc_open()", CURLE_SSL_CONNECT_ERROR);
  679. gsk_environment_close(&envir);
  680. if(result)
  681. return result;
  682. /* Establish a pipelining socket pair for SSL over SSL. */
  683. if(connssl_next) {
  684. if(Curl_socketpair(0, 0, 0, sockpair))
  685. return CURLE_SSL_CONNECT_ERROR;
  686. BACKEND->localfd = sockpair[0];
  687. BACKEND->remotefd = sockpair[1];
  688. setsockopt(BACKEND->localfd, SOL_SOCKET, SO_RCVBUF,
  689. (void *) &sobufsize, sizeof(sobufsize));
  690. setsockopt(BACKEND->remotefd, SOL_SOCKET, SO_RCVBUF,
  691. (void *) &sobufsize, sizeof(sobufsize));
  692. setsockopt(BACKEND->localfd, SOL_SOCKET, SO_SNDBUF,
  693. (void *) &sobufsize, sizeof(sobufsize));
  694. setsockopt(BACKEND->remotefd, SOL_SOCKET, SO_SNDBUF,
  695. (void *) &sobufsize, sizeof(sobufsize));
  696. curlx_nonblock(BACKEND->localfd, TRUE);
  697. curlx_nonblock(BACKEND->remotefd, TRUE);
  698. }
  699. /* Determine which SSL/TLS version should be enabled. */
  700. sni = hostname;
  701. switch(ssl_version) {
  702. case CURL_SSLVERSION_SSLv2:
  703. protoflags = CURL_GSKPROTO_SSLV2_MASK;
  704. sni = NULL;
  705. break;
  706. case CURL_SSLVERSION_SSLv3:
  707. protoflags = CURL_GSKPROTO_SSLV3_MASK;
  708. sni = NULL;
  709. break;
  710. case CURL_SSLVERSION_DEFAULT:
  711. case CURL_SSLVERSION_TLSv1:
  712. protoflags = CURL_GSKPROTO_TLSV10_MASK |
  713. CURL_GSKPROTO_TLSV11_MASK | CURL_GSKPROTO_TLSV12_MASK;
  714. break;
  715. case CURL_SSLVERSION_TLSv1_0:
  716. case CURL_SSLVERSION_TLSv1_1:
  717. case CURL_SSLVERSION_TLSv1_2:
  718. case CURL_SSLVERSION_TLSv1_3:
  719. result = set_ssl_version_min_max(&protoflags, cf, data);
  720. if(result != CURLE_OK)
  721. return result;
  722. break;
  723. default:
  724. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  725. return CURLE_SSL_CONNECT_ERROR;
  726. }
  727. /* Process SNI. Ignore if not supported (on OS400 < V7R1). */
  728. if(sni) {
  729. char *snihost = Curl_ssl_snihost(data, sni, NULL);
  730. if(!snihost) {
  731. failf(data, "Failed to set SNI");
  732. return CURLE_SSL_CONNECT_ERROR;
  733. }
  734. result = set_buffer(data, BACKEND->handle,
  735. GSK_SSL_EXTN_SERVERNAME_REQUEST, snihost, TRUE);
  736. if(result == CURLE_UNSUPPORTED_PROTOCOL)
  737. result = CURLE_OK;
  738. }
  739. /* Set session parameters. */
  740. if(!result) {
  741. /* Compute the handshake timeout. Since GSKit granularity is 1 second,
  742. we round up the required value. */
  743. timediff_t timeout = Curl_timeleft(data, NULL, TRUE);
  744. if(timeout < 0)
  745. result = CURLE_OPERATION_TIMEDOUT;
  746. else
  747. result = set_numeric(data, BACKEND->handle, GSK_HANDSHAKE_TIMEOUT,
  748. (timeout + 999) / 1000);
  749. }
  750. if(!result)
  751. result = set_numeric(data, BACKEND->handle, GSK_OS400_READ_TIMEOUT, 1);
  752. if(!result)
  753. result = set_numeric(data, BACKEND->handle, GSK_FD, BACKEND->localfd >= 0?
  754. BACKEND->localfd: Curl_conn_cf_get_socket(cf, data));
  755. if(!result)
  756. result = set_ciphers(cf, data, BACKEND->handle, &protoflags);
  757. if(!protoflags) {
  758. failf(data, "No SSL protocol/cipher combination enabled");
  759. result = CURLE_SSL_CIPHER;
  760. }
  761. if(!result)
  762. result = set_enum(data, BACKEND->handle, GSK_PROTOCOL_SSLV2,
  763. (protoflags & CURL_GSKPROTO_SSLV2_MASK)?
  764. GSK_PROTOCOL_SSLV2_ON: GSK_PROTOCOL_SSLV2_OFF, FALSE);
  765. if(!result)
  766. result = set_enum(data, BACKEND->handle, GSK_PROTOCOL_SSLV3,
  767. (protoflags & CURL_GSKPROTO_SSLV3_MASK)?
  768. GSK_PROTOCOL_SSLV3_ON: GSK_PROTOCOL_SSLV3_OFF, FALSE);
  769. if(!result)
  770. result = set_enum(data, BACKEND->handle, GSK_PROTOCOL_TLSV1,
  771. (protoflags & CURL_GSKPROTO_TLSV10_MASK)?
  772. GSK_PROTOCOL_TLSV1_ON: GSK_PROTOCOL_TLSV1_OFF, FALSE);
  773. if(!result) {
  774. result = set_enum(data, BACKEND->handle, GSK_PROTOCOL_TLSV11,
  775. (protoflags & CURL_GSKPROTO_TLSV11_MASK)?
  776. GSK_TRUE: GSK_FALSE, TRUE);
  777. if(result == CURLE_UNSUPPORTED_PROTOCOL) {
  778. result = CURLE_OK;
  779. if(protoflags == CURL_GSKPROTO_TLSV11_MASK) {
  780. failf(data, "TLS 1.1 not yet supported");
  781. result = CURLE_SSL_CIPHER;
  782. }
  783. }
  784. }
  785. if(!result) {
  786. result = set_enum(data, BACKEND->handle, GSK_PROTOCOL_TLSV12,
  787. (protoflags & CURL_GSKPROTO_TLSV12_MASK)?
  788. GSK_TRUE: GSK_FALSE, TRUE);
  789. if(result == CURLE_UNSUPPORTED_PROTOCOL) {
  790. result = CURLE_OK;
  791. if(protoflags == CURL_GSKPROTO_TLSV12_MASK) {
  792. failf(data, "TLS 1.2 not yet supported");
  793. result = CURLE_SSL_CIPHER;
  794. }
  795. }
  796. }
  797. if(!result)
  798. result = set_enum(data, BACKEND->handle, GSK_SERVER_AUTH_TYPE,
  799. verifypeer? GSK_SERVER_AUTH_FULL:
  800. GSK_SERVER_AUTH_PASSTHRU, FALSE);
  801. if(!result) {
  802. /* Start handshake. Try asynchronous first. */
  803. memset(&commarea, 0, sizeof(commarea));
  804. BACKEND->iocport = QsoCreateIOCompletionPort();
  805. if(BACKEND->iocport != -1) {
  806. result = gskit_status(data,
  807. gsk_secure_soc_startInit(BACKEND->handle,
  808. BACKEND->iocport,
  809. &commarea),
  810. "gsk_secure_soc_startInit()",
  811. CURLE_SSL_CONNECT_ERROR);
  812. if(!result) {
  813. connssl->connecting_state = ssl_connect_2;
  814. return CURLE_OK;
  815. }
  816. else
  817. close_async_handshake(connssl);
  818. }
  819. else if(errno != ENOBUFS)
  820. result = gskit_status(data, GSK_ERROR_IO,
  821. "QsoCreateIOCompletionPort()", 0);
  822. else if(connssl_next) {
  823. /* Cannot pipeline while handshaking synchronously. */
  824. result = CURLE_SSL_CONNECT_ERROR;
  825. }
  826. else {
  827. /* No more completion port available. Use synchronous IO. */
  828. result = gskit_status(data, gsk_secure_soc_init(BACKEND->handle),
  829. "gsk_secure_soc_init()", CURLE_SSL_CONNECT_ERROR);
  830. if(!result) {
  831. connssl->connecting_state = ssl_connect_3;
  832. return CURLE_OK;
  833. }
  834. }
  835. }
  836. /* Error: rollback. */
  837. close_one(cf, data);
  838. return result;
  839. }
  840. static CURLcode gskit_connect_step2(struct Curl_cfilter *cf,
  841. struct Curl_easy *data,
  842. bool nonblocking)
  843. {
  844. struct ssl_connect_data *connssl = cf->ctx;
  845. Qso_OverlappedIO_t cstat;
  846. struct timeval stmv;
  847. CURLcode result;
  848. /* Poll or wait for end of SSL asynchronous handshake. */
  849. DEBUGASSERT(BACKEND);
  850. for(;;) {
  851. timediff_t timeout_ms = nonblocking? 0: Curl_timeleft(data, NULL, TRUE);
  852. stmv.tv_sec = 0;
  853. stmv.tv_usec = 0;
  854. if(timeout_ms < 0)
  855. timeout_ms = 0;
  856. switch(QsoWaitForIOCompletion(BACKEND->iocport, &cstat,
  857. curlx_mstotv(&stmv, timeout_ms))) {
  858. case 1: /* Operation complete. */
  859. break;
  860. case -1: /* An error occurred: handshake still in progress. */
  861. if(errno == EINTR) {
  862. if(nonblocking)
  863. return CURLE_OK;
  864. continue; /* Retry. */
  865. }
  866. if(errno != ETIME) {
  867. char buffer[STRERROR_LEN];
  868. failf(data, "QsoWaitForIOCompletion() I/O error: %s",
  869. Curl_strerror(errno, buffer, sizeof(buffer)));
  870. cancel_async_handshake(cf, data);
  871. close_async_handshake(connssl);
  872. return CURLE_SSL_CONNECT_ERROR;
  873. }
  874. /* FALL INTO... */
  875. case 0: /* Handshake in progress, timeout occurred. */
  876. if(nonblocking)
  877. return CURLE_OK;
  878. cancel_async_handshake(cf, data);
  879. close_async_handshake(connssl);
  880. return CURLE_OPERATION_TIMEDOUT;
  881. }
  882. break;
  883. }
  884. result = gskit_status(data, cstat.returnValue, "SSL handshake",
  885. CURLE_SSL_CONNECT_ERROR);
  886. if(!result)
  887. connssl->connecting_state = ssl_connect_3;
  888. close_async_handshake(connssl);
  889. return result;
  890. }
  891. static CURLcode gskit_connect_step3(struct Curl_cfilter *cf,
  892. struct Curl_easy *data)
  893. {
  894. struct ssl_connect_data *connssl = cf->ctx;
  895. const gsk_cert_data_elem *cdev;
  896. int cdec;
  897. const gsk_cert_data_elem *p;
  898. const char *cert = (const char *) NULL;
  899. const char *certend = (const char *) NULL;
  900. const char *ptr;
  901. CURLcode result;
  902. /* SSL handshake done: gather certificate info and verify host. */
  903. DEBUGASSERT(BACKEND);
  904. if(gskit_status(data, gsk_attribute_get_cert_info(BACKEND->handle,
  905. GSK_PARTNER_CERT_INFO,
  906. &cdev, &cdec),
  907. "gsk_attribute_get_cert_info()", CURLE_SSL_CONNECT_ERROR) ==
  908. CURLE_OK) {
  909. int i;
  910. infof(data, "Server certificate:");
  911. p = cdev;
  912. for(i = 0; i++ < cdec; p++)
  913. switch(p->cert_data_id) {
  914. case CERT_BODY_DER:
  915. cert = p->cert_data_p;
  916. certend = cert + cdev->cert_data_l;
  917. break;
  918. case CERT_DN_PRINTABLE:
  919. infof(data, "\t subject: %.*s", p->cert_data_l, p->cert_data_p);
  920. break;
  921. case CERT_ISSUER_DN_PRINTABLE:
  922. infof(data, "\t issuer: %.*s", p->cert_data_l, p->cert_data_p);
  923. break;
  924. case CERT_VALID_FROM:
  925. infof(data, "\t start date: %.*s", p->cert_data_l, p->cert_data_p);
  926. break;
  927. case CERT_VALID_TO:
  928. infof(data, "\t expire date: %.*s", p->cert_data_l, p->cert_data_p);
  929. break;
  930. }
  931. }
  932. /* Verify host. */
  933. result = Curl_verifyhost(cf, data, cert, certend);
  934. if(result)
  935. return result;
  936. /* The only place GSKit can get the whole CA chain is a validation
  937. callback where no user data pointer is available. Therefore it's not
  938. possible to copy this chain into our structures for CAINFO.
  939. However the server certificate may be available, thus we can return
  940. info about it. */
  941. if(data->set.ssl.certinfo) {
  942. result = Curl_ssl_init_certinfo(data, 1);
  943. if(result)
  944. return result;
  945. if(cert) {
  946. result = Curl_extract_certinfo(data, 0, cert, certend);
  947. if(result)
  948. return result;
  949. }
  950. }
  951. /* Check pinned public key. */
  952. ptr = Curl_ssl_cf_is_proxy(cf)?
  953. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]:
  954. data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  955. if(!result && ptr) {
  956. struct Curl_X509certificate x509;
  957. struct Curl_asn1Element *p;
  958. memset(&x509, 0, sizeof(x509));
  959. if(Curl_parseX509(&x509, cert, certend))
  960. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  961. p = &x509.subjectPublicKeyInfo;
  962. result = Curl_pin_peer_pubkey(data, ptr, p->header, p->end - p->header);
  963. if(result) {
  964. failf(data, "SSL: public key does not match pinned public key");
  965. return result;
  966. }
  967. }
  968. connssl->connecting_state = ssl_connect_done;
  969. return CURLE_OK;
  970. }
  971. static CURLcode gskit_connect_common(struct Curl_cfilter *cf,
  972. struct Curl_easy *data,
  973. bool nonblocking, bool *done)
  974. {
  975. struct ssl_connect_data *connssl = cf->ctx;
  976. timediff_t timeout_ms;
  977. CURLcode result = CURLE_OK;
  978. *done = connssl->state == ssl_connection_complete;
  979. if(*done)
  980. return CURLE_OK;
  981. /* Step 1: create session, start handshake. */
  982. if(connssl->connecting_state == ssl_connect_1) {
  983. /* check allowed time left */
  984. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  985. if(timeout_ms < 0) {
  986. /* no need to continue if time already is up */
  987. failf(data, "SSL connection timeout");
  988. result = CURLE_OPERATION_TIMEDOUT;
  989. }
  990. else
  991. result = gskit_connect_step1(cf, data);
  992. }
  993. /* Handle handshake pipelining. */
  994. if(!result)
  995. if(pipe_ssloverssl(cf, data, SOS_READ | SOS_WRITE) < 0)
  996. result = CURLE_SSL_CONNECT_ERROR;
  997. /* Step 2: check if handshake is over. */
  998. if(!result && connssl->connecting_state == ssl_connect_2) {
  999. /* check allowed time left */
  1000. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1001. if(timeout_ms < 0) {
  1002. /* no need to continue if time already is up */
  1003. failf(data, "SSL connection timeout");
  1004. result = CURLE_OPERATION_TIMEDOUT;
  1005. }
  1006. else
  1007. result = gskit_connect_step2(cf, data, nonblocking);
  1008. }
  1009. /* Handle handshake pipelining. */
  1010. if(!result)
  1011. if(pipe_ssloverssl(cf, data, SOS_READ | SOS_WRITE) < 0)
  1012. result = CURLE_SSL_CONNECT_ERROR;
  1013. /* Step 3: gather certificate info, verify host. */
  1014. if(!result && connssl->connecting_state == ssl_connect_3)
  1015. result = gskit_connect_step3(cf, data);
  1016. if(result)
  1017. close_one(cf, data);
  1018. else if(connssl->connecting_state == ssl_connect_done) {
  1019. connssl->state = ssl_connection_complete;
  1020. connssl->connecting_state = ssl_connect_1;
  1021. *done = TRUE;
  1022. }
  1023. return result;
  1024. }
  1025. static CURLcode gskit_connect_nonblocking(struct Curl_cfilter *cf,
  1026. struct Curl_easy *data,
  1027. bool *done)
  1028. {
  1029. struct ssl_connect_data *connssl = cf->ctx;
  1030. CURLcode result;
  1031. result = gskit_connect_common(cf, data, TRUE, done);
  1032. if(*done || result)
  1033. connssl->connecting_state = ssl_connect_1;
  1034. return result;
  1035. }
  1036. static CURLcode gskit_connect(struct Curl_cfilter *cf,
  1037. struct Curl_easy *data)
  1038. {
  1039. struct ssl_connect_data *connssl = cf->ctx;
  1040. CURLcode result;
  1041. bool done;
  1042. connssl->connecting_state = ssl_connect_1;
  1043. result = gskit_connect_common(cf, data, FALSE, &done);
  1044. if(result)
  1045. return result;
  1046. DEBUGASSERT(done);
  1047. return CURLE_OK;
  1048. }
  1049. static void gskit_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1050. {
  1051. close_one(cf, data);
  1052. }
  1053. static int gskit_shutdown(struct Curl_cfilter *cf,
  1054. struct Curl_easy *data)
  1055. {
  1056. struct ssl_connect_data *connssl = cf->ctx;
  1057. int what;
  1058. int rc;
  1059. char buf[120];
  1060. int loop = 10; /* don't get stuck */
  1061. DEBUGASSERT(BACKEND);
  1062. if(!BACKEND->handle)
  1063. return 0;
  1064. #ifndef CURL_DISABLE_FTP
  1065. if(data->set.ftp_ccc != CURLFTPSSL_CCC_ACTIVE)
  1066. return 0;
  1067. #endif
  1068. close_one(cf, data);
  1069. rc = 0;
  1070. what = SOCKET_READABLE(Curl_conn_cf_get_socket(cf, data),
  1071. SSL_SHUTDOWN_TIMEOUT);
  1072. while(loop--) {
  1073. ssize_t nread;
  1074. if(what < 0) {
  1075. /* anything that gets here is fatally bad */
  1076. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1077. rc = -1;
  1078. break;
  1079. }
  1080. if(!what) { /* timeout */
  1081. failf(data, "SSL shutdown timeout");
  1082. break;
  1083. }
  1084. /* Something to read, let's do it and hope that it is the close
  1085. notify alert from the server. No way to gsk_secure_soc_read() now, so
  1086. use read(). */
  1087. nread = read(Curl_conn_cf_get_socket(cf, data), buf, sizeof(buf));
  1088. if(nread < 0) {
  1089. char buffer[STRERROR_LEN];
  1090. failf(data, "read: %s", Curl_strerror(errno, buffer, sizeof(buffer)));
  1091. rc = -1;
  1092. }
  1093. if(nread <= 0)
  1094. break;
  1095. what = SOCKET_READABLE(Curl_conn_cf_get_socket(cf, data), 0);
  1096. }
  1097. return rc;
  1098. }
  1099. static size_t gskit_version(char *buffer, size_t size)
  1100. {
  1101. return msnprintf(buffer, size, "GSKit");
  1102. }
  1103. static int gskit_check_cxn(struct Curl_cfilter *cf,
  1104. struct Curl_easy *data)
  1105. {
  1106. struct ssl_connect_data *connssl = cf->ctx;
  1107. int err;
  1108. int errlen;
  1109. (void)data;
  1110. /* The only thing that can be tested here is at the socket level. */
  1111. DEBUGASSERT(BACKEND);
  1112. if(!BACKEND->handle)
  1113. return 0; /* connection has been closed */
  1114. err = 0;
  1115. errlen = sizeof(err);
  1116. if(getsockopt(Curl_conn_cf_get_socket(cf, data), SOL_SOCKET, SO_ERROR,
  1117. (unsigned char *) &err, &errlen) ||
  1118. errlen != sizeof(err) || err)
  1119. return 0; /* connection has been closed */
  1120. return -1; /* connection status unknown */
  1121. }
  1122. static void *gskit_get_internals(struct ssl_connect_data *connssl,
  1123. CURLINFO info UNUSED_PARAM)
  1124. {
  1125. (void)info;
  1126. DEBUGASSERT(BACKEND);
  1127. return BACKEND->handle;
  1128. }
  1129. const struct Curl_ssl Curl_ssl_gskit = {
  1130. { CURLSSLBACKEND_GSKIT, "gskit" }, /* info */
  1131. SSLSUPP_CERTINFO |
  1132. SSLSUPP_PINNEDPUBKEY,
  1133. sizeof(struct ssl_backend_data),
  1134. gskit_init, /* init */
  1135. gskit_cleanup, /* cleanup */
  1136. gskit_version, /* version */
  1137. gskit_check_cxn, /* check_cxn */
  1138. gskit_shutdown, /* shutdown */
  1139. Curl_none_data_pending, /* data_pending */
  1140. Curl_none_random, /* random */
  1141. Curl_none_cert_status_request, /* cert_status_request */
  1142. gskit_connect, /* connect */
  1143. gskit_connect_nonblocking, /* connect_nonblocking */
  1144. Curl_ssl_get_select_socks, /* getsock */
  1145. gskit_get_internals, /* get_internals */
  1146. gskit_close, /* close_one */
  1147. Curl_none_close_all, /* close_all */
  1148. /* No session handling for GSKit */
  1149. Curl_none_session_free, /* session_free */
  1150. Curl_none_set_engine, /* set_engine */
  1151. Curl_none_set_engine_default, /* set_engine_default */
  1152. Curl_none_engines_list, /* engines_list */
  1153. Curl_none_false_start, /* false_start */
  1154. NULL, /* sha256sum */
  1155. NULL, /* associate_connection */
  1156. NULL, /* disassociate_connection */
  1157. NULL, /* free_multi_ssl_backend_data */
  1158. gskit_recv, /* recv decrypted data */
  1159. gskit_send, /* send data to encrypt */
  1160. };
  1161. #endif /* USE_GSKIT */