line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Chef::Encoder; |
2
|
|
|
|
|
|
|
$Chef::Encoder::VERSION = 1.1; |
3
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
=pod |
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Chef::Encoder |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
1.1 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=head1 SYNOPSIS |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
use Chef::Encoder; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my $obj = new Chef::Encoder |
19
|
|
|
|
|
|
|
( |
20
|
|
|
|
|
|
|
'data' => $data |
21
|
|
|
|
|
|
|
, 'private_key_file' => ../data/private_key.pem |
22
|
|
|
|
|
|
|
); |
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
$obj->pki->sign ( 'data' => $data ); |
25
|
|
|
|
|
|
|
$obj->sha1->digest ( 'data' => $data ); |
26
|
|
|
|
|
|
|
$obj->base64->encode( 'data' => $data ); |
27
|
|
|
|
|
|
|
$obj->base64->decode( 'data' => $data ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=head1 DESCRIPTION |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
This module imiplements methods that perform the encoding and decoding and signing of the header |
32
|
|
|
|
|
|
|
once you load this module it will inturn load three classes |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=over |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
=item * Chef::Encode::pki |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=item * Chef::Encode::sha1 |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=item * Chef::Encode::base64 |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=back |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
=begin html |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
+----------------+ |
47
|
|
|
|
|
|
|
| Chef::Encode | |
48
|
|
|
|
|
|
|
+---------------------+ |
49
|
|
|
|
|
|
|
| Chef::Encode::pki | |
50
|
|
|
|
|
|
|
+----------------------+ |
51
|
|
|
|
|
|
|
| Chef::Encode::sha1 | |
52
|
|
|
|
|
|
|
+------------------------+ |
53
|
|
|
|
|
|
|
| Chef::Encode::base64 | |
54
|
|
|
|
|
|
|
+------------------------+ |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
=end html |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=head1 METHODS |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=head2 Chef::Encoder( %params ) |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
return new object initialzied as per %params. |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 private_key_file |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
returns $private_key_file |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
=head2 private_key |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
returns $private_key |
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
=head2 data |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
returns $data |
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
=cut |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
my @base; |
80
|
|
|
|
|
|
|
BEGIN { |
81
|
5
|
|
|
5
|
|
133907
|
use File::Basename qw { dirname }; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
323
|
|
82
|
5
|
|
|
5
|
|
32
|
use File::Spec::Functions qw { splitdir rel2abs }; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
424
|
|
83
|
5
|
|
|
5
|
|
403
|
@base = ( splitdir ( rel2abs ( dirname(__FILE__) ) ) ); |
84
|
5
|
|
|
|
|
155
|
pop @base; #Chef |
85
|
5
|
|
|
|
|
2248
|
push @INC, '/', @base; |
86
|
|
|
|
|
|
|
}; |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
sub new { |
89
|
3
|
|
|
3
|
0
|
1083
|
my $class = shift; |
90
|
3
|
|
|
|
|
9
|
my $param = {@_}; |
91
|
3
|
|
|
|
|
8
|
my $self = {}; |
92
|
3
|
|
|
|
|
8
|
my $_tmp = pop @base; #lib; |
93
|
3
|
|
|
|
|
9
|
bless $self, $class; |
94
|
|
|
|
|
|
|
|
95
|
3
|
50
|
|
|
|
24
|
$self->{'data' } = $param->{'data'} if defined $param->{'data'}; |
96
|
3
|
|
|
|
|
45
|
$self->{'private_key_file'} = join '/' , @base , 'data', 'private_key.pem'; |
97
|
3
|
50
|
|
|
|
18
|
$self->{'private_key_file'} = $param->{'private_key_file'} if defined $param->{'private_key_file'}; |
98
|
|
|
|
|
|
|
|
99
|
3
|
|
|
|
|
8
|
push @base , $_tmp; |
100
|
|
|
|
|
|
|
|
101
|
3
|
|
|
|
|
18
|
return $self; |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub private_key_file { |
105
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
106
|
0
|
|
|
|
|
0
|
return $self->{'private_key_file'}; |
107
|
|
|
|
|
|
|
} |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub private_key { |
110
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
111
|
0
|
|
|
|
|
0
|
return $self->{'private_key'}; |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub execute { |
115
|
2
|
|
|
2
|
0
|
4
|
my ($self , $command) = (@_); |
116
|
2
|
|
|
|
|
11
|
my $output = undef; |
117
|
2
|
|
|
|
|
5
|
eval { |
118
|
2
|
|
|
|
|
73199
|
$output = `$command`; |
119
|
2
|
|
|
|
|
72
|
chomp($output); |
120
|
|
|
|
|
|
|
}; |
121
|
2
|
50
|
|
|
|
177
|
return ($@) ? undef : $output; |
122
|
|
|
|
|
|
|
} |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
sub data |
125
|
0
|
0
|
|
0
|
1
|
0
|
{ $_[0]->{'data'} = $_[1] if defined $_[1]; return $_[0]->{'data'}; } |
|
0
|
|
|
|
|
0
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=pod |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=head2 pki ( %params) |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
loads L class and returns new object of class L |
132
|
|
|
|
|
|
|
it accepts ( 'private_key_file' => $private_key_file , 'private_key' => $private_key ) |
133
|
|
|
|
|
|
|
if none is provided it will try to use the values initialized from parent class L |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
=head3 NAME |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
Chef::Encoder::pki |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
=head3 VERSION |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
1.0 |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
=head3 DESCRIPTION |
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
This class provides siging request as per private key specified. |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
=head3 METHODS |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
=head4 B |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
returns signed data based on the private_key_file or privete_key |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=cut |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
#----------------------------# |
156
|
|
|
|
|
|
|
# class Chef::Encoder::pki # |
157
|
|
|
|
|
|
|
#----------------------------# |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
sub pki { |
160
|
0
|
|
|
0
|
1
|
0
|
my $class = shift; |
161
|
0
|
|
|
|
|
0
|
my $param = {@_}; |
162
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
package Chef::Encoder::pki; |
164
|
5
|
|
|
5
|
|
4266
|
use parent qw{ Chef::Encoder }; |
|
5
|
|
|
|
|
884
|
|
|
5
|
|
|
|
|
34
|
|
165
|
5
|
|
|
5
|
|
5099
|
use Crypt::OpenSSL::RSA; |
|
5
|
|
|
|
|
41955
|
|
|
5
|
|
|
|
|
297
|
|
166
|
5
|
|
|
5
|
|
20100
|
use File::Slurp; |
|
5
|
|
|
|
|
96483
|
|
|
5
|
|
|
|
|
3027
|
|
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
0
|
my $self = {}; |
169
|
0
|
|
|
|
|
0
|
bless $self, qw { Chef::Encoder::pki }; |
170
|
|
|
|
|
|
|
|
171
|
0
|
0
|
|
|
|
0
|
$self->{'private_key_file'} = (defined($param->{'private_key_file'}))? |
172
|
|
|
|
|
|
|
$param->{'private_key_file'} : |
173
|
|
|
|
|
|
|
$class->private_key_file; |
174
|
0
|
0
|
|
|
|
0
|
$self->{'private_key'} = (defined($param->{'private_key'}))? |
175
|
|
|
|
|
|
|
$param->{'private_key'} : |
176
|
|
|
|
|
|
|
$class->private_key; |
177
|
0
|
|
|
|
|
0
|
return $self; |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
sub sign { |
180
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
181
|
0
|
|
|
|
|
0
|
my $param = {@_}; |
182
|
0
|
|
|
|
|
0
|
my $data = $param->{'data'}; |
183
|
0
|
|
|
|
|
0
|
my $private_key = $self->private_key_file; |
184
|
|
|
|
|
|
|
|
185
|
0
|
|
|
|
|
0
|
return $self->execute("echo -n '$data'| openssl rsautl -sign -inkey $private_key| openssl enc -base64"); |
186
|
|
|
|
|
|
|
} |
187
|
|
|
|
|
|
|
|
188
|
|
|
|
|
|
|
sub rsa_private { |
189
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
190
|
0
|
|
|
|
|
0
|
my $param = {@_}; |
191
|
0
|
0
|
|
|
|
0
|
$self->{'private_key'} = read_file( $param->{'private_key_file'} ) if |
192
|
|
|
|
|
|
|
defined( $param->{'private_key_file'} ) ; |
193
|
|
|
|
|
|
|
|
194
|
0
|
|
|
|
|
0
|
my $_openssl_rsa_obj; |
195
|
0
|
|
|
|
|
0
|
eval { |
196
|
0
|
0
|
|
|
|
0
|
$_openssl_rsa_obj = Crypt::OpenSSL::RSA->new_private_key( |
197
|
|
|
|
|
|
|
defined( $param->{'private_key'} ) ? |
198
|
|
|
|
|
|
|
$param->{'private_key'} : |
199
|
|
|
|
|
|
|
$self->private_key |
200
|
|
|
|
|
|
|
); |
201
|
|
|
|
|
|
|
}; |
202
|
0
|
0
|
|
|
|
0
|
return ($@)? $self : $_openssl_rsa_obj; |
203
|
|
|
|
|
|
|
} |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
sub private_key { |
206
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
207
|
|
|
|
|
|
|
|
208
|
0
|
0
|
|
|
|
0
|
if( !defined( $self->{'private_key'} ) ){ |
209
|
0
|
|
|
|
|
0
|
$self->{'private_key'} = read_file( $self->private_key_file ); |
210
|
|
|
|
|
|
|
} |
211
|
|
|
|
|
|
|
|
212
|
0
|
|
|
|
|
0
|
return $self->{'private_key'}; |
213
|
|
|
|
|
|
|
} |
214
|
|
|
|
|
|
|
|
215
|
|
|
|
|
|
|
sub private_key_file { |
216
|
0
|
|
|
0
|
|
0
|
my $self = shift; |
217
|
0
|
|
|
|
|
0
|
return $self->{'private_key_file'}; |
218
|
|
|
|
|
|
|
} |
219
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
}# package pki ends |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=pod |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=head2 sha1 ( %params) |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
loads L class and returns new object of class L |
227
|
|
|
|
|
|
|
it accepts ( 'data' => $data ) |
228
|
|
|
|
|
|
|
if none is provided it will try to use the values initialized from parent class L |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
=head3 NAME |
231
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
Chef::Encoder::sha1 |
233
|
|
|
|
|
|
|
|
234
|
|
|
|
|
|
|
=head3 VERSION |
235
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
1.0 |
237
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
=head3 DESCRIPTION |
239
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
This class provides sha1 digest of the data initialized |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=head3 METHODS |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
=head4 B |
245
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
it accepts data as parameter $obj->digest( 'data' => $data ) |
247
|
|
|
|
|
|
|
returns sha1 digest in binary encoded with base64 of the data passed. |
248
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
=cut |
250
|
|
|
|
|
|
|
|
251
|
|
|
|
|
|
|
#----------------------------# |
252
|
|
|
|
|
|
|
# class Chef::Encoder::sha1 # |
253
|
|
|
|
|
|
|
#----------------------------# |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
sub sha1 { |
256
|
2
|
|
|
2
|
1
|
4
|
my $class = shift; |
257
|
2
|
|
|
|
|
5
|
my $param = {@_}; |
258
|
|
|
|
|
|
|
|
259
|
|
|
|
|
|
|
package Chef::Encoder::sha1; |
260
|
|
|
|
|
|
|
|
261
|
5
|
|
|
5
|
|
54
|
use parent qw { Chef::Encoder }; |
|
5
|
|
|
|
|
11
|
|
|
5
|
|
|
|
|
39
|
|
262
|
|
|
|
|
|
|
|
263
|
2
|
|
|
|
|
5
|
my $self = {}; |
264
|
2
|
|
|
|
|
7
|
bless $self, qw { Chef::Encoder::sha1 }; |
265
|
2
|
|
|
|
|
13
|
return $self; |
266
|
|
|
|
|
|
|
|
267
|
|
|
|
|
|
|
sub digest { |
268
|
2
|
|
|
2
|
|
4
|
my $self = shift; |
269
|
2
|
|
|
|
|
25
|
my $param = {@_}; |
270
|
2
|
|
|
|
|
6
|
my $data = $param->{'data'}; |
271
|
|
|
|
|
|
|
# return undef unless defined $data; |
272
|
2
|
|
|
|
|
25
|
return $self->execute("echo -n '$data'| openssl dgst -sha1 -binary| openssl enc -base64"); |
273
|
|
|
|
|
|
|
} |
274
|
|
|
|
|
|
|
|
275
|
|
|
|
|
|
|
}#sha1 package ends |
276
|
|
|
|
|
|
|
|
277
|
|
|
|
|
|
|
=pod |
278
|
|
|
|
|
|
|
|
279
|
|
|
|
|
|
|
=head2 base64 ( %params) |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
loads L class and returns new object of class L |
282
|
|
|
|
|
|
|
it accepts ( 'data' => $data ) |
283
|
|
|
|
|
|
|
if none is provided it will try to use the values initialized from parent class L |
284
|
|
|
|
|
|
|
|
285
|
|
|
|
|
|
|
=head3 NAME |
286
|
|
|
|
|
|
|
|
287
|
|
|
|
|
|
|
Chef::Encoder::base64 |
288
|
|
|
|
|
|
|
|
289
|
|
|
|
|
|
|
=head3 VERSION |
290
|
|
|
|
|
|
|
|
291
|
|
|
|
|
|
|
1.0 |
292
|
|
|
|
|
|
|
|
293
|
|
|
|
|
|
|
=head3 DESCRIPTION |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
This class provides base64 encoding and ecoding functionality |
296
|
|
|
|
|
|
|
|
297
|
|
|
|
|
|
|
=head3 METHODS |
298
|
|
|
|
|
|
|
|
299
|
|
|
|
|
|
|
=head4 B |
300
|
|
|
|
|
|
|
|
301
|
|
|
|
|
|
|
it accepts data as parameter $obj->encode( 'data' => $data ) |
302
|
|
|
|
|
|
|
returns base64 encoded value of data |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
=head4 B |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
it accepts data as parameter $obj->decode( 'data' => $data ) |
307
|
|
|
|
|
|
|
returns base64 decoded value of data |
308
|
|
|
|
|
|
|
|
309
|
|
|
|
|
|
|
=cut |
310
|
|
|
|
|
|
|
|
311
|
|
|
|
|
|
|
#-------------------------------# |
312
|
|
|
|
|
|
|
# class Chef::Encoder::base64 # |
313
|
|
|
|
|
|
|
#-------------------------------# |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
sub base64 { |
316
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
317
|
0
|
|
|
|
|
|
my $param ={@_}; |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
package Chef::Encoder::base64; |
320
|
5
|
|
|
5
|
|
1371
|
use parent qw { Chef::Encoder }; |
|
5
|
|
|
|
|
10
|
|
|
5
|
|
|
|
|
99
|
|
321
|
|
|
|
|
|
|
|
322
|
0
|
|
|
|
|
|
my $self = {}; |
323
|
0
|
|
|
|
|
|
bless $self , qw{ Chef::Encoder::base64 }; |
324
|
0
|
|
|
|
|
|
return $self; |
325
|
|
|
|
|
|
|
|
326
|
|
|
|
|
|
|
sub encode { |
327
|
0
|
|
|
0
|
|
|
my $self = shift; |
328
|
0
|
|
|
|
|
|
my $param = {@_}; |
329
|
0
|
|
|
|
|
|
my $data = $param->{'data'}; |
330
|
0
|
0
|
|
|
|
|
return undef unless defined $data; |
331
|
0
|
|
|
|
|
|
return $self->execute("echo '$data'| openssl enc -base64"); |
332
|
|
|
|
|
|
|
} |
333
|
|
|
|
|
|
|
|
334
|
|
|
|
|
|
|
sub decode { |
335
|
0
|
|
|
0
|
|
|
my $self = shift; |
336
|
0
|
|
|
|
|
|
my $param = {@_}; |
337
|
0
|
0
|
|
|
|
|
return undef unless defined $data; |
338
|
0
|
|
|
|
|
|
return decode_base64( $param->{'data'} ); |
339
|
|
|
|
|
|
|
} |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
}# base64 package end |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
|
344
|
|
|
|
|
|
|
1; |
345
|
|
|
|
|
|
|
|
346
|
|
|
|
|
|
|
=head1 KNOWN BUGS |
347
|
|
|
|
|
|
|
|
348
|
|
|
|
|
|
|
=head1 SUPPORT |
349
|
|
|
|
|
|
|
|
350
|
|
|
|
|
|
|
open a github ticket or email comments to Bhavin Patel |
351
|
|
|
|
|
|
|
|
352
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
353
|
|
|
|
|
|
|
|
354
|
|
|
|
|
|
|
This Software is free to use , licensed under : The Artisic License 2.0 (GPL Compatible) |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
=cut |