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

org.bouncycastle.its.jcajce.ClassUtil Maven / Gradle / Ivy

Go to download

The Bouncy Castle Java APIs for CMS, PKCS, EAC, TSP, CMP, CRMF, OCSP, and certificate generation. This jar contains APIs for JDK 1.5 to JDK 1.8. The APIs can be used in conjunction with a JCE/JCA provider such as the one provided with the Bouncy Castle Cryptography APIs.

There is a newer version: 1.79
Show newest version
package org.bouncycastle.its.jcajce;

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

import org.bouncycastle.jcajce.spec.AEADParameterSpec;
import org.bouncycastle.util.Integers;

class ClassUtil
{
    private static final Class gcmSpecClass = loadClass(ClassUtil.class, "javax.crypto.spec.GCMParameterSpec");

    public static AlgorithmParameterSpec getGCMSpec(final byte[] nonce, final int tagSize)
    {
        if (gcmSpecClass != null)
        {
              try
              {
                  return (AlgorithmParameterSpec)AccessController.doPrivileged(new PrivilegedAction()
                  {
                      @Override
                      public Object run()
                      {
                          try
                          {
                              Constructor cons = gcmSpecClass.getConstructor(new Class[] { Integer.TYPE, byte[].class });

                              return cons.newInstance(Integers.valueOf(tagSize), nonce);
                          }
                          catch (NoSuchMethodException e)
                          {
                              throw new IllegalStateException("no matching constructor: " + e.getMessage());
                          }
                          catch (Exception e)
                          {
                              throw new IllegalStateException("constructor failed" + e.getMessage());
                          }
                      }
                  });
              }
              catch (IllegalStateException e)
              {
		  // ignore
              }
        }
        return new AEADParameterSpec(nonce, tagSize);
    }

    static Class loadClass(Class sourceClass, final String className)
        {
            try
            {
                ClassLoader loader = sourceClass.getClassLoader();

                if (loader != null)
                {
                    return loader.loadClass(className);
                }
                else
                {
                    return (Class)AccessController.doPrivileged(new PrivilegedAction()
                    {
                        public Object run()
                        {
                            try
                            {
                                return Class.forName(className);
                            }
                            catch (Exception e)
                            {
                                // ignore - maybe log?
                            }

                            return null;
                        }
                    });
                }
            }
            catch (ClassNotFoundException e)
            {
                // ignore - maybe log?
            }

            return null;
        }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy