1e1051a39Sopenharmony_ci#! /usr/bin/env perl
2e1051a39Sopenharmony_ci# Copyright 2017-2022 The OpenSSL Project Authors. All Rights Reserved.
3e1051a39Sopenharmony_ci#
4e1051a39Sopenharmony_ci# Licensed under the Apache License 2.0 (the "License").  You may not use
5e1051a39Sopenharmony_ci# this file except in compliance with the License.  You can obtain a copy
6e1051a39Sopenharmony_ci# in the file LICENSE in the source distribution or at
7e1051a39Sopenharmony_ci# https://www.openssl.org/source/license.html
8e1051a39Sopenharmony_ci
9e1051a39Sopenharmony_ci
10e1051a39Sopenharmony_ciuse strict;
11e1051a39Sopenharmony_ciuse warnings;
12e1051a39Sopenharmony_ci
13e1051a39Sopenharmony_ciuse File::Spec;
14e1051a39Sopenharmony_ciuse OpenSSL::Test qw/:DEFAULT with srctop_file data_file/;
15e1051a39Sopenharmony_ciuse OpenSSL::Test::Utils;
16e1051a39Sopenharmony_ci
17e1051a39Sopenharmony_cisetup("test_rsapss");
18e1051a39Sopenharmony_ci
19e1051a39Sopenharmony_ciplan tests => 11;
20e1051a39Sopenharmony_ci
21e1051a39Sopenharmony_ci#using test/testrsa.pem which happens to be a 512 bit RSA
22e1051a39Sopenharmony_ciok(run(app(['openssl', 'dgst', '-sign', srctop_file('test', 'testrsa.pem'), '-sha1',
23e1051a39Sopenharmony_ci            '-sigopt', 'rsa_padding_mode:pss',
24e1051a39Sopenharmony_ci            '-sigopt', 'rsa_pss_saltlen:max',
25e1051a39Sopenharmony_ci            '-sigopt', 'rsa_mgf1_md:sha512',
26e1051a39Sopenharmony_ci            '-out', 'testrsapss-restricted.sig',
27e1051a39Sopenharmony_ci            srctop_file('test', 'testrsa.pem')])),
28e1051a39Sopenharmony_ci   "openssl dgst -sign [plain RSA key, PSS padding mode, PSS restrictions]");
29e1051a39Sopenharmony_ci
30e1051a39Sopenharmony_ciok(run(app(['openssl', 'dgst', '-sign', srctop_file('test', 'testrsa.pem'), '-sha1',
31e1051a39Sopenharmony_ci            '-sigopt', 'rsa_padding_mode:pss',
32e1051a39Sopenharmony_ci            '-out', 'testrsapss-unrestricted.sig',
33e1051a39Sopenharmony_ci            srctop_file('test', 'testrsa.pem')])),
34e1051a39Sopenharmony_ci   "openssl dgst -sign [plain RSA key, PSS padding mode, no PSS restrictions]");
35e1051a39Sopenharmony_ci
36e1051a39Sopenharmony_ciok(!run(app(['openssl', 'dgst', '-sign', srctop_file('test', 'testrsa.pem'), '-sha512',
37e1051a39Sopenharmony_ci             '-sigopt', 'rsa_padding_mode:pss', '-sigopt', 'rsa_pss_saltlen:max',
38e1051a39Sopenharmony_ci             '-sigopt', 'rsa_mgf1_md:sha512', srctop_file('test', 'testrsa.pem')])),
39e1051a39Sopenharmony_ci   "openssl dgst -sign, expect to fail gracefully");
40e1051a39Sopenharmony_ci
41e1051a39Sopenharmony_ciok(!run(app(['openssl', 'dgst', '-sign', srctop_file('test', 'testrsa.pem'), '-sha512',
42e1051a39Sopenharmony_ci             '-sigopt', 'rsa_padding_mode:pss', '-sigopt', 'rsa_pss_saltlen:2147483647',
43e1051a39Sopenharmony_ci             '-sigopt', 'rsa_mgf1_md:sha1', srctop_file('test', 'testrsa.pem')])),
44e1051a39Sopenharmony_ci   "openssl dgst -sign, expect to fail gracefully");
45e1051a39Sopenharmony_ci
46e1051a39Sopenharmony_ciok(!run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'), '-sha512',
47e1051a39Sopenharmony_ci             '-sigopt', 'rsa_padding_mode:pss', '-sigopt', 'rsa_pss_saltlen:max',
48e1051a39Sopenharmony_ci             '-sigopt', 'rsa_mgf1_md:sha512', '-signature', 'testrsapss.sig',
49e1051a39Sopenharmony_ci             srctop_file('test', 'testrsa.pem')])),
50e1051a39Sopenharmony_ci   "openssl dgst -prverify, expect to fail gracefully");
51e1051a39Sopenharmony_ci
52e1051a39Sopenharmony_ciok(run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'),
53e1051a39Sopenharmony_ci            '-sha1',
54e1051a39Sopenharmony_ci            '-sigopt', 'rsa_padding_mode:pss',
55e1051a39Sopenharmony_ci            '-sigopt', 'rsa_pss_saltlen:max',
56e1051a39Sopenharmony_ci            '-sigopt', 'rsa_mgf1_md:sha512',
57e1051a39Sopenharmony_ci            '-signature', 'testrsapss-restricted.sig',
58e1051a39Sopenharmony_ci            srctop_file('test', 'testrsa.pem')])),
59e1051a39Sopenharmony_ci   "openssl dgst -prverify [plain RSA key, PSS padding mode, PSS restrictions]");
60e1051a39Sopenharmony_ci
61e1051a39Sopenharmony_ciok(run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'),
62e1051a39Sopenharmony_ci            '-sha1',
63e1051a39Sopenharmony_ci            '-sigopt', 'rsa_padding_mode:pss',
64e1051a39Sopenharmony_ci            '-sigopt', 'rsa_pss_saltlen:42',
65e1051a39Sopenharmony_ci            '-sigopt', 'rsa_mgf1_md:sha512',
66e1051a39Sopenharmony_ci            '-signature', 'testrsapss-restricted.sig',
67e1051a39Sopenharmony_ci            srctop_file('test', 'testrsa.pem')])),
68e1051a39Sopenharmony_ci   "openssl dgst -sign rsa512bit.pem -sha1 -sigopt rsa_pss_saltlen:max produces 42 bits of PSS salt");
69e1051a39Sopenharmony_ci
70e1051a39Sopenharmony_ciok(run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'),
71e1051a39Sopenharmony_ci            '-sha1',
72e1051a39Sopenharmony_ci            '-sigopt', 'rsa_padding_mode:pss',
73e1051a39Sopenharmony_ci            '-signature', 'testrsapss-unrestricted.sig',
74e1051a39Sopenharmony_ci            srctop_file('test', 'testrsa.pem')])),
75e1051a39Sopenharmony_ci   "openssl dgst -prverify [plain RSA key, PSS padding mode, no PSS restrictions]");
76e1051a39Sopenharmony_ci
77e1051a39Sopenharmony_ci# Test that RSA-PSS keys are supported by genpkey and rsa commands.
78e1051a39Sopenharmony_ci{
79e1051a39Sopenharmony_ci   my $rsapss = "rsapss.key";
80e1051a39Sopenharmony_ci   ok(run(app(['openssl', 'genpkey', '-algorithm', 'RSA-PSS',
81e1051a39Sopenharmony_ci               '-pkeyopt', 'rsa_keygen_bits:1024',
82e1051a39Sopenharmony_ci               '--out', $rsapss])));
83e1051a39Sopenharmony_ci   ok(run(app(['openssl', 'rsa', '-check',
84e1051a39Sopenharmony_ci               '-in', $rsapss])));
85e1051a39Sopenharmony_ci}
86e1051a39Sopenharmony_ci
87e1051a39Sopenharmony_ciok(!run(app([ 'openssl', 'rsa',
88e1051a39Sopenharmony_ci             '-in' => data_file('negativesaltlen.pem')],
89e1051a39Sopenharmony_ci             '-out' => 'badout')));
90