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