Update Certificate for all FQDNs
[aaf/authz.git] / auth / auth-certman / src / main / java / org / onap / aaf / auth / cm / cert / BCFactory.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.cert;
22
23 import java.io.File;
24 import java.io.FileReader;
25 import java.io.IOException;
26 import java.security.InvalidKeyException;
27 import java.security.NoSuchAlgorithmException;
28 import java.security.PrivateKey;
29 import java.security.SignatureException;
30 import java.util.List;
31
32 import org.bouncycastle.asn1.ASN1Object;
33 import org.bouncycastle.operator.ContentSigner;
34 import org.bouncycastle.operator.OperatorCreationException;
35 import org.bouncycastle.operator.jcajce.JcaContentSignerBuilder;
36 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
37 import org.onap.aaf.auth.cm.ca.CA;
38 import org.onap.aaf.auth.cm.validation.CertmanValidator;
39 import org.onap.aaf.cadi.Symm;
40 import org.onap.aaf.cadi.cm.CertException;
41 import org.onap.aaf.cadi.cm.Factory;
42 import org.onap.aaf.misc.env.Env;
43 import org.onap.aaf.misc.env.TimeTaken;
44 import org.onap.aaf.misc.env.Trans;
45
46
47 /**
48  * Additional Factory mechanisms for CSRs, and BouncyCastle.  The main Factory
49  * utilizes only Java abstractions, and is useful in Client code.
50  * 
51  * @author JonathanGathman
52  *
53  */
54 public class BCFactory extends Factory {
55         private static final JcaContentSignerBuilder jcsb;
56
57
58         static {
59                 // Bouncy
60                 jcsb = new JcaContentSignerBuilder(Factory.SIG_ALGO);
61         }
62         
63         public static ContentSigner contentSigner(PrivateKey pk) throws OperatorCreationException {
64                 return jcsb.build(pk);
65         }
66         
67         public static String toString(PKCS10CertificationRequest csr) throws IOException, CertException {
68                 if(csr==null) {
69                         throw new CertException("x509 Certificate Request not built");
70                 }
71                 return textBuilder("CERTIFICATE REQUEST",csr.getEncoded());
72         }
73
74         public static PKCS10CertificationRequest toCSR(Trans trans, File file) throws IOException {
75                 TimeTaken tt = trans.start("Reconstitute CSR", Env.SUB);
76                 try {
77                         FileReader fr = new FileReader(file);
78                         return new PKCS10CertificationRequest(decode(strip(fr)));
79                 } finally {
80                         tt.done();
81                 }
82         }
83
84         public static byte[] sign(Trans trans, ASN1Object toSign, PrivateKey pk) throws IOException, InvalidKeyException, SignatureException, NoSuchAlgorithmException {
85                 TimeTaken tt = trans.start("Encode Security Object", Env.SUB);
86                 try {
87                         return sign(trans,toSign.getEncoded(),pk);
88                 } finally {
89                         tt.done();
90                 }
91         }
92         
93         public static CSRMeta createCSRMeta(CA ca, String mechid, String sponsorEmail, List<String> fqdns) throws CertException {
94                 CSRMeta csr = ca.newCSRMeta();
95                 boolean first = true;
96                 // Set CN (and SAN)
97                 for(String fqdn : fqdns) {
98                         if(first) {
99                                 first = false;
100                                 csr.cn(fqdn);
101                         }
102                         csr.san(fqdn); // duplicate CN in SAN, per RFC 5280 section 4.2.1.6 
103                 }
104                 
105                 csr.challenge(new String(Symm.randomGen(24)));
106                 csr.mechID(mechid);
107                 csr.email(sponsorEmail);
108                 String errs;
109                 if((errs=validateApp(csr))!=null) {
110                         throw new CertException(errs);
111                 }
112                 return csr;
113         }
114         
115         private static String validateApp(CSRMeta csr) {
116                 CertmanValidator v = new CertmanValidator();
117                 if(v.nullOrBlank("cn", csr.cn())
118                         .nullOrBlank("mechID", csr.mechID())
119                         .nullOrBlank("email", csr.email())
120                         .err()) {
121                         return v.errs();
122                 } else {
123                         return null;
124                 }
125         }
126
127         public static CSRMeta createPersonalCSRMeta(CA ca, String personal, String email) throws CertException {
128                 CSRMeta csr = ca.newCSRMeta();
129                 csr.cn(personal);
130                 csr.challenge(new String(Symm.randomGen(24)));
131                 csr.email(email);
132                 String errs;
133                 if((errs=validatePersonal(csr))!=null) {
134                         throw new CertException(errs);
135                 }
136                 return csr;
137         }
138
139         private static String validatePersonal(CSRMeta csr) {
140                 CertmanValidator v = new CertmanValidator();
141                 if(v.nullOrBlank("cn", csr.cn())
142                         .nullOrBlank("email", csr.email())
143                         .err()) {
144                         return v.errs();
145                 } else {
146                         return null;
147                 }
148         }
149         
150
151 }