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