@@ -40,28 +40,34 @@ func New(opts x509.VerifyOptions, user UserConversion) *Authenticator {
40
40
41
41
// AuthenticateRequest authenticates the request using presented client certificates
42
42
func (a * Authenticator ) AuthenticateRequest (req * http.Request ) (user.Info , bool , error ) {
43
- if req .TLS == nil {
43
+ if req .TLS == nil || len ( req . TLS . PeerCertificates ) == 0 {
44
44
return nil , false , nil
45
45
}
46
46
47
+ // Use intermediates, if provided
48
+ optsCopy := a .opts
49
+ if optsCopy .Intermediates == nil && len (req .TLS .PeerCertificates ) > 1 {
50
+ optsCopy .Intermediates = x509 .NewCertPool ()
51
+ for _ , intermediate := range req .TLS .PeerCertificates [1 :] {
52
+ optsCopy .Intermediates .AddCert (intermediate )
53
+ }
54
+ }
55
+
56
+ chains , err := req .TLS .PeerCertificates [0 ].Verify (optsCopy )
57
+ if err != nil {
58
+ return nil , false , err
59
+ }
60
+
47
61
var errlist []error
48
- for _ , cert := range req . TLS . PeerCertificates {
49
- chains , err := cert . Verify ( a . opts )
62
+ for _ , chain := range chains {
63
+ user , ok , err := a . user . User ( chain )
50
64
if err != nil {
51
65
errlist = append (errlist , err )
52
66
continue
53
67
}
54
68
55
- for _ , chain := range chains {
56
- user , ok , err := a .user .User (chain )
57
- if err != nil {
58
- errlist = append (errlist , err )
59
- continue
60
- }
61
-
62
- if ok {
63
- return user , ok , err
64
- }
69
+ if ok {
70
+ return user , ok , err
65
71
}
66
72
}
67
73
return nil , false , kerrors .NewAggregate (errlist )
@@ -81,25 +87,28 @@ func NewVerifier(opts x509.VerifyOptions, auth authenticator.Request, allowedCom
81
87
return & Verifier {opts , auth , allowedCommonNames }
82
88
}
83
89
84
- // AuthenticateRequest verifies the presented client certificates , then delegates to the wrapped auth
90
+ // AuthenticateRequest verifies the presented client certificate , then delegates to the wrapped auth
85
91
func (a * Verifier ) AuthenticateRequest (req * http.Request ) (user.Info , bool , error ) {
86
- if req .TLS == nil {
92
+ if req .TLS == nil || len ( req . TLS . PeerCertificates ) == 0 {
87
93
return nil , false , nil
88
94
}
89
95
90
- var errlist []error
91
- for _ , cert := range req .TLS .PeerCertificates {
92
- if _ , err := cert .Verify (a .opts ); err != nil {
93
- errlist = append (errlist , err )
94
- continue
95
- }
96
- if err := a .verifySubject (cert .Subject ); err != nil {
97
- errlist = append (errlist , err )
98
- continue
96
+ // Use intermediates, if provided
97
+ optsCopy := a .opts
98
+ if optsCopy .Intermediates == nil && len (req .TLS .PeerCertificates ) > 1 {
99
+ optsCopy .Intermediates = x509 .NewCertPool ()
100
+ for _ , intermediate := range req .TLS .PeerCertificates [1 :] {
101
+ optsCopy .Intermediates .AddCert (intermediate )
99
102
}
100
- return a .auth .AuthenticateRequest (req )
101
103
}
102
- return nil , false , kerrors .NewAggregate (errlist )
104
+
105
+ if _ , err := req .TLS .PeerCertificates [0 ].Verify (optsCopy ); err != nil {
106
+ return nil , false , err
107
+ }
108
+ if err := a .verifySubject (req .TLS .PeerCertificates [0 ].Subject ); err != nil {
109
+ return nil , false , err
110
+ }
111
+ return a .auth .AuthenticateRequest (req )
103
112
}
104
113
105
114
func (a * Verifier ) verifySubject (subject pkix.Name ) error {
0 commit comments