From 3e72dcade8a117672ae74c3fb4ebc6657cb04b6c Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Sat, 20 Oct 2018 16:05:25 +0530 Subject: [PATCH] Model: Make it impossible to create invalid interface / peer configuration instances --- WireGuard/WireGuard/Model/Configuration.swift | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/WireGuard/WireGuard/Model/Configuration.swift b/WireGuard/WireGuard/Model/Configuration.swift index 79018ca..152d30a 100644 --- a/WireGuard/WireGuard/Model/Configuration.swift +++ b/WireGuard/WireGuard/Model/Configuration.swift @@ -25,20 +25,31 @@ class InterfaceConfiguration: Codable { var listenPort: UInt64? = nil var mtu: UInt64? = nil var dns: String? = nil + init(name: String, privateKey: Data) { self.name = name self.privateKey = privateKey + if (name.isEmpty) { fatalError("Empty name") } + if (privateKey.count != 32) { fatalError("Invalid private key") } } } @available(OSX 10.14, iOS 12.0, *) class PeerConfiguration: Codable { var publicKey: Data - var preSharedKey: Data? + var preSharedKey: Data? { + didSet(value) { + if let value = value { + if (value.count != 32) { fatalError("Invalid pre-shared key") } + } + } + } var allowedIPs: [IPAddressRange] = [] var endpoint: Endpoint? var persistentKeepAlive: UInt64? + init(publicKey: Data) { self.publicKey = publicKey + if (publicKey.count != 32) { fatalError("Invalid public key") } } }