Move the RSA key parsing into NetworkConstants so that the modulus and exponent can be immutable.

This commit is contained in:
Major-
2014-08-08 06:41:24 +01:00
parent caa0eda7ee
commit 1ecdedd2e8
3 changed files with 38 additions and 86 deletions
-6
View File
@@ -16,7 +16,6 @@ import org.apollo.game.model.World.RegistrationStatus;
import org.apollo.game.model.entity.Player; import org.apollo.game.model.entity.Player;
import org.apollo.game.sync.ClientSynchronizer; import org.apollo.game.sync.ClientSynchronizer;
import org.apollo.io.EventHandlerChainParser; import org.apollo.io.EventHandlerChainParser;
import org.apollo.io.RsaKeyParser;
import org.apollo.login.LoginService; import org.apollo.login.LoginService;
import org.apollo.net.session.GameSession; import org.apollo.net.session.GameSession;
import org.apollo.util.NamedThreadFactory; import org.apollo.util.NamedThreadFactory;
@@ -120,11 +119,6 @@ public final class GameService extends Service {
Class<?> clazz = Class.forName(activeNode.getValue()); Class<?> clazz = Class.forName(activeNode.getValue());
synchronizer = (ClientSynchronizer) clazz.newInstance(); synchronizer = (ClientSynchronizer) clazz.newInstance();
} }
try (InputStream is = new FileInputStream("data/rsa.xml")) {
RsaKeyParser parser = new RsaKeyParser(is);
parser.parse();
}
} }
/** /**
-66
View File
@@ -1,66 +0,0 @@
package org.apollo.io;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import org.apollo.net.NetworkConstants;
import org.apollo.util.xml.XmlNode;
import org.apollo.util.xml.XmlParser;
import org.xml.sax.SAXException;
/**
* A class that parses the {@code rsa.xml} file.
*
* @author Major
*/
public final class RsaKeyParser {
/**
* The source {@link InputStream}.
*/
private final InputStream is;
/**
* The {@link XmlParser} instance.
*/
private final XmlParser parser;
/**
* Creates the RSA specification parser.
*
* @param is The source {@link InputStream}.
* @throws SAXException If a SAX error occurs.
*/
public RsaKeyParser(InputStream is) throws SAXException {
this.is = is;
parser = new XmlParser();
}
/**
* Parses the {@code rsa.xml} file.
*
* @throws SAXException If a SAX error occurs.
* @throws IOException
*/
public void parse() throws SAXException, IOException {
XmlNode rootNode = parser.parse(is);
if (!rootNode.getName().equals("rsa")) {
throw new IOException("Root node name is not 'rsa'.");
}
XmlNode modulusNode = rootNode.getChild("modulus");
if (modulusNode == null) {
throw new IOException("No node named 'modulus' beneath root node.");
}
XmlNode exponentNode = rootNode.getChild("private-exponent");
if (exponentNode == null) {
throw new IOException("No node named 'private-exponent' beneath root node.");
}
NetworkConstants.RSA_MODULUS = new BigInteger(modulusNode.getValue());
NetworkConstants.RSA_EXPONENT = new BigInteger(exponentNode.getValue());
}
}
+38 -14
View File
@@ -2,9 +2,14 @@ package org.apollo.net;
import io.netty.util.AttributeKey; import io.netty.util.AttributeKey;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger; import java.math.BigInteger;
import org.apollo.net.session.Session; import org.apollo.net.session.Session;
import org.apollo.util.xml.XmlNode;
import org.apollo.util.xml.XmlParser;
/** /**
* Holds various network-related constants such as port numbers. * Holds various network-related constants such as port numbers.
@@ -13,16 +18,6 @@ import org.apollo.net.session.Session;
*/ */
public final class NetworkConstants { public final class NetworkConstants {
/**
* The service port.
*/
public static final int SERVICE_PORT = 43594;
/**
* The JAGGRAB port.
*/
public static final int JAGGRAB_PORT = 43595;
/** /**
* The HTTP port. * The HTTP port.
*/ */
@@ -34,25 +29,54 @@ public final class NetworkConstants {
public static final int IDLE_TIME = 15; public static final int IDLE_TIME = 15;
/** /**
* The terminator of a string. * The JAGGRAB port.
*/ */
public static final int STRING_TERMINATOR = 10; public static final int JAGGRAB_PORT = 43595;
/** /**
* The exponent used when decrypting the RSA block. * The exponent used when decrypting the RSA block.
*/ */
public static BigInteger RSA_EXPONENT; public static final BigInteger RSA_EXPONENT;
/** /**
* The modulus used when decrypting the RSA block. * The modulus used when decrypting the RSA block.
*/ */
public static BigInteger RSA_MODULUS; public static final BigInteger RSA_MODULUS;
/**
* The service port.
*/
public static final int SERVICE_PORT = 43594;
/** /**
* The {@link Session} {@link AttributeKey}. * The {@link Session} {@link AttributeKey}.
*/ */
public static final AttributeKey<Session> SESSION_KEY = AttributeKey.valueOf("session"); public static final AttributeKey<Session> SESSION_KEY = AttributeKey.valueOf("session");
/**
* The terminator of a string.
*/
public static final int STRING_TERMINATOR = 10;
static {
try (InputStream is = new FileInputStream("data/rsa.xml")) {
XmlNode rsa = new XmlParser().parse(is);
if (!rsa.getName().equals("rsa")) {
throw new IOException("Root node name is not 'rsa'.");
}
XmlNode modulus = rsa.getChild("modulus"), exponent = rsa.getChild("private-exponent");
if (modulus == null || exponent == null) {
throw new IOException("Root node must have two children - 'modulus' and 'private-exponent'.");
}
RSA_MODULUS = new BigInteger(modulus.getValue());
RSA_EXPONENT = new BigInteger(exponent.getValue());
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
/** /**
* Default private constructor to prevent instantiation by other classes. * Default private constructor to prevent instantiation by other classes.
*/ */