Skip to content

Commit d72a64a

Browse files
committed
Split out SSL init from https_get
1 parent 5cb3a36 commit d72a64a

File tree

2 files changed

+69
-33
lines changed

2 files changed

+69
-33
lines changed

Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ doc_DATA = \
99
COPYING \
1010
INSTALL \
1111
NEWS \
12-
README \
12+
README.md \
1313
ChangeLog
1414

1515
EXTRA_DIST = \

src/simple_http.c

Lines changed: 68 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
#include <cyassl/ctaocrypt/error-crypt.h>
4545
#endif
4646

47-
int http_get(const int sockfd, char *buf) {
47+
int
48+
http_get(const int sockfd, char *buf) {
4849

4950
ssize_t numbytes;
5051
size_t totalbytes;
@@ -119,8 +120,68 @@ int http_get(const int sockfd, char *buf) {
119120

120121
#ifdef USE_CYASSL
121122

123+
CYASSL_CTX *cyassl_ctx = NULL;
124+
pthread_mutex_t cyassl_ctx_mutex = PTHREAD_MUTEX_INITIALIZER;
125+
126+
#define LOCK_CYASSL_CTX() do { \
127+
debug(LOG_DEBUG, "Locking CyaSSL Context"); \
128+
pthread_mutex_lock(&cyassl_ctx_mutex); \
129+
debug(LOG_DEBUG, "CyaSSL Context locked"); \
130+
} while (0)
131+
132+
#define UNLOCK_CYASSL_CTX() do { \
133+
debug(LOG_DEBUG, "Unlocking CyaSSL Context"); \
134+
pthread_mutex_unlock(&cyassl_ctx_mutex); \
135+
debug(LOG_DEBUG, "CyaSSL Context unlocked"); \
136+
} while (0)
137+
138+
139+
CYASSL_CTX *
140+
get_cyassl_ctx(void)
141+
{
142+
CYASSL_CTX *ret;
143+
s_config *config = config_get_config();
144+
145+
LOCK_CYASSL_CTX();
146+
147+
if (NULL == cyassl_ctx) {
148+
CyaSSL_Init();
149+
/* Create the CYASSL_CTX */
150+
/* Allow SSLv3 up to TLSv1.2 */
151+
if ( (cyassl_ctx = CyaSSL_CTX_new(CyaSSLv23_client_method())) == NULL){
152+
debug(LOG_ERR, "Could not create CYASSL context.");
153+
return NULL;
154+
}
155+
156+
if (config->ssl_verify) {
157+
/* Use trusted certs */
158+
/* Note: CyaSSL requires that the certificates are named by their hash values */
159+
int err = CyaSSL_CTX_load_verify_locations(cyassl_ctx, NULL, config->ssl_certs);
160+
if (err != SSL_SUCCESS) {
161+
debug(LOG_ERR, "Could not load SSL certificates (error %d)", err);
162+
if (err == ASN_UNKNOWN_OID_E) {
163+
debug(LOG_ERR, "Error is ASN_UNKNOWN_OID_E - try compiling cyassl/wolfssl with --enable-ecc");
164+
} else {
165+
debug(LOG_ERR, "Make sure that SSLCertPath points to the correct path in the config file");
166+
debug(LOG_ERR, "Or disable certificate loading with 'SSLPeerVerification No'.");
167+
}
168+
return NULL;;
169+
}
170+
debug(LOG_INFO, "Loading SSL certificates from %s", config->ssl_certs);
171+
} else {
172+
CyaSSL_CTX_set_verify(cyassl_ctx, SSL_VERIFY_NONE, 0);
173+
debug(LOG_INFO, "Disabling SSL certificate verification!");
174+
}
175+
}
176+
177+
ret = cyassl_ctx;
178+
UNLOCK_CYASSL_CTX();
179+
return ret;
180+
}
181+
122182

123-
int https_get(const int sockfd, char *buf, const char* hostname) {
183+
int
184+
https_get(const int sockfd, char *buf, const char* hostname) {
124185

125186
ssize_t numbytes;
126187
size_t totalbytes;
@@ -134,43 +195,18 @@ int https_get(const int sockfd, char *buf, const char* hostname) {
134195
s_config *config;
135196
config = config_get_config();
136197

137-
CyaSSL_Init();
138-
139-
CYASSL_CTX* ctx;
140-
/* Create the CYASSL_CTX */
141-
/* Allow SSLv3 up to TLSv1.2 */
142-
if ( (ctx = CyaSSL_CTX_new(CyaSSLv23_client_method())) == NULL){
143-
debug(LOG_ERR, "Could not create CYASSL context.");
144-
return -1;
145-
}
146-
147-
if (config->ssl_verify) {
148-
/* Use trusted certs */
149-
/* Note: CyaSSL requires that the certificates are named by their hash values */
150-
int err = CyaSSL_CTX_load_verify_locations(ctx, NULL, config->ssl_certs);
151-
if (err != SSL_SUCCESS) {
152-
debug(LOG_ERR, "Could not load SSL certificates (error %d)", err);
153-
if (err == ASN_UNKNOWN_OID_E) {
154-
debug(LOG_ERR, "Error is ASN_UNKNOWN_OID_E - try compiling cyassl/wolfssl with --enable-ecc");
155-
} else {
156-
debug(LOG_ERR, "Make sure that SSLCertPath points to the correct path in the config file");
157-
debug(LOG_ERR, "Or disable certificate loading with 'SSLPeerVerification No'.");
158-
}
159-
return -1;
160-
}
161-
debug(LOG_INFO, "Loading SSL certificates from %s", config->ssl_certs);
162-
} else {
163-
CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
164-
debug(LOG_INFO, "Disabling SSL certificate verification!");
165-
}
198+
CYASSL_CTX* ctx = get_cyassl_ctx();
199+
if (NULL == ctx) {
200+
debug(LOG_ERR, "Could not get CyaSSL Context!");
201+
return -1;
202+
}
166203

167204
if (sockfd == -1) {
168205
/* Could not connect to server */
169206
debug(LOG_ERR, "Could not open socket to server!");
170207
return -1;
171208
}
172209

173-
174210
/* Create CYASSL object */
175211
CYASSL* ssl;
176212
if( (ssl = CyaSSL_new(ctx)) == NULL) {

0 commit comments

Comments
 (0)