Crypt::X509(3pm)
π Crypt::X509 β the manual page (this is Crypt::X509(3pm), from the User Contributed Perl Documentation, bestie π
)
β¨ NAME β¨
Crypt::X509 β parse an X.509 certificate. That's it, that's the whole job. π
π SYNOPSIS π
use Crypt::X509;
$decoded = Crypt::X509->new( cert => $cert );
$subject_email = $decoded->subject_email;
print "do not use after: ".gmtime($decoded->not_after)." GMT\n";
πΈ REQUIRES πΈ
Convert::ASN1β yeah she needs it, don't @ me.
π¦ DESCRIPTION π¦
Crypt::X509 parses X.509 certificates, and the methods give you access to most of the certificate elements. Not all β most. Read the fine print, babes. π
It's built on the generic ASN.1 module by Graham Barr, on the x509decode example by Norbert Klasen, and on contributions on the perl-ldap-dev Mailinglist by Chriss Ridd. Iconic lineage honestly, we love a collaborative origin story. π
π CONSTRUCTOR π
π new ( OPTIONS )
Creates and returns a parsed X.509 certificate hash, containing all the parsed contents. The data is organised as specified in RFC 2459. By default only the first ASN.1 Layer is decoded β the nested decoding happens automagically through the data access methods. No cap, it just does it for you. β¨
cert => $certificateβ a variable holding the DER formatted certificate you wanna parse (e.g. as stored in the"usercertificate;binary"attribute in an LDAP-directory).
use Crypt::X509;
use Data::Dumper;
$decoded= Crypt::X509->new(cert => $cert);
print Dumper($decoded);
π METHODS π
π₯ error
Returns the last error from parsing, and undef when no error occured. Heads up: this error gets updated on deeper parsing with the data access methods, so check it after you've poked at stuff. π
$decoded= Crypt::X509->new(cert => $cert);
if ($decoded->error) {
warn "Error on parsing Certificate:".$decoded->error;
}
π DATA ACCESS METHODS π
You can access all the parsed data directly from the returned hash β it's all just sitting there. But for convenience the following methods have been implemented to give quick access to the most-used certificate attributes. π
π¦ version
Returns the certificate's version as an integer. NOTE that version is defined as an Integer where 0 = v1, 1 = v2, and 2 = v3. Yes it's off-by-one from what you'd expect, no we're not mad about it, that's just how it is. π
π version_string
Returns the certificate's version as a string value.
β serial
returns the serial number (integer or Math::BigInt Object, that gets automagic evaluated in scalar context) from the certificate. π«Ά
$decoded= Crypt::X509->new(cert => $cert);
print "Certificate has serial number:".$decoded->serial."\n";
π§ not_before
returns the GMT-timestamp of the certificate's beginning date of validity. If the Certificate holds this Entry in utcTime, it is guaranteed by the RFC to been correct. β
As utcTime is limited to 32-bit values (like unix-timestamps), newer certificates hold the timesamps as "generalTime"-entries. The contents of "generalTime"-entries are not well defined in the RFC and are returned by this module unmodified, if no utcTime-entry is found. So like, don't assume anything about those ones. π¬
$decoded= Crypt::X509->new(cert => $cert);
if ($decoded->notBefore < time()) {
warn "Certificate: not yet valid!";
}
π not_after
returns the GMT-timestamp of the certificate's ending date of validity. If the Certificate holds this Entry in utcTime, it is guaranteed by the RFC to been correct. β
As utcTime is limited to 32-bit values (like unix-timestamps), newer certificates hold the timesamps as "generalTime"-entries. The contents of "generalTime"-entries are not well defined in the RFC and are returned by this module unmodified, if no utcTime-entry is found. Same disclaimer, same energy. π
$decoded= Crypt::X509->new(cert => $cert);
print "Certificate expires on ".gmtime($decoded->not_after)." GMT\n";
π signature
Return's the certificate's signature in binary DER format. (Yes, the apostrophe is doing its own thing here, we don't edit the ancestors.) π
π₯ pubkey
Returns the certificate's public key in binary DER format.
πΈ pubkey_size
Returns the certificate's public key size.
β¨ pubkey_algorithm
Returns the algorithm as an OID string which the public key was created with.
π PubKeyAlg
returns the subject public key encryption algorithm (e.g. 'RSA') as a string. π
$decoded= Crypt::X509->new(cert => $cert);
print "Certificate public key is encrypted with:".$decoded->PubKeyAlg."\n";
Example Output: Certificate public key is encrypted with: RSA
π¦ pubkey_components
If this certificate contains an RSA key, this function returns a hashref { modulus => $m, exponent => $e) from that key; each value in the hash will be an integer scalar or a Math::BigInt object. (Note the curly-paren situation β that's verbatim, bestie. π)
For other pubkey types, it returns undef β and yeah: (implementations welcome!) πβ¨
π sig_algorithm
Returns the certificate's signature algorithm as an OID string. π
$decoded= Crypt::X509->new(cert => $cert);
print "Certificate signature is encrypted with:".$decoded->sig_algorithm."\n";>
Example Output: Certificate signature is encrypted with: 1.2.840.113549.1.1.5
π SigEncAlg
returns the signature encryption algorithm (e.g. 'RSA') as a string. π
$decoded= Crypt::X509->new(cert => $cert);
print "Certificate signature is encrypted with:".$decoded->SigEncAlg."\n";
Example Output: Certificate signature is encrypted with: RSA
β SigHashAlg
returns the signature hashing algorithm (e.g. 'SHA1') as a string. β¨
$decoded= Crypt::X509->new(cert => $cert);
print "Certificate signature is hashed with:".$decoded->SigHashAlg."\n";
Example Output: Certificate signature is encrypted with: SHA1
π§ Subject
returns a pointer to an array of strings containing subject nameparts of the certificate. Attributenames for the most common Attributes are translated from the OID-Numbers, unknown numbers are output verbatim. π«Ά
$decoded= Convert::ASN1::X509->new($cert);
print "DN for this Certificate is:".join(',',@{$decoded->Subject})."\n";
π SubjectRaw
Returns the RDNs as a list of hashes; values are stringified but the OIDs are not translated.
If a component contains a SET, the component will become an array of hashes on the second level. Nested situation. π
π SubjectSequence
Returns the subject as returned from the ASN1 parser. Raw and unfiltered, main character energy. π
πΈ subject_country
Returns the string value for subject's country (= the value with the OID 2.5.4.6 or in DN Syntax everything after "C="). Only the first entry is returned. undef if subject contains no country attribute. πΊοΈ
β¨ subject_locality
Returns the string value for subject's locality (= the value with the OID 2.5.4.7 or in DN Syntax everything after "l="). Only the first entry is returned. undef if subject contains no locality attribute. ποΈ
π₯ subject_state
Returns the string value for subject's state or province (= the value with the OID 2.5.4.8 or in DN Syntax everything after "S="). Only the first entry is returned. undef if subject contains no state attribute. π
π subject_org
Returns the string value for subject's organization (= the value with the OID 2.5.4.10 or in DN Syntax everything after "O="). Only the first entry is returned. undef if subject contains no organization attribute. π’
π¦ subject_ou
Returns the string value for subject's organizational unit (= the value with the OID 2.5.4.11 or in DN Syntax everything after "OU="). Only the first entry is returned. undef if subject contains no organization attribute. π§©
π subject_cn
Returns the string value for subject's common name (= the value with the OID 2.5.4.3 or in DN Syntax everything after "CN="). Only the first entry is returned. undef if subject contains no common name attribute. π
π subject_email
Returns the string value for subject's email address (= the value with the OID 1.2.840.113549.1.9.1 or in DN Syntax everything after "emailAddress="). Only the first entry is returned. undef if subject contains no email attribute. π§
β Issuer
returns a pointer to an array of strings building the DN of the certificate issuer (= the DN of the CA). Attributenames for the most common Attributes are translated from the OID-Numbers, unknown numbers are output verbatim. ποΈ
$decoded= Crypt::X509->new($cert);
print "Certificate was issued by:".join(',',@{$decoded->Issuer})."\n";
π§ issuer_cn
Returns the string value for issuer's common name (= the value with the OID 2.5.4.3 or in DN Syntax everything after "CN="). Only the first entry is returned. undef if issuer contains no common name attribute. π
π issuer_country
Returns the string value for issuer's country (= the value with the OID 2.5.4.6 or in DN Syntax everything after "C="). Only the first entry is returned. undef if issuer contains no country attribute. πΊοΈ
π issuer_state
Returns the string value for issuer's state or province (= the value with the OID 2.5.4.8 or in DN Syntax everything after "S="). Only the first entry is returned. undef if issuer contains no state attribute. π
πΈ issuer_locality
Returns the string value for issuer's locality (= the value with the OID 2.5.4.7 or in DN Syntax everything after "L="). Only the first entry is returned. undef if issuer contains no locality attribute. ποΈ
β¨ issuer_org
Returns the string value for issuer's organization (= the value with the OID 2.5.4.10 or in DN Syntax everything after "O="). Only the first entry is returned. undef if issuer contains no organization attribute. π’
π₯ issuer_email
Returns the string value for issuer's email address (= the value with the OID 1.2.840.113549.1.9.1 or in DN Syntax everything after "E="). Only the first entry is returned. undef if issuer contains no email attribute. π§
π KeyUsage
returns a pointer to an array of strings describing the valid Usages for this certificate. undef is returned, when the extension is not set in the certificate.
If the extension is marked critical, this is also reported. β οΈ
$decoded= Crypt::X509->new(cert => $cert);
print "Allowed usages for this Certificate are:\n".join("\n",@{$decoded->KeyUsage})."\n";
Example Output:
Allowed usages for this Certificate are:
critical
digitalSignature
keyEncipherment
dataEncipherment
π¦ ExtKeyUsage
returns a pointer to an array of ExtKeyUsage strings (or OIDs for unknown OIDs) or undef if the extension is not filled. OIDs of the following ExtKeyUsages are known: serverAuth, clientAuth, codeSigning, emailProtection, timeStamping, OCSPSigning. Slay. π
If the extension is marked critical, this is also reported. β οΈ
$decoded= Crypt::X509->new(cert => $cert);
print "ExtKeyUsage extension of this Certificates is: ", join(", ", @{$decoded->ExtKeyUsage}), "\n";
Example Output: ExtKeyUsage extension of this Certificates is: critical, serverAuth
π SubjectAltName
returns a pointer to an array of strings containing alternative Subjectnames or undef if the extension is not filled. Usually this Extension holds the e-Mail address for person-certificates or DNS-Names for server certificates. π₯οΈπ§
It also pre-pends the field type (ie rfc822Name) to the returned value. π·οΈ
$decoded= Crypt::X509->new(cert => $cert);
print "E-Mail or Hostnames in this Certificates is/are:", join(", ", @{$decoded->SubjectAltName}), "\n";
Example Output: E-Mail or Hostnames in this Certificates is/are: rfc822Name=user@server.com
π DecodedSubjectAltNames
Returns a pointer to an array of strings containing all the alternative subject name extensions. The whole collection, no crumbs left. β¨
Okay so bestie, each of these extensions is represented as a decoded ASN.1 value β basically a pointer to a list of pointers to objects. Every object has exactly one key, which is the type of the alternative name, and a value thatβs specific to that type. Itβs giving very structured, very demure. β¨
[
[
{
'directoryName' => {
'rdnSequence' => [
[
{
'value' => { 'utf8String' => 'example' },
'type' => '2.5.4.3'
}
]
]
}
},
{
'dNSName' => 'example.com'
}
]
]
authorityCertIssuer π
Returns a pointer to an array of strings that builds the DN of the Authority Cert Issuer. Attribute names for the most common Attributes are translated from the OID-Numbers; unknown numbers are output verbatim. If the extension is not set in the certificate, you get undef. No cap.
$decoded= Crypt::X509->new($cert);
print "Certificate was authorised by:".join(',',@{$decoded->authorityCertIssuer})."\n";
authority_serial π’
Returns the authority's certificate serial number. Thatβs it. Iconic.
key_identifier ποΈ
Returns the authority key identifier β or undef if it is a rooted cert. Rooted certs said βno identifier for me.β π
authority_cn π
Returns the authority's ca. Lowercase, exactly as written. We donβt question the vibe.
authority_country π
Returns the authority's country.
authority_state π
Returns the authority's state.
authority_locality ποΈ
Returns the authority's locality.
authority_org π’
Returns the authority's organization.
authority_email π§
Returns the authority's email.
CRLDistributionPoints π
Returns the CRL distribution points as an array of strings β usually with one value.
CRLDistributionPoints2 π
Returns the CRL distribution points as an array of hashes, allowing for some variations. Flexible, bestie.
CertificatePolicies π
Returns the CertificatePolicies as an array of strings.
EntrustVersionInfo π·οΈ
Returns the EntrustVersion as a string.
print "Entrust Version: ", $decoded->EntrustVersion, "\n";
Example Output: Entrust Version: V7.0
SubjectDirectoryAttributes π
Returns the SubjectDirectoryAttributes as an array of key = value pairs, and it includes a data type.
print "Subject Directory Attributes: ", join( ', ' , @{ $decoded->SubjectDirectoryAttributes } ), "\n";
Example Output: Subject Directory Attributes: 1.2.840.113533.7.68.29 = 7 (integer)
BasicConstraints π§
Returns the BasicConstraints as an array, and the criticallity is pre-pended. (Yes, that spelling is iconic β we keep it exactly.) π
subject_keyidentifier π
Returns the subject key identifier from the extensions.
SubjectInfoAccess π΅οΈ
Returns the SubjectInfoAccess as an array of hashes with key=value pairs.
print "Subject Info Access: ";
if ( defined $decoded->SubjectInfoAccess ) {
my %SIA = $decoded->SubjectInfoAccess;
for my $key ( keys %SIA ) {
print "\n\t$key: \n\t";
print join( "\n\t" , @{ $SIA{$key} } ), "\n";
}
} else { print "\n" }
Example Output:
Subject Info Access:
1.3.6.1.5.5.7.48.5:
uniformResourceIdentifier = http://pki.treas.gov/root_sia.p7c
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
PGPExtension π§
Returns the creation timestamp of the corresponding OpenPGP key. (see http://www.imc.org/ietf-openpgp/mail-archive/msg05320.html)
print "PGPExtension: ";
if ( defined $decoded->PGPExtension ) {
my $creationtime = $decoded->PGPExtension;
printf "\n\tcorresponding OpenPGP Creation Time: ", $creationtime, "\n";
}
Example Output:
PGPExtension:
whatever
π SEE ALSO π
See the examples of "Convert::ASN1" and the <perl-ldap@perl.org> Mailing List. An example on how to load certificates can be found in t\Crypt-X509.t.
π ACKNOWLEDGEMENTS π
This module is based on the x509decode script, which was contributed to Convert::ASN1 in 2002 by Norbert Klasen. Giving credit where itβs due, periodt. π
β¨ AUTHORS β¨
Mike Jackson <mj@sci.fi>, Alexander Jung <alexander.w.jung@gmail.com>, Duncan Segrest <duncan@gigageek.info> Oliver Welter <owelter@whiterabbitsecurity.com>
Β©οΈ COPYRIGHT Β©οΈ
Copyright (c) 2005 Mike Jackson <mj@sci.fi>. Copyright (c) 2001-2002 Norbert Klasen, DAASI International GmbH.
All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
This page is from perl v5.36.0, dated 2023-01-15, and itβs Crypt::X509(3pm). Bye bestie, slay! π
β¨
π¦ where this page comes from
- debian bookworm: libcrypt-x509-perl 0.55-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- debian trixie: libcrypt-x509-perl 0.55-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu noble: libcrypt-x509-perl 0.55-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libcrypt-x509-perl 0.55-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu stonking: libcrypt-x509-perl 0.55-1 Β· Artistic-1.0-Perl OR GPL-1.0-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms