1 // Copyright 2000-2005 the Contributors, as shown in the revision logs.
2 // Licensed under the Apache Public Source License 2.0 ("the License").
3 // You may not use this file except in compliance with the License.
5 package org.ibex.net.ssl;
8 //import org.bouncycastle.asn1.*;
9 //import org.bouncycastle.asn1.x509.*;
11 public class GenCompactCAList {
13 public static void main(String[] args) throws Exception {
14 if(args.length < 2) throw new Exception("Usage: GenCAList format file(s)");
15 String format = args[0];
16 DER.EncodableVector vec = new DEREncodableVector();
17 for(int i=1;i<args.length;i++) {
18 X509.CertificateStructure x509 = new X509.CertificateStructure((ASN1Sequence) new ASN1InputStream(new FileInputStream(args[i])).readObject());
19 X509.Name subject = x509.getSubject();
20 SubjectPublicKeyInfo pki = x509.getSubjectPublicKeyInfo();
21 RSA.PublicKeyStructure rsa = new RSA.PublicKeyStructure((ASN1Sequence) pki.getPublicKey());
22 DER.EncodableVector vec2 = new DEREncodableVector();
25 vec.add(new DERSequence(vec2));
27 if(format.equals("binary")) {
28 DER.OutputStream dos = new DEROutputStream(System.out);
29 dos.writeObject(new DERSequence(vec));
31 } else if(format.equals("class")){
32 ByteArrayOutputStream baos = new ByteArrayOutputStream();
33 DER.OutputStream dos = new DEROutputStream(baos);
34 dos.writeObject(new DERSequence(vec));
37 byte[] buf = baos.toByteArray();
38 StringBuffer sb = new StringBuffer();
39 for(int i=0;i<buf.length;i+=7) {
41 for(int j=0;j<7;j++) {
43 byte b = (i+j < buf.length) ? buf[i+j] : -1;
46 for(int j=0;j<8;j++) {
47 char c = (char) ((l>>>(7*(7-j)))&0x7f);
48 if(c=='\n') sb.append("\\n");
49 else if(c=='\r') sb.append("\\r");
50 else if(c=='\\') sb.append("\\\\");
51 else if(c=='"') sb.append("\\\"");
52 else if(c >= 32 && c <= 126) sb.append(c);
53 else sb.append("\\" + toOctal3(c));
56 System.out.println("package org.ibex.net.ssl;");
57 System.out.println("public final class RootCerts {");
58 System.out.println(" private final static String DATA = \"" + sb.toString() + "\";");
62 " org.ibex.net.SSL.addCompactCAKeys(new java.io.ByteArrayInputStream(unpack(DATA)));\n" +
63 " } catch(Exception e) {\n" +
64 " System.err.println(\"Error loading root CA keys: \" + e.getMessage());\n" +
67 System.out.println(" public static void load() { }"); // force clinit
69 " private static byte[] unpack(String s) {\n" +
70 " int len = s.length();\n" +
71 " if(len % 8 != 0) throw new IllegalArgumentException(\"not a multiple of 8\");\n" +
72 " byte[] ret = new byte[(len / 8) * 7];\n" +
73 " for(int i=0; i<len; i += 8) {\n" +
75 " for(int j=0;j<8;j++) {\n" +
77 " l |= (s.charAt(i + j) & 0x7fL);\n" +
79 " int base = (i / 8) * 7;\n" +
80 " for(int j=6; j>=0; j--) {\n" +
81 " ret[base + j] = (byte)(l & 0xff);\n" +
87 System.out.println("}");
89 throw new Error("unknown format");
93 private final static String toOctal3(int n) {
94 char[] buf = new char[3];
95 for(int i=2;i>=0;i--) {
96 buf[i] = (char) ('0' + (n & 7));
99 return new String(buf);