-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdecoder.go
269 lines (209 loc) · 6.3 KB
/
decoder.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
package main
import (
"encoding/hex"
"flag"
"fmt"
"os"
"strconv"
"github.com/webshield-dev/eudvcdecoder/datamodel"
"github.com/webshield-dev/eudvcdecoder/helper"
)
/*
Decoder will decode and display the contents of a EU COVID-19 Digital Certificate, starting with teh QR code .png
The CLI flags are
1. -qrc_file <value> file containing the qr code png
2. -verbose <level> where level is 0 -> 9, default is zero
Example running with no verbose
- `go run . -qrfile ./testfiles/at_1.png`
- `go run . -qrfile ./testfiles/ie_1_qr.png`
Example running with verbose
`go run . -qrfile ./testfiles/ie_1_qr.png -verbose 1`
*/
const (
cliVerboseFlag = "verbose"
cliQRFilenameFlag = "qrfile"
)
var (
cliVerbose string
cliQRFilename string
)
// makeFlagSet return flag set needed to start
func makeFlagSet() *flag.FlagSet {
fs := flag.NewFlagSet("", flag.ExitOnError)
fs.StringVar(&cliVerbose, cliVerboseFlag, "0", "level of verbose")
fs.StringVar(&cliQRFilename, cliQRFilenameFlag, "", "qr code (.png) file name")
return fs
}
func main() {
fs := makeFlagSet()
err := fs.Parse(os.Args[1:])
if err != nil {
fmt.Printf("error parsing command line flags err=%s\n", err)
fs.PrintDefaults()
os.Exit(1)
}
verbose, err := strconv.Atoi(cliVerbose)
if err != nil {
fmt.Printf("error parsing verbose flag err=%s\n", err)
fs.PrintDefaults()
os.Exit(1)
}
maxVerbose := verbose > 1
lowVerbose := verbose == 1
//set up value set data
vsDataPath := os.Getenv("VS_DATA_PATH")
if vsDataPath == "" {
vsDataPath = "./valuesetdata"
}
vsMapper, err := helper.NewValueSetMapper(vsDataPath)
if err != nil {
fmt.Printf("error setting up value set mapper err=%s", err)
os.Exit(1)
}
dc := helper.NewDecoder(true, true)
fmt.Printf("Decoding EU Covid-19 Certificate\n")
fmt.Printf(" qrCodefile=%s ValueSetPath=%s verbose=%d\n", cliQRFilename, vsDataPath, verbose)
decodeOutput, err := dc.FromFileQRCode(cliQRFilename)
if err != nil {
_ = displayResults(vsMapper, decodeOutput, lowVerbose, maxVerbose)
fmt.Printf("ERROR processing certficate err=%s\n", err)
os.Exit(1)
}
if err := displayResults(vsMapper, decodeOutput, lowVerbose, maxVerbose); err != nil {
fmt.Printf("error displaying successful decode err=%s\n", err)
os.Exit(1)
}
}
func displayResults(vsMapper *helper.ValueSetMapper, output *helper.Output,
lowVerbose bool, maxVerbose bool) error {
if output == nil {
return nil
}
if len(output.DecodedQRCode) != 0 {
fmt.Printf(" Step 1 - Read QR Code PNG %s Successfully...\n", cliQRFilename)
if maxVerbose {
fmt.Printf(" value=%s\n", string(output.DecodedQRCode))
}
}
if len(output.Base45Decoded) != 0 {
fmt.Printf(" Step 2 - Base45 Decoded Successfully...\n")
if maxVerbose {
fmt.Printf(" hex(value)=%s\n", hex.EncodeToString(output.Base45Decoded))
}
}
if len(output.Inflated) != 0 {
fmt.Printf(" Step 3 - ZLIB Inflated Successfully...\n")
if maxVerbose {
fmt.Printf(" hex(value)=%s\n", hex.EncodeToString(output.Inflated))
}
}
if output.CBORUnmarshalledI != nil {
fmt.Printf(" Step 4 - CBOR UnMarshalled CBOR Web Token (CWT) using COSE tagged message COSE Number=%d Successfully...\n",
output.COSeCBORTag)
if maxVerbose {
fmt.Printf(" value=%+v\n", output.CBORUnmarshalledI)
}
}
if output.ProtectedHeader != nil {
fmt.Printf(" CWT CBOR UnMarshalled the Protected Header Successfully...\n")
if maxVerbose {
fmt.Printf(" value=%+v\n", output.ProtectedHeader)
}
}
if output.UnProtectedHeader != nil {
fmt.Printf(" CWT Read the UnProtected Header Map Successfully...\n")
if maxVerbose {
fmt.Printf(" value=%+v\n", *output.UnProtectedHeader)
}
}
if output.PayloadI != nil {
fmt.Printf(" CWT CBOR UnMarshalled the Payload Successfully...\n")
if maxVerbose {
fmt.Printf(" value=%+v\n", output.PayloadI)
}
}
if len(output.COSESignature) != 0 {
fmt.Printf(" CWT Read the COSE Signature (single signer) Successfully...\n")
if maxVerbose {
fmt.Printf(" hex(value)=%s\n", hex.EncodeToString(output.COSESignature))
}
}
if len(output.DiagnoseLines) != 0 {
for _, line := range output.DiagnoseLines {
fmt.Printf("%s\n", line)
}
}
if output.CommonPayload != nil {
//
// Display details
//
//
//Lets display all the important parts
//
fmt.Printf("Successfully Decoded EU Covid-19 Certificate\n")
if lowVerbose || maxVerbose {
fmt.Printf("\n**** EU Covid-19 Certificate Details **** \n")
//
//Protected header
//
prettyResult, err := helper.PrettyIdent(output.ProtectedHeader)
if err != nil {
return err
}
fmt.Printf("Protected Header=%s\n", prettyResult)
//
// Common payload
//
prettyPayload, err := helper.PettyIdentCommonPayload(output.CommonPayload)
if err != nil {
fmt.Printf("Error pretty printing payload raw=%+v\n", output.PayloadI)
return err
}
fmt.Printf("Common Payload=%s\n", prettyPayload)
//
// Signature in hex
//
fmt.Printf("hex(signature)=%s\n", hex.EncodeToString(output.COSESignature))
}
//
// Always Display Summary
//
displaySummary(vsMapper, output)
}
return nil
}
func displaySummary(vsMapper *helper.ValueSetMapper, output *helper.Output) {
cert := output.CommonPayload.HCERT[datamodel.HCERTMapKeyOne]
if cert == nil {
return
}
fmt.Printf("\n**** EU Covid-19 Certificate Summary **** \n")
fmt.Printf("")
fullName := cert.Name.FullName()
fmt.Printf("Name:%s\n", fullName)
fmt.Printf("DOB :%s\n", cert.DOB)
fmt.Printf("Vaccine Details\n")
for _, vaccine := range cert.Vaccine {
//display MP - Medicinal product used for this specific dose of vaccination. A
maVS := vsMapper.DecodeMA(vaccine.MA)
mpVS := vsMapper.DecodeMP(vaccine.MP)
vpVS := vsMapper.DecodeVP(vaccine.VP)
//convert dosage infomation to ints
sdI := int64(vaccine.SD)
dnI := int64(vaccine.DN)
fmt.Printf(" Doses Administered: %d\n", dnI)
fmt.Printf(" Doses Required: %d\n", sdI)
fmt.Printf(" When: %s\n", vaccine.DT)
if mpVS != nil {
fmt.Printf(" Vaccine Product: %s\n", mpVS.Display)
}
if vpVS != nil {
fmt.Printf(" Vaccine Type: %s\n", vpVS.Display)
}
if maVS != nil {
fmt.Printf(" Vaccine Maker: %s\n", maVS.Display)
}
fmt.Printf(" Issuer: %s\n", vaccine.IS)
fmt.Printf(" ID: %s\n", vaccine.CI)
}
}