Skip to content

Commit c6b61ea

Browse files
committed
support legacy SRP (s2k_fo) logins
1 parent 1e2cc92 commit c6b61ea

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

Sources/AppleAPI/Client.swift

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class Client {
105105
let iterations = srpInit.iteration
106106

107107
do {
108-
guard let encryptedPassword = self.pbkdf2(password: password, saltData: decodedSalt, keyByteCount: 32, prf: CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA256), rounds: iterations) else {
108+
guard let encryptedPassword = self.pbkdf2(password: password, saltData: decodedSalt, keyByteCount: 32, prf: CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA256), rounds: iterations, protocol: srpInit.protocol) else {
109109
throw Error.srpInvalidPublicKey
110110
}
111111

@@ -382,9 +382,14 @@ public class Client {
382382
return Data(hash)
383383
}
384384

385-
private func pbkdf2(password: String, saltData: Data, keyByteCount: Int, prf: CCPseudoRandomAlgorithm, rounds: Int) -> Data? {
385+
private func pbkdf2(password: String, saltData: Data, keyByteCount: Int, prf: CCPseudoRandomAlgorithm, rounds: Int, protocol srpProtocol: SRPProtocol) -> Data? {
386386
guard let passwordData = password.data(using: .utf8) else { return nil }
387-
let hashedPasswordData = sha256(data: passwordData)
387+
let hashedPasswordDataRaw = sha256(data: passwordData)
388+
let hashedPasswordData = switch srpProtocol {
389+
case .s2k: hashedPasswordDataRaw
390+
// the legacy s2k_fo protocol requires hex-encoding the digest before performing PBKDF2.
391+
case .s2k_fo: Data(hashedPasswordDataRaw.hexEncodedString().lowercased().utf8)
392+
}
388393

389394
var derivedKeyData = Data(repeating: 0, count: keyByteCount)
390395
let derivedCount = derivedKeyData.count
@@ -515,4 +520,10 @@ public struct ServerSRPInitResponse: Decodable {
515520
let salt: String
516521
let b: String
517522
let c: String
523+
let `protocol`: SRPProtocol
524+
}
525+
extension Data {
526+
func hexEncodedString() -> String {
527+
return map { String(format: "%02hhx", $0) }.joined()
528+
}
518529
}

0 commit comments

Comments
 (0)