From ba90e41312f9b53cd98a92cd2e32f214e2cd29c9 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Wed, 25 Oct 2023 17:57:03 +0100 Subject: [PATCH] Save public key as PEM encoding --- cmd/lavender/serve.go | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/cmd/lavender/serve.go b/cmd/lavender/serve.go index 23fba2b..490feda 100644 --- a/cmd/lavender/serve.go +++ b/cmd/lavender/serve.go @@ -1,10 +1,12 @@ package main import ( + "bytes" "context" "crypto/rand" "crypto/x509" "encoding/json" + "encoding/pem" "errors" "flag" "github.com/1f349/lavender/server" @@ -73,10 +75,7 @@ func normalLoad(startUp server.Conf, wd string) { if err != nil { log.Fatal("[Lavender] Failed to load or create MJWT signer:", err) } - err = os.WriteFile("lavender.public.key", x509.MarshalPKCS1PublicKey(mSign.PublicKey()), 0600) - if err != nil && !errors.Is(err, os.ErrExist) { - log.Fatal("[Lavender] Failed to save MJWT public key:", err) - } + saveMjwtPubKey(mSign) if err := pages.LoadPages(wd); err != nil { log.Fatal("[Lavender] Failed to load page templates:", err) @@ -91,3 +90,16 @@ func normalLoad(startUp server.Conf, wd string) { _ = srv.Close() }) } + +func saveMjwtPubKey(mSign mjwt.Signer) { + pubKey := x509.MarshalPKCS1PublicKey(mSign.PublicKey()) + b := new(bytes.Buffer) + err := pem.Encode(b, &pem.Block{Type: "RSA PUBLIC KEY", Bytes: pubKey}) + if err != nil { + log.Fatal("[Lavender] Failed to encode MJWT public key:", err) + } + err = os.WriteFile("lavender.public.key", b.Bytes(), 0600) + if err != nil && !errors.Is(err, os.ErrExist) { + log.Fatal("[Lavender] Failed to save MJWT public key:", err) + } +}