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