This repository has been archived by the owner on Nov 21, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2135 from kvhnuke/devop/update-u2f-api
devop/update-u2f-api
- Loading branch information
Showing
10 changed files
with
423 additions
and
1,054 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
Large diffs are not rendered by default.
Oops, something went wrong.
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,54 +1,69 @@ | ||
/** | ||
* (c) 2017 Douglas Bakkum, Shift Devices AG | ||
* (c) 2017 Douglas Bakkum, Shift Devices AG | ||
* MIT license | ||
**/ | ||
|
||
'use strict'; | ||
**/ | ||
|
||
"use strict"; | ||
const u2f = require("u2f-api"); | ||
var DigitalBitboxUsb = function(timeoutSeconds) { | ||
this.timeoutSeconds = timeoutSeconds; | ||
} | ||
this.timeoutSeconds = timeoutSeconds; | ||
}; | ||
|
||
// Convert from normal to web-safe, strip trailing "="s | ||
DigitalBitboxUsb.webSafe64 = function(base64) { | ||
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); | ||
} | ||
return base64 | ||
.replace(/\+/g, "-") | ||
.replace(/\//g, "_") | ||
.replace(/=+$/, ""); | ||
}; | ||
|
||
// Convert from web-safe to normal, add trailing "="s | ||
DigitalBitboxUsb.normal64 = function(base64) { | ||
return base64.replace(/\-/g, '+').replace(/_/g, '/') + '=='.substring(0, (3*base64.length)%4); | ||
} | ||
return ( | ||
base64.replace(/\-/g, "+").replace(/_/g, "/") + | ||
"==".substring(0, (3 * base64.length) % 4) | ||
); | ||
}; | ||
|
||
DigitalBitboxUsb.prototype.u2fCallback = function(response, callback) { | ||
if ('signatureData' in response) { | ||
var data = new Buffer((DigitalBitboxUsb.normal64(response.signatureData)), 'base64'); | ||
if (data.length > 7) | ||
callback(data.slice(5)); | ||
else | ||
return;// Empty frame received, wait for data | ||
} | ||
else { | ||
callback(undefined, response); | ||
} | ||
} | ||
if ("signatureData" in response) { | ||
var data = new Buffer( | ||
DigitalBitboxUsb.normal64(response.signatureData), | ||
"base64" | ||
); | ||
if (data.length > 7) callback(data.slice(5)); | ||
else return; // Empty frame received, wait for data | ||
} else { | ||
callback(undefined, response); | ||
} | ||
}; | ||
|
||
DigitalBitboxUsb.prototype.exchange = function(msg, callback) { | ||
msg = Buffer.from(msg, 'ascii'); | ||
var kh_max_len = 128 - 2;// Subtract 2 bytes for `index` and `total` header | ||
var challenge = new Buffer("dbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdb", 'hex'); | ||
var total = Math.ceil(msg.length / kh_max_len); | ||
var self = this; | ||
var localCallback = function(result) { | ||
self.u2fCallback(result, callback); | ||
} | ||
for (var index = 0; index < total; index++) { | ||
var kh = Buffer.concat([Buffer.from([total]), Buffer.from([index]), msg.slice(index * kh_max_len, (index + 1) * kh_max_len)]); | ||
var key = { | ||
version: 'U2F_V2', | ||
keyHandle: DigitalBitboxUsb.webSafe64(kh.toString('base64')), | ||
}; | ||
u2f.sign(location.origin, DigitalBitboxUsb.webSafe64(challenge.toString('base64')), [key], localCallback, this.timeoutSeconds); | ||
} | ||
} | ||
msg = Buffer.from(msg, "ascii"); | ||
var kh_max_len = 128 - 2; // Subtract 2 bytes for `index` and `total` header | ||
var challenge = new Buffer( | ||
"dbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdbdb", | ||
"hex" | ||
); | ||
var total = Math.ceil(msg.length / kh_max_len); | ||
var self = this; | ||
var localCallback = function(result) { | ||
self.u2fCallback(result, callback); | ||
}; | ||
for (var index = 0; index < total; index++) { | ||
var kh = Buffer.concat([ | ||
Buffer.from([total]), | ||
Buffer.from([index]), | ||
msg.slice(index * kh_max_len, (index + 1) * kh_max_len) | ||
]); | ||
var key = { | ||
appId: location.origin, | ||
challenge: DigitalBitboxUsb.webSafe64(challenge.toString("base64")), | ||
version: "U2F_V2", | ||
keyHandle: DigitalBitboxUsb.webSafe64(kh.toString("base64")) | ||
}; | ||
u2f.sign([key], this.timeoutSeconds).then(localCallback); | ||
} | ||
}; | ||
|
||
module.exports = DigitalBitboxUsb | ||
module.exports = DigitalBitboxUsb; |
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,68 +1,81 @@ | ||
/******************************************************************************** | ||
* Ledger Communication toolkit | ||
* (c) 2016 Ledger | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
********************************************************************************/ | ||
|
||
'use strict'; | ||
* Ledger Communication toolkit | ||
* (c) 2016 Ledger | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
********************************************************************************/ | ||
|
||
"use strict"; | ||
const u2f = require("u2f-api"); | ||
var Ledger3 = function(scrambleKey, timeoutSeconds) { | ||
this.scrambleKey = new Buffer(scrambleKey, 'ascii'); | ||
this.timeoutSeconds = timeoutSeconds; | ||
} | ||
this.scrambleKey = new Buffer(scrambleKey, "ascii"); | ||
this.timeoutSeconds = timeoutSeconds; | ||
}; | ||
|
||
Ledger3.wrapApdu = function(apdu, key) { | ||
var result = new Buffer(apdu.length); | ||
for (var i=0; i<apdu.length; i++) { | ||
result[i] = apdu[i] ^ key[i % key.length]; | ||
} | ||
return result; | ||
} | ||
var result = new Buffer(apdu.length); | ||
for (var i = 0; i < apdu.length; i++) { | ||
result[i] = apdu[i] ^ key[i % key.length]; | ||
} | ||
return result; | ||
}; | ||
|
||
// Convert from normal to web-safe, strip trailing "="s | ||
Ledger3.webSafe64 = function(base64) { | ||
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); | ||
} | ||
return base64 | ||
.replace(/\+/g, "-") | ||
.replace(/\//g, "_") | ||
.replace(/=+$/, ""); | ||
}; | ||
|
||
// Convert from web-safe to normal, add trailing "="s | ||
Ledger3.normal64 = function(base64) { | ||
return base64.replace(/\-/g, '+').replace(/_/g, '/') + '=='.substring(0, (3*base64.length)%4); | ||
} | ||
return ( | ||
base64.replace(/\-/g, "+").replace(/_/g, "/") + | ||
"==".substring(0, (3 * base64.length) % 4) | ||
); | ||
}; | ||
|
||
Ledger3.prototype.u2fCallback = function(response, callback) { | ||
if (typeof response['signatureData'] != "undefined") { | ||
var data = new Buffer((Ledger3.normal64(response['signatureData'])), 'base64'); | ||
callback(data.toString('hex', 5)); | ||
} | ||
else { | ||
callback(undefined, response); | ||
} | ||
} | ||
if (typeof response["signatureData"] != "undefined") { | ||
var data = new Buffer( | ||
Ledger3.normal64(response["signatureData"]), | ||
"base64" | ||
); | ||
callback(data.toString("hex", 5)); | ||
} else { | ||
callback(undefined, response); | ||
} | ||
}; | ||
|
||
// callback is function(response, error) | ||
Ledger3.prototype.exchange = function(apduHex, callback) { | ||
var apdu = new Buffer(apduHex, 'hex'); | ||
var keyHandle = Ledger3.wrapApdu(apdu, this.scrambleKey); | ||
var challenge = new Buffer("0000000000000000000000000000000000000000000000000000000000000000", 'hex'); | ||
var key = {}; | ||
key['version'] = 'U2F_V2'; | ||
key['keyHandle'] = Ledger3.webSafe64(keyHandle.toString('base64')); | ||
var self = this; | ||
var localCallback = function(result) { | ||
self.u2fCallback(result, callback); | ||
} | ||
u2f.sign(location.origin, Ledger3.webSafe64(challenge.toString('base64')), [key], localCallback, this.timeoutSeconds); | ||
} | ||
var apdu = new Buffer(apduHex, "hex"); | ||
var keyHandle = Ledger3.wrapApdu(apdu, this.scrambleKey); | ||
var challenge = new Buffer( | ||
"0000000000000000000000000000000000000000000000000000000000000000", | ||
"hex" | ||
); | ||
var key = {}; | ||
key["appId"] = location.origin; | ||
key["challenge"] = Ledger3.webSafe64(challenge.toString("base64")); | ||
key["version"] = "U2F_V2"; | ||
key["keyHandle"] = Ledger3.webSafe64(keyHandle.toString("base64")); | ||
var self = this; | ||
var localCallback = function(result) { | ||
self.u2fCallback(result, callback); | ||
}; | ||
u2f.sign([key], this.timeoutSeconds).then(localCallback); | ||
}; | ||
|
||
module.exports = Ledger3 | ||
module.exports = Ledger3; |
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,41 +1,54 @@ | ||
'use strict'; | ||
|
||
"use strict"; | ||
const u2f = require("u2f-api"); | ||
var SecalotUsb = function(timeoutSeconds) { | ||
this.timeoutSeconds = timeoutSeconds; | ||
} | ||
this.timeoutSeconds = timeoutSeconds; | ||
}; | ||
|
||
// Convert from normal to web-safe, strip trailing "="s | ||
SecalotUsb.webSafe64 = function(base64) { | ||
return base64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, ''); | ||
} | ||
return base64 | ||
.replace(/\+/g, "-") | ||
.replace(/\//g, "_") | ||
.replace(/=+$/, ""); | ||
}; | ||
|
||
// Convert from web-safe to normal, add trailing "="s | ||
SecalotUsb.normal64 = function(base64) { | ||
return base64.replace(/\-/g, '+').replace(/_/g, '/') + '=='.substring(0, (3*base64.length)%4); | ||
} | ||
return ( | ||
base64.replace(/\-/g, "+").replace(/_/g, "/") + | ||
"==".substring(0, (3 * base64.length) % 4) | ||
); | ||
}; | ||
|
||
SecalotUsb.prototype.u2fCallback = function(response, callback) { | ||
if (typeof response['signatureData'] != "undefined") { | ||
var data = new Buffer((SecalotUsb.normal64(response['signatureData'])), 'base64'); | ||
callback(data.toString('hex')); | ||
} | ||
else { | ||
callback(undefined, response); | ||
} | ||
} | ||
if (typeof response["signatureData"] != "undefined") { | ||
var data = new Buffer( | ||
SecalotUsb.normal64(response["signatureData"]), | ||
"base64" | ||
); | ||
callback(data.toString("hex")); | ||
} else { | ||
callback(undefined, response); | ||
} | ||
}; | ||
|
||
// callback is function(response, error) | ||
SecalotUsb.prototype.exchange = function(apduHex, callback) { | ||
var apdu = new Buffer('1122334455667788' + apduHex, 'hex'); | ||
var challenge = new Buffer("0000000000000000000000000000000000000000000000000000000000000000", 'hex'); | ||
var key = {}; | ||
key['version'] = 'U2F_V2'; | ||
key['keyHandle'] = SecalotUsb.webSafe64(apdu.toString('base64')); | ||
var self = this; | ||
var localCallback = function(result) { | ||
self.u2fCallback(result, callback); | ||
} | ||
u2f.sign(location.origin, SecalotUsb.webSafe64(challenge.toString('base64')), [key], localCallback, this.timeoutSeconds); | ||
} | ||
var apdu = new Buffer("1122334455667788" + apduHex, "hex"); | ||
var challenge = new Buffer( | ||
"0000000000000000000000000000000000000000000000000000000000000000", | ||
"hex" | ||
); | ||
var key = {}; | ||
key["appId"] = location.origin; | ||
key["challenge"] = SecalotUsb.webSafe64(challenge.toString("base64")); | ||
key["version"] = "U2F_V2"; | ||
key["keyHandle"] = SecalotUsb.webSafe64(apdu.toString("base64")); | ||
var self = this; | ||
var localCallback = function(result) { | ||
self.u2fCallback(result, callback); | ||
}; | ||
u2f.sign([key], this.timeoutSeconds).then(localCallback); | ||
}; | ||
|
||
module.exports = SecalotUsb | ||
module.exports = SecalotUsb; |
Oops, something went wrong.