@@ -204,12 +204,15 @@ static bool pemify(char** pToB64, const char* header, const char* footer) {
204204 if (worked ) {
205205 if (* pToB64 ) {
206206 tempSize = strlen (result ) + 1 ;
207- * pToB64 = (char * )realloc (* pToB64 ,tempSize );
208- if (NULL == * pToB64 ) {
207+ char * temp = (char * )realloc (* pToB64 ,tempSize );
208+ if (NULL == temp ) {
209209 log_error ("%s::%s(%d) : Out of memory in realloc" , LOG_INF );
210+ free (* pToB64 ); /* Free original pointer to prevent leak */
211+ * pToB64 = NULL ;
210212 worked = false;
211213 } else {
212214 log_trace ("%s::%s(%d) : Successfully reallocated memory" , LOG_INF );
215+ * pToB64 = temp ;
213216 memcpy (* pToB64 ,result ,tempSize );
214217 worked = true;
215218 }
@@ -317,8 +320,8 @@ static bool get_datestring_ASN1(char* buf, int bufLen, const ASN1_TIME* date) {
317320 break ;
318321 }
319322 } else {
320- log_error ("%s::%s(%d) : Out of memory -- exiting " , LOG_INF );
321- exit ( EXIT_FAILURE ) ;
323+ log_error ("%s::%s(%d) : Out of memory" , LOG_INF );
324+ break ;
322325 }
323326 } while (false);
324327 return bResult ;
@@ -415,9 +418,10 @@ static bool PrivKeyList_add(PrivKeyList* list, EVP_PKEY* key)
415418 bool bResult = false;
416419 if (list && key )
417420 {
418- list -> keys = realloc (list -> keys , (1 + list -> key_count ) * sizeof (key ));
419- if (list -> keys )
421+ EVP_PKEY * * temp = realloc (list -> keys , (1 + list -> key_count ) * sizeof (EVP_PKEY * ));
422+ if (temp )
420423 {
424+ list -> keys = temp ;
421425 log_trace ("%s::%s(%d) : Added EVP_PKEY #%d to PrivKeyList" ,
422426 LOG_INF , list -> key_count );
423427 list -> keys [list -> key_count ] = key ;
@@ -427,6 +431,7 @@ static bool PrivKeyList_add(PrivKeyList* list, EVP_PKEY* key)
427431 else
428432 {
429433 log_error ("%s::%s(%d) : Out of memory" , LOG_INF );
434+ /* Original list->keys pointer remains valid */
430435 }
431436 }
432437 else
@@ -502,10 +507,11 @@ static bool PEMx509List_add(PEMx509List* list, X509* cert)
502507 bool bResult = false;
503508 if (list && cert )
504509 {
505- list -> certs = realloc (list -> certs , \
506- (1 + list -> item_count ) * sizeof (cert ));
507- if (list -> certs )
510+ X509 * * temp = realloc (list -> certs , \
511+ (1 + list -> item_count ) * sizeof (X509 * ));
512+ if (temp )
508513 {
514+ list -> certs = temp ;
509515 log_trace ("%s::%s(%d) : Adding X509 cert #%d to PEMx509List" ,
510516 LOG_INF , list -> item_count );
511517 list -> certs [list -> item_count ] = cert ;
@@ -516,6 +522,7 @@ static bool PEMx509List_add(PEMx509List* list, X509* cert)
516522 {
517523 log_error ("%s::%s(%d) : Out of memory" ,
518524 LOG_INF );
525+ /* Original list->certs pointer remains valid */
519526 }
520527 }
521528 else
@@ -607,10 +614,11 @@ static bool PemInventoryList_add(PemInventoryList* list, PemInventoryItem* item)
607614 bool bResult = false;
608615 if (list && item )
609616 {
610- list -> items = realloc (list -> items ,
611- (1 + list -> item_count ) * sizeof (item ));
612- if (list -> items )
617+ PemInventoryItem * * temp = realloc (list -> items ,
618+ (1 + list -> item_count ) * sizeof (PemInventoryItem * ));
619+ if (temp )
613620 {
621+ list -> items = temp ;
614622 list -> items [list -> item_count ] = item ;
615623 list -> item_count ++ ;
616624 log_trace ("%s::%s(%d) : Added cert with thumbprint %s to local "
@@ -620,6 +628,7 @@ static bool PemInventoryList_add(PemInventoryList* list, PemInventoryItem* item)
620628 else
621629 {
622630 log_error ("%s::%s(%d) : Out of memory" , LOG_INF );
631+ /* Original list->items pointer remains valid */
623632 }
624633 }
625634 else
@@ -688,7 +697,17 @@ static bool PemInventoryItem_populate(PemInventoryItem* pem, X509* cert)
688697 /* Store the b64 encoded DER version of the pem in here */
689698 log_trace ("%s::%s(%d) : Storing certContent into PEMInventoryItem" , LOG_INF );
690699 pem -> cert = base64_encode (certContent , contLen , false, NULL );
700+ if (!pem -> cert ) {
701+ log_error ("%s::%s(%d) : Failed to base64 encode certificate" , LOG_INF );
702+ return false;
703+ }
691704 pem -> thumbprint_string = strdup (thumb );
705+ if (!pem -> thumbprint_string ) {
706+ log_error ("%s::%s(%d) : Failed to allocate thumbprint string" , LOG_INF );
707+ free (pem -> cert );
708+ pem -> cert = NULL ;
709+ return false;
710+ }
692711 pem -> has_private_key = false;
693712 bResult = true;
694713 }
@@ -1061,6 +1080,10 @@ static X509_NAME* parse_subject(const char* subject)
10611080 }
10621081
10631082 localSubjectPtr = strdup (subject );
1083+ if (!localSubjectPtr ) {
1084+ log_error ("%s::%s(%d) : Failed to allocate subject string" , LOG_INF );
1085+ goto cleanup ;
1086+ }
10641087 curPtr = localSubjectPtr ;
10651088 log_debug ("%s::%s(%d) : Subject \"%s\" is %ld characters long" ,
10661089 LOG_INF , curPtr , strlen (curPtr ));
@@ -1081,7 +1104,11 @@ static X509_NAME* parse_subject(const char* subject)
10811104 }
10821105 strncpy (keyBytes , curPtr , (int )keyLen );
10831106
1084- strippedKey = strip_blanks (keyBytes , keyLen );
1107+ strippedKey = strip_blanks (keyBytes , keyLen );
1108+ if (!strippedKey ) {
1109+ log_error ("%s::%s(%d) : Failed to strip blanks from key" , LOG_INF );
1110+ goto cleanup ;
1111+ }
10851112 log_verbose ("%s::%s(%d) : Key: \"%s\" is %ld characters long" ,
10861113 LOG_INF , strippedKey , strlen (strippedKey ));
10871114
@@ -2074,14 +2101,23 @@ char* ssl_generate_csr(const char* asciiSubject, size_t* csrLen,
20742101 int writeLen = i2d_X509_REQ (req , & tempReqBytes );
20752102 /* Now convert this structure to an ASCII string */
20762103 csrString = base64_encode (reqBytes , (size_t )writeLen , false, NULL );
2077- * csrLen = (size_t )writeLen ;
2078- log_trace ("%s::%s(%d) : csrString=%s" , LOG_INF , csrString );
2079- log_trace ("%s::%s(%d) : csrLen = %ld" , LOG_INF , * csrLen );
2080- if (MAX_CSR_SIZE < * csrLen ) {
2081- log_error ("%s::%s(%d) : The length of the CSR = %ld which is longer than the maximum defined "
2082- "length of %d -- ABORTING, please increase the maximum CSR length above %ld and re-compile" ,
2083- LOG_INF , * csrLen , MAX_CSR_SIZE , * csrLen );
2084- exit (EXIT_FAILURE );
2104+ if (!csrString ) {
2105+ log_error ("%s::%s(%d) : Failed to base64 encode CSR" , LOG_INF );
2106+ append_linef (pMessage , "Failed to base64 encode CSR" );
2107+ } else {
2108+ * csrLen = (size_t )writeLen ;
2109+ }
2110+ if (csrString ) {
2111+ log_trace ("%s::%s(%d) : csrString=%s" , LOG_INF , csrString );
2112+ log_trace ("%s::%s(%d) : csrLen = %ld" , LOG_INF , * csrLen );
2113+ if (MAX_CSR_SIZE < * csrLen ) {
2114+ log_error ("%s::%s(%d) : The length of the CSR = %ld which is longer than the maximum defined "
2115+ "length of %d -- Please increase the maximum CSR length above %ld and re-compile" ,
2116+ LOG_INF , * csrLen , MAX_CSR_SIZE , * csrLen );
2117+ append_linef (pMessage , "CSR length %ld exceeds maximum %d" , * csrLen , MAX_CSR_SIZE );
2118+ free (csrString );
2119+ csrString = NULL ;
2120+ }
20852121 }
20862122 }
20872123
0 commit comments