Esempio n. 1
0
  public void digestData() throws IOException, PKCS11Exception {
    byte[] buffer = new byte[1024];
    byte[] helpBuffer, testDigest;
    int bytesRead;

    System.out.println("Digest Data");
    myPKCS11Module_.C_DigestInit(session_, digestMechanism_);
    try {
      messageDigest_ = MessageDigest.getInstance("SHA-1");
    } catch (Exception e) {
      System.out.println(e);
    }
    InputStream dataInput = new FileInputStream(file_);
    while ((bytesRead = dataInput.read(buffer, 0, buffer.length)) >= 0) {
      helpBuffer =
          new byte[bytesRead]; // we need a buffer that only holds what to send for digesting
      System.arraycopy(buffer, 0, helpBuffer, 0, bytesRead);
      myPKCS11Module_.C_DigestUpdate(session_, helpBuffer);
      messageDigest_.update(helpBuffer);
      Arrays.fill(helpBuffer, (byte) 0);
    }
    Arrays.fill(buffer, (byte) 0);
    digest_ = myPKCS11Module_.C_DigestFinal(session_);
    testDigest = messageDigest_.digest();
    System.out.println("PKCS11digest:" + Functions.toHexString(digest_));
    System.out.println("TestDigest  :" + Functions.toHexString(testDigest));
    System.out.println("FINISHED\n");
  }
  /**
   * Returns the string representation of this object. Do not parse data from this string, it is for
   * debugging only.
   *
   * @return A string representation of this object.
   */
  public String toString() {
    StringBuffer buffer = new StringBuffer();

    buffer.append(Constants.INDENT);
    buffer.append("Key Derivation Function: ");
    if (keyDerivationFunction_ == KeyDerivationFunctionType.NULL) {
      buffer.append("NULL");
    } else if (keyDerivationFunction_ == KeyDerivationFunctionType.SHA1_KDF) {
      buffer.append("SHA1_KDF");
    } else if (keyDerivationFunction_ == KeyDerivationFunctionType.SHA1_KDF_ASN1) {
      buffer.append("SHA1_KDF_ASN1");
    } else if (keyDerivationFunction_ == KeyDerivationFunctionType.SHA1_KDF_CONCATENATE) {
      buffer.append("SHA1_KDF_CONCATENATE");
    } else {
      buffer.append("<unknown>");
    }
    buffer.append(Constants.NEWLINE);

    buffer.append(Constants.INDENT);
    buffer.append("Public Data: ");
    buffer.append(Functions.toHexString(publicData_));
    // buffer.append(Constants.NEWLINE);

    return buffer.toString();
  }
  /**
   * Compares all member variables of this object with the other object. Returns only true, if all
   * are equal in both objects.
   *
   * @param otherObject The other object to compare to.
   * @return True, if other is an instance of this class and all member variables of both objects
   *     are equal. False, otherwise.
   */
  public boolean equals(java.lang.Object otherObject) {
    boolean equal = false;

    if (otherObject instanceof DHPkcsDeriveParameters) {
      DHPkcsDeriveParameters other = (DHPkcsDeriveParameters) otherObject;
      equal = (this == other) || Functions.equals(this.publicValue_, other.publicValue_);
    }

    return equal;
  }
  /**
   * Returns the string representation of this object. Do not parse data from this string, it is for
   * debugging only.
   *
   * @return A string representation of this object.
   */
  public String toString() {
    StringBuffer buffer = new StringBuffer();

    buffer.append(Constants.INDENT);
    buffer.append("Public Value (hex): ");
    buffer.append(Functions.toHexString(publicValue_));
    // buffer.append(Constants.NEWLINE);

    return buffer.toString();
  }
 /**
  * Set the ey derivation function used on the shared secret value.
  *
  * @param keyDerivationFunction The key derivation function used on the shared secret value. One
  *     of the values defined in KeyDerivationFunctionType.
  * @preconditions (keyDerivationFunction == KeyDerivationFunctionType.NULL) or
  *     (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF)) or (keyDerivationFunction ==
  *     KeyDerivationFunctionType.SHA1_KDF_ASN1)) or (keyDerivationFunction ==
  *     KeyDerivationFunctionType.SHA1_KDF_CONCATENATE))
  */
 public void setKeyDerivationFunction(long keyDerivationFunction) {
   if ((keyDerivationFunction != KeyDerivationFunctionType.NULL)
       && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF)
       && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF_ASN1)
       && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF_CONCATENATE)) {
     throw new IllegalArgumentException(
         "Illegal value for argument\"keyDerivationFunction\": "
             + Functions.toHexString(keyDerivationFunction));
   }
   keyDerivationFunction_ = keyDerivationFunction;
 }
Esempio n. 6
0
  public void readCertificate() throws PKCS11Exception {
    System.out.println("read certificate");

    CK_ATTRIBUTE[] template = new CK_ATTRIBUTE[1];
    template[0] = new CK_ATTRIBUTE();
    template[0].type = PKCS11Constants.CKA_VALUE;
    myPKCS11Module_.C_GetAttributeValue(session_, certificateHandle_, template);
    derEncodedCertificate_ = (byte[]) template[0].pValue;
    System.out.println("DER encoded certificate (" + derEncodedCertificate_.length + " bytes):");
    System.out.println(Functions.toHexString(derEncodedCertificate_));

    System.out.println("FINISHED\n");
  }
  /**
   * Compares all member variables of this object with the other object. Returns only true, if all
   * are equal in both objects.
   *
   * @param otherObject The other object to compare to.
   * @return True, if other is an instance of this class and all member variables of both objects
   *     are equal. False, otherwise.
   */
  public boolean equals(java.lang.Object otherObject) {
    boolean equal = false;

    if (otherObject instanceof DHKeyDerivationParameters) {
      DHKeyDerivationParameters other = (DHKeyDerivationParameters) otherObject;
      equal =
          (this == other)
              || ((this.keyDerivationFunction_ == other.keyDerivationFunction_)
                  && Functions.equals(this.publicData_, other.publicData_));
    }

    return equal;
  }
Esempio n. 8
0
  public void printAllObjects() throws PKCS11Exception {
    System.out.println("print all objects");

    for (int i = 0; i < objects_.length; i++) {
      System.out.println("object No. " + i);
      CK_ATTRIBUTE[] template = new CK_ATTRIBUTE[1];
      template[0] = new CK_ATTRIBUTE();
      template[0].type = PKCS11Constants.CKA_CLASS;
      myPKCS11Module_.C_GetAttributeValue(session_, objects_[i], template);
      System.out.println(
          "CKA_CLASS: " + Functions.classTypeToString(((Long) template[0].pValue).longValue()));
    }

    System.out.println("FINISHED\n");
  }
 /**
  * Create a new DHKeyDerivationParameters object with the given attributes.
  *
  * @param keyDerivationFunction The key derivation function used on the shared secret value. One
  *     of the values defined in KeyDerivationFunctionType.
  * @param publicData The other partie's public key value.
  * @preconditions ((keyDerivationFunction == KeyDerivationFunctionType.NULL) or
  *     (keyDerivationFunction == KeyDerivationFunctionType.SHA1_KDF) or (keyDerivationFunction ==
  *     KeyDerivationFunctionType.SHA1_KDF_ASN1) or (keyDerivationFunction ==
  *     KeyDerivationFunctionType.SHA1_KDF_CONCATENATE)) and (publicData <> null)
  */
 protected DHKeyDerivationParameters(long keyDerivationFunction, byte[] publicData) {
   if ((keyDerivationFunction != KeyDerivationFunctionType.NULL)
       && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF)
       && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF_ASN1)
       && (keyDerivationFunction != KeyDerivationFunctionType.SHA1_KDF_CONCATENATE)) {
     throw new IllegalArgumentException(
         "Illegal value for argument\"keyDerivationFunction\": "
             + Functions.toHexString(keyDerivationFunction));
   }
   if (publicData == null) {
     throw new NullPointerException("Argument \"publicData\" must not be null.");
   }
   keyDerivationFunction_ = keyDerivationFunction;
   publicData_ = publicData;
 }
Esempio n. 10
0
  public void getMechanismInfo() throws PKCS11Exception {
    CK_MECHANISM_INFO mechanismInfo;

    System.out.println("getting mechanism list");
    System.out.println("getting slot list");
    long[] slotIDs = myPKCS11Module_.C_GetSlotList(true);
    for (int i = 0; i < slotIDs.length; i++) {
      System.out.println("getting mechanism list for slot " + slotIDs[i]);
      long[] mechanismIDs = myPKCS11Module_.C_GetMechanismList(slotIDs[i]);
      for (int j = 0; j < mechanismIDs.length; j++) {
        System.out.println(
            "mechanism info for mechanism "
                + Functions.mechanismCodeToString(mechanismIDs[j])
                + ": ");
        mechanismInfo = myPKCS11Module_.C_GetMechanismInfo(slotIDs[i], mechanismIDs[j]);
        System.out.println(mechanismInfo);
      }
    }
    System.out.println("FINISHED\n");
  }
Esempio n. 11
0
  public void findCertificate() throws PKCS11Exception {
    System.out.println("find certificate");

    // first get the ID of the signature key
    CK_ATTRIBUTE[] attributeTemplateList = new CK_ATTRIBUTE[1];
    attributeTemplateList[0] = new CK_ATTRIBUTE();
    attributeTemplateList[0].type = PKCS11Constants.CKA_ID;

    myPKCS11Module_.C_GetAttributeValue(session_, signatureKeyHandle_, attributeTemplateList);
    byte[] keyAndCertificateID = (byte[]) attributeTemplateList[0].pValue;
    System.out.println("ID of siganture key: " + Functions.toHexString(keyAndCertificateID));

    // now get the certificate with the same ID as the signature key
    attributeTemplateList = new CK_ATTRIBUTE[2];

    attributeTemplateList[0] = new CK_ATTRIBUTE();
    attributeTemplateList[0].type = PKCS11Constants.CKA_CLASS;
    attributeTemplateList[0].pValue = new Long(PKCS11Constants.CKO_CERTIFICATE);
    attributeTemplateList[1] = new CK_ATTRIBUTE();
    attributeTemplateList[1].type = PKCS11Constants.CKA_ID;
    attributeTemplateList[1].pValue = keyAndCertificateID;

    myPKCS11Module_.C_FindObjectsInit(session_, attributeTemplateList);
    long[] availableCertificates =
        myPKCS11Module_.C_FindObjects(session_, 100); // maximum of 100 at once
    if (availableCertificates == null) {
      System.out.println("null returned - no certificate found");
    } else {
      System.out.println(
          "found " + availableCertificates.length + " certificates with matching ID");
      for (int i = 0; i < availableCertificates.length; i++) {
        if (i == 0) { // the first we find, we take as our certificate
          certificateHandle_ = availableCertificates[i];
          System.out.print("for verification we use ");
        }
        System.out.println("certificate " + i);
      }
    }
    myPKCS11Module_.C_FindObjectsFinal(session_);
    System.out.println("FINISHED\n");
  }
 /**
  * The overriding of this method should ensure that the objects of this class work correctly in a
  * hashtable.
  *
  * @return The hash code of this object.
  */
 public int hashCode() {
   return Functions.hashCode(publicValue_);
 }
 /**
  * The overriding of this method should ensure that the objects of this class work correctly in a
  * hashtable.
  *
  * @return The hash code of this object.
  */
 public int hashCode() {
   return ((int) keyDerivationFunction_) ^ Functions.hashCode(publicData_);
 }