Skip to content

Commit 9f0b90b

Browse files
committed
Update tlszmq.cpp
use new exception class for error handling
1 parent fb62b8f commit 9f0b90b

1 file changed

Lines changed: 28 additions & 29 deletions

File tree

tlszmq.cpp

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,32 +2,39 @@
22
#include <openssl/ssl.h>
33
#include <openssl/err.h>
44
#include "tlszmq.h"
5+
#include "tlsexception.h"
56

67
TLSZmq::TLSZmq(SSL_CTX *ctx)
78
{
8-
init_(ctx);
9+
init_(ctx);
910
SSL_set_connect_state(ssl);
1011
}
1112

1213
TLSZmq::TLSZmq(
13-
SSL_CTX *ctx,
14-
const char *certificate,
15-
const char *key)
14+
SSL_CTX *ctx,
15+
const char *certificate,
16+
const char *key)
1617
{
17-
// This could do with some error checking!
18-
SSL_CTX_use_certificate_file(ctx, certificate, SSL_FILETYPE_PEM);
19-
SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM);
18+
int rc = SSL_CTX_use_certificate_file(ctx, certificate, SSL_FILETYPE_PEM);
19+
if (rc != 1) {
20+
throw TLSException("failed to read credentials.");
21+
}
22+
23+
rc = SSL_CTX_use_PrivateKey_file(ctx, key, SSL_FILETYPE_PEM);
24+
if (rc != 1) {
25+
throw TLSException("failed to use private key.");
26+
}
27+
2028
init_(ctx);
2129
SSL_set_accept_state(ssl);
2230
}
2331

2432
void TLSZmq::shutdown() {
2533
int ret = SSL_shutdown(ssl);
26-
printf("SSL_shutdown returned %d\n",ret);
2734

2835
switch (ret) {
2936
case 0:
30-
SSL_shutdown(ssl);
37+
SSL_shutdown(ssl);
3138
break;
3239
case 1:
3340
default:
@@ -36,8 +43,9 @@ void TLSZmq::shutdown() {
3643
}
3744

3845
TLSZmq::~TLSZmq() {
39-
4046
SSL_free(ssl);
47+
ERR_free_strings();
48+
4149
delete ssl_to_app;
4250
delete app_to_ssl;
4351
delete zmq_to_ssl;
@@ -57,10 +65,8 @@ void TLSZmq::update()
5765
if (app_to_ssl->size() > 0) {
5866
int rc = SSL_write(ssl, app_to_ssl->data(), app_to_ssl->size());
5967

60-
if (!continue_ssl_(rc)) {
61-
throw std::runtime_error("An SSL error occured.");
62-
}
63-
68+
check_ssl_(rc);
69+
6470
if ( rc == app_to_ssl->size() ) {
6571
app_to_ssl->rebuild(0);
6672
}
@@ -118,7 +124,7 @@ SSL_CTX *TLSZmq::init_ctx(int mode) {
118124
} else if (SSL_SERVER == mode) {
119125
meth = SSLv3_server_method ();
120126
} else {
121-
throw std::runtime_error("Error: Invalid SSL mode. Valid modes are TLSZmq::SSL_CLIENT and TLSZmq::SSL_SERVER \n");
127+
throw TLSException("Error: Invalid SSL mode. Valid modes are TLSZmq::SSL_CLIENT and TLSZmq::SSL_SERVER");
122128
}
123129

124130
SSL_CTX *ctxt = SSL_CTX_new (meth);
@@ -172,9 +178,7 @@ void TLSZmq::net_read_() {
172178
char readto[1024];
173179
int read = SSL_read(ssl, readto, 1024);
174180

175-
if (!continue_ssl_(read)) {
176-
throw std::runtime_error("An SSL error occured.");
177-
}
181+
check_ssl_(read);
178182

179183
if (read > 0) {
180184
size_t cur_size = aread.length();
@@ -196,22 +200,17 @@ void TLSZmq::net_read_() {
196200
}
197201
}
198202

199-
bool TLSZmq::continue_ssl_(int rc) {
203+
void TLSZmq::check_ssl_(int rc) {
200204
int err = SSL_get_error(ssl, rc);
201205

202206
if (err == SSL_ERROR_NONE || err == SSL_ERROR_WANT_READ) {
203-
return true;
207+
return;
204208
}
205209

206-
if (err == SSL_ERROR_SYSCALL) {
207-
ERR_print_errors_fp(stderr);
208-
perror("DEBUG: syscall error: ");
209-
return false;
210+
if (err == SSL_ERROR_SYSCALL ||
211+
err == SSL_ERROR_SSL) {
212+
throw TLSException(err);
210213
}
211214

212-
if (err == SSL_ERROR_SSL) {
213-
ERR_print_errors_fp(stderr);
214-
return false;
215-
}
216-
return true;
215+
return;
217216
}

0 commit comments

Comments
 (0)