-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
278 lines (232 loc) · 10.1 KB
/
server.js
File metadata and controls
278 lines (232 loc) · 10.1 KB
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
// ------------------------------------------------------------------------------------
// Initialization
'use strict';
var port = process.env.PORT || 1337;
var express = require("express");
var bodyParser = require('body-parser')
var fs = require("fs");
var crypto = require("crypto");
var uuidv4 = require('uuid/v4');
var NodeRSA = require('node-rsa');
var app = express();
app.use(bodyParser.json()); // to support JSON-encoded bodies
app.use(bodyParser.urlencoded({ // to support URL-encoded bodies
extended: true
}));
app.use('/', express.static('static'));
app.use(function (err, req, res, next) {
res.status(500);
res.render('error', { error: err });
});
app.get("/", function (req, res) {
res.sendFile(__dirname + "/res/Index.html");
});
// Initialize logging to console
app.use(function (req, res, next) {
function truncateString(s, max) {
if (max < 3) return "...";
s = "" + s;
if (s.length > max - 3) {
return s.substring(0, max - 3) + "...";
}
return s;
}
if (req.body) {
console.log("------------------------------------------------");
console.log(req.path);
for (let k in req.body) {
console.log(" " + k + " : " + truncateString(JSON.stringify(req.body[k]), 40));
}
console.log("----");
let old_end = res.end;
res.end = function (value) {
let obj;
try { obj = JSON.parse(value);} catch (err){}
if (obj) {
for (let k in obj) {
console.log(" " + k + " : " + truncateString(JSON.stringify(obj[k]), 40));
}
} else {
console.log(value);
}
old_end.apply(res, arguments);
};
}
next();
});
// ------------------------------------------------------------------------------------
// Data and Helper functions
var accounts = [];
var accountsByPublicKey = {};
var sessions = {};
function createSession() {
let session = {
sessionId: uuidv4(), // Generate a random GUID to identify this session
account: null, // Not yet attached to an Account, because the user hasn't identified itself and logged in yet
loggedIn: false // Not logged in yet...
};
sessions[session.sessionId] = session;
return session;
}
function generateChallenge() {
return crypto.randomBytes(32).toString('base64');
}
function sha256(str) {
return crypto.createHash('sha256').update('alice', 'utf8').digest("base64");
}
// ------------------------------------------------------------------------------------
// Registration
app.post("/api/register_begin", function (req, res) {
let account = {
accountId: uuidv4(), // Generate a random GUID for the AccountId
firstName: req.body.firstName,
lastName: req.body.lastName,
authenticators : [],
register_challenge: generateChallenge()
};
accounts.push(account);
res.end(JSON.stringify({
accountId: account.accountId,
register_challenge: account.register_challenge
}));
});
function fakeX509verify(cert, data, sig) {
return cert == "<<attestationCertificate>>" && sig == "<<attestationSignature>>";
}
app.post("/api/register_end", function (req, res) {
let account = accounts.find(a => a.accountId === req.body.accountId);
if (account) {
let publicKey = req.body.attestationObject.authData.publicKey;
let authenticatorData = req.body.attestationObject.authData.authenticatorData;
// Here we could verify if the Authentication (authenticatorData) meets the requirements of the web application. (For example disallow specific vendors.)
// https://www.w3.org/TR/2017/WD-webauthn-20170811/#sec-client-data
let clientData = {
challenge: account.register_challenge,
origin: "webauth-prototype.org", // This name should be the domain where the web application is hostet.
hashAlgorithm: "SHA-256",
tokenBindingId: null,
clientExtensions: null,
authenticatorExtensions: null
};
let clientDataJSON = JSON.stringify(clientData);
let clientDataHash = sha256(clientDataJSON);
// Here the server would validate the X.509 certificate and signature from the Authentication
// The signature should be over a concatination of clientDataHash (which is generated over the challenge, among other things)
// and attestationObject (which contains the generated publicKey)
//
// This did not make it into the prototype, so it is a fake implementation.
let x509cert = req.body.attestationObject.attStmt.x5c;
let x509sign = req.body.attestationObject.attStmt.sig;
let x509data = clientDataHash + JSON.stringify(req.body.attestationObject); // This would be a binary concatination in a real implementaiton
if (fakeX509verify(x509cert, x509data, x509sign)) {
account.authenticators.push({
publicKey: publicKey,
authenticatorData: authenticatorData
});
accountsByPublicKey[publicKey] = account;
let session = createSession();
session.loggedIn = true; // Automatically log in when device is registered.
res.end(JSON.stringify({ success: true, sessionId: session.sessionId }));
} else {
res.end(JSON.stringify({ success: false, message : "Attestation did not verify correctly" }));
}
} else {
res.end(JSON.stringify({ success: false, message: "Invalid Account" }));
}
});
// ------------------------------------------------------------------------------------
// Login
app.post("/api/login_begin", function (req, res) {
let session = createSession();
session.login_challenge = generateChallenge();
res.end(JSON.stringify({
sessionId: session.sessionId,
login_challenge: session.login_challenge
}));
});
app.post("/api/login_end", function (req, res) {
let session = sessions[req.body.sessionId];
if (!session) {
res.end(JSON.stringify({ success: false, message: "Session not found." }));
return;
}
let account = accountsByPublicKey[req.body.publicKey];
if (!account) {
res.end(JSON.stringify({ success: false, message: "Invalid Account" }));
return;
}
if (session.loggedIn) {
res.end(JSON.stringify({ success: false, message : "Already logged in" }));
return;
}
let authenticator = account.authenticators.find(a => a.publicKey == req.body.publicKey);
if (!authenticator) {
res.end(JSON.stringify({ success: false, message: "Invalid Authenticator" }));
return;
}
// https://www.w3.org/TR/2017/WD-webauthn-20170811/#sec-client-data
let clientData = {
challenge: session.login_challenge,
origin: "webauth-prototype.org", // This name should be the domain where the web application is hostet.
hashAlgorithm: "SHA-256",
tokenBindingId: null, // Omittet in this prototype
clientExtensions: null, // Omittet in this prototype
authenticatorExtensions: null // Omittet in this prototype
};
let clientDataJSON = JSON.stringify(clientData);
let clientDataHash = sha256(clientDataJSON);
// The signature is over the concatination of clientDataHash and authenticatorData (https://www.w3.org/TR/2017/WD-webauthn-20170811/#op-get-assertion)
// We use string concatination here, but it may be binary concatination in the protocol. It is somewhat unclear in the spec.
let challenge = clientDataHash + req.body.authenticatorData;
let key = new NodeRSA(authenticator.publicKey, "pkcs1-public-pem", { signingScheme: "pkcs1-sha256" });
if (key.verify(challenge, req.body.signature, "base64", "base64")) {
session.account = account; // Attach the account to the session
session.loggedIn = true;
res.end(JSON.stringify({
success: true,
accountId: account.accountId,
firstName: account.firstName,
lastName: account.lastName
}));
} else {
res.end(JSON.stringify({ success: false, message : "Invalid Attestation" }));
}
});
// ------------------------------------------------------------------------------------
// Logout
app.post("/api/logout", function (req, res) {
let session = sessions[req.body.sessionId];
if (session) {
delete sessions[req.body.sessionId];
res.end(JSON.stringify({ success: true }));
} else {
res.end(JSON.stringify({ success: false }));
}
});
//------------------------------------------------------------------------------------
// Cryptographic helper functions for the client
// In a real life scenario theese functions would be implemented inside the
// authenticator devices, but in this prototype we cheat and let the server help.
app.post("/api/client_crypto/generate_rsa_key", function (req, res) {
let key = new NodeRSA({ b: 2048 });
res.end(JSON.stringify({
publicKey : key.exportKey("pkcs1-public-pem"),
privateKey: key.exportKey("pkcs1-private-pem")
}));
});
app.post("/api/client_crypto/sign_challenge", function (req, res) {
let key = new NodeRSA(req.body.privateKey, "pkcs1-private-pem", { signingScheme: "pkcs1-sha256" });
let signature = key.sign(req.body.challenge, "base64", "base64");
res.end(JSON.stringify({
signature: signature
}));
});
app.post("/api/client_crypto/sha256", function (req, res) {
res.end(JSON.stringify({
hash: sha256(req.body.value)
}));
});
//------------------------------------------------------------------------------------
app.listen(port, function () {
console.log("Listening on port " + port);
});