-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathSSLeay.xs
8731 lines (7253 loc) · 216 KB
/
SSLeay.xs
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
/* SSLeay.xs - Perl module for using Eric Young's implementation of SSL
*
* Copyright (c) 1996-2003 Sampo Kellomäki <[email protected]>
* Copyright (c) 2005-2010 Florian Ragwitz <[email protected]>
* Copyright (c) 2005-2018 Mike McCauley <[email protected]>
* Copyright (c) 2018 Tuure Vartiainen <[email protected]>
* Copyright (c) 2018- Chris Novakovic <[email protected]>
* Copyright (c) 2018- Heikki Vatiainen <[email protected]>
*
* All rights reserved.
*
* Change data removed. See Changes
*
* This module is released under the terms of the Artistic License 2.0. For
* details, see the LICENSE file.
*/
/* ####
* #### PLEASE READ THE FOLLOWING RULES BEFORE YOU START EDITING THIS FILE! ####
* ####
*
* Function naming conventions:
*
* 1/ never change the already existing function names (all calling convention) in a way
* that may cause backward incompatibility (e.g. add ALIAS with old name if necessary)
*
* 2/ it is recommended to keep the original openssl function names for functions that are:
*
* 1:1 wrappers to the original openssl functions
* see for example: X509_get_issuer_name(cert) >> Net::SSLeay::X509_get_issuer_name($cert)
*
* nearly 1:1 wrappers implementing only necessary "glue" e.g. buffer handling
* see for example: RAND_seed(buf,len) >> Net::SSLeay::RAND_seed($buf)
*
* 3/ OpenSSL functions starting with "SSL_" are added into SSLeay.xs with "SLL_" prefix
* (e.g. SSL_CTX_new) but keep in mind that they will be available in Net::SSLeay without
* "SSL_" prefix (e.g. Net::SSLeay::CTX_new) - keep this for all new functions
*
* 4/ The names of functions which do not fit rule 2/ (which means they implement some non
* trivial code around original openssl function or do more complex tasks) should be
* prefixed with "P_" - see for example: P_ASN1_TIME_set_isotime
*
* 5/ Exceptions from rules above:
* functions that are part or wider set of already existing function not following this rule
* for example: there already exists: PEM_get_string_X509_CRL + PEM_get_string_X509_REQ and you want
* to add PEM_get_string_SOMETHING - then no need to follow 3/ (do not prefix with "P_")
*
* Support for different Perl versions, libssl implementations, platforms, and compilers:
*
* 1/ Net-SSLeay has a version support policy for Perl and OpenSSL/LibreSSL (described in the
* "Prerequisites" section in the README file). The test suite must pass when run on any
* of those version combinations.
*
* 2/ Fix all compiler warnings - we expect 100% clean build
*
* 3/ If you add a function which is available since certain openssl version
* use proper #ifdefs to assure that SSLeay.xs will compile also with older versions
* which are missing this function
*
* 4/ Even warnings arising from different use of "const" in different openssl versions
* needs to be hanled with #ifdefs - see for example: X509_NAME_add_entry_by_txt
*
* 5/ avoid using global C variables (it is very likely to break thread-safetyness)
* use rather global MY_CXT structure
*
* 6/ avoid using any UNIX/POSIX specific functions, keep in mind that SSLeay.xs must
* compile also on non-UNIX platforms like MS Windows and others
*
* 7/ avoid using c++ comments "//" (or other c++ features accepted by some c compiler)
* even if your compiler can handle them without warnings
*
* Passing test suite:
*
* 1/ any changes to SSLeay.xs must not introduce a failure of existing test suite
*
* 2/ it is strongly recommended to create test(s) for newly added function(s), especially
* when the new function is not only a 1:1 wrapper but contains a complex code
*
* 3/ it is mandatory to add a documentation for all newly added functions into SSLeay.pod
* otherwise t/local/02_pod_coverage.t fail (and you will be asked to add some doc into
* your patch)
*
* Preferred code layout:
*
* 1/ for simple 1:1 XS wrappers use:
*
* a/ functions with short "signature" (short list of args):
*
* long
* SSL_set_tmp_dh(SSL *ssl,DH *dh)
*
* b/ functions with long "signature" (long list of args):
* simply when approach a/ does not fit to 120 columns
*
* void
* SSL_any_functions(library_flag,function_name,reason,file_name,line)
* int library_flag
* int function_name
* int reason
* char *file_name
* int line
*
* 2/ for XS functions with full implementation use identation like this:
*
* int
* RAND_bytes(buf, num)
* SV *buf
* int num
* PREINIT:
* int rc;
* unsigned char *random;
* CODE:
* / * some code here * /
* RETVAL = rc;
* OUTPUT:
* RETVAL
*
*
* Runtime debugging:
*
* with TRACE(level,fmt,...) you can output debug messages.
* it behaves the same as
* warn sprintf($msg,...) if $Net::SSLeay::trace>=$level
* would do in Perl (e.g. it is using also the $Net::SSLeay::trace variable)
*
*
* THE LAST RULE:
*
* The fact that some parts of SSLeay.xs do not follow the rules above is not
* a reason why any new code can also break these rules in the same way
*
*/
/* Prevent warnings about strncpy from Windows compilers */
#ifndef _CRT_SECURE_NO_DEPRECATE
# define _CRT_SECURE_NO_DEPRECATE
#endif
/* Silence compound-token-split-by-macro warnings from perl.h when building for
* Perl < 5.35.2 with Clang >= 12 - see GH-383
*/
#if NET_SSLEAY_PERL_VERSION < 5035002 && defined(__clang__) && defined(__clang_major__) && __clang_major__ >= 12
#pragma clang diagnostic ignored "-Wunknown-warning-option"
#pragma clang diagnostic ignored "-Wcompound-token-split-by-macro"
#pragma clang diagnostic warning "-Wunknown-warning-option"
#endif
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <stdarg.h>
#define NEED_my_snprintf
#include "ppport.h"
/* Sigh: openssl 1.0 has
typedef void *BLOCK;
which conflicts with perls
typedef struct block BLOCK;
*/
#define BLOCK OPENSSL_BLOCK
#include <openssl/err.h>
#include <openssl/conf.h>
#include <openssl/lhash.h>
#include <openssl/rand.h>
#include <openssl/buffer.h>
#include <openssl/ssl.h>
#include <openssl/pkcs12.h>
#ifndef OPENSSL_NO_COMP
#include <openssl/comp.h> /* openssl-0.9.6a forgets to include this */
#endif
#ifndef OPENSSL_NO_MD2
#include <openssl/md2.h>
#endif
#ifndef OPENSSL_NO_MD4
#include <openssl/md4.h>
#endif
#ifndef OPENSSL_NO_MD5
#include <openssl/md5.h> /* openssl-SNAP-20020227 does not automatically include this */
#endif
#include <openssl/ripemd.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#ifndef OPENSSL_NO_ENGINE
#include <openssl/engine.h>
#endif
#ifdef OPENSSL_FIPS
#include <openssl/fips.h>
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10000000L
#include <openssl/ocsp.h>
#endif
#if OPENSSL_VERSION_NUMBER >= 0x30000000L
#include <openssl/provider.h>
#endif
#undef BLOCK
/* Beginning with OpenSSL 3.0.0-alpha17, SSL_CTX_get_options() and
* related functions return uint64_t instead of long. For this reason
* constant() in constant.c and Net::SSLeay must also be able to
* return 64bit constants. However, this creates a problem with Perls
* that have only 32 bit integers. The define below helps with
* handling this API change.
*/
#if (OPENSSL_VERSION_NUMBER < 0x30000000L) || defined(NET_SSLEAY_32BIT_INT_PERL)
#define NET_SSLEAY_32BIT_CONSTANTS
#endif
/* Debugging output - to enable use:
*
* perl Makefile.PL DEFINE=-DSHOW_XS_DEBUG
* make
*
*/
#ifdef SHOW_XS_DEBUG
#define PR1(s) fprintf(stderr,s);
#define PR2(s,t) fprintf(stderr,s,t);
#define PR3(s,t,u) fprintf(stderr,s,t,u);
#define PR4(s,t,u,v) fprintf(stderr,s,t,u,v);
#else
#define PR1(s)
#define PR2(s,t)
#define PR3(s,t,u)
#define PR4(s,t,u,v)
#endif
static void TRACE(int level,const char *msg,...) {
va_list args;
SV *trace = get_sv("Net::SSLeay::trace",0);
if (trace && SvIOK(trace) && SvIV(trace)>=level) {
char buf[4096];
va_start(args,msg);
vsnprintf(buf,4095,msg,args);
warn("%s",buf);
va_end(args);
}
}
#include "constants.c"
/* ============= thread-safety related stuff ============== */
#define MY_CXT_KEY "Net::SSLeay::_guts" XS_VERSION
typedef struct {
HV* global_cb_data;
UV tid;
} my_cxt_t;
START_MY_CXT
#ifdef USE_ITHREADS
static perl_mutex LIB_init_mutex;
#if OPENSSL_VERSION_NUMBER < 0x10100000L
static perl_mutex *GLOBAL_openssl_mutex = NULL;
#endif
#endif
static int LIB_initialized;
UV get_my_thread_id(void) /* returns threads->tid() value */
{
dSP;
UV tid = 0;
#ifdef USE_ITHREADS
int count = 0;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSVpv("threads", 0)));
PUTBACK;
count = call_method("tid", G_SCALAR|G_EVAL);
SPAGAIN;
/* Caution: recent perls do not appear support threads->tid() */
if (SvTRUE(ERRSV) || count != 1)
{
/* if compatible threads not loaded or an error occurs return 0 */
tid = 0;
}
else
tid = (UV)POPi;
PUTBACK;
FREETMPS;
LEAVE;
#endif
return tid;
}
/* IMPORTANT NOTE:
* openssl locking was implemented according to http://www.openssl.org/docs/crypto/threads.html
* we implement both static and dynamic locking as described on URL above
* locking is supported when OPENSSL_THREADS macro is defined which means openssl-0.9.7 or newer
* we intentionally do not implement cleanup of openssl's threading as it causes troubles
* with apache-mpm-worker+mod_perl+mod_ssl+net-ssleay
*/
#if defined(USE_ITHREADS) && defined(OPENSSL_THREADS)
#if OPENSSL_VERSION_NUMBER < 0x10100000L
static void openssl_locking_function(int mode, int type, const char *file, int line)
{
PR3("openssl_locking_function %d %d\n", mode, type);
if (!GLOBAL_openssl_mutex) return;
if (mode & CRYPTO_LOCK)
MUTEX_LOCK(&GLOBAL_openssl_mutex[type]);
else
MUTEX_UNLOCK(&GLOBAL_openssl_mutex[type]);
}
#if OPENSSL_VERSION_NUMBER < 0x10000000L
static unsigned long openssl_threadid_func(void)
{
dMY_CXT;
return (unsigned long)(MY_CXT.tid);
}
#else
void openssl_threadid_func(CRYPTO_THREADID *id)
{
dMY_CXT;
CRYPTO_THREADID_set_numeric(id, (unsigned long)(MY_CXT.tid));
}
#endif
struct CRYPTO_dynlock_value
{
perl_mutex mutex;
};
struct CRYPTO_dynlock_value * openssl_dynlocking_create_function (const char *file, int line)
{
struct CRYPTO_dynlock_value *retval;
New(0, retval, 1, struct CRYPTO_dynlock_value);
if (!retval) return NULL;
MUTEX_INIT(&retval->mutex);
return retval;
}
void openssl_dynlocking_lock_function (int mode, struct CRYPTO_dynlock_value *l, const char *file, int line)
{
if (!l) return;
if (mode & CRYPTO_LOCK)
MUTEX_LOCK(&l->mutex);
else
MUTEX_UNLOCK(&l->mutex);
}
void openssl_dynlocking_destroy_function (struct CRYPTO_dynlock_value *l, const char *file, int line)
{
if (!l) return;
MUTEX_DESTROY(&l->mutex);
Safefree(l);
}
#endif
void openssl_threads_init(void)
{
int i;
PR1("STARTED: openssl_threads_init\n");
#if OPENSSL_VERSION_NUMBER < 0x10100000L
/* initialize static locking */
if ( !CRYPTO_get_locking_callback() ) {
#if OPENSSL_VERSION_NUMBER < 0x10000000L
if ( !CRYPTO_get_id_callback() ) {
#else
if ( !CRYPTO_THREADID_get_callback() ) {
#endif
PR2("openssl_threads_init static locking %d\n", CRYPTO_num_locks());
New(0, GLOBAL_openssl_mutex, CRYPTO_num_locks(), perl_mutex);
if (!GLOBAL_openssl_mutex) return;
for (i=0; i<CRYPTO_num_locks(); i++) MUTEX_INIT(&GLOBAL_openssl_mutex[i]);
CRYPTO_set_locking_callback((void (*)(int,int,const char *,int))openssl_locking_function);
#ifndef WIN32
/* no need for threadid_func() on Win32 */
#if OPENSSL_VERSION_NUMBER < 0x10000000L
CRYPTO_set_id_callback(openssl_threadid_func);
#else
CRYPTO_THREADID_set_callback(openssl_threadid_func);
#endif
#endif
}
}
/* initialize dynamic locking */
if ( !CRYPTO_get_dynlock_create_callback() &&
!CRYPTO_get_dynlock_lock_callback() &&
!CRYPTO_get_dynlock_destroy_callback() ) {
PR1("openssl_threads_init dynamic locking\n");
CRYPTO_set_dynlock_create_callback(openssl_dynlocking_create_function);
CRYPTO_set_dynlock_lock_callback(openssl_dynlocking_lock_function);
CRYPTO_set_dynlock_destroy_callback(openssl_dynlocking_destroy_function);
}
#endif
}
#endif
/* ============= typedefs to agument TYPEMAP ============== */
typedef void callback_no_ret(void);
typedef RSA * cb_ssl_int_int_ret_RSA(SSL * ssl,int is_export, int keylength);
typedef DH * cb_ssl_int_int_ret_DH(SSL * ssl,int is_export, int keylength);
typedef STACK_OF(X509_NAME) X509_NAME_STACK;
typedef int perl_filehandle_t;
/* ======= special handler used by EVP_MD_do_all_sorted ======= */
#if OPENSSL_VERSION_NUMBER >= 0x1000000fL
static void handler_list_md_fn(const EVP_MD *m, const char *from, const char *to, void *arg)
{
/* taken from apps/dgst.c */
const char *mname;
if (!m) return; /* Skip aliases */
mname = OBJ_nid2ln(EVP_MD_type(m));
if (strcmp(from, mname)) return; /* Skip shortnames */
#if OPENSSL_VERSION_NUMBER < 0x10100000L
if (EVP_MD_flags(m) & EVP_MD_FLAG_PKEY_DIGEST) return; /* Skip clones */
#endif
if (strchr(mname, ' ')) mname= EVP_MD_name(m);
av_push((AV *)arg, newSVpv(mname,0));
}
#endif
/* ============= callbacks - basic info =============
*
* PLEASE READ THIS BEFORE YOU ADD ANY NEW CALLBACK!!
*
* There are basically 2 types of callbacks used in SSLeay:
*
* 1/ "one-time" callbacks - these are created+used+destroyed within one perl function implemented in XS.
* These callbacks use a special C structure simple_cb_data_t to pass necessary data.
* There are 2 related helper functions: simple_cb_data_new() + simple_cb_data_free()
* For example see implementation of these functions:
* - RSA_generate_key
* - PEM_read_bio_PrivateKey
*
* 2/ "advanced" callbacks - these are setup/destroyed by one function but used by another function. These
* callbacks use global hash MY_CXT.global_cb_data to store perl functions + data to be uset at callback time.
* There are 2 related helper functions: cb_data_advanced_put() + cb_data_advanced_get() for manipulating
* global hash MY_CXT.global_cb_data which work like this:
* cb_data_advanced_put(<pointer>, "data_name", dataSV)
* >>>
* global_cb_data->{"ptr_<pointer>"}->{"data_name"} = dataSV)
* or
* data = cb_data_advanced_get(<pointer>, "data_name")
* >>>
* my $data = global_cb_data->{"ptr_<pointer>"}->{"data_name"}
* For example see implementation of these functions:
* - SSL_CTX_set_verify
* - SSL_set_verify
* - SSL_CTX_set_cert_verify_callback
* - SSL_CTX_set_default_passwd_cb
* - SSL_CTX_set_default_passwd_cb_userdata
* - SSL_set_session_secret_cb
*
* If you want to add a new callback:
* - you very likely need a new function "your_callback_name_invoke()"
* - decide whether your case fits case 1/ or 2/ (and implement likewise existing functions)
* - try to avoid adding a new style of callback implementation (or ask Net::SSLeay maintainers before)
*
*/
/* ============= callback stuff - generic functions============== */
struct _ssleay_cb_t {
SV* func;
SV* data;
};
typedef struct _ssleay_cb_t simple_cb_data_t;
simple_cb_data_t* simple_cb_data_new(SV* func, SV* data)
{
simple_cb_data_t* cb;
New(0, cb, 1, simple_cb_data_t);
if (cb) {
SvREFCNT_inc(func);
SvREFCNT_inc(data);
cb->func = func;
cb->data = (data == &PL_sv_undef) ? NULL : data;
}
return cb;
}
void simple_cb_data_free(simple_cb_data_t* cb)
{
if (cb) {
if (cb->func) {
SvREFCNT_dec(cb->func);
cb->func = NULL;
}
if (cb->data) {
SvREFCNT_dec(cb->data);
cb->data = NULL;
}
}
Safefree(cb);
}
int cb_data_advanced_put(const void *ptr, const char* data_name, SV* data)
{
HV * L2HV;
SV ** svtmp;
int len;
char key_name[500];
dMY_CXT;
len = my_snprintf(key_name, sizeof(key_name), "ptr_%p", ptr);
if (len == sizeof(key_name)) return 0; /* error - key_name too short*/
/* get or create level-2 hash */
svtmp = hv_fetch(MY_CXT.global_cb_data, key_name, strlen(key_name), 0);
if (svtmp == NULL) {
L2HV = newHV();
hv_store(MY_CXT.global_cb_data, key_name, strlen(key_name), newRV_noinc((SV*)L2HV), 0);
}
else {
if (!SvOK(*svtmp) || !SvROK(*svtmp)) return 0;
#if defined(MUTABLE_PTR)
L2HV = (HV*)MUTABLE_PTR(SvRV(*svtmp));
#else
L2HV = (HV*)(SvRV(*svtmp));
#endif
}
/* first delete already stored value */
hv_delete(L2HV, data_name, strlen(data_name), G_DISCARD);
if (data!=NULL) {
if (SvOK(data))
hv_store(L2HV, data_name, strlen(data_name), data, 0);
else
/* we're not storing data so discard it */
SvREFCNT_dec(data);
}
return 1;
}
SV* cb_data_advanced_get(const void *ptr, const char* data_name)
{
HV * L2HV;
SV ** svtmp;
int len;
char key_name[500];
dMY_CXT;
len = my_snprintf(key_name, sizeof(key_name), "ptr_%p", ptr);
if (len == sizeof(key_name)) return &PL_sv_undef; /* return undef on error - key_name too short*/
/* get level-2 hash */
svtmp = hv_fetch(MY_CXT.global_cb_data, key_name, strlen(key_name), 0);
if (svtmp == NULL) return &PL_sv_undef;
if (!SvOK(*svtmp)) return &PL_sv_undef;
if (!SvROK(*svtmp)) return &PL_sv_undef;
#if defined(MUTABLE_PTR)
L2HV = (HV*)MUTABLE_PTR(SvRV(*svtmp));
#else
L2HV = (HV*)(SvRV(*svtmp));
#endif
/* get stored data */
svtmp = hv_fetch(L2HV, data_name, strlen(data_name), 0);
if (svtmp == NULL) return &PL_sv_undef;
if (!SvOK(*svtmp)) return &PL_sv_undef;
return *svtmp;
}
int cb_data_advanced_drop(const void *ptr)
{
int len;
char key_name[500];
dMY_CXT;
len = my_snprintf(key_name, sizeof(key_name), "ptr_%p", ptr);
if (len == sizeof(key_name)) return 0; /* error - key_name too short*/
hv_delete(MY_CXT.global_cb_data, key_name, strlen(key_name), G_DISCARD);
return 1;
}
/* ============= callback stuff - invoke functions ============== */
static int ssleay_verify_callback_invoke (int ok, X509_STORE_CTX* x509_store)
{
dSP;
SSL* ssl;
int count = -1, res;
SV *cb_func;
PR1("STARTED: ssleay_verify_callback_invoke\n");
ssl = (SSL *)X509_STORE_CTX_get_ex_data(x509_store, SSL_get_ex_data_X509_STORE_CTX_idx());
cb_func = cb_data_advanced_get(ssl, "ssleay_verify_callback!!func");
if (!SvOK(cb_func)) {
SSL_CTX* ssl_ctx = SSL_get_SSL_CTX(ssl);
cb_func = cb_data_advanced_get(ssl_ctx, "ssleay_verify_callback!!func");
}
if (!SvOK(cb_func))
croak("Net::SSLeay: verify_callback called, but not set to point to any perl function.\n");
ENTER;
SAVETMPS;
PR2("verify callback glue ok=%d\n", ok);
PUSHMARK(sp);
EXTEND( sp, 2 );
PUSHs( sv_2mortal(newSViv(ok)) );
PUSHs( sv_2mortal(newSViv(PTR2IV(x509_store))) );
PUTBACK;
PR1("About to call verify callback.\n");
count = call_sv(cb_func, G_SCALAR);
PR1("Returned from verify callback.\n");
SPAGAIN;
if (count != 1)
croak ( "Net::SSLeay: verify_callback perl function did not return a scalar.\n");
res = POPi;
PUTBACK;
FREETMPS;
LEAVE;
return res;
}
static int ssleay_ctx_passwd_cb_invoke(char *buf, int size, int rwflag, void *userdata)
{
dSP;
int count = -1;
char *res;
SV *cb_func, *cb_data;
PR1("STARTED: ssleay_ctx_passwd_cb_invoke\n");
cb_func = cb_data_advanced_get(userdata, "ssleay_ctx_passwd_cb!!func");
cb_data = cb_data_advanced_get(userdata, "ssleay_ctx_passwd_cb!!data");
if(!SvOK(cb_func))
croak ("Net::SSLeay: ssleay_ctx_passwd_cb_invoke called, but not set to point to any perl function.\n");
ENTER;
SAVETMPS;
PUSHMARK(sp);
XPUSHs(sv_2mortal(newSViv(rwflag)));
XPUSHs(sv_2mortal(newSVsv(cb_data)));
PUTBACK;
count = call_sv( cb_func, G_SCALAR );
SPAGAIN;
if (count != 1)
croak("Net::SSLeay: ssleay_ctx_passwd_cb_invoke perl function did not return a scalar.\n");
res = POPp;
if (res == NULL) {
*buf = '\0';
} else {
strncpy(buf, res, size);
buf[size - 1] = '\0';
}
PUTBACK;
FREETMPS;
LEAVE;
return strlen(buf);
}
#if OPENSSL_VERSION_NUMBER >= 0x1010006fL /* In OpenSSL 1.1.0 but actually called for $ssl from 1.1.0f */
#ifndef LIBRESSL_VERSION_NUMBER
static int ssleay_ssl_passwd_cb_invoke(char *buf, int size, int rwflag, void *userdata)
{
dSP;
int count = -1;
char *res;
SV *cb_func, *cb_data;
PR1("STARTED: ssleay_ssl_passwd_cb_invoke\n");
cb_func = cb_data_advanced_get(userdata, "ssleay_ssl_passwd_cb!!func");
cb_data = cb_data_advanced_get(userdata, "ssleay_ssl_passwd_cb!!data");
if(!SvOK(cb_func))
croak ("Net::SSLeay: ssleay_ssl_passwd_cb_invoke called, but not set to point to any perl function.\n");
ENTER;
SAVETMPS;
PUSHMARK(sp);
XPUSHs(sv_2mortal(newSViv(rwflag)));
XPUSHs(sv_2mortal(newSVsv(cb_data)));
PUTBACK;
count = call_sv( cb_func, G_SCALAR );
SPAGAIN;
if (count != 1)
croak("Net::SSLeay: ssleay_ssl_passwd_cb_invoke perl function did not return a scalar.\n");
res = POPp;
if (res == NULL) {
*buf = '\0';
} else {
strncpy(buf, res, size);
buf[size - 1] = '\0';
}
PUTBACK;
FREETMPS;
LEAVE;
return strlen(buf);
}
#endif /* !LibreSSL */
#endif /* >= 1.1.0f */
int ssleay_ctx_cert_verify_cb_invoke(X509_STORE_CTX* x509_store_ctx, void* data)
{
dSP;
int count = -1;
int res;
SV * cb_func, *cb_data;
PR1("STARTED: ssleay_ctx_cert_verify_cb_invoke\n");
cb_func = cb_data_advanced_get(data, "ssleay_ctx_cert_verify_cb!!func");
cb_data = cb_data_advanced_get(data, "ssleay_ctx_cert_verify_cb!!data");
if(!SvOK(cb_func))
croak ("Net::SSLeay: ssleay_ctx_cert_verify_cb_invoke called, but not set to point to any perl function.\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSViv(PTR2IV(x509_store_ctx))));
XPUSHs(sv_2mortal(newSVsv(cb_data)));
PUTBACK;
count = call_sv(cb_func, G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Net::SSLeay: ssleay_ctx_cert_verify_cb_invoke perl function did not return a scalar.\n");
res = POPi;
PUTBACK;
FREETMPS;
LEAVE;
return res;
}
#if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
int tlsext_servername_callback_invoke(SSL *ssl, int *ad, void *arg)
{
dSP;
int count = -1;
int res;
SV * cb_func, *cb_data;
PR1("STARTED: tlsext_servername_callback_invoke\n");
cb_func = cb_data_advanced_get(arg, "tlsext_servername_callback!!func");
cb_data = cb_data_advanced_get(arg, "tlsext_servername_callback!!data");
if(!SvOK(cb_func))
croak ("Net::SSLeay: tlsext_servername_callback_invoke called, but not set to point to any perl function.\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSViv(PTR2IV(ssl))));
XPUSHs(sv_2mortal(newSVsv(cb_data)));
PUTBACK;
count = call_sv(cb_func, G_SCALAR);
SPAGAIN;
if (count != 1)
croak("Net::SSLeay: tlsext_servername_callback_invoke perl function did not return a scalar.\n");
res = POPi;
PUTBACK;
FREETMPS;
LEAVE;
return res;
}
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10000000L && !defined(OPENSSL_NO_TLSEXT)
int tlsext_status_cb_invoke(SSL *ssl, void *arg)
{
dSP;
SV *cb_func, *cb_data;
SSL_CTX *ctx = SSL_get_SSL_CTX(ssl);
int len,res,nres = -1;
const unsigned char *p = NULL;
OCSP_RESPONSE *ocsp_response = NULL;
cb_func = cb_data_advanced_get(ctx, "tlsext_status_cb!!func");
cb_data = cb_data_advanced_get(ctx, "tlsext_status_cb!!data");
if ( ! SvROK(cb_func) || (SvTYPE(SvRV(cb_func)) != SVt_PVCV))
croak ("Net::SSLeay: tlsext_status_cb_invoke called, but not set to point to any perl function.\n");
len = SSL_get_tlsext_status_ocsp_resp(ssl, &p);
if (p) ocsp_response = d2i_OCSP_RESPONSE(NULL, &p, len);
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSViv(PTR2IV(ssl))));
PUSHs( sv_2mortal(newSViv(PTR2IV(ocsp_response))) );
XPUSHs(sv_2mortal(newSVsv(cb_data)));
PUTBACK;
nres = call_sv(cb_func, G_SCALAR);
if (ocsp_response) OCSP_RESPONSE_free(ocsp_response);
SPAGAIN;
if (nres != 1)
croak("Net::SSLeay: tlsext_status_cb_invoke perl function did not return a scalar.\n");
res = POPi;
PUTBACK;
FREETMPS;
LEAVE;
return res;
}
int session_ticket_ext_cb_invoke(SSL *ssl, const unsigned char *data, int len, void *arg)
{
dSP;
SV *cb_func, *cb_data;
int res,nres = -1;
cb_func = cb_data_advanced_get(arg, "session_ticket_ext_cb!!func");
cb_data = cb_data_advanced_get(arg, "session_ticket_ext_cb!!data");
if ( ! SvROK(cb_func) || (SvTYPE(SvRV(cb_func)) != SVt_PVCV))
croak ("Net::SSLeay: session_ticket_ext_cb_invoke called, but not set to point to any perl function.\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(sv_2mortal(newSViv(PTR2IV(ssl))));
XPUSHs(sv_2mortal(newSVpvn((const char *)data, len)));
XPUSHs(sv_2mortal(newSVsv(cb_data)));
PUTBACK;
nres = call_sv(cb_func, G_SCALAR);
SPAGAIN;
if (nres != 1)
croak("Net::SSLeay: session_ticket_ext_cb_invoke perl function did not return a scalar.\n");
res = POPi;
PUTBACK;
FREETMPS;
LEAVE;
return res;
}
#endif
#ifdef SSL_F_SSL_SET_SESSION_TICKET_EXT
int ssleay_session_secret_cb_invoke(SSL* s, void* secret, int *secret_len,
STACK_OF(SSL_CIPHER) *peer_ciphers,
const SSL_CIPHER **cipher, void *arg)
{
dSP;
int count = -1, res, i;
AV *ciphers = newAV();
SV *pref_cipher = sv_newmortal();
SV * cb_func, *cb_data;
SV * secretsv;
PR1("STARTED: ssleay_session_secret_cb_invoke\n");
cb_func = cb_data_advanced_get(arg, "ssleay_session_secret_cb!!func");
cb_data = cb_data_advanced_get(arg, "ssleay_session_secret_cb!!data");
if(!SvOK(cb_func))
croak ("Net::SSLeay: ssleay_ctx_passwd_cb_invoke called, but not set to point to any perl function.\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
secretsv = sv_2mortal( newSVpv((const char *)secret, *secret_len));
XPUSHs(secretsv);
for (i=0; i<sk_SSL_CIPHER_num(peer_ciphers); i++) {
const SSL_CIPHER *c = sk_SSL_CIPHER_value(peer_ciphers,i);
av_store(ciphers, i, sv_2mortal(newSVpv(SSL_CIPHER_get_name(c), 0)));
}
XPUSHs(sv_2mortal(newRV_inc((SV*)ciphers)));
XPUSHs(sv_2mortal(newRV_inc(pref_cipher)));
XPUSHs(sv_2mortal(newSVsv(cb_data)));
PUTBACK;
count = call_sv( cb_func, G_SCALAR );
SPAGAIN;
if (count != 1)
croak ("Net::SSLeay: ssleay_session_secret_cb_invoke perl function did not return a scalar.\n");
res = POPi;
if (res) {
/* See if there is a preferred cipher selected, if so it is an index into the stack */
if (SvIOK(pref_cipher))
*cipher = sk_SSL_CIPHER_value(peer_ciphers, SvIV(pref_cipher));
#if OPENSSL_VERSION_NUMBER >= 0x10100000L
{
/* Use any new master secret set by the callback function in secret */
STRLEN newsecretlen;
char* newsecretdata = SvPV(secretsv, newsecretlen);
if (*secret_len < 0 || newsecretlen > (STRLEN)*secret_len)
croak("Net::SSLeay: ssleay_session_secret_cb_invoke perl function returned too long secret: %ld > %ld.\n", (long)newsecretlen, (long)*secret_len);
memcpy(secret, newsecretdata, newsecretlen);
*secret_len = newsecretlen;
}
#endif
}
PUTBACK;
FREETMPS;
LEAVE;
return res;
}
#endif
#if OPENSSL_VERSION_NUMBER >= 0x10000000L && !defined(OPENSSL_NO_PSK)
#define NET_SSLEAY_CAN_PSK_CLIENT_CALLBACK
unsigned int ssleay_set_psk_client_callback_invoke(SSL *ssl, const char *hint,
char *identity, unsigned int max_identity_len,
unsigned char *psk, unsigned int max_psk_len)
{
dSP;
int count = -1;
char *identity_val, *psk_val;
unsigned int psk_len = 0;
BIGNUM *psk_bn = NULL;
SV * cb_func;
SV * hintsv;
/* this n_a is required for building with old perls: */
STRLEN n_a;
PR1("STARTED: ssleay_set_psk_client_callback_invoke\n");
cb_func = cb_data_advanced_get(ssl, "ssleay_set_psk_client_callback!!func");
if(!SvOK(cb_func))
croak ("Net::SSLeay: ssleay_set_psk_client_callback_invoke called, but not set to point to any perl function.\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
if (hint != NULL) {
hintsv = sv_2mortal( newSVpv(hint, strlen(hint)));
XPUSHs(hintsv);
}