Skip to content

Commit e607a6f

Browse files
committed
match SignatureAlgorithm for GenerateCertificateByCSR(); QoL updates
1 parent 2f6320a commit e607a6f

2 files changed

Lines changed: 9 additions & 9 deletions

File tree

internal/certmaker/certmaker.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,13 @@ func (cm *CertMaker) GenerateLeafCertAndKey(request entity.SimpleRequest) (int64
334334
}
335335

336336
func (cm *CertMaker) GenerateCertificateByCSR(csr *x509.CertificateRequest) (int64, error) {
337-
caTls, err := tls.LoadX509KeyPair(filepath.Join(cm.Config.DataDir, global.RootCertificateFilename), filepath.Join(cm.Config.DataDir, global.RootCertificateFilename))
337+
caTls, err := tls.LoadX509KeyPair(filepath.Join(cm.Config.DataDir, global.RootCertificateFilename), filepath.Join(cm.Config.DataDir, global.RootPrivateKeyFilename))
338338
if err != nil {
339-
panic(err)
339+
return 0, err
340340
}
341341
ca, err := x509.ParseCertificate(caTls.Certificate[0])
342342
if err != nil {
343-
panic(err)
343+
return 0, err
344344
}
345345

346346
nextSn, err := cm.GetNextSerialNumber()
@@ -356,7 +356,7 @@ func (cm *CertMaker) GenerateCertificateByCSR(csr *x509.CertificateRequest) (int
356356
SubjectKeyId: []byte{1, 2, 3, 4, 6},
357357
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
358358
KeyUsage: x509.KeyUsageDigitalSignature,
359-
SignatureAlgorithm: x509.ECDSAWithSHA256,
359+
SignatureAlgorithm: csr.SignatureAlgorithm,
360360
OCSPServer: []string{cm.Config.ServerHost + global.OCSPPath}, // TODO implement/fix
361361

362362
EmailAddresses: csr.EmailAddresses,

internal/handler/api.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/pem"
1111
"fmt"
1212
"io"
13-
"io/ioutil"
1413
"net/http"
1514
"os"
1615
"path/filepath"
@@ -122,6 +121,7 @@ func (bh *BaseHandler) ApiRequestCertificateHandler(w http.ResponseWriter, r *ht
122121

123122
w.Header().Add(global.CertificateLocationHeader, fmt.Sprintf(bh.Config.ServerHost+global.CertificateObtainPath, sn))
124123
w.Header().Add(global.PrivateKeyLocationHandler, fmt.Sprintf(bh.Config.ServerHost+global.PrivateKeyObtainPath, sn))
124+
w.WriteHeader(http.StatusCreated)
125125
}
126126

127127
// ApiRequestCertificateWithCSRHandler handles a client's request for a new certificate,
@@ -147,16 +147,14 @@ func (bh *BaseHandler) ApiRequestCertificateWithCSRHandler(w http.ResponseWriter
147147
return
148148
}
149149

150-
csrBytes, err := ioutil.ReadAll(r.Body)
150+
csrBytes, err := io.ReadAll(r.Body)
151151
if err != nil {
152152
logger.Debugf("could not read request body: %s", err.Error())
153153
http.Error(w, "malformed http request", http.StatusBadRequest)
154154
return
155155
}
156156

157-
p, _ := pem.Decode(csrBytes)
158-
159-
csr, err := x509.ParseCertificateRequest(p.Bytes)
157+
csr, err := x509.ParseCertificateRequest(csrBytes)
160158
if err != nil {
161159
logger.Debugf("could not parse certificate signing request: %s", err.Error())
162160
http.Error(w, "malformed certificate signing request", http.StatusBadRequest)
@@ -216,6 +214,8 @@ func (bh *BaseHandler) ApiRequestCertificateWithCSRHandler(w http.ResponseWriter
216214
}
217215

218216
w.Header().Add(global.CertificateLocationHeader, fmt.Sprintf(bh.Config.ServerHost+global.CertificateObtainPath, sn))
217+
// private key location header intentionally omitted because it's not needed
218+
219219
w.WriteHeader(http.StatusCreated)
220220
}
221221

0 commit comments

Comments
 (0)