forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbadCertListener.js
executable file
·343 lines (277 loc) · 11.6 KB
/
badCertListener.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
/*
Copyright 2007-2009 WebDriver committers
Copyright 2007-2009 Google Inc.
Portions copyright 2011 Software Freedom Conservancy
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
* @fileoverview Contains a Javascript implementation for
* a custom nsICertOverrideService. This class will forward requests to the
* original certificate override service - unless it was told to accept all
* of them.
*/
goog.provide('WdCertOverrideService');
goog.require('bot.userAgent');
goog.require('fxdriver.logging');
goog.require('fxdriver.moz');
function getPreferenceFromProfile(prefName, prefDefaultValue) {
var prefs =
CC['@mozilla.org/preferences-service;1'].getService(CI['nsIPrefBranch']);
if (!prefs.prefHasUserValue(prefName)) {
fxdriver.logging.info(prefName + ' not set; defaulting to ' + prefDefaultValue);
return prefDefaultValue;
}
var prefValue = prefs.getBoolPref(prefName);
fxdriver.logging.info('Found preference for ' + prefName + ': ' + prefValue);
return prefValue;
}
function shouldAcceptUntrustedCerts() {
// Defaults to true - accepting untrusted certificates.
// This puts the module into effect - setting it to false
// will delegate all calls to the original service.
return getPreferenceFromProfile('webdriver_accept_untrusted_certs', true);
}
function shouldAssumeUntrustedIssuer() {
return getPreferenceFromProfile('webdriver_assume_untrusted_issuer', true);
}
WdCertOverrideService = function() {
// If untrusted issuer is set to false by the user,
// the initial bitmask will not include ERROR_UNTRUSTED.
//
// See the javadoc for FirefoxProfile and documentation
// for fillNeededBits for further explanation.
var untrusted_issuer = shouldAssumeUntrustedIssuer();
if (untrusted_issuer) {
this.default_bits = this.ERROR_UNTRUSTED;
} else {
this.default_bits = 0;
}
fxdriver.logging.info('Accept untrusted certificates: ' + shouldAcceptUntrustedCerts());
// UUID of the original implementor of this service.
var ORIGINAL_OVERRIDE_SERVICE_ID = '{67ba681d-5485-4fff-952c-2ee337ffdcd6}';
// Keep a reference to the original bad certificate listener.
var originalService = Components.classesByID[ORIGINAL_OVERRIDE_SERVICE_ID].
getService();
this.origListener_ =
originalService.QueryInterface(
CI['nsICertOverrideService']);
};
// Constants needed since WdCertOverrideService implements
// nsICertOverrideService
WdCertOverrideService.prototype = {
ERROR_UNTRUSTED: 1,
ERROR_MISMATCH: 2,
ERROR_TIME: 4
};
// Returns the bit needed to mask if the certificate has expired, 0 otherwise.
WdCertOverrideService.prototype.certificateExpiredBit_ =
function(theCert, verification_result) {
if ((verification_result & theCert.CERT_EXPIRED) != 0) {
fxdriver.logging.info('Certificate expired.');
return this.ERROR_TIME;
}
return 0;
};
// Returns the bit needed to mask untrusted issuers, 0 otherwise.
// Note that this bit is already set by default in default_bits
WdCertOverrideService.prototype.certificateIssuerUntrusted_ =
function(theCert, verification_result) {
if (((verification_result & theCert.ISSUER_UNKNOWN) != 0) ||
((verification_result & theCert.ISSUER_NOT_TRUSTED) != 0) ||
((verification_result & theCert.CERT_NOT_TRUSTED) != 0) ||
((verification_result & theCert.INVALID_CA) != 0)) {
fxdriver.logging.info('Certificate issuer unknown.');
fxdriver.logging.info('Unknown: ' + (theCert.ISSUER_UNKNOWN & verification_result));
fxdriver.logging.info('Issuer not trusted: ' + (theCert.ISSUER_NOT_TRUSTED & verification_result));
fxdriver.logging.info('Cert not trusted: ' + (theCert.CERT_NOT_TRUSTED & verification_result));
fxdriver.logging.info('Invalid CA: ' + (theCert.INVALID_CA & verification_result));
return this.ERROR_UNTRUSTED;
}
return 0;
};
// Returns the bit needed to mask mismatch between actual hostname
// and the hostname the certificate was issued for, 0 otherwise.
WdCertOverrideService.prototype.certificateHostnameMismatch_ =
function(theCert, aHost) {
var commonNameRE = new RegExp('^' + theCert.commonName.replace('*', '[\\w|\-]+') + '$', 'i');
if (aHost.match(commonNameRE) === null) {
fxdriver.logging.info('Host name mismatch: cert: ' + theCert.commonName + ' get: ' + aHost);
return this.ERROR_MISMATCH;
}
return 0;
};
// Given a certificate and the host it was received for, fill in the bits
// needed to accept this certificate for this host, even though the
// certificate is invalid.
//
// Note that the bitmask has to be accurate: At the moment, Firefox expects
// the returned bitmask to match *exactly* to the errors the certificate
// caused. If extra bits will be set, the untrusted certificate screen
// will appear.
WdCertOverrideService.prototype.fillNeededBits = function(aCert, aHost) {
var verification_bits = aCert.verifyForUsage(aCert.CERT_USAGE_SSLClient);
var return_bits = this.default_bits;
fxdriver.logging.info('Certificate verification results: ' + verification_bits);
return_bits = return_bits | this.certificateExpiredBit_(
aCert, verification_bits);
return_bits = return_bits | this.certificateHostnameMismatch_(aCert, aHost);
// Return bits will be 0 here only if:
// 1. Both checks above returned 0.
// 2. shouldAssumeUntrustedIssuer is false (otherwise this.default_bits = 1)
// It has been observed that if there's a host name mismatch then it
// may not be required to check the trust status of the certificate issuer.
if (return_bits == 0) {
fxdriver.logging.info('Checking issuer since certificate has not expired or has a host name mismatch.');
return_bits = return_bits | this.certificateIssuerUntrusted_(
aCert, verification_bits);
}
fxdriver.logging.info('return_bits now: ' + return_bits);
return return_bits;
};
// Interface functions from now on.
WdCertOverrideService.prototype.hasMatchingOverride = function(
aHostName, aPort, aCert, aOverrideBits, aIsTemporary) {
var retval = false;
var acceptAll = shouldAcceptUntrustedCerts();
if (acceptAll === true) {
fxdriver.logging.info('Allowing certificate from site: ' + aHostName + ':' + aPort);
retval = true;
aIsTemporary.value = false;
if (bot.userAgent.isProductVersion(19)) {
fxdriver.logging.info('Setting all Override Bits');
aOverrideBits.value = this.ERROR_UNTRUSTED | this.ERROR_MISMATCH | this.ERROR_TIME;
} else {
aOverrideBits.value = this.fillNeededBits(aCert, aHostName);
}
fxdriver.logging.info('Override Bits: ' + aOverrideBits.value);
} else {
retval = this.origListener_.hasMatchingOverride(aHostName, aPort,
aCert, aOverrideBits, aIsTemporary);
}
return retval;
};
// Delegate the rest of the functions - they are not interesting as they are not
// called during validation of invalid certificate normally.
WdCertOverrideService.prototype.clearValidityOverride = function(aHostName,
aPort) {
this.origListener_.clearValidityOverride(aHostName, aPort);
};
WdCertOverrideService.prototype.getAllOverrideHostsWithPorts = function(
aCount, aHostsWithPortsArray) {
this.origListener_.getAllOverrideHostsWithPorts(aCount, aHostsWithPortsArray);
};
WdCertOverrideService.prototype.getValidityOverride = function(
aHostName, aPort, aHashAlg, aFingerprint, aOverrideBits, aIsTemporary) {
return this.origListener_.getValidityOverride(
aHostName, aPort, aHashAlg, aFingerprint, aOverrideBits, aIsTemporary);
};
WdCertOverrideService.prototype.isCertUsedForOverrides = function(
aCert, aCheckTemporaries, aCheckPermanents) {
return this.origListener_.isCertUsedForOverrides(
aCert, aCheckTemporaries, aCheckPermanents);
};
WdCertOverrideService.prototype.rememberValidityOverride = function(
aHostName, aPort, aCert, aOverrideBits, aTemporary) {
this.origListener_.rememberValidityOverride(
aHostName, aPort, aCert, aOverrideBits, aTemporary);
};
WdCertOverrideService.prototype.QueryInterface = function(aIID) {
if (aIID.equals(CI['nsICertOverrideService']) ||
aIID.equals(CI['nsIInterfaceRequestor']) ||
aIID.equals(CI['nsISupports'])) {
return this;
}
throw CR['NS_ERROR_NO_INTERFACE'];
};
// Service contract ID which we override
/** @const */ var CERTOVERRIDE_CONTRACT_ID = '@mozilla.org/security/certoverride;1';
// UUID for this instance specifically.
/** @const */ var DUMMY_CERTOVERRIDE_SERVICE_CLASS_ID =
Components.ID('{c8fffaba-9b7a-41aa-872d-7e7366c16715}');
var service = undefined;
var WDCertOverrideFactory = {
createInstance: function(aOuter, aIID) {
if (aOuter != null)
throw CR['NS_ERROR_NO_AGGREGATION'];
if (service == undefined) {
service = new WdCertOverrideService();
}
return service;
}
};
function WDBadCertListenerModule() {
this.firstTime_ = true;
}
WDBadCertListenerModule.prototype.registerSelf = function(
aCompMgr, aFileSpec, aLocation, aType) {
if (this.firstTime_) {
this.firstTime_ = false;
throw CR['NS_ERROR_FACTORY_REGISTER_AGAIN'];
}
fxdriver.logging.info('Registering Override Certificate service.');
aCompMgr = aCompMgr.QueryInterface(
CI['nsIComponentRegistrar']);
aCompMgr.registerFactoryLocation(
DUMMY_CERTOVERRIDE_SERVICE_CLASS_ID, 'badCertListener',
CERTOVERRIDE_CONTRACT_ID, aFileSpec, aLocation, aType);
};
WDBadCertListenerModule.prototype.unregisterSelf = function(
aCompMgr, aLocation, aType) {
fxdriver.logging.info('Un-registering Override Certificate service.');
aCompMgr =
aCompMgr.QueryInterface(CI['nsIComponentRegistrar']);
aCompMgr.unregisterFactoryLocation(DUMMY_CERTOVERRIDE_SERVICE_CLASS_ID, aLocation);
};
var FACTORY;
if (!bot.userAgent.isProductVersion('10')) {
/** @const */ FACTORY = {
createInstance: function(aOuter, aIID) {
if (aOuter != null)
throw CR['NS_ERROR_NO_AGGREGATION'];
if (service != undefined) {
return service;
}
var raw = new WdCertOverrideService();
var mainThread = CC['@mozilla.org/thread-manager;1'].getService().mainThread;
var proxyManager = CC['@mozilla.org/xpcomproxy;1']
.getService(CI.nsIProxyObjectManager);
// 5 == NS_PROXY_ALWAYS | NS_PROXY_SYNC
service = proxyManager.getProxyForObject(mainThread,
CI.nsICertOverrideService, raw, 5);
return service;
}
};
WdCertOverrideService.prototype._xpcom_factory = FACTORY;
}
WDBadCertListenerModule.prototype.getClassObject = function(
aCompMgr, aCID, aIID) {
if (!aIID.equals(CI['nsIFactory']))
throw CR['NS_ERROR_NOT_IMPLEMENTED'];
if (aCID.equals(DUMMY_CERTOVERRIDE_SERVICE_CLASS_ID)) {
if (bot.userAgent.isProductVersion('10')) {
return WDCertOverrideFactory;
} else {
return FACTORY;
}
}
throw CR['NS_ERROR_NO_INTERFACE'];
};
WDBadCertListenerModule.prototype.canUnload = function(aCompMgr) {
return true;
};
NSGetModule = function(comMgr, fileSpec) {
return new WDBadCertListenerModule();
};
WdCertOverrideService.prototype.classID = DUMMY_CERTOVERRIDE_SERVICE_CLASS_ID;
fxdriver.moz.load('resource://gre/modules/XPCOMUtils.jsm');
if (XPCOMUtils.generateNSGetFactory) {
/** @const */ NSGetFactory = XPCOMUtils.generateNSGetFactory([WdCertOverrideService]);
}