qssl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2013, 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 http://curl.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifdef USE_QSOSSL
  24. #include <qsossl.h>
  25. #ifdef HAVE_LIMITS_H
  26. # include <limits.h>
  27. #endif
  28. #include <curl/curl.h>
  29. #include "urldata.h"
  30. #include "sendf.h"
  31. #include "qssl.h"
  32. #include "sslgen.h"
  33. #include "connect.h" /* for the connect timeout */
  34. #include "select.h"
  35. #include "x509asn1.h"
  36. #include "curl_memory.h"
  37. /* The last #include file should be: */
  38. #include "memdebug.h"
  39. int Curl_qsossl_init(void)
  40. {
  41. /* Nothing to do here. We must have connection data to initialize ssl, so
  42. * defer.
  43. */
  44. return 1;
  45. }
  46. void Curl_qsossl_cleanup(void)
  47. {
  48. /* Nothing to do. */
  49. }
  50. static CURLcode Curl_qsossl_init_session(struct SessionHandle * data)
  51. {
  52. int rc;
  53. char * certname;
  54. SSLInit initstr;
  55. SSLInitApp initappstr;
  56. /* Initialize the job for SSL according to the current parameters.
  57. * QsoSSL offers two ways to do it: SSL_Init_Application() that uses an
  58. * application identifier to select certificates in the main certificate
  59. * store, and SSL_Init() that uses named keyring files and a password.
  60. * It is not possible to have different keyrings for the CAs and the
  61. * local certificate. We thus use the certificate name to identify the
  62. * keyring if given, else the CA file name.
  63. * If the key file name is given, it is taken as the password for the
  64. * keyring in certificate file.
  65. * We first try to SSL_Init_Application(), then SSL_Init() if it failed.
  66. */
  67. certname = data->set.str[STRING_CERT];
  68. if(!certname) {
  69. certname = data->set.str[STRING_SSL_CAFILE];
  70. if(!certname)
  71. return CURLE_OK; /* Use previous setup. */
  72. }
  73. memset((char *) &initappstr, 0, sizeof initappstr);
  74. initappstr.applicationID = certname;
  75. initappstr.applicationIDLen = strlen(certname);
  76. initappstr.protocol = SSL_VERSION_CURRENT; /* TLSV1 compat. SSLV[23]. */
  77. initappstr.sessionType = SSL_REGISTERED_AS_CLIENT;
  78. rc = SSL_Init_Application(&initappstr);
  79. if(rc == SSL_ERROR_NOT_REGISTERED) {
  80. initstr.keyringFileName = certname;
  81. initstr.keyringPassword = data->set.str[STRING_KEY];
  82. initstr.cipherSuiteList = NULL; /* Use default. */
  83. initstr.cipherSuiteListLen = 0;
  84. rc = SSL_Init(&initstr);
  85. }
  86. switch (rc) {
  87. case 0: /* No error. */
  88. break;
  89. case SSL_ERROR_IO:
  90. failf(data, "SSL_Init() I/O error: %s", strerror(errno));
  91. return CURLE_SSL_CONNECT_ERROR;
  92. case SSL_ERROR_BAD_CIPHER_SUITE:
  93. return CURLE_SSL_CIPHER;
  94. case SSL_ERROR_KEYPASSWORD_EXPIRED:
  95. case SSL_ERROR_NOT_REGISTERED:
  96. return CURLE_SSL_CONNECT_ERROR;
  97. case SSL_ERROR_NO_KEYRING:
  98. return CURLE_SSL_CACERT;
  99. case SSL_ERROR_CERT_EXPIRED:
  100. return CURLE_SSL_CERTPROBLEM;
  101. default:
  102. failf(data, "SSL_Init(): %s", SSL_Strerror(rc, NULL));
  103. return CURLE_SSL_CONNECT_ERROR;
  104. }
  105. return CURLE_OK;
  106. }
  107. static CURLcode Curl_qsossl_create(struct connectdata * conn, int sockindex)
  108. {
  109. SSLHandle * h;
  110. struct ssl_connect_data * connssl = &conn->ssl[sockindex];
  111. h = SSL_Create(conn->sock[sockindex], SSL_ENCRYPT);
  112. if(!h) {
  113. failf(conn->data, "SSL_Create() I/O error: %s", strerror(errno));
  114. return CURLE_SSL_CONNECT_ERROR;
  115. }
  116. connssl->handle = h;
  117. return CURLE_OK;
  118. }
  119. static int Curl_qsossl_trap_cert(SSLHandle * h)
  120. {
  121. return 1; /* Accept certificate. */
  122. }
  123. static CURLcode Curl_qsossl_handshake(struct connectdata * conn, int sockindex)
  124. {
  125. int rc;
  126. struct SessionHandle * data = conn->data;
  127. struct ssl_connect_data * connssl = &conn->ssl[sockindex];
  128. SSLHandle * h = connssl->handle;
  129. long timeout_ms;
  130. h->exitPgm = data->set.ssl.verifypeer? NULL: Curl_qsossl_trap_cert;
  131. /* figure out how long time we should wait at maximum */
  132. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  133. if(timeout_ms < 0) {
  134. /* time-out, bail out, go home */
  135. failf(data, "Connection time-out");
  136. return CURLE_OPERATION_TIMEDOUT;
  137. }
  138. /* SSL_Handshake() timeout resolution is second, so round up. */
  139. h->timeout = (timeout_ms + 1000 - 1) / 1000;
  140. /* Set-up protocol. */
  141. switch (data->set.ssl.version) {
  142. default:
  143. case CURL_SSLVERSION_DEFAULT:
  144. h->protocol = SSL_VERSION_CURRENT; /* TLSV1 compat. SSLV[23]. */
  145. break;
  146. case CURL_SSLVERSION_TLSv1:
  147. h->protocol = TLS_VERSION_1;
  148. break;
  149. case CURL_SSLVERSION_SSLv2:
  150. h->protocol = SSL_VERSION_2;
  151. break;
  152. case CURL_SSLVERSION_SSLv3:
  153. h->protocol = SSL_VERSION_3;
  154. break;
  155. }
  156. h->peerCert = NULL;
  157. h->peerCertLen = 0;
  158. rc = SSL_Handshake(h, SSL_HANDSHAKE_AS_CLIENT);
  159. switch (rc) {
  160. case 0: /* No error. */
  161. break;
  162. case SSL_ERROR_BAD_CERTIFICATE:
  163. case SSL_ERROR_BAD_CERT_SIG:
  164. case SSL_ERROR_NOT_TRUSTED_ROOT:
  165. return CURLE_PEER_FAILED_VERIFICATION;
  166. case SSL_ERROR_BAD_CIPHER_SUITE:
  167. case SSL_ERROR_NO_CIPHERS:
  168. return CURLE_SSL_CIPHER;
  169. case SSL_ERROR_CERTIFICATE_REJECTED:
  170. case SSL_ERROR_CERT_EXPIRED:
  171. case SSL_ERROR_NO_CERTIFICATE:
  172. return CURLE_SSL_CERTPROBLEM;
  173. case SSL_ERROR_IO:
  174. failf(data, "SSL_Handshake() I/O error: %s", strerror(errno));
  175. return CURLE_SSL_CONNECT_ERROR;
  176. default:
  177. failf(data, "SSL_Handshake(): %s", SSL_Strerror(rc, NULL));
  178. return CURLE_SSL_CONNECT_ERROR;
  179. }
  180. /* Verify host. */
  181. rc = Curl_verifyhost(conn, h->peerCert, h->peerCert + h->peerCertLen);
  182. if(rc != CURLE_OK)
  183. return rc;
  184. /* Gather certificate info. */
  185. if(data->set.ssl.certinfo) {
  186. if(Curl_ssl_init_certinfo(data, 1))
  187. return CURLE_OUT_OF_MEMORY;
  188. if(h->peerCert) {
  189. rc = Curl_extract_certinfo(conn, 0, h->peerCert,
  190. h->peerCert + h->peerCertLen);
  191. if(rc != CURLE_OK)
  192. return rc;
  193. }
  194. }
  195. return CURLE_OK;
  196. }
  197. static Curl_recv qsossl_recv;
  198. static Curl_send qsossl_send;
  199. CURLcode Curl_qsossl_connect(struct connectdata * conn, int sockindex)
  200. {
  201. struct SessionHandle * data = conn->data;
  202. struct ssl_connect_data * connssl = &conn->ssl[sockindex];
  203. int rc;
  204. rc = Curl_qsossl_init_session(data);
  205. if(rc == CURLE_OK) {
  206. rc = Curl_qsossl_create(conn, sockindex);
  207. if(rc == CURLE_OK) {
  208. rc = Curl_qsossl_handshake(conn, sockindex);
  209. if(rc != CURLE_OK)
  210. SSL_Destroy(connssl->handle);
  211. }
  212. }
  213. if(rc == CURLE_OK) {
  214. conn->recv[sockindex] = qsossl_recv;
  215. conn->send[sockindex] = qsossl_send;
  216. connssl->state = ssl_connection_complete;
  217. }
  218. else {
  219. connssl->handle = NULL;
  220. connssl->use = FALSE;
  221. connssl->state = ssl_connection_none;
  222. }
  223. return rc;
  224. }
  225. static int Curl_qsossl_close_one(struct ssl_connect_data * conn,
  226. struct SessionHandle * data)
  227. {
  228. int rc;
  229. if(!conn->handle)
  230. return 0;
  231. rc = SSL_Destroy(conn->handle);
  232. if(rc) {
  233. if(rc == SSL_ERROR_IO) {
  234. failf(data, "SSL_Destroy() I/O error: %s", strerror(errno));
  235. return -1;
  236. }
  237. /* An SSL error. */
  238. failf(data, "SSL_Destroy() returned error %s", SSL_Strerror(rc, NULL));
  239. return -1;
  240. }
  241. conn->handle = NULL;
  242. return 0;
  243. }
  244. void Curl_qsossl_close(struct connectdata *conn, int sockindex)
  245. {
  246. struct SessionHandle *data = conn->data;
  247. struct ssl_connect_data *connssl = &conn->ssl[sockindex];
  248. if(connssl->use)
  249. (void) Curl_qsossl_close_one(connssl, data);
  250. }
  251. int Curl_qsossl_close_all(struct SessionHandle * data)
  252. {
  253. /* Unimplemented. */
  254. (void) data;
  255. return 0;
  256. }
  257. int Curl_qsossl_shutdown(struct connectdata * conn, int sockindex)
  258. {
  259. struct ssl_connect_data * connssl = &conn->ssl[sockindex];
  260. struct SessionHandle *data = conn->data;
  261. ssize_t nread;
  262. int what;
  263. int rc;
  264. char buf[120];
  265. if(!connssl->handle)
  266. return 0;
  267. if(data->set.ftp_ccc != CURLFTPSSL_CCC_ACTIVE)
  268. return 0;
  269. if(Curl_qsossl_close_one(connssl, data))
  270. return -1;
  271. rc = 0;
  272. what = Curl_socket_ready(conn->sock[sockindex],
  273. CURL_SOCKET_BAD, SSL_SHUTDOWN_TIMEOUT);
  274. for(;;) {
  275. if(what < 0) {
  276. /* anything that gets here is fatally bad */
  277. failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
  278. rc = -1;
  279. break;
  280. }
  281. if(!what) { /* timeout */
  282. failf(data, "SSL shutdown timeout");
  283. break;
  284. }
  285. /* Something to read, let's do it and hope that it is the close
  286. notify alert from the server. No way to SSL_Read now, so use read(). */
  287. nread = read(conn->sock[sockindex], buf, sizeof(buf));
  288. if(nread < 0) {
  289. failf(data, "read: %s", strerror(errno));
  290. rc = -1;
  291. }
  292. if(nread <= 0)
  293. break;
  294. what = Curl_socket_ready(conn->sock[sockindex], CURL_SOCKET_BAD, 0);
  295. }
  296. return rc;
  297. }
  298. static ssize_t qsossl_send(struct connectdata * conn, int sockindex,
  299. const void * mem, size_t len, CURLcode * curlcode)
  300. {
  301. /* SSL_Write() is said to return 'int' while write() and send() returns
  302. 'size_t' */
  303. int rc;
  304. rc = SSL_Write(conn->ssl[sockindex].handle, (void *) mem, (int) len);
  305. if(rc < 0) {
  306. switch(rc) {
  307. case SSL_ERROR_BAD_STATE:
  308. /* The operation did not complete; the same SSL I/O function
  309. should be called again later. This is basically an EWOULDBLOCK
  310. equivalent. */
  311. *curlcode = CURLE_AGAIN;
  312. return -1;
  313. case SSL_ERROR_IO:
  314. switch (errno) {
  315. case EWOULDBLOCK:
  316. case EINTR:
  317. *curlcode = CURLE_AGAIN;
  318. return -1;
  319. }
  320. failf(conn->data, "SSL_Write() I/O error: %s", strerror(errno));
  321. *curlcode = CURLE_SEND_ERROR;
  322. return -1;
  323. }
  324. /* An SSL error. */
  325. failf(conn->data, "SSL_Write() returned error %s",
  326. SSL_Strerror(rc, NULL));
  327. *curlcode = CURLE_SEND_ERROR;
  328. return -1;
  329. }
  330. return (ssize_t) rc; /* number of bytes */
  331. }
  332. static ssize_t qsossl_recv(struct connectdata * conn, int num, char * buf,
  333. size_t buffersize, CURLcode * curlcode)
  334. {
  335. char error_buffer[120]; /* OpenSSL documents that this must be at
  336. least 120 bytes long. */
  337. unsigned long sslerror;
  338. int buffsize;
  339. int nread;
  340. buffsize = (buffersize > (size_t)INT_MAX) ? INT_MAX : (int)buffersize;
  341. nread = SSL_Read(conn->ssl[num].handle, buf, buffsize);
  342. if(nread < 0) {
  343. /* failed SSL_read */
  344. switch (nread) {
  345. case SSL_ERROR_BAD_STATE:
  346. /* there's data pending, re-invoke SSL_Read(). */
  347. *curlcode = CURLE_AGAIN;
  348. return -1;
  349. case SSL_ERROR_IO:
  350. switch (errno) {
  351. case EWOULDBLOCK:
  352. *curlcode = CURLE_AGAIN;
  353. return -1;
  354. }
  355. failf(conn->data, "SSL_Read() I/O error: %s", strerror(errno));
  356. *curlcode = CURLE_RECV_ERROR;
  357. return -1;
  358. default:
  359. failf(conn->data, "SSL read error: %s", SSL_Strerror(nread, NULL));
  360. *curlcode = CURLE_RECV_ERROR;
  361. return -1;
  362. }
  363. }
  364. return (ssize_t) nread;
  365. }
  366. size_t Curl_qsossl_version(char * buffer, size_t size)
  367. {
  368. strncpy(buffer, "IBM OS/400 SSL", size);
  369. return strlen(buffer);
  370. }
  371. int Curl_qsossl_check_cxn(struct connectdata * cxn)
  372. {
  373. int err;
  374. int errlen;
  375. /* The only thing that can be tested here is at the socket level. */
  376. if(!cxn->ssl[FIRSTSOCKET].handle)
  377. return 0; /* connection has been closed */
  378. err = 0;
  379. errlen = sizeof err;
  380. if(getsockopt(cxn->sock[FIRSTSOCKET], SOL_SOCKET, SO_ERROR,
  381. (unsigned char *) &err, &errlen) ||
  382. errlen != sizeof err || err)
  383. return 0; /* connection has been closed */
  384. return -1; /* connection status unknown */
  385. }
  386. #endif /* USE_QSOSSL */