use of org.xipki.security.pkcs11.P11TokenException in project xipki by xipki.
the class IaikP11Slot method generateKeyPair.
private P11Identity generateKeyPair(long mech, PrivateKey privateKey, PublicKey publicKey) throws P11TokenException {
final String label = toString(privateKey.getLabel());
byte[] id = null;
try {
KeyPair keypair;
Session session = borrowWritableSession();
try {
if (labelExists(session, label)) {
throw new IllegalArgumentException("label " + label + " exists, please specify another one");
}
id = generateKeyId(session);
privateKey.getId().setByteArrayValue(id);
publicKey.getId().setByteArrayValue(id);
try {
keypair = session.generateKeyPair(Mechanism.get(mech), publicKey, privateKey);
} catch (TokenException ex) {
throw new P11TokenException("could not generate keypair " + Pkcs11Functions.mechanismCodeToString(mech), ex);
}
P11ObjectIdentifier objId = new P11ObjectIdentifier(id, label);
P11EntityIdentifier entityId = new P11EntityIdentifier(slotId, objId);
java.security.PublicKey jcePublicKey;
try {
jcePublicKey = generatePublicKey(keypair.getPublicKey());
} catch (XiSecurityException ex) {
throw new P11TokenException("could not generate public key " + objId, ex);
}
PrivateKey privateKey2 = getPrivateKeyObject(session, id, label.toCharArray());
if (privateKey2 == null) {
throw new P11TokenException("could not read the generated private key");
}
return new IaikP11Identity(this, entityId, privateKey2, jcePublicKey, null);
} finally {
returnWritableSession(session);
}
} catch (P11TokenException | RuntimeException ex) {
try {
removeObjects(id, label);
} catch (Throwable th) {
LogUtil.error(LOG, th, "could not remove objects");
}
throw ex;
}
}
use of org.xipki.security.pkcs11.P11TokenException in project xipki by xipki.
the class IaikP11Slot method importSecretKey0.
@Override
protected P11Identity importSecretKey0(long keyType, byte[] keyValue, String label, P11NewKeyControl control) throws P11TokenException {
ValuedSecretKey template = new ValuedSecretKey(keyType);
template.getToken().setBooleanValue(true);
template.getLabel().setCharArrayValue(label.toCharArray());
template.getSign().setBooleanValue(true);
template.getSensitive().setBooleanValue(true);
template.getExtractable().setBooleanValue(control.isExtractable());
template.getValue().setByteArrayValue(keyValue);
SecretKey key;
Session session = borrowWritableSession();
try {
if (labelExists(session, label)) {
throw new IllegalArgumentException("label " + label + " exists, please specify another one");
}
byte[] id = generateKeyId(session);
template.getId().setByteArrayValue(id);
try {
key = (SecretKey) session.createObject(template);
} catch (TokenException ex) {
throw new P11TokenException("could not create secret key", ex);
}
P11ObjectIdentifier objId = new P11ObjectIdentifier(id, label);
P11EntityIdentifier entityId = new P11EntityIdentifier(slotId, objId);
return new IaikP11Identity(this, entityId, key);
} finally {
returnWritableSession(session);
}
}
use of org.xipki.security.pkcs11.P11TokenException in project xipki by xipki.
the class ProxyP11Slot method parseGenerateKeypairResult.
private P11Identity parseGenerateKeypairResult(byte[] resp) throws P11TokenException {
if (resp == null) {
throw new P11TokenException("server returned no result");
}
Asn1P11EntityIdentifier ei;
try {
ei = Asn1P11EntityIdentifier.getInstance(resp);
} catch (BadAsn1ObjectException ex) {
throw new P11TokenException("invalid ASN1 object Asn1P11EntityIdentifier: " + ex.getMessage(), ex);
}
if (!slotId.equals(ei.getSlotId().getSlotId())) {
throw new P11TokenException("");
}
P11EntityIdentifier entityId = ei.getEntityId();
PublicKey publicKey = getPublicKey(entityId.getObjectId());
return new ProxyP11Identity(this, entityId, publicKey, null);
}
use of org.xipki.security.pkcs11.P11TokenException in project xipki by xipki.
the class ProxyP11Slot method parseGenerateSecretKeyResult.
private P11Identity parseGenerateSecretKeyResult(byte[] resp) throws P11TokenException {
if (resp == null) {
throw new P11TokenException("server returned no result");
}
Asn1P11EntityIdentifier ei;
try {
ei = Asn1P11EntityIdentifier.getInstance(resp);
} catch (BadAsn1ObjectException ex) {
throw new P11TokenException("invalid ASN1 object Asn1P11EntityIdentifier: " + ex.getMessage(), ex);
}
if (!slotId.equals(ei.getSlotId().getSlotId())) {
throw new P11TokenException("");
}
P11EntityIdentifier entityId = ei.getEntityId();
return new ProxyP11Identity(this, entityId);
}
use of org.xipki.security.pkcs11.P11TokenException in project xipki by xipki.
the class ProxyP11Slot method updateCertificate0.
@Override
protected void updateCertificate0(P11ObjectIdentifier objectId, X509Certificate newCert) throws P11TokenException, CertificateException {
Asn1EntityIdAndCert asn1 = new Asn1EntityIdAndCert(new P11EntityIdentifier(slotId, objectId), newCert);
module.send(P11ProxyConstants.ACTION_UPDATE_CERT, asn1);
}
Aggregations