KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > gnupg > model > GnuPGKeyID


1 package SnowMailClient.gnupg.model;
2
3 import java.util.*;
4 import java.io.*;
5 import java.text.*;
6 import SnowMailClient.Language.Language;
7
8 /** represent a keyID read from the keystore
9 */

10 public final class GnuPGKeyID implements Comparable JavaDoc
11 {
12   public static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
13
14   public enum TrustType { Unknown(1), Never(2), Marginal(3), Fully(4), Ultimatively(5); // q,n,m,f,u
15
int code;
16       TrustType(int code)
17       {
18         this.code = code;
19       }
20       public int getCode() { return code; }
21   }
22
23   private String JavaDoc type ; // pub, sec
24
private String JavaDoc shortID; // 1024D/DCCA76DF
25
private String JavaDoc keyID;
26
27 /**
28   - Not yet calculated
29   e Trust calculation has failed; probably due to an expired key
30   q ot enough information for calculation
31   n Never trust this key
32   m Marginally trusted
33   f Fully trusted
34   u Ultimately trusted
35
36   e das Vertrauen läßt sich gerade nicht berechnen (Schlüssel ist wahrscheinlich über dem Verfallsdatum)
37   q ich habe wenig Informationen/ich bin mir nicht sicher
38   n ich vertraue dem Schlüssel nie
39   m ich vertraue dem Schlüssel halbwegs
40   f ich vertraue dem Schlüssel voll
41   u ich vertraue dem Schlüssel uneingeschränkt (ultimately)
42 */

43   private String JavaDoc calculatedTrust;
44   private String JavaDoc fingerprint = "no fingerprint found !";
45   private String JavaDoc keyCapabilities;
46   private int algorithm;
47   private int keyLength;
48
49
50
51   private long expires = -1, created = -1;
52
53   private final Vector<UIDRecord> uids = new Vector<UIDRecord>();
54   private final Vector<GnuPGKeyID> subKeys = new Vector<GnuPGKeyID>();
55
56
57   boolean mainKey = true;
58   public GnuPGKeyID(boolean mainKey)
59   {
60     this.mainKey = mainKey;
61   } // empty constructor
62

63   public void addSubKey(GnuPGKeyID sk)
64   {
65     subKeys.addElement(sk);
66   }
67
68   /** this is the complete ID (name + (comment) + <mail>
69   */

70   public String JavaDoc getKeyID() { return keyID; }
71   public String JavaDoc getCalculatedTrust()
72   {
73     return calculatedTrust;
74   }
75
76   public String JavaDoc getCalculatedTrustMessage()
77   {
78     if(calculatedTrust.equalsIgnoreCase("e"))
79     {
80       if(this.hasExpired())
81       {
82         return Language.translate("Trust calculation failed, key has expired");
83       }
84       else
85       {
86         return Language.translate("Trust calculation has failed");
87       }
88     }
89     if(calculatedTrust.equalsIgnoreCase("q")) return Language.translate("Not enough information for calculation");
90     if(calculatedTrust.equalsIgnoreCase("n")) return Language.translate("Never trust this key");
91     if(calculatedTrust.equalsIgnoreCase("m")) return Language.translate("Marginally trusted");
92     if(calculatedTrust.equalsIgnoreCase("f")) return Language.translate("Fully trusted");
93     if(calculatedTrust.equalsIgnoreCase("u")) return Language.translate("Ultimately trusted");
94     // -
95
return Language.translate("Not yet calculated");
96   }
97
98   public int getKeyLength() { return this.keyLength; }
99   public int getAlgorithm() { return this.algorithm; }
100   public String JavaDoc getAlgorithmName()
101   {
102     switch(algorithm)
103     {
104       case 1: return "RSA";
105       case 16: return "Elgamal"; // (encrypt only)";
106
case 17: return "DSA"; // (sign only)";
107
case 20: return "Elgamal"; // (sign and encrypt, don't use!)";
108
default: return Language.translate("unknown algorithm");
109     }
110   }
111   public long getCreationDate() { return created; }
112   public Vector<UIDRecord> getUIDs() { return uids; }
113   public String JavaDoc getNames()
114   {
115     if(uids.size()==0) return "No UID !";
116     UIDRecord r = uids.firstElement();
117     return r.getName();
118   }
119
120   public String JavaDoc getMails()
121   {
122     if(uids.size()==0) return "No UID !";
123     UIDRecord r = uids.firstElement();
124     return r.getMail();
125   }
126
127   public long getExpirationDate() { return expires; }
128   public boolean hasExpired()
129   {
130     if(expires<0) return false; // never
131
if(expires<System.currentTimeMillis()) return true;
132     return false;
133   }
134
135   public String JavaDoc getFingerprint() { return fingerprint; }
136   public String JavaDoc getKeyCapabilities() { return keyCapabilities; }
137
138   public boolean isSecret() { return type.equalsIgnoreCase("sec"); }
139
140
141
142   public String JavaDoc toString()
143   {
144     if(this.isSecret()) return "Secret key "+getMails() ;
145     return "Public key "+getMails() ;
146   }
147
148
149   public int compareTo(Object JavaDoc okey)
150   {
151      GnuPGKeyID key2 = (GnuPGKeyID) okey;
152
153      return toString().compareTo(key2.toString());
154   }
155
156
157   //
158
// Robust parsing from colon print
159
//
160

161   /** 1.4.0 example ssb::1024:16:194A98B13443A525:1106759649::::::::::
162   */

163   public void parseSEC_colon_format(String JavaDoc line) throws Exception JavaDoc
164   {
165     Scanner scanner = new Scanner(line);
166     scanner.useDelimiter(":");
167
168     String JavaDoc r1 = scanner.next();
169     if(!r1.equalsIgnoreCase("sec")) throw new Exception JavaDoc("SEC reader read only sec records!, not "+r1);
170
171     type = "sec";
172     parse_sec_or_pub(scanner);
173   }
174
175   /** 1.4.0 example
176   */

177   public void parsePUB_colon_format(String JavaDoc line) throws Exception JavaDoc
178   {
179     //System.out.println(""+line);
180

181     Scanner scanner = new Scanner(line);
182     scanner.useDelimiter(":");
183
184
185     String JavaDoc r1 = scanner.next();
186     if(!r1.equalsIgnoreCase("pub")) throw new Exception JavaDoc("PUB reader read only pub records!, not "+r1);
187
188     type = "pub";
189     parse_sec_or_pub(scanner);
190   }
191
192   public void parseSUB_colon_format(String JavaDoc line) throws Exception JavaDoc
193   {
194     //System.out.println(""+line);
195

196     Scanner scanner = new Scanner(line);
197     scanner.useDelimiter(":");
198
199     String JavaDoc r1 = scanner.next();
200     if(!r1.equalsIgnoreCase("sub")) throw new Exception JavaDoc("SUB reader read only sub records!, not "+r1);
201
202     type = "sub";
203     parse_sec_or_pub(scanner);
204   }
205
206   public void parseSSB_colon_format(String JavaDoc line) throws Exception JavaDoc
207   {
208     //System.out.println(""+line);
209

210     Scanner scanner = new Scanner(line);
211     scanner.useDelimiter(":");
212
213     String JavaDoc r1 = scanner.next();
214     if(!r1.equalsIgnoreCase("ssb")) throw new Exception JavaDoc("SSB reader read only ssb records!, not "+r1);
215
216     type = "ssb";
217     parse_sec_or_pub(scanner);
218   }
219
220   private void parse_sec_or_pub(Scanner scanner) throws Exception JavaDoc
221   {
222     calculatedTrust = scanner.next().trim();
223
224     int strength = scanner.nextInt();
225     keyLength = strength;
226
227     int algo = scanner.nextInt();
228     algorithm = algo;
229
230     keyID = scanner.next();
231
232     String JavaDoc creationDate = scanner.next();
233     long cdmilisec = Long.parseLong(creationDate)*1000;
234     created = cdmilisec;
235
236     String JavaDoc expirationDate = scanner.next().trim();
237     if(expirationDate.length()>0)
238     {
239       long edmilisec = Long.parseLong(expirationDate)*1000;
240       this.expires = edmilisec;
241
242     }
243
244     skip(scanner, 1, type+"2");
245     String JavaDoc ignore = scanner.next();
246
247     skip(scanner, 2, type+"3");
248     keyCapabilities = scanner.next();
249   }
250
251   /** fingerprint read
252   */

253   public void parseFPR_colon_format(String JavaDoc line) throws Exception JavaDoc
254   {
255     Scanner scanner = new Scanner(line);
256     scanner.useDelimiter(":");
257
258     String JavaDoc r1 = scanner.next();
259     if(!r1.equalsIgnoreCase("fpr")) throw new Exception JavaDoc("FPR reader read only fpr records!, not "+r1);
260
261     skip(scanner, 8, "FPR0");
262     this.fingerprint = scanner.next();
263     skip(scanner, 0, "FPR1");
264   }
265
266   /** parse name and mail
267   */

268   public void parseUID_colon_format(String JavaDoc line) throws Exception JavaDoc
269   {
270     UIDRecord uid = new UIDRecord();
271     uid.parseUID_colon_format(line);
272     this.uids.add(uid);
273
274   }
275
276   private void skip(Scanner scanner, int n, String JavaDoc prefix)
277   {
278      for(int i=0; i<n; i++)
279      {
280         if(!scanner.hasNext())
281         {
282           System.out.println(""+prefix+" has no more entries, stopped skip("+n+") after "+i);
283           return;
284         }
285
286         String JavaDoc ent = scanner.next();
287         if(ent.length()>0)
288         {
289           System.out.println(prefix+" unread rec("+i+")="+ent);
290         }
291      }
292   }
293 } // KeyID
Popular Tags