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