Search in sources :

Example 1 with DnieProvider

use of es.gob.jmulticard.jse.provider.DnieProvider in project jmulticard by ctt-gob-es.

the class TestJseProvider method testGetCertificateChain.

/**
 * prueba de obtención de la cadena de certificados.
 * @throws Exception En cualquier error.
 */
@SuppressWarnings("static-method")
@Test
@Ignore
public void testGetCertificateChain() throws Exception {
    final Provider p = new DnieProvider();
    Security.addProvider(p);
    // $NON-NLS-1$
    final KeyStore ks = KeyStore.getInstance("DNI");
    ks.load(null, PASSWORD);
    final Enumeration<String> aliases = ks.aliases();
    while (aliases.hasMoreElements()) {
        final String alias = aliases.nextElement();
        for (final Certificate cert : ks.getCertificateChain(alias)) {
            System.out.println(// $NON-NLS-1$
            "XXX: " + ((X509Certificate) cert).getSubjectX500Principal());
        }
    }
}
Also used : DnieProvider(es.gob.jmulticard.jse.provider.DnieProvider) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) Provider(java.security.Provider) DnieProvider(es.gob.jmulticard.jse.provider.DnieProvider) X509Certificate(java.security.cert.X509Certificate) Certificate(java.security.cert.Certificate) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with DnieProvider

use of es.gob.jmulticard.jse.provider.DnieProvider in project jmulticard by ctt-gob-es.

the class TestJseProvider method testProviderWithCustomConnection.

static void testProviderWithCustomConnection() throws Exception {
    final Provider p = new DnieProvider(new SmartcardIoConnection());
    Security.addProvider(p);
    // $NON-NLS-1$
    final KeyStore ks = KeyStore.getInstance("DNI");
    final CallbackHandler callbackHandler;
    // $NON-NLS-1$
    callbackHandler = (CallbackHandler) Class.forName("es.gob.jmulticard.ui.passwordcallback.gui.DnieCallbackHandler").getConstructor().newInstance();
    final LoadStoreParameter lsp = new LoadStoreParameter() {

        @Override
        public ProtectionParameter getProtectionParameter() {
            return new KeyStore.CallbackHandlerProtection(callbackHandler);
        }
    };
    ks.load(lsp);
    final Enumeration<String> aliases = ks.aliases();
    while (aliases.hasMoreElements()) {
        System.out.println(aliases.nextElement());
    }
    // $NON-NLS-1$
    final Signature signature = Signature.getInstance("SHA1withRSA");
    // $NON-NLS-1$
    signature.initSign((PrivateKey) ks.getKey("CertFirmaDigital", PASSWORD));
    // $NON-NLS-1$
    signature.update("Hola Mundo!!".getBytes());
    signature.sign();
    // $NON-NLS-1$
    System.out.println("Firma generada correctamente");
}
Also used : CallbackHandler(javax.security.auth.callback.CallbackHandler) DnieProvider(es.gob.jmulticard.jse.provider.DnieProvider) Signature(java.security.Signature) SmartcardIoConnection(es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection) KeyStore(java.security.KeyStore) Provider(java.security.Provider) DnieProvider(es.gob.jmulticard.jse.provider.DnieProvider) LoadStoreParameter(java.security.KeyStore.LoadStoreParameter)

Example 3 with DnieProvider

use of es.gob.jmulticard.jse.provider.DnieProvider in project jmulticard by ctt-gob-es.

the class TestJseProvider method testProviderWithDefaultConnection.

static void testProviderWithDefaultConnection() throws Exception {
    final Provider p = new DnieProvider();
    Security.addProvider(p);
    // $NON-NLS-1$
    final KeyStore ks = KeyStore.getInstance("DNI");
    ks.load(null, PASSWORD);
    final Enumeration<String> aliases = ks.aliases();
    String alias = null;
    while (aliases.hasMoreElements()) {
        alias = aliases.nextElement();
        System.out.println(alias);
    }
    // $NON-NLS-1$
    Assert.assertNotNull("La tarjeta debe tener al menos un certificado", alias);
    // $NON-NLS-1$
    final Signature signature = Signature.getInstance("SHA1withRSA");
    signature.initSign((PrivateKey) ks.getKey(alias, PASSWORD));
    // $NON-NLS-1$
    signature.update("Hola Mundo!!".getBytes());
    signature.sign();
    // $NON-NLS-1$
    System.out.println("Firma generada correctamente");
    System.out.println(((X509Certificate) ks.getCertificate(alias)).getIssuerX500Principal().toString());
}
Also used : DnieProvider(es.gob.jmulticard.jse.provider.DnieProvider) Signature(java.security.Signature) KeyStore(java.security.KeyStore) X509Certificate(java.security.cert.X509Certificate) Provider(java.security.Provider) DnieProvider(es.gob.jmulticard.jse.provider.DnieProvider)

Example 4 with DnieProvider

use of es.gob.jmulticard.jse.provider.DnieProvider in project jmulticard by ctt-gob-es.

the class TestDoubleSign method testDoubleSign.

static void testDoubleSign() throws Exception {
    final Provider p = new DnieProvider(new SmartcardIoConnection());
    Security.addProvider(p);
    // $NON-NLS-1$
    final KeyStore ks = KeyStore.getInstance("DNI");
    ks.load(null, PASSWORD);
    final Enumeration<String> aliases = ks.aliases();
    while (aliases.hasMoreElements()) {
        System.out.println(aliases.nextElement());
    }
    // $NON-NLS-1$
    Signature signature = Signature.getInstance("SHA1withRSA");
    // $NON-NLS-1$
    signature.initSign((PrivateKey) ks.getKey("CertFirmaDigital", PASSWORD));
    // $NON-NLS-1$
    signature.update("Hola Mundo!!".getBytes());
    signature.sign();
    // $NON-NLS-1$
    System.out.println("Primera firma generada correctamente");
    // $NON-NLS-1$
    signature = Signature.getInstance("SHA1withRSA");
    // $NON-NLS-1$
    signature.initSign((PrivateKey) ks.getKey("CertFirmaDigital", PASSWORD));
    // $NON-NLS-1$
    signature.update("Hola Mundo 2!!".getBytes());
    signature.sign();
    // $NON-NLS-1$
    System.out.println("Segunda firma generada correctamente");
}
Also used : DnieProvider(es.gob.jmulticard.jse.provider.DnieProvider) Signature(java.security.Signature) SmartcardIoConnection(es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection) KeyStore(java.security.KeyStore) DnieProvider(es.gob.jmulticard.jse.provider.DnieProvider) Provider(java.security.Provider)

Example 5 with DnieProvider

use of es.gob.jmulticard.jse.provider.DnieProvider in project jmulticard by ctt-gob-es.

the class TestKeyStoreBuilder method testKeyStoreBuilderCreation.

/**
 * Prueba de construcci&oacute;n de KeyStore mediante <code>KeyStore.Builder</code>.
 * @throws Exception En cualquier error.
 */
@SuppressWarnings("static-method")
@Test
@Ignore
public void testKeyStoreBuilderCreation() throws Exception {
    final KeyStore.Builder builder = KeyStore.Builder.newInstance(// $NON-NLS-1$
    "DNI", new DnieProvider(), new KeyStore.CallbackHandlerProtection(// $NON-NLS-1$ //$NON-NLS-2$
    new TestingDnieCallbackHandler("can", "pin")));
    final KeyStore ks = builder.getKeyStore();
    // $NON-NLS-1$
    System.out.println("Numero de certificados: " + ks.size());
    System.out.println(ks.getCertificate(ks.aliases().nextElement()));
}
Also used : DnieProvider(es.gob.jmulticard.jse.provider.DnieProvider) KeyStore(java.security.KeyStore) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

DnieProvider (es.gob.jmulticard.jse.provider.DnieProvider)5 KeyStore (java.security.KeyStore)5 Provider (java.security.Provider)4 Signature (java.security.Signature)3 SmartcardIoConnection (es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection)2 X509Certificate (java.security.cert.X509Certificate)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 LoadStoreParameter (java.security.KeyStore.LoadStoreParameter)1 Certificate (java.security.cert.Certificate)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1