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