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