@ -54,6 +54,8 @@ import java.nio.file.Files;
import java.nio.file.InvalidPathException ;
import java.nio.file.InvalidPathException ;
import java.nio.file.Path ;
import java.nio.file.Path ;
import java.nio.file.Paths ;
import java.nio.file.Paths ;
import java.security.NoSuchAlgorithmException ;
import java.security.NoSuchProviderException ;
import java.text.MessageFormat ;
import java.text.MessageFormat ;
import java.util.Iterator ;
import java.util.Iterator ;
import java.util.Locale ;
import java.util.Locale ;
@ -67,6 +69,7 @@ import org.bouncycastle.gpg.keybox.KeyBox;
import org.bouncycastle.gpg.keybox.KeyInformation ;
import org.bouncycastle.gpg.keybox.KeyInformation ;
import org.bouncycastle.gpg.keybox.PublicKeyRingBlob ;
import org.bouncycastle.gpg.keybox.PublicKeyRingBlob ;
import org.bouncycastle.gpg.keybox.UserID ;
import org.bouncycastle.gpg.keybox.UserID ;
import org.bouncycastle.gpg.keybox.jcajce.JcaKeyBoxBuilder ;
import org.bouncycastle.openpgp.PGPException ;
import org.bouncycastle.openpgp.PGPException ;
import org.bouncycastle.openpgp.PGPPublicKey ;
import org.bouncycastle.openpgp.PGPPublicKey ;
import org.bouncycastle.openpgp.PGPSecretKey ;
import org.bouncycastle.openpgp.PGPSecretKey ;
@ -210,9 +213,12 @@ class BouncyCastleGpgKeyLocator {
* @return publicKey the public key ( maybe < code > null < / code > )
* @return publicKey the public key ( maybe < code > null < / code > )
* @throws IOException
* @throws IOException
* in case of problems reading the file
* in case of problems reading the file
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* /
* /
private PGPPublicKey findPublicKeyInKeyBox ( Path keyboxFile )
private PGPPublicKey findPublicKeyInKeyBox ( Path keyboxFile )
throws IOException {
throws IOException , NoSuchAlgorithmException ,
NoSuchProviderException {
KeyBox keyBox = readKeyBoxFile ( keyboxFile ) ;
KeyBox keyBox = readKeyBoxFile ( keyboxFile ) ;
for ( KeyBlob keyBlob : keyBox . getKeyBlobs ( ) ) {
for ( KeyBlob keyBlob : keyBox . getKeyBlobs ( ) ) {
if ( keyBlob . getType ( ) = = BlobType . OPEN_PGP_BLOB ) {
if ( keyBlob . getType ( ) = = BlobType . OPEN_PGP_BLOB ) {
@ -236,15 +242,17 @@ class BouncyCastleGpgKeyLocator {
* @return the secret key
* @return the secret key
* @throws IOException
* @throws IOException
* in case of issues reading key files
* in case of issues reading key files
* @throws NoSuchAlgorithmException
* @throws NoSuchProviderException
* @throws PGPException
* @throws PGPException
* in case of issues finding a key
* in case of issues finding a key
* @throws CanceledException
* @throws CanceledException
* @throws URISyntaxException
* @throws URISyntaxException
* @throws UnsupportedCredentialItem
* @throws UnsupportedCredentialItem
* /
* /
public BouncyCastleGpgKey findSecretKey ( )
public BouncyCastleGpgKey findSecretKey ( ) throws IOException ,
throws IOException , PGPException , Canceled Exception,
NoSuchAlgorithmException , NoSuchProviderException , PGP Exception,
UnsupportedCredentialItem , URISyntaxException {
CanceledException , UnsupportedCredentialItem , URISyntaxException {
if ( exists ( USER_KEYBOX_PATH ) ) {
if ( exists ( USER_KEYBOX_PATH ) ) {
PGPPublicKey publicKey = //
PGPPublicKey publicKey = //
findPublicKeyInKeyBox ( USER_KEYBOX_PATH ) ;
findPublicKeyInKeyBox ( USER_KEYBOX_PATH ) ;
@ -376,14 +384,12 @@ class BouncyCastleGpgKeyLocator {
. getPublicKey ( ) ;
. getPublicKey ( ) ;
}
}
private KeyBox readKeyBoxFile ( Path keyboxFile ) throws IOException {
private KeyBox readKeyBoxFile ( Path keyboxFile ) throws IOException ,
NoSuchAlgorithmException , NoSuchProviderException {
KeyBox keyBox ;
KeyBox keyBox ;
try ( InputStream in = new BufferedInputStream (
try ( InputStream in = new BufferedInputStream (
newInputStream ( keyboxFile ) ) ) {
newInputStream ( keyboxFile ) ) ) {
// note: KeyBox constructor reads in the whole InputStream at once
keyBox = new JcaKeyBoxBuilder ( ) . build ( in ) ;
// this code will change in 1.61 to
// either 'new BcKeyBox(in)' or 'new JcaKeyBoxBuilder().build(in)'
keyBox = new KeyBox ( in , new JcaKeyFingerprintCalculator ( ) ) ;
}
}
return keyBox ;
return keyBox ;
}
}