forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.go
559 lines (483 loc) · 13.3 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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
package otr3
import (
"bufio"
"bytes"
"crypto/aes"
"crypto/cipher"
"crypto/dsa"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"math/big"
"os"
"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/twstrike/otr3/sexp"
)
// PublicKey is a public key used to verify signed messages
type PublicKey interface {
Parse([]byte) ([]byte, bool)
Fingerprint() []byte
Verify([]byte, []byte) ([]byte, bool)
serialize() []byte
IsSame(PublicKey) bool
}
// PrivateKey is a private key used to sign messages
type PrivateKey interface {
Parse([]byte) ([]byte, bool)
Serialize() []byte
Sign(io.Reader, []byte) ([]byte, error)
Generate(io.Reader) error
PublicKey() PublicKey
IsAvailableForVersion(uint16) bool
}
// GenerateMissingKeys will look through the existing serialized keys and generate new keys to ensure that the functioning of this version of OTR will work correctly. It will only return the newly generated keys, not the old ones
func GenerateMissingKeys(existing [][]byte) ([]PrivateKey, error) {
var result []PrivateKey
hasDSA := false
for _, x := range existing {
_, typeTag, ok := extractShort(x)
if ok && typeTag == dsaKeyTypeValue {
hasDSA = true
}
}
if !hasDSA {
var priv DSAPrivateKey
if err := priv.Generate(rand.Reader); err != nil {
return nil, err
}
result = append(result, &priv)
}
return result, nil
}
// DSAPublicKey is a DSA public key
type DSAPublicKey struct {
dsa.PublicKey
}
// DSAPrivateKey is a DSA private key
type DSAPrivateKey struct {
DSAPublicKey
dsa.PrivateKey
}
// Account is a holder for the private key associated with an account
// It contains name, protocol and otr private key of an otr Account
type Account struct {
Name string
Protocol string
Key PrivateKey
}
func readSymbolAndExpect(r *bufio.Reader, s string) bool {
res, ok := readPotentialSymbol(r)
return ok && res == s
}
func readPotentialBigNum(r *bufio.Reader) (*big.Int, bool) {
res, _ := sexp.ReadValue(r)
if res != nil {
if tres, ok := res.(sexp.BigNum); ok {
return tres.Value().(*big.Int), true
}
}
return nil, false
}
func readPotentialSymbol(r *bufio.Reader) (string, bool) {
res, _ := sexp.ReadValue(r)
if res != nil {
if tres, ok := res.(sexp.Symbol); ok {
return tres.Value().(string), true
}
}
return "", false
}
func readPotentialString(r *bufio.Reader) (string, bool) {
res, _ := sexp.ReadValue(r)
if res != nil {
if tres, ok := res.(sexp.Sstring); ok {
return tres.Value().(string), true
}
}
return "", false
}
// ImportKeysFromFile will read the libotr formatted file given and return all accounts defined in it
func ImportKeysFromFile(fname string) ([]*Account, error) {
f, err := os.Open(fname)
if err != nil {
return nil, err
}
defer f.Close()
return ImportKeys(f)
}
// ExportKeysToFile will create the named file (or truncate it) and write all the accounts to that file in libotr format.
func ExportKeysToFile(acs []*Account, fname string) error {
f, err := os.Create(fname)
if err != nil {
return err
}
defer f.Close()
exportAccounts(acs, f)
return nil
}
// ImportKeys will read the libotr formatted data given and return all accounts defined in it
func ImportKeys(r io.Reader) ([]*Account, error) {
res, ok := readAccounts(bufio.NewReader(r))
if !ok {
return nil, newOtrError("couldn't import data into private key")
}
return res, nil
}
func assignParameter(k *dsa.PrivateKey, s string, v *big.Int) bool {
switch s {
case "g":
k.G = v
case "p":
k.P = v
case "q":
k.Q = v
case "x":
k.X = v
case "y":
k.Y = v
default:
return false
}
return true
}
func readAccounts(r *bufio.Reader) ([]*Account, bool) {
sexp.ReadListStart(r)
ok1 := readSymbolAndExpect(r, "privkeys")
ok2 := true
var as []*Account
for {
a, ok, atEnd := readAccount(r)
ok2 = ok2 && ok
if atEnd {
break
}
as = append(as, a)
}
ok3 := sexp.ReadListEnd(r)
return as, ok1 && ok2 && ok3
}
func readAccountName(r *bufio.Reader) (string, bool) {
sexp.ReadListStart(r)
ok1 := readSymbolAndExpect(r, "name")
nm, ok2 := readPotentialString(r)
ok3 := sexp.ReadListEnd(r)
return nm, ok1 && ok2 && ok3
}
func readAccountProtocol(r *bufio.Reader) (string, bool) {
sexp.ReadListStart(r)
ok1 := readSymbolAndExpect(r, "protocol")
nm, ok2 := readPotentialSymbol(r)
ok3 := sexp.ReadListEnd(r)
return nm, ok1 && ok2 && ok3
}
func readAccount(r *bufio.Reader) (a *Account, ok bool, atEnd bool) {
if !sexp.ReadListStart(r) {
return nil, true, true
}
ok1 := readSymbolAndExpect(r, "account")
a = new(Account)
var ok2, ok3, ok4 bool
a.Name, ok2 = readAccountName(r)
a.Protocol, ok3 = readAccountProtocol(r)
a.Key, ok4 = readPrivateKey(r)
ok5 := sexp.ReadListEnd(r)
return a, ok1 && ok2 && ok3 && ok4 && ok5, false
}
func readPrivateKey(r *bufio.Reader) (PrivateKey, bool) {
sexp.ReadListStart(r)
ok1 := readSymbolAndExpect(r, "private-key")
k := new(DSAPrivateKey)
res, ok2 := readDSAPrivateKey(r)
if ok2 {
k.PrivateKey = *res
k.DSAPublicKey.PublicKey = k.PrivateKey.PublicKey
}
ok3 := sexp.ReadListEnd(r)
return k, ok1 && ok2 && ok3
}
func readDSAPrivateKey(r *bufio.Reader) (*dsa.PrivateKey, bool) {
sexp.ReadListStart(r)
ok1 := readSymbolAndExpect(r, "dsa")
k := new(dsa.PrivateKey)
for {
tag, value, end, ok := readParameter(r)
if !ok {
return nil, false
}
if end {
break
}
if !assignParameter(k, tag, value) {
return nil, false
}
}
ok2 := sexp.ReadListEnd(r)
return k, ok1 && ok2
}
func readParameter(r *bufio.Reader) (tag string, value *big.Int, end bool, ok bool) {
if !sexp.ReadListStart(r) {
return "", nil, true, true
}
tag, ok1 := readPotentialSymbol(r)
value, ok2 := readPotentialBigNum(r)
ok = ok1 && ok2
end = false
if !sexp.ReadListEnd(r) {
return "", nil, true, true
}
return
}
// IsAvailableForVersion returns true if this key is possible to use with the given version
func (pub *DSAPublicKey) IsAvailableForVersion(v uint16) bool {
return v == 2 || v == 3
}
// IsSame returns true if the given public key is a DSA public key that is equal to this key
func (pub *DSAPublicKey) IsSame(other PublicKey) bool {
oth, ok := other.(*DSAPublicKey)
return ok && pub == oth
}
// ParsePrivateKey is an algorithm indepedent way of parsing private keys
func ParsePrivateKey(in []byte) (index []byte, ok bool, key PrivateKey) {
var typeTag uint16
index, typeTag, ok = extractShort(in)
if !ok {
return in, false, nil
}
switch typeTag {
case dsaKeyTypeValue:
key = &DSAPrivateKey{}
index, ok = key.Parse(in)
return
}
return in, false, nil
}
// ParsePublicKey is an algorithm independent way of parsing public keys
func ParsePublicKey(in []byte) (index []byte, ok bool, key PublicKey) {
var typeTag uint16
index, typeTag, ok = extractShort(in)
if !ok {
return in, false, nil
}
switch typeTag {
case dsaKeyTypeValue:
key = &DSAPublicKey{}
index, ok = key.Parse(in)
return
}
return in, false, nil
}
// Parse takes the given data and tries to parse it into the PublicKey receiver. It will return not ok if the data is malformed or not for a DSA key
func (pub *DSAPublicKey) Parse(in []byte) (index []byte, ok bool) {
var typeTag uint16
if index, typeTag, ok = extractShort(in); !ok || typeTag != dsaKeyTypeValue {
return in, false
}
if index, pub.P, ok = extractMPI(index); !ok {
return in, false
}
if index, pub.Q, ok = extractMPI(index); !ok {
return in, false
}
if index, pub.G, ok = extractMPI(index); !ok {
return in, false
}
if index, pub.Y, ok = extractMPI(index); !ok {
return in, false
}
return
}
// Parse will parse a Private Key from the given data, by first parsing the public key components and then the private key component. It returns not ok for the same reasons as PublicKey.Parse.
func (priv *DSAPrivateKey) Parse(in []byte) (index []byte, ok bool) {
if in, ok = priv.DSAPublicKey.Parse(in); !ok {
return nil, false
}
priv.PrivateKey.PublicKey = priv.DSAPublicKey.PublicKey
index, priv.X, ok = extractMPI(in)
return index, ok
}
var dsaKeyType = []byte{0x00, 0x00}
var dsaKeyTypeValue = uint16(0x0000)
func (priv *DSAPrivateKey) serialize() []byte {
result := priv.DSAPublicKey.serialize()
return appendMPI(result, priv.PrivateKey.X)
}
// Serialize will return the serialization of the private key to a byte array
func (priv *DSAPrivateKey) Serialize() []byte {
return priv.serialize()
}
func (pub *DSAPublicKey) serialize() []byte {
if pub.P == nil || pub.Q == nil || pub.G == nil || pub.Y == nil {
return nil
}
result := dsaKeyType
result = appendMPI(result, pub.P)
result = appendMPI(result, pub.Q)
result = appendMPI(result, pub.G)
result = appendMPI(result, pub.Y)
return result
}
// Fingerprint will generate a fingerprint of the serialized version of the key using the provided hash.
func (pub *DSAPublicKey) Fingerprint() []byte {
b := pub.serialize()
if b == nil {
return nil
}
h := fingerprintHashInstanceForVersion(3)
h.Write(b[2:]) // if public key is DSA, ignore the leading 0x00 0x00 for the key type (according to spec)
return h.Sum(nil)
}
// Sign will generate a signature of a hashed data using dsa Sign.
func (priv *DSAPrivateKey) Sign(rand io.Reader, hashed []byte) ([]byte, error) {
r, s, err := dsa.Sign(rand, &priv.PrivateKey, hashed)
if err == nil {
rBytes := r.Bytes()
sBytes := s.Bytes()
out := make([]byte, 40)
copy(out[20-len(rBytes):], rBytes)
copy(out[len(out)-len(sBytes):], sBytes)
return out, nil
}
return nil, err
}
// Verify will verify a signature of a hashed data using dsa Verify.
func (pub *DSAPublicKey) Verify(hashed, sig []byte) (nextPoint []byte, sigOk bool) {
if len(sig) < 2*20 {
return nil, false
}
r := new(big.Int).SetBytes(sig[:20])
s := new(big.Int).SetBytes(sig[20:40])
ok := dsa.Verify(&pub.PublicKey, hashed, r, s)
return sig[20*2:], ok
}
func counterEncipher(key, iv, src, dst []byte) error {
aesCipher, err := aes.NewCipher(key)
if err != nil {
return err
}
ctr := cipher.NewCTR(aesCipher, iv)
ctr.XORKeyStream(dst, src)
return nil
}
func encrypt(key, data []byte) (dst []byte, err error) {
dst = make([]byte, len(data))
err = counterEncipher(key, dst[:aes.BlockSize], data, dst)
return
}
func decrypt(key, dst, src []byte) error {
return counterEncipher(key, make([]byte, aes.BlockSize), src, dst)
}
// Import parses the contents of a libotr private key file.
func (priv *DSAPrivateKey) Import(in []byte) bool {
mpiStart := []byte(" #")
mpis := make([]*big.Int, 5)
for i := 0; i < len(mpis); i++ {
start := bytes.Index(in, mpiStart)
if start == -1 {
return false
}
in = in[start+len(mpiStart):]
end := bytes.IndexFunc(in, notHex)
if end == -1 {
return false
}
hexBytes := in[:end]
in = in[end:]
if len(hexBytes)&1 != 0 {
return false
}
mpiBytes := make([]byte, len(hexBytes)/2)
if _, err := hex.Decode(mpiBytes, hexBytes); err != nil {
return false
}
mpis[i] = new(big.Int).SetBytes(mpiBytes)
}
priv.PrivateKey.P = mpis[0]
priv.PrivateKey.Q = mpis[1]
priv.PrivateKey.G = mpis[2]
priv.PrivateKey.Y = mpis[3]
priv.PrivateKey.X = mpis[4]
priv.DSAPublicKey.PublicKey = priv.PrivateKey.PublicKey
a := new(big.Int).Exp(priv.PrivateKey.G, priv.PrivateKey.X, priv.PrivateKey.P)
return a.Cmp(priv.PrivateKey.Y) == 0
}
// Generate will generate a new DSA Private Key with the randomness provided. The parameter size used is 1024 and 160.
func (priv *DSAPrivateKey) Generate(rand io.Reader) error {
if err := dsa.GenerateParameters(&priv.PrivateKey.PublicKey.Parameters, rand, dsa.L1024N160); err != nil {
return err
}
if err := dsa.GenerateKey(&priv.PrivateKey, rand); err != nil {
return err
}
priv.DSAPublicKey.PublicKey = priv.PrivateKey.PublicKey
return nil
}
// PublicKey returns the public key corresponding to this private key
func (priv *DSAPrivateKey) PublicKey() PublicKey {
return &priv.DSAPublicKey
}
func notHex(r rune) bool {
if r >= '0' && r <= '9' ||
r >= 'a' && r <= 'f' ||
r >= 'A' && r <= 'F' {
return false
}
return true
}
func exportName(n string, w *bufio.Writer) {
indent := " "
w.WriteString(indent)
w.WriteString("(name \"")
w.WriteString(n)
w.WriteString("\")\n")
}
func exportProtocol(n string, w *bufio.Writer) {
indent := " "
w.WriteString(indent)
w.WriteString("(protocol ")
w.WriteString(n)
w.WriteString(")\n")
}
func exportPrivateKey(key PrivateKey, w *bufio.Writer) {
indent := " "
w.WriteString(indent)
w.WriteString("(private-key\n")
exportDSAPrivateKey(key.(*DSAPrivateKey), w)
w.WriteString(indent)
w.WriteString(")\n")
}
func exportDSAPrivateKey(key *DSAPrivateKey, w *bufio.Writer) {
indent := " "
w.WriteString(indent)
w.WriteString("(dsa\n")
exportParameter("p", key.PrivateKey.P, w)
exportParameter("q", key.PrivateKey.Q, w)
exportParameter("g", key.PrivateKey.G, w)
exportParameter("y", key.PrivateKey.Y, w)
exportParameter("x", key.PrivateKey.X, w)
w.WriteString(indent)
w.WriteString(")\n")
}
func exportParameter(name string, val *big.Int, w *bufio.Writer) {
indent := " "
w.WriteString(indent)
w.WriteString(fmt.Sprintf("(%s #%X#)\n", name, val))
}
func exportAccount(a *Account, w *bufio.Writer) {
indent := " "
w.WriteString(indent)
w.WriteString("(account\n")
exportName(a.Name, w)
exportProtocol(a.Protocol, w)
exportPrivateKey(a.Key, w)
w.WriteString(indent)
w.WriteString(")\n")
}
func exportAccounts(as []*Account, w io.Writer) {
bw := bufio.NewWriter(w)
bw.WriteString("(privkeys\n")
for _, a := range as {
exportAccount(a, bw)
}
bw.WriteString(")\n")
bw.Flush()
}