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

com.unbound.provider.UBObject 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.provider;

import com.dyadicsec.provider.KeyParameters;
import com.unbound.common.HEX;
import com.unbound.common.Log;
import com.unbound.provider.kmip.KMIP;
import com.unbound.provider.kmip.attribute.*;
import com.unbound.provider.kmip.request.*;
import com.unbound.provider.kmip.response.GetAttributesResponse;
import com.unbound.provider.kmip.response.GetResponse;
import com.unbound.provider.kmip.response.ResponseMessage;

import java.io.IOException;
import java.security.ProviderException;
import java.security.cert.CertificateException;
import java.security.spec.InvalidKeySpecException;

import static com.unbound.common.Converter.setBE8;
import static com.unbound.provider.kmip.KMIP.Tag.*;

abstract class UBObject
{
  Partition partition;
  long uid;
  String name;
  long initialDate;

  abstract int kmipObjectType();

  static long strToUid(String str)
  {
    if (str.length() == 16) str = "0x00" + str;
    if (str.length() == 20 && str.charAt(0) == '0' && str.charAt(1) == 'x' && str.charAt(2) == '0' && str.charAt(3) == '0')
    {
      long uid = 0;
      for (int i = 4; i < 20; i += 2)
      {
        int hi = HEX.from(str.charAt(i));
        int lo = HEX.from(str.charAt(i + 1));
        if (lo < 0 || hi < 0) return 0;
        uid = (uid << 8) | (hi << 4) | lo;
      }
      return uid;
    }

    return 0;
  }

  static String uidToStr(long uid)
  {
    byte[] cka_id = new byte[9];
    cka_id[0] = 0;
    setBE8(cka_id, 1, uid);

    char[] out = new char[20];
    out[0] = '0';
    out[1] = 'x';

    for (int i = 0; i < cka_id.length; i++)
    {
      out[2 + i * 2 + 0] = HEX.chars[(cka_id[i] >> 4) & 0x0f];
      out[2 + i * 2 + 1] = HEX.chars[cka_id[i] & 0x0f];
    }

    return new String(out);
  }


  UBObject(Partition partition)
  {
    this.partition = partition;
  }

  UBObject(Partition partition, long uid, GetAttributesResponse getAttrResp)
  {
    this.partition = partition;
    this.uid = uid;
    name = ((Name)getAttrResp.attrs.get(2)).value;
    initialDate = ((DateAttribute)getAttrResp.attrs.get(3)).value;
  }

  static UBObject[] read(Partition partition, long[] uids, boolean get) throws CertificateException, InvalidKeySpecException, IOException
  {
    if (uids.length==0) return new UBObject[0];

    ResponseMessage respMsg = partition.read(uids, get);
    UBObject[] list = new UBObject[uids.length];

    int n = 0;
    for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy