001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.pgp;
017:
018: import java.util.Hashtable;
019: import java.util.Map;
020: import java.util.Properties;
021:
022: import org.columba.mail.gui.util.PGPPassphraseDialog;
023: import org.waffel.jscf.JSCFConnection;
024: import org.waffel.jscf.JSCFException;
025: import org.waffel.jscf.JSCFStatement;
026:
027: /**
028: * Checks via a dialog if a passphrase (given from the user over the dialog) can
029: * be used to sign a testmessage. The dialog-test is a while loop, which breaks
030: * only if the user cancels the dialog or the passphrase is correct.
031: *
032: * @author waffel
033: *
034: */
035: public class PGPPassChecker {
036: private static PGPPassChecker myInstance = null;
037:
038: private Map passwordMap = new Hashtable();
039:
040: /**
041: * Returns the instance of the class. If no instance is created, a new
042: * instance are created.
043: *
044: * @return a instance of this class.
045: */
046: public static PGPPassChecker getInstance() {
047: if (myInstance == null) {
048: myInstance = new PGPPassChecker();
049: }
050:
051: return myInstance;
052: }
053:
054: /**
055: * Checks with a test string if the test String can be signed. The user is
056: * ask for his passphrase until the passphrase is ok or the user cancels the
057: * dialog. If the user cancels the dialog the method returns false. This
058: * method return normal only if the user give the right passphrase.
059: *
060: * @param con
061: * JSCFConnection used to check a passphrase given by a dialog
062: * @return Returns true if the given passphrase (via a dialog) is correct
063: * and the user can sign a teststring with the passphrase from the
064: * dialog. Returns false if the user cancels the dialog.
065: * @exception JSCFException
066: * if the concrete JSCF implementation has real probelms (for
067: * example a extern tool cannot be found)
068: */
069: public boolean checkPassphrase(JSCFConnection con)
070: throws JSCFException {
071: boolean stmtCheck = false;
072: JSCFStatement stmt = con.createStatement();
073:
074: // loop until signing was sucessful or the user cancels the passphrase
075: // dialog
076: Properties props = con.getProperties();
077:
078: while (!stmtCheck && (this .checkPassphraseDialog(con) == true)) {
079: stmtCheck = stmt.checkPassphrase();
080:
081: if (!stmtCheck) {
082: this .passwordMap.remove(props.get("USERID"));
083: }
084: }
085:
086: return stmtCheck;
087: }
088:
089: private boolean checkPassphraseDialog(JSCFConnection con) {
090: String passphrase = "";
091: Properties props = con.getProperties();
092:
093: if (this .passwordMap.containsKey(props.get("USERID"))) {
094: passphrase = (String) this .passwordMap.get(props
095: .get("USERID"));
096: }
097:
098: props.put("PASSWORD", passphrase);
099:
100: boolean ret = true;
101:
102: PGPPassphraseDialog dialog = new PGPPassphraseDialog();
103:
104: if (passphrase.length() == 0) {
105: dialog.showDialog((String) props.get("USERID"),
106: (String) props.get("PASSWORD"), false);
107:
108: if (dialog.success()) {
109: passphrase = new String(dialog.getPassword(), 0, dialog
110: .getPassword().length);
111: props.put("PASSWORD", passphrase);
112:
113: boolean save = dialog.getSave();
114:
115: // save passphrase in hash map
116: if (save) {
117: this .passwordMap.put(props.get("USERID"),
118: passphrase);
119: }
120:
121: ret = true;
122: } else {
123: ret = false;
124: }
125: }
126:
127: con.setProperties(props);
128:
129: return ret;
130: }
131: }
|