15-test_genrsa.t 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. use strict;
  9. use warnings;
  10. use File::Spec;
  11. use OpenSSL::Test qw/:DEFAULT srctop_file/;
  12. use OpenSSL::Test::Utils;
  13. setup("test_genrsa");
  14. plan tests => 12;
  15. # We want to know that an absurdly small number of bits isn't support
  16. if (disabled("deprecated-3.0")) {
  17. is(run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem',
  18. '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_bits:8',
  19. '-pkeyopt', 'rsa_keygen_pubexp:3'])),
  20. 0, "genrsa -3 8");
  21. } else {
  22. is(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', '8'])),
  23. 0, "genrsa -3 8");
  24. }
  25. # Depending on the shared library, we might have different lower limits.
  26. # Let's find it! This is a simple binary search
  27. # ------------------------------------------------------------
  28. # NOTE: $good may need an update in the future
  29. # ------------------------------------------------------------
  30. note "Looking for lowest amount of bits";
  31. my $bad = 3; # Log2 of number of bits (2 << 3 == 8)
  32. my $good = 11; # Log2 of number of bits (2 << 11 == 2048)
  33. my $fin;
  34. while ($good > $bad + 1) {
  35. my $checked = int(($good + $bad + 1) / 2);
  36. my $bits = 2 ** $checked;
  37. if (disabled("deprecated-3.0")) {
  38. $fin = run(app([ 'openssl', 'genpkey', '-out', 'genrsatest.pem',
  39. '-algorithm', 'RSA', '-pkeyopt', 'rsa_keygen_pubexp:65537',
  40. '-pkeyopt', "rsa_keygen_bits:$bits",
  41. ], stderr => undef));
  42. } else {
  43. $fin = run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem',
  44. $bits
  45. ], stderr => undef));
  46. }
  47. if ($fin) {
  48. note 2 ** $checked, " bits is good";
  49. $good = $checked;
  50. } else {
  51. note 2 ** $checked, " bits is bad";
  52. $bad = $checked;
  53. }
  54. }
  55. $good++ if $good == $bad;
  56. $good = 2 ** $good;
  57. note "Found lowest allowed amount of bits to be $good";
  58. ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
  59. '-pkeyopt', 'rsa_keygen_pubexp:65537',
  60. '-pkeyopt', "rsa_keygen_bits:$good",
  61. '-out', 'genrsatest.pem' ])),
  62. "genpkey -3 $good");
  63. ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest.pem', '-noout' ])),
  64. "pkey -check");
  65. ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
  66. '-pkeyopt', 'rsa_keygen_pubexp:65537',
  67. '-pkeyopt', "rsa_keygen_bits:$good",
  68. '-out', 'genrsatest.pem' ])),
  69. "genpkey -f4 $good");
  70. ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
  71. '-pkeyopt', 'rsa_keygen_bits:2048',
  72. '-out', 'genrsatest2048.pem' ])),
  73. "genpkey 2048 bits");
  74. ok(run(app([ 'openssl', 'pkey', '-check', '-in', 'genrsatest2048.pem', '-noout' ])),
  75. "pkey -check");
  76. ok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
  77. '-pkeyopt', 'hexe:02',
  78. '-out', 'genrsatest.pem' ])),
  79. "genpkey with a bad public exponent should fail");
  80. ok(!run(app([ 'openssl', 'genpkey', '-algorithm', 'RSA',
  81. '-pkeyopt', 'e:65538',
  82. '-out', 'genrsatest.pem' ])),
  83. "genpkey with a even public exponent should fail");
  84. SKIP: {
  85. skip "Skipping rsa command line test", 4 if disabled("deprecated-3.0");
  86. ok(run(app([ 'openssl', 'genrsa', '-3', '-out', 'genrsatest.pem', $good ])),
  87. "genrsa -3 $good");
  88. ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
  89. "rsa -check");
  90. ok(run(app([ 'openssl', 'genrsa', '-f4', '-out', 'genrsatest.pem', $good ])),
  91. "genrsa -f4 $good");
  92. ok(run(app([ 'openssl', 'rsa', '-check', '-in', 'genrsatest.pem', '-noout' ])),
  93. "rsa -check");
  94. }