corrected copyright notices
[gnutls.git] / lib / gnutlsxx.cpp
bloba01f4627573ddcc3e8d32ffaabae78dbc9ce83b1
1 /*
2 * Copyright (C) 2006-2012 Free Software Foundation, Inc.
4 * Author: Nikos Mavrogiannopoulos
6 * This file is part of GnuTLS.
8 * The GnuTLS is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public License
10 * as published by the Free Software Foundation; either version 3 of
11 * the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>
23 #ifdef HAVE_CONFIG_H
24 # include <config.h>
25 #endif
27 #include <gnutls/gnutlsxx.h>
29 namespace gnutls
32 inline static int RETWRAP (int ret)
34 if (ret < 0)
35 throw (exception (ret));
36 return ret;
39 session::session (unsigned int flags)
41 RETWRAP (gnutls_init (&s, flags));
44 session::~session ()
46 gnutls_deinit (s);
49 int session::bye (gnutls_close_request_t how)
51 return RETWRAP (gnutls_bye (s, how));
54 int session::handshake ()
56 return RETWRAP (gnutls_handshake (s));
59 server_session::server_session ():session (GNUTLS_SERVER)
63 server_session::~server_session ()
67 int server_session::rehandshake ()
69 return RETWRAP (gnutls_rehandshake (s));
72 gnutls_alert_description_t session::get_alert () const
74 return gnutls_alert_get (s);
77 int session::send_alert (gnutls_alert_level_t level,
78 gnutls_alert_description_t desc)
80 return RETWRAP (gnutls_alert_send (s, level, desc));
83 int session::send_appropriate_alert (int err)
85 return RETWRAP (gnutls_alert_send_appropriate (s, err));
88 gnutls_cipher_algorithm_t session::get_cipher () const
90 return gnutls_cipher_get (s);
93 gnutls_kx_algorithm_t session::get_kx () const
95 return gnutls_kx_get (s);
98 gnutls_mac_algorithm_t session::get_mac () const
100 return gnutls_mac_get (s);
103 gnutls_compression_method_t session::get_compression () const
105 return gnutls_compression_get (s);
108 gnutls_certificate_type_t session::get_certificate_type () const
110 return gnutls_certificate_type_get (s);
113 void session::set_private_extensions (bool allow)
115 gnutls_handshake_set_private_extensions (s, (int) allow);
118 gnutls_handshake_description_t session::get_handshake_last_out () const
120 return gnutls_handshake_get_last_out (s);
123 gnutls_handshake_description_t session::get_handshake_last_in () const
125 return gnutls_handshake_get_last_in (s);
128 ssize_t session::send (const void *data, size_t sizeofdata)
130 return RETWRAP (gnutls_record_send (s, data, sizeofdata));
133 ssize_t session::recv (void *data, size_t sizeofdata)
135 return RETWRAP (gnutls_record_recv (s, data, sizeofdata));
138 bool session::get_record_direction () const
140 return gnutls_record_get_direction (s);
143 // maximum packet size
144 size_t session::get_max_size () const
146 return gnutls_record_get_max_size (s);
149 void session::set_max_size (size_t size)
151 RETWRAP (gnutls_record_set_max_size (s, size));
154 size_t session::check_pending () const
156 return gnutls_record_check_pending (s);
160 void session::prf (size_t label_size, const char *label,
161 int server_random_first,
162 size_t extra_size, const char *extra,
163 size_t outsize, char *out)
165 RETWRAP (gnutls_prf (s, label_size, label, server_random_first,
166 extra_size, extra, outsize, out));
169 void session::prf_raw (size_t label_size, const char *label,
170 size_t seed_size, const char *seed,
171 size_t outsize, char *out)
173 RETWRAP (gnutls_prf_raw
174 (s, label_size, label, seed_size, seed, outsize, out));
178 /* if you just want some defaults, use the following.
180 void session::set_priority (const char *prio, const char **err_pos)
182 RETWRAP (gnutls_priority_set_direct (s, prio, err_pos));
185 void session::set_priority (gnutls_priority_t p)
187 RETWRAP (gnutls_priority_set (s, p));
190 gnutls_protocol_t session::get_protocol_version () const
192 return gnutls_protocol_get_version (s);
195 void session::set_data (const void *session_data, size_t session_data_size)
197 RETWRAP (gnutls_session_set_data (s, session_data, session_data_size));
200 void session::get_data (void *session_data, size_t * session_data_size) const
202 RETWRAP (gnutls_session_get_data (s, session_data, session_data_size));
205 void session::get_data (gnutls_session_t session, gnutls_datum_t & data) const
207 RETWRAP (gnutls_session_get_data2 (s, &data));
211 void session::get_id (void *session_id, size_t * session_id_size) const
213 RETWRAP (gnutls_session_get_id (s, session_id, session_id_size));
216 bool session::is_resumed () const
218 int ret = gnutls_session_is_resumed (s);
220 return (ret != 0);
223 bool session::get_peers_certificate (std::vector < gnutls_datum_t >
224 &out_certs) const
226 const gnutls_datum_t *certs;
227 unsigned int certs_size;
229 certs = gnutls_certificate_get_peers (s, &certs_size);
231 if (certs == NULL)
232 return false;
234 for (unsigned int i = 0; i < certs_size; i++)
235 out_certs.push_back (certs[i]);
237 return true;
240 bool session::get_peers_certificate (const gnutls_datum_t ** certs,
241 unsigned int *certs_size) const
243 *certs = gnutls_certificate_get_peers (s, certs_size);
245 if (*certs == NULL)
246 return false;
247 return true;
250 void session::get_our_certificate (gnutls_datum_t & cert) const
252 const gnutls_datum_t *d;
254 d = gnutls_certificate_get_ours (s);
255 if (d == NULL)
256 throw (exception (GNUTLS_E_INVALID_REQUEST));
257 cert = *d;
260 time_t session::get_peers_certificate_activation_time () const
262 return gnutls_certificate_activation_time_peers (s);
265 time_t session::get_peers_certificate_expiration_time () const
267 return gnutls_certificate_expiration_time_peers (s);
269 void session::verify_peers_certificate (unsigned int &status) const
271 RETWRAP (gnutls_certificate_verify_peers2 (s, &status));
275 client_session::client_session ():session (GNUTLS_CLIENT)
279 client_session::~client_session ()
283 // client session
284 void client_session::set_server_name (gnutls_server_name_type_t type,
285 const void *name, size_t name_length)
287 RETWRAP (gnutls_server_name_set (s, type, name, name_length));
290 bool client_session::get_request_status ()
292 return RETWRAP (gnutls_certificate_client_get_request_status (s));
295 // server_session
296 void server_session::get_server_name (void *data, size_t * data_length,
297 unsigned int *type,
298 unsigned int indx) const
300 RETWRAP (gnutls_server_name_get (s, data, data_length, type, indx));
303 // internal DB stuff
304 static int store_function (void *_db, gnutls_datum_t key,
305 gnutls_datum_t data)
309 DB *db = static_cast < DB * >(_db);
311 if (db->store (key, data) == false)
312 return -1;
314 catch (...)
316 return -1;
319 return 0;
322 const static gnutls_datum_t null_datum = { NULL, 0 };
324 static gnutls_datum_t retrieve_function (void *_db, gnutls_datum_t key)
326 gnutls_datum_t data;
330 DB *db = static_cast < DB * >(_db);
332 if (db->retrieve (key, data) == false)
333 return null_datum;
336 catch (...)
338 return null_datum;
341 return data;
344 static int remove_function (void *_db, gnutls_datum_t key)
348 DB *db = static_cast < DB * >(_db);
350 if (db->remove (key) == false)
351 return -1;
353 catch (...)
355 return -1;
358 return 0;
361 void server_session::set_db (const DB & db)
363 gnutls_db_set_ptr (s, const_cast < DB * >(&db));
364 gnutls_db_set_store_function (s, store_function);
365 gnutls_db_set_retrieve_function (s, retrieve_function);
366 gnutls_db_set_remove_function (s, remove_function);
369 void server_session::set_db_cache_expiration (unsigned int seconds)
371 gnutls_db_set_cache_expiration (s, seconds);
374 void server_session::db_remove () const
376 gnutls_db_remove_session (s);
379 bool server_session::db_check_entry (gnutls_datum_t & session_data) const
381 int ret = gnutls_db_check_entry (s, session_data);
383 if (ret != 0)
384 return true;
385 return false;
388 void session::set_max_handshake_packet_length (size_t max)
390 gnutls_handshake_set_max_packet_length (s, max);
393 void session::clear_credentials ()
395 gnutls_credentials_clear (s);
398 void session::set_credentials (credentials & cred)
400 RETWRAP (gnutls_credentials_set (s, cred.get_type (), cred.ptr ()));
403 const char *server_session::get_srp_username () const
405 #ifdef ENABLE_SRP
406 return gnutls_srp_server_get_username (s);
407 #else
408 return NULL;
409 #endif
412 const char *server_session::get_psk_username () const
414 return gnutls_psk_server_get_username (s);
418 void session::set_transport_ptr (gnutls_transport_ptr_t ptr)
420 gnutls_transport_set_ptr (s, ptr);
423 void session::set_transport_ptr (gnutls_transport_ptr_t recv_ptr,
424 gnutls_transport_ptr_t send_ptr)
426 gnutls_transport_set_ptr2 (s, recv_ptr, send_ptr);
430 gnutls_transport_ptr_t session::get_transport_ptr () const
432 return gnutls_transport_get_ptr (s);
435 void session::get_transport_ptr (gnutls_transport_ptr_t & recv_ptr,
436 gnutls_transport_ptr_t & send_ptr) const
438 gnutls_transport_get_ptr2 (s, &recv_ptr, &send_ptr);
441 void session::set_transport_lowat (size_t num)
443 throw (exception (GNUTLS_E_UNIMPLEMENTED_FEATURE));
446 void session::set_transport_push_function (gnutls_push_func push_func)
448 gnutls_transport_set_push_function (s, push_func);
451 void session::set_transport_vec_push_function (gnutls_vec_push_func vec_push_func)
453 gnutls_transport_set_vec_push_function (s, vec_push_func);
456 void session::set_transport_pull_function (gnutls_pull_func pull_func)
458 gnutls_transport_set_pull_function (s, pull_func);
461 void session::set_user_ptr (void *ptr)
463 gnutls_session_set_ptr (s, ptr);
466 void *session::get_user_ptr () const
468 return gnutls_session_get_ptr (s);
471 void session::send_openpgp_cert (gnutls_openpgp_crt_status_t status)
473 #ifdef ENABLE_OPENPGP
474 gnutls_openpgp_send_cert (s, status);
475 #endif
478 void session::set_dh_prime_bits (unsigned int bits)
480 gnutls_dh_set_prime_bits (s, bits);
483 unsigned int session::get_dh_secret_bits () const
485 return RETWRAP (gnutls_dh_get_secret_bits (s));
488 unsigned int session::get_dh_peers_public_bits () const
490 return RETWRAP (gnutls_dh_get_peers_public_bits (s));
493 unsigned int session::get_dh_prime_bits () const
495 return RETWRAP (gnutls_dh_get_prime_bits (s));
498 void session::get_dh_group (gnutls_datum_t & gen,
499 gnutls_datum_t & prime) const
501 RETWRAP (gnutls_dh_get_group (s, &gen, &prime));
504 void session::get_dh_pubkey (gnutls_datum_t & raw_key) const
506 RETWRAP (gnutls_dh_get_pubkey (s, &raw_key));
509 void session::get_rsa_export_pubkey (gnutls_datum_t & exponent,
510 gnutls_datum_t & modulus) const
512 RETWRAP (gnutls_rsa_export_get_pubkey (s, &exponent, &modulus));
515 unsigned int session::get_rsa_export_modulus_bits () const
517 return RETWRAP (gnutls_rsa_export_get_modulus_bits (s));
520 void server_session::
521 set_certificate_request (gnutls_certificate_request_t req)
523 gnutls_certificate_server_set_request (s, req);
526 gnutls_credentials_type_t session::get_auth_type () const
528 return gnutls_auth_get_type (s);
531 gnutls_credentials_type_t session::get_server_auth_type () const
533 return gnutls_auth_server_get_type (s);
536 gnutls_credentials_type_t session::get_client_auth_type () const
538 return gnutls_auth_client_get_type (s);
542 certificate_credentials::~certificate_credentials ()
544 gnutls_certificate_free_credentials (cred);
547 certificate_credentials::certificate_credentials ():credentials
548 (GNUTLS_CRD_CERTIFICATE)
550 RETWRAP (gnutls_certificate_allocate_credentials (&cred));
551 set_ptr (cred);
554 void certificate_server_credentials::
555 set_params_function (gnutls_params_function * func)
557 gnutls_certificate_set_params_function (cred, func);
560 anon_server_credentials::anon_server_credentials ():credentials
561 (GNUTLS_CRD_ANON)
563 RETWRAP (gnutls_anon_allocate_server_credentials (&cred));
564 set_ptr (cred);
567 anon_server_credentials::~anon_server_credentials ()
569 gnutls_anon_free_server_credentials (cred);
572 void anon_server_credentials::set_dh_params (const dh_params & params)
574 gnutls_anon_set_server_dh_params (cred, params.get_params_t ());
577 void anon_server_credentials::set_params_function (gnutls_params_function *
578 func)
580 gnutls_anon_set_server_params_function (cred, func);
583 anon_client_credentials::anon_client_credentials ():credentials
584 (GNUTLS_CRD_ANON)
586 RETWRAP (gnutls_anon_allocate_client_credentials (&cred));
587 set_ptr (cred);
590 anon_client_credentials::~anon_client_credentials ()
592 gnutls_anon_free_client_credentials (cred);
595 void certificate_credentials::free_keys ()
597 gnutls_certificate_free_keys (cred);
600 void certificate_credentials::free_cas ()
602 gnutls_certificate_free_cas (cred);
605 void certificate_credentials::free_ca_names ()
607 gnutls_certificate_free_ca_names (cred);
610 void certificate_credentials::free_crls ()
612 gnutls_certificate_free_crls (cred);
616 void certificate_credentials::set_dh_params (const dh_params & params)
618 gnutls_certificate_set_dh_params (cred, params.get_params_t ());
621 void certificate_credentials::
622 set_rsa_export_params (const rsa_params & params)
624 gnutls_certificate_set_rsa_export_params (cred, params.get_params_t ());
627 void certificate_credentials::set_verify_flags (unsigned int flags)
629 gnutls_certificate_set_verify_flags (cred, flags);
632 void certificate_credentials::set_verify_limits (unsigned int max_bits,
633 unsigned int max_depth)
635 gnutls_certificate_set_verify_limits (cred, max_bits, max_depth);
638 void certificate_credentials::set_x509_trust_file (const char *cafile,
639 gnutls_x509_crt_fmt_t
640 type)
642 RETWRAP (gnutls_certificate_set_x509_trust_file (cred, cafile, type));
645 void certificate_credentials::set_x509_trust (const gnutls_datum_t & CA,
646 gnutls_x509_crt_fmt_t type)
648 RETWRAP (gnutls_certificate_set_x509_trust_mem (cred, &CA, type));
652 void certificate_credentials::set_x509_crl_file (const char *crlfile,
653 gnutls_x509_crt_fmt_t type)
655 RETWRAP (gnutls_certificate_set_x509_crl_file (cred, crlfile, type));
658 void certificate_credentials::set_x509_crl (const gnutls_datum_t & CRL,
659 gnutls_x509_crt_fmt_t type)
661 RETWRAP (gnutls_certificate_set_x509_crl_mem (cred, &CRL, type));
664 void certificate_credentials::set_x509_key_file (const char *certfile,
665 const char *keyfile,
666 gnutls_x509_crt_fmt_t type)
668 RETWRAP (gnutls_certificate_set_x509_key_file
669 (cred, certfile, keyfile, type));
672 void certificate_credentials::set_x509_key (const gnutls_datum_t & CERT,
673 const gnutls_datum_t & KEY,
674 gnutls_x509_crt_fmt_t type)
676 RETWRAP (gnutls_certificate_set_x509_key_mem (cred, &CERT, &KEY, type));
679 void certificate_credentials::
680 set_simple_pkcs12_file (const char *pkcs12file,
681 gnutls_x509_crt_fmt_t type, const char *password)
683 RETWRAP (gnutls_certificate_set_x509_simple_pkcs12_file
684 (cred, pkcs12file, type, password));
687 void certificate_credentials::set_x509_key (gnutls_x509_crt_t * cert_list,
688 int cert_list_size,
689 gnutls_x509_privkey_t key)
691 RETWRAP (gnutls_certificate_set_x509_key
692 (cred, cert_list, cert_list_size, key));
695 void certificate_credentials::set_x509_trust (gnutls_x509_crt_t * ca_list,
696 int ca_list_size)
698 RETWRAP (gnutls_certificate_set_x509_trust (cred, ca_list, ca_list_size));
701 void certificate_credentials::set_x509_crl (gnutls_x509_crl_t * crl_list,
702 int crl_list_size)
704 RETWRAP (gnutls_certificate_set_x509_crl (cred, crl_list, crl_list_size));
707 void certificate_credentials::
708 set_retrieve_function (gnutls_certificate_retrieve_function * func)
710 gnutls_certificate_set_retrieve_function (cred, func);
713 // SRP
715 #ifdef ENABLE_SRP
717 srp_server_credentials::srp_server_credentials ():credentials
718 (GNUTLS_CRD_SRP)
720 RETWRAP (gnutls_srp_allocate_server_credentials (&cred));
721 set_ptr (cred);
724 srp_server_credentials::~srp_server_credentials ()
726 gnutls_srp_free_server_credentials (cred);
729 srp_client_credentials::srp_client_credentials ():credentials
730 (GNUTLS_CRD_SRP)
732 RETWRAP (gnutls_srp_allocate_client_credentials (&cred));
733 set_ptr (cred);
736 srp_client_credentials::~srp_client_credentials ()
738 gnutls_srp_free_client_credentials (cred);
741 void srp_client_credentials::set_credentials (const char *username,
742 const char *password)
744 RETWRAP (gnutls_srp_set_client_credentials (cred, username, password));
747 void srp_server_credentials::
748 set_credentials_file (const char *password_file,
749 const char *password_conf_file)
751 RETWRAP (gnutls_srp_set_server_credentials_file
752 (cred, password_file, password_conf_file));
755 void srp_server_credentials::
756 set_credentials_function (gnutls_srp_server_credentials_function * func)
758 gnutls_srp_set_server_credentials_function (cred, func);
761 void srp_client_credentials::
762 set_credentials_function (gnutls_srp_client_credentials_function * func)
764 gnutls_srp_set_client_credentials_function (cred, func);
767 #endif /* ENABLE_SRP */
769 // PSK
771 psk_server_credentials::psk_server_credentials ():credentials
772 (GNUTLS_CRD_PSK)
774 RETWRAP (gnutls_psk_allocate_server_credentials (&cred));
775 set_ptr (cred);
778 psk_server_credentials::~psk_server_credentials ()
780 gnutls_psk_free_server_credentials (cred);
783 void psk_server_credentials::
784 set_credentials_file (const char *password_file)
786 RETWRAP (gnutls_psk_set_server_credentials_file (cred, password_file));
789 void psk_server_credentials::
790 set_credentials_function (gnutls_psk_server_credentials_function * func)
792 gnutls_psk_set_server_credentials_function (cred, func);
795 void psk_server_credentials::set_dh_params (const dh_params & params)
797 gnutls_psk_set_server_dh_params (cred, params.get_params_t ());
800 void psk_server_credentials::set_params_function (gnutls_params_function *
801 func)
803 gnutls_psk_set_server_params_function (cred, func);
806 psk_client_credentials::psk_client_credentials ():credentials
807 (GNUTLS_CRD_PSK)
809 RETWRAP (gnutls_psk_allocate_client_credentials (&cred));
810 set_ptr (cred);
813 psk_client_credentials::~psk_client_credentials ()
815 gnutls_psk_free_client_credentials (cred);
818 void psk_client_credentials::set_credentials (const char *username,
819 const gnutls_datum_t & key,
820 gnutls_psk_key_flags flags)
822 RETWRAP (gnutls_psk_set_client_credentials (cred, username, &key, flags));
825 void psk_client_credentials::
826 set_credentials_function (gnutls_psk_client_credentials_function * func)
828 gnutls_psk_set_client_credentials_function (cred, func);
831 credentials::credentials (gnutls_credentials_type_t t):type (t),
832 cred (NULL)
836 gnutls_credentials_type_t credentials::get_type () const
838 return type;
841 void *credentials::ptr () const
843 return cred;
846 void credentials::set_ptr (void *ptr)
848 cred = ptr;
851 exception::exception (int x)
853 retcode = x;
856 int exception::get_code ()
858 return retcode;
861 const char *exception::what () const throw ()
863 return gnutls_strerror (retcode);
866 dh_params::dh_params ()
868 RETWRAP (gnutls_dh_params_init (&params));
871 dh_params::~dh_params ()
873 gnutls_dh_params_deinit (params);
876 void dh_params::import_raw (const gnutls_datum_t & prime,
877 const gnutls_datum_t & generator)
879 RETWRAP (gnutls_dh_params_import_raw (params, &prime, &generator));
882 void dh_params::import_pkcs3 (const gnutls_datum_t & pkcs3_params,
883 gnutls_x509_crt_fmt_t format)
885 RETWRAP (gnutls_dh_params_import_pkcs3 (params, &pkcs3_params, format));
888 void dh_params::generate (unsigned int bits)
890 RETWRAP (gnutls_dh_params_generate2 (params, bits));
893 void dh_params::export_pkcs3 (gnutls_x509_crt_fmt_t format,
894 unsigned char *params_data,
895 size_t * params_data_size)
897 RETWRAP (gnutls_dh_params_export_pkcs3
898 (params, format, params_data, params_data_size));
901 void dh_params::export_raw (gnutls_datum_t & prime,
902 gnutls_datum_t & generator)
904 RETWRAP (gnutls_dh_params_export_raw (params, &prime, &generator, NULL));
907 gnutls_dh_params_t dh_params::get_params_t () const
909 return params;
912 dh_params & dh_params::operator= (const dh_params & src)
914 dh_params *dst = new dh_params;
915 int ret;
917 ret = gnutls_dh_params_cpy (dst->params, src.params);
919 if (ret < 0)
921 delete dst;
922 throw (exception (ret));
925 return *dst;
928 // RSA
930 rsa_params::rsa_params ()
932 RETWRAP (gnutls_rsa_params_init (&params));
935 rsa_params::~rsa_params ()
937 gnutls_rsa_params_deinit (params);
940 void rsa_params::import_pkcs1 (const gnutls_datum_t & pkcs1_params,
941 gnutls_x509_crt_fmt_t format)
943 RETWRAP (gnutls_rsa_params_import_pkcs1 (params, &pkcs1_params, format));
946 void rsa_params::generate (unsigned int bits)
948 RETWRAP (gnutls_rsa_params_generate2 (params, bits));
951 void rsa_params::export_pkcs1 (gnutls_x509_crt_fmt_t format,
952 unsigned char *params_data,
953 size_t * params_data_size)
955 RETWRAP (gnutls_rsa_params_export_pkcs1
956 (params, format, params_data, params_data_size));
959 gnutls_rsa_params_t rsa_params::get_params_t () const
961 return params;
964 rsa_params & rsa_params::operator= (const rsa_params & src)
966 rsa_params *dst = new rsa_params;
967 int ret;
969 ret = gnutls_rsa_params_cpy (dst->params, src.params);
971 if (ret < 0)
973 delete dst;
974 throw (exception (ret));
977 return *dst;
980 void rsa_params::import_raw (const gnutls_datum_t & m,
981 const gnutls_datum_t & e,
982 const gnutls_datum_t & d,
983 const gnutls_datum_t & p,
984 const gnutls_datum_t & q,
985 const gnutls_datum_t & u)
988 RETWRAP (gnutls_rsa_params_import_raw (params, &m, &e, &d, &p, &q, &u));
992 void rsa_params::export_raw (gnutls_datum_t & m, gnutls_datum_t & e,
993 gnutls_datum_t & d, gnutls_datum_t & p,
994 gnutls_datum_t & q, gnutls_datum_t & u)
996 RETWRAP (gnutls_rsa_params_export_raw
997 (params, &m, &e, &d, &p, &q, &u, NULL));
1000 } // namespace gnutls