Skip to content

Commit 3533c3c

Browse files
committed
Added TODO[ecgost] notes at the four hand-rolled ECGOST3410 encoders and decoders in the ecgost and ecgost12 provider key classes and at the lightweight PublicKeyFactory, PrivateKeyFactory and ECGOST3410Util they duplicate, recording the divergences found in review - bitLength()-based 256/512 detection where the lightweight side sizes from the curve field, three variants of the PKCS#8 parameter-sequence guard, the 2001 public key's bare-OID parameter leniency, the X9.62 ECPrivateKey fallback form emitted for keys built from an ECPrivateKeySpec that the lightweight PrivateKeyFactory cannot read because its own fallback branch is unreachable, and decoded provider keys losing their GOST parameter sets - together with the proposed consolidation onto the lightweight factories.
1 parent 816cc20 commit 3533c3c

7 files changed

Lines changed: 60 additions & 0 deletions

File tree

‎core/src/main/java/org/bouncycastle/crypto/util/ECGOST3410Util.java‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@
1111
* Shared logic for encoding ECGOST3410 keys, used by {@link SubjectPublicKeyInfoFactory} and
1212
* {@link PrivateKeyInfoFactory} so that the two stay in step.
1313
*/
14+
// TODO[ecgost] The JCE provider does not use this: the four key classes under
15+
// org.bouncycastle.jcajce.provider.asymmetric.ecgost and .ecgost12 each carry their own encoder (taking the
16+
// algorithm OID from the class and 256/512 from a bitLength() heuristic) and their own decoder. The
17+
// consolidation notes on those classes point at delegating to the two factories here, so this class stays
18+
// package-private for now; it need only become public if the provider ends up calling the rule directly.
1419
class ECGOST3410Util
1520
{
1621
/**

‎core/src/main/java/org/bouncycastle/crypto/util/PrivateKeyFactory.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ else if (
362362
algOID.equals(RosstandartObjectIdentifiers.id_tc26_gost_3410_12_256))
363363
{
364364
ASN1Encodable algParameters = algId.getParameters();
365+
// TODO[ecgost] The X962Parameters branch below is unreachable: this getInstance throws on a bare curve
366+
// OID before the size check runs, and any SEQUENCE that reaches the check is now 1..3 elements (the
367+
// parameters class rejects the rest). The provider's BCECGOST3410PrivateKey / BCECGOST3410_2012PrivateKey
368+
// emit and read exactly that bare-OID + ECPrivateKey form when built from an ECPrivateKeySpec, and
369+
// this factory cannot read it. Make the fallback live (parse GOST3410PublicKeyAlgParameters only when
370+
// the parameters are a SEQUENCE) so the provider decode paths can be consolidated onto this factory.
365371
GOST3410PublicKeyAlgParameters gostParams = GOST3410PublicKeyAlgParameters.getInstance(algParameters);
366372
ECGOST3410Parameters ecSpec = null;
367373
BigInteger d = null;

‎core/src/main/java/org/bouncycastle/crypto/util/PublicKeyFactory.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,9 @@ AsymmetricKeyParameter getPublicKeyParameters(SubjectPublicKeyInfo keyInfo, Obje
455455
{
456456
AlgorithmIdentifier algID = keyInfo.getAlgorithm();
457457
// ASN1ObjectIdentifier algOid = algID.getAlgorithm();
458+
// TODO[ecgost] The provider's ecgost.BCECGOST3410PublicKey also accepts the parameters being a bare
459+
// publicKeyParamSet OID (no digestParamSet) here, where this getInstance throws. Accept that form
460+
// too, so the provider decode paths can be consolidated onto this factory.
458461
GOST3410PublicKeyAlgParameters gostParams = GOST3410PublicKeyAlgParameters.getInstance(algID.getParameters());
459462
ASN1ObjectIdentifier publicKeyParamSet = gostParams.getPublicKeyParamSet();
460463

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ecgost/BCECGOST3410PrivateKey.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,11 @@ private void populateFromPrivKeyInfo(PrivateKeyInfo info)
207207
ASN1Encodable pkParams = pkAlg.getParameters();
208208
ASN1Primitive p = pkParams.toASN1Primitive();
209209

210+
// TODO[ecgost] Duplicate of the GOST branch in crypto.util.PrivateKeyFactory, with its own variant of
211+
// the parameter-sequence guard (== 2 || == 3 here, <= 3 in the 2012 class, 1..3 in the lightweight
212+
// factory). Consolidate by delegating to PrivateKeyFactory once its X9.62 fallback branch can read the
213+
// bare-curve-OID form the gostParams == null branch of getEncoded() emits. The prov/src/main/jdk1.4
214+
// overlay of this class has to follow.
210215
if (p instanceof ASN1Sequence && (ASN1Sequence.getInstance(p).size() == 2 || ASN1Sequence.getInstance(p).size() == 3))
211216
{
212217
GOST3410PublicKeyAlgParameters gParams = GOST3410PublicKeyAlgParameters.getInstance(pkParams);
@@ -322,6 +327,11 @@ public String getFormat()
322327
*
323328
* @return a PKCS8 representation of the key.
324329
*/
330+
// TODO[ecgost] See the note on ecgost12.BCECGOST3410_2012PrivateKey.getEncoded(): the 2001 encoder is the
331+
// fourth hand-rolled copy, with the same gostParams == null branch emitting an X9.62 ECPrivateKey body
332+
// with a bare curve OID that the lightweight PrivateKeyFactory cannot read. Consolidate by delegating to
333+
// PrivateKeyInfoFactory and asserting the resulting OID is id-GostR3410-2001. The prov/src/main/jdk1.4
334+
// overlay of this class has to follow.
325335
public byte[] getEncoded()
326336
{
327337
if (destroyed)

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ecgost/BCECGOST3410PublicKey.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,12 @@ public BCECGOST3410PublicKey(
178178
populateFromPubKeyInfo(info);
179179
}
180180

181+
// TODO[ecgost] Duplicate of crypto.util.PublicKeyFactory's GOST3410_2001Converter, except that this copy
182+
// also accepts the AlgorithmIdentifier parameters being a bare OID (see below) where the lightweight
183+
// converter throws. Consolidate by delegating to PublicKeyFactory after moving that leniency into the
184+
// converter, and keep the ECGOST3410Parameters it returns as this key's domain parameters (a decoded key
185+
// today carries plain ECDomainParameters and so loses the GOST parameter sets on the way back to the
186+
// lightweight API). The prov/src/main/jdk1.4 overlay of this class has to follow.
181187
private void populateFromPubKeyInfo(SubjectPublicKeyInfo info)
182188
{
183189
ASN1BitString bits = info.getPublicKeyData();
@@ -242,6 +248,12 @@ public String getFormat()
242248
return "X.509";
243249
}
244250

251+
// TODO[ecgost] See the note on ecgost12.BCECGOST3410_2012PublicKey.getEncoded(): the 2001 encoder is the
252+
// third hand-rolled copy. Consolidate by delegating to SubjectPublicKeyInfoFactory (building an
253+
// ECGOST3410Parameters from getGostParams() and the domain parameters) and asserting the resulting OID is
254+
// id-GostR3410-2001, i.e. that the digestParamSet is a GOST R 34.11-94 parameter set; a bare-OID
255+
// gostParams (accepted by populateFromPubKeyInfo) has no lightweight equivalent and needs deciding.
256+
// The prov/src/main/jdk1.4 overlay of this class has to follow.
245257
public byte[] getEncoded()
246258
{
247259
ASN1Encodable params;

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ecgost12/BCECGOST3410_2012PrivateKey.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,10 @@ private void populateFromPrivKeyInfo(PrivateKeyInfo info)
211211
{
212212
ASN1Primitive p = info.getPrivateKeyAlgorithm().getParameters().toASN1Primitive();
213213

214+
// TODO[ecgost] Duplicate of the GOST branch in crypto.util.PrivateKeyFactory, with a third variant of
215+
// the parameter-sequence guard (here <= 3; the 2001 class has == 2 || == 3; the lightweight factory
216+
// has 1..3). Consolidate by delegating to PrivateKeyFactory once its X9.62 fallback branch (currently
217+
// unreachable, see the note there) can read the bare-curve-OID form this class emits and accepts.
214218
if (p instanceof ASN1Sequence &&
215219
(ASN1Sequence.getInstance(p).size() <= 3))
216220
{
@@ -337,6 +341,14 @@ public String getFormat()
337341
*
338342
* @return a PKCS8 representation of the key.
339343
*/
344+
// TODO[ecgost] See the note on BCECGOST3410_2012PublicKey.getEncoded(): delegate to the lightweight
345+
// PrivateKeyInfoFactory instead of this hand-rolled encoder. Two things to fix on the way: the
346+
// d.bitLength() 512-detection below (a 2^-256 misfire; the lightweight side sizes from the curve field),
347+
// and the gostParams == null branch, which emits an X9.62 ECPrivateKey body with a bare curve OID under
348+
// the GOST algorithm OID. A key built from an ECPrivateKeySpec over a GOST named curve takes that branch,
349+
// so its PKCS#8 is in a different shape from the SPKI of the public key built from the matching
350+
// ECPublicKeySpec (which getGostParams() steers into the RFC 9215 form), and the lightweight
351+
// PrivateKeyFactory cannot read it at all. The provider reads it back itself, so keep accepting it.
340352
public byte[] getEncoded()
341353
{
342354
if (destroyed)

‎prov/src/main/java/org/bouncycastle/jcajce/provider/asymmetric/ecgost12/BCECGOST3410_2012PublicKey.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ public BCECGOST3410_2012PublicKey(
183183
populateFromPubKeyInfo(info);
184184
}
185185

186+
// TODO[ecgost] Duplicate of crypto.util.PublicKeyFactory's GOST3410_2012Converter. Consolidate by
187+
// delegating to PublicKeyFactory and keeping the ECGOST3410Parameters it returns as this key's domain
188+
// parameters: today a decoded key carries plain ECDomainParameters, so ECUtil.generatePublicKeyParameter
189+
// on it loses the GOST parameter sets and the lightweight factory would re-encode it as X9.62 EC.
186190
private void populateFromPubKeyInfo(SubjectPublicKeyInfo info)
187191
{
188192
ASN1ObjectIdentifier algOid = info.getAlgorithm().getAlgorithm();
@@ -243,6 +247,14 @@ public String getFormat()
243247
return "X.509";
244248
}
245249

250+
// TODO[ecgost] This is one of four hand-rolled ECGOST3410 encoders in the provider (this class,
251+
// BCECGOST3410_2012PrivateKey, ecgost.BCECGOST3410PublicKey, ecgost.BCECGOST3410PrivateKey), each with its
252+
// own extractBytes and its own size/OID rules, none of them sharing code with the lightweight
253+
// SubjectPublicKeyInfoFactory / PrivateKeyInfoFactory (crypto.util.ECGOST3410Util holds the shared rule
254+
// there). Consolidate by building an ECGOST3410Parameters from getGostParams() and the key's domain
255+
// parameters and delegating to SubjectPublicKeyInfoFactory, asserting the resulting algorithm OID is a
256+
// 2012 one so the class-determines-algorithm contract is kept. That also replaces the bX.bitLength()
257+
// 512-detection below (a 2^-256 misfire) with the curve field size the lightweight side now uses.
246258
public byte[] getEncoded()
247259
{
248260
ASN1Encodable params;

0 commit comments

Comments
 (0)