0f82b1f21a49a3df15683733b0996178f6814484
[openssl.git] / test / recipes / 20-test_pkeyutl.t
1 #! /usr/bin/env perl
2 # Copyright 2018 The OpenSSL Project Authors. All Rights Reserved.
3 #
4 # Licensed under the Apache License 2.0 (the "License").  You may not use
5 # this file except in compliance with the License.  You can obtain a copy
6 # in the file LICENSE in the source distribution or at
7 # https://www.openssl.org/source/license.html
8
9 use strict;
10 use warnings;
11
12 use File::Spec;
13 use OpenSSL::Test qw/:DEFAULT srctop_file ok_nofips/;
14 use OpenSSL::Test::Utils;
15
16 setup("test_pkeyutl");
17
18 plan tests => 11;
19
20 # For the tests below we use the cert itself as the TBS file
21
22 SKIP: {
23     skip "Skipping tests that require EC, SM2 or SM3", 2
24         if disabled("ec") || disabled("sm2") || disabled("sm3");
25
26     # SM2
27     ok_nofips(run(app(([ 'openssl', 'pkeyutl', '-sign',
28                       '-in', srctop_file('test', 'certs', 'sm2.pem'),
29                       '-inkey', srctop_file('test', 'certs', 'sm2.key'),
30                       '-out', 'signature.dat', '-rawin',
31                       '-digest', 'sm3', '-pkeyopt', 'sm2_id:someid']))),
32                       "Sign a piece of data using SM2");
33     ok_nofips(run(app(([ 'openssl', 'pkeyutl', '-verify', '-certin',
34                       '-in', srctop_file('test', 'certs', 'sm2.pem'),
35                       '-inkey', srctop_file('test', 'certs', 'sm2.pem'),
36                       '-sigfile', 'signature.dat', '-rawin',
37                       '-digest', 'sm3', '-pkeyopt', 'sm2_id:someid']))),
38                       "Verify an SM2 signature against a piece of data");
39 }
40
41 SKIP: {
42     skip "Skipping tests that require EC", 4
43         if disabled("ec");
44
45     # Ed25519
46     ok(run(app(([ 'openssl', 'pkeyutl', '-sign', '-in',
47                   srctop_file('test', 'certs', 'server-ed25519-cert.pem'),
48                   '-inkey', srctop_file('test', 'certs', 'server-ed25519-key.pem'),
49                   '-out', 'signature.dat', '-rawin']))),
50                   "Sign a piece of data using Ed25519");
51     ok(run(app(([ 'openssl', 'pkeyutl', '-verify', '-certin', '-in',
52                   srctop_file('test', 'certs', 'server-ed25519-cert.pem'),
53                   '-inkey', srctop_file('test', 'certs', 'server-ed25519-cert.pem'),
54                   '-sigfile', 'signature.dat', '-rawin']))),
55                   "Verify an Ed25519 signature against a piece of data");
56
57     # Ed448
58     ok(run(app(([ 'openssl', 'pkeyutl', '-sign', '-in',
59                   srctop_file('test', 'certs', 'server-ed448-cert.pem'),
60                   '-inkey', srctop_file('test', 'certs', 'server-ed448-key.pem'),
61                   '-out', 'signature.dat', '-rawin']))),
62                   "Sign a piece of data using Ed448");
63     ok(run(app(([ 'openssl', 'pkeyutl', '-verify', '-certin', '-in',
64                   srctop_file('test', 'certs', 'server-ed448-cert.pem'),
65                   '-inkey', srctop_file('test', 'certs', 'server-ed448-cert.pem'),
66                   '-sigfile', 'signature.dat', '-rawin']))),
67                   "Verify an Ed448 signature against a piece of data");
68 }
69
70 unlink 'signature.dat';
71
72 sub tsignverify {
73     my $testtext = shift;
74     my $privkey = shift;
75     my $pubkey = shift;
76     my @extraopts = @_;
77
78     my $data_to_sign = srctop_file('test', 'README');
79     my $other_data = srctop_file('test', 'README.external');
80     my $sigfile = 'testpkeyutl.sig';
81
82     my @args = ();
83     plan tests => 4;
84
85     @args = ('openssl', 'pkeyutl', '-sign',
86              '-inkey', $privkey,
87              '-out', $sigfile,
88              '-in', $data_to_sign);
89     push(@args, @extraopts);
90     ok(run(app([@args])),
91        $testtext.": Generating signature");
92
93     @args = ('openssl', 'pkeyutl', '-verify',
94              '-inkey', $privkey,
95              '-sigfile', $sigfile,
96              '-in', $data_to_sign);
97     push(@args, @extraopts);
98     ok(run(app([@args])),
99        $testtext.": Verify signature with private key");
100
101     @args = ('openssl', 'pkeyutl', '-verify',
102              '-inkey', $pubkey, '-pubin',
103              '-sigfile', $sigfile,
104              '-in', $data_to_sign);
105     push(@args, @extraopts);
106     ok(run(app([@args])),
107        $testtext.": Verify signature with public key");
108
109     @args = ('openssl', 'pkeyutl', '-verify',
110              '-inkey', $pubkey, '-pubin',
111              '-sigfile', $sigfile,
112              '-in', $other_data);
113     push(@args, @extraopts);
114     ok(!run(app([@args])),
115        $testtext.": Expect failure verifying mismatching data");
116
117     unlink $sigfile;
118 }
119
120 SKIP: {
121     skip "RSA is not supported by this OpenSSL build", 1
122         if disabled("rsa");
123
124     subtest "RSA CLI signature generation and verification" => sub {
125         tsignverify("RSA",
126                     srctop_file("test","testrsa.pem"),
127                     srctop_file("test","testrsapub.pem"),
128                     "-rawin", "-digest", "sha256");
129     };
130 }
131
132 SKIP: {
133     skip "DSA is not supported by this OpenSSL build", 1
134         if disabled("dsa");
135
136     subtest "DSA CLI signature generation and verification" => sub {
137         tsignverify("DSA",
138                     srctop_file("test","testdsa.pem"),
139                     srctop_file("test","testdsapub.pem"),
140                     "-rawin", "-digest", "sha256");
141     };
142 }
143
144 SKIP: {
145     skip "ECDSA is not supported by this OpenSSL build", 1
146         if disabled("ec");
147
148     subtest "ECDSA CLI signature generation and verification" => sub {
149         tsignverify("ECDSA",
150                     srctop_file("test","testec-p256.pem"),
151                     srctop_file("test","testecpub-p256.pem"),
152                     "-rawin", "-digest", "sha256");
153     };
154 }
155
156 SKIP: {
157     skip "EdDSA is not supported by this OpenSSL build", 2
158         if disabled("ec");
159
160     subtest "Ed2559 CLI signature generation and verification" => sub {
161         tsignverify("Ed25519",
162                     srctop_file("test","tested25519.pem"),
163                     srctop_file("test","tested25519pub.pem"),
164                     "-rawin");
165     };
166
167     subtest "Ed448 CLI signature generation and verification" => sub {
168         tsignverify("Ed448",
169                     srctop_file("test","tested448.pem"),
170                     srctop_file("test","tested448pub.pem"),
171                     "-rawin");
172     };
173 }