Merge "[OOM-K8S-CERT-EXTERNAL-PROVIDER] Add check if cert should be updated"
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / cmpv2client / impl / protections / PkiMessageProtection.java
1 /*-
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
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.protections;
22
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;
30
31 import java.security.GeneralSecurityException;
32
33 import static org.onap.oom.certservice.cmpv2client.impl.CmpUtil.generateProtectedBytes;
34
35 /**
36  * Representation of PKIMessage protection. Complies with RFC4210 (Certificate Management Protocol
37  * (CMP)) and RFC4211 (Certificate Request Message Format (CRMF)) standards.
38  */
39 public abstract class PkiMessageProtection {
40
41     private static final Logger LOG = LoggerFactory.getLogger(PkiMessageProtection.class);
42
43     /**
44      * Takes PKIHeader and PKIBody as parameters and generates protection bytes.
45      *
46      * @return bytes representing protection wrapped into DERBitString object.
47      */
48     public DERBitString generatePkiMessageProtection(PKIHeader pkiHeader, PKIBody pkiBody) throws CmpClientException {
49         try {
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;
59         }
60     }
61
62     /**
63      * Returns Algorithm Identifier for protection of PKIMessage.
64      *
65      * @return Algorithm Identifier.
66      */
67     public abstract AlgorithmIdentifier getAlgorithmIdentifier();
68
69     /**
70      * Takes encoded bytes of PKIMessage (PKIHeader and PKIBody) and generates protection bytes.
71      *
72      * @return bytes representing protection.
73      */
74     abstract byte[] generateProtectionBytes(byte[] protectedBytes) throws GeneralSecurityException;
75
76 }