-
Notifications
You must be signed in to change notification settings - Fork 13
/
signature.go
181 lines (158 loc) · 5.33 KB
/
signature.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
176
177
178
179
180
181
// Copyright 2016 CoreOS, 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,
// 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 signature
import (
"crypto"
"crypto/rand"
"crypto/rsa"
_ "crypto/sha256"
"crypto/x509"
"encoding/pem"
"fmt"
"hash"
"github.com/coreos/pkg/capnslog"
"github.com/golang/protobuf/proto"
"github.com/coreos/mantle/update/metadata"
)
const (
signatureVersion = 2
signatureHash = crypto.SHA256
developerPubKey = `
-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzFS5uVJ+pgibcFLD3kbY
k02Edj0HXq31ZT/Bva1sLp3Ysv+QTv/ezjf0gGFfASdgpz6G+zTipS9AIrQr0yFR
+tdp1ZsHLGxVwvUoXFftdapqlyj8uQcWjjbN7qJsZu0Ett/qo93hQ5nHW7Sv5dRm
/ZsDFq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V0YYiDjjaCM
T5G4Tcs7xhBWszLMCjhpJCrwHdGk3aa65UQ+angZlxhyziULCjpb5rMCgYEA3GUb
VkqlVuNkHoogOMwg+h1jUSkwtWvP/z/FOXrKjivuwSgQ+i6PsildI3FL/WQtJxgd
arB+l0L8TZJ6spFdNXwGmdCLqEcgEBYl11EojOXYLa7oLONI41iRQ3/nBBIqC38P
Cs6CZQG/ZpKSoOzXE34BwcrOL99MA2oaVpGHuQcCgYA1IIk3Mbph8FyqOwb3rGHd
Dksdt48GXHyiUy2BixCWtS+6blA+0cLGB0/PAS07wAw/WdmiCAMR55Ml7w1Hh6m0
bkJrAK9schmhTvwUzBCJ8JLatF37f+qojQfichHJPjMKHd7KkuIGNI5XPmxXKVFA
rMwD7SpdRh28w1H7UiDsPQKBgGebnFtXohyTr2hv9K/evo32LM9ltsFC2rga6YOZ
BwoI+yeQx1JleyX9LgzQYTHQ2y0quAGE0S4YznVFLCswDQpssMm0cUL9lMQbNVTg
kViTYKoxNHKNsqE17Kw3v4l5ZIydAZxJ8qC7TphQxV+jl4RRU1AgIAf/SEO+qH0T
0yMXAoGBAN+y9QpGnGX6cgwLQQ7IC6MC+3NRed21s+KxHzpyF+Zh/q6NTLUSgp8H
dBmeF4wAZTY+g/fdB9drYeaSdRs3SZsM7gMEvjspjYgE2rV/5gkncFyGKRAiNOR4
bsy1Gm/UYLTc8+S3fq/xjg9RCjW9JMwavAwL6oVNNt7nyAXPfvSu
-----END RSA PRIVATE KEY-----
`
)
var (
plog = capnslog.NewPackageLogger("github.com/coreos/mantle", "update/signature")
)
func NewSignatureHash() hash.Hash {
return signatureHash.New()
}
func keySize() (int, error) {
pemBlock, _ := pem.Decode([]byte(developerPubKey))
if pemBlock == nil {
return 0, fmt.Errorf("unable to parse key")
}
somePub, err := x509.ParsePKIXPublicKey(pemBlock.Bytes)
if err != nil {
return 0, err
}
rsaPub, ok := somePub.(*rsa.PublicKey)
if !ok {
return 0, fmt.Errorf("unexpected key type %T", somePub)
}
// This is how the rsa package computes key length.
return (rsaPub.N.BitLen() + 7) / 8, nil
}
func SignaturesSize() (int, error) {
dataLen, err := keySize()
if err != nil {
return 0, err
}
data := make([]byte, dataLen)
sigs := &metadata.Signatures{
Signatures: []*metadata.Signatures_Signature{
&metadata.Signatures_Signature{
Version: proto.Uint32(signatureVersion),
Data: data,
},
},
}
return proto.Size(sigs), nil
}
func Sign(sum []byte) (*metadata.Signatures, error) {
pemBlock, _ := pem.Decode([]byte(developerSecKey))
if pemBlock == nil {
return nil, fmt.Errorf("unable to parse key")
}
rsaKey, err := x509.ParsePKCS1PrivateKey(pemBlock.Bytes)
if err != nil {
return nil, err
}
sig, err := rsa.SignPKCS1v15(rand.Reader, rsaKey, signatureHash, sum)
if err != nil {
return nil, err
}
return &metadata.Signatures{
Signatures: []*metadata.Signatures_Signature{
&metadata.Signatures_Signature{
Version: proto.Uint32(signatureVersion),
Data: sig,
},
},
}, nil
}
func VerifySignature(sum []byte, sigs *metadata.Signatures) error {
pemBlock, _ := pem.Decode([]byte(developerPubKey))
if pemBlock == nil {
return fmt.Errorf("unable to parse key")
}
somePub, err := x509.ParsePKIXPublicKey(pemBlock.Bytes)
if err != nil {
return err
}
rsaPub, ok := somePub.(*rsa.PublicKey)
if !ok {
return fmt.Errorf("unexpected key type %T", somePub)
}
for _, sig := range sigs.Signatures {
v := sig.GetVersion()
if v != signatureVersion {
plog.Debugf("Skipping v%d signature", v)
continue
}
if err := rsa.VerifyPKCS1v15(rsaPub, signatureHash, sum, sig.Data); err != nil {
plog.Debugf("Cannot verify v%d signature with dev key", v)
} else {
plog.Infof("Good v%d signature by dev key", v)
return nil
}
}
return fmt.Errorf("no valid signatures found")
}