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