1 /*
2  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 
26 package java.security;
27 
28 import java.io.*;
29 
30 /**
31  * This class is used to represent an Identity that can also digitally
32  * sign data.
33  *
34  * <p>The management of a signer's private keys is an important and
35  * sensitive issue that should be handled by subclasses as appropriate
36  * to their intended use.
37  *
38  * @see Identity
39  *
40  * @author Benjamin Renaud
41  *
42  * @deprecated This class is no longer used. Its functionality has been
43  * replaced by {@code java.security.KeyStore}, the
44  * {@code java.security.cert} package, and
45  * {@code java.security.Principal}.
46  */
47 @Deprecated
48 public abstract class Signer extends Identity {
49 
50     private static final long serialVersionUID = -1763464102261361480L;
51 
52     /**
53      * The signer's private key.
54      *
55      * @serial
56      */
57     private PrivateKey privateKey;
58 
59     /**
60      * Creates a signer. This constructor should only be used for
61      * serialization.
62      */
Signer()63     protected Signer() {
64         super();
65     }
66 
67 
68     /**
69      * Creates a signer with the specified identity name.
70      *
71      * @param name the identity name.
72      */
Signer(String name)73     public Signer(String name) {
74         super(name);
75     }
76 
77     /**
78      * Creates a signer with the specified identity name and scope.
79      *
80      * @param name the identity name.
81      *
82      * @param scope the scope of the identity.
83      *
84      * @exception KeyManagementException if there is already an identity
85      * with the same name in the scope.
86      */
Signer(String name, IdentityScope scope)87     public Signer(String name, IdentityScope scope)
88     throws KeyManagementException {
89         super(name, scope);
90     }
91 
92     /**
93      * Returns this signer's private key.
94      *
95      * <p>First, if there is a security manager, its {@code checkSecurityAccess}
96      * method is called with {@code "getSignerPrivateKey"}
97      * as its argument to see if it's ok to return the private key.
98      *
99      * @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} method doesn't allow
104      * returning the private key.
105      *
106      * @see SecurityManager#checkSecurityAccess
107      */
getPrivateKey()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}
117      * method is called with {@code "setSignerKeyPair"}
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} method doesn't allow
128      * setting the key pair.
129      *
130      * @see SecurityManager#checkSecurityAccess
131      */
setKeyPair(KeyPair pair)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.doPrivileged(
143                 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 
printKeys()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      */
toString()172     public String toString() {
173         return "[Signer]" + super.toString();
174     }
175 
check(String directive)176     private static void check(String directive) {
177         SecurityManager security = System.getSecurityManager();
178         if (security != null) {
179             security.checkSecurityAccess(directive);
180         }
181     }
182 
183 }
184