• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ebourg / jsign / #429

12 Oct 2026 08:24AM UTC coverage: 80.879% (+0.3%) from 80.604%
#429

push

ebourg
Release version 7.6

5080 of 6281 relevant lines covered (80.88%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

84.62
/jsign-crypto/src/main/java/net/jsign/KeyStoreType.java
1
/*
2
 * Copyright 2023 Emmanuel Bourg
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16

17
package net.jsign;
18

19
import java.io.File;
20
import java.io.FileInputStream;
21
import java.io.IOException;
22
import java.net.UnknownServiceException;
23
import java.nio.ByteBuffer;
24
import java.security.KeyStore;
25
import java.security.KeyStoreException;
26
import java.security.PrivateKey;
27
import java.security.Provider;
28
import java.security.Security;
29
import java.security.cert.Certificate;
30
import java.security.cert.CertificateException;
31
import java.util.Collections;
32
import java.util.LinkedHashSet;
33
import java.util.Set;
34
import java.util.function.Function;
35

36
import javax.smartcardio.CardException;
37

38
import net.jsign.jca.AmazonCredentials;
39
import net.jsign.jca.AmazonSigningService;
40
import net.jsign.jca.AzureCredentials;
41
import net.jsign.jca.AzureKeyVaultSigningService;
42
import net.jsign.jca.AzureTrustedSigningService;
43
import net.jsign.jca.CryptoCertumCardSigningService;
44
import net.jsign.jca.DigiCertOneSigningService;
45
import net.jsign.jca.ESignerSigningService;
46
import net.jsign.jca.GaraSignCredentials;
47
import net.jsign.jca.GaraSignSigningService;
48
import net.jsign.jca.GoogleCloudSigningService;
49
import net.jsign.jca.HashiCorpVaultSigningService;
50
import net.jsign.jca.OpenPGPCardSigningService;
51
import net.jsign.jca.OracleCloudCredentials;
52
import net.jsign.jca.OracleCloudSigningService;
53
import net.jsign.jca.PIVCardSigningService;
54
import net.jsign.jca.SignPathSigningService;
55
import net.jsign.jca.SignServerCredentials;
56
import net.jsign.jca.SignServerSigningService;
57
import net.jsign.jca.SigningServiceJcaProvider;
58

59
/**
60
 * Type of a keystore.
61
 *
62
 * @since 5.0
63
 */
64
public enum KeyStoreType {
1 ✔
65

66
    /** Not a keystore, a private key file and a certificate file are provided separately and assembled into an in-memory keystore */
67
    NONE(true, false) {
1 ✔
68
        @Override
69
        void validate(KeyStoreBuilder params) {
70
            if (params.keyfile() == null) {
1 ✔
71
                throw new IllegalArgumentException("keyfile " + params.parameterName() + " must be set");
1 ✔
72
            }
73
            if (!params.keyfile().exists()) {
1 ✔
74
                throw new IllegalArgumentException("The keyfile " + params.keyfile() + " couldn't be found");
1 ✔
75
            }
76
            if (params.certfile() == null) {
1 ✔
77
                throw new IllegalArgumentException("certfile " + params.parameterName() + " must be set");
1 ✔
78
            }
79
            if (!params.certfile().exists()) {
1 ✔
80
                throw new IllegalArgumentException("The certfile " + params.certfile() + " couldn't be found");
1 ✔
81
            }
82
        }
1 ✔
83

84
        @Override
85
        KeyStore getKeystore(KeyStoreBuilder params, Provider provider) throws KeyStoreException {
86
            // load the certificate chain
87
            Certificate[] chain;
88
            try {
89
                chain = CertificateUtils.loadCertificateChain(params.certfile());
1 ✔
90
            } catch (Exception e) {
1 ✔
91
                throw new KeyStoreException("Failed to load the certificate from " + params.certfile(), e);
1 ✔
92
            }
1 ✔
93

94
            // load the private key
95
            PrivateKey privateKey;
96
            try {
97
                privateKey = PrivateKeyUtils.load(params.keyfile(), params.keypass() != null ? params.keypass() : params.storepass());
1 ✔
98
            } catch (Exception e) {
1 ✔
99
                throw new KeyStoreException("Failed to load the private key from " + params.keyfile(), e);
1 ✔
100
            }
1 ✔
101

102
            // build the in-memory keystore
103
            KeyStore ks = KeyStore.getInstance("JKS");
1 ✔
104
            try {
105
                ks.load(null, null);
1 ✔
106
                String keypass = params.keypass();
1 ✔
107
                ks.setKeyEntry("jsign", privateKey, keypass != null ? keypass.toCharArray() : new char[0], chain);
1 ✔
108
            } catch (Exception e) {
×
109
                throw new KeyStoreException(e);
×
110
            }
1 ✔
111

112
            return ks;
1 ✔
113
        }
114
    },
115

116
    /** Java keystore */
117
    JKS(true, false) {
1 ✔
118
        @Override
119
        void validate(KeyStoreBuilder params) {
120
            if (params.keystore() == null) {
1 ✔
121
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must be set");
1 ✔
122
            }
123
            if (!params.createFile(params.keystore()).exists()) {
1 ✔
124
                throw new IllegalArgumentException("The keystore " + params.keystore() + " couldn't be found");
1 ✔
125
            }
126
            if (params.keypass() == null && params.storepass() != null) {
1 ✔
127
                // reuse the storepass as the keypass
128
                params.keypass(params.storepass());
1 ✔
129
            }
130
        }
1 ✔
131
    },
132

133
    /** JCE keystore */
134
    JCEKS(true, false) {
1 ✔
135
        @Override
136
        void validate(KeyStoreBuilder params) {
137
            if (params.keystore() == null) {
1 ✔
138
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must be set");
1 ✔
139
            }
140
            if (!params.createFile(params.keystore()).exists()) {
1 ✔
141
                throw new IllegalArgumentException("The keystore " + params.keystore() + " couldn't be found");
1 ✔
142
            }
143
            if (params.keypass() == null && params.storepass() != null) {
1 ✔
144
                // reuse the storepass as the keypass
145
                params.keypass(params.storepass());
1 ✔
146
            }
147
        }
1 ✔
148
    },
149

150
    /** PKCS#12 keystore */
151
    PKCS12(true, false) {
1 ✔
152
        @Override
153
        void validate(KeyStoreBuilder params) {
154
            if (params.keystore() == null) {
1 ✔
155
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must be set");
1 ✔
156
            }
157
            if (!params.createFile(params.keystore()).exists()) {
1 ✔
158
                throw new IllegalArgumentException("The keystore " + params.keystore() + " couldn't be found");
1 ✔
159
            }
160
            if (params.keypass() == null && params.storepass() != null) {
1 ✔
161
                // reuse the storepass as the keypass
162
                params.keypass(params.storepass());
1 ✔
163
            }
164
        }
1 ✔
165
    },
166

167
    /**
168
     * PKCS#11 hardware token. The keystore parameter specifies either the name of the provider defined
169
     * in <code>jre/lib/security/java.security</code> or the path to the
170
     * <a href="https://docs.oracle.com/javase/8/docs/technotes/guides/security/p11guide.html#Config">SunPKCS11 configuration file</a>.
171
     */
172
    PKCS11(false, true) {
1 ✔
173
        @Override
174
        void validate(KeyStoreBuilder params) {
175
            if (params.keystore() == null) {
1 ✔
176
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must be set");
1 ✔
177
            }
178
        }
1 ✔
179

180
        @Override
181
        Provider getProvider(KeyStoreBuilder params) {
182
            // the keystore parameter is either the provider name or the SunPKCS11 configuration file
183
            if (params.createFile(params.keystore()).exists()) {
1 ✔
184
                return ProviderUtils.createSunPKCS11Provider(params.keystore());
×
185
            } else if (params.keystore().startsWith("SunPKCS11-")) {
1 ✔
186
                Provider provider = Security.getProvider(params.keystore());
1 ✔
187
                if (provider == null) {
1 ✔
188
                    throw new IllegalArgumentException("Security provider " + params.keystore() + " not found");
1 ✔
189
                }
190
                return provider;
×
191
            } else {
192
                throw new IllegalArgumentException("keystore " + params.parameterName() + " should either refer to the SunPKCS11 configuration file or to the name of the provider configured in jre/lib/security/java.security");
1 ✔
193
            }
194
        }
195
    },
196

197
    /**
198
     * OpenPGP card. OpenPGP cards contain up to 3 keys, one for signing, one for encryption, and one for authentication.
199
     * All of them can be used for code signing (except encryption keys based on an elliptic curve). The alias
200
     * to select the key is either, <code>SIGNATURE</code>, <code>ENCRYPTION</code> or <code>AUTHENTICATION</code>.
201
     * This keystore can be used with a Nitrokey (non-HSM models) or a Yubikey. If multiple devices are connected,
202
     * the keystore parameter can be used to specify the name of the one to use. This keystore type doesn't require
203
     * any external library to be installed.
204
     */
205
    OPENPGP(false, false) {
1 ✔
206
        @Override
207
        void validate(KeyStoreBuilder params) {
208
            if (params.storepass() == null) {
1 ✔
209
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the PIN");
1 ✔
210
            }
211
        }
×
212

213
        @Override
214
        Provider getProvider(KeyStoreBuilder params) {
215
            try {
216
                return new SigningServiceJcaProvider(new OpenPGPCardSigningService(params.keystore(), params.storepass(), params.certfile() != null ? getCertificateStore(params) : null));
×
217
            } catch (CardException e) {
×
218
                throw new IllegalStateException("Failed to initialize the OpenPGP card", e);
×
219
            }
220
        }
221
    },
222

223
    /**
224
     * OpenSC supported smart card.
225
     * This keystore requires the installation of <a href="https://github.com/OpenSC/OpenSC">OpenSC</a>.
226
     * If multiple devices are connected, the keystore parameter can be used to specify the name of the one to use.
227
     */
228
    OPENSC(false, true) {
1 ✔
229
        @Override
230
        Provider getProvider(KeyStoreBuilder params) {
231
            return OpenSC.getProvider(params.keystore());
×
232
        }
233
    },
234

235
    /**
236
     * PIV card. PIV cards contain up to 24 private keys and certificates. The alias to select the key is either,
237
     * <code>AUTHENTICATION</code>, <code>SIGNATURE</code>, <code>KEY_MANAGEMENT</code>, <code>CARD_AUTHENTICATION</code>,
238
     * or <code>RETIRED&lt;1-20&gt;</code>. Slot numbers are also accepted (for example <code>9c</code> for the digital
239
     * signature key). If multiple devices are connected, the keystore parameter can be used to specify the name
240
     * of the one to use. This keystore type doesn't require any external library to be installed.
241
     */
242
    PIV(false, false) {
1 ✔
243
        @Override
244
        void validate(KeyStoreBuilder params) {
245
            if (params.storepass() == null) {
1 ✔
246
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the PIN");
1 ✔
247
            }
248
        }
×
249

250
        @Override
251
        Provider getProvider(KeyStoreBuilder params) {
252
            try {
253
                return new SigningServiceJcaProvider(new PIVCardSigningService(params.keystore(), params.storepass(), params.certfile() != null ? getCertificateStore(params) : null));
×
254
            } catch (CardException e) {
×
255
                throw new IllegalStateException("Failed to initialize the PIV card", e);
×
256
            }
257
        }
258
    },
259

260
    /**
261
     * Nitrokey HSM. This keystore requires the installation of <a href="https://github.com/OpenSC/OpenSC">OpenSC</a>.
262
     * Other Nitrokeys based on the OpenPGP card standard are also supported with this storetype, but an X.509
263
     * certificate must be imported into the Nitrokey (using the gnupg writecert command). Keys without certificates
264
     * are ignored. Otherwise the {@link #OPENPGP} type should be used.
265
     */
266
    NITROKEY(false, true) {
1 ✔
267
        @Override
268
        Provider getProvider(KeyStoreBuilder params) {
269
            return OpenSC.getProvider(params.keystore() != null ? params.keystore() : "Nitrokey");
×
270
        }
271
    },
272

273
    /**
274
     * YubiKey PIV. This keystore requires the ykcs11 library from the <a href="https://developers.yubico.com/yubico-piv-tool/">Yubico PIV Tool</a>
275
     * to be installed at the default location. On Windows, the path to the library must be specified in the
276
     * <code>PATH</code> environment variable.
277
     */
278
    YUBIKEY(false, true) {
1 ✔
279
        @Override
280
        Provider getProvider(KeyStoreBuilder params) {
281
            return YubiKey.getProvider();
×
282
        }
283

284
        @Override
285
        Set<String> getAliases(KeyStore keystore) throws KeyStoreException {
286
            Set<String> aliases = super.getAliases(keystore);
×
287
            // the attestation certificate is never used for signing
288
            aliases.remove("X.509 Certificate for PIV Attestation");
×
289
            return aliases;
×
290
        }
291
    },
292

293
    /**
294
     * AWS Key Management Service (KMS). AWS KMS stores only the private key, the certificate must be provided
295
     * separately. The keystore parameter references the AWS region.
296
     *
297
     * <p>The AWS access key, secret key, and optionally the session token, are concatenated and used as
298
     * the storepass parameter; if the latter is not provided, Jsign attempts to fetch the credentials from
299
     * the environment variables (<code>AWS_ACCESS_KEY_ID</code>, <code>AWS_SECRET_ACCESS_KEY</code> and
300
     * <code>AWS_SESSION_TOKEN</code>), from the ECS container credentials endpoint, or from the IMDSv2
301
     * service when running on an AWS EC2 instance.</p>
302
     *
303
     * <p>In any case, the credentials must allow the following actions: <code>kms:ListKeys</code>,
304
     * <code>kms:DescribeKey</code> and <code>kms:Sign</code>.</p>
305
     * */
306
    AWS(false, false) {
1 ✔
307
        @Override
308
        void validate(KeyStoreBuilder params) {
309
            if (params.keystore() == null) {
1 ✔
310
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must specify the AWS region");
1 ✔
311
            }
312
            if (params.certfile() == null) {
1 ✔
313
                throw new IllegalArgumentException("certfile " + params.parameterName() + " must be set");
1 ✔
314
            }
315
        }
1 ✔
316

317
        @Override
318
        Provider getProvider(KeyStoreBuilder params) {
319
            AmazonCredentials credentials;
320
            if (params.storepass() != null) {
1 ✔
321
                credentials = AmazonCredentials.parse(params.storepass());
1 ✔
322
            } else {
323
                try {
324
                    credentials = AmazonCredentials.getDefault();
×
325
                } catch (UnknownServiceException e) {
1 ✔
326
                    throw new IllegalArgumentException("storepass " + params.parameterName()
1 ✔
327
                            + " must specify the AWS credentials: <accessKey>|<secretKey>[|<sessionToken>]"
328
                            + ", when not running from ECS or an EC2 instance", e);
329
                } catch (IOException e) {
×
330
                    throw new RuntimeException("Failed fetching temporary credentials from ECS and IMDSv2 services", e);
×
331
                }
×
332
            }
333

334
            return new SigningServiceJcaProvider(new AmazonSigningService(params.keystore(), credentials, getCertificateStore(params)));
1 ✔
335
        }
336
    },
337

338
    /**
339
     * Azure Key Vault. The keystore parameter specifies the name of the key vault, either the short name
340
     * (e.g. <code>myvault</code>), or the full URL (e.g. <code>https://myvault.vault.azure.net</code>).
341
     * The storepass parameter specifies either the Azure API access token obtained with the Azure CLI:
342
     *
343
     * <pre>  az account get-access-token --resource https://vault.azure.net</pre>
344
     *
345
     * Or the Azure credentials used to fetch the access token in the form <code>&lt;tenantId&gt;|&lt;clientId&gt;|&lt;clientSecret&gt;</code>.
346
     * If the password isn't specified the credentials are read from the <code>AZURE_TENANT_ID</code>,
347
     * <code>AZURE_CLIENT_ID</code> and <code>AZURE_CLIENT_SECRET</code> environment variables.
348
     */
349
    AZUREKEYVAULT(false, false) {
1 ✔
350
        @Override
351
        void validate(KeyStoreBuilder params) {
352
            if (params.keystore() == null) {
1 ✔
353
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must specify the Azure vault name");
1 ✔
354
            }
355
            if (params.storepass() != null && params.storepass().contains("|") && params.storepass().split("\\|").length != 3) {
1 ✔
356
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the Azure access token or the Azure credentials: <tenantId>|<clientId>|<clientSecret>");
1 ✔
357
            }
358
        }
1 ✔
359

360
        @Override
361
        Provider getProvider(KeyStoreBuilder params) {
362
            AzureCredentials credentials = params.storepass() != null ? new AzureCredentials(params.storepass()) : AzureCredentials.getDefault();
1 ✔
363
            return new SigningServiceJcaProvider(new AzureKeyVaultSigningService(params.keystore(), credentials));
1 ✔
364
        }
365
    },
366

367
    /**
368
     * DigiCert ONE. Certificates and keys stored in the DigiCert ONE Secure Software Manager can be used directly
369
     * without installing the DigiCert client tools. The API key, the PKCS#12 keystore holding the client certificate
370
     * and its password are combined to form the storepass parameter: <code>&lt;api-key&gt;|&lt;keystore&gt;|&lt;password&gt;</code>.
371
     */
372
    DIGICERTONE(false, false) {
1 ✔
373
        @Override
374
        void validate(KeyStoreBuilder params) {
375
            if (params.storepass() == null || params.storepass().split("\\|").length != 3) {
1 ✔
376
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the DigiCert ONE API key and the client certificate: <apikey>|<keystore>|<password>");
1 ✔
377
            }
378
        }
1 ✔
379

380
        @Override
381
        Provider getProvider(KeyStoreBuilder params) {
382
            String[] elements = params.storepass().split("\\|");
1 ✔
383
            return new SigningServiceJcaProvider(new DigiCertOneSigningService(params.keystore(), elements[0], params.createFile(elements[1]), elements[2]));
1 ✔
384
        }
385
    },
386

387
    /**
388
     * SSL.com eSigner. The SSL.com username and password are used as the keystore password (<code>&lt;username&gt;|&lt;password&gt;</code>),
389
     * and the base64 encoded TOTP secret is used as the key password.
390
     */
391
    ESIGNER(false, false) {
1 ✔
392
        @Override
393
        void validate(KeyStoreBuilder params) {
394
            if (params.storepass() == null || !params.storepass().contains("|")) {
1 ✔
395
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the SSL.com username and password: <username>|<password>");
1 ✔
396
            }
397
        }
1 ✔
398

399
        @Override
400
        Provider getProvider(KeyStoreBuilder params) {
401
            String[] elements = params.storepass().split("\\|", 2);
1 ✔
402
            String endpoint = params.keystore() != null ? params.keystore() : "https://cs.ssl.com";
1 ✔
403
            try {
404
                return new SigningServiceJcaProvider(new ESignerSigningService(endpoint, elements[0], elements[1]));
1 ✔
405
            } catch (IOException e) {
1 ✔
406
                throw new IllegalStateException("Authentication failed with SSL.com", e);
1 ✔
407
            }
408
        }
409
    },
410

411
    /**
412
     * Google Cloud KMS. Google Cloud KMS stores only the private key, the certificate must be provided separately.
413
     * The keystore parameter references the path of the keyring. The alias can specify either the full path of the key,
414
     * or only the short name. If the version is omitted the most recent one will be picked automatically.
415
     */
416
    GOOGLECLOUD(false, false) {
1 ✔
417
        @Override
418
        void validate(KeyStoreBuilder params) {
419
            if (params.keystore() == null) {
1 ✔
420
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must specify the Google Cloud keyring");
1 ✔
421
            }
422
            if (!params.keystore().matches("projects/[^/]+/locations/[^/]+/keyRings/[^/]+")) {
1 ✔
423
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must specify the path of the keyring (projects/{projectName}/locations/{location}/keyRings/{keyringName})");
1 ✔
424
            }
425
            if (params.storepass() == null) {
1 ✔
426
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the Google Cloud API access token");
1 ✔
427
            }
428
            if (params.certfile() == null) {
1 ✔
429
                throw new IllegalArgumentException("certfile " + params.parameterName() + " must be set");
1 ✔
430
            }
431
        }
1 ✔
432

433
        @Override
434
        Provider getProvider(KeyStoreBuilder params) {
435
            return new SigningServiceJcaProvider(new GoogleCloudSigningService(params.keystore(), params.storepass(), getCertificateStore(params)));
1 ✔
436
        }
437
    },
438

439
    /**
440
     * HashiCorp Vault secrets engine (Transit or GCPKMS). The certificate must be provided separately. The keystore
441
     * parameter references the URL of the HashiCorp Vault secrets engine (<code>https://vault.example.com/v1/gcpkms</code>).
442
     * The alias parameter specifies the name of the key in Vault. For the Google Cloud KMS secrets engine, the version
443
     * of the Google Cloud key is appended to the key name, separated by a colon character. (<code>mykey:1</code>).
444
     */
445
    HASHICORPVAULT(false, false) {
1 ✔
446
        @Override
447
        void validate(KeyStoreBuilder params) {
448
            if (params.keystore() == null) {
1 ✔
449
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must specify the HashiCorp Vault secrets engine URL");
1 ✔
450
            }
451
            if (params.storepass() == null) {
1 ✔
452
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the HashiCorp Vault token");
1 ✔
453
            }
454
            if (params.certfile() == null) {
1 ✔
455
                throw new IllegalArgumentException("certfile " + params.parameterName() + " must be set");
1 ✔
456
            }
457
        }
1 ✔
458

459
        @Override
460
        Provider getProvider(KeyStoreBuilder params) {
461
            return new SigningServiceJcaProvider(new HashiCorpVaultSigningService(params.keystore(), params.storepass(), getCertificateStore(params)));
1 ✔
462
        }
463
    },
464

465
    /**
466
     * SafeNet eToken
467
     * This keystore requires the installation of the SafeNet Authentication Client.
468
     */
469
    ETOKEN(false, true) {
1 ✔
470
        @Override
471
        Provider getProvider(KeyStoreBuilder params) {
472
            return SafeNetEToken.getProvider(params.keystore());
×
473
        }
474
    },
475

476
    /**
477
     * Oracle Cloud Infrastructure Key Management Service. This keystore requires the <a href="https://docs.oracle.com/en-us/iaas/Content/API/Concepts/sdkconfig.htm">configuration file</a>
478
     * or the <a href="https://docs.oracle.com/en-us/iaas/Content/API/SDKDocs/clienvironmentvariables.htm">environment
479
     * variables</a> used by the OCI CLI. The storepass parameter specifies the path to the configuration file
480
     * (<code>~/.oci/config</code> by default). If the configuration file contains multiple profiles, the name of the
481
     * non-default profile to use is appended to the storepass (for example <code>~/.oci/config|PROFILE</code>).
482
     * The keypass parameter may be used to specify the passphrase of the key file used for signing the requests to
483
     * the OCI API if it isn't set in the configuration file.
484
     *
485
     * <p>The certificate must be provided separately using the certfile parameter. The alias specifies the OCID
486
     * of the key.</p>
487
     */
488
    ORACLECLOUD(false, false) {
1 ✔
489
        @Override
490
        void validate(KeyStoreBuilder params) {
491
            if (params.certfile() == null) {
1 ✔
492
                throw new IllegalArgumentException("certfile " + params.parameterName() + " must be set");
1 ✔
493
            }
494
        }
1 ✔
495

496
        @Override
497
        Provider getProvider(KeyStoreBuilder params) {
498
            OracleCloudCredentials credentials = new OracleCloudCredentials();
1 ✔
499
            try {
500
                File config = null;
1 ✔
501
                String profile = null;
1 ✔
502
                if (params.storepass() != null) {
1 ✔
503
                    String[] elements = params.storepass().split("\\|", 2);
1 ✔
504
                    config = new File(elements[0]);
1 ✔
505
                    if (elements.length > 1) {
1 ✔
506
                        profile = elements[1];
1 ✔
507
                    }
508
                }
509
                credentials.load(config, profile);
1 ✔
510
                credentials.loadFromEnvironment();
1 ✔
511
                if (params.keypass() != null) {
1 ✔
512
                    credentials.setPassphrase(params.keypass());
×
513
                }
514
            } catch (IOException e) {
×
515
                throw new RuntimeException("An error occurred while fetching the Oracle Cloud credentials", e);
×
516
            }
1 ✔
517
            return new SigningServiceJcaProvider(new OracleCloudSigningService(credentials, getCertificateStore(params)));
1 ✔
518
        }
519
    },
520

521
    /**
522
     * Azure Trusted Signing Service. The keystore parameter specifies the API endpoint (for example
523
     * <code>weu.codesigning.azure.net</code>). The storepass parameter specifies either the Azure API access token
524
     * obtained with the Azure CLI:
525
     *
526
     * <pre>  az account get-access-token --resource https://codesigning.azure.net</pre>
527
     *
528
     * Or the Azure credentials used to fetch the access token in the form <code>&lt;tenantId&gt;|&lt;clientId&gt;|&lt;clientSecret&gt;</code>.
529
     * If the password isn't specified the credentials are read from the <code>AZURE_TENANT_ID</code>,
530
     * <code>AZURE_CLIENT_ID</code> and <code>AZURE_CLIENT_SECRET</code> environment variables.
531
     */
532
    TRUSTEDSIGNING(false, false) {
1 ✔
533
        @Override
534
        void validate(KeyStoreBuilder params) {
535
            if (params.keystore() == null) {
1 ✔
536
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must specify the Azure endpoint (<region>.codesigning.azure.net)");
1 ✔
537
            }
538
            if (params.storepass() != null && params.storepass().contains("|") && params.storepass().split("\\|").length != 3) {
1 ✔
539
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the Azure access token or the Azure credentials: <tenantId>|<clientId>|<clientSecret>");
1 ✔
540
            }
541
        }
1 ✔
542

543
        @Override
544
        Provider getProvider(KeyStoreBuilder params) {
545
            AzureCredentials credentials = params.storepass() != null ? new AzureCredentials(params.storepass()) : AzureCredentials.getDefault();
1 ✔
546
            return new SigningServiceJcaProvider(new AzureTrustedSigningService(params.keystore(), credentials));
1 ✔
547
        }
548
    },
549

550
    GARASIGN(false, false) {
1 ✔
551
        @Override
552
        void validate(KeyStoreBuilder params) {
553
            if (params.storepass() == null || params.storepass().split("\\|").length > 3) {
1 ✔
554
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the GaraSign username/password and/or the path to the keystore containing the TLS client certificate: <username>|<password>, <certificate>, or <username>|<password>|<certificate>");
1 ✔
555
            }
556
        }
1 ✔
557

558
        @Override
559
        Provider getProvider(KeyStoreBuilder params) {
560
            String[] elements = params.storepass().split("\\|");
1 ✔
561
            String username = null;
1 ✔
562
            String password = null;
1 ✔
563
            String certificate = null;
1 ✔
564
            if (elements.length == 1) {
1 ✔
565
                certificate = elements[0];
1 ✔
566
            } else if (elements.length == 2) {
1 ✔
567
                username = elements[0];
1 ✔
568
                password = elements[1];
1 ✔
569
            } else if (elements.length == 3) {
1 ✔
570
                username = elements[0];
1 ✔
571
                password = elements[1];
1 ✔
572
                certificate = elements[2];
1 ✔
573
            }
574

575
            GaraSignCredentials credentials = new GaraSignCredentials(username, password, certificate, params.keypass());
1 ✔
576
            return new SigningServiceJcaProvider(new GaraSignSigningService(params.keystore(), credentials));
1 ✔
577
        }
578
    },
579

580
    /**
581
     * Keyfactor SignServer. This keystore requires a Plain Signer worker, preferably configured to allow client-side
582
     * hashing (with the properties <code>CLIENTSIDEHASHING</code> or <code>ALLOW_CLIENTSIDEHASHING_OVERRIDE</code> set
583
     * to true), and the <code>SIGNATUREALGORITHM</code> property set to <code>NONEwithRSA</code> or <code>NONEwithECDSA</code>.
584
     * The worker may be configured with server-side hashing (i.e. with <code>CLIENTSIDEHASHING</code> and
585
     * <code>ALLOW_CLIENTSIDEHASHING_OVERRIDE</code> set to <code>false</code>, and a proper <code>SIGNATUREALGORITHM</code>
586
     * set), in this case the worker name or id in the alias has to be suffixed with <code>|serverside</code>.
587
     *
588
     * <p>If necessary the authentication is performed by specifying the username/password or the TLS client certificate
589
     * in the storepass parameter. If the TLS client certificate is stored in a password protected keystore, the password
590
     * is specified in the keypass parameter. The keystore parameter references the URL of the SignServer REST API. The
591
     * alias parameter specifies the id or the name of the worker.</p>
592
     */
593
    SIGNSERVER(false, false) {
1 ✔
594
        @Override
595
        void validate(KeyStoreBuilder params) {
596
            if (params.keystore() == null) {
1 ✔
597
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must specify the SignServer API endpoint (e.g. https://example.com/signserver/)");
1 ✔
598
            }
599
            if (params.storepass() != null && params.storepass().split("\\|").length > 2) {
1 ✔
600
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the SignServer username/password or the path to the keystore containing the TLS client certificate: <username>|<password> or <certificate>");
1 ✔
601
            }
602
        }
1 ✔
603

604
        @Override
605
        Provider getProvider(KeyStoreBuilder params) {
606
            String username = null;
1 ✔
607
            String password = null;
1 ✔
608
            String certificate = null;
1 ✔
609
            if (params.storepass() != null) {
1 ✔
610
                String[] elements = params.storepass().split("\\|");
1 ✔
611
                if (elements.length == 1) {
1 ✔
612
                    certificate = elements[0];
×
613
                } else if (elements.length == 2) {
1 ✔
614
                    username = elements[0];
1 ✔
615
                    password = elements[1];
1 ✔
616
                }
617
            }
618

619
            SignServerCredentials credentials = new SignServerCredentials(username, password, certificate, params.keypass());
1 ✔
620
            return new SigningServiceJcaProvider(new SignServerSigningService(params.keystore(), credentials));
1 ✔
621
        }
622
    },
623

624
    /**
625
     * SignPath. The keystore parameter specifies the organization, and the storepass parameter the API access token.
626
     * The alias parameter is the concatenation of the project slug and the signing policy slug, separated by a slash
627
     * character (e.g. <code>myproject/mypolicy</code>).
628
     */
629
    SIGNPATH(false, false) {
1 ✔
630
        @Override
631
        void validate(KeyStoreBuilder params) {
632
            if (params.keystore() == null) {
1 ✔
633
                throw new IllegalArgumentException("keystore " + params.parameterName() + " must specify the SignPath organization id (e.g. eacd4b78-6038-4450-9eec-4acd1c7ba6f1)");
1 ✔
634
            }
635
            if (params.storepass() == null) {
1 ✔
636
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the SignPath API access token");
1 ✔
637
            }
638
        }
1 ✔
639

640
        @Override
641
        Provider getProvider(KeyStoreBuilder params) {
642
            return new SigningServiceJcaProvider(new SignPathSigningService(params.keystore(), params.storepass()));
1 ✔
643
        }
644
    },
645

646
    /**
647
     * CryptoCertum card. No PKCS#11 module is required, Jsign communicates directly with the card and uses the keys
648
     * and certificates stored in the common file (the secure profile containing eIDAS certificates is not supported).
649
     */
650
    CRYPTOCERTUM(false, false) {
1 ✔
651
        @Override
652
        void validate(KeyStoreBuilder params) {
653
            if (params.storepass() == null) {
×
654
                throw new IllegalArgumentException("storepass " + params.parameterName() + " must specify the PIN");
×
655
            }
656
        }
×
657

658
        @Override
659
        Provider getProvider(KeyStoreBuilder params) {
660
            try {
661
                return new SigningServiceJcaProvider(new CryptoCertumCardSigningService(params.storepass()));
×
662
            } catch (CardException e) {
×
663
                throw new IllegalStateException("Failed to initialize the CryptoCertum card", e);
×
664
            }
665
        }
666
    };
667

668
    /** Tells if the keystore is contained in a local file */
669
    private final boolean fileBased;
670

671
    /** Tells if the keystore is actually a PKCS#11 keystore */
672
    private final boolean pkcs11;
673

674
    KeyStoreType(boolean fileBased, boolean pkcs11) {
1 ✔
675
        this.fileBased = fileBased;
1 ✔
676
        this.pkcs11 = pkcs11;
1 ✔
677
    }
1 ✔
678

679
    /**
680
     * Validates the keystore parameters.
681
     */
682
    void validate(KeyStoreBuilder params) throws IllegalArgumentException {
683
    }
×
684

685
    /**
686
     * Returns the security provider to use the keystore.
687
     */
688
    Provider getProvider(KeyStoreBuilder params) {
689
        return null;
1 ✔
690
    }
691

692
    /**
693
     * Build the keystore.
694
     */
695
    KeyStore getKeystore(KeyStoreBuilder params, Provider provider) throws KeyStoreException {
696
        KeyStore ks;
697
        try {
698
            KeyStoreType storetype = pkcs11 ? PKCS11 : this;
1 ✔
699
            if (provider != null) {
1 ✔
700
                ks = KeyStore.getInstance(storetype.name(), provider);
1 ✔
701
            } else {
702
                ks = KeyStore.getInstance(storetype.name());
1 ✔
703
            }
704
        } catch (KeyStoreException e) {
×
705
            throw new KeyStoreException("keystore type '" + name() + "' is not supported" + (provider != null ? " with security provider " + provider.getName() : ""), e);
×
706
        }
1 ✔
707

708
        try {
709
            try (FileInputStream in = fileBased ? new FileInputStream(params.createFile(params.keystore())) : null) {
1 ✔
710
                ks.load(in, params.storepass() != null ? params.storepass().toCharArray() : null);
1 ✔
711
            }
712
        } catch (Exception e) {
1 ✔
713
            throw new KeyStoreException("Unable to load the " + name() + " keystore" + (params.keystore() != null ? " " + params.keystore() : ""), e);
1 ✔
714
        }
1 ✔
715

716
        return ks;
1 ✔
717
    }
718

719
    /**
720
     * Returns the aliases of the keystore available for signing.
721
     */
722
    Set<String> getAliases(KeyStore keystore) throws KeyStoreException {
723
        return new LinkedHashSet<>(Collections.list(keystore.aliases()));
1 ✔
724
    }
725

726
    /**
727
     * Guess the type of the keystore from the header or the extension of the file.
728
     *
729
     * @param path   the path to the keystore
730
     */
731
    static KeyStoreType of(File path) {
732
        // guess the type of the keystore from the header of the file
733
        if (path.exists()) {
1 ✔
734
            try (FileInputStream in = new FileInputStream(path)) {
1 ✔
735
                byte[] header = new byte[4];
1 ✔
736
                in.read(header);
1 ✔
737
                ByteBuffer buffer = ByteBuffer.wrap(header);
1 ✔
738
                if (buffer.get(0) == 0x30) {
1 ✔
739
                    return PKCS12;
1 ✔
740
                } else if ((buffer.getInt(0) & 0xFFFFFFFFL) == 0xCECECECEL) {
1 ✔
741
                    return JCEKS;
1 ✔
742
                } else if ((buffer.getInt(0) & 0xFFFFFFFFL) == 0xFEEDFEEDL) {
1 ✔
743
                    return JKS;
1 ✔
744
                }
745
            } catch (IOException e) {
1 ✔
746
                throw new RuntimeException("Unable to load the keystore " + path, e);
×
747
            }
1 ✔
748
        }
749

750
        // guess the type of the keystore from the extension of the file
751
        String filename = path.getName().toLowerCase();
1 ✔
752
        if (filename.endsWith(".p12") || filename.endsWith(".pfx")) {
1 ✔
753
            return PKCS12;
1 ✔
754
        } else if (filename.endsWith(".jceks")) {
1 ✔
755
            return JCEKS;
1 ✔
756
        } else if (filename.endsWith(".jks")) {
1 ✔
757
            return JKS;
1 ✔
758
        } else {
759
            return null;
1 ✔
760
        }
761
    }
762

763
    private static Function<String, Certificate[]> getCertificateStore(KeyStoreBuilder params) {
764
        return alias -> {
1 ✔
765
            if (alias == null || alias.isEmpty()) {
×
766
                return null;
×
767
            }
768

769
            try {
770
                return CertificateUtils.loadCertificateChain(params.certfile());
×
771
            } catch (IOException | CertificateException e) {
×
772
                throw new RuntimeException("Failed to load the certificate from " + params.certfile(), e);
×
773
            }
774
        };
775
    }
776
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc