org.bouncycastle.gpg.keybox.Blob Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of bcpg-fips Show documentation
Show all versions of bcpg-fips Show documentation
The Bouncy Castle Java APIs for the OpenPGP Protocol. The APIs are designed primarily to be used in conjunction with the BC FIPS provider. The APIs may also be used with other providers although if being used in a FIPS context it is the responsibility of the user to ensure that any other providers used are FIPS certified and used appropriately.
package org.bouncycastle.gpg.keybox;
import java.io.IOException;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
/**
* GnuPG keybox blob.
* Based on:
*
* @see
*/
public class Blob
{
protected static final byte[] magicBytes = "KBXf".getBytes();
protected final int base; // position from start of keybox file.
protected final long length;
protected final BlobType type;
protected final int version;
protected Blob(int base, long length, BlobType type, int version)
{
this.base = base;
this.length = length;
this.type = type;
this.version = version;
}
/**
* Return an instance of a blob from the source.
* Will return null if no more blobs exist.
*
* @param source The source, KeyBoxByteBuffer, ByteBuffer, byte[], InputStream or File.
* @return
* @throws Exception
*/
static Blob getInstance(Object source, KeyFingerPrintCalculator keyFingerPrintCalculator, BlobVerifier blobVerifier)
throws IOException
{
if (source == null)
{
throw new IllegalArgumentException("Cannot take get instance of null");
}
KeyBoxByteBuffer buffer = KeyBoxByteBuffer.wrap(source);
if (!buffer.hasRemaining())
{
return null;
}
int base = buffer.position();
long len = buffer.u32();
BlobType type = BlobType.fromByte(buffer.u8());
int version = buffer.u8();
switch (type)
{
case EMPTY_BLOB:
break;
case FIRST_BLOB:
return FirstBlob.parseContent(base, len, type, version, buffer);
case X509_BLOB:
return CertificateBlob.parseContent(base, len, type, version, buffer, blobVerifier);
case OPEN_PGP_BLOB:
return PublicKeyRingBlob.parseContent(base, len, type, version, buffer, keyFingerPrintCalculator, blobVerifier);
}
return null;
}
public BlobType getType()
{
return type;
}
public int getVersion()
{
return version;
}
}