Step-by-step Instruction
▪
Analyze the code: Carefully read the provided Java code to spot the vulnerabilities
▪
Explain the vulnerabilities: Provide a detailed explanation of why the identified lines are insecure
▪
Identify the lines causing the vulnerabilities: Pinpoint the exact lines in the code that are problematic
▪
Explain the defensive coding principle: Describe the principle that is violated and how it mitigates the vulnerability
Question i) Name the kind of vulnerability and explain why it is insecure.
Explanation
The first vulnerability is using a "String" for the password. Strings are immutable in Java, meaning once created, they cannot be modified. This can lead to security issues because the password remains in memory until garbage collection occurs, which can be a potential target for memory dump attacks.
Question ii) Identify the line(s) that cause the vulnerability and describe the problem.
Explanation
The second vulnerability is in the line where the password is converted to a "char[]" array. Specifically, the line "password.getChars(0, 32, chars, 0);" (line 11). This line is problematic because it assumes the password is at least 32 characters long. If the password is shorter, it will throw an "IndexOutOfBoundsException".
Question iii) Explain which defensive coding principle is violated and how it mitigates the vulnerability.
Explanation
The defensive coding principle violated here is "Fail-Safe Defaults". The code should not assume the password length and should handle cases where the password is shorter than expected. This can be mitigated by checking the length of the password before attempting to copy it into the "char[]" array.
Example of Corrected Code
Time Complexity
The time complexity of the "verifyPassword" method is O(n), where n is the length of the password. This is because the method performs operations that depend on the length of the password, such as copying characters and hashing.
Space Complexity
The space complexity is O(1) for the fixed-size arrays and O(n) for the password length. The method uses a fixed amount of space for the "char[]" and "byte[]" arrays, but the space required for the password depends on its length.