From 58964a492275ca9a59a0cd9c8155cb2491b4b909 Mon Sep 17 00:00:00 2001 From: "Ralf S. Engelschall" Date: Mon, 21 Dec 1998 10:56:39 +0000 Subject: Import of old SSLeay release: SSLeay 0.9.0b --- perl/MANIFEST | 17 ++ perl/Makefile.PL | 25 +++ perl/OpenSSL.xs | 63 ++++++ perl/SSLeay.pm | 78 +++++++ perl/SSLeay.xs | 63 ++++++ perl/b.pl | 21 ++ perl/bio.pl | 28 +++ perl/bio.txt | 36 +++ perl/bio.xs | 448 +++++++++++++++++++++++++++++++++++++ perl/bn.pl | 23 ++ perl/bn.txt | 38 ++++ perl/bn.xs | 589 +++++++++++++++++++++++++++++++++++++++++++++++++ perl/callback.c | 103 +++++++++ perl/cipher.pl | 39 ++++ perl/cipher.txt | 10 + perl/cipher.xs | 152 +++++++++++++ perl/dh.pl | 40 ++++ perl/digest.txt | 7 + perl/digest.xs | 83 +++++++ perl/err.txt | 2 + perl/err.xs | 46 ++++ perl/f.pl | 25 +++ perl/g.pl | 18 ++ perl/gen_rsa.pl | 49 ++++ perl/mul.pl | 56 +++++ perl/openssl.h | 96 ++++++++ perl/openssl_bio.xs | 448 +++++++++++++++++++++++++++++++++++++ perl/openssl_bn.xs | 589 +++++++++++++++++++++++++++++++++++++++++++++++++ perl/openssl_cb.c | 103 +++++++++ perl/openssl_cipher.xs | 152 +++++++++++++ perl/openssl_digest.xs | 83 +++++++ perl/openssl_err.xs | 46 ++++ perl/openssl_ssl.xs | 474 +++++++++++++++++++++++++++++++++++++++ perl/openssl_x509.xs | 74 +++++++ perl/p5SSLeay.h | 96 ++++++++ perl/r.pl | 56 +++++ perl/s.pl | 72 ++++++ perl/s2.pl | 49 ++++ perl/server.pem | 369 +++++++++++++++++++++++++++++++ perl/ss.pl | 64 ++++++ perl/ssl.pl | 71 ++++++ perl/ssl.txt | 43 ++++ perl/ssl.xs | 474 +++++++++++++++++++++++++++++++++++++++ perl/ssl_srvr.pl | 35 +++ perl/sslbio.pl | 40 ++++ perl/t.pl | 12 + perl/test | 32 +++ perl/test.pl | 30 +++ perl/test.txt | 36 +++ perl/test2.pl | 28 +++ perl/test3.pl | 19 ++ perl/test8.pl | 19 ++ perl/test9.pl | 38 ++++ perl/testbn.pl | 23 ++ perl/testdec.pl | 14 ++ perl/testmd.pl | 26 +++ perl/tt.pl | 15 ++ perl/typemap | 96 ++++++++ perl/x509.txt | 6 + perl/x509.xs | 74 +++++++ perl/xstmp.c | 102 +++++++++ perl/y.pl | 7 + perl/yy.pl | 19 ++ perl/z.pl | 32 +++ perl/zz.pl | 22 ++ 65 files changed, 6143 insertions(+) create mode 100644 perl/MANIFEST create mode 100644 perl/Makefile.PL create mode 100644 perl/OpenSSL.xs create mode 100644 perl/SSLeay.pm create mode 100644 perl/SSLeay.xs create mode 100644 perl/b.pl create mode 100644 perl/bio.pl create mode 100644 perl/bio.txt create mode 100644 perl/bio.xs create mode 100644 perl/bn.pl create mode 100644 perl/bn.txt create mode 100644 perl/bn.xs create mode 100644 perl/callback.c create mode 100644 perl/cipher.pl create mode 100644 perl/cipher.txt create mode 100644 perl/cipher.xs create mode 100644 perl/dh.pl create mode 100644 perl/digest.txt create mode 100644 perl/digest.xs create mode 100644 perl/err.txt create mode 100644 perl/err.xs create mode 100644 perl/f.pl create mode 100644 perl/g.pl create mode 100644 perl/gen_rsa.pl create mode 100644 perl/mul.pl create mode 100644 perl/openssl.h create mode 100644 perl/openssl_bio.xs create mode 100644 perl/openssl_bn.xs create mode 100644 perl/openssl_cb.c create mode 100644 perl/openssl_cipher.xs create mode 100644 perl/openssl_digest.xs create mode 100644 perl/openssl_err.xs create mode 100644 perl/openssl_ssl.xs create mode 100644 perl/openssl_x509.xs create mode 100644 perl/p5SSLeay.h create mode 100644 perl/r.pl create mode 100644 perl/s.pl create mode 100644 perl/s2.pl create mode 100644 perl/server.pem create mode 100644 perl/ss.pl create mode 100644 perl/ssl.pl create mode 100644 perl/ssl.txt create mode 100644 perl/ssl.xs create mode 100644 perl/ssl_srvr.pl create mode 100644 perl/sslbio.pl create mode 100644 perl/t.pl create mode 100644 perl/test create mode 100644 perl/test.pl create mode 100644 perl/test.txt create mode 100644 perl/test2.pl create mode 100644 perl/test3.pl create mode 100644 perl/test8.pl create mode 100644 perl/test9.pl create mode 100644 perl/testbn.pl create mode 100644 perl/testdec.pl create mode 100644 perl/testmd.pl create mode 100644 perl/tt.pl create mode 100644 perl/typemap create mode 100644 perl/x509.txt create mode 100644 perl/x509.xs create mode 100644 perl/xstmp.c create mode 100644 perl/y.pl create mode 100644 perl/yy.pl create mode 100644 perl/z.pl create mode 100644 perl/zz.pl (limited to 'perl') diff --git a/perl/MANIFEST b/perl/MANIFEST new file mode 100644 index 0000000000..992db5ed4c --- /dev/null +++ b/perl/MANIFEST @@ -0,0 +1,17 @@ +MANIFEST +Makefile.PL +SSLeay.pm +bio.xs +bn.xs +cipher.xs +digest.xs +err.xs +ssl.xs +x509.xs +test.pl +test2.pl +test3.pl +test9.pl +testbn.pl +testmd.pl +typemap diff --git a/perl/Makefile.PL b/perl/Makefile.PL new file mode 100644 index 0000000000..f9998e0a61 --- /dev/null +++ b/perl/Makefile.PL @@ -0,0 +1,25 @@ +use ExtUtils::MakeMaker; +# See lib/ExtUtils/MakeMaker.pm for details of how to influence +# the contents of the Makefile that is written. +WriteMakefile( + 'OPTIMIZE' => '-g', + 'DISTNAME' => 'SSLeay-perl5-0.8.5', + 'NAME' => 'SSLeay', + 'VERSION_FROM' => 'SSLeay.pm', + 'LIBS' => ['-L.. -lssl -lcrypto'], + 'DEFINE' => '', + 'INC' => '-I../include', + 'C' => ['callback.c'], + 'H' => ['p5SSLeay.h'], + 'OBJECT' => " SSLeay.o bio.o bn.o cipher.o digest.o err.o + ssl.o x509.o", + 'XS' => { 'SSLeay.xs' => 'SSLeay.c', + 'bio.xs' => 'bio.c', + 'bn.xs' => 'bn.c', + 'cipher.xs' => 'cipher.c', + 'digest.xs' => 'digest.c', + 'err.xs' => 'err.c', + 'ssl.xs' => 'ssl.c', + 'x509.xs' => 'x509.c', + } + ); diff --git a/perl/OpenSSL.xs b/perl/OpenSSL.xs new file mode 100644 index 0000000000..582b5705aa --- /dev/null +++ b/perl/OpenSSL.xs @@ -0,0 +1,63 @@ +#include "p5SSLeay.h" + +SV *new_ref(type,obj,mort) +char *type; +char *obj; + { + SV *ret; + + if (mort) + ret=sv_newmortal(); + else + ret=newSViv(0); + sv_setref_pv(ret,type,(void *)obj); + return(ret); + } + +int ex_new(obj,data,ad,idx,argl,argp) +char *obj; +SV *data; +CRYPTO_EX_DATA *ad; +int idx; +long argl; +char *argp; + { + SV *sv; + +fprintf(stderr,"ex_new %08X %s\n",obj,argp); + sv=sv_newmortal(); + sv_setref_pv(sv,argp,(void *)obj); + CRYPTO_set_ex_data(ad,idx,(char *)sv); + return(1); + } + +void ex_cleanup(obj,data,ad,idx,argl,argp) +char *obj; +SV *data; +CRYPTO_EX_DATA *ad; +int idx; +long argl; +char *argp; + { + pr_name("ex_cleanup"); +fprintf(stderr,"ex_cleanup %08X %s\n",obj,argp); + if (data != NULL) + SvREFCNT_dec((SV *)data); + } + +MODULE = SSLeay PACKAGE = SSLeay + +BOOT: + boot_bio(); + boot_cipher(); + boot_digest(); + boot_err(); + boot_ssl(); + boot_SSLeay__BN(); + boot_SSLeay__BIO(); + boot_SSLeay__Cipher(); + boot_SSLeay__MD(); + boot_SSLeay__ERR(); + boot_SSLeay__SSL(); + boot_SSLeay__X509(); + diff --git a/perl/SSLeay.pm b/perl/SSLeay.pm new file mode 100644 index 0000000000..f7710039d2 --- /dev/null +++ b/perl/SSLeay.pm @@ -0,0 +1,78 @@ +package SSLeay; + +use Exporter; +use DynaLoader; + +@ISA = qw(Exporter DynaLoader); +@EXPORT = qw(); + +$VERSION='0.82'; +$VERSION='0.82'; +bootstrap SSLeay; + +@SSLeay::BN::ISA= qw(SSLeay::ERR); +@SSLeay::MD::ISA= qw(SSLeay::ERR); +@SSLeay::Cipher::ISA= qw(SSLeay::ERR); +@SSLeay::SSL::CTX::ISA= qw(SSLeay::ERR); +@SSLeay::BIO::ISA= qw(SSLeay::ERR); +@SSLeay::SSL::ISA= qw(SSLeay::ERR); + +@BN::ISA= qw(SSLeay::BN); +@MD::ISA= qw(SSLeay::MD); +@Cipher::ISA= qw(SSLeay::Cipher); +@SSL::ISA= qw(SSLeay::SSL); +@SSL::CTX::ISA= qw(SSLeay::SSL::CTX); +@BIO::ISA= qw(SSLeay::BIO); + + +@SSLeay::MD::names=qw(md2 md5 sha sha1 ripemd160 mdc2); + +@SSLeay::Cipher::names=qw( + des-ecb des-cfb des-ofb des-cbc + des-ede des-ede-cfb des-ede-ofb des-ede-cbc + des-ede3 des-ede3-cfb des-ede3-ofb des-ede3-cbc + desx-cbc rc4 rc4-40 + idea-ecb idea-cfb idea-ofb idea-cbc + rc2-ecb rc2-cbc rc2-40-cbc rc2-cfb rc2-ofb + bf-ecb bf-cfb bf-ofb bf-cbc + cast5-ecb cast5-cfb cast5-ofb cast5-cbc + rc5-ecb rc5-cfb rc5-ofb rc5-cbc + ); + +sub SSLeay::SSL::CTX::new_ssl { SSLeay::SSL::new($_[0]); } + +sub SSLeay::ERR::error + { + my($o)=@_; + my($s,$ret); + + while (($s=$o->get_error()) != 0) + { + $ret.=$s."\n"; + } + return($ret); + } + +@SSLeay::Cipher::aliases=qw(des desx des3 idea rc2 bf cast); + +package SSLeay::BN; + +sub bnfix { (ref($_[0]) ne "SSLeay::BN")?SSLeay::BN::dec2bn($_[0]):$_[0]; } +use overload +"=" => sub { dup($_[0]); }, +"+" => sub { add($_[0],$_[1]); }, +"-" => sub { ($_[1],$_[0])=($_[0],$_[1]) if $_[2]; + SSLeay::BN::sub($_[0],$_[1]); }, +"*" => sub { mul($_[0],$_[1]); }, +"/" => sub { ($_[1],$_[0])=($_[0],$_[1]) if $_[2]; (div($_[0],$_[1]))[0]; }, +"%" => sub { ($_[1],$_[0])=($_[0],$_[1]) if $_[2]; mod($_[0],$_[1]); }, +"**" => sub { ($_[1],$_[0])=($_[0],$_[1]) if $_[2]; exp($_[0],$_[1]); }, +"<<" => sub { lshift($_[0],$_[1]); }, +">>" => sub { rshift($_[0],$_[1]); }, +"<=>" => sub { SSLeay::BN::cmp($_[0],$_[1]); }, +'""' => sub { bn2dec($_[0]); }, +'0+' => sub { dec2bn($_[0]); }, +"bool" => sub { ref($_[0]) eq "SSLeay::BN"; }; + +sub SSLeay::BIO::do_accept { SSLeay::BIO::do_handshake(@_); } +1; diff --git a/perl/SSLeay.xs b/perl/SSLeay.xs new file mode 100644 index 0000000000..582b5705aa --- /dev/null +++ b/perl/SSLeay.xs @@ -0,0 +1,63 @@ +#include "p5SSLeay.h" + +SV *new_ref(type,obj,mort) +char *type; +char *obj; + { + SV *ret; + + if (mort) + ret=sv_newmortal(); + else + ret=newSViv(0); + sv_setref_pv(ret,type,(void *)obj); + return(ret); + } + +int ex_new(obj,data,ad,idx,argl,argp) +char *obj; +SV *data; +CRYPTO_EX_DATA *ad; +int idx; +long argl; +char *argp; + { + SV *sv; + +fprintf(stderr,"ex_new %08X %s\n",obj,argp); + sv=sv_newmortal(); + sv_setref_pv(sv,argp,(void *)obj); + CRYPTO_set_ex_data(ad,idx,(char *)sv); + return(1); + } + +void ex_cleanup(obj,data,ad,idx,argl,argp) +char *obj; +SV *data; +CRYPTO_EX_DATA *ad; +int idx; +long argl; +char *argp; + { + pr_name("ex_cleanup"); +fprintf(stderr,"ex_cleanup %08X %s\n",obj,argp); + if (data != NULL) + SvREFCNT_dec((SV *)data); + } + +MODULE = SSLeay PACKAGE = SSLeay + +BOOT: + boot_bio(); + boot_cipher(); + boot_digest(); + boot_err(); + boot_ssl(); + boot_SSLeay__BN(); + boot_SSLeay__BIO(); + boot_SSLeay__Cipher(); + boot_SSLeay__MD(); + boot_SSLeay__ERR(); + boot_SSLeay__SSL(); + boot_SSLeay__X509(); + diff --git a/perl/b.pl b/perl/b.pl new file mode 100644 index 0000000000..ac1e52de79 --- /dev/null +++ b/perl/b.pl @@ -0,0 +1,21 @@ +#!/usr/local/bin/perl + +use ExtUtils::testlib; + +use SSLeay; + +$cmd=<<"EOF"; + +EOF + +$conn="localhost:4433"; +$conn=$ARGV[0] if $#ARGV >= 0; +print "X\n"; +$bio=BIO->new("connect"); +print "XX\n"; +$bio->set_callback(sub {print STDERR $_[0]->number_read."\n"; $_[$#_] }); +print "XXX\n"; +$bio->hostname($conn) || die $ssl->error(); +print "XXXX\n"; + +#$ssl=BIO->new("ssl"); diff --git a/perl/bio.pl b/perl/bio.pl new file mode 100644 index 0000000000..be27581c02 --- /dev/null +++ b/perl/bio.pl @@ -0,0 +1,28 @@ +#!/usr/local/bin/perl + +use ExtUtils::testlib; + +use SSLeay; + +$cmd=<<"EOF"; + +EOF + +$conn="localhost:4433"; +$conn=$ARGV[0] if $#ARGV >= 0; +$bio=SSLeay::BIO::new("connect"); +$bio->set_callback(sub {print STDERR $_[0]->number_read."\n"; $_[$#_] }); +$bio->hostname($conn) || die $ssl->error(); + + +(($ret=$bio->do_handshake()) > 0) || die $bio->error(); + +(($ret=$bio->syswrite($cmd)) > 0) || die $bio->error(); + +while (1) + { + $ret=$bio->sysread($buf,10240); + last if ($ret <= 0); + print $buf; + } + diff --git a/perl/bio.txt b/perl/bio.txt new file mode 100644 index 0000000000..5b46c9f5ee --- /dev/null +++ b/perl/bio.txt @@ -0,0 +1,36 @@ +BIO::new(type) + "connect" + "accept" + "ssl" + "buffer" + +"connect" + BIO::hostname(name) host:port to connect to + +"accept" + BIO::set_accept_port(port) port to connect too. + +"connect", "accept", "ssl" + BIO::do_andshake do protocol + +"ssl" + BIO::set_ssl(ssl) + BIO::get_ssl() + +BIO::push(bio) +BIO::pop; return BIO +BIO::number_read() +BIO::number_written() +BIO::references() + +BIO::sysread(buf,len[,offset]) +BIO::syswrite(in[,len][,offset]) +BIO::getline() +BIO::puts(in) +BIO::flush() +BIO::type() +BIO::next_bio(); +BIO::set_callback(callback[, args]) + +BIO::new_buffer_ssl_connect(SSL_CTX) +BIO::new_ssl_connect(SSL_CTX) diff --git a/perl/bio.xs b/perl/bio.xs new file mode 100644 index 0000000000..3782d42062 --- /dev/null +++ b/perl/bio.xs @@ -0,0 +1,448 @@ +#include "p5SSLeay.h" + +static int p5_bio_ex_bio_ptr=0; +static int p5_bio_ex_bio_callback=0; +static int p5_bio_ex_bio_callback_data=0; + +static long p5_bio_callback(bio,state,parg,cmd,larg,ret) +BIO *bio; +int state; +char *parg; +int cmd; +long larg; +int ret; + { + int i; + SV *me,*cb; + + me=(SV *)BIO_get_ex_data(bio,p5_bio_ex_bio_ptr); + cb=(SV *)BIO_get_ex_data(bio,p5_bio_ex_bio_callback); + if (cb != NULL) + { + dSP; + + ENTER ; + SAVETMPS; + + PUSHMARK(sp); + XPUSHs(me); + XPUSHs(sv_2mortal(newSViv(state))); + XPUSHs(sv_2mortal(newSViv(cmd))); + if ((state == BIO_CB_READ) || (state == BIO_CB_WRITE)) + { + XPUSHs(sv_2mortal(newSVpv(parg,larg))); + } + else + XPUSHs(&sv_undef); + /* ptr one */ + XPUSHs(sv_2mortal(newSViv(larg))); + XPUSHs(sv_2mortal(newSViv(ret))); + PUTBACK; + + i=perl_call_sv(cb,G_SCALAR); + + SPAGAIN; + if (i == 1) + ret=POPi; + else + ret=1; + PUTBACK; + FREETMPS; + LEAVE; + } + else + { + croak("Internal error in SSL p5_ssl_info_callback"); + } + return(ret); + } + +int boot_bio() + { + p5_bio_ex_bio_ptr= + BIO_get_ex_new_index(0,"SSLeay::BIO",ex_new,NULL,ex_cleanup); + p5_bio_ex_bio_callback= + BIO_get_ex_new_index(0,"bio_callback",NULL,NULL, + ex_cleanup); + p5_bio_ex_bio_callback_data= + BIO_get_ex_new_index(0,"bio_callback_data",NULL,NULL, + ex_cleanup); + return(1); + } + +MODULE = SSLeay::BIO PACKAGE = SSLeay::BIO PREFIX = p5_BIO_ + +VERSIONCHECK: DISABLE + +void +p5_BIO_new_buffer_ssl_connect(...) + PREINIT: + SSL_CTX *ctx; + BIO *bio; + SV *arg; + PPCODE: + if (items == 1) + arg=ST(0); + else if (items == 2) + arg=ST(1); + else + arg=NULL; + + if ((arg == NULL) || !(sv_derived_from(arg,"SSLeay::SSL::CTX"))) + croak("Usage: SSLeay::BIO::new_buffer_ssl_connect(SSL_CTX)"); + else + { + IV tmp=SvIV((SV *)SvRV(arg)); + ctx=(SSL_CTX *)tmp; + } + EXTEND(sp,1); + bio=BIO_new_buffer_ssl_connect(ctx); + arg=new_ref("SSLeay::BIO",(char *)bio,0); + PUSHs(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,(char *)arg); + +void +p5_BIO_new_ssl_connect(...) + PREINIT: + SSL_CTX *ctx; + BIO *bio; + SV *arg; + PPCODE: + if (items == 1) + arg=ST(0); + else if (items == 2) + arg=ST(1); + else + arg=NULL; + + if ((arg == NULL) || !(sv_derived_from(arg,"SSLeay::SSL::CTX"))) + croak("Usage: SSLeay::BIO::new_ssl_connect(SSL_CTX)"); + else + { + IV tmp=SvIV((SV *)SvRV(arg)); + ctx=(SSL_CTX *)tmp; + } + EXTEND(sp,1); + bio=BIO_new_ssl_connect(ctx); + arg=new_ref("SSLeay::BIO",(char *)bio,0); + PUSHs(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,(char *)arg); + +void +p5_BIO_new(...) + PREINIT: + BIO *bio; + char *type; + SV *arg; + PPCODE: + pr_name("p5_BIO_new"); + if ((items == 1) && SvPOK(ST(0))) + type=SvPV(ST(0),na); + else if ((items == 2) && SvPOK(ST(1))) + type=SvPV(ST(1),na); + else + croak("Usage: SSLeay::BIO::new(type)"); + + EXTEND(sp,1); + if (strcmp(type,"connect") == 0) + bio=BIO_new(BIO_s_connect()); + else if (strcmp(type,"accept") == 0) + bio=BIO_new(BIO_s_accept()); + else if (strcmp(type,"ssl") == 0) + bio=BIO_new(BIO_f_ssl()); + else if (strcmp(type,"buffer") == 0) + bio=BIO_new(BIO_f_buffer()); + else + croak("unknown BIO type"); + arg=new_ref("SSLeay::BIO",(char *)bio,0); + PUSHs(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,(char *)arg); + +int +p5_BIO_hostname(bio,name) + BIO *bio; + char *name; + CODE: + RETVAL=BIO_set_hostname(bio,name); + OUTPUT: + RETVAL + +int +p5_BIO_set_accept_port(bio,str) + BIO *bio; + char *str; + CODE: + RETVAL=BIO_set_accept_port(bio,str); + OUTPUT: + RETVAL + +int +p5_BIO_do_handshake(bio) + BIO *bio; + CODE: + RETVAL=BIO_do_handshake(bio); + OUTPUT: + RETVAL + +BIO * +p5_BIO_push(b,bio) + BIO *b; + BIO *bio; + CODE: + /* This reference will be reduced when the reference is + * let go, and then when the BIO_free_all() is called + * inside the SSLeay library by the BIO with this + * pushed into */ + bio->references++; + RETVAL=BIO_push(b,bio); + OUTPUT: + RETVAL + +void +p5_BIO_pop(b) + BIO *b + PREINIT: + BIO *bio; + char *type; + SV *arg; + PPCODE: + bio=BIO_pop(b); + if (bio != NULL) + { + /* This BIO will either be one created in the + * perl library, in which case it will have a perl + * SV, otherwise it will have been created internally, + * inside SSLeay. For the 'pushed in', it needs + * the reference count decememted. */ + arg=(SV *)BIO_get_ex_data(bio,p5_bio_ex_bio_ptr); + if (arg == NULL) + { + arg=new_ref("SSLeay::BIO",(char *)bio,0); + PUSHs(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,(char *)arg); + } + else + { + /* it was pushed in */ + SvREFCNT_inc(arg); + PUSHs(arg); + if (bio->references < 1) + abort(); + /* decrement the reference count */ + BIO_free(bio); + } + } + +int +p5_BIO_sysread(bio,in,num, ...) + BIO *bio; + SV *in; + int num; + PREINIT: + int i,n,olen; + int offset; + char *p; + CODE: + offset=0; + if (!SvPOK(in)) + sv_setpvn(in,"",0); + SvPV(in,olen); + if (items > 3) + { + offset=SvIV(ST(3)); + if (offset < 0) + { + if (-offset > olen) + croad("Offset outside string"); + offset+=olen; + } + } + if ((num+offset) > olen) + { + SvGROW(in,num+offset+1); + p=SvPV(in,i); + memset(&(p[olen]),0,(num+offset)-olen+1); + } + p=SvPV(in,n); + + i=BIO_read(bio,p+offset,num); + RETVAL=i; + if (i <= 0) i=0; + SvCUR_set(in,offset+i); + OUTPUT: + RETVAL + +int +p5_BIO_syswrite(bio,in, ...) + BIO *bio; + SV *in; + PREINIT: + char *ptr; + int len,in_len; + int offset=0; + int n; + CODE: + ptr=SvPV(in,in_len); + if (items > 2) + { + len=SvOK(ST(2))?SvIV(ST(2)):in_len; + if (items > 3) + { + offset=SvIV(ST(3)); + if (offset < 0) + { + if (-offset > in_len) + croak("Offset outside string"); + offset+=in_len; + } + else if ((offset >= in_len) && (in_len > 0)) + croak("Offset outside string"); + } + if (len >= (in_len-offset)) + len=in_len-offset; + } + else + len=in_len; + + RETVAL=BIO_write(bio,ptr+offset,len); + OUTPUT: + RETVAL + +void +p5_BIO_getline(bio) + BIO *bio; + PREINIT: + int i; + char *p; + PPCODE: + pr_name("p5_BIO_gets"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + sv_setpvn(ST(0),"",0); + SvGROW(ST(0),1024); + p=SvPV(ST(0),na); + i=BIO_gets(bio,p,1024); + if (i < 0) i=0; + SvCUR_set(ST(0),i); + +int +p5_BIO_flush(bio) + BIO *bio; + CODE: + RETVAL=BIO_flush(bio); + OUTPUT: + RETVAL + +char * +p5_BIO_type(bio) + BIO *bio; + CODE: + RETVAL=bio->method->name; + OUTPUT: + RETVAL + +void +p5_BIO_next_bio(b) + BIO *b + PREINIT: + BIO *bio; + char *type; + SV *arg; + PPCODE: + bio=b->next_bio; + if (bio != NULL) + { + arg=(SV *)BIO_get_ex_data(bio,p5_bio_ex_bio_ptr); + if (arg == NULL) + { + arg=new_ref("SSLeay::BIO",(char *)bio,0); + PUSHs(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,(char *)arg); + } + else + { + SvREFCNT_inc(arg); + PUSHs(arg); + } + } + +int +p5_BIO_puts(bio,in) + BIO *bio; + SV *in; + PREINIT: + char *ptr; + CODE: + ptr=SvPV(in,na); + RETVAL=BIO_puts(bio,ptr); + OUTPUT: + RETVAL + +void +p5_BIO_set_callback(bio,cb,...) + BIO *bio; + SV *cb; + PREINIT: + SV *arg=NULL; + SV *arg2=NULL; + CODE: + if (items > 3) + croak("Usage: SSLeay::BIO::set_callback(bio,callback[,arg]"); + if (items == 3) + { + arg2=sv_mortalcopy(ST(2)); + SvREFCNT_inc(arg2); + BIO_set_ex_data(bio,p5_bio_ex_bio_callback_data, + (char *)arg2); + } + arg=sv_mortalcopy(ST(1)); + SvREFCNT_inc(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_callback,(char *)arg); + BIO_set_callback(bio,p5_bio_callback); + +void +p5_BIO_DESTROY(bio) + BIO *bio + PREINIT: + SV *sv; + PPCODE: + pr_name_d("p5_BIO_DESTROY",bio->references); + printf("p5_BIO_DESTROY <%s> %d\n",bio->method->name,bio->references); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,NULL); + BIO_free_all(bio); + +int +p5_BIO_set_ssl(bio,ssl) + BIO *bio; + SSL *ssl; + CODE: + pr_name("p5_BIO_set_ssl"); + ssl->references++; + RETVAL=BIO_set_ssl(bio,ssl,BIO_CLOSE); + OUTPUT: + RETVAL + +int +p5_BIO_number_read(bio) + BIO *bio; + CODE: + RETVAL=BIO_number_read(bio); + OUTPUT: + RETVAL + +int +p5_BIO_number_written(bio) + BIO *bio; + CODE: + RETVAL=BIO_number_written(bio); + OUTPUT: + RETVAL + +int +p5_BIO_references(bio) + BIO *bio; + CODE: + RETVAL=bio->references; + OUTPUT: + RETVAL + diff --git a/perl/bn.pl b/perl/bn.pl new file mode 100644 index 0000000000..388e19c6de --- /dev/null +++ b/perl/bn.pl @@ -0,0 +1,23 @@ +#!/usr/local/bin/perl +use ExtUtils::testlib; +use SSLeay; + +$message=SSLeay::BN::new(); +$e=SSLeay::BN::new(); +$mod=SSLeay::BN::new(); + +$mod=SSLeay::BN::dec2bn("114381625757888867669235779976146612010218296721242362562561842935706935245733897830597123563958705058989075147599290026879543541"); +$e=5; +$d=SSLeay::BN::dec2bn("45752650303155547067694311990458644804087318688496945025024737159778909096647814932594914301288138204957467016445183857236173773"); + +$message=SSLeay::BN::bin2bn("The magic words are squeamish ossifrage"); + + + $cipher_text= $message->mod_exp($e,$mod); +print $mod."\n"; +print $mod->num_bits()."\n"; +for (1 .. 1000) + { + $clear= $cipher_text->mod_exp($d,$mod); + } +print $clear->bn2bin()."\n"; diff --git a/perl/bn.txt b/perl/bn.txt new file mode 100644 index 0000000000..784e761558 --- /dev/null +++ b/perl/bn.txt @@ -0,0 +1,38 @@ +BN->new() +BN->dup(a) +BN->rand(bits[,top][,bottom]) + +BN->hex2bn(a) +BN->bn2hex(a) +BN->dec2bn(a) +BN->bn2dec(a) +BN->bin2bn(a) +BN->bn2bin(a) +BN->mpi2bn(a) +BN->bn2mpi(a) + +BN->add(a,b) +BN->sub(a,b) +BN->mul(a,b) +(div,mod)=BN->div(a,b) +BN->mod(a,b) +BN->exp(a,p) +BN->mod_mul(a,b,c) +BN->mod_exp(a,b,c) + +BN->is_prime(p,num) +BN->generate_prime(bits,strong,callback) + +BN->num_bits(a) +BN->cmp(a,b) +BN->ucmp(a,b) +BN->is_bit_set(a,pos) +BN->set_bit(a,pos) +BN->clear_bit(a,pos) +BN->lshift(a,num) +BN->rshift(a,num) +BN->mask_bits(a,pos) +BN->clear(a) +BN->gcd(a,b) +BN->mod_inverse(a,mod) + diff --git a/perl/bn.xs b/perl/bn.xs new file mode 100644 index 0000000000..c15be3729a --- /dev/null +++ b/perl/bn.xs @@ -0,0 +1,589 @@ +#include "p5SSLeay.h" + +int sv_to_BIGNUM(var,arg,name) +BIGNUM **var; +SV *arg; +char *name; + { + int ret=1; + + if (sv_derived_from(arg,"SSLeay::BN")) + { + IV tmp = SvIV((SV*)SvRV(arg)); + *var = (BIGNUM *) tmp; + } + else if (SvIOK(arg)) { + SV *tmp=sv_newmortal(); + *var=BN_new(); + BN_set_word(*var,SvIV(arg)); + sv_setref_pv(tmp,"SSLeay::BN",(void*)*var); + } + else if (SvPOK(arg)) { + char *ptr; + STRLEN len; + SV *tmp=sv_newmortal(); + *var=BN_new(); + sv_setref_pv(tmp,"SSLeay::BN", (void*)*var); + ptr=SvPV(arg,len); + SvGROW(arg,len+1); + ptr[len]='\0'; + BN_dec2bn(var,ptr); + } + else + { + croak(name); + ret=0; + } + return(ret); + } + +typedef struct gpc_args_st { + SV *cb; + SV *arg; + } GPC_ARGS; + +static void generate_prime_callback(pos,num,arg) +int pos; +int num; +char *arg; + { + dSP ; + int i; + GPC_ARGS *a=(GPC_ARGS *)arg; + + ENTER ; + SAVETMPS ; + + PUSHMARK(sp); + XPUSHs(sv_2mortal(newSViv(pos))); + XPUSHs(sv_2mortal(newSViv(num))); + XPUSHs(sv_2mortal(newSVsv(a->arg))); + PUTBACK; + + i=perl_call_sv(a->cb,G_DISCARD); + + SPAGAIN; + + PUTBACK; + FREETMPS; + LEAVE; + } + +MODULE = SSLeay::BN PACKAGE = SSLeay::BN PREFIX = p5_BN_ + +VERSIONCHECK: DISABLE + +void +p5_BN_new(...) + PREINIT: + BIGNUM *bn; + SV *arg; + PPCODE: + pr_name("p5_BN_new"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + bn=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)bn); + +void +p5_BN_dup(a) + BIGNUM *a; + PREINIT: + BIGNUM *bn; + PPCODE: + pr_name("p5_BN_dup"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + bn=BN_dup(a); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)bn); + +void +p5_BN_rand(bits,...) + int bits; + PREINIT: + int top=1; + int bottom=0; + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_rand"); + if ((items < 1) || (items > 3)) + croak("Usage: SSLeay::BN::rand(bits[,top_bit][,bottombit]"); + if (items >= 2) top=(int)SvIV(ST(0)); + if (items >= 3) bottom=(int)SvIV(ST(1)); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + BN_rand(ret,bits,top,bottom); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + +void +p5_BN_bin2bn(a) + datum a; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_bin2bn"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_bin2bn(a.dptr,a.dsize,NULL); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + +void +p5_BN_bn2bin(a) + BIGNUM *a; + PREINIT: + int i; + PPCODE: + pr_name("p5_BN_bn2bin"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + i=BN_num_bytes(a)+2; + sv_setpvn(ST(0),"",1); + SvGROW(ST(0),i+1); + SvCUR_set(ST(0),BN_bn2bin(a,SvPV(ST(0),na))); + +void +p5_BN_mpi2bn(a) + datum a; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_mpi2bn"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_mpi2bn(a.dptr,a.dsize,NULL); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + +void +p5_BN_bn2mpi(a) + BIGNUM *a; + PREINIT: + int i; + PPCODE: + pr_name("p5_BN_bn2mpi"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + i=BN_bn2mpi(a,NULL); + sv_setpvn(ST(0),"",1); + SvGROW(ST(0),i+1); + SvCUR_set(ST(0),BN_bn2mpi(a,SvPV(ST(0),na))); + +void +p5_BN_hex2bn(a) + datum a; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_hex2bn"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_hex2bn(&ret,a.dptr); + +void +p5_BN_dec2bn(a) + datum a; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_dec2bn"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_dec2bn(&ret,a.dptr); + +SV * +p5_BN_bn2hex(a) + BIGNUM *a; + PREINIT: + char *ptr; + int i; + CODE: + pr_name("p5_BN_bn2hex"); + ptr=BN_bn2hex(a); + RETVAL=newSVpv("",0); + i=strlen(ptr); + SvGROW(RETVAL,i+1); + memcpy(SvPV(RETVAL,na),ptr,i+1); + SvCUR_set(RETVAL,i); + Free(ptr); + OUTPUT: + RETVAL + +SV * +p5_BN_bn2dec(a) + BIGNUM *a; + PREINIT: + char *ptr; + int i; + CODE: + pr_name("p5_BN_bn2dec"); + ptr=BN_bn2dec(a); + RETVAL=newSVpv("",0); + i=strlen(ptr); + SvGROW(RETVAL,i+1); + memcpy(SvPV(RETVAL,na),ptr,i+1); + SvCUR_set(RETVAL,i); + Free(ptr); + OUTPUT: + RETVAL + +void +p5_BN_add(a,b) + BIGNUM *a; + BIGNUM *b; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_add"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_add(ret,a,b); + +void +p5_BN_sub(a,b) + BIGNUM *a; + BIGNUM *b; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_sub"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_sub(ret,a,b); + +void +p5_BN_mul(a,b) + BIGNUM *a; + BIGNUM *b; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_mul"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_mul(ret,a,b); + +void +p5_BN_div(a,b) + BIGNUM *a; + BIGNUM *b; + PREINIT: + static BN_CTX *ctx=NULL; + BIGNUM *div,*mod; + PPCODE: + pr_name("p5_BN_div"); + if (ctx == NULL) ctx=BN_CTX_new(); + EXTEND(sp,2); + PUSHs(sv_newmortal()); + PUSHs(sv_newmortal()); + div=BN_new(); + mod=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)div); + sv_setref_pv(ST(1), "SSLeay::BN", (void*)mod); + BN_div(div,mod,a,b,ctx); + +void +p5_BN_mod(a,b) + BIGNUM *a; + BIGNUM *b; + PREINIT: + static BN_CTX *ctx=NULL; + BIGNUM *rem; + PPCODE: + pr_name("p5_BN_mod"); + if (ctx == NULL) ctx=BN_CTX_new(); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + rem=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)rem); + BN_mod(rem,a,b,ctx); + +void +p5_BN_exp(a,p) + BIGNUM *a; + BIGNUM *p; + PREINIT: + BIGNUM *ret; + static BN_CTX *ctx=NULL; + PPCODE: + pr_name("p5_BN_exp"); + if (ctx == NULL) ctx=BN_CTX_new(); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_exp(ret,a,p,ctx); + +void +p5_BN_mod_mul(a,b,c) + BIGNUM *a; + BIGNUM *b; + BIGNUM *c; + PREINIT: + static BN_CTX *ctx=NULL; + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_mod_mul"); + if (ctx == NULL) ctx=BN_CTX_new(); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_mod_mul(ret,a,b,c,ctx); + +void +p5_BN_mod_exp(a,b,c) + BIGNUM *a; + BIGNUM *b; + BIGNUM *c; + PREINIT: + static BN_CTX *ctx=NULL; + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_mod_exp"); + if (ctx == NULL) ctx=BN_CTX_new(); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_mod_exp(ret,a,b,c,ctx); + +void +p5_BN_generate_prime(...) + PREINIT: + int bits=512; + int strong=0; + BIGNUM *ret=NULL; + SV *callback=NULL; + SV *cb_arg=NULL; + GPC_ARGS arg; + dSP; + + PPCODE: + pr_name("p5_BN_generate_prime"); + if ((items < 0) || (items > 4)) + croak("Usage: SSLeay::BN::generate_prime(a[,strong][,callback][,cb_arg]"); + if (items >= 1) bits=(int)SvIV(ST(0)); + if (items >= 2) strong=(int)SvIV(ST(1)); + if (items >= 3) callback=ST(2); + if (items == 4) cb_arg=ST(3); + + if (callback == NULL) + ret=BN_generate_prime(bits,strong,NULL,NULL,NULL,NULL); + else + { + arg.cb=callback; + arg.arg=cb_arg; + + ret=BN_generate_prime(bits,strong,NULL,NULL, + generate_prime_callback,(char *)&arg); + } + + SPAGAIN; + sp-=items; /* a bit evil that I do this */ + + EXTEND(sp,1); + PUSHs(sv_newmortal()); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + +void +p5_BN_is_prime(p,...) + BIGNUM *p; + PREINIT: + int nchecks=5,ret; + SV *callback=NULL; + SV *cb_arg=NULL; + GPC_ARGS arg; + dSP; + static BN_CTX *ctx=NULL; + PPCODE: + pr_name("p5_BN_is_prime"); + if ((items < 1) || (items > 4)) + croak("Usage: SSLeay::BN::is_prime(a[,ncheck][,callback][,callback_arg]"); + if (ctx == NULL) ctx=BN_CTX_new(); + if (items >= 2) nchecks=(int)SvIV(ST(1)); + if (items >= 3) callback=ST(2); + if (items >= 4) cb_arg=ST(3); + arg.arg=cb_arg; + if (callback == NULL) + ret=BN_is_prime(p,nchecks,NULL,ctx,NULL); + else + { + arg.cb=callback; + arg.arg=cb_arg; + ret=BN_is_prime(p,nchecks,generate_prime_callback, + ctx,(char *)&arg); + } + SPAGAIN; + sp-=items; /* a bit evil */ + PUSHs(sv_2mortal(newSViv(ret))); + +int +p5_BN_num_bits(a) + BIGNUM *a; + CODE: + pr_name("p5_BN_num_bits"); + RETVAL=BN_num_bits(a); + OUTPUT: + RETVAL + +int +p5_BN_cmp(a,b) + BIGNUM *a; + BIGNUM *b; + CODE: + pr_name("p5_BN_cmp"); + RETVAL=BN_cmp(a,b); + OUTPUT: + RETVAL + +int +p5_BN_ucmp(a,b) + BIGNUM *a; + BIGNUM *b; + CODE: + pr_name("p5_BN_ucmp"); + RETVAL=BN_ucmp(a,b); + OUTPUT: + RETVAL + +int +p5_BN_is_bit_set(a,b) + BIGNUM *a; + int b; + CODE: + pr_name("p5_BN_is_bit_set"); + RETVAL=BN_is_bit_set(a,b); + OUTPUT: + RETVAL + +void +p5_BN_set_bit(a,b) + BIGNUM *a; + int b; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_set_bit"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_dup(a); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_set_bit(ret,b); + +void +p5_BN_clear_bit(a,b) + BIGNUM *a; + int b; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_clear_bit"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_dup(a); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_clear_bit(ret,b); + +void +p5_BN_lshift(a,b) + BIGNUM *a; + int b; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_lshift"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + if (b == 1) + BN_lshift1(ret,a); + else + BN_lshift(ret,a,b); + +void +p5_BN_rshift(a,b) + BIGNUM *a; + int b; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_rshift"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + if (b == 1) + BN_rshift1(ret,a); + else + BN_rshift(ret,a,b); + +void +p5_BN_mask_bits(a,b) + BIGNUM *a; + int b; + PREINIT: + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_mask_bits"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_dup(a); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_mask_bits(ret,b); + +void +p5_BN_clear(a) + BIGNUM *a; + PPCODE: + pr_name("p5_BN_clear"); + BN_clear(a); + +void +p5_BN_gcd(a,b) + BIGNUM *a; + BIGNUM *b; + PREINIT: + static BN_CTX *ctx=NULL; + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_gcd"); + if (ctx == NULL) ctx=BN_CTX_new(); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + ret=BN_new(); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + BN_gcd(ret,a,b,ctx); + +void +p5_BN_mod_inverse(a,mod) + BIGNUM *a; + BIGNUM *mod; + PREINIT: + static BN_CTX *ctx=NULL; + BIGNUM *ret; + PPCODE: + pr_name("p5_BN_mod_inverse"); + if (ctx == NULL) ctx=BN_CTX_new(); + ret=BN_mod_inverse(a,mod,ctx); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + sv_setref_pv(ST(0), "SSLeay::BN", (void*)ret); + +void +p5_BN_DESTROY(bn) + BIGNUM *bn + CODE: + pr_name("p5_BN_DESTROY"); + BN_free(bn); + diff --git a/perl/callback.c b/perl/callback.c new file mode 100644 index 0000000000..01840abc85 --- /dev/null +++ b/perl/callback.c @@ -0,0 +1,103 @@ +/* perl/callback.c */ +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ + +SV *new_ref(type,obj,mort) +char *type; +char *obj; + { + SV *ret; + + if (mort) + ret=sv_newmortal(); + else + ret=newSViv(0); + sv_setref_pv(ret,type,(void *)obj); + return(ret); + } + +int ex_new(obj,data,ad,idx,argl,argp) +char *obj; +SV *data; +CRYPTO_EX_DATA *ad; +int idx; +long argl; +char *argp; + { + SV *sv; + +fprintf(stderr,"ex_new %08X %s\n",obj,argp); + sv=sv_newmortal(); + sv_setref_pv(sv,argp,(void *)obj); + CRYPTO_set_ex_data(ad,idx,(char *)sv); + return(1); + } + +void ex_cleanup(obj,data,ad,idx,argl,argp) +char *obj; +SV *data; +CRYPTO_EX_DATA *ad; +int idx; +long argl; +char *argp; + { + pr_name("ex_cleanup"); +fprintf(stderr,"ex_cleanup %08X %s\n",obj,argp); + if (data != NULL) + SvREFCNT_dec((SV *)data); + } + diff --git a/perl/cipher.pl b/perl/cipher.pl new file mode 100644 index 0000000000..efc712fdf5 --- /dev/null +++ b/perl/cipher.pl @@ -0,0 +1,39 @@ +#!/usr/local/bin/perl + +use ExtUtils::testlib; + +use SSLeay; + +$md=SSLeay::MD::new("md5"); + +foreach (@SSLeay::Cipher::names) + { + ($c=SSLeay::Cipher::new($_)) || + die "'$_' is an unknown cipher algorithm\n"; + + + $data="012345678abcdefghijklmnopqrstuvwxyz"; + $c->init("01234567abcdefghABCDEFGH","zyxwvut",1); + + $in =$c->update(substr($data, 0, 5)); + $in.=$c->update(substr($data, 5,10)); + $in.=$c->update(substr($data,15,1)); + $in.=$c->update(substr($data,16)); + + $in.=$c->final(); + + $c->init("01234567abcdefghABCDEFGH","zyxwvut",0); + $out=$c->update($in); + $out.=$c->final(); + + ($out eq $data) || die "decrypt for $_ failed:$!\n"; + + $md->init(); + $md->update($in); + $digest=$md->final(); + + print unpack("H*",$digest); + printf " %2d %2d %2d %s\n", $c->key_length(), $c->iv_length(), + $c->block_size(), $c->name(); + } + diff --git a/perl/cipher.txt b/perl/cipher.txt new file mode 100644 index 0000000000..c47952b5c9 --- /dev/null +++ b/perl/cipher.txt @@ -0,0 +1,10 @@ +Cipher::new(name) "des-cbc" etc +Cipher::name() +Cipher::key_length() +Cipher::iv_length() +Cipher::block_size() + +Cipher::init(key,iv,enc) +Cipher::update(in) +Cipher::final() +Cipher::cipher(in) diff --git a/perl/cipher.xs b/perl/cipher.xs new file mode 100644 index 0000000000..1044d7a4ef --- /dev/null +++ b/perl/cipher.xs @@ -0,0 +1,152 @@ +#include "p5SSLeay.h" + +int boot_cipher() + { + SSLeay_add_all_ciphers(); + return(1); + } + +MODULE = SSLeay::Cipher PACKAGE = SSLeay::Cipher PREFIX = p5_EVP_C_ + +VERSIONCHECK: DISABLE + +void +p5_EVP_C_new(...) + PREINIT: + EVP_CIPHER_CTX *ctx; + EVP_CIPHER *c; + char *name; + PPCODE: + if ((items == 1) && SvPOK(ST(0))) + name=SvPV(ST(0),na); + else if ((items == 2) && SvPOK(ST(1))) + name=SvPV(ST(1),na); + else + croak("Usage: SSLeay::Cipher::new(type)"); + PUSHs(sv_newmortal()); + c=EVP_get_cipherbyname(name); + if (c != NULL) + { + ctx=malloc(sizeof(EVP_CIPHER_CTX)); + EVP_EncryptInit(ctx,c,NULL,NULL); + sv_setref_pv(ST(0), "SSLeay::Cipher", (void*)ctx); + } + +datum +p5_EVP_C_name(ctx) + EVP_CIPHER_CTX *ctx + CODE: + RETVAL.dptr=OBJ_nid2ln(EVP_CIPHER_CTX_nid(ctx)); + RETVAL.dsize=strlen(RETVAL.dptr); + OUTPUT: + RETVAL + +int +p5_EVP_C_key_length(ctx) + EVP_CIPHER_CTX *ctx + CODE: + RETVAL=EVP_CIPHER_CTX_key_length(ctx); + OUTPUT: + RETVAL + +int +p5_EVP_C_iv_length(ctx) + EVP_CIPHER_CTX *ctx + CODE: + RETVAL=EVP_CIPHER_CTX_iv_length(ctx); + OUTPUT: + RETVAL + +int +p5_EVP_C_block_size(ctx) + EVP_CIPHER_CTX *ctx + CODE: + RETVAL=EVP_CIPHER_CTX_block_size(ctx); + OUTPUT: + RETVAL + +void +p5_EVP_C_init(ctx,key,iv,enc) + EVP_CIPHER_CTX *ctx + datum key + datum iv + int enc + PREINIT: + char loc_iv[EVP_MAX_IV_LENGTH]; + char loc_key[EVP_MAX_KEY_LENGTH]; + char *ip=loc_iv,*kp=loc_key; + int i; + memset(loc_iv,0,EVP_MAX_IV_LENGTH); + memset(loc_key,0,EVP_MAX_KEY_LENGTH); + CODE: + i=key.dsize; + if (key.dsize > EVP_CIPHER_CTX_key_length(ctx)) + i=EVP_CIPHER_CTX_key_length(ctx); + if (i > 0) + { + memset(kp,0,EVP_MAX_KEY_LENGTH); + memcpy(kp,key.dptr,i); + } + else + kp=NULL; + i=iv.dsize; + if (iv.dsize > EVP_CIPHER_CTX_iv_length(ctx)) + i=EVP_CIPHER_CTX_iv_length(ctx); + if (i > 0) + { + memcpy(ip,iv.dptr,i); + memset(ip,0,EVP_MAX_IV_LENGTH); + } + else + ip=NULL; + EVP_CipherInit(ctx,EVP_CIPHER_CTX_cipher(ctx),kp,ip,enc); + memset(loc_key,0,sizeof(loc_key)); + memset(loc_iv,0,sizeof(loc_iv)); + +SV * +p5_EVP_C_cipher(ctx,in) + EVP_CIPHER_CTX *ctx; + datum in; + CODE: + RETVAL=newSVpv("",0); + SvGROW(RETVAL,in.dsize+EVP_CIPHER_CTX_block_size(ctx)+1); + EVP_Cipher(ctx,SvPV(RETVAL,na),in.dptr,in.dsize); + SvCUR_set(RETVAL,in.dsize); + OUTPUT: + RETVAL + +SV * +p5_EVP_C_update(ctx, in) + EVP_CIPHER_CTX *ctx + datum in + PREINIT: + int i; + CODE: + RETVAL=newSVpv("",0); + SvGROW(RETVAL,in.dsize+EVP_CIPHER_CTX_block_size(ctx)+1); + EVP_CipherUpdate(ctx,SvPV(RETVAL,na),&i,in.dptr,in.dsize); + SvCUR_set(RETVAL,i); + OUTPUT: + RETVAL + +SV * +p5_EVP_C_final(ctx) + EVP_CIPHER_CTX *ctx + PREINIT: + int i; + CODE: + RETVAL=newSVpv("",0); + SvGROW(RETVAL,EVP_CIPHER_CTX_block_size(ctx)+1); + if (!EVP_CipherFinal(ctx,SvPV(RETVAL,na),&i)) + sv_setpv(RETVAL,"BAD DECODE"); + else + SvCUR_set(RETVAL,i); + OUTPUT: + RETVAL + +void +p5_EVP_C_DESTROY(ctx) + EVP_CIPHER_CTX *ctx + CODE: + free((char *)ctx); + diff --git a/perl/dh.pl b/perl/dh.pl new file mode 100644 index 0000000000..61d2debe73 --- /dev/null +++ b/perl/dh.pl @@ -0,0 +1,40 @@ +#!/usr/local/bin/perl + +use ExtUtils::testlib; + +use SSLeay; + +$g=SSLeay::BN::hex2bn("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002"); +$p=SSLeay::BN::hex2bn("ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff"); +$pub=SSLeay::BN::hex2bn("521b5b72d0a23f5f908eff62741b9c43ac65c47ad264a4f8d62d73dfab4938a6e019f81c28d64efd9b47c1b8188566c6184b6064cc42fac2778bd732678148c6cc7601bfe0ed18da76dd7bb976cd2ff0afc7b20f3f81171e2ea6534de061f929"); +$priv=SSLeay::BN::hex2bn("6e15c752af3f4cf6d7425164c451eeba760ec0651d12dc3b0ee5002a95af6191268ca47c0fbb3d836136eee795ae4af3a1adad5e04d0dbb04378cae0406ece23ca3b86839c0fd60064c1019c7d18be4dc0ec4be6c1e9ff6b0f5bd76373585503"); +$his=SSLeay::BN::hex2bn("d0fb51cd44a8578f55eb0822ede90f07504f4720d7367ff4bf76c27fedbce79d9204421ff7e86bd1dd02031bce4ceccd1d3e7c62679b6eb5fda8238fd4fe07bff573d552795f0d46f25753c688300fb9ed396792b59a49fdf89c8429124b668e"); +$sh=SSLeay::BN::hex2bn("66ec34b09bddf86147f6c6efd5ee4e6691e690eb0e90aceda16a742cad0abe531cb61d057aff362001ca19013215140ca2a1dd8966c78105bacbf2161f9cfbd58d351ff87923de77f9c56851037223d48272565416ee769e65a621cefb90b403"); + +$g2=SSLeay::BN::hex2bn("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002"); +$p2=SSLeay::BN::hex2bn("ffffffffffffffffc90fdaa22168c234c4c6628b80dc1cd129024e088a67cc74020bbea63b139b22514a08798e3404ddef9519b3cd3a431b302b0a6df25f14374fe1356d6d51c245e485b576625e7ec6f44c42e9a63a3620ffffffffffffffff"); +$pub2=SSLeay::BN::hex2bn("d0fb51cd44a8578f55eb0822ede90f07504f4720d7367ff4bf76c27fedbce79d9204421ff7e86bd1dd02031bce4ceccd1d3e7c62679b6eb5fda8238fd4fe07bff573d552795f0d46f25753c688300fb9ed396792b59a49fdf89c8429124b668e"); +$priv2=SSLeay::BN::hex2bn("b81d54f52b687669fc8bd8087ac319accc2f94a2feafe09779f4a81e8e01f77290f5bbe84a08003afc4448145be427fad0b9d047889cf361c9dd378b15c1ebd5bda33e051fbd9eba8bf063e2bd836467cddb61f1db5c4b06bea5c9a77fb87b24"); +$his2=SSLeay::BN::hex2bn("521b5b72d0a23f5f908eff62741b9c43ac65c47ad264a4f8d62d73dfab4938a6e019f81c28d64efd9b47c1b8188566c6184b6064cc42fac2778bd732678148c6cc7601bfe0ed18da76dd7bb976cd2ff0afc7b20f3f81171e2ea6534de061f929"); +$sh2=SSLeay::BN::hex2bn("791faba7a6b592cb68a963945229483dc30f80f5cb295b2b5a59ef618a262d22de0845948a34db83d8bde260b940967ff85593a609e53ee6510aea09b776b4704d5e916917f384458d4790b6e0befcb1cb2f112b850e9ed410a091db80e1db2e"); + +print "g=".$g->bn2hex."\n"; +print "p=".$p->bn2hex."\n"; +print "pub=".$pub->bn2hex."\n"; +print "priv=".$priv->bn2hex."\n"; +print "sh=".$sh->bn2hex."\n"; + +print "new p - p2 = ".($p-$p2)."\n"; + +$tmp=$g->mod_exp($priv,$p); +print "XXXXXXXXXXXXXXXX\n"; +print "new pub - pub = ".($tmp-$pub)."\n"; +$tmp2=$g2->mod_exp($priv2,$p2); +print "XXXXXXXXXXXXXXXX\n"; + +print $p." pub\n"; +print $tmp2." calc pub\n"; +print $pub2." txt pub\n"; + + + diff --git a/perl/digest.txt b/perl/digest.txt new file mode 100644 index 0000000000..6cb3ffedb0 --- /dev/null +++ b/perl/digest.txt @@ -0,0 +1,7 @@ +MD::new(name) "md2", "md5" etc +MD::name() +MD::init() +MD::update(in) +MD::final() + + diff --git a/perl/digest.xs b/perl/digest.xs new file mode 100644 index 0000000000..5738b09e48 --- /dev/null +++ b/perl/digest.xs @@ -0,0 +1,83 @@ +#include "p5SSLeay.h" + +int boot_digest() + { + SSLeay_add_all_digests(); + return(1); + } + +MODULE = SSLeay::MD PACKAGE = SSLeay::MD PREFIX = p5_EVP_MD_ + +PROTOTYPES: ENABLE +VERSIONCHECK: DISABLE + +# SSLeay::MD::new(name) name= md2, md5, sha, sha1, or mdc2 +# md->name() - returns the name +# md->init() - reinitalises the digest +# md->update(data) - adds more data to digest +# digest=md->final() - returns digest +# + +void +p5_EVP_MD_new(...) + PREINIT: + EVP_MD_CTX *ctx; + EVP_MD *md; + char *name; + PPCODE: + if ((items == 1) && SvPOK(ST(0))) + name=SvPV(ST(0),na); + else if ((items == 2) && SvPOK(ST(1))) + name=SvPV(ST(1),na); + else + croak("Usage: SSLeay::MD::new(type)"); + PUSHs(sv_newmortal()); + md=EVP_get_digestbyname(name); + if (md != NULL) + { + ctx=malloc(sizeof(EVP_MD_CTX)); + EVP_DigestInit(ctx,md); + sv_setref_pv(ST(0), "SSLeay::MD", (void*)ctx); + } + +datum +p5_EVP_MD_name(ctx) + EVP_MD_CTX *ctx + CODE: + RETVAL.dptr=OBJ_nid2ln(EVP_MD_type(EVP_MD_CTX_type(ctx))); + RETVAL.dsize=strlen(RETVAL.dptr); + OUTPUT: + RETVAL + +void +p5_EVP_MD_init(ctx) + EVP_MD_CTX *ctx + CODE: + EVP_DigestInit(ctx,EVP_MD_CTX_type(ctx)); + +void +p5_EVP_MD_update(ctx, in) + EVP_MD_CTX *ctx + datum in + CODE: + EVP_DigestUpdate(ctx,in.dptr,in.dsize); + +datum +p5_EVP_MD_final(ctx) + EVP_MD_CTX *ctx + PREINIT: + char md[EVP_MAX_MD_SIZE]; + int len; + CODE: + EVP_DigestFinal(ctx,md,&len); + RETVAL.dptr=md; + RETVAL.dsize=len; + OUTPUT: + RETVAL + +void +p5_EVP_MD_DESTROY(ctx) + EVP_MD_CTX *ctx + CODE: + free((char *)ctx); + diff --git a/perl/err.txt b/perl/err.txt new file mode 100644 index 0000000000..5e6cdaecdc --- /dev/null +++ b/perl/err.txt @@ -0,0 +1,2 @@ +ERR::get_error() +ERR::peek_error() diff --git a/perl/err.xs b/perl/err.xs new file mode 100644 index 0000000000..6d1aec3ea1 --- /dev/null +++ b/perl/err.xs @@ -0,0 +1,46 @@ +#include "p5SSLeay.h" + +int boot_err() + { + SSL_load_error_strings(); + return(1); + } + +MODULE = SSLeay::ERR PACKAGE = SSLeay::ERR PREFIX = p5_ERR_ + +PROTOTYPES: ENABLE +VERSIONCHECK: DISABLE + +# md->error() - returns the last error in text or numeric context + +void +p5_ERR_get_error(...) + PPCODE: + char buf[512]; + unsigned long l; + + pr_name("p5_ERR_get_code"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + l=ERR_get_error(); + ERR_error_string(l,buf); + sv_setiv(ST(0),l); + sv_setpv(ST(0),buf); + SvIOK_on(ST(0)); + +void +p5_ERR_peek_error(...) + PPCODE: + char buf[512]; + unsigned long l; + + pr_name("p5_ERR_get_code"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + l=ERR_peek_error(); + ERR_error_string(l,buf); + sv_setiv(ST(0),l); + sv_setpv(ST(0),buf); + SvIOK_on(ST(0)); + + diff --git a/perl/f.pl b/perl/f.pl new file mode 100644 index 0000000000..a255931864 --- /dev/null +++ b/perl/f.pl @@ -0,0 +1,25 @@ +#!/usr/bin/perl + +use ExtUtils::testlib; + +use SSLeay; + +$data=<>; + +#$b=SSLeay::BN::hex2bn($a); +#$data=$b->bn2bin; + +#substr($data,0,8)=""; +#print $data; + +$md=SSLeay::MD::new("md5"); +$md->init(); +$md->update("test"); +$key=$md->final(); + +$rc4=SSLeay::Cipher::new("rc4"); +$rc4->init($key,"",1); +$out=$rc4->cipher($data); + +print $out; + diff --git a/perl/g.pl b/perl/g.pl new file mode 100644 index 0000000000..80b1a422f8 --- /dev/null +++ b/perl/g.pl @@ -0,0 +1,18 @@ +#!/usr/local/bin/perl +use ExtUtils::testlib; +use SSLeay; + +$num=SSLeay::BN::new(); +$shift=SSLeay::BN::new(); + +print "0\n"; +$num=SSLeay::BN::hex2bn("1234329378209857309429670349760347603497603496398"); +print "1\n"; +$s=SSLeay::BN::hex2bn("59"); +print "a\n"; +$r=$num->lshift(59); +print "b"; + +print $num->bn2hex."\n"; +print $s->bn2hex."\n"; +print $r->bn2hex."\n"; diff --git a/perl/gen_rsa.pl b/perl/gen_rsa.pl new file mode 100644 index 0000000000..6acf043c2a --- /dev/null +++ b/perl/gen_rsa.pl @@ -0,0 +1,49 @@ +#!/usr/local/bin/perl + +use ExtUtils::testlib; + +use SSLeay; + +$bits=512; +$bits=$ARGV[0] if $#ARGV >= 0; + +$p=SSLeay::BN::generate_prime($bits/2,0,sub {print STDERR $_[0]?"+":"."}); +print "\n"; +$q=SSLeay::BN::generate_prime($bits/2,0,sub {print STDERR $_[0]?"+":"."}); +print "\n"; + +$e=SSLeay::BN::hex2bn("10001"); + +$t1=$p-1; +$t2=$q-1; + +($t1->gcd($e) == 1) || die "p failed the gcd test\n"; +($t2->gcd($e) == 1) || die "q failed the gcd test\n"; + +($q,$p)=($p,$q) if ($p < $q); +$n=$p*$q; +$t=($p-1)*($q-1); +($t->gcd($e) == 1) || die "t failed the gcd test\n"; + +$d=$e->mod_inverse($t); + +$dmp1=$d%($p-1); +$dmq1=$d%($q-1); +$iqmp=$q->mod_inverse($p); + +print "n =$n\n"; +print "e =$e\n"; +print "d =$d\n"; +print "dmp1=$dmp1\n"; +print "dmq1=$dmq1\n"; +print "iqmp=$iqmp\n"; + +$a=SSLeay::BN::bin2bn("This is an RSA test"); +print "Test with\n'".$a->bn2bin."' or\n$a\n"; + +$t1=$a->mod_exp($e,$n); +print "$t1\n"; +$t2=$t1->mod_exp($d,$n); +print "'".$t2->bn2bin."'\n"; + + diff --git a/perl/mul.pl b/perl/mul.pl new file mode 100644 index 0000000000..611a760625 --- /dev/null +++ b/perl/mul.pl @@ -0,0 +1,56 @@ +#!/usr/bin/perl + +use ExtUtils::testlib; + +use SSLeay; + + +sub mul + { + my($ab,$cd,$num)=@_; + + if ($num <= 4096) + { + return($ab*$cd); + } + else + { + my($a,$b,$c,$d,$n,$ac,$bd,$m,$t1,$t2); + + $n=$num/2; + + $a=$ab->mask_bits($n); + $b=$ab->rshift($n); + $c=$cd->mask_bits($n); + $d=$cd->rshift($n); + + $t1=($b-$a); + $t2=($c-$d); + $m= &mul($t1,$t2,$n); + $ac=&mul($a,$c,$n); + $bd=&mul($b,$d,$n); + $m=$m+$ac+$bd; + $m=$m->lshift($n); + $bd=$bd->lshift($num); + + $r=$ac+$m+$bd; + return($r); + } + } + +$num=4096*32; +$a=SSLeay::BN::rand($num); +$b=SSLeay::BN::rand($num); + +#for (1 .. 10) + { + $r=&mul($a,$b,$num); + } + +#for (1 .. 10) + { + $rr=$a*$b; + } + +$res=$rr-$r; +print $res->bn2hex()."\n"; diff --git a/perl/openssl.h b/perl/openssl.h new file mode 100644 index 0000000000..bcccda7d5f --- /dev/null +++ b/perl/openssl.h @@ -0,0 +1,96 @@ +/* perl/p5SSLeay.h */ +/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) + * All rights reserved. + * + * This package is an SSL implementation written + * by Eric Young (eay@cryptsoft.com). + * The implementation was written so as to conform with Netscapes SSL. + * + * This library is free for commercial and non-commercial use as long as + * the following conditions are aheared to. The following conditions + * apply to all code found in this distribution, be it the RC4, RSA, + * lhash, DES, etc., code; not just the SSL code. The SSL documentation + * included with this distribution is covered by the same copyright terms + * except that the holder is Tim Hudson (tjh@cryptsoft.com). + * + * Copyright remains Eric Young's, and as such any Copyright notices in + * the code are not to be removed. + * If this package is used in a product, Eric Young should be given attribution + * as the author of the parts of the library used. + * This can be in the form of a textual message at program startup or + * in documentation (online or textual) provided with the package. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. All advertising materials mentioning features or use of this software + * must display the following acknowledgement: + * "This product includes cryptographic software written by + * Eric Young (eay@cryptsoft.com)" + * The word 'cryptographic' can be left out if the rouines from the library + * being used are not cryptographic related :-). + * 4. If you include any Windows specific code (or a derivative thereof) from + * the apps directory (application code) you must include an acknowledgement: + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" + * + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. + * + * The licence and distribution terms for any publically available version or + * derivative of this code cannot be changed. i.e. this code cannot simply be + * copied and put under another distribution licence + * [including the GNU Public Licence.] + */ +#ifdef __cplusplus +extern "C" { +#endif +#include "EXTERN.h" +#include "perl.h" +#include "XSUB.h" +#ifdef __cplusplus +} +#endif + +typedef struct datum_st + { + char *dptr; + int dsize; + } datum; + +#include "crypto.h" +#include "buffer.h" +#include "bio.h" +#include "evp.h" +#include "err.h" +#include "x509.h" +#include "ssl.h" + +#if 0 +#define pr_name(name) printf("%s\n",name) +#define pr_name_d(name,p2) printf("%s %d\n",name,p2) +#define pr_name_dd(name,p2,p3) printf("%s %d %d\n",name,p2,p3) +#else +#define pr_name(name) +#define pr_name_d(name,p2) +#define pr_name_dd(name,p2,p3) +#endif + +SV *new_ref(char *type, char *obj, int mort); +int ex_new(char *obj,SV *data,CRYPTO_EX_DATA *ad,int idx,long argl,char *argp); +void ex_cleanup(char *obj,SV *data,CRYPTO_EX_DATA *ad,int idx, + long argl,char *argp); + diff --git a/perl/openssl_bio.xs b/perl/openssl_bio.xs new file mode 100644 index 0000000000..3782d42062 --- /dev/null +++ b/perl/openssl_bio.xs @@ -0,0 +1,448 @@ +#include "p5SSLeay.h" + +static int p5_bio_ex_bio_ptr=0; +static int p5_bio_ex_bio_callback=0; +static int p5_bio_ex_bio_callback_data=0; + +static long p5_bio_callback(bio,state,parg,cmd,larg,ret) +BIO *bio; +int state; +char *parg; +int cmd; +long larg; +int ret; + { + int i; + SV *me,*cb; + + me=(SV *)BIO_get_ex_data(bio,p5_bio_ex_bio_ptr); + cb=(SV *)BIO_get_ex_data(bio,p5_bio_ex_bio_callback); + if (cb != NULL) + { + dSP; + + ENTER ; + SAVETMPS; + + PUSHMARK(sp); + XPUSHs(me); + XPUSHs(sv_2mortal(newSViv(state))); + XPUSHs(sv_2mortal(newSViv(cmd))); + if ((state == BIO_CB_READ) || (state == BIO_CB_WRITE)) + { + XPUSHs(sv_2mortal(newSVpv(parg,larg))); + } + else + XPUSHs(&sv_undef); + /* ptr one */ + XPUSHs(sv_2mortal(newSViv(larg))); + XPUSHs(sv_2mortal(newSViv(ret))); + PUTBACK; + + i=perl_call_sv(cb,G_SCALAR); + + SPAGAIN; + if (i == 1) + ret=POPi; + else + ret=1; + PUTBACK; + FREETMPS; + LEAVE; + } + else + { + croak("Internal error in SSL p5_ssl_info_callback"); + } + return(ret); + } + +int boot_bio() + { + p5_bio_ex_bio_ptr= + BIO_get_ex_new_index(0,"SSLeay::BIO",ex_new,NULL,ex_cleanup); + p5_bio_ex_bio_callback= + BIO_get_ex_new_index(0,"bio_callback",NULL,NULL, + ex_cleanup); + p5_bio_ex_bio_callback_data= + BIO_get_ex_new_index(0,"bio_callback_data",NULL,NULL, + ex_cleanup); + return(1); + } + +MODULE = SSLeay::BIO PACKAGE = SSLeay::BIO PREFIX = p5_BIO_ + +VERSIONCHECK: DISABLE + +void +p5_BIO_new_buffer_ssl_connect(...) + PREINIT: + SSL_CTX *ctx; + BIO *bio; + SV *arg; + PPCODE: + if (items == 1) + arg=ST(0); + else if (items == 2) + arg=ST(1); + else + arg=NULL; + + if ((arg == NULL) || !(sv_derived_from(arg,"SSLeay::SSL::CTX"))) + croak("Usage: SSLeay::BIO::new_buffer_ssl_connect(SSL_CTX)"); + else + { + IV tmp=SvIV((SV *)SvRV(arg)); + ctx=(SSL_CTX *)tmp; + } + EXTEND(sp,1); + bio=BIO_new_buffer_ssl_connect(ctx); + arg=new_ref("SSLeay::BIO",(char *)bio,0); + PUSHs(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,(char *)arg); + +void +p5_BIO_new_ssl_connect(...) + PREINIT: + SSL_CTX *ctx; + BIO *bio; + SV *arg; + PPCODE: + if (items == 1) + arg=ST(0); + else if (items == 2) + arg=ST(1); + else + arg=NULL; + + if ((arg == NULL) || !(sv_derived_from(arg,"SSLeay::SSL::CTX"))) + croak("Usage: SSLeay::BIO::new_ssl_connect(SSL_CTX)"); + else + { + IV tmp=SvIV((SV *)SvRV(arg)); + ctx=(SSL_CTX *)tmp; + } + EXTEND(sp,1); + bio=BIO_new_ssl_connect(ctx); + arg=new_ref("SSLeay::BIO",(char *)bio,0); + PUSHs(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,(char *)arg); + +void +p5_BIO_new(...) + PREINIT: + BIO *bio; + char *type; + SV *arg; + PPCODE: + pr_name("p5_BIO_new"); + if ((items == 1) && SvPOK(ST(0))) + type=SvPV(ST(0),na); + else if ((items == 2) && SvPOK(ST(1))) + type=SvPV(ST(1),na); + else + croak("Usage: SSLeay::BIO::new(type)"); + + EXTEND(sp,1); + if (strcmp(type,"connect") == 0) + bio=BIO_new(BIO_s_connect()); + else if (strcmp(type,"accept") == 0) + bio=BIO_new(BIO_s_accept()); + else if (strcmp(type,"ssl") == 0) + bio=BIO_new(BIO_f_ssl()); + else if (strcmp(type,"buffer") == 0) + bio=BIO_new(BIO_f_buffer()); + else + croak("unknown BIO type"); + arg=new_ref("SSLeay::BIO",(char *)bio,0); + PUSHs(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,(char *)arg); + +int +p5_BIO_hostname(bio,name) + BIO *bio; + char *name; + CODE: + RETVAL=BIO_set_hostname(bio,name); + OUTPUT: + RETVAL + +int +p5_BIO_set_accept_port(bio,str) + BIO *bio; + char *str; + CODE: + RETVAL=BIO_set_accept_port(bio,str); + OUTPUT: + RETVAL + +int +p5_BIO_do_handshake(bio) + BIO *bio; + CODE: + RETVAL=BIO_do_handshake(bio); + OUTPUT: + RETVAL + +BIO * +p5_BIO_push(b,bio) + BIO *b; + BIO *bio; + CODE: + /* This reference will be reduced when the reference is + * let go, and then when the BIO_free_all() is called + * inside the SSLeay library by the BIO with this + * pushed into */ + bio->references++; + RETVAL=BIO_push(b,bio); + OUTPUT: + RETVAL + +void +p5_BIO_pop(b) + BIO *b + PREINIT: + BIO *bio; + char *type; + SV *arg; + PPCODE: + bio=BIO_pop(b); + if (bio != NULL) + { + /* This BIO will either be one created in the + * perl library, in which case it will have a perl + * SV, otherwise it will have been created internally, + * inside SSLeay. For the 'pushed in', it needs + * the reference count decememted. */ + arg=(SV *)BIO_get_ex_data(bio,p5_bio_ex_bio_ptr); + if (arg == NULL) + { + arg=new_ref("SSLeay::BIO",(char *)bio,0); + PUSHs(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,(char *)arg); + } + else + { + /* it was pushed in */ + SvREFCNT_inc(arg); + PUSHs(arg); + if (bio->references < 1) + abort(); + /* decrement the reference count */ + BIO_free(bio); + } + } + +int +p5_BIO_sysread(bio,in,num, ...) + BIO *bio; + SV *in; + int num; + PREINIT: + int i,n,olen; + int offset; + char *p; + CODE: + offset=0; + if (!SvPOK(in)) + sv_setpvn(in,"",0); + SvPV(in,olen); + if (items > 3) + { + offset=SvIV(ST(3)); + if (offset < 0) + { + if (-offset > olen) + croad("Offset outside string"); + offset+=olen; + } + } + if ((num+offset) > olen) + { + SvGROW(in,num+offset+1); + p=SvPV(in,i); + memset(&(p[olen]),0,(num+offset)-olen+1); + } + p=SvPV(in,n); + + i=BIO_read(bio,p+offset,num); + RETVAL=i; + if (i <= 0) i=0; + SvCUR_set(in,offset+i); + OUTPUT: + RETVAL + +int +p5_BIO_syswrite(bio,in, ...) + BIO *bio; + SV *in; + PREINIT: + char *ptr; + int len,in_len; + int offset=0; + int n; + CODE: + ptr=SvPV(in,in_len); + if (items > 2) + { + len=SvOK(ST(2))?SvIV(ST(2)):in_len; + if (items > 3) + { + offset=SvIV(ST(3)); + if (offset < 0) + { + if (-offset > in_len) + croak("Offset outside string"); + offset+=in_len; + } + else if ((offset >= in_len) && (in_len > 0)) + croak("Offset outside string"); + } + if (len >= (in_len-offset)) + len=in_len-offset; + } + else + len=in_len; + + RETVAL=BIO_write(bio,ptr+offset,len); + OUTPUT: + RETVAL + +void +p5_BIO_getline(bio) + BIO *bio; + PREINIT: + int i; + char *p; + PPCODE: + pr_name("p5_BIO_gets"); + EXTEND(sp,1); + PUSHs(sv_newmortal()); + sv_setpvn(ST(0),"",0); + SvGROW(ST(0),1024); + p=SvPV(ST(0),na); + i=BIO_gets(bio,p,1024); + if (i < 0) i=0; + SvCUR_set(ST(0),i); + +int +p5_BIO_flush(bio) + BIO *bio; + CODE: + RETVAL=BIO_flush(bio); + OUTPUT: + RETVAL + +char * +p5_BIO_type(bio) + BIO *bio; + CODE: + RETVAL=bio->method->name; + OUTPUT: + RETVAL + +void +p5_BIO_next_bio(b) + BIO *b + PREINIT: + BIO *bio; + char *type; + SV *arg; + PPCODE: + bio=b->next_bio; + if (bio != NULL) + { + arg=(SV *)BIO_get_ex_data(bio,p5_bio_ex_bio_ptr); + if (arg == NULL) + { + arg=new_ref("SSLeay::BIO",(char *)bio,0); + PUSHs(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,(char *)arg); + } + else + { + SvREFCNT_inc(arg); + PUSHs(arg); + } + } + +int +p5_BIO_puts(bio,in) + BIO *bio; + SV *in; + PREINIT: + char *ptr; + CODE: + ptr=SvPV(in,na); + RETVAL=BIO_puts(bio,ptr); + OUTPUT: + RETVAL + +void +p5_BIO_set_callback(bio,cb,...) + BIO *bio; + SV *cb; + PREINIT: + SV *arg=NULL; + SV *arg2=NULL; + CODE: + if (items > 3) + croak("Usage: SSLeay::BIO::set_callback(bio,callback[,arg]"); + if (items == 3) + { + arg2=sv_mortalcopy(ST(2)); + SvREFCNT_inc(arg2); + BIO_set_ex_data(bio,p5_bio_ex_bio_callback_data, + (char *)arg2); + } + arg=sv_mortalcopy(ST(1)); + SvREFCNT_inc(arg); + BIO_set_ex_data(bio,p5_bio_ex_bio_callback,(char *)arg); + BIO_set_callback(bio,p5_bio_callback); + +void +p5_BIO_DESTROY(bio) + BIO *bio + PREINIT: + SV *sv; + PPCODE: + pr_name_d("p5_BIO_DESTROY",bio->references); + printf("p5_BIO_DESTROY <%s> %d\n",bio->method->name,bio->references); + BIO_set_ex_data(bio,p5_bio_ex_bio_ptr,NULL); + BIO_free_all(bio); + +int +p5_BIO_set_ssl(bio,ssl) + BIO *bio; + SSL *ssl; + CODE: + pr_name("p5_BIO_set_ssl"); + ssl->references++; + RETVAL=BIO_set_ssl(bio,ssl,BIO_CLOSE); + OUTPUT: + RETVAL + +int +p5_BIO_number_read(bio) + BIO *bio; + CODE: + RETVAL=BIO_number_read(bio); + OUTPUT: + RETVAL + +int +p5_BIO_number_written(bio) + BIO *bio; + CODE: + RETVAL=BIO_number_written(bio); + OUTPUT: + RETVAL + +int +p5_BIO_references(bio) + BIO *bio; + CODE: + RETVAL=bio->references; + OUTPUT: + RETVAL + diff --git a/perl/openssl_bn.xs b/perl/openssl_bn.xs new fi