blockchainIDsystem/clientApp/clientAppRESTFunctions.go
clientApp/clientAppRESTFunctions.goRaw
1package main
2
3import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "net/http"
8 "time"
9
10 ownrsa "./ownrsa"
11 "github.com/fatih/color"
12 "github.com/gorilla/mux"
13)
14
15//generate key pair
16//blind m
17//unblind m
18
19func Index(w http.ResponseWriter, r *http.Request) {
20 fmt.Fprintln(w, "serverIDsigner")
21}
22
23func IDs(w http.ResponseWriter, r *http.Request) {
24 //read the keys stored in /keys directory
25 keys := readKeys("keys.json")
26 saveKeys(keys, "keys.json")
27
28 jResp, err := json.Marshal(keys)
29 check(err)
30 fmt.Fprintln(w, string(jResp))
31}
32func NewID(w http.ResponseWriter, r *http.Request) {
33 //generate RSA keys pair
34 newKey := ownrsa.GenerateKeyPair()
35
36 key := ownrsa.PackKey(newKey)
37 key.Date = time.Now()
38 fmt.Println(key)
39
40 keys := readKeys("keys.json")
41 keys = append(keys, key)
42 saveKeys(keys, "keys.json")
43
44 jResp, err := json.Marshal(keys)
45 check(err)
46 fmt.Fprintln(w, string(jResp))
47}
48
49type AskBlindSign struct {
50 M string `json:"m"`
51}
52
53func BlindAndSendToSign(w http.ResponseWriter, r *http.Request) {
54 vars := mux.Vars(r)
55 packPubK := vars["pubK"]
56 color.Green(packPubK)
57
58 //read the keys stored in /keys directory
59 keys := readKeys("keys.json")
60
61 /*
62 var key ownrsa.RSA
63 //search for complete key
64 for _, k := range keys {
65 if k.PubK == packPubK {
66 key = ownrsa.UnpackKey(k)
67 }
68 }
69 */
70
71 //get the serverIDsigner pubK
72 serverPubK := getServerPubK("http://" + config.ServerIDSigner.IP + ":" + config.ServerIDSigner.Port)
73
74 //blind the key.PubK
75 var m []int
76 //convert packPubK to []bytes
77 mBytes := []byte(packPubK)
78 for _, byte := range mBytes {
79 m = append(m, int(byte))
80 }
81 rVal := 101
82 blinded := ownrsa.Blind(m, rVal, serverPubK)
83 fmt.Println(blinded)
84
85 //convert blinded to string
86 var askBlindSign AskBlindSign
87 askBlindSign.M = ownrsa.ArrayIntToString(blinded, "_")
88
89 //send to the serverIDsigner the key.PubK blinded
90 color.Green(askBlindSign.M)
91 body := new(bytes.Buffer)
92 json.NewEncoder(body).Encode(askBlindSign)
93 res, err := http.Post("http://"+config.ServerIDSigner.IP+":"+config.ServerIDSigner.Port+"/blindsign", "application/json", body)
94 check(err)
95 fmt.Println(res)
96
97 decoder := json.NewDecoder(res.Body)
98 //var sigmaString string
99 err = decoder.Decode(&askBlindSign)
100 if err != nil {
101 panic(err)
102 }
103 defer r.Body.Close()
104
105 fmt.Println("sigmaString")
106 fmt.Println(askBlindSign)
107 sigma := ownrsa.StringToArrayInt(askBlindSign.M, "_")
108 fmt.Println(sigma)
109
110 //unblind the response
111 mSigned := ownrsa.Unblind(sigma, rVal, serverPubK)
112 fmt.Print("mSigned: ")
113 fmt.Println(mSigned)
114
115 verified := ownrsa.Verify(m, mSigned, serverPubK)
116 fmt.Println(verified)
117
118 var iKey int
119 for i, k := range keys {
120 if k.PubK == packPubK {
121 iKey = i
122 //save to k the key updated
123 k.PubKSigned = ownrsa.ArrayIntToString(mSigned, "_")
124 k.Verified = verified
125 }
126 fmt.Println(k)
127 }
128 keys[iKey].PubKSigned = ownrsa.ArrayIntToString(mSigned, "_")
129 keys[iKey].Verified = verified
130 fmt.Println(keys)
131 saveKeys(keys, "keys.json")
132
133 jResp, err := json.Marshal(keys)
134 check(err)
135 fmt.Fprintln(w, string(jResp))
136}
137
138func Verify(w http.ResponseWriter, r *http.Request) {
139 vars := mux.Vars(r)
140 packPubK := vars["pubK"]
141 color.Green(packPubK)
142
143 //read the keys stored in /keys directory
144 keys := readKeys("keys.json")
145
146 var key ownrsa.PackRSA
147 //search for complete key
148 for _, k := range keys {
149 if k.PubK == packPubK {
150 key = k
151 }
152 }
153
154 //get the serverIDsigner pubK
155 serverPubK := getServerPubK("http://" + config.ServerIDSigner.IP + ":" + config.ServerIDSigner.Port)
156 m := ownrsa.StringToArrayInt(key.PubK, "_")
157 mSigned := ownrsa.StringToArrayInt(key.PubKSigned, "_")
158
159 verified := ownrsa.Verify(m, mSigned, serverPubK)
160 fmt.Println(verified)
161
162 for _, k := range keys {
163 if k.PubK == packPubK {
164 //save to k the key updated
165 k.PubKSigned = ownrsa.ArrayIntToString(mSigned, "_")
166 k.Verified = verified
167 }
168 }
169 saveKeys(keys, "keys.json")
170
171 jResp, err := json.Marshal(keys)
172 check(err)
173 fmt.Fprintln(w, string(jResp))
174}