Debian Codegraph @

examples/x509decode

raw
1 #!/usr/bin/perl
2 # $Id: x509decode,v 1.1 2002/02/10 16:41:28 gbarr Exp $
3 # (c) 2001-2002 Norbert Klasen, DAASI International GmbH. All rights reserved.
4 # This package is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
6 #
7 # decode X.509 certificates
8 #
9 # varable naming
10 # Convert::ASN1 objects are prefixed with asn_
11 # variables holding binary DER content are prefixed with der_
12
13 use strict;
14
15 use Data::Dumper;
16 $Data::Dumper::Indent=1;
17 $Data::Dumper::Quotekeys=1;
18 $Data::Dumper::Useqq=1;
19
20 use Convert::ASN1 qw(:io :debug);
21
22 # parse ASN.1 desciptions
23 my $asn = Convert::ASN1->new;
24 $asn->prepare(<<ASN1) or die "prepare: ", $asn->error;
25 -- ASN.1 from RFC2459 and X.509(2001)
26 -- Adapted for use with Convert::ASN1
27
28 -- attribute data types --
29
30 Attribute ::= SEQUENCE {
31 type AttributeType,
32 values SET OF AttributeValue
33 -- at least one value is required --
34 }
35
36 AttributeType ::= OBJECT IDENTIFIER
37
38 AttributeValue ::= DirectoryString --ANY
39
40 AttributeTypeAndValue ::= SEQUENCE {
41 type AttributeType,
42 value AttributeValue
43 }
44
45
46 -- naming data types --
47
48 Name ::= CHOICE { -- only one possibility for now
49 rdnSequence RDNSequence
50 }
51
52 RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
53
54 DistinguishedName ::= RDNSequence
55
56 RelativeDistinguishedName ::=
57 SET OF AttributeTypeAndValue --SET SIZE (1 .. MAX) OF
58
59
60 -- Directory string type --
61
62 DirectoryString ::= CHOICE {
63 teletexString TeletexString, --(SIZE (1..MAX)),
64 printableString PrintableString, --(SIZE (1..MAX)),
65 bmpString BMPString, --(SIZE (1..MAX)),
66 universalString UniversalString, --(SIZE (1..MAX)),
67 utf8String UTF8String, --(SIZE (1..MAX)),
68 ia5String IA5String --added for EmailAddress
69 }
70
71
72 -- certificate and CRL specific structures begin here
73
74 Certificate ::= SEQUENCE {
75 tbsCertificate TBSCertificate,
76 signatureAlgorithm AlgorithmIdentifier,
77 signature BIT STRING
78 }
79
80 TBSCertificate ::= SEQUENCE {
81 version [0] EXPLICIT Version OPTIONAL, --DEFAULT v1
82 serialNumber CertificateSerialNumber,
83 signature AlgorithmIdentifier,
84 issuer Name,
85 validity Validity,
86 subject Name,
87 subjectPublicKeyInfo SubjectPublicKeyInfo,
88 issuerUniqueID [1] IMPLICIT UniqueIdentifier OPTIONAL,
89 -- If present, version shall be v2 or v3
90 subjectUniqueID [2] IMPLICIT UniqueIdentifier OPTIONAL,
91 -- If present, version shall be v2 or v3
92 extensions [3] EXPLICIT Extensions OPTIONAL
93 -- If present, version shall be v3
94 }
95
96 Version ::= INTEGER --{ v1(0), v2(1), v3(2) }
97
98 CertificateSerialNumber ::= INTEGER
99
100 Validity ::= SEQUENCE {
101 notBefore Time,
102 notAfter Time
103 }
104
105 Time ::= CHOICE {
106 utcTime UTCTime,
107 generalTime GeneralizedTime
108 }
109
110 UniqueIdentifier ::= BIT STRING
111
112 SubjectPublicKeyInfo ::= SEQUENCE {
113 algorithm AlgorithmIdentifier,
114 subjectPublicKey BIT STRING
115 }
116
117 Extensions ::= SEQUENCE OF Extension --SIZE (1..MAX) OF Extension
118
119 Extension ::= SEQUENCE {
120 extnID OBJECT IDENTIFIER,
121 critical BOOLEAN OPTIONAL, --DEFAULT FALSE,
122 extnValue OCTET STRING
123 }
124
125 AlgorithmIdentifier ::= SEQUENCE {
126 algorithm OBJECT IDENTIFIER,
127 parameters ANY OPTIONAL
128 }
129
130
131 --extensions
132
133 AuthorityKeyIdentifier ::= SEQUENCE {
134 keyIdentifier [0] KeyIdentifier OPTIONAL,
135 authorityCertIssuer [1] GeneralNames OPTIONAL,
136 authorityCertSerialNumber [2] CertificateSerialNumber OPTIONAL }
137 -- authorityCertIssuer and authorityCertSerialNumber shall both
138 -- be present or both be absent
139
140 KeyIdentifier ::= OCTET STRING
141
142 SubjectKeyIdentifier ::= KeyIdentifier
143
144
145 -- key usage extension OID and syntax
146 -- id-ce-keyUsage OBJECT IDENTIFIER ::= { id-ce 15 }
147
148 KeyUsage ::= BIT STRING --{
149 -- digitalSignature (0),
150 -- nonRepudiation (1),
151 -- keyEncipherment (2),
152 -- dataEncipherment (3),
153 -- keyAgreement (4),
154 -- keyCertSign (5),
155 -- cRLSign (6),
156 -- encipherOnly (7),
157 -- decipherOnly (8) }
158
159
160 -- private key usage period extension OID and syntax
161 -- id-ce-privateKeyUsagePeriod OBJECT IDENTIFIER ::= { id-ce 16 }
162
163 PrivateKeyUsagePeriod ::= SEQUENCE {
164 notBefore [0] GeneralizedTime OPTIONAL,
165 notAfter [1] GeneralizedTime OPTIONAL }
166 -- either notBefore or notAfter shall be present
167
168
169 -- certificate policies extension OID and syntax
170 -- id-ce-certificatePolicies OBJECT IDENTIFIER ::= { id-ce 32 }
171
172 CertificatePolicies ::= SEQUENCE OF PolicyInformation
173
174 PolicyInformation ::= SEQUENCE {
175 policyIdentifier CertPolicyId,
176 policyQualifiers SEQUENCE OF
177 PolicyQualifierInfo } --OPTIONAL }
178
179 CertPolicyId ::= OBJECT IDENTIFIER
180
181 PolicyQualifierInfo ::= SEQUENCE {
182 policyQualifierId PolicyQualifierId,
183 qualifier ANY } --DEFINED BY policyQualifierId }
184
185 -- Implementations that recognize additional policy qualifiers shall
186 -- augment the following definition for PolicyQualifierId
187
188 PolicyQualifierId ::=
189 OBJECT IDENTIFIER --( id-qt-cps | id-qt-unotice )
190
191 -- CPS pointer qualifier
192
193 CPSuri ::= IA5String
194
195 -- user notice qualifier
196
197 UserNotice ::= SEQUENCE {
198 noticeRef NoticeReference OPTIONAL,
199 explicitText DisplayText OPTIONAL}
200
201 NoticeReference ::= SEQUENCE {
202 organization DisplayText,
203 noticeNumbers SEQUENCE OF INTEGER }
204
205 DisplayText ::= CHOICE {
206 visibleString VisibleString ,
207 bmpString BMPString ,
208 utf8String UTF8String }
209
210
211 -- policy mapping extension OID and syntax
212 -- id-ce-policyMappings OBJECT IDENTIFIER ::= { id-ce 33 }
213
214 PolicyMappings ::= SEQUENCE OF SEQUENCE {
215 issuerDomainPolicy CertPolicyId,
216 subjectDomainPolicy CertPolicyId }
217
218
219 -- subject alternative name extension OID and syntax
220 -- id-ce-subjectAltName OBJECT IDENTIFIER ::= { id-ce 17 }
221
222 SubjectAltName ::= GeneralNames
223
224 GeneralNames ::= SEQUENCE OF GeneralName
225
226 GeneralName ::= CHOICE {
227 otherName [0] AnotherName,
228 rfc822Name [1] IA5String,
229 dNSName [2] IA5String,
230 x400Address [3] ANY, --ORAddress,
231 directoryName [4] Name,
232 ediPartyName [5] EDIPartyName,
233 uniformResourceIdentifier [6] IA5String,
234 iPAddress [7] OCTET STRING,
235 registeredID [8] OBJECT IDENTIFIER }
236
237 -- AnotherName replaces OTHER-NAME ::= TYPE-IDENTIFIER, as
238 -- TYPE-IDENTIFIER is not supported in the '88 ASN.1 syntax
239
240 AnotherName ::= SEQUENCE {
241 type OBJECT IDENTIFIER,
242 value [0] EXPLICIT ANY } --DEFINED BY type-id }
243
244 EDIPartyName ::= SEQUENCE {
245 nameAssigner [0] DirectoryString OPTIONAL,
246 partyName [1] DirectoryString }
247
248
249 -- issuer alternative name extension OID and syntax
250 -- id-ce-issuerAltName OBJECT IDENTIFIER ::= { id-ce 18 }
251
252 IssuerAltName ::= GeneralNames
253
254
255 -- id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::= { id-ce 9 }
256
257 SubjectDirectoryAttributes ::= SEQUENCE OF Attribute
258
259
260 -- basic constraints extension OID and syntax
261 -- id-ce-basicConstraints OBJECT IDENTIFIER ::= { id-ce 19 }
262
263 BasicConstraints ::= SEQUENCE {
264 cA BOOLEAN OPTIONAL, --DEFAULT FALSE,
265 pathLenConstraint INTEGER OPTIONAL }
266
267
268 -- name constraints extension OID and syntax
269 -- id-ce-nameConstraints OBJECT IDENTIFIER ::= { id-ce 30 }
270
271 NameConstraints ::= SEQUENCE {
272 permittedSubtrees [0] GeneralSubtrees OPTIONAL,
273 excludedSubtrees [1] GeneralSubtrees OPTIONAL }
274
275 GeneralSubtrees ::= SEQUENCE OF GeneralSubtree
276
277 GeneralSubtree ::= SEQUENCE {
278 base GeneralName,
279 minimum [0] BaseDistance OPTIONAL, --DEFAULT 0,
280 maximum [1] BaseDistance OPTIONAL }
281
282 BaseDistance ::= INTEGER
283
284
285 -- policy constraints extension OID and syntax
286 -- id-ce-policyConstraints OBJECT IDENTIFIER ::= { id-ce 36 }
287
288 PolicyConstraints ::= SEQUENCE {
289 requireExplicitPolicy [0] SkipCerts OPTIONAL,
290 inhibitPolicyMapping [1] SkipCerts OPTIONAL }
291
292 SkipCerts ::= INTEGER
293
294
295 -- CRL distribution points extension OID and syntax
296 -- id-ce-cRLDistributionPoints OBJECT IDENTIFIER ::= {id-ce 31}
297
298 cRLDistributionPoints ::= SEQUENCE OF DistributionPoint
299
300 DistributionPoint ::= SEQUENCE {
301 distributionPoint [0] DistributionPointName OPTIONAL,
302 reasons [1] ReasonFlags OPTIONAL,
303 cRLIssuer [2] GeneralNames OPTIONAL }
304
305 DistributionPointName ::= CHOICE {
306 fullName [0] GeneralNames,
307 nameRelativeToCRLIssuer [1] RelativeDistinguishedName }
308
309 ReasonFlags ::= BIT STRING --{
310 -- unused (0),
311 -- keyCompromise (1),
312 -- cACompromise (2),
313 -- affiliationChanged (3),
314 -- superseded (4),
315 -- cessationOfOperation (5),
316 -- certificateHold (6),
317 -- privilegeWithdrawn (7),
318 -- aACompromise (8) }
319
320
321 -- extended key usage extension OID and syntax
322 -- id-ce-extKeyUsage OBJECT IDENTIFIER ::= {id-ce 37}
323
324 ExtKeyUsageSyntax ::= SEQUENCE OF KeyPurposeId
325
326 KeyPurposeId ::= OBJECT IDENTIFIER
327
328 -- extended key purpose OIDs
329 -- id-kp-serverAuth OBJECT IDENTIFIER ::= { id-kp 1 }
330 -- id-kp-clientAuth OBJECT IDENTIFIER ::= { id-kp 2 }
331 -- id-kp-codeSigning OBJECT IDENTIFIER ::= { id-kp 3 }
332 -- id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 }
333 -- id-kp-ipsecEndSystem OBJECT IDENTIFIER ::= { id-kp 5 }
334 -- id-kp-ipsecTunnel OBJECT IDENTIFIER ::= { id-kp 6 }
335 -- id-kp-ipsecUser OBJECT IDENTIFIER ::= { id-kp 7 }
336 -- id-kp-timeStamping OBJECT IDENTIFIER ::= { id-kp 8 }
337 ASN1
338
339 # decoders for basic types
340 my $asn_BitString = Convert::ASN1->new();
341 $asn_BitString->prepare("bitString BIT STRING");
342
343 my $asn_OctetString = Convert::ASN1->new();
344 $asn_OctetString->prepare("octetString OCTET STRING");
345
346 # decoders for extensions
347 my %extnoid2asn = (
348 '2.5.29.9' => $asn->find('SubjectDirectoryAttributes'),
349 '2.5.29.14' => $asn_OctetString, #'SubjectKeyIdentifier',
350 '2.5.29.15' => $asn_BitString, #'keyUsage',
351 '2.5.29.16' => $asn->find('PrivateKeyUsagePeriod'),
352 '2.5.29.17' => $asn->find('SubjectAltName'),
353 '2.5.29.18' => $asn->find('IssuerAltName'),
354 '2.5.29.19' => $asn->find('BasicConstraints'),
355 # '2.5.29.20' => 'cRLNumber',
356 # '2.5.29.21' => 'cRLReasons',
357 # '2.5.29.23' => 'holdInstructionCode',
358 # '2.5.29.24' => 'invalidityDate',
359 # '2.5.29.27' => 'deltaCRLIndicator',
360 # '2.5.29.28' => 'issuingDistributionPoint',
361 # '2.5.29.29' => 'certificateIssuer',
362 '2.5.29.30' => $asn->find('NameConstraints'),
363 '2.5.29.31' => $asn->find('cRLDistributionPoints'),
364 '2.5.29.32' => $asn->find('CertificatePolicies'),
365 '2.5.29.33' => $asn->find('PolicyMappings'),
366 '2.5.29.35' => $asn->find('AuthorityKeyIdentifier'),
367 '2.5.29.36' => $asn->find('PolicyConstraints'),
368 '2.5.29.37' => $asn->find('ExtKeyUsageSyntax'),
369 # '2.5.29.40' => 'cRLStreamIdentifier',
370 # '2.5.29.44' => 'cRLScope',
371 # '2.5.29.45' => 'statusReferrals',
372 # '2.5.29.46' => 'freshestCRL',
373 # '2.5.29.47' => 'orderedList',
374 # '2.5.29.51' => 'baseUpdateTime',
375 # '2.5.29.53' => 'deltaInfo',
376 # '2.5.29.54' => 'inhibitAnyPolicy',
377 # netscape-cert-extensions
378 '2.16.840.1.113730.1.1' => $asn_BitString, # netscape-cert-type
379 '2.16.840.1.113730.1.2' => $asn->find('DirectoryString'), # netscape-base-url
380 '2.16.840.1.113730.1.3' => $asn->find('DirectoryString'), # netscape-revocation-url
381 '2.16.840.1.113730.1.4' => $asn->find('DirectoryString'), # netscape-ca-revocation-url
382 '2.16.840.1.113730.1.7' => $asn->find('DirectoryString'), # netscape-cert-renewal-url
383 '2.16.840.1.113730.1.8' => $asn->find('DirectoryString'), # netscape-ca-policy-url
384 '2.16.840.1.113730.1.12' => $asn->find('DirectoryString'), # netscape-ssl-server-name
385 '2.16.840.1.113730.1.13' => $asn->find('DirectoryString'), # netscape-comment
386 );
387
388 my $asn_cert = $asn->find('Certificate');
389
390 while ( my $filename = shift ) {
391 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
392 $atime,$mtime,$ctime,$blksize,$blocks) = stat $filename;
393 open FILE, "<$filename" or die "no such file";
394 binmode FILE;
395 my $der_cert;
396 read FILE, $der_cert, $size;
397 close FILE;
398 decodeCert( $der_cert );
399 }
400
401 sub decodeCert {
402 my $der_cert = shift;
403 #asn_dump( $der_cert );
404
405 my $cert = $asn_cert->decode($der_cert) or die $asn_cert->error;
406
407 #extensions
408 foreach my $extension ( @{$cert->{'tbsCertificate'}->{'extensions'}} ) {
409 #print "extension: ", $oid2extension{$extension->{'extnID'}}, "\n";
410 if ( exists $extnoid2asn{$extension->{'extnID'}} ) {
411 $extension->{'extnValue'} = ($extnoid2asn{$extension->{'extnID'}})->decode( $extension->{'extnValue'} );
412 } else {
413 print STDERR "unknown ", $extension->{'critical'} ? "critical " : "", "extension: ", $extension->{'extnID'}, "\n";
414 asn_dump( $extension->{'extnValue'} );
415 }
416 }
417
418 print Dumper( $cert );
419 }