Skip to content

Commit 7b15450

Browse files
authored
Merge pull request #650 from kabiroberai/kabir/legacy-srp
Fix handling of s2k_fo
2 parents 0a02b29 + 8654756 commit 7b15450

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

Xcodes/AppleAPI/Sources/AppleAPI/Client.swift

+9-3
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public class Client {
5454
let iterations = srpInit.iteration
5555

5656
do {
57-
guard let encryptedPassword = self.pbkdf2(password: password, saltData: decodedSalt, keyByteCount: 32, prf: CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA256), rounds: iterations) else {
57+
guard let encryptedPassword = self.pbkdf2(password: password, saltData: decodedSalt, keyByteCount: 32, prf: CCPseudoRandomAlgorithm(kCCPRFHmacAlgSHA256), rounds: iterations, protocol: srpInit.protocol) else {
5858
return Fail(error: AuthenticationError.srpInvalidPublicKey)
5959
.eraseToAnyPublisher()
6060
}
@@ -308,9 +308,14 @@ public class Client {
308308
return Data(hash)
309309
}
310310

311-
private func pbkdf2(password: String, saltData: Data, keyByteCount: Int, prf: CCPseudoRandomAlgorithm, rounds: Int) -> Data? {
311+
private func pbkdf2(password: String, saltData: Data, keyByteCount: Int, prf: CCPseudoRandomAlgorithm, rounds: Int, protocol srpProtocol: SRPProtocol) -> Data? {
312312
guard let passwordData = password.data(using: .utf8) else { return nil }
313-
let hashedPasswordData = sha256(data: passwordData)
313+
let hashedPasswordDataRaw = sha256(data: passwordData)
314+
let hashedPasswordData = switch srpProtocol {
315+
case .s2k: hashedPasswordDataRaw
316+
// the legacy s2k_fo protocol requires hex-encoding the digest before performing PBKDF2.
317+
case .s2k_fo: Data(hashedPasswordDataRaw.hexEncodedString().lowercased().utf8)
318+
}
314319

315320
var derivedKeyData = Data(repeating: 0, count: keyByteCount)
316321
let derivedCount = derivedKeyData.count
@@ -584,6 +589,7 @@ public struct ServerSRPInitResponse: Decodable {
584589
let salt: String
585590
let b: String
586591
let c: String
592+
let `protocol`: SRPProtocol
587593
}
588594

589595

0 commit comments

Comments
 (0)