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