2 # Copyright 1995-2016 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));
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.
51 open(IN, "$ARGV[0]") || die "Can't open input file $ARGV[0], $!";
53 next unless /^\#define\s+(\S+)\s+(.*)$/;
58 if ($v =~ /^SN_(.*)$/) {
59 if (defined $dupsn{$d}) {
60 print "WARNING: Duplicate short name \"$d\"\n";
66 elsif ($v =~ /^LN_(.*)$/) {
67 if (defined $dupln{$d}) {
68 print "WARNING: Duplicate long name \"$d\"\n";
74 elsif ($v =~ /^NID_(.*)$/) {
77 elsif ($v =~ /^OBJ_(.*)$/) {
84 # For every value in %obj, recursively expand OBJ_xxx values. That is:
86 # #define OBJ_identified_organization OBJ_iso,3L
87 # Modify %objd values in-place. Create an %objn array that has
91 foreach my $k (keys %objd) {
92 $changed = 1 if $objd{$k} =~ s/(OBJ_[^,]+),/$objd{$1},/;
96 my @a = sort { $a <=> $b } keys %nid;
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.
107 for (my $i = 0; $i < $n; $i++) {
108 if (!defined $nid{$i}) {
109 push(@out, " { NULL, NULL, NID_undef },\n");
113 my $sn = defined $sn{$nid{$i}} ? "$sn{$nid{$i}}" : "NULL";
114 my $ln = defined $ln{$nid{$i}} ? "$ln{$nid{$i}}" : "NULL";
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}}};
132 # Format using fixed-with because we use strcmp later.
133 foreach (unpack("C*",$r)) {
134 $z .= sprintf("0x%02X,", $_);
137 $obj_der{$obj{$nid{$i}}} = $z;
138 $obj_len{$obj{$nid{$i}}} = $length;
141 sprintf(" %-45s /* [%5d] %s */\n",
142 $z, $lvalues, $obj{$nid{$i}}));
143 $out .= ", $length, &so[$lvalues]";
150 # Finally ready to generate the output.
151 open(OUT, ">$ARGV[1]") || die "Can't open output file $ARGV[1], $!";
154 * WARNING: do not edit!
155 * Generated by crypto/objects/obj_dat.pl
157 * Copyright 1995-2016 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
166 print OUT "/* Serialized OID's */\n";
167 printf OUT "static const unsigned char so[%d] = {\n", $lvalues + 1;
171 printf OUT "#define NUM_NID %d\n", $n;
172 printf OUT "static const ASN1_OBJECT nid_objs[NUM_NID] = {\n";
177 no warnings "uninitialized";
178 @a = grep(defined $sn{$nid{$_}}, 0 .. $n);
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", $_;
188 no warnings "uninitialized";
189 @a = grep(defined $ln{$nid{$_}}, 0 .. $n);
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", $_;
199 no warnings "uninitialized";
200 @a = grep(defined $obj{$nid{$_}}, 0 .. $n);
202 printf OUT "#define NUM_OBJ %d\n", $#a + 1;
203 printf OUT "static const unsigned int obj_objs[NUM_OBJ] = {\n";
205 # Compare DER; prefer shorter; if some length, use the "smaller" encoding.
208 no warnings "uninitialized";
209 my $A = $obj_len{$obj{$nid{$a}}};
210 my $B = $obj_len{$obj{$nid{$b}}};
212 return $r if $r != 0;
214 $A = $obj_der{$obj{$nid{$a}}};
215 $B = $obj_der{$obj{$nid{$b}}};
218 foreach (sort obj_cmp @a) {
219 my $m = $obj{$nid{$_}};
223 printf OUT " %4d, /* %-32s %s */\n", $_, $m, $v;