1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
# -*- perl -*-
use Test::More tests => 23;
use Test::Exception;
use File::Spec;
use strict;
use warnings;
BEGIN {
use Crypt::SMIME;
my $openssl = '/usr/local/ymir/perl/openssl/bin/openssl';
if (!-x $openssl) {
$openssl = '/usr/bin/openssl';
}
if(!-x $openssl && -e 'c:/openssl/bin/openssl.exe' )
{
$openssl = 'c:/openssl/bin/openssl.exe';
}
my $devnull = File::Spec->devnull();
open(FILE, "> tmp-$$.config") or die $!;
print FILE<<'CONFIG';
[ req ]
distinguished_name = req_distinguished_name
attributes = req_attributes
prompt = no
[ req_distinguished_name ]
C = AU
ST = Some-State
L = Test Locality
O = Organization Name
OU = Organizational Unit Name
CN = Common Name
emailAddress = test@email.address
[ req_attributes ]
CONFIG
close(FILE);
foreach my $i (1 .. 2) {
system(qq{$openssl genrsa > tmp-$$-$i.key 2>$devnull}) and die $!;
system(qq{$openssl req -new -key tmp-$$-$i.key -out tmp-$$-$i.csr -config tmp-$$.config >$devnull}) and die $!;
system(qq{$openssl x509 -in tmp-$$-$i.csr -out tmp-$$-$i.crt -req -signkey tmp-$$-$i.key -set_serial $i 2>$devnull >$devnull}) and die $!;
}
}
END {
foreach my $i (1 .. 2) {
unlink "tmp-$$-$i.key", "tmp-$$-$i.csr", "tmp-$$-$i.crt";
}
unlink("tmp-$$.config");
}
sub key {
my $i = shift;
local $/ = undef;
open my $fh, '<', "tmp-$$-$i.key";
<$fh>;
}
sub crt {
my $i = shift;
local $/ = undef;
open my $fh, '<', "tmp-$$-$i.crt";
<$fh>;
}
my $plain = q{From: alice@example.org
To: bob@example.org
Subject: Crypt::SMIME test
This is a test mail. Please ignore...
};
$plain =~ s/\r?\n|\r/\r\n/g;
my $verify = q{Subject: Crypt::SMIME test
This is a test mail. Please ignore...
};
$verify =~ s/\r?\n|\r/\r\n/g;
#-----------------------
my $smime;
ok($smime = Crypt::SMIME->new, 'new');
ok($smime->setPrivateKey(key(1), crt(1)), 'setPrivateKey (without passphrase)');
dies_ok {$smime->sign} 'sign undef';
dies_ok {$smime->sign(\123)} 'sign ref';
dies_ok {$smime->signonly} 'signonly undef';
dies_ok {$smime->signonly(\123)} 'signonly ref';
dies_ok {$smime->encrypt} 'encrypt undef';
dies_ok {$smime->encrypt(\123)} 'encrypt ref';
dies_ok {$smime->isSigned} 'isSigned undef';
dies_ok {$smime->isSigned(\123)} 'isSigned ref';
dies_ok {$smime->isEncrypted} 'isEncrypted undef';
dies_ok {$smime->isEncrypted(\123)} 'isEncrypted ref';
my $signed;
ok($signed = $smime->sign($plain), 'sign');
ok($smime->isSigned($signed), 'signed');
ok($smime->setPublicKey(crt(1)), 'setPublicKey (one key)');
my $checked;
ok($checked = $smime->check($signed), 'check');
is($checked, $verify, '$verify eq check(sign($plain))');
ok($smime->setPublicKey([crt(1), crt(2)]), 'setPublicKey (two keys)');
my $encrypted;
ok($encrypted = $smime->encrypt($plain), 'encrypt');
ok($smime->isEncrypted($encrypted), 'isEncrypted');
my $decrypted;
ok($decrypted = $smime->decrypt($encrypted), 'decrypt (by sender\'s key)');
is($decrypted, $verify, '$plain eq decrypt(encrypt($plain))');
$smime->setPrivateKey(key(2), crt(2));
ok($decrypted = $smime->decrypt($encrypted), 'decrypt (by recipient\'s key)');
1;
|