You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
79 lines
2.0 KiB
Go
79 lines
2.0 KiB
Go
3 years ago
|
package revoking
|
||
|
|
||
|
import (
|
||
2 years ago
|
"crypto/rand"
|
||
2 years ago
|
"crypto/rsa"
|
||
|
"crypto/x509"
|
||
|
"crypto/x509/pkix"
|
||
3 years ago
|
"math/big"
|
||
|
"testing"
|
||
2 years ago
|
"time"
|
||
3 years ago
|
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
)
|
||
|
|
||
|
type testRepo struct {
|
||
|
revoked []big.Int
|
||
|
}
|
||
|
|
||
2 years ago
|
func (t *testRepo) RevokedCertificates() ([]pkix.RevokedCertificate, error) {
|
||
|
result := make([]pkix.RevokedCertificate, len(t.revoked))
|
||
|
|
||
|
for i, s := range t.revoked {
|
||
|
result[i] = pkix.RevokedCertificate{
|
||
|
SerialNumber: &s,
|
||
|
RevocationTime: time.Now(),
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return result, nil
|
||
|
}
|
||
|
|
||
|
func (t *testRepo) StoreRevocation(revoked *pkix.RevokedCertificate) error {
|
||
|
t.revoked = append(t.revoked, *revoked.SerialNumber)
|
||
3 years ago
|
return nil
|
||
|
}
|
||
|
|
||
2 years ago
|
func randomSerial(t *testing.T) *big.Int {
|
||
|
t.Helper()
|
||
|
serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
||
|
if err != nil {
|
||
|
t.Fatalf("could not generate random serial number: %v", err)
|
||
|
}
|
||
|
return serial
|
||
|
}
|
||
|
|
||
3 years ago
|
func TestRevoking(t *testing.T) {
|
||
|
testRepository := testRepo{revoked: make([]big.Int, 0)}
|
||
2 years ago
|
|
||
|
caKey, err := rsa.GenerateKey(rand.Reader, 3072)
|
||
|
if err != nil {
|
||
|
t.Fatalf("could not generate key pair: %v", err)
|
||
|
}
|
||
2 years ago
|
caTemplate := &x509.Certificate{Subject: pkix.Name{CommonName: "Test CA"}, SerialNumber: randomSerial(t)}
|
||
2 years ago
|
|
||
|
certificateBytes, err := x509.CreateCertificate(rand.Reader, caTemplate, caTemplate, caKey.Public(), caKey)
|
||
|
if err != nil {
|
||
|
t.Fatalf("could not self-sign CA certificate: %v", err)
|
||
|
}
|
||
|
caCertificate, err := x509.ParseCertificate(certificateBytes)
|
||
|
if err != nil {
|
||
|
t.Fatalf("could not create test CA certificate: %v", err)
|
||
|
}
|
||
|
|
||
|
r := NewX509Revoking(&testRepository, x509.ECDSAWithSHA256, caCertificate, caKey)
|
||
3 years ago
|
|
||
2 years ago
|
serial, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
|
||
|
if err != nil {
|
||
|
t.Errorf("could not create random serial: %v", err)
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
revoke, err := r.Revoke(&RevokeCertificate{serialNumber: serial, reason: CRLReasonKeyCompromise})
|
||
3 years ago
|
assert.NoError(t, err)
|
||
|
|
||
2 years ago
|
assert.Equal(t, CRLReasonKeyCompromise.BuildExtension(), revoke.Extensions[0])
|
||
|
assert.Equal(t, serial, revoke.SerialNumber)
|
||
3 years ago
|
|
||
|
assert.Contains(t, testRepository.revoked, *serial)
|
||
|
}
|