[OOM-CERT-SERVICE] Refactor CertService API code
[oom/platform/cert-service.git] / certService / src / main / java / org / onap / oom / certservice / cmpv2client / impl / protections / PasswordBasedProtection.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.ASN1Integer;
24 import org.bouncycastle.asn1.ASN1ObjectIdentifier;
25 import org.bouncycastle.asn1.DEROctetString;
26 import org.bouncycastle.asn1.cmp.PBMParameter;
27 import org.bouncycastle.asn1.x509.AlgorithmIdentifier;
28 import org.bouncycastle.jce.provider.BouncyCastleProvider;
29
30 import javax.crypto.Mac;
31 import javax.crypto.SecretKey;
32 import javax.crypto.spec.SecretKeySpec;
33 import java.security.GeneralSecurityException;
34 import java.security.MessageDigest;
35 import java.security.NoSuchAlgorithmException;
36 import java.security.NoSuchProviderException;
37
38 import static org.onap.oom.certservice.cmpv2client.impl.CmpUtil.createRandomBytes;
39 import static org.onap.oom.certservice.cmpv2client.impl.CmpUtil.createRandomInt;
40
41 /**
42  * Implementation of password-based PKIMessage protection
43  */
44 public class PasswordBasedProtection extends PkiMessageProtection {
45
46     private static final int ITERATIONS = createRandomInt(1000);
47     private static final byte[] SALT = createRandomBytes();
48     private static final AlgorithmIdentifier OWF_ALGORITHM =
49             new AlgorithmIdentifier(new ASN1ObjectIdentifier("1.3.14.3.2.26"));
50     private static final AlgorithmIdentifier MAC_ALGORITHM =
51             new AlgorithmIdentifier(new ASN1ObjectIdentifier("1.3.6.1.5.5.8.1.2"));
52     private static final ASN1ObjectIdentifier PASSWORD_BASED_MAC =
53             new ASN1ObjectIdentifier("1.2.840.113533.7.66.13");
54
55     private final String initAuthPassword;
56
57     public PasswordBasedProtection(String initAuthPassword) {
58         this.initAuthPassword = initAuthPassword;
59     }
60
61     @Override
62     public AlgorithmIdentifier getAlgorithmIdentifier() {
63         ASN1Integer iteration = new ASN1Integer(ITERATIONS);
64         DEROctetString derSalt = new DEROctetString(SALT);
65
66         PBMParameter pp = new PBMParameter(derSalt, OWF_ALGORITHM, iteration, MAC_ALGORITHM);
67         return new AlgorithmIdentifier(PASSWORD_BASED_MAC, pp);
68     }
69
70     @Override
71     byte[] generateProtectionBytes(byte[] protectedBytes) throws GeneralSecurityException {
72         byte[] baseKey = generateBaseKey();
73         return generateMacBytes(baseKey, protectedBytes);
74     }
75
76     private byte[] generateBaseKey() throws NoSuchAlgorithmException, NoSuchProviderException {
77         byte[] raSecret = initAuthPassword.getBytes();
78         byte[] baseKey = new byte[raSecret.length + SALT.length];
79         System.arraycopy(raSecret, 0, baseKey, 0, raSecret.length);
80         System.arraycopy(SALT, 0, baseKey, raSecret.length, SALT.length);
81         MessageDigest dig =
82                 MessageDigest.getInstance(
83                         OWF_ALGORITHM.getAlgorithm().getId(), BouncyCastleProvider.PROVIDER_NAME);
84         for (int i = 0; i < ITERATIONS; i++) {
85             baseKey = dig.digest(baseKey);
86             dig.reset();
87         }
88         return baseKey;
89     }
90
91     private byte[] generateMacBytes(byte[] baseKey, byte[] protectedBytes) throws GeneralSecurityException {
92         Mac mac = Mac.getInstance(MAC_ALGORITHM.getAlgorithm().getId(), BouncyCastleProvider.PROVIDER_NAME);
93         SecretKey key = new SecretKeySpec(baseKey, MAC_ALGORITHM.getAlgorithm().getId());
94         mac.init(key);
95         mac.reset();
96         mac.update(protectedBytes, 0, protectedBytes.length);
97         return mac.doFinal();
98     }
99
100 }