@@ -87,31 +87,52 @@ type PostHandshakeVerificationFunc func(params *HandshakeVerificationInfo) (*Pos
87
87
// Deprecated: use PostHandshakeVerificationFunc instead.
88
88
type CustomVerificationFunc = PostHandshakeVerificationFunc
89
89
90
- // GetRootCAsParams contains the parameters available to users when
91
- // implementing GetRootCAs.
92
- type GetRootCAsParams struct {
93
- RawConn net.Conn
90
+ // ConnectionInfo contains the parameters available to users when
91
+ // implementing GetRootCertificates.
92
+ type ConnectionInfo struct {
93
+ // RawConn is the raw net.Conn representing a connection.
94
+ RawConn net.Conn
95
+ // RawCerts is the byte representation of the presented peer cert chain.
94
96
RawCerts [][]byte
95
97
}
96
98
97
- // GetRootCAsResults contains the results of GetRootCAs.
99
+ // GetRootCAsParams contains the parameters available to users when
100
+ // implementing GetRootCAs.
101
+ //
102
+ // Deprecated: use ConnectionInfo instead.
103
+ type GetRootCAsParams = ConnectionInfo
104
+
105
+ // RootCertificates is the result of GetRootCertificates.
98
106
// If users want to reload the root trust certificate, it is required to return
99
107
// the proper TrustCerts in GetRootCAs.
100
- type GetRootCAsResults struct {
108
+ type RootCertificates struct {
109
+ // TrustCerts is the pool of trusted certificates.
101
110
TrustCerts * x509.CertPool
102
111
}
103
112
113
+ // GetRootCAsResults contains the results of GetRootCAs.
114
+ // If users want to reload the root trust certificate, it is required to return
115
+ // the proper TrustCerts in GetRootCAs.
116
+ //
117
+ // Deprecated: use RootCertificates instead.
118
+ type GetRootCAsResults = RootCertificates
119
+
104
120
// RootCertificateOptions contains options to obtain root trust certificates
105
121
// for both the client and the server.
106
122
// At most one option could be set. If none of them are set, we
107
123
// use the system default trust certificates.
108
124
type RootCertificateOptions struct {
125
+ // If RootCertificates is set, it will be used every time when verifying
126
+ // the peer certificates, without performing root certificate reloading.
127
+ RootCertificates * x509.CertPool
109
128
// If RootCACerts is set, it will be used every time when verifying
110
129
// the peer certificates, without performing root certificate reloading.
130
+ //
131
+ // Deprecated: use RootCertificates instead.
111
132
RootCACerts * x509.CertPool
112
133
// If GetRootCertificates is set, it will be invoked to obtain root certs for
113
134
// every new connection.
114
- GetRootCertificates func (params * GetRootCAsParams ) (* GetRootCAsResults , error )
135
+ GetRootCertificates func (params * ConnectionInfo ) (* RootCertificates , error )
115
136
// If RootProvider is set, we will use the root certs from the Provider's
116
137
// KeyMaterial() call in the new connections. The Provider must have initial
117
138
// credentials if specified. Otherwise, KeyMaterial() will block forever.
@@ -277,6 +298,12 @@ func (o *Options) clientConfig() (*tls.Config, error) {
277
298
if o .MaxTLSVersion == 0 {
278
299
o .MaxTLSVersion = o .MaxVersion
279
300
}
301
+ // TODO(gtcooke94) RootCACerts is deprecated, eventually remove this block.
302
+ // This will ensure that users still explicitly setting RootCACerts will get
303
+ // the setting int the right place.
304
+ if o .RootOptions .RootCACerts != nil {
305
+ o .RootOptions .RootCertificates = o .RootOptions .RootCACerts
306
+ }
280
307
if o .VerificationType == SkipVerification && o .AdditionalPeerVerification == nil {
281
308
return nil , fmt .Errorf ("client needs to provide custom verification mechanism if choose to skip default verification" )
282
309
}
@@ -312,19 +339,19 @@ func (o *Options) clientConfig() (*tls.Config, error) {
312
339
}
313
340
// Propagate root-certificate-related fields in tls.Config.
314
341
switch {
315
- case o .RootOptions .RootCACerts != nil :
316
- config .RootCAs = o .RootOptions .RootCACerts
342
+ case o .RootOptions .RootCertificates != nil :
343
+ config .RootCAs = o .RootOptions .RootCertificates
317
344
case o .RootOptions .GetRootCertificates != nil :
318
345
// In cases when users provide GetRootCertificates callback, since this
319
346
// callback is not contained in tls.Config, we have nothing to set here.
320
347
// We will invoke the callback in ClientHandshake.
321
348
case o .RootOptions .RootProvider != nil :
322
- o .RootOptions .GetRootCertificates = func (* GetRootCAsParams ) (* GetRootCAsResults , error ) {
349
+ o .RootOptions .GetRootCertificates = func (* ConnectionInfo ) (* RootCertificates , error ) {
323
350
km , err := o .RootOptions .RootProvider .KeyMaterial (context .Background ())
324
351
if err != nil {
325
352
return nil , err
326
353
}
327
- return & GetRootCAsResults {TrustCerts : km .Roots }, nil
354
+ return & RootCertificates {TrustCerts : km .Roots }, nil
328
355
}
329
356
default :
330
357
// No root certificate options specified by user. Use the certificates
@@ -381,6 +408,12 @@ func (o *Options) serverConfig() (*tls.Config, error) {
381
408
if o .MaxTLSVersion == 0 {
382
409
o .MaxTLSVersion = o .MaxVersion
383
410
}
411
+ // TODO(gtcooke94) RootCACerts is deprecated, eventually remove this block.
412
+ // This will ensure that users still explicitly setting RootCACerts will get
413
+ // the setting int the right place.
414
+ if o .RootOptions .RootCACerts != nil {
415
+ o .RootOptions .RootCertificates = o .RootOptions .RootCACerts
416
+ }
384
417
if o .RequireClientCert && o .VerificationType == SkipVerification && o .AdditionalPeerVerification == nil {
385
418
return nil , fmt .Errorf ("server needs to provide custom verification mechanism if choose to skip default verification, but require client certificate(s)" )
386
419
}
@@ -420,19 +453,19 @@ func (o *Options) serverConfig() (*tls.Config, error) {
420
453
}
421
454
// Propagate root-certificate-related fields in tls.Config.
422
455
switch {
423
- case o .RootOptions .RootCACerts != nil :
424
- config .ClientCAs = o .RootOptions .RootCACerts
456
+ case o .RootOptions .RootCertificates != nil :
457
+ config .ClientCAs = o .RootOptions .RootCertificates
425
458
case o .RootOptions .GetRootCertificates != nil :
426
459
// In cases when users provide GetRootCertificates callback, since this
427
460
// callback is not contained in tls.Config, we have nothing to set here.
428
461
// We will invoke the callback in ServerHandshake.
429
462
case o .RootOptions .RootProvider != nil :
430
- o .RootOptions .GetRootCertificates = func (* GetRootCAsParams ) (* GetRootCAsResults , error ) {
463
+ o .RootOptions .GetRootCertificates = func (* ConnectionInfo ) (* RootCertificates , error ) {
431
464
km , err := o .RootOptions .RootProvider .KeyMaterial (context .Background ())
432
465
if err != nil {
433
466
return nil , err
434
467
}
435
- return & GetRootCAsResults {TrustCerts : km .Roots }, nil
468
+ return & RootCertificates {TrustCerts : km .Roots }, nil
436
469
}
437
470
default :
438
471
// No root certificate options specified by user. Use the certificates
@@ -477,12 +510,12 @@ func (o *Options) serverConfig() (*tls.Config, error) {
477
510
// advancedTLSCreds is the credentials required for authenticating a connection
478
511
// using TLS.
479
512
type advancedTLSCreds struct {
480
- config * tls.Config
481
- verifyFunc PostHandshakeVerificationFunc
482
- getRootCAs func (params * GetRootCAsParams ) (* GetRootCAsResults , error )
483
- isClient bool
484
- revocationOptions * RevocationOptions
485
- verificationType VerificationType
513
+ config * tls.Config
514
+ verifyFunc PostHandshakeVerificationFunc
515
+ getRootCertificates func (params * ConnectionInfo ) (* RootCertificates , error )
516
+ isClient bool
517
+ revocationOptions * RevocationOptions
518
+ verificationType VerificationType
486
519
}
487
520
488
521
func (c advancedTLSCreds ) Info () credentials.ProtocolInfo {
@@ -548,10 +581,10 @@ func (c *advancedTLSCreds) ServerHandshake(rawConn net.Conn) (net.Conn, credenti
548
581
549
582
func (c * advancedTLSCreds ) Clone () credentials.TransportCredentials {
550
583
return & advancedTLSCreds {
551
- config : credinternal .CloneTLSConfig (c .config ),
552
- verifyFunc : c .verifyFunc ,
553
- getRootCAs : c .getRootCAs ,
554
- isClient : c .isClient ,
584
+ config : credinternal .CloneTLSConfig (c .config ),
585
+ verifyFunc : c .verifyFunc ,
586
+ getRootCertificates : c .getRootCertificates ,
587
+ isClient : c .isClient ,
555
588
}
556
589
}
557
590
@@ -588,8 +621,8 @@ func buildVerifyFunc(c *advancedTLSCreds,
588
621
rootCAs = c .config .ClientCAs
589
622
}
590
623
// Reload root CA certs.
591
- if rootCAs == nil && c .getRootCAs != nil {
592
- results , err := c .getRootCAs ( & GetRootCAsParams {
624
+ if rootCAs == nil && c .getRootCertificates != nil {
625
+ results , err := c .getRootCertificates ( & ConnectionInfo {
593
626
RawConn : rawConn ,
594
627
RawCerts : rawCerts ,
595
628
})
@@ -661,12 +694,12 @@ func NewClientCreds(o *Options) (credentials.TransportCredentials, error) {
661
694
return nil , err
662
695
}
663
696
tc := & advancedTLSCreds {
664
- config : conf ,
665
- isClient : true ,
666
- getRootCAs : o .RootOptions .GetRootCertificates ,
667
- verifyFunc : o .AdditionalPeerVerification ,
668
- revocationOptions : o .RevocationOptions ,
669
- verificationType : o .VerificationType ,
697
+ config : conf ,
698
+ isClient : true ,
699
+ getRootCertificates : o .RootOptions .GetRootCertificates ,
700
+ verifyFunc : o .AdditionalPeerVerification ,
701
+ revocationOptions : o .RevocationOptions ,
702
+ verificationType : o .VerificationType ,
670
703
}
671
704
tc .config .NextProtos = credinternal .AppendH2ToNextProtos (tc .config .NextProtos )
672
705
return tc , nil
@@ -680,12 +713,12 @@ func NewServerCreds(o *Options) (credentials.TransportCredentials, error) {
680
713
return nil , err
681
714
}
682
715
tc := & advancedTLSCreds {
683
- config : conf ,
684
- isClient : false ,
685
- getRootCAs : o .RootOptions .GetRootCertificates ,
686
- verifyFunc : o .AdditionalPeerVerification ,
687
- revocationOptions : o .RevocationOptions ,
688
- verificationType : o .VerificationType ,
716
+ config : conf ,
717
+ isClient : false ,
718
+ getRootCertificates : o .RootOptions .GetRootCertificates ,
719
+ verifyFunc : o .AdditionalPeerVerification ,
720
+ revocationOptions : o .RevocationOptions ,
721
+ verificationType : o .VerificationType ,
689
722
}
690
723
tc .config .NextProtos = credinternal .AppendH2ToNextProtos (tc .config .NextProtos )
691
724
return tc , nil
0 commit comments