Configuration and Auto-Certificates
[aaf/authz.git] / cadi / aaf / src / main / java / org / onap / aaf / cadi / configure / 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.configure;
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.Collections;
32 import java.util.HashSet;
33 import java.util.List;
34 import java.util.Set;
35
36 import org.onap.aaf.cadi.CadiException;
37 import org.onap.aaf.cadi.Symm;
38 import org.onap.aaf.cadi.config.Config;
39 import org.onap.aaf.cadi.util.Chmod;
40 import org.onap.aaf.misc.env.Trans;
41
42 import certman.v1_0.Artifacts.Artifact;
43 import certman.v1_0.CertInfo;
44
45 public class PlaceArtifactInKeystore extends ArtifactDir {
46         private String kst;
47
48         public PlaceArtifactInKeystore(String kst) {
49                 this.kst = kst;
50         }
51
52         @Override
53         public boolean _place(Trans trans, CertInfo certInfo, Artifact arti) throws CadiException {
54                 File fks = new File(dir,arti.getNs()+'.'+(kst=="pkcs12"?"p12":kst));
55                 try {
56                         KeyStore jks = KeyStore.getInstance(kst);
57                         if(fks.exists()) {
58                                 File backup = File.createTempFile(fks.getName()+'.', ".backup",dir);
59                                 fks.renameTo(backup);
60                         }       
61
62                         // Get the Cert(s)... Might include Trust store
63                         Collection<? extends Certificate> certColl = Factory.toX509Certificate(certInfo.getCerts());
64                         // find where the trusts end in 1.0 API
65                 
66                         X509Certificate x509;
67                         List<X509Certificate> chainList = new ArrayList<>();
68                         Set<X509Certificate> caSet = new HashSet<>();
69                         for(Certificate c : certColl) {
70                                 x509 = (X509Certificate)c;
71                                 // Is a Root (self-signed, anyway)
72                                 if(x509.getSubjectDN().equals(x509.getIssuerDN())) {
73                                         caSet.add(x509);
74                                 } else {
75                                         chainList.add(x509);
76                                 }
77                         }
78 //                      chainList.addAll(caSet);
79                         //Collections.reverse(chainList);
80
81                         // Properties, etc
82                         // Add CADI Keyfile Entry to Properties
83                         addProperty(Config.CADI_KEYFILE,arti.getDir()+'/'+arti.getNs() + ".keyfile");
84                         // Set Keystore Password
85                         addProperty(Config.CADI_KEYSTORE,fks.getAbsolutePath());
86                         String keystorePass = Symm.randomGen(Agent.PASS_SIZE);
87                         addEncProperty(Config.CADI_KEYSTORE_PASSWORD,keystorePass);
88                         char[] keystorePassArray = keystorePass.toCharArray();
89                         jks.load(null,keystorePassArray); // load in
90                         
91                         // Add Private Key/Cert Entry for App
92                         // Note: Java SSL security classes, while having a separate key from keystore,
93                         // is documented to not actually work. 
94                         // java.security.UnrecoverableKeyException: Cannot recover key
95                         // You can create a custom Key Manager to make it work, but Practicality  
96                         // dictates that you live with the default, meaning, they are the same
97                         String keyPass = keystorePass; //Symm.randomGen(CmAgent.PASS_SIZE);
98                         PrivateKey pk = Factory.toPrivateKey(trans, certInfo.getPrivatekey());
99                         addEncProperty(Config.CADI_KEY_PASSWORD, keyPass);
100                         addProperty(Config.CADI_ALIAS, arti.getMechid());
101 //                      Set<Attribute> attribs = new HashSet<>();
102 //                      if(kst.equals("pkcs12")) {
103 //                              // Friendly Name
104 //                              attribs.add(new PKCS12Attribute("1.2.840.113549.1.9.20", arti.getNs()));
105 //                      } 
106 //                      
107                         KeyStore.ProtectionParameter protParam = 
108                                         new KeyStore.PasswordProtection(keyPass.toCharArray());
109                         
110                         Certificate[] trustChain = new Certificate[chainList.size()];
111                         chainList.toArray(trustChain);
112                         KeyStore.PrivateKeyEntry pkEntry = 
113                                 new KeyStore.PrivateKeyEntry(pk, trustChain);
114                         jks.setEntry(arti.getMechid(), 
115                                         pkEntry, protParam);
116
117                         // Write out
118                         write(fks,Chmod.to400,jks,keystorePassArray);
119                         
120                         // Change out to TrustStore
121                         // NOTE: PKCS12 does NOT support Trusted Entries.  Put in JKS Always
122                         fks = new File(dir,arti.getNs()+".trust.jks");
123                         if(fks.exists()) {
124                                 File backup = File.createTempFile(fks.getName()+'.', ".backup",dir);
125                                 fks.renameTo(backup);
126                         }       
127
128                         jks = KeyStore.getInstance("jks");
129                         
130                         // Set Truststore Password
131                         addProperty(Config.CADI_TRUSTSTORE,fks.getAbsolutePath());
132                         String trustStorePass = Symm.randomGen(Agent.PASS_SIZE);
133                         addEncProperty(Config.CADI_TRUSTSTORE_PASSWORD,trustStorePass);
134                         char[] truststorePassArray = trustStorePass.toCharArray();
135                         jks.load(null,truststorePassArray); // load in
136                         
137                         // Add Trusted Certificates, but PKCS12 doesn't support
138                         Certificate[] trustCAs = new Certificate[caSet.size()];
139                         caSet.toArray(trustCAs);
140                         for(int i=0; i<trustCAs.length;++i) {
141                                 jks.setCertificateEntry("ca_" + arti.getCa() + '_' + i, trustCAs[i]);
142                         }
143                         // Write out
144                         write(fks,Chmod.to644,jks,truststorePassArray);
145                         return true;
146                 } catch (Exception e) {
147                         throw new CadiException(e);
148                 }
149         }
150
151 }