Skip to content

Commit e66a8df

Browse files
committed
asn1: Edwards/Montgomery OIDs; RFC 8410-tolerant AlgorithmIdentifier; minor DER decoder tolerance
1 parent 1d0f98f commit e66a8df

File tree

4 files changed

+36
-7
lines changed

4 files changed

+36
-7
lines changed

cryptography-serialization/asn1/modules/src/commonMain/kotlin/AlgorithmIdentifierSerializer.kt

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,18 @@ public abstract class AlgorithmIdentifierSerializer<AI : AlgorithmIdentifier> :
4545
index = 0,
4646
deserializer = ObjectIdentifier.serializer()
4747
)
48-
check(decodeElementIndex(descriptor) == 1)
49-
val parameters = decodeParameters(algorithm)
50-
check(decodeElementIndex(descriptor) == CompositeDecoder.DECODE_DONE)
51-
parameters
48+
when (val idx = decodeElementIndex(descriptor)) {
49+
1 -> {
50+
val parameters = decodeParameters(algorithm)
51+
check(decodeElementIndex(descriptor) == CompositeDecoder.DECODE_DONE)
52+
parameters
53+
}
54+
CompositeDecoder.DECODE_DONE -> {
55+
// Some algorithms (e.g., Ed25519/Ed448/X25519/X448 per RFC 8410) omit parameters.
56+
// Delegate to subclass to construct an identifier without consuming parameters from the stream.
57+
decodeParameters(algorithm)
58+
}
59+
else -> error("Unexpected element index: $idx")
60+
}
5261
}
53-
}
62+
}

cryptography-serialization/asn1/modules/src/commonMain/kotlin/KeyAlgorithmIdentifierSerializer.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ internal object KeyAlgorithmIdentifierSerializer : AlgorithmIdentifierSerializer
2626
}
2727
ObjectIdentifier.EC -> EcKeyAlgorithmIdentifier(decodeParameters(EcParameters.serializer()))
2828
else -> {
29-
// TODO: somehow we should ignore parameters here
29+
// For algorithms like Ed25519/Ed448/X25519/X448 (RFC 8410), parameters are absent.
30+
// Do not attempt to read parameters when the element is omitted; just construct the identifier.
3031
UnknownKeyAlgorithmIdentifier(algorithm)
3132
}
3233
}
33-
}
34+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2025 Oleg Yukhnevich. Use of this source code is governed by the Apache 2.0 license.
3+
*/
4+
5+
package dev.whyoleg.cryptography.serialization.asn1.modules
6+
7+
import dev.whyoleg.cryptography.serialization.asn1.ObjectIdentifier
8+
9+
public object EdwardsOids {
10+
public val Ed25519: ObjectIdentifier = ObjectIdentifier("1.3.101.112")
11+
public val Ed448: ObjectIdentifier = ObjectIdentifier("1.3.101.113")
12+
}
13+
14+
public object MontgomeryOids {
15+
public val X25519: ObjectIdentifier = ObjectIdentifier("1.3.101.110")
16+
public val X448: ObjectIdentifier = ObjectIdentifier("1.3.101.111")
17+
}
18+

cryptography-serialization/asn1/src/commonMain/kotlin/internal/DerDecoder.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ internal class DerDecoder(
3636

3737
while (true) {
3838
val index = currentIndex
39+
if (index >= descriptor.elementsCount) return CompositeDecoder.DECODE_DONE
3940
tagOverride = descriptor.getElementContextSpecificTag(index)
4041

4142
if (descriptor.isElementOptional(index)) {

0 commit comments

Comments
 (0)