-
Notifications
You must be signed in to change notification settings - Fork 0
/
dg11.go
132 lines (107 loc) · 3.44 KB
/
dg11.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
package gmrtd
import (
"bytes"
"fmt"
"log"
"log/slog"
"slices"
"strings"
)
const DG11Tag = 0x6B
// TODO - review parsing (< to space) and < separator on fields
// - currently we replace '<' with space, unless spec indicates that '<' is used as separator
// TODO - verify data.. e.g. max length.. some fields are mandatory based on others (but example contradicts spec)
// 9303p10 - Table 71. Data Group 11 Tags
type PersonDetails struct {
NameOfHolder MrzName
OtherNames []MrzName
PersonalNumber string
FullDateOfBirth string // YYYYMMDD
PlaceOfBirth []string
Address []string
Telephone string
Profession string
Title string
PersonalSummary string
ProofOfCitizenship []byte // image (ISO-10918)
OtherTravelDocuments []string
CustodyInformation string
}
type DG11 struct {
RawData []byte
Details PersonDetails
}
func NewDG11(data []byte) (*DG11, error) {
if len(data) < 1 {
return nil, nil
}
var out *DG11 = new(DG11)
out.RawData = slices.Clone(data)
nodes := TlvDecode(out.RawData)
slog.Debug("DG11", "TLV", nodes)
rootNode := nodes.GetNode(DG11Tag)
if !rootNode.IsValidNode() {
return nil, fmt.Errorf("root node (%x) missing", DG11Tag)
}
out.Details = parseData(rootNode)
slog.Debug("DG11", "Details", out.Details)
return out, nil
}
func parseData(node TlvNode) PersonDetails {
var out PersonDetails
tagList := getTagList(node.GetNode(0x5C).GetValue())
for _, tag := range tagList {
out.processTag(tag, node)
}
return out
}
// TODO - also used by EF.COM... have common TLV function for this?
func getTagList(data []byte) []TlvTag {
var out []TlvTag
buf := bytes.NewBuffer(data)
for {
if buf.Len() <= 0 {
break
}
out = append(out, TlvGetTag(buf))
}
return out
}
// processes the 'tag', getting the data from the TLV and populating PersonDetails
func (details *PersonDetails) processTag(tag TlvTag, node TlvNode) {
switch tag {
case 0x5F0E:
details.NameOfHolder = parseName(decodeValue(string(node.GetNode(tag).GetValue())))
case 0x5F0f:
// special handling as 'Other Names' are nested within tag A0 and there can be multiple instances
numOtherNames := bytesToInt(node.GetNode(0xA0).GetNode(0x02).GetValue())
for occur := 1; occur <= numOtherNames; occur++ {
details.OtherNames = append(details.OtherNames, parseName(decodeValue(string(node.GetNode(0xA0).GetNodeByOccur(0x5f0f, occur).GetValue()))))
}
case 0x5F10:
details.PersonalNumber = string(node.GetNode(tag).GetValue())
case 0x5F2B:
details.FullDateOfBirth = string(node.GetNode(tag).GetValue())
case 0x5F11:
details.PlaceOfBirth = strings.Split(string(node.GetNode(tag).GetValue()), "<")
case 0x5F42:
details.Address = strings.Split(string(node.GetNode(tag).GetValue()), "<")
case 0x5F12:
details.Telephone = string(node.GetNode(tag).GetValue())
case 0x5F13:
details.Profession = decodeValue(string(node.GetNode(tag).GetValue()))
case 0x5F14:
details.Title = decodeValue(string(node.GetNode(tag).GetValue()))
case 0x5F15:
details.PersonalSummary = decodeValue(string(node.GetNode(tag).GetValue()))
case 0x5F16:
// image data
details.ProofOfCitizenship = node.GetNode(tag).GetValue()
case 0x5F17:
details.OtherTravelDocuments = strings.Split(string(node.GetNode(tag).GetValue()), "<")
case 0x5F18:
details.CustodyInformation = decodeValue(string(node.GetNode(tag).GetValue()))
default:
log.Panicf("Unsupported tag:%x", tag)
}
}