| 1 |
--- /dev/null |
| 2 |
+++ b/t/Crypt-X509.t |
| 3 |
@@ -0,0 +1,134 @@ |
| 4 |
+# Before `make install' is performed this script should be runnable with |
| 5 |
+# `make test'. After `make install' it should work as `perl Crypt-X509.t' |
| 6 |
+use Test::More tests => 71; |
| 7 |
+use Math::BigInt; |
| 8 |
+BEGIN { use_ok('Crypt::X509') } |
| 9 |
+ |
| 10 |
+$cert = loadcert('t/verisign.der'); |
| 11 |
+is( length $cert, 774, 'certificate file loaded' ); |
| 12 |
+$decoded = Crypt::X509->new( cert => $cert ); |
| 13 |
+ok( defined $decoded, 'new() returned something' ); |
| 14 |
+ok( $decoded->isa('Crypt::X509'), 'and it\'s the right class' ); |
| 15 |
+is( $decoded->error, undef, 'decode successful' ); |
| 16 |
+is( $decoded->not_after, 1848787199, 'not_after got parsed' ); |
| 17 |
+is( join( ',', @{ $decoded->Issuer } ), join( ',', @{ $decoded->Subject } ), 'Root CA: Subject equals Issuer' ); |
| 18 |
+ |
| 19 |
+$cert = loadcert('t/aj.cer'); |
| 20 |
+$decoded2 = Crypt::X509->new( cert => $cert ); |
| 21 |
+is( $decoded2->error, undef, 'decode successful' ); |
| 22 |
+is( join( ':', @{ $decoded2->KeyUsage } ), "critical:digitalSignature:keyEncipherment:dataEncipherment", 'Keyusagecheck' ); |
| 23 |
+is( join( ':', @{ $decoded2->ExtKeyUsage } ), "clientAuth:emailProtection", 'Extkeyusagecheck' ); |
| 24 |
+# this has also to work twice |
| 25 |
+is( join( ':', @{ $decoded2->KeyUsage } ), "critical:digitalSignature:keyEncipherment:dataEncipherment", 'Keyusagecheck again' ); |
| 26 |
+is( join( ':', @{ $decoded2->ExtKeyUsage } ), "clientAuth:emailProtection", 'Extkeyusagecheck again' ); |
| 27 |
+is( join( ',', @{ $decoded2->Subject } ), "emailAddress=alexander.jung\@allianz.de,C=DE,O=Allianz Group,CN=Alexander Jung", 'Subject parsed' ); |
| 28 |
+is( $decoded2->subject_country, "DE", "Subject_country" ); |
| 29 |
+is( $decoded2->subject_state, undef, "Subject_state" ); |
| 30 |
+is( $decoded2->subject_org, "Allianz Group", "Subject_org" ); |
| 31 |
+is( $decoded2->subject_ou, undef, "Subject_ou" ); |
| 32 |
+is( $decoded2->subject_email, "alexander.jung\@allianz.de", "Subject_email" ); |
| 33 |
+is_deeply( $decoded2->SubjectRaw, [ |
| 34 |
+ {'type' => '1.2.840.113549.1.9.1','value' => '[email protected]','format' => 'ia5String'}, |
| 35 |
+ {'type' => '2.5.4.6','value' => 'DE','format' => 'printableString'}, |
| 36 |
+ {'type' => '2.5.4.10','value' => 'Allianz Group','format' => 'printableString'}, |
| 37 |
+ {'type' => '2.5.4.3','value' => 'Alexander Jung','format' => 'printableString'} |
| 38 |
+]); |
| 39 |
+is( join( ',', @{ $decoded2->Issuer } ), "C=DE,O=Allianz Group,CN=Allianz Dresdner CA", "Issuer Parsed"); |
| 40 |
+is( $decoded2->issuer_cn, "Allianz Dresdner CA", "Issuer_cn" ); |
| 41 |
+is( $decoded2->issuer_country, "DE", "Isssuer_country" ); |
| 42 |
+is( $decoded2->issuer_state, undef, "Issuer_state" ); |
| 43 |
+is( $decoded2->issuer_locality, undef, "Issuer_locality" ); |
| 44 |
+is( $decoded2->issuer_org, "Allianz Group", "Issuer_org" ); |
| 45 |
+is( $decoded2->issuer_email, undef, "Issuer_email" ); |
| 46 |
+is( $decoded2->pubkey_algorithm, "1.2.840.113549.1.1.1", "pubkey_algorithm" ); |
| 47 |
+is( $decoded2->sig_algorithm, "1.2.840.113549.1.1.5", "sig_algorithm" ); |
| 48 |
+is( length( $decoded2->pubkey ), 140, "Pubkey length" ); |
| 49 |
+is( length( $decoded2->signature ), 256, "Signature Length" ); |
| 50 |
+is( join( ',', @{ $decoded2->SubjectAltName } ), "rfc822Name=alexander.jung\@allianz.de", 'SubjectAltName parsed' ); |
| 51 |
+is_deeply( $decoded2->DecodedSubjectAltNames, [[{rfc822Name => '[email protected]'}]], 'DecodedSubjectAltName parsed' ); |
| 52 |
+ |
| 53 |
+ |
| 54 |
+$cert = loadcert('t/aj2.cer'); |
| 55 |
+$decoded3 = Crypt::X509->new( cert => $cert ); |
| 56 |
+is( $decoded3->error, undef, 'decode successful' ); |
| 57 |
+is( join( ':', @{ $decoded3->KeyUsage } ), "critical:digitalSignature:keyAgreement", 'KeyUsage Check AuthCert' ); |
| 58 |
+ |
| 59 |
+$cert = loadcert('t/allianz_root.cer'); |
| 60 |
+$decoded = Crypt::X509->new( cert => $cert ); |
| 61 |
+is( $decoded->error, undef, 'decode successful' ); |
| 62 |
+is( join( ',', @{ $decoded->authorityCertIssuer } ), "C=DE,O=Allianz Group,CN=Allianz Group Root CA", "authorityCertIssuer" ); |
| 63 |
+is( $decoded->CRLDistributionPoints->[0], "http://rootca.allianz.com/rootca.crl", "CRLDistributionPoints" ); |
| 64 |
+is( $decoded->authority_cn, "Allianz Group Root CA", "authority_cn" ); |
| 65 |
+is( $decoded->authority_country, "DE", "authority_country" ); |
| 66 |
+is( $decoded->authority_state, undef, "authority_state" ); |
| 67 |
+is( $decoded->authority_locality, undef, "authority_locality" ); |
| 68 |
+is( $decoded->authority_org, "Allianz Group", "authority_org" ); |
| 69 |
+is( $decoded->authority_email, undef, "authority_email" ); |
| 70 |
+is( $decoded->pubkey_components()->{modulus}, Math::BigInt::->new('0x00d0a158415c62152e7b342f5881e5bca0842089d583929265af37099d4b4208f149c7084a3eec548fae81823884bbf0f7aee254c44a5a956eafd531b97cb7e9f7e88c9ebfb15b1126cc2edc616ea8b3be3af23a61e8ee0a5ea5af100f0bc7e3f5fcc45acf3f956f3073186fa7e815e853588d02d7942f36680e88c501c70b3d91be3fe96548a352355678c8088d6ac5bda1e9187a05dc3119c68bea1f8ee2acf0a6d261099eeaf10ca8fd380da43eb31010b4015f9e9fc0ab2075fb0b5f56796010cf46760cc73c058a1ec8dcb39e04079025446a2a45a4188bfbb3e299afb01cb5332ee56f72dce6c46fb2f6b5956afb5fece48a6ec6f82313effa057ecd874b'), 'pubkey_components modulus'); |
| 71 |
+is( $decoded->pubkey_components()->{exponent}, Math::BigInt::->new('0x10001'), 'pubkey_components exponent' ); |
| 72 |
+ |
| 73 |
+$cert = loadcert('t/new_root_ca.cer'); |
| 74 |
+$decoded = Crypt::X509->new( cert => $cert ); |
| 75 |
+is( $decoded->error, undef, 'decode of new_root_ca.cer successful' ); |
| 76 |
+is( join( ', ', @{ $decoded->BasicConstraints } ), 'critical, cA = 1', 'Basic Constraints' ); |
| 77 |
+is( $decoded->EntrustVersion, 'V7.1:4.0', 'Entrust Version' ); |
| 78 |
+is( $decoded->version_string, 'v3', 'certificate version string' ); |
| 79 |
+%SIA = $decoded->SubjectInfoAccess; |
| 80 |
+is( $SIA{'1.3.6.1.5.5.7.48.5'}[0], 'uniformResourceIdentifier = http://pki.treas.gov/root_sia.p7c', 'Subject Info Access' ); |
| 81 |
+ |
| 82 |
+$cert = loadcert('t/subca_2.cer'); |
| 83 |
+$decoded = Crypt::X509->new( cert => $cert ); |
| 84 |
+is( $decoded->error, undef, 'decode of subca_2.cer successful' ); |
| 85 |
+%CDPs = $decoded->CRLDistributionPoints2; |
| 86 |
+is( $CDPs{'1'}[0],'Directory Address: CN=CRL1,OU=US Treasury Root CA,OU=Certification Authorities,OU=Department of the Treasury,O=U.S. Government,C=US', 'CRL Distribution Points' ); |
| 87 |
+is( unpack( "H*", $decoded->subject_keyidentifier ), '86595f93caf32da620a4f9595a4a935370e792c9', 'subject key identifier' ); |
| 88 |
+ |
| 89 |
+$cert = loadcert('t/telesec_799972029.crt'); |
| 90 |
+$decoded = Crypt::X509->new( cert => $cert ); |
| 91 |
+( $sec, $min, $hour, $mday, $mon, $year,, ) = gmtime( $decoded->not_after ); |
| 92 |
+is( $decoded->not_after, 1111826160, 'not_after got parsed' ); |
| 93 |
+is( $sec, 0, "generalTime Seconds" ); |
| 94 |
+is( $min, 36, "generalTime Minutess" ); |
| 95 |
+is( $hour, 8, "generalTime hours" ); |
| 96 |
+is( $mday, 26, "generalTime day" ); |
| 97 |
+is( $mon + 1, 3, "generalTime month" ); |
| 98 |
+is( $year + 1900, 2005, "generalTime year" ); |
| 99 |
+is( join( ',', @{ $decoded->Issuer } ), 'C=DE,O=Deutsche Telekom AG,nameDistinguisher=1,CN=NKS CA 6:PN', 'Issuer for telesec' ); |
| 100 |
+is( join( ',', @{ $decoded->Subject } ), 'C=DE,nameDistinguisher=2,CN=Schefe, Jan', 'Subject for telesec' ); |
| 101 |
+is_deeply( $decoded->DecodedSubjectAltNames, [[ |
| 102 |
+ {otherName => {value => "0\x111\x0f0\x0d\x06\x03U\x04\x04\x14\x06Schefe", type => '0.2.262.1.10.3.0'}}, |
| 103 |
+ {otherName => {value => "0\x0e1\x0c0\x0a\x06\x03U\x04*\x14\x03Jan", type => '0.2.262.1.10.3.0'}}]], |
| 104 |
+ 'DecodedSubjectAltNames for telesec'); |
| 105 |
+ |
| 106 |
+$cert = loadcert('t/dsacert.der'); |
| 107 |
+$decoded = Crypt::X509->new( cert => $cert ); |
| 108 |
+is( $decoded->error, undef, 'decode of dsacert.der successful' ); |
| 109 |
+ |
| 110 |
+#test parser after invalid cert has been loaded |
| 111 |
+$invalid_cert = Crypt::X509->new( cert => 'invalid' ); |
| 112 |
+ok( $invalid_cert->error, 'got error on invalid data' ); |
| 113 |
+$cert = loadcert('t/verisign.der'); |
| 114 |
+$valid_cert = Crypt::X509->new( cert => $cert ); |
| 115 |
+ok( defined $valid_cert, 'new() returned something' ); |
| 116 |
+is( $valid_cert->error, undef, 'decode successful' ); |
| 117 |
+is( $valid_cert->not_after, 1848787199, 'not_after got parsed' ); |
| 118 |
+ |
| 119 |
+$cert = loadcert('t/pgpextension.der'); |
| 120 |
+$decoded = Crypt::X509->new( cert => $cert ); |
| 121 |
+is( $decoded->error, undef, 'decode of pgpextension.der successful' ); |
| 122 |
+is( $decoded->SigHashAlg, 'SHA512', 'Detecting SHA512 correctly' ); |
| 123 |
+is( $decoded->PGPExtension, 1292907852, 'creation time matched' ); |
| 124 |
+ |
| 125 |
+exit(); |
| 126 |
+ |
| 127 |
+sub loadcert { |
| 128 |
+ my $file = shift; |
| 129 |
+ open FILE, $file || die "cannot load test certificate" . $file . "\n"; |
| 130 |
+ binmode FILE; # HELLO Windows, dont fuss with this |
| 131 |
+ my $holdTerminator = $/; |
| 132 |
+ undef $/; # using slurp mode to read the DER-encoded binary certificate |
| 133 |
+ my $cert = <FILE>; |
| 134 |
+ $/ = $holdTerminator; |
| 135 |
+ close FILE; |
| 136 |
+ return $cert; |
| 137 |
+} |