Upgrade TLS suites for GnuTLS, mbedTLS and OpenSSL
[umurmur.git] / src / ssli_gnutls.c
1 /* Copyright (C) 2015-2016, Felix Morgner <felix.morgner@gmail.com>
2
3    All rights reserved.
4
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8
9    - Redistributions of source code must retain the above copyright notice,
10      this list of conditions and the following disclaimer.
11    - Redistributions in binary form must reproduce the above copyright notice,
12      this list of conditions and the following disclaimer in the documentation
13      and/or other materials provided with the distribution.
14    - Neither the name of the Developers nor the names of its contributors may
15      be used to endorse or promote products derived from this software without
16      specific prior written permission.
17
18    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21    A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
22    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
25    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
26    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
27    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include "ssl.h"
32 #include "conf.h"
33 #include "log.h"
34 #include "memory.h"
35
36 #include <stdlib.h>
37
38 static gnutls_dh_params_t dhParameters;
39 static gnutls_certificate_credentials_t certificate;
40
41 static const char * ciphers = "NONE:"
42                                                           "+ECDHE-ECDSA:+ECDHE-RSA:+RSA:"
43                                                           "+AES-256-GCM:+AES-128-GCM:"
44                                                           "+AEAD:+SHA384:+SHA256:+SHA1:"
45                                                           "+CURVE-ALL:"
46                                                           "+COMP-NULL:"
47                                                           "+SIGN-ALL:"
48                                                           "+VERS-TLS1.2:+VERS-TLS1.0:"
49                                                           "+CTYPE-X509";
50
51 static gnutls_priority_t cipherCache;
52
53 void initializeCertificate()
54 {
55         char* certificatePath = (char*) getStrConf(CERTIFICATE);
56
57         if(!certificatePath) {
58                 Log_fatal("No certificate file specified.");
59         }
60
61         char* keyPath = (char*) getStrConf(KEY);
62
63         if(!keyPath) {
64                 Log_fatal("No key file specified");
65         }
66
67         gnutls_certificate_allocate_credentials(&certificate);
68
69         int error = gnutls_certificate_set_x509_key_file(certificate, certificatePath, keyPath, GNUTLS_X509_FMT_PEM);
70
71         if( error != GNUTLS_E_SUCCESS ) {
72                 Log_fatal("Could not open key (%s) or certificate (%s).", keyPath, certificatePath);
73         }
74 }
75
76 void SSLi_init()
77 {
78         if(gnutls_priority_init(&cipherCache, ciphers, NULL) != GNUTLS_E_SUCCESS)
79         {
80                 Log_fatal("Failed to set priorities");
81         }
82
83         initializeCertificate();
84
85         Log_info("Sucessfully initialized GNUTLS version %s", gnutls_check_version(NULL));
86 }
87
88 void SSLi_deinit()
89 {
90         gnutls_certificate_free_credentials(certificate);
91         gnutls_priority_deinit(cipherCache);
92         gnutls_global_deinit();
93 }
94
95 SSL_handle_t * SSLi_newconnection( int * fileDescriptor, bool_t * isSSLReady )
96 {
97         gnutls_session_t * session
98                 = Memory_safeCalloc(1, sizeof(gnutls_session_t));
99
100         gnutls_init(session, GNUTLS_SERVER);
101         gnutls_priority_set(*session, cipherCache);
102         gnutls_credentials_set(*session, GNUTLS_CRD_CERTIFICATE, certificate);
103
104         gnutls_certificate_server_set_request(*session, GNUTLS_CERT_REQUIRE);
105
106         gnutls_transport_set_int(*session, *fileDescriptor);
107
108         if(isSSLReady && SSLi_nonblockaccept(session, isSSLReady))
109                 *isSSLReady = true;
110
111         return session;
112 }
113
114 bool_t SSLi_getSHA1Hash(SSL_handle_t *session, uint8_t *hash)
115 {
116         gnutls_datum_t const * certificateData = gnutls_certificate_get_peers(*session, NULL);
117
118         size_t resultSize = 0;
119         int error = gnutls_fingerprint( GNUTLS_DIG_SHA1, certificateData, hash, &resultSize);
120         return error == GNUTLS_E_SUCCESS && resultSize == 20;
121 }
122
123 int SSLi_nonblockaccept( SSL_handle_t *session, bool_t * isSSLReady )
124 {
125         int error;
126         do {
127                 error = gnutls_handshake(*session);
128         } while(error < GNUTLS_E_SUCCESS && !gnutls_error_is_fatal(error));
129
130         if ( error < GNUTLS_E_SUCCESS ) {
131                 Log_warn("TLS handshake failed with error %i (%s).", error, gnutls_strerror(error));
132         }
133
134         if(isSSLReady)
135                 *isSSLReady = true;
136
137         return error;
138 }
139
140 int SSLi_read(SSL_handle_t *session, uint8_t *buffer, int length)
141 {
142         return gnutls_record_recv(*session, buffer, length);
143 }
144
145 int SSLi_write(SSL_handle_t *session, uint8_t *buffer, int length)
146 {
147         return gnutls_record_send(*session, buffer, length);
148 }
149
150 int SSLi_get_error(SSL_handle_t *session, int code)
151 {
152         return code;
153 }
154
155 bool_t SSLi_data_pending(SSL_handle_t *session)
156 {
157         return gnutls_record_check_pending(*session);
158 }
159
160 void SSLi_shutdown(SSL_handle_t *session)
161 {
162         gnutls_bye(*session, GNUTLS_SHUT_WR);
163 }
164
165 void SSLi_free(SSL_handle_t *session)
166 {
167         gnutls_deinit(*session);
168         free(session);
169 }