1 package org.neuclear.commons.crypto.signers;
2
3 /*
4 * The NeuClear Project and it's libraries are
5 * (c) 2002-2004 Antilles Software Ventures SA
6 * For more information see: http://neuclear.org
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 */
22
23 import org.neuclear.commons.LowLevelException;
24 import org.neuclear.commons.crypto.CryptoException;
25 import org.neuclear.commons.crypto.passphraseagents.InteractiveAgent;
26 import org.neuclear.commons.crypto.passphraseagents.UserCancellationException;
27 import org.neuclear.commons.crypto.passphraseagents.swing.SwingAgent;
28
29 import javax.swing.*;
30 import javax.swing.event.EventListenerList;
31 import javax.swing.event.ListDataEvent;
32 import javax.swing.event.ListDataListener;
33 import java.io.IOException;
34 import java.security.KeyStoreException;
35 import java.security.PublicKey;
36 import java.util.ArrayList;
37 import java.util.Iterator;
38 import java.util.prefs.Preferences;
39
40 /***
41 * User: pelleb
42 * Date: May 13, 2004
43 * Time: 9:12:42 AM
44 */
45 public class PersonalSigner implements BrowsableSigner, ComboBoxModel {
46 public PersonalSigner(JFrame frame) {
47 // signer=null;
48 this.frame = frame;
49 this.listeners = new EventListenerList();
50 this.list = new ArrayList();
51 agent = new SwingAgent(this);
52 this.dia = new OpenSignerDialog(frame, agent);
53 prefs = Preferences.userNodeForPackage(PersonalSigner.class);
54
55 }
56
57 public final byte[] sign(final String name, final byte[] data) throws NonExistingSignerException, UserCancellationException {
58 openIfNecessary();
59 return signer.sign(name, data);
60 }
61
62 private void openIfNecessary() throws UserCancellationException {
63 if (signer == null)
64 open();
65 }
66
67 public void open() throws UserCancellationException {
68 signer = dia.openSigner();
69 updateList();
70 fireListUpdated();
71 }
72
73
74 public final boolean canSignFor(final String name) {
75 return signer.canSignFor(name);
76 }
77
78 public final int getKeyType(final String name) {
79 return signer.getKeyType(name);
80 }
81
82 public final PublicKey generateKey(final String alias) throws UserCancellationException {
83 openIfNecessary();
84 return signer.generateKey(alias);
85 }
86
87 public PublicKey generateKey() throws UserCancellationException {
88 return signer.generateKey();
89 }
90
91 public final PublicKey getPublicKey(final String name) throws NonExistingSignerException {
92 return signer.getPublicKey(name);
93 }
94
95 public byte[] sign(byte data[], SetPublicKeyCallBack callback) throws UserCancellationException {
96 openIfNecessary();
97 return ((InteractiveAgent) agent).sign(this, data, callback);
98 }
99
100 public byte[] sign(String name, char pass[], byte data[], SetPublicKeyCallBack callback) throws InvalidPassphraseException {
101 return signer.sign(name, pass, data, callback);
102 }
103
104 public void createKeyPair(String alias, char passphrase[]) throws CryptoException {
105 signer.createKeyPair(alias, passphrase);
106 updateList();
107 fireListUpdated();
108 try {
109 save(true);
110 } catch (IOException e) {
111 e.printStackTrace();
112 } catch (UserCancellationException e) {
113 e.printStackTrace();
114 }
115 }
116
117 public void save() {
118 try {
119 save(false);
120 } catch (IOException e) {
121 throw new LowLevelException(e);
122 } catch (UserCancellationException e) {
123 throw new LowLevelException(e);
124 }
125 }
126
127 public void save(final boolean force) throws IOException, UserCancellationException {
128 if (signer != null)
129 dia.save((JCESigner) signer, force);
130 }
131
132 public boolean isOpen() {
133 return signer != null;
134 }
135
136 public Iterator iterator() throws KeyStoreException {
137 if (signer == null)
138 return new Iterator() {
139 public void remove() {
140
141 }
142
143 public boolean hasNext() {
144 return false;
145 }
146
147 public Object next() {
148 return null;
149 }
150
151 };
152 return signer.iterator();
153 }
154
155 private void updateList() {
156 try {
157 Iterator iter = iterator();
158 list = new ArrayList();
159 while (iter.hasNext()) {
160 Object o = (Object) iter.next();
161 list.add(o);
162 }
163 } catch (KeyStoreException e) {
164 ;
165 }
166 }
167
168 /***
169 * Returns the length of the list.
170 *
171 * @return the length of the list
172 */
173 public int getSize() {
174 return list.size();
175 }
176
177 /***
178 * Returns the value at the specified index.
179 *
180 * @param index the requested index
181 * @return the value at <code>index</code>
182 */
183 public Object getElementAt(int index) {
184 return list.get(index);
185 }
186
187 /***
188 * Adds a listener to the list that's notified each time a change
189 * to the data model occurs.
190 *
191 * @param l the <code>ListDataListener</code> to be added
192 */
193 public void addListDataListener(ListDataListener l) {
194 listeners.add(ListDataListener.class, l);
195 }
196
197 /***
198 * Removes a listener from the list that's notified each time a
199 * change to the data model occurs.
200 *
201 * @param l the <code>ListDataListener</code> to be removed
202 */
203 public void removeListDataListener(ListDataListener l) {
204 listeners.remove(ListDataListener.class, l);
205 }
206
207 protected void fireListUpdated() {
208 // Guaranteed to return a non-null array
209 Object[] listenerArray = listeners.getListenerList();
210 // Process the listeners last to first, notifying
211 // those that are interested in this event
212 ListDataEvent event = null;
213 for (int i = listenerArray.length - 1; i >= 0; i--) {
214 if (listenerArray[i] instanceof ListDataListener) {
215 // Lazily create the event:
216 if (event == null)
217 event = new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, 0, getSize());
218 ((ListDataListener) listenerArray[i]).contentsChanged(event);
219 }
220 }
221 }
222
223 public Object getSelectedItem() {
224 return selected;
225 }
226
227 public void setSelectedItem(Object anItem) {
228 selected = anItem;
229 }
230
231 private Object selected;
232
233 private OpenSignerDialog dia;
234 private BrowsableSigner signer;
235 private JFrame frame;
236 private ArrayList list;
237 private EventListenerList listeners;
238 private String filename;
239 private final SwingAgent agent;
240 private final Preferences prefs;
241 }
This page was automatically generated by Maven