forked from LedgerHQ/app-ssh-agent
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgetPublicKey.py
77 lines (68 loc) · 2.26 KB
/
getPublicKey.py
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
#!/usr/bin/env python
"""
*******************************************************************************
* Ledger Blue
* (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.
********************************************************************************
"""
from ledgerblue.comm import getDongle
from ledgerblue.commException import CommException
import argparse
import struct
import base64
KEY_HEADER = "ecdsa-sha2-nistp256"
CURVE_NAME = "nistp256"
KEY_HEADER_ED25519 = "ssh-ed25519"
def parse_bip32_path(path):
if len(path) == 0:
return ""
result = ""
elements = path.split('/')
for pathElement in elements:
element = pathElement.split('\'')
if len(element) == 1:
result = result + struct.pack(">I", int(element[0]))
else:
result = result + struct.pack(">I", 0x80000000 | int(element[0]))
return result
parser = argparse.ArgumentParser()
parser.add_argument('--path', help="BIP 32 path to retrieve")
parser.add_argument("--ed25519", help="Use Ed25519 curve", action='store_true')
args = parser.parse_args()
if args.path == None:
args.path = "44'/535348'/0'/0/0"
if args.ed25519:
p2 = "02"
keyHeader = KEY_HEADER_ED25519
else:
p2 = "01"
keyHeader = KEY_HEADER
donglePath = parse_bip32_path(args.path)
apdu = "800200" + p2
apdu = apdu.decode('hex') + chr(len(donglePath) + 1) + chr(len(donglePath) / 4) + donglePath
dongle = getDongle(True)
result = dongle.exchange(bytes(apdu))
key = str(result[1:])
blob = struct.pack(">I", len(KEY_HEADER)) + keyHeader
if args.ed25519:
keyX = bytearray(key[0:32])
keyY = bytearray(key[32:][::-1])
if ((keyX[31] & 1)<>0):
keyY[31] |= 0x80
key = str(keyY)
else:
blob += struct.pack(">I", len(CURVE_NAME)) + CURVE_NAME
blob += struct.pack(">I", len(key)) + key
print keyHeader + " " + base64.b64encode(blob)