crl_test.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. package crl
  2. import (
  3. "crypto/x509"
  4. "testing"
  5. "time"
  6. "github.com/cloudflare/cfssl/certdb"
  7. "github.com/cloudflare/cfssl/certdb/sql"
  8. "github.com/cloudflare/cfssl/certdb/testdb"
  9. "github.com/cloudflare/cfssl/cli"
  10. "github.com/cloudflare/cfssl/helpers"
  11. )
  12. var dbAccessor certdb.Accessor
  13. const (
  14. fakeAKI = "fake aki"
  15. testCaFile = "../testdata/ca.pem"
  16. testCaKeyFile = "../testdata/ca-key.pem"
  17. )
  18. func prepDB() (err error) {
  19. db := testdb.SQLiteDB("../../certdb/testdb/certstore_development.db")
  20. expirationTime := time.Now().AddDate(1, 0, 0)
  21. var cert = certdb.CertificateRecord{
  22. Serial: "1",
  23. AKI: fakeAKI,
  24. Expiry: expirationTime,
  25. PEM: "revoked cert",
  26. Status: "revoked",
  27. RevokedAt: time.Now(),
  28. Reason: 4,
  29. }
  30. dbAccessor = sql.NewAccessor(db)
  31. err = dbAccessor.InsertCertificate(cert)
  32. if err != nil {
  33. return err
  34. }
  35. return
  36. }
  37. func verifyCRL(t *testing.T, crlBytesDER []byte, serial string, expireAfter time.Duration) {
  38. parsedCrl, err := x509.ParseCRL(crlBytesDER)
  39. if err != nil {
  40. t.Fatal("failed to get certificate ", err)
  41. }
  42. if !parsedCrl.HasExpired(time.Now().Add(expireAfter)) {
  43. t.Fatal("the CRL should have expired")
  44. }
  45. certs := parsedCrl.TBSCertList.RevokedCertificates
  46. if len(certs) != 1 {
  47. t.Fatal("failed to get one certificate")
  48. }
  49. cert := certs[0]
  50. if cert.SerialNumber.String() != serial {
  51. t.Fatal("cert was not correctly inserted in CRL, serial was " + cert.SerialNumber.String())
  52. }
  53. }
  54. func TestRevokeMain(t *testing.T) {
  55. err := prepDB()
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. crlBytes, err := generateCRL(cli.Config{CAFile: testCaFile, CAKeyFile: testCaKeyFile, DBConfigFile: "../testdata/db-config.json"})
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. verifyCRL(t, crlBytes, "1", 7*helpers.OneDay+time.Second)
  64. }
  65. func TestRevokeExpiry(t *testing.T) {
  66. err := prepDB()
  67. if err != nil {
  68. t.Fatal(err)
  69. }
  70. crlBytes, err := generateCRL(cli.Config{CAFile: testCaFile, CAKeyFile: testCaKeyFile, DBConfigFile: "../testdata/db-config.json", CRLExpiration: 23 * time.Hour})
  71. if err != nil {
  72. t.Fatal(err)
  73. }
  74. verifyCRL(t, crlBytes, "1", 23*time.Hour+time.Second)
  75. }