Search in sources :

Example 11 with SmartcardIoConnection

use of es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection in project jmulticard by ctt-gob-es.

the class JMultiCardProviderFactory method getProvider.

/**
 * Obtiene el proveedor (con la conexión por defecto) correspondiente
 * a la primera tarjeta encontrada en el sistema.
 * @return Proveedor (con la conexión por defecto) correspondiente
 *         a la primera tarjeta encontrada insertada o <code>null</code> si
 *         no hay ninguna insertada, no ha lector de tarjetas o no se
 *         encuentra ninguna tarjeta soportada.
 */
public static Provider getProvider() {
    final ApduConnection conn = new SmartcardIoConnection();
    final long[] terminals;
    try {
        terminals = conn.getTerminals(false);
    } catch (final ApduConnectionException e1) {
        LOGGER.warning(// $NON-NLS-1$
        "No se ha podido obtener la lista de lectores de tarjetas del sistema: " + e1);
        return null;
    }
    byte[] atr = null;
    for (final long terminal : terminals) {
        conn.setTerminal((int) terminal);
        try {
            atr = conn.reset();
        } catch (final Exception e) {
            continue;
        }
    }
    return getProvider(atr);
}
Also used : SmartcardIoConnection(es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection) ApduConnection(es.gob.jmulticard.apdu.connection.ApduConnection) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException)

Example 12 with SmartcardIoConnection

use of es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection in project jmulticard by ctt-gob-es.

the class TestAccv method testReadCertificates.

/**
 * Prueba de lectura de certificados.
 * @throws Exception En cualquier error.
 */
@SuppressWarnings("static-method")
@Test
@Ignore
public void testReadCertificates() throws Exception {
    final CryptoCard card = new SmartCafePkcs15Applet(new SmartcardIoConnection(), new JseCryptoHelper());
    final String[] aliases = card.getAliases();
    if (aliases.length < 1) {
        // $NON-NLS-1$
        System.out.println("La tarjeta no tiene certificados");
        return;
    }
    final String selectedAlias = aliases[0];
    // $NON-NLS-1$
    System.out.println("Alias encontrados:");
    for (final String alias : aliases) {
        // $NON-NLS-1$
        System.out.println("  " + alias);
    }
    System.out.println();
    final X509Certificate c = card.getCertificate(selectedAlias);
    // $NON-NLS-1$
    System.out.println("Primer certificado encontrado: " + AOUtil.getCN(c));
}
Also used : SmartCafePkcs15Applet(es.gob.jmulticard.card.gide.smartcafe.SmartCafePkcs15Applet) CryptoCard(es.gob.jmulticard.card.CryptoCard) SmartcardIoConnection(es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection) X509Certificate(java.security.cert.X509Certificate) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with SmartcardIoConnection

use of es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection in project jmulticard by ctt-gob-es.

the class TestCeres method main.

/**
 * Main.
 * @param args No se usa.
 * @throws Exception En cualquier error.
 */
public static void main(final String[] args) throws Exception {
    final Ceres ceres = new Ceres(new SmartcardIoConnection(), new JseCryptoHelper());
    ceres.setPasswordCallback(new CachePasswordCallback(PIN.toCharArray()));
    System.out.println(ceres.getCardName());
    System.out.println(Arrays.asList(ceres.getAliases()));
    System.out.println(ceres.getCertificate(ceres.getAliases()[0]));
    final PrivateKeyReference pkr = ceres.getPrivateKey(ceres.getAliases()[0]);
    System.out.println(HexUtils.hexify(// $NON-NLS-1$//$NON-NLS-2$
    ceres.sign("hola".getBytes(), "SHA1withRSA", pkr), true));
}
Also used : Ceres(es.gob.jmulticard.card.fnmt.ceres.Ceres) PrivateKeyReference(es.gob.jmulticard.card.PrivateKeyReference) SmartcardIoConnection(es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection)

Example 14 with SmartcardIoConnection

use of es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection in project jmulticard by ctt-gob-es.

the class TestJseProviderCeres method testProviderWithCustomConnection.

static void testProviderWithCustomConnection() throws Exception {
    final Provider p = new CeresProvider(new SmartcardIoConnection());
    Security.addProvider(p);
    // $NON-NLS-1$
    final KeyStore ks = KeyStore.getInstance("CERES");
    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$
    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");
}
Also used : CeresProvider(es.gob.jmulticard.jse.provider.ceres.CeresProvider) Signature(java.security.Signature) SmartcardIoConnection(es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection) KeyStore(java.security.KeyStore) CeresProvider(es.gob.jmulticard.jse.provider.ceres.CeresProvider) Provider(java.security.Provider)

Aggregations

SmartcardIoConnection (es.gob.jmulticard.jse.smartcardio.SmartcardIoConnection)14 Ignore (org.junit.Ignore)5 Test (org.junit.Test)5 ApduConnection (es.gob.jmulticard.apdu.connection.ApduConnection)4 PrivateKeyReference (es.gob.jmulticard.card.PrivateKeyReference)3 SmartCafePkcs15Applet (es.gob.jmulticard.card.gide.smartcafe.SmartCafePkcs15Applet)3 KeyStore (java.security.KeyStore)3 Provider (java.security.Provider)3 Signature (java.security.Signature)3 Dnie (es.gob.jmulticard.card.dnie.Dnie)2 Ceres (es.gob.jmulticard.card.fnmt.ceres.Ceres)2 DnieProvider (es.gob.jmulticard.jse.provider.DnieProvider)2 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)1 CryptoCard (es.gob.jmulticard.card.CryptoCard)1 StCard (es.gob.jmulticard.card.bit4id.stcm.StCard)1 CardOS (es.gob.jmulticard.card.cardos.CardOS)1 TuiR5 (es.gob.jmulticard.card.gemalto.tuir5.TuiR5)1 Iso7816FourCard (es.gob.jmulticard.card.iso7816four.Iso7816FourCard)1 CeresProvider (es.gob.jmulticard.jse.provider.ceres.CeresProvider)1 LoadStoreParameter (java.security.KeyStore.LoadStoreParameter)1