91-test_pkey_check.t 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #! /usr/bin/env perl
  2. # Copyright 2017-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 File::Spec;
  11. use OpenSSL::Test qw/:DEFAULT data_file/;
  12. use OpenSSL::Test::Utils;
  13. sub pkey_check {
  14. my $f = shift;
  15. return run(app(['openssl', 'pkey', '-check', '-text',
  16. '-in', $f]));
  17. }
  18. sub check_key {
  19. my $f = shift;
  20. my $should_fail = shift;
  21. my $str;
  22. $str = "$f should fail validation" if $should_fail;
  23. $str = "$f should pass validation" unless $should_fail;
  24. $f = data_file($f);
  25. if ( -s $f ) {
  26. if ($should_fail) {
  27. ok(!pkey_check($f), $str);
  28. } else {
  29. ok(pkey_check($f), $str);
  30. }
  31. } else {
  32. fail("Missing file $f");
  33. }
  34. }
  35. setup("test_pkey_check");
  36. my @negative_tests = ();
  37. push(@negative_tests, (
  38. # For EC keys the range for the secret scalar `k` is `1 <= k <= n-1`
  39. "ec_p256_bad_0.pem", # `k` set to `n` (equivalent to `0 mod n`, invalid)
  40. "ec_p256_bad_1.pem", # `k` set to `n+1` (equivalent to `1 mod n`, invalid)
  41. )) unless disabled("ec");
  42. push(@negative_tests, (
  43. # For SM2 keys the range for the secret scalar `k` is `1 <= k < n-1`
  44. "sm2_bad_neg1.pem", # `k` set to `n-1` (invalid, because SM2 range)
  45. "sm2_bad_0.pem", # `k` set to `n` (equivalent to `0 mod n`, invalid)
  46. "sm2_bad_1.pem", # `k` set to `n+1` (equivalent to `1 mod n`, invalid)
  47. )) unless disabled("sm2");
  48. my @positive_tests = ();
  49. push(@positive_tests, (
  50. "dhpkey.pem"
  51. )) unless disabled("dh");
  52. plan skip_all => "No tests within the current enabled feature set"
  53. unless @negative_tests && @positive_tests;
  54. plan tests => scalar(@negative_tests) + scalar(@positive_tests);
  55. foreach my $t (@negative_tests) {
  56. check_key($t, 1);
  57. }
  58. foreach my $t (@positive_tests) {
  59. check_key($t, 0);
  60. }