Skip to content

Commit 583378e

Browse files
committed
refactor: throw GoogleAuthException for signing and transcoding errors
1 parent bd7af8d commit 583378e

2 files changed

Lines changed: 26 additions & 18 deletions

File tree

oauth2_http/java/com/google/auth/oauth2/GdchCredentials.java

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,8 @@ String createAssertion(JsonFactory jsonFactory, long currentTime) throws IOExcep
341341
try {
342342
assertion = signUsingEsSha256(privateKey, jsonFactory, header, payload);
343343
} catch (GeneralSecurityException e) {
344-
throw new IOException(
345-
"Error signing service account access token request with private key.", e);
344+
throw new GoogleAuthException(
345+
false, 0, "Error signing service account access token request with private key.", e);
346346
}
347347

348348
return assertion;
@@ -689,7 +689,7 @@ static String signUsingEsSha256(
689689
JsonFactory jsonFactory,
690690
JsonWebSignature.Header header,
691691
JsonWebToken.Payload payload)
692-
throws GeneralSecurityException, IOException {
692+
throws GeneralSecurityException, GoogleAuthException {
693693

694694
// 1. Construct the JWS Signing Input: Base64URL(UTF8(Header)) + '.' + Base64URL(UTF8(Payload))
695695
String content =
@@ -725,10 +725,11 @@ static String signUsingEsSha256(
725725
* @throws IOException If the DER format is invalid.
726726
*/
727727
@VisibleForTesting
728-
static byte[] transcodeDerToConcat(byte[] derSignature, int outputLength) throws IOException {
728+
static byte[] transcodeDerToConcat(byte[] derSignature, int outputLength)
729+
throws GoogleAuthException {
729730
// Validate basic ASN.1 DER structure (0x30 = SEQUENCE)
730731
if (derSignature.length < 8 || derSignature[0] != 0x30) {
731-
throw new IOException("Invalid DER signature format.");
732+
throw new GoogleAuthException(false, 0, "Invalid DER signature format.", null);
732733
}
733734

734735
int offset = 2;
@@ -740,12 +741,12 @@ static byte[] transcodeDerToConcat(byte[] derSignature, int outputLength) throws
740741
}
741742

742743
if (derSignature.length - offset != seqLength) {
743-
throw new IOException("Invalid DER signature length.");
744+
throw new GoogleAuthException(false, 0, "Invalid DER signature length.", null);
744745
}
745746

746747
// Parse Integer R (0x02 = INTEGER)
747748
if (derSignature[offset++] != 0x02) {
748-
throw new IOException("Expected INTEGER for R.");
749+
throw new GoogleAuthException(false, 0, "Expected INTEGER for R.", null);
749750
}
750751
int rLength = derSignature[offset++];
751752
// Skip leading zero byte if it exists (DER integers are signed; zero is added to stay positive)
@@ -759,7 +760,7 @@ static byte[] transcodeDerToConcat(byte[] derSignature, int outputLength) throws
759760

760761
// Parse Integer S
761762
if (derSignature[offset++] != 0x02) {
762-
throw new IOException("Expected INTEGER for S.");
763+
throw new GoogleAuthException(false, 0, "Expected INTEGER for S.", null);
763764
}
764765
int sLength = derSignature[offset++];
765766
if (derSignature[offset] == 0x00 && sLength > 1 && (derSignature[offset + 1] & 0x80) != 0) {
@@ -772,10 +773,13 @@ static byte[] transcodeDerToConcat(byte[] derSignature, int outputLength) throws
772773
// Concatenate r and s into fixed-length segments (32 bytes each for ES256)
773774
int keySizeBytes = outputLength / 2;
774775
if (r.length > keySizeBytes || s.length > keySizeBytes) {
775-
throw new IOException(
776+
throw new GoogleAuthException(
777+
false,
778+
0,
776779
String.format(
777780
"Invalid R or S length. R: %d, S: %d, Expected: %d",
778-
r.length, s.length, keySizeBytes));
781+
r.length, s.length, keySizeBytes),
782+
null);
779783
}
780784

781785
byte[] result = new byte[outputLength];

oauth2_http/javatests/com/google/auth/oauth2/GdchCredentialsTest.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,35 +1128,39 @@ void transcodeDerToConcat_withGeneratedSignature() throws Exception {
11281128
@Test
11291129
void transcodeDerToConcat_invalidDerFormat() {
11301130
byte[] invalidDer = new byte[] {0x31, 0x00}; // Not a SEQUENCE
1131-
IOException e =
1132-
assertThrows(IOException.class, () -> GdchCredentials.transcodeDerToConcat(invalidDer, 64));
1131+
GoogleAuthException e =
1132+
assertThrows(
1133+
GoogleAuthException.class, () -> GdchCredentials.transcodeDerToConcat(invalidDer, 64));
11331134
assertEquals("Invalid DER signature format.", e.getMessage());
11341135
}
11351136

11361137
@Test
11371138
void transcodeDerToConcat_invalidLength() {
11381139
// SEQUENCE length doesn't match actual length
11391140
byte[] invalidDer = new byte[] {0x30, 0x05, 0x02, 0x01, 0x01, 0x02, 0x01, 0x02};
1140-
IOException e =
1141-
assertThrows(IOException.class, () -> GdchCredentials.transcodeDerToConcat(invalidDer, 64));
1141+
GoogleAuthException e =
1142+
assertThrows(
1143+
GoogleAuthException.class, () -> GdchCredentials.transcodeDerToConcat(invalidDer, 64));
11421144
assertEquals("Invalid DER signature length.", e.getMessage());
11431145
}
11441146

11451147
@Test
11461148
void transcodeDerToConcat_invalidRInteger() {
11471149
// Missing INTEGER for R
11481150
byte[] invalidDer = new byte[] {0x30, 0x06, 0x03, 0x01, 0x01, 0x02, 0x01, 0x02};
1149-
IOException e =
1150-
assertThrows(IOException.class, () -> GdchCredentials.transcodeDerToConcat(invalidDer, 64));
1151+
GoogleAuthException e =
1152+
assertThrows(
1153+
GoogleAuthException.class, () -> GdchCredentials.transcodeDerToConcat(invalidDer, 64));
11511154
assertEquals("Expected INTEGER for R.", e.getMessage());
11521155
}
11531156

11541157
@Test
11551158
void transcodeDerToConcat_invalidSInteger() {
11561159
// Missing INTEGER for S
11571160
byte[] invalidDer = new byte[] {0x30, 0x06, 0x02, 0x01, 0x01, 0x03, 0x01, 0x01};
1158-
IOException e =
1159-
assertThrows(IOException.class, () -> GdchCredentials.transcodeDerToConcat(invalidDer, 64));
1161+
GoogleAuthException e =
1162+
assertThrows(
1163+
GoogleAuthException.class, () -> GdchCredentials.transcodeDerToConcat(invalidDer, 64));
11601164
assertEquals("Expected INTEGER for S.", e.getMessage());
11611165
}
11621166

0 commit comments

Comments
 (0)