Top Description Inners Fields Constructors Methods
org.eclipse.jgit.gpg.bc.internal

public Class BouncyCastleGpgKeyLocator

extends Object
Class Inheritance
Static Imports
java.nio.file.Files.exists, .Files.newInputStream

Locates GPG keys from either ~/.gnupg/private-keys-v1.d or ~/.gnupg/secring.gpg

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
BouncyCastleGpgKeyLocator.NoOpenPgpKeyException

Thrown if a keybox file exists but doesn't contain an OpenPGP key.

Field Summary

Modifier and TypeField and Description
pack-priv static final Path
private static final Logger
private BouncyCastleGpgKeyPassphrasePrompt
private final String
private static final Path
private static final Path
private static final Path
private static final Path

Constructor Summary

AccessConstructor and Description
public
BouncyCastleGpgKeyLocator(String
the signing key to search for
signingKey
,
BouncyCastleGpgKeyPassphrasePrompt
the provider to use when asking for key passphrase
passphrasePrompt
)

Create a new key locator for the specified signing key.

Method Summary

Modifier and TypeMethod and Description
private PGPSecretKey
attemptParseSecretKey(Path keyFile, PGPDigestCalculatorProvider calculatorProvider, SecretKeys.PassphraseSupplier passphraseSupplier, PGPPublicKey publicKey)

private static Path
private static boolean
pack-priv static boolean

Returns:

whether the userId matches
containsSigningKey
(String
of a key
userId
,
String
GPG key identification
signingKeySpec
)

Checks whether a given OpenPGP userId matches a given signingKeySpec, which is supposed to have one of the formats defined by GPG.

private static Path
pack-priv static BouncyCastleGpgPublicKey
findPublicKey(String fingerprint, String keySpec)

private static PGPPublicKey
private static PGPPublicKey
private static BouncyCastleGpgPublicKey

Returns:

publicKey the public key (maybe null)
findPublicKeyInKeyBox
(Path
the KeyBox file
keyboxFile
,
String
to look for, may be null
keyId
,
String
to look for
keySpec
)

Finds a public key associated with the signing key.

private static BouncyCastleGpgPublicKey

Returns:

the PGP public key, or null if none found
findPublicKeyInPubring
(Path
to search
pubringFile
,
String
to look for, may be null
keyId
,
String
to look for
keySpec
)

Return the first public key matching the key id (signingKey.

private static BouncyCastleGpgPublicKey
public BouncyCastleGpgKey

Returns:

the secret key
findSecretKey
()

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 BouncyCastleGpgKey
private PGPSecretKey

Returns:

the first suitable PGP secret key found for signing
findSecretKeyInLegacySecring
(String
the signing key
signingKeyName
,
Path
the secring file
secringFile
)

Return the first suitable key for signing in the key ring collection.

private static PGPPublicKey
getPublicKey(KeyBlob blob, byte[] fingerprint)

private static PGPPublicKey
private boolean
private static boolean
private BouncyCastleGpgKey
private static KeyBox
readKeyBoxFile(Path keyboxFile)

private static String
private static List<String>
toStrings(List<UserID> userIds)

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

GPG_DIRECTORYback to summary
pack-priv static final Path GPG_DIRECTORY
logback to summary
private static final Logger log
passphrasePromptback to summary
private BouncyCastleGpgKeyPassphrasePrompt passphrasePrompt
signingKeyback to summary
private final String signingKey
USER_KEYBOX_PATHback to summary
private static final Path USER_KEYBOX_PATH
USER_PGP_LEGACY_SECRING_FILEback to summary
private static final Path USER_PGP_LEGACY_SECRING_FILE
USER_PGP_PUBRING_FILEback to summary
private static final Path USER_PGP_PUBRING_FILE
USER_SECRET_KEY_DIRback to summary
private static final Path USER_SECRET_KEY_DIR

Constructor Detail

BouncyCastleGpgKeyLocatorback to summary
public BouncyCastleGpgKeyLocator(String signingKey, 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:String

the signing key to search for

passphrasePrompt:BouncyCastleGpgKeyPassphrasePrompt

the provider to use when asking for key passphrase

Method Detail

attemptParseSecretKeyback to summary
private PGPSecretKey attemptParseSecretKey(Path keyFile, PGPDigestCalculatorProvider calculatorProvider, SecretKeys.PassphraseSupplier passphraseSupplier, PGPPublicKey publicKey) throws IOException, PGPException, CanceledException, UnsupportedCredentialItem, URISyntaxException
checkDirectoryback to summary
private static Path checkDirectory(String dir, Function<String, Path> toPath, Consumer<String> warn)
containsIgnoreCaseback to summary
private static boolean containsIgnoreCase(String a, String b)
containsSigningKeyback to summary
pack-priv static boolean containsSigningKey(String userId, String signingKeySpec)

Checks whether a given OpenPGP userId matches a given signingKeySpec, 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:String

of a key

signingKeySpec:String

GPG key identification

Returns:boolean

whether the userId matches

See Also
GPG Documentation: How to Specify a User ID
findGpgDirectoryback to summary
private static Path findGpgDirectory()
findPublicKeyback to summary
pack-priv static BouncyCastleGpgPublicKey findPublicKey(String fingerprint, String keySpec) throws IOException, PGPException
findPublicKeyByKeyIdback to summary
private static PGPPublicKey findPublicKeyByKeyId(KeyBlob keyBlob, String keyId) throws IOException
findPublicKeyByUserIdback to summary
private static PGPPublicKey findPublicKeyByUserId(KeyBlob keyBlob, String keySpec) throws IOException
findPublicKeyInKeyBoxback to summary
private static BouncyCastleGpgPublicKey findPublicKeyInKeyBox(Path keyboxFile, String keyId, String keySpec) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, NoOpenPgpKeyException

Finds a public key associated with the signing key.

Parameters
keyboxFile:Path

the KeyBox file

keyId:String

to look for, may be null

keySpec:String

to look for

Returns:BouncyCastleGpgPublicKey

publicKey the public key (maybe null)

Exceptions
IOException:
in case of problems reading the file
NoSuchAlgorithmException:
if an algorithm isn't available
NoSuchProviderException:
if a provider isn't available
NoOpenPgpKeyException:
if the file does not contain any OpenPGP key
findPublicKeyInPubringback to summary
private static BouncyCastleGpgPublicKey findPublicKeyInPubring(Path pubringFile, String keyId, String keySpec) throws IOException, PGPException

Return the first public key matching the key id (signingKey.

Parameters
pubringFile:Path

to search

keyId:String

to look for, may be null

keySpec:String

to look for

Returns:BouncyCastleGpgPublicKey

the PGP public key, or null if none found

Exceptions
IOException:
on I/O related errors
PGPException:
on BouncyCastle errors
findPublicKeyInPubringback to summary
private static BouncyCastleGpgPublicKey findPublicKeyInPubring(PGPPublicKeyRing keyRing, String keyId, String keySpec)
findSecretKeyback to summary
public BouncyCastleGpgKey findSecretKey() throws IOException, NoSuchAlgorithmException, NoSuchProviderException, PGPException, CanceledException, UnsupportedCredentialItem, 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:BouncyCastleGpgKey

the secret key

Annotations
@NonNull
Exceptions
IOException:
in case of issues reading key files
NoSuchAlgorithmException:
algorithm is not available
NoSuchProviderException:
provider is not available
PGPException:
in case of issues finding a key, including no key found
CanceledException:
operation was cancelled
UnsupportedCredentialItem:
credential item is not supported
URISyntaxException:
URI is invalid
findSecretKeyForKeyBoxPublicKeyback to summary
private BouncyCastleGpgKey findSecretKeyForKeyBoxPublicKey(PGPPublicKey publicKey, Path userKeyboxPath) throws PGPException, CanceledException, UnsupportedCredentialItem, URISyntaxException
findSecretKeyInLegacySecringback to summary
private PGPSecretKey findSecretKeyInLegacySecring(String signingKeyName, Path secringFile) throws IOException, PGPException

Return 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
signingKeyName:String

the signing key

secringFile:Path

the secring file

Returns:PGPSecretKey

the first suitable PGP secret key found for signing

Exceptions
IOException:
on I/O related errors
PGPException:
on BouncyCastle errors
getPublicKeyback to summary
private static PGPPublicKey getPublicKey(KeyBlob blob, byte[] fingerprint) throws IOException
getSigningPublicKeyback to summary
private static PGPPublicKey getSigningPublicKey(KeyBlob blob) throws IOException
hasKeyFilesback to summary
private boolean hasKeyFiles(Path dir)
isSigningKeyback to summary
private static boolean isSigningKey(PGPPublicKey key)
loadKeyFromSecringback to summary
private BouncyCastleGpgKey loadKeyFromSecring(Path secring) throws IOException, PGPException
readKeyBoxFileback to summary
private static KeyBox readKeyBoxFile(Path keyboxFile) throws IOException, NoSuchAlgorithmException, NoSuchProviderException, NoOpenPgpKeyException
toFingerprintback to summary
private static String toFingerprint(String keyId)
toStringsback to summary
private static List<String> toStrings(List<UserID> userIds)
org.eclipse.jgit.gpg.bc.internal back to summary

private Class BouncyCastleGpgKeyLocator.NoOpenPgpKeyException

extends Exception
Class Inheritance

Thrown if a keybox file exists but doesn't contain an OpenPGP key.

Field Summary

Modifier and TypeField and Description
private static final long

Constructor Summary

AccessConstructor and Description
private

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Exception.serialVersionUID.

Constructor Detail

NoOpenPgpKeyExceptionback to summary
private NoOpenPgpKeyException()