1 package net.sf.bse;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import java.io.FileOutputStream;
27 import java.io.PrintStream;
28 import java.security.Key;
29 import java.security.KeyPair;
30 import java.security.KeyPairGenerator;
31 import java.security.SecureRandom;
32 import java.util.Map;
33
34 import org.bouncycastle.asn1.DERConstructedSequence;
35 import org.bouncycastle.asn1.DERSet;
36 import org.bouncycastle.asn1.DERUTF8String;
37 import org.bouncycastle.asn1.x509.X509Name;
38 import org.bouncycastle.jce.PKCS10CertificationRequest;
39
40 /***
41 * Command to generate a request for an MHP leaf certificate.
42 *
43 * @author Bill Foote (bill.foote@sun.com)
44 * @author Aleksi Peebles (aleksi.peebles@infocast.fi)
45 * @version $Revision: 1.3 $ $Date: 2004/05/06 09:51:15 $
46 */
47 public class GenerateLeafRequest extends Command
48 {
49 public GenerateLeafRequest(Map args)
50 {
51 super(args);
52 }
53
54 public void usageMessage(PrintStream out)
55 {
56 out.println(
57 "Command: request\n\n" +
58
59 " Generates a request for an MHP leaf certificate\n\n" +
60
61 " Arguments:\n\n" +
62
63 " name: Subject commonName of leaf (not including org id)\n" +
64 " country: Subject countryName of leaf\n" +
65 " email: Subject e-mail address of leaf\n" +
66 " strength: Length of key in bits\n" +
67 " file: Where to store the results.\n\n" +
68
69 " Plus, optionally:\n\n" +
70
71 " org: Subject organisation specific text followed by a\n" +
72 " dot and the organisation ID as eight hex digits\n" +
73 " with leading zeroes\n" +
74 " validFrom: Date cert to be valid from, in dd/mm/yyyy format\n" +
75 " validUntil: Date cert to be valid until, in dd/mm/yyyy format\n");
76 }
77
78 public String[] getRequiredArgs()
79 {
80 return new String[] { "name:", "country:", "email:", "strength:",
81 "file:" };
82 }
83
84 public String[] getOptionalArgs()
85 {
86 return new String[] { "org:", "validFrom:", "validUntil:" };
87 }
88
89 public void run() throws Exception
90 {
91 System.out.println("Generating leaf certificate request.");
92
93
94 if (getArg("validFrom:") != null)
95 {
96 getDateArg("validFrom:");
97 }
98 if (getArg("validUntil:") != null)
99 {
100 getDateArg("validUntil:");
101 }
102
103 KeyPairGenerator kpGen = KeyPairGenerator.getInstance("RSA");
104
105
106 int strength = Integer.parseInt(getArg("strength:"));
107
108 kpGen.initialize(strength, new SecureRandom());
109
110 System.out.println("Generating key pair. This may take a few minutes.");
111 KeyPair pair = kpGen.genKeyPair();
112
113 String privateFile = getArg("file:") + "_private.pkcs";
114 String derCsrFile = getArg("file:") + ".der.csr";
115
116
117 Key key = pair.getPrivate();
118 System.out.println("Writing root private key in "
119 + key.getFormat() + " format to "
120 + privateFile + ".");
121 FileOutputStream str = new FileOutputStream(privateFile);
122 str.write(key.getEncoded());
123 str.close();
124
125 System.out.println("Writing request in DER encoded format to " +
126 derCsrFile + ".");
127
128 DERConstructedSequence seq = new DERConstructedSequence();
129 DERConstructedSequence p = new DERConstructedSequence();
130 p.addObject(X509Name.CN);
131 p.addObject(new DERUTF8String(getArg("name:")));
132 seq.addObject(new DERSet(p));
133 p = new DERConstructedSequence();
134 p.addObject(X509Name.C);
135 p.addObject(new DERUTF8String(getArg("country:")));
136 seq.addObject(new DERSet(p));
137 p = new DERConstructedSequence();
138 p.addObject(X509Name.O);
139 p.addObject(new DERUTF8String(getArg("org:")));
140 seq.addObject(new DERSet(p));
141 p = new DERConstructedSequence();
142 p.addObject(X509Name.EmailAddress);
143 p.addObject(new DERUTF8String(getArg("email:")));
144 seq.addObject(new DERSet(p));
145 X509Name subject = new X509Name(seq);
146
147 PKCS10CertificationRequest req =
148 new PKCS10CertificationRequest("MD5WITHRSA", subject,
149 pair.getPublic(), null, pair.getPrivate());
150 FileOutputStream fos = new FileOutputStream(derCsrFile);
151 fos.write(req.getEncoded());
152 fos.close();
153
154 System.out.println("Done!");
155 System.out.println();
156 System.out.println(" Please send " + derCsrFile);
157 System.out.println(" to your certificate authority.");
158 System.out.println(" Keep " + privateFile);
159 System.out.println(" in a safe place.");
160 System.out.println();
161 }
162 }