Ignore:
Timestamp:
2014-11-29T16:30:52+01:00 (10 years ago)
Author:
donvip
Message:

[josm_native-password-manager] update Netbeans Keyring code + make plugin rely on JNA plugin

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/native-password-manager/src/org/netbeans/modules/keyring/mac/MacProvider.java

    r26335 r30822  
    7777
    7878    public void save(String key, char[] password, String description) {
    79         delete(key); // XXX supposed to use SecKeychainItemModifyContent instead, but this seems like too much work
    8079        try {
    8180            byte[] serviceName = key.getBytes("UTF-8");
     
    8382            // Keychain Access seems to expect UTF-8, so do not use Utils.chars2Bytes:
    8483            byte[] data = new String(password).getBytes("UTF-8");
    85             error("save", SecurityLibrary.LIBRARY.SecKeychainAddGenericPassword(null, serviceName.length, serviceName,
    86                     accountName.length, accountName, data.length, data, null));
     84            Pointer[] itemRef = new Pointer[1];
     85            error("find (for save)", SecurityLibrary.LIBRARY.SecKeychainFindGenericPassword(null, serviceName.length, serviceName,
     86                    accountName.length, accountName, null, null, itemRef));
     87            if (itemRef[0] != null) {
     88                error("save (update)", SecurityLibrary.LIBRARY.SecKeychainItemModifyContent(itemRef[0], null, data.length, data));
     89                SecurityLibrary.LIBRARY.CFRelease(itemRef[0]);
     90            } else {
     91                error("save (new)", SecurityLibrary.LIBRARY.SecKeychainAddGenericPassword(null, serviceName.length, serviceName,
     92                        accountName.length, accountName, data.length, data, null));
     93            }
    8794        } catch (UnsupportedEncodingException x) {
    8895            LOG.log(Level.WARNING, null, x);
     
    100107            if (itemRef[0] != null) {
    101108                error("delete", SecurityLibrary.LIBRARY.SecKeychainItemDelete(itemRef[0]));
     109                SecurityLibrary.LIBRARY.CFRelease(itemRef[0]);
    102110            }
    103111        } catch (UnsupportedEncodingException x) {
     
    108116    private static void error(String msg, int code) {
    109117        if (code != 0 && code != /* errSecItemNotFound, always returned from find it seems */-25300) {
    110             // XXX translate, but SecCopyErrorMessageString returns weird CFStringRef
    111             LOG.warning(msg + ": " + code);
     118            Pointer translated = SecurityLibrary.LIBRARY.SecCopyErrorMessageString(code, null);
     119            String str;
     120            if (translated == null) {
     121                str = String.valueOf(code);
     122            } else {
     123                char[] buf = new char[(int) SecurityLibrary.LIBRARY.CFStringGetLength(translated)];
     124                for (int i = 0; i < buf.length; i++) {
     125                    buf[i] = SecurityLibrary.LIBRARY.CFStringGetCharacterAtIndex(translated, i);
     126                }
     127                SecurityLibrary.LIBRARY.CFRelease(translated);
     128                str = new String(buf) + " (" + code + ")";
     129            }
     130            LOG.log(Level.WARNING, "{0}: {1}", new Object[] {msg, str});
    112131        }
    113132    }
Note: See TracChangeset for help on using the changeset viewer.