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
|
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
// Need profile dir to store the key / cert
do_get_profile();
// Ensure PSM is initialized
Cc["@mozilla.org/psm;1"].getService(Ci.nsISupports);
const { MockRegistrar } = ChromeUtils.importESModule(
"resource://testing-common/MockRegistrar.sys.mjs"
);
const certOverrideService = Cc[
"@mozilla.org/security/certoverride;1"
].getService(Ci.nsICertOverrideService);
const socketTransportService = Cc[
"@mozilla.org/network/socket-transport-service;1"
].getService(Ci.nsISocketTransportService);
const prefs = Services.prefs;
function areCertsEqual(certA, certB) {
let derA = certA.getRawDER();
let derB = certB.getRawDER();
if (derA.length != derB.length) {
return false;
}
for (let i = 0; i < derA.length; i++) {
if (derA[i] != derB[i]) {
return false;
}
}
return true;
}
function startServer(
cert,
expectingPeerCert,
clientCertificateConfig,
expectedVersion,
expectedVersionStr
) {
let tlsServer = Cc["@mozilla.org/network/tls-server-socket;1"].createInstance(
Ci.nsITLSServerSocket
);
tlsServer.init(-1, true, -1);
tlsServer.serverCert = cert;
let input, output;
let listener = {
onSocketAccepted(socket, transport) {
info("Accept TLS client connection");
let connectionInfo = transport.securityCallbacks.getInterface(
Ci.nsITLSServerConnectionInfo
);
connectionInfo.setSecurityObserver(listener);
input = transport.openInputStream(0, 0, 0);
output = transport.openOutputStream(0, 0, 0);
},
onHandshakeDone(socket, status) {
info("TLS handshake done");
if (expectingPeerCert) {
ok(!!status.peerCert, "Has peer cert");
ok(
areCertsEqual(status.peerCert, cert),
"Peer cert matches expected cert"
);
} else {
ok(!status.peerCert, "No peer cert (as expected)");
}
equal(
status.tlsVersionUsed,
expectedVersion,
"Using " + expectedVersionStr
);
let expectedCipher;
if (expectedVersion >= 772) {
expectedCipher = "TLS_AES_128_GCM_SHA256";
} else {
expectedCipher = "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256";
}
equal(status.cipherName, expectedCipher, "Using expected cipher");
equal(status.keyLength, 128, "Using 128-bit key");
equal(status.macLength, 128, "Using 128-bit MAC");
input.asyncWait(
{
onInputStreamReady(input1) {
NetUtil.asyncCopy(input1, output);
},
},
0,
0,
Services.tm.currentThread
);
},
onStopListening() {
info("onStopListening");
input.close();
output.close();
},
};
tlsServer.setSessionTickets(false);
tlsServer.setRequestClientCertificate(clientCertificateConfig);
tlsServer.asyncListen(listener);
return tlsServer;
}
function storeCertOverride(port, cert) {
certOverrideService.rememberValidityOverride(
"127.0.0.1",
port,
{},
cert,
true
);
}
function startClient(port, sendClientCert, expectingAlert, tlsVersion) {
gClientAuthDialogService.selectCertificate = sendClientCert;
let SSL_ERROR_BASE = Ci.nsINSSErrorsService.NSS_SSL_ERROR_BASE;
let SSL_ERROR_BAD_CERT_ALERT = SSL_ERROR_BASE + 17;
let SSL_ERROR_RX_CERTIFICATE_REQUIRED_ALERT = SSL_ERROR_BASE + 181;
let transport = socketTransportService.createTransport(
["ssl"],
"127.0.0.1",
port,
null,
null
);
let input;
let output;
let inputDeferred = Promise.withResolvers();
let outputDeferred = Promise.withResolvers();
let handler = {
onTransportStatus(transport1, status) {
if (status === Ci.nsISocketTransport.STATUS_CONNECTED_TO) {
output.asyncWait(handler, 0, 0, Services.tm.currentThread);
}
},
onInputStreamReady(input1) {
try {
let data = NetUtil.readInputStreamToString(input1, input1.available());
equal(data, "HELLO", "Echoed data received");
input1.close();
output.close();
ok(!expectingAlert, "No cert alert expected");
inputDeferred.resolve();
} catch (e) {
let errorCode = -1 * (e.result & 0xffff);
if (expectingAlert) {
if (
tlsVersion == Ci.nsITLSClientStatus.TLS_VERSION_1_2 &&
errorCode == SSL_ERROR_BAD_CERT_ALERT
) {
info("Got bad cert alert as expected for tls 1.2");
input1.close();
output.close();
inputDeferred.resolve();
return;
}
if (
tlsVersion == Ci.nsITLSClientStatus.TLS_VERSION_1_3 &&
errorCode == SSL_ERROR_RX_CERTIFICATE_REQUIRED_ALERT
) {
info("Got cert required alert as expected for tls 1.3");
input1.close();
output.close();
inputDeferred.resolve();
return;
}
}
inputDeferred.reject(e);
}
},
onOutputStreamReady(output1) {
try {
output1.write("HELLO", 5);
info("Output to server written");
outputDeferred.resolve();
input = transport.openInputStream(0, 0, 0);
input.asyncWait(handler, 0, 0, Services.tm.currentThread);
} catch (e) {
let errorCode = -1 * (e.result & 0xffff);
if (errorCode == SSL_ERROR_BAD_CERT_ALERT) {
info("Server doesn't like client cert");
}
outputDeferred.reject(e);
}
},
};
transport.setEventSink(handler, Services.tm.currentThread);
output = transport.openOutputStream(0, 0, 0);
return Promise.all([inputDeferred.promise, outputDeferred.promise]);
}
// Replace the UI dialog that prompts the user to pick a client certificate.
const gClientAuthDialogService = {
_selectCertificate: false,
set selectCertificate(value) {
this._selectCertificate = value;
},
chooseCertificate(hostname, certArray, loadContext, callback) {
if (this._selectCertificate) {
callback.certificateChosen(certArray[0], false);
} else {
callback.certificateChose(null, false);
}
},
QueryInterface: ChromeUtils.generateQI([Ci.nsIClientAuthDialogService]),
};
const ClientAuthDialogServiceContractID =
"@mozilla.org/security/ClientAuthDialogService;1";
MockRegistrar.register(
ClientAuthDialogServiceContractID,
gClientAuthDialogService
);
const tests = [
{
expectingPeerCert: true,
clientCertificateConfig: Ci.nsITLSServerSocket.REQUIRE_ALWAYS,
sendClientCert: true,
expectingAlert: false,
},
{
expectingPeerCert: true,
clientCertificateConfig: Ci.nsITLSServerSocket.REQUIRE_ALWAYS,
sendClientCert: false,
expectingAlert: true,
},
{
expectingPeerCert: true,
clientCertificateConfig: Ci.nsITLSServerSocket.REQUEST_ALWAYS,
sendClientCert: true,
expectingAlert: false,
},
{
expectingPeerCert: false,
clientCertificateConfig: Ci.nsITLSServerSocket.REQUEST_ALWAYS,
sendClientCert: false,
expectingAlert: false,
},
{
expectingPeerCert: false,
clientCertificateConfig: Ci.nsITLSServerSocket.REQUEST_NEVER,
sendClientCert: true,
expectingAlert: false,
},
{
expectingPeerCert: false,
clientCertificateConfig: Ci.nsITLSServerSocket.REQUEST_NEVER,
sendClientCert: false,
expectingAlert: false,
},
];
const versions = [
{
prefValue: 3,
version: Ci.nsITLSClientStatus.TLS_VERSION_1_2,
versionStr: "TLS 1.2",
},
{
prefValue: 4,
version: Ci.nsITLSClientStatus.TLS_VERSION_1_3,
versionStr: "TLS 1.3",
},
];
add_task(async function () {
let cert = getTestServerCertificate();
ok(!!cert, "Got self-signed cert");
for (let v of versions) {
prefs.setIntPref("security.tls.version.max", v.prefValue);
for (let t of tests) {
let server = startServer(
cert,
t.expectingPeerCert,
t.clientCertificateConfig,
v.version,
v.versionStr
);
storeCertOverride(server.port, cert);
await startClient(
server.port,
t.sendClientCert,
t.expectingAlert,
v.version
);
server.close();
}
}
});
registerCleanupFunction(function () {
prefs.clearUserPref("security.tls.version.max");
});
|