2
0

obj_dat.pl 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #! /usr/bin/env perl
  2. # Copyright 1995-2018 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. use integer;
  9. use strict;
  10. use warnings;
  11. # Generate the DER encoding for the given OID.
  12. sub der_it
  13. {
  14. # Prologue
  15. my ($v) = @_;
  16. my @a = split(/\s+/, $v);
  17. my $ret = pack("C*", $a[0] * 40 + $a[1]);
  18. shift @a;
  19. shift @a;
  20. # Loop over rest of bytes; or in 0x80 for multi-byte numbers.
  21. my $t;
  22. foreach (@a) {
  23. my @r = ();
  24. $t = 0;
  25. while ($_ >= 128) {
  26. my $x = $_ % 128;
  27. $_ /= 128;
  28. push(@r, ($t++ ? 0x80 : 0) | $x);
  29. }
  30. push(@r, ($t++ ? 0x80 : 0) | $_);
  31. $ret .= pack("C*", reverse(@r));
  32. }
  33. return $ret;
  34. }
  35. # Output year depends on the year of the script and the input file.
  36. my $YEAR = [localtime([stat($0)]->[9])]->[5] + 1900;
  37. my $iYEAR = [localtime([stat($ARGV[0])]->[9])]->[5] + 1900;
  38. $YEAR = $iYEAR if $iYEAR > $YEAR;
  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.
  43. my %ln;
  44. my %sn;
  45. my %dupln;
  46. my %dupsn;
  47. my %nid;
  48. my %obj;
  49. my %objd;
  50. open(IN, "$ARGV[0]") || die "Can't open input file $ARGV[0], $!";
  51. while (<IN>) {
  52. next unless /^\#define\s+(\S+)\s+(.*)$/;
  53. my $v = $1;
  54. my $d = $2;
  55. $d =~ s/^\"//;
  56. $d =~ s/\"$//;
  57. if ($v =~ /^SN_(.*)$/) {
  58. if (defined $dupsn{$d}) {
  59. print "WARNING: Duplicate short name \"$d\"\n";
  60. } else {
  61. $dupsn{$d} = 1;
  62. }
  63. $sn{$1} = $d;
  64. }
  65. elsif ($v =~ /^LN_(.*)$/) {
  66. if (defined $dupln{$d}) {
  67. print "WARNING: Duplicate long name \"$d\"\n";
  68. } else {
  69. $dupln{$d} = 1;
  70. }
  71. $ln{$1} = $d;
  72. }
  73. elsif ($v =~ /^NID_(.*)$/) {
  74. $nid{$d} = $1;
  75. }
  76. elsif ($v =~ /^OBJ_(.*)$/) {
  77. $obj{$1} = $v;
  78. $objd{$v} = $d;
  79. }
  80. }
  81. close IN;
  82. # For every value in %obj, recursively expand OBJ_xxx values. That is:
  83. # #define OBJ_iso 1L
  84. # #define OBJ_identified_organization OBJ_iso,3L
  85. # Modify %objd values in-place. Create an %objn array that has
  86. my $changed;
  87. do {
  88. $changed = 0;
  89. foreach my $k (keys %objd) {
  90. $changed = 1 if $objd{$k} =~ s/(OBJ_[^,]+),/$objd{$1},/;
  91. }
  92. } while ($changed);
  93. my @a = sort { $a <=> $b } keys %nid;
  94. my $n = $a[$#a] + 1;
  95. my @lvalues = ();
  96. my $lvalues = 0;
  97. # Scan all defined objects, building up the @out array.
  98. # %obj_der holds the DER encoding as an array of bytes, and %obj_len
  99. # holds the length in bytes.
  100. my @out;
  101. my %obj_der;
  102. my %obj_len;
  103. for (my $i = 0; $i < $n; $i++) {
  104. if (!defined $nid{$i}) {
  105. push(@out, " { NULL, NULL, NID_undef },\n");
  106. next;
  107. }
  108. my $sn = defined $sn{$nid{$i}} ? "$sn{$nid{$i}}" : "NULL";
  109. my $ln = defined $ln{$nid{$i}} ? "$ln{$nid{$i}}" : "NULL";
  110. if ($sn eq "NULL") {
  111. $sn = $ln;
  112. $sn{$nid{$i}} = $ln;
  113. }
  114. if ($ln eq "NULL") {
  115. $ln = $sn;
  116. $ln{$nid{$i}} = $sn;
  117. }
  118. my $out = " {\"$sn\", \"$ln\", NID_$nid{$i}";
  119. if (defined $obj{$nid{$i}} && $objd{$obj{$nid{$i}}} =~ /,/) {
  120. my $v = $objd{$obj{$nid{$i}}};
  121. $v =~ s/L//g;
  122. $v =~ s/,/ /g;
  123. my $r = &der_it($v);
  124. my $z = "";
  125. my $length = 0;
  126. # Format using fixed-with because we use strcmp later.
  127. foreach (unpack("C*",$r)) {
  128. $z .= sprintf("0x%02X,", $_);
  129. $length++;
  130. }
  131. $obj_der{$obj{$nid{$i}}} = $z;
  132. $obj_len{$obj{$nid{$i}}} = $length;
  133. push(@lvalues,
  134. sprintf(" %-45s /* [%5d] %s */\n",
  135. $z, $lvalues, $obj{$nid{$i}}));
  136. $out .= ", $length, &so[$lvalues]";
  137. $lvalues += $length;
  138. }
  139. $out .= "},\n";
  140. push(@out, $out);
  141. }
  142. # Finally ready to generate the output.
  143. print <<"EOF";
  144. /*
  145. * WARNING: do not edit!
  146. * Generated by crypto/objects/obj_dat.pl
  147. *
  148. * Copyright 1995-$YEAR The OpenSSL Project Authors. All Rights Reserved.
  149. * Licensed under the OpenSSL license (the "License"). You may not use
  150. * this file except in compliance with the License. You can obtain a copy
  151. * in the file LICENSE in the source distribution or at
  152. * https://www.openssl.org/source/license.html
  153. */
  154. EOF
  155. print "/* Serialized OID's */\n";
  156. printf "static const unsigned char so[%d] = {\n", $lvalues + 1;
  157. print @lvalues;
  158. print "};\n\n";
  159. printf "#define NUM_NID %d\n", $n;
  160. printf "static const ASN1_OBJECT nid_objs[NUM_NID] = {\n";
  161. print @out;
  162. print "};\n\n";
  163. {
  164. no warnings "uninitialized";
  165. @a = grep(defined $sn{$nid{$_}}, 0 .. $n);
  166. }
  167. printf "#define NUM_SN %d\n", $#a + 1;
  168. printf "static const unsigned int sn_objs[NUM_SN] = {\n";
  169. foreach (sort { $sn{$nid{$a}} cmp $sn{$nid{$b}} } @a) {
  170. printf " %4d, /* \"$sn{$nid{$_}}\" */\n", $_;
  171. }
  172. print "};\n\n";
  173. {
  174. no warnings "uninitialized";
  175. @a = grep(defined $ln{$nid{$_}}, 0 .. $n);
  176. }
  177. printf "#define NUM_LN %d\n", $#a + 1;
  178. printf "static const unsigned int ln_objs[NUM_LN] = {\n";
  179. foreach (sort { $ln{$nid{$a}} cmp $ln{$nid{$b}} } @a) {
  180. printf " %4d, /* \"$ln{$nid{$_}}\" */\n", $_;
  181. }
  182. print "};\n\n";
  183. {
  184. no warnings "uninitialized";
  185. @a = grep(defined $obj{$nid{$_}}, 0 .. $n);
  186. }
  187. printf "#define NUM_OBJ %d\n", $#a + 1;
  188. printf "static const unsigned int obj_objs[NUM_OBJ] = {\n";
  189. # Compare DER; prefer shorter; if some length, use the "smaller" encoding.
  190. sub obj_cmp
  191. {
  192. no warnings "uninitialized";
  193. my $A = $obj_len{$obj{$nid{$a}}};
  194. my $B = $obj_len{$obj{$nid{$b}}};
  195. my $r = $A - $B;
  196. return $r if $r != 0;
  197. $A = $obj_der{$obj{$nid{$a}}};
  198. $B = $obj_der{$obj{$nid{$b}}};
  199. return $A cmp $B;
  200. }
  201. foreach (sort obj_cmp @a) {
  202. my $m = $obj{$nid{$_}};
  203. my $v = $objd{$m};
  204. $v =~ s/L//g;
  205. $v =~ s/,/ /g;
  206. printf " %4d, /* %-32s %s */\n", $_, $m, $v;
  207. }
  208. print "};\n";