The data contained in this repository can be downloaded to your computer using one of several clients.
Please see the documentation of your version control software client for more information.
Please select the desired protocol below to get the URL.
This URL has Read-Only access.
main_repo / test / simple / test-https-strict.js @ b0c0111b
History | View | Annotate | Download (5.97 KB)
1 |
// Copyright Joyent, Inc. and other Node contributors.
|
---|---|
2 |
//
|
3 |
// Permission is hereby granted, free of charge, to any person obtaining a
|
4 |
// copy of this software and associated documentation files (the
|
5 |
// "Software"), to deal in the Software without restriction, including
|
6 |
// without limitation the rights to use, copy, modify, merge, publish,
|
7 |
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
8 |
// persons to whom the Software is furnished to do so, subject to the
|
9 |
// following conditions:
|
10 |
//
|
11 |
// The above copyright notice and this permission notice shall be included
|
12 |
// in all copies or substantial portions of the Software.
|
13 |
//
|
14 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
15 |
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16 |
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
17 |
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
18 |
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
19 |
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
20 |
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
21 |
|
22 |
if (!process.versions.openssl) {
|
23 |
console.error('Skipping because node compiled without OpenSSL.');
|
24 |
process.exit(0);
|
25 |
} |
26 |
|
27 |
var common = require('../common'); |
28 |
var assert = require('assert'); |
29 |
|
30 |
var fs = require('fs'); |
31 |
var path = require('path'); |
32 |
var https = require('https'); |
33 |
|
34 |
function file(fname) { |
35 |
return path.resolve(common.fixturesDir, 'keys', fname); |
36 |
} |
37 |
|
38 |
function read(fname) { |
39 |
return fs.readFileSync(file(fname));
|
40 |
} |
41 |
|
42 |
// key1 is signed by ca1.
|
43 |
var key1 = read('agent1-key.pem'); |
44 |
var cert1 = read('agent1-cert.pem'); |
45 |
|
46 |
// key2 has a self signed cert
|
47 |
var key2 = read('agent2-key.pem'); |
48 |
var cert2 = read('agent2-cert.pem'); |
49 |
|
50 |
// key3 is signed by ca2.
|
51 |
var key3 = read('agent3-key.pem'); |
52 |
var cert3 = read('agent3-cert.pem'); |
53 |
|
54 |
var ca1 = read('ca1-cert.pem'); |
55 |
var ca2 = read('ca2-cert.pem'); |
56 |
|
57 |
// different agents to use different CA lists.
|
58 |
// this api is beyond bad.
|
59 |
var agent0 = new https.Agent(); |
60 |
var agent1 = new https.Agent({ ca: [ca1] }); |
61 |
var agent2 = new https.Agent({ ca: [ca2] }); |
62 |
var agent3 = new https.Agent({ ca: [ca1, ca2] }); |
63 |
|
64 |
var options1 = {
|
65 |
key: key1,
|
66 |
cert: cert1
|
67 |
}; |
68 |
|
69 |
var options2 = {
|
70 |
key: key2,
|
71 |
cert: cert2
|
72 |
}; |
73 |
|
74 |
var options3 = {
|
75 |
key: key3,
|
76 |
cert: cert3
|
77 |
}; |
78 |
|
79 |
var server1 = server(options1);
|
80 |
var server2 = server(options2);
|
81 |
var server3 = server(options3);
|
82 |
|
83 |
var listenWait = 0; |
84 |
|
85 |
var port = common.PORT;
|
86 |
var port1 = port++;
|
87 |
var port2 = port++;
|
88 |
var port3 = port++;
|
89 |
server1.listen(port1, listening()); |
90 |
server2.listen(port2, listening()); |
91 |
server3.listen(port3, listening()); |
92 |
|
93 |
var responseErrors = {};
|
94 |
var expectResponseCount = 0; |
95 |
var responseCount = 0; |
96 |
var pending = 0; |
97 |
|
98 |
|
99 |
|
100 |
function server(options, port) { |
101 |
var s = https.createServer(options, handler);
|
102 |
s.requests = []; |
103 |
s.expectCount = 0;
|
104 |
return s;
|
105 |
} |
106 |
|
107 |
function handler(req, res) { |
108 |
this.requests.push(req.url);
|
109 |
res.statusCode = 200;
|
110 |
res.setHeader('foo', 'bar'); |
111 |
res.end('hello, world\n');
|
112 |
} |
113 |
|
114 |
function listening() { |
115 |
listenWait++; |
116 |
return function() { |
117 |
listenWait--; |
118 |
if (listenWait === 0) { |
119 |
allListening(); |
120 |
} |
121 |
} |
122 |
} |
123 |
|
124 |
function makeReq(path, port, error, host, ca) { |
125 |
pending++; |
126 |
var options = {
|
127 |
port: port,
|
128 |
path: path,
|
129 |
ca: ca
|
130 |
}; |
131 |
var whichCa = 0; |
132 |
if (!ca) {
|
133 |
options.agent = agent0; |
134 |
} else {
|
135 |
if (!Array.isArray(ca)) ca = [ca];
|
136 |
if (-1 !== ca.indexOf(ca1) && -1 !== ca.indexOf(ca2)) { |
137 |
options.agent = agent3; |
138 |
} else if (-1 !== ca.indexOf(ca1)) { |
139 |
options.agent = agent1; |
140 |
} else if (-1 !== ca.indexOf(ca2)) { |
141 |
options.agent = agent2; |
142 |
} else {
|
143 |
options.agent = agent0; |
144 |
} |
145 |
} |
146 |
|
147 |
if (host) {
|
148 |
options.headers = { host: host }
|
149 |
} |
150 |
var req = https.get(options);
|
151 |
expectResponseCount++; |
152 |
var server = port === port1 ? server1
|
153 |
: port === port2 ? server2 |
154 |
: port === port3 ? server3 |
155 |
: null;
|
156 |
|
157 |
if (!server) throw new Error('invalid port: '+port); |
158 |
server.expectCount++; |
159 |
|
160 |
req.on('response', function(res) { |
161 |
responseCount++; |
162 |
assert.equal(res.connection.authorizationError, error); |
163 |
responseErrors[path] = res.connection.authorizationError; |
164 |
pending--; |
165 |
if (pending === 0) { |
166 |
server1.close(); |
167 |
server2.close(); |
168 |
server3.close(); |
169 |
} |
170 |
}) |
171 |
} |
172 |
|
173 |
function allListening() { |
174 |
// ok, ready to start the tests!
|
175 |
|
176 |
// server1: host 'agent1', signed by ca1
|
177 |
makeReq('/inv1', port1, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'); |
178 |
makeReq('/inv1-ca1', port1,
|
179 |
'Hostname/IP doesn\'t match certificate\'s altnames',
|
180 |
null, ca1);
|
181 |
makeReq('/inv1-ca1ca2', port1,
|
182 |
'Hostname/IP doesn\'t match certificate\'s altnames',
|
183 |
null, [ca1, ca2]);
|
184 |
makeReq('/val1-ca1', port1, null, 'agent1', ca1); |
185 |
makeReq('/val1-ca1ca2', port1, null, 'agent1', [ca1, ca2]); |
186 |
makeReq('/inv1-ca2', port1,
|
187 |
'UNABLE_TO_VERIFY_LEAF_SIGNATURE', 'agent1', ca2); |
188 |
|
189 |
// server2: self-signed, host = 'agent2'
|
190 |
// doesn't matter that thename matches, all of these will error.
|
191 |
makeReq('/inv2', port2, 'DEPTH_ZERO_SELF_SIGNED_CERT'); |
192 |
makeReq('/inv2-ca1', port2, 'DEPTH_ZERO_SELF_SIGNED_CERT', |
193 |
'agent2', ca1);
|
194 |
makeReq('/inv2-ca1ca2', port2, 'DEPTH_ZERO_SELF_SIGNED_CERT', |
195 |
'agent2', [ca1, ca2]);
|
196 |
|
197 |
// server3: host 'agent3', signed by ca2
|
198 |
makeReq('/inv3', port3, 'UNABLE_TO_VERIFY_LEAF_SIGNATURE'); |
199 |
makeReq('/inv3-ca2', port3,
|
200 |
'Hostname/IP doesn\'t match certificate\'s altnames',
|
201 |
null, ca2);
|
202 |
makeReq('/inv3-ca1ca2', port3,
|
203 |
'Hostname/IP doesn\'t match certificate\'s altnames',
|
204 |
null, [ca1, ca2]);
|
205 |
makeReq('/val3-ca2', port3, null, 'agent3', ca2); |
206 |
makeReq('/val3-ca1ca2', port3, null, 'agent3', [ca1, ca2]); |
207 |
makeReq('/inv3-ca1', port3,
|
208 |
'UNABLE_TO_VERIFY_LEAF_SIGNATURE', 'agent1', ca1); |
209 |
|
210 |
} |
211 |
|
212 |
process.on('exit', function() { |
213 |
console.error(responseErrors); |
214 |
assert.equal(server1.requests.length, server1.expectCount); |
215 |
assert.equal(server2.requests.length, server2.expectCount); |
216 |
assert.equal(server3.requests.length, server3.expectCount); |
217 |
assert.equal(responseCount, expectResponseCount); |
218 |
}); |