FileDocCategorySizeDatePackage
Main.javaAPI DocphoneME MR2 API (J2ME)4729Wed May 02 18:00:40 BST 2007dummyCA

Main

public class Main extends Object

Fields Summary
Constructors Summary
Methods Summary
public static voidmain(java.lang.String[] args)


        if (args.length != 2) {
            System.out.println("Specify CSR and sertificate file names.");
            System.exit(1);
        }

        byte[] data = parseCSR(args[0]);

        Authority.init(null);
        Authority CA = new Authority();

        if (! CA.createCertificate(data)) {
            System.out.println(CA.getStatus());
            System.exit(1);
        }

//        writeBinary(args[1], CA.getCertificate());
        writeText(args[1], CA.getCertificate());

        System.out.println("Ok.");
    
private static byte[]parseCSR(java.lang.String arg)


        BufferedReader reader = null;

        try {
            reader = new BufferedReader(
                         new InputStreamReader(
                             new FileInputStream(new File(arg))));
        } catch (FileNotFoundException e) {
            System.out.println("Source file not found.");
            System.exit(1);
        }

        String s = "";

        String line = null;
        try {
            line = reader.readLine();
            while (line != null) {
                if (! line.startsWith("-")) {
                    s += line.trim();
                }
                line = reader.readLine();
            }
            reader.close();
        } catch (IOException e) {
            System.out.println("Error reading the CSR.");
            System.exit(1);
        }

        byte[] data = null;
        try {
            data = Base64.decode(s);
        } catch (IOException e) {
            System.out.println("Error decoding the CSR.");
            System.exit(1);
        }

        return data;
    
private static voidwriteArray(java.lang.String arg, byte[] data)


        PrintWriter writer = null;

        try {
            writer = new PrintWriter(new FileOutputStream(arg));
        } catch (IOException e) {
            System.out.println("Can't create output file.");
            System.exit(1);
        }

        for (int i = 0; i < data.length; i++) {

            writer.print("(byte) 0x" +
                    Integer.toHexString(data[i] & 0xff) + ", ");
            if ((i + 1) % 6 == 0) {
                writer.println();
            }
        }
        writer.close();
    
private static voidwriteBinary(java.lang.String arg, byte[] data)


        DataOutputStream writer = null;

        try {
            writer = new DataOutputStream(
                         new FileOutputStream(arg));
        } catch (IOException e) {
            System.out.println("Can't create certificate file.");
            System.exit(1);
        }

        try {
            writer.write(data, 0, data.length);
            writer.close();
        } catch (IOException e) {
            System.out.println("Error writing the certificate file.");
            System.exit(1);
        }
    
private static voidwriteText(java.lang.String arg, byte[] data)


        PrintWriter writer = null;

        try {
            writer = new PrintWriter(new FileOutputStream(arg));
        } catch (IOException e) {
            System.out.println("Can't create certificate file.");
            System.exit(1);
        }

        String s = Base64.encode(data, 0, data.length);

        writer.println("-----BEGIN CERTIFICATE-----");
        int i = 0;
        while (i < s.length()) {

            int j = s.length() - i;
            if (j > 64) {
                j = 64;
            }
            writer.println(s.substring(i, i + j));
            i += j;
        }
        writer.println("-----END CERTIFICATE-----");
        writer.close();