|
| 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.jca; |
| 18 | + |
| 19 | +import java.io.InputStream; |
| 20 | +import java.security.AccessController; |
| 21 | +import java.security.InvalidParameterException; |
| 22 | +import java.security.Key; |
| 23 | +import java.security.KeyStore; |
| 24 | +import java.security.KeyStoreException; |
| 25 | +import java.security.NoSuchAlgorithmException; |
| 26 | +import java.security.PrivilegedAction; |
| 27 | +import java.security.Provider; |
| 28 | +import java.security.UnrecoverableKeyException; |
| 29 | +import java.security.cert.Certificate; |
| 30 | +import java.util.Enumeration; |
| 31 | + |
| 32 | +import net.jsign.DigestAlgorithm; |
| 33 | +import net.jsign.KeyStoreBuilder; |
| 34 | +import net.jsign.KeyStoreType; |
| 35 | + |
| 36 | +/** |
| 37 | + * JCA provider using a Jsign keystore and compatible with jarsigner. |
| 38 | + * |
| 39 | + * <p>The provider must be configured with the keystore parameter (the value depends on the keystore type). |
| 40 | + * The type of the keystore is one of the names from the {@link KeyStoreType} enum.</p> |
| 41 | + * |
| 42 | + * <p>Example:</p> |
| 43 | + * <pre> |
| 44 | + * Provider provider = new JsignJcaProvider(); |
| 45 | + * provider.configure(vaultname) |
| 46 | + * KeyStore keystore = KeyStore.getInstance(AZUREKEYVAULT.name(), provider); |
| 47 | + * keystore.load(null, accessToken); |
| 48 | + * </pre> |
| 49 | + * |
| 50 | + * @since 5.1 |
| 51 | + */ |
| 52 | +public class JsignJcaProvider extends Provider { |
| 53 | + |
| 54 | + private String keystore; |
| 55 | + |
| 56 | + public JsignJcaProvider() { |
| 57 | + super("Jsign", 1.0, "Jsign security provider"); |
| 58 | + |
| 59 | + AccessController.doPrivileged((PrivilegedAction<Object>) () -> { |
| 60 | + for (KeyStoreType type : KeyStoreType.values()) { |
| 61 | + putService(new ProviderService(this, "KeyStore", type.name(), JsignJcaKeyStore.class.getName(), () -> new JsignJcaKeyStore(type, keystore))); |
| 62 | + } |
| 63 | + for (String alg : new String[]{"RSA", "ECDSA"}) { |
| 64 | + for (DigestAlgorithm digest : DigestAlgorithm.values()) { |
| 65 | + if (digest != DigestAlgorithm.MD5) { |
| 66 | + String algorithm = digest.name() + "with" + alg; |
| 67 | + putService(new ProviderService(this, "Signature", algorithm, SigningServiceSignature.class.getName(), () -> new SigningServiceSignature(algorithm))); |
| 68 | + } |
| 69 | + } |
| 70 | + } |
| 71 | + return null; |
| 72 | + }); |
| 73 | + } |
| 74 | + |
| 75 | + public Provider configure(String configArg) throws InvalidParameterException { |
| 76 | + this.keystore = configArg; |
| 77 | + |
| 78 | + return this; |
| 79 | + } |
| 80 | + |
| 81 | + static class JsignJcaKeyStore extends AbstractKeyStoreSpi { |
| 82 | + |
| 83 | + private KeyStoreBuilder builder = new KeyStoreBuilder(); |
| 84 | + private KeyStore keystore; |
| 85 | + |
| 86 | + public JsignJcaKeyStore(KeyStoreType type, String keystore) { |
| 87 | + builder.storetype(type); |
| 88 | + builder.keystore(keystore); |
| 89 | + builder.certfile(""); |
| 90 | + } |
| 91 | + |
| 92 | + private KeyStore getKeyStore() throws KeyStoreException { |
| 93 | + if (keystore == null) { |
| 94 | + keystore = builder.build(); |
| 95 | + } |
| 96 | + |
| 97 | + return keystore; |
| 98 | + } |
| 99 | + |
| 100 | + @Override |
| 101 | + public Key engineGetKey(String alias, char[] password) throws UnrecoverableKeyException { |
| 102 | + if (password != null) { |
| 103 | + builder.keypass(new String(password)); |
| 104 | + } |
| 105 | + try { |
| 106 | + return getKeyStore().getKey(alias, password); |
| 107 | + } catch (UnrecoverableKeyException e) { |
| 108 | + e.printStackTrace(); // because jarsigner swallows the root cause and hides what's going on |
| 109 | + throw e; |
| 110 | + } catch (KeyStoreException | NoSuchAlgorithmException e) { |
| 111 | + throw new RuntimeException(e); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public Certificate[] engineGetCertificateChain(String alias) { |
| 117 | + try { |
| 118 | + return getKeyStore().getCertificateChain(alias); |
| 119 | + } catch (KeyStoreException e) { |
| 120 | + return null; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + @Override |
| 125 | + public Enumeration<String> engineAliases() { |
| 126 | + try { |
| 127 | + return getKeyStore().aliases(); |
| 128 | + } catch (KeyStoreException e) { |
| 129 | + throw new RuntimeException(e); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void engineLoad(InputStream stream, char[] password) { |
| 135 | + if (password != null) { |
| 136 | + builder.storepass(new String(password)); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments