-
Notifications
You must be signed in to change notification settings - Fork 1
/
trezor_connect.helpers.inc
355 lines (270 loc) · 7.52 KB
/
trezor_connect.helpers.inc
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
<?php
/**
* @file
* Provides helper functions.
*/
/**
* Provides a string containing the administration menu prefix.
*/
define('TREZOR_CONNECT_PREFIX', 'admin/config/trezor-connect');
/**
* Provides a string containing the administration permission.
*/
define('TREZOR_CONNECT_PERMISSION', 'administer TREZOR connect');
/**
* Provides a string containing the use permission.
*/
define('TREZOR_CONNECT_PERMISSION_USE', 'use TREZOR connect');
/**
* Provides a boolean indicating if the TREZOR connect javascript should be
* loaded externally.
*/
define('TREZOR_CONNECT_EXTERNAL', TRUE);
/**
* Provides a string containing the TREZOR connect external javascript url.
*/
define('TREZOR_CONNECT_URL', 'https://trezor.github.io/connect/login.js');
/**
* Provides a string containing the TREZOR connect callback function.
*/
define('TREZOR_CONNECT_CALLBACK', 'trezorLogin');
/**
* Provides a string containing the TREZOR connect login callback url.
*/
define('TREZOR_CONNECT_URL_LOGIN', 'user/login/trezor');
/**
* Provides a string containing the TREZOR connect register callback url.
*/
define('TREZOR_CONNECT_URL_REGISTER', 'user/register/trezor');
/**
* Provides a string containing the TREZOR connect manage url.
*/
define('TREZOR_CONNECT_URL_MANAGE', 'user/%user/trezor-connect');
/**
* Provides a string containing the TREZOR connect manage callback url.
*/
define('TREZOR_CONNECT_URL_MANAGE_JS', TREZOR_CONNECT_URL_MANAGE . '/js');
/**
* Provides an integer containing the challenge expiration offset.
*/
define('TREZOR_CONNECT_OFFSET', 60 * 60);
/**
* Provides an integer representing challenge response version 1.
*/
define('TREZOR_CONNECT_VERSION_1', 1);
/**
* Provides an integer representing challenge response version 2.
*/
define('TREZOR_CONNECT_VERSION_2', 2);
/**
* Returns a boolean indicating if the TREZOR connect javascript should be
* loaded externally.
*/
function trezor_connect_external() {
$output = variable_get('trezor_connect_external', TREZOR_CONNECT_EXTERNAL);
return $output;
}
/**
* Returns a string containing the TREZOR connect external javascript url.
*/
function trezor_connect_url() {
$output = variable_get('trezor_connect_url', TREZOR_CONNECT_URL);
return $output;
}
/**
* Returns a string containing the TREZOR connect callback function.
*/
function trezor_connect_callback() {
$output = variable_get('trezor_connect_callback', TREZOR_CONNECT_CALLBACK);
return $output;
}
/**
* Returns an integer containing the challenge expiration offset.
*/
function trezor_connect_offset() {
$output = variable_get('trezor_connect_offset', TREZOR_CONNECT_OFFSET);
return $output;
}
/**
* Responsible for generating a challenge array.
*
* @return array
*/
function trezor_connect_challenge() {
$output = array();
$challenge_hidden = trezor_connect_random(64);
$challenge_hidden = implode('', $challenge_hidden);
$output['challenge_hidden'] = $challenge_hidden;
$challenge_visual = date('Y-m-d H:i:s');
$output['challenge_visual'] = $challenge_visual;
return $output;
}
/**
* Responsible for returning random data.
*
* @param $length
*
* @return array
*/
function trezor_connect_random($length) {
$output = array();
if ($length<2) {
$length = 2;
}
$exists = function_exists('openssl_random_pseudo_bytes');
if (!$exists) {
$message = <<<EOF
A cryptographically secure random number can not be generated without the PHP
5.3.0 and the OpenSSL extensions openssl_random_pseudo_bytes function.
EOF;
$message = t($message);
throw new Exception($message);
}
else {
$length = $length / 2;
$crypto_strong = FALSE;
while (!$crypto_strong) {
$random = openssl_random_pseudo_bytes($length, $crypto_strong);
}
$random = bin2hex($random);
$output = str_split($random);
}
return $output;
}
/**
* Responsible for adding a challenge to the database.
*
* @param $record
*
* @return bool|int
*/
function trezor_connect_write_challenge($record) {
if (is_object($record)) {
$record = (array)$record;
}
if (!isset($record['created'])) {
$record['created'] = time();
}
$output = drupal_write_record('trezor_connect_challenges', $record);
return $output;
}
/**
* Returns a boolean indicating if a TREZOR response signature is valid.
*
* @param $response
*
* @param $challenge
*
* @return string
*/
function trezor_connect_response_valid($response, $challenge) {
$output = FALSE;
if (is_array($response)) {
$version = TREZOR_CONNECT_VERSION_1;
if (isset($response['version'])) {
$version = $response['version'];
}
$challenge_hidden = NULL;
if (isset($challenge['challenge_hidden'])) {
$challenge_hidden = $challenge['challenge_hidden'];
}
$challenge_visual = NULL;
if (isset($challenge['challenge_visual'])) {
$challenge_visual = $challenge['challenge_visual'];
}
if ($version == TREZOR_CONNECT_VERSION_1) {
$challenge_hidden = hex2bin($challenge_hidden);
}
else if ($version == TREZOR_CONNECT_VERSION_2) {
$challenge_hidden = hex2bin($challenge_hidden);
$challenge_hidden = hash('sha256', $challenge_hidden, TRUE);
$challenge_visual = hash('sha256', $challenge_visual, TRUE);
}
else {
throw new LogicException('The challenge response version is unknown.');
}
$message = $challenge_hidden . $challenge_visual;
$address = NULL;
if (isset($response['address'])) {
$address = $response['address'];
}
$public_key = NULL;
if (isset($response['public_key'])) {
$public_key = $response['public_key'];
}
$signature = NULL;
if (isset($response['signature'])) {
$signature = $response['signature'];
}
$R = substr($signature, 2, 64);
$S = substr($signature, 66, 64);
$ecdsa = new \BitcoinPHP\BitcoinECDSA\BitcoinECDSA();
$prefix = "\x18Bitcoin Signed Message:\n";
$len = strlen($message);
$len = $ecdsa->numToVarIntString($len);
$data = $prefix . $len . $message;
$hash = $ecdsa->hash256($data);
$result = $ecdsa->checkSignaturePoints($public_key, $R, $S, $hash);
if($result) {
/*
$result = $ecdsa->getAddress($public_key);
if ($result == $address) {
$output = TRUE;
}
*/
$output = TRUE;
}
}
return $output;
}
/**
* Responsible for writing a TREZOR connect account mapping to the database.
*
* @param $record
*
* @return bool|int
*/
function trezor_connect_write_map($record) {
if (is_object($record)) {
$record = (array)$record;
}
if (!isset($record['created'])) {
$record['created'] = time();
}
$output = drupal_write_record('trezor_connect_map', $record);
return $output;
}
/**
* Returns a TREZOR connect mapping.
*
* @param $response
*/
function trezor_connect_mapping($response) {
$query = db_select('trezor_connect_map', 'm');
$query->fields('m');
$query->condition('public_key', $response['public_key']);
$output = $query->execute()->fetchAssoc();
return $output;
}
/**
* Deletes any TREZOR connect mappings associated with an account.
*
* @param $account
*/
function trezor_connect_delete_mapping($account) {
$query = db_delete('trezor_connect_map');
$query->condition('uid', $account->uid);
$query->execute();
}
/**
* Returns a TREZOR connect mapping associated with an account.
*
* @param $response
*/
function trezor_connect_account_mapping($account) {
$query = db_select('trezor_connect_map', 'm');
$query->fields('m');
$query->condition('uid', $account->uid);
$output = $query->execute()->fetchAssoc();
return $output;
}