1*e0c4386eSCy Schubert#! /usr/bin/env perl 2*e0c4386eSCy Schubert# Copyright 2017-2022 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 srctop_file srctop_dir bldtop_dir bldtop_file/; 15*e0c4386eSCy Schubertuse OpenSSL::Test::Utils; 16*e0c4386eSCy Schubert 17*e0c4386eSCy SchubertBEGIN { 18*e0c4386eSCy Schubert setup("test_genrsa"); 19*e0c4386eSCy Schubert} 20*e0c4386eSCy Schubert 21*e0c4386eSCy Schubertuse lib srctop_dir('Configurations'); 22*e0c4386eSCy Schubertuse lib bldtop_dir('.'); 23*e0c4386eSCy Schubert 24*e0c4386eSCy Schubertmy $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0); 25*e0c4386eSCy Schubert 26*e0c4386eSCy Schubertplan tests => 27*e0c4386eSCy Schubert ($no_fips ? 0 : 5) # Extra FIPS related tests 28*e0c4386eSCy Schubert + 15; 29*e0c4386eSCy Schubert 30*e0c4386eSCy Schubert# We want to know that an absurdly small number of bits isn't support 31*e0c4386eSCy Schubertis(run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem', 32*e0c4386eSCy Schubert '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_bits:8', 33*e0c4386eSCy Schubert '-pkeyopt', 'rsa_keygen_pubexp:3'])), 34*e0c4386eSCy Schubert 0, "genpkey 8"); 35*e0c4386eSCy Schubertis(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', '8'])), 36*e0c4386eSCy Schubert 0, "genrsa -3 8"); 37*e0c4386eSCy Schubert 38*e0c4386eSCy Schubert# Depending on the shared library, we might have different lower limits. 39*e0c4386eSCy Schubert# Let's find it! This is a simple binary search 40*e0c4386eSCy Schubert# ------------------------------------------------------------ 41*e0c4386eSCy Schubert# NOTE: $good may need an update in the future 42*e0c4386eSCy Schubert# ------------------------------------------------------------ 43*e0c4386eSCy Schubertnote "Looking for lowest amount of bits"; 44*e0c4386eSCy Schubertmy $bad = 3; # Log2 of number of bits (2 << 3 == 8) 45*e0c4386eSCy Schubertmy $good = 11; # Log2 of number of bits (2 << 11 == 2048) 46*e0c4386eSCy Schubertmy $fin; 47*e0c4386eSCy Schubertwhile ($good > $bad + 1) { 48*e0c4386eSCy Schubert my $checked = int(($good + $bad + 1) / 2); 49*e0c4386eSCy Schubert my $bits = 2 ** $checked; 50*e0c4386eSCy Schubert $fin = run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem', 51*e0c4386eSCy Schubert '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_pubexp:65537', 52*e0c4386eSCy Schubert '-pkeyopt', "rsa_keygen_bits:$bits", 53*e0c4386eSCy Schubert ], stderr => undef)); 54*e0c4386eSCy Schubert if ($fin) { 55*e0c4386eSCy Schubert note 2 ** $checked, " bits is good"; 56*e0c4386eSCy Schubert $good = $checked; 57*e0c4386eSCy Schubert } else { 58*e0c4386eSCy Schubert note 2 ** $checked, " bits is bad"; 59*e0c4386eSCy Schubert $bad = $checked; 60*e0c4386eSCy Schubert } 61*e0c4386eSCy Schubert} 62*e0c4386eSCy Schubert$good++ if $good == $bad; 63*e0c4386eSCy Schubert$good = 2 ** $good; 64*e0c4386eSCy Schubertnote "Found lowest allowed amount of bits to be $good"; 65*e0c4386eSCy Schubert 66*e0c4386eSCy Schubertok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA', 67*e0c4386eSCy Schubert '-pkeyopt', 'rsa_keygen_pubexp:65537', 68*e0c4386eSCy Schubert '-pkeyopt', "rsa_keygen_bits:$good", 69*e0c4386eSCy Schubert '-out', 'genrsatest.pem' ])), 70*e0c4386eSCy Schubert "genpkey $good"); 71*e0c4386eSCy Schubertok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest.pem', '-noout' ])), 72*e0c4386eSCy Schubert "pkey -check"); 73*e0c4386eSCy Schubert 74*e0c4386eSCy Schubertok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA', 75*e0c4386eSCy Schubert '-pkeyopt', 'rsa_keygen_bits:2048', 76*e0c4386eSCy Schubert '-out', 'genrsatest2048.pem' ])), 77*e0c4386eSCy Schubert "genpkey 2048 bits"); 78*e0c4386eSCy Schubertok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest2048.pem', '-noout' ])), 79*e0c4386eSCy Schubert "pkey -check"); 80*e0c4386eSCy Schubert 81*e0c4386eSCy Schubertok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA', 82*e0c4386eSCy Schubert '-pkeyopt', 'hexe:02', 83*e0c4386eSCy Schubert '-out', 'genrsatest.pem' ])), 84*e0c4386eSCy Schubert "genpkey with a bad public exponent should fail"); 85*e0c4386eSCy Schubertok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA', 86*e0c4386eSCy Schubert '-pkeyopt', 'e:65538', 87*e0c4386eSCy Schubert '-out', 'genrsatest.pem' ])), 88*e0c4386eSCy Schubert "genpkey with a even public exponent should fail"); 89*e0c4386eSCy Schubertok(!run(app([ 'openssl', 'genpkey', '-propquery', 'unknown', 90*e0c4386eSCy Schubert '-algorithm', 'RSA' ])), 91*e0c4386eSCy Schubert "genpkey requesting unknown=yes property should fail"); 92*e0c4386eSCy Schubert 93*e0c4386eSCy Schubert SKIP: { 94*e0c4386eSCy Schubert skip "Skipping rsa command line test", 2 if disabled("deprecated-3.0"); 95*e0c4386eSCy Schubert 96*e0c4386eSCy Schubert ok(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', $good ])), 97*e0c4386eSCy Schubert "genrsa -3 $good"); 98*e0c4386eSCy Schubert ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])), 99*e0c4386eSCy Schubert "rsa -check"); 100*e0c4386eSCy Schubert } 101*e0c4386eSCy Schubert 102*e0c4386eSCy Schubertok(run(app([ 'openssl', 'genrsa', '-f4', '-out', 'genrsatest.pem', $good ])), 103*e0c4386eSCy Schubert "genrsa -f4 $good"); 104*e0c4386eSCy Schubertok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])), 105*e0c4386eSCy Schubert "rsa -check"); 106*e0c4386eSCy Schubertok(run(app([ 'openssl', 'rsa', '-in', 'genrsatest.pem', '-out', 'genrsatest-enc.pem', 107*e0c4386eSCy Schubert '-aes256', '-passout', 'pass:x' ])), 108*e0c4386eSCy Schubert "rsa encrypt"); 109*e0c4386eSCy Schubertok(run(app([ 'openssl', 'rsa', '-in', 'genrsatest-enc.pem', '-passin', 'pass:x' ])), 110*e0c4386eSCy Schubert "rsa decrypt"); 111*e0c4386eSCy Schubert 112*e0c4386eSCy Schubertunless ($no_fips) { 113*e0c4386eSCy Schubert my $provconf = srctop_file("test", "fips-and-base.cnf"); 114*e0c4386eSCy Schubert my $provpath = bldtop_dir("providers"); 115*e0c4386eSCy Schubert my @prov = ( "-provider-path", $provpath, 116*e0c4386eSCy Schubert "-config", $provconf); 117*e0c4386eSCy Schubert 118*e0c4386eSCy Schubert $ENV{OPENSSL_TEST_LIBCTX} = "1"; 119*e0c4386eSCy Schubert ok(run(app(['openssl', 'genpkey', 120*e0c4386eSCy Schubert @prov, 121*e0c4386eSCy Schubert '-algorithm', 'RSA', 122*e0c4386eSCy Schubert '-pkeyopt', 'bits:2080', 123*e0c4386eSCy Schubert '-out', 'genrsatest2080.pem'])), 124*e0c4386eSCy Schubert "Generating RSA key with > 2048 bits and < 3072 bits"); 125*e0c4386eSCy Schubert ok(run(app(['openssl', 'genpkey', 126*e0c4386eSCy Schubert @prov, 127*e0c4386eSCy Schubert '-algorithm', 'RSA', 128*e0c4386eSCy Schubert '-pkeyopt', 'bits:3072', 129*e0c4386eSCy Schubert '-out', 'genrsatest3072.pem'])), 130*e0c4386eSCy Schubert "Generating RSA key with 3072 bits"); 131*e0c4386eSCy Schubert 132*e0c4386eSCy Schubert ok(!run(app(['openssl', 'genrsa', @prov, '512'])), 133*e0c4386eSCy Schubert "Generating RSA key with 512 bits should fail in FIPS provider"); 134*e0c4386eSCy Schubert 135*e0c4386eSCy Schubert ok(!run(app(['openssl', 'genrsa', 136*e0c4386eSCy Schubert @prov, 137*e0c4386eSCy Schubert '-provider', 'default', 138*e0c4386eSCy Schubert '-propquery', '?fips!=yes', 139*e0c4386eSCy Schubert '512'])), 140*e0c4386eSCy Schubert "Generating RSA key with 512 bits should succeed with FIPS provider as". 141*e0c4386eSCy Schubert " default with a non-FIPS property query"); 142*e0c4386eSCy Schubert 143*e0c4386eSCy Schubert # We want to know that an absurdly large number of bits fails the RNG check 144*e0c4386eSCy Schubert is(run(app([ 'openssl', 'genpkey', 145*e0c4386eSCy Schubert @prov, 146*e0c4386eSCy Schubert '-algorithm', 'RSA', 147*e0c4386eSCy Schubert '-pkeyopt', 'bits:1000000000', 148*e0c4386eSCy Schubert '-out', 'genrsatest.pem'])), 149*e0c4386eSCy Schubert 0, "genpkey 1000000000"); 150*e0c4386eSCy Schubert} 151