summaryrefslogtreecommitdiff
path: root/extra/gnutls_openssl.c
blob: e8cbe675d366b29245f16eb33cab7cecacd9ca9b (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
/*
 * Copyright (C) 2004-2012 Free Software Foundation, Inc.
 * Copyright (c) 2002 Andrew McDonald <andrew@mcdonald.org.uk>
 *
 * This file is part of GnuTLS-EXTRA.
 *
 * GnuTLS-extra is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *               
 * GnuTLS-extra 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 General Public License for more details.
 *                               
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <config.h>

#include <gnutls/gnutls.h>
#include <openssl_compat.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../lib/gnutls_int.h"
#include "../lib/random.h"
#include "../lib/gnutls_hash_int.h"

/* In win32 X509_NAME is defined in wincrypt.h.
 * undefine it to avoid the conflict with openssl.h.
 */
#ifdef X509_NAME
# undef X509_NAME
#endif
#include <gnutls/openssl.h>

/* Gnulib re-defines shutdown on mingw.  We only use it as a variable
   name, so restore the original name. */
#undef shutdown

/* XXX: See lib/gnutls_int.h. */
#define GNUTLS_POINTER_TO_INT(_) ((int) GNUTLS_POINTER_TO_INT_CAST (_))
#define GNUTLS_INT_TO_POINTER(_) ((void*) GNUTLS_POINTER_TO_INT_CAST (_))

/* WARNING: Error functions aren't currently thread-safe */

static int last_error = 0;

/* Library initialisation functions */

int
SSL_library_init (void)
{
  gnutls_global_init ();
  /* NB: we haven't got anywhere to call gnutls_global_deinit() */
  return 1;
}

void
OpenSSL_add_all_algorithms (void)
{
}


/* SSL_CTX structure handling */

SSL_CTX *
SSL_CTX_new (SSL_METHOD * method)
{
  SSL_CTX *ctx;

  ctx = (SSL_CTX *) calloc (1, sizeof (SSL_CTX));
  ctx->method = method;

  return ctx;
}

void
SSL_CTX_free (SSL_CTX * ctx)
{
  free (ctx->method);
  free (ctx);
}

int
SSL_CTX_set_default_verify_paths (SSL_CTX * ctx)
{
  return 0;
}

int
SSL_CTX_use_certificate_file (SSL_CTX * ctx, const char *certfile, int type)
{
  ctx->certfile = (char *) calloc (1, strlen (certfile) + 1);
  if (!ctx->certfile)
    return -1;
  memcpy (ctx->certfile, certfile, strlen (certfile));

  ctx->certfile_type = type;

  return 1;
}

int
SSL_CTX_use_PrivateKey_file (SSL_CTX * ctx, const char *keyfile, int type)
{
  ctx->keyfile = (char *) calloc (1, strlen (keyfile) + 1);
  if (!ctx->keyfile)
    return -1;
  memcpy (ctx->keyfile, keyfile, strlen (keyfile));

  ctx->keyfile_type = type;

  return 1;

}

void
SSL_CTX_set_verify (SSL_CTX * ctx, int verify_mode,
                    int (*verify_callback) (int, X509_STORE_CTX *))
{
  ctx->verify_mode = verify_mode;
  ctx->verify_callback = verify_callback;
}

unsigned long
SSL_CTX_set_options (SSL_CTX * ctx, unsigned long options)
{
  return (ctx->options |= options);
}

long
SSL_CTX_set_mode (SSL_CTX * ctx, long mode)
{
  return 0;
}

int
SSL_CTX_set_cipher_list (SSL_CTX * ctx, const char *list)
{
  /* FIXME: ignore this for the moment */
  /* We're going to have to parse the "list" string to do this */
  /* It is a string, which in its simplest form is something like
     "DES-CBC3-SHA:IDEA-CBC-MD5", but can be rather more complicated
     (see OpenSSL's ciphers(1) manpage for details) */

  return 1;
}


/* SSL_CTX statistics */

long
SSL_CTX_sess_number (SSL_CTX * ctx)
{
  return 0;
}

long
SSL_CTX_sess_connect (SSL_CTX * ctx)
{
  return 0;
}

long
SSL_CTX_sess_connect_good (SSL_CTX * ctx)
{
  return 0;
}

long
SSL_CTX_sess_connect_renegotiate (SSL_CTX * ctx)
{
  return 0;
}

long
SSL_CTX_sess_accept (SSL_CTX * ctx)
{
  return 0;
}

long
SSL_CTX_sess_accept_good (SSL_CTX * ctx)
{
  return 0;
}

long
SSL_CTX_sess_accept_renegotiate (SSL_CTX * ctx)
{
  return 0;
}

long
SSL_CTX_sess_hits (SSL_CTX * ctx)
{
  return 0;
}

long
SSL_CTX_sess_misses (SSL_CTX * ctx)
{
  return 0;
}

long
SSL_CTX_sess_timeouts (SSL_CTX * ctx)
{
  return 0;
}



/* SSL structure handling */

SSL *
SSL_new (SSL_CTX * ctx)
{
  SSL *ssl;
  int err;

  ssl = (SSL *) calloc (1, sizeof (SSL));
  if (!ssl)
    return NULL;

  err = gnutls_certificate_allocate_credentials (&ssl->gnutls_cred);
  if (err < 0)
    {
      last_error = err;
      free (ssl);
      return NULL;
    }

  gnutls_init (&ssl->gnutls_state, ctx->method->connend);

  gnutls_priority_set_direct (ssl->gnutls_state,
                                ctx->method->priority_string, NULL);

  gnutls_credentials_set (ssl->gnutls_state, GNUTLS_CRD_CERTIFICATE,
                          ssl->gnutls_cred);
  if (ctx->certfile)
    gnutls_certificate_set_x509_trust_file (ssl->gnutls_cred,
                                            ctx->certfile,
                                            ctx->certfile_type);
  if (ctx->keyfile)
    gnutls_certificate_set_x509_key_file (ssl->gnutls_cred,
                                          ctx->certfile, ctx->keyfile,
                                          ctx->keyfile_type);
  ssl->ctx = ctx;
  ssl->verify_mode = ctx->verify_mode;
  ssl->verify_callback = ctx->verify_callback;

  ssl->options = ctx->options;

  ssl->rfd = (gnutls_transport_ptr_t) - 1;
  ssl->wfd = (gnutls_transport_ptr_t) - 1;

  return ssl;
}

void
SSL_free (SSL * ssl)
{
  gnutls_certificate_free_credentials (ssl->gnutls_cred);
  gnutls_deinit (ssl->gnutls_state);
  free (ssl);
}

void
SSL_load_error_strings (void)
{
}

int
SSL_get_error (SSL * ssl, int ret)
{
  if (ret > 0)
    return SSL_ERROR_NONE;

  return SSL_ERROR_ZERO_RETURN;
}

int
SSL_set_fd (SSL * ssl, int fd)
{
  gnutls_transport_set_ptr (ssl->gnutls_state, GNUTLS_INT_TO_POINTER (fd));
  return 1;
}

int
SSL_set_rfd (SSL * ssl, int fd)
{
  ssl->rfd = GNUTLS_INT_TO_POINTER (fd);

  if (ssl->wfd != (gnutls_transport_ptr_t) - 1)
    gnutls_transport_set_ptr2 (ssl->gnutls_state, ssl->rfd, ssl->wfd);

  return 1;
}

int
SSL_set_wfd (SSL * ssl, int fd)
{
  ssl->wfd = GNUTLS_INT_TO_POINTER (fd);

  if (ssl->rfd != (gnutls_transport_ptr_t) - 1)
    gnutls_transport_set_ptr2 (ssl->gnutls_state, ssl->rfd, ssl->wfd);

  return 1;
}

void
SSL_set_bio (SSL * ssl, BIO * rbio, BIO * wbio)
{
  gnutls_transport_set_ptr2 (ssl->gnutls_state, rbio->fd, wbio->fd);
  /*    free(BIO); ? */
}

void
SSL_set_connect_state (SSL * ssl)
{
}

int
SSL_pending (SSL * ssl)
{
  return gnutls_record_check_pending (ssl->gnutls_state);
}

void
SSL_set_verify (SSL * ssl, int verify_mode,
                int (*verify_callback) (int, X509_STORE_CTX *))
{
  ssl->verify_mode = verify_mode;
  ssl->verify_callback = verify_callback;
}

const X509 *
SSL_get_peer_certificate (SSL * ssl)
{
  const gnutls_datum_t *cert_list;
  unsigned int cert_list_size = 0;

  cert_list = gnutls_certificate_get_peers (ssl->gnutls_state,
                                            &cert_list_size);

  return cert_list;
}

/* SSL connection open/close/read/write functions */

int
SSL_connect (SSL * ssl)
{
  X509_STORE_CTX *store;
  unsigned int cert_list_size = 0;
  int err;
  char x_priority[256];
  /* take options into account before connecting */

  memset (x_priority, 0, sizeof (x_priority));
  if (ssl->options & SSL_OP_NO_TLSv1)
    {
      snprintf(x_priority, sizeof(x_priority), "%s:-VERS-TLS1.0", ssl->ctx->method->priority_string);
      err = gnutls_priority_set_direct(ssl->gnutls_state, x_priority, NULL);
      if (err < 0)
        {
          last_error = err;
          return 0;
        }
    }

  err = gnutls_handshake (ssl->gnutls_state);
  ssl->last_error = err;

  if (err < 0)
    {
      last_error = err;
      return 0;
    }

  store = (X509_STORE_CTX *) calloc (1, sizeof (X509_STORE_CTX));
  store->ssl = ssl;
  store->cert_list = gnutls_certificate_get_peers (ssl->gnutls_state,
                                                   &cert_list_size);

  if (ssl->verify_callback)
    {
      ssl->verify_callback (1 /*FIXME*/, store);
    }
  ssl->state = SSL_ST_OK;

  err = store->error;
  free (store);

  /* FIXME: deal with error from callback */

  return 1;
}

int
SSL_accept (SSL * ssl)
{
  X509_STORE_CTX *store;
  unsigned int cert_list_size = 0;
  int err;
  char x_priority[256];
  /* take options into account before connecting */

  memset (x_priority, 0, sizeof (x_priority));
  if (ssl->options & SSL_OP_NO_TLSv1)
    {
      snprintf(x_priority, sizeof(x_priority), "%s:-VERS-TLS1.0", ssl->ctx->method->priority_string);
      err = gnutls_priority_set_direct(ssl->gnutls_state, x_priority, NULL);
      if (err < 0)
        {
          last_error = err;
          return 0;
        }
    }

  /* FIXME: dh params, do we want client cert? */

  err = gnutls_handshake (ssl->gnutls_state);
  ssl->last_error = err;

  if (err < 0)
    {
      last_error = err;
      return 0;
    }

  store = (X509_STORE_CTX *) calloc (1, sizeof (X509_STORE_CTX));
  store->ssl = ssl;
  store->cert_list = gnutls_certificate_get_peers (ssl->gnutls_state,
                                                   &cert_list_size);

  if (ssl->verify_callback)
    {
      ssl->verify_callback (1 /*FIXME*/, store);
    }
  ssl->state = SSL_ST_OK;

  err = store->error;
  free (store);

  /* FIXME: deal with error from callback */

  return 1;
}

int
SSL_shutdown (SSL * ssl)
{
  if (!ssl->shutdown)
    {
      gnutls_bye (ssl->gnutls_state, GNUTLS_SHUT_WR);
      ssl->shutdown++;
    }
  else
    {
      gnutls_bye (ssl->gnutls_state, GNUTLS_SHUT_RDWR);
      ssl->shutdown++;
    }

  /* FIXME */
  return 1;
}

int
SSL_read (SSL * ssl, void *buf, int len)
{
  int ret;

  ret = gnutls_record_recv (ssl->gnutls_state, buf, len);
  ssl->last_error = ret;

  if (ret < 0)
    {
      last_error = ret;
      return 0;
    }

  return ret;
}

int
SSL_write (SSL * ssl, const void *buf, int len)
{
  int ret;

  ret = gnutls_record_send (ssl->gnutls_state, buf, len);
  ssl->last_error = ret;

  if (ret < 0)
    {
      last_error = ret;
      return 0;
    }

  return ret;
}

int
SSL_want (SSL * ssl)
{
  return SSL_NOTHING;
}


/* SSL_METHOD functions */

SSL_METHOD *
SSLv23_client_method (void)
{
  SSL_METHOD *m;
  m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
  if (!m)
    return NULL;

  strcpy(m->priority_string, "NONE:+VERS-TLS1.0:+VERS-SSL3.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");

  m->connend = GNUTLS_CLIENT;

  return m;
}

SSL_METHOD *
SSLv23_server_method (void)
{
  SSL_METHOD *m;
  m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
  if (!m)
    return NULL;

  strcpy(m->priority_string, "NONE:+VERS-TLS1.0:+VERS-SSL3.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
  m->connend = GNUTLS_SERVER;

  return m;
}

SSL_METHOD *
SSLv3_client_method (void)
{
  SSL_METHOD *m;
  m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
  if (!m)
    return NULL;

  strcpy(m->priority_string, "NONE:+VERS-SSL3.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
  m->connend = GNUTLS_CLIENT;

  return m;
}

SSL_METHOD *
SSLv3_server_method (void)
{
  SSL_METHOD *m;
  m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
  if (!m)
    return NULL;

  strcpy(m->priority_string, "NONE:+VERS-SSL3.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
  m->connend = GNUTLS_SERVER;

  return m;
}

SSL_METHOD *
TLSv1_client_method (void)
{
  SSL_METHOD *m;
  m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
  if (!m)
    return NULL;

  strcpy(m->priority_string, "NONE:+VERS-TLS1.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
  m->connend = GNUTLS_CLIENT;

  return m;
}

SSL_METHOD *
TLSv1_server_method (void)
{
  SSL_METHOD *m;
  m = (SSL_METHOD *) calloc (1, sizeof (SSL_METHOD));
  if (!m)
    return NULL;

  strcpy(m->priority_string, "NONE:+VERS-TLS1.0:+CIPHER-ALL:+COMP-ALL:+RSA:+DHE-RSA:+DHE-DSS:+MAC-ALL");
  m->connend = GNUTLS_SERVER;

  return m;
}


/* SSL_CIPHER functions */

SSL_CIPHER *
SSL_get_current_cipher (SSL * ssl)
{
  if (!ssl)
    return NULL;

  ssl->ciphersuite.version = gnutls_protocol_get_version (ssl->gnutls_state);
  ssl->ciphersuite.cipher = gnutls_cipher_get (ssl->gnutls_state);
  ssl->ciphersuite.kx = gnutls_kx_get (ssl->gnutls_state);
  ssl->ciphersuite.mac = gnutls_mac_get (ssl->gnutls_state);
  ssl->ciphersuite.compression = gnutls_compression_get (ssl->gnutls_state);
  ssl->ciphersuite.cert = gnutls_certificate_type_get (ssl->gnutls_state);

  return &(ssl->ciphersuite);
}

const char *
SSL_CIPHER_get_name (SSL_CIPHER * cipher)
{
  if (!cipher)
    return ("NONE");

  return gnutls_cipher_suite_get_name (cipher->kx,
                                       cipher->cipher, cipher->mac);
}

int
SSL_CIPHER_get_bits (SSL_CIPHER * cipher, int *bits)
{
  int bit_result;

  if (!cipher)
    return 0;

  bit_result = (8 * gnutls_cipher_get_key_size (cipher->cipher));

  if (bits)
    *bits = bit_result;

  return bit_result;
}

const char *
SSL_CIPHER_get_version (SSL_CIPHER * cipher)
{
  const char *ret;

  if (!cipher)
    return ("(NONE)");

  ret = gnutls_protocol_get_name (cipher->version);
  if (ret)
    return ret;

  return ("unknown");
}

char *
SSL_CIPHER_description (SSL_CIPHER * cipher, char *buf, int size)
{
  char *tmpbuf;
  int tmpsize;
  int local_alloc;

  if (buf)
    {
      tmpbuf = buf;
      tmpsize = size;
      local_alloc = 0;
    }
  else
    {
      tmpbuf = (char *) malloc (128);
      tmpsize = 128;
      local_alloc = 1;
    }

  if (snprintf (tmpbuf, tmpsize, "%s %s %s %s",
                gnutls_protocol_get_name (cipher->version),
                gnutls_kx_get_name (cipher->kx),
                gnutls_cipher_get_name (cipher->cipher),
                gnutls_mac_get_name (cipher->mac)) == -1)
    {
      if (local_alloc)
        free (tmpbuf);
      return (char *) "Buffer too small";
    }

  return tmpbuf;
}


/* X509 functions */

X509_NAME *
X509_get_subject_name (const X509 * cert)
{
  gnutls_x509_dn *dn;
  dn = (gnutls_x509_dn *) calloc (1, sizeof (gnutls_x509_dn));
  if (gnutls_x509_extract_certificate_dn (cert, dn) < 0)
    {
      free (dn);
      return NULL;
    }
  return dn;
}

X509_NAME *
X509_get_issuer_name (const X509 * cert)
{
  gnutls_x509_dn *dn;
  dn = (gnutls_x509_dn *) calloc (1, sizeof (gnutls_x509_dn));
  if (gnutls_x509_extract_certificate_issuer_dn (cert, dn) < 0)
    {
      free (dn);
      return NULL;
    }
  return dn;
}

char *
X509_NAME_oneline (gnutls_x509_dn * name, char *buf, int len)
{
  /* XXX openssl allocates buffer if buf == NULL */
  if (!buf)
    return NULL;
  memset (buf, 0, len);

  snprintf (buf, len - 1,
            "C=%s, ST=%s, L=%s, O=%s, OU=%s, CN=%s/Email=%s",
            name->country, name->state_or_province_name,
            name->locality_name, name->organization,
            name->organizational_unit_name, name->common_name, name->email);
  return buf;
}

void
X509_free (const X509 * cert)
{
  /* only get certificates as const items */
}


/* BIO functions */

void
BIO_get_fd (gnutls_session_t gnutls_state, int *fd)
{
  gnutls_transport_ptr_t tmp = gnutls_transport_get_ptr (gnutls_state);
  *fd = GNUTLS_POINTER_TO_INT (tmp);
}

BIO *
BIO_new_socket (int sock, int close_flag)
{
  BIO *bio;

  bio = (BIO *) malloc (sizeof (BIO));
  if (!bio)
    return NULL;

  bio->fd = GNUTLS_INT_TO_POINTER (sock);

  return bio;
}


/* error handling */

unsigned long
ERR_get_error (void)
{
  unsigned long ret;

  ret = -1 * last_error;
  last_error = 0;

  return ret;
}

const char *
ERR_error_string (unsigned long e, char *buf)
{
  return gnutls_strerror (-1 * e);
}


/* RAND functions */

int
RAND_status (void)
{
  return 1;
}

void
RAND_seed (const void *buf, int num)
{
}

int
RAND_bytes (unsigned char *buf, int num)
{
  gnutls_rnd (GNUTLS_RND_RANDOM, buf, num);
  return 1;
}

int
RAND_pseudo_bytes (unsigned char *buf, int num)
{
  gnutls_rnd (GNUTLS_RND_NONCE, buf, num);
  return 1;
}

const char *
RAND_file_name (char *buf, size_t len)
{
  return "";
}

int
RAND_load_file (const char *name, long maxbytes)
{
  return maxbytes;
}

int
RAND_write_file (const char *name)
{
  return 0;
}

int
RAND_egd_bytes (const char *path, int bytes)
{
  /* fake it */
  return bytes;
}


/* message digest functions */

void
MD5_Init (MD5_CTX * ctx)
{
  ctx->handle = gnutls_malloc (sizeof (digest_hd_st));
  if (!ctx->handle)
    abort ();
  _gnutls_hash_init (ctx->handle, GNUTLS_DIG_MD5);
}

void
MD5_Update (MD5_CTX * ctx, const void *buf, int len)
{
  _gnutls_hash (ctx->handle, buf, len);
}

void
MD5_Final (unsigned char *md, MD5_CTX * ctx)
{
  _gnutls_hash_deinit (ctx->handle, md);
  gnutls_free (ctx->handle);
}

unsigned char *
MD5 (const unsigned char *buf, unsigned long len, unsigned char *md)
{
  if (!md)
    return NULL;

  _gnutls_hash_fast (GNUTLS_DIG_MD5, buf, len, md);

  return md;
}

void
RIPEMD160_Init (RIPEMD160_CTX * ctx)
{
  ctx->handle = gnutls_malloc (sizeof (digest_hd_st));
  if (!ctx->handle)
    abort ();
  _gnutls_hash_init (ctx->handle, GNUTLS_DIG_RMD160);
}

void
RIPEMD160_Update (RIPEMD160_CTX * ctx, const void *buf, int len)
{
  _gnutls_hash (ctx->handle, buf, len);
}

void
RIPEMD160_Final (unsigned char *md, RIPEMD160_CTX * ctx)
{
  _gnutls_hash_deinit (ctx->handle, md);
  gnutls_free (ctx->handle);
}

unsigned char *
RIPEMD160 (const unsigned char *buf, unsigned long len, unsigned char *md)
{
  if (!md)
    return NULL;

  _gnutls_hash_fast (GNUTLS_DIG_RMD160, buf, len, md);

  return md;
}