Skip to contentSkip to conversation

allow PrivateKey and PresharedKey to be files too

PATCH New Michael Tokarev 1 participant 2 messages Jan 21, 2021 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/2 allow PrivateKey and PresharedKey to be files too

In the config file, allow specifying PrivateKey and PresharedKey as files, not only directly. This way the config file can be made readable since the only security-sensitive part there is the keys which can now be stored separately.

---
 src/config.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
src/config.c +12 -2
index e0b4b7c..b4280e5 100644
@@ -157,6 +157,16 @@ out:
 	return ret;
 }
 
+static inline bool parse_key_or_file(uint8_t key[static WG_KEY_LEN], const char *value)
+{
+	if (value[0] == '/' && (strlen(value) != WG_KEY_LEN_BASE64 - 1 || value[WG_KEY_LEN_BASE64 - 2] != '=')) {
+		return parse_keyfile(key, value);
+	}
+	else {
+		return parse_key(key, value);
+	}
+}
+
 static inline bool parse_ip(struct wgallowedip *allowedip, const char *value)
 {
 	allowedip->family = AF_UNSPEC;
@@ -447,7 +457,7 @@ static bool process_line(struct config_ctx *ctx, const char *line)
 		else if (key_match("FwMark"))
 			ret = parse_fwmark(&ctx->device->fwmark, &ctx->device->flags, value);
 		else if (key_match("PrivateKey")) {
-			ret = parse_key(ctx->device->private_key, value);
+			ret = parse_key_or_file(ctx->device->private_key, value);
 			if (ret)
 				ctx->device->flags |= WGDEVICE_HAS_PRIVATE_KEY;
 		} else
@@ -464,7 +474,7 @@ static bool process_line(struct config_ctx *ctx, const char *line)
 		else if (key_match("PersistentKeepalive"))
 			ret = parse_persistent_keepalive(&ctx->last_peer->persistent_keepalive_interval, &ctx->last_peer->flags, value);
 		else if (key_match("PresharedKey")) {
-			ret = parse_key(ctx->last_peer->preshared_key, value);
+			ret = parse_key_or_file(ctx->last_peer->preshared_key, value);
 			if (ret)
 				ctx->last_peer->flags |= WGPEER_HAS_PRESHARED_KEY;
 		} else
Signature · 1 line
2.20.1
#2 lore raw

PATCH 2/2 recognize and ignore commands used by wg-quick

In reply to a message not in the archive

When reading the config file, recognize and ignore commands in the [Interface] section which are used by wg-qukck. This way wg-quick config file can be read by wg too directly so there's no need to filter it out by wg-quick and it's easy to reload configuration too.

---
 src/config.c | 4 ++++
 1 file changed, 4 insertions(+)
src/config.c +4 -0
index b4280e5..0095556 100644
@@ -460,6 +460,10 @@ static bool process_line(struct config_ctx *ctx, const char *line)
 			ret = parse_key_or_file(ctx->device->private_key, value);
 			if (ret)
 				ctx->device->flags |= WGDEVICE_HAS_PRIVATE_KEY;
+		} else if (key_match("Address") || key_match("DNS") || key_match("MTU") || key_match("Table") ||
+		           key_match("PreUp") || key_match("PostUp") || key_match("PreDown") || key_match("PostDown") ||
+		           key_match("SaveConfig")) {
+			/* ignore commands used by wg-quick */
 		} else
 			goto error;
 	} else if (ctx->is_peer_section) {
Signature · 1 line
2.20.1