Revert "Revert ABI version, make libgnutls-extra use another ABI version."
[gnutls.git] / lib / gnutlsxx.cpp
blob9d38f2349eb4b91315ed15d6b0fea26bfc673d72
1 #include <gnutls/gnutlsxx.h>
3 namespace gnutls
6 inline static int RETWRAP_NET(int ret)
8 if (gnutls_error_is_fatal(ret)) throw(exception(ret));
9 else return ret;
12 inline static int RETWRAP(int ret)
14 if (ret < 0) throw(exception(ret));
15 return ret;
18 session::session( gnutls_connection_end_t end)
20 RETWRAP(gnutls_init( &s, end));
23 session::~session()
25 gnutls_deinit( s);
28 int session::bye( gnutls_close_request_t how)
30 return RETWRAP_NET( gnutls_bye( s, how));
33 int session::handshake ()
35 return RETWRAP_NET( gnutls_handshake( s));
39 server_session::server_session() : session( GNUTLS_SERVER)
43 server_session::~server_session()
47 int server_session::rehandshake()
49 return RETWRAP_NET( gnutls_rehandshake( s));
52 gnutls_alert_description_t session::get_alert() const
54 return gnutls_alert_get( s);
57 int session::send_alert ( gnutls_alert_level_t level,
58 gnutls_alert_description_t desc)
60 return RETWRAP_NET(gnutls_alert_send( s, level, desc));
63 int session::send_appropriate_alert (int err)
65 return RETWRAP_NET(gnutls_alert_send_appropriate( s, err));
68 gnutls_cipher_algorithm_t session::get_cipher() const
70 return gnutls_cipher_get( s);
73 gnutls_kx_algorithm_t session::get_kx () const
75 return gnutls_kx_get( s);
78 gnutls_mac_algorithm_t session::get_mac () const
80 return gnutls_mac_get( s);
83 gnutls_compression_method_t session::get_compression() const
85 return gnutls_compression_get( s);
88 gnutls_certificate_type_t session::get_certificate_type() const
90 return gnutls_certificate_type_get( s);
93 void session::set_private_extensions ( bool allow)
95 gnutls_handshake_set_private_extensions( s, (int)allow);
98 gnutls_handshake_description_t session::get_handshake_last_out() const
100 return gnutls_handshake_get_last_out( s);
103 gnutls_handshake_description_t session::get_handshake_last_in() const
105 return gnutls_handshake_get_last_in( s);
108 ssize_t session::send (const void *data, size_t sizeofdata)
110 return RETWRAP_NET(gnutls_record_send( s, data, sizeofdata));
113 ssize_t session::recv (void *data, size_t sizeofdata)
115 return RETWRAP_NET(gnutls_record_recv( s, data, sizeofdata));
118 bool session::get_record_direction() const
120 return gnutls_record_get_direction(s);
123 // maximum packet size
124 size_t session::get_max_size () const
126 return gnutls_record_get_max_size( s);
129 void session::set_max_size(size_t size)
131 RETWRAP( gnutls_record_set_max_size( s, size));
134 size_t session::check_pending () const
136 return gnutls_record_check_pending( s);
140 void session::prf (size_t label_size, const char *label,
141 int server_random_first,
142 size_t extra_size, const char *extra,
143 size_t outsize, char *out)
145 RETWRAP(gnutls_prf( s, label_size, label, server_random_first,
146 extra_size, extra, outsize, out));
149 void session::prf_raw ( size_t label_size, const char *label,
150 size_t seed_size, const char *seed,
151 size_t outsize, char *out)
153 RETWRAP( gnutls_prf_raw( s, label_size, label, seed_size, seed, outsize, out));
157 void session::set_cipher_priority (const int *list)
159 RETWRAP( gnutls_cipher_set_priority( s, list));
162 void session::set_mac_priority (const int *list)
164 RETWRAP( gnutls_mac_set_priority( s, list));
167 void session::set_compression_priority (const int *list)
169 RETWRAP( gnutls_compression_set_priority( s, list));
172 void session::set_kx_priority (const int *list)
174 RETWRAP( gnutls_kx_set_priority( s, list));
177 void session::set_protocol_priority (const int *list)
179 RETWRAP( gnutls_protocol_set_priority( s, list));
182 void session::set_certificate_type_priority (const int *list)
184 RETWRAP( gnutls_certificate_type_set_priority( s, list));
188 /* if you just want some defaults, use the following.
190 void session::set_priority(const char* prio, const char** err_pos)
192 RETWRAP(gnutls_priority_set_direct( s, prio, err_pos));
195 void session::set_priority(gnutls_priority_t p)
197 RETWRAP(gnutls_priority_set( s, p));
200 gnutls_protocol_t session::get_protocol_version() const
202 return gnutls_protocol_get_version( s);
205 void session::set_data ( const void *session_data,
206 size_t session_data_size)
208 RETWRAP(gnutls_session_set_data( s, session_data, session_data_size));
211 void session::get_data (void *session_data,
212 size_t * session_data_size) const
214 RETWRAP(gnutls_session_get_data( s, session_data, session_data_size));
217 void session::get_data(gnutls_session_t session,
218 gnutls_datum_t & data) const
220 RETWRAP(gnutls_session_get_data2( s, &data));
224 void session::get_id ( void *session_id,
225 size_t * session_id_size) const
227 RETWRAP( gnutls_session_get_id( s, session_id, session_id_size));
230 bool session::is_resumed() const
232 int ret = gnutls_session_is_resumed( s);
234 return (ret != 0);
238 bool session::get_peers_certificate(std::vector<gnutls_datum_t> &out_certs) const
240 const gnutls_datum_t *certs;
241 unsigned int certs_size;
243 certs = gnutls_certificate_get_peers (s, &certs_size);
245 if (certs==NULL) return false;
247 for(unsigned int i=0;i<certs_size;i++)
248 out_certs.push_back( certs[i]);
250 return true;
253 bool session::get_peers_certificate(const gnutls_datum_t** certs, unsigned int *certs_size) const
255 *certs = gnutls_certificate_get_peers (s, certs_size);
257 if (*certs==NULL) return false;
258 return true;
261 void session::get_our_certificate(gnutls_datum_t& cert) const
263 const gnutls_datum_t *d;
265 d = gnutls_certificate_get_ours(s);
266 if (d==NULL)
267 throw(exception( GNUTLS_E_INVALID_REQUEST));
268 cert = *d;
271 time_t session::get_peers_certificate_activation_time() const
273 return gnutls_certificate_activation_time_peers( s);
276 time_t session::get_peers_certificate_expiration_time() const
278 return gnutls_certificate_expiration_time_peers( s);
280 void session::verify_peers_certificate( unsigned int& status) const
282 RETWRAP( gnutls_certificate_verify_peers2( s, &status));
286 client_session::client_session() : session( GNUTLS_CLIENT)
290 client_session::~client_session()
294 // client session
295 void client_session::set_server_name (gnutls_server_name_type_t type,
296 const void *name, size_t name_length)
298 RETWRAP( gnutls_server_name_set( s, type, name, name_length));
301 bool client_session::get_request_status()
303 return RETWRAP(gnutls_certificate_client_get_request_status (s));
306 // server_session
307 void server_session::get_server_name (void *data, size_t * data_length,
308 unsigned int *type, unsigned int indx) const
310 RETWRAP( gnutls_server_name_get( s, data, data_length, type, indx));
313 // internal DB stuff
314 static int store_function(void *_db, gnutls_datum_t key, gnutls_datum_t data)
316 try {
317 DB* db = static_cast<DB*>(_db);
319 if (db->store( key, data)==false) return -1;
320 } catch(...) {
321 return -1;
324 return 0;
327 const static gnutls_datum_t null_datum = { NULL, 0 };
329 static gnutls_datum_t retrieve_function(void *_db, gnutls_datum_t key)
331 gnutls_datum_t data;
333 try {
334 DB* db = static_cast<DB*>(_db);
336 if (db->retrieve( key, data)==false) return null_datum;
338 } catch(...) {
339 return null_datum;
342 return data;
345 static int remove_function(void *_db, gnutls_datum_t key)
347 try {
348 DB* db = static_cast<DB*>(_db);
350 if (db->remove( key)==false) return -1;
351 } catch(...) {
352 return -1;
355 return 0;
358 void server_session::set_db( const DB& db)
360 gnutls_db_set_ptr( s, const_cast<DB*>(&db));
361 gnutls_db_set_store_function( s, store_function);
362 gnutls_db_set_retrieve_function( s, retrieve_function);
363 gnutls_db_set_remove_function( s, remove_function);
366 void server_session::set_db_cache_expiration (unsigned int seconds)
368 gnutls_db_set_cache_expiration( s, seconds);
371 void server_session::db_remove () const
373 gnutls_db_remove_session( s);
376 bool server_session::db_check_entry ( gnutls_datum_t &session_data) const
378 int ret = gnutls_db_check_entry( s, session_data);
380 if (ret != 0) return true;
381 return false;
384 void session::set_max_handshake_packet_length ( size_t max)
386 gnutls_handshake_set_max_packet_length( s, max);
389 void session::clear_credentials()
391 gnutls_credentials_clear( s);
394 void session::set_credentials( credentials &cred)
396 RETWRAP(gnutls_credentials_set( s, cred.get_type(), cred.ptr()));
399 const char* server_session::get_srp_username() const
401 return gnutls_srp_server_get_username( s);
404 const char* server_session::get_psk_username() const
406 return gnutls_psk_server_get_username( s);
410 void session::set_transport_ptr( gnutls_transport_ptr_t ptr)
412 gnutls_transport_set_ptr( s, ptr);
415 void session::set_transport_ptr( gnutls_transport_ptr_t recv_ptr, gnutls_transport_ptr_t send_ptr)
417 gnutls_transport_set_ptr2( s, recv_ptr, send_ptr);
421 gnutls_transport_ptr_t session::get_transport_ptr () const
423 return gnutls_transport_get_ptr (s);
426 void session::get_transport_ptr( gnutls_transport_ptr_t & recv_ptr,
427 gnutls_transport_ptr_t & send_ptr) const
429 gnutls_transport_get_ptr2 (s, &recv_ptr, &send_ptr);
432 void session::set_transport_lowat( size_t num)
434 gnutls_transport_set_lowat (s, num);
437 void session::set_transport_push_function( gnutls_push_func push_func)
439 gnutls_transport_set_push_function ( s, push_func);
442 void session::set_transport_pull_function( gnutls_pull_func pull_func)
444 gnutls_transport_set_pull_function ( s, pull_func);
447 void session::set_user_ptr( void* ptr)
449 gnutls_session_set_ptr( s, ptr);
452 void* session::get_user_ptr( ) const
454 return gnutls_session_get_ptr(s);
457 void session::send_openpgp_cert( gnutls_openpgp_crt_status_t status)
459 gnutls_openpgp_send_cert(s, status);
463 void session::set_dh_prime_bits( unsigned int bits)
465 gnutls_dh_set_prime_bits( s, bits);
468 unsigned int session::get_dh_secret_bits() const
470 return RETWRAP( gnutls_dh_get_secret_bits( s));
473 unsigned int session::get_dh_peers_public_bits() const
475 return RETWRAP(gnutls_dh_get_peers_public_bits( s));
478 unsigned int session::get_dh_prime_bits() const
480 return RETWRAP( gnutls_dh_get_prime_bits( s));
483 void session::get_dh_group( gnutls_datum_t & gen, gnutls_datum_t & prime) const
485 RETWRAP( gnutls_dh_get_group( s, &gen, &prime));
488 void session::get_dh_pubkey( gnutls_datum_t & raw_key) const
490 RETWRAP(gnutls_dh_get_pubkey( s, &raw_key));
493 void session::get_rsa_export_pubkey( gnutls_datum_t& exponent, gnutls_datum_t& modulus) const
495 RETWRAP( gnutls_rsa_export_get_pubkey( s, &exponent, &modulus));
498 unsigned int session::get_rsa_export_modulus_bits() const
500 return RETWRAP(gnutls_rsa_export_get_modulus_bits( s));
503 void server_session::set_certificate_request( gnutls_certificate_request_t req)
505 gnutls_certificate_server_set_request (s, req);
511 gnutls_credentials_type_t session::get_auth_type() const
513 return gnutls_auth_get_type( s);
516 gnutls_credentials_type_t session::get_server_auth_type() const
518 return gnutls_auth_server_get_type( s);
521 gnutls_credentials_type_t session::get_client_auth_type() const
523 return gnutls_auth_client_get_type( s);
527 certificate_credentials::~certificate_credentials()
529 gnutls_certificate_free_credentials (cred);
532 certificate_credentials::certificate_credentials() : credentials(GNUTLS_CRD_CERTIFICATE)
534 RETWRAP(gnutls_certificate_allocate_credentials ( &cred));
535 set_ptr(cred);
538 void certificate_server_credentials::set_params_function( gnutls_params_function* func)
540 gnutls_certificate_set_params_function( cred, func);
543 anon_server_credentials::anon_server_credentials() : credentials(GNUTLS_CRD_ANON)
545 RETWRAP(gnutls_anon_allocate_server_credentials( &cred));
546 set_ptr(cred);
549 anon_server_credentials::~anon_server_credentials()
551 gnutls_anon_free_server_credentials( cred);
554 void anon_server_credentials::set_dh_params( const dh_params& params)
556 gnutls_anon_set_server_dh_params (cred, params.get_params_t());
559 void anon_server_credentials::set_params_function ( gnutls_params_function * func)
561 gnutls_anon_set_server_params_function ( cred, func);
564 anon_client_credentials::anon_client_credentials() : credentials(GNUTLS_CRD_ANON)
566 RETWRAP(gnutls_anon_allocate_client_credentials( &cred));
567 set_ptr(cred);
570 anon_client_credentials::~anon_client_credentials()
572 gnutls_anon_free_client_credentials( cred);
575 void certificate_credentials::free_keys ()
577 gnutls_certificate_free_keys( cred);
580 void certificate_credentials::free_cas ()
582 gnutls_certificate_free_cas( cred);
585 void certificate_credentials::free_ca_names ()
587 gnutls_certificate_free_ca_names( cred);
590 void certificate_credentials::free_crls ()
592 gnutls_certificate_free_crls( cred);
596 void certificate_credentials::set_dh_params ( const dh_params& params)
598 gnutls_certificate_set_dh_params( cred, params.get_params_t());
601 void certificate_credentials::set_rsa_export_params ( const rsa_params & params)
603 gnutls_certificate_set_rsa_export_params( cred, params.get_params_t());
606 void certificate_credentials::set_verify_flags ( unsigned int flags)
608 gnutls_certificate_set_verify_flags( cred, flags);
611 void certificate_credentials::set_verify_limits ( unsigned int max_bits, unsigned int max_depth)
613 gnutls_certificate_set_verify_limits( cred, max_bits, max_depth);
616 void certificate_credentials::set_x509_trust_file(const char *cafile, gnutls_x509_crt_fmt_t type)
618 RETWRAP( gnutls_certificate_set_x509_trust_file( cred, cafile, type));
621 void certificate_credentials::set_x509_trust(const gnutls_datum_t & CA, gnutls_x509_crt_fmt_t type)
623 RETWRAP( gnutls_certificate_set_x509_trust_mem( cred, &CA, type));
627 void certificate_credentials::set_x509_crl_file( const char *crlfile, gnutls_x509_crt_fmt_t type)
629 RETWRAP( gnutls_certificate_set_x509_crl_file( cred, crlfile, type));
632 void certificate_credentials::set_x509_crl(const gnutls_datum_t & CRL, gnutls_x509_crt_fmt_t type)
634 RETWRAP( gnutls_certificate_set_x509_crl_mem( cred, &CRL, type));
637 void certificate_credentials::set_x509_key_file(const char *certfile, const char *keyfile, gnutls_x509_crt_fmt_t type)
639 RETWRAP( gnutls_certificate_set_x509_key_file( cred, certfile, keyfile, type));
642 void certificate_credentials::set_x509_key(const gnutls_datum_t & CERT, const gnutls_datum_t & KEY, gnutls_x509_crt_fmt_t type)
644 RETWRAP( gnutls_certificate_set_x509_key_mem( cred, &CERT, &KEY, type));
647 void certificate_credentials::set_simple_pkcs12_file( const char *pkcs12file,
648 gnutls_x509_crt_fmt_t type, const char *password)
650 RETWRAP( gnutls_certificate_set_x509_simple_pkcs12_file( cred, pkcs12file, type, password));
653 void certificate_credentials::set_x509_key ( gnutls_x509_crt_t * cert_list, int cert_list_size,
654 gnutls_x509_privkey_t key)
656 RETWRAP( gnutls_certificate_set_x509_key( cred, cert_list, cert_list_size, key));
659 void certificate_credentials::set_x509_trust ( gnutls_x509_crt_t * ca_list, int ca_list_size)
661 RETWRAP( gnutls_certificate_set_x509_trust( cred, ca_list, ca_list_size));
664 void certificate_credentials::set_x509_crl ( gnutls_x509_crl_t * crl_list, int crl_list_size)
666 RETWRAP( gnutls_certificate_set_x509_crl( cred, crl_list, crl_list_size));
669 void certificate_server_credentials::set_retrieve_function( gnutls_certificate_server_retrieve_function* func)
671 gnutls_certificate_server_set_retrieve_function( cred, func);
674 void certificate_client_credentials::set_retrieve_function( gnutls_certificate_client_retrieve_function* func)
676 gnutls_certificate_client_set_retrieve_function( cred, func);
679 // SRP
681 srp_server_credentials::srp_server_credentials() : credentials(GNUTLS_CRD_SRP)
683 RETWRAP(gnutls_srp_allocate_server_credentials( &cred));
684 set_ptr(cred);
687 srp_server_credentials::~srp_server_credentials()
689 gnutls_srp_free_server_credentials( cred);
692 srp_client_credentials::srp_client_credentials() : credentials(GNUTLS_CRD_SRP)
694 RETWRAP(gnutls_srp_allocate_client_credentials( &cred));
695 set_ptr(cred);
698 srp_client_credentials::~srp_client_credentials()
700 gnutls_srp_free_client_credentials( cred);
703 void srp_client_credentials::set_credentials( const char* username, const char* password)
705 RETWRAP(gnutls_srp_set_client_credentials (cred, username, password));
708 void srp_server_credentials::set_credentials_file (
709 const char *password_file, const char *password_conf_file)
711 RETWRAP( gnutls_srp_set_server_credentials_file( cred, password_file, password_conf_file));
715 void srp_server_credentials::set_credentials_function(gnutls_srp_server_credentials_function * func)
717 gnutls_srp_set_server_credentials_function( cred, func);
720 void srp_client_credentials::set_credentials_function(gnutls_srp_client_credentials_function * func)
722 gnutls_srp_set_client_credentials_function( cred, func);
725 // PSK
727 psk_server_credentials::psk_server_credentials() : credentials(GNUTLS_CRD_PSK)
729 RETWRAP(gnutls_psk_allocate_server_credentials( &cred));
730 set_ptr(cred);
733 psk_server_credentials::~psk_server_credentials()
735 gnutls_psk_free_server_credentials( cred);
738 void psk_server_credentials::set_credentials_file(const char* password_file)
740 RETWRAP(gnutls_psk_set_server_credentials_file( cred, password_file));
743 void psk_server_credentials::set_credentials_function(gnutls_psk_server_credentials_function * func)
745 gnutls_psk_set_server_credentials_function( cred, func);
748 void psk_server_credentials::set_dh_params( const dh_params &params)
750 gnutls_psk_set_server_dh_params( cred, params.get_params_t());
753 void psk_server_credentials::set_params_function(gnutls_params_function * func)
755 gnutls_psk_set_server_params_function (cred, func);
760 psk_client_credentials::psk_client_credentials() : credentials(GNUTLS_CRD_PSK)
762 RETWRAP(gnutls_psk_allocate_client_credentials( &cred));
763 set_ptr(cred);
766 psk_client_credentials::~psk_client_credentials()
768 gnutls_psk_free_client_credentials( cred);
771 void psk_client_credentials::set_credentials(const char* username,
772 const gnutls_datum_t& key, gnutls_psk_key_flags flags)
774 RETWRAP(gnutls_psk_set_client_credentials( cred, username, &key, flags));
777 void psk_client_credentials::set_credentials_function(gnutls_psk_client_credentials_function * func)
779 gnutls_psk_set_client_credentials_function( cred, func);
783 credentials::credentials(gnutls_credentials_type_t t) : type(t), cred(NULL)
787 gnutls_credentials_type_t credentials::get_type() const
789 return type;
792 void* credentials::ptr() const
794 return cred;
798 void credentials::set_ptr(void* ptr)
800 cred = ptr;
805 exception::exception( int x)
807 retcode = x;
810 int exception::get_code()
812 return retcode;
815 const char* exception::what() const throw()
817 return gnutls_strerror(retcode);
823 dh_params::dh_params()
825 RETWRAP(gnutls_dh_params_init( &params));
828 dh_params::~dh_params()
830 gnutls_dh_params_deinit(params);
833 void dh_params::import_raw( const gnutls_datum_t & prime,
834 const gnutls_datum_t & generator)
836 RETWRAP( gnutls_dh_params_import_raw( params, &prime, &generator));
839 void dh_params::import_pkcs3( const gnutls_datum_t & pkcs3_params,
840 gnutls_x509_crt_fmt_t format)
842 RETWRAP(gnutls_dh_params_import_pkcs3( params, &pkcs3_params, format));
845 void dh_params::generate( unsigned int bits)
847 RETWRAP(gnutls_dh_params_generate2( params, bits));
850 void dh_params::export_pkcs3( gnutls_x509_crt_fmt_t format, unsigned char *params_data, size_t * params_data_size)
852 RETWRAP( gnutls_dh_params_export_pkcs3( params, format, params_data, params_data_size));
855 void dh_params::export_raw( gnutls_datum_t& prime, gnutls_datum_t &generator)
857 RETWRAP( gnutls_dh_params_export_raw( params, &prime, &generator, NULL));
860 gnutls_dh_params_t dh_params::get_params_t() const
862 return params;
865 dh_params & dh_params::operator=(const dh_params& src)
867 dh_params* dst = new dh_params;
868 int ret;
870 ret = gnutls_dh_params_cpy( dst->params, src.params);
872 if (ret < 0) {
873 delete dst;
874 throw(ret);
877 return *dst;
881 // RSA
883 rsa_params::rsa_params()
885 RETWRAP(gnutls_rsa_params_init( &params));
888 rsa_params::~rsa_params()
890 gnutls_rsa_params_deinit(params);
893 void rsa_params::import_pkcs1( const gnutls_datum_t & pkcs1_params,
894 gnutls_x509_crt_fmt_t format)
896 RETWRAP(gnutls_rsa_params_import_pkcs1( params, &pkcs1_params, format));
899 void rsa_params::generate( unsigned int bits)
901 RETWRAP(gnutls_rsa_params_generate2( params, bits));
904 void rsa_params::export_pkcs1( gnutls_x509_crt_fmt_t format, unsigned char *params_data, size_t * params_data_size)
906 RETWRAP( gnutls_rsa_params_export_pkcs1( params, format, params_data, params_data_size));
909 gnutls_rsa_params_t rsa_params::get_params_t() const
911 return params;
914 rsa_params & rsa_params::operator=(const rsa_params& src)
916 rsa_params* dst = new rsa_params;
917 int ret;
919 ret = gnutls_rsa_params_cpy( dst->params, src.params);
921 if (ret < 0)
922 delete dst;
923 throw(ret);
925 return *dst;
928 void rsa_params::import_raw( const gnutls_datum_t & m,
929 const gnutls_datum_t & e,
930 const gnutls_datum_t & d,
931 const gnutls_datum_t & p,
932 const gnutls_datum_t & q,
933 const gnutls_datum_t & u)
936 RETWRAP(gnutls_rsa_params_import_raw ( params, &m, &e, &d, &p, &q, &u));
940 void rsa_params::export_raw( gnutls_datum_t & m, gnutls_datum_t & e,
941 gnutls_datum_t & d, gnutls_datum_t & p,
942 gnutls_datum_t & q, gnutls_datum_t & u)
944 RETWRAP( gnutls_rsa_params_export_raw ( params, &m, &e, &d, &p, &q, &u, NULL));
947 } // namespace gnutls