Skip to content

Commit

Permalink
compatibility updates
Browse files Browse the repository at this point in the history
  • Loading branch information
dghgit committed Feb 28, 2015
1 parent ad9b951 commit 586da5a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 107 deletions.
62 changes: 0 additions & 62 deletions pg/src/main/j2me/org/bouncycastle/openpgp/PGPKeyPair.java

This file was deleted.

115 changes: 82 additions & 33 deletions pg/src/main/j2me/org/bouncycastle/openpgp/PGPObjectFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,49 @@
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.bcpg.PacketTags;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.bouncycastle.util.Iterable;

/**
* General class for reading a PGP object stream.
* <p>
* Note: if this class finds a PGPPublicKey or a PGPSecretKey it
* will create a PGPPublicKeyRing, or a PGPSecretKeyRing for each
* key found. If all you are trying to do is read a key ring file use
* either PGPPublicKeyRingCollection or PGPSecretKeyRingCollection.
* Note: if this class finds a {@link PGPPublicKey} or a {@link PGPSecretKey} it will create a
* {@link PGPPublicKeyRing}, or a {@link PGPSecretKeyRing} for each key found. If all you are trying
* to do is read a key ring file use either {@link PGPPublicKeyRingCollection} or
* {@link PGPSecretKeyRingCollection}.
* </p><p>
* This factory supports reading the following types of objects:
* <ul>
* <li>{@link PacketTags#SIGNATURE} - produces a {@link PGPSignatureList}</li>
* <li>{@link PacketTags#SECRET_KEY} - produces a {@link PGPSecretKeyRing}</li>
* <li>{@link PacketTags#PUBLIC_KEY} - produces a {@link PGPPublicKeyRing}</li>
* <li>{@link PacketTags#PUBLIC_SUBKEY} - produces a {@link PGPPublicKey}</li>
* <li>{@link PacketTags#COMPRESSED_DATA} - produces a {@link PGPCompressedData}</li>
* <li>{@link PacketTags#LITERAL_DATA} - produces a {@link PGPLiteralData}</li>
* <li>{@link PacketTags#PUBLIC_KEY_ENC_SESSION} - produces a {@link PGPEncryptedDataList}</li>
* <li>{@link PacketTags#SYMMETRIC_KEY_ENC_SESSION} - produces a {@link PGPEncryptedDataList}</li>
* <li>{@link PacketTags#ONE_PASS_SIGNATURE} - produces a {@link PGPOnePassSignatureList}</li>
* <li>{@link PacketTags#MARKER} - produces a {@link PGPMarker}</li>
* </ul>
* </p>
*/
public class PGPObjectFactory
implements Iterable
{
private BCPGInputStream in;
private KeyFingerPrintCalculator fingerPrintCalculator;

public PGPObjectFactory(
InputStream in)
{
this(in, new BcKeyFingerprintCalculator());
}

/**
* Create an object factor suitable for reading keys, key rings and key ring collections.
* Create an object factory suitable for reading PGP objects such as keys, key rings and key
* ring collections, or PGP encrypted data.
*
* @param in stream to read from
* @param fingerPrintCalculator calculator to use in key finger print calculations.
* @param in stream to read PGP data from.
* @param fingerPrintCalculator calculator to use in key finger print calculations.
*/
public PGPObjectFactory(
InputStream in,
Expand All @@ -44,17 +56,12 @@ public PGPObjectFactory(
this.fingerPrintCalculator = fingerPrintCalculator;
}

public PGPObjectFactory(
byte[] bytes)
{
this(new ByteArrayInputStream(bytes));
}

/**
* Create an object factor suitable for reading keys, key rings and key ring collections.
* Create an object factory suitable for reading PGP objects such as keys, key rings and key
* ring collections, or PGP encrypted data.
*
* @param bytes stream to read from
* @param fingerPrintCalculator calculator to use in key finger print calculations.
* @param bytes PGP encoded data.
* @param fingerPrintCalculator calculator to use in key finger print calculations.
*/
public PGPObjectFactory(
byte[] bytes,
Expand All @@ -64,10 +71,10 @@ public PGPObjectFactory(
}

/**
* Return the next object in the stream, or null if the end is reached.
*
* @return Object
* @throws IOException on a parse error
* Return the next object in the stream, or <code>null</code> if the end of stream is reached.
*
* @return one of the supported objects - see class docs for details.
* @throws IOException if an error occurs reading from the wrapped stream or parsing data.
*/
public Object nextObject()
throws IOException
Expand All @@ -80,7 +87,7 @@ public Object nextObject()
return null;
case PacketTags.SIGNATURE:
l = new ArrayList();

while (in.nextPacketTag() == PacketTags.SIGNATURE)
{
try
Expand All @@ -92,7 +99,7 @@ public Object nextObject()
throw new IOException("can't create signature object: " + e);
}
}

return new PGPSignatureList((PGPSignature[])l.toArray(new PGPSignature[l.size()]));
case PacketTags.SECRET_KEY:
try
Expand All @@ -115,15 +122,15 @@ public Object nextObject()
throw new IOException("processing error: " + e.getMessage());
}
case PacketTags.COMPRESSED_DATA:
throw new IOException("processing error: " + "compressed data not supported");
throw new IOException("data compression not implemented");
case PacketTags.LITERAL_DATA:
return new PGPLiteralData(in);
case PacketTags.PUBLIC_KEY_ENC_SESSION:
case PacketTags.SYMMETRIC_KEY_ENC_SESSION:
return new PGPEncryptedDataList(in);
case PacketTags.ONE_PASS_SIGNATURE:
l = new ArrayList();

while (in.nextPacketTag() == PacketTags.ONE_PASS_SIGNATURE)
{
try
Expand All @@ -135,7 +142,7 @@ public Object nextObject()
throw new IOException("can't create one pass signature object: " + e);
}
}

return new PGPOnePassSignatureList((PGPOnePassSignature[])l.toArray(new PGPOnePassSignature[l.size()]));
case PacketTags.MARKER:
return new PGPMarker(in);
Expand All @@ -145,7 +152,49 @@ public Object nextObject()
case PacketTags.EXPERIMENTAL_4:
return in.readPacket();
}

throw new IOException("unknown object in stream: " + in.nextPacketTag());
}

/**
* Support method for Iterable where available.
*/
public Iterator iterator()
{
return new Iterator()
{
private Object obj = getObject();

public boolean hasNext()
{
return obj != null;
}

public Object next()
{
Object rv = obj;

obj = getObject();;

return rv;
}

public void remove()
{
throw new RuntimeException("Cannot remove element from factory.");
}

private Object getObject()
{
try
{
return PGPObjectFactory.this.nextObject();
}
catch (IOException e)
{
throw new PGPRuntimeOperationException("Iterator failed to get next object: " + e.getMessage(), e);
}
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.bouncycastle.openpgp.PGPObjectFactory;
import org.bouncycastle.openpgp.PGPPBEEncryptedData;
import org.bouncycastle.openpgp.bc.BcPGPObjectFactory;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.bc.BcPBEDataDecryptorFactory;
import org.bouncycastle.openpgp.operator.bc.BcPBEKeyEncryptionMethodGenerator;
import org.bouncycastle.openpgp.operator.bc.BcPGPDataEncryptorBuilder;
Expand Down Expand Up @@ -105,13 +106,13 @@ private byte[] decryptMessageBuffered(
Date date)
throws Exception
{
PGPObjectFactory pgpF = new PGPObjectFactory(message);
PGPObjectFactory pgpF = new PGPObjectFactory(message, new BcKeyFingerprintCalculator());
PGPEncryptedDataList enc = (PGPEncryptedDataList)pgpF.nextObject();
PGPPBEEncryptedData pbe = (PGPPBEEncryptedData)enc.get(0);

InputStream clear = pbe.getDataStream(new BcPBEDataDecryptorFactory(pass, new BcPGPDigestCalculatorProvider()));

PGPObjectFactory pgpFact = new PGPObjectFactory(clear);;
PGPObjectFactory pgpFact = new PGPObjectFactory(clear, new BcKeyFingerprintCalculator());

PGPLiteralData ld = (PGPLiteralData)pgpFact.nextObject();

Expand Down Expand Up @@ -295,15 +296,15 @@ public void performTest()
//
// sample message
//
PGPObjectFactory pgpFact = new PGPObjectFactory(testPBEAsym);
PGPObjectFactory pgpFact = new PGPObjectFactory(testPBEAsym, new BcKeyFingerprintCalculator());

PGPEncryptedDataList enc = (PGPEncryptedDataList)pgpFact.nextObject();

PGPPBEEncryptedData pbe = (PGPPBEEncryptedData)enc.get(1);

InputStream clear = pbe.getDataStream(new BcPBEDataDecryptorFactory("password".toCharArray(), new BcPGPDigestCalculatorProvider()));

pgpFact = new PGPObjectFactory(clear);
pgpFact = new PGPObjectFactory(clear, new BcKeyFingerprintCalculator());

// Compressed data not supported
// PGPLiteralData ld = (PGPLiteralData)pgpFact.nextObject();
Expand Down
16 changes: 8 additions & 8 deletions pg/src/main/j2me/org/bouncycastle/openpgp/test/BcPGPRSATest.java
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ private void mixedTest(PGPPrivateKey pgpPrivKey, PGPPublicKey pgpPubKey)

byte[] bytes = bOut.toByteArray();

PGPObjectFactory f = new PGPObjectFactory(bytes);
PGPObjectFactory f = new PGPObjectFactory(bytes, new BcKeyFingerprintCalculator());
checkLiteralData((PGPLiteralData)f.nextObject(), text);

ByteArrayOutputStream bcOut = new ByteArrayOutputStream();
Expand All @@ -464,30 +464,30 @@ private void mixedTest(PGPPrivateKey pgpPrivKey, PGPPublicKey pgpPubKey)
//
// asymmetric
//
PGPObjectFactory pgpF = new PGPObjectFactory(encData);
PGPObjectFactory pgpF = new PGPObjectFactory(encData, new BcKeyFingerprintCalculator());

PGPEncryptedDataList encList = (PGPEncryptedDataList)pgpF.nextObject();

PGPPublicKeyEncryptedData encP = (PGPPublicKeyEncryptedData)encList.get(0);

InputStream clear = encP.getDataStream(new BcPublicKeyDataDecryptorFactory(pgpPrivKey));

PGPObjectFactory pgpFact = new PGPObjectFactory(clear);
PGPObjectFactory pgpFact = new PGPObjectFactory(clear, new BcKeyFingerprintCalculator());

checkLiteralData((PGPLiteralData)pgpFact.nextObject(), text);

//
// PBE
//
pgpF = new PGPObjectFactory(encData);
pgpF = new PGPObjectFactory(encData, new BcKeyFingerprintCalculator());

encList = (PGPEncryptedDataList)pgpF.nextObject();

PGPPBEEncryptedData encPbe = (PGPPBEEncryptedData)encList.get(1);

clear = encPbe.getDataStream(new BcPBEDataDecryptorFactory("password".toCharArray(), new BcPGPDigestCalculatorProvider()));

pgpF = new PGPObjectFactory(clear);
pgpF = new PGPObjectFactory(clear, new BcKeyFingerprintCalculator());

checkLiteralData((PGPLiteralData)pgpF.nextObject(), text);
}
Expand Down Expand Up @@ -998,7 +998,7 @@ public void performTest()

cOut.close();

pgpF = new PGPObjectFactory(cbOut.toByteArray());
pgpF = new PGPObjectFactory(cbOut.toByteArray(), new BcKeyFingerprintCalculator());

encList = (PGPEncryptedDataList)pgpF.nextObject();

Expand Down Expand Up @@ -1199,7 +1199,7 @@ public void performTest()
//
// verify generated signature
//
pgpFact = new PGPObjectFactory(bOut.toByteArray());
pgpFact = new PGPObjectFactory(bOut.toByteArray(), new BcKeyFingerprintCalculator());

PGPOnePassSignatureList p1 = (PGPOnePassSignatureList)pgpFact.nextObject();

Expand Down Expand Up @@ -1262,7 +1262,7 @@ public void performTest()
//
// verify generated signature
//
pgpFact = new PGPObjectFactory(bOut.toByteArray());
pgpFact = new PGPObjectFactory(bOut.toByteArray(), new BcKeyFingerprintCalculator());

p1 = (PGPOnePassSignatureList)pgpFact.nextObject();

Expand Down

0 comments on commit 586da5a

Please sign in to comment.