-
Notifications
You must be signed in to change notification settings - Fork 2
/
PIVageant.pyw
275 lines (236 loc) · 8.99 KB
/
PIVageant.pyw
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# PIVageant
# a Windows Pageant SSH agent with PIV dongle
# Copyright (C) 2021-2022 BitLogiK
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
from functools import partial
from ctypes import windll
import os
import sys
import wx
import wx.lib.newevent
import lib.gui.mainwin
import lib.gui.pageant_win
from lib.gui.getwin import check_pageant_running
from lib.gui.systemtray import PIVagTray
from lib.piv.piv_card import (
PIVCardException,
PIVCardTimeoutException,
ConnectionException,
)
from lib.piv.genkeys import generate_key
from lib.ssh.ssh_encodings import openssh_to_wire
from lib.pageantclient import process_command, read_pubkey
from _version import __version__
KEY_NAME = "ECPSSHKey"
def is_tty():
if not hasattr(sys, "stdin"):
return False
if not hasattr(sys.stdin, "isatty"):
return False
return sys.stdin.isatty()
DEBUG_OUTPUT = False
class ModalWait(lib.gui.mainwin.ModalDialog):
def end_sign(self, event):
if event:
event.Skip()
self.Destroy()
class PIVageantwin(lib.gui.mainwin.PIVageant):
def copy_content(self, evt):
if wx.TheClipboard.Open():
pubkey_txt = self.pubkey_text.GetValue().replace("\n", "")
if pubkey_txt.startswith("ecdsa-"):
pubkey_str = wx.TextDataObject(pubkey_txt)
wx.TheClipboard.SetData(pubkey_str)
wx.TheClipboard.Close()
wx.TheClipboard.Flush()
self.change_status("Public key in clipboard")
wx.CallLater(800, self.change_status, "Ready")
evt.Skip()
def clear(self):
self.gen_btn.Disable()
self.refresh_btn.Disable()
self.cpy_btn.Disable()
self.pubkey_text.SetValue("")
self.change_status("Connect a PIV device")
def refresh_key(self, evt):
evt.Skip()
self.waiting_for_pivkey("start")
def gen_key(self, evt):
evt.Skip()
self.gen_btn.Disable()
confirm_modal = wx.MessageDialog(
self,
"A new key generation can erase any current key in the dongle."
"\nConfirm the key creation ?",
caption="Confirm key generation",
style=wx.YES | wx.NO | wx.CENTRE,
pos=wx.DefaultPosition,
)
confirm_modal.SetYesNoLabels("Proceed", "cancel")
if confirm_modal.ShowModal() == wx.ID_YES:
self.refresh_btn.Disable()
self.cpy_btn.Disable()
self.change_status("Key generation ...")
self.print_pubkey("")
res_gen = generate_key(DEBUG_OUTPUT)
if res_gen == "done":
self.waiting_for_pivkey("genkey")
else:
self.change_status(res_gen)
else:
self.gen_btn.Enable()
def go_start(self, ssh_pubkey, close):
self.print_pubkey(ssh_pubkey)
process_cb = partial(
process_command,
DEBUG_OUTPUT,
openssh_to_wire(ssh_pubkey),
self.sign_status,
self.end_status,
)
if close:
self.change_status("Key read, closing to tray")
close_agentwindow()
self.refresh_btn.Enable()
self.cpy_btn.Enable()
wx.CallLater(500, lib.gui.pageant_win.MainWin, process_cb)
def change_status(self, text_status):
self.status_text.SetLabelText(text_status)
def sign_status(self, user, card_info):
self.change_status("Signature requested")
self.sign_alert = ModalWait(self)
if not card_info["isYubico"]:
self.sign_alert.static_text_modal.SetLabel("Signing with the PIV dongle")
self.sign_alert.username_txt.SetLabel(f"as user : {user}")
self.sign_alert.Restore()
self.sign_alert.Show(True)
self.sign_alert.gauge_wait.Pulse()
self.sign_alert.Refresh()
self.sign_alert.Update()
wx.MilliSleep(250)
def end_status(self, data_text):
self.change_status(data_text)
# save main windows status for selective hide
is_disp = self.IsShownOnScreen()
if not is_disp:
self.Lower()
self.SetTransparent(0)
self.Show(True)
self.sign_alert.Destroy()
if not is_disp:
self.Hide()
self.SetTransparent(255)
wx.CallLater(3500, self.change_status, "Ready")
def print_pubkey(self, pubkey_value):
self.pubkey_text.SetValue(pubkey_value)
def get_event(self, event):
# Process PIVkey events
if event.type == "Connected":
self.go_start(event.data, True)
wx.CallLater(3500, self.change_status, "Ready")
wx.CallLater(850, self.sendtray, None)
return
if event.type == "Generated":
self.go_start(event.data, False)
wx.CallLater(250, self.change_status, "New key generated")
return
if event.type == "Timeout":
self.waiting_for_pivkey("start")
return
if event.type == "Error":
self.change_status(event.data)
return
def sendtray(self, _):
wx.CallLater(400, self.Hide)
def closing(self, event):
close_agentwindow()
self.trayicon.RemoveIcon()
self.trayicon.Destroy()
if hasattr(event, "Skip"):
event.Skip()
def waiting_for_pivkey(self, caller):
wx.CallLater(500, self.get_pubkey, caller)
def get_pubkey(self, caller):
try:
piv_ssh_public_key = read_pubkey(KEY_NAME, 0.8, DEBUG_OUTPUT)
self.gen_btn.Enable()
if caller == "start":
self.change_status("PIV key detected")
wx.PostEvent(
self, PivKeyEvent(type="Connected", data=piv_ssh_public_key)
)
elif caller == "genkey":
wx.PostEvent(
self, PivKeyEvent(type="Generated", data=piv_ssh_public_key)
)
else:
raise Exception("Invalid caller")
except PIVCardTimeoutException:
wx.PostEvent(self, PivKeyEvent(type="Timeout"))
except PIVCardException as exc:
err_msg = str(exc)
if err_msg == "Error status : 0x6A82" or err_msg == "Error status : 0x6A83":
self.gen_btn.Enable()
wx.PostEvent(
self, PivKeyEvent(type="Error", data="No key found, generate a key")
)
else:
wx.PostEvent(self, PivKeyEvent(type="Error", data="Error: " + err_msg))
except ConnectionException as exc:
wx.PostEvent(self, PivKeyEvent(type="Error", data=str(exc)))
def close_agentwindow():
agent_win_id = lib.gui.pageant_win.get_window_id()
if agent_win_id != 0:
lib.gui.pageant_win.close_window(agent_win_id)
def get_path(fpath):
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, fpath)
return fpath
PivKeyEvent, EVT_PIVKEY_EVENT = wx.lib.newevent.NewEvent()
# PIVKEY_EVENT Attribute type :
# "Timeout" (no key connected yey)
# "Connected"
# "Error"
# "Signed"
# Attribute data :
# for type Error : error message
# for type Connected : public_key
def mainapp():
app = wx.App()
windll.shcore.SetProcessDpiAwareness(2)
# Check if a Pageant is running
if check_pageant_running():
wx.MessageBox(
"A Pageant process is already running.",
"PIVageant info",
wx.OK | wx.ICON_WARNING | wx.STAY_ON_TOP,
)
return
app.main_frame = PIVageantwin(None)
app.main_frame.SetTitle(f"PIVageant - {__version__}")
icon_file = get_path("res\\pivageant.ico")
app.main_frame.SetIcons(wx.IconBundle(icon_file))
app.main_frame.clear()
app.main_frame.Show()
app.main_frame.Bind(wx.EVT_CLOSE, app.main_frame.closing)
app.main_frame.Bind(wx.EVT_ICONIZE, app.main_frame.sendtray)
app.main_frame.Bind(EVT_PIVKEY_EVENT, app.main_frame.get_event)
app.main_frame.trayicon = PIVagTray(app.main_frame, icon_file)
app.main_frame.Update()
app.main_frame.waiting_for_pivkey("start")
app.MainLoop()
if __name__ == "__main__":
if "-v" in sys.argv[1:]:
DEBUG_OUTPUT = is_tty()
mainapp()