encrypt.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package samlutils
  15. import (
  16. "crypto/aes"
  17. "crypto/cipher"
  18. "crypto/rand"
  19. "crypto/rsa"
  20. "crypto/sha1"
  21. "crypto/x509"
  22. "encoding/base64"
  23. "encoding/pem"
  24. "hash"
  25. "io/ioutil"
  26. "yunion.io/x/pkg/errors"
  27. "yunion.io/x/pkg/util/seclib"
  28. )
  29. func (saml *SSAMLInstance) parseKeys() error {
  30. privData, err := ioutil.ReadFile(saml.privateKeyFile)
  31. if err != nil {
  32. return errors.Wrapf(err, "ioutil.ReadFile %s", saml.privateKeyFile)
  33. }
  34. saml.privateKey, err = seclib.DecodePrivateKey(privData)
  35. if err != nil {
  36. return errors.Wrap(err, "decodePrivateKey")
  37. }
  38. certData, err := ioutil.ReadFile(saml.certFile)
  39. if err != nil {
  40. return errors.Wrapf(err, "ioutil.Readfile %s", saml.certFile)
  41. }
  42. var block *pem.Block
  43. saml.certs = make([]*x509.Certificate, 0)
  44. first := true
  45. for {
  46. block, certData = pem.Decode(certData)
  47. if block == nil {
  48. break
  49. }
  50. if first {
  51. first = false
  52. saml.certString = seclib.CleanCertificate(string(pem.EncodeToMemory(block)))
  53. }
  54. cert, err := x509.ParseCertificate(block.Bytes)
  55. if err != nil {
  56. return errors.Wrap(err, "x509.ParseCertificate")
  57. }
  58. saml.certs = append(saml.certs, cert)
  59. }
  60. return nil
  61. }
  62. func (key EncryptedKey) decryptKey(privateKey *rsa.PrivateKey) ([]byte, error) {
  63. cipher, err := base64.StdEncoding.DecodeString(key.CipherData.CipherValue.Value)
  64. if err != nil {
  65. return nil, errors.Wrap(err, "base64.StdEncoding.DecodeString")
  66. }
  67. encAlg := key.EncryptionMethod.Algorithm
  68. switch encAlg {
  69. case "http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p":
  70. var shaAlg hash.Hash
  71. hashAlg := key.EncryptionMethod.DigestMethod.Algorithm
  72. switch hashAlg {
  73. case "http://www.w3.org/2000/09/xmldsig#sha1":
  74. shaAlg = sha1.New()
  75. default:
  76. return nil, errors.Wrapf(errors.ErrUnsupportedProtocol, "unsupported digest algorithm %s", hashAlg)
  77. }
  78. plaintext, err := rsa.DecryptOAEP(shaAlg, rand.Reader, privateKey, cipher, nil)
  79. if err != nil {
  80. return nil, errors.Wrap(err, "rsa.DecryptOAEP")
  81. }
  82. return plaintext, nil
  83. default:
  84. return nil, errors.Wrapf(errors.ErrUnsupportedProtocol, "unsupported encryption algorithm %s", encAlg)
  85. }
  86. }
  87. func (data EncryptedData) decryptData(privateKey *rsa.PrivateKey) ([]byte, error) {
  88. cipher, err := base64.StdEncoding.DecodeString(data.CipherData.CipherValue.Value)
  89. if err != nil {
  90. return nil, errors.Wrap(err, "base64.StdEncoding.DecodeString")
  91. }
  92. key, err := data.KeyInfo.EncryptedKey.decryptKey(privateKey)
  93. if err != nil {
  94. return nil, errors.Wrap(err, "KeyInfo.EncryptedKey.decryptKey")
  95. }
  96. encAlg := data.EncryptionMethod.Algorithm
  97. switch encAlg {
  98. case "http://www.w3.org/2001/04/xmlenc#aes128-cbc", "http://www.w3.org/2001/04/xmlenc#aes192-cbc", "http://www.w3.org/2001/04/xmlenc#aes256-cbc":
  99. return decryptAesCbc(key, cipher)
  100. default:
  101. return nil, errors.Wrapf(errors.ErrUnsupportedProtocol, "unsupported encryption algorithm %s", encAlg)
  102. }
  103. }
  104. func decryptAesCbc(key []byte, secret []byte) ([]byte, error) {
  105. c, err := aes.NewCipher(key)
  106. if err != nil {
  107. return nil, errors.Wrap(err, "aes.NewCipher")
  108. }
  109. decrypter := cipher.NewCBCDecrypter(c, secret[0:aes.BlockSize])
  110. data := make([]byte, len(secret)-aes.BlockSize)
  111. copy(data, secret[aes.BlockSize:])
  112. decrypter.CryptBlocks(data, data)
  113. return data, nil
  114. }