Merge pull request #364 from Barenboim/master

add SSLHandshaker
This commit is contained in:
xiehan
2021-05-13 00:56:13 +08:00
committed by GitHub
3 changed files with 106 additions and 4 deletions
+1
View File
@@ -12,6 +12,7 @@ set(SRC
HttpMessage.cc
RedisMessage.cc
HttpUtil.cc
SSLWrapper.cc
)
add_library(${PROJECT_NAME} OBJECT ${SRC})
+92 -4
View File
@@ -25,14 +25,102 @@
namespace protocol
{
int SSLHandshaker::encode(struct iovec vectors[], int max)
{
BIO *wbio = SSL_get_wbio(this->ssl);
char *ptr;
long len;
int ret;
if (BIO_reset(wbio) <= 0)
return -1;
ret = SSL_do_handshake(this->ssl);
if (ret <= 0)
{
ret = SSL_get_error(this->ssl, ret);
if (ret != SSL_ERROR_WANT_READ)
{
if (ret != SSL_ERROR_SYSCALL)
errno = -ret;
return -1;
}
}
len = BIO_get_mem_data(wbio, &ptr);
if (len > 0)
{
vectors[0].iov_base = ptr;
vectors[0].iov_len = len;
return 1;
}
else if (len == 0)
return 0;
else
return -1;
}
int SSLHandshaker::append(const void *buf, size_t *size)
{
BIO *rbio = SSL_get_rbio(this->ssl);
BIO *wbio = SSL_get_wbio(this->ssl);
char *ptr;
long len;
int ret;
if (BIO_reset(wbio) <= 0)
return -1;
ret = BIO_write(rbio, buf, *size);
if (ret <= 0)
return -1;
*size = ret;
ret = SSL_do_handshake(this->ssl);
if (ret <= 0)
{
ret = SSL_get_error(this->ssl, ret);
if (ret != SSL_ERROR_WANT_READ)
{
if (ret != SSL_ERROR_SYSCALL)
errno = -ret;
return -1;
}
ret = 0;
}
len = BIO_get_mem_data(wbio, &ptr);
if (len >= 0)
{
long n = 0;
if (len > 0)
n = this->feedback(ptr, len);
if (n == len)
return ret;
if (n >= 0)
errno = EAGAIN;
}
return -1;
}
int SSLWrapper::encode(struct iovec vectors[], int max)
{
BIO *bio = SSL_get_wbio(this->ssl);
BIO *wbio = SSL_get_wbio(this->ssl);
struct iovec *iov;
char *ptr;
long len;
int ret;
if (BIO_reset(wbio) <= 0)
return -1;
ret = this->msg->encode(vectors, max);
if ((unsigned int)ret > (unsigned int)max)
return ret;
@@ -54,7 +142,7 @@ int SSLWrapper::encode(struct iovec vectors[], int max)
}
}
len = BIO_get_mem_data(bio, &ptr);
len = BIO_get_mem_data(wbio, &ptr);
if (len > 0)
{
vectors[0].iov_base = ptr;
@@ -71,13 +159,13 @@ int SSLWrapper::encode(struct iovec vectors[], int max)
int SSLWrapper::append(const void *buf, size_t *size)
{
BIO *bio = SSL_get_rbio(this->ssl);
BIO *rbio = SSL_get_rbio(this->ssl);
char rbuf[BUFSIZE];
size_t nleft;
size_t n;
int ret;
ret = BIO_write(bio, buf, *size);
ret = BIO_write(rbio, buf, *size);
if (ret <= 0)
return -1;
+13
View File
@@ -25,6 +25,19 @@
namespace protocol
{
class SSLHandshaker : public ProtocolMessage
{
protected:
virtual int encode(struct iovec vectors[], int max);
virtual int append(const void *buf, size_t *size);
protected:
SSL *ssl;
public:
SSLHandshaker(SSL *ssl) { this->ssl = ssl; }
};
class SSLWrapper : public ProtocolMessage
{
protected: