1 /*******************************************************************************
\r
2 * ============LICENSE_START====================================================
\r
4 * * ===========================================================================
\r
5 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * * ===========================================================================
\r
7 * * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * * you may not use this file except in compliance with the License.
\r
9 * * You may obtain a copy of the License at
\r
11 * * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * * Unless required by applicable law or agreed to in writing, software
\r
14 * * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * * See the License for the specific language governing permissions and
\r
17 * * limitations under the License.
\r
18 * * ============LICENSE_END====================================================
\r
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
\r
22 ******************************************************************************/
\r
23 package org.onap.aaf.authz.cm.cert;
\r
25 import java.io.IOException;
\r
26 import java.math.BigInteger;
\r
27 import java.security.KeyPair;
\r
28 import java.security.SecureRandom;
\r
29 import java.security.cert.CertificateException;
\r
30 import java.security.cert.X509Certificate;
\r
31 import java.util.ArrayList;
\r
32 import java.util.Date;
\r
33 import java.util.GregorianCalendar;
\r
34 import java.util.List;
\r
36 import org.bouncycastle.asn1.ASN1Sequence;
\r
37 import org.bouncycastle.asn1.DERPrintableString;
\r
38 import org.bouncycastle.asn1.pkcs.Attribute;
\r
39 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
\r
40 import org.bouncycastle.asn1.x500.X500Name;
\r
41 import org.bouncycastle.asn1.x500.X500NameBuilder;
\r
42 import org.bouncycastle.asn1.x500.style.BCStyle;
\r
43 import org.bouncycastle.asn1.x509.Extension;
\r
44 import org.bouncycastle.asn1.x509.Extensions;
\r
45 import org.bouncycastle.asn1.x509.GeneralName;
\r
46 import org.bouncycastle.asn1.x509.GeneralNames;
\r
47 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
\r
48 import org.bouncycastle.cert.X509v3CertificateBuilder;
\r
49 import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
\r
50 import org.bouncycastle.operator.OperatorCreationException;
\r
51 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
\r
52 import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
\r
53 import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
\r
55 import org.onap.aaf.cadi.cm.CertException;
\r
56 import org.onap.aaf.cadi.cm.Factory;
\r
57 import org.onap.aaf.inno.env.Trans;
\r
59 public class CSRMeta {
\r
60 private String environment;
\r
62 private String mechID;
\r
63 private String email;
\r
68 private String challenge;
\r
70 private ArrayList<String> sanList = new ArrayList<String>();
\r
72 private KeyPair keyPair;
\r
73 private X500Name name = null;
\r
74 private SecureRandom random = new SecureRandom();
\r
76 public X500Name x500Name() throws IOException {
\r
78 X500NameBuilder xnb = new X500NameBuilder();
\r
79 xnb.addRDN(BCStyle.CN,cn);
\r
80 xnb.addRDN(BCStyle.E,email);
\r
81 if(environment==null) {
\r
82 xnb.addRDN(BCStyle.OU,mechID);
\r
84 xnb.addRDN(BCStyle.OU,mechID+':'+environment);
\r
86 xnb.addRDN(BCStyle.O,o);
\r
87 xnb.addRDN(BCStyle.L,l);
\r
88 xnb.addRDN(BCStyle.ST,st);
\r
89 xnb.addRDN(BCStyle.C,c);
\r
96 public PKCS10CertificationRequest generateCSR(Trans trans) throws IOException, CertException {
\r
97 PKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(x500Name(),keypair(trans).getPublic());
\r
98 if(challenge!=null) {
\r
99 DERPrintableString password = new DERPrintableString(challenge);
\r
100 builder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_challengePassword, password);
\r
103 if(sanList.size()>0) {
\r
104 GeneralName[] gna = new GeneralName[sanList.size()];
\r
106 for(String s : sanList) {
\r
107 gna[++i]=new GeneralName(GeneralName.dNSName,s);
\r
110 builder.addAttribute(
\r
111 PKCSObjectIdentifiers.pkcs_9_at_extensionRequest,
\r
112 new Extensions(new Extension[] {
\r
113 new Extension(Extension.subjectAlternativeName,false,new GeneralNames(gna).getEncoded())
\r
117 // builder.addAttribute(Extension.basicConstraints,new BasicConstraints(false))
\r
118 // .addAttribute(Extension.keyUsage, new KeyUsage(KeyUsage.digitalSignature
\r
119 // | KeyUsage.keyEncipherment));
\r
121 return builder.build(BCFactory.contentSigner(keypair(trans).getPrivate()));
\r
122 } catch (OperatorCreationException e) {
\r
123 throw new CertException(e);
\r
127 @SuppressWarnings("deprecation")
\r
128 public static void dump(PKCS10CertificationRequest csr) {
\r
129 Attribute[] certAttributes = csr.getAttributes();
\r
130 for (Attribute attribute : certAttributes) {
\r
131 if (attribute.getAttrType().equals(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest)) {
\r
132 Extensions extensions = Extensions.getInstance(attribute.getAttrValues().getObjectAt(0));
\r
133 // Extension ext = extensions.getExtension(Extension.subjectAlternativeName);
\r
134 GeneralNames gns = GeneralNames.fromExtensions(extensions,Extension.subjectAlternativeName);
\r
135 GeneralName[] names = gns.getNames();
\r
136 for(int k=0; k < names.length; k++) {
\r
138 if(names[k].getTagNo() == GeneralName.dNSName) {
\r
141 else if(names[k].getTagNo() == GeneralName.iPAddress) {
\r
142 title = "iPAddress";
\r
143 // Deprecated, but I don't see anything better to use.
\r
144 names[k].toASN1Object();
\r
146 else if(names[k].getTagNo() == GeneralName.otherName) {
\r
147 title = "otherName";
\r
149 System.out.println(title + ": "+ names[k].getName());
\r
155 public X509Certificate initialConversationCert(Trans trans) throws IOException, CertificateException, OperatorCreationException {
\r
156 GregorianCalendar gc = new GregorianCalendar();
\r
157 Date start = gc.getTime();
\r
158 gc.add(GregorianCalendar.DAY_OF_MONTH,2);
\r
159 Date end = gc.getTime();
\r
160 X509v3CertificateBuilder xcb = new X509v3CertificateBuilder(
\r
162 new BigInteger(12,random), // replace with Serialnumber scheme
\r
166 // SubjectPublicKeyInfoFactory.createSubjectPublicKeyInfo(caCert.getPublicKey().getEn)
\r
167 new SubjectPublicKeyInfo(ASN1Sequence.getInstance(keypair(trans).getPublic().getEncoded()))
\r
169 return new JcaX509CertificateConverter().getCertificate(
\r
170 xcb.build(BCFactory.contentSigner(keypair(trans).getPrivate())));
\r
173 public CSRMeta san(String v) {
\r
178 public List<String> sans() {
\r
183 public KeyPair keypair(Trans trans) {
\r
184 if(keyPair == null) {
\r
185 keyPair = Factory.generateKeyPair(trans);
\r
193 public String cn() {
\r
199 * @param cn the cn to set
\r
201 public void cn(String cn) {
\r
206 * Environment of Service MechID is good for
\r
208 public void environment(String env) {
\r
216 public String environment() {
\r
217 return environment;
\r
221 * @return the mechID
\r
223 public String mechID() {
\r
229 * @param mechID the mechID to set
\r
231 public void mechID(String mechID) {
\r
232 this.mechID = mechID;
\r
237 * @return the email
\r
239 public String email() {
\r
245 * @param email the email to set
\r
247 public void email(String email) {
\r
248 this.email = email;
\r
255 public String o() {
\r
261 * @param o the o to set
\r
263 public void o(String o) {
\r
271 public String l() {
\r
276 * @param l the l to set
\r
278 public void l(String l) {
\r
285 public String st() {
\r
291 * @param st the st to set
\r
293 public void st(String st) {
\r
301 public String c() {
\r
307 * @param c the c to set
\r
309 public void c(String c) {
\r
315 * @return the challenge
\r
317 public String challenge() {
\r
323 * @param challenge the challenge to set
\r
325 public void challenge(String challenge) {
\r
326 this.challenge = challenge;
\r