2019-02-06 16:40:55 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2019-08-19 23:10:38 +01:00
|
|
|
package revocation
|
2019-02-06 16:40:55 +00:00
|
|
|
|
|
|
|
import (
|
2019-06-04 12:36:27 +01:00
|
|
|
"context"
|
2019-02-06 16:40:55 +00:00
|
|
|
"crypto/x509"
|
|
|
|
"crypto/x509/pkix"
|
|
|
|
|
2019-11-08 20:40:39 +00:00
|
|
|
"github.com/spacemonkeygo/monkit/v3"
|
2019-08-19 23:10:38 +01:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/peertls"
|
|
|
|
"storj.io/common/peertls/extensions"
|
2023-04-06 14:54:41 +01:00
|
|
|
"storj.io/storj/private/kvstore"
|
2019-02-06 16:40:55 +00:00
|
|
|
)
|
|
|
|
|
2019-08-19 23:10:38 +01:00
|
|
|
var (
|
|
|
|
mon = monkit.Package()
|
|
|
|
|
2021-04-28 09:06:17 +01:00
|
|
|
// Error is a revocation error.
|
|
|
|
Error = errs.Class("revocation")
|
2019-08-19 23:10:38 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
// DB stores the most recently seen revocation for each nodeID
|
2019-02-06 16:40:55 +00:00
|
|
|
// (i.e. nodeID [CA certificate's public key hash] is the key, values is
|
|
|
|
// the most recently seen revocation).
|
2019-08-19 23:10:38 +01:00
|
|
|
type DB struct {
|
2023-04-06 14:54:41 +01:00
|
|
|
store kvstore.Store
|
2019-03-25 21:52:12 +00:00
|
|
|
}
|
|
|
|
|
2019-02-06 16:40:55 +00:00
|
|
|
// Get attempts to retrieve the most recent revocation for the given cert chain
|
|
|
|
// (the key used in the underlying database is the nodeID of the certificate chain).
|
2019-08-20 16:04:17 +01:00
|
|
|
func (db *DB) Get(ctx context.Context, chain []*x509.Certificate) (_ *extensions.Revocation, err error) {
|
2019-06-04 12:36:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-08-20 16:04:17 +01:00
|
|
|
|
|
|
|
if db.store == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-08-19 23:10:38 +01:00
|
|
|
nodeID, err := identity.NodeIDFromCert(chain[peertls.CAIndex])
|
2019-02-06 16:40:55 +00:00
|
|
|
if err != nil {
|
2019-03-25 21:52:12 +00:00
|
|
|
return nil, extensions.ErrRevocation.Wrap(err)
|
2019-02-06 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 16:04:17 +01:00
|
|
|
revBytes, err := db.store.Get(ctx, nodeID.Bytes())
|
2023-04-06 14:54:41 +01:00
|
|
|
if err != nil && !kvstore.ErrKeyNotFound.Has(err) {
|
2019-03-25 21:52:12 +00:00
|
|
|
return nil, extensions.ErrRevocationDB.Wrap(err)
|
2019-02-06 16:40:55 +00:00
|
|
|
}
|
|
|
|
if revBytes == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2019-03-25 21:52:12 +00:00
|
|
|
rev := new(extensions.Revocation)
|
2019-02-06 16:40:55 +00:00
|
|
|
if err = rev.Unmarshal(revBytes); err != nil {
|
2019-03-25 21:52:12 +00:00
|
|
|
return rev, extensions.ErrRevocationDB.Wrap(err)
|
2019-02-06 16:40:55 +00:00
|
|
|
}
|
|
|
|
return rev, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Put stores the most recent revocation for the given cert chain IF the timestamp
|
|
|
|
// is newer than the current value (the key used in the underlying database is
|
|
|
|
// the nodeID of the certificate chain).
|
2019-08-20 16:04:17 +01:00
|
|
|
func (db *DB) Put(ctx context.Context, chain []*x509.Certificate, revExt pkix.Extension) (err error) {
|
2019-06-04 12:36:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-08-20 16:04:17 +01:00
|
|
|
|
|
|
|
if db.store == nil {
|
|
|
|
return extensions.ErrRevocationDB.New("not supported")
|
|
|
|
}
|
|
|
|
|
2019-02-06 16:40:55 +00:00
|
|
|
ca := chain[peertls.CAIndex]
|
2019-03-25 21:52:12 +00:00
|
|
|
var rev extensions.Revocation
|
2019-02-06 16:40:55 +00:00
|
|
|
if err := rev.Unmarshal(revExt.Value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: do we care if cert/timestamp/sig is empty/garbage?
|
|
|
|
// TODO(bryanchriswhite): test empty/garbage cert/timestamp/sig
|
|
|
|
|
|
|
|
if err := rev.Verify(ca); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-19 23:10:38 +01:00
|
|
|
lastRev, err := db.Get(ctx, chain)
|
2019-02-06 16:40:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
} else if lastRev != nil && lastRev.Timestamp >= rev.Timestamp {
|
2019-03-25 21:52:12 +00:00
|
|
|
return extensions.ErrRevocationTimestamp
|
2019-02-06 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 23:10:38 +01:00
|
|
|
nodeID, err := identity.NodeIDFromCert(ca)
|
2019-02-06 16:40:55 +00:00
|
|
|
if err != nil {
|
2019-03-25 21:52:12 +00:00
|
|
|
return extensions.ErrRevocationDB.Wrap(err)
|
2019-02-06 16:40:55 +00:00
|
|
|
}
|
2019-08-20 16:04:17 +01:00
|
|
|
if err := db.store.Put(ctx, nodeID.Bytes(), revExt.Value); err != nil {
|
2019-03-25 21:52:12 +00:00
|
|
|
return extensions.ErrRevocationDB.Wrap(err)
|
2019-02-06 16:40:55 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// List lists all revocations in the store.
|
2019-08-20 16:04:17 +01:00
|
|
|
func (db *DB) List(ctx context.Context) (revs []*extensions.Revocation, err error) {
|
2019-06-04 12:36:27 +01:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-08-20 16:04:17 +01:00
|
|
|
|
|
|
|
if db.store == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-04-06 14:54:41 +01:00
|
|
|
err = db.store.Range(ctx, func(ctx context.Context, key kvstore.Key, value kvstore.Value) error {
|
2019-03-25 21:52:12 +00:00
|
|
|
rev := new(extensions.Revocation)
|
2023-04-06 10:53:25 +01:00
|
|
|
if err := rev.Unmarshal(value); err != nil {
|
|
|
|
return extensions.ErrRevocationDB.Wrap(err)
|
2019-02-06 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
revs = append(revs, rev)
|
2023-04-06 10:53:25 +01:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
return revs, extensions.ErrRevocationDB.Wrap(err)
|
2019-02-06 16:40:55 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 16:04:17 +01:00
|
|
|
// TestGetStore returns the internal store for testing.
|
2023-04-06 14:54:41 +01:00
|
|
|
func (db *DB) TestGetStore() kvstore.Store {
|
2019-08-20 16:04:17 +01:00
|
|
|
return db.store
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Close closes the underlying store.
|
2019-08-20 16:04:17 +01:00
|
|
|
func (db *DB) Close() error {
|
|
|
|
if db.store == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return db.store.Close()
|
2019-02-06 16:40:55 +00:00
|
|
|
}
|