try {
serviceBytes = service.getBytes("UTF-8");
accountBytes = account.getBytes("UTF-8");
passwordBytes = password.getBytes("UTF-8");
} catch (UnsupportedEncodingException ex) {
throw new PasswordSaveException("Unsupported encoding 'UTF-8' specified");
}
//
Pointer[] itemRef = new Pointer[1];
//
int status = NativeLibraryManager.Security.SecKeychainFindGenericPassword(
null, serviceBytes.length, serviceBytes,
accountBytes.length, accountBytes,
null, null, itemRef);
if (status != SecurityLibrary.ERR_SEC_SUCCESS
&& status != SecurityLibrary.ERR_SEC_ITEM_NOT_FOUND) {
throw new PasswordSaveException(convertErrorCodeToMessage(status));
}
//
if (itemRef[0] != null) {
status = NativeLibraryManager.Security.SecKeychainItemModifyContent(
itemRef[0], null, passwordBytes.length, passwordBytes);
// TODO: add code to release itemRef[0]
} else {
status = NativeLibraryManager.Security.SecKeychainAddGenericPassword(
Pointer.NULL, serviceBytes.length, serviceBytes,
accountBytes.length, accountBytes,
passwordBytes.length, passwordBytes, null);
}
if (status != 0) {
throw new PasswordSaveException(convertErrorCodeToMessage(status));
}
}