001 /*
002 * Copyright 1996-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.security;
027
028 import java.io.*;
029
030 /**
031 * This class is used to represent an Identity that can also digitally
032 * sign data.
033 *
034 * <p>The management of a signer's private keys is an important and
035 * sensitive issue that should be handled by subclasses as appropriate
036 * to their intended use.
037 *
038 * @see Identity
039 *
040 * @version 1.50 07/05/05
041 * @author Benjamin Renaud
042 *
043 * @deprecated This class is no longer used. Its functionality has been
044 * replaced by <code>java.security.KeyStore</code>, the
045 * <code>java.security.cert</code> package, and
046 * <code>java.security.Principal</code>.
047 */
048 @Deprecated
049 public abstract class Signer extends Identity {
050
051 private static final long serialVersionUID = -1763464102261361480L;
052
053 /**
054 * The signer's private key.
055 *
056 * @serial
057 */
058 private PrivateKey privateKey;
059
060 /**
061 * Creates a signer. This constructor should only be used for
062 * serialization.
063 */
064 protected Signer() {
065 super ();
066 }
067
068 /**
069 * Creates a signer with the specified identity name.
070 *
071 * @param name the identity name.
072 */
073 public Signer(String name) {
074 super (name);
075 }
076
077 /**
078 * Creates a signer with the specified identity name and scope.
079 *
080 * @param name the identity name.
081 *
082 * @param scope the scope of the identity.
083 *
084 * @exception KeyManagementException if there is already an identity
085 * with the same name in the scope.
086 */
087 public Signer(String name, IdentityScope scope)
088 throws KeyManagementException {
089 super (name, scope);
090 }
091
092 /**
093 * Returns this signer's private key.
094 *
095 * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
096 * method is called with <code>"getSignerPrivateKey"</code>
097 * as its argument to see if it's ok to return the private key.
098 *
099 * @return this signer's private key, or null if the private key has
100 * not yet been set.
101 *
102 * @exception SecurityException if a security manager exists and its
103 * <code>checkSecurityAccess</code> method doesn't allow
104 * returning the private key.
105 *
106 * @see SecurityManager#checkSecurityAccess
107 */
108 public PrivateKey getPrivateKey() {
109 check("getSignerPrivateKey");
110 return privateKey;
111 }
112
113 /**
114 * Sets the key pair (public key and private key) for this signer.
115 *
116 * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
117 * method is called with <code>"setSignerKeyPair"</code>
118 * as its argument to see if it's ok to set the key pair.
119 *
120 * @param pair an initialized key pair.
121 *
122 * @exception InvalidParameterException if the key pair is not
123 * properly initialized.
124 * @exception KeyException if the key pair cannot be set for any
125 * other reason.
126 * @exception SecurityException if a security manager exists and its
127 * <code>checkSecurityAccess</code> method doesn't allow
128 * setting the key pair.
129 *
130 * @see SecurityManager#checkSecurityAccess
131 */
132 public final void setKeyPair(KeyPair pair)
133 throws InvalidParameterException, KeyException {
134 check("setSignerKeyPair");
135 final PublicKey pub = pair.getPublic();
136 PrivateKey priv = pair.getPrivate();
137
138 if (pub == null || priv == null) {
139 throw new InvalidParameterException();
140 }
141 try {
142 AccessController
143 .doPrivileged(new PrivilegedExceptionAction<Void>() {
144 public Void run() throws KeyManagementException {
145 setPublicKey(pub);
146 return null;
147 }
148 });
149 } catch (PrivilegedActionException pae) {
150 throw (KeyManagementException) pae.getException();
151 }
152 privateKey = priv;
153 }
154
155 String printKeys() {
156 String keys = "";
157 PublicKey publicKey = getPublicKey();
158 if (publicKey != null && privateKey != null) {
159 keys = "\tpublic and private keys initialized";
160
161 } else {
162 keys = "\tno keys";
163 }
164 return keys;
165 }
166
167 /**
168 * Returns a string of information about the signer.
169 *
170 * @return a string of information about the signer.
171 */
172 public String toString() {
173 return "[Signer]" + super .toString();
174 }
175
176 private static void check(String directive) {
177 SecurityManager security = System.getSecurityManager();
178 if (security != null) {
179 security.checkSecurityAccess(directive);
180 }
181 }
182
183 }
|