30-test_evp.t 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #! /usr/bin/env perl
  2. # Copyright 2015-2021 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 data_file bldtop_dir srctop_file srctop_dir bldtop_file);
  11. use OpenSSL::Test::Utils;
  12. BEGIN {
  13. setup("test_evp");
  14. }
  15. use lib srctop_dir('Configurations');
  16. use lib bldtop_dir('.');
  17. my $no_fips = disabled('fips') || ($ENV{NO_FIPS} // 0);
  18. my $no_legacy = disabled('legacy') || ($ENV{NO_LEGACY} // 0);
  19. my $no_dh = disabled("dh");
  20. my $no_dsa = disabled("dsa");
  21. my $no_ec = disabled("ec");
  22. my $no_gost = disabled("gost");
  23. my $no_sm2 = disabled("sm2");
  24. # Default config depends on if the legacy module is built or not
  25. my $defaultcnf = $no_legacy ? 'default.cnf' : 'default-and-legacy.cnf';
  26. my @configs = ( $defaultcnf );
  27. # Only add the FIPS config if the FIPS module has been built
  28. push @configs, 'fips-and-base.cnf' unless $no_fips;
  29. # A list of tests that run with both the default and fips provider.
  30. my @files = qw(
  31. evpciph_aes_ccm_cavs.txt
  32. evpciph_aes_common.txt
  33. evpciph_aes_cts.txt
  34. evpciph_aes_wrap.txt
  35. evpciph_des3_common.txt
  36. evpkdf_hkdf.txt
  37. evpkdf_pbkdf2.txt
  38. evpkdf_ss.txt
  39. evpkdf_ssh.txt
  40. evpkdf_tls12_prf.txt
  41. evpkdf_x942.txt
  42. evpkdf_x963.txt
  43. evpmac_common.txt
  44. evpmd_sha.txt
  45. evppbe_pbkdf2.txt
  46. evppkey_kdf_hkdf.txt
  47. evppkey_rsa_common.txt
  48. evprand.txt
  49. );
  50. push @files, qw(evppkey_ffdhe.txt) unless $no_dh;
  51. push @files, qw(evppkey_dsa.txt) unless $no_dsa;
  52. push @files, qw(evppkey_ecx.txt) unless $no_ec;
  53. push @files, qw(
  54. evppkey_ecc.txt
  55. evppkey_ecdh.txt
  56. evppkey_ecdsa.txt
  57. evppkey_kas.txt
  58. evppkey_mismatch.txt
  59. ) unless $no_ec || $no_gost;
  60. # A list of tests that only run with the default provider
  61. # (i.e. The algorithms are not present in the fips provider)
  62. my @defltfiles = qw(
  63. evpciph_aes_ocb.txt
  64. evpciph_aes_siv.txt
  65. evpciph_aria.txt
  66. evpciph_bf.txt
  67. evpciph_camellia.txt
  68. evpciph_cast5.txt
  69. evpciph_chacha.txt
  70. evpciph_des.txt
  71. evpciph_idea.txt
  72. evpciph_rc2.txt
  73. evpciph_rc4.txt
  74. evpciph_rc5.txt
  75. evpciph_seed.txt
  76. evpciph_sm4.txt
  77. evpencod.txt
  78. evpkdf_krb5.txt
  79. evpkdf_scrypt.txt
  80. evpkdf_tls11_prf.txt
  81. evpmac_blake.txt
  82. evpmac_poly1305.txt
  83. evpmac_siphash.txt
  84. evpmd_blake.txt
  85. evpmd_md.txt
  86. evpmd_mdc2.txt
  87. evpmd_ripemd.txt
  88. evpmd_sm3.txt
  89. evpmd_whirlpool.txt
  90. evppbe_scrypt.txt
  91. evppbe_pkcs12.txt
  92. evppkey_kdf_scrypt.txt
  93. evppkey_kdf_tls1_prf.txt
  94. evppkey_rsa.txt
  95. );
  96. push @defltfiles, qw(evppkey_brainpool.txt) unless $no_ec;
  97. push @defltfiles, qw(evppkey_sm2.txt) unless $no_sm2;
  98. plan tests =>
  99. + (scalar(@configs) * scalar(@files))
  100. + scalar(@defltfiles)
  101. + 3; # error output tests
  102. foreach (@configs) {
  103. my $conf = srctop_file("test", $_);
  104. foreach my $f ( @files ) {
  105. ok(run(test(["evp_test",
  106. "-config", $conf,
  107. data_file("$f")])),
  108. "running evp_test -config $conf $f");
  109. }
  110. }
  111. my $conf = srctop_file("test", $defaultcnf);
  112. foreach my $f ( @defltfiles ) {
  113. ok(run(test(["evp_test",
  114. "-config", $conf,
  115. data_file("$f")])),
  116. "running evp_test -config $conf $f");
  117. }
  118. # test_errors OPTIONS
  119. #
  120. # OPTIONS may include:
  121. #
  122. # key => "filename" # expected to be found in $SRCDIR/test/certs
  123. # out => "filename" # file to write error strings to
  124. # args => [ ... extra openssl pkey args ... ]
  125. # expected => regexps to match error lines against
  126. sub test_errors { # actually tests diagnostics of OSSL_STORE
  127. my %opts = @_;
  128. my $infile = srctop_file('test', 'certs', $opts{key});
  129. my @args = ( qw(openssl pkey -in), $infile, @{$opts{args} // []} );
  130. my $res = !run(app([@args], stderr => $opts{out}));
  131. my $found = !exists $opts{expected};
  132. open(my $in, '<', $opts{out}) or die "Could not open file $opts{out}";
  133. while(my $errline = <$in>) {
  134. print $errline; # this may help debugging
  135. # output must not include ASN.1 parse errors
  136. $res &&= $errline !~ m/asn1 encoding/;
  137. # output must include what is expressed in $opts{$expected}
  138. $found = 1
  139. if exists $opts{expected} && $errline =~ m/$opts{expected}/;
  140. }
  141. close $in;
  142. # $tmpfile is kept to help with investigation in case of failure
  143. return $res && $found;
  144. }
  145. SKIP: {
  146. skip "DSA not disabled", 2 if !disabled("dsa");
  147. ok(test_errors(key => 'server-dsa-key.pem',
  148. out => 'server-dsa-key.err'),
  149. "expected error loading unsupported dsa private key");
  150. ok(test_errors(key => 'server-dsa-pubkey.pem',
  151. out => 'server-dsa-pubkey.err',
  152. args => [ '-pubin' ],
  153. expected => 'unsupported algorithm'),
  154. "expected error loading unsupported dsa public key");
  155. }
  156. SKIP: {
  157. skip "SM2 not disabled", 1 if !disabled("sm2");
  158. ok(test_errors(key => 'sm2.key', out => 'sm2.err'),
  159. "expected error loading unsupported sm2 private key");
  160. }