Skip to content

Commit 18d83d2

Browse files
feat(client): add ExternalKeyProvider SPI for HSM-managed A005/X002/E002 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.
1 parent c09a1ae commit 18d83d2

4 files changed

Lines changed: 407 additions & 0 deletions

File tree

src/main/java/org/kopi/ebics/client/EbicsClient.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,58 @@ public User createUser(URL url, String bankName, String hostId, String partnerId
221221
}
222222
}
223223

224+
/**
225+
* Creates a new EBICS user whose A005/X002/E002 key material is supplied
226+
* externally (HSM, smartcard, or any PKCS#11 token). The library will
227+
* neither call {@code KeyUtil.makeKeyPair(...)} nor write a PKCS#12 file
228+
* for this user — both would conflict with the no-heap-key contract
229+
* that motivates external key custody.
230+
*
231+
* <p>Bank, partner, and user are persisted via the configured
232+
* {@link org.kopi.ebics.interfaces.SerializationManager} just like the
233+
* heap-key overload. The INI/HIA letters are also produced (they only
234+
* need the public-key bytes that the supplied X.509 certificates expose).
235+
*
236+
* @param externalKeys the externally-managed key material. Must not be
237+
* null; each role accessor must return a non-null
238+
* {@link ExternalKeyProvider.KeyMaterial}.
239+
* @return the created {@link User}.
240+
* @throws IllegalArgumentException if {@code externalKeys} is null or
241+
* partial.
242+
* @since 2.1.0
243+
*/
244+
public User createUser(URL url, String bankName, String hostId, String partnerId,
245+
String userId, String name, String email, String country, String organization,
246+
boolean useCertificates, PasswordCallback passwordCallback,
247+
ExternalKeyProvider externalKeys)
248+
throws Exception {
249+
if (externalKeys == null) {
250+
throw new IllegalArgumentException("externalKeys must not be null");
251+
}
252+
log.info(messages.getString("user.create.info", userId));
253+
254+
Bank bank = createBank(url, bankName, hostId, useCertificates);
255+
Partner partner = createPartner(bank, partnerId);
256+
try {
257+
User user = new User(partner, userId, name, email, country, organization,
258+
passwordCallback, externalKeys);
259+
createUserDirectories(user);
260+
configuration.getSerializationManager().serialize(bank);
261+
configuration.getSerializationManager().serialize(partner);
262+
configuration.getSerializationManager().serialize(user);
263+
createLetters(user, useCertificates);
264+
users.put(userId, user);
265+
partners.put(partner.getPartnerId(), partner);
266+
banks.put(bank.getHostId(), bank);
267+
268+
log.info(messages.getString("user.create.success", userId));
269+
return user;
270+
} catch (Exception e) {
271+
log.error(messages.getString("user.create.error"), e);
272+
throw e;
273+
}
274+
}
275+
224276
private void createLetters(EbicsUser user, boolean useCertificates)
225277
throws GeneralSecurityException, IOException, EbicsException {
226278
user.getPartner().getBank().setUseCertificate(useCertificates);
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Copyright (c) 1990-2012 kopiLeft Development SARL, Bizerte, Tunisia
3+
*
4+
* This library is free software; you can redistribute it and/or
5+
* modify it under the terms of the GNU Lesser General Public
6+
* License version 2.1 as published by the Free Software Foundation.
7+
*
8+
* This library is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11+
* Lesser General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU Lesser General Public
14+
* License along with this library; if not, write to the Free Software
15+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16+
*
17+
*/
18+
19+
package org.kopi.ebics.client;
20+
21+
import java.security.PrivateKey;
22+
import java.security.cert.X509Certificate;
23+
24+
/**
25+
* Supplies the A005/X002/E002 key material for a subscriber when the caller
26+
* holds the private keys outside of the JVM heap (HSM, smartcard, or any other
27+
* PKCS#11 token).
28+
*
29+
* <p>Pass an implementation of this interface to
30+
* {@link EbicsClient#createUser(java.net.URL, String, String, String, String,
31+
* String, String, String, String, boolean, boolean,
32+
* org.kopi.ebics.interfaces.PasswordCallback, ExternalKeyProvider)} or to the
33+
* matching {@link User} constructor; the library will then skip its built-in
34+
* {@code KeyUtil.makeKeyPair(...)} path and install the supplied keys and
35+
* certificates on the {@link User}. This is the seam that allows callers to
36+
* keep DSGVO / FIPS / ISO 27001 commitments under which an EBICS subscriber's
37+
* private signing key never resides outside its hardware token.
38+
*
39+
* <p>Implementations MUST NOT return a {@link PrivateKey} whose
40+
* {@link PrivateKey#getEncoded()} is non-null when the key is intended to be
41+
* HSM-resident &mdash; a non-null encoding indicates the key escaped the token.
42+
* The library does not assert this invariant; callers must.
43+
*
44+
* <p>All three role methods must return a non-null {@link KeyMaterial}. Returning
45+
* {@code null} from any role causes the consuming overload to throw
46+
* {@link IllegalArgumentException} at the boundary.
47+
*
48+
* @since 2.1.0
49+
*/
50+
public interface ExternalKeyProvider {
51+
52+
/** Bank-technical / electronic-signature key (EBICS role A005). */
53+
KeyMaterial a005();
54+
55+
/** Identification &amp; authentication key (EBICS role X002). */
56+
KeyMaterial x002();
57+
58+
/** Encryption key (EBICS role E002). */
59+
KeyMaterial e002();
60+
61+
/**
62+
* A {@code (PrivateKey, X509Certificate)} pair for one EBICS role.
63+
* Both components are required; the compact constructor rejects {@code null}.
64+
*/
65+
record KeyMaterial(PrivateKey privateKey, X509Certificate certificate) {
66+
public KeyMaterial {
67+
if (privateKey == null) {
68+
throw new IllegalArgumentException("privateKey must not be null");
69+
}
70+
if (certificate == null) {
71+
throw new IllegalArgumentException("certificate must not be null");
72+
}
73+
}
74+
}
75+
}

src/main/java/org/kopi/ebics/client/User.java

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,79 @@ public User(EbicsPartner partner,
8787
needSave = true;
8888
}
8989

90+
/**
91+
* First-time constructor that installs externally supplied key material
92+
* instead of generating fresh RSA key pairs in-process.
93+
*
94+
* <p>Use this constructor when the A005/X002/E002 private keys live in an
95+
* HSM, a smartcard, or any other PKCS#11 token and must not be materialised
96+
* on the JVM heap. The supplied {@link ExternalKeyProvider} is consulted
97+
* once for all three roles; the library will not call
98+
* {@code KeyUtil.makeKeyPair(...)} for the same user, so there is no
99+
* heap-resident duplicate of the on-token key.
100+
*
101+
* <p>The library does not write a PKCS#12 file for an externally-keyed user
102+
* (the private keys may not be exportable). Callers that need to persist a
103+
* record of the public certificates should serialise the certificate bytes
104+
* separately.
105+
*
106+
* @param partner customer in whose name we operate.
107+
* @param userId UserId as obtained from the bank.
108+
* @param name the user name.
109+
* @param email the user email.
110+
* @param country the user country.
111+
* @param organization the user organization or company.
112+
* @param passwordCallback a callback-handler that supplies us with the
113+
* password. This parameter can be null, in which
114+
* case no password is used.
115+
* @param externalKeys the externally-managed key material for A005/X002/E002.
116+
* Must not be null; each role method must return a
117+
* non-null {@link ExternalKeyProvider.KeyMaterial}.
118+
* @throws IllegalArgumentException if {@code externalKeys} is null or any of
119+
* its role accessors return null.
120+
* @since 2.1.0
121+
*/
122+
public User(EbicsPartner partner,
123+
String userId,
124+
String name,
125+
String email,
126+
String country,
127+
String organization,
128+
PasswordCallback passwordCallback,
129+
ExternalKeyProvider externalKeys)
130+
{
131+
if (externalKeys == null) {
132+
throw new IllegalArgumentException("externalKeys must not be null");
133+
}
134+
ExternalKeyProvider.KeyMaterial a005 = externalKeys.a005();
135+
ExternalKeyProvider.KeyMaterial x002 = externalKeys.x002();
136+
ExternalKeyProvider.KeyMaterial e002 = externalKeys.e002();
137+
if (a005 == null) {
138+
throw new IllegalArgumentException("externalKeys.a005() returned null");
139+
}
140+
if (x002 == null) {
141+
throw new IllegalArgumentException("externalKeys.x002() returned null");
142+
}
143+
if (e002 == null) {
144+
throw new IllegalArgumentException("externalKeys.e002() returned null");
145+
}
146+
147+
this.partner = partner;
148+
this.userId = userId;
149+
this.name = name;
150+
this.dn = makeDN(name, email, country, organization);
151+
this.passwordCallback = passwordCallback;
152+
153+
this.a005PrivateKey = a005.privateKey();
154+
this.a005Certificate = a005.certificate();
155+
this.x002PrivateKey = x002.privateKey();
156+
this.x002Certificate = x002.certificate();
157+
this.e002PrivateKey = e002.privateKey();
158+
this.e002Certificate = e002.certificate();
159+
160+
needSave = true;
161+
}
162+
90163
/**
91164
* Reconstructs a persisted EBICS user.
92165
*

0 commit comments

Comments
 (0)