andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: /* andre@0: * ckhelper.h andre@0: * andre@0: * This file contains some helper utilities for interaction with cryptoki. andre@0: */ andre@0: andre@0: #ifndef CKHELPER_H andre@0: #define CKHELPER_H andre@0: andre@0: PR_BEGIN_EXTERN_C andre@0: andre@0: /* Some globals to keep from constantly redeclaring common cryptoki andre@0: * attribute types on the stack. andre@0: */ andre@0: andre@0: /* Boolean values */ andre@0: NSS_EXTERN_DATA const NSSItem g_ck_true; andre@0: NSS_EXTERN_DATA const NSSItem g_ck_false; andre@0: andre@0: /* Object classes */ andre@0: NSS_EXTERN_DATA const NSSItem g_ck_class_cert; andre@0: NSS_EXTERN_DATA const NSSItem g_ck_class_pubkey; andre@0: NSS_EXTERN_DATA const NSSItem g_ck_class_privkey; andre@0: andre@0: #define NSS_CK_TEMPLATE_START(_template, attr, size) \ andre@0: attr = _template; \ andre@0: size = 0; andre@0: andre@0: #define NSS_CK_SET_ATTRIBUTE_ITEM(pattr, kind, item) \ andre@0: (pattr)->type = kind; \ andre@0: (pattr)->pValue = (CK_VOID_PTR)(item)->data; \ andre@0: (pattr)->ulValueLen = (CK_ULONG)(item)->size; \ andre@0: (pattr)++; andre@0: andre@0: #define NSS_CK_SET_ATTRIBUTE_UTF8(pattr, kind, utf8) \ andre@0: (pattr)->type = kind; \ andre@0: (pattr)->pValue = (CK_VOID_PTR)utf8; \ andre@0: (pattr)->ulValueLen = (CK_ULONG)nssUTF8_Size(utf8, NULL); \ andre@0: if ((pattr)->ulValueLen) ((pattr)->ulValueLen)--; \ andre@0: (pattr)++; andre@0: andre@0: #define NSS_CK_SET_ATTRIBUTE_VAR(pattr, kind, var) \ andre@0: (pattr)->type = kind; \ andre@0: (pattr)->pValue = (CK_VOID_PTR)&var; \ andre@0: (pattr)->ulValueLen = (CK_ULONG)sizeof(var); \ andre@0: (pattr)++; andre@0: andre@0: #define NSS_CK_SET_ATTRIBUTE_NULL(pattr, kind) \ andre@0: (pattr)->type = kind; \ andre@0: (pattr)->pValue = (CK_VOID_PTR)NULL; \ andre@0: (pattr)->ulValueLen = 0; \ andre@0: (pattr)++; andre@0: andre@0: #define NSS_CK_TEMPLATE_FINISH(_template, attr, size) \ andre@0: size = (attr) - (_template); \ andre@0: PR_ASSERT(size <= sizeof(_template)/sizeof(_template[0])); andre@0: andre@0: /* NSS_CK_ATTRIBUTE_TO_ITEM(attrib, item) andre@0: * andre@0: * Convert a CK_ATTRIBUTE to an NSSItem. andre@0: */ andre@0: #define NSS_CK_ATTRIBUTE_TO_ITEM(attrib, item) \ andre@0: if ((CK_LONG)(attrib)->ulValueLen > 0) { \ andre@0: (item)->data = (void *)(attrib)->pValue; \ andre@0: (item)->size = (PRUint32)(attrib)->ulValueLen; \ andre@0: } else { \ andre@0: (item)->data = 0; \ andre@0: (item)->size = 0; \ andre@0: } andre@0: andre@0: #define NSS_CK_ATTRIBUTE_TO_BOOL(attrib, boolvar) \ andre@0: if ((attrib)->ulValueLen > 0) { \ andre@0: if (*((CK_BBOOL*)(attrib)->pValue) == CK_TRUE) { \ andre@0: boolvar = PR_TRUE; \ andre@0: } else { \ andre@0: boolvar = PR_FALSE; \ andre@0: } \ andre@0: } andre@0: andre@0: #define NSS_CK_ATTRIBUTE_TO_ULONG(attrib, ulongvar) \ andre@0: if ((attrib)->ulValueLen > 0) { \ andre@0: ulongvar = *((CK_ULONG*)(attrib)->pValue); \ andre@0: } andre@0: andre@0: /* NSS_CK_ATTRIBUTE_TO_UTF8(attrib, str) andre@0: * andre@0: * Convert a CK_ATTRIBUTE to a string. andre@0: */ andre@0: #define NSS_CK_ATTRIBUTE_TO_UTF8(attrib, str) \ andre@0: str = (NSSUTF8 *)((attrib)->pValue); andre@0: andre@0: /* NSS_CK_ITEM_TO_ATTRIBUTE(item, attrib) andre@0: * andre@0: * Convert an NSSItem to a CK_ATTRIBUTE. andre@0: */ andre@0: #define NSS_CK_ITEM_TO_ATTRIBUTE(item, attrib) \ andre@0: (attrib)->pValue = (CK_VOID_PTR)(item)->data; \ andre@0: (attrib)->ulValueLen = (CK_ULONG)(item)->size; \ andre@0: andre@0: /* Get an array of attributes from an object. */ andre@0: NSS_EXTERN PRStatus andre@0: nssCKObject_GetAttributes andre@0: ( andre@0: CK_OBJECT_HANDLE object, andre@0: CK_ATTRIBUTE_PTR obj_template, andre@0: CK_ULONG count, andre@0: NSSArena *arenaOpt, andre@0: nssSession *session, andre@0: NSSSlot *slot andre@0: ); andre@0: andre@0: /* Get a single attribute as an item. */ andre@0: NSS_EXTERN PRStatus andre@0: nssCKObject_GetAttributeItem andre@0: ( andre@0: CK_OBJECT_HANDLE object, andre@0: CK_ATTRIBUTE_TYPE attribute, andre@0: NSSArena *arenaOpt, andre@0: nssSession *session, andre@0: NSSSlot *slot, andre@0: NSSItem *rvItem andre@0: ); andre@0: andre@0: NSS_EXTERN PRBool andre@0: nssCKObject_IsAttributeTrue andre@0: ( andre@0: CK_OBJECT_HANDLE object, andre@0: CK_ATTRIBUTE_TYPE attribute, andre@0: nssSession *session, andre@0: NSSSlot *slot, andre@0: PRStatus *rvStatus andre@0: ); andre@0: andre@0: NSS_EXTERN PRStatus andre@0: nssCKObject_SetAttributes andre@0: ( andre@0: CK_OBJECT_HANDLE object, andre@0: CK_ATTRIBUTE_PTR obj_template, andre@0: CK_ULONG count, andre@0: nssSession *session, andre@0: NSSSlot *slot andre@0: ); andre@0: andre@0: NSS_EXTERN PRBool andre@0: nssCKObject_IsTokenObjectTemplate andre@0: ( andre@0: CK_ATTRIBUTE_PTR objectTemplate, andre@0: CK_ULONG otsize andre@0: ); andre@0: andre@0: PR_END_EXTERN_C andre@0: andre@0: #endif /* CKHELPER_H */