… | |
… | |
296 | |
296 | |
297 | Instead of an object, you can also specify a hash reference with C<< key |
297 | Instead of an object, you can also specify a hash reference with C<< key |
298 | => value >> pairs. Those will be passed to L<AnyEvent::TLS> to create a |
298 | => value >> pairs. Those will be passed to L<AnyEvent::TLS> to create a |
299 | new TLS context object. |
299 | new TLS context object. |
300 | |
300 | |
301 | =item on_starttls => $cb->($handle, $success) |
301 | =item on_starttls => $cb->($handle, $success[, $error_message]) |
302 | |
302 | |
303 | This callback will be invoked when the TLS/SSL handshake has finished. If |
303 | This callback will be invoked when the TLS/SSL handshake has finished. If |
304 | C<$success> is true, then the TLS handshake succeeded, otherwise it failed |
304 | C<$success> is true, then the TLS handshake succeeded, otherwise it failed |
305 | (C<on_stoptls> will not be called in this case). |
305 | (C<on_stoptls> will not be called in this case). |
306 | |
306 | |
307 | The session in C<< $handle->{tls} >> can still be examined in this |
307 | The session in C<< $handle->{tls} >> can still be examined in this |
308 | callback, even when the handshake was not successful. |
308 | callback, even when the handshake was not successful. |
|
|
309 | |
|
|
310 | TLS handshake failures will not cause C<on_error> to be invoked when this |
|
|
311 | callback is in effect, instead, the error message will be passed to C<on_starttls>. |
|
|
312 | |
|
|
313 | Without this callback, handshake failures lead to C<on_error> being |
|
|
314 | called, as normal. |
|
|
315 | |
|
|
316 | Note that you cannot call C<starttls> right again in this callback. If you |
|
|
317 | need to do that, start an zero-second timer instead whose callback can |
|
|
318 | then call C<< ->starttls >> again. |
309 | |
319 | |
310 | =item on_stoptls => $cb->($handle) |
320 | =item on_stoptls => $cb->($handle) |
311 | |
321 | |
312 | When a SSLv3/TLS shutdown/close notify/EOF is detected and this callback is |
322 | When a SSLv3/TLS shutdown/close notify/EOF is detected and this callback is |
313 | set, then it will be invoked after freeing the TLS session. If it is not, |
323 | set, then it will be invoked after freeing the TLS session. If it is not, |
… | |
… | |
349 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
359 | $self->no_delay (delete $self->{no_delay}) if exists $self->{no_delay}; |
350 | |
360 | |
351 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
361 | $self->starttls (delete $self->{tls}, delete $self->{tls_ctx}) |
352 | if $self->{tls}; |
362 | if $self->{tls}; |
353 | |
363 | |
354 | $self->on_drain (delete $self->{on_drain}) if exists $self->{on_drain}; |
364 | $self->on_drain (delete $self->{on_drain}) if $self->{on_drain}; |
355 | |
365 | |
356 | $self->start_read |
366 | $self->start_read |
357 | if $self->{on_read}; |
367 | if $self->{on_read}; |
358 | |
368 | |
359 | $self->{fh} && $self |
369 | $self->{fh} && $self |
… | |
… | |
1435 | my $err =Net::SSLeay::ERR_error_string (Net::SSLeay::ERR_get_error ()); |
1445 | my $err =Net::SSLeay::ERR_error_string (Net::SSLeay::ERR_get_error ()); |
1436 | |
1446 | |
1437 | # reduce error string to look less scary |
1447 | # reduce error string to look less scary |
1438 | $err =~ s/^error:[0-9a-fA-F]{8}:[^:]+:([^:]+):/\L$1: /; |
1448 | $err =~ s/^error:[0-9a-fA-F]{8}:[^:]+:([^:]+):/\L$1: /; |
1439 | |
1449 | |
|
|
1450 | if ($self->{_on_starttls}) { |
|
|
1451 | (delete $self->{_on_starttls})->($self, undef, $err); |
|
|
1452 | &_freetls; |
|
|
1453 | } else { |
|
|
1454 | &_freetls; |
1440 | $self->_error (&Errno::EPROTO, 1, $err); |
1455 | $self->_error (&Errno::EPROTO, 1, $err); |
|
|
1456 | } |
1441 | } |
1457 | } |
1442 | |
1458 | |
1443 | # poll the write BIO and send the data if applicable |
1459 | # poll the write BIO and send the data if applicable |
1444 | # also decode read data if possible |
1460 | # also decode read data if possible |
1445 | # this is basiclaly our TLS state machine |
1461 | # this is basiclaly our TLS state machine |
… | |
… | |
1461 | && ($tmp != $ERROR_SYSCALL || $!); |
1477 | && ($tmp != $ERROR_SYSCALL || $!); |
1462 | } |
1478 | } |
1463 | |
1479 | |
1464 | while (defined ($tmp = Net::SSLeay::read ($self->{tls}))) { |
1480 | while (defined ($tmp = Net::SSLeay::read ($self->{tls}))) { |
1465 | unless (length $tmp) { |
1481 | unless (length $tmp) { |
|
|
1482 | $self->{_on_starttls} |
|
|
1483 | and (delete $self->{_on_starttls})->($self, undef, "EOF during handshake"); # ??? |
1466 | &_freetls; |
1484 | &_freetls; |
|
|
1485 | |
1467 | if ($self->{on_stoptls}) { |
1486 | if ($self->{on_stoptls}) { |
1468 | $self->{on_stoptls}($self); |
1487 | $self->{on_stoptls}($self); |
1469 | return; |
1488 | return; |
1470 | } else { |
1489 | } else { |
1471 | # let's treat SSL-eof as we treat normal EOF |
1490 | # let's treat SSL-eof as we treat normal EOF |
… | |
… | |
1489 | $self->_drain_wbuf; |
1508 | $self->_drain_wbuf; |
1490 | } |
1509 | } |
1491 | |
1510 | |
1492 | $self->{_on_starttls} |
1511 | $self->{_on_starttls} |
1493 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1512 | and Net::SSLeay::state ($self->{tls}) == Net::SSLeay::ST_OK () |
1494 | and (delete $self->{_on_starttls})->($self, 1); |
1513 | and (delete $self->{_on_starttls})->($self, 1, "TLS/SSL connection established"); |
1495 | } |
1514 | } |
1496 | |
1515 | |
1497 | =item $handle->starttls ($tls[, $tls_ctx]) |
1516 | =item $handle->starttls ($tls[, $tls_ctx]) |
1498 | |
1517 | |
1499 | Instead of starting TLS negotiation immediately when the AnyEvent::Handle |
1518 | Instead of starting TLS negotiation immediately when the AnyEvent::Handle |
… | |
… | |
1570 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1589 | $self->{_wbio} = Net::SSLeay::BIO_new (Net::SSLeay::BIO_s_mem ()); |
1571 | |
1590 | |
1572 | Net::SSLeay::set_bio ($ssl, $self->{_rbio}, $self->{_wbio}); |
1591 | Net::SSLeay::set_bio ($ssl, $self->{_rbio}, $self->{_wbio}); |
1573 | |
1592 | |
1574 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1593 | $self->{_on_starttls} = sub { $_[0]{on_starttls}(@_) } |
1575 | if exists $self->{on_starttls}; |
1594 | if $self->{on_starttls}; |
1576 | |
1595 | |
1577 | &_dotls; # need to trigger the initial handshake |
1596 | &_dotls; # need to trigger the initial handshake |
1578 | $self->start_read; # make sure we actually do read |
1597 | $self->start_read; # make sure we actually do read |
1579 | } |
1598 | } |
1580 | |
1599 | |
… | |
… | |
1604 | sub _freetls { |
1623 | sub _freetls { |
1605 | my ($self) = @_; |
1624 | my ($self) = @_; |
1606 | |
1625 | |
1607 | return unless $self->{tls}; |
1626 | return unless $self->{tls}; |
1608 | |
1627 | |
1609 | $self->{_on_starttls} |
|
|
1610 | and (delete $self->{_on_starttls})->($self, undef); |
|
|
1611 | |
|
|
1612 | $self->{tls_ctx}->_put_session (delete $self->{tls}); |
1628 | $self->{tls_ctx}->_put_session (delete $self->{tls}); |
1613 | |
1629 | |
1614 | delete @$self{qw(_rbio _wbio _tls_wbuf)}; |
1630 | delete @$self{qw(_rbio _wbio _tls_wbuf _on_starttls)}; |
1615 | } |
1631 | } |
1616 | |
1632 | |
1617 | sub DESTROY { |
1633 | sub DESTROY { |
1618 | my ($self) = @_; |
1634 | my ($self) = @_; |
1619 | |
1635 | |
… | |
… | |
1755 | $handle->on_drain (sub { |
1771 | $handle->on_drain (sub { |
1756 | warn "all data submitted to the kernel\n"; |
1772 | warn "all data submitted to the kernel\n"; |
1757 | undef $handle; |
1773 | undef $handle; |
1758 | }); |
1774 | }); |
1759 | |
1775 | |
|
|
1776 | If you just want to queue some data and then signal EOF to the other side, |
|
|
1777 | consider using C<< ->push_shutdown >> instead. |
|
|
1778 | |
|
|
1779 | =item I want to contact a TLS/SSL server, I don't care about security. |
|
|
1780 | |
|
|
1781 | If your TLS server is a pure TLS server (e.g. HTTPS) that only speaks TLS, |
|
|
1782 | simply connect to it and then create the AnyEvent::Handle with the C<tls> |
|
|
1783 | parameter: |
|
|
1784 | |
|
|
1785 | my $handle = new AnyEvent::Handle |
|
|
1786 | fh => $fh, |
|
|
1787 | tls => "connect", |
|
|
1788 | on_error => sub { ... }; |
|
|
1789 | |
|
|
1790 | $handle->push_write (...); |
|
|
1791 | |
|
|
1792 | =item I want to contact a TLS/SSL server, I do care about security. |
|
|
1793 | |
|
|
1794 | Then you #x##TODO# |
|
|
1795 | |
|
|
1796 | |
|
|
1797 | |
1760 | =back |
1798 | =back |
1761 | |
1799 | |
1762 | |
1800 | |
1763 | =head1 SUBCLASSING AnyEvent::Handle |
1801 | =head1 SUBCLASSING AnyEvent::Handle |
1764 | |
1802 | |