-
Notifications
You must be signed in to change notification settings - Fork 308
/
keys.go
155 lines (138 loc) · 3.82 KB
/
keys.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
// Copyright 2020 PingCAP, Inc.
//
// 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,
// See the License for the specific language governing permissions and
// limitations under the License.
package v1manifest
import (
"crypto/sha256"
"fmt"
cjson "github.com/gibson042/canonicaljson-go"
"github.com/pingcap/errors"
"github.com/pingcap/tiup/pkg/crypto"
)
// ShortKeyIDLength is the number of bytes used for filenames
const ShortKeyIDLength = 16
// ErrorNotPrivateKey indicate that it need a private key, but the supplied is not.
var ErrorNotPrivateKey = errors.New("not a private key")
// NewKeyInfo make KeyInfo from private key, public key should be load from json
func NewKeyInfo(privKey []byte) *KeyInfo {
// TODO: support other key type and scheme
return &KeyInfo{
Type: crypto.KeyTypeRSA,
Scheme: crypto.KeySchemeRSASSAPSSSHA256,
Value: map[string]string{
"private": string(privKey),
},
}
}
// GenKeyInfo generate a new private KeyInfo
func GenKeyInfo() (*KeyInfo, error) {
// TODO: support other key type and scheme
priv, err := crypto.NewKeyPair(crypto.KeyTypeRSA, crypto.KeySchemeRSASSAPSSSHA256)
if err != nil {
return nil, err
}
bytes, err := priv.Serialize()
if err != nil {
return nil, err
}
return NewKeyInfo(bytes), nil
}
// ID returns the hash id of the key
func (ki *KeyInfo) ID() (string, error) {
// Make sure private key and correspond public key has the same id
pk, err := ki.publicKey()
if err != nil {
return "", err
}
data, err := pk.Serialize()
if err != nil {
return "", err
}
value := map[string]string{
"public": string(data),
}
payload, err := cjson.Marshal(KeyInfo{
Type: ki.Type,
Scheme: ki.Scheme,
Value: value,
})
if err != nil {
// XXX: maybe we can assume that the error should always be nil since the KeyInfo struct is valid
return "", err
}
sum := sha256.Sum256(payload)
return fmt.Sprintf("%x", sum), nil
}
// IsPrivate detect if this is a private key
func (ki *KeyInfo) IsPrivate() bool {
return len(ki.Value["private"]) > 0
}
// Signature sign a signature with the key for payload
func (ki *KeyInfo) Signature(payload []byte) (string, error) {
pk, err := ki.privateKey()
if err != nil {
return "", err
}
return pk.Signature(payload)
}
// SignManifest wrap Signature with the param manifest
func (ki *KeyInfo) SignManifest(m ValidManifest) (string, error) {
payload, err := cjson.Marshal(m)
if err != nil {
return "", errors.Annotate(err, "marshal for signature")
}
return ki.Signature(payload)
}
// Verify check the signature is right
func (ki *KeyInfo) Verify(payload []byte, sig string) error {
pk, err := ki.publicKey()
if err != nil {
return err
}
return pk.VerifySignature(payload, sig)
}
// Public returns the public keyInfo
func (ki *KeyInfo) Public() (*KeyInfo, error) {
pk, err := ki.publicKey()
if err != nil {
return nil, err
}
bytes, err := pk.Serialize()
if err != nil {
return nil, err
}
return &KeyInfo{
Type: pk.Type(),
Scheme: pk.Scheme(),
Value: map[string]string{
"public": string(bytes),
},
}, nil
}
// publicKey returns PubKey
func (ki *KeyInfo) publicKey() (crypto.PubKey, error) {
if ki.IsPrivate() {
priv, err := ki.privateKey()
if err != nil {
return nil, err
}
return priv.Public(), nil
}
return crypto.NewPubKey(ki.Type, ki.Scheme, []byte(ki.Value["public"]))
}
// privateKey returns PrivKey
func (ki *KeyInfo) privateKey() (crypto.PrivKey, error) {
if !ki.IsPrivate() {
return nil, ErrorNotPrivateKey
}
return crypto.NewPrivKey(ki.Type, ki.Scheme, []byte(ki.Value["private"]))
}