From 1ecdedd2e874bb9482154449bbfcd33ecc64994c Mon Sep 17 00:00:00 2001 From: Major- Date: Fri, 8 Aug 2014 06:41:24 +0100 Subject: [PATCH] Move the RSA key parsing into NetworkConstants so that the modulus and exponent can be immutable. --- src/org/apollo/game/GameService.java | 6 --- src/org/apollo/io/RsaKeyParser.java | 66 ------------------------ src/org/apollo/net/NetworkConstants.java | 52 ++++++++++++++----- 3 files changed, 38 insertions(+), 86 deletions(-) delete mode 100644 src/org/apollo/io/RsaKeyParser.java diff --git a/src/org/apollo/game/GameService.java b/src/org/apollo/game/GameService.java index 14ca7361..ecee01b0 100644 --- a/src/org/apollo/game/GameService.java +++ b/src/org/apollo/game/GameService.java @@ -16,7 +16,6 @@ import org.apollo.game.model.World.RegistrationStatus; import org.apollo.game.model.entity.Player; import org.apollo.game.sync.ClientSynchronizer; import org.apollo.io.EventHandlerChainParser; -import org.apollo.io.RsaKeyParser; import org.apollo.login.LoginService; import org.apollo.net.session.GameSession; import org.apollo.util.NamedThreadFactory; @@ -120,11 +119,6 @@ public final class GameService extends Service { Class clazz = Class.forName(activeNode.getValue()); synchronizer = (ClientSynchronizer) clazz.newInstance(); } - - try (InputStream is = new FileInputStream("data/rsa.xml")) { - RsaKeyParser parser = new RsaKeyParser(is); - parser.parse(); - } } /** diff --git a/src/org/apollo/io/RsaKeyParser.java b/src/org/apollo/io/RsaKeyParser.java deleted file mode 100644 index 090ce913..00000000 --- a/src/org/apollo/io/RsaKeyParser.java +++ /dev/null @@ -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()); - } - -} \ No newline at end of file diff --git a/src/org/apollo/net/NetworkConstants.java b/src/org/apollo/net/NetworkConstants.java index 0c13c117..af65c9f4 100644 --- a/src/org/apollo/net/NetworkConstants.java +++ b/src/org/apollo/net/NetworkConstants.java @@ -2,9 +2,14 @@ package org.apollo.net; import io.netty.util.AttributeKey; +import java.io.FileInputStream; +import java.io.IOException; +import java.io.InputStream; import java.math.BigInteger; 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. @@ -13,16 +18,6 @@ import org.apollo.net.session.Session; */ 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. */ @@ -34,25 +29,54 @@ public final class NetworkConstants { 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. */ - public static BigInteger RSA_EXPONENT; + public static final BigInteger RSA_EXPONENT; /** * 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}. */ public static final AttributeKey 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. */