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