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