Răsfoiți Sursa

Add revocation reason code to CRL | replace deprecated pkix.RevokeCertificate

authored-by: gwe32 <139247241+gwe32@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
Max 8 luni în urmă
părinte
comite
07e4424836
2 a modificat fișierele cu 88 adăugiri și 16 ștergeri
  1. 9 9
      authority/tls.go
  2. 79 7
      authority/tls_test.go

+ 9 - 9
authority/tls.go

@@ -805,9 +805,9 @@ func (a *Authority) GenerateCertificateRevocationList() error {
 		bn.SetInt64(crlInfo.Number + 1)
 	}
 
-	// Convert our database db.RevokedCertificateInfo types into the pkix
+	// Convert our database db.RevokedCertificateInfo types into the x509
 	// representation ready for the CAS to sign it
-	var revokedCertificates []pkix.RevokedCertificate
+	var revokedCertificateEntries []x509.RevocationListEntry
 	skipExpiredTime := now.Add(-config.DefaultCRLExpiredDuration)
 	for _, revokedCert := range *revokedList {
 		// skip expired certificates
@@ -817,10 +817,10 @@ func (a *Authority) GenerateCertificateRevocationList() error {
 
 		var sn big.Int
 		sn.SetString(revokedCert.Serial, 10)
-		revokedCertificates = append(revokedCertificates, pkix.RevokedCertificate{
+		revokedCertificateEntries = append(revokedCertificateEntries, x509.RevocationListEntry{
 			SerialNumber:   &sn,
 			RevocationTime: revokedCert.RevokedAt,
-			Extensions:     nil,
+			ReasonCode:     revokedCert.ReasonCode,
 		})
 	}
 
@@ -834,11 +834,11 @@ func (a *Authority) GenerateCertificateRevocationList() error {
 	// Create a RevocationList representation ready for the CAS to sign
 	// TODO: allow SignatureAlgorithm to be specified?
 	revocationList := x509.RevocationList{
-		SignatureAlgorithm:  0,
-		RevokedCertificates: revokedCertificates,
-		Number:              &bn,
-		ThisUpdate:          now,
-		NextUpdate:          now.Add(updateDuration),
+		SignatureAlgorithm:        0,
+		RevokedCertificateEntries: revokedCertificateEntries,
+		Number:                    &bn,
+		ThisUpdate:                now,
+		NextUpdate:                now.Add(updateDuration),
 	}
 
 	// Set CRL IDP to config item, otherwise, leave as default

+ 79 - 7
authority/tls_test.go

@@ -1942,10 +1942,11 @@ func TestAuthority_CRL(t *testing.T) {
 	var revokedList []db.RevokedCertificateInfo
 
 	type test struct {
-		auth     *Authority
-		ctx      context.Context
-		expected []string
-		err      error
+		auth               *Authority
+		ctx                context.Context
+		expected           []string
+		expectedReasonCode *int
+		err                error
 	}
 	tests := map[string]func() test{
 		"fail/empty-crl": func() test {
@@ -2038,9 +2039,72 @@ func TestAuthority_CRL(t *testing.T) {
 			}
 
 			return test{
-				auth:     a,
-				ctx:      crlCtx,
-				expected: ex,
+				auth:               a,
+				ctx:                crlCtx,
+				expected:           ex,
+				expectedReasonCode: &reasonCode,
+			}
+		},
+		"ok/crl-no-reason-code": func() test {
+			var localRevokedList []db.RevokedCertificateInfo
+			var localCRLStore db.CertificateRevocationListInfo
+			a := testAuthority(t, WithDatabase(&db.MockAuthDB{
+				MUseToken: func(id, tok string) (bool, error) {
+					return true, nil
+				},
+				MGetCertificate: func(sn string) (*x509.Certificate, error) {
+					return nil, errors.New("not found")
+				},
+				MStoreCRL: func(i *db.CertificateRevocationListInfo) error {
+					localCRLStore = *i
+					return nil
+				},
+				MGetCRL: func() (*db.CertificateRevocationListInfo, error) {
+					return &localCRLStore, nil
+				},
+				MGetRevokedCertificates: func() (*[]db.RevokedCertificateInfo, error) {
+					return &localRevokedList, nil
+				},
+				MRevoke: func(rci *db.RevokedCertificateInfo) error {
+					localRevokedList = append(localRevokedList, *rci)
+					return nil
+				},
+			}))
+			a.config.CRL = &config.CRLConfig{
+				Enabled:          true,
+				GenerateOnRevoke: true,
+			}
+
+			var ex []string
+			zeroReasonCode := 0
+
+			for i := 0; i < 5; i++ {
+				sn := fmt.Sprintf("%v", i)
+				cl := jose.Claims{
+					Subject:   sn,
+					Issuer:    validIssuer,
+					NotBefore: jose.NewNumericDate(now),
+					Expiry:    jose.NewNumericDate(now.Add(time.Minute)),
+					Audience:  validAudience,
+					ID:        sn,
+				}
+				raw, err := jose.Signed(sig).Claims(cl).CompactSerialize()
+				require.NoError(t, err)
+				err = a.Revoke(crlCtx, &RevokeOptions{
+					Serial:     sn,
+					ReasonCode: zeroReasonCode,
+					Reason:     reason,
+					OTT:        raw,
+				})
+				require.NoError(t, err)
+				ex = append(ex, sn)
+			}
+
+			return test{
+				auth:               a,
+				ctx:                crlCtx,
+				expected:           ex,
+				expectedReasonCode: &zeroReasonCode,
 			}
 		},
 	}
@@ -2060,6 +2124,14 @@ func TestAuthority_CRL(t *testing.T) {
 			var cmpList []string
 			for _, c := range crl.RevokedCertificateEntries {
 				cmpList = append(cmpList, c.SerialNumber.String())
+				// ReasonCode 0 causes Go's x509 package to omit the reasonCode
+				// extension entirely. Parsing it back yields 0 as the zero value
+				// of the field, not from an explicit extension. This confirms the
+				// zero-code path produces a well-formed CRL, not that the
+				// extension round-trips.
+				if tc.expectedReasonCode != nil {
+					assert.Equal(t, *tc.expectedReasonCode, c.ReasonCode)
+				}
 			}
 
 			assert.Equal(t, tc.expected, cmpList)