예제 #1
0
  private void getByteKeyAttr(Template t, String attr) {
    Template s = null;
    boolean noEx = true;
    Method m;
    byte[] v;

    try {
      s = new Template();
    } catch (BeeException e) {
      try {
        m = t.getClass().getMethod("set" + attr + "Exception", e.getClass());
        m.invoke(t, e);
      } catch (Exception ex) {
        return;
      }
    }

    try {
      m = s.getClass().getMethod("add" + attr);
      m.invoke(s);
    } catch (Exception e) {
      return;
    }

    try {
      lib.bee_get_attrs(bee, handle, s.getPointer());
      Errors.checkError();
    } catch (BeeException e) {
      noEx = false;
      try {
        m = s.getClass().getDeclaredMethod("set" + attr + "Exception", e.getClass());
        m.invoke(t, e);
      } catch (Exception ex) {
        return;
      }
    }

    if (noEx) {
      try {
        m = s.getClass().getMethod("set" + attr, byte[].class);
        Method g = s.getClass().getMethod("get" + attr);
        v = (byte[]) g.invoke(s);
        m.invoke(t, v);
      } catch (Exception e) {
        return;
      }
    }
  }
예제 #2
0
 public void setTemplate(Template t) throws BeeException {
   lib.bee_reset_error();
   lib.bee_set_attrs(bee, handle, t.getPointer());
   Errors.checkError();
 }
예제 #3
0
  public Template getTemplate() throws BeeException {
    Pkcs11Class c = null;
    boolean foundClass = true;
    boolean noEx = true;
    Template t = new Template();

    t.addToken();
    t.addPrivate();
    t.addModifiable();
    t.addLabel();
    t.addClass();
    t.addKeyType();
    t.addId();
    t.addDerive();
    t.addLocal();

    lib.bee_reset_error();
    lib.bee_get_attrs(bee, handle, t.getPointer());
    Errors.checkError();

    try {
      c = t.getObjClass();
    } catch (BeeException e) {
      foundClass = false;
    }

    if (foundClass) {
      Template keyAttrs = new Template();
      switch (c.getCl().intValue()) {
        case Pkcs11Class.SECRET_KEY:
          keyAttrs.addEncrypt();
          keyAttrs.addDecrypt();
          keyAttrs.addWrap();
          keyAttrs.addUnwrap();
          keyAttrs.addSign();
          keyAttrs.addVerify();
          keyAttrs.addNeverExtractable();
          keyAttrs.addExtractable();
          keyAttrs.addAlwaysSensitive();
          keyAttrs.addSensitive();

          getByteKeyAttr(t, "Value");
          break;
        case Pkcs11Class.PUBLIC_KEY:
          keyAttrs.addEncrypt();
          keyAttrs.addWrap();
          keyAttrs.addVerify();

          // CKA_MODULUS_BITS
          Template p = new Template();
          p.addModulusBits();
          try {
            lib.bee_get_attrs(bee, handle, p.getPointer());
            Errors.checkError();
          } catch (BeeException e) {
            noEx = false;
            t.setModulusBits(-1);
          }

          if (noEx) t.setModulusBits(p.getModulusBits());

          // CKA_MODULUS
          getByteKeyAttr(t, "Modulus");

          // CKA_PUBLIC_EXPONENT
          getByteKeyAttr(t, "PublicExponent");
          break;
        case Pkcs11Class.PRIVATE_KEY:
          keyAttrs.addDecrypt();
          keyAttrs.addUnwrap();
          keyAttrs.addSign();
          keyAttrs.addNeverExtractable();
          keyAttrs.addExtractable();
          keyAttrs.addAlwaysSensitive();
          keyAttrs.addSensitive();

          // CKA_MODULUS
          getByteKeyAttr(t, "Modulus");
          // CKA_PRIVATE_EXPONENT
          getByteKeyAttr(t, "PrivateExponent");
          break;
      }
      lib.bee_reset_error();
      lib.bee_get_attrs(bee, handle, keyAttrs.getPointer());
      Errors.checkError();
      t.merge(keyAttrs);
    }

    return t;
  }