]> git.ipfire.org Git - thirdparty/openssl.git/blame - util/ck_errf.pl
Test that we can use the FIPS provider
[thirdparty/openssl.git] / util / ck_errf.pl
CommitLineData
e0a65194 1#! /usr/bin/env perl
fd38836b 2# Copyright 1995-2018 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3#
9059ab42 4# Licensed under the Apache License 2.0 (the "License"). You may not use
e0a65194
RS
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
d02b48c6
RE
9# This is just a quick script to scan for cases where the 'error'
10# function name in a XXXerr() macro is wrong.
609b0852 11#
d02b48c6
RE
12# Run in the top level by going
13# perl util/ck_errf.pl */*.c */*/*.c
14#
15
5038e325
RS
16use strict;
17use warnings;
18
b9def672 19my $config;
3ed3603b 20my $err_strict = 0;
b9def672
RL
21my $debug = 0;
22my $internal = 0;
23
24sub help
25{
26 print STDERR <<"EOF";
27mkerr.pl [options] [files...]
28
29Options:
30
31 -conf FILE Use the named config file FILE instead of the default.
32
33 -debug Verbose output debugging on stderr.
34
35 -internal Generate code that is to be built as part of OpenSSL itself.
36 Also scans internal list of files.
37
38 -strict If any error was found, fail with exit code 1, otherwise 0.
39
40 -help Show this help text.
41
42 ... Additional arguments are added to the file list to scan,
43 if '-internal' was NOT specified on the command line.
44
45EOF
46}
47
48while ( @ARGV ) {
49 my $arg = $ARGV[0];
50 last unless $arg =~ /-.*/;
51 $arg = $1 if $arg =~ /-(-.*)/;
52 if ( $arg eq "-conf" ) {
53 $config = $ARGV[1];
54 shift @ARGV;
55 } elsif ( $arg eq "-debug" ) {
56 $debug = 1;
57 } elsif ( $arg eq "-internal" ) {
58 $internal = 1;
59 } elsif ( $arg eq "-strict" ) {
60 $err_strict = 1;
61 } elsif ( $arg =~ /-*h(elp)?/ ) {
62 &help();
63 exit;
64 } elsif ( $arg =~ /-.*/ ) {
65 die "Unknown option $arg; use -h for help.\n";
66 }
67 shift @ARGV;
68}
69
70my @source;
71if ( $internal ) {
72 die "Extra parameters given.\n" if @ARGV;
73 $config = "crypto/err/openssl.ec" unless defined $config;
74 @source = ( glob('crypto/*.c'), glob('crypto/*/*.c'),
75 glob('ssl/*.c'), glob('ssl/*/*.c') );
76} else {
77 die "Configuration file not given.\nSee '$0 -help' for information\n"
78 unless defined $config;
79 @source = @ARGV;
80}
5038e325 81
a21180b7
RL
82# To detect if there is any error generation for a libcrypto/libssl libs
83# we don't know, we need to find out what libs we do know. That list is
84# readily available in crypto/err/openssl.ec, in form of lines starting
b9def672
RL
85# with "L ". Note that we always rely on the modules SYS and ERR to be
86# generally available.
87my %libs = ( SYS => 1, ERR => 1 );
a21180b7
RL
88open my $cfh, $config or die "Trying to read $config: $!\n";
89while (<$cfh>) {
90 s|\R$||; # Better chomp
91 next unless m|^L ([0-9A-Z_]+)\s|;
92 next if $1 eq "NONE";
93 $libs{$1} = 1;
94}
95
b9def672
RL
96my $bad = 0;
97foreach my $file (@source) {
5038e325
RS
98 open( IN, "<$file" ) || die "Can't open $file, $!";
99 my $func = "";
100 while (<IN>) {
101 if ( !/;$/ && /^\**([a-zA-Z_].*[\s*])?([A-Za-z_0-9]+)\(.*([),]|$)/ ) {
102 /^([^()]*(\([^()]*\)[^()]*)*)\(/;
103 $1 =~ /([A-Za-z_0-9]*)$/;
104 $func = $1;
105 $func =~ tr/A-Z/a-z/;
106 }
a21180b7 107 if ( /([A-Z0-9_]+[A-Z0-9])err\(([^,]+)/ && !/ckerr_ignore/ ) {
5038e325
RS
108 my $errlib = $1;
109 my $n = $2;
3ed3603b 110
a21180b7 111 unless ( $libs{$errlib} ) {
b9def672
RL
112 print "$file:$.:$errlib not listed in $config\n";
113 $libs{$errlib} = 1; # To not display it again
a21180b7
RL
114 $bad = 1;
115 }
116
5038e325
RS
117 if ( $func eq "" ) {
118 print "$file:$.:???:$n\n";
119 $bad = 1;
120 next;
121 }
8afca8d9 122
a21180b7 123 if ( $n !~ /^(.+)_F_(.+)$/ ) {
5038e325
RS
124 #print "check -$file:$.:$func:$n\n";
125 next;
126 }
127 my $lib = $1;
128 $n = $2;
8afca8d9 129
5038e325
RS
130 if ( $lib ne $errlib ) {
131 print "$file:$.:$func:$n [${errlib}err]\n";
132 $bad = 1;
133 next;
134 }
d02b48c6 135
5038e325
RS
136 $n =~ tr/A-Z/a-z/;
137 if ( $n ne $func && $errlib ne "SYS" ) {
138 print "$file:$.:$func:$n\n";
139 $bad = 1;
140 next;
141 }
d02b48c6 142
5038e325 143 # print "$func:$1\n";
d02b48c6 144 }
5038e325
RS
145 }
146 close(IN);
147}
d02b48c6 148
5038e325
RS
149if ( $bad && $err_strict ) {
150 print STDERR "FATAL: error discrepancy\n";
151 exit 1;
152}