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