From 1b481302a8cdb71bf66958841cb0839331ccc673 Mon Sep 17 00:00:00 2001 From: Roma Sosnovsky Date: Tue, 5 Sep 2023 11:05:05 +0300 Subject: [PATCH] Remove SwiftyRSA and IDZSwiftCommonCrypto libs (#2346) * remove SwiftyRSA and IDZSwiftCommonCrypto * wip * wip --- FlowCrypt.xcodeproj/project.pbxproj | 53 ---- .../xcshareddata/swiftpm/Package.resolved | 29 +-- FlowCrypt/Core/RSA/Asn1Parser.swift | 220 ---------------- FlowCrypt/Core/RSA/Key.swift | 90 ------- FlowCrypt/Core/RSA/PrivateKey.swift | 65 ----- FlowCrypt/Core/RSA/PublicKey.swift | 76 ------ FlowCrypt/Core/RSA/RSAMessage.swift | 35 --- FlowCrypt/Core/RSA/SwiftyRSA.swift | 238 ------------------ FlowCrypt/Core/RSA/SwiftyRSAError.swift | 88 ------- .../WkdUrlConstructor.swift | 5 +- .../StorageEncryptionKeyProvider.swift | 1 - .../WKDURLs/ZBase32EncodingTests.swift | 4 +- 12 files changed, 16 insertions(+), 888 deletions(-) delete mode 100644 FlowCrypt/Core/RSA/Asn1Parser.swift delete mode 100644 FlowCrypt/Core/RSA/Key.swift delete mode 100644 FlowCrypt/Core/RSA/PrivateKey.swift delete mode 100644 FlowCrypt/Core/RSA/PublicKey.swift delete mode 100644 FlowCrypt/Core/RSA/RSAMessage.swift delete mode 100644 FlowCrypt/Core/RSA/SwiftyRSA.swift delete mode 100644 FlowCrypt/Core/RSA/SwiftyRSAError.swift diff --git a/FlowCrypt.xcodeproj/project.pbxproj b/FlowCrypt.xcodeproj/project.pbxproj index cd7ab3f89..84ee74944 100644 --- a/FlowCrypt.xcodeproj/project.pbxproj +++ b/FlowCrypt.xcodeproj/project.pbxproj @@ -94,13 +94,6 @@ 5133B6742716E5EA00C95463 /* LabelCellNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5133B6732716E5EA00C95463 /* LabelCellNode.swift */; }; 5137CB1427F8E0A900AEF895 /* EnterpriseServerApiHelper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5137CB1327F8E0A900AEF895 /* EnterpriseServerApiHelper.swift */; }; 513DC088284F161400FAD5F2 /* EmailKeyManagerApi.swift in Sources */ = {isa = PBXBuildFile; fileRef = 513DC087284F161400FAD5F2 /* EmailKeyManagerApi.swift */; }; - 51433AB82902A6EB00E9D488 /* PublicKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51433AA82902A6EA00E9D488 /* PublicKey.swift */; }; - 51433ABB2902A6EB00E9D488 /* Asn1Parser.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51433AAB2902A6EB00E9D488 /* Asn1Parser.swift */; }; - 51433ABC2902A6EB00E9D488 /* SwiftyRSA.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51433AAC2902A6EB00E9D488 /* SwiftyRSA.swift */; }; - 51433ABF2902A6EB00E9D488 /* Key.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51433AAF2902A6EB00E9D488 /* Key.swift */; }; - 51433AC12902A6EB00E9D488 /* PrivateKey.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51433AB12902A6EB00E9D488 /* PrivateKey.swift */; }; - 51433AC32902A6EB00E9D488 /* RSAMessage.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51433AB32902A6EB00E9D488 /* RSAMessage.swift */; }; - 51433AC52902A6EB00E9D488 /* SwiftyRSAError.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51433AB62902A6EB00E9D488 /* SwiftyRSAError.swift */; }; 514C34DB276CE19C00FCAB79 /* ComposeMessageContext.swift in Sources */ = {isa = PBXBuildFile; fileRef = 514C34DA276CE19C00FCAB79 /* ComposeMessageContext.swift */; }; 514C34DD276CE1C000FCAB79 /* ComposeMessageRecipient.swift in Sources */ = {isa = PBXBuildFile; fileRef = 514C34DC276CE1C000FCAB79 /* ComposeMessageRecipient.swift */; }; 514C34DF276CE20700FCAB79 /* ComposeMessageHelper+State.swift in Sources */ = {isa = PBXBuildFile; fileRef = 514C34DE276CE20700FCAB79 /* ComposeMessageHelper+State.swift */; }; @@ -137,7 +130,6 @@ 51DAD9BD273E7DD20076CBA7 /* BadgeNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DAD9BC273E7DD20076CBA7 /* BadgeNode.swift */; }; 51DDA24E28044F0700EE13A8 /* CheckMailAuthViewDecorator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DDA24D28044F0700EE13A8 /* CheckMailAuthViewDecorator.swift */; }; 51DE2FEE2714DA0400916222 /* ContactKeyCellNode.swift in Sources */ = {isa = PBXBuildFile; fileRef = 51DE2FED2714DA0400916222 /* ContactKeyCellNode.swift */; }; - 51E13F12270F92BA00F287CA /* IDZSwiftCommonCrypto in Frameworks */ = {isa = PBXBuildFile; productRef = 51E13F11270F92BA00F287CA /* IDZSwiftCommonCrypto */; }; 51E1673D270DAFF900D27C52 /* Localizable.stringsdict in Resources */ = {isa = PBXBuildFile; fileRef = 51E1673C270DAFF900D27C52 /* Localizable.stringsdict */; }; 51E16746270F301F00D27C52 /* GoogleAPIClientForREST_Gmail in Frameworks */ = {isa = PBXBuildFile; productRef = 51E16745270F301F00D27C52 /* GoogleAPIClientForREST_Gmail */; }; 51E16749270F303100D27C52 /* GoogleSignIn in Frameworks */ = {isa = PBXBuildFile; productRef = 51E16748270F303100D27C52 /* GoogleSignIn */; }; @@ -565,13 +557,6 @@ 5133B6732716E5EA00C95463 /* LabelCellNode.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LabelCellNode.swift; sourceTree = ""; }; 5137CB1327F8E0A900AEF895 /* EnterpriseServerApiHelper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EnterpriseServerApiHelper.swift; sourceTree = ""; }; 513DC087284F161400FAD5F2 /* EmailKeyManagerApi.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmailKeyManagerApi.swift; sourceTree = ""; }; - 51433AA82902A6EA00E9D488 /* PublicKey.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PublicKey.swift; sourceTree = ""; }; - 51433AAB2902A6EB00E9D488 /* Asn1Parser.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Asn1Parser.swift; sourceTree = ""; }; - 51433AAC2902A6EB00E9D488 /* SwiftyRSA.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyRSA.swift; sourceTree = ""; }; - 51433AAF2902A6EB00E9D488 /* Key.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Key.swift; sourceTree = ""; }; - 51433AB12902A6EB00E9D488 /* PrivateKey.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = PrivateKey.swift; sourceTree = ""; }; - 51433AB32902A6EB00E9D488 /* RSAMessage.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RSAMessage.swift; sourceTree = ""; }; - 51433AB62902A6EB00E9D488 /* SwiftyRSAError.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SwiftyRSAError.swift; sourceTree = ""; }; 514C34DA276CE19C00FCAB79 /* ComposeMessageContext.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeMessageContext.swift; sourceTree = ""; }; 514C34DC276CE1C000FCAB79 /* ComposeMessageRecipient.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComposeMessageRecipient.swift; sourceTree = ""; }; 514C34DE276CE20700FCAB79 /* ComposeMessageHelper+State.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ComposeMessageHelper+State.swift"; sourceTree = ""; }; @@ -899,7 +884,6 @@ 51A1A12829070CDF007F1188 /* AsyncDisplayKit in Frameworks */, 51E1675D270F36A400D27C52 /* Realm in Frameworks */, 51C0C1EF271982A1000C9738 /* MailCore in Frameworks */, - 51E13F12270F92BA00F287CA /* IDZSwiftCommonCrypto in Frameworks */, 040FDF0E27EDA70700CB936A /* GoogleAPIClientForREST_Oauth2 in Frameworks */, 51E1675F270F36A400D27C52 /* RealmSwift in Frameworks */, 512C1414271077F8002DE13F /* GoogleAPIClientForREST_PeopleService in Frameworks */, @@ -1115,7 +1099,6 @@ isa = PBXGroup; children = ( D212D36224C1AC1900035991 /* Models */, - 51433AA52902A6C200E9D488 /* RSA */, 32DCAC732B988D9704658812 /* CoreTypes.swift */, 32DCA0C3D34A69851A238E87 /* Core.swift */, ); @@ -1177,20 +1160,6 @@ path = "Remote Private Key Apis"; sourceTree = ""; }; - 51433AA52902A6C200E9D488 /* RSA */ = { - isa = PBXGroup; - children = ( - 51433AAB2902A6EB00E9D488 /* Asn1Parser.swift */, - 51433AAF2902A6EB00E9D488 /* Key.swift */, - 51433AB32902A6EB00E9D488 /* RSAMessage.swift */, - 51433AB12902A6EB00E9D488 /* PrivateKey.swift */, - 51433AA82902A6EA00E9D488 /* PublicKey.swift */, - 51433AAC2902A6EB00E9D488 /* SwiftyRSA.swift */, - 51433AB62902A6EB00E9D488 /* SwiftyRSAError.swift */, - ); - path = RSA; - sourceTree = ""; - }; 51540F702950ED4D00DCDC40 /* Helpers */ = { isa = PBXGroup; children = ( @@ -2377,7 +2346,6 @@ 51E16748270F303100D27C52 /* GoogleSignIn */, 51E1675C270F36A400D27C52 /* Realm */, 51E1675E270F36A400D27C52 /* RealmSwift */, - 51E13F11270F92BA00F287CA /* IDZSwiftCommonCrypto */, 512C1413271077F8002DE13F /* GoogleAPIClientForREST_PeopleService */, 51C0C1EE271982A1000C9738 /* MailCore */, 040FDF0D27EDA70700CB936A /* GoogleAPIClientForREST_Oauth2 */, @@ -2492,7 +2460,6 @@ 51E16744270F301F00D27C52 /* XCRemoteSwiftPackageReference "google-api-objectivec-client-for-rest" */, 51E16747270F303100D27C52 /* XCRemoteSwiftPackageReference "GoogleSignIn-iOS" */, 51E1675B270F36A400D27C52 /* XCRemoteSwiftPackageReference "realm-cocoa" */, - 51E13F10270F92BA00F287CA /* XCRemoteSwiftPackageReference "IDZSwiftCommonCrypto" */, 51C0C1ED271982A1000C9738 /* XCRemoteSwiftPackageReference "mailcore2" */, 51C0C1F0271987DB000C9738 /* XCRemoteSwiftPackageReference "Toast-Swift" */, 51A1A12629070CDF007F1188 /* XCRemoteSwiftPackageReference "Texture" */, @@ -2630,7 +2597,6 @@ 04B472951ECE29F600B8266F /* MyMenuViewController.swift in Sources */, 51B4AE5327144E590001F33B /* PubKey.swift in Sources */, 21623D1826FA860700A11B9A /* PhotosManager.swift in Sources */, - 51433ABF2902A6EB00E9D488 /* Key.swift in Sources */, 049E605927FDB6310089EE2A /* ComposeViewController+Attachment.swift in Sources */, C132B9B41EC2DBD800763715 /* AppDelegate.swift in Sources */, 9F65B0B7277DF2AD005CD19C /* Imap+ThreadOperations.swift in Sources */, @@ -2693,7 +2659,6 @@ 2C124DB42728809100A2EFA6 /* ApiCall.swift in Sources */, 9F7E8F19269C538E0021C07F /* NavigationChildController.swift in Sources */, 9FB22CF025715D960026EE64 /* BackupsManagerError.swift in Sources */, - 51433AB82902A6EB00E9D488 /* PublicKey.swift in Sources */, 9FBEAE5525D41BFF009E98D4 /* UserMailSessionProvider.swift in Sources */, D212D36424C1AC4800035991 /* KeyId.swift in Sources */, D27B911924EFE79F002DF0A1 /* LocalContactsProvider.swift in Sources */, @@ -2711,7 +2676,6 @@ 512C4B9929114F7A00AAE52B /* ThreadDetailsViewController+TableView.swift in Sources */, 5A39F437239ECC23001F4607 /* KeySettingsViewController.swift in Sources */, 9FF0673325520DE400FCC9E6 /* GmailService+send.swift in Sources */, - 51433AC52902A6EB00E9D488 /* SwiftyRSAError.swift in Sources */, D20D3C892520B67C00D4AA9A /* BackupOptionsViewController.swift in Sources */, 2C2D0B95275FDF6B0052771D /* Version6SchemaMigration.swift in Sources */, 21750D7D26C6AFA6007E6A6F /* SetupEKMKeyViewController.swift in Sources */, @@ -2774,7 +2738,6 @@ D212D36024C1AC0D00035991 /* KeyDetails.swift in Sources */, 9F9AAFFD2383E216000A00F1 /* Document.swift in Sources */, 51DDA24E28044F0700EE13A8 /* CheckMailAuthViewDecorator.swift in Sources */, - 51433AC12902A6EB00E9D488 /* PrivateKey.swift in Sources */, 9FE1B3A02565B0CE00D6D086 /* Message.swift in Sources */, D27177492424D73000BDA9A9 /* InboxViewDecorator.swift in Sources */, D2F41371243CC76F0066AFB5 /* SessionRealmObject.swift in Sources */, @@ -2789,7 +2752,6 @@ 9F0C3C102316DD5B00299985 /* GoogleAuthManager.swift in Sources */, D24F4C2223E2359B00C5EEE4 /* BootstrapViewController.swift in Sources */, 21750D7F26C6C1E3007E6A6F /* SetupCreatePassphraseAbstractViewController.swift in Sources */, - 51433ABB2902A6EB00E9D488 /* Asn1Parser.swift in Sources */, 516481B5294112A400A96095 /* IdToken.swift in Sources */, 9FC7EBAA266EBD3700F3BF5D /* InMemoryPassPhraseStorage.swift in Sources */, 9FA9C83C264C2D75005A9670 /* MessageHelper.swift in Sources */, @@ -2821,7 +2783,6 @@ 9F5C2A8B257E6C4900DE9B4B /* ImapError.swift in Sources */, 9FAFD7592713870800321FA4 /* InboxViewController+State.swift in Sources */, 32DCA594BD65DE3AF94569F3 /* ComposeViewController.swift in Sources */, - 51433ABC2902A6EB00E9D488 /* SwiftyRSA.swift in Sources */, 9FC411352595EA94001180A8 /* Imap+Search.swift in Sources */, 9FC4114C25961CEA001180A8 /* MailServiceProviderType.swift in Sources */, 049E606727FDBAEF0089EE2A /* ComposeViewController+RecipientInput.swift in Sources */, @@ -2857,7 +2818,6 @@ 040FDF1227EDFC5C00CB936A /* IdTokenUtils.swift in Sources */, 9F953E09238310D500AEB98B /* KeyMethods.swift in Sources */, 5137CB1427F8E0A900AEF895 /* EnterpriseServerApiHelper.swift in Sources */, - 51433AC32902A6EB00E9D488 /* RSAMessage.swift in Sources */, 32DCA00224982EDA88D69C6E /* AppErr.swift in Sources */, 9F6EE17B2598F9FA0059BA51 /* Gmail+Backup.swift in Sources */, ); @@ -3864,14 +3824,6 @@ minimumVersion = 5.0.0; }; }; - 51E13F10270F92BA00F287CA /* XCRemoteSwiftPackageReference "IDZSwiftCommonCrypto" */ = { - isa = XCRemoteSwiftPackageReference; - repositoryURL = "https://github.com/iosdevzone/IDZSwiftCommonCrypto"; - requirement = { - kind = upToNextMajorVersion; - minimumVersion = 0.13.0; - }; - }; 51E16744270F301F00D27C52 /* XCRemoteSwiftPackageReference "google-api-objectivec-client-for-rest" */ = { isa = XCRemoteSwiftPackageReference; repositoryURL = "https://github.com/google/google-api-objectivec-client-for-rest"; @@ -3943,11 +3895,6 @@ package = 51C0C1ED271982A1000C9738 /* XCRemoteSwiftPackageReference "mailcore2" */; productName = MailCore; }; - 51E13F11270F92BA00F287CA /* IDZSwiftCommonCrypto */ = { - isa = XCSwiftPackageProductDependency; - package = 51E13F10270F92BA00F287CA /* XCRemoteSwiftPackageReference "IDZSwiftCommonCrypto" */; - productName = IDZSwiftCommonCrypto; - }; 51E16745270F301F00D27C52 /* GoogleAPIClientForREST_Gmail */ = { isa = XCSwiftPackageProductDependency; package = 51E16744270F301F00D27C52 /* XCRemoteSwiftPackageReference "google-api-objectivec-client-for-rest" */; diff --git a/FlowCrypt.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved b/FlowCrypt.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved index 1d28b50d2..cc53dafa0 100644 --- a/FlowCrypt.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved +++ b/FlowCrypt.xcodeproj/project.xcworkspace/xcshareddata/swiftpm/Package.resolved @@ -50,8 +50,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/google/gtm-session-fetcher.git", "state" : { - "revision" : "5ccda3981422a84186387dbb763ba739178b529c", - "version" : "2.3.0" + "revision" : "d415594121c9e8a4f9d79cecee0965cf35e74dbd", + "version" : "3.1.1" } }, { @@ -59,17 +59,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/google/GTMAppAuth.git", "state" : { - "revision" : "6dee0cde8a1b223737a5159e55e6b4ec16bbbdd9", - "version" : "1.3.1" - } - }, - { - "identity" : "idzswiftcommoncrypto", - "kind" : "remoteSourceControl", - "location" : "https://github.com/iosdevzone/IDZSwiftCommonCrypto", - "state" : { - "revision" : "d824371e670bd57eb456bbc41139b4997f7207b8", - "version" : "0.13.1" + "revision" : "cee3c709307912d040bd1e06ca919875a92339c6", + "version" : "2.0.0" } }, { @@ -104,8 +95,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/realm/realm-cocoa", "state" : { - "revision" : "b287dc102036ff425bd8a88483f0a5596871f05e", - "version" : "10.41.0" + "revision" : "330a239712af77a3b0926b9ffa9582302a0b9923", + "version" : "10.42.1" } }, { @@ -113,8 +104,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/realm/realm-core.git", "state" : { - "revision" : "f1434caadda443b4ed2261b91ea4f43ab1ee2aa5", - "version" : "13.15.1" + "revision" : "c04f5e401a1ec682e6b08b1ee157e19a0f834a5f", + "version" : "13.17.1" } }, { @@ -131,8 +122,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-argument-parser.git", "state" : { - "revision" : "fee6933f37fde9a5e12a1e4aeaa93fe60116ff2a", - "version" : "1.2.2" + "revision" : "8f4d2753f0e4778c76d5f05ad16c74f707390531", + "version" : "1.2.3" } }, { diff --git a/FlowCrypt/Core/RSA/Asn1Parser.swift b/FlowCrypt/Core/RSA/Asn1Parser.swift deleted file mode 100644 index 01663d73e..000000000 --- a/FlowCrypt/Core/RSA/Asn1Parser.swift +++ /dev/null @@ -1,220 +0,0 @@ -// -// Asn1Parser.swift -// SwiftyRSA -// -// Created by Lois Di Qual on 5/9/17. -// Copyright © 2017 Scoop. All rights reserved. -// - -import Foundation - -/// Simple data scanner that consumes bytes from a raw data and keeps an updated position. -private class Scanner { - - enum ScannerError: Error { - case outOfBounds - } - - let data: Data - var index: Int = 0 - - /// Returns whether there is no more data to consume - var isComplete: Bool { - return index >= data.count - } - - /// Creates a scanner with provided data - /// - /// - Parameter data: Data to consume - init(data: Data) { - self.data = data - } - - /// Consumes data of provided length and returns it - /// - /// - Parameter length: length of the data to consume - /// - Returns: data consumed - /// - Throws: ScannerError.outOfBounds error if asked to consume too many bytes - func consume(length: Int) throws -> Data { - - guard length > 0 else { - return Data() - } - - guard index + length <= data.count else { - throw ScannerError.outOfBounds - } - - let subdata = data.subdata(in: index ..< index + length) - index += length - return subdata - } - - /// Consumes a primitive, definite ASN1 length and returns its value. - /// - /// See http://luca.ntop.org/Teaching/Appunti/asn1.html, - /// - /// - Short form. One octet. Bit 8 has value "0" and bits 7-1 give the length. - /// - Long form. Two to 127 octets. Bit 8 of first octet has value "1" and - /// bits 7-1 give the number of additional length octets. - /// Second and following octets give the length, base 256, most significant digit first. - /// - /// - Returns: Length that was consumed - /// - Throws: ScannerError.outOfBounds error if asked to consume too many bytes - func consumeLength() throws -> Int { - - let lengthByte = try consume(length: 1).firstByte - - // If the first byte's value is less than 0x80, it directly contains the length - // so we can return it - guard lengthByte >= 0x80 else { - return Int(lengthByte) - } - - // If the first byte's value is more than 0x80, it indicates how many following bytes - // will describe the length. For instance, 0x85 indicates that 0x85 - 0x80 = 0x05 = 5 - // bytes will describe the length, so we need to read the 5 next bytes and get their integer - // value to determine the length. - let nextByteCount = lengthByte - 0x80 - let length = try consume(length: Int(nextByteCount)) - - return length.integer - } -} - -private extension Data { - - /// Returns the first byte of the current data - var firstByte: UInt8 { - var byte: UInt8 = 0 - copyBytes(to: &byte, count: MemoryLayout.size) - return byte - } - - /// Returns the integer value of the current data. - /// @warning: this only supports data up to 4 bytes, as we can only extract 32-bit integers. - var integer: Int { - - guard !isEmpty else { - return 0 - } - - var int: UInt32 = 0 - var offset = Int32(count - 1) - forEach { byte in - let byte32 = UInt32(byte) - let shifted = byte32 << (UInt32(offset) * 8) - int = int | shifted - offset -= 1 - } - - return Int(int) - } -} - -/// A simple ASN1 parser that will recursively iterate over a root node and return a Node tree. -/// The root node can be any of the supported nodes described in `Node`. If the parser encounters a sequence -/// it will recursively parse its children. -enum Asn1Parser { - - /// An ASN1 node - enum Node { - case sequence(nodes: [Node]) - case integer(data: Data) - case objectIdentifier(data: Data) - case null - case bitString(data: Data) - case octetString(data: Data) - } - - enum ParserError: Error { - case noType - case invalidType(value: UInt8) - } - - /// Parses ASN1 data and returns its root node. - /// - /// - Parameter data: ASN1 data to parse - /// - Returns: Root ASN1 Node - /// - Throws: A ParserError if anything goes wrong, or if an unknown node was encountered - static func parse(data: Data) throws -> Node { - let scanner = Scanner(data: data) - let node = try parseNode(scanner: scanner) - return node - } - - /// Parses an ASN1 given an existing scanne. - /// @warning: this will modify the state (ie: position) of the provided scanner. - /// - /// - Parameter scanner: Scanner to use to consume the data - /// - Returns: Parsed node - /// - Throws: A ParserError if anything goes wrong, or if an unknown node was encountered - private static func parseNode(scanner: Scanner) throws -> Node { - - let firstByte = try scanner.consume(length: 1).firstByte - - // Sequence - if firstByte == 0x30 { - let length = try scanner.consumeLength() - let data = try scanner.consume(length: length) - let nodes = try parseSequence(data: data) - return .sequence(nodes: nodes) - } - - // Integer - if firstByte == 0x02 { - let length = try scanner.consumeLength() - let data = try scanner.consume(length: length) - return .integer(data: data) - } - - // Object identifier - if firstByte == 0x06 { - let length = try scanner.consumeLength() - let data = try scanner.consume(length: length) - return .objectIdentifier(data: data) - } - - // Null - if firstByte == 0x05 { - _ = try scanner.consume(length: 1) - return .null - } - - // Bit String - if firstByte == 0x03 { - let length = try scanner.consumeLength() - - // There's an extra byte (0x00) after the bit string length in all the keys I've encountered. - // I couldn't find a specification that referenced this extra byte, but let's consume it and discard it. - _ = try scanner.consume(length: 1) - - let data = try scanner.consume(length: length - 1) - return .bitString(data: data) - } - - // Octet String - if firstByte == 0x04 { - let length = try scanner.consumeLength() - let data = try scanner.consume(length: length) - return .octetString(data: data) - } - - throw ParserError.invalidType(value: firstByte) - } - - /// Parses an ASN1 sequence and returns its child nodes - /// - /// - Parameter data: ASN1 data - /// - Returns: A list of ASN1 nodes - /// - Throws: A ParserError if anything goes wrong, or if an unknown node was encountered - private static func parseSequence(data: Data) throws -> [Node] { - let scanner = Scanner(data: data) - var nodes: [Node] = [] - while !scanner.isComplete { - let node = try parseNode(scanner: scanner) - nodes.append(node) - } - return nodes - } -} diff --git a/FlowCrypt/Core/RSA/Key.swift b/FlowCrypt/Core/RSA/Key.swift deleted file mode 100644 index cc4a8a4c9..000000000 --- a/FlowCrypt/Core/RSA/Key.swift +++ /dev/null @@ -1,90 +0,0 @@ -// -// Key.swift -// SwiftyRSA -// -// Created by Loïs Di Qual on 9/19/16. -// Copyright © 2016 Scoop. All rights reserved. -// - -import Foundation -import Security - -public protocol Key: AnyObject { - - var reference: SecKey { get } - var originalData: Data? { get } - - init(data: Data) throws - init(reference: SecKey) throws - init(base64Encoded base64String: String) throws - init(pemEncoded pemString: String) throws - init(pemNamed pemName: String, in bundle: Bundle) throws - init(derNamed derName: String, in bundle: Bundle) throws - - func pemString() throws -> String - func data() throws -> Data - func base64String() throws -> String -} - -public extension Key { - - /// Returns a Base64 representation of the public key. - /// - /// - Returns: Data of the key, Base64-encoded - /// - Throws: SwiftyRSAError - func base64String() throws -> String { - return try data().base64EncodedString() - } - - func data() throws -> Data { - return try SwiftyRSA.data(forKeyReference: reference) - } - - /// Creates a public key with a base64-encoded string. - /// - /// - Parameter base64String: Base64-encoded public key data - /// - Throws: SwiftyRSAError - init(base64Encoded base64String: String) throws { - guard let data = Data(base64Encoded: base64String, options: [.ignoreUnknownCharacters]) else { - throw SwiftyRSAError.invalidBase64String - } - try self.init(data: data) - } - - /// Creates a public key with a PEM string. - /// - /// - Parameter pemString: PEM-encoded public key string - /// - Throws: SwiftyRSAError - init(pemEncoded pemString: String) throws { - let base64String = try SwiftyRSA.base64String(pemEncoded: pemString) - try self.init(base64Encoded: base64String) - } - - /// Creates a public key with a PEM file. - /// - /// - Parameters: - /// - pemName: Name of the PEM file - /// - bundle: Bundle in which to look for the PEM file. Defaults to the main bundle. - /// - Throws: SwiftyRSAError - init(pemNamed pemName: String, in bundle: Bundle = Bundle.main) throws { - guard let path = bundle.path(forResource: pemName, ofType: "pem") else { - throw SwiftyRSAError.pemFileNotFound(name: pemName) - } - let keyString = try String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8) - try self.init(pemEncoded: keyString) - } - - /// Creates a private key with a DER file. - /// - /// - Parameters: - /// - derName: Name of the DER file - /// - bundle: Bundle in which to look for the DER file. Defaults to the main bundle. - /// - Throws: SwiftyRSAError - init(derNamed derName: String, in bundle: Bundle = Bundle.main) throws { - guard let path = bundle.path(forResource: derName, ofType: "der") else { - throw SwiftyRSAError.derFileNotFound(name: derName) - } - let data = try Data(contentsOf: URL(fileURLWithPath: path)) - try self.init(data: data) - } -} diff --git a/FlowCrypt/Core/RSA/PrivateKey.swift b/FlowCrypt/Core/RSA/PrivateKey.swift deleted file mode 100644 index d3c109732..000000000 --- a/FlowCrypt/Core/RSA/PrivateKey.swift +++ /dev/null @@ -1,65 +0,0 @@ -// -// PrivateKey.swift -// SwiftyRSA -// -// Created by Lois Di Qual on 5/17/17. -// Copyright © 2017 Scoop. All rights reserved. -// - -import Foundation - -public class PrivateKey: Key { - - /// Reference to the key within the keychain - public let reference: SecKey - - /// Original data of the private key. - /// Note that it does not contain PEM headers and holds data as bytes, not as a base 64 string. - public let originalData: Data? - - let tag: String? - - /// Returns a PEM representation of the private key. - /// - /// - Returns: Data of the key, PEM-encoded - /// - Throws: SwiftyRSAError - public func pemString() throws -> String { - let data = try self.data() - let pem = SwiftyRSA.format(keyData: data, withPemType: "RSA PRIVATE KEY") - return pem - } - - /// Creates a private key with a keychain key reference. - /// This initializer will throw if the provided key reference is not a private RSA key. - /// - /// - Parameter reference: Reference to the key within the keychain. - /// - Throws: SwiftyRSAError - public required init(reference: SecKey) throws { - - guard SwiftyRSA.isValidKeyReference(reference, forClass: kSecAttrKeyClassPrivate) else { - throw SwiftyRSAError.notAPrivateKey - } - - self.reference = reference - self.tag = nil - self.originalData = nil - } - - /// Creates a private key with a RSA public key data. - /// - /// - Parameter data: Private key data - /// - Throws: SwiftyRSAError - public required init(data: Data) throws { - self.originalData = data - let tag = UUID().uuidString - self.tag = tag - let dataWithoutHeader = try SwiftyRSA.stripKeyHeader(keyData: data) - reference = try SwiftyRSA.addKey(dataWithoutHeader, isPublic: false, tag: tag) - } - - deinit { - if let tag { - SwiftyRSA.removeKey(tag: tag) - } - } -} diff --git a/FlowCrypt/Core/RSA/PublicKey.swift b/FlowCrypt/Core/RSA/PublicKey.swift deleted file mode 100644 index d0d7ece8f..000000000 --- a/FlowCrypt/Core/RSA/PublicKey.swift +++ /dev/null @@ -1,76 +0,0 @@ -// -// PublicKey.swift -// SwiftyRSA -// -// Created by Lois Di Qual on 5/17/17. -// Copyright © 2017 Scoop. All rights reserved. -// - -import Foundation - -public class PublicKey: Key { - - /// Reference to the key within the keychain - public let reference: SecKey - - /// Data of the public key as provided when creating the key. - /// Note that if the key was created from a base64string / DER string / PEM file / DER file, - /// the data holds the actual bytes of the key, not any textual representation like PEM headers - /// or base64 characters. - public let originalData: Data? - - let tag: String? // Only used on iOS 8/9 - - /// Returns a PEM representation of the public key. - /// - /// - Returns: Data of the key, PEM-encoded - /// - Throws: SwiftyRSAError - public func pemString() throws -> String { - let data = try self.data() - let pem = SwiftyRSA.format(keyData: data, withPemType: "RSA PUBLIC KEY") - return pem - } - - /// Creates a public key with a keychain key reference. - /// This initializer will throw if the provided key reference is not a public RSA key. - /// - /// - Parameter reference: Reference to the key within the keychain. - /// - Throws: SwiftyRSAError - public required init(reference: SecKey) throws { - - guard SwiftyRSA.isValidKeyReference(reference, forClass: kSecAttrKeyClassPublic) else { - throw SwiftyRSAError.notAPublicKey - } - - self.reference = reference - self.tag = nil - self.originalData = nil - } - - /// Data of the public key as returned by the keychain. - /// This method throws if SwiftyRSA cannot extract data from the key. - /// - /// - Returns: Data of the public key as returned by the keychain. - /// - Throws: SwiftyRSAError - public required init(data: Data) throws { - - let tag = UUID().uuidString - self.tag = tag - - self.originalData = data - let dataWithoutHeader = try SwiftyRSA.stripKeyHeader(keyData: data) - - reference = try SwiftyRSA.addKey(dataWithoutHeader, isPublic: true, tag: tag) - } - - static let publicKeyRegex: NSRegularExpression? = { - let publicKeyRegex = "(-----BEGIN PUBLIC KEY-----.+?-----END PUBLIC KEY-----)" - return try? NSRegularExpression(pattern: publicKeyRegex, options: .dotMatchesLineSeparators) - }() - - deinit { - if let tag { - SwiftyRSA.removeKey(tag: tag) - } - } -} diff --git a/FlowCrypt/Core/RSA/RSAMessage.swift b/FlowCrypt/Core/RSA/RSAMessage.swift deleted file mode 100644 index d6bace9e1..000000000 --- a/FlowCrypt/Core/RSA/RSAMessage.swift +++ /dev/null @@ -1,35 +0,0 @@ -// -// Message.swift -// SwiftyRSA -// -// Created by Loïs Di Qual on 9/19/16. -// Copyright © 2016 Scoop. All rights reserved. -// - -import Foundation - -public protocol RSAMessage { - var data: Data { get } - var base64String: String { get } - init(data: Data) - init(base64Encoded base64String: String) throws -} - -public extension RSAMessage { - - /// Base64-encoded string of the message data - var base64String: String { - return data.base64EncodedString() - } - - /// Creates an encrypted message with a base64-encoded string. - /// - /// - Parameter base64String: Base64-encoded data of the encrypted message - /// - Throws: SwiftyRSAError - init(base64Encoded base64String: String) throws { - guard let data = Data(base64Encoded: base64String) else { - throw SwiftyRSAError.invalidBase64String - } - self.init(data: data) - } -} diff --git a/FlowCrypt/Core/RSA/SwiftyRSA.swift b/FlowCrypt/Core/RSA/SwiftyRSA.swift deleted file mode 100644 index 00b11393b..000000000 --- a/FlowCrypt/Core/RSA/SwiftyRSA.swift +++ /dev/null @@ -1,238 +0,0 @@ -// -// SwiftyRSA.swift -// SwiftyRSA -// -// Created by Loïs Di Qual on 7/2/15. -// -// Copyright (c) 2015 Scoop Technologies, Inc. All rights reserved. -// - -import Foundation -import Security - -public enum SwiftyRSA { - - static func base64String(pemEncoded pemString: String) throws -> String { - let lines = pemString.components(separatedBy: "\n").filter { line in - return !line.hasPrefix("-----BEGIN") && !line.hasPrefix("-----END") - } - - guard !lines.isEmpty else { - throw SwiftyRSAError.pemDoesNotContainKey - } - - return lines.joined(separator: "") - } - - static func isValidKeyReference(_ reference: SecKey, forClass requiredClass: CFString) -> Bool { - let attributes = SecKeyCopyAttributes(reference) as? [CFString: Any] - guard let keyType = attributes?[kSecAttrKeyType] as? String, let keyClass = attributes?[kSecAttrKeyClass] as? String else { - return false - } - - let isRSA = keyType == (kSecAttrKeyTypeRSA as String) - let isValidClass = keyClass == (requiredClass as String) - return isRSA && isValidClass - } - - static func format(keyData: Data, withPemType pemType: String) -> String { - - func split(_ str: String, byChunksOfLength length: Int) -> [String] { - return stride(from: 0, to: str.count, by: length).map { index -> String in - let startIndex = str.index(str.startIndex, offsetBy: index) - let endIndex = str.index(startIndex, offsetBy: length, limitedBy: str.endIndex) ?? str.endIndex - return String(str[startIndex ..< endIndex]) - } - } - - // Line length is typically 64 characters, except the last line. - // See https://tools.ietf.org/html/rfc7468#page-6 (64base64char) - // See https://tools.ietf.org/html/rfc7468#page-11 (example) - let chunks = split(keyData.base64EncodedString(), byChunksOfLength: 64) - - let pem = [ - "-----BEGIN \(pemType)-----", - chunks.joined(separator: "\n"), - "-----END \(pemType)-----" - ] - - return pem.joined(separator: "\n") - } - - static func data(forKeyReference reference: SecKey) throws -> Data { - var error: Unmanaged? - let data = SecKeyCopyExternalRepresentation(reference, &error) - guard let unwrappedData = data as Data? else { - throw SwiftyRSAError.keyRepresentationFailed(error: error?.takeRetainedValue()) - } - return unwrappedData - } - - /// Will generate a new private and public key - /// - /// - Parameters: - /// - size: Indicates the total number of bits in this cryptographic key - /// - Returns: A touple of a private and public key - /// - Throws: Throws and error if the tag cant be parsed or if keygeneration fails - public static func generateRSAKeyPair(sizeInBits size: Int) throws -> (privateKey: PrivateKey, publicKey: PublicKey) { - return try generateRSAKeyPair(sizeInBits: size, applyUnitTestWorkaround: false) - } - - static func generateRSAKeyPair(sizeInBits size: Int, applyUnitTestWorkaround: Bool = false) throws -> (privateKey: PrivateKey, publicKey: PublicKey) { - - guard let tagData = UUID().uuidString.data(using: .utf8) else { - throw SwiftyRSAError.stringToDataConversionFailed - } - - // @hack Don't store permanently when running unit tests, otherwise we'll get a key creation error (NSOSStatusErrorDomain -50) - // @see http://www.openradar.me/36809637 - // @see https://stackoverflow.com/q/48414685/646960 - let isPermanent = applyUnitTestWorkaround ? false : true - - let attributes: [CFString: Any] = [ - kSecAttrKeyType: kSecAttrKeyTypeRSA, - kSecAttrKeySizeInBits: size, - kSecPrivateKeyAttrs: [ - kSecAttrIsPermanent: isPermanent, - kSecAttrApplicationTag: tagData - ] - ] - - var error: Unmanaged? - guard let privKey = SecKeyCreateRandomKey(attributes as CFDictionary, &error), - let pubKey = SecKeyCopyPublicKey(privKey) else { - throw SwiftyRSAError.keyGenerationFailed(error: error?.takeRetainedValue()) - } - let privateKey = try PrivateKey(reference: privKey) - let publicKey = try PublicKey(reference: pubKey) - - return (privateKey: privateKey, publicKey: publicKey) - } - - static func addKey(_ keyData: Data, isPublic: Bool, tag: String) throws -> SecKey { - - let keyData = keyData - - guard tag.data(using: .utf8) != nil else { - throw SwiftyRSAError.tagEncodingFailed - } - - let keyClass = isPublic ? kSecAttrKeyClassPublic : kSecAttrKeyClassPrivate - - let sizeInBits = keyData.count * 8 - let keyDict: [CFString: Any] = [ - kSecAttrKeyType: kSecAttrKeyTypeRSA, - kSecAttrKeyClass: keyClass, - kSecAttrKeySizeInBits: NSNumber(value: sizeInBits), - kSecReturnPersistentRef: true - ] - - var error: Unmanaged? - guard let key = SecKeyCreateWithData(keyData as CFData, keyDict as CFDictionary, &error) else { - throw SwiftyRSAError.keyCreateFailed(error: error?.takeRetainedValue()) - } - return key - } - - /** - This method strips the x509 header from a provided ASN.1 DER key. - If the key doesn't contain a header, the DER data is returned as is. - - Supported formats are: - - Headerless: - SEQUENCE - INTEGER (1024 or 2048 bit) -- modulo - INTEGER -- public exponent - - With x509 header: - SEQUENCE - SEQUENCE - OBJECT IDENTIFIER 1.2.840.113549.1.1.1 - NULL - BIT STRING - SEQUENCE - INTEGER (1024 or 2048 bit) -- modulo - INTEGER -- public exponent - - Example of headerless key: - https://lapo.it/asn1js/#3082010A0282010100C1A0DFA367FBC2A5FD6ED5A071E02A4B0617E19C6B5AD11BB61192E78D212F10A7620084A3CED660894134D4E475BAD7786FA1D40878683FD1B7A1AD9C0542B7A666457A270159DAC40CE25B2EAE7CCD807D31AE725CA394F90FBB5C5BA500545B99C545A9FE08EFF00A5F23457633E1DB84ED5E908EF748A90F8DFCCAFF319CB0334705EA012AF15AA090D17A9330159C9AFC9275C610BB9B7C61317876DC7386C723885C100F774C19830F475AD1E9A9925F9CA9A69CE0181A214DF2EB75FD13E6A546B8C8ED699E33A8521242B7E42711066AEC22D25DD45D56F94D3170D6F2C25164D2DACED31C73963BA885ADCB706F40866B8266433ED5161DC50E4B3B0203010001 - - Example of key with X509 header (notice the additional ASN.1 sequence): - https://lapo.it/asn1js/#30819F300D06092A864886F70D010101050003818D0030818902818100D0674615A252ED3D75D2A3073A0A8A445F3188FD3BEB8BA8584F7299E391BDEC3427F287327414174997D147DD8CA62647427D73C9DA5504E0A3EED5274A1D50A1237D688486FADB8B82061675ABFA5E55B624095DB8790C6DBCAE83D6A8588C9A6635D7CF257ED1EDE18F04217D37908FD0CBB86B2C58D5F762E6207FF7B92D0203010001 - */ - static func stripKeyHeader(keyData: Data) throws -> Data { - - let node: Asn1Parser.Node - do { - node = try Asn1Parser.parse(data: keyData) - } catch { - throw SwiftyRSAError.asn1ParsingFailed - } - - // Ensure the raw data is an ASN1 sequence - guard case let .sequence(nodes) = node else { - throw SwiftyRSAError.invalidAsn1RootNode - } - - // Detect whether the sequence only has integers, in which case it's a headerless key - let onlyHasIntegers = nodes.filter { node -> Bool in - if case .integer = node { - return false - } - return true - }.isEmpty - - // Headerless key - if onlyHasIntegers { - return keyData - } - - // If last element of the sequence is a bit string, return its data - if let last = nodes.last, case let .bitString(data) = last { - return data - } - - // If last element of the sequence is an octet string, return its data - if let last = nodes.last, case let .octetString(data) = last { - return data - } - - // Unable to extract bit/octet string or raw integer sequence - throw SwiftyRSAError.invalidAsn1Structure - } - - /** - This method prepend the x509 header to the given PublicKey data. - If the key already contain a x509 header, the given data is returned as is. - It letterally does the opposite of the previous method : - From a given headerless key : - SEQUENCE - INTEGER (1024 or 2048 bit) -- modulo - INTEGER -- public exponent - the key is returned following the X509 header : - SEQUENCE - SEQUENCE - OBJECT IDENTIFIER 1.2.840.113549.1.1.1 - NULL - BIT STRING - SEQUENCE - INTEGER (1024 or 2048 bit) -- modulo - INTEGER -- public exponent - */ - - static func removeKey(tag: String) { - - guard let tagData = tag.data(using: .utf8) else { - return - } - - let keyRemoveDict: [CFString: Any] = [ - kSecClass: kSecClassKey, - kSecAttrKeyType: kSecAttrKeyTypeRSA, - kSecAttrApplicationTag: tagData, - ] - - SecItemDelete(keyRemoveDict as CFDictionary) - } -} diff --git a/FlowCrypt/Core/RSA/SwiftyRSAError.swift b/FlowCrypt/Core/RSA/SwiftyRSAError.swift deleted file mode 100644 index e3495eed9..000000000 --- a/FlowCrypt/Core/RSA/SwiftyRSAError.swift +++ /dev/null @@ -1,88 +0,0 @@ -// -// SwiftyRSAError.swift -// SwiftyRSA -// -// Created by Lois Di Qual on 5/15/17. -// Contributions by Stchepinsky Nathan on 24/06/2021 -// Copyright © 2017 Scoop. All rights reserved. -// - -import Foundation - -public enum SwiftyRSAError: Error { - - case pemDoesNotContainKey - case keyRepresentationFailed(error: CFError?) - case keyGenerationFailed(error: CFError?) - case keyCreateFailed(error: CFError?) - case keyAddFailed(status: OSStatus) - case keyCopyFailed(status: OSStatus) - case tagEncodingFailed - case asn1ParsingFailed - case invalidAsn1RootNode - case invalidAsn1Structure - case invalidBase64String - case chunkDecryptFailed(index: Int) - case chunkEncryptFailed(index: Int) - case stringToDataConversionFailed - case dataToStringConversionFailed - case invalidDigestSize(digestSize: Int, maxChunkSize: Int) - case signatureCreateFailed(status: OSStatus) - case signatureVerifyFailed(status: OSStatus) - case pemFileNotFound(name: String) - case derFileNotFound(name: String) - case notAPublicKey - case notAPrivateKey - case x509CertificateFailed - - var localizedDescription: String { - switch self { - case .pemDoesNotContainKey: - return "Couldn't get data from PEM key: no data available after stripping headers" - case let .keyRepresentationFailed(error): - return "Couldn't retrieve key data from the keychain: CFError \(String(describing: error))" - case let .keyGenerationFailed(error): - return "Couldn't generate key pair: CFError: \(String(describing: error))" - case let .keyCreateFailed(error): - return "Couldn't create key reference from key data: CFError \(String(describing: error))" - case let .keyAddFailed(status): - return "Couldn't retrieve key data from the keychain: OSStatus \(status)" - case let .keyCopyFailed(status): - return "Couldn't copy and retrieve key reference from the keychain: OSStatus \(status)" - case .tagEncodingFailed: - return "Couldn't create tag data for key" - case .asn1ParsingFailed: - return "Couldn't parse the ASN1 key data. Please file a bug at https://goo.gl/y67MW6" - case .invalidAsn1RootNode: - return "Couldn't parse the provided key because its root ASN1 node is not a sequence. The key is probably corrupt" - case .invalidAsn1Structure: - return "Couldn't parse the provided key because it has an unexpected ASN1 structure" - case .invalidBase64String: - return "The provided string is not a valid Base 64 string" - case let .chunkDecryptFailed(index): - return "Couldn't decrypt chunk at index \(index)" - case let .chunkEncryptFailed(index): - return "Couldn't encrypt chunk at index \(index)" - case .stringToDataConversionFailed: - return "Couldn't convert string to data using specified encoding" - case .dataToStringConversionFailed: - return "Couldn't convert data to string representation" - case let .invalidDigestSize(digestSize, maxChunkSize): - return "Provided digest type produces a size (\(digestSize)) that is bigger than the maximum chunk size \(maxChunkSize) of the RSA key" - case let .signatureCreateFailed(status): - return "Couldn't sign provided data: OSStatus \(status)" - case let .signatureVerifyFailed(status): - return "Couldn't verify signature of the provided data: OSStatus \(status)" - case let .pemFileNotFound(name): - return "Couldn't find a PEM file named '\(name)'" - case let .derFileNotFound(name): - return "Couldn't find a DER file named '\(name)'" - case .notAPublicKey: - return "Provided key is not a valid RSA public key" - case .notAPrivateKey: - return "Provided key is not a valid RSA pivate key" - case .x509CertificateFailed: - return "Couldn't prepend the provided key because it has an unexpected structure" - } - } -} diff --git a/FlowCrypt/Functionality/Api/Remote Pub Key Apis/WkdUrlConstructor.swift b/FlowCrypt/Functionality/Api/Remote Pub Key Apis/WkdUrlConstructor.swift index e9d5667b7..4156676bd 100644 --- a/FlowCrypt/Functionality/Api/Remote Pub Key Apis/WkdUrlConstructor.swift +++ b/FlowCrypt/Functionality/Api/Remote Pub Key Apis/WkdUrlConstructor.swift @@ -1,5 +1,5 @@ // -// WKDURLsConstructor.swift +// WkdUrlConstructor.swift // FlowCrypt // // Created by Yevhen Kyivskyi on 15.05.2021. @@ -36,7 +36,8 @@ class WkdUrlConstructor: WkdUrlConstructorType { let domain = Bundle.shouldUseMockAttesterApi ? GeneralConstants.Mock.backendUrl.replacingOccurrences(of: "https://", with: "") : String(parts[1]).lowercased() - let hu = String(decoding: user.lowercased().data().SHA1.zBase32EncodedBytes(), as: Unicode.UTF8.self) + let hashedRecipient = Data(Insecure.SHA1.hash(data: user.lowercased().data())) + let hu = String(decoding: hashedRecipient.zBase32EncodedBytes(), as: Unicode.UTF8.self) let userPart = "hu/\(hu)?l=\(user)" let base = method == .direct ? "https://\(domain)/.well-known/openpgpkey/" diff --git a/FlowCrypt/Functionality/DataManager/Encrypted Storage/StorageEncryptionKeyProvider.swift b/FlowCrypt/Functionality/DataManager/Encrypted Storage/StorageEncryptionKeyProvider.swift index 09ac967c1..b24164a52 100644 --- a/FlowCrypt/Functionality/DataManager/Encrypted Storage/StorageEncryptionKeyProvider.swift +++ b/FlowCrypt/Functionality/DataManager/Encrypted Storage/StorageEncryptionKeyProvider.swift @@ -7,7 +7,6 @@ // import FlowCryptCommon -import IDZSwiftCommonCrypto import Security import UIKit diff --git a/FlowCryptAppTests/Functionality/WKDURLs/ZBase32EncodingTests.swift b/FlowCryptAppTests/Functionality/WKDURLs/ZBase32EncodingTests.swift index c10e39e5b..90cfcd9b6 100644 --- a/FlowCryptAppTests/Functionality/WKDURLs/ZBase32EncodingTests.swift +++ b/FlowCryptAppTests/Functionality/WKDURLs/ZBase32EncodingTests.swift @@ -6,6 +6,7 @@ // Copyright © 2017-present FlowCrypt a. s. All rights reserved. // +import CryptoKit import XCTest class ZBase32EncodingTests: XCTestCase { @@ -23,8 +24,9 @@ class ZBase32EncodingTests: XCTestCase { let inputString = "example@email.com" let encodedString = "8dkp15twcw7feu1i8em784qtw91y3cs7" + let hashedInputString = Data(Insecure.SHA1.hash(data: inputString.data())) XCTAssert( - String(decoding: inputString.data().SHA1.zBase32EncodedBytes(), as: Unicode.UTF8.self) == encodedString + String(decoding: hashedInputString.zBase32EncodedBytes(), as: Unicode.UTF8.self) == encodedString ) } }