Add own test cert and key since there seem to be a problem with the ones in 0.14.2
[umurmur.git] / src / ssl.c
1 /* Copyright (C) 2009-2010, Martin Johansson <martin@fatbob.nu>
2    Copyright (C) 2005-2010, Thorvald Natvig <thorvald@natvig.com>
3
4    All rights reserved.
5
6    Redistribution and use in source and binary forms, with or without
7    modification, are permitted provided that the following conditions
8    are met:
9
10    - Redistributions of source code must retain the above copyright notice,
11      this list of conditions and the following disclaimer.
12    - Redistributions in binary form must reproduce the above copyright notice,
13      this list of conditions and the following disclaimer in the documentation
14      and/or other materials provided with the distribution.
15    - Neither the name of the Developers nor the names of its contributors may
16      be used to endorse or promote products derived from this software without
17      specific prior written permission.
18
19    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
23    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
26    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
27    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
28    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <string.h>
32 #include <stdlib.h>
33
34 #include "conf.h"
35 #include "log.h"
36 #include "ssl.h"
37
38 #ifdef USE_POLARSSL
39 /*
40  * PolarSSL interface
41  */
42
43 #include <polarssl/havege.h>
44 #include <polarssl/certs.h>
45 #include <polarssl/x509.h>
46 #include <polarssl/ssl.h>
47 #include <polarssl/net.h>
48
49 #define CA_CRT_FILENAME "ca.crt"
50
51 int ciphers[] =
52 {
53     SSL_EDH_RSA_AES_256_SHA,
54     SSL_EDH_RSA_CAMELLIA_256_SHA,
55     SSL_EDH_RSA_DES_168_SHA,
56     SSL_RSA_AES_256_SHA,
57     SSL_RSA_CAMELLIA_256_SHA,
58     SSL_RSA_AES_128_SHA,
59     SSL_RSA_CAMELLIA_128_SHA,
60     SSL_RSA_DES_168_SHA,
61     SSL_RSA_RC4_128_SHA,
62     SSL_RSA_RC4_128_MD5,
63     0
64 };
65 static x509_cert certificate;
66 static rsa_context key;
67 bool_t builtInTestCertificate;
68
69 havege_state hs; /* exported to crypt.c */
70
71 /* DH prime */
72 const char *my_dhm_P =
73         "9CE85640903BF123906947FEDE767261" \
74         "D9B4A973EB8F7D984A8C656E2BCC161C" \
75         "183D4CA471BA78225F940F16D1D99CA3" \
76         "E66152CC68EDCE1311A390F307741835" \
77         "44FF6AB553EC7073AD0CB608F2A3B480" \
78         "19E6C02BCED40BD30E91BB2469089670" \
79         "DEF409C08E8AC24D1732A6128D2220DC53";
80 const char *my_dhm_G = "4";
81
82 const char *test_key =
83         "-----BEGIN RSA PRIVATE KEY-----\r\n"
84         "MIICXAIBAAKBgQC/zZ5p/st9JUgoFIuNCwyYJhKhnqKlAk36RJJAXhJ/3FZCThQK\r\n"
85         "J0jxMCjnQb9envZPIKrAfyDtlEGKU8OI2HvDsFL7Y2fNjew0R9DoUyzPRVBInfW9\r\n"
86         "JP03aiOldpooTjXBrzoZV+DGqRYJE/IRJEyXir5NEJu624bMJm0XDVkxiwIDAQAB\r\n"
87         "AoGAPcFJiR01jYSsd+Mtj2sIUhCoHQuDmJdmXfcoE7t2P17FEzIqd55weN+lu9fK\r\n"
88         "cv/BlHaTI8mK45PBinUv1ubE6gzzyLtNgBW7ko8i23YQNMS4+8ApZZoCFsVbN533\r\n"
89         "/mFYcOmWpdgIsAOyi3gAyO5OyGA71a3gkNX+MDYc7PgbTkkCQQDq1NN/j6xVw4qc\r\n"
90         "3qunsMOVJxln7Gkgt0fOrCV0WltJNkKYiARwtpvB/SHIj6nHhun+J/ee/v+QjAL4\r\n"
91         "aUwC8OmnAkEA0RfWWVQ2Yxk9QjRcqcpDddtc+9xxM6Dt+edGDSYKRUB/S2cl/Sdg\r\n"
92         "abFdrFEnFdO03VknCrqUgweegeUJhU1tfQJBAKLFFjxK59cijMejCDRZr5eI3HFO\r\n"
93         "Sqgkoh8871Ew+ClM9OgpD3rY+CmEPZB5E+N7PmGublLEyXv1sHFi+w7m0e0CQDnH\r\n"
94         "eoYIzVapHNJ0ob6Rk/63dYRrsCRyLhDGpgbwIhps7kAp6sd/4BaU2qvJaSGQ9QPN\r\n"
95         "pQpD8NIcguKmJfFeKgkCQBABatS74xI2UAW/IIbRBg8a8z4v5JxEnQwp0EtmON2I\r\n"
96         "+AVBenUVFjpdjaaQ2/2IEgEwnzRfbERQfUlAkjczRaM=\r\n"
97         "-----END RSA PRIVATE KEY-----\r\n";
98
99 const char *test_cert =
100         "-----BEGIN CERTIFICATE-----\r\n"
101         "MIICfDCCAeWgAwIBAgIJANEN9Jb9sp0oMA0GCSqGSIb3DQEBBQUAMFcxCzAJBgNV\r\n"
102         "BAYTAlNFMRMwEQYDVQQHDApHb3RoZW5idXJnMRAwDgYDVQQKDAd1TXVybXVyMSEw\r\n"
103         "HwYDVQQDDBh1TXVybXVyIHRlc3QgY2VydGlmaWNhdGUwHhcNMTEwMzIyMjIxMTEy\r\n"
104         "WhcNMjEwMzE5MjIxMTEyWjBXMQswCQYDVQQGEwJTRTETMBEGA1UEBwwKR290aGVu\r\n"
105         "YnVyZzEQMA4GA1UECgwHdU11cm11cjEhMB8GA1UEAwwYdU11cm11ciB0ZXN0IGNl\r\n"
106         "cnRpZmljYXRlMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQC/zZ5p/st9JUgo\r\n"
107         "FIuNCwyYJhKhnqKlAk36RJJAXhJ/3FZCThQKJ0jxMCjnQb9envZPIKrAfyDtlEGK\r\n"
108         "U8OI2HvDsFL7Y2fNjew0R9DoUyzPRVBInfW9JP03aiOldpooTjXBrzoZV+DGqRYJ\r\n"
109         "E/IRJEyXir5NEJu624bMJm0XDVkxiwIDAQABo1AwTjAdBgNVHQ4EFgQUyI0/J6gS\r\n"
110         "G7vEZl1nKq9rAYAg/TMwHwYDVR0jBBgwFoAUyI0/J6gSG7vEZl1nKq9rAYAg/TMw\r\n"
111         "DAYDVR0TBAUwAwEB/zANBgkqhkiG9w0BAQUFAAOBgQCGL3T4a7g+MUtcjIq2XLON\r\n"
112         "cN/oPgPpMNcSPj7zQF1NVzjeQagvKIe3mL5sNIYuoIQm1Xm7aH2ueruhH0wkSOf0\r\n"
113         "+7U/g7r70U1CFWzfgwzz3EWRq3hUQmZ9/Xy9V2P/iRrFNyAKE8MneuVv3aAIN6/W\r\n"
114         "rWVxhCquqhFM3yIGe0f6hw==\r\n"
115         "-----END CERTIFICATE-----\r\n";
116
117
118 static void initTestCert()
119 {
120         int rc;
121         builtInTestCertificate = true;
122         rc = x509parse_crt(&certificate, (unsigned char *)test_cert,
123                                            strlen(test_cert));  
124         if (rc != 0)
125                 Log_fatal("Could not parse built-in test certificate");
126         rc = x509parse_crt(&certificate, (unsigned char *)test_cert,
127                                            strlen(test_cert));  
128         if (rc != 0)
129                 Log_fatal("Could not parse built-in test certificate");
130 }
131
132 static void initTestKey()
133 {
134         int rc;
135         
136         rc = x509parse_key(&key, (unsigned char *)test_key,
137                                            strlen(test_key), NULL, 0);
138         if (rc != 0)
139                 Log_fatal("Could not parse built-in test RSA key");
140 }
141
142 /*
143  * openssl genrsa 1024 > host.key
144  * openssl req -new -x509 -nodes -sha1 -days 365 -key host.key > host.cert
145  */
146 static void initCert()
147 {
148         int rc;
149         char *crtfile = (char *)getStrConf(CERTIFICATE);
150         char *ca_file, *p;
151         
152         if (crtfile == NULL) {
153                 Log_warn("No certificate file specified");
154                 initTestCert();
155                 return;
156         }
157         rc = x509parse_crtfile(&certificate, crtfile);
158         if (rc != 0) {
159                 Log_warn("Could not read certificate file %s", crtfile);
160                 initTestCert();
161                 return;
162         }
163         
164         /* Look for CA certificate file in same dir */
165         ca_file = malloc(strlen(crtfile) + strlen(CA_CRT_FILENAME) + 1);
166         strcpy(ca_file, crtfile);
167         p = strrchr(ca_file, '/');
168         if (p != NULL)
169                 strcpy(p + 1, CA_CRT_FILENAME);
170         else
171                 strcpy(ca_file, CA_CRT_FILENAME);
172         
173         rc = x509parse_crtfile(&certificate, ca_file);
174         if (rc != 0) { /* No CA certifiacte found. Assume self-signed. */
175                 Log_info("CA certificate file %s not found. Assuming self-signed certificate.", ca_file);
176         }
177         
178         /*
179          * PolarSSL 0.11 - 0.12,1 has a bug; it ignores the last certificate in the chain.
180          * Read the certificate again so that it gets last in chain. Later releases like 0.14.0 works
181          * fine with the extra certificate, so I don't see any harm in doing so.
182          */
183         rc = x509parse_crtfile(&certificate, crtfile);
184         if (rc != 0)
185                 Log_fatal("Could not read certificate file %s", crtfile);
186         
187         free(ca_file);
188 }
189
190 static void initKey()
191 {
192         int rc;
193         char *keyfile = (char *)getStrConf(KEY);
194
195         if (keyfile == NULL)
196                 Log_fatal("No key file specified"); 
197         rc = x509parse_keyfile(&key, keyfile, NULL);
198         if (rc != 0)
199                 Log_fatal("Could not read RSA key file %s", keyfile);
200 }
201
202 #define DEBUG_LEVEL 0
203 static void pssl_debug(void *ctx, int level, const char *str)
204 {
205     if (level <= DEBUG_LEVEL)
206                 Log_debug("PolarSSL [level %d]: %s", level, str);
207 }
208
209 void SSLi_init(void)
210 {
211         initCert();
212         if (builtInTestCertificate) {
213                 Log_warn("*** Using built-in test certificate and RSA key ***");
214                 Log_warn("*** This is not secure! Please use a CA-signed certificate or create a self-signed certificate ***");
215                 initTestKey();
216         }
217         else
218                 initKey();
219     havege_init(&hs);
220         Log_info("PolarSSL library initialized");
221 }
222
223 void SSLi_deinit(void)
224 {
225         x509_free(&certificate);
226         rsa_free(&key);
227 }
228
229 SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready)
230 {
231         ssl_context *ssl;
232         ssl_session *ssn;
233         int rc;
234         
235         ssl = malloc(sizeof(ssl_context));
236         ssn = malloc(sizeof(ssl_session));
237         if (!ssl || !ssn)
238                 Log_fatal("Out of memory");
239         memset(ssl, 0, sizeof(ssl_context));
240         memset(ssn, 0, sizeof(ssl_session));
241         
242         rc = ssl_init(ssl);
243         if (rc != 0 )
244                 Log_fatal("Failed to initalize: %d", rc);
245         
246         ssl_set_endpoint(ssl, SSL_IS_SERVER);   
247     ssl_set_authmode(ssl, SSL_VERIFY_OPTIONAL);
248
249     ssl_set_rng(ssl, havege_rand, &hs);
250     ssl_set_dbg(ssl, pssl_debug, NULL);
251     ssl_set_bio(ssl, net_recv, fd, net_send, fd);
252
253     ssl_set_ciphers(ssl, ciphers);
254     ssl_set_session(ssl, 0, 0, ssn);
255
256     ssl_set_ca_chain(ssl, certificate.next, NULL, NULL);
257     ssl_set_own_cert(ssl, &certificate, &key);
258     ssl_set_dh_param(ssl, my_dhm_P, my_dhm_G);
259
260         return ssl;
261 }
262
263 int SSLi_nonblockaccept(SSL_handle_t *ssl, bool_t *SSLready)
264 {
265         int rc;
266         
267         rc = ssl_handshake(ssl);
268         if (rc != 0) {
269                 if (rc == POLARSSL_ERR_NET_TRY_AGAIN) {
270                         return 0;
271                 } else {
272                         Log_warn("SSL handshake failed: %d", rc);
273                         return -1;
274                 }
275         }
276         *SSLready = true;
277         return 0;
278 }
279
280 int SSLi_read(SSL_handle_t *ssl, uint8_t *buf, int len)
281 {
282         int rc;
283         rc = ssl_read(ssl, buf, len);
284         if (rc == POLARSSL_ERR_NET_TRY_AGAIN)
285                 return SSLI_ERROR_WANT_READ;
286         return rc;
287 }
288
289 int SSLi_write(SSL_handle_t *ssl, uint8_t *buf, int len)
290 {
291         int rc;
292         rc = ssl_write(ssl, buf, len);
293         if (rc == POLARSSL_ERR_NET_TRY_AGAIN)
294                 return SSLI_ERROR_WANT_WRITE;
295         return rc;
296 }
297
298 int SSLi_get_error(SSL_handle_t *ssl, int code)
299 {
300         return code;
301 }
302
303 bool_t SSLi_data_pending(SSL_handle_t *ssl)
304 {
305         return ssl_get_bytes_avail(ssl) > 0;
306 }
307
308 void SSLi_shutdown(SSL_handle_t *ssl)
309 {
310         ssl_close_notify(ssl);
311 }
312
313 void SSLi_free(SSL_handle_t *ssl)
314 {
315         Log_debug("SSLi_free");
316         free(ssl->session); /* XXX - Hmmm. */
317         ssl_free(ssl);
318         free(ssl);
319 }
320
321 #else
322 /*
323  * OpenSSL interface
324  */
325
326 #include <openssl/x509v3.h>
327 #include <openssl/ssl.h>
328 #include <openssl/err.h>
329 #include <openssl/safestack.h>
330 static X509 *x509;
331 static RSA *rsa;
332 static SSL_CTX *context;
333 static EVP_PKEY *pkey;
334
335 static int SSL_add_ext(X509 * crt, int nid, char *value) {
336         X509_EXTENSION *ex;
337         X509V3_CTX ctx;
338         X509V3_set_ctx_nodb(&ctx);
339         X509V3_set_ctx(&ctx, crt, crt, NULL, NULL, 0);
340         ex = X509V3_EXT_conf_nid(NULL, &ctx, nid, value);
341         if (!ex)
342                 return 0;
343
344         X509_add_ext(crt, ex, -1);
345         X509_EXTENSION_free(ex);
346         return 1;
347 }
348
349 static X509 *SSL_readcert(char *certfile)
350 {
351         FILE *fp;
352         X509 *x509;
353                         
354         /* open the private key file */
355         fp = fopen(certfile, "r");
356         if (fp == NULL) {
357                 Log_warn("Unable to open the X509 file %s for reading.", certfile);
358                 return NULL;
359         }
360         
361         /* allocate memory for the cert structure */    
362         x509 = X509_new();
363         
364         if (PEM_read_X509(fp, &x509, NULL, NULL) == 0) {
365                 /* error reading the x509 information - check the error stack */
366                 Log_warn("Error trying to read X509 info.");
367                 fclose(fp);
368                 X509_free(x509);
369                 return NULL;
370         }
371         fclose(fp);
372         return x509;
373 }
374
375 static RSA *SSL_readprivatekey(char *keyfile)
376 {
377         FILE *fp;
378         RSA *rsa;
379
380 /* open the private key file for reading */
381         fp = fopen(keyfile, "r");
382         if (fp == NULL) {
383                 Log_warn("Unable to open the private key file %s for reading.", keyfile);
384                 return NULL;
385         }
386         
387 /* allocate memory for the RSA structure */
388         rsa = RSA_new();
389         
390         /* assign a callback function for the password */
391         
392         /* read a private key from file */      
393         if (PEM_read_RSAPrivateKey(fp, &rsa, NULL, NULL) <= 0) {
394                 /* error reading the key - check the error stack */
395                 Log_warn("Error trying to read private key.");
396                 RSA_free(rsa);
397                 fclose(fp);
398                 return NULL;
399         }
400         fclose(fp);
401         return rsa;
402 }
403
404 static void SSL_writecert(char *certfile, X509 *x509)
405 {
406         FILE *fp;
407         BIO *err_output;
408         
409         /* prepare a BIO for outputting error messages */
410         
411         err_output = BIO_new_fp(stderr,BIO_NOCLOSE);
412         
413         /* open the private key file */
414         fp = fopen(certfile, "w");
415         if (fp == NULL) {
416                 BIO_printf(err_output, "Unable to open the X509 file for writing.\n");
417                 BIO_free(err_output);
418                 return;
419         }
420                 
421         if (PEM_write_X509(fp, x509) == 0) {
422                 BIO_printf(err_output, "Error trying to write X509 info.\n");
423                 ERR_print_errors(err_output);
424         }
425         fclose(fp);
426 }
427
428 static void SSL_writekey(char *keyfile, RSA *rsa)
429 {
430         FILE *fp;
431         BIO *err_output;
432         /* prepare a BIO for outputing error messages */        
433         err_output = BIO_new_fp(stderr, BIO_NOCLOSE);
434         
435         /* open the private key file for reading */
436         fp = fopen(keyfile, "w");
437         if (fp == NULL) {
438                 BIO_printf(err_output, "Unable to open the private key file %s for writing.\n", keyfile);
439                 BIO_free(err_output);
440                 return;
441         }
442         
443         if (PEM_write_RSAPrivateKey(fp, rsa, NULL, NULL, 0, NULL, NULL) == 0) {
444                 /* error reading the key - check the error stack */
445                 BIO_printf(err_output, "Error trying to write private key\n");
446                 ERR_print_errors(err_output);
447         }
448         fclose(fp);
449 }
450
451 static void SSL_initializeCert() {
452         char *crt, *key, *pass;
453         
454         crt = (char *)getStrConf(CERTIFICATE);
455         key = (char *)getStrConf(KEY);
456         pass = (char *)getStrConf(PASSPHRASE);
457
458         x509 = SSL_readcert(crt);
459         rsa = SSL_readprivatekey(key);
460         if (rsa != NULL) {
461                 pkey = EVP_PKEY_new();
462                 EVP_PKEY_assign_RSA(pkey, rsa);
463         }               
464         
465 #if 0
466         /* Later ... */
467         if (key && !x509) {             
468                 qscCert = QSslCertificate(key);
469                 if (! qscCert.isNull()) {
470                         logthis("Using certificate from key.");
471                 }
472         }
473
474         if (! qscCert.isNull()) {
475                 QSsl::KeyAlgorithm alg = qscCert.publicKey().algorithm();
476                 /* Fetch algorith from cert */
477                 if (! key.isEmpty()) {
478                         /* get key */
479                         qskKey = QSslKey(key, alg, QSsl::Pem, QSsl::PrivateKey, pass);
480                         if (qskKey.isNull()) {
481                                 logthis("Failed to parse key.");
482                         }
483                 }
484
485                 if (! crt.isEmpty() && qskKey.isNull()) {
486                         /* get key from certificate */
487                         qskKey = QSslKey(crt, alg, QSsl::Pem, QSsl::PrivateKey, pass);
488                         if (! qskKey.isNull()) {
489                                 logthis("Using key from certificate.");
490                         }
491                 }
492
493         }
494 #endif
495         
496         if (!rsa || !x509) {
497                 logthis("Generating new server certificate.");
498
499                 BIO *bio_err;
500                 
501                 CRYPTO_mem_ctrl(CRYPTO_MEM_CHECK_ON);
502                 
503                 bio_err=BIO_new_fp(stderr, BIO_NOCLOSE);
504                 
505                 x509 = X509_new();
506                 pkey = EVP_PKEY_new();
507                 rsa = RSA_generate_key(1024,RSA_F4,NULL,NULL);
508                 EVP_PKEY_assign_RSA(pkey, rsa);
509                 
510                 X509_set_version(x509, 2);
511                 ASN1_INTEGER_set(X509_get_serialNumber(x509),1);
512                 X509_gmtime_adj(X509_get_notBefore(x509),0);
513                 X509_gmtime_adj(X509_get_notAfter(x509),60*60*24*365);
514                 X509_set_pubkey(x509, pkey);
515                 
516                 X509_NAME *name=X509_get_subject_name(x509);
517                 
518                 X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (const uint8_t *)"Murmur Autogenerated Certificate v2", -1, -1, 0);
519                 X509_set_issuer_name(x509, name);
520                 SSL_add_ext(x509, NID_basic_constraints, "critical,CA:FALSE");
521                 SSL_add_ext(x509, NID_ext_key_usage, "serverAuth,clientAuth");
522                 SSL_add_ext(x509, NID_subject_key_identifier, "hash");
523                 SSL_add_ext(x509, NID_netscape_comment, "Generated from umurmur");
524                 
525                 X509_sign(x509, pkey, EVP_md5());
526                 
527                 SSL_writecert(crt, x509);
528                 SSL_writekey(key, rsa);
529         }
530
531 }
532
533 void SSLi_init(void)
534 {
535         const SSL_METHOD *method;
536         SSL *ssl;
537         int i, offset = 0;
538         STACK_OF(SSL_CIPHER) *cipherlist = NULL, *cipherlist_new = NULL;
539         SSL_CIPHER *cipher;
540         char cipherstring[1024];
541                 
542         SSL_library_init();
543     OpenSSL_add_all_algorithms();               /* load & register all cryptos, etc. */
544     SSL_load_error_strings();                   /* load all error messages */
545     ERR_load_crypto_strings();                  /* load all error messages */
546     method = SSLv23_server_method();            /* create new server-method instance */
547     context = SSL_CTX_new(method);                      /* create new context from method */
548     if (context == NULL)
549     {
550         ERR_print_errors_fp(stderr);
551         abort();
552     }
553         SSL_initializeCert();
554         if (SSL_CTX_use_certificate(context, x509) <= 0)
555                 Log_fatal("Failed to initialize cert");
556         if (SSL_CTX_use_PrivateKey(context, pkey) <= 0) {
557                 ERR_print_errors_fp(stderr);
558                 Log_fatal("Failed to initialize private key");
559         }
560
561         /* Set cipher list */
562         ssl = SSL_new(context); 
563         cipherlist = (STACK_OF(SSL_CIPHER) *) SSL_get_ciphers(ssl);
564         cipherlist_new = (STACK_OF(SSL_CIPHER) *) sk_SSL_CIPHER_new_null();
565         
566         for ( i = 0; (cipher = sk_SSL_CIPHER_value(cipherlist, i)) != NULL; i++) {
567                 if (SSL_CIPHER_get_bits(cipher, NULL) >= 128) {
568                         sk_SSL_CIPHER_push(cipherlist_new, cipher);
569                 }
570         }
571         Log_debug("List of ciphers:");
572         if (cipherlist_new) {
573                 for ( i = 0; (cipher = sk_SSL_CIPHER_value(cipherlist_new, i)) != NULL; i++) {
574                         Log_debug("%s", SSL_CIPHER_get_name(cipher));
575                         offset += snprintf(cipherstring + offset, 1024 - offset, "%s:", SSL_CIPHER_get_name(cipher));
576                 }
577                 cipherstring[offset - 1] = '\0';
578         }
579         
580         if (cipherlist_new)
581                 sk_SSL_CIPHER_free(cipherlist_new);
582         
583         if (strlen(cipherstring) == 0)
584                 Log_fatal("No suitable ciphers found!");
585         
586         if (SSL_CTX_set_cipher_list(context, cipherstring) == 0)
587                 Log_fatal("Failed to set cipher list!");
588                 
589         
590         SSL_free(ssl);
591         Log_info("OpenSSL library initialized");
592
593 }
594
595 void SSLi_deinit(void)
596 {
597         SSL_CTX_free(context);
598         EVP_cleanup();
599 }
600
601 int SSLi_nonblockaccept(SSL_handle_t *ssl, bool_t *SSLready)
602 {
603         int rc;
604         rc = SSL_accept(ssl);
605         if (rc < 0) {
606                 if (SSL_get_error(ssl, rc) == SSL_ERROR_WANT_READ ||
607                         SSL_get_error(ssl, rc) == SSL_ERROR_WANT_WRITE) {
608                         Log_debug("SSL not ready");
609                         return 0;
610                 } else {
611                         Log_warn("SSL error: %s", ERR_error_string(SSL_get_error(ssl, rc), NULL));
612                         return -1;
613                 }
614         }
615         *SSLready = true;
616         return 0;
617 }
618
619 SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready)
620 {
621         SSL *ssl;
622         
623         *SSLready = false;
624         ssl = SSL_new(context);
625         SSL_set_fd(ssl, *fd);
626         if (SSLi_nonblockaccept(ssl, SSLready) < 0) {
627                 SSL_free(ssl);
628                 return NULL;
629         }
630         return ssl;
631 }
632
633 void SSLi_closeconnection(SSL_handle_t *ssl)
634 {
635         SSL_free(ssl);
636 }
637
638 void SSLi_shutdown(SSL_handle_t *ssl)
639 {
640         SSL_shutdown(ssl);
641 }
642
643 int SSLi_read(SSL_handle_t *ssl, uint8_t *buf, int len)
644 {
645         return SSL_read(ssl, buf, len);
646 }
647
648 int SSLi_write(SSL_handle_t *ssl, uint8_t *buf, int len)
649 {
650         return SSL_write(ssl, buf, len);
651 }
652
653 int SSLi_get_error(SSL_handle_t *ssl, int code)
654 {
655         return SSL_get_error(ssl, code);
656 }
657
658 bool_t SSLi_data_pending(SSL_handle_t *ssl)
659 {
660         return SSL_pending(ssl);
661 }
662
663 void SSLi_free(SSL_handle_t *ssl)
664 {
665         SSL_free(ssl);
666 }
667
668 #endif