Skip to content

Commit 9ee72a9

Browse files
committed
Use Test::XML::Enc to determine xmlsec features
1 parent 9f01450 commit 9ee72a9

File tree

6 files changed

+181
-14
lines changed

6 files changed

+181
-14
lines changed

Makefile.PL

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,14 @@ my %WriteMakefileArgs = (
2929
"warnings" => 0
3030
},
3131
"TEST_REQUIRES" => {
32+
"Crypt::OpenSSL::Guess" => 0,
33+
"Exporter" => 0,
3234
"File::Slurper" => 0,
3335
"File::Which" => 0,
34-
"Test::More" => 0
36+
"Import::Into" => 0,
37+
"Test::Lib" => 0,
38+
"Test::More" => 0,
39+
"namespace::autoclean" => 0
3540
},
3641
"VERSION" => "0.09",
3742
"test" => {
@@ -44,14 +49,19 @@ my %FallbackPrereqs = (
4449
"Carp" => 0,
4550
"Crypt::AuthEnc::GCM" => "0.062",
4651
"Crypt::Mode::CBC" => 0,
52+
"Crypt::OpenSSL::Guess" => 0,
4753
"Crypt::OpenSSL::X509" => 0,
4854
"Crypt::PK::RSA" => 0,
4955
"Crypt::PRNG" => 0,
56+
"Exporter" => 0,
5057
"File::Slurper" => 0,
5158
"File::Which" => 0,
59+
"Import::Into" => 0,
5260
"MIME::Base64" => 0,
61+
"Test::Lib" => 0,
5362
"Test::More" => 0,
5463
"XML::LibXML" => 0,
64+
"namespace::autoclean" => 0,
5565
"strict" => 0,
5666
"vars" => 0,
5767
"warnings" => 0

cpanfile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@ requires "vars" => "0";
1414
requires "warnings" => "0";
1515

1616
on 'test' => sub {
17+
requires "Crypt::OpenSSL::Guess" => "0";
18+
requires "Exporter" => "0";
1719
requires "File::Slurper" => "0";
1820
requires "File::Which" => "0";
21+
requires "Import::Into" => "0";
22+
requires "Test::Lib" => "0";
1923
requires "Test::More" => "0";
24+
requires "namespace::autoclean" => "0";
2025
};
2126

2227
on 'configure' => sub {

t/06-test-encryption-methods.t

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use strict;
22
use warnings;
33
use Test::More tests => 126;
4+
use Test::Lib;
5+
use Test::XML::Enc;
46
use XML::Enc;
57
use MIME::Base64 qw/decode_base64 encode_base64/;
6-
use File::Which;
78

89
my $xml = <<'XML';
910
<?xml version="1.0"?>
@@ -16,6 +17,9 @@ my @key_methods = qw/rsa-1_5 rsa-oaep-mgf1p/;
1617
my @data_methods = qw/aes128-cbc aes192-cbc aes256-cbc tripledes-cbc aes128-gcm aes192-gcm aes256-gcm/;
1718
my @oaep_mgf_algs = qw/mgf1sha1 mgf1sha224 mgf1sha256 mgf1sha384 mgf1sha512/;
1819

20+
my $xmlsec = get_xmlsec_features();
21+
my $lax_key_search = $xmlsec->{lax_key_search} ? '--lax_key_search': '';
22+
1923
foreach my $km (@key_methods) {
2024
foreach my $dm (@data_methods) {
2125
my $encrypter = XML::Enc->new(
@@ -34,16 +38,16 @@ foreach my $km (@key_methods) {
3438
ok($encrypter->decrypt($encrypted) =~ /XML-SIG_1/, "Successfully Decrypted with XML::Enc");
3539

3640
SKIP: {
37-
skip "xmlsec1 not installed", 2 unless which('xmlsec1');
41+
skip "xmlsec1 not installed", 2 unless $xmlsec->{installed};
3842
my $version;
3943
if (`xmlsec1 version` =~ m/(\d+\.\d+\.\d+)/) {
4044
$version = $1;
4145
};
42-
skip "xmlsec version 1.2.27 minimum for GCM", 2 if $version lt '1.2.27';
46+
skip "xmlsec version 1.2.27 minimum for GCM", 2 if ! $xmlsec->{aes_gcm};
4347
ok( open XML, '>', 'tmp.xml' );
4448
print XML $encrypted;
4549
close XML;
46-
my $verify_response = `xmlsec1 --decrypt --privkey-pem t/sign-private.pem tmp.xml 2>&1`;
50+
my $verify_response = `xmlsec1 --decrypt $lax_key_search --privkey-pem t/sign-private.pem tmp.xml 2>&1`;
4751
ok( $verify_response =~ m/XML-SIG_1/, "Successfully decrypted with xmlsec1" )
4852
or warn "calling xmlsec1 failed: '$verify_response'\n";
4953
unlink 'tmp.xml';

t/07-decrypt-xmlsec.t

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use strict;
22
use warnings;
33
use Test::More tests => 70;
4+
use Test::Lib;
5+
use Test::XML::Enc;
46
use XML::Enc;
57
use MIME::Base64 qw/decode_base64/;
6-
use File::Which;
78
use File::Slurper qw/read_text/;
89

910
my $plaintext = <<'UNENCRYPTED';
@@ -41,6 +42,9 @@ my %sesskey = (
4142
'aes256-gcm' => 'aes-256-GCM',
4243
);
4344

45+
my $xmlsec = get_xmlsec_features();
46+
my $lax_key_search = $xmlsec->{lax_key_search} ? '--lax-key-search' : '';
47+
4448
foreach my $km (@key_methods) {
4549
foreach my $dm (@data_methods) {
4650

@@ -103,13 +107,9 @@ XML Security Library example: Original XML
103107
CONTENT
104108

105109
SKIP: {
106-
skip "xmlsec1 not installed", 5 unless which('xmlsec1');
110+
skip "xmlsec1 not installed", 5 unless $xmlsec->{installed};
107111
skip "xmlsec1 no support for MGF element", 5 if $km eq 'rsa-oaep';
108-
my $version;
109-
if (`xmlsec1 version` =~ m/(\d+\.\d+\.\d+)/) {
110-
$version = $1;
111-
};
112-
skip "xmlsec version 1.2.27 minimum for GCM", 5 if $version lt '1.2.27';
112+
skip "xmlsec version 1.2.27 minimum for GCM", 5 if ! $xmlsec->{aes_gcm};
113113

114114
ok( open XML, '>', 'plaintext.xml' );
115115
print XML $plaintext;
@@ -120,7 +120,7 @@ SKIP: {
120120
close ELEMENT;
121121

122122
# Encrypt using xmlsec
123-
my $encrypt_response = `xmlsec1 encrypt --pubkey-cert-pem t/sign-certonly.pem --session-key $sesskey{$dm} --xml-data plaintext.xml --output encrypted-element.xml element_tmpl.xml 2>&1`;
123+
my $encrypt_response = `xmlsec1 encrypt $lax_key_search --pubkey-cert-pem t/sign-certonly.pem --session-key $sesskey{$dm} --xml-data plaintext.xml --output encrypted-element.xml element_tmpl.xml 2>&1`;
124124

125125
my $encrypted = read_text('encrypted-element.xml');
126126

@@ -143,7 +143,7 @@ SKIP: {
143143
print CONTENT $content_tmpl;
144144
close CONTENT;
145145

146-
$encrypt_response = `xmlsec1 encrypt --pubkey-cert-pem t/sign-certonly.pem --session-key $sesskey{$dm} --xml-data plaintext.xml --output encrypted-content.xml --node-xpath '/PayInfo/CreditCard/Number' content-template.xml 2>&1`;
146+
$encrypt_response = `xmlsec1 encrypt $lax_key_search --pubkey-cert-pem t/sign-certonly.pem --session-key $sesskey{$dm} --xml-data plaintext.xml --output encrypted-content.xml --node-xpath '/PayInfo/CreditCard/Number' content-template.xml 2>&1`;
147147

148148
$encrypted = read_text('encrypted-content.xml');
149149

t/lib/Test/XML/Enc.pm

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package Test::XML::Enc;
2+
use strict;
3+
use warnings;
4+
use namespace::autoclean ();
5+
6+
use Test::Lib;
7+
8+
# ABSTRACT: Test module for XML::Enc
9+
10+
use Import::Into;
11+
12+
use Test::XML::Enc::Util ();
13+
14+
sub import {
15+
16+
my $caller_level = 1;
17+
18+
my @imports = qw(
19+
Test::XML::Enc::Util
20+
namespace::autoclean
21+
strict
22+
warnings
23+
);
24+
25+
$_->import::into($caller_level) for @imports;
26+
}
27+
28+
=head1 DESCRIPTION
29+
30+
Main test module for XML::Enc
31+
32+
=head1 SYNOPSIS
33+
34+
use Test::Lib;
35+
use Test::XML::Enc;
36+
37+
# tests here
38+
39+
...;
40+
41+
done_testing();
42+
43+
=cut
44+
45+
1;
46+
__END__

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

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package Test::XML::Enc::Util;
2+
use warnings;
3+
use strict;
4+
5+
# ABSTRACT: Utils for testsuite of XML::Enc
6+
7+
require Exporter;
8+
our @ISA = qw(Exporter);
9+
our @EXPORT = qw(
10+
get_xmlsec_features
11+
get_openssl_features
12+
);
13+
14+
our @EXPORT_OK;
15+
16+
our %EXPORT_TAGS = (
17+
all => [@EXPORT, @EXPORT_OK],
18+
);
19+
20+
use File::Which;
21+
use Crypt::OpenSSL::Guess;
22+
23+
#########################################################################
24+
# get_xmlsec_features
25+
#
26+
# Parameter: none
27+
#
28+
# Returns a hash of the major, minor and letter version of xmlsec
29+
# it also sets features to true or false depending if it is supported
30+
# in the version that is installed
31+
#
32+
# Response: hash
33+
#
34+
# %features = (
35+
# installed => 1,
36+
# major => '1',
37+
# minor => '3',
38+
# patch => '0',
39+
# ripemd160 => 0,
40+
# );
41+
##########################################################################
42+
sub get_xmlsec_features {
43+
return unless which('xmlsec1');
44+
45+
my ($cmd, $ver, $engine) = split / /, (`xmlsec1 --version`);
46+
my ($major, $minor, $patch) = split /\./, $ver;
47+
48+
my %xmlsec = (
49+
installed => 1,
50+
major => $major,
51+
minor => $minor,
52+
patch => $patch,
53+
ripemd160 => ($major >= 1 and $minor >= 3) ? 1 : 0,
54+
aes_gcm => ($major >= 1 and $minor >= 2 and $patch >= 27) ? 1 : 0,
55+
lax_key_search => ($major >= 1 and $minor >= 3) ? 1 : 0,
56+
);
57+
return \%xmlsec;
58+
}
59+
60+
#########################################################################
61+
# get_openssl_features
62+
#
63+
# Parameter: none
64+
#
65+
# Returns a hash of the major, minor and letter version of openssl
66+
# it also sets features to true or false depending if it is supported
67+
# in the version that is installed
68+
#
69+
# Response: hash
70+
#
71+
# %features = (
72+
# major => '3.0',
73+
# minor => '0',
74+
# letter => '',
75+
# ripemd160 => 0,
76+
# );
77+
##########################################################################
78+
sub get_openssl_features {
79+
my ($major, $minor, $letter) = Crypt::OpenSSL::Guess->openssl_version();
80+
81+
my %openssl = (
82+
major => $major,
83+
minor => $minor,
84+
letter => (defined $letter) ? $letter : '',
85+
ripemd160 => ($major eq '3.0' and ($minor >= 0) and ($minor <= 7)) ? 0 : 1,
86+
);
87+
return \%openssl;
88+
}
89+
90+
1;
91+
92+
__END__
93+
94+
=head1 DESCRIPTION
95+
96+
=head1 SYNOPSIS
97+
98+
use Test::XML::Enc;
99+
100+
my $features = get_xmlsec_features();
101+
my $features = get_openssl_features();
102+
# go from here

0 commit comments

Comments
 (0)