@@ -14,6 +14,7 @@ import (
1414 "syscall"
1515
1616 "github.com/goccy/go-json"
17+ "github.com/subuk/csrtool/pkg/csrtool"
1718)
1819
1920func 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
5961type 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}
0 commit comments