cleaning up the things that are exclusive to a cli interface.

Preparing to dig into Util and hydrate-fu
This commit is contained in:
Vincent Batts 2012-10-25 16:18:42 -04:00
parent e0692ae64f
commit c07d6ab172
2 changed files with 62 additions and 47 deletions

View file

@ -17,8 +17,6 @@ import java.io.IOException;
import com.redhat.trie.PathNode; import com.redhat.trie.PathNode;
import com.redhat.trie.Util; import com.redhat.trie.Util;
import java.util.zip.Inflater;
import java.util.zip.DataFormatException;
import java.security.cert.CertificateException; import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.security.cert.CertificateFactory; import java.security.cert.CertificateFactory;
@ -31,35 +29,6 @@ import org.bouncycastle.x509.extension.X509ExtensionUtil;
public class App { public class App {
public static ASN1InputStream toASN1Stream(byte[] b) {
return new ASN1InputStream(new ByteArrayInputStream(b));
}
public final static byte[] decompress(byte[] input) {
Inflater inflator = new Inflater();
inflator.setInput(input);
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
byte[] buf = new byte[1024];
try {
while (true) {
int count = inflator.inflate(buf);
if (count > 0) {
bos.write(buf, 0, count);
} else if (count == 0 && inflator.finished()) {
break;
} else {
throw new RuntimeException("bad zip data, size:"
+ input.length);
}
}
} catch (DataFormatException t) {
throw new RuntimeException(t);
} finally {
inflator.end();
}
return bos.toByteArray();
}
public static byte[] getBytesFromFile(File file) throws IOException { public static byte[] getBytesFromFile(File file) throws IOException {
InputStream is = new FileInputStream(file); InputStream is = new FileInputStream(file);
@ -116,23 +85,21 @@ public class App {
return null; return null;
} }
public static void showTreeFromCSFIle(String filename) { public static List<String> listFromFile(String filename) throws IOException, FileNotFoundException {
FileInputStream fis; FileInputStream fis;
DataInputStream in; DataInputStream in;
BufferedReader br; BufferedReader br;
String content; String content;
List<String> contentList; List<String> contentList;
Util util = new Util();
try { try {
fis = new FileInputStream(filename); fis = new FileInputStream(filename);
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
System.out.printf("ERROR: failed to find file %s\n", filename); throw ex;
return;
} catch (Throwable t) { } catch (Throwable t) {
System.out.printf("ERROR: [%s] %s\n", filename, t); System.out.printf("ERROR: [%s] %s\n", filename, t);
return; throw t;
} }
in = new DataInputStream(fis); in = new DataInputStream(fis);
@ -143,13 +110,27 @@ public class App {
while ((content = br.readLine()) != null) { while ((content = br.readLine()) != null) {
contentList.add(content); contentList.add(content);
} }
} catch (IOException ex) {
throw ex;
}
return contentList;
}
public static void showTree(String filename) {
List<String> contentList;
try {
contentList = listFromFile(filename);
} catch (IOException ex) { } catch (IOException ex) {
System.out.printf("ERROR: [%s] - %s\n", filename, ex); System.out.printf("ERROR: [%s] - %s\n", filename, ex);
return; return;
} }
showTree(contentList);
}
//System.out.println(contentList.toString()); public static void showTree(List<String> contentList) {
PathNode root = new PathNode(); PathNode root = new PathNode();
Util util = new Util();
util.makePathTree(contentList, root); util.makePathTree(contentList, root);
Util.printTree(root, 0); Util.printTree(root, 0);
} }
@ -157,16 +138,7 @@ public class App {
public static ASN1Encodable objectFromCertOid(String certFilename, String oid) { public static ASN1Encodable objectFromCertOid(String certFilename, String oid) {
X509Certificate cert; X509Certificate cert;
cert = certFromFile(certFilename); cert = certFromFile(certFilename);
if (cert == null) { return null; } return Util.objectFromOid(cert,oid);
try {
for (String thisOid : cert.getNonCriticalExtensionOIDs()) {
if (thisOid.equals(oid)) {
return X509ExtensionUtil.fromExtensionValue(cert.getExtensionValue(oid));
}
}
} catch (IOException ex) { }
return null;
} }
public static X509Certificate certFromFile(String certFilename) { public static X509Certificate certFromFile(String certFilename) {

View file

@ -27,6 +27,7 @@ import java.util.zip.Deflater;
import java.util.zip.DeflaterOutputStream; import java.util.zip.DeflaterOutputStream;
import java.util.zip.Inflater; import java.util.zip.Inflater;
import java.util.zip.InflaterOutputStream; import java.util.zip.InflaterOutputStream;
import java.util.zip.DataFormatException;
import java.io.IOException; import java.io.IOException;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
@ -35,6 +36,10 @@ import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.security.cert.X509Certificate;
import org.bouncycastle.asn1.ASN1Encodable;
import org.bouncycastle.x509.extension.X509ExtensionUtil;
/* /*
* Util * Util
* *
@ -651,6 +656,44 @@ public class Util {
} }
} }
public static ASN1Encodable objectFromOid(X509Certificate cert, String oid) {
if (cert == null) { return null; }
try {
for (String thisOid : cert.getNonCriticalExtensionOIDs()) {
if (thisOid.equals(oid)) {
return X509ExtensionUtil.fromExtensionValue(cert.getExtensionValue(oid));
}
}
} catch (IOException ex) { }
return null;
}
public static byte[] decompress(byte[] input) {
Inflater inflator = new Inflater();
inflator.setInput(input);
ByteArrayOutputStream bos = new ByteArrayOutputStream(input.length);
byte[] buf = new byte[1024];
try {
while (true) {
int count = inflator.inflate(buf);
if (count > 0) {
bos.write(buf, 0, count);
} else if (count == 0 && inflator.finished()) {
break;
} else {
throw new RuntimeException("bad zip data, size:"
+ input.length);
}
}
} catch (DataFormatException t) {
throw new RuntimeException(t);
} finally {
inflator.end();
}
return bos.toByteArray();
}
} }