Skip to content

Commit a46adf3

Browse files
committed
Added PBMAC1 support to pkcs package MAC verifier provider.
Added BC provider support for LEA, relates to github #1880.
1 parent 31fe836 commit a46adf3

8 files changed

Lines changed: 1350 additions & 404 deletions

File tree

‎docs/releasenotes.html‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ <h3>2.1.2 Defects Fixed</h3>
3131
<li>PGPPublicKey.getValidSeconds() returned a stale expiration time when an earlier self-signature carried a Key Expiration Time subpacket and a more recent self-signature omitted it; per RFC 4880 5.2.4.1 the latest self-signature wins, so the absence of the subpacket on the newer signature now correctly cancels the expiry and the method returns 0 (issue #1749).</li>
3232
<li>PEMParser failed to parse a "BEGIN PRIVATE KEY" block carrying OpenSSL-legacy encryption headers (Proc-Type: 4,ENCRYPTED / DEK-Info), throwing "corrupted stream" while ASN.1-decoding the ciphertext. The PRIVATE KEY parser now honours those headers and returns a PEMEncryptedKeyPair whose decryptKeyPair() yields a PEMKeyPair holding the decrypted PKCS#8 PrivateKeyInfo (issue #1238).</li>
3333
<li>PKIXCertPathValidatorSpi (and its JDK 8+ revocation-checker-aware variant) threw a NullPointerException when validating against a TrustAnchor constructed with (caName, caPublicKey, nameConstraints) instead of an X509Certificate, because the trust anchor's certificate encoding was always validated up front. The check is now skipped when no certificate is supplied, allowing name-and-key trust anchors to validate as expected (issue #1420).</li>
34+
<li>PKCS12PfxPdu.isMacValid threw ClassCastException when the PFX used PBMAC1 (id-PBMAC1) for integrity, because JcePKCS12MacCalculatorBuilderProvider tried to parse the algorithm parameters as PKCS12PBEParams. The provider now dispatches to JcePBMac1CalculatorBuilder for id-PBMAC1, and isMacValid compares only the inner MAC digest bytes for PBMAC1 (RFC 9579 sec. 6 leaves the MacData salt and iterations unused, so producers may write arbitrary placeholder values) — PBMAC1 PFX files written by OpenSSL and BCJSSE now verify correctly.</li>
3435
</ul>
3536
<h3>2.2.3 Additional Features and Functionality</h3>
3637
<ul>
@@ -44,6 +45,7 @@ <h3>2.2.3 Additional Features and Functionality</h3>
4445
<li>org.bouncycastle.gpg.KeyGripCalculator computes the GnuPG-style 20-byte SHA-1 keygrip for a BCPGKey. The calculator is constructed with a caller-supplied SHA-1 PGPDigestCalculator. RSA public keys are supported initially (matching libgcrypt's _gcry_rsa_compute_keygrip: SHA-1 of the canonical unsigned big-endian modulus); other key types throw on calculateKeygrip() until support is added (issue #676).</li>
4546
<li>CMS key transport now supports the SM2 cipher: JceKeyTransRecipientInfoGenerator wraps the CEK and JceKeyTransRecipient unwraps it when the keyEncryptionAlgorithm is GMObjectIdentifiers.sm2encrypt_with_sm3. The ciphertext format defaults to C1C3C2 (GB/T 35276 envelope encoding) and the SM4-CBC content encryption is exposed as the new CMSAlgorithm.SM4_CBC constant.</li>
4647
<li>org.bouncycastle.asn1.pkcs.SecretBag — RFC 7292 ASN.1 holder for the PKCS#12 secretBag bag type, complementing the existing CertBag / CRLBag classes. PKCS12SecretBag and PKCS12SecretBagBuilder in org.bouncycastle.pkcs sit alongside the SafeBag / SafeBagBuilder pair: PKCS12SafeBagBuilder takes a PKCS12SecretBag in a new constructor, and PKCS12SafeBag.getBagValue() returns a PKCS12SecretBag for safe bags of type secretBag.</li>
48+
<li>JCE provider plumbing for the LEA (Lightweight Encryption Algorithm) block cipher built on the existing core LEAEngine. Cipher.LEA (ECB) plus the standard transformation forms (LEA/CBC/PKCS5Padding etc.), Cipher.LEA-GCM, Cipher.LEA-CCM, Mac.LEA-CMAC / LEA-GMAC / LEA-Poly1305, KeyGenerator.LEA and SecretKeyFactory.LEA are registered, with 128/192/256-bit keys supported.</li>
4749
</ul>
4850

4951
<a id="r1rv84"><h3>2.2.1 Version</h3></a>

‎pkix/src/main/java/org/bouncycastle/pkcs/PKCS12PfxPdu.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ public boolean isMacValid(PKCS12MacCalculatorBuilderProvider macCalcProviderBuil
141141

142142
MacData mData = mdGen.build(password, pfxContents);
143143

144+
// RFC 9579 sec. 6: for PBMAC1 the MacData salt and iterations fields are
145+
// unused, so producers (e.g. OpenSSL) may write arbitrary placeholders.
146+
// Compare only the inner MAC digest bytes in that case.
147+
if (PKCSObjectIdentifiers.id_PBMAC1.equals(macAlgOid))
148+
{
149+
return Arrays.constantTimeAreEqual(
150+
mData.getMac().getDigest(), pfxmData.getMac().getDigest());
151+
}
152+
144153
return Arrays.constantTimeAreEqual(mData.getEncoded(), pfxmData.getEncoded());
145154
}
146155
catch (IOException e)

‎pkix/src/main/java/org/bouncycastle/pkcs/jcajce/JcePKCS12MacCalculatorBuilderProvider.java‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99

1010
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
1111
import org.bouncycastle.asn1.DERNull;
12+
import org.bouncycastle.asn1.pkcs.PBMAC1Params;
1213
import org.bouncycastle.asn1.pkcs.PKCS12PBEParams;
14+
import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
1315
import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
1416
import org.bouncycastle.jcajce.PKCS12Key;
1517
import org.bouncycastle.jcajce.io.MacOutputStream;
@@ -49,6 +51,25 @@ public JcePKCS12MacCalculatorBuilderProvider setProvider(String providerName)
4951

5052
public PKCS12MacCalculatorBuilder get(final AlgorithmIdentifier algorithmIdentifier)
5153
{
54+
if (PKCSObjectIdentifiers.id_PBMAC1.equals(algorithmIdentifier.getAlgorithm()))
55+
{
56+
final PBMAC1Params pbmac1Params = PBMAC1Params.getInstance(algorithmIdentifier.getParameters());
57+
58+
return new PKCS12MacCalculatorBuilder()
59+
{
60+
public MacCalculator build(char[] password)
61+
throws OperatorCreationException
62+
{
63+
return new JcePBMac1CalculatorBuilder(pbmac1Params).setHelper(helper).build(password);
64+
}
65+
66+
public AlgorithmIdentifier getDigestAlgorithmIdentifier()
67+
{
68+
return new AlgorithmIdentifier(PKCSObjectIdentifiers.id_PBMAC1, pbmac1Params);
69+
}
70+
};
71+
}
72+
5273
return new PKCS12MacCalculatorBuilder()
5374
{
5475
public MacCalculator build(final char[] password)

0 commit comments

Comments
 (0)