Update copyright year
[oweals/openssl.git] / crypto / objects / obj_dat.pl
1 #! /usr/bin/env perl
2 # Copyright 1995-2020 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
9 use integer;
10 use strict;
11 use warnings;
12
13 # Generate the DER encoding for the given OID.
14 sub der_it
15 {
16     # Prologue
17     my ($v) = @_;
18     my @a = split(/\s+/, $v);
19     my $ret = pack("C*", $a[0] * 40 + $a[1]);
20     shift @a;
21     shift @a;
22
23     # Loop over rest of bytes; or in 0x80 for multi-byte numbers.
24     my $t;
25     foreach (@a) {
26         my @r = ();
27         $t = 0;
28         while ($_ >= 128) {
29             my $x = $_ % 128;
30             $_ /= 128;
31             push(@r, ($t++ ? 0x80 : 0) | $x);
32         }
33         push(@r, ($t++ ? 0x80 : 0) | $_);
34         $ret .= pack("C*", reverse(@r));
35     }
36     return $ret;
37 }
38
39 # The year the output file is generated.
40 my $YEAR = [localtime()]->[5] + 1900;
41
42 # Read input, parse all #define's into OID name and value.
43 # Populate %ln and %sn with long and short names (%dupln and %dupsn)
44 # are used to watch for duplicates.  Also %nid and %obj get the
45 # NID and OBJ entries.
46 my %ln;
47 my %sn;
48 my %dupln;
49 my %dupsn;
50 my %nid;
51 my %obj;
52 my %objd;
53 open(IN, "$ARGV[0]") || die "Can't open input file $ARGV[0], $!";
54 while (<IN>) {
55     next unless /^\#define\s+(\S+)\s+(.*)$/;
56     my $v = $1;
57     my $d = $2;
58     $d =~ s/^\"//;
59     $d =~ s/\"$//;
60     if ($v =~ /^SN_(.*)$/) {
61         if (defined $dupsn{$d}) {
62             print "WARNING: Duplicate short name \"$d\"\n";
63         } else {
64             $dupsn{$d} = 1;
65         }
66         $sn{$1} = $d;
67     }
68     elsif ($v =~ /^LN_(.*)$/) {
69         if (defined $dupln{$d}) {
70             print "WARNING: Duplicate long name \"$d\"\n";
71         } else {
72             $dupln{$d} = 1;
73         }
74         $ln{$1} = $d;
75     }
76     elsif ($v =~ /^NID_(.*)$/) {
77         $nid{$d} = $1;
78     }
79     elsif ($v =~ /^OBJ_(.*)$/) {
80         $obj{$1} = $v;
81         $objd{$v} = $d;
82     }
83 }
84 close IN;
85
86 # For every value in %obj, recursively expand OBJ_xxx values.  That is:
87 #     #define OBJ_iso 1L
88 #     #define OBJ_identified_organization OBJ_iso,3L
89 # Modify %objd values in-place.  Create an %objn array that has
90 my $changed;
91 do {
92     $changed = 0;
93     foreach my $k (keys %objd) {
94         $changed = 1 if $objd{$k} =~ s/(OBJ_[^,]+),/$objd{$1},/;
95     }
96 } while ($changed);
97
98 my @a = sort { $a <=> $b } keys %nid;
99 my $n = $a[$#a] + 1;
100 my @lvalues = ();
101 my $lvalues = 0;
102
103 # Scan all defined objects, building up the @out array.
104 # %obj_der holds the DER encoding as an array of bytes, and %obj_len
105 # holds the length in bytes.
106 my @out;
107 my %obj_der;
108 my %obj_len;
109 for (my $i = 0; $i < $n; $i++) {
110     if (!defined $nid{$i}) {
111         push(@out, "    { NULL, NULL, NID_undef },\n");
112         next;
113     }
114
115     my $sn = defined $sn{$nid{$i}} ? "$sn{$nid{$i}}" : "NULL";
116     my $ln = defined $ln{$nid{$i}} ? "$ln{$nid{$i}}" : "NULL";
117     if ($sn eq "NULL") {
118         $sn = $ln;
119         $sn{$nid{$i}} = $ln;
120     }
121     if ($ln eq "NULL") {
122         $ln = $sn;
123         $ln{$nid{$i}} = $sn;
124     }
125
126     my $out = "    {\"$sn\", \"$ln\", NID_$nid{$i}";
127     if (defined $obj{$nid{$i}} && $objd{$obj{$nid{$i}}} =~ /,/) {
128         my $v = $objd{$obj{$nid{$i}}};
129         $v =~ s/L//g;
130         $v =~ s/,/ /g;
131         my $r = &der_it($v);
132         my $z = "";
133         my $length = 0;
134         # Format using fixed-with because we use strcmp later.
135         foreach (unpack("C*",$r)) {
136             $z .= sprintf("0x%02X,", $_);
137             $length++;
138         }
139         $obj_der{$obj{$nid{$i}}} = $z;
140         $obj_len{$obj{$nid{$i}}} = $length;
141
142         push(@lvalues,
143             sprintf("    %-45s  /* [%5d] %s */\n",
144                 $z, $lvalues, $obj{$nid{$i}}));
145         $out .= ", $length, &so[$lvalues]";
146         $lvalues += $length;
147     }
148     $out .= "},\n";
149     push(@out, $out);
150 }
151
152 # Finally ready to generate the output.
153 print <<"EOF";
154 /*
155  * WARNING: do not edit!
156  * Generated by crypto/objects/obj_dat.pl
157  *
158  * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
159  * Licensed under the Apache License 2.0 (the "License").  You may not use
160  * this file except in compliance with the License.  You can obtain a copy
161  * in the file LICENSE in the source distribution or at
162  * https://www.openssl.org/source/license.html
163  */
164
165 EOF
166
167 print "/* Serialized OID's */\n";
168 printf "static const unsigned char so[%d] = {\n", $lvalues + 1;
169 print @lvalues;
170 print "};\n\n";
171
172 printf "#define NUM_NID %d\n", $n;
173 printf "static const ASN1_OBJECT nid_objs[NUM_NID] = {\n";
174 print @out;
175 print  "};\n\n";
176
177 {
178     no warnings "uninitialized";
179     @a = grep(defined $sn{$nid{$_}}, 0 .. $n);
180 }
181 printf "#define NUM_SN %d\n", $#a + 1;
182 printf "static const unsigned int sn_objs[NUM_SN] = {\n";
183 foreach (sort { $sn{$nid{$a}} cmp $sn{$nid{$b}} } @a) {
184     printf "    %4d,    /* \"$sn{$nid{$_}}\" */\n", $_;
185 }
186 print  "};\n\n";
187
188 {
189     no warnings "uninitialized";
190     @a = grep(defined $ln{$nid{$_}}, 0 .. $n);
191 }
192 printf "#define NUM_LN %d\n", $#a + 1;
193 printf "static const unsigned int ln_objs[NUM_LN] = {\n";
194 foreach (sort { $ln{$nid{$a}} cmp $ln{$nid{$b}} } @a) {
195     printf "    %4d,    /* \"$ln{$nid{$_}}\" */\n", $_;
196 }
197 print  "};\n\n";
198
199 {
200     no warnings "uninitialized";
201     @a = grep(defined $obj{$nid{$_}}, 0 .. $n);
202 }
203 printf "#define NUM_OBJ %d\n", $#a + 1;
204 printf "static const unsigned int obj_objs[NUM_OBJ] = {\n";
205
206 # Compare DER; prefer shorter; if some length, use the "smaller" encoding.
207 sub obj_cmp
208 {
209     no warnings "uninitialized";
210     my $A = $obj_len{$obj{$nid{$a}}};
211     my $B = $obj_len{$obj{$nid{$b}}};
212     my $r = $A - $B;
213     return $r if $r != 0;
214
215     $A = $obj_der{$obj{$nid{$a}}};
216     $B = $obj_der{$obj{$nid{$b}}};
217     return $A cmp $B;
218 }
219 foreach (sort obj_cmp @a) {
220     my $m = $obj{$nid{$_}};
221     my $v = $objd{$m};
222     $v =~ s/L//g;
223     $v =~ s/,/ /g;
224     printf "    %4d,    /* %-32s %s */\n", $_, $m, $v;
225 }
226 print  "};\n";