| 1 | package Authen::HTTP::Signature::Method::RSA; | ||||||||||||||||||
| 2 | |||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | 2 | ▶ | use 5.010; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 2 | ▶ | use Moo; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 9 | 2 | ▶ | use Crypt::OpenSSL::RSA; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 10 | 2 | ▶ | use MIME::Base64 qw(encode_base64 decode_base64); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 11 | 2 | ▶ | use Carp qw(confess); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 12 | |||||||||||||||||||
| 13 | =head1 NAME | ||||||||||||||||||
| 14 | |||||||||||||||||||
| 15 | Crypt::HTTP::Signature::Method::RSA - Compute digest using asymmetric keys | ||||||||||||||||||
| 16 | |||||||||||||||||||
| 17 | =cut | ||||||||||||||||||
| 18 | |||||||||||||||||||
| 19 | 2 | 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<Crypt::OpenSSL::RSA> can instantiate into | ||||||||||||||||||
| 33 | a private or public key. | ||||||||||||||||||
| 34 | |||||||||||||||||||
| 35 | If the operation is C<sign()>, 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<verify()>, 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 | 2 | 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<signing_string> attribute | ||||||||||||||||||
| 60 | from L<Authen::HTTP::Signature>. Read-only. Required. | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | =back | ||||||||||||||||||
| 63 | |||||||||||||||||||
| 64 | =cut | ||||||||||||||||||
| 65 | |||||||||||||||||||
| 66 | 2 | 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 | 2 | has 'hash' => ( | |||||||||||||||||
| 82 | is => 'ro', | ||||||||||||||||||
| 83 | required => 1, | ||||||||||||||||||
| 84 | ); | ||||||||||||||||||
| 85 | |||||||||||||||||||
| 86 | =head1 METHODS | ||||||||||||||||||
| 87 | |||||||||||||||||||
| 88 | =over | ||||||||||||||||||
| 89 | |||||||||||||||||||
| 90 | =item sign() | ||||||||||||||||||
| 91 | |||||||||||||||||||
| 92 | Signs C<data> using C<key>. | ||||||||||||||||||
| 93 | |||||||||||||||||||
| 94 | Returns a base 64 encoded signature. | ||||||||||||||||||
| 95 | |||||||||||||||||||
| 96 | =back | ||||||||||||||||||
| 97 | |||||||||||||||||||
| 98 | =cut | ||||||||||||||||||
| 99 | |||||||||||||||||||
| 100 | 2 | ▶ | sub sign { | ||||||||||||||||
|
Subroutine
sign: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||
| 101 | 2 | my $self = shift; | |||||||||||||||||
| 102 | |||||||||||||||||||
| 103 | 2 | my $key = Crypt::OpenSSL::RSA->new_private_key($self->key); | |||||||||||||||||
| 104 | 2 | ▶ | confess "I don't have a key!" unless $key; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 105 | |||||||||||||||||||
| 106 | 2 | $self->_set_digest($key); | |||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | 2 | my $s = $key->sign($self->data); | |||||||||||||||||
| 109 | |||||||||||||||||||
| 110 | # pass empty string as second arg to prevent line breaks in stream | ||||||||||||||||||
| 111 | 2 | return encode_base64($s, ""); | |||||||||||||||||
| 112 | } | ||||||||||||||||||
| 113 | |||||||||||||||||||
| 114 | 4 | ▶ | sub _set_digest { | ||||||||||||||||
|
Subroutine
_set_digest: called CC=4 SCAR=17.9
| |||||||||||||||||||
| 115 | 4 | my $self = shift; | |||||||||||||||||
| 116 | 4 | my $key = shift; | |||||||||||||||||
| 117 | |||||||||||||||||||
| 118 | 4 | ▶ | if ( $self->hash =~ /sha1/ ) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 119 | 0 | $key->use_sha1_hash(); | |||||||||||||||||
| 120 | } | ||||||||||||||||||
| 121 | elsif ( $self->hash =~ /sha256/ ) { | ||||||||||||||||||
| 122 | 4 | $key->use_sha256_hash(); | |||||||||||||||||
| 123 | } | ||||||||||||||||||
| 124 | elsif ( $self->hash =~ /sha512/ ) { | ||||||||||||||||||
| 125 | 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 | 2 | ▶ | sub verify { | ||||||||||||||||
|
Subroutine
verify: called CC=3 SCAR=11.2
Pod
documented
| |||||||||||||||||||
| 145 | 2 | my $self = shift; | |||||||||||||||||
| 146 | 2 | my $signature = shift; | |||||||||||||||||
| 147 | |||||||||||||||||||
| 148 | 2 | ▶ | confess "I don't have a signature to verify!" unless $signature; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 149 | |||||||||||||||||||
| 150 | 2 | my $key = Crypt::OpenSSL::RSA->new_public_key($self->key); | |||||||||||||||||
| 151 | 2 | ▶ | confess "I don't have a key!" unless $key; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 152 | |||||||||||||||||||
| 153 | 2 | $self->_set_digest($key); | |||||||||||||||||
| 154 | |||||||||||||||||||
| 155 | 2 | return $key->verify($self->data, decode_base64( $signature )); | |||||||||||||||||
| 156 | } | ||||||||||||||||||
| 157 | |||||||||||||||||||
| 158 | =head1 SEE ALSO | ||||||||||||||||||
| 159 | |||||||||||||||||||
| 160 | L<Authen::HTTP::Signature> | ||||||||||||||||||
| 161 | |||||||||||||||||||
| 162 | =cut | ||||||||||||||||||
| 163 | |||||||||||||||||||
| 164 | 2 | 1; | |||||||||||||||||