jdk/src/java.base/share/classes/java/security/Signer.java
author mullan
Mon, 06 Feb 2017 08:59:00 -0500
changeset 43541 9aeb1de77a63
parent 39337 ac7e4f1d69db
child 45434 4582657c7260
permissions -rw-r--r--
8173827: Remove forRemoval=true from several deprecated security APIs Reviewed-by: redestad, xuelei
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
43541
9aeb1de77a63 8173827: Remove forRemoval=true from several deprecated security APIs
mullan
parents: 39337
diff changeset
     2
 * Copyright (c) 1996, 2017, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * This class is used to represent an Identity that can also digitally
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * sign data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * <p>The management of a signer's private keys is an important and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * sensitive issue that should be handled by subclasses as appropriate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * to their intended use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * @see Identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * @deprecated This class is no longer used. Its functionality has been
43541
9aeb1de77a63 8173827: Remove forRemoval=true from several deprecated security APIs
mullan
parents: 39337
diff changeset
    43
 * replaced by {@code java.security.KeyStore}, the
9aeb1de77a63 8173827: Remove forRemoval=true from several deprecated security APIs
mullan
parents: 39337
diff changeset
    44
 * {@code java.security.cert} package, and
9aeb1de77a63 8173827: Remove forRemoval=true from several deprecated security APIs
mullan
parents: 39337
diff changeset
    45
 * {@code java.security.Principal}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 */
43541
9aeb1de77a63 8173827: Remove forRemoval=true from several deprecated security APIs
mullan
parents: 39337
diff changeset
    47
@Deprecated(since="1.2")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
public abstract class Signer extends Identity {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private static final long serialVersionUID = -1763464102261361480L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * The signer's private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    private PrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * Creates a signer. This constructor should only be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected Signer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     * Creates a signer with the specified identity name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param name the identity name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public Signer(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * Creates a signer with the specified identity name and scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @param name the identity name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * @param scope the scope of the identity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @exception KeyManagementException if there is already an identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * with the same name in the scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public Signer(String name, IdentityScope scope)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        super(name, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Returns this signer's private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
    95
     * <p>First, if there is a security manager, its {@code checkSecurityAccess}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
    96
     * method is called with {@code "getSignerPrivateKey"}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * as its argument to see if it's ok to return the private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @return this signer's private key, or null if the private key has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * not yet been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @exception  SecurityException  if a security manager exists and its
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   103
     * {@code checkSecurityAccess} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * returning the private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see SecurityManager#checkSecurityAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    public PrivateKey getPrivateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        check("getSignerPrivateKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * Sets the key pair (public key and private key) for this signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   116
     * <p>First, if there is a security manager, its {@code checkSecurityAccess}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   117
     * method is called with {@code "setSignerKeyPair"}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     * as its argument to see if it's ok to set the key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * @param pair an initialized key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @exception InvalidParameterException if the key pair is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * properly initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @exception KeyException if the key pair cannot be set for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @exception  SecurityException  if a security manager exists and its
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   127
     * {@code checkSecurityAccess} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * setting the key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * @see SecurityManager#checkSecurityAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    public final void setKeyPair(KeyPair pair)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    throws InvalidParameterException, KeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        check("setSignerKeyPair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        final PublicKey pub = pair.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        PrivateKey priv = pair.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        if (pub == null || priv == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            throw new InvalidParameterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            AccessController.doPrivileged(
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   143
                new PrivilegedExceptionAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
                public Void run() throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                    setPublicKey(pub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            throw (KeyManagementException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        privateKey = priv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    String printKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        String keys = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        PublicKey publicKey = getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        if (publicKey != null && privateKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            keys = "\tpublic and private keys initialized";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            keys = "\tno keys";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
     * Returns a string of information about the signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     * @return a string of information about the signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return "[Signer]" + super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private static void check(String directive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
            security.checkSecurityAccess(directive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
}