📄 osptnepinit.c
字号:
*ospvCACertB64Out = caCertB64; *ospvCACertB64LenOut = caCertB64Len; } OSPM_DBGEXIT(( "EXIT: OSPPExtractCACertFromResponse\n" )); return retVal;}/* Now take the enrollment parameter's CA certificate fingerprint in hex * ( which should have been taken from a secure channel, such as paper * [ if ubiquity can be considered security ] or an SSL session with the * enrollment server's accompanying web site ( if available. ) If the * fingerprint is missing, then an error will not be generated; we'll assume * for now ( although this is debatable ) that the fingerprint isn't * necessary. If any of the other parameters are otherwise null, then * this function will fail. If the CA certificate is missing, then this * function will fail regardless of whether or not we care about checking * for the fingerprint. This may be changed too ( just rearrange the ordering * of the statements for checking the null values. ) * * Input: a pointer to the enrollment parameters, which contains the CA * certificate and the CA's intended fingerprint. * * Output: an error code if anything is null or if the fingerprint cannot be * taken or doesn't match up to what's expected. */int OSPPValidateCACertificate( OSPTENROLLPARAMS* ospvEnrollParams){ int retVal = OSPC_ERR_NO_ERROR; unsigned char* caFprint = OSPC_OSNULL; OSPTASN1OBJECT* caCertASN1Object = OSPC_OSNULL; int caFprintLen = -1; /* The binary version of the input fingerprint: */ unsigned char* inputFprintBin = OSPC_OSNULL; int inputFprintBinLen = 0; /* This is the index of the CA certificate's parent ( itself ) within the * given list. It's ignored because it should be self-signed. */ int ignoreIndex = -1; OSPM_DBGENTER(( "ENTER: OSPPValidateCACertificate\n" )); /* If ( the input parameters are bad ) then * o there's nothing we can do; set the errorcode and complain. */ if ( ospvEnrollParams == OSPC_OSNULL ) { retVal = OSPC_ERR_ENROLL_INVALID_ARG; OSPM_DBGERRORLOG( retVal, "Invalid enrollment parameters passed in.\n" ); } /* Now check the CA certificate's value; if the CA certificate is null * or empty, then we can't check it. In this case, set an errorcode * and complain. */ if ( ( ospvEnrollParams->CACert == OSPC_OSNULL ) || ( ospvEnrollParams->CACertLen <= 0 ) ) { retVal = OSPC_ERR_ENROLL_INVALID_ARG; OSPM_DBGERRORLOG( retVal, "The CA certificate being validated is empty.\n" ); } /* Create an ASN1 object out of the CA certificate received. If we cannot * form a certificate, then we know that there's something wrong with the * general format of the certificate. */ if ( retVal == OSPC_ERR_NO_ERROR ) { retVal = OSPPX509CertCreate( ospvEnrollParams->CACert, &caCertASN1Object ); if ( retVal != OSPC_ERR_NO_ERROR ) { OSPM_DBGERRORLOG( retVal, "The CA certificate received is not a valid X.509 cert.\n" ); } } /* Then we successfully parsed the CA certificate's structure; now let's * check the signature. The signature must be self-signed; if it isn't, * then we'll need some other way of authenticating the certificate * ( such as assuming that the CA certificate entered by hand is valid. */ if ( retVal == OSPC_ERR_NO_ERROR ) { OSPM_DBGMISC(( "CA cert:\n" )); OSPPDumpHex( caCertASN1Object->ElementInfo->Content, caCertASN1Object->ElementInfo->ContentLength ); retVal = OSPPX509CertValidateCertificate( caCertASN1Object, &caCertASN1Object, 1, &ignoreIndex ); if ( retVal != OSPC_ERR_NO_ERROR ) { OSPM_DBGERRORLOG( retVal, "The CA certificate chain is not self-signed.\n" ); } } /* If the fingerprint passed in was null, then we're done; we don't * care about the CA fingerprint. The security policy here may be * changed if we absolutely require the CA's fingerprint. We won't * return right away if there's no other processing to be done, * simply for the sake of having a single entry/exit point. */ if ( ( ospvEnrollParams->CAFprint != OSPC_OSNULL ) && ( OSPM_STRLEN( ospvEnrollParams->CAFprint ) > 0 ) ) { OSPM_DBGMISC(( "fingerprint on input: \n" )); OSPPDumpHex( ospvEnrollParams->CAFprint, 40 ); if ( ospvEnrollParams->CACert == OSPC_OSNULL ) { retVal = OSPC_ERR_ENROLL_INVALID_ARG; OSPM_DBGERRORLOG( retVal, "The CA certificate is missing.\n" ); } /* Create the memory for the hash: */ if ( retVal == OSPC_ERR_NO_ERROR ) { OSPM_MALLOC( caFprint, unsigned char, OSPC_ENROLL_MAX_FPRINT_SIZE + 1 ); OSPM_MALLOC( inputFprintBin, unsigned char, 2 * OSPC_ENROLL_MAX_FPRINT_SIZE + 1 ); /* If ( the memory allocation didn't work ) then * o set an error code and complain; we'll clean up any * memory allocated at the end of this block anyway. */ if ( ( caFprint == OSPC_OSNULL ) || ( inputFprintBin == OSPC_OSNULL ) ) { retVal = OSPC_ERR_ENROLL_NO_MEMORY; OSPM_DBGERRORLOG( retVal, "Unable to create memory for the fingerprint.\n" ); } } /* Now turn the hexadecimal ASCII into binary; this is better than * comparing two hex encodings that may have different capitalization * schemes. */ if ( retVal == OSPC_ERR_NO_ERROR ) { inputFprintBinLen = 2 * OSPC_ENROLL_MAX_FPRINT_SIZE; retVal = OSPPHexToBinary( ospvEnrollParams->CAFprint, OSPM_STRLEN( ospvEnrollParams->CAFprint ), inputFprintBin, &inputFprintBinLen ); if ( retVal != OSPC_ERR_NO_ERROR ) { OSPM_DBGERRORLOG( retVal, "The fingerprint passed in could not be decoded to binary.\n" ); OSPM_DBGMISC(( "fingerprint passed in: <%s>\n", ospvEnrollParams->CAFprint )); } } /* If ( we could initialize for the hash ) then * o initialize the memory; * o take the hash; * o check the return value for calculating the fingerprint. */ if ( retVal == OSPC_ERR_NO_ERROR ) { OSPM_MEMSET( caFprint, 0, OSPC_ENROLL_MAX_FPRINT_SIZE + 1 ); OSPPDumpHex( caCertASN1Object->ElementInfo->Content, caCertASN1Object->ElementInfo->ContentLength ); /* Use the CA certificate and its length as the parameters for * the fingerprint/digest; put the digest in caFprint and its * length in caFprintLen. We'll use SHA 1 for the digesting * algorithm; the other two paramters( OSPC_OSNULL and 0 ) are * superfluous values; ignore them and their green curtain. */ retVal = OSPPCryptoWrapDigest( caFprint, &caFprintLen, OSPC_OSNULL, 0, ospvEnrollParams->CACert, ospvEnrollParams->CACertLen, OSPC_CRYPTO_FLAG_USE_SHA1_DIGEST ); OSPM_DBGMISC(( "retVal for digesting: <%d>\n", retVal )); if ( retVal == OSPC_ERR_NO_ERROR ) { OSPM_DBGMISC(( "CA fingerprint:\n" )); OSPPDumpHex( caFprint, caFprintLen ); } } /* Now check the fingerprint against what we were given. Translate * the fingerprint that was taken into hex and compare it against the * hex fingerprint given on input. */ if ( retVal == OSPC_ERR_NO_ERROR ) { OSPM_DBGMISC(( "input fingerprint: \n" )); OSPPDumpHex( inputFprintBin, caFprintLen ); if ( caFprintLen != inputFprintBinLen ) { retVal = OSPC_ERR_ENROLL_BAD_FPRINT; } else if ( OSPM_MEMCMP( inputFprintBin, caFprint, caFprintLen ) != 0 ) { retVal = OSPC_ERR_ENROLL_BAD_FPRINT; } if ( retVal == OSPC_ERR_NO_ERROR ) { OSPM_DBGMISC(( "The CA fingerprint matches the one passed in.\n" )); } else { OSPM_DBGERRORLOG( retVal, "The CA certificate received had a bad fingerprint.\n" ); } } /* Free up everything that we've allocated, but only if the * space exists: */ if ( caFprint != OSPC_OSNULL ) { OSPM_FREE( caFprint ); } if ( inputFprintBin != OSPC_OSNULL ) { OSPM_FREE( inputFprintBin ); } } /* This object is allocated outside the scope of when we check * the CA certificate's fingerpring, so save the freeing of this * object until the end. */ if ( caCertASN1Object != OSPC_OSNULL ) { OSPPASN1ObjectDelete( &caCertASN1Object ); } OSPM_DBGEXIT(( "EXIT: OSPPValidateCACertificate\n" )); return retVal;}/* Translate the given hex string into binary. We'll be given the * length of the hex string and the length of the binary string as * well. We'll expect that the binary string already has enough * memory allocated ( at least twice as much as the hex encoding. ) * * The length of the hex string dictates how much memory will be copied. * For example, if the length of the hex string is 200 bytes ( or 100 bytes * in its full binary representation ), then the number of octets to * be copied from the hex string can be anywhere from 2 to 200. * * The length of the binary string initially refers to the amount of * space that the binary string can contain. It will be set to the * number of bytes that were copied into the binary string. * * Input: the hex string, the length of the hex string, the binary * representation, and the length of the binary representation * * Output: the binary representation and its length will be output. * A return code of OSPC_ERR_NO_ERRRO ( 0 ) will be returned * if everything went alright; otherwise, a non-zero code * will be returned. */int OSPPHexToBinary ( unsigned char* ospvHexStr, unsigned ospvHexStrLen, unsigned char* ospvBinaryStr, unsigned* ospvBinaryStrLen ){ int retVal = OSPC_ERR_NO_ERROR; /* Index for looping through the hex string contents: */ unsigned hexStrIndex = 0; /* The next two bytes from the hex string that will compose the * next byte in the binary string: */ unsigned char* nextByte = OSPC_OSNULL; /* The next word of data to be placed in the binary string; this * will be the output of a strtol on nextByte: */ long nextWord = 0; /* If ( any of the values input are null ) then * o complain and set an error code. */ if ( ( ospvHexStr == OSPC_OSNULL ) || ( ospvBinaryStr == OSPC_OSNULL ) || ( ospvBinaryStrLen == OSPC_OSNULL ) ) { retVal = OSPC_ERR_ENROLL_INVALID_ARG; OSPM_DBGERRORLOG( retVal, "A null pointer was passed in.\n" ); } /* If ( the hex string is not as long as what the caller wants * to copy from it ) then * o set an error code and complain. */ if ( retVal == OSPC_ERR_NO_ERROR ) { if ( OSPM_STRLEN( ospvHexStr ) < ospvHexStrLen ) { retVal = OSPC_ERR_ENROLL_INVALID_ARG; OSPM_DBGERRORLOG( retVal, "The requested length of the hex string to be translated is longer than the string itself.\n" ); } } /* If ( the caller wants more bytes from the binary string than are * allocated to it or if the binary string is empty ) then * o set an error code and complain. */ if ( retVal == OSPC_ERR_NO_ERROR ) { if ( ( *ospvBinaryStrLen <= 0 ) || ( *ospvBinaryStrLen < 2 * ospvHexStrLen ) ) { retVal = OSPC_ERR_ENROLL_INVALID_ARG; OSPM_DBGERRORLOG( retVal, "The given binary string is too small.\n" ); OSPM_DBGMISC(( "%d < 2 * %d\n", *ospvBinaryStrLen, ospvHexStrLen )); } } /* For ( each character ) * o if ( the character isn't a hex digit ) then * - set an error code and return. */ for ( hexStrIndex = 0; hexStrIndex < ospvHexStrLen; hexStrIndex++ ) { if ( !OSPM_ISXDIGIT( ospvHexStr[ hexStrIndex ] ) ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -