-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathhttpclient.c
More file actions
638 lines (559 loc) · 28.9 KB
/
httpclient.c
File metadata and controls
638 lines (559 loc) · 28.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/******************************************************************************/
/* Copyright 2021 Keyfactor */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain a */
/* copy of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless */
/* required by applicable law or agreed to in writing, software distributed */
/* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES */
/* OR CONDITIONS OF ANY KIND, either express or implied. See the License for */
/* the specific language governing permissions and limitations under the */
/* License. */
/******************************************************************************/
/* @file httpclient.c */
/* */
/* Provides http_post_json(), the single HTTP transport function used by the */
/* agent to communicate with the Keyfactor platform. */
/* */
/* Internal structure: */
/* setup_curl_handle() -- init, error buffer, timeouts, HTTP version */
/* apply_basic_auth() -- optional username/password */
/* apply_trust_store() -- optional CA bundle */
/* apply_client_cert() -- bootstrap or agent mTLS cert + key */
/* build_request_headers()-- Content-Type/Accept/Content-Length slist */
/* attach_post_body() -- POSTFIELDS + write callback */
/* execute_with_retry() -- perform loop, HTTP code check, response alloc */
/******************************************************************************/
#include "constants.h"
#include <curl/curl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "config.h"
#include "global.h"
#include "httpclient.h"
#include "logging.h"
#include "utils.h"
#if defined(__TPM__)
#include "agent.h"
#include <tpm2-tss-engine.h>
#include <tss2/tss2_esys.h>
#include <tss2/tss2_mu.h>
#endif
/******************************************************************************/
/***************************** GLOBAL VARIABLES *******************************/
/******************************************************************************/
bool add_client_cert_to_header = false;
/******************************************************************************/
/***************************** LOCAL DEFINES *********************************/
/******************************************************************************/
/******************************************************************************/
/************************ LOCAL GLOBAL STRUCTURES *****************************/
/******************************************************************************/
/* */
/* Response accumulator passed as userdata to WriteMemoryCallback. */
/* memory: heap buffer grown by realloc as chunks arrive. */
/* size: total bytes written so far (excludes the null terminator). */
/* */
struct MemoryStruct {
char *memory;
size_t size;
};
/******************************************************************************/
/************************ LOCAL FUNCTION DEFINITIONS **************************/
/******************************************************************************/
/* */
/* Log a cURL setup error, clean up the handle, and return the error code. */
/* */
/* errBuff must have been registered with CURLOPT_ERRORBUFFER before calling */
/* this function; if it has not yet been populated it will be empty and the */
/* strerror fallback will be used instead. */
/* */
/* @param curl - CURL handle to clean up (must not be NULL) */
/* @param errNum - CURLcode that triggered the error */
/* @param errBuff - buffer registered via CURLOPT_ERRORBUFFER */
/* @return errNum (pass-through for single-expression callers) */
/* */
static int handle_curl_error(CURL *curl, int errNum, const char *errBuff) {
if (is_log_trace() && errBuff && errBuff[0] != '\0') {
size_t len = strlen(errBuff);
log_error("%s::%s(%d) : libcurl (%d): %s%s", LOG_INF, errNum, errBuff,
(errBuff[len - 1] != '\n') ? "\n" : "");
} else {
log_error("%s::%s(%d) : libcurl (%d): %s", LOG_INF, errNum,
curl_easy_strerror(errNum));
}
curl_easy_cleanup(curl);
return errNum;
} /* handle_curl_error */
/* */
/* curl write callback — appends each received chunk to a MemoryStruct. */
/* Registered via CURLOPT_WRITEFUNCTION / CURLOPT_WRITEDATA. */
/* */
/* @return bytes consumed; returning 0 signals an error to libcurl */
/* */
static size_t WriteMemoryCallback(const void *contents, const size_t size,
const size_t nmemb, const void *userp) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)userp;
char *ptr = realloc(mem->memory, mem->size + realsize + 1);
if (!ptr) {
log_error("%s::%s(%d) : Out of memory in write callback", LOG_INF);
return 0;
}
mem->memory = ptr;
memcpy(&mem->memory[mem->size], contents, realsize);
mem->size += realsize;
mem->memory[mem->size] = '\0';
return realsize;
} /* WriteMemoryCallback */
/* */
/* Strip newline characters ('\n') from a cert string in-place. */
/* Used before embedding a PEM cert in an HTTP header value. */
/* Bounded by MAX_CERT_SIZE to guard against unterminated input. */
/* */
static void stripNewlines(char *string) {
size_t x = 0, y = 0;
while (string[x] != '\0' && x < MAX_CERT_SIZE) {
if (string[x] != '\n')
string[y++] = string[x];
x++;
}
string[y] = '\0';
} /* stripNewlines */
/*----------------------------------------------------------------------------*/
/* CURL SETUP HELPERS */
/*----------------------------------------------------------------------------*/
/* */
/* Initialise the curl handle with core POST options: URL, error buffer, */
/* connect timeout, HTTP version, and verbose tracing if enabled. */
/* */
/* @param curl - freshly initialised CURL handle */
/* @param url - target URL */
/* @param errBuff - CURL_ERROR_SIZE buffer, zeroed by caller */
/* @return CURLE_OK or a CURLcode on failure (handle already cleaned up) */
/* */
static int setup_curl_handle(CURL *curl, const char *url, char *errBuff) {
int errNum;
/* Register the error buffer first so every subsequent error captures it */
curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, errBuff);
errNum = curl_easy_setopt(curl, CURLOPT_URL, url);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
errNum = curl_easy_setopt(curl, CURLOPT_POST, 1L);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
errNum = curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, CONNECTION_TIMEOUT);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
#ifdef __HTTP_1_1__
/* Some OpenSSL builds default to HTTP/2; force 1.1 when required */
errNum =
curl_easy_setopt(curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
#endif
if (is_log_trace()) {
log_trace("%s::%s(%d) : Enabling cURL verbose output", LOG_INF);
errNum = curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
}
return CURLE_OK;
} /* setup_curl_handle */
/* */
/* Apply HTTP basic auth credentials to the curl handle. */
/* Both username and password must be non-NULL; otherwise auth is skipped. */
/* */
static int apply_basic_auth(CURL *curl, const char *username,
const char *password, char *errBuff) {
if (!username || !password) {
log_trace("%s::%s(%d) : No basic auth credentials supplied — skipping",
LOG_INF);
return CURLE_OK;
}
int errNum;
log_trace("%s::%s(%d) : Configuring basic auth", LOG_INF);
errNum = curl_easy_setopt(curl, CURLOPT_USERNAME, username);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
errNum = curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
return CURLE_OK;
} /* apply_basic_auth */
/* */
/* Configure an additional CA bundle for peer certificate verification. */
/* If the file does not exist the system default trust store is used. */
/* */
static int apply_trust_store(CURL *curl, const char *trustStore,
char *errBuff) {
if (!file_exists(trustStore)) {
log_trace("%s::%s(%d) : Trust store not found — using system default",
LOG_INF);
return CURLE_OK;
}
log_trace("%s::%s(%d) : Setting trust store: %s", LOG_INF, trustStore);
int errNum = curl_easy_setopt(curl, CURLOPT_CAINFO, trustStore);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
return CURLE_OK;
} /* apply_trust_store */
/* */
/* Configure mTLS client certificate and key on the curl handle. */
/* */
/* Selects bootstrap credentials when EnrollOnStartup + UseBootstrapCert */
/* are both set; otherwise uses the agent cert/key passed by the caller. */
/* If the configuration indicates no cert should be used, returns CURLE_OK */
/* immediately without modifying the handle. */
/* */
/* @param curl - curl handle to configure */
/* @param clientCert - agent cert path (ignored during bootstrap) */
/* @param clientKey - agent key path (ignored during bootstrap) */
/* @param clientKeyPass - agent key password, may be NULL */
/* @param pCertBytes - out: heap copy of the cert PEM for header use, */
/* or unchanged if not needed. Caller must free. */
/* @param errBuff - curl error buffer */
/* @return CURLE_OK, a CURLcode, or CURLE_OUT_OF_MEMORY */
/* */
static int apply_client_cert(CURL *curl, const char *clientCert,
const char *clientKey, const char *clientKeyPass,
unsigned char **pCertBytes, char *errBuff) {
const char *certPath = NULL;
const char *keyPath = NULL;
const char *keyPass = NULL;
int errNum;
if (ConfigData->EnrollOnStartup) {
if (!ConfigData->UseBootstrapCert) {
log_info("%s::%s(%d) : Bypassing client cert on initial enrollment",
LOG_INF);
return CURLE_OK;
}
log_trace("%s::%s(%d) : Using bootstrap cert and key", LOG_INF);
certPath = ConfigData->BootstrapCert;
keyPath = ConfigData->BootstrapKey;
keyPass = ConfigData->BootstrapKeyPassword;
} else {
if (!ConfigData->UseAgentCert) {
log_verbose("%s::%s(%d) : Configured to not use agent cert",
LOG_INF);
return CURLE_OK;
}
log_trace("%s::%s(%d) : Using agent cert and key", LOG_INF);
certPath = clientCert;
keyPath = clientKey;
keyPass = clientKeyPass;
}
/* Client certificate */
if (file_exists(certPath)) {
log_trace("%s::%s(%d) : Setting client cert: %s", LOG_INF, certPath);
errNum = curl_easy_setopt(curl, CURLOPT_SSLCERT, certPath);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
size_t dummySize = 0;
read_file_bytes(certPath, pCertBytes, &dummySize);
if (!*pCertBytes) {
log_error("%s::%s(%d) : Out of memory reading client certificate",
LOG_INF);
return CURLE_OUT_OF_MEMORY;
}
} else {
log_warn("%s::%s(%d) : Client cert not found at %s", LOG_INF, certPath);
}
/* Client private key */
if (file_exists(keyPath)) {
log_trace("%s::%s(%d) : Setting client key: %s", LOG_INF, keyPath);
errNum = curl_easy_setopt(curl, CURLOPT_SSLKEY, keyPath);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
if (keyPass) {
log_trace("%s::%s(%d) : Setting client key password", LOG_INF);
errNum = curl_easy_setopt(curl, CURLOPT_KEYPASSWD, keyPass);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
}
} else {
log_warn("%s::%s(%d) : Client key not found at %s", LOG_INF, keyPath);
}
return CURLE_OK;
} /* apply_client_cert */
#if defined(__TPM__)
/* */
/* Configure the TPM2 SSL engine on the curl handle. */
/* Only called when __TPM__ is defined and EnrollOnStartup is false. */
/* */
static int apply_tpm_engine(CURL *curl, char *errBuff) {
int errNum;
log_verbose("%s::%s(%d) : Configuring TPM2 SSL engine: %s", LOG_INF,
engine_id);
errNum = curl_easy_setopt(curl, CURLOPT_SSLENGINE, engine_id);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
errNum = curl_easy_setopt(curl, CURLOPT_SSLENGINE_DEFAULT, 1L);
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
errNum = curl_easy_setopt(curl, CURLOPT_SSLKEYTYPE, "ENG");
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
errNum = curl_easy_setopt(curl, CURLOPT_KEYPASSWD, "");
if (CURLE_OK != errNum)
return handle_curl_error(curl, errNum, errBuff);
return CURLE_OK;
} /* apply_tpm_engine */
#endif /* __TPM__ */
/* */
/* Build the HTTP request header slist: Content-Type, Accept, */
/* Content-Length, and optionally the client cert header. */
/* */
/* @param postData - the JSON body (used only to compute Content-Length) */
/* @param certBytes - PEM cert bytes to embed in header, or NULL to skip */
/* @return allocated slist on success, NULL on failure */
/* */
static struct curl_slist *
build_request_headers(const char *postData, const unsigned char *certBytes) {
struct curl_slist *list = NULL;
list = curl_slist_append(NULL, "Content-Type: application/json");
if (!list)
return NULL;
list = curl_slist_append(list, "Accept: application/json");
if (!list)
return NULL;
char clBuf[30];
(void)snprintf(clBuf, sizeof(clBuf), "Content-Length: %d",
(int)strlen(postData));
list = curl_slist_append(list, clBuf);
if (!list)
return NULL;
if (add_client_cert_to_header && certBytes) {
log_debug("%s::%s(%d) : Adding client cert to %s header", LOG_INF,
CLIENT_CERT_HEADER);
/* Copy into a local buffer so stripNewlines does not modify the */
/* original cert bytes, which may be needed for error reporting. */
char certBuf[MAX_CERT_SIZE];
strncpy(certBuf, (const char *)certBytes, sizeof(certBuf) - 1);
certBuf[sizeof(certBuf) - 1] = '\0';
stripNewlines(certBuf);
char headerBuf[MAX_CERT_SIZE + 32];
(void)snprintf(headerBuf, sizeof(headerBuf), "%s: %s",
CLIENT_CERT_HEADER, certBuf);
list = curl_slist_append(list, headerBuf);
} else {
log_debug("%s::%s(%d) : Skipping %s header", LOG_INF,
CLIENT_CERT_HEADER);
}
return list;
} /* build_request_headers */
/* */
/* Attach the POST body and write callback to the curl handle, then set */
/* the pre-built header slist. */
/* */
/* Takes ownership of list on success; on failure list is freed here and */
/* the caller should not free it again. */
/* */
static int attach_post_body(CURL *curl, char *postData, struct curl_slist *list,
struct MemoryStruct *chunk, char *errBuff) {
int errNum =
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteMemoryCallback);
if (CURLE_OK != errNum)
goto fail;
errNum = curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void *)chunk);
if (CURLE_OK != errNum)
goto fail;
errNum = curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
if (CURLE_OK != errNum)
goto fail;
errNum = curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postData);
if (CURLE_OK != errNum)
goto fail;
errNum =
curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, (int)strlen(postData));
if (CURLE_OK != errNum)
goto fail;
#ifdef __QATESTING__
log_qa("%s::%s(%d) : postData = %s", LOG_INF, postData);
#else
log_trace("%s::%s(%d) : postData = %s", LOG_INF, postData);
#endif
return CURLE_OK;
fail:
curl_slist_free_all(list);
return handle_curl_error(curl, errNum, errBuff);
} /* attach_post_body */
/* */
/* Execute the curl request with retry logic, check the HTTP response code, */
/* and allocate the response string on success. */
/* */
/* The retry sleep is applied before each attempt after the first, so a */
/* retryCount of 1 makes exactly one attempt with no sleep. */
/* A retryCount <= 0 is treated as 1 (at least one attempt always occurs). */
/* */
/* @param curl - fully configured curl handle */
/* @param retryCount - total number of attempts */
/* @param retryInterval - seconds between attempts */
/* @param chunk - response accumulator populated by the callback */
/* @param pRespData - out: heap-allocated response body on success */
/* @param errBuff - curl error buffer */
/* @return 0 on success */
/* 1-99 CURLcode on transport failure */
/* 255 out of memory allocating *pRespData */
/* 300-511 HTTP error response code */
/* */
static int execute_with_retry(CURL *curl, int retryCount, int retryInterval,
struct MemoryStruct *chunk, char **pRespData,
char *errBuff) {
int res = CURLE_FAILED_INIT;
long httpCode = 0;
int attempts = (retryCount > 0) ? retryCount : 1;
for (int i = 0; i < attempts; i++) {
if (i > 0 && retryInterval > 0) {
log_verbose("%s::%s(%d) : Retry %d/%d — sleeping %d seconds",
LOG_INF, i + 1, attempts, retryInterval);
(void)sleep((unsigned int)retryInterval);
}
res = curl_easy_perform(curl);
(void)curl_easy_getinfo(curl, CURLINFO_HTTP_CODE, &httpCode);
log_verbose("%s::%s(%d) : Attempt %d/%d — curl=%d httpCode=%ld",
LOG_INF, i + 1, attempts, res, httpCode);
if (CURLE_OK == res && httpCode < 300)
break;
}
/* Evaluate final outcome */
if (CURLE_OK != res) {
if (is_log_trace() && errBuff[0] != '\0') {
size_t len = strlen(errBuff);
log_error("%s::%s(%d) : libcurl (%d): %s%s", LOG_INF, res, errBuff,
(errBuff[len - 1] != '\n') ? "\n" : "");
} else {
log_error("%s::%s(%d) : libcurl (%d): %s", LOG_INF, res,
curl_easy_strerror(res));
}
return res;
}
if (httpCode >= 300) {
log_error("%s::%s(%d) : HTTP error: %ld", LOG_INF, httpCode);
return (int)httpCode;
}
log_verbose("%s::%s(%d) : %lu bytes received", LOG_INF,
(unsigned long)chunk->size);
*pRespData = strdup(chunk->memory);
if (!*pRespData) {
log_error("%s::%s(%d) : Out of memory allocating response", LOG_INF);
return 255;
}
#ifdef __QATESTING__
log_qa("%s::%s(%d) : Response:\n%s", LOG_INF, *pRespData);
#else
log_trace("%s::%s(%d) : Response:\n%s", LOG_INF, *pRespData);
#endif
return 0;
} /* execute_with_retry */
/******************************************************************************/
/*********************** GLOBAL FUNCTION DEFINITIONS **************************/
/******************************************************************************/
/* */
/* Issue an HTTP POST with JSON content and accept headers, with optional */
/* basic auth, trust store, and mTLS client certificate. */
/* */
/* @param url - URL to POST to */
/* @param username - basic auth username, or NULL to skip */
/* @param password - basic auth password, or NULL to skip */
/* @param trustStore - path to CA bundle file, or NULL to use system */
/* @param clientCert - path to client certificate file */
/* @param clientKey - path to client private key file */
/* @param clientKeyPass - password for client key, or NULL */
/* @param postData - null-terminated JSON string to POST */
/* @param pRespData - out: dynamically allocated response body; */
/* caller must free on success */
/* @param retryCount - total number of attempts (clamped to min 1) */
/* @param retryInterval - seconds to wait between attempts */
/* @return 0 on success */
/* 1-99 cURL error code */
/* 255 response memory allocation failure */
/* 300-511 HTTP error response code */
/* */
int http_post_json(const char *url, const char *username, const char *password,
const char *trustStore, const char *clientCert,
const char *clientKey, const char *clientKeyPass,
char *postData, char **pRespData, int retryCount,
int retryInterval) {
#ifdef __QATESTING__
log_qa("%s::%s(%d) : Preparing to POST to %s", LOG_INF, url);
#else
log_info("%s::%s(%d) : Preparing to POST to %s", LOG_INF, url);
#endif
int toReturn = CURLE_FAILED_INIT;
unsigned char *client_cert_bytes = NULL;
char errBuff[CURL_ERROR_SIZE];
errBuff[0] = '\0';
struct MemoryStruct chunk = {.memory = calloc(1, 1), .size = 0};
if (!chunk.memory) {
log_error("%s::%s(%d) : Out of memory allocating response buffer",
LOG_INF);
return CURLE_FAILED_INIT;
}
CURL *curl = curl_easy_init();
if (!curl) {
log_error("%s::%s(%d) : curl_easy_init() failed", LOG_INF);
free(chunk.memory);
return CURLE_FAILED_INIT;
}
/* ------------------------------------------------------------------ */
/* Step 1: Core handle options (URL, timeout, HTTP version, verbose) */
/* ------------------------------------------------------------------ */
toReturn = setup_curl_handle(curl, url, errBuff);
if (CURLE_OK != toReturn)
goto exit;
/* ------------------------------------------------------------------ */
/* Step 2: TPM engine (compiled out on non-TPM builds) */
/* ------------------------------------------------------------------ */
#if defined(__TPM__)
if (!ConfigData->EnrollOnStartup) {
toReturn = apply_tpm_engine(curl, errBuff);
if (CURLE_OK != toReturn)
goto exit;
} else {
log_info("%s::%s(%d) : Skipping TPM setup — enroll on startup active",
LOG_INF);
}
#endif
/* ------------------------------------------------------------------ */
/* Step 3: Authentication and TLS material */
/* ------------------------------------------------------------------ */
toReturn = apply_basic_auth(curl, username, password, errBuff);
if (CURLE_OK != toReturn)
goto exit;
toReturn = apply_trust_store(curl, trustStore, errBuff);
if (CURLE_OK != toReturn)
goto exit;
toReturn = apply_client_cert(curl, clientCert, clientKey, clientKeyPass,
&client_cert_bytes, errBuff);
if (CURLE_OK != toReturn)
goto exit;
/* ------------------------------------------------------------------ */
/* Step 4: Build headers and attach POST body */
/* ------------------------------------------------------------------ */
struct curl_slist *list =
build_request_headers(postData, client_cert_bytes);
if (!list) {
log_error("%s::%s(%d) : Failed to build request headers", LOG_INF);
toReturn = CURLE_OUT_OF_MEMORY;
goto exit;
}
toReturn = attach_post_body(curl, postData, list, &chunk, errBuff);
if (CURLE_OK != toReturn)
goto exit; /* list already freed by attach_post_body on failure */
/* ------------------------------------------------------------------ */
/* Step 5: Execute with retry and harvest response */
/* ------------------------------------------------------------------ */
toReturn = execute_with_retry(curl, retryCount, retryInterval, &chunk,
pRespData, errBuff);
exit:
curl_easy_cleanup(curl);
free(chunk.memory);
if (client_cert_bytes)
free(client_cert_bytes);
return toReturn;
} /* http_post_json */
/******************************************************************************/
/******************************* END OF FILE **********************************/
/******************************************************************************/