Skip to content

Commit 43e6407

Browse files
committed
fix: resolve comments for Two-Hearts
Signed-off-by: Junjie Gao <junjiegao@microsoft.com>
1 parent 6becbe1 commit 43e6407

File tree

5 files changed

+113
-37
lines changed

5 files changed

+113
-37
lines changed

revocation/crl/fetcher.go

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ import (
2525
"io"
2626
"net/http"
2727
"net/url"
28-
"slices"
2928
"time"
3029

30+
"github.com/notaryproject/notation-core-go/revocation/internal/x509util"
3131
"golang.org/x/crypto/cryptobyte"
32-
cryptobyte_asn1 "golang.org/x/crypto/cryptobyte/asn1"
32+
cbasn1 "golang.org/x/crypto/cryptobyte/asn1"
3333
)
3434

3535
// oidFreshestCRL is the object identifier for the distribution point
@@ -142,17 +142,16 @@ func (f *HTTPFetcher) fetch(ctx context.Context, url string) (*Bundle, error) {
142142
//
143143
// It returns errDeltaCRLNotFound if the delta CRL is not found.
144144
func (f *HTTPFetcher) fetchDeltaCRL(ctx context.Context, extensions []pkix.Extension) (*x509.RevocationList, error) {
145-
idx := slices.IndexFunc(extensions, func(ext pkix.Extension) bool {
146-
return ext.Id.Equal(oidFreshestCRL)
147-
})
148-
if idx < 0 {
145+
extension := x509util.FindExtensionByOID(oidFreshestCRL, extensions)
146+
if extension == nil {
149147
return nil, errDeltaCRLNotFound
150148
}
149+
151150
// RFC 5280, 4.2.1.15
152151
// id-ce-freshestCRL OBJECT IDENTIFIER ::= { id-ce 46 }
153152
//
154153
// FreshestCRL ::= CRLDistributionPoints
155-
urls, err := parseCRLDistributionPoint(extensions[idx].Value)
154+
urls, err := parseCRLDistributionPoint(extension.Value)
156155
if err != nil {
157156
return nil, fmt.Errorf("failed to parse Freshest CRL extension: %w", err)
158157
}
@@ -197,31 +196,31 @@ func parseCRLDistributionPoint(value []byte) ([]string, error) {
197196
// fullName [0] GeneralNames,
198197
// nameRelativeToCRLIssuer [1] RelativeDistinguishedName }
199198
val := cryptobyte.String(value)
200-
if !val.ReadASN1(&val, cryptobyte_asn1.SEQUENCE) {
199+
if !val.ReadASN1(&val, cbasn1.SEQUENCE) {
201200
return nil, errors.New("x509: invalid CRL distribution points")
202201
}
203202
for !val.Empty() {
204203
var dpDER cryptobyte.String
205-
if !val.ReadASN1(&dpDER, cryptobyte_asn1.SEQUENCE) {
204+
if !val.ReadASN1(&dpDER, cbasn1.SEQUENCE) {
206205
return nil, errors.New("x509: invalid CRL distribution point")
207206
}
208207
var dpNameDER cryptobyte.String
209208
var dpNamePresent bool
210-
if !dpDER.ReadOptionalASN1(&dpNameDER, &dpNamePresent, cryptobyte_asn1.Tag(0).Constructed().ContextSpecific()) {
209+
if !dpDER.ReadOptionalASN1(&dpNameDER, &dpNamePresent, cbasn1.Tag(0).Constructed().ContextSpecific()) {
211210
return nil, errors.New("x509: invalid CRL distribution point")
212211
}
213212
if !dpNamePresent {
214213
continue
215214
}
216-
if !dpNameDER.ReadASN1(&dpNameDER, cryptobyte_asn1.Tag(0).Constructed().ContextSpecific()) {
215+
if !dpNameDER.ReadASN1(&dpNameDER, cbasn1.Tag(0).Constructed().ContextSpecific()) {
217216
return nil, errors.New("x509: invalid CRL distribution point")
218217
}
219218
for !dpNameDER.Empty() {
220-
if !dpNameDER.PeekASN1Tag(cryptobyte_asn1.Tag(6).ContextSpecific()) {
219+
if !dpNameDER.PeekASN1Tag(cbasn1.Tag(6).ContextSpecific()) {
221220
break
222221
}
223222
var uri cryptobyte.String
224-
if !dpNameDER.ReadASN1(&uri, cryptobyte_asn1.Tag(6).ContextSpecific()) {
223+
if !dpNameDER.ReadASN1(&uri, cbasn1.Tag(6).ContextSpecific()) {
225224
return nil, errors.New("x509: invalid CRL distribution point")
226225
}
227226
urls = append(urls, string(uri))

revocation/internal/crl/crl.go

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -23,30 +23,34 @@ import (
2323
"errors"
2424
"fmt"
2525
"math/big"
26-
"slices"
2726
"time"
2827

2928
"github.com/notaryproject/notation-core-go/revocation/crl"
29+
"github.com/notaryproject/notation-core-go/revocation/internal/x509util"
3030
"github.com/notaryproject/notation-core-go/revocation/result"
3131
"golang.org/x/crypto/cryptobyte"
3232
)
3333

34+
// RFC 5280, 5.3.1
35+
//
36+
// CRLReason ::= ENUMERATED {
37+
// unspecified (0),
38+
// keyCompromise (1),
39+
// cACompromise (2),
40+
// affiliationChanged (3),
41+
// superseded (4),
42+
// cessationOfOperation (5),
43+
// certificateHold (6),
44+
// -- value 7 is not used
45+
// removeFromCRL (8),
46+
// privilegeWithdrawn (9),
47+
// aACompromise (10) }
3448
const (
35-
// RFC 5280, 5.3.1
36-
// CRLReason ::= ENUMERATED {
37-
// unspecified (0),
38-
// keyCompromise (1),
39-
// cACompromise (2),
40-
// affiliationChanged (3),
41-
// superseded (4),
42-
// cessationOfOperation (5),
43-
// certificateHold (6),
44-
// -- value 7 is not used
45-
// removeFromCRL (8),
46-
// privilegeWithdrawn (9),
47-
// aACompromise (10) }
49+
// certificateHold
4850
reasonCodeCertificateHold = 6
49-
reasonCodeRemoveFromCRL = 8
51+
52+
// removeFromCRL
53+
reasonCodeRemoveFromCRL = 8
5054
)
5155

5256
var (
@@ -197,9 +201,7 @@ func Supported(cert *x509.Certificate) bool {
197201
}
198202

199203
func hasFreshestCRL(extensions []pkix.Extension) bool {
200-
return slices.ContainsFunc(extensions, func(ext pkix.Extension) bool {
201-
return ext.Id.Equal(oidFreshestCRL)
202-
})
204+
return x509util.FindExtensionByOID(oidFreshestCRL, extensions) != nil
203205
}
204206

205207
func validate(bundle *crl.Bundle, issuer *x509.Certificate) error {
@@ -224,14 +226,12 @@ func validate(bundle *crl.Bundle, issuer *x509.Certificate) error {
224226
}
225227

226228
// check delta CRL indicator extension
227-
idx := slices.IndexFunc(deltaCRL.Extensions, func(ext pkix.Extension) bool {
228-
return ext.Id.Equal(oidDeltaCRLIndicator)
229-
})
230-
if idx < 0 {
229+
extension := x509util.FindExtensionByOID(oidDeltaCRLIndicator, deltaCRL.Extensions)
230+
if extension == nil {
231231
return errors.New("delta CRL indicator extension is not found")
232232
}
233233
minimumBaseCRLNumber := new(big.Int)
234-
value := cryptobyte.String(deltaCRL.Extensions[idx].Value)
234+
value := cryptobyte.String(extension.Value)
235235
if !value.ReadASN1Integer(minimumBaseCRLNumber) {
236236
return errors.New("failed to parse delta CRL indicator extension")
237237
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright The Notary Project Authors.
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package x509util
15+
16+
import (
17+
"crypto/x509/pkix"
18+
"encoding/asn1"
19+
"slices"
20+
)
21+
22+
// FindExtensionByOID finds the extension by the given OID.
23+
func FindExtensionByOID(oid asn1.ObjectIdentifier, extensions []pkix.Extension) *pkix.Extension {
24+
idx := slices.IndexFunc(extensions, func(ext pkix.Extension) bool {
25+
return ext.Id.Equal(oid)
26+
})
27+
if idx < 0 {
28+
return nil
29+
}
30+
return &extensions[idx]
31+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package x509util
2+
3+
import (
4+
"crypto/x509/pkix"
5+
"encoding/asn1"
6+
"testing"
7+
)
8+
9+
func TestFindExtensionByOID(t *testing.T) {
10+
oid1 := asn1.ObjectIdentifier{1, 2, 3, 4}
11+
oid2 := asn1.ObjectIdentifier{1, 2, 3, 5}
12+
extensions := []pkix.Extension{
13+
{Id: oid1, Value: []byte("value1")},
14+
{Id: oid2, Value: []byte("value2")},
15+
}
16+
17+
tests := []struct {
18+
name string
19+
oid asn1.ObjectIdentifier
20+
extensions []pkix.Extension
21+
expected *pkix.Extension
22+
}{
23+
{
24+
name: "Extension found",
25+
oid: oid1,
26+
extensions: extensions,
27+
expected: &extensions[0],
28+
},
29+
{
30+
name: "Extension not found",
31+
oid: asn1.ObjectIdentifier{1, 2, 3, 6},
32+
extensions: extensions,
33+
expected: nil,
34+
},
35+
}
36+
37+
for _, tt := range tests {
38+
t.Run(tt.name, func(t *testing.T) {
39+
result := FindExtensionByOID(tt.oid, tt.extensions)
40+
if result != tt.expected {
41+
t.Errorf("expected %v, got %v", tt.expected, result)
42+
}
43+
})
44+
}
45+
}

revocation/internal/x509util/validate.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// limitations under the License.
1313

1414
// Package x509util provides the method to validate the certificate chain for a
15-
// specific purpose, including code signing and timestamping.
15+
// specific purpose, including code signing and timestamping. It also provides
16+
// the method to find the extension by the given OID.
1617
package x509util
1718

1819
import (

0 commit comments

Comments
 (0)