src/java.base/share/classes/java/security/SignatureSpi.java
author valeriep
Mon, 23 Jul 2018 23:18:19 +0000
changeset 51229 17b7d7034e8e
parent 47216 71c04702a3d5
child 54483 ac20c3bdc55d
permissions -rw-r--r--
8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized Summary: Changed SunRsaSign and SunMSCAPI provider to return null and updated javadoc Reviewed-by: weijun, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
51229
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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: 2589
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: 2589
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: 2589
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2589
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2589
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.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.nio.ByteBuffer;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.jca.JCAUtil;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * This class defines the <i>Service Provider Interface</i> (<b>SPI</b>)
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
    38
 * for the {@code Signature} class, which is used to provide the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * functionality of a digital signature algorithm. Digital signatures are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * for authentication and integrity assurance of digital data.
40789
43b42538af90 8165413: Typos in javadoc: extra period, wrong number, misspelled word
igerasim
parents: 28059
diff changeset
    41
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <p> All the abstract methods in this class must be implemented by each
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * cryptographic service provider who wishes to supply the implementation
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * of a particular signature algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @author Benjamin Renaud
45434
4582657c7260 8181082: class-level since tag issues in java.base & java.datatransfer module
mli
parents: 40789
diff changeset
    47
 * @since 1.2
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * @see Signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public abstract class SignatureSpi {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * Application-specified source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    protected SecureRandom appRandom = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     * Initializes this signature object with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
     * public key for verification operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * @param publicKey the public key of the identity whose signature is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     * going to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * @exception InvalidKeyException if the key is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * encoded, parameters are missing, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected abstract void engineInitVerify(PublicKey publicKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        throws InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Initializes this signature object with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     * private key for signing operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param privateKey the private key of the identity whose signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * will be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * @exception InvalidKeyException if the key is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * encoded, parameters are missing, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected abstract void engineInitSign(PrivateKey privateKey)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        throws InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     * Initializes this signature object with the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * private key and source of randomness for signing operations.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <p>This concrete method has been added to this previously-defined
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * abstract class. (For backwards compatibility, it cannot be abstract.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param privateKey the private key of the identity whose signature
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * will be generated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @exception InvalidKeyException if the key is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * encoded, parameters are missing, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected void engineInitSign(PrivateKey privateKey,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                                  SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            this.appRandom = random;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            engineInitSign(privateKey);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * Updates the data to be signed or verified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * using the specified byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * @param b the byte to use for the update.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @exception SignatureException if the engine is not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * properly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    protected abstract void engineUpdate(byte b) throws SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
     * Updates the data to be signed or verified, using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
     * specified array of bytes, starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
     * @param b the array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * @param off the offset to start from in the array of bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param len the number of bytes to use, starting at offset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     * @exception SignatureException if the engine is not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    protected abstract void engineUpdate(byte[] b, int off, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        throws SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * 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: 5506
diff changeset
   134
     * ByteBuffer. Processes the {@code data.remaining()} bytes
28059
e576535359cc 8067377: My hobby: caning, then then canning, the the can-can
martin
parents: 25859
diff changeset
   135
     * starting at {@code data.position()}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * Upon return, the buffer's position will be equal to its limit;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * its limit will not have changed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * @param input the ByteBuffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @since 1.5
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    protected void engineUpdate(ByteBuffer input) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        if (input.hasRemaining() == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            if (input.hasArray()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                byte[] b = input.array();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
                int ofs = input.arrayOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
                int pos = input.position();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                int lim = input.limit();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                engineUpdate(b, ofs + pos, lim - pos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                input.position(lim);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                int len = input.remaining();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                byte[] b = new byte[JCAUtil.getTempArraySize(len)];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                while (len > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                    int chunk = Math.min(len, b.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
                    input.get(b, 0, chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                    engineUpdate(b, 0, chunk);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                    len -= chunk;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        } catch (SignatureException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            // is specified to only occur when the engine is not initialized
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            // this case should never occur as it is caught in Signature.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            throw new ProviderException("update() failed", e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Returns the signature bytes of all the data
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * updated so far.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     * The format of the signature depends on the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * signature scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * @return the signature bytes of the signing operation's result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     * @exception SignatureException if the engine is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * initialized properly or if this signature algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * process the input data provided.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected abstract byte[] engineSign() throws SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * Finishes this signature operation and stores the resulting signature
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   187
     * bytes in the provided buffer {@code outbuf}, starting at
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   188
     * {@code offset}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * The format of the signature depends on the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     * signature scheme.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     * <p>The signature implementation is reset to its initial state
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * (the state it was in after a call to one of the
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   194
     * {@code engineInitSign} methods)
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * and can be reused to generate further signatures with the same private
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     * key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * This method should be abstract, but we leave it concrete for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     * binary compatibility.  Knowledgeable providers should override this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @param outbuf buffer for the signature result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   204
     * @param offset offset into {@code outbuf} where the signature is
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * stored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   207
     * @param len number of bytes within {@code outbuf} allotted for the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     * Both this default implementation and the SUN provider do not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * return partial digests. If the value of this parameter is less
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
     * than the actual signature length, this method will throw a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
     * SignatureException.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * This parameter is ignored if its value is greater than or equal to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     * the actual signature length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   216
     * @return the number of bytes placed into {@code outbuf}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     * @exception SignatureException if the engine is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
     * initialized properly, if this signature algorithm is unable to
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   220
     * process the input data provided, or if {@code len} is less
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * than the actual signature length.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @since 1.2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
    protected int engineSign(byte[] outbuf, int offset, int len)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
                        throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        byte[] sig = engineSign();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        if (len < sig.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
                throw new SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
                    ("partial signatures not returned");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
        if (outbuf.length - offset < sig.length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                throw new SignatureException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                    ("insufficient space in the output buffer to store the "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
                     + "signature");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        System.arraycopy(sig, 0, outbuf, offset, sig.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        return sig.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * Verifies the passed-in signature.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     * @param sigBytes the signature bytes to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     * @return true if the signature was verified, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     * @exception SignatureException if the engine is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * initialized properly, the passed-in signature is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * encoded or of the wrong type, if this signature algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     * process the input data provided, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    protected abstract boolean engineVerify(byte[] sigBytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        throws SignatureException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     * Verifies the passed-in signature in the specified array
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * of bytes, starting at the specified offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * <p> Note: Subclasses should overwrite the default implementation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @param sigBytes the signature bytes to be verified.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * @param offset the offset to start from in the array of bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * @param length the number of bytes to use, starting at offset.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     * @return true if the signature was verified, false if not.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
     * @exception SignatureException if the engine is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
     * initialized properly, the passed-in signature is improperly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
     * encoded or of the wrong type, if this signature algorithm is unable to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
     * process the input data provided, etc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
    protected boolean engineVerify(byte[] sigBytes, int offset, int length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
        throws SignatureException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        byte[] sigBytesCopy = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        System.arraycopy(sigBytes, offset, sigBytesCopy, 0, length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        return engineVerify(sigBytesCopy);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * Sets the specified algorithm parameter to the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * value. This method supplies a general-purpose mechanism through
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * which it is possible to set the various parameters of this object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     * A parameter may be any settable parameter for the algorithm, such as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * a parameter size, or a source of random bits for signature generation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     * (if appropriate), or an indication of whether or not to perform
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * a specific but optional computation. A uniform algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     * naming scheme for each parameter is desirable but left unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     * @param param the string identifier of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
     * @param value the parameter value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   297
     * @exception InvalidParameterException if {@code param} is an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
     * invalid parameter for this signature algorithm engine,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
     * the parameter is already set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
     * and cannot be set again, a security exception occurs, and so on.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
     * @deprecated Replaced by {@link
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
     * #engineSetParameter(java.security.spec.AlgorithmParameterSpec)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
     * engineSetParameter}.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    protected abstract void engineSetParameter(String param, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        throws InvalidParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * <p>This method is overridden by providers to initialize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * this signature engine with the specified parameter set.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * @param params the parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @exception UnsupportedOperationException if this method is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     * overridden by a provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * @exception InvalidAlgorithmParameterException if this method is
2589
af4853bc7e87 6827153: Miscellaneous typos in javadoc
martin
parents: 2
diff changeset
   320
     * overridden by a provider and the given parameters
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * are inappropriate for this signature engine
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    protected void engineSetParameter(AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
        throws InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
            throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
    /**
51229
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   329
     * <p>This method is overridden by providers to return the parameters
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   330
     * used with this signature engine.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
     *
51229
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   332
     * <p> If this signature engine has been previously initialized with
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   333
     * parameters (by calling the {@code engineSetParameter} method), this
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   334
     * method returns the same parameters. If this signature engine has not been
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   335
     * initialized with parameters, this method may return a combination of
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   336
     * default and randomly generated parameter values if the underlying
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   337
     * signature implementation supports it and can successfully generate
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   338
     * them. Otherwise, {@code null} is returned.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     *
51229
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   340
     * @return the parameters used with this signature engine, or {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * @exception UnsupportedOperationException if this method is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     * not overridden by a provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    protected AlgorithmParameters engineGetParameters() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        throw new UnsupportedOperationException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * Gets the value of the specified algorithm parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * This method supplies a general-purpose mechanism through which it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     * is possible to get the various parameters of this object. A parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
     * may be any settable parameter for the algorithm, such as a parameter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
     * size, or  a source of random bits for signature generation (if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * appropriate), or an indication of whether or not to perform a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * specific but optional computation. A uniform algorithm-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * naming scheme for each parameter is desirable but left unspecified
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     * at this time.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
     * @param param the string name of the parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
     *
51229
17b7d7034e8e 8206171: Signature#getParameters for RSASSA-PSS throws ProviderException when not initialized
valeriep
parents: 47216
diff changeset
   363
     * @return the object that represents the parameter value, or {@code null} if
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
     * there is none.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
     *
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   366
     * @exception InvalidParameterException if {@code param} is an
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * invalid parameter for this engine, or another exception occurs while
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     * trying to get this parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * @deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    @Deprecated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
    protected abstract Object engineGetParameter(String param)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        throws InvalidParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * Returns a clone if the implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @return a clone if the implementation is cloneable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @exception CloneNotSupportedException if this is called
18579
b678846778ad 8019360: Cleanup of the javadoc <code> tag in java.security.*
juh
parents: 5506
diff changeset
   382
     * on an implementation that does not support {@code Cloneable}.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
    public Object clone() throws CloneNotSupportedException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        if (this instanceof Cloneable) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
            return super.clone();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            throw new CloneNotSupportedException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
}