line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::Format; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
339701
|
use strict; |
|
5
|
|
|
|
|
49
|
|
|
5
|
|
|
|
|
144
|
|
4
|
5
|
|
|
5
|
|
26
|
use warnings; |
|
5
|
|
|
|
|
8
|
|
|
5
|
|
|
|
|
2754
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.10'; |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
our $BASE64_MODULE = 'MIME::Base64'; |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=encoding utf-8 |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
=head1 NAME |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
Crypt::Format - Conversion utilities for encryption applications |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
=head1 SYNOPSIS |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
use Crypt::Format; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
my $der = Crypt::Format::pem2der($pem); |
21
|
|
|
|
|
|
|
my $pem = Crypt::Format::der2pem($der, 'CERTIFICATE REQUEST'); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
my $good_pem = Crypt::Format::normalize_pem($weird_pem); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
# Split PEM chains such as application/pem-certificate-chain … |
26
|
|
|
|
|
|
|
my @pems = Crypt::Format::split_pem_chain($pem_chain); |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
{ |
29
|
|
|
|
|
|
|
#If, for whatever reason, you don’t like MIME::Base64, |
30
|
|
|
|
|
|
|
#then customize this. The module must have encode() and/or decode() |
31
|
|
|
|
|
|
|
#functions, depending on which of this module’s functions you use. |
32
|
|
|
|
|
|
|
# |
33
|
|
|
|
|
|
|
local $Crypt::Format::BASE64_MODULE = '..'; |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
Crypt::Format::... |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 DESCRIPTION |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
Not much more to say! This module is for simple conversions that I got |
41
|
|
|
|
|
|
|
tired of writing out. |
42
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
=cut |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
sub der2pem { |
46
|
4
|
|
|
4
|
0
|
1503
|
my ($der_r, $whatsit) = (\$_[0], $_[1]); |
47
|
|
|
|
|
|
|
|
48
|
4
|
100
|
|
|
|
29
|
die "Missing object type!" if !$whatsit; |
49
|
|
|
|
|
|
|
|
50
|
3
|
|
|
|
|
9
|
my $pem = _do_base64('encode', $$der_r); |
51
|
3
|
|
|
|
|
14
|
my $line_sep = substr($pem, -1); |
52
|
|
|
|
|
|
|
|
53
|
3
|
|
|
|
|
16
|
substr( $pem, 0, 0, "-----BEGIN $whatsit-----$line_sep" ); |
54
|
3
|
|
|
|
|
13
|
substr( $pem, length($pem), 0, "-----END $whatsit-----" ); |
55
|
|
|
|
|
|
|
|
56
|
3
|
|
|
|
|
13
|
return $pem; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
sub pem2der { |
60
|
6
|
|
|
6
|
0
|
2619
|
my ($pem) = @_; |
61
|
|
|
|
|
|
|
|
62
|
6
|
|
|
|
|
34
|
chomp $pem; |
63
|
|
|
|
|
|
|
|
64
|
6
|
|
|
|
|
51
|
$pem =~ s<.+?[\x0d\x0a]+><>s; |
65
|
6
|
|
|
|
|
226
|
$pem =~ s<[\x0d\x0a]+[^\x0d\x0a]+?\z><>s; |
66
|
|
|
|
|
|
|
|
67
|
6
|
|
|
|
|
31
|
return _do_base64('decode', $pem); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub split_pem_chain { |
71
|
5
|
|
|
5
|
0
|
3076
|
return split m[(?<=-)[\x0d\x0a]+(?=-)], shift(); |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub _do_base64 { |
75
|
9
|
|
|
9
|
|
21
|
my $path = "$BASE64_MODULE.pm"; |
76
|
9
|
|
|
|
|
32
|
$path =~ s<::>>g; |
77
|
|
|
|
|
|
|
|
78
|
9
|
100
|
|
|
|
33
|
_load_module($BASE64_MODULE) if !$INC{$path}; |
79
|
|
|
|
|
|
|
|
80
|
9
|
|
|
|
|
53
|
my $cr = $BASE64_MODULE->can(shift); |
81
|
9
|
|
|
|
|
70
|
return $cr->(@_); |
82
|
|
|
|
|
|
|
} |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub _load_module { |
85
|
2
|
|
|
2
|
|
4
|
local $@; |
86
|
2
|
50
|
|
2
|
|
134
|
eval "use $_[0]; 1" or die; |
|
2
|
|
|
|
|
905
|
|
|
2
|
|
|
|
|
1277
|
|
|
2
|
|
|
|
|
81
|
|
87
|
2
|
|
|
|
|
6
|
return $_[0]; |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
sub normalize_pem { |
91
|
1
|
|
|
1
|
0
|
816
|
my ($pem) = @_; |
92
|
|
|
|
|
|
|
|
93
|
1
|
50
|
|
|
|
8
|
$pem =~ m or die "Invalid PEM: “$pem”"; |
94
|
|
|
|
|
|
|
|
95
|
1
|
|
|
|
|
3
|
return der2pem( pem2der( $pem ), $1 ); |
96
|
|
|
|
|
|
|
} |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=pod |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
=head1 AUTHOR |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
Felipe Gasper (FELIPE) |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
=head1 REPOSITORY |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
https://github.com/FGasper/p5-Crypt-Format |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 COPYRIGHT |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
This program is free software; you can redistribute |
111
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
The full text of the license can be found in the |
114
|
|
|
|
|
|
|
LICENSE file included with this module. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
=cut |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
1; |