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 10 public final class GnuPGKeyID implements Comparable 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); int code; 16 TrustType(int code) 17 { 18 this.code = code; 19 } 20 public int getCode() { return code; } 21 } 22 23 private String type ; private String shortID; private String keyID; 26 27 43 private String calculatedTrust; 44 private String fingerprint = "no fingerprint found !"; 45 private String 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 } 63 public void addSubKey(GnuPGKeyID sk) 64 { 65 subKeys.addElement(sk); 66 } 67 68 70 public String getKeyID() { return keyID; } 71 public String getCalculatedTrust() 72 { 73 return calculatedTrust; 74 } 75 76 public String 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 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 getAlgorithmName() 101 { 102 switch(algorithm) 103 { 104 case 1: return "RSA"; 105 case 16: return "Elgamal"; case 17: return "DSA"; case 20: return "Elgamal"; default: return Language.translate("unknown algorithm"); 109 } 110 } 111 public long getCreationDate() { return created; } 112 public Vector<UIDRecord> getUIDs() { return uids; } 113 public String getNames() 114 { 115 if(uids.size()==0) return "No UID !"; 116 UIDRecord r = uids.firstElement(); 117 return r.getName(); 118 } 119 120 public String 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; if(expires<System.currentTimeMillis()) return true; 132 return false; 133 } 134 135 public String getFingerprint() { return fingerprint; } 136 public String getKeyCapabilities() { return keyCapabilities; } 137 138 public boolean isSecret() { return type.equalsIgnoreCase("sec"); } 139 140 141 142 public String toString() 143 { 144 if(this.isSecret()) return "Secret key "+getMails() ; 145 return "Public key "+getMails() ; 146 } 147 148 149 public int compareTo(Object okey) 150 { 151 GnuPGKeyID key2 = (GnuPGKeyID) okey; 152 153 return toString().compareTo(key2.toString()); 154 } 155 156 157 161 163 public void parseSEC_colon_format(String line) throws Exception 164 { 165 Scanner scanner = new Scanner(line); 166 scanner.useDelimiter(":"); 167 168 String r1 = scanner.next(); 169 if(!r1.equalsIgnoreCase("sec")) throw new Exception ("SEC reader read only sec records!, not "+r1); 170 171 type = "sec"; 172 parse_sec_or_pub(scanner); 173 } 174 175 177 public void parsePUB_colon_format(String line) throws Exception 178 { 179 181 Scanner scanner = new Scanner(line); 182 scanner.useDelimiter(":"); 183 184 185 String r1 = scanner.next(); 186 if(!r1.equalsIgnoreCase("pub")) throw new Exception ("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 line) throws Exception 193 { 194 196 Scanner scanner = new Scanner(line); 197 scanner.useDelimiter(":"); 198 199 String r1 = scanner.next(); 200 if(!r1.equalsIgnoreCase("sub")) throw new Exception ("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 line) throws Exception 207 { 208 210 Scanner scanner = new Scanner(line); 211 scanner.useDelimiter(":"); 212 213 String r1 = scanner.next(); 214 if(!r1.equalsIgnoreCase("ssb")) throw new Exception ("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 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 creationDate = scanner.next(); 233 long cdmilisec = Long.parseLong(creationDate)*1000; 234 created = cdmilisec; 235 236 String 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 ignore = scanner.next(); 246 247 skip(scanner, 2, type+"3"); 248 keyCapabilities = scanner.next(); 249 } 250 251 253 public void parseFPR_colon_format(String line) throws Exception 254 { 255 Scanner scanner = new Scanner(line); 256 scanner.useDelimiter(":"); 257 258 String r1 = scanner.next(); 259 if(!r1.equalsIgnoreCase("fpr")) throw new Exception ("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 268 public void parseUID_colon_format(String line) throws Exception 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 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 ent = scanner.next(); 287 if(ent.length()>0) 288 { 289 System.out.println(prefix+" unread rec("+i+")="+ent); 290 } 291 } 292 } 293 } | Popular Tags |