1*e0c4386eSCy Schubert#! /usr/bin/env perl 2*e0c4386eSCy Schubert# Copyright 2017-2024 The OpenSSL Project Authors. All Rights Reserved. 3*e0c4386eSCy Schubert# 4*e0c4386eSCy Schubert# Licensed under the Apache License 2.0 (the "License"). You may not use 5*e0c4386eSCy Schubert# this file except in compliance with the License. You can obtain a copy 6*e0c4386eSCy Schubert# in the file LICENSE in the source distribution or at 7*e0c4386eSCy Schubert# https://www.openssl.org/source/license.html 8*e0c4386eSCy Schubert 9*e0c4386eSCy Schubert 10*e0c4386eSCy Schubertuse strict; 11*e0c4386eSCy Schubertuse warnings; 12*e0c4386eSCy Schubert 13*e0c4386eSCy Schubertuse File::Spec; 14*e0c4386eSCy Schubertuse OpenSSL::Test qw/:DEFAULT data_file with/; 15*e0c4386eSCy Schubertuse OpenSSL::Test::Utils; 16*e0c4386eSCy Schubert 17*e0c4386eSCy Schubertsub pkey_check { 18*e0c4386eSCy Schubert my $f = shift; 19*e0c4386eSCy Schubert my $pubcheck = shift; 20*e0c4386eSCy Schubert my @checkopt = ('-check'); 21*e0c4386eSCy Schubert 22*e0c4386eSCy Schubert @checkopt = ('-pubcheck', '-pubin') if $pubcheck; 23*e0c4386eSCy Schubert 24*e0c4386eSCy Schubert return run(app(['openssl', 'pkey', @checkopt, '-text', 25*e0c4386eSCy Schubert '-in', $f])); 26*e0c4386eSCy Schubert} 27*e0c4386eSCy Schubert 28*e0c4386eSCy Schubertsub check_key { 29*e0c4386eSCy Schubert my $f = shift; 30*e0c4386eSCy Schubert my $should_fail = shift; 31*e0c4386eSCy Schubert my $pubcheck = shift; 32*e0c4386eSCy Schubert my $str; 33*e0c4386eSCy Schubert 34*e0c4386eSCy Schubert 35*e0c4386eSCy Schubert $str = "$f should fail validation" if $should_fail; 36*e0c4386eSCy Schubert $str = "$f should pass validation" unless $should_fail; 37*e0c4386eSCy Schubert 38*e0c4386eSCy Schubert $f = data_file($f); 39*e0c4386eSCy Schubert 40*e0c4386eSCy Schubert if ( -s $f ) { 41*e0c4386eSCy Schubert with({ exit_checker => sub { return shift == $should_fail; } }, 42*e0c4386eSCy Schubert sub { 43*e0c4386eSCy Schubert ok(pkey_check($f, $pubcheck), $str); 44*e0c4386eSCy Schubert }); 45*e0c4386eSCy Schubert } else { 46*e0c4386eSCy Schubert fail("Missing file $f"); 47*e0c4386eSCy Schubert } 48*e0c4386eSCy Schubert} 49*e0c4386eSCy Schubert 50*e0c4386eSCy Schubertsetup("test_pkey_check"); 51*e0c4386eSCy Schubert 52*e0c4386eSCy Schubertmy @negative_tests = (); 53*e0c4386eSCy Schubert 54*e0c4386eSCy Schubertpush(@negative_tests, ( 55*e0c4386eSCy Schubert # For EC keys the range for the secret scalar `k` is `1 <= k <= n-1` 56*e0c4386eSCy Schubert "ec_p256_bad_0.pem", # `k` set to `n` (equivalent to `0 mod n`, invalid) 57*e0c4386eSCy Schubert "ec_p256_bad_1.pem", # `k` set to `n+1` (equivalent to `1 mod n`, invalid) 58*e0c4386eSCy Schubert )) unless disabled("ec"); 59*e0c4386eSCy Schubert 60*e0c4386eSCy Schubertpush(@negative_tests, ( 61*e0c4386eSCy Schubert # For SM2 keys the range for the secret scalar `k` is `1 <= k < n-1` 62*e0c4386eSCy Schubert "sm2_bad_neg1.pem", # `k` set to `n-1` (invalid, because SM2 range) 63*e0c4386eSCy Schubert "sm2_bad_0.pem", # `k` set to `n` (equivalent to `0 mod n`, invalid) 64*e0c4386eSCy Schubert "sm2_bad_1.pem", # `k` set to `n+1` (equivalent to `1 mod n`, invalid) 65*e0c4386eSCy Schubert )) unless disabled("sm2"); 66*e0c4386eSCy Schubert 67*e0c4386eSCy Schubertmy @positive_tests = (); 68*e0c4386eSCy Schubert 69*e0c4386eSCy Schubertpush(@positive_tests, ( 70*e0c4386eSCy Schubert "dhpkey.pem" 71*e0c4386eSCy Schubert )) unless disabled("dh"); 72*e0c4386eSCy Schubert 73*e0c4386eSCy Schubertmy @negative_pubtests = ("rsapub_17k.pem"); # Too big RSA public key 74*e0c4386eSCy Schubert 75*e0c4386eSCy Schubertpush(@negative_pubtests, ( 76*e0c4386eSCy Schubert "dsapub_noparam.der" 77*e0c4386eSCy Schubert )) unless disabled("dsa"); 78*e0c4386eSCy Schubert 79*e0c4386eSCy Schubertmy @positive_pubtests = (); 80*e0c4386eSCy Schubert 81*e0c4386eSCy Schubertpush(@positive_pubtests, ( 82*e0c4386eSCy Schubert "dsapub.pem" 83*e0c4386eSCy Schubert )) unless disabled("dsa"); 84*e0c4386eSCy Schubert 85*e0c4386eSCy Schubertplan skip_all => "No tests within the current enabled feature set" 86*e0c4386eSCy Schubert unless @negative_tests && @positive_tests 87*e0c4386eSCy Schubert && @negative_pubtests && @positive_pubtests; 88*e0c4386eSCy Schubert 89*e0c4386eSCy Schubertplan tests => scalar(@negative_tests) + scalar(@positive_tests) 90*e0c4386eSCy Schubert + scalar(@negative_pubtests) + scalar(@positive_pubtests); 91*e0c4386eSCy Schubert 92*e0c4386eSCy Schubertforeach my $t (@negative_tests) { 93*e0c4386eSCy Schubert check_key($t, 1, 0); 94*e0c4386eSCy Schubert} 95*e0c4386eSCy Schubert 96*e0c4386eSCy Schubertforeach my $t (@positive_tests) { 97*e0c4386eSCy Schubert check_key($t, 0, 0); 98*e0c4386eSCy Schubert} 99*e0c4386eSCy Schubert 100*e0c4386eSCy Schubertforeach my $t (@negative_pubtests) { 101*e0c4386eSCy Schubert check_key($t, 1, 1); 102*e0c4386eSCy Schubert} 103*e0c4386eSCy Schubert 104*e0c4386eSCy Schubertforeach my $t (@positive_pubtests) { 105*e0c4386eSCy Schubert check_key($t, 0, 1); 106*e0c4386eSCy Schubert} 107