Save public key as PEM encoding

This commit is contained in:
Melon 2023-10-25 17:57:03 +01:00
parent 9af891dd68
commit ba90e41312
Signed by: melon
GPG Key ID: 6C9D970C50D26A25

View File

@ -1,10 +1,12 @@
package main package main
import ( import (
"bytes"
"context" "context"
"crypto/rand" "crypto/rand"
"crypto/x509" "crypto/x509"
"encoding/json" "encoding/json"
"encoding/pem"
"errors" "errors"
"flag" "flag"
"github.com/1f349/lavender/server" "github.com/1f349/lavender/server"
@ -73,10 +75,7 @@ func normalLoad(startUp server.Conf, wd string) {
if err != nil { if err != nil {
log.Fatal("[Lavender] Failed to load or create MJWT signer:", err) log.Fatal("[Lavender] Failed to load or create MJWT signer:", err)
} }
err = os.WriteFile("lavender.public.key", x509.MarshalPKCS1PublicKey(mSign.PublicKey()), 0600) saveMjwtPubKey(mSign)
if err != nil && !errors.Is(err, os.ErrExist) {
log.Fatal("[Lavender] Failed to save MJWT public key:", err)
}
if err := pages.LoadPages(wd); err != nil { if err := pages.LoadPages(wd); err != nil {
log.Fatal("[Lavender] Failed to load page templates:", err) log.Fatal("[Lavender] Failed to load page templates:", err)
@ -91,3 +90,16 @@ func normalLoad(startUp server.Conf, wd string) {
_ = srv.Close() _ = 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)
}
}