Why is char[] preferred over String for passwords?

后端 未结 17 3643
清歌不尽
清歌不尽 2020-11-21 04:34

In Swing, the password field has a getPassword() (returns char[]) method instead of the usual getText() (returns String)

相关标签:
17条回答
  • 2020-11-21 04:52

    It is debatable as to whether you should use String or use Char[] for this purpose because both have their advantages and disadvantages. It depends on what the user needs.

    Since Strings in Java are immutable, whenever some tries to manipulate your string it creates a new Object and the existing String remains unaffected. This could be seen as an advantage for storing a password as a String, but the object remains in memory even after use. So if anyone somehow got the memory location of the object, that person can easily trace your password stored at that location.

    Char[] is mutable, but it has the advantage that after its usage the programmer can explicitly clean the array or override values. So when it's done being used it is cleaned and no one could ever know about the information you had stored.

    Based on the above circumstances, one can get an idea whether to go with String or to go with Char[] for their requirements.

    0 讨论(0)
  • 2020-11-21 04:53

    Character arrays (char[]) can be cleared after use by setting each character to zero and Strings not. If someone can somehow see the memory image, they can see a password in plain text if Strings are used, but if char[] is used, after purging data with 0's, the password is secure.

    0 讨论(0)
  • 2020-11-21 04:55

    To quote an official document, the Java Cryptography Architecture guide says this about char[] vs. String passwords (about password-based encryption, but this is more generally about passwords of course):

    It would seem logical to collect and store the password in an object of type java.lang.String. However, here's the caveat: Objects of type String are immutable, i.e., there are no methods defined that allow you to change (overwrite) or zero out the contents of a String after usage. This feature makes String objects unsuitable for storing security sensitive information such as user passwords. You should always collect and store security sensitive information in a char array instead.

    Guideline 2-2 of the Secure Coding Guidelines for the Java Programming Language, Version 4.0 also says something similar (although it is originally in the context of logging):

    Guideline 2-2: Do not log highly sensitive information

    Some information, such as Social Security numbers (SSNs) and passwords, is highly sensitive. This information should not be kept for longer than necessary nor where it may be seen, even by administrators. For instance, it should not be sent to log files and its presence should not be detectable through searches. Some transient data may be kept in mutable data structures, such as char arrays, and cleared immediately after use. Clearing data structures has reduced effectiveness on typical Java runtime systems as objects are moved in memory transparently to the programmer.

    This guideline also has implications for implementation and use of lower-level libraries that do not have semantic knowledge of the data they are dealing with. As an example, a low-level string parsing library may log the text it works on. An application may parse an SSN with the library. This creates a situation where the SSNs are available to administrators with access to the log files.

    0 讨论(0)
  • 2020-11-21 04:56

    As Jon Skeet states, there is no way except by using reflection.

    However, if reflection is an option for you, you can do this.

    public static void main(String[] args) {
        System.out.println("please enter a password");
        // don't actually do this, this is an example only.
        Scanner in = new Scanner(System.in);
        String password = in.nextLine();
        usePassword(password);
    
        clearString(password);
    
        System.out.println("password: '" + password + "'");
    }
    
    private static void usePassword(String password) {
    
    }
    
    private static void clearString(String password) {
        try {
            Field value = String.class.getDeclaredField("value");
            value.setAccessible(true);
            char[] chars = (char[]) value.get(password);
            Arrays.fill(chars, '*');
        } catch (Exception e) {
            throw new AssertionError(e);
        }
    }
    

    when run

    please enter a password
    hello world
    password: '***********'
    

    Note: if the String's char[] has been copied as a part of a GC cycle, there is a chance the previous copy is somewhere in memory.

    This old copy wouldn't appear in a heap dump, but if you have direct access to the raw memory of the process you could see it. In general you should avoid anyone having such access.

    0 讨论(0)
  • 2020-11-21 05:02

    The answer has already been given, but I'd like to share an issue that I discovered lately with Java standard libraries. While they take great care now of replacing password strings with char[] everywhere (which of course is a good thing), other security-critical data seems to be overlooked when it comes to clearing it from memory.

    I'm thinking of e.g. the PrivateKey class. Consider a scenario where you would load a private RSA key from a PKCS#12 file, using it to perform some operation. Now in this case, sniffing the password alone wouldn't help you much as long as physical access to the key file is properly restricted. As an attacker, you would be much better off if you obtained the key directly instead of the password. The desired information can be leaked manifold, core dumps, a debugger session or swap files are just some examples.

    And as it turns out, there is nothing that lets you clear the private information of a PrivateKey from memory, because there's no API that lets you wipe the bytes that form the corresponding information.

    This is a bad situation, as this paper describes how this circumstance could be potentially exploited.

    The OpenSSL library for example overwrites critical memory sections before private keys are freed. Since Java is garbage-collected, we would need explicit methods to wipe and invalidate private information for Java keys, which are to be applied immediately after using the key.

    0 讨论(0)
  • 2020-11-21 05:04

    While other suggestions here seem valid, there is one other good reason. With plain String you have much higher chances of accidentally printing the password to logs, monitors or some other insecure place. char[] is less vulnerable.

    Consider this:

    public static void main(String[] args) {
        Object pw = "Password";
        System.out.println("String: " + pw);
    
        pw = "Password".toCharArray();
        System.out.println("Array: " + pw);
    }
    

    Prints:

    String: Password
    Array: [C@5829428e
    
    0 讨论(0)
提交回复
热议问题