All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.unbound.client.pkcs11.PKCS11EDDSAPrivateKey Maven / Gradle / Ivy

Go to download

This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi

There is a newer version: 42761
Show newest version
package com.unbound.client.pkcs11;

import com.dyadicsec.cryptoki.*;
import com.unbound.client.EDDSAPrivateKeyObject;
import com.unbound.client.ObjectType;
import com.unbound.client.Session;
import com.unbound.provider.KeyParameters;

import java.security.ProviderException;
import java.util.ArrayList;

import static com.dyadicsec.cryptoki.CK.*;

public class PKCS11EDDSAPrivateKey extends PKCS11Object implements EDDSAPrivateKeyObject
{
  private byte[] pubKeyValue;

  PKCS11EDDSAPrivateKey(PKCS11Session session, int handle)
  {
    super(ObjectType.EDDSAPrv, handle);
    read(session);
  }

  @Override
  public byte[] getPublicKeyValue()
  {
    return pubKeyValue;
  }

  @Override
  protected void getReadTemplate(ArrayList t)
  {
    super.getReadTemplate(t);
    t.add(new CK_ATTRIBUTE(CK.DYCKA_EDDSA_PUB_KEY));
  }

  @Override
  protected int acceptReadTempate(CK_ATTRIBUTE[] attrs) throws CKR_Exception
  {
    int index = super.acceptReadTempate(attrs);
    pubKeyValue = (byte[]) attrs[index+0].pValue;
    return index+1;
  }

  static ArrayList getNewTemplate(String name, KeyParameters kp)
  {
    if (name==null && kp!=null) name = kp.getName();
    ArrayList t = new ArrayList();
    try
    {
      t.add(new CK_ATTRIBUTE(CKA_TOKEN, true));
      t.add(new CK_ATTRIBUTE(CKA_CLASS, CKO_PRIVATE_KEY));
      t.add(new CK_ATTRIBUTE(CKA_KEY_TYPE, DYCKK_EDDSA));

      if (name!=null) t.add(new CK_ATTRIBUTE(CKA_ID, strToId(name)));

      if (kp!=null)
      {
        if (kp.isSetSign()) t.add(new CK_ATTRIBUTE(CKA_SIGN, kp.isAllowSign()));
        makeExportLevel(t, kp);
      }
    }
    catch (CKR_Exception e)
    {
      throw new ProviderException(e);
    }
    return t;
  }

  static PKCS11EDDSAPrivateKey generate(PKCS11Session session, String name, KeyParameters kp)
  {
    try
    {
      ArrayList t = getNewTemplate(name, kp);
      CK_ATTRIBUTE[] tPub =
      {
        new CK_ATTRIBUTE(CKA_TOKEN, false),
        new CK_ATTRIBUTE(CKA_CLASS, CKO_PUBLIC_KEY),
        new CK_ATTRIBUTE(CKA_KEY_TYPE, DYCKK_EDDSA),
      };

      int[] keyHandles = Library.C_GenerateKeyPair(session.getHandle(), new CK_MECHANISM(CK.DYCKM_EDDSA_KEY_GEN), tPub, getAttrs(t));
      Library.C_DestroyObject(session.getHandle(), keyHandles[0]);
		  return new PKCS11EDDSAPrivateKey(session, keyHandles[1]);
    }
    catch (CKR_Exception e) { throw new ProviderException(e); }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy