-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_modified.js
100 lines (87 loc) · 3.07 KB
/
reader_modified.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
var express = require('express');
var request = require('request');
var crypto = require('crypto');
var app = express();
var MAX_INT = 4294967295;
var id = 'reader123';
var pass = 'secret';
var K_enc = 'AaBbCcDdEe';
var K_mac = '1234567890';
function encrypt(key, arr){
var buf = new Buffer(key, 'binary');
var cipher = crypto.createCipher('aes192', buf);
msg = [];
for (var i = 0; i < arr.length; i++) {
msg.push(cipher.update(":" + arr[i], "binary", "base64"));
}
msg.push(cipher.final("base64"));
return msg.join('');
}
// Compute hash of user, encrypt password, and compute and send MAC
var shasum = crypto.createHash('sha256');
shasum.update(id);
var c_alpha = shasum.digest('base64');
var c_beta = encrypt(K_enc, [pass]);
var hmac = crypto.createHmac('sha256', K_mac);
hmac.update(c_beta);
var c_gamma = hmac.digest('base64');
var cert_options = {
uri: 'http://localhost:3002/authenticate',
method: 'GET',
json: {
c_alpha: c_alpha,
c_beta: c_beta,
c_gamma: c_gamma
}
}
console.log("Start");
request(cert_options, function (error, response, body) {
if (error) console.log(error);
else if (response.statusCode == 200) {
var r = Math.floor(Math.random() * MAX_INT);
var tag_options = {
uri: 'http://localhost:3000/query/' + r,
method: 'GET',
json: {
access: body.access
}
};
request(tag_options, function (error, response, body) {
if (error) console.log(error);
else if (response.statusCode == 200) {
var post_data = body;
post_data.N_r = r;
var options = {
uri: 'http://localhost:3001/query',
method: 'GET',
json: post_data
};
request(options, function (error, response, body) {
if (error) console.log(error);
else if (response.statusCode == 200) {
var auth_data = body;
var auth_options = {
uri: 'http://localhost:3000/authenticate',
method: 'GET',
json: auth_data
};
request(auth_options, function (error, response, body) {
if (error) console.log(error);
else if (response.statusCode == 200) {
console.log('success');
} else {
console.log('Authentication failed: ' + response.statusCode);
}
})
} else {
console.log('Database query failed: ' + response.statusCode);
}
});
} else {
console.log('Tag query failed: ' + response.statusCode);
}
});
} else {
console.log('Reader query failed: ' + response.statusCode);
}
});