Assuming that I’m trying to pull from a RESTful api that uses basic authentication / basic certificates, what would be the best way to store that user name and password in my program? Right now it’s just sitting there in plaintext.
UsernamePasswordCredentials creds = new UsernamePasswordCredentials("myName@myserver","myPassword1234");
Is there some way of doing this that is more security minded?
Thanks
Important note:
If you’re designing the authentication system as a whole, you shouldn’t store passwords, even if they’re encrypted. You store a hash, and check if passwords provided during login match the same hash. That way, a security breach on your database avoids getting your users’ passwords exposed.
With that said, for situations where you are going to store data as-is (in this case passwords), then with an inner-to-outer mindset, here are some steps to protect your process:
First step, you should change your password-handling from
Stringtocharacter array.The reason for this is that a
Stringis animmutableobject, and so it’s data will not be cleansed immediately even if the object is set tonull; The data is set for garbage-collection instead, and this poses security problems because malicious programs might gain access to thatString(password) data before it is cleaned.This is the main reason why Swing’s JPasswordField’s
getText()method is deprecated, and whygetPassword()uses character arrays.The second step is to encrypt your credentials, only decrypting them temporarily during the authentication process. Or to hash them server-side, store that hash, and "forget" the original password.
This, similarly to the first step, makes sure your vulnerability-time is as small as possible.
It is recommended that your credentials are not hard-coded, and that instead, you store them in a centralized, configurable and easily-maintainable manner, such as a configuration or properties file, or a database.
You should encrypt your credentials before saving the file, and additionally, you can apply a second encryption to the file itself (2-layer encryption to the credentials, and 1-layer to other file contents).
Note that each of the two encryption processes mentioned above can be multiple-layered themselves. Each encryption can be an individual application of Triple Data Encryption Standard (AKA TDES and 3DES), as a conceptual example.
After your local environment is properly protected (but remember, it’s never ever "safe"!), the third step is apply basic protection to your transmission process, by using TLS (Transport Layer Security) or SSL (Secure Sockets Layer).
The forth step is to apply other protection methods.
For example, applying obfuscation techniques to your "to-use" compile, to avoid (even if shortly) the exposure of your security measures in case your program is obtained by Ms. Eve, Mr. Mallory, or someone else (the bad-guys) and decompiled.
UPDATE 1:
By @Damien.Bell ‘s request, here is an example that covers the first and second steps:
A full example, addressing every protection step, would far exceed what I think is reasonable for this question, since it’s about "what are the steps", not "how to apply them".
It would far over-size my answer (at last the sampling), while other questions here on S.O. are already directed on the "How to" of those steps, being far more appropriate, and offering far better explanation and sampling on the implementation of each individual step.