FileDocCategorySizeDatePackage
WrapCipherSpi.javaAPI DocAndroid 1.5 API13376Wed May 06 22:41:06 BST 2009org.bouncycastle.jce.provider

WrapCipherSpi

public abstract class WrapCipherSpi extends CipherSpi implements PBE

Fields Summary
private Class[]
availableSpecs
protected int
pbeType
protected int
pbeHash
protected int
pbeKeySize
protected int
pbeIvSize
protected AlgorithmParameters
engineParams
protected org.bouncycastle.crypto.Wrapper
wrapEngine
Constructors Summary
protected WrapCipherSpi()


     
    
    
protected WrapCipherSpi(org.bouncycastle.crypto.Wrapper wrapEngine)

        this.wrapEngine = wrapEngine;
    
Methods Summary
protected byte[]engineDoFinal(byte[] input, int inputOffset, int inputLen)

        return null;
    
protected intengineDoFinal(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)

        return 0;
    
protected intengineGetBlockSize()

        return 0;
    
protected byte[]engineGetIV()

        return null;
    
protected intengineGetKeySize(java.security.Key key)

        return key.getEncoded().length;
    
protected intengineGetOutputSize(int inputLen)

        return -1;
    
protected java.security.AlgorithmParametersengineGetParameters()

        return null;
    
protected voidengineInit(int opmode, java.security.Key key, java.security.spec.AlgorithmParameterSpec params, java.security.SecureRandom random)

        CipherParameters        param;

        if (key instanceof JCEPBEKey)
        {
            JCEPBEKey   k = (JCEPBEKey)key;
            
            if (params instanceof PBEParameterSpec)
            {
                param = PBE.Util.makePBEParameters(k, params, wrapEngine.getAlgorithmName());
            }
            else if (k.getParam() != null)
            {
                param = k.getParam();
            }
            else
            {
                throw new InvalidAlgorithmParameterException("PBE requires PBE parameters to be set.");
            }
        }
        else
        {
            param = new KeyParameter(key.getEncoded());
        }

        if (params instanceof javax.crypto.spec.IvParameterSpec)
        {
            IvParameterSpec iv = (IvParameterSpec) params;
            CipherParameters paramPlusIV = new ParametersWithIV(param, iv.getIV());
            param = paramPlusIV;
        }

        switch (opmode)
        {
        case Cipher.WRAP_MODE:
            wrapEngine.init(true, param);
            break;
        case Cipher.UNWRAP_MODE:
            wrapEngine.init(false, param);
            break;
        case Cipher.ENCRYPT_MODE:
        case Cipher.DECRYPT_MODE:
            throw new IllegalArgumentException("engine only valid for wrapping");
        default:
            System.out.println("eeek!");
        }
    
protected voidengineInit(int opmode, java.security.Key key, java.security.AlgorithmParameters params, java.security.SecureRandom random)

        AlgorithmParameterSpec  paramSpec = null;

        if (params != null)
        {
            for (int i = 0; i != availableSpecs.length; i++)
            {
                try
                {
                    paramSpec = params.getParameterSpec(availableSpecs[i]);
                    break;
                }
                catch (Exception e)
                {
                    continue;
                }
            }

            if (paramSpec == null)
            {
                throw new InvalidAlgorithmParameterException("can't handle parameter " + params.toString());
            }
        }

        engineParams = params;
        engineInit(opmode, key, paramSpec, random);
    
protected voidengineInit(int opmode, java.security.Key key, java.security.SecureRandom random)

        try
        {
            engineInit(opmode, key, (AlgorithmParameterSpec)null, random);
        }
        catch (InvalidAlgorithmParameterException e)
        {
            throw new IllegalArgumentException(e.getMessage());
        }
    
protected voidengineSetMode(java.lang.String mode)

        throw new NoSuchAlgorithmException("can't support mode " + mode);
    
protected voidengineSetPadding(java.lang.String padding)

        throw new NoSuchPaddingException("Padding " + padding + " unknown.");
    
protected java.security.KeyengineUnwrap(byte[] wrappedKey, java.lang.String wrappedKeyAlgorithm, int wrappedKeyType)

        // BEGIN android-note
        // added ShortBufferException to throws statement
        // END android-note
        byte[] encoded = null;
        try
        {
            if (wrapEngine == null)
            {
                encoded = engineDoFinal(wrappedKey, 0, wrappedKey.length);
            }
            else
            {
                encoded = wrapEngine.unwrap(wrappedKey, 0, wrappedKey.length);
            }
        }
        catch (InvalidCipherTextException e)
        {
            throw new InvalidKeyException(e.getMessage());
        }
        catch (BadPaddingException e)
        {
            throw new InvalidKeyException(e.getMessage());
        }
        catch (IllegalBlockSizeException e2)
        {
            throw new InvalidKeyException(e2.getMessage());
        }

        if (wrappedKeyType == Cipher.SECRET_KEY)
        {
            return new SecretKeySpec(encoded, wrappedKeyAlgorithm);
        }
        else if (wrappedKeyAlgorithm.equals("") && wrappedKeyType == Cipher.PRIVATE_KEY)
        {
            /*
             * The caller doesnt know the algorithm as it is part of
             * the encrypted data.
             */
            ASN1InputStream bIn = new ASN1InputStream(encoded);
            PrivateKey      privKey = null;

            try
            {
                ASN1Sequence         s = (ASN1Sequence)bIn.readObject();
                PrivateKeyInfo       in = new PrivateKeyInfo(s);

                DERObjectIdentifier  oid = in.getAlgorithmId().getObjectId();

                // BEGIN android-removed
                // if (oid.equals(X9ObjectIdentifiers.id_ecPublicKey))
                // {
                //     privKey = new JCEECPrivateKey(in);
                // }
                // else if (oid.equals(CryptoProObjectIdentifiers.gostR3410_94))
                // {
                //     privKey = new JDKGOST3410PrivateKey(in);
                // }
                // else if (oid.equals(X9ObjectIdentifiers.id_dsa))
                // END android-removed
                // BEGIN android-added
                if (oid.equals(X9ObjectIdentifiers.id_dsa))
                // END android-added
                {
                    privKey = new JDKDSAPrivateKey(in);
                }
                else if (oid.equals(PKCSObjectIdentifiers.dhKeyAgreement))
                {
                    privKey = new JCEDHPrivateKey(in);
                }
                else if (oid.equals(X9ObjectIdentifiers.dhpublicnumber))
                {
                    privKey = new JCEDHPrivateKey(in);
                }
                else    // the old standby!
                {
                    privKey = new JCERSAPrivateCrtKey(in);
                }
            }
            catch (Exception e)
            {
                throw new InvalidKeyException("Invalid key encoding.");
            }

            return privKey;
        }
        else
        {
            try
            {
                KeyFactory kf = KeyFactory.getInstance(wrappedKeyAlgorithm, "BC");

                if (wrappedKeyType == Cipher.PUBLIC_KEY)
                {
                    return kf.generatePublic(new X509EncodedKeySpec(encoded));
                }
                else if (wrappedKeyType == Cipher.PRIVATE_KEY)
                {
                    return kf.generatePrivate(new PKCS8EncodedKeySpec(encoded));
                }
            }
            catch (NoSuchProviderException e)
            {
                throw new InvalidKeyException("Unknown key type " + e.getMessage());
            }
            // BEGIN android-removed
            // catch (NoSuchAlgorithmException e)
            // {
            //     throw new InvalidKeyException("Unknown key type " + e.getMessage());
            // }
            // END android-removed
            catch (InvalidKeySpecException e2)
            {
                throw new InvalidKeyException("Unknown key type " + e2.getMessage());
            }

            throw new InvalidKeyException("Unknown key type " + wrappedKeyType);
        }
    
protected byte[]engineUpdate(byte[] input, int inputOffset, int inputLen)

        throw new RuntimeException("not supported for wrapping");
    
protected intengineUpdate(byte[] input, int inputOffset, int inputLen, byte[] output, int outputOffset)

        throw new RuntimeException("not supported for wrapping");
    
protected byte[]engineWrap(java.security.Key key)

        byte[] encoded = key.getEncoded();
        if (encoded == null)
        {
            throw new InvalidKeyException("Cannot wrap key, null encoding.");
        }

        try
        {
            if (wrapEngine == null)
            {
                return engineDoFinal(encoded, 0, encoded.length);
            }
            else
            {
                return wrapEngine.wrap(encoded, 0, encoded.length);
            }
        }
        catch (BadPaddingException e)
        {
            throw new IllegalBlockSizeException(e.getMessage());
        }