15-test_genrsa.t 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #! /usr/bin/env perl
  2. # Copyright 2017-2023 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. + 16;
  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. # Check the default salt length for PBKDF2 is 16 bytes
  95. # We expect the output to be of the form "0:d=0 hl=2 l= 16 prim: OCTET STRING [HEX DUMP]:FAC7F37508E6B7A805BF4B13861B3687"
  96. # i.e. 2 byte header + 16 byte salt.
  97. ok(run(app(([ 'openssl', 'asn1parse',
  98. '-in', 'genrsatest-enc.pem',
  99. '-offset', '34', '-length', '18']))),
  100. "Check the default size of the PBKDF2 PARAM 'salt length' is 16");
  101. ok(run(app([ 'openssl', 'rsa', '-in', 'genrsatest-enc.pem', '-passin', 'pass:x' ])),
  102. "rsa decrypt");
  103. unless ($no_fips) {
  104. my $provconf = srctop_file("test", "fips-and-base.cnf");
  105. my $provpath = bldtop_dir("providers");
  106. my @prov = ( "-provider-path", $provpath,
  107. "-config", $provconf);
  108. $ENV{OPENSSL_TEST_LIBCTX} = "1";
  109. ok(run(app(['openssl', 'genpkey',
  110. @prov,
  111. '-algorithm', 'RSA',
  112. '-pkeyopt', 'bits:2080',
  113. '-out', 'genrsatest2080.pem'])),
  114. "Generating RSA key with > 2048 bits and < 3072 bits");
  115. ok(run(app(['openssl', 'genpkey',
  116. @prov,
  117. '-algorithm', 'RSA',
  118. '-pkeyopt', 'bits:3072',
  119. '-out', 'genrsatest3072.pem'])),
  120. "Generating RSA key with 3072 bits");
  121. ok(!run(app(['openssl', 'genrsa', @prov, '512'])),
  122. "Generating RSA key with 512 bits should fail in FIPS provider");
  123. ok(!run(app(['openssl', 'genrsa',
  124. @prov,
  125. '-provider', 'default',
  126. '-propquery', '?fips!=yes',
  127. '512'])),
  128. "Generating RSA key with 512 bits should succeed with FIPS provider as".
  129. " default with a non-FIPS property query");
  130. # We want to know that an absurdly large number of bits fails the RNG check
  131. is(run(app([ 'openssl', 'genpkey',
  132. @prov,
  133. '-algorithm', 'RSA',
  134. '-pkeyopt', 'bits:1000000000',
  135. '-out', 'genrsatest.pem'])),
  136. 0, "genpkey 1000000000");
  137. }