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.

165 lines
3.8 KiB

  1. package main
  2. import (
  3. "bytes"
  4. "crypto"
  5. "crypto/rand"
  6. "crypto/rsa"
  7. "crypto/sha1"
  8. "encoding/json"
  9. "fmt"
  10. "net/http"
  11. "os/exec"
  12. "time"
  13. "github.com/cryptoballot/fdh"
  14. "github.com/cryptoballot/rsablind"
  15. "github.com/fatih/color"
  16. )
  17. func IDs() []Key {
  18. //read the keys stored in /keys directory
  19. keys := readKeys()
  20. return keys
  21. }
  22. func NewID() []Key {
  23. //generate RSA keys pair
  24. reader := rand.Reader
  25. k, err := rsa.GenerateKey(reader, keysize)
  26. check(err)
  27. id := hash(time.Now().String())
  28. savePEMKey(keysDir+"/"+id+"private.pem", k)
  29. savePublicPEMKey(keysDir+"/"+id+"public.pem", k.PublicKey)
  30. var key Key
  31. key.ID = id
  32. key.PrivK = id + "private.pem"
  33. key.PubK = id + "public.pem"
  34. key.Date = time.Now()
  35. fmt.Println(key)
  36. keys := readKeys()
  37. keys = append(keys, key)
  38. saveKeys(keys)
  39. return keys
  40. }
  41. type AskBlindSign struct {
  42. M []byte `json:"m"`
  43. }
  44. type SignResponse struct {
  45. Sig []byte `json:"sig"`
  46. PubK rsa.PublicKey `json:"pubK"`
  47. }
  48. func BlindAndSendToSign(keyID string) []Key {
  49. //get the key
  50. key := getKeyByKeyID(keyID)
  51. //privK := openPEMKey(key.PrivK)
  52. pubK, err := openPublicPEMKey(keysDir + "/" + key.PubK)
  53. check(err)
  54. //pubK to string
  55. m, err := ExportRsaPublicKeyAsPemStr(pubK)
  56. check(err)
  57. mB := []byte(m)
  58. //get serverPubK
  59. var serverPubK *rsa.PublicKey
  60. res, err := http.Get(config.Server)
  61. check(err)
  62. decoder := json.NewDecoder(res.Body)
  63. err = decoder.Decode(&serverPubK)
  64. if err != nil {
  65. panic(err)
  66. }
  67. defer res.Body.Close()
  68. //blind the hashed message
  69. // We do a SHA256 full-domain-hash expanded to 1536 bits (3/4 the key size)
  70. hashed := fdh.Sum(crypto.SHA256, hashize, mB)
  71. blinded, unblinder, err := rsablind.Blind(serverPubK, hashed)
  72. if err != nil {
  73. panic(err)
  74. }
  75. var askBlindSign AskBlindSign
  76. askBlindSign.M = blinded
  77. //send blinded to serverIDsigner
  78. body := new(bytes.Buffer)
  79. json.NewEncoder(body).Encode(askBlindSign)
  80. res, err = http.Post(config.Server+"blindsign", "application/json", body)
  81. check(err)
  82. var signResponse SignResponse
  83. decoder = json.NewDecoder(res.Body)
  84. err = decoder.Decode(&signResponse)
  85. if err != nil {
  86. panic(err)
  87. }
  88. defer res.Body.Close()
  89. sig := signResponse.Sig
  90. //serverPubK := signResponse.PubK
  91. //unblind the signedblind
  92. unblindedSig := rsablind.Unblind(serverPubK, sig, unblinder)
  93. color.Green("unblindedSig")
  94. fmt.Println(unblindedSig)
  95. // Verify the original hashed message against the unblinded signature
  96. if err := rsablind.VerifyBlindSignature(serverPubK, hashed, unblindedSig); err != nil {
  97. fmt.Println(err)
  98. } else {
  99. color.Green("blind signature verified")
  100. key.Verified = true
  101. }
  102. key.UnblindedSig = unblindedSig
  103. key.Hashed = hashed
  104. key.ServerVerifier = serverPubK
  105. saveKey(key)
  106. keys := readKeys()
  107. return keys
  108. }
  109. func Verify(packPubK string) {
  110. return
  111. }
  112. func Delete(keyID string) []Key {
  113. originalKeys := readKeys()
  114. //remove key .pem files
  115. key := getKeyByKeyID(keyID)
  116. _, err := exec.Command("rm", keysDir+"/"+key.PrivK).CombinedOutput()
  117. check(err)
  118. _, err = exec.Command("rm", keysDir+"/"+key.PubK).CombinedOutput()
  119. check(err)
  120. //remove key from keys.json
  121. keys := removeKey(keyID, originalKeys)
  122. saveKeys(keys)
  123. return keys
  124. }
  125. func Encrypt(keyID string, encryptData EncryptData) EncryptData {
  126. key := getKeyByKeyID(keyID)
  127. pubK, err := openPublicPEMKey(keysDir + "/" + key.PubK)
  128. check(err)
  129. out, err := rsa.EncryptOAEP(sha1.New(), rand.Reader, &pubK, []byte(encryptData.M), []byte("orders"))
  130. check(err)
  131. fmt.Println(string(out))
  132. encryptData.C = out
  133. return encryptData
  134. }
  135. func Decrypt(keyID string, encryptData EncryptData) EncryptData {
  136. key := getKeyByKeyID(keyID)
  137. privK, err := openPEMKey(keysDir + "/" + key.PrivK)
  138. check(err)
  139. out, err := rsa.DecryptOAEP(sha1.New(), rand.Reader, privK, []byte(encryptData.C), []byte("orders"))
  140. check(err)
  141. fmt.Println(string(out))
  142. encryptData.M = string(out)
  143. return encryptData
  144. }