Update protos

authentication
Niels Andriesse 4 years ago
parent 02a318a1e3
commit f6ecd9decf

@ -15,7 +15,7 @@ public final class ClosedGroupControlMessage : ControlMessage {
// MARK: Kind
public enum Kind : CustomStringConvertible {
case new(publicKey: Data, name: String, encryptionKeyPair: ECKeyPair, members: [Data], admins: [Data], expirationTimer: UInt32)
/// An encryption key pair encrypted for each member individually.
/// The group x25519 and ed25519 encryption key pairs encrypted for each member individually.
///
/// - Note: `publicKey` is only set when an encryption key pair is sent in a one-to-one context (i.e. not in a group).
case encryptionKeyPair(publicKey: Data?, wrappers: [KeyPairWrapper])
@ -42,32 +42,37 @@ public final class ClosedGroupControlMessage : ControlMessage {
@objc(SNKeyPairWrapper)
public final class KeyPairWrapper : NSObject, NSCoding { // NSObject/NSCoding conformance is needed for YapDatabase compatibility
public var publicKey: String?
public var encryptedKeyPair: Data?
public var encryptedX25519KeyPair: Data?
public var encryptedED25519KeyPair: Data?
public var isValid: Bool { publicKey != nil && encryptedKeyPair != nil }
public var isValid: Bool { publicKey != nil && encryptedX25519KeyPair != nil }
public init(publicKey: String, encryptedKeyPair: Data) {
public init(publicKey: String, encryptedX25519KeyPair: Data, encryptedED25519KeyPair: Data?) {
self.publicKey = publicKey
self.encryptedKeyPair = encryptedKeyPair
self.encryptedX25519KeyPair = encryptedX25519KeyPair
self.encryptedED25519KeyPair = encryptedED25519KeyPair
}
public required init?(coder: NSCoder) {
if let publicKey = coder.decodeObject(forKey: "publicKey") as! String? { self.publicKey = publicKey }
if let encryptedKeyPair = coder.decodeObject(forKey: "encryptedKeyPair") as! Data? { self.encryptedKeyPair = encryptedKeyPair }
if let encryptedX25519KeyPair = coder.decodeObject(forKey: "encryptedKeyPair") as! Data? { self.encryptedX25519KeyPair = encryptedX25519KeyPair }
if let encryptedED25519KeyPair = coder.decodeObject(forKey: "encryptedED25519KeyPair") as! Data? { self.encryptedED25519KeyPair = encryptedED25519KeyPair }
}
public func encode(with coder: NSCoder) {
coder.encode(publicKey, forKey: "publicKey")
coder.encode(encryptedKeyPair, forKey: "encryptedKeyPair")
coder.encode(encryptedX25519KeyPair, forKey: "encryptedKeyPair")
coder.encode(encryptedED25519KeyPair, forKey: "encryptedED25519KeyPair")
}
public static func fromProto(_ proto: SNProtoDataMessageClosedGroupControlMessageKeyPairWrapper) -> KeyPairWrapper? {
return KeyPairWrapper(publicKey: proto.publicKey.toHexString(), encryptedKeyPair: proto.encryptedKeyPair)
return KeyPairWrapper(publicKey: proto.publicKey.toHexString(), encryptedX25519KeyPair: proto.x25519, encryptedED25519KeyPair: proto.ed25519)
}
public func toProto() -> SNProtoDataMessageClosedGroupControlMessageKeyPairWrapper? {
guard let publicKey = publicKey, let encryptedKeyPair = encryptedKeyPair else { return nil }
let result = SNProtoDataMessageClosedGroupControlMessageKeyPairWrapper.builder(publicKey: Data(hex: publicKey), encryptedKeyPair: encryptedKeyPair)
guard let publicKey = publicKey, let encryptedX25519KeyPair = encryptedX25519KeyPair else { return nil }
let result = SNProtoDataMessageClosedGroupControlMessageKeyPairWrapper.builder(publicKey: Data(hex: publicKey), x25519: encryptedX25519KeyPair)
if let encryptedED25519KeyPair = encryptedED25519KeyPair { result.setEd25519(encryptedED25519KeyPair) }
do {
return try result.build()
} catch {
@ -89,7 +94,7 @@ public final class ClosedGroupControlMessage : ControlMessage {
public override var isValid: Bool {
guard super.isValid, let kind = kind else { return false }
switch kind {
case .new(let publicKey, let name, let encryptionKeyPair, let members, let admins, let expirationTimer):
case .new(let publicKey, let name, let encryptionKeyPair, let members, let admins, _):
return !publicKey.isEmpty && !name.isEmpty && !encryptionKeyPair.publicKey.isEmpty
&& !encryptionKeyPair.privateKey.isEmpty && !members.isEmpty && !admins.isEmpty
case .encryptionKeyPair: return true

@ -1412,13 +1412,16 @@ extension SNProtoDataMessageOpenGroupInvitation.SNProtoDataMessageOpenGroupInvit
// MARK: - SNProtoDataMessageClosedGroupControlMessageKeyPairWrapperBuilder
@objc public class func builder(publicKey: Data, encryptedKeyPair: Data) -> SNProtoDataMessageClosedGroupControlMessageKeyPairWrapperBuilder {
return SNProtoDataMessageClosedGroupControlMessageKeyPairWrapperBuilder(publicKey: publicKey, encryptedKeyPair: encryptedKeyPair)
@objc public class func builder(publicKey: Data, x25519: Data) -> SNProtoDataMessageClosedGroupControlMessageKeyPairWrapperBuilder {
return SNProtoDataMessageClosedGroupControlMessageKeyPairWrapperBuilder(publicKey: publicKey, x25519: x25519)
}
// asBuilder() constructs a builder that reflects the proto's contents.
@objc public func asBuilder() -> SNProtoDataMessageClosedGroupControlMessageKeyPairWrapperBuilder {
let builder = SNProtoDataMessageClosedGroupControlMessageKeyPairWrapperBuilder(publicKey: publicKey, encryptedKeyPair: encryptedKeyPair)
let builder = SNProtoDataMessageClosedGroupControlMessageKeyPairWrapperBuilder(publicKey: publicKey, x25519: x25519)
if let _value = ed25519 {
builder.setEd25519(_value)
}
return builder
}
@ -1428,19 +1431,23 @@ extension SNProtoDataMessageOpenGroupInvitation.SNProtoDataMessageOpenGroupInvit
@objc fileprivate override init() {}
@objc fileprivate init(publicKey: Data, encryptedKeyPair: Data) {
@objc fileprivate init(publicKey: Data, x25519: Data) {
super.init()
setPublicKey(publicKey)
setEncryptedKeyPair(encryptedKeyPair)
setX25519(x25519)
}
@objc public func setPublicKey(_ valueParam: Data) {
proto.publicKey = valueParam
}
@objc public func setEncryptedKeyPair(_ valueParam: Data) {
proto.encryptedKeyPair = valueParam
@objc public func setX25519(_ valueParam: Data) {
proto.x25519 = valueParam
}
@objc public func setEd25519(_ valueParam: Data) {
proto.ed25519 = valueParam
}
@objc public func build() throws -> SNProtoDataMessageClosedGroupControlMessageKeyPairWrapper {
@ -1456,14 +1463,24 @@ extension SNProtoDataMessageOpenGroupInvitation.SNProtoDataMessageOpenGroupInvit
@objc public let publicKey: Data
@objc public let encryptedKeyPair: Data
@objc public let x25519: Data
@objc public var ed25519: Data? {
guard proto.hasEd25519 else {
return nil
}
return proto.ed25519
}
@objc public var hasEd25519: Bool {
return proto.hasEd25519
}
private init(proto: SessionProtos_DataMessage.ClosedGroupControlMessage.KeyPairWrapper,
publicKey: Data,
encryptedKeyPair: Data) {
x25519: Data) {
self.proto = proto
self.publicKey = publicKey
self.encryptedKeyPair = encryptedKeyPair
self.x25519 = x25519
}
@objc
@ -1482,10 +1499,10 @@ extension SNProtoDataMessageOpenGroupInvitation.SNProtoDataMessageOpenGroupInvit
}
let publicKey = proto.publicKey
guard proto.hasEncryptedKeyPair else {
throw SNProtoError.invalidProtobuf(description: "\(logTag) missing required field: encryptedKeyPair")
guard proto.hasX25519 else {
throw SNProtoError.invalidProtobuf(description: "\(logTag) missing required field: x25519")
}
let encryptedKeyPair = proto.encryptedKeyPair
let x25519 = proto.x25519
// MARK: - Begin Validation Logic for SNProtoDataMessageClosedGroupControlMessageKeyPairWrapper -
@ -1493,7 +1510,7 @@ extension SNProtoDataMessageOpenGroupInvitation.SNProtoDataMessageOpenGroupInvit
let result = SNProtoDataMessageClosedGroupControlMessageKeyPairWrapper(proto: proto,
publicKey: publicKey,
encryptedKeyPair: encryptedKeyPair)
x25519: x25519)
return result
}

@ -848,21 +848,32 @@ struct SessionProtos_DataMessage {
mutating func clearPublicKey() {self._publicKey = nil}
/// @required
var encryptedKeyPair: Data {
get {return _encryptedKeyPair ?? Data()}
set {_encryptedKeyPair = newValue}
var x25519: Data {
get {return _x25519 ?? Data()}
set {_x25519 = newValue}
}
/// Returns true if `encryptedKeyPair` has been explicitly set.
var hasEncryptedKeyPair: Bool {return self._encryptedKeyPair != nil}
/// Clears the value of `encryptedKeyPair`. Subsequent reads from it will return its default value.
mutating func clearEncryptedKeyPair() {self._encryptedKeyPair = nil}
/// Returns true if `x25519` has been explicitly set.
var hasX25519: Bool {return self._x25519 != nil}
/// Clears the value of `x25519`. Subsequent reads from it will return its default value.
mutating func clearX25519() {self._x25519 = nil}
/// The encrypted ed25519 key pair
var ed25519: Data {
get {return _ed25519 ?? Data()}
set {_ed25519 = newValue}
}
/// Returns true if `ed25519` has been explicitly set.
var hasEd25519: Bool {return self._ed25519 != nil}
/// Clears the value of `ed25519`. Subsequent reads from it will return its default value.
mutating func clearEd25519() {self._ed25519 = nil}
var unknownFields = SwiftProtobuf.UnknownStorage()
init() {}
fileprivate var _publicKey: Data? = nil
fileprivate var _encryptedKeyPair: Data? = nil
fileprivate var _x25519: Data? = nil
fileprivate var _ed25519: Data? = nil
}
init() {}
@ -2188,12 +2199,13 @@ extension SessionProtos_DataMessage.ClosedGroupControlMessage.KeyPairWrapper: Sw
static let protoMessageName: String = SessionProtos_DataMessage.ClosedGroupControlMessage.protoMessageName + ".KeyPairWrapper"
static let _protobuf_nameMap: SwiftProtobuf._NameMap = [
1: .same(proto: "publicKey"),
2: .same(proto: "encryptedKeyPair"),
2: .same(proto: "x25519"),
3: .same(proto: "ed25519"),
]
public var isInitialized: Bool {
if self._publicKey == nil {return false}
if self._encryptedKeyPair == nil {return false}
if self._x25519 == nil {return false}
return true
}
@ -2204,7 +2216,8 @@ extension SessionProtos_DataMessage.ClosedGroupControlMessage.KeyPairWrapper: Sw
// enabled. https://github.com/apple/swift-protobuf/issues/1034
switch fieldNumber {
case 1: try { try decoder.decodeSingularBytesField(value: &self._publicKey) }()
case 2: try { try decoder.decodeSingularBytesField(value: &self._encryptedKeyPair) }()
case 2: try { try decoder.decodeSingularBytesField(value: &self._x25519) }()
case 3: try { try decoder.decodeSingularBytesField(value: &self._ed25519) }()
default: break
}
}
@ -2214,15 +2227,19 @@ extension SessionProtos_DataMessage.ClosedGroupControlMessage.KeyPairWrapper: Sw
if let v = self._publicKey {
try visitor.visitSingularBytesField(value: v, fieldNumber: 1)
}
if let v = self._encryptedKeyPair {
if let v = self._x25519 {
try visitor.visitSingularBytesField(value: v, fieldNumber: 2)
}
if let v = self._ed25519 {
try visitor.visitSingularBytesField(value: v, fieldNumber: 3)
}
try unknownFields.traverse(visitor: &visitor)
}
static func ==(lhs: SessionProtos_DataMessage.ClosedGroupControlMessage.KeyPairWrapper, rhs: SessionProtos_DataMessage.ClosedGroupControlMessage.KeyPairWrapper) -> Bool {
if lhs._publicKey != rhs._publicKey {return false}
if lhs._encryptedKeyPair != rhs._encryptedKeyPair {return false}
if lhs._x25519 != rhs._x25519 {return false}
if lhs._ed25519 != rhs._ed25519 {return false}
if lhs.unknownFields != rhs.unknownFields {return false}
return true
}

@ -122,9 +122,10 @@ message DataMessage {
message KeyPairWrapper {
// @required
required bytes publicKey = 1; // The public key of the user the key pair is meant for
required bytes publicKey = 1; // The public key of the user the key pair is meant for
// @required
required bytes encryptedKeyPair = 2; // The encrypted key pair
required bytes x25519 = 2; // The encrypted x25519 key pair
optional bytes ed25519 = 3; // The encrypted ed25519 key pair
}
// @required

@ -422,10 +422,10 @@ extension MessageReceiver {
return SNLog("Ignoring closed group encryption key pair from non-admin.")
}
// Find our wrapper and decrypt it if possible
guard let wrapper = wrappers.first(where: { $0.publicKey == userPublicKey }), let encryptedKeyPair = wrapper.encryptedKeyPair else { return }
guard let wrapper = wrappers.first(where: { $0.publicKey == userPublicKey }), let encryptedX25519KeyPair = wrapper.encryptedX25519KeyPair else { return }
let plaintext: Data
do {
plaintext = try MessageReceiver.decryptWithSessionProtocol(ciphertext: encryptedKeyPair, using: userKeyPair).plaintext
plaintext = try MessageReceiver.decryptWithSessionProtocol(ciphertext: encryptedX25519KeyPair, using: userKeyPair).plaintext
} catch {
return SNLog("Couldn't decrypt closed group encryption key pair.")
}

@ -74,8 +74,8 @@ extension MessageSender {
privateKey: newKeyPair.privateKey).build()
let plaintext = try! proto.serializedData()
let wrappers = targetMembers.compactMap { publicKey -> ClosedGroupControlMessage.KeyPairWrapper in
let ciphertext = try! MessageSender.encryptWithSessionProtocol(plaintext, for: publicKey)
return ClosedGroupControlMessage.KeyPairWrapper(publicKey: publicKey, encryptedKeyPair: ciphertext)
let encryptedX25519KeyPair = try! MessageSender.encryptWithSessionProtocol(plaintext, for: publicKey)
return ClosedGroupControlMessage.KeyPairWrapper(publicKey: publicKey, encryptedX25519KeyPair: encryptedX25519KeyPair, encryptedED25519KeyPair: nil)
}
let closedGroupControlMessage = ClosedGroupControlMessage(kind: .encryptionKeyPair(publicKey: nil, wrappers: wrappers))
var distributingKeyPairs = distributingClosedGroupEncryptionKeyPairs[groupPublicKey] ?? []
@ -330,7 +330,7 @@ extension MessageSender {
let contactThread = TSContactThread.getOrCreateThread(withContactSessionID: publicKey, transaction: transaction)
guard let ciphertext = try? MessageSender.encryptWithSessionProtocol(plaintext, for: publicKey) else { return }
SNLog("Sending latest encryption key pair to: \(publicKey).")
let wrapper = ClosedGroupControlMessage.KeyPairWrapper(publicKey: publicKey, encryptedKeyPair: ciphertext)
let wrapper = ClosedGroupControlMessage.KeyPairWrapper(publicKey: publicKey, encryptedX25519KeyPair: ciphertext, encryptedED25519KeyPair: nil)
let closedGroupControlMessage = ClosedGroupControlMessage(kind: .encryptionKeyPair(publicKey: Data(hex: groupPublicKey), wrappers: [ wrapper ]))
MessageSender.send(closedGroupControlMessage, in: contactThread, using: transaction)
}

Loading…
Cancel
Save