Manual Pages for UNIX Darwin command on man IO::Socket::SSL
MyWebUniversity

Manual Pages for UNIX Darwin command on man IO::Socket::SSL

SSL(3) User Contributed Perl Documentation SSL(3)

NAME

IO::Socket::SSL - Nearly transparent SSL encapsulation for

IO::Socket::INET.

SYNOPSIS

use IO::Socket::SSL;

my $client = IO::Socket::SSL->new("www.example.com:https");

if ($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();

}

DESCRIPTION

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 modperl. 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.93 and

above have several changes from the previous IO::Socket::SSL versions

(especially see the note about return values).

If you are using non-blocking sockets read on, as version 0.98 added

better support for non-blocking.

MMEETTHHOODDSS

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 empty list (false in all contexts). The methods that

have changed from the perspective of the user are re-documented here:

nneeww((......))

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: SSLversion Sets the version of the SSL protocol used to transmit data. The

default is SSLv2/3, which auto-negotiates between SSLv2 and

SSLv3. You may specify 'SSLv2', 'SSLv3', or 'TLSv1' (case-

insensitive) if you do not want this behavior. SSLcipherlist 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

()

for more details. If this option is not used the openssl builtin default is used which is suitable for most cases. SSLusecert

If this is set, 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. SSLkeyfile 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 SSLpasswdcb option). SSLkey This is an EVPPKEY* and can be used instead of SSLkeyfile. Useful if you don't have your key in a file but create it dynamically or get it from a string (see openssl PEMreadbioPrivateKey etc for getting a EVPPKEY* from a string). SSLcertfile 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. Note that a key and certificate are only required for an SSL server, so you do not need to bother with these trifling options should you be setting up an unauthenticated client. SSLcert This is an X509* or an array of X509*. The first X509* is the internal representation of the certificate while the following ones are extra certificates. Useful if you create your certificate dynamically (like in a SSL intercepting proxy) or get it from a string (see openssl PEMreadbioX509 etc for getting a X509* from a string). SSLdhfile

If you want Diffie-Hellman key exchange you need to supply a

suitable file here or use the SSLdh parameter. See dhparam command in openssl for more information. SSLdh Like SSLdhfile, but instead of giving a file you use a preloaded or generated DH*. SSLpasswdcb 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. SSLcafile If you want to verify that the peer certificate has been signed by a reputable certificate authority, then you should 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.

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

SSLverifymode This option sets the verification mode for the peer certificate. The default (0x00) does no authentication. You may combine 0x01 (verify peer), 0x02 (fail verification if no peer certificate exists; ignored for clients), and 0x04 (verify client once) to change the default. SSLverifycallback 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). 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. SSLcheckcrl 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 SSLcapath. 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. SSLreusectx If you have already set the above options (SSLversion through SSLcheckcrl; this does not include SSLcipherlist 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 SSLreusectx parameter. You may also create a new instance of

the IO::Socket::SSL::SSLContext 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 setdefaultcontext() function. SSLsessioncachesize 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 SSLreusectx option to the new() calls (or use setdefaultcontext()) 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. SSLerrortrap 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 and the full text of the error message. cclloossee((......)) 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(SSLnoshutdown => 1)) and one or more of the

following parameters: SSLnoshutdown If set to a true value, this option will make close() not use the SSLshutdown() 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. SSLctxfree 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. ppeeeekk((......)) 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. ppeennddiinngg(()) 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. ggeettcciipphheerr(())

Returns the string form of the cipher that the IO::Socket::SSL

object is using. dduummppppeeeerrcceerrttiiffiiccaattee(()) Returns a parsable string with select fields from the peer SSL certificate. This method directly returns the result of the dumppeercertificate() method of Net::SSLeay.

ppeeeerrcceerrttiiffiiccaattee(($$ffiieelldd))

If a peer certificate exists, this function can retrieve values from it. Right now, the only fields it can return are "authority" and "owner" (or "issuer" and "subject" if you want to use OpenSSL names), corresponding to the certificate authority that signed the peer certificate and the owner of the peer certificate. This function returns a string with all the information about the particular field in one parsable line. If no field is given it returns the full certificate (x509). eerrrrssttrr(()) 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 $SSLERROR against the exported symbols

SSLWANTREAD and SSLWANTWRITE.

IIOO::::SSoocckkeett::::SSSSLL->>ssttaarrttSSSSLL(($$ssoocckkeett,, ...... ))

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 "SSLserver" to 1, i.e.

IO::Socket::SSL->startSSL($socket, SSLserver => 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->startSSL($socket) to achieve the desired effect.

Note that if startSSL() 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

acceptSSL or connectSSL and the upgraded object. To just upgrade the socket set SSSSLLssttaarrttHHaannddsshhaakkee explicitly to 0. If you call startSSL w/o this parameter it will revert to blocking behavior for acceptSSL and connectSSL.

IIOO::::SSoocckkeett::::SSSSLL->>nneewwffrroommffdd(($$ffdd,, ......))

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. IIOO::::SSoocckkeett::::SSSSLL::::sseettddeeffaauullttccoonntteexxtt((......))

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::SSLContext object. See the

SSLreusectx option of new() for more details. Note that this sets the default context globally, so use with caution (esp. in modperl scripts). 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: truncate stat ungetc setbuf setvbuf fdopen send/recv 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.

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

Support for IPv6 with IO::Socket::SSL is expected to work, but is

experimental, as none of the author's machines use IPv6 and hence he

cannot test IO::Socket::SSL with them. However, a few brave people

have used it without incident, so if you wish to make IO::Socket::SSL

IPv6 aware, pass the 'inet6' option to IO::Socket::SSL when calling it

(i.e. "use IO::Socket::SSL qw(inet6);"). You will need

IO::Socket::INET6 and Socket6 to use this option, and you will also

need to write "use Socket6;" before using IO::Socket::SSL. If you

absolutely do not want to use this (or want a quick change back to IPv4), pass the 'inet4' option instead. Currently, there is no support for using IPv4 and IPv6 simultaneously in a single program, but it is planned for a future release. DDEEBBUUGGGGIINNGG

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:

use IO::Socket::SSL qw(debug0);

#No debugging (default).

use IO::Socket::SSL qw(debug1);

#Only print out errors.

use IO::Socket::SSL qw(debug2);

#Print out errors and cipher negotiation.

use IO::Socket::SSL qw(debug3);

#Print out progress, ciphers, and errors.

use IO::Socket::SSL qw(debug4);

#Print out everything, including data.

You can also set $IO::Socket::SSL::DEBUG to 0-4, but that's a bit of a

mouthful, isn't it? EEXXAAMMPPLLEESS See the 'example' directory.

BUGS

IO::Socket::SSL is not threadsafe. This is because IO::Socket::SSL is

based on Net::SSLeay which uses a global object to access some of the API of openssl and is therefore not threadsafe. LLIIMMIITTAATTIIOONNSS

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. DDEEPPRREECCAATTIIOONNSS The following functions are deprecated and are only retained for compatibility: contextinit()

(use the SSLreusectx option if you want to re-use a context)

socketToSSL() and sockettoSSL()

(use IO::Socket::SSL->startSSL() instead)

getpeercertificate() and friends (use the peercertificate() function instead) The following classes have been removed: SSLSSL (not that you should have been directly accessing this anyway): X509Certificate (but getpeercertificate() will still Do The Right Thing)

SEE ALSO

IO::Socket::INET, IO::Socket::INET6, Net::SSLeay. AUTHORS Steffen Ullrich, is the current maintainer. Peter Behroozi, (Note the lack of an "i" at the end of "behrooz") Marko Asplund, , was the original author

of IO::Socket::SSL.

Patches incorporated from various people, see file Changes. COPYRIGHT

Working support for non-blocking was added by Steffen Ullrich.

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

The original versions of this module are 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. AAppppeennddiixx:: UUssiinngg SSSSLL 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

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

perl v5.8.8 2006-08-15 SSL(3)




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