Sensitive data must be protected from eavesdropping and malicious tampering during transit. An Obfuscated Transfer Object [[Steel 2005]] that is strongly encrypted can protect data in exchanges that involve multiple business tiers or end user systems. This approach is known as sealing the object. To guarantee object integrity, apply a digital signature to the sealed object.
Sealing and signing objects is the preferred mechanism to secure data when
- Serializing or transporting sensitive data is necessary
- A secure communication channel such as SSL is absent or is too costly for limited transactions
- Some sensitive data must persist over an extended period of time (e.g. on an external hard drive)
Avoid using home-brewed cryptographic algorithms; such algorithms almost certainly introduce unnecessary vulnerabilities. Applications that apply home-brewed "cryptography" in the readObject()
and writeObject()
methods are prime examples of anti-patterns.
Noncompliant Code Example
This noncompliant code example is capable of being serialized and transferred across different business tiers. Unfortunately, there are no safeguards against byte stream manipulation attacks while the binary data is in transit. Likewise, anyone can reverse engineer the stream data from its hexadecimal notation to reveal the data in the HashMap
.
class SerializableMap<K,V> implements Serializable { final static long serialVersionUID = -2648720192864531932L; private HashMap<K,V> map; public SerializableMap() { map = new HashMap<K,V>(); } public Object getData(K key) { return map.get(key); } public void setData(K key, V data) { map.put(key, data); } }
If the data in the map is considered sensitive, this example will also violate SER03-J. Do not serialize unencrypted, sensitive data.
Compliant Solution
To provide message confidentiality, use the javax.crypto.SealedObject
class. This class encapsulates a serialized object and encrypts (or seals) it. A strong cryptographic algorithm that uses a secure cryptographic key and padding scheme must be employed to initialize the Cipher
object parameter. The seal
and unseal
utility methods provide the encryption and decryption facilities respectively.
In addition, use the java.security.SignedObject
class to sign the object, when the integrity of the object is to be ensured. The two new arguments passed in to the SignedObject()
method to sign the object are Signature
and a private key derived from a KeyPair
object. To verify the signature, a PublicKey
as well as a Signature
argument is passed to the SignedObject.verify()
method. This enables the code to comply with SEC17-J. Create and sign a SignedObject before creating a SealedObject.
class SerializableMap<K,V> implements Serializable { // other fields and methods... private SignedObject signedMap; public void sign(Signature sig, PrivateKey key) throws IOException, GeneralSecurityException { signedMap = new SignedObject( map, key, sig); map = null; } public void unsign(Signature sig, PublicKey key) throws IOException, GeneralSecurityException, ClassNotFoundException { if (signedMap.verify(key, sig)) { map = (HashMap<K,V>) signedMap.getObject(); signedMap = null; } } private SealedObject sealedMap; public void seal(Cipher cipher) throws IOException, IllegalBlockSizeException { sealedMap = new SealedObject(signedMap, cipher); // Now set the Map to null so that original data does not remain in cleartext signedMap = null; } public void unseal(Cipher cipher) throws IOException, GeneralSecurityException, ClassNotFoundException { signedMap = (SignedObject) sealedMap.getObject(cipher); sealedMap = null; } }
Risk Assessment
Failure to sign and/or seal objects during transit can lead to loss of object integrity or confidentiality.
Guideline |
Severity |
Likelihood |
Remediation Cost |
Priority |
Level |
---|---|---|---|---|---|
SEC16-J |
medium |
probable |
high |
P4 |
L3 |
Automated Detection
Not amenable to static analysis in the general case.
Related Vulnerabilities
Search for vulnerabilities resulting from the violation of this guideline on the CERT website.
Bibliography
[[API 2006]]
[[Gong 2003]] 9.10 Sealing Objects
[[Harold 1999]] Chapter 11: Object Serialization, Sealed Objects
[[Neward 2004]] Item 64: Use SignedObject to provide integrity of Serialized objects and Item 65: Use SealedObject to provide confidentiality of Serializable objects
[[MITRE 2009]] CWE ID 319 "Cleartext Transmission of Sensitive Information"
[[Steel 2005]] Chapter 10: Securing the Business Tier, Obfuscated Transfer Object
SEC15-J. Use SSLSockets rather than Sockets for secure data exchange 14. Platform Security (SEC) SEC17-J. Create and sign a SignedObject before creating a SealedObject