Update copyright year
[umurmur.git] / src / ssl.c
index 2ea92dbf8129509d38afe48e63178140f35b99a5..42385abd0b7f2cf34765a9609cdf0c83f7073a5a 100644 (file)
--- a/src/ssl.c
+++ b/src/ssl.c
@@ -1,5 +1,5 @@
-/* Copyright (C) 2009-2012, Martin Johansson <martin@fatbob.nu>
-   Copyright (C) 2005-2012, Thorvald Natvig <thorvald@natvig.com>
+/* Copyright (C) 2009-2014, Martin Johansson <martin@fatbob.nu>
+   Copyright (C) 2005-2014, Thorvald Natvig <thorvald@natvig.com>
 
    All rights reserved.
 
  * PolarSSL interface
  */
 
+#include <polarssl/config.h>
 #include <polarssl/havege.h>
 #include <polarssl/certs.h>
 #include <polarssl/x509.h>
 #include <polarssl/ssl.h>
 #include <polarssl/net.h>
 
-#ifdef POLARSSL_API_V1_2
+#ifdef POLARSSL_API_V1_2_ABOVE
 int ciphers[] =
 {
     TLS_DHE_RSA_WITH_AES_256_CBC_SHA,
@@ -64,7 +65,30 @@ int ciphers[] =
     0
 };
 #endif
+
+#ifdef POLARSSL_API_V1_3_ABOVE
+static x509_crt certificate;
+static inline int x509parse_keyfile(rsa_context *rsa, const char *path,
+                                    const char *pwd)
+{
+    int ret;
+    pk_context pk;
+    
+    pk_init(&pk);
+    ret = pk_parse_keyfile(&pk, path, pwd);
+    if (ret == 0 && !pk_can_do( &pk, POLARSSL_PK_RSA))
+        ret = POLARSSL_ERR_PK_TYPE_MISMATCH;
+    if (ret == 0)
+        rsa_copy(rsa, pk_rsa(pk));
+    else
+        rsa_free(rsa);
+    pk_free(&pk);
+    return ret;
+}
+#else
 static x509_cert certificate;
+#endif
+
 static rsa_context key;
 bool_t builtInTestCertificate;
 
@@ -90,8 +114,13 @@ static void initTestCert()
 {
        int rc;
        builtInTestCertificate = true;
+#ifdef POLARSSL_API_V1_3_ABOVE
+       rc = x509_crt_parse_rsa(&certificate, (unsigned char *)test_srv_crt,
+               strlen(test_srv_crt));
+#else
        rc = x509parse_crt(&certificate, (unsigned char *)test_srv_crt,
-                                          strlen(test_srv_crt));       
+               strlen(test_srv_crt));
+#endif
        if (rc != 0)
                Log_fatal("Could not parse built-in test certificate");
 }
@@ -100,8 +129,8 @@ static void initTestKey()
 {
        int rc;
        
-       rc = x509parse_key(&key, (unsigned char *)test_srv_key,
-                                          strlen(test_srv_key), NULL, 0);
+       rc = x509parse_key_rsa(&key, (unsigned char *)test_srv_key,
+                              strlen(test_srv_key), NULL, 0);
        if (rc != 0)
                Log_fatal("Could not parse built-in test RSA key");
 }
@@ -126,7 +155,11 @@ static void initCert()
 #endif
                return;
        }
+#ifdef POLARSSL_API_V1_3_ABOVE
+       rc = x509_crt_parse_file(&certificate, crtfile);
+#else
        rc = x509parse_crtfile(&certificate, crtfile);
+#endif
        if (rc != 0) {
 #ifdef USE_POLARSSL_TESTCERT
                Log_warn("Could not read certificate file '%s'. Falling back to test certificate.", crtfile);
@@ -204,15 +237,23 @@ void SSLi_init(void)
 
 void SSLi_deinit(void)
 {
+#ifdef POLARSSL_API_V1_3_ABOVE
+       x509_crt_free(&certificate);
+#else  
        x509_free(&certificate);
+#endif
        rsa_free(&key);
 }
 
 /* Create SHA1 of last certificate in the peer's chain. */
 bool_t SSLi_getSHA1Hash(SSL_handle_t *ssl, uint8_t *hash)
 {
+#ifdef POLARSSL_API_V1_3_ABOVE
+       x509_crt const *cert;
+#else
        x509_cert const *cert;
-#ifdef POLARSSL_API_V1_2
+#endif
+#ifdef POLARSSL_API_V1_2_ABOVE
        cert = ssl_get_peer_cert(ssl);
 #else
        cert = ssl->peer_cert;
@@ -255,14 +296,18 @@ SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready)
 
        ssl_set_ciphersuites(ssl, ciphers);
 
-#ifdef POLARSSL_API_V1_2
+#ifdef POLARSSL_API_V1_2_ABOVE
     ssl_set_session(ssl, ssn);
 #else
     ssl_set_session(ssl, 0, 0, ssn);
 #endif
     
     ssl_set_ca_chain(ssl, &certificate, NULL, NULL);
+#ifdef POLARSSL_API_V1_3_ABOVE
+       ssl_set_own_cert_rsa(ssl, &certificate, &key);
+#else
        ssl_set_own_cert(ssl, &certificate, &key);
+#endif
        ssl_set_dh_param(ssl, my_dhm_P, my_dhm_G);
 
        return ssl;
@@ -368,7 +413,7 @@ static X509 *SSL_readcert(char *certfile)
        FILE *fp;
        X509 *x509;
                        
-       /* open the private key file */
+       /* open the certificate file */
        fp = fopen(certfile, "r");
        if (fp == NULL) {
                Log_warn("Unable to open the X509 file %s for reading.", certfile);
@@ -452,6 +497,7 @@ static void SSL_writekey(char *keyfile, RSA *rsa)
 }
 
 static void SSL_initializeCert() {
+
        char *crt, *key, *pass;
        
        crt = (char *)getStrConf(CERTIFICATE);
@@ -464,6 +510,7 @@ static void SSL_initializeCert() {
                pkey = EVP_PKEY_new();
                EVP_PKEY_assign_RSA(pkey, rsa);
        }               
+
        
 #if 0
        /* Later ... */
@@ -540,16 +587,23 @@ void SSLi_init(void)
        char *cipherstring, tempstring[128];
                
        SSL_library_init();
-    OpenSSL_add_all_algorithms();              /* load & register all cryptos, etc. */
-    SSL_load_error_strings();                  /* load all error messages */
-    ERR_load_crypto_strings();                 /* load all error messages */
-    method = SSLv23_server_method();           /* create new server-method instance */
-    context = SSL_CTX_new(method);                     /* create new context from method */
-    if (context == NULL)
-    {
-        ERR_print_errors_fp(stderr);
-        abort();
-    }
+       OpenSSL_add_all_algorithms();           /* load & register all cryptos, etc. */
+       SSL_load_error_strings();                       /* load all error messages */
+       ERR_load_crypto_strings();                      /* load all error messages */
+       method = SSLv23_server_method();                /* create new server-method instance */
+       context = SSL_CTX_new(method);                  /* create new context from method */
+       if (context == NULL)
+       {
+               ERR_print_errors_fp(stderr);
+               abort();
+       }
+       
+       char* sslCAPath = getStrConf(CAPATH);
+       if(sslCAPath != NULL)
+       {
+               SSL_CTX_load_verify_locations(context, NULL, sslCAPath);
+       }
+       
        SSL_initializeCert();
        if (SSL_CTX_use_certificate(context, x509) <= 0)
                Log_fatal("Failed to initialize cert");
@@ -557,7 +611,7 @@ void SSLi_init(void)
                ERR_print_errors_fp(stderr);
                Log_fatal("Failed to initialize private key");
        }
-
+       
        /* Set cipher list */
        ssl = SSL_new(context); 
        cipherlist = (STACK_OF(SSL_CIPHER) *) SSL_get_ciphers(ssl);