From 23716b46788685efd583fda17d21cee67f05c22f Mon Sep 17 00:00:00 2001
From: lazarog98 <lazarog98@ninlil.local>
Date: Tue, 31 Mar 2020 17:07:14 +0200
Subject: [PATCH] #232 finalize enc/dec test

---
 enzevalos_iphone/SMIME.swift           | 79 ++++++++++++++++++++++++--
 enzevalos_iphoneTests/SMIMETests.swift | 33 +++++++++++
 2 files changed, 107 insertions(+), 5 deletions(-)
 create mode 100644 enzevalos_iphoneTests/SMIMETests.swift

diff --git a/enzevalos_iphone/SMIME.swift b/enzevalos_iphone/SMIME.swift
index 6290636e..e097d066 100644
--- a/enzevalos_iphone/SMIME.swift
+++ b/enzevalos_iphone/SMIME.swift
@@ -375,12 +375,19 @@ PkfA6mR7rtcyIbHi34tfkCv/qolV3QivMHov0IJpRyNO
         return fingerprints
     }
     
+    // TODO: importCertsForAddress
     func importCerts(certs: [String]) -> [String] {
         return importInKeychain(certs: certs.map({ (cert: String) -> String in
             return cert.extractPattern(pattern: certificatePattern).joined(separator: "\r\n")
         }), keychain: certsKeychain)
     }
     
+    func importCertForAddress(cert: String, addr: String) -> String {
+        let fp = importCerts(certs: [cert])[0]
+        certsKeychain[addr] = fp
+        return fp
+    }
+    
     func importCA(certs: [String]) -> [String] {
         return importInKeychain(certs: certs.map({ (cert: String) -> String in
             return cert.extractPattern(pattern: certificatePattern).joined(separator: "\r\n")
@@ -638,8 +645,70 @@ PkfA6mR7rtcyIbHi34tfkCv/qolV3QivMHov0IJpRyNO
     }
     
     func testCryptoObjectMethods() {
+        let certi = Certificate(pem: test_key)
+        let othercerti = Certificate(pem: test_key_other)
+        let ourAddr = certi.eMails![0]
+        let otherAddr = othercerti.eMails![0]
+        
+        let fp = addPrivateKey(keyPlusCertPEM: test_key)
+        let _ = importCertForAddress(cert: test_key, addr: ourAddr)
+
+        
+        let _ = importCertForAddress(cert: test_key_other, addr: otherAddr)
+        let cert = certsKeychain[privateKeyKeychain["ownkey"]!]!
+        let key = privateKeyKeychain[privateKeyKeychain["ownkey"]!]!
+        
+        let testData = test_string.data(using: .utf8)!
         
+        var encCryptObj: CryptoObject? = nil
+        do {
+            encCryptObj = try encrypt(plainData: testData, ids: [otherAddr], ownAddr: ourAddr)
+        } catch let error {
+            if error is SMIMEError {
+                let smimeError = error as! SMIMEError
+                print(smimeError.message ?? "No error message")
+                print("SWIFT encrypt errors")
+                for x in smimeError.errorArray ?? [] {
+                    print("error code: ", x)
+                    print("error string: ", getErrorString(errCode: x))
+                    print("error reason: ", getErrorReasonString(errCode: x))
+                }
+            }
+        }
+        
+        if let obj = encCryptObj {
+            print("\n\nNew crypto object")
+            print("decrypted text: ", obj.decryptedText ?? "")
+            print("sign key: ", obj.signedKeys ?? "")
+            print("sign addr: ", obj.signedAdrs)
+            print("enc type:", obj.encType)
+            print("cipther string: ", obj.chiperString ?? "")
+            var decObj : CryptoObject? = nil
+            do {
+                decObj = try decrypt(data: (encCryptObj?.chiphertext)!, fromAddr: ourAddr, ownId: ourAddr, isMailNew: false)
+            }catch let error {
+            if error is SMIMEError {
+                let smimeError = error as! SMIMEError
+                print(smimeError.message ?? "No error message")
+                print("SWIFT decrypt errors")
+                for x in smimeError.errorArray ?? [] {
+                    print("error code: ", x)
+                    print("error string: ", getErrorString(errCode: x))
+                    print("error reason: ", getErrorReasonString(errCode: x))
+                }
+            }
+        }
+        if let obj = decObj {
+            print("\n\nDEC crypto object")
+            print("decrypted text: ", obj.decryptedText ?? "")
+            print("signed key: ", obj.signedKeys )
+            print("sign addr: ", obj.signedAdrs)
+            print("enc type:", obj.encType)
+            print ("sign: ", obj.signatureState)
+            print("cipther string: ", obj.chiperString ?? "")
+        }
     }
+}
     
     func testKeyEnc() {
         let (pKeyEnc, errArrEnc) = getEncryptedPemFromPKey(pem: test_key, passwd: "testpwd")
@@ -711,13 +780,14 @@ PkfA6mR7rtcyIbHi34tfkCv/qolV3QivMHov0IJpRyNO
         
         let verifyCryptoObj = verify(data: outputData, email: fromAddr, isMailNew: isMailNew)
         
+        let signedKeys = verifyCryptoObj.signedKeys
         let signKey = verifyCryptoObj.signKey
         let sigState = verifyCryptoObj.signatureState
         let addresses = verifyCryptoObj.signedAdrs
         let plainText = verifyCryptoObj.plaintext
         let decryptedData = plainText!.data(using: .utf8)!
         
-        return CryptoObject(chiphertext: data, plaintext: plainText, decryptedData: decryptedData, sigState: sigState, encState: encState, signKey: signKey, encType: CryptoScheme.SMIME, signedAdrs: sigState == SignatureState.ValidSignature ? addresses : [])
+        return CryptoObject(chiphertext: data, plaintext: plainText, decryptedData: decryptedData, sigState: sigState, encState: encState, signKey: signKey, encType: CryptoScheme.SMIME, signedAdrs: addresses, signedKeys: signedKeys)
     }
     
     func sign(plainData: Data, myEmail: String, detached: Bool = true) throws -> CryptoObject {
@@ -817,11 +887,10 @@ PkfA6mR7rtcyIbHi34tfkCv/qolV3QivMHov0IJpRyNO
             return fp
         })
     
-        // TODO: set signKey to the entire array after CryptoObject has been modified to use multiple keys
-        return CryptoObject(chiphertext: data, plaintext: verStr!, decryptedData: nil, sigState: sigState, encState: EncryptionState.NoEncryption, signKey: signKeyFps[0], encType: .SMIME, signedAdrs: signedAddresses)
+        return CryptoObject(chiphertext: data, plaintext: verStr!, decryptedData: nil, sigState: sigState, encState: EncryptionState.NoEncryption, signKey: nil, encType: .SMIME, signedAdrs: signedAddresses, signedKeys: signKeyFps)
     }
     
-    func encrypt(plainData: Data, ids: [String], ownId: String, encryptForMyId: Bool = true) throws -> CryptoObject {
+    func encrypt(plainData: Data, ids: [String], ownAddr: String, encryptForMyId: Bool = true) throws -> CryptoObject {
         let plainText = String(data: plainData, encoding: .utf8)
         var (sigText, sigErrArr): (String?, [UInt]?) = (nil, nil)
         var pems: [String] = []
@@ -867,7 +936,7 @@ PkfA6mR7rtcyIbHi34tfkCv/qolV3QivMHov0IJpRyNO
             throw SMIMEError(message: "Encryption failed!", errorArray: errArr, type: SMIMEError.ErrorType.encryption)
         }
         
-        return CryptoObject(chiphertext: encStr!.data(using: .utf8), plaintext: plainText, decryptedData: plainData, sigState: SignatureState.ValidSignature, encState: EncryptionState.ValidedEncryptedWithCurrentKey, signKey: ownFp, encType: CryptoScheme.SMIME, signedAdrs: [ownId])
+        return CryptoObject(chiphertext: encStr!.data(using: .utf8), plaintext: plainText, decryptedData: plainData, sigState: SignatureState.ValidSignature, encState: EncryptionState.ValidedEncryptedWithCurrentKey, signKey: ownFp, encType: CryptoScheme.SMIME, signedAdrs: [ownAddr])
     }
 }
 
diff --git a/enzevalos_iphoneTests/SMIMETests.swift b/enzevalos_iphoneTests/SMIMETests.swift
new file mode 100644
index 00000000..5553ee2e
--- /dev/null
+++ b/enzevalos_iphoneTests/SMIMETests.swift
@@ -0,0 +1,33 @@
+//
+//  SMIMETests.swift
+//  enzevalos_iphoneTests
+//
+//  Created by lazarog98 on 31.03.20.
+//  Copyright © 2020 fu-berlin. All rights reserved.
+//
+
+import XCTest
+
+class SMIMETests: XCTestCase {
+
+    override func setUp() {
+        // Put setup code here. This method is called before the invocation of each test method in the class.
+    }
+
+    override func tearDown() {
+        // Put teardown code here. This method is called after the invocation of each test method in the class.
+    }
+
+    func testExample() {
+        // This is an example of a functional test case.
+        // Use XCTAssert and related functions to verify your tests produce the correct results.
+    }
+
+    func testPerformanceExample() {
+        // This is an example of a performance test case.
+        self.measure {
+            // Put the code you want to measure the time of here.
+        }
+    }
+
+}
-- 
GitLab