| 1 | # Copyrights 2012-2016 by [Mark Overmeer]. | ||||||||||||||||||||||||||||
| 2 | # For other contributors see ChangeLog. | ||||||||||||||||||||||||||||
| 3 | # See the manual pages for details on the licensing terms. | ||||||||||||||||||||||||||||
| 4 | # Pod stripped from pm file by OODoc 2.02. | ||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | package XML::Compile::WSS::Sign::RSA; | ||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use vars '$VERSION'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | $VERSION = '2.02'; | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | ▶ | use base 'XML::Compile::WSS::Sign'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Log::Report 'xml-compile-wss-sig'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 5 | ▶ | use Crypt::OpenSSL::RSA (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 5 | ▶ | use File::Slurp qw/read_file/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 5 | ▶ | use Scalar::Util qw/blessed/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | sub init($) | ||||||||||||||||||||||||||||
| 22 | 5 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 23 | 5 | $self->SUPER::init($args); | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | $self->privateKey | ||||||||||||||||||||||||||||
| 26 | ( $args->{private_key} | ||||||||||||||||||||||||||||
| 27 | , hashing => $args->{hashing} | ||||||||||||||||||||||||||||
| 28 | , padding => $args->{padding} | ||||||||||||||||||||||||||||
| 29 | 5 | ); | |||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | $self->publicKey | ||||||||||||||||||||||||||||
| 32 | ( $args->{public_key} | ||||||||||||||||||||||||||||
| 33 | , hashing => $args->{hashing} | ||||||||||||||||||||||||||||
| 34 | , padding => $args->{padding} | ||||||||||||||||||||||||||||
| 35 | 5 | ); | |||||||||||||||||||||||||||
| 36 | 5 | $self; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | #----------------- | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | sub _setRSAflags($$%) | ||||||||||||||||||||||||||||
| 43 | 5 | ▶ | { my ($self, $key, $rsa, %args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
_setRSAflags: called CC=5 SCAR=19.7
| |||||||||||||||||||||||||||||
| 44 | 5 | ▶ | if(my $hashing = $args{hashing}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 5 | { my $use_hash = "use_\L$hashing\E_hash"; | |||||||||||||||||||||||||||
| 46 | 5 | ▶ | $rsa->can($use_hash) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 47 | or error __x"hash {type} not supported by {pkg}" | ||||||||||||||||||||||||||||
| 48 | , type => $hashing, pkg => ref $key; | ||||||||||||||||||||||||||||
| 49 | 5 | $rsa->$use_hash(); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 5 | ▶ | if(my $padding = $args{padding}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 53 | 0 | { my $use_pad = "use_\L$padding\E_padding"; | |||||||||||||||||||||||||||
| 54 | 0 | ▶ | $rsa->can($use_pad) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | or error __x"padding {type} not supported by {pkg}" | ||||||||||||||||||||||||||||
| 56 | , type => $padding, pkg => ref $key; | ||||||||||||||||||||||||||||
| 57 | 0 | $rsa->$use_pad(); | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | 5 | $rsa; | |||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | sub privateKey(;$%) | ||||||||||||||||||||||||||||
| 63 | 5 | ▶ | { my ($self, $priv) = (shift, shift); | ||||||||||||||||||||||||||
|
Subroutine
privateKey: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 64 | 5 | ▶ | defined $priv or return $self->{XCWSR_privkey}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 2 | my ($key, $rsa) = $self->toPrivateSHA($priv); | |||||||||||||||||||||||||||
| 67 | 2 | $self->{XCWSR_privrsa} = $self->_setRSAflags($key, $rsa, @_); | |||||||||||||||||||||||||||
| 68 | 2 | $self->{XCWSR_privkey} = $key; | |||||||||||||||||||||||||||
| 69 | 2 | $key; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | sub toPrivateSHA($) | ||||||||||||||||||||||||||||
| 74 | 2 | ▶ | { my ($self, $priv) = @_; | ||||||||||||||||||||||||||
|
Subroutine
toPrivateSHA: called CC=6 SCAR=19.9
Pod
documented
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | 2 | ▶ | return ($priv->get_private_key_string, $priv) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
builtin::blessed($priv) and $priv->isa("Crypt::OpenSSL::RSA")
MC/DC0%0/2
builtin::blessed($priv) and $priv->isa("Crypt::OpenSSL::RSA")
builtin::blessed($priv)
$priv->isa("Crypt::OpenSSL::RSA")
Truth table33%1/3
A and B
A= builtin::blessed($priv)B= $priv->isa("Crypt::OpenSSL::RSA")
| |||||||||||||||||||||||||||||
| 77 | if blessed $priv && $priv->isa('Crypt::OpenSSL::RSA'); | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 2 | ▶ | error __x"unsupported private key object `{object}'", object=>$priv | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | if ref $priv =~ m/Crypt/; | ||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||
| 82 | 2 | ▶ | return ($priv, Crypt::OpenSSL::RSA->new_private_key($priv)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | if index($priv, "\n") >= 0; | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 2 | my $key = read_file $priv; | |||||||||||||||||||||||||||
| 86 | 2 | my $rsa = eval { Crypt::OpenSSL::RSA->new_private_key($key) }; | |||||||||||||||||||||||||||
| 87 | 2 | ▶ | if($@) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 88 | 0 | { error __x"cannot read private RSA key from {file}: {err}" | |||||||||||||||||||||||||||
| 89 | , file => $priv, err => $@; | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 2 | ($key, $rsa); | |||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 6 | ▶ | sub privateKeyRSA() {shift->{XCWSR_privrsa}} | ||||||||||||||||||||||||||
|
Subroutine
privateKeyRSA: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | sub publicKey(;$%) | ||||||||||||||||||||||||||||
| 100 | 5 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
publicKey: called CC=6 SCAR=18.1
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | 5 | ▶ | my $pub = @_%2==1 ? shift : undef; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | return $self->{XCWSR_pubkey} | ||||||||||||||||||||||||||||
| 104 | 5 | ▶ | if !defined $pub && $self->{XCWSR_pubkey}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not defined $pub and $self->{"XCWSR_pubkey"}
MC/DC0%0/2
not defined $pub and $self->{"XCWSR_pubkey"}
not defined $pub
$self->{"XCWSR_pubkey"}
Truth table66%2/3
not(A) and B
A= not defined $pubB= $self->{"XCWSR_pubkey"}
| |||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 5 | ▶ | my $token = $pub || $self->privateKeyRSA | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$pub || $self->privateKeyRSA
MC/DC100%2/2
$pub || $self->privateKeyRSA
$pub
$self->privateKeyRSA
Truth table100%3/3
A || B
A= $pubB= $self->privateKeyRSA
| |||||||||||||||||||||||||||||
| 107 | or return; | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 3 | my ($key, $rsa) = $self->toPublicRSA($token); | |||||||||||||||||||||||||||
| 110 | 3 | $self->{XCWSR_pubrsa} = $self->_setRSAflags($key, $rsa, @_); | |||||||||||||||||||||||||||
| 111 | 3 | $self->{XCWSR_pubkey} = $pub; | |||||||||||||||||||||||||||
| 112 | 3 | $pub; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub toPublicRSA($) | ||||||||||||||||||||||||||||
| 117 | 3 | ▶ | { my ($thing, $token) = @_; | ||||||||||||||||||||||||||
|
Subroutine
toPublicRSA: called CC=6 SCAR=19.1
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 3 | ▶ | defined $token or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 3 | ▶ | blessed $token | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 121 | or panic "expects a public_key as object, not ".$token; | ||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 3 | ▶ | return ($token->get_public_key_string, $token) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | if $token->isa('Crypt::OpenSSL::RSA'); | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 1 | ▶ | $token = $token->certificate | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | if $token->isa('XML::Compile::WSS::SecToken::X509v3'); | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | 1 | my $key = $token->pubkey; | |||||||||||||||||||||||||||
| 130 | 1 | ▶ | return ($key, Crypt::OpenSSL::RSA->new_public_key($key)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | if $token->isa('Crypt::OpenSSL::X509'); | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | 0 | error __x"unsupported public key `{token}' for check RSA" | |||||||||||||||||||||||||||
| 134 | , token => $token; | ||||||||||||||||||||||||||||
| 135 | } | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | sub publicKeyString($) | ||||||||||||||||||||||||||||
| 139 | 0 | ▶ | { my $rsa = shift->publicKeyRSA; | ||||||||||||||||||||||||||
|
Subroutine
publicKeyString: not called CC=4 SCAR=30.0
Pod
documented
| |||||||||||||||||||||||||||||
| 140 | 0 | ▶ | my $how = shift || '(NONE)'; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || '(NONE)'
MC/DC0%0/1
shift() || '(NONE)'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 0 | ▶ | $how eq 'PKCS1' ? $rsa->get_public_key_string | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 143 | : $how eq 'X509' ? $rsa->get_public_key_x509_string | ||||||||||||||||||||||||||||
| 144 | : error __x"unknown public key string format `{name}'", name => $how; | ||||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | 1 | ▶ | sub publicKeyRSA() {shift->{XCWSR_pubrsa}} | ||||||||||||||||||||||||||
|
Subroutine
publicKeyRSA: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||
| 151 | #----------------- | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | # Do we need next 4? Probably not | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | sub sign(@) | ||||||||||||||||||||||||||||
| 156 | 0 | ▶ | { my ($self, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
sign: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 157 | 0 | ▶ | my $priv = $self->privateKeyRSA | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | or error "signing rsa requires the private_key"; | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | 0 | $priv->sign($text); | |||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | sub encrypt(@) | ||||||||||||||||||||||||||||
| 164 | 0 | ▶ | { my ($self, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
encrypt: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 165 | 0 | ▶ | my $pub = $self->publicKeyRSA | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 166 | or error "encrypting rsa requires the public_key"; | ||||||||||||||||||||||||||||
| 167 | 0 | $pub->encrypt($text); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | sub decrypt(@) | ||||||||||||||||||||||||||||
| 171 | 0 | ▶ | { my ($self, $text) = @_; | ||||||||||||||||||||||||||
|
Subroutine
decrypt: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 172 | 0 | ▶ | my $priv = $self->privateKeyRSA | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 173 | or error "decrypting rsa requires the private_key"; | ||||||||||||||||||||||||||||
| 174 | 0 | $priv->decrypt($text); | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | sub check($$) | ||||||||||||||||||||||||||||
| 179 | 0 | ▶ | { my ($self, $text, $signature) = @_; | ||||||||||||||||||||||||||
|
Subroutine
check: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 180 | 0 | ▶ | my $rsa = $self->publicKeyRSA | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | or error "checking signature with rsa requires the public_key"; | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 0 | $rsa->verify($text, $signature); | |||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | ### above functions probably not needed. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | sub builder() | ||||||||||||||||||||||||||||
| 189 | 2 | ▶ | { my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
builder: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 190 | 2 | ▶ | my $priv = $self->privateKeyRSA | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | or error "signing rsa requires the private_key"; | ||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||
| 193 | 2 | ▶ | sub { $priv->sign($_[0]) }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | sub checker() | ||||||||||||||||||||||||||||
| 197 | 1 | ▶ | { my ($self) = @_; | ||||||||||||||||||||||||||
|
Subroutine
checker: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||
| 198 | 1 | ▶ | my $pub = $self->publicKeyRSA | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | or error "checking signature with rsa requires the public_key"; | ||||||||||||||||||||||||||||
| 200 | |||||||||||||||||||||||||||||
| 201 | sub { # ($text, $signature) | ||||||||||||||||||||||||||||
| 202 | 1 | ▶ | $pub->verify($_[0], $_[1]); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 203 | 1 | }; | |||||||||||||||||||||||||||
| 204 | } | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | #----------------- | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 5 | 1; | |||||||||||||||||||||||||||