use of java.security.KeyStore.ProtectionParameter in project robovm by robovm.
the class KeyStore4Test method testLoadLoadStoreParameter.
public void testLoadLoadStoreParameter() {
try {
keyStore.load(null);
fail("expected NoSuchAlgorithmException");
} catch (NoSuchAlgorithmException e) {
// ok
} catch (CertificateException e) {
fail("unexpected exception: " + e);
} catch (IOException e) {
fail("unexpected exception: " + e);
}
try {
keyStore.load(new KeyStore.LoadStoreParameter() {
public ProtectionParameter getProtectionParameter() {
return new KeyStore.PasswordProtection("PASSWORD".toCharArray());
}
});
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
} catch (CertificateException e) {
fail("unexpected exception: " + e);
} catch (IOException e) {
fail("unexpected exception: " + e);
}
try {
keyStore.load(new KeyStore.LoadStoreParameter() {
public ProtectionParameter getProtectionParameter() {
return null;
}
});
fail("expected NoSuchAlgorithmException");
} catch (NoSuchAlgorithmException e) {
// ok
} catch (CertificateException e) {
fail("unexpected exception: " + e);
} catch (IOException e) {
fail("unexpected exception: " + e);
}
try {
keyStore.load(new KeyStore.LoadStoreParameter() {
public ProtectionParameter getProtectionParameter() {
return new KeyStore.ProtectionParameter() {
};
}
});
fail("expected CertificateException");
} catch (NoSuchAlgorithmException e) {
fail("unexpected exception: " + e);
} catch (CertificateException e) {
// ok
} catch (IOException e) {
fail("unexpected exception: " + e);
}
}
use of java.security.KeyStore.ProtectionParameter in project robovm by robovm.
the class PKCS12KeyStoreSpi method engineStore.
public void engineStore(LoadStoreParameter param) throws IOException, NoSuchAlgorithmException, CertificateException {
if (param == null) {
throw new IllegalArgumentException("'param' arg cannot be null");
}
if (!(param instanceof PKCS12StoreParameter || param instanceof JDKPKCS12StoreParameter)) {
throw new IllegalArgumentException("No support for 'param' of type " + param.getClass().getName());
}
PKCS12StoreParameter bcParam;
if (param instanceof PKCS12StoreParameter) {
bcParam = (PKCS12StoreParameter) param;
} else {
bcParam = new PKCS12StoreParameter(((JDKPKCS12StoreParameter) param).getOutputStream(), param.getProtectionParameter(), ((JDKPKCS12StoreParameter) param).isUseDEREncoding());
}
char[] password;
ProtectionParameter protParam = param.getProtectionParameter();
if (protParam == null) {
password = null;
} else if (protParam instanceof KeyStore.PasswordProtection) {
password = ((KeyStore.PasswordProtection) protParam).getPassword();
} else {
throw new IllegalArgumentException("No support for protection parameter of type " + protParam.getClass().getName());
}
doStore(bcParam.getOutputStream(), password, bcParam.isForDEREncoding());
}
use of java.security.KeyStore.ProtectionParameter in project midpoint by Evolveum.
the class KeyStoreDumper method execute.
public void execute() {
try {
ApplicationContext context = new ClassPathXmlApplicationContext(CONTEXTS);
Protector protector = context.getBean("protector", Protector.class);
KeyStore keyStore = protector.getKeyStore();
System.out.println("###################################################");
System.out.println("Printing keys from key store");
if (protector instanceof ProtectorImpl) {
ProtectorImpl aesProtector = (ProtectorImpl) protector;
System.out.println("Using key store from location: " + aesProtector.getKeyStorePath());
// System.out.println("Cipher: " + aesProtector.getXmlCipher());
}
Enumeration<String> aliases = keyStore.aliases();
while (aliases.hasMoreElements()) {
String alias = aliases.nextElement();
System.out.println("===== ALIAS: " + alias + "=====");
System.out.println("Creation date: " + keyStore.getCreationDate(alias));
System.out.println("Type: " + keyStore.getType());
if (keyStore.getCertificate(alias) != null) {
System.out.println("Certificate: " + keyStore.getCertificate(alias));
}
if (keyStore.getCertificateChain(alias) != null) {
System.out.println("Certificate chain: " + keyStore.getCertificateChain(alias));
}
ProtectionParameter protParam = new KeyStore.PasswordProtection("midpoint".toCharArray());
Entry entry = keyStore.getEntry(alias, protParam);
if (entry instanceof SecretKeyEntry) {
System.out.println("Secret key entry: ");
SecretKeyEntry skEntry = (SecretKeyEntry) entry;
SecretKey key = skEntry.getSecretKey();
System.out.println(" Algorithm: " + key.getAlgorithm());
System.out.println(" Format: " + key.getFormat());
System.out.println(" Key length: " + key.getEncoded().length * 8);
if (protector instanceof ProtectorImpl) {
System.out.println(" Key name: " + ((ProtectorImpl) protector).getSecretKeyDigest(key));
}
// Cipher cipher = Cipher.getInstance(key.getAlgorithm());
// System.out.println(" Cipher algorithm" + cipher.getAlgorithm());
}
//TODO: add dump also for other types of keys
Provider provider = keyStore.getProvider();
System.out.println("Provder name: " + provider.getName() + "\n");
}
System.out.println("###################################################");
} catch (KeyStoreException ex) {
System.out.println("Failed to print information about keyStore. Reason: " + ex.getMessage());
return;
} catch (UnrecoverableEntryException ex) {
System.out.println("Failed to print information about keyStore. Reason: " + ex.getMessage());
return;
} catch (NoSuchAlgorithmException ex) {
System.out.println("Failed to print information about keyStore. Reason: " + ex.getMessage());
return;
} catch (EncryptionException ex) {
System.out.println("Failed to print information about keyStore. Reason: " + ex.getMessage());
return;
}
}
use of java.security.KeyStore.ProtectionParameter in project jmulticard by ctt-gob-es.
the class SmartCafeKeyStoreImpl method engineLoad.
/**
* {@inheritDoc}
*/
@Override
public void engineLoad(final KeyStore.LoadStoreParameter param) throws IOException {
final ApduConnection conn = new es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection();
this.cryptoCard = new SmartCafePkcs15Applet(conn, new JseCryptoHelper());
if (param != null) {
final ProtectionParameter pp = param.getProtectionParameter();
if (pp instanceof KeyStore.CallbackHandlerProtection) {
if (((KeyStore.CallbackHandlerProtection) pp).getCallbackHandler() == null) {
// $NON-NLS-1$
throw new IllegalArgumentException("El CallbackHandler no puede ser nulo");
}
this.cryptoCard.setCallbackHandler(((KeyStore.CallbackHandlerProtection) pp).getCallbackHandler());
} else if (pp instanceof KeyStore.PasswordProtection) {
final PasswordCallback pwc = new CachePasswordCallback(((PasswordProtection) pp).getPassword());
this.cryptoCard.setPasswordCallback(pwc);
} else {
LOGGER.warning(// $NON-NLS-1$ //$NON-NLS-2$
"Se ha proporcionado un LoadStoreParameter de tipo no soportado, se ignorara: " + (pp != null ? pp.getClass().getName() : "NULO"));
}
}
this.aliases = Arrays.asList(this.cryptoCard.getAliases());
}
use of java.security.KeyStore.ProtectionParameter in project jmulticard by ctt-gob-es.
the class CeresKeyStoreImpl method engineLoad.
/**
* {@inheritDoc}
*/
@Override
public void engineLoad(final KeyStore.LoadStoreParameter param) throws IOException {
if (param != null) {
final ProtectionParameter pp = param.getProtectionParameter();
if (pp instanceof KeyStore.CallbackHandlerProtection) {
if (((KeyStore.CallbackHandlerProtection) pp).getCallbackHandler() == null) {
// $NON-NLS-1$
throw new IllegalArgumentException("El CallbackHandler no puede ser nulo");
}
this.cryptoCard = new Ceres(CeresProvider.getDefaultApduConnection(), new JseCryptoHelper());
this.cryptoCard.setCallbackHandler(((KeyStore.CallbackHandlerProtection) pp).getCallbackHandler());
} else if (pp instanceof KeyStore.PasswordProtection) {
final PasswordCallback pwc = new CeresPasswordCallback((PasswordProtection) pp);
this.cryptoCard = new Ceres(CeresProvider.getDefaultApduConnection(), new JseCryptoHelper());
this.cryptoCard.setPasswordCallback(pwc);
} else {
// $NON-NLS-1$
Logger.getLogger("es.gob.jmulticard").warning(// $NON-NLS-1$ //$NON-NLS-2$
"Se ha proporcionado un LoadStoreParameter de tipo no soportado, se ignorara: " + (pp != null ? pp.getClass().getName() : "NULO"));
}
} else {
this.cryptoCard = new Ceres(CeresProvider.getDefaultApduConnection(), new JseCryptoHelper());
}
userCertAliases = Arrays.asList(this.cryptoCard.getAliases());
}
Aggregations