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.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;
47 * Implementation of the CmpClient Interface conforming to RFC4210 (Certificate Management Protocol
48 * (CMP)) and RFC4211 (Certificate Request Message Format (CRMF)) standards.
50 class CreateCertRequest {
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;
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");
67 public void setIssuerDn(X500Name issuerDn) {
68 this.issuerDn = issuerDn;
71 public void setSubjectDn(X500Name subjectDn) {
72 this.subjectDn = subjectDn;
75 public void setSansArray(GeneralName[] sansArray) {
76 this.sansArray = sansArray;
79 public void setSubjectKeyPair(KeyPair subjectKeyPair) {
80 this.subjectKeyPair = subjectKeyPair;
83 public void setNotBefore(Date notBefore) {
84 this.notBefore = notBefore;
87 public void setNotAfter(Date notAfter) {
88 this.notAfter = notAfter;
91 public void setInitAuthPassword(String initAuthPassword) {
92 this.initAuthPassword = initAuthPassword;
95 public void setSenderKid(String senderKid) {
96 this.senderKid = senderKid;
100 * Method to create {@link PKIMessage} from {@link CertRequest},{@link ProofOfPossession}, {@link
101 * CertReqMsg}, {@link CertReqMessages}, {@link PKIHeader} and {@link PKIBody}.
103 * @return {@link PKIMessage}
105 public PKIMessage generateCertReq() throws CmpClientException {
106 final CertTemplateBuilder certTemplateBuilder =
107 new CertTemplateBuilder()
109 .setSubject(subjectDn)
110 .setExtensions(CmpMessageHelper.generateExtension(sansArray))
111 .setValidity(CmpMessageHelper.generateOptionalValidity(notBefore, notAfter))
113 .setSerialNumber(new ASN1Integer(0L))
114 .setSigningAlg(signingAlgorithm)
116 SubjectPublicKeyInfo.getInstance(subjectKeyPair.getPublic().getEncoded()));
118 final CertRequest certRequest = new CertRequest(certReqId, certTemplateBuilder.build(), null);
119 final ProofOfPossession proofOfPossession =
120 CmpMessageHelper.generateProofOfPossession(certRequest, subjectKeyPair);
122 final CertReqMsg certReqMsg = new CertReqMsg(certRequest, proofOfPossession, null);
123 final CertReqMessages certReqMessages = new CertReqMessages(certReqMsg);
125 final PKIHeader pkiHeader =
129 CmpMessageHelper.protectionAlgoIdentifier(ITERATIONS, SALT),
131 final PKIBody pkiBody = new PKIBody(PKIBody.TYPE_INIT_REQ, certReqMessages);
133 return CmpMessageHelper.protectPkiMessage(
134 pkiHeader, pkiBody, initAuthPassword, ITERATIONS, SALT);