fipstokn.c

来自「支持SSL v2/v3, TLS, PKCS #5, PKCS #7, PKCS」· C语言 代码 · 共 952 行 · 第 1/2 页

C
952
字号
/* * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ *  * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. *  * The Original Code is the Netscape security libraries. *  * The Initial Developer of the Original Code is Netscape * Communications Corporation.  Portions created by Netscape are  * Copyright (C) 1994-2000 Netscape Communications Corporation.  All * Rights Reserved. *  * Contributor(s): *  * Alternatively, the contents of this file may be used under the * terms of the GNU General Public License Version 2 or later (the * "GPL"), in which case the provisions of the GPL are applicable  * instead of those above.  If you wish to allow use of your  * version of this file only under the terms of the GPL and not to * allow others to use your version of this file under the MPL, * indicate your decision by deleting the provisions above and * replace them with the notice and other provisions required by * the GPL.  If you do not delete the provisions above, a recipient * may use your version of this file under either the MPL or the * GPL. *//* * This file implements PKCS 11 on top of our existing security modules * * For more information about PKCS 11 See PKCS 11 Token Inteface Standard. *   This implementation has two slots: *	slot 1 is our generic crypto support. It does not require login *   (unless you've enabled FIPS). It supports Public Key ops, and all they *   bulk ciphers and hashes. It can also support Private Key ops for imported *   Private keys. It does not have any token storage. *	slot 2 is our private key support. It requires a login before use. It *   can store Private Keys and Certs as token objects. Currently only private *   keys and their associated Certificates are saved on the token. * *   In this implementation, session objects are only visible to the session *   that created or generated them. */#include "seccomon.h"#include "softoken.h"#include "key.h"#include "pkcs11.h"#include "pkcs11i.h"/* The next two strings must be exactly 64 characters long, with the   first 32 characters meaningful  */static char *slotDescription     = 	"Netscape Internal FIPS-140-1 Cryptographic Services             ";static char *privSlotDescription = 	"Netscape FIPS-140-1 User Private Key Services                   ";/* * Configuration utils */voidPK11_ConfigureFIPS(char *slotdes, char *pslotdes) {    if (slotdes && (PORT_Strlen(slotdes) == 65)) {	slotDescription = slotdes;    }    if (pslotdes && (PORT_Strlen(pslotdes) == 65)) {	privSlotDescription = pslotdes;    }    return;}/* * ******************** Password Utilities ******************************* */static PRBool isLoggedIn = PR_FALSE;static PRBool fatalError = PR_FALSE;/* Fips required checks before any useful crypto graphic services */static CK_RV pk11_fipsCheck(void) {    if (isLoggedIn != PR_TRUE) 	return CKR_USER_NOT_LOGGED_IN;    if (fatalError) 	return CKR_DEVICE_ERROR;    return CKR_OK;}#define PK11_FIPSCHECK() \    CK_RV rv; \    if ((rv = pk11_fipsCheck()) != CKR_OK) return rv;#define PK11_FIPSFATALCHECK() \    if (fatalError) return CKR_DEVICE_ERROR;/* grab an attribute out of a raw template */void *fc_getAttribute(CK_ATTRIBUTE_PTR pTemplate, 				CK_ULONG ulCount, CK_ATTRIBUTE_TYPE type){    int i;    for (i=0; i < (int) ulCount; i++) {	if (pTemplate[i].type == type) {	    return pTemplate[i].pValue;	}    }    return NULL;}#define __PASTE(x,y)	x##y/* ------------- forward declare all the NSC_ functions ------------- */#undef CK_NEED_ARG_LIST#undef CK_PKCS11_FUNCTION_INFO#define CK_PKCS11_FUNCTION_INFO(name) CK_RV __PASTE(NS,name)#define CK_NEED_ARG_LIST 1#include "pkcs11f.h"/* ------------- forward declare all the FIPS functions ------------- */#undef CK_NEED_ARG_LIST#undef CK_PKCS11_FUNCTION_INFO#define CK_PKCS11_FUNCTION_INFO(name) CK_RV __PASTE(F,name)#define CK_NEED_ARG_LIST 1#include "pkcs11f.h"/* ------------- build the CK_CRYPTO_TABLE ------------------------- */static CK_FUNCTION_LIST pk11_fipsTable = {    { 1, 10 },#undef CK_NEED_ARG_LIST#undef CK_PKCS11_FUNCTION_INFO#define CK_PKCS11_FUNCTION_INFO(name) __PASTE(F,name),#include "pkcs11f.h"};#undef CK_NEED_ARG_LIST#undef CK_PKCS11_FUNCTION_INFO#undef __PASTE/********************************************************************** * *     Start of PKCS 11 functions  * **********************************************************************//* return the function list */CK_RV FC_GetFunctionList(CK_FUNCTION_LIST_PTR *pFunctionList) {    *pFunctionList = &pk11_fipsTable;    return CKR_OK;}/* FC_Initialize initializes the PKCS #11 library. */CK_RV FC_Initialize(CK_VOID_PTR pReserved) {    CK_RV rv;    static PRBool init= PR_FALSE;    rv = PK11_LowInitialize(pReserved);    if (rv == CKR_OK && !init) {	init = PR_TRUE;	rv = PK11_SlotInit(FIPS_SLOT_ID,PR_TRUE);	/* fall through to check below */    }    /* not an 'else' rv can be set by either PK11_LowInit or PK11_SlotInit*/    if (rv != CKR_OK) {	fatalError = PR_TRUE;	return rv;    }    fatalError = PR_FALSE; /* any error has been reset */    rv = pk11_fipsPowerUpSelfTest();    if (rv != CKR_OK) {	fatalError = PR_TRUE;	return rv;    }    return CKR_OK;}/*FC_Finalize indicates that an application is done with the PKCS #11 library.*/CK_RV FC_Finalize (CK_VOID_PTR pReserved) {   /* this should free up FIPS Slot */   return NSC_Finalize (pReserved);}/* FC_GetInfo returns general information about PKCS #11. */CK_RV  FC_GetInfo(CK_INFO_PTR pInfo) {    return NSC_GetInfo(pInfo);}/* FC_GetSlotList obtains a list of slots in the system. */CK_RV FC_GetSlotList(CK_BBOOL tokenPresent,	 		CK_SLOT_ID_PTR pSlotList, CK_ULONG_PTR pulCount) {    *pulCount = 1;    if (pSlotList != NULL) {	pSlotList[0] = FIPS_SLOT_ID;    }    return CKR_OK;}	/* FC_GetSlotInfo obtains information about a particular slot in the system. */CK_RV FC_GetSlotInfo(CK_SLOT_ID slotID, CK_SLOT_INFO_PTR pInfo) {    CK_RV crv;    if (slotID != FIPS_SLOT_ID) return CKR_SLOT_ID_INVALID;    /* Use NETSCAPE_SLOT_ID as a basis so that we get Library version number,     * not key_DB version number */    crv = NSC_GetSlotInfo(NETSCAPE_SLOT_ID,pInfo);    if (crv != CKR_OK) {	return crv;    }    PORT_Memcpy(pInfo->slotDescription,slotDescription,64);    return CKR_OK;}/*FC_GetTokenInfo obtains information about a particular token in the system.*/ CK_RV FC_GetTokenInfo(CK_SLOT_ID slotID,CK_TOKEN_INFO_PTR pInfo) {    CK_RV crv;    if (slotID != FIPS_SLOT_ID) return CKR_SLOT_ID_INVALID;    /* use PRIVATE_KEY_SLOT_ID so we get the correct 						Authentication information */    crv = NSC_GetTokenInfo(PRIVATE_KEY_SLOT_ID,pInfo);    pInfo->flags |= CKF_RNG | CKF_LOGIN_REQUIRED;    /* yes virginia, FIPS can do random number generation:) */    return crv;}/*FC_GetMechanismList obtains a list of mechanism types supported by a token.*/ CK_RV FC_GetMechanismList(CK_SLOT_ID slotID,	CK_MECHANISM_TYPE_PTR pMechanismList, CK_ULONG_PTR pusCount) {    PK11_FIPSFATALCHECK();    if (slotID != FIPS_SLOT_ID) return CKR_SLOT_ID_INVALID;    /* FIPS Slot supports all functions */    return NSC_GetMechanismList(NETSCAPE_SLOT_ID,pMechanismList,pusCount);}/* FC_GetMechanismInfo obtains information about a particular mechanism  * possibly supported by a token. */ CK_RV FC_GetMechanismInfo(CK_SLOT_ID slotID, CK_MECHANISM_TYPE type,    					CK_MECHANISM_INFO_PTR pInfo) {    PK11_FIPSFATALCHECK();    if (slotID != FIPS_SLOT_ID) return CKR_SLOT_ID_INVALID;    /* FIPS Slot supports all functions */    return NSC_GetMechanismInfo(NETSCAPE_SLOT_ID,type,pInfo);}/* FC_InitToken initializes a token. */ CK_RV FC_InitToken(CK_SLOT_ID slotID,CK_CHAR_PTR pPin, 				CK_ULONG usPinLen,CK_CHAR_PTR pLabel) {    return CKR_HOST_MEMORY; /*is this the right function for not implemented*/}/* FC_InitPIN initializes the normal user's PIN. */ CK_RV FC_InitPIN(CK_SESSION_HANDLE hSession,    					CK_CHAR_PTR pPin, CK_ULONG ulPinLen) {    return NSC_InitPIN(hSession,pPin,ulPinLen);}/* FC_SetPIN modifies the PIN of user that is currently logged in. *//* NOTE: This is only valid for the PRIVATE_KEY_SLOT */ CK_RV FC_SetPIN(CK_SESSION_HANDLE hSession, CK_CHAR_PTR pOldPin,    CK_ULONG usOldLen, CK_CHAR_PTR pNewPin, CK_ULONG usNewLen) {    CK_RV rv;    if ((rv = pk11_fipsCheck()) != CKR_OK) return rv;    return NSC_SetPIN(hSession,pOldPin,usOldLen,pNewPin,usNewLen);}/* FC_OpenSession opens a session between an application and a token. */ CK_RV FC_OpenSession(CK_SLOT_ID slotID, CK_FLAGS flags,   CK_VOID_PTR pApplication,CK_NOTIFY Notify,CK_SESSION_HANDLE_PTR phSession) {    PK11_FIPSFATALCHECK();    return NSC_OpenSession(slotID,flags,pApplication,Notify,phSession);}/* FC_CloseSession closes a session between an application and a token. */ CK_RV FC_CloseSession(CK_SESSION_HANDLE hSession) {    return NSC_CloseSession(hSession);}/* FC_CloseAllSessions closes all sessions with a token. */ CK_RV FC_CloseAllSessions (CK_SLOT_ID slotID) {    return NSC_CloseAllSessions (slotID);}/* FC_GetSessionInfo obtains information about the session. */ CK_RV FC_GetSessionInfo(CK_SESSION_HANDLE hSession,						CK_SESSION_INFO_PTR pInfo) {    CK_RV rv;    PK11_FIPSFATALCHECK();    rv = NSC_GetSessionInfo(hSession,pInfo);    if (rv == CKR_OK) {	if ((isLoggedIn) && (pInfo->state == CKS_RO_PUBLIC_SESSION)) {		pInfo->state = CKS_RO_USER_FUNCTIONS;	}	if ((isLoggedIn) && (pInfo->state == CKS_RW_PUBLIC_SESSION)) {		pInfo->state = CKS_RW_USER_FUNCTIONS;	}    }    return rv;}/* FC_Login logs a user into a token. */ CK_RV FC_Login(CK_SESSION_HANDLE hSession, CK_USER_TYPE userType,				    CK_CHAR_PTR pPin, CK_ULONG usPinLen) {    CK_RV rv;    PK11_FIPSFATALCHECK();    rv = NSC_Login(hSession,userType,pPin,usPinLen);    if (rv == CKR_OK)	isLoggedIn = PR_TRUE;    else if (rv == CKR_USER_ALREADY_LOGGED_IN)    {	isLoggedIn = PR_TRUE;	/* Provide FIPS PUB 140-1 power-up self-tests on demand. */	rv = pk11_fipsPowerUpSelfTest();	if (rv == CKR_OK)		return CKR_USER_ALREADY_LOGGED_IN;	else		fatalError = PR_TRUE;    }    return rv;}/* FC_Logout logs a user out from a token. */ CK_RV FC_Logout(CK_SESSION_HANDLE hSession) {    PK11_FIPSCHECK();     rv = NSC_Logout(hSession);    isLoggedIn = PR_FALSE;    return rv;}/* FC_CreateObject creates a new object. */ CK_RV FC_CreateObject(CK_SESSION_HANDLE hSession,		CK_ATTRIBUTE_PTR pTemplate, CK_ULONG ulCount, 					CK_OBJECT_HANDLE_PTR phObject) {    CK_OBJECT_CLASS * classptr;    PK11_FIPSCHECK();    classptr = (CK_OBJECT_CLASS *)fc_getAttribute(pTemplate,ulCount,CKA_CLASS);    if (classptr == NULL) return CKR_TEMPLATE_INCOMPLETE;    /* FIPS can't create keys from raw key material */    if ((*classptr == CKO_SECRET_KEY) || (*classptr == CKO_PRIVATE_KEY)) {	return CKR_ATTRIBUTE_VALUE_INVALID;    }    return NSC_CreateObject(hSession,pTemplate,ulCount,phObject);}/* FC_CopyObject copies an object, creating a new object for the copy. */ CK_RV FC_CopyObject(CK_SESSION_HANDLE hSession,       CK_OBJECT_HANDLE hObject, CK_ATTRIBUTE_PTR pTemplate, CK_ULONG usCount,					CK_OBJECT_HANDLE_PTR phNewObject) {    PK11_FIPSCHECK();    return NSC_CopyObject(hSession,hObject,pTemplate,usCount,phNewObject);}/* FC_DestroyObject destroys an object. */ CK_RV FC_DestroyObject(CK_SESSION_HANDLE hSession,		 				CK_OBJECT_HANDLE hObject) {    PK11_FIPSCHECK();    return NSC_DestroyObject(hSession,hObject);}/* FC_GetObjectSize gets the size of an object in bytes. */ CK_RV FC_GetObjectSize(CK_SESSION_HANDLE hSession,    			CK_OBJECT_HANDLE hObject, CK_ULONG_PTR pusSize) {    PK11_FIPSCHECK();    return NSC_GetObjectSize(hSession, hObject, pusSize);}/* FC_GetAttributeValue obtains the value of one or more object attributes. */ CK_RV FC_GetAttributeValue(CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG usCount) {    PK11_FIPSCHECK();    return NSC_GetAttributeValue(hSession,hObject,pTemplate,usCount);}/* FC_SetAttributeValue modifies the value of one or more object attributes */ CK_RV FC_SetAttributeValue (CK_SESSION_HANDLE hSession, CK_OBJECT_HANDLE hObject,CK_ATTRIBUTE_PTR pTemplate,CK_ULONG usCount) {    PK11_FIPSCHECK();    return NSC_SetAttributeValue(hSession,hObject,pTemplate,usCount);}/* FC_FindObjectsInit initializes a search for token and session objects  * that match a template. */ CK_RV FC_FindObjectsInit(CK_SESSION_HANDLE hSession,    			CK_ATTRIBUTE_PTR pTemplate,CK_ULONG usCount) {    PK11_FIPSCHECK();    return NSC_FindObjectsInit(hSession,pTemplate,usCount);}/* FC_FindObjects continues a search for token and session objects  * that match a template, obtaining additional object handles. */ CK_RV FC_FindObjects(CK_SESSION_HANDLE hSession,    CK_OBJECT_HANDLE_PTR phObject,CK_ULONG usMaxObjectCount,    					CK_ULONG_PTR pusObjectCount) {    PK11_FIPSCHECK();    return NSC_FindObjects(hSession,phObject,usMaxObjectCount,    							pusObjectCount);}/* ************** Crypto Functions:     Encrypt ************************ *//* FC_EncryptInit initializes an encryption operation. */ CK_RV FC_EncryptInit(CK_SESSION_HANDLE hSession,		 CK_MECHANISM_PTR pMechanism, CK_OBJECT_HANDLE hKey) {    PK11_FIPSCHECK();    return NSC_EncryptInit(hSession,pMechanism,hKey);}/* FC_Encrypt encrypts single-part data. */ CK_RV FC_Encrypt (CK_SESSION_HANDLE hSession, CK_BYTE_PTR pData,    		CK_ULONG usDataLen, CK_BYTE_PTR pEncryptedData,					 CK_ULONG_PTR pusEncryptedDataLen) {    PK11_FIPSCHECK();    return NSC_Encrypt(hSession,pData,usDataLen,pEncryptedData,							pusEncryptedDataLen);}

⌨️ 快捷键说明

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