Use malloc/free rather than TPM_Malloc/TPM_Free in library code

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
This commit is contained in:
Stefan Berger 2018-06-15 17:41:33 -04:00
parent d9d83de2d3
commit 707a9046cd
3 changed files with 28 additions and 9 deletions

View File

@ -240,7 +240,6 @@ static unsigned char *TPMLIB_OpenSSL_Base64Decode(char *input,
BIO *b64, *bmem;
unsigned char *res = NULL;
int n;
TPM_RESULT rc;
b64 = BIO_new(BIO_f_base64());
if (!b64) {
@ -255,14 +254,15 @@ static unsigned char *TPMLIB_OpenSSL_Base64Decode(char *input,
bmem = BIO_push(b64, bmem);
BIO_set_flags(bmem, BIO_FLAGS_BASE64_NO_NL);
rc = TPM_Malloc(&res, outputlen);
if (rc != TPM_SUCCESS) {
res = malloc(outputlen);
if (!res) {
TPMLIB_LogError("Could not allocate %u bytes.\n", outputlen);
goto cleanup;
}
n = BIO_read(bmem, res, outputlen);
if (n <= 0) {
TPM_Free(res);
free(res);
res = NULL;
goto cleanup;
}
@ -296,8 +296,12 @@ static unsigned char *TPMLIB_Base64Decode(const char *start, const char *end,
end++;
if (TPM_Malloc((unsigned char **)&input, end - start + 1) != TPM_SUCCESS)
input = malloc(end - start + 1);
if (!input) {
TPMLIB_LogError("Could not allocate %u bytes.\n",
(unsigned int)(end - start + 1));
return NULL;
}
/* copy from source string skipping '\n' and '\r' and using
'=' to calculate the exact length */
@ -537,9 +541,13 @@ TPM_RESULT CopyCachedState(enum TPMLIB_StateType st,
*is_empty_buffer = (*buflen == BUFLEN_EMPTY_BUFFER);
if (cached_blobs[st].buffer) {
ret = TPM_Malloc(buffer, *buflen);
if (ret == TPM_SUCCESS)
*buffer = malloc(*buflen);
if (!*buffer) {
TPMLIB_LogError("Could not allocate %u bytes.\n", *buflen);
ret = TPM_SIZE;
} else {
memcpy(*buffer, cached_blobs[st].buffer, *buflen);
}
} else {
*buffer = NULL;
}

View File

@ -90,6 +90,8 @@ uint32_t TPM12_GetBufferSize(void);
int TPMLIB_LogPrintf(const char *format, ...);
void TPMLIB_LogPrintfA(unsigned int indent, const char *format, ...);
#define TPMLIB_LogError(format, ...) \
TPMLIB_LogPrintfA(~0, "libtpms: "format, __VA_ARGS__)
#define TPMLIB_LogTPM12Error(format, ...) \
TPMLIB_LogPrintfA(~0, "libtpms/tpm12: "format, __VA_ARGS__)

View File

@ -445,14 +445,23 @@ TPM_RESULT TPM12_SetState(enum TPMLIB_StateType st,
return TPM_INVALID_POSTINIT;
if (ret == TPM_SUCCESS) {
ret = TPM_Malloc((unsigned char **)&stream, buflen);
stream = malloc(buflen);
if (!stream) {
TPMLIB_LogError("Could not allocate %u bytes.\n", buflen);
ret = TPM_SIZE;
}
}
if (ret == TPM_SUCCESS) {
orig_stream = stream;
memcpy(stream, buffer, buflen);
ret = TPM_Malloc((unsigned char **)&tpm_state, sizeof(tpm_state_t));
tpm_state = malloc(sizeof(tpm_state_t));
if (!tpm_state) {
TPMLIB_LogError("Could not allocated %zu bytes.\n",
sizeof(tpm_state_t));
ret = TPM_SIZE;
}
}
if (ret == TPM_SUCCESS) {