2677 lines · 110.0 KB · Java
RawDownload
        // Check if key size and default parameters are within legal limits        AlgorithmParameterSpec params;        try {            params = getAlgorithmParameterSpec(checkSpi.engineGetParameters());        } catch (InvalidParameterSpecException ipse) {            throw new InvalidKeyException                ("Unsupported default algorithm parameters");        }        if (!passCryptoPermCheck(checkSpi, key, params)) {            throw new InvalidKeyException(                "Illegal key size or default parameters");        }    }     private void checkCryptoPerm(CipherSpi checkSpi, Key key,            AlgorithmParameterSpec params) throws InvalidKeyException,            InvalidAlgorithmParameterException {        if (cryptoPerm == CryptoAllPermission.INSTANCE) {            return;        }        // Determine keysize and check if it is within legal limits        if (!passCryptoPermCheck(checkSpi, key, null)) {            throw new InvalidKeyException("Illegal key size");        }        if ((params != null) && (!passCryptoPermCheck(checkSpi, key, params))) {            throw new InvalidAlgorithmParameterException("Illegal parameters");        }    }     private void checkCryptoPerm(CipherSpi checkSpi, Key key,            AlgorithmParameters params)            throws InvalidKeyException, InvalidAlgorithmParameterException {        if (cryptoPerm == CryptoAllPermission.INSTANCE) {            return;        }        // Convert the specified parameters into specs and then delegate.        AlgorithmParameterSpec pSpec;        try {            pSpec = getAlgorithmParameterSpec(params);        } catch (InvalidParameterSpecException ipse) {            throw new InvalidAlgorithmParameterException                ("Failed to retrieve algorithm parameter specification");        }        checkCryptoPerm(checkSpi, key, pSpec);    }     private boolean passCryptoPermCheck(CipherSpi checkSpi, Key key,                                        AlgorithmParameterSpec params)            throws InvalidKeyException {        String em = cryptoPerm.getExemptionMechanism();        int keySize = checkSpi.engineGetKeySize(key);        // Use the "algorithm" component of the cipher        // transformation so that the perm check would        // work when the key has the "aliased" algo.        String algComponent;        int index = transformation.indexOf('/');        if (index != -1) {            algComponent = transformation.substring(0, index);        } else {            algComponent = transformation;        }        CryptoPermission checkPerm =            new CryptoPermission(algComponent, keySize, params, em);         if (!cryptoPerm.implies(checkPerm)) {            if (debug != null) {                debug.println("Crypto Permission check failed");                debug.println("granted: " + cryptoPerm);                debug.println("requesting: " + checkPerm);            }            return false;        }        if (exmech == null) {            return true;        }        try {            if (!exmech.isCryptoAllowed(key)) {                if (debug != null) {                    debug.println(exmech.getName() + " isn't enforced");                }                return false;            }        } catch (ExemptionMechanismException eme) {            if (debug != null) {                debug.println("Cannot determine whether "+                              exmech.getName() + " has been enforced");                eme.printStackTrace();            }            return false;        }        return true;    }     // check if opmode is one of the defined constants    // throw InvalidParameterExeption if not    private static void checkOpmode(int opmode) {        if ((opmode < ENCRYPT_MODE) || (opmode > UNWRAP_MODE)) {            throw new InvalidParameterException("Invalid operation mode");        }    }     /**     * Initializes this cipher with a key.     *     * <p>The cipher is initialized for one of the following four operations:     * encryption, decryption, key wrapping or key unwrapping, depending     * on the value of <code>opmode</code>.     *     * <p>If this cipher requires any algorithm parameters that cannot be     * derived from the given <code>key</code>, the underlying cipher     * implementation is supposed to generate the required parameters itself     * (using provider-specific default or random values) if it is being     * initialized for encryption or key wrapping, and raise an     * <code>InvalidKeyException</code> if it is being     * initialized for decryption or key unwrapping.     * The generated parameters can be retrieved using     * {@link #getParameters() getParameters} or     * {@link #getIV() getIV} (if the parameter is an IV).     *     * <p>If this cipher requires algorithm parameters that cannot be     * derived from the input parameters, and there are no reasonable     * provider-specific default values, initialization will     * necessarily fail.     *     * <p>If this cipher (including its underlying feedback or padding scheme)     * requires any random bytes (e.g., for parameter generation), it will get     * them using the {@link SecureRandom <code>SecureRandom</code>}     * implementation of the highest-priority     * installed provider as the source of randomness.     * (If none of the installed providers supply an implementation of     * SecureRandom, a system-provided source of randomness will be used.)     *     * <p>Note that when a Cipher object is initialized, it loses all     * previously-acquired state. In other words, initializing a Cipher is     * equivalent to creating a new instance of that Cipher and initializing     * it.     *     * @param opmode the operation mode of this cipher (this is one of     * the following:     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)     * @param key the key     *     * @exception InvalidKeyException if the given key is inappropriate for     * initializing this cipher, or requires     * algorithm parameters that cannot be     * determined from the given key, or if the given key has a keysize that     * exceeds the maximum allowable keysize (as determined from the     * configured jurisdiction policy files).     */    public final void init(int opmode, Key key) throws InvalidKeyException {        init(opmode, key, JceSecurity.RANDOM);    }     /**     * Initializes this cipher with a key and a source of randomness.     *     * <p>The cipher is initialized for one of the following four operations:     * encryption, decryption, key wrapping or  key unwrapping, depending     * on the value of <code>opmode</code>.     *     * <p>If this cipher requires any algorithm parameters that cannot be     * derived from the given <code>key</code>, the underlying cipher     * implementation is supposed to generate the required parameters itself     * (using provider-specific default or random values) if it is being     * initialized for encryption or key wrapping, and raise an     * <code>InvalidKeyException</code> if it is being     * initialized for decryption or key unwrapping.     * The generated parameters can be retrieved using     * {@link #getParameters() getParameters} or     * {@link #getIV() getIV} (if the parameter is an IV).     *     * <p>If this cipher requires algorithm parameters that cannot be     * derived from the input parameters, and there are no reasonable     * provider-specific default values, initialization will     * necessarily fail.     *     * <p>If this cipher (including its underlying feedback or padding scheme)     * requires any random bytes (e.g., for parameter generation), it will get     * them from <code>random</code>.     *     * <p>Note that when a Cipher object is initialized, it loses all     * previously-acquired state. In other words, initializing a Cipher is     * equivalent to creating a new instance of that Cipher and initializing     * it.     *     * @param opmode the operation mode of this cipher (this is one of the     * following:     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)     * @param key the encryption key     * @param random the source of randomness     *     * @exception InvalidKeyException if the given key is inappropriate for     * initializing this cipher, or requires     * algorithm parameters that cannot be     * determined from the given key, or if the given key has a keysize that     * exceeds the maximum allowable keysize (as determined from the     * configured jurisdiction policy files).     */    public final void init(int opmode, Key key, SecureRandom random)            throws InvalidKeyException    {        initialized = false;        checkOpmode(opmode);         if (spi != null) {            checkCryptoPerm(spi, key);            spi.engineInit(opmode, key, random);        } else {            try {                chooseProvider(I_KEY, opmode, key, null, null, random);            } catch (InvalidAlgorithmParameterException e) {                // should never occur                throw new InvalidKeyException(e);            }        }         initialized = true;        this.opmode = opmode;    }     /**     * Initializes this cipher with a key and a set of algorithm     * parameters.     *     * <p>The cipher is initialized for one of the following four operations:     * encryption, decryption, key wrapping or  key unwrapping, depending     * on the value of <code>opmode</code>.     *     * <p>If this cipher requires any algorithm parameters and     * <code>params</code> is null, the underlying cipher implementation is     * supposed to generate the required parameters itself (using     * provider-specific default or random values) if it is being     * initialized for encryption or key wrapping, and raise an     * <code>InvalidAlgorithmParameterException</code> if it is being     * initialized for decryption or key unwrapping.     * The generated parameters can be retrieved using     * {@link #getParameters() getParameters} or     * {@link #getIV() getIV} (if the parameter is an IV).     *     * <p>If this cipher requires algorithm parameters that cannot be     * derived from the input parameters, and there are no reasonable     * provider-specific default values, initialization will     * necessarily fail.     *     * <p>If this cipher (including its underlying feedback or padding scheme)     * requires any random bytes (e.g., for parameter generation), it will get     * them using the {@link SecureRandom <code>SecureRandom</code>}     * implementation of the highest-priority     * installed provider as the source of randomness.     * (If none of the installed providers supply an implementation of     * SecureRandom, a system-provided source of randomness will be used.)     *     * <p>Note that when a Cipher object is initialized, it loses all     * previously-acquired state. In other words, initializing a Cipher is     * equivalent to creating a new instance of that Cipher and initializing     * it.     *     * @param opmode the operation mode of this cipher (this is one of the     * following:     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)     * @param key the encryption key     * @param params the algorithm parameters     *     * @exception InvalidKeyException if the given key is inappropriate for     * initializing this cipher, or its keysize exceeds the maximum allowable     * keysize (as determined from the configured jurisdiction policy files).     * @exception InvalidAlgorithmParameterException if the given algorithm     * parameters are inappropriate for this cipher,     * or this cipher requires     * algorithm parameters and <code>params</code> is null, or the given     * algorithm parameters imply a cryptographic strength that would exceed     * the legal limits (as determined from the configured jurisdiction     * policy files).     */    public final void init(int opmode, Key key, AlgorithmParameterSpec params)            throws InvalidKeyException, InvalidAlgorithmParameterException    {        init(opmode, key, params, JceSecurity.RANDOM);    }     /**     * Initializes this cipher with a key, a set of algorithm     * parameters, and a source of randomness.     *     * <p>The cipher is initialized for one of the following four operations:     * encryption, decryption, key wrapping or  key unwrapping, depending     * on the value of <code>opmode</code>.     *     * <p>If this cipher requires any algorithm parameters and     * <code>params</code> is null, the underlying cipher implementation is     * supposed to generate the required parameters itself (using     * provider-specific default or random values) if it is being     * initialized for encryption or key wrapping, and raise an     * <code>InvalidAlgorithmParameterException</code> if it is being     * initialized for decryption or key unwrapping.     * The generated parameters can be retrieved using     * {@link #getParameters() getParameters} or     * {@link #getIV() getIV} (if the parameter is an IV).     *     * <p>If this cipher requires algorithm parameters that cannot be     * derived from the input parameters, and there are no reasonable     * provider-specific default values, initialization will     * necessarily fail.     *     * <p>If this cipher (including its underlying feedback or padding scheme)     * requires any random bytes (e.g., for parameter generation), it will get     * them from <code>random</code>.     *     * <p>Note that when a Cipher object is initialized, it loses all     * previously-acquired state. In other words, initializing a Cipher is     * equivalent to creating a new instance of that Cipher and initializing     * it.     *     * @param opmode the operation mode of this cipher (this is one of the     * following:     * <code>ENCRYPT_MODE</code>, <code>DECRYPT_MODE</code>,     * <code>WRAP_MODE</code> or <code>UNWRAP_MODE</code>)     * @param key the encryption key     * @param params the algorithm parameters     * @param random the source of randomness     *     * @exception InvalidKeyException if the given key is inappropriate for     * initializing this cipher, or its keysize exceeds the maximum allowable     * keysize (as determined from the configured jurisdiction policy files).     * @exception InvalidAlgorithmParameterException if the given algorithm     * parameters are inappropriate for this cipher,     * or this cipher requires     * algorithm parameters and <code>params</code> is null, or the given     * algorithm parameters imply a cryptographic strength that would exceed     * the legal limits (as determined from the configured jurisdiction     * policy files).     */    public final void init(int opmode, Key key, AlgorithmParameterSpec params,                           SecureRandom random)            throws InvalidKeyException, InvalidAlgorithmParameterException    {        initialized = false;        checkOpmode(opmode);         if (spi != null) {            checkCryptoPerm(spi, key, params);            spi.engineInit(opmode, key, params, random);        } else {            chooseProvider(I_PARAMSPEC, opmode, key, params, null, random);        }         initialized = true;        this.opmode = opmode;    }     /**     * Initializes this cipher with a key and a set of algorithm     * parameters.     *     * <p>The cipher is initialized for one of the following four operations:     * encryption, decryption, key wrapping or  key unwrapping, depending     * on the value of <code>opmode</code>.     *     * <p>If this cipher requires any algorithm parameters and     * <code>params</code> is null, the underlying cipher implementation is     * supposed to generate the required parameters itself (using     * provider-specific default or random values) if it is being     * initialized for encryption or key wrapping, and raise an     * <code>InvalidAlgorithmParameterException</code> if it is being     * initialized for decryption or key unwrapping.     * The generated parameters can be retrieved using     * {@link #getParameters() getParameters} or     * {@link #getIV() getIV} (if the parameter is an IV).     *     * <p>If this cipher requires algorithm parameters that cannot be     * derived from the input parameters, and there are no reasonable     * provider-specific default values, initialization will     * necessarily fail.     *     * <p>If this cipher (including its underlying feedback or padding scheme)     * requires any random bytes (e.g., for parameter generation), it will get     * them using the {@link SecureRandom <code>SecureRandom</code>}     * implementation of the highest-priority     * installed provider as the source of randomness.     * (If none of the installed providers supply an implementation of     * SecureRandom, a system-provided source of randomness will be used.)     *     * <p>Note that when a Cipher object is initialized, it loses all     * previously-acquired state. In other words, initializing a Cipher is     * equivalent to creating a new instance of that Cipher and initializing     * it.     *     * @param opmode the operation mode of this cipher (this is one of the     * following: <code>ENCRYPT_MODE</code>,     * <code>DECRYPT_MODE</code>, <code>WRAP_MODE</code>     * or <code>UNWRAP_MODE</code>)     * @param key the encryption key     * @param params the algorithm parameters     *     * @exception InvalidKeyException if the given key is inappropriate for     * initializing this cipher, or its keysize exceeds the maximum allowable     * keysize (as determined from the configured jurisdiction policy files).     * @exception InvalidAlgorithmParameterException if the given algorithm     * parameters are inappropriate for this cipher,     * or this cipher requires     * algorithm parameters and <code>params</code> is null, or the given     * algorithm parameters imply a cryptographic strength that would exceed     * the legal limits (as determined from the configured jurisdiction     * policy files).     */    public final void init(int opmode, Key key, AlgorithmParameters params)            throws InvalidKeyException, InvalidAlgorithmParameterException    {        init(opmode, key, params, JceSecurity.RANDOM);    }     /**     * Initializes this cipher with a key, a set of algorithm     * parameters, and a source of randomness.     *     * <p>The cipher is initialized for one of the following four operations:     * encryption, decryption, key wrapping or  key unwrapping, depending     * on the value of <code>opmode</code>.     *     * <p>If this cipher requires any algorithm parameters and     * <code>params</code> is null, the underlying cipher implementation is     * supposed to generate the required parameters itself (using     * provider-specific default or random values) if it is being     * initialized for encryption or key wrapping, and raise an     * <code>InvalidAlgorithmParameterException</code> if it is being     * initialized for decryption or key unwrapping.     * The generated parameters can be retrieved using     * {@link #getParameters() getParameters} or     * {@link #getIV() getIV} (if the parameter is an IV).     *     * <p>If this cipher requires algorithm parameters that cannot be     * derived from the input parameters, and there are no reasonable     * provider-specific default values, initialization will     * necessarily fail.     *     * <p>If this cipher (including its underlying feedback or padding scheme)     * requires any random bytes (e.g., for parameter generation), it will get     * them from <code>random</code>.     *     * <p>Note that when a Cipher object is initialized, it loses all     * previously-acquired state. In other words, initializing a Cipher is     * equivalent to creating a new instance of that Cipher and initializing     * it.     *     * @param opmode the operation mode of this cipher (this is one of the     * following: <code>ENCRYPT_MODE</code>,     * <code>DECRYPT_MODE</code>, <code>WRAP_MODE</code>     * or <code>UNWRAP_MODE</code>)     * @param key the encryption key     * @param params the algorithm parameters     * @param random the source of randomness     *     * @exception InvalidKeyException if the given key is inappropriate for     * initializing this cipher, or its keysize exceeds the maximum allowable     * keysize (as determined from the configured jurisdiction policy files).     * @exception InvalidAlgorithmParameterException if the given algorithm     * parameters are inappropriate for this cipher,     * or this cipher requires     * algorithm parameters and <code>params</code> is null, or the given     * algorithm parameters imply a cryptographic strength that would exceed     * the legal limits (as determined from the configured jurisdiction     * policy files).     */    public final void init(int opmode, Key key, AlgorithmParameters params,                           SecureRandom random)            throws InvalidKeyException, InvalidAlgorithmParameterException    {        initialized = false;        checkOpmode(opmode);         if (spi != null) {            checkCryptoPerm(spi, key, params);            spi.engineInit(opmode, key, params, random);        } else {            chooseProvider(I_PARAMS, opmode, key, null, params, random);        }         initialized = true;        this.opmode = opmode;    }     /**     * Initializes this cipher with the public key from the given certificate.     * <p> The cipher is initialized for one of the following four operations:     * encryption, decryption, key wrapping or  key unwrapping, depending     * on the value of <code>opmode</code>.     *     * <p>If the certificate is of type X.509 and has a <i>key usage</i>     * extension field marked as critical, and the value of the <i>key usage</i>     * extension field implies that the public key in     * the certificate and its corresponding private key are not     * supposed to be used for the operation represented by the value     * of <code>opmode</code>,     * an <code>InvalidKeyException</code>     * is thrown.     *     * <p> If this cipher requires any algorithm parameters that cannot be

Lines 1001–1500 of 2677. Previous lines

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t