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

fix ca key save issue #340

Merged
merged 1 commit into from
Aug 3, 2023
Merged
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
21 changes: 14 additions & 7 deletions pkg/certs/mitm.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,19 @@ func GetMitMConfig() *mitm.Config {

func SaveCAToFile(filename string) error {
buffer := &bytes.Buffer{}
_ = pem.Encode(buffer, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw})
err := pem.Encode(buffer, &pem.Block{Type: "CERTIFICATE", Bytes: cert.Raw})
if err != nil {
return err
}
return os.WriteFile(filename, buffer.Bytes(), 0600)
}

func SaveKeyToFile(filename string) error {
buffer := &bytes.Buffer{}
err := pem.Encode(buffer, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(pkey)})
if err != nil {
return err
}
return os.WriteFile(filename, buffer.Bytes(), 0600)
}

Expand All @@ -58,14 +70,9 @@ func generateCertificate(certFile, keyFile string) error {
if err = SaveCAToFile(certFile); err != nil {
gologger.Fatal().Msgf("failed to save certFile to disk got %v", err)
}
kf, err := os.OpenFile(keyFile, os.O_WRONLY, 0600)
if err != nil {
gologger.Fatal().Msgf("failed to load open %v while saving private key got %v", keyFile, err)
}
if err := pem.Encode(kf, &pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(pkey)}); err != nil {
if err := SaveKeyToFile(keyFile); err != nil {
gologger.Fatal().Msgf("failed to write private key to file got %v", err)
}
_ = kf.Close()
return nil
}

Expand Down