func TestGenerateOCSPResponses(t *testing.T) { updater, sa, _, fc, cleanUp := setup(t) defer cleanUp() reg := satest.CreateWorkingRegistration(t, sa) parsedCert, err := core.LoadCert("test-cert.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(parsedCert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add test-cert.pem") parsedCert, err = core.LoadCert("test-cert-b.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(parsedCert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add test-cert-b.pem") earliest := fc.Now().Add(-time.Hour) certs, err := updater.findStaleOCSPResponses(earliest, 10) test.AssertNotError(t, err, "Couldn't find stale responses") test.AssertEquals(t, len(certs), 2) updater.generateOCSPResponses(certs) certs, err = updater.findStaleOCSPResponses(earliest, 10) test.AssertNotError(t, err, "Failed to find stale responses") test.AssertEquals(t, len(certs), 0) }
func TestGenerateAndStoreOCSPResponse(t *testing.T) { updater, sa, _, _, cleanUp := setup(t) defer cleanUp() reg := satest.CreateWorkingRegistration(t, sa) parsedCert, err := core.LoadCert("test-cert.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(parsedCert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add www.eff.org.der") status, err := sa.GetCertificateStatus(core.SerialToString(parsedCert.SerialNumber)) test.AssertNotError(t, err, "Couldn't get the core.CertificateStatus from the database") meta, err := updater.generateResponse(status) test.AssertNotError(t, err, "Couldn't generate OCSP response") err = updater.storeResponse(meta) test.AssertNotError(t, err, "Couldn't store certificate status") secondMeta, err := updater.generateRevokedResponse(status) test.AssertNotError(t, err, "Couldn't generate revoked OCSP response") err = updater.storeResponse(secondMeta) test.AssertNotError(t, err, "Couldn't store certificate status") newStatus, err := sa.GetCertificateStatus(status.Serial) test.AssertNotError(t, err, "Couldn't retrieve certificate status") test.AssertByteEquals(t, meta.OCSPResponse, newStatus.OCSPResponse) }
func TestFindStaleOCSPResponses(t *testing.T) { updater, sa, _, fc, cleanUp := setup(t) defer cleanUp() reg := satest.CreateWorkingRegistration(t, sa) parsedCert, err := core.LoadCert("test-cert.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(parsedCert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add www.eff.org.der") earliest := fc.Now().Add(-time.Hour) certs, err := updater.findStaleOCSPResponses(earliest, 10) test.AssertNotError(t, err, "Couldn't find certificate") test.AssertEquals(t, len(certs), 1) status, err := sa.GetCertificateStatus(core.SerialToString(parsedCert.SerialNumber)) test.AssertNotError(t, err, "Couldn't get the core.Certificate from the database") meta, err := updater.generateResponse(status) test.AssertNotError(t, err, "Couldn't generate OCSP response") err = updater.storeResponse(meta) test.AssertNotError(t, err, "Couldn't store OCSP response") certs, err = updater.findStaleOCSPResponses(earliest, 10) test.AssertNotError(t, err, "Failed to find stale responses") test.AssertEquals(t, len(certs), 0) }
func TestStoreResponseGuard(t *testing.T) { updater, sa, _, _, cleanUp := setup(t) defer cleanUp() reg := satest.CreateWorkingRegistration(t, sa) parsedCert, err := core.LoadCert("test-cert.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(parsedCert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add www.eff.org.der") status, err := sa.GetCertificateStatus(core.SerialToString(parsedCert.SerialNumber)) test.AssertNotError(t, err, "Failed to get certificate status") status.OCSPResponse = []byte{0} err = updater.storeResponse(&status, core.OCSPStatusRevoked) test.AssertNotError(t, err, "Failed to update certificate status") unchangedStatus, err := sa.GetCertificateStatus(core.SerialToString(parsedCert.SerialNumber)) test.AssertNotError(t, err, "Failed to get certificate status") test.AssertEquals(t, len(unchangedStatus.OCSPResponse), 0) err = updater.storeResponse(&status, core.OCSPStatusGood) test.AssertNotError(t, err, "Failed to updated certificate status") changedStatus, err := sa.GetCertificateStatus(core.SerialToString(parsedCert.SerialNumber)) test.AssertNotError(t, err, "Failed to get certificate status") test.AssertEquals(t, len(changedStatus.OCSPResponse), 1) }
func TestGetCertificatesWithMissingResponses(t *testing.T) { updater, sa, _, _, cleanUp := setup(t) defer cleanUp() reg := satest.CreateWorkingRegistration(t, sa) cert, err := core.LoadCert("test-cert.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(cert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add www.eff.org.der") statuses, err := updater.getCertificatesWithMissingResponses(10) test.AssertNotError(t, err, "Couldn't get status") test.AssertEquals(t, len(statuses), 1) }
func TestGetAndProcessCerts(t *testing.T) { saDbMap, err := sa.NewDbMap(saDbConnStr) test.AssertNotError(t, err, "Couldn't connect to database") paDbMap, err := sa.NewDbMap(paDbConnStr) test.AssertNotError(t, err, "Couldn't connect to policy database") fc := clock.NewFake() checker := newChecker(saDbMap, paDbMap, fc, false) sa, err := sa.NewSQLStorageAuthority(saDbMap, fc) test.AssertNotError(t, err, "Couldn't create SA to insert certificates") saCleanUp := test.ResetTestDatabase(t, saDbMap.Db) paCleanUp := test.ResetTestDatabase(t, paDbMap.Db) defer func() { saCleanUp() paCleanUp() }() testKey, _ := rsa.GenerateKey(rand.Reader, 1024) // Problems // Expiry period is too long rawCert := x509.Certificate{ Subject: pkix.Name{ CommonName: "not-blacklisted.com", }, BasicConstraintsValid: true, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, } reg, err := sa.NewRegistration(core.Registration{ Key: satest.GoodJWK(), }) test.AssertNotError(t, err, "Couldn't create registration") for i := int64(0); i < 5; i++ { rawCert.SerialNumber = big.NewInt(i) certDER, err := x509.CreateCertificate(rand.Reader, &rawCert, &rawCert, &testKey.PublicKey, testKey) test.AssertNotError(t, err, "Couldn't create certificate") _, err = sa.AddCertificate(certDER, reg.ID) test.AssertNotError(t, err, "Couldn't add certificate") } err = checker.getCerts() test.AssertNotError(t, err, "Failed to retrieve certificates") test.AssertEquals(t, len(checker.certs), 5) wg := new(sync.WaitGroup) wg.Add(1) checker.processCerts(wg) test.AssertEquals(t, checker.issuedReport.BadCerts, int64(5)) test.AssertEquals(t, len(checker.issuedReport.Entries), 5) }
func TestOldOCSPResponsesTick(t *testing.T) { updater, sa, _, fc, cleanUp := setup(t) defer cleanUp() reg := satest.CreateWorkingRegistration(t, sa) parsedCert, err := core.LoadCert("test-cert.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(parsedCert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add www.eff.org.der") updater.ocspMinTimeToExpiry = 1 * time.Hour updater.oldOCSPResponsesTick(10) certs, err := updater.findStaleOCSPResponses(fc.Now().Add(-updater.ocspMinTimeToExpiry), 10) test.AssertNotError(t, err, "Failed to find stale responses") test.AssertEquals(t, len(certs), 0) }
func TestMissingReceiptsTick(t *testing.T) { updater, sa, _, _, cleanUp := setup(t) defer cleanUp() reg := satest.CreateWorkingRegistration(t, sa) parsedCert, err := core.LoadCert("test-cert.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(parsedCert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add www.eff.org.der") updater.numLogs = 1 updater.oldestIssuedSCT = 1 * time.Hour updater.missingReceiptsTick(10) count, err := updater.getNumberOfReceipts("00") test.AssertNotError(t, err, "Couldn't get number of receipts") test.AssertEquals(t, count, 1) }
func TestGetAndProcessCerts(t *testing.T) { saDbMap, err := sa.NewDbMap(vars.DBConnSA, 0) test.AssertNotError(t, err, "Couldn't connect to database") fc := clock.NewFake() checker := newChecker(saDbMap, fc, pa, expectedValidityPeriod) sa, err := sa.NewSQLStorageAuthority(saDbMap, fc, blog.NewMock()) test.AssertNotError(t, err, "Couldn't create SA to insert certificates") saCleanUp := test.ResetSATestDatabase(t) defer func() { saCleanUp() }() testKey, _ := rsa.GenerateKey(rand.Reader, 1024) // Problems // Expiry period is too long rawCert := x509.Certificate{ Subject: pkix.Name{ CommonName: "not-blacklisted.com", }, BasicConstraintsValid: true, DNSNames: []string{"not-blacklisted.com"}, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}, } reg := satest.CreateWorkingRegistration(t, sa) test.AssertNotError(t, err, "Couldn't create registration") for i := int64(0); i < 5; i++ { rawCert.SerialNumber = big.NewInt(mrand.Int63()) certDER, err := x509.CreateCertificate(rand.Reader, &rawCert, &rawCert, &testKey.PublicKey, testKey) test.AssertNotError(t, err, "Couldn't create certificate") _, err = sa.AddCertificate(context.Background(), certDER, reg.ID) test.AssertNotError(t, err, "Couldn't add certificate") } batchSize = 2 err = checker.getCerts(false) test.AssertNotError(t, err, "Failed to retrieve certificates") test.AssertEquals(t, len(checker.certs), 5) wg := new(sync.WaitGroup) wg.Add(1) checker.processCerts(wg, false) test.AssertEquals(t, checker.issuedReport.BadCerts, int64(5)) test.AssertEquals(t, len(checker.issuedReport.Entries), 5) }
func TestTotalCertRateLimit(t *testing.T) { _, sa, ra, fc, cleanUp := initAuthorities(t) defer cleanUp() ra.rlPolicies = &dummyRateLimitConfig{ TotalCertificatesPolicy: ratelimit.RateLimitPolicy{ Threshold: 1, Window: cmd.ConfigDuration{Duration: 24 * 90 * time.Hour}, }, } fc.Add(24 * 90 * time.Hour) AuthzFinal.RegistrationID = Registration.ID AuthzFinal, err := sa.NewPendingAuthorization(ctx, AuthzFinal) test.AssertNotError(t, err, "Could not store test data") err = sa.FinalizeAuthorization(ctx, AuthzFinal) // Inject another final authorization to cover www.not-example.com authzFinalWWW := AuthzFinal authzFinalWWW.Identifier.Value = "www.not-example.com" authzFinalWWW, err = sa.NewPendingAuthorization(ctx, authzFinalWWW) test.AssertNotError(t, err, "Could not store test data") err = sa.FinalizeAuthorization(ctx, authzFinalWWW) test.AssertNotError(t, err, "Could not store test data") ExampleCSR.Subject.CommonName = "www.NOT-example.com" certRequest := core.CertificateRequest{ CSR: ExampleCSR, } // TODO(jsha): Since we're using a real SA rather than a mock, we call // NewCertificate twice and insert the first result into the SA. Instead we // should mock out the SA and have it return the cert count that we want. cert, err := ra.NewCertificate(ctx, certRequest, Registration.ID) test.AssertNotError(t, err, "Failed to issue certificate") _, err = sa.AddCertificate(ctx, cert.DER, Registration.ID) test.AssertNotError(t, err, "Failed to store certificate") fc.Add(time.Hour) _, err = ra.NewCertificate(ctx, certRequest, Registration.ID) test.AssertError(t, err, "Total certificate rate limit failed") }
func TestFindRevokedCertificatesToUpdate(t *testing.T) { updater, sa, _, _, cleanUp := setup(t) defer cleanUp() reg := satest.CreateWorkingRegistration(t, sa) cert, err := core.LoadCert("test-cert.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(cert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add www.eff.org.der") statuses, err := updater.findRevokedCertificatesToUpdate(10) test.AssertNotError(t, err, "Failed to find revoked certificates") test.AssertEquals(t, len(statuses), 0) err = sa.MarkCertificateRevoked(core.SerialToString(cert.SerialNumber), core.RevocationCode(1)) test.AssertNotError(t, err, "Failed to revoke certificate") statuses, err = updater.findRevokedCertificatesToUpdate(10) test.AssertNotError(t, err, "Failed to find revoked certificates") test.AssertEquals(t, len(statuses), 1) }
func TestStoreResponseGuard(t *testing.T) { updater, sa, _, _, cleanUp := setup(t) defer cleanUp() reg := satest.CreateWorkingRegistration(t, sa) parsedCert, err := core.LoadCert("test-cert.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(parsedCert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add www.eff.org.der") status, err := sa.GetCertificateStatus(core.SerialToString(parsedCert.SerialNumber)) test.AssertNotError(t, err, "Failed to get certificate status") err = sa.MarkCertificateRevoked(core.SerialToString(parsedCert.SerialNumber), 0) test.AssertNotError(t, err, "Failed to revoked certificate") // Attempt to update OCSP response where status.Status is good but stored status // is revoked, this should fail silently status.OCSPResponse = []byte{0, 1, 1} err = updater.storeResponse(&status) test.AssertNotError(t, err, "Failed to update certificate status") // Make sure the OCSP response hasn't actually changed unchangedStatus, err := sa.GetCertificateStatus(core.SerialToString(parsedCert.SerialNumber)) test.AssertNotError(t, err, "Failed to get certificate status") test.AssertEquals(t, len(unchangedStatus.OCSPResponse), 0) // Changing the status to the stored status should allow the update to occur status.Status = core.OCSPStatusRevoked err = updater.storeResponse(&status) test.AssertNotError(t, err, "Failed to updated certificate status") // Make sure the OCSP response has been updated changedStatus, err := sa.GetCertificateStatus(core.SerialToString(parsedCert.SerialNumber)) test.AssertNotError(t, err, "Failed to get certificate status") test.AssertEquals(t, len(changedStatus.OCSPResponse), 3) }
func TestRevokedCertificatesTick(t *testing.T) { updater, sa, _, _, cleanUp := setup(t) defer cleanUp() reg := satest.CreateWorkingRegistration(t, sa) parsedCert, err := core.LoadCert("test-cert.pem") test.AssertNotError(t, err, "Couldn't read test certificate") _, err = sa.AddCertificate(parsedCert.Raw, reg.ID) test.AssertNotError(t, err, "Couldn't add www.eff.org.der") err = sa.MarkCertificateRevoked(core.SerialToString(parsedCert.SerialNumber), core.RevocationCode(1)) test.AssertNotError(t, err, "Failed to revoke certificate") statuses, err := updater.findRevokedCertificatesToUpdate(10) test.AssertNotError(t, err, "Failed to find revoked certificates") test.AssertEquals(t, len(statuses), 1) updater.revokedCertificatesTick(10) status, err := sa.GetCertificateStatus(core.SerialToString(parsedCert.SerialNumber)) test.AssertNotError(t, err, "Failed to get certificate status") test.AssertEquals(t, status.Status, core.OCSPStatusRevoked) test.Assert(t, len(status.OCSPResponse) != 0, "Certificate status doesn't contain OCSP response") }