diff --git a/enzevalos_iphone.xcodeproj/project.pbxproj b/enzevalos_iphone.xcodeproj/project.pbxproj index 7a7ddd05e35060a9738ab25f1c828f735f656f34..90d6c625f715802d4f281448c527365d698b7f17 100644 --- a/enzevalos_iphone.xcodeproj/project.pbxproj +++ b/enzevalos_iphone.xcodeproj/project.pbxproj @@ -150,6 +150,9 @@ 47C036FF2347C0F5006295E8 /* ImportKeyOverviewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47C036FE2347C0F4006295E8 /* ImportKeyOverviewController.swift */; }; 47C037032347D4D1006295E8 /* PasteKeyViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47C037022347D4D1006295E8 /* PasteKeyViewController.swift */; }; 47C09C76243B3395007F74A2 /* SmallContactListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47C09C75243B3395007F74A2 /* SmallContactListView.swift */; }; + 47C112C22531D72E00621A07 /* PublicKeyRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47C112C12531D72E00621A07 /* PublicKeyRecord.swift */; }; + 47C112C62531DBDD00621A07 /* SecretKeyRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47C112C52531DBDD00621A07 /* SecretKeyRecord.swift */; }; + 47C112CA2531E9B000621A07 /* AttachmentRecord.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47C112C92531E9B000621A07 /* AttachmentRecord.swift */; }; 47C22281218AFD6300BD2C2B /* AutocryptTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47C22280218AFD6300BD2C2B /* AutocryptTest.swift */; }; 47C22283218B02C700BD2C2B /* autocryptSimpleExample1.eml in Resources */ = {isa = PBXBuildFile; fileRef = 47C22282218B02C700BD2C2B /* autocryptSimpleExample1.eml */; }; 47C8225324379EAE005BCE73 /* AttachmentsViewMain.swift in Sources */ = {isa = PBXBuildFile; fileRef = 47C8224324379EAE005BCE73 /* AttachmentsViewMain.swift */; }; @@ -611,6 +614,9 @@ 47C036FE2347C0F4006295E8 /* ImportKeyOverviewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ImportKeyOverviewController.swift; sourceTree = "<group>"; }; 47C037022347D4D1006295E8 /* PasteKeyViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PasteKeyViewController.swift; sourceTree = "<group>"; }; 47C09C75243B3395007F74A2 /* SmallContactListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SmallContactListView.swift; sourceTree = "<group>"; }; + 47C112C12531D72E00621A07 /* PublicKeyRecord.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PublicKeyRecord.swift; sourceTree = "<group>"; }; + 47C112C52531DBDD00621A07 /* SecretKeyRecord.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecretKeyRecord.swift; sourceTree = "<group>"; }; + 47C112C92531E9B000621A07 /* AttachmentRecord.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AttachmentRecord.swift; sourceTree = "<group>"; }; 47C22280218AFD6300BD2C2B /* AutocryptTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AutocryptTest.swift; sourceTree = "<group>"; }; 47C22282218B02C700BD2C2B /* autocryptSimpleExample1.eml */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = autocryptSimpleExample1.eml; sourceTree = "<group>"; }; 47C8224324379EAE005BCE73 /* AttachmentsViewMain.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AttachmentsViewMain.swift; sourceTree = "<group>"; }; @@ -1393,6 +1399,9 @@ 4733B1E42527196100AB5600 /* PersistentDataProvider.swift */, 4733B201252B142B00AB5600 /* Properties.swift */, 4733B205252B16D100AB5600 /* FolderRecord.swift */, + 47C112C12531D72E00621A07 /* PublicKeyRecord.swift */, + 47C112C52531DBDD00621A07 /* SecretKeyRecord.swift */, + 47C112C92531E9B000621A07 /* AttachmentRecord.swift */, ); path = persistentData; sourceTree = "<group>"; @@ -2381,6 +2390,7 @@ 475DF47A1F0D54C9009D807F /* Folder+CoreDataProperties.swift in Sources */, 475B00431F7BB6D6006CDD41 /* PersistentKey+CoreDataProperties.swift in Sources */, 479C649A21F45DAF00A01071 /* HideShowPasswordTextField.swift in Sources */, + 47C112C62531DBDD00621A07 /* SecretKeyRecord.swift in Sources */, 47CEAC98222541B40075B7DC /* MailSession.swift in Sources */, 47EABF292423C1FB00774A93 /* KeyboardChecker.swift in Sources */, F119D2901E364B59001D732A /* AnimatedSendIcon.swift in Sources */, @@ -2400,6 +2410,7 @@ 3EC35F2420037651008BDF95 /* InvitationHelper.swift in Sources */, A1B49E6421E55ECD00ED86FC /* IntroPageViewController.swift in Sources */, A1D5076F1E80257A00B68B38 /* KeyViewController.swift in Sources */, + 47C112C22531D72E00621A07 /* PublicKeyRecord.swift in Sources */, A1A9DE731F864B0500B808AA /* ExportCells.swift in Sources */, 472F39901E252470009260FB /* CNMailAddressesExtension.swift in Sources */, F113C38B1F3344C200E7F1D6 /* ViewControllerPannable.swift in Sources */, @@ -2418,6 +2429,7 @@ 47E737762284632F00972401 /* KeyCell.swift in Sources */, A1EB05961D956939008659C1 /* InboxTableViewCell.swift in Sources */, 47F79240203492E3005E7DB6 /* KeyRecord+CoreDataClass.swift in Sources */, + 47C112CA2531E9B000621A07 /* AttachmentRecord.swift in Sources */, A1083A541E8BFEA6003666B7 /* Onboarding.swift in Sources */, 476406982416B54D00C7D426 /* CiricleImage.swift in Sources */, A111F6AD1FA77B170060AFDE /* LoggerDetail.swift in Sources */, diff --git a/enzevalos_iphone/Autocrypt.swift b/enzevalos_iphone/Autocrypt.swift index 1419dc389ac0a1913af9f2f181c9181347892b8f..ad218766154dc27cf218d4f6556e05d72f7f6406 100644 --- a/enzevalos_iphone/Autocrypt.swift +++ b/enzevalos_iphone/Autocrypt.swift @@ -23,7 +23,7 @@ class Autocrypt { var addr: String = "" var type: CryptoScheme = .PGP - var prefer_encryption: EncState = EncState.NOAUTOCRYPT + var prefer_encryption: AutocryptState = AutocryptState.NOAUTOCRYPT var key: String = "" init(addr: String, type: String, prefer_encryption: String, key: String) { @@ -38,7 +38,7 @@ class Autocrypt { var field: [String] var addr = "" var type = "1" - var pref = EncState.MUTUAL.name + var pref = AutocryptState.MUTUAL.name var key = "" if autocrypt != nil { @@ -91,13 +91,13 @@ class Autocrypt { func setPrefer_encryption(_ input: String){ let pref = input.lowercased() - if pref == "yes" || pref == "mutual" || pref == EncState.MUTUAL.name { - self.prefer_encryption = EncState.MUTUAL - } else if pref == "no" || pref == EncState.NOPREFERENCE.name { - self.prefer_encryption = EncState.NOPREFERENCE + if pref == "yes" || pref == "mutual" || pref == AutocryptState.MUTUAL.name { + self.prefer_encryption = AutocryptState.MUTUAL + } else if pref == "no" || pref == AutocryptState.NOPREFERENCE.name { + self.prefer_encryption = AutocryptState.NOPREFERENCE } else { - self.prefer_encryption = EncState.NOPREFERENCE + self.prefer_encryption = AutocryptState.NOPREFERENCE } } @@ -111,7 +111,7 @@ class Autocrypt { } let adr = (UserManager.loadUserValue(Attribute.userAddr) as! String).lowercased() let skID = prefKey.keyID - let encPref = EncState.MUTUAL + let encPref = AutocryptState.MUTUAL let pgp = SwiftPGP() if let key = pgp.exportKey(id: skID, isSecretkey: false, autocrypt: true) { diff --git a/enzevalos_iphone/CNMailAddressesExtension.swift b/enzevalos_iphone/CNMailAddressesExtension.swift index cf4de43c21bbd0bf3af6b8ee19a854b3d47b2a91..11f5086e83ee2a109dd2cb5c3635041f87a2cb07 100644 --- a/enzevalos_iphone/CNMailAddressesExtension.swift +++ b/enzevalos_iphone/CNMailAddressesExtension.swift @@ -43,9 +43,9 @@ open class CNMailAddressExtension: MailAddress { } open var label: CNLabeledValue<NSString> - open var prefEnc: EncState { + open var prefEnc: AutocryptState { get { - return EncState.NOAUTOCRYPT + return AutocryptState.NOAUTOCRYPT } set { } diff --git a/enzevalos_iphone/MailAddress.swift b/enzevalos_iphone/MailAddress.swift index 122dabdf51529af1b7aff4e8cbe70efb61f311aa..8eab2691699d4a1fa76808e1c70cbc2338338055 100644 --- a/enzevalos_iphone/MailAddress.swift +++ b/enzevalos_iphone/MailAddress.swift @@ -21,7 +21,7 @@ import Foundation import Contacts -public enum EncState { +public enum AutocryptState { case MUTUAL case GOSSIP case NOPREFERENCE @@ -42,31 +42,31 @@ public enum EncState { } - static func find(i: Int) -> EncState { + static func find(i: Int) -> AutocryptState { switch i { case 0: - return EncState.MUTUAL + return AutocryptState.MUTUAL case 1: - return EncState.GOSSIP + return AutocryptState.GOSSIP case 2: - return EncState.NOPREFERENCE + return AutocryptState.NOPREFERENCE case 3: - return EncState.RESET + return AutocryptState.RESET case 4: - return EncState.NOAUTOCRYPT + return AutocryptState.NOAUTOCRYPT default: - return EncState.NOAUTOCRYPT + return AutocryptState.NOAUTOCRYPT } } func canEnc() -> Bool { switch self { - case EncState.MUTUAL: + case AutocryptState.MUTUAL: return true - case EncState.GOSSIP: + case AutocryptState.GOSSIP: return true - case EncState.RESET: + case AutocryptState.RESET: return true default: return false @@ -74,15 +74,15 @@ public enum EncState { } func asInt() -> Int16 { switch self { - case EncState.MUTUAL: + case AutocryptState.MUTUAL: return 0 - case EncState.GOSSIP: + case AutocryptState.GOSSIP: return 1 - case EncState.NOPREFERENCE: + case AutocryptState.NOPREFERENCE: return 2 - case EncState.RESET: + case AutocryptState.RESET: return 3 - case EncState.NOAUTOCRYPT: + case AutocryptState.NOAUTOCRYPT: return 4 } } diff --git a/enzevalos_iphone/PersistentKey+CoreDataProperties.swift b/enzevalos_iphone/PersistentKey+CoreDataProperties.swift index 74919be9eea96c1aaf3a93f08785f9c1d0fc814d..dc7f0cf71d5f8f7b7cf958236836b44145282a1d 100644 --- a/enzevalos_iphone/PersistentKey+CoreDataProperties.swift +++ b/enzevalos_iphone/PersistentKey+CoreDataProperties.swift @@ -179,7 +179,7 @@ extension PersistentKey { /** This is the autocrypt prefer enc state. */ - public var prefer_encryption: EncState{ + public var prefer_encryption: AutocryptState{ set { let name = "preferEncryption" self.willChangeValue(forKey: name) @@ -193,9 +193,9 @@ extension PersistentKey { if let i = self.primitiveValue(forKey: name) { let value = i as! Int self.didAccessValue(forKey: name) - return EncState.find(i: value) + return AutocryptState.find(i: value) } - return EncState.NOAUTOCRYPT + return AutocryptState.NOAUTOCRYPT } } diff --git a/enzevalos_iphone/UserData.swift b/enzevalos_iphone/UserData.swift index 39cf6eeb64542f4c21f9a4a67740312150c1db63..57be5c90efdb908f8273d585f87ef2b3f4e844d9 100644 --- a/enzevalos_iphone/UserData.swift +++ b/enzevalos_iphone/UserData.swift @@ -27,7 +27,7 @@ enum Attribute: Int { var defaultValue: AnyObject? { switch self { case .prefEncryption: - return EncState.MUTUAL.name as AnyObject? + return AutocryptState.MUTUAL.name as AnyObject? case .autocryptType: return "1" as AnyObject? // only openpgp case .sentFolderPath: //save backendFolderPath here diff --git a/enzevalos_iphone/mail/IncomingMail.swift b/enzevalos_iphone/mail/IncomingMail.swift index f3ce9a4805b41c30a51dd4a46ce8ccbb970affc8..1b34959289e33506ce307d554340b9ea672547cb 100644 --- a/enzevalos_iphone/mail/IncomingMail.swift +++ b/enzevalos_iphone/mail/IncomingMail.swift @@ -275,7 +275,8 @@ class IncomingMail { let sigState = self.cryptoObj?.signatureState.rawValue ?? 0 let encState = self.cryptoObj?.encryptionState.rawValue ?? 0 let f = Int16(self.flags.rawValue) - var m = MailProperties(messageID: self.msgID, subject: subject, date: date, flags: f, from: from, folder: folder, body: body, signatureState: sigState, encryptionState: encState) + var m = MailProperties(messageID: self.msgID, subject: subject, date: date, flags: f, from: from, to: to, cc: ccProperties, bcc: bccProperties, folder: folder, body: body, signatureState: sigState, encryptionState: encState, signedKey: nil, decryptionKey: nil, autocryptHeaderKey: nil, attachedPublicKeys: [], attachedSecretKeys: []) + // TODO: FIX KEYS m.to = to m.cc = ccProperties m.bcc = bccProperties diff --git a/enzevalos_iphone/persistentData/AttachmentRecord.swift b/enzevalos_iphone/persistentData/AttachmentRecord.swift new file mode 100644 index 0000000000000000000000000000000000000000..3598f7d68b404802117af41f345b749c6fb6af18 --- /dev/null +++ b/enzevalos_iphone/persistentData/AttachmentRecord.swift @@ -0,0 +1,19 @@ +// +// AttachmentRecord.swift +// enzevalos_iphone +// +// Created by Oliver Wiese on 10.10.20. +// Copyright © 2020 fu-berlin. All rights reserved. +// + +extension AttachmentRecord { + static let entityName = "AttachmentRecord" + + func update(property: AttachmentProperties) { + self.type = property.type + self.name = property.name + self.mimeType = property.mimeType + self.contentID = property.contentID + self.data = property.data + } +} diff --git a/enzevalos_iphone/persistentData/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents b/enzevalos_iphone/persistentData/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents index fee19b7bf690827d93a497419cd31e0db24e6974..f0db75e4b7a699f56636c8c7964d7428f7b96119 100644 --- a/enzevalos_iphone/persistentData/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents +++ b/enzevalos_iphone/persistentData/DataModel.xcdatamodeld/DataModel.xcdatamodel/contents @@ -2,7 +2,7 @@ <model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="17192" systemVersion="19H2" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier=""> <entity name="AbstractKeyRecord" representedClassName="AbstractKeyRecord" isAbstract="YES" syncable="YES" codeGenerationType="class"> <attribute name="cryptoProtocol" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/> - <attribute name="id" attributeType="String"/> + <attribute name="fingerprint" attributeType="String"/> </entity> <entity name="AddressRecord" representedClassName="AddressRecord" syncable="YES" codeGenerationType="class"> <attribute name="displayname" optional="YES" attributeType="String"/> @@ -51,9 +51,9 @@ <attribute name="signatureStateInt" attributeType="Integer 16" minValueString="-1" maxValueString="2" defaultValueString="0" usesScalarValueType="YES"/> <attribute name="subject" attributeType="String"/> <attribute name="xMailer" optional="YES" attributeType="String"/> - <relationship name="attachedAutocryptPublicKey" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PublicKeyRecord" inverseName="sendWithAutocryptHeader" inverseEntity="PublicKeyRecord"/> - <relationship name="attachedPublicKeys" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PublicKeyRecord" inverseName="sendWithMail" inverseEntity="PublicKeyRecord"/> - <relationship name="attachedSecretKey" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="SecretKeyRecord" inverseName="sendWithMail" inverseEntity="SecretKeyRecord"/> + <relationship name="attachedAutocryptPublicKey" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="PublicKeyRecord" inverseName="sendWithAutocryptHeader" inverseEntity="PublicKeyRecord"/> + <relationship name="attachedPublicKeys" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="PublicKeyRecord" inverseName="sendWithMail" inverseEntity="PublicKeyRecord"/> + <relationship name="attachedSecretKey" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="SecretKeyRecord" inverseName="sendWithMail" inverseEntity="SecretKeyRecord"/> <relationship name="bccAddresses" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="AddressRecord" inverseName="inBCCField" inverseEntity="AddressRecord"/> <relationship name="ccAddresses" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="AddressRecord" inverseName="inCCField" inverseEntity="AddressRecord"/> <relationship name="decryptionKey" optional="YES" maxCount="1" deletionRule="Nullify" destinationEntity="SecretKeyRecord" inverseName="decryptedMails" inverseEntity="SecretKeyRecord"/> @@ -68,18 +68,18 @@ </uniquenessConstraint> </uniquenessConstraints> </entity> - <entity name="PublicKeyRecord" representedClassName="PublicKeyRecord" syncable="YES" codeGenerationType="class"> + <entity name="PublicKeyRecord" representedClassName="PublicKeyRecord" parentEntity="AbstractKeyRecord" syncable="YES" codeGenerationType="class"> <attribute name="discoveryDate" attributeType="Date" usesScalarValueType="NO"/> <attribute name="lastSeenInAutocryptHeader" optional="YES" attributeType="Date" usesScalarValueType="NO"/> <attribute name="lastSeenSignedMail" optional="YES" attributeType="Date" usesScalarValueType="NO"/> <attribute name="origin" attributeType="Integer 16" defaultValueString="0" usesScalarValueType="YES"/> - <attribute name="preferEncryption" attributeType="Integer 16" usesScalarValueType="YES"/> + <attribute name="preferEncryption" optional="YES" attributeType="Integer 16" usesScalarValueType="YES"/> <relationship name="sendWithAutocryptHeader" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MailRecord" inverseName="attachedAutocryptPublicKey" inverseEntity="MailRecord"/> <relationship name="sendWithMail" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MailRecord" inverseName="attachedPublicKeys" inverseEntity="MailRecord"/> <relationship name="signedMails" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MailRecord" inverseName="signatureKey" inverseEntity="MailRecord"/> </entity> <entity name="SecretKeyRecord" representedClassName="SecretKeyRecord" parentEntity="AbstractKeyRecord" syncable="YES" codeGenerationType="class"> - <attribute name="exported" attributeType="Boolean" usesScalarValueType="YES"/> + <attribute name="exported" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/> <attribute name="importedDate" optional="YES" attributeType="Date" usesScalarValueType="NO"/> <relationship name="decryptedMails" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MailRecord" inverseName="decryptionKey" inverseEntity="MailRecord"/> <relationship name="sendWithMail" optional="YES" toMany="YES" deletionRule="Nullify" destinationEntity="MailRecord" inverseName="attachedSecretKey" inverseEntity="MailRecord"/> @@ -87,10 +87,10 @@ <elements> <element name="AbstractKeyRecord" positionX="-27" positionY="54" width="128" height="28"/> <element name="AddressRecord" positionX="-36" positionY="27" width="128" height="133"/> - <element name="FolderRecord" positionX="27" positionY="135" width="128" height="193"/> - <element name="MailRecord" positionX="-54" positionY="-9" width="128" height="328"/> - <element name="PublicKeyRecord" positionX="9" positionY="63" width="128" height="163"/> - <element name="SecretKeyRecord" positionX="-9" positionY="63" width="128" height="103"/> <element name="AttachmentRecord" positionX="0" positionY="144" width="128" height="133"/> + <element name="FolderRecord" positionX="27" positionY="135" width="128" height="193"/> + <element name="MailRecord" positionX="-54" positionY="-9" width="128" height="28"/> + <element name="PublicKeyRecord" positionX="9" positionY="63" width="128" height="28"/> + <element name="SecretKeyRecord" positionX="-9" positionY="63" width="128" height="28"/> </elements> </model> \ No newline at end of file diff --git a/enzevalos_iphone/persistentData/PersistentDataProvider.swift b/enzevalos_iphone/persistentData/PersistentDataProvider.swift index ccf2025296a74af939bd8e940e4553df81047fc4..723c1ee20c4c995870b95d9f72750edc262ea806 100644 --- a/enzevalos_iphone/persistentData/PersistentDataProvider.swift +++ b/enzevalos_iphone/persistentData/PersistentDataProvider.swift @@ -27,6 +27,7 @@ import CoreData class PersitentDataProvider { // MARK: - Core Data + private let entitiesNames = [AddressRecord.entityName, MailRecord.entityName, FolderRecord.entityName, PublicKeyRecord.entityName, SecretKeyRecord.entityName, AttachmentRecord.entityName] /** A persistent container to set up the Core Data stack. */ @@ -184,8 +185,47 @@ class PersitentDataProvider { folder.update(properties: m.folder) mail.inFolder = folder + // Add attachments + + for att in m.attachments { + let record = NSEntityDescription.insertNewObject(forEntityName: AttachmentRecord.entityName, into: taskContext) as! AttachmentRecord + record.update(property: att) + mail.addToIncludedAttachments(record) + } + + // Handle related cryptokeys + if let signedKey = m.signedKey { + let key = NSEntityDescription.insertNewObject(forEntityName: PublicKeyRecord.entityName, into: taskContext) as! PublicKeyRecord + key.update(property: signedKey) + mail.signatureKey = key + } + if let decKey = m.decryptionKey { + let key = NSEntityDescription.insertNewObject(forEntityName: SecretKeyRecord.entityName, into: taskContext) as! SecretKeyRecord + key.update(property: decKey) + mail.decryptionKey = key + } + if let autocryptKey = m.autocryptHeaderKey { + let key = NSEntityDescription.insertNewObject(forEntityName: PublicKeyRecord.entityName, into: taskContext) as! PublicKeyRecord + key.update(property: autocryptKey) + mail.attachedAutocryptPublicKey = key + } + for pk in m.attachedPublicKeys { + let key = NSEntityDescription.insertNewObject(forEntityName: PublicKeyRecord.entityName, into: taskContext) as! PublicKeyRecord + key.update(property: pk) + mail.addToAttachedPublicKeys(key) + } + for sk in m.attachedSecretKeys { + let key = NSEntityDescription.insertNewObject(forEntityName: SecretKeyRecord.entityName, into: taskContext) as! SecretKeyRecord + key.update(property: sk) + mail.addToAttachedSecretKey(key) + } + + + + } + private func importManyAddresses(addrs: [AddressProperties], taskContext: NSManagedObjectContext, addTo: (AddressRecord) -> () ) { for addr in addrs { let record = NSEntityDescription.insertNewObject(forEntityName: AddressRecord.entityName, into: taskContext) as! AddressRecord @@ -202,7 +242,7 @@ class PersitentDataProvider { func deleteAll(completionHandler: @escaping (Error?) -> Void) { let taskContext = newTaskContext() taskContext.perform { - for name in [AddressRecord.entityName, MailRecord.entityName, FolderRecord.entityName] { + for name in self.entitiesNames { let fetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: name) let batchDeleteRequest = NSBatchDeleteRequest(fetchRequest: fetchRequest) batchDeleteRequest.resultType = .resultTypeCount diff --git a/enzevalos_iphone/persistentData/Properties.swift b/enzevalos_iphone/persistentData/Properties.swift index 96e18a9b683c5714830bbbac5565acd0ee44ed9c..0de1c5e15d056bb87400447d919708acdf8251d9 100644 --- a/enzevalos_iphone/persistentData/Properties.swift +++ b/enzevalos_iphone/persistentData/Properties.swift @@ -37,10 +37,19 @@ struct MailProperties: DataPropertyProtocol { // Content properties var body: String = "" + var attachments: [AttachmentProperties] = [] - // Security properties + // Security properties & keys let signatureState: Int16 let encryptionState: Int16 + var signedKey: PublicKeyProperties? // TODO: Refactor! -> signatureKey + var decryptionKey: SecretKeyProperties? + + var autocryptHeaderKey: PublicKeyProperties? + var attachedPublicKeys: [PublicKeyProperties] = [] + var attachedSecretKeys: [SecretKeyProperties] = [] + + func update(m: Any) -> Bool { if let m = m as? MailRecord { @@ -87,6 +96,59 @@ struct FolderProperties: DataPropertyProtocol{ } return false } +} + +struct PublicKeyProperties: DataPropertyProtocol { + let entityName = "PublicKeyRecord" + + let fingerprint: String + let cryptoProtocol: CryptoScheme + + var origin: Origin + var preferEncryption: AutocryptState? + + var lastSeenInAutocryptHeader: Date? + var lastSeenSignedMail: Date? + + + func update(m: Any) -> Bool { + if let pk = m as? PublicKeyRecord { + pk.update(property: self) + return true + } + return false + } +} + +struct SecretKeyProperties: DataPropertyProtocol { + let entityName = "SecretKeyRecord" + + let fingerprint: String + let cryptoProtocol: CryptoScheme + + + func update(m: Any) -> Bool { + if let sk = m as? SecretKeyRecord { + sk.update(property: self) + return true + } + return false + } + +} + +struct AttachmentProperties: DataPropertyProtocol { + let entityName = "AttachmentRecord" + + let type: Int16 + let name: String + let mimeType: String + let contentID: Int64 + let data: Data + + func update(m: Any) -> Bool { + return false + } } diff --git a/enzevalos_iphone/persistentData/PublicKeyRecord.swift b/enzevalos_iphone/persistentData/PublicKeyRecord.swift new file mode 100644 index 0000000000000000000000000000000000000000..8a714134fcb6b4789c9fd6069679d1cd6cd75715 --- /dev/null +++ b/enzevalos_iphone/persistentData/PublicKeyRecord.swift @@ -0,0 +1,57 @@ +// +// PublicKeyRecord.swift +// enzevalos_iphone +// +// Created by Oliver Wiese on 10.10.20. +// Copyright © 2020 fu-berlin. All rights reserved. +// + +enum Origin: Int16 { + case AutocryptHeader = 0 + case AutocryptGossip = 1 + case MailAttachment = 2 + case KeyServer = 3 + case Generated = 4 + case FileTransfer = 5 +} + + +extension PublicKeyRecord { + static let entityName = "PublicKeyRecord" + + func update(property: PublicKeyProperties) { + self.fingerprint = property.fingerprint + self.cryptoProtocol = property.cryptoProtocol.asInt() + + if discoveryDate == nil { + discoveryDate = Date() + } + + self.origin = property.origin.rawValue + + if let lastSeen = property.lastSeenInAutocryptHeader { + if let last = self.lastSeenInAutocryptHeader { + if last < lastSeen { + self.lastSeenInAutocryptHeader = lastSeen + } + } + else { + self.lastSeenInAutocryptHeader = lastSeen + } + + } + if let prefEnc = property.preferEncryption { + self.preferEncryption = prefEnc.asInt() + } + if let lastSeen = property.lastSeenSignedMail { + if let last = self.lastSeenSignedMail { + if last < lastSeen { + self.lastSeenSignedMail = lastSeen + } + } + else { + self.lastSeenSignedMail = lastSeen + } + } + } +} diff --git a/enzevalos_iphone/persistentData/SecretKeyRecord.swift b/enzevalos_iphone/persistentData/SecretKeyRecord.swift new file mode 100644 index 0000000000000000000000000000000000000000..9e1f059fc86c65b359c28d015dabb85546b82bdf --- /dev/null +++ b/enzevalos_iphone/persistentData/SecretKeyRecord.swift @@ -0,0 +1,20 @@ +// +// SecretKeyRecord.swift +// enzevalos_iphone +// +// Created by Oliver Wiese on 10.10.20. +// Copyright © 2020 fu-berlin. All rights reserved. +// + +extension SecretKeyRecord { + static let entityName = "SecretKeyRecord" + + func update(property: SecretKeyProperties) { + self.fingerprint = property.fingerprint + self.cryptoProtocol = property.cryptoProtocol.asInt() + + if self.importedDate == nil { + self.importedDate = Date() + } + } +} diff --git a/enzevalos_iphoneTests/AutocryptTest.swift b/enzevalos_iphoneTests/AutocryptTest.swift index 5e16e1e73f546106afc68348f2a370e9ab9423fc..72863dcd2cf7c1ccb0fd52f42068097c920ff476 100644 --- a/enzevalos_iphoneTests/AutocryptTest.swift +++ b/enzevalos_iphoneTests/AutocryptTest.swift @@ -102,7 +102,7 @@ class AutocryptTest: XCTestCase { if let parser = MCOMessageParser(data: outmail.plainData), let _ = pgp.exportKey(id: userKeyID, isSecretkey: false, autocrypt: false) { let autocrypt = Autocrypt.init(header: parser.header) XCTAssertEqual(autocrypt.addr, userAdr) - XCTAssertEqual(autocrypt.prefer_encryption, EncState.MUTUAL) + XCTAssertEqual(autocrypt.prefer_encryption, AutocryptState.MUTUAL) do { let autoKeyIds = try pgp.importKeys(key: autocrypt.key, pw: nil, isSecretKey: false, autocrypt: true) if autoKeyIds.count > 0, let autoKeyId = autoKeyIds.first { @@ -126,7 +126,7 @@ class AutocryptTest: XCTestCase { if let parser = MCOMessageParser(data: mailData) { let autocrypt = Autocrypt.init(header: parser.header) XCTAssertEqual(autocrypt.addr, "alice@autocrypt.example") - XCTAssertEqual(autocrypt.prefer_encryption, EncState.MUTUAL) + XCTAssertEqual(autocrypt.prefer_encryption, AutocryptState.MUTUAL) do { let autoKeyIds = try pgp.importKeys(key: autocrypt.key, pw: nil, isSecretKey: false, autocrypt: true) XCTAssertEqual(autoKeyIds.first, "71DBC5657FDE65A7") diff --git a/enzevalos_iphoneTests/CoreMailTest.swift b/enzevalos_iphoneTests/CoreMailTest.swift index ff4f72a6144129f7fd1661eb6a6cdf5b37368e67..7bf56ef927101c08664957775cd435d0e54ea725 100644 --- a/enzevalos_iphoneTests/CoreMailTest.swift +++ b/enzevalos_iphoneTests/CoreMailTest.swift @@ -8,6 +8,12 @@ import XCTest +/** + TODO: + Test attachments + Test keys stuff. Update keys etc. + */ + @testable import enzevalos_iphone class CoreMailTest: XCTestCase { @@ -48,7 +54,8 @@ class CoreMailTest: XCTestCase { let importExpectation = expectation(description: "Import new msg!") - let m1 = MailProperties(messageID: "1", subject: "MSG1", date: Date(), flags: 0, from: addr1, folder: folder, signatureState: 0, encryptionState: 0) + + let m1 = MailProperties(messageID: "1", subject: "MSG1", date: Date(), flags: 0, from: addr1, folder: folder, signatureState: 0, encryptionState: 0, attachedPublicKeys: [], attachedSecretKeys: []) let m2 = MailProperties(messageID: "2", subject: "MSG2", date: Date(), flags: 0, from: addr1, folder: folder, signatureState: 0, encryptionState: 0) provider.importMails(from: [m1,m2], completionHandler: {error in @@ -103,7 +110,7 @@ class CoreMailTest: XCTestCase { } XCTAssertEqual(frc.fetchedObjects?.count ?? 0, 1, "Missing message! \(frc.fetchedObjects?.count ?? 0)") if let obj = frc.fetchedObjects?.first, let subject = obj.subject { - XCTAssert(subject == "Second MSG1" || subject == "MSG1", "Wired subject: \(subject)") + XCTAssert(subject == "MSG1", "Wired subject: \(subject)")// || subject == "MSG1", "Wired subject: \(subject)") } else { XCTFail("No messages!") } diff --git a/enzevalos_iphoneTests/GeneratedMocks.swift b/enzevalos_iphoneTests/GeneratedMocks.swift index 0d92cc8b9e27ffb4369a4399649f5c7d77ffa444..61950f1f559a8741feb36ba98e69e79f30226724 100644 --- a/enzevalos_iphoneTests/GeneratedMocks.swift +++ b/enzevalos_iphoneTests/GeneratedMocks.swift @@ -1,4 +1,4 @@ -// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationModel.swift at 2020-10-05 16:31:02 +0000 +// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationModel.swift at 2020-10-11 13:07:20 +0000 // // AuthenticationModel.swift @@ -654,7 +654,7 @@ import Foundation } -// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationViewModel.swift at 2020-10-05 16:31:02 +0000 +// MARK: - Mocks generated from file: enzevalos_iphone/AuthenticationViewModel.swift at 2020-10-11 13:07:20 +0000 // // AuthenticationViewModel.swift