-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathoesenc-export.py
More file actions
executable file
·477 lines (386 loc) · 14.1 KB
/
oesenc-export.py
File metadata and controls
executable file
·477 lines (386 loc) · 14.1 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
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
#!/usr/bin/python3
import os
import argparse
import enum
import sys
import struct
import random
import oesenc
import shutil
import logging
import subprocess
import asyncio
import signal, psutil
import string
import platform
import time
import xml.dom.pulldom
if platform.system() == 'Windows':
import win32file
import pywintypes
import winerror
logging.basicConfig(format='%(message)s')
log = logging.getLogger(__name__)
log.setLevel(logging.INFO)
class OeserverdCmd(enum.IntEnum):
CMD_READ_ESENC = 0
CMD_TEST_AVAIL = 1
CMD_EXIT = 2
CMD_READ_ESENC_HDR = 3
CMD_READ_OESU = 8
class ServiceType(enum.Enum):
Oeserver = 1
Oexserver = 2
class fifo_msg():
cmd = OeserverdCmd.CMD_READ_ESENC
fifo_name = ''
senc_name = ''
senc_key = ''
def pack(self):
msg = struct.Struct("=c256s256s256s")
data = msg.pack(bytes([self.cmd]),
self.fifo_name.encode(),
self.senc_name.encode(),
self.senc_key.encode())
return data
class fifo_msg_oexserverd():
cmd = OeserverdCmd.CMD_READ_ESENC
fifo_name = ''
senc_name = ''
senc_key = ''
def pack(self):
msg = struct.Struct("=c256s256s512s")
data = msg.pack(bytes([self.cmd]),
self.fifo_name.encode(),
self.senc_name.encode(),
self.senc_key.encode())
return data
def parseXmlList(path : str):
chartLevel = 0
fileName = ''
installKey = ''
charts = {}
xmlPath = []
doc = xml.dom.pulldom.parse(path)
for event, node in doc:
if event == xml.dom.pulldom.START_ELEMENT:
chartLevel += 1
xmlPath.append(node.tagName)
elif event == xml.dom.pulldom.END_ELEMENT:
if xmlPath == ['keyList', 'Chart']:
charts[fileName] = installKey
installKey = ''
fileName = ''
chartLevel -= 1
xmlPath.pop()
elif event == xml.dom.pulldom.CHARACTERS:
if xmlPath == ['keyList', 'Chart', 'FileName']:
fileName += node.data
elif xmlPath == ['keyList', 'Chart', 'RInstallKey']:
installKey += node.data
return charts
def locateService():
if platform.system() == 'Windows':
oexserverdDir = os.path.expandvars(r'%LOCALAPPDATA%\opencpn\plugins')
exe = shutil.which("oexserverd.exe", path=oexserverdDir)
if exe != None:
return exe
else:
return shutil.which("oeserverd.exe", path=r'C:\Program Files (x86)\OpenCPN\plugins\oesenc_pi')
else:
oexserverdDirs = [os.path.expandvars('$HOME/.var/app/org.opencpn.OpenCPN/bin'),
os.path.expandvars('$HOME/.local/bin'),
'/usr/local/bin',
'/usr/bin',
'/usr/lib/avnav/plugins/ocharts/bin/',
'/usr/lib/avnav/plugins/ochartsng']
for dir in oexserverdDirs:
exe = shutil.which('oexserverd', path=dir)
if exe != None:
return exe
oeserverDirs = ['/usr/local/bin/', '/usr/bin/']
for dir in oeserverDirs:
exe = shutil.which('oeserverd', path=dir)
if exe != None:
return exe
return None
def testPipe(pipeName):
try:
if platform.system() == 'Windows':
pipe = os.open(pipeName, os.O_RDWR)
else:
pipe = os.open(pipeName, os.O_WRONLY)
os.close(pipe)
except OSError as e:
return False
return True
async def startOeservd(path, pipe):
if testPipe(pipe):
log.info("Decryption service already running")
return True
if platform.system() == 'Windows':
index = pipe.rfind('\\')
pipeName = pipe[index + 1:]
cmd = '"{}" -p {}'.format(path, pipeName)
else:
cmd = path
proc = await asyncio.create_subprocess_shell(cmd, stdout=subprocess.DEVNULL)
for counter in range(0, 50):
await asyncio.sleep(0.1)
if testPipe(pipe):
log.info('Started decryption service: {}'.format(path))
return True
return False
def requestStopOeserverd(pipeName, serviceType):
pipe = os.open(pipeName, os.O_WRONLY)
if serviceType == ServiceType.Oexserver:
msg = fifo_msg_oexserverd()
elif serviceType == ServiceType.Oeserver:
msg = fifo_msg()
msg.cmd = OeserverdCmd.CMD_EXIT
data = msg.pack()
os.write(pipe, data)
os.close(pipe)
def createPipe(pipeName):
os.mkfifo(pipeName)
def readPipe(fifoName, outputFile):
readSize = 0
with open(fifoName, 'rb') as fifo, open(outputFile, 'wb') as file:
data = b''
while True:
newData = fifo.read()
if len(newData) == 0:
break
readSize += len(newData)
file.write(newData)
return readSize > 0
def writeFile(data, path):
with open(path, 'wb') as f:
f.write(data)
def requestReadChart(chartFile, pipe, returnPipe, key, serviceType):
with open(pipe, 'wb') as f :
extension = os.path.splitext(chartFile)[1]
if extension == ".oesu":
cmd = OeserverdCmd.CMD_READ_OESU
elif extension == ".oesenc":
cmd = OeserverdCmd.CMD_READ_ESENC
else:
log.error('Unknown file extension: {}'.format(chartFile))
return False
if serviceType == ServiceType.Oexserver:
msg = fifo_msg_oexserverd()
elif serviceType == ServiceType.Oeserver:
if extension == ".oesu":
log.error('Cannot decrypt oesu format with oeserverd')
return False
msg = fifo_msg()
msg.cmd = cmd
msg.fifo_name = returnPipe
msg.senc_name = chartFile
msg.senc_key = key
data = msg.pack()
f.write(data)
return True
return False
def exportChartFileWindows(chartFile, pipeName, outputFile, key, serviceType):
extension = os.path.splitext(chartFile)[1]
if extension == ".oesu":
cmd = OeserverdCmd.CMD_READ_OESU
elif extension == ".oesenc":
cmd = OeserverdCmd.CMD_READ_ESENC
else:
log.error('Unknown file extension: {}'.format(chartFile))
return False
if serviceType == ServiceType.Oexserver:
msg = fifo_msg_oexserverd()
msg.cmd = cmd
msg.senc_name = chartFile
msg.senc_key = key
elif serviceType == ServiceType.Oeserver:
if extension == ".oesu":
log.error('Cannot decrypt oesu format with oeserver')
return False
msg = fifo_msg()
msg.cmd = cmd
msg.senc_name = chartFile
msg.senc_key = key
data = msg.pack()
handle = win32file.CreateFile(pipeName,
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
0,
None,
win32file.OPEN_EXISTING,
0,
None)
win32file.WriteFile(handle, data)
output = os.open(outputFile, os.O_WRONLY | os.O_CREAT | os.O_BINARY)
readAnyData = False
while True:
try:
data = win32file.ReadFile(handle, 1024*1024)
os.write(output, data[1])
readAnyData = True
except pywintypes.error as e:
if e.args[0] == winerror.ERROR_PIPE_NOT_CONNECTED:
break
raise e
os.close(output)
win32file.CloseHandle(handle)
if not readAnyData:
return False
return True
def parseChartInfo(path):
chartInfo = {}
try:
userKeyLine = ''
with open(path, 'r') as file:
for line in file:
userKeyLine = line
parts = userKeyLine.split(':')
if len(parts) == 2:
chartInfo[parts[0].strip()] = parts[1].strip()
except FileNotFoundError as e:
print('Unable to open the file: {}'.format(path))
return chartInfo
def copyFilesAndDirs(basePath, input, destination):
for element in input:
elementPath = os.path.join(basePath, element)
if os.path.isdir(elementPath):
shutil.copytree(elementPath, os.path.join(destination, element))
else:
shutil.copy(elementPath, destination)
def unencryptChart(path, destination):
servicePath = locateService()
if servicePath == None:
log.fatal('Unable to locate decryption service')
return False
if 'oexserver' in servicePath:
serviceType = ServiceType.Oexserver
else:
serviceType = ServiceType.Oeserver
if platform.system() == 'Windows':
pipeName = '\\\\.\\pipe\\ocpn_pipe'
else:
if serviceType == ServiceType.Oexserver:
pipeName= '/tmp/OCPN_PIPEX'
else:
pipeName= '/tmp/OCPN_PIPE'
if not asyncio.run(startOeservd(servicePath, pipeName)):
log.fatal("Unable to start {}".format(servicePath))
return False
if os.path.isdir(destination):
log.fatal("Destination exists")
requestStopOeserverd(pipeName, serviceType)
return False
os.mkdir(destination)
log.info('Created destination directory')
listing = os.listdir(path)
chartFiles = [file for file in listing if file.endswith('.oesu') or file.endswith(".oesenc")]
otherFiles = set(listing) - set(chartFiles)
if not chartFiles:
log.warning('No oeseu or oesenc chart files in dir: {}'.format(path))
return False
if platform.system() != 'Windows':
possibleLetters = string.ascii_lowercase + string.ascii_uppercase + string.digits
returnPipeName = '/tmp/' + ''.join((random.choice(possibleLetters) for i in range(6)))
createPipe(returnPipeName)
log.info('Created random pipe {}'.format(returnPipeName))
totalString = '{}'.format(len(chartFiles))
numFailed = 0
chartInfoFile = os.path.join(path, 'Chartinfo.txt')
userKey = ''
if os.path.isfile(chartInfoFile):
chartInfo = parseChartInfo(os.path.join(path, 'Chartinfo.txt'))
if 'UserKey' in chartInfo:
log.info('Found UserKey in Chartinfo.txt')
userKey = chartInfo['UserKey']
xmlFiles = [file for file in listing if file.lower().endswith('.xml')]
chartKeys = {}
for xmlFile in xmlFiles:
chartKeys = parseXmlList(os.path.join(path, xmlFile))
if chartKeys:
log.info('Loaded chart keys from {}'.format(xmlFile))
break
log.info('Decrypting charts files:')
oesuFileFailed = False
for counter, chartFile in enumerate(chartFiles):
fullPathToChart = os.path.abspath(os.path.join(path, chartFile))
outputFile = os.path.join(destination, chartFile)
if chartFile.endswith('.oesu'):
baseName = os.path.splitext(chartFile)[0]
if baseName not in chartKeys:
log.warning('No key found for chart {}'.format(chartFile))
numFailed += 1
continue
chartKey = chartKeys[baseName]
elif chartFile.endswith('.oesenc'):
chartKey = userKey
else:
continue
if platform.system() == 'Windows':
if not exportChartFileWindows(fullPathToChart, pipeName, outputFile, chartKey, serviceType):
log.warning('{}: No data from encryption service'.format(chartFile))
oesuFileFailed |= fullPathToChart.endswith('.oesu')
numFailed += 1
continue
else:
if not requestReadChart(fullPathToChart, pipeName, returnPipeName, chartKey, serviceType):
log.warning('{}: No data from encryption service'.format(chartFile))
oesuFileFailed |= fullPathToChart.endswith('.oesu')
numFailed += 1
continue
if not readPipe(returnPipeName, outputFile):
numFailed += 1
continue
chart = oesenc.Oesenc(outputFile)
if chart.isValid():
text = 'OK'
else:
text = 'Failed to validate decrypted chart'
numFailed +=1
output = '{:>{width}} of {}: {} {}'.format(counter + 1, len(chartFiles), chartFile, text, width=len(totalString))
if chart.isValid():
log.info(output)
else:
log.warning(output)
numFailedText = ''
if numFailed > 0:
numFailedText = 'and {} failed '.format(numFailed)
if oesuFileFailed:
log.warning('Please ensure oeserverd is not running and try again')
requestStopOeserverd(pipeName, ServiceType.Oeserver)
else:
requestStopOeserverd(pipeName, serviceType)
if platform.system() != 'Windows':
os.remove(returnPipeName)
if numFailed == len(chartFiles):
log.warning('Failed to decrypt any chart')
return False
copyFilesAndDirs(path, otherFiles, destination)
log.info('Copied other files')
log.info('Decrypted {} charts {}to directory: {}'.format(len(chartFiles), numFailedText, destination))
return True
def handleDecrypt(args):
unencryptChart(args.input_dir, args.output_dir)
def handleInfo(args):
chartFile = args.chart_file
chart = oesenc.Oesenc(chartFile)
if chart.isValid():
chart.print()
log.info('Chart ok')
else:
log.fatal('Unable to parse chart')
parser = argparse.ArgumentParser(description='')
subparsers = parser.add_subparsers(help='Command')
decryptParser = subparsers.add_parser('decrypt', help='Decrypt chart directory', description='Decrypt all charts in a chart directory and output to another directory')
decryptParser.add_argument('-i', '--input-dir', help='Input chart director', required=True, type=str)
decryptParser.add_argument("-o", '--output-dir', help="Input chart director", required=True, type=str)
decryptParser.set_defaults(func=handleDecrypt)
infoParser= subparsers.add_parser('info', help='Print info about chart', description='Reads an already decrypted chart and prints its header data to stdout')
infoParser.add_argument('-c', '--chart-file', help='The path to the oesenc chart', required=True, type=str)
infoParser.set_defaults(func=handleInfo)
args = parser.parse_args()
if hasattr(args, 'func'):
args.func(args)
else:
parser.print_help()