|
|
IO::Socket::SSL -- Nearly transparent SSL encapsulation for IO::Socket::INET.
use IO::Socket::SSL;
my $client = new IO::Socket::SSL("www.example.com:https");
if (defined $client) { print $client "GET / HTTP/1.0\r\n\r\n"; print <$client>; close $client; } else { warn "I encountered a problem: ", &IO::Socket::SSL::errstr(); }
This module is a true drop-in replacement for IO::Socket::INET that uses SSL to encrypt data before it is transferred to a remote server or client. IO::Socket::SSL supports all the extra features that one needs to write a full-featured SSL client or server application: multiple SSL contexts, cipher selection, certificate verification, and SSL version selection. As an extra bonus, it works perfectly with mod_perl.
If you have never used SSL before, you should read the appendix labelled 'Using SSL' before attempting to use this module.
If you have used this module before, read on, as versions 0.90 and above represent a complete rewrite of the IO::Socket::SSL internals.
IO::Socket::SSL inherits its methods from IO::Socket::INET, overriding them as necessary. If there is an SSL error, the method (or operation) will return an undefined value. The methods that have changed from the perspective of the user are re-documented here:
shutdown()
on your
sockets. Since the SSL protocol mandates that a SSL ``close notify'' message be
sent before the socket is closed, a shutdown()
that closes the socket's write channel
will cause the close call to hang. For a similar reason, if you try to close a
copy of a socket (as in a forking server) you will affect the original socket as well.
To get around these problems, call close with an object-oriented syntax
(e.g. $socket->close(SSL_no_shutdown => 1))
and one or more of the following parameters:
close()
not use the SSL_shutdown()
call
on the socket in question so that the close operation can complete without problems
if you have used shutdown()
or are working on a copy of a socket.
peek()
with the same arguments will return the same results.
This function requires Net::SSLeay v1.19 or higher and OpenSSL 0.9.6a or later to work.
dump_peer_certificate()
method of Net::SSLeay.
SSL wants a read first!
or SSL wants a write first!
meaning that the other side
is expecting to read from or write to the socket and wants to be satisfied before you
get to do anything.
The following methods are unsupported (not to mention futile!) and IO::Socket::SSL
will emit a large CROAK()
if you are silly enough to use them:
If you are having problems using IO::Socket::SSL despite the fact that can recite backwards the section of this documentation labelled 'Using SSL', you should try enabling debugging. To specify the debug level, pass 'debug#' (where # is a number from 0 to 4) to IO::Socket::SSL when calling it:
You can also set $IO::Socket::SSL::DEBUG to 0-4, but that's a bit of a mouthful, isn't it?
See the 'example' directory.
I have never shipped a module with a known bug, and IO::Socket::SSL is no different. If you feel that you have found a bug in the module and you are using the latest version of Net::SSLeay, send an email immediately to behroozi@www.pls.uni.edu with a subject of 'IO::Socket::SSL Bug'. I am not responsible for problems in your code, so make sure that an example actually works before sending it. It is merely acceptable if you send me a bug report, it is better if you send a small chunk of code that points it out, and it is best if you send a patch--if the patch is good, you might see a release the next day on CPAN. Otherwise, it could take weeks . . .
IO::Socket::SSL uses Net::SSLeay as the shiny interface to OpenSSL, which is the shiny interface to the ugliness of SSL. As a result, you will need both Net::SSLeay (1.20 recommended) and OpenSSL (0.9.6g recommended) on your computer before using this module.
The following functions are deprecated and are only retained for compatibility:
context_init()
socketToSSL()
socket_to_SSL())
get_peer_certificate()
and friendspeer_certificate()
function instead)
want_read()
and want_write()
errstr())
The following classes have been removed:
get_peer_certificate()
will still Do The Right Thing)
IO::Socket::INET, Net::SSLeay.
Peter Behroozi, behroozi@www.pls.uni.edu.
Marko Asplund, aspa@kronodoc.fi, was the original author of IO::Socket::SSL.
The rewrite of this module is Copyright (C) 2002 Peter Behroozi.
This module is Copyright (C) 1999-2002 Marko Asplund.
This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
If you are unfamiliar with the way OpenSSL works, a good reference may be found in both the book ``Network Security with OpenSSL'' (Oreilly & Assoc.) and the web site http://www.tldp.org/HOWTO/SSL-Certificates-HOWTO/. Read on for a quick overview.
The usual reason for using SSL is to keep your data safe. This means that not only do you have to encrypt the data while it is being transported over a network, but you also have to make sure that the right person gets the data. To accomplish this with SSL, you have to use certificates. A certificate closely resembles a Government-issued ID (at least in places where you can trust them). The ID contains some sort of identifying information such as a name and address, and is usually stamped with a seal of Government Approval. Theoretically, this means that you may trust the information on the card and do business with the owner of the card. The ideas apply to SSL certificates, which have some identifying information and are ``stamped'' [most people refer to this as signing instead] by someone (a Certificate Authority) who you trust will adequately verify the identifying information. In this case, because of some clever number theory, it is extremely difficult to falsify the stamping process. Another useful consequence of number theory is that the certificate is linked to the encryption process, so you may encrypt data (using information on the certificate) that only the certificate owner can decrypt.
What does this mean for you? It means that at least one person in the party has to have an ID to get drinks :-). Seriously, it means that one of the people communicating has to have a certificate to ensure that your data is safe. For client/server interactions, the server must always have a certificate. If the server wants to verify that the client is safe, then the client must also have a personal certificate. To verify that a certificate is safe, one compares the stamped ``seal'' [commonly called an encrypted digest/hash/signature] on the certificate with the official ``seal'' of the Certificate Authority to make sure that they are the same. To do this, you will need the [unfortunately named] certificate of the Certificate Authority. With all these in hand, you can set up a SSL connection and be reasonably confident that no-one is reading your data.
For servers, you will need to generate a cryptographic private key and a certificate request. You will need to send the certificate request to a Certificate Authority to get a real certificate back, after which you can start serving people. For clients, you will not need anything unless the server wants validation, in which case you will also need a private key and a real certificate. For more information about how to get these, see http://www.modssl.org/docs/2.8/ssl_faq.html#ToC24.