master_rsa_cipher.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package osscrypto
  2. import (
  3. "crypto/rand"
  4. "crypto/rsa"
  5. "crypto/x509"
  6. "encoding/asn1"
  7. "encoding/json"
  8. "encoding/pem"
  9. "fmt"
  10. )
  11. // CreateMasterRsa Create master key interface implemented by rsa
  12. // matDesc will be converted to json string
  13. func CreateMasterRsa(matDesc map[string]string, publicKey string, privateKey string) (MasterCipher, error) {
  14. var masterCipher MasterRsaCipher
  15. var jsonDesc string
  16. if len(matDesc) > 0 {
  17. b, err := json.Marshal(matDesc)
  18. if err != nil {
  19. return masterCipher, err
  20. }
  21. jsonDesc = string(b)
  22. }
  23. masterCipher.MatDesc = jsonDesc
  24. masterCipher.PublicKey = publicKey
  25. masterCipher.PrivateKey = privateKey
  26. return masterCipher, nil
  27. }
  28. // MasterRsaCipher rsa master key interface
  29. type MasterRsaCipher struct {
  30. MatDesc string
  31. PublicKey string
  32. PrivateKey string
  33. }
  34. // GetWrapAlgorithm get master key wrap algorithm
  35. func (mrc MasterRsaCipher) GetWrapAlgorithm() string {
  36. return RsaCryptoWrap
  37. }
  38. // GetMatDesc get master key describe
  39. func (mrc MasterRsaCipher) GetMatDesc() string {
  40. return mrc.MatDesc
  41. }
  42. // Encrypt encrypt data by rsa public key
  43. // Mainly used to encrypt object's symmetric secret key and iv
  44. func (mrc MasterRsaCipher) Encrypt(plainData []byte) ([]byte, error) {
  45. block, _ := pem.Decode([]byte(mrc.PublicKey))
  46. if block == nil {
  47. return nil, fmt.Errorf("pem.Decode public key error")
  48. }
  49. var pub *rsa.PublicKey
  50. if block.Type == "PUBLIC KEY" {
  51. // pks8 format
  52. pubInterface, err := x509.ParsePKIXPublicKey(block.Bytes)
  53. if err != nil {
  54. return nil, err
  55. }
  56. pub = pubInterface.(*rsa.PublicKey)
  57. } else if block.Type == "RSA PUBLIC KEY" {
  58. // pks1 format
  59. pub = &rsa.PublicKey{}
  60. _, err := asn1.Unmarshal(block.Bytes, pub)
  61. if err != nil {
  62. return nil, err
  63. }
  64. } else {
  65. return nil, fmt.Errorf("not supported public key,type:%s", block.Type)
  66. }
  67. return rsa.EncryptPKCS1v15(rand.Reader, pub, plainData)
  68. }
  69. // Decrypt Decrypt data by rsa private key
  70. // Mainly used to decrypt object's symmetric secret key and iv
  71. func (mrc MasterRsaCipher) Decrypt(cryptoData []byte) ([]byte, error) {
  72. block, _ := pem.Decode([]byte(mrc.PrivateKey))
  73. if block == nil {
  74. return nil, fmt.Errorf("pem.Decode private key error")
  75. }
  76. if block.Type == "PRIVATE KEY" {
  77. // pks8 format
  78. privInterface, err := x509.ParsePKCS8PrivateKey(block.Bytes)
  79. if err != nil {
  80. return nil, err
  81. }
  82. return rsa.DecryptPKCS1v15(rand.Reader, privInterface.(*rsa.PrivateKey), cryptoData)
  83. } else if block.Type == "RSA PRIVATE KEY" {
  84. // pks1 format
  85. priv, err := x509.ParsePKCS1PrivateKey(block.Bytes)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return rsa.DecryptPKCS1v15(rand.Reader, priv, cryptoData)
  90. } else {
  91. return nil, fmt.Errorf("not supported private key,type:%s", block.Type)
  92. }
  93. }