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