working along to decode the DEROctetString
This commit is contained in:
parent
5a3abc75a1
commit
b07f5a05e8
6 changed files with 285 additions and 24 deletions
|
@ -4,10 +4,13 @@ import java.util.List;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
|
||||
import java.io.InputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.InputStreamReader;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.BufferedInputStream;
|
||||
import java.io.File;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
|
@ -15,46 +18,224 @@ import java.io.IOException;
|
|||
import com.redhat.trie.PathNode;
|
||||
import com.redhat.trie.Util;
|
||||
|
||||
import java.util.zip.Inflater;
|
||||
import java.util.zip.InflaterOutputStream;
|
||||
import java.util.zip.Deflater;
|
||||
import java.util.zip.DeflaterOutputStream;
|
||||
import java.util.zip.DataFormatException;
|
||||
import java.security.cert.Certificate;
|
||||
import java.security.cert.CertificateException;
|
||||
import java.security.cert.X509Certificate;
|
||||
import java.security.cert.CertificateFactory;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
|
||||
import org.bouncycastle.asn1.*;
|
||||
//import org.bouncycastle.asn1.ASN1Encodable;
|
||||
//import org.bouncycastle.asn1.ASN1InputStream;
|
||||
import org.bouncycastle.x509.extension.X509ExtensionUtil;
|
||||
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
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 {
|
||||
InputStream is = new FileInputStream(file);
|
||||
|
||||
// Get the size of the file
|
||||
long length = file.length();
|
||||
|
||||
// You cannot create an array using a long type.
|
||||
// It needs to be an int type.
|
||||
// Before converting to an int type, check
|
||||
// to ensure that file is not larger than Integer.MAX_VALUE.
|
||||
if (length > Integer.MAX_VALUE) {
|
||||
// File is too large
|
||||
}
|
||||
|
||||
// Create the byte array to hold the data
|
||||
byte[] bytes = new byte[(int)length];
|
||||
|
||||
// Read in the bytes
|
||||
int offset = 0;
|
||||
int numRead = 0;
|
||||
while (offset < bytes.length
|
||||
&& (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
|
||||
offset += numRead;
|
||||
}
|
||||
|
||||
// Ensure all the bytes have been read in
|
||||
if (offset < bytes.length) {
|
||||
throw new IOException("Could not completely read file "+file.getName());
|
||||
}
|
||||
|
||||
// Close the input stream and return bytes
|
||||
is.close();
|
||||
return bytes;
|
||||
}
|
||||
|
||||
|
||||
public static List<String> hydrateFromBytes(byte[] derblob) {
|
||||
Util util = new Util();
|
||||
|
||||
try {
|
||||
return util.hydrateContentPackage(derblob);
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static List<String> hydrateFromFile(String filename) {
|
||||
try {
|
||||
return hydrateFromBytes(getBytesFromFile(new File(filename)));
|
||||
} catch (IOException ex) {
|
||||
System.out.println(ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void showTreeFromCSFIle(String filename) {
|
||||
FileInputStream fis;
|
||||
DataInputStream in;
|
||||
BufferedReader br;
|
||||
|
||||
String content;
|
||||
List<String> contentList;
|
||||
Util util = new Util();
|
||||
|
||||
try {
|
||||
fis = new FileInputStream(filename);
|
||||
} catch (FileNotFoundException ex) {
|
||||
System.out.printf("ERROR: failed to find file %s\n", filename);
|
||||
return;
|
||||
} catch (Throwable t) {
|
||||
System.out.printf("ERROR: [%s] %s\n", filename, t);
|
||||
return;
|
||||
}
|
||||
|
||||
in = new DataInputStream(fis);
|
||||
br = new BufferedReader(new InputStreamReader(in));
|
||||
contentList = new ArrayList<String>();
|
||||
|
||||
try {
|
||||
while ((content = br.readLine()) != null) {
|
||||
contentList.add(content);
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
System.out.printf("ERROR: [%s] - %s\n", filename, ex);
|
||||
return;
|
||||
}
|
||||
|
||||
//System.out.println(contentList.toString());
|
||||
PathNode root = new PathNode();
|
||||
util.makePathTree(contentList, root);
|
||||
Util.printTree(root, 0);
|
||||
}
|
||||
|
||||
public static ASN1Encodable objectFromCertOid(String certFilename, String oid) {
|
||||
X509Certificate cert;
|
||||
cert = certFromFile(certFilename);
|
||||
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 X509Certificate certFromFile(String certFilename) {
|
||||
FileInputStream fis;
|
||||
BufferedInputStream bis;
|
||||
CertificateFactory cf;
|
||||
X509Certificate cert;
|
||||
|
||||
try {
|
||||
fis = new FileInputStream(certFilename);
|
||||
} catch (FileNotFoundException ex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
bis = new BufferedInputStream(fis);
|
||||
|
||||
try {
|
||||
cf = CertificateFactory.getInstance("X.509");
|
||||
} catch (CertificateException ex) {
|
||||
return null;
|
||||
}
|
||||
|
||||
try {
|
||||
while (bis.available() > 0) {
|
||||
cert = (X509Certificate) cf.generateCertificate(bis);
|
||||
return cert;
|
||||
}
|
||||
} catch (IOException ex) {
|
||||
} catch (CertificateException cex) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (String arg : args) {
|
||||
try {
|
||||
fis = new FileInputStream(arg);
|
||||
} catch (FileNotFoundException ex) {
|
||||
System.out.printf("ERROR: failed to find file %s\n", arg);
|
||||
continue;
|
||||
} catch (Throwable t) {
|
||||
System.out.printf("ERROR: [%s] %s\n", arg, t);
|
||||
continue;
|
||||
}
|
||||
//showTreeFromCSFIle(arg);
|
||||
//showTreeFromCSFIle(arg);
|
||||
//bytesFromCertOid(arg, "1.3.6.1.4.1.2312.9.7");
|
||||
//System.out.println(objectFromCertOid(arg, "1.3.6.1.4.1.2312.9.7").toString());
|
||||
|
||||
in = new DataInputStream(fis);
|
||||
br = new BufferedReader(new InputStreamReader(in));
|
||||
contentList = new ArrayList<String>();
|
||||
//System.out.println(objectFromCertOid(arg, "1.3.6.1.4.1.2312.9.7").getClass().getName());
|
||||
DEROctetString dos;
|
||||
byte[] bytes;
|
||||
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||
List<String> contents;
|
||||
|
||||
dos = (DEROctetString)objectFromCertOid(arg, "1.3.6.1.4.1.2312.9.7");
|
||||
try {
|
||||
while ((content = br.readLine()) != null) {
|
||||
contentList.add(content);
|
||||
}
|
||||
baos.write(dos.getOctets());
|
||||
} catch (IOException ex) {
|
||||
System.out.printf("ERROR: [%s] - %s\n", arg, ex);
|
||||
continue;
|
||||
System.out.println(ex);
|
||||
}
|
||||
bytes = decompress(baos.toByteArray());
|
||||
contents = hydrateFromBytes(bytes);
|
||||
if (contents != null) {
|
||||
for (String content : contents) {
|
||||
System.out.println(content);
|
||||
}
|
||||
}
|
||||
|
||||
//System.out.println(contentList.toString());
|
||||
|
||||
PathNode root = new PathNode();
|
||||
util.makePathTree(contentList, root);
|
||||
|
||||
Util.printTree(root, 0);
|
||||
//X509Certificate cert = certFromFile(arg);
|
||||
//System.out.println(cert.toString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -521,6 +521,7 @@ public class Util {
|
|||
pathDictionary.add(new HuffNode(HuffNode.END_NODE, weight));
|
||||
HuffNode pathTrie = makeTrie(pathDictionary);
|
||||
|
||||
// setup input stream, offset by the dictionary, to the end
|
||||
StringBuffer nodeBits = new StringBuffer();
|
||||
ByteArrayInputStream bais = new ByteArrayInputStream(payload,
|
||||
(new Long(read)).intValue(), (new Long(payload.length - read).intValue()));
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue