Scoop API

scoop:scoop.tls.asyncio:0.1.0

src/context.scoop

Selected production source · src/context.scoop

1package scoop.tls.asyncio
3import scoop.asyncio.*
4import scoop.crypto.x509.TrustStore
5import scoop.platform.fs.Path
6import scoop.tls.*
8/**
9 * Creates a TLS client context for the current event loop and job. System trust loading runs as
10 * managed blocking work; the CA-file overload reads the file asynchronously. The returned context must be
11 * closed after use.
12 */
13public suspend fun openTlsClientContext(
14 trust: TlsTrustSource = TlsTrustSource.System,
15 options: TlsOptions = TlsOptions(),
16 identity: TlsIdentity? = None
17): 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 result
29 } catch (error: Throwable) {
30 result.use { _ -> throw error }
31 }
32 }
33 }
34 }
37/**
38 * Creates a TLS client context for the current event loop and job. System trust loading runs as
39 * managed blocking work; the CA-file overload reads the file asynchronously. The returned context must be
40 * closed after use.
41 */
42public suspend fun openTlsClientContext(
43 caFile: Path,
44 options: TlsOptions = TlsOptions(),
45 identity: TlsIdentity? = None
46): 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 }