source: osm/applications/editors/josm/plugins/native-password-manager/src/org/netbeans/modules/keyring/fallback/FallbackProvider.java@ 26335

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

new plugin: native password manager

File size: 5.2 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2010 Oracle and/or its affiliates. All rights reserved.
5 *
6 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7 * Other names may be trademarks of their respective owners.
8 *
9 * The contents of this file are subject to the terms of either the GNU
10 * General Public License Version 2 only ("GPL") or the Common
11 * Development and Distribution License("CDDL") (collectively, the
12 * "License"). You may not use this file except in compliance with the
13 * License. You can obtain a copy of the License at
14 * http://www.netbeans.org/cddl-gplv2.html
15 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16 * specific language governing permissions and limitations under the
17 * License. When distributing the software, include this License Header
18 * Notice in each file and include the License file at
19 * nbbuild/licenses/CDDL-GPL-2-CP. Oracle designates this
20 * particular file as subject to the "Classpath" exception as provided
21 * by Oracle in the GPL Version 2 section of the License file that
22 * accompanied this code. If applicable, add the following below the
23 * License Header, with the fields enclosed by brackets [] replaced by
24 * your own identifying information:
25 * "Portions Copyrighted [year] [name of copyright owner]"
26 *
27 * If you wish your version of this file to be governed by only the CDDL
28 * or only the GPL Version 2, indicate your decision by adding
29 * "[Contributor] elects to include this software in this distribution
30 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31 * single choice of license, a recipient has the option to distribute
32 * your version of this file under either the CDDL, the GPL Version 2 or
33 * to extend the choice of license to its licensees as provided above.
34 * However, if you add GPL Version 2 code and therefore, elected the GPL
35 * Version 2 license, then the option applies only if the new code is
36 * made subject to such option by the copyright holder.
37 *
38 * Contributor(s):
39 *
40 * Portions Copyrighted 2009 Sun Microsystems, Inc.
41 */
42
43package org.netbeans.modules.keyring.fallback;
44
45import java.util.UUID;
46import java.util.logging.Level;
47import java.util.logging.Logger;
48import org.netbeans.modules.keyring.impl.Utils;
49import org.netbeans.modules.keyring.spi.EncryptionProvider;
50import org.netbeans.spi.keyring.KeyringProvider;
51
52/**
53 * Platform-independent keyring provider using a master password and the user directory.
54 */
55public class FallbackProvider implements KeyringProvider {
56
57 private static final Logger LOG = Logger.getLogger(FallbackProvider.class.getName());
58 private static final String DESCRIPTION = ".description";
59 private static final String SAMPLE_KEY = "__sample__";
60
61 private EncryptionProvider encryption;
62 private IPreferences pre;
63
64 // simple interface for a generic preferences store
65 public interface IPreferences {
66 String get(String key, String def);
67 void put(String key, String val);
68 void remove(String key);
69 }
70
71 public FallbackProvider(EncryptionProvider encryption, IPreferences pref) {
72 this.encryption = encryption;
73 this.pre = pref;
74 }
75
76 public boolean enabled() {
77 if (encryption.enabled()) {
78 if (testSampleKey()) {
79 LOG.log(Level.FINE, "Using provider: {0}", encryption);
80 return true;
81 }
82 }
83 LOG.fine("No provider");
84 return false;
85 }
86
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;
93 } else {
94 LOG.fine("could not save sample key");
95 return false;
96 }
97 }
98
99 public char[] read(String key) {
100 String ciphertext_string = pre.get(key, null);
101 byte[] ciphertext = ciphertext_string == null ? null : Utils.chars2Bytes(ciphertext_string.toCharArray());
102 if (ciphertext == null) {
103 return null;
104 }
105 try {
106 return encryption.decrypt(ciphertext);
107 } catch (Exception x) {
108 LOG.log(Level.FINE, "failed to decrypt password for " + key, x);
109 }
110 return null;
111 }
112
113 public void save(String key, char[] password, String description) {
114 _save(key, password, description);
115 }
116 private boolean _save(String key, char[] password, String description) {
117 try {
118 byte[] encryptedPasswordByteArray = encryption.encrypt(password);
119 String encryptedPassword = encryptedPasswordByteArray == null ? null : String.valueOf(Utils.bytes2Chars(encryptedPasswordByteArray));
120 pre.put(key, encryptedPassword);
121 } catch (Exception x) {
122 LOG.log(Level.FINE, "failed to encrypt password for " + key, x);
123 return false;
124 }
125 if (description != null) {
126 // Preferences interface gives no access to *.properties comments, so:
127 pre.put(key + DESCRIPTION, description);
128 }
129 return true;
130 }
131
132 public void delete(String key) {
133 pre.remove(key);
134 pre.remove(key + DESCRIPTION);
135 }
136
137}
Note: See TracBrowser for help on using the repository browser.