|
| 1 | +/* |
| 2 | + * Copyright 2024 Björn Kautler |
| 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 net.jsign.DigestAlgorithm; |
| 20 | + |
| 21 | +import javax.net.ssl.HttpsURLConnection; |
| 22 | +import javax.net.ssl.KeyManagerFactory; |
| 23 | +import javax.net.ssl.SSLContext; |
| 24 | +import java.io.ByteArrayInputStream; |
| 25 | +import java.io.IOException; |
| 26 | +import java.security.GeneralSecurityException; |
| 27 | +import java.security.KeyStore; |
| 28 | +import java.security.KeyStoreException; |
| 29 | +import java.security.SecureRandom; |
| 30 | +import java.security.UnrecoverableKeyException; |
| 31 | +import java.security.cert.Certificate; |
| 32 | +import java.security.cert.CertificateException; |
| 33 | +import java.security.cert.CertificateFactory; |
| 34 | +import java.util.Base64; |
| 35 | +import java.util.HashMap; |
| 36 | +import java.util.LinkedHashMap; |
| 37 | +import java.util.List; |
| 38 | +import java.util.Map; |
| 39 | + |
| 40 | +import static java.nio.charset.StandardCharsets.UTF_8; |
| 41 | +import static java.util.Collections.emptyList; |
| 42 | +import static java.util.Objects.requireNonNull; |
| 43 | + |
| 44 | +/** |
| 45 | + * Signing service using the SignServer REST interface. |
| 46 | + * |
| 47 | + * @since 7.0 |
| 48 | + */ |
| 49 | +public class SignServerSigningService implements SigningService { |
| 50 | + /** Cache of certificates indexed by id or alias */ |
| 51 | + private final Map<String, Certificate[]> certificates = new HashMap<>(); |
| 52 | + |
| 53 | + private final RESTClient client; |
| 54 | + |
| 55 | + /** |
| 56 | + * Creates a new SignServer signing service. |
| 57 | + * |
| 58 | + * @param endpoint the SignServer API endpoint (for example <tt>https://signserver.company.com/signserver/</tt>) |
| 59 | + * @param credentials the SignServer credentials |
| 60 | + */ |
| 61 | + public SignServerSigningService(String endpoint, SignServerCredentials credentials) { |
| 62 | + this.client = new RESTClient( |
| 63 | + requireNonNull(endpoint, "You need to provide the SignServer endpoint URL as keystore parameter") |
| 64 | + + (endpoint.endsWith("/") ? "" : "/")) |
| 65 | + .authentication(credentials::addAuthentication) |
| 66 | + .errorHandler(response -> response.get("error").toString()); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public String getName() { |
| 71 | + return "SignServer"; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public List<String> aliases() { |
| 76 | + return emptyList(); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public Certificate[] getCertificateChain(String alias) throws KeyStoreException { |
| 81 | + if (!certificates.containsKey(alias)) { |
| 82 | + try { |
| 83 | + Map<String, ?> response = client.post(getResourcePath(alias), "{\"data\":\"\"}"); |
| 84 | + String encodedCertificate = response.get("signerCertificate").toString(); |
| 85 | + byte[] certificateBytes = Base64.getDecoder().decode(encodedCertificate); |
| 86 | + Certificate certificate = CertificateFactory |
| 87 | + .getInstance("X.509") |
| 88 | + .generateCertificate(new ByteArrayInputStream(certificateBytes)); |
| 89 | + certificates.put(alias, new Certificate[]{certificate}); |
| 90 | + } catch (IOException | CertificateException e) { |
| 91 | + throw new KeyStoreException(e); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + return certificates.get(alias); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public SigningServicePrivateKey getPrivateKey(String alias, char[] password) throws UnrecoverableKeyException { |
| 100 | + try { |
| 101 | + String algorithm = getCertificateChain(alias)[0].getPublicKey().getAlgorithm(); |
| 102 | + return new SigningServicePrivateKey(alias, algorithm, this); |
| 103 | + } catch (KeyStoreException e) { |
| 104 | + throw (UnrecoverableKeyException) new UnrecoverableKeyException().initCause(e); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public byte[] sign(SigningServicePrivateKey privateKey, String algorithm, byte[] data) throws GeneralSecurityException { |
| 110 | + DigestAlgorithm digestAlgorithm = DigestAlgorithm.of(algorithm.substring(0, algorithm.toLowerCase().indexOf("with"))); |
| 111 | + data = digestAlgorithm.getMessageDigest().digest(data); |
| 112 | + |
| 113 | + Map<String, Object> request = new HashMap<>(); |
| 114 | + request.put("data", Base64.getEncoder().encodeToString(data)); |
| 115 | + request.put("encoding", "BASE64"); |
| 116 | + Map<String, Object> metaData = new HashMap<>(); |
| 117 | + metaData.put("USING_CLIENTSUPPLIED_HASH", true); |
| 118 | + metaData.put("CLIENTSIDE_HASHDIGESTALGORITHM", digestAlgorithm.id); |
| 119 | + request.put("metaData", metaData); |
| 120 | + |
| 121 | + try { |
| 122 | + Map<String, ?> response = client.post(getResourcePath(privateKey.getId()), JsonWriter.format(request)); |
| 123 | + String value = response.get("data").toString(); |
| 124 | + return Base64.getDecoder().decode(value); |
| 125 | + } catch (IOException e) { |
| 126 | + throw new GeneralSecurityException(e); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private String getResourcePath(String alias) { |
| 131 | + return "rest/v1/workers/" + alias + "/process"; |
| 132 | + } |
| 133 | +} |
0 commit comments