2 # Copyright 1995-2019 The OpenSSL Project Authors. All Rights Reserved.
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
13 # Generate the DER encoding for the given OID.
18 my @a = split(/\s+/, $v);
19 my $ret = pack("C*", $a[0] * 40 + $a[1]);
23 # Loop over rest of bytes; or in 0x80 for multi-byte numbers.
31 push(@r, ($t++ ? 0x80 : 0) | $x);
33 push(@r, ($t++ ? 0x80 : 0) | $_);
34 $ret .= pack("C*", reverse(@r));
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;
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.
55 open(IN, "$ARGV[0]") || die "Can't open input file $ARGV[0], $!";
57 next unless /^\#define\s+(\S+)\s+(.*)$/;
62 if ($v =~ /^SN_(.*)$/) {
63 if (defined $dupsn{$d}) {
64 print "WARNING: Duplicate short name \"$d\"\n";
70 elsif ($v =~ /^LN_(.*)$/) {
71 if (defined $dupln{$d}) {
72 print "WARNING: Duplicate long name \"$d\"\n";
78 elsif ($v =~ /^NID_(.*)$/) {
81 elsif ($v =~ /^OBJ_(.*)$/) {
88 # For every value in %obj, recursively expand OBJ_xxx values. That is:
90 # #define OBJ_identified_organization OBJ_iso,3L
91 # Modify %objd values in-place. Create an %objn array that has
95 foreach my $k (keys %objd) {
96 $changed = 1 if $objd{$k} =~ s/(OBJ_[^,]+),/$objd{$1},/;
100 my @a = sort { $a <=> $b } keys %nid;
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.
111 for (my $i = 0; $i < $n; $i++) {
112 if (!defined $nid{$i}) {
113 push(@out, " { NULL, NULL, NID_undef },\n");
117 my $sn = defined $sn{$nid{$i}} ? "$sn{$nid{$i}}" : "NULL";
118 my $ln = defined $ln{$nid{$i}} ? "$ln{$nid{$i}}" : "NULL";
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}}};
136 # Format using fixed-with because we use strcmp later.
137 foreach (unpack("C*",$r)) {
138 $z .= sprintf("0x%02X,", $_);
141 $obj_der{$obj{$nid{$i}}} = $z;
142 $obj_len{$obj{$nid{$i}}} = $length;
145 sprintf(" %-45s /* [%5d] %s */\n",
146 $z, $lvalues, $obj{$nid{$i}}));
147 $out .= ", $length, &so[$lvalues]";
154 # Finally ready to generate the output.
157 * WARNING: do not edit!
158 * Generated by crypto/objects/obj_dat.pl
160 * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
161 * Licensed under the OpenSSL license (the "License"). You may not use
162 * this file except in compliance with the License. You can obtain a copy
163 * in the file LICENSE in the source distribution or at
164 * https://www.openssl.org/source/license.html
169 print "/* Serialized OID's */\n";
170 printf "static const unsigned char so[%d] = {\n", $lvalues + 1;
174 printf "#define NUM_NID %d\n", $n;
175 printf "static const ASN1_OBJECT nid_objs[NUM_NID] = {\n";
180 no warnings "uninitialized";
181 @a = grep(defined $sn{$nid{$_}}, 0 .. $n);
183 printf "#define NUM_SN %d\n", $#a + 1;
184 printf "static const unsigned int sn_objs[NUM_SN] = {\n";
185 foreach (sort { $sn{$nid{$a}} cmp $sn{$nid{$b}} } @a) {
186 printf " %4d, /* \"$sn{$nid{$_}}\" */\n", $_;
191 no warnings "uninitialized";
192 @a = grep(defined $ln{$nid{$_}}, 0 .. $n);
194 printf "#define NUM_LN %d\n", $#a + 1;
195 printf "static const unsigned int ln_objs[NUM_LN] = {\n";
196 foreach (sort { $ln{$nid{$a}} cmp $ln{$nid{$b}} } @a) {
197 printf " %4d, /* \"$ln{$nid{$_}}\" */\n", $_;
202 no warnings "uninitialized";
203 @a = grep(defined $obj{$nid{$_}}, 0 .. $n);
205 printf "#define NUM_OBJ %d\n", $#a + 1;
206 printf "static const unsigned int obj_objs[NUM_OBJ] = {\n";
208 # Compare DER; prefer shorter; if some length, use the "smaller" encoding.
211 no warnings "uninitialized";
212 my $A = $obj_len{$obj{$nid{$a}}};
213 my $B = $obj_len{$obj{$nid{$b}}};
215 return $r if $r != 0;
217 $A = $obj_der{$obj{$nid{$a}}};
218 $B = $obj_der{$obj{$nid{$b}}};
221 foreach (sort obj_cmp @a) {
222 my $m = $obj{$nid{$_}};
226 printf " %4d, /* %-32s %s */\n", $_, $m, $v;