use of es.gob.jmulticard.card.CryptoCardException 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));
}
use of es.gob.jmulticard.card.CryptoCardException in project jmulticard by ctt-gob-es.
the class CeresSc method openSecureChannelIfNotAlreadyOpened.
/**
* Establece y abre el canal seguro CWA-14890 si no lo estaba ya hecho.
* @throws CryptoCardException Si hay problemas en el proceso.
* @throws PinException Si el PIN usado para la apertura de canal no es válido o no se ha proporcionado
* un PIN para validar.
*/
@Override
protected void openSecureChannelIfNotAlreadyOpened() throws CryptoCardException, PinException {
// Abrimos el canal seguro si no lo esta ya
if (!isSecurityChannelOpen()) {
// Aunque el canal seguro estuviese cerrado, podria si estar enganchado
if (!(getConnection() instanceof Cwa14890Connection)) {
final ApduConnection secureConnection;
secureConnection = new Cwa14890OneV2Connection(this, getConnection(), this.cryptoHelper, getCwa14890PublicConstants(), getCwa14890PrivateConstants());
try {
setConnection(secureConnection);
} catch (final ApduConnectionException e) {
// $NON-NLS-1$
throw new CryptoCardException("Error en el establecimiento del canal seguro: " + e, e);
}
}
try {
verifyPin(getInternalPasswordCallback());
} catch (final ApduConnectionException e) {
// $NON-NLS-1$
throw new CryptoCardException("Error en la apertura del canal seguro: " + e, e);
}
}
}
use of es.gob.jmulticard.card.CryptoCardException in project jmulticard by ctt-gob-es.
the class Dnie method sign.
/**
* {@inheritDoc}
*/
@Override
public byte[] sign(final byte[] data, final String signAlgorithm, final PrivateKeyReference privateKeyReference) throws CryptoCardException, PinException {
final byte[] ret = signInternal(data, signAlgorithm, privateKeyReference);
// el PIN y rehacer los canales CWA
try {
this.rawConnection.reset();
setConnection(this.rawConnection);
} catch (final ApduConnectionException e) {
throw new CryptoCardException(// $NON-NLS-1$
"Error en el establecimiento del canal inicial previo al seguro de PIN: " + e, // $NON-NLS-1$
e);
}
return ret;
}
use of es.gob.jmulticard.card.CryptoCardException in project jmulticard by ctt-gob-es.
the class Dnie method loadCertificatesInternal.
protected void loadCertificatesInternal() throws CryptoCardException {
// Cargamos certificados si es necesario
if (this.authCert == null || this.signCert == null || this.cyphCert == null && this.cyphCertPath != null || this.signAliasCert == null && this.signAliasCertPath != null) {
try {
this.signCert = loadCertificate(this.signCertPath);
this.authCert = loadCertificate(this.authCertPath);
if (this.cyphCertPath != null) {
this.cyphCert = loadCertificate(this.cyphCertPath);
}
if (this.signAliasCertPath != null) {
this.signAliasCert = loadCertificate(this.signAliasCertPath);
}
} catch (final CertificateException e) {
throw new CryptoCardException(// $NON-NLS-1$
"Error al cargar los certificados del DNIe, no es posible obtener una factoria de certificados X.509: " + e, // $NON-NLS-1$
e);
} catch (final IOException e) {
throw new CryptoCardException(// $NON-NLS-1$
"Error al cargar los certificados del DNIe, error en la descompresion de los datos: " + e, // $NON-NLS-1$
e);
} catch (final Iso7816FourCardException e) {
throw new CryptoCardException(// $NON-NLS-1$
"Error al cargar los certificados del DNIe: " + e, // $NON-NLS-1$
e);
}
}
}
use of es.gob.jmulticard.card.CryptoCardException in project jmulticard by ctt-gob-es.
the class Dnie method signOperation.
/**
* Realiza la operación de firma.
* @param data Datos que se desean firmar.
* @param signAlgorithm Algoritmo de firma (por ejemplo, <code>SHA512withRSA</code>, <code>SHA1withRSA</code>, etc.).
* @param privateKeyReference Referencia a la clave privada para la firma.
* @return Firma de los datos.
* @throws CryptoCardException Cuando se produce un error durante la operación de firma.
* @throws PinException Si el PIN proporcionado en la <i>PasswordCallback</i>
* es incorrecto y no estaba habilitado el reintento automático.
* @throws es.gob.jmulticard.card.AuthenticationModeLockedException Cuando el DNIe está bloqueado.
*/
protected byte[] signOperation(final byte[] data, final String signAlgorithm, final PrivateKeyReference privateKeyReference) throws CryptoCardException, PinException {
openSecureChannelIfNotAlreadyOpened();
ResponseApdu res;
try {
CommandApdu apdu = new MseSetComputationApduCommand((byte) 0x00, ((DniePrivateKeyReference) privateKeyReference).getKeyPath().getLastFilePath(), null);
res = getConnection().transmit(apdu);
if (!res.isOk()) {
throw new DnieCardException(// $NON-NLS-1$
"Error en el establecimiento de las clave de firma con respuesta: " + res.getStatusWord(), // $NON-NLS-1$
res.getStatusWord());
}
final byte[] digestInfo;
try {
digestInfo = DigestInfo.encode(signAlgorithm, data, this.cryptoHelper);
} catch (final IOException e) {
// $NON-NLS-1$
throw new DnieCardException("Error en el calculo de la huella para firmar: " + e, e);
}
apdu = new PsoSignHashApduCommand((byte) 0x00, digestInfo);
res = getConnection().transmit(apdu);
if (!res.isOk()) {
throw new DnieCardException(// $NON-NLS-1$
"Error durante la operacion de firma con respuesta: " + res.getStatusWord(), res.getStatusWord());
}
} catch (final LostChannelException e) {
try {
getConnection().close();
if (getConnection() instanceof Cwa14890Connection) {
setConnection(((Cwa14890Connection) getConnection()).getSubConnection());
}
} catch (final Exception ex) {
// $NON-NLS-1$
throw new DnieCardException("No se pudo recuperar el canal seguro para firmar: " + ex, ex);
}
return signOperation(data, signAlgorithm, privateKeyReference);
} catch (final ApduConnectionException e) {
// $NON-NLS-1$
throw new DnieCardException("Error en la transmision de comandos a la tarjeta: " + e, e);
}
return res.getData();
}
Aggregations