diff --git a/Supersign/GrandSlam/Crypto/AppTokens.swift b/Supersign/GrandSlam/Crypto/AppTokens.swift new file mode 100644 index 00000000..b5d66ab3 --- /dev/null +++ b/Supersign/GrandSlam/Crypto/AppTokens.swift @@ -0,0 +1,44 @@ +// +// AppTokens.swift +// Supersign +// +// Created by Kabir Oberai on 11/04/20. +// Copyright © 2020 Kabir Oberai. All rights reserved. +// + +import Foundation + +enum AppTokens { + + static func checksum(withSK sk: Data, adsid: String, apps: [String]) -> Data { + sk.withUnsafeBytes { buf in + let bound = buf.bindMemory(to: Int8.self) + let ptrArray = apps.map { strdup($0)! } + defer { ptrArray.forEach { free($0) } } + let immutablePtrArray = ptrArray.map { UnsafePointer($0) } + return immutablePtrArray.withUnsafeBufferPointer { buf in + var length = 0 + let bytes = app_tokens_create_checksum( + bound.baseAddress!, bound.count, adsid, buf.baseAddress!, buf.count, &length + ) + return Data(bytesNoCopy: bytes, count: length, deallocator: .free) + } + } + } + + static func decrypt(gcm: Data, sk: Data) -> Data? { + gcm.withUnsafeBytes { gcmBuf in + sk.withUnsafeBytes { skBuf in + let gcmBound = gcmBuf.bindMemory(to: Int8.self) + let skBound = skBuf.bindMemory(to: Int8.self) + var length = 0 + return app_tokens_decrypt_gcm( + gcmBound.baseAddress!, gcmBound.count, + skBound.baseAddress!, skBound.count, + &length + ).map { Data(bytesNoCopy: $0, count: length, deallocator: .free) } + } + } + } + +} diff --git a/Supersign/GrandSlam/Crypto/SRPClient.swift b/Supersign/GrandSlam/Crypto/SRPClient.swift new file mode 100644 index 00000000..cf55ac92 --- /dev/null +++ b/Supersign/GrandSlam/Crypto/SRPClient.swift @@ -0,0 +1,85 @@ +// +// SRPClient.swift +// Supersign +// +// Created by Kabir Oberai on 11/04/20. +// Copyright © 2020 Kabir Oberai. All rights reserved. +// + +import Foundation + +class SRPClient { + + let raw = srp_client_create() + deinit { srp_client_free(raw) } + + // MARK: - Encryption & decryption + + func add(string: String) { + srp_client_add_string(raw, string) + } + + func add(data: Data) { + data.withUnsafeBytes { buf in + let bound = buf.bindMemory(to: Int8.self) + srp_client_add_data(raw, bound.baseAddress!, bound.count) + } + } + + func decrypt(cbc: Data) -> Data? { + cbc.withUnsafeBytes { buf in + let bound = buf.bindMemory(to: Int8.self) + var length = 0 + return srp_client_decrypt_cbc(raw, bound.baseAddress!, bound.count, &length) + .map { Data(bytesNoCopy: $0, count: length, deallocator: .free) } + } + } + + // MARK: - SRP + + func startAuthenticationAndGetA() -> Data { + var length = 0 + return Data(bytesNoCopy: srp_client_start_auth_and_get_a(raw, &length), count: length, deallocator: .free) + } + + func processChallenge( + withUsername username: String, + password: String, + salt: Data, + iterations: Int, + bData: Data, + isS2K: Bool + ) -> Data? { + salt.withUnsafeBytes { saltBuf in + bData.withUnsafeBytes { bDataBuf in + let saltBound = saltBuf.bindMemory(to: Int8.self) + let bDataBound = bDataBuf.bindMemory(to: Int8.self) + var length = 0 + return srp_client_process_challenge( + raw, + username, password, + saltBound.baseAddress!, saltBound.count, + .init(iterations), + bDataBound.baseAddress!, + isS2K, + &length + ).map { Data(bytesNoCopy: $0, count: length, deallocator: .free) } + } + } + } + + func verify(m2: Data) -> Bool { + m2.withUnsafeBytes { buf in + let bound = buf.bindMemory(to: Int8.self) + return srp_client_verify_session_m2(raw, bound.baseAddress!, bound.count) + } + } + + func verify(negProto: Data) -> Bool { + negProto.withUnsafeBytes { buf in + let bound = buf.bindMemory(to: Int8.self) + return srp_client_verify_neg_proto(raw, bound.baseAddress!, bound.count) + } + } + +} diff --git a/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift b/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift index 2e1520d5..788b51da 100644 --- a/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift +++ b/Supersign/GrandSlam/Operations/GrandSlamAuthenticateOperation.swift @@ -27,7 +27,7 @@ class GrandSlamAuthenticateOperation { } } - private var helper = SRPHelper() + private var srpClient = SRPClient() private var isSecondAttempt = false private let decoder = PropertyListDecoder() @@ -50,7 +50,7 @@ class GrandSlamAuthenticateOperation { } private func restartAuth(completion: @escaping (Result) -> Void) { - helper = .init() + srpClient = .init() isSecondAttempt = true authenticate(completion: completion) } @@ -77,17 +77,16 @@ class GrandSlamAuthenticateOperation { completeResponse: GrandSlamAuthCompleteRequest.Decoder.Value, completion: @escaping (Result) -> Void ) { - helper.addString(toNegProt: "|") - helper.addData(toNegProt: completeResponse.encryptedResponse) - helper.addString(toNegProt: "|") - completeResponse.sc.map(helper.addData) - helper.addString(toNegProt: "|") + srpClient.add(string: "|") + srpClient.add(data: completeResponse.encryptedResponse) + srpClient.add(string: "|") + completeResponse.sc.map(srpClient.add(data:)) + srpClient.add(string: "|") - guard helper.verifySession(withM2: completeResponse.m2Data) && helper.verifyNegProto(completeResponse.negProto) else { - return completion(.failure(Error.invalidSession)) - } + guard srpClient.verify(m2: completeResponse.m2Data) && srpClient.verify(negProto: completeResponse.negProto) + else { return completion(.failure(Error.invalidSession)) } - guard let rawLoginResponse = helper.decryptCBC(completeResponse.encryptedResponse) else { + guard let rawLoginResponse = srpClient.decrypt(cbc: completeResponse.encryptedResponse) else { return completion(.failure(Error.invalidResponse)) } @@ -124,10 +123,10 @@ class GrandSlamAuthenticateOperation { completion: @escaping (Result) -> Void ) { let isS2K = initResponse.selectedProtocol == .s2k - helper.addString(toNegProt: "|") - helper.addString(toNegProt: initResponse.selectedProtocol.rawValue) + srpClient.add(string: "|") + srpClient.add(string: initResponse.selectedProtocol.rawValue) - let mDataRaw = helper.processChallenge( + let mDataRaw = srpClient.processChallenge( withUsername: username, password: password, salt: initResponse.salt, @@ -149,10 +148,10 @@ class GrandSlamAuthenticateOperation { func authenticate( completion: @escaping (Result) -> Void ) { - helper.addString(toNegProt: GrandSlamAuthInitRequest.protocols.joined(separator: ",")) - helper.addString(toNegProt: "|") + srpClient.add(string: GrandSlamAuthInitRequest.protocols.joined(separator: ",")) + srpClient.add(string: "|") - let aData = helper.startAuthenticationAndGetA() + let aData = srpClient.startAuthenticationAndGetA() let initRequest = GrandSlamAuthInitRequest(username: username, aData: aData) client.send(initRequest) { result in diff --git a/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift b/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift index 66a0dda4..70527177 100644 --- a/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift +++ b/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift @@ -61,7 +61,7 @@ class GrandSlamFetchAppTokensOperation { response: GrandSlamAppTokensRequest.Decoder.Value, completion: @escaping (Result<[AppTokenKey: Token], Swift.Error>) -> Void ) { - guard let decrypted = AppTokensHelper.decryptGCM(response.encryptedToken, sk: self.loginData.sk) + guard let decrypted = AppTokens.decrypt(gcm: response.encryptedToken, sk: self.loginData.sk) else { return completion(.failure(Error.invalidResponse)) } completion(Result { let pairs = try decoder @@ -73,9 +73,7 @@ class GrandSlamFetchAppTokensOperation { } func perform(completion: @escaping (Result<[AppTokenKey: Token], Swift.Error>) -> Void) { - let checksum = AppTokensHelper.createAppTokensChecksum( - withSK: loginData.sk, adsid: loginData.adsid, apps: apps.map { $0.rawValue } - ) + let checksum = AppTokens.checksum(withSK: loginData.sk, adsid: loginData.adsid, apps: apps.map { $0.rawValue }) let appTokensRequest = GrandSlamAppTokensRequest( username: loginData.adsid, apps: apps,