Java Code Examples for java.security.Security#insertProviderAt()

The following examples show how to use java.security.Security#insertProviderAt() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HandshakeHashCloneExhaustion.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // Add in a non-cloneable MD5/SHA1/SHA-256 implementation
    Security.insertProviderAt(new MyProvider(), 1);
    // make sure our provider is functioning
    for (String s : mds) {
        MessageDigest md = MessageDigest.getInstance(s);
        String p = md.getProvider().getName();
        if (!p.equals("MyProvider")) {
            throw new RuntimeException("Unexpected provider: " + p);
        }
    }

    if (args.length != 2) {
        throw new Exception(
                "Usage: HandshakeHashCloneExhaustion protocol ciphersuite");
    }

    System.out.println("Testing:  " + args[0] + " " + args[1]);
    protocol = new String [] { args[0] };
    ciphersuite = new String[] { args[1] };

    (new HandshakeHashCloneExhaustion()).run();
}
 
Example 2
Source File: CertificateFactory2Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
protected void setUp() throws Exception {
    super.setUp();
    mProv = (new SpiEngUtils()).new MyProvider("MyCFProvider",
            "Provider for testing", CertificateFactory1Test.srvCertificateFactory
                    .concat(".").concat(defaultAlg),
            CertificateFactoryProviderClass);
    Security.insertProviderAt(mProv, 1);
}
 
Example 3
Source File: CocosBcxApiWrapper.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Initialize SDK
 */
public void init(Context context) {
    //init ThreadPool
    proxy = ThreadManager.getThreadPollProxy();
    // need to init in case some class can not use
    Security.insertProviderAt(new BouncyCastleProvider(), 1);
    // init db dao
    accountDao = new AccountDao(context);
    //  class to deal business logic
    cocosBcxApi = CocosBcxApi.getBcxInstance();
}
 
Example 4
Source File: ZipSigner.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void loadProvider(String providerClassName)
        throws ClassNotFoundException, IllegalAccessException, InstantiationException {
    Class providerClass = Class.forName(providerClassName);
    Provider provider = (Provider) providerClass.newInstance();
    Security.insertProviderAt(provider, 1);
}
 
Example 5
Source File: AddProvider.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    boolean legacy = args[0].equals("2");
    Security.addProvider(new TestProvider("Test1"));
    Security.insertProviderAt(new TestProvider("Test2"), 1);
    try {
        Security.addProvider(new TestProvider("Test3"));
        if (legacy) {
            throw new Exception("Expected SecurityException");
        }
    } catch (SecurityException se) {
        if (!legacy) {
            throw se;
        }
    }
}
 
Example 6
Source File: AddProvider.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    boolean legacy = args[0].equals("2");
    Security.addProvider(new TestProvider("Test1"));
    Security.insertProviderAt(new TestProvider("Test2"), 1);
    try {
        Security.addProvider(new TestProvider("Test3"));
        if (legacy) {
            throw new Exception("Expected SecurityException");
        }
    } catch (SecurityException se) {
        if (!legacy) {
            throw se;
        }
    }
}
 
Example 7
Source File: CommCareApplication.java    From commcare-android with Apache License 2.0 5 votes vote down vote up
private void initTls12IfNeeded() {
    if (useConscryptSecurity()) {
        Security.insertProviderAt(Conscrypt.newProvider(), 1);
    }

    if (Build.VERSION.SDK_INT >= 16 && Build.VERSION.SDK_INT < 22) {
        CommCareNetworkServiceGenerator.customizeRetrofitSetup(new ForceTLS12BuilderConfig());
    }
}
 
Example 8
Source File: AddProvider.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    boolean legacy = args[0].equals("2");
    Security.addProvider(new TestProvider("Test1"));
    Security.insertProviderAt(new TestProvider("Test2"), 1);
    try {
        Security.addProvider(new TestProvider("Test3"));
        if (legacy) {
            throw new Exception("Expected SecurityException");
        }
    } catch (SecurityException se) {
        if (!legacy) {
            throw se;
        }
    }
}
 
Example 9
Source File: AddProvider.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    boolean legacy = args[0].equals("2");
    Security.addProvider(new TestProvider("Test1"));
    Security.insertProviderAt(new TestProvider("Test2"), 1);
    try {
        Security.addProvider(new TestProvider("Test3"));
        if (legacy) {
            throw new Exception("Expected SecurityException");
        }
    } catch (SecurityException se) {
        if (!legacy) {
            throw se;
        }
    }
}
 
Example 10
Source File: ComplexSignatureFields.java    From testarea-itext5 with GNU Affero General Public License v3.0 5 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception
{
    RESULT_FOLDER.mkdirs();

    BouncyCastleProvider bcp = new BouncyCastleProvider();
    //Security.addProvider(bcp);
    Security.insertProviderAt(bcp, 1);

    ks = KeyStore.getInstance(KeyStore.getDefaultType());
    ks.load(new FileInputStream(KEYSTORE), PASSWORD);
    String alias = (String) ks.aliases().nextElement();
    pk = (PrivateKey) ks.getKey(alias, PASSWORD);
    chain = ks.getCertificateChain(alias);
}
 
Example 11
Source File: KeyPairGenerator2Test.java    From j2objc with Apache License 2.0 5 votes vote down vote up
protected void setProv() {
    mProv = (new SpiEngUtils()).new MyProvider("MyKPGenProvider".concat(post),
            "Testing provider", KeyPairGenerator1Test.srvKeyPairGenerator.concat(".")
                    .concat(defaultAlg.concat(post)),
            KeyPairGeneratorProviderClass);
    Security.insertProviderAt(mProv, 1);
}
 
Example 12
Source File: PRNGFixes.java    From Silence with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Installs a Linux PRNG-backed {@code SecureRandom} implementation as the
 * default. Does nothing if the implementation is already the default or if
 * there is not need to install the implementation.
 *
 * @throws SecurityException if the fix is needed but could not be applied.
 */
private static void installLinuxPRNGSecureRandom()
    throws SecurityException {
  if (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2) {
    // No need to apply the fix
    return;
  }

  // Install a Linux PRNG-based SecureRandom implementation as the
  // default, if not yet installed.
  Provider[] secureRandomProviders =
      Security.getProviders("SecureRandom.SHA1PRNG");
  if ((secureRandomProviders == null)
      || (secureRandomProviders.length < 1)
      || (!LinuxPRNGSecureRandomProvider.class.equals(
      secureRandomProviders[0].getClass()))) {
    Security.insertProviderAt(new LinuxPRNGSecureRandomProvider(), 1);
  }

  // Assert that new SecureRandom() and
  // SecureRandom.getInstance("SHA1PRNG") return a SecureRandom backed
  // by the Linux PRNG-based SecureRandom implementation.
  SecureRandom rng1 = new SecureRandom();
  if (!LinuxPRNGSecureRandomProvider.class.equals(
      rng1.getProvider().getClass())) {
    throw new SecurityException(
        "new SecureRandom() backed by wrong Provider: "
            + rng1.getProvider().getClass());
  }

  SecureRandom rng2;
  try {
    rng2 = SecureRandom.getInstance("SHA1PRNG");
  } catch (NoSuchAlgorithmException e) {
    throw new SecurityException("SHA1PRNG not available", e);
  }
  if (!LinuxPRNGSecureRandomProvider.class.equals(
      rng2.getProvider().getClass())) {
    throw new SecurityException(
        "SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong"
            + " Provider: " + rng2.getProvider().getClass());
  }
}
 
Example 13
Source File: MeshManagerApi.java    From Android-nRF-Mesh-Library with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private void initBouncyCastle() {
    Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
}
 
Example 14
Source File: VerifyTimestamp.java    From testarea-itext5 with GNU Affero General Public License v3.0 4 votes vote down vote up
@BeforeClass
public static void setUp() throws Exception {
    BouncyCastleProvider bcp = new BouncyCastleProvider();
    //Security.addProvider(bcp);
    Security.insertProviderAt(bcp, 1);
}
 
Example 15
Source File: PRNGFixes.java    From EosCommander with MIT License 4 votes vote down vote up
/**
 * Installs a Linux PRNG-backed {@code SecureRandom} implementation as the
 * default. Does nothing if the implementation is already the default or if
 * there is not need to install the implementation.
 *
 * @throws SecurityException if the fix is needed but could not be applied.
 */
private static void installLinuxPRNGSecureRandom()
        throws SecurityException {
    if (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2) {
        // No need to apply the fix
        return;
    }

    // Install a Linux PRNG-based SecureRandom implementation as the
    // default, if not yet installed.
    Provider[] secureRandomProviders =
            Security.getProviders("SecureRandom.SHA1PRNG");
    if ((secureRandomProviders == null)
            || (secureRandomProviders.length < 1)
            || (!LinuxPRNGSecureRandomProvider.class.equals(
            secureRandomProviders[0].getClass()))) {
        Security.insertProviderAt(new LinuxPRNGSecureRandomProvider(), 1);
    }

    // Assert that new SecureRandom() and
    // SecureRandom.getInstance("SHA1PRNG") return a SecureRandom backed
    // by the Linux PRNG-based SecureRandom implementation.
    SecureRandom rng1 = new SecureRandom();
    if (!LinuxPRNGSecureRandomProvider.class.equals(
            rng1.getProvider().getClass())) {
        throw new SecurityException(
                "new SecureRandom() backed by wrong Provider: "
                        + rng1.getProvider().getClass());
    }

    SecureRandom rng2;
    try {
        rng2 = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
        throw new SecurityException("SHA1PRNG not available", e);
    }
    if (!LinuxPRNGSecureRandomProvider.class.equals(
            rng2.getProvider().getClass())) {
        throw new SecurityException(
                "SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong"
                        + " Provider: " + rng2.getProvider().getClass());
    }
}
 
Example 16
Source File: PRNGFixes.java    From react-native-securerandom with MIT License 4 votes vote down vote up
/**
 * Installs a Linux PRNG-backed {@code SecureRandom} implementation as the
 * default. Does nothing if the implementation is already the default or if
 * there is not need to install the implementation.
 *
 * @throws \SecurityException if the fix is needed but could not be applied.
 */
private static void installLinuxPRNGSecureRandom()
        throws SecurityException {
  if (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2) {
    // No need to apply the fix
    return;
  }

  // Install a Linux PRNG-based SecureRandom implementation as the
  // default, if not yet installed.
  Provider[] secureRandomProviders =
          Security.getProviders("SecureRandom.SHA1PRNG");
  if ((secureRandomProviders == null)
          || (secureRandomProviders.length < 1)
          || (!LinuxPRNGSecureRandomProvider.class.equals(
          secureRandomProviders[0].getClass()))) {
    Security.insertProviderAt(new LinuxPRNGSecureRandomProvider(), 1);
  }

  // Assert that new SecureRandom() and
  // SecureRandom.getInstance("SHA1PRNG") return a SecureRandom backed
  // by the Linux PRNG-based SecureRandom implementation.
  SecureRandom rng1 = new SecureRandom();
  if (!LinuxPRNGSecureRandomProvider.class.equals(
          rng1.getProvider().getClass())) {
    throw new SecurityException(
            "new SecureRandom() backed by wrong Provider: "
                    + rng1.getProvider().getClass());
  }

  SecureRandom rng2;
  try {
    rng2 = SecureRandom.getInstance("SHA1PRNG");
  } catch (NoSuchAlgorithmException e) {
    throw new SecurityException("SHA1PRNG not available", e);
  }
  if (!LinuxPRNGSecureRandomProvider.class.equals(
          rng2.getProvider().getClass())) {
    throw new SecurityException(
            "SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong"
                    + " Provider: " + rng2.getProvider().getClass());
  }
}
 
Example 17
Source File: CipherStorageKeystoreAesCbcTests.java    From react-native-keychain with MIT License 4 votes vote down vote up
@Before
public void setUp() throws Exception {
  Security.insertProviderAt(provider, 0);
}
 
Example 18
Source File: AesCbcWithIntegrity.java    From Iron with Apache License 2.0 4 votes vote down vote up
/**
 * Installs a Linux PRNG-backed {@code SecureRandom} implementation as
 * the default. Does nothing if the implementation is already the
 * default or if there is not need to install the implementation.
 *
 * @throws SecurityException if the fix is needed but could not be
 *                           applied.
 */
private static void installLinuxPRNGSecureRandom() throws SecurityException {
    if (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2) {
        // No need to apply the fix
        return;
    }

    // Install a Linux PRNG-based SecureRandom implementation as the
    // default, if not yet installed.
    Provider[] secureRandomProviders = Security.getProviders("SecureRandom.SHA1PRNG");

    // Insert and check the provider atomically.
    // The official Android Java libraries use synchronized methods for
    // insertProviderAt, etc., so synchronizing on the class should
    // make things more stable, and prevent race conditions with other
    // versions of this code.
    synchronized (Security.class) {
        if ((secureRandomProviders == null)
                || (secureRandomProviders.length < 1)
                || (!secureRandomProviders[0].getClass().getSimpleName().equals("LinuxPRNGSecureRandomProvider"))) {
            Security.insertProviderAt(new LinuxPRNGSecureRandomProvider(), 1);
        }

        // Assert that new SecureRandom() and
        // SecureRandom.getInstance("SHA1PRNG") return a SecureRandom backed
        // by the Linux PRNG-based SecureRandom implementation.
        SecureRandom rng1 = new SecureRandom();
        if (!rng1.getProvider().getClass().getSimpleName().equals("LinuxPRNGSecureRandomProvider")) {
            /*if (ALLOW_BROKEN_PRNG) {
                Log.w(PrngFixes.class.getSimpleName(),
                        "new SecureRandom() backed by wrong Provider: " + rng1.getProvider().getClass());
                return;
            } else {*/
                throw new SecurityException("new SecureRandom() backed by wrong Provider: "
                        + rng1.getProvider().getClass());
            //}
        }

        SecureRandom rng2 = null;
        try {
            rng2 = SecureRandom.getInstance("SHA1PRNG");
        } catch (NoSuchAlgorithmException e) {
            /*if (ALLOW_BROKEN_PRNG) {
                Log.w(PrngFixes.class.getSimpleName(), "SHA1PRNG not available", e);
                return;
            } else {*/
                new SecurityException("SHA1PRNG not available", e);
            //}
        }
        if (!rng2.getProvider().getClass().getSimpleName().equals("LinuxPRNGSecureRandomProvider")) {
            /*if (ALLOW_BROKEN_PRNG) {
                Log.w(PrngFixes.class.getSimpleName(),
                        "SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong" + " Provider: "
                                + rng2.getProvider().getClass());
                return;
            } else {*/
                throw new SecurityException(
                        "SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong" + " Provider: "
                                + rng2.getProvider().getClass());
            //}
        }
    }
}
 
Example 19
Source File: PRNGFixes.java    From Zom-Android-XMPP with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Installs a Linux PRNG-backed {@code SecureRandom} implementation as the
 * default. Does nothing if the implementation is already the default or if
 * there is not need to install the implementation.
 *
 * @throws SecurityException if the fix is needed but could not be applied.
 */
private static void installLinuxPRNGSecureRandom()
        throws SecurityException {
    if (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2) {
        // No need to apply the fix
        return;
    }

    // Install a Linux PRNG-based SecureRandom implementation as the
    // default, if not yet installed.
    Provider[] secureRandomProviders =
            Security.getProviders("SecureRandom.SHA1PRNG");
    if ((secureRandomProviders == null)
            || (secureRandomProviders.length < 1)
            || (!LinuxPRNGSecureRandomProvider.class.equals(
                    secureRandomProviders[0].getClass()))) {
        Security.insertProviderAt(new LinuxPRNGSecureRandomProvider(), 1);
    }

    // Assert that new SecureRandom() and
    // SecureRandom.getInstance("SHA1PRNG") return a SecureRandom backed
    // by the Linux PRNG-based SecureRandom implementation.
    SecureRandom rng1 = new SecureRandom();
    if (!LinuxPRNGSecureRandomProvider.class.equals(
            rng1.getProvider().getClass())) {
        throw new SecurityException(
                "new SecureRandom() backed by wrong Provider: "
                        + rng1.getProvider().getClass());
    }

    SecureRandom rng2;
    try {
        rng2 = SecureRandom.getInstance("SHA1PRNG");
    } catch (NoSuchAlgorithmException e) {
        throw new SecurityException("SHA1PRNG not available", e);
    }
    if (!LinuxPRNGSecureRandomProvider.class.equals(
            rng2.getProvider().getClass())) {
        throw new SecurityException(
                "SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong"
                + " Provider: " + rng2.getProvider().getClass());
    }
}
 
Example 20
Source File: PRNGFixes.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
/**
 * Installs a Linux PRNG-backed {@code SecureRandom} implementation as the
 * default. Does nothing if the implementation is already the default or if
 * there is not need to install the implementation.
 *
 * @throws SecurityException if the fix is needed but could not be applied.
 */
private static void installLinuxPRNGSecureRandom()
    throws SecurityException {
  if (Build.VERSION.SDK_INT > VERSION_CODE_JELLY_BEAN_MR2) {
    // No need to apply the fix
    return;
  }

  // Install a Linux PRNG-based SecureRandom implementation as the
  // default, if not yet installed.
  Provider[] secureRandomProviders =
      Security.getProviders("SecureRandom.SHA1PRNG");
  if ((secureRandomProviders == null)
      || (secureRandomProviders.length < 1)
      || (!LinuxPRNGSecureRandomProvider.class.equals(
      secureRandomProviders[0].getClass()))) {
    Security.insertProviderAt(new LinuxPRNGSecureRandomProvider(), 1);
  }

  // Assert that new SecureRandom() and
  // SecureRandom.getInstance("SHA1PRNG") return a SecureRandom backed
  // by the Linux PRNG-based SecureRandom implementation.
  SecureRandom rng1 = new SecureRandom();
  if (!LinuxPRNGSecureRandomProvider.class.equals(
      rng1.getProvider().getClass())) {
    throw new SecurityException(
        "new SecureRandom() backed by wrong Provider: "
            + rng1.getProvider().getClass());
  }

  SecureRandom rng2;
  try {
    rng2 = SecureRandom.getInstance("SHA1PRNG");
  } catch (NoSuchAlgorithmException e) {
    throw new SecurityException("SHA1PRNG not available", e);
  }
  if (!LinuxPRNGSecureRandomProvider.class.equals(
      rng2.getProvider().getClass())) {
    throw new SecurityException(
        "SecureRandom.getInstance(\"SHA1PRNG\") backed by wrong"
            + " Provider: " + rng2.getProvider().getClass());
  }
}