22
33import java .io .File ;
44import java .io .IOException ;
5- import java .io .StringWriter ;
6- import java .math .BigInteger ;
7- import java .security .KeyPair ;
8- import java .security .KeyPairGenerator ;
9- import java .security .NoSuchAlgorithmException ;
10- import java .security .Provider ;
11- import java .security .SecureRandom ;
12- import java .security .Security ;
135import java .security .cert .CertificateEncodingException ;
14- import java .security .cert .CertificateException ;
156import java .security .cert .X509Certificate ;
16- import java .util .Calendar ;
17- import java .util .Date ;
18- import javax .security .auth .x500 .X500Principal ;
197//import org.bouncycastle.asn1.ASN1ObjectIdentifier;
208//import org.bouncycastle.asn1.x500.X500Name;
219//import org.bouncycastle.asn1.x509.BasicConstraints;
5038 * as accepts self signed (anonymous) certs from the broker.
5139 *
5240 * @author Aaron Hansen
41+ * @author Daniel Shapiro
5342 */
5443public class SysCertManager extends DSNode {
5544
@@ -61,11 +50,14 @@ public class SysCertManager extends DSNode {
6150 private static final String CERTFILE = "Cert_File" ;
6251 private static final String CERTFILE_PASS = "Cert_File_Pass" ;
6352 private static final String CERTFILE_TYPE = "Cert_File_Type" ;
64- private static final String LOCAL_TRUSTSTORE = "Local_Truststore " ;
53+ private static final String LOCAL_TRUSTSTORE = "Local Truststore " ;
6554 private static final String QUARANTINE = "Quarantine" ;
66- private static final String GENERATE_CSR = "Generate_Certificate_Signing_Request " ;
55+ private static final String GENERATE_CSR = "Generate Certificate Signing Request " ;
6756 private static final String IMPORT_CA_CERT = "Import CA Certificate" ;
6857 private static final String IMPORT_PRIMARY_CERT = "Import Primary Certificate" ;
58+ private static final String GENERATE_SELF_SIGNED = "Generate Self-Signed Certificate" ;
59+ private static final String DELETE_KS_ENTRY = "Delete Keystore Entry" ;
60+ private static final String GET_KS_ENTRY = "Get Keystore Entry" ;
6961
7062 // Fields
7163 // ------
@@ -121,6 +113,9 @@ public void declareDefaults() {
121113 declareDefault (GENERATE_CSR , getGenerateCSRAction ());
122114 declareDefault (IMPORT_CA_CERT , getImportCACertAction ());
123115 declareDefault (IMPORT_PRIMARY_CERT , getImportPrimaryCertAction ());
116+ declareDefault (GENERATE_SELF_SIGNED , getGenerateSelfSignedAction ());
117+ declareDefault (GET_KS_ENTRY , getGetKSEntryAction ());
118+ declareDefault (DELETE_KS_ENTRY , getDeleteKSEntryAction ());
124119 }
125120
126121 private DSAbstractAction getGenerateCSRAction () {
@@ -132,21 +127,24 @@ public void prepareParameter(DSInfo info, DSMap parameter) {
132127
133128 @ Override
134129 public ActionResult invoke (DSInfo info , ActionInvocation invocation ) {
135- String csr ;
136- try {
137- csr = KeyToolUtil .generateCSR (keystore .getElement ().toString ());
138- } catch (IOException e ) {
139- DSException .throwRuntime (e );
140- return null ;
141- }
142- return new DSActionValues (info .getAction ()).addResult (DSString .valueOf (csr ));
130+ String csr = ((SysCertManager ) info .getParent ()).generateCSR ();
131+ return csr != null ? new DSActionValues (info .getAction ()).addResult (DSString .valueOf (csr )) : null ;
143132 }
144133 };
145134 act .setResultType (ResultType .VALUES );
146- act .addValueResult ("CSR" , DSValueType .STRING );
135+ act .addValueResult ("CSR" , DSValueType .STRING ). setEditor ( "textarea" ) ;
147136 return act ;
148137 }
149138
139+ private String generateCSR () {
140+ try {
141+ return KeyToolUtil .generateCSR (getKeystorePath (), getCertFilePass ());
142+ } catch (IOException e ) {
143+ DSException .throwRuntime (e );
144+ return null ;
145+ }
146+ }
147+
150148 private DSAbstractAction getImportCACertAction () {
151149 DSAbstractAction act = new DSAbstractAction () {
152150
@@ -157,13 +155,7 @@ public void prepareParameter(DSInfo info, DSMap parameter) {
157155 @ Override
158156 public ActionResult invoke (DSInfo info , ActionInvocation invocation ) {
159157 DSMap parameters = invocation .getParameters ();
160- String alias = parameters .getString ("Alias" );
161- String certStr = parameters .getString ("Certificate" );
162- try {
163- KeyToolUtil .importCACert (keystore .getElement ().toString (), certStr , alias );
164- } catch (IOException e ) {
165- DSException .throwRuntime (e );
166- }
158+ ((SysCertManager ) info .getParent ()).importCACert (parameters );
167159 return null ;
168160 }
169161 };
@@ -172,6 +164,16 @@ public ActionResult invoke(DSInfo info, ActionInvocation invocation) {
172164 return act ;
173165 }
174166
167+ private void importCACert (DSMap parameters ) {
168+ String alias = parameters .getString ("Alias" );
169+ String certStr = parameters .getString ("Certificate" );
170+ try {
171+ KeyToolUtil .importCACert (getKeystorePath (), certStr , alias , getCertFilePass ());
172+ } catch (IOException e ) {
173+ DSException .throwRuntime (e );
174+ }
175+ }
176+
175177 private DSAbstractAction getImportPrimaryCertAction () {
176178 DSAbstractAction act = new DSAbstractAction () {
177179
@@ -182,29 +184,95 @@ public void prepareParameter(DSInfo info, DSMap parameter) {
182184 @ Override
183185 public ActionResult invoke (DSInfo info , ActionInvocation invocation ) {
184186 DSMap parameters = invocation .getParameters ();
185- String certStr = parameters .getString ("Certificate" );
186- try {
187- KeyToolUtil .importPrimaryCert (keystore .getElement ().toString (), certStr );
188- } catch (IOException e ) {
189- DSException .throwRuntime (e );
190- }
187+ ((SysCertManager ) info .getParent ()).importPrimaryCert (parameters );
191188 return null ;
192189 }
193190 };
194191 act .addParameter ("Certificate" , DSValueType .STRING , null ).setEditor ("textarea" );
195192 return act ;
196193 }
194+
195+ private void importPrimaryCert (DSMap parameters ) {
196+ String certStr = parameters .getString ("Certificate" );
197+ try {
198+ KeyToolUtil .importPrimaryCert (getKeystorePath (), certStr , getCertFilePass ());
199+ } catch (IOException e ) {
200+ DSException .throwRuntime (e );
201+ }
202+ }
203+
204+ private DSAbstractAction getGenerateSelfSignedAction () {
205+ DSAbstractAction act = new DSAbstractAction () {
206+
207+ @ Override
208+ public void prepareParameter (DSInfo info , DSMap parameter ) {
209+ }
210+
211+ @ Override
212+ public ActionResult invoke (DSInfo info , ActionInvocation invocation ) {
213+ ((SysCertManager ) info .getParent ()).keytoolGenkey ();
214+ return null ;
215+ }
216+ };
217+ return act ;
218+ }
219+
220+ private DSAbstractAction getGetKSEntryAction () {
221+ DSAbstractAction act = new DSAbstractAction () {
222+
223+ @ Override
224+ public void prepareParameter (DSInfo info , DSMap parameter ) {
225+ }
226+
227+ @ Override
228+ public ActionResult invoke (DSInfo info , ActionInvocation invocation ) {
229+ String result = ((SysCertManager ) info .getParent ()).getKSEntry ();
230+ return new DSActionValues (info .getAction ()).addResult (DSString .valueOf (result ));
231+ }
232+ };
233+ act .setResultType (ResultType .VALUES );
234+ act .addValueResult ("Entry" , DSValueType .STRING ).setEditor ("textarea" );
235+ return act ;
236+ }
237+
238+ private String getKSEntry () {
239+ return KeyToolUtil .getEntry (getKeystorePath (), getCertFilePass ());
240+ }
241+
242+ private DSAbstractAction getDeleteKSEntryAction () {
243+ DSAbstractAction act = new DSAbstractAction () {
244+
245+ @ Override
246+ public void prepareParameter (DSInfo info , DSMap parameter ) {
247+ }
248+
249+ @ Override
250+ public ActionResult invoke (DSInfo info , ActionInvocation invocation ) {
251+ ((SysCertManager ) info .getParent ()).deleteKSEntry ();
252+ return null ;
253+ }
254+ };
255+ return act ;
256+ }
257+
258+ private void deleteKSEntry () {
259+ KeyToolUtil .deleteEntry (getKeystorePath (), getCertFilePass ());
260+ }
197261
198262 private String getCertFilePass () {
199263 DSPasswordAes128 pass = (DSPasswordAes128 ) keystorePass .getObject ();
200264 return pass .decode ();
201265 }
266+
267+ private String getKeystorePath () {
268+ return keystore .getElement ().toString ();
269+ }
202270
203271 /**
204272 * Executes the java keytool to generate a new self signed cert.
205273 */
206274 private void keytoolGenkey () {
207- KeyToolUtil .generateSelfSigned (keystore . getElement (). toString (), getCertFilePass ());
275+ KeyToolUtil .generateSelfSigned (getKeystorePath (), getCertFilePass ());
208276 }
209277
210278 @ Override
0 commit comments