Skip to content

Commit

Permalink
Added Encryption tests, fixed macOS keystorage init
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugen Pivovarov committed Oct 19, 2018
1 parent c9782c5 commit 3850fb9
Show file tree
Hide file tree
Showing 16 changed files with 1,084 additions and 92 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -64,3 +64,4 @@ fastlane/report.xml
fastlane/Preview.html
fastlane/screenshots/**/*.png
fastlane/test_output
TestConfig.plist
17 changes: 14 additions & 3 deletions Source/EThree.swift
Expand Up @@ -47,6 +47,8 @@ import VirgilCryptoApiImpl
case missingKeys = 6
case passwordRequired = 7
case notBootstrapped = 8
case missingAppName = 9
case missingIdentities = 10
}

@objc(VTEEThree) open class EThree: NSObject {
Expand Down Expand Up @@ -83,11 +85,20 @@ import VirgilCryptoApiImpl
return IdentityKeyPair(privateKey: identityKey, publicKey: publicKey, isPublished: isPublished)
}

internal init(identity: String, cardManager: CardManager) throws {
internal init(identity: String, cardManager: CardManager, appName: String?) throws {
self.identity = identity
self.crypto = VirgilCrypto()
let keychainStorageParams = try KeychainStorageParams.makeKeychainStorageParams()
self.keychainStorage = KeychainStorage(storageParams: keychainStorageParams)

#if os(iOS) || os(tvOS) || os(watchOS)
let storageParams = try KeychainStorageParams.makeKeychainStorageParams()
#elseif os(macOS)
guard let appName = appName else {
throw EThreeError.missingAppName
}
let storageParams = KeychainStorageParams(appName: appName, trustedApplications: [])
#endif

self.keychainStorage = KeychainStorage(storageParams: storageParams)
self.privateKeyExporter = VirgilPrivateKeyExporter()
self.cardManager = cardManager
}
Expand Down
8 changes: 4 additions & 4 deletions Source/PublicAPI/EThree+Bootstrap.swift
Expand Up @@ -38,7 +38,7 @@ import VirgilSDK
import VirgilCryptoApiImpl

extension EThree {
@objc public static func initialize(tokenCallback: @escaping RenewJwtCallback,
@objc public static func initialize(appName: String? = nil, tokenCallback: @escaping RenewJwtCallback,
completion: @escaping (EThree?, Error?) -> ()) {
let renewTokenCallback: CachingJwtProvider.RenewJwtCallback = { _, completion in
tokenCallback(completion)
Expand All @@ -62,7 +62,7 @@ extension EThree {
cardVerifier: verifier)
let cardManager = CardManager(params: params)

let ethree = try EThree(identity: identity, cardManager: cardManager)
let ethree = try EThree(identity: identity, cardManager: cardManager, appName: appName)
completion(ethree, nil)
} catch {
completion(nil, error)
Expand All @@ -87,9 +87,9 @@ extension EThree {
}

if cards.isEmpty {
self.signIn(password: password, completion: completion)
} else {
self.signUp(password: password, completion: completion)
} else {
self.signIn(password: password, completion: completion)
}
}
}
Expand Down
22 changes: 17 additions & 5 deletions Source/PublicAPI/EThree+Encryption.swift
Expand Up @@ -38,26 +38,38 @@ import Foundation
import VirgilCryptoApiImpl

extension EThree {
@objc public func encrypt(_ text: String, for recipientKeys: [VirgilPublicKey]) throws -> String {
@objc public func encrypt(_ text: String, for recipientKeys: [VirgilPublicKey]? = nil) throws -> String {
guard let data = text.data(using: .utf8) else {
throw EThreeError.strToDataFailed
}
guard !recipientKeys.isEmpty, let selfKeyPair = self.identityKeyPair else {
if let recipientKeys = recipientKeys, recipientKeys.isEmpty {
throw EThreeError.missingKeys
}
let recipientKeys = recipientKeys ?? []

guard let selfKeyPair = self.identityKeyPair else {
throw EThreeError.notBootstrapped
}

let publicKeys = recipientKeys + [selfKeyPair.publicKey]
let encryptedData = try self.crypto.signThenEncrypt(data, with: selfKeyPair.privateKey, for: publicKeys)

return encryptedData.base64EncodedString()
}

@objc public func decrypt(_ encrypted: String, from senderKeys: [VirgilPublicKey]) throws -> String {
@objc public func decrypt(_ encrypted: String, from senderKeys: [VirgilPublicKey]? = nil) throws -> String {
guard let data = Data(base64Encoded: encrypted) else {
throw EThreeError.strToDataFailed
}
guard !senderKeys.isEmpty, let selfKeyPair = self.identityKeyPair else {
if let senderKeys = senderKeys, senderKeys.isEmpty {
throw EThreeError.missingKeys
}
let senderKeys = senderKeys ?? []

guard let selfKeyPair = self.identityKeyPair else {
throw EThreeError.notBootstrapped
}

let publicKeys = senderKeys + [selfKeyPair.publicKey]

let decryptedData = try self.crypto.decryptThenVerify(data, with: selfKeyPair.privateKey,
Expand All @@ -72,7 +84,7 @@ extension EThree {
@objc public func lookupPublicKeys(of identities: [String],
completion: @escaping ([VirgilPublicKey], [Error]) -> ()) {
guard !identities.isEmpty else {
completion([], [])
completion([], [EThreeError.missingIdentities])
return
}

Expand Down
6 changes: 1 addition & 5 deletions Source/VirgilE3Kit.h
Expand Up @@ -34,8 +34,4 @@
// Lead Maintainer: Virgil Security Inc. <support@virgilsecurity.com>
//

#ifndef VirgilE3Kit_h
#define VirgilE3Kit_h


#endif /* VirgilE3Kit_h */
#import "VirgilSDK/VSSKeyStoragePublic.h"
61 changes: 0 additions & 61 deletions Tests/TestConfig.swift

This file was deleted.

0 comments on commit 3850fb9

Please sign in to comment.