… | |
… | |
654 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) { |
654 | if (defined (my $buf = Net::SSLeay::BIO_read ($self->{tls_wbio}))) { |
655 | $self->{wbuf} .= $buf; |
655 | $self->{wbuf} .= $buf; |
656 | $self->_drain_wbuf; |
656 | $self->_drain_wbuf; |
657 | } |
657 | } |
658 | |
658 | |
659 | while () { |
|
|
660 | if (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
659 | while (defined (my $buf = Net::SSLeay::read ($self->{tls}))) { |
661 | $self->{rbuf} .= $buf; |
660 | $self->{rbuf} .= $buf; |
662 | $self->_drain_rbuf; |
661 | $self->_drain_rbuf; |
663 | } elsif ( |
662 | } |
|
|
663 | |
664 | (my $err = Net::SSLeay::get_error ($self->{tls}, -1)) |
664 | my $err = Net::SSLeay::get_error ($self->{tls}, -1); |
|
|
665 | |
665 | != Net::SSLeay::ERROR_WANT_READ () |
666 | if ($err!= Net::SSLeay::ERROR_WANT_READ ()) { |
666 | ) { |
|
|
667 | if ($err == Net::SSLeay::ERROR_SYSCALL ()) { |
667 | if ($err == Net::SSLeay::ERROR_SYSCALL ()) { |
668 | $self->error; |
668 | $self->error; |
669 | } elsif ($err == Net::SSLeay::ERROR_SSL ()) { |
669 | } elsif ($err == Net::SSLeay::ERROR_SSL ()) { |
670 | $! = &Errno::EIO; |
670 | $! = &Errno::EIO; |
671 | $self->error; |
671 | $self->error; |
672 | } |
|
|
673 | |
|
|
674 | last; |
|
|
675 | # all others are fine for our purposes |
|
|
676 | } else { |
|
|
677 | last; |
|
|
678 | } |
672 | } |
|
|
673 | |
|
|
674 | # all others are fine for our purposes |
679 | } |
675 | } |
680 | } |
676 | } |
|
|
677 | |
|
|
678 | =item $handle->starttls ($tls[, $tls_ctx]) |
|
|
679 | |
|
|
680 | Instead of starting TLS negotiation immediately when the AnyEvent::Handle |
|
|
681 | object is created, you can also do that at a later time by calling |
|
|
682 | C<starttls>. |
|
|
683 | |
|
|
684 | The first argument is the same as the C<tls> constructor argument (either |
|
|
685 | C<"connect">, C<"accept"> or an existing Net::SSLeay object). |
|
|
686 | |
|
|
687 | The second argument is the optional C<Net::SSLeay::CTX> object that is |
|
|
688 | used when AnyEvent::Handle has to create its own TLS connection object. |
|
|
689 | |
|
|
690 | =cut |
681 | |
691 | |
682 | # TODO: maybe document... |
692 | # TODO: maybe document... |
683 | sub starttls { |
693 | sub starttls { |
684 | my ($self, $ssl, $ctx) = @_; |
694 | my ($self, $ssl, $ctx) = @_; |
|
|
695 | |
|
|
696 | $self->stoptls; |
685 | |
697 | |
686 | if ($ssl eq "accept") { |
698 | if ($ssl eq "accept") { |
687 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
699 | $ssl = Net::SSLeay::new ($ctx || TLS_CTX ()); |
688 | Net::SSLeay::set_accept_state ($ssl); |
700 | Net::SSLeay::set_accept_state ($ssl); |
689 | } elsif ($ssl eq "connect") { |
701 | } elsif ($ssl eq "connect") { |
… | |
… | |
714 | Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); |
726 | Net::SSLeay::BIO_write ($_[0]{tls_rbio}, ${$_[1]}); |
715 | &_dotls; |
727 | &_dotls; |
716 | }; |
728 | }; |
717 | } |
729 | } |
718 | |
730 | |
|
|
731 | =item $handle->stoptls |
|
|
732 | |
|
|
733 | Destroys the SSL connection, if any. Partial read or write data will be |
|
|
734 | lost. |
|
|
735 | |
|
|
736 | =cut |
|
|
737 | |
|
|
738 | sub stoptls { |
|
|
739 | my ($self) = @_; |
|
|
740 | |
|
|
741 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
|
|
742 | delete $self->{tls_rbio}; |
|
|
743 | delete $self->{tls_wbio}; |
|
|
744 | delete $self->{tls_wbuf}; |
|
|
745 | delete $self->{filter_r}; |
|
|
746 | delete $self->{filter_w}; |
|
|
747 | } |
|
|
748 | |
719 | sub DESTROY { |
749 | sub DESTROY { |
720 | my $self = shift; |
750 | my $self = shift; |
721 | |
751 | |
722 | Net::SSLeay::free (delete $self->{tls}) if $self->{tls}; |
752 | $self->stoptls; |
723 | } |
753 | } |
724 | |
754 | |
725 | =item AnyEvent::Handle::TLS_CTX |
755 | =item AnyEvent::Handle::TLS_CTX |
726 | |
756 | |
727 | This function creates and returns the Net::SSLeay::CTX object used by |
757 | This function creates and returns the Net::SSLeay::CTX object used by |