Skip to content

Commit

Permalink
Simplified loading session
Browse files Browse the repository at this point in the history
  • Loading branch information
szysas committed Jun 29, 2022
1 parent 85adc88 commit 0975470
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 10 deletions.
4 changes: 2 additions & 2 deletions kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/MbedtlsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ internal object MbedtlsApi {
external fun mbedtls_ssl_conf_cid(mbedtlsSslConfig: Pointer, len: Int, ignoreOtherCids: Int): Int
external fun mbedtls_ssl_set_cid(sslContext: Pointer, enable: Int, ownCid: ByteArray, ownCidLen: Int): Int
external fun mbedtls_ssl_get_peer_cid(sslContext: Pointer, enabled: Pointer, peerCid: Pointer, peerCidLen: Pointer): Int
external fun mbedtls_ssl_context_save(sslContext: Pointer, buf: Pointer, bufLen: Int, outputLen: Pointer): Int
external fun mbedtls_ssl_context_load(sslContext: Pointer, buf: Pointer, len: Int): Int
external fun mbedtls_ssl_context_save(sslContext: Pointer, buf: ByteArray, bufLen: Int, outputLen: ByteArray): Int
external fun mbedtls_ssl_context_load(sslContext: Pointer, buf: ByteArray, len: Int): Int
external fun mbedtls_ssl_conf_ca_chain(sslConfig: Pointer, caChain: Pointer, caCrl: Pointer?)
external fun mbedtls_ssl_conf_own_cert(sslConfig: Pointer, ownCert: Memory, pkKey: Pointer): Int
external fun mbedtls_ssl_set_mtu(sslContext: Pointer, mtu: Int)
Expand Down
4 changes: 1 addition & 3 deletions kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/SslConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,7 @@ class SslConfig(
val sslContext = Memory(MbedtlsSizeOf.mbedtls_ssl_context).apply(MbedtlsApi::mbedtls_ssl_init)

mbedtls_ssl_setup(sslContext, conf).verify()
val buffer = Memory(session.size.toLong())
buffer.write(0, session, 0, session.size)
mbedtls_ssl_context_load(sslContext, buffer, buffer.size().toInt()).verify()
mbedtls_ssl_context_load(sslContext, session, session.size).verify()
mbedtls_ssl_set_bio(sslContext, Pointer.NULL, SendCallback, null, ReceiveCallback)

return SslSession(this, sslContext, cid).also {
Expand Down
11 changes: 6 additions & 5 deletions kotlin-mbedtls/src/main/kotlin/org/opencoap/ssl/SslContext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,18 @@ class SslSession internal constructor(
}

fun saveAndClose(): ByteArray {
val buffer = Memory(1280)
val outputLen = Memory(8)
mbedtls_ssl_context_save(sslContext, buffer, buffer.size().toInt(), outputLen).verify()
val buffer = ByteArray(1280)
val outputLen = ByteArray(4)
mbedtls_ssl_context_save(sslContext, buffer, buffer.size, outputLen).verify()
close()

return buffer.getByteArray(0, outputLen.getLong(0).toInt())
val size = (outputLen[0].toInt() and 0xff) + (outputLen[1].toInt() and 0xff shl 8)
return buffer.copyOf(size)
}

override fun toString(): String {
return if (peerCid != null) {
"[CID:${cid?.toHex()}, peerCID:${peerCid?.toHex()}, cipher-suite:${getCipherSuite()}]"
"[CID:${cid?.toHex()}, peerCID:${peerCid.toHex()}, cipher-suite:${getCipherSuite()}]"
} else {
"[cipher-suite:${getCipherSuite()}]"
}
Expand Down

0 comments on commit 0975470

Please sign in to comment.