This repository has been archived by the owner on Jun 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Document API, tests, example, CHANGELOG
- Loading branch information
1 parent
682fa30
commit 8af95ce
Showing
4 changed files
with
271 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,48 +1,130 @@ | ||
'use strict'; | ||
const assert = require('assert'); | ||
const util = require('util'); | ||
const https = require('https'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
const tls = require('tls'); | ||
const sslkeylog = require('../index.js'); | ||
const unlink = util.promisify(fs.unlink); | ||
|
||
describe('sslkeylog', function(){ | ||
const once = (obj, event)=>new Promise((resolve, reject)=>{ | ||
function listener() { | ||
resolve(Array.from(arguments)); | ||
obj.removeListener('error', errorListener); | ||
} | ||
function errorListener(err) { | ||
reject(err); | ||
obj.removeListener(event, listener); | ||
} | ||
obj.once(event, listener).once('error', errorListener); | ||
}) | ||
|
||
describe('sslkeylog API', function(){ | ||
const hello = "Hello, world"; | ||
let server, result; | ||
before(()=>new Promise(resolve=>{ | ||
const client_random_line_ws = /^CLIENT_RANDOM [0-9a-f]{64} [0-9a-f]{96}\n$/; | ||
const client_random_line = /^CLIENT_RANDOM [0-9a-f]{64} [0-9a-f]{96}$/; | ||
let server, key; | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; | ||
const logFile = `${__dirname}/keys.log`; | ||
// care has to be taken with order of tests, because they have side-effects | ||
|
||
before(async ()=>{ | ||
const ssl_opt = { | ||
key: fs.readFileSync(`${__dirname}/test.key`), | ||
cert: fs.readFileSync(`${__dirname}/test.crt`), | ||
host: '127.0.0.1', | ||
maxVersion: 'TLSv1.2', | ||
}; | ||
server = https.createServer(ssl_opt, (req, res)=>{ | ||
res.writeHead(200); | ||
res.end(hello); | ||
}); | ||
server.on('secureConnection', socket=>{ | ||
result = sslkeylog.get_session_key(socket); | ||
key = sslkeylog.get_session_key(socket); | ||
}); | ||
server.listen(resolve); | ||
})); | ||
server.listen(); | ||
await once(server, 'listening'); | ||
}); | ||
after(()=>{ | ||
server.close(); | ||
}); | ||
it('basic', ()=>new Promise(resolve=>{ | ||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0; | ||
let req = https.request({port: server.address().port}, res=>{ | ||
let response = []; | ||
res.on('data', chunk=>response.push(chunk)); | ||
res.on('end', ()=>{ | ||
let str = Buffer.concat(response).toString(); | ||
assert(str==hello); | ||
assert(result instanceof Object); | ||
let {client_random, master_key} = result; | ||
assert(client_random instanceof Buffer); | ||
assert(client_random.length==32); | ||
assert(master_key instanceof Buffer); | ||
assert(master_key.length==48); | ||
resolve(); | ||
}); | ||
}); | ||
req.end(); | ||
})); | ||
|
||
it('should retrieve session key', async ()=>{ | ||
let req = https.get({port: server.address().port}); | ||
let [res] = await once(req, 'response'); | ||
|
||
let response = []; | ||
res.on('data', chunk=>response.push(chunk)); | ||
await once(res, 'end'); | ||
let str = Buffer.concat(response).toString(); | ||
|
||
assert.equal(str, hello); | ||
assert(key instanceof Object); | ||
let {client_random, master_key} = key; | ||
assert(client_random instanceof Buffer); | ||
assert.equal(client_random.length, 32); | ||
assert(master_key instanceof Buffer); | ||
assert.equal(master_key.length, 48); | ||
}); | ||
|
||
it('should intercept default agent', async ()=>{ | ||
await unlink(logFile).catch(() => {}); | ||
sslkeylog.set_log(logFile); | ||
|
||
sslkeylog.hook_agent(); | ||
let req = https.get({port: server.address().port}); | ||
let [res] = await once(req, 'response'); | ||
assert(client_random_line_ws.test(fs.readFileSync(logFile, 'utf8'))); | ||
res.resume(); | ||
await once(res, 'end'); | ||
}); | ||
|
||
it("shouldn't intercept other agent connections", async ()=>{ | ||
await unlink(logFile).catch(() => {}); | ||
|
||
let req = https.get({port: server.address().port, agent: false}); | ||
let [res] = await once(req, 'response'); | ||
assert(!fs.existsSync(logFile)); | ||
res.resume(); | ||
await once(res, 'end'); | ||
}); | ||
|
||
it('should intercept a particular connection', async ()=>{ | ||
await unlink(logFile).catch(() => {}); | ||
const socket1 = tls.connect({port: server.address().port}); | ||
const socket2 = tls.connect({port: server.address().port}); | ||
sslkeylog.hook_socket(socket1); | ||
await once(socket1, 'secureConnect'); | ||
await once(socket2, 'secureConnect'); | ||
assert(client_random_line_ws.test(fs.readFileSync(logFile, 'utf8'))); | ||
socket1.destroy(); | ||
socket2.destroy(); | ||
}); | ||
|
||
it('should intercept server connections', async ()=>{ | ||
await unlink(logFile).catch(() => {}); | ||
|
||
sslkeylog.hook_server(server); | ||
let req = https.get({port: server.address().port}); | ||
let [res] = await once(req, 'response'); | ||
const [line1, line2] = fs.readFileSync(logFile, 'utf8').trimRight().split('\n'); | ||
assert.equal(line1, line2); | ||
assert(client_random_line.test(line1)); | ||
res.resume(); | ||
await once(res, 'end'); | ||
}); | ||
|
||
it('should intercept all connections', async ()=>{ | ||
await unlink(logFile).catch(() => {}); | ||
|
||
sslkeylog.hook_all(); | ||
const socket = tls.connect({port: server.address().port}); | ||
await once(socket, 'secureConnect'); | ||
const [line1, line2] = fs.readFileSync(logFile, 'utf8').trimRight().split('\n'); | ||
assert.equal(line1, line2); | ||
assert(client_random_line.test(line1)); | ||
socket.destroy(); | ||
}); | ||
|
||
}); |