MatrixSSL.pm

filter: Statementstmt 90.4% 19 / 21 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 7 / 7 Podpod n/a totaltotal 92.8% 26 / 28 SCAR 0.0
CC
1
Cov
90%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« MatrixSSL/const.pm ↑ Summary MatrixSSL/Client.pm »
1 ## no critic (Capitalization)
2 package IO::Stream::MatrixSSL;
3  
4 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 10 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 10 ▶use version; our $VERSION = qv('1.1.2'); # update POD & Changes & README
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 # update DEPENDENCIES in POD & Makefile.PL & README
11 10 ▶use IO::Stream::const;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 ▶use IO::Stream::MatrixSSL::const;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 10 ▶use Crypt::MatrixSSL 1.83;
Subroutine BEGIN: called CC=1 SCAR=2.6
14  
15 use IO::Stream::MatrixSSL::Client;
16 use IO::Stream::MatrixSSL::Server;
17  
18  
19 sub DESTROY {
20 my ($self) = @_;
21 # Free memory in MatrixSSL.
22 matrixSslDeleteSession($self->{_ssl});
23 matrixSslFreeKeys($self->{_ssl_keys});
24 return;
25 }
26  
27 sub T {
28 my ($self) = @_;
29 my $m = $self->{_master};
30 $m->EVENT(0, ETOHANDSHAKE);
31 return;
32 }
33  
34 sub WRITE {
35 my ($self) = @_;
36 if (!$self->{_handshaked}) {
37 $self->{_want_write} = 1;
38 }
39 else {
40 my $m = $self->{_master};
41 my $s = substr $m->{out_buf}, $m->{out_pos}||0;
42 my $n = length $s;
43 while (length $s) {
44 my $s2 = substr $s, 0, $SSL_MAX_PLAINTEXT_LEN, q{};
45 if (matrixSslEncode($self->{_ssl}, $s2, $self->{out_buf}) < 0) {
46 $m->EVENT(0, 'matrixSslEncode');
47 return;
48 }
49 }
50 if (defined $m->{out_pos}) {
51 $m->{out_pos} += $n;
52 } else {
53 $m->{out_buf} = q{};
54 }
55 $m->{out_bytes} += $n;
56 $m->EVENT(OUT);
57 $self->{_slave}->WRITE();
58 }
59 return;
60 }
61  
62 sub EVENT { ## no critic (ProhibitExcessComplexity)
63 my ($self, $e, $err) = @_;
64 my $m = $self->{_master};
65 $e &= ~OUT;
66 if (!$self->{_handshaked}) {
67 $e &= ~SENT;
68 }
69 return if !$e && !$err;
70 if ($e & IN) {
71 $e &= ~IN;
72 while (length $self->{in_buf}) {
73 my ($error, $alertLevel, $alertDescription);
74 my $rc = matrixSslDecode($self->{_ssl}, $self->{in_buf},
75 my $buf=q{}, $error, $alertLevel, $alertDescription);
76 if ($rc == $SSL_PROCESS_DATA) {
77 $e |= IN;
78 $m->{in_buf} .= $buf;
79 $m->{in_bytes} += length $buf;
80 }
81 else {
82 $self->{out_buf} .= $buf;
83 $self->{_slave}->WRITE();
84 ## no critic (ProhibitCascadingIfElse ProhibitDeepNests)
85 if ($rc == $SSL_SUCCESS || $rc == $SSL_SEND_RESPONSE) {
86 if (!$self->{_handshaked}) {
87 if (matrixSslHandshakeIsComplete($self->{_ssl})) {
88 $self->{_handshaked} = 1;
89 undef $self->{_t};
90 if ($self->{_want_write}) {
91 $self->WRITE();
92 }
93 }
94 }
95 }
96 # WARNING After $SSL_ERROR or $SSL_ALERT {in_buf} may
97 # contain non-decoded packets. These packets will be lost,
98 # except in case user will not $stream->close() on this
99 # error AND there will be more data later (got EPOLLIN).
100 # This behaviour is ok because all ERROR/ALERT are fatal
101 # anyway (except NO_CERTIFICATE).
102 # TODO FIXME If we'll support commercial MatrixSSL we
103 # should add handling for NO_CERTIFICATE case.
104 elsif ($rc == $SSL_ERROR) {
105 $err ||= "ssl error: $SSL_alertDescription{$error}";
106 last;
107 }
108 elsif ($rc == $SSL_ALERT) {
109 if ($alertLevel == $SSL_ALERT_LEVEL_WARNING
110 && $alertDescription == $SSL_ALERT_CLOSE_NOTIFY) {
111 # Workaround MatrixSSL bug: ALERT packet doesn't removed
112 # from {in_buf}, and next matrixSslDecode() on this {in_buf}
113 # return SSL_ERROR while CLOSE_NOTIFY alert shouldn't be
114 # error at all. :(
115 # TODO Is it still needed in Crypt::MatrixSSL 1.83?
116 $self->{in_buf} = q{};
117 }
118 else {
119 $err ||= "ssl alert: $SSL_alertLevel{$alertLevel}: $SSL_alertDescription{$alertDescription}";
120 last;
121 }
122 }
123 elsif ($rc == $SSL_PARTIAL) {
124 last;
125 }
126 else {
127 $err ||= "matrixSslDecode: unexpected return code ($rc)";
128 last;
129 }
130 ## use critic
131 }
132 }
133 }
134 if ($e & RESOLVED) {
135 $m->{ip} = $self->{ip};
136 }
137 if ($e & EOF) {
138 $m->{is_eof} = $self->{is_eof};
139 if (!$self->{_handshaked}) {
140 $err ||= 'ssl handshake error: unexpected EOF';
141 }
142 }
143 if ($e & CONNECTED) {
144 $self->{_t} = EV::timer(TOHANDSHAKE, 0, $self->{_cb_t});
145 }
146 $m->EVENT($e, $err);
147 return;
148 }
149  
150  
151 1; # Magic true value required at end of module
152 __END__
153  
154 =head1 NAME
155  
156 IO::Stream::MatrixSSL - Crypt::MatrixSSL plugin for IO::Stream
157  
158  
159 =head1 VERSION
160  
161 This document describes IO::Stream::MatrixSSL version 1.1.2
162  
163  
164 =head1 SYNOPSIS
165  
166 use IO::Stream;
167 use IO::Stream::MatrixSSL;
168  
169 # SSL server
170 IO::Stream->new({
171 ...
172 plugin => [
173 ...
174 ssl => IO::Stream::MatrixSSL::Server->new({
175 crt => 'mysrv.crt',
176 key => 'mysrv.key',
177 }),
178 ...
179 ],
180 });
181  
182 # SSL client
183 IO::Stream->new({
184 ...
185 plugin => [
186 ...
187 ssl => IO::Stream::MatrixSSL::Client->new({
188 cb => \&validate,
189 }),
190 ...
191 ],
192 });
193 sub validate {
194 my ($certs, $ssl, $stream) = ($_[0], @{ $_[1] });
195 # check cert, for ex.: $certs->[0]{subject}{commonName}
196 return 0;
197 }
198  
199  
200 =head1 DESCRIPTION
201  
202 This module is plugin for IO::Stream which allow you to use SSL (on both
203 client and server streams).
204  
205  
206 =head1 INTERFACE
207  
208 =over
209  
210 =item IO::Stream::MatrixSSL::Client->new(\%opt)
211  
212 Create and return new IO::Stream plugin object.
213  
214 There two optional parameters:
215  
216 =over
217  
218 =item cb
219  
220 This should be CODE ref to your callback, which should check server
221 certificate. Callback will be called with two parameters: HASH ref with
222 certificate details, and ARRAY ref with two elements:
223 IO::Stream::MatrixSSL::Client object and IO::Stream object (see L<SYNOPSIS>
224 for example).
225  
226 Callback should return a number >=0 if this certificate is acceptable,
227 and we can continue with SSL handshake, or number <0 if this certificate
228 isn't acceptable and we should interrupt this connection and return error
229 to IO::Stream user callback. If this function will throw exception, it will
230 be handled just as return(-1).
231  
232 Hash with certificate details will looks this way:
233  
234 verified => $verified,
235 notBefore => $notBefore,
236 notAfter => $notAfter,
237 subjectAltName => {
238 dns => $dns,
239 uri => $uri,
240 email => $email,
241 },
242 subject => {
243 country => $country,
244 state => $state,
245 locality => $locality,
246 organization => $organization,
247 orgUnit => $orgUnit,
248 commonName => $commonName,
249 },
250 issuer => {
251 country => $country,
252 state => $state,
253 locality => $locality,
254 organization => $organization,
255 orgUnit => $orgUnit,
256 commonName => $commonName,
257 },
258  
259 where all values are just strings except these:
260  
261 $verified
262 Status of cetrificate RSA signature check:
263 -1 signature is wrong
264 1 signature is correct
265 $notBefore
266 $notAfter
267 Time period when certificate is active, in format
268 YYYYMMDDHHMMSSZ (for ex.: 20061231235959Z)
269  
270 =item trusted_CA
271  
272 This should be name of file (or files) with allowed CA certificates,
273 required to check RSA signature of server certificate. This module
274 installed with such file, so chances are you doesn't need to change
275 default {trusted_CA} value if you just wanna connect to https servers.
276  
277 There may be many files listed in {trusted_CA}, separated by ";".
278 Each file can contain many CA certificates.
279  
280 =back
281  
282 =item IO::Stream::MatrixSSL::Server->new(\%opt)
283  
284 Create and return new IO::Stream plugin object.
285  
286 There at least two required parameters: {crt} and {key}. If {key} is
287 encrypted, then one more parameter required: {pass}.
288  
289 =over
290  
291 =item crt
292  
293 This should be name of file (or files) with server certificate (or chain
294 of certicates). See above {trusted_CA} about format of this parameter.
295  
296 =item key
297  
298 This should be name of file with private key file for server certicate
299 (file should be in PEM format).
300  
301 =item pass
302  
303 If file with private key is encrypted, you should provide password for
304 decrypting it in this parameter.
305  
306 =back
307  
308 =back
309  
310  
311 =head1 DIAGNOSTICS
312  
313 =head2 IO::Stream::MatrixSSL::Client
314  
315 =over
316  
317 =item C<< matrixSslReadKeys: wrong {trusted_CA}? >>
318  
319 File with trusted CA certificates can't be read. If you provide own file,
320 there some problem with it. If you doesn't provided own file, then probably
321 this module was installed incorrectly - there should be default file with
322 trusted CA certificates (taken from Mozilla) installed with module.
323  
324 =item C<< matrixSslNewSession: wrong {_ssl_session}? >>
325  
326 This error shouldn't happens, it mean there some bug in this module,
327 or Crypt::MatrixSSL, or MatrixSSL itself.
328  
329 =item C<< matrixSslEncodeClientHello >>
330  
331 This error shouldn't happens, it mean there some bug in this module,
332 or Crypt::MatrixSSL, or MatrixSSL itself.
333  
334 =back
335  
336 =head2 IO::Stream::MatrixSSL::Server
337  
338 =over
339  
340 =item C<< {crt} and {key} required >>
341  
342 You can't create SSL server without certificate and key files.
343  
344 =item C<< matrixSslReadKeys: wrong {crt}, {key} or {pass}? >>
345  
346 Certificate and key files you provided can't be read by MatrixSSL,
347 or may be you used wrong password for key file.
348  
349 =item C<< matrixSslNewSession >>
350  
351 This error shouldn't happens, it mean there some bug in this module,
352 or Crypt::MatrixSSL, or MatrixSSL itself.
353  
354 =back
355  
356  
357 =head1 CONFIGURATION AND ENVIRONMENT
358  
359 IO::Stream::MatrixSSL requires no configuration files or environment variables.
360  
361  
362 =head1 DEPENDENCIES
363  
364 L<IO::Stream>,
365 L<Crypt::MatrixSSL> 1.83,
366 L<File::ShareDir>.
367  
368  
369 =head1 INCOMPATIBILITIES
370  
371 None reported.
372  
373  
374 =head1 BUGS AND LIMITATIONS
375  
376 No bugs have been reported.
377  
378 Please report any bugs or feature requests to author, or
379 C<bug-io-stream-matrixssl@rt.cpan.org>, or through the web interface at
380 L<http://rt.cpan.org>.
381  
382  
383 =head1 AUTHOR
384  
385 Alex Efros C<< <powerman-asdf@ya.ru> >>
386  
387  
388 =head1 LICENSE AND COPYRIGHT
389  
390 Copyright (c) 2008, Alex Efros C<< <powerman-asdf@ya.ru> >>. All rights reserved.
391  
392 MatrixSSL is distrubed under the GNU Public License.
393  
394 Crypt::MatrixSSL uses MatrixSSL, and so inherits the same license.
395  
396 IO::Stream::MatrixSSL uses Crypt::MatrixSSL, and so inherits the same license.
397  
398 ... GPL is a virus, avoid it whenever possible!
399  
400  
401 =head1 DISCLAIMER OF WARRANTY
402  
403 BECAUSE THIS SOFTWARE IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
404 FOR THE SOFTWARE, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
405 OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
406 PROVIDE THE SOFTWARE "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
407 EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
408 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE
409 ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE SOFTWARE IS WITH
410 YOU. SHOULD THE SOFTWARE PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL
411 NECESSARY SERVICING, REPAIR, OR CORRECTION.
412  
413 IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
414 WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
415 REDISTRIBUTE THE SOFTWARE AS PERMITTED BY THE ABOVE LICENCE, BE
416 LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL,
417 OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE
418 THE SOFTWARE (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING
419 RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A
420 FAILURE OF THE SOFTWARE TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
421 SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
422 SUCH DAMAGES.
« MatrixSSL/const.pm ↑ Summary MatrixSSL/Client.pm »