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