Skip to content

Commit cd45eb4

Browse files
carmenyhcopybara-github
authored andcommitted
Wrap CharacterCodingException.
PiperOrigin-RevId: 902573134
1 parent a1bf5ab commit cd45eb4

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

src/main/kotlin/Extension.kt

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,14 @@ import org.bouncycastle.asn1.DERSet
5858
import org.bouncycastle.asn1.DERTaggedObject
5959
import org.bouncycastle.asn1.x509.Extension
6060

61-
@Immutable
6261
@RequiresApi(24)
63-
data class ExtensionParsingException(val msg: String, val reason: KeyAttestationReason? = null) :
64-
Exception(msg)
62+
data class ExtensionParsingException
63+
@JvmOverloads
64+
constructor(
65+
override val message: String,
66+
val reason: KeyAttestationReason? = null,
67+
override val cause: Throwable? = null,
68+
) : Exception(message, cause)
6569

6670
@Immutable
6771
data class ProvisioningInfoMap(
@@ -444,13 +448,12 @@ data class AuthorizationList(
444448
}
445449

446450
fun from(seq: ASN1Sequence, logFn: (String) -> Unit = { _ -> }): AuthorizationList {
447-
val objects =
448-
seq.associate {
449-
require(it is ASN1TaggedObject) {
450-
"Must be an ASN1TaggedObject, was ${it::class.simpleName}"
451-
}
452-
KeyMintTag.from(it.tagNo) to it.explicitBaseObject
451+
val objects = seq.associate {
452+
require(it is ASN1TaggedObject) {
453+
"Must be an ASN1TaggedObject, was ${it::class.simpleName}"
453454
}
455+
KeyMintTag.from(it.tagNo) to it.explicitBaseObject
456+
}
454457

455458
/**
456459
* X.680 section 8.6
@@ -775,11 +778,15 @@ private inline fun <reified T> ASN1Encodable.toSet(): Set<T> {
775778

776779
@RequiresApi(24)
777780
private fun ASN1Encodable.toStr() =
778-
UTF_8.newDecoder()
779-
.onMalformedInput(CodingErrorAction.REPORT)
780-
.onUnmappableCharacter(CodingErrorAction.REPORT)
781-
.decode(this.toByteBuffer())
782-
.toString()
781+
try {
782+
UTF_8.newDecoder()
783+
.onMalformedInput(CodingErrorAction.REPORT)
784+
.onUnmappableCharacter(CodingErrorAction.REPORT)
785+
.decode(this.toByteBuffer())
786+
.toString()
787+
} catch (e: CharacterCodingException) {
788+
throw ExtensionParsingException("error decoding ASN.1: ${e.message}", cause = e)
789+
}
783790

784791
private fun ASN1Encodable.toTaggedObject(tag: KeyMintTag) = DERTaggedObject(tag.value, this)
785792

0 commit comments

Comments
 (0)