2 # Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
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
14 use OpenSSL::Test qw/:DEFAULT data_file with/;
15 use OpenSSL::Test::Utils;
20 my @checkopt = ('-check');
22 @checkopt = ('-pubcheck', '-pubin') if $pubcheck;
24 return run(app(['openssl', 'pkey', @checkopt, '-text',
30 my $should_fail = shift;
35 $str = "$f should fail validation" if $should_fail;
36 $str = "$f should pass validation" unless $should_fail;
41 with({ exit_checker => sub { return shift == $should_fail; } },
43 ok(pkey_check($f, $pubcheck), $str);
46 fail("Missing file $f");
50 setup("test_pkey_check");
52 my @negative_tests = ();
54 push(@negative_tests, (
55 # For EC keys the range for the secret scalar `k` is `1 <= k <= n-1`
56 "ec_p256_bad_0.pem", # `k` set to `n` (equivalent to `0 mod n`, invalid)
57 "ec_p256_bad_1.pem", # `k` set to `n+1` (equivalent to `1 mod n`, invalid)
58 )) unless disabled("ec");
60 push(@negative_tests, (
61 # For SM2 keys the range for the secret scalar `k` is `1 <= k < n-1`
62 "sm2_bad_neg1.pem", # `k` set to `n-1` (invalid, because SM2 range)
63 "sm2_bad_0.pem", # `k` set to `n` (equivalent to `0 mod n`, invalid)
64 "sm2_bad_1.pem", # `k` set to `n+1` (equivalent to `1 mod n`, invalid)
65 )) unless disabled("sm2");
67 my @positive_tests = ();
69 push(@positive_tests, (
71 )) unless disabled("dh");
73 my @negative_pubtests = ();
75 push(@negative_pubtests, (
77 )) unless disabled("dsa");
79 my @positive_pubtests = ();
81 push(@positive_pubtests, (
83 )) unless disabled("dsa");
85 plan skip_all => "No tests within the current enabled feature set"
86 unless @negative_tests && @positive_tests
87 && @negative_pubtests && @positive_pubtests;
89 plan tests => scalar(@negative_tests) + scalar(@positive_tests)
90 + scalar(@negative_pubtests) + scalar(@positive_pubtests);
92 foreach my $t (@negative_tests) {
96 foreach my $t (@positive_tests) {
100 foreach my $t (@negative_pubtests) {
104 foreach my $t (@positive_pubtests) {