I have generated a pair of RSA keys on a smart card with OpenSC tools and retrieved it as wrapped key blob encrypted with DKEK key:

sc-hsm-tool --create-dkek-share dkek/dkek-share-1.pbe
sc-hsm-tool --create-dkek-share dkek/dkek-share-2.pbe

sc-hsm-tool --initialize --dkek-shares 2
sc-hsm-tool --import-dkek-share dkek/dkek-share-1.pbe
sc-hsm-tool --import-dkek-share dkek/dkek-share-2.pbe
pkcs11-tool -l --pin 123456 --keypairgen --key-type rsa:2048 --id 11 --usage-sign
sc-hsm-tool --wrap-key wrap-key.bin --key-reference 1

I omitted output of the commands, since everything went as expected.

I now have encrypted key in wrap-key.bin file and both dkek-shares and associated passwords.

I have found plenty of tutorials how can I load this key on a new smart card (an obvious example: https://github.com/OpenSC/OpenSC/wiki/SmartCardHSM), but I don't have one and I still need to use the key.

How can I retrieve it? Since the DKEK is a 256-Bit AES key and I have all key components it should be possible, but I don't know how. I assume I first need to reconstruct the AES key from shares and then retrieve key data from wrap-key.bin file (some parts of the file are in plain text, so I figure that not the whole file is encrypted), but I could not find any information about it anywhere.

EDIT: wrap-key.bin is encoded in ASN.1 format. I suspect, that the encrypted part is an Octet String at the beginning, however I am unable to verify it without the key.

EDIT2: DKEK parts are formatted according to OpenSSL salted format http://justsolve.archiveteam.org/wiki/OpenSSL_salted_format but I was unable to decrypt them by simply providing the password. Perhaps some form of interaction with the card is required, but I doubt it. If that was the case, how would this allow to load keys on a brand new card, with no relation to the old one?


With the help from Andreas Schwier from CardContact I've managed to solve the problem.

First step is to isolate the encrypted portion of the wrapped key.

You can easily achieve it with openssl utilities:

[user@localhost opensc]$ openssl asn1parse -in wrap-key.bin -inform der
    0:d=0  hl=4 l=1692 cons: SEQUENCE          
    4:d=1  hl=4 l= 971 prim: OCTET STRING      [HEX DUMP]:65623B6828FE1D530C174687AF81978BC12FAB3A1AAF4D4A05FBD1E41D145E15A7D1B4A32CC1A801711B5B1A5CAC98FE8DEFF2DAB46FB72EA0DCDC10BF169EB19FE155A171378E33ED518D1BD9A2ADD75C822E033D4BB308D4EE9665E66B37F32342F1751A59141B48BF450A877F477B3E9EDBC467CE59CDD7AC08949494B2FD4D2B009F61F9F71FF55DD7579160C5A01CF1E38F50D71A8994E53F359EB9D506B481955802C27319763211EFA6490FB6CB92595D70E5162535167314703877842490FDB549D1ABC16E201838F560A951BEF5768DA273272A6233B4B951BFA9C7FD15AB69B11027435E8DC19F339FD36F1393E10404BA794CCE0D57311C2519F915F5D6A3C7C2F3E5859D784FC4C58B66F2C195D27EF11F8D6266C16C852A806EFB5B055BE6D1E44D791A0991B1AC211D4BD621CFCCC67E033260BA1DB299EEAAADBFE8ECF7EE2641BA87FACD18436D8BDE42DA4061078FC3986F4AC04B2914FE343A9484CE28D32D40444378AC8D97B38701AF47379A50388EDCD18556D6472C725F581D0FD04D538394B6E209B120590C6C1977DA998FCA40470DC14E812DD7AC62C50EBB301D644E741F7E13E122B3F23712F4992E6EC7581A4E41B247BFDC7D1CF8AC41D13190F9D7B52C082CA60DF89CE1CD284558B65FD1A318C45C0BD45DB0E40E1F2487425C2876F91A93C47510BEFD72C5E47ED923A3CD993E78BEB5C7907B7479B5430FF991EA047D7D81A2CC94887AF302D873DD1EC4D6606F3067794C1F521D27D8D88E53F25D200E3B62D6FC93C74E26E891F1689538C13514A8DA772821D364D53F8AC036C66EE4D1758F2DEA43E85506090CE29FEEBD65F874F698527895A692A7635FDEC8A44ECC2395650C80BA56516B70493300453327B268C3127EE0A7CF08E131A90A3430ECA955C78AE7411D211C1BC7DCF95CC6598FDB58B8A619E48AFD0D33D1D3B9F6F52280122D01DC914A0D612F2F2F7327DBF3D6060EA95E5491565CC1775E2A0750636AD42B2376C5ABC52DEA1AE71020EC55DF31AA8D58AAA61ED18994F36A7F1E97F1603D1C7A922D4309F73E81837B9AF64811F32170A3C0298C710176D5A97360F3FEB04C0B42C1E3265BEF9E09A20BDD2EEF6DDE40B686A534573C180A737D35B2765DA29974A23F1DD4AEE70781153582A36EE0253FDF9191A1F07E932332BA25C1D7BE59E813B1F0A27F526502F4ACBA6B508BBC30D1C1CCAF553B908F6BF41D6D466D6B43A6F35C1D47F54E839BCF1786705CB7C2BA3365B95244372246B4FC7FBCAE7F58D65601C48434BDE35731813860B4A9F9EB5F1B6E411AA39D2086C9237CF92702FE5159B29C3421F0064FB8B6E9F18735A95B0C13A1  
  979:d=1  hl=2 l=  46 cons: SEQUENCE          
  981:d=2  hl=2 l=  20 cons: SEQUENCE          
  983:d=3  hl=2 l=  11 prim: UTF8STRING        :Private Key
  996:d=3  hl=2 l=   2 prim: BIT STRING        
 1000:d=3  hl=2 l=   1 prim: OCTET STRING      [HEX DUMP]:01
 1003:d=2  hl=2 l=  10 cons: SEQUENCE          
 1005:d=3  hl=2 l=   1 prim: OCTET STRING      [HEX DUMP]:11
 1008:d=3  hl=2 l=   2 prim: BIT STRING        
 1012:d=3  hl=2 l=   1 prim: INTEGER           :01
 1015:d=2  hl=2 l=  10 cons: cont [ 1 ]        
 1017:d=3  hl=2 l=   8 cons: SEQUENCE          
 1019:d=4  hl=2 l=   2 cons: SEQUENCE          
 1021:d=5  hl=2 l=   0 prim: OCTET STRING      
 1023:d=4  hl=2 l=   2 prim: INTEGER           :0800
 1027:d=1  hl=4 l= 665 cons: appl [ 7 ]        
 1031:d=2  hl=5 l= 575 cons: appl [ 33 ]       
 1036:d=3  hl=5 l= 309 cons: appl [ 78 ]       
 1041:d=4  hl=3 l=   1 prim: appl [ 41 ]       
 1045:d=4  hl=2 l=   9 prim: appl [ 2 ]        
 1056:d=4  hl=5 l= 277 cons: appl [ 73 ]       
 1061:d=5  hl=2 l=  10 prim: OBJECT            :
 1073:d=5  hl=4 l= 256 prim: cont [ 1 ]        
 1333:d=5  hl=2 l=   3 prim: cont [ 2 ]        
 1338:d=4  hl=3 l=   9 prim: appl [ 32 ]       
 1350:d=3  hl=5 l= 256 prim: appl [ 55 ]       
 1611:d=2  hl=2 l=  16 prim: appl [ 2 ]                                                                                                                                                                        
 1629:d=2  hl=3 l=  64 prim: appl [ 55 ]                                                                                                                                                                       
[user@localhost opensc]$

The long OCTET STRING at the beginning contains the required data. Since I didn't have to isolate it more than once, I simply copy-pasted it from the terminal. Automation of this process is left as an exercise for the ambitious reader. Please note, that for security purposes I replaced actual data with randomly generated bytes.

In order to decipher the data I used SmartCard Shell and sc-hsm-workspace set of scripts.

SmartCard Shell is available here: http://www.openscdp.org/scsh3/index.html

The scripts are a little bit more difficult to acquire. You need SSL certificate loaded on a SmartCard and a web browser with support for authentication devices. Further instructions are available here: http://www.cardcontact.de/cdn/activation.html

Once you have access to Contact Card Developer Network, you have create an account and after it's approved by the administrator, you can download sc-hsm-workspace set of scripts. Set it as workspace directory for SmartCard Shell.

While none of the scripts is a solution itself, all necessary pieces of code are available.

Unfortunately the source code is provided with Information contained in this script is confidential and released under NDA clause, so I cannot post the full solution, but this pseudo-code should suffice to easily recreate it:

getEncryptedDkekShare() {
    // Can be copy-pasted from keymanager.js
    return encdkekshare;

getEncryptedDkekPassword() {
    // Can be copy-pasted from keymanager.js
    return pwd;

getDecryptedDkekShare(encdkekshare, pwd) {
    // Can be copy-pasted from SmartCardHSM.js,
    // please note that you should change line sending 
    // decrypted data to SmartCard to simple return
    return plain.left(32);

// The following part can be obtained from decrypt_keyblob.js
dkekshare1= getDecryptedDkekShare(getEncryptedDkekShare(), getEncryptedDkekPassword());
dkekshare2= getDecryptedDkekShare(getEncryptedDkekShare(), getEncryptedDkekPassword());

dkek = new DKEK();

keyblob = new ByteString("65623B..."); // Encrypted data obtained from wrap-key.bin


Security warning: the key will be dumped on console in plaintext.

I hope that the answer provide sufficient information to meet StackExchange's high standards without violating NDA. I suspect that similar solution can be derived from free, open source code of OpenSC library, but using sc-hsm-workspace scripts turned out to be easy, fast and convenient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.