2 # Copyright 1995-2017 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
12 # Generate the DER encoding for the given OID.
17 my @a = split(/\s+/, $v);
18 my $ret = pack("C*", $a[0] * 40 + $a[1]);
22 # Loop over rest of bytes; or in 0x80 for multi-byte numbers.
30 push(@r, ($t++ ? 0x80 : 0) | $x);
32 push(@r, ($t++ ? 0x80 : 0) | $_);
33 $ret .= pack("C*", reverse(@r));
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.
50 open(IN, "$ARGV[0]") || die "Can't open input file $ARGV[0], $!";
52 next unless /^\#define\s+(\S+)\s+(.*)$/;
57 if ($v =~ /^SN_(.*)$/) {
58 if (defined $dupsn{$d}) {
59 print "WARNING: Duplicate short name \"$d\"\n";
65 elsif ($v =~ /^LN_(.*)$/) {
66 if (defined $dupln{$d}) {
67 print "WARNING: Duplicate long name \"$d\"\n";
73 elsif ($v =~ /^NID_(.*)$/) {
76 elsif ($v =~ /^OBJ_(.*)$/) {
83 # For every value in %obj, recursively expand OBJ_xxx values. That is:
85 # #define OBJ_identified_organization OBJ_iso,3L
86 # Modify %objd values in-place. Create an %objn array that has
90 foreach my $k (keys %objd) {
91 $changed = 1 if $objd{$k} =~ s/(OBJ_[^,]+),/$objd{$1},/;
95 my @a = sort { $a <=> $b } keys %nid;
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.
106 for (my $i = 0; $i < $n; $i++) {
107 if (!defined $nid{$i}) {
108 push(@out, " { NULL, NULL, NID_undef },\n");
112 my $sn = defined $sn{$nid{$i}} ? "$sn{$nid{$i}}" : "NULL";
113 my $ln = defined $ln{$nid{$i}} ? "$ln{$nid{$i}}" : "NULL";
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}}};
131 # Format using fixed-with because we use strcmp later.
132 foreach (unpack("C*",$r)) {
133 $z .= sprintf("0x%02X,", $_);
136 $obj_der{$obj{$nid{$i}}} = $z;
137 $obj_len{$obj{$nid{$i}}} = $length;
140 sprintf(" %-45s /* [%5d] %s */\n",
141 $z, $lvalues, $obj{$nid{$i}}));
142 $out .= ", $length, &so[$lvalues]";
149 # Finally ready to generate the output.
150 open(OUT, ">$ARGV[1]") || die "Can't open output file $ARGV[1], $!";
153 * WARNING: do not edit!
154 * Generated by crypto/objects/obj_dat.pl
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
165 print OUT "/* Serialized OID's */\n";
166 printf OUT "static const unsigned char so[%d] = {\n", $lvalues + 1;
170 printf OUT "#define NUM_NID %d\n", $n;
171 printf OUT "static const ASN1_OBJECT nid_objs[NUM_NID] = {\n";
176 no warnings "uninitialized";
177 @a = grep(defined $sn{$nid{$_}}, 0 .. $n);
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", $_;
187 no warnings "uninitialized";
188 @a = grep(defined $ln{$nid{$_}}, 0 .. $n);
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", $_;
198 no warnings "uninitialized";
199 @a = grep(defined $obj{$nid{$_}}, 0 .. $n);
201 printf OUT "#define NUM_OBJ %d\n", $#a + 1;
202 printf OUT "static const unsigned int obj_objs[NUM_OBJ] = {\n";
204 # Compare DER; prefer shorter; if some length, use the "smaller" encoding.
207 no warnings "uninitialized";
208 my $A = $obj_len{$obj{$nid{$a}}};
209 my $B = $obj_len{$obj{$nid{$b}}};
211 return $r if $r != 0;
213 $A = $obj_der{$obj{$nid{$a}}};
214 $B = $obj_der{$obj{$nid{$b}}};
217 foreach (sort obj_cmp @a) {
218 my $m = $obj{$nid{$_}};
222 printf OUT " %4d, /* %-32s %s */\n", $_, $m, $v;