src/java.base/share/classes/java/security/Signer.java
author erikj
Tue, 12 Sep 2017 19:03:39 +0200
changeset 47216 71c04702a3d5
parent 45434 jdk/src/java.base/share/classes/java/security/Signer.java@4582657c7260
child 47742 28096878fae5
permissions -rw-r--r--
8187443: Forest Consolidation: Move files to unified layout Reviewed-by: darcy, ihse
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
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 43541
diff changeset
    41
 * @since 1.1
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @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
    44
 * replaced by {@code java.security.KeyStore}, the
9aeb1de77a63 8173827: Remove forRemoval=true from several deprecated security APIs
mullan
parents: 39337
diff changeset
    45
 * {@code java.security.cert} package, and
9aeb1de77a63 8173827: Remove forRemoval=true from several deprecated security APIs
mullan
parents: 39337
diff changeset
    46
 * {@code java.security.Principal}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 */
43541
9aeb1de77a63 8173827: Remove forRemoval=true from several deprecated security APIs
mullan
parents: 39337
diff changeset
    48
@Deprecated(since="1.2")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
public abstract class Signer extends Identity {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    private static final long serialVersionUID = -1763464102261361480L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The signer's private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * @serial
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private PrivateKey privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Creates a signer. This constructor should only be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    protected Signer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        super();
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
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * Creates a signer with the specified identity name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @param name the identity name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public Signer(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        super(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Creates a signer with the specified identity name and scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * @param name the identity name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     * @param scope the scope of the identity.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @exception KeyManagementException if there is already an identity
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     * with the same name in the scope.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    public Signer(String name, IdentityScope scope)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        super(name, scope);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Returns this signer's private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
    96
     * <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
    97
     * method is called with {@code "getSignerPrivateKey"}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * as its argument to see if it's ok to return the private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return this signer's private key, or null if the private key has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * not yet been set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @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
   104
     * {@code checkSecurityAccess} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * returning the private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @see SecurityManager#checkSecurityAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    public PrivateKey getPrivateKey() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        check("getSignerPrivateKey");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        return privateKey;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
   /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * Sets the key pair (public key and private key) for this signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   117
     * <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
   118
     * method is called with {@code "setSignerKeyPair"}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * as its argument to see if it's ok to set the key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     * @param pair an initialized key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @exception InvalidParameterException if the key pair is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * properly initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * @exception KeyException if the key pair cannot be set for any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * other reason.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * @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
   128
     * {@code checkSecurityAccess} method doesn't allow
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * setting the key pair.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * @see SecurityManager#checkSecurityAccess
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    public final void setKeyPair(KeyPair pair)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    throws InvalidParameterException, KeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        check("setSignerKeyPair");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        final PublicKey pub = pair.getPublic();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        PrivateKey priv = pair.getPrivate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        if (pub == null || priv == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            throw new InvalidParameterException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            AccessController.doPrivileged(
30033
b9c86c17164a 8078468: Update security libraries to use diamond with anonymous classes
darcy
parents: 25859
diff changeset
   144
                new PrivilegedExceptionAction<>() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
                public Void run() throws KeyManagementException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                    setPublicKey(pub);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
                    return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        } catch (PrivilegedActionException pae) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            throw (KeyManagementException) pae.getException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        privateKey = priv;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    String printKeys() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        String keys = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        PublicKey publicKey = getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        if (publicKey != null && privateKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            keys = "\tpublic and private keys initialized";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            keys = "\tno keys";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        return keys;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * Returns a string of information about the signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @return a string of information about the signer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        return "[Signer]" + super.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    private static void check(String directive) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        SecurityManager security = System.getSecurityManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        if (security != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
            security.checkSecurityAccess(directive);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
}