|
|
|
@ -17,27 +17,42 @@ internal extension Crypto.Generator {
|
|
|
|
|
id: "sessionId_for_ONS_response",
|
|
|
|
|
args: [name, response]
|
|
|
|
|
) {
|
|
|
|
|
guard let hexEncodedNonce: String = response.result.nonce else {
|
|
|
|
|
guard var cName: [CChar] = name.lowercased().cString(using: .utf8) else {
|
|
|
|
|
throw SnodeAPIError.onsDecryptionFailed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Name must be in lowercase
|
|
|
|
|
var cCiphertext: [UInt8] = Array(Data(hex: response.result.encryptedValue))
|
|
|
|
|
var cNonce: [UInt8] = Array(Data(hex: hexEncodedNonce))
|
|
|
|
|
var cSessionId: [CChar] = [CChar](repeating: 0, count: 67)
|
|
|
|
|
|
|
|
|
|
guard
|
|
|
|
|
cNonce.count == 24,
|
|
|
|
|
var cName: [CChar] = name.lowercased().cString(using: .utf8),
|
|
|
|
|
session_decrypt_ons_response(
|
|
|
|
|
&cName,
|
|
|
|
|
cName.count,
|
|
|
|
|
&cCiphertext,
|
|
|
|
|
cCiphertext.count,
|
|
|
|
|
&cNonce,
|
|
|
|
|
&cSessionId
|
|
|
|
|
)
|
|
|
|
|
else { throw SnodeAPIError.onsDecryptionFailed }
|
|
|
|
|
// Need to switch on `result.nonce` and explciitly pass `nil` because passing an optional
|
|
|
|
|
// to a C function doesn't seem to work correctly
|
|
|
|
|
switch response.result.nonce {
|
|
|
|
|
case .none:
|
|
|
|
|
guard
|
|
|
|
|
session_decrypt_ons_response(
|
|
|
|
|
&cName,
|
|
|
|
|
&cCiphertext,
|
|
|
|
|
cCiphertext.count,
|
|
|
|
|
nil,
|
|
|
|
|
&cSessionId
|
|
|
|
|
)
|
|
|
|
|
else { throw SnodeAPIError.onsDecryptionFailed }
|
|
|
|
|
|
|
|
|
|
case .some(let nonce):
|
|
|
|
|
var cNonce: [UInt8] = Array(Data(hex: nonce))
|
|
|
|
|
|
|
|
|
|
guard
|
|
|
|
|
cNonce.count == 24,
|
|
|
|
|
session_decrypt_ons_response(
|
|
|
|
|
&cName,
|
|
|
|
|
&cCiphertext,
|
|
|
|
|
cCiphertext.count,
|
|
|
|
|
&cNonce,
|
|
|
|
|
&cSessionId
|
|
|
|
|
)
|
|
|
|
|
else { throw SnodeAPIError.onsDecryptionFailed }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return String(cString: cSessionId)
|
|
|
|
|
}
|
|
|
|
|