RSA, DSA, DH: Allow some given input to be NULL on already initialised keys
[openssl.git] / crypto / objects / objects.pl
1 #! /usr/bin/env perl
2 # Copyright 2000-2016 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
9 open (NUMIN,"$ARGV[1]") || die "Can't open number file $ARGV[1]";
10 $max_nid=0;
11 $o=0;
12 while(<NUMIN>)
13         {
14         s|\R$||;
15         $o++;
16         s/#.*$//;
17         next if /^\s*$/;
18         $_ = 'X'.$_;
19         ($Cname,$mynum) = split;
20         $Cname =~ s/^X//;
21         if (defined($nidn{$mynum}))
22                 { die "$ARGV[1]:$o:There's already an object with NID ",$mynum," on line ",$order{$mynum},"\n"; }
23         if (defined($nid{$Cname}))
24                 { die "$ARGV[1]:$o:There's already an object with name ",$Cname," on line ",$order{$nid{$Cname}},"\n"; }
25         $nid{$Cname} = $mynum;
26         $nidn{$mynum} = $Cname;
27         $order{$mynum} = $o;
28         $max_nid = $mynum if $mynum > $max_nid;
29         }
30 close NUMIN;
31
32 open (IN,"$ARGV[0]") || die "Can't open input file $ARGV[0]";
33 $Cname="";
34 $o=0;
35 while (<IN>)
36         {
37         s|\R$||;
38         $o++;
39         if (/^!module\s+(.*)$/)
40                 {
41                 $module = $1."-";
42                 $module =~ s/\./_/g;
43                 $module =~ s/-/_/g;
44                 }
45         if (/^!global$/)
46                 { $module = ""; }
47         if (/^!Cname\s+(.*)$/)
48                 { $Cname = $1; }
49         if (/^!Alias\s+(.+?)\s+(.*)$/)
50                 {
51                 $Cname = $module.$1;
52                 $myoid = $2;
53                 $myoid = &process_oid($myoid);
54                 $Cname =~ s/-/_/g;
55                 $ordern{$o} = $Cname;
56                 $order{$Cname} = $o;
57                 $obj{$Cname} = $myoid;
58                 $_ = "";
59                 $Cname = "";
60                 }
61         s/!.*$//;
62         s/#.*$//;
63         next if /^\s*$/;
64         ($myoid,$mysn,$myln) = split ':';
65         $mysn =~ s/^\s*//;
66         $mysn =~ s/\s*$//;
67         $myln =~ s/^\s*//;
68         $myln =~ s/\s*$//;
69         $myoid =~ s/^\s*//;
70         $myoid =~ s/\s*$//;
71         if ($myoid ne "")
72                 {
73                 $myoid = &process_oid($myoid);
74                 }
75
76         if ($Cname eq "" && ($myln =~ /^[_A-Za-z][\w.-]*$/ ))
77                 {
78                 $Cname = $myln;
79                 $Cname =~ s/\./_/g;
80                 $Cname =~ s/-/_/g;
81                 if ($Cname ne "" && defined($ln{$module.$Cname}))
82                         { die "objects.txt:$o:There's already an object with long name ",$ln{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
83                 }
84         if ($Cname eq "")
85                 {
86                 $Cname = $mysn;
87                 $Cname =~ s/-/_/g;
88                 if ($Cname ne "" && defined($sn{$module.$Cname}))
89                         { die "objects.txt:$o:There's already an object with short name ",$sn{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
90                 }
91         if ($Cname eq "")
92                 {
93                 $Cname = $myln;
94                 $Cname =~ s/-/_/g;
95                 $Cname =~ s/\./_/g;
96                 $Cname =~ s/ /_/g;
97                 if ($Cname ne "" && defined($ln{$module.$Cname}))
98                         { die "objects.txt:$o:There's already an object with long name ",$ln{$module.$Cname}," on line ",$order{$module.$Cname},"\n"; }
99                 }
100         $Cname =~ s/\./_/g;
101         $Cname =~ s/-/_/g;
102         $Cname = $module.$Cname;
103         $ordern{$o} = $Cname;
104         $order{$Cname} = $o;
105         $sn{$Cname} = $mysn;
106         $ln{$Cname} = $myln;
107         $obj{$Cname} = $myoid;
108         if (!defined($nid{$Cname}))
109                 {
110                 $max_nid++;
111                 $nid{$Cname} = $max_nid;
112                 $nidn{$max_nid} = $Cname;
113 print STDERR "Added OID $Cname\n";
114                 }
115         $Cname="";
116         }
117 close IN;
118
119 open (NUMOUT,">$ARGV[1]") || die "Can't open output file $ARGV[1]";
120 foreach (sort { $a <=> $b } keys %nidn)
121         {
122         print NUMOUT $nidn{$_},"\t\t",$_,"\n";
123         }
124 close NUMOUT;
125
126 open (OUT,">$ARGV[2]") || die "Can't open output file $ARGV[2]";
127 print OUT <<'EOF';
128 /*
129  * WARNING: do not edit!
130  * Generated by crypto/objects/objects.pl
131  *
132  * Copyright 2000-2016 The OpenSSL Project Authors. All Rights Reserved.
133  * Licensed under the OpenSSL license (the "License").  You may not use
134  * this file except in compliance with the License.  You can obtain a copy
135  * in the file LICENSE in the source distribution or at
136  * https://www.openssl.org/source/license.html
137  */
138
139 #define SN_undef                        "UNDEF"
140 #define LN_undef                        "undefined"
141 #define NID_undef                       0
142 #define OBJ_undef                       0L
143 EOF
144
145 sub expand
146         {
147         my $string = shift;
148
149         1 while $string =~ s/\t+/' ' x (length($&) * 8 - length($`) % 8)/e;
150
151         return $string;
152         }
153
154 foreach (sort { $a <=> $b } keys %ordern)
155         {
156         $Cname=$ordern{$_};
157         print OUT "\n";
158         print OUT expand("#define SN_$Cname\t\t\"$sn{$Cname}\"\n") if $sn{$Cname} ne "";
159         print OUT expand("#define LN_$Cname\t\t\"$ln{$Cname}\"\n") if $ln{$Cname} ne "";
160         print OUT expand("#define NID_$Cname\t\t$nid{$Cname}\n") if $nid{$Cname} ne "";
161         print OUT expand("#define OBJ_$Cname\t\t$obj{$Cname}\n") if $obj{$Cname} ne "";
162         }
163
164 close OUT;
165
166 sub process_oid
167         {
168         local($oid)=@_;
169         local(@a,$oid_pref);
170
171         @a = split(/\s+/,$myoid);
172         $pref_oid = "";
173         $pref_sep = "";
174         if (!($a[0] =~ /^[0-9]+$/))
175                 {
176                 $a[0] =~ s/-/_/g;
177                 if (!defined($obj{$a[0]}))
178                         { die "$ARGV[0]:$o:Undefined identifier ",$a[0],"\n"; }
179                 $pref_oid = "OBJ_" . $a[0];
180                 $pref_sep = ",";
181                 shift @a;
182                 }
183         $oids = join('L,',@a) . "L";
184         if ($oids ne "L")
185                 {
186                 $oids = $pref_oid . $pref_sep . $oids;
187                 }
188         else
189                 {
190                 $oids = $pref_oid;
191                 }
192         return($oids);
193         }