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. Any colons inside the hex string will be ignored.
827
828 If you want to use the fingerprint of the pubkey inside the
829 certificate instead of the certificate use the syntax
830 'algo$pub$hex_fingerprint' instead. To get the fingerprint of an
831 established connection you can use "get_fingerprint".
832
833 It is also possible to skip "algo$", i.e. only specifiy the
834 fingerprint. In this case the likely algorithms will be
835 automatically detected based on the length of the digest string.
836
837 You can specify a list of fingerprints in case you have several
838 acceptable certificates. If a fingerprint matches the topmost
839 (i.e. leaf) certificate no additional validations can make the
840 verification fail.
841
842 SSL_cert_file | SSL_cert | SSL_key_file | SSL_key
843 If you create a server you usually need to specify a server
844 certificate which should be verified by the client. Same is true
845 for client certificates, which should be verified by the server.
846 The certificate can be given as a file with SSL_cert_file or as
847 an internal representation of an X509* object (like you get from
848 Net::SSLeay or IO::Socket::SSL::Utils::PEM_xxx2cert) with
849 SSL_cert. If given as a file it will automatically detect the
850 format. Supported file formats are PEM, DER and PKCS#12, where
851 PEM and PKCS#12 can contain the certificate and the chain to use,
852 while DER can only contain a single certificate.
853
854 If given as a list of X509* please note, that the all the chain
855 certificates (e.g. all except the first) will be "consumed" by
856 openssl and will be freed if the SSL context gets destroyed - so
857 you should never free them yourself. But the servers certificate
858 (e.g. the first) will not be consumed by openssl and thus must be
859 freed by the application.
860
861 For each certificate a key is need, which can either be given as
862 a file with SSL_key_file or as an internal representation of an
863 EVP_PKEY* object with SSL_key (like you get from Net::SSLeay or
864 IO::Socket::SSL::Utils::PEM_xxx2key). If a key was already given
865 within the PKCS#12 file specified by SSL_cert_file it will ignore
866 any SSL_key or SSL_key_file. If no SSL_key or SSL_key_file was
867 given it will try to use the PEM file given with SSL_cert_file
868 again, maybe it contains the key too.
869
870 If your SSL server should be able to use different certificates
871 on the same IP address, depending on the name given by SNI, you
872 can use a hash reference instead of a file with "<hostname ="
873 cert_file>>.
874
875 If your SSL server should be able to use both RSA and ECDSA
876 certificates for the same domain/IP a similar hash reference like
877 with SNI is given. The domain names used to specify the
878 additional certificates should be "hostname%whatever", i.e.
879 "hostname%ecc" or similar. This needs at least OpenSSL 1.0.2. To
880 let the server pick the certificate based on the clients cipher
881 preference "SSL_honor_cipher_order" should be set to false.
882
883 In case certs and keys are needed but not given it might fall
884 back to builtin defaults, see "Defaults for Cert, Key and CA".
885
886 Examples:
887
888 SSL_cert_file => 'mycert.pem',
889 SSL_key_file => 'mykey.pem',
890
891 SSL_cert_file => {
892 "foo.example.org" => 'foo-cert.pem',
893 "foo.example.org%ecc" => 'foo-ecc-cert.pem',
894 "bar.example.org" => 'bar-cert.pem',
895 # used when nothing matches or client does not support SNI
896 '' => 'default-cert.pem',
897 '%ecc' => 'default-ecc-cert.pem',
898 },
899 SSL_key_file => {
900 "foo.example.org" => 'foo-key.pem',
901 "foo.example.org%ecc" => 'foo-ecc-key.pem',
902 "bar.example.org" => 'bar-key.pem',
903 # used when nothing matches or client does not support SNI
904 '' => 'default-key.pem',
905 '%ecc' => 'default-ecc-key.pem',
906 }
907
908 SSL_passwd_cb
909 If your private key is encrypted, you might not want the default
910 password prompt from Net::SSLeay. This option takes a reference
911 to a subroutine that should return the password required to
912 decrypt your private key.
913
914 SSL_use_cert
915 If this is true, it forces IO::Socket::SSL to use a certificate
916 and key, even if you are setting up an SSL client. If this is
917 set to 0 (the default), then you will only need a certificate and
918 key if you are setting up a server.
919
920 SSL_use_cert will implicitly be set if SSL_server is set. For
921 convenience it is also set if it was not given but a cert was
922 given for use (SSL_cert_file or similar).
923
924 SSL_version
925 Sets the version of the SSL protocol used to transmit data.
926 'SSLv23' uses a handshake compatible with SSL2.0, SSL3.0 and
927 TLS1.x, while 'SSLv2', 'SSLv3', 'TLSv1', 'TLSv1_1', 'TLSv1_2', or
928 'TLSv1_3' restrict handshake and protocol to the specified
929 version. All values are case-insensitive. Instead of 'TLSv1_1',
930 'TLSv1_2', and 'TLSv1_3' one can also use 'TLSv11', 'TLSv12', and
931 'TLSv13'. Support for 'TLSv1_1', 'TLSv1_2', and 'TLSv1_3'
932 requires recent versions of Net::SSLeay and openssl. The default
933 SSL_version is defined by the underlying cryptographic library.
934
935 Independent from the handshake format you can limit to set of
936 accepted SSL versions by adding !version separated by ':'.
937
938 For example, 'SSLv23:!SSLv3:!SSLv2' means that the handshake
939 format is compatible to SSL2.0 and higher, but that the
940 successful handshake is limited to TLS1.0 and higher, that is no
941 SSL2.0 or SSL3.0 because both of these versions have serious
942 security issues and should not be used anymore. You can also use
943 !TLSv1_1 and !TLSv1_2 to disable TLS versions 1.1 and 1.2 while
944 still allowing TLS version 1.0.
945
946 Setting the version instead to 'TLSv1' might break interaction
947 with older clients, which need and SSL2.0 compatible handshake.
948 On the other side some clients just close the connection when
949 they receive a TLS version 1.1 request. In this case setting the
950 version to 'SSLv23:!SSLv2:!SSLv3:!TLSv1_1:!TLSv1_2' might help.
951
952 SSL_cipher_list
953 If this option is set the cipher list for the connection will be
954 set to the given value, e.g. something like
955 'ALL:!LOW:!EXP:!aNULL'. Look into the OpenSSL documentation
956 (<http://www.openssl.org/docs/apps/ciphers.html#CIPHER_STRINGS>)
957 for more details.
958
959 Unless you fail to contact your peer because of no shared ciphers
960 it is recommended to leave this option at the default setting,
961 which honors the system-wide PROFILE=SYSTEM cipher list.
962
963 In case different cipher lists are needed for different SNI hosts
964 a hash can be given with the host as key and the cipher suite as
965 value, similar to SSL_cert*.
966
967 SSL_honor_cipher_order
968 If this option is true the cipher order the server specified is
969 used instead of the order proposed by the client. This option
970 defaults to true to make use of our secure cipher list setting.
971
972 SSL_dh_file
973 To create a server which provides forward secrecy you need to
974 either give the DH parameters or (better, because faster) the
975 ECDH curve. This setting cares about DH parameters.
976
977 To support non-elliptic Diffie-Hellman key exchange a suitable
978 file needs to be given here or the SSL_dh should be used with a
979 appropriate value. See dhparam command in openssl for more
980 information.
981
982 If neither "SSL_dh_file" nor "SSL_dh" are set a builtin DH
983 parameter with a length of 2048 bit is used to offer DH key
984 exchange by default. If you don't want this (e.g. disable DH key
985 exchange) explicitly set this or the "SSL_dh" parameter to undef.
986
987 SSL_dh
988 Like SSL_dh_file, but instead of giving a file you use a
989 preloaded or generated DH*.
990
991 SSL_ecdh_curve
992 To create a server which provides forward secrecy you need to
993 either give the DH parameters or (better, because faster) the
994 ECDH curve. This setting cares about the ECDH curve(s).
995
996 To support Elliptic Curve Diffie-Hellmann key exchange the OID or
997 NID of at least one suitable curve needs to be provided here.
998
999 With OpenSSL 1.1.0+ this parameter defaults to "auto", which
1000 means that it lets OpenSSL pick the best settings. If support for
1001 CTX_set_ecdh_auto is implemented in Net::SSLeay (needs at least
1002 version 1.86) it will use this to implement the same default.
1003 Otherwise it will default to "prime256v1" (builtin of OpenSSL) in
1004 order to offer ECDH key exchange by default.
1005
1006 If setting groups or curves is supported by Net::SSLeay (needs at
1007 least version 1.86) then multiple curves can be given here in the
1008 order of the preference, i.e. "P-521:P-384:P-256". When used at
1009 the client side this will include the supported curves as
1010 extension in the TLS handshake.
1011
1012 If you don't want to have ECDH key exchange this could be set to
1013 undef or set "SSL_ciphers" to exclude all of these ciphers.
1014
1015 You can check if ECDH support is available by calling
1016 "IO::Socket::SSL->can_ecdh".
1017
1018 SSL_verify_mode
1019 This option sets the verification mode for the peer certificate.
1020 You may combine SSL_VERIFY_PEER (verify_peer),
1021 SSL_VERIFY_FAIL_IF_NO_PEER_CERT (fail verification if no peer
1022 certificate exists; ignored for clients), SSL_VERIFY_CLIENT_ONCE
1023 (verify client once; ignored for clients). See OpenSSL man page
1024 for SSL_CTX_set_verify for more information.
1025
1026 The default is SSL_VERIFY_NONE for server (e.g. no check for
1027 client certificate) and SSL_VERIFY_PEER for client (check server
1028 certificate).
1029
1030 SSL_verify_callback
1031 If you want to verify certificates yourself, you can pass a sub
1032 reference along with this parameter to do so. When the callback
1033 is called, it will be passed:
1034
1035 1. a true/false value that indicates what OpenSSL thinks of the
1036 certificate,
1037 2. a C-style memory address of the certificate store,
1038 3. a string containing the certificate's issuer attributes and
1039 owner attributes, and
1040 4. a string containing any errors encountered (0 if no errors).
1041 5. a C-style memory address of the peer's own certificate
1042 (convertible to PEM form with
1043 Net::SSLeay::PEM_get_string_X509()).
1044 6. The depth of the certificate in the chain. Depth 0 is the leaf
1045 certificate.
1046
1047 The function should return 1 or 0, depending on whether it thinks
1048 the certificate is valid or invalid. The default is to let
1049 OpenSSL do all of the busy work.
1050
1051 The callback will be called for each element in the certificate
1052 chain.
1053
1054 See the OpenSSL documentation for SSL_CTX_set_verify for more
1055 information.
1056
1057 SSL_verifycn_scheme
1058 The scheme is used to correctly verify the identity inside the
1059 certificate by using the hostname of the peer. See the
1060 information about the verification schemes in verify_hostname.
1061
1062 If you don't specify a scheme it will use 'default', but only
1063 complain loudly if the name verification fails instead of letting
1064 the whole certificate verification fail. THIS WILL CHANGE, e.g.
1065 it will let the certificate verification fail in the future if
1066 the hostname does not match the certificate !!!! To override the
1067 name used in verification use SSL_verifycn_name.
1068
1069 The scheme 'default' is a superset of the usual schemes, which
1070 will accept the hostname in common name and subjectAltName and
1071 allow wildcards everywhere. While using this scheme is way more
1072 secure than no name verification at all you better should use the
1073 scheme specific to your application protocol, e.g. 'http',
1074 'ftp'...
1075
1076 If you are really sure, that you don't want to verify the
1077 identity using the hostname you can use 'none' as a scheme. In
1078 this case you'd better have alternative forms of verification,
1079 like a certificate fingerprint or do a manual verification later
1080 by calling verify_hostname yourself.
1081
1082 SSL_verifycn_publicsuffix
1083 This option is used to specify the behavior when checking
1084 wildcards certificates for public suffixes, e.g. no wildcard
1085 certificates for *.com or *.co.uk should be accepted, while
1086 *.example.com or *.example.co.uk is ok.
1087
1088 If not specified it will simply use the builtin default of
1089 IO::Socket::SSL::PublicSuffix, you can create another object with
1090 from_string or from_file of this module.
1091
1092 To disable verification of public suffix set this option to ''.
1093
1094 SSL_verifycn_name
1095 Set the name which is used in verification of hostname. If
1096 SSL_verifycn_scheme is set and no SSL_verifycn_name is given it
1097 will try to use SSL_hostname or PeerHost and PeerAddr settings
1098 and fail if no name can be determined. If SSL_verifycn_scheme is
1099 not set it will use a default scheme and warn if it cannot
1100 determine a hostname, but it will not fail.
1101
1102 Using PeerHost or PeerAddr works only if you create the
1103 connection directly with "IO::Socket::SSL->new", if an
1104 IO::Socket::INET object is upgraded with start_SSL the name has
1105 to be given in SSL_verifycn_name or SSL_hostname.
1106
1107 SSL_check_crl
1108 If you want to verify that the peer certificate has not been
1109 revoked by the signing authority, set this value to true. OpenSSL
1110 will search for the CRL in your SSL_ca_path, or use the file
1111 specified by SSL_crl_file. See the Net::SSLeay documentation for
1112 more details. Note that this functionality appears to be broken
1113 with OpenSSL < v0.9.7b, so its use with lower versions will
1114 result in an error.
1115
1116 SSL_crl_file
1117 If you want to specify the CRL file to be used, set this value to
1118 the pathname to be used. This must be used in addition to
1119 setting SSL_check_crl.
1120
1121 SSL_ocsp_mode
1122 Defines how certificate revocation is done using OCSP (Online
1123 Status Revocation Protocol). The default is to send a request for
1124 OCSP stapling to the server and if the server sends an OCSP
1125 response back the result will be used.
1126
1127 Any other OCSP checking needs to be done manually with
1128 "ocsp_resolver".
1129
1130 The following flags can be combined with "|":
1131
1132 SSL_OCSP_NO_STAPLE
1133 Don't ask for OCSP stapling. This is the default if
1134 SSL_verify_mode is VERIFY_NONE.
1135
1136 SSL_OCSP_TRY_STAPLE
1137 Try OCSP stapling, but don't complain if it gets no
1138 stapled response back. This is the default if
1139 SSL_verify_mode is VERIFY_PEER (the default).
1140
1141 SSL_OCSP_MUST_STAPLE
1142 Consider it a hard error, if the server does not send a
1143 stapled OCSP response back. Most servers currently send
1144 no stapled OCSP response back.
1145
1146 SSL_OCSP_FAIL_HARD
1147 Fail hard on response errors, default is to fail soft
1148 like the browsers do. Soft errors mean, that the OCSP
1149 response is not usable, e.g. no response, error response,
1150 no valid signature etc. Certificate revocations inside a
1151 verified response are considered hard errors in any case.
1152
1153 Soft errors inside a stapled response are never
1154 considered hard, e.g. it is expected that in this case an
1155 OCSP request will be send to the responsible OCSP
1156 responder.
1157
1158 SSL_OCSP_FULL_CHAIN
1159 This will set up the "ocsp_resolver" so that all
1160 certificates from the peer chain will be checked,
1161 otherwise only the leaf certificate will be checked
1162 against revocation.
1163
1164 SSL_ocsp_staple_callback
1165 If this callback is defined, it will be called with the SSL
1166 object and the OCSP response handle obtained from the peer, e.g.
1167 "<$cb-"($ssl,$resp)>>. If the peer did not provide a stapled
1168 OCSP response the function will be called with "$resp=undef".
1169 Because the OCSP response handle is no longer valid after leaving
1170 this function it should not by copied or freed. If access to the
1171 response is necessary after leaving this function it can be
1172 serialized with "Net::SSLeay::i2d_OCSP_RESPONSE".
1173
1174 If no such callback is provided, it will use the default one,
1175 which verifies the response and uses it to check if the
1176 certificate(s) of the connection got revoked.
1177
1178 SSL_ocsp_cache
1179 With this option a cache can be given for caching OCSP responses,
1180 which could be shared between different SSL contexts. If not
1181 given a cache specific to the SSL context only will be used.
1182
1183 You can either create a new cache with
1184 "IO::Socket::SSL::OCSP_Cache->new([size])" or implement your own
1185 cache, which needs to have methods "put($key,\%entry)" and
1186 "get($key)" (returning "\%entry") where entry is the hash
1187 representation of the OCSP response with fields like
1188 "nextUpdate". The default implementation of the cache will
1189 consider responses valid as long as "nextUpdate" is less then the
1190 current time.
1191
1192 SSL_reuse_ctx
1193 If you have already set the above options for a previous instance
1194 of IO::Socket::SSL, then you can reuse the SSL context of that
1195 instance by passing it as the value for the SSL_reuse_ctx
1196 parameter. You may also create a new instance of the
1197 IO::Socket::SSL::SSL_Context class, using any context options
1198 that you desire without specifying connection options, and pass
1199 that here instead.
1200
1201 If you use this option, all other context-related options that
1202 you pass in the same call to new() will be ignored unless the
1203 context supplied was invalid. Note that, contrary to versions of
1204 IO::Socket::SSL below v0.90, a global SSL context will not be
1205 implicitly used unless you use the set_default_context()
1206 function.
1207
1208 SSL_create_ctx_callback
1209 With this callback you can make individual settings to the
1210 context after it got created and the default setup was done. The
1211 callback will be called with the CTX object from Net::SSLeay as
1212 the single argument.
1213
1214 Example for limiting the server session cache size:
1215
1216 SSL_create_ctx_callback => sub {
1217 my $ctx = shift;
1218 Net::SSLeay::CTX_sess_set_cache_size($ctx,128);
1219 }
1220
1221 SSL_session_cache_size
1222 If you make repeated connections to the same host/port and the
1223 SSL renegotiation time is an issue, you can turn on client-side
1224 session caching with this option by specifying a positive cache
1225 size. For successive connections, pass the SSL_reuse_ctx option
1226 to the new() calls (or use set_default_context()) to make use of
1227 the cached sessions. The session cache size refers to the number
1228 of unique host/port pairs that can be stored at one time; the
1229 oldest sessions in the cache will be removed if new ones are
1230 added.
1231
1232 This option does not effect the session cache a server has for
1233 it's clients, e.g. it does not affect SSL objects with SSL_server
1234 set.
1235
1236 Note that session caching with TLS 1.3 needs at least Net::SSLeay
1237 1.86.
1238
1239 SSL_session_cache
1240 Specifies session cache object which should be used instead of
1241 creating a new. Overrules SSL_session_cache_size. This option
1242 is useful if you want to reuse the cache, but not the rest of the
1243 context.
1244
1245 A session cache object can be created using
1246 "IO::Socket::SSL::Session_Cache->new( cachesize )".
1247
1248 Use set_default_session_cache() to set a global cache object.
1249
1250 SSL_session_key
1251 Specifies a key to use for lookups and inserts into client-side
1252 session cache. Per default ip:port of destination will be used,
1253 but sometimes you want to share the same session over multiple
1254 ports on the same server (like with FTPS).
1255
1256 SSL_session_id_context
1257 This gives an id for the servers session cache. It's necessary if
1258 you want clients to connect with a client certificate. If not
1259 given but SSL_verify_mode specifies the need for client
1260 certificate a context unique id will be picked.
1261
1262 SSL_error_trap
1263 When using the accept() or connect() methods, it may be the case
1264 that the actual socket connection works but the SSL negotiation
1265 fails, as in the case of an HTTP client connecting to an HTTPS
1266 server. Passing a subroutine ref attached to this parameter
1267 allows you to gain control of the orphaned socket instead of
1268 having it be closed forcibly. The subroutine, if called, will be
1269 passed two parameters: a reference to the socket on which the SSL
1270 negotiation failed and the full text of the error message.
1271
1272 SSL_npn_protocols
1273 If used on the server side it specifies list of protocols
1274 advertised by SSL server as an array ref, e.g.
1275 ['spdy/2','http1.1']. On the client side it specifies the
1276 protocols offered by the client for NPN as an array ref. See
1277 also method "next_proto_negotiated".
1278
1279 Next Protocol Negotiation (NPN) is available with Net::SSLeay
1280 1.46+ and openssl-1.0.1+. NPN is unavailable in TLSv1.3 protocol.
1281 To check support you might call "IO::Socket::SSL->can_npn()". If
1282 you use this option with an unsupported Net::SSLeay/OpenSSL it
1283 will throw an error.
1284
1285 SSL_alpn_protocols
1286 If used on the server side it specifies list of protocols
1287 supported by the SSL server as an array ref, e.g. ['http/2.0',
1288 'spdy/3.1','http/1.1']. On the client side it specifies the
1289 protocols advertised by the client for ALPN as an array ref. See
1290 also method "alpn_selected".
1291
1292 Application-Layer Protocol Negotiation (ALPN) is available with
1293 Net::SSLeay 1.56+ and openssl-1.0.2+. More details about the
1294 extension are in RFC7301. To check support you might call
1295 "IO::Socket::SSL->can_alpn()". If you use this option with an
1296 unsupported Net::SSLeay/OpenSSL it will throw an error.
1297
1298 Note that some client implementations may encounter problems if
1299 both NPN and ALPN are specified. Since ALPN is intended as a
1300 replacement for NPN, try providing ALPN protocols then fall back
1301 to NPN if that fails.
1302
1303 SSL_ticket_keycb => [$sub,$data] | $sub
1304 This is a callback used for stateless session reuse (Session
1305 Tickets, RFC 5077).
1306
1307 This callback will be called as "$sub->($data,[$key_name])" where
1308 $data is the argument given to SSL_ticket_keycb (or undef) and
1309 $key_name depends on the mode:
1310
1311 encrypt ticket
1312 If a ticket needs to be encrypted the callback will be
1313 called without $key_name. In this case it should return
1314 "($current_key,$current_key_name") where $current_key is
1315 the current key (32 byte random data) and
1316 $current_key_name the name associated with this key
1317 (exactly 16 byte). This $current_key_name will be
1318 incorporated into the ticket.
1319
1320 decrypt ticket
1321 If a ticket needs to be decrypted the callback will be
1322 called with $key_name as found in the ticket. It should
1323 return "($key,$current_key_name") where $key is the key
1324 associated with the given $key_name and $current_key_name
1325 the name associated with the currently active key. If
1326 $current_key_name is different from the given $key_name
1327 the callback will be called again to re-encrypt the
1328 ticket with the currently active key.
1329
1330 If no key can be found which matches the given $key_name
1331 then this function should return nothing (empty list).
1332
1333 This mechanism should be used to limit the life time for
1334 each key encrypting the ticket. Compromise of a ticket
1335 encryption key might lead to decryption of SSL sessions
1336 which used session tickets protected by this key.
1337
1338 Example:
1339
1340 Net::SSLeay::RAND_bytes(my $oldkey,32);
1341 Net::SSLeay::RAND_bytes(my $newkey,32);
1342 my $oldkey_name = pack("a16",'oldsecret');
1343 my $newkey_name = pack("a16",'newsecret');
1344
1345 my @keys = (
1346 [ $newkey_name, $newkey ], # current active key
1347 [ $oldkey_name, $oldkey ], # already expired
1348 );
1349
1350 my $keycb = [ sub {
1351 my ($mykeys,$name) = @_;
1352
1353 # return (current_key, current_key_name) if no name given
1354 return ($mykeys->[0][1],$mykeys->[0][0]) if ! $name;
1355
1356 # return (matching_key, current_key_name) if we find a key matching
1357 # the given name
1358 for(my $i = 0; $i<@$mykeys; $i++) {
1359 next if $name ne $mykeys->[$i][0];
1360 return ($mykeys->[$i][1],$mykeys->[0][0]);
1361 }
1362
1363 # no matching key found
1364 return;
1365 },\@keys ];
1366
1367 my $srv = IO::Socket::SSL->new(..., SSL_ticket_keycb => $keycb);
1368
1369 accept
1370 This behaves similar to the accept function of the underlying
1371 socket class, but additionally does the initial SSL handshake. But
1372 because the underlying socket class does return a blocking file
1373 handle even when accept is called on a non-blocking socket, the SSL
1374 handshake on the new file object will be done in a blocking way.
1375 Please see the section about non-blocking I/O for details. If you
1376 don't like this behavior you should do accept on the TCP socket and
1377 then upgrade it with "start_SSL" later.
1378
1379 connect(...)
1380 This behaves similar to the connect function but also does an SSL
1381 handshake. Because you cannot give SSL specific arguments to this
1382 function, you should better either use "new" to create a connect
1383 SSL socket or "start_SSL" to upgrade an established TCP socket to
1384 SSL.
1385
1386 close(...)
1387 Contrary to a close for a simple INET socket a close in SSL also
1388 mandates a proper shutdown of the SSL part. This is done by sending
1389 a close notify message by both peers.
1390
1391 A naive implementation would thus wait until it receives the close
1392 notify message from the peer - which conflicts with the commonly
1393 expected semantic that a close will not block. The default behavior
1394 is thus to only send a close notify but not wait for the close
1395 notify of the peer. If this is required "SSL_fast_shutdown" need to
1396 be explicitly set to false.
1397
1398 There are also cases where a SSL shutdown should not be done at
1399 all. This is true for example when forking to let a child deal with
1400 the socket and closing the socket in the parent process. A naive
1401 explicit "close" or an implicit close when destroying the socket in
1402 the parent would send a close notify to the peer which would make
1403 the SSL socket in the client process unusable. In this case an
1404 explicit "close" with "SSL_no_shutdown" set to true should be done
1405 in the parent process.
1406
1407 For more details and other arguments see "stop_SSL" which gets
1408 called from "close" to shutdown the SSL state of the socket.
1409
1410 sysread( BUF, LEN, [ OFFSET ] )
1411 This function behaves from the outside the same as sysread in other
1412 IO::Socket objects, e.g. it returns at most LEN bytes of data. But
1413 in reality it reads not only LEN bytes from the underlying socket,
1414 but at a single SSL frame. It then returns up to LEN bytes it
1415 decrypted from this SSL frame. If the frame contained more data
1416 than requested it will return only LEN data, buffer the rest and
1417 return it on further read calls. This means, that it might be
1418 possible to read data, even if the underlying socket is not
1419 readable, so using poll or select might not be sufficient.
1420
1421 sysread will only return data from a single SSL frame, e.g. either
1422 the pending data from the already buffered frame or it will read a
1423 frame from the underlying socket and return the decrypted data. It
1424 will not return data spanning several SSL frames in a single call.
1425
1426 Also, calls to sysread might fail, because it must first finish an
1427 SSL handshake.
1428
1429 To understand these behaviors is essential, if you write
1430 applications which use event loops and/or non-blocking sockets.
1431 Please read the specific sections in this documentation.
1432
1433 syswrite( BUF, [ LEN, [ OFFSET ]] )
1434 This functions behaves from the outside the same as syswrite in
1435 other IO::Socket objects, e.g. it will write at most LEN bytes to
1436 the socket, but there is no guarantee, that all LEN bytes are
1437 written. It will return the number of bytes written. Because it
1438 basically just calls SSL_write from OpenSSL syswrite will write at
1439 most a single SSL frame. This means, that no more than 16.384
1440 bytes, which is the maximum size of an SSL frame, will be written
1441 at once.
1442
1443 For non-blocking sockets SSL specific behavior applies. Pease read
1444 the specific section in this documentation.
1445
1446 peek( BUF, LEN, [ OFFSET ])
1447 This function has exactly the same syntax as sysread, and performs
1448 nearly the same task but will not advance the read position so that
1449 successive calls to peek() with the same arguments will return the
1450 same results. This function requires OpenSSL 0.9.6a or later to
1451 work.
1452
1453 pending()
1454 This function gives you the number of bytes available without
1455 reading from the underlying socket object. This function is
1456 essential if you work with event loops, please see the section
1457 about polling SSL sockets.
1458
1459 get_fingerprint([algo,certificate,pubkey])
1460 This methods returns the fingerprint of the given certificate in
1461 the form "algo$digest_hex", where "algo" is the used algorithm,
1462 default 'sha256'. If no certificate is given the peer certificate
1463 of the connection is used. If "pubkey" is true it will not return
1464 the fingerprint of the certificate but instead the fingerprint of
1465 the pubkey inside the certificate as "algo$pub$digest_hex".
1466
1467 get_fingerprint_bin([algo,certificate,pubkey])
1468 This methods returns the binary fingerprint of the given
1469 certificate by using the algorithm "algo", default 'sha256'. If no
1470 certificate is given the peer certificate of the connection is
1471 used. If "pubkey" is true it will not return the fingerprint of
1472 the certificate but instead the fingerprint of the pubkey inside
1473 the certificate.
1474
1475 get_cipher()
1476 Returns the string form of the cipher that the IO::Socket::SSL
1477 object is using.
1478
1479 get_sslversion()
1480 Returns the string representation of the SSL version of an
1481 established connection.
1482
1483 get_sslversion_int()
1484 Returns the integer representation of the SSL version of an
1485 established connection.
1486
1487 get_session_reused()
1488 This returns true if the session got reused and false otherwise.
1489 Note that with a reused session no certificates are send within the
1490 handshake and no ciphers are offered and thus functions which rely
1491 on this might not work.
1492
1493 dump_peer_certificate()
1494 Returns a parsable string with select fields from the peer SSL
1495 certificate. This method directly returns the result of the
1496 dump_peer_certificate() method of Net::SSLeay.
1497
1498 peer_certificate($field;[$refresh])
1499 If a peer certificate exists, this function can retrieve values
1500 from it. If no field is given the internal representation of
1501 certificate from Net::SSLeay is returned. If refresh is true it
1502 will not used a cached version, but check again in case the
1503 certificate of the connection has changed due to renegotiation.
1504
1505 The following fields can be queried:
1506
1507 authority (alias issuer)
1508 The certificate authority which signed the certificate.
1509
1510 owner (alias subject)
1511 The owner of the certificate.
1512
1513 commonName (alias cn) - only for Net::SSLeay version >=1.30
1514 The common name, usually the server name for SSL
1515 certificates.
1516
1517 subjectAltNames - only for Net::SSLeay version >=1.33
1518 Alternative names for the subject, usually different names
1519 for the same server, like example.org, example.com,
1520 *.example.com.
1521
1522 It returns a list of (typ,value) with typ GEN_DNS,
1523 GEN_IPADD etc (these constants are exported from
1524 IO::Socket::SSL). See
1525 Net::SSLeay::X509_get_subjectAltNames.
1526
1527 sock_certificate($field)
1528 This is similar to "peer_certificate" but will return the sites own
1529 certificate. The same arguments for $field can be used. If no
1530 $field is given the certificate handle from the underlying OpenSSL
1531 will be returned. This handle will only be valid as long as the SSL
1532 connection exists and if used afterwards it might result in strange
1533 crashes of the application.
1534
1535 peer_certificates
1536 This returns all the certificates send by the peer, e.g. first the
1537 peers own certificate and then the rest of the chain. You might use
1538 CERT_asHash from IO::Socket::SSL::Utils to inspect each of the
1539 certificates.
1540
1541 This function depends on a version of Net::SSLeay >= 1.58 .
1542
1543 get_servername
1544 This gives the name requested by the client if Server Name
1545 Indication (SNI) was used.
1546
1547 verify_hostname($hostname,$scheme,$publicsuffix)
1548 This verifies the given hostname against the peer certificate using
1549 the given scheme. Hostname is usually what you specify within the
1550 PeerAddr. See the "SSL_verifycn_publicsuffix" parameter for an
1551 explanation of suffix checking and for the possible values.
1552
1553 Verification of hostname against a certificate is different between
1554 various applications and RFCs. Some scheme allow wildcards for
1555 hostnames, some only in subjectAltNames, and even their different
1556 wildcard schemes are possible. RFC 6125 provides a good overview.
1557
1558 To ease the verification the following schemes are predefined (both
1559 protocol name and rfcXXXX name can be used):
1560
1561 rfc2818, xmpp (rfc3920), ftp (rfc4217)
1562 Extended wildcards in subjectAltNames and common name are
1563 possible, e.g. *.example.org or even www*.example.org. The
1564 common name will be only checked if no DNS names are given
1565 in subjectAltNames.
1566
1567 http (alias www)
1568 While name checking is defined in rfc2818 the current
1569 browsers usually accept also an IP address (w/o wildcards)
1570 within the common name as long as no subjectAltNames are
1571 defined. Thus this is rfc2818 extended with this feature.
1572
1573 smtp (rfc2595), imap, pop3, acap (rfc4642), netconf (rfc5538),
1574 syslog (rfc5425), snmp (rfc5953)
1575 Simple wildcards in subjectAltNames are possible, e.g.
1576 *.example.org matches www.example.org but not
1577 lala.www.example.org. If nothing from subjectAltNames match
1578 it checks against the common name, where wildcards are also
1579 allowed to match the full leftmost label.
1580
1581 ldap (rfc4513)
1582 Simple wildcards are allowed in subjectAltNames, but not in
1583 common name. Common name will be checked even if
1584 subjectAltNames exist.
1585
1586 sip (rfc5922)
1587 No wildcards are allowed and common name is checked even if
1588 subjectAltNames exist.
1589
1590 gist (rfc5971)
1591 Simple wildcards are allowed in subjectAltNames and common
1592 name, but common name will only be checked if their are no
1593 DNS names in subjectAltNames.
1594
1595 default This is a superset of all the rules and is automatically
1596 used if no scheme is given but a hostname (instead of IP)
1597 is known. Extended wildcards are allowed in
1598 subjectAltNames and common name and common name is checked
1599 always.
1600
1601 none No verification will be done. Actually is does not make
1602 any sense to call verify_hostname in this case.
1603
1604 The scheme can be given either by specifying the name for one of
1605 the above predefined schemes, or by using a hash which can have the
1606 following keys and values:
1607
1608 check_cn: 0|'always'|'when_only'
1609 Determines if the common name gets checked. If 'always' it
1610 will always be checked (like in ldap), if 'when_only' it
1611 will only be checked if no names are given in
1612 subjectAltNames (like in http), for any other values the
1613 common name will not be checked.
1614
1615 wildcards_in_alt: 0|'full_label'|'anywhere'
1616 Determines if and where wildcards in subjectAltNames are
1617 possible. If 'full_label' only cases like *.example.org
1618 will be possible (like in ldap), for 'anywhere'
1619 www*.example.org is possible too (like http), dangerous
1620 things like but www.*.org or even '*' will not be allowed.
1621 For compatibility with older versions 'leftmost' can be
1622 given instead of 'full_label'.
1623
1624 wildcards_in_cn: 0|'full_label'|'anywhere'
1625 Similar to wildcards_in_alt, but checks the common name.
1626 There is no predefined scheme which allows wildcards in
1627 common names.
1628
1629 ip_in_cn: 0|1|4|6
1630 Determines if an IP address is allowed in the common name
1631 (no wildcards are allowed). If set to 4 or 6 it only allows
1632 IPv4 or IPv6 addresses, any other true value allows both.
1633
1634 callback: \&coderef
1635 If you give a subroutine for verification it will be called
1636 with the arguments
1637 ($hostname,$commonName,@subjectAltNames), where hostname is
1638 the name given for verification, commonName is the result
1639 from peer_certificate('cn') and subjectAltNames is the
1640 result from peer_certificate('subjectAltNames').
1641
1642 All other arguments for the verification scheme will be
1643 ignored in this case.
1644
1645 next_proto_negotiated()
1646 This method returns the name of negotiated protocol - e.g.
1647 'http/1.1'. It works for both client and server side of SSL
1648 connection.
1649
1650 NPN support is available with Net::SSLeay 1.46+ and openssl-1.0.1+.
1651 To check support you might call "IO::Socket::SSL->can_npn()".
1652
1653 alpn_selected()
1654 Returns the protocol negotiated via ALPN as a string, e.g.
1655 'http/1.1', 'http/2.0' or 'spdy/3.1'.
1656
1657 ALPN support is available with Net::SSLeay 1.56+ and
1658 openssl-1.0.2+. To check support, use
1659 "IO::Socket::SSL->can_alpn()".
1660
1661 errstr()
1662 Returns the last error (in string form) that occurred. If you do
1663 not have a real object to perform this method on, call
1664 IO::Socket::SSL::errstr() instead.
1665
1666 For read and write errors on non-blocking sockets, this method may
1667 include the string "SSL wants a read first!" or "SSL wants a write
1668 first!" meaning that the other side is expecting to read from or
1669 write to the socket and wants to be satisfied before you get to do
1670 anything. But with version 0.98 you are better comparing the global
1671 exported variable $SSL_ERROR against the exported symbols
1672 SSL_WANT_READ and SSL_WANT_WRITE.
1673
1674 opened()
1675 This returns false if the socket could not be opened, 1 if the
1676 socket could be opened and the SSL handshake was successful done
1677 and -1 if the underlying IO::Handle is open, but the SSL handshake
1678 failed.
1679
1680 IO::Socket::SSL->start_SSL($socket, ... )
1681 This will convert a glob reference or a socket that you provide to
1682 an IO::Socket::SSL object. You may also pass parameters to
1683 specify context or connection options as with a call to new(). If
1684 you are using this function on an accept()ed socket, you must set
1685 the parameter "SSL_server" to 1, i.e.
1686 IO::Socket::SSL->start_SSL($socket, SSL_server => 1). If you have
1687 a class that inherits from IO::Socket::SSL and you want the $socket
1688 to be blessed into your own class instead, use
1689 MyClass->start_SSL($socket) to achieve the desired effect.
1690
1691 Note that if start_SSL() fails in SSL negotiation, $socket will
1692 remain blessed in its original class. For non-blocking sockets
1693 you better just upgrade the socket to IO::Socket::SSL and call
1694 accept_SSL or connect_SSL and the upgraded object. To just upgrade
1695 the socket set SSL_startHandshake explicitly to 0. If you call
1696 start_SSL w/o this parameter it will revert to blocking behavior
1697 for accept_SSL and connect_SSL.
1698
1699 If given the parameter "Timeout" it will stop if after the timeout
1700 no SSL connection was established. This parameter is only used for
1701 blocking sockets, if it is not given the default Timeout from the
1702 underlying IO::Socket will be used.
1703
1704 stop_SSL(...)
1705 This is the opposite of start_SSL(), connect_SSL() and
1706 accept_SSL(), e.g. it will shutdown the SSL connection and return
1707 to the class before start_SSL(). It gets the same arguments as
1708 close(), in fact close() calls stop_SSL() (but without downgrading
1709 the class).
1710
1711 Will return true if it succeeded and undef if failed. This might be
1712 the case for non-blocking sockets. In this case $! is set to
1713 EWOULDBLOCK and the ssl error to SSL_WANT_READ or SSL_WANT_WRITE.
1714 In this case the call should be retried again with the same
1715 arguments once the socket is ready.
1716
1717 For calling from "stop_SSL" "SSL_fast_shutdown" default to false,
1718 e.g. it waits for the close_notify of the peer. This is necessary
1719 in case you want to downgrade the socket and continue to use it as
1720 a plain socket.
1721
1722 After stop_SSL the socket can again be used to exchange plain data.
1723
1724 connect_SSL, accept_SSL
1725 These functions should be used to do the relevant handshake, if the
1726 socket got created with "new" or upgraded with "start_SSL" and
1727 "SSL_startHandshake" was set to false. They will return undef
1728 until the handshake succeeded or an error got thrown. As long as
1729 the function returns undef and $! is set to EWOULDBLOCK one could
1730 retry the call after the socket got readable (SSL_WANT_READ) or
1731 writeable (SSL_WANT_WRITE).
1732
1733 ocsp_resolver
1734 This will create an OCSP resolver object, which can be used to
1735 create OCSP requests for the certificates of the SSL connection.
1736 Which certificates are verified depends on the setting of
1737 "SSL_ocsp_mode": by default only the leaf certificate will be
1738 checked, but with SSL_OCSP_FULL_CHAIN all chain certificates will
1739 be checked.
1740
1741 Because to create an OCSP request the certificate and its issuer
1742 certificate need to be known it is not possible to check
1743 certificates when the trust chain is incomplete or if the
1744 certificate is self-signed.
1745
1746 The OCSP resolver gets created by calling "$ssl->ocsp_resolver" and
1747 provides the following methods:
1748
1749 hard_error
1750 This returns the hard error when checking the OCSP
1751 response. Hard errors are certificate revocations. With
1752 the "SSL_ocsp_mode" of SSL_OCSP_FAIL_HARD any soft error
1753 (e.g. failures to get signed information about the
1754 certificates) will be considered a hard error too.
1755
1756 The OCSP resolving will stop on the first hard error.
1757
1758 The method will return undef as long as no hard errors
1759 occurred and still requests to be resolved. If all requests
1760 got resolved and no hard errors occurred the method will
1761 return ''.
1762
1763 soft_error
1764 This returns the soft error(s) which occurred when asking
1765 the OCSP responders.
1766
1767 requests
1768 This will return a hash consisting of
1769 "(url,request)"-tuples, e.g. which contain the OCSP request
1770 string and the URL where it should be sent too. The usual
1771 way to send such a request is as HTTP POST request with a
1772 content-type of "application/ocsp-request" or as a GET
1773 request with the base64 and url-encoded request is added to
1774 the path of the URL.
1775
1776 After you've handled all these requests and added the
1777 response with "add_response" you should better call this
1778 method again to make sure, that no more requests are
1779 outstanding. IO::Socket::SSL will combine multiple OCSP
1780 requests for the same server inside a single request, but
1781 some server don't give a response to all these requests, so
1782 that one has to ask again with the remaining requests.
1783
1784 add_response($uri,$response)
1785 This method takes the HTTP body of the response which got
1786 received when sending the OCSP request to $uri. If no
1787 response was received or an error occurred one should
1788 either retry or consider $response as empty which will
1789 trigger a soft error.
1790
1791 The method returns the current value of "hard_error", e.g.
1792 a defined value when no more requests need to be done.
1793
1794 resolve_blocking(%args)
1795 This combines "requests" and "add_response" which
1796 HTTP::Tiny to do all necessary requests in a blocking way.
1797 %args will be given to HTTP::Tiny so that you can put proxy
1798 settings etc here. HTTP::Tiny will be called with
1799 "verify_SSL" of false, because the OCSP responses have
1800 their own signatures so no extra SSL verification is
1801 needed.
1802
1803 If you don't want to use blocking requests you need to roll
1804 your own user agent with "requests" and "add_response".
1805
1806 IO::Socket::SSL->new_from_fd($fd, [mode], %sslargs)
1807 This will convert a socket identified via a file descriptor into an
1808 SSL socket. Note that the argument list does not include a "MODE"
1809 argument; if you supply one, it will be thoughtfully ignored (for
1810 compatibility with IO::Socket::INET). Instead, a mode of '+<' is
1811 assumed, and the file descriptor passed must be able to handle such
1812 I/O because the initial SSL handshake requires bidirectional
1813 communication.
1814
1815 Internally the given $fd will be upgraded to a socket object using
1816 the "new_from_fd" method of the super class (IO::Socket::INET or
1817 similar) and then "start_SSL" will be called using the given
1818 %sslargs. If $fd is already an IO::Socket object you should better
1819 call "start_SSL" directly.
1820
1821 IO::Socket::SSL::default_ca([ path|dir| SSL_ca_file = ..., SSL_ca_path
1822 => ... ])>
1823 Determines or sets the default CA path. If existing path or dir or
1824 a hash is given it will set the default CA path to this value and
1825 never try to detect it automatically. If "undef" is given it will
1826 forget any stored defaults and continue with detection of system
1827 defaults. If no arguments are given it will start detection of
1828 system defaults, unless it has already stored user-set or
1829 previously detected values.
1830
1831 The detection of system defaults works similar to OpenSSL, e.g. it
1832 will check the directory specified in environment variable
1833 SSL_CERT_DIR or the path OPENSSLDIR/certs (SSLCERTS: on VMS) and
1834 the file specified in environment variable SSL_CERT_FILE or the
1835 path OPENSSLDIR/cert.pem (SSLCERTS:cert.pem on VMS). Contrary to
1836 OpenSSL it will check if the SSL_ca_path contains PEM files with
1837 the hash as file name and if the SSL_ca_file looks like PEM. If no
1838 usable system default can be found it will try to load and use
1839 Mozilla::CA and if not available give up detection. The result of
1840 the detection will be saved to speed up future calls.
1841
1842 The function returns the saved default CA as hash with SSL_ca_file
1843 and SSL_ca_path.
1844
1845 IO::Socket::SSL::set_default_context(...)
1846 You may use this to make IO::Socket::SSL automatically re-use a
1847 given context (unless specifically overridden in a call to new()).
1848 It accepts one argument, which should be either an IO::Socket::SSL
1849 object or an IO::Socket::SSL::SSL_Context object. See the
1850 SSL_reuse_ctx option of new() for more details. Note that this
1851 sets the default context globally, so use with caution (esp. in
1852 mod_perl scripts).
1853
1854 IO::Socket::SSL::set_default_session_cache(...)
1855 You may use this to make IO::Socket::SSL automatically re-use a
1856 given session cache (unless specifically overridden in a call to
1857 new()). It accepts one argument, which should be an
1858 IO::Socket::SSL::Session_Cache object or similar (e.g. something
1859 which implements get_session, add_session and del_session like
1860 IO::Socket::SSL::Session_Cache does). See the SSL_session_cache
1861 option of new() for more details. Note that this sets the default
1862 cache globally, so use with caution.
1863
1864 IO::Socket::SSL::set_defaults(%args)
1865 With this function one can set defaults for all SSL_* parameter
1866 used for creation of the context, like the SSL_verify* parameter.
1867 Any SSL_* parameter can be given or the following short versions:
1868
1869 mode - SSL_verify_mode
1870 callback - SSL_verify_callback
1871 scheme - SSL_verifycn_scheme
1872 name - SSL_verifycn_name
1873 IO::Socket::SSL::set_client_defaults(%args)
1874 Similar to "set_defaults", but only sets the defaults for client
1875 mode.
1876
1877 IO::Socket::SSL::set_server_defaults(%args)
1878 Similar to "set_defaults", but only sets the defaults for server
1879 mode.
1880
1881 IO::Socket::SSL::set_args_filter_hack(\&code|'use_defaults')
1882 Sometimes one has to use code which uses unwanted or invalid
1883 arguments for SSL, typically disabling SSL verification or setting
1884 wrong ciphers or SSL versions. With this hack it is possible to
1885 override these settings and restore sanity. Example:
1886
1887 IO::Socket::SSL::set_args_filter_hack( sub {
1888 my ($is_server,$args) = @_;
1889 if ( ! $is_server ) {
1890 # client settings - enable verification with default CA
1891 # and fallback hostname verification etc
1892 delete @{$args}{qw(
1893 SSL_verify_mode
1894 SSL_ca_file
1895 SSL_ca_path
1896 SSL_verifycn_scheme
1897 SSL_version
1898 )};
1899 # and add some fingerprints for known certs which are signed by
1900 # unknown CAs or are self-signed
1901 $args->{SSL_fingerprint} = ...
1902 }
1903 });
1904
1905 With the short setting "set_args_filter_hack('use_defaults')" it
1906 will prefer the default settings in all cases. These default
1907 settings can be modified with "set_defaults", "set_client_defaults"
1908 and "set_server_defaults".
1909
1910 The following methods are unsupported (not to mention futile!) and
1911 IO::Socket::SSL will emit a large CROAK() if you are silly enough to
1912 use them:
1913
1914 truncate
1915 stat
1916 ungetc
1917 setbuf
1918 setvbuf
1919 fdopen
1920 send/recv
1921 Note that send() and recv() cannot be reliably trapped by a tied
1922 filehandle (such as that used by IO::Socket::SSL) and so may send
1923 unencrypted data over the socket. Object-oriented calls to these
1924 functions will fail, telling you to use the print/printf/syswrite
1925 and read/sysread families instead.
1926
1928 The following functions are deprecated and are only retained for
1929 compatibility:
1930
1931 context_init()
1932 use the SSL_reuse_ctx option if you want to re-use a context
1933
1934 socketToSSL() and socket_to_SSL()
1935 use IO::Socket::SSL->start_SSL() instead
1936
1937 kill_socket()
1938 use close() instead
1939
1940 get_peer_certificate()
1941 use the peer_certificate() function instead. Used to return
1942 X509_Certificate with methods subject_name and issuer_name. Now
1943 simply returns $self which has these methods (although deprecated).
1944
1945 issuer_name()
1946 use peer_certificate( 'issuer' ) instead
1947
1948 subject_name()
1949 use peer_certificate( 'subject' ) instead
1950
1952 See the 'example' directory, the tests in 't' and also the tools in
1953 'util'.
1954
1956 If you use IO::Socket::SSL together with threads you should load it
1957 (e.g. use or require) inside the main thread before creating any other
1958 threads which use it. This way it is much faster because it will be
1959 initialized only once. Also there are reports that it might crash the
1960 other way.
1961
1962 Creating an IO::Socket::SSL object in one thread and closing it in
1963 another thread will not work.
1964
1965 IO::Socket::SSL does not work together with
1966 Storable::fd_retrieve/fd_store. See BUGS file for more information and
1967 how to work around the problem.
1968
1969 Non-blocking and timeouts (which are based on non-blocking) are not
1970 supported on Win32, because the underlying IO::Socket::INET does not
1971 support non-blocking on this platform.
1972
1973 If you have a server and it looks like you have a memory leak you might
1974 check the size of your session cache. Default for Net::SSLeay seems to
1975 be 20480, see the example for SSL_create_ctx_callback for how to limit
1976 it.
1977
1978 TLS 1.3 support regarding session reuse is incomplete.
1979
1981 IO::Socket::INET, IO::Socket::INET6, IO::Socket::IP, Net::SSLeay.
1982
1984 Many thanks to all who added patches or reported bugs or helped
1985 IO::Socket::SSL another way. Please keep reporting bugs and help with
1986 patches, even if they just fix the documentation.
1987
1988 Special thanks to the team of Net::SSLeay for the good cooperation.
1989
1991 Steffen Ullrich, <sullr at cpan.org> is the current maintainer.
1992
1993 Peter Behroozi, <behrooz at fas.harvard.edu> (Note the lack of an "i"
1994 at the end of "behrooz")
1995
1996 Marko Asplund, <marko.asplund at kronodoc.fi>, was the original author
1997 of IO::Socket::SSL.
1998
1999 Patches incorporated from various people, see file Changes.
2000
2002 The original versions of this module are Copyright (C) 1999-2002 Marko
2003 Asplund.
2004
2005 The rewrite of this module is Copyright (C) 2002-2005 Peter Behroozi.
2006
2007 Versions 0.98 and newer are Copyright (C) 2006-2014 Steffen Ullrich.
2008
2009 This module is free software; you can redistribute it and/or modify it
2010 under the same terms as Perl itself.
2011
2012
2013
2014perl v5.30.1 2019-11-25 IO::Socket::SSL(3)