obj_dat.pl 5.9 KB

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