Back to index

lightning-sunbird  0.9+nobinonly
ckhelper.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is the Netscape security libraries.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation.
00018  * Portions created by the Initial Developer are Copyright (C) 1994-2000
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Alternatively, the contents of this file may be used under the terms of
00024  * either the GNU General Public License Version 2 or later (the "GPL"), or
00025  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00026  * in which case the provisions of the GPL or the LGPL are applicable instead
00027  * of those above. If you wish to allow use of your version of this file only
00028  * under the terms of either the GPL or the LGPL, and not to allow others to
00029  * use your version of this file under the terms of the MPL, indicate your
00030  * decision by deleting the provisions above and replace them with the notice
00031  * and other provisions required by the GPL or the LGPL. If you do not delete
00032  * the provisions above, a recipient may use your version of this file under
00033  * the terms of any one of the MPL, the GPL or the LGPL.
00034  *
00035  * ***** END LICENSE BLOCK ***** */
00036 
00037 /*
00038  * ckhelper.h
00039  *
00040  * This file contains some helper utilities for interaction with cryptoki.
00041  */
00042 
00043 #ifndef CKHELPER_H
00044 #define CKHELPER_H
00045 
00046 #ifdef DEBUG
00047 static const char CKHELPER_CVS_ID[] = "@(#) $RCSfile: ckhelper.h,v $ $Revision: 1.19 $ $Date: 2005/01/20 02:25:47 $";
00048 #endif /* DEBUG */
00049 
00050 #ifndef NSSCKT_H
00051 #include "nssckt.h"
00052 #endif /* NSSCKT_H */
00053 
00054 PR_BEGIN_EXTERN_C
00055 
00056 /* Some globals to keep from constantly redeclaring common cryptoki
00057  * attribute types on the stack.
00058  */
00059 
00060 /* Boolean values */
00061 NSS_EXTERN_DATA const NSSItem g_ck_true;
00062 NSS_EXTERN_DATA const NSSItem g_ck_false;
00063 
00064 /* Object classes */
00065 NSS_EXTERN_DATA const NSSItem g_ck_class_cert;
00066 NSS_EXTERN_DATA const NSSItem g_ck_class_pubkey;
00067 NSS_EXTERN_DATA const NSSItem g_ck_class_privkey;
00068 
00069 #define NSS_CK_TEMPLATE_START(_template, attr, size)   \
00070     attr = _template;                                  \
00071     size = 0;
00072 
00073 #define NSS_CK_SET_ATTRIBUTE_ITEM(pattr, kind, item)  \
00074     (pattr)->type = kind;                             \
00075     (pattr)->pValue = (CK_VOID_PTR)(item)->data;      \
00076     (pattr)->ulValueLen = (CK_ULONG)(item)->size;     \
00077     (pattr)++;
00078 
00079 #define NSS_CK_SET_ATTRIBUTE_UTF8(pattr, kind, utf8)  \
00080     (pattr)->type = kind;                             \
00081     (pattr)->pValue = (CK_VOID_PTR)utf8;              \
00082     (pattr)->ulValueLen = (CK_ULONG)nssUTF8_Size(utf8, NULL); \
00083     if ((pattr)->ulValueLen) ((pattr)->ulValueLen)--; \
00084     (pattr)++;
00085 
00086 #define NSS_CK_SET_ATTRIBUTE_VAR(pattr, kind, var)    \
00087     (pattr)->type = kind;                             \
00088     (pattr)->pValue = (CK_VOID_PTR)&var;              \
00089     (pattr)->ulValueLen = (CK_ULONG)sizeof(var);      \
00090     (pattr)++;
00091 
00092 #define NSS_CK_SET_ATTRIBUTE_NULL(pattr, kind)        \
00093     (pattr)->type = kind;                             \
00094     (pattr)->pValue = (CK_VOID_PTR)NULL;              \
00095     (pattr)->ulValueLen = 0;                          \
00096     (pattr)++;
00097 
00098 #define NSS_CK_TEMPLATE_FINISH(_template, attr, size) \
00099     size = (attr) - (_template);                      \
00100     PR_ASSERT(size <= sizeof(_template)/sizeof(_template[0]));
00101 
00102 /* NSS_CK_ATTRIBUTE_TO_ITEM(attrib, item)
00103  *
00104  * Convert a CK_ATTRIBUTE to an NSSItem.
00105  */
00106 #define NSS_CK_ATTRIBUTE_TO_ITEM(attrib, item)         \
00107     if ((CK_LONG)(attrib)->ulValueLen > 0) {           \
00108        (item)->data = (void *)(attrib)->pValue;       \
00109        (item)->size = (PRUint32)(attrib)->ulValueLen; \
00110     } else {                                           \
00111        (item)->data = 0;                              \
00112        (item)->size = 0;                              \
00113     }
00114 
00115 #define NSS_CK_ATTRIBUTE_TO_BOOL(attrib, boolvar)        \
00116     if ((attrib)->ulValueLen > 0) {                      \
00117        if (*((CK_BBOOL*)(attrib)->pValue) == CK_TRUE) { \
00118            boolvar = PR_TRUE;                           \
00119        } else {                                         \
00120            boolvar = PR_FALSE;                          \
00121        }                                                \
00122     }
00123 
00124 #define NSS_CK_ATTRIBUTE_TO_ULONG(attrib, ulongvar)      \
00125     if ((attrib)->ulValueLen > 0) {                      \
00126        ulongvar = *((CK_ULONG*)(attrib)->pValue);       \
00127     }
00128 
00129 /* NSS_CK_ATTRIBUTE_TO_UTF8(attrib, str)
00130  *
00131  * Convert a CK_ATTRIBUTE to a string.
00132  */
00133 #define NSS_CK_ATTRIBUTE_TO_UTF8(attrib, str)      \
00134     str = (NSSUTF8 *)((attrib)->pValue);
00135 
00136 /* NSS_CK_ITEM_TO_ATTRIBUTE(item, attrib)
00137  *
00138  * Convert an NSSItem to a  CK_ATTRIBUTE.
00139  */
00140 #define NSS_CK_ITEM_TO_ATTRIBUTE(item, attrib)     \
00141     (attrib)->pValue = (CK_VOID_PTR)(item)->data;  \
00142     (attrib)->ulValueLen = (CK_ULONG)(item)->size; \
00143 
00144 /* Get an array of attributes from an object. */
00145 NSS_EXTERN PRStatus 
00146 nssCKObject_GetAttributes
00147 (
00148   CK_OBJECT_HANDLE object,
00149   CK_ATTRIBUTE_PTR obj_template,
00150   CK_ULONG count,
00151   NSSArena *arenaOpt,
00152   nssSession *session,
00153   NSSSlot *slot
00154 );
00155 
00156 /* Get a single attribute as an item. */
00157 NSS_EXTERN PRStatus
00158 nssCKObject_GetAttributeItem
00159 (
00160   CK_OBJECT_HANDLE object,
00161   CK_ATTRIBUTE_TYPE attribute,
00162   NSSArena *arenaOpt,
00163   nssSession *session,
00164   NSSSlot *slot,
00165   NSSItem *rvItem
00166 );
00167 
00168 NSS_EXTERN PRBool
00169 nssCKObject_IsAttributeTrue
00170 (
00171   CK_OBJECT_HANDLE object,
00172   CK_ATTRIBUTE_TYPE attribute,
00173   nssSession *session,
00174   NSSSlot *slot,
00175   PRStatus *rvStatus
00176 );
00177 
00178 NSS_EXTERN PRStatus 
00179 nssCKObject_SetAttributes
00180 (
00181   CK_OBJECT_HANDLE object,
00182   CK_ATTRIBUTE_PTR obj_template,
00183   CK_ULONG count,
00184   nssSession *session,
00185   NSSSlot  *slot
00186 );
00187 
00188 NSS_EXTERN PRBool
00189 nssCKObject_IsTokenObjectTemplate
00190 (
00191   CK_ATTRIBUTE_PTR objectTemplate, 
00192   CK_ULONG otsize
00193 );
00194 
00195 PR_END_EXTERN_C
00196 
00197 #endif /* CKHELPER_H */