Manual Pages for UNIX Darwin command on man Net::SSLeay
MyWebUniversity

Manual Pages for UNIX Darwin command on man Net::SSLeay

SSLeay(3) User Contributed Perl Documentation SSLeay(3)

NAME

Net::SSLeay - Perl extension for using OpenSSL

SYNOPSIS

use Net::SSLeay, qw(gethttps posthttps sslcat makeheaders makeform);

($page) = gethttps('www.bacus.pt', 443, '/'); # 1

($page, $response, %replyheaders)

= gethttps('www.bacus.pt', 443, '/', # 2

makeheaders(User-Agent => 'Cryptozilla/5.0b1',

Referer => 'https://www.bacus.pt' ));

($page, $result, %headers) = # 2b

= gethttps('www.bacus.pt', 443, '/protected.html', makeheaders(Authorization =>

'Basic ' . MIME::Base64::encode("$user:$pass",''))

);

($page, $response, %replyheaders)

= posthttps('www.bacus.pt', 443, '/foo.cgi', '', # 3

makeform(OK => '1', name => 'Sampo' ));

$reply = sslcat($host, $port, $request); # 4

($reply, $err, $servercert) = sslcat($host, $port, $request); # 5

$Net::SSLeay::trace = 2; # 0=no debugging, 1=ciphers, 2=trace, 3=dump data

DESCRIPTION

There is a related module called Net::SSLeay::Handle included in this

distribution that you might want to use instead. It has its own pod documentation. This module offers some high level convinience functions for accessing web pages on SSL servers (for symmetry, same API is offered for accessing http servers, too), a sslcat() function for writing your own clients, and finally access to the SSL api of SSLeay/OpenSSL package so you can write servers or clients for more complicated applications. For high level functions it is most convinient to import them to your main namespace as indicated in the synopsis. Case 1 demonstrates typical invocation of gethttps() to fetch an HTML page from secure server. The first argument provides host name or ip in dotted decimal notation of the remote server to contact. Second argument is the TCP port at the remote end (your own port is picked arbitrarily from high numbered ports as usual for TCP). The third argument is the URL of the page without the host name part. If in doubt consult HTTP specifications at Case 2 demonstrates full fledged use of gethttps(). As can be seen, gethttps() parses the response and response headers and returns them as a list, which can be captured in a hash for later reference. Also a fourth argument to gethttps() is used to insert some additional headers in the request. makeheaders() is a function that will convert a list or hash to such headers. By default gethttps() supplies Host (make virtual hosting easy) and Accept (reportedly needed by IIS) headers. Case 2b demonstrates how to get password protected page. Refer to HTTP protocol specifications for further details (e.g. RFC2617). Case 3 invokes posthttps() to submit a HTML/CGI form to secure server. First four arguments are equal to gethttps() (note that empty string ('') is passed as header argument). The fifth argument is the contents of the form formatted according to CGI specification. In this case the helper function makehttps() is used to do the formatting, but you

could pass any string. The posthttps() automatically adds Content-Type

and Content-Length headers to the request.

Case 4 shows the fundamental sslcat() function (inspired in spirit by

netcat utility :-). Its your swiss army knife that allows you to easily

contact servers, send some data, and then get the response. You are

responsible for formatting the data and parsing the response - sslcat()

is just a transport. Case 5 is a full invocation of sslcat() which allows return of errors as well as the server (peer) certificate.

The $trace global variable can be used to control the verbosity of high

level functions. Level 0 guarantees silence, level 1 (the default) only emits error messages. AAlltteerrnnaattee vveerrssiioonnss ooff tthhee AAPPII The above mentioned functions actually return the response headers as a list, which only gets converted to hash upon assignment (this assignment looses information if the same header occurs twice, as may be the case with cookies). There are also other variants of the functions that return unprocessed headers and that return a reference to a hash.

($page, $response, @headers) = gethttps('www.bacus.pt', 443, '/');

for ($i = 0; $i < $#headers; $i+=2) {

print "$headers[$i] = " . $headers[$i+1] . "\n";

}

($page, $response, $headers, $servercert)

= gethttps3('www.bacus.pt', 443, '/');

print "$headers\n";

($page, $response, %headersref, $servercert)

= gethttps4('www.bacus.pt', 443, '/');

for $k (sort keys %{headersref}) {

for $v (@{$headersref{$k}}) {

print "$k = $v\n";

} } All of the above code fragments accomplish the same thing: display all values of all headers. The API functions ending in "3" return the headers simply as a scalar string and it is up to the application to split them up. The functions ending in "4" return a reference to hash of arrays (see perlref and perllol manual pages if you are not familiar with complex perl data structures). To access single value of such header hash you would do something like

print $headersref{COOKIE}[0];

The variants 3 and 4 also allow you to discover the server certificate in case you would like to store or display it, e.g.

($p, $resp, $hdrs, $servercert) = gethttps3('www.bacus.pt', 443, '/');

if (!defined($servercert) || ($servercert == 0)) {

warn "Subject Name: undefined, Issuer Name: undefined"; } else { warn 'Subject Name: '

. Net::SSLeay::X509NAMEoneline(

Net::SSLeay::X509getsubjectname($servercert))

. 'Issuer Name: '

. Net::SSLeay::X509NAMEoneline(

Net::SSLeay::X509getissuername($servercert));

} Beware that this method only allows after the fact verification of the certificate: by the time gethttps3() has returned the https request has already been sent to the server, whether you decide to tryst it or not. To do the verification correctly you must either employ the OpenSSL certificate verification framework or use the lower level API to first connect and verify the certificate and only then send the http data. See implementation of dshttps3() for guidance on how to do this. UUssiinngg cclliieenntt cceerrttiiffiiccaatteess Secure web communications are encrypted using symmetric crypto keys exchanged using encryption based on the certificate of the server. Therefore in all SSL connections the server must have a certificate. This serves both to authenticate the server to the clients and to perform the key exchange. Sometimes it is necessary to authenticate the client as well. Two options are available: http basic authentication and client side certificate. The basic authentication over https is actually quite safe because https guarantees that the password will not travel in clear.

Never-the-less, problems like easily guessable passwords remain. The

client certificate method involves authentication of the client at SSL level using a certificate. For this to work, both the client and the server will have certificates (which typically are different) and private keys. The API functions outlined above accept additional arguments that allow one to supply the client side certificate and key files. The format of these files is the same as used for server certificates and the caveat about encrypting private key applies.

($page, $result, %headers) = # 2c

= gethttps('www.bacus.pt', 443, '/protected.html', makeheaders(Authorization =>

'Basic ' . MIME::Base64::encode("$user:$pass",'')),

'', $mimetype6, $pathtocrt7, $pathtokey8);

($page, $response, %replyheaders)

= posthttps('www.bacus.pt', 443, '/foo.cgi', # 3b

makeheaders('Authorization' =>

'Basic ' . MIME::Base64::encode("$user:$pass",'')),

makeform(OK => '1', name => 'Sampo'),

$mimetype6, $pathtocrt7, $pathtokey8);

Case 2c demonstrates getting password protected page that also requires client certificate, i.e. it is possible to use both authentication methods simultaneously. Case 3b is full blown post to secure server that requires both password authentication and client certificate, just like in case 2c. Note: Client will not send a certificate unless the server requests one. This is typically achieved by setting verify mode to VERIFYPEER on the server:

Net::SSLeay::setverify(ssl, Net::SSLeay::VERIFYPEER, 0);

See perldoc ~openssl/doc/ssl/SSLCTXsetverify.pod for full description. WWoorrkkiinngg tthhrroouugghh WWeebb pprrooxxyy

Net::SSLeay can use a web proxy to make its connections. You need to

first set the proxy host and port using setproxy() and then just use the normal API functions, e.g:

Net::SSLeay::setproxy('gateway.myorg.com', 8080);

($page) = gethttps('www.bacus.pt', 443, '/');

If your proxy requires authentication, you can supply username and password as well

Net::SSLeay::setproxy('gateway.myorg.com', 8080, 'joe', 'salainen');

($page, $result, %headers) =

= gethttps('www.bacus.pt', 443, '/protected.html', makeheaders(Authorization => 'Basic ' . MIME::Base64::encode("susie:pass",'')) ); This example demonstrates case where we authenticate to the proxy as "joe" and to the final web server as "susie". Proxy authentication requires MIME::Base64 module to work. CCeerrttiiffiiccaattee vveerriiffiiccaattiioonn aanndd CCeerrttiiffiiccaattee RReevvooooccaattiioonn LLiissttss ((CCRRLLss)) OpenSSL supports the ability to verify peer certificates. It can also optionally check the peer certificate against a Certificate Revocation List (CRL) from the certificates issuer. A CRL is a file, created by the certificate issuer that lists all the certificates that it previously signed, but which it now revokes. CRLs are in PEM format.

You can enable Net::SSLeay CRL checking like this:

&Net::SSLeay::X509STORECTXsetflags

(&Net::SSLeay::CTXgetcertstore($ssl),

&Net::SSLeay::X509VFLAGCRLCHECK);

After setting this flag, if OpenSSL checks a peer's certificate, then it will attempt to find a CRL for the issuer. It does this by looking for a specially named file in the search directory specified by CTXloadverifylocations. CRL files are named with the hash of the issuer's subject name, followed by .r0, .r1 etc. For example ab1331b2.r0, ab1331b2.r1. It will read all the .r files for the issuer, and then check for a revocation of the peer cerificate in all of them. (You can also force it to look in a specific named CRL file., see below). You can find out the hash of the issuer subject name in a CRL with

openssl crl -in crl.pem -hash -noout

If the peer certificate does not pass the revocation list, or if no CRL is found, then the handshaking fails with an error. You can also force OpenSSL to look for CRLs in one or more arbitrarily named files.

my $bio = &Net::SSLeay::BIOnewfile($crlfilename, 'r'); my $crl =

&Net::SSLeay::PEMreadbioX509CRL($bio); if ($crl) {

&Net::SSLeay::X509STOREaddcrl(&Net::SSLeay::CTXgetcertstore($ssl,

$crl); } else {

error reading CRL.... } CCoonnvveenniieennccee rroouuttiinneess To be used with Low level API

Net::SSLeay::randomize($rnseedfile,$additionalseed);

Net::SSLeay::setcertandkey($ctx, $certpath, $keypath);

$cert = Net::SSLeay::dumppeercertificate($ssl);

Net::SSLeay::sslwriteall($ssl, $message) or die "ssl write failure";

$got = Net::SSLeay::sslreadall($ssl) or die "ssl read failure";

$got = Net::SSLeay::sslreadCRLF($ssl [, $maxlength]);

$got = Net::SSLeay::sslreaduntil($ssl [, $delimit [, $maxlength]]);

Net::SSLeay::sslwriteCRLF($ssl, $message);

randomize() seeds the eay PRNG with /dev/urandom (see top of SSLeay.pm for how to change or configure this) and optionally with user provided data. It is very important to properly seed your random numbers, so do not forget to call this. The high level API functions automatically call randomize() so it is not needed with them. See also caveats. setcertandkey() takes two file names as arguments and sets the certificate and private key to those. This can be used to set either cerver certificates or client certificates. dumppeercertificate() allows you to get plaintext description of the certificate the peer (usually server) presented to us. sslreadall() and sslwriteall() provide true blocking semantics for these operations (see limitation, below, for explanation). These are much preferred to the low level API equivalents (which implement BSD blocking semantics). The message argument to sslwriteall() can be reference. This is helpful to avoid unnecessary copy when writing something big, e.g:

$data = 'A' x 1000000000;

Net::SSLeay::sslwriteall($ssl, \$data) or die "ssl write failed";

sslreadCRLF() uses sslreadall() to read in a line terminated with a carriage return followed by a linefeed (CRLF). The CRLF is included in the returned scalar. sslreaduntil() uses sslreadall() to read from the SSL input stream until it encounters a programmer specified delimiter. If the delimiter

is undefined, $/ is used. If $/ is undefined, \n is used. One can

optionally set a maximum length of bytes to read from the SSL input stream.

sslwriteCRLF() writes $message and appends CRLF to the SSL output

stream. LLooww lleevveell AAPPII In addition to the high level functions outlined above, this module contains straight forward access to SSL part of OpenSSL C api. Only the SSL subpart of OpenSSL is implemented (if anyone wants to implement other parts, feel free to submit patches). See ssl.h header from OpenSSL C distribution for list of low lever SSLeay functions to call (to check if some function has been implemented see directly in SSLeay.xs). The module strips SSLeay names

of the initial "SSL", generally you should use Net::SSLeay:: in place.

For example: In C:

#include

err = SSLsetverify (ssl, SSLVERIFYCLIENTONCE, &yourcallbackhere); In perl:

use Net::SSLeay;

$err = Net::SSLeay::setverify ($ssl,

&Net::SSLeay::VERIFYCLIENTONCE,

\&yourcallbackhere); If the function does not start by SSL you should use the full function name, e.g.:

$err = &Net::SSLeay::ERRgeterror;

Following new functions behave in perlish way:

$got = Net::SSLeay::read($ssl);

# Performs SSLread, but returns $got

# resized according to data received.

# Returns undef on failure.

Net::SSLeay::write($ssl, $foo) || die;

# Performs SSLwrite, but automatically

# figures out the size of $foo

In order to use the low level API you should start your programs with the following encantation:

use Net::SSLeay qw(dienow dieifsslerror);

Net::SSLeay::loaderrorstrings();

Net::SSLeay::SSLeayaddsslalgorithms(); # Important!

Net::SSLeay::randomize();

dienow() and dieifsslerror() are used to conveniently print SSLeay error stack when something goes wrong, thusly:

Net::SSLeay:connect($ssl) or dienow("Failed SSL connect ($!)");

Net::SSLeay::write($ssl, "foo") or dieifsslerror("SSL write ($!)");

You can also use Net::SSLeay::printerrs() to dump the error stack

without exiting the program. As can be seen, your code becomes much more readable if you import the error reporting functions to your main name space. I can not emphasize enough the need to check error returns. Use these functions even in most simple programs, they will reduce debugging time greatly. Do not ask questions in mailing list without having first sprinkled these in your code. SSoocckkeettss Perl uses file handles for all I/O. While SSLeay has quite flexible BIO mechanism and perl has evolved PerlIO mechanism, this module still sticks to using file descriptors. Thus to attach SSLeay to socket you should use fileno() to extract the underlying file descriptor:

Net::SSLeay::setfd($ssl, fileno(S)); # Must use fileno

You should also use "$|=1;" to eliminate STDIO buffering so you do not

get confused if you use perl I/O functions to manipulate your socket handle. If you need to select(2) on the socket, go right ahead, but be warned that OpenSSL does some internal buffering so SSLread does not always return data even if socket selected for reading (just keep on selecting

and trying to read). Net::SSLeay.pm is no different from the C language

OpenSSL in this respect. CCaallllbbaacckkss At this moment the implementation of verifycallback is crippeled in the sense that at any given time there can be only one call back which is shared by all SSL contexts, sessions and connections. This is due to having to keep the reference to the perl call back in a static variable so that the callback C glue can find it. To remove this restriction would require either a more complex data structure (like a hash?) in XSUB to map the call backs to their owners or, cleaner, adding a context pointer in the SSL structure. This context would then be passed to the C callback, which in our case would be the glue to look up the proper Perl function from the context and call it.

-- inaccurate -- The verify call back looks like this in C:

int (*callback)(int ok,X509 *subjcert,X509 *issuercert, int depth,int errorcode,char *arg,STACK *certchain) The corresponding Perl function should be something like this: sub verify {

my ($ok, $subjcert, $issuercert, $depth, $errorcode,

$arg, $chain) = @;

print "Verifying certificate...\n"; ...

return $ok;

} It is used like this:

Net::SSLeay::setverify ($ssl, Net::SSLeay::VERIFYPEER, \&verify);

Callbacks for decrypting private keys are implemented, but have the same limitation as the verifycallback implementation (one password callback shared between all contexts.) You might use it something like this:

Net::SSLeay::CTXsetdefaultpasswdcb($ctx, sub { "top-secret" });

Net::SSLeay::CTXusePrivateKeyfile($ctx, "key.pem",

Net::SSLeay::FILETYPEPEM)

or die "Error reading private key";

Net::SSLeay::CTXsetdefaultpasswdcb($ctx, undef);

No other callbacks are implemented. You do not need to use any callback

for simple (i.e. normal) cases where the SSLeay built-in verify

mechanism satisfies your needs. It is desirable to reset these callbacks to undef immediately after use to prevent thread safety problems and crashes on exit that can occur if different threads set different callbacks.

-- end inaccurate --

If you want to use callback stuff, see examples/callback.pl! Its the only one I am able to make work reliably. XX550099 aanndd RRAANNDD ssttuuffff This module largely lacks interface to the X509 and RAND routines, but as I was lazy and needed them, the following kludges are implemented:

$x509name = Net::SSLeay::X509getsubjectname($x509cert);

$x509name = Net::SSLeay::X509getissuername($x509cert);

print Net::SSLeay::X509NAMEoneline($x509name);

$text = Net::SSLeay::X509NAMEgettextbyNID($name, $nid);

Net::SSLeay::RANDseed($buf); # Perlishly figures out buf size

Net::SSLeay::RANDbytes($buf, $num);

Net::SSLeay::RANDpseudobytes($buf, $num);

Net::SSLeay::RANDadd($buf, $num, $entropy);

Net::SSLeay::RANDpoll();

Net::SSLeay::RANDstatus();

Net::SSLeay::RANDcleanup();

Net::SSLeay::RANDfilename($num);

Net::SSLeay::RANDloadfile($filename, $howmanybytes);

Net::SSLeay::RANDwritefile($filename);

Net::SSLeay::RANDegd($path);

Net::SSLeay::RANDegdbytes($path, $bytes);

Actually you should consider using the following helper functions:

print Net::SSLeay::dumppeercertificate($ssl);

Net::SSLeay::randomize();

RRSSAA iinntteerrffaaccee Some RSA functions are available:

$rsakey = Net::SSLeay::RSAgeneratekey();

Net::SSLeay::CTXsettmprsa($ctx, $rsakey);

Net::SSLeay::RSAfree($rsakey);

BBIIOO iinntteerrffaaccee Some BIO functions are available:

Net::SSLeay::BIOsmem();

$bio = Net::SSLeay::BIOnew(BIOsmem())

$bio = Net::SSLeay::BIOnewfile($filename, $mode);

Net::SSLeay::BIOfree($bio)

$count = Net::SSLeay::BIOwrite($data);

$data = Net::SSLeay::BIOread($bio);

$data = Net::SSLeay::BIOread($bio, $maxbytes);

$iseof = Net::SSLeay::BIOeof($bio);

$count = Net::SSLeay::BIOpending($bio);

$count = Net::SSLeay::BIOwpending ($bio);

LLooww lleevveell AAPPII Some very low level API functions are available:

$clientrandom = &Net::SSLeay::getclientrandom($ssl);

$serverrandom = &Net::SSLeay::getserverrandom($ssl);

$session = &Net::SSLeay::getsession($ssl);

$masterkey = &Net::SSLeay::SESSIONgetmasterkey($session);

HHTTTTPP ((wwiitthhoouutt SS)) AAPPII Over the years it has become clear that it would be convenient to use

the light weight flavour API of Net::SSLeay also for normal http (see

LWP for heavy weight object oriented approach). In fact it would be nice to be able to flip https on and off on the fly. Thus regular http support was evolved.

use Net::SSLeay, qw(gethttp posthttp tcpcat

gethttpx posthttpx tcpxcat makeheaders makeform);

($page, $result, %headers) =

= gethttp('www.bacus.pt', 443, '/protected.html', makeheaders(Authorization =>

'Basic ' . MIME::Base64::encode("$user:$pass",''))

);

($page, $response, %replyheaders)

= posthttp('www.bacus.pt', 443, '/foo.cgi', '', makeform(OK => '1', name => 'Sampo' ));

($reply, $err) = tcpcat($host, $port, $request);

($page, $result, %headers) =

= gethttpx($usessl, 'www.bacus.pt', 443, '/protected.html',

makeheaders(Authorization =>

'Basic ' . MIME::Base64::encode("$user:$pass",''))

);

($page, $response, %replyheaders)

= posthttpx($usessl, 'www.bacus.pt', 443, '/foo.cgi', '',

makeform(OK => '1', name => 'Sampo' ));

($reply, $err, $servercert) = tcpxcat($usessl, $host, $port, $request);

As can be seen, the "x" family of APIs takes as first argument a flag which indicated whether SSL is used or not. EEXXAAMMPPLLEESS One very good example is to look at the implementation of sslcat() in the SSLeay.pm file.

Following is a simple SSLeay client (with too little error checking :-(

#!/usr/local/bin/perl

use Socket;

use Net::SSLeay qw(dienow dieifsslerror) ;

Net::SSLeay::loaderrorstrings();

Net::SSLeay::SSLeayaddsslalgorithms();

Net::SSLeay::randomize();

($destserv, $port, $msg) = @ARGV; # Read command line

$port = getservbyname ($port, 'tcp') unless $port =~ /^\d+$/;

$destip = gethostbyname ($destserv);

$destservparams = sockaddrin($port, $destip);

socket (S, &AFINET, &SOCKSTREAM, 0) or die "socket: $!";

connect (S, $destservparams) or die "connect: $!";

select (S); $| = 1; select (STDOUT); # Eliminate STDIO buffering

# The network connection is now open, lets fire up SSL

$ctx = Net::SSLeay::CTXnew() or dienow("Failed to create SSLCTX $!");

Net::SSLeay::CTXsetoptions($ctx, &Net::SSLeay::OPALL)

and dieifsslerror("ssl ctx set options");

$ssl = Net::SSLeay::new($ctx) or dienow("Failed to create SSL $!");

Net::SSLeay::setfd($ssl, fileno(S)); # Must use fileno

$res = Net::SSLeay::connect($ssl) and dieifsslerror("ssl connect");

print "Cipher `" . Net::SSLeay::getcipher($ssl) . "'\n";

# Exchange data

$res = Net::SSLeay::write($ssl, $msg); # Perl knows how long $msg is

dieifsslerror("ssl write");

CORE::shutdown S, 1; # Half close -> No more output, sends EOF to server

$got = Net::SSLeay::read($ssl); # Perl returns undef on failure

dieifsslerror("ssl read");

print $got;

Net::SSLeay::free ($ssl); # Tear down connection

Net::SSLeay::CTXfree ($ctx);

close S; Following is a simple SSLeay echo server (non forking):

#!/usr/local/bin/perl -w

use Socket;

use Net::SSLeay qw(dienow dieifsslerror);

Net::SSLeay::loaderrorstrings();

Net::SSLeay::SSLeayaddsslalgorithms();

Net::SSLeay::randomize();

$ourip = "\0\0\0\0"; # Bind to all interfaces

$port = 1235;

$sockaddrtemplate = 'S n a4 x8';

$ourservparams = pack ($sockaddrtemplate, &AFINET, $port, $ourip);

socket (S, &AFINET, &SOCKSTREAM, 0) or die "socket: $!";

bind (S, $ourservparams) or die "bind: $!";

listen (S, 5) or die "listen: $!";

$ctx = Net::SSLeay::CTXnew () or dienow("CTXnew ($ctx): $!");

Net::SSLeay::CTXsetoptions($ctx, &Net::SSLeay::OPALL)

and dieifsslerror("ssl ctx set options");

# Following will ask password unless private key is not encrypted

Net::SSLeay::CTXuseRSAPrivateKeyfile ($ctx, 'plain-rsa.pem',

&Net::SSLeay::FILETYPEPEM);

dieifsslerror("private key");

Net::SSLeay::CTXusecertificatefile ($ctx, 'plain-cert.pem',

&Net::SSLeay::FILETYPEPEM);

dieifsslerror("certificate"); while (1) { print "Accepting connections...\n";

($addr = accept (NS, S)) or die "accept: $!";

select (NS); $| = 1; select (STDOUT); # Piping hot!

($af,$clientport,$clientip) = unpack($sockaddrtemplate,$addr);

@inetaddr = unpack('C4',$clientip);

print "$af connection from " .

join ('.', @inetaddr) . ":$clientport\n";

# We now have a network connection, lets fire up SSLeay...

$ssl = Net::SSLeay::new($ctx) or dienow("SSLnew ($ssl): $!");

Net::SSLeay::setfd($ssl, fileno(NS));

$err = Net::SSLeay::accept($ssl) and dieifsslerror('ssl accept');

print "Cipher `" . Net::SSLeay::getcipher($ssl) . "'\n";

# Connected. Exchange some data.

$got = Net::SSLeay::read($ssl); # Returns undef on fail

dieifsslerror("ssl read");

print "Got `$got' (" . length ($got) . " chars)\n";

Net::SSLeay::write ($ssl, uc ($got)) or die "write: $!";

dieifsslerror("ssl write");

Net::SSLeay::free ($ssl); # Tear down connection

close NS; } Yet another echo server. This one runs from /etc/inetd.conf so it avoids all the socket code overhead. Only caveat is opening rsa key

file - it had better be without any encryption or else it will not know

where to ask for the password. Note how STDIN and STDOUT are wired to SSL.

#!/usr/local/bin/perl

# /etc/inetd.conf

# ssltst stream tcp nowait root /path/to/server.pl server.pl

# /etc/services

# ssltst 1234/tcp

use Net::SSLeay qw(dienow dieifsslerror);

Net::SSLeay::loaderrorstrings();

Net::SSLeay::SSLeayaddsslalgorithms();

Net::SSLeay::randomize();

chdir '/key/dir' or die "chdir: $!";

$| = 1; # Piping hot!

open LOG, ">>/dev/console" or die "Can't open log file $!";

select LOG; print "server.pl started\n";

$ctx = Net::SSLeay::CTXnew() or dienow "CTXnew ($ctx) ($!)";

$ssl = Net::SSLeay::new($ctx) or dienow "new ($ssl) ($!)";

Net::SSLeay::setoptions($ssl, &Net::SSLeay::OPALL)

and dieifsslerror("ssl set options");

# We get already open network connection from inetd, now we just

# need to attach SSLeay to STDIN and STDOUT

Net::SSLeay::setrfd($ssl, fileno(STDIN));

Net::SSLeay::setwfd($ssl, fileno(STDOUT));

Net::SSLeay::useRSAPrivateKeyfile ($ssl, 'plain-rsa.pem',

&Net::SSLeay::FILETYPEPEM);

dieifsslerror("private key");

Net::SSLeay::usecertificatefile ($ssl, 'plain-cert.pem',

&Net::SSLeay::FILETYPEPEM);

dieifsslerror("certificate");

Net::SSLeay::accept($ssl) and dieifsslerr("ssl accept: $!");

print "Cipher `" . Net::SSLeay::getcipher($ssl) . "'\n";

$got = Net::SSLeay::read($ssl);

dieifsslerror("ssl read");

print "Got `$got' (" . length ($got) . " chars)\n";

Net::SSLeay::write ($ssl, uc($got)) or die "write: $!";

dieifsslerror("ssl write");

Net::SSLeay::free ($ssl); # Tear down the connection

Net::SSLeay::CTXfree ($ctx);

close LOG; There are also a number of example/test programs in the examples directory:

sslecho.pl - A simple server, not unlike the one above

minicli.pl - Implements a client using low level SSLeay routines

sslcat.pl - Demonstrates using high level sslcat utility function

getpage.pl - Is a utility for getting html pages from secure servers

callback.pl - Demonstrates certificate verification and callback usage

stdiobulk.pl - Does SSL over Unix pipes

ssl-inetd-serv.pl - SSL server that can be invoked from inetd.conf

httpd-proxy-snif.pl - Utility that allows you to see how a browser

sends https request to given server and what reply

it gets back (very educative :-)

makecert.pl - Creates a self signed cert (does not use this module)

LLIIMMIITTAATTIIOONNSS

Net::SSLeay::read uses internal buffer of 32KB, thus no single read

will return more. In practice one read returns much less, usually as much as fits in one network packet. To work around this, you should use a loop like this:

$reply = '';

while ($got = Net::SSLeay::read($ssl)) {

last if printerrs('SSLread');

$reply .= $got;

}

Although there is no built-in limit in Net::SSLeay::write, the network

packet size limitation applies here as well, thus use:

$written = 0;

while ($written < length($message)) {

$written += Net::SSLeay::write($ssl, substr($message, $written));

last if printerrs('SSLwrite'); } Or alternatively you can just use the following convinence functions:

Net::SSLeay::sslwriteall($ssl, $message) or die "ssl write failure";

$got = Net::SSLeay::sslreadall($ssl) or die "ssl read failure";

KNOWN BUGS AND CAVEATS

Autoloader emits Argument "xxx" isn't numeric in entersub at blib/lib/Net/SSLeay.pm' warning if dieifsslerror is made autoloadable. If you figure out why, drop me a line. Callback set using SSLsetverify() does not appear to work. This may well be eay problem (e.g. see ssl/ssllib.c line 1029). Try using SSLCTXsetverify() instead and do not be surprised if even this stops working in future versions. Callback and certificate verification stuff is generally too little tested. Random numbers are not initialized randomly enough, especially if you do not have /dev/random and/or /dev/urandom (such as in Solaris

platforms - but I've been suggested that cryptorand daemon from SUNski

package solves this). In this case you should investigate third party software that can emulate these devices, e.g. by way of a named pipe to some program. Another gotcha with random number initialization is randomness depletion. This phenomenon, which has been extensively discussed in

OpenSSL, Apache-SSL, and Apache-modssl forums, can cause your script

to block if you use /dev/random or to operate insecurely if you use /dev/urandom. What happens is that when too much randomness is drawn from the operating system's randomness pool then randomness can temporarily be unavailable. /dev/random solves this problem by waiting

until enough randomness can be gathered - and this can take a long time

since blocking reduces activity in the machine and less activity provides less random events: a vicious circle. /dev/urandom solves this dilemma more pragmatically by simply returning predictable "random" numbers. Some /dev/urandom emulation software however actually seems to implement /dev/random semantics. Caveat emptor. I've been pointed to two such daemons by Mik Firestone who has used them on Solaris 8 1. Entropy Gathering Daemon (EGD) at http://www.lothar.com/tech/crypto/

2. Pseudo-random number generating daemon (PRNGD) at

http://www.aet.tu-cottbus.de/personen/jaenicke/postfixtls/prngd.html

If you are using the low level API functions to communicate with other SSL implementations, you would do well to call

Net::SSLeay::CTXsetoptions($ctx, &Net::SSLeay::OPALL)

and dieifsslerror("ssl ctx set options"); to cope with some well know bugs in some other SSL implementations. The high level API functions always set all known compatibility options. Sometimes sslcat (and the high level https functions that build on it) is too fast in signaling the EOF to legacy https servers. This causes the server to return empty page. To work around this problem you can set global variable

$Net::SSLeay::slowly = 1; # Add sleep so broken servers can keep up

http/1.1 is not supported. Specifically this module does not know to issue or serve multiple http requests per connection. This is a serious short coming, but using SSL session cache on your server helps to alleviate the CPU load somewhat. As of version 1.09 many newer OpenSSL auxiliary functions were added (from REMAUTOMATICALLYGENERATED109 onwards in SSLeay.xs). Unfortunately I have not had any opportunity to test these. Some of them are trivial enough that I believe they "just work", but others have rather complex interfaces with function pointers and all. In these cases you should proceed wit great caution. This module defaults to using OpenSSL automatic protocol negotiation code for automatically detecting the version of the SSL protocol that the other end talks. With most web servers this works just fine, but once in a while I get complaints from people that the module does not work with some web servers. Usually this can be solved by explicitly setting the protocol version, e.g.

$Net::SSLeay::sslversion = 2; # Insist on SSLv2

$Net::SSLeay::sslversion = 3; # Insist on SSLv3

$Net::SSLeay::sslversion = 10; # Insist on TLSv1

Although the autonegotiation is nice to have, the SSL standards do not formally specify any such mechanism. Most of the world has accepted the SSLeay/OpenSSL way of doing it as the de facto standard. But for the few that think differently, you have to explicitly speak the correct version. This is not really a bug, but rather a deficiency in the standards. If a site refuses to respond or sends back some nonsensical error codes (at SSL handshake level), try this option before mailing me. The high level API returns the certificate of the peer, thus allowing one to check what certificate was supplied. However, you will only be able to check the certificate after the fact, i.e. you already sent your form data by the time you find out that you did not trust them, oops. So, while being able to know the certificate after the fact is surely useful, the security minded would still choose to do the connection and certificate verification first and only after that exchange data with the site. Currently none of the high level API functions do this, thus you would have to program it using the low level API. A good place to

start is to see how Net::SSLeay::httpcat() function is implemented.

The high level API functions use a global file handle SSLCATS internally. This really should not be a problem because there is no way to interleave the high level API functions, unless you use threads (but threads are not very well supported in perl anyway (as of version 5.6.1). However, you may run into problems if you call undocumented internal functions in an interleaved fashion. DIAGNOSTICS "Random number generator not seeded!!!" This warning indicates that randomize() was not able to read /dev/random or /dev/urandom, possibly because your system does not have them or they are differently named. You can still use SSL, but the encryption will not be as strong. "opentcpconnection: destination host not found:`server' (port 123)

($!)"

Name lookup for host named `server' failed.

"opentcpconnection: failed `server', 123 ($!)"

The name was resolved, but establising the TCP connection failed.

"msg 123: 1 - error:140770F8:SSL

routines:SSL23GETSERVERHELLO:unknown proto" SSLeay error string. First (123) number is PID, second number (1) indicates the position of the error message in SSLeay error stack. You often see a pile of these messages as errors cascade.

"msg 123: 1 - error:02001002::lib(2) :func(1) :reason(2)"

The same as above, but you didn't call loaderrorstrings() so SSLeay couldn't verbosely explain the error. You can still find out what it means with this command: /usr/local/ssl/bin/ssleay errstr 02001002 Password is being asked for private key This is normal behaviour if your private key is encrypted. Either you have to supply the password or you have to use unencrypted private key. Scan OpenSSL.org for the FAQ that explains how to do this (or just study examples/makecert.pl which is used during `make test' to do just that).

REPORTING BUGS AND SUPPORT

Bug reports, patch submission, feature requests, subversion access to the latest source code etc can be obtained at

http://alioth.debian.org/projects/net-ssleay

The developer mailing list (for people interested in contributin to the source code) can be found at

http://lists.alioth.debian.org/mailman/listinfo/net-ssleay-devel

Commercial support for Net::SSLeay may be obtained from

Symlabs (netssleay@symlabs.com)

Tel: +351-214.222.630

Fax: +351-214.222.637

VVEERRSSIIOONN This man page documents version 1.24, released on 18.8.2003. There are currently two perl modules for using OpenSSL C library:

Net::SSLeay (maintaned by me) and SSLeay (maintained by OpenSSL team).

This module is the Net::SSLeay variant.

At the time of making this release, Eric's module was still quite sketchy and could not be used for real work, thus I felt motivated to make this maintenance release. This module is not planned to evolve to contain any further functionality, i.e. I will concentrate on just making a simple SSL connection over TCP socket. Presumably Eric's own module will offer full SSLeay API one day.

This module uses OpenSSL-0.9.6c. It does not work with any earlier

version and there is no guarantee that it will work with later versions either, though as long as C API does not change, it should. This module requires perl5.005, or 5.6.0 (or better?) though I believe it would build with any perl5.002 or newer. AUTHOR Originally written by Sampo Kellomaeki Maintained by Mike McCauley and Florian Ragwitz since November 2005 COPYRIGHT

Copyright (c) 1996-2003 Sampo Kellomaeki Copyright

(C) 2005 Florian Ragwitz Copyright (C) 2005 Mike McCauley All Rights Reserved. Distribution and use of this module is under the same terms as the OpenSSL package itself (i.e. free, but mandatory attribution; NO WARRANTY). Please consult LICENSE file in the root of the OpenSSL distribution. While the source distribution of this perl module does not contain Eric's or OpenSSL's code, if you use this module you will use OpenSSL library. Please give Eric and OpenSSL team credit (as required by their licenses). And remember, you, and nobody else but you, are responsible for auditing this module and OpenSSL library for security problems, backdoors, and general suitability for your application.

SEE ALSO

Net::SSLeay::Handle - File handle interface

./NetSSLeay/examples - Example servers and a clients

- Net::SSLeay.pm home

- Another module using OpenSSL

- OpenSSL source, documentation, etc

openssl-users-request@openssl.org - General OpenSSL mailing list

- SSL Draft specification

- HTTP specifications

- How to send password

- Entropy Gathering Daemon (EGD)

- pseudo-random number generating daemon (PRNGD)

perl(1) perlref(1) perllol(1) perldoc ~openssl/doc/ssl/SSLCTXsetverify.pod

perl v5.8.8 2005-12-20 SSLeay(3)




Contact us      |      About us      |      Term of use      |       Copyright © 2000-2019 MyWebUniversity.com ™