at.gv.egovernment.moa.id.config.auth.data.BPKDecryptionParameters.java Source code

Java tutorial

Introduction

Here is the source code for at.gv.egovernment.moa.id.config.auth.data.BPKDecryptionParameters.java

Source

/*
 * Copyright 2014 Federal Chancellery Austria
 * MOA-ID has been developed in a cooperation between BRZ, the Federal
 * Chancellery Austria - ICT staff unit, and Graz University of Technology.
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * http://www.osor.eu/eupl/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 *
 * This product combines work with different licenses. See the "NOTICE" text
 * file for details on the various modules and licenses.
 * The "NOTICE" text file is part of the distribution. Any derivative works
 * that you distribute must include a readable copy of the "NOTICE" text file.
 */
package at.gv.egovernment.moa.id.config.auth.data;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;

import org.apache.commons.lang.SerializationUtils;

import at.gv.egovernment.moa.logging.Logger;
import at.gv.egovernment.moa.util.Base64Utils;
import at.gv.egovernment.moa.util.KeyStoreUtils;

/**
 * @author tlenz
 *
 */
public class BPKDecryptionParameters implements Serializable {

    private static final long serialVersionUID = 1L;

    private byte[] keyStore = null;
    private String keyStorePassword = null;
    private String keyAlias = null;
    private String keyPassword = null;

    /**
     * @return
     * @throws IOException 
     */
    public PrivateKey getPrivateKey() {
        InputStream in = null;
        try {
            in = new ByteArrayInputStream(keyStore);
            KeyStore store = KeyStoreUtils.loadKeyStore(in, keyStorePassword);

            char[] chPassword = " ".toCharArray();
            if (keyPassword != null)
                chPassword = keyPassword.toCharArray();

            //          Certificate test = store.getCertificate(keyAlias);
            //          Base64Utils.encode(test.getPublicKey().getEncoded());

            return (PrivateKey) store.getKey(keyAlias, chPassword);

        } catch (KeyStoreException e) {
            Logger.error("Can not load private key from keystore.", e);

        } catch (IOException e) {
            Logger.error("Can not load private key from keystore.", e);

        } catch (UnrecoverableKeyException e) {
            Logger.error("Can not load private key from keystore.", e);

        } catch (NoSuchAlgorithmException e) {
            Logger.error("Can not load private key from keystore.", e);

        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    Logger.warn("Close InputStream failed.", e);
                }
            }
        }

        return null;
    }

    public byte[] serialize() {
        return SerializationUtils.serialize(this);

    }

    /**
     * @param keyStore the keyStore to set
     */
    public void setKeyStore(byte[] keyStore) {
        this.keyStore = keyStore;
    }

    /**
     * @param keyStorePassword the keyStorePassword to set
     */
    public void setKeyStorePassword(String keyStorePassword) {
        this.keyStorePassword = keyStorePassword;
    }

    /**
     * @param keyAlias the keyAlias to set
     */
    public void setKeyAlias(String keyAlias) {
        this.keyAlias = keyAlias;
    }

    /**
     * @param keyPassword the keyPassword to set
     */
    public void setKeyPassword(String keyPassword) {
        this.keyPassword = keyPassword;
    }

}