Move alloc_sprintf into the winpr string api

This commit is contained in:
fifthdegree 2023-05-17 13:17:59 -04:00 committed by akallabeth
parent 7832ba2361
commit ba7fdcb5f0
5 changed files with 43 additions and 92 deletions

View File

@ -129,35 +129,6 @@ static BOOL generate_pop_key(rdpAad* aad);
static BOOL read_http_message(rdpAad* aad, BIO* bio, long* status_code, char** content,
size_t* content_length);
static int alloc_sprintf(char** s, size_t* slen, const char* template, ...)
{
va_list ap;
WINPR_ASSERT(s);
WINPR_ASSERT(slen);
*s = NULL;
*slen = 0;
va_start(ap, template);
const int length = vsnprintf(NULL, 0, template, ap);
va_end(ap);
if (length < 0)
return length;
char* str = calloc((size_t)length + 1ul, sizeof(char));
if (!str)
return -1;
va_start(ap, template);
const int plen = vsprintf(str, template, ap);
va_end(ap);
WINPR_ASSERT(length == plen);
*s = str;
*slen = (size_t)length;
return length;
}
static SSIZE_T stream_sprintf(wStream* s, const char* fmt, ...)
{
va_list ap;
@ -445,11 +416,11 @@ static BOOL aad_send_token_request(rdpAad* aad, BIO* bio, const char* auth_code)
char* req_header = NULL;
size_t req_body_len = 0;
size_t req_header_len = 0;
const int trc = alloc_sprintf(&req_body, &req_body_len, token_http_request_body, auth_code,
const int trc = winpr_asprintf(&req_body, &req_body_len, token_http_request_body, auth_code,
aad->hostname, aad->kid);
if (trc < 0)
goto fail;
const int trh = alloc_sprintf(&req_header, &req_header_len, token_http_request_header, trc);
const int trh = winpr_asprintf(&req_header, &req_header_len, token_http_request_header, trc);
if (trh < 0)
goto fail;
@ -564,7 +535,7 @@ static char* aad_create_jws_header(rdpAad* aad)
char* buffer = NULL;
size_t bufferlen = 0;
const int length =
alloc_sprintf(&buffer, &bufferlen, "{\"alg\":\"RS256\",\"kid\":\"%s\"}", aad->kid);
winpr_asprintf(&buffer, &bufferlen, "{\"alg\":\"RS256\",\"kid\":\"%s\"}", aad->kid);
if (length < 0)
return NULL;
@ -588,7 +559,7 @@ static char* aad_create_jws_payload(rdpAad* aad, const char* ts_nonce)
char* buffer = NULL;
size_t bufferlen = 0;
const int length =
alloc_sprintf(&buffer, &bufferlen,
winpr_asprintf(&buffer, &bufferlen,
"{"
"\"ts\":\"%li\","
"\"at\":\"%s\","
@ -947,7 +918,7 @@ static BOOL generate_json_base64_str(rdpAad* aad, const char* b64_hash)
char* buffer = NULL;
size_t blen = 0;
const int length = alloc_sprintf(&buffer, &blen, "{\"kid\":\"%s\"}", b64_hash);
const int length = winpr_asprintf(&buffer, &blen, "{\"kid\":\"%s\"}", b64_hash);
if (length < 0)
return FALSE;
@ -982,7 +953,7 @@ BOOL generate_pop_key(rdpAad* aad)
size_t blen = 0;
const int alen =
alloc_sprintf(&buffer, &blen, "{\"e\":\"%s\",\"kty\":\"RSA\",\"n\":\"%s\"}", e, n);
winpr_asprintf(&buffer, &blen, "{\"e\":\"%s\",\"kty\":\"RSA\",\"n\":\"%s\"}", e, n);
if (alen < 0)
goto fail;

View File

@ -239,31 +239,6 @@ static BOOL set_info_certificate(SmartcardCertInfo* cert, BYTE* certBytes, DWORD
return TRUE;
}
static int allocating_sprintf(char** dst, const char* fmt, ...)
{
int rc;
va_list ap;
WINPR_ASSERT(dst);
va_start(ap, fmt);
rc = vsnprintf(NULL, 0, fmt, ap);
va_end(ap);
if (rc < 0)
return rc;
{
char* tmp = realloc(*dst, (size_t)rc + 1);
if (!tmp)
return -1;
*dst = tmp;
}
va_start(ap, fmt);
rc = vsnprintf(*dst, (size_t)rc + 1, fmt, ap);
va_end(ap);
return rc;
}
#ifndef _WIN32
static BOOL build_pkinit_args(const rdpSettings* settings, SmartcardCertInfo* scCert)
{
@ -272,8 +247,9 @@ static BOOL build_pkinit_args(const rdpSettings* settings, SmartcardCertInfo* sc
*/
const char* Pkcs11Module = freerdp_settings_get_string(settings, FreeRDP_Pkcs11Module);
const char* pkModule = Pkcs11Module ? Pkcs11Module : "opensc-pkcs11.so";
size_t size = 0;
if (allocating_sprintf(&scCert->pkinitArgs, "PKCS11:module_name=%s:slotid=%" PRIu16, pkModule,
if (winpr_asprintf(&scCert->pkinitArgs, &size, "PKCS11:module_name=%s:slotid=%" PRIu16, pkModule,
(UINT16)scCert->slotId) <= 0)
return FALSE;
return TRUE;
@ -729,6 +705,8 @@ static char* create_temporary_file(void)
static SmartcardCertInfo* smartcardCertInfo_New(const char* privKeyPEM, const char* certPEM)
{
size_t size = 0;
WINPR_ASSERT(privKeyPEM);
WINPR_ASSERT(certPEM);
@ -776,7 +754,7 @@ static SmartcardCertInfo* smartcardCertInfo_New(const char* privKeyPEM, const ch
if (!crypto_write_pem(info->certPath, certPEM, strlen(certPEM)))
goto fail;
int res = allocating_sprintf(&cert->pkinitArgs, "FILE:%s,%s", info->certPath, info->keyPath);
int res = winpr_asprintf(&cert->pkinitArgs, &size, "FILE:%s,%s", info->certPath, info->keyPath);
if (res <= 0)
goto fail;

View File

@ -39,6 +39,8 @@ extern "C"
WINPR_API BOOL winpr_str_append(const char* what, char* buffer, size_t size,
const char* separator);
WINPR_API int winpr_asprintf(char** s, size_t* slen, const char* templ, ...);
#ifndef _WIN32
#define CSTR_LESS_THAN 1

View File

@ -142,6 +142,35 @@ BOOL winpr_str_append(const char* what, char* buffer, size_t size, const char* s
return TRUE;
}
int winpr_asprintf(char** s, size_t* slen, const char* templ, ...)
{
va_list ap;
WINPR_ASSERT(s);
WINPR_ASSERT(slen);
*s = NULL;
*slen = 0;
va_start(ap, templ);
const int length = vsnprintf(NULL, 0, templ, ap);
va_end(ap);
if (length < 0)
return length;
char* str = calloc((size_t)length + 1ul, sizeof(char));
if (!str)
return -1;
va_start(ap, templ);
const int plen = vsprintf(str, templ, ap);
va_end(ap);
WINPR_ASSERT(length == plen);
*s = str;
*slen = (size_t)length;
return length;
}
#ifndef _WIN32
char* _strdup(const char* strSource)

View File

@ -29,35 +29,6 @@
#include "krb5glue.h"
#include <profile.h>
static int alloc_sprintf(char** s, size_t* slen, const char* template, ...)
{
va_list ap;
WINPR_ASSERT(s);
WINPR_ASSERT(slen);
*s = NULL;
*slen = 0;
va_start(ap, template);
const int length = vsnprintf(NULL, 0, template, ap);
va_end(ap);
if (length < 0)
return length;
char* str = calloc((size_t)length + 1ul, sizeof(char));
if (!str)
return -1;
va_start(ap, template);
const int plen = vsprintf(str, template, ap);
va_end(ap);
WINPR_ASSERT(length == plen);
*s = str;
*slen = (size_t)length;
return length;
}
static char* create_temporary_file(void)
{
BYTE buffer[32];
@ -190,7 +161,7 @@ krb5_error_code krb5glue_get_init_creds(krb5_context ctx, krb5_principal princ,
goto cleanup;
rv = ENOMEM;
if (alloc_sprintf(&kdc_url, &size, "https://%s/KdcProxy", krb_settings->kdcUrl) <= 0)
if (winpr_asprintf(&kdc_url, &size, "https://%s/KdcProxy", krb_settings->kdcUrl) <= 0)
goto cleanup;
realm = calloc(princ->realm.length + 1, 1);