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