-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
327 lines (293 loc) · 8.04 KB
/
api.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
// Package signify creates and verifies OpenBSD signify and minisign compatible signatures.
package signify
import (
"bytes"
"crypto/ed25519"
"encoding/binary"
"errors"
"strings"
)
//
// CONSTANTS
//
const (
// PrivateKeySize ...
PrivateKeySize = ed25519.PrivateKeySize
// PublicKeySize ...
PublicKeySize = ed25519.PublicKeySize
// SignatureSize ...
SignatureSize = ed25519.SignatureSize
// PrivatePublicKeyOffset ...
PrivatePublicKeyOffset = PrivateKeySize - PublicKeySize
// SeedSize ...
SeedSize = ed25519.SeedSize
// FingerPrintSize ...
FingerPrintSize = 8
// KeyAlgoSize ...
KeyAlgoSize = 2
// SeedTokenSize ...
SeedTokenSize = SeedSize + FingerPrintSize
)
// KeyAlgo ...
type KeyAlgo [KeyAlgoSize]byte
// FingerPrint ...
type FingerPrint [FingerPrintSize]byte
//
// SIGNATURE
//
// Signature ...
type Signature struct {
Base64 string
Raw RawSignature
}
// RawSignature ...
type RawSignature struct {
PKAlgo KeyAlgo
Fingerprint FingerPrint
RawSig [SignatureSize]byte
}
// NewSignature ...
func NewSignature() Signature {
return Signature{Raw: RawSignature{PKAlgo: _algoEd25519}}
}
//
// PUBLIC KEY
//
// PublicKey ...
type PublicKey struct {
Base64 string
Raw RawPublicKey
}
// RawPublicKey ...
type RawPublicKey struct {
PKAlgo KeyAlgo
Fingerprint FingerPrint
RawKey [PublicKeySize]byte
}
// NewPublicKey ...
func NewPublicKey() PublicKey {
return PublicKey{Raw: RawPublicKey{PKAlgo: _algoEd25519}}
}
// GetPubKeyFile from PublicKey encodes and assembles an OpenBSD Signify compatible publickey file
func (pub PublicKey) GetPubKeyFile(addUntrusted string) ([]byte, error) {
if pub.Base64 == _empty {
if err := pub.Encode(); err != nil {
return nil, errors.New("internal error: gen publickey file: decode public key: " + err.Error())
}
}
var s strings.Builder
s.WriteString(_untrustedComment)
s.WriteString(_pubUT)
s.WriteString(_space)
s.WriteString(addUntrusted)
s.WriteString(_linefeed)
s.WriteString(pub.Base64)
s.WriteString(_linefeed)
return []byte(s.String()), nil
}
// Encode PublicKey
func (pub *PublicKey) Encode() error {
var buf bytes.Buffer
err := binary.Write(&buf, binary.BigEndian, pub.Raw)
if err != nil {
return err
}
pub.Base64, err = enc(buf.Bytes())
if err != nil {
return err
}
return nil
}
// Decode PublicKey
func (pub *PublicKey) Decode() (err error) {
s, err := dec(pub.Base64)
if err != nil {
return err
}
if len(s) != KeyAlgoSize+FingerPrintSize+PublicKeySize {
return errors.New("invalid base64 pulic key size")
}
pub.Raw.PKAlgo, err = sliceTo2(s[:KeyAlgoSize])
if err != nil {
return err
}
pub.Raw.Fingerprint, err = sliceTo8(s[KeyAlgoSize : KeyAlgoSize+FingerPrintSize])
if err != nil {
return err
}
pub.Raw.RawKey, err = sliceTo32(s[KeyAlgoSize+FingerPrintSize:])
return err
}
//
// PRIVATE KEY
//
// PrivateKey ...
type PrivateKey struct {
Base64 string
Raw RawPrivateKey
}
// RawPrivateKey ...
type RawPrivateKey struct {
PKAlgo KeyAlgo
Fingerprint FingerPrint
RawKey [PrivateKeySize]byte
}
// NewPrivateKey ...
func NewPrivateKey() PrivateKey {
return PrivateKey{Raw: RawPrivateKey{PKAlgo: _algoEd25519}}
}
// GeneratePKFromSeed generates a new deterministic, reproduceable PrivateKey from seed
func GeneratePKFromSeed(seedToken [SeedTokenSize]byte) PrivateKey {
var err error
pk := NewPrivateKey()
pk.Raw.Fingerprint, err = sliceTo8(seedToken[ed25519.SeedSize:])
if err != nil {
panic("internal: generate keypair: " + err.Error()) // do not recover!
}
pk.Raw.RawKey, err = sliceTo64(ed25519.NewKeyFromSeed(seedToken[:SeedSize]))
if err != nil {
panic("internal: generate keypair: " + err.Error()) // do not recover!
}
msg := Message{Raw: []byte(_verifyThisKeyGenMessage)}
if err := msg.Sign(pk); err != nil {
panic("internal: generate keypair: " + err.Error()) // do not recover!
}
return pk
}
// GetPublicKey from PrivateKey
func (pk PrivateKey) GetPublicKey() PublicKey {
pub := NewPublicKey()
pub.Raw.Fingerprint = pk.Raw.Fingerprint
copy(pub.Raw.RawKey[:], pk.Raw.RawKey[PrivatePublicKeyOffset:])
return pub
}
// GetPubKeyFile from PrivateKey encodes and assembles an OpenBSD Signify compatible publickey file
func (pk PrivateKey) GetPubKeyFile(addUntrusted string) ([]byte, error) {
if len(pk.Raw.RawKey[:]) != PrivateKeySize {
return nil, errors.New("gen publickey file: no private key")
}
return pk.GetPublicKey().GetPubKeyFile(addUntrusted)
}
//
// MESSAGE
//
// Message ...
type Message struct {
Base64 string
Raw []byte
UntrustedComment string
Signature Signature
PublicKey PublicKey
}
// NewMessage ...
func NewMessage() *Message {
return &Message{Signature: NewSignature(), PublicKey: NewPublicKey()}
}
// Sign a message decodes, when nessesary, the Message and generates an encoded signature
func (msg *Message) Sign(pk PrivateKey) (err error) {
switch {
case pk.Raw.RawKey[:] == nil:
return errors.New("sign failed: no private key found")
case msg.Raw == nil:
if err := msg.Decode(); err != nil {
return errors.New("sign failed: no raw message and " + err.Error())
}
}
msg.PublicKey = pk.GetPublicKey()
msg.Signature.Raw.Fingerprint = pk.Raw.Fingerprint
switch {
case pk.Raw.PKAlgo == _algoEd25519:
msg.Signature.Raw.PKAlgo = _algoEd25519
key := ed25519.Sign(ed25519.PrivateKey(pk.Raw.RawKey[:]), msg.Raw)
if msg.Signature.Raw.RawSig, err = sliceTo64(key); err != nil {
return err
}
default:
return errors.New("unknown signature Algo")
}
if ok, err := msg.Verify(pk.GetPublicKey()); !ok {
return errors.New("sign failed: verify after sign: " + err.Error())
}
if err := msg.Signature.Encode(); err != nil {
return errors.New("sign failed: unable to encode signature: " + err.Error())
}
return nil
}
// Verify a message decodes, when nessesary, the Message and the PublicKey and verifies the signature
func (msg *Message) Verify(pub PublicKey) (bool, error) {
switch {
case pub.Raw.RawKey[:] == nil:
if err := pub.Decode(); err != nil {
return false, errors.New("verify failed: no public key found")
}
case msg.Raw == nil:
if err := msg.Decode(); err != nil {
return false, errors.New("verify failed: no raw message and " + err.Error())
}
}
if msg.Signature.Raw.Fingerprint != msg.PublicKey.Raw.Fingerprint {
return false, errors.New("fingerprint matching failed: wrong public key for message")
}
switch {
case msg.Signature.Raw.PKAlgo == _algoEd25519 && pub.Raw.PKAlgo == _algoEd25519:
if !ed25519.Verify(pub.Raw.RawKey[:], msg.Raw, msg.Signature.Raw.RawSig[:]) {
return false, errors.New("ed25519 signature verification failed")
}
default:
return false, errors.New("unknown signature Algo")
}
return true, nil
}
// GetSigFile signs an message (raw or encoded) and provides an OpenBSD Signify compatible signature file
func (msg *Message) GetSigFile(pk PrivateKey) ([]byte, error) {
if err := msg.Sign(pk); err != nil {
return nil, errors.New("generate sig file: " + err.Error())
}
return []byte(_untrustedComment + msg.UntrustedComment + _linefeed + msg.Signature.Base64 + _linefeed), nil
}
// Encode Raw Message to Base64
func (msg *Message) Encode() (err error) {
msg.Base64, err = enc(msg.Raw[:])
return err
}
// Decode Base64 Message to Raw Message
func (msg *Message) Decode() (err error) {
msg.Raw, err = dec(msg.Base64)
return err
}
//
// SIGNATURE
//
// Encode Raw Signature to Base64
func (sig *Signature) Encode() error {
var buf bytes.Buffer
err := binary.Write(&buf, binary.BigEndian, sig.Raw)
if err != nil {
return err
}
sig.Base64, err = enc(buf.Bytes())
if err != nil {
return err
}
return nil
}
// Decode Base64 Signature to Raw
func (sig *Signature) Decode() (err error) {
s, err := dec(sig.Base64)
if err != nil {
return err
}
if len(s) != KeyAlgoSize+FingerPrintSize+SignatureSize {
return errors.New("invalid base64 signature size")
}
sig.Raw.PKAlgo, err = sliceTo2(s[:KeyAlgoSize])
if err != nil {
return err
}
sig.Raw.Fingerprint, err = sliceTo8(s[KeyAlgoSize : KeyAlgoSize+FingerPrintSize])
if err != nil {
return err
}
sig.Raw.RawSig, err = sliceTo64(s[KeyAlgoSize+FingerPrintSize:])
return err
}