|
| 1 | +package checks |
| 2 | + |
| 3 | +import javax.crypto.SecretKeyFactory |
| 4 | +import javax.crypto.spec.PBEKeySpec |
| 5 | + |
| 6 | +typealias SecretKeyFactoryAlias = SecretKeyFactory |
| 7 | +typealias PBEKeySpecAlias = PBEKeySpec |
| 8 | + |
| 9 | +class PasswordPlaintextFastHashingCheckSample { |
| 10 | + // region Noncompliant cases |
| 11 | + |
| 12 | + class NoncompliantConstantIterations { |
| 13 | + companion object { |
| 14 | + private const val PBKDF2_ITERATIONS = 120000 |
| 15 | + // ^^^^^^> |
| 16 | + } |
| 17 | + |
| 18 | + fun noncompliantConstantIterations(password: String, salt: ByteArray) { |
| 19 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, PBKDF2_ITERATIONS, 256) // Noncompliant {{Use at least 210000 PBKDF2 iterations.}} |
| 20 | + // ^^^^^^^^^^^^^^^^^ |
| 21 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512") |
| 22 | + // ^^^^^^^^^^^^^^^^^^^^^^< |
| 23 | + secretKeyFactory.generateSecret(keySpec) |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + class NoncompliantConstantAlgorithm { |
| 28 | + companion object { |
| 29 | + private const val SHA512_ALGORITHM = "PBKDF2withHmacSHA512" |
| 30 | + // ^^^^^^^^^^^^^^^^^^^^^^> |
| 31 | + } |
| 32 | + |
| 33 | + fun noncompliantConstantAlgorithm(password: String, salt: ByteArray) { |
| 34 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 120000, 256) // Noncompliant {{Use at least 210000 PBKDF2 iterations.}} |
| 35 | + // ^^^^^^ |
| 36 | + val secretKeyFactory = SecretKeyFactory.getInstance(SHA512_ALGORITHM) |
| 37 | + secretKeyFactory.generateSecret(keySpec) |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + fun noncompliantNoKeyLength(password: String, salt: ByteArray) { |
| 42 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 120000) // Noncompliant |
| 43 | + // ^^^^^^ |
| 44 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512") |
| 45 | + // ^^^^^^^^^^^^^^^^^^^^^^< |
| 46 | + secretKeyFactory.generateSecret(keySpec) |
| 47 | + } |
| 48 | + |
| 49 | + fun noncompliantIntLiteralIterationWithSha512(password: String, salt: ByteArray) { |
| 50 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 120000, 256) // Noncompliant {{Use at least 210000 PBKDF2 iterations.}} |
| 51 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512") |
| 52 | + secretKeyFactory.generateSecret(keySpec) |
| 53 | + } |
| 54 | + |
| 55 | + fun noncompliantIntLiteralIterationWithSha256(password: String, salt: ByteArray) { |
| 56 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 300000, 256) // Noncompliant {{Use at least 600000 PBKDF2 iterations.}} |
| 57 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA256") |
| 58 | + secretKeyFactory.generateSecret(keySpec) |
| 59 | + } |
| 60 | + |
| 61 | + fun noncompliantIntLiteralIterationWithSha1(password: String, salt: ByteArray) { |
| 62 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 1_200_000, 256) // Noncompliant {{Use at least 1300000 PBKDF2 iterations.}} |
| 63 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA1") |
| 64 | + secretKeyFactory.generateSecret(keySpec) |
| 65 | + } |
| 66 | + |
| 67 | + fun noncompliantLocalVariableIteration(password: String, salt: ByteArray) { |
| 68 | + val iterations = 120000 |
| 69 | + // ^^^^^^> |
| 70 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, iterations, 256) // Noncompliant |
| 71 | + // ^^^^^^^^^^ |
| 72 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512") |
| 73 | + // ^^^^^^^^^^^^^^^^^^^^^^< |
| 74 | + secretKeyFactory.generateSecret(keySpec) |
| 75 | + } |
| 76 | + |
| 77 | + fun noncompliantLocalVariableIterationAndAlgorithm(password: String, salt: ByteArray) { |
| 78 | + val algorithm = "PBKDF2withHmacSHA512" |
| 79 | + // ^^^^^^^^^^^^^^^^^^^^^^> |
| 80 | + val iterations = 120000 |
| 81 | + // ^^^^^^> |
| 82 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, iterations, 256) // Noncompliant |
| 83 | + // ^^^^^^^^^^ |
| 84 | + val secretKeyFactory = SecretKeyFactory.getInstance(algorithm) |
| 85 | + secretKeyFactory.generateSecret(keySpec) |
| 86 | + } |
| 87 | + |
| 88 | + fun noncompliantComplexFlow(password: String, salt: ByteArray) { |
| 89 | + val iterations = 500_000 |
| 90 | + // ^^^^^^^> |
| 91 | + if (salt.size > 10) { |
| 92 | + print("Some flow between relevant code") |
| 93 | + } |
| 94 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, iterations, 256) // Noncompliant |
| 95 | + // ^^^^^^^^^^ |
| 96 | + while (salt.size < 10) { |
| 97 | + if (salt.hashCode() == 42) { |
| 98 | + print("Some other flow between relevant code") |
| 99 | + |
| 100 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA256") |
| 101 | + // ^^^^^^^^^^^^^^^^^^^^^^< |
| 102 | + val aLambda = { |
| 103 | + secretKeyFactory.generateSecret(keySpec) |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + fun noncompliantStringConcatenation(password: String, salt: ByteArray) { |
| 110 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 120000, 256) // Noncompliant |
| 111 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmac" + "SHA512") |
| 112 | + secretKeyFactory.generateSecret(keySpec) |
| 113 | + } |
| 114 | + |
| 115 | + fun noncompliantToString(password: String, salt: ByteArray) { |
| 116 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 120000, 256) // FN |
| 117 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512".toString()) |
| 118 | + secretKeyFactory.generateSecret(keySpec) |
| 119 | + } |
| 120 | + |
| 121 | + fun noncompliantInlineSecretKeyFactoryGetInstance(password: String, salt: ByteArray) { |
| 122 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 120000, 256) // Noncompliant |
| 123 | + SecretKeyFactory.getInstance("PBKDF2withHmacSHA512").generateSecret(keySpec) |
| 124 | + } |
| 125 | + |
| 126 | + fun noncompliantEverythingInline(password: String, salt: ByteArray) { |
| 127 | + SecretKeyFactory.getInstance("PBKDF2withHmacSHA512").generateSecret( |
| 128 | + // ^^^^^^^^^^^^^^^^^^^^^^> |
| 129 | + PBEKeySpec(password.toCharArray(), salt, 110000, 256) // Noncompliant |
| 130 | + // ^^^^^^ |
| 131 | + ) |
| 132 | + } |
| 133 | + |
| 134 | + fun noncompliantEverythingInlineWithLet(password: String, salt: ByteArray) { |
| 135 | + PBEKeySpec(password.toCharArray(), salt, 110000, 256).let { // Noncompliant |
| 136 | + // ^^^^^^ |
| 137 | + SecretKeyFactory.getInstance("PBKDF2withHmacSHA512").generateSecret(it) |
| 138 | + // ^^^^^^^^^^^^^^^^^^^^^^< |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + fun noncompliantWithAliases(password: String, salt: ByteArray) { |
| 143 | + val keySpec = PBEKeySpecAlias(password.toCharArray(), salt, 120000, 256) // Noncompliant |
| 144 | + SecretKeyFactoryAlias.getInstance("PBKDF2withHmacSHA512").generateSecret(keySpec) |
| 145 | + } |
| 146 | + |
| 147 | + fun noncompliantDefaultArgumentIteration(password: String, salt: ByteArray, iteration: Int = 120000) { |
| 148 | + // ^^^^^^> |
| 149 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, iteration, 256) // Noncompliant |
| 150 | + // ^^^^^^^^^ |
| 151 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512") |
| 152 | + // ^^^^^^^^^^^^^^^^^^^^^^< |
| 153 | + secretKeyFactory.generateSecret(keySpec) |
| 154 | + } |
| 155 | + |
| 156 | + fun noncompliantDefaultArgumentIterationAndAlgorithm( |
| 157 | + password: String, |
| 158 | + salt: ByteArray, |
| 159 | + iteration: Int = 110000, |
| 160 | + // ^^^^^^> |
| 161 | + algorithm: String = "PBKDF2withHmacSHA512", |
| 162 | + // ^^^^^^^^^^^^^^^^^^^^^^> |
| 163 | + ) { |
| 164 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, iteration, 256) // Noncompliant |
| 165 | + // ^^^^^^^^^ |
| 166 | + val secretKeyFactory = SecretKeyFactory.getInstance(algorithm) |
| 167 | + secretKeyFactory.generateSecret(keySpec) |
| 168 | + } |
| 169 | + |
| 170 | + class NoncompliantDefaultPrimaryConstructorArgumentIterationAndAlgorithm( |
| 171 | + password: String, |
| 172 | + salt: ByteArray, |
| 173 | + val iteration: Int = 110000, |
| 174 | + // ^^^^^^> |
| 175 | + val algorithm: String = "PBKDF2withHmacSHA512", |
| 176 | + // ^^^^^^^^^^^^^^^^^^^^^^> |
| 177 | + ) { |
| 178 | + fun test(password: String, salt: ByteArray) { |
| 179 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, iteration, 256) // Noncompliant |
| 180 | + // ^^^^^^^^^ |
| 181 | + val secretKeyFactory = SecretKeyFactory.getInstance(algorithm) |
| 182 | + secretKeyFactory.generateSecret(keySpec) |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + // endregion |
| 187 | + |
| 188 | + // region Compliant cases |
| 189 | + |
| 190 | + fun compliantIntLiteralAboveThresholdForSHA512(password: String, salt: ByteArray) { |
| 191 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 210000, 256) // Compliant: 210_000 >= 210_000 |
| 192 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512") |
| 193 | + secretKeyFactory.generateSecret(keySpec) |
| 194 | + } |
| 195 | + |
| 196 | + fun compliantIntLiteralAboveThresholdForSHA256(password: String, salt: ByteArray) { |
| 197 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 700000, 256) // Compliant: 700_000 >= 600_000 |
| 198 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA256") |
| 199 | + secretKeyFactory.generateSecret(keySpec) |
| 200 | + } |
| 201 | + |
| 202 | + fun compliantIntLiteralAboveThresholdForSHA1(password: String, salt: ByteArray) { |
| 203 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 1_400_000, 256) // Compliant: 1_400_000 >= 1_300_000 |
| 204 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA1") |
| 205 | + secretKeyFactory.generateSecret(keySpec) |
| 206 | + } |
| 207 | + |
| 208 | + fun compliantUnknownAlgorithm(password: String, salt: ByteArray) { |
| 209 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 120000, 256) // Compliant: unknown algorithm |
| 210 | + val secretKeyFactory = SecretKeyFactory.getInstance("unknown") |
| 211 | + secretKeyFactory.generateSecret(keySpec) |
| 212 | + } |
| 213 | + |
| 214 | + fun compliantComplexFlow(password: String, salt: ByteArray) { |
| 215 | + val iterations = if (salt.size > 10) 210000 else 60000 |
| 216 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, iterations, 256) // Compliant: salt.size can be anything |
| 217 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512") |
| 218 | + secretKeyFactory.generateSecret(keySpec) |
| 219 | + } |
| 220 | + |
| 221 | + fun compliantWithoutSecretKeyFactoryGenerateSecret(password: String, salt: ByteArray) { |
| 222 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 120000, 256) // Compliant: no generateSecret |
| 223 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512") |
| 224 | + } |
| 225 | + |
| 226 | + fun compliantWithoutSecretKeyFactoryGetInstance(password: String, salt: ByteArray, secretKeyFactory: SecretKeyFactory) { |
| 227 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, 120000, 256) // Compliant: no getInstance |
| 228 | + secretKeyFactory.generateSecret(keySpec) |
| 229 | + } |
| 230 | + |
| 231 | + fun compliantMultipleKeySpec(password: String, salt: ByteArray) { |
| 232 | + val keySpec1 = PBEKeySpec(password.toCharArray(), salt, 200000, 256) |
| 233 | + val keySpec2 = PBEKeySpec(password.toCharArray(), salt, 220000, 256) |
| 234 | + val secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2withHmacSHA512") |
| 235 | + // This is commented out: secretKeyFactory.generateSecret(keySpec1) |
| 236 | + secretKeyFactory.generateSecret(keySpec2) // Compliant: keySpec2 has enough iterations |
| 237 | + } |
| 238 | + |
| 239 | + fun compliantDefaultArgumentIterationAndAlgorithm( |
| 240 | + password: String, |
| 241 | + salt: ByteArray, |
| 242 | + iteration: Int = 220000, |
| 243 | + algorithm: String = "PBKDF2withHmacSHA512", |
| 244 | + ) { |
| 245 | + val keySpec = PBEKeySpec(password.toCharArray(), salt, iteration, 256) // Compliant: 220_000 >= 210_000 |
| 246 | + val secretKeyFactory = SecretKeyFactory.getInstance(algorithm) |
| 247 | + secretKeyFactory.generateSecret(keySpec) |
| 248 | + } |
| 249 | + |
| 250 | + // endregion |
| 251 | +} |
0 commit comments