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.

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