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 var key ownrsa.RSA
62 //search for complete key
63 for _, k := range keys {
64 if k.PubK == packPubK {
65 key = ownrsa.UnpackKey(k)
66 }
67 }
68 //blind the key.PubK
69 var m []int
70 //convert packPubK to []bytes
71 mBytes := []byte(packPubK)
72 for _, byte := range mBytes {
73 m = append(m, int(byte))
74 }
75 rVal := 101
76 blinded := ownrsa.Blind(m, rVal, key.PubK, key.PrivK)
77 fmt.Println(blinded)
78
79 //convert blinded to string
80 var askBlindSign AskBlindSign
81 askBlindSign.M = ownrsa.ArrayIntToString(blinded, "_")
82
83 //send to the serverIDsigner the key.PubK blinded
84 color.Green(askBlindSign.M)
85 body := new(bytes.Buffer)
86 json.NewEncoder(body).Encode(askBlindSign)
87 res, err := http.Post("http://"+config.ServerIDSigner.IP+":"+config.ServerIDSigner.Port+"/blindsign", "application/json", body)
88 check(err)
89 fmt.Println(res)
90
91 decoder := json.NewDecoder(res.Body)
92 //var sigmaString string
93 err = decoder.Decode(&askBlindSign)
94 if err != nil {
95 panic(err)
96 }
97 defer r.Body.Close()
98
99 fmt.Println("sigmaString")
100 fmt.Println(askBlindSign)
101 sigma := ownrsa.StringToArrayInt(askBlindSign.M, "_")
102 fmt.Println(sigma)
103
104 //get the serverIDsigner pubK
105 serverPubK := getServerPubK("http://" + config.ServerIDSigner.IP + ":" + config.ServerIDSigner.Port)
106
107 //unblind the response
108 mSigned := ownrsa.Unblind(sigma, rVal, serverPubK)
109 fmt.Print("mSigned: ")
110 fmt.Println(mSigned)
111
112 verified := ownrsa.Verify(m, mSigned, serverPubK)
113 fmt.Println(verified)
114
115 var iKey int
116 for i, k := range keys {
117 if k.PubK == packPubK {
118 iKey = i
119 //save to k the key updated
120 k.PubKSigned = ownrsa.ArrayIntToString(mSigned, "_")
121 k.Verified = verified
122 }
123 fmt.Println(k)
124 }
125 keys[iKey].PubKSigned = ownrsa.ArrayIntToString(mSigned, "_")
126 keys[iKey].Verified = verified
127 fmt.Println(keys)
128 saveKeys(keys, "keys.json")
129
130 jResp, err := json.Marshal(keys)
131 check(err)
132 fmt.Fprintln(w, string(jResp))
133}
134
135func Verify(w http.ResponseWriter, r *http.Request) {
136 vars := mux.Vars(r)
137 packPubK := vars["pubK"]
138 color.Green(packPubK)
139
140 //read the keys stored in /keys directory
141 keys := readKeys("keys.json")
142
143 var key ownrsa.PackRSA
144 //search for complete key
145 for _, k := range keys {
146 if k.PubK == packPubK {
147 key = k
148 }
149 }
150
151 //get the serverIDsigner pubK
152 serverPubK := getServerPubK("http://" + config.ServerIDSigner.IP + ":" + config.ServerIDSigner.Port)
153 m := ownrsa.StringToArrayInt(key.PubK, "_")
154 mSigned := ownrsa.StringToArrayInt(key.PubKSigned, "_")
155
156 verified := ownrsa.Verify(m, mSigned, serverPubK)
157 fmt.Println(verified)
158
159 for _, k := range keys {
160 if k.PubK == packPubK {
161 //save to k the key updated
162 k.PubKSigned = ownrsa.ArrayIntToString(mSigned, "_")
163 k.Verified = verified
164 }
165 }
166 saveKeys(keys, "keys.json")
167
168 jResp, err := json.Marshal(keys)
169 check(err)
170 fmt.Fprintln(w, string(jResp))
171}