summaryrefslogtreecommitdiff
path: root/src/org/noreply/fancydress/type3/mmtp/MMTP.java
blob: bc19e8efa07cf32b622250c659bf953bb5d2807c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* $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();
	}
}