예제 #1
0
  /**
   * Enumeraate all available slots of a given PKCS11 provider.
   *
   * @param provider The PKCS11 provider to retrieve the slots for.
   * @return A list of all available slots.
   * @throws PKCS11Exception Upon errors when retrieving the slot information.
   */
  public static List<PKCS11Slot> enumerateSlots(PKCS11Provider provider) throws PKCS11Exception {
    long[] ids = enumerateSlotsNative(provider.getPkcs11ModuleHandle());

    List<PKCS11Slot> ret = new ArrayList<PKCS11Slot>(ids.length);

    for (int i = 0; i < ids.length; i++) {
      ret.add(new PKCS11Slot(provider, ids[i]));
    }
    return ret;
  }
예제 #2
0
  /**
   * Enumerate all available slots of a given PKCS11 provider.
   *
   * @param provider The PKCS11 provider to retrieve the slots for.
   * @return A list of all available slots.
   * @throws PKCS11Exception Upon errors when retrieving the slot information.
   */
  public static PKCS11Slot waitForSlot(PKCS11Provider provider) throws PKCS11Exception {
    long id = -1;

    try {
      id = waitForSlotNative(provider.getPkcs11ModuleHandle());
    } catch (PKCS11Exception e) {
      if (e.getErrorCode() == PKCS11Exception.CKR_FUNCTION_NOT_SUPPORTED)
        try {
          PKCS11Slot ret = null;

          do {
            Thread.sleep(1000);

            List<PKCS11Slot> slots = enumerateSlots(provider);

            for (PKCS11Slot slot : slots) {
              if (ret == null && slot.isTokenPresent()) ret = slot;
              else
                try {
                  slot.destroy();
                } catch (DestroyFailedException e1) {
                  log.warn("destroy error while waiting for slot:", e1);
                }
            }
          } while (ret == null);

          return ret;

        } catch (InterruptedException e1) {
          throw new PKCS11Exception(
              PKCS11Exception.CKR_FUNCTION_CANCELED, "The operation has been interrupted.");
        }
      else {
        throw e;
      }
    }

    return new PKCS11Slot(provider, id);
  }
예제 #3
0
 /**
  * This contructor constructs an instance of an individual slot. The slots a usually label
  * starting with an Id of 0 and onwards. So, if you have just one device attached to your computer
  * you should usually done by calling new PKCS11Slot(provider,0).
  *
  * @param id The Id of the slot.
  * @throws PKCS11Exception Upon errors when retrieving the slot information.
  */
 public PKCS11Slot(PKCS11Provider provider, long id) throws PKCS11Exception {
   super(provider);
   this.id = id;
   this.pvh = provider.getPkcs11ModuleHandle();
   this.handle = initSlotNative(this.pvh, id);
 }