com.unbound.client.pkcs11.PKCS11RSAPublicKey Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of unbound-java-provider Show documentation
Show all versions of unbound-java-provider Show documentation
This is a collection of JAVA libraries that implement Unbound cryptographic classes for JAVA provider, PKCS11 wrapper, cryptoki, and advapi
package com.unbound.client.pkcs11;
import com.dyadicsec.cryptoki.CKR_Exception;
import com.dyadicsec.cryptoki.CK_ATTRIBUTE;
import com.dyadicsec.cryptoki.Library;
import com.unbound.client.ObjectType;
import com.unbound.client.RSAPublicKeyObject;
import com.unbound.common.Converter;
import com.unbound.provider.KeyParameters;
import java.math.BigInteger;
import java.security.ProviderException;
import java.security.interfaces.RSAPublicKey;
import java.util.ArrayList;
import static com.dyadicsec.cryptoki.CK.*;
public class PKCS11RSAPublicKey extends PKCS11Object implements RSAPublicKeyObject
{
private BigInteger cka_modulus = null;
private BigInteger cka_public_exponent = null;
PKCS11RSAPublicKey(PKCS11Session session, int handle)
{
super(ObjectType.RSAPub, handle);
read(session);
}
@Override
protected void getReadTemplate(ArrayList t)
{
super.getReadTemplate(t);
t.add(new CK_ATTRIBUTE(CKA_MODULUS));
t.add(new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT));
}
@Override
protected int acceptReadTempate(CK_ATTRIBUTE[] attrs) throws CKR_Exception
{
int index = super.acceptReadTempate(attrs);
cka_modulus = (BigInteger) attrs[index+0].pValue;
cka_public_exponent = (BigInteger) attrs[index+1].pValue;
return index+2;
}
@Override
public BigInteger getPublicExponent()
{
return cka_public_exponent;
}
@Override
public BigInteger getModulus()
{
return cka_modulus;
}
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_PUBLIC_KEY));
t.add(new CK_ATTRIBUTE(CKA_KEY_TYPE, CKK_RSA));
if (name!=null) t.add(new CK_ATTRIBUTE(CKA_ID, strToId(name)));
if (kp!=null)
{
if (kp.isSetTrusted()) t.add(new CK_ATTRIBUTE(CKA_TRUSTED, kp.isTrusted()));
if (kp.isSetVerify()) t.add(new CK_ATTRIBUTE(CKA_VERIFY, kp.isAllowVerify()));
if (kp.isSetEncrypt()) t.add(new CK_ATTRIBUTE(CKA_ENCRYPT, kp.isAllowEncrypt()));
if (kp.isSetWrap()) t.add(new CK_ATTRIBUTE(CKA_WRAP, kp.isAllowWrap()));
}
}
catch (CKR_Exception e)
{
throw new ProviderException(e);
}
return t;
}
public static RSAPublicKeyObject importKey(PKCS11Session session, String name, RSAPublicKey keyValue, KeyParameters kp)
{
try
{
int keySize = keyValue.getModulus().bitLength() / 8;
ArrayList t = getNewTemplate(name, kp);
t.add(new CK_ATTRIBUTE(CKA_MODULUS, Converter.bigNumToBin(keyValue.getModulus(), keySize)));
t.add(new CK_ATTRIBUTE(CKA_PUBLIC_EXPONENT, Converter.bigNumToBin(keyValue.getPublicExponent())));
int keyHandle = Library.C_CreateObject(session.getHandle(), getAttrs(t));
return new PKCS11RSAPublicKey(session, keyHandle);
}
catch (CKR_Exception e) { throw new ProviderException(e); }
}
@Override
public long getReplacedUid()
{
return replacedUid;
}
}