Skip to content

Commit

Permalink
update bouncy castle 1.79
Browse files Browse the repository at this point in the history
  • Loading branch information
Zschimmer committed Dec 7, 2024
1 parent fe8e6f8 commit caea165
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import org.bouncycastle.openpgp.examples.PubringDump
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator
import org.bouncycastle.openpgp.{PGPPublicKey, PGPPublicKeyRing, PGPPublicKeyRingCollection, PGPSecretKey, PGPSecretKeyRing, PGPSecretKeyRingCollection, PGPSignature, PGPUtil}
import scala.annotation.nowarn
import scala.jdk.CollectionConverters.*
import scala.util.control.NonFatal

Expand Down Expand Up @@ -126,6 +127,7 @@ object PgpCommons:
case HashAlgorithmTags.TIGER_192 => "TIGER"
case _ => hashAlgorithm.toString

@nowarn("cat=deprecation")
private def publicKeyAlgorithmToString(n: Int) =
n match
case PublicKeyAlgorithmTags.RSA_GENERAL => "'RSA general'"
Expand All @@ -136,7 +138,7 @@ object PgpCommons:
case PublicKeyAlgorithmTags.DIFFIE_HELLMAN => "Diffie-Hellman"
case _ =>
try PubringDump.getAlgorithm(n)
catch { case NonFatal(_) => n.toString }
catch case NonFatal(_) => n.toString

private def cipherToString(n: Int) =
try PGPUtil.getSymmetricCipherName(n)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,51 @@ import js7.base.crypt.SignerId
import js7.base.generic.SecretString
import js7.base.log.Logger
import org.bouncycastle.bcpg.sig.{Features, KeyFlags}
import org.bouncycastle.bcpg.{HashAlgorithmTags, PublicKeyAlgorithmTags, SymmetricKeyAlgorithmTags}
import org.bouncycastle.bcpg.{HashAlgorithmTags, PublicKeyAlgorithmTags, PublicKeyPacket, SymmetricKeyAlgorithmTags}
import org.bouncycastle.crypto.AsymmetricCipherKeyPair
import org.bouncycastle.crypto.generators.RSAKeyPairGenerator
import org.bouncycastle.crypto.params.RSAKeyGenerationParameters
import org.bouncycastle.openpgp.operator.bc.{BcPBESecretKeyEncryptorBuilder, BcPGPContentSignerBuilder, BcPGPDigestCalculatorProvider, BcPGPKeyPair}
import org.bouncycastle.openpgp.{PGPKeyRingGenerator, PGPSecretKey, PGPSignature, PGPSignatureSubpacketGenerator, PGPSignatureSubpacketVector}
import org.jetbrains.annotations.TestOnly

/**
* @author Joacim Zschimmer
*/
object PgpKeyGenerator:
private val logger = Logger[this.type]

@TestOnly
def generateSecretKey(id: SignerId, password: SecretString, keySize: Int = 4096): PGPSecretKey =
// See https://stackoverflow.com/questions/3087049/bouncy-castle-rsa-keypair-generation-using-lightweight-api
val publicExponent = 0x10001 // Should be a Fermat number
val certainty = 80
val controllerSigningKeyPair = newKeyPair(new RSAKeyGenerationParameters(BigInteger.valueOf(publicExponent), new SecureRandom, keySize, certainty))
val shaCalculator = new BcPGPDigestCalculatorProvider().get(HashAlgorithmTags.SHA1) // "only SHA1 supported for key checksum calculations"
val controllerSigningKeyPair = newKeyPair:
new RSAKeyGenerationParameters(
BigInteger.valueOf(publicExponent),
new SecureRandom, keySize, certainty)
val shaCalculator = new BcPGPDigestCalculatorProvider()
.get(HashAlgorithmTags.SHA1) // "only SHA1 supported for key checksum calculations"
new PGPKeyRingGenerator(
PGPSignature.POSITIVE_CERTIFICATION,
controllerSigningKeyPair,
id.string,
shaCalculator,
signatureSubpackets,
null,
new BcPGPContentSignerBuilder(controllerSigningKeyPair.getPublicKey.getAlgorithm, HashAlgorithmTags.SHA512),
new BcPGPContentSignerBuilder(
controllerSigningKeyPair.getPublicKey.getAlgorithm,
HashAlgorithmTags.SHA512),
new BcPBESecretKeyEncryptorBuilder(SymmetricKeyAlgorithmTags.AES_256, shaCalculator)
.build(password.string.toArray)
).generateSecretKeyRing.getSecretKey

private def newKeyPair(parameters: RSAKeyGenerationParameters): BcPGPKeyPair =
new BcPGPKeyPair(PublicKeyAlgorithmTags.RSA_SIGN, newAsymmetricCipherKeyPair(parameters), new java.util.Date)
new BcPGPKeyPair(
PublicKeyPacket.VERSION_4,
PublicKeyAlgorithmTags.RSA_GENERAL,
newAsymmetricCipherKeyPair(parameters),
new java.util.Date)

private def newAsymmetricCipherKeyPair(parameters: RSAKeyGenerationParameters): AsymmetricCipherKeyPair =
val generator = new RSAKeyPairGenerator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import js7.service.pgp.PgpCommons.*
import org.bouncycastle.bcpg.HashAlgorithmTags
import org.bouncycastle.openpgp.operator.jcajce.{JcaPGPContentSignerBuilder, JcePBESecretKeyDecryptorBuilder}
import org.bouncycastle.openpgp.{PGPSecretKey, PGPSecretKeyRingCollection, PGPSignature, PGPSignatureGenerator, PGPSignatureSubpacketGenerator, PGPUtil}
import org.jetbrains.annotations.TestOnly
import scala.jdk.CollectionConverters.*
import scala.util.Random

Expand Down Expand Up @@ -74,6 +75,7 @@ object PgpSigner extends DocumentSigner.Companion:
Checked.catchNonFatal(
new PgpSigner(pgpSecretKey, password))

@TestOnly
def forTest(): (PgpSigner, PgpSignatureVerifier) =
val pgpPassword = SecretString(Vector.fill(10)('a' + Random.nextInt('z' - 'a' + 1)).mkString)
val pgpSecretKey = PgpKeyGenerator.generateSecretKey(SignerId("TEST"), pgpPassword, keySize = 1024/*fast for test*/)
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ object Dependencies
val intelliJAnnotations = "com.intellij" % "annotations" % "12.0"

val findbugs = "com.google.code.findbugs" % "jsr305" % "3.0.2"
val bouncyCastle = "org.bouncycastle" % "bcpg-jdk18on" % "1.78.1"
val bouncyCastle = "org.bouncycastle" % "bcpg-jdk18on" % "1.79"
val hamcrest = "org.hamcrest" % "hamcrest" % "3.0" ::
"org.hamcrest" % "hamcrest-library" % "3.0" :: Nil
val jna = "net.java.dev.jna" % "jna-platform" % jnaVersion ::
Expand Down

0 comments on commit caea165

Please sign in to comment.