scoop:scoop.tls.asyncio:0.1.0
src/context.scoop
Selected production source · src/context.scoop
1
package scoop.tls.asyncio3
import scoop.asyncio.*4
import scoop.crypto.x509.TrustStore5
import scoop.platform.fs.Path6
import scoop.tls.*8
/**9
* Creates a TLS client context for the current event loop and job. System trust loading runs as10
* managed blocking work; the CA-file overload reads the file asynchronously. The returned context must be11
* closed after use.12
*/13
public suspend fun openTlsClientContext(14
trust: TlsTrustSource = TlsTrustSource.System,15
options: TlsOptions = TlsOptions(),16
identity: TlsIdentity? = None17
): TlsClientContext {18
val loop = currentEventLoop()19
val job = requireJob(loop, true)20
when (trust) {21
case Store(val store) -> return TlsClientContext(store, options, identity)22
case System -> {23
return SystemTrustWork(options, identity).use {24
work ->25
val result = loop.executor.blocking { work.load() }26
try {27
job.ensureActive()28
result29
} catch (error: Throwable) {30
result.use { _ -> throw error }31
}32
}33
}34
}35
}37
/**38
* Creates a TLS client context for the current event loop and job. System trust loading runs as39
* managed blocking work; the CA-file overload reads the file asynchronously. The returned context must be40
* closed after use.41
*/42
public suspend fun openTlsClientContext(43
caFile: Path,44
options: TlsOptions = TlsOptions(),45
identity: TlsIdentity? = None46
): TlsClientContext {47
val bytes = currentEventLoop().openFile(caFile).useAsync suspend { file -> file.readAllBytes() }48
return TrustStore.fromPem(String.fromUtf8(bytes)).use {49
store -> TlsClientContext(store, options, identity)50
}51
}