Let clients list and revoke certs

This commit is contained in:
2021-12-06 19:14:39 +01:00
parent 79b80772c7
commit be564f9a3e
12 changed files with 684 additions and 122 deletions

View File

@@ -132,12 +132,12 @@ func (s *BoltStore) ListFiles() ([]*pb.ListFilesResponse_ListFileInfo, error) {
// Certificate store
var _ CertificateStore = &BoltStore{}
func (s *BoltStore) GetCertificate(id string) (*x509.Certificate, error) {
func (s *BoltStore) GetCertificate(serial string) (*x509.Certificate, error) {
var raw []byte
err := s.db.View(func(t *bolt.Tx) error {
bkt := t.Bucket(bktKeyCerts)
raw = bkt.Get([]byte(id))
raw = bkt.Get([]byte(serial))
return nil
})
if err != nil {
@@ -155,13 +155,13 @@ func (s *BoltStore) GetCertificate(id string) (*x509.Certificate, error) {
return cert, nil
}
func (s *BoltStore) StoreCertificate(id string, cert *x509.Certificate) error {
func (s *BoltStore) StoreCertificate(cert *x509.Certificate) error {
data := make([]byte, len(cert.Raw))
copy(data, cert.Raw)
return s.db.Update(func(t *bolt.Tx) error {
bkt := t.Bucket(bktKeyCerts)
return bkt.Put([]byte(id), cert.Raw)
return bkt.Put([]byte(cert.SerialNumber.String()), cert.Raw)
})
}

View File

@@ -91,11 +91,11 @@ func (s *MemoryStore) ListFiles() ([]*pb.ListFilesResponse_ListFileInfo, error)
var _ CertificateStore = &MemoryStore{}
func (s *MemoryStore) GetCertificate(id string) (*x509.Certificate, error) {
func (s *MemoryStore) GetCertificate(serial string) (*x509.Certificate, error) {
s.certLock.Lock()
defer s.certLock.Unlock()
data, ok := s.certs[id]
data, ok := s.certs[serial]
if !ok {
// TODO: Make separate error, or rename error
return nil, ErrNoSuchItem
@@ -104,7 +104,7 @@ func (s *MemoryStore) GetCertificate(id string) (*x509.Certificate, error) {
return x509.ParseCertificate(data)
}
func (s *MemoryStore) StoreCertificate(id string, cert *x509.Certificate) error {
func (s *MemoryStore) StoreCertificate(cert *x509.Certificate) error {
s.certLock.Lock()
defer s.certLock.Unlock()
@@ -112,7 +112,7 @@ func (s *MemoryStore) StoreCertificate(id string, cert *x509.Certificate) error
data := make([]byte, len(cert.Raw))
copy(data, cert.Raw)
s.certs[id] = data
s.certs[cert.SerialNumber.String()] = data
return nil
}

View File

@@ -18,8 +18,8 @@ type FileStore interface {
}
type CertificateStore interface {
GetCertificate(id string) (*x509.Certificate, error)
StoreCertificate(id string, cert *x509.Certificate) error
GetCertificate(serial string) (*x509.Certificate, error)
StoreCertificate(cert *x509.Certificate) error
GetKey(id string) (*ecdsa.PrivateKey, error)
StoreKey(id string, key *ecdsa.PrivateKey) error
ListCertificates() ([]string, error)

View File

@@ -106,7 +106,7 @@ func doCertificateStoreTest(s store.CertificateStore, t *testing.T) {
}
// Store cert
if err := s.StoreCertificate("cert", cert); err != nil {
if err := s.StoreCertificate(cert); err != nil {
t.Fatalf("Error storing cert: %s", err)
}
@@ -123,11 +123,11 @@ func doCertificateStoreTest(s store.CertificateStore, t *testing.T) {
if len(ids) != 1 {
t.Fatalf("List has wrong length: %s", err)
}
if ids[0] != "cert" {
if ids[0] != cert.SerialNumber.String() {
t.Fatalf("List has wrong id")
}
retrievedCert, err := s.GetCertificate("cert")
retrievedCert, err := s.GetCertificate(cert.SerialNumber.String())
if err != nil {
t.Fatalf("Unable to get certificate from store: %s", err)
}