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