X-Git-Url: http://git.code-monkey.de/?a=blobdiff_plain;f=src%2Fssl.c;h=4224c1de08f152d93922dc4eda09c8b03480f98e;hb=454ad122eb158b4391e2690fe6a7e127d24c525b;hp=09510c033cabdb35ce118ce1ac078f71de5e0c53;hpb=820c23b9acd55999cc2e5777d5beeec8efd8970d;p=umurmur.git diff --git a/src/ssl.c b/src/ssl.c index 09510c0..4224c1d 100644 --- a/src/ssl.c +++ b/src/ssl.c @@ -1,5 +1,5 @@ -/* Copyright (C) 2009, Martin Johansson - Copyright (C) 2005-2009, Thorvald Natvig +/* Copyright (C) 2009-2010, Martin Johansson + Copyright (C) 2005-2010, Thorvald Natvig All rights reserved. @@ -28,16 +28,269 @@ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include -#include -#include -#include #include +#include #include "conf.h" #include "log.h" #include "ssl.h" +#ifdef USE_POLARSSL +/* + * PolarSSL interface + */ + +#include +#include +#include +#include +#include + +#define CA_CRT_FILENAME "ca.crt" + +int ciphers[] = +{ + SSL_EDH_RSA_AES_256_SHA, + SSL_EDH_RSA_CAMELLIA_256_SHA, + SSL_EDH_RSA_DES_168_SHA, + SSL_RSA_AES_256_SHA, + SSL_RSA_CAMELLIA_256_SHA, + SSL_RSA_AES_128_SHA, + SSL_RSA_CAMELLIA_128_SHA, + SSL_RSA_DES_168_SHA, + SSL_RSA_RC4_128_SHA, + SSL_RSA_RC4_128_MD5, + 0 +}; +static x509_cert certificate; +static rsa_context key; +bool_t builtInTestCertificate; + +havege_state hs; /* exported to crypt.c */ + +/* DH prime */ +char *my_dhm_P = + "9CE85640903BF123906947FEDE767261" \ + "D9B4A973EB8F7D984A8C656E2BCC161C" \ + "183D4CA471BA78225F940F16D1D99CA3" \ + "E66152CC68EDCE1311A390F307741835" \ + "44FF6AB553EC7073AD0CB608F2A3B480" \ + "19E6C02BCED40BD30E91BB2469089670" \ + "DEF409C08E8AC24D1732A6128D2220DC53"; +char *my_dhm_G = "4"; + +static void initTestCert() +{ + int rc; + builtInTestCertificate = true; + rc = x509parse_crt(&certificate, (unsigned char *)test_srv_crt, + strlen(test_srv_crt)); + if (rc != 0) + Log_fatal("Could not parse built-in test certificate"); + rc = x509parse_crt(&certificate, (unsigned char *)test_ca_crt, + strlen(test_ca_crt)); + if (rc != 0) + Log_fatal("Could not parse built-in test CA certificate"); +} + +static void initTestKey() +{ + int rc; + + rc = x509parse_key(&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"); +} + +/* + * openssl genrsa 1024 > host.key + * openssl req -new -x509 -nodes -sha1 -days 365 -key host.key > host.cert + */ +static void initCert() +{ + int rc; + char *crtfile = (char *)getStrConf(CERTIFICATE); + char *ca_file, *p; + + if (crtfile == NULL) { + Log_warn("No certificate file specified"); + initTestCert(); + return; + } + rc = x509parse_crtfile(&certificate, crtfile); + if (rc != 0) { + Log_warn("Could not read certificate file %s", crtfile); + initTestCert(); + return; + } + + /* Look for CA certificate file in same dir */ + ca_file = malloc(strlen(crtfile) + strlen(CA_CRT_FILENAME) + 1); + strcpy(ca_file, crtfile); + p = strrchr(ca_file, '/'); + if (p != NULL) + strcpy(p + 1, CA_CRT_FILENAME); + else + strcpy(ca_file, CA_CRT_FILENAME); + + rc = x509parse_crtfile(&certificate, ca_file); + if (rc != 0) { /* No CA certifiacte found. Assume self-signed. */ + Log_info("CA certificate file %s not found. Assuming self-signed certificate.", ca_file); + } + + /* + * PolarSSL 0.11 - 0.12,1 has a bug; it ignores the last certificate in the chain. + * Read the certificate again so that it gets last in chain. Later releases like 0.14.0 works + * fine with the extra certificate, so I don't see any harm in doing so. + */ + rc = x509parse_crtfile(&certificate, crtfile); + if (rc != 0) + Log_fatal("Could not read certificate file %s", crtfile); + + free(ca_file); +} + +static void initKey() +{ + int rc; + char *keyfile = (char *)getStrConf(KEY); + + if (keyfile == NULL) + Log_fatal("No key file specified"); + rc = x509parse_keyfile(&key, keyfile, NULL); + if (rc != 0) + Log_fatal("Could not read RSA key file %s", keyfile); +} + +#define DEBUG_LEVEL 0 +static void pssl_debug(void *ctx, int level, const char *str) +{ + if (level <= DEBUG_LEVEL) + Log_debug("PolarSSL [level %d]: %s", level, str); +} + +void SSLi_init(void) +{ + initCert(); + if (builtInTestCertificate) { + Log_warn("*** Using built-in test certificate and RSA key ***"); + Log_warn("*** This is not secure! Please use a CA-signed certificate or create a self-signed certificate ***"); + initTestKey(); + } + else + initKey(); + havege_init(&hs); + Log_info("PolarSSL library initialized"); +} + +void SSLi_deinit(void) +{ + x509_free(&certificate); + rsa_free(&key); +} + +SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready) +{ + ssl_context *ssl; + ssl_session *ssn; + int rc; + + ssl = malloc(sizeof(ssl_context)); + ssn = malloc(sizeof(ssl_session)); + if (!ssl || !ssn) + Log_fatal("Out of memory"); + memset(ssl, 0, sizeof(ssl_context)); + memset(ssn, 0, sizeof(ssl_session)); + + rc = ssl_init(ssl); + if (rc != 0 ) + Log_fatal("Failed to initalize: %d", rc); + + ssl_set_endpoint(ssl, SSL_IS_SERVER); + ssl_set_authmode(ssl, SSL_VERIFY_OPTIONAL); + + ssl_set_rng(ssl, havege_rand, &hs); + ssl_set_dbg(ssl, pssl_debug, NULL); + ssl_set_bio(ssl, net_recv, fd, net_send, fd); + + ssl_set_ciphers(ssl, ciphers); + ssl_set_session(ssl, 0, 0, ssn); + + ssl_set_ca_chain(ssl, certificate.next, NULL, NULL); + ssl_set_own_cert(ssl, &certificate, &key); + ssl_set_dh_param(ssl, my_dhm_P, my_dhm_G); + + return ssl; +} + +int SSLi_nonblockaccept(SSL_handle_t *ssl, bool_t *SSLready) +{ + int rc; + + rc = ssl_handshake(ssl); + if (rc != 0) { + if (rc == POLARSSL_ERR_NET_TRY_AGAIN) { + return 0; + } else { + Log_warn("SSL handshake failed: %d", rc); + return -1; + } + } + *SSLready = true; + return 0; +} + +int SSLi_read(SSL_handle_t *ssl, uint8_t *buf, int len) +{ + int rc; + rc = ssl_read(ssl, buf, len); + if (rc == POLARSSL_ERR_NET_TRY_AGAIN) + return SSLI_ERROR_WANT_READ; + return rc; +} + +int SSLi_write(SSL_handle_t *ssl, uint8_t *buf, int len) +{ + int rc; + rc = ssl_write(ssl, buf, len); + if (rc == POLARSSL_ERR_NET_TRY_AGAIN) + return SSLI_ERROR_WANT_WRITE; + return rc; +} + +int SSLi_get_error(SSL_handle_t *ssl, int code) +{ + return code; +} + +bool_t SSLi_data_pending(SSL_handle_t *ssl) +{ + return ssl_get_bytes_avail(ssl) > 0; +} + +void SSLi_shutdown(SSL_handle_t *ssl) +{ + ssl_close_notify(ssl); +} + +void SSLi_free(SSL_handle_t *ssl) +{ + Log_debug("SSLi_free"); + free(ssl->session); /* XXX - Hmmm. */ + ssl_free(ssl); + free(ssl); +} + +#else +/* + * OpenSSL interface + */ + +#include +#include +#include +#include static X509 *x509; static RSA *rsa; static SSL_CTX *context; @@ -112,7 +365,7 @@ static RSA *SSL_readprivatekey(char *keyfile) return rsa; } -void SSL_writecert(char *certfile, X509 *x509) +static void SSL_writecert(char *certfile, X509 *x509) { FILE *fp; BIO *err_output; @@ -136,7 +389,7 @@ void SSL_writecert(char *certfile, X509 *x509) fclose(fp); } -void SSL_writekey(char *keyfile, RSA *rsa) +static void SSL_writekey(char *keyfile, RSA *rsa) { FILE *fp; BIO *err_output; @@ -159,7 +412,7 @@ void SSL_writekey(char *keyfile, RSA *rsa) fclose(fp); } -void SSL_initializeCert() { +static void SSL_initializeCert() { char *crt, *key, *pass; crt = (char *)getStrConf(CERTIFICATE); @@ -241,30 +494,9 @@ void SSL_initializeCert() { } -void SSL_getdigest(char *s, int l) -{ - unsigned char md[EVP_MAX_MD_SIZE]; - unsigned int n; - int j; - - if (!X509_digest (x509, EVP_md5(), md, &n)) - { - snprintf (s, l, "[unable to calculate]"); - } - else - { - for (j = 0; j < (int) n; j++) - { - char ch[8]; - snprintf(ch, 8, "%02X%s", md[j], (j % 2 ? " " : "")); - strcat(s, ch); - } - } -} - -void SSL_init(void) +void SSLi_init(void) { - SSL_METHOD *method; + const SSL_METHOD *method; SSL *ssl; int i, offset = 0; STACK_OF(SSL_CIPHER) *cipherlist = NULL, *cipherlist_new = NULL; @@ -320,15 +552,17 @@ void SSL_init(void) SSL_free(ssl); + Log_info("OpenSSL library initialized"); + } -void SSL_deinit(void) +void SSLi_deinit(void) { SSL_CTX_free(context); EVP_cleanup(); } -int SSL_nonblockaccept(SSL *ssl, bool_t *SSLready) +int SSLi_nonblockaccept(SSL_handle_t *ssl, bool_t *SSLready) { int rc; rc = SSL_accept(ssl); @@ -346,23 +580,53 @@ int SSL_nonblockaccept(SSL *ssl, bool_t *SSLready) return 0; } -SSL *SSL_newconnection(int fd, bool_t *SSLready) +SSL_handle_t *SSLi_newconnection(int *fd, bool_t *SSLready) { SSL *ssl; *SSLready = false; ssl = SSL_new(context); - SSL_set_fd(ssl, fd); - if (SSL_nonblockaccept(ssl, SSLready) < 0) { + SSL_set_fd(ssl, *fd); + if (SSLi_nonblockaccept(ssl, SSLready) < 0) { SSL_free(ssl); return NULL; } return ssl; } -void SSL_closeconnection(SSL *ssl) +void SSLi_closeconnection(SSL_handle_t *ssl) { SSL_free(ssl); } +void SSLi_shutdown(SSL_handle_t *ssl) +{ + SSL_shutdown(ssl); +} + +int SSLi_read(SSL_handle_t *ssl, uint8_t *buf, int len) +{ + return SSL_read(ssl, buf, len); +} + +int SSLi_write(SSL_handle_t *ssl, uint8_t *buf, int len) +{ + return SSL_write(ssl, buf, len); +} + +int SSLi_get_error(SSL_handle_t *ssl, int code) +{ + return SSL_get_error(ssl, code); +} +bool_t SSLi_data_pending(SSL_handle_t *ssl) +{ + return SSL_pending(ssl); +} + +void SSLi_free(SSL_handle_t *ssl) +{ + SSL_free(ssl); +} + +#endif