Skip to content

Add ExternalKeyProvider SPI for HSM-backed EBICS keys#48

Open
florianpollstaetter-dot wants to merge 1 commit into
ebics-java:masterfrom
florianpollstaetter-dot:feature/external-key-spi
Open

Add ExternalKeyProvider SPI for HSM-backed EBICS keys#48
florianpollstaetter-dot wants to merge 1 commit into
ebics-java:masterfrom
florianpollstaetter-dot:feature/external-key-spi

Conversation

@florianpollstaetter-dot
Copy link
Copy Markdown

Add ExternalKeyProvider SPI for HSM-backed EBICS keys

This PR adds a small, additive and non-breaking SPI so callers can supply A005/X002/E002 key material from outside the JVM (HSM, smartcard, any PKCS#11 token) instead of having the library generate keys via KeyUtil.makeKeyPair(...).

Motivation: production EBICS use cases that must keep private keys in tamper-resistant hardware (regulatory requirements, DSGVO-Schutzbedarf-Hoch in our case). Today EbicsClient.createUser(...) generates the keys internally and persists them as PKCS#12 — no public seam to inject an externally-supplied PrivateKey.

Design

  1. New interface org.kopi.ebics.client.ExternalKeyProvider

    • Three role methods: a005(), x002(), e002()
    • Each returns a KeyMaterial(PrivateKey, X509Certificate) record
    • Record's compact constructor rejects null privateKey/certificate at the boundary
  2. New User(...) constructor overload accepting ExternalKeyProvider

    • Installs the three key/cert pairs directly on the User
    • Skips CertificateManager.create() (no internal KeyUtil.makeKeyPair)
    • Skips PKCS#12 export (no in-process duplicate of the on-token key)
  3. New EbicsClient.createUser(...) overload accepting ExternalKeyProvider

    • Thin facade: creates user via the new constructor, persists bank/partner/user, writes INI/HIA letters using public-key bytes only

Goals

Goal How
Additive, non-breaking Existing overloads unchanged; existing callers behave identically
Minimal surface One interface + two overloads. No refactor of CertificateManager, no breaking change to User
Provider-agnostic Accepts any java.security.PrivateKey. SunPKCS11, BouncyCastleFipsProvider, JCE software — library is unaware
DSGVO-safe by default When external keys are supplied, the library does NOT also call KeyUtil.makeKeyPair(...) for the same role (no double-generation leak)

Tests (committed in this branch)

  • UserExternalKeysTest#constructorAssignsExternalKeysVerbatim — supplied PrivateKey instances arrive on User unchanged; public keys match supplied certs
  • UserExternalKeysTest#nullProviderRejected — null ExternalKeyProviderIllegalArgumentException
  • UserExternalKeysTest#partialProviderRejected — provider returning null for x002()IllegalArgumentException naming the offending role
  • UserExternalKeysTest#keyMaterialRecordRejectsNullPrivateKey / …NullCertificate — record-level null guards

Context

This SPI closes the HSM integration gap documented at https://github.com/florianpollstaetter-dot/rechnungstool (treasury sidecar). We've shipped the PKCS#11 plumbing on our side (Pkcs11A005Signer, smartcard + AWS CloudHSM locators), and would prefer to drop our local wrapper in favor of an upstream SPI if accepted.

Happy to iterate on naming, scope, or testing approach. Reach out anytime — we're committed to maintaining this if accepted.

…002 keys

Closes the gap that forces in-process generation of EBICS subscriber RSA key
pairs. Callers that hold the private keys in an HSM, smartcard, or any other
PKCS#11 token can now pass an ExternalKeyProvider; the library installs the
supplied (PrivateKey, X509Certificate) triples on the User and skips
KeyUtil.makeKeyPair(...) for that subscriber. No PKCS#12 is written for an
externally-keyed user, so the heap-key duplicate path stays cold.

The change is additive and non-breaking: existing createUser(...) callers see
no behaviour change.

Surface:

* New interface org.kopi.ebics.client.ExternalKeyProvider with the
  KeyMaterial(PrivateKey, X509Certificate) record. Compact constructor
  rejects null components at the boundary.
* New User constructor accepting an ExternalKeyProvider; bypasses
  CertificateManager.create().
* New EbicsClient.createUser(..., ExternalKeyProvider) overload that
  produces the user, persists bank/partner/user, and writes the
  INI/HIA letters (public-key bytes only) without writing PKCS#12.

JUnit coverage:

* UserExternalKeysTest#constructorAssignsExternalKeysVerbatim verifies the
  supplied PrivateKey instances reach the User unchanged (no internal
  KeyPair generation overwrites them) and that the public keys returned by
  the User match the supplied certificates for all three roles.
* UserExternalKeysTest#nullProviderRejected and #partialProviderRejected
  cover the boundary checks; partial errors name the offending role.
* UserExternalKeysTest#keyMaterialRecordRejects{Null,Null}* verify the
  record-level null guards.

Motivated by DSGVO + ISO 27001 commitments under which EBICS subscriber
A005/X002 private keys must never reside outside their hardware token.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants