01: package org.bouncycastle.openpgp;
02:
03: import org.bouncycastle.bcpg.BCPGInputStream;
04: import org.bouncycastle.bcpg.Packet;
05: import org.bouncycastle.bcpg.PacketTags;
06: import org.bouncycastle.bcpg.SignaturePacket;
07: import org.bouncycastle.bcpg.TrustPacket;
08: import org.bouncycastle.bcpg.UserAttributePacket;
09: import org.bouncycastle.bcpg.UserIDPacket;
10:
11: import java.io.IOException;
12: import java.io.InputStream;
13: import java.util.ArrayList;
14: import java.util.List;
15:
16: public abstract class PGPKeyRing {
17: PGPKeyRing() {
18: }
19:
20: static BCPGInputStream wrap(InputStream in) {
21: if (in instanceof BCPGInputStream) {
22: return (BCPGInputStream) in;
23: }
24:
25: return new BCPGInputStream(in);
26: }
27:
28: static TrustPacket readOptionalTrustPacket(BCPGInputStream pIn)
29: throws IOException {
30: return (pIn.nextPacketTag() == PacketTags.TRUST) ? (TrustPacket) pIn
31: .readPacket()
32: : null;
33: }
34:
35: static List readSignaturesAndTrust(BCPGInputStream pIn)
36: throws IOException {
37: try {
38: List sigList = new ArrayList();
39:
40: while (pIn.nextPacketTag() == PacketTags.SIGNATURE) {
41: SignaturePacket signaturePacket = (SignaturePacket) pIn
42: .readPacket();
43: TrustPacket trustPacket = readOptionalTrustPacket(pIn);
44:
45: sigList.add(new PGPSignature(signaturePacket,
46: trustPacket));
47: }
48:
49: return sigList;
50: } catch (PGPException e) {
51: throw new IOException("can't create signature object: "
52: + e.getMessage() + ", cause: "
53: + e.getUnderlyingException().toString());
54: }
55: }
56:
57: static void readUserIDs(BCPGInputStream pIn, List ids,
58: List idTrusts, List idSigs) throws IOException {
59: while (pIn.nextPacketTag() == PacketTags.USER_ID
60: || pIn.nextPacketTag() == PacketTags.USER_ATTRIBUTE) {
61: Packet obj = pIn.readPacket();
62: if (obj instanceof UserIDPacket) {
63: UserIDPacket id = (UserIDPacket) obj;
64: ids.add(id.getID());
65: } else {
66: UserAttributePacket user = (UserAttributePacket) obj;
67: ids.add(new PGPUserAttributeSubpacketVector(user
68: .getSubpackets()));
69: }
70:
71: idTrusts.add(readOptionalTrustPacket(pIn));
72: idSigs.add(readSignaturesAndTrust(pIn));
73: }
74: }
75: }
|