How do I protect my OpenPGP key on my computer? If I make a backup of my private key I will keep it secure, but if someone gets access to my computer he/she will can get all my keys through Kleopatra or GPA. How do I avoid this?
3 Answers
What's your threat model?
If your threat is that you are protecting from physical theft, then encrypting the key (using passphrase) or full disk encryption would suffice.
If your threat is a malicious program running under your user account, then you want to run potentially malicious programs in a sandbox, or just not run it at all.
If your threat is a malicious program with root access, then you should use an OpenPGP Card.
There are different ways to reduce impact of a compromised computer containing OpenPGP keys. I ordered them somewhat following paranoidity (you might prefer calling it "more advanced attackers"). Often, those counter measures can be combined as required.
Passphrase Protection
You can have your private key(s) encrypted by a passphrase. If the attacker has the encrypted private keys, but not the (reasonably chosen) passphrase, he will have a very hard time brute-forcing it.
This will not protect you in case the attacker not only got hold of a copy of your hard disk, but also gets continuous access to your computer, as he will be able to eavesdrop the passphrase.
Full Disk Encryption
Full disk encryption pretty much equals encrypting the passphrase, but might make setting up a trojan to get machine access harder. I'd combine it with passphrase encryption of the private key.
Using an Offline Primary Key
The worst thing that might happen is that your private primary key is exposed to the attacker, excelled by the attacker getting hold of it and at the same preventing you from accessing it. In this case, the attacker will not only be able to create new keys and certifications, but you can't even revoke them any more!
It might prove reasonable to move the private primary key to some offline location, maybe even some dedicated offline computer not connected to the internet. This will make it much harder for a possible attacker to get control over the private primary key, as long as physical access to the key/offline drive/machine is out of scope.
If the private primary key stays offline/secure, the only thing an attacker can do is decrypting old messages using an encryption subkey or signing messages using a signature subkey. You can revoke them at any time to limit the breach without losing all your certifications. Exchanging subkeys is a rather cheap action.
OpenPGP Smart Cards
Finally, to protect your private subkeys, you could use OpenPGP smart cards (or other implementation like YubiKeys). The key is created on the card, which it will never leave. It can only be used while the card is connected, or in the case of a YubiKey the hardware token must be touched to perform some cryptographic operations. These cards or hardware tokens contain their own crypto processor, so it is able to perform cryptographic operations on their own.
An attacker will lose access as soon as you removed the card, as he's not holding a copy of the key.
GnuPG supports those cards/tokens, but configuration might involve some hassles. The FSFE membership card is such a smartcard, but you can also buy them individually, look for "OpenPGP Smartcards".
Full disk encryption could solve the data at rest problem, but if someone can access your computer whilst it is on, there is very little you can do to prevent it. You can ensure you use a password for your private key, and avoid using an agent type program (which holds the key in memory), but that does introduce a usability disadvantage - you have to provide the password for the key every time you want to use it legitimately, and ensure that it is cleared from memory between those times.