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