use of com.unboundid.asn1.ASN1Exception in project jmulticard by ctt-gob-es.
the class TestAsn1SimpleTypes method testBitStringCreationWithBadData.
/**
* Prueba la creació de un tipo <code>BitString</code> con datos incorrectos.
* @throws TlvException Si no se puede crear el TLV.
*/
@Test
@SuppressWarnings("static-method")
public void testBitStringCreationWithBadData() throws TlvException {
final BitString u = new AccessFlags();
try {
u.setDerValue(new byte[] { (byte) 0x00, (byte) 0x01, (byte) 0xff });
} catch (final Asn1Exception e) {
// $NON-NLS-1$
System.out.println("Fallo esperado: " + e);
return;
}
// $NON-NLS-1$
Assert.fail("Tendria que haber saltado un Asn1Exception");
}
use of com.unboundid.asn1.ASN1Exception in project jmulticard by ctt-gob-es.
the class Dnie3 method getCom.
@Override
public Com getCom() throws IOException {
try {
final Com com = new Com();
com.setDerValue(selectFileByLocationAndRead(FILE_COM_LOCATION));
return com;
} catch (final es.gob.jmulticard.card.iso7816four.FileNotFoundException e) {
// $NON-NLS-1$
throw (IOException) new FileNotFoundException("COM no encontrado").initCause(e);
} catch (final Iso7816FourCardException | TlvException | Asn1Exception e) {
// $NON-NLS-1$
throw new CryptoCardException("Error leyendo el 'Common Data' (COM)", e);
}
}
use of com.unboundid.asn1.ASN1Exception in project jmulticard by ctt-gob-es.
the class TuiR5 method preloadCertificates.
private void preloadCertificates() throws IOException, Iso7816FourCardException {
selectMasterFile();
final Cdf cdf = new Cdf();
try {
cdf.setDerValue(selectFileByLocationAndRead(CDF_LOCATION));
} catch (final Asn1Exception | TlvException e) {
// $NON-NLS-1$
throw new IOException("Error en la lectura del CDF", e);
}
for (int i = 0; i < cdf.getCertificateCount(); i++) {
try {
CERTIFICATES_BY_ALIAS.put(cdf.getCertificateAlias(i), CertificateUtils.generateCertificate(selectFileByLocationAndRead(new Location(cdf.getCertificatePath(i)))));
} catch (final CertificateException e) {
// $NON-NLS-1$ //$NON-NLS-2$
throw new IOException("Error en la lectura del certificado " + i + " del dispositivo", e);
}
}
}
use of com.unboundid.asn1.ASN1Exception in project jmulticard by ctt-gob-es.
the class SmartCafePkcs15Applet method preloadCertificates.
private void preloadCertificates() throws FileNotFoundException, Iso7816FourCardException, IOException, Asn1Exception, TlvException {
selectMasterFile();
// Seleccionamos el ODF, no nos devuelve FCI ni nada
selectFileById(ODF_PATH);
// Leemos el ODF
final byte[] odfBytes = readBinaryComplete(162);
final Odf odf = new Odf();
odf.setDerValue(odfBytes);
// Sacamos del ODF la ruta del CDF
final Path cdfPath = odf.getCdfPath();
// Leemos el CDF
final Cdf cdf = new Cdf();
try {
selectMasterFile();
final byte[] cdfBytes = selectFileByIdAndRead(cdfPath.getPathBytes());
cdf.setDerValue(cdfBytes);
} catch (final Exception e) {
throw new ApduConnectionException(// $NON-NLS-1$
"No se ha podido cargar el CDF de la tarjeta", // $NON-NLS-1$
e);
}
if (cdf.getCertificateCount() < 1) {
// $NON-NLS-1$
LOGGER.warning("La tarjeta no contiene ningun certificado");
}
for (int i = 0; i < cdf.getCertificateCount(); i++) {
try {
int fileLength = -1;
Location certLocation = new Location(cdf.getCertificatePath(i));
while (certLocation != null) {
final byte[] id = certLocation.getFile();
try {
fileLength = selectFileById(id);
} catch (final FileNotFoundException e) {
LOGGER.warning(// $NON-NLS-1$//$NON-NLS-2$
"El CDF indicaba un certificado en la ruta '" + certLocation + "', pero un elemento de esta no existe, se ignorara: " + e);
}
certLocation = certLocation.getChild();
}
final byte[] certBytes;
if (fileLength <= 0) {
// A veces hay punteros que apuntan a localizaciones vacias
LOGGER.warning(// $NON-NLS-1$ //$NON-NLS-2$
"El certificado " + i + " del dispositivo esta vacio");
continue;
}
certBytes = readBinaryComplete(fileLength);
CERTS_BY_ALIAS.put(cdf.getCertificateAlias(i), CertificateUtils.generateCertificate(certBytes));
} catch (final Exception e) {
// Puede darse el caso de que el puntero apunte a algo que no es un certificado
LOGGER.severe(// $NON-NLS-1$ //$NON-NLS-2$
"Error en la lectura del certificado " + i + " del dispositivo: " + e);
continue;
}
}
}
use of com.unboundid.asn1.ASN1Exception in project jmulticard by ctt-gob-es.
the class CeresSc method preload.
private void preload() throws ApduConnectionException, Iso7816FourCardException, IOException, CertificateException, Asn1Exception, TlvException {
// Nos vamos al raiz antes de nada
selectMasterFile();
// Leemos el CDF
final byte[] cdfBytes = selectFileByLocationAndRead(CDF_LOCATION);
// Cargamos el CDF
final Pkcs15Cdf cdf = new Cdf();
cdf.setDerValue(cdfBytes);
this.certs = new LinkedHashMap<>(cdf.getCertificateCount());
this.aliasByCertAndKeyId = new LinkedHashMap<>(cdf.getCertificateCount());
for (int i = 0; i < cdf.getCertificateCount(); i++) {
final Location l = new Location(// $NON-NLS-1$ //$NON-NLS-2$
cdf.getCertificatePath(i).replace("\\", "").trim());
final X509Certificate cert = CompressionUtils.getCertificateFromCompressedOrNotData(selectFileByLocationAndRead(l));
// $NON-NLS-1$
final String alias = i + " " + cert.getSerialNumber();
this.aliasByCertAndKeyId.put(HexUtils.hexify(cdf.getCertificateId(i), false), alias);
this.certs.put(alias, cert);
}
// Leemos el PrKDF
final byte[] prkdfValue = selectFileByLocationAndRead(PRKDF_LOCATION);
// Establecemos el valor del PrKDF
PrKdf prkdf;
try {
prkdf = new PrKdf();
prkdf.setDerValue(prkdfValue);
} catch (final Exception e) {
LOGGER.warning(// $NON-NLS-1$
"Detectado posible PrKDF con CommonPrivateKeyAttributes vacio, se prueba con estructura alternativa: " + e);
prkdf = new PrKdfCeres();
prkdf.setDerValue(prkdfValue);
}
this.keyReferences = new LinkedHashMap<>();
for (int i = 0; i < prkdf.getKeyCount(); i++) {
final String alias = this.aliasByCertAndKeyId.get(HexUtils.hexify(prkdf.getKeyId(i), false));
if (alias != null) {
this.keyReferences.put(alias, new DniePrivateKeyReference(this, prkdf.getKeyIdentifier(i), new Location(prkdf.getKeyPath(i)), prkdf.getKeyName(i), prkdf.getKeyReference(i), ((RSAPublicKey) this.certs.get(alias).getPublicKey()).getModulus().bitLength()));
}
}
// Sincronizamos claves y certificados
hideCertsWithoutKey();
}
Aggregations