Skip to content

Commit 3f31517

Browse files
[cert-v2] nebula-cert should verify all certs (#1291)
1 parent 21a117a commit 3f31517

File tree

4 files changed

+31
-17
lines changed

4 files changed

+31
-17
lines changed

cert/ca_pool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (ncp *CAPool) GetCAForCert(c Certificate) (*CachedCertificate, error) {
213213
return signer, nil
214214
}
215215

216-
return nil, fmt.Errorf("could not find ca for the certificate")
216+
return nil, ErrCaNotFound
217217
}
218218

219219
// GetFingerprints returns an array of trusted CA fingerprints

cert/errors.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ var (
1717
ErrInvalidPrivateKey = errors.New("invalid private key")
1818
ErrPublicPrivateCurveMismatch = errors.New("public key does not match private key curve")
1919
ErrPublicPrivateKeyMismatch = errors.New("public key and private key are not a pair")
20+
ErrCaNotFound = errors.New("could not find ca for the certificate")
2021

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

cmd/nebula-cert/verify.go

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"errors"
45
"flag"
56
"fmt"
67
"io"
@@ -41,14 +42,14 @@ func verify(args []string, out io.Writer, errOut io.Writer) error {
4142

4243
rawCACert, err := os.ReadFile(*vf.caPath)
4344
if err != nil {
44-
return fmt.Errorf("error while reading ca: %s", err)
45+
return fmt.Errorf("error while reading ca: %w", err)
4546
}
4647

4748
caPool := cert.NewCAPool()
4849
for {
4950
rawCACert, err = caPool.AddCAFromPEM(rawCACert)
5051
if err != nil {
51-
return fmt.Errorf("error while adding ca cert to pool: %s", err)
52+
return fmt.Errorf("error while adding ca cert to pool: %w", err)
5253
}
5354

5455
if rawCACert == nil || len(rawCACert) == 0 || strings.TrimSpace(string(rawCACert)) == "" {
@@ -58,20 +59,30 @@ func verify(args []string, out io.Writer, errOut io.Writer) error {
5859

5960
rawCert, err := os.ReadFile(*vf.certPath)
6061
if err != nil {
61-
return fmt.Errorf("unable to read crt; %s", err)
62+
return fmt.Errorf("unable to read crt: %w", err)
6263
}
63-
64-
c, _, err := cert.UnmarshalCertificateFromPEM(rawCert)
65-
if err != nil {
66-
return fmt.Errorf("error while parsing crt: %s", err)
67-
}
68-
69-
_, err = caPool.VerifyCertificate(time.Now(), c)
70-
if err != nil {
71-
return err
64+
var errs []error
65+
for {
66+
if len(rawCert) == 0 {
67+
break
68+
}
69+
c, extra, err := cert.UnmarshalCertificateFromPEM(rawCert)
70+
if err != nil {
71+
return fmt.Errorf("error while parsing crt: %w", err)
72+
}
73+
rawCert = extra
74+
_, err = caPool.VerifyCertificate(time.Now(), c)
75+
if err != nil {
76+
switch {
77+
case errors.Is(err, cert.ErrCaNotFound):
78+
errs = append(errs, fmt.Errorf("error while verifying certificate v%d %s with issuer %s: %w", c.Version(), c.Name(), c.Issuer(), err))
79+
default:
80+
errs = append(errs, fmt.Errorf("error while verifying certificate %+v: %w", c, err))
81+
}
82+
}
7283
}
7384

74-
return nil
85+
return errors.Join(errs...)
7586
}
7687

7788
func verifySummary() string {
@@ -80,7 +91,7 @@ func verifySummary() string {
8091

8192
func verifyHelp(out io.Writer) {
8293
vf := newVerifyFlags()
83-
out.Write([]byte("Usage of " + os.Args[0] + " " + verifySummary() + "\n"))
94+
_, _ = out.Write([]byte("Usage of " + os.Args[0] + " " + verifySummary() + "\n"))
8495
vf.set.SetOutput(out)
8596
vf.set.PrintDefaults()
8697
}

cmd/nebula-cert/verify_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package main
33
import (
44
"bytes"
55
"crypto/rand"
6+
"errors"
67
"os"
78
"testing"
89
"time"
910

11+
"github.com/slackhq/nebula/cert"
1012
"github.com/stretchr/testify/assert"
1113
"golang.org/x/crypto/ed25519"
1214
)
@@ -76,7 +78,7 @@ func Test_verify(t *testing.T) {
7678
err = verify([]string{"-ca", caFile.Name(), "-crt", "does_not_exist"}, ob, eb)
7779
assert.Equal(t, "", ob.String())
7880
assert.Equal(t, "", eb.String())
79-
assert.EqualError(t, err, "unable to read crt; open does_not_exist: "+NoSuchFileError)
81+
assert.EqualError(t, err, "unable to read crt: open does_not_exist: "+NoSuchFileError)
8082

8183
// invalid crt at path
8284
ob.Reset()
@@ -106,7 +108,7 @@ func Test_verify(t *testing.T) {
106108
err = verify([]string{"-ca", caFile.Name(), "-crt", certFile.Name()}, ob, eb)
107109
assert.Equal(t, "", ob.String())
108110
assert.Equal(t, "", eb.String())
109-
assert.EqualError(t, err, "certificate signature did not match")
111+
assert.True(t, errors.Is(err, cert.ErrSignatureMismatch))
110112

111113
// verified cert at path
112114
crt, _ = NewTestCert(ca, caPriv, "test-cert", time.Now().Add(time.Hour*-1), time.Now().Add(time.Hour), nil, nil, nil)

0 commit comments

Comments
 (0)