xref: /freebsd/crypto/openssl/test/recipes/15-test_genrsa.t (revision a90b9d0159070121c221b966469c3e36d912bf82)
1#! /usr/bin/env perl
2# Copyright 2017-2022 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    + 15;
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");
109ok(run(app([ 'openssl', 'rsa', '-in', 'genrsatest-enc.pem', '-passin', 'pass:x' ])),
110   "rsa decrypt");
111
112unless ($no_fips) {
113    my $provconf = srctop_file("test", "fips-and-base.cnf");
114    my $provpath = bldtop_dir("providers");
115    my @prov = ( "-provider-path", $provpath,
116                 "-config", $provconf);
117
118    $ENV{OPENSSL_TEST_LIBCTX} = "1";
119    ok(run(app(['openssl', 'genpkey',
120                @prov,
121                '-algorithm', 'RSA',
122                '-pkeyopt', 'bits:2080',
123                '-out', 'genrsatest2080.pem'])),
124       "Generating RSA key with > 2048 bits and < 3072 bits");
125    ok(run(app(['openssl', 'genpkey',
126                @prov,
127                '-algorithm', 'RSA',
128                '-pkeyopt', 'bits:3072',
129                '-out', 'genrsatest3072.pem'])),
130       "Generating RSA key with 3072 bits");
131
132   ok(!run(app(['openssl', 'genrsa', @prov, '512'])),
133       "Generating RSA key with 512 bits should fail in FIPS provider");
134
135   ok(!run(app(['openssl', 'genrsa',
136                @prov,
137                '-provider', 'default',
138                '-propquery', '?fips!=yes',
139                '512'])),
140       "Generating RSA key with 512 bits should succeed with FIPS provider as".
141       " default with a non-FIPS property query");
142
143    # We want to know that an absurdly large number of bits fails the RNG check
144    is(run(app([ 'openssl', 'genpkey',
145                 @prov,
146                 '-algorithm', 'RSA',
147                 '-pkeyopt', 'bits:1000000000',
148                 '-out', 'genrsatest.pem'])),
149               0, "genpkey 1000000000");
150}
151