Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix VPN rekeying #716

Merged
merged 2 commits into from
Mar 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 17 additions & 8 deletions Sources/NetworkProtection/NetworkProtectionDeviceManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,15 +138,28 @@ public actor NetworkProtectionDeviceManager: NetworkProtectionDeviceManagement {
if regenerateKey {
keyPair = keyStore.newKeyPair()
} else {
keyPair = keyStore.currentKeyPair() ?? keyStore.newKeyPair()
// Temporary code added on 2024-03-12 to fix a previous issue where users had a really long
// key expiration date. We should remove this after a month or so.
if let existingKeyPair = keyStore.currentKeyPair(),
existingKeyPair.expirationDate > Date().addingTimeInterval(TimeInterval.day) {

keyPair = keyStore.newKeyPair()
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the expiration date is over a day from now, we create a new key pair. This is to fix users with long expiration dates... although this could be a good security measure in case users are manipulating expiration dates.

} else {
// This is the regular code to restore when the above code is removed.
keyPair = keyStore.currentKeyPair() ?? keyStore.newKeyPair()
}
}

let (selectedServer, newExpiration) = try await register(keyPair: keyPair, selectionMethod: selectionMethod)
os_log("Server registration successul", log: .networkProtection)

keyStore.updateKeyPair(keyPair)

if let newExpiration {
// We only update the expiration date if it happens before our client-set expiration date.
// This way we respect the client-set expiration date, unless the server has set an earlier
// expiration for whatever reason (like if the subscription is known to expire).
//
if let newExpiration, newExpiration < keyPair.expirationDate {
Copy link
Contributor Author

@diegoreymendez diegoreymendez Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comments are self-explanatory, never extend the local expiration, but always shorten it if the server considers that important.

keyPair = KeyPair(privateKey: keyPair.privateKey, expirationDate: newExpiration)
keyStore.updateKeyPair(keyPair)
}
Expand Down Expand Up @@ -224,9 +237,7 @@ public actor NetworkProtectionDeviceManager: NetworkProtectionDeviceManagement {
// If we're looking to exclude a server we should have a few other options available. If we can't find any
// then it means theres an inconsistency in the server list that was returned.
errorEvents?.fire(NetworkProtectionError.serverListInconsistency)

let cachedServer = try cachedServer(registeredWith: keyPair)
return (cachedServer, nil)
throw NetworkProtectionError.serverListInconsistency
}

selectedServer = registeredServer
Expand All @@ -238,9 +249,7 @@ public actor NetworkProtectionDeviceManager: NetworkProtectionDeviceManagement {
}

handle(clientError: error)

let cachedServer = try cachedServer(registeredWith: keyPair)
Copy link
Contributor Author

@diegoreymendez diegoreymendez Mar 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will need more cleanup as this code isn't used anymore, but not in this hotfix.

I'm removing this because:

  1. If the auth token is bad this code doesn't help.
  2. This code hides the actual backend issue from pixels.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good find, the try in the cachedServer call does indeed mask the real issue.

return (cachedServer, nil)
throw error
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/NetworkProtection/PacketTunnelProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ open class PacketTunnelProvider: NEPacketTunnelProvider {
serverSelectionMethod: currentServerSelectionMethod,
includedRoutes: includedRoutes ?? [],
excludedRoutes: settings.excludedRanges,
regenerateKey: false)
regenerateKey: true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Always use a new key when starting up the tunnel. Why not?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One downside here is that it'll make the NetP connection speed overall feel slower since it has to do extra work to get started.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure... after all we're not saving a call to register right now.

startTunnel(with: tunnelConfiguration, onDemand: onDemand, completionHandler: completionHandler)
os_log("🔵 Done generating tunnel config", log: .networkProtection, type: .info)
} catch {
Expand Down
Loading