scoop:scoop.crypto:0.1.0
src/x509/trust.scoop
Selected production source · src/x509/trust.scoop
1
package scoop.crypto.x5093
import scoop.Result4
import scoop.crypto.native.*5
import scoop.time.Instant7
/**8
* The intended certificate usage checked during chain validation.9
*/10
public enum CertificatePurpose {11
/**12
* Require suitability for a TLS server.13
*/14
ServerAuth,15
/**16
* Require suitability for a TLS client.17
*/18
ClientAuth,19
/**20
* Apply chain validation without a server/client usage restriction.21
*/22
Any }23
/**24
* A normal chain-validation rejection, including backend code, failing chain depth, and25
* explanatory text.26
*/27
public struct CertificateValidationFailure(val code: Long, val depth: Int, val detail: String)29
/**30
* An owned set of explicit trust anchors. It does not implicitly load host operating-system trust31
* settings.32
*/33
public class TrustStore private constructor(private var handle: ULong) : Closeable {34
/**35
* Creates a trust store from explicit certificate anchors.36
*/37
public companion object {38
/**39
* Parses PEM anchors into a new trust store and closes temporary certificate handles.40
*/41
public fun fromPem(text: String): TrustStore {42
val certificates = parseCertificatesPem(text)43
try { return fromCertificates(certificates) }44
finally { for (certificate in certificates) certificate.close() }45
}47
/**48
* Creates a store retaining the supplied anchors independently; the caller keeps ownership49
* of its certificate handles.50
*/51
public fun fromCertificates(anchors: List<Certificate>): TrustStore {52
@Unsafe {53
val store = TrustStore(cryptoValue(storeNew(), "create trust store"))54
try {55
for (anchor in anchors) cryptoValue(storeAdd(store.handle, certificateHandle(anchor)), "add trust anchor")56
return store57
} catch (error: Throwable) { store.close(); throw error }58
}59
}60
}62
internal fun nativeHandle(): ULong {63
if (handle == 0UL) throw IllegalStateException(Some("trust store is closed"))64
return handle65
}67
/**68
* Validates a certificate at the supplied time for the requested purpose, with optional69
* intermediates and expected peer identity. Ordinary rejection returns a failure Result;70
* native-operation failure throws.71
*/72
public fun verify(certificate: Certificate, at: Instant, purpose: CertificatePurpose,73
intermediates: List<Certificate> = ArrayList<Certificate>(),74
identity: PeerIdentity? = None): Result<Unit, CertificateValidationFailure> {75
val store = nativeHandle()76
val usage = when (purpose) { case ServerAuth -> 0; case ClientAuth -> 1; case Any -> 2 }77
val (kind, name) = when (identity) {78
case Some(_) -> peerIdentityData(identity!!)79
case None -> (-1, Array<Byte>(0L) { _ -> 0.toByte() })80
}81
val chain = Array<ULong>(intermediates.size) { index -> @Unsafe { return@Array certificateHandle(intermediates[index]) } }82
@Unsafe {83
val leaf = certificateHandle(certificate)84
return chain.withDataPointer { chainPointer, count ->85
@Unsafe { return@withDataPointer name.withDataPointer { namePointer, size ->86
@Unsafe {87
val result = certificateVerify(leaf, store, chainPointer, count, at.epochSeconds, usage,88
kind, if (kind < 0) None else Some(namePointer), size)89
cryptoValue(CryptoResult(0UL, result.error), "verify certificate")90
if (result.code == 0L) return@withDataPointer Result.Success<Unit, CertificateValidationFailure>(Unit)91
return@withDataPointer Result.Failure<Unit, CertificateValidationFailure>(CertificateValidationFailure(92
result.code, result.depth, String.fromCString(verifyMessage(result.code))))93
}94
} }95
}96
}97
}99
/**100
* Releases the native trust store; repeated closes are harmless.101
*/102
public override fun close() {103
val owned = handle104
handle = 0UL105
if (owned != 0UL) @Unsafe { storeFree(owned) }106
}107
}