Пример #1
0
void
rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout)
{
	if(new_F->sctx == NULL)
		return;

	new_F->type |= RB_FD_SSL;
	new_F->ssl = SSL_new(new_F->sctx->ssl_ctx);
	if(new_F->ssl == NULL)
	{
		new_F->sslerr.ssl_errno = get_last_err();
		rb_lib_log("rb_ssl_start_accepted: SSL_new() fails: %s", ERR_error_string(new_F->sslerr.ssl_errno, NULL));
		cb(new_F, RB_ERROR_SSL, NULL, 0, data); 
		return;
	}

	new_F->accept = rb_malloc(sizeof(struct acceptdata));

	new_F->accept->callback = cb;
	new_F->accept->data = data;
	rb_settimeout(new_F, timeout, rb_ssl_timeout, NULL);

	new_F->accept->addrlen = 0;
	SSL_set_fd((SSL *) new_F->ssl, rb_get_fd(new_F));
	rb_setup_ssl_cb(new_F);
	rb_ssl_accept_common(new_F);
}
Пример #2
0
static void
rb_ssl_tryaccept(rb_fde_t *F, void *data)
{
	int ret;
	struct acceptdata *ad;

	lrb_assert(F->accept != NULL);

	ret = do_ssl_handshake(F, rb_ssl_tryaccept, NULL);

	/* do_ssl_handshake does the rb_setselect */
	if(ret == 0)
		return;

	ad = F->accept;
	F->accept = NULL;
	rb_settimeout(F, 0, NULL, NULL);
	rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL);

	if(ret > 0)
		ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
	else
		ad->callback(F, RB_ERROR_SSL, NULL, 0, ad->data);

	rb_free(ad);
}
Пример #3
0
void
rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, rb_socklen_t addrlen)
{
	if(new_F->sctx == NULL)
		return;

	new_F->type |= RB_FD_SSL;
	new_F->ssl = SSL_new(new_F->sctx->ssl_ctx);

	if(new_F->ssl == NULL)
	{
		new_F->sslerr.ssl_errno = get_last_err();
		rb_lib_log("rb_ssl_accept_setup: SSL_new() fails: %s", ERR_error_string(new_F->sslerr.ssl_errno, NULL));
		new_F->accept->callback(new_F, RB_ERROR_SSL, NULL, 0, new_F->accept->data);
		return;
	}
	
	new_F->accept = rb_malloc(sizeof(struct acceptdata));

	new_F->accept->callback = F->accept->callback;
	new_F->accept->data = F->accept->data;
	rb_settimeout(new_F, 10, rb_ssl_timeout, NULL);
	memcpy(&new_F->accept->S, st, addrlen);
	new_F->accept->addrlen = addrlen;

	SSL_set_fd((SSL *) new_F->ssl, rb_get_fd(new_F));
	rb_setup_ssl_cb(new_F);
	rb_ssl_accept_common(new_F);
}
Пример #4
0
void
rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout)
{
    struct ssl_connect *sconn;
    if (F == NULL)
        return;

    sconn = rb_malloc(sizeof(struct ssl_connect));
    sconn->data = data;
    sconn->callback = callback;
    sconn->timeout = timeout;
    F->connect = rb_malloc(sizeof(struct conndata));
    F->connect->callback = callback;
    F->connect->data = data;
    F->type |= RB_FD_SSL;
    F->ssl = rb_malloc(sizeof(gnutls_session_t));

    gnutls_init(F->ssl, GNUTLS_CLIENT);
    gnutls_set_default_priority(SSL_P(F));
    gnutls_dh_set_prime_bits(SSL_P(F), 1024);
    gnutls_transport_set_ptr(SSL_P(F), (gnutls_transport_ptr_t) (long int)F->fd);

    rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);

    if (do_ssl_handshake(F, rb_ssl_tryconn_cb))
    {
        rb_ssl_connect_realcb(F, RB_OK, sconn);
    }
}
Пример #5
0
static void
rb_ssl_tryconn(rb_fde_t *F, int status, void *data)
{
    struct ssl_connect *sconn = data;
    if (status != RB_OK)
    {
        rb_ssl_connect_realcb(F, status, sconn);
        return;
    }

    F->type |= RB_FD_SSL;


    rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
    F->ssl = rb_malloc(sizeof(gnutls_session_t));
    gnutls_init(F->ssl, GNUTLS_CLIENT);
    gnutls_set_default_priority(SSL_P(F));
    gnutls_dh_set_prime_bits(SSL_P(F), 1024);
    gnutls_transport_set_ptr(SSL_P(F), (gnutls_transport_ptr_t) (long int)F->fd);

    if (do_ssl_handshake(F, rb_ssl_tryconn_cb))
    {
        rb_ssl_connect_realcb(F, RB_OK, sconn);
    }
}
Пример #6
0
void
rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, int addrlen)
{
    new_F->type |= RB_FD_SSL;
    new_F->ssl = rb_malloc(sizeof(gnutls_session_t));
    new_F->accept = rb_malloc(sizeof(struct acceptdata));

    new_F->accept->callback = F->accept->callback;
    new_F->accept->data = F->accept->data;
    rb_settimeout(new_F, 10, rb_ssl_timeout, NULL);
    memcpy(&new_F->accept->S, st, addrlen);
    new_F->accept->addrlen = addrlen;

    gnutls_init((gnutls_session_t *) new_F->ssl, GNUTLS_SERVER);
    gnutls_set_default_priority(SSL_P(new_F));
    gnutls_credentials_set(SSL_P(new_F), GNUTLS_CRD_CERTIFICATE, x509);
    gnutls_dh_set_prime_bits(SSL_P(new_F), 1024);
    gnutls_transport_set_ptr(SSL_P(new_F), (gnutls_transport_ptr_t) (long int)rb_get_fd(new_F));
    if (do_ssl_handshake(F, rb_ssl_tryaccept))
    {
        struct acceptdata *ad = F->accept;
        F->accept = NULL;
        ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
        rb_free(ad);
    }
}
Пример #7
0
void
rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout)
{
    gnutls_session_t *ssl;
    new_F->type |= RB_FD_SSL;
    ssl = new_F->ssl = rb_malloc(sizeof(gnutls_session_t));
    new_F->accept = rb_malloc(sizeof(struct acceptdata));

    new_F->accept->callback = cb;
    new_F->accept->data = data;
    rb_settimeout(new_F, timeout, rb_ssl_timeout, NULL);

    new_F->accept->addrlen = 0;

    gnutls_init(ssl, GNUTLS_SERVER);
    gnutls_set_default_priority(*ssl);
    gnutls_credentials_set(*ssl, GNUTLS_CRD_CERTIFICATE, x509);
    gnutls_dh_set_prime_bits(*ssl, 1024);
    gnutls_transport_set_ptr(*ssl, (gnutls_transport_ptr_t) (long int)new_F->fd);
    if (do_ssl_handshake(new_F, rb_ssl_tryaccept))
    {
        struct acceptdata *ad = new_F->accept;
        new_F->accept = NULL;
        ad->callback(new_F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
        rb_free(ad);
    }

}
Пример #8
0
static void
rb_ssl_tryconn(rb_fde_t *F, int status, void *data)
{
    struct ssl_connect *sconn = data;
    int ssl_err;
    if(status != RB_OK) {
        rb_ssl_connect_realcb(F, status, sconn);
        return;
    }

    F->type |= RB_FD_SSL;
    F->ssl = SSL_new(ssl_client_ctx);
    SSL_set_fd((SSL *) F->ssl, F->fd);
    rb_setup_ssl_cb(F);
    rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
    if((ssl_err = SSL_connect((SSL *) F->ssl)) <= 0) {
        switch (ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err)) {
        case SSL_ERROR_SYSCALL:
            if(rb_ignore_errno(errno))
            case SSL_ERROR_WANT_READ:
        case SSL_ERROR_WANT_WRITE: {
            F->ssl_errno = get_last_err();
            rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE,
                         rb_ssl_tryconn_cb, sconn);
            return;
        }
        default:
            F->ssl_errno = get_last_err();
            rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn);
            return;
        }
    } else {
        rb_ssl_connect_realcb(F, RB_OK, sconn);
    }
}
Пример #9
0
void
rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout)
{
	ssl_connect_t *sconn;
	int ssl_err;
	if(F == NULL)
		return;

	sconn = rb_malloc(sizeof(ssl_connect_t));
	sconn->data = data;
	sconn->callback = callback;
	sconn->timeout = timeout;
	F->connect = rb_malloc(sizeof(struct conndata));
	F->connect->callback = callback;
	F->connect->data = data;
	F->type |= RB_FD_SSL;
	F->ssl = SSL_new(F->sctx->ssl_ctx);

        if(F->ssl == NULL)
        {
                F->sslerr.ssl_errno = get_last_err();
                rb_lib_log("rb_ssl_start_Connected: SSL_new() fails: %s", ERR_error_string(F->sslerr.ssl_errno, NULL));
                
                rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn);
                return;
        }

	SSL_set_fd((SSL *) F->ssl, F->fd);
	rb_setup_ssl_cb(F);
	rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
	if((ssl_err = SSL_connect((SSL *) F->ssl)) <= 0)
	{
		switch (ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err))
		{
		case SSL_ERROR_SYSCALL:
			if(rb_ignore_errno(errno))
		case SSL_ERROR_WANT_READ:
		case SSL_ERROR_WANT_WRITE:
				{
					F->sslerr.ssl_errno = get_last_err();
					rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE,
						     rb_ssl_tryconn_cb, sconn);
					return;
				}
		default:
			F->sslerr.ssl_errno = get_last_err();
			rb_ssl_connect_realcb(F, RB_ERROR_SSL, sconn);
			return;
		}
	}
	else
	{
		rb_ssl_connect_realcb(F, RB_OK, sconn);
	}
}
Пример #10
0
void
rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout)
{
	new_F->type |= RB_FD_SSL;
	new_F->ssl = SSL_new(ssl_server_ctx);
	new_F->accept = rb_malloc(sizeof(struct acceptdata));

	new_F->accept->callback = cb;
	new_F->accept->data = data;
	rb_settimeout(new_F, timeout, rb_ssl_timeout, NULL);

	new_F->accept->addrlen = 0;
	SSL_set_fd((SSL *) new_F->ssl, rb_get_fd(new_F));
	rb_setup_ssl_cb(new_F);
	rb_ssl_accept_common(new_F);
}
Пример #11
0
void
rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, int addrlen)
{
	new_F->type |= RB_FD_SSL;
	new_F->ssl = SSL_new(ssl_server_ctx);
	new_F->accept = rb_malloc(sizeof(struct acceptdata));

	new_F->accept->callback = F->accept->callback;
	new_F->accept->data = F->accept->data;
	rb_settimeout(new_F, 10, rb_ssl_timeout, NULL);
	memcpy(&new_F->accept->S, st, addrlen);
	new_F->accept->addrlen = addrlen;

	SSL_set_fd((SSL *) new_F->ssl, rb_get_fd(new_F));
	rb_setup_ssl_cb(new_F);
	rb_ssl_accept_common(new_F);
}
Пример #12
0
static void
rb_ssl_tryaccept(rb_fde_t *F, void *data)
{
	int ssl_err;
	lrb_assert(F->accept != NULL);
	int flags;
	struct acceptdata *ad;

	if(!SSL_is_init_finished((SSL *) F->ssl))
	{
		if((ssl_err = SSL_accept((SSL *) F->ssl)) <= 0)
		{
			switch (ssl_err = SSL_get_error((SSL *) F->ssl, ssl_err))
			{
			case SSL_ERROR_WANT_READ:
			case SSL_ERROR_WANT_WRITE:
				if(ssl_err == SSL_ERROR_WANT_WRITE)
					flags = RB_SELECT_WRITE;
				else
					flags = RB_SELECT_READ;
				F->ssl_errno = get_last_err();
				rb_setselect(F, flags, rb_ssl_tryaccept, NULL);
				break;
			case SSL_ERROR_SYSCALL:
				F->accept->callback(F, RB_ERROR, NULL, 0, F->accept->data);
				break;
			default:
				F->ssl_errno = get_last_err();
				F->accept->callback(F, RB_ERROR_SSL, NULL, 0, F->accept->data);
				break;
			}
			return;
		}
	}
	rb_settimeout(F, 0, NULL, NULL);
	rb_setselect(F, RB_SELECT_READ | RB_SELECT_WRITE, NULL, NULL);

	ad = F->accept;
	F->accept = NULL;
	ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
	rb_free(ad);

}
Пример #13
0
static void
rb_ssl_tryconn(rb_fde_t *F, int status, void *data)
{
	struct ssl_connect *sconn = data;
	if(status != RB_OK)
	{
		rb_ssl_connect_realcb(F, status, sconn);
		return;
	}

	F->type |= RB_FD_SSL;


	rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);
	F->ssl = rb_malloc(sizeof(mbedtls_ssl_context));
	rb_ssl_setup_client_context(F);

	do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn);
}
Пример #14
0
void
rb_ssl_start_connected(rb_fde_t *F, CNCB * callback, void *data, int timeout)
{
	struct ssl_connect *sconn;
	if(F == NULL)
		return;

	sconn = rb_malloc(sizeof(struct ssl_connect));
	sconn->data = data;
	sconn->callback = callback;
	sconn->timeout = timeout;
	F->connect = rb_malloc(sizeof(struct conndata));
	F->connect->callback = callback;
	F->connect->data = data;
	F->type |= RB_FD_SSL;
	F->ssl = rb_malloc(sizeof(mbedtls_ssl_context));

	rb_ssl_setup_client_context(F);
	rb_settimeout(F, sconn->timeout, rb_ssl_tryconn_timeout_cb, sconn);

	do_ssl_handshake(F, rb_ssl_tryconn_cb, (void *)sconn);
}
Пример #15
0
void
rb_ssl_accept_setup(rb_fde_t *F, rb_fde_t *new_F, struct sockaddr *st, rb_socklen_t addrlen)
{
	new_F->type |= RB_FD_SSL;
	new_F->ssl = rb_malloc(sizeof(mbedtls_ssl_context));
	new_F->accept = rb_malloc(sizeof(struct acceptdata));

	new_F->accept->callback = F->accept->callback;
	new_F->accept->data = F->accept->data;
	rb_settimeout(new_F, 10, rb_ssl_timeout, NULL);
	memcpy(&new_F->accept->S, st, addrlen);
	new_F->accept->addrlen = addrlen;

	rb_ssl_setup_srv_context(new_F);
	if(do_ssl_handshake(F, rb_ssl_tryaccept, NULL))
	{
		struct acceptdata *ad = F->accept;
		F->accept = NULL;

		ad->callback(F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
		rb_free(ad);
	}
}
Пример #16
0
void
rb_ssl_start_accepted(rb_fde_t *new_F, ACCB * cb, void *data, int timeout)
{
	mbedtls_ssl_context *ssl;
	new_F->type |= RB_FD_SSL;
	ssl = new_F->ssl = rb_malloc(sizeof(mbedtls_ssl_context));
	new_F->accept = rb_malloc(sizeof(struct acceptdata));

	new_F->accept->callback = cb;
	new_F->accept->data = data;
	rb_settimeout(new_F, timeout, rb_ssl_timeout, NULL);

	new_F->accept->addrlen = 0;

	rb_ssl_setup_srv_context(new_F);
	if(do_ssl_handshake(new_F, rb_ssl_tryaccept, NULL))
	{
		struct acceptdata *ad = new_F->accept;
		new_F->accept = NULL;

		ad->callback(new_F, RB_OK, (struct sockaddr *)&ad->S, ad->addrlen, ad->data);
		rb_free(ad);
	}
}