Skip to content

Commit a4104d5

Browse files
authored
Merge pull request #626 from sigstore/fix-keys-fuzzer
Fix keys fuzzer
2 parents 31dc318 + b3f1eb3 commit a4104d5

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

fuzzing/src/main/java/fuzzing/KeysFuzzer.java renamed to fuzzing/src/main/java/fuzzing/KeysParsingFuzzer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@
2121
import java.security.NoSuchAlgorithmException;
2222
import java.security.spec.InvalidKeySpecException;
2323

24-
public class KeysFuzzer {
24+
public class KeysParsingFuzzer {
2525
public static void fuzzerTestOneInput(FuzzedDataProvider data) {
2626
try {
27-
String[] schemes = {"rsassa-pss-sha256", "ed25519", "ecdsa-sha2-nistp256"};
28-
String scheme = data.pickValue(schemes);
2927
byte[] byteArray = data.consumeRemainingAsBytes();
3028

3129
Keys.parsePublicKey(byteArray);
32-
Keys.constructTufPublicKey(byteArray, scheme);
3330
} catch (IOException | InvalidKeySpecException | NoSuchAlgorithmException e) {
3431
// known exceptions
3532
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2023 The Sigstore Authors.
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+
package fuzzing;
17+
18+
import com.code_intelligence.jazzer.api.FuzzedDataProvider;
19+
import dev.sigstore.encryption.Keys;
20+
import java.security.NoSuchAlgorithmException;
21+
import java.security.spec.InvalidKeySpecException;
22+
23+
public class TufKeysFuzzer {
24+
public static void fuzzerTestOneInput(FuzzedDataProvider data) {
25+
try {
26+
String[] schemes = {"rsassa-pss-sha256", "ed25519", "ecdsa-sha2-nistp256", "ecdsa"};
27+
String scheme = data.pickValue(schemes);
28+
byte[] byteArray = data.consumeRemainingAsBytes();
29+
30+
Keys.constructTufPublicKey(byteArray, scheme);
31+
} catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
32+
// known exceptions
33+
} catch (RuntimeException e) {
34+
if (!e.toString().contains("not currently supported")) {
35+
throw e;
36+
}
37+
}
38+
}
39+
}

sigstore-java/src/main/java/dev/sigstore/encryption/Keys.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ public static PublicKey parsePkcs1RsaPublicKey(byte[] contents)
135135
*/
136136
public static PublicKey constructTufPublicKey(byte[] contents, String scheme)
137137
throws NoSuchAlgorithmException, InvalidKeySpecException {
138+
if (contents == null || contents.length == 0) {
139+
throw new InvalidKeySpecException("key contents was empty");
140+
}
138141
switch (scheme) {
139142
case "ed25519":
140143
{
@@ -172,11 +175,15 @@ public static PublicKey constructTufPublicKey(byte[] contents, String scheme)
172175

173176
// code below just creates the public key from key contents using the curve parameters
174177
// (spec variable)
175-
ECNamedCurveSpec params =
176-
new ECNamedCurveSpec("P-256", spec.getCurve(), spec.getG(), spec.getN());
177-
ECPoint point = decodePoint(params.getCurve(), contents);
178-
ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params);
179-
return kf.generatePublic(pubKeySpec);
178+
try {
179+
ECNamedCurveSpec params =
180+
new ECNamedCurveSpec("P-256", spec.getCurve(), spec.getG(), spec.getN());
181+
ECPoint point = decodePoint(params.getCurve(), contents);
182+
ECPublicKeySpec pubKeySpec = new ECPublicKeySpec(point, params);
183+
return kf.generatePublic(pubKeySpec);
184+
} catch (IllegalArgumentException | NullPointerException ex) {
185+
throw new InvalidKeySpecException("ecdsa key was not parseable", ex);
186+
}
180187
}
181188
default:
182189
throw new RuntimeException(scheme + " not currently supported");

sigstore-java/src/test/java/dev/sigstore/encryption/KeysTest.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ void parseTufPublicKeyPemEncoded_sha2_nistp256()
107107
}
108108

109109
@Test
110-
void parseTufPublicKey_ecdsa()
111-
throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
110+
void parseTufPublicKey_ecdsa() throws NoSuchAlgorithmException, InvalidKeySpecException {
112111
PublicKey key =
113112
Keys.constructTufPublicKey(
114113
Hex.decode(
@@ -119,10 +118,9 @@ void parseTufPublicKey_ecdsa()
119118
}
120119

121120
@Test
122-
void parseTufPublicKey_ecdsaBad()
123-
throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchProviderException {
121+
void parseTufPublicKey_ecdsaBad() {
124122
Assertions.assertThrows(
125-
RuntimeException.class,
123+
InvalidKeySpecException.class,
126124
() -> {
127125
Keys.constructTufPublicKey(
128126
Hex.decode(

0 commit comments

Comments
 (0)