-
Notifications
You must be signed in to change notification settings - Fork 160
/
keyconf.go
90 lines (78 loc) · 2.23 KB
/
keyconf.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
// Copyright 2018 ETH Zurich, Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package keyconf
import (
"encoding/base64"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/scionproto/scion/pkg/private/common"
"github.com/scionproto/scion/pkg/private/serrors"
)
const (
MasterKey0 = "master0.key"
MasterKey1 = "master1.key"
RawKey = "raw"
)
// Errors
const (
ErrOpen common.ErrMsg = "Unable to load key"
ErrParse common.ErrMsg = "Unable to parse key file"
ErrUnknown common.ErrMsg = "Unknown algorithm"
)
func LoadKey(file string) ([]byte, error) {
return loadKey(file, RawKey)
}
// loadKey decodes a base64 encoded key stored in file and returns the raw bytes.
func loadKey(file string, algo string) ([]byte, error) {
b, err := os.ReadFile(file)
if err != nil {
return nil, serrors.Wrap(ErrOpen, err)
}
dbuf := make([]byte, base64.StdEncoding.DecodedLen(len(b)))
n, err := base64.StdEncoding.Decode(dbuf, b)
if err != nil {
return nil, serrors.Wrap(ErrParse, err)
}
dbuf = dbuf[:n]
if strings.ToLower(algo) != RawKey {
return nil, serrors.WithCtx(ErrUnknown, "algo", algo)
}
return dbuf, nil
}
type Master struct {
Key0 []byte
Key1 []byte
}
func LoadMaster(path string) (Master, error) {
var err error
m := Master{}
if m.Key0, err = loadKey(filepath.Join(path, MasterKey0), RawKey); err != nil {
return m, err
}
if m.Key1, err = loadKey(filepath.Join(path, MasterKey1), RawKey); err != nil {
return m, err
}
return m, nil
}
func (m Master) MarshalJSON() ([]byte, error) {
return []byte(`{"key0":"redacted","key1":"redacted"}`), nil
}
func (m Master) String() string {
return fmt.Sprintf("Key0:%s Key1:%s",
//XXX(roosd): Uncomment for debugging.
//m.Key0, m.Key1
"<redacted>", "<redacted>")
}