Fix build with OpenSSL on earlier versions.
[exim.git] / src / src / tlscert-gnu.c
CommitLineData
9d1c15ef
JH
1/*************************************************
2* Exim - an Internet mail transport agent *
3*************************************************/
4
5/* Copyright (c) Jeremy Harris 2014 */
6
7/* This file provides TLS/SSL support for Exim using the GnuTLS library,
8one of the available supported implementations. This file is #included into
9tls.c when USE_GNUTLS has been set.
10*/
11
12#include <gnutls/gnutls.h>
13/* needed for cert checks in verification and DN extraction: */
14#include <gnutls/x509.h>
15/* needed to disable PKCS11 autoload unless requested */
16#if GNUTLS_VERSION_NUMBER >= 0x020c00
17# include <gnutls/pkcs11.h>
18#endif
19
20
21/*****************************************************
22* Export/import a certificate, binary/printable
23*****************************************************/
24int
25tls_export_cert(uschar * buf, size_t buflen, void * cert)
26{
27size_t sz = buflen;
28void * reset_point = store_get(0);
29int fail = 0;
30uschar * cp;
31
32if (gnutls_x509_crt_export((gnutls_x509_crt_t)cert,
33 GNUTLS_X509_FMT_PEM, buf, &sz))
34 return 1;
35if ((cp = string_printing(buf)) != buf)
36 {
37 Ustrncpy(buf, cp, buflen);
38 if (buf[buflen-1])
39 fail = 1;
40 }
41store_reset(reset_point);
42return fail;
43}
44
45int
46tls_import_cert(const uschar * buf, void ** cert)
47{
48void * reset_point = store_get(0);
49gnutls_datum_t datum;
50gnutls_x509_crt_t crt;
51int fail = 0;
52
53gnutls_global_init();
54gnutls_x509_crt_init(&crt);
55
56datum.data = string_unprinting(US buf);
57datum.size = Ustrlen(datum.data);
58if (gnutls_x509_crt_import(crt, &datum, GNUTLS_X509_FMT_PEM))
59 fail = 1;
60else
61 *cert = (void *)crt;
62
63store_reset(reset_point);
64return fail;
65}
66
67void
68tls_free_cert(void * cert)
69{
70gnutls_x509_crt_deinit((gnutls_x509_crt_t) cert);
71gnutls_global_deinit();
72}
73
74/*****************************************************
75* Certificate field extraction routines
76*****************************************************/
77static uschar *
78time_copy(time_t t)
79{
80uschar * cp = store_get(32);
81struct tm * tp = gmtime(&t);
82size_t len = strftime(CS cp, 32, "%b %e %T %Y %Z", tp);
83return len > 0 ? cp : NULL;
84}
85
86/**/
87
88uschar *
89tls_cert_issuer(void * cert)
90{
91uschar txt[256];
92size_t sz = sizeof(txt);
93return ( gnutls_x509_crt_get_issuer_dn(cert, CS txt, &sz) == 0 )
94 ? string_copy(txt) : NULL;
95}
96
97uschar *
98tls_cert_not_after(void * cert)
99{
100return time_copy(
101 gnutls_x509_crt_get_expiration_time((gnutls_x509_crt_t)cert));
102}
103
104uschar *
105tls_cert_not_before(void * cert)
106{
107return time_copy(
108 gnutls_x509_crt_get_activation_time((gnutls_x509_crt_t)cert));
109}
110
111uschar *
112tls_cert_serial_number(void * cert)
113{
114uschar bin[50], txt[150];
115size_t sz = sizeof(bin);
116uschar * sp;
117uschar * dp;
118
119if (gnutls_x509_crt_get_serial((gnutls_x509_crt_t)cert,
120 bin, &sz) || sz > sizeof(bin))
121 return NULL;
122for(dp = txt, sp = bin; sz; dp += 2, sp++, sz--)
123 sprintf(dp, "%.2x", *sp);
124for(sp = txt; sp[0]=='0' && sp[1]; ) sp++; /* leading zeroes */
125return string_copy(sp);
126}
127
128uschar *
129tls_cert_signature(void * cert)
130{
131uschar * cp1;
132uschar * cp2;
133uschar * cp3;
134size_t len = 0;
135int ret;
136
137if ((ret = gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len)) !=
138 GNUTLS_E_SHORT_MEMORY_BUFFER)
139 {
140 fprintf(stderr, "%s: gs0 fail: %s\n", __FUNCTION__, gnutls_strerror(ret));
141 return NULL;
142 }
143
144cp1 = store_get(len*4+1);
145
146if (gnutls_x509_crt_get_signature((gnutls_x509_crt_t)cert, cp1, &len) != 0)
147 {
148 fprintf(stderr, "%s: gs1 fail\n", __FUNCTION__);
149 return NULL;
150 }
151
152for(cp3 = cp2 = cp1+len; cp1 < cp2; cp3 += 3, cp1++)
153 sprintf(cp3, "%.2x ", *cp1);
154cp3[-1]= '\0';
155
156return cp2;
157}
158
159uschar *
160tls_cert_signature_algorithm(void * cert)
161{
162gnutls_sign_algorithm_t algo =
163 gnutls_x509_crt_get_signature_algorithm((gnutls_x509_crt_t)cert);
164return algo < 0 ? NULL : string_copy(gnutls_sign_get_name(algo));
165}
166
167uschar *
168tls_cert_subject(void * cert)
169{
170static uschar txt[256];
171size_t sz = sizeof(txt);
172return ( gnutls_x509_crt_get_dn(cert, CS txt, &sz) == 0 )
173 ? string_copy(txt) : NULL;
174}
175
176uschar *
177tls_cert_version(void * cert)
178{
179return string_sprintf("%d", gnutls_x509_crt_get_version(cert));
180}
181
182uschar *
183tls_cert_ext_by_oid(void * cert, uschar * oid, int idx)
184{
185uschar * cp1 = NULL;
186uschar * cp2;
187uschar * cp3;
188size_t siz = 0;
189unsigned int crit;
190int ret;
191
192ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
193 oid, idx, cp1, &siz, &crit);
194if (ret != GNUTLS_E_SHORT_MEMORY_BUFFER)
195 {
196 fprintf(stderr, "%s: ge0 fail: %s\n", __FUNCTION__, gnutls_strerror(ret));
197 return NULL;
198 }
199
200cp1 = store_get(siz*4 + 1);
201
202ret = gnutls_x509_crt_get_extension_by_oid ((gnutls_x509_crt_t)cert,
203 oid, idx, cp1, &siz, &crit);
204if (ret < 0)
205 {
206 fprintf(stderr, "%s: ge1 fail: %s\n", __FUNCTION__, gnutls_strerror(ret));
207 return NULL;
208 }
209
210/* binary data, DER encoded */
211
212/* just dump for now */
213for(cp3 = cp2 = cp1+siz; cp1 < cp2; cp3 += 3, cp1++)
214 sprintf(cp3, "%.2x ", *cp1);
215cp3[-1]= '\0';
216
217return cp2;
218}
219
220uschar *
221tls_cert_subject_altname(void * cert)
222{
223uschar * cp = NULL;
224size_t siz = 0;
225unsigned int crit;
226int ret;
227
228ret = gnutls_x509_crt_get_subject_alt_name ((gnutls_x509_crt_t)cert,
229 0, cp, &siz, &crit);
230switch(ret)
231 {
232 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
233 return NULL;
234 case GNUTLS_E_SHORT_MEMORY_BUFFER:
235 break;
236 default:
237 expand_string_message =
238 string_sprintf("%s: gs0 fail: %d %s\n", __FUNCTION__,
239 ret, gnutls_strerror(ret));
240 return NULL;
241 }
242
243cp = store_get(siz+1);
244ret = gnutls_x509_crt_get_subject_alt_name ((gnutls_x509_crt_t)cert,
245 0, cp, &siz, &crit);
246if (ret < 0)
247 {
248 expand_string_message =
249 string_sprintf("%s: gs1 fail: %d %s\n", __FUNCTION__,
250 ret, gnutls_strerror(ret));
251 return NULL;
252 }
253cp[siz] = '\0';
254return cp;
255}
256
257uschar *
258tls_cert_ocsp_uri(void * cert)
259{
260#if GNUTLS_VERSION_NUMBER >= 0x030000
261gnutls_datum_t uri;
262unsigned int crit;
263int ret = gnutls_x509_crt_get_authority_info_access((gnutls_x509_crt_t)cert,
264 0, GNUTLS_IA_OCSP_URI, &uri, &crit);
265
266if (ret >= 0)
267 return string_copyn(uri.data, uri.size);
268
269if (ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE)
270 expand_string_message =
271 string_sprintf("%s: gai fail: %d %s\n", __FUNCTION__,
272 ret, gnutls_strerror(ret));
273
274return NULL;
275
276#else
277
278expand_string_message =
279 string_sprintf("%s: OCSP support with GnuTLS requires version 3.0.0\n",
280 __FUNCTION__);
281return NULL;
282
283#endif
284}
285
286uschar *
287tls_cert_crl_uri(void * cert)
288{
289int ret;
290uschar * cp = NULL;
291size_t siz = 0;
292
293ret = gnutls_x509_crt_get_crl_dist_points ((gnutls_x509_crt_t)cert,
294 0, cp, &siz, NULL, NULL);
295switch(ret)
296 {
297 case GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE:
298 return NULL;
299 case GNUTLS_E_SHORT_MEMORY_BUFFER:
300 break;
301 default:
302 expand_string_message =
303 string_sprintf("%s: gc0 fail: %d %s\n", __FUNCTION__,
304 ret, gnutls_strerror(ret));
305 return NULL;
306 }
307
308cp = store_get(siz+1);
309ret = gnutls_x509_crt_get_crl_dist_points ((gnutls_x509_crt_t)cert,
310 0, cp, &siz, NULL, NULL);
311if (ret < 0)
312 {
313 expand_string_message =
314 string_sprintf("%s: gs1 fail: %d %s\n", __FUNCTION__,
315 ret, gnutls_strerror(ret));
316 return NULL;
317 }
318cp[siz] = '\0';
319return cp;
320}
321
322
323/* vi: aw ai sw=2
324*/
325/* End of tlscert-gnu.c */