Skip to content

Commit 60768bd

Browse files
committed
Ensure that version supports CryptX <= 0.077
1 parent 1d4e756 commit 60768bd

File tree

3 files changed

+82
-29
lines changed

3 files changed

+82
-29
lines changed

lib/XML/Enc.pm

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,15 +617,23 @@ sub _DecryptKey {
617617
my $params_hash = defined $keymethod->{oaep_digest} ?
618618
$self->_getParamsAlgorithm($keymethod->{oaep_digest}) : 'SHA1';
619619

620-
return $self->{key_obj}->decrypt($encryptedkey, 'oaep', 'SHA1', $params_hash, $oaep_params);
620+
if ($CryptX::VERSION le 0.077) {
621+
return $self->{key_obj}->decrypt($encryptedkey, 'oaep', 'SHA1', $oaep_params);
622+
} else {
623+
return $self->{key_obj}->decrypt($encryptedkey, 'oaep', 'SHA1', $params_hash, $oaep_params);
624+
}
621625
}
622626
elsif ($keymethod->{Algorithm} eq 'http://www.w3.org/2009/xmlenc11#rsa-oaep') {
623627
my $oaep_params = defined $keymethod->{OAEPparams} ?
624628
decode_base64(_trim($keymethod->{OAEPparams}) ) : '';
625629
my $mgf_hash = defined $keymethod->{MGF} ? $self->_getOAEPAlgorithm($keymethod->{MGF}) : undef;
626630
my $params_hash = defined $keymethod->{oaep_digest} ? $self->_getParamsAlgorithm($keymethod->{oaep_digest}) : $mgf_hash;
627631

628-
return $self->{key_obj}->decrypt($encryptedkey, 'oaep', $mgf_hash, $params_hash, $oaep_params);
632+
if ($CryptX::VERSION le 0.077) {
633+
return $self->{key_obj}->decrypt($encryptedkey, 'oaep', $mgf_hash, $oaep_params);
634+
} else {
635+
return $self->{key_obj}->decrypt($encryptedkey, 'oaep', $mgf_hash, $params_hash, $oaep_params);
636+
}
629637
} else {
630638
die "Unsupported Key Encryption Method";
631639
}
@@ -645,12 +653,20 @@ sub _EncryptKey {
645653
${$key} = $rsa_pub->encrypt(${$key}, 'v1.5');
646654
}
647655
elsif ($keymethod eq 'http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p') {
648-
${$key} = $rsa_pub->encrypt(${$key}, 'oaep', 'SHA1', 'SHA1', $self->{oaep_params});
656+
if ($CryptX::VERSION le 0.077) {
657+
${$key} = $rsa_pub->encrypt(${$key}, 'oaep', 'SHA1', $self->{oaep_params});
658+
} else {
659+
${$key} = $rsa_pub->encrypt(${$key}, 'oaep', 'SHA1', 'SHA1', $self->{oaep_params});
660+
}
649661
}
650662
elsif ($keymethod eq 'http://www.w3.org/2009/xmlenc11#rsa-oaep') {
651663
my $mgf_hash = defined $self->{oaep_mgf_alg} ? $self->_getOAEPAlgorithm($self->{oaep_mgf_alg}) : undef;
652664
my $params_hash = defined $self->{oaep_digest} & $self->{oaep_digest} ne '' ? $self->_getParamsAlgorithm($self->{oaep_digest}) : $mgf_hash;
653-
${$key} = $rsa_pub->encrypt(${$key}, 'oaep', $mgf_hash, $params_hash, $self->{oaep_params});
665+
if ($CryptX::VERSION le 0.077) {
666+
${$key} = $rsa_pub->encrypt(${$key}, 'oaep', $mgf_hash, $self->{oaep_params});
667+
} else {
668+
${$key} = $rsa_pub->encrypt(${$key}, 'oaep', $mgf_hash, $params_hash, $self->{oaep_params});
669+
}
654670
} else {
655671
die "Unsupported Key Encryption Method";
656672
}

t/06-test-encryption-methods.t

Lines changed: 32 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ my @oaep_digests = qw/sha1 sha224 sha256 sha384 sha512/;
2020

2121
my $xmlsec = get_xmlsec_features();
2222
my $lax_key_search = $xmlsec->{lax_key_search} ? '--lax-key-search': '';
23+
my $cryptx = get_cryptx_features();
2324

2425
foreach my $km (@key_methods) {
2526
foreach my $dm (@data_methods) {
@@ -54,35 +55,41 @@ foreach my $km (@key_methods) {
5455

5556
foreach my $om (@oaep_mgf_algs) {
5657
foreach my $omdig (@oaep_digests) {
57-
foreach my $dm (@data_methods) {
58-
my $encrypter = XML::Enc->new(
59-
{
60-
key => 't/sign-private.pem',
61-
cert => 't/sign-certonly.pem',
62-
data_enc_method => $dm,
63-
key_transport => 'rsa-oaep',
64-
oaep_mgf_alg => $om,
65-
oaep_digest => $omdig,
66-
no_xml_declaration => 1,
58+
SKIP: {
59+
if (! $cryptx->{oaem_mgf_digest} & ($om ne $omdig)) {
60+
my $skip = (scalar @data_methods) * 4;
61+
skip "CryptX $cryptx->{version} does not support rsa-oaep MGF: $om and digest $omdig", $skip;
6762
}
68-
);
63+
foreach my $dm (@data_methods) {
64+
my $encrypter = XML::Enc->new(
65+
{
66+
key => 't/sign-private.pem',
67+
cert => 't/sign-certonly.pem',
68+
data_enc_method => $dm,
69+
key_transport => 'rsa-oaep',
70+
oaep_mgf_alg => $om,
71+
oaep_digest => $omdig,
72+
no_xml_declaration => 1,
73+
}
74+
);
6975

70-
my $encrypted = $encrypter->encrypt($xml);
71-
ok($encrypted =~ /EncryptedData/, "Successful Encrypted: Key Method:rsa-oaep MGF:$om, param:$omdig Data Method:$dm");
76+
my $encrypted = $encrypter->encrypt($xml);
77+
ok($encrypted =~ /EncryptedData/, "Successful Encrypted: Key Method:rsa-oaep MGF:$om, param:$omdig Data Method:$dm");
7278

73-
SKIP: {
74-
skip "xmlsec1 not installed", 2 unless $xmlsec->{installed};
75-
skip "xmlsec version 1.2.27 minimum for GCM", 2 if ! $xmlsec->{aes_gcm};
76-
ok( open XML, '>', "$om-$omdig-$dm-tmp.xml" );
77-
print XML $encrypted;
78-
close XML;
79-
my $verify_response = `xmlsec1 --decrypt $lax_key_search --privkey-pem t/sign-private.pem $om-$omdig-$dm-tmp.xml 2>&1`;
80-
ok( $verify_response =~ m/XML-SIG_1/, "Successfully decrypted with xmlsec1" )
81-
or warn "calling xmlsec1 failed: '$verify_response'\n";
82-
unlink "$om-$omdig-$dm-tmp.xml";
79+
SKIP: {
80+
skip "xmlsec1 not installed", 2 unless $xmlsec->{installed};
81+
skip "xmlsec version 1.2.27 minimum for GCM", 2 if ! $xmlsec->{aes_gcm};
82+
ok( open XML, '>', "$om-$omdig-$dm-tmp.xml" );
83+
print XML $encrypted;
84+
close XML;
85+
my $verify_response = `xmlsec1 --decrypt $lax_key_search --privkey-pem t/sign-private.pem $om-$omdig-$dm-tmp.xml 2>&1`;
86+
ok( $verify_response =~ m/XML-SIG_1/, "Successfully decrypted with xmlsec1" )
87+
or warn "calling xmlsec1 failed: '$verify_response'\n";
88+
unlink "$om-$omdig-$dm-tmp.xml";
89+
}
90+
ok($encrypter->decrypt($encrypted) =~ /XML-SIG_1/, "Successfully Decrypted with XML::Enc");
91+
}
8392
}
84-
ok($encrypter->decrypt($encrypted) =~ /XML-SIG_1/, "Successfully Decrypted with XML::Enc");
85-
}
8693
}
8794
}
8895
done_testing;

t/lib/Test/XML/Enc/Util.pm

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ our @ISA = qw(Exporter);
99
our @EXPORT = qw(
1010
get_xmlsec_features
1111
get_openssl_features
12+
get_cryptx_features
1213
);
1314

1415
our @EXPORT_OK;
@@ -87,6 +88,35 @@ sub get_openssl_features {
8788
return \%openssl;
8889
}
8990

91+
#########################################################################
92+
# get_cryptx_features
93+
#
94+
# Parameter: none
95+
#
96+
# Returns a hash of the version and any features that are needed
97+
# if proper the version is installed
98+
#
99+
# Response: hash
100+
#
101+
# %features = (
102+
# version => '0.077',
103+
# oaem_mgf_digest => 0,
104+
# );
105+
##########################################################################
106+
sub get_cryptx_features {
107+
108+
require CryptX;
109+
110+
my $version = $CryptX::VERSION;
111+
112+
my %cryptx = (
113+
version => $version,
114+
oaem_mgf_digest => ($version gt '0.077') ? 1 : 0,
115+
);
116+
117+
return \%cryptx;
118+
}
119+
90120
1;
91121

92122
__END__

0 commit comments

Comments
 (0)