xmlenc: Decrypt(): return the plaintext directly, don’t modify the source document.

This commit is contained in:
Ross Kinder
2015-11-30 16:49:52 -05:00
parent a4571e8fd9
commit f0631d11c8
2 changed files with 9 additions and 15 deletions

View File

@@ -12,6 +12,7 @@ import (
"encoding/base64" "encoding/base64"
"encoding/pem" "encoding/pem"
"encoding/xml" "encoding/xml"
"errors"
"fmt" "fmt"
"hash" "hash"
"io" "io"
@@ -58,16 +59,15 @@ type cipherData struct {
CipherValue string `xml:"CipherValue"` CipherValue string `xml:"CipherValue"`
} }
var ErrNoEncryptedDataFound = errors.New("no EncryptedData elements found")
// Decrypt searches the serialized XML document `doc` looking for // Decrypt searches the serialized XML document `doc` looking for
// EncryptedData elements and decrypting them. It returns the // an EncryptedData element. When found, it decrypts the element
// original document with the each EncryptedData element replaced // and returns the plaintext of the encrypted section.
// by the derived plaintext.
// //
// Key is a PEM-encoded RSA private key, or a binary TDES key or a // Key is a PEM-encoded RSA private key, or a binary TDES key or a
// binary AES key, depending on the encryption type in use. // binary AES key, depending on the encryption type in use.
func Decrypt(key []byte, doc []byte) ([]byte, error) { func Decrypt(key []byte, doc []byte) ([]byte, error) {
out := bytes.NewBuffer(nil)
encoder := xml.NewEncoder(out)
decoder := xml.NewDecoder(bytes.NewReader(doc)) decoder := xml.NewDecoder(bytes.NewReader(doc))
for { for {
t, err := decoder.Token() t, err := decoder.Token()
@@ -89,17 +89,11 @@ func Decrypt(key []byte, doc []byte) ([]byte, error) {
return nil, err return nil, err
} }
encoder.Flush() return plaintext, nil
out.Write(plaintext)
continue
} }
} }
encoder.EncodeToken(t)
} }
encoder.Flush() return nil, ErrNoEncryptedDataFound
return out.Bytes(), nil
} }
// decryptEncryptedData decrypts the EncryptedData element and returns the // decryptEncryptedData decrypts the EncryptedData element and returns the

File diff suppressed because one or more lines are too long