use of iaik.pkcs.pkcs11.Mechanism in project xipki by xipki.
the class IaikP11Slot method digestKey.
byte[] digestKey(long mechanism, IaikP11Identity identity) throws P11TokenException {
ParamUtil.requireNonNull("identity", identity);
assertMechanismSupported(mechanism);
Key signingKey = identity.getSigningKey();
if (!(signingKey instanceof SecretKey)) {
throw new P11TokenException("digestSecretKey could not be applied to non-SecretKey");
}
if (LOG.isTraceEnabled()) {
LOG.debug("digest (init, digestKey, then finish)\n{}", signingKey);
}
int digestLen;
if (PKCS11Constants.CKM_SHA_1 == mechanism) {
digestLen = 20;
} else if (PKCS11Constants.CKM_SHA224 == mechanism || PKCS11Constants.CKM_SHA3_224 == mechanism) {
digestLen = 28;
} else if (PKCS11Constants.CKM_SHA256 == mechanism || PKCS11Constants.CKM_SHA3_256 == mechanism) {
digestLen = 32;
} else if (PKCS11Constants.CKM_SHA384 == mechanism || PKCS11Constants.CKM_SHA3_384 == mechanism) {
digestLen = 48;
} else if (PKCS11Constants.CKM_SHA512 == mechanism || PKCS11Constants.CKM_SHA3_512 == mechanism) {
digestLen = 64;
} else {
throw new P11TokenException("unsupported mechnism " + mechanism);
}
ConcurrentBagEntry<Session> session0 = borrowSession();
try {
Session session = session0.value();
session.digestInit(Mechanism.get(mechanism));
session.digestKey((SecretKey) signingKey);
byte[] digest = new byte[digestLen];
session.digestFinal(digest, 0, digestLen);
return digest;
} catch (TokenException ex) {
throw new P11TokenException(ex);
} finally {
sessions.requite(session0);
}
}
use of iaik.pkcs.pkcs11.Mechanism in project xipki by xipki.
the class IaikP11Slot method getMechanism.
private static Mechanism getMechanism(long mechanism, P11Params parameters) throws P11TokenException {
Mechanism ret = Mechanism.get(mechanism);
if (parameters == null) {
return ret;
}
Params paramObj;
if (parameters instanceof P11RSAPkcsPssParams) {
P11RSAPkcsPssParams param = (P11RSAPkcsPssParams) parameters;
paramObj = new RSAPkcsPssParams(Mechanism.get(param.getHashAlgorithm()), param.getMaskGenerationFunction(), param.getSaltLength());
} else if (parameters instanceof P11ByteArrayParams) {
paramObj = new OpaqueParams(((P11ByteArrayParams) parameters).getBytes());
} else if (parameters instanceof P11IVParams) {
paramObj = new IVParams(((P11IVParams) parameters).getIV());
} else {
throw new P11TokenException("unknown P11Parameters " + parameters.getClass().getName());
}
if (paramObj != null) {
ret.setParams(paramObj);
}
return ret;
}
use of iaik.pkcs.pkcs11.Mechanism in project xipki by xipki.
the class IaikP11Slot method refresh0.
@Override
protected P11SlotRefreshResult refresh0() throws P11TokenException {
Mechanism[] mechanisms;
try {
mechanisms = slot.getToken().getMechanismList();
} catch (TokenException ex) {
throw new P11TokenException("could not getMechanismList: " + ex.getMessage(), ex);
}
P11SlotRefreshResult ret = new P11SlotRefreshResult();
if (mechanisms != null) {
for (Mechanism mech : mechanisms) {
ret.addMechanism(mech.getMechanismCode());
}
}
ConcurrentBagEntry<Session> session = borrowSession();
try {
// secret keys
List<SecretKey> secretKeys = getAllSecretKeyObjects(session.value());
for (SecretKey secKey : secretKeys) {
byte[] keyId = secKey.getId().getByteArrayValue();
if (keyId == null || keyId.length == 0) {
continue;
}
analyseSingleKey(secKey, ret);
}
// first get the list of all CA certificates
List<X509PublicKeyCertificate> p11Certs = getAllCertificateObjects(session.value());
for (X509PublicKeyCertificate p11Cert : p11Certs) {
P11ObjectIdentifier objId = new P11ObjectIdentifier(p11Cert.getId().getByteArrayValue(), toString(p11Cert.getLabel()));
ret.addCertificate(objId, parseCert(p11Cert));
}
List<PrivateKey> privKeys = getAllPrivateObjects(session.value());
for (PrivateKey privKey : privKeys) {
byte[] keyId = privKey.getId().getByteArrayValue();
if (keyId == null || keyId.length == 0) {
break;
}
try {
analyseSingleKey(session.value(), privKey, ret);
} catch (XiSecurityException ex) {
LogUtil.error(LOG, ex, "XiSecurityException while initializing private key " + "with id " + hex(keyId));
continue;
} catch (Throwable th) {
String label = "";
if (privKey.getLabel() != null) {
label = new String(privKey.getLabel().getCharArrayValue());
}
LOG.error("unexpected exception while initializing private key with id " + hex(keyId) + " and label " + label, th);
continue;
}
}
return ret;
} finally {
sessions.requite(session);
}
}
use of iaik.pkcs.pkcs11.Mechanism in project xipki by xipki.
the class IaikP11Slot method generateSecretKey0.
@Override
protected P11Identity generateSecretKey0(long keyType, int keysize, String label, P11NewKeyControl control) throws P11TokenException {
if (keysize % 8 != 0) {
throw new IllegalArgumentException("keysize is not multiple of 8: " + keysize);
}
long mech;
if (PKCS11Constants.CKK_AES == keyType) {
mech = PKCS11Constants.CKM_AES_KEY_GEN;
} else if (PKCS11Constants.CKK_DES3 == keyType) {
mech = PKCS11Constants.CKM_DES3_KEY_GEN;
} else if (PKCS11Constants.CKK_GENERIC_SECRET == keyType) {
mech = PKCS11Constants.CKM_GENERIC_SECRET_KEY_GEN;
} else if (PKCS11Constants.CKK_SHA_1_HMAC == keyType || PKCS11Constants.CKK_SHA224_HMAC == keyType || PKCS11Constants.CKK_SHA256_HMAC == keyType || PKCS11Constants.CKK_SHA384_HMAC == keyType || PKCS11Constants.CKK_SHA512_HMAC == keyType || PKCS11Constants.CKK_SHA3_224_HMAC == keyType || PKCS11Constants.CKK_SHA3_256_HMAC == keyType || PKCS11Constants.CKK_SHA3_384_HMAC == keyType || PKCS11Constants.CKK_SHA3_512_HMAC == keyType) {
mech = PKCS11Constants.CKM_GENERIC_SECRET_KEY_GEN;
} else {
throw new IllegalArgumentException("unsupported key type 0x" + Functions.toFullHex((int) keyType));
}
assertMechanismSupported(mech);
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.getValueLen().setLongValue((long) (keysize / 8));
Mechanism mechanism = Mechanism.get(mech);
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.generateKey(mechanism, template);
} catch (TokenException ex) {
throw new P11TokenException("could not generate generic secret key using " + mechanism.getName(), ex);
}
P11ObjectIdentifier objId = new P11ObjectIdentifier(id, label);
P11EntityIdentifier entityId = new P11EntityIdentifier(slotId, objId);
return new IaikP11Identity(this, entityId, key);
} finally {
returnWritableSession(session);
}
}
use of iaik.pkcs.pkcs11.Mechanism in project xipki by xipki.
the class IaikP11Slot method sign.
byte[] sign(long mechanism, P11Params parameters, byte[] content, IaikP11Identity identity) throws P11TokenException {
ParamUtil.requireNonNull("content", content);
assertMechanismSupported(mechanism);
int len = content.length;
int expectedSignatureLen;
if (mechanism == PKCS11Constants.CKM_SHA_1_HMAC) {
expectedSignatureLen = 20;
} else if (mechanism == PKCS11Constants.CKM_SHA224_HMAC || mechanism == PKCS11Constants.CKM_SHA3_224) {
expectedSignatureLen = 28;
} else if (mechanism == PKCS11Constants.CKM_SHA256_HMAC || mechanism == PKCS11Constants.CKM_SHA3_256) {
expectedSignatureLen = 32;
} else if (mechanism == PKCS11Constants.CKM_SHA384_HMAC || mechanism == PKCS11Constants.CKM_SHA3_384) {
expectedSignatureLen = 48;
} else if (mechanism == PKCS11Constants.CKM_SHA512_HMAC || mechanism == PKCS11Constants.CKM_SHA3_512) {
expectedSignatureLen = 64;
} else if (mechanism == PKCS11Constants.CKM_VENDOR_SM2 || mechanism == PKCS11Constants.CKM_VENDOR_SM2_SM3) {
expectedSignatureLen = 32;
} else {
expectedSignatureLen = identity.getExpectedSignatureLen();
}
ConcurrentBagEntry<Session> session0 = borrowSession();
try {
Session session = session0.value();
if (len <= maxMessageSize) {
return singleSign(session, mechanism, parameters, content, identity);
}
Key signingKey = identity.getSigningKey();
Mechanism mechanismObj = getMechanism(mechanism, parameters);
if (LOG.isTraceEnabled()) {
LOG.debug("sign (init, update, then finish) with private key:\n{}", signingKey);
}
session.signInit(mechanismObj, signingKey);
for (int i = 0; i < len; i += maxMessageSize) {
int blockLen = Math.min(maxMessageSize, len - i);
// byte[] block = new byte[blockLen];
// System.arraycopy(content, i, block, 0, blockLen);
session.signUpdate(content, i, blockLen);
}
return session.signFinal(expectedSignatureLen);
} catch (TokenException ex) {
throw new P11TokenException(ex);
} finally {
sessions.requite(session0);
}
}
Aggregations