obj_dat.pl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #! /usr/bin/env perl
  2. # Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License 2.0 (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. use integer;
  9. use strict;
  10. use warnings;
  11. use FindBin;
  12. use lib "$FindBin::Bin/../../util/perl";
  13. use OpenSSL::copyright;
  14. # Generate the DER encoding for the given OID.
  15. sub der_it
  16. {
  17. # Prologue
  18. my ($v) = @_;
  19. my @a = split(/\s+/, $v);
  20. my $ret = pack("C*", $a[0] * 40 + $a[1]);
  21. shift @a;
  22. shift @a;
  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. # The year the output file is generated.
  39. my $YEAR = OpenSSL::copyright::latest(($0, $ARGV[0]));
  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. # 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. my @a = sort { $a <=> $b } keys %nid;
  95. my $n = $a[$#a] + 1;
  96. my @lvalues = ();
  97. my $lvalues = 0;
  98. # Scan all defined objects, building up the @out array.
  99. # %obj_der holds the DER encoding as an array of bytes, and %obj_len
  100. # holds the length in bytes.
  101. my @out;
  102. my %obj_der;
  103. my %obj_len;
  104. for (my $i = 0; $i < $n; $i++) {
  105. if (!defined $nid{$i}) {
  106. push(@out, " { NULL, NULL, NID_undef },\n");
  107. next;
  108. }
  109. my $sn = defined $sn{$nid{$i}} ? "$sn{$nid{$i}}" : "NULL";
  110. my $ln = defined $ln{$nid{$i}} ? "$ln{$nid{$i}}" : "NULL";
  111. if ($sn eq "NULL") {
  112. $sn = $ln;
  113. $sn{$nid{$i}} = $ln;
  114. }
  115. if ($ln eq "NULL") {
  116. $ln = $sn;
  117. $ln{$nid{$i}} = $sn;
  118. }
  119. my $out = " {\"$sn\", \"$ln\", NID_$nid{$i}";
  120. if (defined $obj{$nid{$i}} && $objd{$obj{$nid{$i}}} =~ /,/) {
  121. my $v = $objd{$obj{$nid{$i}}};
  122. $v =~ s/L//g;
  123. $v =~ s/,/ /g;
  124. my $r = &der_it($v);
  125. my $z = "";
  126. my $length = 0;
  127. # Format using fixed-with because we use strcmp later.
  128. foreach (unpack("C*",$r)) {
  129. $z .= sprintf("0x%02X,", $_);
  130. $length++;
  131. }
  132. $obj_der{$obj{$nid{$i}}} = $z;
  133. $obj_len{$obj{$nid{$i}}} = $length;
  134. push(@lvalues,
  135. sprintf(" %-45s /* [%5d] %s */\n",
  136. $z, $lvalues, $obj{$nid{$i}}));
  137. $out .= ", $length, &so[$lvalues]";
  138. $lvalues += $length;
  139. }
  140. $out .= "},\n";
  141. push(@out, $out);
  142. }
  143. # Finally ready to generate the output.
  144. print <<"EOF";
  145. /*
  146. * WARNING: do not edit!
  147. * Generated by crypto/objects/obj_dat.pl
  148. *
  149. * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  150. * Licensed under the Apache License 2.0 (the "License"). You may not use
  151. * this file except in compliance with the License. You can obtain a copy
  152. * in the file LICENSE in the source distribution or at
  153. * https://www.openssl.org/source/license.html
  154. */
  155. EOF
  156. print "/* Serialized OID's */\n";
  157. printf "static const unsigned char so[%d] = {\n", $lvalues + 1;
  158. print @lvalues;
  159. print "};\n\n";
  160. printf "#define NUM_NID %d\n", $n;
  161. printf "static const ASN1_OBJECT nid_objs[NUM_NID] = {\n";
  162. print @out;
  163. print "};\n\n";
  164. {
  165. no warnings "uninitialized";
  166. @a = grep(defined $sn{$nid{$_}}, 0 .. $n);
  167. }
  168. printf "#define NUM_SN %d\n", $#a + 1;
  169. printf "static const unsigned int sn_objs[NUM_SN] = {\n";
  170. foreach (sort { $sn{$nid{$a}} cmp $sn{$nid{$b}} } @a) {
  171. printf " %4d, /* \"$sn{$nid{$_}}\" */\n", $_;
  172. }
  173. print "};\n\n";
  174. {
  175. no warnings "uninitialized";
  176. @a = grep(defined $ln{$nid{$_}}, 0 .. $n);
  177. }
  178. printf "#define NUM_LN %d\n", $#a + 1;
  179. printf "static const unsigned int ln_objs[NUM_LN] = {\n";
  180. foreach (sort { $ln{$nid{$a}} cmp $ln{$nid{$b}} } @a) {
  181. printf " %4d, /* \"$ln{$nid{$_}}\" */\n", $_;
  182. }
  183. print "};\n\n";
  184. {
  185. no warnings "uninitialized";
  186. @a = grep(defined $obj{$nid{$_}}, 0 .. $n);
  187. }
  188. printf "#define NUM_OBJ %d\n", $#a + 1;
  189. printf "static const unsigned int obj_objs[NUM_OBJ] = {\n";
  190. # Compare DER; prefer shorter; if some length, use the "smaller" encoding.
  191. sub obj_cmp
  192. {
  193. no warnings "uninitialized";
  194. my $A = $obj_len{$obj{$nid{$a}}};
  195. my $B = $obj_len{$obj{$nid{$b}}};
  196. my $r = $A - $B;
  197. return $r if $r != 0;
  198. $A = $obj_der{$obj{$nid{$a}}};
  199. $B = $obj_der{$obj{$nid{$b}}};
  200. return $A cmp $B;
  201. }
  202. foreach (sort obj_cmp @a) {
  203. my $m = $obj{$nid{$_}};
  204. my $v = $objd{$m};
  205. $v =~ s/L//g;
  206. $v =~ s/,/ /g;
  207. printf " %4d, /* %-32s %s */\n", $_, $m, $v;
  208. }
  209. print "};\n";