Scroll to navigation

IO::Socket::SSL(3) User Contributed Perl Documentation IO::Socket::SSL(3)

NAME

IO::Socket::SSL -- SSL sockets with IO::Socket interface

SYNOPSIS

    use strict;
    use IO::Socket::SSL;
    # simple HTTP client -----------------------------------------------
    my $sock = IO::Socket::SSL->new(
        # where to connect
        PeerHost => "www.example.com",
        PeerPort => "https",
        # certificate verification
        SSL_verify_mode => SSL_VERIFY_PEER,
        # location of CA store
        # need only be given if default store should not be used
        SSL_ca_path => '/etc/ssl/certs', # typical CA path on Linux
        SSL_ca_file => '/etc/ssl/cert.pem', # typical CA file on BSD
        # easy hostname verification 
        SSL_verifycn_name => 'foo.bar', # defaults to PeerHost
        SSL_verifycn_schema => 'http',
        # SNI support
        SSL_hostname => 'foo.bar', # defaults to PeerHost
    ) or die "failed connect or ssl handshake: $!,$SSL_ERROR";
    # send and receive over SSL connection
    print $client "GET / HTTP/1.0\r\n\r\n";
    print <$client>;
    # simple server ----------------------------------------------------
    my $server = IO::Socket::SSL->new(
        # where to listen
        LocalAddr => '127.0.0.1',
        LocalPort => 8080,
        Listen => 10,
        # which certificate to offer
        # with SNI support there can be different certificates per hostname
        SSL_cert_file => 'cert.pem',
        SSL_key_file => 'key.pem',
    ) or die "failed to listen: $!";
    # accept client
    my $client = $server->accept or die 
        "failed to accept or ssl handshake: $!,$SSL_ERROR";
    # Upgrade existing socket to SSL ---------------------------------
    my $sock = IO::Socket::INET->new('imap.example.com:imap');
    # ... receive greeting, send STARTTLS, receive ok ...
    IO::Socket::SSL->start_SSL($sock,
        SSL_verify_mode => SSL_VERIFY_PEER,
        SSL_ca_path => '/etc/ssl/certs',
        ...
    ) or die "failed to upgrade to SSL: $SSL_ERROR";
    # manual name verification, could also be done in start_SSL with
    # SSL_verifycn_name etc
    $client->verify_hostname( 'imap.example.com','imap' )
        or die "hostname verification failed";
    # all data are now SSL encrypted
    print $sock ....

DESCRIPTION

This module provides an interface to SSL sockets, similar to other IO::Socket modules. Because of that, it can be used to make existing programs using IO::Socket::INET or similar modules to provide SSL encryption without much effort. 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, Server Name Indication (SNI), Next Protocol Negotiation (NPN), SSL version selection and more.

If you have never used SSL before, you should read the appendix labelled 'Using SSL' before attempting to use this module.

If you are trying to use it with threads see the BUGS section.

METHODS

IO::Socket::SSL inherits from another IO::Socket module. The choice of the super class depends on the installed modules:

  • If IO::Socket::IP with at least version 0.20 is installed it will use this module as super class, transparently providing IPv6 and IPv4 support.
  • If IO::Socket::INET6 is installed it will use this module as super class, transparently providing IPv6 and IPv4 support.
  • Otherwise it will fall back to IO::Socket::INET, which is a perl core module. With IO::Socket::INET you only get IPv4 support.

Please be aware, that with the IPv6 capable super classes, it will lookup first for the IPv6 address of a given hostname. If the resolver provides an IPv6 address, but the host cannot be reached by IPv6, there will be no automatic fallback to IPv4. To avoid these problems you can either force IPv4 by specifying and AF_INET as "Domain" of the socket or globally enforce IPv4 by loading IO::Socket::SSL with the option 'inet4'.

IO::Socket::SSL will provide all of the methods of its super class, but sometimes it will override them to match the behavior expected from SSL or to provide additional arguments.

The new or changed methods are described below, but please read also the section about SSL specific error handling.

Creates a new IO::Socket::SSL object. You may use all the friendly options that came bundled with IO::Socket::INET, plus (optionally) the ones that follow:
This can be given to specify the hostname used for SNI, which is needed if you have multiple SSL hostnames on the same IP address. If not given it will try to determine hostname from PeerAddr, which will fail if only IP was given or if this argument is used within start_SSL.

If you want to disable SNI set this argument to ''.

Currently only supported for the client side and will be ignored for the server side.

See section "SNI Support" for details of SNI the support.

Sets the version of the SSL protocol used to transmit data. 'SSLv23' auto-negotiates between SSLv2 and SSLv3, while 'SSLv2', 'SSLv3', 'TLSv1', 'TLSv1_1' or 'TLSv1_2' restrict the protocol to the specified version. All values are case-insensitive. Instead of 'TLSv1_1' and 'TLSv1_2' one can also use 'TLSv11' and 'TLSv12'. Support for 'TLSv1_1' and 'TLSv1_2' requires recent versions of Net::SSLeay and openssl.

You can limit to set of supported protocols by adding !version separated by ':'.

The default SSL_version is defined by underlying cryptographic library. E.g. 'SSLv23:!SSLv2' means, that SSLv2, SSLv3 and TLSv1 are supported for initial protocol handshakes, but SSLv2 will not be accepted, leaving only SSLv3 and TLSv1. You can also use !TLSv1_1 and !TLSv1_2 to disable TLS versions 1.1 and 1.2 while allowing TLS version 1.0.

Setting the version instead to 'TLSv1' will probably break interaction with lots of clients which start with SSLv2 and then upgrade to TLSv1. On the other side some clients just close the connection when they receive a TLS version 1.1 request. In this case setting the version to 'SSLv23:!SSLv2:!TLSv1_1:!TLSv1_2' might help.

If this option is set the cipher list for the connection will be set to the given value, e.g. something like 'ALL:!LOW:!EXP:!ADH'. Look into the OpenSSL documentation (<http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS>) for more details.

If this option is not set or is set to '', OpenSSL builtin default (whatever this is) will be used.

If this option is true the cipher order the server specified is used instead of the order proposed by the client. To mitigate BEAST attack you might use something like

  SSL_honor_cipher_order => 1,
  SSL_cipher_list => 'RC4-SHA:ALL:!ADH:!LOW',
    
If this is true, it forces IO::Socket::SSL to use a certificate and key, even if you are setting up an SSL client. If this is set to 0 (the default), then you will only need a certificate and key if you are setting up a server.

SSL_use_cert will implicitly be set if SSL_server is set. For convenience it is also set if it was not given but a cert was given for use (SSL_cert_file or similar).

Set this option to a true value, if the socket should be used as a server. If this is not explicitly set it is assumed, if the Listen parameter is given when creating the socket.
If your SSL certificate is not in the default place (certs/server-cert.pem for servers, certs/client-cert.pem for clients), then you should use this option to specify the location of your certificate. A certificate is usually needed for an SSL server, but might also be needed, if the client should authorize itself with a certificate.

If your SSL server should be able to use different certificates on the same IP address, depending on the name given by SNI, you can use a hash reference instead of a file with "<hostname =" cert_file>>.

Examples:

 SSL_cert_file => 'mycert.pem'
 SSL_cert_file => {
    "foo.example.org" => 'foo.pem',
    "bar.example.org" => 'bar.pem',
    # used when nothing matches or client does not support SNI
    '' => 'default.pem', 
 }
    
This option can be used instead of "SSL_cert_file" to specify the certificate.

Instead with a file the certificate is given as an X509* object or array of X509* objects, where the first X509* is the internal representation of the certificate while the following ones are extra certificates. The option is useful if you create your certificate dynamically (like in a SSL intercepting proxy) or get it from a string (see openssl PEM_read_bio_X509 etc for getting a X509* from a string).

For SNI support a hash reference can be given, similar to the "SSL_cert_file" option.

If your RSA private key is not in default place (certs/server-key.pem for servers, certs/client-key.pem for clients), then this is the option that you would use to specify a different location. Keys should be PEM formatted, and if they are encrypted, you will be prompted to enter a password before the socket is formed (unless you specified the SSL_passwd_cb option).

For SNI support a hash reference can be given, similar to the "SSL_cert_file" option.

This option can be used instead of "SSL_key" to specify the certificate. Instead of a file an EVP_PKEY* should be given. This option is useful if you don't have your key in a file but create it dynamically or get it from a string (see openssl PEM_read_bio_PrivateKey etc for getting a EVP_PKEY* from a string).

For SNI support a hash reference can be given, similar to the "SSL_key" option.

If you want Diffie-Hellman key exchange you need to supply a suitable file here or use the SSL_dh parameter. See dhparam command in openssl for more information. To create a server which provides perfect forward secrecy you need to either give the DH parameters or (better, because faster) the ECDH curve.
Like SSL_dh_file, but instead of giving a file you use a preloaded or generated DH*.
If you want Elliptic Curve Diffie-Hellmann key exchange you need to supply the OID or NID of a suitable curve (like 'prime256v1') here. To create a server which provides perfect forward secrecy you need to either give the DH parameters or (better, because faster) the ECDH curve.
If your private key is encrypted, you might not want the default password prompt from Net::SSLeay. This option takes a reference to a subroutine that should return the password required to decrypt your private key.
If you want to verify that the peer certificate has been signed by a reputable certificate authority, then you can use this option to locate the file containing the certificate(s) of the reputable certificate authorities if it is not already in the file certs/my-ca.pem or in a system-wide certificate authority certificates store. If you definitely want no SSL_ca_file used you should set it to undef.
If you are unusually friendly with the OpenSSL documentation, you might have set yourself up a directory containing several trusted certificates as separate files as well as an index of the certificates. If you want to use that directory for validation purposes, and that directory is not ca/, then use this option to point IO::Socket::SSL to the right place to look. If you definitely want no SSL_ca_path used you should set it to undef.
This option sets the verification mode for the peer certificate. You may combine SSL_VERIFY_PEER (verify_peer), SSL_VERIFY_FAIL_IF_NO_PEER_CERT (fail verification if no peer certificate exists; ignored for clients), SSL_VERIFY_CLIENT_ONCE (verify client once; ignored for clients). See OpenSSL man page for SSL_CTX_set_verify for more information.

The default is SSL_VERIFY_NONE for server (e.g. no check for client certificate). For historical reasons the default for client is currently also SSL_VERIFY_NONE, but this will change to SSL_VERIFY_PEER in the near future. To aid transition a warning is issued if the client is used with the default SSL_VERIFY_NONE, unless SSL_verify_mode was explicitly set by the application.

If you want to verify certificates yourself, you can pass a sub reference along with this parameter to do so. When the callback is called, it will be passed:
1. a true/false value that indicates what OpenSSL thinks of the certificate,
2. a C-style memory address of the certificate store,
3. a string containing the certificate's issuer attributes and owner attributes, and
4. a string containing any errors encountered (0 if no errors).
5. a C-style memory address of the peer's own certificate (convertible to PEM form with Net::SSLeay::PEM_get_string_X509()).

The function should return 1 or 0, depending on whether it thinks the certificate is valid or invalid. The default is to let OpenSSL do all of the busy work.

The callback will be called for each element in the certificate chain.

See the OpenSSL documentation for SSL_CTX_set_verify for more information.

Set the scheme used to automatically verify the hostname of the peer. See the information about the verification schemes in verify_hostname.

The default is undef, e.g. to not automatically verify the hostname. If no verification is done the other SSL_verifycn_* options have no effect, but you might still do manual verification by calling verify_hostname.

Set the name which is used in verification of hostname. If SSL_verifycn_scheme is set and no SSL_verifycn_name is given it will try to use the PeerHost and PeerAddr settings and fail if no name can be determined.

Using PeerHost or PeerAddr works only if you create the connection directly with "IO::Socket::SSL->new", if an IO::Socket::INET object is upgraded with start_SSL the name has to be given in SSL_verifycn_name.

If you want to verify that the peer certificate has not been revoked by the signing authority, set this value to true. OpenSSL will search for the CRL in your SSL_ca_path, or use the file specified by SSL_crl_file. See the Net::SSLeay documentation for more details. Note that this functionality appears to be broken with OpenSSL < v0.9.7b, so its use with lower versions will result in an error.
If you want to specify the CRL file to be used, set this value to the pathname to be used. This must be used in addition to setting SSL_check_crl.
If you have already set the above options (SSL_version through SSL_check_crl; this does not include SSL_cipher_list yet) for a previous instance of IO::Socket::SSL, then you can reuse the SSL context of that instance by passing it as the value for the SSL_reuse_ctx parameter. You may also create a new instance of the IO::Socket::SSL::SSL_Context class, using any context options that you desire without specifying connection options, and pass that here instead.

If you use this option, all other context-related options that you pass in the same call to new() will be ignored unless the context supplied was invalid. Note that, contrary to versions of IO::Socket::SSL below v0.90, a global SSL context will not be implicitly used unless you use the set_default_context() function.

With this callback you can make individual settings to the context after it got created and the default setup was done. The callback will be called with the CTX object from Net::SSLeay as the single argument.

Example for limiting the server session cache size:

  SSL_create_ctx_callback => sub { 
      my $ctx = shift;
          Net::SSLeay::CTX_sess_set_cache_size($ctx,128);
  }
    
If you make repeated connections to the same host/port and the SSL renegotiation time is an issue, you can turn on client-side session caching with this option by specifying a positive cache size. For successive connections, pass the SSL_reuse_ctx option to the new() calls (or use set_default_context()) to make use of the cached sessions. The session cache size refers to the number of unique host/port pairs that can be stored at one time; the oldest sessions in the cache will be removed if new ones are added.

This option does not effect the session cache a server has for it's clients, e.g. it does not affect SSL objects with SSL_server set.

Specifies session cache object which should be used instead of creating a new. Overrules SSL_session_cache_size. This option is useful if you want to reuse the cache, but not the rest of the context.

A session cache object can be created using "IO::Socket::SSL::Session_Cache->new( cachesize )".

Use set_default_session_cache() to set a global cache object.

This gives an id for the servers session cache. It's necessary if you want clients to connect with a client certificate. If not given but SSL_verify_mode specifies the need for client certificate a context unique id will be picked.
When using the accept() or connect() methods, it may be the case that the actual socket connection works but the SSL negotiation fails, as in the case of an HTTP client connecting to an HTTPS server. Passing a subroutine ref attached to this parameter allows you to gain control of the orphaned socket instead of having it be closed forcibly. The subroutine, if called, will be passed two parameters: a reference to the socket on which the SSL negotiation failed and the full text of the error message.
If used on the server side it specifies list of protocols advertised by SSL server as an array ref, e.g. ['spdy/2','http1.1']. On the client side it specifies the protocols offered by the client for NPN as an array ref. See also method next_proto_negotiated.

Next Protocol Negotioation (NPN) is available with Net::SSLeay 1.46+ and openssl-1.0.1+. To check support you might call "IO::Socket::SSL-"can_npn()>. If you use this option with an unsupported Net::SSLeay/OpenSSL it will throw an error.

There are a number of nasty traps that lie in wait if you are not careful about using close(). The first of these will bite you if you have been using 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:
If set to a true value, this option will make 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.
If set to true only a unidirectional shutdown will be done, e.g. only the close_notify (see SSL_shutdown(3)) will be called. Otherwise a bidirectional shutdown will be done. If used within close() it defaults to true, if used within stop_SSL() it defaults to false.
If you want to make sure that the SSL context of the socket is destroyed when you close it, set this option to a true value.
This function has exactly the same syntax as sysread(), and performs nearly the same task (reading data from the socket) but will not advance the read position so that successive calls to peek() with the same arguments will return the same results. This function requires OpenSSL 0.9.6a or later to work.
This function will let you know how many bytes of data are immediately ready for reading from the socket. This is especially handy if you are doing reads on a blocking socket or just want to know if new data has been sent over the socket.
Returns the string form of the cipher that the IO::Socket::SSL object is using.
Returns a parsable string with select fields from the peer SSL certificate. This method directly returns the result of the dump_peer_certificate() method of Net::SSLeay.
If a peer certificate exists, this function can retrieve values from it. If no field is given the internal representation of certificate from Net::SSLeay is returned. The following fields can be queried:
The certificate authority which signed the certificate.
The owner of the certificate.
The common name, usually the server name for SSL certificates.
Alternative names for the subject, usually different names for the same server, like example.org, example.com, *.example.com.

It returns a list of (typ,value) with typ GEN_DNS, GEN_IPADD etc (these constants are exported from IO::Socket::SSL). See Net::SSLeay::X509_get_subjectAltNames.

This gives the name requested by the client if Server Name Indication (SNI) was used.
This verifies the given hostname against the peer certificate using the given scheme. Hostname is usually what you specify within the PeerAddr.

Verification of hostname against a certificate is different between various applications and RFCs. Some scheme allow wildcards for hostnames, some only in subjectAltNames, and even their different wildcard schemes are possible.

To ease the verification the following schemes are predefined:

Simple wildcards in subjectAltNames are possible, e.g. *.example.org matches www.example.org but not lala.www.example.org. If nothing from subjectAltNames match it checks against the common name, but there are no wildcards allowed.
Extended wildcards in subjectAltNames and common name are possible, e.g. *.example.org or even www*.example.org. The common name will be only checked if no names are given in subjectAltNames.
This RFC doesn't say much useful about the verification so it just assumes that subjectAltNames are possible, but no wildcards are possible anywhere.
No verification will be done. Actually is does not make any sense to call verify_hostname in this case.

The scheme can be given either by specifying the name for one of the above predefined schemes, or by using a hash which can have the following keys and values:

Determines if the common name gets checked. If 'always' it will always be checked (like in ldap), if 'when_only' it will only be checked if no names are given in subjectAltNames (like in http), for any other values the common name will not be checked.
Determines if and where wildcards in subjectAltNames are possible. If 'leftmost' only cases like *.example.org will be possible (like in ldap), for 'anywhere' www*.example.org is possible too (like http), dangerous things like but www.*.org or even '*' will not be allowed.
Similar to wildcards_in_alt, but checks the common name. There is no predefined scheme which allows wildcards in common names.
If you give a subroutine for verification it will be called with the arguments ($hostname,$commonName,@subjectAltNames), where hostname is the name given for verification, commonName is the result from peer_certificate('cn') and subjectAltNames is the result from peer_certificate('subjectAltNames').

All other arguments for the verification scheme will be ignored in this case.

This method returns the name of negotiated protocol - e.g. 'http/1.1'. It works for both client and server side of SSL connection.

NPN support is available with Net::SSLeay 1.46+ and openssl-1.0.1+. To check support you might call "IO::Socket::SSL-"can_npn()>.

Returns the last error (in string form) that occurred. If you do not have a real object to perform this method on, call IO::Socket::SSL::errstr() instead.

For read and write errors on non-blocking sockets, this method may include the string "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. But with version 0.98 you are better comparing the global exported variable $SSL_ERROR against the exported symbols SSL_WANT_READ and SSL_WANT_WRITE.

This returns false if the socket could not be opened, 1 if the socket could be opened and the SSL handshake was successful done and -1 if the underlying IO::Handle is open, but the SSL handshake failed.
This will convert a glob reference or a socket that you provide to an IO::Socket::SSL object. You may also pass parameters to specify context or connection options as with a call to new(). If you are using this function on an accept()ed socket, you must set the parameter "SSL_server" to 1, i.e. IO::Socket::SSL->start_SSL($socket, SSL_server => 1). If you have a class that inherits from IO::Socket::SSL and you want the $socket to be blessed into your own class instead, use MyClass->start_SSL($socket) to achieve the desired effect.

Note that if start_SSL() fails in SSL negotiation, $socket will remain blessed in its original class. For non-blocking sockets you better just upgrade the socket to IO::Socket::SSL and call accept_SSL or connect_SSL and the upgraded object. To just upgrade the socket set SSL_startHandshake explicitly to 0. If you call start_SSL w/o this parameter it will revert to blocking behavior for accept_SSL and connect_SSL.

If given the parameter "Timeout" it will stop if after the timeout no SSL connection was established. This parameter is only used for blocking sockets, if it is not given the default Timeout from the underlying IO::Socket will be used.

This is the opposite of start_SSL(), e.g. it will shutdown the SSL connection and return to the class before start_SSL(). It gets the same arguments as close(), in fact close() calls stop_SSL() (but without downgrading the class).

Will return true if it succeeded and undef if failed. This might be the case for non-blocking sockets. In this case $! is set to EAGAIN and the ssl error to SSL_WANT_READ or SSL_WANT_WRITE. In this case the call should be retried again with the same arguments once the socket is ready is until it succeeds.

This will convert a socket identified via a file descriptor into an SSL socket. Note that the argument list does not include a "MODE" argument; if you supply one, it will be thoughtfully ignored (for compatibility with IO::Socket::INET). Instead, a mode of '+<' is assumed, and the file descriptor passed must be able to handle such I/O because the initial SSL handshake requires bidirectional communication.
You may use this to make IO::Socket::SSL automatically re-use a given context (unless specifically overridden in a call to new()). It accepts one argument, which should be either an IO::Socket::SSL object or an IO::Socket::SSL::SSL_Context object. See the SSL_reuse_ctx option of new() for more details. Note that this sets the default context globally, so use with caution (esp. in mod_perl scripts).
You may use this to make IO::Socket::SSL automatically re-use a given session cache (unless specifically overridden in a call to new()). It accepts one argument, which should be an IO::Socket::SSL::Session_Cache object or similar (e.g something which implements get_session and add_session like IO::Socket::SSL::Session_Cache does). See the SSL_session_cache option of new() for more details. Note that this sets the default cache globally, so use with caution.
With this function one can set defaults for all SSL_* parameter used for creation of the context, like the SSL_verify* parameter.

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:

Note that send() and recv() cannot be reliably trapped by a tied filehandle (such as that used by IO::Socket::SSL) and so may send unencrypted data over the socket. Object-oriented calls to these functions will fail, telling you to use the print/printf/syswrite and read/sysread families instead.

ERROR HANDLING

If an SSL specific error occurs the global variable $SSL_ERROR will be set. If the error occurred on an existing SSL socket the method "errstr" will give access to the latest socket specific error. Both $SSL_ERROR and "errstr" method give a dualvar similar to $!, e.g. providing an error number in numeric context or an error description in string context.

NON-BLOCKING I/O

If you have a non-blocking socket, the expected behavior on read, write, accept or connect is to set $! to EAGAIN if the operation can not be completed immediately.

With SSL there are cases, like with SSL handshakes, where the write operation can not be completed until it can read from the socket or vice versa. In these cases $! is set to EGAIN like expected, and additionally $SSL_ERROR is set to either SSL_WANT_READ or SSL_WANT_WRITE. Thus if you get EAGAIN on a SSL socket you must check $SSL_ERROR for SSL_WANT_* and adapt your event mask accordingly.

Using readline on non-blocking sockets does not make much sense and I would advise against using it. And, while the behavior is not documented for other IO::Socket classes, it will try to emulate the behavior seen there, e.g. to return the received data instead of blocking, even if the line is not complete. If an unrecoverable error occurs it will return nothing, even if it already received some data.

SNI Support

Newer extensions to SSL can distinguish between multiple hostnames on the same IP address using Server Name Indication (SNI).

Support for SNI on the client side was added somewhere in the OpenSSL 0.9.8 series, but only with 1.0 a bug was fixed when the server could not decide about its hostname. Therefore client side SNI is only supported with OpenSSL 1.0 or higher in IO::Socket::SSL. With a supported version, SNI is used automatically on the client side, if it can determine the hostname from "PeerAddr" or "PeerHost". On unsupported OpenSSL versions it will silently not use SNI. The hostname can also be given explicitly given with "SSL_hostname", but in this case it will throw in error, if SNI is not supported. To check for support you might call "IO::Socket::SSL-"can_client_sni()>.

On the server side earlier versions of OpenSSL are supported, but only together with Net::SSLeay version >= 1.50. To check for support you might call "IO::Socket::SSL-"can_server_sni()>. If server side SNI is supported, you might specify different certificates per host with "SSL_cert*" and "SSL_key*", and check the requested name using "get_servername".

RETURN VALUES

A few changes have gone into IO::Socket::SSL v0.93 and later with respect to return values. The behavior on success remains unchanged, but for all functions, the return value on error is now an empty list. Therefore, the return value will be false in all contexts, but those who have been using the return values as arguments to subroutines (like "mysub(IO::Socket::SSL(...)-"new, ...)>) may run into problems. The moral of the story: always check the return values of these functions before using them in any way that you consider meaningful.

DEBUGGING

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 3) to IO::Socket::SSL when calling it. The debug level will also be propagated to Net::SSLeay::trace, see also Net::SSLeay:

No debugging (default).
Print out errors from IO::Socket::SSL and ciphers from Net::SSLeay.
Print also information about call flow from IO::Socket::SSL and progress information from Net::SSLeay.
Print also some data dumps from IO::Socket::SSL and from Net::SSLeay.

EXAMPLES

See the 'example' directory.

BUGS

IO::Socket::SSL depends on Net::SSLeay. Up to version 1.43 of Net::SSLeay it was not thread safe, although it did probably work if you did not use SSL_verify_callback and SSL_password_cb.

If you use IO::Socket::SSL together with threads you should load it (e.g. use or require) inside the main thread before creating any other threads which use it. This way it is much faster because it will be initialized only once. Also there are reports that it might crash the other way.

Creating an IO::Socket::SSL object in one thread and closing it in another thread will not work.

IO::Socket::SSL does not work together with Storable::fd_retrieve/fd_store. See BUGS file for more information and how to work around the problem.

Non-blocking and timeouts (which are based on non-blocking) are not supported on Win32, because the underlying IO::Socket::INET does not support non-blocking on this platform.

If you have a server and it looks like you have a memory leak you might check the size of your session cache. Default for Net::SSLeay seems to be 20480, see the example for SSL_create_ctx_callback for how to limit it.

The default for SSL_verify_mode on the client is currently SSL_VERIFY_NONE, which is a very bad idea, thus the default will change in the near future. See documentation for SSL_verify_mode for more information.

LIMITATIONS

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 and OpenSSL on your computer before using this module.

If you have Scalar::Util (standard with Perl 5.8.0 and above) or WeakRef, IO::Socket::SSL sockets will auto-close when they go out of scope, just like IO::Socket::INET sockets. If you do not have one of these modules, then IO::Socket::SSL sockets will stay open until the program ends or you explicitly close them. This is due to the fact that a circular reference is required to make IO::Socket::SSL sockets act simultaneously like objects and glob references.

DEPRECATIONS

The following functions are deprecated and are only retained for compatibility:

use the SSL_reuse_ctx option if you want to re-use a context
use IO::Socket::SSL->start_SSL() instead
use close() instead
use the peer_certificate() function instead. Used to return X509_Certificate with methods subject_name and issuer_name. Now simply returns $self which has these methods (although deprecated).
use peer_certificate( 'issuer' ) instead
use peer_certificate( 'subject' ) instead

SEE ALSO

IO::Socket::INET, IO::Socket::INET6, IO::Socket::IP, Net::SSLeay.

AUTHORS

Steffen Ullrich, <steffen at genua.de> is the current maintainer.

Peter Behroozi, <behrooz at fas.harvard.edu> (Note the lack of an "i" at the end of "behrooz")

Marko Asplund, <marko.asplund at kronodoc.fi>, was the original author of IO::Socket::SSL.

Patches incorporated from various people, see file Changes.

COPYRIGHT

The original versions of this module are Copyright (C) 1999-2002 Marko Asplund.

The rewrite of this module is Copyright (C) 2002-2005 Peter Behroozi.

Versions 0.98 and newer are Copyright (C) 2006-2013 Steffen Ullrich.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Appendix: Using SSL

If you are unfamiliar with the way OpenSSL works, good references 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 Long of It (Detail)

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 same 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.

The Short of It (Summary)

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>.

2018-04-13 perl v5.16.3