Skip to content
Permalink
Browse files

Merge pull request #18 from VirgilSecurity/develop

Develop
  • Loading branch information...
SanjoDeundiak committed Aug 14, 2019
2 parents 30acbc5 + dba291b commit 0dbd3cc0ac25615e4e7f55b9ace4b05cd306a2cb
@@ -39,7 +39,7 @@ To integrate Virgil E3Kit into your Xcode project using CocoaPods, specify it in
target '<Your Target Name>' do
use_frameworks!
pod 'VirgilE3Kit', '~> 0.7.0-beta1'
pod 'VirgilE3Kit', '~> 0.7.0-beta2'
end
```

@@ -63,7 +63,7 @@ $ brew install carthage
To integrate VirgilE3Kit into your Xcode project using Carthage, create an empty file with name *Cartfile* in your project's root folder and add following lines to your *Cartfile*

```
github "VirgilSecurity/virgil-e3kit-x" ~> 0.7.0-beta1
github "VirgilSecurity/virgil-e3kit-x" ~> 0.7.0-beta2
```

#### Linking against prebuilt binaries
@@ -42,12 +42,12 @@ import Foundation
/// - strToDataFailed: String to Data failed
/// - strFromDataFailed: Data to String failed
/// - missingPrivateKey: No private key on device. You should call `register()` of `retrievePrivateKey()`
/// - missingPublicKey: Passed empty LookupResult
/// - missingIdentities: Passed empty array of identities to lookup for
/// - missingPublicKey: Passed empty FindUsersResult
/// - missingIdentities: Passed empty array of identities to findUsers
/// - userIsAlreadyRegistered: User is already registered
/// - userIsNotRegistered: User is not registered
/// - privateKeyExists: Private key already exists in local key storage
/// - verificationFailed: Verification of message failed. This may be caused by rotating sender key. Try lookup new one
/// - verificationFailed: Verification of message failed. This may be caused by rotating sender key. Try finding new one
@objc(VTEEThreeError) public enum EThreeError: Int, LocalizedError {
case verifierInitFailed = 1
case strToDataFailed = 3
@@ -58,7 +58,7 @@ import Foundation
case userIsAlreadyRegistered = 8
case userIsNotRegistered = 9
case privateKeyExists = 10
case verificationFailed = 11
case verificationFailed = 12

/// Human-readable localized description
public var errorDescription: String? {
@@ -72,17 +72,17 @@ import Foundation
case .missingPrivateKey:
return "No private key on device. You should call register() of retrievePrivateKey()"
case .missingPublicKey:
return "Passed empty LookupResult"
return "Passed empty FindUsersResult"
case .missingIdentities:
return "Passed empty array of identities to lookup for"
return "Passed empty array of identities to findUsers"
case .userIsAlreadyRegistered:
return "User is already registered"
case .userIsNotRegistered:
return "User is not registered"
case .privateKeyExists:
return "Private key already exists in local key storage"
case .verificationFailed:
return "Verification of message failed. This may be caused by rotating sender key. Try lookup new one"
return "Verification of message failed. This may be caused by rotating sender key. Try finding new one"
}
}
}
@@ -36,12 +36,12 @@
import Foundation

/// Declares error types and codes for `LookupManager`
/// Declares error types and codes for finding users
///
/// - duplicateCards: Found duplicated Cards
/// - missingCachedCard: Card with provided identity was not found locally. Try to call lookupCard first
/// - missingCachedCard: Card with provided identity was not found locally. Try to call findUsers first
/// - cardWasNotFound: Card for one or more of provided identities was not found
@objc(VTELookupError) public enum LookupError: Int, LocalizedError {
@objc(VTEFindUsersError) public enum FindUsersError: Int, LocalizedError {
case duplicateCards = 1
case missingCachedCard = 2
case cardWasNotFound = 3
@@ -52,7 +52,7 @@ import Foundation
case .duplicateCards:
return "Found duplicated Cards"
case .missingCachedCard:
return "Card with provided identity was not found locally. Try to call lookupCard first"
return "Card with provided identity was not found locally. Try to call findUsers first"
case .cardWasNotFound:
return "Card for one or more of provided identities was not found"
}
@@ -46,7 +46,7 @@ internal class GroupManager {
private let lookupManager: LookupManager
private let crypto: VirgilCrypto

internal static let MaxTicketsInGroup: Int = 50
internal static let maxTicketsInGroup: Int = 50

internal init(localGroupStorage: FileGroupStorage,
cloudTicketStorage: CloudTicketStorage,
@@ -107,7 +107,7 @@ internal class GroupManager {

internal func retrieve(sessionId: Data) -> Group? {
guard let rawGroup = self.localGroupStorage.retrieve(sessionId: sessionId,
lastTicketsCount: GroupManager.MaxTicketsInGroup) else {
lastTicketsCount: GroupManager.maxTicketsInGroup) else {
return nil
}

@@ -36,8 +36,8 @@
import VirgilSDK

/// Typealias for the result of lookupCards call
public typealias LookupResult = [String: Card]
/// Typealias for the result of findUsers call
public typealias FindUsersResult = [String: Card]

internal class LookupManager {
internal let cardStorage: SQLiteCardStorage
@@ -74,7 +74,7 @@ internal class LookupManager {
Log.debug("Cached card with id: \(outdatedId) expired")

guard let outdatedCard = try self.cardStorage.getCard(cardId: outdatedId) else {
throw LookupError.missingCachedCard
throw FindUsersError.missingCachedCard
}

if let changedKeyDelegate = self.changedKeyDelegate {
@@ -98,18 +98,18 @@ internal class LookupManager {
}

extension LookupManager {
internal func lookupCachedCards(of identities: [String]) throws -> LookupResult {
internal func lookupCachedCards(of identities: [String]) throws -> FindUsersResult {
guard !identities.isEmpty else {
throw EThreeError.missingIdentities
}

var result: LookupResult = [:]
var result: FindUsersResult = [:]

let cards = try self.cardStorage.searchCards(identities: identities)

for identity in identities {
guard let card = cards.first(where: { $0.identity == identity }) else {
throw LookupError.missingCachedCard
throw FindUsersError.missingCachedCard
}

result[identity] = card
@@ -122,22 +122,22 @@ extension LookupManager {
let cards = try self.cardStorage.searchCards(identities: [identity])

guard cards.count < 2 else {
throw LookupError.duplicateCards
throw FindUsersError.duplicateCards
}

guard let card = cards.first else {
throw LookupError.missingCachedCard
throw FindUsersError.missingCachedCard
}

return card
}

internal func lookupCards(of identities: [String], forceReload: Bool = false) throws -> LookupResult {
internal func lookupCards(of identities: [String], forceReload: Bool = false) throws -> FindUsersResult {
guard !identities.isEmpty else {
throw EThreeError.missingIdentities
}

var result: LookupResult = [:]
var result: FindUsersResult = [:]

var identitiesSet = Set(identities)

@@ -160,7 +160,7 @@ extension LookupManager {

for card in cards {
guard result[card.identity] == nil else {
throw LookupError.duplicateCards
throw FindUsersError.duplicateCards
}

try self.cardStorage.storeCard(card)
@@ -171,7 +171,7 @@ extension LookupManager {
}

guard Set(result.keys) == Set(identities) else {
throw LookupError.cardWasNotFound
throw FindUsersError.cardWasNotFound
}

return result
@@ -181,7 +181,7 @@ extension LookupManager {
let cards = try self.lookupCards(of: [identity], forceReload: forceReload)

guard let card = cards[identity] else {
throw LookupError.cardWasNotFound
throw FindUsersError.cardWasNotFound
}

return card
@@ -87,7 +87,7 @@ extension Group {
}

do {
if currentEpoch - messageEpoch < GroupManager.MaxTicketsInGroup {
if currentEpoch - messageEpoch < GroupManager.maxTicketsInGroup {
return try self.session.decrypt(message: encrypted, publicKey: card.publicKey.key)
} else {
let sessionId = encrypted.getSessionId()
@@ -64,15 +64,15 @@ extension Group {
/// Adds new participants to group
///
/// - Note: New participant will be able to decrypt all history
/// - Parameter lookup: Cards of users to add. Result of lookupCards call
/// - Parameter participants: Cards of users to add. Result of findUsers call
/// - Returns: CallbackOperation<Void>
public func add(participants lookup: LookupResult) -> GenericOperation<Void> {
public func add(participants: FindUsersResult) -> GenericOperation<Void> {
return CallbackOperation { _, completion in
do {
try self.checkPermissions()

let oldSet = self.participants
let newSet = oldSet.union(lookup.keys)
let newSet = oldSet.union(participants.keys)

try Group.validateParticipantsCount(newSet.count)

@@ -83,7 +83,7 @@ extension Group {
let addSet = newSet.subtracting(oldSet)

let addedCards: [Card] = try addSet.map {
guard let card = lookup[$0] else {
guard let card = participants[$0] else {
throw GroupError.inconsistentState
}

@@ -121,15 +121,15 @@ extension Group {
/// Removes participants from group
///
/// - Note: Removed participant will not be able to decrypt previous history again after group update
/// - Parameter lookup: Cards of users to remove. Result of lookupCards call
/// - Parameter participants: Cards of users to remove. Result of findUsers call
/// - Returns: CallbackOperation<Void>
public func remove(participants lookup: LookupResult) -> GenericOperation<Void> {
public func remove(participants: FindUsersResult) -> GenericOperation<Void> {
return CallbackOperation { _, completion in
do {
try self.checkPermissions()

let oldSet = self.participants
let newSet = oldSet.subtracting(lookup.keys)
let newSet = oldSet.subtracting(participants.keys)

try Group.validateParticipantsCount(newSet.count)

@@ -180,13 +180,13 @@ extension Group {
self.participants = self.participants.union(newParticipants)
}

private func addNewTicket(for lookup: LookupResult) throws {
let newSet = Set(lookup.keys)
private func addNewTicket(for participants: FindUsersResult) throws {
let newSet = Set(participants.keys)

let ticketMessage = try self.session.createGroupTicket().getTicketMessage()
let ticket = Ticket(groupMessage: ticketMessage, participants: newSet)

_ = try self.groupManager.store(ticket, sharedWith: Array(lookup.values))
_ = try self.groupManager.store(ticket, sharedWith: Array(participants.values))

try self.session.addEpoch(message: ticket.groupMessage)

@@ -53,12 +53,12 @@ extension Group {
///
/// - Note: New participant will be able to decrypt all history
/// - Parameters:
/// - lookup: Cards of users to add. Result of lookupCards call
/// - participants: Cards of users to add. Result of findUsers call
/// - completion: completion handler
/// - error: corresponding error
@objc public func add(participants lookup: LookupResult,
@objc public func add(participants: FindUsersResult,
completion: @escaping (_ error: Error?) -> Void) {
self.add(participants: lookup).start { _, error in
self.add(participants: participants).start { _, error in
completion(error)
}
}
@@ -67,12 +67,12 @@ extension Group {
///
/// - Note: New participant will be able to decrypt all history
/// - Parameters:
/// - card: Card of user to add
/// - participant: Card of user to add
/// - completion: completion handler
/// - error: corresponding error
@objc public func add(participant card: Card,
@objc public func add(participant: Card,
completion: @escaping (_ error: Error?) -> Void) {
self.add(participant: card).start { _, error in
self.add(participant: participant).start { _, error in
completion(error)
}
}
@@ -93,25 +93,25 @@ extension Group {
///
/// - Note: Removed participant will not be able to decrypt previous history again after group update
/// - Parameters:
/// - lookup: Cards of users to remove. Result of lookupCards call
/// - participants: Cards of users to remove. Result of findUsers call
/// - completion: completion handler
/// - error: corresponding error
@objc public func remove(participants lookup: LookupResult,
@objc public func remove(participants: FindUsersResult,
completion: @escaping (_ error: Error?) -> Void) {
self.remove(participants: lookup).start { _, error in
self.remove(participants: participants).start { _, error in
completion(error)
}
}

/// Removes participant from group
///
/// - Parameters:
/// - card: Card of user to remove
/// - participant: Card of user to remove
/// - completion: completion handler
/// - error: corresponding error
@objc public func remove(participant card: Card,
@objc public func remove(participant: Card,
completion: @escaping (_ error: Error?) -> Void) {
self.remove(participant: card).start { _, error in
self.remove(participant: participant).start { _, error in
completion(error)
}
}
@@ -45,7 +45,7 @@ import Foundation
/// - invalidChangeParticipants: Group is invalid
/// - invalidParticipantsCount: Invalid change of group participants.
/// e.g. Add smb who is already in group or remove smb who is not
/// - verificationFailed: Verification of message failed. This may be caused by rotating sender key. Try lookup new one
/// - verificationFailed: Verification of message failed. This may be caused by rotating sender key. Try finding new one
/// - shortGroupId: Group Id length should be > 10
/// - messageNotFromThisGroup: Message was encrypted in group with different identifier
/// - groupIsOutdated: Group is not up to date. Call update or loadGroup
@@ -79,7 +79,7 @@ import Foundation
case .invalidParticipantsCount:
return "Please check valid participants count range in Group.ValidParticipatnsCountRange"
case .verificationFailed:
return "Verification of message failed. This may be caused by rotating sender key. Try lookup new one"
return "Verification of message failed. This may be caused by rotating sender key. Try finding new one"
case .shortGroupId:
return "Group Id length should be > 10"
case .messageNotFromThisGroup:
@@ -43,22 +43,22 @@ extension EThree {
/// - Note: identifier length should be > 10
/// - Parameters:
/// - identifier: identifier of group
/// - lookup: Cards of participants. Result of lookupCards call
/// - users: Cards of participants. Result of findUsers call
/// - Returns: CallbackOperation<Group>
public func createGroup(id identifier: Data, with lookup: LookupResult) -> GenericOperation<Group> {
public func createGroup(id identifier: Data, with users: FindUsersResult) -> GenericOperation<Group> {
return CallbackOperation { _, completion in
do {
let sessionId = try self.computeSessionId(from: identifier)

let participants = Set(lookup.keys + [self.identity])
let participants = Set(users.keys + [self.identity])

try Group.validateParticipantsCount(participants.count)

let ticket = try Ticket(crypto: self.crypto,
sessionId: sessionId,
participants: participants)

let group = try self.getGroupManager().store(ticket, sharedWith: Array(lookup.values))
let group = try self.getGroupManager().store(ticket, sharedWith: Array(users.values))

completion(group, nil)
} catch {

0 comments on commit 0dbd3cc

Please sign in to comment.
You can’t perform that action at this time.