line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Authen::HTTP::Signature::Method::RSA; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
12
|
use strict; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
61
|
|
4
|
2
|
|
|
2
|
|
11
|
use warnings; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
57
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
57
|
use 5.010; |
|
2
|
|
|
|
|
8
|
|
7
|
|
|
|
|
|
|
|
8
|
2
|
|
|
2
|
|
12
|
use Moo; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
21
|
|
9
|
2
|
|
|
2
|
|
2457
|
use Crypt::OpenSSL::RSA; |
|
2
|
|
|
|
|
18191
|
|
|
2
|
|
|
|
|
86
|
|
10
|
2
|
|
|
2
|
|
1625
|
use MIME::Base64 qw(encode_base64 decode_base64); |
|
2
|
|
|
|
|
1321
|
|
|
2
|
|
|
|
|
159
|
|
11
|
2
|
|
|
2
|
|
18
|
use Carp qw(confess); |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
868
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
Crypt::HTTP::Signature::Method::RSA - Compute digest using asymmetric keys |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
=head1 PURPOSE |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
This class uses asymmetric RSA keys to compute a HTTP signature digest. It implements the |
24
|
|
|
|
|
|
|
RSA-SHA{1, 256, 512} algorithms. |
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
=head1 ATTRIBUTES |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
=over |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=item key |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
Holds the key data. This should be a string that L can instantiate into |
33
|
|
|
|
|
|
|
a private or public key. |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
If the operation is C, then this attribute must hold a private key. |
36
|
|
|
|
|
|
|
In other words, the string this attribute holds should start with |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
-----BEGIN RSA PRIVATE KEY----- |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
If the operation is C, then this attribute must hold a public key. |
42
|
|
|
|
|
|
|
In other words, the string this attribute holds should start with |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
-----BEGIN PUBLIC KEY----- |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=back |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
=cut |
49
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
has 'key' => ( |
51
|
|
|
|
|
|
|
is => 'ro', |
52
|
|
|
|
|
|
|
required => 1, |
53
|
|
|
|
|
|
|
); |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
=over |
56
|
|
|
|
|
|
|
|
57
|
|
|
|
|
|
|
=item data |
58
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
Holds the data to be signed or verified. This is typically the C attribute |
60
|
|
|
|
|
|
|
from L. Read-only. Required. |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
=back |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=cut |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
has 'data' => ( |
67
|
|
|
|
|
|
|
is => 'ro', |
68
|
|
|
|
|
|
|
required => 1, |
69
|
|
|
|
|
|
|
); |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=over |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
=item hash |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
Digest algorithm. Read-only. Required. |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=back |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=cut |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
has 'hash' => ( |
82
|
|
|
|
|
|
|
is => 'ro', |
83
|
|
|
|
|
|
|
required => 1, |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head1 METHODS |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=over |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item sign() |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Signs C using C. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
Returns a base 64 encoded signature. |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
=back |
97
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=cut |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub sign { |
101
|
2
|
|
|
2
|
1
|
6
|
my $self = shift; |
102
|
|
|
|
|
|
|
|
103
|
2
|
|
|
|
|
217
|
my $key = Crypt::OpenSSL::RSA->new_private_key($self->key); |
104
|
2
|
50
|
|
|
|
14
|
confess "I don't have a key!" unless $key; |
105
|
|
|
|
|
|
|
|
106
|
2
|
|
|
|
|
5
|
$self->_set_digest($key); |
107
|
|
|
|
|
|
|
|
108
|
2
|
|
|
|
|
2007
|
my $s = $key->sign($self->data); |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
# pass empty string as second arg to prevent line breaks in stream |
111
|
2
|
|
|
|
|
37
|
return encode_base64($s, ""); |
112
|
|
|
|
|
|
|
} |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub _set_digest { |
115
|
4
|
|
|
4
|
|
7
|
my $self = shift; |
116
|
4
|
|
|
|
|
8
|
my $key = shift; |
117
|
|
|
|
|
|
|
|
118
|
4
|
50
|
|
|
|
28
|
if ( $self->hash =~ /sha1/ ) { |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
119
|
0
|
|
|
|
|
0
|
$key->use_sha1_hash(); |
120
|
|
|
|
|
|
|
} |
121
|
|
|
|
|
|
|
elsif ( $self->hash =~ /sha256/ ) { |
122
|
4
|
|
|
|
|
16
|
$key->use_sha256_hash(); |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
elsif ( $self->hash =~ /sha512/ ) { |
125
|
0
|
|
|
|
|
0
|
$key->use_sha512_hash(); |
126
|
|
|
|
|
|
|
} |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=over |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item verify() |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
This method validates a signature was generated by a specific private key by using the corresponding |
134
|
|
|
|
|
|
|
public key. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
Takes a Base64 encoded signature string as input. |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
Returns a boolean. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=back |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
=cut |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub verify { |
145
|
2
|
|
|
2
|
1
|
4
|
my $self = shift; |
146
|
2
|
|
|
|
|
4
|
my $signature = shift; |
147
|
|
|
|
|
|
|
|
148
|
2
|
50
|
|
|
|
6
|
confess "I don't have a signature to verify!" unless $signature; |
149
|
|
|
|
|
|
|
|
150
|
2
|
|
|
|
|
41
|
my $key = Crypt::OpenSSL::RSA->new_public_key($self->key); |
151
|
2
|
50
|
|
|
|
732
|
confess "I don't have a key!" unless $key; |
152
|
|
|
|
|
|
|
|
153
|
2
|
|
|
|
|
7
|
$self->_set_digest($key); |
154
|
|
|
|
|
|
|
|
155
|
2
|
|
|
|
|
214
|
return $key->verify($self->data, decode_base64( $signature )); |
156
|
|
|
|
|
|
|
} |
157
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
=head1 SEE ALSO |
159
|
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
L |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
1; |