1

Setup:

  • Linux with GnuPG or Windows with GPG4Win(OpenPGP)
  • A 2048 RSA keypair has been created by a privileged user who can access the key ring
  • A second lower privilege user has been created for a java application to run under
  • Permission has been granted for this user to run GPG commands but cannot access physical key ring files
  • The key IDs are known to the java application and so is the passphrase to extract private key
  • Java application has imported Bouncycastle library
  • The java program needs to perform an encryption and decryption using the key pair

Problem:

I have successfully performed encryption and decryption using Bouncycastle. But it involved trying to read the public and private keys from the pubring and secring files. For security, I would rather not have the java application have direct access to the key ring files.

What are my options. Are there any options in Bouncycastle to do this without reading the key rings or exporting the keys to separate files?

Note: Using Bouncy castle is not necessary.

aland
  • 4,829
  • 2
  • 24
  • 42
MickJ
  • 2,127
  • 3
  • 20
  • 34
  • You can't use the keys without reading them. What kind of answers were you expecting? Like figuring out which commands you'd have to exec as the privileged user? – erickson Dec 01 '14 at 16:29
  • So I dont really know. Maybe the answer is no it cant be done. Maybe it is some way to communicate with the key agent that is more secure. Maybe it a well know jni based implementation or some other implementation to exec gpg commands. But the idea is that I would rather not allow the user the application is running as to access the physical key ring files. – MickJ Dec 01 '14 at 16:36

2 Answers2

1

Your Java application can call gnupg with whatever privileges the user that executes the application currently has. Fortunately, gnupg supports a wealth of command-line options that allow you to supply all necessary arguments and read/collect back status and results, such as -batch, -options or -status-fd. You can call programs from Java and read their results back using Java's ProcessBuilder or higher level libraries such as Plexus Utils

On the other hand, there is at least one java-based wrapper library that speaks directly with the GnuPG executable. While platform-dependent, this probably has speed advantages over spawning a process for each transaction; and will probably spare you a lot of implementation effort figuring out command-line options.

Note that using GnuPG this way bypasses BouncyCastle entirely -- you would be automating calls to GnuPG, effectively using it as your "library".

Community
  • 1
  • 1
tucuxi
  • 17,561
  • 2
  • 43
  • 74
  • Awesome. Thanks for the info, especially link to the java wrapper library link. I looked at the master which wasn't updated for 5 years and gave up on it. But I see that this branch has been more or less well maintained. I would give it a shot. – MickJ Dec 01 '14 at 16:52
1

BouncyCastle requires direct access to the key files, both the public and private keys.

Not Using BouncyCastle

If you want to prevent the Java application from accessing the key files, but still use it for encryption and decryption, you might be successful using GnuPG 2.1 which offloads all operation requiring access to the private keyring to the gpg-agent. A setup that might work would look somewhat like that:

  • Start gpg-agent
  • Restrict access for your Java application, so it can access the gpg-agent socket, but not the private key files

In Linux, you might consider using chroot or appropriate permissions for the socket and keyring files. In Windows, there might be something like a sandbox solution.

Building your own BouncyCastle Daemon

Similar to the small-weight gpg-agent, you could write your own daemon that handles all secret key operations, while not exposing the key at the same time (so you've got a small daemon with much lower chance of critical bugs; while the large, possibly insecure and exposed main application can send requests for encryption/decryption to the "agent" application).

Jens Erat
  • 37,523
  • 16
  • 80
  • 96
  • Thank you for the valuable advice. I appreciate it. Any suggestions on how to communicate with the gpg-agent from a java application. I really like the idea of breaking up the application into a gpg daemon and main application. – MickJ Dec 01 '14 at 16:55
  • Although it might be a possible way to go the hard route and implement communication with the agent on your own ([the protocol is described in the GnuPG documentation](https://gnupg.org/documentation/manuals/gnupg/Agent-Protocol.html#Agent-Protocol), I'd rather go for calling the GnuPG application instead. When only interfacing the `gpg-agent`, you'd still require the public key somewhere else for _encryption_ and handle the symmetric decryption in your application, as the agent just decrypts and returns the (symmetric) session key, which is different for each encrypted message. – Jens Erat Dec 01 '14 at 17:06
  • Thanks again for the info. Looking at the documentation, I too fear that I might be trying to get myself into too much so I am split between your suggestion of splitting the applicaiton into main and small one under elevated permissions to act as gpg agent vs @tucuxy's answer above. – MickJ Dec 01 '14 at 17:20