⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cryptkrn.h

📁 提供了很多种加密算法和CA认证及相关服务如CMP、OCSP等的开发
💻 H
📖 第 1 页 / 共 4 页
字号:
#define setMessageCertMgmtInfo( certMgmtInfo, mgmtCaKey, mgmtRequest ) \
		{ \
		( certMgmtInfo )->cryptCert = CRYPT_ERROR; \
		( certMgmtInfo )->caKey = ( mgmtCaKey ); \
		( certMgmtInfo )->request = ( mgmtRequest ); \
		}

/****************************************************************************
*																			*
*					Object Table Definition and Access Macros				*
*																			*
****************************************************************************/

/* The object manipulation functions are implemented as macros since they
   need to access data internal to the object whose location differs
   depending on the object type.  For example the code to lock an object
   needs to manipulate the object's internal mutex, which will be stored in
   different locations for different objects.  Because of this we use macros
   to do this and let the compiler sort out the memory location within the
   object.  It would be somewhat cleaner to do this as a function, but this
   would mean ensuring the object-management related information was stored
   at the same location in each object (at the start of the object), which
   isn't very practical for clonable objects which assume that ephemeral data
   is located at the end of the object.

   To convert from the external handles which are used to reference internal
   data structures to the internal structure itself, the kernel maintains an
   object property table which contains the information required to manage 
   access */

typedef struct {
	/* Object type and value */
	OBJECT_TYPE type;			/* Object type */
	void *objectPtr;			/* Object data */

	/* Object properties */
	int flags;					/* Internal-only, locked, etc */
	int actionFlags;			/* Permitted actions */
	int subType;				/* Object subtype for attribute ACL chk.*/
	int forwardCount;			/* Number of times ownership can be transferred */
	int usageCount;				/* Number of times obj.can be used */
	int referenceCount;			/* Number of references to this object */
	int inUse;					/* Whether object is busy processing a msg.*/
/*	time_t lastAccess;			// Last access time */

	/* Object methods */
	RESOURCE_MESSAGE_FUNCTION messageFunction;
								/* The object's message handler */
	/* Owning and dependent objects */
	CRYPT_USER owner;			/* Owner object handle */
	CRYPT_HANDLE dependentObject;	/* Dependent object (context or cert) */
	CRYPT_HANDLE dependentDevice;	/* Dependent crypto device */

	/* Variable-length fields */
	DECLARE_OWNERSHIP_VARS		/* Information on the object's owner */
	} OBJECT_INFO;

extern OBJECT_INFO *objectTable;
extern int objectTableSize;

/* The variables required to synchronise access to the object map */

DEFINE_LOCKING_VARS( objectTable )

/* The flags which apply to each object in the table */

#define OBJECT_FLAG_NONE		0x0000	/* Non-flag */
#define OBJECT_FLAG_NOTINITED	0x0001	/* Still being initialised */
#define OBJECT_FLAG_INTERNAL	0x0002	/* Internal-use only */
#define OBJECT_FLAG_LOCKED		0x0004	/* Security properties can't be modified */
#define OBJECT_FLAG_BUSY		0x0008	/* Busy with async.op */
#define OBJECT_FLAG_SIGNALLED	0x0010	/* In signalled state */
#define OBJECT_FLAG_HIGH		0x0020	/* In 'high' security state */

/* The flags which convey information about an object's status */

#define OBJECT_FLAGMASK_STATUS \
		( OBJECT_FLAG_NOTINITED | OBJECT_FLAG_BUSY | OBJECT_FLAG_SIGNALLED )

/* Map an external object handle to an object data pointer.  This macro is
   used in a number of the following macros, but should never be called
   directly since it doesn't perform any object locking.  The checks
   performed are as follows:

	Check that the handle refers to an object within the object table
	Check that the object isn't a cryptlib-internal object
	Check that the object is accessible to the caller
	Check that the object is of the requested type

   If all these checks succeed, a pointer to the object data is returned */

#define mapResourceHandle( handle, resType ) \
	( ( ( handle ) >= 0 && ( handle ) < objectTableSize && \
			!( objectTable[ handle ].flags & OBJECT_FLAG_INTERNAL ) && \
			checkObjectOwnership( objectTable[ handle ] ) && \
			objectTable[ handle ].type == resType ) ? \
			objectTable[ ( handle ) ].objectPtr : NULL )

/* Map an internal object handle to an object data pointer.  This macro is
   almost identical to mapResourceHandle() except that it doesn't check
   whether the handle is for internal use only */

#define mapInternalResourceHandle( handle, resType ) \
	( ( ( handle ) >= 0 && ( handle ) < objectTableSize && \
			objectTable[ handle ].type == resType ) ? \
			objectTable[ ( handle ) ].objectPtr : NULL )

/* Get an object, lock it for exclusive use, and exit with an error code if 
   there's a problem */

#define getCheckResource( handle, objectPtr, resType, errCode ) \
	{ \
	int flags; \
	\
	lockGlobalResource( objectTable ); \
	objectPtr = mapResourceHandle( handle, resType ); \
	if( objectPtr != NULL && \
		!( ( flags = objectTable[ ( handle ) ].flags ) & \
			 ( OBJECT_FLAG_BUSY | OBJECT_FLAG_SIGNALLED ) ) ) \
		{ lockResource( objectPtr ); } \
	unlockGlobalResource( objectTable ); \
	if( ( objectPtr ) == NULL ) \
		return( errCode ); \
	if( flags & ( OBJECT_FLAG_BUSY | OBJECT_FLAG_SIGNALLED ) ) \
		return( ( flags & OBJECT_FLAG_SIGNALLED ) ? \
				CRYPT_ERROR_SIGNALLED : CRYPT_ERROR_BUSY ); \
	}

/* A variant of the above which is only used with internal objects, so the 
   only way the object could be absent is if it was signalled.  Returning 
   any other form of error code in fact presents considerable difficulties 
   since internal objects are hidden from the user, and a return code of 
   (say) CRYPT_BADPARM2 wouldn't make much sense */

#define getCheckInternalResource( handle, objectPtr, resType ) \
	{ \
	lockGlobalResource( objectTable ); \
	objectPtr = mapInternalResourceHandle( handle, resType ); \
	if( objectPtr != NULL ) \
		{ lockResource( objectPtr ); } \
	unlockGlobalResource( objectTable ); \
	if( objectPtr == NULL ) \
		return( CRYPT_ERROR_SIGNALLED ); \
	}

/* Sometimes we have multiple objects which need to be unlocked on exit.
   The following variant of the previous macro unlocks an extra object
   before returning */

#define getCheckInternalResource2( handle, objectPtr, resType, secondObjectPtr ) \
	{ \
	lockGlobalResource( objectTable ); \
	objectPtr = mapInternalResourceHandle( handle, resType ); \
	if( objectPtr != NULL ) \
		{ lockResource( objectPtr ); } \
	unlockGlobalResource( objectTable ); \
	if( objectPtr == NULL ) \
		{ unlockResourceExit( secondObjectPtr, CRYPT_ERROR_SIGNALLED ); } \
	}

/* The following macros process object ownership information using the
   previously-defined primitives */

#ifndef checkObjectOwnership			/* May be defined to empty macro */
  #define checkObjectOwnership( objectPtr ) \
		( ( objectPtr## ).objectOwner == ( OWNERSHIP_VAR_TYPE ) CRYPT_UNUSED || \
		  ( objectPtr## ).objectOwner == getCurrentIdentity() )
  #define checkObjectOwned( objectPtr ) \
		( ( objectPtr## ).objectOwner != ( OWNERSHIP_VAR_TYPE ) CRYPT_UNUSED )
  #define getObjectOwnership( objectPtr ) \
		( objectPtr## )->objectOwner
  #define setObjectOwnership( objectPtr, owner ) \
		( objectPtr## )->objectOwner = ( OWNERSHIP_VAR_TYPE ) ( owner )
#endif /* checkObjectOwnership */

/* Since we typically unlock an object when we've finished using it, we
   combine the unlock and function exit in one macro */

#ifndef unlockResourceExit				/* May be defined to empty macro */
  #define unlockResourceExit( objectPtr, retCode )	\
		{ \
		unlockResource( objectPtr ); \
		return( retCode ); \
		}
#endif /* !unlockResourceExit */

/****************************************************************************
*																			*
*							Object Management Functions						*
*																			*
****************************************************************************/

/* Object management functions.  A dummy object is one which exists but
   doesn't have the capabilities of the actual object (for example an
   encryption context which just maps to underlying crypto hardware).  This
   doesn't affect krnlCreateObject(), but is used by the object-type-specific
   routines which decorate the results of krnlCreateObject() with object-
   specific extras */

#define CREATEOBJECT_FLAG_SECUREMALLOC \
									0x01	/* Use krnlMemAlloc() to alloc.*/
#define CREATEOBJECT_FLAG_DUMMY		0x02	/* Dummy obj.used as placeholder */

int krnlCreateObject( void **objectInfo, const CRYPT_USER owner,
					  const OBJECT_TYPE type, const int subType, 
					  const int objectSize, const int createObjectFlags, 
					  const int actionFlags,
					  RESOURCE_MESSAGE_FUNCTION messageFunction );
int krnlSendMessage( const int objectHandle,
					 const RESOURCE_MESSAGE_TYPE message,
					 void *messageDataPtr, const int messageValue );

/* Since some messages contain no data but act only as notifiers, we define
   the following macro to make using them less messy */

#define krnlSendNotifier( handle, message ) \
		krnlSendMessage( handle, message, NULL, 0 )

/* Semaphores and mutexes */

typedef enum {
	SEMAPHORE_NONE,					/* No semaphore */
	SEMAPHORE_DRIVERBIND,			/* Async driver bind */
	SEMAPHORE_LAST					/* Last semaphore */
	} SEMAPHORE_TYPE;

typedef enum {
	MUTEX_NONE,						/* No mutex */
	MUTEX_TRUSTINFO,				/* Certificate trust information */
	MUTEX_SOCKETPOOL,				/* Network socket pool */
	MUTEX_RANDOMPOLLING,			/* Randomness polling */
	MUTEX_NONCE,					/* Nonce generation */
	MUTEX_LAST						/* Last mutex */
	} MUTEX_TYPE;

/* Set/clear/wait on a semaphore, enter and exit a mutex */

void setSemaphore( const SEMAPHORE_TYPE semaphore, 
				   const SEMAPHORE_HANDLE object );
void clearSemaphore( const SEMAPHORE_TYPE semaphore );
void waitSemaphore( const SEMAPHORE_TYPE semaphore );
void enterMutex( const MUTEX_TYPE mutex );
void exitMutex( const MUTEX_TYPE mutex );

/* Register and deregister service functions to be called by cryptlibs
   background thread */

int registerServiceRoutine( void ( *serviceDispatchFunction )
	( const int object, void ( *serviceFunction )( void *info ) ),
	void ( *serviceFunction )( void *info ), const int object );
void deregisterServiceRoutine( const int serviceID );

/* Secure memory handling functions */

int krnlMemalloc( void **pointer, int size );
void krnlMemfree( void **pointer );
int krnlMemsize( const void *pointer );

#endif /* _CRYPTKRN_DEFINED */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -