Debian Codegraph @

deps/libneon/src/ne_gnutls.c

raw
1 /*
2 neon SSL/TLS support using GNU TLS
3 Copyright (C) 2002-2010, Joe Orton <[email protected]>
4 Copyright (C) 2004, Aleix Conchillo Flaque <[email protected]>
5
6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version.
10
11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
15
16 You should have received a copy of the GNU Library General Public
17 License along with this library; if not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
19 MA 02111-1307, USA
20 */
21
22 #include "config.h"
23
24 #include <sys/types.h>
25
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29
30 #include <stdio.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <errno.h>
34
35 #include <gnutls/gnutls.h>
36 #include <gnutls/pkcs12.h>
37
38 #ifdef NE_HAVE_TS_SSL
39 #include <errno.h>
40 #include <pthread.h>
41 #if LIBGNUTLS_VERSION_NUMBER < 0x020b01
42 #include <gcrypt.h>
43 GCRY_THREAD_OPTION_PTHREAD_IMPL;
44 #endif
45 #else
46 #if LIBGNUTLS_VERSION_NUMBER < 0x020b01
47 #include <gcrypt.h>
48 #endif
49 #endif
50
51 #ifdef HAVE_ICONV
52 #include <iconv.h>
53 #endif
54
55 #include "ne_ssl.h"
56 #include "ne_string.h"
57 #include "ne_session.h"
58 #include "ne_internal.h"
59
60 #include "ne_private.h"
61 #include "ne_privssl.h"
62
63 #if LIBGNUTLS_VERSION_NUMBER >= 0x020302
64 /* The GnuTLS DN functions in 2.3.2 and later allow a simpler DN
65 * abstraction to be used. */
66 #define HAVE_NEW_DN_API
67 #endif
68
69 struct ne_ssl_dname_s {
70 #ifdef HAVE_NEW_DN_API
71 gnutls_x509_dn_t dn;
72 #else
73 int subject; /* non-zero if this is the subject DN object */
74 gnutls_x509_crt cert;
75 #endif
76 };
77
78 struct ne_ssl_certificate_s {
79 ne_ssl_dname subj_dn, issuer_dn;
80 gnutls_x509_crt *chain;
81 unsigned chain_len;
82 ne_ssl_certificate *issuer;
83 char *identity;
84 };
85
86 struct ne_ssl_client_cert_s {
87 gnutls_pkcs12 p12;
88 int decrypted; /* non-zero if successfully decrypted. */
89 int keyless;
90 ne_ssl_certificate cert;
91 gnutls_x509_privkey pkey;
92 char *friendly_name;
93 };
94
95 /* Returns the highest used index in subject (or issuer) DN of
96 * certificate CERT for OID, or -1 if no RDNs are present in the DN
97 * using that OID. */
98 static int oid_find_highest_index(gnutls_x509_crt cert, int subject, const char *oid)
99 {
100 int ret, idx = -1;
101
102 do {
103 size_t len = 0;
104
105 if (subject)
106 ret = gnutls_x509_crt_get_dn_by_oid(cert, oid, ++idx, 0,
107 NULL, &len);
108 else
109 ret = gnutls_x509_crt_get_issuer_dn_by_oid(cert, oid, ++idx, 0,
110 NULL, &len);
111 } while (ret == GNUTLS_E_SHORT_MEMORY_BUFFER);
112
113 return idx - 1;
114 }
115
116 #ifdef HAVE_GNUTLS_X509_DN_GET_RDN_AVA
117 /* New-style RDN handling introduced in GnuTLS 1.7.x. */
118
119 #ifdef HAVE_ICONV
120 static void convert_dirstring(ne_buffer *buf, const char *charset,
121 gnutls_datum *data)
122 {
123 iconv_t id = iconv_open("UTF-8", charset);
124 size_t inlen = data->size, outlen = buf->length - buf->used;
125 char *inbuf = (char *)data->data;
126 char *outbuf = buf->data + buf->used - 1;
127
128 if (id == (iconv_t)-1) {
129 char err[128], err2[128];
130
131 ne_snprintf(err, sizeof err, "[unprintable in %s: %s]",
132 charset, ne_strerror(errno, err2, sizeof err2));
133 ne_buffer_zappend(buf, err);
134 return;
135 }
136
137 ne_buffer_grow(buf, buf->used + 64);
138
139 while (inlen && outlen
140 && iconv(id, &inbuf, &inlen, &outbuf, &outlen) == 0)
141 ;
142
143 iconv_close(id);
144 buf->used += buf->length - buf->used - outlen;
145 buf->data[buf->used - 1] = '\0';
146 }
147 #endif
148
149 /* From section 11.13 of the Dubuisson ASN.1 bible: */
150 #define TAG_UTF8 (12)
151 #define TAG_PRINTABLE (19)
152 #define TAG_T61 (20)
153 #define TAG_IA5 (22)
154 #define TAG_VISIBLE (26)
155 #define TAG_UNIVERSAL (28)
156 #define TAG_BMP (30)
157
158 static void append_dirstring(ne_buffer *buf, gnutls_datum *data, unsigned long tag)
159 {
160 switch (tag) {
161 case TAG_UTF8:
162 case TAG_IA5:
163 case TAG_PRINTABLE:
164 case TAG_VISIBLE:
165 ne_buffer_append(buf, (char *)data->data, data->size);
166 break;
167 #ifdef HAVE_ICONV
168 case TAG_T61:
169 convert_dirstring(buf, "ISO-8859-1", data);
170 break;
171 case TAG_BMP:
172 convert_dirstring(buf, "UCS-2BE", data);
173 break;
174 #endif
175 default: {
176 char tmp[128];
177 ne_snprintf(tmp, sizeof tmp, _("[unprintable:#%lu]"), tag);
178 ne_buffer_zappend(buf, tmp);
179 } break;
180 }
181 }
182
183 /* OIDs to not include in readable DNs by default: */
184 #define OID_emailAddress "1.2.840.113549.1.9.1"
185 #define OID_commonName "2.5.4.3"
186
187 #define CMPOID(a,o) ((a)->oid.size == sizeof(o) \
188 && memcmp((a)->oid.data, o, strlen(o)) == 0)
189
190 char *ne_ssl_readable_dname(const ne_ssl_dname *name)
191 {
192 gnutls_x509_dn_t dn;
193 int ret, rdn = 0, flag = 0;
194 ne_buffer *buf;
195 gnutls_x509_ava_st val;
196
197 #ifdef HAVE_NEW_DN_API
198 dn = name->dn;
199 #else
200 if (name->subject)
201 ret = gnutls_x509_crt_get_subject(name->cert, &dn);
202 else
203 ret = gnutls_x509_crt_get_issuer(name->cert, &dn);
204
205 if (ret)
206 return ne_strdup(_("[unprintable]"));
207 #endif /* HAVE_NEW_DN_API */
208
209 buf = ne_buffer_create();
210
211 /* Find the highest rdn... */
212 while (gnutls_x509_dn_get_rdn_ava(dn, rdn++, 0, &val) == 0)
213 ;
214
215 /* ..then iterate back to the first: */
216 while (--rdn >= 0) {
217 int ava = 0;
218
219 /* Iterate through all AVAs for multivalued AVAs; better than
220 * ne_openssl can do! */
221 do {
222 ret = gnutls_x509_dn_get_rdn_ava(dn, rdn, ava, &val);
223
224 /* If the *only* attribute to append is the common name or
225 * email address, use it; otherwise skip those
226 * attributes. */
227 if (ret == 0 && val.value.size > 0
228 && ((!CMPOID(&val, OID_emailAddress)
229 && !CMPOID(&val, OID_commonName))
230 || (buf->used == 1 && rdn == 0))) {
231 flag = 1;
232 if (buf->used > 1) ne_buffer_append(buf, ", ", 2);
233
234 append_dirstring(buf, &val.value, val.value_tag);
235 }
236
237 ava++;
238 } while (ret == 0);
239 }
240
241 return ne_buffer_finish(buf);
242 }
243
244 #else /* !HAVE_GNUTLS_X509_DN_GET_RDN_AVA */
245
246 /* Appends the value of RDN with given oid from certitifcate x5
247 * subject (if subject is non-zero), or issuer DN to buffer 'buf': */
248 static void append_rdn(ne_buffer *buf, gnutls_x509_crt x5, int subject, const char *oid)
249 {
250 int idx, top, ret;
251 char rdn[50];
252
253 top = oid_find_highest_index(x5, subject, oid);
254
255 for (idx = top; idx >= 0; idx--) {
256 size_t rdnlen = sizeof rdn;
257
258 if (subject)
259 ret = gnutls_x509_crt_get_dn_by_oid(x5, oid, idx, 0, rdn, &rdnlen);
260 else
261 ret = gnutls_x509_crt_get_issuer_dn_by_oid(x5, oid, idx, 0, rdn, &rdnlen);
262
263 if (ret < 0)
264 return;
265
266 if (buf->used > 1) {
267 ne_buffer_append(buf, ", ", 2);
268 }
269
270 ne_buffer_append(buf, rdn, rdnlen);
271 }
272 }
273
274 char *ne_ssl_readable_dname(const ne_ssl_dname *name)
275 {
276 ne_buffer *buf = ne_buffer_create();
277 int ret, idx = 0;
278
279 do {
280 char oid[32] = {0};
281 size_t oidlen = sizeof oid;
282
283 ret = name->subject
284 ? gnutls_x509_crt_get_dn_oid(name->cert, idx, oid, &oidlen)
285 : gnutls_x509_crt_get_issuer_dn_oid(name->cert, idx, oid, &oidlen);
286
287 if (ret == 0) {
288 append_rdn(buf, name->cert, name->subject, oid);
289 idx++;
290 }
291 } while (ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
292
293 return ne_buffer_finish(buf);
294 }
295 #endif /* HAVE_GNUTLS_X509_DN_GET_RDN_AVA */
296
297 int ne_ssl_dname_cmp(const ne_ssl_dname *dn1, const ne_ssl_dname *dn2)
298 {
299 char c1[1024], c2[1024];
300 size_t s1 = sizeof c1, s2 = sizeof c2;
301
302 #ifdef HAVE_NEW_DN_API
303 if (gnutls_x509_dn_export(dn1->dn, GNUTLS_X509_FMT_DER, c1, &s1))
304 return 1;
305
306 if (gnutls_x509_dn_export(dn2->dn, GNUTLS_X509_FMT_DER, c2, &s2))
307 return -1;
308 #else
309 int ret;
310
311 if (dn1->subject)
312 ret = gnutls_x509_crt_get_dn(dn1->cert, c1, &s1);
313 else
314 ret = gnutls_x509_crt_get_issuer_dn(dn1->cert, c1, &s1);
315 if (ret)
316 return 1;
317
318 if (dn2->subject)
319 ret = gnutls_x509_crt_get_dn(dn2->cert, c2, &s2);
320 else
321 ret = gnutls_x509_crt_get_issuer_dn(dn2->cert, c2, &s2);
322 if (ret)
323 return -1;
324 #endif /* HAVE_NEW_DN_API */
325
326 if (s1 != s2)
327 return s2 - s1;
328
329 return memcmp(c1, c2, s1);
330 }
331
332 void ne_ssl_clicert_free(ne_ssl_client_cert *cc)
333 {
334 if (cc->p12)
335 gnutls_pkcs12_deinit(cc->p12);
336 if (cc->decrypted) {
337 unsigned n;
338 if (cc->cert.identity) ne_free(cc->cert.identity);
339 if (cc->pkey) gnutls_x509_privkey_deinit(cc->pkey);
340
341 for (n = 0; n < cc->cert.chain_len; ++n)
342 gnutls_x509_crt_deinit(cc->cert.chain[n]);
343 gnutls_free(cc->cert.chain);
344 }
345 if (cc->friendly_name) ne_free(cc->friendly_name);
346 ne_free(cc);
347 }
348
349 void ne_ssl_cert_validity_time(const ne_ssl_certificate *cert,
350 time_t *from, time_t *until)
351 {
352 if (from) {
353 *from = gnutls_x509_crt_get_activation_time(cert->chain[0]);
354 }
355 if (until) {
356 *until = gnutls_x509_crt_get_expiration_time(cert->chain[0]);
357 }
358 }
359
360 /* Check certificate identity. Returns zero if identity matches; 1 if
361 * identity does not match, or <0 if the certificate had no identity.
362 * If 'identity' is non-NULL, store the malloc-allocated identity in
363 * *identity. If 'server' is non-NULL, it must be the network address
364 * of the server in use, and identity must be NULL. */
365 static int check_identity(const ne_uri *server, gnutls_x509_crt cert,
366 char **identity)
367 {
368 char name[255];
369 unsigned int critical;
370 int ret, seq = 0;
371 int match = 0, found = 0;
372 size_t len;
373 const char *hostname;
374
375 hostname = server ? server->host : "";
376
377 do {
378 len = sizeof name - 1;
379 ret = gnutls_x509_crt_get_subject_alt_name(cert, seq, name, &len,
380 &critical);
381 switch (ret) {
382 case GNUTLS_SAN_DNSNAME:
383 name[len] = '\0';
384 if (identity && !found) *identity = ne_strdup(name);
385 match = ne__ssl_match_hostname(name, len, hostname);
386 found = 1;
387 break;
388 case GNUTLS_SAN_IPADDRESS: {
389 ne_inet_addr *ia;
390 if (len == 4)
391 ia = ne_iaddr_make(ne_iaddr_ipv4, (unsigned char *)name);
392 else if (len == 16)
393 ia = ne_iaddr_make(ne_iaddr_ipv6, (unsigned char *)name);
394 else
395 ia = NULL;
396 if (ia) {
397 char buf[128];
398
399 match = strcmp(hostname,
400 ne_iaddr_print(ia, buf, sizeof buf)) == 0;
401 if (identity) *identity = ne_strdup(buf);
402 found = 1;
403 ne_iaddr_free(ia);
404 } else {
405 NE_DEBUG(NE_DBG_SSL, "iPAddress name with unsupported "
406 "address type (length %" NE_FMT_SIZE_T "), skipped.\n",
407 len);
408 }
409 } break;
410 case GNUTLS_SAN_URI: {
411 ne_uri uri;
412
413 name[len] = '\0';
414
415 if (ne_uri_parse(name, &uri) == 0 && uri.host && uri.scheme) {
416 ne_uri tmp;
417
418 if (identity && !found) *identity = ne_strdup(name);
419 found = 1;
420
421 if (server) {
422 /* For comparison purposes, all that matters is
423 * host, scheme and port; ignore the rest. */
424 memset(&tmp, 0, sizeof tmp);
425 tmp.host = uri.host;
426 tmp.scheme = uri.scheme;
427 tmp.port = uri.port;
428
429 match = ne_uri_cmp(server, &tmp) == 0;
430 }
431 }
432
433 ne_uri_free(&uri);
434 } break;
435
436 default:
437 break;
438 }
439 seq++;
440 } while (!match && ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE);
441
442 /* Check against the commonName if no DNS alt. names were found,
443 * as per RFC3280. */
444 if (!found) {
445 seq = oid_find_highest_index(cert, 1, GNUTLS_OID_X520_COMMON_NAME);
446
447 if (seq >= 0) {
448 len = sizeof name;
449 name[0] = '\0';
450 ret = gnutls_x509_crt_get_dn_by_oid(cert, GNUTLS_OID_X520_COMMON_NAME,
451 seq, 0, name, &len);
452 if (ret == 0) {
453 if (identity) *identity = ne_strdup(name);
454 match = ne__ssl_match_hostname(name, len, hostname);
455 }
456 } else {
457 return -1;
458 }
459 }
460
461 if (*hostname)
462 NE_DEBUG(NE_DBG_SSL, "ssl: Identity match for '%s': %s", hostname,
463 match ? "good" : "bad");
464
465 return match ? 0 : 1;
466 }
467
468 /* Populate an ne_ssl_certificate structure from an X509 object. Note
469 * that x5 is owned by returned cert object and must not be otherwise
470 * freed by the caller. */
471 static ne_ssl_certificate *populate_cert(ne_ssl_certificate *cert,
472 gnutls_x509_crt x5)
473 {
474 #ifdef HAVE_NEW_DN_API
475 gnutls_x509_crt_get_subject(x5, &cert->subj_dn.dn);
476 gnutls_x509_crt_get_issuer(x5, &cert->issuer_dn.dn);
477 #else
478 cert->subj_dn.cert = x5;
479 cert->subj_dn.subject = 1;
480 cert->issuer_dn.cert = x5;
481 cert->issuer_dn.subject = 0;
482 #endif
483 cert->issuer = NULL;
484 cert->chain_len = 1;
485 cert->chain = gnutls_malloc(sizeof(gnutls_x509_crt));
486 cert->chain[0] = x5;
487 cert->identity = NULL;
488 check_identity(NULL, x5, &cert->identity);
489 return cert;
490 }
491
492 /* Returns a copy certificate of certificate SRC. */
493 static gnutls_x509_crt x509_crt_copy(gnutls_x509_crt src)
494 {
495 int ret;
496 size_t size;
497 gnutls_datum tmp;
498 gnutls_x509_crt dest;
499
500 if (gnutls_x509_crt_init(&dest) != 0) {
501 return NULL;
502 }
503
504 if (gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, NULL, &size)
505 != GNUTLS_E_SHORT_MEMORY_BUFFER) {
506 gnutls_x509_crt_deinit(dest);
507 return NULL;
508 }
509
510 tmp.data = ne_malloc(size);
511 ret = gnutls_x509_crt_export(src, GNUTLS_X509_FMT_DER, tmp.data, &size);
512 if (ret == 0) {
513 tmp.size = size;
514 ret = gnutls_x509_crt_import(dest, &tmp, GNUTLS_X509_FMT_DER);
515 }
516
517 if (ret) {
518 gnutls_x509_crt_deinit(dest);
519 dest = NULL;
520 }
521
522 ne_free(tmp.data);
523 return dest;
524 }
525
526
527
528 /* Duplicate a client certificate, which must be in the decrypted state. */
529 ne_ssl_client_cert *dup_client_cert(const ne_ssl_client_cert *cc)
530 {
531 int ret, n;
532 ne_ssl_client_cert *newcc = ne_calloc(sizeof *newcc);
533
534 newcc->decrypted = 1;
535
536 if (cc->keyless) {
537 newcc->keyless = 1;
538 }
539 else {
540 ret = gnutls_x509_privkey_init(&newcc->pkey);
541 if (ret != 0) goto dup_error;
542
543 ret = gnutls_x509_privkey_cpy(newcc->pkey, cc->pkey);
544 if (ret != 0) goto dup_error;
545 }
546
547 newcc->cert.chain = gnutls_malloc(sizeof(gnutls_x509_crt_t) * cc->cert.chain_len);
548 memset(newcc->cert.chain, 0, sizeof(gnutls_x509_crt_t) * cc->cert.chain_len);
549 newcc->cert.chain_len = cc->cert.chain_len;
550 for (n = 0; n < cc->cert.chain_len; ++n) {
551 newcc->cert.chain[n] = x509_crt_copy(cc->cert.chain[n]);
552 if (!newcc->cert.chain[n]) goto dup_error;
553 }
554
555 return newcc;
556
557 dup_error:
558 if (newcc->pkey) gnutls_x509_privkey_deinit(newcc->pkey);
559 if (newcc->cert.chain) {
560 for (n = 0; n < cc->cert.chain_len; ++n)
561 gnutls_x509_crt_deinit(newcc->cert.chain[n]);
562 gnutls_free(newcc->cert.chain);
563 }
564 ne_free(newcc);
565 return NULL;
566 }
567
568 ne_ssl_client_cert * ne_ssl_dup_client_cert(const ne_ssl_client_cert *cc)
569 {
570 return dup_client_cert(cc);
571 }
572
573 /* Callback invoked when the SSL server requests a client certificate. */
574 static int provide_client_cert(gnutls_session session,
575 const gnutls_datum *req_ca_rdn, int nreqs,
576 const gnutls_pk_algorithm *sign_algos,
577 int sign_algos_length, gnutls_retr_st *st)
578 {
579 ne_session *sess = gnutls_session_get_ptr(session);
580
581 if (!sess) {
582 return GNUTLS_E_RECEIVED_ILLEGAL_PARAMETER;
583 }
584
585 NE_DEBUG(NE_DBG_SSL, "ssl: Client cert provider callback; %d CA names.",
586 nreqs);
587
588 if (!sess->client_cert && sess->ssl_provide_fn) {
589 #ifdef HAVE_NEW_DN_API
590 const ne_ssl_dname **dns;
591 ne_ssl_dname *dnarray;
592 unsigned dncount = 0;
593 int n;
594
595 dns = ne_malloc(nreqs * sizeof(ne_ssl_dname *));
596 dnarray = ne_calloc(nreqs * sizeof(ne_ssl_dname));
597
598 for (n = 0; n < nreqs; n++) {
599 gnutls_x509_dn_t dn;
600
601 if (gnutls_x509_dn_init(&dn) == 0) {
602 dnarray[n].dn = dn;
603 if (gnutls_x509_dn_import(dn, &req_ca_rdn[n]) == 0) {
604 dns[dncount++] = &dnarray[n];
605 }
606 else {
607 gnutls_x509_dn_deinit(dn);
608 }
609 }
610 }
611
612 NE_DEBUG(NE_DBG_SSL, "ssl: Mapped %d CA names to %u DN objects.",
613 nreqs, dncount);
614
615 sess->ssl_provide_fn(sess->ssl_provide_ud, sess, dns, dncount);
616
617 for (n = 0; n < nreqs; n++) {
618 if (dnarray[n].dn) {
619 gnutls_x509_dn_deinit(dnarray[n].dn);
620 }
621 }
622
623 ne_free(dns);
624 ne_free(dnarray);
625 #else /* HAVE_NEW_DN_API */
626 /* Nothing to do here other than pretend no CA names were
627 * given, and hope the caller can cope. */
628 sess->ssl_provide_fn(sess->ssl_provide_ud, sess, NULL, 0);
629 #endif
630 }
631
632 if (sess->client_cert) {
633 gnutls_certificate_type type = gnutls_certificate_type_get(session);
634 if (type == GNUTLS_CRT_X509) {
635 NE_DEBUG(NE_DBG_SSL, "Supplying client certificate.");
636
637 st->type = type;
638 st->ncerts = sess->client_cert->cert.chain_len;;
639 st->cert.x509 = sess->client_cert->cert.chain;
640 st->key.x509 = sess->client_cert->pkey;
641
642 /* tell GNU TLS not to deallocate the certs. */
643 st->deinit_all = 0;
644 } else {
645 return GNUTLS_E_UNSUPPORTED_CERTIFICATE_TYPE;
646 }
647 }
648 else {
649 NE_DEBUG(NE_DBG_SSL, "No client certificate supplied.");
650 st->ncerts = 0;
651 sess->ssl_cc_requested = 1;
652 return 0;
653 }
654
655 return 0;
656 }
657
658 void ne_ssl_set_clicert(ne_session *sess, const ne_ssl_client_cert *cc)
659 {
660 sess->client_cert = dup_client_cert(cc);
661 }
662
663 ne_ssl_context *ne_ssl_context_create(int flags)
664 {
665 ne_ssl_context *ctx = ne_calloc(sizeof *ctx);
666 gnutls_certificate_allocate_credentials(&ctx->cred);
667 if (flags == NE_SSL_CTX_CLIENT) {
668 gnutls_certificate_client_set_retrieve_function(ctx->cred,
669 provide_client_cert);
670 }
671 gnutls_certificate_set_verify_flags(ctx->cred,
672 GNUTLS_VERIFY_ALLOW_X509_V1_CA_CRT);
673 return ctx;
674 }
675
676 int ne_ssl_context_keypair(ne_ssl_context *ctx,
677 const char *cert, const char *key)
678 {
679 gnutls_certificate_set_x509_key_file(ctx->cred, cert, key,
680 GNUTLS_X509_FMT_PEM);
681 return 0;
682 }
683
684 int ne_ssl_context_set_verify(ne_ssl_context *ctx, int required,
685 const char *ca_names, const char *verify_cas)
686 {
687 ctx->verify = required;
688 if (verify_cas) {
689 gnutls_certificate_set_x509_trust_file(ctx->cred, verify_cas,
690 GNUTLS_X509_FMT_PEM);
691 }
692 /* gnutls_certificate_send_x509_rdn_sequence in gnutls >= 1.2 can
693 * be used to *suppress* sending the CA names, but not control it,
694 * it seems. */
695 return 0;
696 }
697
698 void ne_ssl_context_set_flag(ne_ssl_context *ctx, int flag, int value)
699 {
700 /* SSLv2 not supported. */
701 }
702
703 void ne_ssl_context_destroy(ne_ssl_context *ctx)
704 {
705 gnutls_certificate_free_credentials(ctx->cred);
706 if (ctx->cache.client.data) {
707 ne_free(ctx->cache.client.data);
708 } else if (ctx->cache.server.key.data) {
709 gnutls_free(ctx->cache.server.key.data);
710 gnutls_free(ctx->cache.server.data.data);
711 }
712 ne_free(ctx);
713 }
714
715 #ifdef HAVE_GNUTLS_CERTIFICATE_GET_X509_CAS
716 /* Return the issuer of the given certificate, or NULL if none can be
717 * found. */
718 static gnutls_x509_crt find_issuer(gnutls_x509_crt *ca_list,
719 unsigned int num_cas,
720 gnutls_x509_crt cert)
721 {
722 unsigned int n;
723
724 for (n = 0; n < num_cas; n++) {
725 if (gnutls_x509_crt_check_issuer(cert, ca_list[n]) == 1)
726 return ca_list[n];
727 }
728
729 return NULL;
730 }
731 #endif
732
733 /* Return the certificate chain sent by the peer, or NULL on error. */
734 static ne_ssl_certificate *make_peers_chain(gnutls_session sock,
735 gnutls_certificate_credentials crd)
736 {
737 ne_ssl_certificate *current = NULL, *top = NULL;
738 const gnutls_datum *certs;
739 unsigned int n, count;
740 ne_ssl_certificate *cert;
741
742 certs = gnutls_certificate_get_peers(sock, &count);
743 if (!certs) {
744 return NULL;
745 }
746
747 NE_DEBUG(NE_DBG_SSL, "ssl: Got %u certs in peer chain.", count);
748
749 for (n = 0; n < count; n++) {
750 gnutls_x509_crt x5;
751
752 if (gnutls_x509_crt_init(&x5) ||
753 gnutls_x509_crt_import(x5, &certs[n], GNUTLS_X509_FMT_DER)) {
754 if (top) {
755 ne_ssl_cert_free(top);
756 }
757 return NULL;
758 }
759
760 cert = populate_cert(ne_calloc(sizeof *cert), x5);
761
762 if (top == NULL) {
763 current = top = cert;
764 } else {
765 current->issuer = cert;
766 current = cert;
767 }
768 }
769
770 #ifdef HAVE_GNUTLS_CERTIFICATE_GET_X509_CAS
771 /* GnuTLS only returns the peers which were *sent* by the server
772 * in the Certificate list during the handshake. Fill in the
773 * complete chain manually against the certs we trust: */
774 if (current->issuer == NULL) {
775 gnutls_x509_crt issuer;
776 gnutls_x509_crt *ca_list;
777 unsigned int num_cas;
778
779 gnutls_certificate_get_x509_cas(crd, &ca_list, &num_cas);
780
781 do {
782 /* Look up the issuer. */
783 issuer = find_issuer(ca_list, num_cas, current->chain[0]);
784 if (issuer) {
785 issuer = x509_crt_copy(issuer);
786 cert = populate_cert(ne_calloc(sizeof *cert), issuer);
787 /* Check that the issuer does not match the current
788 * cert. */
789 if (ne_ssl_cert_cmp(current, cert)) {
790 current = current->issuer = cert;
791 }
792 else {
793 ne_ssl_cert_free(cert);
794 issuer = NULL;
795 }
796 }
797 } while (issuer);
798 }
799 #endif
800
801 return top;
802 }
803
804 /* Map from GnuTLS verify failure mask *status to NE_SSL_* failure
805 * bitmask, which is returned. *status is modified, removing all
806 * mapped bits. */
807 static int map_verify_failures(unsigned int *status)
808 {
809 static const struct {
810 gnutls_certificate_status_t from;
811 int to;
812 } map[] = {
813 { GNUTLS_CERT_REVOKED, NE_SSL_REVOKED },
814 #if LIBGNUTLS_VERSION_NUMBER >= 0x020800
815 { GNUTLS_CERT_NOT_ACTIVATED, NE_SSL_NOTYETVALID },
816 { GNUTLS_CERT_EXPIRED, NE_SSL_EXPIRED },
817 #endif
818 { GNUTLS_CERT_INVALID|GNUTLS_CERT_SIGNER_NOT_FOUND, NE_SSL_UNTRUSTED },
819 { GNUTLS_CERT_INVALID|GNUTLS_CERT_SIGNER_NOT_CA, NE_SSL_UNTRUSTED }
820 };
821 size_t n;
822 int ret = 0;
823
824 for (n = 0; n < sizeof(map)/sizeof(map[0]); n++) {
825 if ((*status & map[n].from) == map[n].from) {
826 *status &= ~map[n].from;
827 ret |= map[n].to;
828 }
829 }
830
831 return ret;
832 }
833
834 /* Return a malloc-allocated human-readable error string describing
835 * GnuTLS verification error bitmask 'status'; return value must be
836 * freed by the caller. */
837 static char *verify_error_string(unsigned int status)
838 {
839 ne_buffer *buf = ne_buffer_create();
840
841 /* sorry, i18n-ers */
842 if (status & GNUTLS_CERT_INSECURE_ALGORITHM) {
843 ne_buffer_zappend(buf, _("signed using insecure algorithm"));
844 }
845 else {
846 ne_buffer_snprintf(buf, 64, _("unrecognized errors (%u)"),
847 status);
848 }
849
850 return ne_buffer_finish(buf);
851 }
852
853 /* Return NE_SSL_* failure bits after checking chain expiry. */
854 static int check_chain_expiry(ne_ssl_certificate *chain)
855 {
856 time_t before, after, now = time(NULL);
857 ne_ssl_certificate *cert;
858 int failures = 0;
859
860 /* Check that all certs within the chain are inside their defined
861 * validity period. Note that the errors flagged for the server
862 * cert are different from the generic error for issues higher up
863 * the chain. */
864 for (cert = chain; cert; cert = cert->issuer) {
865 before = gnutls_x509_crt_get_activation_time(cert->chain[0]);
866 after = gnutls_x509_crt_get_expiration_time(cert->chain[0]);
867
868 if (now < before)
869 failures |= (cert == chain) ? NE_SSL_NOTYETVALID : NE_SSL_BADCHAIN;
870 else if (now > after)
871 failures |= (cert == chain) ? NE_SSL_EXPIRED : NE_SSL_BADCHAIN;
872 }
873
874 return failures;
875 }
876
877 /* Verifies an SSL server certificate. */
878 static int check_certificate(ne_session *sess, gnutls_session sock,
879 ne_ssl_certificate *chain)
880 {
881 int ret, failures = 0;
882 ne_uri server;
883 unsigned int status;
884
885 memset(&server, 0, sizeof server);
886 ne_fill_server_uri(sess, &server);
887 ret = check_identity(&server, chain->chain[0], NULL);
888 ne_uri_free(&server);
889
890 if (ret < 0) {
891 ne_set_error(sess, _("Server certificate was missing commonName "
892 "attribute in subject name"));
893 return NE_ERROR;
894 }
895 else if (ret > 0) {
896 failures |= NE_SSL_IDMISMATCH;
897 }
898
899 failures |= check_chain_expiry(chain);
900
901 ret = gnutls_certificate_verify_peers2(sock, &status);
902 NE_DEBUG(NE_DBG_SSL, "ssl: Verify peers returned %d, status=%u",
903 ret, status);
904 if (ret != GNUTLS_E_SUCCESS) {
905 ne_set_error(sess, _("Could not verify server certificate: %s"),
906 gnutls_strerror(ret));
907 return NE_ERROR;
908 }
909
910 failures |= map_verify_failures(&status);
911
912 NE_DEBUG(NE_DBG_SSL, "ssl: Verification failures = %d (status = %u).",
913 failures, status);
914
915 if (status && status != GNUTLS_CERT_INVALID) {
916 char *errstr = verify_error_string(status);
917 ne_set_error(sess, _("Certificate verification error: %s"), errstr);
918 ne_free(errstr);
919 return NE_ERROR;
920 }
921
922 if (failures == 0) {
923 ret = NE_OK;
924 } else {
925 ne__ssl_set_verify_err(sess, failures);
926 ret = NE_ERROR;
927 if (sess->ssl_verify_fn
928 && sess->ssl_verify_fn(sess->ssl_verify_ud, failures, chain) == 0)
929 ret = NE_OK;
930 }
931
932 return ret;
933 }
934
935 /* Negotiate an SSL connection. */
936 int ne__negotiate_ssl(ne_session *sess)
937 {
938 ne_ssl_context *const ctx = sess->ssl_context;
939 ne_ssl_certificate *chain;
940 gnutls_session sock;
941
942 NE_DEBUG(NE_DBG_SSL, "Negotiating SSL connection.");
943
944 /* Pass through the hostname if SNI is enabled. */
945 ctx->hostname =
946 sess->flags[NE_SESSFLAG_TLS_SNI] ? sess->server.hostname : NULL;
947
948 if (ne_sock_connect_ssl(sess->socket, ctx, sess)) {
949 if (sess->ssl_cc_requested) {
950 ne_set_error(sess, _("SSL handshake failed, "
951 "client certificate was requested: %s"),
952 ne_sock_error(sess->socket));
953 }
954 else {
955 ne_set_error(sess, _("SSL handshake failed: %s"),
956 ne_sock_error(sess->socket));
957 }
958 return NE_ERROR;
959 }
960
961 sock = ne__sock_sslsock(sess->socket);
962
963 chain = make_peers_chain(sock, ctx->cred);
964 if (chain == NULL) {
965 ne_set_error(sess, _("Server did not send certificate chain"));
966 return NE_ERROR;
967 }
968
969 if (sess->server_cert && ne_ssl_cert_cmp(sess->server_cert, chain) == 0) {
970 /* Same cert as last time; presume OK. This is not optimal as
971 * make_peers_chain() has already gone through and done the
972 * expensive DER parsing stuff for the whole chain by now. */
973 ne_ssl_cert_free(chain);
974 return NE_OK;
975 }
976
977 if (check_certificate(sess, sock, chain)) {
978 ne_ssl_cert_free(chain);
979 return NE_ERROR;
980 }
981
982 sess->server_cert = chain;
983
984 return NE_OK;
985 }
986
987 const ne_ssl_dname *ne_ssl_cert_issuer(const ne_ssl_certificate *cert)
988 {
989 return &cert->issuer_dn;
990 }
991
992 const ne_ssl_dname *ne_ssl_cert_subject(const ne_ssl_certificate *cert)
993 {
994 return &cert->subj_dn;
995 }
996
997 const ne_ssl_certificate *ne_ssl_cert_signedby(const ne_ssl_certificate *cert)
998 {
999 return cert->issuer;
1000 }
1001
1002 const char *ne_ssl_cert_identity(const ne_ssl_certificate *cert)
1003 {
1004 return cert->identity;
1005 }
1006
1007 void ne_ssl_context_trustcert(ne_ssl_context *ctx, const ne_ssl_certificate *cert)
1008 {
1009 gnutls_x509_crt certs = cert->chain[0];
1010 gnutls_certificate_set_x509_trust(ctx->cred, &certs, 1);
1011 }
1012
1013 void ne_ssl_trust_default_ca(ne_session *sess)
1014 {
1015 #ifdef NE_SSL_CA_BUNDLE
1016 gnutls_certificate_set_x509_trust_file(sess->ssl_context->cred,
1017 NE_SSL_CA_BUNDLE,
1018 GNUTLS_X509_FMT_PEM);
1019 #endif
1020 }
1021
1022 /* Read the contents of file FILENAME into *DATUM. */
1023 static int read_to_datum(const char *filename, gnutls_datum *datum)
1024 {
1025 FILE *f = fopen(filename, "r");
1026 ne_buffer *buf;
1027 char tmp[4192];
1028 size_t len;
1029
1030 if (!f) {
1031 return -1;
1032 }
1033
1034 buf = ne_buffer_ncreate(8192);
1035 while ((len = fread(tmp, 1, sizeof tmp, f)) > 0) {
1036 ne_buffer_append(buf, tmp, len);
1037 }
1038
1039 if (!feof(f)) {
1040 fclose(f);
1041 ne_buffer_destroy(buf);
1042 return -1;
1043 }
1044
1045 fclose(f);
1046
1047 datum->size = ne_buffer_size(buf);
1048 datum->data = (unsigned char *)ne_buffer_finish(buf);
1049 return 0;
1050 }
1051
1052 /* Parses a PKCS#12 structure and loads the certificate, private key
1053 * and friendly name if possible. Returns zero on success, non-zero
1054 * on error. */
1055 static int pkcs12_parse(gnutls_pkcs12 p12, gnutls_x509_privkey *pkey,
1056 gnutls_x509_crt **chain, int *chain_len,
1057 char **friendly_name,
1058 const char *password)
1059 {
1060 gnutls_pkcs12_bag bag = NULL;
1061 int i, j, ret = 0;
1062 gnutls_x509_crt certs[10];
1063 int ncerts = 0;
1064
1065 for (i = 0; ret == 0; ++i) {
1066 if (bag) gnutls_pkcs12_bag_deinit(bag);
1067
1068 ret = gnutls_pkcs12_bag_init(&bag);
1069 if (ret < 0) continue;
1070
1071 ret = gnutls_pkcs12_get_bag(p12, i, bag);
1072 if (ret < 0) continue;
1073
1074 gnutls_pkcs12_bag_decrypt(bag, password);
1075
1076 for (j = 0; ret == 0 && j < gnutls_pkcs12_bag_get_count(bag); ++j) {
1077 gnutls_pkcs12_bag_type type;
1078 gnutls_datum data;
1079
1080 if (friendly_name && *friendly_name == NULL) {
1081 char *name = NULL;
1082 gnutls_pkcs12_bag_get_friendly_name(bag, j, &name);
1083 if (name) {
1084 if (name[0] == '.') name++; /* weird GnuTLS bug? */
1085 *friendly_name = ne_strdup(name);
1086 }
1087 }
1088
1089 type = gnutls_pkcs12_bag_get_type(bag, j);
1090 switch (type) {
1091 case GNUTLS_BAG_PKCS8_KEY:
1092 case GNUTLS_BAG_PKCS8_ENCRYPTED_KEY:
1093 /* Ignore any but the first key encountered; really
1094 * need to match up keyids. */
1095 if (*pkey) break;
1096
1097 gnutls_x509_privkey_init(pkey);
1098
1099 ret = gnutls_pkcs12_bag_get_data(bag, j, &data);
1100 if (ret < 0) continue;
1101
1102 ret = gnutls_x509_privkey_import_pkcs8(*pkey, &data,
1103 GNUTLS_X509_FMT_DER,
1104 password,
1105 0);
1106 if (ret < 0) continue;
1107 break;
1108 case GNUTLS_BAG_CERTIFICATE:
1109 if (ncerts >= 10) continue;
1110
1111 ret = gnutls_x509_crt_init(&certs[ncerts]);
1112 if (ret < 0) continue;
1113
1114 ret = gnutls_pkcs12_bag_get_data(bag, j, &data);
1115 if (ret < 0) continue;
1116
1117 ret = gnutls_x509_crt_import(certs[ncerts], &data, GNUTLS_X509_FMT_DER);
1118 if (ret < 0) continue;
1119 ++ncerts;
1120
1121 break;
1122 default:
1123 break;
1124 }
1125 }
1126 }
1127
1128 /* Make sure last bag is freed */
1129 if (bag) gnutls_pkcs12_bag_deinit(bag);
1130
1131 /* Free in case of error */
1132 if (ret < 0 && ret != GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) {
1133 for (i = 0; i < ncerts; ++i)
1134 gnutls_x509_crt_deinit(certs[i]);
1135 if (*pkey) gnutls_x509_privkey_deinit(*pkey);
1136 if (friendly_name && *friendly_name) ne_free(*friendly_name);
1137 }
1138 else {
1139 /* Copy to chain */
1140 *chain_len = ncerts;
1141 *chain = gnutls_malloc(sizeof(gnutls_x509_crt_t) * ncerts);
1142 for (i = 0; i < ncerts; ++i)
1143 (*chain)[i] = certs[i];
1144 }
1145
1146 if (ret == GNUTLS_E_REQUESTED_DATA_NOT_AVAILABLE) ret = 0;
1147 return ret;
1148 }
1149
1150 ne_ssl_client_cert *ne_ssl_clicert_read(const char *filename)
1151 {
1152 int ret;
1153 gnutls_datum data;
1154 gnutls_pkcs12 p12;
1155 ne_ssl_client_cert *cc;
1156 char *friendly_name = NULL;
1157 gnutls_x509_crt* chain = NULL;
1158 unsigned chain_len = 0;
1159 gnutls_x509_privkey pkey = NULL;
1160
1161 if (read_to_datum(filename, &data))
1162 return NULL;
1163
1164 if (gnutls_pkcs12_init(&p12) != 0) {
1165 return NULL;
1166 }
1167
1168 ret = gnutls_pkcs12_import(p12, &data, GNUTLS_X509_FMT_DER, 0);
1169 ne_free(data.data);
1170 if (ret < 0) {
1171 gnutls_pkcs12_deinit(p12);
1172 return NULL;
1173 }
1174
1175 if (gnutls_pkcs12_verify_mac(p12, "") == 0) {
1176 if (pkcs12_parse(p12, &pkey, &chain, &chain_len, &friendly_name, "") != 0
1177 || !chain || !pkey) {
1178 gnutls_pkcs12_deinit(p12);
1179 return NULL;
1180 }
1181
1182 cc = ne_calloc(sizeof *cc);
1183 cc->pkey = pkey;
1184 cc->decrypted = 1;
1185 cc->friendly_name = friendly_name;
1186 cc->cert.chain = chain;
1187 cc->cert.chain_len = chain_len;
1188 gnutls_pkcs12_deinit(p12);
1189 cc->p12 = NULL;
1190 return cc;
1191 } else {
1192 /* TODO: calling pkcs12_parse() here to find the friendly_name
1193 * seems to break horribly. */
1194 cc = ne_calloc(sizeof *cc);
1195 cc->p12 = p12;
1196 return cc;
1197 }
1198 }
1199
1200 ne_ssl_client_cert *ne__ssl_clicert_exkey_import(const unsigned char *der,
1201 size_t der_len)
1202 {
1203 ne_ssl_client_cert *cc;
1204 gnutls_x509_crt x5;
1205 gnutls_datum datum;
1206
1207 datum.data = (unsigned char *)der;
1208 datum.size = der_len;
1209
1210 if (gnutls_x509_crt_init(&x5)
1211 || gnutls_x509_crt_import(x5, &datum, GNUTLS_X509_FMT_DER)) {
1212 NE_DEBUG(NE_DBG_SSL, "ssl: crt_import failed.");
1213 return NULL;
1214 }
1215
1216 cc = ne_calloc(sizeof *cc);
1217 cc->keyless = 1;
1218 cc->decrypted = 1;
1219 populate_cert(&cc->cert, x5);
1220
1221 return cc;
1222 }
1223
1224 int ne_ssl_clicert_encrypted(const ne_ssl_client_cert *cc)
1225 {
1226 return !cc->decrypted;
1227 }
1228
1229 int ne_ssl_clicert_decrypt(ne_ssl_client_cert *cc, const char *password)
1230 {
1231 int ret;
1232 gnutls_x509_crt *chain = NULL;
1233 int chain_len = 0;
1234 gnutls_x509_privkey pkey = NULL;
1235
1236 if (gnutls_pkcs12_verify_mac(cc->p12, password) != 0) {
1237 return -1;
1238 }
1239
1240 ret = pkcs12_parse(cc->p12, &pkey, &chain, &chain_len, NULL, password);
1241 if (ret < 0)
1242 return ret;
1243
1244 if (!chain || (!pkey && !cc->keyless)) {
1245 int n;
1246 for (n = 0; n < chain_len; ++n)
1247 gnutls_x509_crt_deinit(chain[0]);
1248 gnutls_free(chain);
1249 if (pkey) gnutls_x509_privkey_deinit(pkey);
1250 return -1;
1251 }
1252
1253 gnutls_pkcs12_deinit(cc->p12);
1254 cc->cert.chain = chain;
1255 cc->cert.chain_len = chain_len;
1256 cc->pkey = pkey;
1257 cc->decrypted = 1;
1258 cc->p12 = NULL;
1259 return 0;
1260 }
1261
1262 const ne_ssl_certificate *ne_ssl_clicert_owner(const ne_ssl_client_cert *cc)
1263 {
1264 return &cc->cert;
1265 }
1266
1267 const char *ne_ssl_clicert_name(const ne_ssl_client_cert *ccert)
1268 {
1269 return ccert->friendly_name;
1270 }
1271
1272 ne_ssl_certificate *ne_ssl_cert_read(const char *filename)
1273 {
1274 int ret;
1275 gnutls_datum data;
1276 gnutls_x509_crt x5;
1277
1278 if (read_to_datum(filename, &data))
1279 return NULL;
1280
1281 if (gnutls_x509_crt_init(&x5) != 0)
1282 return NULL;
1283
1284 ret = gnutls_x509_crt_import(x5, &data, GNUTLS_X509_FMT_PEM);
1285 ne_free(data.data);
1286 if (ret < 0) {
1287 gnutls_x509_crt_deinit(x5);
1288 return NULL;
1289 }
1290
1291 return populate_cert(ne_calloc(sizeof(struct ne_ssl_certificate_s)), x5);
1292 }
1293
1294 int ne_ssl_cert_write(const ne_ssl_certificate *cert, const char *filename)
1295 {
1296 unsigned char buffer[10*1024];
1297 size_t len = sizeof buffer;
1298
1299 FILE *fp = fopen(filename, "w");
1300
1301 if (fp == NULL) return -1;
1302
1303 if (gnutls_x509_crt_export(cert->chain[0], GNUTLS_X509_FMT_PEM, buffer,
1304 &len) < 0) {
1305 fclose(fp);
1306 return -1;
1307 }
1308
1309 if (fwrite(buffer, len, 1, fp) != 1) {
1310 fclose(fp);
1311 return -1;
1312 }
1313
1314 if (fclose(fp) != 0)
1315 return -1;
1316
1317 return 0;
1318 }
1319
1320 void ne_ssl_cert_free(ne_ssl_certificate *cert)
1321 {
1322 int n;
1323 for (n = 0; n < cert->chain_len; ++n)
1324 gnutls_x509_crt_deinit(cert->chain[n]);
1325 gnutls_free(cert->chain);
1326 if (cert->identity) ne_free(cert->identity);
1327 if (cert->issuer) ne_ssl_cert_free(cert->issuer);
1328 ne_free(cert);
1329 }
1330
1331 int ne_ssl_cert_cmp(const ne_ssl_certificate *c1, const ne_ssl_certificate *c2)
1332 {
1333 char digest1[NE_SSL_DIGESTLEN], digest2[NE_SSL_DIGESTLEN];
1334
1335 if (ne_ssl_cert_digest(c1, digest1) || ne_ssl_cert_digest(c2, digest2)) {
1336 return -1;
1337 }
1338
1339 return strcmp(digest1, digest2);
1340 }
1341
1342 /* The certificate import/export format is the base64 encoding of the
1343 * raw DER; PEM without the newlines and wrapping. */
1344
1345 ne_ssl_certificate *ne_ssl_cert_import(const char *data)
1346 {
1347 int ret;
1348 size_t len;
1349 unsigned char *der;
1350 gnutls_datum buffer = { NULL, 0 };
1351 gnutls_x509_crt x5;
1352
1353 if (gnutls_x509_crt_init(&x5) != 0)
1354 return NULL;
1355
1356 /* decode the base64 to get the raw DER representation */
1357 len = ne_unbase64(data, &der);
1358 if (len == 0) return NULL;
1359
1360 buffer.data = der;
1361 buffer.size = len;
1362
1363 ret = gnutls_x509_crt_import(x5, &buffer, GNUTLS_X509_FMT_DER);
1364 ne_free(der);
1365
1366 if (ret < 0) {
1367 gnutls_x509_crt_deinit(x5);
1368 return NULL;
1369 }
1370
1371 return populate_cert(ne_calloc(sizeof(struct ne_ssl_certificate_s)), x5);
1372 }
1373
1374 char *ne_ssl_cert_export(const ne_ssl_certificate *cert)
1375 {
1376 unsigned char *der;
1377 size_t len = 0;
1378 char *ret;
1379
1380 /* find the length of the DER encoding. */
1381 if (gnutls_x509_crt_export(cert->chain[0], GNUTLS_X509_FMT_DER, NULL, &len) !=
1382 GNUTLS_E_SHORT_MEMORY_BUFFER) {
1383 return NULL;
1384 }
1385
1386 der = ne_malloc(len);
1387 if (gnutls_x509_crt_export(cert->chain[0], GNUTLS_X509_FMT_DER, der, &len)) {
1388 ne_free(der);
1389 return NULL;
1390 }
1391
1392 ret = ne_base64(der, len);
1393 ne_free(der);
1394 return ret;
1395 }
1396
1397 int ne_ssl_cert_digest(const ne_ssl_certificate *cert, char *digest)
1398 {
1399 char sha1[20], *p;
1400 int j;
1401 size_t len = sizeof sha1;
1402
1403 if (gnutls_x509_crt_get_fingerprint(cert->chain[0], GNUTLS_DIG_SHA,
1404 sha1, &len) < 0)
1405 return -1;
1406
1407 for (j = 0, p = digest; j < 20; j++) {
1408 *p++ = NE_HEX2ASC((sha1[j] >> 4) & 0x0f);
1409 *p++ = NE_HEX2ASC(sha1[j] & 0x0f);
1410 *p++ = ':';
1411 }
1412
1413 *--p = '\0';
1414 return 0;
1415 }
1416
1417 int ne__ssl_init(void)
1418 {
1419 #if LIBGNUTLS_VERSION_NUMBER < 0x020b01
1420 #ifdef NE_HAVE_TS_SSL
1421 gcry_control(GCRYCTL_SET_THREAD_CBS, &gcry_threads_pthread);
1422 #endif
1423 gcry_control(GCRYCTL_ENABLE_QUICK_RANDOM, 0);
1424 #endif
1425 return gnutls_global_init();
1426 }
1427
1428 void ne__ssl_exit(void)
1429 {
1430 /* No way to unregister the thread callbacks. Doomed. */
1431 #if LIBGNUTLS_VERSION_MAJOR > 1 || LIBGNUTLS_VERSION_MINOR > 3 \
1432 || (LIBGNUTLS_VERSION_MINOR == 3 && LIBGNUTLS_VERSION_PATCH >= 3)
1433 /* It's safe to call gnutls_global_deinit() here only with
1434 * gnutls >= 1.3., since older versions don't refcount and
1435 * doing so would prevent any other use of gnutls within
1436 * the process. */
1437 gnutls_global_deinit();
1438 #endif
1439 }