in platform/posix/transport/src/openssl_posix.c [301:387]
static int32_t setRootCa( const SSL_CTX * pSslContext,
const char * pRootCaPath )
{
int32_t sslStatus = 1;
FILE * pRootCaFile = NULL;
X509 * pRootCa = NULL;
assert( pSslContext != NULL );
assert( pRootCaPath != NULL );
#if ( LIBRARY_LOG_LEVEL == LOG_DEBUG )
logPath( pRootCaPath, ROOT_CA_LABEL );
#endif
/* MISRA Rule 21.6 flags the following line for using the standard
* library input/output function `fopen()`. This rule is suppressed because
* openssl function #PEM_read_X509 takes an argument of type `FILE *` for
* reading the root ca PEM file and `fopen()` needs to be used to get the
* file pointer. */
/* coverity[misra_c_2012_rule_21_6_violation] */
pRootCaFile = fopen( pRootCaPath, "r" );
if( pRootCaFile == NULL )
{
LogError( ( "fopen failed to find the root CA certificate file: "
"ROOT_CA_PATH=%s.",
pRootCaPath ) );
sslStatus = -1;
}
if( sslStatus == 1 )
{
/* Read the root CA into an X509 object. */
pRootCa = PEM_read_X509( pRootCaFile, NULL, NULL, NULL );
if( pRootCa == NULL )
{
LogError( ( "PEM_read_X509 failed to parse root CA." ) );
sslStatus = -1;
}
}
if( sslStatus == 1 )
{
/* Add the certificate to the context. */
sslStatus =
X509_STORE_add_cert( SSL_CTX_get_cert_store( pSslContext ), pRootCa );
if( sslStatus != 1 )
{
LogError(
( "X509_STORE_add_cert failed to add root CA to certificate store." ) );
sslStatus = -1;
}
}
/* Free the X509 object used to set the root CA. */
if( pRootCa != NULL )
{
X509_free( pRootCa );
pRootCa = NULL;
}
/* Close the file if it was successfully opened. */
if( pRootCaFile != NULL )
{
/* MISRA Rule 21.6 flags the following line for using the standard
* library input/output function `fclose()`. This rule is suppressed
* because openssl function #PEM_read_X509 takes an argument of type
* `FILE *` for reading the root ca PEM file and `fopen()` is used to
* get the file pointer. The file opened with `fopen()` needs to be
* closed by calling `fclose()`.*/
/* coverity[misra_c_2012_rule_21_6_violation] */
if( fclose( pRootCaFile ) != 0 )
{
LogWarn( ( "fclose failed to close file %s", pRootCaPath ) );
}
}
/* Log the success message if we successfully imported the root CA. */
if( sslStatus == 1 )
{
LogDebug( ( "Successfully imported root CA." ) );
}
return sslStatus;
}