How to fix method C_GenerateKeyPair returned CKR_FUNCTION_FAILEDHow do I calculate someone's age in C#?How do I enumerate an enum in C#?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How do I update the GUI from another thread?How to loop through all enum values in C#?How to mark a method as obsolete or deprecated?How do I remedy the “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?What is a NullReferenceException, and how do I fix it?Hot to use mechanisms CKM_ECDH1_DERIVE with pkcs11interop

Meaning of 'lose their grip on the groins of their followers'

Why does the Mishnah use the terms poor person and homeowner when discussing carrying on Shabbat?

A word that means "blending into a community too much"

How to hide rifle during medieval town entrance inspection?

Is it safe to change the harddrive power feature so that it never turns off?

Electricity free spaceship

Who enforces MPAA rating adherence?

Generate basis elements of the Steenrod algebra

Non-aqueous eyes?

Check if three arrays contains the same element

How creative should the DM let an artificer be in terms of what they can build?

Second (easy access) account in case my bank screws up

Is an entry level DSLR going to shoot nice portrait pictures?

What is the maximum number of net attacks that one can make in a round?

A map of non-pathological topology?

Does the 2019 UA Artificer's Many-Handed Pouch infusion enable unlimited infinite-range cross-planar communication?

Why we don’t make use of the t-distribution for constructing a confidence interval for a proportion?

Fermat's statement about the ancients: How serious was he?

Getting UPS Power from One Room to Another

Does the Long March-11 increase its thrust after clearing the launch tower?

Why does logistic function use e rather than 2?

Why can I traceroute to this IP address, but not ping?

Let M and N be single-digit integers. If the product 2M5 x 13N is divisible by 36, how many ordered pairs (M,N) are possible?

Is it a bad idea to to run 24 tap and shock lands in standard



How to fix method C_GenerateKeyPair returned CKR_FUNCTION_FAILED


How do I calculate someone's age in C#?How do I enumerate an enum in C#?How to create Excel (.XLS and .XLSX) file in C# without installing Ms Office?How do I get a consistent byte representation of strings in C# without manually specifying an encoding?How do I update the GUI from another thread?How to loop through all enum values in C#?How to mark a method as obsolete or deprecated?How do I remedy the “The breakpoint will not currently be hit. No symbols have been loaded for this document.” warning?What is a NullReferenceException, and how do I fix it?Hot to use mechanisms CKM_ECDH1_DERIVE with pkcs11interop






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















I am trying to use the Pkcs11Interop library to get my own certificate from HSM(Safenet inc) when i have generated public/private key i got error "Method C_GenerateKeyPair returned CKR_FUNCTION_FAILED"



My code



if (Net.Pkcs11Interop.Common.Platform.Uses64BitRuntime)

loggerLibraryPath = @"C:inetpubwwwrootETPkcs11ETPkcsIIlibspkcs11-logger-x64.dll";

else

loggerLibraryPath = @"C:inetpubwwwrootETPkcs11ETPkcsIIlibspkcs11-logger-x86.dll";

System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LIBRARY_PATH", pkcs11LibraryPath);
System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LOG_FILE_PATH", loogerLogFilePath);
System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_FLAGS", "64");

if (System.IO.File.Exists(loogerLogFilePath))

System.IO.File.Delete(loogerLogFilePath);


using (Pkcs11 pkcs11 = new Pkcs11(loggerLibraryPath, AppType.SingleThreaded))

LibraryInfo libraryInfo = pkcs11.GetInfo();
var aviSlot = pkcs11.GetSlotList(SlotsType.WithTokenPresent).Where(slot => slot.GetSlotInfo().SlotFlags.TokenPresent).FirstOrDefault();

using (Session session = aviSlot.OpenSession(SessionType.ReadWrite))

// Login as normal user
session.Login(CKU.CKU_USER, "xxxxxxxx");
byte[] ckaId = session.GenerateRandom(20);

// Prepare attribute template of new public key
List<ObjectAttribute> publicKeyAttributes = new List<ObjectAttribute>();
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, false));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, Settings.ApplicationName));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ENCRYPT, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY_RECOVER, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_WRAP, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_MODULUS_BITS, 1024));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PUBLIC_EXPONENT, new byte[] 0x01, 0x00, 0x01 ));

// Prepare attribute template of new private key
List<ObjectAttribute> privateKeyAttributes = new List<ObjectAttribute>();
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, Settings.ApplicationName));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SENSITIVE, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_DECRYPT, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN_RECOVER, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_UNWRAP, true));

// Specify key generation mechanism
Mechanism mechanism = new Mechanism(CKM.CKM_RSA_PKCS_KEY_PAIR_GEN);

// Generate key pair
ObjectHandle publicKeyHandle = null;
ObjectHandle privateKeyHandle = null;
session.GenerateKeyPair(mechanism, publicKeyAttributes, privateKeyAttributes, out publicKeyHandle, out privateKeyHandle);

// Do something interesting with generated key pair
// Destroy keys
session.DestroyObject(privateKeyHandle);
session.DestroyObject(publicKeyHandle);

session.Logout();




Here are some of the log




0x00002478 : 0x00001af8 : Attribute 7

0x00002478 : 0x00001af8 : Attribute: 265 (CKA_SIGN_RECOVER)

0x00002478 : 0x00001af8 : pValue: 0597E850

0x00002478 : 0x00001af8 : ulValueLen: 1

0x00002478 : 0x00001af8 : *pValue: HEX(01)

0x00002478 : 0x00001af8 : Attribute 8

0x00002478 : 0x00001af8 : Attribute: 263 (CKA_UNWRAP)

0x00002478 : 0x00001af8 : pValue: 0597E830

0x00002478 : 0x00001af8 : ulValueLen: 1

0x00002478 : 0x00001af8 : pValue: HEX(01)

0x00002478 : 0x00001af8 :
End attribute template *

0x00002478 : 0x00001af8 : phPublicKey: 0643EA74

0x00002478 : 0x00001af8 : *phPublicKey: 0

0x00002478 : 0x00001af8 : phPrivateKey: 0643EA70

0x00002478 : 0x00001af8 : *phPrivateKey: 0

0x00002478 : 0x00001af8 : Returning 6 (CKR_FUNCTION_FAILED)

0x00002478 : 0x00001af8 : ****************************** 2019-03-22 16:37:32 *

0x00002478 : 0x00001af8 : Calling C_CloseSession

0x00002478 : 0x00001af8 : Input

0x00002478 : 0x00001af8 : hSession: 2490369

0x00002478 : 0x00001af8 : Returning 0 (CKR_OK)

0x00002478 : 0x00001af8 : ****************************** 2019-03-22 16:37:32 *


0x00002478 : 0x00001af8 : Calling C_Finalize

0x00002478 : 0x00001af8 : Input

0x00002478 : 0x00001af8 : pReserved: 00000000

0x00002478 : 0x00001af8 : Returning 0 (CKR_OK)











share|improve this question
























  • Please use proper formatting. if you want people to use their free time to solve YOUR problems, then you should at least take some time to put the problem into a presentable form. Proper indentation and line breaks are key to understanding code. you just dumping it in here is rude towards the people you expect to help you. I have done the work you should've done to begin with.

    – FalcoGer
    Mar 24 at 20:16






  • 2





    Try creating the Public Key and Private Key objects with a very minimal template configuration. To begin with, just set the token, label and id attributes in both the templates, and see if you are able to create the key pair objects. If you are successfully able to create them, try setting the other attributes you might need. And FYI, the public and private key objects might be related by the id, so try to use the same id in both the templates.

    – always_a_rookie_to_learn
    Mar 25 at 0:46











  • FalcoGer,Thank you for your suggestions and corrections.

    – Miracu
    Mar 25 at 4:21












  • always_a_rookie_to_learn , Thank you for your advice I've tried But still not successful.

    – Miracu
    Mar 25 at 4:26











  • Consider examining attributes of an existing key pair (generated by official client) and using similar values. An alternative way is to use pkcs11-logger (you seem to be already familiar with) to log templates used by the official client during key pair generation...Good luck!

    – vlp
    Apr 15 at 20:40

















1















I am trying to use the Pkcs11Interop library to get my own certificate from HSM(Safenet inc) when i have generated public/private key i got error "Method C_GenerateKeyPair returned CKR_FUNCTION_FAILED"



My code



if (Net.Pkcs11Interop.Common.Platform.Uses64BitRuntime)

loggerLibraryPath = @"C:inetpubwwwrootETPkcs11ETPkcsIIlibspkcs11-logger-x64.dll";

else

loggerLibraryPath = @"C:inetpubwwwrootETPkcs11ETPkcsIIlibspkcs11-logger-x86.dll";

System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LIBRARY_PATH", pkcs11LibraryPath);
System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LOG_FILE_PATH", loogerLogFilePath);
System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_FLAGS", "64");

if (System.IO.File.Exists(loogerLogFilePath))

System.IO.File.Delete(loogerLogFilePath);


using (Pkcs11 pkcs11 = new Pkcs11(loggerLibraryPath, AppType.SingleThreaded))

LibraryInfo libraryInfo = pkcs11.GetInfo();
var aviSlot = pkcs11.GetSlotList(SlotsType.WithTokenPresent).Where(slot => slot.GetSlotInfo().SlotFlags.TokenPresent).FirstOrDefault();

using (Session session = aviSlot.OpenSession(SessionType.ReadWrite))

// Login as normal user
session.Login(CKU.CKU_USER, "xxxxxxxx");
byte[] ckaId = session.GenerateRandom(20);

// Prepare attribute template of new public key
List<ObjectAttribute> publicKeyAttributes = new List<ObjectAttribute>();
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, false));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, Settings.ApplicationName));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ENCRYPT, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY_RECOVER, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_WRAP, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_MODULUS_BITS, 1024));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PUBLIC_EXPONENT, new byte[] 0x01, 0x00, 0x01 ));

// Prepare attribute template of new private key
List<ObjectAttribute> privateKeyAttributes = new List<ObjectAttribute>();
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, Settings.ApplicationName));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SENSITIVE, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_DECRYPT, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN_RECOVER, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_UNWRAP, true));

// Specify key generation mechanism
Mechanism mechanism = new Mechanism(CKM.CKM_RSA_PKCS_KEY_PAIR_GEN);

// Generate key pair
ObjectHandle publicKeyHandle = null;
ObjectHandle privateKeyHandle = null;
session.GenerateKeyPair(mechanism, publicKeyAttributes, privateKeyAttributes, out publicKeyHandle, out privateKeyHandle);

// Do something interesting with generated key pair
// Destroy keys
session.DestroyObject(privateKeyHandle);
session.DestroyObject(publicKeyHandle);

session.Logout();




Here are some of the log




0x00002478 : 0x00001af8 : Attribute 7

0x00002478 : 0x00001af8 : Attribute: 265 (CKA_SIGN_RECOVER)

0x00002478 : 0x00001af8 : pValue: 0597E850

0x00002478 : 0x00001af8 : ulValueLen: 1

0x00002478 : 0x00001af8 : *pValue: HEX(01)

0x00002478 : 0x00001af8 : Attribute 8

0x00002478 : 0x00001af8 : Attribute: 263 (CKA_UNWRAP)

0x00002478 : 0x00001af8 : pValue: 0597E830

0x00002478 : 0x00001af8 : ulValueLen: 1

0x00002478 : 0x00001af8 : pValue: HEX(01)

0x00002478 : 0x00001af8 :
End attribute template *

0x00002478 : 0x00001af8 : phPublicKey: 0643EA74

0x00002478 : 0x00001af8 : *phPublicKey: 0

0x00002478 : 0x00001af8 : phPrivateKey: 0643EA70

0x00002478 : 0x00001af8 : *phPrivateKey: 0

0x00002478 : 0x00001af8 : Returning 6 (CKR_FUNCTION_FAILED)

0x00002478 : 0x00001af8 : ****************************** 2019-03-22 16:37:32 *

0x00002478 : 0x00001af8 : Calling C_CloseSession

0x00002478 : 0x00001af8 : Input

0x00002478 : 0x00001af8 : hSession: 2490369

0x00002478 : 0x00001af8 : Returning 0 (CKR_OK)

0x00002478 : 0x00001af8 : ****************************** 2019-03-22 16:37:32 *


0x00002478 : 0x00001af8 : Calling C_Finalize

0x00002478 : 0x00001af8 : Input

0x00002478 : 0x00001af8 : pReserved: 00000000

0x00002478 : 0x00001af8 : Returning 0 (CKR_OK)











share|improve this question
























  • Please use proper formatting. if you want people to use their free time to solve YOUR problems, then you should at least take some time to put the problem into a presentable form. Proper indentation and line breaks are key to understanding code. you just dumping it in here is rude towards the people you expect to help you. I have done the work you should've done to begin with.

    – FalcoGer
    Mar 24 at 20:16






  • 2





    Try creating the Public Key and Private Key objects with a very minimal template configuration. To begin with, just set the token, label and id attributes in both the templates, and see if you are able to create the key pair objects. If you are successfully able to create them, try setting the other attributes you might need. And FYI, the public and private key objects might be related by the id, so try to use the same id in both the templates.

    – always_a_rookie_to_learn
    Mar 25 at 0:46











  • FalcoGer,Thank you for your suggestions and corrections.

    – Miracu
    Mar 25 at 4:21












  • always_a_rookie_to_learn , Thank you for your advice I've tried But still not successful.

    – Miracu
    Mar 25 at 4:26











  • Consider examining attributes of an existing key pair (generated by official client) and using similar values. An alternative way is to use pkcs11-logger (you seem to be already familiar with) to log templates used by the official client during key pair generation...Good luck!

    – vlp
    Apr 15 at 20:40













1












1








1


1






I am trying to use the Pkcs11Interop library to get my own certificate from HSM(Safenet inc) when i have generated public/private key i got error "Method C_GenerateKeyPair returned CKR_FUNCTION_FAILED"



My code



if (Net.Pkcs11Interop.Common.Platform.Uses64BitRuntime)

loggerLibraryPath = @"C:inetpubwwwrootETPkcs11ETPkcsIIlibspkcs11-logger-x64.dll";

else

loggerLibraryPath = @"C:inetpubwwwrootETPkcs11ETPkcsIIlibspkcs11-logger-x86.dll";

System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LIBRARY_PATH", pkcs11LibraryPath);
System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LOG_FILE_PATH", loogerLogFilePath);
System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_FLAGS", "64");

if (System.IO.File.Exists(loogerLogFilePath))

System.IO.File.Delete(loogerLogFilePath);


using (Pkcs11 pkcs11 = new Pkcs11(loggerLibraryPath, AppType.SingleThreaded))

LibraryInfo libraryInfo = pkcs11.GetInfo();
var aviSlot = pkcs11.GetSlotList(SlotsType.WithTokenPresent).Where(slot => slot.GetSlotInfo().SlotFlags.TokenPresent).FirstOrDefault();

using (Session session = aviSlot.OpenSession(SessionType.ReadWrite))

// Login as normal user
session.Login(CKU.CKU_USER, "xxxxxxxx");
byte[] ckaId = session.GenerateRandom(20);

// Prepare attribute template of new public key
List<ObjectAttribute> publicKeyAttributes = new List<ObjectAttribute>();
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, false));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, Settings.ApplicationName));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ENCRYPT, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY_RECOVER, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_WRAP, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_MODULUS_BITS, 1024));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PUBLIC_EXPONENT, new byte[] 0x01, 0x00, 0x01 ));

// Prepare attribute template of new private key
List<ObjectAttribute> privateKeyAttributes = new List<ObjectAttribute>();
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, Settings.ApplicationName));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SENSITIVE, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_DECRYPT, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN_RECOVER, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_UNWRAP, true));

// Specify key generation mechanism
Mechanism mechanism = new Mechanism(CKM.CKM_RSA_PKCS_KEY_PAIR_GEN);

// Generate key pair
ObjectHandle publicKeyHandle = null;
ObjectHandle privateKeyHandle = null;
session.GenerateKeyPair(mechanism, publicKeyAttributes, privateKeyAttributes, out publicKeyHandle, out privateKeyHandle);

// Do something interesting with generated key pair
// Destroy keys
session.DestroyObject(privateKeyHandle);
session.DestroyObject(publicKeyHandle);

session.Logout();




Here are some of the log




0x00002478 : 0x00001af8 : Attribute 7

0x00002478 : 0x00001af8 : Attribute: 265 (CKA_SIGN_RECOVER)

0x00002478 : 0x00001af8 : pValue: 0597E850

0x00002478 : 0x00001af8 : ulValueLen: 1

0x00002478 : 0x00001af8 : *pValue: HEX(01)

0x00002478 : 0x00001af8 : Attribute 8

0x00002478 : 0x00001af8 : Attribute: 263 (CKA_UNWRAP)

0x00002478 : 0x00001af8 : pValue: 0597E830

0x00002478 : 0x00001af8 : ulValueLen: 1

0x00002478 : 0x00001af8 : pValue: HEX(01)

0x00002478 : 0x00001af8 :
End attribute template *

0x00002478 : 0x00001af8 : phPublicKey: 0643EA74

0x00002478 : 0x00001af8 : *phPublicKey: 0

0x00002478 : 0x00001af8 : phPrivateKey: 0643EA70

0x00002478 : 0x00001af8 : *phPrivateKey: 0

0x00002478 : 0x00001af8 : Returning 6 (CKR_FUNCTION_FAILED)

0x00002478 : 0x00001af8 : ****************************** 2019-03-22 16:37:32 *

0x00002478 : 0x00001af8 : Calling C_CloseSession

0x00002478 : 0x00001af8 : Input

0x00002478 : 0x00001af8 : hSession: 2490369

0x00002478 : 0x00001af8 : Returning 0 (CKR_OK)

0x00002478 : 0x00001af8 : ****************************** 2019-03-22 16:37:32 *


0x00002478 : 0x00001af8 : Calling C_Finalize

0x00002478 : 0x00001af8 : Input

0x00002478 : 0x00001af8 : pReserved: 00000000

0x00002478 : 0x00001af8 : Returning 0 (CKR_OK)











share|improve this question
















I am trying to use the Pkcs11Interop library to get my own certificate from HSM(Safenet inc) when i have generated public/private key i got error "Method C_GenerateKeyPair returned CKR_FUNCTION_FAILED"



My code



if (Net.Pkcs11Interop.Common.Platform.Uses64BitRuntime)

loggerLibraryPath = @"C:inetpubwwwrootETPkcs11ETPkcsIIlibspkcs11-logger-x64.dll";

else

loggerLibraryPath = @"C:inetpubwwwrootETPkcs11ETPkcsIIlibspkcs11-logger-x86.dll";

System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LIBRARY_PATH", pkcs11LibraryPath);
System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_LOG_FILE_PATH", loogerLogFilePath);
System.Environment.SetEnvironmentVariable("PKCS11_LOGGER_FLAGS", "64");

if (System.IO.File.Exists(loogerLogFilePath))

System.IO.File.Delete(loogerLogFilePath);


using (Pkcs11 pkcs11 = new Pkcs11(loggerLibraryPath, AppType.SingleThreaded))

LibraryInfo libraryInfo = pkcs11.GetInfo();
var aviSlot = pkcs11.GetSlotList(SlotsType.WithTokenPresent).Where(slot => slot.GetSlotInfo().SlotFlags.TokenPresent).FirstOrDefault();

using (Session session = aviSlot.OpenSession(SessionType.ReadWrite))

// Login as normal user
session.Login(CKU.CKU_USER, "xxxxxxxx");
byte[] ckaId = session.GenerateRandom(20);

// Prepare attribute template of new public key
List<ObjectAttribute> publicKeyAttributes = new List<ObjectAttribute>();
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, false));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, Settings.ApplicationName));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ENCRYPT, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_VERIFY_RECOVER, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_WRAP, true));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_MODULUS_BITS, 1024));
publicKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PUBLIC_EXPONENT, new byte[] 0x01, 0x00, 0x01 ));

// Prepare attribute template of new private key
List<ObjectAttribute> privateKeyAttributes = new List<ObjectAttribute>();
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_TOKEN, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_PRIVATE, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_LABEL, Settings.ApplicationName));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_ID, ckaId));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SENSITIVE, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_DECRYPT, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_SIGN_RECOVER, true));
privateKeyAttributes.Add(new ObjectAttribute(CKA.CKA_UNWRAP, true));

// Specify key generation mechanism
Mechanism mechanism = new Mechanism(CKM.CKM_RSA_PKCS_KEY_PAIR_GEN);

// Generate key pair
ObjectHandle publicKeyHandle = null;
ObjectHandle privateKeyHandle = null;
session.GenerateKeyPair(mechanism, publicKeyAttributes, privateKeyAttributes, out publicKeyHandle, out privateKeyHandle);

// Do something interesting with generated key pair
// Destroy keys
session.DestroyObject(privateKeyHandle);
session.DestroyObject(publicKeyHandle);

session.Logout();




Here are some of the log




0x00002478 : 0x00001af8 : Attribute 7

0x00002478 : 0x00001af8 : Attribute: 265 (CKA_SIGN_RECOVER)

0x00002478 : 0x00001af8 : pValue: 0597E850

0x00002478 : 0x00001af8 : ulValueLen: 1

0x00002478 : 0x00001af8 : *pValue: HEX(01)

0x00002478 : 0x00001af8 : Attribute 8

0x00002478 : 0x00001af8 : Attribute: 263 (CKA_UNWRAP)

0x00002478 : 0x00001af8 : pValue: 0597E830

0x00002478 : 0x00001af8 : ulValueLen: 1

0x00002478 : 0x00001af8 : pValue: HEX(01)

0x00002478 : 0x00001af8 :
End attribute template *

0x00002478 : 0x00001af8 : phPublicKey: 0643EA74

0x00002478 : 0x00001af8 : *phPublicKey: 0

0x00002478 : 0x00001af8 : phPrivateKey: 0643EA70

0x00002478 : 0x00001af8 : *phPrivateKey: 0

0x00002478 : 0x00001af8 : Returning 6 (CKR_FUNCTION_FAILED)

0x00002478 : 0x00001af8 : ****************************** 2019-03-22 16:37:32 *

0x00002478 : 0x00001af8 : Calling C_CloseSession

0x00002478 : 0x00001af8 : Input

0x00002478 : 0x00001af8 : hSession: 2490369

0x00002478 : 0x00001af8 : Returning 0 (CKR_OK)

0x00002478 : 0x00001af8 : ****************************** 2019-03-22 16:37:32 *


0x00002478 : 0x00001af8 : Calling C_Finalize

0x00002478 : 0x00001af8 : Input

0x00002478 : 0x00001af8 : pReserved: 00000000

0x00002478 : 0x00001af8 : Returning 0 (CKR_OK)








c# .net pkcs#11 pkcs11interop






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 25 at 5:04







Miracu

















asked Mar 24 at 19:12









MiracuMiracu

61




61












  • Please use proper formatting. if you want people to use their free time to solve YOUR problems, then you should at least take some time to put the problem into a presentable form. Proper indentation and line breaks are key to understanding code. you just dumping it in here is rude towards the people you expect to help you. I have done the work you should've done to begin with.

    – FalcoGer
    Mar 24 at 20:16






  • 2





    Try creating the Public Key and Private Key objects with a very minimal template configuration. To begin with, just set the token, label and id attributes in both the templates, and see if you are able to create the key pair objects. If you are successfully able to create them, try setting the other attributes you might need. And FYI, the public and private key objects might be related by the id, so try to use the same id in both the templates.

    – always_a_rookie_to_learn
    Mar 25 at 0:46











  • FalcoGer,Thank you for your suggestions and corrections.

    – Miracu
    Mar 25 at 4:21












  • always_a_rookie_to_learn , Thank you for your advice I've tried But still not successful.

    – Miracu
    Mar 25 at 4:26











  • Consider examining attributes of an existing key pair (generated by official client) and using similar values. An alternative way is to use pkcs11-logger (you seem to be already familiar with) to log templates used by the official client during key pair generation...Good luck!

    – vlp
    Apr 15 at 20:40

















  • Please use proper formatting. if you want people to use their free time to solve YOUR problems, then you should at least take some time to put the problem into a presentable form. Proper indentation and line breaks are key to understanding code. you just dumping it in here is rude towards the people you expect to help you. I have done the work you should've done to begin with.

    – FalcoGer
    Mar 24 at 20:16






  • 2





    Try creating the Public Key and Private Key objects with a very minimal template configuration. To begin with, just set the token, label and id attributes in both the templates, and see if you are able to create the key pair objects. If you are successfully able to create them, try setting the other attributes you might need. And FYI, the public and private key objects might be related by the id, so try to use the same id in both the templates.

    – always_a_rookie_to_learn
    Mar 25 at 0:46











  • FalcoGer,Thank you for your suggestions and corrections.

    – Miracu
    Mar 25 at 4:21












  • always_a_rookie_to_learn , Thank you for your advice I've tried But still not successful.

    – Miracu
    Mar 25 at 4:26











  • Consider examining attributes of an existing key pair (generated by official client) and using similar values. An alternative way is to use pkcs11-logger (you seem to be already familiar with) to log templates used by the official client during key pair generation...Good luck!

    – vlp
    Apr 15 at 20:40
















Please use proper formatting. if you want people to use their free time to solve YOUR problems, then you should at least take some time to put the problem into a presentable form. Proper indentation and line breaks are key to understanding code. you just dumping it in here is rude towards the people you expect to help you. I have done the work you should've done to begin with.

– FalcoGer
Mar 24 at 20:16





Please use proper formatting. if you want people to use their free time to solve YOUR problems, then you should at least take some time to put the problem into a presentable form. Proper indentation and line breaks are key to understanding code. you just dumping it in here is rude towards the people you expect to help you. I have done the work you should've done to begin with.

– FalcoGer
Mar 24 at 20:16




2




2





Try creating the Public Key and Private Key objects with a very minimal template configuration. To begin with, just set the token, label and id attributes in both the templates, and see if you are able to create the key pair objects. If you are successfully able to create them, try setting the other attributes you might need. And FYI, the public and private key objects might be related by the id, so try to use the same id in both the templates.

– always_a_rookie_to_learn
Mar 25 at 0:46





Try creating the Public Key and Private Key objects with a very minimal template configuration. To begin with, just set the token, label and id attributes in both the templates, and see if you are able to create the key pair objects. If you are successfully able to create them, try setting the other attributes you might need. And FYI, the public and private key objects might be related by the id, so try to use the same id in both the templates.

– always_a_rookie_to_learn
Mar 25 at 0:46













FalcoGer,Thank you for your suggestions and corrections.

– Miracu
Mar 25 at 4:21






FalcoGer,Thank you for your suggestions and corrections.

– Miracu
Mar 25 at 4:21














always_a_rookie_to_learn , Thank you for your advice I've tried But still not successful.

– Miracu
Mar 25 at 4:26





always_a_rookie_to_learn , Thank you for your advice I've tried But still not successful.

– Miracu
Mar 25 at 4:26













Consider examining attributes of an existing key pair (generated by official client) and using similar values. An alternative way is to use pkcs11-logger (you seem to be already familiar with) to log templates used by the official client during key pair generation...Good luck!

– vlp
Apr 15 at 20:40





Consider examining attributes of an existing key pair (generated by official client) and using similar values. An alternative way is to use pkcs11-logger (you seem to be already familiar with) to log templates used by the official client during key pair generation...Good luck!

– vlp
Apr 15 at 20:40












1 Answer
1






active

oldest

votes


















-1














Unfortunately PKCS#11 API does not provide any details on why C_GenerateKeyPair function failed but many PKCS#11 libraries support some kind of internal logging mechanism which may reveal the real cause of error. Exact steps needed to enable logging should be present in the documentation provided by the PKCS#11 library vendor.






share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327503%2fhow-to-fix-method-c-generatekeypair-returned-ckr-function-failed%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    -1














    Unfortunately PKCS#11 API does not provide any details on why C_GenerateKeyPair function failed but many PKCS#11 libraries support some kind of internal logging mechanism which may reveal the real cause of error. Exact steps needed to enable logging should be present in the documentation provided by the PKCS#11 library vendor.






    share|improve this answer



























      -1














      Unfortunately PKCS#11 API does not provide any details on why C_GenerateKeyPair function failed but many PKCS#11 libraries support some kind of internal logging mechanism which may reveal the real cause of error. Exact steps needed to enable logging should be present in the documentation provided by the PKCS#11 library vendor.






      share|improve this answer

























        -1












        -1








        -1







        Unfortunately PKCS#11 API does not provide any details on why C_GenerateKeyPair function failed but many PKCS#11 libraries support some kind of internal logging mechanism which may reveal the real cause of error. Exact steps needed to enable logging should be present in the documentation provided by the PKCS#11 library vendor.






        share|improve this answer













        Unfortunately PKCS#11 API does not provide any details on why C_GenerateKeyPair function failed but many PKCS#11 libraries support some kind of internal logging mechanism which may reveal the real cause of error. Exact steps needed to enable logging should be present in the documentation provided by the PKCS#11 library vendor.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Apr 7 at 21:14









        jariqjariq

        8,16122340




        8,16122340





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55327503%2fhow-to-fix-method-c-generatekeypair-returned-ckr-function-failed%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript