-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlib.py
More file actions
306 lines (264 loc) · 7.32 KB
/
lib.py
File metadata and controls
306 lines (264 loc) · 7.32 KB
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
"""
Common routines
"""
from base64 import urlsafe_b64encode, urlsafe_b64decode
from json import dumps as jsonEnc, loads as jsonDec
from time import sleep
from os.path import exists as pathExists
from subprocess import PIPE, STDOUT
import subprocess
##
# Manage manifest files
class JsonSerializable:
"""
Classes that can be (de)serialized (from) to JSON.
"""
def _getContents(self):
"""
Subclasses implement this to retrieve a dictionary that will be serialized
to JSON.
"""
pass
def write(self):
"""
Writes a dictionary to a file in JSON format.
"""
with open(self.path, 'wt') as f:
f.write(jsonEnc(self._getContents(), sort_keys=True, indent=3))
def _readJson(self):
"""
Reads and decodes a JSON file.
"""
with open(self.path, 'rt') as f:
return jsonDec(f.read())
class ShareManifest(JsonSerializable):
"""
Stores information about encrypted share files so that they can be
reconstructed.
"""
FILENAME = "shares-manifest.json"
def __init__(self, directory):
"""
Creates a new object. Callers should use .new() and .load() to
create new manifests or read them from a file, respectively.
"""
self.path = f"{directory}/{ShareManifest.FILENAME}"
def new(directory, n, k):
"""
Creates a new secret share manifest that can be written
to the given directoryectory.
"""
m = ShareManifest(directory)
m.n = n
m.k = k
m.shares = {}
return m
def load(directory):
"""
Reads an existing secret share manifest from the specified
directory.
"""
m = ShareManifest(directory)
m.path = f"{directory}/{ShareManifest.FILENAME}"
# Read the manifest file and store entries as object attributes
manifest = m._readJson()
m.k = manifest["k"]
m.n = manifest["n"]
m.shares = manifest["shares"]
return m
def addShare(self, coeff, encryptedShareFile,
pubkeyFilename, pubkeyFingerprint):
"""
Adds a new encrypted secret share entry to this manifest.
"""
# TODO: Index shares by pubkey fingerprints
# But for development we want to re-use a single device
self.shares[pubkeyFilename] = {
"coeff": coeff,
"encryptedShareFile": encryptedShareFile,
"pubkeyFilename": pubkeyFilename,
"pubkeyFingerprint": pubkeyFingerprint
}
def _getContents(self):
return {
"k": self.k,
"n": self.n,
"shares": self.shares
}
class DeviceManifest(JsonSerializable):
"""
Stores info about individual devices (Yubikeys) managed by Q.
"""
FILENAME = "device-manifest.json"
PUBKEY_BASENAME = "device-{number}.pubkey"
def __init__(self, directory):
self.path = f"{directory}/{self.FILENAME}"
# Read the manifest if one exists
if pathExists(self.path):
self.deviceTable = self._readJson()
else:
self.deviceTable = {}
def load(directory):
"""
Load a device manifest file.
"""
return DeviceManifest(directory)
def findDevice(self, pubkeyFingerprint):
"""
Finds the device entry that matches a pubkeyFingerprint.
"""
for device in self.devices():
if device["pubkeyFingerprint"] == pubkeyFingerprint:
return device
def devices(self):
"""
Retrieves a list (of dictionaries) of all devices.
"""
return list(self.deviceTable.values())
def newDevice(self):
"""
Generates a unique device number and pubkey filename for a new device.
"""
dn = str(self._findUnusedDeviceNumber())
pubkeyFilename = self.PUBKEY_BASENAME.format(number=dn)
return dn, pubkeyFilename
def addDevice(self, deviceNumber, pubkeyFilename, pubkeyFingerprint,
adminPin, operationsPin, managementKey):
"""
Adds a new device to this manifest and re-writes the file.
@returns pubkeyfilename
"""
# TODO: Index these by device fingerprint to avoid duplicate devices
# and enable faster lookup
# But for debug we want to use the same yubikey mutiple times.
self.deviceTable[deviceNumber] = {
"number": deviceNumber,
"pubkeyFilename": pubkeyFilename,
"pubkeyFingerprint": pubkeyFingerprint,
"operationsPin": operationsPin,
"adminPin": adminPin,
"managementKey": managementKey
}
def _getContents(self):
return self.deviceTable
def _findUnusedDeviceNumber(self):
if len(self.deviceTable) == 0:
return 1
else:
return max([int(d["number"]) for d in self.deviceTable.values()])+1
##
# Utilities
def b64enc(x):
"""
Encode a bytes object in base64 and return a str object.
"""
return toStr(urlsafe_b64encode(x))
def b64dec(x):
"""
Decode a base64 str and return a bytes object.
"""
return toBytes(urlsafe_b64decode(x))
def toStr(b):
"""
Converts a utf-8 bytes object to a string.
"""
if type(b) == str:
return b
else:
return b.decode("utf-8")
def toBytes(b):
"""
Converts a string to a bytes object by encoding it as utf-8.
"""
if type(b) == str:
return b.encode("utf-8")
else:
return b
# TODO: Refactor into a single run()
# with optional input and maybe even detect the type and apply
# encoding automatically
# How to guess at the output?
# Maybe decodeStdout=True which reads stdout as text unless
# otherwise specified.
# def run(cmd, echo, printErrorMsg, ...):
# if echo:
# print()
# if input:
# _runWithStdin
# else:
# _run()
#
# if not ok and printErrorMSg()
# if not echo:
# echoNow()
# print(error)
# ...
def run(cmd, echo=True, printErrorMsg=True):
"""
Runs @cmd and captures stdout.
"""
cmdString = " ".join(cmd)
if echo:
print(cmdString)
result = subprocess.run(cmd, stdout=PIPE, stderr=PIPE)
ok = (result.returncode == 0)
# Handle any errors
if ok:
output = result.stdout
else:
output = toStr(result.stderr)
if printErrorMsg:
print(f"Error running command: {cmdString}\n{output}")
return ok, output
def runWithStdin(cmd, echo=True, inputString=None, inputBytes=None, printErrorMsg=True):
"""
Runs @cmd, passes the string @inputString to the process (or the bytes
object @inputBytes).
@returns (ok, output)
"""
# We must have exactly one of inputString, inputBytes set.
if not (inputBytes or inputString):
raise ValueError("inputString and inputBytes cannot both be None")
if (inputString and inputBytes):
raise ValueError("Only one of inputString and inputBytes can be set")
if echo:
print(" ".join(cmd))
# Convert our string to bytes if it was provided
if inputString:
inputBytes = toBytes(inputString)
proc = subprocess.Popen(
cmd,
stdin=PIPE,
stdout=PIPE,
stderr=STDOUT)
# Write the plaintext to STDIN
proc.stdin.write(inputBytes)
proc.stdin.close()
# Wait for openssl to finish
while proc.returncode is None:
proc.poll()
sleep(1)
# Read and close stdout
output = proc.stdout.read()
proc.stdout.close()
# Handle any errors
ok = proc.returncode == 0
if not ok and printErrorMsg:
cmdString = " ".join(cmd)
print(f"Error running command: {cmdString}\n{toStr(output)}")
return ok, output
def exitOnFail(ok, msg=None):
if not ok and msg:
print(msg)
if not ok:
exit(1)
def strOrNone(x):
"""
Converts numeric values to strings, but leaves None as None.
"""
if x is None:
return None
if type(x) == int:
return str(x)
else:
return x