-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocalResources.go
195 lines (161 loc) · 4.15 KB
/
localResources.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
package main
import (
"context"
"crypto"
"fmt"
"github.com/quan-to/chevron/pkg/chevronlib"
"github.com/quan-to/chevron/pkg/interfaces"
"github.com/quan-to/chevron/pkg/models"
"io/ioutil"
"net/http"
"os"
"path"
"path/filepath"
"strings"
)
const (
MaxFileSize = 2 * 1024 * 1024 // 1MB
)
var (
pgp interfaces.PGPManager
krm interfaces.KeyRingManager
executableFolder string
keysFolder string
)
var ctx = context.Background()
func init() {
ex, _ := os.Executable()
executableFolder = filepath.Dir(ex)
keysFolder = path.Join(executableFolder, "keys")
}
func Begin() {
_ = os.Mkdir(keysFolder, os.ModePerm)
kb := chevronlib.MakeSaveToDiskBackend(nil, keysFolder, "key_")
krm = chevronlib.MakeKeyRingManager(nil)
pgp = chevronlib.MakePGPManager(nil, kb, krm)
pgp.LoadKeys(ctx)
}
func AddPrivateKey(privateKeyData string) (string, error) {
n, err := pgp.LoadKey(ctx, privateKeyData)
if err != nil {
return err.Error(), err
}
return fmt.Sprintf("Loaded %d keys", n), nil
}
func UnlockKey(fingerPrint, password string) (string, error) {
err := pgp.UnlockKey(ctx, fingerPrint, password)
if err != nil {
log.Error("Error unlocking key %s: %s", fingerPrint, err)
return err.Error(), err
}
return fmt.Sprintf("Key %s unlocked!", fingerPrint), nil
}
func Sign(fingerPrint, data string) (string, error) {
key := pgp.GetPrivateKeyInfo(ctx, fingerPrint)
if key == nil {
err := fmt.Errorf("key not found")
return err.Error(), err
}
if !key.PrivateKeyIsDecrypted {
err := fmt.Errorf("key is not decrypted")
return err.Error(), err
}
signature, err := pgp.SignData(ctx, fingerPrint, []byte(data), crypto.SHA512)
if err != nil {
return err.Error(), err
}
quantoSig := chevronlib.GPG2Quanto(signature, fingerPrint, "SHA512")
return quantoSig, nil
}
func ListPrivateKeys() ([]models.KeyInfo, error) {
return pgp.GetLoadedPrivateKeys(ctx), nil
}
func IsFile(name string) bool {
fi, err := os.Stat(name)
if err != nil {
return false
}
return fi.Mode().IsRegular()
}
func FileSize(name string) int64 {
fi, err := os.Stat(name)
if err != nil {
return 0
}
return fi.Size()
}
func GetFileContentType(name string) string {
f, err := os.Open(name)
if err != nil {
panic(err)
}
defer f.Close()
// Only the first 512 bytes are used to sniff the content type.
buffer := make([]byte, 512)
_, err = f.Read(buffer)
if err != nil {
return "error reading file"
}
// Use the net/http package's handy DectectContentType function. Always returns a valid
// content-type by returning "application/octet-stream" if no others seemed to match.
contentType := http.DetectContentType(buffer)
return contentType
}
func AddKeys(files []string) (bool, []string) {
errors := make([]string, len(files))
hasErrors := false
for i, file := range files {
if !IsFile(file) {
errors[i] = fmt.Sprintf("%s is not a regular file", file)
hasErrors = true
continue
}
fileType := GetFileContentType(file)
if !strings.Contains(fileType, "text/plain") {
errors[i] = fmt.Sprintf("invalid file type: %s", fileType)
hasErrors = true
continue
}
size := FileSize(file)
if size > MaxFileSize {
errors[i] = fmt.Sprintf("file size too big: %d", size)
hasErrors = true
continue
}
data, err := ioutil.ReadFile(file)
if err != nil {
errors[i] = err.Error()
hasErrors = true
continue
}
fingerPrint, err := chevronlib.GetFingerprintFromKey(string(data))
if err != nil {
errors[i] = err.Error()
continue
}
log.Info("Saving key %s from %s", fingerPrint, file)
err = pgp.SaveKey(fingerPrint, string(data), nil)
if err != nil {
errors[i] = err.Error()
hasErrors = true
continue
}
pgp.LoadKeys(ctx)
}
return hasErrors, errors
}
func Migrate() {
storeFolder := path.Join(executableFolder, "store")
if chevronlib.FolderExists(storeFolder) { // Old key store
log.Warn("Found \"store\" folder. Migrating keys...")
err := chevronlib.CopyFiles(storeFolder, keysFolder)
if err != nil {
log.Error("Error moving files from store to keys: %s", err)
} else {
err = os.RemoveAll(storeFolder)
if err != nil {
log.Error("Error removing folder store: %s", err)
}
}
}
}