KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > security > Signer


1 /*
2  * @(#)Signer.java 1.42 04/05/18
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.security;
9
10 import java.io.*;
11
12 /**
13  * This class is used to represent an Identity that can also digitally
14  * sign data.
15  *
16  * <p>The management of a signer's private keys is an important and
17  * sensitive issue that should be handled by subclasses as appropriate
18  * to their intended use.
19  *
20  * @see Identity
21  *
22  * @version 1.42 04/05/18
23  * @author Benjamin Renaud
24  *
25  * @deprecated This class is no longer used. Its functionality has been
26  * replaced by <code>java.security.KeyStore</code>, the
27  * <code>java.security.cert</code> package, and
28  * <code>java.security.Principal</code>.
29  */

30 @Deprecated JavaDoc
31 public abstract class Signer extends Identity JavaDoc {
32
33     private static final long serialVersionUID = -1763464102261361480L;
34
35     /**
36      * The signer's private key.
37      *
38      * @serial
39      */

40     private PrivateKey JavaDoc privateKey;
41
42     /**
43      * Creates a signer. This constructor should only be used for
44      * serialization.
45      */

46     protected Signer() {
47     super();
48     }
49
50
51     /**
52      * Creates a signer with the specified identity name.
53      *
54      * @param name the identity name.
55      */

56     public Signer(String JavaDoc name) {
57     super(name);
58     }
59
60     /**
61      * Creates a signer with the specified identity name and scope.
62      *
63      * @param name the identity name.
64      *
65      * @param scope the scope of the identity.
66      *
67      * @exception KeyManagementException if there is already an identity
68      * with the same name in the scope.
69      */

70     public Signer(String JavaDoc name, IdentityScope JavaDoc scope)
71     throws KeyManagementException JavaDoc {
72     super(name, scope);
73     }
74
75     /**
76      * Returns this signer's private key.
77      *
78      * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
79      * method is called with <code>"getSignerPrivateKey"</code>
80      * as its argument to see if it's ok to return the private key.
81      *
82      * @return this signer's private key, or null if the private key has
83      * not yet been set.
84      *
85      * @exception SecurityException if a security manager exists and its
86      * <code>checkSecurityAccess</code> method doesn't allow
87      * returning the private key.
88      *
89      * @see SecurityManager#checkSecurityAccess
90      */

91     public PrivateKey JavaDoc getPrivateKey() {
92     check("getSignerPrivateKey");
93     return privateKey;
94     }
95
96    /**
97      * Sets the key pair (public key and private key) for this signer.
98      *
99      * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
100      * method is called with <code>"setSignerKeyPair"</code>
101      * as its argument to see if it's ok to set the key pair.
102      *
103      * @param pair an initialized key pair.
104      *
105      * @exception InvalidParameterException if the key pair is not
106      * properly initialized.
107      * @exception KeyException if the key pair cannot be set for any
108      * other reason.
109      * @exception SecurityException if a security manager exists and its
110      * <code>checkSecurityAccess</code> method doesn't allow
111      * setting the key pair.
112      *
113      * @see SecurityManager#checkSecurityAccess
114      */

115     public final void setKeyPair(KeyPair JavaDoc pair)
116     throws InvalidParameterException JavaDoc, KeyException JavaDoc {
117     check("setSignerKeyPair");
118     final PublicKey JavaDoc pub = pair.getPublic();
119     PrivateKey JavaDoc priv = pair.getPrivate();
120
121     if (pub == null || priv == null) {
122         throw new InvalidParameterException JavaDoc();
123     }
124     try {
125         AccessController.doPrivileged(new PrivilegedExceptionAction JavaDoc() {
126         public Object JavaDoc run() throws KeyManagementException JavaDoc {
127             setPublicKey(pub);
128             return null;
129         }
130         });
131     } catch (PrivilegedActionException JavaDoc pae) {
132         throw (KeyManagementException JavaDoc) pae.getException();
133     }
134     privateKey = priv;
135     }
136
137     String JavaDoc printKeys() {
138     String JavaDoc keys = "";
139     PublicKey JavaDoc publicKey = getPublicKey();
140     if (publicKey != null && privateKey != null) {
141         keys = "\tpublic and private keys initialized";
142
143     } else {
144         keys = "\tno keys";
145     }
146     return keys;
147     }
148
149     /**
150      * Returns a string of information about the signer.
151      *
152      * @return a string of information about the signer.
153      */

154     public String JavaDoc toString() {
155     return "[Signer]" + super.toString();
156     }
157
158     private static void check(String JavaDoc directive) {
159     SecurityManager JavaDoc security = System.getSecurityManager();
160     if (security != null) {
161         security.checkSecurityAccess(directive);
162     }
163     }
164
165 }
166
167
Popular Tags