1#! /usr/bin/env perl
2# Copyright 1998-2022 The OpenSSL Project Authors. All Rights Reserved.
3#
4# Licensed under the OpenSSL license (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
10use strict;
11use FindBin;
12use lib "$FindBin::Bin/../../util/perl";
13use OpenSSL::copyright;
14
15my %xref_tbl;
16my %oid_tbl;
17
18my ($mac_file, $xref_file) = @ARGV;
19
20# The year the output file is generated.
21my $YEAR = OpenSSL::copyright::latest(($0, $mac_file, $xref_file));
22
23open(IN, $mac_file) || die "Can't open $mac_file, $!\n";
24
25# Read in OID nid values for a lookup table.
26
27while (<IN>)
28	{
29	s|\R$||;                # Better chomp
30	my ($name, $num) = /^(\S+)\s+(\S+)$/;
31	$oid_tbl{$name} = $num;
32	}
33close IN;
34
35open(IN, $xref_file) || die "Can't open $xref_file, $!\n";
36
37my $ln = 1;
38
39while (<IN>)
40	{
41	s|\R$||;                # Better chomp
42	s/#.*$//;
43	next if (/^\S*$/);
44	my ($xr, $p1, $p2) = /^(\S+)\s+(\S+)\s+(\S+)/;
45	check_oid($xr);
46	check_oid($p1);
47	check_oid($p2);
48	$xref_tbl{$xr} = [$p1, $p2, $ln];
49	}
50
51my @xrkeys = keys %xref_tbl;
52
53my @srt1 = sort { $oid_tbl{$a} <=> $oid_tbl{$b}} @xrkeys;
54
55my $i;
56for($i = 0; $i <= $#srt1; $i++)
57	{
58	$xref_tbl{$srt1[$i]}[2] = $i;
59	}
60
61my @srt2 = sort
62	{
63	my$ap1 = $oid_tbl{$xref_tbl{$a}[0]};
64	my$bp1 = $oid_tbl{$xref_tbl{$b}[0]};
65	return $ap1 - $bp1 if ($ap1 != $bp1);
66	my$ap2 = $oid_tbl{$xref_tbl{$a}[1]};
67	my$bp2 = $oid_tbl{$xref_tbl{$b}[1]};
68
69	return $ap2 - $bp2;
70	} @xrkeys;
71
72my $pname = $0;
73$pname =~ s|.*/||;
74
75print <<EOF;
76/*
77 * WARNING: do not edit!
78 * Generated by $pname
79 *
80 * Copyright 1998-$YEAR The OpenSSL Project Authors. All Rights Reserved.
81 *
82 * Licensed under the OpenSSL license (the "License").  You may not use
83 * this file except in compliance with the License.  You can obtain a copy
84 * in the file LICENSE in the source distribution or at
85 * https://www.openssl.org/source/license.html
86 */
87
88
89typedef struct {
90    int sign_id;
91    int hash_id;
92    int pkey_id;
93} nid_triple;
94
95DEFINE_STACK_OF(nid_triple)
96
97static const nid_triple sigoid_srt[] = {
98EOF
99
100foreach (@srt1)
101	{
102	my $xr = $_;
103	my ($p1, $p2) = @{$xref_tbl{$_}};
104	my $o1 = "    {NID_$xr, NID_$p1,";
105	my $o2 = "NID_$p2},";
106        if (length("$o1 $o2") < 78)
107		{
108		print "$o1 $o2\n";
109		}
110	else
111		{
112		print "$o1\n     $o2\n";
113		}
114        }
115
116print "};";
117print <<EOF;
118
119
120static const nid_triple *const sigoid_srt_xref[] = {
121EOF
122
123foreach (@srt2)
124	{
125	my ($p1, $p2, $x) = @{$xref_tbl{$_}};
126	# If digest or signature algorithm is "undef" then the algorithm
127	# needs special handling and is excluded from the cross reference table.
128	next if $p1 eq "undef" || $p2 eq "undef";
129	print "    \&sigoid_srt\[$x\],\n";
130	}
131
132print "};\n";
133
134sub check_oid
135	{
136	my ($chk) = @_;
137	if (!exists $oid_tbl{$chk})
138		{
139		die "Can't find \"$chk\"\n";
140		}
141	}
142