| 1 | package DJabberd::Stanza::StartTLS; | ||||||||||||||||||||||||||||
| 2 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 3 | 20 | ▶ | use base qw(DJabberd::Stanza); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 20 | ▶ | use Net::SSLeay qw(ERROR_WANT_READ ERROR_WANT_WRITE ERROR_SYSCALL); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 20 | Net::SSLeay::load_error_strings(); | |||||||||||||||||||||||||||
| 7 | 20 | Net::SSLeay::SSLeay_add_ssl_algorithms(); | |||||||||||||||||||||||||||
| 8 | 20 | Net::SSLeay::randomize(); | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 0 | ▶ | sub on_recv_from_server { &process } | ||||||||||||||||||||||||||
|
Subroutine
on_recv_from_server: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 11 | 0 | ▶ | sub on_recv_from_client { &process } | ||||||||||||||||||||||||||
|
Subroutine
on_recv_from_client: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub process { | ||||||||||||||||||||||||||
|
Subroutine
process: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||
| 14 | 0 | my ($self, $conn) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # {=tls-no-spaces} -- we can't send spaces after the closing bracket | ||||||||||||||||||||||||||||
| 17 | 0 | $conn->write("<proceed xmlns='urn:ietf:params:xml:ns:xmpp-tls' />"); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 0 | ▶ | my $ctx = Net::SSLeay::CTX_new() | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 20 | or die("Failed to create SSL_CTX $!"); | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 0 | $Net::SSLeay::ssl_version = 10; # Insist on TLSv1 | |||||||||||||||||||||||||||
| 23 | #$Net::SSLeay::ssl_version = 3; # Insist on SSLv3 | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 0 | ▶ | Net::SSLeay::CTX_set_options($ctx, &Net::SSLeay::OP_ALL) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 26 | and Net::SSLeay::die_if_ssl_error("ssl ctx set options"); | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 0 | ▶ | Net::SSLeay::CTX_set_mode($ctx, 1) # enable partial writes | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 29 | and Net::SSLeay::die_if_ssl_error("ssl ctx set options"); | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | # Following will ask password unless private key is not encrypted | ||||||||||||||||||||||||||||
| 32 | 0 | Net::SSLeay::CTX_use_RSAPrivateKey_file ($ctx, $conn->vhost->server->ssl_private_key_file, | |||||||||||||||||||||||||||
| 33 | &Net::SSLeay::FILETYPE_PEM); | ||||||||||||||||||||||||||||
| 34 | 0 | Net::SSLeay::die_if_ssl_error("private key"); | |||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 0 | Net::SSLeay::CTX_use_certificate_file ($ctx, $conn->vhost->server->ssl_cert_file, | |||||||||||||||||||||||||||
| 37 | &Net::SSLeay::FILETYPE_PEM); | ||||||||||||||||||||||||||||
| 38 | 0 | Net::SSLeay::die_if_ssl_error("certificate"); | |||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | if ($conn->vhost->server->ssl_cert_chain_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 0 | Net::SSLeay::CTX_use_certificate_chain_file ($ctx, $conn->vhost->server->ssl_cert_chain_file); | |||||||||||||||||||||||||||
| 42 | 0 | Net::SSLeay::die_if_ssl_error("certificate chain file"); | |||||||||||||||||||||||||||
| 43 | } | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 0 | ▶ | my $ssl = Net::SSLeay::new($ctx) or die_now("Failed to create SSL $!"); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | 0 | $conn->{ssl} = $ssl; | |||||||||||||||||||||||||||
| 48 | 0 | $conn->restart_stream; | |||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | DJabberd::Stanza::StartTLS->finalize_ssl_negotiation($conn, $ssl, $ctx); | |||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # Complete the transformation of stream from tcp socket into ssl socket: | ||||||||||||||||||||||||||||
| 54 | # 1. setup disconnect handler to free memory for $ssl and $ctx on connection close | ||||||||||||||||||||||||||||
| 55 | # 2. SSL object is connected to underlying connection socket | ||||||||||||||||||||||||||||
| 56 | # 3. 'accept' tells SSL to start negotiating encryption | ||||||||||||||||||||||||||||
| 57 | # 4. set a socket write function that encrypts data before writting to the underlying socket | ||||||||||||||||||||||||||||
| 58 | 0 | ▶ | sub finalize_ssl_negotiation { | ||||||||||||||||||||||||||
|
Subroutine
finalize_ssl_negotiation: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 59 | 0 | my ($class, $conn, $ssl, $ctx) = @_; | |||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | # Add a disconnect handler to this connection that will free memory | ||||||||||||||||||||||||||||
| 62 | # and remove references to junk no longer needed on close | ||||||||||||||||||||||||||||
| 63 | 0 | ▶ | $conn->add_disconnect_handler(sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | $conn->set_writer_func(sub { return 0 }); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 65 | 0 | Net::SSLeay::free($ssl); | |||||||||||||||||||||||||||
| 66 | # Currently, a CTX_new is being called for every SSL connection. | ||||||||||||||||||||||||||||
| 67 | # It would be more efficient to create one $ctx per-vhost instead of per-connection | ||||||||||||||||||||||||||||
| 68 | # and to re-use that $ctx object for each new connection to that vhost. | ||||||||||||||||||||||||||||
| 69 | # This would eliminate the need to free $ctx here. | ||||||||||||||||||||||||||||
| 70 | 0 | Net::SSLeay::CTX_free($ctx); | |||||||||||||||||||||||||||
| 71 | 0 | $conn->{ssl} = undef; | |||||||||||||||||||||||||||
| 72 | 0 | }); | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 0 | my $fileno = $conn->{sock}->fileno; | |||||||||||||||||||||||||||
| 75 | 0 | warn "setting ssl ($ssl) fileno to $fileno\n"; | |||||||||||||||||||||||||||
| 76 | 0 | Net::SSLeay::set_fd($ssl, $fileno); | |||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 0 | $Net::SSLeay::trace = 2; | |||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | my $rv = Net::SSLeay::accept($ssl); | |||||||||||||||||||||||||||
| 81 | 0 | ▶ | if (!$rv) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | warn "SSL accept error on $conn\n"; | |||||||||||||||||||||||||||
| 83 | 0 | $conn->close; | |||||||||||||||||||||||||||
| 84 | 0 | return; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | warn "$conn: Cipher `" . Net::SSLeay::get_cipher($ssl) . "'\n"; | |||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 0 | $conn->set_writer_func(DJabberd::Stanza::StartTLS->danga_socket_writerfunc($conn)); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | sub actual_error_on_empty_read { | ||||||||||||||||||||||||||
|
Subroutine
actual_error_on_empty_read: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 93 | 0 | my ($class, $ssl) = @_; | |||||||||||||||||||||||||||
| 94 | 0 | my $err = Net::SSLeay::get_error($ssl, -1); | |||||||||||||||||||||||||||
| 95 | 0 | ▶ | if ($err == ERROR_WANT_READ || $err == ERROR_WANT_WRITE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$err == ERROR_WANT_READ() or $err == ERROR_WANT_WRITE()
MC/DC0%0/2
$err == ERROR_WANT_READ() or $err == ERROR_WANT_WRITE()
$err == ERROR_WANT_READ()
$err == ERROR_WANT_WRITE()
Truth table0%0/3
A or B
A= $err == ERROR_WANT_READ()B= $err == ERROR_WANT_WRITE()
| |||||||||||||||||||||||||||||
| 96 | # Not an actual error, SSL is busy doing something like renegotiating encryption | ||||||||||||||||||||||||||||
| 97 | # just try again next time | ||||||||||||||||||||||||||||
| 98 | 0 | return undef; | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 0 | ▶ | if ($err == ERROR_SYSCALL) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 101 | # return the specific syscall error | ||||||||||||||||||||||||||||
| 102 | 0 | return "syscall error: $!"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | # This is actually an error (return the SSL err code) | ||||||||||||||||||||||||||||
| 105 | # unlike the 'no-op' WANT_READ and WANT_WRITE | ||||||||||||||||||||||||||||
| 106 | 0 | return "ssl error $err: " . Net::SSLeay::ERR_error_string($err); | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub danga_socket_writerfunc { | ||||||||||||||||||||||||||
|
Subroutine
danga_socket_writerfunc: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 111 | 0 | my ($class, $conn) = @_; | |||||||||||||||||||||||||||
| 112 | 0 | my $ssl = $conn->{ssl}; | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 114 | 0 | my ($bref, $to_write, $offset) = @_; | |||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | # unless our event_read has been called, we don't want to try | ||||||||||||||||||||||||||||
| 117 | # to do any work now. and probably we should complain. | ||||||||||||||||||||||||||||
| 118 | 0 | ▶ | if ($conn->{write_when_readable}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | 0 | warn "writer func called when we're waiting for readability first.\n"; | |||||||||||||||||||||||||||
| 120 | 0 | return 0; | |||||||||||||||||||||||||||
| 121 | } | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | # we can't write a lot or we get some SSL non-blocking error. | ||||||||||||||||||||||||||||
| 124 | # NO LONGER RELEVANT? | ||||||||||||||||||||||||||||
| 125 | # $to_write = 4096 if $to_write > 4096; | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | my $str = substr($$bref, $offset, $to_write); | |||||||||||||||||||||||||||
| 128 | 0 | my $written = Net::SSLeay::write($ssl, $str); | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | ▶ | if ($written == -1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 0 | my $err = Net::SSLeay::get_error($ssl, $written); | |||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | ▶ | if ($err == ERROR_WANT_READ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 134 | 0 | $conn->write_when_readable; | |||||||||||||||||||||||||||
| 135 | 0 | return 0; | |||||||||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | if ($err == ERROR_WANT_WRITE) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | # unclear here. it just wants to write some more? okay. | ||||||||||||||||||||||||||||
| 139 | # easy enough. do nothing? | ||||||||||||||||||||||||||||
| 140 | 0 | return 0; | |||||||||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 0 | my $errstr = Net::SSLeay::ERR_error_string($err); | |||||||||||||||||||||||||||
| 144 | 0 | warn " SSL write err = $err, $errstr\n"; | |||||||||||||||||||||||||||
| 145 | 0 | Net::SSLeay::print_errs("SSL_write"); | |||||||||||||||||||||||||||
| 146 | 0 | $conn->close; | |||||||||||||||||||||||||||
| 147 | 0 | return 0; | |||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | 0 | return $written; | |||||||||||||||||||||||||||
| 151 | 0 | }; | |||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 20 | 1; | |||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | # LocalWords: conn | ||||||||||||||||||||||||||||