-
-
Notifications
You must be signed in to change notification settings - Fork 113
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
236 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
jsign-crypto/src/main/java/net/jsign/jca/SignServerCredentials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
* Copyright 2024 Björn Kautler | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.jsign.jca; | ||
|
||
import net.jsign.KeyStoreBuilder; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import javax.net.ssl.KeyManagerFactory; | ||
import javax.net.ssl.SSLContext; | ||
import java.net.HttpURLConnection; | ||
import java.security.GeneralSecurityException; | ||
import java.security.KeyStore; | ||
import java.security.SecureRandom; | ||
import java.util.Base64; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
|
||
/** | ||
* Credentials for the SignServer REST interface. | ||
* | ||
* @since 7.0 | ||
*/ | ||
public class SignServerCredentials { | ||
|
||
public String username; | ||
public String password; | ||
public KeyStore.Builder keystore; | ||
|
||
public SignServerCredentials(String username, String password, String keystore, String storepass) { | ||
this(username, password, keystore == null ? null : new KeyStoreBuilder().keystore(keystore).storepass(storepass).builder()); | ||
} | ||
|
||
public SignServerCredentials(String username, String password, KeyStore.Builder keystore) { | ||
this.username = username; | ||
this.password = password; | ||
this.keystore = keystore; | ||
} | ||
|
||
void addAuthentication(HttpURLConnection conn) { | ||
if (conn instanceof HttpsURLConnection && keystore != null) { | ||
try { | ||
KeyManagerFactory kmf = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm()); | ||
kmf.init(keystore.getKeyStore(), ((KeyStore.PasswordProtection) keystore.getProtectionParameter("")).getPassword()); | ||
|
||
SSLContext context = SSLContext.getInstance("TLS"); | ||
context.init(kmf.getKeyManagers(), null, new SecureRandom()); | ||
((HttpsURLConnection) conn).setSSLSocketFactory(context.getSocketFactory()); | ||
} catch (GeneralSecurityException e) { | ||
throw new RuntimeException("Unable to load the SignServer client certificate", e); | ||
} | ||
} | ||
|
||
if (username != null) { | ||
conn.setRequestProperty( | ||
"Authorization", | ||
"Basic " + Base64.getEncoder().encodeToString((username + ":" + (password == null ? "" : password)).getBytes(UTF_8))); | ||
} | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
jsign-crypto/src/main/java/net/jsign/jca/SignServerSigningService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
/* | ||
* Copyright 2024 Björn Kautler | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package net.jsign.jca; | ||
|
||
import net.jsign.DigestAlgorithm; | ||
|
||
import javax.net.ssl.HttpsURLConnection; | ||
import javax.net.ssl.KeyManagerFactory; | ||
import javax.net.ssl.SSLContext; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.IOException; | ||
import java.security.GeneralSecurityException; | ||
import java.security.KeyStore; | ||
import java.security.KeyStoreException; | ||
import java.security.SecureRandom; | ||
import java.security.UnrecoverableKeyException; | ||
import java.security.cert.Certificate; | ||
import java.security.cert.CertificateException; | ||
import java.security.cert.CertificateFactory; | ||
import java.util.Base64; | ||
import java.util.HashMap; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.nio.charset.StandardCharsets.UTF_8; | ||
import static java.util.Collections.emptyList; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Signing service using the SignServer REST interface. | ||
* | ||
* @since 7.0 | ||
*/ | ||
public class SignServerSigningService implements SigningService { | ||
/** Cache of certificates indexed by id or alias */ | ||
private final Map<String, Certificate[]> certificates = new HashMap<>(); | ||
|
||
private final RESTClient client; | ||
|
||
/** | ||
* Creates a new SignServer signing service. | ||
* | ||
* @param endpoint the SignServer API endpoint (for example <tt>https://signserver.company.com/signserver/</tt>) | ||
* @param credentials the SignServer credentials | ||
*/ | ||
public SignServerSigningService(String endpoint, SignServerCredentials credentials) { | ||
this.client = new RESTClient( | ||
requireNonNull(endpoint, "You need to provide the SignServer endpoint URL as keystore parameter") | ||
+ (endpoint.endsWith("/") ? "" : "/")) | ||
.authentication(credentials::addAuthentication) | ||
.errorHandler(response -> response.get("error").toString()); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "SignServer"; | ||
} | ||
|
||
@Override | ||
public List<String> aliases() { | ||
return emptyList(); | ||
} | ||
|
||
@Override | ||
public Certificate[] getCertificateChain(String alias) throws KeyStoreException { | ||
if (!certificates.containsKey(alias)) { | ||
try { | ||
Map<String, ?> response = client.post(getResourcePath(alias), "{\"data\":\"\"}"); | ||
String encodedCertificate = response.get("signerCertificate").toString(); | ||
byte[] certificateBytes = Base64.getDecoder().decode(encodedCertificate); | ||
Certificate certificate = CertificateFactory | ||
.getInstance("X.509") | ||
.generateCertificate(new ByteArrayInputStream(certificateBytes)); | ||
certificates.put(alias, new Certificate[]{certificate}); | ||
} catch (IOException | CertificateException e) { | ||
throw new KeyStoreException(e); | ||
} | ||
} | ||
|
||
return certificates.get(alias); | ||
} | ||
|
||
@Override | ||
public SigningServicePrivateKey getPrivateKey(String alias, char[] password) throws UnrecoverableKeyException { | ||
try { | ||
String algorithm = getCertificateChain(alias)[0].getPublicKey().getAlgorithm(); | ||
return new SigningServicePrivateKey(alias, algorithm, this); | ||
} catch (KeyStoreException e) { | ||
throw (UnrecoverableKeyException) new UnrecoverableKeyException().initCause(e); | ||
} | ||
} | ||
|
||
@Override | ||
public byte[] sign(SigningServicePrivateKey privateKey, String algorithm, byte[] data) throws GeneralSecurityException { | ||
DigestAlgorithm digestAlgorithm = DigestAlgorithm.of(algorithm.substring(0, algorithm.toLowerCase().indexOf("with"))); | ||
data = digestAlgorithm.getMessageDigest().digest(data); | ||
|
||
Map<String, Object> request = new HashMap<>(); | ||
request.put("data", Base64.getEncoder().encodeToString(data)); | ||
request.put("encoding", "BASE64"); | ||
Map<String, Object> metaData = new HashMap<>(); | ||
metaData.put("USING_CLIENTSUPPLIED_HASH", true); | ||
metaData.put("CLIENTSIDE_HASHDIGESTALGORITHM", digestAlgorithm.id); | ||
request.put("metaData", metaData); | ||
|
||
try { | ||
Map<String, ?> response = client.post(getResourcePath(privateKey.getId()), JsonWriter.format(request)); | ||
String value = response.get("data").toString(); | ||
return Base64.getDecoder().decode(value); | ||
} catch (IOException e) { | ||
throw new GeneralSecurityException(e); | ||
} | ||
} | ||
|
||
private String getResourcePath(String alias) { | ||
return "rest/v1/workers/" + alias + "/process"; | ||
} | ||
} |