SefazNfe.Certificate (sefaz_nfe v0.5.0)

Copy Markdown View Source

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 — DateTime in UTC.
  • :tax_id — the holder's CNPJ or CPF, or nil when none can be found. It comes from the ICP-Brasil subjectAltName otherName 2.16.76.1.3.3 (CNPJ) or 2.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 (see SefazNfe.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, unless allow_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

load_error()

@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()}

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

days_until_expiry(cert, now \\ DateTime.utc_now())

@spec days_until_expiry(t(), DateTime.t()) :: integer()

Whole days from now until not_after; negative once expired.

expired?(cert, now \\ DateTime.utc_now())

@spec expired?(t(), DateTime.t()) :: boolean()

Whether now is past the leaf's not_after.

extra_cacerts()

@spec extra_cacerts() :: [binary()]

DER trust anchors from config :sefaz_nfe, :cacerts.

load(pfx, password, opts \\ [])

@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). Default false.
  • :now — a DateTime that replaces the clock, for tests. Default DateTime.utc_now().

See the module documentation for the error contract.

ssl_options(cert)

@spec ssl_options(t()) :: keyword()

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.