Change logic of passphrass for multikeys

This commit is contained in:
Mingshen Sun 2020-04-13 19:15:52 -07:00
parent e9c5b63c4b
commit eb6e854d92
15 changed files with 95 additions and 99 deletions

View file

@ -52,13 +52,14 @@ struct GopenPgp: PgpInterface {
for line in str.splitByNewline() {
if line.trimmed.uppercased().hasPrefix("-----BEGIN PGP") {
key = ""
key += line + "\n"
key += line
} else if line.trimmed.uppercased().hasPrefix("-----END PGP") {
key += line
keys.append(key)
} else {
key += line + "\n"
key += line
}
key += "\n"
}
return keys
}
@ -114,14 +115,12 @@ struct GopenPgp: PgpInterface {
return encryptedData.getBinary()!
}
var keyId: String {
let fingerprint = publicKeys.first?.key ?? ""
return String(fingerprint).uppercased()
var keyID: [String] {
return publicKeys.keys.map({ $0.uppercased() })
}
var shortKeyId: String {
let fingerprint = publicKeys.first?.key ?? ""
return String(fingerprint.suffix(8)).uppercased()
var shortKeyID: [String] {
return publicKeys.keys.map({ $0.suffix(8).uppercased()})
}
private func createPgpMessage(from encryptedData: Data) -> CryptoPGPMessage? {

View file

@ -42,11 +42,11 @@ struct ObjectivePgp: PgpInterface {
return encryptedData
}
var keyId: String {
return publicKey.keyID.longIdentifier
var keyID: [String] {
return keyring.keys.map({ $0.keyID.longIdentifier })
}
var shortKeyId: String {
return publicKey.keyID.shortIdentifier
var shortKeyID: [String] {
return keyring.keys.map({ $0.keyID.shortIdentifier })
}
}

View file

@ -35,14 +35,14 @@ public class PGPAgent {
pgpInterface = nil
}
public func getKeyId() throws -> String? {
public func getKeyID() throws -> [String] {
try checkAndInit()
return pgpInterface?.keyId
return pgpInterface?.keyID ?? []
}
public func getShortKeyId() throws -> String? {
public func getShortKeyID() throws -> [String] {
try checkAndInit()
return pgpInterface?.shortKeyId
return pgpInterface?.shortKeyID ?? []
}
public func decrypt(encryptedData: Data, keyID: String, requestPGPKeyPassphrase: (String) -> String) throws -> Data? {
@ -56,7 +56,7 @@ public class PGPAgent {
if previousDecryptStatus == false {
passphrase = requestPGPKeyPassphrase(keyID)
} else {
passphrase = keyStore.get(for: Globals.pgpKeyPassphrase) ?? requestPGPKeyPassphrase(keyID)
passphrase = keyStore.get(for: AppKeychain.getPGPKeyPassphraseKey(keyID: keyID)) ?? requestPGPKeyPassphrase(keyID)
}
// Decrypt.
guard let result = try pgpInterface!.decrypt(encryptedData: encryptedData, keyID: keyID, passphrase: passphrase) else {

View file

@ -12,7 +12,7 @@ protocol PgpInterface {
func encrypt(plainData: Data, keyID: String) throws -> Data
var keyId: String { get }
var keyID: [String] { get }
var shortKeyId: String { get }
var shortKeyID: [String] { get }
}

View file

@ -43,4 +43,16 @@ public class AppKeychain: KeyStore {
public func removeAllContent() {
try? keychain.removeAll()
}
public func removeAllContent(withPrefix prefix: String) {
for k in keychain.allKeys() {
if k.hasPrefix(prefix) {
try? keychain.remove(k)
}
}
}
public static func getPGPKeyPassphraseKey(keyID: String) -> String {
Globals.pgpKeyPassphrase + "-" + keyID
}
}

View file

@ -40,24 +40,26 @@ public class Utils {
}
public static func createRequestPGPKeyPassphraseHandler(controller: UIViewController) -> (String) -> String {
return { keyID in
return { keyID in
let sem = DispatchSemaphore(value: 0)
var passphrase = ""
DispatchQueue.main.async {
let alert = UIAlertController(title: "Passphrase".localize() + " (\(keyID.suffix(8)))", message: "FillInPgpPassphrase.".localize(), preferredStyle: UIAlertController.Style.alert)
let title = "Passphrase".localize() + " (\(keyID.suffix(8)))"
let message = "FillInPgpPassphrase.".localize()
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok".localize(), style: UIAlertAction.Style.default, handler: {_ in
passphrase = alert.textFields!.first!.text!
passphrase = alert.textFields?.first?.text ?? ""
sem.signal()
}))
alert.addTextField(configurationHandler: {(textField: UITextField!) in
textField.text = AppKeychain.shared.get(for: Globals.pgpKeyPassphrase) ?? ""
textField.text = AppKeychain.shared.get(for: AppKeychain.getPGPKeyPassphraseKey(keyID: keyID)) ?? ""
textField.isSecureTextEntry = true
})
controller.present(alert, animated: true, completion: nil)
}
let _ = sem.wait(timeout: DispatchTime.distantFuture)
if Defaults.isRememberPGPPassphraseOn {
AppKeychain.shared.add(string: passphrase, for: Globals.pgpKeyPassphrase)
AppKeychain.shared.add(string: passphrase, for: AppKeychain.getPGPKeyPassphraseKey(keyID: keyID))
}
return passphrase
}