3 # Generate an identifier from an X.509 certificate that can be placed in a
4 # module signature to indentify the key to use.
8 # ./scripts/x509keyid <x509-cert> <signer's-name> <key-id>
10 # We read the DER-encoded X509 certificate and parse it to extract the Subject
11 # name and Subject Key Identifier. The provide the data we need to build the
12 # certificate identifier.
14 # The signer's name part of the identifier is fabricated from the commonName,
15 # the organizationName or the emailAddress components of the X.509 subject
16 # name and written to the second named file.
18 # The subject key ID to select which of that signer's certificates we're
19 # intending to use to sign the module is written to the third named file.
25 die "Need three filenames\n" if ($#ARGV != 2);
29 open(FD, "<$src") || die $src;
33 read(FD, $raw_data, $st[7]) || die $src;
45 my $BIT_STRING = 0x03;
46 my $OCTET_STRING = 0x04;
49 my $UTF8String = 0x0c;
53 my $GeneralizedTime = 0x18;
56 pack("CCC", 85, 4, 3) => "commonName",
57 pack("CCC", 85, 4, 6) => "countryName",
58 pack("CCC", 85, 4, 10) => "organizationName",
59 pack("CCC", 85, 4, 11) => "organizationUnitName",
60 pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 1) => "rsaEncryption",
61 pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 1, 5) => "sha1WithRSAEncryption",
62 pack("CCCCCCCCC", 42, 134, 72, 134, 247, 13, 1, 9, 1) => "emailAddress",
63 pack("CCC", 85, 29, 35) => "authorityKeyIdentifier",
64 pack("CCC", 85, 29, 14) => "subjectKeyIdentifier",
65 pack("CCC", 85, 29, 19) => "basicConstraints"
68 ###############################################################################
70 # Extract an ASN.1 element from a string and return information about it.
72 ###############################################################################
75 my ($cursor, $expected_tag, $optional) = @_;
78 if ($cursor->[1] == 0 && $optional);
80 die $src, ": ", $cursor->[0], ": ASN.1 data underrun (elem ", $cursor->[1], ")\n"
81 if ($cursor->[1] < 2);
83 my ($tag, $len) = unpack("CC", substr(${$cursor->[2]}, $cursor->[0], 2));
85 if ($expected_tag != -1 && $tag != $expected_tag) {
88 die $src, ": ", $cursor->[0], ": ASN.1 unexpected tag (", $tag,
89 " not ", $expected_tag, ")\n";
95 die $src, ": ", $cursor->[0], ": ASN.1 long tag\n"
96 if (($tag & 0x1f) == 0x1f);
97 die $src, ": ", $cursor->[0], ": ASN.1 indefinite length\n"
102 die $src, ": ", $cursor->[0], ": ASN.1 data underrun (len len $l)\n"
103 if ($cursor->[1] < $l);
106 $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1));
108 $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0], 2));
110 $len = unpack("C", substr(${$cursor->[2]}, $cursor->[0], 1)) << 16;
111 $len = unpack("n", substr(${$cursor->[2]}, $cursor->[0] + 1, 2));
113 $len = unpack("N", substr(${$cursor->[2]}, $cursor->[0], 4));
115 die $src, ": ", $cursor->[0], ": ASN.1 element too long (", $l, ")\n";
122 die $src, ": ", $cursor->[0], ": ASN.1 data underrun (", $len, ")\n"
123 if ($cursor->[1] < $len);
125 my $ret = [ $tag, [ $cursor->[0], $len, $cursor->[2] ] ];
126 $cursor->[0] += $len;
127 $cursor->[1] -= $len;
132 ###############################################################################
134 # Retrieve the data referred to by a cursor
136 ###############################################################################
140 my ($offset, $len, $data) = @$cursor;
141 return substr($$data, $offset, $len);
144 ###############################################################################
146 # Roughly parse the X.509 certificate
148 ###############################################################################
149 my $cursor = [ 0, length($raw_data), \$raw_data ];
151 my $cert = asn1_extract($cursor, $UNIV | $CONS | $SEQUENCE);
152 my $tbs = asn1_extract($cert->[1], $UNIV | $CONS | $SEQUENCE);
153 my $version = asn1_extract($tbs->[1], $CONT | $CONS | 0, 1);
154 my $serial_number = asn1_extract($tbs->[1], $UNIV | $INTEGER);
155 my $sig_type = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
156 my $issuer = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
157 my $validity = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
158 my $subject = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
159 my $key = asn1_extract($tbs->[1], $UNIV | $CONS | $SEQUENCE);
160 my $issuer_uid = asn1_extract($tbs->[1], $CONT | $CONS | 1, 1);
161 my $subject_uid = asn1_extract($tbs->[1], $CONT | $CONS | 2, 1);
162 my $extension_list = asn1_extract($tbs->[1], $CONT | $CONS | 3, 1);
164 my $subject_key_id = ();
165 my $authority_key_id = ();
168 # Parse the extension list
170 if ($extension_list->[0] != -1) {
171 my $extensions = asn1_extract($extension_list->[1], $UNIV | $CONS | $SEQUENCE);
173 while ($extensions->[1]->[1] > 0) {
174 my $ext = asn1_extract($extensions->[1], $UNIV | $CONS | $SEQUENCE);
175 my $x_oid = asn1_extract($ext->[1], $UNIV | $OBJ_ID);
176 my $x_crit = asn1_extract($ext->[1], $UNIV | $BOOLEAN, 1);
177 my $x_val = asn1_extract($ext->[1], $UNIV | $OCTET_STRING);
179 my $raw_oid = asn1_retrieve($x_oid->[1]);
180 next if (!exists($OIDs{$raw_oid}));
181 my $x_type = $OIDs{$raw_oid};
183 my $raw_value = asn1_retrieve($x_val->[1]);
185 if ($x_type eq "subjectKeyIdentifier") {
186 my $vcursor = [ 0, length($raw_value), \$raw_value ];
188 $subject_key_id = asn1_extract($vcursor, $UNIV | $OCTET_STRING);
193 ###############################################################################
195 # Determine what we're going to use as the signer's name. In order of
196 # preference, take one of: commonName, organizationName or emailAddress.
198 ###############################################################################
203 while ($subject->[1]->[1] > 0) {
204 my $rdn = asn1_extract($subject->[1], $UNIV | $CONS | $SET);
205 my $attr = asn1_extract($rdn->[1], $UNIV | $CONS | $SEQUENCE);
206 my $n_oid = asn1_extract($attr->[1], $UNIV | $OBJ_ID);
207 my $n_val = asn1_extract($attr->[1], -1);
209 my $raw_oid = asn1_retrieve($n_oid->[1]);
210 next if (!exists($OIDs{$raw_oid}));
211 my $n_type = $OIDs{$raw_oid};
213 my $raw_value = asn1_retrieve($n_val->[1]);
215 if ($n_type eq "organizationName") {
217 } elsif ($n_type eq "commonName") {
219 } elsif ($n_type eq "emailAddress") {
224 my $id_name = $email;
227 # Don't use the organizationName if the commonName repeats it
228 if (length($org) <= length($cn) &&
229 substr($cn, 0, length($org)) eq $org) {
234 # Or a signifcant chunk of it
235 if (length($org) >= 7 &&
237 substr($cn, 0, 7) eq substr($org, 0, 7)) {
242 $id_name = $org . ": " . $cn;
251 ###############################################################################
253 # Output the signer's name and the key identifier that we're going to include
254 # in module signatures.
256 ###############################################################################
257 die $src, ": ", "X.509: Couldn't find the Subject Key Identifier extension\n"
258 if (!$subject_key_id);
260 my $id_key_id = asn1_retrieve($subject_key_id->[1]);
262 open(OUTFD, ">$ARGV[1]") || die $ARGV[1];
263 print OUTFD $id_name;
264 close OUTFD || die $ARGV[1];
266 open(OUTFD, ">$ARGV[2]") || die $ARGV[2];
267 print OUTFD $id_key_id;
268 close OUTFD || die $ARGV[2];