Fix incorrect padding setting

pull/148/head
gmbnt 6 years ago
parent 3c2109e698
commit ede00b1a02

@ -42,8 +42,7 @@ internal enum HTTP {
// MARK: Main // MARK: Main
internal static func execute(_ verb: Verb, _ url: String, parameters: JSON? = nil, timeout: TimeInterval = HTTP.timeout) -> Promise<JSON> { internal static func execute(_ verb: Verb, _ url: String, parameters: JSON? = nil, timeout: TimeInterval = HTTP.timeout) -> Promise<JSON> {
return Promise<JSON> { seal in return Promise<JSON> { seal in
let url = URL(string: url)! var request = URLRequest(url: URL(string: url)!)
var request = URLRequest(url: url)
request.httpMethod = verb.rawValue request.httpMethod = verb.rawValue
if let parameters = parameters { if let parameters = parameters {
do { do {

@ -43,14 +43,14 @@ public extension LokiAPI {
let target = seedNodePool.randomElement()! let target = seedNodePool.randomElement()!
let url = "\(target)/json_rpc" let url = "\(target)/json_rpc"
let parameters: JSON = [ let parameters: JSON = [
"method" : "get_service_nodes", "method" : "get_n_service_nodes",
"params" : [ "params" : [
"active_only" : true, "active_only" : true,
"fields" : [ "fields" : [
"public_ip" : true, "public_ip" : true,
"storage_port" : true, "storage_port" : true,
"pubkey_ed25519": true, "pubkey_ed25519" : true,
"pubkey_x25519": true "pubkey_x25519" : true
] ]
] ]
] ]

@ -29,12 +29,13 @@ public final class LokiAPI : NSObject {
internal static let userHexEncodedPublicKey = getUserHexEncodedPublicKey() internal static let userHexEncodedPublicKey = getUserHexEncodedPublicKey()
// MARK: Settings // MARK: Settings
private static let apiVersion = "v1" private static let maxRetryCount: UInt = 4
private static let maxRetryCount: UInt = 8
private static let defaultTimeout: TimeInterval = 20 private static let defaultTimeout: TimeInterval = 20
private static let longPollingTimeout: TimeInterval = 40 private static let longPollingTimeout: TimeInterval = 40
private static var userIDScanLimit: UInt = 4096 private static var userIDScanLimit: UInt = 4096
internal static var powDifficulty: UInt = 4
internal static var powDifficulty: UInt = 2
public static let defaultMessageTTL: UInt64 = 24 * 60 * 60 * 1000 public static let defaultMessageTTL: UInt64 = 24 * 60 * 60 * 1000
public static let deviceLinkUpdateInterval: TimeInterval = 20 public static let deviceLinkUpdateInterval: TimeInterval = 20
@ -93,8 +94,8 @@ public final class LokiAPI : NSObject {
// MARK: Internal API // MARK: Internal API
internal static func invoke(_ method: LokiAPITarget.Method, on target: LokiAPITarget, associatedWith hexEncodedPublicKey: String, internal static func invoke(_ method: LokiAPITarget.Method, on target: LokiAPITarget, associatedWith hexEncodedPublicKey: String,
parameters: [String:Any], headers: [String:String]? = nil, timeout: TimeInterval? = nil) -> RawResponsePromise { parameters: JSON, headers: [String:String]? = nil, timeout: TimeInterval? = nil) -> RawResponsePromise {
let url = URL(string: "\(target.address):\(target.port)/storage_rpc/\(apiVersion)")! let url = URL(string: "\(target.address):\(target.port)/storage_rpc/v1")!
let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ]) let request = TSRequest(url: url, method: "POST", parameters: [ "method" : method.rawValue, "params" : parameters ])
if let headers = headers { request.allHTTPHeaderFields = headers } if let headers = headers { request.allHTTPHeaderFields = headers }
request.timeoutInterval = timeout ?? defaultTimeout request.timeoutInterval = timeout ?? defaultTimeout

@ -25,7 +25,7 @@ extension OnionRequestAPI {
guard !Thread.isMainThread else { preconditionFailure("It's illegal to call encrypt(_:usingAESGCMWithSymmetricKey:) from the main thread.") } guard !Thread.isMainThread else { preconditionFailure("It's illegal to call encrypt(_:usingAESGCMWithSymmetricKey:) from the main thread.") }
let iv = try getSecureRandomData(ofSize: ivSize) let iv = try getSecureRandomData(ofSize: ivSize)
let gcm = GCM(iv: iv.bytes, tagLength: Int(gcmTagSize), mode: .combined) let gcm = GCM(iv: iv.bytes, tagLength: Int(gcmTagSize), mode: .combined)
let aes = try AES(key: symmetricKey.bytes, blockMode: gcm, padding: .pkcs7) let aes = try AES(key: symmetricKey.bytes, blockMode: gcm, padding: .noPadding)
let ciphertext = try aes.encrypt(plaintext.bytes) let ciphertext = try aes.encrypt(plaintext.bytes)
return iv + Data(bytes: ciphertext) return iv + Data(bytes: ciphertext)
} }

@ -199,7 +199,7 @@ internal enum OnionRequestAPI {
let ciphertext = ivAndCiphertext[Int(ivSize)...] let ciphertext = ivAndCiphertext[Int(ivSize)...]
do { do {
let gcm = GCM(iv: iv.bytes, tagLength: Int(gcmTagSize), mode: .combined) let gcm = GCM(iv: iv.bytes, tagLength: Int(gcmTagSize), mode: .combined)
let aes = try AES(key: targetSnodeSymmetricKey.bytes, blockMode: gcm, padding: .pkcs7) let aes = try AES(key: targetSnodeSymmetricKey.bytes, blockMode: gcm, padding: .noPadding)
let result = try aes.decrypt(ciphertext.bytes) let result = try aes.decrypt(ciphertext.bytes)
seal.fulfill(Data(bytes: result)) seal.fulfill(Data(bytes: result))
} catch (let error) { } catch (let error) {

@ -12,6 +12,7 @@ class OnionRequestAPITests : XCTestCase {
var totalSuccessRate: Double = 0 var totalSuccessRate: Double = 0
let testCount = 10 let testCount = 10
LokiAPI.getRandomSnode().then(on: OnionRequestAPI.workQueue) { snode -> Promise<LokiAPITarget> in LokiAPI.getRandomSnode().then(on: OnionRequestAPI.workQueue) { snode -> Promise<LokiAPITarget> in
print("[Loki] [Onion Request API] Target snode: \(snode).")
return OnionRequestAPI.getPath(excluding: snode).map(on: OnionRequestAPI.workQueue) { _ in snode } // Ensure we only build a path once return OnionRequestAPI.getPath(excluding: snode).map(on: OnionRequestAPI.workQueue) { _ in snode } // Ensure we only build a path once
}.done(on: OnionRequestAPI.workQueue) { snode in }.done(on: OnionRequestAPI.workQueue) { snode in
var successCount = 0 var successCount = 0

Loading…
Cancel
Save