Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## 3.0

### NEXT


### 3.16.1 (Supreme 0.8.1) Hotfix
* Generalized, proper COSE to MAC mapping, preventing unexpected behaviour for `HS265_24`

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ kotlin.code.style=official
kotlin.js.compiler=ir
org.gradle.jvmargs=-Xmx4g -Dfile.encoding=UTF-8

artifactVersion=3.16.1
supremeVersion=0.8.1
artifactVersion=3.17.0-SNAPSHOT
supremeVersion=0.9.0-SNAPSHOT

# This is not a well-defined property, the ASP convention plugin respects it, though
jdk.version=17
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,31 @@ abstract class PemDecodable<A : Asn1Element, out T : PemEncodable<A>>
constructor(vararg ebStrings: String) : this(ebStrings.associateWith { PemDecoder.Default })
constructor(vararg decoders: Pair<String, ((ByteArray)->T)?>) : this(decoders.associate { it.first to PemDecoder(it.second) })

companion object {
private val KNOWN_DECODERS = mutableMapOf<String, MutableSet<String>>()
}

init {
val myName = this::class.simpleName
if (myName != null) {
decoders.keys.forEach { ebString ->
KNOWN_DECODERS.getOrPut(ebString) { mutableSetOf() }.add(myName)
}
}
}

class UnknownEncapsulationBoundaryException(val ebString: String)
: IllegalArgumentException(
"Unknown encapsulation boundary string $ebString" +
KNOWN_DECODERS[ebString].let { decoderClasses ->
when {
(decoderClasses == null) -> ""
(decoderClasses.size == 1) -> "\nDid you mean to use ${decoderClasses.first()}.decodeFromPem(...)?"
else -> "\nDid you mean to use one of the following?\n${
decoderClasses.joinToString("\n") { "- $it.decodeFromPem(...)" }}"
}
})

/** Decodes a PEM-encoded string into [T] */
fun decodeFromPem(src: String): KmmResult<T> = catching {
src.lineSequence()
Expand All @@ -56,7 +81,7 @@ abstract class PemDecodable<A : Asn1Element, out T : PemEncodable<A>>
val firstLine = it.next()
val ebString = firstLine.substring(FENCE_PREFIX_BEGIN.length, firstLine.length - FENCE_SUFFIX.length)
val decoder: PemDecoder<T> = decoders.getOrElse(ebString)
{ throw IllegalArgumentException("Unknown encapsulation boundary string $ebString") }
{ throw UnknownEncapsulationBoundaryException(ebString) }
val b64data = StringBuilder()
while (it.hasNext()) {
val line = it.next()
Expand Down
13 changes: 13 additions & 0 deletions indispensable/src/commonTest/kotlin/Asn1AddonsTest.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import at.asitplus.signum.indispensable.asn1.*
import at.asitplus.signum.indispensable.asn1.encoding.parse
import at.asitplus.signum.indispensable.pki.X509Certificate
import com.ionspin.kotlin.bignum.integer.BigInteger
import com.ionspin.kotlin.bignum.integer.Sign
import io.kotest.common.Platform
import io.kotest.common.platform
import io.kotest.core.spec.style.FreeSpec
import io.kotest.datatest.withData
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.types.shouldBeTypeOf
import io.kotest.property.Arb
import io.kotest.property.arbitrary.*
Expand Down Expand Up @@ -86,4 +88,15 @@ class Asn1AddonsTest: FreeSpec({
}
}
}

"PemDecodable error messages" {
val x = X509Certificate.decodeFromPem("-----BEGIN PUBLIC KEY-----\n" +
"MFswDQYJKoZIhvcNAQEBBQADSgAwRwJAXWRPQyGlEY+SXz8Uslhe+MLjTgWd8lf/\n" +
"nA0hgCm9JFKC1tq1S73cQ9naClNXsMqY7pwPt1bSY8jYRqHHbdoUvwIDAQAB\n" +
"-----END PUBLIC KEY-----")
.exceptionOrNull()
.shouldBeTypeOf<PemDecodable.UnknownEncapsulationBoundaryException>()
x.ebString shouldBe "PUBLIC KEY"
x.message shouldContain "CryptoPublicKey.decodeFromPem"
}
})
Loading