Scoop API

scoop:scoop.tls:0.1.0

src/identity.scoop

Selected production source · src/identity.scoop

1package scoop.tls
3import scoop.crypto.PrivateKey
4import scoop.crypto.DigestAlgorithm
5import scoop.crypto.digest
6import scoop.crypto.native.*
7import scoop.crypto.x509.*
8import scoop.tls.native.*
10/**
11 * A retained certificate chain and matching private key for TLS authentication. Construction
12 * requires a nonempty chain with the leaf first; the supplied certificate and key wrappers retain
13 * their own ownership. Close this identity after use.
14 */
15public class TlsIdentity private constructor(retained: RetainedTlsIdentity) : Closeable {
16 private var handle: ULong = retained.handle
17 private val held: AtomicBoolean = AtomicBoolean(false)
18 /**
19 * SHA-256 digest of the first certificate's DER encoding.
20 */
21 public val leafCertificateSha256: Array<Byte> = retained.leafCertificateSha256
23 public constructor(certificates: List<Certificate>, privateKey: PrivateKey)
24 : this(retainIdentity(certificates, privateKey)) {}
26 /**
27 * Factories for reading a TLS certificate chain and private key together.
28 */
29 public companion object {
30 /**
31 * Parses a PEM certificate chain and private key into one retained identity, releasing
32 * temporary certificate and key wrappers even on failure.
33 */
34 public fun fromPem(certificates: String, privateKey: String): TlsIdentity {
35 val chain = parseCertificatesPem(certificates)
36 try { return PrivateKey.fromPem(privateKey).use { key -> TlsIdentity(chain, key) } }
37 finally { for (certificate in chain) certificate.close() }
38 }
39 }
41 private fun <T> locked(block: () -> T): T {
42 while (!held.compareAndSet(false, true, MemoryOrder.Acquire, MemoryOrder.Relaxed)) { }
43 try { return block() }
44 finally { held.store(false, MemoryOrder.Release) }
45 }
47 /**
48 * Whether this identity has released its retained native state.
49 */
50 public val isClosed: Boolean get() = locked { handle == 0UL }
52 internal fun configure(context: ULong) {
53 locked {
54 if (handle == 0UL) throw IllegalStateException(Some("TLS identity is closed"))
55 @Unsafe { tlsValue(contextIdentity(context, handle), "configure TLS identity") }
56 Unit
57 }
58 }
60 /**
61 * Releases the retained certificate chain and key. Repeated closes are allowed.
62 */
63 public override fun close() {
64 locked {
65 val owned = handle
66 handle = 0UL
67 if (owned != 0UL) @Unsafe { identityFree(owned) }
68 Unit
69 }
70 }
73private struct RetainedTlsIdentity(val handle: ULong, val leafCertificateSha256: Array<Byte>)
75private fun retainIdentity(certificates: List<Certificate>, privateKey: PrivateKey): RetainedTlsIdentity {
76 val chain = certificates.toArray()
77 if (chain.size == 0L) throw IllegalArgumentException(Some("TLS identity requires a certificate chain"))
78 val fingerprint = digest(DigestAlgorithm.Sha256, chain[0L].toDer())
79 @Unsafe {
80 val key = privateKeyHandle(privateKey)
81 val handles = Array<ULong>(chain.size) { index -> @Unsafe { return@Array certificateHandle(chain[index]) } }
82 return handles.withDataPointer { pointer, count ->
83 @Unsafe { return@withDataPointer RetainedTlsIdentity(
84 tlsValue(identityNew(key, pointer, count), "create TLS identity"), fingerprint) }
85 }
86 }