-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmanagement.c
More file actions
586 lines (527 loc) · 22.8 KB
/
management.c
File metadata and controls
586 lines (527 loc) · 22.8 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
/******************************************************************************/
/* 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 */
/* thespecific language governing permissions and limitations under the */
/* License. */
/******************************************************************************/
#include "management.h"
#include "httpclient.h"
#include "lib/base64.h"
#include "logging.h"
#include "utils.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/stat.h>
#ifdef __WOLF_SSL__
#include "wolfssl_wrapper/wolfssl_wrapper.h"
#else
#ifdef __OPEN_SSL__
#include "openssl_wrapper/openssl_wrapper.h"
#else
#ifdef __TPM__
#else
#endif
#endif
#endif
/******************************************************************************/
/***************************** LOCAL DEFINES *********************************/
/******************************************************************************/
/******************************************************************************/
/************************ LOCAL GLOBAL STRUCTURES *****************************/
/******************************************************************************/
/******************************************************************************/
/************************* LOCAL GLOBAL VARIABLES *****************************/
/******************************************************************************/
/******************************************************************************/
/************************ LOCAL FUNCTION DEFINITIONS **************************/
/******************************************************************************/
/**
* @brief Requests the detailed management job configuration from the platform.
*
* @param[in] sessionToken GUID for the current curl session.
* @param[in] jobId Platform GUID identifying this job.
* @param[in] endpoint Relative URL for the configuration endpoint.
* @param[out] pManConf Receives the parsed platform response.
* @return 0 on success, HTTP response code on failure.
*/
static int get_management_config(const char *sessionToken, const char *jobId,
const char *endpoint,
ManagementConfigResp_t **pManConf) {
char *url = NULL;
char *jsonReq = NULL;
char *jsonResp = NULL;
int res = 0;
log_verbose("%s::%s(%d) : Sending management config request: %s", LOG_INF,
jobId);
CommonConfigReq_t *req = CommonConfigReq_new();
if (!req) {
log_error("%s::%s(%d) : Error creating new request structure", LOG_INF);
return 999;
}
req->JobId = strdup(jobId);
req->SessionToken = strdup(sessionToken);
jsonReq = CommonConfigReq_toJson(req);
url = config_build_url(endpoint, true);
res = http_post_json(url, ConfigData->Username, ConfigData->Password,
ConfigData->TrustStore, ConfigData->AgentCert,
ConfigData->AgentKey, ConfigData->AgentKeyPassword,
jsonReq, &jsonResp, ConfigData->httpRetries,
ConfigData->retryInterval);
if (res == 0) {
*pManConf = ManagementConfigResp_fromJson(jsonResp);
if (!*pManConf) {
log_error("%s::%s(%d) : Error parsing management config response",
LOG_INF);
return 999;
}
} else {
log_error("%s::%s(%d) : Config retrieval failed with error code %d",
LOG_INF, res);
}
if (jsonReq)
free(jsonReq);
if (jsonResp)
free(jsonResp);
if (url)
free(url);
if (req)
CommonConfigReq_free(req);
return res;
} /* get_management_config */
/**
* @brief Sends job completion status and result data to the platform.
*
* @param[in] sessionToken GUID for the current curl session.
* @param[in] jobId Platform GUID identifying this job.
* @param[in] endpoint Relative URL for the completion endpoint.
* @param[in] jobStatus Numeric status code to report.
* @param[in] auditId Audit record ID associated with this job.
* @param[in] message Human-readable result or error message.
* @param[out] pManComp Receives the parsed platform acknowledgement.
* @return 0 on success, HTTP response code on failure.
*/
static int send_management_job_complete(const char *sessionToken,
const char *jobId, const char *endpoint,
int jobStatus, long auditId,
const char *message,
ManagementCompleteResp_t **pManComp) {
char *url = NULL;
char *jsonReq = NULL;
char *jsonResp = NULL;
int res = 0;
log_verbose("%s::%s(%d) : Sending management complete request: %ld "
"for session: %s",
LOG_INF, auditId, sessionToken);
CommonCompleteReq_t *req = CommonCompleteReq_new();
if (!req) {
log_error("%s::%s(%d) : Error creating new request structure", LOG_INF);
return 999;
}
req->SessionToken = strdup(sessionToken);
req->JobId = strdup(jobId);
req->Status = jobStatus;
req->AuditId = auditId;
req->Message = strdup(message);
jsonReq = CommonCompleteReq_toJson(req);
url = config_build_url(endpoint, true);
res = http_post_json(url, ConfigData->Username, ConfigData->Password,
ConfigData->TrustStore, ConfigData->AgentCert,
ConfigData->AgentKey, ConfigData->AgentKeyPassword,
jsonReq, &jsonResp, ConfigData->httpRetries,
ConfigData->retryInterval);
if (res == 0) {
*pManComp = ManagementCompleteResp_fromJson(jsonResp);
} else {
log_error("%s::%s(%d) : Job completion failed with error code %d",
LOG_INF, res);
}
if (jsonReq)
free(jsonReq);
if (jsonResp)
free(jsonResp);
if (url)
free(url);
if (req)
CommonCompleteReq_free(req);
return res;
} /* send_management_job_complete */
/**
* @brief Returns true if a certificate already exists in the inventory list.
*
* Performs a case-insensitive thumbprint comparison between the candidate
* certificate and every entry in the provided inventory list.
*
* @param[in] pemList Inventory list to search.
* @param[in] certToAdd Certificate whose thumbprint is being sought.
* @return true if a matching thumbprint is found, false otherwise.
*/
static bool cert_exists_in_store(const PemInventoryList *pemList,
const PemInventoryItem *certToAdd) {
if (!certToAdd->thumbprint_string)
return false;
for (int i = 0; i < pemList->item_count; i++) {
if (!pemList->items[i]->thumbprint_string)
continue;
log_trace("%s::%s(%d) : Comparing thumbprints:\n%s\n%s", LOG_INF,
certToAdd->thumbprint_string,
pemList->items[i]->thumbprint_string);
if (0 == strcasecmp(certToAdd->thumbprint_string,
pemList->items[i]->thumbprint_string)) {
return true;
}
}
return false;
} /* cert_exists_in_store */
/**
* @brief Adds a PEM certificate to the specified certificate store.
*
* Reads the existing store inventory, checks for a duplicate by thumbprint,
* and appends the certificate only if it is not already present.
*
* @param[in] storePath Filesystem path to the target certificate store.
* @param[in] certASCII PEM-encoded certificate to add.
* @param[out] pMessage Accumulates human-readable status messages.
* @param[out] pStatus Receives the result status of the operation.
* @return 0 on success, -1 on failure.
*/
static int add_cert_to_store(const char *storePath, const char *certASCII,
char **pMessage,
enum AgentApiResultStatus *pStatus) {
PemInventoryItem *certToAdd = NULL;
PemInventoryList *pemList = NULL;
int ret = 0;
log_trace("%s::%s(%d) : Creating a new PemInventoryItem for certificate",
LOG_INF);
if (!ssl_PemInventoryItem_create(&certToAdd, certASCII)) {
log_error("%s::%s(%d) : Error creating cert thumbprint or invalid cert",
LOG_INF);
append_linef(
pMessage,
"%s::%s(%d) : Error creating cert thumbprint or invalid cert",
LOG_INF);
*pStatus = STAT_ERR;
return -1;
}
log_trace("%s::%s(%d) : New certificate thumbprint: %s", LOG_INF,
certToAdd->thumbprint_string);
log_trace("%s::%s(%d) : Reading cert store %s inventory", LOG_INF,
storePath);
if (0 != ssl_read_store_inventory(storePath, NULL, &pemList)) {
log_error("%s::%s(%d) : Error reading PEM store at %s", LOG_INF,
storePath);
append_linef(pMessage, "%s::%s(%d) : Error reading PEM store at %s",
LOG_INF, storePath);
*pStatus = STAT_ERR;
ret = -1;
goto cleanup;
}
log_trace("%s::%s(%d) : Found %d certs in store", LOG_INF,
pemList->item_count);
if (cert_exists_in_store(pemList, certToAdd)) {
log_warn("%s::%s(%d) : Certificate with thumbprint %s already present "
"in store %s",
LOG_INF, certToAdd->thumbprint_string, storePath);
append_linef(pMessage,
"%s::%s(%d) : WARNING: Certificate with thumbprint %s "
"was already present in store %s",
LOG_INF, certToAdd->thumbprint_string, storePath);
*pStatus = STAT_WARN;
goto cleanup;
}
log_trace("%s::%s(%d) : Adding cert with thumbprint %s to store %s",
LOG_INF, certToAdd->thumbprint_string, storePath);
if (!ssl_Store_Cert_add(storePath, certASCII)) {
log_error("%s::%s(%d) : Error writing cert to store", LOG_INF);
append_linef(pMessage, "%s::%s(%d) Error writing cert to store",
LOG_INF);
*pStatus = STAT_ERR;
ret = -1;
} else {
log_verbose("%s::%s(%d) : Certificate successfully written to store",
LOG_INF);
*pStatus = STAT_SUCCESS;
}
cleanup:
if (certToAdd)
PemInventoryItem_free(certToAdd);
if (pemList)
PemInventoryList_free(pemList);
return ret;
} /* add_cert_to_store */
/**
* @brief Removes a certificate and its associated key from a store.
*
* @param[in] storePath Filesystem path to the certificate store.
* @param[in] searchThumb SHA-1 thumbprint of the certificate to remove.
* @param[in] keyPath Optional path to the associated private key file.
* Pass NULL if the key is embedded in the store.
* @param[in] password Password for an encrypted key, or NULL.
* @param[out] pMessage Accumulates human-readable status messages.
* @param[out] pStatus Receives the result status of the operation.
* @return 0 on success, -1 on failure.
*/
static int remove_cert_from_store(const char *storePath,
const char *searchThumb, const char *keyPath,
const char *password, char **pMessage,
enum AgentApiResultStatus *pStatus) {
if (!ssl_remove_cert_from_store(storePath, searchThumb, keyPath,
password)) {
log_error("%s::%s(%d) : Unable to remove cert from store at %s",
LOG_INF, storePath);
append_linef(pMessage, "Unable to remove cert from store at %s",
storePath);
*pStatus = STAT_ERR;
return -1;
}
return 0;
} /* remove_cert_from_store */
/**
* @brief Validates the management store configuration received from the
* platform.
*
* Checks that a store path was provided, that it is a file and not a directory,
* that it is not the agent's own certificate store, and that it exists on disk.
*
* @param[in] manConf Management configuration response to validate.
* @param[out] statusMessage Accumulates human-readable validation failure
* messages.
* @return true if all validation checks pass, false if any check fails.
*/
static bool management_store_config_valid(ManagementConfigResp_t *manConf,
char **statusMessage) {
if (!manConf->Job.StorePath) {
log_error("%s::%s(%d) : Job doesn't contain a target store to manage.",
LOG_INF);
append_linef(statusMessage,
"Job doesn't contain a target store to manage.");
return false;
}
if (is_directory(manConf->Job.StorePath)) {
log_error(
"%s::%s(%d) : The store path must be a file and not a directory.",
LOG_INF);
append_linef(statusMessage,
"The store path must be a file and not a directory.");
return false;
}
if (ConfigData->UseAgentCert && ConfigData->AgentCert &&
0 == strcasecmp(ConfigData->AgentCert, manConf->Job.StorePath)) {
log_warn("%s::%s(%d) : Attempting a Management job on the agent cert "
"store is not allowed.",
LOG_INF);
append_linef(statusMessage,
"Attempting a Management job on the agent cert store is "
"not allowed.");
return false;
}
if (!file_exists(manConf->Job.StorePath)) {
log_warn("%s::%s(%d) : Attempting to manage a certificate store that "
"does not exist yet.",
LOG_INF);
append_linef(statusMessage,
"Attempting to manage a certificate store that does not "
"exist yet.");
return false;
}
return true;
} /* management_store_config_valid */
/**
* @brief Executes the ADD operation for a management job.
*
* Rejects PFX (private key entry) additions as unsupported, then delegates
* to add_cert_to_store for plain certificate additions.
*
* @param[in] manConf Management configuration containing job parameters.
* @param[out] pMessage Accumulates human-readable status messages.
* @param[out] pStatus Receives the result status of the operation.
* @return 0 on success, non-zero on failure.
*/
static int handle_op_add(const ManagementConfigResp_t *manConf, char **pMessage,
enum AgentApiResultStatus *pStatus) {
if (manConf->Job.PrivateKeyEntry) {
const char *msg = "Adding a PFX is not supported at this time";
log_info("%s::%s(%d) : %s", LOG_INF, msg);
append_line(pMessage, msg);
*pStatus = STAT_ERR;
return 999;
}
if (!manConf->Job.EntryContents) {
log_error("%s::%s(%d) : EntryContents is NULL", LOG_INF);
append_line(pMessage, "EntryContents is NULL");
*pStatus = STAT_ERR;
return -1;
}
log_info("%s::%s(%d) : Attempting to add certificate to the store:\n%s",
LOG_INF, manConf->Job.EntryContents);
return add_cert_to_store(manConf->Job.StorePath, manConf->Job.EntryContents,
pMessage, pStatus);
} /* handle_op_add */
/**
* @brief Executes the REMOVE operation for a management job.
*
* Delegates directly to remove_cert_from_store using the job's alias as the
* thumbprint to search for.
*
* @param[in] manConf Management configuration containing job parameters.
* @param[out] pMessage Accumulates human-readable status messages.
* @param[out] pStatus Receives the result status of the operation.
* @return 0 on success, non-zero on failure.
*/
static int handle_op_remove(const ManagementConfigResp_t *manConf,
char **pMessage,
enum AgentApiResultStatus *pStatus) {
log_verbose("%s::%s(%d) : Remove certificate operation", LOG_INF);
return remove_cert_from_store(
manConf->Job.StorePath, manConf->Job.Alias, manConf->Job.PrivateKeyPath,
manConf->Job.StorePassword, pMessage, pStatus);
} /* handle_op_remove */
/**
* @brief Dispatches the management job to the correct operation handler.
*
* Routes OP_ADD and OP_REM to their respective handlers. Any unrecognised
* operation type is logged and reported as an error.
*
* @param[in] manConf Management configuration containing the operation type.
* @param[out] pMessage Accumulates human-readable status messages.
* @param[out] pStatus Receives the result status of the dispatched operation.
* @return 0 on success, 999 on unsupported or failed operation.
*/
static int dispatch_management_operation(const ManagementConfigResp_t *manConf,
char **pMessage,
enum AgentApiResultStatus *pStatus) {
switch (manConf->Job.OperationType) {
case OP_ADD:
log_verbose("%s::%s(%d) : Add certificate operation", LOG_INF);
return handle_op_add(manConf, pMessage, pStatus);
case OP_REM:
log_verbose("%s::%s(%d) : Remove certificate operation", LOG_INF);
return handle_op_remove(manConf, pMessage, pStatus);
default:
log_error("%s::%s(%d) : Unsupported operation type: %d", LOG_INF,
manConf->Job.OperationType);
append_linef(pMessage, "Unsupported operation type: %d",
manConf->Job.OperationType);
*pStatus = STAT_ERR;
return 999;
}
} /* dispatch_management_operation */
/**
* @brief Sends job completion to the platform and logs the outcome.
*
* Transmits the final status to the platform, then logs a success, warning,
* or error message based on the reported status value.
*
* @param[in] sessionToken GUID for the current curl session.
* @param[in] jobInfo Job descriptor containing endpoint and ID fields.
* @param[in] status Final result status of the management operation.
* @param[in] auditId Audit record ID associated with this job.
* @param[in] statusMessage Human-readable result or error message to send.
* @return 0 on success, 999 if the completion POST itself fails.
*/
static int finalize_management_job(const char *sessionToken,
const SessionJob_t *jobInfo,
enum AgentApiResultStatus status,
long auditId, const char *statusMessage) {
ManagementCompleteResp_t *manComp = NULL;
int res = send_management_job_complete(
sessionToken, jobInfo->JobId, jobInfo->CompletionEndpoint, status + 1,
auditId, statusMessage, &manComp);
if (res == 0 && manComp) {
AgentApiResult_log(manComp->Result, NULL, NULL);
}
ManagementCompleteResp_free(manComp);
if (res != 0) {
log_error("%s::%s(%d) : Failed to send management job complete",
LOG_INF);
return 999;
}
if (status >= STAT_ERR) {
log_error("%s::%s(%d) : Management job %s failed with error: %s",
LOG_INF, jobInfo->JobId, statusMessage);
} else if (status == STAT_WARN) {
log_warn("%s::%s(%d) : Management job %s completed with warning: %s",
LOG_INF, jobInfo->JobId, statusMessage);
} else {
log_info("%s::%s(%d) : Management job %s completed successfully",
LOG_INF, jobInfo->JobId);
}
return (status >= STAT_ERR) ? 999 : 0;
} /* finalize_management_job */
/******************************************************************************/
/*********************** GLOBAL FUNCTION DEFINITIONS **************************/
/******************************************************************************/
/**
* @brief Entry point for the certificate management job.
*
* Orchestrates the full management job lifecycle:
* 1. Fetches the job configuration from the platform.
* 2. Validates the store configuration.
* 3. Exits early if the job was cancelled.
* 4. Dispatches the operation (ADD or REMOVE).
* 5. Reports job completion back to the platform.
*
* @param[in] jobInfo Job descriptor received from the scheduler.
* @param[in] sessionToken GUID for the current curl session.
* @param[out] chainJob Reserved for any follow-on job to chain.
* @return 0 on success, 1 if the job was cancelled, 999 on error.
*/
int cms_job_manage(SessionJob_t *jobInfo, char *sessionToken, char **chainJob) {
ManagementConfigResp_t *manConf = NULL;
char *statusMessage = strdup("");
enum AgentApiResultStatus status = STAT_UNK;
int returnable = 0;
int res = 0;
log_info("%s::%s(%d) : Starting management job %s", LOG_INF,
jobInfo->JobId);
res = get_management_config(sessionToken, jobInfo->JobId,
jobInfo->ConfigurationEndpoint, &manConf);
if (res != 0) {
log_error("%s::%s(%d) : Failed to get management config", LOG_INF);
free(statusMessage);
return res;
}
if (!manConf) {
log_error("%s::%s(%d) : No management configuration returned from "
"the platform.",
LOG_INF);
free(statusMessage);
return 999;
}
if (!management_store_config_valid(manConf, &statusMessage)) {
finalize_management_job(sessionToken, jobInfo, STAT_ERR,
manConf->AuditId, statusMessage);
returnable = 999;
goto exit;
}
if (!AgentApiResult_log(manConf->Result, &statusMessage, &status)) {
returnable = 999;
goto exit;
}
if (manConf->JobCancelled) {
log_info("%s::%s(%d) : Job has been cancelled and will not be run",
LOG_INF);
returnable = 1;
goto exit;
}
res = dispatch_management_operation(manConf, &statusMessage, &status);
if (res != 0) {
log_error("%s::%s(%d) : Management operation failed", LOG_INF);
}
returnable = finalize_management_job(sessionToken, jobInfo, status,
manConf->AuditId, statusMessage);
exit:
ManagementConfigResp_free(manConf);
free(statusMessage);
return returnable;
} /* cms_job_manage */
/******************************************************************************/
/******************************* END OF FILE **********************************/
/******************************************************************************/