2 # Copyright 2017-2020 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/;
15 use OpenSSL::Test::Utils;
21 # We want to know that an absurdly small number of bits isn't support
22 if (disabled("deprecated-3.0")) {
23 is(run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem',
24 '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_bits:8',
25 '-pkeyopt', 'rsa_keygen_pubexp:3'])),
28 is(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', '8'])),
32 # Depending on the shared library, we might have different lower limits.
33 # Let's find it! This is a simple binary search
34 # ------------------------------------------------------------
35 # NOTE: $good may need an update in the future
36 # ------------------------------------------------------------
37 note "Looking for lowest amount of bits";
38 my $bad = 3; # Log2 of number of bits (2 << 3 == 8)
39 my $good = 11; # Log2 of number of bits (2 << 11 == 2048)
41 while ($good > $bad + 1) {
42 my $checked = int(($good + $bad + 1) / 2);
43 my $bits = 2 ** $checked;
44 if (disabled("deprecated-3.0")) {
45 $fin = run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem',
46 '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_pubexp:65537',
47 '-pkeyopt', "rsa_keygen_bits:$bits",
50 $fin = run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem',
55 note 2 ** $checked, " bits is good";
58 note 2 ** $checked, " bits is bad";
62 $good++ if $good == $bad;
64 note "Found lowest allowed amount of bits to be $good";
66 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
67 '-pkeyopt', 'rsa_keygen_pubexp:65537',
68 '-pkeyopt', "rsa_keygen_bits:$good",
69 '-out', 'genrsatest.pem' ])),
71 ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest.pem', '-noout' ])),
73 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
74 '-pkeyopt', 'rsa_keygen_pubexp:65537',
75 '-pkeyopt', "rsa_keygen_bits:$good",
76 '-out', 'genrsatest.pem' ])),
79 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
80 '-pkeyopt', 'rsa_keygen_bits:2048',
81 '-out', 'genrsatest2048.pem' ])),
83 ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest2048.pem', '-noout' ])),
86 ok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
87 '-pkeyopt', 'hexe:02',
88 '-out', 'genrsatest.pem' ])),
89 "genpkey with a bad public exponent should fail");
90 ok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
91 '-pkeyopt', 'e:65538',
92 '-out', 'genrsatest.pem' ])),
93 "genpkey with a even public exponent should fail");
97 skip "Skipping rsa command line test", 4 if disabled("deprecated-3.0");
99 ok(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', $good ])),
101 ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
103 ok(run(app([ 'openssl', 'genrsa', '-f4', '-out', 'genrsatest.pem', $good ])),
105 ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),