New Russian TLS 1.2 implementation
[openssl.git] / test / recipes / 15-test_genrsa.t
1 #! /usr/bin/env perl
2 # Copyright 2017-2020 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/;
15 use OpenSSL::Test::Utils;
16
17 setup("test_genrsa");
18
19 plan tests => 9;
20
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'])),
26                0, "genrsa -3 8");
27 } else {
28     is(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', '8'])),
29                0, "genrsa -3 8");
30 }
31
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)
40 my $fin;
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:3',
47                          '-pkeyopt', "rsa_keygen_bits:$bits",
48                        ], stderr => undef));
49     } else {
50         $fin = run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem',
51                          $bits
52                        ], stderr => undef));
53     }
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;
64 note "Found lowest allowed amount of bits to be $good";
65
66 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
67              '-pkeyopt', 'rsa_keygen_pubexp:3',
68              '-pkeyopt', "rsa_keygen_bits:$good",
69              '-out', 'genrsatest.pem' ])),
70    "genpkey -3 $good");
71 ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest.pem', '-noout' ])),
72    "pkey -check");
73 ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
74              '-pkeyopt', 'rsa_keygen_pubexp:65537',
75              '-pkeyopt', "rsa_keygen_bits:$good",
76              '-out', 'genrsatest.pem' ])),
77    "genpkey -f4 $good");
78 ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest.pem', '-noout' ])),
79    "pkey -check");
80
81  SKIP: {
82     skip "Skipping rsa command line test", 4 if disabled("deprecated-3.0");
83
84     ok(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', $good ])),
85        "genrsa -3 $good");
86     ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
87        "rsa -check");
88     ok(run(app([ 'openssl', 'genrsa', '-f4', '-out', 'genrsatest.pem', $good ])),
89        "genrsa -f4 $good");
90     ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
91        "rsa -check");
92 }