Skip to content

Commit

Permalink
Merge pull request #67 from VirgilSecurity/release-2.2.0
Browse files Browse the repository at this point in the history
Release 2.2.0
  • Loading branch information
SergeySeroshtan authored Aug 13, 2020
2 parents 1ca3a62 + d493a9f commit 4e1198b
Show file tree
Hide file tree
Showing 35 changed files with 888 additions and 778 deletions.
40 changes: 32 additions & 8 deletions Source/EThree+Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,32 @@ extension EThree {
return HttpConnection(adapters: [virgilAdapter])
}

internal func publishCardThenSaveLocal(keyPair: VirgilKeyPair? = nil, previousCardId: String? = nil) throws {
internal func publishCardThenSaveLocal(keyPair: VirgilKeyPair? = nil,
publishCardCallback: PublishCardCallback? = nil,
previousCardId: String? = nil) throws {
let keyPair = try keyPair ?? self.crypto.generateKeyPair(ofType: self.keyPairType)

let card = try self.cardManager.publishCard(privateKey: keyPair.privateKey,
let card: Card

if let publishCardCallback = publishCardCallback {
let modelSigner = ModelSigner(crypto: self.crypto)

let rawCard = try CardManager.generateRawCard(crypto: self.crypto,
modelSigner: modelSigner,
privateKey: keyPair.privateKey,
publicKey: keyPair.publicKey,
identity: self.identity)

card = try publishCardCallback(rawCard)
}
else {
card = try self.cardManager.publishCard(privateKey: keyPair.privateKey,
publicKey: keyPair.publicKey,
identity: self.identity,
previousCardId: previousCardId)
.startSync()
.get()
}

let data = try self.crypto.exportPrivateKey(keyPair.privateKey)

Expand All @@ -104,7 +121,8 @@ extension EThree {
}

private func setupTempChannelManager(keyPair: VirgilKeyPair) throws {
self.tempChannelManager = try TempChannelManager(crypto: self.crypto,
self.tempChannelManager = try TempChannelManager(appGroup: self.appGroup,
crypto: self.crypto,
accessTokenProvider: self.accessTokenProvider,
localKeyStorage: self.localKeyStorage,
keyknoxServiceUrl: self.serviceUrls.keyknoxServiceUrl,
Expand All @@ -114,9 +132,10 @@ extension EThree {
}

private func setupGroupManager(keyPair: VirgilKeyPair) throws {
let localGroupStorage = try FileGroupStorage(identity: self.identity,
crypto: self.crypto,
identityKeyPair: keyPair)
let localGroupStorage = try FileGroupStorage(appGroup: self.appGroup,
identity: self.identity,
crypto: self.crypto,
identityKeyPair: keyPair)

let cloudTicketStorage = try CloudTicketStorage(accessTokenProvider: self.accessTokenProvider,
localKeyStorage: self.localKeyStorage,
Expand Down Expand Up @@ -176,7 +195,7 @@ extension EThree {

let chat = try self.setupSecureChat(keyPair: keyPair, card: card)

try self.scheduleKeysRotation(with: chat, startFromNow: true)
try self.scheduleKeysRotation(with: chat, startFromNow: !self.offlineInit)
}
}

Expand All @@ -186,6 +205,9 @@ extension EThree {
accessTokenProvider: self.accessTokenProvider,
enablePostQuantum: Defaults.enableRatchetPqc)

context.appName = self.appName
context.appGroup = self.appGroup

context.client = RatchetClient(accessTokenProvider: self.accessTokenProvider,
serviceUrl: self.serviceUrls.ratchetServiceUrl,
connection: EThree.getConnection(),
Expand Down Expand Up @@ -229,7 +251,9 @@ extension EThree {
receiverCard card: Card,
name: String?) throws -> SecureSession {
do {
return try secureChat.startNewSessionAsSender(receiverCard: card, name: name, enablePostQuantum: Defaults.enableRatchetPqc)
return try secureChat.startNewSessionAsSender(receiverCard: card,
name: name,
enablePostQuantum: Defaults.enableRatchetPqc)
.startSync()
.get()
}
Expand Down
42 changes: 35 additions & 7 deletions Source/EThree.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ import VirgilSDKRatchet
public typealias JwtStringCallback = (String?, Error?) -> Void
/// Typealias for callback used below
public typealias RenewJwtCallback = (@escaping JwtStringCallback) -> Void
/// Typealias for callback used below
public typealias PublishCardCallback = (RawSignedModel) throws -> Card

/// Identity of user
@objc public let identity: String
Expand All @@ -56,6 +58,12 @@ import VirgilSDKRatchet
/// AccessTokenProvider
@objc public let accessTokenProvider: AccessTokenProvider

/// LocalKeyStorage
@objc public let localKeyStorage: LocalKeyStorage

/// Offline init
@objc public let offlineInit: Bool

/// VirgilCrypto instance
@objc public var crypto: VirgilCrypto {
return self.cardManager.crypto
Expand All @@ -68,9 +76,11 @@ import VirgilSDKRatchet

internal let keyPairType: KeyPairType
internal let enableRatchet: Bool
internal let enableRatchetPqc: Bool
internal let keyRotationInterval: TimeInterval
internal let appGroup: String?
internal let appName: String?

internal let localKeyStorage: LocalKeyStorage
internal let cloudKeyManager: CloudKeyManager
internal let cloudRatchetStorage: CloudRatchetStorage

Expand Down Expand Up @@ -103,12 +113,14 @@ import VirgilSDKRatchet
storageParams: KeychainStorageParams? = nil,
keyPairType: KeyPairType = Defaults.keyPairType,
enableRatchet: Bool = Defaults.enableRatchet,
enableRatchetPqc: Bool = Defaults.enableRatchetPqc,
keyRotationInterval: TimeInterval = Defaults.keyRotationInterval) throws {
let params = EThreeParams(identity: identity, tokenCallback: tokenCallback)
params.changedKeyDelegate = changedKeyDelegate
params.storageParams = storageParams
params.keyPairType = keyPairType
params.enableRatchet = enableRatchet
params.enableRatchetPqc = enableRatchetPqc
params.keyRotationInterval = keyRotationInterval

try self.init(params: params)
Expand Down Expand Up @@ -146,8 +158,9 @@ import VirgilSDKRatchet
verifier.whitelists = [whitelist]
}

let accessTokenProvider = CachingJwtProvider(initialJwt: params.initialJwt,
renewTokenCallback: { params.tokenCallback($1) })
let accessTokenProvider = CachingJwtProvider(initialJwt: params.initialJwt) {
params.tokenCallback($1)
}

let cardManagerParams = CardManagerParams(crypto: crypto,
accessTokenProvider: accessTokenProvider,
Expand Down Expand Up @@ -175,7 +188,8 @@ import VirgilSDKRatchet
keyknoxServiceUrl: params.serviceUrls.keyknoxServiceUrl,
pythiaServiceUrl: params.serviceUrls.pythiaServiceUrl)

let sqliteCardStorage = try SQLiteCardStorage(userIdentifier: params.identity,
let sqliteCardStorage = try SQLiteCardStorage(appGroup: params.appGroup,
userIdentifier: params.identity,
crypto: crypto,
verifier: verifier)

Expand All @@ -197,7 +211,11 @@ import VirgilSDKRatchet
serviceUrls: params.serviceUrls,
keyPairType: params.keyPairType,
enableRatchet: params.enableRatchet,
keyRotationInterval: params.keyRotationInterval)
enableRatchetPqc: params.enableRatchetPqc,
appGroup: params.appGroup,
appName: params.storageParams?.appName,
keyRotationInterval: params.keyRotationInterval,
offlineInit: params.offlineInit)
}

internal init(identity: String,
Expand All @@ -210,7 +228,11 @@ import VirgilSDKRatchet
serviceUrls: EThreeParams.ServiceUrls,
keyPairType: KeyPairType,
enableRatchet: Bool,
keyRotationInterval: TimeInterval) throws {
enableRatchetPqc: Bool,
appGroup: String?,
appName: String?,
keyRotationInterval: TimeInterval,
offlineInit: Bool) throws {
self.identity = identity
self.cardManager = cardManager
self.accessTokenProvider = accessTokenProvider
Expand All @@ -221,14 +243,20 @@ import VirgilSDKRatchet
self.serviceUrls = serviceUrls
self.keyPairType = keyPairType
self.enableRatchet = enableRatchet
self.enableRatchetPqc = enableRatchetPqc
self.appGroup = appGroup
self.appName = appName
self.keyRotationInterval = keyRotationInterval
self.offlineInit = offlineInit

super.init()

if try localKeyStorage.exists() {
try self.privateKeyChanged()
}

lookupManager.startUpdateCachedCards()
if !offlineInit {
lookupManager.startUpdateCachedCards()
}
}
}
6 changes: 4 additions & 2 deletions Source/Managers/TempChannelManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ internal class TempChannelManager {
return self.localKeyStorage.identity
}

internal init(crypto: VirgilCrypto,
internal init(appGroup: String?,
crypto: VirgilCrypto,
accessTokenProvider: AccessTokenProvider,
localKeyStorage: LocalKeyStorage,
keyknoxServiceUrl: URL,
Expand All @@ -64,7 +65,8 @@ internal class TempChannelManager {

let identity = localKeyStorage.identity

self.localStorage = try FileTempKeysStorage(identity: identity,
self.localStorage = try FileTempKeysStorage(appGroup: appGroup,
identity: identity,
crypto: crypto,
identityKeyPair: keyPair)

Expand Down
24 changes: 24 additions & 0 deletions Source/Models/EThreeParams.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,18 @@ import VirgilCrypto
@objc public let tokenCallback: EThree.RenewJwtCallback
/// [ChangedKeyDelegate](x-source-tag://ChangedKeyDelegate) to notify changing of User's keys
@objc public weak var changedKeyDelegate: ChangedKeyDelegate? = nil
/// AppGroup
@objc public var appGroup: String? = nil
/// `KeychainStorageParams` with specific parameters
@objc public var storageParams: KeychainStorageParams? = nil
/// Default key pair type
@objc public var keyPairType: KeyPairType = Defaults.keyPairType
/// Enables ratchet operations
@objc public var enableRatchet: Bool = Defaults.enableRatchet
/// Enables ratchet pqc
@objc public var enableRatchetPqc: Bool = Defaults.enableRatchetPqc
/// Offline init
@objc public var offlineInit: Bool = Defaults.offlineInit
/// TimeInterval of automatic rotate keys for double ratchet
@objc public var keyRotationInterval: TimeInterval = Defaults.keyRotationInterval
/// Service urls
Expand Down Expand Up @@ -98,11 +104,15 @@ import VirgilCrypto
private struct Config: Decodable {
var keyPairType: KeyPairType = Defaults.keyPairType
var enableRatchet: Bool = Defaults.enableRatchet
var enableRatchetPqc: Bool = Defaults.enableRatchetPqc
var offlineInit: Bool = Defaults.offlineInit
var keyRotationInterval: TimeInterval = Defaults.keyRotationInterval

enum CodingKeys: String, CodingKey {
case keyPairType
case enableRatchet
case enableRatchetPqc
case offlineInit
case keyRotationInterval
}

Expand All @@ -122,6 +132,18 @@ import VirgilCrypto
catch DecodingError.keyNotFound(_, _) { }
catch DecodingError.valueNotFound(_, _) { }

do {
self.enableRatchetPqc = try container.decode(Bool.self, forKey: .enableRatchetPqc)
}
catch DecodingError.keyNotFound(_, _) { }
catch DecodingError.valueNotFound(_, _) { }

do {
self.offlineInit = try container.decode(Bool.self, forKey: .offlineInit)
}
catch DecodingError.keyNotFound(_, _) { }
catch DecodingError.valueNotFound(_, _) { }

do {
self.keyRotationInterval = try container.decode(TimeInterval.self, forKey: .keyRotationInterval)
}
Expand Down Expand Up @@ -180,6 +202,8 @@ import VirgilCrypto

self.keyPairType = config.keyPairType
self.enableRatchet = config.enableRatchet
self.enableRatchetPqc = config.enableRatchetPqc
self.offlineInit = config.offlineInit
self.keyRotationInterval = config.keyRotationInterval
}

Expand Down
Loading

0 comments on commit 4e1198b

Please sign in to comment.