IO::Soc...::Utils(3)

✨ NAME ✨

IO::Socket::SSL::Utils β€” loading, storing, creating certificates and keys πŸŽ€


🌸 SYNOPSIS 🌸

okay bestie, here's the vibe, literally copy-paste ready:

use IO::Socket::SSL::Utils;

$cert = PEM_file2cert('cert.pem');     # load certificate from file
my $hash = CERT_asHash($cert);         # get details from certificate
PEM_cert2file($cert,'cert.pem');       # write certificate to file
CERT_free($cert);                      # free memory within OpenSSL

@certs = PEM_file2certs('chain.pem');  # load multiple certificates from file
PEM_certs2file('chain.pem', @certs);   # write multiple certificates to file
CERT_free(@certs);                     # free memory for all within OpenSSL

my $cert = PEM_string2cert($pem);      # load certificate from PEM string
$pem = PEM_cert2string($cert);         # convert certificate to PEM string

$key = KEY_create_rsa(2048);           # create new 2048-bit RSA key
PEM_key2file($key,"key.pem");          # and write it to file
KEY_free($key);                        # free memory within OpenSSL

πŸ¦‹ DESCRIPTION πŸ¦‹

so this module is basically your little helper squad for certificates and private keys 🫢 it hands you a bunch of utility functions so you don't have to rawdog the complexity of the underlying Net::SSLeay and OpenSSL. it's giving "let me handle the scary parts for you", and honestly? iconic.


πŸ’… FUNCTIONS πŸ’…

πŸŽ€ Functions converting between string or file and certificates and keys

these do the convert-y stuff. heads up: they croak if the operation cannot be completed, so like, no silent failing, she will tell you. 😀

PEM_file2cert(file) -> cert
PEM_cert2file(cert,file)
PEM_file2certs(file) -> @certs
PEM_certs2file(file,@certs)
PEM_string2cert(string) -> cert
PEM_cert2string(cert) -> string
PEM_file2key(file) -> key
PEM_key2file(key,file)
PEM_string2key(string) -> key
PEM_key2string(key) -> string

🧚 Functions for cleaning up

the memory hygiene era ✨ every cert and key you load or create must be freed, otherwise you're just leaking memory like it's a broken faucet. not cute.

CERT_free(@certs)
KEY_free(@keys)

πŸ”₯ KEY_create_rsa(bits) -> key

creates an RSA key pair. bits defaults to 2048 πŸ’ͺ

🌈 KEY_create_ec(curve) -> key

creates an EC key. curve defaults to "prime256v1" 🌿

πŸ’œ CERT_asHash(cert,[digest_algo]) -> hash

extracts all the info out of the certificate into a hash, and uses the given digest_algo (default: SHA-256) to figure out the digest of pubkey and cert. the resulting hash contains:

  • subject β€” hash with the parts of the subject, e.g. commonName, countryName, organizationName, stateOrProvinceName, localityName. if there are multiple values for any of these parts, the hash value will be an array ref with the values in order instead of just a scalar. periodt.
  • subjectAltNames β€” array with list of alternative names. each entry in the list is of "[type,value]", where "type" can be OTHERNAME, EMAIL, DNS, X400, DIRNAME, EDIPARTY, URI, IP or RID.
  • issuer β€” hash with the parts of the issuer, e.g. commonName, countryName, organizationName, stateOrProvinceName, localityName. if there are multiple values for any of these parts the hash value will be an array ref with the values in order instead of just a scalar.
  • not_before, not_after β€” the time frame where the certificate is valid, as time_t, e.g. can be converted with localtime or similar functions. ⏰
  • serial β€” the serial number
  • crl_uri β€” list of URIs for CRL distribution.
  • ocsp_uri β€” list of URIs for revocation checking using OCSP.
  • keyusage β€” list of keyUsage information in the certificate.
  • extkeyusage β€” list of extended key usage information from the certificate. each entry in this list consists of a hash with oid, nid, ln and sn.
  • pubkey_digest_xxx β€” binary digest of the pubkey using the given digest algorithm, e.g. pubkey_digest_sha256 if (the default) SHA-256 was used.
  • x509_digest_xxx β€” binary digest of the X.509 certificate using the given digest algorithm, e.g. x509_digest_sha256 if (the default) SHA-256 was used.
  • fingerprint_xxx β€” fingerprint of the certificate using the given digest algorithm, e.g. fingerprint_sha256 if (the default) SHA-256 was used. contrary to digest_* this is an ASCII string with a list if hexadecimal numbers, e.g. "73:59:75:5C:6D...".
  • signature_alg β€” algorithm used to sign certificate, e.g. "sha256WithRSAEncryption".
  • ext β€” list of extensions. each entry in the list is a hash with oid, nid, sn, critical flag (boolean) and data (string representation given by X509V3_EXT_print).
  • version β€” certificate version, usually 2 (x509v3)

πŸ‘‘ CERT_create(hash) -> (cert,key)

creates a certificate based on the given hash. if the issuer is not specified, the certificate will be self-signed (main character energy, no permission needed πŸ’…). the following keys can be given:

  • subject β€” hash with the parts of the subject, e.g. commonName, countryName, ... as described in "CERT_asHash". default points to IO::Socket::SSL.
  • not_before β€” a time_t value when the certificate starts to be valid. defaults to current time.
  • not_after β€” a time_t value when the certificate ends to be valid. defaults to current time plus one 365 days.
  • serial β€” the serial number. if not given a random number will be used. 🎲
  • version β€” the version of the certificate, default 2 (x509v3).
  • CA true|false β€” if true declare certificate as CA, defaults to false.
  • purpose string|array|hash β€” set the purpose of the certificate. the different purposes can be given as a string separated by non-word character, as array or hash. with string or array each purpose can be prefixed with '+' (enable) or '-' (disable) and same can be done with the value when given as a hash. by default enabling the purpose is assumed.

if the CA option is given and true the defaults "ca,sslca,emailca,objca" are assumed, but can be overridden with explicit purpose. if the CA option is given and false the defaults "server,client" are assumed. if no CA option and no purpose is given it defaults to "server,client".

purpose affects basicConstraints, keyUsage, extKeyUsage and netscapeCertType. the following purposes are defined (case is not important):

client
server
email
objsign

CA
sslCA
emailCA
objCA

emailProtection
codeSigning
timeStamping

digitalSignature
nonRepudiation
keyEncipherment
dataEncipherment
keyAgreement
keyCertSign
cRLSign
encipherOnly
decipherOnly

examples:

 # root-CA for SSL certificates
 purpose => 'sslCA'   # or CA => 1

 # server certificate and CA (typically self-signed)
 purpose => 'sslCA,server'

 # client certificate
 purpose => 'client',
  • ext [{ sn => .., data => ... }, ... ] β€” list of extensions. the type of the extension can be specified as name with "sn" or as NID with "nid" and the data with "data". these data must be in the same syntax as expected within openssl.cnf, e.g. something like "OCSP;URI=http://...". additionally the critical flag can be set with "critical =" 1>.
  • key β€” key use given key as key for certificate, otherwise a new one will be generated and returned
  • issuer_cert cert β€” set issuer for new certificate
  • issuer_key key β€” sign new certificate with given key
  • issuer [ cert, key ] β€” instead of giving issuer_key and issuer_cert as separate arguments they can be given both together.
  • digest algorithm β€” specify the algorithm used to sign the certificate, default SHA-256.
  • ignore_invalid_args β€” ignore any unknown arguments which might be in the argument list (which might be in the arguments for example as result from CERT_asHash)

🌟 AUTHOR 🌟

Steffen Ullrich β€” thank you for your service, king 🫑


okay that's the whole page bestie, no cap πŸ’– this was IO::Socket::SSL::Utils in section 3, shipped with perl v5.42.0, dated 2026-01-17, and yes the header/footer were out here like IO::Soc...::Utils(3) because even man pages gotta abbreviate sometimes 😭 slay, go free your certs 🌸

πŸ“¦ where this page comes from