[gpkcs11-dev] Problem storing objects in the software token
Status: Beta
Brought to you by:
cypherfox
|
From: Calogero <ca...@ti...> - 2002-10-03 20:00:03
|
I'm writing for my thesis an application that signs and verify messages, =
making use smart-card. I installed the gpkcs11 and the ceay sw token to =
test it but I had some problems storing objects in the sw token.
These are the steps to generate a key pairs:
1-I get a list of all the slots with a token present
2-I open a R/W session with a specified token
3-I log into the token=20
4-I generate the keys pair
and this is a simple code to do it:
int main(void){
CK_RV rv;
CK_ULONG ulSlotWithTokenCount;
CK_SLOT_ID_PTR pSlotWithTokenList;
CK_SESSION_HANDLE hSession;
int num=3D0;
char *user_pin=3D"12345678";
CK_BBOOL bTrue=3DTRUE;
CK_OBJECT_HANDLE hPublicKey, hPrivateKey;
CK_MECHANISM keyPair_mechanism=3D{CKM_RSA_PKCS_KEY_PAIR_GEN, =
NULL_PTR,0};
CK_ULONG modulusBits=3D768;
CK_BYTE publicExponent[]=3D{3};
CK_CHAR subject[]=3D{'T','e','s','t','i','n','g',' ','k','e','y','s'};
CK_BYTE id[]=3D{'1','2','3'};
CK_ATTRIBUTE publicKeyTemplate[]=3D{
{CKA_ENCRYPT,&bTrue,sizeof(bTrue)},
{CKA_VERIFY,&bTrue,sizeof(bTrue)},
{CKA_WRAP,&bTrue,sizeof(bTrue)},
{CKA_MODULUS_BITS,&modulusBits,sizeof(modulusBits)},
{CKA_PUBLIC_EXPONENT,publicExponent,sizeof(publicExponent)}
};
=20
CK_ATTRIBUTE privateKeyTemplate[]=3D{
{CKA_TOKEN,&bTrue,sizeof(bTrue)},
{CKA_PRIVATE,&bTrue,sizeof(bTrue)},
{CKA_SUBJECT, subject,sizeof(subject)},
{CKA_ID,id,sizeof(id)},
{CKA_SENSITIVE,&bTrue,sizeof(bTrue)},
{CKA_DECRYPT,&bTrue,sizeof(bTrue)},
{CKA_SIGN,&bTrue,sizeof(bTrue)},
{CKA_UNWRAP,&bTrue,sizeof(bTrue)}
};
=20
rv=3D C_Initialize(NULL_PTR);
=20
//Get list of all slots with a token present
rv=3D C_GetSlotList(TRUE, NULL_PTR, &ulSlotWithTokenCount);
cout<<"Number of slots with a token present: =
"<<int(ulSlotWithTokenCount)<<'\n';
pSlotWithTokenList =3D(CK_SLOT_ID_PTR) =
malloc(sizeof(CK_SLOT_ID)*ulSlotWithTokenCount);
rv =3D C_GetSlotList(TRUE,pSlotWithTokenList,&ulSlotWithTokenCount);
=20
//Open a R/W session with the specified token
num=3D0;
if(ulSlotWithTokenCount>1){
cout<<"Wich token (0- "<<(ulSlotWithTokenCount-1)<<"): ";
cin>>num;
cout<<'\n';
}
C_OpenSession(pSlotWithTokenList[num],CKF_SERIAL_SESSION | =
CKF_RW_SESSION,NULL,NULL,&hSession);
cout<<"R/W Session opened\n";
=20
//Login
rv=3DC_Login(hSession,CKU_USER,(CK_CHAR *)user_pin,strlen(user_pin));
cout<<"Login OK\n";
=20
//Generate KeyPair
=
rv=3DC_GenerateKeyPair(hSession,&keyPair_mechanism,publicKeyTemplate,5,pr=
ivateKeyTemplate,8,&hPublicKey,&hPrivateKey);
=20
C_CloseSession(hSession);
=20
free(pSlotWithTokenList);
rv=3D C_Finalize(NULL_PTR);
ASSERT(rv=3D=3DCKR_OK,"C_Finalize failed!.\n");
=20
exit(0);
}
It seems the easiest thing in the worl but this code doesn't work, or =
the sw token doesn't work. The log file reports this problem (look at =
the end):
=20
/* C_Initialize(): ...complete (OK) */
/* C_GetSlotList(): starting... (OK) */
/* C_GetSlotList(): Listing only slots with present tokens (OK) */
DO_FKT(C_GetSlotList, (TRUE,(nil),0xbffff888));
/* C_GetSlotList(): computing needed size of List-Array (OK) */
/* C_GetSlotList(): starting... (OK) */
/* C_GetSlotList(): Listing only slots with present tokens (OK) */
DO_FKT(C_GetSlotList, (TRUE,0x804ab78,0xbffff888));
/* C_GetSlotList(): *pulCount: 1(OK) */
/* C_GetSlotList(): returning *pulCount: 1(OK) */
/* C_GetSlotList(): ...complete (OK) */
/* C_OpenSession(): starting... (OK) */
DO_FKT(C_OpenSession, (0,6,(nil),(nil),0xbffff880));
/* CI_Ceay_OpenSession(): starting... (OK) */
/* CI_Ceay_ReadPersistentFile(): starting... (OK) */
/* CDB_Open(): no further objects (OK) */
/* CI_Ceay_ReadPersistentFile(): ...complete (OK) */
/* CI_Ceay_OpenSession(): ...complete (OK) */
/* C_OpenSession(): for Session 2...complete(OK) */
/* C_Login(): starting... (OK) */
DO_FKT(C_Login, (2,1,<opaque PIN>,8));
/* CI_Ceay_Login(): starting... (OK) */
/* CI_Ceay_TokenObjLoad(): starting... (OK) */
/* CDB_Open(): no further objects (OK) */
/* CI_Ceay_TokenObjLoad(): ...complete (OK) */
/* CI_Ceay_Login(): complete... (OK) */
/* CI_PropagateSessionState(): starting... (OK) */
/* CI_PropagateSessionState(): setting state to 3 for session 2(OK) */
/* CI_PropagateSessionState(): ...done (OK) */
/* C_Login(): ...complete (OK) */
/* C_GenerateKeyPair(): starting... (OK) */
DO_FKT(C_GenerateKeyPair, (2,{CKM_RSA_PKCS_KEY_PAIR_GEN, NULL_PTR, =
0x00000000},((ENCRYPT 01: #x00000001)
(VERIFY 01: #x00000001)
(WRAP 01: #x00000001)
(MODULUS_BITS 00:03:00:00: #x00000004)
(PUBLIC_EXPONENT 03: #x00000001)
),5,((TOKEN 01: #x00000001)
(PRIVATE 01: #x00000001)
(SUBJECT 54:65:73:74:69:6e:67:20:6b:65:79:73: #x0000000c)
(ID 31:32:33: #x00000003)
(SENSITIVE 01: #x00000001)
(DECRYPT 01: #x00000001)
(SIGN 01: #x00000001)
(UNWRAP 01: #x00000001)
),8,0xbffff870,0xbffff86c));
/* CI_ObjReadTemplate(): starting... (OK) */
/* CI_ObjReadTemplate(): ...complete (OK) */
/* CI_ObjReadTemplate(): starting... (OK) */
/* CI_ObjReadTemplate(): ...complete (OK) */
/* CI_Ceay_GenerateKeyPair(): starting... (OK) */
/* CI_Ceay_GenerateKeyPair(): warning: public key type not defined =
(TEMPLATE INCONSISTENT) */
/* CI_Ceay_GenerateKeyPair(): warning: private key type not defined =
(TEMPLATE INCONSISTENT) */
/* CI_ObjReadTemplate(): starting... (OK) */
/* CI_ObjReadTemplate(): ...complete (OK) */
/* CI_ObjReadTemplate(): starting... (OK) */
/* CI_ObjReadTemplate(): ...complete (OK) */
/* CI_Ceay_GeneratePair(): session_data is 0x804ab88(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime suspected: 0(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime suspected: 1(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime suspected: 2(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime suspected: 3(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime suspected: 4(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime rejected: 0(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime suspected: 0(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime suspected: 1(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime suspected: 2(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime suspected: 3(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime suspected: 4(OK) */
/* CI_Ceay_RSA_Callback(): callback for prime rejected: 1(OK) */
/* CI_ObjMergeObj(): starting... (OK) */
/* CI_ObjMergeObj(): ...complete (OK) */
/* CI_ObjMergeObj(): starting... (OK) */
/* CI_ObjMergeObj(): ...complete (OK) */
/* CI_Ceay_GenerateKeyPair(): ...complete (OK) */
/* CI_TokenObjAdd(): starting... (OK) */
/* CI_Ceay_TokenObjAdd(): starting... (OK) */
/* CI_Ceay_TokenObjAdd(): cache already loaded =
(IMPL_DATA(persistent_cache): 0x804ac18(OK) */ <----- HERE
I tried also to store a certificate, but when the CI_Ceay_TokenObjAdd() =
is called, the problem is the same: "cache already loaded".
If you know how I can resolve this problem and complete my thesis I'll =
be very grateful with you.
Thank you in very much.
Calogero.=20
ca...@ti...
|