OneTimeString — do not clear on set

This commit is contained in:
Vladimir Krivosheev
2016-08-25 09:15:49 +02:00
parent d90d001327
commit 0670fd3af0
4 changed files with 10 additions and 7 deletions
@@ -46,7 +46,7 @@ internal class KeyChainCredentialStore() : CredentialStore {
return
}
val password = credentials!!.password!!.toByteArray()
val password = credentials!!.password!!.toByteArray(false)
saveGenericPassword(attributes.serviceName.toByteArray(), attributes.userName ?: credentials.userName, password, password.size)
password.fill(0)
}
@@ -86,13 +86,16 @@ class OneTimeString(value: CharArray, offset: Int = 0, length: Int = value.size)
}
// string will be cleared and not valid after
fun toByteArray(): ByteArray {
if (!consumed.compareAndSet(false, true)) {
@JvmOverloads
fun toByteArray(clear: Boolean = true): ByteArray {
if (clear && !consumed.compareAndSet(false, true)) {
throw Error("Already consumed")
}
val result = Charsets.UTF_8.encode(CharBuffer.wrap(myChars, myStart, length))
myChars.fill('\u0000', myStart, myEnd)
if (clear) {
myChars.fill('\u0000', myStart, myEnd)
}
return result.toByteArray()
}
@@ -43,11 +43,11 @@ public abstract class BasePasswordSafeProvider implements PasswordStorage {
public final void set(@NotNull CredentialAttributes attributes, @Nullable Credentials value) {
byte[] key = EncryptionUtil.encryptKey(key(), EncryptionUtil.rawKey(attributes));
if (value == null) {
if (value == null || value.getPassword() == null) {
removeEncryptedPassword(key);
}
else {
storeEncryptedPassword(key, EncryptionUtil.encryptText(key(), value == null ? null : value.getPassword()));
storeEncryptedPassword(key, EncryptionUtil.encryptText(key(), value.getPassword()));
}
}
@@ -171,7 +171,7 @@ public class EncryptionUtil {
* @return encrypted text
*/
public static byte[] encryptText(byte[] password, @NotNull OneTimeString value) {
byte[] data = value.toByteArray();
byte[] data = value.toByteArray(false);
return encryptData(password, data.length, data);
}