working along to decode the DEROctetString

This commit is contained in:
Vincent Batts 2012-10-23 15:18:04 -04:00
parent 5a3abc75a1
commit b07f5a05e8
6 changed files with 285 additions and 24 deletions

View file

@ -18,7 +18,12 @@
<target name="compile" depends="init" <target name="compile" depends="init"
description="compile the source " > description="compile the source " >
<!-- Compile the java code from ${src.dir} into ${build.dir} --> <!-- Compile the java code from ${src.dir} into ${build.dir} -->
<javac srcdir="${src.dir}" destdir="${build.dir}"/> <javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}">
<classpath>
<pathelement path="${classpath}"/>
<pathelement location="lib/bcprov-jdk16-1.46.jar"/>
</classpath>
</javac>
</target> </target>
<target name="dist" depends="compile" <target name="dist" depends="compile"

70
data/test-certv3.pem Normal file
View file

@ -0,0 +1,70 @@
-----BEGIN CERTIFICATE-----
MIIDrjCCAxegAwIBAgIIPjHvQ1mahX4wDQYJKoZIhvcNAQEFBQAwUjExMC8GA1UE
AwwoanNlZmxlci1mMTQtY2FuZGxlcGluLnVzZXJzeXMucmVkaGF0LmNvbTELMAkG
A1UEBhMCVVMxEDAOBgNVBAcMB1JhbGVpZ2gwHhcNMTIwOTI0MDAwMDAwWhcNMTMw
OTI0MDAwMDAwWjArMSkwJwYDVQQDEyA4YTkwZjgxZDM5ZmFjMGVlMDEzOWZlMzhl
NGY2MzE3ZjCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAK+Dq57Pnq5i
IvRT+nyvSMvhxTyzoBQ67Do3AKG0LvRopKLq0gNjouuJCUrLSGIVy2yEnbM+HPnX
Nkh/QLsOc6QUcBSVlnFV/Xd/sXB+GtWoy1GzcmMNUUoNDbdIZjj+Omdm0/8ueofG
1QDOH4RZHR25lquDovkZU/WzSv5XKyiqPqFiHt+D8QFRXiHb7i6+yLThF4nNdtkk
97yai3xHRrRVqWhqR8nYIF7dPHsTByMUSaoRz5iKO5rmg9BBrZE4+kDZLFE+28wR
xtzD/Die2GdJwe7mQ5m0HUJC+1g9lyOruAyUK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svrmaWpcI4FKDCo/pDkHv44Ms/ukxRCG9eCy
ndmrxQKBgGJiXMZnKjK9AUUDvsFBSp8Otrf20BoeCiq+tZF95S5jR9/I8nv4NBYp
59zCR1DOxxbyAHbRCjqZxdpZVAqKBl1BP+cmw93sAwJ3v9m+V4wFHVUFLJGmkXXV
X2mYlgErL9vzxIQrwfL5JdEo9f+PQ0eVs/lh9MPY2TliwEyXDrVp
-----END RSA PRIVATE KEY-----

BIN
lib/bcprov-jdk16-1.46.jar Normal file

Binary file not shown.

4
run.sh Executable file
View file

@ -0,0 +1,4 @@
#!/bin/sh
java -cp ./lib/bcprov-jdk16-1.46.jar:$(ls -rt ./dist/*jar | tail -1) com.hashbangbash.trie.App ./data/test-certv3.pem

View file

@ -4,10 +4,13 @@ import java.util.List;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.io.InputStream;
import java.io.DataInputStream; import java.io.DataInputStream;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
@ -15,25 +18,127 @@ 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.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 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(); 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; FileInputStream fis;
DataInputStream in; DataInputStream in;
BufferedReader br; BufferedReader br;
String content; String content;
List<String> contentList; List<String> contentList;
Util util = new Util();
for (String arg : args) {
try { try {
fis = new FileInputStream(arg); fis = new FileInputStream(filename);
} catch (FileNotFoundException ex) { } catch (FileNotFoundException ex) {
System.out.printf("ERROR: failed to find file %s\n", arg); System.out.printf("ERROR: failed to find file %s\n", filename);
continue; return;
} catch (Throwable t) { } catch (Throwable t) {
System.out.printf("ERROR: [%s] %s\n", arg, t); System.out.printf("ERROR: [%s] %s\n", filename, t);
continue; return;
} }
in = new DataInputStream(fis); in = new DataInputStream(fis);
@ -45,17 +150,93 @@ public class App {
contentList.add(content); contentList.add(content);
} }
} catch (IOException ex) { } catch (IOException ex) {
System.out.printf("ERROR: [%s] - %s\n", arg, ex); System.out.printf("ERROR: [%s] - %s\n", filename, ex);
continue; return;
} }
//System.out.println(contentList.toString()); //System.out.println(contentList.toString());
PathNode root = new PathNode(); PathNode root = new PathNode();
util.makePathTree(contentList, root); util.makePathTree(contentList, root);
Util.printTree(root, 0); 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) {
//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());
//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 {
baos.write(dos.getOctets());
} catch (IOException ex) {
System.out.println(ex);
}
bytes = decompress(baos.toByteArray());
contents = hydrateFromBytes(bytes);
if (contents != null) {
for (String content : contents) {
System.out.println(content);
}
}
//X509Certificate cert = certFromFile(arg);
//System.out.println(cert.toString());
}
} }
} }

View file

@ -521,6 +521,7 @@ public class Util {
pathDictionary.add(new HuffNode(HuffNode.END_NODE, weight)); pathDictionary.add(new HuffNode(HuffNode.END_NODE, weight));
HuffNode pathTrie = makeTrie(pathDictionary); HuffNode pathTrie = makeTrie(pathDictionary);
// setup input stream, offset by the dictionary, to the end
StringBuffer nodeBits = new StringBuffer(); StringBuffer nodeBits = new StringBuffer();
ByteArrayInputStream bais = new ByteArrayInputStream(payload, ByteArrayInputStream bais = new ByteArrayInputStream(payload,
(new Long(read)).intValue(), (new Long(payload.length - read).intValue())); (new Long(read)).intValue(), (new Long(payload.length - read).intValue()));