2 # Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
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
14 use OpenSSL::Test qw/:DEFAULT srctop_file srctop_dir bldtop_dir bldtop_file/;
15 use OpenSSL::Test::Utils;
21 use lib srctop_dir('Configurations');
22 use lib bldtop_dir('.');
24 my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
27 ($no_fips ? 0 : 2) # Extra FIPS related test
30 # We want to know that an absurdly small number of bits isn't support
31 if (disabled("deprecated-3.0")) {
32 is(run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem',
33 '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_bits:8',
34 '-pkeyopt', 'rsa_keygen_pubexp:3'])),
37 is(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', '8'])),
41 # Depending on the shared library, we might have different lower limits.
42 # Let's find it! This is a simple binary search
43 # ------------------------------------------------------------
44 # NOTE: $good may need an update in the future
45 # ------------------------------------------------------------
46 note "Looking for lowest amount of bits";
47 my $bad = 3; # Log2 of number of bits (2 << 3 == 8)
48 my $good = 11; # Log2 of number of bits (2 << 11 == 2048)
50 while ($good > $bad + 1) {
51 my $checked = int(($good + $bad + 1) / 2);
52 my $bits = 2 ** $checked;
53 if (disabled("deprecated-3.0")) {
54 $fin = run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem',
55 '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_pubexp:65537',
56 '-pkeyopt', "rsa_keygen_bits:$bits",
59 $fin = run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem',
64 note 2 ** $checked, " bits is good";
67 note 2 ** $checked, " bits is bad";
71 $good++ if $good == $bad;
73 note "Found lowest allowed amount of bits to be $good";
75 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
76 '-pkeyopt', 'rsa_keygen_pubexp:65537',
77 '-pkeyopt', "rsa_keygen_bits:$good",
78 '-out', 'genrsatest.pem' ])),
80 ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest.pem', '-noout' ])),
82 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
83 '-pkeyopt', 'rsa_keygen_pubexp:65537',
84 '-pkeyopt', "rsa_keygen_bits:$good",
85 '-out', 'genrsatest.pem' ])),
88 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
89 '-pkeyopt', 'rsa_keygen_bits:2048',
90 '-out', 'genrsatest2048.pem' ])),
92 ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest2048.pem', '-noout' ])),
95 ok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
96 '-pkeyopt', 'hexe:02',
97 '-out', 'genrsatest.pem' ])),
98 "genpkey with a bad public exponent should fail");
99 ok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
100 '-pkeyopt', 'e:65538',
101 '-out', 'genrsatest.pem' ])),
102 "genpkey with a even public exponent should fail");
103 ok(!run(app([ 'openssl', 'genpkey', '-propquery', 'unknown',
104 '-algorithm', 'RSA' ])),
105 "genpkey requesting unknown=yes property should fail");
109 skip "Skipping rsa command line test", 4 if disabled("deprecated-3.0");
111 ok(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', $good ])),
113 ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
115 ok(run(app([ 'openssl', 'genrsa', '-f4', '-out', 'genrsatest.pem', $good ])),
117 ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
122 my $provconf = srctop_file("test", "fips-and-base.cnf");
123 my $provpath = bldtop_dir("providers");
124 my @prov = ( "-provider-path", $provpath,
125 "-config", $provconf);
127 $ENV{OPENSSL_TEST_LIBCTX} = "1";
128 ok(run(app(['openssl', 'genpkey',
131 '-pkeyopt', 'bits:2080',
132 '-out', 'genrsatest2080.pem'])),
133 "Generating RSA key with > 2048 bits and < 3072 bits");
134 ok(run(app(['openssl', 'genpkey',
137 '-pkeyopt', 'bits:3072',
138 '-out', 'genrsatest3072.pem'])),
139 "Generating RSA key with 3072 bits");