DJabberd/Stanza/StartTLS.pm

filter: Statementstmt 17.1% 13 / 76 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 27.2% 3 / 11 Podpod 0.0% 0 / 6 totaltotal 13.3% 16 / 120 CC 13 SCAR 28.3
CC
13
Cov
13%
process
30.0
__ANON__
30.0
actual_error_on_empty_read
20.0
CRAP 16.9
« DJabberd/Delivery/ComponentConnection.pm ↑ Summary DJabberd/Subscription.pm »
1 package DJabberd::Stanza::StartTLS;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use base qw(DJabberd::Stanza);
Subroutine BEGIN: called CC=1 SCAR=0.0
4 17 ▶use Net::SSLeay qw(ERROR_WANT_READ ERROR_WANT_WRITE ERROR_SYSCALL);
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 17 Net::SSLeay::load_error_strings();
7 17 Net::SSLeay::SSLeay_add_ssl_algorithms();
8 17 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 sub process {
14 0 ▶ my ($self, $conn) = @_;
Subroutine process: not called CC=5 SCAR=34.0
Pod undocumented
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
BranchTrueFalseTotal
my $ctx = &Net::SSLeay::CTX_new() or die "Failed to create SSL_CTX $!" 0 0 0
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
BranchTrueFalseTotal
&Net::SSLeay::CTX_set_options($ctx, scalar &Net::SSLeay::OP_ALL) and Net::SSLeay::die_if_ssl_error("ssl ctx set options") 0 0 0
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
BranchTrueFalseTotal
&Net::SSLeay::CTX_set_mode($ctx, 1) and Net::SSLeay::die_if_ssl_error("ssl ctx set options") 0 0 0
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  
41 0 ▶ my $ssl = Net::SSLeay::new($ctx) or die_now("Failed to create SSL $!");
Branch: 1 branch
BranchTrueFalseTotal
my $ssl = &Net::SSLeay::new($ctx) or die_now("Failed to create SSL $!") 0 0 0
42 0 $conn->{ssl} = $ssl;
43 0 $conn->restart_stream;
44
45 0 DJabberd::Stanza::StartTLS->finalize_ssl_negotiation($conn, $ssl, $ctx);
46 }
47  
48 # Complete the transformation of stream from tcp socket into ssl socket:
49 # 1. setup disconnect handler to free memory for $ssl and $ctx on connection close
50 # 2. SSL object is connected to underlying connection socket
51 # 3. 'accept' tells SSL to start negotiating encryption
52 # 4. set a socket write function that encrypts data before writting to the underlying socket
53 sub finalize_ssl_negotiation {
54 0 ▶ my ($class, $conn, $ssl, $ctx) = @_;
Subroutine finalize_ssl_negotiation: not called CC=2 SCAR=17.9
Pod undocumented
55  
56 # Add a disconnect handler to this connection that will free memory
57 # and remove references to junk no longer needed on close
58 $conn->add_disconnect_handler(sub {
59 0 ▶ $conn->set_writer_func(sub { return 0 });
Subroutine __ANON__: not called CC=1 SCAR=6.9
60 0 Net::SSLeay::free($ssl);
61 # Currently, a CTX_new is being called for every SSL connection.
62 # It would be more efficient to create one $ctx per-vhost instead of per-connection
63 # and to re-use that $ctx object for each new connection to that vhost.
64 # This would eliminate the need to free $ctx here.
65 0 Net::SSLeay::CTX_free($ctx);
66 0 $conn->{ssl} = undef;
67 0 });
68  
69 0 my $fileno = $conn->{sock}->fileno;
70 0 warn "setting ssl ($ssl) fileno to $fileno\n";
71 0 Net::SSLeay::set_fd($ssl, $fileno);
72  
73 0 $Net::SSLeay::trace = 2;
74  
75 0 my $rv = Net::SSLeay::accept($ssl);
76 0 ▶ if (!$rv) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($rv) 0 0 0
77 0 warn "SSL accept error on $conn\n";
78 0 $conn->close;
79 0 return;
80 }
81  
82 0 warn "$conn: Cipher `" . Net::SSLeay::get_cipher($ssl) . "'\n";
83  
84 0 $conn->set_writer_func(DJabberd::Stanza::StartTLS->danga_socket_writerfunc($conn));
85 }
86  
87 sub actual_error_on_empty_read {
88 0 ▶ my ($class, $ssl) = @_;
Subroutine actual_error_on_empty_read: not called CC=4 SCAR=30.0
Pod undocumented
89 0 my $err = Net::SSLeay::get_error($ssl, -1);
90 0 ▶ if ($err == ERROR_WANT_READ || $err == ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err == ERROR_WANT_READ() or $err == ERROR_WANT_WRITE()) 0 0 0
Condition0%0/3
$err == ERROR_WANT_READ() or $err == ERROR_WANT_WRITE()
l!l&&r!l&&!r
000
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()
ABresult
1X1
011
000
91 # Not an actual error, SSL is busy doing something like renegotiating encryption
92 # just try again next time
93 0 return undef;
94 }
95 0 ▶ if ($err == ERROR_SYSCALL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err == ERROR_SYSCALL()) 0 0 0
96 # return the specific syscall error
97 0 return "syscall error: $!";
98 }
99 # This is actually an error (return the SSL err code)
100 # unlike the 'no-op' WANT_READ and WANT_WRITE
101 0 return "ssl error $err: " . Net::SSLeay::ERR_error_string($err);
102 }
103  
104  
105 sub danga_socket_writerfunc {
106 0 ▶ my ($class, $conn) = @_;
Subroutine danga_socket_writerfunc: not called CC=1 SCAR=6.9
Pod undocumented
107 0 my $ssl = $conn->{ssl};
108 return sub {
109 0 ▶ my ($bref, $to_write, $offset) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
110  
111 # unless our event_read has been called, we don't want to try
112 # to do any work now. and probably we should complain.
113 0 ▶ if ($conn->{write_when_readable}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($conn->{"write_when_readable"}) 0 0 0
114 0 warn "writer func called when we're waiting for readability first.\n";
115 0 return 0;
116 }
117  
118 # we can't write a lot or we get some SSL non-blocking error.
119 # NO LONGER RELEVANT?
120 # $to_write = 4096 if $to_write > 4096;
121  
122 0 my $str = substr($$bref, $offset, $to_write);
123 0 my $written = Net::SSLeay::write($ssl, $str);
124  
125 0 ▶ if ($written == -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($written == -1) 0 0 0
126 0 my $err = Net::SSLeay::get_error($ssl, $written);
127  
128 0 ▶ if ($err == ERROR_WANT_READ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err == ERROR_WANT_READ()) 0 0 0
129 0 $conn->write_when_readable;
130 0 return 0;
131 }
132 0 ▶ if ($err == ERROR_WANT_WRITE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err == ERROR_WANT_WRITE()) 0 0 0
133 # unclear here. it just wants to write some more? okay.
134 # easy enough. do nothing?
135 0 return 0;
136 }
137  
138 0 my $errstr = Net::SSLeay::ERR_error_string($err);
139 0 warn " SSL write err = $err, $errstr\n";
140 0 Net::SSLeay::print_errs("SSL_write");
141 0 $conn->close;
142 0 return 0;
143 }
144  
145 0 return $written;
146 0 };
147 }
148  
149 17 1;
150  
151 # LocalWords: conn
« DJabberd/Delivery/ComponentConnection.pm ↑ Summary DJabberd/Subscription.pm »