forked from status-im/keycard-go
/
load.go
164 lines (132 loc) · 3.23 KB
/
load.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
package globalplatform
import (
"archive/zip"
"bytes"
"io/ioutil"
"math"
"os"
"strings"
"github.com/GridPlus/keycard-go/apdu"
)
var internalFiles = []string{
"Header", "Directory", "Import", "Applet", "Class",
"Method", "StaticField", "Export", "ConstantPool", "RefLocation",
}
const blockSize = 247 // 255 - 8 bytes for MAC
// LoadCommandStream implement a struct that generates multiple Load commands used to load files to smartcards.
type LoadCommandStream struct {
data *bytes.Reader
currentIndex uint8
currentData []byte
p1 uint8
blocksCount int
}
// NewLoadCommandStream returns a new LoadCommandStream to load the specified file.
func NewLoadCommandStream(file *os.File) (*LoadCommandStream, error) {
files, err := loadFiles(file)
if err != nil {
return nil, err
}
data, err := encodeFilesData(files)
if err != nil {
return nil, err
}
return &LoadCommandStream{
data: bytes.NewReader(data),
p1: P1LoadMoreBlocks,
blocksCount: int(math.Ceil(float64(len(data)) / float64(blockSize))),
}, nil
}
// BlocksCount returns the total number of blocks based on data length and blockSize
func (lcs *LoadCommandStream) BlocksCount() int {
return lcs.blocksCount
}
// Next returns initialize the data for the next Load command.
// TODO:@gravityblast update blockSize when using encrypted data
func (lcs *LoadCommandStream) Next() bool {
if lcs.data.Len() == 0 {
return false
}
buf := make([]byte, blockSize)
n, err := lcs.data.Read(buf)
if err != nil {
return false
}
lcs.currentData = buf[:n]
lcs.currentIndex++
if lcs.data.Len() == 0 {
lcs.p1 = P1LoadLastBlock
}
return true
}
// Index returns the command index.
func (lcs *LoadCommandStream) Index() uint8 {
return lcs.currentIndex - 1
}
// GetCommand returns the current apdu command.
func (lcs *LoadCommandStream) GetCommand() *apdu.Command {
return apdu.NewCommand(ClaGp, InsLoad, lcs.p1, lcs.Index(), lcs.currentData)
}
func loadFiles(f *os.File) (map[string][]byte, error) {
fi, err := f.Stat()
if err != nil {
return nil, err
}
z, err := zip.NewReader(f, fi.Size())
if err != nil {
return nil, err
}
files := make(map[string][]byte)
for _, item := range z.File {
name := strings.Split(item.FileInfo().Name(), ".")[0]
f, err := item.Open()
if err != nil {
return nil, err
}
data, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
files[name] = data
}
return files, nil
}
func encodeFilesData(files map[string][]byte) ([]byte, error) {
var buf bytes.Buffer
for _, name := range internalFiles {
if data, ok := files[name]; ok {
buf.Write(data)
}
}
filesData := buf.Bytes()
length := encodeLength(len(filesData))
data := make([]byte, 0)
data = append(data, tagLoadFileDataBlock)
data = append(data, length...)
data = append(data, filesData...)
return data, nil
}
func encodeLength(length int) []byte {
if length < 0x80 {
return []byte{byte(length)}
}
if length < 0xFF {
return []byte{
byte(0x81),
byte(length),
}
}
if length < 0xFFFF {
return []byte{
byte(0x82),
byte((length & 0xFF00) >> 8),
byte(length & 0xFF),
}
}
return []byte{
byte(0x83),
byte((length & 0xFF0000) >> 16),
byte((length & 0xFF00) >> 8),
byte(length & 0xFF),
}
}