jdk/src/share/classes/javax/crypto/KeyAgreement.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 8152 94e5966bdf22
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1997, 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.crypto;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Provider.Service;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.spec.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import sun.security.util.Debug;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import sun.security.jca.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * This class provides the functionality of a key agreement (or key
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * exchange) protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * The keys involved in establishing a shared secret are created by one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * key generators (<code>KeyPairGenerator</code> or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <code>KeyGenerator</code>), a <code>KeyFactory</code>, or as a result from
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * an intermediate phase of the key agreement protocol.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * <p> For each of the correspondents in the key exchange, <code>doPhase</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * needs to be called. For example, if this key exchange is with one other
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * party, <code>doPhase</code> needs to be called once, with the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 * <code>lastPhase</code> flag set to <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * If this key exchange is
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 * with two other parties, <code>doPhase</code> needs to be called twice,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 * the first time setting the <code>lastPhase</code> flag to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 * <code>false</code>, and the second time setting it to <code>true</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 * There may be any number of parties involved in a key exchange.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 * @author Jan Luehe
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 * @see KeyGenerator
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 * @see SecretKey
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
public class KeyAgreement {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static final Debug debug =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                        Debug.getInstance("jca", "KeyAgreement");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    private KeyAgreementSpi spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // The name of the key agreement algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private final String algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    // next service to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    private Service firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    // remaining services to try in provider selection
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    // null once provider is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    private Iterator serviceIterator;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    private final Object lock;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Creates a KeyAgreement object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * @param keyAgreeSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * @param algorithm the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected KeyAgreement(KeyAgreementSpi keyAgreeSpi, Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                           String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        this.spi = keyAgreeSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        lock = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    private KeyAgreement(Service s, Iterator t, String algorithm) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        firstService = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        serviceIterator = t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        this.algorithm = algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        lock = new Object();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Returns the algorithm name of this <code>KeyAgreement</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <p>This is the same name that was specified in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * <code>getInstance</code> calls that created this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * <code>KeyAgreement</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
     * @return the algorithm name of this <code>KeyAgreement</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public final String getAlgorithm() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return this.algorithm;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * Returns a <code>KeyAgreement</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * specified key agreement algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
     * A new KeyAgreement object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * KeyAgreementSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
     * @param algorithm the standard name of the requested key agreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * See Appendix A in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     *   "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * Java Cryptography Architecture Reference Guide</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * @return the new <code>KeyAgreement</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * @exception NullPointerException if the specified algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * @exception NoSuchAlgorithmException if no Provider supports a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     *          KeyAgreementSpi implementation for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
     *          specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    public static final KeyAgreement getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        List services = GetInstance.getServices("KeyAgreement", algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        // make sure there is at least one service from a signed provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        Iterator t = services.iterator();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
        while (t.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            Service s = (Service)t.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
            if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            return new KeyAgreement(s, t, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        throw new NoSuchAlgorithmException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
                                ("Algorithm " + algorithm + " not available");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     * Returns a <code>KeyAgreement</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
     * specified key agreement algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
     * <p> A new KeyAgreement object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
     * KeyAgreementSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
     * @param algorithm the standard name of the requested key agreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
     * See Appendix A in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     * <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     *   "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     * Java Cryptography Architecture Reference Guide</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
     * @return the new <code>KeyAgreement</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
     * @exception NullPointerException if the specified algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
     * @exception NoSuchAlgorithmException if a KeyAgreementSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
     *          implementation for the specified algorithm is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     *          available from the specified provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     * @exception NoSuchProviderException if the specified provider is not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     *          registered in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     *          is null or empty.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
    public static final KeyAgreement getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            NoSuchProviderException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
        Instance instance = JceSecurity.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                ("KeyAgreement", KeyAgreementSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        return new KeyAgreement((KeyAgreementSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
     * Returns a <code>KeyAgreement</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * specified key agreement algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * <p> A new KeyAgreement object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * KeyAgreementSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @param algorithm the standard name of the requested key agreement
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     * See Appendix A in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
     * <a href=
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
     *   "{@docRoot}/../technotes/guides/security/crypto/CryptoSpec.html#AppA">
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
     * Java Cryptography Architecture Reference Guide</a>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
     * for information about standard algorithm names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
     * @return the new <code>KeyAgreement</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * @exception NullPointerException if the specified algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     * @exception NoSuchAlgorithmException if a KeyAgreementSpi
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
     *          implementation for the specified algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     *          from the specified Provider object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
     * @exception IllegalArgumentException if the <code>provider</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
     *          is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    public static final KeyAgreement getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            Provider provider) throws NoSuchAlgorithmException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        Instance instance = JceSecurity.getInstance
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                ("KeyAgreement", KeyAgreementSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return new KeyAgreement((KeyAgreementSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
    // max number of debug warnings to print from chooseFirstProvider()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    private static int warnCount = 10;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * Choose the Spi from the first provider available. Used if
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     * delayed provider selection is not possible because init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
     * is not the first method called.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    void chooseFirstProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
            if (debug != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
                int w = --warnCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
                if (w >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
                    debug.println("KeyAgreement.init() not first method "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
                        + "called, disabling delayed provider selection");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
                    if (w == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
                        debug.println("Further warnings of this type will "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
                            + "be suppressed");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
                    new Exception("Call trace").printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
                Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                    s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
                    s = (Service)serviceIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                    Object obj = s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
                    if (obj instanceof KeyAgreementSpi == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
                        continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
                    spi = (KeyAgreementSpi)obj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                    // not needed any more
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                    serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                    lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            ProviderException e = new ProviderException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                    ("Could not construct KeyAgreementSpi instance");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
            if (lastException != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                e.initCause(lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
            throw e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    private final static int I_NO_PARAMS = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
    private final static int I_PARAMS    = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    private void implInit(KeyAgreementSpi spi, int type, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        if (type == I_NO_PARAMS) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
            spi.engineInit(key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
        } else { // I_PARAMS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            spi.engineInit(key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    private void chooseProvider(int initType, Key key,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            AlgorithmParameterSpec params, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
            throws InvalidKeyException, InvalidAlgorithmParameterException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        synchronized (lock) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
                implInit(spi, initType, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
            Exception lastException = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            while ((firstService != null) || serviceIterator.hasNext()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                Service s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                if (firstService != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                    s = firstService;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                    s = (Service)serviceIterator.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
                // if provider says it does not support this key, ignore it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
                if (s.supportsParameter(key) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
                if (JceSecurity.canUseProvider(s.getProvider()) == false) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
                    continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
                    KeyAgreementSpi spi = (KeyAgreementSpi)s.newInstance(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
                    implInit(spi, initType, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
                    provider = s.getProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
                    this.spi = spi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
                    firstService = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                    serviceIterator = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                    return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                    // NoSuchAlgorithmException from newInstance()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    // InvalidKeyException from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    // RuntimeException (ProviderException) from init()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    if (lastException == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                        lastException = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
            // no working provider found, fail
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
            if (lastException instanceof InvalidKeyException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                throw (InvalidKeyException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
            if (lastException instanceof InvalidAlgorithmParameterException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                throw (InvalidAlgorithmParameterException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
            if (lastException instanceof RuntimeException) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                throw (RuntimeException)lastException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            String kName = (key != null) ? key.getClass().getName() : "(null)";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
            throw new InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                ("No installed provider supports this key: "
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                + kName, lastException);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
     * Returns the provider of this <code>KeyAgreement</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * @return the provider of this <code>KeyAgreement</code> object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     * Initializes this key agreement with the given key, which is required to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
     * contain all the algorithm parameters required for this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
     * <p> If this key agreement requires any random bytes, it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
     * them using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
     * {@link SecureRandom <code>SecureRandom</code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
     * implementation of the highest-priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
     * installed provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
     * @param key the party's private information. For example, in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
     * of the Diffie-Hellman key agreement, this would be the party's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Diffie-Hellman private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     * @exception InvalidKeyException if the given key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * inappropriate for this key agreement, e.g., is of the wrong type or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * has an incompatible algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
    public final void init(Key key) throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        init(key, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * Initializes this key agreement with the given key and source of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     * randomness. The given key is required to contain all the algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * parameters required for this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * <p> If the key agreement algorithm requires random bytes, it gets them
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * from the given source of randomness, <code>random</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * However, if the underlying
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * algorithm implementation does not require any random bytes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     * <code>random</code> is ignored.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
     * @param key the party's private information. For example, in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
     * of the Diffie-Hellman key agreement, this would be the party's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
     * Diffie-Hellman private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
     * @exception InvalidKeyException if the given key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
     * inappropriate for this key agreement, e.g., is of the wrong type or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
     * has an incompatible algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    public final void init(Key key, SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
            throws InvalidKeyException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
            spi.engineInit(key, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
                chooseProvider(I_NO_PARAMS, key, null, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
            } catch (InvalidAlgorithmParameterException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
                // should never occur
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
                throw new InvalidKeyException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * Initializes this key agreement with the given key and set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     * algorithm parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     * <p> If this key agreement requires any random bytes, it will get
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * them using the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * {@link SecureRandom <code>SecureRandom</code>}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * implementation of the highest-priority
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * installed provider as the source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * (If none of the installed providers supply an implementation of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * SecureRandom, a system-provided source of randomness will be used.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
     * @param key the party's private information. For example, in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
     * of the Diffie-Hellman key agreement, this would be the party's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
     * Diffie-Hellman private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
     * @param params the key agreement parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
     * @exception InvalidKeyException if the given key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
     * inappropriate for this key agreement, e.g., is of the wrong type or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
     * has an incompatible algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
     * are inappropriate for this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
    public final void init(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
        throws InvalidKeyException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
        init(key, params, JceSecurity.RANDOM);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
     * Initializes this key agreement with the given key, set of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
     * algorithm parameters, and source of randomness.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
     * @param key the party's private information. For example, in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
     * of the Diffie-Hellman key agreement, this would be the party's own
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
     * Diffie-Hellman private key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
     * @param params the key agreement parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
     * @param random the source of randomness
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
     * @exception InvalidKeyException if the given key is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
     * inappropriate for this key agreement, e.g., is of the wrong type or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
     * has an incompatible algorithm type.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
     * @exception InvalidAlgorithmParameterException if the given parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
     * are inappropriate for this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
    public final void init(Key key, AlgorithmParameterSpec params,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
                           SecureRandom random)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
        throws InvalidKeyException, InvalidAlgorithmParameterException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
        if (spi != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
            spi.engineInit(key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
            chooseProvider(I_PARAMS, key, params, random);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
        }
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
     * Executes the next phase of this key agreement with the given
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
     * key that was received from one of the other parties involved in this key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
     * agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
     * @param key the key for this phase. For example, in the case of
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
     * Diffie-Hellman between 2 parties, this would be the other party's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
     * Diffie-Hellman public key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
     * @param lastPhase flag which indicates whether or not this is the last
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
     * phase of this key agreement.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
     * @return the (intermediate) key resulting from this phase, or null
90ce3da70b43 Initial load
duke
parents:
diff changeset
   531
     * if this phase does not yield a key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
     * this phase.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
     * initialized.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
    public final Key doPhase(Key key, boolean lastPhase)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
        throws InvalidKeyException, IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
        return spi.engineDoPhase(key, lastPhase);
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
     * Generates the shared secret and returns it in a new buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
     * <p>This method resets this <code>KeyAgreement</code> object, so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
     * reinitialized with one of the <code>init</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
     * @return the new buffer with the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   559
    public final byte[] generateSecret() throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
        return spi.engineGenerateSecret();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   562
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   563
90ce3da70b43 Initial load
duke
parents:
diff changeset
   564
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
     * Generates the shared secret, and places it into the buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
     * <code>sharedSecret</code>, beginning at <code>offset</code> inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
     * <p>If the <code>sharedSecret</code> buffer is too small to hold the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
     * result, a <code>ShortBufferException</code> is thrown.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
     * In this case, this call should be repeated with a larger output buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
     * <p>This method resets this <code>KeyAgreement</code> object, so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
     * reinitialized with one of the <code>init</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
     * @param sharedSecret the buffer for the shared secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
     * @param offset the offset in <code>sharedSecret</code> where the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
     * shared secret will be stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     * @return the number of bytes placed into <code>sharedSecret</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
     * to hold the secret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    public final int generateSecret(byte[] sharedSecret, int offset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
        throws IllegalStateException, ShortBufferException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
        return spi.engineGenerateSecret(sharedSecret, offset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
     * Creates the shared secret and returns it as a <code>SecretKey</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
     * object of the specified algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
     * <p>This method resets this <code>KeyAgreement</code> object, so that it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
     * can be reused for further key agreements. Unless this key agreement is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
     * reinitialized with one of the <code>init</code> methods, the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
     * private information and algorithm parameters will be used for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
     * subsequent key agreements.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
     * @param algorithm the requested secret-key algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
     * @return the shared secret key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
     * @exception IllegalStateException if this key agreement has not been
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
     * completed yet
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
     * @exception NoSuchAlgorithmException if the specified secret-key
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
     * algorithm is not available
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
     * @exception InvalidKeyException if the shared secret-key material cannot
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     * be used to generate a secret key of the specified algorithm (e.g.,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
     * the key material is too short)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
    public final SecretKey generateSecret(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
        throws IllegalStateException, NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
            InvalidKeyException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
        chooseFirstProvider();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
        return spi.engineGenerateSecret(algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
}