📄 context.c
字号:
/*-----------------------------------------------------------------------------
* File: context.c
*
Copyright (c) 1999 - 2002 Intel Corporation. All rights reserved
This software and associated documentation (if any) is furnished
under a license and may only be used or copied in accordance
with the terms of the license. Except as permitted by such
license, no part of this software or documentation may be
reproduced, stored in a retrieval system, or transmitted in any
form or by any means without the express written consent of
Intel Corporation.
*-----------------------------------------------------------------------------
*/
/*
* INTEL CONFIDENTIAL
* This file, software, or program is supplied under the terms of a
* license agreement or nondisclosure agreement with Intel Corporation
* and may not be copied or disclosed except in accordance with the
* terms of that agreement. This file, software, or program contains
* copyrighted material and/or trade secret information of Intel
* Corporation, and must be treated as such. Intel reserves all rights
* in this material, except as the license agreement or nondisclosure
* agreement specifically indicate.
*/
/*
* WARNING: EXPORT RESTRICTED.
* This software is subject to the U.S. Export Administration Regulations
* and other U.S. law, and may not be exported or re-exported to certain
* countries (currently Afghanistan (Taliban-controlled areas), Cuba, Iran,
* Iraq, Libya, North Korea, Serbia (except Kosovo), Sudan and Syria) or to
* persons or entities prohibited from receiving U.S. exports (including Denied
* Parties, Specially Designated Nationals, and entities on the Bureau of
* Export Administration Entity List or involved with missile technology or
* nuclear, chemical or biological weapons).
*/
/*
* This file contains the internal functions for managing the crypto context
*/
/*
* The CSSM_BIS implementation of this code was inspected 8/19/98.
*/
#include "internal.h"
#include "context.h"
cssm_CONTEXT_NODE_PTR ContextHead = NULL;
/*---------------------------------------------------------------
*Name: cssm_IsBadCryptoDataPtr
*
*Description:
* Determines whether this is a valid CryptoData structure.
* A valid CryptoData structure must contain either a Callback
* function pointer or a Param structure.
*
*Parameters:
* Data (input) - CryptoData structure to be checked
*
*Returns:
* CSSM_FALSE - valid crypto data structure
* CSSM_TRUE - invalid crypto data structure
*
*-------------------------------------------------------------*/
CSSM_BOOL cssm_IsBadCryptoDataPtr (CSSM_CRYPTO_DATA_PTR Data)
{
if (!Data)
return CSSM_TRUE;
if (!Data->Callback) {
if (!Data->Param)
return CSSM_TRUE;
if (!Data->Param->Length || !Data->Param->Data)
return CSSM_TRUE;
}
return CSSM_FALSE;
}
/*---------------------------------------------------------------
*Name: cssm_CopyDataStruct
*
*Description:
* Create a duplicate CSSM_DATA structure
*
*Parameters:
* Data (input) - pointer to the CSSM_DATA structure to copy
*
*Returns:
* NULL - unable to allocate sufficient memory
* non NULL - pointer to the new CSSM_DATA structure
*
*-------------------------------------------------------------*/
CSSM_DATA_PTR cssm_CopyDataStruct (CSSM_DATA_PTR Data)
{
CSSM_DATA_PTR Dest;
if (!Data) return NULL;
/* Allocate the CSSM_DATA struct */
if ((Dest = cssm_malloc (sizeof (CSSM_DATA), 0)) == NULL)
return NULL;
/* Allocate the actual data pointer */
if ((Dest->Data = cssm_malloc (Data->Length, 0)) == NULL) {
cssm_free (Dest, 0);
return NULL;
}
/* Copy the data */
Dest->Length = Data->Length;
cssm_memcpy(Dest->Data, Data->Data, Data->Length);
return Dest;
}
/*---------------------------------------------------------------
*Name: cssm_CreateContext
*
*Description:
* Create and initialize a context structure.
* Initialization includes allocating the attributes array
* and adding the CSPHandle attribute.
*
*Parameters:
* CSPHandle (input) - the CSP that will use this context
* Class (input) - class of context
* AlgorithmID (input) - algorithm for the crypto operation
* NumberAttributes (input) - the number of attributes to allocate.
* An additional attribute is allocated for the CSPHandle.
*
*Returns:
* NULL - unable to create the context
* non NULL - pointer to the new context
*
*-------------------------------------------------------------*/
CSSM_CONTEXT_PTR cssm_CreateContext (CSSM_CSP_HANDLE CSPHandle,
CSSM_CONTEXT_TYPE Class,
uint32 AlgorithmID,
uint32 NumberAttributes)
{
CSSM_CONTEXT_PTR Context;
/* CheckInit & ClearError are done in cssm_GetModuleRecord */
/* Make sure that the CSP handle is valid */
if (cssm_GetModuleRecord (CSPHandle, 0, NULL) == NULL) {
CSSM_SetError (&CssmGUID, CSSM_INVALID_CSP_HANDLE);
return NULL;
}
/* Allocate memory for the structure */
if ((Context = cssm_calloc (1, sizeof (CSSM_CONTEXT), 0)) == NULL) {
CSSM_SetError (&CssmGUID, CSSM_MEMORY_ERROR);
return NULL;
}
/* Initialize the context structure */
Context->ContextType = Class;
Context->AlgorithmType = AlgorithmID;
Context->CSPHandle = CSPHandle;
Context->NumberOfAttributes = NumberAttributes+1; /* +1 for the CSPHandle */
Context->ContextAttributes = cssm_calloc(NumberAttributes +1,
sizeof(CSSM_CONTEXT_ATTRIBUTE), 0);
if (!Context->ContextAttributes) {
cssm_free (Context, 0);
CSSM_SetError (&CssmGUID, CSSM_MEMORY_ERROR);
return NULL;
}
/* Add the CSPHandle attribute */
/* There are no failure cases for CSSM_ATTRIBUTE_CSP_HANDLE AddAttribute */
cssm_AddAttribute (&Context->ContextAttributes[NumberAttributes],
CSSM_ATTRIBUTE_CSP_HANDLE, (void*) CSPHandle);
return Context;
}
/*---------------------------------------------------------------
*Name: cssm_InsertContext
*
*Description:
* Insert the context into the list of context nodes
*
*Parameters:
* Context (input) - pointer to the context to be inserted
*
*Returns:
* CSSM_INVALID_HANDLE - memory could not be allocated
* non 0 - crypto handle associated with this context
*
*-------------------------------------------------------------*/
CSSM_CC_HANDLE cssm_InsertContext (CSSM_CONTEXT_PTR Context)
{
cssm_CONTEXT_NODE_PTR TempContext;
/* Allocate the context node */
TempContext = cssm_malloc(sizeof (cssm_CONTEXT_NODE), 0);
if (!TempContext) {
CSSM_SetError (&CssmGUID, CSSM_MEMORY_ERROR);
return CSSM_INVALID_HANDLE;
}
/* Initialize the context node */
TempContext->ContextHandle = cssm_GetHandle();
TempContext->Context = Context;
/* Add this node to the list of context nodes */
TempContext->Next = ContextHead;
ContextHead = TempContext;
#ifndef CSSM_BIS
{
cssm_ATTACHED_MODULE_NODE_PTR ModuleInfo;
/* Callback to csp to notify of context creation */
ModuleInfo = cssm_GetModuleRecord (TempContext->Context->CSPHandle, 0, NULL);
if (ModuleInfo && ModuleInfo->AddInJT)
if (ModuleInfo->AddInJT->EventNotify)
ModuleInfo->AddInJT->EventNotify (TempContext->Context->CSPHandle,
CSSM_EVENT_CREATE_CONTEXT,
TempContext->ContextHandle);
}
#endif
return TempContext->ContextHandle;
}
/*---------------------------------------------------------------
*Name: cssm_AddAttribute
*
*Description:
* Initialize the input attribute with a copy of the input data
*
*Parameters:
* Attribute (input/output) - pointer to attribute to be initialized
* Type (input) - type of attribute data
* Data (input) - pointer to attribute data
*
*Returns:
* CSSM_FAIL - sufficient memory could not be allocated
* CSSM_OK - Attribute was successfully initialized
*
*-------------------------------------------------------------*/
CSSM_RETURN cssm_AddAttribute (CSSM_CONTEXT_ATTRIBUTE_PTR Attribute,
CSSM_ATTRIBUTE_TYPE Type,
void *Data)
{
Attribute->AttributeType = Type;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -