-
Notifications
You must be signed in to change notification settings - Fork 7
/
make_test_data.py
762 lines (544 loc) · 25.4 KB
/
make_test_data.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
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
#
# David Schuetz
# November 2018
#
# https://github.com/dschuetz/1password
#
# Creates a bunch of test data to work with when exploring 1Password
# encryption. This way you don't have to risk writing your actual password
# database keys or passwords to bash_history or something like that.
#
# Can preserve certain values to guarantee that subsequent runs produce
# the same results. Change "PRE_LOAD" to {} to remove all those and
# (hopefully) produce a completely random set of data. The included
# pre-loaded data was used in generating my 2018 BSides Delaware talk
# and associated blog post series (darthnull.org).
#
#
# this is ugly. my apologies.
#
#
# I considered simply writing full-on sqlite databases that the client can
# then load, but that would require a lot of extra stuff that really doesn't
# matter to this experiment (just playing with encryption and vaults), and
# the client wouldn't be guaranteed to work anyway (since it wouldn't have a
# valid cloud-based account to connect back to).
#
#
# Optional Pre-Load Data
#
# Can pre-set different items to ensure that multiple runs produce the same
# results.
#
# Will be processed in turn by each individual generation routine.
#
PRE_LOAD = {
'muk_salt': 'cA4f6QY7wwUoclj74RMvUg==',
'srp_salt': 'OtQiDn4YnrTMoYXponFtfA==',
'vault1': {
'uuid': "r07y2eh9nj8vjf20g6a9vpbkv7",
'k': "N5UH1HxXJgtTSrvMHWStrEnuiHiq9Q1Vf064XlCYfgg=",
'iv': 'py0VvhU4S0lsVp3HCWPVBQ==',
'vault_uuid': 'ixaw6slq5k7c7d71lwzkh87qy1',
},
'keyset1': {
'uuid': 'qn8uimc4l7sofa26yivex24j7q',
'pub': {
'e': 'AQAB',
'n': 'xA6dAIu2_S9Ia_xRkodmvBv9w4pMyjE7FFAiXKTcQJS8d1RLkY82hwghBa6YK7V28_-S0Hfe2_NecesRMCpDf03kl1SClJkl8bJpJ0AwZFhvj6JO1JUZAj8o06OpgUCij_Jt8YSiu8bQIXgH5bEEkZ3oBx1OyozgqCo6JBa7cQVlv2LGV25YnqIbzOTof8YBZMNM0GuzPQQDxJUEB4ktmKekFjtDvHzAUmtMEgGYpbgXl4AmRAbHlYPpepSBplqXSrJfxVfEgftAJudjQsrMr_uVNX5TYGgFJDqUzkiXBXEUFy22GqcIArLLiOtvUwEU843wYpLtSPN-A20YLfSTCw',
'kty': 'RSA'
},
'priv': {
'iv': 'x_pZCisivs-aCINbqS4fLQ==',
'key': {
'e': 'AQAB',
'd': 'XcdvqfcqjGi1h5GloyVJKulotMPOf1iVHd5G0XG6ONnsXFfh3bpXJrfos8MT3rRqNcQmAbmUzDjZEDyUeCl_J8GmegxeeZ3X3Iiua6v0ecsjcdz9QAohcEWtza4XQlAcciZQGJqNDKzImXnErUXDHbQebGjEa3Z_b3DjZqfI-QH5DYDMh5W61L7Ky8_8kc54A9EtJupqtKZwYnBtazzLTcl82APkyQ71aN9kD-iO8qA3lAQGkUykRBa_8TF0tDCGgFfW7ijcGk06NGsYex9ir_n8fYZOP3LXahEMO5_3j4vIixmktFpI8IhtdQNvXqYir3JyB3WOmszr5XC4VasAYQ',
'n': 'xA6dAIu2_S9Ia_xRkodmvBv9w4pMyjE7FFAiXKTcQJS8d1RLkY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B79nkq3G8jBZyBmzrWzZFKWqfK3Td2rJ315UOXaLhW3Z6R7JGkujlr15oecs27jl1SITF4TPxZnATCLOH6UOf7VFFoZr8zTNo8mEoE_Yvd3KNfjiM9VWJctNHhbyzgH3KFHDeazm6a4hw8yEeMN8",
'kty': 'RSA'
}
}, 'sym': {
'k': 'E8nTamfBwcgmCR3cq5Z5b5ssaRvj2xMVc_10NdTZhkE=',
},
},
'mac_enc_login_iv': 'eaPYeKzI6X8xvRBonJ7Y2g==',
'win_mk': 'cgUlD73bdmMYKUtpv3CwHgnbI8/fTBumvhi5EKoRdbU=',
'win_mk_hmac': 'rRTU/gk49J/ygyjJP5yCGmVEB1O3iMxjaLE7V66FQms=',
'win_emk_iv': '6SxXzysYhFber930SflusA==',
'win_emk_padding': 'RpUVKSWc32nU7wrwhrlwDg==',
'win_enc_login_padding': 'McpQvMgog4rwvRTH',
'emk_iter': 588972,
'emk_salt': 'wcU1SabU3OEoVfmrmYqwTA==',
'win_enc_login_iv': '50C0aQfo36gxlOcoF75Npw==',
'local_vault': {
'salt': 'AXJZTNToCfbWGM61D5fg3w==',
'mk': {
'data': 'HdZBvq7CH6cUau1qtDhgqQqJRWeOfcB6olMi6FHMyojFDjUKVxj3bSgSwxjb3UA1I9PdXUw3HI3StQPvGXvrQUAh5FgAZ61fMTq+QeJ2EDKbCQ+96y3NR6bsLo7crRV5XtOSp9Zr/WhzPA1gJz60O6dhpJ8/NmYQe6O5YgLAdVGjrSbHf6g9mic/Pe7P5qYZIJrdlJ1Hjce45WUN6St7stRDggDeCX438b/IUrzfsTEBRVl++gWdQ4itif7GmdwMdHVpPDdLx6urNSKFQBbQXQxXQ7gH1NwgqsXTHidDeKeg6qaN99ayiuBiuOqcY0X1yemHzuMAuN1rxaubVWB0LA==',
'iv': 'mtn1wHVe5VbdZCfIhPU+lQ==',
'padding': '+i8dDdQkQGbkUIv3RlhVxQ==',
},
'ok': {
'data': '9azgZJxBKAeQdxdU3gjU6cM7LP2Kx/Ch9Pwfq41RxreyJwXv820oB5ljFlM1EvvY2SSfwd+B/Jgi07aTSpOw7Q==',
'iv': 'nNugIZecvwuWrncmby4V7w==',
'padding': 'TDFPhZ35Y4DYUxcsoZ0IuA==',
},
'enc_login': {
'iv': 'w+NRRE4uIpM3gLRsFhcvUw==',
'padding': 'NYoQ6QLE9g==',
}
},
'local_vault_data': {
'ik_iv': 'TLJVIK30PFzpdsuC+gfFTw==',
'ik_key': 'zWDXNOvfF9XZ50Jk773Q/8yDO/NwgCLoEvNLhqyJWfc=',
'ik_hmac': 'JfjcxD/BNf93c5BaUtN0ZCsiGm8dkIZaIKe6PJ8XTJw=',
'ik_padding': '+DlGQVEt295jIynBBdkOpw=='
}
}
DEBUG=1
SecretKey ='A3-ASWWYB-798JRY-LJVD4-23DC2-86TVM-H43EB'
MasterPassword ='update-clown-squid-bedpost'
Email='[email protected]'
p2c_muk = 100000
alg_muk = 'PBES2g-HS256'
p2s_muk = ''
p2c_srp = 100000
alg_srp = 'SRPg-4096'
p2s_srp = ''
sym_keys = {}
pri_keys = {}
pub_keys = {}
keysets = {}
vault_access = {}
vaults = {}
vault_kids = {}
items = {}
emk_iter = 588972
emk_slen = 16
opk_iter = 100000
from Cryptodome.Random import get_random_bytes
from Cryptodome.Cipher import AES
from Cryptodome.Protocol.KDF import HKDF
from Cryptodome.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_OAEP
from Cryptodome.Hash import SHA256, SHA512
from jwkest.jwk import RSAKey, load_jwks
from jwkest.jwe import JWE
from Crypto.PublicKey import RSA
from Cryptodome.Cipher import PKCS1_OAEP
import hashlib,hmac
import sys, base64, binascii, re, json, struct
import optestlib
def read_default(tag, decode=True):
cur_dict = PRE_LOAD
tags = tag.split('.')
for t in tags[0:-1]:
cur_dict = cur_dict.get(t)
if cur_dict == None:
break
if cur_dict == None:
return None
t = cur_dict.get(tags[-1])
if t == None:
return None
if decode:
return(optestlib.opb64d(t))
else:
return t
def main():
global p2s_muk, p2s_srp
optestlib.p_debug(" *** Generating Test Data ***")
optestlib.p_debug("\n\n**************************************************************")
optestlib.p_debug('* Deriving Master Unlock Key (MUK) (AES kid mp)')
p2s_muk = read_default('muk_salt') or get_random_bytes(16)
MUK = optestlib.compute_2skd(SecretKey, MasterPassword, Email,
p2s_muk, p2c_muk, alg_muk)
sym_keys['mp'] = MUK
optestlib.p_debug("\n\n**************************************************************")
optestlib.p_debug('* Deriving SRP-X')
p2s_srp = read_default('srp_salt') or get_random_bytes(16)
SRPx = optestlib.compute_2skd(SecretKey, MasterPassword, Email,
p2s_srp, p2c_srp, alg_srp)
sym_keys['srp-x'] = SRPx
optestlib.p_debug("\n\n**************************************************************")
optestlib.p_debug('* Generating keyset1')
keyset1_kid = gen_keyset('keyset1', 'mp')
optestlib.p_debug("\n\n**************************************************************")
optestlib.p_debug('* Generating keyset2')
keyset2_kid = gen_keyset('keyset2', keyset1_kid)
optestlib.p_debug("\n\n**************************************************************")
optestlib.p_debug('* Generating login detail records')
mac_login_data = gen_mac_login()
mac_login = enc_mac_login(keyset1_kid, mac_login_data)
optestlib.p_debug('\n* Generating Windows EMK')
emk = gen_emk()
optestlib.p_debug('\n* Generating Windows login details')
win_login = enc_win_login()
optestlib.p_debug('\n* Generating private vault unlock keys')
op_local_vault_keys = gen_local_vault_keys()
op_local_vault_login = gen_local_login(mac_login_data)
optestlib.p_debug("\n\n**************************************************************")
optestlib.p_debug('* Generating vaults')
vault1_kid = gen_vault_key('vault1', keyset1_kid)
gen_vault_entry('vault1', vault1_kid)
optestlib.p_debug("\n\n**************************************************************")
optestlib.p_debug('* Generating vault items')
uuid = read_default('items.1.uuid')
gen_item('items.1', 'vault1', uuid, title='My test!', url='https://example.com', user='user', password='password')
optestlib.p_debug('** Local private vault item')
old_vault_data = gen_old_vault_data()
print "\n-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n"
print " *** Generated Data ***\n"
print("MUK data:")
print(" Password: %s" % MasterPassword)
print(" Secret Key: %s" % SecretKey)
print(" Email: %s" % Email)
print(" p2s: %s" % optestlib.opb64e(p2s_muk))
print(" p2c: %d" % p2c_muk)
print(" alg: %s" % alg_muk)
print(" MUK: %s" % optestlib.opb64e(MUK))
print("\nSRP data")
print(" p2s: %s" % optestlib.opb64e(p2s_srp))
print(" p2c: %d" % p2c_srp)
print(" alg: %s" % alg_srp)
print(" SRP-X: %s" % optestlib.opb64e(SRPx))
print("\n\nMaster key (mk): %s" % optestlib.opb64e(sym_keys['mk']))
print("\n\nSymmetric Keys (kid, base64-encoded key):")
for k in sorted(sym_keys):
print(" %-26s %s" % (k, optestlib.opb64e(sym_keys[k])))
print("\n\nPublic Keys (kid, json dump of key):")
for k in sorted(pub_keys):
print(" %-26s %s\n" % (k, json.dumps(pub_keys[k], indent=4)))
print("\n\nPrivate Keys (kid, json dump of key):")
for k in sorted(pri_keys):
print(" %-26s %s\n" % (k, json.dumps(pri_keys[k], indent=4)))
print("\nKeyset 1:\n%s" % json.dumps(keysets[keyset1_kid], indent=4))
print("\nKeyset 2:\n%s" % json.dumps(keysets[keyset2_kid], indent=4))
print("\nmacOS enc_login:\n%s" % json.dumps(mac_login, indent=4))
print("\nEMK:\n%s" % emk)
print("\nWindows enc_login:\n%s" % win_login)
print("\nLocal vault key data:\n%s" % json.dumps(op_local_vault_keys, indent=4))
print("\nAccount enc_login encrypted for local vaults:\n%s" % json.dumps(op_local_vault_login, indent=4))
for vault in ['vault1']:
print("\nVault Data for %s:\n" % vault)
print(" * Vault access key\n%s" % (json.dumps(vault_access[vault])))
print(" * Vault attributes\n%s" % (json.dumps(vaults[vault])))
print("\nEncrypted vault items")
for item in items:
print json.dumps(items[item]), "\n"
print("\nLocal private vault item")
print json.dumps(old_vault_data)
#
# generate encrypted vault items
#
def gen_item(name, vault, uuid, title=None, url=None, user=None, password=None):
global items
ekid = vault_kids[vault]
overview = {'ainfo': 'account-name', 'title': title, 'url': url}
iv = read_default(name + 'ov_iv')
iv, enc_overview = optestlib.enc_aes_gcm(json.dumps(overview), sym_keys[ekid], iv)
ov_dat = {'data': optestlib.opb64e(enc_overview), 'iv': optestlib.opb64e(iv), 'kid': ekid,
'cty': 'b5+jwk+json', 'enc': 'A256GCM'}
details = {'fields': [ {'name': 'username', 'type': 'T', 'value': user},
{'name': 'password', 'type': 'P', 'value': password} ] }
iv = read_default(name + 'det_iv')
iv, enc_details = optestlib.enc_aes_gcm(json.dumps(details), sym_keys[ekid], iv)
det_dat = {'data': optestlib.opb64e(enc_details), 'iv': optestlib.opb64e(iv), 'kid': ekid,
'cty': 'b5+jwk+json', 'enc': 'A256GCM'}
out = {'vault': vault, 'item_num': name, 'overview': ov_dat, 'details': det_dat}
items[name] = out
#
# generate vault table entry for given vault name and kid
#
def gen_vault_entry(name, ekid):
global vaults
out = {'enc': 'A256GCM', 'kid': ekid, 'cty': 'b5+jwk+json'}
uuid = read_default(name + '.vault_uuid', decode=False) or gen_uuid()
attrs = {'uuid': uuid, 'name': 'Test vault: %s' % name, 'type': 'P', 'desc': 'unk-b64-blob', 'avatar': ''}
iv = read_default(name + '.iv')
iv, enc_attrs = optestlib.enc_aes_gcm(json.dumps(attrs), sym_keys[ekid], iv=iv)
out['data'] = optestlib.opb64e(enc_attrs)
out['iv'] = optestlib.opb64e(iv)
vaults[name] = out
#
# generate a vault entry for local vaults (old style)
#
def gen_old_vault_data():
out = {}
m_key = sym_keys['opv-mk']
m_hmac = sym_keys['opv-mk-hmac']
o_key = sym_keys['opv-ok']
o_hmac = sym_keys['opv-ok-hmac']
o_data = '{"title":"Vault entry for local private vaults","url":"www.example.com","ainfo":"-","ps":75}'
o_data_enc = optestlib.encrypt_opdata(o_data, o_key, o_hmac)
out['overview_data'] = optestlib.opb64e(o_data_enc)
ik_iv = read_default('priv_vault_data.ik_iv') or get_random_bytes(16)
ik_key = read_default('priv_vault_data.ik_key') or get_random_bytes(32)
ik_hmac = read_default('priv_vault_data.ik_hmac') or get_random_bytes(32)
ik_padding = read_default('priv_vault_data.ik_padding')
optestlib.p_data('Item key IV', ik_iv)
optestlib.p_data('Item key', ik_key)
optestlib.p_data('Item HMAC key', ik_hmac)
item_key_data = optestlib.encrypt_tag_cbc(ik_key + ik_hmac, ik_iv, m_key, m_hmac)
check = optestlib.decrypt_verify_cbc(item_key_data, m_key, m_hmac)
out['key_data'] = optestlib.opb64e(item_key_data)
item_data = '{"fields": [ { "id": "OldPassword;opid=__2", "name": "OldPassword", "type": "P", "value": "notagoodpassword" }, { "designation": "password", "id": "NewPassword;opid=__3", "name": "NewPassword", "type": "P", "value": "OldSk00lRulzFTW!" } ] }'
item_data_enc = optestlib.encrypt_opdata(item_data, ik_key, ik_hmac, iv=ik_iv, padding=ik_padding)
out['data'] = optestlib.opb64e(item_data_enc)
return out
#
# generate a vault key and store in vault access
#
def gen_vault_key(name, ekid):
global vault_access, vault_kids
optestlib.p_debug('** Generating access keys for %s' % name)
out= {"enc":"RSA-OAEP","kid":ekid,"cty":"b5+jwk+json"}
new_kid = read_default(name + '.uuid', decode=False)
new_key = read_default(name + '.k')
kid, sym_key = gen_sym_key(ekid, new_kid=new_kid, k=new_key)
out['data'] = sym_key
vault_access[name] = sym_key
vault_kids[name] = kid
return kid
def gen_local_vault_keys():
global sym_keys
optestlib.p_debug('\n** Generating MasterKey (MK) and OverviewKey (OK) (OnePassword local private vaults)')
salt = read_default('local_vault.salt') or get_random_bytes(16)
mkd = read_default('local_vault.mk.data') or get_random_bytes(256)
mk_iv = read_default('local_vault.mk.iv')
mk_padding = read_default('local_vault.mk.padding')
okd = read_default('local_vault.ok.data') or get_random_bytes(64)
ok_iv = read_default('local_vault.ok.iv')
ok_padding = read_default('local_vault.ok.padding')
data = optestlib.gen_local_vault_keys(MasterPassword, salt,
mkd, mk_iv, mk_padding, okd, ok_iv, ok_padding)
sym_keys['opv-mk'] = data['master_key']
sym_keys['opv-mk-hmac'] = data['master_key_hmac']
sym_keys['opv-ok'] = data['overview_key']
sym_keys['opv-ok-hmac'] = data['overview_key_hmac']
enc_master_key = data['enc_master_key_data']
enc_overview_key = data['enc_overview_key_data']
profile_data = {'salt': optestlib.opb64e(salt), 'iterations': opk_iter,
'overview_key_data': optestlib.opb64e(enc_overview_key),
'master_key_data': optestlib.opb64e(enc_master_key)}
return profile_data
def gen_local_login(mac_login):
iv = read_default('local_vault.enc_login.iv')
padding = read_default('local_vault.enc_login.padding')
op_mk = sym_keys['opv-mk']
op_mk_hmac = sym_keys['opv-mk-hmac']
enc_login = optestlib.encrypt_opdata(json.dumps(mac_login), op_mk, op_mk_hmac,
iv=iv, padding=padding)
account_data = {'enc_login': optestlib.opb64e(enc_login)}
return account_data
#
# generate windows EMK block
#
def gen_emk():
global sym_keys
optestlib.p_debug('\n** Generating Encrypted Master Key (EMK) block')
win_mk = read_default('win_mk') or get_random_bytes(32)
sym_keys['win-mk'] = win_mk
win_mk_hmac = read_default('win_mk_hmac') or get_random_bytes(32)
sym_keys['win-mk-hmac'] = win_mk_hmac
optestlib.p_data('New MK', win_mk)
optestlib.p_data('New MK HMAC Key', win_mk_hmac)
emk_salt = read_default('emk_salt') or get_random_bytes(emk_slen)
raw_derived_key = hashlib.pbkdf2_hmac('sha512', MasterPassword, emk_salt, emk_iter, 64)
enc_key = raw_derived_key[0:32]
enc_hmac_key = raw_derived_key[32:64]
optestlib.p_data('MP-derived key', enc_key)
optestlib.p_data('MP-derived HMAC key', enc_hmac_key)
iv = read_default('win_emk_iv')
padding = read_default('win_emk_padding')
op_msg = optestlib.encrypt_opdata(win_mk + win_mk_hmac, enc_key, enc_hmac_key,
iv=iv, padding=padding)
emk = struct.pack('<I', emk_iter) # iterations
emk += struct.pack('<I', emk_slen) # salt length
emk += emk_salt
emk += struct.pack('<I', len(op_msg))
emk += op_msg
optestlib.p_data('Final EMK block', emk)
return optestlib.opb64e(emk)
#
# encrypt windows login data
#
def enc_win_login():
optestlib.p_debug('\n** Windows login details for accounts table')
data = json.dumps({'accountKey': SecretKey, 'password': MasterPassword})
iv = read_default('win_enc_login_iv')
padding = read_default('win_enc_login_padding')
op_msg = optestlib.encrypt_opdata(data,
sym_keys['win-mk'], sym_keys['win-mk-hmac'], iv=iv, padding=padding)
return optestlib.opb64e(op_msg)
#
#
#
# generate account login data structure for macOS client
#
def gen_mac_login():
optestlib.p_debug('\n** Mac login details for accounts table')
enc_login = {'email': Email, 'personalKey': SecretKey}
muk = {'k': optestlib.opb64e(sym_keys['mp']), 'key_ops': ['encrypt','decrypt'],
'alg':'A256GCM', 'ext':True, 'key':'oct', 'kid':'mp'}
srp = {'hexX': optestlib.opb64e(sym_keys['srp-x']),
'params': {'method': alg_srp, 'iterations': p2c_srp, 'alg': alg_muk,
'salt': optestlib.opb64e(p2s_srp)}}
enc_login['masterUnlockKey'] = muk
enc_login['SRPComputedXDictionary'] = srp
optestlib.p_str("mac enc_login contents:", json.dumps(enc_login, indent=4))
return enc_login
#
# encrypt account login data for macOS client
#
def enc_mac_login(kid, mac_login_data):
out_pt = json.dumps(mac_login_data)
iv = read_default('mac_enc_login_iv')
iv, ct = optestlib.enc_aes_gcm(out_pt, sym_keys[kid], iv=iv)
out = {'iv': optestlib.opb64e(iv), 'data': optestlib.opb64e(ct), 'enc': 'A256GCM',
'cty': 'b5+jwk+json', 'kid': kid}
optestlib.p_str('Encrypted macOS enc_login:', out)
return out
#
# generate a keyset
# * enc_sym_key
# * enc_pri_key
# * pub_key
#
# Encrypt the keyset (the sym key) with the key identified by ekid
# If ekid = 'mp' then it's the primary keyset, and add the global 2SKD params
#
def gen_keyset(name, ekid):
optestlib.p_debug('** Generating %s - encrypted by %s' % (name, ekid))
out = {'encrypted_by': ekid}
iv = None
try:
if ekid == 'mp':
iv = read_default(name + '.sym.iv')
new_kid = read_default(name + '.uuid', decode=False)
new_key = read_default(name + '.sym.k')
kid, sym_key = gen_sym_key(ekid, new_kid=new_kid,
iv=iv, k=new_key)
new_pub = read_default(name + '.pub', decode=False)
new_priv = read_default(name + '.priv', decode=False)
rsa_pub, rsa_priv = gen_rsa_key(kid, pub=new_pub, priv=new_priv)
except:
kid, sym_key = gen_sym_key(ekid)
rsa_pub, rsa_priv = gen_rsa_key(kid)
if ekid == 'mp':
sym_keys['mk'] = sym_keys[kid]
sym_key['p2s'] = optestlib.opb64e(p2s_muk)
sym_key['p2c'] = p2c_muk
sym_key['alg'] = alg_muk
out['uuid'] = kid
out['enc_sym_key'] = sym_key
out['enc_pri_key'] = rsa_priv
out['pub_key'] = rsa_pub
keysets[kid] = out
return kid
def gen_rsa_key(ekid, pub=None, priv=None):
if priv == None:
new_priv = RSA.generate(2048)
jwk_priv = RSAKey(key=new_priv).to_dict()
iv = None
else:
iv = optestlib.opb64d(priv['iv'])
jwk_priv = priv['key']
if pub == None:
new_pub = new_priv.publickey()
jwk_pub = RSAKey(key=new_pub).to_dict()
else:
jwk_pub = pub
jwk_priv['alg'] = 'RSA-OAEP'
jwk_priv['key_ops'] = ['decrypt']
jwk_priv['kid'] = ekid
jwk_pub['key_ops'] = ['encrypt']
jwk_pub['alg'] = 'RSA-OAEP'
jwk_pub['ext'] = True
jwk_pub['kid'] = ekid
optestlib.p_str("New Private key", json.dumps(jwk_priv, indent=4))
pri_keys[ekid] = jwk_priv
pub_keys[ekid] = jwk_pub
key_dat_str = json.dumps(jwk_priv)
optestlib.p_debug('\n*** Encrypting pri key with AES kid %s' % ekid)
iv, ct = optestlib.enc_aes_gcm(key_dat_str, sym_keys[ekid], iv=iv)
optestlib.p_data('IV', iv, dump=False)
optestlib.p_data('KEY', sym_keys[ekid], dump=False)
optestlib.p_data('Ciphertext', ct, dump=False)
priv_out = {'kid': ekid, 'cty': 'b5+jwk+json', 'enc': 'A256GCM',
'data': optestlib.opb64e(ct), 'iv': optestlib.opb64e(iv)}
optestlib.p_str("New Public key", json.dumps(jwk_pub, indent=4))
return jwk_pub, priv_out
def gen_sym_key(ekid, new_kid=None, iv=None, k=None):
out = {'kid': ekid, 'cty': 'b5+jwk+json'}
if new_kid != None:
kid = new_kid
else:
kid = gen_uuid()
if k != None:
new_key = k
else:
new_key = get_random_bytes(32)
key_dat = {'alg': 'A256GCM', 'ext': True, 'key_ops': ['decrypt', 'encrypt'], 'kty': 'oct', 'kid': kid}
key_dat['k'] = optestlib.opb64e(new_key)
key_dat_str = json.dumps(key_dat)
optestlib.p_str("New symmetric key",json.dumps(key_dat, indent=4))
sym_keys[kid] = new_key
if ekid == 'mp':
# sym_keys['mk'] = new_key
optestlib.p_debug('\n*** Encrypting sym key with AES kid %s' % ekid)
iv, ct = optestlib.enc_aes_gcm(key_dat_str, sym_keys[ekid], iv=iv)
optestlib.p_data('IV', iv, dump=False)
optestlib.p_data('KEY', sym_keys[ekid], dump=False)
optestlib.p_data('Ciphertext', ct, dump=False)
out['iv'] = optestlib.opb64e(iv)
out['data'] = optestlib.opb64e(ct)
out['enc'] = 'A256GCM'
else: # only the primary sym_key is itself AES encrypted, rest by RSA
optestlib.p_debug('\n*** Encrypting sym key with RSA kid %s\n' % ekid)
jwkj = '{"keys": [%s]}' % json.dumps(pub_keys[ekid])
jwk = load_jwks(jwkj)[0]
optestlib.p_str('Public key e:', jwk.e)
optestlib.p_str('Public key n:', jwk.n)
RSA_Key = RSA.construct((jwk.n, jwk.e))
C = PKCS1_OAEP.new(RSA_Key)
ct = C.encrypt(key_dat_str)
out['enc'] = 'RSA-OAEP'
out['data'] = optestlib.opb64e(ct)
optestlib.p_debug('')
optestlib.p_data('RSA-OAEP ciphertext', ct, dump=False)
return kid, out
def gen_uuid():
out = ''
for rc in get_random_bytes(26):
c = ord(rc) % 36
if c < 10:
ch = chr(48+c)
else:
ch = chr(97+c-10)
out += ch
return out
if __name__ == '__main__':
main()