-
Notifications
You must be signed in to change notification settings - Fork 1
Parsing X509 IpAddressName #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
a69b3e1
824229f
328bd5d
66b61dc
0d12dcb
26225ab
1fcf3eb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||
|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,6 +2,7 @@ package at.asitplus.cidre | |||||||||
|
|
||||||||||
| import at.asitplus.cidre.byteops.CidrNumber | ||||||||||
| import at.asitplus.cidre.byteops.invInPlace | ||||||||||
| import at.asitplus.cidre.byteops.toPrefix | ||||||||||
| import kotlin.contracts.ExperimentalContracts | ||||||||||
| import kotlin.contracts.contract | ||||||||||
|
|
||||||||||
|
|
@@ -44,6 +45,39 @@ sealed interface IpAddressAndPrefix<N : Number, S : CidrNumber<S>> { | |||||||||
| /**`true` if this is (part of) the [IpNetwork.SpecialRanges.multicast] network */ | ||||||||||
| val isMulticast: Boolean | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Encodes this network into X.509 iPAddressName ByteArray (RFC 5280). | ||||||||||
| * IPv4: [4 bytes base address][4 bytes subnet mask] (8 bytes) | ||||||||||
| * IPv6: [16 bytes base address][16 bytes subnet mask] (32 bytes) | ||||||||||
| */ | ||||||||||
| fun toX509Octets(): ByteArray = address.octets + netmask | ||||||||||
|
|
||||||||||
| companion object { | ||||||||||
|
|
||||||||||
| /** | ||||||||||
| * Low-level helper used by [IpNetwork.fromX509Octets] and [IpInterface.fromX509Octets] | ||||||||||
| * to extract base address and CIDR prefix | ||||||||||
| * IPv4: [4 bytes base address][4 bytes subnet mask] (8 bytes) | ||||||||||
| * IPv6: [16 bytes base address][16 bytes subnet mask] (32 bytes) | ||||||||||
|
||||||||||
| * IPv4: [4 bytes base address][4 bytes subnet mask] (8 bytes) | |
| * IPv6: [16 bytes base address][16 bytes subnet mask] (32 bytes) | |
| * * IPv4 byte layout: `AAAANNNN`, where `A` ist an address octet and `N` is a netmask octet (8 bytes total) | |
| * * IPv6 byte layout: `AAAAAAAAAAAAAAAANNNNNNNNNNNNNNNN`, where `A` ist an address octet and `N` is a netmask octet(32 bytes total) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fee free to reformat, introduce spaces of whatever, if you feel it makes it more legible
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package at.asitplus.cidre | ||
|
|
||
| import at.asitplus.cidre.IpAddressAndPrefix.Companion.parseX509Octets | ||
| import at.asitplus.cidre.byteops.CidrNumber | ||
|
|
||
|
|
||
|
|
@@ -13,6 +14,8 @@ constructor(override val prefix: Prefix, val network: IpNetwork<N, S>) : | |
|
|
||
| override fun toString(): String = "$address/$prefix" | ||
|
|
||
| override fun toX509Octets(): ByteArray = super.toX509Octets() | ||
|
|
||
| companion object { | ||
| @Suppress("UNCHECKED_CAST") | ||
| internal fun <N : Number, S: CidrNumber<S>> unsafe( | ||
|
|
@@ -39,6 +42,20 @@ constructor(override val prefix: Prefix, val network: IpNetwork<N, S>) : | |
| val (addr, prefix) = parseIpAndPrefix(stringRepresentation) | ||
| return IpInterface(addr, prefix) | ||
| } | ||
|
|
||
| /** | ||
| * Decodes an IpInterface from X.509 iPAddressName ByteArray (RFC 5280). | ||
| * IPv4: [4 bytes base address][4 bytes subnet mask] (8 bytes) | ||
| * IPv6: [16 bytes base address][16 bytes subnet mask] (32 bytes) | ||
|
||
| */ | ||
| @Throws(IllegalArgumentException::class) | ||
| fun fromX509Octets(bytes: ByteArray): IpInterface<*, *> { | ||
| val (address, prefix) = parseX509Octets(bytes) | ||
| return when (address) { | ||
| is IpAddress.V4 -> V4(address, prefix) | ||
| is IpAddress.V6 -> V6(address, prefix) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class V4 internal constructor(override val address: IpAddress.V4, prefix: Prefix, network: IpNetwork.V4) : | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| package at.asitplus.cidre | ||
|
|
||
| import at.asitplus.cidre.IpAddressAndPrefix.Companion.parseX509Octets | ||
| import at.asitplus.cidre.byteops.CidrNumber | ||
| import at.asitplus.cidre.byteops.and | ||
| import at.asitplus.cidre.byteops.or | ||
|
|
@@ -528,6 +529,19 @@ constructor(address: IpAddress<N, S>, override val prefix: Prefix, strict: Boole | |
| ) as IpNetwork<N, S> | ||
| } | ||
|
|
||
| /** | ||
| * Decodes an IpNetwork from X.509 iPAddressName ByteArray (RFC 5280). | ||
JesusMcCloud marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| * IPv4: [4 bytes base address][4 bytes subnet mask] (8 bytes) | ||
| * IPv6: [16 bytes base address][16 bytes subnet mask] (32 bytes) | ||
|
||
| */ | ||
| fun fromX509Octets(bytes: ByteArray): IpNetwork<*, *> { | ||
| val (addr, prefix) = parseX509Octets(bytes) | ||
| return when (addr) { | ||
| is IpAddress.V4 -> V4(addr, prefix) | ||
| is IpAddress.V6 -> V6(addr, prefix) | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not render correctly, because […] is just MD syntax for a href with an internal link target of the same name as the visible text