feat: Implement all protobuf messages

This commit is contained in:
PapaTutuWawa 2022-08-05 12:28:46 +02:00
parent b8d70f1b88
commit 5c3cc424de
3 changed files with 170 additions and 0 deletions

View File

@ -0,0 +1,37 @@
import 'package:omemo_dart/src/helpers.dart';
import 'package:omemo_dart/src/protobuf/protobuf.dart';
class OmemoAuthenticatedMessage {
const OmemoAuthenticatedMessage(this.mac, this.message);
factory OmemoAuthenticatedMessage.fromBuffer(List<int> data) {
var i = 0;
// required bytes mac = 1;
if (data[0] != fieldId(1, fieldTypeByteArray)) {
throw Exception();
}
final mac = data.sublist(2, i + 2 + data[1]);
i += data[1] + 2;
if (data[i] != fieldId(2, fieldTypeByteArray)) {
throw Exception();
}
final message = data.sublist(i + 2, i + 2 + data[i + 1]);
return OmemoAuthenticatedMessage(mac, message);
}
final List<int> mac;
final List<int> message;
List<int> writeToBuffer() {
return concat([
[fieldId(1, fieldTypeByteArray), mac.length],
mac,
[fieldId(2, fieldTypeByteArray), message.length],
message,
]);
}
}

View File

@ -0,0 +1,67 @@
import 'package:omemo_dart/src/helpers.dart';
import 'package:omemo_dart/src/protobuf/omemo_authenticated_message.dart';
import 'package:omemo_dart/src/protobuf/protobuf.dart';
class OmemoKeyExchange {
const OmemoKeyExchange(this.pkId, this.spkId, this.ik, this.ek, this.message);
factory OmemoKeyExchange.fromBuffer(List<int> data) {
var i = 0;
if (data[i] != fieldId(1, fieldTypeUint32)) {
throw Exception();
}
var decoded = decodeVarint(data, 1);
final pkId = decoded.n;
i += decoded.length + 1;
if (data[i] != fieldId(2, fieldTypeUint32)) {
throw Exception();
}
decoded = decodeVarint(data, i + 1);
final spkId = decoded.n;
i += decoded.length + 1;
if (data[i] != fieldId(3, fieldTypeByteArray)) {
throw Exception();
}
final ik = data.sublist(i + 2, i + 2 + data[i + 1]);
i += 2 + data[i + 1];
if (data[i] != fieldId(4, fieldTypeByteArray)) {
throw Exception();
}
final ek = data.sublist(i + 2, i + 2 + data[i + 1]);
i += 2 + data[i + 1];
if (data[i] != fieldId(5, fieldTypeByteArray)) {
throw Exception();
}
final message = OmemoAuthenticatedMessage.fromBuffer(data.sublist(i + 2));
return OmemoKeyExchange(pkId, spkId, ik, ek, message);
}
final int pkId;
final int spkId;
final List<int> ik;
final List<int> ek;
final OmemoAuthenticatedMessage message;
List<int> writeToBuffer() {
final msg = message.writeToBuffer();
return concat([
[fieldId(1, fieldTypeUint32)],
encodeVarint(pkId),
[fieldId(2, fieldTypeUint32)],
encodeVarint(spkId),
[fieldId(3, fieldTypeByteArray), ik.length],
ik,
[fieldId(4, fieldTypeByteArray), ek.length],
ek,
[fieldId(5, fieldTypeByteArray), msg.length],
msg,
]);
}
}

View File

@ -1,4 +1,6 @@
import 'package:omemo_dart/protobuf/schema.pb.dart';
import 'package:omemo_dart/src/protobuf/omemo_authenticated_message.dart';
import 'package:omemo_dart/src/protobuf/omemo_key_exchange.dart';
import 'package:omemo_dart/src/protobuf/omemo_message.dart';
import 'package:omemo_dart/src/protobuf/protobuf.dart';
import 'package:test/test.dart';
@ -107,4 +109,68 @@ void main() {
expect(msg.ciphertext, <int>[]);
});
});
group('OMEMOAuthenticatedMessage', () {
test('Test encoding a message', () {
const msg = OmemoAuthenticatedMessage(<int>[1, 2, 3], <int>[4, 5, 6]);
final decoded = OMEMOAuthenticatedMessage.fromBuffer(msg.writeToBuffer());
expect(decoded.mac, <int>[1, 2, 3]);
expect(decoded.message, <int>[4, 5, 6]);
});
test('Test decoding a message', () {
final msg = OMEMOAuthenticatedMessage()
..mac = <int>[1, 2, 3]
..message = <int>[4, 5, 6];
final bytes = msg.writeToBuffer();
final decoded = OmemoAuthenticatedMessage.fromBuffer(bytes);
expect(decoded.mac, <int>[1, 2, 3]);
expect(decoded.message, <int>[4, 5, 6]);
});
});
group('OMEMOKeyExchange', () {
test('Test encoding a message', () {
const message = OmemoKeyExchange(
698,
245,
<int>[1, 4, 6],
<int>[4, 6, 7, 80],
OmemoAuthenticatedMessage(
<int>[5, 6, 8, 0],
<int>[4, 5, 7, 3, 2],
),
);
final kex = OMEMOKeyExchange.fromBuffer(message.writeToBuffer());
expect(kex.pkId, 698);
expect(kex.spkId, 245);
expect(kex.ik, <int>[1, 4, 6]);
expect(kex.ek, <int>[4, 6, 7, 80]);
expect(kex.message.mac, <int>[5, 6, 8, 0]);
expect(kex.message.message, <int>[4, 5, 7, 3, 2]);
});
test('Test decoding a message', () {
final message = OMEMOAuthenticatedMessage()
..mac = <int>[5, 6, 8, 0]
..message = <int>[4, 5, 7, 3, 2];
final kex = OMEMOKeyExchange()
..pkId = 698
..spkId = 245
..ik = <int>[1, 4, 6]
..ek = <int>[4, 6, 7, 80]
..message = message;
final decoded = OmemoKeyExchange.fromBuffer(kex.writeToBuffer());
expect(decoded.pkId, 698);
expect(decoded.spkId, 245);
expect(decoded.ik, <int>[1, 4, 6]);
expect(decoded.ek, <int>[4 ,6 ,7 , 80]);
expect(decoded.message.mac, <int>[5, 6, 8, 0]);
expect(decoded.message.message, <int>[4, 5, 7, 3, 2]);
});
});
}