Skip to content

Commit

Permalink
feat: generate self signed certs if no certs are detected
Browse files Browse the repository at this point in the history
  • Loading branch information
HilkopterBob committed Aug 20, 2024
1 parent aab797e commit 75ed0d4
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
certs/**
!certs/*.go
80 changes: 80 additions & 0 deletions certs/generate-certs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package certs

import (
"crypto/ecdsa"
"crypto/elliptic"
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"os"
"time"
)

func CreateSelfSignedCert(certFile, keyFile string) error {
// Generate a private key
priv, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
return fmt.Errorf("failed to generate private key: %v", err)
}

// Create a certificate template
notBefore := time.Now()
notAfter := notBefore.Add(365 * 24 * time.Hour)

serialNumber, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
if err != nil {
return fmt.Errorf("failed to generate serial number: %v", err)
}

template := x509.Certificate{
SerialNumber: serialNumber,
Subject: pkix.Name{
Organization: []string{"Self-Signed Co"},
},
NotBefore: notBefore,
NotAfter: notAfter,
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
ExtKeyUsage: []x509.ExtKeyUsage{
x509.ExtKeyUsageServerAuth,
},
BasicConstraintsValid: true,
}

// Generate a self-signed certificate
certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
if err != nil {
return fmt.Errorf("failed to create certificate: %v", err)
}

// Save the certificate to certFile
certOut, err := os.Create(certFile)
if err != nil {
return fmt.Errorf("failed to open cert.pem for writing: %v", err)
}
defer certOut.Close()

if err := pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: certDER}); err != nil {
return fmt.Errorf("failed to write data to cert.pem: %v", err)
}

// Save the private key to keyFile
keyOut, err := os.Create(keyFile)
if err != nil {
return fmt.Errorf("failed to open key.pem for writing: %v", err)
}
defer keyOut.Close()

privBytes, err := x509.MarshalECPrivateKey(priv)
if err != nil {
return fmt.Errorf("failed to marshal private key: %v", err)
}
if err := pem.Encode(keyOut, &pem.Block{Type: "EC PRIVATE KEY", Bytes: privBytes}); err != nil {
return fmt.Errorf("failed to write data to key.pem: %v", err)
}

fmt.Println("Successfully created self-signed certificate and private key.")
return nil
}
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ general:
debug: true
production: false
network:
forcehttp: true
fqdn: 0.0.0.0
port: 8080
ssl: true
ssl-config:
redirecthttp: true
allowselfsigned: true
certificatepath: ./certs/testing.crt
privatekeypath: ./certs/testing.key
10 changes: 10 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"os"
"os/signal"
"packagelock/certs"
"packagelock/config"
"packagelock/server"
"syscall"
Expand All @@ -30,6 +31,15 @@ func main() {
config.Config.SetDefault("general.app-version", AppVersion)
}

if _, err := os.Stat(config.Config.GetString("network.ssl-config.certificatepath")); os.IsNotExist(err) {
fmt.Println("Certificate files missing, creating new self-signed.")
err := certs.CreateSelfSignedCert(config.Config.GetString("network.ssl-config.certificatepath"), config.Config.GetString("network.ssl-config.privatekeypath"))
if err != nil {
fmt.Printf("Error creating self-signed certificate: %v\n", err)
return
}
}

fmt.Println(config.Config.AllSettings())

// Channel to signal the restart
Expand Down

0 comments on commit 75ed0d4

Please sign in to comment.