test.unit.be.fedict.hsm.entity.KeyStoreSingletonBeanTest.java Source code

Java tutorial

Introduction

Here is the source code for test.unit.be.fedict.hsm.entity.KeyStoreSingletonBeanTest.java

Source

/*
 * HSM Proxy Project.
 * Copyright (C) 2013 FedICT.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version
 * 3.0 as published by the Free Software Foundation.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, see 
 * http://www.gnu.org/licenses/.
 */

package test.unit.be.fedict.hsm.entity;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.lang.reflect.Field;
import java.math.BigInteger;
import java.security.KeyStore;
import java.security.MessageDigest;
import java.security.Signature;
import java.security.interfaces.RSAPublicKey;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.apache.commons.codec.binary.Hex;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;

import be.fedict.hsm.entity.KeyStoreEntity;
import be.fedict.hsm.entity.KeyStoreType;
import be.fedict.hsm.model.KeyStoreLoaderBean;
import be.fedict.hsm.model.KeyStoreSingletonBean;

public class KeyStoreSingletonBeanTest {

    private static final Log LOG = LogFactory.getLog(KeyStoreSingletonBeanTest.class);

    @Test
    public void testSignature() throws Exception {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("test");
        EntityManager entityManager = entityManagerFactory.createEntityManager();

        EntityTransaction entityTransaction = entityManager.getTransaction();
        entityTransaction.begin();

        KeyStoreEntity keyStoreEntity = new KeyStoreEntity("test", KeyStoreType.PKCS12,
                KeyStoreSingletonBeanTest.class.getResource("/keystore.p12").toURI().getPath(), "secret");
        entityManager.persist(keyStoreEntity);

        KeyStoreSingletonBean keyStoreSingletonBean = new KeyStoreSingletonBean();

        Field entityManagerField = KeyStoreSingletonBean.class.getDeclaredField("entityManager");
        entityManagerField.setAccessible(true);
        entityManagerField.set(keyStoreSingletonBean, entityManager);

        KeyStoreLoaderBean keyStoreLoaderBean = new KeyStoreLoaderBean();
        Field keyStoreLoaderField = KeyStoreSingletonBean.class.getDeclaredField("keyStoreLoader");
        keyStoreLoaderField.setAccessible(true);
        keyStoreLoaderField.set(keyStoreSingletonBean, keyStoreLoaderBean);

        keyStoreSingletonBean.loadKeys();

        keyStoreSingletonBean.newKeyStore(keyStoreEntity.getId());

        byte[] toBeSigned = "hello world".getBytes();
        MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
        messageDigest.update(toBeSigned);
        byte[] digestValue = messageDigest.digest();
        LOG.debug("digest value: " + new String(Hex.encodeHex(digestValue)));
        byte[] signatureValue = keyStoreSingletonBean.sign(keyStoreEntity.getId(), "alias", "SHA-1", digestValue);

        assertNotNull(signatureValue);
        LOG.debug("signature size: " + signatureValue.length);

        KeyStore keyStore = KeyStore.getInstance("PKCS12");
        keyStore.load(KeyStoreSingletonBeanTest.class.getResourceAsStream("/keystore.p12"), "secret".toCharArray());
        RSAPublicKey publicKey = (RSAPublicKey) keyStore.getCertificate("alias").getPublicKey();

        BigInteger signatureValueBigInteger = new BigInteger(signatureValue);
        BigInteger originalBigInteger = signatureValueBigInteger.modPow(publicKey.getPublicExponent(),
                publicKey.getModulus());
        LOG.debug("original message: " + new String(Hex.encodeHex(originalBigInteger.toByteArray())));

        Signature signature = Signature.getInstance("SHA1withRSA");
        signature.initVerify(publicKey);
        signature.update(toBeSigned);
        boolean result = signature.verify(signatureValue);
        assertTrue(result);
    }
}