Skip to content

Commit 4b8c3e3

Browse files
committed
Implement new SimpleSigstoreSigner and update exceptions in Signer
1 parent c3325e4 commit 4b8c3e3

File tree

3 files changed

+72
-1
lines changed

3 files changed

+72
-1
lines changed

pom.xml

+7
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@
119119
<artifactId>jakarta.el</artifactId>
120120
<version>4.0.2</version>
121121
</dependency>
122+
<!-- sigstore-java -->
123+
<dependency>
124+
<groupId>dev.sigstore</groupId>
125+
<artifactId>sigstore-java</artifactId>
126+
<version>0.10.0</version>
127+
</dependency>
128+
122129

123130
</dependencies>
124131
<properties>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package io.github.intoto.dsse.helpers;
2+
3+
import dev.sigstore.KeylessSigner;
4+
import dev.sigstore.KeylessSignerException;
5+
import dev.sigstore.bundle.Bundle;
6+
import io.github.intoto.dsse.models.Signer;
7+
8+
import java.io.IOException;
9+
import java.nio.charset.StandardCharsets;
10+
import java.security.*;
11+
import java.security.cert.CertificateException;
12+
import java.security.cert.X509Certificate;
13+
import java.security.spec.InvalidKeySpecException;
14+
import java.util.Optional;
15+
16+
public class SimpleSigstoreSigner implements Signer {
17+
private String keyId;
18+
Optional<Bundle.DSSESignature> dsseSignature;
19+
Bundle result;
20+
21+
public byte[] sign(byte[] payload) throws InvalidAlgorithmParameterException, CertificateException, IOException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, KeylessSignerException {
22+
KeylessSigner functionary = new KeylessSigner.Builder().sigstorePublicDefaults().build();
23+
this.result = functionary.sign(payload);
24+
25+
// set keyId
26+
X509Certificate certificate = (X509Certificate) (this.result.getCertPath().getCertificates().getFirst());
27+
String oid = "1.3.6.1.4.1.57264.1.8";
28+
byte[] extensionValue = certificate.getExtensionValue(oid);
29+
String issuer = new String(extensionValue, StandardCharsets.UTF_8);
30+
this.keyId = issuer.substring(4);
31+
Object subAltArr = certificate.getSubjectAlternativeNames().toArray()[0];
32+
String subAltName = subAltArr.toString();
33+
subAltName = subAltName.substring(4, subAltName.length() - 1);
34+
this.keyId = keyId.concat(" " + subAltName);
35+
36+
this.dsseSignature = result.getDSSESignature();
37+
return dsseSignature.get().getSignature();
38+
}
39+
40+
@Override
41+
public String getKeyId() {
42+
if (this.keyId.isEmpty()) {
43+
throw new RuntimeException("Sign the artifact to initialize keyId");
44+
}
45+
return this.keyId;
46+
}
47+
48+
public byte[] getPayload() {
49+
if (this.dsseSignature.isEmpty()) {
50+
throw new RuntimeException("Cannot retrieve and unsigned payload");
51+
}
52+
return this.dsseSignature.get().getPayload().getBytes(StandardCharsets.UTF_8);
53+
}
54+
55+
public Bundle getResult() {
56+
return this.result;
57+
}
58+
}

src/main/java/io/github/intoto/dsse/models/Signer.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package io.github.intoto.dsse.models;
22

3+
import dev.sigstore.KeylessSignerException;
4+
5+
import java.io.IOException;
6+
import java.security.InvalidAlgorithmParameterException;
37
import java.security.InvalidKeyException;
48
import java.security.NoSuchAlgorithmException;
59
import java.security.SignatureException;
10+
import java.security.cert.CertificateException;
11+
import java.security.spec.InvalidKeySpecException;
612

713
/** Interface for a DSSE Signer. */
814
public interface Signer {
@@ -13,7 +19,7 @@ public interface Signer {
1319
* @param payload the message that you want to sign.
1420
*/
1521
byte[] sign(byte[] payload)
16-
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException;
22+
throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, InvalidAlgorithmParameterException, CertificateException, IOException, InvalidKeySpecException, KeylessSignerException;
1723

1824
/** Returns the ID of this key, or null if not supported. */
1925
String getKeyId();

0 commit comments

Comments
 (0)