@@ -3,6 +3,7 @@ package verify
33import (
44 "net/http"
55 "slices"
6+ "time"
67
78 "github.com/botsman/tppVerifier/app/db"
89 vhttp "github.com/botsman/tppVerifier/app/http"
@@ -48,8 +49,19 @@ type VerifyResult struct {
4849 Reason string `json:"reason,omitempty"`
4950}
5051
51- func (s * VerifySvc ) SetRoots (roots * x509.CertPool ) {
52- s .roots = roots
52+ func (s * VerifySvc ) SetRoots (roots []string ) {
53+ certPool := x509 .NewCertPool ()
54+ for _ , rawRoot := range roots {
55+ pemBytes , err := RawCertToPEM ([]byte (rawRoot ))
56+ if err != nil {
57+ log .Printf ("Error converting root certificate to PEM format: %s" , err )
58+ continue
59+ }
60+ if ! certPool .AppendCertsFromPEM (pemBytes ) {
61+ panic ("Failed to append root certificate" )
62+ }
63+ }
64+ s .roots = certPool
5365}
5466
5567func (s * VerifySvc ) Verify (c * gin.Context ) {
@@ -320,26 +332,42 @@ func (s *VerifySvc) isRevoked(c, issuer *x509.Certificate) (bool, error) {
320332 return ocspResponse .Status == ocsp .Revoked , nil
321333}
322334
335+ func RawCertToPEM (raw []byte ) ([]byte , error ) {
336+ pemBytes := pem .EncodeToMemory (& pem.Block {
337+ Type : "CERTIFICATE" ,
338+ Bytes : raw ,
339+ })
340+ if pemBytes == nil {
341+ return nil , errors .New ("error encoding certificate to PEM format" )
342+ }
343+ return pemBytes , nil
344+ }
345+
323346func (s * VerifySvc ) isTrusted (cert * x509.Certificate , chain []* x509.Certificate ) (bool , error ) {
324- return true , nil // TODO: Implement certificate trust verification logic
325- // intermediatePool := x509.NewCertPool()
326- // for _, intermediate := range chain {
327- // intermediatePool.AddCert(intermediate)
328- // }
329- // opts := x509.VerifyOptions{
330- // Roots: s.roots,
331- // Intermediates: intermediatePool,
332- // }
333- // _, err := cert.Verify(opts)
334- // if err != nil {
335- // log.Printf("Certificate verification failed: %s", err)
336- // if _, ok := err.(x509.UnknownAuthorityError); ok {
337- // log.Printf("Certificate is not trusted")
338- // return false, nil
339- // }
340- // }
341- // log.Printf("Certificate is trusted")
342- // return true, nil
347+ intermediatePool := x509 .NewCertPool ()
348+ for _ , intermediate := range chain {
349+ pemBytes , err := RawCertToPEM (intermediate .Raw )
350+ if err != nil {
351+ log .Printf ("Error converting intermediate certificate to PEM format: %s" , err )
352+ return false , err
353+ }
354+ if ! intermediatePool .AppendCertsFromPEM (pemBytes ) {
355+ log .Printf ("Failed to append intermediate certificate to pool" )
356+ return false , errors .New ("failed to append intermediate certificate to pool" )
357+ }
358+ }
359+ opts := x509.VerifyOptions {
360+ Roots : s .roots ,
361+ Intermediates : intermediatePool ,
362+ CurrentTime : time .Now (),
363+ }
364+ _ , err := cert .Verify (opts )
365+ if err != nil {
366+ log .Printf ("Certificate verification failed: %s" , err )
367+ return false , err
368+ }
369+ log .Printf ("Certificate is trusted" )
370+ return true , nil
343371}
344372
345373func formatCertContent (content []byte ) ([]byte , error ) {
0 commit comments