-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsend.go
92 lines (81 loc) · 2.14 KB
/
send.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
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"github.com/rakoo/goax/pkg/ratchet"
"golang.org/x/crypto/openpgp/armor"
)
func send(peer string) {
r, err := openRatchet(peer)
if err != nil {
if err == errNoRatchet {
fmt.Fprintf(os.Stderr, "No ratchet for %s, please send this to the peer and \"receive\" what they send you back", peer)
fmt.Println("\n")
r, err := createRatchet(peer)
if err != nil {
log.Fatalf("Couldn't create ratchet for %s: %s", peer, err)
}
err = saveRatchet(r, peer)
if err != nil {
log.Fatal("Couldn't save ratchet, will have to try another time", err)
}
sendRatchet(r)
fmt.Println("")
return
} else {
log.Fatal(err)
}
}
fmt.Println("")
msg, err := ioutil.ReadAll(os.Stdin)
if err != nil {
log.Fatal("Couldn't read all stdin")
}
fmt.Println("")
cipherText, err := r.Encrypt(msg)
if err != nil {
if err == ratchet.ErrHandshakeNotComplete {
fmt.Fprintf(os.Stderr, "\nSorry, the handshake is not complete yet; you can't send any messages. Please ask %s for their key exchange material and use \"goax receive %s\" to finish handshake.\n", peer, peer)
fmt.Fprintf(os.Stderr, "Here's your own key exchange material, in case you want to send it again to them:\n\n")
sendRatchet(r)
os.Exit(1)
} else {
log.Fatal(err)
}
}
if err := saveRatchet(r, peer); err != nil {
log.Println("Couldn't save ratchet:", err)
os.Remove(path.Join("ratchets", hex.EncodeToString([]byte(peer))))
os.Exit(1)
}
if isNew(peer) {
sendRatchet(r)
}
encoder, err := armor.Encode(os.Stdout, ENCRYPTED_MESSAGE_TYPE, nil)
if err != nil {
log.Fatal("Couldn't create armor encoder: ", err)
}
io.Copy(encoder, bytes.NewReader(cipherText))
encoder.Close()
fmt.Println("")
}
func sendRatchet(r *ratchet.Ratchet) {
kx, err := r.GetKeyExchangeMaterial()
if err != nil {
log.Fatal("Couldn't get key exchange material ", err)
}
encoder, err := armor.Encode(os.Stdout, KEY_EXCHANGE_TYPE, nil)
if err != nil {
log.Fatal("Couldn't get armor encoder")
}
json.NewEncoder(encoder).Encode(kx)
encoder.Close()
fmt.Println("")
}