blib/lib/XML/Compile/WSS/SecToken/EncrKey.pm untested

filter: Statementstmt 0.0% 0 / 90 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 39 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 17 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 157 CC 2 SCAR 8.9
CC
2
Cov
0%
init
6.0
signer
2.0
key
2.0
CRAP 2.4
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::SecToken::EncrKey;
9 use vars '$VERSION';
10 $VERSION = '2.02';
11  
12 use base 'XML::Compile::WSS::SecToken';
13  
14 use Log::Report 'xml-compile-wss-sig';
15  
16 use XML::Compile::WSS::Util qw/:xenc :wsm10/;
17 use XML::Compile::WSS::Sign ();
18 use XML::Compile::WSS::KeyInfo ();
19  
20  
21 sub init($)
22 { my ($self, $args) = @_;
23 $args->{type} ||= XENC_RSA_OAEP;
24  
25 $self->SUPER::init($args);
26  
27 my $type = $self->type;
28 $type eq XENC_RSA_OAEP
29 or error __x"unsupported encrypted key type {type}", type => $type;
30  
31 # This can be made cleaner, via SecToken::fromConfig
32 my $signer = $args->{signer}
33 or error __x"EncryptedKey needs info about its signer";
34  
35 if(ref $signer eq 'HASH')
36 { $signer->{padding} ||= 'PKCS1_OAEP';
37 $signer = XML::Compile::WSS::Sign->fromConfig($signer);
38 }
39 $self->{XCWSE_signer} = $signer;
40 $self->{XCWSE_key} = $args->{key} or panic "no key";
41  
42 my $ki = $args->{key_info} || {};
43 $ki->{publish_token} ||= 'SECTOKREF_KEYID';
44 $self->{XCWSE_keyinfo} = XML::Compile::WSS::KeyInfo->fromConfig($ki);
45  
46 $self;
47 }
48  
49 #-----------------
50  
51 sub signer() {shift->{XCWSE_signer}}
52 sub key() {shift->{XCWSE_key}}
53 sub keyInfo(){shift->{XCWSE_keyinfo}}
54  
55 #-----------------
56  
57 # See http://en.wikibooks.org/wiki/XML_-_Managing_Data_Exchange/XML_Encryption
58  
59 sub _get_encr($$)
60 { my ($class, $wss, $args) = @_;
61 my $keyinfo = $wss->keyInfo;
62 my $gettokens = $keyinfo->getTokens($wss);
63 my $type_default = $args->{encrtype_default};
64  
65 sub {
66 my ($h, $sec) = @_;
67 my $id = $h->{Id};
68 my @tokens = $gettokens->($h->{ds_KeyInfo}, $sec, $id);
69 my $token = $tokens[0]
70 or error __x"no token for encryption key {id}", id => $id;
71  
72 my $type = $h->{xenc_EncryptionMethod}{Algorithm} || $type_default;
73 $type eq XENC_RSA_OAEP
74 or error __x"unsupported encryption type {type}", type => $type;
75  
76 $class->new
77 ( id => $id
78 , type => $type
79 , key => $h->{xenc_CipherData}{xenc_CipherValue}
80 , key_info =>
81 { key_size => $h->{xenc_KeySize} # not used
82 }
83 , signer =>
84 { padding => 'PKCS1_OAEP'
85 , public_key => $tokens[0]
86 # OAEP parameters are only used by old PKCS and not supported
87 # by openssl
88 , params => $h->{xenc_OAEPparams}
89 }
90 );
91 };
92 }
93  
94 # The key may differ per message, not the certificate
95 # Do not reinstate existing encrypters
96  
97  
98 my %encrs;
99 sub getEncrypter($%)
100 { my ($class, $wss, %args) = @_;
101 my $get_encr = $class->_get_encr($wss, \%args);
102  
103 sub {
104 my ($h, $sec) = @_;
105 my $id = $h->{Id};
106 $encrs{$id} ||= $get_encr->($h, $sec);
107 };
108 }
109  
110  
111 sub getKey($%)
112 { my ($class, $wss, %args) = @_;
113 my $get_encr = $class->getEncrypter($wss, %args);
114  
115 sub {
116 my ($h, $sec) = @_;
117 my $encr = $get_encr->($h, $sec);
118  
119 # xenc_CipherReference not (yet) supported
120 $h->{xenc_CipherData}{xenc_CipherValue}
121 or error __x"cipher data not understood for {id}", id => $encr->id;
122 };
123 }
124  
125  
126 sub getChecker($%)
127 { my ($class, $wss, %args) = @_;
128 my $get_encr = $class->getEncrypter($wss, %args);
129  
130 sub {
131 my ($h, $sec, $value) = @_;
132 my $encr = $get_encr->($h, $sec);
133 my $id = $encr->id;
134  
135 # xenc_CipherReference not (yet) supported
136 my $outcome = $h->{xenc_CipherData}{xenc_CipherValue}
137 or error __x"cipher data not understood for {id}", id => $id;
138  
139 my $got = $encr->signer->encrypt($value);
140  
141 #use MIME::Base64;
142 #warn "OUT=", encode_base64 $outcome;
143 #warn "GOT=", encode_base64 $got;
144 # This warning is currently produced in t/21enckey.t, but that example
145 # may be wrong... or something else... no idea yet.
146  
147 $got eq $outcome
148 or warning __x"check of crypto checksum failed {id}", id => $id;
149  
150 1;
151 };
152 }
153  
154 sub builder($%)
155 { my ($self, $wss, %args) = @_;
156  
157 my $keylink = $self->keyInfo->builder($wss, %args);
158 my $signer = $self->signer;
159 my $encr_type = $self->type;
160 my $key = $self->key;
161 my $seckeyw = $wss->schema->writer('xenc:EncryptedKey');
162  
163 sub {
164 my ($doc, $sec_node) = @_;
165 my $ki = undef; # $keylink->($doc, $signer->privateKey, $sec_node)
166  
167 # see dump/encrkey/template
168 my %data =
169 ( xenc_EncryptionMethod => { Algorithm => $encr_type }
170 , ds_KeyInfo => $ki
171 , xenc_CipherData => { xenc_CipherValue => $signer->encrypt($key) }
172 );
173  
174 my $node = $seckeyw->($doc, \%data);
175 #warn $node->toString(1);
176 $sec_node->appendChild($node);
177 $node;
178 };
179 }
180  
181 1;