File Coverage

blib/lib/Protocol/TLS/Utils.pm
Criterion Covered Total %
statement 31 31 100.0
branch 6 12 50.0
condition n/a
subroutine 7 7 100.0
pod 0 2 0.0
total 44 52 84.6


line stmt bran cond sub pod time code
1             package Protocol::TLS::Utils;
2 2     2   12 use strict;
  2         4  
  2         57  
3 2     2   10 use warnings;
  2         3  
  2         55  
4 2     2   11 use Carp;
  2         2  
  2         132  
5 2     2   796 use MIME::Base64;
  2         705  
  2         133  
6 2     2   12 use Exporter qw(import);
  2         4  
  2         675  
7             our @EXPORT_OK = qw(load_cert load_priv_key);
8              
9             sub load_cert {
10 2     2 0 5 my ($file) = @_;
11 2 50       7 croak "specify cert_file path" unless defined $file;
12              
13 2         6 local $/;
14 2 50       64 open my $fh, '<', $file or croak "opening cert_file error: $!";
15              
16             # TODO: multiple certs
17 2         207 my ($cert) = (
18             <$fh> =~ /^-----BEGIN\x20CERTIFICATE-----\r?\n
19             (.+?\r?\n)
20             -----END\x20CERTIFICATE-----\r?\n/msx
21             );
22 2         14 close $fh;
23 2 50       6 croak "Certificate must be in PEM format" unless $cert;
24 2         35 decode_base64($cert);
25             }
26              
27             sub load_priv_key {
28 2     2 0 4 my ($file) = @_;
29 2 50       6 croak "specify key_file path" unless defined $file;
30              
31 2         6 local $/;
32 2 50       49 open my $fh, '<', $file or croak "opening key_file error: $!";
33 2         276 my ($key) = (
34             <$fh> =~ /^-----BEGIN\x20(?:RSA\x20)?PRIVATE\x20KEY-----\r?\n
35             (.+?\r?\n)
36             -----END\x20(?:RSA\x20)?PRIVATE\x20KEY-----\r?\n/msx
37             );
38 2         15 close $fh;
39 2 50       7 croak "Private key must be in PEM format" unless $key;
40 2         31 decode_base64($key);
41             }
42              
43             1