/*
 * Copyright 2016 Datalogics Inc.
 */

package com.datalogics.pdf.hsm.samples.mock;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.security.Key;
import java.security.KeyStoreException;
import java.security.KeyStoreSpi;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.security.cert.CertificateException;
import java.util.Date;
import java.util.Enumeration;

/**
 * Mock key store for testing.
 */
public class MockKeyStore extends KeyStoreSpi {

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineGetKey(java.lang.String, char[])
     */
    @Override
    public Key engineGetKey(final String alias, final char[] password) throws NoSuchAlgorithmException,
                    UnrecoverableKeyException {
        return null;
    }

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineGetCertificateChain(java.lang.String)
     */
    @Override
    public Certificate[] engineGetCertificateChain(final String alias) {
        return new Certificate[0];
    }

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineGetCertificate(java.lang.String)
     */
    @Override
    public Certificate engineGetCertificate(final String alias) {
        return null;
    }

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineGetCreationDate(java.lang.String)
     */
    @Override
    public Date engineGetCreationDate(final String alias) {
        return null;
    }

    /*
     * (non-Javadoc)
     *
     * @see java.security.KeyStoreSpi#engineSetKeyEntry(java.lang.String, java.security.Key, char[],
     * java.security.cert.Certificate[])
     */
    @Override
    public void engineSetKeyEntry(final String alias, final Key key, final char[] password, final Certificate[] chain)
                    throws KeyStoreException {

    }

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineSetKeyEntry(java.lang.String, byte[], java.security.cert.Certificate[])
     */
    @Override
    public void engineSetKeyEntry(final String alias, final byte[] key, final Certificate[] chain)
                    throws KeyStoreException {}

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineSetCertificateEntry(java.lang.String, java.security.cert.Certificate)
     */
    @Override
    public void engineSetCertificateEntry(final String alias, final Certificate cert) throws KeyStoreException {}

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineDeleteEntry(java.lang.String)
     */
    @Override
    public void engineDeleteEntry(final String alias) throws KeyStoreException {}

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineAliases()
     */
    @Override
    public Enumeration<String> engineAliases() {
        return null;
    }

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineContainsAlias(java.lang.String)
     */
    @Override
    public boolean engineContainsAlias(final String alias) {
        return false;
    }

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineSize()
     */
    @Override
    public int engineSize() {
        return 0;
    }

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineIsKeyEntry(java.lang.String)
     */
    @Override
    public boolean engineIsKeyEntry(final String alias) {
        return false;
    }

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineIsCertificateEntry(java.lang.String)
     */
    @Override
    public boolean engineIsCertificateEntry(final String alias) {
        return false;
    }

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineGetCertificateAlias(java.security.cert.Certificate)
     */
    @Override
    public String engineGetCertificateAlias(final Certificate cert) {
        return null;
    }

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineStore(java.io.OutputStream, char[])
     */
    @Override
    public void engineStore(final OutputStream stream, final char[] password)
                    throws IOException, NoSuchAlgorithmException, CertificateException {}

    /* (non-Javadoc)
     * @see java.security.KeyStoreSpi#engineLoad(java.io.InputStream, char[])
     */
    @Override
    public void engineLoad(final InputStream stream, final char[] password)
                    throws IOException, NoSuchAlgorithmException, CertificateException {}
}

@@ -192,16 +192,85 @@
 ty.Key;%0A
+import java.security.KeyPair;%0Aimport java.security.KeyPairGenerator;%0A
 import j
@@ -407,33 +407,66 @@
 ity.
-UnrecoverableKeyException
+NoSuchProviderException;%0Aimport java.security.SecureRandom
 ;%0Aim
@@ -619,16 +619,20 @@
 Mock key
+Pair
  store f
@@ -694,16 +694,572 @@
 oreSpi %7B
+%0A    private KeyPair keyPair;%0A%0A    /**%0A     * Constructor for MockKeyStore.%0A     *%0A     * @throws NoSuchProviderException if provider is not found%0A     * @throws NoSuchAlgorithmException if algorithm is not found%0A     */%0A    public MockKeyStore() throws NoSuchAlgorithmException, NoSuchProviderException %7B%0A        final KeyPairGenerator generator = KeyPairGenerator.getInstance(%22RSA%22);%0A        final SecureRandom random = SecureRandom.getInstance(%22SHA1PRNG%22);%0A        generator.initialize(1024, random);%0A        keyPair = generator.generateKeyPair();%0A    %7D
 %0A%0A    /*
@@ -1447,108 +1447,45 @@
 rd) 
-throws NoSuchAlgorithmException,%0A                    UnrecoverableKeyException %7B%0A        return null
+%7B%0A        return keyPair.getPrivate()
 ;%0A  
@@ -2516,22 +2516,16 @@
 eption %7B
-%0A%0A    
 %7D%0A%0A    /

Add ability to return a private key from the MockKeyStore