Skip to content

Commit

Permalink
minor typos, Java 1.3 fixes from 1.79 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Oct 31, 2024
1 parent c836fbb commit f3b079d
Show file tree
Hide file tree
Showing 16 changed files with 1,473 additions and 136 deletions.
15 changes: 14 additions & 1 deletion ant/jdk13.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<exclude name="**/asymmetric/dstu/*.java" />
<exclude name="**/Logging*.java" />
<exclude name="**/provider/config/PKCS12StoreParameter.java" />
<exclude name="**/COMPOSITE.java"/>
<exclude name="**/gemss/*.java"/>
<exclude name="**/rainbow/*.java"/>
<exclude name="**/Rainbow*.java"/>
Expand Down Expand Up @@ -90,6 +91,7 @@
<exclude name="**/gemss/*.java" />
<exclude name="**/CertPathReviewer*.java" />
<exclude name="**/PKIXCertPathReviewer.java" />
<exclude name="**/COMPOSITE.java"/>
<exclude name="**/PKIXAttrCert*.java" />
<exclude name="**/PKIXNameConstraints*.java" />
<exclude name="**/PKCS12StoreParameter.java" />
Expand Down Expand Up @@ -249,6 +251,7 @@
<exclude name="**/jce/provider/test/CertLocaleTest.java" />
</fileset>
<fileset dir="pkix/src/test/java">
<exclude name="**/CheckNameConstraintsTest.java"/>
<exclude name="**/pkix/test/RevocationTest.java"/>
<exclude name="**/SunProviderTest.java" />
<exclude name="**/NullProviderTest.java" />
Expand Down Expand Up @@ -329,19 +332,29 @@

<replaceregexp match="${regexp}" replace=" " flags="g" byline="true">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
<include name="**/*.java"/>
<exclude name="**/SICBlockCipher.java"/>
</fileset>
</replaceregexp>
<replaceregexp match="(List|Map|Set) >" replace="\1" flags="g" byline="true">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
<exclude name="**/MultipartParserTest.java"/>
<exclude name="**/SICBlockCipher.java"/>
</fileset>
</replaceregexp>
<replaceregexp match="StringBuilder" replace="StringBuffer" flags="g" byline="true">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
<exclude name="**/MultipartParserTest.java"/>
<exclude name="**/SICBlockCipher.java"/>
</fileset>
</replaceregexp>
<replaceregexp match="LinkedHashSet" replace="HashSet" flags="g" byline="true">
<fileset dir="${src.dir}">
<include name="**/*.java"/>
<exclude name="**/MultipartParserTest.java"/>
<exclude name="**/SICBlockCipher.java"/>
</fileset>
</replaceregexp>
<replaceregexp match="\.\.\." replace="[]" flags="g" byline="true">
Expand Down
1 change: 1 addition & 0 deletions ant/jdk15+.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<property name="src.dir" value="${build.dir}/${jdk.name}" />
<property name="target.prefix" value="jdk15to18" />
<property name="javadoc.args" value="-breakiterator" />
<property name="jmail.present" value="true" />
<property name="junit.maxmemory" value="1536m" />

<target name="clean">
Expand Down
2 changes: 1 addition & 1 deletion bc-build.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
release.suffix: 1.79
release.name: 1.79
release.version: 1.79
release.debug: false
release.debug: true

mail.jar.home: ./libs/javax.mail-1.4.7.jar
activation.jar.home: ./libs/activation-1.1.1.jar
Expand Down
109 changes: 109 additions & 0 deletions core/src/main/jdk1.3/org/bouncycastle/util/io/pem/PemReader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package org.bouncycastle.util.io.pem;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.util.ArrayList;
import java.util.List;
//import java.util.logging.Level;
//import java.util.logging.Logger;

import org.bouncycastle.util.encoders.Base64;

/**
* A generic PEM reader, based on the format outlined in RFC 1421
*/
public class PemReader
extends BufferedReader
{
public static final String LAX_PEM_PARSING_SYSTEM_PROPERTY_NAME = "org.bouncycastle.pemreader.lax";

private static final String BEGIN = "-----BEGIN ";
private static final String END = "-----END ";
//private static final Logger LOG = Logger.getLogger(PemReader.class.getName());

public PemReader(Reader reader)
{
super(reader);
}

/**
* Read the next PEM object as a blob of raw data with header information.
*
* @return the next object in the stream, null if no objects left.
* @throws IOException in case of a parse error.
*/
public PemObject readPemObject()
throws IOException
{
String line = readLine();

while (line != null && !line.startsWith(BEGIN))
{
line = readLine();
}

if (line != null)
{
line = line.substring(BEGIN.length()).trim();
int index = line.indexOf('-');

if (index > 0 && line.endsWith("-----") && (line.length() - index) == 5)
{
String type = line.substring(0, index);

return loadObject(type);
}
}

return null;
}

private PemObject loadObject(String type)
throws IOException
{
String line;
String endMarker = END + type + "-----";
StringBuffer buf = new StringBuffer();
List headers = new ArrayList();

while ((line = readLine()) != null)
{
int index = line.indexOf(':');
if (index >= 0)
{
String hdr = line.substring(0, index);
String value = line.substring(index + 1).trim();

headers.add(new PemHeader(hdr, value));

continue;
}

if (System.getProperty(LAX_PEM_PARSING_SYSTEM_PROPERTY_NAME, "false").equalsIgnoreCase("true"))
{
String trimmedLine = line.trim();
//if (!trimmedLine.equals(line) && LOG.isLoggable(Level.WARNING))
//{
//LOG.log(Level.WARNING, "PEM object contains whitespaces on -----END line", new Exception("trace"));
//}
line = trimmedLine;
}

if (line.indexOf(endMarker) == 0)
{
break;
}

buf.append(line.trim());
}

if (line == null)
{
throw new IOException(endMarker + " not found");
}

return new PemObject(type, headers, Base64.decode(buf.toString()));
}

}
188 changes: 188 additions & 0 deletions core/src/test/jdk1.3/org/bouncycastle/crypto/test/CipherTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
package org.bouncycastle.crypto.test;

import java.security.SecureRandom;

import org.bouncycastle.crypto.BlockCipher;
import org.bouncycastle.crypto.CipherKeyGenerator;
import org.bouncycastle.crypto.DataLengthException;
import org.bouncycastle.crypto.InvalidCipherTextException;
import org.bouncycastle.crypto.KeyGenerationParameters;
import org.bouncycastle.crypto.modes.AEADCipher;
import org.bouncycastle.crypto.params.KeyParameter;
import org.bouncycastle.crypto.params.ParametersWithIV;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.test.SimpleTest;

public abstract class CipherTest
extends SimpleTest
{
private SimpleTest[] _tests;
private BlockCipher _engine;
private KeyParameter _validKey;

// protected CipherTest(
// SimpleTest[] tests)
// {
// _tests = tests;
// }

protected CipherTest(
SimpleTest[] tests,
BlockCipher engine,
KeyParameter validKey)
{
_tests = tests;
_engine = engine;
_validKey = validKey;
}

public abstract String getName();

public void performTest()
throws Exception
{
for (int i = 0; i != _tests.length; i++)
{
_tests[i].performTest();
}

if (_engine != null)
{
//
// state tests
//
byte[] buf = new byte[128];

try
{
_engine.processBlock(buf, 0, buf, 0);

fail("failed initialisation check");
}
catch (IllegalStateException e)
{
// expected
}

bufferSizeCheck((_engine));
}
}

private void bufferSizeCheck(
BlockCipher engine)
{
byte[] correctBuf = new byte[engine.getBlockSize()];
byte[] shortBuf = new byte[correctBuf.length / 2];

engine.init(true, _validKey);

try
{
engine.processBlock(shortBuf, 0, correctBuf, 0);

fail("failed short input check");
}
catch (DataLengthException e)
{
// expected
}

try
{
engine.processBlock(correctBuf, 0, shortBuf, 0);

fail("failed short output check");
}
catch (DataLengthException e)
{
// expected
}

engine.init(false, _validKey);

try
{
engine.processBlock(shortBuf, 0, correctBuf, 0);

fail("failed short input check");
}
catch (DataLengthException e)
{
// expected
}

try
{
engine.processBlock(correctBuf, 0, shortBuf, 0);

fail("failed short output check");
}
catch (DataLengthException e)
{
// expected
}
}

interface Instace
{
AEADCipher CreateInstace();
}

static void checkCipher(int aeadLen, int ivLen, int msgLen, Instace instace)
{
AEADCipher pCipher = instace.CreateInstace();

try
{
/* Obtain some random data */
final byte[] myData = new byte[msgLen];
final SecureRandom myRandom = new SecureRandom();
myRandom.nextBytes(myData);

/* Obtain some random AEAD */
final byte[] myAEAD = new byte[aeadLen];
myRandom.nextBytes(myAEAD);

/* Create the Key parameters */
final CipherKeyGenerator myGenerator = new CipherKeyGenerator();
final KeyGenerationParameters myGenParams = new KeyGenerationParameters(myRandom, 128);
myGenerator.init(myGenParams);
final byte[] myKey = myGenerator.generateKey();
final KeyParameter myKeyParams = new KeyParameter(myKey);

/* Create the nonce */
final byte[] myNonce = new byte[ivLen];
myRandom.nextBytes(myNonce);
final ParametersWithIV myParams = new ParametersWithIV(myKeyParams, myNonce);

/* Initialise the cipher for encryption */
pCipher.init(true, myParams);
final int myMaxOutLen = pCipher.getOutputSize(msgLen);
final byte[] myEncrypted = new byte[myMaxOutLen];
pCipher.processAADBytes(myAEAD, 0, aeadLen);
int myOutLen = pCipher.processBytes(myData, 0, msgLen, myEncrypted, 0);
myOutLen += pCipher.doFinal(myEncrypted, myOutLen);

/* Note that myOutLen is too large by DATALEN */
pCipher = instace.CreateInstace();
/* Initialise the cipher for decryption */
pCipher.init(false, myParams);
final int myMaxClearLen = pCipher.getOutputSize(myOutLen);
final byte[] myDecrypted = new byte[myMaxClearLen];
pCipher.processAADBytes(myAEAD, 0, aeadLen);
int myClearLen = pCipher.processBytes(myEncrypted, 0, myEncrypted.length, myDecrypted, 0);
myClearLen += pCipher.doFinal(myDecrypted, myClearLen);
final byte[] myResult = Arrays.copyOf(myDecrypted, msgLen);

/* Check that we have the same result */
if (!Arrays.areEqual(myData, myResult))
{
System.out.println("Cipher " + pCipher.getAlgorithmName() + " failed");
}
}
catch (InvalidCipherTextException e)
{
throw new RuntimeException(e.toString());
}
}
}
4 changes: 2 additions & 2 deletions docs/releasenotes.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ <h3>2.1.2 Defects Fixed</h3>
<li>ERSInputStreamData would fail to generate the correct hash if called a second time with a different hash algorithm. This has been fixed.</li>
<li>A downcast in the CrlCache which would cause FTP based CRLs to fail to load has been removed.</li>
<li>ECUtil.getNamedCurveOid() now trims curve names of excess space before look up.</li>
<li>The PhotonBeetle and Xoodyak did not reset properly after a doFinal() call. This has been fixed.</li>
<li>The PhotonBeetle and Xoodyak digests did not reset properly after a doFinal() call. This has been fixed.</li>
<li>Malformed AlgorithmIdentifiers in CertIDs could cause caching issues in the OCSP cache. This has been fixed.</li>
<li>With Java 21 a provider service class will now be returned with a null class name where previously a null would have been returned for a service. This can cause a NullPointerException to be thrown by the BC provider if a non-existant service is requested. This issue has now been worked around.</li>
<li>CMS: OtherKeyAttribute.keyAttr now treated as optional.</li>
Expand All @@ -49,7 +49,7 @@ <h3>2.1.3 Additional Features and Functionality</h3>
<li>Delta Certificates now support the latest revision of the delta certificate extension draft.</li>
<li>A general KeyIdentifier class, encapsulating both PGP KeyID and the PGP key fingerprint has been added to the PGP API.</li>
<li>Support for the LibrePGP PreferredEncryptionModes signature subpacket has been added to the PGP API.</li>
<li>Support Version 6 signatures, including salts, has been added to the PGP API.</li>
<li>Support for Version 6 signatures, including salts, has been added to the PGP API.</li>
<li>Support for the PreferredKeyServer signature supacket has been added to the PGP API.</li>
<li>Support for RFC 9269, "Using KEMs in Cryptographic Message Syntax (CMS)", has been added to the CMS API.</li>
<li>Support for the Argon2 S2K has been added to the PGP API.</li>
Expand Down
Loading

0 comments on commit f3b079d

Please sign in to comment.