예제 #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();
  }
  /**
   * 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;
 }
예제 #5
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");
  }
 /**
  * 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;
 }
예제 #7
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");
  }