"Downgrade" provider-native keys to legacy where needed
[openssl.git] / test / recipes / 20-test_pkeyutl.t
1 #! /usr/bin/env perl
2 # Copyright 2018-2020 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 File::Basename;
14 use OpenSSL::Test qw/:DEFAULT srctop_file ok_nofips/;
15 use OpenSSL::Test::Utils;
16
17 setup("test_pkeyutl");
18
19 plan tests => 11;
20
21 # For the tests below we use the cert itself as the TBS file
22
23 SKIP: {
24     skip "Skipping tests that require EC, SM2 or SM3", 2
25         if disabled("ec") || disabled("sm2") || disabled("sm3");
26
27     # TODO(3.0) Remove this when we have a SM2 keymgmt and decoder
28     my @tmp_sm2_hack = qw(-engine loader_attic)
29         unless disabled('dynamic-engine') || disabled('deprecated-3.0');
30     skip "Skipping tests that require dynamic enginess (temporary meaasure)", 2
31         unless @tmp_sm2_hack;
32
33     # SM2
34     ok_nofips(run(app(([ 'openssl', 'pkeyutl', @tmp_sm2_hack, '-sign',
35                       '-in', srctop_file('test', 'certs', 'sm2.pem'),
36                       '-inkey', srctop_file('test', 'certs', 'sm2.key'),
37                       '-out', 'sm2.sig', '-rawin',
38                       '-digest', 'sm3', '-pkeyopt', 'distid:someid']))),
39                       "Sign a piece of data using SM2");
40     ok_nofips(run(app(([ 'openssl', 'pkeyutl', @tmp_sm2_hack,
41                       '-verify', '-certin',
42                       '-in', srctop_file('test', 'certs', 'sm2.pem'),
43                       '-inkey', srctop_file('test', 'certs', 'sm2.pem'),
44                       '-sigfile', 'sm2.sig', '-rawin',
45                       '-digest', 'sm3', '-pkeyopt', 'distid:someid']))),
46                       "Verify an SM2 signature against a piece of data");
47 }
48
49 SKIP: {
50     skip "Skipping tests that require EC", 4
51         if disabled("ec");
52
53     # Ed25519
54     ok(run(app(([ 'openssl', 'pkeyutl', '-sign', '-in',
55                   srctop_file('test', 'certs', 'server-ed25519-cert.pem'),
56                   '-inkey', srctop_file('test', 'certs', 'server-ed25519-key.pem'),
57                   '-out', 'Ed25519.sig', '-rawin']))),
58                   "Sign a piece of data using Ed25519");
59     ok(run(app(([ 'openssl', 'pkeyutl', '-verify', '-certin', '-in',
60                   srctop_file('test', 'certs', 'server-ed25519-cert.pem'),
61                   '-inkey', srctop_file('test', 'certs', 'server-ed25519-cert.pem'),
62                   '-sigfile', 'Ed25519.sig', '-rawin']))),
63                   "Verify an Ed25519 signature against a piece of data");
64
65     # Ed448
66     ok(run(app(([ 'openssl', 'pkeyutl', '-sign', '-in',
67                   srctop_file('test', 'certs', 'server-ed448-cert.pem'),
68                   '-inkey', srctop_file('test', 'certs', 'server-ed448-key.pem'),
69                   '-out', 'Ed448.sig', '-rawin']))),
70                   "Sign a piece of data using Ed448");
71     ok(run(app(([ 'openssl', 'pkeyutl', '-verify', '-certin', '-in',
72                   srctop_file('test', 'certs', 'server-ed448-cert.pem'),
73                   '-inkey', srctop_file('test', 'certs', 'server-ed448-cert.pem'),
74                   '-sigfile', 'Ed448.sig', '-rawin']))),
75                   "Verify an Ed448 signature against a piece of data");
76 }
77
78 sub tsignverify {
79     my $testtext = shift;
80     my $privkey = shift;
81     my $pubkey = shift;
82     my @extraopts = @_;
83
84     my $data_to_sign = srctop_file('test', 'data.txt');
85     my $other_data = srctop_file('test', 'data2.txt');
86     my $sigfile = basename($privkey, '.pem') . '.sig';
87
88     my @args = ();
89     plan tests => 4;
90
91     @args = ('openssl', 'pkeyutl', '-sign',
92              '-inkey', $privkey,
93              '-out', $sigfile,
94              '-in', $data_to_sign);
95     push(@args, @extraopts);
96     ok(run(app([@args])),
97        $testtext.": Generating signature");
98
99     @args = ('openssl', 'pkeyutl', '-verify',
100              '-inkey', $privkey,
101              '-sigfile', $sigfile,
102              '-in', $data_to_sign);
103     push(@args, @extraopts);
104     ok(run(app([@args])),
105        $testtext.": Verify signature with private key");
106
107     @args = ('openssl', 'pkeyutl', '-verify',
108              '-inkey', $pubkey, '-pubin',
109              '-sigfile', $sigfile,
110              '-in', $data_to_sign);
111     push(@args, @extraopts);
112     ok(run(app([@args])),
113        $testtext.": Verify signature with public key");
114
115     @args = ('openssl', 'pkeyutl', '-verify',
116              '-inkey', $pubkey, '-pubin',
117              '-sigfile', $sigfile,
118              '-in', $other_data);
119     push(@args, @extraopts);
120     ok(!run(app([@args])),
121        $testtext.": Expect failure verifying mismatching data");
122 }
123
124 SKIP: {
125     skip "RSA is not supported by this OpenSSL build", 1
126         if disabled("rsa");
127
128     subtest "RSA CLI signature generation and verification" => sub {
129         tsignverify("RSA",
130                     srctop_file("test","testrsa.pem"),
131                     srctop_file("test","testrsapub.pem"),
132                     "-rawin", "-digest", "sha256");
133     };
134 }
135
136 SKIP: {
137     skip "DSA is not supported by this OpenSSL build", 1
138         if disabled("dsa");
139
140     subtest "DSA CLI signature generation and verification" => sub {
141         tsignverify("DSA",
142                     srctop_file("test","testdsa.pem"),
143                     srctop_file("test","testdsapub.pem"),
144                     "-rawin", "-digest", "sha256");
145     };
146 }
147
148 SKIP: {
149     skip "ECDSA is not supported by this OpenSSL build", 1
150         if disabled("ec");
151
152     subtest "ECDSA CLI signature generation and verification" => sub {
153         tsignverify("ECDSA",
154                     srctop_file("test","testec-p256.pem"),
155                     srctop_file("test","testecpub-p256.pem"),
156                     "-rawin", "-digest", "sha256");
157     };
158 }
159
160 SKIP: {
161     skip "EdDSA is not supported by this OpenSSL build", 2
162         if disabled("ec");
163
164     subtest "Ed2559 CLI signature generation and verification" => sub {
165         tsignverify("Ed25519",
166                     srctop_file("test","tested25519.pem"),
167                     srctop_file("test","tested25519pub.pem"),
168                     "-rawin");
169     };
170
171     subtest "Ed448 CLI signature generation and verification" => sub {
172         tsignverify("Ed448",
173                     srctop_file("test","tested448.pem"),
174                     srctop_file("test","tested448pub.pem"),
175                     "-rawin");
176     };
177 }