📄 osppkcs8.c
字号:
/**########################################################################*########################################################################*########################################################################* * COPYRIGHT (c) 1998, 1999 by TransNexus, LLC * * This software contains proprietary and confidential information * of TransNexus, LLC. Except as may be set forth in the license * agreement under which this software is supplied, use, disclosure, * or reproduction is prohibited without the prior, express, written* consent of TransNexus, LLC. * *******#########################################################################*#########################################################################*#########################################################################*//* * osppkcs8.c - PKCS #8 Private Key Information Standard */#include "osp.h"#include "ospdatatypes.h"#include "ospasn1.h"#include "osppkcs8.h"#include "ospcrypto.h"/* FUNCTION PROTOTYPES *//* ---------------------------------------------------------*//* Member functions *//* ---------------------------------------------------------*//* ---------------------------------------------------------*//* Non-Member functions *//* ---------------------------------------------------------*//* This routine assumes the private key passed by the caller is anRSAEncryption PKCS1 Private Key. It is converted to a OSPPASN1OBJECT andwrapped to become a PKCS8 PrivateKeyInfo Encoded object */intOSPPPKCS8KeyInfoCreate( OSPTASN1OBJECT **ospvPrivateKeyInfo, OSPTPRIVATEKEY *ospvPrivateKey){ int errorcode = OSPC_ERR_NO_ERROR; OSPEASN1DATAREFID dataRefId = OSPEDRID_NOTDEFINED; OSPTASN1OBJECT *privateKeyInfo = OSPC_OSNULL; OSPTASN1OBJECT *newObject = OSPC_OSNULL; OSPTASN1OBJECT *tmpObject = OSPC_OSNULL; unsigned char *rsaPrivateKey = OSPC_OSNULL; unsigned rsaPrivateKeyLength = 0; int i = 0; if (ospvPrivateKey == OSPC_OSNULL) { errorcode = OSPC_ERR_PKCS8_INVALID_PRIVATE_KEY_POINTER; OSPM_DBGERRORLOG(errorcode, "Private Key is NULL"); } if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1ObjectNew(&privateKeyInfo, OSPEDRID_PRIVATEKEYINFO); } /* The elements are a contentType OID, and a content element. */ /* Add the content type Element to the result list */ for (i = 0 ;errorcode == OSPC_ERR_NO_ERROR ; i++) { switch(i) { case 0: /* Add VERSION Element */ dataRefId = OSPEDRID_PVTKEYINF_VERSION; errorcode = OSPPASN1SmallIntegerEncode(&newObject, OSPC_ASN1_PRIVATEKEYINFO_VERSION, dataRefId); break; case 1: /* Add PrivateKey Algorithm */ dataRefId = OSPEDRID_PVTKEYINF_ALGORITHM; errorcode = OSPPASN1AlgorithmIdEncode(&tmpObject, OSPEID_RSAENCRYPTION, dataRefId); if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1ObjectCopy(&newObject, tmpObject); } OSPM_FREE(newObject->ElementInfo->Element); /* !!! PS */ OSPM_FREE(newObject->ElementInfo); /* !!! PS */ OSPPASN1ObjectDelete(&tmpObject); break; case 2: /* Add PrivateKey OctetString */ dataRefId = OSPEDRID_PVTKEYINF_PRIVATEKEY; rsaPrivateKey = ospvPrivateKey->PrivateKeyData; rsaPrivateKeyLength = ospvPrivateKey->PrivateKeyLength; errorcode = OSPPASN1OctetStringEncode(&newObject, rsaPrivateKey, rsaPrivateKeyLength, dataRefId); break; case 3: /* Add PrivateKey Attributes */ dataRefId = OSPEDRID_PVTKEYINF_ATTRIBUTES; newObject = OSPC_OSNULL;/* None required for rsaPrivateKey */ break; case 4: errorcode = OSPC_ERR_ASN1_PARSE_COMPLETE; break; default: errorcode = OSPC_ERR_PKCS7_ENCODING_ERROR; OSPM_DBGERRORLOG(errorcode, "Unknown case encountered encoding PKCS7 SignerInfo"); } if (errorcode == OSPC_ERR_NO_ERROR) { /* Add new object to this object */ if (newObject != OSPC_OSNULL) { errorcode = OSPPASN1ObjectAddChild(privateKeyInfo, newObject, dataRefId); OSPM_FREE(newObject); /* !!! PS */ newObject = OSPC_OSNULL; } } } if (errorcode == OSPC_ERR_ASN1_PARSE_COMPLETE) { errorcode = OSPC_ERR_NO_ERROR; } if (errorcode == OSPC_ERR_NO_ERROR) { /* Complete the encoding for this object. Update results, elements, etc. */ errorcode = OSPPASN1ObjectDeparse(privateKeyInfo, OSPEPTID_PRIVATEKEYINFO, OSPEDRID_PRIVATEKEYINFO); } if (errorcode == OSPC_ERR_NO_ERROR) { *ospvPrivateKeyInfo = privateKeyInfo; } else { /* Clean up from errors */ OSPPASN1ObjectDelete(&privateKeyInfo); } return errorcode;}intOSPPPKCS8KeyInfoTestContext( OSPTASN1OBJECT *ospvPrivateKeyInfo){ int errorcode = OSPC_ERR_NO_ERROR; if (ospvPrivateKeyInfo == OSPC_OSNULL) { errorcode = OSPC_ERR_PKCS8_INVALID_CONTEXT; OSPM_DBGERRORLOG(errorcode, "PKCS#1 Private Key context is null pointer"); } return errorcode;}void OSPPPKCS8KeyInfoDelete( OSPTASN1OBJECT **ospvPrivateKeyInfo){ OSPPASN1ObjectDelete(ospvPrivateKeyInfo);}intOSPPKCS8KeyInfoGetEncodedKeyInfo( OSPTASN1OBJECT *ospvPrivateKeyInfo, unsigned char **ospvBERPrivateKeyInfo, unsigned *ospvBERPrivateKeyInfoLength){ int errorcode = OSPC_ERR_NO_ERROR; OSPTASN1ELEMENTINFO *eInfo = OSPC_OSNULL; errorcode = OSPPPKCS8KeyInfoTestContext(ospvPrivateKeyInfo); if ( errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPPKCS8KeyInfoGetPrivateKeyElement(ospvPrivateKeyInfo, OSPEDRID_PRIVATEKEYINFO, &eInfo); } if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1ElementGetElementData(eInfo, ospvBERPrivateKeyInfo, ospvBERPrivateKeyInfoLength); } return errorcode;}intOSPPPKCS8KeyInfoGetPrivateKey( OSPTASN1OBJECT *ospvPrivateKeyInfo, unsigned char **ospvPrivateKey, unsigned *ospvPrivateKeyLength){ int errorcode = OSPC_ERR_NO_ERROR; OSPTASN1ELEMENTINFO *eInfo = OSPC_OSNULL; errorcode = OSPPPKCS8KeyInfoTestContext(ospvPrivateKeyInfo); if ( errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPPKCS8KeyInfoGetPrivateKeyElement(ospvPrivateKeyInfo, OSPEDRID_PVTKEYINF_PRIVATEKEY, &eInfo); } if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1ElementGetContentData(eInfo, ospvPrivateKey, ospvPrivateKeyLength); } return errorcode;}intOSPPPKCS8KeyInfoGetPrivateKeyElement( OSPTASN1OBJECT *ospvPrivateKeyInfo, OSPEASN1DATAREFID ospvDataRefId, OSPTASN1ELEMENTINFO **ospvElementInfo){ int errorcode = OSPC_ERR_NO_ERROR; OSPTASN1ELEMENTINFO *foundElement = OSPC_OSNULL; errorcode = OSPPPKCS8KeyInfoTestContext(ospvPrivateKeyInfo); if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1ElementGet(ospvDataRefId, ospvPrivateKeyInfo->ParseResults, &foundElement); } *ospvElementInfo = foundElement; return errorcode;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -