-
Notifications
You must be signed in to change notification settings - Fork 2
/
pairing-general-data-io-characteristic.js
546 lines (483 loc) · 27.5 KB
/
pairing-general-data-io-characteristic.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
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
var util = require('util');
var crc = require('crc');
var nukiConstants = require('./nuki-constants');
var _ = require('underscore');
var sodium = require('sodium');
var HSalsa20 = require('./hsalsa20');
var crypto = require('crypto');
var bleno = require('bleno');
var BlenoCharacteristic = bleno.Characteristic;
var BlenoDescriptor = bleno.Descriptor;
function PairingGeneralDataInputOutputCharacteristic(keys, config) {
this.state = this.PAIRING_IDLE;
this.keys = keys;
this.dataStillToSend = new Buffer(0);
this.config = config;
this.users = config.get('users');
if (!this.users) {
config.set('users', {});
this.users = config.get("users");
config.save(function (err) {
if (err) {
console.log("Writing configuration failed", err);
} else {
console.log("Initial user configuration saved");
}
});
}
var strUuid = config.get('uuid');
if (strUuid && _.isString(strUuid) && strUuid.length === 32) {
this.slUuid = new Buffer(strUuid, 'hex');
PairingGeneralDataInputOutputCharacteristic.super_.call(this, {
uuid: 'a92ee101550111e4916c0800200c9a66',
properties: ['read', 'write', 'indicate', 'notify'],
descriptors: [
new BlenoDescriptor({
uuid: '2902' // client characterstic configuration
})
]
});
} else {
console.log("ERROR: no SL UUID in config", strUuid);
}
}
util.inherits(PairingGeneralDataInputOutputCharacteristic, BlenoCharacteristic);
PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_IDLE = 0;
PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_SL_SEND_PUBKEY = 1;
PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_PUBKEY = 2;
PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_SL_SEND_CHALLENGE = 3;
PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHENTICATOR = 4;
PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_SL_SEND_CHALLENGE_2 = 5;
PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHORIZATION_DATA = 6;
PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_SL_SEND_AUTHORIZATION_ID = 7;
PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHORIZATION_ID_CONFIRMATION = 8;
PairingGeneralDataInputOutputCharacteristic.prototype.crcOk = nukiConstants.crcOk;
PairingGeneralDataInputOutputCharacteristic.prototype.getNextChunk = function (data) {
var block0;
if (data.length > 20) {
block0 = data.slice(0, 20);
this.dataStillToSend = data.slice(20);
} else {
block0 = data;
this.dataStillToSend = new Buffer(0);
}
return block0;
};
PairingGeneralDataInputOutputCharacteristic.prototype.prepareDataToSend = function (cmd, data) {
var cmdBuffer = new Buffer(2);
cmdBuffer.writeUInt16LE(cmd);
var responseData = Buffer.concat([cmdBuffer, data]);
var checksum = crc.crc16ccitt(responseData);
var checksumBuffer = new Buffer(2);
checksumBuffer.writeUInt16LE(checksum);
this.dataStillToSend = Buffer.concat([responseData, checksumBuffer]);
// console.log("prepared to send:", this.dataStillToSend, this.dataStillToSend.length);
};
PairingGeneralDataInputOutputCharacteristic.prototype.onReadRequest = function (offset, callback) {
callback(this.RESULT_SUCCESS, new Buffer('Hallo', 'ascii'));
};
PairingGeneralDataInputOutputCharacteristic.prototype.onWriteRequest = function (data, offset, withoutResponse, callback) {
var cmdId, slPk, value, clCr, cr;
// console.log("PairingGeneralDataInputOutputCharacteristic data: ", data);
// console.log("PairingGeneralDataInputOutputCharacteristic state: ", this.state);
if (this.crcOk(data)) {
// console.log("checksum is ok");
if (offset) {
callback(this.RESULT_ATTR_NOT_LONG);
} else if (data.length > 200) {
callback(this.RESULT_INVALID_ATTRIBUTE_LENGTH);
} else {
switch (this.state) {
case PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_IDLE:
var rCmd = data.readUInt16LE(0);
cmdId = data.readUInt16LE(2);
if (rCmd === nukiConstants.CMD_REQUEST_DATA && cmdId === nukiConstants.CMD_ID_PUBLIC_KEY) {
slPk = new Buffer(0);
if (Buffer.isBuffer(this.keys.slPk)) {
slPk = this.keys.slPk;
} else {
if (_.isString(this.keys.slPk)) {
slPk = new Buffer(this.keys.slPk, 'hex');
} else {
if (_.isArray(this.keys.slPk)) {
slPk = new Buffer(this.keys.slPk);
}
}
}
if (slPk.length > 0) {
this.prepareDataToSend(nukiConstants.CMD_ID_PUBLIC_KEY, slPk);
this.state = PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_PUBKEY;
while (this.dataStillToSend.length > 0) {
value = this.getNextChunk(this.dataStillToSend);
if (this._updateValueCallback && value.length > 0) {
console.log("Step 4: SL sending PK...");
this._updateValueCallback(value);
}
}
callback(this.RESULT_SUCCESS);
} else {
console.log("ERROR: SL does not have a public key");
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
}
}
else {
console.log("ERROR: command or command identifier wrong");
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
}
break;
case PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_PUBKEY:
cmdId = data.readUInt16LE(0);
if (cmdId === nukiConstants.CMD_ID_PUBLIC_KEY) {
this.keys.clPk = data.slice(2, data.length - 2);
console.log("Step 6: CL sent PK:");
slPk = new Buffer(0);
if (Buffer.isBuffer(this.keys.slPk)) {
slPk = this.keys.slPk;
} else {
if (_.isString(this.keys.slPk)) {
slPk = new Buffer(this.keys.slPk, 'hex');
} else {
if (_.isArray(this.keys.slPk)) {
slPk = new Buffer(this.keys.slPk);
}
}
}
var slSk = new Buffer(0);
if (Buffer.isBuffer(this.keys.slSk)) {
slSk = this.keys.slSk;
} else {
if (_.isString(this.keys.slSk)) {
slSk = new Buffer(this.keys.slSk, 'hex');
} else {
if (_.isArray(this.keys.slSk)) {
slSk = new Buffer(this.keys.slSk);
}
}
}
this.keys.slPk = slPk;
this.keys.slSk = slSk;
// console.log("slPK", slPk);
// console.log("slSK", slSk);
// console.log("clPK", this.keys.clPk);
// Create Diffie-Hellman key from nuki secret key and clients public key
// crypto_scalarmult_curve25519(k,secretKey,pk)
console.log("Step 7: creating Diffie-Hellman key...");
var k = sodium.api.crypto_scalarmult(slSk, this.keys.clPk);
// console.log("SL DH Key from SL SK and CL PK: ", k);
console.log("Step 8: deriving long term shared key...");
// derive a longterm shared secret key s from k using function kdf1
// static const unsigned char _0[16];
// static const unsigned char sigma[16] = "expand 32-byte k";
// crypto_core_hsalsa20(firstKey,_0,sharedKey,sigma)
var hsalsa20 = new HSalsa20();
this.keys.sharedSecret = new Buffer(32);
var inv = new Buffer(16);
inv.fill(0);
var c = new Buffer("expand 32-byte k");
hsalsa20.crypto_core(this.keys.sharedSecret, inv, k, c);
// console.log("derived shared key: ", this.keys.sharedSecret);
console.log("Step 9: creating one time challenge...");
this.keys.sc = new Buffer(nukiConstants.NUKI_NONCEBYTES);
sodium.api.randombytes_buf(this.keys.sc);
// this.keys.sc = new Buffer("6CD4163D159050C798553EAA57E278A579AFFCBC56F09FC57FE879E51C42DF17", 'hex');
if (this.keys.sc.length != nukiConstants.NUKI_NONCEBYTES) {
console.log("Nonce length (" + this.keys.sc.length + ") is not " + nukiConstants.NUKI_NONCEBYTES);
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
return;
}
this.state = PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHENTICATOR;
this.prepareDataToSend(nukiConstants.CMD_CHALLENGE, this.keys.sc);
while (this.dataStillToSend.length > 0) {
value = this.getNextChunk(this.dataStillToSend);
if (this._updateValueCallback && value.length > 0) {
// console.log("sending challenge 1: " + value.length + " bytes");
this._updateValueCallback(value);
} else {
this.dataStillToSend = new Buffer(0);
console.log("ERROR: no updateValueCallback. Can't continue with pairing.");
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
return;
}
}
console.log("Step 12: creating authorization authenticator...");
var r = Buffer.concat([this.keys.clPk, slPk, this.keys.sc]);
// use HMAC-SHA256 to create the authenticator
var a = crypto.createHmac('SHA256', this.keys.sharedSecret).update(r).digest();
callback(this.RESULT_SUCCESS);
}
else {
console.log("command or command identifier wrong");
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
}
break;
case PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHENTICATOR:
cmdId = data.readUInt16LE(0);
if (cmdId === nukiConstants.CMD_AUTHORIZATION_AUTHENTICATOR) {
console.log("Step 13: CL sent authorization authenticator.");
clCr = data.slice(2, data.length - 2);
console.log("Step 14: verifying authorization authenticator...");
r = Buffer.concat([this.keys.clPk, this.keys.slPk, this.keys.sc]);
// use HMAC-SHA256 to create the authenticator
cr = crypto.createHmac('SHA256', this.keys.sharedSecret).update(r).digest();
// Step 14: verify authenticator
if (Buffer.compare(clCr, cr) === 0) {
console.log("Step 14: authenticators verified ok");
// Step 15: send second challenge
console.log("Step 15: creating one time challenge...");
this.keys.sc = new Buffer(nukiConstants.NUKI_NONCEBYTES);
sodium.api.randombytes_buf(this.keys.sc);
// this.keys.sc = new Buffer("E0742CFEA39CB46109385BF91286A3C02F40EE86B0B62FC34033094DE41E2C0D", 'hex');
// if (this.keys.sc.length != nukiConstants.NUKI_NONCEBYTES) {
// console.log("Nonce length (" + this.keys.sc.length + ") is not " + nukiConstants.NUKI_NONCEBYTES);
// this.state = this.PAIRING_IDLE;
// callback(this.RESULT_UNLIKELY_ERROR);
// return;
// }
this.state = PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHORIZATION_DATA;
this.prepareDataToSend(nukiConstants.CMD_CHALLENGE, this.keys.sc);
while (this.dataStillToSend.length > 0) {
value = this.getNextChunk(this.dataStillToSend);
if (this._updateValueCallback && value.length > 0) {
// console.log("sending challenge 2: " + value.length + " bytes");
console.log("Step 15: sending one time challenge...");
this._updateValueCallback(value);
}
}
callback(this.RESULT_SUCCESS);
} else {
console.log("Step 14: CL and SL authenticators are not equal. Possible man in the middle attack. Exiting.");
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
}
} else {
console.log("ERROR: command or command identifier wrong. Expected CMD_AUTHORIZATION_AUTHENTICATOR");
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
}
break;
case PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHORIZATION_DATA:
cmdId = data.readUInt16LE(0);
if (cmdId === nukiConstants.CMD_AUTHORIZATION_DATA) {
// Step 16: client sent authorization data
var clAuthData = data.slice(2, data.length - 2);
console.log("Step 16: CL sent authorization data.");
clCr = clAuthData.slice(0, 32);
console.log("Step 17: verifying authenticator...");
var appType = clAuthData.readUInt8(32);
var idTypeBuffer = new Buffer([appType]);
var appId = clAuthData.readUInt32LE(33);
var idBuffer = clAuthData.slice(33, 33 + 4);
var nameBuffer = clAuthData.slice(37, 37 + 32);
this.keys.nonceABF = clAuthData.slice(69, 69 + 32);
// create authenticator for the authorization data message
r = Buffer.concat([idTypeBuffer, idBuffer, nameBuffer, this.keys.nonceABF, this.keys.sc]);
// use HMAC-SHA256 to create the authenticator
cr = crypto.createHmac('SHA256', this.keys.sharedSecret).update(r).digest();
if (Buffer.compare(clCr, cr) === 0) {
console.log("Step 17: authenticator verified ok.");
switch (appType) {
case 0:
console.log("Type is App");
break;
case 1:
console.log("Type is Bridge");
break;
case 2:
console.log("Type is Fob");
break;
}
console.log("App ID: " + appId);
var name = nameBuffer.toString().trim();
console.log("Name: " + name);
var newAuthorizationId = 1;
var user = _.findWhere(this.users, {name: name});
if (user) {
newAuthorizationId = user.authorizationId;
} else {
_.each(this.users, function (user) {
if (user.authorizationId >= newAuthorizationId) {
newAuthorizationId = user.authorizationId + 1;
}
})
}
this.users[newAuthorizationId] = {
authorizationId: newAuthorizationId,
name: name,
appId: appId,
appType: appType,
sharedSecret: this.keys.sharedSecret.toString('hex')
};
this.config.set("users", this.users);
this.config.save(function (err) {
if (err) {
console.log("ERROR: writing configuration with new authorization id failed", err);
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
return;
} else {
console.log("Step 18: new user " + name + " with authorization id " + newAuthorizationId + " added to configuration");
}
});
// 32 authenticator
// 4 auth id
// 16 uuid
// 32 nonce
console.log("Step 19: creating authorization-id command...");
this.keys.sc = new Buffer(nukiConstants.NUKI_NONCEBYTES);
sodium.api.randombytes_buf(this.keys.sc);
var newAuthorizationIdBuffer = new Buffer(4);
newAuthorizationIdBuffer.writeUInt32LE(newAuthorizationId);
r = Buffer.concat([newAuthorizationIdBuffer, this.slUuid, this.keys.sc, this.keys.nonceABF]);
// use HMAC-SHA256 to create the authenticator
cr = crypto.createHmac('SHA256', this.keys.sharedSecret).update(r).digest();
this.state = this.PAIRING_CL_SEND_AUTHORIZATION_ID_CONFIRMATION;
var wData = Buffer.concat([cr, newAuthorizationIdBuffer, this.slUuid, this.keys.sc]);
this.prepareDataToSend(nukiConstants.CMD_AUTHORIZATION_ID, wData);
while (this.dataStillToSend.length > 0) {
value = this.getNextChunk(this.dataStillToSend);
if (this._updateValueCallback && value.length > 0) {
// console.log("sending authorization id: " + value.length + " bytes");
this._updateValueCallback(value);
}
}
callback(this.RESULT_SUCCESS);
} else {
console.log("CL and SL authenticators are not equal. Possible man in the middle attack. Exiting.");
console.log("CL Authenticator:", clCr, clCr.length);
console.log("SL Authenticator:", cr, cr.length);
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
}
} else {
console.log("ERROR: command or command identifier wrong. CMD_AUTHORIZATION_DATA was expected");
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
}
break;
case PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHORIZATION_ID_CONFIRMATION:
cmdId = data.readUInt16LE(0);
if (cmdId === nukiConstants.CMD_AUTHORIZATION_ID_CONFIRMATION) {
// todo check confirmation
console.log("Step 21: CL authorization-id confirmation");
console.log("Step 22: Sending status complete.");
this.prepareDataToSend(nukiConstants.CMD_STATUS, new Buffer([nukiConstants.STATUS_COMPLETE]));
value = this.getNextChunk(this.dataStillToSend);
if (this._updateValueCallback && value.length > 0) {
// console.log("sending authorization id: " + value.length + " bytes");
this._updateValueCallback(value);
}
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
} else {
console.log("ERROR: command wrong");
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
}
break;
default:
console.log("ERROR unexpected pairing state");
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
}
}
} else {
console.log("checksum is NOT ok");
this.state = this.PAIRING_IDLE;
callback(this.RESULT_SUCCESS);
}
};
PairingGeneralDataInputOutputCharacteristic.prototype.onSubscribe = function (maxValueSize, updateValueCallback) {
console.log('PairingGeneralDataInputOutputCharacteristic - onSubscribe');
this._updateValueCallback = updateValueCallback;
if (this.dataStillToSend.length > 0) {
switch (this.state) {
case PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_SL_SEND_PUBKEY:
while (this.dataStillToSend.length > 0) {
var value = this.getNextChunk(this.dataStillToSend);
if (value.length > 0) {
console.log("sending " + value.length + " bytes from onSubscribe");
updateValueCallback(value);
}
}
if (this.dataStillToSend.length === 0) {
this.state = PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_PUBKEY;
}
break;
default:
console.log("ERROR unexpected pairing state");
this.state = this.PAIRING_IDLE;
}
} else {
console.log("don't have more data to notify");
}
};
PairingGeneralDataInputOutputCharacteristic.prototype.onUnsubscribe = function () {
console.log('PairingGeneralDataInputOutputCharacteristic - onUnsubscribe');
this._updateValueCallback = null;
};
PairingGeneralDataInputOutputCharacteristic.prototype.onIndicate = function () {
// console.log("PairingGeneralDataInputOutputCharacteristic indicate");
if (this.dataStillToSend.length > 0) {
if (this._updateValueCallback) {
var value;
switch (this.state) {
case PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_SL_SEND_PUBKEY:
value = this.getNextChunk(this.dataStillToSend);
if (value.length > 0) {
// console.log("sending PK: " + value.length + " bytes as indication");
this._updateValueCallback(value);
}
if (this.dataStillToSend.length === 0) {
this.state = PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_PUBKEY;
}
break;
case PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_SL_SEND_CHALLENGE:
value = this.getNextChunk(this.dataStillToSend);
if (value.length > 0) {
// console.log("sending challenge 1: " + value.length + " bytes as indication");
this._updateValueCallback(value);
}
if (this.dataStillToSend.length === 0) {
this.state = PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHENTICATOR;
}
break;
case PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_SL_SEND_CHALLENGE_2:
value = this.getNextChunk(this.dataStillToSend);
if (value.length > 0) {
// console.log("sending challenge 2: " + value.length + " bytes as indication");
this._updateValueCallback(value);
}
if (this.dataStillToSend.length === 0) {
this.state = PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHORIZATION_DATA;
}
break;
case PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_SL_SEND_AUTHORIZATION_ID:
while (this.dataStillToSend.length > 0) {
value = this.getNextChunk(this.dataStillToSend);
if (value.length > 0) {
// console.log("sending authorization id: " + value.length + " bytes as indication");
this._updateValueCallback(value);
}
}
if (this.dataStillToSend.length === 0) {
this.state = PairingGeneralDataInputOutputCharacteristic.prototype.PAIRING_CL_SEND_AUTHORIZATION_ID_CONFIRMATION;
}
break;
default:
console.log("ERROR: unexpected pairing state");
this.state = this.PAIRING_IDLE;
}
} else {
console.log("ERROR: don't have updateValueCallback on indicate");
this.state = this.PAIRING_IDLE;
}
} else {
console.log("don't have more data to indicate");
}
};
module.exports = PairingGeneralDataInputOutputCharacteristic;