aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRoopesh Chander <roop@roopc.net>2018-12-08 18:52:11 +0530
committerRoopesh Chander <roop@roopc.net>2018-12-09 14:07:03 +0530
commit60e13ddbf6e0d9fc03b57806043dbf0266a8cf70 (patch)
tree6a77e77358b5d9b25231d70058732aafaf7b486f
parentTunnelsManager: Observe status for all tunnels in one block (diff)
downloadwireguard-apple-60e13ddbf6e0d9fc03b57806043dbf0266a8cf70.tar.xz
wireguard-apple-60e13ddbf6e0d9fc03b57806043dbf0266a8cf70.zip
Model: Declare keyLength constant and use that wherever applicable
Signed-off-by: Roopesh Chander <roop@roopc.net>
-rw-r--r--WireGuard/Shared/Model/Configuration.swift9
-rw-r--r--WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift6
-rw-r--r--WireGuard/WireGuard/Crypto/Curve25519.swift13
-rw-r--r--WireGuard/WireGuard/UI/TunnelViewModel.swift8
4 files changed, 21 insertions, 15 deletions
diff --git a/WireGuard/Shared/Model/Configuration.swift b/WireGuard/Shared/Model/Configuration.swift
index 41ff7bc0..d2680cbe 100644
--- a/WireGuard/Shared/Model/Configuration.swift
+++ b/WireGuard/Shared/Model/Configuration.swift
@@ -7,6 +7,9 @@ import Foundation
final class TunnelConfiguration: Codable {
var interface: InterfaceConfiguration
let peers: [PeerConfiguration]
+
+ static let keyLength: Int = 32
+
init(interface: InterfaceConfiguration, peers: [PeerConfiguration]) {
self.interface = interface
self.peers = peers
@@ -32,7 +35,7 @@ struct InterfaceConfiguration: Codable {
self.name = name
self.privateKey = privateKey
if (name.isEmpty) { fatalError("Empty name") }
- if (privateKey.count != 32) { fatalError("Invalid private key") }
+ if (privateKey.count != TunnelConfiguration.keyLength) { fatalError("Invalid private key") }
}
}
@@ -42,7 +45,7 @@ struct PeerConfiguration: Codable {
var preSharedKey: Data? {
didSet(value) {
if let value = value {
- if (value.count != 32) { fatalError("Invalid preshared key") }
+ if (value.count != TunnelConfiguration.keyLength) { fatalError("Invalid preshared key") }
}
}
}
@@ -52,6 +55,6 @@ struct PeerConfiguration: Codable {
init(publicKey: Data) {
self.publicKey = publicKey
- if (publicKey.count != 32) { fatalError("Invalid public key") }
+ if (publicKey.count != TunnelConfiguration.keyLength) { fatalError("Invalid public key") }
}
}
diff --git a/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift b/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift
index af3baf0e..4cba816c 100644
--- a/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift
+++ b/WireGuard/WireGuard/ConfigFile/WgQuickConfigFileParser.swift
@@ -27,7 +27,7 @@ class WgQuickConfigFileParser {
func collate(interfaceAttributes attributes: [String: String]) -> InterfaceConfiguration? {
// required wg fields
guard let privateKeyString = attributes["privatekey"] else { return nil }
- guard let privateKey = Data(base64Encoded: privateKeyString), privateKey.count == 32 else { return nil }
+ guard let privateKey = Data(base64Encoded: privateKeyString), privateKey.count == TunnelConfiguration.keyLength else { return nil }
var interface = InterfaceConfiguration(name: name, privateKey: privateKey)
// other wg fields
if let listenPortString = attributes["listenport"] {
@@ -63,11 +63,11 @@ class WgQuickConfigFileParser {
func collate(peerAttributes attributes: [String: String]) -> PeerConfiguration? {
// required wg fields
guard let publicKeyString = attributes["publickey"] else { return nil }
- guard let publicKey = Data(base64Encoded: publicKeyString), publicKey.count == 32 else { return nil }
+ guard let publicKey = Data(base64Encoded: publicKeyString), publicKey.count == TunnelConfiguration.keyLength else { return nil }
var peer = PeerConfiguration(publicKey: publicKey)
// wg fields
if let preSharedKeyString = attributes["presharedkey"] {
- guard let preSharedKey = Data(base64Encoded: preSharedKeyString), preSharedKey.count == 32 else { return nil }
+ guard let preSharedKey = Data(base64Encoded: preSharedKeyString), preSharedKey.count == TunnelConfiguration.keyLength else { return nil }
peer.preSharedKey = preSharedKey
}
if let allowedIPsString = attributes["allowedips"] {
diff --git a/WireGuard/WireGuard/Crypto/Curve25519.swift b/WireGuard/WireGuard/Crypto/Curve25519.swift
index 84c35d3b..43d9b00d 100644
--- a/WireGuard/WireGuard/Crypto/Curve25519.swift
+++ b/WireGuard/WireGuard/Crypto/Curve25519.swift
@@ -4,24 +4,27 @@
import UIKit
struct Curve25519 {
+
+ static let keyLength: Int = 32
+
static func generatePrivateKey() -> Data {
- var privateKey = Data(repeating: 0, count: 32)
+ var privateKey = Data(repeating: 0, count: TunnelConfiguration.keyLength)
privateKey.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) in
curve25519_generate_private_key(bytes)
}
- assert(privateKey.count == 32)
+ assert(privateKey.count == TunnelConfiguration.keyLength)
return privateKey
}
static func generatePublicKey(fromPrivateKey privateKey: Data) -> Data {
- assert(privateKey.count == 32)
- var publicKey = Data(repeating: 0, count: 32)
+ assert(privateKey.count == TunnelConfiguration.keyLength)
+ var publicKey = Data(repeating: 0, count: TunnelConfiguration.keyLength)
privateKey.withUnsafeBytes { (privateKeyBytes: UnsafePointer<UInt8>) in
publicKey.withUnsafeMutableBytes { (bytes: UnsafeMutablePointer<UInt8>) in
curve25519_derive_public_key(bytes, privateKeyBytes)
}
}
- assert(publicKey.count == 32)
+ assert(publicKey.count == TunnelConfiguration.keyLength)
return publicKey
}
}
diff --git a/WireGuard/WireGuard/UI/TunnelViewModel.swift b/WireGuard/WireGuard/UI/TunnelViewModel.swift
index de14ad5a..92a1a646 100644
--- a/WireGuard/WireGuard/UI/TunnelViewModel.swift
+++ b/WireGuard/WireGuard/UI/TunnelViewModel.swift
@@ -65,7 +65,7 @@ class TunnelViewModel {
if (field == .privateKey) {
if (stringValue.count == TunnelViewModel.keyLengthInBase64),
let privateKey = Data(base64Encoded: stringValue),
- privateKey.count == 32 {
+ privateKey.count == TunnelConfiguration.keyLength {
let publicKey = Curve25519.generatePublicKey(fromPrivateKey: privateKey)
scratchpad[.publicKey] = publicKey.base64EncodedString()
} else {
@@ -109,7 +109,7 @@ class TunnelViewModel {
fieldsWithError.insert(.privateKey)
return .error("Interface's private key is required")
}
- guard let privateKey = Data(base64Encoded: privateKeyString), privateKey.count == 32 else {
+ guard let privateKey = Data(base64Encoded: privateKeyString), privateKey.count == TunnelConfiguration.keyLength else {
fieldsWithError.insert(.privateKey)
return .error("Interface's private key must be a 32-byte key in base64 encoding")
}
@@ -247,14 +247,14 @@ class TunnelViewModel {
fieldsWithError.insert(.publicKey)
return .error("Peer's public key is required")
}
- guard let publicKey = Data(base64Encoded: publicKeyString), publicKey.count == 32 else {
+ guard let publicKey = Data(base64Encoded: publicKeyString), publicKey.count == TunnelConfiguration.keyLength else {
fieldsWithError.insert(.publicKey)
return .error("Peer's public key must be a 32-byte key in base64 encoding")
}
var config = PeerConfiguration(publicKey: publicKey)
var errorMessages: [String] = []
if let preSharedKeyString = scratchpad[.preSharedKey] {
- if let preSharedKey = Data(base64Encoded: preSharedKeyString), preSharedKey.count == 32 {
+ if let preSharedKey = Data(base64Encoded: preSharedKeyString), preSharedKey.count == TunnelConfiguration.keyLength {
config.preSharedKey = preSharedKey
} else {
fieldsWithError.insert(.preSharedKey)