Repository navigation
Expand file tree
/
Copy pathsecurity.xml
More file actions
91 lines (78 loc) · 3.47 KB
/
Copy pathsecurity.xml
File metadata and controls
91 lines (78 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?xml version="1.0" encoding="UTF-8"?>
<ruleset name="Security" xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 https://pmd.sourceforge.io/ruleset_2_0_0.xsd">
<description>
Rules that flag potential security flaws.
</description>
<rule name="HardCodedCryptoKey"
language="java"
since="6.4.0"
message="Do not use hard coded encryption keys"
class="net.sourceforge.pmd.lang.java.rule.security.HardCodedCryptoKeyRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_security.html#hardcodedcryptokey">
<description>
Do not use hard-coded values for cryptographic operations. Store keys outside of source code or generate
it from a random source.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class HardCodedCryptoKeyExample {
void good() throws Exception {
// loading the key via properties from outside of the source code
SecretKey secretKey1 = new SecretKeySpec(Properties.getKey(), "AES");
// generating a fresh key
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256);
SecretKey secretKey2 = keyGenerator.generateKey();
// generating from a password, the password is from outside the source code
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
KeySpec spec = new PBEKeySpec(Properties.getPassword().toCharArray(), Properties.getSalt().getBytes(), 65536, 256);
SecretKey secretKey3 = new SecretKeySpec(factory.generateSecret(spec).getEncoded(), "AES");
}
void bad() {
SecretKey secretKey1 = new SecretKeySpec("my secret here".getBytes(), "AES");
byte[] key2 = new byte[] { 1, 2, 3 };
SecretKey secretKey2 = new SecretKeySpec(key2, "AES");
byte[] key3 = new byte[32];
SecretKey secretKey3 = new SecretKeySpec(key3, "AES");
}
}
]]>
</example>
</rule>
<rule name="InsecureCryptoIv"
language="java"
since="6.3.0"
message="Do not use hard coded initialization vector in crypto operations"
class="net.sourceforge.pmd.lang.java.rule.security.InsecureCryptoIvRule"
externalInfoUrl="${pmd.website.baseurl}/pmd_rules_java_security.html#insecurecryptoiv">
<description>
Do not use hard coded initialization vector in cryptographic operations. Use a randomly generated IV.
</description>
<priority>3</priority>
<example>
<![CDATA[
public class InsecureCryptoIvExample {
void good(SecretKey key) throws Exception {
SecureRandom random = SecureRandom.getInstanceStrong();
byte[] iv = new byte[Cipher.getInstance("AES").getBlockSize()];
random.nextBytes(iv);
IvParameterSpec ivParam = new IvParameterSpec(iv);
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, key, ivParam);
}
void bad() {
byte[] iv1 = new byte[] { 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, 00, };
IvParameterSpec ivParam1 = new IvParameterSpec(iv1);
byte[] iv2 = new byte[16];
IvParameterSpec ivParam2 = new IvParameterSpec(iv2);
byte[] iv3 = "secret iv in here".getBytes();
IvParameterSpec ivParam3 = new IvParameterSpec(iv3);
}
}
]]>
</example>
</rule>
</ruleset>