use of java.security.interfaces.DSAPrivateKey in project camel by apache.
the class SftpOperations method createSession.
protected Session createSession(final RemoteFileConfiguration configuration) throws JSchException {
final JSch jsch = new JSch();
JSch.setLogger(new JSchLogger(endpoint.getConfiguration().getJschLoggingLevel()));
SftpConfiguration sftpConfig = (SftpConfiguration) configuration;
if (isNotEmpty(sftpConfig.getCiphers())) {
LOG.debug("Using ciphers: {}", sftpConfig.getCiphers());
Hashtable<String, String> ciphers = new Hashtable<String, String>();
ciphers.put("cipher.s2c", sftpConfig.getCiphers());
ciphers.put("cipher.c2s", sftpConfig.getCiphers());
JSch.setConfig(ciphers);
}
if (isNotEmpty(sftpConfig.getPrivateKeyFile())) {
LOG.debug("Using private keyfile: {}", sftpConfig.getPrivateKeyFile());
if (isNotEmpty(sftpConfig.getPrivateKeyPassphrase())) {
jsch.addIdentity(sftpConfig.getPrivateKeyFile(), sftpConfig.getPrivateKeyPassphrase());
} else {
jsch.addIdentity(sftpConfig.getPrivateKeyFile());
}
}
if (sftpConfig.getPrivateKey() != null) {
LOG.debug("Using private key information from byte array");
byte[] passphrase = null;
if (isNotEmpty(sftpConfig.getPrivateKeyPassphrase())) {
try {
passphrase = sftpConfig.getPrivateKeyPassphrase().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new JSchException("Cannot transform passphrase to byte[]", e);
}
}
jsch.addIdentity("ID", sftpConfig.getPrivateKey(), null, passphrase);
}
if (sftpConfig.getPrivateKeyUri() != null) {
LOG.debug("Using private key uri : {}", sftpConfig.getPrivateKeyUri());
byte[] passphrase = null;
if (isNotEmpty(sftpConfig.getPrivateKeyPassphrase())) {
try {
passphrase = sftpConfig.getPrivateKeyPassphrase().getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new JSchException("Cannot transform passphrase to byte[]", e);
}
}
try {
InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(endpoint.getCamelContext(), sftpConfig.getPrivateKeyUri());
ByteArrayOutputStream bos = new ByteArrayOutputStream();
IOHelper.copyAndCloseInput(is, bos);
jsch.addIdentity("ID", bos.toByteArray(), null, passphrase);
} catch (IOException e) {
throw new JSchException("Cannot read resource: " + sftpConfig.getPrivateKeyUri(), e);
}
}
if (sftpConfig.getKeyPair() != null) {
LOG.debug("Using private key information from key pair");
KeyPair keyPair = sftpConfig.getKeyPair();
if (keyPair.getPrivate() != null && keyPair.getPublic() != null) {
if (keyPair.getPrivate() instanceof RSAPrivateKey && keyPair.getPublic() instanceof RSAPublicKey) {
jsch.addIdentity(new RSAKeyPairIdentity("ID", keyPair), null);
} else if (keyPair.getPrivate() instanceof DSAPrivateKey && keyPair.getPublic() instanceof DSAPublicKey) {
jsch.addIdentity(new DSAKeyPairIdentity("ID", keyPair), null);
} else {
LOG.warn("Only RSA and DSA key pairs are supported");
}
} else {
LOG.warn("PrivateKey and PublicKey in the KeyPair must be filled");
}
}
if (isNotEmpty(sftpConfig.getKnownHostsFile())) {
LOG.debug("Using knownhosts file: {}", sftpConfig.getKnownHostsFile());
jsch.setKnownHosts(sftpConfig.getKnownHostsFile());
}
if (isNotEmpty(sftpConfig.getKnownHostsUri())) {
LOG.debug("Using known hosts uri: {}", sftpConfig.getKnownHostsUri());
try {
InputStream is = ResourceHelper.resolveMandatoryResourceAsInputStream(endpoint.getCamelContext(), sftpConfig.getKnownHostsUri());
jsch.setKnownHosts(is);
} catch (IOException e) {
throw new JSchException("Cannot read resource: " + sftpConfig.getKnownHostsUri(), e);
}
}
if (sftpConfig.getKnownHosts() != null) {
LOG.debug("Using known hosts information from byte array");
jsch.setKnownHosts(new ByteArrayInputStream(sftpConfig.getKnownHosts()));
}
String knownHostsFile = sftpConfig.getKnownHostsFile();
if (knownHostsFile == null && sftpConfig.isUseUserKnownHostsFile()) {
knownHostsFile = System.getProperty("user.home") + "/.ssh/known_hosts";
LOG.info("Known host file not configured, using user known host file: {}", knownHostsFile);
}
if (ObjectHelper.isNotEmpty(knownHostsFile)) {
LOG.debug("Using known hosts information from file: {}", knownHostsFile);
jsch.setKnownHosts(knownHostsFile);
}
final Session session = jsch.getSession(configuration.getUsername(), configuration.getHost(), configuration.getPort());
if (isNotEmpty(sftpConfig.getStrictHostKeyChecking())) {
LOG.debug("Using StrickHostKeyChecking: {}", sftpConfig.getStrictHostKeyChecking());
session.setConfig("StrictHostKeyChecking", sftpConfig.getStrictHostKeyChecking());
}
session.setServerAliveInterval(sftpConfig.getServerAliveInterval());
session.setServerAliveCountMax(sftpConfig.getServerAliveCountMax());
// compression
if (sftpConfig.getCompression() > 0) {
LOG.debug("Using compression: {}", sftpConfig.getCompression());
session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none");
session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none");
session.setConfig("compression_level", Integer.toString(sftpConfig.getCompression()));
}
// set the PreferredAuthentications
if (sftpConfig.getPreferredAuthentications() != null) {
LOG.debug("Using PreferredAuthentications: {}", sftpConfig.getPreferredAuthentications());
session.setConfig("PreferredAuthentications", sftpConfig.getPreferredAuthentications());
}
// set user information
session.setUserInfo(new ExtendedUserInfo() {
public String getPassphrase() {
return null;
}
public String getPassword() {
return configuration.getPassword();
}
public boolean promptPassword(String s) {
return true;
}
public boolean promptPassphrase(String s) {
return true;
}
public boolean promptYesNo(String s) {
LOG.warn("Server asks for confirmation (yes|no): " + s + ". Camel will answer no.");
// Return 'false' indicating modification of the hosts file is disabled.
return false;
}
public void showMessage(String s) {
LOG.trace("Message received from Server: " + s);
}
public String[] promptKeyboardInteractive(String destination, String name, String instruction, String[] prompt, boolean[] echo) {
// must return an empty array if password is null
if (configuration.getPassword() == null) {
return new String[0];
} else {
return new String[] { configuration.getPassword() };
}
}
});
// set the SO_TIMEOUT for the time after the connect phase
if (configuration.getSoTimeout() > 0) {
session.setTimeout(configuration.getSoTimeout());
}
// set proxy if configured
if (proxy != null) {
session.setProxy(proxy);
}
return session;
}
use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.
the class OpenSSLSignature method engineInitSign.
@Override
protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException {
destroyContextIfExists();
if (privateKey instanceof OpenSSLKeyHolder) {
OpenSSLKey pkey = ((OpenSSLKeyHolder) privateKey).getOpenSSLKey();
checkEngineType(pkey);
key = pkey;
} else if (privateKey instanceof RSAPrivateCrtKey) {
if (engineType != EngineType.RSA) {
throw new InvalidKeyException("Signature not initialized as RSA");
}
RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
key = OpenSSLRSAPrivateCrtKey.getInstance(rsaPrivateKey);
} else if (privateKey instanceof RSAPrivateKey) {
if (engineType != EngineType.RSA) {
throw new InvalidKeyException("Signature not initialized as RSA");
}
RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) privateKey;
key = OpenSSLRSAPrivateKey.getInstance(rsaPrivateKey);
} else if (privateKey instanceof DSAPrivateKey) {
if (engineType != EngineType.DSA) {
throw new InvalidKeyException("Signature not initialized as DSA");
}
DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
key = OpenSSLDSAPrivateKey.getInstance(dsaPrivateKey);
} else if (privateKey instanceof ECPrivateKey) {
if (engineType != EngineType.EC) {
throw new InvalidKeyException("Signature not initialized as EC");
}
ECPrivateKey ecPrivateKey = (ECPrivateKey) privateKey;
key = OpenSSLECPrivateKey.getInstance(ecPrivateKey);
} else {
throw new InvalidKeyException("Need DSA or RSA or EC private key");
}
}
use of java.security.interfaces.DSAPrivateKey in project robovm by robovm.
the class OpenSSLDSAPrivateKey method equals.
@Override
public boolean equals(Object o) {
if (o == this) {
return true;
}
if (o instanceof OpenSSLDSAPrivateKey) {
OpenSSLDSAPrivateKey other = (OpenSSLDSAPrivateKey) o;
/*
* We can shortcut the true case, but it still may be equivalent but
* different copies.
*/
if (key.equals(other.getOpenSSLKey())) {
return true;
}
}
if (!(o instanceof DSAPrivateKey)) {
return false;
}
ensureReadParams();
final BigInteger x = params.getX();
if (x == null) {
/*
* If our X is null, we can't tell if these two private keys are
* equivalent. This usually happens if this key is ENGINE-based. If
* the other key was ENGINE-based, we should have caught it in the
* OpenSSLDSAPrivateKey case.
*/
return false;
}
final DSAPrivateKey other = (DSAPrivateKey) o;
return x.equals(other.getX()) && params.equals(other.getParams());
}
use of java.security.interfaces.DSAPrivateKey in project bitsquare by bitsquare.
the class KeyStorage method loadKeyPair.
public KeyPair loadKeyPair(KeyEntry keyEntry) {
FileUtil.rollingBackup(storageDir, keyEntry.getFileName() + ".key", 20);
// long now = System.currentTimeMillis();
try {
KeyFactory keyFactory = KeyFactory.getInstance(keyEntry.getAlgorithm(), "BC");
PublicKey publicKey;
PrivateKey privateKey;
File filePrivateKey = new File(storageDir + "/" + keyEntry.getFileName() + ".key");
try (FileInputStream fis = new FileInputStream(filePrivateKey.getPath())) {
byte[] encodedPrivateKey = new byte[(int) filePrivateKey.length()];
fis.read(encodedPrivateKey);
PKCS8EncodedKeySpec privateKeySpec = new PKCS8EncodedKeySpec(encodedPrivateKey);
privateKey = keyFactory.generatePrivate(privateKeySpec);
} catch (InvalidKeySpecException | IOException e) {
e.printStackTrace();
log.error(e.getMessage());
throw new RuntimeException("Could not load key " + keyEntry.toString(), e);
}
if (privateKey instanceof RSAPrivateCrtKey) {
RSAPrivateCrtKey rsaPrivateKey = (RSAPrivateCrtKey) privateKey;
RSAPublicKeySpec publicKeySpec = new RSAPublicKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPublicExponent());
publicKey = keyFactory.generatePublic(publicKeySpec);
} else if (privateKey instanceof DSAPrivateKey) {
DSAPrivateKey dsaPrivateKey = (DSAPrivateKey) privateKey;
DSAParams dsaParams = dsaPrivateKey.getParams();
BigInteger p = dsaParams.getP();
BigInteger q = dsaParams.getQ();
BigInteger g = dsaParams.getG();
BigInteger y = g.modPow(dsaPrivateKey.getX(), p);
KeySpec publicKeySpec = new DSAPublicKeySpec(y, p, q, g);
publicKey = keyFactory.generatePublic(publicKeySpec);
} else {
throw new RuntimeException("Unsupported key algo" + keyEntry.getAlgorithm());
}
log.debug("load completed in {} msec", System.currentTimeMillis() - new Date().getTime());
return new KeyPair(publicKey, privateKey);
} catch (NoSuchAlgorithmException | InvalidKeySpecException | NoSuchProviderException e) {
e.printStackTrace();
log.error(e.getMessage());
throw new RuntimeException("Could not load key " + keyEntry.toString(), e);
}
}
use of java.security.interfaces.DSAPrivateKey in project keystore-explorer by kaikramer.
the class MsPvkUtil method getInternal.
private static byte[] getInternal(PrivateKey privateKey, int keyType) throws CryptoException {
try {
// Write PVK to a byte buffer set up to write little endian
ByteBuffer bb = ByteBuffer.wrap(new byte[PVK_BUFFER_LENGTH]);
bb.order(ByteOrder.LITTLE_ENDIAN);
// Write magic number, reserved and and key type fields
writeReservedMagicKeyType(bb, keyType);
// Get unencrypted private key blob
byte[] privateKeyBlob = null;
if (privateKey instanceof RSAPrivateCrtKey) {
privateKeyBlob = rsaPrivateKeyToBlob((RSAPrivateCrtKey) privateKey);
} else {
privateKeyBlob = dsaPrivateKeyToBlob((DSAPrivateKey) privateKey);
}
// Write type field - unencrypted
UnsignedUtil.putInt(bb, PVK_UNENCRYPTED);
// Write salt length - unencrypted so no salt, length = 0
UnsignedUtil.putInt(bb, UNENCRYPTED_SALT_LENGTH);
// Write key length field - length of the blob plus length of blob header
long keyLength = privateKeyBlob.length + BLOB_HEADER_LENGTH;
UnsignedUtil.putInt(bb, keyLength);
// Write private key blob header
writePrivateKeyBlobHeader(bb, keyType, privateKey);
// Write private key blob
bb.put(privateKeyBlob);
byte[] pvk = getBufferBytes(bb);
return pvk;
} catch (IOException ex) {
throw new CryptoException(res.getString("NoGetMsPvk.exception.message"), ex);
}
}
Aggregations