forked from SWI-Prolog/packages-ssl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathssl4pl.c
1588 lines (1354 loc) · 43.5 KB
/
ssl4pl.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: [email protected]
WWW: http://www.swi-prolog.org
Copyright (C): 1985-2007, 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 <SWI-Stream.h>
#include <SWI-Prolog.h>
#include <assert.h>
#include <string.h>
#include "ssllib.h"
#ifdef _REENTRANT
#include <pthread.h>
static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
#define LOCK() pthread_mutex_lock(&mutex)
#define UNLOCK() pthread_mutex_unlock(&mutex)
#else
#define LOCK()
#define UNLOCK()
#endif
static atom_t ATOM_server;
static atom_t ATOM_client;
static atom_t ATOM_password;
static atom_t ATOM_host;
static atom_t ATOM_port;
static atom_t ATOM_cert;
static atom_t ATOM_peer_cert;
static atom_t ATOM_cacert_file;
static atom_t ATOM_certificate_file;
static atom_t ATOM_key_file;
static atom_t ATOM_pem_password_hook;
static atom_t ATOM_cert_verify_hook;
static atom_t ATOM_close_parent;
static functor_t FUNCTOR_ssl1;
static functor_t FUNCTOR_error2;
static functor_t FUNCTOR_type_error2;
static functor_t FUNCTOR_domain_error2;
static functor_t FUNCTOR_resource_error1;
static functor_t FUNCTOR_ssl_error1;
static functor_t FUNCTOR_existence_error1;
static functor_t FUNCTOR_permission_error3;
static functor_t FUNCTOR_ip4;
static functor_t FUNCTOR_version1;
static functor_t FUNCTOR_notbefore1;
static functor_t FUNCTOR_notafter1;
static functor_t FUNCTOR_subject1;
static functor_t FUNCTOR_issuername1;
static functor_t FUNCTOR_serial1;
static functor_t FUNCTOR_public_key5;
static functor_t FUNCTOR_private_key8;
static functor_t FUNCTOR_key1;
static functor_t FUNCTOR_hash1;
static functor_t FUNCTOR_signature1;
static functor_t FUNCTOR_equals2;
static functor_t FUNCTOR_crl1;
static functor_t FUNCTOR_revocations1;
static functor_t FUNCTOR_revoked2;
static int
type_error(term_t val, const char *type)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_type_error2,
PL_CHARS, type,
PL_TERM, val,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
static int
domain_error(term_t val, const char *type)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_domain_error2,
PL_TERM, val,
PL_CHARS, type,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
static int
resource_error(const char *resource)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_resource_error1,
PL_CHARS, resource,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
static int
ssl_error(const char *id)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_ssl_error1,
PL_CHARS, id,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
static int
permission_error(const char *action, const char *type, term_t obj)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_permission_error3,
PL_CHARS, action,
PL_CHARS, type,
PL_TERM, obj,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
/* not used now
static int
existence_error(term_t resource)
{ term_t ex;
if ( (ex=PL_new_term_ref()) &&
PL_unify_term(ex,
PL_FUNCTOR, FUNCTOR_error2,
PL_FUNCTOR, FUNCTOR_existence_error1,
PL_TERM, resource,
PL_VARIABLE) )
return PL_raise_exception(ex);
return FALSE;
}
*/
static int i2d_X509_CRL_INFO_wrapper(void* i, unsigned char** d)
{
return i2d_X509_CRL_INFO(i, d);
}
static int i2d_X509_CINF_wrapper(void* i, unsigned char** d)
{
return i2d_X509_CINF(i, d);
}
static int
get_atom_ex(term_t t, atom_t *a)
{ if ( !PL_get_atom(t, a) )
return type_error(t, "atom");
return TRUE;
}
static int
get_char_arg(int a, term_t t, char **s)
{ term_t t2 = PL_new_term_ref();
_PL_get_arg(a, t, t2);
if ( !PL_get_atom_chars(t2, s) )
return type_error(t2, "atom");
return TRUE;
}
static int
get_int_arg(int a, term_t t, int *i)
{ term_t t2 = PL_new_term_ref();
_PL_get_arg(a, t, t2);
if ( !PL_get_integer(t2, i) )
return type_error(t2, "integer");
return TRUE;
}
static int
get_bool_arg(int a, term_t t, int *i)
{ term_t t2 = PL_new_term_ref();
_PL_get_arg(a, t, t2);
if ( !PL_get_bool(t2, i) )
return type_error(t2, "boolean");
return TRUE;
}
static int
get_file_arg(int a, term_t t, char **f)
{ term_t t2 = PL_new_term_ref();
_PL_get_arg(a, t, t2);
if ( !PL_get_file_name(t2, f, PL_FILE_EXIST) )
return type_error(t2, "file"); /* TBD: check errors */
return TRUE;
}
static int
get_predicate_arg(int a, module_t m, term_t t, int arity, predicate_t *pred)
{ term_t t2 = PL_new_term_ref();
atom_t name;
_PL_get_arg(a, t, t2);
PL_strip_module(t2, &m, t2);
if ( !get_atom_ex(t2, &name) )
return FALSE;
*pred = PL_pred(PL_new_functor(name, arity), m);
return TRUE;
}
static int
recover_public_key(term_t public_t, RSA** rsa)
{
char *n, *e;
term_t n_t, e_t;
n_t = PL_new_term_ref();
e_t = PL_new_term_ref();
if(!(PL_get_arg(1, public_t, n_t) &&
PL_get_arg(2, public_t, e_t)))
return type_error(public_t, "public_key");
if (!(PL_get_atom_chars(n_t, &n) &&
PL_get_atom_chars(e_t, &e)))
return type_error(public_t, "public_key");
*rsa = RSA_new();
BN_hex2bn(&((*rsa)->n), n);
BN_hex2bn(&((*rsa)->e), e);
return TRUE;
}
static int
recover_private_key(term_t private_t, RSA** rsa)
{
char *n, *d, *e, *p, *q, *dmp1, *dmq1, *iqmp;
term_t n_t, d_t, e_t, p_t, q_t, dmp1_t, dmq1_t, iqmp_t;
n_t = PL_new_term_ref();
e_t = PL_new_term_ref();
d_t = PL_new_term_ref();
p_t = PL_new_term_ref();
q_t = PL_new_term_ref();
dmp1_t = PL_new_term_ref();
dmq1_t = PL_new_term_ref();
iqmp_t = PL_new_term_ref();
if(!(PL_get_arg(1, private_t, n_t) &&
PL_get_arg(2, private_t, e_t) &&
PL_get_arg(3, private_t, d_t) &&
PL_get_arg(4, private_t, p_t) &&
PL_get_arg(5, private_t, q_t) &&
PL_get_arg(6, private_t, dmp1_t) &&
PL_get_arg(7, private_t, dmq1_t) &&
PL_get_arg(8, private_t, iqmp_t)))
return type_error(private_t, "private_key");
ssl_deb(1, "Dismantling key");
if (!(PL_get_atom_chars(n_t, &n) &&
PL_get_atom_chars(e_t, &e) &&
PL_get_atom_chars(d_t, &d) &&
PL_get_atom_chars(p_t, &p) &&
PL_get_atom_chars(q_t, &q) &&
PL_get_atom_chars(dmp1_t, &dmp1) &&
PL_get_atom_chars(dmq1_t, &dmq1) &&
PL_get_atom_chars(iqmp_t, &iqmp)))
return type_error(private_t, "private_key");
ssl_deb(1, "Assembling RSA");
*rsa = RSA_new();
BN_hex2bn(&((*rsa)->n), n);
BN_hex2bn(&((*rsa)->d), d);
BN_hex2bn(&((*rsa)->e), e);
BN_hex2bn(&((*rsa)->p), p);
BN_hex2bn(&((*rsa)->q), q);
BN_hex2bn(&((*rsa)->dmp1), dmp1);
BN_hex2bn(&((*rsa)->dmq1), dmq1);
BN_hex2bn(&((*rsa)->iqmp), iqmp);
return TRUE;
}
static int
unify_private_key(term_t item, RSA* rsa)
{ term_t n_t, d_t, e_t, p_t, q_t, dmp1_t, dmq1_t, iqmp_t;
char* hex;
int retval = 1;
n_t = PL_new_term_ref();
e_t = PL_new_term_ref();
d_t = PL_new_term_ref();
p_t = PL_new_term_ref();
q_t = PL_new_term_ref();
dmp1_t = PL_new_term_ref();
dmq1_t = PL_new_term_ref();
iqmp_t = PL_new_term_ref();
hex = BN_bn2hex(rsa->n);
retval = retval && (PL_unify_atom_nchars(n_t, strlen(hex), hex));
OPENSSL_free(hex);
hex = BN_bn2hex(rsa->e);
retval = retval && (PL_unify_atom_nchars(e_t, strlen(hex), hex));
OPENSSL_free(hex);
if (rsa->d != NULL)
{ hex = BN_bn2hex(rsa->d);
retval = retval && (PL_unify_atom_nchars(d_t, strlen(hex), hex));
OPENSSL_free(hex);
} else
retval = retval && (PL_unify_atom_chars(d_t, "-"));
if (rsa->p != NULL)
{ hex = BN_bn2hex(rsa->p);
retval = retval && (PL_unify_atom_nchars(p_t, strlen(hex), hex));
OPENSSL_free(hex);
} else
retval = retval && (PL_unify_atom_chars(p_t, "-"));
if (rsa->q != NULL)
{ hex = BN_bn2hex(rsa->q);
retval = retval && (PL_unify_atom_nchars(q_t, strlen(hex), hex));
OPENSSL_free(hex);
} else
retval = retval && (PL_unify_atom_chars(q_t, "-"));
if (rsa->dmp1 != NULL)
{ hex = BN_bn2hex(rsa->dmp1);
retval = retval && (PL_unify_atom_nchars(dmp1_t, strlen(hex), hex));
OPENSSL_free(hex);
} else
retval = retval && (PL_unify_atom_chars(dmp1_t, "-"));
if (rsa->dmq1 != NULL)
{ hex = BN_bn2hex(rsa->dmq1);
retval = retval && (PL_unify_atom_nchars(dmq1_t, strlen(hex), hex));
OPENSSL_free(hex);
} else
retval = retval && (PL_unify_atom_chars(dmq1_t, "-"));
if (rsa->iqmp != NULL)
{ hex = BN_bn2hex(rsa->iqmp);
retval = retval && (PL_unify_atom_nchars(iqmp_t, strlen(hex), hex));
OPENSSL_free(hex);
} else
retval = retval && (PL_unify_atom_chars(iqmp_t, "-"));
return retval && PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_key1,
PL_FUNCTOR, FUNCTOR_private_key8,
PL_TERM, n_t,
PL_TERM, e_t,
PL_TERM, d_t,
PL_TERM, p_t,
PL_TERM, q_t,
PL_TERM, dmp1_t,
PL_TERM, dmq1_t,
PL_TERM, iqmp_t);
}
static int
unify_public_key(term_t item, RSA* rsa)
{ term_t n_t, e_t, dmp1_t, dmq1_t, iqmp_t, key_t;
char* hex;
int retval = 1;
n_t = PL_new_term_ref();
e_t = PL_new_term_ref();
dmp1_t = PL_new_term_ref();
dmq1_t = PL_new_term_ref();
iqmp_t = PL_new_term_ref();
hex = BN_bn2hex(rsa->n);
retval = retval && (PL_unify_atom_nchars(n_t, strlen(hex), hex));
OPENSSL_free(hex);
hex = BN_bn2hex(rsa->e);
retval = retval && (PL_unify_atom_nchars(e_t, strlen(hex), hex));
OPENSSL_free(hex);
if (rsa->dmp1 != NULL)
{ hex = BN_bn2hex(rsa->dmp1);
retval = retval && (PL_unify_atom_nchars(dmp1_t, strlen(hex), hex));
OPENSSL_free(hex);
} else
retval = retval && (PL_unify_atom_chars(dmp1_t, "-"));
if (rsa->dmq1 != NULL)
{ hex = BN_bn2hex(rsa->dmq1);
retval = retval && (PL_unify_atom_nchars(dmq1_t, strlen(hex), hex));
OPENSSL_free(hex);
} else
retval = retval && (PL_unify_atom_chars(dmq1_t, "-"));
if (rsa->iqmp != NULL)
{ hex = BN_bn2hex(rsa->iqmp);
retval = retval && (PL_unify_atom_nchars(iqmp_t, strlen(hex), hex));
OPENSSL_free(hex);
} else
retval = retval && (PL_unify_atom_chars(iqmp_t, "-"));
key_t = PL_new_term_ref();
retval = retval && PL_unify_term(key_t,
PL_FUNCTOR, FUNCTOR_public_key5,
PL_TERM, n_t,
PL_TERM, e_t,
PL_TERM, dmp1_t,
PL_TERM, dmq1_t,
PL_TERM, iqmp_t);
return retval && PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_key1,
PL_TERM, key_t);
}
/* Note that while this might seem incredibly hacky, it is
essentially the same algorithm used by X509_cmp_time to
parse the date. Some
Fractional seconds are ignored. This is also largely untested - there
may be a lot of edge cases that dont work!
*/
static int
unify_asn1_time(term_t term, ASN1_TIME *time)
{ time_t result = 0;
char buffer[24];
char* pbuffer = buffer;
size_t length = time->length;
char * source = (char *)time->data;
struct tm time_tm;
time_t lSecondsFromUTC;
if (time->type == V_ASN1_UTCTIME)
{ if ((length < 11) || (length > 17))
{ ssl_deb(2, "Unable to parse time - expected either 11 or 17 chars, not %d", length);
return FALSE;
}
/* Otherwise just get the first 10 chars - ignore seconds */
memcpy(pbuffer, source, 10);
pbuffer += 10;
source += 10;
} else
{ if (length < 13)
{ ssl_deb(2, "Unable to parse time - expected at least 13 chars, not %d", length);
return FALSE;
}
/* Otherwise just get the first 12 chars - ignore seconds */
memcpy(pbuffer, source, 10);
pbuffer += 10;
source += 10;
}
/* Next find end of string */
if ((*source == 'Z') || (*source == '-') || (*source == '+'))
{ *(pbuffer++) = '0';
*(pbuffer++) = '0';
} else
{ *(pbuffer++) = *(source++);
*(pbuffer++) = *(source++);
if (*source == '.')
{ source++;
while ((*source >= '0') && (*source <= '9'))
source++;
}
}
*(pbuffer++) = 'Z';
*(pbuffer++) = '\0';
/* If not UTC, calculate offset */
if (*source == 'Z')
lSecondsFromUTC = 0;
else
{ if ((*source != '+') && (source[5] != '-'))
{ ssl_deb(2, "Unable to parse time. Missing UTC offset");
return FALSE;
}
lSecondsFromUTC = ((source[1]-'0') * 10 + (source[2]-'0')) * 60;
lSecondsFromUTC += (source[3]-'0') * 10 + (source[4]-'0');
if (*source == '-')
lSecondsFromUTC = -lSecondsFromUTC;
}
/* Parse date */
time_tm.tm_sec = ((buffer[10] - '0') * 10) + (buffer[11] - '0');
time_tm.tm_min = ((buffer[8] - '0') * 10) + (buffer[9] - '0');
time_tm.tm_hour = ((buffer[6] - '0') * 10) + (buffer[7] - '0');
time_tm.tm_mday = ((buffer[4] - '0') * 10) + (buffer[5] - '0');
time_tm.tm_mon = (((buffer[2] - '0') * 10) + (buffer[3] - '0')) - 1;
time_tm.tm_year = ((buffer[0] - '0') * 10) + (buffer[1] - '0');
if (time_tm.tm_year < 50)
time_tm.tm_year += 100; /* according to RFC 2459 */
time_tm.tm_wday = 0;
time_tm.tm_yday = 0;
time_tm.tm_isdst = 0; /* No DST adjustment requested, though mktime might do it anyway */
result = mktime(&time_tm);
if ((time_t)-1 != result)
{ if (time_tm.tm_isdst != 0)
result -= 3600; /* if mktime has adjusted the time for DST, adjust it back 1 hour*/
result += lSecondsFromUTC; /* Add in the UTC offset, or should we return UTC time? */
} else
{ ssl_deb(2, "mktime() failed");
return FALSE;
}
return PL_unify_integer(term, result);
}
static int
unify_hash(term_t hash, ASN1_OBJECT* algorithm, int (*i2d)(void*, unsigned char**), void * data)
{ const EVP_MD *type;
EVP_MD_CTX ctx;
int digestible_length;
unsigned char* digest_buffer;
unsigned char digest[EVP_MAX_MD_SIZE];
unsigned int digest_length;
unsigned char* p;
/* Generate hash */
type=EVP_get_digestbyname(OBJ_nid2sn(OBJ_obj2nid(algorithm)));
if (type == NULL)
{ return ssl_error("digest_lookup");
}
EVP_MD_CTX_init( &ctx);
digestible_length=i2d(data,NULL);
digest_buffer = PL_malloc(digestible_length);
if (digest_buffer == NULL)
return resource_error("memory");
/* i2d_X509_CINF will change the value of p. We need to pass in a copy */
p = digest_buffer;
i2d(data,&p);
if (!EVP_DigestInit(&ctx, type))
{ EVP_MD_CTX_cleanup(&ctx);
PL_free(digest_buffer);
return ssl_error("digest_initialize");
}
if (!EVP_DigestUpdate(&ctx, digest_buffer, digestible_length))
{ EVP_MD_CTX_cleanup(&ctx);
PL_free(digest_buffer);
return ssl_error("digest_update");
}
if (!EVP_DigestFinal(&ctx, digest, &digest_length))
{ EVP_MD_CTX_cleanup(&ctx);
PL_free(digest_buffer);
return ssl_error("digest_finalize");
}
EVP_MD_CTX_cleanup(&ctx);
PL_free(digest_buffer);
return PL_unify_term(hash,
PL_NCHARS, digest_length, digest);
}
static int
unify_name(term_t term, X509_NAME* name)
{ int ni;
term_t list = PL_copy_term_ref(term);
term_t item = PL_new_term_ref();
if (name == NULL)
return PL_unify_term(term,
PL_CHARS, "<null>");
for (ni = 0; ni < X509_NAME_entry_count(name); ni++)
{ X509_NAME_ENTRY* e = X509_NAME_get_entry(name, ni);
ASN1_STRING* entry_data = X509_NAME_ENTRY_get_data(e);
if (!(PL_unify_list(list, item, list) &&
PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_equals2,
PL_CHARS, OBJ_nid2sn(OBJ_obj2nid(e->object)),
PL_NCHARS, entry_data->length, entry_data->data)
))
return FALSE;
}
return PL_unify_nil(list);
}
static int
unify_crl(term_t term, X509_CRL* crl)
{ X509_CRL_INFO* info = crl->crl;
int i;
term_t item = PL_new_term_ref();
term_t hash = PL_new_term_ref();
term_t issuer = PL_new_term_ref();
term_t revocations = PL_new_term_ref();
term_t list = PL_copy_term_ref(revocations);
int result = 1;
long n;
unsigned char* p;
term_t revocation_date;
BIO* mem;
mem = BIO_new(BIO_s_mem());
if (mem == NULL)
return resource_error("memory");
i2a_ASN1_INTEGER(mem, crl->signature);
if (!(unify_name(issuer, X509_CRL_get_issuer(crl)) &&
unify_hash(hash, crl->sig_alg->algorithm, i2d_X509_CRL_INFO_wrapper, crl->crl) &&
PL_unify_term(term,
PL_LIST, 4,
PL_FUNCTOR, FUNCTOR_issuername1,
PL_TERM, issuer,
PL_FUNCTOR, FUNCTOR_signature1,
PL_NCHARS, crl->signature->length, crl->signature->data,
PL_FUNCTOR, FUNCTOR_hash1,
PL_TERM, hash,
PL_FUNCTOR, FUNCTOR_revocations1,
PL_TERM, revocations)))
{
return FALSE;
}
for (i = 0; i < sk_X509_REVOKED_num(info->revoked); i++)
{
X509_REVOKED* revoked = sk_X509_REVOKED_value(info->revoked, i);
i2a_ASN1_INTEGER(mem, revoked->serialNumber);
result &= (((n = BIO_get_mem_data(mem, &p)) > 0) &&
PL_unify_list(list, item, list) &&
(revocation_date = PL_new_term_ref()) &&
unify_asn1_time(revocation_date, revoked->revocationDate) &&
PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_revoked2,
PL_NCHARS, n, p,
PL_TERM, revocation_date));
if (BIO_reset(mem) != 1)
{
BIO_free(mem);
// The only reason I can imagine this would fail is out of memory
return resource_error("memory");
}
}
BIO_free(mem);
return result && PL_unify_nil(list);
}
static int
unify_certificate(term_t cert, X509* data)
{ term_t list = PL_copy_term_ref(cert);
term_t item = PL_new_term_ref();
BIO * mem = NULL;
long n;
EVP_PKEY *key;
RSA* rsa;
term_t issuername;
term_t subject;
term_t hash;
term_t not_before;
term_t not_after;
unsigned int crl_ext_id;
unsigned char *p;
X509_EXTENSION * crl_ext = NULL;
if (!(PL_unify_list(list, item, list) &&
PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_version1,
PL_LONG, X509_get_version(data))
))
return FALSE;
if (!(PL_unify_list(list, item, list) &&
(not_before = PL_new_term_ref()) &&
unify_asn1_time(not_before, X509_get_notBefore(data)) &&
PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_notbefore1,
PL_TERM, not_before)))
return FALSE;
if (!(PL_unify_list(list, item, list) &&
(not_after = PL_new_term_ref()) &&
unify_asn1_time(not_after, X509_get_notAfter(data)) &&
PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_notafter1,
PL_TERM, not_after)))
return FALSE;
if ((mem = BIO_new(BIO_s_mem())) != NULL)
{ i2a_ASN1_INTEGER(mem, X509_get_serialNumber(data));
if ((n = BIO_get_mem_data(mem, &p)) > 0)
{ if (!(PL_unify_list(list, item, list) &&
PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_serial1,
PL_NCHARS, n, p)
))
return FALSE;
} else
Sdprintf("Failed to print serial - continuing without serial\n");
} else
Sdprintf("Failed to allocate BIO for printing - continuing without serial\n");
if (!(PL_unify_list(list, item, list) &&
(subject = PL_new_term_ref()) &&
unify_name(subject, X509_get_subject_name(data)) &&
PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_subject1,
PL_TERM, subject))
)
return FALSE;
if (!((hash = PL_new_term_ref()) &&
unify_hash(hash, data->sig_alg->algorithm, i2d_X509_CINF_wrapper, data->cert_info) &&
PL_unify_list(list, item, list) &&
PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_hash1,
PL_TERM, hash)))
return FALSE;
if (!(PL_unify_list(list, item, list) &&
PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_signature1,
PL_NCHARS, data->signature->length, data->signature->data)
))
return FALSE;
if (!(PL_unify_list(list, item, list) &&
(issuername = PL_new_term_ref()) &&
unify_name(issuername, X509_get_issuer_name(data)) &&
PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_issuername1,
PL_TERM, issuername))
)
return FALSE;
/* X509_extract_key returns a reference to the existing key, not a copy */
key = X509_extract_key(data);
/* EVP_PKEY_get1_RSA returns a reference to the existing key, not a copy */
rsa = EVP_PKEY_get1_RSA(key);
if (!(PL_unify_list(list, item, list) &&
unify_public_key(item, rsa)))
return FALSE;
/* If the cert has a CRL distribution point, return that. If it does not,
it is not an error
*/
crl_ext_id = X509_get_ext_by_NID(data, NID_crl_distribution_points, -1);
crl_ext = X509_get_ext(data, crl_ext_id);
if (crl_ext != NULL)
{ STACK_OF(DIST_POINT) * distpoints;
int i, j;
term_t crl;
term_t crl_list;
term_t crl_item;
distpoints = X509_get_ext_d2i(data, NID_crl_distribution_points, NULL, NULL);
/* Loop through the CRL points, putting them into a list */
if (!PL_unify_list(list, item, list))
return FALSE;
crl = PL_new_term_ref();
crl_list = PL_copy_term_ref(crl);
crl_item = PL_new_term_ref();
for (i = 0; i < sk_DIST_POINT_num(distpoints); i++)
{ DIST_POINT *point;
GENERAL_NAME *name;
point = sk_DIST_POINT_value(distpoints, i);
if (point->distpoint != NULL)
{ /* Each point may have several names? May as well put them all in */
for (j = 0; j < sk_GENERAL_NAME_num(point->distpoint->name.fullname); j++)
{ name = sk_GENERAL_NAME_value(point->distpoint->name.fullname, j);
if (name != NULL && name->type == GEN_URI)
{ if (!(PL_unify_list(crl_list, crl_item, crl_list) &&
PL_unify_atom_chars(crl_item, (const char *)name->d.ia5->data)))
return FALSE;
}
}
}
}
if (!PL_unify_nil(crl_list))
return FALSE;
if (!PL_unify_term(item,
PL_FUNCTOR, FUNCTOR_crl1,
PL_TERM, crl))
return FALSE;
}
return PL_unify_nil(list);
}
static int
unify_certificates(term_t certs, term_t tail, STACK_OF(X509)* stack)
{ term_t item = PL_new_term_ref();
term_t list = PL_copy_term_ref(certs);
X509* cert = sk_X509_pop(stack);
int retval = 1;
while (cert != NULL && retval == 1)
{ retval &= PL_unify_list(list, item, list);
retval &= unify_certificate(item, cert);
X509_free(cert);
cert = sk_X509_pop(stack);
if (cert == NULL)
return PL_unify(tail, item) && PL_unify_nil(list);
}
return retval && PL_unify_nil(list);
}
foreign_t
pl_load_public_key(term_t source, term_t key_t)
{ EVP_PKEY* key;
RSA* rsa;
BIO* bio;
IOSTREAM* stream;
int c;
if ( !PL_get_stream_handle(source, &stream) )
return type_error(source, "stream");
bio = BIO_new(&bio_read_functions);
BIO_set_ex_data(bio, 0, stream);
/* Determine format */
c = Sgetc(stream);
if (c != EOF)
Sungetc(c, stream);
if (c == 0x30) /* ASN.1 sequence, so assume DER */
key = d2i_PUBKEY_bio(bio, NULL);
else
key = PEM_read_bio_PUBKEY(bio, NULL, NULL, NULL);
BIO_free(bio);
PL_release_stream(stream);
if (key == NULL)
return permission_error("read", "key", source);
rsa = EVP_PKEY_get1_RSA(key);
EVP_PKEY_free(key);
if (unify_public_key(key_t, rsa))
{ RSA_free(rsa);
PL_succeed;
} else
{ RSA_free(rsa);
PL_fail;
}
}
int private_password_callback(char *buf, int bufsiz, int verify, void* pw)
{ int res;
char* password = (char*)pw;
res = (int)strlen(password);
if (res > bufsiz)
res = bufsiz;
memcpy(buf, password, res);
return res;
}
foreign_t
pl_load_private_key(term_t source, term_t password, term_t key_t)
{ EVP_PKEY* key;
RSA* rsa;
BIO* bio;
IOSTREAM* stream;
char* password_chars;
int c;
if (!PL_get_atom_chars(password, &password_chars))
return type_error(password, "atom");
if ( !PL_get_stream_handle(source, &stream) )
return type_error(source, "stream");
bio = BIO_new(&bio_read_functions);
BIO_set_ex_data(bio, 0, stream);
/* Determine format */
c = Sgetc(stream);
if (c != EOF)
Sungetc(c, stream);
if (c == 0x30) /* ASN.1 sequence, so assume DER */
key = d2i_PrivateKey_bio(bio, NULL); /* TBD: Password! */
else
key = PEM_read_bio_PrivateKey(bio, NULL, &private_password_callback, (void*)password_chars);
BIO_free(bio);
PL_release_stream(stream);
if (key == NULL)
return permission_error("read", "key", source);
rsa = EVP_PKEY_get1_RSA(key);
EVP_PKEY_free(key);
if (unify_private_key(key_t, rsa))
{ RSA_free(rsa);
PL_succeed;
} else
{ RSA_free(rsa);
PL_fail;
}
}
foreign_t
pl_load_crl(term_t source, term_t list)
{ X509_CRL* crl;
BIO* bio;
IOSTREAM* stream;
int result;
int c;
if ( !PL_get_stream_handle(source, &stream) )
return type_error(source, "stream");
bio = BIO_new(&bio_read_functions);
BIO_set_ex_data(bio, 0, stream);
/* Determine the format of the CRL */
c = Sgetc(stream);
if (c != EOF)
Sungetc(c, stream);
if (c == 0x30) /* ASN.1 sequence, so assume DER */
crl = d2i_X509_CRL_bio(bio, NULL);
else
crl = PEM_read_bio_X509_CRL(bio, NULL, NULL, NULL);
BIO_free(bio);
PL_release_stream(stream);
if (crl == NULL)
{ ssl_deb(2, "Failed to load CRL");
PL_fail;
}
result = unify_crl(list, crl);
X509_CRL_free(crl);
return result;
}
foreign_t
pl_load_certificate(term_t source, term_t cert)
{ X509* x509;
BIO* bio;
IOSTREAM* stream;
int c = 0;
if ( !PL_get_stream_handle(source, &stream) )
return type_error(source, "stream");
bio = BIO_new(&bio_read_functions);
BIO_set_ex_data(bio, 0, stream);
/* Determine format */
c = Sgetc(stream);
if (c != EOF)
Sungetc(c, stream);
if (c == 0x30) /* ASN.1 sequence, so assume DER */
x509 = d2i_X509_bio(bio, NULL);
else
x509 = PEM_read_bio_X509(bio, NULL, 0, NULL);
BIO_free(bio);
PL_release_stream(stream);
if (x509 == NULL)
return ssl_error("read_x509");
if (unify_certificate(cert, x509))
{ X509_free(x509);
PL_succeed;
} else
{ X509_free(x509);
PL_fail;
}
}
static int
unify_conf(term_t config, PL_SSL *conf)
{ return PL_unify_term(config,
PL_FUNCTOR, FUNCTOR_ssl1,
PL_POINTER, conf);
}
static int