blib/lib/Net/BEEP/Lite/TLSProfile.pm

filter: Statementstmt 31.6% 43 / 136 Branchbran 3.3% 1 / 30 Conditioncond 11.1% 1 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 56.2% 9 / 16 Podpod 100% 7 / 7 totaltotal 30.0% 61 / 203 CC 20 SCAR 27.4
CC
20
Cov
26%
start_TLS
42.0
_start_TLS
30.0
MSG
12.0
CRAP 15.5
↑ Summary
1 # $Id: TLSProfile.pm,v 1.1 2003/09/11 23:25:51 davidb Exp $
2 #
3 # Copyright (C) 2003 Verisign, Inc.
4 #
5 # This library is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU Lesser General Public
7 # License as published by the Free Software Foundation; either
8 # version 2.1 of the License, or (at your option) any later version.
9 #
10 # This library is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 # Lesser General Public License for more details.
14 #
15 # You should have received a copy of the GNU Lesser General Public
16 # License along with this library; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18 # USA
19  
20 package Net::BEEP::Lite::TLSProfile;
21  
22 =head1 NAME
23  
24 Net::BEEP::Lite::TLSProfile - A TLS tuning profile for Net::BEEP::Lite.
25  
26 =head1 SYNOPSIS
27  
28 use Net::BEEP::Lite;
29 use Net::BEEP::Lite::TLSProfile;
30  
31 my $c_session = Net::BEEP::Lite::beep_connect(Host => localhost,
32 Port => 12345) ||
33 die "could not connect to beep peer: $!";
34  
35 if ($c_session->has_remote_profile($Net::BEEP::Lite::TLSProfile::URI)) {
36 my $tls_profile = new Net::BEEP::Lite::TLSProfile(SSL_verify_mode => 0x01);
37  
38 $tls_profile->start_TLS($c_session) || die "could not establish TLS";
39  
40 print "peer certificate info: ", $session->{peer_certificate}, "\n";
41 }
42  
43 ---
44  
45 use Net::BEEP::Lite;
46 use Net::BEEP::Lite::TLSProfile;
47  
48 my $other_profile = ...;
49  
50 my $tls_profile = Net::BEEP::Lite::TLSProfile
51 (Server => 1,
52 Callback => sub { my $session = shift;
53 $session->add_local_profile($other_profile); },
54 SSL_cert_file => 'my_cert.pem',
55 SSL_key_file => 'my_key.pem',
56 SSL_ca_file => 'my_ca.pem',
57 SSL_passwd_db => sub { "some-passwd" });
58  
59 Net::BEEP::Lite::beep_listen(Port => 12345,
60 Method => 'fork',
61 Profiles => [ $tls_profile ]);
62  
63 =head1 ABSTRACT
64  
65 <Net::BEEP::Lite::TLSProfile> is a TLS profile for BEEP as defined by
66 RFC 3080 for use with the C<Net::BEEP::Lite> module.
67  
68 =head1 DESCRIPTION
69  
70 This is a TLS profile for BEEP as defined by RFC 3080 for use with the
71 C<Net::BEEP::Lite> module. It can be use for both the initiator and
72 listener roles. This module relies heavily on the C<IO::Socket::SSL>
73 module for the TLS implementation.
74  
75 =cut
76  
77 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
78 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 1 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
82 1 ▶use IO::Socket::SSL;
Subroutine BEGIN: called CC=1 SCAR=0.0
83  
84 1 ▶use Net::BEEP::Lite::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
85  
86 1 ▶use base qw(Net::BEEP::Lite::BaseProfile);
Subroutine BEGIN: called CC=1 SCAR=0.0
87  
88 1 our($URI, $errstr, $VERSION);
89  
90 1 $URI = 'http://iana.org/beep/TLS';
91  
92 1 $VERSION = '0.01';
93  
94 =head1 CONSTRUCTOR
95  
96 =over 4
97  
98 =item new( I<ARGS> )
99  
100 This is the main constructor. It takes a named parameter lists as its
101 argument. See the C<initialize> method of a list of valid parameters.
102 It also takes the named parameters of C<Net::BEEP::Lite::BaseProfile>.
103  
104 =back
105  
106 =cut
107  
108 1 ▶sub new {
Subroutine new: called CC=2 SCAR=7.1
Pod documented
109 1 my $this = shift;
110 1 ▶ my $class = ref($this) || $this;
Condition33%1/3
ref $this || $this
l!l&&r!l&&!r
010
MC/DC0%0/2
ref $this || $this
ref $this $this
Truth table33%1/3
A || B
A= ref $thisB= $this
ABresult
1X1
011
000
111  
112 1 my $self = {};
113  
114 1 bless $self, $class;
115  
116 1 $self->SUPER::initialize(@_);
117 1 $self->initialize(@_);
118  
119 1 $self->{parser} = XML::LibXML->new();
120  
121 1 $self;
122 }
123  
124 =head1 METHODS
125  
126 =over 4
127  
128 =item initialize( I<ARGS> )
129  
130 Initialze this profile. This is generally called by the constructor.
131 It takes the following named parameters:
132  
133 =over 4
134  
135 =item Server
136  
137 Set this to true when this profile is being used by a BEEP peer in the
138 Listener role. This will tell the underlying TLS negotation that it
139 is the server. If this isn't set correctly, the TLS negotiation will
140 fail.
141  
142 =item Callback
143  
144 If this is set to a sub reference, this subroutine will be called upon
145 a successful TLS negotiation. It will be passed a reference to the
146 session as its first and only argument. For example, this might be
147 used to change the local profiles offered.
148  
149 =item SSL_*
150  
151 These are parameters that are understood by C<IO::Socket::SSL::new>.
152 You will probably want to use a few of them: SSL_cert_file,
153 SSL_key_file, and SSL_verify_mode are typical.
154  
155 =back
156  
157 =cut
158  
159 1 ▶sub initialize {
Subroutine initialize: called CC=5 SCAR=20.9
Pod documented
160 1 my $self = shift;
161 1 my %args = @_;
162  
163 1 $self->{uri} = $URI;
164 1 $self->{_callback} = 0;
165 1 $self->{_ssl_args} = { SSL_version => 'TLSv1' };
166  
167 1 for (keys %args) {
168 1 my $val = $args{$_};
169  
170 1 ▶ /^server$/io and do {
Branch: 1 branch
BranchTrueFalseTotal
/^server$/io and do { $self->{"_is_server"} = $val; next } 1 0 1
171 1 $self->{_is_server} = $val;
172 1 next;
173 };
174 0 ▶ /^callback$/io and do {
Branch: 1 branch
BranchTrueFalseTotal
/^callback$/io and do { $self->{"_callback"} = $val; next } 0 0 0
175 0 $self->{_callback} = $val;
176 0 next;
177 };
178 0 ▶ /^SSL_/ and do {
Branch: 1 branch
BranchTrueFalseTotal
/^SSL_/ and do { $self->{"_ssl_args"}{$_} = $val; next } 0 0 0
179 0 $self->{_ssl_args}->{$_} = $val;
180 0 next;
181 };
182 }
183 }
184  
185  
186 # This handles the piggybacked <ready /> request. IMO, this is really
187 # the only way to do TLS. I'm not sure why anyone would bother with
188 # the non-piggybacked form of this profile.
189 #
190 # NOTE: this handles the back end of the exchange itself, so we can
191 # drop right into the TLS negotation after sending the <proceed />
192 # response.
193 0 ▶sub start_channel_request {
Subroutine start_channel_request: not called CC=2 SCAR=17.9
Pod documented
194 0 my $self = shift;
195 0 my $session = shift;
196 0 my $message = shift;
197 0 my $data = shift;
198  
199 0 my $el = $self->_parse_content($data);
200 0 ▶ if ($el->nodeName eq 'ready') {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->nodeName eq 'ready') { } 0 0 0
201  
202 # FIXME: deal with version attribute.
203  
204 # send <profile> response ourselves.
205 0 my $proceed_cdata = new XML::LibXML::CDATASection("<proceed />");
206 $session->{mgmt_profile}->send_profile_message
207 0 ($session, $message->msgno(), $self->uri(), $proceed_cdata, 0);
208  
209 # start TLS
210 0 $self->_start_TLS($session);
211  
212 # inform the management profile to do nothing else.
213 0 return 'NUL';
214 }
215 else {
216 # we create the channel, but return an embedded error.
217 0 return ('RPY', "<error code='501'>unknown request.</error>", 0);
218 }
219 }
220  
221 # This handles server side of the non-piggybacked form of the TLS
222 # negotiation.
223 0 ▶sub MSG {
Subroutine MSG: not called CC=3 SCAR=24.8
Pod documented
224 0 my $self = shift;
225 0 my $session = shift;
226 0 my $message = shift;
227  
228 0 ▶ if ($message->content_type() ne 'application/beep+xml') {
Branch: 1 branch
BranchTrueFalseTotal
if ($message->content_type ne "application/beep+xml") 0 0 0
229 0 my $resp = new Net::BEEP::Lite::Message
230 (Type => 'ERR',
231 Msgno => $message->msgno(),
232 Channel => $message->channel_number(),
233 ContentType => 'application/beep+xml',
234 Content => "<error code='501'>Unknown request.</error>");
235  
236 0 $session->send_message($resp);
237 0 return;
238 }
239  
240 0 my $el = $self->_parse_content($message->content());
241 0 ▶ if ($el->nodeName eq 'ready') {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->nodeName eq 'ready') { } 0 0 0
242  
243 # send <proceed /> response
244 0 my $resp = new Net::BEEP::Lite::Message
245 (Type => 'RPY',
246 Channel => $message->channel_number(),
247 Msgno => $message->msgno(),
248 ContentType => 'application/beep+xml',
249 Content => '<proceed />');
250  
251 0 $session->send_message($resp);
252  
253 # start TLS
254 0 $self->_start_TLS($session);
255 }
256 else {
257  
258 0 my $resp = new Net::BEEP::Lite::Message
259 (Type => 'ERR',
260 Channel => $message->channel_number(),
261 Msgno => $message->msgno(),
262 ContentType => 'application/beep+xml',
263 Content => "<error code='501'>Unknown request.</error>");
264  
265 0 $session->send_message($resp);
266 }
267  
268 0 $message;
269 }
270  
271  
272 # This handles the client side of the non-piggybacked form of this
273 # profile.
274 0 ▶sub RPY {
Subroutine RPY: not called CC=3 SCAR=24.8
Pod documented
275 0 my $self = shift;
276 0 my $session = shift;
277 0 my $message = shift;
278  
279 0 my $el = $self->_parse_content($message->content());
280 0 ▶ if ($el->nodeName eq 'proceed') {
Branch: 1 branch
BranchTrueFalseTotal
if ($el->nodeName eq 'proceed') { } 0 0 0
281  
282 # start TLS
283 0 ▶ $self->_start_TLS($session) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_start_TLS($session) or return undef 0 0 0
284 }
285 else {
286 0 $errstr = "Non-proceed response: " . $message->content();
287 0 return undef;
288 }
289  
290 0 $message;
291 }
292  
293 # This handles error messages on the channel. Presumably, a
294 # non-piggybacked "ready" request was broken in some way.
295 0 ▶sub ERR {
Subroutine ERR: not called CC=1 SCAR=6.9
Pod documented
296 0 my $self = shift;
297 0 my $session = shift;
298 0 my $message = shift;
299  
300 0 $errstr = "error response: ", $message->content();
301  
302 0 $message;
303 }
304  
305 0 ▶sub _parse_content {
Subroutine _parse_content: not called CC=1 SCAR=6.9
306 0 my $self = shift;
307 0 my $content = shift;
308  
309 0 my $doc = $self->{parser}->parse_string($content);
310 0 $doc->documentElement();
311 }
312  
313 # This method actually does the TLS negotiation. It returns undef if
314 # it fails, and true if it succeeds, and does a tuning reset
315 # regardless. This should only be called once the peer is past the
316 # "<proceed />" phase (either it sent it or received it).
317 0 ▶sub _start_TLS {
Subroutine _start_TLS: not called CC=5 SCAR=34.0
318 0 my $self = shift;
319 0 my $session = shift;
320 0 my $res;
321  
322 # start SSL
323 0 my $sock = $session->_socket();
324 0 my %ssl_args = %{$self->{_ssl_args}};
325 0 ▶ $ssl_args{SSL_server} = $self->{_is_server} if $self->{_is_server};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_is_server"} 0 0 0
326  
327  
328 0 my $ssl_sock = IO::Socket::SSL->start_SSL($sock, %ssl_args);
329  
330 0 ▶ if ($ssl_sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ssl_sock) { } 0 0 0
331 # SSL negotation succeeded.
332 0 $session->_set_socket($ssl_sock);
333  
334 # if there is a peer cert, load its info into the session;
335 0 $session->{peer_certificate} = $ssl_sock->dump_peer_certificate();
336  
337 # normally, we remove the TLS profile itself.
338 0 delete $session->{profiles}->{$self->uri()};
339  
340 # if there is a callback, call it.
341 0 ▶ &{$self->{_callback}}($session) if $self->{_callback};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_callback"} 0 0 0
342  
343 # FIXME: normally this would be done below, but some testing has
344 # indicated that negotiation failure doesn't work the way it
345 # ought.
346 0 $session->_tuning_reset();
347  
348 0 $res = 1;
349 }
350 else {
351 0 $errstr = "SSL/TLS negotiation failed: ", &IO::Socket::SSL::errstr();
352 0 ▶ print STDERR $errstr if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
353  
354 0 $res = undef;
355 }
356  
357 # Do a tuning reset.
358 # NOTE: this must be done even if the TLS negotation failed.
359 # FIXME: some testing indicates otherwise, although the spec is clear.
360 #$session->_tuning_reset();
361  
362 0 return $res;
363 }
364  
365 =item start_TLS($session)
366  
367 This is the main routine for the client side. This will initiate a
368 request for TLS. It will return undef if it failed, setting $errstr,
369 true if it succeeded. The peer certificate info will be placed in
370 $session->{peer_certificate}.
371  
372 =cut
373  
374 0 ▶sub start_TLS {
Subroutine start_TLS: not called CC=6 SCAR=37.4
Pod documented
375 0 my $self = shift;
376 0 my $session = shift;
377  
378 0 my $mgmt_profile = $session->{mgmt_profile};
379  
380 # Start a channel for the TLS profile, piggybacking our "ready"
381 # message on the request.
382  
383 0 my %start_channel_args;
384 0 $start_channel_args{Channel} = $session->_next_channel_number();
385 0 $start_channel_args{URI} = $self->uri();
386 0 $start_channel_args{StartData} = "<ready />";
387  
388 0 my ($channel_num, $start_msg) = $mgmt_profile->send_start_channel_message
389 ($session, %start_channel_args);
390  
391 # look for the response to this request (RPY on channel zero with
392 # the same message number.). This will place those messages on the
393 # session's message queue. This will only matter if the TLS
394 # negotiation doesn't happen.
395  
396 # NOTE: this has to do a lot of stuff sort of manually, because the
397 # normally used routines will emit SEQs when we don't want, and will
398 # intercept channel zero messages, which we also don't want.
399  
400 0 my $resp;
401  
402 0 while (1) {
403 # get the next message, but don't emit SEQ frames!
404 0 $resp = $session->_recv_message(NoSEQ => 1);
405  
406 # if the message we received is the reply to our start channel
407 # request, we are done reading.
408 0 ▶ last if $resp->type() eq 'RPY' and $resp->channel_number() == 0 and
Branch: 1 branch
BranchTrueFalseTotal
if $resp->type eq "RPY" and $resp->channel_number == 0 and $resp->msgno == $start_msg->msgno 0 0 0
Condition0%0/6
$resp->type eq "RPY" and $resp->channel_number == 0 and $resp->msgno == $start_msg->msgno
!ll&&!rl&&r
000
$resp->type eq "RPY" and $resp->channel_number == 0
!ll&&!rl&&r
000
MC/DC0%0/3
$resp->type eq "RPY" and $resp->channel_number == 0 and $resp->msgno == $start_msg->msgno
$resp->type eq "RPY" $resp->channel_number == 0 $resp->msgno == $start_msg->msgno
Truth table0%0/4
A and B and C
A= $resp->type eq "RPY"B= $resp->channel_number == 0C= $resp->msgno == $start_msg->msgno
ABCresult
0XX0
10X0
1100
1111
409 $resp->msgno() == $start_msg->msgno();
410  
411 # otherwise, we send a SEQ frame ourselves.
412 0 my $channel = $session->channel($resp->channel_number());
413 0 $session->_send_seq($channel, $channel->peer_seqno());
414  
415 # if the message we got was for channel zero (but not the one we
416 # wanted) we let the mangement profile handle it. Otherwise we
417 # queue it.
418 0 ▶ if ($resp->channel_number() == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->channel_number == 0) { } 0 0 0
419 0 $mgmt_profile->handle_message($session, $resp);
420 } else {
421 0 $session->_queue_message($resp);
422 }
423 }
424  
425 # Let the management profile do its thing.
426 0 $mgmt_profile->handle_message($session, $resp);
427  
428 0 my $root = $self->_parse_content($session->{start_channel_data});
429 0 ▶ if ($root->nodeName eq "proceed") {
Branch: 1 branch
BranchTrueFalseTotal
if ($root->nodeName eq 'proceed') { } 0 0 0
430 0 return $self->_start_TLS($session);
431 }
432 else {
433 0 $errstr="non-<proceed> response: " . $session->{start_channel_data};
434 0 return undef;
435 }
436 }
437  
438 =pod
439  
440 =back
441  
442 =head1 SEE ALSO
443  
444 =over 4
445  
446 =item L<IO::Socket::SSL>
447  
448 =item L<Net::BEEP::Lite>
449  
450 =cut
451  
452 1 1;
↑ Summary