Skip to content

Commit c5f51b8

Browse files
committed
Use GetRevocationStatus instead of GetCertificateStatus (#8402)
In the SA, change the implementation of GetRevocationStatus to read from the revokedCertificates table instead of reading from the certificateStatus table. In the WFE, switch to calling GetRevocationStatus when computing ARI windows. Similarly, in the RA, make the same switch when checking if a to-be-revoked certificate is already revoked. Across all three locations, use new core constants to represent "good" and "revoked", to avoid references to OCSP and unwieldy string/int conversions. This paves the way for removing sa.GetCertificateStatus, which now has only one remaining caller which is not quite so easily changed. Part of #8322 (cherry picked from commit a5cf372)
1 parent d8c5e81 commit c5f51b8

File tree

9 files changed

+87
-151
lines changed

9 files changed

+87
-151
lines changed

core/objects.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,11 @@ var OCSPStatusToInt = map[OCSPStatus]int{
8383
OCSPStatusRevoked: ocsp.Revoked,
8484
}
8585

86+
const (
87+
RevocationStatusGood int64 = 0
88+
RevocationStatusRevoked int64 = 1
89+
)
90+
8691
// DNSPrefix is attached to DNS names in DNS challenges
8792
const DNSPrefix = "_acme-challenge"
8893

mocks/sa.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ func (sa *StorageAuthorityReadOnly) GetCertificateStatus(_ context.Context, req
213213

214214
// GetRevocationStatus is a mock
215215
func (sa *StorageAuthorityReadOnly) GetRevocationStatus(_ context.Context, req *sapb.Serial, _ ...grpc.CallOption) (*sapb.RevocationStatus, error) {
216-
return nil, nil
216+
return nil, errors.New("no revocation status")
217217
}
218218

219219
// SerialsForIncident is a mock

ra/ra.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,12 +1683,12 @@ func (ra *RegistrationAuthorityImpl) revokeCertificate(ctx context.Context, cert
16831683
// certificates that were previously revoked for a reason other than
16841684
// keyCompromise, and which are now being updated to keyCompromise instead.
16851685
func (ra *RegistrationAuthorityImpl) updateRevocationForKeyCompromise(ctx context.Context, serialString string, issuerID issuance.NameID) error {
1686-
status, err := ra.SA.GetCertificateStatus(ctx, &sapb.Serial{Serial: serialString})
1686+
status, err := ra.SA.GetRevocationStatus(ctx, &sapb.Serial{Serial: serialString})
16871687
if err != nil {
16881688
return berrors.NotFoundError("unable to confirm that serial %q was ever issued: %s", serialString, err)
16891689
}
16901690

1691-
if status.Status != string(core.OCSPStatusRevoked) {
1691+
if status.Status != core.RevocationStatusRevoked {
16921692
// Internal server error, because we shouldn't be in the function at all
16931693
// unless the cert was already revoked.
16941694
return fmt.Errorf("unable to re-revoke serial %q which is not currently revoked", serialString)

ra/ra_test.go

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3511,20 +3511,20 @@ type mockSARevocation struct {
35113511
sapb.StorageAuthorityClient
35123512

35133513
known map[string]*x509.Certificate
3514-
revoked map[string]*corepb.CertificateStatus
3514+
revoked map[string]*sapb.RevocationStatus
35153515
blocked []*sapb.AddBlockedKeyRequest
35163516
}
35173517

35183518
func newMockSARevocation(known *x509.Certificate) *mockSARevocation {
35193519
return &mockSARevocation{
35203520
known: map[string]*x509.Certificate{core.SerialToString(known.SerialNumber): known},
3521-
revoked: make(map[string]*corepb.CertificateStatus),
3521+
revoked: make(map[string]*sapb.RevocationStatus),
35223522
blocked: make([]*sapb.AddBlockedKeyRequest, 0),
35233523
}
35243524
}
35253525

35263526
func (msar *mockSARevocation) reset() {
3527-
msar.revoked = make(map[string]*corepb.CertificateStatus)
3527+
msar.revoked = make(map[string]*sapb.RevocationStatus)
35283528
msar.blocked = make([]*sapb.AddBlockedKeyRequest, 0)
35293529
}
35303530

@@ -3552,14 +3552,13 @@ func (msar *mockSARevocation) GetLintPrecertificate(_ context.Context, req *sapb
35523552
return nil, berrors.UnknownSerialError()
35533553
}
35543554

3555-
func (msar *mockSARevocation) GetCertificateStatus(_ context.Context, req *sapb.Serial, _ ...grpc.CallOption) (*corepb.CertificateStatus, error) {
3555+
func (msar *mockSARevocation) GetRevocationStatus(_ context.Context, req *sapb.Serial, _ ...grpc.CallOption) (*sapb.RevocationStatus, error) {
35563556
if status, present := msar.revoked[req.Serial]; present {
35573557
return status, nil
35583558
}
3559-
if cert, present := msar.known[req.Serial]; present {
3560-
return &corepb.CertificateStatus{
3561-
Serial: core.SerialToString(cert.SerialNumber),
3562-
IssuerID: int64(issuance.IssuerNameID(cert)),
3559+
if _, present := msar.known[req.Serial]; present {
3560+
return &sapb.RevocationStatus{
3561+
Status: core.RevocationStatusGood,
35633562
}, nil
35643563
}
35653564
return nil, berrors.UnknownSerialError()
@@ -3598,14 +3597,12 @@ func (msar *mockSARevocation) RevokeCertificate(_ context.Context, req *sapb.Rev
35983597
if _, present := msar.revoked[req.Serial]; present {
35993598
return nil, berrors.AlreadyRevokedError("already revoked")
36003599
}
3601-
cert, present := msar.known[req.Serial]
3600+
_, present := msar.known[req.Serial]
36023601
if !present {
36033602
return nil, berrors.UnknownSerialError()
36043603
}
3605-
msar.revoked[req.Serial] = &corepb.CertificateStatus{
3606-
Serial: req.Serial,
3607-
IssuerID: int64(issuance.IssuerNameID(cert)),
3608-
Status: string(core.OCSPStatusRevoked),
3604+
msar.revoked[req.Serial] = &sapb.RevocationStatus{
3605+
Status: core.RevocationStatusRevoked,
36093606
RevokedReason: req.Reason,
36103607
}
36113608
return &emptypb.Empty{}, nil
@@ -3772,7 +3769,7 @@ func TestRevokeCertByKey(t *testing.T) {
37723769

37733770
// Reset and have the Subscriber revoke for a different reason.
37743771
// Then re-revoking using the key should work.
3775-
mockSA.revoked = make(map[string]*corepb.CertificateStatus)
3772+
mockSA.revoked = make(map[string]*sapb.RevocationStatus)
37763773
_, err = ra.RevokeCertByApplicant(context.Background(), &rapb.RevokeCertByApplicantRequest{
37773774
Cert: cert.Raw,
37783775
Code: int64(revocation.Unspecified),

sa/model.go

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -161,41 +161,6 @@ func SelectCertificateStatus(ctx context.Context, s db.OneSelector, serial strin
161161
return model.toPb(), err
162162
}
163163

164-
// RevocationStatusModel represents a small subset of the columns in the
165-
// certificateStatus table, used to determine the authoritative revocation
166-
// status of a certificate.
167-
type RevocationStatusModel struct {
168-
Status core.OCSPStatus `db:"status"`
169-
RevokedDate time.Time `db:"revokedDate"`
170-
RevokedReason revocation.Reason `db:"revokedReason"`
171-
}
172-
173-
// SelectRevocationStatus returns the authoritative revocation information for
174-
// the certificate with the given serial.
175-
func SelectRevocationStatus(ctx context.Context, s db.OneSelector, serial string) (*sapb.RevocationStatus, error) {
176-
var model RevocationStatusModel
177-
err := s.SelectOne(
178-
ctx,
179-
&model,
180-
"SELECT status, revokedDate, revokedReason FROM certificateStatus WHERE serial = ? LIMIT 1",
181-
serial,
182-
)
183-
if err != nil {
184-
return nil, err
185-
}
186-
187-
statusInt, ok := core.OCSPStatusToInt[model.Status]
188-
if !ok {
189-
return nil, fmt.Errorf("got unrecognized status %q", model.Status)
190-
}
191-
192-
return &sapb.RevocationStatus{
193-
Status: int64(statusInt),
194-
RevokedDate: timestamppb.New(model.RevokedDate),
195-
RevokedReason: int64(model.RevokedReason),
196-
}, nil
197-
}
198-
199164
var mediumBlobSize = int(math.Pow(2, 24))
200165

201166
type issuedNameModel struct {

sa/sa_test.go

Lines changed: 26 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -396,15 +396,19 @@ func TestAddPrecertificate(t *testing.T) {
396396
defer cleanUp()
397397

398398
reg := createWorkingRegistration(t, sa)
399+
regID := reg.Id
399400

400-
// Create a throw-away self signed certificate with a random name and
401-
// serial number
401+
// Add a cert to the DB to test with.
402402
serial, testCert := test.ThrowAwayCert(t, clk)
403-
404-
// Add the cert as a precertificate
405-
regID := reg.Id
403+
_, err := sa.AddSerial(ctx, &sapb.AddSerialRequest{
404+
RegID: regID,
405+
Serial: serial,
406+
Created: timestamppb.New(testCert.NotBefore),
407+
Expires: timestamppb.New(testCert.NotAfter),
408+
})
409+
test.AssertNotError(t, err, "failed to add test serial")
406410
issuedTime := mustTimestamp("2018-04-01 07:00")
407-
_, err := sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
411+
_, err = sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
408412
Der: testCert.Raw,
409413
RegID: regID,
410414
Issued: issuedTime,
@@ -413,11 +417,9 @@ func TestAddPrecertificate(t *testing.T) {
413417
test.AssertNotError(t, err, "Couldn't add test cert")
414418

415419
// It should have the expected certificate status
416-
certStatus, err := sa.GetCertificateStatus(ctx, &sapb.Serial{Serial: serial})
420+
certStatus, err := sa.GetRevocationStatus(ctx, &sapb.Serial{Serial: serial})
417421
test.AssertNotError(t, err, "Couldn't get status for test cert")
418-
test.AssertEquals(t, certStatus.Status, string(core.OCSPStatusGood))
419-
now := clk.Now()
420-
test.AssertEquals(t, now, certStatus.OcspLastUpdated.AsTime())
422+
test.AssertEquals(t, certStatus.Status, core.RevocationStatusGood)
421423

422424
// It should show up in the issued names table
423425
issuedNamesSerial, err := findIssuedName(ctx, sa.dbMap, reverseFQDN(testCert.DNSNames[0]))
@@ -1787,10 +1789,9 @@ func TestRevokeCertificate(t *testing.T) {
17871789
sa, fc, cleanUp := initSA(t)
17881790
defer cleanUp()
17891791

1790-
reg := createWorkingRegistration(t, sa)
17911792
// Add a cert to the DB to test with.
1793+
reg := createWorkingRegistration(t, sa)
17921794
serial, testCert := test.ThrowAwayCert(t, fc)
1793-
issuedTime := sa.clk.Now()
17941795
_, err := sa.AddSerial(ctx, &sapb.AddSerialRequest{
17951796
RegID: reg.Id,
17961797
Serial: core.SerialToString(testCert.SerialNumber),
@@ -1801,14 +1802,14 @@ func TestRevokeCertificate(t *testing.T) {
18011802
_, err = sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
18021803
Der: testCert.Raw,
18031804
RegID: reg.Id,
1804-
Issued: timestamppb.New(issuedTime),
1805+
Issued: timestamppb.New(testCert.NotBefore),
18051806
IssuerNameID: 1,
18061807
})
18071808
test.AssertNotError(t, err, "Couldn't add test cert")
18081809

1809-
status, err := sa.GetCertificateStatus(ctx, &sapb.Serial{Serial: serial})
1810+
status, err := sa.GetRevocationStatus(ctx, &sapb.Serial{Serial: serial})
18101811
test.AssertNotError(t, err, "GetCertificateStatus failed")
1811-
test.AssertEquals(t, core.OCSPStatus(status.Status), core.OCSPStatusGood)
1812+
test.AssertEquals(t, status.Status, core.RevocationStatusGood)
18121813

18131814
fc.Add(1 * time.Hour)
18141815

@@ -1824,12 +1825,11 @@ func TestRevokeCertificate(t *testing.T) {
18241825
})
18251826
test.AssertNotError(t, err, "RevokeCertificate with no OCSP response should succeed")
18261827

1827-
status, err = sa.GetCertificateStatus(ctx, &sapb.Serial{Serial: serial})
1828+
status, err = sa.GetRevocationStatus(ctx, &sapb.Serial{Serial: serial})
18281829
test.AssertNotError(t, err, "GetCertificateStatus failed")
1829-
test.AssertEquals(t, core.OCSPStatus(status.Status), core.OCSPStatusRevoked)
1830+
test.AssertEquals(t, status.Status, core.RevocationStatusRevoked)
18301831
test.AssertEquals(t, status.RevokedReason, reason)
18311832
test.AssertEquals(t, status.RevokedDate.AsTime(), now)
1832-
test.AssertEquals(t, status.OcspLastUpdated.AsTime(), now)
18331833

18341834
_, err = sa.RevokeCertificate(context.Background(), &sapb.RevokeCertificateRequest{
18351835
IssuerID: 1,
@@ -1840,67 +1840,13 @@ func TestRevokeCertificate(t *testing.T) {
18401840
test.AssertError(t, err, "RevokeCertificate should've failed when certificate already revoked")
18411841
}
18421842

1843-
func TestRevokeCertificateWithShard(t *testing.T) {
1844-
sa, fc, cleanUp := initSA(t)
1845-
defer cleanUp()
1846-
1847-
// Add a cert to the DB to test with.
1848-
reg := createWorkingRegistration(t, sa)
1849-
eeCert, err := core.LoadCert("../test/hierarchy/ee-e1.cert.pem")
1850-
test.AssertNotError(t, err, "failed to load test cert")
1851-
_, err = sa.AddSerial(ctx, &sapb.AddSerialRequest{
1852-
RegID: reg.Id,
1853-
Serial: core.SerialToString(eeCert.SerialNumber),
1854-
Created: timestamppb.New(eeCert.NotBefore),
1855-
Expires: timestamppb.New(eeCert.NotAfter),
1856-
})
1857-
test.AssertNotError(t, err, "failed to add test serial")
1858-
_, err = sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
1859-
Der: eeCert.Raw,
1860-
RegID: reg.Id,
1861-
Issued: timestamppb.New(eeCert.NotBefore),
1862-
IssuerNameID: 1,
1863-
})
1864-
test.AssertNotError(t, err, "failed to add test cert")
1865-
1866-
serial := core.SerialToString(eeCert.SerialNumber)
1867-
fc.Add(1 * time.Hour)
1868-
now := fc.Now()
1869-
reason := int64(1)
1870-
1871-
_, err = sa.RevokeCertificate(context.Background(), &sapb.RevokeCertificateRequest{
1872-
IssuerID: 1,
1873-
ShardIdx: 9,
1874-
Serial: serial,
1875-
Date: timestamppb.New(now),
1876-
Reason: reason,
1877-
})
1878-
test.AssertNotError(t, err, "RevokeCertificate with no OCSP response should succeed")
1879-
1880-
status, err := sa.GetCertificateStatus(ctx, &sapb.Serial{Serial: serial})
1881-
test.AssertNotError(t, err, "GetCertificateStatus failed")
1882-
test.AssertEquals(t, core.OCSPStatus(status.Status), core.OCSPStatusRevoked)
1883-
test.AssertEquals(t, status.RevokedReason, reason)
1884-
test.AssertEquals(t, status.RevokedDate.AsTime(), now)
1885-
test.AssertEquals(t, status.OcspLastUpdated.AsTime(), now)
1886-
test.AssertEquals(t, status.NotAfter.AsTime(), eeCert.NotAfter)
1887-
1888-
var result revokedCertModel
1889-
err = sa.dbMap.SelectOne(
1890-
ctx, &result, `SELECT * FROM revokedCertificates WHERE serial = ?`, core.SerialToString(eeCert.SerialNumber))
1891-
test.AssertNotError(t, err, "should be exactly one row in revokedCertificates")
1892-
test.AssertEquals(t, result.ShardIdx, int64(9))
1893-
test.AssertEquals(t, result.RevokedReason, revocation.KeyCompromise)
1894-
}
1895-
18961843
func TestUpdateRevokedCertificate(t *testing.T) {
18971844
sa, fc, cleanUp := initSA(t)
18981845
defer cleanUp()
18991846

19001847
// Add a cert to the DB to test with.
19011848
reg := createWorkingRegistration(t, sa)
19021849
serial, testCert := test.ThrowAwayCert(t, fc)
1903-
issuedTime := fc.Now()
19041850
_, err := sa.AddSerial(ctx, &sapb.AddSerialRequest{
19051851
RegID: reg.Id,
19061852
Serial: core.SerialToString(testCert.SerialNumber),
@@ -1911,7 +1857,7 @@ func TestUpdateRevokedCertificate(t *testing.T) {
19111857
_, err = sa.AddPrecertificate(ctx, &sapb.AddCertificateRequest{
19121858
Der: testCert.Raw,
19131859
RegID: reg.Id,
1914-
Issued: timestamppb.New(issuedTime),
1860+
Issued: timestamppb.New(testCert.NotBefore),
19151861
IssuerNameID: 1,
19161862
})
19171863
test.AssertNotError(t, err, "Couldn't add test cert")
@@ -1944,10 +1890,10 @@ func TestUpdateRevokedCertificate(t *testing.T) {
19441890
test.AssertNotError(t, err, "RevokeCertificate failed")
19451891

19461892
// Double check that setup worked.
1947-
status, err := sa.GetCertificateStatus(ctx, &sapb.Serial{Serial: serial})
1893+
status, err := sa.GetRevocationStatus(ctx, &sapb.Serial{Serial: serial})
19481894
test.AssertNotError(t, err, "GetCertificateStatus failed")
1949-
test.AssertEquals(t, core.OCSPStatus(status.Status), core.OCSPStatusRevoked)
1950-
test.AssertEquals(t, revocation.Reason(status.RevokedReason), revocation.CessationOfOperation)
1895+
test.AssertEquals(t, status.Status, core.RevocationStatusRevoked)
1896+
test.AssertEquals(t, status.RevokedReason, int64(revocation.CessationOfOperation))
19511897
fc.Add(1 * time.Hour)
19521898

19531899
// Try to update its revocation info with no backdate
@@ -3000,10 +2946,10 @@ func TestGetRevokedCerts(t *testing.T) {
30002946
test.AssertNotError(t, err, "failed to add test cert")
30012947

30022948
// Check that it worked.
3003-
status, err := sa.GetCertificateStatus(
2949+
status, err := sa.GetRevocationStatus(
30042950
ctx, &sapb.Serial{Serial: core.SerialToString(eeCert.SerialNumber)})
30052951
test.AssertNotError(t, err, "GetCertificateStatus failed")
3006-
test.AssertEquals(t, core.OCSPStatus(status.Status), core.OCSPStatusGood)
2952+
test.AssertEquals(t, status.Status, core.RevocationStatusGood)
30072953

30082954
// Here's a little helper func we'll use to call GetRevokedCerts and count
30092955
// how many results it returned.
@@ -3107,10 +3053,10 @@ func TestGetRevokedCertsByShard(t *testing.T) {
31073053
test.AssertNotError(t, err, "failed to add test cert")
31083054

31093055
// Check that it worked.
3110-
status, err := sa.GetCertificateStatus(
3056+
status, err := sa.GetRevocationStatus(
31113057
ctx, &sapb.Serial{Serial: core.SerialToString(eeCert.SerialNumber)})
31123058
test.AssertNotError(t, err, "GetCertificateStatus failed")
3113-
test.AssertEquals(t, core.OCSPStatus(status.Status), core.OCSPStatusGood)
3059+
test.AssertEquals(t, status.Status, core.RevocationStatusGood)
31143060

31153061
// Here's a little helper func we'll use to call GetRevokedCertsByShard and count
31163062
// how many results it returned.

0 commit comments

Comments
 (0)