Skip to content

Commit f721005

Browse files
committed
deprecated JcaJceUtils
minor bugfixes in string representations. modified RFC3211Wrap to use nextBytes on random
1 parent 1d966cc commit f721005

File tree

11 files changed

+180
-19
lines changed

11 files changed

+180
-19
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.bouncycastle.asn1.iso;
2+
3+
import org.bouncycastle.asn1.ASN1ObjectIdentifier;
4+
5+
/**
6+
* OIDS from ISO/IEC 10118-3:2004
7+
*/
8+
public interface ISOIECObjectIdentifiers
9+
{
10+
ASN1ObjectIdentifier iso_encryption_algorithms = new ASN1ObjectIdentifier("1.0.10118");
11+
12+
ASN1ObjectIdentifier hash_algorithms = iso_encryption_algorithms.branch("3.0");
13+
14+
ASN1ObjectIdentifier ripemd160 = hash_algorithms.branch("49");
15+
ASN1ObjectIdentifier ripemd128 = hash_algorithms.branch("50");
16+
ASN1ObjectIdentifier whirlpool = hash_algorithms.branch("55");
17+
}

core/src/main/java/org/bouncycastle/asn1/misc/MiscObjectIdentifiers.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,10 @@ public interface MiscObjectIdentifiers
6464
/** NortelNetworks Entrust VersionExtension OID: 1.2.840.113533.7.65.0 */
6565
static final ASN1ObjectIdentifier entrustVersionExtension = entrust.branch("65.0");
6666

67-
//
67+
/** cast5CBC OBJECT IDENTIFIER ::= {iso(1) member-body(2) us(840) nt(113533) nsn(7) algorithms(66) 10} SEE RFC 2984 */
68+
ASN1ObjectIdentifier cast5CBC = entrust.branch("66.10");
69+
70+
//
6871
// Ascom
6972
//
7073
ASN1ObjectIdentifier as_sys_sec_alg_ideaCBC = new ASN1ObjectIdentifier("1.3.6.1.4.1.188.7.1.1.2");

core/src/main/java/org/bouncycastle/crypto/engines/RFC3211WrapEngine.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.bouncycastle.crypto.engines;
22

3+
import java.security.SecureRandom;
4+
35
import org.bouncycastle.crypto.BlockCipher;
46
import org.bouncycastle.crypto.CipherParameters;
57
import org.bouncycastle.crypto.InvalidCipherTextException;
@@ -8,8 +10,6 @@
810
import org.bouncycastle.crypto.params.ParametersWithIV;
911
import org.bouncycastle.crypto.params.ParametersWithRandom;
1012

11-
import java.security.SecureRandom;
12-
1313
/**
1414
* an implementation of the RFC 3211 Key Wrap
1515
* Specification.
@@ -87,10 +87,10 @@ public byte[] wrap(
8787

8888
System.arraycopy(in, inOff, cekBlock, 4, inLen);
8989

90-
for (int i = inLen + 4; i < cekBlock.length; i++)
91-
{
92-
cekBlock[i] = (byte)rand.nextInt();
93-
}
90+
byte[] pad = new byte[cekBlock.length - (inLen + 4)];
91+
92+
rand.nextBytes(pad);
93+
System.arraycopy(pad, 0, cekBlock, inLen + 4, pad.length);
9494

9595
for (int i = 0; i < cekBlock.length; i += blockSize)
9696
{

core/src/main/java/org/bouncycastle/crypto/modes/CCMBlockCipher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ else if (params instanceof ParametersWithIV)
8787
}
8888
else
8989
{
90-
throw new IllegalArgumentException("invalid parameters passed to CCM");
90+
throw new IllegalArgumentException("invalid parameters passed to CCM: " + params.getClass().getName());
9191
}
9292

9393
// NOTE: Very basic support for key re-use, but no performance gain from it

core/src/main/java/org/bouncycastle/crypto/modes/GCFBBlockCipher.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public void init(boolean forEncryption, CipherParameters params)
6565
public String getAlgorithmName()
6666
{
6767
String name = cfbEngine.getAlgorithmName();
68-
return name.substring(0, name.indexOf('/') - 1) + "/G" + name.substring(name.indexOf('/') + 1);
68+
return name.substring(0, name.indexOf('/')) + "/G" + name.substring(name.indexOf('/') + 1);
6969
}
7070

7171
public int getBlockSize()

pkix/src/main/java/org/bouncycastle/cert/crmf/jcajce/CRMFHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
4242
import org.bouncycastle.cert.crmf.CRMFException;
4343
import org.bouncycastle.cms.CMSAlgorithm;
44+
import org.bouncycastle.jcajce.util.AlgorithmParametersUtils;
4445
import org.bouncycastle.jcajce.util.JcaJceHelper;
45-
import org.bouncycastle.jcajce.util.JcaJceUtils;
4646

4747
class CRMFHelper
4848
{
@@ -183,7 +183,7 @@ public Object doInJCE()
183183

184184
try
185185
{
186-
JcaJceUtils.loadParameters(params, sParams);
186+
AlgorithmParametersUtils.loadParameters(params, sParams);
187187
}
188188
catch (IOException e)
189189
{
@@ -392,7 +392,7 @@ AlgorithmIdentifier getAlgorithmIdentifier(ASN1ObjectIdentifier encryptionOID, A
392392
{
393393
try
394394
{
395-
asn1Params = JcaJceUtils.extractParameters(params);
395+
asn1Params = AlgorithmParametersUtils.extractParameters(params);
396396
}
397397
catch (IOException e)
398398
{

pkix/src/main/java/org/bouncycastle/cms/jcajce/CMSUtils.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
import org.bouncycastle.asn1.x509.Extension;
1818
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
1919
import org.bouncycastle.cms.CMSException;
20-
import org.bouncycastle.jcajce.util.JcaJceUtils;
20+
import org.bouncycastle.jcajce.util.AlgorithmParametersUtils;
2121

2222
class CMSUtils
2323
{
@@ -89,7 +89,7 @@ static ASN1Encodable extractParameters(AlgorithmParameters params)
8989
{
9090
try
9191
{
92-
return JcaJceUtils.extractParameters(params);
92+
return AlgorithmParametersUtils.extractParameters(params);
9393
}
9494
catch (IOException e)
9595
{
@@ -102,7 +102,7 @@ static void loadParameters(AlgorithmParameters params, ASN1Encodable sParams)
102102
{
103103
try
104104
{
105-
JcaJceUtils.loadParameters(params, sParams);
105+
AlgorithmParametersUtils.loadParameters(params, sParams);
106106
}
107107
catch (IOException e)
108108
{

pkix/src/main/java/org/bouncycastle/operator/jcajce/OperatorHelper.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@
3838
import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
3939
import org.bouncycastle.asn1.x9.X9ObjectIdentifiers;
4040
import org.bouncycastle.cert.X509CertificateHolder;
41+
import org.bouncycastle.jcajce.util.AlgorithmParametersUtils;
4142
import org.bouncycastle.jcajce.util.JcaJceHelper;
42-
import org.bouncycastle.jcajce.util.JcaJceUtils;
43+
import org.bouncycastle.jcajce.util.MessageDigestUtils;
4344
import org.bouncycastle.operator.OperatorCreationException;
4445

4546
class OperatorHelper
@@ -242,7 +243,7 @@ MessageDigest createDigest(AlgorithmIdentifier digAlgId)
242243

243244
try
244245
{
245-
dig = helper.createDigest(JcaJceUtils.getDigestAlgName(digAlgId.getAlgorithm()));
246+
dig = helper.createDigest(MessageDigestUtils.getDigestName(digAlgId.getAlgorithm()));
246247
}
247248
catch (NoSuchAlgorithmException e)
248249
{
@@ -312,7 +313,7 @@ public Signature createRawSignature(AlgorithmIdentifier algorithm)
312313
{
313314
AlgorithmParameters params = helper.createAlgorithmParameters(algName);
314315

315-
JcaJceUtils.loadParameters(params, algorithm.getParameters());
316+
AlgorithmParametersUtils.loadParameters(params, algorithm.getParameters());
316317

317318
PSSParameterSpec spec = (PSSParameterSpec)params.getParameterSpec(PSSParameterSpec.class);
318319
sig.setParameter(spec);
@@ -336,7 +337,7 @@ private static String getSignatureName(
336337
if (sigAlgId.getAlgorithm().equals(PKCSObjectIdentifiers.id_RSASSA_PSS))
337338
{
338339
RSASSAPSSparams rsaParams = RSASSAPSSparams.getInstance(params);
339-
return JcaJceUtils.getDigestAlgName(rsaParams.getHashAlgorithm().getAlgorithm()) + "WITHRSAANDMGF1";
340+
return getDigestName(rsaParams.getHashAlgorithm().getAlgorithm()) + "WITHRSAANDMGF1";
340341
}
341342
}
342343

@@ -348,6 +349,20 @@ private static String getSignatureName(
348349
return sigAlgId.getAlgorithm().getId();
349350
}
350351

352+
// we need to remove the - to create a correct signature name
353+
private static String getDigestName(ASN1ObjectIdentifier oid)
354+
{
355+
String name = MessageDigestUtils.getDigestName(oid);
356+
357+
int dIndex = name.indexOf('-');
358+
if (dIndex > 0)
359+
{
360+
return name.substring(0, dIndex) + name.substring(dIndex + 1);
361+
}
362+
363+
return MessageDigestUtils.getDigestName(oid);
364+
}
365+
351366
public X509Certificate convertCertificate(X509CertificateHolder certHolder)
352367
throws CertificateException
353368
{
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/***************************************************************/
2+
/****** DO NOT EDIT THIS CLASS bc-java SOURCE FILE ******/
3+
/***************************************************************/
4+
package org.bouncycastle.jcajce.util;
5+
6+
import java.io.IOException;
7+
import java.security.AlgorithmParameters;
8+
9+
import org.bouncycastle.asn1.ASN1Encodable;
10+
import org.bouncycastle.asn1.ASN1Primitive;
11+
12+
/**
13+
* General JCA/JCE utility methods.
14+
*/
15+
public class AlgorithmParametersUtils
16+
{
17+
18+
19+
private AlgorithmParametersUtils()
20+
{
21+
22+
}
23+
24+
/**
25+
* Extract an ASN.1 encodable from an AlgorithmParameters object.
26+
*
27+
* @param params the object to get the encoding used to create the return value.
28+
* @return an ASN.1 object representing the primitives making up the params parameter.
29+
* @throws IOException if an encoding cannot be extracted.
30+
*/
31+
public static ASN1Encodable extractParameters(AlgorithmParameters params)
32+
throws IOException
33+
{
34+
// we try ASN.1 explicitly first just in case and then role back to the default.
35+
ASN1Encodable asn1Params;
36+
try
37+
{
38+
asn1Params = ASN1Primitive.fromByteArray(params.getEncoded("ASN.1"));
39+
}
40+
catch (Exception ex)
41+
{
42+
asn1Params = ASN1Primitive.fromByteArray(params.getEncoded());
43+
}
44+
45+
return asn1Params;
46+
}
47+
48+
/**
49+
* Load an AlgorithmParameters object with the passed in ASN.1 encodable - if possible.
50+
*
51+
* @param params the AlgorithmParameters object to be initialised.
52+
* @param sParams the ASN.1 encodable to initialise params with.
53+
* @throws IOException if the parameters cannot be initialised.
54+
*/
55+
public static void loadParameters(AlgorithmParameters params, ASN1Encodable sParams)
56+
throws IOException
57+
{
58+
// we try ASN.1 explicitly first just in case and then role back to the default.
59+
try
60+
{
61+
params.init(sParams.toASN1Primitive().getEncoded(), "ASN.1");
62+
}
63+
catch (Exception ex)
64+
{
65+
params.init(sParams.toASN1Primitive().getEncoded());
66+
}
67+
}
68+
}

prov/src/main/java/org/bouncycastle/jcajce/util/JcaJceUtils.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ private JcaJceUtils()
2828
* @param params the object to get the encoding used to create the return value.
2929
* @return an ASN.1 object representing the primitives making up the params parameter.
3030
* @throws IOException if an encoding cannot be extracted.
31+
* @deprecated use AlgorithmParametersUtils.extractParameters(AlgorithmParameters params)
3132
*/
3233
public static ASN1Encodable extractParameters(AlgorithmParameters params)
3334
throws IOException
@@ -52,6 +53,7 @@ public static ASN1Encodable extractParameters(AlgorithmParameters params)
5253
* @param params the AlgorithmParameters object to be initialised.
5354
* @param sParams the ASN.1 encodable to initialise params with.
5455
* @throws IOException if the parameters cannot be initialised.
56+
* @deprecated use AlgorithmParametersUtils.loadParameters(AlgorithmParameters params, ASN1Encodable sParams)
5557
*/
5658
public static void loadParameters(AlgorithmParameters params, ASN1Encodable sParams)
5759
throws IOException
@@ -72,6 +74,7 @@ public static void loadParameters(AlgorithmParameters params, ASN1Encodable sPar
7274
*
7375
* @param digestAlgOID the OID of the digest algorithm of interest.
7476
* @return a string representing the standard name - the OID as a string if none available.
77+
* @deprecated use MessageDigestUtils,getDigestName()
7578
*/
7679
public static String getDigestAlgName(
7780
ASN1ObjectIdentifier digestAlgOID)

0 commit comments

Comments
 (0)