Class KeyImports

java.lang.Object
com.revetsec.KeyImports

@ThreadSafe public final class KeyImports extends Object
Bounded imports of ordinary JCA key material: RSA (2048 through 16384 bits, the existing exponent/ROCA policy), EC P-256/P-384 and canonical, non-small-order Ed25519 public points. DER is at most 65536 bytes and PEM at most 98304 characters, checked before copying/decoding; PEM's decoded DER is also bounded before allocation.

One exact PEM block is accepted, with canonical padded Base64 and matching labels. Public DER is SPKI. Private material is unencrypted PKCS#8 version 0 without attributes/outer public fields, or two-prime RSA PKCS#1 version 0. SEC1 armor, encrypted keys, RFC5958 version 1/attributes, P-521, Ed448, XDH and RSA-PSS containers are outside this profile. EC's inner named parameters must agree with its outer parameters; an embedded public hint must be a well-formed on-curve point but remains untrusted metadata. Import does not establish scalar-to-hint or key-pair agreement; use the separate explicit pair check.

Certificate parsing checks the embedded key profile, not certificate signatures, trust, chains, hostname, time, revocation or complete inner DER canonicality. Caller input remains unchanged. Owned private DER and pair-check challenge/signature buffers are erased; provider-owned keys, copies and immutable values remain under JCA/provider ownership. No method exports private key material or implicitly signs during import.

Since:
1.0.0
Author:
Mark Allen
  • Method Details

    • certificateFromPem

      @CheckReturnValue public static @NonNull X509Certificate certificateFromPem(@NonNull String pem)
      Imports one CERTIFICATE PEM block and checks its embedded key profile, without establishing certificate trust.
      Parameters:
      pem - at most 98304 characters
      Returns:
      the caller-owned parsed certificate
      Throws:
      NullPointerException - if pem is null
      KeyImportException - if parsing, size or the selected key profile fails
      Since:
      1.0.0
    • certificateFromDer

      @CheckReturnValue public static @NonNull X509Certificate certificateFromDer(byte @NonNull [] der)
      Imports one DER certificate and checks its embedded key profile, without establishing certificate trust.
      Parameters:
      der - at most 65536 bytes; not modified
      Returns:
      the caller-owned parsed certificate
      Throws:
      NullPointerException - if der is null
      KeyImportException - if parsing, size or the selected key profile fails
      Since:
      1.0.0
    • publicKeyFromPem

      @CheckReturnValue public static @NonNull PublicKey publicKeyFromPem(@NonNull String pem)
      Imports one PUBLIC KEY PEM block under the selected public-key profile.
      Parameters:
      pem - at most 98304 characters
      Returns:
      the caller-owned checked JCA key
      Throws:
      NullPointerException - if pem is null
      KeyImportException - if parsing, size or the selected key profile fails
      Since:
      1.0.0
    • publicKeyFromSpkiDer

      @CheckReturnValue public static @NonNull PublicKey publicKeyFromSpkiDer(byte @NonNull [] der)
      Imports one canonical SubjectPublicKeyInfo DER value under the selected public-key profile.
      Parameters:
      der - at most 65536 bytes; not modified
      Returns:
      the caller-owned checked JCA key
      Throws:
      NullPointerException - if der is null
      KeyImportException - if parsing, size or the selected key profile fails
      Since:
      1.0.0
    • privateKeyFromPem

      @CheckReturnValue public static @NonNull PrivateKey privateKeyFromPem(@NonNull String pem)
      Imports one PRIVATE KEY or RSA PRIVATE KEY PEM block in the selected unencrypted private-container profile.
      Parameters:
      pem - at most 98304 characters
      Returns:
      the caller-owned decoded JCA key; pair agreement is not established
      Throws:
      NullPointerException - if pem is null
      KeyImportException - if parsing, size or the selected key profile fails
      Since:
      1.0.0
    • privateKeyFromPkcs8Der

      @CheckReturnValue public static @NonNull PrivateKey privateKeyFromPkcs8Der(byte @NonNull [] der)
      Imports one unencrypted PKCS#8 version 0 key without attributes or an outer public-key field.
      Parameters:
      der - at most 65536 bytes; not modified
      Returns:
      the caller-owned decoded JCA key; pair agreement is not established
      Throws:
      NullPointerException - if der is null
      KeyImportException - if parsing, size or the selected key profile fails
      Since:
      1.0.0
    • rsaPrivateKeyFromPkcs1Der

      @CheckReturnValue public static @NonNull PrivateKey rsaPrivateKeyFromPkcs1Der(byte @NonNull [] der)
      Imports one two-prime RSA PKCS#1 version 0 private key.
      Parameters:
      der - at most 65536 bytes; not modified
      Returns:
      the caller-owned decoded RSA key; pair agreement is not established
      Throws:
      NullPointerException - if der is null
      KeyImportException - if parsing, size or the selected key profile fails
      Since:
      1.0.0
    • checkKeyPair

      public static void checkKeyPair(@NonNull PrivateKey privateKey, @NonNull PublicKey publicKey, @NonNull Duration remainingBudget)
      Checks agreement with a fresh 32-byte noncredential challenge, a fresh JCA signer and the independent verifier. The public key must provide a bounded canonical SPKI encoding; it is reparsed to a checked stable projection. The private key may remain opaque; its encoding is never requested. No signature or challenge is returned. One original monotonic budget covers public snapshot, provider/random initialization, signing and verification. Trusted synchronous providers must cooperate; these checks cannot preempt a blocked provider. No retry occurs.
      Parameters:
      privateKey - the caller-owned private key
      publicKey - the caller-owned encodable public key
      remainingBudget - the original remaining budget; nonpositive values are exhausted
      Throws:
      NullPointerException - if a required argument is null
      KeyImportException - if the profile, pair, provider, interruption or budget check fails
      Since:
      1.0.0