Skip to content
Snippets Groups Projects
Commit 5ef74f03 authored by Oliver Wiese's avatar Oliver Wiese
Browse files

parse keys from mail

parent a40eef33
Branches
Tags
No related merge requests found
......@@ -564,6 +564,7 @@ class MailHandler {
}
}
}
newKeyIds.append(contentsOf: findKeyString(content: body))
if let header = header, let from = header.from, let date = header.date {
let mail = DataHandler.handler.createMail(uid, sender: from, receivers: rec, cc: cc, time: date, received: true, subject: header.subject ?? "", body: body, flags: flags, record: record, autocrypt: autocrypt, decryptedData: dec, folderPath: folderPath, secretKey: secretKey, references: references, mailagent: userAgent, messageID: msgID)
if let m = mail {
......@@ -620,22 +621,33 @@ class MailHandler {
return nil
}
private func parsePublicKeys(attachment: MCOAttachment) -> [String] {
private func findKeyString(content: String) -> [String] {
var newKey = [String]()
if let content = attachment.decodedString() {
if content.contains("-----BEGIN PGP PUBLIC KEY BLOCK-----") {
if let start = content.range(of: "-----BEGIN PGP PUBLIC KEY BLOCK-----") {
if let end = content.range(of: "-----END PGP PUBLIC KEY BLOCK-----\n") {
let s = start.lowerBound
let e = end.upperBound
let pk = content[s..<e]
let pgp = SwiftPGP()
if let keyId = try? pgp.importKeys(key: String(pk), pw: nil, isSecretKey: false, autocrypt: false) {
newKey.append(contentsOf: keyId)
}
if content.contains("-----BEGIN PGP PUBLIC KEY BLOCK-----") {
if let start = content.range(of: "-----BEGIN PGP PUBLIC KEY BLOCK-----") {
var end = content.range(of: "-----END PGP PUBLIC KEY BLOCK-----\n")
if end == nil {
end = content.range(of: "-----END PGP PUBLIC KEY BLOCK-----")
}
if let end = end {
let s = start.lowerBound
let e = end.upperBound
let pk = content[s..<e]
let pgp = SwiftPGP()
if let keyId = try? pgp.importKeys(key: String(pk), pw: nil, isSecretKey: false, autocrypt: false) {
newKey.append(contentsOf: keyId)
}
}
}
}
return newKey
}
private func parsePublicKeys(attachment: MCOAttachment) -> [String] {
var newKey = [String]()
if let content = attachment.decodedString() {
newKey.append(contentsOf: findKeyString(content: content))
} else if attachment.mimeType == "application/octet-stream", let content = String(data: attachment.data, encoding: String.Encoding.utf8), content.hasPrefix("-----BEGIN PGP PUBLIC KEY BLOCK-----") && (content.hasSuffix("-----END PGP PUBLIC KEY BLOCK-----") || content.hasSuffix("-----END PGP PUBLIC KEY BLOCK-----\n")) {
let pgp = SwiftPGP()
if let keyId = try? pgp.importKeys(key: content, pw: nil, isSecretKey: false, autocrypt: false) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment