jdk/src/java.base/share/classes/java/security/Signature.java
author vinnie
Thu, 25 Sep 2014 12:24:19 +0100
changeset 26736 5a93000b26cd
parent 25859 3317bb8137f4
child 27077 c943ca4a3380
permissions -rw-r--r--
8056026: Debug security logging should print Provider used for each crypto operation Reviewed-by: mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
     2
 * Copyright (c) 1996, 2014, 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.security.spec.AlgorithmParameterSpec;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.util.concurrent.ConcurrentHashMap;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.cert.Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.cert.X509Certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import javax.crypto.Cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
import javax.crypto.CipherSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.crypto.IllegalBlockSizeException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.crypto.BadPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.crypto.NoSuchPaddingException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
/**
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
    50
 * The Signature class is used to provide applications the functionality
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * of a digital signature algorithm. Digital signatures are used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * authentication and integrity assurance of digital data.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <p> The signature algorithm can be, among others, the NIST standard
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * DSA, using DSA and SHA-1. The DSA algorithm using the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
    56
 * SHA-1 message digest algorithm can be specified as {@code SHA1withDSA}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * In the case of RSA, there are multiple choices for the message digest
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 * algorithm, so the signing algorithm could be specified as, for example,
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
    59
 * {@code MD2withRSA}, {@code MD5withRSA}, or {@code SHA1withRSA}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * The algorithm name must be specified, as there is no default.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 * <p> A Signature object can be used to generate and verify digital
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 * signatures.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 * <p> There are three phases to the use of a Signature object for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 * either signing data or verifying a signature:<ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <li>Initialization, with either
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *     <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *     <li>a public key, which initializes the signature for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *     verification (see {@link #initVerify(PublicKey) initVerify}), or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *     <li>a private key (and optionally a Secure Random Number Generator),
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     which initializes the signature for signing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     (see {@link #initSign(PrivateKey)}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 *     and {@link #initSign(PrivateKey, SecureRandom)}).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18579
diff changeset
    80
 *     </ul>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18579
diff changeset
    82
 * <li>Updating
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * <p>Depending on the type of initialization, this will update the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * bytes to be signed or verified. See the
21334
c60dfce46a77 8026982: javadoc errors in core libs
rriggs
parents: 18579
diff changeset
    86
 * {@link #update(byte) update} methods.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 * <li>Signing or Verifying a signature on all updated bytes. See the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
 * {@link #sign() sign} methods and the {@link #verify(byte[]) verify}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
 * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
 * </ol>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
 * <p>Note that this class is abstract and extends from
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
    95
 * {@code SignatureSpi} for historical reasons.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
 * Application developers should only take notice of the methods defined in
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
    97
 * this {@code Signature} class; all the methods in
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
 * the superclass are intended for cryptographic service providers who wish to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
 * supply their own implementations of digital signature algorithms.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
 *
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   101
 * <p> Every implementation of the Java platform is required to support the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   102
 * following standard {@code Signature} algorithms:
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   103
 * <ul>
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   104
 * <li>{@code SHA1withDSA}</li>
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   105
 * <li>{@code SHA1withRSA}</li>
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   106
 * <li>{@code SHA256withRSA}</li>
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   107
 * </ul>
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   108
 * These algorithms are described in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   109
 * "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   110
 * Signature section</a> of the
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   111
 * Java Cryptography Architecture Standard Algorithm Name Documentation.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   112
 * Consult the release documentation for your implementation to see if any
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   113
 * other algorithms are supported.
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   114
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
 * @author Benjamin Renaud
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
public abstract class Signature extends SignatureSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    private static final Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
                        Debug.getInstance("jca", "Signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   124
    private static final Debug pdebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   125
                        Debug.getInstance("provider", "Provider");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   126
    private static final boolean skipDebug =
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   127
        Debug.isOn("engine=") && !Debug.isOn("signature");
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   128
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * The algorithm for this signature object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * This value is used to map an OID to the particular algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     * The mapping is done in AlgorithmObject.algOID(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    private String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * Possible {@link #state} value, signifying that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * this signature object has not yet been initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected final static int UNINITIALIZED = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * Possible {@link #state} value, signifying that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     * this signature object has been initialized for signing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    protected final static int SIGN = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     * Possible {@link #state} value, signifying that
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * this signature object has been initialized for verification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    protected final static int VERIFY = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * Current state of this signature object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    protected int state = UNINITIALIZED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
     * Creates a Signature object for the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
     * @param algorithm the standard string name of the algorithm.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   166
     * See the Signature section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   167
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   168
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected Signature(String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    // name of the special signature alg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    private final static String RSA_SIGNATURE = "NONEwithRSA";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    // name of the equivalent cipher alg
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    private final static String RSA_CIPHER = "RSA/ECB/PKCS1Padding";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    // all the services we need to lookup for compatibility with Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    private final static List<ServiceId> rsaIds = Arrays.asList(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        new ServiceId[] {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            new ServiceId("Signature", "NONEwithRSA"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            new ServiceId("Cipher", "RSA/ECB/PKCS1Padding"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            new ServiceId("Cipher", "RSA/ECB"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            new ServiceId("Cipher", "RSA//PKCS1Padding"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            new ServiceId("Cipher", "RSA"),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * Returns a Signature object that implements the specified signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * A new Signature object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * SignatureSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @param algorithm the standard name of the algorithm requested.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   206
     * See the Signature section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   207
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   208
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * @return the new Signature object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *          Signature implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    public static Signature getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
        List<Service> list;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        if (algorithm.equalsIgnoreCase(RSA_SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            list = GetInstance.getServices(rsaIds);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            list = GetInstance.getServices("Signature", algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        Iterator<Service> t = list.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (t.hasNext() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
            throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                (algorithm + " Signature not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        // try services until we find an Spi or a working Signature subclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        NoSuchAlgorithmException failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            Service s = t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            if (isSpi(s)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
                return new Delegate(s, t, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
                // must be a subclass of Signature, disable dynamic selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
                    Instance instance =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
                        GetInstance.getInstance(s, SignatureSpi.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
                    return getInstance(instance, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
                } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
                    failure = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        } while (t.hasNext());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        throw failure;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    private static Signature getInstance(Instance instance, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        Signature sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        if (instance.impl instanceof Signature) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
            sig = (Signature)instance.impl;
18181
4f0a36582461 8014620: Signature.getAlgorithm return null in special case
youdwei
parents: 12875
diff changeset
   256
            sig.algorithm = algorithm;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
            SignatureSpi spi = (SignatureSpi)instance.impl;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
            sig = new Delegate(spi, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
        sig.provider = instance.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
        return sig;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    private final static Map<String,Boolean> signatureInfo;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        signatureInfo = new ConcurrentHashMap<String,Boolean>();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        Boolean TRUE = Boolean.TRUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        // pre-initialize with values for our SignatureSpi implementations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        signatureInfo.put("sun.security.provider.DSA$RawDSA", TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        signatureInfo.put("sun.security.provider.DSA$SHA1withDSA", TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        signatureInfo.put("sun.security.rsa.RSASignature$MD2withRSA", TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        signatureInfo.put("sun.security.rsa.RSASignature$MD5withRSA", TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        signatureInfo.put("sun.security.rsa.RSASignature$SHA1withRSA", TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        signatureInfo.put("sun.security.rsa.RSASignature$SHA256withRSA", TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        signatureInfo.put("sun.security.rsa.RSASignature$SHA384withRSA", TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        signatureInfo.put("sun.security.rsa.RSASignature$SHA512withRSA", TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        signatureInfo.put("com.sun.net.ssl.internal.ssl.RSASignature", TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        signatureInfo.put("sun.security.pkcs11.P11Signature", TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
    private static boolean isSpi(Service s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        if (s.getType().equals("Cipher")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // must be a CipherSpi, which we can wrap with the CipherAdapter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        String className = s.getClassName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        Boolean result = signatureInfo.get(className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        if (result == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                Object instance = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                // Signature extends SignatureSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                // so it is a "real" Spi if it is an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                // instance of SignatureSpi but not Signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                boolean r = (instance instanceof SignatureSpi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                                && (instance instanceof Signature == false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                if ((debug != null) && (r == false)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                    debug.println("Not a SignatureSpi " + className);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                    debug.println("Delayed provider selection may not be "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                        + "available for algorithm " + s.getAlgorithm());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                result = Boolean.valueOf(r);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                signatureInfo.put(className, result);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                // something is wrong, assume not an SPI
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        return result.booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * Returns a Signature object that implements the specified signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * <p> A new Signature object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * SignatureSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
     * @param algorithm the name of the algorithm requested.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   326
     * See the Signature section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   327
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   328
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
     * @return the new Signature object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * @exception NoSuchAlgorithmException if a SignatureSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @exception IllegalArgumentException if the provider name is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *          or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
    public static Signature getInstance(String algorithm, String provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            throws NoSuchAlgorithmException, NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        if (algorithm.equalsIgnoreCase(RSA_SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
            // exception compatibility with existing code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
            if ((provider == null) || (provider.length() == 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
            Provider p = Security.getProvider(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            if (p == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                throw new NoSuchProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                    ("no such provider: " + provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
            return getInstanceRSA(p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        Instance instance = GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                ("Signature", SignatureSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        return getInstance(instance, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * Returns a Signature object that implements the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * <p> A new Signature object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * SignatureSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     * @param algorithm the name of the algorithm requested.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   376
     * See the Signature section in the <a href=
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   377
     * "{@docRoot}/../technotes/guides/security/StandardNames.html#Signature">
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   378
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @return the new Signature object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     * @exception NoSuchAlgorithmException if a SignatureSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
     * @exception IllegalArgumentException if the provider is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
     * @see Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    public static Signature getInstance(String algorithm, Provider provider)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        if (algorithm.equalsIgnoreCase(RSA_SIGNATURE)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
            // exception compatibility with existing code
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            if (provider == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
                throw new IllegalArgumentException("missing provider");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
            return getInstanceRSA(provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        Instance instance = GetInstance.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
                ("Signature", SignatureSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        return getInstance(instance, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    // return an implementation for NONEwithRSA, which is a special case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
    // because of the Cipher.RSA/ECB/PKCS1Padding compatibility wrapper
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private static Signature getInstanceRSA(Provider p)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        // try Signature first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        Service s = p.getService("Signature", RSA_SIGNATURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        if (s != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
            Instance instance = GetInstance.getInstance(s, SignatureSpi.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
            return getInstance(instance, RSA_SIGNATURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        // check Cipher
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
            Cipher c = Cipher.getInstance(RSA_CIPHER, p);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
            return new Delegate(new CipherAdapter(c), RSA_SIGNATURE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        } catch (GeneralSecurityException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
            // throw Signature style exception message to avoid confusion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
            // but append Cipher exception as cause
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
            throw new NoSuchAlgorithmException("no such algorithm: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
                + RSA_SIGNATURE + " for provider " + p.getName(), e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * Returns the provider of this signature object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @return the provider of this signature object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
    void chooseFirstProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        // empty, overridden in Delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * Initializes this object for verification. If this method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * again with a different argument, it negates the effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     * of this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
     * @param publicKey the public key of the identity whose signature is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
     * going to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * @exception InvalidKeyException if the key is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    public final void initVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
        engineInitVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
        state = VERIFY;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   459
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   460
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   461
            pdebug.println("Signature." + algorithm +
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   462
                " verification algorithm from: " + this.provider.getName());
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   463
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * Initializes this object for verification, using the public key from
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * the given certificate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * <p>If the certificate is of type X.509 and has a <i>key usage</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * extension field marked as critical, and the value of the <i>key usage</i>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * extension field implies that the public key in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * the certificate and its corresponding private key are not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * supposed to be used for digital signatures, an
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   474
     * {@code InvalidKeyException} is thrown.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * @param certificate the certificate of the identity whose signature is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * going to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     * @exception InvalidKeyException  if the public key in the certificate
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * is not encoded properly or does not include required  parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * information or cannot be used for digital signature purposes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * @since 1.3
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
    public final void initVerify(Certificate certificate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
        // If the certificate is of type X509Certificate,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        // we should check whether it has a Key Usage
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        // extension marked as critical.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        if (certificate instanceof java.security.cert.X509Certificate) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
            // Check whether the cert has a key usage extension
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
            // marked as a critical extension.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
            // The OID for KeyUsage extension is 2.5.29.15.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
            X509Certificate cert = (X509Certificate)certificate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
            Set<String> critSet = cert.getCriticalExtensionOIDs();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
            if (critSet != null && !critSet.isEmpty()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
                && critSet.contains("2.5.29.15")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
                boolean[] keyUsageInfo = cert.getKeyUsage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
                // keyUsageInfo[0] is for digitalSignature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
                if ((keyUsageInfo != null) && (keyUsageInfo[0] == false))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
                    throw new InvalidKeyException("Wrong key usage");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
        PublicKey publicKey = certificate.getPublicKey();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
        engineInitVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
        state = VERIFY;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   508
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   509
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   510
            pdebug.println("Signature." + algorithm +
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   511
                " verification algorithm from: " + this.provider.getName());
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   512
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
     * Initialize this object for signing. If this method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
     * again with a different argument, it negates the effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
     * of this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
     * @param privateKey the private key of the identity whose signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * is going to be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     * @exception InvalidKeyException if the key is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    public final void initSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        engineInitSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
        state = SIGN;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   529
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   530
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   531
            pdebug.println("Signature." + algorithm +
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   532
                " signing algorithm from: " + this.provider.getName());
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   533
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     * Initialize this object for signing. If this method is called
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
     * again with a different argument, it negates the effect
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
     * of this call.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
     * @param privateKey the private key of the identity whose signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
     * is going to be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
     * @param random the source of randomness for this signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
     * @exception InvalidKeyException if the key is invalid.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
    public final void initSign(PrivateKey privateKey, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
        engineInitSign(privateKey, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
        state = SIGN;
26736
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   552
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   553
        if (!skipDebug && pdebug != null) {
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   554
            pdebug.println("Signature." + algorithm +
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   555
                " signing algorithm from: " + this.provider.getName());
5a93000b26cd 8056026: Debug security logging should print Provider used for each crypto operation
vinnie
parents: 25859
diff changeset
   556
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
     * Returns the signature bytes of all the data updated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
     * The format of the signature depends on the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
     * signature scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
     * <p>A call to this method resets this signature object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * it was in when previously initialized for signing via a
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   566
     * call to {@code initSign(PrivateKey)}. That is, the object is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     * reset and available to generate another signature from the same
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   568
     * signer, if desired, via new calls to {@code update} and
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   569
     * {@code sign}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     * @return the signature bytes of the signing operation's result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * @exception SignatureException if this signature object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * initialized properly or if this signature algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
    public final byte[] sign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
        if (state == SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
            return engineSign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
        throw new SignatureException("object not initialized for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
                                     "signing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * Finishes the signature operation and stores the resulting signature
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   587
     * bytes in the provided buffer {@code outbuf}, starting at
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   588
     * {@code offset}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
     * The format of the signature depends on the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
     * signature scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
     * <p>This signature object is reset to its initial state (the state it
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   593
     * was in after a call to one of the {@code initSign} methods) and
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
     * can be reused to generate further signatures with the same private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
     * @param outbuf buffer for the signature result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   598
     * @param offset offset into {@code outbuf} where the signature is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     * stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   601
     * @param len number of bytes within {@code outbuf} allotted for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   604
     * @return the number of bytes placed into {@code outbuf}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @exception SignatureException if this signature object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     * initialized properly, if this signature algorithm is unable to
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   608
     * process the input data provided, or if {@code len} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     * than the actual signature length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
    public final int sign(byte[] outbuf, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
        throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
        if (outbuf == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
            throw new IllegalArgumentException("No output buffer given");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
        if (outbuf.length - offset < len) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
            throw new IllegalArgumentException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
                ("Output buffer too small for specified offset and length");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        if (state != SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
            throw new SignatureException("object not initialized for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
                                         "signing");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
        return engineSign(outbuf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
     * Verifies the passed-in signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
     * <p>A call to this method resets this signature object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
     * it was in when previously initialized for verification via a
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   634
     * call to {@code initVerify(PublicKey)}. That is, the object is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
     * reset and available to verify another signature from the identity
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   636
     * whose public key was specified in the call to {@code initVerify}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
     * @param signature the signature bytes to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
     * @return true if the signature was verified, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
     * @exception SignatureException if this signature object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
     * initialized properly, the passed-in signature is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
     * encoded or of the wrong type, if this signature algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
     * process the input data provided, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
    public final boolean verify(byte[] signature) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
        if (state == VERIFY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
            return engineVerify(signature);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
        throw new SignatureException("object not initialized for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
                                     "verification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
     * Verifies the passed-in signature in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
     * of bytes, starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
     * <p>A call to this method resets this signature object to the state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
     * it was in when previously initialized for verification via a
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   661
     * call to {@code initVerify(PublicKey)}. That is, the object is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
     * reset and available to verify another signature from the identity
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   663
     * whose public key was specified in the call to {@code initVerify}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
     * @param signature the signature bytes to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
     * @param offset the offset to start from in the array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
     * @param length the number of bytes to use, starting at offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
     * @return true if the signature was verified, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
     * @exception SignatureException if this signature object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
     * initialized properly, the passed-in signature is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
     * encoded or of the wrong type, if this signature algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
     * process the input data provided, etc.
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   676
     * @exception IllegalArgumentException if the {@code signature}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   677
     * byte array is null, or the {@code offset} or {@code length}
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   678
     * is less than 0, or the sum of the {@code offset} and
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   679
     * {@code length} is greater than the length of the
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   680
     * {@code signature} byte array.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
    public final boolean verify(byte[] signature, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
        throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
        if (state == VERIFY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
            if ((signature == null) || (offset < 0) || (length < 0) ||
12875
43a1cd4cfc32 7172149: ArrayIndexOutOfBoundsException from Signature.verify
xuelei
parents: 10709
diff changeset
   687
                (length > signature.length - offset)) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
                throw new IllegalArgumentException("Bad arguments");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
            return engineVerify(signature, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
        throw new SignatureException("object not initialized for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
                                     "verification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
     * Updates the data to be signed or verified by a byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
     * @param b the byte to use for the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
     * @exception SignatureException if this signature object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
     * initialized properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
    public final void update(byte b) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (state == VERIFY || state == SIGN) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            engineUpdate(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            throw new SignatureException("object not initialized for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
                                         + "signature or verification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
     * Updates the data to be signed or verified, using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
     * array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
     * @param data the byte array to use for the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
     * @exception SignatureException if this signature object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
     * initialized properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
    public final void update(byte[] data) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        update(data, 0, data.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
     * Updates the data to be signed or verified, using the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
     * array of bytes, starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
     * @param data the array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
     * @param off the offset to start from in the array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
     * @param len the number of bytes to use, starting at offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
     * @exception SignatureException if this signature object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
     * initialized properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
    public final void update(byte[] data, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
            throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
        if (state == SIGN || state == VERIFY) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
            engineUpdate(data, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
            throw new SignatureException("object not initialized for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
                                         + "signature or verification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   747
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
     * Updates the data to be signed or verified using the specified
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   750
     * ByteBuffer. Processes the {@code data.remaining()} bytes
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   751
     * starting at at {@code data.position()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
     * Upon return, the buffer's position will be equal to its limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
     * its limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
     * @param data the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
     * @exception SignatureException if this signature object is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
     * initialized properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   761
    public final void update(ByteBuffer data) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
        if ((state != SIGN) && (state != VERIFY)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
            throw new SignatureException("object not initialized for "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
                                         + "signature or verification");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        if (data == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
            throw new NullPointerException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   768
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
        engineUpdate(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   773
     * Returns the name of the algorithm for this signature object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
     * @return the name of the algorithm for this signature object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
     * Returns a string representation of this signature object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
     * providing information that includes the state of the object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
     * and the name of the algorithm used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
     * @return a string representation of this signature object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        String initState = "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
        switch (state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
        case UNINITIALIZED:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
            initState = "<not initialized>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        case VERIFY:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            initState = "<initialized for verifying>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
        case SIGN:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
            initState = "<initialized for signing>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
        return "Signature object: " + getAlgorithm() + initState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
     * Sets the specified algorithm parameter to the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
     * This method supplies a general-purpose mechanism through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
     * which it is possible to set the various parameters of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
     * A parameter may be any settable parameter for the algorithm, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
     * a parameter size, or a source of random bits for signature generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
     * (if appropriate), or an indication of whether or not to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
     * a specific but optional computation. A uniform algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
     * naming scheme for each parameter is desirable but left unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
     * at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
     * @param param the string identifier of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
     * @param value the parameter value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   818
     * @exception InvalidParameterException if {@code param} is an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
     * invalid parameter for this signature algorithm engine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
     * the parameter is already set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
     * and cannot be set again, a security exception occurs, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   823
     * @see #getParameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
     * @deprecated Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
     * {@link #setParameter(java.security.spec.AlgorithmParameterSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   827
     * setParameter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    public final void setParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        engineSetParameter(param, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
     * Initializes this signature engine with the specified parameter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   837
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   838
     * @param params the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   839
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   840
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
     * are inappropriate for this signature engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
     * @see #getParameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   844
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
    public final void setParameter(AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
        engineSetParameter(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   848
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
     * Returns the parameters used with this signature object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
     * <p>The returned parameters may be the same that were used to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
     * this signature, or may contain a combination of default and randomly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
     * generated parameter values used by the underlying signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
     * implementation if this signature requires algorithm parameters but
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
     * was not initialized with any.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
     * @return the parameters used with this signature, or null if this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
     * signature does not use any parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
     * @see #setParameter(AlgorithmParameterSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
    public final AlgorithmParameters getParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
        return engineGetParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
     * Gets the value of the specified algorithm parameter. This method
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
     * supplies a general-purpose mechanism through which it is possible to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
     * get the various parameters of this object. A parameter may be any
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
     * settable parameter for the algorithm, such as a parameter size, or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
     * a source of random bits for signature generation (if appropriate),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
     * or an indication of whether or not to perform a specific but optional
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
     * computation. A uniform algorithm-specific naming scheme for each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
     * parameter is desirable but left unspecified at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
     * @param param the string name of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
     * @return the object that represents the parameter value, or null if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
     * there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   884
     * @exception InvalidParameterException if {@code param} is an invalid
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
     * parameter for this engine, or another exception occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
     * trying to get this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
     * @see #setParameter(String, Object)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
    public final Object getParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
            throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
        return engineGetParameter(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
     * Returns a clone if the implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
     * @return a clone if the implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
     * @exception CloneNotSupportedException if this is called
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   904
     * on an implementation that does not support {@code Cloneable}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
    public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        if (this instanceof Cloneable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
            throw new CloneNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
    /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
     * The following class allows providers to extend from SignatureSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
     * rather than from Signature. It represents a Signature with an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
     * encapsulated, provider-supplied SPI object (of type SignatureSpi).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
     * If the provider implementation is an instance of SignatureSpi, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
     * getInstance() methods above return an instance of this class, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
     * the SPI object encapsulated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
     * Note: All SPI methods from the original Signature class have been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
     * moved up the hierarchy into a new class (SignatureSpi), which has
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
     * been interposed in the hierarchy between the API (Signature)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
     * and its original parent (Object).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
   928
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
    private static class Delegate extends Signature {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
        // filled in once the provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        private SignatureSpi sigSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
        // lock for mutex during provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
        private final Object lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
        // next service to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
        private Service firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        // remaining services to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
        // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
        private Iterator<Service> serviceIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
        // constructor
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
        Delegate(SignatureSpi sigSpi, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
            super(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
            this.sigSpi = sigSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            this.lock = null; // no lock needed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
        // used with delayed provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        Delegate(Service service,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
                        Iterator<Service> iterator, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            super(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
            this.firstService = service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
            this.serviceIterator = iterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
            this.lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
         * Returns a clone if the delegate is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
         * @return a clone if the delegate is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
         * @exception CloneNotSupportedException if this is called on a
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 18181
diff changeset
   968
         * delegate that does not support {@code Cloneable}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
        public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            if (sigSpi instanceof Cloneable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                SignatureSpi sigSpiClone = (SignatureSpi)sigSpi.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
                // Because 'algorithm' and 'provider' are private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
                // members of our supertype, we must perform a cast to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
                // access them.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
                Signature that =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
                    new Delegate(sigSpiClone, ((Signature)this).algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
                that.provider = ((Signature)this).provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
                return that;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
                throw new CloneNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
        private static SignatureSpi newInstance(Service s)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   988
            if (s.getType().equals("Cipher")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                // must be NONEwithRSA
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                    Cipher c = Cipher.getInstance(RSA_CIPHER, s.getProvider());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
                    return new CipherAdapter(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
                } catch (NoSuchPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
                    throw new NoSuchAlgorithmException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
                Object o = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
                if (o instanceof SignatureSpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
                    throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                        ("Not a SignatureSpi: " + o.getClass().getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                return (SignatureSpi)o;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
        // max number of debug warnings to print from chooseFirstProvider()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
        private static int warnCount = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
        /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
         * Choose the Spi from the first provider available. Used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
         * delayed provider selection is not possible because initSign()/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
         * initVerify() is not the first method called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
        void chooseFirstProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1015
            if (sigSpi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
                if (sigSpi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
                    int w = --warnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                    if (w >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
                        debug.println("Signature.init() not first method "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                            + "called, disabling delayed provider selection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
                        if (w == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
                            debug.println("Further warnings of this type will "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
                                + "be suppressed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
                        new Exception("Call trace").printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
                Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
                while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
                    Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
                    if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
                        s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
                        firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
                        s = serviceIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
                    if (isSpi(s) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
                        sigSpi = newInstance(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
                        provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
                        // not needed any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
                        firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
                        serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
                    } catch (NoSuchAlgorithmException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
                        lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
                ProviderException e = new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
                        ("Could not construct SignatureSpi instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
                if (lastException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
                    e.initCause(lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
                throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
        private void chooseProvider(int type, Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
                throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
            synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
                if (sigSpi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
                    init(sigSpi, type, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
                Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
                while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
                    Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
                    if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
                        s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
                        firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
                    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
                        s = serviceIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
                    // if provider says it does not support this key, ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
                    if (s.supportsParameter(key) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
                    // if instance is not a SignatureSpi, ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
                    if (isSpi(s) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
                    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
                        SignatureSpi spi = newInstance(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
                        init(spi, type, key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
                        provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
                        sigSpi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
                        firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
                        serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
                        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
                    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
                        // NoSuchAlgorithmException from newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
                        // InvalidKeyException from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
                        // RuntimeException (ProviderException) from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
                        if (lastException == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
                            lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
                        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
                // no working provider found, fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
                if (lastException instanceof InvalidKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
                    throw (InvalidKeyException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
                if (lastException instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
                    throw (RuntimeException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
                String k = (key != null) ? key.getClass().getName() : "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
                throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
                    ("No installed provider supports this key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
                    + k, lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1121
        private final static int I_PUB     = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1122
        private final static int I_PRIV    = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
        private final static int I_PRIV_SR = 3;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
        private void init(SignatureSpi spi, int type, Key  key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                SecureRandom random) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
            switch (type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
            case I_PUB:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                spi.engineInitVerify((PublicKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
            case I_PRIV:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                spi.engineInitSign((PrivateKey)key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
            case I_PRIV_SR:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                spi.engineInitSign((PrivateKey)key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
            default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                throw new AssertionError("Internal error: " + type);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
        protected void engineInitVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
            if (sigSpi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                sigSpi.engineInitVerify(publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                chooseProvider(I_PUB, publicKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        protected void engineInitSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
                throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
            if (sigSpi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
                sigSpi.engineInitSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
                chooseProvider(I_PRIV, privateKey, null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
        protected void engineInitSign(PrivateKey privateKey, SecureRandom sr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
                throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
            if (sigSpi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
                sigSpi.engineInitSign(privateKey, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
                chooseProvider(I_PRIV_SR, privateKey, sr);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        protected void engineUpdate(byte b) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
            sigSpi.engineUpdate(b);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        protected void engineUpdate(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
                throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            sigSpi.engineUpdate(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
        protected void engineUpdate(ByteBuffer data) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
            sigSpi.engineUpdate(data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
        protected byte[] engineSign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
            return sigSpi.engineSign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1189
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1190
        protected int engineSign(byte[] outbuf, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
            return sigSpi.engineSign(outbuf, offset, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
        protected boolean engineVerify(byte[] sigBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
            return sigSpi.engineVerify(sigBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
        protected boolean engineVerify(byte[] sigBytes, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
            return sigSpi.engineVerify(sigBytes, offset, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
        protected void engineSetParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
            sigSpi.engineSetParameter(param, value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
        protected void engineSetParameter(AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
            sigSpi.engineSetParameter(params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
        protected Object engineGetParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
                throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            return sigSpi.engineGetParameter(param);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
            chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
            return sigSpi.engineGetParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
    // adapter for RSA/ECB/PKCS1Padding ciphers
10709
d865c9f21240 7092375: Security Libraries don't build with javac -Werror
xuelei
parents: 9035
diff changeset
  1233
    @SuppressWarnings("deprecation")
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
    private static class CipherAdapter extends SignatureSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
        private final Cipher cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
        private ByteArrayOutputStream data;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        CipherAdapter(Cipher cipher) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
            this.cipher = cipher;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        protected void engineInitVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
                throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            cipher.init(Cipher.DECRYPT_MODE, publicKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            if (data == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
                data = new ByteArrayOutputStream(128);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
                data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
        protected void engineInitSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
                throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
            cipher.init(Cipher.ENCRYPT_MODE, privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
        protected void engineInitSign(PrivateKey privateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
                SecureRandom random) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
            cipher.init(Cipher.ENCRYPT_MODE, privateKey, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            data = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
        protected void engineUpdate(byte b) throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1267
            engineUpdate(new byte[] {b}, 0, 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1269
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
        protected void engineUpdate(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            if (data != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                data.write(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            byte[] out = cipher.update(b, off, len);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            if ((out != null) && (out.length != 0)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
                throw new SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
                    ("Cipher unexpectedly returned data");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        protected byte[] engineSign() throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
                return cipher.doFinal();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            } catch (IllegalBlockSizeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
                throw new SignatureException("doFinal() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
            } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
                throw new SignatureException("doFinal() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        protected boolean engineVerify(byte[] sigBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
                throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
                byte[] out = cipher.doFinal(sigBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
                byte[] dataBytes = data.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
                data.reset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
                return Arrays.equals(out, dataBytes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
            } catch (BadPaddingException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
                // e.g. wrong public key used
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
                // return false rather than throwing exception
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
            } catch (IllegalBlockSizeException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
                throw new SignatureException("doFinal() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
        protected void engineSetParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
                throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
            throw new InvalidParameterException("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        protected Object engineGetParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
                throws InvalidParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
            throw new InvalidParameterException("Parameters not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
}