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