Added new gnulib
[gnutls.git] / libextra / gnutls_openssl.c
blob5eadc94f3b325ed8270040e26116fb3d526a2b5c
1 /*
2 * Copyright (C) 2004, 2005, 2006, 2008, 2010 Free Software Foundation,
3 * Inc.
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/>.
22 #include <config.h>
24 #include <gnutls/gnutls.h>
25 #include <openssl_compat.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.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. */
36 #undef shutdown
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 */
48 int
49 SSL_library_init (void)
51 gnutls_global_init ();
52 /* NB: we haven't got anywhere to call gnutls_global_deinit() */
53 return 1;
56 void
57 OpenSSL_add_all_algorithms (void)
62 /* SSL_CTX structure handling */
64 SSL_CTX *
65 SSL_CTX_new (SSL_METHOD * method)
67 SSL_CTX *ctx;
69 ctx = (SSL_CTX *) calloc (1, sizeof (SSL_CTX));
70 ctx->method = method;
72 return ctx;
75 void
76 SSL_CTX_free (SSL_CTX * ctx)
78 free (ctx->method);
79 free (ctx);
82 int
83 SSL_CTX_set_default_verify_paths (SSL_CTX * ctx)
85 return 0;
88 int
89 SSL_CTX_use_certificate_file (SSL_CTX * ctx, const char *certfile, int type)
91 ctx->certfile = (char *) calloc (1, strlen (certfile) + 1);
92 if (!ctx->certfile)
93 return -1;
94 memcpy (ctx->certfile, certfile, strlen (certfile));
96 ctx->certfile_type = type;
98 return 1;
102 SSL_CTX_use_PrivateKey_file (SSL_CTX * ctx, const char *keyfile, int type)
104 ctx->keyfile = (char *) calloc (1, strlen (keyfile) + 1);
105 if (!ctx->keyfile)
106 return -1;
107 memcpy (ctx->keyfile, keyfile, strlen (keyfile));
109 ctx->keyfile_type = type;
111 return 1;
115 void
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;
123 unsigned long
124 SSL_CTX_set_options (SSL_CTX * ctx, unsigned long options)
126 return (ctx->options |= options);
129 long
130 SSL_CTX_set_mode (SSL_CTX * ctx, long mode)
132 return 0;
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) */
144 return 1;
148 /* SSL_CTX statistics */
150 long
151 SSL_CTX_sess_number (SSL_CTX * ctx)
153 return 0;
156 long
157 SSL_CTX_sess_connect (SSL_CTX * ctx)
159 return 0;
162 long
163 SSL_CTX_sess_connect_good (SSL_CTX * ctx)
165 return 0;
168 long
169 SSL_CTX_sess_connect_renegotiate (SSL_CTX * ctx)
171 return 0;
174 long
175 SSL_CTX_sess_accept (SSL_CTX * ctx)
177 return 0;
180 long
181 SSL_CTX_sess_accept_good (SSL_CTX * ctx)
183 return 0;
186 long
187 SSL_CTX_sess_accept_renegotiate (SSL_CTX * ctx)
189 return 0;
192 long
193 SSL_CTX_sess_hits (SSL_CTX * ctx)
195 return 0;
198 long
199 SSL_CTX_sess_misses (SSL_CTX * ctx)
201 return 0;
204 long
205 SSL_CTX_sess_timeouts (SSL_CTX * ctx)
207 return 0;
212 /* SSL structure handling */
214 SSL *
215 SSL_new (SSL_CTX * ctx)
217 SSL *ssl;
218 int err;
220 ssl = (SSL *) calloc (1, sizeof (SSL));
221 if (!ssl)
222 return NULL;
224 err = gnutls_certificate_allocate_credentials (&ssl->gnutls_cred);
225 if (err < 0)
227 last_error = err;
228 free (ssl);
229 return NULL;
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,
238 ssl->gnutls_cred);
239 if (ctx->certfile)
240 gnutls_certificate_set_x509_trust_file (ssl->gnutls_cred,
241 ctx->certfile,
242 ctx->certfile_type);
243 if (ctx->keyfile)
244 gnutls_certificate_set_x509_key_file (ssl->gnutls_cred,
245 ctx->certfile, ctx->keyfile,
246 ctx->keyfile_type);
247 ssl->ctx = ctx;
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;
256 return ssl;
259 void
260 SSL_free (SSL * ssl)
262 gnutls_certificate_free_credentials (ssl->gnutls_cred);
263 gnutls_deinit (ssl->gnutls_state);
264 free (ssl);
267 void
268 SSL_load_error_strings (void)
273 SSL_get_error (SSL * ssl, int ret)
275 if (ret > 0)
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));
285 return 1;
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);
296 return 1;
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);
307 return 1;
310 void
311 SSL_set_bio (SSL * ssl, BIO * rbio, BIO * wbio)
313 gnutls_transport_set_ptr2 (ssl->gnutls_state, rbio->fd, wbio->fd);
314 /* free(BIO); ? */
317 void
318 SSL_set_connect_state (SSL * ssl)
323 SSL_pending (SSL * ssl)
325 return gnutls_record_check_pending (ssl->gnutls_state);
328 void
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;
336 const X509 *
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,
343 &cert_list_size);
345 return cert_list;
348 /* SSL connection open/close/read/write functions */
351 SSL_connect (SSL * ssl)
353 X509_STORE_CTX *store;
354 int cert_list_size = 0;
355 int err;
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);
364 if (err < 0)
366 last_error = err;
367 return 0;
371 err = gnutls_handshake (ssl->gnutls_state);
372 ssl->last_error = err;
374 if (err < 0)
376 last_error = err;
377 return 0;
380 store = (X509_STORE_CTX *) calloc (1, sizeof (X509_STORE_CTX));
381 store->ssl = ssl;
382 store->cert_list = gnutls_certificate_get_peers (ssl->gnutls_state,
383 &cert_list_size);
385 if (ssl->verify_callback)
387 ssl->verify_callback (1 /*FIXME*/, store);
389 ssl->state = SSL_ST_OK;
391 err = store->error;
392 free (store);
394 /* FIXME: deal with error from callback */
396 return 1;
400 SSL_accept (SSL * ssl)
402 X509_STORE_CTX *store;
403 int cert_list_size = 0;
404 int err;
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);
413 if (err < 0)
415 last_error = err;
416 return 0;
420 /* FIXME: dh params, do we want client cert? */
422 err = gnutls_handshake (ssl->gnutls_state);
423 ssl->last_error = err;
425 if (err < 0)
427 last_error = err;
428 return 0;
431 store = (X509_STORE_CTX *) calloc (1, sizeof (X509_STORE_CTX));
432 store->ssl = ssl;
433 store->cert_list = gnutls_certificate_get_peers (ssl->gnutls_state,
434 &cert_list_size);
436 if (ssl->verify_callback)
438 ssl->verify_callback (1 /*FIXME*/, store);
440 ssl->state = SSL_ST_OK;
442 err = store->error;
443 free (store);
445 /* FIXME: deal with error from callback */
447 return 1;
451 SSL_shutdown (SSL * ssl)
453 if (!ssl->shutdown)
455 gnutls_bye (ssl->gnutls_state, GNUTLS_SHUT_WR);
456 ssl->shutdown++;
458 else
460 gnutls_bye (ssl->gnutls_state, GNUTLS_SHUT_RDWR);
461 ssl->shutdown++;
464 /* FIXME */
465 return 1;
469 SSL_read (SSL * ssl, void *buf, int len)
471 int ret;
473 ret = gnutls_record_recv (ssl->gnutls_state, buf, len);
474 ssl->last_error = ret;
476 if (ret < 0)
478 last_error = ret;
479 return 0;
482 return ret;
486 SSL_write (SSL * ssl, const void *buf, int len)
488 int ret;
490 ret = gnutls_record_send (ssl->gnutls_state, buf, len);
491 ssl->last_error = ret;
493 if (ret < 0)
495 last_error = ret;
496 return 0;
499 return ret;
503 SSL_want (SSL * ssl)
505 return SSL_NOTHING;
509 /* SSL_METHOD functions */
511 SSL_METHOD *
512 SSLv23_client_method (void)
514 SSL_METHOD *m;
515 m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
516 if (!m)
517 return NULL;
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;
523 return m;
526 SSL_METHOD *
527 SSLv23_server_method (void)
529 SSL_METHOD *m;
530 m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
531 if (!m)
532 return NULL;
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;
537 return m;
540 SSL_METHOD *
541 SSLv3_client_method (void)
543 SSL_METHOD *m;
544 m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
545 if (!m)
546 return NULL;
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;
551 return m;
554 SSL_METHOD *
555 SSLv3_server_method (void)
557 SSL_METHOD *m;
558 m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
559 if (!m)
560 return NULL;
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;
565 return m;
568 SSL_METHOD *
569 TLSv1_client_method (void)
571 SSL_METHOD *m;
572 m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
573 if (!m)
574 return NULL;
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;
579 return m;
582 SSL_METHOD *
583 TLSv1_server_method (void)
585 SSL_METHOD *m;
586 m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
587 if (!m)
588 return NULL;
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;
593 return m;
597 /* SSL_CIPHER functions */
599 SSL_CIPHER *
600 SSL_get_current_cipher (SSL * ssl)
602 if (!ssl)
603 return NULL;
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);
615 const char *
616 SSL_CIPHER_get_name (SSL_CIPHER * cipher)
618 if (!cipher)
619 return ("NONE");
621 return gnutls_cipher_suite_get_name (cipher->kx,
622 cipher->cipher, cipher->mac);
626 SSL_CIPHER_get_bits (SSL_CIPHER * cipher, int *bits)
628 int bit_result;
630 if (!cipher)
631 return 0;
633 bit_result = (8 * gnutls_cipher_get_key_size (cipher->cipher));
635 if (bits)
636 *bits = bit_result;
638 return bit_result;
641 const char *
642 SSL_CIPHER_get_version (SSL_CIPHER * cipher)
644 const char *ret;
646 if (!cipher)
647 return ("(NONE)");
649 ret = gnutls_protocol_get_name (cipher->version);
650 if (ret)
651 return ret;
653 return ("unknown");
656 char *
657 SSL_CIPHER_description (SSL_CIPHER * cipher, char *buf, int size)
659 char *tmpbuf;
660 int tmpsize;
661 int local_alloc;
663 if (buf)
665 tmpbuf = buf;
666 tmpsize = size;
667 local_alloc = 0;
669 else
671 tmpbuf = (char *) malloc (128);
672 tmpsize = 128;
673 local_alloc = 1;
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)
682 if (local_alloc)
683 free (tmpbuf);
684 return (char *) "Buffer too small";
687 return tmpbuf;
691 /* X509 functions */
693 X509_NAME *
694 X509_get_subject_name (const X509 * cert)
696 gnutls_x509_dn *dn;
697 dn = (gnutls_x509_dn *) calloc (1, sizeof (gnutls_x509_dn));
698 if (gnutls_x509_extract_certificate_dn (cert, dn) < 0)
700 free (dn);
701 return NULL;
703 return dn;
706 X509_NAME *
707 X509_get_issuer_name (const X509 * cert)
709 gnutls_x509_dn *dn;
710 dn = (gnutls_x509_dn *) calloc (1, sizeof (gnutls_x509_dn));
711 if (gnutls_x509_extract_certificate_issuer_dn (cert, dn) < 0)
713 free (dn);
714 return NULL;
716 return dn;
719 char *
720 X509_NAME_oneline (gnutls_x509_dn * name, char *buf, int len)
722 /* XXX openssl allocates buffer if buf == NULL */
723 if (!buf)
724 return 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);
732 return buf;
735 void
736 X509_free (const X509 * cert)
738 /* only get certificates as const items */
742 /* BIO functions */
744 void
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);
751 BIO *
752 BIO_new_socket (int sock, int close_flag)
754 BIO *bio;
756 bio = (BIO *) malloc (sizeof (BIO));
757 if (!bio)
758 return NULL;
760 bio->fd = GNUTLS_INT_TO_POINTER (sock);
762 return bio;
766 /* error handling */
768 unsigned long
769 ERR_get_error (void)
771 unsigned long ret;
773 ret = -1 * last_error;
774 last_error = 0;
776 return ret;
779 const char *
780 ERR_error_string (unsigned long e, char *buf)
782 return gnutls_strerror (-1 * e);
786 /* RAND functions */
789 RAND_status (void)
791 return 1;
794 void
795 RAND_seed (const void *buf, int num)
800 RAND_bytes (unsigned char *buf, int num)
802 _gnutls_rnd (GNUTLS_RND_RANDOM, buf, num);
803 return 1;
807 RAND_pseudo_bytes (unsigned char *buf, int num)
809 _gnutls_rnd (GNUTLS_RND_NONCE, buf, num);
810 return 1;
813 const char *
814 RAND_file_name (char *buf, size_t len)
816 return "";
820 RAND_load_file (const char *name, long maxbytes)
822 return maxbytes;
826 RAND_write_file (const char *name)
828 return 0;
832 RAND_egd_bytes (const char *path, int bytes)
834 /* fake it */
835 return bytes;
839 /* message digest functions */
841 void
842 MD5_Init (MD5_CTX * ctx)
844 ctx->handle = gnutls_malloc (sizeof (digest_hd_st));
845 if (!ctx->handle)
846 abort ();
847 _gnutls_hash_init (ctx->handle, GNUTLS_DIG_MD5);
850 void
851 MD5_Update (MD5_CTX * ctx, const void *buf, int len)
853 _gnutls_hash (ctx->handle, buf, len);
856 void
857 MD5_Final (unsigned char *md, MD5_CTX * ctx)
859 _gnutls_hash_deinit (ctx->handle, md);
860 gnutls_free (ctx->handle);
863 unsigned char *
864 MD5 (const unsigned char *buf, unsigned long len, unsigned char *md)
866 if (!md)
867 return NULL;
869 _gnutls_hash_fast (GNUTLS_DIG_MD5, buf, len, md);
871 return md;
874 void
875 RIPEMD160_Init (RIPEMD160_CTX * ctx)
877 ctx->handle = gnutls_malloc (sizeof (digest_hd_st));
878 if (!ctx->handle)
879 abort ();
880 _gnutls_hash_init (ctx->handle, GNUTLS_DIG_RMD160);
883 void
884 RIPEMD160_Update (RIPEMD160_CTX * ctx, const void *buf, int len)
886 _gnutls_hash (ctx->handle, buf, len);
889 void
890 RIPEMD160_Final (unsigned char *md, RIPEMD160_CTX * ctx)
892 _gnutls_hash_deinit (ctx->handle, md);
893 gnutls_free (ctx->handle);
896 unsigned char *
897 RIPEMD160 (const unsigned char *buf, unsigned long len, unsigned char *md)
899 if (!md)
900 return NULL;
902 _gnutls_hash_fast (GNUTLS_DIG_RMD160, buf, len, md);
904 return md;