summaryrefslogtreecommitdiff
path: root/lib/gnutlsxx.cpp
blob: 98f12a1358af48681879b03822e34508c57e5329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
/*
 * Copyright (C) 2006-2012 Free Software Foundation, Inc.
 *
 * Author: Nikos Mavrogiannopoulos
 *
 * This file is part of GnuTLS.
 *
 * The GnuTLS is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
 *
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <gnutls/gnutlsxx.h>

namespace gnutls
{

  inline static int RETWRAP (int ret)
  {
    if (ret < 0)
      throw (exception (ret));
    return ret;
  }

  session::session (unsigned int flags)
  {
    RETWRAP (gnutls_init (&s, flags));
  }

  session::~session ()
  {
    gnutls_deinit (s);
  }

  gnutls_session_t session::ptr()
  {
    return s;
  }

  int session::bye (gnutls_close_request_t how)
  {
    return RETWRAP (gnutls_bye (s, how));
  }

  int session::handshake ()
  {
    return RETWRAP (gnutls_handshake (s));
  }

  server_session::server_session ():session (GNUTLS_SERVER)
  {
  }

  server_session::server_session (int flags):session (GNUTLS_SERVER |
			      (flags & ~GNUTLS_CLIENT))
  {
  }

  server_session::~server_session ()
  {
  }

  int server_session::rehandshake ()
  {
    return RETWRAP (gnutls_rehandshake (s));
  }

  gnutls_alert_description_t session::get_alert () const
  {
    return gnutls_alert_get (s);
  }

  int session::send_alert (gnutls_alert_level_t level,
			   gnutls_alert_description_t desc)
  {
    return RETWRAP (gnutls_alert_send (s, level, desc));
  }

  int session::send_appropriate_alert (int err)
  {
    return RETWRAP (gnutls_alert_send_appropriate (s, err));
  }

  gnutls_cipher_algorithm_t session::get_cipher () const
  {
    return gnutls_cipher_get (s);
  }

  gnutls_kx_algorithm_t session::get_kx () const
  {
    return gnutls_kx_get (s);
  }

  gnutls_mac_algorithm_t session::get_mac () const
  {
    return gnutls_mac_get (s);
  }

  gnutls_compression_method_t session::get_compression () const
  {
    return gnutls_compression_get (s);
  }

  gnutls_certificate_type_t session::get_certificate_type () const
  {
    return gnutls_certificate_type_get (s);
  }

  void session::set_private_extensions (bool allow)
  {
    gnutls_handshake_set_private_extensions (s, (int) allow);
  }

  gnutls_handshake_description_t session::get_handshake_last_out () const
  {
    return gnutls_handshake_get_last_out (s);
  }

  gnutls_handshake_description_t session::get_handshake_last_in () const
  {
    return gnutls_handshake_get_last_in (s);
  }

  ssize_t session::send (const void *data, size_t sizeofdata)
  {
    return RETWRAP (gnutls_record_send (s, data, sizeofdata));
  }

  ssize_t session::recv (void *data, size_t sizeofdata)
  {
    return RETWRAP (gnutls_record_recv (s, data, sizeofdata));
  }

  bool session::get_record_direction () const
  {
    return gnutls_record_get_direction (s);
  }

  // maximum packet size
  size_t session::get_max_size () const
  {
    return gnutls_record_get_max_size (s);
  }

  void session::set_max_size (size_t size)
  {
    RETWRAP (gnutls_record_set_max_size (s, size));
  }

  size_t session::check_pending () const
  {
    return gnutls_record_check_pending (s);
  }


  void session::prf (size_t label_size, const char *label,
		     int server_random_first,
		     size_t extra_size, const char *extra,
		     size_t outsize, char *out)
  {
    RETWRAP (gnutls_prf (s, label_size, label, server_random_first,
			 extra_size, extra, outsize, out));
  }

  void session::prf_raw (size_t label_size, const char *label,
			 size_t seed_size, const char *seed,
			 size_t outsize, char *out)
  {
    RETWRAP (gnutls_prf_raw
	     (s, label_size, label, seed_size, seed, outsize, out));
  }


/* if you just want some defaults, use the following.
 */
  void session::set_priority (const char *prio, const char **err_pos)
  {
    RETWRAP (gnutls_priority_set_direct (s, prio, err_pos));
  }

  void session::set_priority (gnutls_priority_t p)
  {
    RETWRAP (gnutls_priority_set (s, p));
  }

  gnutls_protocol_t session::get_protocol_version () const
  {
    return gnutls_protocol_get_version (s);
  }

  void session::set_data (const void *session_data, size_t session_data_size)
  {
    RETWRAP (gnutls_session_set_data (s, session_data, session_data_size));
  }

  void session::get_data (void *session_data, size_t * session_data_size) const
  {
    RETWRAP (gnutls_session_get_data (s, session_data, session_data_size));
  }

  void session::get_data (gnutls_session_t session, gnutls_datum_t & data) const
  {
    RETWRAP (gnutls_session_get_data2 (s, &data));

  }

  void session::get_id (void *session_id, size_t * session_id_size) const
  {
    RETWRAP (gnutls_session_get_id (s, session_id, session_id_size));
  }

  bool session::is_resumed () const
  {
    int ret = gnutls_session_is_resumed (s);

    return (ret != 0);
  }

  bool session::get_peers_certificate (std::vector < gnutls_datum_t >
				       &out_certs) const
  {
    const gnutls_datum_t *certs;
    unsigned int certs_size;

    certs = gnutls_certificate_get_peers (s, &certs_size);

    if (certs == NULL)
      return false;

    for (unsigned int i = 0; i < certs_size; i++)
      out_certs.push_back (certs[i]);

    return true;
  }

  bool session::get_peers_certificate (const gnutls_datum_t ** certs,
				       unsigned int *certs_size) const
  {
    *certs = gnutls_certificate_get_peers (s, certs_size);

    if (*certs == NULL)
      return false;
    return true;
  }

  void session::get_our_certificate (gnutls_datum_t & cert) const
  {
    const gnutls_datum_t *d;

    d = gnutls_certificate_get_ours (s);
    if (d == NULL)
      throw (exception (GNUTLS_E_INVALID_REQUEST));

    cert = *d;
  }

  time_t session::get_peers_certificate_activation_time () const
  {
    return gnutls_certificate_activation_time_peers (s);
  }

  time_t session::get_peers_certificate_expiration_time () const
  {
    return gnutls_certificate_expiration_time_peers (s);
  }
  void session::verify_peers_certificate (unsigned int &status) const
  {
    RETWRAP (gnutls_certificate_verify_peers2 (s, &status));
  }


  client_session::client_session ():session (GNUTLS_CLIENT)
  {
  }

  client_session::client_session (int flags):session (GNUTLS_CLIENT |
				  (flags & ~GNUTLS_SERVER))
  {
  }

  client_session::~client_session ()
  {
  }

// client session
  void client_session::set_verify_cert (const char *hostname, unsigned flags)
  {
    gnutls_session_set_verify_cert(s, hostname, flags);
  }

  void client_session::set_server_name (gnutls_server_name_type_t type,
					const void *name, size_t name_length)
  {
    RETWRAP (gnutls_server_name_set (s, type, name, name_length));
  }

  bool client_session::get_request_status ()
  {
    return RETWRAP (gnutls_certificate_client_get_request_status (s));
  }

// server_session
  void server_session::get_server_name (void *data, size_t * data_length,
					unsigned int *type,
					unsigned int indx) const
  {
    RETWRAP (gnutls_server_name_get (s, data, data_length, type, indx));
  }

// internal DB stuff
  static int store_function (void *_db, gnutls_datum_t key,
			     gnutls_datum_t data)
  {
    try
    {
      DB *db = static_cast < DB * >(_db);

      if (db->store (key, data) == false)
	return -1;
    }
    catch (...)
    {
      return -1;
    }

    return 0;
  }

  const static gnutls_datum_t null_datum = { NULL, 0 };

  static gnutls_datum_t retrieve_function (void *_db, gnutls_datum_t key)
  {
    gnutls_datum_t data;

    try
    {
      DB *db = static_cast < DB * >(_db);

      if (db->retrieve (key, data) == false)
	return null_datum;

    }
    catch (...)
    {
      return null_datum;
    }

    return data;
  }

  static int remove_function (void *_db, gnutls_datum_t key)
  {
    try
    {
      DB *db = static_cast < DB * >(_db);

      if (db->remove (key) == false)
	return -1;
    }
    catch (...)
    {
      return -1;
    }

    return 0;
  }

  void server_session::set_db (const DB & db)
  {
    gnutls_db_set_ptr (s, const_cast < DB * >(&db));
    gnutls_db_set_store_function (s, store_function);
    gnutls_db_set_retrieve_function (s, retrieve_function);
    gnutls_db_set_remove_function (s, remove_function);
  }

  void server_session::set_db_cache_expiration (unsigned int seconds)
  {
    gnutls_db_set_cache_expiration (s, seconds);
  }

  void server_session::db_remove () const
  {
    gnutls_db_remove_session (s);
  }

  bool server_session::db_check_entry (gnutls_datum_t & session_data) const
  {
    int ret = gnutls_db_check_entry (s, session_data);

    if (ret != 0)
      return true;
    return false;
  }

  void session::set_max_handshake_packet_length (size_t max)
  {
    gnutls_handshake_set_max_packet_length (s, max);
  }

  void session::clear_credentials ()
  {
    gnutls_credentials_clear (s);
  }

  void session::set_credentials (credentials & cred)
  {
    RETWRAP (gnutls_credentials_set (s, cred.get_type (), cred.ptr ()));
  }

  const char *server_session::get_srp_username () const
  {
#ifdef ENABLE_SRP
    return gnutls_srp_server_get_username (s);
#else
    return NULL;
#endif
  }

  const char *server_session::get_psk_username () const
  {
    return gnutls_psk_server_get_username (s);
  }


  void session::set_transport_ptr (gnutls_transport_ptr_t ptr)
  {
    gnutls_transport_set_ptr (s, ptr);
  }

  void session::set_transport_ptr (gnutls_transport_ptr_t recv_ptr,
				   gnutls_transport_ptr_t send_ptr)
  {
    gnutls_transport_set_ptr2 (s, recv_ptr, send_ptr);
  }


  gnutls_transport_ptr_t session::get_transport_ptr () const
  {
    return gnutls_transport_get_ptr (s);
  }

  void session::get_transport_ptr (gnutls_transport_ptr_t & recv_ptr,
				   gnutls_transport_ptr_t & send_ptr) const
  {
    gnutls_transport_get_ptr2 (s, &recv_ptr, &send_ptr);
  }

  void session::set_transport_lowat (size_t num)
  {
    throw (exception (GNUTLS_E_UNIMPLEMENTED_FEATURE));
  }

  void session::set_transport_push_function (gnutls_push_func push_func)
  {
    gnutls_transport_set_push_function (s, push_func);
  }

  void session::set_transport_vec_push_function (gnutls_vec_push_func vec_push_func)
  {
    gnutls_transport_set_vec_push_function (s, vec_push_func);
  }

  void session::set_transport_pull_function (gnutls_pull_func pull_func)
  {
    gnutls_transport_set_pull_function (s, pull_func);
  }

  void session::set_transport_pull_timeout_function (gnutls_pull_timeout_func pull_timeout_func)
  {
    gnutls_transport_set_pull_timeout_function (s, pull_timeout_func);
  }

  void session::set_user_ptr (void *ptr)
  {
    gnutls_session_set_ptr (s, ptr);
  }

  void *session::get_user_ptr () const
  {
    return gnutls_session_get_ptr (s);
  }

  void session::send_openpgp_cert (gnutls_openpgp_crt_status_t status)
  {
#ifdef ENABLE_OPENPGP
    gnutls_openpgp_send_cert (s, status);
#endif
  }

  void session::set_dh_prime_bits (unsigned int bits)
  {
    gnutls_dh_set_prime_bits (s, bits);
  }

  unsigned int session::get_dh_secret_bits () const
  {
    return RETWRAP (gnutls_dh_get_secret_bits (s));
  }

  unsigned int session::get_dh_peers_public_bits () const
  {
    return RETWRAP (gnutls_dh_get_peers_public_bits (s));
  }

  unsigned int session::get_dh_prime_bits () const
  {
    return RETWRAP (gnutls_dh_get_prime_bits (s));
  }

  void session::get_dh_group (gnutls_datum_t & gen,
			      gnutls_datum_t & prime) const
  {
    RETWRAP (gnutls_dh_get_group (s, &gen, &prime));
  }

  void session::get_dh_pubkey (gnutls_datum_t & raw_key) const
  {
    RETWRAP (gnutls_dh_get_pubkey (s, &raw_key));
  }

#ifdef ENABLE_RSA_EXPORT
  void session::get_rsa_export_pubkey (gnutls_datum_t & exponent,
				       gnutls_datum_t & modulus) const
  {
    RETWRAP (gnutls_rsa_export_get_pubkey (s, &exponent, &modulus));
  }

  unsigned int session::get_rsa_export_modulus_bits () const
  {
    return RETWRAP (gnutls_rsa_export_get_modulus_bits (s));
  }

  void certificate_credentials::
    set_rsa_export_params (const rsa_params & params)
  {
    gnutls_certificate_set_rsa_export_params (cred, params.get_params_t ());
  }
#endif

  void server_session::
    set_certificate_request (gnutls_certificate_request_t req)
  {
    gnutls_certificate_server_set_request (s, req);
  }

  gnutls_credentials_type_t session::get_auth_type () const
  {
    return gnutls_auth_get_type (s);
  }

  gnutls_credentials_type_t session::get_server_auth_type () const
  {
    return gnutls_auth_server_get_type (s);
  }

  gnutls_credentials_type_t session::get_client_auth_type () const
  {
    return gnutls_auth_client_get_type (s);
  }


  certificate_credentials::~certificate_credentials ()
  {
    gnutls_certificate_free_credentials (cred);
  }

  certificate_credentials::certificate_credentials ():credentials
    (GNUTLS_CRD_CERTIFICATE)
  {
    RETWRAP (gnutls_certificate_allocate_credentials (&cred));
    set_ptr (cred);
  }

  void certificate_server_credentials::
    set_params_function (gnutls_params_function * func)
  {
    gnutls_certificate_set_params_function (cred, func);
  }

  anon_server_credentials::anon_server_credentials ():credentials
    (GNUTLS_CRD_ANON)
  {
    RETWRAP (gnutls_anon_allocate_server_credentials (&cred));
    set_ptr (cred);
  }

  anon_server_credentials::~anon_server_credentials ()
  {
    gnutls_anon_free_server_credentials (cred);
  }

  void anon_server_credentials::set_dh_params (const dh_params & params)
  {
    gnutls_anon_set_server_dh_params (cred, params.get_params_t ());
  }

  void anon_server_credentials::set_params_function (gnutls_params_function *
						     func)
  {
    gnutls_anon_set_server_params_function (cred, func);
  }

  anon_client_credentials::anon_client_credentials ():credentials
    (GNUTLS_CRD_ANON)
  {
    RETWRAP (gnutls_anon_allocate_client_credentials (&cred));
    set_ptr (cred);
  }

  anon_client_credentials::~anon_client_credentials ()
  {
    gnutls_anon_free_client_credentials (cred);
  }

  void certificate_credentials::free_keys ()
  {
    gnutls_certificate_free_keys (cred);
  }

  void certificate_credentials::free_cas ()
  {
    gnutls_certificate_free_cas (cred);
  }

  void certificate_credentials::free_ca_names ()
  {
    gnutls_certificate_free_ca_names (cred);
  }

  void certificate_credentials::free_crls ()
  {
    gnutls_certificate_free_crls (cred);
  }


  void certificate_credentials::set_dh_params (const dh_params & params)
  {
    gnutls_certificate_set_dh_params (cred, params.get_params_t ());
  }

  void certificate_credentials::set_verify_flags (unsigned int flags)
  {
    gnutls_certificate_set_verify_flags (cred, flags);
  }

  void certificate_credentials::set_verify_limits (unsigned int max_bits,
						   unsigned int max_depth)
  {
    gnutls_certificate_set_verify_limits (cred, max_bits, max_depth);
  }

  void certificate_credentials::set_x509_trust_file (const char *cafile,
						     gnutls_x509_crt_fmt_t
						     type)
  {
    RETWRAP (gnutls_certificate_set_x509_trust_file (cred, cafile, type));
  }

  void certificate_credentials::set_x509_trust (const gnutls_datum_t & CA,
						gnutls_x509_crt_fmt_t type)
  {
    RETWRAP (gnutls_certificate_set_x509_trust_mem (cred, &CA, type));
  }


  void certificate_credentials::set_x509_crl_file (const char *crlfile,
						   gnutls_x509_crt_fmt_t type)
  {
    RETWRAP (gnutls_certificate_set_x509_crl_file (cred, crlfile, type));
  }

  void certificate_credentials::set_x509_crl (const gnutls_datum_t & CRL,
					      gnutls_x509_crt_fmt_t type)
  {
    RETWRAP (gnutls_certificate_set_x509_crl_mem (cred, &CRL, type));
  }

  void certificate_credentials::set_x509_key_file (const char *certfile,
						   const char *keyfile,
						   gnutls_x509_crt_fmt_t type)
  {
    RETWRAP (gnutls_certificate_set_x509_key_file
	     (cred, certfile, keyfile, type));
  }

  void certificate_credentials::set_x509_key (const gnutls_datum_t & CERT,
					      const gnutls_datum_t & KEY,
					      gnutls_x509_crt_fmt_t type)
  {
    RETWRAP (gnutls_certificate_set_x509_key_mem (cred, &CERT, &KEY, type));
  }

  void certificate_credentials::
    set_simple_pkcs12_file (const char *pkcs12file,
			    gnutls_x509_crt_fmt_t type, const char *password)
  {
    RETWRAP (gnutls_certificate_set_x509_simple_pkcs12_file
	     (cred, pkcs12file, type, password));
  }

  void certificate_credentials::set_x509_key (gnutls_x509_crt_t * cert_list,
					      int cert_list_size,
					      gnutls_x509_privkey_t key)
  {
    RETWRAP (gnutls_certificate_set_x509_key
	     (cred, cert_list, cert_list_size, key));
  }

  void certificate_credentials::set_x509_trust (gnutls_x509_crt_t * ca_list,
						int ca_list_size)
  {
    RETWRAP (gnutls_certificate_set_x509_trust (cred, ca_list, ca_list_size));
  }

  void certificate_credentials::set_x509_crl (gnutls_x509_crl_t * crl_list,
					      int crl_list_size)
  {
    RETWRAP (gnutls_certificate_set_x509_crl (cred, crl_list, crl_list_size));
  }

  void certificate_credentials::
    set_retrieve_function (gnutls_certificate_retrieve_function * func)
  {
    gnutls_certificate_set_retrieve_function (cred, func);
  }

// SRP

#ifdef ENABLE_SRP

  srp_server_credentials::srp_server_credentials ():credentials
    (GNUTLS_CRD_SRP)
  {
    RETWRAP (gnutls_srp_allocate_server_credentials (&cred));
    set_ptr (cred);
  }

  srp_server_credentials::~srp_server_credentials ()
  {
    gnutls_srp_free_server_credentials (cred);
  }

  srp_client_credentials::srp_client_credentials ():credentials
    (GNUTLS_CRD_SRP)
  {
    RETWRAP (gnutls_srp_allocate_client_credentials (&cred));
    set_ptr (cred);
  }

  srp_client_credentials::~srp_client_credentials ()
  {
    gnutls_srp_free_client_credentials (cred);
  }

  void srp_client_credentials::set_credentials (const char *username,
						const char *password)
  {
    RETWRAP (gnutls_srp_set_client_credentials (cred, username, password));
  }

  void srp_server_credentials::
    set_credentials_file (const char *password_file,
			  const char *password_conf_file)
  {
    RETWRAP (gnutls_srp_set_server_credentials_file
	     (cred, password_file, password_conf_file));
  }

  void srp_server_credentials::
    set_credentials_function (gnutls_srp_server_credentials_function * func)
  {
    gnutls_srp_set_server_credentials_function (cred, func);
  }

  void srp_client_credentials::
    set_credentials_function (gnutls_srp_client_credentials_function * func)
  {
    gnutls_srp_set_client_credentials_function (cred, func);
  }

#endif /* ENABLE_SRP */

// PSK

psk_server_credentials::psk_server_credentials ():credentials
    (GNUTLS_CRD_PSK)
  {
    RETWRAP (gnutls_psk_allocate_server_credentials (&cred));
    set_ptr (cred);
  }

  psk_server_credentials::~psk_server_credentials ()
  {
    gnutls_psk_free_server_credentials (cred);
  }

  void psk_server_credentials::
    set_credentials_file (const char *password_file)
  {
    RETWRAP (gnutls_psk_set_server_credentials_file (cred, password_file));
  }

  void psk_server_credentials::
    set_credentials_function (gnutls_psk_server_credentials_function * func)
  {
    gnutls_psk_set_server_credentials_function (cred, func);
  }

  void psk_server_credentials::set_dh_params (const dh_params & params)
  {
    gnutls_psk_set_server_dh_params (cred, params.get_params_t ());
  }

  void psk_server_credentials::set_params_function (gnutls_params_function *
						    func)
  {
    gnutls_psk_set_server_params_function (cred, func);
  }

  psk_client_credentials::psk_client_credentials ():credentials
    (GNUTLS_CRD_PSK)
  {
    RETWRAP (gnutls_psk_allocate_client_credentials (&cred));
    set_ptr (cred);
  }

  psk_client_credentials::~psk_client_credentials ()
  {
    gnutls_psk_free_client_credentials (cred);
  }

  void psk_client_credentials::set_credentials (const char *username,
						const gnutls_datum_t & key,
						gnutls_psk_key_flags flags)
  {
    RETWRAP (gnutls_psk_set_client_credentials (cred, username, &key, flags));
  }

  void psk_client_credentials::
    set_credentials_function (gnutls_psk_client_credentials_function * func)
  {
    gnutls_psk_set_client_credentials_function (cred, func);
  }

  credentials::credentials (gnutls_credentials_type_t t):type (t),
    cred (NULL)
  {
  }

  gnutls_credentials_type_t credentials::get_type () const
  {
    return type;
  }

  void *credentials::ptr () const
  {
    return cred;
  }

  void credentials::set_ptr (void *ptr)
  {
    cred = ptr;
  }

  exception::exception (int x)
  {
    retcode = x;
  }

  int exception::get_code ()
  {
    return retcode;
  }

  const char *exception::what () const throw ()
  {
    return gnutls_strerror (retcode);
  }

  dh_params::dh_params ()
  {
    RETWRAP (gnutls_dh_params_init (&params));
  }

  dh_params::~dh_params ()
  {
    gnutls_dh_params_deinit (params);
  }

  void dh_params::import_raw (const gnutls_datum_t & prime,
			      const gnutls_datum_t & generator)
  {
    RETWRAP (gnutls_dh_params_import_raw (params, &prime, &generator));
  }

  void dh_params::import_pkcs3 (const gnutls_datum_t & pkcs3_params,
				gnutls_x509_crt_fmt_t format)
  {
    RETWRAP (gnutls_dh_params_import_pkcs3 (params, &pkcs3_params, format));
  }

  void dh_params::generate (unsigned int bits)
  {
    RETWRAP (gnutls_dh_params_generate2 (params, bits));
  }

  void dh_params::export_pkcs3 (gnutls_x509_crt_fmt_t format,
				unsigned char *params_data,
				size_t * params_data_size)
  {
    RETWRAP (gnutls_dh_params_export_pkcs3
	     (params, format, params_data, params_data_size));
  }

  void dh_params::export_raw (gnutls_datum_t & prime,
			      gnutls_datum_t & generator)
  {
    RETWRAP (gnutls_dh_params_export_raw (params, &prime, &generator, NULL));
  }

  gnutls_dh_params_t dh_params::get_params_t () const
  {
    return params;
  }

  dh_params & dh_params::operator= (const dh_params & src)
  {
    dh_params *dst = new dh_params;
    int ret;

    ret = gnutls_dh_params_cpy (dst->params, src.params);

    if (ret < 0)
      {
	delete dst;
        throw (exception (ret));
      }

    return *dst;
  }

// RSA

#ifdef ENABLE_RSA_EXPORT
  rsa_params::rsa_params ()
  {
    RETWRAP (gnutls_rsa_params_init (&params));
  }

  rsa_params::~rsa_params ()
  {
    gnutls_rsa_params_deinit (params);
  }

  void rsa_params::import_pkcs1 (const gnutls_datum_t & pkcs1_params,
				 gnutls_x509_crt_fmt_t format)
  {
    RETWRAP (gnutls_rsa_params_import_pkcs1 (params, &pkcs1_params, format));
  }

  void rsa_params::generate (unsigned int bits)
  {
    RETWRAP (gnutls_rsa_params_generate2 (params, bits));
  }

  void rsa_params::export_pkcs1 (gnutls_x509_crt_fmt_t format,
				 unsigned char *params_data,
				 size_t * params_data_size)
  {
    RETWRAP (gnutls_rsa_params_export_pkcs1
	     (params, format, params_data, params_data_size));
  }

  gnutls_rsa_params_t rsa_params::get_params_t () const
  {
    return params;
  }

  rsa_params & rsa_params::operator= (const rsa_params & src)
  {
    rsa_params *dst = new rsa_params;
    int ret;

    ret = gnutls_rsa_params_cpy (dst->params, src.params);

    if (ret < 0)
      {
	delete dst;
        throw (exception (ret));
      }

    return *dst;
  }

  void rsa_params::import_raw (const gnutls_datum_t & m,
			       const gnutls_datum_t & e,
			       const gnutls_datum_t & d,
			       const gnutls_datum_t & p,
			       const gnutls_datum_t & q,
			       const gnutls_datum_t & u)
  {

    RETWRAP (gnutls_rsa_params_import_raw (params, &m, &e, &d, &p, &q, &u));
  }


  void rsa_params::export_raw (gnutls_datum_t & m, gnutls_datum_t & e,
			       gnutls_datum_t & d, gnutls_datum_t & p,
			       gnutls_datum_t & q, gnutls_datum_t & u)
  {
    RETWRAP (gnutls_rsa_params_export_raw
	     (params, &m, &e, &d, &p, &q, &u, NULL));
  }
#endif
}				// namespace gnutls