Search in sources :

Example 1 with MseSetSignatureKeyApduCommand

use of es.gob.jmulticard.apdu.gemalto.MseSetSignatureKeyApduCommand in project jmulticard by ctt-gob-es.

the class TuiR5 method getPrivateKey.

@Override
public PrivateKeyReference getPrivateKey(final String alias) throws CryptoCardException {
    if (alias == null) {
        // $NON-NLS-1$
        throw new IllegalArgumentException("El alias no puede ser nulo");
    }
    if (!certificatesByAlias.containsKey(alias)) {
        // $NON-NLS-1$ //$NON-NLS-2$
        LOGGER.warning("La tarjeta no contiene el alias '" + alias + "', se devolvera null");
        return null;
    }
    final String[] aliases = getAliases();
    byte index = (byte) 0xff;
    for (int i = 0; i < aliases.length; i++) {
        if (alias.equals(aliases[i])) {
            index = (byte) i;
            break;
        }
    }
    if (index == (byte) 0xff) {
        // $NON-NLS-1$
        throw new IllegalStateException("La tarjeta no contiene el alias: " + alias);
    }
    final MseSetSignatureKeyApduCommand mseSet = new MseSetSignatureKeyApduCommand(CLA, MseSetSignatureKeyApduCommand.CryptographicMechanism.RSASSA_PKCS1v1_5_SHA1, index);
    final ResponseApdu res;
    try {
        res = sendArbitraryApdu(mseSet);
    } catch (final Exception e) {
        // $NON-NLS-1$
        throw new CryptoCardException("Error enviando la APDU de establecimiento de clave privada para firma: " + e, e);
    }
    if (res.isOk()) {
        return new TuiPrivateKeyReference(index);
    }
    throw new CryptoCardException(// $NON-NLS-1$
    "No se ha podido recuperar la referencia a la clave privada: " + HexUtils.hexify(res.getBytes(), true));
}
Also used : MseSetSignatureKeyApduCommand(es.gob.jmulticard.apdu.gemalto.MseSetSignatureKeyApduCommand) ResponseApdu(es.gob.jmulticard.apdu.ResponseApdu) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) ApduConnectionException(es.gob.jmulticard.apdu.connection.ApduConnectionException) FileNotFoundException(es.gob.jmulticard.card.iso7816four.FileNotFoundException) CryptoCardException(es.gob.jmulticard.card.CryptoCardException) InvalidCardException(es.gob.jmulticard.card.InvalidCardException) CardNotPresentException(es.gob.jmulticard.apdu.connection.CardNotPresentException) BadPinException(es.gob.jmulticard.card.BadPinException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoReadersFoundException(es.gob.jmulticard.apdu.connection.NoReadersFoundException) Iso7816FourCardException(es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)

Aggregations

ResponseApdu (es.gob.jmulticard.apdu.ResponseApdu)1 ApduConnectionException (es.gob.jmulticard.apdu.connection.ApduConnectionException)1 CardNotPresentException (es.gob.jmulticard.apdu.connection.CardNotPresentException)1 NoReadersFoundException (es.gob.jmulticard.apdu.connection.NoReadersFoundException)1 MseSetSignatureKeyApduCommand (es.gob.jmulticard.apdu.gemalto.MseSetSignatureKeyApduCommand)1 BadPinException (es.gob.jmulticard.card.BadPinException)1 CryptoCardException (es.gob.jmulticard.card.CryptoCardException)1 InvalidCardException (es.gob.jmulticard.card.InvalidCardException)1 FileNotFoundException (es.gob.jmulticard.card.iso7816four.FileNotFoundException)1 Iso7816FourCardException (es.gob.jmulticard.card.iso7816four.Iso7816FourCardException)1 IOException (java.io.IOException)1 CertificateException (java.security.cert.CertificateException)1