Reputation: 529
I have an encryption program and I was looking for a way in so that only the encryption program have access to they keys folder. But setting it to the owner or anyone in the users aren't a good idea. I was thinking maybe something like Steam did with its folder. It restricted access even to the owner/admin of the computer and only the Steam app can communicate/edit/access the folder. I was wondering how to do it in Java.
The code that I currently have right now is this.
Path file = Paths.get("F:\\keys\\pic.datakey");
UserPrincipal owner = file.GetFileSystem().getUserPrincipalLookupService()
.lookupPrincipalByName("username");
Files.setOwner(file, owner);
with keys
being the folder name and pic.datakey
is the key that I'm trying to prevent anyone except the program to have access with.
How to set the folder owner to the encryption program's?
Upvotes: 0
Views: 268
Reputation: 718986
The encryption program is not a principal. It cannot own things. What you need is to run the program as some special principal1.
Problems:
What principal should you use? There won't be a platform-independent answer to that. Indeed, there isn't an obvious platform-specific answer, at least for Linux.
How do you ensure that the encryption program runs as the designated principal. This is not solvable in Java. (Long story ... but mechanisms like UNIX setuid will require a non-Java (non-shell) launcher to implement them securely for a Java program.)
But once you have done those things, it will be unnecessary to change the owner of the file (as per your code). The owner will default to the principal under which the program is currently being run.
But here's the real question. What do you think you will achieve by hiding the encryption keys from the user who has encrypted the file?
If we assume that the user has full "root" access (or equivalent) then they can access any file stored locally on the system, either directly (i.e. as root) or with some extra effort. Certainly on a typical operating system.
And the flip-side is that if the user doesn't have "root" (or equivalent) access, you can stop him / her from seeing files by setting the owner / permissions of the files. Adding encryption doesn't achieve a lot more.
Yes it's a security measure.
Security against what? The user who owns the machine? Even assuming that it is technically feasible, is it a reasonable thing to do?
(These are rhetorical questions. I am posing them for you to think about, not because I either agree or disagree with what you are doing. And not because I want to debate this.)
1 - Suppose that the program runs as the user. First of all a user cannot transfer ownership of his files to another user. That would provide an easy way to "cheat" file usage accounting. Your encryption program running as the user couldn't do that either. (The OS cannot determine the intent of an operation.) Second, assuming an ordinary user could change the owner of a file, then after the change of ownership the user would not be able to read it. Moreover, neither could the encryption program.
Upvotes: 2