Skip to content

Commit

Permalink
cert-v2: verify all certificates provided to us. If one or more certs…
Browse files Browse the repository at this point in the history
… fail, report which ones.
  • Loading branch information
JackDoanRivian committed Dec 12, 2024
1 parent 21a117a commit 66fe4ff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cert/ca_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func (ncp *CAPool) GetCAForCert(c Certificate) (*CachedCertificate, error) {
return signer, nil
}

return nil, fmt.Errorf("could not find ca for the certificate")
return nil, ErrCaNotFound
}

// GetFingerprints returns an array of trusted CA fingerprints
Expand Down
1 change: 1 addition & 0 deletions cert/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ var (
ErrInvalidPrivateKey = errors.New("invalid private key")
ErrPublicPrivateCurveMismatch = errors.New("public key does not match private key curve")
ErrPublicPrivateKeyMismatch = errors.New("public key and private key are not a pair")
ErrCaNotFound = errors.New("could not find ca for the certificate")

ErrPrivateKeyEncrypted = errors.New("private key must be decrypted")

Expand Down
31 changes: 21 additions & 10 deletions cmd/nebula-cert/verify.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"flag"
"fmt"
"io"
Expand Down Expand Up @@ -60,18 +61,28 @@ func verify(args []string, out io.Writer, errOut io.Writer) error {
if err != nil {
return fmt.Errorf("unable to read crt; %s", err)
}

c, _, err := cert.UnmarshalCertificateFromPEM(rawCert)
if err != nil {
return fmt.Errorf("error while parsing crt: %s", err)
}

_, err = caPool.VerifyCertificate(time.Now(), c)
if err != nil {
return err
var errs []error
for {
if len(rawCert) == 0 {
break
}
c, extra, err := cert.UnmarshalCertificateFromPEM(rawCert)
if err != nil {
return fmt.Errorf("error while parsing crt: %s", err)
}
rawCert = extra
_, err = caPool.VerifyCertificate(time.Now(), c)
if err != nil {
switch {
case errors.Is(err, cert.ErrCaNotFound):
errs = append(errs, fmt.Errorf("error while verifying certificate v%d %s with issuer %s: %s", c.Version(), c.Name(), c.Issuer(), err))
default:
errs = append(errs, fmt.Errorf("error while verifying certificate %+v: %s", c, err))
}
}
}

return nil
return errors.Join(errs...)
}

func verifySummary() string {
Expand Down

0 comments on commit 66fe4ff

Please sign in to comment.