use of java.security.Provider.Service in project jdk8u_jdk by JetBrains.
the class SupportsParameter method main.
public static void main(String[] args) throws Exception {
KeyPairGenerator kpg = KeyPairGenerator.getInstance("DSA");
kpg.initialize(512);
KeyPair kp = kpg.generateKeyPair();
PublicKey dsaPublicKey = kp.getPublic();
PrivateKey dsaPrivateKey = kp.getPrivate();
PublicKey myPublicKey = new MyPublicKey();
PrivateKey myPrivateKey = new MyPrivateKey();
SecretKey mySecretKey = new MySecretKey();
Provider p = new MyProvider();
Service s;
// none specified, always true
s = p.getService("Signature", "DSA0");
checkSupports(s, null, true);
checkSupports(s, dsaPublicKey, true);
checkSupports(s, dsaPrivateKey, true);
checkSupports(s, myPublicKey, true);
checkSupports(s, myPrivateKey, true);
checkSupports(s, mySecretKey, true);
// DSAPublic/PrivateKey specified as classes
s = p.getService("Signature", "DSA");
checkSupports(s, dsaPublicKey, true);
checkSupports(s, dsaPrivateKey, true);
checkSupports(s, myPublicKey, false);
checkSupports(s, myPrivateKey, false);
checkSupports(s, mySecretKey, false);
// X.509/PKCS#8 specified as formats
s = p.getService("Signature", "DSA2");
checkSupports(s, dsaPublicKey, true);
checkSupports(s, dsaPrivateKey, true);
checkSupports(s, myPublicKey, false);
checkSupports(s, myPrivateKey, false);
checkSupports(s, mySecretKey, false);
// MyPublic/PrivateKey + DSAPublicKey specified as classes
s = p.getService("Signature", "DSA3");
checkSupports(s, dsaPublicKey, true);
checkSupports(s, dsaPrivateKey, false);
checkSupports(s, myPublicKey, true);
checkSupports(s, myPrivateKey, true);
checkSupports(s, mySecretKey, false);
// MyPublic/PrivateKey + DSAPublicKey specified as classes
s = p.getService("Cipher", "DES");
checkSupports(s, dsaPublicKey, false);
checkSupports(s, dsaPrivateKey, false);
checkSupports(s, myPublicKey, false);
checkSupports(s, myPrivateKey, false);
checkSupports(s, mySecretKey, true);
Key secretKeySpec = new SecretKeySpec(new byte[8], "DES");
checkSupports(s, secretKeySpec, true);
}
use of java.security.Provider.Service in project robovm by robovm.
the class ProviderTest method testRemoveService.
// END android-added
// BEGIN android-added
public final void testRemoveService() {
MyProvider myProvider = new MyProvider(null, 1, null);
try {
myProvider.removeService(null);
fail("NullPoiterException expected");
} catch (NullPointerException e) {
// expected
}
Provider.Service[] s = new Provider.Service[3];
s[0] = new Provider.Service(p, "type0", "algorithm0", "className0", null, null);
s[1] = new Provider.Service(p, "type1", "algorithm1", "className1", null, null);
s[2] = new Provider.Service(p, "type2", "algorithm2", "className2", null, null);
try {
myProvider.removeService(s[0]);
} catch (NullPointerException e) {
fail("Unexpected exception");
}
myProvider.putService(s[0]);
myProvider.putService(s[1]);
myProvider.putService(s[2]);
assertEquals(3, myProvider.getNumServices());
Set<Service> actual = myProvider.getServices();
assertTrue(actual.contains(s[0]));
assertTrue(actual.contains(s[1]));
assertTrue(actual.contains(s[2]));
myProvider.removeService(s[1]);
assertEquals(2, myProvider.getNumServices());
actual = myProvider.getServices();
assertTrue(actual.contains(s[0]));
assertTrue(!actual.contains(s[1]));
assertTrue(actual.contains(s[2]));
myProvider.removeService(s[0]);
assertEquals(1, myProvider.getNumServices());
actual = myProvider.getServices();
assertTrue(!actual.contains(s[0]));
assertTrue(!actual.contains(s[1]));
assertTrue(actual.contains(s[2]));
myProvider.removeService(s[2]);
assertEquals(0, myProvider.getNumServices());
actual = myProvider.getServices();
assertTrue(!actual.contains(s[0]));
assertTrue(!actual.contains(s[1]));
assertTrue(!actual.contains(s[2]));
try {
myProvider.removeService(null);
fail("NullPoiterException expected");
} catch (NullPointerException e) {
// expected
}
}
use of java.security.Provider.Service in project robovm by robovm.
the class ProviderTest method testPutService.
// END android-added
// BEGIN android-added
public final void testPutService() {
MyProvider myProvider = new MyProvider(null, 1, null);
Provider.Service[] s = new Provider.Service[3];
s[0] = new Provider.Service(p, "type1", "algorithm1", "className1", null, null);
s[1] = new Provider.Service(p, "type2", "algorithm2", "className2", null, null);
s[2] = new Provider.Service(p, "type3", "algorithm3", "className3", null, null);
myProvider.putService(s[0]);
myProvider.putService(s[1]);
assertEquals(2, myProvider.getNumServices());
Set<Service> actual = myProvider.getServices();
assertTrue(actual.contains(s[0]));
assertTrue(actual.contains(s[1]));
assertTrue(!actual.contains(s[2]));
myProvider.removeService(s[1]);
assertEquals(1, myProvider.getNumServices());
actual = myProvider.getServices();
assertTrue(actual.contains(s[0]));
assertTrue(!actual.contains(s[1]));
assertTrue(!actual.contains(s[2]));
myProvider.putService(s[2]);
actual = myProvider.getServices();
assertEquals(2, myProvider.getNumServices());
assertTrue(actual.contains(s[0]));
assertTrue(!actual.contains(s[1]));
assertTrue(actual.contains(s[2]));
myProvider.putService(s[2]);
actual = myProvider.getServices();
assertEquals(2, myProvider.getNumServices());
assertTrue(actual.contains(s[0]));
assertTrue(!actual.contains(s[1]));
assertTrue(actual.contains(s[2]));
try {
myProvider.putService(null);
fail("NullPointerException expected");
} catch (NullPointerException e) {
// expected
}
}
use of java.security.Provider.Service in project j2objc by google.
the class JceSecurity method getInstance.
static Instance getInstance(String type, Class clazz, String algorithm) throws NoSuchAlgorithmException {
List services = GetInstance.getServices(type, algorithm);
NoSuchAlgorithmException failure = null;
for (Iterator t = services.iterator(); t.hasNext(); ) {
Service s = (Service) t.next();
if (canUseProvider(s.getProvider()) == false) {
// allow only signed providers
continue;
}
try {
Instance instance = GetInstance.getInstance(s, clazz);
return instance;
} catch (NoSuchAlgorithmException e) {
failure = e;
}
}
throw new NoSuchAlgorithmException("Algorithm " + algorithm + " not available", failure);
}
use of java.security.Provider.Service in project camel by apache.
the class ApnsUtils method getAlgorithm.
public static String getAlgorithm() {
List<String> keys = new LinkedList<String>();
List<String> trusts = new LinkedList<String>();
for (Provider p : Security.getProviders()) {
for (Service s : p.getServices()) {
if ("KeyManagerFactory".equals(s.getType()) && s.getAlgorithm().endsWith("509")) {
keys.add(s.getAlgorithm());
} else if ("TrustManagerFactory".equals(s.getType()) && s.getAlgorithm().endsWith("509")) {
trusts.add(s.getAlgorithm());
}
}
}
keys.retainAll(trusts);
return keys.get(0);
}
Aggregations