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