Blame t/x509.t

Packit Service a45ac5
#!/usr/bin/perl
Packit Service f99136
Packit Service f99136
print "1..26\n";
Packit Service f99136
Packit Service f99136
BEGIN { require 't/funcs.pl' }
Packit Service f99136
Packit Service f99136
use Convert::ASN1;
Packit Service f99136
Packit Service f99136
my $t = 1;
Packit Service f99136
Packit Service f99136
my $asn = Convert::ASN1->new;
Packit Service f99136
btest $t++, $asn->prepare(<
Packit Service f99136
-- ASN.1 from RFC2459 and X.509(2001)
Packit Service f99136
-- Adapted for use with Convert::ASN1
Packit Service f99136
-- Id: x509decode,v 1.1 2002/02/10 16:41:28 gbarr Exp 
Packit Service f99136
Packit Service f99136
-- attribute data types --
Packit Service f99136
Packit Service f99136
Attribute ::= SEQUENCE {
Packit Service f99136
	type			AttributeType,
Packit Service f99136
	values			SET OF AttributeValue
Packit Service f99136
		-- at least one value is required -- 
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
AttributeType ::= OBJECT IDENTIFIER
Packit Service f99136
Packit Service f99136
AttributeValue ::= DirectoryString  --ANY 
Packit Service f99136
Packit Service f99136
AttributeTypeAndValue ::= SEQUENCE {
Packit Service f99136
	type			AttributeType,
Packit Service f99136
	value			AttributeValue
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- naming data types --
Packit Service f99136
Packit Service f99136
Name ::= CHOICE { -- only one possibility for now 
Packit Service f99136
	rdnSequence		RDNSequence 			
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
RDNSequence ::= SEQUENCE OF RelativeDistinguishedName
Packit Service f99136
Packit Service f99136
DistinguishedName ::= RDNSequence
Packit Service f99136
Packit Service f99136
RelativeDistinguishedName ::= 
Packit Service f99136
	SET OF AttributeTypeAndValue  --SET SIZE (1 .. MAX) OF
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- Directory string type --
Packit Service f99136
Packit Service f99136
DirectoryString ::= CHOICE {
Packit Service f99136
	teletexString		TeletexString,  --(SIZE (1..MAX)),
Packit Service f99136
	printableString		PrintableString,  --(SIZE (1..MAX)),
Packit Service f99136
	bmpString		BMPString,  --(SIZE (1..MAX)),
Packit Service f99136
	universalString		UniversalString,  --(SIZE (1..MAX)),
Packit Service f99136
	utf8String		UTF8String,  --(SIZE (1..MAX)),
Packit Service f99136
	ia5String		IA5String,  --added for EmailAddress,
Packit Service f99136
	integer			INTEGER
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- certificate and CRL specific structures begin here
Packit Service f99136
Packit Service f99136
Certificate ::= SEQUENCE  {
Packit Service f99136
	tbsCertificate		TBSCertificate,
Packit Service f99136
	signatureAlgorithm	AlgorithmIdentifier,
Packit Service f99136
	signature		BIT STRING
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
TBSCertificate  ::=  SEQUENCE  {
Packit Service f99136
	version		    [0] EXPLICIT Version OPTIONAL,  --DEFAULT v1
Packit Service f99136
	serialNumber		CertificateSerialNumber,
Packit Service f99136
	signature		AlgorithmIdentifier,
Packit Service f99136
	issuer			Name,
Packit Service f99136
	validity		Validity,
Packit Service f99136
	subject			Name,
Packit Service f99136
	subjectPublicKeyInfo	SubjectPublicKeyInfo,
Packit Service f99136
	issuerUniqueID	    [1] IMPLICIT UniqueIdentifier OPTIONAL,
Packit Service f99136
		-- If present, version shall be v2 or v3
Packit Service f99136
	subjectUniqueID	    [2] IMPLICIT UniqueIdentifier OPTIONAL,
Packit Service f99136
		-- If present, version shall be v2 or v3
Packit Service f99136
	extensions	    [3] EXPLICIT Extensions OPTIONAL
Packit Service f99136
		-- If present, version shall be v3
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
Version ::= INTEGER  --{  v1(0), v2(1), v3(2)  }
Packit Service f99136
Packit Service f99136
CertificateSerialNumber ::= INTEGER
Packit Service f99136
Packit Service f99136
Validity ::= SEQUENCE {
Packit Service f99136
	notBefore		Time,
Packit Service f99136
	notAfter		Time
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
Time ::= CHOICE {
Packit Service f99136
	utcTime			UTCTime,
Packit Service f99136
	generalTime		GeneralizedTime
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
UniqueIdentifier ::= BIT STRING
Packit Service f99136
Packit Service f99136
SubjectPublicKeyInfo ::= SEQUENCE {
Packit Service f99136
	algorithm		AlgorithmIdentifier,
Packit Service f99136
	subjectPublicKey	BIT STRING
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
Packit Service f99136
RSAPubKeyInfo ::=   SEQUENCE {
Packit Service f99136
	modulus INTEGER,
Packit Service f99136
	exponent INTEGER
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
Extensions ::= SEQUENCE OF Extension  --SIZE (1..MAX) OF Extension
Packit Service f99136
Packit Service f99136
Extension ::= SEQUENCE {
Packit Service f99136
	extnID			OBJECT IDENTIFIER,
Packit Service f99136
	critical		BOOLEAN OPTIONAL,  --DEFAULT FALSE,
Packit Service f99136
	extnValue		OCTET STRING
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
AlgorithmIdentifier ::= SEQUENCE {
Packit Service f99136
	algorithm		OBJECT IDENTIFIER,
Packit Service f99136
	parameters		ANY OPTIONAL
Packit Service f99136
	}
Packit Service f99136
Packit Service f99136
Packit Service f99136
--extensions
Packit Service f99136
Packit Service f99136
AuthorityKeyIdentifier ::= SEQUENCE {
Packit Service f99136
      keyIdentifier             [0] KeyIdentifier            OPTIONAL,
Packit Service f99136
      authorityCertIssuer       [1] GeneralNames             OPTIONAL,
Packit Service f99136
      authorityCertSerialNumber [2] CertificateSerialNumber  OPTIONAL }
Packit Service f99136
    -- authorityCertIssuer and authorityCertSerialNumber shall both
Packit Service f99136
    -- be present or both be absent
Packit Service f99136
Packit Service f99136
KeyIdentifier ::= OCTET STRING
Packit Service f99136
Packit Service f99136
SubjectKeyIdentifier ::= KeyIdentifier
Packit Service f99136
Packit Service f99136
-- key usage extension OID and syntax
Packit Service f99136
Packit Service f99136
-- id-ce-keyUsage OBJECT IDENTIFIER ::=  { id-ce 15 }
Packit Service f99136
Packit Service f99136
KeyUsage ::= BIT STRING --{
Packit Service f99136
--      digitalSignature        (0),
Packit Service f99136
--      nonRepudiation          (1),
Packit Service f99136
--      keyEncipherment         (2),
Packit Service f99136
--      dataEncipherment        (3),
Packit Service f99136
--      keyAgreement            (4),
Packit Service f99136
--      keyCertSign             (5),
Packit Service f99136
--      cRLSign                 (6),
Packit Service f99136
--      encipherOnly            (7),
Packit Service f99136
--      decipherOnly            (8) }
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- private key usage period extension OID and syntax
Packit Service f99136
Packit Service f99136
-- id-ce-privateKeyUsagePeriod OBJECT IDENTIFIER ::=  { id-ce 16 }
Packit Service f99136
Packit Service f99136
PrivateKeyUsagePeriod ::= SEQUENCE {
Packit Service f99136
     notBefore       [0]     GeneralizedTime OPTIONAL,
Packit Service f99136
     notAfter        [1]     GeneralizedTime OPTIONAL }
Packit Service f99136
     -- either notBefore or notAfter shall be present
Packit Service f99136
     
Packit Service f99136
-- certificate policies extension OID and syntax
Packit Service f99136
-- id-ce-certificatePolicies OBJECT IDENTIFIER ::=  { id-ce 32 }
Packit Service f99136
Packit Service f99136
CertificatePolicies ::= SEQUENCE OF PolicyInformation
Packit Service f99136
Packit Service f99136
PolicyInformation ::= SEQUENCE {
Packit Service f99136
     policyIdentifier   CertPolicyId,
Packit Service f99136
     policyQualifiers   SEQUENCE OF
Packit Service f99136
             PolicyQualifierInfo OPTIONAL }
Packit Service f99136
Packit Service f99136
CertPolicyId ::= OBJECT IDENTIFIER
Packit Service f99136
Packit Service f99136
PolicyQualifierInfo ::= SEQUENCE {
Packit Service f99136
       policyQualifierId  PolicyQualifierId,
Packit Service f99136
       qualifier        ANY } --DEFINED BY policyQualifierId }
Packit Service f99136
Packit Service f99136
-- Implementations that recognize additional policy qualifiers shall
Packit Service f99136
-- augment the following definition for PolicyQualifierId
Packit Service f99136
Packit Service f99136
PolicyQualifierId ::=
Packit Service f99136
     OBJECT IDENTIFIER --( id-qt-cps | id-qt-unotice )
Packit Service f99136
Packit Service f99136
-- CPS pointer qualifier
Packit Service f99136
Packit Service f99136
CPSuri ::= IA5String
Packit Service f99136
Packit Service f99136
-- user notice qualifier
Packit Service f99136
Packit Service f99136
UserNotice ::= SEQUENCE {
Packit Service f99136
     noticeRef        NoticeReference OPTIONAL,
Packit Service f99136
     explicitText     DisplayText OPTIONAL}
Packit Service f99136
Packit Service f99136
NoticeReference ::= SEQUENCE {
Packit Service f99136
     organization     DisplayText,
Packit Service f99136
     noticeNumbers    SEQUENCE OF INTEGER }
Packit Service f99136
Packit Service f99136
DisplayText ::= CHOICE {
Packit Service f99136
     visibleString    VisibleString  ,
Packit Service f99136
     bmpString        BMPString      ,
Packit Service f99136
     utf8String       UTF8String      }
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- policy mapping extension OID and syntax
Packit Service f99136
-- id-ce-policyMappings OBJECT IDENTIFIER ::=  { id-ce 33 }
Packit Service f99136
Packit Service f99136
PolicyMappings ::= SEQUENCE OF SEQUENCE {
Packit Service f99136
     issuerDomainPolicy      CertPolicyId,
Packit Service f99136
     subjectDomainPolicy     CertPolicyId }
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- subject alternative name extension OID and syntax
Packit Service f99136
-- id-ce-subjectAltName OBJECT IDENTIFIER ::=  { id-ce 17 }
Packit Service f99136
Packit Service f99136
SubjectAltName ::= GeneralNames
Packit Service f99136
Packit Service f99136
GeneralNames ::= SEQUENCE OF GeneralName
Packit Service f99136
Packit Service f99136
GeneralName ::= CHOICE {
Packit Service f99136
     otherName                       [0]     AnotherName,
Packit Service f99136
     rfc822Name                      [1]     IA5String,
Packit Service f99136
     dNSName                         [2]     IA5String,
Packit Service f99136
     x400Address                     [3]     ANY, --ORAddress,
Packit Service f99136
     directoryName                   [4]     Name,
Packit Service f99136
     ediPartyName                    [5]     EDIPartyName,
Packit Service f99136
     uniformResourceIdentifier       [6]     IA5String,
Packit Service f99136
     iPAddress                       [7]     OCTET STRING,
Packit Service f99136
     registeredID                    [8]     OBJECT IDENTIFIER }
Packit Service f99136
Packit Service f99136
EntrustVersionInfo ::= SEQUENCE {
Packit Service f99136
              entrustVers  GeneralString,
Packit Service f99136
              entrustInfoFlags EntrustInfoFlags }
Packit Service f99136
Packit Service f99136
EntrustInfoFlags::= BIT STRING --{
Packit Service f99136
--      keyUpdateAllowed
Packit Service f99136
--      newExtensions     (1),  -- not used
Packit Service f99136
--      pKIXCertificate   (2) } -- certificate created by pkix
Packit Service f99136
Packit Service f99136
-- AnotherName replaces OTHER-NAME ::= TYPE-IDENTIFIER, as
Packit Service f99136
-- TYPE-IDENTIFIER is not supported in the 88 ASN.1 syntax
Packit Service f99136
Packit Service f99136
AnotherName ::= SEQUENCE {
Packit Service f99136
     type    OBJECT IDENTIFIER,
Packit Service f99136
     value      [0] EXPLICIT ANY } --DEFINED BY type-id }
Packit Service f99136
Packit Service f99136
EDIPartyName ::= SEQUENCE {
Packit Service f99136
     nameAssigner            [0]     DirectoryString OPTIONAL,
Packit Service f99136
     partyName               [1]     DirectoryString }
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- issuer alternative name extension OID and syntax
Packit Service f99136
-- id-ce-issuerAltName OBJECT IDENTIFIER ::=  { id-ce 18 }
Packit Service f99136
Packit Service f99136
IssuerAltName ::= GeneralNames
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- id-ce-subjectDirectoryAttributes OBJECT IDENTIFIER ::=  { id-ce 9 }
Packit Service f99136
Packit Service f99136
SubjectDirectoryAttributes ::= SEQUENCE OF Attribute
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- basic constraints extension OID and syntax
Packit Service f99136
-- id-ce-basicConstraints OBJECT IDENTIFIER ::=  { id-ce 19 }
Packit Service f99136
Packit Service f99136
BasicConstraints ::= SEQUENCE {
Packit Service f99136
     cA                      BOOLEAN OPTIONAL, --DEFAULT FALSE,
Packit Service f99136
     pathLenConstraint       INTEGER OPTIONAL }
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- name constraints extension OID and syntax
Packit Service f99136
-- id-ce-nameConstraints OBJECT IDENTIFIER ::=  { id-ce 30 }
Packit Service f99136
Packit Service f99136
NameConstraints ::= SEQUENCE {
Packit Service f99136
     permittedSubtrees       [0]     GeneralSubtrees OPTIONAL,
Packit Service f99136
     excludedSubtrees        [1]     GeneralSubtrees OPTIONAL }
Packit Service f99136
Packit Service f99136
GeneralSubtrees ::= SEQUENCE OF GeneralSubtree
Packit Service f99136
Packit Service f99136
GeneralSubtree ::= SEQUENCE {
Packit Service f99136
     base                    GeneralName,
Packit Service f99136
     minimum         [0]     BaseDistance OPTIONAL, --DEFAULT 0,
Packit Service f99136
     maximum         [1]     BaseDistance OPTIONAL }
Packit Service f99136
Packit Service f99136
BaseDistance ::= INTEGER 
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- policy constraints extension OID and syntax
Packit Service f99136
-- id-ce-policyConstraints OBJECT IDENTIFIER ::=  { id-ce 36 }
Packit Service f99136
Packit Service f99136
PolicyConstraints ::= SEQUENCE {
Packit Service f99136
     requireExplicitPolicy           [0] SkipCerts OPTIONAL,
Packit Service f99136
     inhibitPolicyMapping            [1] SkipCerts OPTIONAL }
Packit Service f99136
Packit Service f99136
SkipCerts ::= INTEGER 
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- CRL distribution points extension OID and syntax
Packit Service f99136
-- id-ce-cRLDistributionPoints     OBJECT IDENTIFIER  ::=  {id-ce 31}
Packit Service f99136
Packit Service f99136
cRLDistributionPoints  ::= SEQUENCE OF DistributionPoint
Packit Service f99136
Packit Service f99136
DistributionPoint ::= SEQUENCE {
Packit Service f99136
     distributionPoint       [0]     DistributionPointName OPTIONAL,
Packit Service f99136
     reasons                 [1]     ReasonFlags OPTIONAL,
Packit Service f99136
     cRLIssuer               [2]     GeneralNames OPTIONAL }
Packit Service f99136
Packit Service f99136
DistributionPointName ::= CHOICE {
Packit Service f99136
     fullName                [0]     GeneralNames,
Packit Service f99136
     nameRelativeToCRLIssuer [1]     RelativeDistinguishedName }
Packit Service f99136
Packit Service f99136
ReasonFlags ::= BIT STRING --{
Packit Service f99136
--     unused                  (0),
Packit Service f99136
--     keyCompromise           (1),
Packit Service f99136
--     cACompromise            (2),
Packit Service f99136
--     affiliationChanged      (3),
Packit Service f99136
--     superseded              (4),
Packit Service f99136
--     cessationOfOperation    (5),
Packit Service f99136
--     certificateHold         (6),
Packit Service f99136
--     privilegeWithdrawn      (7),
Packit Service f99136
--     aACompromise            (8) }
Packit Service f99136
Packit Service f99136
Packit Service f99136
-- extended key usage extension OID and syntax
Packit Service f99136
-- id-ce-extKeyUsage OBJECT IDENTIFIER ::= {id-ce 37}
Packit Service f99136
Packit Service f99136
ExtKeyUsageSyntax ::= SEQUENCE OF KeyPurposeId
Packit Service f99136
Packit Service f99136
KeyPurposeId ::= OBJECT IDENTIFIER
Packit Service f99136
Packit Service f99136
-- extended key purpose OIDs
Packit Service f99136
-- id-kp-serverAuth      OBJECT IDENTIFIER ::= { id-kp 1 }
Packit Service f99136
-- id-kp-clientAuth      OBJECT IDENTIFIER ::= { id-kp 2 }
Packit Service f99136
-- id-kp-codeSigning     OBJECT IDENTIFIER ::= { id-kp 3 }
Packit Service f99136
-- id-kp-emailProtection OBJECT IDENTIFIER ::= { id-kp 4 }
Packit Service f99136
-- id-kp-ipsecEndSystem  OBJECT IDENTIFIER ::= { id-kp 5 }
Packit Service f99136
-- id-kp-ipsecTunnel     OBJECT IDENTIFIER ::= { id-kp 6 }
Packit Service f99136
-- id-kp-ipsecUser       OBJECT IDENTIFIER ::= { id-kp 7 }
Packit Service f99136
-- id-kp-timeStamping    OBJECT IDENTIFIER ::= { id-kp 8 }
Packit Service f99136
Packit Service f99136
-- authority info access
Packit Service f99136
Packit Service f99136
-- id-pe-authorityInfoAccess OBJECT IDENTIFIER ::= { id-pe 1 }
Packit Service f99136
Packit Service f99136
AuthorityInfoAccessSyntax  ::=
Packit Service f99136
        SEQUENCE OF AccessDescription --SIZE (1..MAX) OF AccessDescription
Packit Service f99136
Packit Service f99136
AccessDescription  ::=  SEQUENCE {
Packit Service f99136
        accessMethod          OBJECT IDENTIFIER,
Packit Service f99136
        accessLocation        GeneralName  }
Packit Service f99136
Packit Service f99136
-- subject info access
Packit Service f99136
Packit Service f99136
-- id-pe-subjectInfoAccess OBJECT IDENTIFIER ::= { id-pe 11 }
Packit Service f99136
Packit Service f99136
SubjectInfoAccessSyntax  ::=
Packit Service f99136
        SEQUENCE OF AccessDescription --SIZE (1..MAX) OF AccessDescription
Packit Service f99136
Packit Service f99136
-- pgp creation time
Packit Service f99136
Packit Service f99136
PGPExtension ::= SEQUENCE {
Packit Service f99136
       version             Version, -- DEFAULT v1(0)
Packit Service f99136
       keyCreation         Time
Packit Service f99136
}
Packit Service f99136
ASN1
Packit Service f99136
Packit Service f99136
btest $t++, my $parser = $asn->find('Certificate');
Packit Service f99136
btest $t++, my $crlp   = $asn->find('cRLDistributionPoints');
Packit Service f99136
Packit Service f99136
my %certs = (
Packit Service f99136
  't/aj.cer'           => ["http://rootca.allianz.com/ad-ca/ad-ca.crl"],
Packit Service f99136
  't/aj2.cer'          => ["http://rootca.allianz.com/sc-ad-ca/sc-ad-ca.crl"],
Packit Service f99136
  't/allianz_root.cer' => ["http://rootca.allianz.com/rootca.crl"],
Packit Service f99136
  't/pgpextension.der' => ["http://ca.mayfirst.org/mfpl.crl"],
Packit Service f99136
  't/subca_2.cer'      => [
Packit Service f99136
    "ldap://ldap.treas.gov/cn=CRL1,ou=US%20Treasury%20Root%20CA,ou=Certification%20Authorities,ou=Department%20of%20the%20Treasury,o=U.S.%20Government,c=US?authorityRevocationList"
Packit Service f99136
  ],
Packit Service f99136
  't/dsacert.der'           => undef,
Packit Service f99136
  't/new_root_ca.cer'       => undef,
Packit Service f99136
  't/telesec_799972029.crt' => undef,
Packit Service f99136
  't/verisign.der'          => undef,
Packit Service f99136
);
Packit Service f99136
Packit Service f99136
for my $file (sort keys %certs) {
Packit Service f99136
  print "# $file\n";
Packit Service f99136
  my $cert = loadcert($file);
Packit Service f99136
  btest $t++, my $data = $parser->decode($cert);
Packit Service f99136
  $data ||= {};
Packit Service f99136
  my $extns = $data->{tbsCertificate}{extensions} || [];
Packit Service f99136
Packit Service f99136
  my ($ext) = grep { $_->{'extnID'} eq '2.5.29.31' } @$extns;
Packit Service f99136
  if ($ext) {
Packit Service f99136
    my $points = $crlp->decode($ext->{'extnValue'});    # decode the value
Packit Service f99136
    $points = $points && $points->[0]->{'distributionPoint'}->{'fullName'};
Packit Service f99136
    btest $t++, !$crlp->error or warn($crlp->error);
Packit Service f99136
    my @points = grep $_, map { $_->{'uniformResourceIdentifier'} } @{$points || []};
Packit Service f99136
    rtest $t++, $certs{$file}, \@points;
Packit Service f99136
  }
Packit Service f99136
  else {
Packit Service f99136
    btest $t++, !$certs{$file};
Packit Service f99136
  }
Packit Service f99136
}
Packit Service f99136
Packit Service f99136
sub loadcert {
Packit Service f99136
  my $file = shift;
Packit Service f99136
  open FILE, $file || die "cannot load test certificate" . $file . "\n";
Packit Service f99136
  binmode FILE;    # HELLO Windows, dont fuss with this
Packit Service f99136
  my $holdTerminator = $/;
Packit Service f99136
  undef $/;        # using slurp mode to read the DER-encoded binary certificate
Packit Service f99136
  my $cert = <FILE>;
Packit Service f99136
  $/ = $holdTerminator;
Packit Service f99136
  close FILE;
Packit Service f99136
  return $cert;
Packit Service f99136
}