44
44
#include <cyassl/ctaocrypt/error-crypt.h>
45
45
#endif
46
46
47
- int http_get (const int sockfd , char * buf ) {
47
+ int
48
+ http_get (const int sockfd , char * buf ) {
48
49
49
50
ssize_t numbytes ;
50
51
size_t totalbytes ;
@@ -119,8 +120,68 @@ int http_get(const int sockfd, char *buf) {
119
120
120
121
#ifdef USE_CYASSL
121
122
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
+
122
182
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 ) {
124
185
125
186
ssize_t numbytes ;
126
187
size_t totalbytes ;
@@ -134,43 +195,18 @@ int https_get(const int sockfd, char *buf, const char* hostname) {
134
195
s_config * config ;
135
196
config = config_get_config ();
136
197
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
+ }
166
203
167
204
if (sockfd == -1 ) {
168
205
/* Could not connect to server */
169
206
debug (LOG_ERR , "Could not open socket to server!" );
170
207
return -1 ;
171
208
}
172
209
173
-
174
210
/* Create CYASSL object */
175
211
CYASSL * ssl ;
176
212
if ( (ssl = CyaSSL_new (ctx )) == NULL ) {
0 commit comments