jdk/src/java.base/share/classes/javax/crypto/ExemptionMechanism.java
author wetmore
Mon, 31 Oct 2016 14:09:42 -0700
changeset 41826 b35ee9b35b09
parent 39564 ed1bb10788f0
child 45118 e4258d800b54
permissions -rw-r--r--
4985694: Incomplete spec for most of the getInstances Reviewed-by: xuelei, mullan
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
37348
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
     2
 * Copyright (c) 1999, 2016, 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.security.AlgorithmParameters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.security.Provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.security.Key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.security.Security;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.security.NoSuchAlgorithmException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.security.NoSuchProviderException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.security.InvalidKeyException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.security.InvalidAlgorithmParameterException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.security.spec.AlgorithmParameterSpec;
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
    37
import java.util.Objects;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import sun.security.jca.GetInstance.Instance;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class provides the functionality of an exemption mechanism, examples
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * of which are <i>key recovery</i>, <i>key weakening</i>, and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * <i>key escrow</i>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * <p>Applications or applets that use an exemption mechanism may be granted
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 * stronger encryption capabilities than those which don't.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class ExemptionMechanism {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    // The provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    private Provider provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // The provider implementation (delegate)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private ExemptionMechanismSpi exmechSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    // The name of the exemption mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private String mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    // Flag which indicates whether this ExemptionMechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    // result is generated successfully.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    private boolean done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
    // State information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private boolean initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    // Store away the key at init() time for later comparison.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    private Key keyStored = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     * Creates a ExemptionMechanism object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     * @param exmechSpi the delegate
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
     * @param provider the provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
     * @param mechanism the exemption mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected ExemptionMechanism(ExemptionMechanismSpi exmechSpi,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                                 Provider provider,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                                 String mechanism) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        this.exmechSpi = exmechSpi;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        this.provider = provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        this.mechanism = mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * Returns the exemption mechanism name of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * <code>ExemptionMechanism</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * <p>This is the same name that was specified in one of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * <code>getInstance</code> calls that created this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * <code>ExemptionMechanism</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @return the exemption mechanism name of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * <code>ExemptionMechanism</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    public final String getName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        return this.mechanism;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * Returns an <code>ExemptionMechanism</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * specified exemption mechanism algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * <p> This method traverses the list of registered security Providers,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * starting with the most preferred Provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * A new ExemptionMechanism object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * ExemptionMechanismSpi implementation from the first
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Provider that supports the specified algorithm is returned.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   116
     * @implNote
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   117
     * The JDK Reference Implementation additionally uses the
37348
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   118
     * {@code jdk.security.provider.preferred}
9ccec3170d5e 8152205: jdk.security.provider.preferred is ambiguously documented
ascarpino
parents: 33241
diff changeset
   119
     * {@link Security#getProperty(String) Security} property to determine
33241
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   120
     * the preferred provider order for the specified algorithm. This
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   121
     * may be different than the order of providers returned by
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   122
     * {@link Security#getProviders() Security.getProviders()}.
27eb2d6abda9 8133151: Preferred provider configuration for JCE
ascarpino
parents: 25859
diff changeset
   123
     *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * @param algorithm the standard name of the requested exemption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * mechanism.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   126
     * See the ExemptionMechanism section in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
     * <a href=
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 9035
diff changeset
   128
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Exemption">
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   129
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
     * for information about standard exemption mechanism names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   132
     * @return the new {@code ExemptionMechanism} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   134
     * @throws NoSuchAlgorithmException if no {@code Provider} supports an
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   135
     *         {@code ExemptionMechanismSpi} implementation for the
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   136
     *         specified algorithm
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   138
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    public static final ExemptionMechanism getInstance(String algorithm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   144
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        Instance instance = JceSecurity.getInstance("ExemptionMechanism",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
                ExemptionMechanismSpi.class, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        return new ExemptionMechanism((ExemptionMechanismSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
     * Returns an <code>ExemptionMechanism</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
     * specified exemption mechanism algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
     * <p> A new ExemptionMechanism object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
     * ExemptionMechanismSpi implementation from the specified provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
     * is returned.  The specified provider must be registered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
     * in the security provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * <p> Note that the list of registered providers may be retrieved via
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     * the {@link Security#getProviders() Security.getProviders()} method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
     * @param algorithm the standard name of the requested exemption mechanism.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   165
     * See the ExemptionMechanism section in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
     * <a href=
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 9035
diff changeset
   167
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Exemption">
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   168
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
     * for information about standard exemption mechanism names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
     * @param provider the name of the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   173
     * @return the new {@code ExemptionMechanism} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   175
     * @throws IllegalArgumentException if the {@code provider}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   176
     *         is {@code null} or empty
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   178
     * @throws NoSuchAlgorithmException if an {@code ExemptionMechanismSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   179
     *         implementation for the specified algorithm is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   180
     *         available from the specified provider
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   182
     * @throws NoSuchProviderException if the specified provider is not
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   183
     *         registered in the security provider list
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   185
     * @throws NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    public static final ExemptionMechanism getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            String provider) throws NoSuchAlgorithmException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            NoSuchProviderException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   192
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        Instance instance = JceSecurity.getInstance("ExemptionMechanism",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
                ExemptionMechanismSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        return new ExemptionMechanism((ExemptionMechanismSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Returns an <code>ExemptionMechanism</code> object that implements the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * specified exemption mechanism algorithm.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
     * <p> A new ExemptionMechanism object encapsulating the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
     * ExemptionMechanismSpi implementation from the specified Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
     * object is returned.  Note that the specified Provider object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
     * does not have to be registered in the provider list.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
     * @param algorithm the standard name of the requested exemption mechanism.
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   209
     * See the ExemptionMechanism section in the
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
     * <a href=
18771
9dadb0719cea 8019772: Fix doclint issues in javax.crypto and javax.security subpackages
juh
parents: 9035
diff changeset
   211
     *   "{@docRoot}/../technotes/guides/security/StandardNames.html#Exemption">
8152
94e5966bdf22 5001004: Required Security Algorithms need to be defined
mullan
parents: 5506
diff changeset
   212
     * Java Cryptography Architecture Standard Algorithm Name Documentation</a>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
     * for information about standard exemption mechanism names.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
     * @param provider the provider.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   217
     * @return the new {@code ExemptionMechanism} object
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   219
     * @throws IllegalArgumentException if the {@code provider}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   220
     *         is null
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   222
     * @throws NoSuchAlgorithmException if an {@code ExemptionMechanismSpi}
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   223
     *         implementation for the specified algorithm is not available
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   224
     *         from the specified {@code Provider object}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     *
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   226
     * @exception NullPointerException if {@code algorithm} is {@code null}
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * @see java.security.Provider
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
    public static final ExemptionMechanism getInstance(String algorithm,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
            Provider provider) throws NoSuchAlgorithmException {
41826
b35ee9b35b09 4985694: Incomplete spec for most of the getInstances
wetmore
parents: 39564
diff changeset
   232
        Objects.requireNonNull(algorithm, "null algorithm name");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        Instance instance = JceSecurity.getInstance("ExemptionMechanism",
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
                ExemptionMechanismSpi.class, algorithm, provider);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return new ExemptionMechanism((ExemptionMechanismSpi)instance.impl,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
                instance.provider, algorithm);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
     * Returns the provider of this <code>ExemptionMechanism</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
     * @return the provider of this <code>ExemptionMechanism</code> object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    public final Provider getProvider() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        return this.provider;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
     * Returns whether the result blob has been generated successfully by this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
     * exemption mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
     * <p>The method also makes sure that the key passed in is the same as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
     * the one this exemption mechanism used in initializing and generating
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
     * phases.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
     * @param key the key the crypto is going to use.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
     * @return whether the result blob of the same key has been generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
     * successfully by this exemption mechanism; false if <code>key</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * is null.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @exception ExemptionMechanismException if problem(s) encountered
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * while determining whether the result blob has been generated successfully
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     * by this exemption mechanism object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
    public final boolean isCryptoAllowed(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
            throws ExemptionMechanismException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
        boolean ret = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
        if (done && (key != null)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
            // Check if the key passed in is the same as the one
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
            // this exemption mechanism used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
            ret = keyStored.equals(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        return ret;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
     * Returns the length in bytes that an output buffer would need to be in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
     * order to hold the result of the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
     * {@link #genExemptionBlob(byte[]) genExemptionBlob}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
     * operation, given the input length <code>inputLen</code> (in bytes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
     * <p>The actual output length of the next
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
     * {@link #genExemptionBlob(byte[]) genExemptionBlob}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
     * call may be smaller than the length returned by this method.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
     * @param inputLen the input length (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
     * @return the required output buffer size (in bytes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
     * @exception IllegalStateException if this exemption mechanism is in a
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
     * wrong state (e.g., has not yet been initialized)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
    public final int getOutputSize(int inputLen) throws IllegalStateException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                "ExemptionMechanism not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        if (inputLen < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            throw new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
                "Input size must be equal to " + "or greater than zero");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        return exmechSpi.engineGetOutputSize(inputLen);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
     * Initializes this exemption mechanism with a key.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
     * <p>If this exemption mechanism requires any algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
     * that cannot be derived from the given <code>key</code>, the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
     * underlying exemption mechanism implementation is supposed to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
     * generate the required parameters itself (using provider-specific
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
     * default values); in the case that algorithm parameters must be
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
     * specified by the caller, an <code>InvalidKeyException</code> is raised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
     * @param key the key for this exemption mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
     * this exemption mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
     * @exception ExemptionMechanismException if problem(s) encountered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
     * process of initializing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
    public final void init(Key key)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
            throws InvalidKeyException, ExemptionMechanismException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
        keyStored = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
        exmechSpi.engineInit(key);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
     * Initializes this exemption mechanism with a key and a set of algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
     * <p>If this exemption mechanism requires any algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
     * and <code>params</code> is null, the underlying exemption
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
     * mechanism implementation is supposed to generate the required
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
     * parameters itself (using provider-specific default values); in the case
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
     * that algorithm parameters must be specified by the caller, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
     * <code>InvalidAlgorithmParameterException</code> is raised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
     * @param key the key for this exemption mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * this exemption mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     * parameters are inappropriate for this exemption mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
     * @exception ExemptionMechanismException if problem(s) encountered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
     * process of initializing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
    public final void init(Key key, AlgorithmParameterSpec params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
            throws InvalidKeyException, InvalidAlgorithmParameterException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
            ExemptionMechanismException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
        keyStored = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        exmechSpi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
     * Initializes this exemption mechanism with a key and a set of algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
     * parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
     * <p>If this exemption mechanism requires any algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
     * and <code>params</code> is null, the underlying exemption mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
     * implementation is supposed to generate the required parameters itself
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
     * (using provider-specific default values); in the case that algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
     * parameters must be specified by the caller, an
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
     * <code>InvalidAlgorithmParameterException</code> is raised.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
     * @param key the key for this exemption mechanism
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
     * @param params the algorithm parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
     * @exception InvalidKeyException if the given key is inappropriate for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
     * this exemption mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
     * @exception InvalidAlgorithmParameterException if the given algorithm
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
     * parameters are inappropriate for this exemption mechanism.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
     * @exception ExemptionMechanismException if problem(s) encountered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
     * process of initializing.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
    public final void init(Key key, AlgorithmParameters params)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
            throws InvalidKeyException, InvalidAlgorithmParameterException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
            ExemptionMechanismException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        done = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        initialized = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        keyStored = key;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        exmechSpi.engineInit(key, params);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        initialized = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
     * Generates the exemption mechanism key blob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
     * @return the new buffer with the result key blob.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
     * @exception IllegalStateException if this exemption mechanism is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
     * a wrong state (e.g., has not been initialized).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
     * @exception ExemptionMechanismException if problem(s) encountered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
     * process of generating.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
    public final byte[] genExemptionBlob() throws IllegalStateException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
            ExemptionMechanismException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
            throw new IllegalStateException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
                "ExemptionMechanism not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        byte[] blob = exmechSpi.engineGenExemptionBlob();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        return blob;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
     * Generates the exemption mechanism key blob, and stores the result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
     * the <code>output</code> buffer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
     * <p>If the <code>output</code> buffer is too small to hold the result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
     * a <code>ShortBufferException</code> is thrown. In this case, repeat this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * call with a larger output buffer. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * {@link #getOutputSize(int) getOutputSize} to determine how big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * the output buffer should be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
     * @exception IllegalStateException if this exemption mechanism is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
     * a wrong state (e.g., has not been initialized).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
     * to hold the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
     * @exception ExemptionMechanismException if problem(s) encountered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
     * process of generating.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
    public final int genExemptionBlob(byte[] output)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
            throws IllegalStateException, ShortBufferException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            ExemptionMechanismException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
            ("ExemptionMechanism not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        int n = exmechSpi.engineGenExemptionBlob(output, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
     * Generates the exemption mechanism key blob, and stores the result in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
     * the <code>output</code> buffer, starting at <code>outputOffset</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
     * inclusive.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
     * <p>If the <code>output</code> buffer is too small to hold the result,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
     * a <code>ShortBufferException</code> is thrown. In this case, repeat this
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
     * call with a larger output buffer. Use
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
     * {@link #getOutputSize(int) getOutputSize} to determine how big
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
     * the output buffer should be.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
     * @param output the buffer for the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
     * @param outputOffset the offset in <code>output</code> where the result
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
     * is stored
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
     * @return the number of bytes stored in <code>output</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
     * @exception IllegalStateException if this exemption mechanism is in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
     * a wrong state (e.g., has not been initialized).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
     * @exception ShortBufferException if the given output buffer is too small
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
     * to hold the result.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
     * @exception ExemptionMechanismException if problem(s) encountered in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
     * process of generating.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    public final int genExemptionBlob(byte[] output, int outputOffset)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
            throws IllegalStateException, ShortBufferException,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
            ExemptionMechanismException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        if (!initialized) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
            throw new IllegalStateException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
            ("ExemptionMechanism not initialized");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
        int n = exmechSpi.engineGenExemptionBlob(output, outputOffset);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
        done = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        return n;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
}