-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadkeys.py
executable file
·329 lines (286 loc) · 8.91 KB
/
loadkeys.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
#!/usr/bin/env python3
'''
gpg/card> admin
Admin commands are allowed
gpg/card> passwd
gpg: OpenPGP card no. D2760001240102010006061158870000 detected
1 - change PIN
2 - unblock PIN
3 - change Admin PIN
4 - set the Reset Code
Q - quit
Your selection? 1
PIN changed.
1 - change PIN
2 - unblock PIN
3 - change Admin PIN
4 - set the Reset Code
Q - quit
Your selection? q
gpg/card> q
'''
import re
import sys
import pexpect
import os
from random import randint
from getpass import getpass
from time import sleep
import datetime
oldpin = '123456'
newpin = '123456'
oldadminpin = '12345678'
adminpin = oldadminpin
newadminpin = str(randint(1,99999999)).zfill(8)
cardserial = None
try:
passphrase = os.environ['passphrase']
keyid = os.environ['KEYID']
lname = os.environ['lname']
fname = os.environ['fname']
email = os.environ['email']
backupdir = os.environ['BACKUPDIR']
except KeyError:
print("Could not find requirement environment variables...")
print("Please source the key-generated environment variables: '. keygen.env'")
exit(1)
# LOG FILE
logfile = open('%s/loadkeys.log'%(backupdir),'wb')
def logtitle(title):
logfile.write("\n\n\n##################################################################\n".encode())
logfile.write(("### %s\n"%title).encode())
logfile.write("##################################################################\n".encode())
logtitle("TRNG check: infnoise")
p = pexpect.pty_spawn.spawn('infnoise -l')
p.logfile = logfile
ret = p.expect('ID:')
ret = p.read()
if (re.sub(".*Serial:[ ]?[']?",'',str(ret.strip())) != ""):
print("Please disconnect your Infnoise TRNG, as it prevents Yubikey programming")
exit(0)
#cat >> /offline/"${lname},${fname}.csv" <<EOF
#"${email}","${lname}","${fname}",${KEYID},${adminpin},${serialnum},${date}
logtitle("New Admin Pin Code")
logfile.write(("AdminPin:%s"%newadminpin).encode())
# reset the card first
logtitle("ykman card reset")
p = pexpect.pty_spawn.spawn('ykman openpgp reset')
p.logfile = logfile
ret = p.expect('restore factory settings')
p.sendline('y')
ret = p.expect(['Success!','Error: No YubiKey detected!'])
if ret == 0:
print("Yubikey reset successfully")
else:
print("Couldn't reset openpgp... is this a bare metal host and is the yubikey present?")
# set the touch policies
print("Hardening touch policies")
logtitle("ykman touch policies to fixed - sig")
p = pexpect.pty_spawn.spawn('ykman openpgp touch --admin-pin %s -f sig fixed'%(adminpin))
p.logfile = logfile
ret = p.expect(['Touch policy successfully set','Error: No YubiKey detected!'])
if ret == 0:
print("Successfully Updated a Yubikey touch policy")
else:
print("Failed to update a Yubikey touch policy")
logtitle("ykman touch policies to fixed - encrypt")
p = pexpect.pty_spawn.spawn('ykman openpgp touch --admin-pin %s -f enc fixed'%(adminpin))
p.logfile = logfile
ret = p.expect(['Touch policy successfully set','Error: No YubiKey detected!'])
if ret == 0:
print("Successfully Updated a Yubikey touch policy")
else:
print("Failed to update a Yubikey touch policy")
logtitle("ykman touch policies to fixed - auth")
p = pexpect.pty_spawn.spawn('ykman openpgp touch --admin-pin %s -f aut fixed'%(adminpin))
p.logfile = logfile
ret = p.expect(['Touch policy successfully set','Error: No YubiKey detected!'])
if ret == 0:
print("Successfully Updated a Yubikey touch policy")
else:
print("Failed to update a Yubikey touch policy")
'''
ykman openpgp touch --admin-pin 12345678 -f sig on
ykman openpgp touch --admin-pin 12345678 -f enc on
ykman openpgp touch --admin-pin 12345678 -f aut on
'''
testpin = getpass('Please Enter your *new* PIN: ')
testpin2 = getpass('Please Reenter your *new* PIN: ')
if testpin == testpin2:
newpin = testpin
logtitle("gpg card-status")
print("Checking card status...")
# we run this twice... not a typo....
p = pexpect.pty_spawn.spawn('gpg --card-status')
p.logfile = logfile
p.expect(['Serial number \.+: ','card not available'])
if ret == 1:
print("No Yubikey/OpenPGP card found, exiting")
exit(0)
elif ret == 0:
ret = p.read()
cardserial = re.sub("\\\\r.*",'',str(ret))
cardserial = re.sub("[^[0-9]]*",'',cardserial)
print("Found Yubikey/OpenPGP card (#%s)"%(cardserial))
sleep(0.2)
# we run this twice... not a typo....!!!
p = pexpect.pty_spawn.spawn('gpg --card-status')
p.logfile = logfile
p.expect(['Serial','card not available'])
if ret == 1:
print("No Yubikey/OpenPGP card found, exiting")
exit(0)
elif ret == 0:
ret = p.read()
#cardserial = re.sub(".*Serial number \.+: ",'',str(ret.strip()))
#print("Found Yubikey/OpenPGP card (#%s)"%(cardserial))
# set the new pins
logtitle("gpg card-edit new pins")
print("Programming new pin codes into Yubikey/OpenPGP card")
p = pexpect.pty_spawn.spawn('gpg --card-edit --pinentry-mode loopback')
p.logfile = logfile
p.expect('gpg/card>')
p.sendline('admin')
p.expect('are allowed')
p.sendline('passwd')
ret = p.expect(['Your selection?','No such device'])
if ret == 1:
print('Device not connected!')
p.sendline('q')
p.expect('gpg/card>')
p.sendline('q')
sys.exit(1)
p.sendline('1')
p.expect('Enter passphrase')
p.sendline(oldpin)
p.sendline(newpin)
p.sendline(newpin)
p.expect('PIN changed')
p.expect('Your selection?')
p.sendline('3')
p.expect('Enter passphrase')
p.sendline(oldadminpin)
p.sendline(newadminpin)
p.sendline(newadminpin)
p.expect('PIN changed')
p.sendline('q')
adminpin = newadminpin
p.expect('gpg/card>')
print("Programming user identity (%s, %s: %s)."%(lname,fname,email))
p.sendline('name')
p.expect('Cardholder\'s surname:')
p.sendline(lname)
p.expect('Cardholder\'s given name:')
p.sendline(fname)
ret = p.expect(['gpg/card','Enter passphrase'])
if ret == 1:
p.sendline(adminpin)
p.expect('gpg/card>')
p.sendline('lang')
p.expect('Language preferences:')
p.sendline('en')
ret = p.expect(['gpg/card','Enter passphrase'])
if ret == 1:
p.sendline(adminpin)
p.expect('gpg/card>')
p.sendline('login')
p.expect('account name')
p.sendline(email)
p.expect('gpg/card>')
p.sendline('q')
'''
gpg> key 1
sec rsa4096/0x8191ACCD34BE4A72
created: 2019-01-10 expires: never usage: SCEA
trust: ultimate validity: ultimate
ssb* rsa4096/0xB412313296D2E621
created: 2019-01-10 expires: never usage: S
ssb rsa4096/0xD7A205F011EBE5BC
created: 2019-01-10 expires: never usage: E
ssb rsa4096/0xC3FFBB7859ADA9AD
created: 2019-01-10 expires: never usage: A
[ultimate] (1). b, a (Automatic Boot-GPG Generated (v1)) <a@b>
gpg> keytocard
Please select where to store the key:
(1) Signature key
(3) Authentication key
Your selection? 1
'''
# program in the keys
print("Saving the Private key into Yubikey/Openpgp card")
logtitle("gpg edit-key load keys to yubikey")
p = pexpect.pty_spawn.spawn('gpg --pinentry-mode loopback --edit-key %s'%(keyid))
p.logfile = logfile
p.expect('gpg>')
p.sendline('key 1')
p.expect('gpg>')
p.sendline('keytocard')
ret = p.expect(['Your selection?','No such device'])
if ret == 1:
print('Device not connected!')
p.sendline('q')
sys.exit(1)
p.sendline('1')
ret = p.expect(['Enter passphrase:','Replace existing key?'])
if ret==1:
p.sendline('y')
p.expect('Enter passphrase:')
p.sendline(passphrase)
ret = p.expect(['Enter passphrase:','gpg>'])
if ret==0:
p.sendline(adminpin)
ret == p.expect(['Enter passphrase:','gpg>','SCEA'])
if ret == 0:
p.sendline(adminpin)
p.expect('gpg>')
p.sendline('key 1')
print(" key 1... Signing")
p.expect('gpg>')
p.sendline('key 2')
p.expect('gpg>')
p.sendline('keytocard')
p.expect('Your selection?')
p.sendline('2')
ret = p.expect(['Enter passphrase:','Replace existing key?'])
if ret==1:
p.sendline('y')
p.expect('Enter passphrase:')
p.sendline(passphrase)
ret == p.expect(['Enter passphrase:','gpg>','SCEA'])
if ret == 0:
p.sendline(adminpin)
ret == p.expect(['Enter passphrase:','gpg>','SCEA'])
## if NOT gpg, always send the adminpin?... input looks like '\r \r '
if ret == 0:
p.sendline(adminpin)
p.expect('gpg>')
p.sendline('key 2')
print(" key 2... Encryption")
p.expect('gpg>')
p.sendline('key 3')
p.expect('gpg>')
p.sendline('keytocard')
p.expect('Your selection?')
p.sendline('3')
ret = p.expect(['Enter passphrase:','Replace existing key?'])
if ret==1:
p.sendline('y')
p.expect('Enter passphrase:')
p.sendline(passphrase)
ret = p.expect(['Enter passphrase:','gpg>'])
if ret==0:
p.sendline(adminpin)
ret == p.expect(['Enter passphrase:','gpg>','SCEA'])
if ret == 0:
p.sendline(adminpin)
p.expect('gpg>')
p.sendline('key 3')
print(" key 3... Authentication")
p.expect('gpg>')
p.sendline('q')
p.expect('Save changes')
p.sendline('y')
with open("%s/%s,%s.csv"%(backupdir,lname,fname),'a+') as fw:
strdate = datetime.datetime.now(datetime.timezone.utc).strftime('%Y-%m-%dT%H:%M:%S%Z')
fw.write('"%s","%s","%s","%s","%s","%s","%s"\n'%(email,lname,fname,keyid,adminpin,cardserial,strdate))
print("Changes saved, done!")