Use SwiftCrypto for AppTokens
- Commit
52595cda2c955f0acd51b695efe6b9ab49d96516- Author
- Kabir Oberai <[email protected]>
- When
- 2023-09-03 06:36 UTC (3 years ago)
- Parents
b96ab46
Diff
Supersign/Sources/CSupersign/mobileprovision.c
diff --git a/Supersign/Sources/CSupersign/mobileprovision.c b/Supersign/Sources/CSupersign/mobileprovision.cindex ee39f1f..0d7e29f 100644--- a/Supersign/Sources/CSupersign/mobileprovision.c+++ b/Supersign/Sources/CSupersign/mobileprovision.c@@ -9,7 +9,6 @@ #include <stdlib.h> #include <string.h> #include <openssl/pkcs7.h>-#include <openssl/x509.h> #include <openssl/bio.h> #include "mobileprovision.h"
Supersign/Sources/Supersign/GrandSlam/Crypto/AppTokens.swift
diff --git a/Supersign/Sources/Supersign/GrandSlam/Crypto/AppTokens.swift b/Supersign/Sources/Supersign/GrandSlam/Crypto/AppTokens.swiftindex 69df654..530bda5 100644--- a/Supersign/Sources/Supersign/GrandSlam/Crypto/AppTokens.swift+++ b/Supersign/Sources/Supersign/GrandSlam/Crypto/AppTokens.swift@@ -7,40 +7,36 @@ // import Foundation-import CSupersign+import Crypto enum AppTokens { static func checksum(withSK sk: Data, adsid: String, apps: [String]) -> Data {- sk.withUnsafeBytes { skBuf in- let ptrArray = apps.map { strdup($0)! }- defer { ptrArray.forEach { free($0) } }- let immutablePtrArray = ptrArray.map { UnsafePointer($0) }- return Data {- app_tokens_create_checksum(- skBuf.baseAddress, skBuf.count,- adsid,- immutablePtrArray, immutablePtrArray.count,- &$0- )- }+ var hmac = HMAC<SHA256>(key: SymmetricKey(data: sk))+ hmac.update(data: Data("apptokens".utf8))+ hmac.update(data: Data(adsid.utf8))+ for app in apps {+ hmac.update(data: Data(app.utf8)) }+ return Data(hmac.finalize()) } - static func decrypt(gcm: Data, withSK sk: Data) -> Data? {- gcm.withUnsafeBytes { gcmBuf in- sk.withUnsafeBytes { skBuf in- // the data must have a header so it shouldn't be empty- guard let gcmBase = gcmBuf.baseAddress,- // the session key must have a length equal to that required by- // the cipher. It should be non-empty.- let skBase = skBuf.baseAddress- else { return nil }- return Data {- app_tokens_decrypt_gcm(gcmBase, gcmBuf.count, skBase, skBuf.count, &$0)- }- }- }+ // AppleIDAuthSupport`_AppleIDAuthSupportCreateDecryptedData+ static func decrypt(gcm: Data, withSK sk: Data) throws -> Data {+ let aad = gcm[..<3] // should be "XYZ"+ let iv = gcm[3..<19]+ let payload = gcm[19..<(gcm.count - 16)]+ let tag = gcm[(gcm.count - 16)...]++ return try AES.GCM.open(+ AES.GCM.SealedBox(+ nonce: AES.GCM.Nonce(data: iv),+ ciphertext: payload,+ tag: tag+ ),+ using: SymmetricKey(data: sk),+ authenticating: aad+ ) } }
Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift
diff --git a/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift b/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swiftindex 80e0b20..0affea5 100644--- a/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift+++ b/Supersign/Sources/Supersign/GrandSlam/Operations/GrandSlamFetchAppTokensOperation.swift@@ -61,8 +61,12 @@ class GrandSlamFetchAppTokensOperation { response: GrandSlamAppTokensRequest.Decoder.Value, completion: @escaping (Result<[AppTokenKey: Token], Swift.Error>) -> Void ) {- guard let decrypted = AppTokens.decrypt(gcm: response.encryptedToken, withSK: loginData.sk)- else { return completion(.failure(Error.invalidResponse)) }+ let decrypted: Data+ do {+ decrypted = try AppTokens.decrypt(gcm: response.encryptedToken, withSK: loginData.sk)+ } catch {+ return completion(.failure(error))+ } completion(Result { let pairs = try decoder .decode(Response.self, from: decrypted)