44
44
#include <cyassl/ctaocrypt/error-crypt.h>
45
45
#endif
46
46
47
- int http_get (const int sockfd , char * buf ) {
47
+ #ifdef USE_CYASSL
48
+ static CYASSL_CTX * get_cyassl_ctx (void );
49
+ #endif
50
+
51
+
52
+ int
53
+ http_get (const int sockfd , char * buf ) {
48
54
49
55
ssize_t numbytes ;
50
56
size_t totalbytes ;
@@ -119,8 +125,78 @@ int http_get(const int sockfd, char *buf) {
119
125
120
126
#ifdef USE_CYASSL
121
127
128
+ static CYASSL_CTX * cyassl_ctx = NULL ;
129
+ static pthread_mutex_t cyassl_ctx_mutex = PTHREAD_MUTEX_INITIALIZER ;
130
+
131
+ #define LOCK_CYASSL_CTX () do { \
132
+ debug(LOG_DEBUG, "Locking CyaSSL Context"); \
133
+ pthread_mutex_lock(&cyassl_ctx_mutex); \
134
+ debug(LOG_DEBUG, "CyaSSL Context locked"); \
135
+ } while (0)
136
+
137
+ #define UNLOCK_CYASSL_CTX () do { \
138
+ debug(LOG_DEBUG, "Unlocking CyaSSL Context"); \
139
+ pthread_mutex_unlock(&cyassl_ctx_mutex); \
140
+ debug(LOG_DEBUG, "CyaSSL Context unlocked"); \
141
+ } while (0)
142
+
143
+
144
+ static CYASSL_CTX *
145
+ get_cyassl_ctx (void )
146
+ {
147
+ int err ;
148
+ CYASSL_CTX * ret ;
149
+ s_config * config = config_get_config ();
150
+
151
+ LOCK_CYASSL_CTX ();
152
+
153
+ if (NULL == cyassl_ctx ) {
154
+ CyaSSL_Init ();
155
+ /* Create the CYASSL_CTX */
156
+ /* Allow TLSv1.0 up to TLSv1.2 */
157
+ if ( (cyassl_ctx = CyaSSL_CTX_new (CyaTLSv1_client_method ())) == NULL ){
158
+ debug (LOG_ERR , "Could not create CYASSL context." );
159
+ return NULL ;
160
+ }
161
+
162
+ if (config -> ssl_cipher_list ) {
163
+ debug (LOG_INFO , "Setting SSL cipher list to [%s]" , config -> ssl_cipher_list );
164
+ err = CyaSSL_CTX_set_cipher_list (cyassl_ctx , config -> ssl_cipher_list );
165
+ if (SSL_SUCCESS != err ) {
166
+ debug (LOG_ERR , "Could not load SSL cipher list (error %d)" , err );
167
+ return NULL ;
168
+ }
169
+ }
170
+
171
+ if (config -> ssl_verify ) {
172
+ /* Use trusted certs */
173
+ /* Note: CyaSSL requires that the certificates are named by their hash values */
174
+ debug (LOG_INFO , "Loading SSL certificates from %s" , config -> ssl_certs );
175
+ err = CyaSSL_CTX_load_verify_locations (cyassl_ctx , NULL , config -> ssl_certs );
176
+ if (err != SSL_SUCCESS ) {
177
+ debug (LOG_ERR , "Could not load SSL certificates (error %d)" , err );
178
+ if (err == ASN_UNKNOWN_OID_E ) {
179
+ debug (LOG_ERR , "Error is ASN_UNKNOWN_OID_E - try compiling cyassl/wolfssl with --enable-ecc" );
180
+ } else {
181
+ debug (LOG_ERR , "Make sure that SSLCertPath points to the correct path in the config file" );
182
+ debug (LOG_ERR , "Or disable certificate loading with 'SSLPeerVerification No'." );
183
+ }
184
+ return NULL ;
185
+ }
186
+ } else {
187
+ CyaSSL_CTX_set_verify (cyassl_ctx , SSL_VERIFY_NONE , 0 );
188
+ debug (LOG_INFO , "Disabling SSL certificate verification!" );
189
+ }
190
+ }
191
+
192
+ ret = cyassl_ctx ;
193
+ UNLOCK_CYASSL_CTX ();
194
+ return ret ;
195
+ }
122
196
123
- int https_get (const int sockfd , char * buf , const char * hostname ) {
197
+
198
+ int
199
+ https_get (const int sockfd , char * buf , const char * hostname ) {
124
200
125
201
ssize_t numbytes ;
126
202
size_t totalbytes ;
@@ -134,43 +210,18 @@ int https_get(const int sockfd, char *buf, const char* hostname) {
134
210
s_config * config ;
135
211
config = config_get_config ();
136
212
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
- }
213
+ CYASSL_CTX * ctx = get_cyassl_ctx ();
214
+ if (NULL == ctx ) {
215
+ debug (LOG_ERR , "Could not get CyaSSL Context!" );
216
+ return -1 ;
217
+ }
166
218
167
219
if (sockfd == -1 ) {
168
220
/* Could not connect to server */
169
221
debug (LOG_ERR , "Could not open socket to server!" );
170
222
return -1 ;
171
223
}
172
224
173
-
174
225
/* Create CYASSL object */
175
226
CYASSL * ssl ;
176
227
if ( (ssl = CyaSSL_new (ctx )) == NULL ) {
@@ -204,7 +255,7 @@ int https_get(const int sockfd, char *buf, const char* hostname) {
204
255
memset (buf , 0 , buflen );
205
256
206
257
debug (LOG_DEBUG , "Reading response" );
207
- numbytes = totalbytes = 0 ;
258
+ totalbytes = 0 ;
208
259
done = 0 ;
209
260
do {
210
261
FD_ZERO (& readfds );
@@ -257,8 +308,6 @@ int https_get(const int sockfd, char *buf, const char* hostname) {
257
308
debug (LOG_DEBUG , "HTTP Response from Server: [%s]" , buf );
258
309
259
310
CyaSSL_free (ssl );
260
- CyaSSL_CTX_free (ctx );
261
- CyaSSL_Cleanup ();
262
311
263
312
return totalbytes ;
264
313
}
0 commit comments