licensing update to APSL 2.0
[org.ibex.net.git] / src / org / ibex / net / ssl / GenCompactCAList.java
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.
4
5 package org.ibex.net.ssl;
6
7 import java.io.*;
8 //import org.bouncycastle.asn1.*;
9 //import org.bouncycastle.asn1.x509.*;
10
11 public class GenCompactCAList {
12     /*
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();
23             vec2.add(subject);
24             vec2.add(rsa);
25             vec.add(new DERSequence(vec2));
26         }
27         if(format.equals("binary")) {
28             DER.OutputStream dos = new DEROutputStream(System.out);
29             dos.writeObject(new DERSequence(vec));
30             dos.close();
31         } else if(format.equals("class")){
32             ByteArrayOutputStream baos = new ByteArrayOutputStream();
33             DER.OutputStream dos = new DEROutputStream(baos);
34             dos.writeObject(new DERSequence(vec));
35             dos.close();
36             baos.close();            
37             byte[] buf = baos.toByteArray();
38             StringBuffer sb = new StringBuffer();
39             for(int i=0;i<buf.length;i+=7) {
40                 long l = 0;
41                 for(int j=0;j<7;j++) {
42                     l <<= 8;
43                     byte b = (i+j < buf.length) ? buf[i+j] : -1;
44                     l |= (b & 0xffL);
45                 }
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));
54                 }
55             }
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() + "\";");
59             System.out.print(
60                     "    static {\n" +
61                     "        try {\n" + 
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" + 
65                     "        }\n" +
66                     "    }\n");
67             System.out.println("    public static void load() {   }");  // force clinit
68             System.out.print(
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" + 
74                     "            long l = 0;\n" + 
75                     "            for(int j=0;j<8;j++) {\n" + 
76                     "                l <<= 7;\n" + 
77                     "                l |= (s.charAt(i + j) & 0x7fL);\n" + 
78                     "            }\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" + 
82                     "                l >>>= 8;\n" + 
83                     "            }\n" + 
84                     "        }\n" + 
85                     "        return ret;\n" + 
86                     "    }");
87             System.out.println("}");
88         } else {
89             throw new Error("unknown format");
90         }
91     }
92     
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));
97             n >>= 3;
98         }
99         return new String(buf);
100     }
101     */
102 }