コード例 #1
0
  private void preload()
      throws ApduConnectionException, Iso7816FourCardException, IOException, CertificateException,
          Asn1Exception, TlvException {

    // Nos vamos al raiz antes de nada
    selectMasterFile();

    // Cargamos el CDF
    final CeresCdf cdf = new CeresCdf();

    final byte[] cdfBytes = selectFileByLocationAndRead(CDF_LOCATION);

    cdf.setDerValue(cdfBytes);

    // Leemos los certificados segun las rutas del CDF

    final CertificateFactory cf = CertificateFactory.getInstance("X.509"); // $NON-NLS-1$

    this.certs = new LinkedHashMap<String, X509Certificate>(cdf.getCertificateCount());
    this.aliasByCertAndKeyId = new LinkedHashMap<String, String>(cdf.getCertificateCount());

    for (int i = 0; i < cdf.getCertificateCount(); i++) {
      final Location l =
          new Location(
              cdf.getCertificatePath(i).replace("\\", "").trim()); // $NON-NLS-1$ //$NON-NLS-2$
      final X509Certificate cert =
          (X509Certificate)
              cf.generateCertificate(
                  new ByteArrayInputStream(deflate(selectFileByLocationAndRead(l))));
      final String alias = i + " " + cert.getSerialNumber(); // $NON-NLS-1$
      this.aliasByCertAndKeyId.put(HexUtils.hexify(cdf.getCertificateId(i), false), alias);
      this.certs.put(alias, cert);
    }

    final CeresPrKdf prkdf = new CeresPrKdf();
    final byte[] prkdfValue = selectFileByLocationAndRead(PRKDF_LOCATION);

    prkdf.setDerValue(prkdfValue);

    this.keys = new LinkedHashMap<String, Byte>();
    for (int i = 0; i < prkdf.getKeyCount(); i++) {
      final String alias = this.aliasByCertAndKeyId.get(HexUtils.hexify(prkdf.getKeyId(i), false));
      if (alias != null) {
        this.keys.put(alias, Byte.valueOf(prkdf.getKeyReference(i)));
      }
    }

    // Sincronizamos claves y certificados
    hideCertsWithoutKey();
  }
コード例 #2
0
  /**
   * Prueba de generaci&oacute;n de claves de canal.
   *
   * @throws Exception en cualquier error.
   */
  @SuppressWarnings("static-method")
  @Test
  public void testKeysGeneration() throws Exception {

    // Calculamos Kifdicc como el XOR de los valores Kifd y Kicc
    final byte[] kidficc = HexUtils.xor(kicc, kifd);
    Assert.assertEquals(
        "820d590bc2c7aea5f12050374fbe381b76e779d6b57395e90167f657bec47475", //$NON-NLS-1$
        HexUtils.hexify(kidficc, false).toLowerCase());

    final byte[] kenc = generateKenc(kidficc);
    System.out.println("Kenc: " + HexUtils.hexify(kenc, false)); // $NON-NLS-1$

    final byte[] kmac = generateKmac(kidficc);
    System.out.println("Kmac: " + HexUtils.hexify(kmac, false)); // $NON-NLS-1$
  }
コード例 #3
0
  @Override
  public byte[] sign(final byte[] data, final String algorithm, final PrivateKeyReference keyRef)
      throws CryptoCardException, BadPinException {

    if (data == null) {
      throw new CryptoCardException("Los datos a firmar no pueden ser nulos"); // $NON-NLS-1$
    }

    if (keyRef == null) {
      throw new IllegalArgumentException("La clave privada no puede ser nula"); // $NON-NLS-1$
    }
    if (!(keyRef instanceof CeresPrivateKeyReference)) {
      throw new IllegalArgumentException(
          "La clave proporcionada debe ser de tipo CeresPrivateKeyReference, pero se ha recibido de tipo "
              + keyRef.getClass().getName() // $NON-NLS-1$
          );
    }
    final CeresPrivateKeyReference ceresPrivateKey = (CeresPrivateKeyReference) keyRef;

    // Pedimos el PIN si no se ha pedido antes
    if (!this.authenticated) {
      try {
        verifyPin(this.passwordCallback);
        this.authenticated = true;
      } catch (final ApduConnectionException e1) {
        throw new CryptoCardException("Error en la verificacion de PIN: " + e1, e1); // $NON-NLS-1$
      }
    }

    final byte[] digestInfo;
    try {
      digestInfo = DigestInfo.encode(algorithm, data, this.cryptoHelper);
    } catch (final Exception e) {
      throw new CryptoCardException(
          "Erros creando el DigestInfo para la firma con el algoritmo " + algorithm + ": " + e,
          e //$NON-NLS-1$ //$NON-NLS-2$
          );
    }

    loadData(ceresPrivateKey.getKeyBitSize(), digestInfo);

    final ResponseApdu res;

    final CommandApdu cmd =
        new SignDataApduCommand(
            ceresPrivateKey.getKeyReference(), // Referencia
            ceresPrivateKey.getKeyBitSize() // Tamano en bits de la clave
            );
    try {
      res = sendArbitraryApdu(cmd);
    } catch (final Exception e) {
      throw new CryptoCardException("Error firmando los datos: " + e, e); // $NON-NLS-1$
    }
    if (!res.isOk()) {
      throw new CryptoCardException(
          "No se han podido firmar los datos. Respuesta: "
              + HexUtils.hexify(res.getBytes(), true)); // $NON-NLS-1$
    }
    return res.getData();
  }
コード例 #4
0
  /**
   * Genera la clave KENC para encriptar y desencriptar criptogramas.
   *
   * @param kidficc XOR de los valores Kifd y Kicc.
   * @return Clave AES.
   * @throws IOException Cuando no puede generarse la clave.
   */
  private static byte[] generateKenc(final byte[] kidficc) throws IOException {
    // La clave de cifrado Kenc se obtiene como los 16 primeros bytes del hash de la
    // concatenacion de kifdicc con el valor "00 00 00 01" (SECURE_CHANNEL_KENC_AUX).
    final byte[] kidficcConcat = HexUtils.concatenateByteArrays(kidficc, SECURE_CHANNEL_KENC_AUX);

    System.out.println(
        "Datos para el hash de Kenc: " + HexUtils.hexify(kidficcConcat, false)); // $NON-NLS-1$

    final byte[] keyEnc = new byte[16];
    System.arraycopy(
        TestKeyGeneration.cryptoHelper.digest(CryptoHelper.DigestAlgorithm.SHA256, kidficcConcat),
        0,
        keyEnc,
        0,
        keyEnc.length);

    return keyEnc;
  }
コード例 #5
0
  /**
   * Genera la clave KMAC para calcular y verificar checksums.
   *
   * @param kidficc XOR de los valores Kifd y Kicc.
   * @return Clave AES.
   * @throws IOException Cuando no puede generarse la clave.
   */
  private static byte[] generateKmac(final byte[] kidficc) throws IOException {
    // La clave para el calculo del MAC Kmac se obtiene como los 16 primeros bytes
    // del hash de la concatenacion de kifdicc con el valor "00 00 00 02" (SECURE_CHANNEL_KMAC_AUX).
    final byte[] kidficcConcat = HexUtils.concatenateByteArrays(kidficc, SECURE_CHANNEL_KMAC_AUX);

    final byte[] keyMac = new byte[16];
    System.arraycopy(
        TestKeyGeneration.cryptoHelper.digest(CryptoHelper.DigestAlgorithm.SHA256, kidficcConcat),
        0,
        keyMac,
        0,
        keyMac.length);

    return keyMac;
  }
コード例 #6
0
  private void loadData(final int keyBitSize, final byte[] digestInfo) throws CryptoCardException {
    final byte[] paddedData;
    try {
      paddedData = CryptoHelper.addPkcs1PaddingForPrivateKeyOperation(digestInfo, keyBitSize);
    } catch (final Exception e1) {
      throw new CryptoCardException(
          "Error realizando el relleno PKCS#1 de los datos a firmar: " + e1, // $NON-NLS-1$
          e1);
    }

    ResponseApdu res;

    // Si la clave es de 1024 la carga se puede hacer en una unica APDU
    if (keyBitSize < 2048) {
      try {
        res = sendArbitraryApdu(new LoadDataApduCommand(paddedData));
      } catch (final Exception e) {
        throw new CryptoCardException(
            "Error enviando los datos a firmar a la tarjeta: " + e, e); // $NON-NLS-1$
      }
      if (!res.isOk()) {
        throw new CryptoCardException(
            "No se han podido enviar los datos a firmar a la tarjeta. Respuesta: "
                + HexUtils.hexify(res.getBytes(), true)); // $NON-NLS-1$
      }
    }
    // Pero si es de 2048 hacen falta dos APDU, envolviendo la APDU de carga de datos
    else if (keyBitSize == 2048) {

      final byte[] envelopedLoadDataApdu =
          new byte[] {
            (byte) 0x90,
            (byte) 0x58,
            (byte) 0x00,
            (byte) 0x00,
            (byte) 0x00,
            (byte) 0x01,
            (byte) 0x00
          };

      // La primera APDU carga 0xFF octetos (254)
      byte[] data = new byte[255];
      System.arraycopy(envelopedLoadDataApdu, 0, data, 0, envelopedLoadDataApdu.length);
      System.arraycopy(
          paddedData, 0, data, envelopedLoadDataApdu.length, 255 - envelopedLoadDataApdu.length);

      try {
        res = sendArbitraryApdu(new EnvelopeDataApduCommand(data));
      } catch (final Exception e) {
        throw new CryptoCardException(
            "Error en el segundo envio a la tarjeta de los datos a firmar: " + e, e); // $NON-NLS-1$
      }
      if (!res.isOk()) {
        throw new CryptoCardException(
            "No se han podido enviar (segunda tanda) los datos a firmar a la tarjeta. Respuesta: "
                + HexUtils.hexify(res.getBytes(), true) // $NON-NLS-1$
            );
      }

      // La segunda APDU es de 0x08 octetos (8)
      data = new byte[8];
      System.arraycopy(paddedData, 255 - envelopedLoadDataApdu.length, data, 0, 8);

      try {
        res = sendArbitraryApdu(new EnvelopeDataApduCommand(data));
      } catch (final Exception e) {
        throw new CryptoCardException(
            "Error en el primer envio a la tarjeta de los datos a firmar: " + e, e); // $NON-NLS-1$
      }
      if (!res.isOk()) {
        throw new CryptoCardException(
            "No se han podido enviar (primera tanda) los datos a firmar a la tarjeta. Respuesta: "
                + HexUtils.hexify(res.getBytes(), true) // $NON-NLS-1$
            );
      }

    } else {
      throw new IllegalArgumentException(
          "Solo se soportan claves de 2048 o menos bits"); //$NON-NLS-1$
    }
  }