15-test_genrsa.t 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. use strict;
  9. use warnings;
  10. use File::Spec;
  11. use OpenSSL::Test qw/:DEFAULT srctop_file srctop_dir bldtop_dir bldtop_file/;
  12. use OpenSSL::Test::Utils;
  13. BEGIN {
  14. setup("test_genrsa");
  15. }
  16. use lib srctop_dir('Configurations');
  17. use lib bldtop_dir('.');
  18. my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
  19. plan tests =>
  20. ($no_fips ? 0 : 5) # Extra FIPS related tests
  21. + 15;
  22. # We want to know that an absurdly small number of bits isn't support
  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, "genpkey 8");
  27. is(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', '8'])),
  28. 0, "genrsa -3 8");
  29. # Depending on the shared library, we might have different lower limits.
  30. # Let's find it! This is a simple binary search
  31. # ------------------------------------------------------------
  32. # NOTE: $good may need an update in the future
  33. # ------------------------------------------------------------
  34. note "Looking for lowest amount of bits";
  35. my $bad = 3; # Log2 of number of bits (2 << 3 == 8)
  36. my $good = 11; # Log2 of number of bits (2 << 11 == 2048)
  37. my $fin;
  38. while ($good > $bad + 1) {
  39. my $checked = int(($good + $bad + 1) / 2);
  40. my $bits = 2 ** $checked;
  41. $fin = run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem',
  42. '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_pubexp:65537',
  43. '-pkeyopt', "rsa_keygen_bits:$bits",
  44. ], stderr => undef));
  45. if ($fin) {
  46. note 2 ** $checked, " bits is good";
  47. $good = $checked;
  48. } else {
  49. note 2 ** $checked, " bits is bad";
  50. $bad = $checked;
  51. }
  52. }
  53. $good++ if $good == $bad;
  54. $good = 2 ** $good;
  55. note "Found lowest allowed amount of bits to be $good";
  56. ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
  57. '-pkeyopt', 'rsa_keygen_pubexp:65537',
  58. '-pkeyopt', "rsa_keygen_bits:$good",
  59. '-out', 'genrsatest.pem' ])),
  60. "genpkey $good");
  61. ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest.pem', '-noout' ])),
  62. "pkey -check");
  63. ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
  64. '-pkeyopt', 'rsa_keygen_bits:2048',
  65. '-out', 'genrsatest2048.pem' ])),
  66. "genpkey 2048 bits");
  67. ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest2048.pem', '-noout' ])),
  68. "pkey -check");
  69. ok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
  70. '-pkeyopt', 'hexe:02',
  71. '-out', 'genrsatest.pem' ])),
  72. "genpkey with a bad public exponent should fail");
  73. ok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
  74. '-pkeyopt', 'e:65538',
  75. '-out', 'genrsatest.pem' ])),
  76. "genpkey with a even public exponent should fail");
  77. ok(!run(app([ 'openssl', 'genpkey', '-propquery', 'unknown',
  78. '-algorithm', 'RSA' ])),
  79. "genpkey requesting unknown=yes property should fail");
  80. SKIP: {
  81. skip "Skipping rsa command line test", 2 if disabled("deprecated-3.0");
  82. ok(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', $good ])),
  83. "genrsa -3 $good");
  84. ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
  85. "rsa -check");
  86. }
  87. ok(run(app([ 'openssl', 'genrsa', '-f4', '-out', 'genrsatest.pem', $good ])),
  88. "genrsa -f4 $good");
  89. ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
  90. "rsa -check");
  91. ok(run(app([ 'openssl', 'rsa', '-in', 'genrsatest.pem', '-out', 'genrsatest-enc.pem',
  92. '-aes256', '-passout', 'pass:x' ])),
  93. "rsa encrypt");
  94. ok(run(app([ 'openssl', 'rsa', '-in', 'genrsatest-enc.pem', '-passin', 'pass:x' ])),
  95. "rsa decrypt");
  96. unless ($no_fips) {
  97. my $provconf = srctop_file("test", "fips-and-base.cnf");
  98. my $provpath = bldtop_dir("providers");
  99. my @prov = ( "-provider-path", $provpath,
  100. "-config", $provconf);
  101. $ENV{OPENSSL_TEST_LIBCTX} = "1";
  102. ok(run(app(['openssl', 'genpkey',
  103. @prov,
  104. '-algorithm', 'RSA',
  105. '-pkeyopt', 'bits:2080',
  106. '-out', 'genrsatest2080.pem'])),
  107. "Generating RSA key with > 2048 bits and < 3072 bits");
  108. ok(run(app(['openssl', 'genpkey',
  109. @prov,
  110. '-algorithm', 'RSA',
  111. '-pkeyopt', 'bits:3072',
  112. '-out', 'genrsatest3072.pem'])),
  113. "Generating RSA key with 3072 bits");
  114. ok(!run(app(['openssl', 'genrsa', @prov, '512'])),
  115. "Generating RSA key with 512 bits should fail in FIPS provider");
  116. ok(!run(app(['openssl', 'genrsa',
  117. @prov,
  118. '-provider', 'default',
  119. '-propquery', '?fips!=yes',
  120. '512'])),
  121. "Generating RSA key with 512 bits should succeed with FIPS provider as".
  122. " default with a non-FIPS property query");
  123. # We want to know that an absurdly large number of bits fails the RNG check
  124. is(run(app([ 'openssl', 'genpkey',
  125. @prov,
  126. '-algorithm', 'RSA',
  127. '-pkeyopt', 'bits:1000000000',
  128. '-out', 'genrsatest.pem'])),
  129. 0, "genpkey 1000000000");
  130. }