15-test_rsapss.t 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 with srctop_file data_file/;
  12. use OpenSSL::Test::Utils;
  13. setup("test_rsapss");
  14. plan tests => 18;
  15. #using test/testrsa.pem which happens to be a 512 bit RSA
  16. ok(run(app(['openssl', 'dgst', '-sign', srctop_file('test', 'testrsa.pem'), '-sha1',
  17. '-sigopt', 'rsa_padding_mode:pss',
  18. '-sigopt', 'rsa_pss_saltlen:max',
  19. '-sigopt', 'rsa_mgf1_md:sha512',
  20. '-out', 'testrsapss-restricted.sig',
  21. srctop_file('test', 'testrsa.pem')])),
  22. "openssl dgst -sign [plain RSA key, PSS padding mode, PSS restrictions]");
  23. ok(run(app(['openssl', 'dgst', '-sign', srctop_file('test', 'testrsa.pem'), '-sha1',
  24. '-sigopt', 'rsa_padding_mode:pss',
  25. '-out', 'testrsapss-unrestricted.sig',
  26. srctop_file('test', 'testrsa.pem')])),
  27. "openssl dgst -sign [plain RSA key, PSS padding mode, no PSS restrictions]");
  28. ok(!run(app(['openssl', 'dgst', '-sign', srctop_file('test', 'testrsa.pem'), '-sha512',
  29. '-sigopt', 'rsa_padding_mode:pss', '-sigopt', 'rsa_pss_saltlen:max',
  30. '-sigopt', 'rsa_mgf1_md:sha512', srctop_file('test', 'testrsa.pem')])),
  31. "openssl dgst -sign, expect to fail gracefully");
  32. ok(!run(app(['openssl', 'dgst', '-sign', srctop_file('test', 'testrsa.pem'), '-sha512',
  33. '-sigopt', 'rsa_padding_mode:pss', '-sigopt', 'rsa_pss_saltlen:2147483647',
  34. '-sigopt', 'rsa_mgf1_md:sha1', srctop_file('test', 'testrsa.pem')])),
  35. "openssl dgst -sign, expect to fail gracefully");
  36. ok(!run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'), '-sha512',
  37. '-sigopt', 'rsa_padding_mode:pss', '-sigopt', 'rsa_pss_saltlen:max',
  38. '-sigopt', 'rsa_mgf1_md:sha512', '-signature', 'testrsapss.sig',
  39. srctop_file('test', 'testrsa.pem')])),
  40. "openssl dgst -prverify, expect to fail gracefully");
  41. ok(run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'),
  42. '-sha1',
  43. '-sigopt', 'rsa_padding_mode:pss',
  44. '-sigopt', 'rsa_pss_saltlen:max',
  45. '-sigopt', 'rsa_mgf1_md:sha512',
  46. '-signature', 'testrsapss-restricted.sig',
  47. srctop_file('test', 'testrsa.pem')])),
  48. "openssl dgst -prverify [plain RSA key, PSS padding mode, PSS restrictions]");
  49. ok(run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'),
  50. '-sha1',
  51. '-sigopt', 'rsa_padding_mode:pss',
  52. '-sigopt', 'rsa_pss_saltlen:42',
  53. '-sigopt', 'rsa_mgf1_md:sha512',
  54. '-signature', 'testrsapss-restricted.sig',
  55. srctop_file('test', 'testrsa.pem')])),
  56. "openssl dgst -sign rsa512bit.pem -sha1 -sigopt rsa_pss_saltlen:max produces 42 bits of PSS salt");
  57. ok(run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'),
  58. '-sha1',
  59. '-sigopt', 'rsa_padding_mode:pss',
  60. '-sigopt', 'rsa_pss_saltlen:auto-digestmax',
  61. '-sigopt', 'rsa_mgf1_md:sha512',
  62. '-signature', 'testrsapss-restricted.sig',
  63. srctop_file('test', 'testrsa.pem')])),
  64. "openssl dgst -prverify rsa512bit.pem -sha1 -sigopt rsa_pss_saltlen:auto-digestmax verifies signatures with saltlen > digestlen");
  65. ok(run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'),
  66. '-sha1',
  67. '-sigopt', 'rsa_padding_mode:pss',
  68. '-signature', 'testrsapss-unrestricted.sig',
  69. srctop_file('test', 'testrsa.pem')])),
  70. "openssl dgst -prverify [plain RSA key, PSS padding mode, no PSS restrictions]");
  71. ok(run(app(['openssl', 'dgst', '-sign', srctop_file('test', 'testrsa.pem'), '-sha1',
  72. '-sigopt', 'rsa_padding_mode:pss',
  73. '-sigopt', 'rsa_pss_saltlen:auto-digestmax',
  74. '-out', 'testrsapss-sha1-autodigestmax.sig',
  75. srctop_file('test', 'testrsa.pem')])),
  76. "openssl dgst -sign -sha1 -rsa_pss_saltlen:auto-digestmax");
  77. ok(run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'), '-sha1',
  78. '-sigopt', 'rsa_padding_mode:pss',
  79. '-sigopt', 'rsa_pss_saltlen:20',
  80. '-signature', 'testrsapss-sha1-autodigestmax.sig',
  81. srctop_file('test', 'testrsa.pem')])),
  82. "openssl dgst -sign -sha1 -rsa_padding_mode:auto-digestmax produces 20 (i.e., digestlen) bits of PSS salt");
  83. ok(run(app(['openssl', 'dgst', '-sign', srctop_file('test', 'testrsa.pem'), '-sha256',
  84. '-sigopt', 'rsa_padding_mode:pss',
  85. '-sigopt', 'rsa_pss_saltlen:auto-digestmax',
  86. '-out', 'testrsapss-sha256-autodigestmax.sig',
  87. srctop_file('test', 'testrsa.pem')])),
  88. "openssl dgst -sign -sha256 -rsa_pss_saltlen:auto-digestmax");
  89. ok(run(app(['openssl', 'dgst', '-prverify', srctop_file('test', 'testrsa.pem'), '-sha256',
  90. '-sigopt', 'rsa_padding_mode:pss',
  91. '-sigopt', 'rsa_pss_saltlen:30',
  92. '-signature', 'testrsapss-sha256-autodigestmax.sig',
  93. srctop_file('test', 'testrsa.pem')])),
  94. "openssl dgst -sign rsa512bit.pem -sha256 -rsa_padding_mode:auto-digestmax produces 30 bits of PSS salt (due to 512bit key)");
  95. # Test that RSA-PSS keys are supported by genpkey and rsa commands.
  96. {
  97. my $rsapss = "rsapss.key";
  98. ok(run(app(['openssl', 'genpkey', '-algorithm', 'RSA-PSS',
  99. '-pkeyopt', 'rsa_keygen_bits:1024',
  100. '-pkeyopt', 'rsa_keygen_pubexp:65537',
  101. '-pkeyopt', 'rsa_keygen_primes:2',
  102. '--out', $rsapss])));
  103. ok(run(app(['openssl', 'rsa', '-check',
  104. '-in', $rsapss])));
  105. }
  106. ok(!run(app([ 'openssl', 'rsa',
  107. '-in' => data_file('negativesaltlen.pem')],
  108. '-out' => 'badout')));
  109. ok(run(app(['openssl', 'genpkey', '-algorithm', 'RSA-PSS', '-pkeyopt', 'rsa_keygen_bits:1024',
  110. '-pkeyopt', 'rsa_pss_keygen_md:SHA256', '-pkeyopt', 'rsa_pss_keygen_saltlen:10',
  111. '-out', 'testrsapss.pem'])),
  112. "openssl genpkey RSA-PSS with pss parameters");
  113. ok(run(app(['openssl', 'pkey', '-in', 'testrsapss.pem', '-pubout', '-text'])),
  114. "openssl pkey, execute rsa_pub_encode with pss parameters");
  115. unlink 'testrsapss.pem';