1#! /usr/bin/env perl 2# Copyright 2017-2023 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 : 5) # Extra FIPS related tests 28 + 16; 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"); 106ok(run(app([ 'openssl', 'rsa', '-in', 'genrsatest.pem', '-out', 'genrsatest-enc.pem', 107 '-aes256', '-passout', 'pass:x' ])), 108 "rsa encrypt"); 109# Check the default salt length for PBKDF2 is 16 bytes 110# We expect the output to be of the form "0:d=0 hl=2 l= 16 prim: OCTET STRING [HEX DUMP]:FAC7F37508E6B7A805BF4B13861B3687" 111# i.e. 2 byte header + 16 byte salt. 112ok(run(app(([ 'openssl', 'asn1parse', 113 '-in', 'genrsatest-enc.pem', 114 '-offset', '34', '-length', '18']))), 115 "Check the default size of the PBKDF2 PARAM 'salt length' is 16"); 116ok(run(app([ 'openssl', 'rsa', '-in', 'genrsatest-enc.pem', '-passin', 'pass:x' ])), 117 "rsa decrypt"); 118 119unless ($no_fips) { 120 my $provconf = srctop_file("test", "fips-and-base.cnf"); 121 my $provpath = bldtop_dir("providers"); 122 my @prov = ( "-provider-path", $provpath, 123 "-config", $provconf); 124 125 $ENV{OPENSSL_TEST_LIBCTX} = "1"; 126 ok(run(app(['openssl', 'genpkey', 127 @prov, 128 '-algorithm', 'RSA', 129 '-pkeyopt', 'bits:2080', 130 '-out', 'genrsatest2080.pem'])), 131 "Generating RSA key with > 2048 bits and < 3072 bits"); 132 ok(run(app(['openssl', 'genpkey', 133 @prov, 134 '-algorithm', 'RSA', 135 '-pkeyopt', 'bits:3072', 136 '-out', 'genrsatest3072.pem'])), 137 "Generating RSA key with 3072 bits"); 138 139 ok(!run(app(['openssl', 'genrsa', @prov, '512'])), 140 "Generating RSA key with 512 bits should fail in FIPS provider"); 141 142 ok(!run(app(['openssl', 'genrsa', 143 @prov, 144 '-provider', 'default', 145 '-propquery', '?fips!=yes', 146 '512'])), 147 "Generating RSA key with 512 bits should succeed with FIPS provider as". 148 " default with a non-FIPS property query"); 149 150 # We want to know that an absurdly large number of bits fails the RNG check 151 is(run(app([ 'openssl', 'genpkey', 152 @prov, 153 '-algorithm', 'RSA', 154 '-pkeyopt', 'bits:1000000000', 155 '-out', 'genrsatest.pem'])), 156 0, "genpkey 1000000000"); 157} 158