Skip to content

Commit 8377351

Browse files
authored
allow p12 creation without cacert; timelimit openssl exec (#57)
1 parent c140cfe commit 8377351

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

main.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,9 @@ func generateCertificate() {
207207
}
208208

209209
log.Printf("certificate key pair created: cert: %s-cert.pem, key: %s-key.pem", viper.GetString("name"), viper.GetString("name"))
210+
if viper.GetBool("p12") {
211+
log.Printf("p12 bundle created: %s.p12", viper.GetString("name"))
212+
}
210213
}
211214

212215
func generateCSR() {

tglib/pkcs12.go

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,51 @@
1212
package tglib
1313

1414
import (
15+
"bytes"
16+
"context"
1517
"encoding/base64"
1618
"fmt"
1719
"io"
1820
"os"
1921
"os/exec"
2022
"strings"
23+
"time"
2124
)
2225

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

26-
/* #nosec */
27-
return exec.Command(
28-
"openssl",
29+
var errb bytes.Buffer
30+
31+
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
32+
defer cancel()
33+
34+
// TODO for pkcs12 file without encryption use: -keypbe NONE -certpbe NONE -nomaciter
35+
const command = "openssl"
36+
args := append(make([]string, 0, 15),
2937
"pkcs12",
3038
"-export",
3139
"-out", out,
3240
"-inkey", keyPath,
3341
"-in", certPath,
34-
"-certfile", caPath,
3542
"-passout", "pass:"+passphrase,
36-
).Run()
43+
)
44+
if len(caPath) > 0 {
45+
args = append(args, "-certfile", caPath)
46+
}
47+
48+
// #nosec G204 audited OK - no command injection can occur here
49+
cmd := exec.CommandContext(ctx, command, args...)
50+
cmd.Stderr = &errb
51+
cmd.WaitDelay = 5 * time.Second
52+
53+
err := cmd.Run()
54+
if err != nil {
55+
// include the openssl stderr output to aid in debugging the reason for failure
56+
err = fmt.Errorf("exec openssl failed: stderr='%s': %w", strings.TrimSpace(errb.String()), err)
57+
}
58+
59+
return err
3760
}
3861

3962
// GeneratePKCS12 generates a pkcs12

0 commit comments

Comments
 (0)