<commit_before>/*******************************************************************************
 * (C) Copyright 2015 Hewlett Packard Enterprise Development LP
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.hp.ov.sdk.certs;

import com.google.common.io.BaseEncoding;
import com.hp.ov.sdk.constants.SdkConstants;
import com.hp.ov.sdk.dto.CaCert;
import com.hp.ov.sdk.dto.RabbitMqClientCert;
import com.hp.ov.sdk.exceptions.SDKCertificateException;
import com.hp.ov.sdk.exceptions.SDKErrorEnum;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

public class CertificateStoreManager {

    public static SSLContext getSslContext(final RabbitMqClientCert rabbitMqClientCert, final CaCert caCert) {
        // Now initialize SSLContext with KeyStore and TrustStore.
        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance(SdkConstants.SSL_VERSION);
            sslContext.init(getInitiazedKeyManager(rabbitMqClientCert).getKeyManagers(),
                    getInitiazedTrustManager(caCert.getCaCert()).getTrustManagers(), new SecureRandom());

        } catch (final NoSuchAlgorithmException e) {
            throw new SDKCertificateException(SDKErrorEnum.certificateError,
                                              null, null, null,
                                              SdkConstants.CERTS, e);
        } catch (final KeyManagementException e) {
            throw new SDKCertificateException(SDKErrorEnum.certificateError,
                                              null, null, null,
                                              SdkConstants.CERTS, e);
        }
        return sslContext;
    }

    // TODO - exception
    private static KeyManagerFactory getInitiazedKeyManager(final RabbitMqClientCert certDto) {
        KeyManagerFactory kmf = null;
        Security.addProvider(new BouncyCastleProvider());
        try {
            // Read client certificate and private key.
            final byte[] encoded = BaseEncoding.base64().decode(certDto.getBase64SSLKeyData());

            final PKCS8EncodedKeySpec keySpec =
                new PKCS8EncodedKeySpec(encoded);
            final KeyFactory kf = KeyFactory.getInstance("RSA");
            final PrivateKey privateKey = kf.generatePrivate(keySpec);

            final String strClientCert = certDto.getBase64SSLCertData();<commit_msg>Fix exception while decoding RabbitMQ certificate<commit_after>/*******************************************************************************
 * (C) Copyright 2015 Hewlett Packard Enterprise Development LP
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * You may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package com.hp.ov.sdk.certs;

import com.google.common.io.BaseEncoding;
import com.hp.ov.sdk.constants.SdkConstants;
import com.hp.ov.sdk.dto.CaCert;
import com.hp.ov.sdk.dto.RabbitMqClientCert;
import com.hp.ov.sdk.exceptions.SDKCertificateException;
import com.hp.ov.sdk.exceptions.SDKErrorEnum;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

import javax.net.ssl.KeyManagerFactory;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManagerFactory;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.SecureRandom;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

public class CertificateStoreManager {

    public static SSLContext getSslContext(final RabbitMqClientCert rabbitMqClientCert, final CaCert caCert) {
        // Now initialize SSLContext with KeyStore and TrustStore.
        SSLContext sslContext = null;
        try {
            sslContext = SSLContext.getInstance(SdkConstants.SSL_VERSION);
            sslContext.init(getInitiazedKeyManager(rabbitMqClientCert).getKeyManagers(),
                    getInitiazedTrustManager(caCert.getCaCert()).getTrustManagers(), new SecureRandom());

        } catch (final NoSuchAlgorithmException e) {
            throw new SDKCertificateException(SDKErrorEnum.certificateError,
                                              null, null, null,
                                              SdkConstants.CERTS, e);
        } catch (final KeyManagementException e) {
            throw new SDKCertificateException(SDKErrorEnum.certificateError,
                                              null, null, null,
                                              SdkConstants.CERTS, e);
        }
        return sslContext;
    }

    // TODO - exception
    private static KeyManagerFactory getInitiazedKeyManager(final RabbitMqClientCert certDto) {
        KeyManagerFactory kmf = null;
        Security.addProvider(new BouncyCastleProvider());
        try {
            // Read client certificate and private key.
            final byte[] encoded = BaseEncoding.base64().withSeparator("\n", 65).decode(certDto.getBase64SSLKeyData());

            final PKCS8EncodedKeySpec keySpec =
                new PKCS8EncodedKeySpec(encoded);
            final KeyFactory kf = KeyFactory.getInstance("RSA");
            final PrivateKey privateKey = kf.generatePrivate(keySpec);

            final String strClientCert = certDto.getBase64SSLCertData();
{'fraction_non_alphanumeric': 0.11610437532946757, 'fraction_numerical': 0.004744333157617291, 'mean_word_length': 3.3943254198031267, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6038288', 'n_tokens_mistral': 1945, 'n_tokens_neox': 1862, 'n_words': 483}