use of org.simplejavamail.mailer.internal.socks.common.SocksException in project simple-java-mail by bbottema.
the class SSLConfiguration method getSSLSocketFactory.
public SSLSocketFactory getSSLSocketFactory() throws SocksException {
MiscUtil.checkNotNull(trustKeyStoreInfo, "trustKeyStoreInfo may not be null");
FileInputStream s1 = null;
FileInputStream s2 = null;
try {
final SSLContext context = SSLContext.getInstance("SSL");
final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance("SunX509");
final KeyStore trustKeyStore = KeyStore.getInstance(trustKeyStoreInfo.getType());
trustKeyStore.load(s1 = new FileInputStream(trustKeyStoreInfo.getKeyStorePath()), trustKeyStoreInfo.getPassword().toCharArray());
trustManagerFactory.init(trustKeyStore);
KeyStore keyStore = null;
if (keyStoreInfo != null && keyStoreInfo.getKeyStorePath() != null) {
final KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance("SunX509");
keyStore = KeyStore.getInstance(keyStoreInfo.getType());
keyStore.load(s2 = new FileInputStream(keyStoreInfo.getKeyStorePath()), keyStoreInfo.getPassword().toCharArray());
keyManagerFactory.init(keyStore, keyStoreInfo.getPassword().toCharArray());
context.init(keyManagerFactory.getKeyManagers(), trustManagerFactory.getTrustManagers(), null);
} else {
context.init(null, trustManagerFactory.getTrustManagers(), null);
}
if (keyStore != null) {
LOGGER.info("SSL: Key store:{}", keyStoreInfo.getKeyStorePath());
}
LOGGER.info("SSL: Trust key store:{}", trustKeyStoreInfo.getKeyStorePath());
return context.getSocketFactory();
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
throw new SocksException(e.getMessage());
} finally {
tryCloseStream(s1);
tryCloseStream(s2);
}
}
use of org.simplejavamail.mailer.internal.socks.common.SocksException in project simple-java-mail by bbottema.
the class SocksAuthenticationHelper method performUserPasswordAuthentication.
public static void performUserPasswordAuthentication(final Socks5 socksProxy) throws IOException {
MiscUtil.checkNotNull(socksProxy, "Argument [socksProxy] may not be null");
final ProxyCredentials credentials = socksProxy.getCredentials();
if (credentials == null) {
throw new SocksException("Need Username/Password authentication");
}
final String username = credentials.getUsername();
final String password = credentials.getPassword();
final InputStream inputStream = socksProxy.getInputStream();
final OutputStream outputStream = socksProxy.getOutputStream();
final int USERNAME_LENGTH = username.getBytes(UTF_8).length;
final int PASSWORD_LENGTH = password.getBytes(UTF_8).length;
final byte[] bytesOfUsername = username.getBytes(UTF_8);
final byte[] bytesOfPassword = password.getBytes(UTF_8);
final byte[] bufferSent = new byte[3 + USERNAME_LENGTH + PASSWORD_LENGTH];
// VER
bufferSent[0] = 0x01;
// ULEN
bufferSent[1] = (byte) USERNAME_LENGTH;
// UNAME
System.arraycopy(bytesOfUsername, 0, bufferSent, 2, USERNAME_LENGTH);
// PLEN
bufferSent[2 + USERNAME_LENGTH] = (byte) PASSWORD_LENGTH;
// PASSWD
System.arraycopy(bytesOfPassword, 0, bufferSent, 3 + USERNAME_LENGTH, PASSWORD_LENGTH);
outputStream.write(bufferSent);
outputStream.flush();
// logger send bytes
LOGGER.trace("{}", MiscUtil.buildLogString(bufferSent, false));
final byte[] authenticationResult = new byte[2];
checkEnd(inputStream.read(authenticationResult));
// logger
LOGGER.trace("{}", MiscUtil.buildLogString(authenticationResult, true));
if (authenticationResult[1] != Socks5.AUTHENTICATION_SUCCEEDED) {
// Close connection if authentication is failed.
outputStream.close();
inputStream.close();
socksProxy.getProxySocket().close();
throw new SocksException("Username or password error");
}
}
use of org.simplejavamail.mailer.internal.socks.common.SocksException in project simple-java-mail by bbottema.
the class SocksCommandSender method send.
public static void send(final Socket socket, final SocketAddress socketAddress) throws IOException {
if (!(socketAddress instanceof InetSocketAddress)) {
throw new IllegalArgumentException("Unsupported address type");
}
final InputStream inputStream = socket.getInputStream();
final OutputStream outputStream = socket.getOutputStream();
final InetSocketAddress address = (InetSocketAddress) socketAddress;
final byte[] bytesOfAddress = address.getAddress().getAddress();
final int ADDRESS_LENGTH = bytesOfAddress.length;
final int port = address.getPort();
final byte addressType;
final byte[] bufferSent;
if (ADDRESS_LENGTH == LENGTH_OF_IPV4) {
addressType = ATYPE_IPV4;
bufferSent = new byte[6 + LENGTH_OF_IPV4];
} else if (ADDRESS_LENGTH == LENGTH_OF_IPV6) {
addressType = ATYPE_IPV6;
bufferSent = new byte[6 + LENGTH_OF_IPV6];
} else {
// TODO
throw new SocksException("Address error");
}
bufferSent[0] = SOCKS_VERSION;
bufferSent[1] = (byte) COMMAND_CONNECT;
bufferSent[2] = RESERVED;
bufferSent[3] = addressType;
// copy address bytes
System.arraycopy(bytesOfAddress, 0, bufferSent, 4, ADDRESS_LENGTH);
bufferSent[4 + ADDRESS_LENGTH] = (byte) ((port & 0xff00) >> 8);
bufferSent[5 + ADDRESS_LENGTH] = (byte) (port & 0xff);
outputStream.write(bufferSent);
outputStream.flush();
LOGGER.trace("{}", MiscUtil.buildLogString(bufferSent, false));
checkServerReply(inputStream);
}
use of org.simplejavamail.mailer.internal.socks.common.SocksException in project simple-java-mail by bbottema.
the class Socks5Handler method handle.
private void handle(final SocksSession session, final Socks5Bridge socks5Bridge) throws IOException {
if (MethodSelectionMessage.readVersion(session.getInputStream()) != VERSION) {
throw new SocksException("Protocol error");
}
LOGGER.debug("SESSION[{}]", session.getId());
// send select method.
session.write(METHOD_SELECTION_RESPONSE);
final CommandMessage commandMessage = new CommandMessage();
commandMessage.read(session.getInputStream());
// If there is a SOCKS exception in command message, It will send a right response to client.
if (commandMessage.hasSocksException()) {
final ServerReply serverReply = commandMessage.getSocksServerReplyException().getServerReply();
session.write(CommandResponseMessage.getBytes(serverReply));
LOGGER.debug("SESSION[{}] will close, because {}", session.getId(), serverReply);
return;
}
if (commandMessage.getCommand() != CONNECT_COMMAND) {
throw new SocksException("Only CONNECT command is supported");
}
doConnect(session, commandMessage, socks5Bridge);
}
use of org.simplejavamail.mailer.internal.socks.common.SocksException in project simple-java-mail by bbottema.
the class SocksAuthenticationHelper method shouldAuthenticate.
/**
* Performs an authentication method request to see how the proxy server wants to authenticate. GSSAPI is not supported, only anonymous
* and user / password authentication.
*/
public static boolean shouldAuthenticate(final Socket socket) throws IOException {
// send data
final byte[] bufferSent = new byte[4];
bufferSent[0] = SOCKS_VERSION;
bufferSent[1] = (byte) ACCEPTABLE_METHODS;
bufferSent[2] = (byte) NO_AUTHENTICATION_REQUIRED_METHOD;
bufferSent[3] = (byte) USERNAME_PASSWORD_METHOD;
final OutputStream outputStream = socket.getOutputStream();
outputStream.write(bufferSent);
outputStream.flush();
LOGGER.trace("{}", MiscUtil.buildLogString(bufferSent, false));
// Received data.
final InputStream inputStream = socket.getInputStream();
final byte[] receivedData = read2Bytes(inputStream);
LOGGER.trace("{}", MiscUtil.buildLogString(receivedData, true));
if (receivedData[0] != (int) SOCKS_VERSION) {
throw new SocksException("Remote server don't support SOCKS5");
}
final byte command = receivedData[1];
if (command != NO_AUTHENTICATION_REQUIRED_METHOD && command != USERNAME_PASSWORD_METHOD) {
throw new SocksException("requested authentication method not supported: " + command);
}
return command == USERNAME_PASSWORD_METHOD;
}
Aggregations