@@ -63,12 +63,36 @@ type Bundler struct {
63
63
RootPool * x509.CertPool
64
64
IntermediatePool * x509.CertPool
65
65
KnownIssuers map [string ]bool
66
+ opts options
67
+ }
68
+
69
+ type options struct {
70
+ keyUsages []x509.ExtKeyUsage
71
+ }
72
+
73
+ var defaultOptions = options {
74
+ keyUsages : []x509.ExtKeyUsage {
75
+ x509 .ExtKeyUsageServerAuth ,
76
+ x509 .ExtKeyUsageClientAuth ,
77
+ x509 .ExtKeyUsageMicrosoftServerGatedCrypto ,
78
+ x509 .ExtKeyUsageNetscapeServerGatedCrypto ,
79
+ },
80
+ }
81
+
82
+ // An Option sets options such as allowed key usages, etc.
83
+ type Option func (* options )
84
+
85
+ // WithKeyUsages lets you set which Extended Key Usage values are acceptable.
86
+ func WithKeyUsages (usages ... x509.ExtKeyUsage ) Option {
87
+ return func (o * options ) {
88
+ o .keyUsages = usages
89
+ }
66
90
}
67
91
68
92
// NewBundler creates a new Bundler from the files passed in; these
69
93
// files should contain a list of valid root certificates and a list
70
94
// of valid intermediate certificates, respectively.
71
- func NewBundler (caBundleFile , intBundleFile string ) (* Bundler , error ) {
95
+ func NewBundler (caBundleFile , intBundleFile string , opt ... Option ) (* Bundler , error ) {
72
96
var caBundle , intBundle []byte
73
97
var err error
74
98
@@ -103,14 +127,19 @@ func NewBundler(caBundleFile, intBundleFile string) (*Bundler, error) {
103
127
}
104
128
}
105
129
106
- return NewBundlerFromPEM (caBundle , intBundle )
130
+ return NewBundlerFromPEM (caBundle , intBundle , opt ... )
107
131
108
132
}
109
133
110
134
// NewBundlerFromPEM creates a new Bundler from PEM-encoded root certificates and
111
135
// intermediate certificates.
112
136
// If caBundlePEM is nil, the resulting Bundler can only do "Force" bundle.
113
- func NewBundlerFromPEM (caBundlePEM , intBundlePEM []byte ) (* Bundler , error ) {
137
+ func NewBundlerFromPEM (caBundlePEM , intBundlePEM []byte , opt ... Option ) (* Bundler , error ) {
138
+ opts := defaultOptions
139
+ for _ , o := range opt {
140
+ o (& opts )
141
+ }
142
+
114
143
log .Debug ("parsing root certificates from PEM" )
115
144
roots , err := helpers .ParseCertificatesPEM (caBundlePEM )
116
145
if err != nil {
@@ -128,6 +157,7 @@ func NewBundlerFromPEM(caBundlePEM, intBundlePEM []byte) (*Bundler, error) {
128
157
b := & Bundler {
129
158
KnownIssuers : map [string ]bool {},
130
159
IntermediatePool : x509 .NewCertPool (),
160
+ opts : opts ,
131
161
}
132
162
133
163
log .Debug ("building certificate pools" )
@@ -159,12 +189,7 @@ func (b *Bundler) VerifyOptions() x509.VerifyOptions {
159
189
return x509.VerifyOptions {
160
190
Roots : b .RootPool ,
161
191
Intermediates : b .IntermediatePool ,
162
- KeyUsages : []x509.ExtKeyUsage {
163
- x509 .ExtKeyUsageServerAuth ,
164
- x509 .ExtKeyUsageClientAuth ,
165
- x509 .ExtKeyUsageMicrosoftServerGatedCrypto ,
166
- x509 .ExtKeyUsageNetscapeServerGatedCrypto ,
167
- },
192
+ KeyUsages : b .opts .keyUsages ,
168
193
}
169
194
}
170
195
0 commit comments