Stop suppressing exceptions

This commit is contained in:
Cameron Gutman 2019-01-01 23:28:41 -08:00
parent 05e4792d6f
commit 0220dd921a
3 changed files with 61 additions and 37 deletions

View File

@ -239,7 +239,7 @@ public class NvConnection {
return; return;
} }
context.connListener.stageComplete(appName); context.connListener.stageComplete(appName);
} catch (Exception e) { } catch (XmlPullParserException | IOException e) {
e.printStackTrace(); e.printStackTrace();
context.connListener.displayMessage(e.getMessage()); context.connListener.displayMessage(e.getMessage());
context.connListener.stageFailed(appName, 0); context.connListener.stageFailed(appName, 0);

View File

@ -288,6 +288,7 @@ public class NvHTTP {
client.setSslSocketFactory(sc.getSocketFactory()); client.setSslSocketFactory(sc.getSocketFactory());
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
throw new RuntimeException(e);
} }
} }

View File

@ -14,7 +14,6 @@ import com.limelight.LimeLog;
import java.security.cert.Certificate; import java.security.cert.Certificate;
import java.io.*; import java.io.*;
import java.net.MalformedURLException;
import java.security.*; import java.security.*;
import java.security.cert.*; import java.security.cert.*;
import java.util.Arrays; import java.util.Arrays;
@ -68,13 +67,19 @@ public class PairingManager {
return data; return data;
} }
private X509Certificate extractPlainCert(String text) throws XmlPullParserException, IOException, CertificateException private X509Certificate extractPlainCert(String text) throws XmlPullParserException, IOException
{ {
String certText = NvHTTP.getXmlString(text, "plaincert"); String certText = NvHTTP.getXmlString(text, "plaincert");
if (certText != null) { if (certText != null) {
byte[] certBytes = hexToBytes(certText); byte[] certBytes = hexToBytes(certText);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
return (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(certBytes)); try {
CertificateFactory cf = CertificateFactory.getInstance("X.509");
return (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(certBytes));
} catch (CertificateException e) {
e.printStackTrace();
return null;
}
} }
else { else {
return null; return null;
@ -95,45 +100,63 @@ public class PairingManager {
return saltedPin; return saltedPin;
} }
private static boolean verifySignature(byte[] data, byte[] signature, Certificate cert) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException { private static boolean verifySignature(byte[] data, byte[] signature, Certificate cert) {
Signature sig = Signature.getInstance("SHA256withRSA"); try {
sig.initVerify(cert.getPublicKey()); Signature sig = Signature.getInstance("SHA256withRSA");
sig.update(data); sig.initVerify(cert.getPublicKey());
return sig.verify(signature); sig.update(data);
return sig.verify(signature);
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} }
private static byte[] signData(byte[] data, PrivateKey key) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException { private static byte[] signData(byte[] data, PrivateKey key) {
Signature sig = Signature.getInstance("SHA256withRSA"); try {
sig.initSign(key); Signature sig = Signature.getInstance("SHA256withRSA");
sig.update(data); sig.initSign(key);
byte[] signature = new byte[256]; sig.update(data);
sig.sign(signature, 0, signature.length); byte[] signature = new byte[256];
return signature; sig.sign(signature, 0, signature.length);
return signature;
} catch (NoSuchAlgorithmException | SignatureException | InvalidKeyException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} }
private static byte[] decryptAes(byte[] encryptedData, SecretKey secretKey) throws NoSuchAlgorithmException, SignatureException, private static byte[] decryptAes(byte[] encryptedData, SecretKey secretKey) {
InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException { try {
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
int blockRoundedSize = ((encryptedData.length + 15) / 16) * 16; int blockRoundedSize = ((encryptedData.length + 15) / 16) * 16;
byte[] blockRoundedEncrypted = Arrays.copyOf(encryptedData, blockRoundedSize); byte[] blockRoundedEncrypted = Arrays.copyOf(encryptedData, blockRoundedSize);
byte[] fullDecrypted = new byte[blockRoundedSize]; byte[] fullDecrypted = new byte[blockRoundedSize];
cipher.init(Cipher.DECRYPT_MODE, secretKey); cipher.init(Cipher.DECRYPT_MODE, secretKey);
cipher.doFinal(blockRoundedEncrypted, 0, cipher.doFinal(blockRoundedEncrypted, 0,
blockRoundedSize, fullDecrypted); blockRoundedSize, fullDecrypted);
return fullDecrypted; return fullDecrypted;
} catch (GeneralSecurityException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} }
private static byte[] encryptAes(byte[] data, SecretKey secretKey) throws NoSuchAlgorithmException, SignatureException, private static byte[] encryptAes(byte[] data, SecretKey secretKey) {
InvalidKeyException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException { try {
Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding"); Cipher cipher = Cipher.getInstance("AES/ECB/NoPadding");
int blockRoundedSize = ((data.length + 15) / 16) * 16; int blockRoundedSize = ((data.length + 15) / 16) * 16;
byte[] blockRoundedData = Arrays.copyOf(data, blockRoundedSize); byte[] blockRoundedData = Arrays.copyOf(data, blockRoundedSize);
cipher.init(Cipher.ENCRYPT_MODE, secretKey); cipher.init(Cipher.ENCRYPT_MODE, secretKey);
return cipher.doFinal(blockRoundedData); return cipher.doFinal(blockRoundedData);
} catch (GeneralSecurityException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
} }
private static SecretKey generateAesKey(PairingHashAlgorithm hashAlgo, byte[] keyData) { private static SecretKey generateAesKey(PairingHashAlgorithm hashAlgo, byte[] keyData) {
@ -155,7 +178,7 @@ public class PairingManager {
r.nextInt(10), r.nextInt(10)); r.nextInt(10), r.nextInt(10));
} }
public PairState getPairState(String serverInfo) throws MalformedURLException, IOException, XmlPullParserException { public PairState getPairState(String serverInfo) throws IOException, XmlPullParserException {
if (!NvHTTP.getXmlString(serverInfo, "PairStatus").equals("1")) { if (!NvHTTP.getXmlString(serverInfo, "PairStatus").equals("1")) {
return PairState.NOT_PAIRED; return PairState.NOT_PAIRED;
} }
@ -167,7 +190,7 @@ public class PairingManager {
return serverCert; return serverCert;
} }
public PairState pair(String serverInfo, String pin) throws MalformedURLException, IOException, XmlPullParserException, CertificateException, InvalidKeyException, NoSuchAlgorithmException, SignatureException, ShortBufferException, IllegalBlockSizeException, BadPaddingException, NoSuchPaddingException { public PairState pair(String serverInfo, String pin) throws IOException, XmlPullParserException {
PairingHashAlgorithm hashAlgo; PairingHashAlgorithm hashAlgo;
int serverMajorVersion = http.getServerMajorVersion(serverInfo); int serverMajorVersion = http.getServerMajorVersion(serverInfo);