Skip to content
This repository has been archived by the owner on Mar 5, 2021. It is now read-only.

Commit

Permalink
Update for metadata kit changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsandriesse committed Sep 14, 2020
1 parent 688635b commit 06051dd
Show file tree
Hide file tree
Showing 9 changed files with 2,793 additions and 2,774 deletions.
4 changes: 2 additions & 2 deletions Local Podspecs/SessionMetadataKit.podspec.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SessionMetadataKit",
"version": "1.0.8",
"version": "1.0.9",
"summary": "A Swift & Objective-C library used by other Session libraries.",
"description": "A Swift & Objective-C library used by other Session libraries.",
"homepage": "https://github.com/loki-project/session-ios-metadata-kit",
Expand All @@ -10,7 +10,7 @@
},
"source": {
"git": "https://github.com/loki-project/session-ios-metadata-kit.git",
"tag": "1.0.8"
"tag": "1.0.9"
},
"social_media_url": "https://getsession.org/",
"platforms": {
Expand Down
2 changes: 1 addition & 1 deletion Local Podspecs/SessionServiceKit.podspec.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
"~> 1.0.0"
],
"SessionMetadataKit": [
"~> 1.0.8"
"~> 1.0.9"
],
"PromiseKit": [
"~> 6.0"
Expand Down
14 changes: 7 additions & 7 deletions Manifest.lock
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ PODS:
- SessionHKDFKit/Tests (0.0.5):
- CocoaLumberjack
- SessionCoreKit
- SessionMetadataKit (1.0.8):
- SessionMetadataKit (1.0.9):
- CocoaLumberjack
- CryptoSwift (~> 1.3)
- SessionAxolotlKit (~> 1.0.7)
- SessionCoreKit (~> 1.0.0)
- SessionCurve25519Kit (~> 2.1.2)
- SessionHKDFKit (~> 0.0.5)
- SwiftProtobuf (~> 1.5.0)
- SessionMetadataKit/Tests (1.0.8):
- SessionMetadataKit/Tests (1.0.9):
- CocoaLumberjack
- CryptoSwift (~> 1.3)
- SessionAxolotlKit (~> 1.0.7)
Expand All @@ -101,7 +101,7 @@ PODS:
- SessionAxolotlKit (~> 1.0.7)
- SessionCoreKit (~> 1.0.0)
- SessionCurve25519Kit (~> 2.1.3)
- SessionMetadataKit (~> 1.0.8)
- SessionMetadataKit (~> 1.0.9)
- Starscream
- SwiftProtobuf (~> 1.5.0)
- YapDatabase/SQLCipher
Expand All @@ -118,7 +118,7 @@ PODS:
- SessionAxolotlKit (~> 1.0.7)
- SessionCoreKit (~> 1.0.0)
- SessionCurve25519Kit (~> 2.1.3)
- SessionMetadataKit (~> 1.0.8)
- SessionMetadataKit (~> 1.0.9)
- Starscream
- SwiftProtobuf (~> 1.5.0)
- YapDatabase/SQLCipher
Expand Down Expand Up @@ -289,7 +289,7 @@ CHECKOUT OPTIONS:
:commit: 0dcf8cf8a7995ef8663146f7063e6c1d7f5a3274
:git: https://github.com/nielsandriesse/session-ios-hkdf-kit.git
SessionMetadataKit:
:commit: b199165ece8b32bcbfc07cf76979cc925996db79
:commit: df787d84bb8adb23c10df669296dee8d7988e410
:git: https://github.com/loki-project/session-ios-metadata-kit
Starscream:
:commit: b09ea163c3cb305152c65b299cb024610f52e735
Expand All @@ -316,8 +316,8 @@ SPEC CHECKSUMS:
SessionCoreKit: 778a3f6e3da788b43497734166646025b6392e88
SessionCurve25519Kit: 9bb9afe199e4bc23578a4b15932ad2c57bd047b1
SessionHKDFKit: b0f4e669411703ab925aba07491c5611564d1419
SessionMetadataKit: 714497ffb50136b7965426d81bbf9f8cc0e69f98
SessionServiceKit: c86ddc530604d0e6f165a2b28f54929475f1cbfe
SessionMetadataKit: d37afdc47d20c7046faa139a92e68fa99f76c95b
SessionServiceKit: b12afb3975b33a9579802111f948838861d914bb
SQLCipher: e434ed542b24f38ea7b36468a13f9765e1b5c072
SSZipArchive: 62d4947b08730e4cda640473b0066d209ff033c9
Starscream: 8aaf1a7feb805c816d0e7d3190ef23856f6665b9
Expand Down
5,488 changes: 2,774 additions & 2,714 deletions Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,6 @@ struct SMKProtos_UnidentifiedSenderMessage {
case prekeyMessage // = 1
case message // = 2
case fallbackMessage // = 3
case closedGroupCiphertext // = 4

init() {
self = .prekeyMessage
Expand All @@ -283,7 +282,6 @@ struct SMKProtos_UnidentifiedSenderMessage {
case 1: self = .prekeyMessage
case 2: self = .message
case 3: self = .fallbackMessage
case 4: self = .closedGroupCiphertext
default: return nil
}
}
Expand All @@ -293,7 +291,6 @@ struct SMKProtos_UnidentifiedSenderMessage {
case .prekeyMessage: return 1
case .message: return 2
case .fallbackMessage: return 3
case .closedGroupCiphertext: return 4
}
}

Expand Down Expand Up @@ -570,6 +567,5 @@ extension SMKProtos_UnidentifiedSenderMessage.Message.TypeEnum: SwiftProtobuf._P
1: .same(proto: "PREKEY_MESSAGE"),
2: .same(proto: "MESSAGE"),
3: .same(proto: "FALLBACK_MESSAGE"),
4: .same(proto: "CLOSED_GROUP_CIPHERTEXT"),
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -513,15 +513,13 @@ extension SMKProtoSenderCertificate.SMKProtoSenderCertificateBuilder {
case prekeyMessage = 1
case message = 2
case fallbackMessage = 3
case closedGroupCiphertext = 4
}

private class func SMKProtoUnidentifiedSenderMessageMessageTypeWrap(_ value: SMKProtos_UnidentifiedSenderMessage.Message.TypeEnum) -> SMKProtoUnidentifiedSenderMessageMessageType {
switch value {
case .prekeyMessage: return .prekeyMessage
case .message: return .message
case .fallbackMessage: return .fallbackMessage
case .closedGroupCiphertext: return .closedGroupCiphertext
}
}

Expand All @@ -530,7 +528,6 @@ extension SMKProtoSenderCertificate.SMKProtoSenderCertificateBuilder {
case .prekeyMessage: return .prekeyMessage
case .message: return .message
case .fallbackMessage: return .fallbackMessage
case .closedGroupCiphertext: return .closedGroupCiphertext
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,27 +138,24 @@ public class SMKDecryptResult: NSObject {
private let preKeyStore: PreKeyStore
private let signedPreKeyStore: SignedPreKeyStore
private let identityStore: IdentityKeyStore
private let sharedSenderKeysImplementation: SharedSenderKeysProtocol!

@objc public init(sessionResetImplementation: SessionResetProtocol!,
sessionStore: SessionStore,
preKeyStore: PreKeyStore,
signedPreKeyStore: SignedPreKeyStore,
identityStore: IdentityKeyStore,
sharedSenderKeysImplementation: SharedSenderKeysProtocol!) throws {
identityStore: IdentityKeyStore) throws {
self.sessionResetImplementation = sessionResetImplementation
self.sessionStore = sessionStore
self.preKeyStore = preKeyStore
self.signedPreKeyStore = signedPreKeyStore
self.identityStore = identityStore
self.sharedSenderKeysImplementation = sharedSenderKeysImplementation
}

@objc public convenience init(sessionStore: SessionStore,
preKeyStore: PreKeyStore,
signedPreKeyStore: SignedPreKeyStore,
identityStore: IdentityKeyStore) throws {
try self.init(sessionResetImplementation: nil, sessionStore: sessionStore, preKeyStore: preKeyStore, signedPreKeyStore: signedPreKeyStore, identityStore: identityStore, sharedSenderKeysImplementation: nil)
try self.init(sessionResetImplementation: nil, sessionStore: sessionStore, preKeyStore: preKeyStore, signedPreKeyStore: signedPreKeyStore, identityStore: identityStore)
}

// MARK: - Public
Expand All @@ -182,28 +179,18 @@ public class SMKDecryptResult: NSObject {
throw SMKError.assertionError(description: "\(logTag) Missing our identity key pair.")
}

let keyPair: ECKeyPair
let encryptedMessage: CipherMessage
if useFallbackSessionCipher {
let cipher = FallBackSessionCipher(recipientPublicKey: recipientPublicKey, privateKey: ourIdentityKeyPair.privateKey)
let ivAndCiphertext = cipher.encrypt(paddedPlaintext)!
keyPair = ourIdentityKeyPair
encryptedMessage = FallbackMessage(_throws_with: ivAndCiphertext)
} else if sharedSenderKeysImplementation.isClosedGroup(recipientPublicKey) {
let senderPublicKey = "05" + ourIdentityKeyPair.publicKey.map { String(format: "%02hhx", $0) }.joined()
let ciphertextAndKeyIndex = try sharedSenderKeysImplementation.encrypt(paddedPlaintext, forGroupWithPublicKey: recipientPublicKey, senderPublicKey: senderPublicKey, protocolContext: protocolContext)
let ivAndCiphertext = ciphertextAndKeyIndex[0] as! Data
let keyIndex = ciphertextAndKeyIndex[1] as! UInt
keyPair = sharedSenderKeysImplementation.getKeyPair(forGroupWithPublicKey: recipientPublicKey)
encryptedMessage = ClosedGroupCiphertextMessage(_throws_withIVAndCiphertext: ivAndCiphertext, senderPublicKey: Data(hex: senderPublicKey), keyIndex: UInt32(keyIndex))
} else {
let cipher = SessionCipher(sessionStore: sessionStore,
preKeyStore: preKeyStore,
signedPreKeyStore: signedPreKeyStore,
identityKeyStore: identityStore,
recipientId: recipientPublicKey,
deviceId: deviceID)
keyPair = ourIdentityKeyPair
encryptedMessage = try cipher.encryptMessage(paddedPlaintext, protocolContext: protocolContext)
}

Expand Down Expand Up @@ -237,15 +224,15 @@ public class SMKDecryptResult: NSObject {

let staticKeyCipherData = try encrypt(cipherKey: ephemeralKeys.cipherKey,
macKey: ephemeralKeys.macKey,
plaintextData: keyPair.ecPublicKey().serialized)
plaintextData: ourIdentityKeyPair.ecPublicKey().serialized)

let staticSalt = NSData.join([
ephemeralKeys.chainKey,
staticKeyCipherData
])

let staticKeys = try throwswrapped_calculateStaticKeys(staticPublicKey: theirIdentityKey,
staticPrivateKey: keyPair.ecPrivateKey(),
staticPrivateKey: ourIdentityKeyPair.ecPrivateKey(),
salt: staticSalt)

let messageType: SMKMessageType
Expand All @@ -256,8 +243,6 @@ public class SMKDecryptResult: NSObject {
messageType = .whisper
case .fallback:
messageType = .fallback
case .closedGroupCiphertext:
messageType = .closedGroupCiphertext
default:
throw SMKError.assertionError(description: "\(logTag) Unknown cipher message type.")
}
Expand All @@ -279,7 +264,6 @@ public class SMKDecryptResult: NSObject {

@objc
public func throwswrapped_decryptMessage(certificateValidator: SMKCertificateValidator,
senderPublicKey: String,
cipherTextData: Data,
timestamp: UInt64,
localRecipientId: String,
Expand All @@ -293,13 +277,6 @@ public class SMKDecryptResult: NSObject {
throw SMKError.assertionError(description: "\(logTag) Missing our identity key pair.")
}

let keyPair: ECKeyPair
if sharedSenderKeysImplementation.isClosedGroup(senderPublicKey) {
keyPair = sharedSenderKeysImplementation.getKeyPair(forGroupWithPublicKey: senderPublicKey)
} else {
keyPair = ourIdentityKeyPair
}

let wrapper = try SMKUnidentifiedSenderMessage.parse(dataAndPrefix: cipherTextData)

guard let prefixData = kUDPrefixString.data(using: String.Encoding.utf8) else {
Expand All @@ -308,12 +285,12 @@ public class SMKDecryptResult: NSObject {

let ephemeralSalt = NSData.join([
prefixData,
try keyPair.ecPublicKey().serialized,
try ourIdentityKeyPair.ecPublicKey().serialized,
wrapper.ephemeralKey.serialized
])

let ephemeralKeys = try throwswrapped_calculateEphemeralKeys(ephemeralPublicKey: wrapper.ephemeralKey,
ephemeralPrivateKey: keyPair.ecPrivateKey(),
ephemeralPrivateKey: ourIdentityKeyPair.ecPrivateKey(),
salt: ephemeralSalt)

let staticKeyBytes = try decrypt(cipherKey: ephemeralKeys.cipherKey,
Expand All @@ -328,7 +305,7 @@ public class SMKDecryptResult: NSObject {
])

let staticKeys = try throwswrapped_calculateStaticKeys(staticPublicKey: staticKey,
staticPrivateKey: keyPair.ecPrivateKey(),
staticPrivateKey: ourIdentityKeyPair.ecPrivateKey(),
salt: staticSalt)

let messageBytes = try decrypt(cipherKey: staticKeys.cipherKey,
Expand Down Expand Up @@ -370,7 +347,7 @@ public class SMKDecryptResult: NSObject {

let paddedMessagePlaintext: Data
do {
paddedMessagePlaintext = try throwswrapped_decrypt(messageContent: messageContent, senderPublicKey: senderPublicKey, protocolContext: protocolContext)
paddedMessagePlaintext = try throwswrapped_decrypt(messageContent: messageContent, protocolContext: protocolContext)
} catch {
throw wrapAsKnownSenderError(error)
}
Expand Down Expand Up @@ -514,7 +491,6 @@ public class SMKDecryptResult: NSObject {
// MARK: - Decrypt

private func throwswrapped_decrypt(messageContent: SMKUnidentifiedSenderMessageContent,
senderPublicKey: String,
protocolContext: Any) throws -> Data {
// NOTE: We use the sender properties from the sender certificate, not from this class' properties.
let senderRecipientId = messageContent.senderCertificate.senderRecipientId
Expand All @@ -534,11 +510,6 @@ public class SMKDecryptResult: NSObject {
let cipher = FallBackSessionCipher(recipientPublicKey: senderRecipientId, privateKey: privateKey)
let plaintext = try cipher.decrypt(messageContent.contentData)!
return plaintext
case .closedGroupCiphertext:
let closedGroupCiphertextMessage = try ClosedGroupCiphertextMessage(_throws_with: messageContent.contentData)
let plaintext = try sharedSenderKeysImplementation.decrypt(closedGroupCiphertextMessage.ivAndCiphertext, forGroupWithPublicKey: senderPublicKey,
senderPublicKey: closedGroupCiphertextMessage.senderPublicKey.toHexString(), keyIndex: UInt(closedGroupCiphertextMessage.keyIndex), protocolContext: protocolContext)
return plaintext
}

let cipher = LokiSessionCipher(sessionResetImplementation: sessionResetImplementation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import Foundation
case whisper
case prekey
case fallback
case closedGroupCiphertext
}

// See:
Expand Down Expand Up @@ -40,8 +39,6 @@ import Foundation
messageType = .whisper
case .fallbackMessage:
messageType = .fallback
case .closedGroupCiphertext:
messageType = .closedGroupCiphertext
}

let contentData = proto.content
Expand All @@ -60,8 +57,6 @@ import Foundation
builderType = .prekeyMessage
case .fallback:
builderType = .fallbackMessage
case .closedGroupCiphertext:
builderType = .closedGroupCiphertext
}

let builder = SMKProtoUnidentifiedSenderMessageMessage.builder(type: builderType,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.0.8</string>
<string>1.0.9</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
Expand Down

0 comments on commit 06051dd

Please sign in to comment.