Fix onion request encryption

pull/148/head
gmbnt 4 years ago
parent 358b943d7e
commit 4e00d30d05

@ -37,9 +37,8 @@ extension OnionRequestAPI {
let snodeX25519PublicKey = Data(hex: hexEncodedSnodeX25519PublicKey)
let ephemeralKeyPair = Curve25519.generateKeyPair()
let ephemeralSharedSecret = try Curve25519.generateSharedSecret(fromPublicKey: snodeX25519PublicKey, privateKey: ephemeralKeyPair.privateKey)
let password = "LOKI"
let key = try HKDF(password: password.bytes, variant: .sha256).calculate()
let symmetricKey = try HMAC(key: key, variant: .sha256).authenticate(ephemeralSharedSecret.bytes)
let key = "LOKI"
let symmetricKey = try HMAC(key: key.bytes, variant: .sha256).authenticate(ephemeralSharedSecret.bytes)
let ciphertext = try encrypt(plaintext, usingAESGCMWithSymmetricKey: Data(bytes: symmetricKey))
return (ciphertext, Data(bytes: symmetricKey), ephemeralKeyPair.publicKey)
}

@ -97,20 +97,23 @@ internal enum OnionRequestAPI {
var json: JSON? = nil
if let j = try? JSONSerialization.jsonObject(with: data, options: []) as? JSON {
json = j
} else if let message = String(data: data, encoding: .utf8) {
json = [ "message" : message ]
} else if let result = String(data: data, encoding: .utf8) {
json = [ "result" : result ]
}
let jsonDescription = json?.prettifiedDescription ?? "no debugging info provided"
print("[Loki] [Onion Request API] \(verb.rawValue) request to \(url) failed with status code: \(statusCode) (\(jsonDescription)).")
return seal.reject(Error.httpRequestFailed(statusCode: statusCode, json: json))
}
do {
let json = try JSONSerialization.jsonObject(with: data, options: [])
seal.fulfill(json)
} catch (let error) {
var json: JSON! = nil
if let j = try? JSONSerialization.jsonObject(with: data, options: []) as? JSON {
json = j
} else if let result = String(data: data, encoding: .utf8) {
json = [ "result" : result ]
} else {
print("[Loki] [Onion Request API] Couldn't parse JSON returned by \(verb.rawValue) request to \(url).")
seal.reject(error)
return seal.reject(Error.invalidJSON)
}
seal.fulfill(json)
}
task.resume()
}

Loading…
Cancel
Save