[OOM-CERT-SERVICE] Refactor CertService API code
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / cmpv2client / impl / CreateCertRequest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation.
4  *  Copyright (C) 2021 Nokia.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.oom.certservice.cmpv2client.impl;
23
24 import org.bouncycastle.asn1.ASN1Integer;
25 import org.bouncycastle.asn1.DERBitString;
26 import org.bouncycastle.asn1.cmp.CMPCertificate;
27 import org.bouncycastle.asn1.cmp.PKIBody;
28 import org.bouncycastle.asn1.cmp.PKIHeader;
29 import org.bouncycastle.asn1.cmp.PKIMessage;
30 import org.bouncycastle.asn1.crmf.CertReqMessages;
31 import org.bouncycastle.asn1.crmf.CertReqMsg;
32 import org.bouncycastle.asn1.crmf.CertRequest;
33 import org.bouncycastle.asn1.crmf.CertTemplateBuilder;
34 import org.bouncycastle.asn1.crmf.ProofOfPossession;
35 import org.bouncycastle.asn1.x500.X500Name;
36 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
37 import org.bouncycastle.asn1.x509.GeneralName;
38 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
39 import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder;
40 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
41 import org.onap.oom.certservice.cmpv2client.impl.protections.PkiMessageProtection;
42
43 import java.security.KeyPair;
44 import java.util.Date;
45
46 import static org.onap.oom.certservice.cmpv2client.impl.CmpUtil.createRandomInt;
47 import static org.onap.oom.certservice.cmpv2client.impl.CmpUtil.generatePkiHeader;
48
49 /**
50  * Implementation of the CmpClient Interface conforming to RFC4210 (Certificate Management Protocol
51  * (CMP)) and RFC4211 (Certificate Request Message Format (CRMF)) standards.
52  */
53 class CreateCertRequest {
54
55     private PkiMessageProtection pkiMessageProtection;
56     private X500Name issuerDn;
57     private X500Name subjectDn;
58     private GeneralName[] sansArray;
59     private KeyPair subjectKeyPair;
60     private Date notBefore;
61     private Date notAfter;
62     private String senderKid;
63     private int cmpRequestType;
64     private CMPCertificate[] extraCerts;
65
66     private final int certReqId = createRandomInt(Integer.MAX_VALUE);
67     private final AlgorithmIdentifier signingAlgorithm = new DefaultSignatureAlgorithmIdentifierFinder()
68             .find("SHA256withRSA");
69
70     public void setIssuerDn(X500Name issuerDn) {
71         this.issuerDn = issuerDn;
72     }
73
74     public void setSubjectDn(X500Name subjectDn) {
75         this.subjectDn = subjectDn;
76     }
77
78     public void setSansArray(GeneralName[] sansArray) {
79         this.sansArray = sansArray;
80     }
81
82     public void setSubjectKeyPair(KeyPair subjectKeyPair) {
83         this.subjectKeyPair = subjectKeyPair;
84     }
85
86     public void setNotBefore(Date notBefore) {
87         this.notBefore = notBefore;
88     }
89
90     public void setNotAfter(Date notAfter) {
91         this.notAfter = notAfter;
92     }
93
94     public void setProtection(PkiMessageProtection pkiMessageProtection) {
95         this.pkiMessageProtection = pkiMessageProtection;
96     }
97
98     public void setSenderKid(String senderKid) {
99         this.senderKid = senderKid;
100     }
101
102     public void setCmpRequestType(int requestType) {
103         this.cmpRequestType = requestType;
104     }
105
106     public void setExtraCerts(CMPCertificate[] extraCert) {
107         this.extraCerts = extraCert;
108     }
109
110     /**
111      * Method to create {@link PKIMessage} from {@link CertRequest},{@link ProofOfPossession}, {@link
112      * CertReqMsg}, {@link CertReqMessages}, {@link PKIHeader} and {@link PKIBody}.
113      *
114      * @return {@link PKIMessage}
115      */
116     public PKIMessage generateCertReq() throws CmpClientException {
117         final CertTemplateBuilder certTemplateBuilder =
118                 new CertTemplateBuilder()
119                         .setIssuer(issuerDn)
120                         .setSubject(subjectDn)
121                         .setExtensions(CmpMessageHelper.generateExtension(sansArray))
122                         .setValidity(CmpMessageHelper.generateOptionalValidity(notBefore, notAfter))
123                         .setVersion(2)
124                         .setSerialNumber(new ASN1Integer(0L))
125                         .setSigningAlg(signingAlgorithm)
126                         .setPublicKey(
127                                 SubjectPublicKeyInfo.getInstance(subjectKeyPair.getPublic().getEncoded()));
128
129         final CertRequest certRequest = new CertRequest(certReqId, certTemplateBuilder.build(), null);
130         final ProofOfPossession proofOfPossession =
131                 CmpMessageHelper.generateProofOfPossession(certRequest, subjectKeyPair);
132
133         final CertReqMsg certReqMsg = new CertReqMsg(certRequest, proofOfPossession, null);
134         final CertReqMessages certReqMessages = new CertReqMessages(certReqMsg);
135
136         final PKIHeader pkiHeader =
137                 generatePkiHeader(
138                         subjectDn,
139                         issuerDn,
140                         pkiMessageProtection.getAlgorithmIdentifier(),
141                         senderKid);
142         final PKIBody pkiBody = new PKIBody(cmpRequestType, certReqMessages);
143
144         final DERBitString messageProtection = this.pkiMessageProtection.generatePkiMessageProtection(pkiHeader, pkiBody);
145         return new PKIMessage(pkiHeader, pkiBody, messageProtection, extraCerts);
146     }
147 }