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