0N/A/*
2362N/A * Copyright (c) 1996, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage java.security;
0N/A
0N/Aimport java.io.*;
0N/A
0N/A/**
0N/A * This class is used to represent an Identity that can also digitally
0N/A * sign data.
0N/A *
0N/A * <p>The management of a signer's private keys is an important and
0N/A * sensitive issue that should be handled by subclasses as appropriate
0N/A * to their intended use.
0N/A *
0N/A * @see Identity
0N/A *
0N/A * @author Benjamin Renaud
0N/A *
0N/A * @deprecated This class is no longer used. Its functionality has been
0N/A * replaced by <code>java.security.KeyStore</code>, the
0N/A * <code>java.security.cert</code> package, and
0N/A * <code>java.security.Principal</code>.
0N/A */
0N/A@Deprecated
0N/Apublic abstract class Signer extends Identity {
0N/A
0N/A private static final long serialVersionUID = -1763464102261361480L;
0N/A
0N/A /**
0N/A * The signer's private key.
0N/A *
0N/A * @serial
0N/A */
0N/A private PrivateKey privateKey;
0N/A
0N/A /**
0N/A * Creates a signer. This constructor should only be used for
0N/A * serialization.
0N/A */
0N/A protected Signer() {
0N/A super();
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Creates a signer with the specified identity name.
0N/A *
0N/A * @param name the identity name.
0N/A */
0N/A public Signer(String name) {
0N/A super(name);
0N/A }
0N/A
0N/A /**
0N/A * Creates a signer with the specified identity name and scope.
0N/A *
0N/A * @param name the identity name.
0N/A *
0N/A * @param scope the scope of the identity.
0N/A *
0N/A * @exception KeyManagementException if there is already an identity
0N/A * with the same name in the scope.
0N/A */
0N/A public Signer(String name, IdentityScope scope)
0N/A throws KeyManagementException {
0N/A super(name, scope);
0N/A }
0N/A
0N/A /**
0N/A * Returns this signer's private key.
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
0N/A * method is called with <code>"getSignerPrivateKey"</code>
0N/A * as its argument to see if it's ok to return the private key.
0N/A *
0N/A * @return this signer's private key, or null if the private key has
0N/A * not yet been set.
0N/A *
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkSecurityAccess</code> method doesn't allow
0N/A * returning the private key.
0N/A *
0N/A * @see SecurityManager#checkSecurityAccess
0N/A */
0N/A public PrivateKey getPrivateKey() {
0N/A check("getSignerPrivateKey");
0N/A return privateKey;
0N/A }
0N/A
0N/A /**
0N/A * Sets the key pair (public key and private key) for this signer.
0N/A *
0N/A * <p>First, if there is a security manager, its <code>checkSecurityAccess</code>
0N/A * method is called with <code>"setSignerKeyPair"</code>
0N/A * as its argument to see if it's ok to set the key pair.
0N/A *
0N/A * @param pair an initialized key pair.
0N/A *
0N/A * @exception InvalidParameterException if the key pair is not
0N/A * properly initialized.
0N/A * @exception KeyException if the key pair cannot be set for any
0N/A * other reason.
0N/A * @exception SecurityException if a security manager exists and its
0N/A * <code>checkSecurityAccess</code> method doesn't allow
0N/A * setting the key pair.
0N/A *
0N/A * @see SecurityManager#checkSecurityAccess
0N/A */
0N/A public final void setKeyPair(KeyPair pair)
0N/A throws InvalidParameterException, KeyException {
0N/A check("setSignerKeyPair");
0N/A final PublicKey pub = pair.getPublic();
0N/A PrivateKey priv = pair.getPrivate();
0N/A
0N/A if (pub == null || priv == null) {
0N/A throw new InvalidParameterException();
0N/A }
0N/A try {
0N/A AccessController.doPrivileged(
0N/A new PrivilegedExceptionAction<Void>() {
0N/A public Void run() throws KeyManagementException {
0N/A setPublicKey(pub);
0N/A return null;
0N/A }
0N/A });
0N/A } catch (PrivilegedActionException pae) {
0N/A throw (KeyManagementException) pae.getException();
0N/A }
0N/A privateKey = priv;
0N/A }
0N/A
0N/A String printKeys() {
0N/A String keys = "";
0N/A PublicKey publicKey = getPublicKey();
0N/A if (publicKey != null && privateKey != null) {
0N/A keys = "\tpublic and private keys initialized";
0N/A
0N/A } else {
0N/A keys = "\tno keys";
0N/A }
0N/A return keys;
0N/A }
0N/A
0N/A /**
0N/A * Returns a string of information about the signer.
0N/A *
0N/A * @return a string of information about the signer.
0N/A */
0N/A public String toString() {
0N/A return "[Signer]" + super.toString();
0N/A }
0N/A
0N/A private static void check(String directive) {
0N/A SecurityManager security = System.getSecurityManager();
0N/A if (security != null) {
0N/A security.checkSecurityAccess(directive);
0N/A }
0N/A }
0N/A
0N/A}