Skip to content

Commit 48e8c7f

Browse files
committed
use CertificateUtils.
1 parent 927d215 commit 48e8c7f

2 files changed

Lines changed: 12 additions & 13 deletions

File tree

s2a/src/test/java/io/grpc/s2a/internal/handshaker/FakeS2AServer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public final class FakeS2AServer extends S2AServiceGrpc.S2AServiceImplBase {
2828

2929
private final FakeWriter writer;
3030

31-
public FakeS2AServer() throws InvalidKeySpecException, NoSuchAlgorithmException {
31+
public FakeS2AServer() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException {
3232
this.writer = new FakeWriter();
3333
this.writer.setVerificationResult(FakeWriter.VerificationResult.SUCCESS).initializePrivateKey();
3434
}

s2a/src/test/java/io/grpc/s2a/internal/handshaker/FakeWriter.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,18 @@
2323
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2424
import com.google.protobuf.ByteString;
2525
import io.grpc.stub.StreamObserver;
26+
import io.grpc.util.CertificateUtils;
2627
import java.io.File;
28+
import java.io.FileInputStream;
29+
import java.io.FileNotFoundException;
2730
import java.io.IOException;
31+
import java.io.UnsupportedEncodingException;
2832
import java.nio.charset.StandardCharsets;
2933
import java.nio.file.Files;
30-
import java.security.KeyFactory;
3134
import java.security.NoSuchAlgorithmException;
3235
import java.security.PrivateKey;
3336
import java.security.Signature;
3437
import java.security.spec.InvalidKeySpecException;
35-
import java.security.spec.PKCS8EncodedKeySpec;
36-
import java.util.Base64;
3738

3839
/** A fake Writer Class to mock the behavior of S2A server. */
3940
final class FakeWriter implements StreamObserver<SessionReq> {
@@ -59,12 +60,8 @@ enum VerificationResult {
5960
new File("src/test/resources/int_cert2_ec.pem");
6061
public static final File cert1File =
6162
new File("src/test/resources/int_cert1_ec.pem");
62-
63-
// src/test/resources/leaf_key_ec.pem
64-
private static final String PRIVATE_KEY =
65-
"MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgR2HBqtWTWu4NLiow"
66-
+ "ar8vh+9vAmCONE59C+jXNAb9r8ehRANCAATRM8ozcr8PTOVsZNWh+rTmJ6t+rODu"
67-
+ "g3LwWpUQq9h7AddjGlLrrTNrceOyO7nh9aEk5plKhs/h7PO8+vkEFsEx";
63+
public static final File keyFile =
64+
new File("src/test/resources/leaf_key_ec.pem");
6865
private static final ImmutableMap<SignatureAlgorithm, String>
6966
ALGORITHM_TO_SIGNATURE_INSTANCE_IDENTIFIER =
7067
ImmutableMap.of(
@@ -107,10 +104,12 @@ FakeWriter setFailureReason(String failureReason) {
107104
}
108105

109106
@CanIgnoreReturnValue
110-
FakeWriter initializePrivateKey() throws InvalidKeySpecException, NoSuchAlgorithmException {
107+
FakeWriter initializePrivateKey() throws InvalidKeySpecException, NoSuchAlgorithmException,
108+
IOException, FileNotFoundException, UnsupportedEncodingException {
109+
FileInputStream keyInputStream =
110+
new FileInputStream(keyFile);
111111
privateKey =
112-
KeyFactory.getInstance("EC")
113-
.generatePrivate(new PKCS8EncodedKeySpec(Base64.getDecoder().decode(PRIVATE_KEY)));
112+
CertificateUtils.getPrivateKey(keyInputStream);
114113
return this;
115114
}
116115

0 commit comments

Comments
 (0)