1use warnings;
2use strict;
3use Test::More tests => 17;
4use Test::Exception;
5
6use Crypt::MatrixSSL3 qw( :DEFAULT :Error );
7
8my $trustedCAcertFiles  = 't/cert/testca.crt';
9
10my $certFile            = 't/cert/testserver.crt';
11my $privFile            = 't/cert/testserver.key';
12my $privPass            = undef;
13
14my $trustedCA; if(open(IN,'<',"$trustedCAcertFiles.der")) {local $/; $trustedCA=<IN>; close(IN); }
15my $cert; if(open(IN,'<',"$certFile.der")) {local $/; $cert=<IN>; close(IN); }
16my $priv; if(open(IN,'<',"$privFile.der")) {local $/; $priv=<IN>; close(IN); }
17
18my $privFile_des3       = $privFile.'.des3';
19my $privPass_des3       = 'test';
20
21
22is PS_SUCCESS, _load_rsa($certFile, $privFile, $privPass, undef),
23    'server: NO PASSWORD';
24is PS_SUCCESS, _load_rsa($certFile, $privFile, '', undef),
25    'server: EMPTY PASSWORD';
26is PS_SUCCESS, _load_rsa($certFile, $privFile, 'a_n_y', undef),
27    'server: ANY PASSWORD';
28is PS_SUCCESS, _load_rsa(undef, undef, undef, $trustedCAcertFiles),
29    'client';
30is PS_SUCCESS, _load_rsa($certFile, $privFile, $privPass, $trustedCAcertFiles),
31    'both';
32is PS_SUCCESS, _load_rsa_mem($cert, $priv, undef),
33    'Mem server';
34is PS_SUCCESS, _load_rsa_mem(undef, undef, $trustedCA),
35    'Mem client';
36is PS_SUCCESS, _load_rsa_mem($cert, $priv, $trustedCA),
37    'Mem both';
38
39is PS_SUCCESS, _load_rsa($certFile, $privFile_des3, $privPass_des3, undef),
40    'server: encrypted des3, RIGHT PASSWORD';
41is PS_ARG_FAIL, _load_rsa($certFile, $privFile_des3, $privPass, undef),
42    'server: encrypted des3, NO PASSWORD';
43is PS_FAILURE, _load_rsa($certFile, $privFile_des3, '', undef),
44    'server: encrypted des3, EMPTY PASSWORD';
45is PS_FAILURE, _load_rsa($certFile, $privFile_des3, 'WrOnG', undef),
46    'server: encrypted des3, WRONG PASSWORD';
47
48is PS_SUCCESS, _load_rsa(undef, undef, undef, undef),
49    'no keys';
50is PS_PARSE_FAIL, _load_rsa($0, undef, undef, undef),
51    'bad certFile';
52is PS_PARSE_FAIL, _load_rsa_mem('bad cert', undef, undef),
53    'bad cert';
54is PS_PLATFORM_FAIL, _load_rsa(undef, undef, undef, 'no such file'),
55    'no such certFile';
56is PS_CERT_AUTH_FAIL, _load_rsa('t/cert/testserver.crt', undef, undef, undef),
57    'certFile without priv key';
58
59# TODO MatrixSSL-3.3 crash (glibc double-free) on this test:
60# is PS_CERT_AUTH_FAIL, _load_rsa_mem($cert, undef, undef),
61#     'cert without priv key';
62
63# TODO MatrixSSL-3.3 doesn't support public key algorithms used in that file:
64# is PS_UNSUPPORTED_FAIL, _load_rsa(undef, undef, undef, '/etc/ssl/certs/ca-certificates.crt'),
65#     '';
66
67
68sub _load_rsa {
69    my $keys = Crypt::MatrixSSL3::Keys->new();
70    return $keys->load_rsa($_[0], $_[1], $_[2], $_[3]);
71}
72
73sub _load_rsa_mem {
74    my $keys = Crypt::MatrixSSL3::Keys->new();
75    return $keys->load_rsa_mem($_[0], $_[1], $_[2]);
76}
77
78