001: /*
002: *
003: *
004: * Copyright 1990-2007 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026:
027: package dummyCA;
028:
029: import java.io.*;
030:
031: public class Main {
032:
033: public static void main(String[] args) throws Exception {
034:
035: if (args.length != 2) {
036: System.out
037: .println("Specify CSR and sertificate file names.");
038: System.exit(1);
039: }
040:
041: byte[] data = parseCSR(args[0]);
042:
043: Authority.init(null);
044: Authority CA = new Authority();
045:
046: if (!CA.createCertificate(data)) {
047: System.out.println(CA.getStatus());
048: System.exit(1);
049: }
050:
051: // writeBinary(args[1], CA.getCertificate());
052: writeText(args[1], CA.getCertificate());
053:
054: System.out.println("Ok.");
055: }
056:
057: private static byte[] parseCSR(String arg) {
058:
059: BufferedReader reader = null;
060:
061: try {
062: reader = new BufferedReader(new InputStreamReader(
063: new FileInputStream(new File(arg))));
064: } catch (FileNotFoundException e) {
065: System.out.println("Source file not found.");
066: System.exit(1);
067: }
068:
069: String s = "";
070:
071: String line = null;
072: try {
073: line = reader.readLine();
074: while (line != null) {
075: if (!line.startsWith("-")) {
076: s += line.trim();
077: }
078: line = reader.readLine();
079: }
080: reader.close();
081: } catch (IOException e) {
082: System.out.println("Error reading the CSR.");
083: System.exit(1);
084: }
085:
086: byte[] data = null;
087: try {
088: data = Base64.decode(s);
089: } catch (IOException e) {
090: System.out.println("Error decoding the CSR.");
091: System.exit(1);
092: }
093:
094: return data;
095: }
096:
097: private static void writeBinary(String arg, byte[] data) {
098:
099: DataOutputStream writer = null;
100:
101: try {
102: writer = new DataOutputStream(new FileOutputStream(arg));
103: } catch (IOException e) {
104: System.out.println("Can't create certificate file.");
105: System.exit(1);
106: }
107:
108: try {
109: writer.write(data, 0, data.length);
110: writer.close();
111: } catch (IOException e) {
112: System.out.println("Error writing the certificate file.");
113: System.exit(1);
114: }
115: }
116:
117: private static void writeText(String arg, byte[] data) {
118:
119: PrintWriter writer = null;
120:
121: try {
122: writer = new PrintWriter(new FileOutputStream(arg));
123: } catch (IOException e) {
124: System.out.println("Can't create certificate file.");
125: System.exit(1);
126: }
127:
128: String s = Base64.encode(data, 0, data.length);
129:
130: writer.println("-----BEGIN CERTIFICATE-----");
131: int i = 0;
132: while (i < s.length()) {
133:
134: int j = s.length() - i;
135: if (j > 64) {
136: j = 64;
137: }
138: writer.println(s.substring(i, i + j));
139: i += j;
140: }
141: writer.println("-----END CERTIFICATE-----");
142: writer.close();
143: }
144:
145: private static void writeArray(String arg, byte[] data) {
146:
147: PrintWriter writer = null;
148:
149: try {
150: writer = new PrintWriter(new FileOutputStream(arg));
151: } catch (IOException e) {
152: System.out.println("Can't create output file.");
153: System.exit(1);
154: }
155:
156: for (int i = 0; i < data.length; i++) {
157:
158: writer.print("(byte) 0x"
159: + Integer.toHexString(data[i] & 0xff) + ", ");
160: if ((i + 1) % 6 == 0) {
161: writer.println();
162: }
163: }
164: writer.close();
165: }
166: }
|