session-ios/SignalServiceKit/src/Loki/API/Onion Requests/OnionRequestAPI.swift

264 lines
14 KiB
Swift
Raw Normal View History

2020-04-01 04:32:46 +02:00
import CryptoSwift
2020-03-30 02:35:45 +02:00
import PromiseKit
2020-03-30 06:18:55 +02:00
/// See the "Onion Requests" section of [The Session Whitepaper](https://arxiv.org/pdf/2002.04609.pdf) for more information.
2020-05-27 08:48:29 +02:00
public enum OnionRequestAPI {
public static var guardSnodes: Set<Snode> = []
public static var paths: [Path] = [] // Not a set to ensure we consistently show the same path to the user
private static var snodePool: Set<Snode> {
let unreliableSnodes = Set(SnodeAPI.snodeFailureCount.keys)
return SnodeAPI.snodePool.subtracting(unreliableSnodes)
}
2020-03-30 02:35:45 +02:00
2020-03-30 02:38:06 +02:00
// MARK: Settings
2020-03-30 02:35:45 +02:00
/// The number of snodes (including the guard snode) in a path.
2020-05-27 08:48:41 +02:00
private static let pathSize: UInt = 3
2020-05-28 03:01:42 +02:00
public static let pathCount: UInt = 2
2020-03-30 02:35:45 +02:00
private static var guardSnodeCount: UInt { return pathCount } // One per path
2020-03-30 02:38:06 +02:00
// MARK: Error
2020-03-30 02:35:45 +02:00
internal enum Error : LocalizedError {
2020-04-06 05:26:26 +02:00
case httpRequestFailedAtTargetSnode(statusCode: UInt, json: JSON)
2020-03-30 02:35:45 +02:00
case insufficientSnodes
case missingSnodeVersion
2020-03-31 05:52:56 +02:00
case randomDataGenerationFailed
2020-03-31 06:27:28 +02:00
case snodePublicKeySetMissing
case unsupportedSnodeVersion(String)
2020-03-30 02:35:45 +02:00
internal var errorDescription: String? {
2020-03-30 02:35:45 +02:00
switch self {
2020-04-06 05:26:26 +02:00
case .httpRequestFailedAtTargetSnode(let statusCode): return "HTTP request failed at target snode with status code: \(statusCode)."
2020-03-30 02:35:45 +02:00
case .insufficientSnodes: return "Couldn't find enough snodes to build a path."
case .missingSnodeVersion: return "Missing snode version."
2020-03-31 05:52:56 +02:00
case .randomDataGenerationFailed: return "Couldn't generate random data."
2020-03-31 06:27:28 +02:00
case .snodePublicKeySetMissing: return "Missing snode public key set."
case .unsupportedSnodeVersion(let version): return "Unsupported snode version: \(version)."
2020-03-30 02:35:45 +02:00
}
}
}
2020-03-30 02:38:06 +02:00
// MARK: Path
public typealias Path = [Snode]
2020-03-30 02:35:45 +02:00
// MARK: Onion Building Result
private typealias OnionBuildingResult = (guardSnode: Snode, finalEncryptionResult: EncryptionResult, targetSnodeSymmetricKey: Data)
2020-03-30 02:35:45 +02:00
// MARK: Private API
2020-03-30 06:18:55 +02:00
/// Tests the given snode. The returned promise errors out if the snode is faulty; the promise is fulfilled otherwise.
private static func testSnode(_ snode: Snode) -> Promise<Void> {
2020-03-31 07:16:26 +02:00
let (promise, seal) = Promise<Void>.pending()
2020-06-11 08:33:11 +02:00
DispatchQueue.global(qos: .userInitiated).async {
let url = "\(snode.address):\(snode.port)/get_stats/v1"
2020-05-27 08:48:29 +02:00
let timeout: TimeInterval = 3 // Use a shorter timeout for testing
2020-06-11 08:33:11 +02:00
HTTP.execute(.get, url, timeout: timeout).done2 { 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))
}
2020-06-11 08:33:11 +02:00
}.catch2 { error in
2020-03-31 07:16:26 +02:00
seal.reject(error)
}
}
return promise
2020-03-30 02:35:45 +02:00
}
2020-03-31 05:52:56 +02:00
/// Finds `guardSnodeCount` guard snodes to use for path building. The returned promise errors out with `Error.insufficientSnodes`
2020-03-30 02:35:45 +02:00
/// if not enough (reliable) snodes are available.
private static func getGuardSnodes() -> Promise<Set<Snode>> {
if guardSnodes.count >= guardSnodeCount {
return Promise<Set<Snode>> { $0.fulfill(guardSnodes) }
2020-03-30 02:35:45 +02:00
} else {
2020-03-30 06:18:55 +02:00
print("[Loki] [Onion Request API] Populating guard snode cache.")
return SnodeAPI.getRandomSnode().then2 { _ -> Promise<Set<Snode>> in // Just used to populate the snode pool
2020-04-07 03:18:55 +02:00
var unusedSnodes = snodePool // Sync on LokiAPI.workQueue
guard unusedSnodes.count >= guardSnodeCount else { throw Error.insufficientSnodes }
func getGuardSnode() -> Promise<Snode> {
2020-03-30 02:35:45 +02:00
// randomElement() uses the system's default random generator, which is cryptographically secure
guard let candidate = unusedSnodes.randomElement() else { return Promise<Snode> { $0.reject(Error.insufficientSnodes) } }
unusedSnodes.remove(candidate) // All used snodes should be unique
print("[Loki] [Onion Request API] Testing guard snode: \(candidate).")
2020-03-31 06:27:28 +02:00
// Loop until a reliable guard snode is found
2020-06-17 03:25:28 +02:00
return testSnode(candidate).map2 { candidate }.recover(on: DispatchQueue.main) { _ in
withDelay(0.25, completionQueue: SnodeAPI.workQueue) { getGuardSnode() }
2020-06-17 03:25:28 +02:00
}
2020-03-30 02:35:45 +02:00
}
let promises = (0..<guardSnodeCount).map { _ in getGuardSnode() }
2020-06-11 08:33:11 +02:00
return when(fulfilled: promises).map2 { guardSnodes in
2020-03-30 02:35:45 +02:00
let guardSnodesAsSet = Set(guardSnodes)
OnionRequestAPI.guardSnodes = guardSnodesAsSet
return guardSnodesAsSet
}
}
}
}
2020-03-31 05:52:56 +02:00
/// Builds and returns `pathCount` paths. The returned promise errors out with `Error.insufficientSnodes`
2020-03-30 02:35:45 +02:00
/// if not enough (reliable) snodes are available.
2020-06-03 01:03:29 +02:00
private static func buildPaths() -> Promise<[Path]> {
2020-03-30 06:18:55 +02:00
print("[Loki] [Onion Request API] Building onion request paths.")
2020-05-28 03:01:42 +02:00
DispatchQueue.main.async {
NotificationCenter.default.post(name: .buildingPaths, object: nil)
}
return SnodeAPI.getRandomSnode().then2 { _ -> Promise<[Path]> in // Just used to populate the snode pool
2020-06-11 08:33:11 +02:00
return getGuardSnodes().map2 { guardSnodes -> [Path] in
var unusedSnodes = snodePool.subtracting(guardSnodes)
2020-04-01 06:31:04 +02:00
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
2020-05-27 08:48:29 +02:00
return guardSnodes.map { guardSnode in
let result = [ guardSnode ] + (0..<(pathSize - 1)).map { _ in
// randomElement() uses the system's default random generator, which is cryptographically secure
2020-04-08 08:32:07 +02:00
let pathSnode = unusedSnodes.randomElement()! // Safe because of the pathSnodeCount check above
unusedSnodes.remove(pathSnode) // All used snodes should be unique
return pathSnode
}
2020-03-31 07:16:26 +02:00
print("[Loki] [Onion Request API] Built new onion request path: \(result.prettifiedDescription).")
return result
2020-05-27 08:48:29 +02:00
}
2020-06-11 08:33:11 +02:00
}.map2 { paths in
2020-05-28 03:01:42 +02:00
OnionRequestAPI.paths = paths
try! Storage.writeSync { transaction in
print("[Loki] Persisting onion request paths to database.")
OWSPrimaryStorage.shared().setOnionRequestPaths(paths, in: transaction)
}
2020-05-28 03:01:42 +02:00
DispatchQueue.main.async {
NotificationCenter.default.post(name: .pathsBuilt, object: nil)
}
return paths
2020-03-30 02:35:45 +02:00
}
}
}
2020-03-30 06:58:16 +02:00
2020-03-31 06:27:28 +02:00
/// Returns a `Path` to be used for building an onion request. Builds new paths as needed.
2020-04-02 07:10:34 +02:00
///
/// - Note: Exposed for testing purposes.
internal static func getPath(excluding snode: Snode) -> Promise<Path> {
2020-04-02 07:30:42 +02:00
guard pathSize >= 1 else { preconditionFailure("Can't build path of size zero.") }
if paths.count < pathCount {
let storage = OWSPrimaryStorage.shared()
storage.dbReadConnection.read { transaction in
paths = storage.getOnionRequestPaths(in: transaction)
2020-06-03 03:38:43 +02:00
if paths.count >= pathCount {
guardSnodes.formUnion([ paths[0][0], paths[1][0] ])
}
}
}
2020-03-30 02:35:45 +02:00
// randomElement() uses the system's default random generator, which is cryptographically secure
if paths.count >= pathCount {
return Promise<Path> { seal in
seal.fulfill(paths.filter { !$0.contains(snode) }.randomElement()!)
}
2020-03-30 02:35:45 +02:00
} else {
2020-06-11 08:33:11 +02:00
return buildPaths().map2 { paths in
2020-05-28 03:01:42 +02:00
return paths.filter { !$0.contains(snode) }.randomElement()!
2020-03-30 02:35:45 +02:00
}
}
}
2020-03-30 06:18:55 +02:00
2020-06-03 01:03:29 +02:00
private static func dropAllPaths() {
paths.removeAll()
try! Storage.writeSync { transaction in
OWSPrimaryStorage.shared().clearOnionRequestPaths(in: transaction)
}
}
private static func dropGuardSnode(_ snode: Snode) {
2020-04-14 01:55:43 +02:00
guardSnodes = guardSnodes.filter { $0 != snode }
}
2020-03-31 05:52:56 +02:00
/// Builds an onion around `payload` and returns the result.
private static func buildOnion(around payload: JSON, targetedAt snode: Snode) -> Promise<OnionBuildingResult> {
var guardSnode: Snode!
2020-04-02 00:53:03 +02:00
var targetSnodeSymmetricKey: Data! // Needed by invoke(_:on:with:) to decrypt the response sent back by the target snode
var encryptionResult: EncryptionResult!
2020-06-11 08:33:11 +02:00
return getPath(excluding: snode).then2 { path -> Promise<EncryptionResult> in
2020-03-31 05:52:56 +02:00
guardSnode = path.first!
2020-04-02 00:53:03 +02:00
// Encrypt in reverse order, i.e. the target snode first
2020-06-11 08:33:11 +02:00
return encrypt(payload, forTargetSnode: snode).then2 { r -> Promise<EncryptionResult> in
2020-04-02 00:53:03 +02:00
targetSnodeSymmetricKey = r.symmetricKey
// Recursively encrypt the layers of the onion (again in reverse order)
encryptionResult = r
2020-03-31 05:52:56 +02:00
var path = path
2020-03-30 06:18:55 +02:00
var rhs = snode
2020-03-31 05:52:56 +02:00
func addLayer() -> Promise<EncryptionResult> {
2020-03-30 06:18:55 +02:00
if path.isEmpty {
2020-03-31 05:52:56 +02:00
return Promise<EncryptionResult> { $0.fulfill(encryptionResult) }
2020-03-30 06:18:55 +02:00
} else {
let lhs = path.removeLast()
2020-06-11 08:33:11 +02:00
return OnionRequestAPI.encryptHop(from: lhs, to: rhs, using: encryptionResult).then2 { r -> Promise<EncryptionResult> in
2020-03-31 06:27:28 +02:00
encryptionResult = r
2020-03-30 06:18:55 +02:00
rhs = lhs
2020-03-31 05:52:56 +02:00
return addLayer()
2020-03-30 06:18:55 +02:00
}
}
}
2020-03-31 05:52:56 +02:00
return addLayer()
2020-03-30 06:18:55 +02:00
}
2020-06-11 08:33:11 +02:00
}.map2 { _ in (guardSnode, encryptionResult, targetSnodeSymmetricKey) }
2020-03-31 05:52:56 +02:00
}
// MARK: Internal API
2020-03-31 06:27:28 +02:00
/// Sends an onion request to `snode`. Builds new paths as needed.
internal static func sendOnionRequest(invoking method: Snode.Method, on snode: Snode, with parameters: JSON, associatedWith publicKey: String) -> Promise<JSON> {
2020-04-06 03:47:04 +02:00
let (promise, seal) = Promise<JSON>.pending()
var guardSnode: Snode!
2020-06-11 08:33:11 +02:00
DispatchQueue.global(qos: .userInitiated).async {
2020-04-02 03:19:10 +02:00
let payload: JSON = [ "method" : method.rawValue, "params" : parameters ]
2020-06-11 08:33:11 +02:00
buildOnion(around: payload, targetedAt: snode).done2 { intermediate in
2020-04-06 05:26:26 +02:00
guardSnode = intermediate.guardSnode
let url = "\(guardSnode.address):\(guardSnode.port)/onion_req"
2020-04-02 03:19:10 +02:00
let finalEncryptionResult = intermediate.finalEncryptionResult
let onion = finalEncryptionResult.ciphertext
2020-03-31 07:49:07 +02:00
let parameters: JSON = [
"ciphertext" : onion.base64EncodedString(),
2020-04-02 03:19:10 +02:00
"ephemeral_key" : finalEncryptionResult.ephemeralPublicKey.toHexString()
2020-03-31 07:49:07 +02:00
]
2020-04-02 00:53:03 +02:00
let targetSnodeSymmetricKey = intermediate.targetSnodeSymmetricKey
2020-06-11 08:33:11 +02:00
HTTP.execute(.post, url, parameters: parameters).done2 { rawResponse in
2020-04-01 04:32:46 +02:00
guard let json = rawResponse as? JSON, let base64EncodedIVAndCiphertext = json["result"] as? String,
let ivAndCiphertext = Data(base64Encoded: base64EncodedIVAndCiphertext) else { return seal.reject(HTTP.Error.invalidJSON) }
2020-04-01 04:32:46 +02:00
let iv = ivAndCiphertext[0..<Int(ivSize)]
2020-04-01 05:40:15 +02:00
let ciphertext = ivAndCiphertext[Int(ivSize)...]
2020-04-01 04:32:46 +02:00
do {
2020-04-01 05:40:15 +02:00
let gcm = GCM(iv: iv.bytes, tagLength: Int(gcmTagSize), mode: .combined)
2020-04-06 03:11:24 +02:00
let aes = try AES(key: targetSnodeSymmetricKey.bytes, blockMode: gcm, padding: .noPadding)
2020-04-06 03:47:04 +02:00
let data = Data(try aes.decrypt(ciphertext.bytes))
guard let json = try JSONSerialization.jsonObject(with: data, options: [ .fragmentsAllowed ]) as? JSON,
let bodyAsString = json["body"] as? String, let statusCode = json["status"] as? Int else { return seal.reject(HTTP.Error.invalidJSON) }
if statusCode == 406 { // Clock out of sync
print("[Loki] The user's clock is out of sync with the service node network.")
seal.reject(SnodeAPI.SnodeAPIError.clockOutOfSync)
} else {
guard let bodyAsData = bodyAsString.data(using: .utf8),
let body = try JSONSerialization.jsonObject(with: bodyAsData, options: [ .fragmentsAllowed ]) as? JSON else { return seal.reject(HTTP.Error.invalidJSON) }
guard 200...299 ~= statusCode else { return seal.reject(Error.httpRequestFailedAtTargetSnode(statusCode: UInt(statusCode), json: body)) }
seal.fulfill(body)
}
2020-04-01 04:32:46 +02:00
} catch (let error) {
seal.reject(error)
}
2020-06-11 08:33:11 +02:00
}.catch2 { error in
seal.reject(error)
2020-03-30 06:18:55 +02:00
}
2020-06-11 08:33:11 +02:00
}.catch2 { error in
seal.reject(error)
2020-03-30 06:18:55 +02:00
}
}
2020-06-11 08:33:11 +02:00
promise.catch2 { error in // Must be invoked on LokiAPI.workQueue
2020-04-06 05:26:26 +02:00
guard case HTTP.Error.httpRequestFailed(_, _) = error else { return }
2020-06-03 01:03:29 +02:00
dropAllPaths() // A snode in the path is bad; retry with a different path
2020-04-14 01:55:43 +02:00
dropGuardSnode(guardSnode)
2020-04-06 05:26:26 +02:00
}
promise.recover2 { error -> Promise<JSON> in
2020-04-06 05:33:15 +02:00
guard case OnionRequestAPI.Error.httpRequestFailedAtTargetSnode(let statusCode, let json) = error else { throw error }
throw SnodeAPI.handleError(withStatusCode: statusCode, json: json, forSnode: snode, associatedWith: publicKey) ?? error
2020-04-06 05:26:26 +02:00
}
return promise
2020-03-30 06:18:55 +02:00
}
2020-03-30 02:35:45 +02:00
}