[OOM-CERT-SERVICE] Add Key Update Request functionality
[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 static org.onap.oom.certservice.cmpv2client.impl.CmpUtil.createRandomInt;
25 import static org.onap.oom.certservice.cmpv2client.impl.CmpUtil.generatePkiHeader;
26
27 import java.security.KeyPair;
28 import java.util.Date;
29
30 import org.bouncycastle.asn1.ASN1Integer;
31 import org.bouncycastle.asn1.DERBitString;
32 import org.bouncycastle.asn1.cmp.CMPCertificate;
33 import org.bouncycastle.asn1.cmp.PKIBody;
34 import org.bouncycastle.asn1.cmp.PKIHeader;
35 import org.bouncycastle.asn1.cmp.PKIMessage;
36 import org.bouncycastle.asn1.crmf.CertReqMessages;
37 import org.bouncycastle.asn1.crmf.CertReqMsg;
38 import org.bouncycastle.asn1.crmf.CertRequest;
39 import org.bouncycastle.asn1.crmf.CertTemplateBuilder;
40 import org.bouncycastle.asn1.crmf.ProofOfPossession;
41 import org.bouncycastle.asn1.x500.X500Name;
42 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
43 import org.bouncycastle.asn1.x509.GeneralName;
44 import org.bouncycastle.asn1.x509.SubjectPublicKeyInfo;
45 import org.bouncycastle.operator.DefaultSignatureAlgorithmIdentifierFinder;
46 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
47
48 /**
49  * Implementation of the CmpClient Interface conforming to RFC4210 (Certificate Management Protocol
50  * (CMP)) and RFC4211 (Certificate Request Message Format (CRMF)) standards.
51  */
52 class CreateCertRequest {
53
54     private PkiMessageProtection pkiMessageProtection;
55     private X500Name issuerDn;
56     private X500Name subjectDn;
57     private GeneralName[] sansArray;
58     private KeyPair subjectKeyPair;
59     private Date notBefore;
60     private Date notAfter;
61     private String senderKid;
62     private int cmpRequestType;
63     private CMPCertificate[] extraCerts;
64
65     private final int certReqId = createRandomInt(Integer.MAX_VALUE);
66     private final AlgorithmIdentifier signingAlgorithm = new DefaultSignatureAlgorithmIdentifierFinder()
67             .find("SHA256withRSA");
68
69     public void setIssuerDn(X500Name issuerDn) {
70         this.issuerDn = issuerDn;
71     }
72
73     public void setSubjectDn(X500Name subjectDn) {
74         this.subjectDn = subjectDn;
75     }
76
77     public void setSansArray(GeneralName[] sansArray) {
78         this.sansArray = sansArray;
79     }
80
81     public void setSubjectKeyPair(KeyPair subjectKeyPair) {
82         this.subjectKeyPair = subjectKeyPair;
83     }
84
85     public void setNotBefore(Date notBefore) {
86         this.notBefore = notBefore;
87     }
88
89     public void setNotAfter(Date notAfter) {
90         this.notAfter = notAfter;
91     }
92
93     public void setProtection(PkiMessageProtection pkiMessageProtection) {
94         this.pkiMessageProtection = pkiMessageProtection;
95     }
96
97     public void setSenderKid(String senderKid) {
98         this.senderKid = senderKid;
99     }
100
101     public void setCmpRequestType(int requestType) {
102         this.cmpRequestType = requestType;
103     }
104
105     public void setExtraCerts(CMPCertificate[] extraCert) {
106         this.extraCerts = extraCert;
107     }
108
109     /**
110      * Method to create {@link PKIMessage} from {@link CertRequest},{@link ProofOfPossession}, {@link
111      * CertReqMsg}, {@link CertReqMessages}, {@link PKIHeader} and {@link PKIBody}.
112      *
113      * @return {@link PKIMessage}
114      */
115     public PKIMessage generateCertReq() throws CmpClientException {
116         final CertTemplateBuilder certTemplateBuilder =
117                 new CertTemplateBuilder()
118                         .setIssuer(issuerDn)
119                         .setSubject(subjectDn)
120                         .setExtensions(CmpMessageHelper.generateExtension(sansArray))
121                         .setValidity(CmpMessageHelper.generateOptionalValidity(notBefore, notAfter))
122                         .setVersion(2)
123                         .setSerialNumber(new ASN1Integer(0L))
124                         .setSigningAlg(signingAlgorithm)
125                         .setPublicKey(
126                                 SubjectPublicKeyInfo.getInstance(subjectKeyPair.getPublic().getEncoded()));
127
128         final CertRequest certRequest = new CertRequest(certReqId, certTemplateBuilder.build(), null);
129         final ProofOfPossession proofOfPossession =
130                 CmpMessageHelper.generateProofOfPossession(certRequest, subjectKeyPair);
131
132         final CertReqMsg certReqMsg = new CertReqMsg(certRequest, proofOfPossession, null);
133         final CertReqMessages certReqMessages = new CertReqMessages(certReqMsg);
134
135         final PKIHeader pkiHeader =
136                 generatePkiHeader(
137                         subjectDn,
138                         issuerDn,
139                         pkiMessageProtection.getAlgorithmIdentifier(),
140                         senderKid);
141         final PKIBody pkiBody = new PKIBody(cmpRequestType, certReqMessages);
142
143         final DERBitString messageProtection = this.pkiMessageProtection.generatePkiMessageProtection(pkiHeader, pkiBody);
144         return new PKIMessage(pkiHeader, pkiBody, messageProtection, extraCerts);
145     }
146 }