@@ -2,12 +2,14 @@ package csr
22
33import (
44 "context"
5+ "crypto/rand"
56 "crypto/x509"
67 "encoding/pem"
78 "errors"
89 "fmt"
910 "math/big"
1011 "runtime"
12+ "strings"
1113 "time"
1214
1315 "github.com/android-sms-gateway/ca/pkg/client"
@@ -20,6 +22,9 @@ import (
2022type Service struct {
2123 csrs * repository
2224
25+ caCert * x509.Certificate
26+ caKey any
27+
2328 queue * queue.Queue
2429 newid func () string
2530 log * zap.Logger
@@ -57,7 +62,7 @@ func (s *Service) Create(ctx context.Context, csr CSR) (CSRStatus, error) {
5762 s .log .Error ("failed to queue csr" , zap .Error (err ))
5863 }
5964
60- return NewCSRStatus (id , client .CSRStatusPending , "" , "" ), nil
65+ return NewCSRStatus (id , csr . content , csr . metadata , client .CSRStatusPending , "" , "" ), nil
6166}
6267
6368func (s * Service ) Get (ctx context.Context , id string ) (CSRStatus , error ) {
@@ -82,13 +87,13 @@ func (s *Service) process(ctx context.Context, m core.TaskMessage) error {
8287 return nil
8388 }
8489
85- csr , err := s .parseCsr (res .Certificate ())
90+ csr , err := s .parseCsr (res .Content ())
8691 if err != nil {
8792 return err
8893 }
8994
9095 // Create a signed certificate
91- _ = & x509.Certificate {
96+ template : = & x509.Certificate {
9297 SerialNumber : big .NewInt (1 ),
9398 Subject : csr .Subject ,
9499 NotBefore : time .Now (),
@@ -102,20 +107,22 @@ func (s *Service) process(ctx context.Context, m core.TaskMessage) error {
102107 IPAddresses : csr .IPAddresses ,
103108 }
104109
105- // certBytes, err := x509.CreateCertificate(rand.Reader, template, caKey, csr.PublicKey, caPriv)
106- // if err != nil {
107- // return "", fmt.Errorf("failed to sign certificate: %w", err)
108- // }
110+ certBytes , err := x509 .CreateCertificate (rand .Reader , template , s .caCert , csr .PublicKey , s .caKey )
111+ if err != nil {
112+ return fmt .Errorf ("failed to sign certificate: %w" , err )
113+ }
114+
115+ // Encode the signed certificate to PEM format
116+ var certPEM strings.Builder
117+ if err := pem .Encode (& certPEM , & pem.Block {Type : "CERTIFICATE" , Bytes : certBytes }); err != nil {
118+ return fmt .Errorf ("failed to encode certificate: %w" , err )
119+ }
109120
110- time . Sleep ( time . Second * 10 )
121+ s . log . Info ( "signed certificate" , zap . String ( "id" , id ), zap . String ( "csr" , res . Certificate ()), zap . String ( "cert" , certPEM . String ()) )
111122
112123 return nil
113124}
114125
115- // func LoadCA(certPath, keyPath string) (*x509.Certificate, *rsa.PrivateKey, error) {
116-
117- // }
118-
119126func (s * Service ) parseCsr (content string ) (* x509.CertificateRequest , error ) {
120127 block , _ := pem .Decode ([]byte (content ))
121128 if block == nil || block .Type != "CERTIFICATE REQUEST" {
@@ -125,11 +132,19 @@ func (s *Service) parseCsr(content string) (*x509.CertificateRequest, error) {
125132 return x509 .ParseCertificateRequest (block .Bytes )
126133}
127134
128- func NewService (csrs * repository , log * zap.Logger ) * Service {
135+ func NewService (csrs * repository , caCert * x509. Certificate , caKey any , log * zap.Logger ) * Service {
129136 if csrs == nil {
130137 panic ("csrs is required" )
131138 }
132139
140+ if caCert == nil {
141+ panic ("caCert is required" )
142+ }
143+
144+ if caKey == nil {
145+ panic ("caKey is required" )
146+ }
147+
133148 if log == nil {
134149 panic ("log is required" )
135150 }
@@ -139,6 +154,9 @@ func NewService(csrs *repository, log *zap.Logger) *Service {
139154 s := & Service {
140155 csrs : csrs ,
141156
157+ caCert : caCert ,
158+ caKey : caKey ,
159+
142160 newid : newid ,
143161 log : log ,
144162 }
0 commit comments