Class BcPkixProvider

java.lang.Object
zeroecho.pkix.bc.BcPkixProvider
All Implemented Interfaces:
PkixProvider

public final class BcPkixProvider extends Object implements PkixProvider
The default PkixProvider, implemented with BouncyCastle. This is the single place in the codebase that a concrete cryptographic toolkit is used for X.509/PKIX operations; it is confined to the lib module and reached only through Pkix.
  • Constructor Details

    • BcPkixProvider

      public BcPkixProvider()
      Creates the provider, ensuring the BouncyCastle security providers are registered.
  • Method Details

    • verifyAlternativeSignature

      public boolean verifyAlternativeSignature(X509Certificate certificate, PublicKey issuerAlternativeKey)
      Description copied from interface: PkixProvider
      Verifies only the standardized alternative signature using an explicit issuer alternative key. Missing or malformed alternative extensions fail closed. This operation performs no path, time or primary-signature checks.
      Specified by:
      verifyAlternativeSignature in interface PkixProvider
      Parameters:
      certificate - nonnull certificate with all three alternative extensions
      issuerAlternativeKey - nonnull trusted issuer alternative public key
      Returns:
      whether the alternative signature is valid
    • id

      public String id()
      Description copied from interface: PkixProvider
      Returns the stable provider identifier (for diagnostics and selection).
      Specified by:
      id in interface PkixProvider
      Returns:
      the provider id
    • certificateBuilder

      public CertificateBuilder certificateBuilder()
      Description copied from interface: PkixProvider
      Creates a new certificate builder.
      Specified by:
      certificateBuilder in interface PkixProvider
      Returns:
      a fresh certificate builder
    • parseCertificate

      public X509Certificate parseCertificate(byte[] der)
      Description copied from interface: PkixProvider
      Parses a DER-encoded X.509 certificate.
      Specified by:
      parseCertificate in interface PkixProvider
      Parameters:
      der - the certificate DER
      Returns:
      the parsed certificate
    • certificationRequestBuilder

      public CertificationRequestBuilder certificationRequestBuilder()
      Description copied from interface: PkixProvider
      Creates a new certification-request builder.
      Specified by:
      certificationRequestBuilder in interface PkixProvider
      Returns:
      a fresh CSR builder
    • parseCertificationRequest

      public CertificationRequest parseCertificationRequest(byte[] der)
      Description copied from interface: PkixProvider
      Parses a DER-encoded PKCS#10 certification request.
      Specified by:
      parseCertificationRequest in interface PkixProvider
      Parameters:
      der - the request DER
      Returns:
      the parsed request
    • crlBuilder

      public CrlBuilder crlBuilder()
      Description copied from interface: PkixProvider
      Creates a new CRL builder.
      Specified by:
      crlBuilder in interface PkixProvider
      Returns:
      a fresh CRL builder
    • parseCrl

      public X509CRL parseCrl(byte[] der)
      Description copied from interface: PkixProvider
      Parses a DER-encoded X.509 CRL.
      Specified by:
      parseCrl in interface PkixProvider
      Parameters:
      der - the CRL DER
      Returns:
      the parsed CRL
    • ocspResponseBuilder

      public OcspResponseBuilder ocspResponseBuilder()
      Description copied from interface: PkixProvider
      Creates a new OCSP response builder.
      Specified by:
      ocspResponseBuilder in interface PkixProvider
      Returns:
      a fresh OCSP response builder
    • parseOcspRequest

      public OcspRequest parseOcspRequest(byte[] der)
      Description copied from interface: PkixProvider
      Parses a DER-encoded OCSP request.
      Specified by:
      parseOcspRequest in interface PkixProvider
      Parameters:
      der - the request DER
      Returns:
      the parsed request
    • signer

      public ContentSigner signer(PrivateKey privateKey, AlgorithmIdentifier algorithm)
      Description copied from interface: PkixProvider
      Creates a content signer over a local private key.
      Specified by:
      signer in interface PkixProvider
      Parameters:
      privateKey - the signing private key
      algorithm - the signature algorithm identifier
      Returns:
      a content signer
    • verifier

      public ContentVerifier verifier(PublicKey publicKey, AlgorithmIdentifier algorithm)
      Description copied from interface: PkixProvider
      Creates a content verifier over a public key.
      Specified by:
      verifier in interface PkixProvider
      Parameters:
      publicKey - the verifying public key
      algorithm - the signature algorithm identifier
      Returns:
      a content verifier
    • verifier

      public ContentVerifier verifier(byte[] subjectPublicKeyInfoDer, AlgorithmIdentifier algorithm)
      Description copied from interface: PkixProvider
      Creates a content verifier over a DER-encoded SubjectPublicKeyInfo and a signature algorithm identifier, honouring any algorithm parameters (such as RSASSA-PSS parameters) carried by the identifier.
      Specified by:
      verifier in interface PkixProvider
      Parameters:
      subjectPublicKeyInfoDer - the SPKI DER of the verifying key
      algorithm - the signature algorithm identifier
      Returns:
      a content verifier
    • subjectKeyIdentifier

      public byte[] subjectKeyIdentifier(PublicKey publicKey)
      Description copied from interface: PkixProvider
      Computes the standard subject key identifier (SHA-1 of the SPKI) for a key.
      Specified by:
      subjectKeyIdentifier in interface PkixProvider
      Parameters:
      publicKey - the public key
      Returns:
      the subject key identifier bytes
    • computeCertId

      public CertId computeCertId(X509Certificate issuer, BigInteger serialNumber, String digestAlgorithmOid)
      Description copied from interface: PkixProvider
      Computes an OCSP CertId for a certificate under a given issuer.
      Specified by:
      computeCertId in interface PkixProvider
      Parameters:
      issuer - the issuer certificate
      serialNumber - the subject certificate serial number
      digestAlgorithmOid - the digest algorithm OID for the hashes
      Returns:
      the computed cert id
    • distinguishedName

      public X500Principal distinguishedName(List<RelativeDistinguishedName> relativeNames)
      Description copied from interface: PkixProvider
      Builds a byte-exact distinguished name from typed relative distinguished names.
      Specified by:
      distinguishedName in interface PkixProvider
      Parameters:
      relativeNames - the ordered relative distinguished names
      Returns:
      the distinguished name
    • distinguishedNameString

      public String distinguishedNameString(List<RelativeDistinguishedName> relativeNames)
      Description copied from interface: PkixProvider
      Renders a distinguished name, built from typed relative distinguished names, as its canonical string form (attribute short names, most-significant first).
      Specified by:
      distinguishedNameString in interface PkixProvider
      Parameters:
      relativeNames - the ordered relative distinguished names
      Returns:
      the string form of the distinguished name
    • parseAlgorithmIdentifier

      public AlgorithmIdentifier parseAlgorithmIdentifier(byte[] der)
      Description copied from interface: PkixProvider
      Parses a DER-encoded X.509 AlgorithmIdentifier.
      Specified by:
      parseAlgorithmIdentifier in interface PkixProvider
      Parameters:
      der - the algorithm-identifier DER
      Returns:
      the parsed algorithm identifier
    • subjectPublicKeyAlgorithm

      public AlgorithmIdentifier subjectPublicKeyAlgorithm(byte[] subjectPublicKeyInfoDer)
      Description copied from interface: PkixProvider
      Extracts the algorithm identifier from a DER-encoded SubjectPublicKeyInfo.
      Specified by:
      subjectPublicKeyAlgorithm in interface PkixProvider
      Parameters:
      subjectPublicKeyInfoDer - the SPKI DER
      Returns:
      the subject public key algorithm identifier
    • publicKeyFromSubjectPublicKeyInfo

      public PublicKey publicKeyFromSubjectPublicKeyInfo(byte[] subjectPublicKeyInfoDer)
      Description copied from interface: PkixProvider
      Recovers a public key from a DER-encoded SubjectPublicKeyInfo.
      Specified by:
      publicKeyFromSubjectPublicKeyInfo in interface PkixProvider
      Parameters:
      subjectPublicKeyInfoDer - the SPKI DER
      Returns:
      the public key
    • parseOcspResponse

      public OcspResponse parseOcspResponse(byte[] der)
      Description copied from interface: PkixProvider
      Parses a DER-encoded OCSP response.
      Specified by:
      parseOcspResponse in interface PkixProvider
      Parameters:
      der - the response DER
      Returns:
      the parsed response
    • parseDistinguishedName

      public List<RelativeDistinguishedName> parseDistinguishedName(byte[] nameDer)
      Description copied from interface: PkixProvider
      Parses a DER-encoded X.500 Name into its ordered, single-valued relative distinguished names, preserving the ASN.1 string type of each value.
      Specified by:
      parseDistinguishedName in interface PkixProvider
      Parameters:
      nameDer - the DER encoding of an X.500 Name
      Returns:
      the ordered relative distinguished names
    • parseDistinguishedNameString

      public List<RelativeDistinguishedName> parseDistinguishedNameString(String name)
      Description copied from interface: PkixProvider
      Parses a distinguished-name string (as produced by PkixProvider.distinguishedNameString(java.util.List)) into its ordered, single-valued relative distinguished names, preserving the written order.
      Specified by:
      parseDistinguishedNameString in interface PkixProvider
      Parameters:
      name - the distinguished-name string
      Returns:
      the ordered relative distinguished names
    • subjectPublicKeyBytes

      public byte[] subjectPublicKeyBytes(byte[] subjectPublicKeyInfoDer)
      Description copied from interface: PkixProvider
      Returns the raw contents of the public-key BIT STRING of a DER-encoded SubjectPublicKeyInfo (the bytes after the unused-bits octet).
      Specified by:
      subjectPublicKeyBytes in interface PkixProvider
      Parameters:
      subjectPublicKeyInfoDer - the SPKI DER
      Returns:
      the public-key bit-string content bytes
    • subjectPublicKeyInfo

      public byte[] subjectPublicKeyInfo(X509Certificate certificate)
      Description copied from interface: PkixProvider
      Returns the exact DER of the SubjectPublicKeyInfo embedded in a certificate, byte-for-byte as encoded in the certificate.
      Specified by:
      subjectPublicKeyInfo in interface PkixProvider
      Parameters:
      certificate - the certificate
      Returns:
      the exact SPKI DER
    • buildSubjectPublicKeyInfo

      public byte[] buildSubjectPublicKeyInfo(AlgorithmIdentifier algorithm, byte[] publicKeyBits)
      Description copied from interface: PkixProvider
      Builds the DER of a SubjectPublicKeyInfo from an algorithm identifier and the raw public-key BIT STRING contents.
      Specified by:
      buildSubjectPublicKeyInfo in interface PkixProvider
      Parameters:
      algorithm - the subject public-key algorithm identifier
      publicKeyBits - the raw public-key bit-string content bytes
      Returns:
      the SPKI DER
    • parseExtensions

      public List<CertificateExtension> parseExtensions(byte[] der)
      Description copied from interface: PkixProvider
      Parses a DER-encoded X.509 Extensions sequence into typed extensions.
      Specified by:
      parseExtensions in interface PkixProvider
      Parameters:
      der - the DER of an Extensions sequence
      Returns:
      the parsed extensions in order
    • assembleCertificateOrCrl

      public byte[] assembleCertificateOrCrl(byte[] tbs, byte[] signature, boolean crl)
      Description copied from interface: PkixProvider
      Reassembles a signed certificate or CRL from its tbs bytes and a raw signature, repeating the algorithm identifier embedded in the tbs as the outer signatureAlgorithm, and validates that the result parses.
      Specified by:
      assembleCertificateOrCrl in interface PkixProvider
      Parameters:
      tbs - the tbsCertificate or tbsCertList DER
      signature - the raw signature bytes
      crl - true for a CRL, false for a certificate
      Returns:
      the DER of the assembled signed object
    • assembleOcspResponse

      public byte[] assembleOcspResponse(byte[] tbsResponseData, byte[] signatureAlgorithmDer, byte[] signature)
      Description copied from interface: PkixProvider
      Reassembles a successful OCSPResponse wrapping a BasicOCSPResponse from its tbsResponseData, a DER-encoded signature algorithm identifier, and a raw signature, and validates that the result parses as successful.
      Specified by:
      assembleOcspResponse in interface PkixProvider
      Parameters:
      tbsResponseData - the tbsResponseData DER
      signatureAlgorithmDer - the DER of the signature AlgorithmIdentifier
      signature - the raw signature bytes
      Returns:
      the DER of the assembled OCSP response
    • assembleOcspResponse

      public byte[] assembleOcspResponse(byte[] tbsResponseData, byte[] signatureAlgorithmDer, byte[] signature, List<X509Certificate> chain)
      Description copied from interface: PkixProvider
      Reassembles a successful OCSP response from unsigned ResponseData, an encoded signature algorithm, a raw signature and an ordered certificate chain. Certificates are embedded in the supplied order without sorting or trust validation. An empty chain omits the optional certs field, matching the three-argument overload. No signer is invoked and the raw signature is not cryptographically verified by assembly.

      Inputs remain caller-owned and are not retained. The returned DER array is independent. Time and auxiliary memory are linear in response-data, algorithm, signature and total embedded-certificate bytes. No aggregate live-response size limit is introduced.

      Specified by:
      assembleOcspResponse in interface PkixProvider
      Parameters:
      tbsResponseData - nonnull DER-encoded unsigned ResponseData
      signatureAlgorithmDer - nonnull DER-encoded signature AlgorithmIdentifier
      signature - nonnull raw signature bytes
      chain - nonnull certificate list in exact embedding order, with no null elements
      Returns:
      freshly encoded successful OCSP response DER
    • encodeAlgorithmIdentifier

      public byte[] encodeAlgorithmIdentifier(AlgorithmIdentifier algorithm)
      Description copied from interface: PkixProvider
      Returns the DER encoding of an X.509 AlgorithmIdentifier.
      Specified by:
      encodeAlgorithmIdentifier in interface PkixProvider
      Parameters:
      algorithm - the algorithm identifier
      Returns:
      the DER encoding
    • parseGeneralNames

      public List<GeneralName> parseGeneralNames(byte[] der)
      Description copied from interface: PkixProvider
      Parses a DER-encoded GeneralNames sequence into typed general names.
      Specified by:
      parseGeneralNames in interface PkixProvider
      Parameters:
      der - the DER of a GeneralNames sequence
      Returns:
      the parsed general names in order
    • encodeCertsOnly

      public byte[] encodeCertsOnly(List<X509Certificate> chain)
      Description copied from interface: PkixProvider
      Encodes a list of X.509 certificates as a CMS certs-only message (SignedData with no signers, only the certificate bag).

      The returned bytes are the DER-encoded ContentInfo wrapping the SignedData. The encoding is suitable for use as the body of an EST /cacerts or /simpleenroll response.

      Specified by:
      encodeCertsOnly in interface PkixProvider
      Parameters:
      chain - ordered certificate list; must not be null or empty
      Returns:
      DER-encoded CMS certs-only ContentInfo
    • encodeCsrAttrs

      public byte[] encodeCsrAttrs(List<CsrAttribute> attributes)
      Description copied from interface: PkixProvider
      Encodes a list of CSR attributes as an RFC 7030 CsrAttrs structure (DER-encoded ASN.1 sequence of OIDs and optional attribute values).
      Specified by:
      encodeCsrAttrs in interface PkixProvider
      Parameters:
      attributes - the CSR attributes to encode; must not be null
      Returns:
      DER-encoded CsrAttrs sequence
    • buildServerKeygenCsr

      public byte[] buildServerKeygenCsr(byte[] originalCsrDer, PublicKey generatedPublicKey, PrivateKey generatedPrivateKey)
      Builds a new DER-encoded PKCS#10 CSR using the supplied generated key pair, copying the subject DN and all attributes from the original CSR.

      The self-signature over the rebuilt CSR uses SHA-256withECDSA for EC keys and SHA-256withRSA for RSA keys, determined from the generated private key type.

      Specified by:
      buildServerKeygenCsr in interface PkixProvider
      Parameters:
      originalCsrDer - DER-encoded original PKCS#10 CSR (source of subject and attributes)
      generatedPublicKey - server-generated public key to embed
      generatedPrivateKey - server-generated private key used to self-sign
      Returns:
      DER-encoded PKCS#10 CSR signed with the generated key
      Throws:
      UnsupportedRoleException - if the private key uses LMS/XMSS; rejection precedes CSR parsing and signing-engine initialization
    • encryptPrivateKeyForRecipient

      public byte[] encryptPrivateKeyForRecipient(byte[] pkcs8PrivateKeyDer, X509Certificate recipientCertificate)
      Description copied from interface: PkixProvider
      Encrypts a PKCS#8 private key for a single recipient using CMS EnvelopedData with AES-256-GCM content encryption.

      The recipient is identified by the public key in the supplied certificate. The key is wrapped using RSA-OAEP (for RSA keys) or ECDH-KDF (for EC keys) depending on the recipient certificate's public key algorithm.

      Specified by:
      encryptPrivateKeyForRecipient in interface PkixProvider
      Parameters:
      pkcs8PrivateKeyDer - DER-encoded PKCS#8 private key to encrypt
      recipientCertificate - the recipient's certificate (public key used for wrap)
      Returns:
      DER-encoded CMS ContentInfo wrapping the EnvelopedData
    • buildPreCertificate

      public PreCertificateResult buildPreCertificate(byte[] tbsCertificateDer, List<byte[]> issuerChainDer, ContentSigner signer)
      Description copied from interface: PkixProvider
      Builds a CT pre-certificate from an existing TBSCertificate by adding the CT poison extension (OID 1.3.6.1.4.1.11129.2.4.3) and signing with the supplied signer.

      The pre-certificate is submitted to CT logs; logs return SCTs that are later embedded in the final certificate via PkixProvider.embedScts(byte[], List).

      Specified by:
      buildPreCertificate in interface PkixProvider
      Parameters:
      tbsCertificateDer - DER-encoded TBSCertificate without the poison extension
      issuerChainDer - DER-encoded issuer chain (issuer first, root last)
      signer - configured signer for the issuing CA key
      Returns:
      pre-certificate DER and its chain, ready for CT log submission
    • embedScts

      public byte[] embedScts(byte[] tbsCertificateDer, List<SignedCertificateTimestamp> scts)
      Description copied from interface: PkixProvider
      Embeds a list of SCTs into a TBSCertificate as the SCT list extension (OID 1.3.6.1.4.1.11129.2.4.2), producing a modified TBSCertificate DER ready for final signing.

      If the TBSCertificate already carries a poison extension (OID 1.3.6.1.4.1.11129.2.4.3), it is replaced by the SCT list extension. Any pre-existing SCT list extension is also replaced.

      Specified by:
      embedScts in interface PkixProvider
      Parameters:
      tbsCertificateDer - DER-encoded TBSCertificate
      scts - verified SCTs to embed; must contain at least one entry
      Returns:
      modified DER-encoded TBSCertificate with the SCT list extension
    • parseSct

      public SignedCertificateTimestamp parseSct(byte[] jsonResponseBody)
      Description copied from interface: PkixProvider
      Parses a Signed Certificate Timestamp from the JSON response body returned by a CT log's add-pre-chain or add-chain endpoint.

      Expected JSON fields: sct_version, id (base64 log ID), timestamp (epoch milliseconds), extensions (base64, may be empty), signature (base64-encoded TLS DigitallySigned structure).

      Specified by:
      parseSct in interface PkixProvider
      Parameters:
      jsonResponseBody - raw response body bytes; must not be null
      Returns:
      the parsed SCT
    • verifySct

      public boolean verifySct(SignedCertificateTimestamp sct, PublicKey ctLogPublicKey, byte[] preCertDer)
      Description copied from interface: PkixProvider
      Verifies an SCT's digital signature against a known CT log public key.

      The signed data covers the SCT fields (version, log ID, timestamp, extensions) concatenated with a SHA-256 hash of the pre-certificate DER, as a simplified verification approach. Returns false if the signature is invalid; throws only on algorithmic or key errors.

      Specified by:
      verifySct in interface PkixProvider
      Parameters:
      sct - the SCT to verify; must not be null
      ctLogPublicKey - EC or RSA public key of the CT log; must not be null
      preCertDer - DER-encoded pre-certificate that was submitted; must not be null
      Returns:
      true if the signature is valid