コード例 #1
0
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "newInstance",
      args = {java.lang.Object.class})
  public void testNewInstance() throws Exception {
    Provider p = new MyProvider();
    Provider.Service s =
        new Provider.Service(
            p,
            "SecureRandom",
            "algorithm",
            "org.apache.harmony.security.tests.support.RandomImpl",
            null,
            null);

    Object o = s.newInstance(null);
    assertTrue("incorrect instance", o instanceof RandomImpl);

    try {
      o = s.newInstance(new Object());
      fail("No expected NoSuchAlgorithmException");
    } catch (NoSuchAlgorithmException e) {
    }
  }
コード例 #2
0
ファイル: SecurityHelper.java プロジェクト: Jared-Prime/jruby
 // NOTE: none (at least for BC 1.47)
 private static String getSecureRandomAlgorithm(final Provider provider) {
   for (Provider.Service service : provider.getServices()) {
     if ("SecureRandom".equals(service.getType())) {
       return service.getAlgorithm();
     }
   }
   return null;
 }
コード例 #3
0
 @TestTargetNew(
     level = TestLevel.COMPLETE,
     notes = "",
     method = "supportsParameter",
     args = {java.lang.Object.class})
 public void testSupportsParameter() {
   Provider p = new MyProvider();
   Provider.Service s1 = new Provider.Service(p, "type", "algorithm", "className", null, null);
   assertTrue(s1.supportsParameter(null));
   assertTrue(s1.supportsParameter(new Object()));
 }
コード例 #4
0
 /**
  * Get a password factory instance. The returned password factory object will implement the given
  * algorithm.
  *
  * @param algorithm the name of the algorithm
  * @param providerSupplier the provider supplier to search
  * @return a password factory instance
  * @throws NoSuchAlgorithmException if the given algorithm has no available implementations
  */
 public static PasswordFactory getInstance(String algorithm, Supplier<Provider[]> providerSupplier)
     throws NoSuchAlgorithmException {
   for (Provider provider : providerSupplier.get()) {
     final Provider.Service service = provider.getService("PasswordFactory", algorithm);
     if (service != null) {
       return new PasswordFactory(
           (PasswordFactorySpi) service.newInstance(null), provider, algorithm);
     }
   }
   throw log.noSuchAlgorithmInvalidAlgorithm(algorithm);
 }
コード例 #5
0
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "toString",
      args = {})
  public void testToString() {
    Provider p = new MyProvider();
    Provider.Service s1 = new Provider.Service(p, "type", "algorithm", "className", null, null);
    s1.toString();

    Provider.Service s2 =
        new Provider.Service(
            p, "SecureRandom", "algorithm", "tests.java.security.support.RandomImpl", null, null);
    s2.toString();
  }
コード例 #6
0
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "getType",
      args = {})
  public void testGetType() {
    Provider p = new MyProvider();
    Provider.Service s1 = new Provider.Service(p, "type", "algorithm", "className", null, null);
    assertTrue(s1.getType().equals("type"));

    Provider.Service s2 =
        new Provider.Service(
            p, "SecureRandom", "algorithm", "tests.java.security.support.RandomImpl", null, null);
    assertTrue(s2.getType().equals("SecureRandom"));
  }
コード例 #7
0
 void initiateProviderKeyStore(Provider provider) throws Exception {
   String prov = null;
   for (Provider.Service ps : provider.getServices()) {
     if (ps.getType().equals("KeyStore")) {
       prov = ps.getAlgorithm();
     }
   }
   Security.addProvider(provider);
   KeyStore ks = KeyStore.getInstance(prov, provider);
   ks.load(
       CMD_keystore.found ? new FileInputStream(CMD_keystore.getString()) : null,
       CMD_keypass.found ? CMD_keypass.getString().toCharArray() : null);
   wrap.setKeyStore(ks, CMD_keypass.getString());
   CMD_keystore.found = false;
 }
コード例 #8
0
ファイル: FileCache.java プロジェクト: DuanAohas/android
 private static MessageDigest findAlternativeMessageDigest() {
   if ("MD5".equals(hashAlgorithm)) {
     for (Provider provider : Security.getProviders()) {
       for (Provider.Service service : provider.getServices()) {
         hashAlgorithm = service.getAlgorithm();
         try {
           MessageDigest messageDigest = MessageDigest.getInstance(hashAlgorithm);
           if (messageDigest != null) return messageDigest;
         } catch (NoSuchAlgorithmException ignored) {
         }
       }
     }
   }
   return null;
 }
コード例 #9
0
  private String[] getAvailableBuiltInDigestAlgorithmNames() {
    Set<String> algorithmsSet = new HashSet<String>();
    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
      Set<Provider.Service> services = provider.getServices();
      for (Provider.Service service : services) {
        if (service.getType() == "MessageDigest") {
          algorithmsSet.add(service.getAlgorithm());
        }
      }
    }

    // For some reason my Java implementation gives SHA1 two names, so we will remove one.
    if (algorithmsSet.contains("SHA") && algorithmsSet.contains("SHA1")) {
      algorithmsSet.remove("SHA");
    }

    return algorithmsSet.toArray(new String[algorithmsSet.size()]);
  }
コード例 #10
0
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "getAttribute",
      args = {java.lang.String.class})
  public void testGetAttribute() {
    Provider p = new MyProvider();
    Provider.Service s = new Provider.Service(p, "type", "algorithm", "className", null, null);
    try {
      s.getAttribute(null);
      fail("No expected NullPointerException");
    } catch (NullPointerException e) {
    }

    if (s.getAttribute("aaa") != null) {
      fail("getAttribute(aaa) failed");
    }

    HashMap<String, String> hm = new HashMap<String, String>();
    hm.put("attribute", "value");
    hm.put("KeySize", "1024");
    hm.put("AAA", "BBB");

    s = new Provider.Service(p, "type", "algorithm", "className", null, hm);
    if (s.getAttribute("bbb") != null) {
      fail("getAttribute(bbb) failed");
    }
    if (!s.getAttribute("attribute").equals("value")) {
      fail("getAttribute(attribute) failed");
    }
    if (!s.getAttribute("KeySize").equals("1024")) {
      fail("getAttribute(KeySize) failed");
    }
  }
コード例 #11
0
  @TestTargetNew(
      level = TestLevel.COMPLETE,
      notes = "",
      method = "Service",
      args = {
        java.security.Provider.class,
        java.lang.String.class,
        java.lang.String.class,
        java.lang.String.class,
        java.util.List.class,
        java.util.Map.class
      })
  public void testService() {
    Provider p = new MyProvider();
    try {
      new Provider.Service(null, "type", "algorithm", "className", null, null);
      fail("provider is null: No expected NullPointerException");
    } catch (NullPointerException e) {
    }
    try {
      new Provider.Service(p, null, "algorithm", "className", null, null);
      fail("type is null: No expected NullPointerException");
    } catch (NullPointerException e) {
    }
    try {
      new Provider.Service(p, "type", null, "className", null, null);
      fail("algorithm is null: No expected NullPointerException");
    } catch (NullPointerException e) {
    }
    try {
      new Provider.Service(p, "type", "algorithm", null, null, null);
      fail("className is null: No expected NullPointerException");
    } catch (NullPointerException e) {
    }

    Provider.Service s = new Provider.Service(p, "type", "algorithm", "className", null, null);

    if (!s.getType().equals("type")) {
      fail("getType() failed");
    }
    if (!s.getAlgorithm().equals("algorithm")) {
      fail("getAlgorithm() failed");
    }
    if (s.getProvider() != p) {
      fail("getProvider() failed");
    }
    if (!s.getClassName().equals("className")) {
      fail("getClassName() failed");
    }
    if (!s.supportsParameter(new Object())) {
      fail("supportsParameter() failed");
    }
  }
コード例 #12
0
  /**
   * Makes sure all all expected implementations (but not aliases) and that there are no extras,
   * according to what we expect from StandardNames
   */
  public void test_Provider_getServices() throws Exception {

    // build set of expected algorithms
    Map<String, Set<String>> remaining =
        new HashMap<String, Set<String>>(StandardNames.PROVIDER_ALGORITHMS);
    for (Entry<String, Set<String>> entry : remaining.entrySet()) {
      entry.setValue(new HashSet<String>(entry.getValue()));
    }

    List<String> extra = new ArrayList();
    List<String> missing = new ArrayList();

    Provider[] providers = Security.getProviders();
    for (Provider provider : providers) {
      String providerName = provider.getName();
      // ignore BouncyCastle provider if it is installed on the RI
      if (StandardNames.IS_RI && providerName.equals("BC")) {
        continue;
      }
      Set<Provider.Service> services = provider.getServices();
      assertNotNull(services);
      assertFalse(services.isEmpty());

      for (Provider.Service service : services) {
        String type = service.getType();
        String algorithm = service.getAlgorithm().toUpperCase();
        String className = service.getClassName();
        if (false) {
          System.out.println(providerName + " " + type + " " + algorithm + " " + className);
        }

        // remove from remaining, assert unknown if missing
        Set<String> algorithms = remaining.get(type);
        if (algorithms == null || !algorithms.remove(algorithm)) {
          // seems to be missing, but sometimes the same
          // algorithm is available from multiple providers
          // (e.g. KeyFactory RSA is available from
          // SunRsaSign and SunJSSE), so double check in
          // original source before giving error
          if (!(StandardNames.PROVIDER_ALGORITHMS.containsKey(type)
              && StandardNames.PROVIDER_ALGORITHMS.get(type).contains(algorithm))) {
            extra.add("Unknown " + type + " " + algorithm + " " + providerName + "\n");
          }
        }
        if (algorithms != null && algorithms.isEmpty()) {
          remaining.remove(type);
        }

        // make sure class exists and can be initialized
        try {
          assertNotNull(Class.forName(className, true, provider.getClass().getClassLoader()));
        } catch (ClassNotFoundException e) {
          // Sun forgot their own class
          if (!className.equals("sun.security.pkcs11.P11MAC")) {
            missing.add(className);
          }
        }
      }
    }

    // assert that we don't have any extra in the implementation
    Collections.sort(extra); // sort so that its grouped by type
    assertEquals("Extra algorithms", Collections.EMPTY_LIST, extra);

    // assert that we don't have any missing in the implementation
    assertEquals("Missing algorithms", Collections.EMPTY_MAP, remaining);

    // assert that we don't have any missing classes
    Collections.sort(missing); // sort it for readability
    assertEquals("Missing classes", Collections.EMPTY_LIST, missing);
  }
コード例 #13
0
 /**
  * Get a password factory instance. The returned password factory object will implement the given
  * algorithm.
  *
  * @param algorithm the name of the algorithm
  * @param provider the provider to use
  * @return a password factory instance
  * @throws NoSuchAlgorithmException if the given algorithm has no available implementations
  */
 public static PasswordFactory getInstance(String algorithm, Provider provider)
     throws NoSuchAlgorithmException {
   final Provider.Service service = provider.getService("PasswordFactory", algorithm);
   if (service == null) throw log.noSuchAlgorithmInvalidAlgorithm(algorithm);
   return new PasswordFactory((PasswordFactorySpi) service.newInstance(null), provider, algorithm);
 }