|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+package rpc
|
|
|
2
|
+
|
|
|
3
|
+import (
|
|
|
4
|
+ "bytes"
|
|
|
5
|
+ "crypto/rand"
|
|
|
6
|
+ "encoding/binary"
|
|
|
7
|
+ "time"
|
|
|
8
|
+
|
|
|
9
|
+ "github.com/juju/errors"
|
|
|
10
|
+)
|
|
|
11
|
+
|
|
|
12
|
+const (
|
|
|
13
|
+ RPCNonceSeqNo = -2
|
|
|
14
|
+
|
|
|
15
|
+ rpcNonceLength = 16
|
|
|
16
|
+ rpcNonceKeySelectorLength = 4
|
|
|
17
|
+ rpcNonceCryptoTSLength = 4
|
|
|
18
|
+ rpcNonceTagLength = 4
|
|
|
19
|
+ rpcNonceCryptoAESLength = 4
|
|
|
20
|
+
|
|
|
21
|
+ rpcNonceRequestLength = rpcNonceTagLength + rpcNonceKeySelectorLength +
|
|
|
22
|
+ rpcNonceCryptoAESLength + rpcNonceCryptoTSLength + rpcNonceLength
|
|
|
23
|
+)
|
|
|
24
|
+
|
|
|
25
|
+var (
|
|
|
26
|
+ rpcNonceTag = [rpcNonceTagLength]byte{0xaa, 0x87, 0xcb, 0x7a}
|
|
|
27
|
+ rpcNonceCryptoAESTag = [rpcNonceCryptoAESLength]byte{0x01, 0x00, 0x00, 0x00}
|
|
|
28
|
+)
|
|
|
29
|
+
|
|
|
30
|
+type RPCNonceRequest struct {
|
|
|
31
|
+ KeySelector [rpcNonceKeySelectorLength]byte
|
|
|
32
|
+ CryptoTS [rpcNonceCryptoTSLength]byte
|
|
|
33
|
+ Nonce [rpcNonceLength]byte
|
|
|
34
|
+}
|
|
|
35
|
+
|
|
|
36
|
+func (r *RPCNonceRequest) Bytes() []byte {
|
|
|
37
|
+ buf := &bytes.Buffer{}
|
|
|
38
|
+ buf.Grow(rpcNonceRequestLength)
|
|
|
39
|
+
|
|
|
40
|
+ buf.Write(rpcNonceTag[:])
|
|
|
41
|
+ buf.Write(r.KeySelector[:])
|
|
|
42
|
+ buf.Write(rpcNonceCryptoAESTag[:])
|
|
|
43
|
+ buf.Write(r.CryptoTS[:])
|
|
|
44
|
+ buf.Write(r.Nonce[:])
|
|
|
45
|
+
|
|
|
46
|
+ return buf.Bytes()
|
|
|
47
|
+}
|
|
|
48
|
+
|
|
|
49
|
+func NewRPCNonceRequest(proxySecret []byte) (*RPCNonceRequest, error) {
|
|
|
50
|
+ var nonce [rpcNonceLength]byte
|
|
|
51
|
+ var keySelector [rpcNonceKeySelectorLength]byte
|
|
|
52
|
+ var cryptoTS [rpcNonceCryptoTSLength]byte
|
|
|
53
|
+
|
|
|
54
|
+ if _, err := rand.Read(nonce[:]); err != nil {
|
|
|
55
|
+ return nil, errors.Annotate(err, "Cannot generate nonce")
|
|
|
56
|
+ }
|
|
|
57
|
+ copy(keySelector[:], proxySecret)
|
|
|
58
|
+
|
|
|
59
|
+ timestamp := time.Now().Truncate(time.Second).Unix() % 4294967296 // 256 ^ 4 - do not know how to name
|
|
|
60
|
+ binary.LittleEndian.PutUint32(cryptoTS[:], uint32(timestamp))
|
|
|
61
|
+
|
|
|
62
|
+ return &RPCNonceRequest{
|
|
|
63
|
+ KeySelector: keySelector,
|
|
|
64
|
+ CryptoTS: cryptoTS,
|
|
|
65
|
+ Nonce: nonce,
|
|
|
66
|
+ }, nil
|
|
|
67
|
+}
|