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;
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;
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;
38 import static org.onap.oom.certservice.cmpv2client.impl.CmpUtil.createRandomBytes;
39 import static org.onap.oom.certservice.cmpv2client.impl.CmpUtil.createRandomInt;
42 * Implementation of password-based PKIMessage protection
44 public class PasswordBasedProtection extends PkiMessageProtection {
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");
55 private final String initAuthPassword;
57 PasswordBasedProtection(String initAuthPassword) {
58 this.initAuthPassword = initAuthPassword;
62 AlgorithmIdentifier getAlgorithmIdentifier() {
63 ASN1Integer iteration = new ASN1Integer(ITERATIONS);
64 DEROctetString derSalt = new DEROctetString(SALT);
66 PBMParameter pp = new PBMParameter(derSalt, OWF_ALGORITHM, iteration, MAC_ALGORITHM);
67 return new AlgorithmIdentifier(PASSWORD_BASED_MAC, pp);
71 byte[] generateProtectionBytes(byte[] protectedBytes) throws GeneralSecurityException {
72 byte[] baseKey = generateBaseKey();
73 return generateMacBytes(baseKey, protectedBytes);
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);
82 MessageDigest.getInstance(
83 OWF_ALGORITHM.getAlgorithm().getId(), BouncyCastleProvider.PROVIDER_NAME);
84 for (int i = 0; i < ITERATIONS; i++) {
85 baseKey = dig.digest(baseKey);
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());
96 mac.update(protectedBytes, 0, protectedBytes.length);