Don't retain references to passed byte slices (#40)
This commit is contained in:
parent
9caf737bc7
commit
10a9a0f557
4 changed files with 17 additions and 9 deletions
|
|
@ -11,3 +11,11 @@ type GopenPGP struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
var pgp = GopenPGP{}
|
var pgp = GopenPGP{}
|
||||||
|
|
||||||
|
// clone returns a clone of the byte slice. Internal function used to make sure
|
||||||
|
// we don't retain a reference to external data.
|
||||||
|
func clone(input []byte) []byte {
|
||||||
|
data := make([]byte, len(input))
|
||||||
|
copy(data, input)
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -52,7 +52,7 @@ func NewKeyFromReader(r io.Reader) (key *Key, err error) {
|
||||||
|
|
||||||
// NewKey creates a new key from the first key in the unarmored binary data
|
// NewKey creates a new key from the first key in the unarmored binary data
|
||||||
func NewKey(binKeys []byte) (key *Key, err error) {
|
func NewKey(binKeys []byte) (key *Key, err error) {
|
||||||
return NewKeyFromReader(bytes.NewReader(binKeys))
|
return NewKeyFromReader(bytes.NewReader(clone(binKeys)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewKeyFromArmored creates a new key from the first key in an armored
|
// NewKeyFromArmored creates a new key from the first key in an armored
|
||||||
|
|
|
||||||
|
|
@ -61,7 +61,7 @@ type ClearTextMessage struct {
|
||||||
// signature, or verification from the unencrypted binary data.
|
// signature, or verification from the unencrypted binary data.
|
||||||
func NewPlainMessage(data []byte) *PlainMessage {
|
func NewPlainMessage(data []byte) *PlainMessage {
|
||||||
return &PlainMessage{
|
return &PlainMessage{
|
||||||
Data: data,
|
Data: clone(data),
|
||||||
TextType: false,
|
TextType: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -78,7 +78,7 @@ func NewPlainMessageFromString(text string) *PlainMessage {
|
||||||
// NewPGPMessage generates a new PGPMessage from the unarmored binary data.
|
// NewPGPMessage generates a new PGPMessage from the unarmored binary data.
|
||||||
func NewPGPMessage(data []byte) *PGPMessage {
|
func NewPGPMessage(data []byte) *PGPMessage {
|
||||||
return &PGPMessage{
|
return &PGPMessage{
|
||||||
Data: data,
|
Data: clone(data),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -103,8 +103,8 @@ func NewPGPMessageFromArmored(armored string) (*PGPMessage, error) {
|
||||||
// datapacket, and encryption algorithm.
|
// datapacket, and encryption algorithm.
|
||||||
func NewPGPSplitMessage(keyPacket []byte, dataPacket []byte) *PGPSplitMessage {
|
func NewPGPSplitMessage(keyPacket []byte, dataPacket []byte) *PGPSplitMessage {
|
||||||
return &PGPSplitMessage{
|
return &PGPSplitMessage{
|
||||||
KeyPacket: keyPacket,
|
KeyPacket: clone(keyPacket),
|
||||||
DataPacket: dataPacket,
|
DataPacket: clone(dataPacket),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -122,7 +122,7 @@ func NewPGPSplitMessageFromArmored(encrypted string) (*PGPSplitMessage, error) {
|
||||||
// NewPGPSignature generates a new PGPSignature from the unarmored binary data.
|
// NewPGPSignature generates a new PGPSignature from the unarmored binary data.
|
||||||
func NewPGPSignature(data []byte) *PGPSignature {
|
func NewPGPSignature(data []byte) *PGPSignature {
|
||||||
return &PGPSignature{
|
return &PGPSignature{
|
||||||
Data: data,
|
Data: clone(data),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -146,8 +146,8 @@ func NewPGPSignatureFromArmored(armored string) (*PGPSignature, error) {
|
||||||
// NewClearTextMessage generates a new ClearTextMessage from data and signature
|
// NewClearTextMessage generates a new ClearTextMessage from data and signature
|
||||||
func NewClearTextMessage(data []byte, signature []byte) *ClearTextMessage {
|
func NewClearTextMessage(data []byte, signature []byte) *ClearTextMessage {
|
||||||
return &ClearTextMessage{
|
return &ClearTextMessage{
|
||||||
Data: data,
|
Data: clone(data),
|
||||||
Signature: signature,
|
Signature: clone(signature),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -80,7 +80,7 @@ func GenerateSessionKey() (*SessionKey, error) {
|
||||||
|
|
||||||
func NewSessionKeyFromToken(token []byte, algo string) *SessionKey {
|
func NewSessionKeyFromToken(token []byte, algo string) *SessionKey {
|
||||||
return &SessionKey{
|
return &SessionKey{
|
||||||
Key: token,
|
Key: clone(token),
|
||||||
Algo: algo,
|
Algo: algo,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue