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

com.unbound.client.Partition 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;

import com.unbound.common.crypto.EC;
import com.unbound.provider.KeyParameters;
import com.unbound.provider.UBKeyStore;

import java.security.ProviderException;
import java.security.cert.X509Certificate;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;

public abstract class Partition
{
  public abstract String getName();
  public abstract UBKeyStore getKeyStore();
  public abstract Session acquireSession();

  private static void releaseSession(Session session)
  {
    if (session!=null) session.release();
  }

  public static Partition get(String name)
  {
    return Client.getInstance().getPartition(name);
  }

  public static Partition getDefault()
  {
    return get(null);
  }

  private interface Function { R apply(T t); }
  private interface Consumer { void accept(T t); }

  private  T objectWithSession(Function callback)
  {
    Session session = null;
    try
    {
      session = acquireSession();
      return callback.apply(session);
    }
    catch (Exception e) { throw new ProviderException(e); }
    finally { releaseSession(session); }
  }

  private void withSession(Consumer callback)
  {
    Session session = null;
    try
    {
      session = acquireSession();
      callback.accept(session);
    }
    catch (Exception e) { throw new ProviderException(e); }
    finally { releaseSession(session); }
  }

  public ECPrivateKeyObject generateEcKey(final String name, final EC.Curve curve, final KeyParameters kp)
  {
    return objectWithSession(new Function()
    {
      @Override
      public ECPrivateKeyObject apply(Session session) { return session.generateEcKey(name, curve, kp); }
    });
  }

  public RSAPrivateKeyObject generateRsaKey(final String name, final int bits, final KeyParameters kp)
  {
    return objectWithSession(new Function()
    {
      @Override
      public RSAPrivateKeyObject apply(Session session) { return session.generateRsaKey(name, bits, kp); }
    });
  }

  public EDDSAPrivateKeyObject generateEddsaKey(final String name, final KeyParameters kp)
  {
    return objectWithSession(new Function()
    {
      @Override
      public EDDSAPrivateKeyObject apply(Session session) { return session.generateEddsaKey(name, kp); }
    });
  }

  public SecretKeyObject generateSecretKey(final String name, final ObjectType type, final int bitSize, final KeyParameters kp)
  {
    return objectWithSession(new Function()
    {
      @Override
      public SecretKeyObject apply(Session session) { return session.generateSecretKey(name, type, bitSize, kp); }
    });
  }

  public ECPRFKey generateEcprfKey(final String name, final KeyParameters kp)
  {
    return objectWithSession(new Function()
    {
      @Override
      public ECPRFKey apply(Session session) { return session.generateEcprfKey(name, kp); }
    });
  }

  public SecretKeyObject importSecretKey(final String name, final ObjectType type, final byte[] keyValue, final KeyParameters kp)
  {
    return objectWithSession(new Function()
    {
      @Override
      public SecretKeyObject apply(Session session) { return session.importSecretKey(name, type, keyValue, kp); }
    });
  }

  public RSAPrivateKeyObject importRsaKey(final String name, final RSAPrivateCrtKey keyValue, final KeyParameters kp)
  {
    return objectWithSession(new Function()
    {
      @Override
      public RSAPrivateKeyObject apply(Session session) { return session.importRsaKey(name, keyValue, kp); }
    });
  }

  public RSAPublicKeyObject importPubRsaKey(final String name, final RSAPublicKey keyValue, final KeyParameters kp)
  {
    return objectWithSession(new Function()
    {
      @Override
      public RSAPublicKeyObject apply(Session session) { return session.importPubRsaKey(name, keyValue, kp); }
    });
  }

  public ECPrivateKeyObject importEcKey(final String name, final ECPrivateKey keyValue, final KeyParameters kp)
  {
    return objectWithSession(new Function()
    {
      @Override
      public ECPrivateKeyObject apply(Session session) { return session.importEcKey(name, keyValue, kp); }
    });
  }

  public CertObject importCert(final String name, final X509Certificate cert)
  {
    return objectWithSession(new Function()
    {
      @Override
      public CertObject apply(Session session) { return session.importCert(name, cert); }
    });
  }

  public void deleteObject(final BaseObject object)
  {
    withSession(new Consumer()
    {
      @Override
      public void accept(Object session) { object.delete((Session) session); }
    });
  }

  public void changeObjectName(final BaseObject object, final String newName)
  {
    if (object.getName().equals(newName)) return;
    withSession(new Consumer()
    {
      @Override
      public void accept(Object session) { object.changeName((Session) session, newName); }
    });
  }

  public abstract void login(char[] password);

  public BaseObject locate(final ObjectType type, final LocateParams params)
  {
    return objectWithSession(new Function()
    {
      @Override
      public BaseObject apply(Session session) { return session.locate(type, params); }
    });
  }

  public ArrayList locate(final ObjectType type)
  {
    return objectWithSession(new Function>()
    {
      @Override
      public ArrayList apply(Session session) { return session.locate(type); }
    });
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy