forked from netsec-ethz/scion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen.go
144 lines (133 loc) · 4.21 KB
/
gen.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
// Copyright 2018 ETH Zurich
//
// 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 keys
import (
"bytes"
"crypto/rand"
"encoding/base64"
"io"
"os"
"path/filepath"
"golang.org/x/crypto/ed25519"
"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/keyconf"
"github.com/scionproto/scion/go/lib/scrypto"
"github.com/scionproto/scion/go/lib/serrors"
"github.com/scionproto/scion/go/tools/scion-pki/internal/conf"
"github.com/scionproto/scion/go/tools/scion-pki/internal/pkicmn"
)
func runGenKey(args []string) {
asMap, err := pkicmn.ProcessSelector(args[0])
if err != nil {
pkicmn.ErrorAndExit("Error: %s\n", err)
}
for isd, ases := range asMap {
iconf, err := conf.LoadIsdConf(pkicmn.GetIsdPath(pkicmn.RootDir, isd))
if err != nil {
pkicmn.ErrorAndExit("Error reading isd.ini: %s\n", err)
}
for _, ia := range ases {
dir := pkicmn.GetAsPath(pkicmn.OutDir, ia)
core := pkicmn.Contains(iconf.Trc.CoreIAs, ia)
pkicmn.QuietPrint("Generating keys for %s\n", ia)
if err = genAll(filepath.Join(dir, pkicmn.KeysDir), core); err != nil {
pkicmn.ErrorAndExit("Error generating keys: %s\n", err)
}
}
}
os.Exit(0)
}
func genAll(outDir string, core bool) error {
// Generate AS sigining and decryption keys.
if err := genKey(keyconf.SigKeyFile, outDir, genSignKey); err != nil {
return err
}
if err := genKey(keyconf.DecKeyFile, outDir, genEncKey); err != nil {
return err
}
// Generate AS master keys.
if err := genKey(keyconf.MasterKey0, outDir, genMasterKey); err != nil {
return err
}
if err := genKey(keyconf.MasterKey1, outDir, genMasterKey); err != nil {
return err
}
if !core {
return nil
}
// Generate core signing key.
if err := genKey(keyconf.IssSigKeyFile, outDir, genSignKey); err != nil {
return err
}
// Generate offline and online root keys if core was specified.
if err := genKey(keyconf.OffKeyFile, outDir, genSignKey); err != nil {
return err
}
return genKey(keyconf.OnKeyFile, outDir, genSignKey)
}
type keyGenFunc func(io.Reader) ([]byte, error)
func genKey(fname, outDir string, keyGenF keyGenFunc) error {
// Check if out directory exists and if not create it.
_, err := os.Stat(outDir)
if os.IsNotExist(err) {
if err = os.MkdirAll(outDir, 0700); err != nil {
return common.NewBasicError("Cannot create output dir", err, "key", fname)
}
} else if err != nil {
return common.NewBasicError("Error checking output dir", err, "key", fname)
}
// Generate the seed for the public/private key pair.
seed := make([]byte, 32)
_, err = rand.Read(seed)
if err != nil {
return common.NewBasicError("Error generating key seed", err, "key", fname)
}
// Generate a fresh public/private key pair based on seed.
privKey, err := keyGenF(bytes.NewReader(seed))
if err != nil {
return common.NewBasicError("Error generating keys", err, "key", fname)
}
// Write private key to file.
privKeyPath := filepath.Join(outDir, fname)
privKeyEnc := base64.StdEncoding.EncodeToString(privKey)
if err = pkicmn.WriteToFile([]byte(privKeyEnc), privKeyPath, 0600); err != nil {
return common.NewBasicError("Cannot write key file", err, "key", fname)
}
return nil
}
func genSignKey(rand io.Reader) ([]byte, error) {
_, private, err := scrypto.GenKeyPair(scrypto.Ed25519)
if err != nil {
return nil, err
}
return ed25519.PrivateKey(private).Seed(), nil
}
func genEncKey(rand io.Reader) ([]byte, error) {
_, private, err := scrypto.GenKeyPair(scrypto.Curve25519xSalsa20Poly1305)
if err != nil {
return nil, err
}
return private, nil
}
func genMasterKey(rand io.Reader) ([]byte, error) {
key := make([]byte, 16)
n, err := rand.Read(key)
if err != nil {
return nil, err
}
if n != 16 {
return nil, serrors.New("Not enough random bytes")
}
return key, nil
}