Scoop API

scoop:scoop.tls:0.1.0

src/configuration.scoop

Selected production source · src/configuration.scoop

1package scoop.tls
3import scoop.bytes.ByteArrayBuilder
4import scoop.crypto.native.peerIdentityData
5import scoop.crypto.x509.PeerIdentity
6import scoop.platform.net.IpAddress
8/**
9 * TLS protocol versions accepted by [TlsOptions].
10 */
11public enum TlsVersion {
12 /**
13 * TLS version 1.2.
14 */
15 Tls12,
16 /**
17 * TLS version 1.3.
18 */
19 Tls13 }
20/**
21 * The expected peer identity checked during a client handshake. DNS names and IP addresses use
22 * their corresponding certificate identity rules.
23 */
24public enum TlsPeerName {
25 /**
26 * The expected DNS host name, also used for the client's server-name indication.
27 */
28 Dns(String),
29 /**
30 * The expected IP address checked against certificate IP-address entries.
31 */
32 Ip(IpAddress) }
33/**
34 * Protocol bounds and ordered ALPN byte strings for a TLS context.
35 *
36 * @property minimum The oldest permitted TLS version.
37 * @property maximum The newest permitted TLS version.
38 * @property alpn Application protocol identifiers, encoded as opaque bytes.
39 */
40public struct TlsOptions(
41 val minimum: TlsVersion = TlsVersion.Tls12,
42 val maximum: TlsVersion = TlsVersion.Tls13,
43 val alpn: List<Array<Byte>> = ArrayList<Array<Byte>>()
45/**
46 * Negotiated protocol information available after a successful handshake.
47 *
48 * @property version The TLS version selected for the connection.
49 * @property alpn The selected application protocol, or None if none was negotiated.
50 */
51public struct TlsConnectionInfo(val version: TlsVersion, val alpn: Array<Byte>?)
53internal fun versionCode(version: TlsVersion): Int = when (version) {
54 case Tls12 -> 0x0303
55 case Tls13 -> 0x0304
58internal fun encodeOptions(options: TlsOptions): Array<Byte> {
59 if (versionCode(options.minimum) > versionCode(options.maximum))
60 throw IllegalArgumentException(Some("minimum TLS version exceeds maximum"))
61 val result = ByteArrayBuilder()
62 var total = 0L
63 for (protocol in options.alpn) {
64 if (protocol.size < 1L || protocol.size > 255L) throw IllegalArgumentException(Some("ALPN protocol must contain 1 to 255 bytes"))
65 total += 1L + protocol.size
66 if (total > 65535L) throw IllegalArgumentException(Some("ALPN list exceeds 65535 bytes"))
67 result.append(protocol.size.toByte())
68 result.append(protocol)
69 }
70 return result.toByteArray()
73internal fun peerData(peer: TlsPeerName): (Int, Array<Byte>) = when (peer) {
74 case Dns(val name) -> peerIdentityData(PeerIdentity.Dns(name))
75 case Ip(val address) -> peerIdentityData(PeerIdentity.Ip(address.toByteArray()))