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