source: osm/applications/editors/josm/plugins/native-password-manager/netbeans-keyring-patches.diff@ 33088

Last change on this file since 33088 was 26361, checked in by bastik, 13 years ago

make it work for windows

File size: 15.2 KB
RevLine 
[26336]1Index: native-password-manager/src/org/netbeans/modules/keyring/fallback/FallbackProvider.java
2===================================================================
[26361]3--- native-password-manager.orig/src/org/netbeans/modules/keyring/fallback/FallbackProvider.java 2011-07-18 13:04:37.141522563 +0200
4+++ native-password-manager/src/org/netbeans/modules/keyring/fallback/FallbackProvider.java 2011-07-18 13:09:34.194995561 +0200
[26336]5@@ -42,49 +42,41 @@
6
7 package org.netbeans.modules.keyring.fallback;
8
9-import java.util.Arrays;
10-import java.util.HashMap;
11-import java.util.Map;
12 import java.util.UUID;
13-import java.util.concurrent.Callable;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
16-import java.util.prefs.BackingStoreException;
17-import java.util.prefs.Preferences;
18-import org.netbeans.api.keyring.Keyring;
19 import org.netbeans.modules.keyring.impl.Utils;
20 import org.netbeans.modules.keyring.spi.EncryptionProvider;
21 import org.netbeans.spi.keyring.KeyringProvider;
22-import org.openide.DialogDisplayer;
23-import org.openide.NotifyDescriptor;
24-import org.openide.util.Lookup;
25-import org.openide.util.NbBundle;
26-import org.openide.util.NbPreferences;
27-import org.openide.util.lookup.ServiceProvider;
28
29 /**
30 * Platform-independent keyring provider using a master password and the user directory.
31 */
32-@ServiceProvider(service=KeyringProvider.class, position=1000)
33-public class FallbackProvider implements KeyringProvider, Callable<Void> {
34+public class FallbackProvider implements KeyringProvider {
35
36 private static final Logger LOG = Logger.getLogger(FallbackProvider.class.getName());
37 private static final String DESCRIPTION = ".description";
38 private static final String SAMPLE_KEY = "__sample__";
39
40 private EncryptionProvider encryption;
41-
[26361]42+ private IPreferences prefs;
43+
[26336]44+ // simple interface for a generic preferences store
45+ public interface IPreferences {
[26361]46+ byte[] getByteArray(String key, byte[] def);
47+ void putByteArray(String key, byte[] val);
[26336]48+ void remove(String key);
49+ }
50+
[26361]51+ public FallbackProvider(EncryptionProvider encryption, IPreferences prefs) {
[26336]52+ this.encryption = encryption;
[26361]53+ this.prefs = prefs;
[26336]54+ }
[26361]55+
[26336]56 public boolean enabled() {
57- for (EncryptionProvider p : Lookup.getDefault().lookupAll(EncryptionProvider.class)) {
58- if (p.enabled()) {
59- encryption = p;
60- Preferences prefs = prefs();
61- Utils.goMinusR(prefs);
62- p.encryptionChangingCallback(this);
63- if (!testSampleKey(prefs)) {
64- continue;
65- }
66- LOG.log(Level.FINE, "Using provider: {0}", p);
67+ if (encryption.enabled()) {
68+ if (testSampleKey()) {
69+ LOG.log(Level.FINE, "Using provider: {0}", encryption);
70 return true;
71 }
72 }
[26361]73@@ -92,65 +84,20 @@
[26336]74 return false;
75 }
76
77- private boolean testSampleKey(Preferences prefs) {
78- byte[] ciphertext = prefs.getByteArray(SAMPLE_KEY, null);
79- if (ciphertext == null) {
80- encryption.freshKeyring(true);
81- if (_save(SAMPLE_KEY, (SAMPLE_KEY + UUID.randomUUID()).toCharArray(),
82- NbBundle.getMessage(FallbackProvider.class, "FallbackProvider.sample_key.description"))) {
83- LOG.fine("saved sample key");
84- return true;
85- } else {
86- LOG.fine("could not save sample key");
87- return false;
88- }
[26361]89- } else {
[26336]90- encryption.freshKeyring(false);
91- while (true) {
92- try {
93- if (new String(encryption.decrypt(ciphertext)).startsWith(SAMPLE_KEY)) {
94- LOG.fine("succeeded in decrypting sample key");
95- return true;
96- } else {
97- LOG.fine("wrong result decrypting sample key");
98- }
99- } catch (Exception x) {
100- LOG.log(Level.FINE, "failed to decrypt sample key", x);
101- }
102- if (!encryption.decryptionFailed()) {
103- LOG.fine("sample key decryption failed");
104- return promptToDelete(prefs);
105- }
106- LOG.fine("will retry decryption of sample key");
107- }
108- }
109- }
110-
111- private boolean promptToDelete(Preferences prefs) {
112- Object result = DialogDisplayer.getDefault().notify(new NotifyDescriptor.Confirmation(
113- NbBundle.getMessage(FallbackProvider.class, "FallbackProvider.msg_clear_keys"),
114- NbBundle.getMessage(FallbackProvider.class, "FallbackProvider.title_clear_keys"),
115- NotifyDescriptor.OK_CANCEL_OPTION));
116- if (result == NotifyDescriptor.OK_OPTION) {
117- try {
118- LOG.log(Level.FINE, "agreed to delete stored passwords: {0}", Arrays.asList(prefs.keys()));
119- prefs.clear();
120- return testSampleKey(prefs);
121- } catch (BackingStoreException x) {
122- LOG.log(Level.INFO, null, x);
123- }
[26361]124+ private boolean testSampleKey() {
125+ encryption.freshKeyring(true);
126+ if (_save(SAMPLE_KEY, (SAMPLE_KEY + UUID.randomUUID()).toCharArray(),
127+ "Sample value ensuring that decryption is working.")) {
128+ LOG.fine("saved sample key");
129+ return true;
130 } else {
[26336]131- LOG.fine("refused to delete stored passwords");
132+ LOG.fine("could not save sample key");
133+ return false;
134 }
135- return false;
136- }
137-
138- private Preferences prefs() {
139- return NbPreferences.forModule(Keyring.class).node(encryption.id());
140 }
141
142 public char[] read(String key) {
143- byte[] ciphertext = prefs().getByteArray(key, null);
[26361]144+ byte[] ciphertext = prefs.getByteArray(key, null);
[26336]145 if (ciphertext == null) {
146 return null;
147 }
[26361]148@@ -166,47 +113,18 @@
[26336]149 _save(key, password, description);
150 }
151 private boolean _save(String key, char[] password, String description) {
152- Preferences prefs = prefs();
153 try {
[26361]154 prefs.putByteArray(key, encryption.encrypt(password));
[26336]155 } catch (Exception x) {
156 LOG.log(Level.FINE, "failed to encrypt password for " + key, x);
157 return false;
158 }
[26361]159- if (description != null) {
160- // Preferences interface gives no access to *.properties comments, so:
[26336]161- prefs.put(key + DESCRIPTION, description);
[26361]162- }
[26336]163 return true;
164 }
165
166 public void delete(String key) {
167- Preferences prefs = prefs();
[26361]168 prefs.remove(key);
169 prefs.remove(key + DESCRIPTION);
170 }
171
[26336]172- public Void call() throws Exception { // encryption changing
173- LOG.fine("encryption changing");
174- Map<String,char[]> saved = new HashMap<String,char[]>();
175- Preferences prefs = prefs();
176- for (String k : prefs.keys()) {
177- if (k.endsWith(DESCRIPTION)) {
178- continue;
179- }
180- byte[] ciphertext = prefs.getByteArray(k, null);
181- if (ciphertext == null) {
182- continue;
183- }
184- saved.put(k, encryption.decrypt(ciphertext));
185- }
186- LOG.log(Level.FINE, "reencrypting keys: {0}", saved.keySet());
187- encryption.encryptionChanged();
188- for (Map.Entry<String,char[]> entry : saved.entrySet()) {
189- prefs.putByteArray(entry.getKey(), encryption.encrypt(entry.getValue()));
190- }
191- LOG.fine("encryption changing finished");
192- return null;
[26361]193- }
194-
[26336]195 }
196Index: native-password-manager/src/org/netbeans/modules/keyring/gnome/GnomeProvider.java
197===================================================================
[26361]198--- native-password-manager.orig/src/org/netbeans/modules/keyring/gnome/GnomeProvider.java 2011-07-18 13:04:37.141522563 +0200
199+++ native-password-manager/src/org/netbeans/modules/keyring/gnome/GnomeProvider.java 2011-07-18 13:05:00.841640080 +0200
[26336]200@@ -43,16 +43,11 @@
201 package org.netbeans.modules.keyring.gnome;
202
203 import com.sun.jna.Pointer;
204-import java.text.MessageFormat;
205-import java.util.MissingResourceException;
206 import java.util.logging.Level;
207 import java.util.logging.Logger;
208 import static org.netbeans.modules.keyring.gnome.GnomeKeyringLibrary.*;
209 import org.netbeans.spi.keyring.KeyringProvider;
210-import org.openide.util.NbBundle;
211-import org.openide.util.lookup.ServiceProvider;
212
213-@ServiceProvider(service=KeyringProvider.class, position=100)
214 public class GnomeProvider implements KeyringProvider {
215
216 private static final Logger LOG = Logger.getLogger(GnomeProvider.class.getName());
217@@ -74,14 +69,7 @@
218 LOG.fine("no GNOME_KEYRING_* environment variable set");
219 return false;
220 }
221- String appName;
222- try {
223- appName = MessageFormat.format(
224- NbBundle.getBundle("org.netbeans.core.windows.view.ui.Bundle").getString("CTL_MainWindow_Title_No_Project"),
225- /*System.getProperty("netbeans.buildnumber")*/"…");
226- } catch (MissingResourceException x) {
227- appName = "NetBeans"; // NOI18N
228- }
229+ String appName = "JOSM";
230 try {
231 // Need to do this somewhere, or we get warnings on console.
232 // Also used by confirmation dialogs to give the app access to the login keyring.
233Index: native-password-manager/src/org/netbeans/modules/keyring/kde/KWalletProvider.java
234===================================================================
[26361]235--- native-password-manager.orig/src/org/netbeans/modules/keyring/kde/KWalletProvider.java 2011-07-18 13:04:37.149522597 +0200
236+++ native-password-manager/src/org/netbeans/modules/keyring/kde/KWalletProvider.java 2011-07-18 13:05:00.849640122 +0200
[26336]237@@ -45,20 +45,15 @@
238 import java.io.BufferedReader;
239 import java.io.IOException;
240 import java.io.InputStreamReader;
241-import java.text.MessageFormat;
242 import java.util.Arrays;
243-import java.util.MissingResourceException;
244 import java.util.logging.Level;
245 import java.util.logging.Logger;
246 import org.netbeans.spi.keyring.KeyringProvider;
247-import org.openide.util.NbBundle;
248-import org.openide.util.lookup.ServiceProvider;
249
250 /**
251 *
252 * @author psychollek, ynov
253 */
254-@ServiceProvider(service=KeyringProvider.class, position=99)
255 public class KWalletProvider implements KeyringProvider{
256
257 private static final Logger logger = Logger.getLogger(KWalletProvider.class.getName());
258@@ -221,13 +216,7 @@
259 }
260
261 private char[] getApplicationName(boolean version){
262- String appName;
263- try {
264- appName = MessageFormat.format(NbBundle.getBundle("org.netbeans.core.windows.view.ui.Bundle").getString("CTL_MainWindow_Title_No_Project"),version ? System.getProperty("netbeans.buildnumber"):"");
265- } catch (MissingResourceException x) {
266- appName = "NetBeans"+(version? " "+System.getProperty("netbeans.buildnumber"):"");
267- }
268- return appName.toCharArray();
269+ return "JOSM".toCharArray();
270 }
271
272 private void warning(String descr) {
273Index: native-password-manager/src/org/netbeans/modules/keyring/mac/MacProvider.java
274===================================================================
[26361]275--- native-password-manager.orig/src/org/netbeans/modules/keyring/mac/MacProvider.java 2011-07-18 13:04:37.157522639 +0200
276+++ native-password-manager/src/org/netbeans/modules/keyring/mac/MacProvider.java 2011-07-18 13:05:00.849640122 +0200
[26336]277@@ -47,26 +47,19 @@
278 import java.util.logging.Level;
279 import java.util.logging.Logger;
280 import org.netbeans.spi.keyring.KeyringProvider;
281-import org.openide.util.Utilities;
282-import org.openide.util.lookup.ServiceProvider;
283
284-@ServiceProvider(service=KeyringProvider.class, position=200)
285 public class MacProvider implements KeyringProvider {
286
287 private static final Logger LOG = Logger.getLogger(MacProvider.class.getName());
288
289 public boolean enabled() {
290- if (Boolean.getBoolean("netbeans.keyring.no.native")) {
291- LOG.fine("native keyring integration disabled");
292- return false;
293- }
294- return Utilities.isMac();
295+ return true; // test elsewhere if we are on a mac
296 }
297
298 public char[] read(String key) {
299 try {
300 byte[] serviceName = key.getBytes("UTF-8");
301- byte[] accountName = "NetBeans".getBytes("UTF-8");
302+ byte[] accountName = "JOSM".getBytes("UTF-8");
303 int[] dataLength = new int[1];
304 Pointer[] data = new Pointer[1];
305 error("find", SecurityLibrary.LIBRARY.SecKeychainFindGenericPassword(null, serviceName.length, serviceName,
306@@ -86,7 +79,7 @@
307 delete(key); // XXX supposed to use SecKeychainItemModifyContent instead, but this seems like too much work
308 try {
309 byte[] serviceName = key.getBytes("UTF-8");
310- byte[] accountName = "NetBeans".getBytes("UTF-8");
311+ byte[] accountName = "JOSM".getBytes("UTF-8");
312 // Keychain Access seems to expect UTF-8, so do not use Utils.chars2Bytes:
313 byte[] data = new String(password).getBytes("UTF-8");
314 error("save", SecurityLibrary.LIBRARY.SecKeychainAddGenericPassword(null, serviceName.length, serviceName,
315@@ -100,7 +93,7 @@
316 public void delete(String key) {
317 try {
318 byte[] serviceName = key.getBytes("UTF-8");
319- byte[] accountName = "NetBeans".getBytes("UTF-8");
320+ byte[] accountName = "JOSM".getBytes("UTF-8");
321 Pointer[] itemRef = new Pointer[1];
322 error("find (for delete)", SecurityLibrary.LIBRARY.SecKeychainFindGenericPassword(null, serviceName.length, serviceName,
323 accountName.length, accountName, null, null, itemRef));
324Index: native-password-manager/src/org/netbeans/modules/keyring/win32/Win32Protect.java
325===================================================================
[26361]326--- native-password-manager.orig/src/org/netbeans/modules/keyring/win32/Win32Protect.java 2011-07-18 13:04:37.165522672 +0200
327+++ native-password-manager/src/org/netbeans/modules/keyring/win32/Win32Protect.java 2011-07-18 13:05:00.849640122 +0200
[26336]328@@ -54,28 +54,18 @@
329 import java.util.logging.Logger;
330 import org.netbeans.modules.keyring.impl.Utils;
331 import org.netbeans.modules.keyring.spi.EncryptionProvider;
332-import org.openide.util.Utilities;
333-import org.openide.util.lookup.ServiceProvider;
334
335 /**
336 * Data protection utility for Microsoft Windows.
337 * XXX org.tmatesoft.svn.core.internal.util.jna.SVNWinCrypt is a possibly more robust implementation
338 * (though it seems to set CRYPTPROTECT_UI_FORBIDDEN which we do not necessarily want).
339 */
340-@ServiceProvider(service=EncryptionProvider.class, position=100)
341 public class Win32Protect implements EncryptionProvider {
342
343 private static final Logger LOG = Logger.getLogger(Win32Protect.class.getName());
344
345 public @Override boolean enabled() {
346- if (!Utilities.isWindows()) {
347- LOG.fine("not running on Windows");
348- return false;
349- }
350- if (Boolean.getBoolean("netbeans.keyring.no.native")) {
351- LOG.fine("native keyring integration disabled");
352- return false;
353- }
354+ // asssume, we have windows os
355 try {
356 if (CryptLib.INSTANCE == null) {
357 LOG.fine("loadLibrary -> null");
Note: See TracBrowser for help on using the repository browser.