forked from SWI-Prolog/packages-ssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssllib.c
1309 lines (1109 loc) · 31.5 KB
/
ssllib.c
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
/* $Id$
Part of SWI-Prolog
Author: Jan van der Steen and Jan Wielemaker
E-mail: J.van.der.Steen@diff.nl and jan@swi.psy.uva.nl
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2002, SWI-Prolog Foundation
This library 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 library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include "ssllib.h"
#include <openssl/rand.h>
#ifdef __SWI_PROLOG__
#include <SWI-Stream.h>
#include <SWI-Prolog.h>
#define perror(x) Sdprintf("%s: %s\n", x, strerror(errno));
/*
* Remap socket related calls to nonblockio library
*/
#include "../clib/nonblockio.h"
#define closesocket nbio_closesocket
#else /* __SWI_PROLOG__ */
#define Soutput stdout
#define Serror stderr
#define Svfprintf vfprintf
static int PL_handle_signals(void) { return 0; }
#ifndef __WINDOWS__
#define closesocket close
#endif
#endif /* __SWI_PROLOG__ */
#include <openssl/rsa.h>
typedef enum
{ SSL_PL_OK
, SSL_PL_RETRY
, SSL_PL_ERROR
} SSL_PL_STATUS;
#define SSL_CERT_VERIFY_MORE 0
#define SSL_WAIT_CHILD 1
#define SSL_TCP_QUEUE_MAX 5
#ifndef DEBUG
#define DEBUG 1
#endif
/*
* Index of our config data in the SSL data
*/
static int ssl_idx;
static int ctx_idx;
static void
ssl_error(SSL *ssl, int ssl_ret, int code)
/*
* Report about errors occuring in the SSL layer
*/
{
char buf[256];
char *component[5];
char *colon;
int error = ERR_get_error();
int n;
ssl_deb(1, "ssl_error() ret=%d, code=%d, err=%d\n", ssl_ret, code, error);
if ( code == SSL_ERROR_SYSCALL && error == 0 )
{ if ( ssl_ret == 0 )
{ /* normal if peer just hangs up the line */
ssl_deb(1, "SSL error report: unexpected end-of-file\n");
return;
} else if ( ssl_ret == -1 )
{ ssl_deb(0, "SSL error report: syscall error: %s\n", strerror(errno));
return;
}
}
(void) ERR_error_string(error, buf);
/*
* Disect the following error string:
*
* error:[error code]:[library name]:[function name]:[reason string]
*/
for (colon = buf, n = 0; n < 5; n++) {
component[n] = colon;
if ((colon = strchr(colon, ':')) == NULL) break;
*colon++ = 0;
}
ssl_deb(0,
"SSL error report:\n\t%8s: %s\n\t%8s: %s\n\t%8s: %s\n\t%8s: %s\n"
, "code", component[1]
, "library", component[2]
, "function", component[3]
, "reason", component[4]
);
}
static SSL_PL_STATUS
ssl_inspect_status(SSL *ssl, int ssl_ret)
{ int code;
if (ssl_ret >= 0) {
return SSL_PL_OK;
}
code=SSL_get_error(ssl, ssl_ret);
switch (code) {
/* I am not sure what to do here - specifically, I am not sure if our underlying BIO
will block if there is not enough data to complete a handshake. If it will, we should
never get these return values. If it wont, then we presumably need to simply try again
which is why I am returning SSL_PL_RETRY
*/
case SSL_ERROR_WANT_READ:
return SSL_PL_RETRY;
case SSL_ERROR_WANT_WRITE:
return SSL_PL_RETRY;
#ifdef SSL_ERROR_WANT_CONNECT
case SSL_ERROR_WANT_CONNECT:
return SSL_PL_RETRY;
#endif
#ifdef SSL_ERROR_WANT_ACCEPT
case SSL_ERROR_WANT_ACCEPT:
return SSL_PL_RETRY;
#endif
case SSL_ERROR_ZERO_RETURN:
return SSL_PL_OK;
default:
break;
}
ssl_error(ssl, ssl_ret, code);
return SSL_PL_ERROR;
}
static char *
ssl_strdup(const char *s)
{
char *new = NULL;
if (s != NULL && (new = malloc(strlen(s)+1)) != NULL) {
strcpy(new, s);
}
return new;
}
#if SSL_CERT_VERIFY_MORE
static void
ssl_cert_print(X509 *cert, const char *role)
{
#if DEBUG
char * str = NULL;
int i;
ssl_deb(1, "%s certificate:\n", role);
str = X509_NAME_oneline(X509_get_subject_name (cert), NULL, 0);
if (str) {
ssl_deb(1, "\t subject: %s\n", str);
OPENSSL_free(str);
}
str = X509_NAME_oneline(X509_get_issuer_name (cert), NULL, 0);
if (str) {
ssl_deb(1, "\t issuer: %s\n", str);
OPENSSL_free(str);
}
i = X509_get_signature_type(cert);
ssl_deb(1, "\t signature type: %d\n", i);
#endif /* DEBUG */
}
#endif /* SSL_CERT_VERIFY_MORE */
#if SSL_CERT_VERIFY_MORE
static void
ssl_verify_cert(PL_SSL *config)
/*
* Verify certificate
*/
{
SSL * ssl = config->pl_ssl_ssl;
X509 * cert = config->pl_ssl_peer_cert;
const char * role = (config->pl_ssl_role == PL_SSL_SERVER)
? "Client"
: "Server"
;
if (cert != NULL) {
if (SSL_get_verify_result(ssl) != X509_V_OK) {
ssl_err("%s certificate didn't verify, continue anyway\n", role);
} else {
ssl_deb(1, "%s certificate verified ok\n", role);
}
ssl_cert_print(cert, role);
/*
* Do additional certificate verification stuff here.
*/
X509_free (cert);
} else {
ssl_err("%s does not have certificate.\n", role);
}
}
#endif /* SSL_CERT_VERIFY_MORE */
static PL_SSL *
ssl_new(void)
/*
* Allocate new state and configuration storage for an SSL session from PL
*/
{
PL_SSL *new = NULL;
if ((new = malloc(sizeof(*new))) != NULL) {
new->pl_ssl_role = PL_SSL_NONE;
new->sock = -1;
new->closeparent = 0;
new->pl_ssl_peer_cert = NULL;
new->pl_ssl_ctx = NULL;
new->pl_ssl_idx = -1;
new->pl_ssl_password = NULL;
new->pl_ssl_host = NULL;
new->pl_ssl_port = -1;
new->pl_ssl_reuseaddr = TRUE;
new->pl_ssl_cacert = NULL;
new->pl_ssl_cert_required = FALSE;
new->pl_ssl_certf = NULL;
new->pl_ssl_keyf = NULL;
new->pl_ssl_peer_cert_required = FALSE;
new->pl_ssl_cb_cert_verify = NULL;
new->pl_ssl_cb_cert_verify_data = NULL;
new->pl_ssl_cb_pem_passwd = NULL;
new->pl_ssl_cb_pem_passwd_data = NULL;
new->magic = SSL_CONFIG_MAGIC;
}
ssl_deb(1, "Allocated config structure\n");
return new;
}
static void
ssl_free(PL_SSL *config)
/*
* Free resources allocated to store the state and config parameters.
*/
{
if (config) {
if ( config->magic == SSL_CONFIG_MAGIC ) {
config->magic = 0;
free(config->pl_ssl_host);
free(config->pl_ssl_cacert);
free(config->pl_ssl_certf);
free(config->pl_ssl_keyf);
free(config->pl_ssl_password);
free(config);
ssl_deb(1, "Released config structure\n");
} else {
assert(0);
}
} else {
ssl_deb(1, "No config structure to release\n");
}
}
static int
ssl_config_new ( void * ctx
, void * pl_ssl
, CRYPTO_EX_DATA * parent_ctx
, int parent_ctx_idx
, long argl
, void *argp
)
/*
* Called when a new CTX is allocated
*/
{
PL_SSL *config = NULL;
if ((config = ssl_new()) != NULL) {
if (SSL_CTX_set_ex_data( ctx
, ctx_idx
, config) == 0) {
ssl_err("Cannot save application data\n");
ssl_free(config);
config = NULL;
}
}
/*
* 1 = success
* 0 = failure
*/
return (config != NULL);
}
static int
ssl_config_dup ( CRYPTO_EX_DATA * to
, CRYPTO_EX_DATA * from
, void * pl_ssl
, int parent_ctx_idx
, long argl
, void *argp
)
{
return 1;
}
static void
ssl_config_free( void * ctx
, void * pl_ssl
, CRYPTO_EX_DATA * parent_ctx
, int parent_ctx_idx
, long argl
, void *argp
)
{
PL_SSL *config = NULL;
ssl_deb(1, "calling ssl_config_free()\n");
if ((config = SSL_CTX_get_ex_data(ctx, ctx_idx)) != NULL) {
assert(config->magic == SSL_CONFIG_MAGIC);
ssl_free(config);
}
}
char *
ssl_set_cacert(PL_SSL *config, const char *cacert)
/*
* Store certificate authority location in config storage
*/
{
if (cacert) {
if (config->pl_ssl_cacert) free(config->pl_ssl_cacert);
config->pl_ssl_cacert = ssl_strdup(cacert);
}
return config->pl_ssl_cacert;
}
char *
ssl_set_certf(PL_SSL *config, const char *certf)
/*
* Store certificate file location in config storage
*/
{
if (certf) {
if (config->pl_ssl_certf) free(config->pl_ssl_certf);
config->pl_ssl_certf = ssl_strdup(certf);
}
return config->pl_ssl_certf;
}
char *
ssl_set_keyf(PL_SSL *config, const char *keyf)
/*
* Store private key location in config storage
*/
{
if (keyf) {
if (config->pl_ssl_keyf) free(config->pl_ssl_keyf);
config->pl_ssl_keyf = ssl_strdup(keyf);
}
return config->pl_ssl_keyf;
}
char *
ssl_set_password(PL_SSL *config, const char *password)
/*
* Store supplied private key password in config storage
*/
{
if (password) {
if (config->pl_ssl_password) free(config->pl_ssl_password);
config->pl_ssl_password = ssl_strdup(password);
}
return config->pl_ssl_password;
}
char *
ssl_set_host(PL_SSL *config, const char *host)
/*
* Store supplied host in config storage
*/
{
if (host) {
if (config->pl_ssl_host) free(config->pl_ssl_host);
config->pl_ssl_host = ssl_strdup(host);
}
return config->pl_ssl_host;
}
int
ssl_set_port(PL_SSL *config, int port)
/*
* Store supplied port in config storage
*/
{
return config->pl_ssl_port = port;
}
BOOL
ssl_set_cert(PL_SSL *config, BOOL required)
/*
* Do we require our certificate
*/
{
return config->pl_ssl_cert_required = required;
}
BOOL
ssl_set_reuseaddr(PL_SSL *config, BOOL reuse)
{ return config->pl_ssl_reuseaddr = reuse;
}
BOOL
ssl_set_peer_cert(PL_SSL *config, BOOL required)
/*
* Do we require peer certificate
*/
{
return config->pl_ssl_peer_cert_required = required;
}
BOOL
ssl_set_cb_pem_passwd( PL_SSL *config
, char * (*callback)( PL_SSL *
, char *
, int
)
, void *data
)
/*
* Install handler which is called when a certificate is password protected.
*/
{
config->pl_ssl_cb_pem_passwd = callback;
config->pl_ssl_cb_pem_passwd_data = data;
return TRUE;
}
BOOL
ssl_set_cb_cert_verify( PL_SSL *config
, BOOL (*callback)( PL_SSL *
, X509 *
, X509_STORE_CTX *
, const char *
)
, void *data
)
/*
* Install handler which is called when certificate verification fails.
*/
{
config->pl_ssl_cb_cert_verify = callback;
config->pl_ssl_cb_cert_verify_data = data;
return TRUE;
}
static int
ssl_cb_cert_verify(int preverify_ok, X509_STORE_CTX *ctx)
/*
* Function handling certificate verification
*/
{
SSL * ssl = NULL;
PL_SSL * config = NULL;
/*
* Get our config data
*/
ssl = X509_STORE_CTX_get_ex_data(ctx, SSL_get_ex_data_X509_STORE_CTX_idx());
config = SSL_get_ex_data(ssl, ssl_idx);
ssl_deb(1, " ---- INIT Handling certificate verification\n");
if (!preverify_ok) {
X509 *cert = NULL;
int err;
const char *error;
/*
* Get certificate
*/
cert = X509_STORE_CTX_get_current_cert(ctx);
/*
* Get error specification
*/
err = X509_STORE_CTX_get_error(ctx);
error = X509_verify_cert_error_string(err);
if (config->pl_ssl_cb_cert_verify) {
preverify_ok = ((config->pl_ssl_cb_cert_verify)(config, cert, ctx, error) != 0);
} else {
char subject[256];
char issuer [256];
int depth;
depth = X509_STORE_CTX_get_error_depth (ctx);
X509_NAME_oneline( X509_get_subject_name(cert)
, subject, sizeof(subject));
X509_NAME_oneline( X509_get_issuer_name (cert)
, issuer, sizeof(issuer));
ssl_deb(1, "error:%s\n", error);
ssl_deb(1, "subject:%s\n", subject);
ssl_deb(1, "issuer:%s\n", issuer);
}
ssl_deb(1, "Certificate preverified not ok\n");
} else {
ssl_deb(1, "Certificate preverified ok\n");
}
ssl_deb(1, " ---- EXIT Handling certificate verification\n");
return preverify_ok;
}
static int
ssl_cb_pem_passwd(char *buf, int size, int rwflag, void *userdata)
/*
* We're called since the OpenSSL library needs a password to access
* the private key. The method to require the password is defined in
* this function. Either interactive or automated.
* Fill the supplied buffer with the password and return its length
* or 0 on failure.
*/
{
PL_SSL *config = (PL_SSL *) userdata;
char *passwd = NULL;
int len = 0;
if (config->pl_ssl_cb_pem_passwd) {
/*
* Callback installed
*/
passwd = (config->pl_ssl_cb_pem_passwd)(config, buf, size);
} else
if (config->pl_ssl_password) {
/*
* Password defined
*/
passwd = config->pl_ssl_password;
}
if (passwd) {
if ((len = (int)strlen(passwd)) < size) {
strcpy(buf, passwd);
} else {
len = 0;
}
}
return len;
}
BOOL
ssl_set_close_parent(PL_SSL *config, int closeparent)
/*
* Should we close the parent streams?
*/
{
return config->closeparent = closeparent;
}
int
ssl_close(PL_SSL_INSTANCE *instance)
/*
* Clean up TCP and SSL connection resources
*/
{
int ret = 0;
if (instance) {
if (instance->config->pl_ssl_role != PL_SSL_SERVER) {
/*
* Send SSL/TLS close_notify
*/
SSL_shutdown(instance->ssl);
}
if (instance->ssl) {
SSL_free(instance->ssl);
}
if (instance->sock >= 0) {
/* If the socket has been stored, then we ought to close it if the SSL is being closed */
ret = closesocket(instance->sock);
instance->sock = -1;
}
if (instance->sread != NULL) {
/* Indicate we are no longer filtering the stream */
Sset_filter(instance->sread, NULL);
/* Close the stream if requested */
if (instance->config->closeparent)
Sclose(instance->sread);
}
if (instance->swrite != NULL) {
/* Indicate we are no longer filtering the stream */
Sset_filter(instance->swrite, NULL);
/* Close the stream if requested */
if (instance->config->closeparent)
Sclose(instance->swrite);
}
free(instance);
}
ERR_free_strings();
ssl_deb(1, "Controlled close\n");
return ret;
}
void
ssl_exit(PL_SSL *config)
/*
* Clean up all allocated resources.
*/
{
if (config)
{ if ( config->pl_ssl_role == PL_SSL_SERVER && config->sock >= 0 )
{ /* If the socket has been stored, then we ought to close it if the SSL is being closed */
closesocket(config->sock);
config->sock = -1;
}
if (config->pl_ssl_ctx)
{ ssl_deb(1, "Calling SSL_CTX_free()\n");
SSL_CTX_free(config->pl_ssl_ctx); /* doesn't call free hook? */
}
else
{ ssl_deb(1, "config without CTX encountered\n");
}
}
ssl_deb(1, "Controlled exit\n");
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
ERR_print_errors_pl() is like ERR_print_errors_fp(stderr), but deals
with the fact that on Windows stderr is generally lost, so we use Prolog
I/O for portability.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static void
ERR_print_errors_pl()
{ char errmsg[1024];
ERR_error_string_n(ERR_get_error(), errmsg, sizeof(errmsg));
Sdprintf("%s\n", errmsg);
}
PL_SSL *
ssl_init(PL_SSL_ROLE role)
/*
* Allocate the holder for our parameters which will specify the
* configuration parameters and any other statefull parameter.
* Load the OpenSSL error_strings for error reporting.
* Define method for SSL layer depending on whether we're server or client.
* Create SSL context.
*/
{
PL_SSL * config = NULL;
SSL_METHOD * ssl_method = NULL;
SSL_CTX * ssl_ctx = NULL;
#if 0
switch (role) {
case PL_SSL_SERVER:
ssl_method = SSLv23_server_method();
break;
case PL_SSL_NONE:
case PL_SSL_CLIENT:
/*
* Either v2 or v3, we choose v3 to make the shutdown more
* controlled and clean (see util.c)
*/
ssl_method = SSLv3_client_method();
break;
}
#else
ssl_method = SSLv23_method();
#endif
ssl_ctx = SSL_CTX_new(ssl_method);
if (!ssl_ctx) {
ERR_print_errors_pl();
} else {
long ctx_mode = 0L;
if ((config = SSL_CTX_get_ex_data(ssl_ctx, ctx_idx)) == NULL) {
ssl_err("Cannot read back application data\n");
SSL_CTX_free(ssl_ctx);
return NULL;
}
assert(config->magic == SSL_CONFIG_MAGIC);
config->pl_ssl_ctx = ssl_ctx;
config->pl_ssl_role = role;
ssl_set_cert (config, (role == PL_SSL_SERVER));
ssl_set_peer_cert(config, (role != PL_SSL_SERVER));
/*
* Set SSL_{read,write} behaviour when a renegotiation takes place
* in a blocking transport layer.
*/
ctx_mode = SSL_CTX_get_mode(ssl_ctx);
ctx_mode |= SSL_MODE_AUTO_RETRY;
ctx_mode = SSL_CTX_set_mode(ssl_ctx, ctx_mode);
}
ssl_deb(1, "Initialized\n");
return config;
}
int
ssl_debug(PL_SSL *config)
/*
* Get some extra info once the SSL negotiation is completed
*/
{
#if DEBUG
/*
* Get the cipher
*/
ssl_deb(1, "SSL connection using %s\n"
, SSL_get_cipher(config->pl_ssl_ssl)
) ;
#endif
#if SSL_CERT_VERIFY_MORE
if (config->pl_ssl_peer_cert_required) {
/*
* Get peer's certificate
*/
config->pl_ssl_peer_cert = SSL_get_peer_certificate(config->pl_ssl_ssl);
if (config->pl_ssl_peer_cert == NULL) {
return -8;
}
ssl_deb(1, "got peer certificate\n");
ssl_verify_cert(config);
ssl_deb(1, "verified peer certificate\n");
}
#endif
ssl_deb(1, "inspected ssl peer details\n");
return 0;
}
static int
ssl_config(PL_SSL *config)
/*
* Initialize various SSL layer parameters using the supplied
* config parameters.
*/
{
SSL_CTX_load_verify_locations( config->pl_ssl_ctx
, config->pl_ssl_cacert
, NULL
) ;
ssl_deb(1, "certificate authority(s) installed (public keys loaded)\n");
SSL_CTX_set_default_passwd_cb_userdata( config->pl_ssl_ctx
, config
) ;
SSL_CTX_set_default_passwd_cb( config->pl_ssl_ctx
, ssl_cb_pem_passwd
) ;
ssl_deb(1, "password handler installed\n");
if (config->pl_ssl_cert_required) {
if (config->pl_ssl_certf == NULL ||
config->pl_ssl_keyf == NULL) {
ssl_err("certificate and private key required but not set\n");
return -1;
}
if (SSL_CTX_use_certificate_file( config->pl_ssl_ctx
, config->pl_ssl_certf
, SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_pl();
return -2;
}
if (SSL_CTX_use_PrivateKey_file( config->pl_ssl_ctx
, config->pl_ssl_keyf
, SSL_FILETYPE_PEM) <= 0) {
ERR_print_errors_pl();
return -3;
}
if (SSL_CTX_check_private_key(config->pl_ssl_ctx) <= 0) {
ERR_print_errors_pl();
ssl_err("Private key does not match certificate public key\n");
return -4;
}
ssl_deb(1, "certificate installed successfully\n");
}
(void) SSL_CTX_set_verify( config->pl_ssl_ctx
, (config->pl_ssl_peer_cert_required)
? SSL_VERIFY_PEER|SSL_VERIFY_FAIL_IF_NO_PEER_CERT
: SSL_VERIFY_NONE
, ssl_cb_cert_verify
) ;
ssl_deb(1, "installed certificate verification handler\n");
return 0;
}
PL_SSL_INSTANCE *
ssl_instance_new(PL_SSL *config, IOSTREAM* sread, IOSTREAM* swrite)
{
PL_SSL_INSTANCE *new = NULL;
if ((new = malloc(sizeof(PL_SSL_INSTANCE))) != NULL) {
new->config = config;
new->sock = -1;
new->sread = sread;
new->swrite = swrite;
new->close_needed = 0;
}
return new;
}
int
ssl_lib_init(void)
/*
* One-time library initialization code
*/
{
SSL_load_error_strings();
/* This call will ensure we only end up calling RAND_poll() once
- preventing an ugly synchronization issue in OpenSSL */
RAND_status();
(void) SSL_library_init();
if ((ctx_idx = SSL_CTX_get_ex_new_index( 0
, NULL
, ssl_config_new
, ssl_config_dup
, ssl_config_free
)) < 0) {
ssl_err("Cannot register application data\n");
return -1;
}
/*
* Index used to store our config data in the SSL data structure
*/
ssl_idx = SSL_get_ex_new_index(0, "config", NULL, NULL, NULL);
#ifdef __SWI_PROLOG__
/*
* Initialize the nonblockio library
*/
if ( !nbio_init("ssl4pl") ) /* DLL name */
{ ssl_err("Could not initialise nbio module\n");
return -1;
}
#endif
return 0;
}
int
ssl_lib_exit(void)
/*
* One-time library exit calls
*/
{
#ifdef __SWI_PROLOG__
nbio_cleanup();
#endif
return 0;
}
/*
* BIO routines for SSL over streams
*/
/*
* Read function
*/
int bio_read(BIO* bio, char* buf, int len)
{
IOSTREAM* stream;
stream = BIO_get_ex_data(bio, 0);
return (int)Sfread(buf, sizeof(char), len, stream);
}
/*
* Gets function. If only OpenSSL actually had usable documentation, I might know
* what this was actually meant to do....
*/
int bio_gets(BIO* bio, char* buf, int len)
{
IOSTREAM* stream;
int r = 0;
stream = BIO_get_app_data(bio);
for (r = 0; r < len; r++)
{
int c = Sgetc(stream);
if (c == EOF)
return r-1;
buf[r] = (char)c;
if (buf[r] == '\n')
break;
}
return r;
}
/*
* Write function
*/
int bio_write(BIO* bio, const char* buf, int len)
{
IOSTREAM* stream;
int r;
stream = BIO_get_ex_data(bio, 0);
r = (int)Sfwrite(buf, sizeof(char), len, stream);
/* OpenSSL expects there to be no buffering when it writes. Flush here */
Sflush(stream);
return r;
}
/*
* Control function
* (Currently only supports flush. There are several mandatory, but as-yet unsupported functions...)
*/
long bio_control(BIO* bio, int cmd, long num, void* ptr)
{
IOSTREAM* stream;
stream = BIO_get_ex_data(bio, 0);
switch(cmd)
{
case BIO_CTRL_FLUSH:
Sflush(stream);
return 1;
}
return 0;
}
/*
* Create function. Called when a new BIO is created
* It is our responsibility to set init to 1 here
*/
int bio_create(BIO* bio)
{
bio->shutdown = 1;
bio->init = 1;
bio->num = -1;