4747 */
4848public final class DsseUtils {
4949
50- /**
51- * Not instantiable.
52- */
53- private DsseUtils () {
54- }
55-
5650 /**
5751 * Creates and prepares a {@link GpgSigner} from the given configuration.
5852 *
@@ -79,6 +73,56 @@ public static AbstractGpgSigner createGpgSigner(final String executable, final b
7973 return signer ;
8074 }
8175
76+ /**
77+ * Extracts the key identifier from a binary OpenPGP Signature Packet.
78+ *
79+ * @param sigBytes raw binary OpenPGP Signature Packet bytes
80+ * @return uppercase hex-encoded fingerprint or key ID string
81+ * @throws MojoExecutionException if {@code sigBytes} cannot be parsed as an OpenPGP signature
82+ */
83+ public static String getKeyId (final byte [] sigBytes ) throws MojoExecutionException {
84+ try {
85+ final PGPSignatureList sigList = (PGPSignatureList ) new BcPGPObjectFactory (sigBytes ).nextObject ();
86+ final PGPSignature sig = sigList .get (0 );
87+ final PGPSignatureSubpacketVector hashed = sig .getHashedSubPackets ();
88+ if (hashed != null ) {
89+ final IssuerFingerprint fp = hashed .getIssuerFingerprint ();
90+ if (fp != null ) {
91+ return Hex .encodeHexString (fp .getFingerprint ());
92+ }
93+ }
94+ return Long .toHexString (sig .getKeyID ()).toUpperCase (Locale .ROOT );
95+ } catch (final IOException e ) {
96+ throw new MojoExecutionException ("Failed to extract key ID from signature" , e );
97+ }
98+ }
99+
100+ /**
101+ * Signs {@code paeFile} and returns the raw OpenPGP signature bytes.
102+ *
103+ * <p>The signer must already have {@link AbstractGpgSigner#prepare()} called before this method is invoked.</p>
104+ *
105+ * @param signer the configured, prepared signer
106+ * @param path path to the file to sign
107+ * @return raw binary PGP signature bytes
108+ * @throws MojoExecutionException if signing or signature decoding fails
109+ */
110+ public static byte [] signFile (final AbstractGpgSigner signer , final Path path ) throws MojoExecutionException {
111+ final Path signaturePath = signer .generateSignatureForArtifact (path .toFile ()).toPath ();
112+ final byte [] signatureBytes ;
113+ try (InputStream in = Files .newInputStream (signaturePath ); ArmoredInputStream armoredIn = new ArmoredInputStream (in )) {
114+ signatureBytes = IOUtils .toByteArray (armoredIn );
115+ } catch (final IOException e ) {
116+ throw new MojoExecutionException ("Failed to read signature file: " + signaturePath , e );
117+ }
118+ try {
119+ Files .delete (signaturePath );
120+ } catch (final IOException e ) {
121+ throw new MojoExecutionException ("Failed to delete signature file: " + signaturePath , e );
122+ }
123+ return signatureBytes ;
124+ }
125+
82126 /**
83127 * Serializes {@code statement} to JSON using the DSSE Pre-Authentication Encoding (PAE).
84128 *
@@ -127,52 +171,8 @@ public static Path writePaeFile(final byte[] statementBytes, final Path buildDir
127171 }
128172
129173 /**
130- * Signs {@code paeFile} and returns the raw OpenPGP signature bytes.
131- *
132- * <p>The signer must already have {@link AbstractGpgSigner#prepare()} called before this method is invoked.</p>
133- *
134- * @param signer the configured, prepared signer
135- * @param path path to the file to sign
136- * @return raw binary PGP signature bytes
137- * @throws MojoExecutionException if signing or signature decoding fails
138- */
139- public static byte [] signFile (final AbstractGpgSigner signer , final Path path ) throws MojoExecutionException {
140- final Path signaturePath = signer .generateSignatureForArtifact (path .toFile ()).toPath ();
141- final byte [] signatureBytes ;
142- try (InputStream in = Files .newInputStream (signaturePath ); ArmoredInputStream armoredIn = new ArmoredInputStream (in )) {
143- signatureBytes = IOUtils .toByteArray (armoredIn );
144- } catch (final IOException e ) {
145- throw new MojoExecutionException ("Failed to read signature file: " + signaturePath , e );
146- }
147- try {
148- Files .delete (signaturePath );
149- } catch (final IOException e ) {
150- throw new MojoExecutionException ("Failed to delete signature file: " + signaturePath , e );
151- }
152- return signatureBytes ;
153- }
154-
155- /**
156- * Extracts the key identifier from a binary OpenPGP Signature Packet.
157- *
158- * @param sigBytes raw binary OpenPGP Signature Packet bytes
159- * @return uppercase hex-encoded fingerprint or key ID string
160- * @throws MojoExecutionException if {@code sigBytes} cannot be parsed as an OpenPGP signature
174+ * Not instantiable.
161175 */
162- public static String getKeyId (final byte [] sigBytes ) throws MojoExecutionException {
163- try {
164- final PGPSignatureList sigList = (PGPSignatureList ) new BcPGPObjectFactory (sigBytes ).nextObject ();
165- final PGPSignature sig = sigList .get (0 );
166- final PGPSignatureSubpacketVector hashed = sig .getHashedSubPackets ();
167- if (hashed != null ) {
168- final IssuerFingerprint fp = hashed .getIssuerFingerprint ();
169- if (fp != null ) {
170- return Hex .encodeHexString (fp .getFingerprint ());
171- }
172- }
173- return Long .toHexString (sig .getKeyID ()).toUpperCase (Locale .ROOT );
174- } catch (final IOException e ) {
175- throw new MojoExecutionException ("Failed to extract key ID from signature" , e );
176- }
176+ private DsseUtils () {
177177 }
178178}
0 commit comments