How to get the public key from Certificate

11/25/2011 12:26:48 PM
Gravatar
Total Posts 300
Ugo Chirico http://www.ugochirico.com

Re: How to get the public key from Certificate

How are you creating the public key object? Which template are you using?

And also, what native PKCS#11 are you using?

11/25/2011 1:03:50 PM
Gravatar
Total Posts 6

Re: How to get the public key from Certificate

CryptokiCollection template = new CryptokiCollection();
template.Add(new ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_PUBLIC_KEY));
template.Add(new ObjectAttribute(ObjectAttribute.CKA_KEY_TYPE, Key.CKK_RSA));
template.Add(new ObjectAttribute(ObjectAttribute.CKA_WRAP, false));
template.Add(new ObjectAttribute(ObjectAttribute.CKA_DERIVE, false));

template.Add(new ObjectAttribute(ObjectAttribute.CKA_TOKEN, false));
template.Add(new ObjectAttribute(ObjectAttribute.CKA_PRIVATE, false));
template.Add(new ObjectAttribute(ObjectAttribute.CKA_LABEL, "keyname"));
template.Add(new ObjectAttribute(ObjectAttribute.CKA_ID, "12"));
template.Add(new ObjectAttribute(ObjectAttribute.CKA_ENCRYPT, true));
template.Add(new ObjectAttribute(ObjectAttribute.CKA_PUBLIC_EXPONENT, exponent));
template.Add(new ObjectAttribute(ObjectAttribute.CKA_MODULUS, modulus));

CryptokiObject pubKey = session.Objects.Create(template);

 

exponent and modulus are forementioned byte arrays.

 

I'm using cmP11.dll from Charismathics.

I tried OpenSC but it doesn't support encryption so I'm a little helpless.

11/30/2011 4:07:41 PM
Gravatar
Total Posts 6

Re: How to get the public key from Certificate

Ok, I finally got it working.

The exponent and modulus were encoded so the lengths didn't match to create a working key object.

Here the code snippet which exports the values, hope it helps if someone else encounters the same problem.

 

System.Security.Cryptography.X509Certificates.X509Certificate2 x509cert = Cryptware.NCryptoki.Utils.ConvertCertificate((X509Certificate)certs[0]);


byte[] hexponent = null;
byte[] hmodulus = null;
RSACryptoServiceProvider helpkey = x509cert.PublicKey.Key as RSACryptoServiceProvider;
if (helpkey != null)
{
RSAParameters parameters = helpkey.ExportParameters(false);
hexponent = parameters.Exponent;
hmodulus = parameters.Modulus;
}

 

where certs is a CryptokiCollection.

5/22/2014 8:58:21 AM
Gravatar
Total Posts 8

How to export the public key to a file

Hi, 

I have generated the key pairs using these codes 

CryptokiCollection templatePub = new CryptokiCollection();
            templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_PUBLIC_KEY));
            templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_TOKEN, true));
            templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_PRIVATE, true));
            templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_LABEL, "PUKey"));
            templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_ID, "1"));
            templatePub.Add(new ObjectAttribute(ObjectAttribute.CKA_MODULUS_BITS, 1024));

            CryptokiCollection templatePri = new CryptokiCollection();
            templatePri.Add(new ObjectAttribute(ObjectAttribute.CKA_CLASS, CryptokiObject.CKO_PRIVATE_KEY));
            templatePri.Add(new ObjectAttribute(ObjectAttribute.CKA_TOKEN, true));
            templatePri.Add(new ObjectAttribute(ObjectAttribute.CKA_PRIVATE, true));
            templatePri.Add(new ObjectAttribute(ObjectAttribute.CKA_LABEL, "PRKey"));
            templatePri.Add(new ObjectAttribute(ObjectAttribute.CKA_ID, "1"));

            //Generate the key pair
            Key[] keys = session.GenerateKeyPair(Mechanism.RSA_PKCS_KEY_PAIR_GEN, templatePub, templatePri);
            RSAPrivateKey privateKey = (RSAPrivateKey)keys[1];
            RSAPublicKey publicKey = (RSAPublicKey)keys[0];

Now How can i export this public key to a file. ?