...
- serializing or transporting sensitive data.
- a secure communication channel such as SSL (Secure Sockets Layer) is absent or is too costly for limited transactions.
- sensitive data must persist over an extended period of time (for example, on a 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 antipatterns anti-patterns.
This rule applies to the intentional serialization of sensitive information. Rule SER03-J. Do not serialize unencrypted, sensitive data is meant to prevent the unintentional serialization of sensitive information.
Noncompliant Code Example
Code The code examples for this rule are all based upon on the following code example.
Code Block |
---|
class SerializableMap<K,V> implements Serializable { final static long serialVersionUID = -2648720192864531932L; private Map<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); } } public class MapSerializer { public static SerializableMap<String, Integer> buildMap() { SerializableMap<String, Integer> map = new SerializableMap<String, Integer>(); map.setData("John Doe", new Integer(123456789)); map.setData("Richard Roe", new Integer(246813579)); return map; } public static void InspectMap(SerializableMap<String, Integer> map) { System.out.println("John Doe's number is " + map.getData("John Doe")); System.out.println("Richard Roe's number is " + map.getData("Richard Roe")); } public static void main(String[] args) { // ... } } |
...
This noncompliant code example simply serializes then deserializes the map and then deserializes it. Consequently, the map is capable of being serialized can be 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 serialized stream data from its hexadecimal notation to reveal recover the data in the HashMap
.
Code Block | ||
---|---|---|
| ||
public static void main(String[] args) throws IOException, ClassNotFoundException { // Build map SerializableMap<String, Integer> map = buildMap(); // Serialize map ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data")); out.writeObject(map); out.close(); // Deserialize map ObjectInputStream in = new ObjectInputStream(new FileInputStream("data")); map = (SerializableMap<String, Integer>) in.readObject(); in.close(); // Inspect map InspectMap(map); } |
If the data in the map is considered sensitive, this example will also violate violates rule SER03-J. Do not serialize unencrypted, sensitive data.
...
This noncompliant code example uses the javax.crypto.SealedObject
class to provide message confidentiality. 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.
This noncompliant code example encrypts the map into a SealedObject
, rendering the data inaccessible to prying eyes. However, because the program fails to sign the data is not signed, it provides no proof of authenticationrendering it impossible to authenticate.
Code Block | ||
---|---|---|
| ||
public static void main(String[] args) throws IOException, GeneralSecurityException, ClassNotFoundException { // Build map SerializableMap<String, Integer> map = buildMap(); // Generate sealing key & seal map KeyGenerator generator; generator = KeyGenerator.getInstance("AES"); generator.init(new SecureRandom()); Key key = generator.generateKey(); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject sealedMap = new SealedObject(map, cipher); // Serialize map ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data")); out.writeObject(sealedMap); out.close(); // Deserialize map ObjectInputStream in = new ObjectInputStream(new FileInputStream("data")); sealedMap = (SealedObject) in.readObject(); in.close(); // Unseal map cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); map = (SerializableMap<String, Integer>) sealedMap.getObject(cipher); // Inspect map InspectMap(map); } |
...
This noncompliant code example uses the java.security.SignedObject
class to sign an object , when the integrity of the object is to must 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.
...
When a principal signs material that has already been encrypted, it should not be inferred that the principal knows the content of the message. On the other hand, it is proper to infer that the principal that signs a message and then encrypts it for privacy knows the content of the message.
Wiki Markup |
---|
The rationale is that anyAny malicious party can intercept the originally signed encrypted message from the originator, strip the signature, and add its own signature to the encrypted message. Both the malicious party and the receiver have no information about the contents of the original message as it is encrypted and then signed (it can only be decrypted only after verifying the signature). The receiver has no way of confirming the sender's identity unless the legitimate sender's public key is obtained over a secure channel. One of the three Internal Telegraph and Telephone Consultative Committee (CCITT) X.509 standard protocols was susceptible to such an attack \[[CCITT 1988|AA. Bibliography#CCITT 88]\]. |
...
Code Block | ||
---|---|---|
| ||
public static void main(String[] args) throws IOException, GeneralSecurityException, ClassNotFoundException { // Build map SerializableMap<String, Integer> map = buildMap(); // Generate sealing keythrows &IOException, sealGeneralSecurityException, map KeyGenerator generator; generator = KeyGenerator.getInstance("AES"); generator.init(new SecureRandom()); Key key = generator.generateKey(); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject sealedMap = new SealedObject(map, cipher ClassNotFoundException { // Build map SerializableMap<String, Integer> map = buildMap(); // Generate signingsealing public/private key pair & signseal map KeyGenerator generator; KeyPairGenerator kpggenerator = KeyPairGeneratorKeyGenerator.getInstance("DSA"AES"); generator.init(new SecureRandom()); KeyPairKey kpkey = kpggenerator.generateKeyPairgenerateKey(); SignatureCipher sigcipher = SignatureCipher.getInstance("SHA1withDSAAES"); SignedObject signedMap = new SignedObject(sealedMap, kp.getPrivate(), sigcipher.init(Cipher.ENCRYPT_MODE, key); SealedObject sealedMap = new SealedObject(map, cipher); // Generate signing public/ Serializeprivate key pair & sign map ObjectOutputStreamKeyPairGenerator outkpg = new ObjectOutputStream(new FileOutputStreamKeyPairGenerator.getInstance("dataDSA")); out.writeObject(signedMapKeyPair kp = kpg.generateKeyPair(); out.close(Signature sig = Signature.getInstance("SHA1withDSA"); //SignedObject DeserializesignedMap map= ObjectInputStream in = new ObjectInputStream(new FileInputStream("data")); signedMap = (SignedObject) in.readObject(); in.close(SignedObject(sealedMap, kp.getPrivate(), sig); // UnsignSerialize map if (!signedMap.verify(kp.getPublic(), sig)) { ObjectOutputStream out = thrownew ObjectOutputStream(new GeneralSecurityExceptionFileOutputStream("Map failed verificationdata")); }out.writeObject(signedMap); sealedMap = (SealedObject) signedMap.getObjectout.close(); // UnsealDeserialize map ObjectInputStream cipherin = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key new ObjectInputStream(new FileInputStream("data")); mapsignedMap = (SerializableMap<String, Integer>) sealedMap.getObject(cipherSignedObject) in.readObject(); in.close(); // InspectUnsign map InspectMap(map); } |
Compliant Solution (Sign Then Seal)
This compliant solution correctly signs the object before sealing it. This provides a guarantee of authenticity to the object, in addition to protection from man-in-the-middle attacks.
if (!signedMap.verify(kp.getPublic(), sig)) {
throw new GeneralSecurityException("Map failed verification");
}
sealedMap = (SealedObject) signedMap.getObject();
// Unseal map
cipher = Cipher.getInstance("AES");
cipher.init(Cipher.DECRYPT_MODE, key);
map = (SerializableMap<String, Integer>) sealedMap.getObject(cipher);
// Inspect map
InspectMap(map);
}
|
Compliant Solution (Sign Then Seal)
This compliant solution correctly signs the object before sealing it. This provides a guarantee of authenticity to the object in addition to protection from man-in-the-middle attacks.
Code Block | ||
---|---|---|
| ||
public static void main(String[] args)
throws
IOException, GeneralSecurityException,
| ||
Code Block | ||
| ||
public static void main(String[] args) throws IOException, GeneralSecurityException, ClassNotFoundException { // Build map SerializableMap<String, Integer> map = buildMap(); // Generate signing public/private key pair & sign map KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA"); KeyPair kp = kpg.generateKeyPair(); Signature sig = Signature.getInstance("SHA1withDSA"); SignedObject signedMap = new SignedObject(map, kp.getPrivate(), sig); // Generate sealing key & seal map KeyGenerator generator; generator = KeyGenerator.getInstance("AES"); generator.init(new SecureRandom()); Key key = generator.generateKey(); Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, key); SealedObject sealedMap = new SealedObject(signedMap, cipher); // Serialize map ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("data")); out.writeObject(sealedMap); out.close(); // Deserialize map ObjectInputStream in = new ObjectInputStream(new FileInputStream("data")); sealedMap = (SealedObject) in.readObject(); in.close(); // Unseal map cipher = Cipher.getInstance("AES"); cipher.init(Cipher.DECRYPT_MODE, key); signedMap = (SignedObject) sealedMap.getObject(cipher); // Unsign map if (!signedMap.verify(kp.getPublic(), sig)) { throw new GeneralSecurityException("Map failed verification"); } map = (SerializableMap<String, Integer>) signedMap.getObject(); // Inspect map InspectMap(map); } |
...
SER02-EX1: Signing and sealing is required only required for objects that must cross a trust boundary. Objects that never leave the trust boundary need not be signed or sealed. For instance, if when an entire network is contained within a trust boundary, objects that never leave that network need not be signed or sealed.
...
Rule | Severity | Likelihood | Remediation Cost | Priority | Level |
---|---|---|---|---|---|
SER02-J | medium | probable | high | P4 | L3 |
Automated Detection
Not This rule is not amenable to static analysis in the general case.
Related Guidelines
CWE-319, ". Cleartext Transmission of Sensitive Information" transmission of sensitive information |
Bibliography
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="58273db3ead39011-e446f0c6-45414b89-af5e8482-e4b5bc6aba40e5c7eef358dd"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] |
| ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="61a808134fcb52bf-48b7d02d-48ff4fa0-8f38b30b-d0d6a3180c29e185e20d6a4d"><ac:plain-text-body><![CDATA[ | [[Gong 2003 | AA. Bibliography#Gong 03]] | 9.10, Sealing Objects | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="cd027f642f71b3e4-76468956-468a443a-a8eaa018-142475a2a70479bba7dd99a7"><ac:plain-text-body><![CDATA[ | [[Harold 1999 | AA. Bibliography#Harold 99]] | Chapter 11: , Object Serializationserialization, Sealed Objects sealed objects | ]]></ac:plain-text-body></ac:structured-macro> |
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="11454c8d98e6773a-03309e1c-420c4093-90fcaf00-34e7bf3e05d2ee629e45f9fa"><ac:plain-text-body><![CDATA[ | [[Neward 2004 | AA. Bibliography#Neward 04]] | Item 64: , Use | ]]></ac:plain-text-body></ac:structured-macro> |
| Item 65: , Use | |||
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="b29014cf48290035-9a82253b-41a74d16-8161a416-ebe2878d9735607b875d3b10"><ac:plain-text-body><![CDATA[ | [[Steel 2005 | AA. Bibliography#Steel 05]] | Chapter 10: , Securing the Business Tier, Obfuscated Transfer Object | ]]></ac:plain-text-body></ac:structured-macro> |
...