From 8bd9b6192e6ca73388dfce45be0f01c68f0e1547 Mon Sep 17 00:00:00 2001 From: "I.F." <131729144+FukumotoIkuma@users.noreply.github.com> Date: Tue, 10 Nov 2026 06:47:38 +0900 Subject: [PATCH] Feat/client side key generation (#764) * feat: expose editable key capability to frontend Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * feat: expose editable keys setting Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * feat: add browser WireGuard key and config helpers Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * feat: support browser-only key generation for self provisioning Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * fix: hide server config actions without private key Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * refactor: share browser WireGuard key helpers Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * refactor: reuse WireGuard browser key helpers Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * fix: address review feedback for client-side key generation - Make generated key fields read-only so the downloaded config always matches the registered public key - Drop unreachable error handling and defensive branches; mirror wg_peer.tpl in the config builder - Keep only the keypair helper shared with KeyGeneraterView - Restore trailing newlines and remove the duplicated help text - Document browser-side key generation and add EditableKeys to the v0 swagger spec Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * test: cover EditableKeys setting and browser-generated peer keys Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> * improve ui, fix potential race condition --------- Signed-off-by: I.F. <131729144+FukumotoIkuma@users.noreply.github.com> Co-authored-by: Christoph --- docs/documentation/configuration/overview.md | 2 +- frontend/src/components/PeerViewModal.vue | 20 ++-- frontend/src/components/UserPeerEditModal.vue | 78 +++++++++++++-- frontend/src/helpers/wireguard.js | 99 +++++++++++++++++++ frontend/src/lang/translations/de.json | 4 +- frontend/src/lang/translations/en.json | 4 +- frontend/src/views/KeyGeneraterView.vue | 42 +------- .../app/api/core/assets/doc/v0_swagger.json | 3 + .../app/api/core/assets/doc/v0_swagger.yaml | 2 + .../app/api/v0/handlers/endpoint_config.go | 1 + .../api/v0/handlers/endpoint_config_test.go | 63 ++++++++++++ internal/app/api/v0/model/models.go | 1 + .../app/wireguard/wireguard_peers_test.go | 55 +++++++++++ 13 files changed, 313 insertions(+), 61 deletions(-) create mode 100644 frontend/src/helpers/wireguard.js create mode 100644 internal/app/api/v0/handlers/endpoint_config_test.go diff --git a/docs/documentation/configuration/overview.md b/docs/documentation/configuration/overview.md index cb2a6316..24480a66 100644 --- a/docs/documentation/configuration/overview.md +++ b/docs/documentation/configuration/overview.md @@ -154,7 +154,7 @@ More advanced options are found in the subsequent `Advanced` section. ### `editable_keys` - **Default:** `true` - **Environment Variable:** `WG_PORTAL_CORE_EDITABLE_KEYS` -- **Description:** Allow editing of WireGuard key-pairs directly in the UI. +- **Description:** Allow editing of WireGuard key-pairs directly in the UI. When enabled, users creating their own peers (self-provisioning) can also generate the key-pair in the browser: the private key is never sent to the server, and the WireGuard configuration file is downloaded once right after creation. It cannot be downloaded or shown again later. ### `create_default_peer` (deprecated) - **Default:** `false` diff --git a/frontend/src/components/PeerViewModal.vue b/frontend/src/components/PeerViewModal.vue index 4c4149a7..1787cdf8 100644 --- a/frontend/src/components/PeerViewModal.vue +++ b/frontend/src/components/PeerViewModal.vue @@ -47,6 +47,8 @@ const selectedPeer = computed(() => { return p }) +const hasStoredPrivateKey = computed(() => !!selectedPeer.value?.PrivateKey) + const selectedStats = computed(() => { let s = peers.Statistics(props.peerId) @@ -85,8 +87,12 @@ const configStyle = ref("wgquick") watch(() => props.visible, async (newValue, oldValue) => { if (oldValue === false && newValue === true) { // if modal is shown - await peers.LoadPeerConfig(selectedPeer.value.Identifier, configStyle.value) - configString.value = peers.configuration + if (hasStoredPrivateKey.value) { + await peers.LoadPeerConfig(selectedPeer.value.Identifier, configStyle.value) + configString.value = peers.configuration + } else { + configString.value = "" + } } }) @@ -132,7 +138,7 @@ function ConfigQrUrl() { diff --git a/frontend/src/helpers/wireguard.js b/frontend/src/helpers/wireguard.js new file mode 100644 index 00000000..78b2d3f5 --- /dev/null +++ b/frontend/src/helpers/wireguard.js @@ -0,0 +1,99 @@ +/** + * Convert a Base64URL-encoded string to standard Base64 with padding. + * @function b64urlToB64 + * @param {string} input - The Base64URL string. + * @returns {string} The padded, standard Base64 string. + */ +function b64urlToB64(input) { + let b64 = input.replace(/-/g, '+').replace(/_/g, '/'); + while (b64.length % 4) { + b64 += '='; + } + return b64; +} + +/** + * Generate an X25519 keypair using the Web Crypto API and return Base64-encoded strings. + * @async + * @function generateKeypair + * @returns {Promise<{ publicKey: string, privateKey: string }>} Resolves with an object containing + * - publicKey: the Base64-encoded public key + * - privateKey: the Base64-encoded private key + */ +export async function generateKeypair() { + // 1. Generate an X25519 key pair + const keyPair = await crypto.subtle.generateKey( + { name: 'X25519', namedCurve: 'X25519' }, + true, // extractable + ['deriveBits'] // allowed usage for ECDH + ); + + // 2. Export keys as JWK to access raw key material + const pubJwk = await crypto.subtle.exportKey('jwk', keyPair.publicKey); + const privJwk = await crypto.subtle.exportKey('jwk', keyPair.privateKey); + + // 3. Convert Base64URL to standard Base64 with padding + return { + publicKey: b64urlToB64(pubJwk.x), + privateKey: b64urlToB64(privJwk.d) + }; +} + +/** + * Build a wg-quick configuration for a peer, mirroring internal/app/configfile/tpl_files/wg_peer.tpl. + * @function buildWgQuickConfig + * @param {object} peer - The peer as returned by the API. + * @param {string} privateKey - The Base64-encoded private key that only exists in the browser. + * @returns {string} The wg-quick configuration. + */ +function buildWgQuickConfig(peer, privateKey) { + const lines = [ + '[Interface]', + `PrivateKey = ${privateKey}`, + `Address = ${peer.Addresses.join(', ')}`, + ] + + if (peer.Dns.Value.length) { + lines.push(`DNS = ${[...peer.Dns.Value, ...peer.DnsSearch.Value].join(', ')}`) + } + if (peer.Mtu.Value !== 0) lines.push(`MTU = ${peer.Mtu.Value}`) + if (peer.RoutingTable.Value !== '') lines.push(`Table = ${peer.RoutingTable.Value}`) + if (peer.FirewallMark.Value !== 0) lines.push(`FwMark = ${peer.FirewallMark.Value}`) + if (peer.PreUp.Value) lines.push(`PreUp = ${peer.PreUp.Value}`) + if (peer.PostUp.Value) lines.push(`PostUp = ${peer.PostUp.Value}`) + if (peer.PreDown.Value) lines.push(`PreDown = ${peer.PreDown.Value}`) + if (peer.PostDown.Value) lines.push(`PostDown = ${peer.PostDown.Value}`) + + lines.push( + '', + '[Peer]', + `PublicKey = ${peer.EndpointPublicKey.Value}`, + `Endpoint = ${peer.Endpoint.Value}`, + ) + + if (peer.AllowedIPs.Value.length) lines.push(`AllowedIPs = ${peer.AllowedIPs.Value.join(', ')}`) + if (peer.PresharedKey) lines.push(`PresharedKey = ${peer.PresharedKey}`) + if (peer.PersistentKeepalive.Value !== 0 && peer.Mode === 'client') { + lines.push(`PersistentKeepalive = ${peer.PersistentKeepalive.Value}`) + } + + return lines.join('\n') + '\n' +} + +/** + * Download a wg-quick configuration for a peer whose private key only exists in the browser. + * @function downloadWgQuickConfig + * @param {object} peer - The peer as returned by the API. + * @param {string} privateKey - The Base64-encoded private key. + */ +export function downloadWgQuickConfig(peer, privateKey) { + let element = document.createElement('a') + element.setAttribute('href', 'data:application/octet-stream;charset=utf-8,' + encodeURIComponent(buildWgQuickConfig(peer, privateKey))) + element.setAttribute('download', peer.Filename) + + element.style.display = 'none' + document.body.appendChild(element) + + element.click() + document.body.removeChild(element) +} diff --git a/frontend/src/lang/translations/de.json b/frontend/src/lang/translations/de.json index 3e6c96e9..5cc42d20 100644 --- a/frontend/src/lang/translations/de.json +++ b/frontend/src/lang/translations/de.json @@ -587,6 +587,8 @@ "private-key": { "label": "Privater Schlüssel", "placeholder": "Der private Schlüssel", + "local-label": "Lokal erzeugt", + "local-help": "Der private Schlüssel wurde in diesem Browser erzeugt und wird nicht an den Server gesendet. Die Konfiguration wird beim Speichern einmalig heruntergeladen.", "help": "Der private Schlüssel wird sicher auf dem Server gespeichert. Wenn der Benutzer bereits eine Kopie besitzt, kann dieses Feld entfallen. Der Server funktioniert auch ausschließlich mit dem öffentlichen Schlüssel des Peers." }, "public-key": { @@ -678,4 +680,4 @@ } } } -} \ No newline at end of file +} diff --git a/frontend/src/lang/translations/en.json b/frontend/src/lang/translations/en.json index 216b1c70..f5698270 100644 --- a/frontend/src/lang/translations/en.json +++ b/frontend/src/lang/translations/en.json @@ -587,6 +587,8 @@ "private-key": { "label": "Private Key", "placeholder": "The private key", + "local-label": "Generated locally", + "local-help": "The private key was generated in this browser and is not sent to the server. The configuration is downloaded once when you save.", "help": "The private key is stored securely on the server. If the user already holds a copy, you may omit this field. The server still functions exclusively with the peer’s public key." }, "public-key": { @@ -678,4 +680,4 @@ } } } -} \ No newline at end of file +} diff --git a/frontend/src/views/KeyGeneraterView.vue b/frontend/src/views/KeyGeneraterView.vue index db984638..960ead92 100644 --- a/frontend/src/views/KeyGeneraterView.vue +++ b/frontend/src/views/KeyGeneraterView.vue @@ -1,38 +1,12 @@