This repository has been archived by the owner on Sep 23, 2020. It is now read-only.
/
GenerateNewCA.java
81 lines (65 loc) · 2.71 KB
/
GenerateNewCA.java
1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
* Copyright 1999-2008 University of Chicago
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy
* of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package org.nimbustools.auto_common.ezpz_ca;
import java.security.KeyPair;
import java.security.cert.X509Certificate;
import java.io.File;
public class GenerateNewCA {
// five year CA cert
public static final int VALIDITY_MONTHS = 60;
// -------------------------------------------------------------------------
// REPLACE
// -------------------------------------------------------------------------
public void generateCA(String basename,
String caDirPath) throws Exception {
if (basename == null) {
throw new IllegalArgumentException("basename may not be null");
}
if (caDirPath == null) {
throw new IllegalArgumentException("caDirPath may not be null");
}
final CAFactory caFactory = new CAFactory();
final KeyPair keyPair = caFactory.createNewKeyPair();
final X509Certificate caCert =
caFactory.create(basename, VALIDITY_MONTHS, keyPair);
if (caCert == null) {
throw new Exception(
"No certificate authority certificate was created?");
}
caCert.checkValidity();
final String pubpath = caDirPath + File.separator + basename + ".pem";
final String privpath =
caDirPath + File.separator + "private-key-" + basename + ".pem";
new CertWriter().writeCert(caCert, keyPair, pubpath, privpath);
}
// -------------------------------------------------------------------------
// MAIN
// -------------------------------------------------------------------------
public static void main(String[] args) {
if (args == null || args.length != 2) {
System.err.println("Needs these arguments:\n" +
"1 - the ca directory path (created already)\n" +
"2 - the base name of ca");
System.exit(1);
}
try {
new GenerateNewCA().generateCA(args[1], args[0]);
} catch (Exception e) {
System.err.println("Problem creating CA: " + e.getMessage());
System.exit(1);
}
}
}