line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Crypt::Format; |
2
|
|
|
|
|
|
|
|
3
|
5
|
|
|
5
|
|
385577
|
use strict; |
|
5
|
|
|
|
|
51
|
|
|
5
|
|
|
|
|
152
|
|
4
|
5
|
|
|
5
|
|
29
|
use warnings; |
|
5
|
|
|
|
|
9
|
|
|
5
|
|
|
|
|
3228
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
our $VERSION = '0.11_01'; |
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
|
|
|
|
|
|
|
my $_LINE_SEP = "\n"; |
46
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
sub der2pem { |
48
|
4
|
|
|
4
|
0
|
1529
|
my ($der_r, $whatsit) = (\$_[0], $_[1]); |
49
|
|
|
|
|
|
|
|
50
|
4
|
100
|
|
|
|
25
|
die "Missing object type!" if !$whatsit; |
51
|
|
|
|
|
|
|
|
52
|
3
|
|
|
|
|
14
|
my $pem = _do_base64('encode', $$der_r, q<>); |
53
|
3
|
|
|
|
|
55
|
$pem = join( $_LINE_SEP, $pem =~ m<(.{1,64})>g, q<> ); |
54
|
|
|
|
|
|
|
|
55
|
3
|
|
|
|
|
24
|
substr( $pem, 0, 0, "-----BEGIN $whatsit-----$_LINE_SEP" ); |
56
|
3
|
|
|
|
|
12
|
substr( $pem, length($pem), 0, "-----END $whatsit-----" ); |
57
|
|
|
|
|
|
|
|
58
|
3
|
|
|
|
|
13
|
return $pem; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub pem2der { |
62
|
8
|
|
|
8
|
0
|
3135
|
my ($pem) = @_; |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
# Strip the first line: |
65
|
8
|
|
|
|
|
70
|
$pem =~ s<.+?[\x0d\x0a]+><>s; |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
# Strip the last line and any trailing CRs and LFs: |
68
|
8
|
|
|
|
|
452
|
$pem =~ s<[\x0d\x0a]+ [^\x0d\x0a]+? [\x0d\x0a]+?\z><>sx; |
69
|
|
|
|
|
|
|
|
70
|
8
|
|
|
|
|
27
|
return _do_base64('decode', $pem); |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
sub split_pem_chain { |
74
|
5
|
|
|
5
|
0
|
3097
|
return split m[(?<=-)[\x0d\x0a]+(?=-)], shift(); |
75
|
|
|
|
|
|
|
} |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
sub _do_base64 { |
78
|
11
|
|
|
11
|
|
28
|
my $path = "$BASE64_MODULE.pm"; |
79
|
11
|
|
|
|
|
33
|
$path =~ s<::>>g; |
80
|
|
|
|
|
|
|
|
81
|
11
|
100
|
|
|
|
43
|
_load_module($BASE64_MODULE) if !$INC{$path}; |
82
|
|
|
|
|
|
|
|
83
|
11
|
|
|
|
|
64
|
my $cr = $BASE64_MODULE->can(shift); |
84
|
11
|
|
|
|
|
141
|
return $cr->(@_); |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
|
87
|
|
|
|
|
|
|
sub _load_module { |
88
|
2
|
|
|
2
|
|
4
|
local $@; |
89
|
2
|
50
|
|
2
|
|
181
|
eval "use $_[0]; 1" or die; |
|
2
|
|
|
|
|
2100
|
|
|
2
|
|
|
|
|
3158
|
|
|
2
|
|
|
|
|
90
|
|
90
|
2
|
|
|
|
|
7
|
return $_[0]; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
sub normalize_pem { |
94
|
1
|
|
|
1
|
0
|
905
|
my ($pem) = @_; |
95
|
|
|
|
|
|
|
|
96
|
1
|
50
|
|
|
|
8
|
$pem =~ m or die "Invalid PEM: “$pem”"; |
97
|
|
|
|
|
|
|
|
98
|
1
|
|
|
|
|
5
|
return der2pem( pem2der( $pem ), $1 ); |
99
|
|
|
|
|
|
|
} |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
=pod |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
=head1 AUTHOR |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
Felipe Gasper (FELIPE) |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
=head1 REPOSITORY |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
https://github.com/FGasper/p5-Crypt-Format |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
=head1 COPYRIGHT |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
This program is free software; you can redistribute |
114
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
The full text of the license can be found in the |
117
|
|
|
|
|
|
|
LICENSE file included with this module. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
=cut |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
1; |