Package net.east301.keyring.util

Examples of net.east301.keyring.util.FileBasedLock


            CoreFoundation = (CoreFoundationLibrary)Native.loadLibrary(
                    "CoreFoundation", CoreFoundationLibrary.class);
            Security = (SecurityLibrary)Native.loadLibrary(
                    "Security", SecurityLibrary.class);
        } catch (UnsatisfiedLinkError ex) {
            throw new BackendNotSupportedException("Failed to load native library");
        }
    }
View Full Code Here


    public static void main(String[] args) {

        //
        // setup a Keyring instance
        //
        Keyring keyring;

        // create an instance of Keyring by invoking Keyring.create method
        //
        // Keyring.create method finds appropriate keyring backend, and sets it up for you.
        // On Mac OS X environment, OS X Keychain is used, and On Windows environment,
        // DPAPI is used for encryption of passwords.
        // If no supported backend is found, BackendNotSupportedException is thrown.
        try {
            keyring = Keyring.create();
        } catch (BackendNotSupportedException ex) {
            Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }

        // some backend directory handles a file to store password to disks.
        // in this case, we must set path to key store file by Keyring.setKeyStorePath
        // before using Keyring.getPassword and Keyring.getPassword.
        if (keyring.isKeyStorePathRequired()) {
            try {
                File keyStoreFile = File.createTempFile("keystore", ".keystore");
                keyring.setKeyStorePath(keyStoreFile.getPath());
            } catch (IOException ex) {
                Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, ex);
            }
        }

        //
        // store password to key store
        //

        // Password can be stored to key store by using Keyring.setPassword method.
        // PasswordSaveException is thrown when some error happened while saving password.
        // LockException is thrown when keyring backend failed to lock key store file.
        try {
            keyring.setPassword("My service name", "My account name", "My password");
        } catch (LockException ex) {
            Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, ex);
            return;
        } catch (PasswordSaveException ex) {
            Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, ex);
            return;
        }

        //
        // Retrieve password from key store
        //

        // Password can be retrieved by using Keyring.getPassword method.
        // PasswordRetrievalException is thrown when some error happened while getting password.
        // LockException is thrown when keyring backend failed to lock key store file.
        try {
            String password = keyring.getPassword("My service name", "My account name");
            System.out.println(password);
        } catch (LockException ex) {
            Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, ex);
        } catch (PasswordRetrievalException ex) {
            Logger.getLogger(Program.class.getName()).log(Level.SEVERE, null, ex);
View Full Code Here

                    return entries.getValue();
                }
            }

            //
            throw new PasswordRetrievalException(
                    "Password related to the specified service and account is not found");
        } // synchronized
    }
View Full Code Here

        try {
            serviceBytes = service.getBytes("UTF-8");
            accountBytes = account.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new PasswordRetrievalException("Unsupported encoding 'UTF-8' specified");
        }

        //
        int[] dataLength = new int[1];
        Pointer[] data = new Pointer[1];

        //
        int status = NativeLibraryManager.Security.SecKeychainFindGenericPassword(
                null, serviceBytes.length, serviceBytes,
                accountBytes.length, accountBytes,
                dataLength, data, null);
        if (status != 0) {
            throw new PasswordRetrievalException(convertErrorCodeToMessage(status));
        }

        //
        byte[] passwordBytes = data[0].getByteArray(0, dataLength[0]);

        //
        NativeLibraryManager.Security.SecKeychainItemFreeContent(null, data[0]);

        //
        try {
            return new String(passwordBytes, "UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new PasswordRetrievalException("Unsupported encoding 'UTF-8' specified");
        }
    }
View Full Code Here

                    break;
                }
            }

            if (targetEntry == null) {
                throw new PasswordRetrievalException(
                        "Password related to the specified service and account is not found");
            }

            //
            byte[] decryptedBytes;

            try {
                decryptedBytes = Crypt32Util.cryptUnprotectData(targetEntry.getPassword());
            } catch (Exception ex) {
                throw new PasswordRetrievalException("Failed to decrypt password");
            }

            //
            try {
                return new String(decryptedBytes, "UTF-8");
            } catch (UnsupportedEncodingException ex) {
                throw new PasswordRetrievalException("Unsupported encoding 'UTF-8' specified");
            }

        } finally {
            try {
                fileLock.release();
View Full Code Here

        try {
            serviceBytes = service.getBytes("UTF-8");
            accountBytes = account.getBytes("UTF-8");
            passwordBytes = password.getBytes("UTF-8");
        } catch (UnsupportedEncodingException ex) {
            throw new PasswordSaveException("Unsupported encoding 'UTF-8' specified");
        }

        //
        Pointer[] itemRef = new Pointer[1];

        //
        int status = NativeLibraryManager.Security.SecKeychainFindGenericPassword(
                    null, serviceBytes.length, serviceBytes,
                    accountBytes.length, accountBytes,
                    null, null, itemRef);

        if (status != SecurityLibrary.ERR_SEC_SUCCESS
                && status != SecurityLibrary.ERR_SEC_ITEM_NOT_FOUND) {
            throw new PasswordSaveException(convertErrorCodeToMessage(status));
        }

        //
        if (itemRef[0] != null) {
            status = NativeLibraryManager.Security.SecKeychainItemModifyContent(
                    itemRef[0], null, passwordBytes.length, passwordBytes);

            // TODO: add code to release itemRef[0]
        } else {
            status = NativeLibraryManager.Security.SecKeychainAddGenericPassword(
                    Pointer.NULL, serviceBytes.length, serviceBytes,
                    accountBytes.length, accountBytes,
                    passwordBytes.length, passwordBytes, null);
        }

        if (status != 0) {
            throw new PasswordSaveException(convertErrorCodeToMessage(status));
        }
    }
View Full Code Here

            byte[] encryptedBytes;

            try {
                encryptedBytes = Crypt32Util.cryptProtectData(password.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException ex) {
                throw new PasswordSaveException("Unsupported encoding 'UTF-8' specified");
            } catch (Exception ex) {
                throw new PasswordSaveException("Failed to encrypt password");
            }

            //
            ArrayList<PasswordEntry> entries = loadPasswordEntries();
            PasswordEntry targetEntry = null;
View Full Code Here

            ObjectOutputStream oout = new ObjectOutputStream(fout);

            oout.writeObject(entries.toArray(new PasswordEntry[0]));
        } catch (Exception ex) {
            Logger.getLogger(WindowsDPAPIBackend.class.getName()).log(Level.SEVERE, null, ex);
            throw new PasswordSaveException("Failed to save password entries to a file");
        }
    }
View Full Code Here

     */
    @Override
    public String getPassword(String service, String account)
            throws LockException, PasswordRetrievalException {

        FileBasedLock fileLock = new FileBasedLock(getLockPath());

        try {
            //
            fileLock.lock();

            //
            PasswordEntry targetEntry = null;

            for (PasswordEntry entry : loadPasswordEntries()) {
                if (entry.getService().equals(service) && entry.getAccount().equals(account)) {
                    targetEntry = entry;
                    break;
                }
            }

            if (targetEntry == null) {
                throw new PasswordRetrievalException(
                        "Password related to the specified service and account is not found");
            }

            //
            byte[] decryptedBytes;

            try {
                decryptedBytes = Crypt32Util.cryptUnprotectData(targetEntry.getPassword());
            } catch (Exception ex) {
                throw new PasswordRetrievalException("Failed to decrypt password");
            }

            //
            try {
                return new String(decryptedBytes, "UTF-8");
            } catch (UnsupportedEncodingException ex) {
                throw new PasswordRetrievalException("Unsupported encoding 'UTF-8' specified");
            }

        } finally {
            try {
                fileLock.release();
            } catch (Exception ex) {
                Logger.getLogger(WindowsDPAPIBackend.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
View Full Code Here

     */
    @Override
    public void setPassword(String service, String account, String password)
            throws LockException, PasswordSaveException {

        FileBasedLock fileLock = new FileBasedLock(getLockPath());

        try {
            //
            fileLock.lock();

            //
            byte[] encryptedBytes;

            try {
                encryptedBytes = Crypt32Util.cryptProtectData(password.getBytes("UTF-8"));
            } catch (UnsupportedEncodingException ex) {
                throw new PasswordSaveException("Unsupported encoding 'UTF-8' specified");
            } catch (Exception ex) {
                throw new PasswordSaveException("Failed to encrypt password");
            }

            //
            ArrayList<PasswordEntry> entries = loadPasswordEntries();
            PasswordEntry targetEntry = null;

            for (PasswordEntry entry : entries) {
                if (entry.getService().equals(service) && entry.getAccount().equals(account)) {
                    targetEntry = entry;
                    break;
                }
            }

            if (targetEntry != null) {
                targetEntry.setPassword(encryptedBytes);
            } else {
                entries.add(new PasswordEntry(service, account, encryptedBytes));
            }

            //
            savePasswordEnetires(entries);
        } finally {
            try {
                fileLock.release();
            } catch (Exception ex) {
                Logger.getLogger(WindowsDPAPIBackend.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }
View Full Code Here

TOP

Related Classes of net.east301.keyring.util.FileBasedLock

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.