use of es.gob.jmulticard.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
final PrKdf prkdf = new PrKdf();
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();
}
use of es.gob.jmulticard.asn1.Asn1Exception in project jmulticard by ctt-gob-es.
the class CardOS method preloadCertificates.
private void preloadCertificates() throws FileNotFoundException, Iso7816FourCardException, IOException, Asn1Exception, TlvException {
// Entramos en el directorio PKCS#15
selectFileByName(PKCS15_NAME);
// Seleccionamos el ODF, no nos devuelve FCI ni nada
selectFileById(new byte[] { (byte) 0x50, (byte) 0x31 });
// Leemos el ODF, que tiene esta estructura en cada uno de sus registros:
// PKCS15Objects ::= CHOICE {
// privateKeys [0] PrivateKeys,
// publicKeys [1] PublicKeys,
// trustedPublicKeys [2] PublicKeys,
// secretKeys [3] SecretKeys,
// certificates [4] Certificates,
// trustedCertificates [5] Certificates,
// usefulCertificates [6] Certificates,
// dataObjects [7] DataObjects,
// authObjects [8] AuthObjects,
// ... -- For future extensions
// }
// A2
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();
// Seleccionamos el CDF
selectFileById(cdfPath.getPathBytes());
// Leemos el CDF mediante registros
final List<byte[]> cdfRecords = readAllRecords();
final CertificateFactory cf;
try {
// $NON-NLS-1$
cf = CertificateFactory.getInstance("X.509");
} catch (final CertificateException e1) {
throw new IllegalStateException(// $NON-NLS-1$
"No se ha podido obtener la factoria de certificados X.509: " + e1, // $NON-NLS-1$
e1);
}
CertificateObject co;
for (final byte[] b : cdfRecords) {
try {
co = new CertificateObject();
co.setDerValue(HexUtils.subArray(b, 2, b.length - 2));
} catch (final Exception e) {
// $NON-NLS-1$
LOGGER.warning("Omitido registro de certificado por no ser un CertificateObject de PKCS#15: " + e);
continue;
}
final byte[] certPath = co.getPathBytes();
if (certPath == null || certPath.length != 4) {
// $NON-NLS-1$
LOGGER.warning("Se omite una posicion de certificado porque su ruta no es de cuatro octetos: " + co.getAlias());
continue;
}
final byte[] MASTER_FILE = new byte[] { (byte) 0x50, (byte) 0x15 };
sendArbitraryApdu(new CommandApdu(// CLA
getCla(), // INS
(byte) 0xA4, // P1
(byte) 0x08, // P2
(byte) 0x0C, new byte[] { MASTER_FILE[0], MASTER_FILE[1], certPath[0], certPath[1], certPath[2], certPath[3] }, null));
final byte[] certBytes = readBinaryComplete(9999);
final X509Certificate cert;
try {
cert = (X509Certificate) cf.generateCertificate(new ByteArrayInputStream(certBytes));
} catch (final CertificateException e) {
LOGGER.severe(// $NON-NLS-1$ //$NON-NLS-2$
"No ha sido posible generar el certificado para el alias " + co.getAlias() + ": " + e);
continue;
}
certificatesByAlias.put(co.getAlias(), cert);
}
}
use of es.gob.jmulticard.asn1.Asn1Exception in project jmulticard by ctt-gob-es.
the class DerBoolean method decodeValue.
@Override
protected void decodeValue() throws Asn1Exception, TlvException {
final Tlv tmpTlv = new Tlv(this.getRawDerValue());
checkTag(tmpTlv.getTag());
this.booleanValue = Boolean.valueOf(tmpTlv.getValue()[0] == (byte) 0x00);
}
use of es.gob.jmulticard.asn1.Asn1Exception in project jmulticard by ctt-gob-es.
the class Record method decodeValue.
@Override
protected void decodeValue() throws Asn1Exception, TlvException {
if (getRawDerValue().length == 0) {
// $NON-NLS-1$
throw new Asn1Exception("El valor del objeto ASN.1 esta vacio");
}
int offset = 0;
Tlv tlv;
byte[] remainingBytes;
DecoderObject tmpDo;
for (int i = 0; i < this.elementsTypes.length; i++) {
try {
remainingBytes = new byte[getRawDerValue().length - offset];
System.arraycopy(getRawDerValue(), offset, remainingBytes, 0, remainingBytes.length);
tlv = new Tlv(remainingBytes);
try {
tmpDo = this.elementsTypes[i].getElementType().getConstructor().newInstance();
} catch (final Exception e) {
throw new Asn1Exception(// $NON-NLS-1$
"No se ha podido instanciar un " + this.elementsTypes[i].getElementType().getName() + " en la posicion " + Integer.toString(i) + " del registro: " + // $NON-NLS-1$ //$NON-NLS-2$
e, // $NON-NLS-1$ //$NON-NLS-2$
e);
}
tmpDo.checkTag(tlv.getTag());
} catch (final Exception e) {
if (this.elementsTypes[i].isOptional()) {
// Como no ha avanzado el offset, se reutilizara el tipo en el proximo elemento
continue;
}
// $NON-NLS-1$ //$NON-NLS-2$
throw new Asn1Exception("Error en el elemento " + i + " del registro ASN.1: " + e, e);
}
offset = offset + tlv.getBytes().length;
tmpDo.setDerValue(tlv.getBytes());
this.elements.add(tmpDo);
}
}
use of es.gob.jmulticard.asn1.Asn1Exception in project jmulticard by ctt-gob-es.
the class Utf8String method decodeValue.
@Override
protected void decodeValue() throws Asn1Exception, TlvException {
final Tlv tlv = new Tlv(this.getRawDerValue());
checkTag(tlv.getTag());
this.stringValue = new String(tlv.getValue(), StandardCharsets.UTF_8);
}
Aggregations