summaryrefslogtreecommitdiff
path: root/t/openssl/rsa-test.pl
blob: 95f36c108c9ac82e09c3d01c5c8fda065ad3c206 (plain)
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
122
123
124
125
126
127
128
129
130
131
132
use strict;
use warnings;
use File::Glob 'bsd_glob';

sub runcmds {
  my $cmds = shift;
  for (split /\n/, $cmds) {
    s/^\s*(.*?)\s*$/$1/;
    warn "#### >$_<\n";
    my $rv = system($_);
    die "ERROR (rv = $rv)\n" if $rv;
  }
}

sub doit {

### enc openssl > cryptx
runcmds <<'MARKER';
 openssl rsautl -encrypt -inkey test_rsakey.pub.pem -pubin -out test_input.encrypted.rsa -in test_input.data
MARKER

{
  use Crypt::PK::RSA;
  use Crypt::Misc 'read_rawfile';
  
  my $pkrsa = Crypt::PK::RSA->new("test_rsakey.priv.pem");
  my $encfile = read_rawfile("test_input.encrypted.rsa");
  my $plaintext = $pkrsa->decrypt($encfile, 'v1.5');
  print $plaintext;
}

### enc cryptx > openssl
{
  use Crypt::PK::RSA;
  use Crypt::Misc 'write_rawfile';
  
  my $plaintext = 'secret message';
  my $pkrsa = Crypt::PK::RSA->new("test_rsakey.pub.pem");
  my $encrypted = $pkrsa->encrypt($plaintext, 'v1.5');
  write_rawfile("test_input.encrypted.rsa", $encrypted);
}

runcmds <<'MARKER';
 openssl rsautl -decrypt -inkey test_rsakey.priv.pem -in test_input.encrypted.rsa
MARKER

### sign openssl > cryptx
runcmds <<'MARKER';
  openssl dgst -sha1 -sign test_rsakey.priv.pem -out test_input.sha1-rsa.sig test_input.data
MARKER

{
 use Crypt::PK::RSA;
 use Crypt::Digest 'digest_file';
 use Crypt::Misc 'read_rawfile';
  
 my $pkrsa = Crypt::PK::RSA->new("test_rsakey.pub.pem");
 my $signature = read_rawfile("test_input.sha1-rsa.sig");
 my $valid = $pkrsa->verify_hash($signature, digest_file("SHA1", "test_input.data"), "SHA1", "v1.5");
 print $valid ? "SUCCESS" : "FAILURE";
}

### sign cryptx > openssl
{
 use Crypt::PK::RSA;
 use Crypt::Digest 'digest_file';
 use Crypt::Misc 'write_rawfile';
  
 my $pkrsa = Crypt::PK::RSA->new("test_rsakey.priv.pem");
 my $signature = $pkrsa->sign_hash(digest_file("SHA1", "test_input.data"), "SHA1", "v1.5");
 write_rawfile("test_input.sha1-rsa.sig", $signature);
}

runcmds <<'MARKER';
 openssl dgst -sha1 -verify test_rsakey.pub.pem -signature test_input.sha1-rsa.sig test_input.data
MARKER

}

### MAIN ###

write_rawfile("test_input.data", "test-file-content");

### keys generated by cryptx
{
 use Crypt::PK::RSA;
 use Crypt::Misc 'write_rawfile';
 
 my $pkrsa = Crypt::PK::RSA->new;
 $pkrsa->generate_key(256, 65537);
 write_rawfile("test_rsakey.pub.der",  $pkrsa->export_key_der('public'));
 write_rawfile("test_rsakey.priv.der", $pkrsa->export_key_der('private'));
 write_rawfile("test_rsakey.pub.pem",  $pkrsa->export_key_pem('public_x509'));
 write_rawfile("test_rsakey.priv.pem", $pkrsa->export_key_pem('private'));
 write_rawfile("test_rsakey-passwd.priv.pem", $pkrsa->export_key_pem('private', 'secret'));
}

runcmds <<'MARKER';
 openssl rsa -in test_rsakey.priv.der -text -inform der
 openssl rsa -in test_rsakey.priv.pem -text
 openssl rsa -in test_rsakey-passwd.priv.pem -text -inform pem -passin pass:secret
 openssl rsa -in test_rsakey.pub.der -pubin -text -inform der
 openssl rsa -in test_rsakey.pub.pem -pubin -text 
MARKER

doit();

### keys generated by openssl

runcmds <<'MARKER';
 openssl genrsa -out test_rsakey.priv.pem 1024
 openssl rsa -in test_rsakey.priv.pem -out test_rsakey.priv.der -outform der
 openssl rsa -in test_rsakey.priv.pem -out test_rsakey.pub.pem -pubout
 openssl rsa -in test_rsakey.priv.pem -out test_rsakey.pub.der -outform der -pubout
 openssl rsa -in test_rsakey.priv.pem -passout pass:secret -des3 -out test_rsakey-passwd.priv.pem
MARKER

{
 use Crypt::PK::RSA;
 
 my $pkrsa = Crypt::PK::RSA->new;
 $pkrsa->import_key("test_rsakey.pub.der");
 $pkrsa->import_key("test_rsakey.priv.der");
 $pkrsa->import_key("test_rsakey.pub.pem");
 $pkrsa->import_key("test_rsakey.priv.pem");
 $pkrsa->import_key("test_rsakey-passwd.priv.pem", "secret");
}

doit();

warn "\nSUCCESS\n";
unlink $_ for (bsd_glob("test_*.der"), bsd_glob("test_*.pem"), bsd_glob("test_*.sig"), bsd_glob("test_*.rsa"), bsd_glob("test_*.data"));