1IO::Socket::SSL(3) User Contributed Perl Documentation IO::Socket::SSL(3)
2
3
4
6 IO::Socket::SSL - SSL sockets with IO::Socket interface
7
9 use strict;
10 use IO::Socket::SSL;
11
12 # simple client
13 my $cl = IO::Socket::SSL->new('www.google.com:443');
14 print $cl "GET / HTTP/1.0\r\n\r\n";
15 print <$cl>;
16
17 # simple server
18 my $srv = IO::Socket::SSL->new(
19 LocalAddr => '0.0.0.0:1234',
20 Listen => 10,
21 SSL_cert_file => 'server-cert.pem',
22 SSL_key_file => 'server-key.pem',
23 );
24 $srv->accept;
25
27 IO::Socket::SSL makes using SSL/TLS much easier by wrapping the
28 necessary functionality into the familiar IO::Socket interface and
29 providing secure defaults whenever possible. This way, existing
30 applications can be made SSL-aware without much effort, at least if you
31 do blocking I/O and don't use select or poll.
32
33 But, under the hood, SSL is a complex beast. So there are lots of
34 methods to make it do what you need if the default behavior is not
35 adequate. Because it is easy to inadvertently introduce critical
36 security bugs or just hard to debug problems, I would recommend
37 studying the following documentation carefully.
38
39 The documentation consists of the following parts:
40
41 · "Essential Information About SSL/TLS"
42
43 · "Basic SSL Client"
44
45 · "Basic SSL Server"
46
47 · "Common Usage Errors"
48
49 · "Common Problems with SSL"
50
51 · "Using Non-Blocking Sockets"
52
53 · "Advanced Usage"
54
55 · "Integration Into Own Modules"
56
57 · "Description Of Methods"
58
59 Additional documentation can be found in
60
61 · IO::Socket::SSL::Intercept - Doing Man-In-The-Middle with SSL
62
63 · IO::Socket::SSL::Utils - Useful functions for certificates etc
64
66 SSL (Secure Socket Layer) or its successor TLS (Transport Layer
67 Security) are protocols to facilitate end-to-end security. These
68 protocols are used when accessing web sites (https), delivering or
69 retrieving email, and in lots of other use cases. In the following
70 documentation we will refer to both SSL and TLS as simply 'SSL'.
71
72 SSL enables end-to-end security by providing two essential functions:
73
74 Encryption
75 This part encrypts the data for transit between the communicating
76 parties, so that nobody in between can read them. It also provides
77 tamper resistance so that nobody in between can manipulate the
78 data.
79
80 Identification
81 This part makes sure that you talk to the right peer. If the
82 identification is done incorrectly it is easy to mount man-in-the-
83 middle attacks, e.g. if Alice wants to talk to Bob it would be
84 possible for Mallory to put itself in the middle, so that Alice
85 talks to Mallory and Mallory to Bob. All the data would still be
86 encrypted, but not end-to-end between Alice and Bob, but only
87 between Alice and Mallory and then between Mallory and Bob. Thus
88 Mallory would be able to read and modify all traffic between Alice
89 and Bob.
90
91 Identification is the part which is the hardest to understand and the
92 easiest to get wrong.
93
94 With SSL, the Identification is usually done with certificates inside a
95 PKI (Public Key Infrastructure). These Certificates are comparable to
96 an identity card, which contains information about the owner of the
97 card. The card then is somehow signed by the issuer of the card, the CA
98 (Certificate Agency).
99
100 To verify the identity of the peer the following must be done inside
101 SSL:
102
103 · Get the certificate from the peer. If the peer does not present a
104 certificate we cannot verify it.
105
106 · Check if we trust the certificate, e.g. make sure it's not a
107 forgery.
108
109 We believe that a certificate is not a fake if we either know the
110 certificate already or if we trust the issuer (the CA) and can
111 verify the issuers signature on the certificate. In reality there
112 is often a hierarchy of certificate agencies and we only directly
113 trust the root of this hierarchy. In this case the peer not only
114 sends his own certificate, but also all intermediate certificates.
115 Verification will be done by building a trust path from the trusted
116 root up to the peers certificate and checking in each step if the
117 we can verify the issuer's signature.
118
119 This step often causes problems because the client does not know
120 the necessary trusted root certificates. These are usually stored
121 in a system dependent CA store, but often the browsers have their
122 own CA store.
123
124 · Check if the certificate is still valid. Each certificate has a
125 lifetime and should not be used after that time because it might be
126 compromised or the underlying cryptography got broken in the mean
127 time.
128
129 · Check if the subject of the certificate matches the peer. This is
130 like comparing the picture on the identity card against the person
131 representing the identity card.
132
133 When connecting to a server this is usually done by comparing the
134 hostname used for connecting against the names represented in the
135 certificate. A certificate might contain multiple names or
136 wildcards, so that it can be used for multiple hosts (e.g.
137 *.example.com and *.example.org).
138
139 Although nobody sane would accept an identity card where the
140 picture does not match the person we see, it is a common
141 implementation error with SSL to omit this check or get it wrong.
142
143 · Check if the certificate was revoked by the issuer. This might be
144 the case if the certificate was compromised somehow and now
145 somebody else might use it to claim the wrong identity. Such
146 revocations happened a lot after the heartbleed attack.
147
148 For SSL there are two ways to verify a revocation, CRL and OCSP.
149 With CRLs (Certificate Revocation List) the CA provides a list of
150 serial numbers for revoked certificates. The client somehow has to
151 download the list (which can be huge) and keep it up to date. With
152 OCSP (Online Certificate Status Protocol) the client can check a
153 single certificate directly by asking the issuer.
154
155 Revocation is the hardest part of the verification and none of
156 today's browsers get it fully correct. But, they are still better
157 than most other implementations which don't implement revocation
158 checks or leave the hard parts to the developer.
159
160 When accessing a web site with SSL or delivering mail in a secure way
161 the identity is usually only checked one way, e.g. the client wants to
162 make sure it talks to the right server, but the server usually does not
163 care which client it talks to. But, sometimes the server wants to
164 identify the client too and will request a certificate from the client
165 which the server must verify in a similar way.
166
168 A basic SSL client is simple:
169
170 my $client = IO::Socket::SSL->new('www.example.com:443')
171 or die "error=$!, ssl_error=$SSL_ERROR";
172
173 This will take the OpenSSL default CA store as the store for the
174 trusted CA. This usually works on UNIX systems. If there are no
175 certificates in the store it will try use Mozilla::CA which provides
176 the default CAs of Firefox.
177
178 In the default settings, IO::Socket::SSL will use a safer cipher set
179 and SSL version, do a proper hostname check against the certificate,
180 and use SNI (server name indication) to send the hostname inside the
181 SSL handshake. This is necessary to work with servers which have
182 different certificates behind the same IP address. It will also check
183 the revocation of the certificate with OCSP, but currently only if the
184 server provides OCSP stapling (for deeper checks see "ocsp_resolver"
185 method).
186
187 Lots of options can be used to change ciphers, SSL version, location of
188 CA and much more. See documentation of methods for details.
189
190 With protocols like SMTP it is necessary to upgrade an existing socket
191 to SSL. This can be done like this:
192
193 my $client = IO::Socket::INET->new('mx.example.com:25') or die $!;
194 # .. read greeting from server
195 # .. send EHLO and read response
196 # .. send STARTTLS command and read response
197 # .. if response was successful we can upgrade the socket to SSL now:
198 IO::Socket::SSL->start_SSL($client,
199 # explicitly set hostname we should use for SNI
200 SSL_hostname => 'mx.example.com'
201 ) or die $SSL_ERROR;
202
203 A more complete example for a simple HTTP client:
204
205 my $client = IO::Socket::SSL->new(
206 # where to connect
207 PeerHost => "www.example.com",
208 PeerPort => "https",
209
210 # certificate verification - VERIFY_PEER is default
211 SSL_verify_mode => SSL_VERIFY_PEER,
212
213 # location of CA store
214 # need only be given if default store should not be used
215 SSL_ca_path => '/etc/ssl/certs', # typical CA path on Linux
216 SSL_ca_file => '/etc/ssl/cert.pem', # typical CA file on BSD
217
218 # or just use default path on system:
219 IO::Socket::SSL::default_ca(), # either explicitly
220 # or implicitly by not giving SSL_ca_*
221
222 # easy hostname verification
223 # It will use PeerHost as default name a verification
224 # scheme as default, which is safe enough for most purposes.
225 SSL_verifycn_name => 'foo.bar',
226 SSL_verifycn_scheme => 'http',
227
228 # SNI support - defaults to PeerHost
229 SSL_hostname => 'foo.bar',
230
231 ) or die "failed connect or ssl handshake: $!,$SSL_ERROR";
232
233 # send and receive over SSL connection
234 print $client "GET / HTTP/1.0\r\n\r\n";
235 print <$client>;
236
237 And to do revocation checks with OCSP (only available with OpenSSL
238 1.0.0 or higher and Net::SSLeay 1.59 or higher):
239
240 # default will try OCSP stapling and check only leaf certificate
241 my $client = IO::Socket::SSL->new($dst);
242
243 # better yet: require checking of full chain
244 my $client = IO::Socket::SSL->new(
245 PeerAddr => $dst,
246 SSL_ocsp_mode => SSL_OCSP_FULL_CHAIN,
247 );
248
249 # even better: make OCSP errors fatal
250 # (this will probably fail with lots of sites because of bad OCSP setups)
251 # also use common OCSP response cache
252 my $ocsp_cache = IO::Socket::SSL::OCSP_Cache->new;
253 my $client = IO::Socket::SSL->new(
254 PeerAddr => $dst,
255 SSL_ocsp_mode => SSL_OCSP_FULL_CHAIN|SSL_OCSP_FAIL_HARD,
256 SSL_ocsp_cache => $ocsp_cache,
257 );
258
259 # disable OCSP stapling in case server has problems with it
260 my $client = IO::Socket::SSL->new(
261 PeerAddr => $dst,
262 SSL_ocsp_mode => SSL_OCSP_NO_STAPLE,
263 );
264
265 # check any certificates which are not yet checked by OCSP stapling or
266 # where we have already cached results. For your own resolving combine
267 # $ocsp->requests with $ocsp->add_response(uri,response).
268 my $ocsp = $client->ocsp_resolver();
269 my $errors = $ocsp->resolve_blocking();
270 if ($errors) {
271 warn "OCSP verification failed: $errors";
272 close($client);
273 }
274
276 A basic SSL server looks similar to other IO::Socket servers, only that
277 it also contains settings for certificate and key:
278
279 # simple server
280 my $server = IO::Socket::SSL->new(
281 # where to listen
282 LocalAddr => '127.0.0.1',
283 LocalPort => 8080,
284 Listen => 10,
285
286 # which certificate to offer
287 # with SNI support there can be different certificates per hostname
288 SSL_cert_file => 'cert.pem',
289 SSL_key_file => 'key.pem',
290 ) or die "failed to listen: $!";
291
292 # accept client
293 my $client = $server->accept or die
294 "failed to accept or ssl handshake: $!,$SSL_ERROR";
295
296 This will automatically use a secure set of ciphers and SSL version and
297 also supports Forward Secrecy with (Elliptic-Curve) Diffie-Hellmann Key
298 Exchange.
299
300 If you are doing a forking or threading server, we recommend that you
301 do the SSL handshake inside the new process/thread so that the master
302 is free for new connections. We recommend this because a client with
303 improper or slow SSL handshake could make the server block in the
304 handshake which would be bad to do on the listening socket:
305
306 # inet server
307 my $server = IO::Socket::INET->new(
308 # where to listen
309 LocalAddr => '127.0.0.1',
310 LocalPort => 8080,
311 Listen => 10,
312 );
313
314 # accept client
315 my $client = $server->accept or die;
316
317 # SSL upgrade client (in new process/thread)
318 IO::Socket::SSL->start_SSL($client,
319 SSL_server => 1,
320 SSL_cert_file => 'cert.pem',
321 SSL_key_file => 'key.pem',
322 ) or die "failed to ssl handshake: $SSL_ERROR";
323
324 Like with normal sockets, neither forking nor threading servers scale
325 well. It is recommended to use non-blocking sockets instead, see
326 "Using Non-Blocking Sockets"
327
329 This is a list of typical errors seen with the use of IO::Socket::SSL:
330
331 · Disabling verification with "SSL_verify_mode".
332
333 As described in "Essential Information About SSL/TLS", a proper
334 identification of the peer is essential and failing to verify makes
335 Man-In-The-Middle attacks possible.
336
337 Nevertheless, lots of scripts and even public modules or
338 applications disable verification, because it is probably the
339 easiest way to make the thing work and usually nobody notices any
340 security problems anyway.
341
342 If the verification does not succeed with the default settings, one
343 can do the following:
344
345 · Make sure the needed CAs are in the store, maybe use
346 "SSL_ca_file" or "SSL_ca_path" to specify a different CA
347 store.
348
349 · If the validation fails because the certificate is self-
350 signed and that's what you expect, you can use the
351 "SSL_fingerprint" option to accept specific leaf
352 certificates by their certificate or pubkey fingerprint.
353
354 · If the validation failed because the hostname does not
355 match and you cannot access the host with the name given in
356 the certificate, you can use "SSL_verifycn_name" to specify
357 the hostname you expect in the certificate.
358
359 A common error pattern is also to disable verification if they
360 found no CA store (different modules look at different "default"
361 places). Because IO::Socket::SSL is now able to provide a usable
362 CA store on most platforms (UNIX, Mac OSX and Windows) it is better
363 to use the defaults provided by IO::Socket::SSL. If necessary
364 these can be checked with the "default_ca" method.
365
366 · Polling of SSL sockets (e.g. select, poll and other event loops).
367
368 If you sysread one byte on a normal socket it will result in a
369 syscall to read one byte. Thus, if more than one byte is available
370 on the socket it will be kept in the network stack of your OS and
371 the next select or poll call will return the socket as readable.
372 But, with SSL you don't deliver single bytes. Multiple data bytes
373 are packaged and encrypted together in an SSL frame. Decryption can
374 only be done on the whole frame, so a sysread for one byte actually
375 reads the complete SSL frame from the socket, decrypts it and
376 returns the first decrypted byte. Further sysreads will return more
377 bytes from the same frame until all bytes are returned and the next
378 SSL frame will be read from the socket.
379
380 Thus, in order to decide if you can read more data (e.g. if sysread
381 will block) you must check if there are still data in the current
382 SSL frame by calling "pending" and if there are no data pending you
383 might check the underlying socket with select or poll. Another way
384 might be if you try to sysread at least 16kByte all the time.
385 16kByte is the maximum size of an SSL frame and because sysread
386 returns data from only a single SSL frame you can guarantee that
387 there are no pending data.
388
389 See also "Using Non-Blocking Sockets".
390
391 · Expecting exactly the same behavior as plain sockets.
392
393 IO::Socket::SSL tries to emulate the usual socket behavior as good
394 as possible, but full emulation can not be done. Specifically a
395 read on the SSL socket might also result in a write on the TCP
396 socket or a write on the SSL socket might result in a read on the
397 TCP socket. Also "accept" and close on the SSL socket will result
398 in writing and reading data to the TCP socket too.
399
400 Especially the hidden writes might result in a connection reset if
401 the underlying TCP socket is already closed by the peer. Unless
402 signal PIPE is explicitly handled by the application this will
403 ususally result in the application crashing. It is thus recommended
404 to explicitly IGNORE signal PIPE so that the errors get propagated
405 as EPIPE instead of causing a crash of the application.
406
407 · Set 'SSL_version' or 'SSL_cipher_list' to a "better" value.
408
409 IO::Socket::SSL tries to set these values to reasonable, secure
410 values which are compatible with the rest of the world. But, there
411 are some scripts or modules out there which tried to be smart and
412 get more secure or compatible settings. Unfortunately, they did
413 this years ago and never updated these values, so they are still
414 forced to do only 'TLSv1' (instead of also using TLSv12 or TLSv11).
415 Or they set 'HIGH' as the cipher list and thought they were secure,
416 but did not notice that 'HIGH' includes anonymous ciphers, e.g.
417 without identification of the peer.
418
419 So it is recommended to leave the settings at the secure defaults
420 which IO::Socket::SSL sets and which get updated from time to time
421 to better fit the real world.
422
423 · Make SSL settings inaccessible by the user, together with bad
424 builtin settings.
425
426 Some modules use IO::Socket::SSL, but don't make the SSL settings
427 available to the user. This is often combined with bad builtin
428 settings or defaults (like switching verification off).
429
430 Thus the user needs to hack around these restrictions by using
431 "set_args_filter_hack" or similar.
432
433 · Use of constants as strings.
434
435 Constants like "SSL_VERIFY_PEER" or "SSL_WANT_READ" should be used
436 as constants and not be put inside quotes, because they represent
437 numerical values.
438
439 · Forking and handling the socket in parent and child.
440
441 A fork of the process will duplicate the internal user space SSL
442 state of the socket. If both master and child interact with the
443 socket by using their own SSL state strange error messages will
444 happen. Such interaction includes explicit or implicit close of the
445 SSL socket. To avoid this the socket should be explicitly closed
446 with SSL_no_shutdown.
447
448 · Forking and executing a new process.
449
450 Since the SSL state is stored in user space it will be duplicated
451 by a fork but it will be lost when doing exec. This means it is not
452 possible to simply redirect stdin and stdout for the new process to
453 the SSL socket by duplicating the relevant file handles. Instead
454 explicitly exchanging plain data between child-process and SSL
455 socket are needed.
456
458 SSL is a complex protocol with multiple implementations and each of
459 these has their own quirks. While most of these implementations work
460 together, it often gets problematic with older versions, minimal
461 versions in load balancers, or plain wrong setups.
462
463 Unfortunately these problems are hard to debug. Helpful for debugging
464 are a knowledge of SSL internals, wireshark and the use of the debug
465 settings of IO::Socket::SSL and Net::SSLeay, which can both be set with
466 $IO::Socket::SSL::DEBUG. The following debugs levels are defined, but
467 used not in any consistent way:
468
469 · 0 - No debugging (default).
470
471 · 1 - Print out errors from IO::Socket::SSL and ciphers from
472 Net::SSLeay.
473
474 · 2 - Print also information about call flow from IO::Socket::SSL and
475 progress information from Net::SSLeay.
476
477 · 3 - Print also some data dumps from IO::Socket::SSL and from
478 Net::SSLeay.
479
480 Also, "analyze-ssl.pl" from the ssl-tools repository at
481 <https://github.com/noxxi/p5-ssl-tools> might be a helpful tool when
482 debugging SSL problems, as do the "openssl" command line tool and a
483 check with a different SSL implementation (e.g. a web browser).
484
485 The following problems are not uncommon:
486
487 · Bad server setup: missing intermediate certificates.
488
489 It is a regular problem that administrators fail to include all
490 necessary certificates into their server setup, e.g. everything
491 needed to build the trust chain from the trusted root. If they
492 check the setup with the browser everything looks ok, because
493 browsers work around these problems by caching any intermediate
494 certificates and apply them to new connections if certificates are
495 missing.
496
497 But, fresh browser profiles which have never seen these
498 intermediates cannot fill in the missing certificates and fail to
499 verify; the same is true with IO::Socket::SSL.
500
501 · Old versions of servers or load balancers which do not understand
502 specific TLS versions or croak on specific data.
503
504 From time to time one encounters an SSL peer, which just closes the
505 connection inside the SSL handshake. This can usually be worked
506 around by downgrading the SSL version, e.g. by setting
507 "SSL_version". Modern Browsers usually deal with such servers by
508 automatically downgrading the SSL version and repeat the connection
509 attempt until they succeed.
510
511 Worse servers do not close the underlying TCP connection but
512 instead just drop the relevant packet. This is harder to detect
513 because it looks like a stalled connection. But downgrading the SSL
514 version often works here too.
515
516 A cause of such problems are often load balancers or security
517 devices, which have hardware acceleration and only a minimal (and
518 less robust) SSL stack. They can often be detected because they
519 support much fewer ciphers than other implementations.
520
521 · Bad or old OpenSSL versions.
522
523 IO::Socket::SSL uses OpenSSL with the help of the Net::SSLeay
524 library. It is recommend to have a recent version of this library,
525 because it has more features and usually fewer known bugs.
526
527 · Validation of client certificates fail.
528
529 Make sure that the purpose of the certificate allows use as ssl
530 client (check with "openssl x509 -purpose", that the necessary root
531 certificate is in the path specified by "SSL_ca*" (or the default
532 path) and that any intermediate certificates needed to build the
533 trust chain are sent by the client.
534
535 · Validation of self-signed certificate fails even if it is given
536 with "SSL_ca*" argument.
537
538 The "SSL_ca*" arguments do not give a general trust store for
539 arbitrary certificates but only specify a store for CA certificates
540 which then can be used to verify other certificates. This
541 especially means that certificates which are not a CA get simply
542 ignored, notably self-signed certificates which do not also have
543 the CA-flag set.
544
545 This behavior of OpenSSL differs from the more general trust-store
546 concept which can be found in browsers and where it is possible to
547 simply added arbitrary certificates (CA or not) as trusted.
548
550 If you have a non-blocking socket, the expected behavior on read,
551 write, accept or connect is to set $! to EWOULDBLOCK if the operation
552 cannot be completed immediately. Note that EWOULDBLOCK is the same as
553 EAGAIN on UNIX systems, but is different on Windows.
554
555 With SSL, handshakes might occur at any time, even within an
556 established connection. In these cases it is necessary to finish the
557 handshake before you can read or write data. This might result in
558 situations where you want to read but must first finish the write of a
559 handshake or where you want to write but must first finish a read. In
560 these cases $! is set to EAGAIN like expected, and additionally
561 $SSL_ERROR is set to either SSL_WANT_READ or SSL_WANT_WRITE. Thus if
562 you get EWOULDBLOCK on a SSL socket you must check $SSL_ERROR for
563 SSL_WANT_* and adapt your event mask accordingly.
564
565 Using readline on non-blocking sockets does not make much sense and I
566 would advise against using it. And, while the behavior is not
567 documented for other IO::Socket classes, it will try to emulate the
568 behavior seen there, e.g. to return the received data instead of
569 blocking, even if the line is not complete. If an unrecoverable error
570 occurs it will return nothing, even if it already received some data.
571
572 Also, I would advise against using "accept" with a non-blocking SSL
573 object because it might block and this is not what most would expect.
574 The reason for this is that "accept" on a non-blocking TCP socket (e.g.
575 IO::Socket::IP, IO::Socket::INET..) results in a new TCP socket which
576 does not inherit the non-blocking behavior of the master socket. And
577 thus, the initial SSL handshake on the new socket inside
578 "IO::Socket::SSL::accept" will be done in a blocking way. To work
579 around this you are safer by doing a TCP accept and later upgrade the
580 TCP socket in a non-blocking way with "start_SSL" and "accept_SSL".
581
582 my $cl = IO::Socket::SSL->new($dst);
583 $cl->blocking(0);
584 my $sel = IO::Select->new($cl);
585 while (1) {
586 # with SSL a call for reading n bytes does not result in reading of n
587 # bytes from the socket, but instead it must read at least one full SSL
588 # frame. If the socket has no new bytes, but there are unprocessed data
589 # from the SSL frame can_read will block!
590
591 # wait for data on socket
592 $sel->can_read();
593
594 # new data on socket or eof
595 READ:
596 # this does not read only 1 byte from socket, but reads the complete SSL
597 # frame and then just returns one byte. On subsequent calls it than
598 # returns more byte of the same SSL frame until it needs to read the
599 # next frame.
600 my $n = sysread( $cl,my $buf,1);
601 if ( ! defined $n ) {
602 die $! if not ${EWOULDBLOCK};
603 next if $SSL_ERROR == SSL_WANT_READ;
604 if ( $SSL_ERROR == SSL_WANT_WRITE ) {
605 # need to write data on renegotiation
606 $sel->can_write;
607 next;
608 }
609 die "something went wrong: $SSL_ERROR";
610 } elsif ( ! $n ) {
611 last; # eof
612 } else {
613 # read next bytes
614 # we might have still data within the current SSL frame
615 # thus first process these data instead of waiting on the underlying
616 # socket object
617 goto READ if $cl->pending; # goto sysread
618 next; # goto $sel->can_read
619 }
620 }
621
622 Additionally there are differences to plain sockets when using select,
623 poll, kqueue or similar technologies to get notified if data are
624 available. Relying only on these calls is not sufficient in all cases
625 since unread data might be internally buffered in the SSL stack. To
626 detect such buffering pending() need to be used. Alternatively the
627 buffering can be avoided by using sysread with the maximum size of an
628 SSL frame. See "Common Usage Errors" for details.
629
631 SNI Support
632 Newer extensions to SSL can distinguish between multiple hostnames on
633 the same IP address using Server Name Indication (SNI).
634
635 Support for SNI on the client side was added somewhere in the OpenSSL
636 0.9.8 series, but with 1.0 a bug was fixed when the server could not
637 decide about its hostname. Therefore client side SNI is only supported
638 with OpenSSL 1.0 or higher in IO::Socket::SSL. With a supported
639 version, SNI is used automatically on the client side, if it can
640 determine the hostname from "PeerAddr" or "PeerHost" (which are
641 synonyms in the underlying IO::Socket:: classes and thus should never
642 be set both or at least not to different values). On unsupported
643 OpenSSL versions it will silently not use SNI. The hostname can also
644 be given explicitly given with "SSL_hostname", but in this case it will
645 throw in error, if SNI is not supported. To check for support you
646 might call "IO::Socket::SSL->can_client_sni()".
647
648 On the server side, earlier versions of OpenSSL are supported, but only
649 together with Net::SSLeay version >= 1.50. To check for support you
650 might call "IO::Socket::SSL->can_server_sni()". If server side SNI is
651 supported, you might specify different certificates per host with
652 "SSL_cert*" and "SSL_key*", and check the requested name using
653 "get_servername".
654
655 Talk Plain and SSL With The Same Socket
656 It is often required to first exchange some plain data and then upgrade
657 the socket to SSL after some kind of STARTTLS command. Protocols like
658 FTPS even need a way to downgrade the socket again back to plain.
659
660 The common way to do this would be to create a normal socket and use
661 "start_SSL" to upgrade and stop_SSL to downgrade:
662
663 my $sock = IO::Socket::INET->new(...) or die $!;
664 ... exchange plain data on $sock until starttls command ...
665 IO::Socket::SSL->start_SSL($sock,%sslargs) or die $SSL_ERROR;
666 ... now $sock is an IO::Socket::SSL object ...
667 ... exchange data with SSL on $sock until stoptls command ...
668 $sock->stop_SSL or die $SSL_ERROR;
669 ... now $sock is again an IO::Socket::INET object ...
670
671 But, lots of modules just derive directly from IO::Socket::INET. While
672 this base class can be replaced with IO::Socket::SSL, these modules
673 cannot easily support different base classes for SSL and plain data and
674 switch between these classes on a starttls command.
675
676 To help in this case, IO::Socket::SSL can be reduced to a plain socket
677 on startup, and connect_SSL/accept_SSL/start_SSL can be used to enable
678 SSL and "stop_SSL" to talk plain again:
679
680 my $sock = IO::Socket::SSL->new(
681 PeerAddr => ...
682 SSL_startHandshake => 0,
683 %sslargs
684 ) or die $!;
685 ... exchange plain data on $sock until starttls command ...
686 $sock->connect_SSL or die $SSL_ERROR;
687 ... now $sock is an IO::Socket::SSL object ...
688 ... exchange data with SSL on $sock until stoptls command ...
689 $sock->stop_SSL or die $SSL_ERROR;
690 ... $sock is still an IO::Socket::SSL object ...
691 ... but data exchanged again in plain ...
692
694 IO::Socket::SSL behaves similarly to other IO::Socket modules and thus
695 could be integrated in the same way, but you have to take special care
696 when using non-blocking I/O (like for handling timeouts) or using
697 select or poll. Please study the documentation on how to deal with
698 these differences.
699
700 Also, it is recommended to not set or touch most of the "SSL_*"
701 options, so that they keep their secure defaults. It is also
702 recommended to let the user override these SSL specific settings
703 without the need of global settings or hacks like
704 "set_args_filter_hack".
705
706 The notable exception is "SSL_verifycn_scheme". This should be set to
707 the hostname verification scheme required by the module or protocol.
708
710 IO::Socket::SSL inherits from another IO::Socket module. The choice of
711 the super class depends on the installed modules:
712
713 · If IO::Socket::IP with at least version 0.20 is installed it will
714 use this module as super class, transparently providing IPv6 and
715 IPv4 support.
716
717 · If IO::Socket::INET6 is installed it will use this module as super
718 class, transparently providing IPv6 and IPv4 support.
719
720 · Otherwise it will fall back to IO::Socket::INET, which is a perl
721 core module. With IO::Socket::INET you only get IPv4 support.
722
723 Please be aware that with the IPv6 capable super classes, it will look
724 first for the IPv6 address of a given hostname. If the resolver
725 provides an IPv6 address, but the host cannot be reached by IPv6, there
726 will be no automatic fallback to IPv4. To avoid these problems you can
727 enforce IPv4 for a specific socket by using the "Domain" or "Family"
728 option with the value AF_INET as described in IO::Socket::IP.
729 Alternatively you can enforce IPv4 globally by loading IO::Socket::SSL
730 with the option 'inet4', in which case it will use the IPv4 only class
731 IO::Socket::INET as the super class.
732
733 IO::Socket::SSL will provide all of the methods of its super class, but
734 sometimes it will override them to match the behavior expected from SSL
735 or to provide additional arguments.
736
737 The new or changed methods are described below, but please also read
738 the section about SSL specific error handling.
739
740 Error Handling
741 If an SSL specific error occurs, the global variable $SSL_ERROR
742 will be set. If the error occurred on an existing SSL socket, the
743 method "errstr" will give access to the latest socket specific
744 error. Both $SSL_ERROR and the "errstr" method give a dualvar
745 similar to $!, e.g. providing an error number in numeric context
746 or an error description in string context.
747
748 new(...)
749 Creates a new IO::Socket::SSL object. You may use all the friendly
750 options that came bundled with the super class (e.g.
751 IO::Socket::IP, IO::Socket::INET, ...) plus (optionally) the ones
752 described below. If you don't specify any SSL related options it
753 will do its best in using secure defaults, e.g. choosing good
754 ciphers, enabling proper verification, etc.
755
756 SSL_server
757 Set this option to a true value if the socket should be used as a
758 server. If this is not explicitly set it is assumed if the
759 "Listen" parameter is given when creating the socket.
760
761 SSL_hostname
762 This can be given to specify the hostname used for SNI, which is
763 needed if you have multiple SSL hostnames on the same IP address.
764 If not given it will try to determine the hostname from
765 "PeerAddr", which will fail if only an IP was given or if this
766 argument is used within "start_SSL".
767
768 If you want to disable SNI, set this argument to ''.
769
770 Currently only supported for the client side and will be ignored
771 for the server side.
772
773 See section "SNI Support" for details of SNI the support.
774
775 SSL_startHandshake
776 If this option is set to false (defaults to true) it will not
777 start the SSL handshake yet. This has to be done later with
778 "accept_SSL" or "connect_SSL". Before the handshake is started
779 read/write/etc. can be used to exchange plain data.
780
781 SSL_keepSocketOnError
782 If this option is set to true (defaults to false) it will not
783 close the underlying TCP socket on errors. In most cases there is
784 no real use for this behavior since both sides of the TCP
785 connection will probably have a different idea of the current
786 state of the connection.
787
788 SSL_ca | SSL_ca_file | SSL_ca_path
789 Usually you want to verify that the peer certificate has been
790 signed by a trusted certificate authority. In this case you
791 should use this option to specify the file ("SSL_ca_file") or
792 directory ("SSL_ca_path") containing the certificate(s) of the
793 trusted certificate authorities.
794
795 "SSL_ca_path" can also be an array or a string containing
796 multiple path, where the path are separated by the platform
797 specific separator. This separator is ";" on DOS, Windows,
798 Netware, "," on VMS and ":" for all the other systems. If
799 multiple path are given at least one of these must be accessible.
800
801 You can also give a list of X509* certificate handles (like you
802 get from Net::SSLeay or IO::Socket::SSL::Utils::PEM_xxx2cert)
803 with "SSL_ca". These will be added to the CA store before path
804 and file and thus take precedence. If neither SSL_ca, nor
805 SSL_ca_file or SSL_ca_path are set it will use "default_ca()" to
806 determine the user-set or system defaults. If you really don't
807 want to set a CA set SSL_ca_file or SSL_ca_path to "\undef" or
808 SSL_ca to an empty list. (unfortunately '' is used by some
809 modules using IO::Socket::SSL when CA is not explicitly given).
810
811 SSL_client_ca | SSL_client_ca_file
812 If verify_mode is VERIFY_PEER on the server side these options
813 can be used to set the list of acceptable CAs for the client.
814 This way the client can select they required certificate from a
815 list of certificates. The value for these options is similar to
816 "SSL_ca" and "SSL_ca_file".
817
818 SSL_fingerprint
819 Sometimes you have a self-signed certificate or a certificate
820 issued by an unknown CA and you really want to accept it, but
821 don't want to disable verification at all. In this case you can
822 specify the fingerprint of the certificate as
823 'algo$hex_fingerprint'. "algo" is a fingerprint algorithm
824 supported by OpenSSL, e.g. 'sha1','sha256'... and
825 "hex_fingerprint" is the hexadecimal representation of the binary
826 fingerprint. If you want to use the fingerprint of the pubkey
827 inside the certificate instead of the certificate use the syntax
828 'algo$pub$hex_fingerprint' instead. To get the fingerprint of an
829 established connection you can use "get_fingerprint".
830
831 You can specify a list of fingerprints in case you have several
832 acceptable certificates. If a fingerprint matches the topmost
833 (i.e. leaf) certificate no additional validations can make the
834 verification fail.
835
836 SSL_cert_file | SSL_cert | SSL_key_file | SSL_key
837 If you create a server you usually need to specify a server
838 certificate which should be verified by the client. Same is true
839 for client certificates, which should be verified by the server.
840 The certificate can be given as a file with SSL_cert_file or as
841 an internal representation of an X509* object (like you get from
842 Net::SSLeay or IO::Socket::SSL::Utils::PEM_xxx2cert) with
843 SSL_cert. If given as a file it will automatically detect the
844 format. Supported file formats are PEM, DER and PKCS#12, where
845 PEM and PKCS#12 can contain the certificate and the chain to use,
846 while DER can only contain a single certificate.
847
848 If given as a list of X509* please note, that the all the chain
849 certificates (e.g. all except the first) will be "consumed" by
850 openssl and will be freed if the SSL context gets destroyed - so
851 you should never free them yourself. But the servers certificate
852 (e.g. the first) will not be consumed by openssl and thus must be
853 freed by the application.
854
855 For each certificate a key is need, which can either be given as
856 a file with SSL_key_file or as an internal representation of an
857 EVP_PKEY* object with SSL_key (like you get from Net::SSLeay or
858 IO::Socket::SSL::Utils::PEM_xxx2key). If a key was already given
859 within the PKCS#12 file specified by SSL_cert_file it will ignore
860 any SSL_key or SSL_key_file. If no SSL_key or SSL_key_file was
861 given it will try to use the PEM file given with SSL_cert_file
862 again, maybe it contains the key too.
863
864 If your SSL server should be able to use different certificates
865 on the same IP address, depending on the name given by SNI, you
866 can use a hash reference instead of a file with "<hostname ="
867 cert_file>>.
868
869 In case certs and keys are needed but not given it might fall
870 back to builtin defaults, see "Defaults for Cert, Key and CA".
871
872 Examples:
873
874 SSL_cert_file => 'mycert.pem',
875 SSL_key_file => 'mykey.pem',
876
877 SSL_cert_file => {
878 "foo.example.org" => 'foo-cert.pem',
879 "bar.example.org" => 'bar-cert.pem',
880 # used when nothing matches or client does not support SNI
881 '' => 'default-cert.pem',
882 }
883 SSL_key_file => {
884 "foo.example.org" => 'foo-key.pem',
885 "bar.example.org" => 'bar-key.pem',
886 # used when nothing matches or client does not support SNI
887 '' => 'default-key.pem',
888 }
889
890 SSL_passwd_cb
891 If your private key is encrypted, you might not want the default
892 password prompt from Net::SSLeay. This option takes a reference
893 to a subroutine that should return the password required to
894 decrypt your private key.
895
896 SSL_use_cert
897 If this is true, it forces IO::Socket::SSL to use a certificate
898 and key, even if you are setting up an SSL client. If this is
899 set to 0 (the default), then you will only need a certificate and
900 key if you are setting up a server.
901
902 SSL_use_cert will implicitly be set if SSL_server is set. For
903 convenience it is also set if it was not given but a cert was
904 given for use (SSL_cert_file or similar).
905
906 SSL_version
907 Sets the version of the SSL protocol used to transmit data.
908 'SSLv23' uses a handshake compatible with SSL2.0, SSL3.0 and
909 TLS1.x, while 'SSLv2', 'SSLv3', 'TLSv1', 'TLSv1_1' or 'TLSv1_2'
910 restrict handshake and protocol to the specified version. All
911 values are case-insensitive. Instead of 'TLSv1_1' and 'TLSv1_2'
912 one can also use 'TLSv11' and 'TLSv12'. Support for 'TLSv1_1'
913 and 'TLSv1_2' requires recent versions of Net::SSLeay and
914 openssl. The default SSL_version is defined by the underlying
915 cryptographic library.
916
917 Independent from the handshake format you can limit to set of
918 accepted SSL versions by adding !version separated by ':'.
919
920 For example, 'SSLv23:!SSLv3:!SSLv2' means that the handshake
921 format is compatible to SSL2.0 and higher, but that the
922 successful handshake is limited to TLS1.0 and higher, that is no
923 SSL2.0 or SSL3.0 because both of these versions have serious
924 security issues and should not be used anymore. You can also use
925 !TLSv1_1 and !TLSv1_2 to disable TLS versions 1.1 and 1.2 while
926 still allowing TLS version 1.0.
927
928 Setting the version instead to 'TLSv1' might break interaction
929 with older clients, which need and SSL2.0 compatible handshake.
930 On the other side some clients just close the connection when
931 they receive a TLS version 1.1 request. In this case setting the
932 version to 'SSLv23:!SSLv2:!SSLv3:!TLSv1_1:!TLSv1_2' might help.
933
934 SSL_cipher_list
935 If this option is set the cipher list for the connection will be
936 set to the given value, e.g. something like
937 'ALL:!LOW:!EXP:!aNULL'. Look into the OpenSSL documentation
938 (<http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS>)
939 for more details.
940
941 Unless you fail to contact your peer because of no shared ciphers
942 it is recommended to leave this option at the default setting,
943 which honors the system-wide DEFAULT cipher list.
944
945 In case different cipher lists are needed for different SNI hosts
946 a hash can be given with the host as key and the cipher suite as
947 value, similar to SSL_cert*.
948
949 SSL_honor_cipher_order
950 If this option is true the cipher order the server specified is
951 used instead of the order proposed by the client. This option
952 defaults to true to make use of our secure cipher list setting.
953
954 SSL_dh_file
955 If you want Diffie-Hellman key exchange you need to supply a
956 suitable file here or use the SSL_dh parameter. See dhparam
957 command in openssl for more information. To create a server
958 which provides forward secrecy you need to either give the DH
959 parameters or (better, because faster) the ECDH curve.
960
961 If neither "SSL_dh_file" not "SSL_dh" is set a builtin DH
962 parameter with a length of 2048 bit is used to offer DH key
963 exchange by default. If you don't want this (e.g. disable DH key
964 exchange) explicitly set this or the "SSL_dh" parameter to undef.
965
966 SSL_dh
967 Like SSL_dh_file, but instead of giving a file you use a
968 preloaded or generated DH*.
969
970 SSL_ecdh_curve
971 If you want Elliptic Curve Diffie-Hellmann key exchange you need
972 to supply the OID or NID of a suitable curve (like 'prime256v1')
973 here. To create a server which provides forward secrecy you need
974 to either give the DH parameters or (better, because faster) the
975 ECDH curve.
976
977 This parameter defaults to 'prime256v1' (builtin of OpenSSL) to
978 offer ECDH key exchange by default. If you don't want this
979 explicitly set it to undef.
980
981 You can check if ECDH support is available by calling
982 "IO::Socket::SSL->can_ecdh".
983
984 SSL_verify_mode
985 This option sets the verification mode for the peer certificate.
986 You may combine SSL_VERIFY_PEER (verify_peer),
987 SSL_VERIFY_FAIL_IF_NO_PEER_CERT (fail verification if no peer
988 certificate exists; ignored for clients), SSL_VERIFY_CLIENT_ONCE
989 (verify client once; ignored for clients). See OpenSSL man page
990 for SSL_CTX_set_verify for more information.
991
992 The default is SSL_VERIFY_NONE for server (e.g. no check for
993 client certificate) and SSL_VERIFY_PEER for client (check server
994 certificate).
995
996 SSL_verify_callback
997 If you want to verify certificates yourself, you can pass a sub
998 reference along with this parameter to do so. When the callback
999 is called, it will be passed:
1000
1001 1. a true/false value that indicates what OpenSSL thinks of the
1002 certificate,
1003 2. a C-style memory address of the certificate store,
1004 3. a string containing the certificate's issuer attributes and
1005 owner attributes, and
1006 4. a string containing any errors encountered (0 if no errors).
1007 5. a C-style memory address of the peer's own certificate
1008 (convertible to PEM form with
1009 Net::SSLeay::PEM_get_string_X509()).
1010 6. The depth of the certificate in the chain. Depth 0 is the leaf
1011 certificate.
1012
1013 The function should return 1 or 0, depending on whether it thinks
1014 the certificate is valid or invalid. The default is to let
1015 OpenSSL do all of the busy work.
1016
1017 The callback will be called for each element in the certificate
1018 chain.
1019
1020 See the OpenSSL documentation for SSL_CTX_set_verify for more
1021 information.
1022
1023 SSL_verifycn_scheme
1024 The scheme is used to correctly verify the identity inside the
1025 certificate by using the hostname of the peer. See the
1026 information about the verification schemes in verify_hostname.
1027
1028 If you don't specify a scheme it will use 'default', but only
1029 complain loudly if the name verification fails instead of letting
1030 the whole certificate verification fail. THIS WILL CHANGE, e.g.
1031 it will let the certificate verification fail in the future if
1032 the hostname does not match the certificate !!!! To override the
1033 name used in verification use SSL_verifycn_name.
1034
1035 The scheme 'default' is a superset of the usual schemes, which
1036 will accept the hostname in common name and subjectAltName and
1037 allow wildcards everywhere. While using this scheme is way more
1038 secure than no name verification at all you better should use the
1039 scheme specific to your application protocol, e.g. 'http',
1040 'ftp'...
1041
1042 If you are really sure, that you don't want to verify the
1043 identity using the hostname you can use 'none' as a scheme. In
1044 this case you'd better have alternative forms of verification,
1045 like a certificate fingerprint or do a manual verification later
1046 by calling verify_hostname yourself.
1047
1048 SSL_verifycn_publicsuffix
1049 This option is used to specify the behavior when checking
1050 wildcards certificates for public suffixes, e.g. no wildcard
1051 certificates for *.com or *.co.uk should be accepted, while
1052 *.example.com or *.example.co.uk is ok.
1053
1054 If not specified it will simply use the builtin default of
1055 IO::Socket::SSL::PublicSuffix, you can create another object with
1056 from_string or from_file of this module.
1057
1058 To disable verification of public suffix set this option to ''.
1059
1060 SSL_verifycn_name
1061 Set the name which is used in verification of hostname. If
1062 SSL_verifycn_scheme is set and no SSL_verifycn_name is given it
1063 will try to use SSL_hostname or PeerHost and PeerAddr settings
1064 and fail if no name can be determined. If SSL_verifycn_scheme is
1065 not set it will use a default scheme and warn if it cannot
1066 determine a hostname, but it will not fail.
1067
1068 Using PeerHost or PeerAddr works only if you create the
1069 connection directly with "IO::Socket::SSL->new", if an
1070 IO::Socket::INET object is upgraded with start_SSL the name has
1071 to be given in SSL_verifycn_name or SSL_hostname.
1072
1073 SSL_check_crl
1074 If you want to verify that the peer certificate has not been
1075 revoked by the signing authority, set this value to true. OpenSSL
1076 will search for the CRL in your SSL_ca_path, or use the file
1077 specified by SSL_crl_file. See the Net::SSLeay documentation for
1078 more details. Note that this functionality appears to be broken
1079 with OpenSSL < v0.9.7b, so its use with lower versions will
1080 result in an error.
1081
1082 SSL_crl_file
1083 If you want to specify the CRL file to be used, set this value to
1084 the pathname to be used. This must be used in addition to
1085 setting SSL_check_crl.
1086
1087 SSL_ocsp_mode
1088 Defines how certificate revocation is done using OCSP (Online
1089 Status Revocation Protocol). The default is to send a request for
1090 OCSP stapling to the server and if the server sends an OCSP
1091 response back the result will be used.
1092
1093 Any other OCSP checking needs to be done manually with
1094 "ocsp_resolver".
1095
1096 The following flags can be combined with "|":
1097
1098 SSL_OCSP_NO_STAPLE
1099 Don't ask for OCSP stapling. This is the default if
1100 SSL_verify_mode is VERIFY_NONE.
1101
1102 SSL_OCSP_TRY_STAPLE
1103 Try OCSP stapling, but don't complain if it gets no
1104 stapled response back. This is the default if
1105 SSL_verify_mode is VERIFY_PEER (the default).
1106
1107 SSL_OCSP_MUST_STAPLE
1108 Consider it a hard error, if the server does not send a
1109 stapled OCSP response back. Most servers currently send
1110 no stapled OCSP response back.
1111
1112 SSL_OCSP_FAIL_HARD
1113 Fail hard on response errors, default is to fail soft
1114 like the browsers do. Soft errors mean, that the OCSP
1115 response is not usable, e.g. no response, error response,
1116 no valid signature etc. Certificate revocations inside a
1117 verified response are considered hard errors in any case.
1118
1119 Soft errors inside a stapled response are never
1120 considered hard, e.g. it is expected that in this case an
1121 OCSP request will be send to the responsible OCSP
1122 responder.
1123
1124 SSL_OCSP_FULL_CHAIN
1125 This will set up the "ocsp_resolver" so that all
1126 certificates from the peer chain will be checked,
1127 otherwise only the leaf certificate will be checked
1128 against revocation.
1129
1130 SSL_ocsp_staple_callback
1131 If this callback is defined, it will be called with the SSL
1132 object and the OCSP response handle obtained from the peer, e.g.
1133 "<$cb-"($ssl,$resp)>>. If the peer did not provide a stapled
1134 OCSP response the function will be called with "$resp=undef".
1135 Because the OCSP response handle is no longer valid after leaving
1136 this function it should not by copied or freed. If access to the
1137 response is necessary after leaving this function it can be
1138 serialized with "Net::SSLeay::i2d_OCSP_RESPONSE".
1139
1140 If no such callback is provided, it will use the default one,
1141 which verifies the response and uses it to check if the
1142 certificate(s) of the connection got revoked.
1143
1144 SSL_ocsp_cache
1145 With this option a cache can be given for caching OCSP responses,
1146 which could be shared between different SSL contexts. If not
1147 given a cache specific to the SSL context only will be used.
1148
1149 You can either create a new cache with
1150 "IO::Socket::SSL::OCSP_Cache->new([size])" or implement your own
1151 cache, which needs to have methods "put($key,\%entry)" and
1152 "get($key)" (returning "\%entry") where entry is the hash
1153 representation of the OCSP response with fields like
1154 "nextUpdate". The default implementation of the cache will
1155 consider responses valid as long as "nextUpdate" is less then the
1156 current time.
1157
1158 SSL_reuse_ctx
1159 If you have already set the above options for a previous instance
1160 of IO::Socket::SSL, then you can reuse the SSL context of that
1161 instance by passing it as the value for the SSL_reuse_ctx
1162 parameter. You may also create a new instance of the
1163 IO::Socket::SSL::SSL_Context class, using any context options
1164 that you desire without specifying connection options, and pass
1165 that here instead.
1166
1167 If you use this option, all other context-related options that
1168 you pass in the same call to new() will be ignored unless the
1169 context supplied was invalid. Note that, contrary to versions of
1170 IO::Socket::SSL below v0.90, a global SSL context will not be
1171 implicitly used unless you use the set_default_context()
1172 function.
1173
1174 SSL_create_ctx_callback
1175 With this callback you can make individual settings to the
1176 context after it got created and the default setup was done. The
1177 callback will be called with the CTX object from Net::SSLeay as
1178 the single argument.
1179
1180 Example for limiting the server session cache size:
1181
1182 SSL_create_ctx_callback => sub {
1183 my $ctx = shift;
1184 Net::SSLeay::CTX_sess_set_cache_size($ctx,128);
1185 }
1186
1187 SSL_session_cache_size
1188 If you make repeated connections to the same host/port and the
1189 SSL renegotiation time is an issue, you can turn on client-side
1190 session caching with this option by specifying a positive cache
1191 size. For successive connections, pass the SSL_reuse_ctx option
1192 to the new() calls (or use set_default_context()) to make use of
1193 the cached sessions. The session cache size refers to the number
1194 of unique host/port pairs that can be stored at one time; the
1195 oldest sessions in the cache will be removed if new ones are
1196 added.
1197
1198 This option does not effect the session cache a server has for
1199 it's clients, e.g. it does not affect SSL objects with SSL_server
1200 set.
1201
1202 SSL_session_cache
1203 Specifies session cache object which should be used instead of
1204 creating a new. Overrules SSL_session_cache_size. This option
1205 is useful if you want to reuse the cache, but not the rest of the
1206 context.
1207
1208 A session cache object can be created using
1209 "IO::Socket::SSL::Session_Cache->new( cachesize )".
1210
1211 Use set_default_session_cache() to set a global cache object.
1212
1213 SSL_session_key
1214 Specifies a key to use for lookups and inserts into client-side
1215 session cache. Per default ip:port of destination will be used,
1216 but sometimes you want to share the same session over multiple
1217 ports on the same server (like with FTPS).
1218
1219 SSL_session_id_context
1220 This gives an id for the servers session cache. It's necessary if
1221 you want clients to connect with a client certificate. If not
1222 given but SSL_verify_mode specifies the need for client
1223 certificate a context unique id will be picked.
1224
1225 SSL_error_trap
1226 When using the accept() or connect() methods, it may be the case
1227 that the actual socket connection works but the SSL negotiation
1228 fails, as in the case of an HTTP client connecting to an HTTPS
1229 server. Passing a subroutine ref attached to this parameter
1230 allows you to gain control of the orphaned socket instead of
1231 having it be closed forcibly. The subroutine, if called, will be
1232 passed two parameters: a reference to the socket on which the SSL
1233 negotiation failed and the full text of the error message.
1234
1235 SSL_npn_protocols
1236 If used on the server side it specifies list of protocols
1237 advertised by SSL server as an array ref, e.g.
1238 ['spdy/2','http1.1']. On the client side it specifies the
1239 protocols offered by the client for NPN as an array ref. See
1240 also method "next_proto_negotiated".
1241
1242 Next Protocol Negotiation (NPN) is available with Net::SSLeay
1243 1.46+ and openssl-1.0.1+. NPN is unavailable in TLSv1.3 protocol.
1244 To check support you might call "IO::Socket::SSL->can_npn()". If
1245 you use this option with an unsupported Net::SSLeay/OpenSSL it
1246 will throw an error.
1247
1248 SSL_alpn_protocols
1249 If used on the server side it specifies list of protocols
1250 supported by the SSL server as an array ref, e.g. ['http/2.0',
1251 'spdy/3.1','http/1.1']. On the client side it specifies the
1252 protocols advertised by the client for ALPN as an array ref. See
1253 also method "alpn_selected".
1254
1255 Application-Layer Protocol Negotiation (ALPN) is available with
1256 Net::SSLeay 1.56+ and openssl-1.0.2+. More details about the
1257 extension are in RFC7301. To check support you might call
1258 "IO::Socket::SSL->can_alpn()". If you use this option with an
1259 unsupported Net::SSLeay/OpenSSL it will throw an error.
1260
1261 Note that some client implementations may encounter problems if
1262 both NPN and ALPN are specified. Since ALPN is intended as a
1263 replacement for NPN, try providing ALPN protocols then fall back
1264 to NPN if that fails.
1265
1266 SSL_ticket_keycb => [$sub,$data] | $sub
1267 This is a callback used for stateless session reuse (Session
1268 Tickets, RFC 5077).
1269
1270 This callback will be called as "$sub->($data,[$key_name])" where
1271 $data is the argument given to SSL_ticket_keycb (or undef) and
1272 $key_name depends on the mode:
1273
1274 encrypt ticket
1275 If a ticket needs to be encrypted the callback will be
1276 called without $key_name. In this case it should return
1277 "($current_key,$current_key_name") where $current_key is
1278 the current key (32 byte random data) and
1279 $current_key_name the name associated with this key
1280 (exactly 16 byte). This $current_key_name will be
1281 incorporated into the ticket.
1282
1283 decrypt ticket
1284 If a ticket needs to be decrypted the callback will be
1285 called with $key_name as found in the ticket. It should
1286 return "($key,$current_key_name") where $key is the key
1287 associated with the given $key_name and $current_key_name
1288 the name associated with the currently active key. If
1289 $current_key_name is different from the given $key_name
1290 the callback will be called again to re-encrypt the
1291 ticket with the currently active key.
1292
1293 If no key can be found which matches the given $key_name
1294 then this function should return nothing (empty list).
1295
1296 This mechanism should be used to limit the life time for
1297 each key encrypting the ticket. Compromise of a ticket
1298 encryption key might lead to decryption of SSL sessions
1299 which used session tickets protected by this key.
1300
1301 Example:
1302
1303 Net::SSLeay::RAND_bytes(my $oldkey,32);
1304 Net::SSLeay::RAND_bytes(my $newkey,32);
1305 my $oldkey_name = pack("a16",'oldsecret');
1306 my $newkey_name = pack("a16",'newsecret');
1307
1308 my @keys = (
1309 [ $newkey_name, $newkey ], # current active key
1310 [ $oldkey_name, $oldkey ], # already expired
1311 );
1312
1313 my $keycb = [ sub {
1314 my ($mykeys,$name) = @_;
1315
1316 # return (current_key, current_key_name) if no name given
1317 return ($mykeys->[0][1],$mykeys->[0][0]) if ! $name;
1318
1319 # return (matching_key, current_key_name) if we find a key matching
1320 # the given name
1321 for(my $i = 0; $i<@$mykeys; $i++) {
1322 next if $name ne $mykeys->[$i][0];
1323 return ($mykeys->[$i][1],$mykeys->[0][0]);
1324 }
1325
1326 # no matching key found
1327 return;
1328 },\@keys ];
1329
1330 my $srv = IO::Socket::SSL->new(..., SSL_ticket_keycb => $keycb);
1331
1332 accept
1333 This behaves similar to the accept function of the underlying
1334 socket class, but additionally does the initial SSL handshake. But
1335 because the underlying socket class does return a blocking file
1336 handle even when accept is called on a non-blocking socket, the SSL
1337 handshake on the new file object will be done in a blocking way.
1338 Please see the section about non-blocking I/O for details. If you
1339 don't like this behavior you should do accept on the TCP socket and
1340 then upgrade it with "start_SSL" later.
1341
1342 connect(...)
1343 This behaves similar to the connect function but also does an SSL
1344 handshake. Because you cannot give SSL specific arguments to this
1345 function, you should better either use "new" to create a connect
1346 SSL socket or "start_SSL" to upgrade an established TCP socket to
1347 SSL.
1348
1349 close(...)
1350 Contrary to a close for a simple INET socket a close in SSL also
1351 mandates a proper shutdown of the SSL part. This is done by sending
1352 a close notify message by both peers.
1353
1354 A naive implementation would thus wait until it receives the close
1355 notify message from the peer - which conflicts with the commonly
1356 expected semantic that a close will not block. The default behavior
1357 is thus to only send a close notify but not wait for the close
1358 notify of the peer. If this is required "SSL_fast_shutdown" need to
1359 be explicitly set to false.
1360
1361 There are also cases where a SSL shutdown should not be done at
1362 all. This is true for example when forking to let a child deal with
1363 the socket and closing the socket in the parent process. A naive
1364 explicit "close" or an implicit close when destroying the socket in
1365 the parent would send a close notify to the peer which would make
1366 the SSL socket in the client process unusable. In this case an
1367 explicit "close" with "SSL_no_shutdown" set to true should be done
1368 in the parent process.
1369
1370 For more details and other arguments see "stop_SSL" which gets
1371 called from "close" to shutdown the SSL state of the socket.
1372
1373 sysread( BUF, LEN, [ OFFSET ] )
1374 This function behaves from the outside the same as sysread in other
1375 IO::Socket objects, e.g. it returns at most LEN bytes of data. But
1376 in reality it reads not only LEN bytes from the underlying socket,
1377 but at a single SSL frame. It then returns up to LEN bytes it
1378 decrypted from this SSL frame. If the frame contained more data
1379 than requested it will return only LEN data, buffer the rest and
1380 return it on further read calls. This means, that it might be
1381 possible to read data, even if the underlying socket is not
1382 readable, so using poll or select might not be sufficient.
1383
1384 sysread will only return data from a single SSL frame, e.g. either
1385 the pending data from the already buffered frame or it will read a
1386 frame from the underlying socket and return the decrypted data. It
1387 will not return data spanning several SSL frames in a single call.
1388
1389 Also, calls to sysread might fail, because it must first finish an
1390 SSL handshake.
1391
1392 To understand these behaviors is essential, if you write
1393 applications which use event loops and/or non-blocking sockets.
1394 Please read the specific sections in this documentation.
1395
1396 syswrite( BUF, [ LEN, [ OFFSET ]] )
1397 This functions behaves from the outside the same as syswrite in
1398 other IO::Socket objects, e.g. it will write at most LEN bytes to
1399 the socket, but there is no guarantee, that all LEN bytes are
1400 written. It will return the number of bytes written. Because it
1401 basically just calls SSL_write from OpenSSL syswrite will write at
1402 most a single SSL frame. This means, that no more than 16.384
1403 bytes, which is the maximum size of an SSL frame, will be written
1404 at once.
1405
1406 For non-blocking sockets SSL specific behavior applies. Pease read
1407 the specific section in this documentation.
1408
1409 peek( BUF, LEN, [ OFFSET ])
1410 This function has exactly the same syntax as sysread, and performs
1411 nearly the same task but will not advance the read position so that
1412 successive calls to peek() with the same arguments will return the
1413 same results. This function requires OpenSSL 0.9.6a or later to
1414 work.
1415
1416 pending()
1417 This function gives you the number of bytes available without
1418 reading from the underlying socket object. This function is
1419 essential if you work with event loops, please see the section
1420 about polling SSL sockets.
1421
1422 get_fingerprint([algo,certificate,pubkey])
1423 This methods returns the fingerprint of the given certificate in
1424 the form "algo$digest_hex", where "algo" is the used algorithm,
1425 default 'sha256'. If no certificate is given the peer certificate
1426 of the connection is used. If "pubkey" is true it will not return
1427 the fingerprint of the certificate but instead the fingerprint of
1428 the pubkey inside the certificate as "algo$pub$digest_hex".
1429
1430 get_fingerprint_bin([algo,certificate,pubkey])
1431 This methods returns the binary fingerprint of the given
1432 certificate by using the algorithm "algo", default 'sha256'. If no
1433 certificate is given the peer certificate of the connection is
1434 used. If "pubkey" is true it will not return the fingerprint of
1435 the certificate but instead the fingerprint of the pubkey inside
1436 the certificate.
1437
1438 get_cipher()
1439 Returns the string form of the cipher that the IO::Socket::SSL
1440 object is using.
1441
1442 get_sslversion()
1443 Returns the string representation of the SSL version of an
1444 established connection.
1445
1446 get_sslversion_int()
1447 Returns the integer representation of the SSL version of an
1448 established connection.
1449
1450 get_session_reused()
1451 This returns true if the session got reused and false otherwise.
1452 Note that with a reused session no certificates are send within the
1453 handshake and no ciphers are offered and thus functions which rely
1454 on this might not work.
1455
1456 dump_peer_certificate()
1457 Returns a parsable string with select fields from the peer SSL
1458 certificate. This method directly returns the result of the
1459 dump_peer_certificate() method of Net::SSLeay.
1460
1461 peer_certificate($field;[$refresh])
1462 If a peer certificate exists, this function can retrieve values
1463 from it. If no field is given the internal representation of
1464 certificate from Net::SSLeay is returned. If refresh is true it
1465 will not used a cached version, but check again in case the
1466 certificate of the connection has changed due to renegotiation.
1467
1468 The following fields can be queried:
1469
1470 authority (alias issuer)
1471 The certificate authority which signed the certificate.
1472
1473 owner (alias subject)
1474 The owner of the certificate.
1475
1476 commonName (alias cn) - only for Net::SSLeay version >=1.30
1477 The common name, usually the server name for SSL
1478 certificates.
1479
1480 subjectAltNames - only for Net::SSLeay version >=1.33
1481 Alternative names for the subject, usually different names
1482 for the same server, like example.org, example.com,
1483 *.example.com.
1484
1485 It returns a list of (typ,value) with typ GEN_DNS,
1486 GEN_IPADD etc (these constants are exported from
1487 IO::Socket::SSL). See
1488 Net::SSLeay::X509_get_subjectAltNames.
1489
1490 sock_certificate($field)
1491 This is similar to "peer_certificate" but will return the sites own
1492 certificate. The same arguments for $field can be used. If no
1493 $field is given the certificate handle from the underlying OpenSSL
1494 will be returned. This handle will only be valid as long as the SSL
1495 connection exists and if used afterwards it might result in strange
1496 crashes of the application.
1497
1498 peer_certificates
1499 This returns all the certificates send by the peer, e.g. first the
1500 peers own certificate and then the rest of the chain. You might use
1501 CERT_asHash from IO::Socket::SSL::Utils to inspect each of the
1502 certificates.
1503
1504 This function depends on a version of Net::SSLeay >= 1.58 .
1505
1506 get_servername
1507 This gives the name requested by the client if Server Name
1508 Indication (SNI) was used.
1509
1510 verify_hostname($hostname,$scheme,$publicsuffix)
1511 This verifies the given hostname against the peer certificate using
1512 the given scheme. Hostname is usually what you specify within the
1513 PeerAddr. See the "SSL_verifycn_publicsuffix" parameter for an
1514 explanation of suffix checking and for the possible values.
1515
1516 Verification of hostname against a certificate is different between
1517 various applications and RFCs. Some scheme allow wildcards for
1518 hostnames, some only in subjectAltNames, and even their different
1519 wildcard schemes are possible. RFC 6125 provides a good overview.
1520
1521 To ease the verification the following schemes are predefined (both
1522 protocol name and rfcXXXX name can be used):
1523
1524 rfc2818, xmpp (rfc3920), ftp (rfc4217)
1525 Extended wildcards in subjectAltNames and common name are
1526 possible, e.g. *.example.org or even www*.example.org. The
1527 common name will be only checked if no DNS names are given
1528 in subjectAltNames.
1529
1530 http (alias www)
1531 While name checking is defined in rfc2818 the current
1532 browsers usually accept also an IP address (w/o wildcards)
1533 within the common name as long as no subjectAltNames are
1534 defined. Thus this is rfc2818 extended with this feature.
1535
1536 smtp (rfc2595), imap, pop3, acap (rfc4642), netconf (rfc5538),
1537 syslog (rfc5425), snmp (rfc5953)
1538 Simple wildcards in subjectAltNames are possible, e.g.
1539 *.example.org matches www.example.org but not
1540 lala.www.example.org. If nothing from subjectAltNames match
1541 it checks against the common name, where wildcards are also
1542 allowed to match the full leftmost label.
1543
1544 ldap (rfc4513)
1545 Simple wildcards are allowed in subjectAltNames, but not in
1546 common name. Common name will be checked even if
1547 subjectAltNames exist.
1548
1549 sip (rfc5922)
1550 No wildcards are allowed and common name is checked even if
1551 subjectAltNames exist.
1552
1553 gist (rfc5971)
1554 Simple wildcards are allowed in subjectAltNames and common
1555 name, but common name will only be checked if their are no
1556 DNS names in subjectAltNames.
1557
1558 default This is a superset of all the rules and is automatically
1559 used if no scheme is given but a hostname (instead of IP)
1560 is known. Extended wildcards are allowed in
1561 subjectAltNames and common name and common name is checked
1562 always.
1563
1564 none No verification will be done. Actually is does not make
1565 any sense to call verify_hostname in this case.
1566
1567 The scheme can be given either by specifying the name for one of
1568 the above predefined schemes, or by using a hash which can have the
1569 following keys and values:
1570
1571 check_cn: 0|'always'|'when_only'
1572 Determines if the common name gets checked. If 'always' it
1573 will always be checked (like in ldap), if 'when_only' it
1574 will only be checked if no names are given in
1575 subjectAltNames (like in http), for any other values the
1576 common name will not be checked.
1577
1578 wildcards_in_alt: 0|'full_label'|'anywhere'
1579 Determines if and where wildcards in subjectAltNames are
1580 possible. If 'full_label' only cases like *.example.org
1581 will be possible (like in ldap), for 'anywhere'
1582 www*.example.org is possible too (like http), dangerous
1583 things like but www.*.org or even '*' will not be allowed.
1584 For compatibility with older versions 'leftmost' can be
1585 given instead of 'full_label'.
1586
1587 wildcards_in_cn: 0|'full_label'|'anywhere'
1588 Similar to wildcards_in_alt, but checks the common name.
1589 There is no predefined scheme which allows wildcards in
1590 common names.
1591
1592 ip_in_cn: 0|1|4|6
1593 Determines if an IP address is allowed in the common name
1594 (no wildcards are allowed). If set to 4 or 6 it only allows
1595 IPv4 or IPv6 addresses, any other true value allows both.
1596
1597 callback: \&coderef
1598 If you give a subroutine for verification it will be called
1599 with the arguments
1600 ($hostname,$commonName,@subjectAltNames), where hostname is
1601 the name given for verification, commonName is the result
1602 from peer_certificate('cn') and subjectAltNames is the
1603 result from peer_certificate('subjectAltNames').
1604
1605 All other arguments for the verification scheme will be
1606 ignored in this case.
1607
1608 next_proto_negotiated()
1609 This method returns the name of negotiated protocol - e.g.
1610 'http/1.1'. It works for both client and server side of SSL
1611 connection.
1612
1613 NPN support is available with Net::SSLeay 1.46+ and openssl-1.0.1+.
1614 To check support you might call "IO::Socket::SSL->can_npn()".
1615
1616 alpn_selected()
1617 Returns the protocol negotiated via ALPN as a string, e.g.
1618 'http/1.1', 'http/2.0' or 'spdy/3.1'.
1619
1620 ALPN support is available with Net::SSLeay 1.56+ and
1621 openssl-1.0.2+. To check support, use
1622 "IO::Socket::SSL->can_alpn()".
1623
1624 errstr()
1625 Returns the last error (in string form) that occurred. If you do
1626 not have a real object to perform this method on, call
1627 IO::Socket::SSL::errstr() instead.
1628
1629 For read and write errors on non-blocking sockets, this method may
1630 include the string "SSL wants a read first!" or "SSL wants a write
1631 first!" meaning that the other side is expecting to read from or
1632 write to the socket and wants to be satisfied before you get to do
1633 anything. But with version 0.98 you are better comparing the global
1634 exported variable $SSL_ERROR against the exported symbols
1635 SSL_WANT_READ and SSL_WANT_WRITE.
1636
1637 opened()
1638 This returns false if the socket could not be opened, 1 if the
1639 socket could be opened and the SSL handshake was successful done
1640 and -1 if the underlying IO::Handle is open, but the SSL handshake
1641 failed.
1642
1643 IO::Socket::SSL->start_SSL($socket, ... )
1644 This will convert a glob reference or a socket that you provide to
1645 an IO::Socket::SSL object. You may also pass parameters to
1646 specify context or connection options as with a call to new(). If
1647 you are using this function on an accept()ed socket, you must set
1648 the parameter "SSL_server" to 1, i.e.
1649 IO::Socket::SSL->start_SSL($socket, SSL_server => 1). If you have
1650 a class that inherits from IO::Socket::SSL and you want the $socket
1651 to be blessed into your own class instead, use
1652 MyClass->start_SSL($socket) to achieve the desired effect.
1653
1654 Note that if start_SSL() fails in SSL negotiation, $socket will
1655 remain blessed in its original class. For non-blocking sockets
1656 you better just upgrade the socket to IO::Socket::SSL and call
1657 accept_SSL or connect_SSL and the upgraded object. To just upgrade
1658 the socket set SSL_startHandshake explicitly to 0. If you call
1659 start_SSL w/o this parameter it will revert to blocking behavior
1660 for accept_SSL and connect_SSL.
1661
1662 If given the parameter "Timeout" it will stop if after the timeout
1663 no SSL connection was established. This parameter is only used for
1664 blocking sockets, if it is not given the default Timeout from the
1665 underlying IO::Socket will be used.
1666
1667 stop_SSL(...)
1668 This is the opposite of start_SSL(), connect_SSL() and
1669 accept_SSL(), e.g. it will shutdown the SSL connection and return
1670 to the class before start_SSL(). It gets the same arguments as
1671 close(), in fact close() calls stop_SSL() (but without downgrading
1672 the class).
1673
1674 Will return true if it succeeded and undef if failed. This might be
1675 the case for non-blocking sockets. In this case $! is set to
1676 EWOULDBLOCK and the ssl error to SSL_WANT_READ or SSL_WANT_WRITE.
1677 In this case the call should be retried again with the same
1678 arguments once the socket is ready.
1679
1680 For calling from "stop_SSL" "SSL_fast_shutdown" default to false,
1681 e.g. it waits for the close_notify of the peer. This is necessary
1682 in case you want to downgrade the socket and continue to use it as
1683 a plain socket.
1684
1685 After stop_SSL the socket can again be used to exchange plain data.
1686
1687 connect_SSL, accept_SSL
1688 These functions should be used to do the relevant handshake, if the
1689 socket got created with "new" or upgraded with "start_SSL" and
1690 "SSL_startHandshake" was set to false. They will return undef
1691 until the handshake succeeded or an error got thrown. As long as
1692 the function returns undef and $! is set to EWOULDBLOCK one could
1693 retry the call after the socket got readable (SSL_WANT_READ) or
1694 writeable (SSL_WANT_WRITE).
1695
1696 ocsp_resolver
1697 This will create an OCSP resolver object, which can be used to
1698 create OCSP requests for the certificates of the SSL connection.
1699 Which certificates are verified depends on the setting of
1700 "SSL_ocsp_mode": by default only the leaf certificate will be
1701 checked, but with SSL_OCSP_FULL_CHAIN all chain certificates will
1702 be checked.
1703
1704 Because to create an OCSP request the certificate and its issuer
1705 certificate need to be known it is not possible to check
1706 certificates when the trust chain is incomplete or if the
1707 certificate is self-signed.
1708
1709 The OCSP resolver gets created by calling "$ssl->ocsp_resolver" and
1710 provides the following methods:
1711
1712 hard_error
1713 This returns the hard error when checking the OCSP
1714 response. Hard errors are certificate revocations. With
1715 the "SSL_ocsp_mode" of SSL_OCSP_FAIL_HARD any soft error
1716 (e.g. failures to get signed information about the
1717 certificates) will be considered a hard error too.
1718
1719 The OCSP resolving will stop on the first hard error.
1720
1721 The method will return undef as long as no hard errors
1722 occurred and still requests to be resolved. If all requests
1723 got resolved and no hard errors occurred the method will
1724 return ''.
1725
1726 soft_error
1727 This returns the soft error(s) which occurred when asking
1728 the OCSP responders.
1729
1730 requests
1731 This will return a hash consisting of
1732 "(url,request)"-tuples, e.g. which contain the OCSP request
1733 string and the URL where it should be sent too. The usual
1734 way to send such a request is as HTTP POST request with a
1735 content-type of "application/ocsp-request" or as a GET
1736 request with the base64 and url-encoded request is added to
1737 the path of the URL.
1738
1739 After you've handled all these requests and added the
1740 response with "add_response" you should better call this
1741 method again to make sure, that no more requests are
1742 outstanding. IO::Socket::SSL will combine multiple OCSP
1743 requests for the same server inside a single request, but
1744 some server don't give a response to all these requests, so
1745 that one has to ask again with the remaining requests.
1746
1747 add_response($uri,$response)
1748 This method takes the HTTP body of the response which got
1749 received when sending the OCSP request to $uri. If no
1750 response was received or an error occurred one should
1751 either retry or consider $response as empty which will
1752 trigger a soft error.
1753
1754 The method returns the current value of "hard_error", e.g.
1755 a defined value when no more requests need to be done.
1756
1757 resolve_blocking(%args)
1758 This combines "requests" and "add_response" which
1759 HTTP::Tiny to do all necessary requests in a blocking way.
1760 %args will be given to HTTP::Tiny so that you can put proxy
1761 settings etc here. HTTP::Tiny will be called with
1762 "verify_SSL" of false, because the OCSP responses have
1763 their own signatures so no extra SSL verification is
1764 needed.
1765
1766 If you don't want to use blocking requests you need to roll
1767 your own user agent with "requests" and "add_response".
1768
1769 IO::Socket::SSL->new_from_fd($fd, [mode], %sslargs)
1770 This will convert a socket identified via a file descriptor into an
1771 SSL socket. Note that the argument list does not include a "MODE"
1772 argument; if you supply one, it will be thoughtfully ignored (for
1773 compatibility with IO::Socket::INET). Instead, a mode of '+<' is
1774 assumed, and the file descriptor passed must be able to handle such
1775 I/O because the initial SSL handshake requires bidirectional
1776 communication.
1777
1778 Internally the given $fd will be upgraded to a socket object using
1779 the "new_from_fd" method of the super class (IO::Socket::INET or
1780 similar) and then "start_SSL" will be called using the given
1781 %sslargs. If $fd is already an IO::Socket object you should better
1782 call "start_SSL" directly.
1783
1784 IO::Socket::SSL::default_ca([ path|dir| SSL_ca_file = ..., SSL_ca_path
1785 => ... ])>
1786 Determines or sets the default CA path. If existing path or dir or
1787 a hash is given it will set the default CA path to this value and
1788 never try to detect it automatically. If "undef" is given it will
1789 forget any stored defaults and continue with detection of system
1790 defaults. If no arguments are given it will start detection of
1791 system defaults, unless it has already stored user-set or
1792 previously detected values.
1793
1794 The detection of system defaults works similar to OpenSSL, e.g. it
1795 will check the directory specified in environment variable
1796 SSL_CERT_DIR or the path OPENSSLDIR/certs (SSLCERTS: on VMS) and
1797 the file specified in environment variable SSL_CERT_FILE or the
1798 path OPENSSLDIR/cert.pem (SSLCERTS:cert.pem on VMS). Contrary to
1799 OpenSSL it will check if the SSL_ca_path contains PEM files with
1800 the hash as file name and if the SSL_ca_file looks like PEM. If no
1801 usable system default can be found it will try to load and use
1802 Mozilla::CA and if not available give up detection. The result of
1803 the detection will be saved to speed up future calls.
1804
1805 The function returns the saved default CA as hash with SSL_ca_file
1806 and SSL_ca_path.
1807
1808 IO::Socket::SSL::set_default_context(...)
1809 You may use this to make IO::Socket::SSL automatically re-use a
1810 given context (unless specifically overridden in a call to new()).
1811 It accepts one argument, which should be either an IO::Socket::SSL
1812 object or an IO::Socket::SSL::SSL_Context object. See the
1813 SSL_reuse_ctx option of new() for more details. Note that this
1814 sets the default context globally, so use with caution (esp. in
1815 mod_perl scripts).
1816
1817 IO::Socket::SSL::set_default_session_cache(...)
1818 You may use this to make IO::Socket::SSL automatically re-use a
1819 given session cache (unless specifically overridden in a call to
1820 new()). It accepts one argument, which should be an
1821 IO::Socket::SSL::Session_Cache object or similar (e.g. something
1822 which implements get_session, add_session and maybe del_session
1823 like IO::Socket::SSL::Session_Cache does). See the
1824 SSL_session_cache option of new() for more details. Note that this
1825 sets the default cache globally, so use with caution.
1826
1827 IO::Socket::SSL::set_defaults(%args)
1828 With this function one can set defaults for all SSL_* parameter
1829 used for creation of the context, like the SSL_verify* parameter.
1830 Any SSL_* parameter can be given or the following short versions:
1831
1832 mode - SSL_verify_mode
1833 callback - SSL_verify_callback
1834 scheme - SSL_verifycn_scheme
1835 name - SSL_verifycn_name
1836 IO::Socket::SSL::set_client_defaults(%args)
1837 Similar to "set_defaults", but only sets the defaults for client
1838 mode.
1839
1840 IO::Socket::SSL::set_server_defaults(%args)
1841 Similar to "set_defaults", but only sets the defaults for server
1842 mode.
1843
1844 IO::Socket::SSL::set_args_filter_hack(\&code|'use_defaults')
1845 Sometimes one has to use code which uses unwanted or invalid
1846 arguments for SSL, typically disabling SSL verification or setting
1847 wrong ciphers or SSL versions. With this hack it is possible to
1848 override these settings and restore sanity. Example:
1849
1850 IO::Socket::SSL::set_args_filter_hack( sub {
1851 my ($is_server,$args) = @_;
1852 if ( ! $is_server ) {
1853 # client settings - enable verification with default CA
1854 # and fallback hostname verification etc
1855 delete @{$args}{qw(
1856 SSL_verify_mode
1857 SSL_ca_file
1858 SSL_ca_path
1859 SSL_verifycn_scheme
1860 SSL_version
1861 )};
1862 # and add some fingerprints for known certs which are signed by
1863 # unknown CAs or are self-signed
1864 $args->{SSL_fingerprint} = ...
1865 }
1866 });
1867
1868 With the short setting "set_args_filter_hack('use_defaults')" it
1869 will prefer the default settings in all cases. These default
1870 settings can be modified with "set_defaults", "set_client_defaults"
1871 and "set_server_defaults".
1872
1873 The following methods are unsupported (not to mention futile!) and
1874 IO::Socket::SSL will emit a large CROAK() if you are silly enough to
1875 use them:
1876
1877 truncate
1878 stat
1879 ungetc
1880 setbuf
1881 setvbuf
1882 fdopen
1883 send/recv
1884 Note that send() and recv() cannot be reliably trapped by a tied
1885 filehandle (such as that used by IO::Socket::SSL) and so may send
1886 unencrypted data over the socket. Object-oriented calls to these
1887 functions will fail, telling you to use the print/printf/syswrite
1888 and read/sysread families instead.
1889
1891 The following functions are deprecated and are only retained for
1892 compatibility:
1893
1894 context_init()
1895 use the SSL_reuse_ctx option if you want to re-use a context
1896
1897 socketToSSL() and socket_to_SSL()
1898 use IO::Socket::SSL->start_SSL() instead
1899
1900 kill_socket()
1901 use close() instead
1902
1903 get_peer_certificate()
1904 use the peer_certificate() function instead. Used to return
1905 X509_Certificate with methods subject_name and issuer_name. Now
1906 simply returns $self which has these methods (although deprecated).
1907
1908 issuer_name()
1909 use peer_certificate( 'issuer' ) instead
1910
1911 subject_name()
1912 use peer_certificate( 'subject' ) instead
1913
1915 See the 'example' directory, the tests in 't' and also the tools in
1916 'util'.
1917
1919 If you use IO::Socket::SSL together with threads you should load it
1920 (e.g. use or require) inside the main thread before creating any other
1921 threads which use it. This way it is much faster because it will be
1922 initialized only once. Also there are reports that it might crash the
1923 other way.
1924
1925 Creating an IO::Socket::SSL object in one thread and closing it in
1926 another thread will not work.
1927
1928 IO::Socket::SSL does not work together with
1929 Storable::fd_retrieve/fd_store. See BUGS file for more information and
1930 how to work around the problem.
1931
1932 Non-blocking and timeouts (which are based on non-blocking) are not
1933 supported on Win32, because the underlying IO::Socket::INET does not
1934 support non-blocking on this platform.
1935
1936 If you have a server and it looks like you have a memory leak you might
1937 check the size of your session cache. Default for Net::SSLeay seems to
1938 be 20480, see the example for SSL_create_ctx_callback for how to limit
1939 it.
1940
1941 TLS 1.3 support regarding session reuse is incomplete.
1942
1944 IO::Socket::INET, IO::Socket::INET6, IO::Socket::IP, Net::SSLeay.
1945
1947 Many thanks to all who added patches or reported bugs or helped
1948 IO::Socket::SSL another way. Please keep reporting bugs and help with
1949 patches, even if they just fix the documentation.
1950
1951 Special thanks to the team of Net::SSLeay for the good cooperation.
1952
1954 Steffen Ullrich, <sullr at cpan.org> is the current maintainer.
1955
1956 Peter Behroozi, <behrooz at fas.harvard.edu> (Note the lack of an "i"
1957 at the end of "behrooz")
1958
1959 Marko Asplund, <marko.asplund at kronodoc.fi>, was the original author
1960 of IO::Socket::SSL.
1961
1962 Patches incorporated from various people, see file Changes.
1963
1965 The original versions of this module are Copyright (C) 1999-2002 Marko
1966 Asplund.
1967
1968 The rewrite of this module is Copyright (C) 2002-2005 Peter Behroozi.
1969
1970 Versions 0.98 and newer are Copyright (C) 2006-2014 Steffen Ullrich.
1971
1972 This module is free software; you can redistribute it and/or modify it
1973 under the same terms as Perl itself.
1974
1976 Hey! The above document had some coding errors, which are explained
1977 below:
1978
1979 Around line 1490:
1980 '=item' outside of any '=over'
1981
1982
1983
1984perl v5.26.3 2019-05-14 IO::Socket::SSL(3)