Пример #1
0
  /* (non-Javadoc)
   * @see org.opensc.pkcs15.token.Token#selectEF(int)
   */
  @Override
  public EF selectEF(int path) throws IOException {

    if (this.currentFile == null) throw new IOException("No current DF selected.");

    // SELECT FILE, P1=0x02, P2=0x00, no data -> select EF
    CommandAPDU cmd =
        new CommandAPDU(0x00, 0xA4, 0x02, 0x00, PathHelper.idToPath(path), DEFAULT_LE);

    try {
      ResponseAPDU resp = this.channel.transmit(cmd);

      DataInputStream dis = getSelectFileData(resp);

      long fileSize = 0;
      int acRead = TokenFileAcl.AC_ALWAYS;
      int acUpdate = TokenFileAcl.AC_ALWAYS;
      int acAppend = TokenFileAcl.AC_ALWAYS;
      int acDeactivate = TokenFileAcl.AC_ALWAYS;
      int acActivate = TokenFileAcl.AC_ALWAYS;
      int acDelete = TokenFileAcl.AC_ALWAYS;
      int acAdmin = TokenFileAcl.AC_ALWAYS;
      int acIncrease = TokenFileAcl.AC_ALWAYS;
      int acDecrease = TokenFileAcl.AC_ALWAYS;

      int tag;

      while ((tag = dis.read()) >= 0) {
        int n = dis.read();
        if (n < 0) break;

        switch (tag) {
          case 0x80:
            if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x80.");
            fileSize = dis.readUnsignedShort();
            break;

          case 0x83:
            if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x83.");
            int tpath = dis.readUnsignedShort();
            if (tpath != path)
              throw new IOException(
                  "File ID ["
                      + PathHelper.formatID(tpath)
                      + "] reported by SELECT FILE differs from requested ID ["
                      + PathHelper.formatID(path)
                      + "].");
            break;

          case 0x86:
            if (n >= 1) acRead = dis.read();
            if (n >= 2) acUpdate = dis.read();
            if (n >= 3) acAppend = dis.read();
            if (n >= 4) acDeactivate = dis.read();
            if (n >= 5) acActivate = dis.read();
            if (n >= 6) acDelete = dis.read();
            if (n >= 7) acAdmin = dis.read();
            if (n >= 8) acIncrease = dis.read();
            if (n >= 9) acDecrease = dis.read();

            if (n != 9) log.warn("Invalid length [" + n + "] of FCI tag 0x86 for EF.");

            if (n > 9) dis.skipBytes(n - 9);
            break;

          default:
            byte[] tmp = new byte[n];
            dis.readFully(tmp);
            log.warn(
                "skipping FCI tag [0x"
                    + Integer.toHexString(tag)
                    + "], data ["
                    + Util.asHex(tmp)
                    + "].");
        }
      }

      EF ef =
          new EF(
              new TokenPath(this.currentFile.getPath(), path),
              fileSize,
              acRead,
              acUpdate,
              acAppend,
              acDeactivate,
              acActivate,
              acDelete,
              acAdmin,
              acIncrease,
              acDecrease);

      this.currentFile = ef;
      return ef;

    } catch (CardException e) {
      throw new PKCS15Exception("Error sending select MF", e);
    }
  }
Пример #2
0
  /* (non-Javadoc)
   * @see org.opensc.pkcs15.token.Token#selectMF()
   */
  @Override
  public MF selectMF() throws IOException {

    // SELECT FILE, P1=0x00, P2=0x00, no data -> select MF
    CommandAPDU cmd = new CommandAPDU(0x00, 0xA4, 0x00, 0x00, DEFAULT_LE);

    try {
      ResponseAPDU resp = this.channel.transmit(cmd);

      DataInputStream dis = getSelectFileData(resp);

      long bodySize = 0;
      int acLifeCycle = TokenFileAcl.AC_ALWAYS;
      int acUpdate = TokenFileAcl.AC_ALWAYS;
      int acAppend = TokenFileAcl.AC_ALWAYS;
      int acDeactivate = TokenFileAcl.AC_ALWAYS;
      int acActivate = TokenFileAcl.AC_ALWAYS;
      int acDelete = TokenFileAcl.AC_ALWAYS;
      int acAdmin = TokenFileAcl.AC_ALWAYS;
      int acCreate = TokenFileAcl.AC_ALWAYS;
      int acExecute = TokenFileAcl.AC_ALWAYS;
      int acAllocate = TokenFileAcl.AC_ALWAYS;

      int tag;

      while ((tag = dis.read()) >= 0) {
        int n = dis.read();
        if (n < 0) break;

        switch (tag) {
          case 0x81:
            if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x81.");
            bodySize = dis.readUnsignedShort();
            break;

          case 0x83:
            if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x83.");
            int tpath = dis.readUnsignedShort();
            if (tpath != PathHelper.MF_ID)
              throw new IOException(
                  "File ID ["
                      + PathHelper.formatID(tpath)
                      + "] reported by SELECT FILE differs from requested ID ["
                      + PathHelper.formatID(PathHelper.MF_ID)
                      + "].");
            break;

          case 0x86:
            if (n >= 1) acLifeCycle = dis.read();
            if (n >= 2) acUpdate = dis.read();
            if (n >= 3) acAppend = dis.read();
            if (n >= 4) acDeactivate = dis.read();
            if (n >= 5) acActivate = dis.read();
            if (n >= 6) acDelete = dis.read();
            if (n >= 7) acAdmin = dis.read();
            if (n >= 8) acCreate = dis.read();
            if (n >= 9) acExecute = dis.read();
            if (n >= 10) acAllocate = dis.read();

            if (n != 10) log.warn("Invalid length [" + n + "] of FCI tag 0x86 for MF.");

            if (n > 10) dis.skipBytes(n - 10);
            break;

          default:
            byte[] tmp = new byte[n];
            dis.readFully(tmp);
            log.warn(
                "skipping FCI tag [0x"
                    + Integer.toHexString(tag)
                    + "], data ["
                    + Util.asHex(tmp)
                    + "].");
        }
      }

      MF mf =
          new MF(
              PathHelper.MF_PATH,
              bodySize,
              acLifeCycle,
              acUpdate,
              acAppend,
              acDeactivate,
              acActivate,
              acDelete,
              acAdmin,
              acCreate,
              acExecute,
              acAllocate);

      this.currentFile = mf;
      return mf;

    } catch (CardException e) {
      throw new PKCS15Exception("Error sending select MF", e);
    }
  }
Пример #3
0
  private DF selectDFInternal(CommandAPDU cmd, TokenPath targetPath) throws IOException {

    try {
      ResponseAPDU resp = this.channel.transmit(cmd);

      DataInputStream dis = getSelectFileData(resp);

      long bodySize = 0;
      int acLifeCycle = TokenFileAcl.AC_ALWAYS;
      int acUpdate = TokenFileAcl.AC_ALWAYS;
      int acAppend = TokenFileAcl.AC_ALWAYS;
      int acDeactivate = TokenFileAcl.AC_ALWAYS;
      int acActivate = TokenFileAcl.AC_ALWAYS;
      int acDelete = TokenFileAcl.AC_ALWAYS;
      int acAdmin = TokenFileAcl.AC_ALWAYS;
      int acCreate = TokenFileAcl.AC_ALWAYS;

      int tag;

      while ((tag = dis.read()) >= 0) {
        int n = dis.read();
        if (n < 0) break;

        switch (tag) {
          case 0x81:
            if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x81.");
            bodySize = dis.readUnsignedShort();
            break;

          case 0x83:
            if (n != 2) throw new IOException("Invalid length [" + n + "] of FCI tag 0x83.");
            int tpath = dis.readUnsignedShort();
            if (tpath != targetPath.getTailID())
              throw new IOException(
                  "File ID ["
                      + PathHelper.formatID(tpath)
                      + "] reported by SELECT FILE differs from requested ID ["
                      + PathHelper.formatID(targetPath.getTailID())
                      + "].");
            break;

          case 0x86:
            if (n >= 1) acLifeCycle = dis.read();
            if (n >= 2) acUpdate = dis.read();
            if (n >= 3) acAppend = dis.read();
            if (n >= 4) acDeactivate = dis.read();
            if (n >= 5) acActivate = dis.read();
            if (n >= 6) acDelete = dis.read();
            if (n >= 7) acAdmin = dis.read();
            if (n >= 8) acCreate = dis.read();

            if (n != 8) log.warn("Invalid length [" + n + "] of FCI tag 0x86 for DF.");

            if (n > 8) dis.skipBytes(n - 8);
            break;

          default:
            byte[] tmp = new byte[n];
            dis.readFully(tmp);
            log.warn(
                "skipping FCI tag [0x"
                    + Integer.toHexString(tag)
                    + "], data ["
                    + Util.asHex(tmp)
                    + "].");
        }
      }

      DF df =
          new DF(
              targetPath,
              bodySize,
              acLifeCycle,
              acUpdate,
              acAppend,
              acDeactivate,
              acActivate,
              acDelete,
              acAdmin,
              acCreate);

      this.currentFile = df;
      return df;

    } catch (CardException e) {
      throw new PKCS15Exception("Error sending select MF", e);
    }
  }