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