Skip to content

Commit c356bbf

Browse files
committed
* NEW [openssl] Connect openssl APIs to NNG APIs.
Signed-off-by: wanghaemq <[email protected]>
1 parent 762fc24 commit c356bbf

File tree

1 file changed

+77
-0
lines changed

1 file changed

+77
-0
lines changed

src/supplemental/tls/openssl/openssl.c

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,90 @@ open_conn_fini(nng_tls_engine_conn *ec)
3737
SSL_free(ec->ssl);
3838
}
3939

40+
static int
41+
open_net_read(BIO *b, char *buf, int len) {
42+
void *ctx = BIO_get_data(b);
43+
44+
size_t sz = len;
45+
int rv;
46+
47+
rv = nng_tls_engine_recv(ctx, (uint8_t *) buf, &sz);
48+
switch (rv) {
49+
case 0:
50+
return ((int) sz);
51+
case NNG_EAGAIN:
52+
return (SSL_ERROR_WANT_READ);
53+
// return (WOLFSSL_CBIO_ERR_WANT_READ);
54+
case NNG_ECLOSED:
55+
return (SSL_ERROR_WANT_CONNECT);
56+
// return (WOLFSSL_CBIO_ERR_CONN_CLOSE);
57+
case NNG_ECONNSHUT:
58+
return (SSL_ERROR_WANT_CONNECT);
59+
// return (WOLFSSL_CBIO_ERR_CONN_RST);
60+
default:
61+
return (SSL_ERROR_WANT_CONNECT);
62+
// return (WOLFSSL_CBIO_ERR_GENERAL);
63+
}
64+
}
65+
66+
static int
67+
open_net_write(BIO *b, const char *buf, int len) {
68+
void *ctx = BIO_get_data(b);
69+
size_t sz = len;
70+
int rv;
71+
72+
rv = nng_tls_engine_send(ctx, (const uint8_t *) buf, &sz);
73+
switch (rv) {
74+
case 0:
75+
return ((int) sz);
76+
77+
case NNG_EAGAIN:
78+
return (SSL_ERROR_WANT_WRITE);
79+
// return (WOLFSSL_CBIO_ERR_WANT_WRITE);
80+
case NNG_ECLOSED:
81+
return (SSL_ERROR_WANT_CONNECT);
82+
// return (WOLFSSL_CBIO_ERR_CONN_CLOSE);
83+
case NNG_ECONNSHUT:
84+
return (SSL_ERROR_WANT_CONNECT);
85+
// return (WOLFSSL_CBIO_ERR_CONN_RST);
86+
default:
87+
return (SSL_ERROR_WANT_CONNECT);
88+
// return (WOLFSSL_CBIO_ERR_GENERAL);
89+
}
90+
}
91+
92+
static int
93+
rwbio_create(BIO *b) {
94+
BIO_set_init(b, 1);
95+
return 1;
96+
}
97+
98+
static int
99+
rwbio_destroy(BIO *b) {
100+
return b == NULL ? 0 : 1;
101+
}
102+
103+
BIO_METHOD *rwbio_method() {
104+
BIO_METHOD *m = BIO_meth_new(BIO_TYPE_SOURCE_SINK, "OpenSSLrwbio");
105+
BIO_meth_set_write(m, open_net_write);
106+
BIO_meth_set_read(m, open_net_read);
107+
BIO_meth_set_create(m, rwbio_create);
108+
BIO_meth_set_destroy(m, rwbio_destroy);
109+
return m;
110+
}
111+
40112
static int
41113
open_conn_init(nng_tls_engine_conn *ec, void *tls, nng_tls_engine_config *cfg)
42114
{
43115
ec->tls = tls;
44116
if ((ec->ssl = SSL_new(cfg->ctx)) == NULL) {
45117
return (NNG_ENOMEM); // most likely
46118
}
119+
120+
BIO *rwbio = BIO_new(rwbio_method());
121+
BIO_set_data(rwbio, ec->tls);
122+
SSL_set_bio(ec->ssl, rwbio, rwbio);
123+
47124
if (cfg->server_name != NULL) {
48125
/*
49126
if (wolfSSL_check_domain_name(ec->ssl, cfg->server_name) !=

0 commit comments

Comments
 (0)