-
Notifications
You must be signed in to change notification settings - Fork 14
/
mynag.go
428 lines (391 loc) · 10.4 KB
/
mynag.go
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
package main
import "github.com/jpki/myna/libmyna"
import (
"crypto/x509"
_ "errors"
"fmt"
"github.com/mattn/go-gtk/gdkpixbuf"
"github.com/mattn/go-gtk/glib"
"github.com/mattn/go-gtk/gtk"
"github.com/urfave/cli"
"os"
"strings"
)
func main() {
app := cli.NewApp()
app.Name = "mynag"
app.Description = "マイナクライアント(GUI)"
app.Author = "HAMANO Tsukasa"
app.Email = "hamano@osstech.co.jp"
app.Version = libmyna.Version
app.Action = mynag
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "詳細出力",
},
}
app.Run(os.Args)
}
func mynag(c *cli.Context) error {
gtk.Init(&os.Args)
window := gtk.NewWindow(gtk.WINDOW_TOPLEVEL)
window.SetPosition(gtk.WIN_POS_CENTER)
window.SetTitle(c.App.Description)
window.SetIconName("application-certificate")
window.Connect("destroy", func(ctx *glib.CallbackContext) {
gtk.MainQuit()
})
// create menu
menu := gtk.NewMenuBar()
menuHelp := gtk.NewMenuItemWithMnemonic("Menu")
menu.Append(menuHelp)
menuSub := gtk.NewMenu()
menuHelp.SetSubmenu(menuSub)
menuItemAbout := gtk.NewMenuItemWithMnemonic("About")
menuItemAbout.Connect("activate", onAbout, c)
menuSub.Append(menuItemAbout)
menuItemQuit := gtk.NewMenuItemWithMnemonic("Quit")
menuItemQuit.Connect("activate", onQuit, c)
menuSub.Append(menuItemQuit)
// create button box
boxButton := gtk.NewVBox(false, 1)
buttonTest := gtk.NewButtonWithLabel("動作確認")
buttonTest.Clicked(onTest, c)
boxButton.Add(buttonTest)
buttonCardInfo := gtk.NewButtonWithLabel("券面事項確認")
buttonCardInfo.Clicked(onCardInfo, c)
boxButton.Add(buttonCardInfo)
buttonShowCert := gtk.NewButtonWithLabel("証明書表示")
buttonShowCert.Clicked(onShowCert, c)
boxButton.Add(buttonShowCert)
buttonSign := gtk.NewButtonWithLabel("署名")
buttonSign.Clicked(onSign, c)
boxButton.Add(buttonSign)
buttonPinStatus := gtk.NewButtonWithLabel("PINステータス")
buttonPinStatus.Clicked(onPinStatus, c)
boxButton.Add(buttonPinStatus)
// create hbox
hbox := gtk.NewHBox(false, 1)
//dir, _ := filepath.Split(os.Args[0])
//imagefile := filepath.Join(dir, "usagi.png")
//imageLogo := gtk.NewImageFromFile(imagefile)
imageData, _ := Asset("usagi.png")
imageBuf, _ := gdkpixbuf.NewPixbufFromData(imageData)
imageLogo := gtk.NewImageFromPixbuf(imageBuf)
hbox.Add(imageLogo)
hbox.Add(boxButton)
// create main box
boxMain := gtk.NewVBox(false, 1)
boxMain.PackStart(menu, false, false, 0)
boxMain.Add(hbox)
window.Add(boxMain)
//window.SetSizeRequest(400, 400)
window.ShowAll()
gtk.Main()
return nil
}
func onAbout(ctx *glib.CallbackContext) {
c := ctx.Data().(*cli.Context)
dialog := gtk.NewAboutDialog()
dialog.SetProgramName(c.App.Name)
dialog.SetVersion(c.App.Version)
dialog.SetAuthors([]string{c.App.Author})
comments := `
このソフトウェアはMITライセンスで配布されます。
GTK+ は LGPLライセンス
mattn ware の go-gtk
かわいいウサギのイラストはいらすとやの著作物です
`
dialog.SetComments(comments)
dialog.SetWebsite("https://github.com/jpki/myna")
license, _ := Asset("../LICENSE")
dialog.SetLicense(string(license))
dialog.SetWrapLicense(true)
imageData, _ := Asset("usagi.png")
imageBuf, _ := gdkpixbuf.NewPixbufFromData(imageData)
dialog.SetLogo(imageBuf)
dialog.Run()
dialog.Destroy()
}
func onQuit(ctx *glib.CallbackContext) {
gtk.MainQuit()
}
func showErrorMsg(msg string) {
dialog := gtk.NewMessageDialog(
nil,
gtk.DIALOG_MODAL,
gtk.MESSAGE_INFO,
gtk.BUTTONS_OK,
msg)
dialog.Response(func() {
dialog.Destroy()
})
dialog.Run()
}
func showMsg(msg string) {
dialog := gtk.NewMessageDialog(
nil,
gtk.DIALOG_MODAL,
gtk.MESSAGE_INFO,
gtk.BUTTONS_OK,
msg)
dialog.Response(func() {
dialog.Destroy()
})
dialog.Run()
}
func onTest(ctx *glib.CallbackContext) {
c := ctx.Data().(*cli.Context)
err := libmyna.CheckCard(c)
var msg string
if err != nil {
msg = err.Error()
} else {
msg = "正常です。"
}
showErrorMsg(msg)
}
func popupPrompt(title string) string {
dialog := gtk.NewDialog()
dialog.SetTitle(title)
vbox := dialog.GetVBox()
label := gtk.NewLabel(title)
vbox.Add(label)
pinEntry := gtk.NewEntry()
pinEntry.SetVisibility(false)
pinEntry.Connect("activate", func() {
dialog.Hide()
dialog.Response(gtk.RESPONSE_OK)
})
vbox.Add(pinEntry)
dialog.AddButton(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
dialog.AddButton(gtk.STOCK_OK, gtk.RESPONSE_OK)
dialog.SetDefaultResponse(gtk.RESPONSE_OK)
dialog.ShowAll()
res := dialog.Run()
pin := pinEntry.GetText()
dialog.Destroy()
if res == gtk.RESPONSE_OK {
return pin
} else {
return ""
}
}
func popupPinPrompt() string {
return popupPrompt("暗証番号(4桁)")
}
func popupPasswordPrompt() string {
pass := popupPrompt("署名用パスワード(6-16桁)")
if len(pass) < 6 || 16 < len(pass) {
showErrorMsg("署名用パスワード(6-16桁)を入力してください。")
return ""
}
return strings.ToUpper(pass)
}
func onCardInfo(ctx *glib.CallbackContext) {
c := ctx.Data().(*cli.Context)
err := libmyna.CheckCard(c)
if err != nil {
showErrorMsg(err.Error())
return
}
pin := popupPinPrompt()
if pin == "" {
return
}
info, _ := libmyna.GetCardInfo(c, pin)
var msg string
msg += fmt.Sprintf("個人番号: %s\n", info["number"])
msg += fmt.Sprintf("氏名: %s\n", info["name"])
msg += fmt.Sprintf("住所: %s\n", info["address"])
msg += fmt.Sprintf("生年月日: %s\n", info["birth"])
msg += fmt.Sprintf("性別: %s", info["sex"])
showMsg(msg)
}
func onPinStatus(ctx *glib.CallbackContext) {
c := ctx.Data().(*cli.Context)
status, err := libmyna.GetPinStatus(c)
if err != nil {
showErrorMsg(err.Error())
return
}
var msg string
msg += fmt.Sprintf("認証用PIN: のこり%d回\n", status["auth"])
msg += fmt.Sprintf("署名用PIN: のこり%d回\n", status["sign"])
msg += fmt.Sprintf("券面入力補助PIN: のこり%d回\n", status["card"])
msg += fmt.Sprintf("謎のPIN1: のこり%d回\n", status["unknown1"])
msg += fmt.Sprintf("謎のPIN2: のこり%d回", status["unknown2"])
showMsg(msg)
}
func onShowCert(ctx *glib.CallbackContext) {
c := ctx.Data().(*cli.Context)
rc := selectCert()
var title string
var ef string
var pin string
switch rc {
case 0:
// キャンセル
return
case 1:
title = "認証用証明書"
ef = "00 0A"
case 2:
title = "署名用証明書"
ef = "00 01"
pin = popupPasswordPrompt()
if pin == "" {
return
}
case 3:
title = "認証用CA証明書"
ef = "00 0B"
case 4:
title = "署名用CA証明書"
ef = "00 02"
}
cert, err := libmyna.GetCert(c, ef, pin)
if err != nil {
showErrorMsg(err.Error())
return
}
popupCertDialog(title, cert)
}
func popupCertDialog(title string, cert *x509.Certificate) {
dialog := gtk.NewDialog()
defer dialog.Destroy()
dialog.SetTitle(title)
vbox := dialog.GetVBox()
vbox.PackStart(gtk.NewLabel("Subject: "), false, false, 10)
subjectEntry := gtk.NewEntry()
subjectEntry.SetText(libmyna.Name2String(cert.Subject))
subjectEntry.SetWidthChars(64)
vbox.PackStart(subjectEntry, false, false, 10)
vbox.PackStart(gtk.NewLabel("Issuer: "), false, false, 10)
issuerEntry := gtk.NewEntry()
issuerEntry.SetText(libmyna.Name2String(cert.Issuer))
issuerEntry.SetWidthChars(64)
vbox.PackStart(issuerEntry, false, false, 10)
dialog.AddButton(gtk.STOCK_OK, gtk.RESPONSE_OK)
dialog.ShowAll()
dialog.Run()
}
func selectCert() int {
dialog := gtk.NewDialog()
defer dialog.Destroy()
dialog.SetTitle("証明書選択")
vbox := dialog.GetVBox()
radio1 := gtk.NewRadioButtonWithLabel(nil, "認証用証明書")
radio2 := gtk.NewRadioButtonWithLabel(radio1.GetGroup(), "署名用証明書")
radio3 := gtk.NewRadioButtonWithLabel(radio1.GetGroup(), "認証用CA証明書")
radio4 := gtk.NewRadioButtonWithLabel(radio1.GetGroup(), "署名用CA証明書")
vbox.PackStart(radio1, false, false, 10)
vbox.PackStart(radio2, false, false, 10)
vbox.PackStart(radio3, false, false, 10)
vbox.PackStart(radio4, false, false, 10)
dialog.AddButton(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
dialog.AddButton(gtk.STOCK_OK, gtk.RESPONSE_OK)
dialog.SetDefaultResponse(gtk.RESPONSE_OK)
dialog.ShowAll()
res := dialog.Run()
if res == gtk.RESPONSE_OK {
if radio1.GetActive() {
return 1
} else if radio2.GetActive() {
return 2
} else if radio3.GetActive() {
return 3
} else if radio4.GetActive() {
return 4
} else {
return 0
}
} else {
return 0
}
}
func onSign(ctx *glib.CallbackContext) {
c := ctx.Data().(*cli.Context)
dialog := gtk.NewDialog()
defer dialog.Destroy()
dialog.SetTitle("署名")
inputEntry := gtk.NewEntry()
inputEntry.SetWidthChars(64)
inputEntry.SetEditable(false)
outputEntry := gtk.NewEntry()
outputEntry.SetWidthChars(64)
outputEntry.SetEditable(false)
vbox := dialog.GetVBox()
inputButton := gtk.NewButtonWithLabel("入力ファイル")
inputButton.Clicked(func() {
filedialog := gtk.NewFileChooserDialog(
"ファイル選択",
nil,
gtk.FILE_CHOOSER_ACTION_OPEN,
gtk.STOCK_OK,
gtk.RESPONSE_OK,
gtk.STOCK_CANCEL,
gtk.RESPONSE_CANCEL)
res := filedialog.Run()
if res == gtk.RESPONSE_OK {
filename := filedialog.GetFilename()
inputEntry.SetText(filename)
if outputEntry.GetText() == "" {
outputEntry.SetText(filename + ".p7s")
}
}
filedialog.Destroy()
})
row := gtk.NewHBox(false, 1)
row.Add(inputEntry)
row.Add(inputButton)
vbox.Add(row)
outputButton := gtk.NewButtonWithLabel("出力ファイル")
outputButton.Clicked(func() {
filedialog := gtk.NewFileChooserDialog(
"ファイル選択",
nil,
gtk.FILE_CHOOSER_ACTION_SAVE,
gtk.STOCK_OK,
gtk.RESPONSE_OK,
gtk.STOCK_CANCEL,
gtk.RESPONSE_CANCEL)
res := filedialog.Run()
if res == gtk.RESPONSE_OK {
outputEntry.SetText(filedialog.GetFilename())
}
filedialog.Destroy()
})
row = gtk.NewHBox(false, 1)
row.Add(outputEntry)
row.Add(outputButton)
vbox.Add(row)
dialog.AddButton(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL)
dialog.AddButton("署名", gtk.RESPONSE_OK)
dialog.ShowAll()
res := dialog.Run()
if res != gtk.RESPONSE_OK {
return
}
inFile := inputEntry.GetText()
if inFile == "" {
showErrorMsg("入力ファイルを指定してください")
return
}
outFile := outputEntry.GetText()
if outFile == "" {
showErrorMsg("出力ファイルを指定してください")
return
}
pass := popupPasswordPrompt()
if pass == "" {
return
}
err := libmyna.Sign(c, pass, inFile, outFile)
if err != nil {
showErrorMsg(err.Error())
return
}
showMsg("署名完了")
}