You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
4.0 KiB

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