org.spongycastle.bcpg.SignatureSubpacketInputStream Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scprov-jdk15 Show documentation
Show all versions of scprov-jdk15 Show documentation
Spongy Castle is a package-rename (org.bouncycastle.* to org.spongycastle.*) of Bouncy Castle intended for Android.
Android ships with a stripped-down version of Bouncy Castle - this causes classloader collisions if you try to add
an alternative (updated/complete) Bouncy Castle jar.
This jar contains JCE provider and lightweight API for the Bouncy Castle Cryptography APIs for JDK 1.5.
package org.spongycastle.bcpg;
import org.spongycastle.bcpg.sig.Exportable;
import org.spongycastle.bcpg.sig.IssuerKeyID;
import org.spongycastle.bcpg.sig.KeyExpirationTime;
import org.spongycastle.bcpg.sig.KeyFlags;
import org.spongycastle.bcpg.sig.NotationData;
import org.spongycastle.bcpg.sig.PreferredAlgorithms;
import org.spongycastle.bcpg.sig.PrimaryUserID;
import org.spongycastle.bcpg.sig.Revocable;
import org.spongycastle.bcpg.sig.SignatureCreationTime;
import org.spongycastle.bcpg.sig.SignatureExpirationTime;
import org.spongycastle.bcpg.sig.SignerUserID;
import org.spongycastle.bcpg.sig.TrustSignature;
import org.spongycastle.util.io.Streams;
import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
/**
* reader for signature sub-packets
*/
public class SignatureSubpacketInputStream
extends InputStream implements SignatureSubpacketTags
{
InputStream in;
public SignatureSubpacketInputStream(
InputStream in)
{
this.in = in;
}
public int available()
throws IOException
{
return in.available();
}
public int read()
throws IOException
{
return in.read();
}
public SignatureSubpacket readPacket()
throws IOException
{
int l = this.read();
int bodyLen = 0;
if (l < 0)
{
return null;
}
if (l < 192)
{
bodyLen = l;
}
else if (l <= 223)
{
bodyLen = ((l - 192) << 8) + (in.read()) + 192;
}
else if (l == 255)
{
bodyLen = (in.read() << 24) | (in.read() << 16) | (in.read() << 8) | in.read();
}
else
{
// TODO Error?
}
int tag = in.read();
if (tag < 0)
{
throw new EOFException("unexpected EOF reading signature sub packet");
}
byte[] data = new byte[bodyLen - 1];
if (Streams.readFully(in, data) < data.length)
{
throw new EOFException();
}
boolean isCritical = ((tag & 0x80) != 0);
int type = tag & 0x7f;
switch (type)
{
case CREATION_TIME:
return new SignatureCreationTime(isCritical, data);
case KEY_EXPIRE_TIME:
return new KeyExpirationTime(isCritical, data);
case EXPIRE_TIME:
return new SignatureExpirationTime(isCritical, data);
case REVOCABLE:
return new Revocable(isCritical, data);
case EXPORTABLE:
return new Exportable(isCritical, data);
case ISSUER_KEY_ID:
return new IssuerKeyID(isCritical, data);
case TRUST_SIG:
return new TrustSignature(isCritical, data);
case PREFERRED_COMP_ALGS:
case PREFERRED_HASH_ALGS:
case PREFERRED_SYM_ALGS:
return new PreferredAlgorithms(type, isCritical, data);
case KEY_FLAGS:
return new KeyFlags(isCritical, data);
case PRIMARY_USER_ID:
return new PrimaryUserID(isCritical, data);
case SIGNER_USER_ID:
return new SignerUserID(isCritical, data);
case NOTATION_DATA:
return new NotationData(isCritical, data);
}
return new SignatureSubpacket(type, isCritical, data);
}
}