c081a9c0ea948487cafa7e3bb0125b3964e1c26b
[umurmur.git] / src / ssli_gnutls.c
1 #include "ssl.h"
2 #include "conf.h"
3 #include "log.h"
4
5 static gnutls_dh_params_t dhParameters;
6 static gnutls_certificate_credentials_t certificate;
7
8 static const char * ciphers = "SECURE128:-VERS-DTLS-ALL:-VERS-SSL3.0:-VERS-TLS1.0:+COMP_ALL";
9 static gnutls_priority_t cipherCache;
10
11 void initializeCertificate()
12   {
13   char* certificatePath = (char*) getStrConf(CERTIFICATE);
14
15   if(!certificatePath) {
16     Log_fatal("No certificate file specified.");
17   }
18
19   char* keyPath = (char*) getStrConf(KEY);
20
21   if(!keyPath) {
22     Log_fatal("No key file specified");
23   }
24
25   gnutls_certificate_allocate_credentials(&certificate);
26
27   int error = gnutls_certificate_set_x509_key_file(certificate, certificatePath, keyPath, GNUTLS_X509_FMT_PEM);
28
29   if( error != GNUTLS_E_SUCCESS ) {
30     Log_fatal("Could not open key (%s) or certificate (%s).", keyPath, certificatePath);
31   }
32
33   }
34
35 void SSLi_init()
36   {
37   unsigned const bitCount = gnutls_sec_param_to_pk_bits(GNUTLS_PK_DH, GNUTLS_SEC_PARAM_MEDIUM);
38
39   gnutls_dh_params_init(&dhParameters);
40   gnutls_dh_params_generate2(dhParameters, bitCount);
41
42 #if GNUTLS_VERSION_NUMBER < 0x030300
43   gnutls_global_init();
44 #endif
45
46   gnutls_priority_init(&cipherCache, ciphers, NULL);
47
48   initializeCertificate();
49
50   Log_info("Sucessfully initialized GNUTLS version %s", gnutls_check_version(NULL));
51
52   }
53
54 void SSLi_deinit()
55   {
56   gnutls_certificate_free_credentials(certificate);
57   gnutls_priority_deinit(cipherCache);
58   gnutls_global_deinit();
59   }