made SSL error non fatal
[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 = "NONE:+CTYPE-X.509:+DHE-RSA:+RSA:+AES-256-CBC:+AES-128-CBC:+SHA256:+SHA1:+VERS-TLS-ALL:+COMP-ALL:+SIGN-DSA-SHA256:+SIGN-DSA-SHA1";
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   }
60
61 SSL_handle_t * SSLi_newconnection( int * fileDescriptor, bool_t * isSSLReady )
62   {
63   gnutls_session_t * session = calloc(1, sizeof(gnutls_session_t));
64
65   gnutls_init(session, GNUTLS_SERVER);
66   gnutls_priority_set(*session, cipherCache);
67   gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE, certificate);
68
69   gnutls_certificate_server_set_request(*session, GNUTLS_CERT_REQUIRE);
70
71   gnutls_transport_set_int(*session, *fileDescriptor);
72
73   if(isSSLReady && SSLi_nonblockaccept(session, isSSLReady))
74     *isSSLReady = true;
75
76   return session;
77   }
78
79 bool_t SSLi_getSHA1Hash(SSL_handle_t *ssl, uint8_t *hash)
80   {
81   *hash = 0;
82   return true;
83   }
84
85 int SSLi_nonblockaccept( SSL_handle_t *session, bool_t * isSSLReady )
86   {
87   int error;
88   do {
89     error = gnutls_handshake(*session);
90   } while(error < GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(error));
91
92   if ( error < GNUTLS_E_SUCCESS ) {
93     Log_warn("TLS handshake failed with error %i (%s).", error, gnutls_strerror(error));
94   }
95
96   if(isSSLReady)
97     *isSSLReady = true;
98
99   return error;
100   }
101
102 int SSLi_read(SSL_handle_t *session, uint8_t *buffer, int length)
103   {
104   return gnutls_record_recv(*session, buffer, length);
105   }
106
107 int SSLi_write(SSL_handle_t *session, uint8_t *buffer, int length)
108   {
109   return gnutls_record_send(*session, buffer, length);
110   }
111
112 int SSLi_get_error(SSL_handle_t *session, int code)
113   {
114   return code;
115   }
116
117 bool_t SSLi_data_pending(SSL_handle_t *session)
118   {
119   return gnutls_record_check_pending(*session);
120   }
121
122 void SSLi_shutdown(SSL_handle_t *session)
123   {
124   gnutls_bye(*session, GNUTLS_SHUT_WR);
125   }
126
127 void SSLi_free(SSL_handle_t *session)
128   {
129   gnutls_deinit(*session);
130   }