Генерация пары
Попытался генерировать пару Gost3410 по исходникам
final CK_MECHANISM mechanism = new CK_MECHANISM(RtPkcs11Constants.CKM_GOSTR3410_KEY_PAIR_GEN);
final CK_ATTRIBUTE[] publicKeyTemplate = (CK_ATTRIBUTE[]) (new CK_ATTRIBUTE()).toArray(9);
publicKeyTemplate[0].set(Pkcs11Constants.CKA_CLASS, Pkcs11Constants.CKO_PUBLIC_KEY);
publicKeyTemplate[1].set(Pkcs11Constants.CKA_LABEL, "Public Key");
publicKeyTemplate[2].set(Pkcs11Constants.CKA_ID, "GOST R 34.10-2001 sample keypair 1 ID (Aktiv Co.)");
publicKeyTemplate[3].set(Pkcs11Constants.CKA_KEY_TYPE, RtPkcs11Constants.CKK_GOSTR3410);
publicKeyTemplate[4].set(Pkcs11Constants.CKA_ENCRYPT, true);
publicKeyTemplate[5].set(Pkcs11Constants.CKA_TOKEN, true);
publicKeyTemplate[6].set(Pkcs11Constants.CKA_PRIVATE, false);
publicKeyTemplate[7].set(Pkcs11Constants.CKA_DERIVE, true);
publicKeyTemplate[8].set(RtPkcs11Constants.CKA_GOSTR3410_PARAMS, RtPkcs11Constants.GOST3410_PARAMS_OID);
final CK_ATTRIBUTE[] privateKeyTemplate = (CK_ATTRIBUTE[]) (new CK_ATTRIBUTE()).toArray(10);
privateKeyTemplate[0].set(Pkcs11Constants.CKA_CLASS, Pkcs11Constants.CKO_PRIVATE_KEY);
privateKeyTemplate[1].set(Pkcs11Constants.CKA_LABEL, "Private Key");
privateKeyTemplate[2].set(Pkcs11Constants.CKA_ID, "GOST R 34.10-2001 sample keypair 1 ID (Aktiv Co.)");
privateKeyTemplate[3].set(Pkcs11Constants.CKA_KEY_TYPE, RtPkcs11Constants.CKK_GOSTR3410);
privateKeyTemplate[4].set(Pkcs11Constants.CKA_DECRYPT, true);
privateKeyTemplate[5].set(Pkcs11Constants.CKA_TOKEN, true);
privateKeyTemplate[6].set(Pkcs11Constants.CKA_PRIVATE, true);
privateKeyTemplate[7].set(Pkcs11Constants.CKA_DERIVE, true);
privateKeyTemplate[8].set(RtPkcs11Constants.CKA_GOSTR3410_PARAMS, RtPkcs11Constants.GOST3410_PARAMS_OID);
privateKeyTemplate[9].set(RtPkcs11Constants.CKA_GOSTR3411_PARAMS, RtPkcs11Constants.GOST3411_PARAMS_OID);
NativeLongByReference phPublicKey = new NativeLongByReference(new NativeLong(0));
NativeLongByReference phPrivateKey = new NativeLongByReference(new NativeLong(0));
NativeLong rv = Pkcs11Constants.CKR_OK;
rv = mPkcs11.C_GenerateKeyPair(
mSession,
mechanism,
publicKeyTemplate,
new NativeLong(publicKeyTemplate.length),
privateKeyTemplate,
new NativeLong(privateKeyTemplate.length),
phPublicKey,
phPrivateKey);
if (!rv.equals(Pkcs11Constants.CKR_OK)) throw Pkcs11Exception.exceptionWithCode(rv);
дает ошибку CKR_TEMPLATE_INCONSISTENT (0xd1)
Какая может быть причина(ы)?
Вот сами функции set класса CK_ATTRIBUTE
public void set(@NonNull final NativeLong type, @NonNull final NativeLong value) {
this.type = type;
pValue = new NativeLongByReference(value).getPointer();
ulValueLen = new NativeLong(NativeLong.SIZE);
}
public void set(@NonNull final NativeLong type, @NonNull final boolean value) {
mBuffer = ByteBuffer.allocateDirect(1);
mBuffer.put(value? Pkcs11Constants.CK_TRUE : Pkcs11Constants.CK_FALSE);
pValue = Native.getDirectBufferPointer(mBuffer);
ulValueLen = new NativeLong(1);
}
public void set(@NonNull final NativeLong type, @NonNull final int value) {
this.type = type;
mBuffer = ByteBuffer.allocateDirect(4);
mBuffer.putInt(value);
pValue = Native.getDirectBufferPointer(mBuffer);
ulValueLen = new NativeLong(4);
}
public void set(@NonNull final NativeLong type, @NonNull final String value) {
this.type = type;
mBytes = value.getBytes();
mBuffer = ByteBuffer.allocateDirect(mBytes.length);
mBuffer.put(mBytes);
pValue = Native.getDirectBufferPointer(mBuffer);
ulValueLen = new NativeLong(mBytes.length);
}
public void set(@NonNull final NativeLong type, @NonNull final byte[] value) {
this.type = type;
mBuffer = ByteBuffer.allocateDirect(value.length);
mBuffer.put(value);
pValue = Native.getDirectBufferPointer(mBuffer);
ulValueLen = new NativeLong(value.length);
}