30-test_evp.t 7.0 KB

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