scoop:scoop.tls:0.1.0
src/configuration.scoop
Selected production source · src/configuration.scoop
1
package scoop.tls3
import scoop.bytes.ByteArrayBuilder4
import scoop.crypto.native.peerIdentityData5
import scoop.crypto.x509.PeerIdentity6
import scoop.platform.net.IpAddress8
/**9
* TLS protocol versions accepted by [TlsOptions].10
*/11
public 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 use22
* their corresponding certificate identity rules.23
*/24
public 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
*/40
public struct TlsOptions(41
val minimum: TlsVersion = TlsVersion.Tls12,42
val maximum: TlsVersion = TlsVersion.Tls13,43
val alpn: List<Array<Byte>> = ArrayList<Array<Byte>>()44
)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
*/51
public struct TlsConnectionInfo(val version: TlsVersion, val alpn: Array<Byte>?)53
internal fun versionCode(version: TlsVersion): Int = when (version) {54
case Tls12 -> 0x030355
case Tls13 -> 0x030456
}58
internal 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 = 0L63
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.size66
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()71
}73
internal 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()))76
}