forked from usbarmory/armory-drive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathota.go
More file actions
171 lines (125 loc) · 2.85 KB
/
Copy pathota.go
File metadata and controls
171 lines (125 loc) · 2.85 KB
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
// Copyright (c) F-Secure Corporation
// https://foundry.f-secure.com
//
// Use of this source code is governed by the license
// that can be found in the LICENSE file.
package main
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"runtime"
"strconv"
"strings"
"time"
"github.com/f-secure-foundry/armory-drive/assets"
"github.com/f-secure-foundry/armory-drive/minisign"
"github.com/f-secure-foundry/tamago/board/f-secure/usbarmory/mark-two"
"github.com/mitchellh/go-fs"
"github.com/mitchellh/go-fs/fat"
)
const sigLimit = 1024
const OTAName = "UA-DRIVE.OTA"
func ota() {
img, err := os.OpenFile(QR_DISK_PATH, os.O_RDWR|os.O_TRUNC, 0600)
if err != nil {
panic(err)
}
card := cards[0].(*QRCard)
_, err = img.Write(card.diskData[QR_PARTITION_OFFSET:])
if err != nil {
panic(err)
}
img.Seek(0, 0)
dev, err := fs.NewFileDisk(img)
if err != nil {
panic(err)
}
f, err := fat.New(dev)
if err != nil {
panic(err)
}
root, err := f.RootDir()
if err != nil {
panic(err)
}
for _, entry := range root.Entries() {
if entry.Name() == OTAName {
update(entry)
return
}
}
}
func update(entry fs.DirectoryEntry) {
var exit = make(chan bool)
file, err := entry.File()
if err != nil {
panic(err)
}
go func() {
var on bool
for {
select {
case <-exit:
usbarmory.LED("white", false)
return
default:
}
on = !on
usbarmory.LED("white", on)
runtime.Gosched()
time.Sleep(1 * time.Second)
}
}()
buf, err := ioutil.ReadAll(file)
if err != nil {
panic(err)
}
valid, bin, err := verify(buf)
if err != nil {
panic(err)
}
if !valid {
panic("invalid firmware signature")
}
err = usbarmory.MMC.WriteBlocks(2, bin)
if err != nil {
panic(err)
}
exit <- true
usbarmory.LED("blue", false)
usbarmory.LED("white", false)
}
func verify(buf []byte) (valid bool, bin []byte, err error) {
if bytes.Equal(assets.OTAPublicKey, make([]byte, len(assets.OTAPublicKey))) {
// If there is no valid OTA public key we assume that no OTA
// signature is present.
return true, buf, nil
}
if len(buf) < sigLimit {
return false, nil, errors.New("invalid signature")
}
sig, off, err := minisign.DecodeSignature(string(buf[0:sigLimit]))
if err != nil {
return false, nil, fmt.Errorf("invalid signature, %v", err)
}
pub, err := minisign.NewPublicKey(string(assets.OTAPublicKey))
if err != nil {
return false, nil, fmt.Errorf("invalid public key, %v", err)
}
// The fat package from go-fs reads back files adding padding to 4096,
// invalidating the signature. Therefore we receive a hint on the
// actual payload length in an untrusted comment.
n, err := strconv.Atoi(strings.TrimPrefix(sig.UntrustedComment, "untrusted comment: "))
if err != nil {
panic(err)
}
if n > len(buf[off:]) {
panic("invalid signature")
}
bin = buf[off : off+n]
valid, err = pub.Verify(bin, sig)
return
}