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

allow p12 creation without cacert; timelimit openssl exec #57

Merged
merged 1 commit into from
Jan 7, 2025
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
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ func generateCertificate() {
}

log.Printf("certificate key pair created: cert: %s-cert.pem, key: %s-key.pem", viper.GetString("name"), viper.GetString("name"))
if viper.GetBool("p12") {
log.Printf("p12 bundle created: %s.p12", viper.GetString("name"))
}
}

func generateCSR() {
Expand Down
33 changes: 28 additions & 5 deletions tglib/pkcs12.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,51 @@
package tglib

import (
"bytes"
"context"
"encoding/base64"
"fmt"
"io"
"os"
"os/exec"
"strings"
"time"
)

// GeneratePKCS12FromFiles generates a full PKCS certificate based on the input keys.
func GeneratePKCS12FromFiles(out, certPath, keyPath, caPath, passphrase string) error {

/* #nosec */
return exec.Command(
"openssl",
var errb bytes.Buffer

ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
defer cancel()

// TODO for pkcs12 file without encryption use: -keypbe NONE -certpbe NONE -nomaciter
const command = "openssl"
args := append(make([]string, 0, 15),
"pkcs12",
"-export",
"-out", out,
"-inkey", keyPath,
"-in", certPath,
"-certfile", caPath,
"-passout", "pass:"+passphrase,
).Run()
)
if len(caPath) > 0 {
args = append(args, "-certfile", caPath)
}

// #nosec G204 audited OK - no command injection can occur here
cmd := exec.CommandContext(ctx, command, args...)
cmd.Stderr = &errb
cmd.WaitDelay = 5 * time.Second

err := cmd.Run()
if err != nil {
// include the openssl stderr output to aid in debugging the reason for failure
err = fmt.Errorf("exec openssl failed: stderr='%s': %w", strings.TrimSpace(errb.String()), err)
}

return err
}

// GeneratePKCS12 generates a pkcs12
Expand Down
Loading