All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.bouncycastle.tls.crypto.impl.jcajce.GCMUtil Maven / Gradle / Ivy

There is a newer version: 1.0.6
Show newest version
package org.bouncycastle.tls.crypto.impl.jcajce;

import java.lang.reflect.Constructor;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.security.spec.AlgorithmParameterSpec;

import org.bouncycastle.util.Integers;

class GCMUtil
{
    static final Constructor gcmParameterSpec = getConstructor();

    static AlgorithmParameterSpec createGCMParameterSpec(final int tLen, final byte[] src)
        throws Exception
    {
        if (gcmParameterSpec == null)
        {
            throw new IllegalStateException();
        }

        return AccessController.doPrivileged(new PrivilegedExceptionAction()
        {
            public AlgorithmParameterSpec run()
                throws Exception
            {
                return gcmParameterSpec.newInstance(new Object[]{ Integers.valueOf(tLen), src });
            }
        });
    }

    static boolean isGCMParameterSpecAvailable()
    {
        return gcmParameterSpec != null;
    }

    private static Constructor getConstructor()
    {
        return AccessController.doPrivileged(new PrivilegedAction>()
        {
            public Constructor run()
            {
                try
                {
                    String className = "javax.crypto.spec.GCMParameterSpec";
                    //
                    // needs to be exactly the same as in provider
                    //
                    ClassLoader classLoader = GCMUtil.class.getClassLoader();
                    if (classLoader == null)
                    {
                        classLoader = ClassLoader.getSystemClassLoader();
                    }

                    Class clazz = classLoader.loadClass(className);
                    if (clazz != null && AlgorithmParameterSpec.class.isAssignableFrom(clazz))
                    {
                        @SuppressWarnings("unchecked")
                        Class typedClazz = (Class)clazz;
                        return typedClazz.getConstructor(new Class[]{ Integer.TYPE, byte[].class });
                    }
                }
                catch (Exception e)
                {
                }

                return null;
            }
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy