Skip to content

Commit de48bd5

Browse files
committed
fix xy coord parsing
1 parent 58ce717 commit de48bd5

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

aws-runtime/aws-config/common/src/aws/sdk/kotlin/runtime/auth/credentials/LoginTokenProvider.kt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ internal data class ECKeyData(
233233

234234
/**
235235
* Parses a PEM-encoded EC private key and extracts the private key scalar and public key (x, y) coordinates.
236-
* Supports both "EC PRIVATE KEY" and "PRIVATE KEY" PEM formats for P-256 curve keys.
236+
* Supports "EC PRIVATE KEY" PEM formats for P-256 curve keys.
237237
*/
238238
private fun parseECKeyPem(pem: String): ECKeyData {
239239
val base64 = pem.replace("-----BEGIN EC PRIVATE KEY-----", "")
@@ -259,15 +259,15 @@ private fun parseECKeyPem(pem: String): ECKeyData {
259259
val remainingBytes = der.size - publicKeyStart
260260
val coordLen = remainingBytes / 2
261261

262-
val x = der.copyOfRange(publicKeyStart, publicKeyStart + coordLen).padOrTrimTo32()
263-
val y = der.copyOfRange(publicKeyStart + coordLen, publicKeyStart + 2 * coordLen).padOrTrimTo32()
262+
val x = der.copyOfRange(publicKeyStart, publicKeyStart + coordLen).padTo32()
263+
val y = der.copyOfRange(publicKeyStart + coordLen, publicKeyStart + 2 * coordLen).padTo32()
264264

265265
return ECKeyData(d, x, y)
266266
}
267267

268-
private fun ByteArray.padOrTrimTo32(): ByteArray =
269-
if (size >= 32) {
270-
takeLast(32).toByteArray()
268+
private fun ByteArray.padTo32(): ByteArray =
269+
if (size > 32) {
270+
error("Unexpected byte array of size $size; expected 32 bytes or less")
271271
} else {
272272
ByteArray(32 - size) + this
273273
}

0 commit comments

Comments
 (0)