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