-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
193 lines (156 loc) · 4.73 KB
/
main.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
package main
import (
"bytes"
"encoding/base64"
"flag"
"fmt"
"html/template"
"log"
"log/slog"
"os"
"github.com/dumacp/smartcard/pcsc"
"github.com/gmrtd/gmrtd"
"github.com/gmrtd/gmrtd/document"
"github.com/gmrtd/gmrtd/iso7816"
"github.com/gmrtd/gmrtd/password"
"github.com/gmrtd/gmrtd/tlv"
"github.com/gmrtd/gmrtd/utils"
"github.com/pkg/browser"
)
type PCSCTransceiver struct {
card pcsc.Card
}
func (transceiver *PCSCTransceiver) Transceive(cla int, ins int, p1 int, p2 int, data []byte, le int, encodedData []byte) (rApduBytes []byte) {
rApduBytes, err := transceiver.card.Apdu(encodedData)
if err != nil {
slog.Error("Transceive", "error", err)
return
}
slog.Debug("Transceive", "cApdu", utils.BytesToHex(encodedData), "rApdu", utils.BytesToHex(rApduBytes))
return
}
var temp *template.Template
func outputDocument(document *document.Document) {
var err error
if document == nil {
return
}
// TODO - update to include DG2/7 images.. maybe easier if we switch to a html output
// e.g.
// gmrtd.WriteFile("DG2_image", document.Dg2.BITs[0].BDB.Facial.Images[0].Data)
// gmrtd.WriteFile("DG7_image", document.Dg7.Images[0].Image)
funcMap := template.FuncMap{
"BytesToHex": func(bytes []byte) string { return fmt.Sprintf("%X", bytes) },
"TlvBytesToString": func(bytes []byte) string { return tlv.Decode(bytes).String() },
"BytesToBase64": func(bytes []byte) string { return base64.StdEncoding.EncodeToString(bytes) },
"ApduTotalDurMs": func(apdus []iso7816.ApduLog) int {
var totalMs int
for _, apdu := range apdus {
totalMs += apdu.DurMs
}
return totalMs
},
}
temp = template.Must(template.New("template.tpl").Funcs(funcMap).ParseFiles("template.tpl"))
byteBuf := bytes.NewBuffer(nil)
// convert to HTML using template
err = temp.Execute(byteBuf, document)
if err != nil {
log.Fatalln(err)
}
// display in default browser
err = browser.OpenReader(byteBuf)
if err != nil {
log.Fatalln(err)
}
}
func getParams() (pass *password.Password, debug bool, apduMaxRead uint, err error) {
documentNo := flag.String("doc", "", "Document Number")
dateOfBirth := flag.String("dob", "", "Date of Birth (YYMMDD)")
expiryDate := flag.String("exp", "", "Expiry Date (YYMMDD)")
can := flag.String("can", "", "Card Access Number")
debugFlag := flag.Bool("debug", false, "Debug")
maxRead := flag.Uint("maxRead", 0, "Maximum read amount (bytes) e.g. 1..65536")
flag.Parse()
if len(*documentNo) > 0 && len(*dateOfBirth) == 6 && len(*expiryDate) == 6 {
pass = password.NewPasswordMrzi(*documentNo, *dateOfBirth, *expiryDate)
} else if len(*can) > 0 {
pass = password.NewPasswordCan(*can)
} else {
flag.PrintDefaults()
return nil, false, 0, fmt.Errorf("usage: must specify either doc+dob+exp *OR* can")
}
log.Printf("Doc:%s, DOB:%s, Exp:%s, CAN:%s, Debug:%t, MaxRead:%d", *documentNo, *dateOfBirth, *expiryDate, *can, *debugFlag, *maxRead)
log.Printf("Password: %+v", pass)
return pass, *debugFlag, *maxRead, nil
}
func initLogging(debug bool) {
logLevel := &slog.LevelVar{}
opts := &slog.HandlerOptions{
Level: logLevel,
}
logger := slog.New(slog.NewJSONHandler(os.Stdout, opts))
slog.SetDefault(logger)
if debug {
logLevel.Set(slog.LevelDebug.Level()) // enable debug
}
}
func main() {
var pass *password.Password
var debug bool = false
var maxRead uint = 0
var err error
pass, debug, maxRead, err = getParams()
if err != nil {
log.Printf("%s", err)
os.Exit(1)
}
initLogging(debug)
ctx, err := pcsc.NewContext()
if err != nil {
slog.Error("Unable to initialise PCSC")
os.Exit(1)
}
defer ctx.Release()
readers, err := pcsc.ListReaders(ctx)
if err != nil {
slog.Error("Unable to get PCSC readers")
os.Exit(1)
}
slog.Debug("PCSC", "readers", readers)
if len(readers) < 1 {
slog.Error("No PCSC reader found")
os.Exit(1)
}
// NB we currently just select the 1st reader (if multiple)
pcscReader := pcsc.NewReader(ctx, readers[0])
card, err := pcscReader.ConnectCardPCSC()
if err != nil {
slog.Error("No chip detected")
os.Exit(1)
}
defer card.DisconnectCard()
atr, _ := card.ATR()
ats, _ := card.ATS()
var transceiver *PCSCTransceiver = new(PCSCTransceiver)
transceiver.card = card
var reader *gmrtd.Reader = gmrtd.NewReader()
// set APDU Max Read (if specified)
if maxRead > 0 {
reader.SetApduMaxLe(int(maxRead))
}
document, err := reader.ReadDocument(transceiver, pass, atr, ats)
if err != nil {
// output whatever we have from the document
outputDocument(document)
slog.Error("ReadDocument", "error", err)
os.Exit(1)
}
// TODO - should really happen before outputDocument (above).. and info should be included
err = gmrtd.PassiveAuth(document)
if err != nil {
slog.Error("MrtdPassiveAuth", "error", err)
os.Exit(1)
}
outputDocument(document)
}