upload private key to token return error n:5

1/22/2013 11:47:58 AM
Gravatar
Total Posts 3

upload private key to token return error n:5

hello

 

i want to upload private key from p12 file to token but get error n:5. I'm using bellow code:

X509Certificate2 cert = new X509Certificate2("c.pfx", "1234", X509KeyStorageFlags.Exportable);
if (!cert.HasPrivateKey)
{

}
AsymmetricAlgorithm privateKey = cert.PrivateKey;
string NameLabel = cert.GetNameInfo(X509NameType.SimpleName, false);
CryptokiCollection attList = new CryptokiCollection();
RSAParameters parameters1 = new RSAParameters();
if (privateKey is RSA)
{
PrivateKeyInfo privateKeyInfo = PrivateKeyInfoFactory.CreatePrivateKeyInfo(pair.Private);
byte[] serializedKey = privateKeyInfo.ToAsn1Object().GetDerEncoded();
RsaPrivateCrtKeyParameters keyParams = (RsaPrivateCrtKeyParameters)pair.Private;
//Private Key
parameters1 = ((RSA)cert.PrivateKey).ExportParameters(true);
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_CLASS, 0x03));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, 0x00));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_PRIVATE, true));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_SUBJECT, cert.SubjectName.RawData));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_ID, "10"));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_LABEL, NameLabel));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_TOKEN, true));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_MODULUS, parameters1.Modulus));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_PUBLIC_EXPONENT, parameters1.Exponent ));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_PRIVATE_EXPONENT, parameters1.D));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, Key.CKK_RSA));
// attList.Add(new ObjectAttribute(ObjectAttribute.CKH_CLOCK, true));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_MODIFIABLE, true));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_LOCAL, true));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_EXTRACTABLE, false));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_NEVER_EXTRACTABLE, true));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_SENSITIVE, true));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_ALWAYS_SENSITIVE, true));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_DERIVE, false));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_LOCAL, false));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_DECRYPT, true));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_SIGN, true));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_SIGN_RECOVER, false));
attList.Add(new ObjectAttribute(ObjectAttribute.CKA_UNWRAP, false));

session.Objects.Create(attList);
}

 

can you tell me what's wrong this code???

 

Best Regards

1/22/2013 2:26:27 PM
Gravatar
Total Posts 300
Ugo Chirico http://www.ugochirico.com

Re: upload private key to token return error n:5

Hi,

error n.5 is CKR_GENERAL_ERROR.

It is raised by the underlying PKCS#11 module and usually it means that the  token is in some invalid state such as not formatted or finished or something else.

You should refer to its documentations to know what cause that problem.