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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.oom.certservice.cmpv2client.impl;
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;
27 import java.security.KeyPair;
28 import java.util.Date;
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;
44 * Implementation of the CmpClient Interface conforming to RFC4210 (Certificate Management Protocol
45 * (CMP)) and RFC4211 (Certificate Request Message Format (CRMF)) standards.
47 class CreateCertRequest {
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;
58 private static final int ITERATIONS = createRandomInt(5000);
59 private static final byte[] SALT = createRandomBytes();
60 private final int certReqId = createRandomInt(Integer.MAX_VALUE);
62 public void setIssuerDn(X500Name issuerDn) {
63 this.issuerDn = issuerDn;
66 public void setSubjectDn(X500Name subjectDn) {
67 this.subjectDn = subjectDn;
70 public void setSansArray(GeneralName[] sansArray) {
71 this.sansArray = sansArray;
74 public void setSubjectKeyPair(KeyPair subjectKeyPair) {
75 this.subjectKeyPair = subjectKeyPair;
78 public void setNotBefore(Date notBefore) {
79 this.notBefore = notBefore;
82 public void setNotAfter(Date notAfter) {
83 this.notAfter = notAfter;
86 public void setInitAuthPassword(String initAuthPassword) {
87 this.initAuthPassword = initAuthPassword;
90 public void setSenderKid(String senderKid) {
91 this.senderKid = senderKid;
95 * Method to create {@link PKIMessage} from {@link CertRequest},{@link ProofOfPossession}, {@link
96 * CertReqMsg}, {@link CertReqMessages}, {@link PKIHeader} and {@link PKIBody}.
98 * @return {@link PKIMessage}
100 public PKIMessage generateCertReq() throws CmpClientException {
101 final CertTemplateBuilder certTemplateBuilder =
102 new CertTemplateBuilder()
104 .setSubject(subjectDn)
105 .setExtensions(CmpMessageHelper.generateExtension(sansArray))
106 .setValidity(CmpMessageHelper.generateOptionalValidity(notBefore, notAfter))
108 SubjectPublicKeyInfo.getInstance(subjectKeyPair.getPublic().getEncoded()));
110 final CertRequest certRequest = new CertRequest(certReqId, certTemplateBuilder.build(), null);
111 final ProofOfPossession proofOfPossession =
112 CmpMessageHelper.generateProofOfPossession(certRequest, subjectKeyPair);
114 final CertReqMsg certReqMsg = new CertReqMsg(certRequest, proofOfPossession, null);
115 final CertReqMessages certReqMessages = new CertReqMessages(certReqMsg);
117 final PKIHeader pkiHeader =
121 CmpMessageHelper.protectionAlgoIdentifier(ITERATIONS, SALT),
123 final PKIBody pkiBody = new PKIBody(PKIBody.TYPE_INIT_REQ, certReqMessages);
125 return CmpMessageHelper.protectPkiMessage(
126 pkiHeader, pkiBody, initAuthPassword, ITERATIONS, SALT);