scoop:scoop.tls.asyncio:0.1.0
src/protocol.scoop
Selected production source · src/protocol.scoop
1
package scoop.tls.asyncio3
import scoop.tls.*4
import scoop.tls.native.*6
internal fun advanceProtocol(state: TlsConnection): Boolean {7
if (state.verificationBusy) return false8
if (present(state.exclusive)) return advanceRequest(state, state.exclusive!!)9
val read = state.reader10
val write = state.writer11
var changed = false12
if (present(read) && read!!.waiting == RetryKind.Output) {13
changed = advanceRequest(state, read!!)14
if (read!!.waiting == RetryKind.Output || state.closing) return changed15
}16
if (present(write) && write!!.kind == RequestKind.Write) {17
changed = advanceRequest(state, write!!) || changed18
if (write!!.waiting != RetryKind.Finished || state.closing) return changed19
}20
if (present(read)) changed = advanceRequest(state, read!!) || changed21
return changed22
}24
private fun advanceRequest(state: TlsConnection, entry: TlsRequest): Boolean {25
if (entry.selected) return false26
val ready = when (entry.waiting) {27
case Ready -> true28
case Input -> state.inputEpoch != entry.lastInput29
case Output -> state.outputEpoch != entry.lastOutput30
case Verification -> false31
case Finished -> false32
}33
if (!ready) return false34
try {35
val step = stepRequest(state, entry)36
entry.progress = step.count37
entry.lastInput = state.inputEpoch38
entry.lastOutput = state.outputEpoch39
entry.waiting = when (step.kind) {40
case Done -> RetryKind.Finished41
case WantRead -> RetryKind.Input42
case WantWrite -> RetryKind.Output43
case WantVerification -> RetryKind.Verification44
case Closed -> {45
if (entry.kind != RequestKind.Read)46
throw TlsException("TLS protocol", TlsFailureKind.Protocol, 0UL, 0L, 0, "unexpected close_notify")47
RetryKind.Finished48
}49
}50
} catch (error: Throwable) { terminate(state, Some(entry), error) }51
return true52
}54
private fun stepRequest(state: TlsConnection, entry: TlsRequest): TlsStep {55
@Unsafe { return when (entry.kind) {56
case Handshake -> state.engine.handshake()57
case Read -> state.engine.read(entry.borrow ?: throw IllegalStateException(Some("TLS read has no active borrow")))58
case Write -> state.engine.write(entry.borrow ?: throw IllegalStateException(Some("TLS write has no active borrow")))59
case Shutdown -> state.engine.shutdown()60
case Flush -> throw IllegalStateException(Some("flush does not execute an SSL step"))61
} }62
}64
internal fun inputRequest(state: TlsConnection): TlsRequest? {65
if (present(state.exclusive) && state.exclusive!!.waiting == RetryKind.Input) return state.exclusive66
if (present(state.writer) && state.writer!!.waiting == RetryKind.Input) return state.writer67
if (present(state.reader) && state.reader!!.waiting == RetryKind.Input) return state.reader68
return None69
}71
internal fun outputRequest(state: TlsConnection): TlsRequest {72
if (present(state.exclusive)) return state.exclusive!!73
if (present(state.writer)) return state.writer!!74
return state.reader ?: throw IllegalStateException(Some("TLS output has no active request"))75
}77
internal fun finishRequests(state: TlsConnection, actions: ArrayList<() -> Unit>): Boolean {78
if (state.outputBusy || state.output.readableBytes != 0L || state.engine.pendingOutput != 0L) return false79
var changed = false80
for (entry in requests(state)) {81
if (entry.waiting != RetryKind.Finished || entry.selected) continue82
if (present(state.inputOwner) && state.inputOwner!! === entry) continue83
if (entry.kind == RequestKind.Flush && !entry.flushed) continue84
entry.selected = true85
if (entry.kind == RequestKind.Shutdown) {86
state.closeJob = entry.job87
state.closing = true88
return true89
}90
releaseRequest(state, entry, actions)91
changed = true92
}93
return changed94
}