001: package org.bouncycastle.openpgp;
002:
003: import org.bouncycastle.bcpg.SignatureSubpacket;
004: import org.bouncycastle.bcpg.SignatureSubpacketTags;
005: import org.bouncycastle.bcpg.sig.IssuerKeyID;
006: import org.bouncycastle.bcpg.sig.KeyExpirationTime;
007: import org.bouncycastle.bcpg.sig.KeyFlags;
008: import org.bouncycastle.bcpg.sig.NotationData;
009: import org.bouncycastle.bcpg.sig.PreferredAlgorithms;
010: import org.bouncycastle.bcpg.sig.SignatureCreationTime;
011: import org.bouncycastle.bcpg.sig.SignatureExpirationTime;
012: import org.bouncycastle.bcpg.sig.SignerUserID;
013:
014: import java.util.ArrayList;
015: import java.util.Date;
016: import java.util.List;
017:
018: /**
019: * Container for a list of signature subpackets.
020: */
021: public class PGPSignatureSubpacketVector {
022: SignatureSubpacket[] packets;
023:
024: PGPSignatureSubpacketVector(SignatureSubpacket[] packets) {
025: this .packets = packets;
026: }
027:
028: public SignatureSubpacket getSubpacket(int type) {
029: for (int i = 0; i != packets.length; i++) {
030: if (packets[i].getType() == type) {
031: return packets[i];
032: }
033: }
034:
035: return null;
036: }
037:
038: /**
039: * Return all signature subpackets of the passed in type.
040: * @param type subpacket type code
041: * @return an array of zero or more matching subpackets.
042: */
043: public SignatureSubpacket[] getSubpackets(int type) {
044: List list = new ArrayList();
045:
046: for (int i = 0; i != packets.length; i++) {
047: if (packets[i].getType() == type) {
048: list.add(packets[i]);
049: }
050: }
051:
052: return (SignatureSubpacket[]) list
053: .toArray(new SignatureSubpacket[] {});
054: }
055:
056: public NotationData[] getNotationDataOccurences() {
057: SignatureSubpacket[] notations = getSubpackets(SignatureSubpacketTags.NOTATION_DATA);
058: NotationData[] vals = new NotationData[notations.length];
059: for (int i = 0; i < notations.length; i++) {
060: vals[i] = (NotationData) notations[i];
061: }
062:
063: return vals;
064: }
065:
066: public long getIssuerKeyID() {
067: SignatureSubpacket p = this
068: .getSubpacket(SignatureSubpacketTags.ISSUER_KEY_ID);
069:
070: if (p == null) {
071: return 0;
072: }
073:
074: return ((IssuerKeyID) p).getKeyID();
075: }
076:
077: public Date getSignatureCreationTime() {
078: SignatureSubpacket p = this
079: .getSubpacket(SignatureSubpacketTags.CREATION_TIME);
080:
081: if (p == null) {
082: return null;
083: }
084:
085: return ((SignatureCreationTime) p).getTime();
086: }
087:
088: /**
089: * Return the number of seconds a signature is valid for after its creation date. A value of zero means
090: * the signature never expires.
091: *
092: * @return seconds a signature is valid for.
093: */
094: public long getSignatureExpirationTime() {
095: SignatureSubpacket p = this
096: .getSubpacket(SignatureSubpacketTags.EXPIRE_TIME);
097:
098: if (p == null) {
099: return 0;
100: }
101:
102: return ((SignatureExpirationTime) p).getTime();
103: }
104:
105: /**
106: * Return the number of seconds a key is valid for after its creation date. A value of zero means
107: * the key never expires.
108: *
109: * @return seconds a key is valid for.
110: */
111: public long getKeyExpirationTime() {
112: SignatureSubpacket p = this
113: .getSubpacket(SignatureSubpacketTags.KEY_EXPIRE_TIME);
114:
115: if (p == null) {
116: return 0;
117: }
118:
119: return ((KeyExpirationTime) p).getTime();
120: }
121:
122: public int[] getPreferredHashAlgorithms() {
123: SignatureSubpacket p = this
124: .getSubpacket(SignatureSubpacketTags.PREFERRED_HASH_ALGS);
125:
126: if (p == null) {
127: return null;
128: }
129:
130: return ((PreferredAlgorithms) p).getPreferences();
131: }
132:
133: public int[] getPreferredSymmetricAlgorithms() {
134: SignatureSubpacket p = this
135: .getSubpacket(SignatureSubpacketTags.PREFERRED_SYM_ALGS);
136:
137: if (p == null) {
138: return null;
139: }
140:
141: return ((PreferredAlgorithms) p).getPreferences();
142: }
143:
144: public int[] getPreferredCompressionAlgorithms() {
145: SignatureSubpacket p = this
146: .getSubpacket(SignatureSubpacketTags.PREFERRED_COMP_ALGS);
147:
148: if (p == null) {
149: return null;
150: }
151:
152: return ((PreferredAlgorithms) p).getPreferences();
153: }
154:
155: public int getKeyFlags() {
156: SignatureSubpacket p = this
157: .getSubpacket(SignatureSubpacketTags.KEY_FLAGS);
158:
159: if (p == null) {
160: return 0;
161: }
162:
163: return ((KeyFlags) p).getFlags();
164: }
165:
166: public String getSignerUserID() {
167: SignatureSubpacket p = this
168: .getSubpacket(SignatureSubpacketTags.SIGNER_USER_ID);
169:
170: if (p == null) {
171: return null;
172: }
173:
174: return ((SignerUserID) p).getID();
175: }
176:
177: public int[] getCriticalTags() {
178: int count = 0;
179:
180: for (int i = 0; i != packets.length; i++) {
181: if (packets[i].isCritical()) {
182: count++;
183: }
184: }
185:
186: int[] list = new int[count];
187:
188: count = 0;
189:
190: for (int i = 0; i != packets.length; i++) {
191: if (packets[i].isCritical()) {
192: list[count++] = packets[i].getType();
193: }
194: }
195:
196: return list;
197: }
198:
199: /**
200: * Return the number of packets this vector contains.
201: *
202: * @return size of the packet vector.
203: */
204: public int size() {
205: return packets.length;
206: }
207:
208: SignatureSubpacket[] toSubpacketArray() {
209: return packets;
210: }
211: }
|