decrypting PKCS7

2,671 views
Skip to first unread message

alex...@gmail.com

unread,
Mar 1, 2013, 6:29:04 AM3/1/13
to pkcs11...@googlegroups.com
Hello,

I hope i am posting at the right place.

I am creating PKCS7 messages with openssl with commands bellow :

echo "this is a secret message" > clear.txt


openssl smime -encrypt -binary -aes-256-cbc -in clear.txt -out message.pk7 -outform PEM   mydongle.crt

(where mydongle.crt is the public certificate of a private key that only exists on a given pkcs11 dongle)

i now have a PKCS7 message in message.pk7 file.

i can decrypt this pkcs7 file using openssl commands below

openssl> engine -t dynamic -pre SO_PATH:engine_pkcs11 -pre ID:pkcs11 -pre LIST_ADD:1 -pre LOAD -pre MODULE_PATH:opensc-pkcs11.dll
 ==> this loads pkcs11 engine


openssl> smime -decrypt  -binary  -aes-256-cbc -in message.pk7 -inform PEM -inkey slot_1-id_5738c6dee56eb2aa2ca401db18e8fc5669edaf60 -keyform engine -engine pkcs11

 ==> then openssl prompts for the dongle pin and displays "this is a secret message".

I want to implement the decryption in c#/.net.
Since i have not managed to make bouncycastle.net work with pkcs11 dongles so far, i am trying to use PKCS11Interop. but i am facing issues.


using pkcs11Interop:
pkcs11 lib is correctly initialized.
my dongle is listed in the first slot.
session has been opened with correct pin.
So far so good.
But for the rest, i'm kinda puzzled.
the sample 19_EncryptAndDecryptTest.cs describes a scenario where the key pair (private/public keys) is generated during encryption, and then immediately reused at decryption in the same function. In my case, the private key exists only in the dongle and never leaves it. It is generated once and for all months before i decrypt data.
i tried using :
session.Decrypt (new Mechanism( CKM.CKM_RSA_PKCS ), ObjectHandle keyHandle ???? , byte[] encryptedData ???)

but i don't know how to specify "use private key stored in dongle" as second parameter
+ i am not sure of the encryptedData parameter format

all i need with pkcs11 is decrypting (unwrapping?) the symetric Transkey stored. in this example it is :
1333527D55C54CE2F7A88BB3ACB86F042FACED71689AF6F99C8DAF8849B6F9F8E2570268F9ACF02D39FE7FEAC7606C91F4448A841FEFF5B64CE3868489D3C12E59EE1336C8C87067998E47BEDF7DF00ABF60669E030DDF88F9F735AD88AA78372537A7E74B745DA77EBC1EC724A58EB6F3F2FEA9AAAE4BB55135BF080AB4E8DC7BFBBCA919C4849F1141B2E0EA2CAFF7370F59B82445D80689FC9EF0A32E3C218F8A939F2B5AC802C4D55808C48E704E1E524CB6154EDDBA2C7F5C5E4B61108CE6EC4ED3CF8BBD49E4FC2D3478F31FE1DE6812A63D4BF40270E11523A54769A02695219AAEBE048EF1C98CB8EE5E50244DCFAF6B2E307DCE57C9FAD03ADE038F
so i tried also :

session.UnwrapKey( ???? )   <== i don't know where to put 1333...8F in the parameters to get the decrypted key back.

basically i'm stuck.

Anyone has an idea ?


cheers

alex

Jaroslav Imrich

unread,
Mar 1, 2013, 4:04:30 PM3/1/13
to pkcs11...@googlegroups.com
Hello Alex,

you will need to use both BouncyCastle (to process SMIME and CMS
structure you created with OpenSSL) and Pkcs11Interop (to access
private key stored on pkcs11 dongle).


> using pkcs11Interop:
> pkcs11 lib is correctly initialized.
> my dongle is listed in the first slot.
> session has been opened with correct pin.
> So far so good.
> But for the rest, i'm kinda puzzled.

You need to acquire ObjectHandle to the private key stored on your
token and you can use session.FindAllObjects() method for that. You
will need to construct the search template that describes the object
you are looking for - your private key. Looking at your OpenSSL
examples I would try following search template:

List<ObjectAttribute> objectAttributes = new List<ObjectAttribute>();
objectAttributes.Add(new ObjectAttribute(CKA.CKA_CLASS,
(uint)CKO.CKO_PRIVATE_KEY));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_KEY_TYPE, (uint)CKK.CKK_RSA));
objectAttributes.Add(new ObjectAttribute(CKA.CKA_ID, new byte[] {
0x57, 0x38, 0xc6, 0xde, 0xe5, 0x6e, 0xb2, 0xaa, 0x2c, 0xa4, 0x01,
0xdb, 0x18, 0xe8, 0xfc, 0x56, 0x69, 0xed, 0xaf, 0x60 }));

Please take a look at a code sample in 17_ObjectFindingTest.cs file
for more information.


> the sample 19_EncryptAndDecryptTest.cs describes a scenario where the key
> pair (private/public keys) is generated during encryption, and then
> immediately reused at decryption in the same function. In my case, the
> private key exists only in the dongle and never leaves it. It is generated
> once and for all months before i decrypt data.
> i tried using :
> session.Decrypt (new Mechanism( CKM.CKM_RSA_PKCS ), ObjectHandle keyHandle
> ???? , byte[] encryptedData ???)
>
> but i don't know how to specify "use private key stored in dongle" as second
> parameter
> + i am not sure of the encryptedData parameter format

Just use ObjectHandle of your private key you acquired with
session.FindAllObjects() method. With "mechanism" parameter you
specify the decryption algorithm and the format of "encryptedData"
parameter fully depends on the mechanism used. Please note that you
need to use the same mechanism (algorithm) OpenSSL used for data
encryption.


>
> all i need with pkcs11 is decrypting (unwrapping?) the symetric Transkey
> stored. in this example it is :
> 1333527D55C54CE2F7A88BB3ACB86F042FACED71689AF6F99C8DAF8849B6F9F8E2570268F9ACF02D39FE7FEAC7606C91F4448A841FEFF5B64CE3868489D3C12E59EE1336C8C87067998E47BEDF7DF00ABF60669E030DDF88F9F735AD88AA78372537A7E74B745DA77EBC1EC724A58EB6F3F2FEA9AAAE4BB55135BF080AB4E8DC7BFBBCA919C4849F1141B2E0EA2CAFF7370F59B82445D80689FC9EF0A32E3C218F8A939F2B5AC802C4D55808C48E704E1E524CB6154EDDBA2C7F5C5E4B61108CE6EC4ED3CF8BBD49E4FC2D3478F31FE1DE6812A63D4BF40270E11523A54769A02695219AAEBE048EF1C98CB8EE5E50244DCFAF6B2E307DCE57C9FAD03ADE038F
> so i tried also :
>
> session.UnwrapKey( ???? ) <== i don't know where to put 1333...8F in the
> parameters to get the decrypted key back.

You can use session.Decrypt() method to directly acquire the value of
symmetric AES key or you can use session.UnwrapKey() method to let
dongle decrypt and manage your AES key internally. In the latter case
UnwrapKey() does not give you the value of AES key but gives you just
ObjectHandle instead.

If you take the Decrypt() approach you should specify ObjectHandle of
your private key you acquired with session.FindAllObjects() method as
a value of "keyHandle" parameter and 1333...8F (256 bytes long byte
array) as a value of "encryptedData" parameter. Decrypt() method
should return the value of symmetric AES key that can be used to
perform pure software decryption (i.e. with BouncyCastle) of encrypted
message.

If you take the UnwrapKey() approach you should specify ObjectHandle
of your private key you acquired with session.FindAllObjects() method
as a value of "unwrappingKeyHandle" parameter and 1333...8F as a
value of "wrappedKey" parameter. UnwrapKey() method should return
ObjectHandle of decrypted symmetric AES key, You can use this handle
with session.Decrypt() and let your dongle perform decryption of
encrypted message.

Please note that you will also need to use BouncyCastle to parse
initialization vector (needed for AES encryption) out of CMS
structure.


Please reply to this mailing list if you need more information

--
Kind Regards

Jaroslav Imrich
http://www.pkcs11interop.net
Reply all
Reply to author
Forward
This conversation is locked
You cannot reply and perform actions on locked conversations.
0 new messages