com.unbound.client.kmip.KMIPRSAPublicKey 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.kmip;
import com.unbound.client.ObjectType;
import com.unbound.client.RSAPublicKeyObject;
import com.unbound.common.Log;
import com.unbound.common.crypto.SystemProvider;
import com.unbound.kmip.KMIP;
import com.unbound.kmip.attribute.*;
import com.unbound.kmip.object.ManagedObject;
import com.unbound.kmip.request.RegisterRequest;
import com.unbound.provider.KeyParameters;
import java.math.BigInteger;
import java.security.KeyFactory;
import java.security.ProviderException;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.X509EncodedKeySpec;
public class KMIPRSAPublicKey extends KMIPObject implements RSAPublicKeyObject
{
BigInteger modulus = null;
BigInteger publicExponent = null;
KMIPRSAPublicKey(KMIPSession session, long uid)
{
super(ObjectType.RSAPub, uid);
read(session);
}
@Override
public BigInteger getPublicExponent() { return publicExponent; }
@Override
public BigInteger getModulus() { return modulus; }
@Override
void acceptManagedObject(ManagedObject managedObject)
{
com.unbound.kmip.object.PublicKey kmipPublicKey = (com.unbound.kmip.object.PublicKey)managedObject;
try
{
X509EncodedKeySpec spec = new X509EncodedKeySpec(kmipPublicKey.keyBlock.buf);
KeyFactory kf = SystemProvider.KeyFactory.getInstance("RSA");
RSAPublicKey rsa = (RSAPublicKey) kf.generatePublic(spec);
modulus = rsa.getModulus();
publicExponent = rsa.getPublicExponent();
}
catch (InvalidKeySpecException e) { throw new ProviderException(e); }
}
static TemplateAttribute getTemplate(String name, KeyParameters kp)
{
TemplateAttribute template = new TemplateAttribute();
if (kp!=null)
{
if (name==null) name = kp.getName();
template.attrs.add(new BoolAttribute(KMIP.Tag.CKA_ENCRYPT, kp.isAllowEncrypt()));
template.attrs.add(new BoolAttribute(KMIP.Tag.CKA_VERIFY, kp.isAllowVerify()));
template.attrs.add(new BoolAttribute(KMIP.Tag.CKA_WRAP, kp.isAllowWrap()));
template.attrs.add(new BoolAttribute(KMIP.Tag.CKA_TRUSTED, kp.isTrusted()));
}
else template.attrs.add(new IntAttribute(KMIP.Tag.CryptographicUsageMask,
KMIP.CryptographicUsageMask.Encrypt |
KMIP.CryptographicUsageMask.Verify |
KMIP.CryptographicUsageMask.WrapKey));
template.attrs.add(new EnumAttribute(KMIP.Tag.CryptographicAlgorithm, KMIP.CryptographicAlgorithm.RSA));
if (name!=null) template.attrs.add(new Name(name));
return template;
}
static KMIPRSAPublicKey importKey(KMIPSession session, String name, RSAPublicKey keyValue, KeyParameters kp)
{
long uid = 0;
Log log = Log.func("KMIPRSAPrivateKey.importKey").end(); try
{
RegisterRequest req = new RegisterRequest();
req.objectType = KMIP.ObjectType.PublicKey;
req.template = getTemplate(name, kp);
com.unbound.kmip.object.PublicKey mo = new com.unbound.kmip.object.PublicKey();
req.object = mo;
mo.keyBlock.formatType = KMIP.KeyFormatType.X_509;
mo.keyBlock.buf = keyValue.getEncoded();
mo.keyBlock.algorithm = KMIP.CryptographicAlgorithm.RSA;
uid = register(session, req);
return new KMIPRSAPublicKey(session, uid);
}
catch (Exception e) { log.failed(e); throw e; } finally { log.leavePrint().logHex("UID", uid).end(); }
}
}