1 package SnowMailClient.gnupg; 2 3 4 import javax.swing.*; 5 import javax.swing.border.*; 6 import java.awt.*; 7 import java.awt.event.*; 8 import java.util.*; 9 import java.io.*; 10 11 import snow.utils.gui.*; 12 import snow.concurrent.*; 13 import SnowMailClient.Language.Language; 14 import snow.utils.storage.*; 15 import SnowMailClient.utils.*; 16 import SnowMailClient.gnupg.Main.GnuPGCommands; 17 import SnowMailClient.gnupg.model.*; 18 import SnowMailClient.gnupg.Views.GPGPasswordDialog; 19 import SnowMailClient.SnowMailClientApp; 20 21 22 32 public final class GnuPGLink 33 { 34 36 private String pathToGPG = "c:/app/gnupg/gpg.exe"; 37 private String gpgVersion = ""; 38 39 TreeSet<GnuPGKeyID> publicKeyIDsModel = new TreeSet<GnuPGKeyID>(); TreeSet<GnuPGKeyID> secretKeyIDsModel = new TreeSet<GnuPGKeyID>(); 43 final private Map<String , byte[]> keyPasswords = new Hashtable<String , byte[]>(); 45 46 private boolean isGPG_available = false; 47 48 49 public GnuPGLink() 50 { 51 } 53 54 public Vector<Object > getVectorRepresentation() 55 { 56 Vector<Object > rep = new Vector<Object >(); 57 rep.addElement(1); rep.addElement(pathToGPG); rep.addElement(gpgVersion); 61 Vector<Object > keyPasswordsKeys = new Vector<Object >(); 62 Vector<Object > keyPasswordsValues = new Vector<Object >(); 63 64 for(String id: keyPasswords.keySet()) 65 { 66 keyPasswordsKeys.add(id); 67 keyPasswordsValues.addElement(keyPasswords.get(id)); 68 } 69 70 rep.add(keyPasswordsKeys); rep.add(keyPasswordsValues); 73 return rep; 74 } 75 76 @SuppressWarnings ("unchecked") 77 public void createFromVectorRepresentation(Vector<Object > v) 78 { 79 int version = (Integer ) v.elementAt(0); 80 if(version==1) 81 { 82 pathToGPG = (String ) v.elementAt(1); 83 gpgVersion = (String ) v.elementAt(2); 84 Vector<String > ke = (Vector<String >) v.elementAt(3); 85 Vector<byte[]> va = (Vector<byte[]>) v.elementAt(4); 86 keyPasswords.clear(); 87 for(int i=0; i<ke.size(); i++) 88 { 89 keyPasswords.put(ke.elementAt(i), va.elementAt(i)); 90 } 91 92 try 94 { 95 setGPGPath(pathToGPG); 96 } catch(Exception ignored) { System.out.println("No GPG Link");} 97 } 98 else 99 { 100 throw new RuntimeException ("Bad version "+version); 101 } 102 } 103 104 105 public String getGPGVersionString() { return this.gpgVersion; } 106 107 108 110 public boolean isGPG_available() { return isGPG_available; } 111 112 public String getPathToGPG() { return this.pathToGPG; } 113 114 116 public void setGPGPath(String path) throws Exception 117 { 118 pathToGPG = path; 119 120 122 gpgVersion = GnuPGCommands.readGPGVersion(path); 123 126 publicKeyIDsModel.clear(); 127 publicKeyIDsModel.addAll( GnuPGCommands.readPublicKeyIDs(path) ); 128 129 secretKeyIDsModel.clear(); 130 secretKeyIDsModel.addAll( GnuPGCommands.readSecretKeyIDs(path) ); 131 132 isGPG_available = true; 133 134 } 135 136 public TreeSet<GnuPGKeyID> getAllPublicKeyIDs() { return publicKeyIDsModel; } 137 public TreeSet<GnuPGKeyID> getAllSecretKeyIDs() { return secretKeyIDsModel; } 138 139 public GnuPGKeyID[] getPublicKeyIDForAddress(String mail) 140 { 141 Vector<GnuPGKeyID> rep = new Vector<GnuPGKeyID>(); 142 for(GnuPGKeyID k: publicKeyIDsModel) 143 { 144 for(UIDRecord uid: k.getUIDs()) 145 { 146 if(uid.getMail().compareToIgnoreCase(mail)==0) rep.addElement(k); 147 } 148 } 149 return rep.toArray(new GnuPGKeyID[rep.size()]); 150 } 151 152 153 154 public GnuPGKeyID[] getSecretKeyIDForAddress(String mail) 155 { 156 Vector<GnuPGKeyID> rep = new Vector<GnuPGKeyID>(); 157 for(GnuPGKeyID k: secretKeyIDsModel) 158 { 159 for(UIDRecord uid: k.getUIDs()) 160 { 161 if(uid.getMail().compareToIgnoreCase(mail)==0) rep.addElement(k); 162 } 163 } 164 return rep.toArray(new GnuPGKeyID[rep.size()]); 165 } 166 167 public boolean hasSecretKeyAssociated(GnuPGKeyID kid) 168 { 169 for(GnuPGKeyID k: secretKeyIDsModel) 170 { 171 if(k.getFingerprint().compareToIgnoreCase(kid.getFingerprint())==0) return true; 172 } 173 return false; 174 } 175 176 178 public byte[] getPasswordForKey(GnuPGKeyID kid) 179 { 180 return this.keyPasswords.get(kid.getKeyID()); 181 } 182 183 public void setPasswordForKey(GnuPGKeyID kid, byte[] pass) 184 { 185 this.keyPasswords.put(kid.getKeyID(), pass); 186 } 187 188 190 public byte[] getPasswordForKeyAskIfNotFoundOrNotValid(GnuPGKeyID kid, boolean store, JFrame parent) 191 { 192 byte[] pass = this.keyPasswords.get(kid.getKeyID()); 193 194 if(pass!=null) 195 { 196 try 198 { 199 if(GnuPGCommands.verifySecretKeyPassword(pathToGPG, kid, pass)) 200 { 201 return pass; 203 } 204 } catch(Exception ignored){} 205 } 206 207 209 GPGPasswordDialog pd = new GPGPasswordDialog(parent, this, kid, Language.translate("Enter the password for the GPG key\n%", kid.getKeyID())); 210 if(pd.isPasswordValid()) 211 { 212 if(store) 213 { 214 this.setPasswordForKey(kid, pd.getPassword()); 215 } 216 return pd.getPassword(); 217 } 218 219 return null; 220 } 221 222 224 public void askGPGPath(JFrame dialogParent) 225 { 226 final JDialog dialog = new JDialog(dialogParent, Language.translate("GnuPG Location Selection"), true); 227 final FileField gpgPathTF = new FileField(pathToGPG, false, Language.translate("Path to gpg.exe")+": ", JFileChooser.FILES_ONLY); 229 230 dialog.getContentPane().setLayout(new BorderLayout()); 231 232 JPanel inputPanel = new JPanel(); 233 inputPanel.setBorder( BorderFactory.createBevelBorder(BevelBorder.LOWERED)); 234 inputPanel.add(gpgPathTF); 235 236 dialog.getContentPane().add(inputPanel, BorderLayout.CENTER); 237 238 239 JPanel labelP = new JPanel(new BorderLayout()); 240 JTextArea explainTA = new JTextArea( Language.translate( 241 "\nPlease give the location of the application gpg.exe (Gnu Privacy Guard)." 242 + "\nYou must use at least version 1.4 or later." 243 + "\nIf you don't find this program, you have to install first GnuPG" 244 + "\non your computer. Visit the homepage at www.gnupg.org.") ); 245 246 explainTA.setBorder(BorderFactory.createEmptyBorder(5,5,5,5)); 247 explainTA.setBackground(dialog.getContentPane().getBackground()); 248 explainTA.setForeground(dialog.getContentPane().getForeground()); 249 250 labelP.add(explainTA, BorderLayout.CENTER); 251 explainTA.setEditable(false); 252 dialog.getContentPane().add(labelP, BorderLayout.NORTH); 253 254 CloseControlPanel ccp = new CloseControlPanel(dialog, true, true, Language.translate("Ok")); 255 dialog.getContentPane().add(ccp, BorderLayout.SOUTH); 256 257 258 gpgPathTF.addActionListener(new ActionListener() 259 { 260 public void actionPerformed(ActionEvent e2) 261 { 262 dialog.setVisible(false); 263 } 264 }); 265 266 267 dialog.pack(); 268 SnowMailClientApp.centerComponentOnMainFrame(dialog); 269 dialog.setVisible(true); 270 271 if(ccp.getWasCancelled()) return; 272 273 try 274 { 275 setGPGPath(gpgPathTF.getPath().getAbsolutePath()); 276 } catch(Exception e){ e.printStackTrace(); } 277 } 278 279 280 283 285 public String sign(String content, GnuPGKeyID kid, byte[] pass, Interrupter interrupter) throws Exception 286 { 287 ByteArrayInputStream bin = new ByteArrayInputStream(content.getBytes()); 288 byte[] rep = GnuPGCommands.signBuffer(this.pathToGPG, bin, kid, pass, interrupter ); 289 return new String (rep); 290 } 291 292 public byte[] sign(ByteArrayInputStream bin, GnuPGKeyID kid, byte[] pass, Interrupter interrupter) throws Exception 293 { 294 byte[] rep = GnuPGCommands.signBuffer(this.pathToGPG, bin, kid, pass, interrupter ); 295 return rep; 296 } 297 298 public String getPublicKeyContent(GnuPGKeyID kid) throws Exception 299 { 300 return GnuPGCommands.getPublicKey(this.pathToGPG, kid); 301 } 302 303 public String getSecretKeyContent(GnuPGKeyID kid) throws Exception 304 { 305 return GnuPGCommands.getSecretKey(this.pathToGPG, kid); 306 } 307 308 public void addKey(String asciiKey) throws Exception 309 { 310 GnuPGCommands.addKey(this.pathToGPG, asciiKey); 311 } 312 313 public void removeKey(GnuPGKeyID kid) throws Exception 314 { 315 if(kid.isSecret()) 316 { 317 GnuPGCommands.removePublicAndPrivateKey(pathToGPG, kid); 319 } 320 else 321 { 322 GnuPGCommands.removePublicKey(pathToGPG, kid); 323 } 324 } 325 326 public void removePublicAndPrivateKey(GnuPGKeyID kid) throws Exception 327 { 328 GnuPGCommands.removePublicAndPrivateKey(pathToGPG, kid); 329 } 330 331 } | Popular Tags |