source:
osm/applications/editors/josm/plugins/native-password-manager/netbeans-keyring-patches.diff@
34795
Last change on this file since 34795 was 26361, checked in by , 13 years ago | |
---|---|
File size: 15.2 KB |
-
src/org/netbeans/modules/keyring/fallback/FallbackProvider.java
old new 42 42 43 43 package org.netbeans.modules.keyring.fallback; 44 44 45 import java.util.Arrays;46 import java.util.HashMap;47 import java.util.Map;48 45 import java.util.UUID; 49 import java.util.concurrent.Callable;50 46 import java.util.logging.Level; 51 47 import java.util.logging.Logger; 52 import java.util.prefs.BackingStoreException;53 import java.util.prefs.Preferences;54 import org.netbeans.api.keyring.Keyring;55 48 import org.netbeans.modules.keyring.impl.Utils; 56 49 import org.netbeans.modules.keyring.spi.EncryptionProvider; 57 50 import org.netbeans.spi.keyring.KeyringProvider; 58 import org.openide.DialogDisplayer;59 import org.openide.NotifyDescriptor;60 import org.openide.util.Lookup;61 import org.openide.util.NbBundle;62 import org.openide.util.NbPreferences;63 import org.openide.util.lookup.ServiceProvider;64 51 65 52 /** 66 53 * Platform-independent keyring provider using a master password and the user directory. 67 54 */ 68 @ServiceProvider(service=KeyringProvider.class, position=1000) 69 public class FallbackProvider implements KeyringProvider, Callable<Void> { 55 public class FallbackProvider implements KeyringProvider { 70 56 71 57 private static final Logger LOG = Logger.getLogger(FallbackProvider.class.getName()); 72 58 private static final String DESCRIPTION = ".description"; 73 59 private static final String SAMPLE_KEY = "__sample__"; 74 60 75 61 private EncryptionProvider encryption; 76 62 private IPreferences prefs; 63 64 // simple interface for a generic preferences store 65 public interface IPreferences { 66 byte[] getByteArray(String key, byte[] def); 67 void putByteArray(String key, byte[] val); 68 void remove(String key); 69 } 70 71 public FallbackProvider(EncryptionProvider encryption, IPreferences prefs) { 72 this.encryption = encryption; 73 this.prefs = prefs; 74 } 75 77 76 public boolean enabled() { 78 for (EncryptionProvider p : Lookup.getDefault().lookupAll(EncryptionProvider.class)) { 79 if (p.enabled()) { 80 encryption = p; 81 Preferences prefs = prefs(); 82 Utils.goMinusR(prefs); 83 p.encryptionChangingCallback(this); 84 if (!testSampleKey(prefs)) { 85 continue; 86 } 87 LOG.log(Level.FINE, "Using provider: {0}", p); 77 if (encryption.enabled()) { 78 if (testSampleKey()) { 79 LOG.log(Level.FINE, "Using provider: {0}", encryption); 88 80 return true; 89 81 } 90 82 } … … 92 84 return false; 93 85 } 94 86 95 private boolean testSampleKey(Preferences prefs) { 96 byte[] ciphertext = prefs.getByteArray(SAMPLE_KEY, null); 97 if (ciphertext == null) { 98 encryption.freshKeyring(true); 99 if (_save(SAMPLE_KEY, (SAMPLE_KEY + UUID.randomUUID()).toCharArray(), 100 NbBundle.getMessage(FallbackProvider.class, "FallbackProvider.sample_key.description"))) { 101 LOG.fine("saved sample key"); 102 return true; 103 } else { 104 LOG.fine("could not save sample key"); 105 return false; 106 } 107 } else { 108 encryption.freshKeyring(false); 109 while (true) { 110 try { 111 if (new String(encryption.decrypt(ciphertext)).startsWith(SAMPLE_KEY)) { 112 LOG.fine("succeeded in decrypting sample key"); 113 return true; 114 } else { 115 LOG.fine("wrong result decrypting sample key"); 116 } 117 } catch (Exception x) { 118 LOG.log(Level.FINE, "failed to decrypt sample key", x); 119 } 120 if (!encryption.decryptionFailed()) { 121 LOG.fine("sample key decryption failed"); 122 return promptToDelete(prefs); 123 } 124 LOG.fine("will retry decryption of sample key"); 125 } 126 } 127 } 128 129 private boolean promptToDelete(Preferences prefs) { 130 Object result = DialogDisplayer.getDefault().notify(new NotifyDescriptor.Confirmation( 131 NbBundle.getMessage(FallbackProvider.class, "FallbackProvider.msg_clear_keys"), 132 NbBundle.getMessage(FallbackProvider.class, "FallbackProvider.title_clear_keys"), 133 NotifyDescriptor.OK_CANCEL_OPTION)); 134 if (result == NotifyDescriptor.OK_OPTION) { 135 try { 136 LOG.log(Level.FINE, "agreed to delete stored passwords: {0}", Arrays.asList(prefs.keys())); 137 prefs.clear(); 138 return testSampleKey(prefs); 139 } catch (BackingStoreException x) { 140 LOG.log(Level.INFO, null, x); 141 } 87 private boolean testSampleKey() { 88 encryption.freshKeyring(true); 89 if (_save(SAMPLE_KEY, (SAMPLE_KEY + UUID.randomUUID()).toCharArray(), 90 "Sample value ensuring that decryption is working.")) { 91 LOG.fine("saved sample key"); 92 return true; 142 93 } else { 143 LOG.fine("refused to delete stored passwords"); 94 LOG.fine("could not save sample key"); 95 return false; 144 96 } 145 return false;146 }147 148 private Preferences prefs() {149 return NbPreferences.forModule(Keyring.class).node(encryption.id());150 97 } 151 98 152 99 public char[] read(String key) { 153 byte[] ciphertext = prefs ().getByteArray(key, null);100 byte[] ciphertext = prefs.getByteArray(key, null); 154 101 if (ciphertext == null) { 155 102 return null; 156 103 } … … 166 113 _save(key, password, description); 167 114 } 168 115 private boolean _save(String key, char[] password, String description) { 169 Preferences prefs = prefs();170 116 try { 171 117 prefs.putByteArray(key, encryption.encrypt(password)); 172 118 } catch (Exception x) { 173 119 LOG.log(Level.FINE, "failed to encrypt password for " + key, x); 174 120 return false; 175 121 } 176 if (description != null) {177 // Preferences interface gives no access to *.properties comments, so:178 prefs.put(key + DESCRIPTION, description);179 }180 122 return true; 181 123 } 182 124 183 125 public void delete(String key) { 184 Preferences prefs = prefs();185 126 prefs.remove(key); 186 127 prefs.remove(key + DESCRIPTION); 187 128 } 188 129 189 public Void call() throws Exception { // encryption changing190 LOG.fine("encryption changing");191 Map<String,char[]> saved = new HashMap<String,char[]>();192 Preferences prefs = prefs();193 for (String k : prefs.keys()) {194 if (k.endsWith(DESCRIPTION)) {195 continue;196 }197 byte[] ciphertext = prefs.getByteArray(k, null);198 if (ciphertext == null) {199 continue;200 }201 saved.put(k, encryption.decrypt(ciphertext));202 }203 LOG.log(Level.FINE, "reencrypting keys: {0}", saved.keySet());204 encryption.encryptionChanged();205 for (Map.Entry<String,char[]> entry : saved.entrySet()) {206 prefs.putByteArray(entry.getKey(), encryption.encrypt(entry.getValue()));207 }208 LOG.fine("encryption changing finished");209 return null;210 }211 212 130 } -
src/org/netbeans/modules/keyring/gnome/GnomeProvider.java
old new 43 43 package org.netbeans.modules.keyring.gnome; 44 44 45 45 import com.sun.jna.Pointer; 46 import java.text.MessageFormat;47 import java.util.MissingResourceException;48 46 import java.util.logging.Level; 49 47 import java.util.logging.Logger; 50 48 import static org.netbeans.modules.keyring.gnome.GnomeKeyringLibrary.*; 51 49 import org.netbeans.spi.keyring.KeyringProvider; 52 import org.openide.util.NbBundle;53 import org.openide.util.lookup.ServiceProvider;54 50 55 @ServiceProvider(service=KeyringProvider.class, position=100)56 51 public class GnomeProvider implements KeyringProvider { 57 52 58 53 private static final Logger LOG = Logger.getLogger(GnomeProvider.class.getName()); … … 74 69 LOG.fine("no GNOME_KEYRING_* environment variable set"); 75 70 return false; 76 71 } 77 String appName; 78 try { 79 appName = MessageFormat.format( 80 NbBundle.getBundle("org.netbeans.core.windows.view.ui.Bundle").getString("CTL_MainWindow_Title_No_Project"), 81 /*System.getProperty("netbeans.buildnumber")*/"…"); 82 } catch (MissingResourceException x) { 83 appName = "NetBeans"; // NOI18N 84 } 72 String appName = "JOSM"; 85 73 try { 86 74 // Need to do this somewhere, or we get warnings on console. 87 75 // Also used by confirmation dialogs to give the app access to the login keyring. -
src/org/netbeans/modules/keyring/kde/KWalletProvider.java
old new 45 45 import java.io.BufferedReader; 46 46 import java.io.IOException; 47 47 import java.io.InputStreamReader; 48 import java.text.MessageFormat;49 48 import java.util.Arrays; 50 import java.util.MissingResourceException;51 49 import java.util.logging.Level; 52 50 import java.util.logging.Logger; 53 51 import org.netbeans.spi.keyring.KeyringProvider; 54 import org.openide.util.NbBundle;55 import org.openide.util.lookup.ServiceProvider;56 52 57 53 /** 58 54 * 59 55 * @author psychollek, ynov 60 56 */ 61 @ServiceProvider(service=KeyringProvider.class, position=99)62 57 public class KWalletProvider implements KeyringProvider{ 63 58 64 59 private static final Logger logger = Logger.getLogger(KWalletProvider.class.getName()); … … 221 216 } 222 217 223 218 private char[] getApplicationName(boolean version){ 224 String appName; 225 try { 226 appName = MessageFormat.format(NbBundle.getBundle("org.netbeans.core.windows.view.ui.Bundle").getString("CTL_MainWindow_Title_No_Project"),version ? System.getProperty("netbeans.buildnumber"):""); 227 } catch (MissingResourceException x) { 228 appName = "NetBeans"+(version? " "+System.getProperty("netbeans.buildnumber"):""); 229 } 230 return appName.toCharArray(); 219 return "JOSM".toCharArray(); 231 220 } 232 221 233 222 private void warning(String descr) { -
src/org/netbeans/modules/keyring/mac/MacProvider.java
old new 47 47 import java.util.logging.Level; 48 48 import java.util.logging.Logger; 49 49 import org.netbeans.spi.keyring.KeyringProvider; 50 import org.openide.util.Utilities;51 import org.openide.util.lookup.ServiceProvider;52 50 53 @ServiceProvider(service=KeyringProvider.class, position=200)54 51 public class MacProvider implements KeyringProvider { 55 52 56 53 private static final Logger LOG = Logger.getLogger(MacProvider.class.getName()); 57 54 58 55 public boolean enabled() { 59 if (Boolean.getBoolean("netbeans.keyring.no.native")) { 60 LOG.fine("native keyring integration disabled"); 61 return false; 62 } 63 return Utilities.isMac(); 56 return true; // test elsewhere if we are on a mac 64 57 } 65 58 66 59 public char[] read(String key) { 67 60 try { 68 61 byte[] serviceName = key.getBytes("UTF-8"); 69 byte[] accountName = " NetBeans".getBytes("UTF-8");62 byte[] accountName = "JOSM".getBytes("UTF-8"); 70 63 int[] dataLength = new int[1]; 71 64 Pointer[] data = new Pointer[1]; 72 65 error("find", SecurityLibrary.LIBRARY.SecKeychainFindGenericPassword(null, serviceName.length, serviceName, … … 86 79 delete(key); // XXX supposed to use SecKeychainItemModifyContent instead, but this seems like too much work 87 80 try { 88 81 byte[] serviceName = key.getBytes("UTF-8"); 89 byte[] accountName = " NetBeans".getBytes("UTF-8");82 byte[] accountName = "JOSM".getBytes("UTF-8"); 90 83 // Keychain Access seems to expect UTF-8, so do not use Utils.chars2Bytes: 91 84 byte[] data = new String(password).getBytes("UTF-8"); 92 85 error("save", SecurityLibrary.LIBRARY.SecKeychainAddGenericPassword(null, serviceName.length, serviceName, … … 100 93 public void delete(String key) { 101 94 try { 102 95 byte[] serviceName = key.getBytes("UTF-8"); 103 byte[] accountName = " NetBeans".getBytes("UTF-8");96 byte[] accountName = "JOSM".getBytes("UTF-8"); 104 97 Pointer[] itemRef = new Pointer[1]; 105 98 error("find (for delete)", SecurityLibrary.LIBRARY.SecKeychainFindGenericPassword(null, serviceName.length, serviceName, 106 99 accountName.length, accountName, null, null, itemRef)); -
src/org/netbeans/modules/keyring/win32/Win32Protect.java
old new 54 54 import java.util.logging.Logger; 55 55 import org.netbeans.modules.keyring.impl.Utils; 56 56 import org.netbeans.modules.keyring.spi.EncryptionProvider; 57 import org.openide.util.Utilities;58 import org.openide.util.lookup.ServiceProvider;59 57 60 58 /** 61 59 * Data protection utility for Microsoft Windows. 62 60 * XXX org.tmatesoft.svn.core.internal.util.jna.SVNWinCrypt is a possibly more robust implementation 63 61 * (though it seems to set CRYPTPROTECT_UI_FORBIDDEN which we do not necessarily want). 64 62 */ 65 @ServiceProvider(service=EncryptionProvider.class, position=100)66 63 public class Win32Protect implements EncryptionProvider { 67 64 68 65 private static final Logger LOG = Logger.getLogger(Win32Protect.class.getName()); 69 66 70 67 public @Override boolean enabled() { 71 if (!Utilities.isWindows()) { 72 LOG.fine("not running on Windows"); 73 return false; 74 } 75 if (Boolean.getBoolean("netbeans.keyring.no.native")) { 76 LOG.fine("native keyring integration disabled"); 77 return false; 78 } 68 // asssume, we have windows os 79 69 try { 80 70 if (CryptLib.INSTANCE == null) { 81 71 LOG.fine("loadLibrary -> null");
Note:
See TracBrowser
for help on using the repository browser.