Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:generate certificate using yaml #6

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
tmp
tmp1
test-files
config.yaml
128 changes: 100 additions & 28 deletions certgen/certgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,110 @@ import (
"crypto/x509"
"encoding/pem"
"fmt"
"math/big"
"net"
"os"
"path/filepath"
"runtime"
"time"

"gopkg.in/yaml.v2"

"crypto/x509/pkix"
"math/big"
"time"

"github.com/Elixir-Craft/https-server/localip"
)

// file location by os
func getConfigFilePath() string {
var configDir string
var configFileName string

switch OS := runtime.GOOS; OS {
case "windows":
// On Windows, use the APPDATA directory
configDir = filepath.Join(os.Getenv("APPDATA"), "ServeIt")
configFileName = "config.yaml"
case "darwin":
// On macOS, use the home directory
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
configDir = filepath.Join(home, "Library", "Application Support", "ServeIt")
configFileName = "config.yaml"
default:
// On Unix-like systems, use the home directory
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
configDir = filepath.Join(home, ".ServeIt")
configFileName = "config.yaml"
}

// Create configuration directory if it doesn't exist
if _, err := os.Stat(configDir); os.IsNotExist(err) {
os.MkdirAll(configDir, 0755)
}

return filepath.Join(configDir, configFileName)
}

type CertConfig struct {
Organization string `yaml:"organization"`
Country string `yaml:"country"`
Province string `yaml:"province"`
Locality string `yaml:"locality"`
StreetAddress string `yaml:"street_address"`
PostalCode string `yaml:"postal_code"`
}

type Config struct {
Cert CertConfig `yaml:"cert"`
}

func loadConfig() (Config, error) {
config := Config{
Cert: CertConfig{
Organization: "ServeIt Inc",
Country: "LK",
Province: "",
Locality: "Colombo",
StreetAddress: "Main Street",
PostalCode: "00000",
},
}
if _, err := os.Stat(getConfigFilePath()); err == nil {
file, err := os.ReadFile(getConfigFilePath())
if err != nil {
return config, err
}
err = yaml.Unmarshal(file, &config)
if err != nil {
return config, err
}
}
fmt.Println(config)
return config, nil
}

func Certsetup() (serverTLSConf *tls.Config, clientTLSConf *tls.Config, err error) {
config, err := loadConfig()
if err != nil {
return nil, nil, err
}

// set up our CA certificate
ca := &x509.Certificate{
SerialNumber: big.NewInt(2019),
Subject: pkix.Name{
Organization: []string{"Company, INC."},
Country: []string{"US"},
Province: []string{""},
Locality: []string{"San Francisco"},
StreetAddress: []string{"Golden Gate Bridge"},
PostalCode: []string{"94016"},
Organization: []string{config.Cert.Organization},
Country: []string{config.Cert.Country},
Province: []string{config.Cert.Province},
Locality: []string{config.Cert.Locality},
StreetAddress: []string{config.Cert.StreetAddress},
PostalCode: []string{config.Cert.PostalCode},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
Expand All @@ -39,15 +122,12 @@ func Certsetup() (serverTLSConf *tls.Config, clientTLSConf *tls.Config, err erro
}

// generate a private key for the CA

caPrivKey, err := rsa.GenerateKey(rand.Reader, 4096)

if err != nil {
return nil, nil, err
}

// create CA

caBytes, err := x509.CreateCertificate(rand.Reader, ca, ca, &caPrivKey.PublicKey, caPrivKey)
if err != nil {
return nil, nil, err
Expand All @@ -69,21 +149,19 @@ func Certsetup() (serverTLSConf *tls.Config, clientTLSConf *tls.Config, err erro

localIPs, err := localip.Get()
if err != nil {
fmt.Println("Error:", err)
return
return nil, nil, err
}

cert := &x509.Certificate{
SerialNumber: big.NewInt(2019),
Subject: pkix.Name{
Organization: []string{"Company, INC."},
Country: []string{"US"},
Province: []string{""},
Locality: []string{"San Francisco"},
StreetAddress: []string{"Golden Gate Bridge"},
PostalCode: []string{"94016"},
Organization: []string{config.Cert.Organization},
Country: []string{config.Cert.Country},
Province: []string{config.Cert.Province},
Locality: []string{config.Cert.Locality},
StreetAddress: []string{config.Cert.StreetAddress},
PostalCode: []string{config.Cert.PostalCode},
},
// IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback , localip.Get()},
IPAddresses: append([]net.IP{net.IPv4(127, 0, 0, 1), net.IPv6loopback}, localIPs...),
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
Expand Down Expand Up @@ -129,10 +207,6 @@ func Certsetup() (serverTLSConf *tls.Config, clientTLSConf *tls.Config, err erro
RootCAs: certpool,
}

// save the certificates to disk
// ioutil.WriteFile("../cert/ca.pem", caPEM.Bytes(), 0644)
// ioutil.WriteFile("../cert/ca-key.pem", caPrivKeyPEM.Bytes(), 0644)

// create directory if it does not exist
os.Mkdir("cert", 0755)

Expand All @@ -149,9 +223,7 @@ func CertFilesExist(certFilePath, keyFilePath string) bool {
}

_, err = os.Stat(keyFilePath)
if os.IsNotExist(err) {
return false
}

return true
return err == nil

}
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module github.com/Elixir-Craft/https-server

go 1.22.3

require gopkg.in/yaml.v2 v2.4.0 // indirect
3 changes: 3 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Loading