501d3a100f6e4832e34be2bc0e7a4f2b5a5eaf2e
[openssl.git] / test / recipes / 15-test_genrsa.t
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
10 use strict;
11 use warnings;
12
13 use File::Spec;
14 use OpenSSL::Test qw/:DEFAULT srctop_file srctop_dir bldtop_dir bldtop_file/;
15 use OpenSSL::Test::Utils;
16
17 BEGIN {
18     setup("test_genrsa");
19 }
20
21 use lib srctop_dir('Configurations');
22 use lib bldtop_dir('.');
23
24 my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
25
26 plan tests =>
27     ($no_fips ? 0 : 2)          # Extra FIPS related test
28     + 13;
29
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'])),
35                0, "genrsa -3 8");
36 } else {
37     is(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', '8'])),
38                0, "genrsa -3 8");
39 }
40
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)
49 my $fin;
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",
57                        ], stderr => undef));
58     } else {
59         $fin = run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem',
60                          $bits
61                        ], stderr => undef));
62     }
63     if ($fin) {
64         note 2 ** $checked, " bits is good";
65         $good = $checked;
66     } else {
67         note 2 ** $checked, " bits is bad";
68         $bad = $checked;
69     }
70 }
71 $good++ if $good == $bad;
72 $good = 2 ** $good;
73 note "Found lowest allowed amount of bits to be $good";
74
75 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
76              '-pkeyopt', 'rsa_keygen_pubexp:65537',
77              '-pkeyopt', "rsa_keygen_bits:$good",
78              '-out', 'genrsatest.pem' ])),
79    "genpkey -3 $good");
80 ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest.pem', '-noout' ])),
81    "pkey -check");
82 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
83              '-pkeyopt', 'rsa_keygen_pubexp:65537',
84              '-pkeyopt', "rsa_keygen_bits:$good",
85              '-out', 'genrsatest.pem' ])),
86    "genpkey -f4 $good");
87
88 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
89              '-pkeyopt', 'rsa_keygen_bits:2048',
90              '-out', 'genrsatest2048.pem' ])),
91    "genpkey 2048 bits");
92 ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest2048.pem', '-noout' ])),
93    "pkey -check");
94
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");
106
107
108  SKIP: {
109     skip "Skipping rsa command line test", 4 if disabled("deprecated-3.0");
110
111     ok(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', $good ])),
112        "genrsa -3 $good");
113     ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
114        "rsa -check");
115     ok(run(app([ 'openssl', 'genrsa', '-f4', '-out', 'genrsatest.pem', $good ])),
116        "genrsa -f4 $good");
117     ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
118        "rsa -check");
119 }
120
121 unless ($no_fips) {
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);
126
127     $ENV{OPENSSL_TEST_LIBCTX} = "1";
128     ok(run(app(['openssl', 'genpkey',
129                 @prov,
130                '-algorithm', 'RSA',
131                '-pkeyopt', 'bits:2080',
132                '-out', 'genrsatest2080.pem'])),
133        "Generating RSA key with > 2048 bits and < 3072 bits");
134     ok(run(app(['openssl', 'genpkey',
135                 @prov,
136                '-algorithm', 'RSA',
137                '-pkeyopt', 'bits:3072',
138                '-out', 'genrsatest3072.pem'])),
139        "Generating RSA key with 3072 bits");
140 }