qssl.c 11 KB

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