KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > security > util > KeyNode


1 /*
2  * JacORB - a free Java ORB
3  *
4  * Copyright (C) 1997-2004 Gerald Brose.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public
17  * License along with this library; if not, write to the Free
18  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

20
21 package org.jacorb.security.util;
22
23 /**
24  * This class represents key store entries
25  *
26  * @author Gerald Brose, FU Berlin
27  * @version $Id: KeyNode.java,v 1.8 2004/05/06 12:40:01 nicolas Exp $
28  */

29
30 import java.security.*;
31 import java.security.cert.*;
32 import java.util.*;
33 import javax.swing.tree.*;
34 import iaik.asn1.*;
35 import iaik.asn1.structures.*;
36 import iaik.x509.*;
37 import iaik.x509.extensions.*;
38
39 public class KeyNode
40     implements KSNode
41 {
42     /** if this entry represents a key entry or a trusted cert entry,
43     this is its alias */

44
45     private String JavaDoc alias;
46     private java.security.PrivateKey JavaDoc key;
47     private char[] password;
48     Vector children; /* certificate Nodes */
49     private String JavaDoc label;
50     private String JavaDoc keytype = "<unknown key type>";
51     private TreeNode root = null;
52     private KeyStore ks;
53
54     private boolean dirty = false;
55
56     /**
57      * constructor for key entries
58      */

59
60     public KeyNode(String JavaDoc alias, java.security.PrivateKey JavaDoc key, char[] password, KeyStore ks)
61     {
62     this.alias = alias;
63     this.key = key;
64     this.password = password;
65     this.ks = ks;
66     children = new Vector();
67     if( key != null )
68     {
69         if( key instanceof java.security.interfaces.RSAPrivateKey JavaDoc ||
70         key instanceof iaik.security.rsa.RSAPrivateKey)
71         keytype = "RSA";
72         else if( key instanceof java.security.interfaces.DSAPrivateKey JavaDoc ||
73              key instanceof iaik.security.dsa.DSAPrivateKey)
74         keytype = "DSA";
75         else
76         keytype = key.getClass().getName();
77     }
78     else
79     {
80         try
81         {
82             java.security.cert.Certificate JavaDoc cert = ks.getCertificate ( alias );
83             java.security.PublicKey JavaDoc pubKey = cert.getPublicKey ();
84             if( pubKey instanceof java.security.interfaces.RSAPublicKey JavaDoc ||
85             pubKey instanceof iaik.security.rsa.RSAPublicKey)
86             keytype = "RSA";
87             else if( pubKey instanceof java.security.interfaces.DSAPublicKey JavaDoc ||
88                  pubKey instanceof iaik.security.dsa.DSAPublicKey)
89             keytype = "DSA";
90             else
91             keytype = pubKey.getClass().getName();
92         }
93         catch ( Exception JavaDoc ex ) {}
94     }
95     label = alias + " (" + keytype + " key)";
96     }
97
98     public iaik.x509.X509Certificate getCert()
99     {
100     return ((CertNode)children.elementAt(0)).getCert();
101     }
102
103     public iaik.x509.X509Certificate[] getCertificateChain()
104     {
105     iaik.x509.X509Certificate[] certs =
106         new iaik.x509.X509Certificate[getChildCount()];
107
108     int ci = 0;
109     for( Enumeration e = children(); e.hasMoreElements(); ci++ )
110     {
111         CertNode certNode = (CertNode)e.nextElement();
112         certs[ci] = certNode.getCert();
113     }
114     return certs;
115     }
116
117
118      /* MutableTreeNode interface: */
119
120     public void insert(MutableTreeNode child, int index)
121     {
122     if( !checkAccess())
123         return;
124
125     children.insertElementAt(child, index);
126     child.setParent( this );
127     dirty = true;
128     }
129
130     public void remove(int index)
131     {
132     if( !checkAccess())
133         return;
134
135     children.removeElementAt(index);
136     dirty = true;
137     }
138
139     public void remove(MutableTreeNode child)
140     {
141     if( !checkAccess())
142         return;
143     
144     children.remove(child);
145     dirty = true;
146     }
147
148
149     public void setParent(MutableTreeNode root)
150     {
151     this.root = root;
152     }
153
154     public void setUserObject(Object JavaDoc o)
155     {}
156
157     /* TreeNode interface: */
158
159     public Enumeration children()
160     {
161     return children.elements();
162     }
163
164     public boolean getAllowsChildren()
165     {
166     return true;
167     }
168
169     public TreeNode getChildAt(int index)
170     {
171     return (TreeNode)children.elementAt(index);
172     }
173
174     public int getChildCount()
175     {
176     return children.size();
177     }
178
179     public int getIndex(TreeNode node)
180     {
181     return children.indexOf(node);
182     }
183
184     public TreeNode getParent()
185     {
186     return root;
187     }
188     
189     public String JavaDoc getAlias()
190     {
191     return alias;
192     }
193
194     private boolean charsEqual ( char [] a1, char [] a2 )
195     {
196         if ( a1.length != a2.length ) return false;
197         else for ( int i = 0; i < a1.length; i++ )
198             if ( a1[ i ] != a2[ i ] ) return false;
199         return true;
200     }
201
202     public void setKey(java.security.PrivateKey JavaDoc key, char[] password) // bnv: added password param
203
{
204     if ( this.password == null )
205         this.key = key;
206     else if ( charsEqual ( this.password, password ))
207     {
208         this.key = key; this.password = password;
209     }
210     else
211         return;
212     if( key != null )
213     {
214         if( key instanceof java.security.interfaces.RSAPrivateKey JavaDoc ||
215         key instanceof iaik.security.rsa.RSAPrivateKey)
216         keytype = "RSA";
217         else if( key instanceof java.security.interfaces.DSAPrivateKey JavaDoc ||
218              key instanceof iaik.security.dsa.DSAPrivateKey)
219         keytype = "DSA";
220         else
221         keytype = key.getClass().getName();
222         label = alias + " (" + keytype + " key)";
223     }
224     }
225
226     public java.security.PrivateKey JavaDoc getKey( char[] password )
227     {
228     // bnv
229
if (( this.password == null ) ||
230         ! ( charsEqual ( this.password, password )))
231     {
232         System.out.println ( "coucou getKey wrong password >" + this.password + "<>" + password + "<" );
233         return null;
234     }
235     else
236         return key;
237     }
238
239     java.security.PrivateKey JavaDoc getKey()
240     throws IllegalAccessException JavaDoc
241     {
242     if( !checkAccess())
243         throw new IllegalAccessException JavaDoc("Wrong password.");
244
245     return key;
246     }
247
248
249     public boolean isLeaf()
250     {
251     return false;
252     }
253
254     /**
255      * @return true if successful
256      */

257
258     public void removeFromParent()
259     {
260     if( !checkAccess())
261         return;
262
263     try
264     {
265         ks.deleteEntry( alias );
266     }
267     catch( java.security.KeyStoreException JavaDoc kse )
268     {
269         kse.printStackTrace();
270     }
271     }
272
273
274     public void store()
275     {
276     if( !dirty || !checkAccess())
277         return;
278     
279     iaik.x509.X509Certificate[] chain =
280         new iaik.x509.X509Certificate[children.size()];
281     
282     for( int i = 0; i < chain.length; i++)
283     {
284         chain[i] = ((CertNode)children.elementAt(i)).getCert();
285     }
286
287     try
288     {
289         ks.setKeyEntry( alias, key, password, chain );
290         dirty = false;
291     }
292     catch( KeyStoreException kse )
293     {
294         kse.printStackTrace();
295     }
296     }
297
298     public boolean checkAccess()
299     {
300     if( key != null && password != null )
301         return true;
302
303     try
304     {
305         password = UserSponsor.getPasswd("Please enter password for key alias " + alias );
306         if( password != null )
307         {
308         key = (java.security.PrivateKey JavaDoc)ks.getKey( alias, password );
309         return true;
310         }
311         else
312         return false;
313     }
314     catch( java.security.UnrecoverableKeyException JavaDoc kse )
315     {
316         kse.printStackTrace();
317         return false;
318     }
319     catch(java.security.NoSuchAlgorithmException JavaDoc kse )
320     {
321         kse.printStackTrace();
322         return false;
323     }
324     catch( java.security.KeyStoreException JavaDoc kse )
325     {
326         kse.printStackTrace();
327         return false;
328     }
329     }
330
331
332     public String JavaDoc toString()
333     {
334     if( label.indexOf("(") > 0 )
335     {
336         if( key != null )
337         {
338         if( key instanceof java.security.interfaces.RSAPrivateKey JavaDoc ||
339             key instanceof iaik.security.rsa.RSAPrivateKey)
340             keytype = "RSA";
341         else if( key instanceof java.security.interfaces.DSAPrivateKey JavaDoc ||
342              key instanceof iaik.security.dsa.DSAPrivateKey)
343             keytype = "DSA";
344         else
345             keytype = key.getClass().getName();
346         }
347         label = alias + " (" + keytype + " key)";
348     }
349     return label;
350     }
351
352 }
353
354
355
356
357
358
359
360
361
362
363
364
365
366
Popular Tags