Skip to content

Commit 23fca30

Browse files
committed
add challenge password support for csr
1 parent 19180a1 commit 23fca30

File tree

5 files changed

+44
-20
lines changed

5 files changed

+44
-20
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ next:
66
- improve logging of invalid http requests
77
- check_logfile: improve detection of required macros
88
- add list-combine option
9+
- add challenge password support for csr
910

1011
0.37 Sun Sep 7 11:41:00 CEST 2025
1112
- update windows exporter to 0.31.3

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ require (
2626
github.com/spf13/cobra v1.10.1
2727
github.com/spf13/pflag v1.0.10
2828
github.com/stretchr/testify v1.11.1
29+
github.com/subuk/csrtool v0.0.0-20250413213651-887255723652
2930
github.com/yusufpapurcu/wmi v1.2.4
3031
golang.org/x/sys v0.36.0
3132
golang.org/x/term v0.35.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ github.com/spf13/pflag v1.0.10 h1:4EBh2KAYBwaONj6b2Ye1GiHfwjqyROoF4RwYO+vPwFk=
107107
github.com/spf13/pflag v1.0.10/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
108108
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
109109
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
110+
github.com/subuk/csrtool v0.0.0-20250413213651-887255723652 h1:ugfwv+gtOibCeWz4jbabkh64vpzfEHeITEG/iFXRn0M=
111+
github.com/subuk/csrtool v0.0.0-20250413213651-887255723652/go.mod h1:5D8NXQKYH/KRR4+k014+K1J6nxpTxqZAG5ULc2kOiuI=
110112
github.com/tklauser/go-sysconf v0.3.15 h1:VE89k0criAymJ/Os65CSn1IXaol+1wrsFHEB8Ol49K4=
111113
github.com/tklauser/go-sysconf v0.3.15/go.mod h1:Dmjwr6tYFIseJw7a3dRLJfsHAMXZ3nEnL/aZY+0IuI4=
112114
github.com/tklauser/numcpus v0.10.0 h1:18njr6LDBk1zuna922MgdjQuJFjrdppsZG60sHGfjso=

pkg/snclient/listen_web_admin.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"syscall"
1515

1616
"github.com/goccy/go-json"
17+
"github.com/subuk/csrtool/pkg/csrtool"
1718
)
1819

1920
func init() {
@@ -54,6 +55,7 @@ type csrRequestJSON struct {
5455
Organization string `json:"Organization"`
5556
OrganizationalUnit string `json:"OrganizationalUnit"`
5657
KeyLength int `json:"KeyLength"`
58+
ChallengePassword string `json:"ChallengePassword"`
5759
}
5860

5961
type replaceCertData struct {
@@ -226,7 +228,7 @@ func (l *HandlerWebAdmin) serveCertsCSR(res http.ResponseWriter, req *http.Reque
226228

227229
res.Header().Set("Content-Type", "application/json")
228230
res.WriteHeader(http.StatusOK)
229-
err = pem.Encode(res, csrPEM)
231+
_, err = res.Write(csrPEM)
230232
if err != nil {
231233
LogError(json.NewEncoder(res).Encode(map[string]interface{}{
232234
"success": false,
@@ -237,25 +239,19 @@ func (l *HandlerWebAdmin) serveCertsCSR(res http.ResponseWriter, req *http.Reque
237239
}
238240
}
239241

240-
func (l *HandlerWebAdmin) createCSR(data *csrRequestJSON, privateKey *rsa.PrivateKey) (*pem.Block, error) {
241-
csrTemplate := x509.CertificateRequest{
242-
Subject: pkix.Name{
243-
Country: []string{data.Country},
244-
Province: []string{data.State},
245-
Locality: []string{data.Locality},
246-
Organization: []string{data.Organization},
247-
OrganizationalUnit: []string{data.OrganizationalUnit},
248-
CommonName: data.HostName,
249-
},
242+
func (l *HandlerWebAdmin) createCSR(data *csrRequestJSON, privateKey *rsa.PrivateKey) ([]byte, error) {
243+
subject := pkix.Name{
244+
Country: []string{data.Country},
245+
Province: []string{data.State},
246+
Locality: []string{data.Locality},
247+
Organization: []string{data.Organization},
248+
OrganizationalUnit: []string{data.OrganizationalUnit},
249+
CommonName: data.HostName,
250250
}
251-
252-
// create certificate signing request
253-
csrDER, err := x509.CreateCertificateRequest(rand.Reader, &csrTemplate, privateKey)
251+
csrPEM, err := csrtool.GenerateCSR(privateKey, subject, []string{}, data.ChallengePassword)
254252
if err != nil {
255-
return nil, fmt.Errorf("could not create x509 certificate error was: %s", err.Error())
253+
return nil, fmt.Errorf("generate csr: %s", err.Error())
256254
}
257-
// Marshall to pem format
258-
csrPEM := &pem.Block{Type: "CERTIFICATE REQUEST", Bytes: csrDER}
259255

260256
return csrPEM, nil
261257
}

t/02_daemon_linux_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func TestErrorBetweenSavingAndSigning(t *testing.T) {
4040
defer os.Remove("test.key")
4141
defer os.Remove("test.csr")
4242

43-
postData, err := json.Marshal(map[string]any{
43+
rawPostData := map[string]any{
4444
"Country": "DE",
4545
"State": "Bavaria",
4646
"Locality": "Earth",
@@ -49,7 +49,9 @@ func TestErrorBetweenSavingAndSigning(t *testing.T) {
4949
"HostName": "Root CA SNClient",
5050
"NewKey": true,
5151
"KeyLength": 1024,
52-
})
52+
}
53+
54+
postData, err := json.Marshal(rawPostData)
5355
require.NoErrorf(t, err, "post data json encoded")
5456

5557
// Create Temp Server Certs
@@ -115,6 +117,28 @@ func TestErrorBetweenSavingAndSigning(t *testing.T) {
115117

116118
_, err = os.ReadFile("test.key.tmp")
117119
if err == nil {
118-
t.Fatalf("tempory key file was not removed")
120+
t.Fatalf("temporary key file was not removed")
121+
}
122+
123+
// request csr with challenge password
124+
rawPostData["ChallengePassword"] = "test123"
125+
postData, err = json.Marshal(rawPostData)
126+
require.NoErrorf(t, err, "post data json encoded")
127+
commandResult = runCmd(t, &cmd{
128+
Cmd: "curl",
129+
Args: []string{"-s", "-u", "user:" + localDaemonAdminPassword, "-k", "-s", "-d", string(postData), baseURL + "/api/v1/admin/csr"},
130+
Dir: ".",
131+
Like: []string{"CERTIFICATE REQUEST"},
132+
})
133+
134+
err = os.WriteFile("test.csr", []byte(commandResult.Stdout), 0o600)
135+
if err != nil {
136+
t.Fatalf("could not save certificate signing requests")
119137
}
138+
139+
runCmd(t, &cmd{
140+
Cmd: "openssl",
141+
Args: []string{"req", "-in", "test.csr", "-noout", "-text"},
142+
Like: []string{"challengePassword", "test123", "sha256WithRSAEncryption"},
143+
})
120144
}

0 commit comments

Comments
 (0)