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.cert;
23 import java.io.IOException;
24 import java.math.BigInteger;
25 import java.security.KeyPair;
26 import java.security.SecureRandom;
27 import java.security.cert.CertificateException;
28 import java.security.cert.X509Certificate;
29 import java.util.ArrayList;
30 import java.util.Date;
31 import java.util.GregorianCalendar;
32 import java.util.List;
34 import org.bouncycastle.asn1.ASN1Sequence;
35 import org.bouncycastle.asn1.DERPrintableString;
36 import org.bouncycastle.asn1.pkcs.Attribute;
37 import org.bouncycastle.asn1.pkcs.PKCSObjectIdentifiers;
38 import org.bouncycastle.asn1.x500.X500Name;
39 import org.bouncycastle.asn1.x500.X500NameBuilder;
40 import org.bouncycastle.asn1.x500.style.BCStyle;
41 import org.bouncycastle.asn1.x509.Extension;
42 import org.bouncycastle.asn1.x509.Extensions;
43 import org.bouncycastle.asn1.x509.GeneralName;
44 import org.bouncycastle.asn1.x509.GeneralNames;
45 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
46 import org.bouncycastle.cert.X509v3CertificateBuilder;
47 import org.bouncycastle.cert.jcajce.JcaX509CertificateConverter;
48 import org.bouncycastle.operator.OperatorCreationException;
49 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
50 import org.bouncycastle.pkcs.PKCS10CertificationRequestBuilder;
51 import org.bouncycastle.pkcs.jcajce.JcaPKCS10CertificationRequestBuilder;
52 import org.onap.aaf.cadi.cm.CertException;
53 import org.onap.aaf.cadi.cm.Factory;
54 import org.onap.aaf.misc.env.Trans;
56 public class CSRMeta {
58 private String mechID;
59 private String environment;
61 private String challenge;
62 private List<RDN> rdns;
64 public CSRMeta(List<RDN> rdns) {
68 private ArrayList<String> sanList = new ArrayList<String>();
69 private KeyPair keyPair;
70 private X500Name name = null;
71 private SecureRandom random = new SecureRandom();
73 public X500Name x500Name() throws IOException {
75 X500NameBuilder xnb = new X500NameBuilder();
76 xnb.addRDN(BCStyle.CN,cn);
77 xnb.addRDN(BCStyle.E,email);
79 if(environment==null) {
80 xnb.addRDN(BCStyle.OU,mechID);
82 xnb.addRDN(BCStyle.OU,mechID+':'+environment);
86 xnb.addRDN(rdn.aoi,rdn.value);
94 public PKCS10CertificationRequest generateCSR(Trans trans) throws IOException, CertException {
95 PKCS10CertificationRequestBuilder builder = new JcaPKCS10CertificationRequestBuilder(x500Name(),keypair(trans).getPublic());
97 DERPrintableString password = new DERPrintableString(challenge);
98 builder.addAttribute(PKCSObjectIdentifiers.pkcs_9_at_challengePassword, password);
101 int plus = email==null?0:1;
102 if(sanList.size()>0) {
103 GeneralName[] gna = new GeneralName[sanList.size()+plus];
105 for(String s : sanList) {
106 gna[++i]=new GeneralName(GeneralName.dNSName,s);
108 gna[++i]=new GeneralName(GeneralName.rfc822Name,email);
110 builder.addAttribute(
111 PKCSObjectIdentifiers.pkcs_9_at_extensionRequest,
112 new Extensions(new Extension[] {
113 new Extension(Extension.subjectAlternativeName,false,new GeneralNames(gna).getEncoded())
122 return builder.build(BCFactory.contentSigner(keypair(trans).getPrivate()));
123 } catch (OperatorCreationException e) {
124 throw new CertException(e);
128 @SuppressWarnings("deprecation")
129 public static void dump(PKCS10CertificationRequest csr) {
130 Attribute[] certAttributes = csr.getAttributes();
131 for (Attribute attribute : certAttributes) {
132 if (attribute.getAttrType().equals(PKCSObjectIdentifiers.pkcs_9_at_extensionRequest)) {
133 Extensions extensions = Extensions.getInstance(attribute.getAttrValues().getObjectAt(0));
134 GeneralNames gns = GeneralNames.fromExtensions(extensions,Extension.subjectAlternativeName);
135 GeneralName[] names = gns.getNames();
136 for(int k=0; k < names.length; k++) {
138 if(names[k].getTagNo() == GeneralName.dNSName) {
140 } else if(names[k].getTagNo() == GeneralName.iPAddress) {
142 // Deprecated, but I don't see anything better to use.
143 names[k].toASN1Object();
144 } else if(names[k].getTagNo() == GeneralName.otherName) {
146 } else if(names[k].getTagNo() == GeneralName.rfc822Name) {
150 System.out.println(title + ": "+ names[k].getName());
156 public X509Certificate initialConversationCert(Trans trans) throws IOException, CertificateException, OperatorCreationException {
157 GregorianCalendar gc = new GregorianCalendar();
158 Date start = gc.getTime();
159 gc.add(GregorianCalendar.DAY_OF_MONTH,2);
160 Date end = gc.getTime();
161 X509v3CertificateBuilder xcb = new X509v3CertificateBuilder(
163 new BigInteger(12,random), // replace with Serialnumber scheme
167 new SubjectPublicKeyInfo(ASN1Sequence.getInstance(keypair(trans).getPublic().getEncoded()))
169 return new JcaX509CertificateConverter().getCertificate(
170 xcb.build(BCFactory.contentSigner(keypair(trans).getPrivate())));
173 public CSRMeta san(String v) {
178 public List<String> sans() {
183 public KeyPair keypair(Trans trans) {
184 if(keyPair == null) {
185 keyPair = Factory.generateKeyPair(trans);
199 * @param cn the cn to set
201 public void cn(String cn) {
206 * Environment of Service MechID is good for
208 public void environment(String env) {
216 public String environment() {
223 public String mechID() {
229 * @param mechID the mechID to set
231 public void mechID(String mechID) {
232 this.mechID = mechID;
239 public String email() {
245 * @param email the email to set
247 public void email(String email) {
252 * @return the challenge
254 public String challenge() {
260 * @param challenge the challenge to set
262 public void challenge(String challenge) {
263 this.challenge = challenge;