04-test_encoder_decoder.t 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #! /usr/bin/env perl
  2. # Copyright 2020-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 OpenSSL::Test qw/:DEFAULT srctop_dir srctop_file bldtop_dir bldtop_file/;
  11. use OpenSSL::Test::Utils;
  12. BEGIN {
  13. setup("test_encoder_decoder");
  14. }
  15. use lib srctop_dir('Configurations');
  16. use lib bldtop_dir('.');
  17. use platform;
  18. my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
  19. my $rsa_key = srctop_file("test", "certs", "ee-key.pem");
  20. my $pss_key = srctop_file("test", "certs", "ca-pss-key.pem");
  21. plan tests => ($no_fips ? 0 : 5) + 2; # FIPS install test + test
  22. my $conf = srctop_file("test", "default.cnf");
  23. # Check if the specified pattern occurs in the given file
  24. # Returns 1 if the pattern is found and 0 if not
  25. sub find_line_file {
  26. my ($key, $file) = @_;
  27. open(my $in, $file) or return -1;
  28. while (my $line = <$in>) {
  29. if ($line =~ /$key/) {
  30. close($in);
  31. return 1;
  32. }
  33. }
  34. close($in);
  35. return 0;
  36. }
  37. ok(run(test(["endecode_test", "-rsa", $rsa_key,
  38. "-pss", $pss_key,
  39. "-config", $conf,
  40. "-provider", "default"])));
  41. # Run with non-default library context
  42. ok(run(test(["endecode_test", "-rsa", $rsa_key,
  43. "-pss", $pss_key,
  44. "-context",
  45. "-config", $conf,
  46. "-provider", "default"])));
  47. unless ($no_fips) {
  48. # Run with fips library context
  49. my $conf = srctop_file("test", "fips-and-base.cnf");
  50. ok(run(test(["endecode_test", "-rsa", $rsa_key,
  51. "-pss", $pss_key,
  52. "-config", $conf,
  53. "-provider", "fips"])));
  54. SKIP: {
  55. skip "EC disabled", 2 if disabled("ec");
  56. ok(run(app([ 'openssl', 'genpkey', '-algorithm', 'EC',
  57. '-pkeyopt', 'group:P-256', '-text',
  58. '-config', $conf, '-provider', 'fips', '-out', 'ec.txt' ])),
  59. 'Print a FIPS provider EC private key');
  60. ok(find_line_file('NIST CURVE: P-256', 'ec.txt') == 1,
  61. 'Printing an FIPS provider EC private key');
  62. }
  63. my $no_des = disabled("des");
  64. SKIP: {
  65. skip "MD5 disabled", 2 if disabled("md5");
  66. ok(run(app([ 'openssl', 'genrsa', '-aes128', '-out', 'epki.pem',
  67. '-traditional', '-passout', 'pass:pass' ])),
  68. "rsa encrypted using a non fips algorithm MD5 in pbe");
  69. my $conf2 = srctop_file("test", "default-and-fips.cnf");
  70. ok(run(test(['decoder_propq_test', '-config', $conf2,
  71. '-provider', 'fips', 'epki.pem'])));
  72. }
  73. }