Create parser pool interface and move current pool to an implementation of this inter...
[java-idp.git] / src / edu / internet2 / middleware / shibboleth / utils / HandleRepositorySecretGenerator.java
1 /*
2  * Copyright [2005] [University Corporation for Advanced Internet Development, Inc.]
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package edu.internet2.middleware.shibboleth.utils;
18
19 import java.io.File;
20 import java.io.FileOutputStream;
21 import java.io.IOException;
22 import java.security.GeneralSecurityException;
23 import java.security.KeyStore;
24 import java.security.SecureRandom;
25
26 import javax.crypto.SecretKey;
27 import javax.crypto.SecretKeyFactory;
28 import javax.crypto.spec.DESedeKeySpec;
29
30 import org.apache.tools.ant.BuildException;
31 import org.apache.tools.ant.Task;
32
33 /**
34  * Generates a Triple DES key and sticks it in the default location for use by the <code>CryptoHandleRepository</code>
35  * 
36  * @author Walter Hoehn (wassa@columbia.edu)
37  */
38 public class HandleRepositorySecretGenerator extends Task {
39
40         private File keyStorePath;
41         private String keyStorePassword;
42         private String keyStoreKeyAlias;
43         private String keyStoreKeyPassword;
44
45         public void execute() throws BuildException {
46
47                 try {
48                         if (keyStorePath == null || keyStorePassword == null || keyStoreKeyAlias == null
49                                         || keyStoreKeyPassword == null) { throw new BuildException("Missing required parameter."); }
50                         log("Generating secret.");
51                         SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DESede");
52                         byte[] pseudoRand = new byte[24];
53                         new SecureRandom().nextBytes(pseudoRand);
54                         SecretKey secret = keyFactory.generateSecret(new DESedeKeySpec(pseudoRand));
55
56                         log("Writing keystore.");
57                         KeyStore keyStore = KeyStore.getInstance("JCEKS");
58                         keyStore.load(null, keyStorePassword.toCharArray());
59                         keyStore.setKeyEntry(keyStoreKeyAlias, secret, keyStoreKeyPassword.toCharArray(), null);
60                         keyStore.store(new FileOutputStream(keyStorePath), keyStorePassword.toCharArray());
61
62                 } catch (GeneralSecurityException e) {
63                         throw new BuildException("Unable to generate secret: " + e);
64                 } catch (IOException e) {
65                         throw new BuildException("Unable to store secret in keystore: " + e);
66                 }
67         }
68
69         /**
70          * Sets the keyStoreKeyAlias.
71          * 
72          * @param keyStoreKeyAlias
73          *            The keyStoreKeyAlias to set
74          */
75         public void setKeyStoreKeyAlias(String keyStoreKeyAlias) {
76
77                 this.keyStoreKeyAlias = keyStoreKeyAlias;
78         }
79
80         /**
81          * Sets the keyStoreKeyPassword.
82          * 
83          * @param keyStoreKeyPassword
84          *            The keyStoreKeyPassword to set
85          */
86         public void setKeyStoreKeyPassword(String keyStoreKeyPassword) {
87
88                 this.keyStoreKeyPassword = keyStoreKeyPassword;
89         }
90
91         /**
92          * Sets the keyStorePassword.
93          * 
94          * @param keyStorePassword
95          *            The keyStorePassword to set
96          */
97         public void setKeyStorePassword(String keyStorePassword) {
98
99                 this.keyStorePassword = keyStorePassword;
100         }
101
102         /**
103          * Sets the keyStorePath.
104          * 
105          * @param keyStorePath
106          *            The keyStorePath to set
107          */
108         public void setKeyStorePath(File keyStorePath) {
109
110                 this.keyStorePath = keyStorePath;
111         }
112
113 }