From ba7fdcb5f0d966e6bd5eee12e48409f61786dc54 Mon Sep 17 00:00:00 2001 From: fifthdegree Date: Wed, 17 May 2023 13:17:59 -0400 Subject: [PATCH] Move alloc_sprintf into the winpr string api --- libfreerdp/core/aad.c | 41 +++------------------ libfreerdp/core/smartcardlogon.c | 32 +++------------- winpr/include/winpr/string.h | 2 + winpr/libwinpr/crt/string.c | 29 +++++++++++++++ winpr/libwinpr/sspi/Kerberos/krb5glue_mit.c | 31 +--------------- 5 files changed, 43 insertions(+), 92 deletions(-) diff --git a/libfreerdp/core/aad.c b/libfreerdp/core/aad.c index 11d1f8e20..ba0860cbb 100644 --- a/libfreerdp/core/aad.c +++ b/libfreerdp/core/aad.c @@ -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; diff --git a/libfreerdp/core/smartcardlogon.c b/libfreerdp/core/smartcardlogon.c index 6cd1cc511..3b17f97d6 100644 --- a/libfreerdp/core/smartcardlogon.c +++ b/libfreerdp/core/smartcardlogon.c @@ -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; diff --git a/winpr/include/winpr/string.h b/winpr/include/winpr/string.h index 95e9f39f6..8d8ea7663 100644 --- a/winpr/include/winpr/string.h +++ b/winpr/include/winpr/string.h @@ -38,6 +38,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 diff --git a/winpr/libwinpr/crt/string.c b/winpr/libwinpr/crt/string.c index 308548872..ba0e3c210 100644 --- a/winpr/libwinpr/crt/string.c +++ b/winpr/libwinpr/crt/string.c @@ -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) diff --git a/winpr/libwinpr/sspi/Kerberos/krb5glue_mit.c b/winpr/libwinpr/sspi/Kerberos/krb5glue_mit.c index 6a574e08f..6b166387d 100644 --- a/winpr/libwinpr/sspi/Kerberos/krb5glue_mit.c +++ b/winpr/libwinpr/sspi/Kerberos/krb5glue_mit.c @@ -29,35 +29,6 @@ #include "krb5glue.h" #include -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);