[AAF-21] Updated Copyright Headers for AAF
[aaf/cadi.git] / aaf / src / 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",      \r
46                         "CN=ATT AAF CADI CA, OU=CSO, O=ATT, C=US"\r
47         };\r
48 \r
49         public PlaceArtifactInKeystore(String kst) {\r
50                 this.kst = kst;\r
51         }\r
52 \r
53         @Override\r
54         public boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {\r
55                 File fks = new File(dir,arti.getAppName()+'.'+kst);\r
56                 try {\r
57                         KeyStore jks = KeyStore.getInstance(kst);\r
58                         if(fks.exists()) {\r
59                                 fks.delete();\r
60                         }       \r
61 \r
62                         // Get the Cert(s)... Might include Trust store\r
63                         Collection<? extends Certificate> certColl = Factory.toX509Certificate(trans, certInfo.getCerts());\r
64                         Certificate[] certs = new Certificate[certColl.size()];\r
65                         certColl.toArray(certs);\r
66                         \r
67                         boolean first = true;\r
68                         StringBuilder issuers = new StringBuilder();\r
69                         for(Certificate c : certs) {\r
70                                 if(c instanceof X509Certificate) {\r
71                                         X509Certificate xc = (X509Certificate)c;\r
72                                         String issuer = xc.getIssuerDN().toString();\r
73                                         for(String root : rootDNs) {\r
74                                                 if(root.equals(issuer)) {\r
75                                                         if(first) {\r
76                                                                 first=false;\r
77                                                         } else {\r
78                                                                 issuers.append(":");\r
79                                                         }\r
80                                                         if(xc.getSubjectDN().toString().contains("Issuing CA")) {\r
81                                                                 issuers.append(xc.getSubjectDN());\r
82                                                         }\r
83                                                 }\r
84                                         }\r
85                                 }\r
86                         }\r
87                         addProperty(Config.CADI_X509_ISSUERS,issuers.toString());\r
88 \r
89                         // Add CADI Keyfile Entry to Properties\r
90                         addProperty(Config.CADI_KEYFILE,arti.getDir()+'/'+arti.getAppName() + ".keyfile");\r
91                         // Set Keystore Password\r
92                         addProperty(Config.CADI_KEYSTORE,fks.getCanonicalPath());\r
93                         String keystorePass = Symm.randomGen(CmAgent.PASS_SIZE);\r
94                         addEncProperty(Config.CADI_KEYSTORE_PASSWORD,keystorePass);\r
95                         char[] keystorePassArray = keystorePass.toCharArray();\r
96                         jks.load(null,keystorePassArray); // load in\r
97                         \r
98                         // Add Private Key/Cert Entry for App\r
99                         // Note: Java SSL security classes, while having a separate key from keystore,\r
100                         // is documented to not actually work. \r
101                         // java.security.UnrecoverableKeyException: Cannot recover key\r
102                         // You can create a custom Key Manager to make it work, but Practicality  \r
103                         // dictates that you live with the default, meaning, they are the same\r
104                         String keyPass = keystorePass; //Symm.randomGen(CmAgent.PASS_SIZE);\r
105                         PrivateKey pk = Factory.toPrivateKey(trans, certInfo.getPrivatekey());\r
106                         addEncProperty(Config.CADI_KEY_PASSWORD, keyPass);\r
107                         addProperty(Config.CADI_ALIAS, arti.getMechid());\r
108 //                      Set<Attribute> attribs = new HashSet<Attribute>();\r
109 //                      if(kst.equals("pkcs12")) {\r
110 //                              // Friendly Name\r
111 //                              attribs.add(new PKCS12Attribute("1.2.840.113549.1.9.20", arti.getAppName()));\r
112 //                      } \r
113 //                      \r
114                         KeyStore.ProtectionParameter protParam = \r
115                                         new KeyStore.PasswordProtection(keyPass.toCharArray());\r
116                         \r
117                         KeyStore.PrivateKeyEntry pkEntry = \r
118                                 new KeyStore.PrivateKeyEntry(pk, new Certificate[] {certs[0]});\r
119                         jks.setEntry(arti.getMechid(), \r
120                                         pkEntry, protParam);\r
121                 \r
122                         // Write out\r
123                         write(fks,Chmod.to400,jks,keystorePassArray);\r
124                         \r
125                         // Change out to TrustStore\r
126                         fks = new File(dir,arti.getAppName()+".trust."+kst);\r
127                         jks = KeyStore.getInstance(kst);\r
128                         \r
129                         // Set Truststore Password\r
130                         addProperty(Config.CADI_TRUSTSTORE,fks.getCanonicalPath());\r
131                         String trustStorePass = Symm.randomGen(CmAgent.PASS_SIZE);\r
132                         addEncProperty(Config.CADI_TRUSTSTORE_PASSWORD,trustStorePass);\r
133                         char[] truststorePassArray = trustStorePass.toCharArray();\r
134                         jks.load(null,truststorePassArray); // load in\r
135                         \r
136                         // Add Trusted Certificates\r
137                         for(int i=1; i<certs.length;++i) {\r
138                                 jks.setCertificateEntry("cadi_" + arti.getCa() + '_' + i, certs[i]);\r
139                         }\r
140                         // Write out\r
141                         write(fks,Chmod.to400,jks,truststorePassArray);\r
142 \r
143                 } catch (Exception e) {\r
144                         throw new CadiException(e);\r
145                 }\r
146                 return false;\r
147         }\r
148 \r
149 }\r