scoop:scoop.tls:0.1.0
src/identity.scoop
Selected production source · src/identity.scoop
1
package scoop.tls3
import scoop.crypto.PrivateKey4
import scoop.crypto.DigestAlgorithm5
import scoop.crypto.digest6
import scoop.crypto.native.*7
import scoop.crypto.x509.*8
import scoop.tls.native.*10
/**11
* A retained certificate chain and matching private key for TLS authentication. Construction12
* requires a nonempty chain with the leaf first; the supplied certificate and key wrappers retain13
* their own ownership. Close this identity after use.14
*/15
public class TlsIdentity private constructor(retained: RetainedTlsIdentity) : Closeable {16
private var handle: ULong = retained.handle17
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.leafCertificateSha25623
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, releasing32
* 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
Unit57
}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 = handle66
handle = 0UL67
if (owned != 0UL) @Unsafe { identityFree(owned) }68
Unit69
}70
}71
}73
private struct RetainedTlsIdentity(val handle: ULong, val leafCertificateSha256: Array<Byte>)75
private 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
}87
}