/* $Id$ */ package org.noreply.fancydress.type3.mmtp; import org.noreply.fancydress.type3.routing.*; import org.noreply.fancydress.type3.*; import org.noreply.fancydress.crypto.*; import org.noreply.fancydress.misc.*; import javax.net.ssl.*; import java.net.*; import java.io.*; import java.security.*; public class MMTP { private static final String[] acceptableCipherSuits = {"TLS_DHE_RSA_WITH_AES_128_CBC_SHA"}; private static final String[] acceptableProtocols = {"TLSv1"}; //private static final String[] acceptableCipherSuits = {"SSL3_RSA_DES_192_CBC3_SHA"}; public static void send(Packet packet) throws Exception { byte[] hash = CryptoPrimitives.hash(packet.asOctets(), Util.toOctets("SEND")); byte[] ackExpected = Util.concat( Util.toOctets("RECEIVED\r\n"), CryptoPrimitives.hash(packet.asOctets(), Util.toOctets("RECEIVED"))); byte[] ackRead = new byte[30]; MMTPTrustManager trustManager = new MMTPTrustManager(packet.getRoute().getKeyID()); TrustManager[] trustManagers = { trustManager }; SSLContext context = SSLContext.getInstance("TLS"); context.init(null, trustManagers, null); SSLSocketFactory socketFactory = context.getSocketFactory(); //SSLSocketFactory socketFactory = (SSLSocketFactory) SSLSocketFactory.getDefault(); SSLSocket socket = (SSLSocket) socketFactory.createSocket("127.0.0.1", 48099); socket.setEnabledCipherSuites(acceptableCipherSuits); socket.setEnabledProtocols(acceptableProtocols); try { socket.startHandshake(); } catch (Exception e) {}; OutputStream out = socket.getOutputStream(); InputStream in = socket.getInputStream(); out.write(Util.toOctets("MMTP 0.3\r\n")); out.flush(); byte[] foo = new byte[10]; int got = in.read(foo, 0, foo.length); if ((got != 10) || (!Util.equal(foo, Util.toOctets("MMTP 0.3\r\n")))) { in.close(); socket.close(); throw new IOException("Do not agree on MMTP version."); } out.write(Util.toOctets("SEND\r\n")); out.write(packet.asOctets()); out.write(hash); out.flush(); got = in.read(ackRead, 0, ackRead.length); if ((got != ackExpected.length) || (!Util.equal(ackRead, ackExpected))) { System.out.println("Got NAK"); } else { System.out.println("Got ACK"); }; in.close(); out.close(); socket.close(); } }