home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / share / perl5 / Crypt / X509.pm
Encoding:
Perl POD Document  |  2009-04-23  |  44.3 KB  |  1,578 lines

  1. package Crypt::X509;
  2. use Carp;
  3. use strict;
  4. use warnings;
  5. use Convert::ASN1 qw(:io :debug);
  6. require Exporter;
  7. our @ISA         = qw(Exporter);
  8. our %EXPORT_TAGS = ( 'all' => [qw()] );
  9. our @EXPORT_OK   = ( @{ $EXPORT_TAGS{'all'} } );
  10. our @EXPORT      = qw(error new not_before not_after serial);
  11. our $VERSION     = '0.40';
  12. my $parser = undef;
  13. my $asn    = undef;
  14. my $error  = undef;
  15. my %oid2enchash = (
  16.                     '1.2.840.113549.1.1.1' => { 'enc' => 'RSA' },
  17.                     '1.2.840.113549.1.1.2' => { 'enc' => 'RSA', 'hash' => 'MD2' },
  18.                     '1.2.840.113549.1.1.3' => { 'enc' => 'RSA', 'hash' => 'MD4' },
  19.                     '1.2.840.113549.1.1.4' => { 'enc' => 'RSA', 'hash' => 'MD5' },
  20.                     '1.2.840.113549.1.1.5' => { 'enc' => 'RSA', 'hash' => 'SHA1' },
  21.                     '1.2.840.113549.1.1.6' => { 'enc' => 'OAEP' }
  22. );
  23. my %oid2attr = (
  24.                  "2.5.4.3"                    => "CN",
  25.                  "2.5.4.6"                    => "C",
  26.                  "2.5.4.7"                    => "l",
  27.                  "2.5.4.8"                    => "S",
  28.                  "2.5.4.10"                   => "O",
  29.                  "2.5.4.11"                   => "OU",
  30.                  "1.2.840.113549.1.9.1"       => "E",
  31.                  "0.9.2342.19200300.100.1.1"  => "UID",
  32.                  "0.9.2342.19200300.100.1.25" => "DC",
  33.                  "0.2.262.1.10.7.20"          => "nameDistinguisher"
  34. );
  35.  
  36. =head1 NAME
  37.  
  38. Crypt::X509 - Parse a X.509 certificate
  39.  
  40. =head1 SYNOPSIS
  41.  
  42.  use Crypt::X509;
  43.  
  44.  $decoded = Crypt::X509->new( cert => $cert );
  45.  
  46.  $subject_email    = $decoded->subject_email;
  47.  print "do not use after: ".gmtime($decoded->not_after)." GMT\n";
  48.  
  49. =head1 REQUIRES
  50.  
  51. Convert::ASN1
  52.  
  53. =head1 DESCRIPTION
  54.  
  55. B<Crypt::X509> parses X.509 certificates. Methods are provided for accessing most
  56. certificate elements.
  57.  
  58. It is based on the generic ASN.1 module by Graham Barr, on the F<x509decode>
  59. example by Norbert Klasen and contributions on the perl-ldap-dev-Mailinglist
  60. by Chriss Ridd.
  61.  
  62. =head1 CONSTRUCTOR
  63.  
  64. =head2 new ( OPTIONS )
  65.  
  66. Creates and returns a parsed X.509 certificate hash, containing the parsed
  67. contents. The data is organised as specified in RFC 2459.
  68. By default only the first ASN.1 Layer is decoded. Nested decoding 
  69. is done automagically through the data access methods.
  70.  
  71. =over 4
  72.  
  73. =item cert =E<gt> $certificate
  74.  
  75. A variable containing the DER formatted certificate to be parsed 
  76. (eg. as stored in C<usercertificate;binary> attribute in an
  77. LDAP-directory).
  78.  
  79. =back
  80.  
  81.   use Crypt::X509;
  82.   use Data::Dumper;
  83.   
  84.   $decoded= Crypt::X509->new(cert => $cert);
  85.   
  86.   print Dumper($decoded);
  87.  
  88. =cut back
  89.  
  90. sub new {
  91.     my ( $class, %args ) = @_;
  92.     if ( !defined($parser) ) {
  93.         $parser = _init();
  94.     }
  95.     my $self = $parser->decode( $args{'cert'} );
  96.     $self->{"_error"} = $parser->error;
  97.     bless( $self, $class );
  98.     return $self;
  99. }
  100.  
  101. =head1 METHODS
  102.  
  103. =head2 error
  104.  
  105. Returns the last error from parsing, C<undef> when no error occured. 
  106. This error is updated on deeper parsing with the data access methods.
  107.  
  108.  
  109.   $decoded= Crypt::X509->new(cert => $cert);
  110.   if ($decoded->error) {
  111.     warn "Error on parsing Certificate:".$decoded->error;
  112.   }
  113.  
  114. =cut back
  115.  
  116. sub error {
  117.     my $self = shift;
  118.     return $self->{"_error"};
  119. }
  120.  
  121. =head1 DATA ACCESS METHODS
  122.  
  123. You can access all parsed data directly from the returned hash. For convenience
  124. the following methods have been implemented to give quick access to the most-used
  125. certificate attributes.
  126.  
  127. =head2 version 
  128.  
  129. Returns the certificate's version as an integer.  NOTE that version is defined as 
  130. an Integer where 0 = v1, 1 = v2, and 2 = v3.
  131.  
  132. =cut back
  133.  
  134. sub version {
  135.     my $self = shift;
  136.     return $self->{tbsCertificate}{version};
  137. }
  138.  
  139. =head2 version_string
  140.  
  141. Returns the certificate's version as a string value. 
  142.  
  143. =cut back
  144.  
  145. sub version_string {
  146.     my $self = shift;
  147.     my $v    = $self->{tbsCertificate}{version};
  148.     return "v1" if $v == 0;
  149.     return "v2" if $v == 1;
  150.     return "v3" if $v == 2;
  151. }
  152.  
  153. =head2 serial
  154.  
  155. returns the serial number (integer or Math::BigInt Object, that gets automagic
  156. evaluated in scalar context) from the certificate
  157.  
  158.  
  159.   $decoded= Crypt::X509->new(cert => $cert);
  160.   print "Certificate has serial number:".$decoded->serial."\n";  
  161.  
  162. =cut back
  163.  
  164. sub serial {
  165.     my $self = shift;
  166.     return $self->{tbsCertificate}{serialNumber};
  167. }
  168.  
  169. =head2 not_before
  170.  
  171. returns the GMT-timestamp of the certificate's beginning date of validity.
  172. If the Certificate holds this Entry in utcTime, it is guaranteed by the
  173. RFC to been correct.
  174.  
  175. As utcTime is limited to 32-bit values (like unix-timestamps) newer certificates
  176. hold the timesamps as "generalTime"-entries. B<The contents of "generalTime"-entries
  177. are not well defined in the RFC and
  178. are returned by this module unmodified>, if no utcTime-entry is found. 
  179.  
  180.  
  181.   $decoded= Crypt::X509->new(cert => $cert);
  182.   if ($decoded->notBefore < time()) {
  183.     warn "Certificate: not yet valid!";
  184.   }
  185.  
  186. =cut back
  187.  
  188. sub not_before {
  189.     my $self = shift;
  190.     if ( $self->{tbsCertificate}{validity}{notBefore}{utcTime} ) {
  191.         return $self->{tbsCertificate}{validity}{notBefore}{utcTime};
  192.     } elsif ( $self->{tbsCertificate}{validity}{notBefore}{generalTime} ) {
  193.         return $self->{tbsCertificate}{validity}{notBefore}{generalTime};
  194.     } else {
  195.         return undef;
  196.     }
  197. }
  198.  
  199. =head2 not_after
  200.  
  201. returns the GMT-timestamp of the certificate's ending date of validity.
  202. If the Certificate holds this Entry in utcTime, it is guaranteed by the
  203. RFC to been correct.
  204.  
  205. As utcTime is limited to 32-bit values (like unix-timestamps) newer certificates
  206. hold the timesamps as "generalTime"-entries. B<The contents of "generalTime"-entries
  207. are not well defined in the RFC and
  208. are returned by this module unmodified>, if no utcTime-entry is found. 
  209.  
  210.  
  211.   $decoded= Crypt::X509->new(cert => $cert);
  212.   print "Certificate expires on ".gmtime($decoded->not_after)." GMT\n";
  213.  
  214. =cut back
  215.  
  216. sub not_after {
  217.     my $self = shift;
  218.     if ( $self->{tbsCertificate}{validity}{notAfter}{utcTime} ) {
  219.         return $self->{tbsCertificate}{validity}{notAfter}{utcTime};
  220.     } elsif ( $self->{tbsCertificate}{validity}{notAfter}{generalTime} ) {
  221.         return $self->{tbsCertificate}{validity}{notAfter}{generalTime};
  222.     } else {
  223.         return undef;
  224.     }
  225. }
  226.  
  227. =head2 signature
  228.  
  229. Return's the certificate's signature in binary DER format.
  230.  
  231. =cut back
  232.  
  233. sub signature {
  234.     my $self = shift;
  235.     return $self->{signature}[0];
  236. }
  237.  
  238. =head2 pubkey
  239.  
  240. Returns the certificate's public key in binary DER format.
  241.  
  242. =cut back
  243.  
  244. sub pubkey {
  245.     my $self = shift;
  246.     return $self->{tbsCertificate}{subjectPublicKeyInfo}{subjectPublicKey}[0];
  247. }
  248.  
  249. =head2 pubkey_size
  250.  
  251. Returns the certificate's public key size.
  252.  
  253. =cut back
  254.  
  255. sub pubkey_size {
  256.     my $self = shift;
  257.     return $self->{tbsCertificate}{subjectPublicKeyInfo}{subjectPublicKey}[1];
  258. }
  259.  
  260. =head2 pubkey_algorithm
  261.  
  262. Returns the algorithm as OID string which the public key was created with.
  263.  
  264. =cut back
  265.  
  266. sub pubkey_algorithm {
  267.     my $self = shift;
  268.     return $self->{tbsCertificate}{subjectPublicKeyInfo}{algorithm}{algorithm};
  269. }
  270.  
  271. =head2 PubKeyAlg
  272.  
  273. returns the subject public key encryption algorithm (e.g. 'RSA') as string.
  274.  
  275.   $decoded= Crypt::X509->new(cert => $cert);
  276.   print "Certificate public key is encrypted with:".$decoded->PubKeyAlg."\n";
  277.   
  278.   Example Output: Certificate public key is encrypted with: RSA
  279.   
  280. =cut back
  281.  
  282. sub PubKeyAlg {
  283.     my $self = shift;
  284.     return $oid2enchash{ $self->{tbsCertificate}{subjectPublicKeyInfo}{algorithm}{algorithm} }->{'enc'};
  285. }
  286.  
  287. =head2 sig_algorithm
  288.  
  289. Returns the certificate's signature algorithm as OID string
  290.  
  291.   $decoded= Crypt::X509->new(cert => $cert);
  292.   print "Certificate signature is encrypted with:".$decoded->sig_algorithm."\n";>
  293.   
  294.   Example Output: Certificate signature is encrypted with: 1.2.840.113549.1.1.5
  295.  
  296. =cut back
  297.  
  298. sub sig_algorithm {
  299.     my $self = shift;
  300.     return $self->{tbsCertificate}{signature}{algorithm};
  301. }
  302.  
  303. =head2 SigEncAlg
  304.  
  305. returns the signature encryption algorithm (e.g. 'RSA') as string.
  306.  
  307.   $decoded= Crypt::X509->new(cert => $cert);
  308.   print "Certificate signature is encrypted with:".$decoded->SigEncAlg."\n";
  309.   
  310.   Example Output: Certificate signature is encrypted with: RSA
  311.  
  312. =cut back
  313.  
  314. sub SigEncAlg {
  315.     my $self = shift;
  316.     return $oid2enchash{ $self->{'signatureAlgorithm'}->{'algorithm'} }->{'enc'};
  317. }
  318.  
  319. =head2 SigHashAlg
  320.  
  321. returns the signature hashing algorithm (e.g. 'SHA1') as string.
  322.  
  323.   $decoded= Crypt::X509->new(cert => $cert);
  324.   print "Certificate signature is hashed with:".$decoded->SigHashAlg."\n";
  325.  
  326.   Example Output: Certificate signature is encrypted with: SHA1
  327.  
  328. =cut back
  329.  
  330. sub SigHashAlg {
  331.     my $self = shift;
  332.     return $oid2enchash{ $self->{'signatureAlgorithm'}->{'algorithm'} }->{'hash'};
  333. }
  334. #########################################################################
  335. # accessors - subject
  336. #########################################################################
  337.  
  338. =head2 Subject
  339.  
  340. returns a pointer to an array of strings containing subject nameparts of the
  341. certificate. Attributenames for the most common Attributes are translated 
  342. from the OID-Numbers, unknown numbers are output verbatim.
  343.  
  344.   $decoded= Convert::ASN1::X509->new($cert);
  345.   print "DN for this Certificate is:".join(',',@{$decoded->Subject})."\n";
  346.  
  347. =cut back
  348.  
  349. sub Subject {
  350.     my $self = shift;
  351.     my ( $i, $type );
  352.     my $subjrdn = $self->{'tbsCertificate'}->{'subject'}->{'rdnSequence'};
  353.     $self->{'tbsCertificate'}->{'subject'}->{'dn'} = [];
  354.     my $subjdn = $self->{'tbsCertificate'}->{'subject'}->{'dn'};
  355.     foreach my $subj ( @{$subjrdn} ) {
  356.         foreach my $i ( @{$subj} ) {
  357.             if ( $oid2attr{ $i->{'type'} } ) {
  358.                 $type = $oid2attr{ $i->{'type'} };
  359.             } else {
  360.                 $type = $i->{'type'};
  361.             }
  362.             my @key = keys( %{ $i->{'value'} } );
  363.             push @{$subjdn}, $type . "=" . $i->{'value'}->{ $key[0] };
  364.         }
  365.     }
  366.     return $subjdn;
  367. }
  368.  
  369. sub _subject_part {
  370.     my $self    = shift;
  371.     my $oid     = shift;
  372.     my $subjrdn = $self->{'tbsCertificate'}->{'subject'}->{'rdnSequence'};
  373.     foreach my $subj ( @{$subjrdn} ) {
  374.         foreach my $i ( @{$subj} ) {
  375.             if ( $i->{'type'} eq $oid ) {
  376.                 my @key = keys( %{ $i->{'value'} } );
  377.                 return $i->{'value'}->{ $key[0] };
  378.             }
  379.         }
  380.     }
  381.     return undef;
  382. }
  383.  
  384. =head2 subject_country 
  385.  
  386. Returns the string value for subject's country (= the value with the
  387.  OID 2.5.4.6 or in DN Syntax everything after C<C=>).
  388. Only the first entry is returned. C<undef> if subject contains no country attribute.
  389.  
  390. =cut back
  391.  
  392. sub subject_country {
  393.     my $self = shift;
  394.     return _subject_part( $self, '2.5.4.6' );
  395. }
  396.  
  397. =head2 subject_state
  398.  
  399. Returns the string value for subject's state or province (= the value with the 
  400. OID 2.5.4.8 or in DN Syntax everything after C<S=>).
  401. Only the first entry is returned. C<undef> if subject contains no state attribute.
  402.  
  403. =cut back
  404.  
  405. sub subject_state {
  406.     my $self = shift;
  407.     return _subject_part( $self, '2.5.4.8' );
  408. }
  409.  
  410. =head2 subject_org
  411.  
  412. Returns the string value for subject's organization (= the value with the
  413. OID 2.5.4.10 or in DN Syntax everything after C<O=>).
  414. Only the first entry is returned. C<undef> if subject contains no organization attribute.
  415.  
  416. =cut back
  417.  
  418. sub subject_org {
  419.     my $self = shift;
  420.     return _subject_part( $self, '2.5.4.10' );
  421. }
  422.  
  423. =head2 subject_ou
  424.  
  425. Returns the string value for subject's organizational unit (= the value with the
  426. OID 2.5.4.11 or in DN Syntax everything after C<OU=>).
  427. Only the first entry is returned. C<undef> if subject contains no organization attribute.
  428.  
  429. =cut back
  430.  
  431. sub subject_ou {
  432.     my $self = shift;
  433.     return _subject_part( $self, '2.5.4.11' );
  434. }
  435.  
  436. =head2 subject_cn 
  437.  
  438. Returns the string value for subject's common name (= the value with the
  439. OID 2.5.4.3 or in DN Syntax everything after C<CN=>).
  440. Only the first entry is returned. C<undef> if subject contains no common name attribute.
  441.  
  442. =cut back
  443.  
  444. sub subject_cn {
  445.     my $self = shift;
  446.     return _subject_part( $self, '2.5.4.3' );
  447. }
  448.  
  449. =head2 subject_email
  450.  
  451. Returns the string value for subject's email address (= the value with the
  452. OID 1.2.840.113549.1.9.1 or in DN Syntax everything after C<E=>).
  453. Only the first entry is returned. C<undef> if subject contains no email attribute.
  454.  
  455. =cut back
  456.  
  457. sub subject_email {
  458.     my $self = shift;
  459.     return _subject_part( $self, '1.2.840.113549.1.9.1' );
  460. }
  461. #########################################################################
  462. # accessors - issuer
  463. #########################################################################
  464.  
  465. =head2 Issuer
  466.  
  467. returns a pointer to an array of strings building the DN of the certificate
  468. issuer (= the DN of the CA). Attributenames for the most common Attributes
  469. are translated from the OID-Numbers, unknown numbers are output verbatim.
  470.  
  471.   $decoded= Crypt::X509->new($cert);
  472.   print "Certificate was issued by:".join(',',@{$decoded->Issuer})."\n";
  473.  
  474. =cut back
  475.  
  476. sub Issuer {
  477.     my $self = shift;
  478.     my ( $i, $type );
  479.     my $issuerdn = $self->{'tbsCertificate'}->{'issuer'}->{'rdnSequence'};
  480.     $self->{'tbsCertificate'}->{'issuer'}->{'dn'} = [];
  481.     my $issuedn = $self->{'tbsCertificate'}->{'issuer'}->{'dn'};
  482.     foreach my $issue ( @{$issuerdn} ) {
  483.         foreach my $i ( @{$issue} ) {
  484.             if ( $oid2attr{ $i->{'type'} } ) {
  485.                 $type = $oid2attr{ $i->{'type'} };
  486.             } else {
  487.                 $type = $i->{'type'};
  488.             }
  489.             my @key = keys( %{ $i->{'value'} } );
  490.             push @{$issuedn}, $type . "=" . $i->{'value'}->{ $key[0] };
  491.         }
  492.     }
  493.     return $issuedn;
  494. }
  495.  
  496. sub _issuer_part {
  497.     my $self      = shift;
  498.     my $oid       = shift;
  499.     my $issuerrdn = $self->{'tbsCertificate'}->{'issuer'}->{'rdnSequence'};
  500.     foreach my $issue ( @{$issuerrdn} ) {
  501.         foreach my $i ( @{$issue} ) {
  502.             if ( $i->{'type'} eq $oid ) {
  503.                 my @key = keys( %{ $i->{'value'} } );
  504.                 return $i->{'value'}->{ $key[0] };
  505.             }
  506.         }
  507.     }
  508.     return undef;
  509. }
  510.  
  511. =head2 issuer_cn 
  512.  
  513. Returns the string value for issuer's common name (= the value with the
  514. OID 2.5.4.3 or in DN Syntax everything after C<CN=>).
  515. Only the first entry is returned. C<undef> if issuer contains no common name attribute.
  516.  
  517. =cut back
  518.  
  519. sub issuer_cn {
  520.     my $self = shift;
  521.     return _issuer_part( $self, '2.5.4.3' );
  522. }
  523.  
  524. =head2 issuer_country 
  525.  
  526. Returns the string value for issuer's country (= the value with the
  527.  OID 2.5.4.6 or in DN Syntax everything after C<C=>).
  528. Only the first entry is returned. C<undef> if issuer contains no country attribute.
  529.  
  530. =cut back
  531.  
  532. sub issuer_country {
  533.     my $self = shift;
  534.     return _issuer_part( $self, '2.5.4.6' );
  535. }
  536.  
  537. =head2 issuer_state
  538.  
  539. Returns the string value for issuer's state or province (= the value with the 
  540. OID 2.5.4.8 or in DN Syntax everything after C<S=>).
  541. Only the first entry is returned. C<undef> if issuer contains no state attribute.
  542.  
  543. =cut back
  544.  
  545. sub issuer_state {
  546.     my $self = shift;
  547.     return _issuer_part( $self, '2.5.4.8' );
  548. }
  549.  
  550. =head2 issuer_locality
  551.  
  552. Returns the string value for issuer's locality (= the value with the
  553. OID 2.5.4.7 or in DN Syntax everything after C<L=>).
  554. Only the first entry is returned. C<undef> if issuer contains no locality attribute.
  555.  
  556. =cut back
  557.  
  558. sub issuer_locality {
  559.     my $self = shift;
  560.     return _issuer_part( $self, '2.5.4.7' );
  561. }
  562.  
  563. =head2 issuer_org
  564.  
  565. Returns the string value for issuer's organization (= the value with the
  566. OID 2.5.4.10 or in DN Syntax everything after C<O=>).
  567. Only the first entry is returned. C<undef> if issuer contains no organization attribute.
  568.  
  569. =cut back
  570.  
  571. sub issuer_org {
  572.     my $self = shift;
  573.     return _issuer_part( $self, '2.5.4.10' );
  574. }
  575.  
  576. =head2 issuer_email
  577.  
  578. Returns the string value for issuer's email address (= the value with the
  579. OID 1.2.840.113549.1.9.1 or in DN Syntax everything after C<E=>).
  580. Only the first entry is returned. C<undef> if issuer contains no email attribute.
  581.  
  582. =cut back
  583.  
  584. sub issuer_email {
  585.     my $self = shift;
  586.     return _issuer_part( $self, '1.2.840.113549.1.9.1' );
  587. }
  588. #########################################################################
  589. # accessors - extensions (automate this)
  590. #########################################################################
  591.  
  592. =head2 KeyUsage
  593.  
  594. returns a pointer to an array of strings describing the valid Usages 
  595. for this certificate. C<undef> is returned, when the extension is not set in the
  596. certificate.
  597.  
  598. If the extension is marked critical, this is also reported.
  599.  
  600.   $decoded= Crypt::X509->new(cert => $cert);
  601.   print "Allowed usages for this Certificate are:\n".join("\n",@{$decoded->KeyUsage})."\n";
  602.  
  603.   Example Output:
  604.   Allowed usages for this Certificate are:
  605.   critical 
  606.   digitalSignature
  607.   keyEncipherment
  608.   dataEncipherment
  609.  
  610. =cut back
  611.  
  612. sub KeyUsage {
  613.     my $self = shift;
  614.     my $ext;
  615.     my $exts = $self->{'tbsCertificate'}->{'extensions'};
  616.     if ( !defined $exts ) { return undef; }
  617.     ;    # no extensions in certificate
  618.     foreach $ext ( @{$exts} ) {
  619.         if ( $ext->{'extnID'} eq '2.5.29.15' ) {    #OID for keyusage
  620.             my $parsKeyU = _init('KeyUsage');                           # get a parser for this
  621.             my $keyusage = $parsKeyU->decode( $ext->{'extnValue'} );    # decode the value
  622.             if ( $parsKeyU->error ) {
  623.                 $self->{"_error"} = $parsKeyU->error;
  624.                 return undef;
  625.             }
  626.             my $keyu = unpack( "n", ${$keyusage}[0] . ${$keyusage}[1] ) & 0xff80;
  627.             $ext->{'usage'} = [];
  628.             if ( $ext->{'critical'} ) { push @{ $ext->{'usage'} }, "critical"; }           # mark as critical, if appropriate
  629.             if ( $keyu & 0x8000 )     { push @{ $ext->{'usage'} }, "digitalSignature"; }
  630.             if ( $keyu & 0x4000 )     { push @{ $ext->{'usage'} }, "nonRepudiation"; }
  631.             if ( $keyu & 0x2000 )     { push @{ $ext->{'usage'} }, "keyEncipherment"; }
  632.             if ( $keyu & 0x1000 )     { push @{ $ext->{'usage'} }, "dataEncipherment"; }
  633.             if ( $keyu & 0x0800 )     { push @{ $ext->{'usage'} }, "keyAgreement"; }
  634.             if ( $keyu & 0x0400 )     { push @{ $ext->{'usage'} }, "keyCertSign"; }
  635.             if ( $keyu & 0x0200 )     { push @{ $ext->{'usage'} }, "cRLSign"; }
  636.             if ( $keyu & 0x0100 )     { push @{ $ext->{'usage'} }, "encipherOnly"; }
  637.             if ( $keyu & 0x0080 )     { push @{ $ext->{'usage'} }, "decipherOnly"; }
  638.             return $ext->{'usage'};
  639.         }
  640.     }
  641.     return undef;                                                                          # keyusage extension not found
  642. }
  643.  
  644. =head2 ExtKeyUsage
  645.  
  646. returns a pointer to an array of ExtKeyUsage strings (or OIDs for unknown OIDs) or
  647. C<undef> if the extension is not filled. OIDs of the following ExtKeyUsages are known: 
  648. serverAuth, clientAuth, codeSigning, emailProtection, timeStamping, OCSPSigning
  649.  
  650. If the extension is marked critical, this is also reported.
  651.  
  652.   $decoded= Crypt::X509->new($cert);
  653.   print "ExtKeyUsage extension of this Certificates is: ", join(", ", @{$decoded->ExtKeyUsage}), "\n";
  654.   
  655.   Example Output: ExtKeyUsage extension of this Certificates is: critical, serverAuth
  656.  
  657. =cut back
  658.  
  659. my %oid2extkeyusage = (
  660.                         '1.3.6.1.5.5.7.3.1' => 'serverAuth',
  661.                         '1.3.6.1.5.5.7.3.2' => 'clientAuth',
  662.                         '1.3.6.1.5.5.7.3.3' => 'codeSigning',
  663.                         '1.3.6.1.5.5.7.3.4' => 'emailProtection',
  664.                         '1.3.6.1.5.5.7.3.8' => 'timeStamping',
  665.                         '1.3.6.1.5.5.7.3.9' => 'OCSPSigning',
  666. );
  667.  
  668. sub ExtKeyUsage {
  669.     my $self = shift;
  670.     my $ext;
  671.     my $exts = $self->{'tbsCertificate'}->{'extensions'};
  672.     if ( !defined $exts ) { return undef; }
  673.     ;    # no extensions in certificate
  674.     foreach $ext ( @{$exts} ) {
  675.         if ( $ext->{'extnID'} eq '2.5.29.37' ) {    #OID for ExtKeyUsage
  676.             return $ext->{'oids'} if defined $ext->{'oids'};
  677.             my $parsExtKeyUsage = _init('ExtKeyUsageSyntax');                         # get a parser for this
  678.             my $oids            = $parsExtKeyUsage->decode( $ext->{'extnValue'} );    # decode the value
  679.             if ( $parsExtKeyUsage->error ) {
  680.                 $self->{"_error"} = $parsExtKeyUsage->error;
  681.                 return undef;
  682.             }
  683.             $ext->{'oids'} = [ map { $oid2extkeyusage{$_} || $_ } @$oids ];
  684.             if ( $ext->{'critical'} ) { unshift @{ $ext->{'oids'} }, "critical"; }    # mark as critical, if appropriate
  685.             return $ext->{'oids'};
  686.         }
  687.     }
  688.     return undef;
  689. }
  690.  
  691. =head2 SubjectAltName
  692.  
  693. returns a pointer to an array of strings containing alternative Subjectnames or
  694. C<undef> if the extension is not filled. Usually this Extension holds the e-Mail
  695. address for person-certificates or DNS-Names for server certificates.
  696.  
  697. It also pre-pends the field type (ie rfc822Name) to the returned value.
  698.  
  699.   $decoded= Crypt::X509->new($cert);
  700.   print "E-Mail or Hostnames in this Certificates is/are:", join(", ", @{$decoded->SubjectAltName}), "\n";
  701.   
  702.   Example Output: E-Mail or Hostnames in this Certificates is/are: rfc822Name=user@server.com
  703.  
  704. =cut back
  705.  
  706. sub SubjectAltName {
  707.     my $self = shift;
  708.     my $ext;
  709.     my $exts = $self->{'tbsCertificate'}->{'extensions'};
  710.     if ( !defined $exts ) { return undef; }
  711.     ;    # no extensions in certificate
  712.     foreach $ext ( @{$exts} ) {
  713.         if ( $ext->{'extnID'} eq '2.5.29.17' ) {    #OID for SubjectAltName
  714.             my $parsSubjAlt = _init('SubjectAltName');                        # get a parser for this
  715.             my $altnames    = $parsSubjAlt->decode( $ext->{'extnValue'} );    # decode the value
  716.             if ( $parsSubjAlt->error ) {
  717.                 $self->{"_error"} = $parsSubjAlt->error;
  718.                 return undef;
  719.             }
  720.             $ext->{'names'} = [];
  721.             foreach my $name ( @{$altnames} ) {
  722.                 foreach my $value ( keys %{$name} ) {
  723.                     push @{ $ext->{'names'} }, "$value=" . $name->{$value};
  724.                 }
  725.             }
  726.             return $ext->{'names'};
  727.         }
  728.     }
  729.     return undef;
  730. }
  731. #########################################################################
  732. # accessors - authorityCertIssuer
  733. #########################################################################
  734. sub _AuthorityKeyIdentifier {
  735.     my $self = shift;
  736.     my $ext;
  737.     my $exts = $self->{'tbsCertificate'}->{'extensions'};
  738.     if ( !defined $exts ) { return undef; }
  739.     ;    # no extensions in certificate
  740.     if ( defined $self->{'tbsCertificate'}{'AuthorityKeyIdentifier'} ) {
  741.         return ( $self->{'tbsCertificate'}{'AuthorityKeyIdentifier'} );
  742.     }
  743.     foreach $ext ( @{$exts} ) {
  744.         if ( $ext->{'extnID'} eq '2.5.29.35' ) {    #OID for AuthorityKeyIdentifier
  745.             my $pars = _init('AuthorityKeyIdentifier');    # get a parser for this
  746.             $self->{'tbsCertificate'}{'AuthorityKeyIdentifier'} = $pars->decode( $ext->{'extnValue'} );    # decode the value
  747.             if ( $pars->error ) {
  748.                 $self->{"_error"} = $pars->error;
  749.                 return undef;
  750.             }
  751.             return $self->{'tbsCertificate'}{'AuthorityKeyIdentifier'};
  752.         }
  753.     }
  754.     return undef;
  755. }
  756.  
  757. =head2 authorityCertIssuer
  758.  
  759. returns a pointer to an array of strings building the DN of the Authority Cert
  760. Issuer. Attributenames for the most common Attributes
  761. are translated from the OID-Numbers, unknown numbers are output verbatim.
  762. undef if the extension is not set in the certificate.
  763.  
  764.   $decoded= Crypt::X509->new($cert);
  765.   print "Certificate was authorised by:".join(',',@{$decoded->authorityCertIssuer})."\n";
  766.  
  767. =cut back
  768.  
  769. sub authorityCertIssuer {
  770.     my $self = shift;
  771.     my ( $i, $type );
  772.     my $rdn = _AuthorityKeyIdentifier($self);
  773.     if ( !defined($rdn) ) {
  774.         return (undef);    # we do not have that extension
  775.     } else {
  776.         $rdn = $rdn->{'authorityCertIssuer'}[0]->{'directoryName'};
  777.     }
  778.     $rdn->{'dn'} = [];
  779.     my $dn = $rdn->{'dn'};
  780.     $rdn = $rdn->{'rdnSequence'};
  781.     foreach my $r ( @{$rdn} ) {
  782.         $i = @{$r}[0];
  783.         if ( $oid2attr{ $i->{'type'} } ) {
  784.             $type = $oid2attr{ $i->{'type'} };
  785.         } else {
  786.             $type = $i->{'type'};
  787.         }
  788.         my @key = keys( %{ $i->{'value'} } );
  789.         push @{$dn}, $type . "=" . $i->{'value'}->{ $key[0] };
  790.     }
  791.     return $dn;
  792. }
  793.  
  794. sub _authcert_part {
  795.     my $self = shift;
  796.     my $oid  = shift;
  797.     my $rdn  = _AuthorityKeyIdentifier($self);
  798.     if ( !defined($rdn) ) {
  799.         return (undef);    # we do not have that extension
  800.     } else {
  801.         $rdn = $rdn->{'authorityCertIssuer'}[0]->{'directoryName'}->{'rdnSequence'};
  802.     }
  803.     foreach my $r ( @{$rdn} ) {
  804.         my $i = @{$r}[0];
  805.         if ( $i->{'type'} eq $oid ) {
  806.             my @key = keys( %{ $i->{'value'} } );
  807.             return $i->{'value'}->{ $key[0] };
  808.         }
  809.     }
  810.     return undef;
  811. }
  812.  
  813. =head2 authority_serial
  814.  
  815. Returns the authority's certificate serial number.
  816.  
  817. =cut back
  818.  
  819. sub authority_serial {
  820.     my $self = shift;
  821.     return ( $self->_AuthorityKeyIdentifier )->{authorityCertSerialNumber};
  822. }
  823.  
  824. =head2 key_identifier
  825.  
  826. Returns the authority key identifier or undef if it is a rooted cert
  827.  
  828. =cut back
  829.  
  830. sub key_identifier {
  831.     my $self = shift;
  832.     if ( defined $self->_AuthorityKeyIdentifier ) { return ( $self->_AuthorityKeyIdentifier )->{keyIdentifier}; }
  833.     return undef;
  834. }
  835.  
  836. =head2 authority_cn
  837.  
  838. Returns the authority's ca.
  839.  
  840. =cut back
  841.  
  842. sub authority_cn {
  843.     my $self = shift;
  844.     return _authcert_part( $self, '2.5.4.3' );
  845. }
  846.  
  847. =head2 authority_country
  848.  
  849. Returns the authority's country.
  850.  
  851. =cut back
  852.  
  853. sub authority_country {
  854.     my $self = shift;
  855.     return _authcert_part( $self, '2.5.4.6' );
  856. }
  857.  
  858. =head2 authority_state
  859.  
  860. Returns the authority's state.
  861.  
  862. =cut back
  863.  
  864. sub authority_state {
  865.     my $self = shift;
  866.     return _authcert_part( $self, '2.5.4.8' );
  867. }
  868.  
  869. =head2 authority_locality
  870.  
  871. Returns the authority's locality.
  872.  
  873. =cut back
  874.  
  875. sub authority_locality {
  876.     my $self = shift;
  877.     return _authcert_part( $self, '2.5.4.7' );
  878. }
  879.  
  880. =head2 authority_org
  881.  
  882. Returns the authority's organization.
  883.  
  884. =cut back
  885.  
  886. sub authority_org {
  887.     my $self = shift;
  888.     return _authcert_part( $self, '2.5.4.10' );
  889. }
  890.  
  891. =head2 authority_email
  892.  
  893. Returns the authority's email.
  894.  
  895. =cut back
  896.  
  897. sub authority_email {
  898.     my $self = shift;
  899.     return _authcert_part( $self, '1.2.840.113549.1.9.1' );
  900. }
  901.  
  902. =head2 CRLDistributionPoints
  903.  
  904. Returns the CRL distribution points as an array of strings (with one value usually)
  905.  
  906. =cut back
  907.  
  908. sub CRLDistributionPoints {
  909.     my $self = shift;
  910.     my $ext;
  911.     my $exts = $self->{'tbsCertificate'}->{'extensions'};
  912.     if ( !defined $exts ) { return undef; }
  913.     ;    # no extensions in certificate
  914.     foreach $ext ( @{$exts} ) {
  915.         if ( $ext->{'extnID'} eq '2.5.29.31' ) {    #OID for cRLDistributionPoints
  916.             my $crlp   = _init('cRLDistributionPoints');          # get a parser for this
  917.             my $points = $crlp->decode( $ext->{'extnValue'} );    # decode the value
  918.             $points = $points->[0]->{'distributionPoint'}->{'fullName'};
  919.             if ( $crlp->error ) {
  920.                 $self->{"_error"} = $crlp->error;
  921.                 return undef;
  922.             }
  923.             foreach my $name ( @{$points} ) {
  924.                 push @{ $ext->{'crlpoints'} }, $name->{'uniformResourceIdentifier'};
  925.             }
  926.             return $ext->{'crlpoints'};
  927.         }
  928.     }
  929.     return undef;
  930. }
  931.  
  932. =head2 CRLDistributionPoints2
  933.  
  934. Returns the CRL distribution points as an array of hashes (allowing for some variations)
  935.  
  936. =cut back
  937.  
  938. # newer CRL
  939. sub CRLDistributionPoints2 {
  940.     my $self = shift;
  941.     my %CDPs;
  942.     my $dp_cnt = 0;    # this is a counter used to show which CDP a particular value is listed in
  943.     my $extensions = $self->{'tbsCertificate'}->{'extensions'};
  944.     if ( !defined $extensions ) { return undef; }
  945.     ;                  # no extensions in certificate
  946.     for my $extension ( @{$extensions} ) {
  947.         if ( $extension->{'extnID'} eq '2.5.29.31' ) {    # OID for ARRAY of cRLDistributionPoints
  948.             my $parser = _init('cRLDistributionPoints');                  # get a parser for CDPs
  949.             my $points = $parser->decode( $extension->{'extnValue'} );    # decode the values (returns an array)
  950.             for my $each_dp ( @{$points} ) {                              # this loops through multiple "distributionPoint" values
  951.                 $dp_cnt++;
  952.                 for my $each_fullName ( @{ $each_dp->{'distributionPoint'}->{'fullName'} } )
  953.                 {                                                         # this loops through multiple "fullName" values
  954.                     if ( exists $each_fullName->{directoryName} ) {
  955.  
  956.                         # found a rdnSequence
  957.                         my $rdn = join ',', reverse @{ my_CRL_rdn( $each_fullName->{directoryName}->{rdnSequence} ) };
  958.                         push @{ $CDPs{$dp_cnt} }, "Directory Address: $rdn";
  959.                     } elsif ( exists $each_fullName->{uniformResourceIdentifier} ) {
  960.  
  961.                         # found a URI
  962.                         push @{ $CDPs{$dp_cnt} }, "URL: " . $each_fullName->{uniformResourceIdentifier};
  963.                     } else {
  964.  
  965.                         # found some other type of CDP value
  966.                         # return undef;
  967.                     }
  968.                 }
  969.             }
  970.             return %CDPs;
  971.         }
  972.     }
  973.     return undef;
  974. }
  975.  
  976. sub my_CRL_rdn {
  977.     my $crl_rdn = shift;    # this should be the passed in 'rdnSequence' array
  978.     my ( $i, $type );
  979.     my $crl_dn = [];
  980.     for my $part ( @{$crl_rdn} ) {
  981.         $i = @{$part}[0];
  982.         if ( $oid2attr{ $i->{'type'} } ) {
  983.             $type = $oid2attr{ $i->{'type'} };
  984.         } else {
  985.             $type = $i->{'type'};
  986.         }
  987.         my @key = keys( %{ $i->{'value'} } );
  988.         push @{$crl_dn}, $type . "=" . $i->{'value'}->{ $key[0] };
  989.     }
  990.     return $crl_dn;
  991. }
  992.  
  993. =head2 CertificatePolicies
  994.  
  995. Returns the CertificatePolicies as an array of strings
  996.  
  997. =cut back
  998.  
  999. # CertificatePolicies (another extension)
  1000. sub CertificatePolicies {
  1001.     my $self = shift;
  1002.     my $extension;
  1003.     my $CertPolicies = [];
  1004.     my $extensions   = $self->{'tbsCertificate'}->{'extensions'};
  1005.     if ( !defined $extensions ) { return undef; }
  1006.     ;    # no extensions in certificate
  1007.     for $extension ( @{$extensions} ) {
  1008.         if ( $extension->{'extnID'} eq '2.5.29.32' ) {    # OID for CertificatePolicies
  1009.             my $parser   = _init('CertificatePolicies');                    # get a parser for this
  1010.             my $policies = $parser->decode( $extension->{'extnValue'} );    # decode the value
  1011.             for my $policy ( @{$policies} ) {
  1012.                 for my $key ( keys %{$policy} ) {
  1013.                     push @{$CertPolicies}, "$key=" . $policy->{$key};
  1014.                 }
  1015.             }
  1016.             return $CertPolicies;
  1017.         }
  1018.     }
  1019.     return undef;
  1020. }
  1021.  
  1022. =head2 EntrustVersionInfo
  1023.  
  1024. Returns the EntrustVersion as a string
  1025.  
  1026.     print "Entrust Version: ", $decoded->EntrustVersion, "\n";
  1027.     
  1028.     Example Output: Entrust Version: V7.0
  1029.  
  1030. =cut back
  1031.  
  1032. # EntrustVersion (another extension)
  1033. sub EntrustVersion {
  1034.     my $self = shift;
  1035.     my $extension;
  1036.     my $extensions = $self->{'tbsCertificate'}->{'extensions'};
  1037.     if ( !defined $extensions ) { return undef; }
  1038.     ;    # no extensions in certificate
  1039.     for $extension ( @{$extensions} ) {
  1040.         if ( $extension->{'extnID'} eq '1.2.840.113533.7.65.0' ) {    # OID for EntrustVersionInfo
  1041.             my $parser  = _init('EntrustVersionInfo');                     # get a parser for this
  1042.             my $entrust = $parser->decode( $extension->{'extnValue'} );    # decode the value
  1043.             return $entrust->{'entrustVers'};
  1044.  
  1045.             # not doing anything with the EntrustInfoFlags BIT STRING (yet)
  1046.             # $entrust->{'entrustInfoFlags'}
  1047.         }
  1048.     }
  1049.     return undef;
  1050. }
  1051.  
  1052. =head2 SubjectDirectoryAttributes
  1053.  
  1054. Returns the SubjectDirectoryAttributes as an array of key = value pairs, to include a data type
  1055.  
  1056.     print "Subject Directory Attributes: ", join( ', ' , @{ $decoded->SubjectDirectoryAttributes } ), "\n";
  1057.     
  1058.     Example Output: Subject Directory Attributes: 1.2.840.113533.7.68.29 = 7 (integer)
  1059.  
  1060. =cut back
  1061.  
  1062. # SubjectDirectoryAttributes (another extension)
  1063. sub SubjectDirectoryAttributes {
  1064.     my $self = shift;
  1065.     my $extension;
  1066.     my $attributes = [];
  1067.     my $extensions = $self->{'tbsCertificate'}->{'extensions'};
  1068.     if ( !defined $extensions ) { return undef; }
  1069.     ;    # no extensions in certificate
  1070.     for $extension ( @{$extensions} ) {
  1071.         if ( $extension->{'extnID'} eq '2.5.29.9' ) {    # OID for SubjectDirectoryAttributes
  1072.             my $parser            = _init('SubjectDirectoryAttributes');             # get a parser for this
  1073.             my $subject_dir_attrs = $parser->decode( $extension->{'extnValue'} );    # decode the value
  1074.             for my $type ( @{$subject_dir_attrs} ) {
  1075.                 for my $value ( @{ $type->{'values'} } ) {
  1076.                     for my $key ( keys %{$value} ) {
  1077.                         push @{$attributes}, $type->{'type'} . " = " . $value->{$key} . " ($key)";
  1078.                     }
  1079.                 }
  1080.             }
  1081.             return $attributes;
  1082.         }
  1083.     }
  1084.     return undef;
  1085. }
  1086.  
  1087. =head2 BasicConstraints
  1088.  
  1089. Returns the BasicConstraints as an array and the criticallity pre-pended.
  1090.  
  1091. =cut back
  1092.  
  1093. # BasicConstraints (another extension)
  1094. sub BasicConstraints {
  1095.     my $self = shift;
  1096.     my $extension;
  1097.     my $constraints = [];
  1098.     my $extensions  = $self->{'tbsCertificate'}->{'extensions'};
  1099.     if ( !defined $extensions ) { return undef; }
  1100.     ;    # no extensions in certificate
  1101.     for $extension ( @{$extensions} ) {
  1102.         if ( $extension->{'extnID'} eq '2.5.29.19' ) {    # OID for BasicConstraints
  1103.             if ( $extension->{'critical'} ) { push @{$constraints}, "critical"; }    # mark this as critical as appropriate
  1104.             my $parser            = _init('BasicConstraints');                       # get a parser for this
  1105.             my $basic_constraints = $parser->decode( $extension->{'extnValue'} );    # decode the value
  1106.             for my $key ( keys %{$basic_constraints} ) {
  1107.                 push @{$constraints}, "$key = " . $basic_constraints->{$key};
  1108.             }
  1109.             return $constraints;
  1110.         }
  1111.     }
  1112.     return undef;
  1113. }
  1114.  
  1115. =head2 subject_keyidentifier
  1116.  
  1117. Returns the subject key identifier from the extensions.
  1118.  
  1119. =cut back
  1120.  
  1121. # subject_keyidentifier (another extension)
  1122. sub subject_keyidentifier {
  1123.     my $self = shift;
  1124.     return $self->_SubjectKeyIdentifier;
  1125. }
  1126.  
  1127. # _SubjectKeyIdentifier (another extension)
  1128. sub _SubjectKeyIdentifier {
  1129.     my $self       = shift;
  1130.     my $extensions = $self->{'tbsCertificate'}->{'extensions'};
  1131.     if ( !defined $extensions ) { return undef; }
  1132.     ;    # no extensions in certificate
  1133.     if ( defined $self->{'tbsCertificate'}{'SubjectKeyIdentifier'} ) {
  1134.         return ( $self->{'tbsCertificate'}{'SubjectKeyIdentifier'} );
  1135.     }
  1136.     for my $extension ( @{$extensions} ) {
  1137.         if ( $extension->{'extnID'} eq '2.5.29.14' ) {    # OID for SubjectKeyIdentifier
  1138.             my $parser = _init('SubjectKeyIdentifier');    # get a parser for this
  1139.             $self->{'tbsCertificate'}{'SubjectKeyIdentifier'} = $parser->decode( $extension->{'extnValue'} );    # decode the value
  1140.             if ( $parser->error ) {
  1141.                 $self->{"_error"} = $parser->error;
  1142.                 return undef;
  1143.             }
  1144.             return $self->{'tbsCertificate'}{'SubjectKeyIdentifier'};
  1145.         }
  1146.     }
  1147.     return undef;
  1148. }
  1149.  
  1150. =head2 SubjectInfoAccess
  1151.  
  1152. Returns the SubjectInfoAccess as an array of hashes with key=value pairs.
  1153.  
  1154.         print "Subject Info Access: ";
  1155.         if ( defined $decoded->SubjectInfoAccess ) {
  1156.             my %SIA = $decoded->SubjectInfoAccess;
  1157.             for my $key ( keys %SIA ) {
  1158.                 print "\n\t$key: \n\t";
  1159.                 print join( "\n\t" , @{ $SIA{$key} } ), "\n";
  1160.             }
  1161.         } else { print "\n" }
  1162.     
  1163.     Example Output: 
  1164.         Subject Info Access: 
  1165.             1.3.6.1.5.5.7.48.5: 
  1166.             uniformResourceIdentifier = http://pki.treas.gov/root_sia.p7c
  1167.             uniformResourceIdentifier = ldap://ldap.treas.gov/ou=US%20Treasury%20Root%20CA,ou=Certification%20Authorities,ou=Department%20of%20the%20Treasury,o=U.S.%20Government,c=US?cACertificate;binary,crossCertificatePair;binary
  1168.  
  1169. =cut back
  1170.  
  1171. # SubjectInfoAccess (another extension)
  1172. sub SubjectInfoAccess {
  1173.     my $self = shift;
  1174.     my $extension;
  1175.     my %SIA;
  1176.     my $extensions = $self->{'tbsCertificate'}->{'extensions'};
  1177.     if ( !defined $extensions ) { return undef; }
  1178.     ;    # no extensions in certificate
  1179.     for $extension ( @{$extensions} ) {
  1180.         if ( $extension->{'extnID'} eq '1.3.6.1.5.5.7.1.11' ) {    # OID for SubjectInfoAccess
  1181.             my $parser              = _init('SubjectInfoAccessSyntax');                # get a parser for this
  1182.             my $subject_info_access = $parser->decode( $extension->{'extnValue'} );    # decode the value
  1183.             for my $sia ( @{$subject_info_access} ) {
  1184.                 for my $key ( keys %{ $sia->{'accessLocation'} } ) {
  1185.                     push @{ $SIA{ $sia->{'accessMethod'} } }, "$key = " . $sia->{'accessLocation'}{$key};
  1186.                 }
  1187.             }
  1188.             return %SIA;
  1189.         }
  1190.     }
  1191.     return undef;
  1192. }
  1193. #######################################################################
  1194. # internal functions
  1195. #######################################################################
  1196. sub _init {
  1197.     my $what = shift;
  1198.     if ( ( !defined $what ) || ( '' eq $what ) ) { $what = 'Certificate' }
  1199.     if ( !defined $asn ) {
  1200.         $asn = Convert::ASN1->new;
  1201.         $asn->prepare(<<ASN1);
  1202. -- ASN.1 from RFC2459 and X.509(2001)
  1203. -- Adapted for use with Convert::ASN1
  1204. -- Id: x509decode,v 1.1 2002/02/10 16:41:28 gbarr Exp 
  1205.  
  1206. -- attribute data types --
  1207.  
  1208. Attribute ::= SEQUENCE {
  1209.     type            AttributeType,
  1210.     values            SET OF AttributeValue
  1211.         -- at least one value is required -- 
  1212.     }
  1213.  
  1214. AttributeType ::= OBJECT IDENTIFIER
  1215.  
  1216. AttributeValue ::= DirectoryString  --ANY 
  1217.  
  1218. AttributeTypeAndValue ::= SEQUENCE {
  1219.     type            AttributeType,
  1220.     value            AttributeValue
  1221.     }
  1222.  
  1223.  
  1224. -- naming data types --
  1225.  
  1226. Name ::= CHOICE { -- only one possibility for now 
  1227.     rdnSequence        RDNSequence             
  1228.     }
  1229.  
  1230. RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
  1231.  
  1232. DistinguishedName ::= RDNSequence
  1233.  
  1234. RelativeDistinguishedName ::= 
  1235.     SET OF AttributeTypeAndValue  --SET SIZE (1 .. MAX) OF
  1236.  
  1237.  
  1238. -- Directory string type --
  1239.  
  1240. DirectoryString ::= CHOICE {
  1241.     teletexString        TeletexString,  --(SIZE (1..MAX)),
  1242.     printableString        PrintableString,  --(SIZE (1..MAX)),
  1243.     bmpString        BMPString,  --(SIZE (1..MAX)),
  1244.     universalString        UniversalString,  --(SIZE (1..MAX)),
  1245.     utf8String        UTF8String,  --(SIZE (1..MAX)),
  1246.     ia5String        IA5String,  --added for EmailAddress,
  1247.     integer            INTEGER
  1248.     }
  1249.  
  1250.  
  1251. -- certificate and CRL specific structures begin here
  1252.  
  1253. Certificate ::= SEQUENCE  {
  1254.     tbsCertificate        TBSCertificate,
  1255.     signatureAlgorithm    AlgorithmIdentifier,
  1256.     signature        BIT STRING
  1257.     }
  1258.  
  1259. TBSCertificate  ::=  SEQUENCE  {
  1260.     version            [0] EXPLICIT Version OPTIONAL,  --DEFAULT v1
  1261.     serialNumber        CertificateSerialNumber,
  1262.     signature        AlgorithmIdentifier,
  1263.     issuer            Name,
  1264.     validity        Validity,
  1265.     subject            Name,
  1266.     subjectPublicKeyInfo    SubjectPublicKeyInfo,
  1267.     issuerUniqueID        [1] IMPLICIT UniqueIdentifier OPTIONAL,
  1268.         -- If present, version shall be v2 or v3
  1269.     subjectUniqueID        [2] IMPLICIT UniqueIdentifier OPTIONAL,
  1270.         -- If present, version shall be v2 or v3
  1271.     extensions        [3] EXPLICIT Extensions OPTIONAL
  1272.         -- If present, version shall be v3
  1273.     }
  1274.  
  1275. Version ::= INTEGER  --{  v1(0), v2(1), v3(2)  }
  1276.  
  1277. CertificateSerialNumber ::= INTEGER
  1278.  
  1279. Validity ::= SEQUENCE {
  1280.     notBefore        Time,
  1281.     notAfter        Time
  1282.     }
  1283.  
  1284. Time ::= CHOICE {
  1285.     utcTime            UTCTime,
  1286.     generalTime        GeneralizedTime
  1287.     }
  1288.  
  1289. UniqueIdentifier ::= BIT STRING
  1290.  
  1291. SubjectPublicKeyInfo ::= SEQUENCE {
  1292.     algorithm        AlgorithmIdentifier,
  1293.     subjectPublicKey    BIT STRING
  1294.     }
  1295.  
  1296. Extensions ::= SEQUENCE OF Extension  --SIZE (1..MAX) OF Extension
  1297.  
  1298. Extension ::= SEQUENCE {
  1299.     extnID            OBJECT IDENTIFIER,
  1300.     critical        BOOLEAN OPTIONAL,  --DEFAULT FALSE,
  1301.     extnValue        OCTET STRING
  1302.     }
  1303.  
  1304. AlgorithmIdentifier ::= SEQUENCE {
  1305.     algorithm        OBJECT IDENTIFIER,
  1306.     parameters        ANY OPTIONAL
  1307.     }
  1308.  
  1309.  
  1310. --extensions
  1311.  
  1312. AuthorityKeyIdentifier ::= SEQUENCE {
  1313.       keyIdentifier             [0] KeyIdentifier            OPTIONAL,
  1314.       authorityCertIssuer       [1] GeneralNames             OPTIONAL,
  1315.       authorityCertSerialNumber [2] CertificateSerialNumber  OPTIONAL }
  1316.     -- authorityCertIssuer and authorityCertSerialNumber shall both
  1317.     -- be present or both be absent
  1318.  
  1319. KeyIdentifier ::= OCTET STRING
  1320.  
  1321. SubjectKeyIdentifier ::= KeyIdentifier
  1322.  
  1323. -- key usage extension OID and syntax
  1324.  
  1325. -- id-ce-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 }
  1326.  
  1327. KeyUsage ::= BIT STRING --{
  1328. --      digitalSignature        (0),
  1329. --      nonRepudiation          (1),
  1330. --      keyEncipherment         (2),
  1331. --      dataEncipherment        (3),
  1332. --      keyAgreement            (4),
  1333. --      keyCertSign             (5),
  1334. --      cRLSign                 (6),
  1335. --      encipherOnly            (7),
  1336. --      decipherOnly            (8) }
  1337.  
  1338.  
  1339. -- private key usage period extension OID and syntax
  1340.  
  1341. -- id-ce-privateKeyUsagePeriod OBJECT IDENTIFIER ::=  { id-ce 16 }
  1342.  
  1343. PrivateKeyUsagePeriod ::= SEQUENCE {
  1344.      notBefore       [0]     GeneralizedTime OPTIONAL,
  1345.      notAfter        [1]     GeneralizedTime OPTIONAL }
  1346.      -- either notBefore or notAfter shall be present
  1347.      
  1348. -- certificate policies extension OID and syntax
  1349. -- id-ce-certificatePolicies OBJECT IDENTIFIER ::=  { id-ce 32 }
  1350.  
  1351. CertificatePolicies ::= SEQUENCE OF PolicyInformation
  1352.  
  1353. PolicyInformation ::= SEQUENCE {
  1354.      policyIdentifier   CertPolicyId,
  1355.      policyQualifiers   SEQUENCE OF
  1356.              PolicyQualifierInfo OPTIONAL }
  1357.  
  1358. CertPolicyId ::= OBJECT IDENTIFIER
  1359.  
  1360. PolicyQualifierInfo ::= SEQUENCE {
  1361.        policyQualifierId  PolicyQualifierId,
  1362.        qualifier        ANY } --DEFINED BY policyQualifierId }
  1363.  
  1364. -- Implementations that recognize additional policy qualifiers shall
  1365. -- augment the following definition for PolicyQualifierId
  1366.  
  1367. PolicyQualifierId ::=
  1368.      OBJECT IDENTIFIER --( id-qt-cps | id-qt-unotice )
  1369.  
  1370. -- CPS pointer qualifier
  1371.  
  1372. CPSuri ::= IA5String
  1373.  
  1374. -- user notice qualifier
  1375.  
  1376. UserNotice ::= SEQUENCE {
  1377.      noticeRef        NoticeReference OPTIONAL,
  1378.      explicitText     DisplayText OPTIONAL}
  1379.  
  1380. NoticeReference ::= SEQUENCE {
  1381.      organization     DisplayText,
  1382.      noticeNumbers    SEQUENCE OF INTEGER }
  1383.  
  1384. DisplayText ::= CHOICE {
  1385.      visibleString    VisibleString  ,
  1386.      bmpString        BMPString      ,
  1387.      utf8String       UTF8String      }
  1388.  
  1389.  
  1390. -- policy mapping extension OID and syntax
  1391. -- id-ce-policyMappings OBJECT IDENTIFIER ::=  { id-ce 33 }
  1392.  
  1393. PolicyMappings ::= SEQUENCE OF SEQUENCE {
  1394.      issuerDomainPolicy      CertPolicyId,
  1395.      subjectDomainPolicy     CertPolicyId }
  1396.  
  1397.  
  1398. -- subject alternative name extension OID and syntax
  1399. -- id-ce-subjectAltName OBJECT IDENTIFIER ::=  { id-ce 17 }
  1400.  
  1401. SubjectAltName ::= GeneralNames
  1402.  
  1403. GeneralNames ::= SEQUENCE OF GeneralName
  1404.  
  1405. GeneralName ::= CHOICE {
  1406.      otherName                       [0]     AnotherName,
  1407.      rfc822Name                      [1]     IA5String,
  1408.      dNSName                         [2]     IA5String,
  1409.      x400Address                     [3]     ANY, --ORAddress,
  1410.      directoryName                   [4]     Name,
  1411.      ediPartyName                    [5]     EDIPartyName,
  1412.      uniformResourceIdentifier       [6]     IA5String,
  1413.      iPAddress                       [7]     OCTET STRING,
  1414.      registeredID                    [8]     OBJECT IDENTIFIER }
  1415.  
  1416. EntrustVersionInfo ::= SEQUENCE {
  1417.               entrustVers  GeneralString,
  1418.               entrustInfoFlags EntrustInfoFlags }
  1419.  
  1420. EntrustInfoFlags::= BIT STRING --{
  1421. --      keyUpdateAllowed
  1422. --      newExtensions     (1),  -- not used
  1423. --      pKIXCertificate   (2) } -- certificate created by pkix
  1424.  
  1425. -- AnotherName replaces OTHER-NAME ::= TYPE-IDENTIFIER, as
  1426. -- TYPE-IDENTIFIER is not supported in the 88 ASN.1 syntax
  1427.  
  1428. AnotherName ::= SEQUENCE {
  1429.      type    OBJECT IDENTIFIER,
  1430.      value      [0] EXPLICIT ANY } --DEFINED BY type-id }
  1431.  
  1432. EDIPartyName ::= SEQUENCE {
  1433.      nameAssigner            [0]     DirectoryString OPTIONAL,
  1434.      partyName               [1]     DirectoryString }
  1435.  
  1436.  
  1437. -- issuer alternative name extension OID and syntax
  1438. -- id-ce-issuerAltName OBJECT IDENTIFIER ::=  { id-ce 18 }
  1439.  
  1440. IssuerAltName ::= GeneralNames
  1441.  
  1442.  
  1443. -- id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::=  { id-ce 9 }
  1444.  
  1445. SubjectDirectoryAttributes ::= SEQUENCE OF Attribute
  1446.  
  1447.  
  1448. -- basic constraints extension OID and syntax
  1449. -- id-ce-basicConstraints OBJECT IDENTIFIER ::=  { id-ce 19 }
  1450.  
  1451. BasicConstraints ::= SEQUENCE {
  1452.      cA                      BOOLEAN OPTIONAL, --DEFAULT FALSE,
  1453.      pathLenConstraint       INTEGER OPTIONAL }
  1454.  
  1455.  
  1456. -- name constraints extension OID and syntax
  1457. -- id-ce-nameConstraints OBJECT IDENTIFIER ::=  { id-ce 30 }
  1458.  
  1459. NameConstraints ::= SEQUENCE {
  1460.      permittedSubtrees       [0]     GeneralSubtrees OPTIONAL,
  1461.      excludedSubtrees        [1]     GeneralSubtrees OPTIONAL }
  1462.  
  1463. GeneralSubtrees ::= SEQUENCE OF GeneralSubtree
  1464.  
  1465. GeneralSubtree ::= SEQUENCE {
  1466.      base                    GeneralName,
  1467.      minimum         [0]     BaseDistance OPTIONAL, --DEFAULT 0,
  1468.      maximum         [1]     BaseDistance OPTIONAL }
  1469.  
  1470. BaseDistance ::= INTEGER 
  1471.  
  1472.  
  1473. -- policy constraints extension OID and syntax
  1474. -- id-ce-policyConstraints OBJECT IDENTIFIER ::=  { id-ce 36 }
  1475.  
  1476. PolicyConstraints ::= SEQUENCE {
  1477.      requireExplicitPolicy           [0] SkipCerts OPTIONAL,
  1478.      inhibitPolicyMapping            [1] SkipCerts OPTIONAL }
  1479.  
  1480. SkipCerts ::= INTEGER 
  1481.  
  1482.  
  1483. -- CRL distribution points extension OID and syntax
  1484. -- id-ce-cRLDistributionPoints     OBJECT IDENTIFIER  ::=  {id-ce 31}
  1485.  
  1486. cRLDistributionPoints  ::= SEQUENCE OF DistributionPoint
  1487.  
  1488. DistributionPoint ::= SEQUENCE {
  1489.      distributionPoint       [0]     DistributionPointName OPTIONAL,
  1490.      reasons                 [1]     ReasonFlags OPTIONAL,
  1491.      cRLIssuer               [2]     GeneralNames OPTIONAL }
  1492.  
  1493. DistributionPointName ::= CHOICE {
  1494.      fullName                [0]     GeneralNames,
  1495.      nameRelativeToCRLIssuer [1]     RelativeDistinguishedName }
  1496.  
  1497. ReasonFlags ::= BIT STRING --{
  1498. --     unused                  (0),
  1499. --     keyCompromise           (1),
  1500. --     cACompromise            (2),
  1501. --     affiliationChanged      (3),
  1502. --     superseded              (4),
  1503. --     cessationOfOperation    (5),
  1504. --     certificateHold         (6),
  1505. --     privilegeWithdrawn      (7),
  1506. --     aACompromise            (8) }
  1507.  
  1508.  
  1509. -- extended key usage extension OID and syntax
  1510. -- id-ce-extKeyUsage OBJECT IDENTIFIER ::= {id-ce 37}
  1511.  
  1512. ExtKeyUsageSyntax ::= SEQUENCE OF KeyPurposeId
  1513.  
  1514. KeyPurposeId ::= OBJECT IDENTIFIER
  1515.  
  1516. -- extended key purpose OIDs
  1517. -- id-kp-serverAuth      OBJECT IDENTIFIER ::= { id-kp 1 }
  1518. -- id-kp-clientAuth      OBJECT IDENTIFIER ::= { id-kp 2 }
  1519. -- id-kp-codeSigning     OBJECT IDENTIFIER ::= { id-kp 3 }
  1520. -- id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 }
  1521. -- id-kp-ipsecEndSystem  OBJECT IDENTIFIER ::= { id-kp 5 }
  1522. -- id-kp-ipsecTunnel     OBJECT IDENTIFIER ::= { id-kp 6 }
  1523. -- id-kp-ipsecUser       OBJECT IDENTIFIER ::= { id-kp 7 }
  1524. -- id-kp-timeStamping    OBJECT IDENTIFIER ::= { id-kp 8 }
  1525.  
  1526.  
  1527. -- authority info access
  1528.  
  1529. -- id-pe-authorityInfoAccess OBJECT IDENTIFIER ::= { id-pe 1 }
  1530.  
  1531. AuthorityInfoAccessSyntax  ::=
  1532.         SEQUENCE OF AccessDescription --SIZE (1..MAX) OF AccessDescription
  1533.  
  1534. AccessDescription  ::=  SEQUENCE {
  1535.         accessMethod          OBJECT IDENTIFIER,
  1536.         accessLocation        GeneralName  }
  1537.  
  1538. -- subject info access
  1539.  
  1540. -- id-pe-subjectInfoAccess OBJECT IDENTIFIER ::= { id-pe 11 }
  1541.  
  1542. SubjectInfoAccessSyntax  ::=
  1543.         SEQUENCE OF AccessDescription --SIZE (1..MAX) OF AccessDescription
  1544. ASN1
  1545.     }
  1546.     my $self = $asn->find($what);
  1547.     return $self;
  1548. }
  1549.  
  1550. =head1 SEE ALSO
  1551.  
  1552. See the examples of C<Convert::ASN1> and the <perl-ldap@perl.org> Mailing List.
  1553. An example on how to load certificates can be found in F<t\Crypt-X509.t>.
  1554.  
  1555. =head1 ACKNOWLEDGEMENTS
  1556.  
  1557. This module is based on the x509decode script, which was contributed to
  1558. Convert::ASN1 in 2002 by Norbert Klasen.
  1559.  
  1560. =head1 AUTHORS
  1561.  
  1562. Mike Jackson <mj@sci.fi>, 
  1563. Alexander Jung <alexander.w.jung@gmail.com>,
  1564. Duncan Segrest <duncan@gigageek.info>
  1565.  
  1566. =head1 COPYRIGHT
  1567.  
  1568. Copyright (c) 2005 Mike Jackson <mj@sci.fi>.
  1569. Copyright (c) 2001-2002 Norbert Klasen, DAASI International GmbH.
  1570.  
  1571. All rights reserved. This program is free software; you can redistribute
  1572. it and/or modify it under the same terms as Perl itself.
  1573.  
  1574. =cut
  1575.  
  1576. 1;
  1577. __END__
  1578.