2 * Copyright (C) 2004, 2005, 2006, 2008, 2010 Free Software Foundation,
4 * Copyright (c) 2002 Andrew McDonald <andrew@mcdonald.org.uk>
6 * This file is part of GnuTLS-EXTRA.
8 * GnuTLS-extra is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * GnuTLS-extra is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <gnutls/gnutls.h>
25 #include <openssl_compat.h>
29 #include <gnutls/openssl.h>
30 #include "../lib/gnutls_int.h"
31 #include "../lib/random.h"
32 #include "../lib/gnutls_hash_int.h"
34 /* Gnulib re-defines shutdown on mingw. We only use it as a variable
35 name, so restore the original name. */
38 /* XXX: See lib/gnutls_int.h. */
39 #define GNUTLS_POINTER_TO_INT(_) ((int) GNUTLS_POINTER_TO_INT_CAST (_))
40 #define GNUTLS_INT_TO_POINTER(_) ((void*) GNUTLS_POINTER_TO_INT_CAST (_))
42 /* WARNING: Error functions aren't currently thread-safe */
44 static int last_error
= 0;
46 /* Library initialisation functions */
49 SSL_library_init (void)
51 gnutls_global_init ();
52 /* NB: we haven't got anywhere to call gnutls_global_deinit() */
57 OpenSSL_add_all_algorithms (void)
62 /* SSL_CTX structure handling */
65 SSL_CTX_new (SSL_METHOD
* method
)
69 ctx
= (SSL_CTX
*) calloc (1, sizeof (SSL_CTX
));
76 SSL_CTX_free (SSL_CTX
* ctx
)
83 SSL_CTX_set_default_verify_paths (SSL_CTX
* ctx
)
89 SSL_CTX_use_certificate_file (SSL_CTX
* ctx
, const char *certfile
, int type
)
91 ctx
->certfile
= (char *) calloc (1, strlen (certfile
) + 1);
94 memcpy (ctx
->certfile
, certfile
, strlen (certfile
));
96 ctx
->certfile_type
= type
;
102 SSL_CTX_use_PrivateKey_file (SSL_CTX
* ctx
, const char *keyfile
, int type
)
104 ctx
->keyfile
= (char *) calloc (1, strlen (keyfile
) + 1);
107 memcpy (ctx
->keyfile
, keyfile
, strlen (keyfile
));
109 ctx
->keyfile_type
= type
;
116 SSL_CTX_set_verify (SSL_CTX
* ctx
, int verify_mode
,
117 int (*verify_callback
) (int, X509_STORE_CTX
*))
119 ctx
->verify_mode
= verify_mode
;
120 ctx
->verify_callback
= verify_callback
;
124 SSL_CTX_set_options (SSL_CTX
* ctx
, unsigned long options
)
126 return (ctx
->options
|= options
);
130 SSL_CTX_set_mode (SSL_CTX
* ctx
, long mode
)
136 SSL_CTX_set_cipher_list (SSL_CTX
* ctx
, const char *list
)
138 /* FIXME: ignore this for the moment */
139 /* We're going to have to parse the "list" string to do this */
140 /* It is a string, which in its simplest form is something like
141 "DES-CBC3-SHA:IDEA-CBC-MD5", but can be rather more complicated
142 (see OpenSSL's ciphers(1) manpage for details) */
148 /* SSL_CTX statistics */
151 SSL_CTX_sess_number (SSL_CTX
* ctx
)
157 SSL_CTX_sess_connect (SSL_CTX
* ctx
)
163 SSL_CTX_sess_connect_good (SSL_CTX
* ctx
)
169 SSL_CTX_sess_connect_renegotiate (SSL_CTX
* ctx
)
175 SSL_CTX_sess_accept (SSL_CTX
* ctx
)
181 SSL_CTX_sess_accept_good (SSL_CTX
* ctx
)
187 SSL_CTX_sess_accept_renegotiate (SSL_CTX
* ctx
)
193 SSL_CTX_sess_hits (SSL_CTX
* ctx
)
199 SSL_CTX_sess_misses (SSL_CTX
* ctx
)
205 SSL_CTX_sess_timeouts (SSL_CTX
* ctx
)
212 /* SSL structure handling */
215 SSL_new (SSL_CTX
* ctx
)
220 ssl
= (SSL
*) calloc (1, sizeof (SSL
));
224 err
= gnutls_certificate_allocate_credentials (&ssl
->gnutls_cred
);
232 gnutls_init (&ssl
->gnutls_state
, ctx
->method
->connend
);
234 gnutls_priority_set_direct (ssl
->gnutls_state
,
235 ctx
->method
->priority_string
, NULL
);
237 gnutls_credentials_set (ssl
->gnutls_state
, GNUTLS_CRD_CERTIFICATE
,
240 gnutls_certificate_set_x509_trust_file (ssl
->gnutls_cred
,
244 gnutls_certificate_set_x509_key_file (ssl
->gnutls_cred
,
245 ctx
->certfile
, ctx
->keyfile
,
248 ssl
->verify_mode
= ctx
->verify_mode
;
249 ssl
->verify_callback
= ctx
->verify_callback
;
251 ssl
->options
= ctx
->options
;
253 ssl
->rfd
= (gnutls_transport_ptr_t
) - 1;
254 ssl
->wfd
= (gnutls_transport_ptr_t
) - 1;
262 gnutls_certificate_free_credentials (ssl
->gnutls_cred
);
263 gnutls_deinit (ssl
->gnutls_state
);
268 SSL_load_error_strings (void)
273 SSL_get_error (SSL
* ssl
, int ret
)
276 return SSL_ERROR_NONE
;
278 return SSL_ERROR_ZERO_RETURN
;
282 SSL_set_fd (SSL
* ssl
, int fd
)
284 gnutls_transport_set_ptr (ssl
->gnutls_state
, GNUTLS_INT_TO_POINTER (fd
));
289 SSL_set_rfd (SSL
* ssl
, int fd
)
291 ssl
->rfd
= GNUTLS_INT_TO_POINTER (fd
);
293 if (ssl
->wfd
!= (gnutls_transport_ptr_t
) - 1)
294 gnutls_transport_set_ptr2 (ssl
->gnutls_state
, ssl
->rfd
, ssl
->wfd
);
300 SSL_set_wfd (SSL
* ssl
, int fd
)
302 ssl
->wfd
= GNUTLS_INT_TO_POINTER (fd
);
304 if (ssl
->rfd
!= (gnutls_transport_ptr_t
) - 1)
305 gnutls_transport_set_ptr2 (ssl
->gnutls_state
, ssl
->rfd
, ssl
->wfd
);
311 SSL_set_bio (SSL
* ssl
, BIO
* rbio
, BIO
* wbio
)
313 gnutls_transport_set_ptr2 (ssl
->gnutls_state
, rbio
->fd
, wbio
->fd
);
318 SSL_set_connect_state (SSL
* ssl
)
323 SSL_pending (SSL
* ssl
)
325 return gnutls_record_check_pending (ssl
->gnutls_state
);
329 SSL_set_verify (SSL
* ssl
, int verify_mode
,
330 int (*verify_callback
) (int, X509_STORE_CTX
*))
332 ssl
->verify_mode
= verify_mode
;
333 ssl
->verify_callback
= verify_callback
;
337 SSL_get_peer_certificate (SSL
* ssl
)
339 const gnutls_datum_t
*cert_list
;
340 int cert_list_size
= 0;
342 cert_list
= gnutls_certificate_get_peers (ssl
->gnutls_state
,
348 /* SSL connection open/close/read/write functions */
351 SSL_connect (SSL
* ssl
)
353 X509_STORE_CTX
*store
;
354 int cert_list_size
= 0;
356 char x_priority
[256];
357 /* take options into account before connecting */
359 memset (x_priority
, 0, sizeof (x_priority
));
360 if (ssl
->options
& SSL_OP_NO_TLSv1
)
362 snprintf(x_priority
, sizeof(x_priority
), "%s:-VERS-TLS1.0", ssl
->ctx
->method
->priority_string
);
363 err
= gnutls_priority_set_direct(ssl
->gnutls_state
, x_priority
, NULL
);
371 err
= gnutls_handshake (ssl
->gnutls_state
);
372 ssl
->last_error
= err
;
380 store
= (X509_STORE_CTX
*) calloc (1, sizeof (X509_STORE_CTX
));
382 store
->cert_list
= gnutls_certificate_get_peers (ssl
->gnutls_state
,
385 if (ssl
->verify_callback
)
387 ssl
->verify_callback (1 /*FIXME*/, store
);
389 ssl
->state
= SSL_ST_OK
;
394 /* FIXME: deal with error from callback */
400 SSL_accept (SSL
* ssl
)
402 X509_STORE_CTX
*store
;
403 int cert_list_size
= 0;
405 char x_priority
[256];
406 /* take options into account before connecting */
408 memset (x_priority
, 0, sizeof (x_priority
));
409 if (ssl
->options
& SSL_OP_NO_TLSv1
)
411 snprintf(x_priority
, sizeof(x_priority
), "%s:-VERS-TLS1.0", ssl
->ctx
->method
->priority_string
);
412 err
= gnutls_priority_set_direct(ssl
->gnutls_state
, x_priority
, NULL
);
420 /* FIXME: dh params, do we want client cert? */
422 err
= gnutls_handshake (ssl
->gnutls_state
);
423 ssl
->last_error
= err
;
431 store
= (X509_STORE_CTX
*) calloc (1, sizeof (X509_STORE_CTX
));
433 store
->cert_list
= gnutls_certificate_get_peers (ssl
->gnutls_state
,
436 if (ssl
->verify_callback
)
438 ssl
->verify_callback (1 /*FIXME*/, store
);
440 ssl
->state
= SSL_ST_OK
;
445 /* FIXME: deal with error from callback */
451 SSL_shutdown (SSL
* ssl
)
455 gnutls_bye (ssl
->gnutls_state
, GNUTLS_SHUT_WR
);
460 gnutls_bye (ssl
->gnutls_state
, GNUTLS_SHUT_RDWR
);
469 SSL_read (SSL
* ssl
, void *buf
, int len
)
473 ret
= gnutls_record_recv (ssl
->gnutls_state
, buf
, len
);
474 ssl
->last_error
= ret
;
486 SSL_write (SSL
* ssl
, const void *buf
, int len
)
490 ret
= gnutls_record_send (ssl
->gnutls_state
, buf
, len
);
491 ssl
->last_error
= ret
;
509 /* SSL_METHOD functions */
512 SSLv23_client_method (void)
515 m
= (SSL_METHOD
*) calloc (1, sizeof (SSL_METHOD
));
519 strcpy(m
->priority_string
, "NONE:+VERS-TLS1.0:+VERS-SSL3.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
521 m
->connend
= GNUTLS_CLIENT
;
527 SSLv23_server_method (void)
530 m
= (SSL_METHOD
*) calloc (1, sizeof (SSL_METHOD
));
534 strcpy(m
->priority_string
, "NONE:+VERS-TLS1.0:+VERS-SSL3.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
535 m
->connend
= GNUTLS_SERVER
;
541 SSLv3_client_method (void)
544 m
= (SSL_METHOD
*) calloc (1, sizeof (SSL_METHOD
));
548 strcpy(m
->priority_string
, "NONE:+VERS-SSL3.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
549 m
->connend
= GNUTLS_CLIENT
;
555 SSLv3_server_method (void)
558 m
= (SSL_METHOD
*) calloc (1, sizeof (SSL_METHOD
));
562 strcpy(m
->priority_string
, "NONE:+VERS-SSL3.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
563 m
->connend
= GNUTLS_SERVER
;
569 TLSv1_client_method (void)
572 m
= (SSL_METHOD
*) calloc (1, sizeof (SSL_METHOD
));
576 strcpy(m
->priority_string
, "NONE:+VERS-TLS1.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
577 m
->connend
= GNUTLS_CLIENT
;
583 TLSv1_server_method (void)
586 m
= (SSL_METHOD
*) calloc (1, sizeof (SSL_METHOD
));
590 strcpy(m
->priority_string
, "NONE:+VERS-TLS1.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
591 m
->connend
= GNUTLS_SERVER
;
597 /* SSL_CIPHER functions */
600 SSL_get_current_cipher (SSL
* ssl
)
605 ssl
->ciphersuite
.version
= gnutls_protocol_get_version (ssl
->gnutls_state
);
606 ssl
->ciphersuite
.cipher
= gnutls_cipher_get (ssl
->gnutls_state
);
607 ssl
->ciphersuite
.kx
= gnutls_kx_get (ssl
->gnutls_state
);
608 ssl
->ciphersuite
.mac
= gnutls_mac_get (ssl
->gnutls_state
);
609 ssl
->ciphersuite
.compression
= gnutls_compression_get (ssl
->gnutls_state
);
610 ssl
->ciphersuite
.cert
= gnutls_certificate_type_get (ssl
->gnutls_state
);
612 return &(ssl
->ciphersuite
);
616 SSL_CIPHER_get_name (SSL_CIPHER
* cipher
)
621 return gnutls_cipher_suite_get_name (cipher
->kx
,
622 cipher
->cipher
, cipher
->mac
);
626 SSL_CIPHER_get_bits (SSL_CIPHER
* cipher
, int *bits
)
633 bit_result
= (8 * gnutls_cipher_get_key_size (cipher
->cipher
));
642 SSL_CIPHER_get_version (SSL_CIPHER
* cipher
)
649 ret
= gnutls_protocol_get_name (cipher
->version
);
657 SSL_CIPHER_description (SSL_CIPHER
* cipher
, char *buf
, int size
)
671 tmpbuf
= (char *) malloc (128);
676 if (snprintf (tmpbuf
, tmpsize
, "%s %s %s %s",
677 gnutls_protocol_get_name (cipher
->version
),
678 gnutls_kx_get_name (cipher
->kx
),
679 gnutls_cipher_get_name (cipher
->cipher
),
680 gnutls_mac_get_name (cipher
->mac
)) == -1)
684 return (char *) "Buffer too small";
694 X509_get_subject_name (const X509
* cert
)
697 dn
= (gnutls_x509_dn
*) calloc (1, sizeof (gnutls_x509_dn
));
698 if (gnutls_x509_extract_certificate_dn (cert
, dn
) < 0)
707 X509_get_issuer_name (const X509
* cert
)
710 dn
= (gnutls_x509_dn
*) calloc (1, sizeof (gnutls_x509_dn
));
711 if (gnutls_x509_extract_certificate_issuer_dn (cert
, dn
) < 0)
720 X509_NAME_oneline (gnutls_x509_dn
* name
, char *buf
, int len
)
722 /* XXX openssl allocates buffer if buf == NULL */
725 memset (buf
, 0, len
);
727 snprintf (buf
, len
- 1,
728 "C=%s, ST=%s, L=%s, O=%s, OU=%s, CN=%s/Email=%s",
729 name
->country
, name
->state_or_province_name
,
730 name
->locality_name
, name
->organization
,
731 name
->organizational_unit_name
, name
->common_name
, name
->email
);
736 X509_free (const X509
* cert
)
738 /* only get certificates as const items */
745 BIO_get_fd (gnutls_session_t gnutls_state
, int *fd
)
747 gnutls_transport_ptr_t tmp
= gnutls_transport_get_ptr (gnutls_state
);
748 *fd
= GNUTLS_POINTER_TO_INT (tmp
);
752 BIO_new_socket (int sock
, int close_flag
)
756 bio
= (BIO
*) malloc (sizeof (BIO
));
760 bio
->fd
= GNUTLS_INT_TO_POINTER (sock
);
773 ret
= -1 * last_error
;
780 ERR_error_string (unsigned long e
, char *buf
)
782 return gnutls_strerror (-1 * e
);
795 RAND_seed (const void *buf
, int num
)
800 RAND_bytes (unsigned char *buf
, int num
)
802 _gnutls_rnd (GNUTLS_RND_RANDOM
, buf
, num
);
807 RAND_pseudo_bytes (unsigned char *buf
, int num
)
809 _gnutls_rnd (GNUTLS_RND_NONCE
, buf
, num
);
814 RAND_file_name (char *buf
, size_t len
)
820 RAND_load_file (const char *name
, long maxbytes
)
826 RAND_write_file (const char *name
)
832 RAND_egd_bytes (const char *path
, int bytes
)
839 /* message digest functions */
842 MD5_Init (MD5_CTX
* ctx
)
844 ctx
->handle
= gnutls_malloc (sizeof (digest_hd_st
));
847 _gnutls_hash_init (ctx
->handle
, GNUTLS_DIG_MD5
);
851 MD5_Update (MD5_CTX
* ctx
, const void *buf
, int len
)
853 _gnutls_hash (ctx
->handle
, buf
, len
);
857 MD5_Final (unsigned char *md
, MD5_CTX
* ctx
)
859 _gnutls_hash_deinit (ctx
->handle
, md
);
860 gnutls_free (ctx
->handle
);
864 MD5 (const unsigned char *buf
, unsigned long len
, unsigned char *md
)
869 _gnutls_hash_fast (GNUTLS_DIG_MD5
, buf
, len
, md
);
875 RIPEMD160_Init (RIPEMD160_CTX
* ctx
)
877 ctx
->handle
= gnutls_malloc (sizeof (digest_hd_st
));
880 _gnutls_hash_init (ctx
->handle
, GNUTLS_DIG_RMD160
);
884 RIPEMD160_Update (RIPEMD160_CTX
* ctx
, const void *buf
, int len
)
886 _gnutls_hash (ctx
->handle
, buf
, len
);
890 RIPEMD160_Final (unsigned char *md
, RIPEMD160_CTX
* ctx
)
892 _gnutls_hash_deinit (ctx
->handle
, md
);
893 gnutls_free (ctx
->handle
);
897 RIPEMD160 (const unsigned char *buf
, unsigned long len
, unsigned char *md
)
902 _gnutls_hash_fast (GNUTLS_DIG_RMD160
, buf
, len
, md
);