some attribute and session cleanup

This commit is contained in:
ian.mcgreer%sun.com 2001-11-05 17:18:48 +00:00
Родитель c423baa81d
Коммит cd5938ad76
5 изменённых файлов: 140 добавлений и 38 удалений

Просмотреть файл

@ -32,7 +32,7 @@
*/ */
#ifdef DEBUG #ifdef DEBUG
static const char CVS_ID[] = "@(#) $RCSfile: ckhelper.c,v $ $Revision: 1.8 $ $Date: 2001-10-19 18:10:57 $ $Name: $"; static const char CVS_ID[] = "@(#) $RCSfile: ckhelper.c,v $ $Revision: 1.9 $ $Date: 2001-11-05 17:18:47 $ $Name: $";
#endif /* DEBUG */ #endif /* DEBUG */
#ifndef PKIT_H #ifndef PKIT_H
@ -90,34 +90,38 @@ nssCKObject_GetAttributes
NSSSlot *slot NSSSlot *slot
) )
{ {
nssArenaMark *mark; nssArenaMark *mark;
CK_SESSION_HANDLE hSession; CK_SESSION_HANDLE hSession;
CK_ULONG i; CK_ULONG i;
CK_RV ckrv; CK_RV ckrv;
PRStatus nssrv; PRStatus nssrv;
PRBool alloced = PR_FALSE; PRBool alloced = PR_FALSE;
hSession = session->handle; hSession = session->handle;
if (arenaOpt) { if (arenaOpt) {
mark = nssArena_Mark(arenaOpt); mark = nssArena_Mark(arenaOpt);
if (!mark) { if (!mark) {
goto loser; goto loser;
} }
} }
nssSession_EnterMonitor(session); nssSession_EnterMonitor(session);
/* XXX kinda hacky, if the storage size is already in the first template /* XXX kinda hacky, if the storage size is already in the first template
* item, then skip the alloc portion * item, then skip the alloc portion
*/ */
if (obj_template[0].ulValueLen == 0) { if (obj_template[0].ulValueLen == 0) {
/* Get the storage size needed for each attribute */ /* Get the storage size needed for each attribute */
ckrv = CKAPI(slot)->C_GetAttributeValue(hSession, ckrv = CKAPI(slot)->C_GetAttributeValue(hSession,
object, obj_template, count); object, obj_template, count);
if (ckrv != CKR_OK) { if (ckrv != CKR_OK &&
ckrv != CKR_ATTRIBUTE_TYPE_INVALID &&
ckrv != CKR_ATTRIBUTE_SENSITIVE)
{
nssSession_ExitMonitor(session); nssSession_ExitMonitor(session);
/* set an error here */ /* set an error here */
goto loser; goto loser;
} }
/* Allocate memory for each attribute. */ /* Allocate memory for each attribute. */
for (i=0; i<count; i++) { for (i=0; i<count; i++) {
if ((CK_LONG)obj_template[i].ulValueLen <= 0) continue;
obj_template[i].pValue = nss_ZAlloc(arenaOpt, obj_template[i].pValue = nss_ZAlloc(arenaOpt,
obj_template[i].ulValueLen); obj_template[i].ulValueLen);
if (!obj_template[i].pValue) { if (!obj_template[i].pValue) {
@ -126,25 +130,28 @@ nssCKObject_GetAttributes
} }
} }
alloced = PR_TRUE; alloced = PR_TRUE;
} }
/* Obtain the actual attribute values. */ /* Obtain the actual attribute values. */
ckrv = CKAPI(slot)->C_GetAttributeValue(hSession, ckrv = CKAPI(slot)->C_GetAttributeValue(hSession,
object, obj_template, count); object, obj_template, count);
nssSession_ExitMonitor(session); nssSession_ExitMonitor(session);
if (ckrv != CKR_OK) { if (ckrv != CKR_OK &&
ckrv != CKR_ATTRIBUTE_TYPE_INVALID &&
ckrv != CKR_ATTRIBUTE_SENSITIVE)
{
/* set an error here */ /* set an error here */
goto loser; goto loser;
} }
if (alloced && arenaOpt) { if (alloced && arenaOpt) {
nssrv = nssArena_Unmark(arenaOpt, mark); nssrv = nssArena_Unmark(arenaOpt, mark);
if (nssrv != PR_SUCCESS) { if (nssrv != PR_SUCCESS) {
goto loser; goto loser;
} }
} }
return PR_SUCCESS; return PR_SUCCESS;
loser: loser:
if (alloced) { if (alloced) {
if (arenaOpt) { if (arenaOpt) {
/* release all arena memory allocated before the failure. */ /* release all arena memory allocated before the failure. */
(void)nssArena_Release(arenaOpt, mark); (void)nssArena_Release(arenaOpt, mark);
} else { } else {
@ -154,8 +161,8 @@ loser:
nss_ZFreeIf(obj_template[j].pValue); nss_ZFreeIf(obj_template[j].pValue);
} }
} }
} }
return PR_FAILURE; return PR_FAILURE;
} }
NSS_IMPLEMENT PRStatus NSS_IMPLEMENT PRStatus
@ -230,3 +237,29 @@ nssCKObject_SetAttributes
} }
} }
/*
NSS_IMPLEMENT PRBool
nssCKObject_IsTokenObject
(
CK_OBJECT_HANDLE object
)
{
}
*/
NSS_IMPLEMENT PRBool
nssCKObject_IsTokenObjectTemplate
(
CK_ATTRIBUTE_PTR objectTemplate,
CK_ULONG otsize
)
{
CK_ULONG ul;
for (ul=0; ul<otsize; ul++) {
if (objectTemplate[ul].type == CKA_TOKEN) {
return (*((CK_BBOOL*)objectTemplate[ul].pValue) == CK_TRUE);
}
}
return PR_FALSE;
}

Просмотреть файл

@ -41,7 +41,7 @@
#define CKHELPER_H #define CKHELPER_H
#ifdef DEBUG #ifdef DEBUG
static const char CKHELPER_CVS_ID[] = "@(#) $RCSfile: ckhelper.h,v $ $Revision: 1.8 $ $Date: 2001-10-19 18:10:58 $ $Name: $"; static const char CKHELPER_CVS_ID[] = "@(#) $RCSfile: ckhelper.h,v $ $Revision: 1.9 $ $Date: 2001-11-05 17:18:47 $ $Name: $";
#endif /* DEBUG */ #endif /* DEBUG */
#ifdef NSS_3_4_CODE #ifdef NSS_3_4_CODE
@ -88,9 +88,11 @@ NSS_EXTERN_DATA const NSSItem g_ck_class_privkey;
* *
* Convert a CK_ATTRIBUTE to an NSSItem. * Convert a CK_ATTRIBUTE to an NSSItem.
*/ */
#define NSS_CK_ATTRIBUTE_TO_ITEM(attrib, item) \ #define NSS_CK_ATTRIBUTE_TO_ITEM(attrib, item) \
(item)->data = (void *)(attrib)->pValue; \ if ((CK_LONG)(attrib)->ulValueLen > 0) { \
(item)->size = (PRUint32)(attrib)->ulValueLen; \ (item)->data = (void *)(attrib)->pValue; \
(item)->size = (PRUint32)(attrib)->ulValueLen; \
}
/* NSS_CK_ATTRIBUTE_TO_UTF8(attrib, str) /* NSS_CK_ATTRIBUTE_TO_UTF8(attrib, str)
* *
@ -151,6 +153,13 @@ nssCKObject_SetAttributes
NSSSlot *slot NSSSlot *slot
); );
NSS_EXTERN PRBool
nssCKObject_IsTokenObjectTemplate
(
CK_ATTRIBUTE_PTR objectTemplate,
CK_ULONG otsize
);
PR_END_EXTERN_C PR_END_EXTERN_C
#endif /* CKHELPER_H */ #endif /* CKHELPER_H */

Просмотреть файл

@ -32,7 +32,7 @@
*/ */
#ifdef DEBUG #ifdef DEBUG
static const char CVS_ID[] = "@(#) $RCSfile: nss3hack.c,v $ $Revision: 1.1 $ $Date: 2001-10-11 16:33:38 $ $Name: $"; static const char CVS_ID[] = "@(#) $RCSfile: nss3hack.c,v $ $Revision: 1.2 $ $Date: 2001-11-05 17:18:48 $ $Name: $";
#endif /* DEBUG */ #endif /* DEBUG */
#ifndef PKIT_H #ifndef PKIT_H
@ -67,6 +67,45 @@ nssSession_ImportNSS3Session(NSSArena *arenaOpt,
return rvSession; return rvSession;
} }
NSS_IMPLEMENT nssSession *
nssSlot_CreateSession
(
NSSSlot *slot,
NSSArena *arenaOpt,
PRBool readWrite
)
{
nssSession *rvSession;
rvSession = nss_ZNEW(arenaOpt, nssSession);
if (!rvSession) {
return (nssSession *)NULL;
}
if (readWrite) {
rvSession->handle = PK11_GetRWSession(slot->pk11slot);
rvSession->isRW = PR_TRUE;
rvSession->slot = slot;
return rvSession;
} else {
return NULL;
}
}
NSS_IMPLEMENT PRStatus
nssSession_Destroy
(
nssSession *s
)
{
CK_RV ckrv = CKR_OK;
if (s) {
if (s->isRW) {
PK11_RestoreROSession(s->slot->pk11slot, s->handle);
}
nss_ZFreeIf(s);
}
return (ckrv == CKR_OK) ? PR_SUCCESS : PR_FAILURE;
}
static NSSSlot * static NSSSlot *
nssSlot_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot) nssSlot_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
{ {
@ -117,6 +156,7 @@ nssToken_CreateFromPK11SlotInfo(NSSTrustDomain *td, PK11SlotInfo *nss3slot)
} }
rvToken->slot = nssSlot_CreateFromPK11SlotInfo(td, nss3slot); rvToken->slot = nssSlot_CreateFromPK11SlotInfo(td, nss3slot);
rvToken->slot->token = rvToken; rvToken->slot->token = rvToken;
rvToken->defaultSession->slot = rvToken->slot;
return rvToken; return rvToken;
} }

Просмотреть файл

@ -32,7 +32,7 @@
*/ */
#ifdef DEBUG #ifdef DEBUG
static const char CVS_ID[] = "@(#) $RCSfile: slot.c,v $ $Revision: 1.9 $ $Date: 2001-10-11 16:33:38 $ $Name: $"; static const char CVS_ID[] = "@(#) $RCSfile: slot.c,v $ $Revision: 1.10 $ $Date: 2001-11-05 17:18:48 $ $Name: $";
#endif /* DEBUG */ #endif /* DEBUG */
#ifndef DEV_H #ifndef DEV_H
@ -438,6 +438,7 @@ nssSlot_SetPassword
return nssrv; return nssrv;
} }
#ifdef PURE_STAN
NSS_IMPLEMENT nssSession * NSS_IMPLEMENT nssSession *
nssSlot_CreateSession nssSlot_CreateSession
( (
@ -504,6 +505,7 @@ nssSession_Destroy
} }
return (ckrv == CKR_OK) ? PR_SUCCESS : PR_FAILURE; return (ckrv == CKR_OK) ? PR_SUCCESS : PR_FAILURE;
} }
#endif
NSS_IMPLEMENT PRStatus NSS_IMPLEMENT PRStatus
nssSession_EnterMonitor nssSession_EnterMonitor

Просмотреть файл

@ -32,7 +32,7 @@
*/ */
#ifdef DEBUG #ifdef DEBUG
static const char CVS_ID[] = "@(#) $RCSfile: token.c,v $ $Revision: 1.12 $ $Date: 2001-10-19 18:10:58 $ $Name: $"; static const char CVS_ID[] = "@(#) $RCSfile: token.c,v $ $Revision: 1.13 $ $Date: 2001-11-05 17:18:48 $ $Name: $";
#endif /* DEBUG */ #endif /* DEBUG */
#ifndef DEV_H #ifndef DEV_H
@ -241,16 +241,34 @@ nssToken_ImportObject
) )
{ {
nssSession *session; nssSession *session;
PRBool createdSession = PR_FALSE;
CK_OBJECT_HANDLE object; CK_OBJECT_HANDLE object;
CK_RV ckrv; CK_RV ckrv;
session = (sessionOpt) ? sessionOpt : tok->defaultSession; session = (sessionOpt) ? sessionOpt : tok->defaultSession;
if (nssCKObject_IsTokenObjectTemplate(objectTemplate, otsize)) {
if (sessionOpt) {
if (!nssSession_IsReadWrite(sessionOpt)) {
return CK_INVALID_HANDLE;
} else {
session = sessionOpt;
}
} else if (nssSession_IsReadWrite(tok->defaultSession)) {
session = tok->defaultSession;
} else {
session = nssSlot_CreateSession(tok->slot, NULL, PR_TRUE);
createdSession = PR_TRUE;
}
}
nssSession_EnterMonitor(session); nssSession_EnterMonitor(session);
ckrv = CKAPI(tok->slot)->C_CreateObject(session->handle, ckrv = CKAPI(tok->slot)->C_CreateObject(session->handle,
objectTemplate, otsize, objectTemplate, otsize,
&object); &object);
nssSession_ExitMonitor(session); nssSession_ExitMonitor(session);
if (createdSession) {
nssSession_Destroy(session);
}
if (ckrv != CKR_OK) { if (ckrv != CKR_OK) {
return CK_INVALID_KEY; return CK_INVALID_HANDLE;
} }
return object; return object;
} }