~brian-thomason/+junk/bouncycastle

« back to all changes in this revision

Viewing changes to src/org/bouncycastle/openpgp/PGPObjectFactory.java

  • Committer: Brian Thomason
  • Date: 2011-12-20 17:20:32 UTC
  • Revision ID: brian.thomason@canonical.com-20111220172032-rdtm13jgdxtksacr
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.bouncycastle.openpgp;
 
2
 
 
3
import java.io.ByteArrayInputStream;
 
4
import java.io.IOException;
 
5
import java.io.InputStream;
 
6
import java.util.ArrayList;
 
7
import java.util.List;
 
8
 
 
9
import org.bouncycastle.bcpg.BCPGInputStream;
 
10
import org.bouncycastle.bcpg.PacketTags;
 
11
 
 
12
/**
 
13
 * General class for reading a PGP object stream.
 
14
 * <p>
 
15
 * Note: if this class finds a PGPPublicKey or a PGPSecretKey it
 
16
 * will create a PGPPublicKeyRing, or a PGPSecretKeyRing for each
 
17
 * key found. If all you are trying to do is read a key ring file use
 
18
 * either PGPPublicKeyRingCollection or PGPSecretKeyRingCollection.
 
19
 */
 
20
public class PGPObjectFactory
 
21
{
 
22
    BCPGInputStream in;
 
23
    
 
24
    public PGPObjectFactory(
 
25
        InputStream in)
 
26
    {
 
27
        this.in = new BCPGInputStream(in);
 
28
    }
 
29
    
 
30
    public PGPObjectFactory(
 
31
        byte[] bytes)
 
32
    {
 
33
        this(new ByteArrayInputStream(bytes));
 
34
    }
 
35
    
 
36
    /**
 
37
     * Return the next object in the stream, or null if the end is reached.
 
38
     * 
 
39
     * @return Object
 
40
     * @throws IOException on a parse error
 
41
     */
 
42
    public Object nextObject()
 
43
        throws IOException
 
44
    {
 
45
        List l;
 
46
 
 
47
        switch (in.nextPacketTag())
 
48
        {
 
49
        case -1:
 
50
            return null;
 
51
        case PacketTags.SIGNATURE:
 
52
            l = new ArrayList();
 
53
            
 
54
            while (in.nextPacketTag() == PacketTags.SIGNATURE)
 
55
            {
 
56
                try
 
57
                {
 
58
                    l.add(new PGPSignature(in));
 
59
                }
 
60
                catch (PGPException e)
 
61
                {
 
62
                    throw new IOException("can't create signature object: " + e);
 
63
                }
 
64
            }
 
65
            
 
66
            return new PGPSignatureList((PGPSignature[])l.toArray(new PGPSignature[l.size()]));
 
67
        case PacketTags.SECRET_KEY:
 
68
            try
 
69
            {
 
70
                return new PGPSecretKeyRing(in);
 
71
            }
 
72
            catch (PGPException e)
 
73
            {
 
74
                throw new IOException("can't create secret key object: " + e);
 
75
            }
 
76
        case PacketTags.PUBLIC_KEY:
 
77
            return new PGPPublicKeyRing(in);
 
78
        case PacketTags.PUBLIC_SUBKEY:
 
79
            return PGPPublicKeyRing.readSubkey(in);
 
80
        case PacketTags.COMPRESSED_DATA:
 
81
            return new PGPCompressedData(in);
 
82
        case PacketTags.LITERAL_DATA:
 
83
            return new PGPLiteralData(in);
 
84
        case PacketTags.PUBLIC_KEY_ENC_SESSION:
 
85
        case PacketTags.SYMMETRIC_KEY_ENC_SESSION:
 
86
            return new PGPEncryptedDataList(in);
 
87
        case PacketTags.ONE_PASS_SIGNATURE:
 
88
            l = new ArrayList();
 
89
            
 
90
            while (in.nextPacketTag() == PacketTags.ONE_PASS_SIGNATURE)
 
91
            {
 
92
                try
 
93
                {
 
94
                    l.add(new PGPOnePassSignature(in));
 
95
                }
 
96
                catch (PGPException e)
 
97
                {
 
98
                    throw new IOException("can't create one pass signature object: " + e);
 
99
                }
 
100
            }
 
101
            
 
102
            return new PGPOnePassSignatureList((PGPOnePassSignature[])l.toArray(new PGPOnePassSignature[l.size()]));
 
103
        case PacketTags.MARKER:
 
104
            return new PGPMarker(in);
 
105
        case PacketTags.EXPERIMENTAL_1:
 
106
        case PacketTags.EXPERIMENTAL_2:
 
107
        case PacketTags.EXPERIMENTAL_3:
 
108
        case PacketTags.EXPERIMENTAL_4:
 
109
            return in.readPacket();
 
110
        }
 
111
        
 
112
        throw new IOException("unknown object in stream: " + in.nextPacketTag());
 
113
    }
 
114
}