1 package org.bouncycastle.gpg.keybox;
2 
3 import java.io.IOException;
4 
5 import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
6 import org.bouncycastle.util.Strings;
7 
8 /**
9  * GnuPG keybox blob.
10  * Based on:
11  *
12  * @see <a href="http://git.gnupg.org/cgi-bin/gitweb.cgi?p=gnupg.git;a=blob;f=kbx/keybox-blob.c;hb=HEAD"></a>
13  */
14 
15 public class Blob
16 {
17     protected static final byte[] magicBytes = Strings.toByteArray("KBXf");
18 
19     protected final int base; // position from start of keybox file.
20     protected final long length;
21     protected final BlobType type;
22     protected final int version;
23 
Blob(int base, long length, BlobType type, int version)24     protected Blob(int base, long length, BlobType type, int version)
25     {
26         this.base = base;
27         this.length = length;
28         this.type = type;
29         this.version = version;
30     }
31 
32 
33     /**
34      * Return an instance of a blob from the source.
35      * Will return null if no more blobs exist.
36      *
37      * @param source The source, KeyBoxByteBuffer, ByteBuffer, byte[], InputStream or File.
38      * @return
39      * @throws Exception
40      */
getInstance(Object source, KeyFingerPrintCalculator keyFingerPrintCalculator, BlobVerifier blobVerifier)41     static Blob getInstance(Object source, KeyFingerPrintCalculator keyFingerPrintCalculator, BlobVerifier blobVerifier)
42         throws IOException
43     {
44         if (source == null)
45         {
46             throw new IllegalArgumentException("Cannot take get instance of null");
47         }
48 
49         KeyBoxByteBuffer buffer = KeyBoxByteBuffer.wrap(source);
50 
51         if (!buffer.hasRemaining())
52         {
53             return null;
54         }
55 
56         int base = buffer.position();
57         long len = buffer.u32();
58         BlobType type = BlobType.fromByte(buffer.u8());
59         int version = buffer.u8();
60 
61         switch (type)
62         {
63 
64         case EMPTY_BLOB:
65             break;
66         case FIRST_BLOB:
67             return FirstBlob.parseContent(base, len, type, version, buffer);
68         case X509_BLOB:
69             return CertificateBlob.parseContent(base, len, type, version, buffer, blobVerifier);
70         case OPEN_PGP_BLOB:
71             return PublicKeyRingBlob.parseContent(base, len, type, version, buffer, keyFingerPrintCalculator, blobVerifier);
72         }
73 
74         return null;
75 
76     }
77 
78 
getType()79     public BlobType getType()
80     {
81         return type;
82     }
83 
getVersion()84     public int getVersion()
85     {
86         return version;
87     }
88 
89 
90 }
91