SSLeay.xs

filter: Statementstmt 0.0% 0 / 139 Branchbran 0.0% 0 / 94 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 233 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Crypt/SSLeay.pm ↑ Summary blib/lib/Crypt/SSLeay/CTX.pm »
1 /*
2 * $Id: SSLeay.xs,v 1.2 2000/05/10 16:37:25 ben Exp $
3 * Copyright 1998 Gisle Aas.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the same terms as Perl itself.
7 */
8  
9 #ifdef __cplusplus
10 extern "C" {
11 #endif
12 #include "EXTERN.h"
13 #include "perl.h"
14  
15 /* CRYPT_SSLEAY_free() will not be #defined to be free() now that we're no
16 * longer supporting pre-2000 OpenSSL.
17 #define NO_XSLOCKS
18 */
19  
20 #include "XSUB.h"
21  
22 /* build problem under openssl 0.9.6 and some builds of perl 5.8.x */
23 #ifndef PERL5
24 #define PERL5 1
25 #endif
26  
27 /* Makefile.PL no longer generates the following header file
28 * #include "crypt_ssleay_version.h"
29 * Among other things, Makefile.PL used to determine whether
30 * to use #include<openssl/ssl.h> or #include<ssl.h> and
31 * whether to use OPENSSL_free or free etc, but such distinctions
32 * ceased to matter pre-2000. Crypt::SSLeay no longer supports
33 * pre-2000 OpenSSL */
34  
35 #include <openssl/ssl.h>
36 #include <openssl/crypto.h>
37 #include <openssl/err.h>
38 #include <openssl/rand.h>
39 #include <openssl/pkcs12.h>
40  
41 #define CRYPT_SSLEAY_free OPENSSL_free
42  
43 #undef Free /* undo namespace pollution from crypto.h */
44 #ifdef __cplusplus
45 }
46 #endif
47  
48  
49 #if SSLEAY_VERSION_NUMBER >= 0x0900
50 #define CRYPT_SSL_CLIENT_METHOD SSLv3_client_method()
51 #else
52 #define CRYPT_SSL_CLIENT_METHOD SSLv2_client_method()
53 #endif
54  
55 0 static void InfoCallback(const SSL *s,int where,int ret)
56 {
57 const char *str;
58 int w;
59  
60 0 w = where & ~SSL_ST_MASK;
61  
62 0 ▶ if(w & SSL_ST_CONNECT)
Branch: 1 branch
BranchTrueFalseTotal
if(w & SSL_ST_CONNECT) 0 0 0
63 0 str="SSL_connect";
64 0 ▶ else if(w & SSL_ST_ACCEPT)
Branch: 1 branch
BranchTrueFalseTotal
else if(w & SSL_ST_ACCEPT) 0 0 0
65 0 str="SSL_accept";
66 else
67 0 str="undefined";
68  
69 0 ▶ if(where & SSL_CB_LOOP) {
Branch: 1 branch
BranchTrueFalseTotal
if(where & SSL_CB_LOOP) { 0 0 0
70 0 fprintf(stderr,"%s:%s\n",str,SSL_state_string_long(s));
71 }
72 0 ▶ else if(where & SSL_CB_ALERT) {
Branch: 1 branch
BranchTrueFalseTotal
else if(where & SSL_CB_ALERT) { 0 0 0
73 0 ▶ str=(where & SSL_CB_READ)?"read":"write";
Branch: 1 branch
BranchTrueFalseTotal
str=(where & SSL_CB_READ)?"read":"write"; 0 0 0
74 0 fprintf(stderr,"SSL3 alert %s:%s:%s\n",str,
75 SSL_alert_type_string_long(ret),
76 SSL_alert_desc_string_long(ret));
77 }
78 0 ▶ else if(where & SSL_CB_EXIT) {
Branch: 1 branch
BranchTrueFalseTotal
else if(where & SSL_CB_EXIT) { 0 0 0
79 0 ▶ if(ret == 0)
Branch: 1 branch
BranchTrueFalseTotal
if(ret == 0) 0 0 0
80 0 fprintf(stderr,"%s:failed in %s\n",str,SSL_state_string_long(s));
81 0 ▶ else if (ret < 0)
Branch: 1 branch
BranchTrueFalseTotal
else if (ret < 0) 0 0 0
82 0 fprintf(stderr,"%s:error in %s\n",str,SSL_state_string_long(s));
83 }
84 0 }
85  
86 MODULE = Crypt::SSLeay PACKAGE = Crypt::SSLeay
87  
88 PROTOTYPES: DISABLE
89  
90 MODULE = Crypt::SSLeay PACKAGE = Crypt::SSLeay::Err PREFIX = ERR_
91  
92 #define CRYPT_SSLEAY_ERR_BUFSIZE 1024
93  
94 const char *
95 ERR_get_error_string()
96 PREINIT:
97 unsigned long code;
98 char buf[ CRYPT_SSLEAY_ERR_BUFSIZE ];
99  
100 CODE:
101 0 ▶ if ((code = ERR_get_error()) == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ((code = ERR_get_error()) == 0) { 0 0 0
102 0 RETVAL = NULL;
103 }
104 else {
105 /* www.openssl.org/docs/crypto/ERR_error_string.html */
106 0 ERR_error_string_n(code, buf, CRYPT_SSLEAY_ERR_BUFSIZE);
107 0 RETVAL = buf;
108 }
109 OUTPUT:
110 RETVAL
111  
112 MODULE = Crypt::SSLeay PACKAGE = Crypt::SSLeay::CTX PREFIX = SSL_CTX_
113  
114 #define CRYPT_SSLEAY_RAND_BUFSIZE 1024
115  
116 SSL_CTX*
117 SSL_CTX_new(packname, ssl_version)
118 SV* packname
119 int ssl_version
120 CODE:
121 SSL_CTX* ctx;
122 static int bNotFirstTime;
123 char buf[ CRYPT_SSLEAY_RAND_BUFSIZE ];
124  
125 0 ▶ if(!bNotFirstTime) {
Branch: 1 branch
BranchTrueFalseTotal
if(!bNotFirstTime) { 0 0 0
126 0 OpenSSL_add_all_algorithms();
127 0 SSL_load_error_strings();
128 0 ERR_load_crypto_strings();
129 0 SSL_library_init();
130 0 bNotFirstTime = 1;
131 }
132  
133 /**** Code from Devin Heitmueller, 10/3/2002 ****/
134 /**** Use /dev/urandom to seed if available ****/
135 /* ASU: 2014/04/23 It looks like it is OK to leave
136 * this in. See following thread:
137 * http://security.stackexchange.com/questions/56469/
138 */
139 0 ▶ if (RAND_load_file("/dev/urandom", CRYPT_SSLEAY_RAND_BUFSIZE)
Branch: 1 branch
BranchTrueFalseTotal
if (RAND_load_file("/dev/urandom", CRYPT_SSLEAY_RAND_BUFSIZE) 0 0 0
140 != CRYPT_SSLEAY_RAND_BUFSIZE)
141 {
142 /* Couldn't read /dev/urandom, just seed off
143 * of the stack variable (the old way)
144 */
145 0 RAND_seed(buf, CRYPT_SSLEAY_RAND_BUFSIZE);
146 }
147  
148 0 ▶ if(ssl_version == 23) {
Branch: 1 branch
BranchTrueFalseTotal
if(ssl_version == 23) { 0 0 0
149 0 ctx = SSL_CTX_new(SSLv23_client_method());
150 }
151 0 ▶ else if(ssl_version == 3) {
Branch: 1 branch
BranchTrueFalseTotal
else if(ssl_version == 3) { 0 0 0
152 0 ctx = SSL_CTX_new(SSLv3_client_method());
153 }
154 else {
155 #ifndef OPENSSL_NO_SSL2
156 /* v2 is the default */
157 0 ctx = SSL_CTX_new(SSLv2_client_method());
158 #else
159 /* v3 is the default */
160 ctx = SSL_CTX_new(SSLv3_client_method());
161 #endif
162 }
163  
164 0 SSL_CTX_set_options(ctx,SSL_OP_ALL|0);
165 0 SSL_CTX_set_default_verify_paths(ctx);
166 0 SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
167 0 RETVAL = ctx;
168 OUTPUT:
169 RETVAL
170  
171 void
172 SSL_CTX_free(ctx)
173 SSL_CTX* ctx
174  
175 int
176 SSL_CTX_set_cipher_list(ctx, ciphers)
177 SSL_CTX* ctx
178 char* ciphers
179  
180 int
181 SSL_CTX_use_certificate_file(ctx, filename, mode)
182 SSL_CTX* ctx
183 char* filename
184 int mode
185  
186 int
187 SSL_CTX_use_PrivateKey_file(ctx, filename ,mode)
188 SSL_CTX* ctx
189 char* filename
190 int mode
191  
192 int
193 SSL_CTX_use_pkcs12_file(ctx, filename, password)
194 SSL_CTX* ctx
195 const char *filename
196 const char *password
197 PREINIT:
198 FILE *fp;
199 EVP_PKEY *pkey;
200 X509 *cert;
201 0 ▶ STACK_OF(X509) *ca = NULL;
Branch: 1 branch
BranchTrueFalseTotal
STACK_OF(X509) *ca = NULL; 0 0 0
202 PKCS12 *p12;
203 CODE:
204 0 ▶ if ((fp = fopen(filename, "rb"))) {
Branch: 1 branch
BranchTrueFalseTotal
if ((fp = fopen(filename, "rb"))) { 0 0 0
205 0 p12 = d2i_PKCS12_fp(fp, NULL);
206 0 fclose (fp);
207  
208 0 ▶ if (p12) {
Branch: 1 branch
BranchTrueFalseTotal
if (p12) { 0 0 0
209 0 ▶ if(PKCS12_parse(p12, password, &pkey, &cert, &ca)) {
Branch: 1 branch
BranchTrueFalseTotal
if(PKCS12_parse(p12, password, &pkey, &cert, &ca)) { 0 0 0
210 0 ▶ if (pkey) {
Branch: 1 branch
BranchTrueFalseTotal
if (pkey) { 0 0 0
211 0 RETVAL = SSL_CTX_use_PrivateKey(ctx, pkey);
212 0 EVP_PKEY_free(pkey);
213 }
214 0 ▶ if (cert) {
Branch: 1 branch
BranchTrueFalseTotal
if (cert) { 0 0 0
215 0 RETVAL = SSL_CTX_use_certificate(ctx, cert);
216 0 X509_free(cert);
217 }
218 }
219 0 PKCS12_free(p12);
220 }
221 }
222 OUTPUT:
223 RETVAL
224  
225  
226 int
227 SSL_CTX_check_private_key(ctx)
228 SSL_CTX* ctx
229  
230 SV*
231 SSL_CTX_set_verify(ctx)
232 SSL_CTX* ctx
233 PREINIT:
234 char* CAfile;
235 char* CAdir;
236 CODE:
237 0 CAfile=getenv("HTTPS_CA_FILE");
238 0 CAdir =getenv("HTTPS_CA_DIR");
239  
240 0 ▶ if(!CAfile && !CAdir) {
Branch: 2 branches
BranchTrueFalseTotal
if(!CAfile && !CAdir) { 0 0 0
if(!CAfile && !CAdir) { 0 0 0
241 0 SSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, NULL);
242 0 RETVAL = newSViv(0);
243 }
244 else {
245 0 SSL_CTX_load_verify_locations(ctx,CAfile,CAdir);
246 0 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
247 0 RETVAL = newSViv(1);
248 }
249 OUTPUT:
250 RETVAL
251  
252 MODULE = Crypt::SSLeay PACKAGE = Crypt::SSLeay::Conn PREFIX = SSL_
253  
254 SSL*
255 SSL_new(packname, ctx, debug, ...)
256 SV* packname
257 SSL_CTX* ctx
258 SV* debug
259 PREINIT:
260 SSL* ssl;
261 CODE:
262 0 ssl = SSL_new(ctx);
263 0 SSL_set_connect_state(ssl);
264 /* The set mode is necessary so the SSL connection can
265 * survive a renegotiated cipher that results from
266 * modssl VerifyClient config changing between
267 * VirtualHost & some other config block. At modssl
268 * this would be a [trace] ssl message:
269 * "Changed client verification type will force renegotiation"
270 * -- jc 6/28/2001
271 */
272 #ifdef SSL_MODE_AUTO_RETRY
273 0 SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY);
274 #endif
275 0 RETVAL = ssl;
276 0 ▶ if(SvTRUE(debug)) {
Branch: 1 branch
BranchTrueFalseTotal
if(SvTRUE(debug)) { 0 0 0
277 0 SSL_set_info_callback(RETVAL,InfoCallback);
278 }
279 0 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 0 0
280 0 PerlIO* io = IoIFP(sv_2io(ST(3)));
281 #ifdef _WIN32
282 SSL_set_fd(RETVAL, _get_osfhandle(PerlIO_fileno(io)));
283 #else
284 0 SSL_set_fd(RETVAL, PerlIO_fileno(io));
285 #endif
286 }
287 OUTPUT:
288 RETVAL
289  
290 void
291 SSL_free(ssl)
292 SSL* ssl
293  
294 int
295 SSL_pending(ssl)
296 SSL* ssl
297  
298 int
299 SSL_set_fd(ssl,fd)
300 SSL* ssl
301 int fd
302  
303 int
304 SSL_connect(ssl)
305 SSL* ssl
306  
307 int
308 SSL_accept(ssl)
309 SSL* ssl
310  
311 SV*
312 SSL_write(ssl, buf, ...)
313 SSL* ssl
314 PREINIT:
315 STRLEN blen;
316 int len;
317 0 int offset = 0;
318 0 int keep_trying_to_write = 1;
319 INPUT:
320 char* buf = SvPV(ST(1), blen);
321 CODE:
322 0 ▶ if (items > 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 2) { 0 0 0
323 0 ▶ len = SvOK(ST(2)) ? SvIV(ST(2)) : blen;
Branch: 1 branch
BranchTrueFalseTotal
len = SvOK(ST(2)) ? SvIV(ST(2)) : blen; 0 0 0
324 0 ▶ if (items > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) { 0 0 0
325 0 offset = SvIV(ST(3));
326 0 ▶ if (offset < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset < 0) { 0 0 0
327 0 ▶ if (-offset > blen)
Branch: 1 branch
BranchTrueFalseTotal
if (-offset > blen) 0 0 0
328 0 croak("Offset outside string");
329 0 offset += blen;
330 }
331 0 ▶ else if (offset >= blen && blen > 0)
Branch: 2 branches
BranchTrueFalseTotal
else if (offset >= blen && blen > 0) 0 0 0
else if (offset >= blen && blen > 0) 0 0 0
332 0 croak("Offset outside string");
333 }
334 0 ▶ if (len > blen - offset)
Branch: 1 branch
BranchTrueFalseTotal
if (len > blen - offset) 0 0 0
335 0 len = blen - offset;
336 }
337 else {
338 0 len = blen;
339 }
340  
341 /* try to handle incomplete writes properly
342 * see RT bug #64054 and RT bug #78695
343 * 2012/08/02: Stop trying to distinguish between good & bad
344 * zero returns from underlying SSL_read/SSL_write
345 */
346 0 ▶ while (keep_trying_to_write)
Branch: 1 branch
BranchTrueFalseTotal
while (keep_trying_to_write) 0 0 0
347 {
348 0 int n = SSL_write(ssl, buf+offset, len);
349 0 int x = SSL_get_error(ssl, n);
350  
351 0 ▶ if ( n >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( n >= 0 ) 0 0 0
352 {
353 0 keep_trying_to_write = 0;
354 0 RETVAL = newSViv(n);
355 }
356 else
357 {
358 if
359 0 ▶ (
Branch: 1 branch
BranchTrueFalseTotal
( 0 0 0
360 0 ▶ (x != SSL_ERROR_WANT_READ) &&
Branch: 1 branch
BranchTrueFalseTotal
(x != SSL_ERROR_WANT_READ) && 0 0 0
361 (x != SSL_ERROR_WANT_WRITE)
362 )
363 {
364 0 keep_trying_to_write = 0;
365 0 RETVAL = &PL_sv_undef;
366 }
367 }
368 }
369 OUTPUT:
370 RETVAL
371  
372 SV*
373 SSL_read(ssl, buf, len,...)
374 SSL* ssl
375 int len
376 PREINIT:
377 char *buf;
378 STRLEN blen;
379 0 int offset = 0;
380 0 int keep_trying_to_read = 1;
381 INPUT:
382 SV* sv = ST(1);
383 CODE:
384 0 buf = SvPV_force(sv, blen);
385 0 ▶ if (items > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (items > 3) { 0 0 0
386 0 offset = SvIV(ST(3));
387 0 ▶ if (offset < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (offset < 0) { 0 0 0
388 0 ▶ if (-offset > blen)
Branch: 1 branch
BranchTrueFalseTotal
if (-offset > blen) 0 0 0
389 0 croak("Offset outside string");
390 0 offset += blen;
391 }
392 /* this is not a very efficient method of appending
393 * (offset - blen) NUL bytes, but it will probably
394 * seldom happen.
395 */
396 0 ▶ while (offset > blen) {
Branch: 1 branch
BranchTrueFalseTotal
while (offset > blen) { 0 0 0
397 0 sv_catpvn(sv, "\0", 1);
398 0 blen++;
399 }
400 }
401 0 ▶ if (len < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (len < 0) 0 0 0
402 0 croak("Negative length");
403  
404 0 ▶ SvGROW(sv, offset + len + 1);
Branch: 2 branches
BranchTrueFalseTotal
SvGROW(sv, offset + len + 1); 0 0 0
SvGROW(sv, offset + len + 1); 0 0 0
405 0 buf = SvPVX(sv); /* it might have been relocated */
406  
407 /* try to handle incomplete writes properly
408 * see RT bug #64054 and RT bug #78695
409 * 2012/08/02: Stop trying to distinguish between good & bad
410 * zero returns from underlying SSL_read/SSL_write
411 */
412 0 ▶ while (keep_trying_to_read) {
Branch: 1 branch
BranchTrueFalseTotal
while (keep_trying_to_read) { 0 0 0
413 0 int n = SSL_read(ssl, buf+offset, len);
414 0 int x = SSL_get_error(ssl, n);
415  
416 0 ▶ if ( n >= 0 )
Branch: 1 branch
BranchTrueFalseTotal
if ( n >= 0 ) 0 0 0
417 {
418 0 SvCUR_set(sv, offset + n);
419 0 buf[offset + n] = '\0';
420 0 keep_trying_to_read = 0;
421 0 RETVAL = newSViv(n);
422 }
423 else
424 {
425 if
426 0 ▶ (
Branch: 1 branch
BranchTrueFalseTotal
( 0 0 0
427 0 ▶ (x != SSL_ERROR_WANT_READ) &&
Branch: 1 branch
BranchTrueFalseTotal
(x != SSL_ERROR_WANT_READ) && 0 0 0
428 (x != SSL_ERROR_WANT_WRITE)
429 )
430 {
431 0 keep_trying_to_read = 0;
432 0 RETVAL = &PL_sv_undef;
433 }
434 }
435 }
436 OUTPUT:
437 RETVAL
438  
439 X509*
440 SSL_get_peer_certificate(ssl)
441 SSL* ssl
442  
443 SV*
444 SSL_get_verify_result(ssl)
445 SSL* ssl
446 CODE:
447 0 RETVAL = newSViv((SSL_get_verify_result(ssl) == X509_V_OK) ? 1 : 0);
448 OUTPUT:
449 RETVAL
450  
451 #define CRYPT_SSLEAY_SHARED_CIPHERS_BUFSIZE 512
452  
453 char*
454 SSL_get_shared_ciphers(ssl)
455 SSL* ssl
456 PREINIT:
457 char buf[ CRYPT_SSLEAY_SHARED_CIPHERS_BUFSIZE ];
458 CODE:
459 0 RETVAL = SSL_get_shared_ciphers(
460 ssl, buf, CRYPT_SSLEAY_SHARED_CIPHERS_BUFSIZE
461 );
462 OUTPUT:
463 RETVAL
464  
465 char*
466 SSL_get_cipher(ssl)
467 SSL* ssl
468 CODE:
469 0 RETVAL = (char*) SSL_get_cipher(ssl);
470 OUTPUT:
471 RETVAL
472  
473 #if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
474  
475 void
476 SSL_set_tlsext_host_name(ssl, name)
477 SSL *ssl
478 const char *name
479  
480 #endif
481  
482 MODULE = Crypt::SSLeay PACKAGE = Crypt::SSLeay::X509 PREFIX = X509_
483  
484 void
485 X509_free(cert)
486 X509* cert
487  
488 SV*
489 subject_name(cert)
490 X509* cert
491 PREINIT:
492 char* str;
493 CODE:
494 0 str = X509_NAME_oneline(X509_get_subject_name(cert), NULL, 0);
495 0 RETVAL = newSVpv(str, 0);
496 0 CRYPT_SSLEAY_free(str);
497 OUTPUT:
498 RETVAL
499  
500 SV*
501 issuer_name(cert)
502 X509* cert
503 PREINIT:
504 char* str;
505 CODE:
506 0 str = X509_NAME_oneline(X509_get_issuer_name(cert), NULL, 0);
507 0 RETVAL = newSVpv(str, 0);
508 0 CRYPT_SSLEAY_free(str);
509 OUTPUT:
510 RETVAL
511  
512 char *
513 get_notBeforeString(cert)
514 X509* cert
515 CODE:
516 0 RETVAL = (char *)X509_get_notBefore(cert)->data;
517 OUTPUT:
518 RETVAL
519  
520 char *
521 get_notAfterString(cert)
522 X509* cert
523 CODE:
524 0 RETVAL = (char *)X509_get_notAfter(cert)->data;
525 OUTPUT:
526 RETVAL
527  
528 MODULE = Crypt::SSLeay PACKAGE = Crypt::SSLeay::Version PREFIX = VERSION_
529  
530 const char *
531 VERSION_openssl_version()
532 CODE:
533 0 RETVAL = SSLeay_version(SSLEAY_VERSION);
534 OUTPUT:
535 RETVAL
536  
537 long
538 VERSION_openssl_version_number()
539 CODE:
540 0 ▶ RETVAL = OPENSSL_VERSION_NUMBER;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = OPENSSL_VERSION_NUMBER; 0 0 0
541 OUTPUT:
542 RETVAL
543  
544 const char *
545 VERSION_openssl_cflags()
546 CODE:
547 0 RETVAL = SSLeay_version(SSLEAY_CFLAGS);
548 OUTPUT:
549 RETVAL
550  
551 const char *
552 VERSION_openssl_platform()
553 CODE:
554 0 RETVAL = SSLeay_version(SSLEAY_PLATFORM);
555 OUTPUT:
556 RETVAL
557  
558 const char *
559 VERSION_openssl_built_on()
560 CODE:
561 0 RETVAL = SSLeay_version(SSLEAY_BUILT_ON);
562 OUTPUT:
563 RETVAL
564  
565 const char *
566 VERSION_openssl_dir()
567 CODE:
568 0 RETVAL = SSLeay_version(SSLEAY_DIR);
569 OUTPUT:
570 RETVAL
571  
572  
« blib/lib/Crypt/SSLeay.pm ↑ Summary blib/lib/Crypt/SSLeay/CTX.pm »