JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 27.0w | AdaCore Inc |
Base64 encoding is used. This encoding is relatively easy to decode and should not be used with sensitive data.
| Class Name | Unsafe Base64 Encoding (Java) | |||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Significance | security | |||||||||||||||||||||||||||
| Mnemonic | JAVA.CRYPTO.BASE64 | |||||||||||||||||||||||||||
| Categories |
|
|||||||||||||||||||||||||||
| Availability | Available for Java and Kotlin. | |||||||||||||||||||||||||||
| Enabling | Checks for this warning class are disabled by default, and require pedantic Java analysis mode which is also disabled by default. To enable the checks, make the following changes to the project configuration file.
JAVA_ANALYSIS_PEDANTIC_MODE = Yes WARNING_FILTER += allow class="Unsafe Base64 Encoding (Java)" |
In the following program, recoverSecret recovers a secret that was merely Base64-encoded, treating Base64 as if it protected the data. Because Base64 is an encoding rather than encryption, anyone who can read the stored form can decode it back to plaintext. decryptSecret instead uses real encryption (AES in authenticated GCM mode), so recovering the secret requires the secret key.
import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.GCMParameterSpec;
public class SecretStore {
// Base64 is an encoding, not encryption. Treating a Base64-"protected"
// secret as confidential is unsafe: anyone who can read the stored form
// can decode it straight back to the plaintext.
String recoverSecret(String stored) {
byte[] plain = Base64.getDecoder().decode(stored); // 'Unsafe Base64 Encoding (Java)' warning issued here
return new String(plain, StandardCharsets.UTF_8);
}
// The safe alternative is real encryption: AES in authenticated GCM mode
// genuinely protects the secret, so recovering the plaintext requires the
// secret key.
String decryptSecret(SecretKey key, byte[] iv, byte[] ciphertext) throws GeneralSecurityException {
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
cipher.init(Cipher.DECRYPT_MODE, key, new GCMParameterSpec(128, iv));
byte[] plain = cipher.doFinal(ciphertext); // ok: recovered with a real cipher and key
return new String(plain, StandardCharsets.UTF_8);
}
}
To reproduce this example, analyze it with the following configuration:
WARNING_FILTER += allow class="Unsafe Base64 Encoding (Java)" JAVA_ANALYSIS_PEDANTIC_MODE = Yes JAVA_ANALYSIS_ENTRY_POINTS_MODE = ALL_METHODS
Use a safer cryptography algorithm.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.adacore.com/csm.