Permalink
Browse files

Merge branch 'master' of github.com:CSEMike/OneSwarm

  • Loading branch information...
2 parents b3d5143 + 0cd3d3f commit d744134f78bd8d62aac0ea8b76ce8c69bddd624f @willscott willscott committed Apr 16, 2012
Showing with 5,354 additions and 0 deletions.
  1. +74 −0 xmpp_publickey/build.xml
  2. BIN xmpp_publickey/lib/derby.jar
  3. BIN xmpp_publickey/lib/derbytools.jar
  4. BIN xmpp_publickey/lib/mysql-connector-java-5.1.7-bin.jar
  5. BIN xmpp_publickey/lib/smack.jar
  6. +28 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/CryptoHandler.java
  7. +37 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/FriendNetwork.java
  8. +176 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/PublicKeyClient.java
  9. +150 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/PublicKeyFriend.java
  10. +182 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/PublicKeyFriendBean.java
  11. +140 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/PublicKeyServer.java
  12. +122 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/Tools.java
  13. +113 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/ssl/client/PublicKeySSLClient.java
  14. +303 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/ssl/server/PublicKeySSLServer.java
  15. +157 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/ssl/server/SSLKeyManager.java
  16. +297 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/storage/PersistentStorage.java
  17. +70 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/storage/sql/DatabaseJob.java
  18. +444 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/storage/sql/PersistentStorageSQL.java
  19. +125 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/storage/sql/Queries.java
  20. +361 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/storage/sql/QueryManager.java
  21. +76 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/storage/sql/derby/PersistentStorageDerby.java
  22. +132 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/storage/sql/derby/TablesDerby.java
  23. +66 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/storage/sql/mysql/PersistentStorageMySQL.java
  24. +34 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/storage/sql/mysql/QueryManagerMysql.java
  25. +131 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/storage/sql/mysql/TablesMySQL.java
  26. +54 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/xmpp/XMPPNetwork.java
  27. +355 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/xmpp/client/PublicKeyXmppClient.java
  28. +220 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/xmpp/server/PublicKeyXmppServer.java
  29. +299 −0 xmpp_publickey/src/java/edu/washington/cs/publickey/xmpp/server/PublicKeyXmppServerProtocol.java
  30. +101 −0 xmpp_publickey/test/java/edu/washington/cs/publickey/PublicKeyFriendTest.java
  31. +43 −0 xmpp_publickey/test/java/edu/washington/cs/publickey/ssl/client/PublicKeySSLClientTest.java
  32. +106 −0 xmpp_publickey/test/java/edu/washington/cs/publickey/ssl/server/PublicKeySSLServerTest.java
  33. +211 −0 xmpp_publickey/test/java/edu/washington/cs/publickey/storage/sql/PersistentStorageSQLBigFatTest.java
  34. +237 −0 xmpp_publickey/test/java/edu/washington/cs/publickey/storage/sql/PersistentStorageSQLTest.java
  35. +37 −0 ...key/test/java/edu/washington/cs/publickey/storage/sql/derby/PersistentStorageDerbyBigFatTest.java
  36. +55 −0 ...publickey/test/java/edu/washington/cs/publickey/storage/sql/derby/PersistentStorageDerbyTest.java
  37. +24 −0 ...key/test/java/edu/washington/cs/publickey/storage/sql/mysql/PersistentStorageMySQLBigFatTest.java
  38. +23 −0 ...publickey/test/java/edu/washington/cs/publickey/storage/sql/mysql/PersistentStorageMySQLTest.java
  39. +58 −0 xmpp_publickey/test/java/edu/washington/cs/publickey/xmpp/XmppStuffTest.java
  40. +238 −0 xmpp_publickey/test/java/edu/washington/cs/publickey/xmpp/client/PublicKeyCreator.java
  41. +75 −0 xmpp_publickey/test/java/edu/washington/cs/publickey/xmpp/client/PublicKeyXmppClientTest.java
View
@@ -0,0 +1,74 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<project name="publickey" default="all" basedir=".">
+ <taskdef resource="net/sf/antcontrib/antlib.xml" />
+
+ <description>
+ Publickey build file
+ </description>
+
+ <property name="smack.jar" value="lib/smack.jar" />
+ <property name="derby.jar" value="lib/derby.jar" />
+ <property name="derbytools.jar" value="lib/derbytools.jar" />
+ <property name="mysql.jar" value="lib/mysql-connector-java-5.1.7-bin.jar" />
+
+ <property name="bin.dir" value="bin.ant" />
+
+ <!-- set classpath -->
+ <path id="publickey.class.path">
+ <pathelement path="${derby.jar}" />
+ <pathelement path="${smack.jar}" />
+ <pathelement path="${derbytools.jar}" />
+ <pathelement path="${mysql.jar}" />
+ </path>
+
+ <target name="compile" description="compile the code">
+ <mkdir dir="${bin.dir}" />
+ <javac srcdir="src/java" destdir="${bin.dir}" nowarn="yes" source="1.5" target="1.5" includeAntRuntime="no" debug="true" debuglevel="lines,vars,source" fork="yes" memoryMaximumSize="256m">
+ <classpath refid="publickey.class.path" />
+ </javac>
+
+ </target>
+
+
+ <target name="all" depends="client-jar,server-jar" />
+
+ <target name="client-jar" depends="compile" description="Create the client jar">
+ <mkdir dir="build" />
+ <jar destfile="build/publickey-client.jar">
+ <fileset dir="${bin.dir}">
+ <include name="**/*.class" />
+ </fileset>
+ <!-- include the smack.jar file in the release jar -->
+ <!--<zipfileset excludes="META-INF/*.SF" src="${smack.jar}" />-->
+ </jar>
+
+ </target>
+
+ <target name="server-jar" depends="compile" description="Create the server jar">
+ <mkdir dir="build" />
+ <jar destfile="build/publickey-server.jar">
+ <manifest>
+ <attribute name="Built-By" value="${user.name}" />
+ <attribute name="Main-Class" value="edu.washington.cs.publickey.PublicKeyServer" />
+ <attribute name="Class-Path" value="." />
+ </manifest>
+ <fileset dir="${bin.dir}">
+ <include name="**/*.class" />
+ </fileset>
+ <!-- include the smack + derby.jar file in the release jar -->
+ <zipfileset excludes="META-INF/*.SF" src="${smack.jar}" />
+ <zipfileset excludes="META-INF/*.SF" src="${derby.jar}" />
+ <zipfileset excludes="META-INF/*.SF" src="${derbytools.jar}" />
+ <zipfileset excludes="META-INF/*.SF" src="${mysql.jar}" />
+ </jar>
+
+ </target>
+
+ <target name="clean">
+ <!-- Delete the bin directory tree -->
+ <delete failonerror="false">
+ <fileset dir="${bin.dir}" includes="**/*.class" />
+ <fileset dir="build" includes="**/*" />
+ </delete>
+ </target>
+</project>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,28 @@
+/**
+ *
+ */
+package edu.washington.cs.publickey;
+
+import java.security.PublicKey;
+
+import javax.net.ssl.SSLContext;
+
+/**
+ * @author isdal
+ *
+ */
+public interface CryptoHandler {
+
+ /**
+ * Sign the date using a RSA public key
+ *
+ * @param data
+ * @return the signature
+ * @throws Exception
+ */
+ public byte[] sign(byte[] data) throws Exception;
+
+ public SSLContext getSSLContext() throws Exception;
+
+ public PublicKey getPublicKey();
+}
@@ -0,0 +1,37 @@
+/**
+ *
+ */
+package edu.washington.cs.publickey;
+
+/**
+ * @author isdal
+ *
+ */
+public enum FriendNetwork {
+ XMPP_GOOGLE(0, "XMPP (Google)");
+ private final static FriendNetwork[] val = { XMPP_GOOGLE };
+ private final int networkId;
+
+ private final String networkName;
+
+ private FriendNetwork(int networkId, String networkName) {
+ this.networkId = networkId;
+ this.networkName = networkName;
+ }
+
+ public int getNetworkId() {
+ return networkId;
+ }
+
+ public String getNetworkName() {
+ return networkName;
+ }
+
+ public static FriendNetwork getFromId(int id) {
+ if (id < val.length && id >= 0) {
+ return val[id];
+ }
+
+ return null;
+ }
+}
@@ -0,0 +1,176 @@
+package edu.washington.cs.publickey;
+
+import java.io.BufferedReader;
+import java.io.BufferedWriter;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.UnsupportedEncodingException;
+import java.security.NoSuchAlgorithmException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+
+import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
+
+public abstract class PublicKeyClient {
+ private final static String DEFAULT_EXISTING_FRIENDS = "friends.publickeyfriends";
+ public static boolean logToStdOut = false;
+
+ private static void log(String msg) {
+ if (logToStdOut) {
+ System.out.println(msg);
+ }
+ }
+
+ private final File existingFriendsFile;
+
+ protected List<PublicKeyFriend> knownFriends;
+ protected List<byte[]> knownKeys;
+
+ public PublicKeyClient(File existingFriendsFile, List<byte[]> knownKeys) {
+ this.existingFriendsFile = existingFriendsFile;
+ this.knownKeys = knownKeys;
+ this.loadKnownFriends();
+ }
+
+ protected void addKnownFriends(List<PublicKeyFriend> newFriends) throws IOException {
+ log("adding friends:" + newFriends.size());
+
+ HashMap<String, PublicKeyFriend> netUidHashToUid = new HashMap<String, PublicKeyFriend>();
+ HashMap<String, Boolean> knownPubKeys = new HashMap<String, Boolean>();
+ for (PublicKeyFriend f : knownFriends) {
+ String netUid = Base64.encode(f.getSourceNetworkUid());
+ netUidHashToUid.put(netUid, f);
+ if (f.getPublicKey() != null) {
+ String publicKey = Base64.encode(f.getPublicKey());
+ knownPubKeys.put(publicKey, true);
+ }
+ /*
+ * check if any of the known friends does not have any realname, in
+ * that case search for it
+ */
+ if (f.getRealName() == null) {
+ for (PublicKeyFriend newFriend : newFriends) {
+ if (Arrays.equals(newFriend.getSourceNetworkUid(), f.getSourceNetworkUid())) {
+ if (f.getRealName() == null) {
+ log("Updating friend, just got the realname");
+ f.setRealName(newFriend.getRealName());
+ }
+ }
+ }
+ }
+ }
+ // for each entry, check if we already know about that friend
+ // if not, add, if we do, update or ignore
+ for (PublicKeyFriend newFriend : newFriends) {
+ // users can either be added if they are the first user with a
+ // certain uid
+ String netUid = Base64.encode(newFriend.getSourceNetworkUid());
+ if (!netUidHashToUid.containsKey(netUid)) {
+ knownFriends.add(newFriend);
+ log("adding: " + newFriend);
+ } else if (newFriend.getPublicKey() != null) {
+ // or if we don't have that public key
+ String publicKey = Base64.encode(newFriend.getPublicKey());
+ if (!knownPubKeys.containsKey(publicKey)) {
+ // check if we need to add the real name
+ // we might have a dummy user in there
+ PublicKeyFriend existing = netUidHashToUid.get(netUid);
+ if (existing.getPublicKey() == null) {
+ log("updating: " + existing);
+ existing.setPublicKey(newFriend.getPublicKey());
+ existing.setPublicKeySha1(newFriend.getPublicKeySha1());
+ existing.setKeyNick(newFriend.getKeyNick());
+ log(existing.toString());
+ } else {
+ // the existing one already has a public key
+ // this must mean that this is the second+ for that user
+ newFriend.setRealName(existing.getRealName());
+ knownFriends.add(newFriend);
+ log("adding: " + newFriend);
+ }
+ } else {
+ if (newFriend.getRealName() == null) {
+ log("strange, new public key but no uid->realname mapping: " + newFriend);
+ }
+ }
+ }
+ }
+
+ File friendPath = existingFriendsFile;
+ if (friendPath == null) {
+ friendPath = new File(DEFAULT_EXISTING_FRIENDS);
+ }
+ /*
+ * create the parent directory if not exists
+ */
+ if (!friendPath.exists()) {
+ File parent = friendPath.getParentFile();
+ if (parent != null && !parent.exists()) {
+ parent.mkdirs();
+ }
+ }
+
+ String serialized = PublicKeyFriend.serialize(knownFriends.toArray(new PublicKeyFriend[knownFriends.size()]));
+ BufferedWriter out = new BufferedWriter(new FileWriter(friendPath));
+ out.write(serialized);
+ out.close();
+ }
+
+ public abstract void connect() throws Exception;
+
+ public abstract void disconnect() throws Exception;
+
+ public List<PublicKeyFriend> getFriends() {
+ return knownFriends;
+ }
+
+ protected List<byte[]> getKnownKeySha1s() {
+ List<byte[]> knownKeyList = new ArrayList<byte[]>();
+ for (byte[] key : knownKeys) {
+ try {
+ knownKeyList.add(Tools.getSha1(key));
+ } catch (NoSuchAlgorithmException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ } catch (UnsupportedEncodingException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+ return knownKeyList;
+ }
+
+ private void loadKnownFriends() {
+ knownFriends = new ArrayList<PublicKeyFriend>();
+ File friendPath = existingFriendsFile;
+ if (friendPath == null) {
+ friendPath = new File(DEFAULT_EXISTING_FRIENDS);
+ }
+
+ StringBuilder b = new StringBuilder();
+ BufferedReader in;
+ try {
+ in = new BufferedReader(new FileReader(friendPath));
+ String line;
+ while ((line = in.readLine()) != null) {
+ b.append(line);
+ }
+ in.close();
+ knownFriends.addAll(Arrays.asList(PublicKeyFriend.deserialize(b.toString())));
+ } catch (FileNotFoundException e) {
+ log("existing friends file not found: " + friendPath);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
+ }
+
+ public abstract void updateFriends() throws Exception;
+
+}
Oops, something went wrong.

0 comments on commit d744134

Please sign in to comment.