A1 (PKCS#12) handle.
load/3 decodes the PFX, so a wrong password or an expired certificate fails
here — before any SOAP call. The password is never kept on the struct and
Inspect never prints the key or the DER.
The fields are shaped for the two consumers: :der + :key + :chain feed
:ssl client options for mTLS, and :der + :key feed XMLDSig. The
leaf's validity window and the holder's tax ID are read from the certificate
as well:
:not_before/:not_after—DateTimein UTC.:tax_id— the holder's CNPJ or CPF, ornilwhen none can be found. It comes from the ICP-BrasilsubjectAltNameotherName2.16.76.1.3.3(CNPJ) or2.16.76.1.3.1(CPF, after the 8 digit birth date) and falls back to the suffix after the last":"in the CN when that is 11 or 14 characters of[A-Z0-9].
Errors
load/3 returns:
{:error, :invalid_password}— the password is empty, is not valid UTF-8 or the PKCS#12 MAC does not verify. A MAC mismatch is cryptographically indistinguishable from a tampered file, but a wrong password is by far the common cause.{:error, :invalid_certificate}— the input is empty or not a binary, or the file is structurally broken, has no MAC, no private key or no certificate.{:error, {:unsupported_pbe, oid}}/{:error, {:unsupported_mac, oid}}— an algorithm this reader has not been verified against.{:error, {:limit_exceeded, :size | :bags | :iterations | :key_length}}— the file asks for more work or space than a real A1 ever does (seeSefazNfe.Certificate.PKCS12).{:error, :key_cert_mismatch}— no certificate in the file carries the public key of the private key;{:error, :multiple_keys}— the file holds more than one private key;{:error, {:unsupported_key, :ec | :other}}— the key is not RSA.{:error, {:expired, not_after}}/{:error, {:not_yet_valid, not_before}}— the validity window does not contain the current time, unlessallow_expired: true.
Summary
Functions
Whole days from now until not_after; negative once expired.
Whether now is past the leaf's not_after.
DER trust anchors from config :sefaz_nfe, :cacerts.
Decodes an A1 PFX.
Client options for an mTLS connection to SEFAZ.
Types
@type load_error() :: :invalid_password | :invalid_certificate | {:unsupported_pbe, binary()} | {:unsupported_mac, binary()} | {:limit_exceeded, :size | :bags | :iterations | :key_length} | :key_cert_mismatch | :multiple_keys | {:unsupported_key, :ec | :other} | {:expired, DateTime.t()} | {:not_yet_valid, DateTime.t()}
@type t() :: %SefazNfe.Certificate{ chain: [binary()], der: binary(), key: binary(), not_after: DateTime.t() | nil, not_before: DateTime.t() | nil, tax_id: String.t() | nil }
Functions
@spec days_until_expiry(t(), DateTime.t()) :: integer()
Whole days from now until not_after; negative once expired.
@spec expired?(t(), DateTime.t()) :: boolean()
Whether now is past the leaf's not_after.
@spec extra_cacerts() :: [binary()]
DER trust anchors from config :sefaz_nfe, :cacerts.
@spec load(binary(), String.t(), keyword()) :: {:ok, t()} | {:error, load_error()}
Decodes an A1 PFX.
Options
:allow_expired— load a certificate outside its validity window anyway (to inspect it, or to tell the user when it expired). Defaultfalse.:now— aDateTimethat replaces the clock, for tests. DefaultDateTime.utc_now().
See the module documentation for the error contract.
Client options for an mTLS connection to SEFAZ.
The private key is handed to :ssl as a PrivateKeyInfo DER, which is what
the PKCS#12 shrouded key bag already contains. The leaf and the A1's own
chain go into :cert as the client chain to present. They are deliberately
not in :cacerts: there :ssl keeps the trust store that verifies the
server, and the PFX is usually an upload, so every certificate inside it
would otherwise become a trusted root.
Trust anchors
Several SEFAZ endpoints — SP and MT among them — serve certificates issued
under Autoridade Certificadora Raiz Brasileira, which no operating system
bundle carries. Without that root the handshake fails with {:tls, :unknown_ca}, and the fix is to supply it rather than to stop verifying:
config :sefaz_nfe, :cacerts, "/etc/ssl/icp-brasil.pem"The value is a path to a PEM bundle or a list of DER binaries. It is added to the trust anchors, never replacing the system bundle, since other UFs chain to ordinary commercial roots.
This library ships no trust anchors of its own on purpose: a CA bundle vendored from an unverified download is a man-in-the-middle waiting to happen. Fetch the roots from the ITI repository and check their fingerprints before installing them.