-
Notifications
You must be signed in to change notification settings - Fork 164
/
key.go
175 lines (148 loc) · 4.96 KB
/
key.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
package config
import (
"crypto/cipher"
"errors"
"log"
"os"
"gopkg.in/dedis/crypto.v0/abstract"
"gopkg.in/dedis/crypto.v0/base64"
"gopkg.in/dedis/crypto.v0/random"
"gopkg.in/dedis/crypto.v0/util"
)
// KeyPair represents a public/private keypair
// together with the ciphersuite the key was generated from.
type KeyPair struct {
Suite abstract.Suite // Ciphersuite this keypair is for
Public abstract.Point // Public key
Secret abstract.Scalar // Secret key
}
// NewKeyPair directly creates a secret/public key pair
func NewKeyPair(suite abstract.Suite) *KeyPair {
kp := new(KeyPair)
kp.Gen(suite, random.Stream)
return kp
}
// Generate a fresh public/private keypair with the given ciphersuite,
// using a given source of cryptographic randomness.
func (p *KeyPair) Gen(suite abstract.Suite, random cipher.Stream) {
p.Suite = suite
p.Secret = suite.NewKey(random)
p.Public = suite.Point().Mul(nil, p.Secret)
}
// PubId returns the base64-encoded HashId for this KeyPair's public key.
func (p *KeyPair) PubId() string {
buf, _ := p.Public.MarshalBinary()
hash := abstract.Sum(p.Suite, buf)
return base64.RawURLEncoding.EncodeToString(hash)
}
// Keys represents a set of public/private keypairs
// an application is configured to use to identify itself.
// The caller should embed an instance of Keys
// in its application-specific configData struct.
type Keys []KeyInfo
// KeyInfo represents configuration data for a particular public key,
// consisting of the name of the ciphersuite the public key was generated from
// and the unpadded, base64-encoded Hash-Id of the public key itself
// using the appropriate ciphersuite's hash function.
// The corresponding private key is stored separately for security.
type KeyInfo struct {
Suite string // Name of this key's ciphersuite
PubId string // Public key's base64-encoded hash-ID
}
// Retrieve a set of public/private keypairs configured for this application.
// The caller must provide a pointer to an instance of the Keys struct,
// which should be embedded in the configData object that was passed to Load.
// If the provided defaultSuite is non-nil and no keypairs are configured yet,
// automatically creates and saves a keypair with the specified defaultSuite.
//
// If any of the configured public keys cannot be loaded for whatever reason,
// such as a key's ciphersuite becoming no-longer-supported for example,
// logs a warning but continues to load any other configured keys.
//
func (f *File) Keys(keys *Keys, suites map[string]abstract.Suite,
defaultSuite abstract.Suite) ([]KeyPair, error) {
// Read all existing configured keys
klist := *keys
pairs := make([]KeyPair, 0, len(klist))
for i := range klist {
pair, err := f.Key(&klist[i], suites)
if err != nil {
log.Printf("Cannot load public key '%v': %v",
klist[i].PubId, err.Error())
continue
}
pairs = append(pairs, pair)
}
// Create a keypair if none exists yet and we have a defaultSuite.
if len(pairs) == 0 && defaultSuite != nil {
pair, err := f.GenKey(keys, defaultSuite)
if err != nil {
return nil, err
}
pairs = append(pairs, pair)
}
return pairs, nil
}
// Retrieve a public/private keypair for a given KeyInfo configuration record.
func (f *File) Key(key *KeyInfo, suites map[string]abstract.Suite) (KeyPair, error) {
// XXX support passphrase-encrypted or system-keychain keys
// Lookup the appropriate ciphersuite for this public key.
suite := suites[key.Suite]
if suite == nil {
return KeyPair{},
errors.New("Unsupported ciphersuite '" + key.Suite + "'")
}
// Read the private key file
secname := f.dirName + "/sec-" + key.PubId
secf, err := os.Open(secname)
if err != nil {
return KeyPair{}, err
}
defer secf.Close()
p := KeyPair{}
p.Suite = suite
if err := suite.Read(secf, &p.Secret); err != nil {
return KeyPair{}, err
}
// Reconstruct and verify the public key
p.Public = suite.Point().Mul(nil, p.Secret)
if p.PubId() != key.PubId {
return KeyPair{},
errors.New("Secret does not yield public key " +
key.PubId)
}
return p, nil
}
// Generate a new public/private keypair with the given ciphersuite
// and Save it to the application's previously-loaded configuration.
func (f *File) GenKey(keys *Keys, suite abstract.Suite) (KeyPair, error) {
// Create the map if it doesn't exist
// if *keys == nil {
// *keys = make(map[string] KeyInfo)
// }
// Create a fresh public/private keypair
p := KeyPair{}
p.Gen(suite, random.Stream)
pubId := p.PubId()
// Write the private key file
secname := f.dirName + "/sec-" + pubId
r := util.Replacer{}
if err := r.Open(secname); err != nil {
return KeyPair{}, err
}
defer r.Abort()
// Write the secret key
if err := suite.Write(r.File, &p.Secret); err != nil {
return KeyPair{}, err
}
// Commit the secret key
if err := r.Commit(); err != nil {
return KeyPair{}, err
}
// Re-write the config file with the new public key
*keys = append(*keys, KeyInfo{suite.String(), pubId})
if err := f.Save(); err != nil {
return KeyPair{}, err
}
return p, nil
}