/
common.go
167 lines (154 loc) · 4.91 KB
/
common.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
// Copyright 2018 The go-fireblock Authors
// This file is part of the go-fireblock library.
//
// The go-fireblock library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-fireblock library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-fireblock library. If not, see <http://www.gnu.org/licenses/>.
//
// Package common contains various helper functions.
package fireblocklib
import (
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"path/filepath"
"regexp"
)
// Metadata meta data
type Metadata struct {
Kind string `json:"kind,omitempty"`
Filename string `json:"filename,omitempty"`
Type string `json:"type,omitempty"`
Size int64 `json:"size"`
}
// MetadataFile extract Metadata from file
func MetadataFile(filepath string) (Metadata, error) {
metadata := Metadata{"", "", "", 0}
file, err := os.Open(filepath)
if err != nil {
msg := fmt.Sprintf(`No file at %s`, filepath)
return metadata, NewFBKError(msg, InvalidFile)
}
defer file.Close()
fi, err := file.Stat()
if err != nil {
msg := fmt.Sprintf(`No stat info at %s`, filepath)
return metadata, NewFBKError(msg, InvalidFile)
}
// json -> string
metadata.Filename = path.Base(filepath)
metadata.Size = fi.Size()
return metadata, nil
}
const regexPGPPrivKey = `(?s).*(-----BEGIN PGP PRIVATE KEY BLOCK-----.*-----END PGP PRIVATE KEY BLOCK-----).*`
const regexPGPPubKey = `(?s).*(-----BEGIN PGP PUBLIC KEY BLOCK-----.*-----END PGP PUBLIC KEY BLOCK-----).*`
// ECDSAJWK ecdsa jwk
type ECDSAJWK struct {
Crv string `json:"crv"`
Kty string `json:"kty"`
X string `json:"x"`
Y string `json:"y"`
D string `json:"d,omitempty"`
Keyops []string `json:"key_ops,omitempty"`
}
// ECDSAFIO ecdsa fio
type ECDSAFIO struct {
Keys []json.RawMessage `json:"keys"`
}
// KeyFIO key fio
type KeyFIO struct {
Pubkey string `json:"pubkey"`
Privkey string `json:"privkey"`
KeyUID string `json:"keyuid"`
KType string `json:"ktype"`
}
// LoadFioContent load a fio content
func LoadFioContent(content string) (ktype, keyuid, privkey, pubkey string, err error) {
keys := make([]KeyFIO, 0)
err = json.Unmarshal([]byte(content), &keys)
if err != nil {
j, _ := json.Marshal(&keys)
fmt.Print(string(j))
return "", "", "", "", NewFBKError("invalid format file", InvalidKey)
}
// try to load the first one
for _, key := range keys {
if key.KType != "pgp" && key.KType != "ecdsa" {
return "", "", "", "", NewFBKError("invalid ktype", InvalidKey)
}
return key.KType, key.KeyUID, key.Privkey, key.Pubkey, nil
}
return "", "", "", "", NewFBKError("no key in fio file", InvalidKey)
}
// LoadFioFile load a fio file
func LoadFioFile(filepath string) (string, string, string, string, error) {
content, err := ioutil.ReadFile(filepath)
if err != nil {
msg := fmt.Sprintf(`No file at %s`, filepath)
return "", "", "", "", NewFBKError(msg, InvalidFile)
}
return LoadFioContent(string(content))
}
// LoadB64U load a base64url private key
func LoadB64U(data string) (string, string, error) {
content, err := base64.RawURLEncoding.DecodeString(data)
if err != nil {
return "", "", NewFBKError("can't decode key", InvalidKey)
}
regexPriv, _ := regexp.Compile(regexPGPPrivKey)
priv := regexPriv.FindStringSubmatch(string(content))
if len(priv) > 0 {
fp, err := PGPFingerprint(priv[1])
if err != nil {
return "", "", NewFBKError("Malformed private pgp key", InvalidKey)
}
keyuid := PGPToB32(fp)
return keyuid, priv[1], nil
}
var k ECDSAJWK
err = json.Unmarshal(content, &k)
if err != nil {
return "", "", NewFBKError("unknown format", InvalidKey)
}
if k.D == "" {
return "", "", NewFBKError("Not a private ECDSA key", InvalidKey)
}
fp, err := ECDSAFingerprint(string(content))
if err != nil {
return "", "", NewFBKError("no fingerprint", InvalidKey)
}
return fp, string(content), nil
}
// VerifySignature verify signature
func VerifySignature(ktype, pubkey, message, signature string) (bool, error) {
if ktype == "ecdsa" {
return ECDSAVerify(pubkey, message, signature)
} else if ktype == "pgp" {
return PGPVerify(pubkey, message, signature)
}
msg := fmt.Sprintf("Invalid key type %s", ktype)
return false, NewFBKError(msg, InvalidKey)
}
// ListFilesInDirectory recursive
func ListFilesInDirectory(root string) ([]string, error) {
var files []string
err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
if !info.IsDir() {
files = append(files, path)
}
return nil
})
return files, err
}