2 * ============LICENSE_START====================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
21 package org.onap.aaf.auth.cm.ca;
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;
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.cm.CertException;
40 import org.onap.aaf.misc.env.Trans;
41 import org.onap.aaf.misc.env.util.Split;
43 public abstract class CA {
44 private static final String MUST_EXIST_TO_CREATE_CSRS_FOR = " must exist to create CSRs for ";
45 //TODO figuring out what is an Issuing CA is a matter of convention. Consider SubClassing for Open Source
46 public static final String ISSUING_CA = "Issuing CA";
47 public static final String CM_CA_PREFIX = "cm_ca.";
48 public static final String CM_CA_BASE_SUBJECT = ".baseSubject";
49 protected static final String CM_PUBLIC_DIR = "cm_public_dir";
50 private static final String CM_TRUST_CAS = "cm_trust_cas";
51 protected static final String CM_BACKUP_CAS = "cm_backup_cas";
53 public static final Set<String> EMPTY = Collections.unmodifiableSet(new HashSet<String>());
56 private final String name;
57 private final String env;
58 private MessageDigest messageDigest;
59 private final String permType;
60 private Set<String> caIssuerDNs;
61 private final ArrayList<String> idDomains;
62 private String[] trustedCAs;
63 private List<RDN> rdns;
66 protected CA(Access access, String caName, String env) throws IOException, CertException {
67 trustedCAs = new String[4]; // starting array
70 permType = access.getProperty(CM_CA_PREFIX + name + ".perm_type",null);
72 throw new CertException(CM_CA_PREFIX + name + ".perm_type" + MUST_EXIST_TO_CREATE_CSRS_FOR + caName);
74 caIssuerDNs = new HashSet<>();
76 String tag = CA.CM_CA_PREFIX+caName+CA.CM_CA_BASE_SUBJECT;
78 String fields = access.getProperty(tag, null);
80 throw new CertException(tag + MUST_EXIST_TO_CREATE_CSRS_FOR + caName);
82 access.log(Level.INFO, tag, "=",fields);
83 rdns = RDN.parse('/',fields);
85 if(rdn.aoi==BCStyle.EmailAddress) { // Cert Specs say Emails belong in Subject
86 throw new CertException("email address is not allowed in " + CM_CA_BASE_SUBJECT);
90 idDomains = new ArrayList<>();
91 StringBuilder sb = null;
92 for(String s : Split.splitTrim(',', access.getProperty(CA.CM_CA_PREFIX+caName+".idDomains", ""))) {
95 sb = new StringBuilder();
104 access.printf(Level.INIT, "CA '%s' supports Personal Certificates for %s", caName, sb);
107 String dataDir = access.getProperty(CM_PUBLIC_DIR,null);
109 File data = new File(dataDir);
112 String trustCas = access.getProperty(CM_TRUST_CAS,null);
114 for(String fname : Split.splitTrim(',', trustCas)) {
115 File crt = new File(data,fname);
117 access.printf(Level.INIT, "Loading CA Cert from %s", crt.getAbsolutePath());
118 bytes = new byte[(int)crt.length()];
119 FileInputStream fis = new FileInputStream(crt);
121 int read = fis.read(bytes);
123 addTrustedCA(new String(bytes));
129 access.printf(Level.INIT, "FAILED to Load CA Cert from %s", crt.getAbsolutePath());
133 access.printf(Level.INIT, "Cannot load external TRUST CAs: No property %s",CM_TRUST_CAS);
136 access.printf(Level.INIT, "Cannot load external TRUST CAs: %s doesn't exist, or is not accessible",data.getAbsolutePath());
141 protected void addCaIssuerDN(String issuerDN) {
142 caIssuerDNs.add(issuerDN);
145 protected synchronized void addTrustedCA(final String crtString) {
147 if(crtString.endsWith("\n")) {
150 crt = crtString + '\n';
152 for(int i=0;i<trustedCAs.length;++i) {
153 if(trustedCAs[i]==null) {
158 String[] temp = new String[trustedCAs.length+5];
159 System.arraycopy(trustedCAs,0,temp, 0, trustedCAs.length);
160 temp[trustedCAs.length]=crt;
164 public Set<String> getCaIssuerDNs() {
168 public String[] getTrustedCAs() {
172 public String getEnv() {
176 protected void setMessageDigest(MessageDigest md) {
181 * End Required Constructor calls
184 public String getName() {
189 public String getPermType() {
193 public abstract X509andChain sign(Trans trans, CSRMeta csrmeta) throws IOException, CertException;
196 * @see org.onap.aaf.auth.cm.ca.CA#inPersonalDomains(java.security.Principal)
198 public boolean inPersonalDomains(Principal p) {
199 int at = p.getName().indexOf('@');
201 return idDomains.contains(p.getName().substring(at+1));
207 public MessageDigest messageDigest() {
208 return messageDigest;
211 public CSRMeta newCSRMeta() {
212 return new CSRMeta(rdns);