📄 ospasn1primitives.c
字号:
errorcode = OSPPASN1ElementFormat(&eInfo, &tag, OSPC_BER_MODE_PRIMITIVE, 1, ospvData, ospvDataLength); if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1ObjectFormat(&encodedObject, eInfo, ospvDataRefId); } if (errorcode != OSPC_ERR_NO_ERROR) { OSPPASN1ElementDelete(&eInfo,0); OSPPASN1ObjectDelete(&encodedObject); } else { *ospvEncodedObject = encodedObject; } return errorcode; } /* Encodes small integer (represented by unsigned) as an ASN1 Integer. Only encodes in base 256. Only encodes unsigned integers. */ int OSPPASN1SmallIntegerEncode( OSPTASN1OBJECT **ospvEncodedObject, unsigned ospvInteger, OSPEASN1DATAREFID ospvDataRefId) { int errorcode = OSPC_ERR_NO_ERROR; unsigned char tag=0; unsigned char *tmpData=OSPC_OSNULL; unsigned tmpLength=0; /* Set up data and dataLength for octetstring - not much to do here */ /* ...except creating memory leaks */ tag = OSPC_TAG_TYPE_INTEGER; errorcode = OSPPASN1SmallInt2UnsignedChar(ospvInteger, 256, &tmpData, &tmpLength); if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1ObjectEncode(ospvEncodedObject, &tag, 1, OSPC_BER_MODE_PRIMITIVE, tmpData, tmpLength, ospvDataRefId); } if(OSPC_OSNULL!=tmpData) { OSPM_FREE(tmpData); } return errorcode; } int OSPPASN1IntegerGetSmallValue( OSPTASN1ELEMENTINFO *ospvElementInfo, int *ospvValue) { int errorcode = OSPC_ERR_NO_ERROR; unsigned char *digits = OSPC_OSNULL; unsigned int digitCount = 0; int smallValue = 0; unsigned i = 0; errorcode = OSPPASN1ElementTestContext(ospvElementInfo); if (errorcode == OSPC_ERR_NO_ERROR) { if (ospvValue == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_NULL_POINTER; OSPM_DBGERRORLOG(errorcode, "Invalid pointer to integer storage"); } } if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPC_ERR_ASN1_INTEGER_OVERFLOW; *ospvValue = 0; digits = ospvElementInfo->Content; digitCount = ospvElementInfo->ContentLength; if ((digits[0] & 0x8f) == 0) { for (i = 0 ; (i < digitCount) && (smallValue <= 0x7FFFFF) ; i++) { smallValue *= 256; smallValue += digits[i]; } if (i == digitCount) { *ospvValue = smallValue; } } } return errorcode; } /* SmallInt2UnsignedChar Converts integer valuses to unsigned char buffer containing the value in base specified. Unused bits in each byte are set to 1 except for the last byte in the unsigned char value. Does not support negative numbers. Use for base 256 and base 128 values in ASN1 format. */ int OSPPASN1SmallInt2UnsignedChar( unsigned ospvIntegerValue, unsigned ospvBase, unsigned char **ospvBuffer, unsigned *ospvBufferLength) { int errorcode = OSPC_ERR_NO_ERROR; unsigned ucCnt = 0; int i = 0; unsigned div = 0; unsigned char tmpBuffer[129]; unsigned char *ucPtr = OSPC_OSNULL; unsigned char mask = 0x00; div = ospvIntegerValue; switch(ospvBase) { case 256: mask = 0x00; break; case 128: mask = 0x80; break; default: errorcode = OSPC_ERR_ASN1_UNEXPECTED_INT_BASE; OSPM_DBGERRORLOG(errorcode, "Base other than 128 or 256 specified"); break; } if (errorcode == OSPC_ERR_NO_ERROR) { /* Create the unsigned char buffer - backwards */ ucCnt = 0; do { tmpBuffer[ucCnt++] = (unsigned char)(div % ospvBase); div /= ospvBase; } while(div); /* Create buffer for data */ OSPM_MALLOC(*ospvBuffer, unsigned char, ucCnt); if (ospvBuffer == OSPC_OSNULL) { errorcode = OSPC_ERR_ASN1_UNABLE_TO_ALLOCATE_SPACE; OSPM_DBGERRORLOG(errorcode, "Unable to allocate parse element"); } } if (errorcode == OSPC_ERR_NO_ERROR) { ucPtr = *ospvBuffer; *ospvBufferLength = ucCnt; OSPM_MEMSET(ucPtr, mask, ucCnt); for(i = (ucCnt-1) ;i >= 0; i--, ucPtr++) { *ucPtr |= tmpBuffer[i]; }; mask = (unsigned char)~mask; /* Complement Mask */ (*ospvBuffer)[ucCnt - 1] &= mask; /* Apply complement mask */ } return errorcode; } int OSPPASN1ObjectIdentifierEncode( OSPTASN1OBJECT **ospvEncodedObject, OSPEASN1ID ospvObjectIdentifier, OSPEASN1DATAREFID ospvDataRefId) { int errorcode = OSPC_ERR_NO_ERROR; unsigned char tag; unsigned char *tmpData; unsigned tmpLength; /* Set up data and dataLength for ObjectIdentifier - not much to do here */ tag = OSPC_TAG_TYPE_OBJECT_IDENTIFIER; errorcode = OSPPASN1IdGetValue(ospvObjectIdentifier, &tmpData, &tmpLength); if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1ObjectEncode(ospvEncodedObject, &tag, 1, OSPC_BER_MODE_PRIMITIVE, tmpData, tmpLength, ospvDataRefId); } return errorcode; } /***************************************** ALGORITHM ID ROUTINES *****************************************/ int OSPPASN1AlgorithmIdEncode( OSPTASN1OBJECT **ospvEncodedObject, OSPEASN1ID ospvAlgorithmId, OSPEASN1DATAREFID ospvDataRefId) { int errorcode = OSPC_ERR_NO_ERROR; OSPTASN1OBJECT *newObject = OSPC_OSNULL; OSPTASN1OBJECT *encodedObject = OSPC_OSNULL; OSPEASN1DATAREFID dataRefId = OSPEDRID_NOTDEFINED; int i = 0; OSPM_ARGUSED(ospvDataRefId); errorcode = OSPPASN1ObjectNew(&encodedObject, OSPEDRID_ALGORITHMID); for (i = 0 ;errorcode == OSPC_ERR_NO_ERROR ; i++) { switch(i) { case 0: /* Create Digest Algorithm OID*/ dataRefId = OSPEDRID_ALGID_OID; errorcode = OSPPASN1ObjectIdentifierEncode(&newObject, ospvAlgorithmId, dataRefId); break; case 1: /* Add Digest */ dataRefId = OSPEDRID_ALGID_ATTRIBUTES; newObject = OSPC_OSNULL; /* None required for supported algorithm suite */ break; case 2: /* Add NULL */ if(newObject) /* !!! PS */ { OSPM_FREE(newObject->ElementInfo->Element); OSPM_FREE(newObject->ElementInfo); } dataRefId = OSPEDRID_ALGID_TERMINATOR; errorcode = OSPPASN1NullEncode(&newObject); break; case 3: errorcode = OSPC_ERR_ASN1_PARSE_COMPLETE; break; default: errorcode = OSPC_ERR_PKCS7_ENCODING_ERROR; OSPM_DBGERRORLOG(errorcode, "Unknown case encountered encoding PKCS7 DigestInfoCreate"); } if (errorcode == OSPC_ERR_NO_ERROR) { /* Add new object to this object */ if (newObject != OSPC_OSNULL) { errorcode = OSPPASN1ObjectAddChild( encodedObject, newObject, dataRefId ); OSPM_FREE(newObject); 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(encodedObject, OSPEPTID_ALGORITHMID, OSPEDRID_ALGORITHMID); } if (errorcode == OSPC_ERR_NO_ERROR) { *ospvEncodedObject = encodedObject; } else { /* Clean up from errors */ OSPPASN1ObjectDelete(&encodedObject); } return errorcode; } /***************************************** NULL ROUTINES *****************************************/ int OSPPASN1NullEncode( OSPTASN1OBJECT **ospvEncodedObject) { int errorcode = OSPC_ERR_NO_ERROR; OSPTASN1ELEMENTINFO *eInfo = OSPC_OSNULL; OSPTASN1OBJECT *encodedObject = OSPC_OSNULL; unsigned char tag; /* Set up data and dataLength for octetstring - not much to do here */ tag = OSPC_TAG_TYPE_NULL; errorcode = OSPPASN1ElementFormat(&eInfo, &tag, OSPC_BER_MODE_PRIMITIVE, 1, OSPC_OSNULL, 0); if (errorcode == OSPC_ERR_NO_ERROR) { errorcode = OSPPASN1ObjectFormat(&encodedObject, eInfo, OSPEDRID_NOTDEFINED); } if (errorcode != OSPC_ERR_NO_ERROR) { OSPPASN1ElementDelete(&eInfo,0); OSPPASN1ObjectDelete(&encodedObject); } else { *ospvEncodedObject = encodedObject; } return errorcode; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -