import CryptoSwift import PromiseKit /// See the "Onion Requests" section of [The Session Whitepaper](https://arxiv.org/pdf/2002.04609.pdf) for more information. internal enum OnionRequestAPI { /// - Note: Must only be modified from `LokiAPI.workQueue`. internal static var guardSnodes: Set = [] /// - Note: Must only be modified from `LokiAPI.workQueue`. internal static var paths: Set = [] private static var snodePool: Set { let unreliableSnodes = Set(LokiAPI.failureCount.keys) return LokiAPI.randomSnodePool.subtracting(unreliableSnodes) } // MARK: Settings private static let pathCount: UInt = 2 /// The number of snodes (including the guard snode) in a path. private static let pathSize: UInt = 1 private static var guardSnodeCount: UInt { return pathCount } // One per path // MARK: Error internal enum Error : LocalizedError { case httpRequestFailedAtTargetSnode(statusCode: UInt, json: JSON) case insufficientSnodes case missingSnodeVersion case randomDataGenerationFailed case snodePublicKeySetMissing case unsupportedSnodeVersion(String) var errorDescription: String? { switch self { case .httpRequestFailedAtTargetSnode(let statusCode): return "HTTP request failed at target snode with status code: \(statusCode)." case .insufficientSnodes: return "Couldn't find enough snodes to build a path." case .missingSnodeVersion: return "Missing snode version." case .randomDataGenerationFailed: return "Couldn't generate random data." case .snodePublicKeySetMissing: return "Missing snode public key set." case .unsupportedSnodeVersion(let version): return "Unsupported snode version: \(version)." } } } // MARK: Path internal typealias Path = [LokiAPITarget] // MARK: Onion Building Result private typealias OnionBuildingResult = (guardSnode: LokiAPITarget, finalEncryptionResult: EncryptionResult, targetSnodeSymmetricKey: Data) // MARK: Private API /// Tests the given snode. The returned promise errors out if the snode is faulty; the promise is fulfilled otherwise. private static func testSnode(_ snode: LokiAPITarget) -> Promise { let (promise, seal) = Promise.pending() let queue = DispatchQueue.global() // No need to block the work queue for this queue.async { let url = "\(snode.address):\(snode.port)/get_stats/v1" let timeout: TimeInterval = 6 // Use a shorter timeout for testing HTTP.execute(.get, url, timeout: timeout).done(on: queue) { rawResponse in guard let json = rawResponse as? JSON, let version = json["version"] as? String else { return seal.reject(Error.missingSnodeVersion) } if version >= "2.0.0" { seal.fulfill(()) } else { print("[Loki] [Onion Request API] Unsupported snode version: \(version).") seal.reject(Error.unsupportedSnodeVersion(version)) } }.catch(on: queue) { error in seal.reject(error) } } return promise } /// Finds `guardSnodeCount` guard snodes to use for path building. The returned promise errors out with `Error.insufficientSnodes` /// if not enough (reliable) snodes are available. private static func getGuardSnodes() -> Promise> { if guardSnodes.count >= guardSnodeCount { return Promise> { $0.fulfill(guardSnodes) } } else { print("[Loki] [Onion Request API] Populating guard snode cache.") return LokiAPI.getRandomSnode().then(on: LokiAPI.workQueue) { _ -> Promise> in // Just used to populate the snode pool var unusedSnodes = snodePool // Sync on LokiAPI.workQueue guard unusedSnodes.count >= guardSnodeCount else { throw Error.insufficientSnodes } func getGuardSnode() -> Promise { // randomElement() uses the system's default random generator, which is cryptographically secure guard let candidate = unusedSnodes.randomElement() else { return Promise { $0.reject(Error.insufficientSnodes) } } unusedSnodes.remove(candidate) // All used snodes should be unique print("[Loki] [Onion Request API] Testing guard snode: \(candidate).") // Loop until a reliable guard snode is found return testSnode(candidate).map(on: LokiAPI.workQueue) { candidate }.recover(on: LokiAPI.workQueue) { _ in getGuardSnode() } } let promises = (0.. Promise> { print("[Loki] [Onion Request API] Building onion request paths.") return LokiAPI.getRandomSnode().then(on: LokiAPI.workQueue) { _ -> Promise> in // Just used to populate the snode pool return getGuardSnodes().map(on: LokiAPI.workQueue) { guardSnodes in var unusedSnodes = snodePool.subtracting(guardSnodes) let pathSnodeCount = guardSnodeCount * pathSize - guardSnodeCount guard unusedSnodes.count >= pathSnodeCount else { throw Error.insufficientSnodes } // Don't test path snodes as this would reveal the user's IP to them return Set(guardSnodes.map { guardSnode in let result = [ guardSnode ] + (0..<(pathSize - 1)).map { _ in // randomElement() uses the system's default random generator, which is cryptographically secure let pathSnode = unusedSnodes.randomElement()! // Safe because of the minSnodeCount check above unusedSnodes.remove(pathSnode) // All used snodes should be unique return pathSnode } print("[Loki] [Onion Request API] Built new onion request path: \(result.prettifiedDescription).") return result }) } } } /// Returns a `Path` to be used for building an onion request. Builds new paths as needed. /// /// - Note: Exposed for testing purposes. internal static func getPath(excluding snode: LokiAPITarget) -> Promise { guard pathSize >= 1 else { preconditionFailure("Can't build path of size zero.") } // randomElement() uses the system's default random generator, which is cryptographically secure if paths.count >= pathCount { return Promise { seal in seal.fulfill(paths.filter { !$0.contains(snode) }.randomElement()!) } } else { return buildPaths().map(on: LokiAPI.workQueue) { paths in let path = paths.filter { !$0.contains(snode) }.randomElement()! OnionRequestAPI.paths = paths return path } } } private static func dropPath(containing snode: LokiAPITarget) { paths = paths.filter { !$0.contains(snode) } } /// Builds an onion around `payload` and returns the result. private static func buildOnion(around payload: JSON, targetedAt snode: LokiAPITarget) -> Promise { var guardSnode: LokiAPITarget! var targetSnodeSymmetricKey: Data! // Needed by invoke(_:on:with:) to decrypt the response sent back by the target snode var encryptionResult: EncryptionResult! return getPath(excluding: snode).then(on: LokiAPI.workQueue) { path -> Promise in guardSnode = path.first! // Encrypt in reverse order, i.e. the target snode first return encrypt(payload, forTargetSnode: snode).then(on: LokiAPI.workQueue) { r -> Promise in targetSnodeSymmetricKey = r.symmetricKey // Recursively encrypt the layers of the onion (again in reverse order) encryptionResult = r var path = path var rhs = snode func addLayer() -> Promise { if path.isEmpty { return Promise { $0.fulfill(encryptionResult) } } else { let lhs = path.removeLast() return OnionRequestAPI.encryptHop(from: lhs, to: rhs, using: encryptionResult).then(on: LokiAPI.workQueue) { r -> Promise in encryptionResult = r rhs = lhs return addLayer() } } } return addLayer() } }.map(on: LokiAPI.workQueue) { _ in (guardSnode, encryptionResult, targetSnodeSymmetricKey) } } // MARK: Internal API /// Sends an onion request to `snode`. Builds new paths as needed. internal static func sendOnionRequest(invoking method: LokiAPITarget.Method, on snode: LokiAPITarget, with parameters: JSON, associatedWith hexEncodedPublicKey: String) -> Promise { let (promise, seal) = Promise.pending() var guardSnode: LokiAPITarget! LokiAPI.workQueue.async { let payload: JSON = [ "method" : method.rawValue, "params" : parameters ] buildOnion(around: payload, targetedAt: snode).done(on: LokiAPI.workQueue) { intermediate in guardSnode = intermediate.guardSnode let url = "\(guardSnode.address):\(guardSnode.port)/onion_req" let finalEncryptionResult = intermediate.finalEncryptionResult let onion = finalEncryptionResult.ciphertext let parameters: JSON = [ "ciphertext" : onion.base64EncodedString(), "ephemeral_key" : finalEncryptionResult.ephemeralPublicKey.toHexString() ] let targetSnodeSymmetricKey = intermediate.targetSnodeSymmetricKey HTTP.execute(.post, url, parameters: parameters).done(on: LokiAPI.workQueue) { rawResponse in guard let json = rawResponse as? JSON, let base64EncodedIVAndCiphertext = json["result"] as? String, let ivAndCiphertext = Data(base64Encoded: base64EncodedIVAndCiphertext) else { return seal.reject(HTTP.Error.invalidJSON) } let iv = ivAndCiphertext[0.. Promise { return recover(on: LokiAPI.errorHandlingQueue) { error -> Promise in // Must be invoked on LokiAPI.errorHandlingQueue // The code below is very similar to that in LokiAPI.handlingSnodeErrorsIfNeeded(for:associatedWith:), but unfortunately slightly // different due to the fact that OnionRequestAPI uses the newer HTTP API, whereas LokiAPI still uses TSNetworkManager guard case OnionRequestAPI.Error.httpRequestFailedAtTargetSnode(let statusCode, let json) = error else { throw error } switch statusCode { case 0, 400, 500, 503: // The snode is unreachable let oldFailureCount = LokiAPI.failureCount[snode] ?? 0 let newFailureCount = oldFailureCount + 1 LokiAPI.failureCount[snode] = newFailureCount print("[Loki] Couldn't reach snode at: \(snode); setting failure count to \(newFailureCount).") if newFailureCount >= LokiAPI.failureThreshold { print("[Loki] Failure threshold reached for: \(snode); dropping it.") LokiAPI.dropIfNeeded(snode, hexEncodedPublicKey: hexEncodedPublicKey) // Remove it from the swarm cache associated with the given public key LokiAPI.randomSnodePool.remove(snode) // Remove it from the random snode pool LokiAPI.failureCount[snode] = 0 } case 406: print("[Loki] The user's clock is out of sync with the service node network.") throw LokiAPI.LokiAPIError.clockOutOfSync case 421: // The snode isn't associated with the given public key anymore print("[Loki] Invalidating swarm for: \(hexEncodedPublicKey).") LokiAPI.dropIfNeeded(snode, hexEncodedPublicKey: hexEncodedPublicKey) case 432: // The proof of work difficulty is too low if let powDifficulty = json["difficulty"] as? Int { print("[Loki] Setting proof of work difficulty to \(powDifficulty).") LokiAPI.powDifficulty = UInt(powDifficulty) } else { print("[Loki] Failed to update proof of work difficulty.") } break default: break } throw error } } }