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.

184 lines
3.9 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  1. package main
  2. import (
  3. "crypto/rsa"
  4. "crypto/x509"
  5. "encoding/asn1"
  6. "encoding/json"
  7. "encoding/pem"
  8. "errors"
  9. "fmt"
  10. "io/ioutil"
  11. "os"
  12. "time"
  13. "github.com/fatih/color"
  14. )
  15. type Key struct {
  16. ID string `json:"id"`
  17. PrivK string `json:"privK"` //path of the PrivK file
  18. PubK string `json:"pubK"` //path of the PubK file
  19. PublicKey string `json:"publicKey"`
  20. Date time.Time `json:"date"`
  21. Hashed []byte `json:"hashed"`
  22. UnblindedSig []byte `json:"unblindedsig"`
  23. Verified bool `json:"verified"`
  24. ServerVerifier *rsa.PublicKey `json:"serververifier"`
  25. SignerID string `json:"signerid"`
  26. BlockchainRef string `json:"blockchainref"`
  27. }
  28. func ExportRsaPrivateKeyAsPemStr(privkey *rsa.PrivateKey) string {
  29. privkey_bytes := x509.MarshalPKCS1PrivateKey(privkey)
  30. privkey_pem := pem.EncodeToMemory(
  31. &pem.Block{
  32. Type: "RSA PRIVATE KEY",
  33. Bytes: privkey_bytes,
  34. },
  35. )
  36. return string(privkey_pem)
  37. }
  38. func ParseRsaPrivateKeyFromPemStr(privPEM string) (*rsa.PrivateKey, error) {
  39. block, _ := pem.Decode([]byte(privPEM))
  40. if block == nil {
  41. return nil, errors.New("failed to parse PEM block containing the key")
  42. }
  43. priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  44. if err != nil {
  45. return nil, err
  46. }
  47. return priv, nil
  48. }
  49. func ExportRsaPublicKeyAsPemStr(pubkey rsa.PublicKey) (string, error) {
  50. asn1Bytes, err := asn1.Marshal(pubkey)
  51. check(err)
  52. pubkey_pem := pem.EncodeToMemory(
  53. &pem.Block{
  54. Type: "PUBLIC KEY",
  55. Bytes: asn1Bytes,
  56. },
  57. )
  58. color.Red("pubkey_pem")
  59. fmt.Println(pubkey_pem)
  60. return string(pubkey_pem), nil
  61. }
  62. func ParseRsaPublicKeyFromPemStr(pubPEM string) (pub rsa.PublicKey, err error) {
  63. pemBlock, _ := pem.Decode([]byte(pubPEM))
  64. _, err = asn1.Unmarshal(pemBlock.Bytes, &pub)
  65. return
  66. }
  67. func savePEMKey(fileName string, key *rsa.PrivateKey) {
  68. outFile, err := os.Create(fileName)
  69. check(err)
  70. defer outFile.Close()
  71. var privateKey = &pem.Block{
  72. Type: "PRIVATE KEY",
  73. Bytes: x509.MarshalPKCS1PrivateKey(key),
  74. }
  75. err = pem.Encode(outFile, privateKey)
  76. check(err)
  77. }
  78. func savePublicPEMKey(fileName string, pubkey rsa.PublicKey) {
  79. asn1Bytes, err := asn1.Marshal(pubkey)
  80. check(err)
  81. var pemkey = &pem.Block{
  82. Type: "PUBLIC KEY",
  83. Bytes: asn1Bytes,
  84. }
  85. pemfile, err := os.Create(fileName)
  86. check(err)
  87. defer pemfile.Close()
  88. err = pem.Encode(pemfile, pemkey)
  89. check(err)
  90. }
  91. func openPEMKey(path string) (key *rsa.PrivateKey, err error) {
  92. b, err := ioutil.ReadFile(path)
  93. if err != nil {
  94. fmt.Print(err)
  95. }
  96. key, err = ParseRsaPrivateKeyFromPemStr(string(b))
  97. return
  98. }
  99. func openPublicPEMKey(path string) (key rsa.PublicKey, err error) {
  100. b, err := ioutil.ReadFile(path)
  101. if err != nil {
  102. fmt.Print(err)
  103. }
  104. key, err = ParseRsaPublicKeyFromPemStr(string(b))
  105. return
  106. }
  107. func readKeys() []Key {
  108. path := keysDir + "/keys.json"
  109. var keys []Key
  110. file, err := ioutil.ReadFile(path)
  111. check(err)
  112. content := string(file)
  113. json.Unmarshal([]byte(content), &keys)
  114. return keys
  115. }
  116. func saveKeys(keys []Key) {
  117. jsonKeys, err := json.Marshal(keys)
  118. check(err)
  119. err = ioutil.WriteFile(keysDir+"/keys.json", jsonKeys, 0644)
  120. check(err)
  121. }
  122. func saveKey(k Key) {
  123. fmt.Println(k)
  124. keys := readKeys()
  125. for i, key := range keys {
  126. if key.ID == k.ID {
  127. keys[i] = k
  128. }
  129. }
  130. saveKeys(keys)
  131. }
  132. func getKeyByKeyID(keyID string) (k Key) {
  133. keys := readKeys()
  134. for _, key := range keys {
  135. if key.ID == keyID {
  136. k = key
  137. }
  138. }
  139. return k
  140. }
  141. func removeKey(keyID string, originalKeys []Key) (keys []Key) {
  142. for _, key := range originalKeys {
  143. if key.ID != keyID {
  144. keys = append(keys, key)
  145. }
  146. }
  147. return
  148. }
  149. /*
  150. func getServerPubK(url string) ownrsa.RSAPublicKey {
  151. r, err := http.Get(url + "/")
  152. check(err)
  153. fmt.Println(r)
  154. decoder := json.NewDecoder(r.Body)
  155. //var sigmaString string
  156. var pubK ownrsa.RSAPublicKey
  157. err = decoder.Decode(&pubK)
  158. if err != nil {
  159. panic(err)
  160. }
  161. defer r.Body.Close()
  162. color.Blue("received server pubK:")
  163. fmt.Println(pubK)
  164. return pubK
  165. }
  166. */