e840ef56d58714b586ed4a6a4c518926671459d3
[aaf/authz.git] / auth / auth-certman / src / main / java / org / onap / aaf / auth / cm / ca / CA.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 package org.onap.aaf.auth.cm.ca;
22
23 import java.io.File;
24 import java.io.FileInputStream;
25 import java.io.IOException;
26 import java.security.MessageDigest;
27 import java.security.Principal;
28 import java.util.ArrayList;
29 import java.util.Collections;
30 import java.util.HashSet;
31 import java.util.List;
32 import java.util.Set;
33
34 import org.bouncycastle.asn1.x500.style.BCStyle;
35 import org.onap.aaf.auth.cm.cert.CSRMeta;
36 import org.onap.aaf.auth.cm.cert.RDN;
37 import org.onap.aaf.cadi.Access;
38 import org.onap.aaf.cadi.Access.Level;
39 import org.onap.aaf.cadi.config.Config;
40 import org.onap.aaf.cadi.configure.CertException;
41 import org.onap.aaf.misc.env.Trans;
42 import org.onap.aaf.misc.env.util.Split;
43
44 public abstract class CA {
45         private static final String MUST_EXIST_TO_CREATE_CSRS_FOR = " must exist to create CSRs for ";
46         //TODO figuring out what is an Issuing CA is a matter of convention.  Consider SubClassing for Open Source
47         public static final String ISSUING_CA = "Issuing CA";
48         public static final String CM_CA_PREFIX = "cm_ca.";
49         public static final String CM_CA_BASE_SUBJECT = ".baseSubject";
50         protected static final String CM_PUBLIC_DIR = "cm_public_dir";
51         private static final String CM_TRUST_CAS = "cm_trust_cas";
52         protected static final String CM_BACKUP_CAS = "cm_backup_cas";
53
54         public static final Set<String> EMPTY = Collections.unmodifiableSet(new HashSet<>());
55
56         
57         private final String name;
58         private final String env;
59         private MessageDigest messageDigest;
60         private final String permType;
61         private final ArrayList<String> idDomains;
62         private String[] trustedCAs;
63         private String[] caIssuerDNs;
64         private List<RDN> rdns; 
65
66
67         protected CA(Access access, String caName, String env) throws IOException, CertException {
68                 trustedCAs = new String[4]; // starting array
69                 this.name = caName;
70                 this.env = env;
71                 permType = access.getProperty(CM_CA_PREFIX + name + ".perm_type",null);
72                 if(permType==null) {
73                         throw new CertException(CM_CA_PREFIX + name + ".perm_type" + MUST_EXIST_TO_CREATE_CSRS_FOR + caName);
74                 }
75                 caIssuerDNs = Split.splitTrim(':', access.getProperty(Config.CADI_X509_ISSUERS, null));
76                 
77                 String tag = CA.CM_CA_PREFIX+caName+CA.CM_CA_BASE_SUBJECT;
78                 
79                 String fields = access.getProperty(tag, null);
80                 if(fields==null) {
81                         throw new CertException(tag + MUST_EXIST_TO_CREATE_CSRS_FOR + caName);
82                 }
83                 access.log(Level.INFO, tag, "=",fields);
84                 rdns = RDN.parse('/',fields);
85                 for(RDN rdn : rdns) {
86                         if(rdn.aoi==BCStyle.EmailAddress) { // Cert Specs say Emails belong in Subject
87                                 throw new CertException("email address is not allowed in " + CM_CA_BASE_SUBJECT);
88                         }
89                 }
90                 
91                 idDomains = new ArrayList<>();
92                 StringBuilder sb = null;
93                 for(String s : Split.splitTrim(',', access.getProperty(CA.CM_CA_PREFIX+caName+".idDomains", ""))) {
94                         if(s.length()>0) {
95                                 if(sb==null) {
96                                         sb = new StringBuilder();
97                                 } else {
98                                         sb.append(", ");
99                                 }
100                                 idDomains.add(s);
101                                 sb.append(s);
102                         }
103                 }
104                 if(sb!=null) {
105                         access.printf(Level.INIT, "CA '%s' supports Personal Certificates for %s", caName, sb);
106                 }
107                 
108                 String dataDir = access.getProperty(CM_PUBLIC_DIR,null);
109                 if(dataDir!=null) {
110                         File data = new File(dataDir);
111                         byte[] bytes;
112                         if(data.exists()) {
113                                 String trustCas = access.getProperty(CM_TRUST_CAS,null);
114                                 if(trustCas!=null) {
115                                         for(String fname : Split.splitTrim(',', trustCas)) {
116                                                 File crt;
117                                                 if(fname.contains("/")) {
118                                                         crt = new File(fname);
119                                                 } else {
120                                                         crt = new File(data,fname);
121                                                 }
122                                                 if(crt.exists()) {
123                                                         access.printf(Level.INIT, "Loading CA Cert from %s", crt.getAbsolutePath());
124                                                         bytes = new byte[(int)crt.length()];
125                                                         FileInputStream fis = new FileInputStream(crt);
126                                                         try {
127                                                                 int read = fis.read(bytes);
128                                                                 if(read>0) {    
129                                                                         addTrustedCA(new String(bytes));
130                                                                 }
131                                                         } finally {
132                                                                 fis.close();
133                                                         }
134                                                 } else {
135                                                         access.printf(Level.INIT, "FAILED to Load CA Cert from %s", crt.getAbsolutePath());
136                                                 }
137                                         }
138                                 } else {
139                                         access.printf(Level.INIT, "Cannot load external TRUST CAs: No property %s",CM_TRUST_CAS);
140                                 }
141                         } else {
142                                 access.printf(Level.INIT, "Cannot load external TRUST CAs: %s doesn't exist, or is not accessible",data.getAbsolutePath());
143                         }
144                 }
145         }
146
147         protected void addCaIssuerDN(String issuerDN) {
148                 boolean changed = true;
149                 for(String id : caIssuerDNs) {
150                         if(id.equals(issuerDN)) {
151                                 changed = false;
152                                 break;
153                         }
154                 }
155                 if(changed) {
156                         String[] newsa = new String[caIssuerDNs.length+1];
157                         newsa[0]=issuerDN;
158                         System.arraycopy(caIssuerDNs, 0, newsa, 1, caIssuerDNs.length);
159                         caIssuerDNs = newsa;
160                 }
161         }
162         
163         protected synchronized void addTrustedCA(final String crtString) {
164                 String crt;
165                 if(crtString.endsWith("\n")) {
166                         crt = crtString;
167                 } else {
168                         crt = crtString + '\n';
169                 }
170                 for(int i=0;i<trustedCAs.length;++i) {
171                         if(trustedCAs[i]==null) {
172                                 trustedCAs[i]=crt;
173                                 return;
174                         }
175                 }
176                 String[] temp = new String[trustedCAs.length+5];
177                 System.arraycopy(trustedCAs,0,temp, 0, trustedCAs.length);
178                 temp[trustedCAs.length]=crt;
179                 trustedCAs = temp;
180         }
181         
182         public String[] getCaIssuerDNs() {
183                 return caIssuerDNs;
184         }
185         
186         public String[] getTrustedCAs() {
187                 return trustedCAs;
188         }
189         
190         public String getEnv() {
191                 return env;
192         }
193
194         protected void setMessageDigest(MessageDigest md) {
195                 messageDigest = md;
196         }
197
198         /*
199          * End Required Constructor calls
200          */
201
202         public String getName() {
203                 return name;
204         }
205         
206         
207         public String getPermType() {
208                 return permType;
209         }
210         
211         public abstract X509andChain sign(Trans trans, CSRMeta csrmeta) throws IOException, CertException;
212
213         /* (non-Javadoc)
214          * @see org.onap.aaf.auth.cm.ca.CA#inPersonalDomains(java.security.Principal)
215          */
216         public boolean inPersonalDomains(Principal p) {
217                 int at = p.getName().indexOf('@');
218                 if(at>=0) {
219                         return idDomains.contains(p.getName().substring(at+1));
220                 } else {
221                         return false;
222                 }
223         }
224
225         public MessageDigest messageDigest() {
226                 return messageDigest;
227         }
228
229         public CSRMeta newCSRMeta() {
230                 return new CSRMeta(rdns);
231         }
232
233 }