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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.oom.certservice.cmpv2client.impl;
24 import org.bouncycastle.asn1.ASN1Encodable;
25 import org.bouncycastle.asn1.ASN1Encoding;
26 import org.bouncycastle.asn1.ASN1EncodableVector;
27 import org.bouncycastle.asn1.ASN1GeneralizedTime;
28 import org.bouncycastle.asn1.ASN1OctetString;
29 import org.bouncycastle.asn1.ASN1OutputStream;
30 import org.bouncycastle.asn1.DEROctetString;
31 import org.bouncycastle.asn1.DERSequence;
32 import org.bouncycastle.asn1.cmp.CMPObjectIdentifiers;
33 import org.bouncycastle.asn1.cmp.InfoTypeAndValue;
34 import org.bouncycastle.asn1.cmp.PKIBody;
35 import org.bouncycastle.asn1.cmp.PKIHeader;
36 import org.bouncycastle.asn1.cmp.PKIHeaderBuilder;
37 import org.bouncycastle.asn1.x500.X500Name;
38 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
39 import org.bouncycastle.asn1.x509.GeneralName;
40 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
44 import java.io.ByteArrayOutputStream;
45 import java.io.IOException;
46 import java.security.SecureRandom;
47 import java.util.Date;
48 import java.util.Objects;
50 public final class CmpUtil {
52 private static final Logger LOGGER = LoggerFactory.getLogger(CmpUtil.class);
53 private static final SecureRandom SECURE_RANDOM = new SecureRandom();
54 public static final int RANDOM_BYTE_LENGTH = 16;
55 public static final int RANDOM_SEED = 1000;
61 * Validates specified object reference is not null.
63 * @param argument T - the type of the reference.
64 * @param message message - detail message to be used in the event that a NullPointerException is
66 * @return The Object if not null
68 public static <T> T notNull(T argument, String message) {
69 return Objects.requireNonNull(argument, message + " must not be null");
73 * Validates String object reference is not null and not empty.
75 * @param stringArg String Object that need to be validated.
78 public static boolean isNullOrEmpty(String stringArg) {
79 return (stringArg != null && !stringArg.trim().isEmpty());
83 * Creates a random number than can be used for sendernonce, transactionId and salts.
85 * @return bytes containing a random number string representing a nonce
87 public static byte[] createRandomBytes() {
88 LOGGER.debug("Generating random array of bytes");
89 byte[] randomBytes = new byte[RANDOM_BYTE_LENGTH];
90 SECURE_RANDOM.nextBytes(randomBytes);
95 * Creates a random integer than can be used to represent a transactionId or determine the number
96 * iterations in a protection algorithm.
98 * @return bytes containing a random number string representing a nonce
100 public static int createRandomInt(int range) {
101 LOGGER.debug("Generating random integer");
102 return SECURE_RANDOM.nextInt(range) + RANDOM_SEED;
106 * Generates protected bytes of a combined PKIHeader and PKIBody.
108 * @param header Header of PKIMessage containing common parameters
109 * @param body Body of PKIMessage containing specific information for message
110 * @return bytes representing the PKIHeader and PKIBody thats to be protected
112 public static byte[] generateProtectedBytes(PKIHeader header, PKIBody body) throws CmpClientException {
113 LOGGER.debug("Generating array of bytes representing PkiHeader and PkiBody");
115 ASN1EncodableVector vector = new ASN1EncodableVector();
118 ASN1Encodable protectedPart = new DERSequence(vector);
119 try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
120 ASN1OutputStream out = ASN1OutputStream.create(baos,ASN1Encoding.DER);
121 out.writeObject(protectedPart);
122 res = baos.toByteArray();
123 } catch (IOException ioe) {
124 CmpClientException cmpClientException =
125 new CmpClientException("IOException occurred while creating protectedBytes", ioe);
126 LOGGER.error("IOException occurred while creating protectedBytes");
127 throw cmpClientException;
133 * Generates a PKIHeader object.
135 * @param subjectDn distinguished name of Subject
136 * @param issuerDn distinguished name of external CA
137 * @param protectionAlg protection Algorithm used to protect PKIMessage
138 * @param senderKid sender identifier for receiver used for verification
141 static PKIHeader generatePkiHeader(
142 X500Name subjectDn, X500Name issuerDn, AlgorithmIdentifier protectionAlg, String senderKid) {
143 LOGGER.debug("Generating a Pki Header Builder");
144 PKIHeaderBuilder pkiHeaderBuilder =
145 new PKIHeaderBuilder(
146 PKIHeader.CMP_2000, new GeneralName(subjectDn), new GeneralName(issuerDn));
148 pkiHeaderBuilder.setMessageTime(new ASN1GeneralizedTime(new Date()));
149 pkiHeaderBuilder.setSenderNonce(new DEROctetString(createRandomBytes()));
150 pkiHeaderBuilder.setTransactionID(new DEROctetString(createRandomBytes()));
151 pkiHeaderBuilder.setProtectionAlg(protectionAlg);
152 pkiHeaderBuilder.setGeneralInfo(new InfoTypeAndValue(CMPObjectIdentifiers.it_implicitConfirm));
153 pkiHeaderBuilder.setSenderKID(mapToAsn1OctetString(senderKid));
155 return pkiHeaderBuilder.build();
158 private static ASN1OctetString mapToAsn1OctetString(String string) {
159 return string != null ? new DEROctetString(string.getBytes()) : null;