/** setup SSL on the connection */ static SSL* setup_ssl(SSL_CTX* ctx, int fd, struct config_file* cfg) { SSL* ssl; X509* x; int r; ssl = SSL_new(ctx); if(!ssl) ssl_err("could not SSL_new"); SSL_set_connect_state(ssl); (void)SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); if(!SSL_set_fd(ssl, fd)) ssl_err("could not SSL_set_fd"); while(1) { ERR_clear_error(); if( (r=SSL_do_handshake(ssl)) == 1) break; r = SSL_get_error(ssl, r); if(r != SSL_ERROR_WANT_READ && r != SSL_ERROR_WANT_WRITE) ssl_err("SSL handshake failed"); /* wants to be called again */ } /* check authenticity of server */ if(SSL_get_verify_result(ssl) != X509_V_OK) ssl_err("SSL verification failed"); if(cfg->remote_control_use_cert) { x = SSL_get_peer_certificate(ssl); if(!x) ssl_err("Server presented no peer certificate"); X509_free(x); } return ssl; }
static gpointer startup_thread(gpointer user_data) { struct rtcdc_peer_connection *peer = (struct rtcdc_peer_connection *)user_data; struct rtcdc_transport *transport = peer->transport; struct ice_transport *ice = transport->ice; struct dtls_transport *dtls = transport->dtls; struct sctp_transport *sctp = transport->sctp; while (!peer->exit_thread && !ice->negotiation_done) g_usleep(2500); if (peer->exit_thread) return NULL; #ifdef DEBUG_SCTP fprintf(stderr, "ICE negotiation done\n"); #endif if (peer->role == RTCDC_PEER_ROLE_CLIENT) SSL_set_connect_state(dtls->ssl); else SSL_set_accept_state(dtls->ssl); SSL_do_handshake(dtls->ssl); while (!peer->exit_thread && !dtls->handshake_done) g_usleep(2500); if (peer->exit_thread) return NULL; #ifdef DEBUG_SCTP fprintf(stderr, "DTLS handshake done\n"); #endif if (peer->role == RTCDC_PEER_ROLE_CLIENT) { sctp->stream_cursor = 0; // use even streams struct sockaddr_conn sconn; memset(&sconn, 0, sizeof sconn); sconn.sconn_family = AF_CONN; sconn.sconn_port = htons(sctp->remote_port); sconn.sconn_addr = (void *)sctp; #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) sconn.sconn_len = sizeof *sctp; #endif if (usrsctp_connect(sctp->sock, (struct sockaddr *)&sconn, sizeof sconn) < 0) { #ifdef DEBUG_SCTP fprintf(stderr, "SCTP connection failed\n"); #endif } else { #ifdef DEBUG_SCTP fprintf(stderr, "SCTP connected\n"); #endif sctp->handshake_done = TRUE; if (peer->on_connect) peer->on_connect(peer, peer->user_data); } } else { sctp->stream_cursor = 1; // use odd streams struct sockaddr_conn sconn; memset(&sconn, 0, sizeof sconn); sconn.sconn_family = AF_CONN; sconn.sconn_port = htons(sctp->local_port); sconn.sconn_addr = (void *)sctp; #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) sconn.sconn_len = sizeof *sctp; #endif usrsctp_listen(sctp->sock, 1); socklen_t len = sizeof sconn; struct socket *s = usrsctp_accept(sctp->sock, (struct sockaddr *)&sconn, &len); if (s) { #ifdef DEBUG_SCTP fprintf(stderr, "SCTP accepted\n"); #endif sctp->handshake_done = TRUE; struct socket *t = sctp->sock; sctp->sock = s; usrsctp_close(t); if (peer->on_connect) peer->on_connect(peer, peer->user_data); } else { #ifdef DEBUG_SCTP fprintf(stderr, "SCTP acception failed\n"); #endif } } return NULL; }
static long bio_rdp_tls_ctrl(BIO* bio, int cmd, long num, void* ptr) { BIO* ssl_rbio; BIO* ssl_wbio; BIO* next_bio; int status = -1; BIO_RDP_TLS* tls = (BIO_RDP_TLS*) BIO_get_data(bio); if (!tls) return 0; if (!tls->ssl && (cmd != BIO_C_SET_SSL)) return 0; next_bio = BIO_next(bio); ssl_rbio = tls->ssl ? SSL_get_rbio(tls->ssl) : NULL; ssl_wbio = tls->ssl ? SSL_get_wbio(tls->ssl) : NULL; switch (cmd) { case BIO_CTRL_RESET: SSL_shutdown(tls->ssl); if (SSL_in_connect_init(tls->ssl)) SSL_set_connect_state(tls->ssl); else if (SSL_in_accept_init(tls->ssl)) SSL_set_accept_state(tls->ssl); SSL_clear(tls->ssl); if (next_bio) status = BIO_ctrl(next_bio, cmd, num, ptr); else if (ssl_rbio) status = BIO_ctrl(ssl_rbio, cmd, num, ptr); else status = 1; break; case BIO_C_GET_FD: status = BIO_ctrl(ssl_rbio, cmd, num, ptr); break; case BIO_CTRL_INFO: status = 0; break; case BIO_CTRL_SET_CALLBACK: status = 0; break; case BIO_CTRL_GET_CALLBACK: *((ULONG_PTR*) ptr) = (ULONG_PTR) SSL_get_info_callback(tls->ssl); status = 1; break; case BIO_C_SSL_MODE: if (num) SSL_set_connect_state(tls->ssl); else SSL_set_accept_state(tls->ssl); status = 1; break; case BIO_CTRL_GET_CLOSE: status = BIO_get_shutdown(bio); break; case BIO_CTRL_SET_CLOSE: BIO_set_shutdown(bio, (int) num); status = 1; break; case BIO_CTRL_WPENDING: status = BIO_ctrl(ssl_wbio, cmd, num, ptr); break; case BIO_CTRL_PENDING: status = SSL_pending(tls->ssl); if (status == 0) status = BIO_pending(ssl_rbio); break; case BIO_CTRL_FLUSH: BIO_clear_retry_flags(bio); status = BIO_ctrl(ssl_wbio, cmd, num, ptr); BIO_copy_next_retry(bio); status = 1; break; case BIO_CTRL_PUSH: if (next_bio && (next_bio != ssl_rbio)) { #if OPENSSL_VERSION_NUMBER < 0x10100000L SSL_set_bio(tls->ssl, next_bio, next_bio); CRYPTO_add(&(bio->next_bio->references), 1, CRYPTO_LOCK_BIO); #else /* * We are going to pass ownership of next to the SSL object...but * we don't own a reference to pass yet - so up ref */ BIO_up_ref(next_bio); SSL_set_bio(tls->ssl, next_bio, next_bio); #endif } status = 1; break; case BIO_CTRL_POP: /* Only detach if we are the BIO explicitly being popped */ if (bio == ptr) { if (ssl_rbio != ssl_wbio) BIO_free_all(ssl_wbio); #if OPENSSL_VERSION_NUMBER < 0x10100000L if (next_bio) CRYPTO_add(&(bio->next_bio->references), -1, CRYPTO_LOCK_BIO); tls->ssl->wbio = tls->ssl->rbio = NULL; #else /* OpenSSL 1.1: This will also clear the reference we obtained during push */ SSL_set_bio(tls->ssl, NULL, NULL); #endif } status = 1; break; case BIO_C_GET_SSL: if (ptr) { *((SSL**) ptr) = tls->ssl; status = 1; } break; case BIO_C_SET_SSL: BIO_set_shutdown(bio, (int) num); if (ptr) { tls->ssl = (SSL*) ptr; ssl_rbio = SSL_get_rbio(tls->ssl); ssl_wbio = SSL_get_wbio(tls->ssl); } if (ssl_rbio) { if (next_bio) BIO_push(ssl_rbio, next_bio); BIO_set_next(bio, ssl_rbio); #if OPENSSL_VERSION_NUMBER < 0x10100000L CRYPTO_add(&(ssl_rbio->references), 1, CRYPTO_LOCK_BIO); #else BIO_up_ref(ssl_rbio); #endif } BIO_set_init(bio, 1); status = 1; break; case BIO_C_DO_STATE_MACHINE: BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPECIAL); BIO_set_retry_reason(bio, 0); status = SSL_do_handshake(tls->ssl); if (status <= 0) { switch (SSL_get_error(tls->ssl, status)) { case SSL_ERROR_WANT_READ: BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); break; case SSL_ERROR_WANT_WRITE: BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); break; case SSL_ERROR_WANT_CONNECT: BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY); BIO_set_retry_reason(bio, BIO_get_retry_reason(next_bio)); break; default: BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); break; } } break; default: status = BIO_ctrl(ssl_rbio, cmd, num, ptr); break; } return status; }
static int dtls_netsock_handshake_int(NETIO_SOCK_T *pnetsock, const struct sockaddr_in *dest_addr) { int rc = 0; int code; int len; char *pData = NULL; struct sockaddr_in saTmp; socklen_t socklen; if(!pnetsock || !dest_addr || !pnetsock->ssl.pCtxt) { return -1; } else if(pnetsock->ssl.state >= SSL_SOCK_STATE_HANDSHAKE_COMPLETED) { return 1; } else if(pnetsock->ssl.state == SSL_SOCK_STATE_ERROR) { return -1; } if(pnetsock->ssl.state < SSL_SOCK_STATE_HANDSHAKE_INPROGRESS) { pnetsock->ssl.state = SSL_SOCK_STATE_HANDSHAKE_INPROGRESS; } //LOG(X_DEBUG("Calling SSL_do_handshake")); if((rc = SSL_do_handshake(pnetsock->ssl.pCtxt)) != 1){ code = SSL_get_error(pnetsock->ssl.pCtxt, rc); //LOG(X_DEBUG("SSL_do_handshake rc:%d, code:%d"), rc, code); switch(code) { case SSL_ERROR_WANT_READ: // 2 case SSL_ERROR_WANT_WRITE: // 3 case SSL_ERROR_NONE: // 0 break; default: LOG(X_ERROR("DTLS%s %shandshake SSL_do_handshake failed with rc: %d, code: %d %s"), (pnetsock->flags & NETIO_FLAG_SRTP) ? "-SRTP" : "", (pnetsock->flags & NETIO_FLAG_SSL_DTLS_SERVER) ? "passive " : "active ", rc, code, ERR_reason_error_string(ERR_get_error())); pnetsock->ssl.state = SSL_SOCK_STATE_ERROR; return -1; } } rc = 0; if((len = BIO_get_mem_data(pnetsock->ssl.pBioWr, &pData)) > 0 && pData){ //LOG(X_DEBUG("SSL handshake - BIO_get_mem_data len:%d"), len); if((rc = SENDTO_DTLS(pnetsock, pData, len, 0, dest_addr)) < len) { //if((rc = sendto(PNETIOSOCK_FD(pnetsock), pData, len, 0, (const struct sockaddr *) dest_addr, // sizeof(struct sockaddr_in))) < len) { socklen = sizeof(saTmp); getsockname(PNETIOSOCK_FD(pnetsock), (struct sockaddr *) &saTmp, &socklen); LOG(X_ERROR("DTLS%s %shandshake sendto %d -> %s:%d for %d bytes failed with "ERRNO_FMT_STR), (pnetsock->flags & NETIO_FLAG_SRTP) ? "-SRTP" : "", (pnetsock->flags & NETIO_FLAG_SSL_DTLS_SERVER) ? "passive " : "active ", ntohs(saTmp.sin_port), inet_ntoa(dest_addr->sin_addr), ntohs(dest_addr->sin_port), len, ERRNO_FMT_ARGS); pnetsock->ssl.state = SSL_SOCK_STATE_ERROR; rc = -1; } else { socklen = sizeof(saTmp); getsockname(PNETIOSOCK_FD(pnetsock), (struct sockaddr *) &saTmp, &socklen); LOG(X_DEBUG("DTLS%s %shandshake sent %d bytes from :%d to %s:%d"), (pnetsock->flags & NETIO_FLAG_SRTP) ? "-SRTP" : "", (pnetsock->flags & NETIO_FLAG_SSL_DTLS_SERVER) ? "passive " : "active ", len, ntohs(saTmp.sin_port), inet_ntoa(dest_addr->sin_addr), ntohs(dest_addr->sin_port)); rc = 0; } } //LOG(X_DEBUG("SSL handshake - Calling BIO_reset, BIO_get_mem_data had len:%d"), len); (void) BIO_reset(pnetsock->ssl.pBioRd); (void) BIO_reset(pnetsock->ssl.pBioWr); if(SSL_is_init_finished(pnetsock->ssl.pCtxt)){ if((rc = dlts_netsock_onhandshake_completed(pnetsock)) < 0) { pnetsock->ssl.state = SSL_SOCK_STATE_ERROR; } else { LOG(X_DEBUG("DTLS%s %shandshake to %s:%d completed"), (pnetsock->flags & NETIO_FLAG_SRTP) ? "-SRTP" : "", (pnetsock->flags & NETIO_FLAG_SSL_DTLS_SERVER) ? "passive " : "active ", inet_ntoa(dest_addr->sin_addr), ntohs(dest_addr->sin_port)); pnetsock->ssl.state = SSL_SOCK_STATE_HANDSHAKE_COMPLETED; rc = 1; } } return rc; }
static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr) { SSL **sslp,*ssl; BIO_SSL *bs; BIO *dbio,*bio; long ret=1; bs=(BIO_SSL *)b->ptr; ssl=bs->ssl; if ((ssl == NULL) && (cmd != BIO_C_SET_SSL)) return(0); switch (cmd) { case BIO_CTRL_RESET: SSL_shutdown(ssl); if (ssl->handshake_func == ssl->method->ssl_connect) SSL_set_connect_state(ssl); else if (ssl->handshake_func == ssl->method->ssl_accept) SSL_set_accept_state(ssl); SSL_clear(ssl); if (b->next_bio != NULL) ret=BIO_ctrl(b->next_bio,cmd,num,ptr); else if (ssl->rbio != NULL) ret=BIO_ctrl(ssl->rbio,cmd,num,ptr); else ret=1; break; case BIO_CTRL_INFO: ret=0; break; case BIO_C_SSL_MODE: if (num) /* client mode */ SSL_set_connect_state(ssl); else SSL_set_accept_state(ssl); break; case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT: ret=bs->renegotiate_timeout; if (num < 60) num=5; bs->renegotiate_timeout=(unsigned long)num; bs->last_time=(unsigned long)time(NULL); break; case BIO_C_SET_SSL_RENEGOTIATE_BYTES: ret=bs->renegotiate_count; if ((long)num >=512) bs->renegotiate_count=(unsigned long)num; break; case BIO_C_GET_SSL_NUM_RENEGOTIATES: ret=bs->num_renegotiates; break; case BIO_C_SET_SSL: if (ssl != NULL) { ssl_free(b); if (!ssl_new(b)) return 0; } b->shutdown=(int)num; ssl=(SSL *)ptr; ((BIO_SSL *)b->ptr)->ssl=ssl; bio=SSL_get_rbio(ssl); if (bio != NULL) { if (b->next_bio != NULL) BIO_push(bio,b->next_bio); b->next_bio=bio; CRYPTO_add(&bio->references,1,CRYPTO_LOCK_BIO); } b->init=1; break; case BIO_C_GET_SSL: if (ptr != NULL) { sslp=(SSL **)ptr; *sslp=ssl; } else ret=0; break; case BIO_CTRL_GET_CLOSE: ret=b->shutdown; break; case BIO_CTRL_SET_CLOSE: b->shutdown=(int)num; break; case BIO_CTRL_WPENDING: ret=BIO_ctrl(ssl->wbio,cmd,num,ptr); break; case BIO_CTRL_PENDING: ret=SSL_pending(ssl); if (ret == 0) ret=BIO_pending(ssl->rbio); break; case BIO_CTRL_FLUSH: BIO_clear_retry_flags(b); ret=BIO_ctrl(ssl->wbio,cmd,num,ptr); BIO_copy_next_retry(b); break; case BIO_CTRL_PUSH: if ((b->next_bio != NULL) && (b->next_bio != ssl->rbio)) { SSL_set_bio(ssl,b->next_bio,b->next_bio); CRYPTO_add(&b->next_bio->references,1,CRYPTO_LOCK_BIO); } break; case BIO_CTRL_POP: /* ugly bit of a hack */ if (ssl->rbio != ssl->wbio) /* we are in trouble :-( */ { BIO_free_all(ssl->wbio); } if (b->next_bio != NULL) { CRYPTO_add(&b->next_bio->references,1,CRYPTO_LOCK_BIO); } ssl->wbio=NULL; ssl->rbio=NULL; break; case BIO_C_DO_STATE_MACHINE: BIO_clear_retry_flags(b); b->retry_reason=0; ret=(int)SSL_do_handshake(ssl); switch (SSL_get_error(ssl,(int)ret)) { case SSL_ERROR_WANT_READ: BIO_set_flags(b, BIO_FLAGS_READ|BIO_FLAGS_SHOULD_RETRY); break; case SSL_ERROR_WANT_WRITE: BIO_set_flags(b, BIO_FLAGS_WRITE|BIO_FLAGS_SHOULD_RETRY); break; case SSL_ERROR_WANT_CONNECT: BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL|BIO_FLAGS_SHOULD_RETRY); b->retry_reason=b->next_bio->retry_reason; break; default: break; } break; case BIO_CTRL_DUP: dbio=(BIO *)ptr; if (((BIO_SSL *)dbio->ptr)->ssl != NULL) SSL_free(((BIO_SSL *)dbio->ptr)->ssl); ((BIO_SSL *)dbio->ptr)->ssl=SSL_dup(ssl); ((BIO_SSL *)dbio->ptr)->renegotiate_count= ((BIO_SSL *)b->ptr)->renegotiate_count; ((BIO_SSL *)dbio->ptr)->byte_count= ((BIO_SSL *)b->ptr)->byte_count; ((BIO_SSL *)dbio->ptr)->renegotiate_timeout= ((BIO_SSL *)b->ptr)->renegotiate_timeout; ((BIO_SSL *)dbio->ptr)->last_time= ((BIO_SSL *)b->ptr)->last_time; ret=(((BIO_SSL *)dbio->ptr)->ssl != NULL); break; case BIO_C_GET_FD: ret=BIO_ctrl(ssl->rbio,cmd,num,ptr); break; case BIO_CTRL_SET_CALLBACK: { #if 0 /* FIXME: Should this be used? -- Richard Levitte */ SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED); ret = -1; #else ret=0; #endif } break; case BIO_CTRL_GET_CALLBACK: { void (**fptr)(const SSL *xssl,int type,int val); fptr=(void (**)(const SSL *xssl,int type,int val))ptr; *fptr=SSL_get_info_callback(ssl); } break; default: ret=BIO_ctrl(ssl->rbio,cmd,num,ptr); break; } return(ret); }
idevice_error_t idevice_connection_enable_ssl(idevice_connection_t connection) { if (!connection || connection->ssl_data) return IDEVICE_E_INVALID_ARG; idevice_error_t ret = IDEVICE_E_SSL_ERROR; uint32_t return_me = 0; plist_t pair_record = NULL; userpref_read_pair_record(connection->udid, &pair_record); if (!pair_record) { debug_info("ERROR: Failed enabling SSL. Unable to read pair record for udid %s.", connection->udid); return ret; } #ifdef HAVE_OPENSSL key_data_t root_cert = { NULL, 0 }; key_data_t root_privkey = { NULL, 0 }; pair_record_import_crt_with_name(pair_record, USERPREF_ROOT_CERTIFICATE_KEY, &root_cert); pair_record_import_key_with_name(pair_record, USERPREF_ROOT_PRIVATE_KEY_KEY, &root_privkey); if (pair_record) plist_free(pair_record); /* Set up OpenSSL */ if (openssl_init_done == 0) { SSL_library_init(); openssl_init_done = 1; } BIO *ssl_bio = BIO_new(BIO_s_socket()); if (!ssl_bio) { debug_info("ERROR: Could not create SSL bio."); return ret; } BIO_set_fd(ssl_bio, (int)(long)connection->data, BIO_NOCLOSE); //SSL_CTX *ssl_ctx = SSL_CTX_new(SSLv3_method()); SSL_CTX *ssl_ctx = SSL_CTX_new(SSLv3_client_method()); if (ssl_ctx == NULL) { debug_info("ERROR: Could not create SSL context."); BIO_free(ssl_bio); return ret; } BIO* membp; X509* rootCert = NULL; membp = BIO_new_mem_buf(root_cert.data, root_cert.size); PEM_read_bio_X509(membp, &rootCert, NULL, NULL); BIO_free(membp); if (SSL_CTX_use_certificate(ssl_ctx, rootCert) != 1) { debug_info("WARNING: Could not load RootCertificate"); } X509_free(rootCert); free(root_cert.data); RSA* rootPrivKey = NULL; membp = BIO_new_mem_buf(root_privkey.data, root_privkey.size); PEM_read_bio_RSAPrivateKey(membp, &rootPrivKey, NULL, NULL); BIO_free(membp); if (SSL_CTX_use_RSAPrivateKey(ssl_ctx, rootPrivKey) != 1) { debug_info("WARNING: Could not load RootPrivateKey"); } RSA_free(rootPrivKey); free(root_privkey.data); SSL *ssl = SSL_new(ssl_ctx); if (!ssl) { debug_info("ERROR: Could not create SSL object"); BIO_free(ssl_bio); SSL_CTX_free(ssl_ctx); return ret; } SSL_set_connect_state(ssl); SSL_set_verify(ssl, 0, ssl_verify_callback); SSL_set_bio(ssl, ssl_bio, ssl_bio); return_me = SSL_do_handshake(ssl); if (return_me != 1) { debug_info("ERROR in SSL_do_handshake: %s", ssl_error_to_string(SSL_get_error(ssl, return_me))); SSL_free(ssl); SSL_CTX_free(ssl_ctx); } else { ssl_data_t ssl_data_loc = (ssl_data_t)malloc(sizeof(struct ssl_data_private)); ssl_data_loc->session = ssl; ssl_data_loc->ctx = ssl_ctx; connection->ssl_data = ssl_data_loc; ret = IDEVICE_E_SUCCESS; debug_info("SSL mode enabled, cipher: %s", SSL_get_cipher(ssl)); } /* required for proper multi-thread clean up to prevent leaks */ #ifdef HAVE_ERR_REMOVE_THREAD_STATE ERR_remove_thread_state(NULL); #else ERR_remove_state(0); #endif #else ssl_data_t ssl_data_loc = (ssl_data_t)malloc(sizeof(struct ssl_data_private)); /* Set up GnuTLS... */ debug_info("enabling SSL mode"); errno = 0; gnutls_certificate_allocate_credentials(&ssl_data_loc->certificate); gnutls_certificate_client_set_retrieve_function(ssl_data_loc->certificate, internal_cert_callback); gnutls_init(&ssl_data_loc->session, GNUTLS_CLIENT); gnutls_priority_set_direct(ssl_data_loc->session, "NONE:+VERS-SSL3.0:+ANON-DH:+RSA:+AES-128-CBC:+AES-256-CBC:+SHA1:+MD5:+COMP-NULL", NULL); gnutls_credentials_set(ssl_data_loc->session, GNUTLS_CRD_CERTIFICATE, ssl_data_loc->certificate); gnutls_session_set_ptr(ssl_data_loc->session, ssl_data_loc); gnutls_x509_crt_init(&ssl_data_loc->root_cert); gnutls_x509_crt_init(&ssl_data_loc->host_cert); gnutls_x509_privkey_init(&ssl_data_loc->root_privkey); gnutls_x509_privkey_init(&ssl_data_loc->host_privkey); pair_record_import_crt_with_name(pair_record, USERPREF_ROOT_CERTIFICATE_KEY, ssl_data_loc->root_cert); pair_record_import_crt_with_name(pair_record, USERPREF_HOST_CERTIFICATE_KEY, ssl_data_loc->host_cert); pair_record_import_key_with_name(pair_record, USERPREF_ROOT_PRIVATE_KEY_KEY, ssl_data_loc->root_privkey); pair_record_import_key_with_name(pair_record, USERPREF_HOST_PRIVATE_KEY_KEY, ssl_data_loc->host_privkey); if (pair_record) plist_free(pair_record); debug_info("GnuTLS step 1..."); gnutls_transport_set_ptr(ssl_data_loc->session, (gnutls_transport_ptr_t)connection); debug_info("GnuTLS step 2..."); gnutls_transport_set_push_function(ssl_data_loc->session, (gnutls_push_func) & internal_ssl_write); debug_info("GnuTLS step 3..."); gnutls_transport_set_pull_function(ssl_data_loc->session, (gnutls_pull_func) & internal_ssl_read); debug_info("GnuTLS step 4 -- now handshaking..."); if (errno) { debug_info("WARNING: errno says %s before handshake!", strerror(errno)); } return_me = gnutls_handshake(ssl_data_loc->session); debug_info("GnuTLS handshake done..."); if (return_me != GNUTLS_E_SUCCESS) { internal_ssl_cleanup(ssl_data_loc); free(ssl_data_loc); debug_info("GnuTLS reported something wrong."); gnutls_perror(return_me); debug_info("oh.. errno says %s", strerror(errno)); } else { connection->ssl_data = ssl_data_loc; ret = IDEVICE_E_SUCCESS; debug_info("SSL mode enabled"); } #endif return ret; }
int main(int argc, char *argv[]) { if (argc < 4) { printf("UNSUPPORTED"); //for now at least return 3; } BIO *sbio; SSL_CTX *ssl_ctx; SSL *ssl; X509 *cert; int returncode = 0; char url[256]; sprintf(url, "%s:%s", argv[1], argv[2]); char ca_bundle[256]; strcpy(ca_bundle, argv[3]); //init: SSL_library_init(); SSL_load_error_strings(); ssl_ctx = SSL_CTX_new(TLSv1_client_method()); SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL); if (SSL_CTX_load_verify_locations(ssl_ctx, ca_bundle, NULL) != 1) { printf("Couldn't load certificate trust store."); returncode=1; goto end; } else { goto connect; } connect: sbio = BIO_new_ssl_connect(ssl_ctx); BIO_get_ssl(sbio, &ssl); if (!ssl) { printf("Connection failed"); returncode=2; goto connect_end; } SSL_set_tlsext_host_name(ssl, url); BIO_set_conn_hostname(sbio, url); if(SSL_do_handshake(ssl) <= 0 || !verify_cert_hostname(SSL_get_peer_certificate(ssl), argv[1])) { printf ("VERIFY FAILURE"); } else { printf ("VERIFY SUCCESS"); } X509_free(cert); BIO_ssl_shutdown(sbio); connect_end: BIO_free_all(sbio); end: SSL_CTX_free(ssl_ctx); EVP_cleanup(); ERR_free_strings(); return returncode; }
static int openssl_ssl_do_handshake(lua_State*L) { SSL* s = CHECK_OBJECT(1, SSL, "openssl.ssl"); int ret = SSL_do_handshake(s); return openssl_ssl_pushresult(L, s, ret); }
// new_connection_cb: gets called when the listen socket for the // server is ready to read (i.e. there's an incoming connection). void new_connection_cb(uv_stream_t *server, int status) { SSL *ssl; uv_tcp_t *client; connection_state *state; worker_data *worker = (worker_data *)server->data; int rc; if (status == -1) { // TODO: should we log this? return; } client = (uv_tcp_t *)malloc(sizeof(uv_tcp_t)); client->data = NULL; rc = uv_tcp_init(server->loop, client); if (rc != 0) { write_log(1, "Failed to setup TCP socket on new connection: %s", error_string(rc)); } else { rc = uv_accept(server, (uv_stream_t *)client); if (rc != 0) { uv_close((uv_handle_t *)client, close_cb); write_log(1, "Failed to accept TCP connection: %s", error_string(rc)); return; } } // The TCP connection has been accepted so now pass it off to a worker // thread to handle state = (connection_state *)malloc(sizeof(connection_state)); initialize_state(&worker->active, state); state->tcp = client; set_get_header_state(state); ssl = SSL_new(worker->ctx); if (!ssl) { uv_close((uv_handle_t *)client, close_cb); write_log(1, "Failed to create SSL context"); return; } state->ssl = ssl; // Set up OpenSSL to use a memory BIO. We'll read and write from this BIO // when the TCP connection has data or is writeable. The BIOs are set to // non-blocking mode. state->read_bio = BIO_new(BIO_s_mem()); BIO_set_nbio(state->read_bio, 1); state->write_bio = BIO_new(BIO_s_mem()); BIO_set_nbio(state->write_bio, 1); SSL_set_bio(ssl, state->read_bio, state->write_bio); client->data = (void *)state; rc = uv_read_start((uv_stream_t*)client, allocate_cb, read_cb); if (rc != 0) { uv_close((uv_handle_t *)client, close_cb); write_log(1, "Failed to start reading on client connection: %s", error_string(rc)); return; } // Start accepting the TLS connection. This will likely not // complete here and will be completed in the read_cb/do_ssl above. SSL_set_accept_state(ssl); SSL_do_handshake(ssl); }
// do_ssl: process pending data from OpenSSL and send any data that's // waiting. Returns 1 if ok, 0 if the connection should be terminated int do_ssl(connection_state *state) { BYTE *response = NULL; int response_len = 0; kssl_error_code err; // First determine whether the SSL_accept has completed. If not then any // data on the TCP connection is related to the handshake and is not // application data. if (!state->connected) { if (!SSL_is_init_finished(state->ssl)) { int rc = SSL_do_handshake(state->ssl); if (rc != 1) { switch (SSL_get_error(state->ssl, rc)) { case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: ERR_clear_error(); return 1; default: ERR_clear_error(); return 0; } } } state->connected = 1; } // Read whatever data needs to be read (controlled by state->need) while (state->need > 0) { int read = SSL_read(state->ssl, state->current, state->need); if (read <= 0) { int err = SSL_get_error(state->ssl, read); switch (err) { // Nothing to read so wait for an event notification by exiting // this function, or SSL needs to do a write (typically because of // a connection regnegotiation happening) and so an SSL_read // isn't possible right now. In either case return from this // function and wait for a callback indicating that the socket // is ready for a read. case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: ERR_clear_error(); return 1; // Connection termination case SSL_ERROR_ZERO_RETURN: ERR_clear_error(); return 0; // Something went wrong so give up on connetion default: log_ssl_error(state->ssl, read); return 0; } } // Read some number of bytes into the state->current buffer so move that // pointer on and reduce the state->need. If there's still more // needed then loop around to see if we can read it. This is // essential because we will only get a single event when data // becomes ready and will need to read it all. state->need -= read; state->current += read; if (state->need > 0) { continue; } // All the required data has been read and is in state->start. If // it's a header then do basic checks on the header and then get // ready to receive the payload if there is one. If it's the // payload then the entire header and payload can now be // processed. if (state->state == CONNECTION_STATE_GET_HEADER) { err = parse_header(state->wire_header, &state->header); if (err != KSSL_ERROR_NONE) { return 0; } state->start = 0; if (state->header.version_maj != KSSL_VERSION_MAJ) { write_log(1, "Message version mismatch %02x != %02x", state->header.version_maj, KSSL_VERSION_MAJ); write_error(state, state->header.id, KSSL_ERROR_VERSION_MISMATCH); clear_read_queue(state); free_read_state(state); set_get_header_state(state); return 1; } // If the header indicates that a payload is coming then read it // before processing the operation requested in the header if (state->header.length > 0) { if (!set_get_payload_state(state, state->header.length)) { write_log(1, "Memory allocation error"); write_error(state, state->header.id, KSSL_ERROR_INTERNAL); clear_read_queue(state); free_read_state(state); set_get_header_state(state); return 1; } continue; } } if (state->state == CONNECTION_STATE_GET_PAYLOAD) { // Do nothing here. If we reach here then we know that the // entire payload has been read. } else { // This should be unreachable. If this occurs give up processing // and reset. write_log(1, "Connection in unknown state %d", state->state); free_read_state(state); set_get_header_state(state); return 1; } // When we reach here state->header is valid and filled in and if // necessary state->start points to the payload. uv_rwlock_rdlock(pk_lock); err = kssl_operate(&state->header, state->start, privates, &response, &response_len); if (err != KSSL_ERROR_NONE) { log_err_error(); } else { queue_write(state, response, response_len); } uv_rwlock_rdunlock(pk_lock); // When this point is reached a complete header (and optional payload) // have been received and processed by the switch() statement above. So, // write the queued messages and then free the allocated memory and get // ready to receive another header. write_queued_messages(state); flush_write(state); free_read_state(state); set_get_header_state(state); // Loop around again in case there are multiple requests queued // up by OpenSSL. } return 1; }
static long bio_rdp_tls_ctrl(BIO* bio, int cmd, long num, void* ptr) { BIO* rbio; int status = -1; BIO_RDP_TLS* tls = (BIO_RDP_TLS*) bio->ptr; if (!tls) return 0; if (!tls->ssl && (cmd != BIO_C_SET_SSL)) return 0; switch (cmd) { case BIO_CTRL_RESET: SSL_shutdown(tls->ssl); if (tls->ssl->handshake_func == tls->ssl->method->ssl_connect) SSL_set_connect_state(tls->ssl); else if (tls->ssl->handshake_func == tls->ssl->method->ssl_accept) SSL_set_accept_state(tls->ssl); SSL_clear(tls->ssl); if (bio->next_bio) status = BIO_ctrl(bio->next_bio, cmd, num, ptr); else if (tls->ssl->rbio) status = BIO_ctrl(tls->ssl->rbio, cmd, num, ptr); else status = 1; break; case BIO_C_GET_FD: status = BIO_ctrl(tls->ssl->rbio, cmd, num, ptr); break; case BIO_CTRL_INFO: status = 0; break; case BIO_CTRL_SET_CALLBACK: status = 0; break; case BIO_CTRL_GET_CALLBACK: *((ULONG_PTR*) ptr) = (ULONG_PTR) SSL_get_info_callback(tls->ssl); status = 1; break; case BIO_C_SSL_MODE: if (num) SSL_set_connect_state(tls->ssl); else SSL_set_accept_state(tls->ssl); status = 1; break; case BIO_CTRL_GET_CLOSE: status = bio->shutdown; break; case BIO_CTRL_SET_CLOSE: bio->shutdown = (int) num; status = 1; break; case BIO_CTRL_WPENDING: status = BIO_ctrl(tls->ssl->wbio, cmd, num, ptr); break; case BIO_CTRL_PENDING: status = SSL_pending(tls->ssl); if (status == 0) status = BIO_pending(tls->ssl->rbio); break; case BIO_CTRL_FLUSH: BIO_clear_retry_flags(bio); status = BIO_ctrl(tls->ssl->wbio, cmd, num, ptr); BIO_copy_next_retry(bio); status = 1; break; case BIO_CTRL_PUSH: if (bio->next_bio && (bio->next_bio != tls->ssl->rbio)) { SSL_set_bio(tls->ssl, bio->next_bio, bio->next_bio); CRYPTO_add(&(bio->next_bio->references), 1, CRYPTO_LOCK_BIO); } status = 1; break; case BIO_CTRL_POP: if (bio == ptr) { if (tls->ssl->rbio != tls->ssl->wbio) BIO_free_all(tls->ssl->wbio); if (bio->next_bio) CRYPTO_add(&(bio->next_bio->references), -1, CRYPTO_LOCK_BIO); tls->ssl->wbio = tls->ssl->rbio = NULL; } status = 1; break; case BIO_C_GET_SSL: if (ptr) { *((SSL**) ptr) = tls->ssl; status = 1; } break; case BIO_C_SET_SSL: bio->shutdown = (int) num; if (ptr) tls->ssl = (SSL*) ptr; rbio = SSL_get_rbio(tls->ssl); if (rbio) { if (bio->next_bio) BIO_push(rbio, bio->next_bio); bio->next_bio = rbio; CRYPTO_add(&(rbio->references), 1, CRYPTO_LOCK_BIO); } bio->init = 1; status = 1; break; case BIO_C_DO_STATE_MACHINE: BIO_clear_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_WRITE | BIO_FLAGS_IO_SPECIAL); bio->retry_reason = 0; status = SSL_do_handshake(tls->ssl); if (status <= 0) { switch (SSL_get_error(tls->ssl, status)) { case SSL_ERROR_WANT_READ: BIO_set_flags(bio, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY); break; case SSL_ERROR_WANT_WRITE: BIO_set_flags(bio, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY); break; case SSL_ERROR_WANT_CONNECT: BIO_set_flags(bio, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY); bio->retry_reason = bio->next_bio->retry_reason; break; default: BIO_clear_flags(bio, BIO_FLAGS_SHOULD_RETRY); break; } } break; default: status = BIO_ctrl(tls->ssl->rbio, cmd, num, ptr); break; } return status; }
/* * Write data to a secure connection. */ ssize_t secure_write(Port *port, void *ptr, size_t len) { ssize_t n; #ifdef USE_SSL if (port->ssl) { int err; /* * If SSL renegotiations are enabled and we're getting close to the * limit, start one now; but avoid it if there's one already in * progress. Request the renegotiation 1kB before the limit has * actually expired. */ if (ssl_renegotiation_limit && !in_ssl_renegotiation && port->count > (ssl_renegotiation_limit - 1) * 1024L) { in_ssl_renegotiation = true; /* * The way we determine that a renegotiation has completed is by * observing OpenSSL's internal renegotiation counter. Make sure * we start out at zero, and assume that the renegotiation is * complete when the counter advances. * * OpenSSL provides SSL_renegotiation_pending(), but this doesn't * seem to work in testing. */ SSL_clear_num_renegotiations(port->ssl); SSL_set_session_id_context(port->ssl, (void *) &SSL_context, sizeof(SSL_context)); if (SSL_renegotiate(port->ssl) <= 0) ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("SSL failure during renegotiation start"))); else { int retries; /* * A handshake can fail, so be prepared to retry it, but only * a few times. */ for (retries = 0; retries++;) { if (SSL_do_handshake(port->ssl) > 0) break; /* done */ ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("SSL handshake failure on renegotiation, retrying"))); if (retries >= 20) ereport(FATAL, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("unable to complete SSL handshake"))); } } } wloop: errno = 0; n = SSL_write(port->ssl, ptr, len); err = SSL_get_error(port->ssl, n); switch (err) { case SSL_ERROR_NONE: port->count += n; break; case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: #ifdef WIN32 pgwin32_waitforsinglesocket(SSL_get_fd(port->ssl), (err == SSL_ERROR_WANT_READ) ? FD_READ | FD_CLOSE : FD_WRITE | FD_CLOSE, INFINITE); #endif goto wloop; case SSL_ERROR_SYSCALL: /* leave it to caller to ereport the value of errno */ if (n != -1) { errno = ECONNRESET; n = -1; } break; case SSL_ERROR_SSL: ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("SSL error: %s", SSLerrmessage()))); /* fall through */ case SSL_ERROR_ZERO_RETURN: errno = ECONNRESET; n = -1; break; default: ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("unrecognized SSL error code: %d", err))); errno = ECONNRESET; n = -1; break; } if (n >= 0) { /* is renegotiation complete? */ if (in_ssl_renegotiation && SSL_num_renegotiations(port->ssl) >= 1) { in_ssl_renegotiation = false; port->count = 0; } /* * if renegotiation is still ongoing, and we've gone beyond the * limit, kill the connection now -- continuing to use it can be * considered a security problem. */ if (in_ssl_renegotiation && port->count > ssl_renegotiation_limit * 1024L) ereport(FATAL, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("SSL failed to renegotiate connection before limit expired"))); } } else #endif n = send(port->sock, ptr, len, 0); return n; }
/** Perform a TLS/SSL handshake. This function will perform a TLS/SSL handshake. @param[in] Tls Pointer to the TLS object for handshake operation. @param[in] BufferIn Pointer to the most recently received TLS Handshake packet. @param[in] BufferInSize Packet size in bytes for the most recently received TLS Handshake packet. @param[out] BufferOut Pointer to the buffer to hold the built packet. @param[in, out] BufferOutSize Pointer to the buffer size in bytes. On input, it is the buffer size provided by the caller. On output, it is the buffer size in fact needed to contain the packet. @retval EFI_SUCCESS The required TLS packet is built successfully. @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE: Tls is NULL. BufferIn is NULL but BufferInSize is NOT 0. BufferInSize is 0 but BufferIn is NOT NULL. BufferOutSize is NULL. BufferOut is NULL if *BufferOutSize is not zero. @retval EFI_BUFFER_TOO_SMALL BufferOutSize is too small to hold the response packet. @retval EFI_ABORTED Something wrong during handshake. **/ EFI_STATUS EFIAPI TlsDoHandshake ( IN VOID *Tls, IN UINT8 *BufferIn, OPTIONAL IN UINTN BufferInSize, OPTIONAL OUT UINT8 *BufferOut, OPTIONAL IN OUT UINTN *BufferOutSize ) { TLS_CONNECTION *TlsConn; UINTN PendingBufferSize; INTN Ret; UINTN ErrorCode; TlsConn = (TLS_CONNECTION *) Tls; PendingBufferSize = 0; Ret = 1; if (TlsConn == NULL || \ TlsConn->Ssl == NULL || TlsConn->InBio == NULL || TlsConn->OutBio == NULL || \ BufferOutSize == NULL || \ (BufferIn == NULL && BufferInSize != 0) || \ (BufferIn != NULL && BufferInSize == 0) || \ (BufferOut == NULL && *BufferOutSize != 0)) { return EFI_INVALID_PARAMETER; } if(BufferIn == NULL && BufferInSize == 0) { // // If RequestBuffer is NULL and RequestSize is 0, and TLS session // status is EfiTlsSessionNotStarted, the TLS session will be initiated // and the response packet needs to be ClientHello. // PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio); if (PendingBufferSize == 0) { SSL_set_connect_state (TlsConn->Ssl); Ret = SSL_do_handshake (TlsConn->Ssl); PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio); } } else { PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio); if (PendingBufferSize == 0) { BIO_write (TlsConn->InBio, BufferIn, (UINT32) BufferInSize); Ret = SSL_do_handshake (TlsConn->Ssl); PendingBufferSize = (UINTN) BIO_ctrl_pending (TlsConn->OutBio); } } if (Ret < 1) { Ret = SSL_get_error (TlsConn->Ssl, (int) Ret); if (Ret == SSL_ERROR_SSL || Ret == SSL_ERROR_SYSCALL || Ret == SSL_ERROR_ZERO_RETURN) { DEBUG (( DEBUG_ERROR, "%a SSL_HANDSHAKE_ERROR State=0x%x SSL_ERROR_%a\n", __FUNCTION__, SSL_get_state (TlsConn->Ssl), Ret == SSL_ERROR_SSL ? "SSL" : Ret == SSL_ERROR_SYSCALL ? "SYSCALL" : "ZERO_RETURN" )); DEBUG_CODE_BEGIN (); while (TRUE) { ErrorCode = ERR_get_error (); if (ErrorCode == 0) { break; } DEBUG (( DEBUG_ERROR, "%a ERROR 0x%x=L%x:F%x:R%x\n", __FUNCTION__, ErrorCode, ERR_GET_LIB (ErrorCode), ERR_GET_FUNC (ErrorCode), ERR_GET_REASON (ErrorCode) )); } DEBUG_CODE_END (); return EFI_ABORTED; } } if (PendingBufferSize > *BufferOutSize) { *BufferOutSize = PendingBufferSize; return EFI_BUFFER_TOO_SMALL; } if (PendingBufferSize > 0) { *BufferOutSize = BIO_read (TlsConn->OutBio, BufferOut, (UINT32) PendingBufferSize); } else { *BufferOutSize = 0; } return EFI_SUCCESS; }
int main(int argc, char *argv[]) { BIO *sbio; SSL_CTX *ssl_ctx; SSL *ssl; X509 *server_cert; // Initialize OpenSSL OpenSSL_add_all_algorithms(); SSL_library_init(); SSL_load_error_strings(); // Check OpenSSL PRNG if(RAND_status() != 1) { fprintf(stderr, "OpenSSL PRNG not seeded with enough data."); goto error_1; } ssl_ctx = SSL_CTX_new(TLSv1_client_method()); // Enable certificate validation SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL); // Configure the CA trust store to be used if (SSL_CTX_load_verify_locations(ssl_ctx, TRUSTED_CA_PATHNAME, NULL) != 1) { fprintf(stderr, "Couldn't load certificate trust store.\n"); goto error_2; } // Only support secure cipher suites if (SSL_CTX_set_cipher_list(ssl_ctx, SECURE_CIPHER_LIST) != 1) goto error_2; // Create the SSL connection sbio = BIO_new_ssl_connect(ssl_ctx); BIO_get_ssl(sbio, &ssl); if(!ssl) { fprintf(stderr, "Can't locate SSL pointer\n"); goto error_3; } // Do the SSL handshake BIO_set_conn_hostname(sbio, TARGET_SERVER); if(SSL_do_handshake(ssl) <= 0) { // SSL Handshake failed long verify_err = SSL_get_verify_result(ssl); if (verify_err != X509_V_OK) { // It failed because the certificate chain validation failed fprintf(stderr, "Certificate chain validation failed: %s\n", X509_verify_cert_error_string(verify_err)); } else { // It failed for another reason ERR_print_errors_fp(stderr); } goto error_3; } // Recover the server's certificate server_cert = SSL_get_peer_certificate(ssl); if (server_cert == NULL) { // The handshake was successful although the server did not provide a certificate // Most likely using an insecure anonymous cipher suite... get out! goto error_4; } // Validate the hostname if (validate_hostname(TARGET_HOST, server_cert) != MatchFound) { fprintf(stderr, "Hostname validation failed.\n"); goto error_5; } // Hostname validation succeeded; we can start sending data send_http_get_and_print(sbio); error_5: X509_free(server_cert); error_4: BIO_ssl_shutdown(sbio); error_3: BIO_free_all(sbio); error_2: SSL_CTX_free(ssl_ctx); error_1: // OpenSSL cleanup EVP_cleanup(); ERR_free_strings(); return 0; }
static PyObject *PySSL_SSLdo_handshake(PySSLObject *self) { int ret; int err; int sockstate; /* Actually negotiate SSL connection */ /* XXX If SSL_do_handshake() returns 0, it's also a failure. */ sockstate = 0; do { PySocketSockObject *sock = (PySocketSockObject *) PyWeakref_GetObject(self->Socket); if (((PyObject*)sock) == Py_None) { _setSSLError("Underlying socket connection gone", PY_SSL_ERROR_NO_SOCKET, __FILE__, __LINE__); return NULL; } PySSL_BEGIN_ALLOW_THREADS ret = SSL_do_handshake(self->ssl); err = SSL_get_error(self->ssl, ret); PySSL_END_ALLOW_THREADS if(PyErr_CheckSignals()) { return NULL; } if (err == SSL_ERROR_WANT_READ) { sockstate = check_socket_and_wait_for_timeout(sock, 0); } else if (err == SSL_ERROR_WANT_WRITE) { sockstate = check_socket_and_wait_for_timeout(sock, 1); } else { sockstate = SOCKET_OPERATION_OK; } if (sockstate == SOCKET_HAS_TIMED_OUT) { PyErr_SetString(PySSLErrorObject, ERRSTR("The handshake operation timed out")); return NULL; } else if (sockstate == SOCKET_HAS_BEEN_CLOSED) { PyErr_SetString(PySSLErrorObject, ERRSTR("Underlying socket has been closed.")); return NULL; } else if (sockstate == SOCKET_TOO_LARGE_FOR_SELECT) { PyErr_SetString(PySSLErrorObject, ERRSTR("Underlying socket too large for select().")); return NULL; } else if (sockstate == SOCKET_IS_NONBLOCKING) { break; } } while (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE); if (ret < 1) return PySSL_SetError(self, ret, __FILE__, __LINE__); self->ssl->debug = 1; if (self->peer_cert) X509_free (self->peer_cert); PySSL_BEGIN_ALLOW_THREADS self->peer_cert = SSL_get_peer_certificate(self->ssl); PySSL_END_ALLOW_THREADS Py_INCREF(Py_None); return Py_None; }
void ServiceTask::run() { //logger << dlib << endl; string ip = "invalid session"; string alldatlg = "\ngot fd from parent"; SSL *ssl=NULL; BIO *sbio=NULL; BIO *io=NULL,*ssl_bio=NULL; try { int cntlen = 0; char buf[MAXBUFLENM]; strVec results; stringstream ss; string temp; //int bytes = -1; if(isSSLEnabled) { sbio=BIO_new_socket(fd,BIO_NOCLOSE); ssl=SSL_new(ctx); SSL_set_bio(ssl,sbio,sbio); io=BIO_new(BIO_f_buffer()); ssl_bio=BIO_new(BIO_f_ssl()); BIO_set_ssl(ssl_bio,ssl,BIO_CLOSE); BIO_push(io,ssl_bio); int r = SSL_accept(ssl); cout << r << endl; int bser = SSL_get_error(ssl,r); cout << bser << endl; if(r<=0) { sslHandler.error_occurred((char*)"SSL accept error",fd,ssl); return; } int er=-1; bool flag = true; while(flag) { er = BIO_gets(io,buf,BUFSIZZ-1); cout << er << endl; int bser = SSL_get_error(ssl,er); cout << bser << endl; switch(bser) { case SSL_ERROR_WANT_READ: { logger << "more to read error" << endl; break; } case SSL_ERROR_WANT_WRITE: { logger << "more to write error" << endl; break; } case SSL_ERROR_NONE: { break; } case SSL_ERROR_ZERO_RETURN: { sslHandler.error_occurred((char*)"SSL error problem",fd,ssl); if(io!=NULL)BIO_free(io); return; } default: { sslHandler.error_occurred((char*)"SSL read problem",fd,ssl); if(io!=NULL)BIO_free(io); return; } } ss << buf; //logger <<buf <<endl; if(!strcmp(buf,"\r\n") || !strcmp(buf,"\n")) break; string temp(buf); if(temp=="")continue; temp = temp.substr(0,temp.length()-1); results.push_back(temp); //logger << temp <<endl; if(temp.find("Content-Length:")!=string::npos) { std::string cntle = temp.substr(temp.find(": ")+2); cntle = cntle.substr(0,cntle.length()-1); //logger << "contne-length="<<cntle <<endl; try { cntlen = CastUtil::lexical_cast<int>(cntle); } catch(const char* ex) { logger << "bad lexical cast" <<endl; } } memset(&buf[0], 0, sizeof(buf)); } } else { int er=-1; bool flag = true; sbio=BIO_new_socket(fd,BIO_CLOSE); io=BIO_new(BIO_f_buffer()); BIO_push(io,sbio); logger << "into run method" << endl; while(flag) { er = BIO_gets(io,buf,BUFSIZZ-1); if(er==0) { close(fd); logger << "\nsocket closed before being serviced" <<flush; return; } ss << buf; if(!strcmp(buf,"\r\n") || !strcmp(buf,"\n") || er<0) break; string temp(buf); temp = temp.substr(0,temp.length()-1); results.push_back(temp); //logger << temp <<endl; if(temp.find("Content-Length:")!=string::npos) { std::string cntle = temp.substr(temp.find(": ")+2); cntle = cntle.substr(0,cntle.length()-1); //logger << "contne-length="<<cntle <<endl; try { cntlen = CastUtil::lexical_cast<int>(cntle); } catch(const char* ex) { logger << "bad lexical cast" <<endl; } } memset(&buf[0], 0, sizeof(buf)); } } ss.clear(); if(isSSLEnabled && cntlen>0) { int er=-1; if(cntlen>0) { //logger << "reading conetnt " << cntlen << endl; er = BIO_read(io,buf,cntlen); switch(SSL_get_error(ssl,er)) { case SSL_ERROR_NONE: cntlen -= er; break; case SSL_ERROR_ZERO_RETURN: { sslHandler.error_occurred((char*)"SSL error problem",fd,ssl); if(io!=NULL)BIO_free(io); return; } default: { sslHandler.error_occurred((char*)"SSL read problem",fd,ssl); if(io!=NULL)BIO_free(io); return; } } string temp(buf); results.push_back("\r"); results.push_back(temp); //logger <<buf <<endl; memset(&buf[0], 0, sizeof(buf)); } } else if(cntlen>0) { int er=-1; if(cntlen>0) { //logger << "reading conetnt " << cntlen << endl; er = BIO_read(io,buf,cntlen); if(er==0) { close(fd); logger << "\nsocket closed before being serviced" <<flush; return; } else if(er>0) { string temp(buf); results.push_back("\r"); results.push_back(temp); //logger << temp <<endl; memset(&buf[0], 0, sizeof(buf)); } } } alldatlg += "--read data"; map<string,string> params1 = *params; string webpath = serverRootDirectory + "web/"; HttpRequest* req= new HttpRequest(results,webpath); //logger << req->toString() << endl; if(req->getFile()=="") { logger << req->getFile() << endl; req->setFile("index.html"); } if(req->hasCookie()) { logger << "has the session id" << endl; if(!configData.sessatserv) req->getSession()->setSessionAttributes(req->getCookieInfo()); else { string id = req->getCookieInfoAttribute("FFEADID"); logger << id << endl; map<string,string> values = readFromSharedMemeory(id); req->getSession()->setSessionAttributes(values); } } if(configData.cntMap[req->getCntxt_name()]!="true") { req->setCntxt_name("default"); req->setCntxt_root(webpath+"default"); req->setUrl(webpath+"default"+req->getActUrl()); } //logger << req->getCntxt_name() << req->getCntxt_root() << req->getUrl() << endl; if(configData.appMap[req->getCntxt_name()]!="false") { if(dlib == NULL) { cerr << dlerror() << endl; exit(-1); } string meth1 = (req->getCntxt_name()+"checkRules"); string path1; void *mkr1 = dlsym(dlib, meth1.c_str()); if(mkr1!=NULL) { typedef string (*DCPPtr1) (string,HttpSession); DCPPtr1 f = (DCPPtr1)mkr1; path1 = f(req->getUrl(),*(req->getSession())); //logger << path1 << flush; if(path1=="FAILED") { req->setUrl(""); } else if(path1!="" && path1!=req->getUrl()) { req->setUrl(path1); } } } HttpResponse res; string ext = getFileExtension(req->getUrl()); vector<unsigned char> test; string content; string claz; bool isoAuthRes = false; long sessionTimeoutVar = configData.sessionTimeout; bool isContrl = securityHandler.handle(configData.ip_address, req, res, configData.securityObjectMap, sessionTimeoutVar, dlib, configData.cntMap); filterHandler.handleIn(req, res, configData.filterMap, dlib, ext); if(!isContrl) { isContrl = authHandler.handle(configData.autMap, configData.autpattMap, req, res, configData.filterMap, dlib, ext); } string pthwofile = req->getCntxt_name()+req->getActUrl(); if(req->getCntxt_name()!="default" && configData.cntMap[req->getCntxt_name()]=="true") { pthwofile = req->getActUrl(); } if(!isContrl) { isContrl = controllerHandler.handle(req, res, configData.urlpattMap, configData.mappattMap, dlib, ext, configData.rstCntMap, configData.mapMap, configData.urlMap, pthwofile); } /*After going through the controller the response might be blank, just set the HTTP version*/ res.setHttpVersion(req->getHttpVersion()); //logger << req->toString() << endl; if(req->getMethod()!="TRACE") { if(isContrl) { } else if(ext==".form") { formHandler.handle(req, res, configData.formMap, dlib); } else if((req->getContent_type().find("application/soap+xml")!=string::npos || req->getContent_type().find("text/xml")!=string::npos) && (req->getContent().find("<soap:Envelope")!=string::npos || req->getContent().find("<soapenv:Envelope")!=string::npos) && configData.wsdlmap[req->getFile()]==req->getCntxt_name()) { soapHandler.handle(req, res, dlib, configData.props[".xml"]); } else { bool cntrlit = scriptHandler.handle(req, res, configData.handoffs, dlib, ext, configData.props); logger << "html page requested" <<endl; if(cntrlit) { } else { cntrlit = extHandler.handle(req, res, dlib, configData.resourcePath, configData.tmplMap, configData.vwMap, ext, configData.props); } if(!cntrlit && ext==".fview") { content = fviewHandler.handle(req, res, configData.fviewmap); } else { if(res.getContent_str()=="") content = getContentStr(req->getUrl(),configData.lprops[req->getDefaultLocale()],ext); else content = res.getContent_str(); } if(content.length()==0) { res.setStatusCode("404"); res.setStatusMsg("Not Found"); //res.setContent_len(CastUtil::lexical_cast<string>(0)); } else { res.setStatusCode("200"); res.setStatusMsg("OK"); if(res.getContent_type()=="")res.setContent_type(configData.props[ext]); res.setContent_str(content); //res.setContent_len(CastUtil::lexical_cast<string>(content.length())); //sess.setAttribute("CURR",req->getUrl()); } } filterHandler.handleOut(req, res, configData.filterMap, dlib, ext); } alldatlg += "--processed data"; string h1; bool sessionchanged = !req->hasCookie(); sessionchanged |= req->getSession()->isDirty(); if(req->getConnection()!="") res.setConnection("close"); createResponse(res,sessionchanged,req->getSession()->getSessionAttributes(),req->getCookieInfoAttribute("FFEADID"), sessionTimeoutVar, configData.sessatserv); //Head should behave exactly as Get but there should be no entity body if(req->getMethod()=="HEAD") { h1 = res.generateHeadResponse(); } else if(req->getMethod()=="OPTIONS") { h1 = res.generateOptionsResponse(); } else if(req->getMethod()=="TRACE") { h1 = res.generateTraceResponse(req); } else { h1 = res.generateResponse(); } //logger << h1 << endl; if(isSSLEnabled) { int r; /* Now perform renegotiation if requested */ if(configData.client_auth==CLIENT_AUTH_REHANDSHAKE){ SSL_set_verify(ssl,SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,0); /* Stop the client from just resuming the un-authenticated session */ SSL_set_session_id_context(ssl, (const unsigned char*)&SSLHandler::s_server_auth_session_id_context, sizeof(SSLHandler::s_server_auth_session_id_context)); if(SSL_renegotiate(ssl)<=0) { sslHandler.error_occurred((char*)"SSL renegotiation error",fd,ssl); if(io!=NULL)BIO_free(io); return; } if(SSL_do_handshake(ssl)<=0) { sslHandler.error_occurred((char*)"SSL renegotiation error",fd,ssl); if(io!=NULL)BIO_free(io); return; } ssl->state=SSL_ST_ACCEPT; if(SSL_do_handshake(ssl)<=0) { sslHandler.error_occurred((char*)"SSL renegotiation error",fd,ssl); if(io!=NULL)BIO_free(io); return; } } if((r=BIO_puts(io,h1.c_str()))<=0) { sslHandler.error_occurred((char*)"send failed",fd,ssl); if(io!=NULL)BIO_free(io); return; } if((r=BIO_flush(io))<0) { sslHandler.error_occurred((char*)"Error flushing BIO",fd,ssl); if(io!=NULL)BIO_free(io); return; } sslHandler.closeSSL(fd,ssl,io); } else { int size; if ((size=send(fd,&h1[0] , h1.length(), 0)) == -1) logger << "send failed" << flush; else if(size==0) { close(fd); memset(&buf[0], 0, sizeof(buf)); logger << "socket closed for writing" << flush; return; } if(io!=NULL)BIO_free_all(io); } close(fd); memset(&buf[0], 0, sizeof(buf)); ss.clear(); //Logger::info("got new connection to process\n"+req->getFile()+" :: " + res.getStatusCode() + "\n"+req->getCntxt_name() + "\n"+req->getCntxt_root() + "\n"+req->getUrl()); delete req; logger << alldatlg << "--sent data--DONE" << flush; //sessionMap[sessId] = sess; } catch(...) { logger << "Standard exception: " << endl; } }
/* * Write data to a secure connection. */ ssize_t secure_write(Port *port, void *ptr, size_t len) { ssize_t n; #ifdef USE_SSL if (port->ssl) { int err; if (ssl_renegotiation_limit && port->count > ssl_renegotiation_limit * 1024L) { SSL_set_session_id_context(port->ssl, (void *) &SSL_context, sizeof(SSL_context)); if (SSL_renegotiate(port->ssl) <= 0) ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("SSL renegotiation failure"))); if (SSL_do_handshake(port->ssl) <= 0) ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("SSL renegotiation failure"))); if (port->ssl->state != SSL_ST_OK) ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("SSL failed to send renegotiation request"))); port->ssl->state |= SSL_ST_ACCEPT; SSL_do_handshake(port->ssl); if (port->ssl->state != SSL_ST_OK) ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("SSL renegotiation failure"))); port->count = 0; } wloop: errno = 0; n = SSL_write(port->ssl, ptr, len); err = SSL_get_error(port->ssl, n); switch (err) { case SSL_ERROR_NONE: port->count += n; break; case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: #ifdef WIN32 pgwin32_waitforsinglesocket(SSL_get_fd(port->ssl), (err == SSL_ERROR_WANT_READ) ? FD_READ | FD_CLOSE : FD_WRITE | FD_CLOSE, INFINITE); #endif goto wloop; case SSL_ERROR_SYSCALL: /* leave it to caller to ereport the value of errno */ if (n != -1) { errno = ECONNRESET; n = -1; } break; case SSL_ERROR_SSL: ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("SSL error: %s", SSLerrmessage()))); /* fall through */ case SSL_ERROR_ZERO_RETURN: errno = ECONNRESET; n = -1; break; default: ereport(COMMERROR, (errcode(ERRCODE_PROTOCOL_VIOLATION), errmsg("unrecognized SSL error code: %d", err))); n = -1; break; } } else #endif n = send(port->sock, ptr, len, 0); return n; }
void SSLAdapter::handshake() { int r = SSL_do_handshake(_ssl); if (r < 0) handleError(r); }
int tnet_dtls_socket_do_handshake(tnet_dtls_socket_handle_t* handle, const struct sockaddr_storage* remote_addr) { #if !HAVE_OPENSSL || !HAVE_OPENSSL_DTLS TSK_DEBUG_ERROR("OpenSSL or DTLS not enabled"); return -1; #else tnet_dtls_socket_t *socket = handle; int ret = 0, len; void* out_data; if (!socket) { TSK_DEBUG_ERROR("Invalid parameter"); return -1; } tsk_safeobj_lock(socket); // update remote address even if handshaking is completed if (remote_addr) { socket->remote.addr = *remote_addr; } if (socket->handshake_completed) { TSK_DEBUG_INFO("Handshake completed"); ret = 0; goto bail; } if (!socket->handshake_started) { if ((ret = SSL_do_handshake(socket->ssl)) != 1) { switch ((ret = SSL_get_error(socket->ssl, ret))) { case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: case SSL_ERROR_NONE: break; default: TSK_DEBUG_ERROR("DTLS handshake failed [%s]", ERR_error_string(ERR_get_error(), tsk_null)); _tnet_dtls_socket_raise_event_dataless(socket, tnet_dtls_socket_event_type_handshake_failed); ret = -2; goto bail; } } socket->handshake_started = (ret == SSL_ERROR_NONE); // TODO: reset for renegotiation } if ((len = (int)BIO_get_mem_data(socket->wbio, &out_data)) > 0 && out_data) { if (socket->handshake_storedata) { // e.g. when TURN is enabled we have to query handshaking data and sent it via the negotiated channel if ((int)socket->handshake_data.size < len) { if (!(socket->handshake_data.ptr = tsk_realloc(socket->handshake_data.ptr, len))) { socket->handshake_data.size = 0; socket->handshake_data.count = 0; ret = -5; goto bail; } socket->handshake_data.size = len; } socket->handshake_data.count = len; memcpy(socket->handshake_data.ptr, out_data, len); } else { int sentlen = 0; tnet_port_t port; tnet_ip_t ip; tsk_bool_t is_dgram = TNET_SOCKET_TYPE_IS_DGRAM(socket->wrapped_sock->type); const uint8_t *record_ptr, *records_ptr = out_data; tsk_size_t record_size; int records_len = len; tnet_get_sockip_n_port((const struct sockaddr *)&socket->remote.addr, &ip, &port); TSK_DEBUG_INFO("DTLS data handshake to send with len = %d, from(%.*s/%d) to(%.*s/%d)", len, (int)sizeof(socket->wrapped_sock->ip), socket->wrapped_sock->ip, socket->wrapped_sock->port, (int)sizeof(ip), ip, port); //!\ IP fragmentation issues must be avoided even if the local transport is TCP/TLS because the relayed (TURN) transport could be UDP while (records_len > 0 && (ret = tnet_dtls_socket_get_record_first(records_ptr, (tsk_size_t)records_len, &record_ptr, &record_size)) == 0) { if (is_dgram) { sentlen += tnet_sockfd_sendto(socket->wrapped_sock->fd, (const struct sockaddr *)&socket->remote.addr, record_ptr, record_size); } else { sentlen += tnet_socket_send_stream(socket->wrapped_sock, record_ptr, record_size); } records_len -= (int)record_size; records_ptr += record_size; } TSK_DEBUG_INFO("DTLS data handshake sent len = %d", sentlen); } } BIO_reset(socket->rbio); BIO_reset(socket->wbio); if ((socket->handshake_completed = SSL_is_init_finished(socket->ssl))) { TSK_DEBUG_INFO("DTLS handshake completed"); #if HAVE_OPENSSL_DTLS_SRTP if (socket->use_srtp){ #if !defined(SRTP_MAX_KEY_LEN) # define cipher_key_length (128 >> 3) // rfc5764 4.1.2. SRTP Protection Profiles # define cipher_salt_length (112 >> 3) // rfc5764 4.1.2. SRTP Protection Profiles // "cipher_key_length" is also equal to srtp_profile_get_master_key_length(srtp_profile_aes128_cm_sha1_80) // "cipher_salt_length" is also srtp_profile_get_master_salt_length(srtp_profile_aes128_cm_sha1_80) # define SRTP_MAX_KEY_LEN (cipher_key_length + cipher_salt_length) #endif /* SRTP_MAX_KEY_LEN */ #define EXTRACTOR_dtls_srtp_text "EXTRACTOR-dtls_srtp" #define EXTRACTOR_dtls_srtp_text_len 19 uint8_t keying_material[SRTP_MAX_KEY_LEN << 1]; static const tsk_size_t keying_material_size = sizeof(keying_material); /*if(socket->use_srtp)*/{ SRTP_PROTECTION_PROFILE *p = SSL_get_selected_srtp_profile(socket->ssl); if (!p) { TSK_DEBUG_ERROR("SSL_get_selected_srtp_profile() returned null [%s]", ERR_error_string(ERR_get_error(), tsk_null)); ret = -2; goto bail; } // alert user _tnet_dtls_socket_raise_event(socket, tnet_dtls_socket_event_type_dtls_srtp_profile_selected, p->name, tsk_strlen(p->name)); memset(keying_material, 0, sizeof(keying_material)); // rfc5764 - 4.2. Key Derivation ret = SSL_export_keying_material(socket->ssl, keying_material, sizeof(keying_material), EXTRACTOR_dtls_srtp_text, EXTRACTOR_dtls_srtp_text_len, tsk_null, 0, 0); if (ret != 1) { // alert listener _tnet_dtls_socket_raise_event_dataless(socket, tnet_dtls_socket_event_type_error); TSK_DEBUG_ERROR("SSL_export_keying_material() failed [%s]", ERR_error_string(ERR_get_error(), tsk_null)); ret = -2; goto bail; } } // alert listener _tnet_dtls_socket_raise_event(socket, tnet_dtls_socket_event_type_dtls_srtp_data, keying_material, keying_material_size); } #endif /* HAVE_OPENSSL_DTLS_SRTP */ _tnet_dtls_socket_raise_event_dataless(socket, tnet_dtls_socket_event_type_handshake_succeed); } ret = 0; // clear "ret", error will directly jump to "bail:" bail: tsk_safeobj_unlock(socket); return ret; #endif }
/* * Write data to a secure connection. */ ssize_t secure_write(Port *port, void *ptr, size_t len) { ssize_t n; #ifdef USE_SSL if (port->ssl) { int err; if (ssl_renegotiation_limit && port->count > ssl_renegotiation_limit * 1024L) { SSL_set_session_id_context(port->ssl, (void *) &SSL_context, sizeof(SSL_context)); if (SSL_renegotiate(port->ssl) <= 0) { report_commerror("SSL renegotiation failure"); } if (SSL_do_handshake(port->ssl) <= 0) { report_commerror("SSL renegotiation failure"); } if (port->ssl->state != SSL_ST_OK) { report_commerror("SSL failed to send renegotiation request"); } port->ssl->state |= SSL_ST_ACCEPT; SSL_do_handshake(port->ssl); if (port->ssl->state != SSL_ST_OK) { report_commerror("SSL renegotiation failure"); } port->count = 0; } wloop: errno = 0; n = SSL_write(port->ssl, ptr, len); err = SSL_get_error(port->ssl, n); const int ERR_MSG_LEN = ERROR_BUF_SIZE + 20; char err_msg[ERR_MSG_LEN]; switch (err) { case SSL_ERROR_NONE: port->count += n; break; case SSL_ERROR_WANT_READ: case SSL_ERROR_WANT_WRITE: #ifdef WIN32 pgwin32_waitforsinglesocket(SSL_get_fd(port->ssl), (err == SSL_ERROR_WANT_READ) ? FD_READ | FD_CLOSE : FD_WRITE | FD_CLOSE, INFINITE); #endif goto wloop; case SSL_ERROR_SYSCALL: /* leave it to caller to ereport the value of errno */ if (n != -1) { errno = ECONNRESET; n = -1; } break; case SSL_ERROR_SSL: snprintf((char *)&err_msg, ERR_MSG_LEN, "SSL error: %s", SSLerrmessage()); report_commerror(err_msg); /* fall through */ case SSL_ERROR_ZERO_RETURN: errno = ECONNRESET; n = -1; break; default: snprintf((char *)&err_msg, ERR_MSG_LEN, "unrecognized SSL error code: %d", err); report_commerror(err_msg); n = -1; break; } } else #endif { prepare_for_client_write(); n = send(port->sock, ptr, len, 0); client_write_ended(); } return n; }
extern "C" int32_t CryptoNative_SslDoHandshake(SSL* ssl) { ERR_clear_error(); return SSL_do_handshake(ssl); }
int main(int argc, char *argv[]) { unsigned int bundleLength=0, urlLength=0; if (argc == 3) { printf("UNSUPPORTED\n"); //for now at least return EXIT_SUCCESS; } else if (argc != 4) { printf("usage: %s <host> <port> <ca-bundle>\n", argv[0]); return EXIT_FAILURE; } else if ((urlLength = strlen(argv[1]) + strlen(argv[2]) + 2) > MAX_LENGTH) { printf("Too long URL: %d characters, max: %d\n", urlLength, MAX_LENGTH); return EXIT_FAILURE; } else if ((bundleLength = strlen(argv[3]) + 1) > MAX_LENGTH) { printf("Too long ca-bundle filepath: %d characters, max: %d\n", bundleLength, MAX_LENGTH); return EXIT_FAILURE; } char url[1024]; snprintf(url, sizeof(url), "%s:%s", argv[1], argv[2]); char ca_bundle[1024]; memcpy(ca_bundle, argv[3], bundleLength); int exitvalue = 0; BIO *sbio; SSL_CTX *ssl_ctx; SSL *ssl; X509 *cert; const char *servername = NULL; X509_VERIFY_PARAM *param = NULL; SSL_load_error_strings(); SSL_library_init(); ssl_ctx = SSL_CTX_new(TLS_method()); //if you are using an older version of openssl, you may need to change this into for example: TLSv1_2_client_method param = SSL_CTX_get0_param(ssl_ctx); //set certificate verify //https://wiki.openssl.org/index.php/Hostname_validation X509_VERIFY_PARAM_set_hostflags(param, X509_CHECK_FLAG_NO_PARTIAL_WILDCARDS); X509_VERIFY_PARAM_set1_host(param, argv[1], 0); SSL_CTX_set_verify(ssl_ctx, SSL_VERIFY_PEER, NULL); if (SSL_CTX_load_verify_locations(ssl_ctx, ca_bundle, NULL) != 1) { printf("Couldn't load certificate trust store\n"); printf("%s\n", ERR_reason_error_string(ERR_get_error())); exitvalue=EXIT_FAILURE; goto end; } sbio = BIO_new_ssl_connect(ssl_ctx); BIO_get_ssl(sbio, &ssl); if (!ssl) { printf("Connection failed\n"); printf("%s\n", ERR_reason_error_string(ERR_get_error())); exitvalue=EXIT_FAILURE; goto connect_end; } //handshake SSL_set_tlsext_host_name(ssl, url); BIO_set_conn_hostname(sbio, url); if(SSL_do_handshake(ssl) <= 0) { unsigned long int error = ERR_get_error(); printf("%s\n", ERR_reason_error_string(error)); printf("REJECT\n"); } else { printf ("ACCEPT\n"); } connect_end: BIO_free_all(sbio); end: SSL_CTX_free(ssl_ctx); EVP_cleanup(); ERR_free_strings(); return exitvalue; }
int dtls_try_handshake(struct openconnect_info *vpninfo) { int ret = SSL_do_handshake(vpninfo->dtls_ssl); if (ret == 1) { const char *c; if (!strcmp(vpninfo->dtls_cipher, "PSK-NEGOTIATE")) { /* For PSK-NEGOTIATE, we have to determine the tunnel MTU * for ourselves based on the base MTU */ int data_mtu = vpninfo->cstp_basemtu; if (vpninfo->peer_addr->sa_family == IPPROTO_IPV6) data_mtu -= 40; /* IPv6 header */ else data_mtu -= 20; /* Legacy IP header */ data_mtu -= 8; /* UDP header */ if (data_mtu < 0) { vpn_progress(vpninfo, PRG_ERR, _("Peer MTU %d too small to allow DTLS\n"), vpninfo->cstp_basemtu); goto nodtls; } /* Reduce it by one because that's the payload header *inside* * the encryption */ data_mtu = dtls_set_mtu(vpninfo, data_mtu) - 1; if (data_mtu < 0) goto nodtls; if (data_mtu < vpninfo->ip_info.mtu) { vpn_progress(vpninfo, PRG_INFO, _("DTLS MTU reduced to %d\n"), data_mtu); vpninfo->ip_info.mtu = data_mtu; } } else if (!SSL_session_reused(vpninfo->dtls_ssl)) { /* Someone attempting to hijack the DTLS session? * A real server would never allow a full session * establishment instead of the agreed resume. */ vpn_progress(vpninfo, PRG_ERR, _("DTLS session resume failed; possible MITM attack. Disabling DTLS.\n")); nodtls: dtls_close(vpninfo); SSL_CTX_free(vpninfo->dtls_ctx); vpninfo->dtls_ctx = NULL; vpninfo->dtls_attempt_period = 0; vpninfo->dtls_state = DTLS_DISABLED; return -EIO; } vpninfo->dtls_state = DTLS_CONNECTED; vpn_progress(vpninfo, PRG_INFO, _("Established DTLS connection (using OpenSSL). Ciphersuite %s.\n"), SSL_get_cipher(vpninfo->dtls_ssl)); c = openconnect_get_dtls_compression(vpninfo); if (c) { vpn_progress(vpninfo, PRG_INFO, _("DTLS connection compression using %s.\n"), c); } vpninfo->dtls_times.last_rekey = vpninfo->dtls_times.last_rx = vpninfo->dtls_times.last_tx = time(NULL); /* From about 8.4.1(11) onwards, the ASA seems to get very unhappy if we resend ChangeCipherSpec messages after the initial setup. This was "fixed" in OpenSSL 1.0.0e for RT#2505, but it's not clear if that was the right fix. What happens if the original packet *does* get lost? Surely we *wanted* the retransmits, because without them the server will never be able to decrypt anything we send? Oh well, our retransmitted packets upset the server because we don't get the Cisco-compatibility right (this is one of the areas in which Cisco's DTLS differs from the RFC4347 spec), and DPD should help us notice if *nothing* is getting through. */ #if OPENSSL_VERSION_NUMBER >= 0x10100000L /* OpenSSL 1.1.0 or above. Do nothing. The SSLeay() function got renamed, and it's a pointless check in this case anyway because there's *no* chance that we linked against 1.1.0 and are running against something older than 1.0.0e. */ #elif OPENSSL_VERSION_NUMBER >= 0x1000005fL /* OpenSSL 1.0.0e or above doesn't resend anyway; do nothing. However, if we were *built* against 1.0.0e or newer, but at runtime we find that we are being run against an older version, warn about it. */ if (SSLeay() < 0x1000005fL) { vpn_progress(vpninfo, PRG_ERR, _("Your OpenSSL is older than the one you built against, so DTLS may fail!")); } #elif defined(HAVE_DTLS1_STOP_TIMER) /* * This works for any normal OpenSSL that supports * Cisco DTLS compatibility (0.9.8m to 1.0.0d inclusive, * and even later versions although it isn't needed there. */ dtls1_stop_timer(vpninfo->dtls_ssl); #elif defined(BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT) /* * Debian restricts visibility of dtls1_stop_timer() * so do it manually. This version also works on all * sane versions of OpenSSL: */ memset(&(vpninfo->dtls_ssl->d1->next_timeout), 0, sizeof((vpninfo->dtls_ssl->d1->next_timeout))); vpninfo->dtls_ssl->d1->timeout_duration = 1; BIO_ctrl(SSL_get_rbio(vpninfo->dtls_ssl), BIO_CTRL_DGRAM_SET_NEXT_TIMEOUT, 0, &(vpninfo->dtls_ssl->d1->next_timeout)); #elif defined(BIO_CTRL_DGRAM_SET_TIMEOUT) /* * OK, here it gets more fun... this shoul handle the case * of older OpenSSL which has the Cisco DTLS compatibility * backported, but *not* the fix for RT#1922. */ BIO_ctrl(SSL_get_rbio(vpninfo->dtls_ssl), BIO_CTRL_DGRAM_SET_TIMEOUT, 0, NULL); #else /* * And if they don't have any of the above, they probably * don't have RT#1829 fixed either, but that's OK because * that's the "fix" that *introduces* the timeout we're * trying to disable. So do nothing... */ #endif dtls_detect_mtu(vpninfo); return 0; } ret = SSL_get_error(vpninfo->dtls_ssl, ret); if (ret == SSL_ERROR_WANT_WRITE || ret == SSL_ERROR_WANT_READ) { static int badossl_bitched = 0; if (time(NULL) < vpninfo->new_dtls_started + 12) return 0; if (((OPENSSL_VERSION_NUMBER >= 0x100000b0L && OPENSSL_VERSION_NUMBER <= 0x100000c0L) || \ (OPENSSL_VERSION_NUMBER >= 0x10001040L && OPENSSL_VERSION_NUMBER <= 0x10001060L) || \ OPENSSL_VERSION_NUMBER == 0x10002000L) && !badossl_bitched) { badossl_bitched = 1; vpn_progress(vpninfo, PRG_ERR, _("DTLS handshake timed out\n")); vpn_progress(vpninfo, PRG_ERR, _("This is probably because your OpenSSL is broken\n" "See http://rt.openssl.org/Ticket/Display.html?id=2984\n")); } else { vpn_progress(vpninfo, PRG_DEBUG, _("DTLS handshake timed out\n")); } } vpn_progress(vpninfo, PRG_ERR, _("DTLS handshake failed: %d\n"), ret); openconnect_report_ssl_errors(vpninfo); dtls_close(vpninfo); vpninfo->dtls_state = DTLS_SLEEPING; time(&vpninfo->new_dtls_started); return -EINVAL; }
static int tls_drv_control(ErlDrvData handle, unsigned int command, char *buf, int len, char **rbuf, int rlen) { tls_data *d = (tls_data *)handle; int res; int size; ErlDrvBinary *b; X509 *cert; unsigned int flags = command; command &= 0xffff; ERR_clear_error(); switch (command) { case SET_CERTIFICATE_FILE_ACCEPT: case SET_CERTIFICATE_FILE_CONNECT: { time_t mtime = 0; SSL_CTX *ssl_ctx = hash_table_lookup(buf, &mtime); if (is_key_file_modified(buf, &mtime) || ssl_ctx == NULL) { SSL_CTX *ctx; hash_table_insert(buf, mtime, NULL); ctx = SSL_CTX_new(SSLv23_method()); die_unless(ctx, "SSL_CTX_new failed"); res = SSL_CTX_use_certificate_chain_file(ctx, buf); die_unless(res > 0, "SSL_CTX_use_certificate_file failed"); res = SSL_CTX_use_PrivateKey_file(ctx, buf, SSL_FILETYPE_PEM); die_unless(res > 0, "SSL_CTX_use_PrivateKey_file failed"); res = SSL_CTX_check_private_key(ctx); die_unless(res > 0, "SSL_CTX_check_private_key failed"); SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); SSL_CTX_set_default_verify_paths(ctx); #ifdef SSL_MODE_RELEASE_BUFFERS SSL_CTX_set_mode(ctx, SSL_MODE_RELEASE_BUFFERS); #endif if (command == SET_CERTIFICATE_FILE_ACCEPT) { SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER|SSL_VERIFY_CLIENT_ONCE, verify_callback); } ssl_ctx = ctx; hash_table_insert(buf, mtime, ssl_ctx); } d->ssl = SSL_new(ssl_ctx); die_unless(d->ssl, "SSL_new failed"); if (flags & VERIFY_NONE) SSL_set_verify(d->ssl, SSL_VERIFY_NONE, verify_callback); d->bio_read = BIO_new(BIO_s_mem()); d->bio_write = BIO_new(BIO_s_mem()); SSL_set_bio(d->ssl, d->bio_read, d->bio_write); if (command == SET_CERTIFICATE_FILE_ACCEPT) { SSL_set_options(d->ssl, SSL_OP_NO_TICKET); SSL_set_accept_state(d->ssl); } else { SSL_set_options(d->ssl, SSL_OP_NO_SSLv2|SSL_OP_NO_TICKET); SSL_set_connect_state(d->ssl); } break; } case SET_ENCRYPTED_INPUT: die_unless(d->ssl, "SSL not initialized"); BIO_write(d->bio_read, buf, len); break; case SET_DECRYPTED_OUTPUT: die_unless(d->ssl, "SSL not initialized"); res = SSL_write(d->ssl, buf, len); if (res <= 0) { res = SSL_get_error(d->ssl, res); if (res == SSL_ERROR_WANT_READ || res == SSL_ERROR_WANT_WRITE) { b = driver_alloc_binary(1); b->orig_bytes[0] = 2; *rbuf = (char *)b; return 1; } else { die_unless(0, "SSL_write failed"); } } break; case GET_ENCRYPTED_OUTPUT: die_unless(d->ssl, "SSL not initialized"); size = BUF_SIZE + 1; rlen = 1; b = driver_alloc_binary(size); b->orig_bytes[0] = 0; while ((res = BIO_read(d->bio_write, b->orig_bytes + rlen, BUF_SIZE)) > 0) { //printf("%d bytes of encrypted data read from state machine\r\n", res); rlen += res; size += BUF_SIZE; b = driver_realloc_binary(b, size); } b = driver_realloc_binary(b, rlen); *rbuf = (char *)b; return rlen; case GET_DECRYPTED_INPUT: if (!SSL_is_init_finished(d->ssl)) { res = SSL_do_handshake(d->ssl); if (res <= 0) die_unless(SSL_get_error(d->ssl, res) == SSL_ERROR_WANT_READ, "SSL_do_handshake failed"); } else { size = BUF_SIZE + 1; rlen = 1; b = driver_alloc_binary(size); b->orig_bytes[0] = 0; while ((res = SSL_read(d->ssl, b->orig_bytes + rlen, BUF_SIZE)) > 0) { //printf("%d bytes of decrypted data read from state machine\r\n",res); rlen += res; size += BUF_SIZE; b = driver_realloc_binary(b, size); } if (res < 0) { int err = SSL_get_error(d->ssl, res); if (err == SSL_ERROR_WANT_READ) { //printf("SSL_read wants more data\r\n"); //return 0; } // TODO } b = driver_realloc_binary(b, rlen); *rbuf = (char *)b; return rlen; } break; case GET_PEER_CERTIFICATE: cert = SSL_get_peer_certificate(d->ssl); if (cert == NULL) { b = driver_alloc_binary(1); b->orig_bytes[0] = 1; *rbuf = (char *)b; return 1; } else { unsigned char *tmp_buf; rlen = i2d_X509(cert, NULL); if (rlen >= 0) { rlen++; b = driver_alloc_binary(rlen); b->orig_bytes[0] = 0; tmp_buf = (unsigned char *)&b->orig_bytes[1]; i2d_X509(cert, &tmp_buf); X509_free(cert); *rbuf = (char *)b; return rlen; } else X509_free(cert); } break; case GET_VERIFY_RESULT: b = driver_alloc_binary(1); b->orig_bytes[0] = SSL_get_verify_result(d->ssl); *rbuf = (char *)b; return 1; break; } b = driver_alloc_binary(1); b->orig_bytes[0] = 0; *rbuf = (char *)b; return 1; }
static void openssl_poll (GstDtlsConnection * self) { int ret; int error; log_state (self, "poll: before handshake"); ERR_clear_error (); ret = SSL_do_handshake (self->priv->ssl); log_state (self, "poll: after handshake"); switch (ret) { case 1: if (!self->priv->keys_exported) { GST_INFO_OBJECT (self, "handshake just completed successfully, exporting keys"); export_srtp_keys (self); } else { GST_INFO_OBJECT (self, "handshake is completed"); } return; case 0: GST_DEBUG_OBJECT (self, "do_handshake encountered EOF"); break; case -1: GST_DEBUG_OBJECT (self, "do_handshake encountered BIO error"); break; default: GST_DEBUG_OBJECT (self, "do_handshake returned %d", ret); } error = SSL_get_error (self->priv->ssl, ret); switch (error) { case SSL_ERROR_NONE: GST_WARNING_OBJECT (self, "no error, handshake should be done"); break; case SSL_ERROR_SSL: GST_ERROR_OBJECT (self, "SSL error"); ERR_print_errors_cb (ssl_err_cb, self); return; case SSL_ERROR_WANT_READ: GST_LOG_OBJECT (self, "SSL wants read"); break; case SSL_ERROR_WANT_WRITE: GST_LOG_OBJECT (self, "SSL wants write"); break; case SSL_ERROR_SYSCALL:{ gchar message[1024] = "<unknown>"; gint syserror; #ifdef G_OS_WIN32 syserror = WSAGetLastError (); FormatMessage (FORMAT_MESSAGE_FROM_SYSTEM, NULL, syserror, 0, message, sizeof message, NULL); #else syserror = errno; strerror_r (syserror, message, sizeof message); #endif GST_CAT_LEVEL_LOG (GST_CAT_DEFAULT, syserror != 0 ? GST_LEVEL_WARNING : GST_LEVEL_LOG, self, "SSL syscall error: errno %d: %s", syserror, message); break; } default: GST_WARNING_OBJECT (self, "Unknown SSL error: %d, ret: %d", error, ret); } ERR_print_errors_cb (ssl_warn_cb, self); }
static Ecore_Con_Ssl_Error _ecore_con_ssl_server_init_openssl(Ecore_Con_Server *svr) { int ret = -1; switch (svr->ssl_state) { case ECORE_CON_SSL_STATE_DONE: return ECORE_CON_SSL_ERROR_NONE; case ECORE_CON_SSL_STATE_INIT: SSL_ERROR_CHECK_GOTO_ERROR(!(svr->ssl = SSL_new(svr->ssl_ctx))); SSL_ERROR_CHECK_GOTO_ERROR(!SSL_set_fd(svr->ssl, svr->fd)); SSL_set_connect_state(svr->ssl); svr->ssl_state = ECORE_CON_SSL_STATE_HANDSHAKING; case ECORE_CON_SSL_STATE_HANDSHAKING: if (!svr->ssl) { DBG("Server was previously lost, going to error condition"); goto error; } ret = SSL_do_handshake(svr->ssl); svr->ssl_err = SSL_get_error(svr->ssl, ret); SSL_ERROR_CHECK_GOTO_ERROR((svr->ssl_err == SSL_ERROR_SYSCALL) || (svr->ssl_err == SSL_ERROR_SSL)); if (ret == 1) { svr->handshaking = EINA_FALSE; svr->ssl_state = ECORE_CON_SSL_STATE_DONE; } else { if (svr->ssl_err == SSL_ERROR_WANT_READ) ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_READ); else if (svr->ssl_err == SSL_ERROR_WANT_WRITE) ecore_main_fd_handler_active_set(svr->fd_handler, ECORE_FD_WRITE); return ECORE_CON_SSL_ERROR_NONE; } default: break; } #ifdef ISCOMFITOR { /* print session info into DBG */ SSL_SESSION *s; BIO *b; char log[4096]; memset(log, 0, sizeof(log)); s = SSL_get_session(svr->ssl); b = BIO_new(BIO_s_mem()); SSL_SESSION_print(b, s); while (BIO_read(b, log, sizeof(log)) > 0) DBG("%s", log); BIO_free(b); } #endif if (!svr->verify) /* not verifying certificates, so we're done! */ return ECORE_CON_SSL_ERROR_NONE; { X509 *cert; SSL_set_verify(svr->ssl, SSL_VERIFY_PEER, NULL); /* use CRL/CA lists to verify */ cert = SSL_get_peer_certificate(svr->ssl); if (cert) { char buf[256] = {0}; SSL_ERROR_CHECK_GOTO_ERROR(SSL_get_verify_result(svr->ssl)); X509_NAME_get_text_by_NID(X509_get_subject_name(cert), NID_subject_alt_name, buf, sizeof(buf)); if (buf[0]) SSL_ERROR_CHECK_GOTO_ERROR(!_openssl_name_verify(buf, svr->name)); else { X509_NAME_get_text_by_NID(X509_get_subject_name(cert), NID_commonName, buf, sizeof(buf)); SSL_ERROR_CHECK_GOTO_ERROR(!_openssl_name_verify(buf, svr->name)); } } } DBG("SSL certificate verification succeeded!"); return ECORE_CON_SSL_ERROR_NONE; error: _openssl_print_errors(); _ecore_con_ssl_server_shutdown_openssl(svr); return ECORE_CON_SSL_ERROR_SERVER_INIT_FAILED; }
int rtcdc_parse_offer_sdp(struct rtcdc_peer_connection *peer, const char *offer) { if (peer == NULL || offer == NULL) return -1; if (peer->transport == NULL) { if (create_rtcdc_transport(peer, RTCDC_PEER_ROLE_CLIENT) < 0) return -1; } char **lines; lines = g_strsplit(offer, "\r\n", 0); char buf[BUFFER_SIZE]; memset(buf, 0, sizeof buf); int pos = 0; int remote_port = 0; for (int i = 0; lines && lines[i]; ++i) { #ifdef LEGACY_SDP if (g_str_has_prefix(lines[i], "m=application")) { char **columns = g_strsplit(lines[i], " ", 0); remote_port = atoi(columns[3]); #else if (g_str_has_prefix(lines[i], "a=sctp-port:")) { char **columns = g_strsplit(lines[i], ":", 0); remote_port = atoi(columns[1]); #endif if (remote_port <= 0) return -1; peer->transport->sctp->remote_port = remote_port; g_strfreev(columns); } else if (g_str_has_prefix(lines[i], "a=setup:")) { char **columns = g_strsplit(lines[i], ":", 0); if (strcmp(columns[1], "active") == 0 && peer->role == RTCDC_PEER_ROLE_CLIENT) { peer->role = RTCDC_PEER_ROLE_SERVER; } else if (strcmp(columns[1], "passive") == 0 && peer->role == RTCDC_PEER_ROLE_SERVER) { //peer->role = RTCDC_PEER_ROLE_CLIENT; peer->role = RTCDC_PEER_ROLE_SERVER; } else { // actpass // nothing to do } g_strfreev(columns); } pos += sprintf(buf + pos, "%s\n", lines[i]); } g_strfreev(lines); return parse_remote_sdp(peer->transport->ice, buf); } int rtcdc_parse_candidate_sdp(struct rtcdc_peer_connection *peer, const char *candidates) { if (peer == NULL || peer->transport == NULL) return -1; return parse_remote_candidate_sdp(peer->transport->ice, candidates); } struct rtcdc_data_channel * rtcdc_create_data_channel(struct rtcdc_peer_connection *peer, const char *label, const char *protocol, rtcdc_on_open_cb on_open, rtcdc_on_message_cb on_message, rtcdc_on_close_cb on_close, void *user_data) { if (peer == NULL || peer->transport == NULL) return NULL; struct rtcdc_transport *transport = peer->transport; struct sctp_transport *sctp = transport->sctp; int i; for (i = 0; i < RTCDC_MAX_CHANNEL_NUM; ++i) { if (peer->channels[i]) continue; break; } if (i == RTCDC_MAX_CHANNEL_NUM) return NULL; struct rtcdc_data_channel *ch = (struct rtcdc_data_channel *)calloc(1, sizeof *ch); if (ch == NULL) return NULL; ch->on_open = on_open; ch->on_message = on_message; ch->on_close = on_close; ch->user_data = user_data; ch->sctp = sctp; struct dcep_open_message *req; int rlen = sizeof *req + strlen(label) + strlen(protocol); req = (struct dcep_open_message *)calloc(1, rlen); if (req == NULL) goto open_channel_err; ch->type = DATA_CHANNEL_RELIABLE; ch->state = RTCDC_CHANNEL_STATE_CONNECTING; if (label) ch->label = strdup(label); if (protocol) ch->protocol = strdup(protocol); ch->sid = sctp->stream_cursor; sctp->stream_cursor += 2; req->message_type = DATA_CHANNEL_OPEN; req->channel_type = ch->type; req->priority = htons(0); req->reliability_param = htonl(0); if (label) req->label_length = htons(strlen(label)); if (protocol) req->protocol_length = htons(strlen(protocol)); memcpy(req->label_and_protocol, label, strlen(label)); memcpy(req->label_and_protocol + strlen(label), protocol, strlen(protocol)); int ret = send_sctp_message(sctp, req, rlen, ch->sid, WEBRTC_CONTROL_PPID); free(req); if (ret < 0) goto open_channel_err; if (0) { open_channel_err: free(ch); ch = NULL; } peer->channels[i] = ch; return ch; } void rtcdc_destroy_data_channel(struct rtcdc_data_channel *channel) { if (channel == NULL) return; // todo: close channel if (channel->label) free(channel->label); if (channel->protocol) free(channel->protocol); } int rtcdc_send_message(struct rtcdc_data_channel *channel, int datatype, void *data, size_t len) { if (channel == NULL) return -1; int ppid; if (datatype == RTCDC_DATATYPE_STRING) { if (data == NULL || len == 0) ppid = WEBRTC_STRING_EMPTY_PPID; else ppid = WEBRTC_STRING_PPID; } else if (datatype == RTCDC_DATATYPE_BINARY) { if (data == NULL || len == 0) ppid = WEBRTC_BINARY_EMPTY_PPID; else ppid = WEBRTC_BINARY_PPID; } else return -1; return send_sctp_message(channel->sctp, data, len, channel->sid, ppid); } static gpointer startup_thread(gpointer user_data) { struct rtcdc_peer_connection *peer = (struct rtcdc_peer_connection *)user_data; struct rtcdc_transport *transport = peer->transport; struct ice_transport *ice = transport->ice; struct dtls_transport *dtls = transport->dtls; struct sctp_transport *sctp = transport->sctp; while (!peer->exit_thread && !ice->negotiation_done) g_usleep(2500); if (peer->exit_thread) return NULL; #ifdef DEBUG_SCTP fprintf(stderr, "ICE negotiation done\n"); #endif if (peer->role == RTCDC_PEER_ROLE_CLIENT) SSL_set_connect_state(dtls->ssl); else SSL_set_accept_state(dtls->ssl); SSL_do_handshake(dtls->ssl); while (!peer->exit_thread && !dtls->handshake_done) g_usleep(2500); if (peer->exit_thread) return NULL; #ifdef DEBUG_SCTP fprintf(stderr, "DTLS handshake done\n"); #endif if (peer->role == RTCDC_PEER_ROLE_CLIENT) { sctp->stream_cursor = 0; // use even streams struct sockaddr_conn sconn; memset(&sconn, 0, sizeof sconn); sconn.sconn_family = AF_CONN; sconn.sconn_port = htons(sctp->remote_port); sconn.sconn_addr = (void *)sctp; #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) sconn.sconn_len = sizeof *sctp; #endif fprintf(stderr, "usrsctp_connect ... \n"); if (usrsctp_connect(sctp->sock, (struct sockaddr *)&sconn, sizeof sconn) < 0) { #ifdef DEBUG_SCTP fprintf(stderr, "SCTP connection failed\n"); #endif } else { #ifdef DEBUG_SCTP fprintf(stderr, "SCTP connected\n"); #endif sctp->handshake_done = TRUE; if (peer->on_connect) peer->on_connect(peer, peer->user_data); } } else { sctp->stream_cursor = 1; // use odd streams struct sockaddr_conn sconn; memset(&sconn, 0, sizeof sconn); sconn.sconn_family = AF_CONN; sconn.sconn_port = htons(sctp->local_port); sconn.sconn_addr = (void *)sctp; #if defined(__APPLE__) || defined(__FreeBSD__) || defined(__OpenBSD__) sconn.sconn_len = sizeof *sctp; #endif usrsctp_listen(sctp->sock, 1); socklen_t len = sizeof sconn; fprintf(stderr, "usrsctp_accept ... \n"); struct socket *s = usrsctp_accept(sctp->sock, (struct sockaddr *)&sconn, &len); if (s) { #ifdef DEBUG_SCTP fprintf(stderr, "SCTP accepted\n"); #endif sctp->handshake_done = TRUE; struct socket *t = sctp->sock; sctp->sock = s; usrsctp_close(t); if (peer->on_connect) peer->on_connect(peer, peer->user_data); } else { #ifdef DEBUG_SCTP fprintf(stderr, "SCTP acception failed\n"); #endif } } return NULL; } void rtcdc_loop(struct rtcdc_peer_connection *peer) { if (peer == NULL) return; while (!peer->initialized) g_usleep(50000); GThread *thread_ice = g_thread_new("ICE thread", &ice_thread, peer); GThread *thread_sctp = g_thread_new("SCTP thread", &sctp_thread, peer); GThread *thread_startup = g_thread_new("Startup thread", &startup_thread, peer); struct ice_transport *ice = peer->transport->ice; g_main_loop_run(ice->loop); peer->exit_thread = TRUE; g_thread_join(thread_ice); g_thread_join(thread_sctp); g_thread_join(thread_startup); g_thread_unref(thread_ice); g_thread_unref(thread_sctp); g_thread_unref(thread_startup); }
static Ecore_Con_Ssl_Error _ecore_con_ssl_client_init_openssl(Ecore_Con_Client *cl) { int ret = -1; switch (cl->ssl_state) { case ECORE_CON_SSL_STATE_DONE: return ECORE_CON_SSL_ERROR_NONE; case ECORE_CON_SSL_STATE_INIT: SSL_ERROR_CHECK_GOTO_ERROR(!(cl->ssl = SSL_new(cl->host_server->ssl_ctx))); SSL_ERROR_CHECK_GOTO_ERROR(!SSL_set_fd(cl->ssl, cl->fd)); SSL_set_accept_state(cl->ssl); cl->ssl_state = ECORE_CON_SSL_STATE_HANDSHAKING; case ECORE_CON_SSL_STATE_HANDSHAKING: if (!cl->ssl) { DBG("Client was previously lost, going to error condition"); goto error; } ret = SSL_do_handshake(cl->ssl); cl->ssl_err = SSL_get_error(cl->ssl, ret); SSL_ERROR_CHECK_GOTO_ERROR((cl->ssl_err == SSL_ERROR_SYSCALL) || (cl->ssl_err == SSL_ERROR_SSL)); if (ret == 1) { cl->handshaking = EINA_FALSE; cl->ssl_state = ECORE_CON_SSL_STATE_DONE; } else { if (cl->ssl_err == SSL_ERROR_WANT_READ) ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_READ); else if (cl->ssl_err == SSL_ERROR_WANT_WRITE) ecore_main_fd_handler_active_set(cl->fd_handler, ECORE_FD_WRITE); return ECORE_CON_SSL_ERROR_NONE; } default: break; } #ifdef ISCOMFITOR { /* print session info into DBG */ SSL_SESSION *s; BIO *b; char log[4096]; memset(log, 0, sizeof(log)); s = SSL_get_session(cl->ssl); b = BIO_new(BIO_s_mem()); SSL_SESSION_print(b, s); while (BIO_read(b, log, sizeof(log)) > 0) DBG("%s", log); BIO_free(b); } #endif if (!cl->host_server->verify) /* not verifying certificates, so we're done! */ return ECORE_CON_SSL_ERROR_NONE; SSL_set_verify(cl->ssl, SSL_VERIFY_PEER, NULL); /* use CRL/CA lists to verify */ if (SSL_get_peer_certificate(cl->ssl)) SSL_ERROR_CHECK_GOTO_ERROR(SSL_get_verify_result(cl->ssl)); return ECORE_CON_SSL_ERROR_NONE; error: _openssl_print_errors(); _ecore_con_ssl_client_shutdown_openssl(cl); return ECORE_CON_SSL_ERROR_SERVER_INIT_FAILED; }
int tls_bio(ACL_SOCKET fd, int timeout, TLS_SESS_STATE *TLScontext, int (*hsfunc) (SSL *), int (*rfunc) (SSL *, void *, int), int (*wfunc) (SSL *, const void *, int), void *buf, int num) { const char *myname = "tls_bio"; int status = 0; int err; int retval = 0; int biop_retval; int done; /* * If necessary, retry the SSL handshake or read/write operation after * handling any pending network I/O. */ for (done = 0; done == 0; /* void */ ) { if (hsfunc) { #if 1 status = hsfunc(TLScontext->con); #else status = SSL_do_handshake(TLScontext->con); #endif } else if (rfunc) status = rfunc(TLScontext->con, buf, num); else if (wfunc) status = wfunc(TLScontext->con, buf, num); else acl_msg_panic("%s: nothing to do here", myname); err = SSL_get_error(TLScontext->con, status); #if (OPENSSL_VERSION_NUMBER <= 0x0090581fL) /* * There is a bug up to and including OpenSSL-0.9.5a: if an error * occurs while checking the peers certificate due to some * certificate error (e.g. as happend with a RSA-padding error), the * error is put onto the error stack. If verification is not * enforced, this error should be ignored, but the error-queue is not * cleared, so we can find this error here. The bug has been fixed on * May 28, 2000. * * This bug so far has only manifested as 4800:error:0407006A:rsa * routines:RSA_padding_check_PKCS1_type_1:block type is not * 01:rsa_pk1.c:100: 4800:error:04067072:rsa * routines:RSA_EAY_PUBLIC_DECRYPT:padding check * failed:rsa_eay.c:396: 4800:error:0D079006:asn1 encoding * routines:ASN1_verify:bad get asn1 object call:a_verify.c:109: so * that we specifically test for this error. We print the errors to * the logfile and automatically clear the error queue. Then we retry * to get another error code. We cannot do better, since we can only * retrieve the last entry of the error-queue without actually * cleaning it on the way. * * This workaround is secure, as verify_result is set to "failed" * anyway. */ if (err == SSL_ERROR_SSL) { if (ERR_peek_error() == 0x0407006AL) { tls_print_errors(); acl_msg_info("OpenSSL <= 0.9.5a workaround called: certificate errors ignored"); err = SSL_get_error(TLScontext->con, status); } } #endif /* * Find out if we must retry the operation and/or if there is pending * network I/O. * * XXX If we're the first to invoke SSL_shutdown(), then the operation * isn't really complete when the call returns. We could hide that * anomaly here and repeat the call. */ switch (err) { case SSL_ERROR_NONE: /* success */ retval = status; done = 1; /* FALLTHROUGH */ case SSL_ERROR_WANT_WRITE: /* flush/update buffers */ case SSL_ERROR_WANT_READ: biop_retval = network_biopair_interop(fd, timeout, TLScontext->network_bio); if (biop_retval < 0) return (-1); /* network read/write error */ break; /* * With tls_timed_read() and tls_timed_write() the caller is the * VSTREAM library module which is unaware of TLS, so we log the * TLS error stack here. In a better world, each VSTREAM I/O * object would provide an error reporting method in addition to * the timed_read and timed_write methods, so that we would not * need to have ad-hoc code like this. */ case SSL_ERROR_SSL: if (rfunc || wfunc) tls_print_errors(); /* FALLTHROUGH */ default: retval = status; done = 1; break; } } return (retval); }
int sslconnect(struct st_VioSSLFd *ptr, Vio *vio, long timeout) { SSL *ssl; my_bool unused; my_bool was_blocking; DBUG_ENTER("sslconnect"); DBUG_PRINT("enter", ("ptr: 0x%lx, sd: %d ctx: 0x%lx", (long) ptr, vio->sd, (long) ptr->ssl_context)); /* Set socket to blocking if not already set */ vio_blocking(vio, 1, &was_blocking); if (!(ssl= SSL_new(ptr->ssl_context))) { DBUG_PRINT("error", ("SSL_new failure")); report_errors(ssl); vio_blocking(vio, was_blocking, &unused); DBUG_RETURN(1); } DBUG_PRINT("info", ("ssl: 0x%lx timeout: %ld", (long) ssl, timeout)); SSL_clear(ssl); SSL_SESSION_set_timeout(SSL_get_session(ssl), timeout); SSL_set_fd(ssl, vio->sd); /* SSL_do_handshake will select between SSL_connect or SSL_accept depending on server or client side */ if (SSL_do_handshake(ssl) < 1) { DBUG_PRINT("error", ("SSL_do_handshake failure")); report_errors(ssl); SSL_free(ssl); vio_blocking(vio, was_blocking, &unused); DBUG_RETURN(1); } /* Connection succeeded. Install new function handlers, change type, set sd to the fd used when connecting and set pointer to the SSL structure */ vio_reset(vio, VIO_TYPE_SSL, SSL_get_fd(ssl), 0, 0); vio->ssl_arg= (void*)ssl; #ifndef DBUG_OFF { /* Print some info about the peer */ X509 *cert; char buf[512]; DBUG_PRINT("info",("SSL connection succeeded")); DBUG_PRINT("info",("Using cipher: '%s'" , SSL_get_cipher_name(ssl))); if ((cert= SSL_get_peer_certificate (ssl))) { DBUG_PRINT("info",("Peer certificate:")); X509_NAME_oneline(X509_get_subject_name(cert), buf, sizeof(buf)); DBUG_PRINT("info",("\t subject: '%s'", buf)); X509_NAME_oneline(X509_get_issuer_name(cert), buf, sizeof(buf)); DBUG_PRINT("info",("\t issuer: '%s'", buf)); X509_free(cert); } else DBUG_PRINT("info",("Peer does not have certificate.")); if (SSL_get_shared_ciphers(ssl, buf, sizeof(buf))) { DBUG_PRINT("info",("shared_ciphers: '%s'", buf)); } else DBUG_PRINT("info",("no shared ciphers!")); } #endif DBUG_RETURN(0); }