use of es.gob.jmulticard.asn1.der.pkcs15.Cdf in project jmulticard by ctt-gob-es.
the class Dnie method preloadCertificates.
/**
* Carga el certificado de la CA intermedia y las localizaciones de los
* certificados de firma y autenticación.
* @throws ApduConnectionException Si hay problemas en la precarga.
*/
protected void preloadCertificates() throws ApduConnectionException {
final Cdf cdf = new Cdf();
try {
selectMasterFile();
final byte[] cdfBytes = selectFileByLocationAndRead(CDF_LOCATION);
cdf.setDerValue(cdfBytes);
} catch (final Exception e) {
throw new ApduConnectionException(// $NON-NLS-1$
"No se ha podido cargar el CDF de la tarjeta: " + e.toString(), // $NON-NLS-1$
e);
}
for (int i = 0; i < cdf.getCertificateCount(); i++) {
final String currentAlias = cdf.getCertificateAlias(i);
if (CERT_ALIAS_AUTH.equals(currentAlias)) {
this.authCertPath = new Location(cdf.getCertificatePath(i));
} else if (CERT_ALIAS_SIGN.equals(currentAlias)) {
this.signCertPath = new Location(cdf.getCertificatePath(i));
} else if (CERT_ALIAS_CYPHER.equals(currentAlias)) {
this.cyphCertPath = new Location(cdf.getCertificatePath(i));
} else if (CERT_ALIAS_INTERMEDIATE_CA.equals(currentAlias)) {
try {
final byte[] intermediateCaCertEncoded = selectFileByLocationAndRead(new Location(cdf.getCertificatePath(i)));
this.intermediateCaCert = CompressionUtils.getCertificateFromCompressedOrNotData(intermediateCaCertEncoded);
} catch (final Exception e) {
LOGGER.warning(// $NON-NLS-1$
"No se ha podido cargar el certificado de la autoridad intermedia del CNP: " + e);
this.intermediateCaCert = null;
}
} else {
this.signAliasCertPath = new Location(cdf.getCertificatePath(i));
}
}
}
use of es.gob.jmulticard.asn1.der.pkcs15.Cdf 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: " + e, // $NON-NLS-1$
e);
}
final CertificateFactory cf;
try {
// $NON-NLS-1$
cf = CertificateFactory.getInstance("X.509");
} catch (final CertificateException e) {
// $NON-NLS-1$
throw new IOException("Error obteniendo la factoria de certificados X.509: " + e, 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) {
System.out.println(// $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) {
certBytes = readBinaryComplete(fileLength);
} else {
// 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;
}
CERTS_BY_ALIAS.put(cdf.getCertificateAlias(i), (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(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 es.gob.jmulticard.asn1.der.pkcs15.Cdf 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 Exception e) {
// $NON-NLS-1$
throw new IOException("Error en la lectura del CDF: " + e, e);
}
final CertificateFactory cf;
try {
// $NON-NLS-1$
cf = CertificateFactory.getInstance("X.509");
} catch (final CertificateException e) {
// $NON-NLS-1$
throw new IOException("Error obteniendo la factoria de certificados X.509: " + e, e);
}
for (int i = 0; i < cdf.getCertificateCount(); i++) {
try {
certificatesByAlias.put(cdf.getCertificateAlias(i), (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(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, e);
}
}
}
use of es.gob.jmulticard.asn1.der.pkcs15.Cdf in project jmulticard by ctt-gob-es.
the class TestCdfCreation method testSampleCdf.
/**
* Prueba de CDF de ejemplo (extraído de una tarjeta GyD de ACCV).
* @throws Exception En cualquier error.
*/
@Test
public static void testSampleCdf() throws Exception {
final Cdf cdf = new Cdf();
Assert.assertNotNull(cdf);
cdf.setDerValue(SAMPLE_CDF);
// $NON-NLS-1$
LOGGER.info("\n" + cdf.toString());
}
use of es.gob.jmulticard.asn1.der.pkcs15.Cdf in project jmulticard by ctt-gob-es.
the class TestCdfCreation method testCdf.
/**
* Prueba la creación de CDF con volcados en disco.
* @throws Exception En caso de cualquier tipo de error
*/
@Test
public static void testCdf() throws Exception {
byte[] cdfdata;
for (final String element : CDF_TEST_FILES) {
cdfdata = getDataFromInputStream(ClassLoader.getSystemResourceAsStream(element));
// $NON-NLS-1$ //$NON-NLS-2$
LOGGER.info("\n\nCDF completo (" + Integer.toString(cdfdata.length) + "):");
LOGGER.info(HexUtils.hexify(cdfdata, true));
final Cdf cdf = new Cdf();
Assert.assertNotNull(cdf);
cdf.setDerValue(cdfdata);
// $NON-NLS-1$
LOGGER.info("\n" + cdf.toString());
}
}
Aggregations