Skip to content
Snippets Groups Projects
Verified Commit cb0a2970 authored by Janne Mareike Koschinski's avatar Janne Mareike Koschinski
Browse files

Early experiments with improved Qt/Quassel type serialization

parent 24bd964e
Branches
No related tags found
1 merge request!2Draft: Jetpack compose rewrite
Pipeline #569 failed
Showing
with 753 additions and 16 deletions
......@@ -17,12 +17,18 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.ProtocolInfo
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.variant.QtType
import java.nio.ByteBuffer
object ProtocolInfoSerializer : Serializer<ProtocolInfo> {
object ProtocolInfoSerializer : QtSerializer<ProtocolInfo> {
override val qtType: QtType = QtType.UserType
override val javaType: Class<ProtocolInfo> = ProtocolInfo::class.java
override fun serialize(buffer: ChainedByteBuffer, data: ProtocolInfo) {
UByteSerializer.serialize(buffer, data.flags)
UShortSerializer.serialize(buffer, data.data)
......
......@@ -17,11 +17,17 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.variant.QtType
import java.nio.ByteBuffer
object ShortSerializer : Serializer<Short> {
object ShortSerializer : QtSerializer<Short> {
override val qtType: QtType = QtType.Short
override val javaType: Class<Short> = Short::class.java
override fun serialize(buffer: ChainedByteBuffer, data: Short) {
buffer.putShort(data)
}
......
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.io.stringEncoderAscii
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.variant.QtType
import java.nio.ByteBuffer
object StringSerializerAscii : QtSerializer<String?> {
override val qtType = QtType.QString
override val javaType: Class<out String> = String::class.java
override fun serialize(buffer: ChainedByteBuffer, data: String?) {
if (data == null) {
IntSerializer.serialize(buffer, -1)
} else {
val stringBuffer = stringEncoderAscii().encode(data, true)
IntSerializer.serialize(buffer, stringBuffer.remaining())
buffer.put(stringBuffer)
}
}
override fun deserialize(buffer: ByteBuffer): String? {
val length = IntSerializer.deserialize(buffer) - 1
return if (length < 0) {
null
} else {
stringEncoderAscii().decode(buffer, length, true)
}
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.io.stringEncoderUtf16
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.variant.QtType
import java.nio.ByteBuffer
object StringSerializerUtf16 : QtSerializer<String?> {
override val qtType = QtType.QString
override val javaType: Class<out String> = String::class.java
override fun serialize(buffer: ChainedByteBuffer, data: String?) {
if (data == null) {
IntSerializer.serialize(buffer, -1)
} else {
val stringBuffer = stringEncoderUtf16().encode(data)
IntSerializer.serialize(buffer, stringBuffer.remaining())
buffer.put(stringBuffer)
}
}
override fun deserialize(buffer: ByteBuffer): String? {
val length = IntSerializer.deserialize(buffer)
return if (length < 0) {
null
} else {
stringEncoderUtf16().decode(buffer, length)
}
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.io.stringEncoderUtf8
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.variant.QtType
import java.nio.ByteBuffer
object StringSerializerUtf8 : QtSerializer<String?> {
override val qtType = QtType.QString
override val javaType: Class<out String> = String::class.java
override fun serialize(buffer: ChainedByteBuffer, data: String?) {
if (data == null) {
IntSerializer.serialize(buffer, -1)
} else {
val stringBuffer = stringEncoderUtf8().encode(data)
IntSerializer.serialize(buffer, stringBuffer.remaining())
buffer.put(stringBuffer)
}
}
override fun deserialize(buffer: ByteBuffer): String? {
val length = IntSerializer.deserialize(buffer)
return if (length < 0) {
null
} else {
stringEncoderUtf8().decode(buffer, length)
}
}
}
......@@ -17,11 +17,17 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.variant.QtType
import java.nio.ByteBuffer
object UByteSerializer : Serializer<UByte> {
object UByteSerializer : QtSerializer<UByte> {
override val qtType: QtType = QtType.UChar
override val javaType: Class<UByte> = UByte::class.java
override fun serialize(buffer: ChainedByteBuffer, data: UByte) {
buffer.put(data.toByte())
}
......
......@@ -17,11 +17,17 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.variant.QtType
import java.nio.ByteBuffer
object UIntSerializer : Serializer<UInt> {
object UIntSerializer : QtSerializer<UInt> {
override val qtType: QtType = QtType.UInt
override val javaType: Class<UInt> = UInt::class.java
override fun serialize(buffer: ChainedByteBuffer, data: UInt) {
buffer.putInt(data.toInt())
}
......
......@@ -17,11 +17,17 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.variant.QtType
import java.nio.ByteBuffer
object ULongSerializer : Serializer<ULong> {
object ULongSerializer : QtSerializer<ULong> {
override val qtType: QtType = QtType.ULong
override val javaType: Class<ULong> = ULong::class.java
override fun serialize(buffer: ChainedByteBuffer, data: ULong) {
buffer.putLong(data.toLong())
}
......
......@@ -17,11 +17,17 @@
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.variant.QtType
import java.nio.ByteBuffer
object UShortSerializer : Serializer<UShort> {
object UShortSerializer : QtSerializer<UShort> {
override val qtType: QtType = QtType.UShort
override val javaType: Class<UShort> = UShort::class.java
override fun serialize(buffer: ChainedByteBuffer, data: UShort) {
buffer.putShort(data.toShort())
}
......
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.variant.QVariantMap
import de.kuschku.quasseldroid.protocol.variant.QVariant_
import de.kuschku.quasseldroid.protocol.variant.QtType
import java.nio.ByteBuffer
object VariantMapSerializer : QtSerializer<QVariantMap> {
override val qtType = QtType.QVariantMap
@Suppress("UNCHECKED_CAST")
override val javaType: Class<out QVariantMap> = Map::class.java as Class<QVariantMap>
override fun serialize(buffer: ChainedByteBuffer, data: QVariantMap) {
IntSerializer.serialize(buffer, data.size)
data.entries.forEach { (key, value) ->
StringSerializerUtf16.serialize(buffer, key)
VariantSerializer.serialize(buffer, value)
}
}
override fun deserialize(buffer: ByteBuffer): QVariantMap {
val result = mutableMapOf<String, QVariant_>()
val length = IntSerializer.deserialize(buffer)
for (i in 0 until length) {
result[StringSerializerUtf16.deserialize(buffer) ?: ""] = VariantSerializer.deserialize(buffer)
}
return result
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol.serializers.primitive
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.serializers.*
import de.kuschku.quasseldroid.protocol.variant.QVariant
import de.kuschku.quasseldroid.protocol.variant.QVariant_
import de.kuschku.quasseldroid.protocol.variant.QtType
import de.kuschku.quasseldroid.protocol.variant.QuasselType
import java.nio.ByteBuffer
object VariantSerializer : QtSerializer<QVariant_> {
override val qtType = QtType.QVariant
override val javaType: Class<QVariant_> = QVariant::class.java
override fun serialize(buffer: ChainedByteBuffer, data: QVariant_) {
IntSerializer.serialize(buffer, data.serializer.qtType.id)
BoolSerializer.serialize(buffer, false)
if (data is QVariant.Custom && data.serializer.qtType == QtType.UserType) {
StringSerializerAscii.serialize(buffer, data.serializer.quasselType.typeName)
}
data.serialize(buffer)
}
override fun deserialize(buffer: ByteBuffer): QVariant_ {
val rawType = IntSerializer.deserialize(buffer)
val qtType = QtType.of(rawType)
?: throw NoSerializerForTypeException(rawType, null)
// isNull, but we ignore it as it has no meaning
BoolSerializer.deserialize(buffer)
return if (qtType == QtType.UserType) {
val name = StringSerializerAscii.deserialize(buffer)
val quasselType = QuasselType.of(name)
?: throw NoSerializerForTypeException(qtType.id, name)
deserialize(quasselType, buffer)
} else {
deserialize(qtType, buffer)
}
}
@Suppress("UNCHECKED_CAST")
private fun deserialize(type: QtType, buffer: ByteBuffer): QVariant_ {
val serializer = Serializers[type]
?: throw NoSerializerForTypeException(type)
val value = serializer.deserialize(buffer)
return QVariant.of(value, serializer as QuasselSerializer<Any?>)
}
@Suppress("UNCHECKED_CAST")
private fun deserialize(type: QuasselType, buffer: ByteBuffer): QVariant_ {
val serializer = Serializers[type]
?: throw NoSerializerForTypeException(type)
val value = serializer.deserialize(buffer)
return QVariant.of(value, serializer as QuasselSerializer<Any?>)
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol.variant
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.serializers.QtSerializer
import de.kuschku.quasseldroid.protocol.serializers.QuasselSerializer
import de.kuschku.quasseldroid.protocol.serializers.primitive.IntSerializer
import de.kuschku.quasseldroid.protocol.serializers.serializerFor
typealias QVariant_ = QVariant<*>
typealias QVariantList = List<QVariant_>
typealias QVariantMap = Map<String, QVariant_>
sealed class QVariant<T> constructor(
val data: T,
open val serializer: QtSerializer<T>,
) {
class Typed<T> internal constructor(data: T, serializer: QtSerializer<T>) :
QVariant<T>(data, serializer) {
override fun toString() = "QVariant.Typed(${serializer.qtType.serializableName}, $data)"
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Typed<*>) return false
if (data != other.data) return false
if (serializer.qtType != other.serializer.qtType) return false
return true
}
override fun hashCode(): Int {
var result = data?.hashCode() ?: 0
result = 31 * result + serializer.qtType.hashCode()
return result
}
}
class Custom<T> internal constructor(data: T, override val serializer: QuasselSerializer<T>) :
QVariant<T>(data, serializer) {
override fun toString() = "QVariant.Custom(${serializer.quasselType}, $data)"
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (other !is Custom<*>) return false
if (data != other.data) return false
if (serializer.quasselType != other.serializer.quasselType) return false
return true
}
override fun hashCode(): Int {
var result = data?.hashCode() ?: 0
result = 31 * result + serializer.quasselType.hashCode()
return result
}
}
fun serialize(buffer: ChainedByteBuffer) {
serializer.serialize(buffer, data)
}
fun or(defValue: T): T {
return data ?: defValue
}
companion object {
fun <T> of(data: T, serializer: QtSerializer<T>) = Typed(data, serializer)
fun <T> of(data: T, serializer: QuasselSerializer<T>) = Custom(data, serializer)
}
}
inline fun <reified T> of(data: T, type: QtType): QVariant<T> =
QVariant.of(data, serializerFor(type))
inline fun <reified T> of(data: T, type: QuasselType): QVariant<T> =
QVariant.of(data, serializerFor(type))
@Suppress("UNCHECKED_CAST")
inline fun <reified T> QVariant_.into(): QVariant<T>? =
if (this.serializer.javaType == T::class.java) this as QVariant<T>
else null
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol.variant
import java.util.*
enum class QtType(val id: kotlin.Int) {
Void(0),//, VoidSerializer),
Bool(1),//, BoolSerializer),
Int(2),//, IntSerializer),
UInt(3),//, UIntSerializer),
LongLong(4),
ULongLong(5),
Double(6),
QChar(7),//, CharSerializer),
QVariantMap(8),//, VariantMapSerializer),
QVariantList(9),//, VariantListSerializer),
QString(10),//, StringSerializer.UTF16),
QStringList(11),//, StringListSerializer),
QByteArray(12),//, ByteArraySerializer),
QBitArray(13),
QDate(14),
QTime(15),//, TimeSerializer),
QDateTime(16),//, DateTimeSerializer),
QUrl(17),
QLocale(18),
QRect(19),
QRectF(20),
QSize(21),
QSizeF(22),
QLine(23),
QLineF(24),
QPoint(25),
QPointF(26),
QRegExp(27),
QVariantHash(28),
QEasingCurve(29),
FirstGuiType(63),
QFont(64),
QPixmap(65),
QBrush(66),
QColor(67),
QPalette(68),
QIcon(69),
QImage(70),
QPolygon(71),
QRegion(72),
QBitmap(73),
QCursor(74),
QSizePolicy(75),
QKeySequence(76),
QPen(77),
QTextLength(78),
QTextFormat(79),
QMatrix(80),
QTransform(81),
QMatrix4x4(82),
QVector2D(83),
QVector3D(84),
QVector4D(85),
QQuaternion(86),
VoidStar(128),
Long(129),//, LongSerializer),
Short(130),//, ShortSerializer),
Char(131),//, ByteSerializer),
ULong(132),//, ULongSerializer),
UShort(133),//, UShortSerializer),
UChar(134),//, UByteSerializer),
Float(135),
QObjectStar(136),
QWidgetStar(137),
QVariant(138),//, VariantSerializer),
User(256),
UserType(127),
LastType(-1);
val serializableName =
if (name.startsWith("Q")) name
else name.toLowerCase(Locale.ENGLISH)
companion object {
private val values = values().associateBy(QtType::id)
fun of(id: kotlin.Int): QtType? = values[id]
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.protocol.variant
enum class QuasselType(
val typeName: String,
val qtType: QtType = QtType.UserType,
) {
BufferId("BufferId"),
BufferInfo("BufferInfo"),
DccConfigIpDetectionMode("DccConfig::IpDetectionMode"),
DccConfigPortSelectionMode("DccConfig::PortSelectionMode"),
IrcUser("IrcUser"),
IrcChannel("IrcChannel"),
Identity("Identity"),
IdentityId("IdentityId"),
Message("Message"),
MsgId("MsgId"),
NetworkId("NetworkId"),
NetworkInfo("NetworkInfo"),
NetworkServer("Network::Server"),
QHostAddress("QHostAddress"),
PeerPtr("PeerPtr");
companion object {
private val values = values().associateBy(QuasselType::typeName)
fun of(typeName: String?): QuasselType? = values[typeName]
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.util
import java.security.cert.X509Certificate
import javax.net.ssl.SSLSession
data class TlsInfo(
val protocol: String,
val cipherSuite: String,
val keyExchangeMechanism: String?,
val certificateChain: List<X509Certificate>,
) {
override fun toString(): String {
return "TlsInfo(protocol='$protocol', cipherSuite='$cipherSuite', keyExchangeMechanism=$keyExchangeMechanism)"
}
companion object {
private val cipherSuiteRegex13 = "TLS_(.*)".toRegex()
private val cipherSuiteRegex12 = "TLS_(.*)_WITH_(.*)".toRegex()
private fun cipherSuiteRegex(protocol: String): Regex =
if (protocol == "TLSv1.3") cipherSuiteRegex13
else cipherSuiteRegex12
private fun parseCipherSuite(protocol: String, cipherSuite: String): Pair<String, String?>? {
val match = cipherSuiteRegex(protocol)
.matchEntire(cipherSuite)
?: return null
return if (protocol == "TLSv1.3") {
Pair(match.groupValues[1], null)
} else {
Pair(match.groupValues[1], match.groupValues.getOrNull(2))
}
}
fun ofSession(session: SSLSession): TlsInfo? {
val (cipherSuite, keyExchangeMechanism) = parseCipherSuite(
session.protocol,
session.cipherSuite,
) ?: return null
return TlsInfo(
session.protocol,
cipherSuite,
keyExchangeMechanism,
session.peerCertificateChain
.map(javax.security.cert.X509Certificate::toJavaCertificate)
)
}
}
}
/*
* Quasseldroid - Quassel client for Android
*
* Copyright (c) 2021 Janne Mareike Koschinski
* Copyright (c) 2021 The Quassel Project
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 3 as published
* by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package de.kuschku.quasseldroid.util
import java.io.ByteArrayInputStream
import java.security.cert.CertificateFactory
import java.security.cert.X509Certificate as javaCertificate
import javax.security.cert.X509Certificate as javaxCertificate
private val certificateFactory = CertificateFactory.getInstance("X.509")
fun javaxCertificate.toJavaCertificate(): javaCertificate =
certificateFactory.generateCertificate(ByteArrayInputStream(this.encoded))
as javaCertificate
fun javaCertificate.toJavaXCertificate(): javaxCertificate =
javaxCertificate.getInstance(this.encoded)
package de.kuschku.quasseldroid
import de.kuschku.quasseldroid.protocol.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.IntSerializer
import de.kuschku.quasseldroid.protocol.UIntSerializer
import de.kuschku.quasseldroid.util.CoroutineChannel
import de.kuschku.quasseldroid.protocol.io.ChainedByteBuffer
import de.kuschku.quasseldroid.protocol.serializers.primitive.IntSerializer
import de.kuschku.quasseldroid.protocol.serializers.primitive.ProtocolInfoSerializer
import de.kuschku.quasseldroid.protocol.serializers.primitive.UIntSerializer
import de.kuschku.quasseldroid.protocol.io.CoroutineChannel
import kotlinx.coroutines.runBlocking
import org.junit.Assert.assertEquals
import org.junit.Test
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.security.cert.X509Certificate
import javax.net.ssl.SSLContext
import javax.net.ssl.X509TrustManager
/**
* Example local unit test, which will execute on the development machine (host).
......@@ -23,19 +27,39 @@ class ExampleUnitTest {
@Test
fun testNetworking() {
val context = SSLContext.getInstance("TLSv1.3")
context.init(null, arrayOf(object : X509TrustManager {
override fun checkClientTrusted(chain: Array<out X509Certificate>?, authType: String?) {
// FIXME: accept everything
}
override fun checkServerTrusted(chain: Array<out X509Certificate>?, authType: String?) {
// FIXME: accept everything
}
override fun getAcceptedIssuers(): Array<X509Certificate> {
// FIXME: accept nothing
return emptyArray()
}
}), null)
runBlocking {
val sizeBuffer = ByteBuffer.allocateDirect(4)
val sendBuffer = ChainedByteBuffer(direct = true)
val channel = CoroutineChannel()
channel.connect(InetSocketAddress("kuschku.de", 4242))
val readBuffer = ByteBuffer.allocateDirect(4)
UIntSerializer.serialize(sendBuffer, 0x42b3_3f00u)
UIntSerializer.serialize(sendBuffer, 0x42b3_3f00u or 0x03u)
IntSerializer.serialize(sendBuffer, 2)
UIntSerializer.serialize(sendBuffer, 0x8000_0000u)
channel.write(sendBuffer)
channel.read(readBuffer)
readBuffer.flip()
println(IntSerializer.deserialize(readBuffer))
println(ProtocolInfoSerializer.deserialize(readBuffer))
println(channel.tlsInfo.value)
channel.enableTLS(context)
println(channel.tlsInfo.value)
channel.enableCompression()
}
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment