| 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::KeyInfo; | ||
| 9 | use vars '$VERSION'; | ||
| 10 | $VERSION = '2.02'; | ||
| 11 | |||
| 12 | |||
| 13 | use Log::Report 'xml-compile-wss-sig'; | ||
| 14 | |||
| 15 | use XML::Compile::WSS::Util qw/:wsm10 :wsm11 :xtp10/; | ||
| 16 | use XML::Compile::WSS::SecToken::X509v3 (); | ||
| 17 | use Crypt::OpenSSL::X509 qw/FORMAT_ASN1/; | ||
| 18 | |||
| 19 | |||
| 20 | sub new(@) { my $class = shift; (bless {}, $class)->init({@_}) } | ||
| 21 | sub init($) | ||
| 22 | { my ($self, $args) = @_; | ||
| 23 | $self->{XCWK_tokens} = []; | ||
| 24 | |||
| 25 | # It's too large to create accessors, so for this hack. | ||
| 26 | $self->{XCWK_config} = $args; | ||
| 27 | $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | sub fromConfig(@) | ||
| 32 | { my $class = shift; | ||
| 33 | $class->new(@_==1 ? %{$_[0]} : @_); # also clones the HASH | ||
| 34 | } | ||
| 35 | |||
| 36 | #----------------- | ||
| 37 | |||
| 38 | sub config() { my $c = shift->{XCWK_config}; wantarray ? %$c : $c } | ||
| 39 | |||
| 40 | #----------------------------- | ||
| 41 | |||
| 42 | sub addToken($) | ||
| 43 | { my $self = shift; | ||
| 44 | push @{$self->{XCWK_tokens}}, @_; | ||
| 45 | $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | |||
| 49 | sub findToken(%) | ||
| 50 | { my ($self, %args) = @_; | ||
| 51 | if(my $fu = $args{uri}) | ||
| 52 | { foreach my $token ($self->tokens) | ||
| 53 | { my $tu = $token->uri or next; | ||
| 54 | return $token if $tu eq $fu; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | if(my $fp = $args{fingerprint}) | ||
| 58 | { foreach my $token ($self->tokens) | ||
| 59 | { my $tp = $token->fingerprint or next; | ||
| 60 | return $token if $tp eq $fp; | ||
| 61 | } | ||
| 62 | } | ||
| 63 | if(my $fn = $args{name}) | ||
| 64 | { foreach my $token ($self->tokens) | ||
| 65 | { my $tn = $token->fingerprint or next; | ||
| 66 | return $token if $tn eq $fn; | ||
| 67 | } | ||
| 68 | } | ||
| 69 | (); | ||
| 70 | } | ||
| 71 | |||
| 72 | |||
| 73 | sub tokens() { @{shift->{XCWK_tokens}} } | ||
| 74 | |||
| 75 | #----------------- | ||
| 76 | |||
| 77 | # See dump/keyinfo/template | ||
| 78 | |||
| 79 | sub getTokens($%) | ||
| 80 | { my ($self, $wss) = (shift, shift); | ||
| 81 | my %args = ($self->config, @_); | ||
| 82 | |||
| 83 | my %keyinfo_handlers = | ||
| 84 | ( ds_KeyName => $self->_get_by_keyname($wss, \%args) | ||
| 85 | , ds_KeyValue => undef | ||
| 86 | , ds_RetrievalMethod => undef | ||
| 87 | , ds_X509Data => $self->_get_as_x509data($wss, \%args) | ||
| 88 | , ds_PGPData => undef | ||
| 89 | , ds_SPKIData => undef | ||
| 90 | , ds_MgmtData => undef | ||
| 91 | , wsse_SecurityTokenReference | ||
| 92 | => $self->_get_by_sectokref($wss, \%args) | ||
| 93 | ); | ||
| 94 | |||
| 95 | sub { | ||
| 96 | my ($h, $sec, $up_id) = @_; | ||
| 97 | my $id = $h->{Id} || $up_id; | ||
| 98 | my @tokens; | ||
| 99 | foreach (@{$h->{cho_ds_KeyName} ||[]}) | ||
| 100 | { my ($way, $how) = %$_; # hash of one pair | ||
| 101 | my $handler = $keyinfo_handlers{$way} | ||
| 102 | or error __x"unsupported key-info type {type} for {id}" | ||
| 103 | , type => $way, id => $id; | ||
| 104 | |||
| 105 | push @tokens, $handler->($id, $sec, $how); | ||
| 106 | } | ||
| 107 | @tokens; | ||
| 108 | }; | ||
| 109 | } | ||
| 110 | |||
| 111 | # ds_keyname | ||
| 112 | sub _get_by_keyname($$) | ||
| 113 | { my ($self, $wss, $args) = @_; | ||
| 114 | sub { my ($id, $sec, $h) = @_; $self->findToken(name => $h) }; | ||
| 115 | } | ||
| 116 | |||
| 117 | # ds_X509Data | ||
| 118 | sub _get_as_x509data($$) | ||
| 119 | { my ($self, $wss, $args) = @_; | ||
| 120 | |||
| 121 | sub { | ||
| 122 | my ($id, $sec, $h) = @_; | ||
| 123 | |||
| 124 | my @tokens; | ||
| 125 | foreach my $rec ( @{$h->{seq_ds_X509IssuerSerial} || []} ) | ||
| 126 | { my $bin_cert = $rec->{ds_X509Certificate} or next; | ||
| 127 | push @tokens, XML::Compile::WSS::SecToken::X509v3 | ||
| 128 | ->new(id => $id, binary => $bin_cert); | ||
| 129 | } | ||
| 130 | @tokens; | ||
| 131 | }; | ||
| 132 | } | ||
| 133 | |||
| 134 | # wsse_SecurityTokenReference | ||
| 135 | sub _get_by_sectokref($$$) | ||
| 136 | { my ($self, $wss, $args) = @_; | ||
| 137 | |||
| 138 | my %str_handlers = | ||
| 139 | ( wsse_KeyIdentifier => $self->_get_str_keyid($wss, $args) | ||
| 140 | , wsse_Reference => $self->_get_str_uri($wss, $args) | ||
| 141 | ); | ||
| 142 | |||
| 143 | sub { | ||
| 144 | my ($id, $sec, $h) = @_; | ||
| 145 | my @tokens; | ||
| 146 | foreach (@{$h->{cho_any}}) | ||
| 147 | { my ($ref, $d) = %$_; # one pair | ||
| 148 | my $handler = $str_handlers{$ref} | ||
| 149 | or error __x"Keyinfo {id}: {type} not supported" | ||
| 150 | , id => $id, type => $ref; | ||
| 151 | |||
| 152 | push @tokens, $handler->($id, $sec, $d); | ||
| 153 | } | ||
| 154 | |||
| 155 | @tokens; | ||
| 156 | }; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub _get_str_keyid($$) # SECTOKREF_KEYID | ||
| 160 | { my ($self, $wss, $args) = @_; | ||
| 161 | sub { | ||
| 162 | my ($id, $sec, $d) = @_; | ||
| 163 | my $valuet = $d->{ValueType}; | ||
| 164 | if($valuet eq WSM11_PRINT_SHA1) | ||
| 165 | { my $p = wsm_decoded $d->{EncodingType}, $d->{_}; | ||
| 166 | return $self->findToken(fingerprint => $p); | ||
| 167 | } | ||
| 168 | |||
| 169 | error __x"Keyinfo {id}: {type} not supported", id => $id, type => $valuet; | ||
| 170 | }; | ||
| 171 | } | ||
| 172 | |||
| 173 | sub _get_str_uri($$) # SECTOKREF_URI | ||
| 174 | { my ($self, $wss, $args) = @_; | ||
| 175 | sub { | ||
| 176 | my ($id, $sec, $d) = @_; | ||
| 177 | my $uri = $d->{URI}; | ||
| 178 | my $token = $self->findToken(uri => $uri); | ||
| 179 | return $token if $token; # already taken | ||
| 180 | |||
| 181 | my $valuet = $d->{ValueType}; | ||
| 182 | if($valuet eq XTP10_X509v3) | ||
| 183 | { substr($uri, 0, 1) eq '#' | ||
| 184 | or error __x"Keyinfo {id}: only inlined token references supported, got {uri}" | ||
| 185 | , id => $id, uri => $uri; | ||
| 186 | |||
| 187 | my $binsec = $sec->{wsse_BinarySecurityToken} | ||
| 188 | or error __x"Keyinfo {id}: cannot find BinarySecurityToken" | ||
| 189 | , id => $id; | ||
| 190 | |||
| 191 | my $have_id = '#'.$binsec->{wsu_Id}; | ||
| 192 | $have_id eq $uri | ||
| 193 | or error __x"Keyinfo {id}: wrong BinarySecurityToken {uri}, expected {expect}" | ||
| 194 | , id => $id, uri => $have_id, expect => $uri; | ||
| 195 | |||
| 196 | my $token = XML::Compile::WSS::SecToken::X509v3->new | ||
| 197 | ( id => $binsec->{wsu_Id}, uri => $uri, type => $valuet | ||
| 198 | , binary => wsm_decoded($binsec->{EncodingType}, $binsec->{_}) | ||
| 199 | ); | ||
| 200 | |||
| 201 | $self->addToken($token); | ||
| 202 | return $token; | ||
| 203 | } | ||
| 204 | |||
| 205 | panic "Keyinfo $id: $valuet not supported"; | ||
| 206 | }; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | sub builder($%) | ||
| 211 | { my ($self, $wss) = @_; | ||
| 212 | my %args = ($self->config, @_); | ||
| 213 | my $type = $args{publish_token} || 'SECTOKREF_URI'; | ||
| 214 | return undef if $type eq 'NO'; | ||
| 215 | |||
| 216 | my %str_handlers = | ||
| 217 | ( KEYNAME => '_make_keyname' | ||
| 218 | , SECTOKREF_KEYID => '_make_sectokref_keyid' | ||
| 219 | , SECTOKREF_URI => '_make_sectokref_uri' | ||
| 220 | , INCLUDE_BY_REF => '_make_sectokref_uri' # name is pre 2.00 | ||
| 221 | , X509DATA => '_make_x509data' | ||
| 222 | ); | ||
| 223 | |||
| 224 | my $handler = $str_handlers{$type} | ||
| 225 | or panic "unknown keyinfo type $type"; | ||
| 226 | |||
| 227 | my $nest = $self->$handler($wss, \%args); | ||
| 228 | my $ki_id = $args{keyinfo_id}; | ||
| 229 | sub { | ||
| 230 | my ($doc, $token, $sec) = @_; | ||
| 231 | +{ cho_ds_KeyName => [ $nest->($doc, $token, $sec) ] | ||
| 232 | , Id => $ki_id | ||
| 233 | }; | ||
| 234 | }; | ||
| 235 | } | ||
| 236 | |||
| 237 | sub _make_keyname($$$) | ||
| 238 | { my ($self, $wss, $args) = @_; | ||
| 239 | sub { | ||
| 240 | my ($doc, $token, $sec) = @_; | ||
| 241 | my $name = $token->name | ||
| 242 | or panic "token $token has no name for KEYNAME"; | ||
| 243 | +{ ds_KeyName => $name }; | ||
| 244 | }; | ||
| 245 | } | ||
| 246 | |||
| 247 | sub _make_x509data($$$) | ||
| 248 | { my ($self, $wss, $args) = @_; | ||
| 249 | my $as = $args->{x509data_type} || 'ASN1DER'; | ||
| 250 | |||
| 251 | my $put | ||
| 252 | = $as eq 'ASN1DER' | ||
| 253 | ? sub { ds_X509Certificate => $_[0]->as_string(FORMAT_ASN1) } | ||
| 254 | : $as eq 'SERIAL' | ||
| 255 | ? sub { ds_X509IssuerSerial => | ||
| 256 | { ds_X509IssuerName => $_[0]->issuer | ||
| 257 | , ds_X509SerialNumber => $_[0]->serial } | ||
| 258 | } | ||
| 259 | : $as eq 'SKI' ? sub { ds_X509SKI => $_[0]->hash} | ||
| 260 | : $as eq 'SUBJECT' ? sub { ds_X509SubjectName => $_[0]->subject } | ||
| 261 | : error __x"write key-info as X509Data, unknown format `{name}'" | ||
| 262 | , name => $as; | ||
| 263 | |||
| 264 | # No idea how we can use this Cert Revocation List, ds_X509CRL | ||
| 265 | # Other elements, not in ds:, not (yet) supported for writing | ||
| 266 | |||
| 267 | # This routine can handle an ARRAY, but the rest of the module | ||
| 268 | # probably not. | ||
| 269 | sub { | ||
| 270 | my ($doc, $token, $sec) = @_; | ||
| 271 | my @data = map $put->($_) | ||
| 272 | , ref $token eq 'ARRAY' ? @$token : $token; | ||
| 273 | |||
| 274 | +{ seq_ds_X509IssuerSerial => \@data } | ||
| 275 | }; | ||
| 276 | } | ||
| 277 | |||
| 278 | sub _make_sectokref($$$) | ||
| 279 | { my ($self, $wss, $args) = @_; | ||
| 280 | my $refid = $args->{sectokref_id}; | ||
| 281 | my $usage = $args->{usage}; | ||
| 282 | my $refw = $wss->schema->writer('wsse:SecurityTokenReference' | ||
| 283 | , include_namespaces => 0); | ||
| 284 | |||
| 285 | sub { | ||
| 286 | my ($doc, $token, $sec, $payload) = @_; | ||
| 287 | my $ref = $refw->($doc, +{wsu_Id => $refid, wsse_Usage => $usage | ||
| 288 | , cho_any => $payload}); | ||
| 289 | +{ 'wsse:SecurityTokenReference' => $ref }; | ||
| 290 | }; | ||
| 291 | } | ||
| 292 | |||
| 293 | sub _make_sectokref_keyid($$$) | ||
| 294 | { my ($self, $wss, $args) = @_; | ||
| 295 | |||
| 296 | my $valuet = $args->{keyid_value} || WSM11_PRINT_SHA1; | ||
| 297 | my $enct = $args->{keyid_encoding} || WSM10_BASE64; | ||
| 298 | my $keyid = $args->{keyident_id}; | ||
| 299 | |||
| 300 | my $valuep; # first param is call is $token | ||
| 301 | if($valuet eq WSM11_PRINT_SHA1) | ||
| 302 | { $valuep = sub {shift->fingerprint or panic "token has no fingerprint" }; | ||
| 303 | } | ||
| 304 | else { panic "unsupported security token reference value type '$valuet'" } | ||
| 305 | |||
| 306 | my $encp = sub { wsm_encoded $enct, $valuep->($_[0]) }; | ||
| 307 | my $kidw = $wss->schema->writer('wsse:KeyIdentifier' | ||
| 308 | , include_namespaces=>0); | ||
| 309 | my $refer = $self->_make_sectokref($wss, $args); | ||
| 310 | |||
| 311 | sub { | ||
| 312 | my ($doc, $token, $sec) = @_; | ||
| 313 | my $elem = $kidw->($doc | ||
| 314 | , +{ wsu_Id => $keyid, ValueType => $valuet, EncodingType => $enct | ||
| 315 | , _ => $encp->($token) }); | ||
| 316 | $refer->($doc, $token, $sec, +{'wsse:KeyIdentifier' => $elem}); | ||
| 317 | }; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub _make_sectokref_uri($$$) | ||
| 321 | { my ($self, $wss, $args) = @_; | ||
| 322 | |||
| 323 | my $schema = $wss->schema; | ||
| 324 | my $binenc = $args->{binsec_encoding} || WSM10_BASE64; | ||
| 325 | my $kidw = $schema->writer('wsse:Reference', include_namespaces => 0); | ||
| 326 | my $refer = $self->_make_sectokref($wss, $args); | ||
| 327 | my $bstw = $schema->writer('wsse:BinarySecurityToken'); | ||
| 328 | my $default_uri = $args->{sectokref_uri}; | ||
| 329 | |||
| 330 | sub { | ||
| 331 | my ($doc, $token, $sec) = @_; | ||
| 332 | my $uri = $default_uri || $token->uri || '#abc'; | ||
| 333 | my $intern = $uri !~ m!^\w+://!; | ||
| 334 | my $type = $token->type; | ||
| 335 | my $elem = $kidw->($doc, +{ValueType => $type, URI => $uri} ); | ||
| 336 | |||
| 337 | if($intern && $token->can('asBinary')) | ||
| 338 | { (my $id = $uri) =~ s/^#//; | ||
| 339 | |||
| 340 | my $bst = $bstw->($doc, | ||
| 341 | +{ wsu_Id => $id | ||
| 342 | , ValueType => $type | ||
| 343 | , EncodingType => $binenc | ||
| 344 | , _ => wsm_encoded($binenc, $token->asBinary) | ||
| 345 | } ); | ||
| 346 | $sec->appendChild($bst); | ||
| 347 | } | ||
| 348 | $refer->($doc, $token, $sec, +{'wsse:Reference' => $elem}); | ||
| 349 | }; | ||
| 350 | } | ||
| 351 | |||
| 352 | #----------------- | ||
| 353 | |||
| 354 | 1; |