summaryrefslogtreecommitdiffhomepage
path: root/ios
diff options
context:
space:
mode:
authorAndrej Mihajlov <and@mullvad.net>2019-12-23 13:12:26 +0100
committerAndrej Mihajlov <and@mullvad.net>2020-01-03 11:40:30 +0100
commitc2085df1311311b26beb35f7fc77fd3ceb7ff91c (patch)
tree17404a709767bd0ebcd07f4a2f74a86a1771b32d /ios
parent600eb998ae5bd89ce9b87503ee5a9900a41b1761 (diff)
downloadmullvadvpn-c2085df1311311b26beb35f7fc77fd3ceb7ff91c.tar.xz
mullvadvpn-c2085df1311311b26beb35f7fc77fd3ceb7ff91c.zip
Add WireguardPrivateKey.creationDate metadata
Diffstat (limited to 'ios')
-rw-r--r--ios/MullvadVPN/TunnelManager.swift2
-rw-r--r--ios/MullvadVPN/WireguardPrivateKey.swift59
2 files changed, 45 insertions, 16 deletions
diff --git a/ios/MullvadVPN/TunnelManager.swift b/ios/MullvadVPN/TunnelManager.swift
index dc2603dad3..cb47a09cca 100644
--- a/ios/MullvadVPN/TunnelManager.swift
+++ b/ios/MullvadVPN/TunnelManager.swift
@@ -320,7 +320,7 @@ class TunnelManager {
}
// Send wireguard key to the server
- let publicKey = tunnelConfig.interface.privateKey.publicKeyRawRepresentation
+ let publicKey = tunnelConfig.interface.privateKey.publicKey.rawRepresentation
return self.apiClient.pushWireguardKey(accountToken: accountToken, publicKey: publicKey)
.mapError { (networkError) -> SetAccountError in
diff --git a/ios/MullvadVPN/WireguardPrivateKey.swift b/ios/MullvadVPN/WireguardPrivateKey.swift
index 781e4f3917..058055e77f 100644
--- a/ios/MullvadVPN/WireguardPrivateKey.swift
+++ b/ios/MullvadVPN/WireguardPrivateKey.swift
@@ -12,43 +12,72 @@ import Foundation
/// A convenience wrapper around the wireguard key
struct WireguardPrivateKey {
- /// An inner impelementation of a private key
- private let innerPrivateKey: CryptoKit.Curve25519.KeyAgreement.PrivateKey
+ /// When the key was created
+ let creationDate: Date
/// Private key's raw representation
var rawRepresentation: Data {
- return innerPrivateKey.rawRepresentation
+ innerPrivateKey.rawRepresentation
}
- /// Public key's raw representation
- var publicKeyRawRepresentation: Data {
- return innerPrivateKey.publicKey.rawRepresentation
+ /// Public key
+ var publicKey: WireguardPublicKey {
+ WireguardPublicKey(
+ creationDate: creationDate,
+ rawRepresentation: innerPrivateKey.publicKey.rawRepresentation
+ )
}
+ /// An inner impelementation of a private key
+ private let innerPrivateKey: Curve25519.KeyAgreement.PrivateKey
+
/// Initialize the new private key
init() {
- innerPrivateKey = CryptoKit.Curve25519.KeyAgreement.PrivateKey()
+ innerPrivateKey = Curve25519.KeyAgreement.PrivateKey()
+ creationDate = Date()
}
/// Load with the existing private key
- init(rawRepresentation: Data) throws {
- innerPrivateKey = try CryptoKit.Curve25519.KeyAgreement.PrivateKey(rawRepresentation: rawRepresentation)
+ init(rawRepresentation: Data, createdAt: Date) throws {
+ innerPrivateKey = try Curve25519.KeyAgreement.PrivateKey(rawRepresentation: rawRepresentation)
+ creationDate = createdAt
}
}
+extension WireguardPrivateKey: Equatable {
+ static func == (lhs: WireguardPrivateKey, rhs: WireguardPrivateKey) -> Bool {
+ lhs.rawRepresentation == rhs.rawRepresentation
+ }
+}
+
+/// A struct holding a public key used for Wireguard with associated metadata
+struct WireguardPublicKey {
+ /// Refers to private key creation date
+ let creationDate: Date
+
+ /// Raw public key representation
+ let rawRepresentation: Data
+}
+
extension WireguardPrivateKey: Codable {
+
+ private enum CodingKeys: String, CodingKey {
+ case privateKeyData, creationDate
+ }
+
func encode(to encoder: Encoder) throws {
- var container = encoder.singleValueContainer()
+ var container = encoder.container(keyedBy: CodingKeys.self)
- try container.encode(innerPrivateKey.rawRepresentation)
+ try container.encode(innerPrivateKey.rawRepresentation, forKey: .privateKeyData)
+ try container.encode(creationDate, forKey: .creationDate)
}
init(from decoder: Decoder) throws {
- let container = try decoder.singleValueContainer()
-
- let privateKeyBytes = try container.decode(Data.self)
+ let container = try decoder.container(keyedBy: CodingKeys.self)
+ let privateKeyBytes = try container.decode(Data.self, forKey: .privateKeyData)
+ let creationDate = try container.decode(Date.self, forKey: .creationDate)
- self = try .init(rawRepresentation: privateKeyBytes)
+ self = try .init(rawRepresentation: privateKeyBytes, createdAt: creationDate)
}
}