00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "curl_setup.h"
00031
00032 #ifdef USE_POLARSSL
00033
00034 #include <polarssl/net.h>
00035 #include <polarssl/ssl.h>
00036 #include <polarssl/certs.h>
00037 #include <polarssl/x509.h>
00038 #include <polarssl/version.h>
00039 #include <polarssl/sha256.h>
00040
00041 #if POLARSSL_VERSION_NUMBER < 0x01030000
00042 #error too old PolarSSL
00043 #endif
00044
00045 #include <polarssl/error.h>
00046 #include <polarssl/entropy.h>
00047 #include <polarssl/ctr_drbg.h>
00048
00049 #include "urldata.h"
00050 #include "sendf.h"
00051 #include "inet_pton.h"
00052 #include "polarssl.h"
00053 #include "vtls.h"
00054 #include "parsedate.h"
00055 #include "connect.h"
00056 #include "select.h"
00057 #include "strcase.h"
00058 #include "polarssl_threadlock.h"
00059 #include "curl_printf.h"
00060 #include "curl_memory.h"
00061
00062 #include "memdebug.h"
00063
00064
00065
00066
00067 #define RSA_PUB_DER_MAX_BYTES (38 + 2 * POLARSSL_MPI_MAX_SIZE)
00068 #define ECP_PUB_DER_MAX_BYTES (30 + 2 * POLARSSL_ECP_MAX_BYTES)
00069
00070 #define PUB_DER_MAX_BYTES (RSA_PUB_DER_MAX_BYTES > ECP_PUB_DER_MAX_BYTES ? \
00071 RSA_PUB_DER_MAX_BYTES : ECP_PUB_DER_MAX_BYTES)
00072
00073
00074 #if defined(USE_THREADS_POSIX) || defined(USE_THREADS_WIN32)
00075 #define THREADING_SUPPORT
00076 #endif
00077
00078 #ifndef POLARSSL_ERROR_C
00079 #define error_strerror(x,y,z)
00080 #endif
00081
00082
00083 #if defined(THREADING_SUPPORT)
00084 static entropy_context entropy;
00085
00086 static int entropy_init_initialized = 0;
00087
00088
00089 static void entropy_init_mutex(entropy_context *ctx)
00090 {
00091
00092 Curl_polarsslthreadlock_lock_function(0);
00093 if(entropy_init_initialized == 0) {
00094 entropy_init(ctx);
00095 entropy_init_initialized = 1;
00096 }
00097 Curl_polarsslthreadlock_unlock_function(0);
00098 }
00099
00100
00101
00102 static int entropy_func_mutex(void *data, unsigned char *output, size_t len)
00103 {
00104 int ret;
00105
00106 Curl_polarsslthreadlock_lock_function(1);
00107 ret = entropy_func(data, output, len);
00108 Curl_polarsslthreadlock_unlock_function(1);
00109
00110 return ret;
00111 }
00112
00113
00114 #endif
00115
00116
00117 #undef POLARSSL_DEBUG
00118
00119 #ifdef POLARSSL_DEBUG
00120 static void polarssl_debug(void *context, int level, const char *line)
00121 {
00122 struct Curl_easy *data = NULL;
00123
00124 if(!context)
00125 return;
00126
00127 data = (struct Curl_easy *)context;
00128
00129 infof(data, "%s", line);
00130 (void) level;
00131 }
00132 #else
00133 #endif
00134
00135
00136 #ifdef POLARSSL_SSL_ALPN
00137 # define HAS_ALPN
00138 #endif
00139
00140 static Curl_recv polarssl_recv;
00141 static Curl_send polarssl_send;
00142
00143
00144 static CURLcode
00145 polarssl_connect_step1(struct connectdata *conn,
00146 int sockindex)
00147 {
00148 struct Curl_easy *data = conn->data;
00149 struct ssl_connect_data* connssl = &conn->ssl[sockindex];
00150 const char *capath = SSL_CONN_CONFIG(CApath);
00151 const char * const hostname = SSL_IS_PROXY() ? conn->http_proxy.host.name :
00152 conn->host.name;
00153 const long int port = SSL_IS_PROXY() ? conn->port : conn->remote_port;
00154 int ret = -1;
00155 char errorbuf[128];
00156 errorbuf[0]=0;
00157
00158
00159 if(SSL_CONN_CONFIG(version) == CURL_SSLVERSION_SSLv2) {
00160 failf(data, "PolarSSL does not support SSLv2");
00161 return CURLE_SSL_CONNECT_ERROR;
00162 }
00163
00164 #ifdef THREADING_SUPPORT
00165 entropy_init_mutex(&entropy);
00166
00167 if((ret = ctr_drbg_init(&connssl->ctr_drbg, entropy_func_mutex, &entropy,
00168 NULL, 0)) != 0) {
00169 error_strerror(ret, errorbuf, sizeof(errorbuf));
00170 failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n",
00171 -ret, errorbuf);
00172 }
00173 #else
00174 entropy_init(&connssl->entropy);
00175
00176 if((ret = ctr_drbg_init(&connssl->ctr_drbg, entropy_func, &connssl->entropy,
00177 NULL, 0)) != 0) {
00178 error_strerror(ret, errorbuf, sizeof(errorbuf));
00179 failf(data, "Failed - PolarSSL: ctr_drbg_init returned (-0x%04X) %s\n",
00180 -ret, errorbuf);
00181 }
00182 #endif
00183
00184
00185 memset(&connssl->cacert, 0, sizeof(x509_crt));
00186
00187 if(SSL_CONN_CONFIG(CAfile)) {
00188 ret = x509_crt_parse_file(&connssl->cacert,
00189 SSL_CONN_CONFIG(CAfile));
00190
00191 if(ret<0) {
00192 error_strerror(ret, errorbuf, sizeof(errorbuf));
00193 failf(data, "Error reading ca cert file %s - PolarSSL: (-0x%04X) %s",
00194 SSL_CONN_CONFIG(CAfile), -ret, errorbuf);
00195
00196 if(SSL_CONN_CONFIG(verifypeer))
00197 return CURLE_SSL_CACERT_BADFILE;
00198 }
00199 }
00200
00201 if(capath) {
00202 ret = x509_crt_parse_path(&connssl->cacert, capath);
00203
00204 if(ret<0) {
00205 error_strerror(ret, errorbuf, sizeof(errorbuf));
00206 failf(data, "Error reading ca cert path %s - PolarSSL: (-0x%04X) %s",
00207 capath, -ret, errorbuf);
00208
00209 if(SSL_CONN_CONFIG(verifypeer))
00210 return CURLE_SSL_CACERT_BADFILE;
00211 }
00212 }
00213
00214
00215 memset(&connssl->clicert, 0, sizeof(x509_crt));
00216
00217 if(SSL_SET_OPTION(cert)) {
00218 ret = x509_crt_parse_file(&connssl->clicert,
00219 SSL_SET_OPTION(cert));
00220
00221 if(ret) {
00222 error_strerror(ret, errorbuf, sizeof(errorbuf));
00223 failf(data, "Error reading client cert file %s - PolarSSL: (-0x%04X) %s",
00224 SSL_SET_OPTION(cert), -ret, errorbuf);
00225
00226 return CURLE_SSL_CERTPROBLEM;
00227 }
00228 }
00229
00230
00231 if(SSL_SET_OPTION(key)) {
00232 pk_context pk;
00233 pk_init(&pk);
00234 ret = pk_parse_keyfile(&pk, SSL_SET_OPTION(key),
00235 SSL_SET_OPTION(key_passwd));
00236 if(ret == 0 && !pk_can_do(&pk, POLARSSL_PK_RSA))
00237 ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
00238 if(ret == 0)
00239 rsa_copy(&connssl->rsa, pk_rsa(pk));
00240 else
00241 rsa_free(&connssl->rsa);
00242 pk_free(&pk);
00243
00244 if(ret) {
00245 error_strerror(ret, errorbuf, sizeof(errorbuf));
00246 failf(data, "Error reading private key %s - PolarSSL: (-0x%04X) %s",
00247 SSL_SET_OPTION(key), -ret, errorbuf);
00248
00249 return CURLE_SSL_CERTPROBLEM;
00250 }
00251 }
00252
00253
00254 memset(&connssl->crl, 0, sizeof(x509_crl));
00255
00256 if(SSL_SET_OPTION(CRLfile)) {
00257 ret = x509_crl_parse_file(&connssl->crl,
00258 SSL_SET_OPTION(CRLfile));
00259
00260 if(ret) {
00261 error_strerror(ret, errorbuf, sizeof(errorbuf));
00262 failf(data, "Error reading CRL file %s - PolarSSL: (-0x%04X) %s",
00263 SSL_SET_OPTION(CRLfile), -ret, errorbuf);
00264
00265 return CURLE_SSL_CRL_BADFILE;
00266 }
00267 }
00268
00269 infof(data, "PolarSSL: Connecting to %s:%d\n", hostname, port);
00270
00271 if(ssl_init(&connssl->ssl)) {
00272 failf(data, "PolarSSL: ssl_init failed");
00273 return CURLE_SSL_CONNECT_ERROR;
00274 }
00275
00276 switch(SSL_CONN_CONFIG(version)) {
00277 case CURL_SSLVERSION_DEFAULT:
00278 case CURL_SSLVERSION_TLSv1:
00279 ssl_set_min_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
00280 SSL_MINOR_VERSION_1);
00281 break;
00282 case CURL_SSLVERSION_SSLv3:
00283 ssl_set_min_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
00284 SSL_MINOR_VERSION_0);
00285 ssl_set_max_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
00286 SSL_MINOR_VERSION_0);
00287 infof(data, "PolarSSL: Forced min. SSL Version to be SSLv3\n");
00288 break;
00289 case CURL_SSLVERSION_TLSv1_0:
00290 ssl_set_min_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
00291 SSL_MINOR_VERSION_1);
00292 ssl_set_max_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
00293 SSL_MINOR_VERSION_1);
00294 infof(data, "PolarSSL: Forced min. SSL Version to be TLS 1.0\n");
00295 break;
00296 case CURL_SSLVERSION_TLSv1_1:
00297 ssl_set_min_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
00298 SSL_MINOR_VERSION_2);
00299 ssl_set_max_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
00300 SSL_MINOR_VERSION_2);
00301 infof(data, "PolarSSL: Forced min. SSL Version to be TLS 1.1\n");
00302 break;
00303 case CURL_SSLVERSION_TLSv1_2:
00304 ssl_set_min_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
00305 SSL_MINOR_VERSION_3);
00306 ssl_set_max_version(&connssl->ssl, SSL_MAJOR_VERSION_3,
00307 SSL_MINOR_VERSION_3);
00308 infof(data, "PolarSSL: Forced min. SSL Version to be TLS 1.2\n");
00309 break;
00310 case CURL_SSLVERSION_TLSv1_3:
00311 failf(data, "PolarSSL: TLS 1.3 is not yet supported");
00312 return CURLE_SSL_CONNECT_ERROR;
00313 default:
00314 failf(data, "Unrecognized parameter passed via CURLOPT_SSLVERSION");
00315 return CURLE_SSL_CONNECT_ERROR;
00316 }
00317
00318 ssl_set_endpoint(&connssl->ssl, SSL_IS_CLIENT);
00319 ssl_set_authmode(&connssl->ssl, SSL_VERIFY_OPTIONAL);
00320
00321 ssl_set_rng(&connssl->ssl, ctr_drbg_random,
00322 &connssl->ctr_drbg);
00323 ssl_set_bio(&connssl->ssl,
00324 net_recv, &conn->sock[sockindex],
00325 net_send, &conn->sock[sockindex]);
00326
00327 ssl_set_ciphersuites(&connssl->ssl, ssl_list_ciphersuites());
00328
00329
00330 if(data->set.general_ssl.sessionid) {
00331 void *old_session = NULL;
00332
00333 Curl_ssl_sessionid_lock(conn);
00334 if(!Curl_ssl_getsessionid(conn, &old_session, NULL, sockindex)) {
00335 ret = ssl_set_session(&connssl->ssl, old_session);
00336 if(ret) {
00337 Curl_ssl_sessionid_unlock(conn);
00338 failf(data, "ssl_set_session returned -0x%x", -ret);
00339 return CURLE_SSL_CONNECT_ERROR;
00340 }
00341 infof(data, "PolarSSL re-using session\n");
00342 }
00343 Curl_ssl_sessionid_unlock(conn);
00344 }
00345
00346 ssl_set_ca_chain(&connssl->ssl,
00347 &connssl->cacert,
00348 &connssl->crl,
00349 hostname);
00350
00351 ssl_set_own_cert_rsa(&connssl->ssl,
00352 &connssl->clicert, &connssl->rsa);
00353
00354 if(ssl_set_hostname(&connssl->ssl, hostname)) {
00355
00356
00357
00358 failf(data, "couldn't set hostname in PolarSSL");
00359 return CURLE_SSL_CONNECT_ERROR;
00360 }
00361
00362 #ifdef HAS_ALPN
00363 if(conn->bits.tls_enable_alpn) {
00364 static const char *protocols[3];
00365 int cur = 0;
00366
00367 #ifdef USE_NGHTTP2
00368 if(data->set.httpversion >= CURL_HTTP_VERSION_2) {
00369 protocols[cur++] = NGHTTP2_PROTO_VERSION_ID;
00370 infof(data, "ALPN, offering %s\n", NGHTTP2_PROTO_VERSION_ID);
00371 }
00372 #endif
00373
00374 protocols[cur++] = ALPN_HTTP_1_1;
00375 infof(data, "ALPN, offering %s\n", ALPN_HTTP_1_1);
00376
00377 protocols[cur] = NULL;
00378
00379 ssl_set_alpn_protocols(&connssl->ssl, protocols);
00380 }
00381 #endif
00382
00383 #ifdef POLARSSL_DEBUG
00384 ssl_set_dbg(&connssl->ssl, polarssl_debug, data);
00385 #endif
00386
00387 connssl->connecting_state = ssl_connect_2;
00388
00389 return CURLE_OK;
00390 }
00391
00392 static CURLcode
00393 polarssl_connect_step2(struct connectdata *conn,
00394 int sockindex)
00395 {
00396 int ret;
00397 struct Curl_easy *data = conn->data;
00398 struct ssl_connect_data* connssl = &conn->ssl[sockindex];
00399 char buffer[1024];
00400 const char * const pinnedpubkey = SSL_IS_PROXY() ?
00401 data->set.str[STRING_SSL_PINNEDPUBLICKEY_PROXY] :
00402 data->set.str[STRING_SSL_PINNEDPUBLICKEY_ORIG];
00403
00404
00405 char errorbuf[128];
00406 errorbuf[0] = 0;
00407
00408 conn->recv[sockindex] = polarssl_recv;
00409 conn->send[sockindex] = polarssl_send;
00410
00411 ret = ssl_handshake(&connssl->ssl);
00412
00413 switch(ret) {
00414 case 0:
00415 break;
00416
00417 case POLARSSL_ERR_NET_WANT_READ:
00418 connssl->connecting_state = ssl_connect_2_reading;
00419 return CURLE_OK;
00420
00421 case POLARSSL_ERR_NET_WANT_WRITE:
00422 connssl->connecting_state = ssl_connect_2_writing;
00423 return CURLE_OK;
00424
00425 default:
00426 error_strerror(ret, errorbuf, sizeof(errorbuf));
00427 failf(data, "ssl_handshake returned - PolarSSL: (-0x%04X) %s",
00428 -ret, errorbuf);
00429 return CURLE_SSL_CONNECT_ERROR;
00430 }
00431
00432 infof(data, "PolarSSL: Handshake complete, cipher is %s\n",
00433 ssl_get_ciphersuite(&conn->ssl[sockindex].ssl) );
00434
00435 ret = ssl_get_verify_result(&conn->ssl[sockindex].ssl);
00436
00437 if(ret && SSL_CONN_CONFIG(verifypeer)) {
00438 if(ret & BADCERT_EXPIRED)
00439 failf(data, "Cert verify failed: BADCERT_EXPIRED");
00440
00441 if(ret & BADCERT_REVOKED) {
00442 failf(data, "Cert verify failed: BADCERT_REVOKED");
00443 return CURLE_SSL_CACERT;
00444 }
00445
00446 if(ret & BADCERT_CN_MISMATCH)
00447 failf(data, "Cert verify failed: BADCERT_CN_MISMATCH");
00448
00449 if(ret & BADCERT_NOT_TRUSTED)
00450 failf(data, "Cert verify failed: BADCERT_NOT_TRUSTED");
00451
00452 return CURLE_PEER_FAILED_VERIFICATION;
00453 }
00454
00455 if(ssl_get_peer_cert(&(connssl->ssl))) {
00456
00457 memset(buffer, 0, sizeof(buffer));
00458
00459 if(x509_crt_info(buffer, sizeof(buffer), (char *)"* ",
00460 ssl_get_peer_cert(&(connssl->ssl))) != -1)
00461 infof(data, "Dumping cert info:\n%s\n", buffer);
00462 }
00463
00464
00465 if(pinnedpubkey) {
00466 int size;
00467 CURLcode result;
00468 x509_crt *p;
00469 unsigned char pubkey[PUB_DER_MAX_BYTES];
00470 const x509_crt *peercert;
00471
00472 peercert = ssl_get_peer_cert(&connssl->ssl);
00473
00474 if(!peercert || !peercert->raw.p || !peercert->raw.len) {
00475 failf(data, "Failed due to missing peer certificate");
00476 return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
00477 }
00478
00479 p = calloc(1, sizeof(*p));
00480
00481 if(!p)
00482 return CURLE_OUT_OF_MEMORY;
00483
00484 x509_crt_init(p);
00485
00486
00487
00488
00489 if(x509_crt_parse_der(p, peercert->raw.p, peercert->raw.len)) {
00490 failf(data, "Failed copying peer certificate");
00491 x509_crt_free(p);
00492 free(p);
00493 return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
00494 }
00495
00496 size = pk_write_pubkey_der(&p->pk, pubkey, PUB_DER_MAX_BYTES);
00497
00498 if(size <= 0) {
00499 failf(data, "Failed copying public key from peer certificate");
00500 x509_crt_free(p);
00501 free(p);
00502 return CURLE_SSL_PINNEDPUBKEYNOTMATCH;
00503 }
00504
00505
00506 result = Curl_pin_peer_pubkey(data,
00507 pinnedpubkey,
00508 &pubkey[PUB_DER_MAX_BYTES - size], size);
00509 if(result) {
00510 x509_crt_free(p);
00511 free(p);
00512 return result;
00513 }
00514
00515 x509_crt_free(p);
00516 free(p);
00517 }
00518
00519 #ifdef HAS_ALPN
00520 if(conn->bits.tls_enable_alpn) {
00521 const char *next_protocol = ssl_get_alpn_protocol(&connssl->ssl);
00522
00523 if(next_protocol != NULL) {
00524 infof(data, "ALPN, server accepted to use %s\n", next_protocol);
00525
00526 #ifdef USE_NGHTTP2
00527 if(!strncmp(next_protocol, NGHTTP2_PROTO_VERSION_ID,
00528 NGHTTP2_PROTO_VERSION_ID_LEN)) {
00529 conn->negnpn = CURL_HTTP_VERSION_2;
00530 }
00531 else
00532 #endif
00533 if(!strncmp(next_protocol, ALPN_HTTP_1_1, ALPN_HTTP_1_1_LENGTH)) {
00534 conn->negnpn = CURL_HTTP_VERSION_1_1;
00535 }
00536 }
00537 else
00538 infof(data, "ALPN, server did not agree to a protocol\n");
00539 }
00540 #endif
00541
00542 connssl->connecting_state = ssl_connect_3;
00543 infof(data, "SSL connected\n");
00544
00545 return CURLE_OK;
00546 }
00547
00548 static CURLcode
00549 polarssl_connect_step3(struct connectdata *conn,
00550 int sockindex)
00551 {
00552 CURLcode retcode = CURLE_OK;
00553 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
00554 struct Curl_easy *data = conn->data;
00555
00556 DEBUGASSERT(ssl_connect_3 == connssl->connecting_state);
00557
00558 if(data->set.general_ssl.sessionid) {
00559 int ret;
00560 ssl_session *our_ssl_sessionid;
00561 void *old_ssl_sessionid = NULL;
00562
00563 our_ssl_sessionid = malloc(sizeof(ssl_session));
00564 if(!our_ssl_sessionid)
00565 return CURLE_OUT_OF_MEMORY;
00566
00567 ssl_session_init(our_ssl_sessionid);
00568
00569 ret = ssl_get_session(&connssl->ssl, our_ssl_sessionid);
00570 if(ret) {
00571 failf(data, "ssl_get_session returned -0x%x", -ret);
00572 return CURLE_SSL_CONNECT_ERROR;
00573 }
00574
00575
00576 Curl_ssl_sessionid_lock(conn);
00577 if(!Curl_ssl_getsessionid(conn, &old_ssl_sessionid, NULL, sockindex))
00578 Curl_ssl_delsessionid(conn, old_ssl_sessionid);
00579
00580 retcode = Curl_ssl_addsessionid(conn, our_ssl_sessionid, 0, sockindex);
00581 Curl_ssl_sessionid_unlock(conn);
00582 if(retcode) {
00583 free(our_ssl_sessionid);
00584 failf(data, "failed to store ssl session");
00585 return retcode;
00586 }
00587 }
00588
00589 connssl->connecting_state = ssl_connect_done;
00590
00591 return CURLE_OK;
00592 }
00593
00594 static ssize_t polarssl_send(struct connectdata *conn,
00595 int sockindex,
00596 const void *mem,
00597 size_t len,
00598 CURLcode *curlcode)
00599 {
00600 int ret = -1;
00601
00602 ret = ssl_write(&conn->ssl[sockindex].ssl,
00603 (unsigned char *)mem, len);
00604
00605 if(ret < 0) {
00606 *curlcode = (ret == POLARSSL_ERR_NET_WANT_WRITE) ?
00607 CURLE_AGAIN : CURLE_SEND_ERROR;
00608 ret = -1;
00609 }
00610
00611 return ret;
00612 }
00613
00614 void Curl_polarssl_close(struct connectdata *conn, int sockindex)
00615 {
00616 rsa_free(&conn->ssl[sockindex].rsa);
00617 x509_crt_free(&conn->ssl[sockindex].clicert);
00618 x509_crt_free(&conn->ssl[sockindex].cacert);
00619 x509_crl_free(&conn->ssl[sockindex].crl);
00620 ssl_free(&conn->ssl[sockindex].ssl);
00621 }
00622
00623 static ssize_t polarssl_recv(struct connectdata *conn,
00624 int num,
00625 char *buf,
00626 size_t buffersize,
00627 CURLcode *curlcode)
00628 {
00629 int ret = -1;
00630 ssize_t len = -1;
00631
00632 memset(buf, 0, buffersize);
00633 ret = ssl_read(&conn->ssl[num].ssl, (unsigned char *)buf, buffersize);
00634
00635 if(ret <= 0) {
00636 if(ret == POLARSSL_ERR_SSL_PEER_CLOSE_NOTIFY)
00637 return 0;
00638
00639 *curlcode = (ret == POLARSSL_ERR_NET_WANT_READ) ?
00640 CURLE_AGAIN : CURLE_RECV_ERROR;
00641 return -1;
00642 }
00643
00644 len = ret;
00645
00646 return len;
00647 }
00648
00649 void Curl_polarssl_session_free(void *ptr)
00650 {
00651 ssl_session_free(ptr);
00652 free(ptr);
00653 }
00654
00655
00656
00657
00658 size_t Curl_polarssl_version(char *buffer, size_t size)
00659 {
00660 unsigned int version = version_get_number();
00661 return snprintf(buffer, size, "%s/%d.%d.%d",
00662 version >= 0x01030A00?"mbedTLS":"PolarSSL",
00663 version>>24, (version>>16)&0xff, (version>>8)&0xff);
00664 }
00665
00666 static CURLcode
00667 polarssl_connect_common(struct connectdata *conn,
00668 int sockindex,
00669 bool nonblocking,
00670 bool *done)
00671 {
00672 CURLcode result;
00673 struct Curl_easy *data = conn->data;
00674 struct ssl_connect_data *connssl = &conn->ssl[sockindex];
00675 curl_socket_t sockfd = conn->sock[sockindex];
00676 long timeout_ms;
00677 int what;
00678
00679
00680 if(ssl_connection_complete == connssl->state) {
00681 *done = TRUE;
00682 return CURLE_OK;
00683 }
00684
00685 if(ssl_connect_1 == connssl->connecting_state) {
00686
00687 timeout_ms = Curl_timeleft(data, NULL, TRUE);
00688
00689 if(timeout_ms < 0) {
00690
00691 failf(data, "SSL connection timeout");
00692 return CURLE_OPERATION_TIMEDOUT;
00693 }
00694
00695 result = polarssl_connect_step1(conn, sockindex);
00696 if(result)
00697 return result;
00698 }
00699
00700 while(ssl_connect_2 == connssl->connecting_state ||
00701 ssl_connect_2_reading == connssl->connecting_state ||
00702 ssl_connect_2_writing == connssl->connecting_state) {
00703
00704
00705 timeout_ms = Curl_timeleft(data, NULL, TRUE);
00706
00707 if(timeout_ms < 0) {
00708
00709 failf(data, "SSL connection timeout");
00710 return CURLE_OPERATION_TIMEDOUT;
00711 }
00712
00713
00714 if(connssl->connecting_state == ssl_connect_2_reading ||
00715 connssl->connecting_state == ssl_connect_2_writing) {
00716
00717 curl_socket_t writefd = ssl_connect_2_writing==
00718 connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
00719 curl_socket_t readfd = ssl_connect_2_reading==
00720 connssl->connecting_state?sockfd:CURL_SOCKET_BAD;
00721
00722 what = Curl_socket_check(readfd, CURL_SOCKET_BAD, writefd,
00723 nonblocking?0:timeout_ms);
00724 if(what < 0) {
00725
00726 failf(data, "select/poll on SSL socket, errno: %d", SOCKERRNO);
00727 return CURLE_SSL_CONNECT_ERROR;
00728 }
00729 else if(0 == what) {
00730 if(nonblocking) {
00731 *done = FALSE;
00732 return CURLE_OK;
00733 }
00734 else {
00735
00736 failf(data, "SSL connection timeout");
00737 return CURLE_OPERATION_TIMEDOUT;
00738 }
00739 }
00740
00741 }
00742
00743
00744
00745
00746
00747
00748
00749
00750 result = polarssl_connect_step2(conn, sockindex);
00751 if(result || (nonblocking &&
00752 (ssl_connect_2 == connssl->connecting_state ||
00753 ssl_connect_2_reading == connssl->connecting_state ||
00754 ssl_connect_2_writing == connssl->connecting_state)))
00755 return result;
00756
00757 }
00758
00759 if(ssl_connect_3 == connssl->connecting_state) {
00760 result = polarssl_connect_step3(conn, sockindex);
00761 if(result)
00762 return result;
00763 }
00764
00765 if(ssl_connect_done == connssl->connecting_state) {
00766 connssl->state = ssl_connection_complete;
00767 conn->recv[sockindex] = polarssl_recv;
00768 conn->send[sockindex] = polarssl_send;
00769 *done = TRUE;
00770 }
00771 else
00772 *done = FALSE;
00773
00774
00775 connssl->connecting_state = ssl_connect_1;
00776
00777 return CURLE_OK;
00778 }
00779
00780 CURLcode
00781 Curl_polarssl_connect_nonblocking(struct connectdata *conn,
00782 int sockindex,
00783 bool *done)
00784 {
00785 return polarssl_connect_common(conn, sockindex, TRUE, done);
00786 }
00787
00788
00789 CURLcode
00790 Curl_polarssl_connect(struct connectdata *conn,
00791 int sockindex)
00792 {
00793 CURLcode result;
00794 bool done = FALSE;
00795
00796 result = polarssl_connect_common(conn, sockindex, FALSE, &done);
00797 if(result)
00798 return result;
00799
00800 DEBUGASSERT(done);
00801
00802 return CURLE_OK;
00803 }
00804
00805
00806
00807
00808
00809 int Curl_polarssl_init(void)
00810 {
00811 return Curl_polarsslthreadlock_thread_setup();
00812 }
00813
00814 void Curl_polarssl_cleanup(void)
00815 {
00816 (void)Curl_polarsslthreadlock_thread_cleanup();
00817 }
00818
00819 #endif