Package org.eclipse.jgit.gpg.bc.internal
Class BouncyCastleGpgKeyLocator
- java.lang.Object
-
- org.eclipse.jgit.gpg.bc.internal.BouncyCastleGpgKeyLocator
-
public class BouncyCastleGpgKeyLocator extends java.lang.ObjectLocates GPG keys from either~/.gnupg/private-keys-v1.dor~/.gnupg/secring.gpg
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private static classBouncyCastleGpgKeyLocator.EncryptedPgpKeyExceptionThrown if we try to read an encrypted private key without password.private static classBouncyCastleGpgKeyLocator.NoOpenPgpKeyExceptionThrown if a keybox file exists but doesn't contain an OpenPGP key.
-
Field Summary
Fields Modifier and Type Field Description private static java.nio.file.PathGPG_DIRECTORYprivate static org.slf4j.Loggerlogprivate BouncyCastleGpgKeyPassphrasePromptpassphrasePromptprivate java.lang.StringsigningKeyprivate static java.nio.file.PathUSER_KEYBOX_PATHprivate static java.nio.file.PathUSER_PGP_LEGACY_SECRING_FILEprivate static java.nio.file.PathUSER_PGP_PUBRING_FILEprivate static java.nio.file.PathUSER_SECRET_KEY_DIR
-
Constructor Summary
Constructors Constructor Description BouncyCastleGpgKeyLocator(java.lang.String signingKey, BouncyCastleGpgKeyPassphrasePrompt passphrasePrompt)Create a new key locator for the specified signing key.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private org.bouncycastle.openpgp.PGPSecretKeyattemptParseSecretKey(java.nio.file.Path keyFile, org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider calculatorProvider, org.bouncycastle.openpgp.operator.PBEProtectionRemoverFactory passphraseProvider, org.bouncycastle.openpgp.PGPPublicKey publicKey)(package private) static booleancontainsSigningKey(java.lang.String userId, java.lang.String signingKeySpec)Checks whether a given OpenPGPuserIdmatches a givensigningKeySpec, which is supposed to have one of the formats defined by GPG.private static java.nio.file.PathfindGpgDirectory()private org.bouncycastle.openpgp.PGPPublicKeyfindPublicKeyByKeyId(org.bouncycastle.gpg.keybox.KeyBlob keyBlob)private org.bouncycastle.openpgp.PGPPublicKeyfindPublicKeyByUserId(org.bouncycastle.gpg.keybox.KeyBlob keyBlob)private org.bouncycastle.openpgp.PGPPublicKeyfindPublicKeyInKeyBox(java.nio.file.Path keyboxFile)Finds a public key associated with the signing key.private org.bouncycastle.openpgp.PGPPublicKeyfindPublicKeyInPubring(java.nio.file.Path pubringFile)Return the first public key matching the key id (signingKey.BouncyCastleGpgKeyfindSecretKey()If there is a private key directory containing keys, use pubring.kbx or pubring.gpg to find the public key; then try to find the secret key in the directory.private BouncyCastleGpgKeyfindSecretKeyForKeyBoxPublicKey(org.bouncycastle.openpgp.PGPPublicKey publicKey, java.nio.file.Path userKeyboxPath)private org.bouncycastle.openpgp.PGPSecretKeyfindSecretKeyInLegacySecring(java.lang.String signingkey, java.nio.file.Path secringFile)Return the first suitable key for signing in the key ring collection.private org.bouncycastle.openpgp.PGPPublicKeygetPublicKey(org.bouncycastle.gpg.keybox.KeyBlob blob, byte[] fingerprint)private org.bouncycastle.openpgp.PGPPublicKeygetSigningPublicKey(org.bouncycastle.gpg.keybox.KeyBlob blob)private booleanhasKeyFiles(java.nio.file.Path dir)private booleanisSigningKey(org.bouncycastle.openpgp.PGPPublicKey key)private BouncyCastleGpgKeyloadKeyFromSecring(java.nio.file.Path secring)private org.bouncycastle.gpg.keybox.KeyBoxreadKeyBoxFile(java.nio.file.Path keyboxFile)private java.lang.StringtoFingerprint(java.lang.String keyId)
-
-
-
Field Detail
-
log
private static final org.slf4j.Logger log
-
GPG_DIRECTORY
private static final java.nio.file.Path GPG_DIRECTORY
-
USER_KEYBOX_PATH
private static final java.nio.file.Path USER_KEYBOX_PATH
-
USER_SECRET_KEY_DIR
private static final java.nio.file.Path USER_SECRET_KEY_DIR
-
USER_PGP_PUBRING_FILE
private static final java.nio.file.Path USER_PGP_PUBRING_FILE
-
USER_PGP_LEGACY_SECRING_FILE
private static final java.nio.file.Path USER_PGP_LEGACY_SECRING_FILE
-
signingKey
private final java.lang.String signingKey
-
passphrasePrompt
private BouncyCastleGpgKeyPassphrasePrompt passphrasePrompt
-
-
Constructor Detail
-
BouncyCastleGpgKeyLocator
public BouncyCastleGpgKeyLocator(java.lang.String signingKey, @NonNull BouncyCastleGpgKeyPassphrasePrompt passphrasePrompt)Create a new key locator for the specified signing key.The signing key must either be a hex representation of a specific key or a user identity substring (eg., email address). All keys in the KeyBox will be looked up in the order as returned by the KeyBox. A key id will be searched before attempting to find a key by user id.
- Parameters:
signingKey- the signing key to search forpassphrasePrompt- the provider to use when asking for key passphrase
-
-
Method Detail
-
findGpgDirectory
private static java.nio.file.Path findGpgDirectory()
-
attemptParseSecretKey
private org.bouncycastle.openpgp.PGPSecretKey attemptParseSecretKey(java.nio.file.Path keyFile, org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider calculatorProvider, org.bouncycastle.openpgp.operator.PBEProtectionRemoverFactory passphraseProvider, org.bouncycastle.openpgp.PGPPublicKey publicKey)
-
containsSigningKey
static boolean containsSigningKey(java.lang.String userId, java.lang.String signingKeySpec)Checks whether a given OpenPGPuserIdmatches a givensigningKeySpec, which is supposed to have one of the formats defined by GPG.Not all formats are supported; only formats starting with '=', '<', '@', and '*' are handled. Any other format results in a case-insensitive substring match.
- Parameters:
userId- of a keysigningKeySpec- GPG key identification- Returns:
- whether the
userIdmatches - See Also:
- GPG Documentation: How to Specify a User ID
-
toFingerprint
private java.lang.String toFingerprint(java.lang.String keyId)
-
findPublicKeyByKeyId
private org.bouncycastle.openpgp.PGPPublicKey findPublicKeyByKeyId(org.bouncycastle.gpg.keybox.KeyBlob keyBlob) throws java.io.IOException- Throws:
java.io.IOException
-
findPublicKeyByUserId
private org.bouncycastle.openpgp.PGPPublicKey findPublicKeyByUserId(org.bouncycastle.gpg.keybox.KeyBlob keyBlob) throws java.io.IOException- Throws:
java.io.IOException
-
findPublicKeyInKeyBox
private org.bouncycastle.openpgp.PGPPublicKey findPublicKeyInKeyBox(java.nio.file.Path keyboxFile) throws java.io.IOException, java.security.NoSuchAlgorithmException, java.security.NoSuchProviderException, BouncyCastleGpgKeyLocator.NoOpenPgpKeyExceptionFinds a public key associated with the signing key.- Parameters:
keyboxFile- the KeyBox file- Returns:
- publicKey the public key (maybe
null) - Throws:
java.io.IOException- in case of problems reading the filejava.security.NoSuchAlgorithmExceptionjava.security.NoSuchProviderExceptionBouncyCastleGpgKeyLocator.NoOpenPgpKeyException- if the file does not contain any OpenPGP key
-
findSecretKey
@NonNull public BouncyCastleGpgKey findSecretKey() throws java.io.IOException, java.security.NoSuchAlgorithmException, java.security.NoSuchProviderException, org.bouncycastle.openpgp.PGPException, CanceledException, UnsupportedCredentialItem, java.net.URISyntaxException
If there is a private key directory containing keys, use pubring.kbx or pubring.gpg to find the public key; then try to find the secret key in the directory.If there is no private key directory (or it doesn't contain any keys), try to find the key in secring.gpg directly.
- Returns:
- the secret key
- Throws:
java.io.IOException- in case of issues reading key filesjava.security.NoSuchAlgorithmExceptionjava.security.NoSuchProviderExceptionorg.bouncycastle.openpgp.PGPException- in case of issues finding a key, including no key foundCanceledExceptionjava.net.URISyntaxExceptionUnsupportedCredentialItem
-
hasKeyFiles
private boolean hasKeyFiles(java.nio.file.Path dir)
-
loadKeyFromSecring
private BouncyCastleGpgKey loadKeyFromSecring(java.nio.file.Path secring) throws java.io.IOException, org.bouncycastle.openpgp.PGPException
- Throws:
java.io.IOExceptionorg.bouncycastle.openpgp.PGPException
-
findSecretKeyForKeyBoxPublicKey
private BouncyCastleGpgKey findSecretKeyForKeyBoxPublicKey(org.bouncycastle.openpgp.PGPPublicKey publicKey, java.nio.file.Path userKeyboxPath) throws org.bouncycastle.openpgp.PGPException, CanceledException, UnsupportedCredentialItem, java.net.URISyntaxException
- Throws:
org.bouncycastle.openpgp.PGPExceptionCanceledExceptionUnsupportedCredentialItemjava.net.URISyntaxException
-
findSecretKeyInLegacySecring
private org.bouncycastle.openpgp.PGPSecretKey findSecretKeyInLegacySecring(java.lang.String signingkey, java.nio.file.Path secringFile) throws java.io.IOException, org.bouncycastle.openpgp.PGPExceptionReturn the first suitable key for signing in the key ring collection. For this case we only expect there to be one key available for signing.- Parameters:
signingkey-secringFile-- Returns:
- the first suitable PGP secret key found for signing
- Throws:
java.io.IOException- on I/O related errorsorg.bouncycastle.openpgp.PGPException- on BouncyCastle errors
-
findPublicKeyInPubring
private org.bouncycastle.openpgp.PGPPublicKey findPublicKeyInPubring(java.nio.file.Path pubringFile) throws java.io.IOException, org.bouncycastle.openpgp.PGPExceptionReturn the first public key matching the key id (signingKey.- Parameters:
pubringFile-- Returns:
- the PGP public key, or
nullif none found - Throws:
java.io.IOException- on I/O related errorsorg.bouncycastle.openpgp.PGPException- on BouncyCastle errors
-
getPublicKey
private org.bouncycastle.openpgp.PGPPublicKey getPublicKey(org.bouncycastle.gpg.keybox.KeyBlob blob, byte[] fingerprint) throws java.io.IOException- Throws:
java.io.IOException
-
getSigningPublicKey
private org.bouncycastle.openpgp.PGPPublicKey getSigningPublicKey(org.bouncycastle.gpg.keybox.KeyBlob blob) throws java.io.IOException- Throws:
java.io.IOException
-
isSigningKey
private boolean isSigningKey(org.bouncycastle.openpgp.PGPPublicKey key)
-
readKeyBoxFile
private org.bouncycastle.gpg.keybox.KeyBox readKeyBoxFile(java.nio.file.Path keyboxFile) throws java.io.IOException, java.security.NoSuchAlgorithmException, java.security.NoSuchProviderException, BouncyCastleGpgKeyLocator.NoOpenPgpKeyException- Throws:
java.io.IOExceptionjava.security.NoSuchAlgorithmExceptionjava.security.NoSuchProviderExceptionBouncyCastleGpgKeyLocator.NoOpenPgpKeyException
-
-