Remove CSRMeta class dependency
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / aaf / certservice / certification / model / CsrModel.java
1 /*
2  * ============LICENSE_START=======================================================
3  * PROJECT
4  * ================================================================================
5  * Copyright (C) 2020 Nokia. 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.certservice.certification.model;
22
23 import java.io.IOException;
24 import java.security.KeyFactory;
25 import java.security.NoSuchAlgorithmException;
26 import java.security.PrivateKey;
27 import java.security.PublicKey;
28 import java.security.spec.InvalidKeySpecException;
29 import java.security.spec.PKCS8EncodedKeySpec;
30 import java.security.spec.X509EncodedKeySpec;
31 import java.util.Arrays;
32 import java.util.Collections;
33 import java.util.List;
34 import java.util.Objects;
35 import java.util.stream.Collectors;
36
37 import org.bouncycastle.asn1.x500.X500Name;
38 import org.bouncycastle.asn1.x509.Extension;
39 import org.bouncycastle.asn1.x509.Extensions;
40 import org.bouncycastle.asn1.x509.GeneralName;
41 import org.bouncycastle.asn1.x509.GeneralNames;
42 import org.bouncycastle.pkcs.PKCS10CertificationRequest;
43 import org.bouncycastle.util.io.pem.PemObject;
44
45 import org.onap.aaf.certservice.certification.exception.CsrDecryptionException;
46 import org.onap.aaf.certservice.certification.exception.DecryptionException;
47 import org.onap.aaf.certservice.certification.exception.KeyDecryptionException;
48
49
50 public class CsrModel {
51
52     private final PKCS10CertificationRequest csr;
53     private final X500Name subjectData;
54     private final PrivateKey privateKey;
55     private final PublicKey publicKey;
56     private final List<String> sans;
57
58     public CsrModel(PKCS10CertificationRequest csr, X500Name subjectData, PrivateKey privateKey, PublicKey publicKey,
59                     List<String> sans) {
60         this.csr = csr;
61         this.subjectData = subjectData;
62         this.privateKey = privateKey;
63         this.publicKey = publicKey;
64         this.sans = sans;
65     }
66
67     public PKCS10CertificationRequest getCsr() {
68         return csr;
69     }
70
71     public X500Name getSubjectData() {
72         return subjectData;
73     }
74
75     public PrivateKey getPrivateKey() {
76         return privateKey;
77     }
78
79     public PublicKey getPublicKey() {
80         return publicKey;
81     }
82
83     public List<String> getSans() {
84         return sans;
85     }
86
87     @Override
88     public String toString() {
89         return "Subject: { " + subjectData + " ,SANs: " + sans + " }";
90     }
91
92     public static class CsrModelBuilder {
93
94         private final PKCS10CertificationRequest csr;
95         private final PemObject privateKey;
96
97         public CsrModel build() throws DecryptionException {
98
99             X500Name subjectData = getSubjectData();
100             PrivateKey javaPrivateKey = convertingPemPrivateKeyToJavaSecurityPrivateKey(getPrivateKey());
101             PublicKey javaPublicKey = convertingPemPublicKeyToJavaSecurityPublicKey(getPublicKey());
102             List<String> sans = getSansData();
103
104             return new CsrModel(csr, subjectData, javaPrivateKey, javaPublicKey, sans);
105         }
106
107         public CsrModelBuilder(PKCS10CertificationRequest csr, PemObject privateKey) {
108             this.csr = csr;
109             this.privateKey = privateKey;
110         }
111
112         private PemObject getPublicKey() throws CsrDecryptionException {
113             try {
114                 return new PemObject("PUBLIC KEY", csr.getSubjectPublicKeyInfo().getEncoded());
115             } catch (IOException e) {
116                 throw new CsrDecryptionException("Reading Public Key from CSR failed", e.getCause());
117             }
118         }
119
120         private PemObject getPrivateKey() {
121             return privateKey;
122         }
123
124         private X500Name getSubjectData() {
125             return csr.getSubject();
126         }
127
128         private List<String> getSansData() {
129             if (!isAttrsEmpty() && !isAttrsValuesEmpty()) {
130                 Extensions extensions = Extensions.getInstance(csr.getAttributes()[0].getAttrValues().getObjectAt(0));
131                 GeneralName[] arrayOfAlternativeNames =
132                         GeneralNames.fromExtensions(extensions, Extension.subjectAlternativeName).getNames();
133                 return Arrays.stream(arrayOfAlternativeNames).map(GeneralName::getName).map(Objects::toString)
134                                .collect(Collectors.toList());
135             }
136             return Collections.emptyList();
137         }
138
139         private boolean isAttrsValuesEmpty() {
140             return csr.getAttributes()[0].getAttrValues().size() == 0;
141         }
142
143         private boolean isAttrsEmpty() {
144             return csr.getAttributes().length == 0;
145         }
146
147         private PrivateKey convertingPemPrivateKeyToJavaSecurityPrivateKey(PemObject privateKey)
148                 throws KeyDecryptionException {
149             try {
150                 KeyFactory factory = KeyFactory.getInstance("RSA");
151                 PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(privateKey.getContent());
152                 return factory.generatePrivate(keySpec);
153             } catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
154                 throw new KeyDecryptionException("Converting Private Key failed", e.getCause());
155             }
156         }
157
158         private PublicKey convertingPemPublicKeyToJavaSecurityPublicKey(PemObject publicKey)
159                 throws KeyDecryptionException {
160             try {
161                 KeyFactory factory = KeyFactory.getInstance("RSA");
162                 X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey.getContent());
163                 return factory.generatePublic(keySpec);
164             } catch (InvalidKeySpecException | NoSuchAlgorithmException e) {
165                 throw new KeyDecryptionException("Converting Public Key from CSR failed", e.getCause());
166             }
167         }
168     }
169
170 }