-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* remove commons-openpgp dependency and implement the necessary with …
…BouncyCastle APIs directly
- Loading branch information
Showing
8 changed files
with
170 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package net.filebot.ant.spk.openpgp; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
|
||
import org.bouncycastle.openpgp.PGPObjectFactory; | ||
import org.bouncycastle.openpgp.PGPSecretKey; | ||
import org.bouncycastle.openpgp.PGPSecretKeyRing; | ||
import org.bouncycastle.openpgp.PGPUtil; | ||
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory; | ||
|
||
public class OpenPGPSecretKey { | ||
|
||
private static final long MASK = 0xFFFFFFFFL; | ||
|
||
private PGPSecretKey secretKey; | ||
private char[] password; | ||
|
||
public OpenPGPSecretKey(String keyId, InputStream secretKeyRing, char[] password) throws IOException { | ||
PGPObjectFactory pgpObjectFactory = new BcPGPObjectFactory(PGPUtil.getDecoderStream(secretKeyRing)); | ||
|
||
for (Object it = pgpObjectFactory.nextObject(); it != null; it = pgpObjectFactory.nextObject()) { | ||
PGPSecretKeyRing pgpSecretKeyRing = (PGPSecretKeyRing) it; | ||
PGPSecretKey pgpSecretKey = pgpSecretKeyRing.getSecretKey(); | ||
|
||
if (keyId == null || keyId.isEmpty() || Long.valueOf(keyId, 16) == (pgpSecretKey.getKeyID() & MASK)) { | ||
this.secretKey = pgpSecretKey; | ||
break; | ||
} | ||
} | ||
|
||
// sanity check | ||
if (secretKey == null) { | ||
throw new IllegalArgumentException("Secret key " + keyId + " not found"); | ||
} | ||
|
||
this.password = password; | ||
} | ||
|
||
public PGPSecretKey getSecretKey() { | ||
return secretKey; | ||
} | ||
|
||
public char[] getPassword() { | ||
return password; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package net.filebot.ant.spk.openpgp; | ||
|
||
import java.io.ByteArrayOutputStream; | ||
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.FileNotFoundException; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.security.Security; | ||
import java.security.SignatureException; | ||
|
||
import org.bouncycastle.bcpg.ArmoredOutputStream; | ||
import org.bouncycastle.bcpg.BCPGOutputStream; | ||
import org.bouncycastle.bcpg.HashAlgorithmTags; | ||
import org.bouncycastle.jce.provider.BouncyCastleProvider; | ||
import org.bouncycastle.openpgp.PGPException; | ||
import org.bouncycastle.openpgp.PGPPrivateKey; | ||
import org.bouncycastle.openpgp.PGPSignature; | ||
import org.bouncycastle.openpgp.PGPSignatureGenerator; | ||
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; | ||
import org.bouncycastle.openpgp.operator.PGPDigestCalculatorProvider; | ||
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPContentSignerBuilder; | ||
import org.bouncycastle.openpgp.operator.jcajce.JcaPGPDigestCalculatorProviderBuilder; | ||
import org.bouncycastle.openpgp.operator.jcajce.JcePBESecretKeyDecryptorBuilder; | ||
|
||
public class OpenPGPSignature { | ||
|
||
static { | ||
Security.addProvider(new BouncyCastleProvider()); | ||
} | ||
|
||
private PGPSignatureGenerator signature; | ||
|
||
public OpenPGPSignature(OpenPGPSecretKey key) throws PGPException { | ||
PGPDigestCalculatorProvider pgpDigestCalculator = new JcaPGPDigestCalculatorProviderBuilder().setProvider(BouncyCastleProvider.PROVIDER_NAME).build(); | ||
PBESecretKeyDecryptor pbeSecretKeyDecryptor = new JcePBESecretKeyDecryptorBuilder(pgpDigestCalculator).setProvider(BouncyCastleProvider.PROVIDER_NAME).build(key.getPassword()); | ||
JcaPGPContentSignerBuilder pgpContentSigner = new JcaPGPContentSignerBuilder(key.getSecretKey().getPublicKey().getAlgorithm(), HashAlgorithmTags.SHA1).setProvider(BouncyCastleProvider.PROVIDER_NAME).setDigestProvider(BouncyCastleProvider.PROVIDER_NAME); | ||
|
||
signature = new PGPSignatureGenerator(pgpContentSigner); | ||
|
||
PGPPrivateKey privateKey = key.getSecretKey().extractPrivateKey(pbeSecretKeyDecryptor); | ||
signature.init(PGPSignature.BINARY_DOCUMENT, privateKey); | ||
} | ||
|
||
public void update(byte[] buffer, int offset, int length) throws SignatureException { | ||
signature.update(buffer, offset, length); | ||
} | ||
|
||
public void generate(OutputStream output, boolean asciiArmor) throws IOException, SignatureException, PGPException { | ||
if (asciiArmor) { | ||
output = new ArmoredOutputStream(output); | ||
} | ||
signature.generate().encode(new BCPGOutputStream(output)); | ||
} | ||
|
||
public byte[] generate(boolean asciiArmor) throws IOException, SignatureException, PGPException { | ||
ByteArrayOutputStream out = new ByteArrayOutputStream(1024); | ||
generate(out, asciiArmor); | ||
return out.toByteArray(); | ||
} | ||
|
||
public static OpenPGPSignature createSignatureGenerator(String keyId, File secring, char[] password) throws FileNotFoundException, IOException, PGPException { | ||
try (InputStream secretKeyRing = new FileInputStream(secring)) { | ||
OpenPGPSecretKey key = new OpenPGPSecretKey(keyId, secretKeyRing, password); | ||
OpenPGPSignature signature = new OpenPGPSignature(key); | ||
return signature; | ||
} | ||
} | ||
|
||
} |