2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Nokia.
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.protections;
23 import org.bouncycastle.asn1.DERBitString;
24 import org.bouncycastle.asn1.cmp.PKIBody;
25 import org.bouncycastle.asn1.cmp.PKIHeader;
26 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
27 import org.onap.oom.certservice.cmpv2client.exceptions.CmpClientException;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
31 import java.security.GeneralSecurityException;
33 import static org.onap.oom.certservice.cmpv2client.impl.CmpUtil.generateProtectedBytes;
36 * Representation of PKIMessage protection. Complies with RFC4210 (Certificate Management Protocol
37 * (CMP)) and RFC4211 (Certificate Request Message Format (CRMF)) standards.
39 public abstract class PkiMessageProtection {
41 private static final Logger LOG = LoggerFactory.getLogger(PkiMessageProtection.class);
44 * Takes PKIHeader and PKIBody as parameters and generates protection bytes.
46 * @return bytes representing protection wrapped into DERBitString object.
48 public DERBitString generatePkiMessageProtection(PKIHeader pkiHeader, PKIBody pkiBody) throws CmpClientException {
50 byte[] protectedBytes = generateProtectedBytes(pkiHeader, pkiBody);
51 byte[] protectionBytes = generateProtectionBytes(protectedBytes);
52 return new DERBitString(protectionBytes);
53 } catch (GeneralSecurityException ex) {
54 CmpClientException cmpClientException =
55 new CmpClientException(
56 "Exception occurred while generating protection for PKIMessage", ex);
57 LOG.error("Exception occurred while generating the protection for PKIMessage");
58 throw cmpClientException;
63 * Returns Algorithm Identifier for protection of PKIMessage.
65 * @return Algorithm Identifier.
67 public abstract AlgorithmIdentifier getAlgorithmIdentifier();
70 * Takes encoded bytes of PKIMessage (PKIHeader and PKIBody) and generates protection bytes.
72 * @return bytes representing protection.
74 abstract byte[] generateProtectionBytes(byte[] protectedBytes) throws GeneralSecurityException;