Update AAF Version 1.0.0
[aaf/cadi.git] / aaf / src / main / java / com / att / cadi / cm / PlaceArtifactInKeystore.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.cm;\r
24 \r
25 import java.io.File;\r
26 import java.security.KeyStore;\r
27 import java.security.PrivateKey;\r
28 import java.security.cert.Certificate;\r
29 import java.security.cert.X509Certificate;\r
30 import java.util.Collection;\r
31 \r
32 import com.att.cadi.CadiException;\r
33 import com.att.cadi.Symm;\r
34 import com.att.cadi.config.Config;\r
35 import com.att.cadi.util.Chmod;\r
36 import com.att.inno.env.Trans;\r
37 \r
38 import certman.v1_0.Artifacts.Artifact;\r
39 import certman.v1_0.CertInfo;\r
40 \r
41 public class PlaceArtifactInKeystore extends ArtifactDir {\r
42         private String kst;\r
43         //TODO get ROOT DNs or Trusted DNs from Certificate Manager.\r
44 //      private static String[] rootDNs = new String[]{                 \r
45 //                      "CN=ATT CADI Root CA - Test, O=ATT, OU=CSO, C=US", // Lab.  delete eventually\r
46 //                      "CN=ATT AAF CADI TEST CA, OU=CSO, O=ATT, C=US",\r
47 //                      "CN=ATT AAF CADI CA, OU=CSO, O=ATT, C=US"\r
48 //      };\r
49 \r
50         public PlaceArtifactInKeystore(String kst) {\r
51                 this.kst = kst;\r
52         }\r
53 \r
54         @Override\r
55         public boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {\r
56                 File fks = new File(dir,arti.getAppName()+'.'+kst);\r
57                 try {\r
58                         KeyStore jks = KeyStore.getInstance(kst);\r
59                         if(fks.exists()) {\r
60                                 fks.delete();\r
61                         }       \r
62 \r
63                         // Get the Cert(s)... Might include Trust store\r
64                         Collection<? extends Certificate> certColl = Factory.toX509Certificate(certInfo.getCerts());\r
65                         X509Certificate[] certs = new X509Certificate[certColl.size()];\r
66                         certColl.toArray(certs);\r
67                         \r
68 \r
69                         // Add CADI Keyfile Entry to Properties\r
70                         addProperty(Config.CADI_KEYFILE,arti.getDir()+'/'+arti.getAppName() + ".keyfile");\r
71                         // Set Keystore Password\r
72                         addProperty(Config.CADI_KEYSTORE,fks.getAbsolutePath());\r
73                         String keystorePass = Symm.randomGen(CmAgent.PASS_SIZE);\r
74                         addEncProperty(Config.CADI_KEYSTORE_PASSWORD,keystorePass);\r
75                         char[] keystorePassArray = keystorePass.toCharArray();\r
76                         jks.load(null,keystorePassArray); // load in\r
77                         \r
78                         // Add Private Key/Cert Entry for App\r
79                         // Note: Java SSL security classes, while having a separate key from keystore,\r
80                         // is documented to not actually work. \r
81                         // java.security.UnrecoverableKeyException: Cannot recover key\r
82                         // You can create a custom Key Manager to make it work, but Practicality  \r
83                         // dictates that you live with the default, meaning, they are the same\r
84                         String keyPass = keystorePass; //Symm.randomGen(CmAgent.PASS_SIZE);\r
85                         PrivateKey pk = Factory.toPrivateKey(trans, certInfo.getPrivatekey());\r
86                         addEncProperty(Config.CADI_KEY_PASSWORD, keyPass);\r
87                         addProperty(Config.CADI_ALIAS, arti.getMechid());\r
88 //                      Set<Attribute> attribs = new HashSet<Attribute>();\r
89 //                      if(kst.equals("pkcs12")) {\r
90 //                              // Friendly Name\r
91 //                              attribs.add(new PKCS12Attribute("1.2.840.113549.1.9.20", arti.getAppName()));\r
92 //                      } \r
93 //                      \r
94                         KeyStore.ProtectionParameter protParam = \r
95                                         new KeyStore.PasswordProtection(keyPass.toCharArray());\r
96                         \r
97                         KeyStore.PrivateKeyEntry pkEntry = \r
98                                 new KeyStore.PrivateKeyEntry(pk, new Certificate[] {certs[0]});\r
99                         jks.setEntry(arti.getMechid(), \r
100                                         pkEntry, protParam);\r
101                 \r
102                         // Write out\r
103                         write(fks,Chmod.to400,jks,keystorePassArray);\r
104                         \r
105                         // Change out to TrustStore\r
106                         fks = new File(dir,arti.getAppName()+".trust."+kst);\r
107                         jks = KeyStore.getInstance(kst);\r
108                         \r
109                         // Set Truststore Password\r
110                         addProperty(Config.CADI_TRUSTSTORE,fks.getAbsolutePath());\r
111                         String trustStorePass = Symm.randomGen(CmAgent.PASS_SIZE);\r
112                         addEncProperty(Config.CADI_TRUSTSTORE_PASSWORD,trustStorePass);\r
113                         char[] truststorePassArray = trustStorePass.toCharArray();\r
114                         jks.load(null,truststorePassArray); // load in\r
115                         \r
116                         // Add Trusted Certificates\r
117                         for(int i=1; i<certs.length;++i) {\r
118                                 jks.setCertificateEntry("cadi_root_" + arti.getCa() + '_' + i, certs[i]);\r
119                         }\r
120                         // Write out\r
121                         write(fks,Chmod.to644,jks,truststorePassArray);\r
122 \r
123                 } catch (Exception e) {\r
124                         throw new CadiException(e);\r
125                 }\r
126                 return false;\r
127         }\r
128 \r
129 }\r