2
0

gskit.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329
  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 gskit_ssl_backend_data {
  88. gsk_handle handle;
  89. int iocport;
  90. int localfd;
  91. int remotefd;
  92. };
  93. #define BACKEND ((struct gskit_ssl_backend_data *)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 gskit_ssl_backend_data *backend_next;
  457. struct pollfd fds[2];
  458. int n;
  459. int m;
  460. int i;
  461. int ret = 0;
  462. char buf[CURL_MAX_WRITE_SIZE];
  463. DEBUGASSERT(BACKEND);
  464. if(!connssl_next)
  465. return 0; /* No SSL over SSL: OK. */
  466. DEBUGASSERT(connssl_next->backend);
  467. backend_next = (struct gskit_ssl_backend_data *)connssl_next->backend;
  468. n = 1;
  469. fds[0].fd = BACKEND->remotefd;
  470. fds[1].fd = Curl_conn_cf_get_socket(cf, data);
  471. if(directions & SOS_READ) {
  472. fds[0].events |= POLLOUT;
  473. }
  474. if(directions & SOS_WRITE) {
  475. n = 2;
  476. fds[0].events |= POLLIN;
  477. fds[1].events |= POLLOUT;
  478. }
  479. i = Curl_poll(fds, n, 0);
  480. if(i < 0)
  481. return -1; /* Select error. */
  482. if(fds[0].revents & POLLOUT) {
  483. /* Try getting data from HTTPS proxy and pipe it upstream. */
  484. n = 0;
  485. i = gsk_secure_soc_read(backend_next->handle, buf, sizeof(buf), &n);
  486. switch(i) {
  487. case GSK_OK:
  488. if(n) {
  489. i = write(BACKEND->remotefd, buf, n);
  490. if(i < 0)
  491. return -1;
  492. ret = 1;
  493. }
  494. break;
  495. case GSK_OS400_ERROR_TIMED_OUT:
  496. case GSK_WOULD_BLOCK:
  497. break;
  498. default:
  499. return -1;
  500. }
  501. }
  502. if((fds[0].revents & POLLIN) && (fds[1].revents & POLLOUT)) {
  503. /* Pipe data to HTTPS proxy. */
  504. n = read(BACKEND->remotefd, buf, sizeof(buf));
  505. if(n < 0)
  506. return -1;
  507. if(n) {
  508. i = gsk_secure_soc_write(backend_next->handle, buf, n, &m);
  509. if(i != GSK_OK || n != m)
  510. return -1;
  511. ret = 1;
  512. }
  513. }
  514. return ret; /* OK */
  515. }
  516. static void close_one(struct Curl_cfilter *cf, struct Curl_easy *data)
  517. {
  518. struct ssl_connect_data *connssl = cf->ctx;
  519. DEBUGASSERT(BACKEND);
  520. if(BACKEND->handle) {
  521. gskit_status(data, gsk_secure_soc_close(&BACKEND->handle),
  522. "gsk_secure_soc_close()", 0);
  523. /* Last chance to drain output. */
  524. while(pipe_ssloverssl(cf, data, SOS_WRITE) > 0)
  525. ;
  526. BACKEND->handle = (gsk_handle) NULL;
  527. if(BACKEND->localfd >= 0) {
  528. close(BACKEND->localfd);
  529. BACKEND->localfd = -1;
  530. }
  531. if(BACKEND->remotefd >= 0) {
  532. close(BACKEND->remotefd);
  533. BACKEND->remotefd = -1;
  534. }
  535. }
  536. if(BACKEND->iocport >= 0)
  537. close_async_handshake(connssl);
  538. }
  539. static ssize_t gskit_send(struct Curl_cfilter *cf, struct Curl_easy *data,
  540. const void *mem, size_t len, CURLcode *curlcode)
  541. {
  542. struct connectdata *conn = cf->conn;
  543. struct ssl_connect_data *connssl = cf->ctx;
  544. CURLcode cc = CURLE_SEND_ERROR;
  545. int written;
  546. DEBUGASSERT(BACKEND);
  547. if(pipe_ssloverssl(cf, data, SOS_WRITE) >= 0) {
  548. cc = gskit_status(data,
  549. gsk_secure_soc_write(BACKEND->handle,
  550. (char *) mem, (int) len, &written),
  551. "gsk_secure_soc_write()", CURLE_SEND_ERROR);
  552. if(cc == CURLE_OK)
  553. if(pipe_ssloverssl(cf, data, SOS_WRITE) < 0)
  554. cc = CURLE_SEND_ERROR;
  555. }
  556. if(cc != CURLE_OK) {
  557. *curlcode = cc;
  558. written = -1;
  559. }
  560. return (ssize_t) written; /* number of bytes */
  561. }
  562. static ssize_t gskit_recv(struct Curl_cfilter *cf, struct Curl_easy *data,
  563. char *buf, size_t buffersize, CURLcode *curlcode)
  564. {
  565. struct connectdata *conn = cf->conn;
  566. struct ssl_connect_data *connssl = cf->ctx;
  567. int nread;
  568. CURLcode cc = CURLE_RECV_ERROR;
  569. (void)data;
  570. DEBUGASSERT(BACKEND);
  571. if(pipe_ssloverssl(cf, data, SOS_READ) >= 0) {
  572. int buffsize = buffersize > (size_t) INT_MAX? INT_MAX: (int) buffersize;
  573. cc = gskit_status(data, gsk_secure_soc_read(BACKEND->handle,
  574. buf, buffsize, &nread),
  575. "gsk_secure_soc_read()", CURLE_RECV_ERROR);
  576. }
  577. switch(cc) {
  578. case CURLE_OK:
  579. break;
  580. case CURLE_OPERATION_TIMEDOUT:
  581. cc = CURLE_AGAIN;
  582. default:
  583. *curlcode = cc;
  584. nread = -1;
  585. break;
  586. }
  587. return (ssize_t) nread;
  588. }
  589. static CURLcode
  590. set_ssl_version_min_max(unsigned int *protoflags,
  591. struct Curl_cfilter *cf,
  592. struct Curl_easy *data)
  593. {
  594. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  595. struct connectdata *conn = data->conn;
  596. long ssl_version = conn_config->version;
  597. long ssl_version_max = conn_config->version_max;
  598. long i = ssl_version;
  599. switch(ssl_version_max) {
  600. case CURL_SSLVERSION_MAX_NONE:
  601. case CURL_SSLVERSION_MAX_DEFAULT:
  602. ssl_version_max = CURL_SSLVERSION_TLSv1_2;
  603. break;
  604. }
  605. for(; i <= (ssl_version_max >> 16); ++i) {
  606. switch(i) {
  607. case CURL_SSLVERSION_TLSv1_0:
  608. *protoflags |= CURL_GSKPROTO_TLSV10_MASK;
  609. break;
  610. case CURL_SSLVERSION_TLSv1_1:
  611. *protoflags |= CURL_GSKPROTO_TLSV11_MASK;
  612. break;
  613. case CURL_SSLVERSION_TLSv1_2:
  614. *protoflags |= CURL_GSKPROTO_TLSV11_MASK;
  615. break;
  616. case CURL_SSLVERSION_TLSv1_3:
  617. failf(data, "GSKit: TLS 1.3 is not yet supported");
  618. return CURLE_SSL_CONNECT_ERROR;
  619. }
  620. }
  621. return CURLE_OK;
  622. }
  623. static CURLcode gskit_connect_step1(struct Curl_cfilter *cf,
  624. struct Curl_easy *data)
  625. {
  626. struct ssl_connect_data *connssl = cf->ctx;
  627. struct ssl_primary_config *conn_config = Curl_ssl_cf_get_primary_config(cf);
  628. struct ssl_config_data *ssl_config = Curl_ssl_cf_get_config(cf, data);
  629. struct Curl_cfilter *cf_ssl_next = Curl_ssl_cf_get_ssl(cf->next);
  630. struct ssl_connect_data *connssl_next = cf_ssl_next?
  631. cf_ssl_next->ctx : NULL;
  632. gsk_handle envir;
  633. CURLcode result;
  634. const char * const keyringfile = conn_config->CAfile;
  635. const char * const keyringpwd = ssl_config->key_passwd;
  636. const char * const keyringlabel = ssl_config->primary.clientcert;
  637. const long int ssl_version = conn_config->version;
  638. const bool verifypeer = conn_config->verifypeer;
  639. const char *hostname = connssl->hostname;
  640. const char *sni;
  641. unsigned int protoflags = 0;
  642. Qso_OverlappedIO_t commarea;
  643. int sockpair[2];
  644. static const int sobufsize = CURL_MAX_WRITE_SIZE;
  645. /* Create SSL environment, start (preferably asynchronous) handshake. */
  646. DEBUGASSERT(BACKEND);
  647. BACKEND->handle = (gsk_handle) NULL;
  648. BACKEND->iocport = -1;
  649. BACKEND->localfd = -1;
  650. BACKEND->remotefd = -1;
  651. /* GSKit supports two ways of specifying an SSL context: either by
  652. * application identifier (that should have been defined at the system
  653. * level) or by keyring file, password and certificate label.
  654. * Local certificate name (CURLOPT_SSLCERT) is used to hold either the
  655. * application identifier of the certificate label.
  656. * Key password (CURLOPT_KEYPASSWD) holds the keyring password.
  657. * It is not possible to have different keyrings for the CAs and the
  658. * local certificate. We thus use the CA file (CURLOPT_CAINFO) to identify
  659. * the keyring file.
  660. * If no key password is given and the keyring is the system keyring,
  661. * application identifier mode is tried first, as recommended in IBM doc.
  662. */
  663. envir = (gsk_handle) NULL;
  664. if(keyringlabel && *keyringlabel && !keyringpwd &&
  665. !strcmp(keyringfile, CURL_CA_BUNDLE)) {
  666. /* Try application identifier mode. */
  667. init_environment(data, &envir, keyringlabel, (const char *) NULL,
  668. (const char *) NULL, (const char *) NULL);
  669. }
  670. if(!envir) {
  671. /* Use keyring mode. */
  672. result = init_environment(data, &envir, (const char *) NULL,
  673. keyringfile, keyringlabel, keyringpwd);
  674. if(result)
  675. return result;
  676. }
  677. /* Create secure session. */
  678. result = gskit_status(data, gsk_secure_soc_open(envir, &BACKEND->handle),
  679. "gsk_secure_soc_open()", CURLE_SSL_CONNECT_ERROR);
  680. gsk_environment_close(&envir);
  681. if(result)
  682. return result;
  683. /* Establish a pipelining socket pair for SSL over SSL. */
  684. if(connssl_next) {
  685. if(Curl_socketpair(0, 0, 0, sockpair))
  686. return CURLE_SSL_CONNECT_ERROR;
  687. BACKEND->localfd = sockpair[0];
  688. BACKEND->remotefd = sockpair[1];
  689. setsockopt(BACKEND->localfd, SOL_SOCKET, SO_RCVBUF,
  690. (void *) &sobufsize, sizeof(sobufsize));
  691. setsockopt(BACKEND->remotefd, SOL_SOCKET, SO_RCVBUF,
  692. (void *) &sobufsize, sizeof(sobufsize));
  693. setsockopt(BACKEND->localfd, SOL_SOCKET, SO_SNDBUF,
  694. (void *) &sobufsize, sizeof(sobufsize));
  695. setsockopt(BACKEND->remotefd, SOL_SOCKET, SO_SNDBUF,
  696. (void *) &sobufsize, sizeof(sobufsize));
  697. curlx_nonblock(BACKEND->localfd, TRUE);
  698. curlx_nonblock(BACKEND->remotefd, TRUE);
  699. }
  700. /* Determine which SSL/TLS version should be enabled. */
  701. sni = hostname;
  702. switch(ssl_version) {
  703. case CURL_SSLVERSION_SSLv2:
  704. protoflags = CURL_GSKPROTO_SSLV2_MASK;
  705. sni = NULL;
  706. break;
  707. case CURL_SSLVERSION_SSLv3:
  708. protoflags = CURL_GSKPROTO_SSLV3_MASK;
  709. sni = NULL;
  710. break;
  711. case CURL_SSLVERSION_DEFAULT:
  712. case CURL_SSLVERSION_TLSv1:
  713. protoflags = CURL_GSKPROTO_TLSV10_MASK |
  714. CURL_GSKPROTO_TLSV11_MASK | CURL_GSKPROTO_TLSV12_MASK;
  715. break;
  716. case CURL_SSLVERSION_TLSv1_0:
  717. case CURL_SSLVERSION_TLSv1_1:
  718. case CURL_SSLVERSION_TLSv1_2:
  719. case CURL_SSLVERSION_TLSv1_3:
  720. result = set_ssl_version_min_max(&protoflags, cf, data);
  721. if(result != CURLE_OK)
  722. return result;
  723. break;
  724. default:
  725. failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
  726. return CURLE_SSL_CONNECT_ERROR;
  727. }
  728. /* Process SNI. Ignore if not supported (on OS400 < V7R1). */
  729. if(sni) {
  730. char *snihost = Curl_ssl_snihost(data, sni, NULL);
  731. if(!snihost) {
  732. failf(data, "Failed to set SNI");
  733. return CURLE_SSL_CONNECT_ERROR;
  734. }
  735. result = set_buffer(data, BACKEND->handle,
  736. GSK_SSL_EXTN_SERVERNAME_REQUEST, snihost, TRUE);
  737. if(result == CURLE_UNSUPPORTED_PROTOCOL)
  738. result = CURLE_OK;
  739. }
  740. /* Set session parameters. */
  741. if(!result) {
  742. /* Compute the handshake timeout. Since GSKit granularity is 1 second,
  743. we round up the required value. */
  744. timediff_t timeout = Curl_timeleft(data, NULL, TRUE);
  745. if(timeout < 0)
  746. result = CURLE_OPERATION_TIMEDOUT;
  747. else
  748. result = set_numeric(data, BACKEND->handle, GSK_HANDSHAKE_TIMEOUT,
  749. (timeout + 999) / 1000);
  750. }
  751. if(!result)
  752. result = set_numeric(data, BACKEND->handle, GSK_OS400_READ_TIMEOUT, 1);
  753. if(!result)
  754. result = set_numeric(data, BACKEND->handle, GSK_FD, BACKEND->localfd >= 0?
  755. BACKEND->localfd: Curl_conn_cf_get_socket(cf, data));
  756. if(!result)
  757. result = set_ciphers(cf, data, BACKEND->handle, &protoflags);
  758. if(!protoflags) {
  759. failf(data, "No SSL protocol/cipher combination enabled");
  760. result = CURLE_SSL_CIPHER;
  761. }
  762. if(!result)
  763. result = set_enum(data, BACKEND->handle, GSK_PROTOCOL_SSLV2,
  764. (protoflags & CURL_GSKPROTO_SSLV2_MASK)?
  765. GSK_PROTOCOL_SSLV2_ON: GSK_PROTOCOL_SSLV2_OFF, FALSE);
  766. if(!result)
  767. result = set_enum(data, BACKEND->handle, GSK_PROTOCOL_SSLV3,
  768. (protoflags & CURL_GSKPROTO_SSLV3_MASK)?
  769. GSK_PROTOCOL_SSLV3_ON: GSK_PROTOCOL_SSLV3_OFF, FALSE);
  770. if(!result)
  771. result = set_enum(data, BACKEND->handle, GSK_PROTOCOL_TLSV1,
  772. (protoflags & CURL_GSKPROTO_TLSV10_MASK)?
  773. GSK_PROTOCOL_TLSV1_ON: GSK_PROTOCOL_TLSV1_OFF, FALSE);
  774. if(!result) {
  775. result = set_enum(data, BACKEND->handle, GSK_PROTOCOL_TLSV11,
  776. (protoflags & CURL_GSKPROTO_TLSV11_MASK)?
  777. GSK_TRUE: GSK_FALSE, TRUE);
  778. if(result == CURLE_UNSUPPORTED_PROTOCOL) {
  779. result = CURLE_OK;
  780. if(protoflags == CURL_GSKPROTO_TLSV11_MASK) {
  781. failf(data, "TLS 1.1 not yet supported");
  782. result = CURLE_SSL_CIPHER;
  783. }
  784. }
  785. }
  786. if(!result) {
  787. result = set_enum(data, BACKEND->handle, GSK_PROTOCOL_TLSV12,
  788. (protoflags & CURL_GSKPROTO_TLSV12_MASK)?
  789. GSK_TRUE: GSK_FALSE, TRUE);
  790. if(result == CURLE_UNSUPPORTED_PROTOCOL) {
  791. result = CURLE_OK;
  792. if(protoflags == CURL_GSKPROTO_TLSV12_MASK) {
  793. failf(data, "TLS 1.2 not yet supported");
  794. result = CURLE_SSL_CIPHER;
  795. }
  796. }
  797. }
  798. if(!result)
  799. result = set_enum(data, BACKEND->handle, GSK_SERVER_AUTH_TYPE,
  800. verifypeer? GSK_SERVER_AUTH_FULL:
  801. GSK_SERVER_AUTH_PASSTHRU, FALSE);
  802. if(!result) {
  803. /* Start handshake. Try asynchronous first. */
  804. memset(&commarea, 0, sizeof(commarea));
  805. BACKEND->iocport = QsoCreateIOCompletionPort();
  806. if(BACKEND->iocport != -1) {
  807. result = gskit_status(data,
  808. gsk_secure_soc_startInit(BACKEND->handle,
  809. BACKEND->iocport,
  810. &commarea),
  811. "gsk_secure_soc_startInit()",
  812. CURLE_SSL_CONNECT_ERROR);
  813. if(!result) {
  814. connssl->connecting_state = ssl_connect_2;
  815. return CURLE_OK;
  816. }
  817. else
  818. close_async_handshake(connssl);
  819. }
  820. else if(errno != ENOBUFS)
  821. result = gskit_status(data, GSK_ERROR_IO,
  822. "QsoCreateIOCompletionPort()", 0);
  823. else if(connssl_next) {
  824. /* Cannot pipeline while handshaking synchronously. */
  825. result = CURLE_SSL_CONNECT_ERROR;
  826. }
  827. else {
  828. /* No more completion port available. Use synchronous IO. */
  829. result = gskit_status(data, gsk_secure_soc_init(BACKEND->handle),
  830. "gsk_secure_soc_init()", CURLE_SSL_CONNECT_ERROR);
  831. if(!result) {
  832. connssl->connecting_state = ssl_connect_3;
  833. return CURLE_OK;
  834. }
  835. }
  836. }
  837. /* Error: rollback. */
  838. close_one(cf, data);
  839. return result;
  840. }
  841. static CURLcode gskit_connect_step2(struct Curl_cfilter *cf,
  842. struct Curl_easy *data,
  843. bool nonblocking)
  844. {
  845. struct ssl_connect_data *connssl = cf->ctx;
  846. Qso_OverlappedIO_t cstat;
  847. struct timeval stmv;
  848. CURLcode result;
  849. /* Poll or wait for end of SSL asynchronous handshake. */
  850. DEBUGASSERT(BACKEND);
  851. for(;;) {
  852. timediff_t timeout_ms = nonblocking? 0: Curl_timeleft(data, NULL, TRUE);
  853. stmv.tv_sec = 0;
  854. stmv.tv_usec = 0;
  855. if(timeout_ms < 0)
  856. timeout_ms = 0;
  857. switch(QsoWaitForIOCompletion(BACKEND->iocport, &cstat,
  858. curlx_mstotv(&stmv, timeout_ms))) {
  859. case 1: /* Operation complete. */
  860. break;
  861. case -1: /* An error occurred: handshake still in progress. */
  862. if(errno == EINTR) {
  863. if(nonblocking)
  864. return CURLE_OK;
  865. continue; /* Retry. */
  866. }
  867. if(errno != ETIME) {
  868. char buffer[STRERROR_LEN];
  869. failf(data, "QsoWaitForIOCompletion() I/O error: %s",
  870. Curl_strerror(errno, buffer, sizeof(buffer)));
  871. cancel_async_handshake(cf, data);
  872. close_async_handshake(connssl);
  873. return CURLE_SSL_CONNECT_ERROR;
  874. }
  875. /* FALL INTO... */
  876. case 0: /* Handshake in progress, timeout occurred. */
  877. if(nonblocking)
  878. return CURLE_OK;
  879. cancel_async_handshake(cf, data);
  880. close_async_handshake(connssl);
  881. return CURLE_OPERATION_TIMEDOUT;
  882. }
  883. break;
  884. }
  885. result = gskit_status(data, cstat.returnValue, "SSL handshake",
  886. CURLE_SSL_CONNECT_ERROR);
  887. if(!result)
  888. connssl->connecting_state = ssl_connect_3;
  889. close_async_handshake(connssl);
  890. return result;
  891. }
  892. static CURLcode gskit_connect_step3(struct Curl_cfilter *cf,
  893. struct Curl_easy *data)
  894. {
  895. struct ssl_connect_data *connssl = cf->ctx;
  896. const gsk_cert_data_elem *cdev;
  897. int cdec;
  898. const gsk_cert_data_elem *p;
  899. const char *cert = (const char *) NULL;
  900. const char *certend = (const char *) NULL;
  901. const char *ptr;
  902. CURLcode result;
  903. /* SSL handshake done: gather certificate info and verify host. */
  904. DEBUGASSERT(BACKEND);
  905. if(gskit_status(data, gsk_attribute_get_cert_info(BACKEND->handle,
  906. GSK_PARTNER_CERT_INFO,
  907. &cdev, &cdec),
  908. "gsk_attribute_get_cert_info()", CURLE_SSL_CONNECT_ERROR) ==
  909. CURLE_OK) {
  910. int i;
  911. infof(data, "Server certificate:");
  912. p = cdev;
  913. for(i = 0; i++ < cdec; p++)
  914. switch(p->cert_data_id) {
  915. case CERT_BODY_DER:
  916. cert = p->cert_data_p;
  917. certend = cert + cdev->cert_data_l;
  918. break;
  919. case CERT_DN_PRINTABLE:
  920. infof(data, "\t subject: %.*s", p->cert_data_l, p->cert_data_p);
  921. break;
  922. case CERT_ISSUER_DN_PRINTABLE:
  923. infof(data, "\t issuer: %.*s", p->cert_data_l, p->cert_data_p);
  924. break;
  925. case CERT_VALID_FROM:
  926. infof(data, "\t start date: %.*s", p->cert_data_l, p->cert_data_p);
  927. break;
  928. case CERT_VALID_TO:
  929. infof(data, "\t expire date: %.*s", p->cert_data_l, p->cert_data_p);
  930. break;
  931. }
  932. }
  933. /* Verify host. */
  934. result = Curl_verifyhost(cf, data, cert, certend);
  935. if(result)
  936. return result;
  937. /* The only place GSKit can get the whole CA chain is a validation
  938. callback where no user data pointer is available. Therefore it's not
  939. possible to copy this chain into our structures for CAINFO.
  940. However the server certificate may be available, thus we can return
  941. info about it. */
  942. if(data->set.ssl.certinfo) {
  943. result = Curl_ssl_init_certinfo(data, 1);
  944. if(result)
  945. return result;
  946. if(cert) {
  947. result = Curl_extract_certinfo(data, 0, cert, certend);
  948. if(result)
  949. return result;
  950. }
  951. }
  952. /* Check pinned public key. */
  953. ptr = Curl_ssl_cf_is_proxy(cf)?
  954. data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY]:
  955. data->set.str[STRING_SSL_PINNEDPUBLICKEY];
  956. if(!result && ptr) {
  957. struct Curl_X509certificate x509;
  958. struct Curl_asn1Element *p;
  959. memset(&x509, 0, sizeof(x509));
  960. if(Curl_parseX509(&x509, cert, certend))
  961. return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
  962. p = &x509.subjectPublicKeyInfo;
  963. result = Curl_pin_peer_pubkey(data, ptr, p->header, p->end - p->header);
  964. if(result) {
  965. failf(data, "SSL: public key does not match pinned public key");
  966. return result;
  967. }
  968. }
  969. connssl->connecting_state = ssl_connect_done;
  970. return CURLE_OK;
  971. }
  972. static CURLcode gskit_connect_common(struct Curl_cfilter *cf,
  973. struct Curl_easy *data,
  974. bool nonblocking, bool *done)
  975. {
  976. struct ssl_connect_data *connssl = cf->ctx;
  977. timediff_t timeout_ms;
  978. CURLcode result = CURLE_OK;
  979. *done = connssl->state == ssl_connection_complete;
  980. if(*done)
  981. return CURLE_OK;
  982. /* Step 1: create session, start handshake. */
  983. if(connssl->connecting_state == ssl_connect_1) {
  984. /* check allowed time left */
  985. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  986. if(timeout_ms < 0) {
  987. /* no need to continue if time already is up */
  988. failf(data, "SSL connection timeout");
  989. result = CURLE_OPERATION_TIMEDOUT;
  990. }
  991. else
  992. result = gskit_connect_step1(cf, data);
  993. }
  994. /* Handle handshake pipelining. */
  995. if(!result)
  996. if(pipe_ssloverssl(cf, data, SOS_READ | SOS_WRITE) < 0)
  997. result = CURLE_SSL_CONNECT_ERROR;
  998. /* Step 2: check if handshake is over. */
  999. if(!result && connssl->connecting_state == ssl_connect_2) {
  1000. /* check allowed time left */
  1001. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  1002. if(timeout_ms < 0) {
  1003. /* no need to continue if time already is up */
  1004. failf(data, "SSL connection timeout");
  1005. result = CURLE_OPERATION_TIMEDOUT;
  1006. }
  1007. else
  1008. result = gskit_connect_step2(cf, data, nonblocking);
  1009. }
  1010. /* Handle handshake pipelining. */
  1011. if(!result)
  1012. if(pipe_ssloverssl(cf, data, SOS_READ | SOS_WRITE) < 0)
  1013. result = CURLE_SSL_CONNECT_ERROR;
  1014. /* Step 3: gather certificate info, verify host. */
  1015. if(!result && connssl->connecting_state == ssl_connect_3)
  1016. result = gskit_connect_step3(cf, data);
  1017. if(result)
  1018. close_one(cf, data);
  1019. else if(connssl->connecting_state == ssl_connect_done) {
  1020. connssl->state = ssl_connection_complete;
  1021. connssl->connecting_state = ssl_connect_1;
  1022. *done = TRUE;
  1023. }
  1024. return result;
  1025. }
  1026. static CURLcode gskit_connect_nonblocking(struct Curl_cfilter *cf,
  1027. struct Curl_easy *data,
  1028. bool *done)
  1029. {
  1030. struct ssl_connect_data *connssl = cf->ctx;
  1031. CURLcode result;
  1032. result = gskit_connect_common(cf, data, TRUE, done);
  1033. if(*done || result)
  1034. connssl->connecting_state = ssl_connect_1;
  1035. return result;
  1036. }
  1037. static CURLcode gskit_connect(struct Curl_cfilter *cf,
  1038. struct Curl_easy *data)
  1039. {
  1040. struct ssl_connect_data *connssl = cf->ctx;
  1041. CURLcode result;
  1042. bool done;
  1043. connssl->connecting_state = ssl_connect_1;
  1044. result = gskit_connect_common(cf, data, FALSE, &done);
  1045. if(result)
  1046. return result;
  1047. DEBUGASSERT(done);
  1048. return CURLE_OK;
  1049. }
  1050. static void gskit_close(struct Curl_cfilter *cf, struct Curl_easy *data)
  1051. {
  1052. close_one(cf, data);
  1053. }
  1054. static int gskit_shutdown(struct Curl_cfilter *cf,
  1055. struct Curl_easy *data)
  1056. {
  1057. struct ssl_connect_data *connssl = cf->ctx;
  1058. int what;
  1059. int rc;
  1060. char buf[120];
  1061. int loop = 10; /* don't get stuck */
  1062. DEBUGASSERT(BACKEND);
  1063. if(!BACKEND->handle)
  1064. return 0;
  1065. #ifndef CURL_DISABLE_FTP
  1066. if(data->set.ftp_ccc != CURLFTPSSL_CCC_ACTIVE)
  1067. return 0;
  1068. #endif
  1069. close_one(cf, data);
  1070. rc = 0;
  1071. what = SOCKET_READABLE(Curl_conn_cf_get_socket(cf, data),
  1072. SSL_SHUTDOWN_TIMEOUT);
  1073. while(loop--) {
  1074. ssize_t nread;
  1075. if(what < 0) {
  1076. /* anything that gets here is fatally bad */
  1077. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  1078. rc = -1;
  1079. break;
  1080. }
  1081. if(!what) { /* timeout */
  1082. failf(data, "SSL shutdown timeout");
  1083. break;
  1084. }
  1085. /* Something to read, let's do it and hope that it is the close
  1086. notify alert from the server. No way to gsk_secure_soc_read() now, so
  1087. use read(). */
  1088. nread = read(Curl_conn_cf_get_socket(cf, data), buf, sizeof(buf));
  1089. if(nread < 0) {
  1090. char buffer[STRERROR_LEN];
  1091. failf(data, "read: %s", Curl_strerror(errno, buffer, sizeof(buffer)));
  1092. rc = -1;
  1093. }
  1094. if(nread <= 0)
  1095. break;
  1096. what = SOCKET_READABLE(Curl_conn_cf_get_socket(cf, data), 0);
  1097. }
  1098. return rc;
  1099. }
  1100. static size_t gskit_version(char *buffer, size_t size)
  1101. {
  1102. return msnprintf(buffer, size, "GSKit");
  1103. }
  1104. static int gskit_check_cxn(struct Curl_cfilter *cf,
  1105. struct Curl_easy *data)
  1106. {
  1107. struct ssl_connect_data *connssl = cf->ctx;
  1108. int err;
  1109. int errlen;
  1110. (void)data;
  1111. /* The only thing that can be tested here is at the socket level. */
  1112. DEBUGASSERT(BACKEND);
  1113. if(!BACKEND->handle)
  1114. return 0; /* connection has been closed */
  1115. err = 0;
  1116. errlen = sizeof(err);
  1117. if(getsockopt(Curl_conn_cf_get_socket(cf, data), SOL_SOCKET, SO_ERROR,
  1118. (unsigned char *) &err, &errlen) ||
  1119. errlen != sizeof(err) || err)
  1120. return 0; /* connection has been closed */
  1121. return -1; /* connection status unknown */
  1122. }
  1123. static void *gskit_get_internals(struct ssl_connect_data *connssl,
  1124. CURLINFO info UNUSED_PARAM)
  1125. {
  1126. (void)info;
  1127. DEBUGASSERT(BACKEND);
  1128. return BACKEND->handle;
  1129. }
  1130. const struct Curl_ssl Curl_ssl_gskit = {
  1131. { CURLSSLBACKEND_GSKIT, "gskit" }, /* info */
  1132. SSLSUPP_CERTINFO |
  1133. SSLSUPP_PINNEDPUBKEY,
  1134. sizeof(struct gskit_ssl_backend_data),
  1135. gskit_init, /* init */
  1136. gskit_cleanup, /* cleanup */
  1137. gskit_version, /* version */
  1138. gskit_check_cxn, /* check_cxn */
  1139. gskit_shutdown, /* shutdown */
  1140. Curl_none_data_pending, /* data_pending */
  1141. Curl_none_random, /* random */
  1142. Curl_none_cert_status_request, /* cert_status_request */
  1143. gskit_connect, /* connect */
  1144. gskit_connect_nonblocking, /* connect_nonblocking */
  1145. Curl_ssl_get_select_socks, /* getsock */
  1146. gskit_get_internals, /* get_internals */
  1147. gskit_close, /* close_one */
  1148. Curl_none_close_all, /* close_all */
  1149. /* No session handling for GSKit */
  1150. Curl_none_session_free, /* session_free */
  1151. Curl_none_set_engine, /* set_engine */
  1152. Curl_none_set_engine_default, /* set_engine_default */
  1153. Curl_none_engines_list, /* engines_list */
  1154. Curl_none_false_start, /* false_start */
  1155. NULL, /* sha256sum */
  1156. NULL, /* associate_connection */
  1157. NULL, /* disassociate_connection */
  1158. NULL, /* free_multi_ssl_backend_data */
  1159. gskit_recv, /* recv decrypted data */
  1160. gskit_send, /* send data to encrypt */
  1161. };
  1162. #endif /* USE_GSKIT */