forked from wearscript/wearscript-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signature.go
162 lines (151 loc) · 3.75 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
package main
import (
"bytes"
"crypto/dsa"
"crypto/rand"
"encoding/base64"
"crypto/sha256"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"math/big"
"net/http"
)
func SignatureGenerateServerKey() *dsa.PrivateKey {
priv := dsa.PrivateKey{}
dsa.GenerateParameters(&priv.Parameters, rand.Reader, dsa.L3072N256)
dsa.GenerateKey(&priv, rand.Reader)
return &priv
}
func SignatureSign(hash []byte, priv *dsa.PrivateKey) (r, s []byte, err error) {
ri, si, err := dsa.Sign(rand.Reader, priv, hash)
if err != nil {
return nil, nil, err
}
return []byte(base64.StdEncoding.EncodeToString(ri.Bytes())), []byte(base64.StdEncoding.EncodeToString(si.Bytes())), nil
}
func SignatureVerify(hash []byte, priv *dsa.PrivateKey, r []byte, s []byte) bool {
ri := big.Int{}
si := big.Int{}
rb, err := base64.StdEncoding.DecodeString(string(r))
if err != nil {
return false
}
sb, err := base64.StdEncoding.DecodeString(string(s))
if err != nil {
return false
}
return dsa.Verify(&priv.PublicKey, hash, ri.SetBytes(rb), si.SetBytes(sb))
}
func hashScript(script []byte) []byte {
h := sha256.New()
h.Write(script)
return h.Sum(nil)
}
type ScriptSignature struct {
R string `json:"r"`
S string `json:"s"`
V int `json:"v"`
H string `json:"h"`
}
func SignatureCreateKey() error {
has, err := hasUserAttribute("", "private_key")
if err != nil {
return err
}
if has {
return nil
}
fmt.Println("Generating DSA Script Key (takes ~30-60 sec)")
priv := SignatureGenerateServerKey()
privJS, err := json.Marshal(priv)
if err != nil {
return err
}
fmt.Println("Done generating key, server running...")
return setUserAttribute("", "private_key", string(privJS))
}
func SignatureSignScript(userId string, script []byte) ([]byte, error) {
// Make script with headers
userInfoJS, err := getUserAttribute(userId, "user_info")
if err != nil {
return nil, err
}
userInfo := map[string]string{}
err = json.Unmarshal([]byte(userInfoJS), &userInfo)
if err != nil {
return nil, err
}
userProfileLink := userInfo["link"]
if userProfileLink == "" {
return nil, errors.New("Can't get user link")
}
parts := [][]byte{}
parts = append(parts, []byte("<!--"))
parts = append(parts, []byte(userProfileLink+"-->\n"))
parts = append(parts, script)
script = bytes.Join(parts, []byte(""))
// Sign script w/ header
// TODO: Cache privateKey
var priv dsa.PrivateKey
privJS, err := getUserAttribute("", "private_key")
if err != nil {
return nil, err
}
err = json.Unmarshal([]byte(privJS), &priv)
if err != nil {
return nil, err
}
r, s, err := SignatureSign(hashScript(script), &priv)
if err != nil {
return nil, err
}
ss := ScriptSignature{R: string(r), S: string(s), V: 0, H: fullUrl}
ssJS, err := json.Marshal(ss)
if err != nil {
return nil, err
}
parts = [][]byte{}
parts = append(parts, []byte("<!--"))
parts = append(parts, ssJS)
parts = append(parts, []byte("-->\n"))
parts = append(parts, script)
return bytes.Join(parts, []byte("")), nil
}
func SignatureVerifyHandler(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
// Get first line from body
splits := bytes.SplitAfterN(body, []byte("\n"), 2)
if len(splits) != 2 {
w.WriteHeader(500)
return
}
// Parse data in the form of: <!--data-->\n
splits[0] = splits[0][4 : len(splits[0])-4]
ss := ScriptSignature{}
err := json.Unmarshal(splits[0], &ss)
if err != nil {
w.WriteHeader(500)
return
}
if ss.V != 0 {
w.WriteHeader(500)
return
}
privJS, err := getUserAttribute("", "private_key")
var priv dsa.PrivateKey
if err != nil {
w.WriteHeader(500)
return
}
err = json.Unmarshal([]byte(privJS), &priv)
if err != nil {
w.WriteHeader(500)
return
}
if !SignatureVerify(hashScript(splits[1]), &priv, []byte(ss.R), []byte(ss.S)) {
w.WriteHeader(500)
return
}
}