diff --git a/docs/cli_usage.adoc b/docs/cli_usage.adoc index bd44df5..bf25585 100644 --- a/docs/cli_usage.adoc +++ b/docs/cli_usage.adoc @@ -136,6 +136,22 @@ Same command with SAT authentication: | revoke | Revoke Certificate + +| sectigo-get-cert +| Fetch Certificate from Sectigo + +| sectigo-search-cert +| Search for certificates issued by Sectigo using various filters + +| sectigo-renew-cert +| Renew a Sectigo certificate if not expired, and meets renew requirements + +| sectigo-revoke-cert +| Revoke a Sectigo certificate + +| sectigo-ocsp-status +| Check OCSP status of a Sectigo certificate + |=== == *certifierUtil get-cert options* @@ -497,6 +513,358 @@ Disabled by default - Only error messages are shown. |=== +== *certifierUtil sectigo-get-cert options* + +|=== +| *Long Option* | *Short Option* | *Examples* | *Description* + +| help +| h +| --help + +-h +| Display this summary + +| common-name +| C +| --common-name + +-C +| Certificate common name + +| id +| I +| --id + +-I +| User or device ID + +| project-name +| r +| --project-name + +-r +| Project name + +| business-justification +| b +| --business-justification + +-b +| Business justification + +| subject-alt-names +| A +| --subject-alt-names + +-A +| Subject alternative names (CSV) + +| group-name +| G +| --group-name + +-G +| Group name + +| group-email +| E +| --group-email + +-E +| Group email + +| owner-first-name +| O +| --owner-first-name + +-O +| Owner first name + +| owner-last-name +| J +| --owner-last-name + +-J +| Owner last name + +| owner-email +| Z +| --owner-email + +-Z +| Owner email + +| devhub-id +| D +| --devhub-id + +-D +| DevHub ID + +| validity-days +| V +| --validity-days + +-V +| Validity days for the certificate + +| key-type +| W +| --key-type + +-W +| Key type for the certificate. Supported key types: [RSA-2048, RSA-3072, RSA-4096, RSA-8192, ECC-PRIME256V1, ECC-SECP384R1] + +| auth-token +| K +| --auth-token + +-K +| Auth token to authenticate with API + +| url +| u +| --url + +-u +| Sectigo API URL + +| config +| l +| --config + +-l +| Path to config file +|=== + +== *certifierUtil sectigo-search-cert options* +|=== +| *Long Option* | *Short Option* | *Examples* | *Description* +| help +| h +| --help + +-h +| Display this summary + +| auth-token +| K +| --auth-token + +-K +| Auth token to authenticate with API + +| group-name +| G +| --group-name + +-G +| Group name to filter the search results + +| group-email +| E +| --group-email + +-E +| Group email to filter the search results + +| status +| S +| --status + +-S +| Status to filter the search results + +| common-name +| C +| --common-name + +-C +| Common name of certificate to filter the search results + +| offset +| o +| --offset + +-o +| Offset value for paginated results + +| limit +| L +| --limit + +-L +| Limit value for paginated results + +| start-date +| f +| --start-date + +-f +| Start date to filter the search results (Format: YYYY-MM-DD) + +| end-date +| t +| --end-date + +-t +| End date to filter the search results (Format: YYYY-MM-DD) + +| certificate-id +| e +| --certificate-id + +-e +| Certificate ID to filter the search results + +| validity-start-date +| p +| --validity-start-date + +-p +| Validity start date to filter the search results (Format: YYYY-MM-DD) + +| validity-end-date +| q +| --validity-end-date + +-q +| Validity end date to filter the search results (Format: YYYY-MM-DD) + +| cert-order +| c +| --cert-order + +-c +| To fetch the certificate order leaf and the ICA and Root order, pass leaf + +| is-cn-in-san +| a +| --is-cn-in-san + +-a +| To check if the common name is also present in the subject alternative names, pass this flag + +| request-type +| y +| --request-type + +-y +| To fetch certificates based on requests. Ex. ACME, API, WEB, XCM + +| timestamp +| m +| --timestamp + +-m +| To fetch certificates based on timestamp types. Ex. createdTimestamp, approvedTimestamp, signedTimestamp, rejectedTimestamp, revokedTimestamp + +| devhub-id +| D +| --devhub-id + +-D +| To fetch certificates based on DevHub ID + +| key-type +| W +| --key-type + +-W +| To fetch certificate(s) that match this public key type/algorithm. Supported key types: [RSA-2048, RSA-3072, RSA-4096, RSA-8192, ECC-PRIME256V1, ECC-SECP384R1] + +| config +| l +| --config + +-l +| Path to config file +|=== + +== *certifierUtil sectigo-renew-cert options* +|=== +| *Long Option* | *Short Option* | *Examples* | *Description* +| help +| h +| --help + +-h +| Display this summary + +| auth-token +| K +| --auth-token + +-K +| Auth token to authenticate with API + +| common-name +| C +| --common-name + +-C +| Certificate common name (required for renewal) + +| serial-number +| N +| --serial-number + +-N +| Certificate serial number (either this or certificate ID is required for renewal) + +| certificate-id +| e +| --certificate-id + +-e +| Certificate ID (either this or serial number is required for renewal) + +| requestor-email +| s +| --requestor-email + +-s +| Requestor email (required for renewal) + +| config +| l +| --config + +-l +| Path to config file +|=== + +== *certifierUtil sectigo-revoke-cert options* +|=== +| *Long Option* | *Short Option* | *Examples* | *Description* +| help +| h +| --help + +-h +| Display this summary + +| auth-token +| K +| --auth-token + +-K +| Auth token to authenticate with API + +| common-name +| C +| --common-name + +-C +| Certificate common name + +| serial-number +| N +| --serial-number + +-N +| Certificate serial number (either this or certificate ID is required for revocation) + +| certificate-id +| e +| --certificate-id + +-e +| Certificate ID (either this or serial number is required for revocation) + +| requestor-email +| s +| --requestor-email + +-s +| Requestor email (required for revocation) + +| revocation-request-reason +| R +| --revocation-request-reason + +-R +| Revocation reason + +| config +| l +| --config + +-l +| Path to config file +|=== + +== *certifierUtil sectigo-ocsp-status options* +|=== +| *Long Option* | *Short Option* | *Examples* | *Description* +| help +| h +| --help + +-h +| Display this summary + +| cert-path +| j +| --cert-path + +-j +| Path to the certificate file (PEM format) + +| config +| l +| --config + +-l + +| Path to config file +|=== + *Configuration File* Configuration File is a file used to specify internal certifier util parameters such as timeouts, ecc curve types and other miscellaneous items. This file follows the JSON Format and can be manually editted from the `libcertifier.cfg.sample` template file present in the root directory. @@ -611,4 +979,53 @@ Note: 64-bit hex integer expected as input. | Mark request for a lite certificate. + Note: value type = `bool` +| libcertifier.sectigo.url +| "https://certs.xpki.io/api/createCertificate" +| Sectigo URL + +| libcertifier.sectigo.auth.token +| "" +| Sectigo API authentication token + +| libcertifier.sectigo.common.name +| "example.com" +| Certificate common name (CN) + +| libcertifier.sectigo.group.name +| "Example Group" +| Group name for the certificate request + +| libcertifier.sectigo.group.email +| "group@example.com" +| Group email for notifications + +| libcertifier.sectigo.id +| "user123" +| User or device ID + +| libcertifier.sectigo.owner.first.name +| "First" +| Owner's first name + +| libcertifier.sectigo.owner.last.name +| "Last" +| Owner's last name + +| libcertifier.sectigo.business.justification +| "Testing" +| Business justification for the request + +| libcertifier.sectigo.subject.alt.names +| [] +| Subject alternative names. + +Note: value type = `array of strings` Pass empty array if you don't have. + +| libcertifier.sectigo.owner.email +| "owner@example.com" +| Owner's email address + +| libcertifier.sectigo.tracking.id +| "1234" +| Tracking ID for the request + |=== diff --git a/docs/configuration.adoc b/docs/configuration.adoc index b4a31d2..6708027 100644 --- a/docs/configuration.adoc +++ b/docs/configuration.adoc @@ -2,6 +2,7 @@ xref:libcertifier.adoc[*Back to Manual*] == Configuration +== xPKI Certificates |======= | *Property Name* | *Default Value* | *Description* | libcertifier.certifier.url | https://certifier.xpki.io/v1/certifier/certificate | @@ -27,6 +28,42 @@ xref:libcertifier.adoc[*Back to Manual*] | libcertifier.ext.key.usage | clientAuth,serverAuth | (See notes below) |======= +== Sectigo Certificates +|======= +| *Property Name* | *Default Value* | *Description* +| libcertifier.sectigo.url | https://certs.xpki.io/api/createCertificate | +| libcertifier.sectigo.auth.token | | +| libcertifier.sectigo.common.name | | +| libcertifier.sectigo.group.name | | +| libcertifier.sectigo.group.email | | +| libcertifier.sectigo.id | user123 | +| libcertifier.sectigo.owner.first.name | | +| libcertifier.sectigo.owner.last.name | | +| libcertifier.sectigo.project.name | | +| libcertifier.sectigo.business.justification | | +| libcertifier.sectigo.subject.alt.names | [] | +| libcertifier.sectigo.owner.email | | +| libcertifier.sectigo.devhub.id | | +| libcertifier.sectigo.validity.days | 365 | +| libcertifier.sectigo.key.type | | +| libcertifier.sectigo.serial.number | | +| libcertifier.sectigo.certificate.id | | +| libcertifier.sectigo.requestor.email | | +| libcertifier.sectigo.revocation.request.reason | | +| libcertifier.sectigo.status | | +| libcertifier.sectigo.offset | | +| libcertifier.sectigo.limit | 10 | +| libcertifier.sectigo.start.date | | +| libcertifier.sectigo.end.date | | +| libcertifier.sectigo.validity.start.date | | +| libcertifier.sectigo.validity.end.date | | +| libcertifier.sectigo.cert.order | | +| libcertifier.sectigo.is.cn.in.san | | +| libcertifier.sectigo.request.type | | +| libcertifier.sectigo.timestamp | | +| libcertifier.cert.path | | +|======= + == Extended Key Usage values: This field can be populated with a list of values, indicating purposes for which the certificate public key can be used for. diff --git a/include/certifier/certifier_api_easy.h b/include/certifier/certifier_api_easy.h index c7fc562..216f5db 100644 --- a/include/certifier/certifier_api_easy.h +++ b/include/certifier/certifier_api_easy.h @@ -101,6 +101,16 @@ typedef enum CERTIFIER_MODE_PRINT_HELP = 65536, + CERTIFIER_MODE_SECTIGO_GET_CERT, + + CERTIFIER_MODE_SECTIGO_SEARCH_CERT, + + CERTIFIER_MODE_SECTIGO_RENEW_CERT, + + CERTIFIER_MODE_SECTIGO_REVOKE_CERT, + + CERTIFIER_MODE_SECTIGO_OCSP_STATUS, + // 131072 is unused } CERTIFIER_MODE; diff --git a/include/certifier/property.h b/include/certifier/property.h index 7e01b6e..7ee6633 100644 --- a/include/certifier/property.h +++ b/include/certifier/property.h @@ -202,6 +202,38 @@ typedef enum CERTIFIER_OPT */ CERTIFIER_OPT_MTLS_P12_PATH, CERTIFIER_OPT_MTLS_P12_PASSWORD, + + CERTIFIER_OPT_SECTIGO_AUTH_TOKEN, + CERTIFIER_OPT_SECTIGO_COMMON_NAME, + CERTIFIER_OPT_SECTIGO_GROUP_NAME, + CERTIFIER_OPT_SECTIGO_GROUP_EMAIL, + CERTIFIER_OPT_SECTIGO_ID, + CERTIFIER_OPT_SECTIGO_OWNER_FIRST_NAME, + CERTIFIER_OPT_SECTIGO_OWNER_LAST_NAME, + CERTIFIER_OPT_SECTIGO_PROJECT_NAME, + CERTIFIER_OPT_SECTIGO_BUSINESS_JUSTIFICATION, + CERTIFIER_OPT_SECTIGO_SUBJECT_ALT_NAMES, + CERTIFIER_OPT_SECTIGO_OWNER_EMAIL, + CERTIFIER_OPT_SECTIGO_URL, + CERTIFIER_OPT_SECTIGO_DEVHUB_ID, + CERTIFIER_OPT_SECTIGO_VALIDITY_DAYS, + CERTIFIER_OPT_SECTIGO_KEY_TYPE, + CERTIFIER_OPT_SECTIGO_SERIAL_NUMBER, + CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID, + CERTIFIER_OPT_SECTIGO_REQUESTOR_EMAIL, + CERTIFIER_OPT_SECTIGO_REVOCATION_REQUEST_REASON, + CERTIFIER_OPT_SECTIGO_STATUS, + CERTIFIER_OPT_SECTIGO_OFFSET, + CERTIFIER_OPT_SECTIGO_LIMIT, + CERTIFIER_OPT_SECTIGO_START_DATE, + CERTIFIER_OPT_SECTIGO_END_DATE, + CERTIFIER_OPT_SECTIGO_VALIDITY_START_DATE, + CERTIFIER_OPT_SECTIGO_VALIDITY_END_DATE, + CERTIFIER_OPT_SECTIGO_CERTIFICATE_ORDER, + CERTIFIER_OPT_SECTIGO_IS_CN_IN_SAN, + CERTIFIER_OPT_SECTIGO_REQUEST_TYPE, + CERTIFIER_OPT_SECTIGO_TIMESTAMP, + CERTIFIER_OPT_SECTIGO_CERT_PATH } CERTIFIER_OPT; diff --git a/internal_headers/certifier/certifier.h b/internal_headers/certifier/certifier.h index 2204277..e2264fa 100644 --- a/internal_headers/certifier/certifier.h +++ b/internal_headers/certifier/certifier.h @@ -21,6 +21,10 @@ #include "certifier/property.h" #include "certifier/types.h" +#include "certifier/error.h" +#include "certifier/property_internal.h" + +#define SMALL_STRING_SIZE 64 #ifdef __cplusplus extern "C" { @@ -28,7 +32,6 @@ extern "C" { /* CHUNK is the size of the memory chunk used by the zlib routines. */ #define CHUNK 10000 - #define ALLOWABLE_CHARACTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnpqrstuvwxyz0123456879" #define CERTIFIER_ERR_INIT_CERTIFIER 1000 @@ -138,7 +141,23 @@ typedef enum CERTIFIER_LOG_FATAL } CertifierLogPriority; -typedef struct Certifier Certifier; +typedef struct Map +{ + char node_address[SMALL_STRING_SIZE]; + char * base64_public_key; + unsigned char * der_public_key; + int der_public_key_len; + ECC_KEY * private_ec_key; + X509_CERT * x509_cert; +} Map; + +typedef struct Certifier +{ + CertifierPropMap * prop_map; + Map tmp_map; + CertifierError last_error; + bool sectigo_mode; +} Certifier; Certifier * certifier_new(void); @@ -165,6 +184,8 @@ bool certifier_is_option_set(Certifier * certifier, int name); */ int certifier_load_cfg_file(Certifier * certifier); +int sectigo_load_cfg_file(Certifier * certifier); + char * certifier_get_version(Certifier * certifier); /** @@ -250,6 +271,10 @@ void certifier_print_certificate(Certifier * certifier, const char * pem, int pe void certifier_print_certificate_validity(Certifier * certifier); +CertifierError sectigo_generate_certificate_signing_request(Certifier *certifier, char **out_csr_pem); + +CertifierPropMap * certifier_get_prop_map(Certifier * certifier); + #ifdef __cplusplus } #endif diff --git a/internal_headers/certifier/http.h b/internal_headers/certifier/http.h index 2ff2324..f081edd 100644 --- a/internal_headers/certifier/http.h +++ b/internal_headers/certifier/http.h @@ -41,6 +41,7 @@ typedef struct http_response { const char * error_msg; const char * payload; + size_t payload_len; int http_code; int error; } http_response; @@ -53,6 +54,8 @@ http_response * http_get(const CertifierPropMap * props, const char * url, const http_response * http_post(const CertifierPropMap * props, const char * url, const char * http_headers[], const char * body); +http_response * http_put(const CertifierPropMap * props, const char * url, const char * http_headers[], const char * body); + void http_free_response(http_response * resp); #ifdef __cplusplus diff --git a/internal_headers/certifier/property_internal.h b/internal_headers/certifier/property_internal.h index f1a79c6..d220e28 100644 --- a/internal_headers/certifier/property_internal.h +++ b/internal_headers/certifier/property_internal.h @@ -49,6 +49,8 @@ typedef struct _PropMap CertifierPropMap; */ CertifierPropMap * property_new(void); +CertifierPropMap * property_new_sectigo(void); + CertifierPropMap * property_ext(void); int property_destroy(CertifierPropMap * prop_map); @@ -76,18 +78,29 @@ int property_set_ext(CertifierPropMap * prop_map); int property_set(CertifierPropMap * prop_map, CERTIFIER_OPT name, const void * value); +int sectigo_property_set(CertifierPropMap * prop_map, int name, const void * value); + int property_set_int(CertifierPropMap * prop_map, CERTIFIER_OPT name, int value); void * property_get(CertifierPropMap * prop_map, CERTIFIER_OPT name); int property_set_defaults_from_cfg_file(CertifierPropMap * propMap); +int property_set_sectigo_defaults_from_cfg_file(CertifierPropMap * propMap); + const char * get_default_cfg_filename(); const char * get_default_ca_path(); const char * get_default_ca_info(); +/** + * Validate if a key type string is a supported Sectigo key type. + * @param key_type The key type string to validate + * @return 1 if valid, 0 otherwise + */ +int is_valid_sectigo_key_type(const char * key_type); + #ifdef __cplusplus } #endif diff --git a/internal_headers/certifier/sectigo_client.h b/internal_headers/certifier/sectigo_client.h new file mode 100644 index 0000000..b56c7f2 --- /dev/null +++ b/internal_headers/certifier/sectigo_client.h @@ -0,0 +1,147 @@ +/** + * Copyright 2019 Comcast Cable Communications Management, LLC + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef SECTIGO_CLIENT_H +#define SECTIGO_CLIENT_H + + +#include +#include +#include +#include +#include +#include +#include +#include + +extern pthread_mutex_t lock; + +#ifdef __cplusplus +extern "C" { +#endif + + +#define IMPULSE_URL "https://certs-dev.xpki.io/" +typedef struct { + const char * auth_token; + const char * common_name; + const char * group_name; + const char * group_email; + const char * id; + const char * owner_first_name; + const char * owner_last_name; + const char * project_name; + const char * business_justification; + const char * subject_alt_names; + const char * owner_email; + const char * sectigo_url; + const char * devhub_id; + size_t validity_days; + const char * key_type; +} sectigo_get_cert_param_t; + +typedef struct { + const char * auth_token; + const char * group_name; + const char * group_email; + const char * status; + const char * common_name; + const char * offset; + size_t limit; + const char * start_date; + const char * end_date; + size_t validity_start_date; + size_t validity_end_date; + const char * certificate_order; + const char * is_cn_in_san; + const char * request_type; + const char * timestamp; + const char * devhub_id; + const char * key_type; +} sectigo_search_cert_param_t; + +typedef struct { + const char * auth_token; + const char * common_name; + const char * serial_number; + const char * certificate_id; + const char * requestor_email; +} sectigo_renew_cert_param_t; + +typedef struct { + const char * auth_token; + const char * common_name; + const char * serial_number; + const char * certificate_id; + const char * requestor_email; + const char * revocation_request_reason; +} sectigo_revoke_cert_param_t; + +typedef struct { + const char * certificate_path; +} sectigo_ocsp_status_param_t; + +typedef enum { + SECTIGO_CLIENT_SUCCESS = 0, + SECTIGO_CLIENT_INVALID_ARGUMENT, + SECTIGO_CLIENT_NOT_IMPLEMENTED, + SECTIGO_CLIENT_ERROR_INTERNAL, + +} SECTIGO_CLIENT_ERROR_CODE; + +typedef enum { + SECTIGO_AUTH_X509, + SECTIGO_AUTH_SAT, +} SECTIGO_AUTH_TYPE; + +CertifierError sectigo_client_request_certificate(CertifierPropMap * props, const unsigned char * csr, +const char * node_address, const char * certifier_id, char ** out_cert); + +CertifierError sectigo_client_search_certificates(CertifierPropMap * props); + +CertifierError sectigo_client_renew_certificate(CertifierPropMap * props); + +CertifierError sectigo_client_revoke_certificate(CertifierPropMap * props); + +CertifierError sectigo_client_ocsp_status(CertifierPropMap * props); + +CertifierError sectigo_generate_certificate_signing_request(Certifier *certifier, char **out_csr_pem); + +Certifier * get_sectigo_certifier_instance(); + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_get_cert(sectigo_get_cert_param_t * params); + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_search_cert(sectigo_search_cert_param_t * params); + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_renew_cert(sectigo_renew_cert_param_t * params); + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_revoke_cert(sectigo_revoke_cert_param_t * params); + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_ocsp_status(sectigo_ocsp_status_param_t * params); + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_get_default_cert_param(sectigo_get_cert_param_t * params); + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_get_default_renew_cert_param(sectigo_renew_cert_param_t * params); + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_get_default_revoke_cert_param(sectigo_revoke_cert_param_t * params); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/internal_headers/certifier/security.h b/internal_headers/certifier/security.h index bea4030..943572a 100644 --- a/internal_headers/certifier/security.h +++ b/internal_headers/certifier/security.h @@ -199,7 +199,7 @@ X509_CERT * security_cert_list_get(X509_LIST * certs, int which); */ X509_CERT * security_cert_list_pop(X509_LIST * certs, int which); -CertifierError security_load_certs_from_pem(const char * pem, X509_LIST ** out); +CertifierError security_load_certs_from_pem(const char * pem, X509_LIST ** out, bool load_all_certs); void security_print_certs_in_list(X509_LIST * certs, XFILE output); diff --git a/libcertifier.cfg.sample b/libcertifier.cfg.sample index 570ab17..8983604 100644 --- a/libcertifier.cfg.sample +++ b/libcertifier.cfg.sample @@ -2,7 +2,7 @@ "libcertifier.certifier.url": "https://certifier.xpki.io/v1/certifier", "libcertifier.profile.name": "XFN_Matter_OP_Class_3_ICA", "libcertifier.validity.days": 365, - "libcertifier.auth.type": "X509", + "libcertifier.auth.type": "x509", "libcertifier.ecc.curve.id": "prime256v1", "libcertifier.http.connect.timeout": 20, "libcertifier.http.timeout": 20, @@ -23,5 +23,37 @@ "libcertifier.product.id":"1101", "libcertifier.cn.name":"AAAAAAAA", "libcertifier.node.id":"CCCCCCCCCCCCCCCC", - "libcertifier.ext.key.usage":"critical,clientAuth,serverAuth" + "libcertifier.ext.key.usage":"critical,clientAuth,serverAuth", + + "libcertifier.sectigo.url": "https://certs.xpki.io", + "libcertifier.sectigo.auth.token": "", + "libcertifier.sectigo.common.name": "", + "libcertifier.sectigo.group.name": "", + "libcertifier.sectigo.group.email": "", + "libcertifier.sectigo.id": "", + "libcertifier.sectigo.owner.first.name": "", + "libcertifier.sectigo.owner.last.name": "", + "libcertifier.sectigo.owner.email": "", + "libcertifier.sectigo.project.name": "", + "libcertifier.sectigo.business.justification": "", + "libcertifier.sectigo.subject.alt.names": [], + "libcertifier.sectigo.devhub.id": "", + "libcertifier.sectigo.validity.days": 365, + "libcertifier.sectigo.key.type": "", + "libcertifier.sectigo.serial.number": "", + "libcertifier.sectigo.certificate.id": "", + "libcertifier.sectigo.requestor.email": "", + "libcertifier.sectigo.revocation.request.reason": "", + "libcertifier.sectigo.status": "", + "libcertifier.sectigo.offset": "", + "libcertifier.sectigo.limit": 10, + "libcertifier.sectigo.start.date": "", + "libcertifier.sectigo.end.date": "", + "libcertifier.sectigo.validity.start.date": "", + "libcertifier.sectigo.validity.end.date": "", + "libcertifier.sectigo.cert.order": "", + "libcertifier.sectigo.is.cn.in.san": "", + "libcertifier.sectigo.request.type": "", + "libcertifier.sectigo.timestamp": "", + "libcertifier.sectigo.cert.path": "" } diff --git a/src/certifier.c b/src/certifier.c index 93b0f3f..04a3fa2 100644 --- a/src/certifier.c +++ b/src/certifier.c @@ -31,6 +31,16 @@ #include "certifier/timer.h" #include "curl/curl.h" +#include +#include +#include +#include +#include +#include +#include "certifier/log.h" +#include "certifier/error.h" +#include "certifier/property.h" + #ifndef CERTIFIER_VERSION #define CERTIFIER_VERSION "0.1-071320 (opensource)" #endif @@ -43,22 +53,8 @@ static CERTIFIER_LOG_callback logger; -typedef struct Map -{ - char node_address[SMALL_STRING_SIZE]; - char * base64_public_key; - unsigned char * der_public_key; - int der_public_key_len; - ECC_KEY * private_ec_key; - X509_CERT * x509_cert; -} Map; - -struct Certifier -{ - CertifierPropMap * prop_map; - Map tmp_map; - CertifierError last_error; -}; + + static inline void free_tmp(Certifier * certifier); @@ -392,14 +388,12 @@ static int save_x509certs_to_filesystem(Certifier * certifier, char * x509_certs CertifierError certifier_err_info = CERTIFIER_ERROR_INITIALIZER; X509_LIST * certs = NULL; const char * password = NULL; - unsigned char *x509_der; - size_t x509_len; log_info("\nTrimming x509 certificates...\n"); util_trim(x509_certs); log_info("\nLoading Certs from PKCS7...\n"); - certifier_err_info = security_load_certs_from_pem(x509_certs, &certs); + certifier_err_info = security_load_certs_from_pem(x509_certs, &certs, false); assign_last_error(certifier, &certifier_err_info); rc = certifier_err_info.application_error_code; if (certifier_err_info.application_error_code != 0) @@ -879,14 +873,14 @@ CertifierPropMap * _certifier_get_properties(Certifier * certifier) void _certifier_set_x509_cert(Certifier * certifier, const X509_CERT * cert) { security_free_cert(certifier->tmp_map.x509_cert); - X509_CERT * tmp = NULL; + const X509_CERT * tmp = NULL; if (cert != NULL) { tmp = cert; } - certifier->tmp_map.x509_cert = tmp; + certifier->tmp_map.x509_cert = (X509_CERT *)tmp; } void _certifier_set_ecc_key(Certifier * certifier, const ECC_KEY * key) @@ -1012,9 +1006,15 @@ int certifier_set_property(Certifier * certifier, int name, const void * value) NULL_CHECK(certifier); int return_code = 0; - const void * origValue = property_get(certifier->prop_map, name); - return_code = property_set(certifier->prop_map, name, value); + if (certifier->sectigo_mode) { + // Only set Sectigo properties for Sectigo logical flow + return_code = sectigo_property_set(certifier->prop_map, name, value); + } else { + // Only set XPKI properties for XPKI logical flow + return_code = property_set(certifier->prop_map, name, value); + } + if (return_code != 0) { return CERTIFIER_ERR_PROPERTY_SET + return_code; @@ -1022,38 +1022,34 @@ int certifier_set_property(Certifier * certifier, int name, const void * value) switch (name) { - case CERTIFIER_OPT_CFG_FILENAME: { + case CERTIFIER_OPT_CFG_FILENAME: + { log_info("Configuration file changed; loading settings"); - /* Blow away all settings and reload from config to avoid mixed configs */ - CertifierPropMap * orig = certifier->prop_map; - certifier->prop_map = property_new(); - if (certifier->prop_map == NULL) - { - log_error("Could not allocate enough memory to construct certifier->prop_map"); - return CERTIFIER_ERR_PROPERTY_SET_MEMORY; - } - property_set(certifier->prop_map, name, value); - - if (value != NULL) - { - return_code = certifier_load_cfg_file(certifier); - } - else - { - return_code = 0; - } + CertifierPropMap *orig = certifier->prop_map; - if (return_code == 0) - { - property_destroy(orig); - } - else - { - property_destroy(certifier->prop_map); - certifier->prop_map = orig; - return_code = property_set(certifier->prop_map, name, origValue); - log_warn("Failed to load configuration (configuration unmodified)!"); + if (value != NULL) { + if (certifier->sectigo_mode) { + + return_code = sectigo_load_cfg_file(certifier); + if (return_code != 0) { + log_warn("Failed to load Sectigo configuration!"); + return CERTIFIER_ERR_PROPERTY_SET + return_code; + } + } else { + return_code = certifier_load_cfg_file(certifier); + if (return_code == 0) { + + property_destroy(orig); + + } else { + property_destroy(certifier->prop_map); + certifier->prop_map = orig; + return_code = property_set(certifier->prop_map, name, property_get(orig, name)); + log_warn("Failed to load configuration (configuration unmodified)!"); + return CERTIFIER_ERR_PROPERTY_SET + return_code; + } + } } break; @@ -1115,6 +1111,23 @@ int certifier_load_cfg_file(Certifier * certifier) return return_code; } +int sectigo_load_cfg_file(Certifier * certifier) +{ + NULL_CHECK(certifier); + + int return_code = 0; + + // Only set Sectigo keys from config + return_code = property_set_sectigo_defaults_from_cfg_file(certifier->prop_map); + + if (return_code != 0) + { + return_code = CERTIFIER_ERR_PROPERTY_SET + return_code; + } + + return return_code; +} + char * certifier_create_info(Certifier * certifier, const int return_code, const char * output) { if (certifier == NULL) @@ -1541,3 +1554,72 @@ char * certifier_create_csr_post_data(CertifierPropMap * props, const unsigned c return json_csr; } + +CertifierError sectigo_generate_certificate_signing_request(Certifier *certifier, char **out_csr_pem) { + CertifierError rc = CERTIFIER_ERROR_INITIALIZER; + EVP_PKEY *pkey = NULL; + X509_REQ *req = NULL; + X509_NAME *name = NULL; + BIO *bio = NULL; + BUF_MEM *bptr = NULL; + char *common_name = (char *)certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_COMMON_NAME); + + if (!common_name) { + set_last_error(certifier, 14, "Common Name not set"); + rc.application_error_code = 14; + rc.application_error_msg = "Common Name not set"; + return rc; + } + + + pkey = EVP_PKEY_new(); + RSA *rsa = RSA_new(); + BIGNUM *bn = BN_new(); + BN_set_word(bn, RSA_F4); + + RSA_generate_key_ex(rsa, 2048, bn, NULL); + EVP_PKEY_assign_RSA(pkey, rsa); + BN_free(bn); + + + req = X509_REQ_new(); + X509_REQ_set_pubkey(req, pkey); + + name = X509_NAME_new(); + X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, (unsigned char*)common_name, -1, -1, 0); + X509_REQ_set_subject_name(req, name); + + if (!X509_REQ_sign(req, pkey, EVP_sha256())) { + set_last_error(certifier, 15, "Error signing CSR"); + rc.application_error_code = 15; + rc.application_error_msg = "Error signing CSR"; + goto cleanup; + } + + //CSR to PEM + bio = BIO_new(BIO_s_mem()); + PEM_write_bio_X509_REQ(bio, req); + BIO_get_mem_ptr(bio, &bptr); + + *out_csr_pem = (char *)malloc(bptr->length + 1); + memcpy(*out_csr_pem, bptr->data, bptr->length); + (*out_csr_pem)[bptr->length] = '\0'; + + rc.application_error_code = 0; + rc.application_error_msg = NULL; + +cleanup: + if (bio) BIO_free(bio); + if (req) X509_REQ_free(req); + if (name) X509_NAME_free(name); + if (pkey) EVP_PKEY_free(pkey); + return rc; +} + +CertifierPropMap * certifier_get_prop_map(Certifier * certifier) +{ + if (certifier == NULL) { + return NULL; + } + return certifier->prop_map; +} \ No newline at end of file diff --git a/src/certifier_api_easy.c b/src/certifier_api_easy.c index a0ef341..2f8368a 100644 --- a/src/certifier_api_easy.c +++ b/src/certifier_api_easy.c @@ -26,6 +26,8 @@ #include "certifier/security.h" #include "certifier/types.h" #include "certifier/util.h" +#include "certifier/sectigo_client.h" +#include "certifier/property_internal.h" #include #include @@ -90,6 +92,67 @@ "ca-path", required_argument, NULL, 'c' \ } +#define SECTIGO_GET_CERT_LONG_OPTIONS \ + { "common-name", required_argument, NULL, 'C' }, \ + { "id", required_argument, NULL, 'I' }, \ + { "project-name", required_argument, NULL, 'r' }, \ + { "business-justification", required_argument, NULL, 'b' }, \ + { "subject-alt-names", required_argument, NULL, 'A' }, \ + {"url", required_argument, NULL, 'u'}, \ + { "auth-token", required_argument, NULL, 'K' }, \ + { "group-name", required_argument, NULL, 'G' }, \ + { "group-email", required_argument, NULL, 'E' }, \ + { "owner-first-name", required_argument, NULL, 'O' }, \ + { "owner-last-name", required_argument, NULL, 'J' }, \ + { "owner-email", required_argument, NULL, 'Z' }, \ + { "config", required_argument, NULL, 'l' }, \ + { NULL, 0, NULL, 0 } + //make default arg '*' for san and ip + //only take in choices=['fte', 'contractor', 'associate'] + +#define SECTIGO_SEARCH_CERT_LONG_OPTIONS \ + { "group-name", required_argument, NULL, 'G' }, \ + { "group-email", required_argument, NULL, 'E' }, \ + { "status", required_argument, NULL, 'S' }, \ + { "common-name", required_argument, NULL, 'C' }, \ + { "offset", required_argument, NULL, 'o' }, \ + { "limit", required_argument, NULL, 'L' }, \ + { "start-date", required_argument, NULL, 'f' }, \ + { "end-date", required_argument, NULL, 't' }, \ + { "certificate-id", required_argument, NULL, 'i' }, \ + { "validity-start-date", required_argument, NULL, 'p' }, \ + { "validity-end-date", required_argument, NULL, 'q' }, \ + { "cert-order", required_argument, NULL, 'c' }, \ + { "is-cn-in-san", required_argument, NULL, 'a' }, \ + { "request-type", required_argument, NULL, 'y' }, \ + { "timestamp", required_argument, NULL, 'm' }, \ + { "devhub-id", required_argument, NULL, 'D' }, \ + { "key-type", required_argument, NULL, 'K' }, \ + { "config", required_argument, NULL, 'l' }, \ + { NULL, 0, NULL, 0 } + +#define SECTIGO_RENEW_CERT_LONG_OPTIONS \ + { "common-name", required_argument, NULL, 'C' }, \ + { "serial-number", required_argument, NULL, 'N' }, \ + { "certificate-id", required_argument, NULL, 'e' }, \ + { "requestor-email", required_argument, NULL, 's' }, \ + { "config", required_argument, NULL, 'l' }, \ + { NULL, 0, NULL, 0 } + +#define SECTIGO_REVOKE_CERT_LONG_OPTIONS \ + { "common-name", required_argument, NULL, 'C' }, \ + { "serial-number", required_argument, NULL, 'N' }, \ + { "certificate-id", required_argument, NULL, 'e' }, \ + { "requestor-email", required_argument, NULL, 's' }, \ + { "revocation-request-reason", required_argument, NULL, 'R' }, \ + { "config", required_argument, NULL, 'l' }, \ + { NULL, 0, NULL, 0 } + +#define SECTIGO_OCSP_STATUS_LONG_OPTIONS \ + { "cert-path", required_argument, NULL, 'j' }, \ + { "config", required_argument, NULL, 'l' }, \ + { NULL, 0, NULL, 0 } + static void finish_operation(CERTIFIER * easy, int return_code, const char * operation_output); // Private data @@ -130,7 +193,7 @@ static size_t get_command_opt_index(command_opt_lut_t * command_opt_lut, size_t return -1; } -static const char * get_command_opt_helper(CERTIFIER_MODE mode) +static const char * get_xpki_command_opt_helper(CERTIFIER_MODE mode) { #define BASE_HELPER \ "Usage: certifierUtil %s [OPTIONS]\n" \ @@ -304,6 +367,11 @@ CERTIFIER_MODE certifier_api_easy_get_mode(CERTIFIER * easy) { "renew-cert", CERTIFIER_MODE_RENEW_CERT }, { "print-cert", CERTIFIER_MODE_PRINT_CERT }, { "revoke", CERTIFIER_MODE_REVOKE_CERT }, + { "sectigo-get-cert", CERTIFIER_MODE_SECTIGO_GET_CERT}, + { "sectigo-search-cert", CERTIFIER_MODE_SECTIGO_SEARCH_CERT}, + { "sectigo-renew-cert", CERTIFIER_MODE_SECTIGO_RENEW_CERT}, + { "sectigo-revoke-cert", CERTIFIER_MODE_SECTIGO_REVOKE_CERT}, + { "sectigo-ocsp-status", CERTIFIER_MODE_SECTIGO_OCSP_STATUS} }; for (int i = 0; i < sizeof(command_map) / sizeof(command_map_t); ++i) @@ -739,6 +807,156 @@ static int do_print_cert(CERTIFIER * easy) return return_code; } +static int do_sectigo_get_cert(CERTIFIER * easy) +{ + int return_code = 0; + char * csr_pem = NULL; + char * cert = NULL; + + // Check for required Sectigo properties + const char *common_name = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_COMMON_NAME); + const char *project_name = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_PROJECT_NAME); + const char *business_justification = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_BUSINESS_JUSTIFICATION); + + if (util_is_empty(common_name) || util_is_empty(business_justification) || util_is_empty(project_name)) { + + finish_operation(easy, CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1, + "Missing required Sectigo flags (common-name, project-name, business-justification)"); + return CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + } + + return_code = certifier_setup_keys(easy->certifier); + if (return_code != 0) { + finish_operation(easy, return_code, NULL); + return return_code; + } + + //Generate CSR + CertifierError rc = sectigo_generate_certificate_signing_request(easy->certifier, &csr_pem); + if (rc.application_error_code != 0 || csr_pem == NULL) { + finish_operation(easy, rc.application_error_code, NULL); + return rc.application_error_code; + } + + // Call Sectigo API to request certificate + CertifierPropMap * props = certifier_easy_api_get_props(easy->certifier); + rc = sectigo_client_request_certificate(props, (unsigned char *)csr_pem, certifier_get_node_address(easy->certifier), NULL, &cert); + + XFREE(csr_pem); + + //Handle result + if (rc.application_error_code == 0 && cert != NULL) { + finish_operation(easy, 0, cert); + XFREE(cert); + return 0; + } else { + finish_operation(easy, rc.application_error_code, rc.application_error_msg); + if (cert) XFREE(cert); + return rc.application_error_code; + } +} + +static int do_sectigo_search_cert(CERTIFIER * easy) +{ + // Check for required Sectigo properties + const char *auth_token = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_AUTH_TOKEN); + + if (util_is_empty(auth_token)) { + finish_operation(easy, CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1, "Missing required Sectigo flag: auth-token"); + return CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + } + + // Call Sectigo API to search for certificates + CertifierPropMap * props = certifier_easy_api_get_props(easy->certifier); + CertifierError rc = sectigo_client_search_certificates(props); + + //Handle result + if (rc.application_error_code == 0) { + finish_operation(easy, 0, rc.application_error_msg); + return 0; + } else { + finish_operation(easy, rc.application_error_code, rc.application_error_msg); + return rc.application_error_code; + } +} + +static int do_sectigo_renew_cert(CERTIFIER * easy) +{ + // Check for required Sectigo properties + const char *common_name = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_COMMON_NAME); + const char *serial_number = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_SERIAL_NUMBER); + const char *certificate_id = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID); + const char *requestor_email = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_REQUESTOR_EMAIL); + if (util_is_empty(common_name) || (util_is_empty(serial_number) && util_is_empty(certificate_id)) || util_is_empty(requestor_email)) { + finish_operation(easy, CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1, + "Missing required Sectigo flag: (common-name, (serial-number or certificate-id), requestor-email)"); + return CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + } + + // Call Sectigo API to renew certificate + CertifierPropMap * props = certifier_easy_api_get_props(easy->certifier); + CertifierError rc = sectigo_client_renew_certificate(props); + + //Handle result + if (rc.application_error_code == 0) { + finish_operation(easy, 0, "Certificate renewed successfully"); + return 0; + } else { + finish_operation(easy, rc.application_error_code, rc.application_error_msg); + return rc.application_error_code; + } +} + +static int do_sectigo_revoke_cert(CERTIFIER * easy) +{ + // Check for required Sectigo properties + const char *common_name = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_COMMON_NAME); + const char *requestor_email = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_REQUESTOR_EMAIL); + const char *revocation_reason = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_REVOCATION_REQUEST_REASON); + if (util_is_empty(common_name) || util_is_empty(requestor_email) || util_is_empty(revocation_reason)) { + finish_operation(easy, CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1, + "Missing required Sectigo flag: (common-name, requestor-email, revocation-request-reason)"); + return CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + } + + // Call Sectigo API to revoke certificate + CertifierPropMap * props = certifier_easy_api_get_props(easy->certifier); + CertifierError rc = sectigo_client_revoke_certificate(props); + + //Handle result + if (rc.application_error_code == 0) { + finish_operation(easy, 0, "Certificate revoked successfully"); + return 0; + } else { + finish_operation(easy, rc.application_error_code, rc.application_error_msg); + return rc.application_error_code; + } +} + +static int do_sectigo_ocsp_status(CERTIFIER * easy) +{ + // Check for required Sectigo properties + const char *cert_path = certifier_get_property(easy->certifier, CERTIFIER_OPT_SECTIGO_CERT_PATH); + if (util_is_empty(cert_path)) { + finish_operation(easy, CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1, "Missing required Sectigo flag: cert-path"); + return CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + } + + // Call Sectigo API to check OCSP status + CertifierPropMap * props = certifier_easy_api_get_props(easy->certifier); + CertifierError rc = sectigo_client_ocsp_status(props); + + //Handle result + if (rc.application_error_code == 0) { + finish_operation(easy, 0, rc.application_error_msg); + return 0; + } else { + finish_operation(easy, rc.application_error_code, rc.application_error_msg); + return rc.application_error_code; + } +} + + char * certifier_api_easy_get_version(CERTIFIER * easy) { if (easy == NULL) @@ -777,7 +995,13 @@ int certifier_api_easy_print_helper(CERTIFIER * easy) "get-cert-status\n" "renew-cert\n" "print-cert\n" - "revoke\n"); + "revoke\n" + "sectigo-get-cert\n" + "sectigo-search-cert\n" + "sectigo-renew-cert\n" + "sectigo-revoke-cert\n" + "sectigo-ocsp-status\n" + ); } return 0; @@ -826,7 +1050,12 @@ static int process_command_line(CERTIFIER * easy) static const char * const get_cert_status_short_options = BASE_SHORT_OPTIONS CA_PATH_SHORT_OPTION; static const char * const renew_cert_short_options = BASE_SHORT_OPTIONS CA_PATH_SHORT_OPTION; static const char * const print_cert_short_options = BASE_SHORT_OPTIONS; - static const char * const revoke_cert_short_options = BASE_SHORT_OPTIONS CA_PATH_SHORT_OPTION; + static const char * const revoke_cert_short_options = BASE_SHORT_OPTIONS; + static const char * const sectigo_get_cert_short_options = BASE_SHORT_OPTIONS CA_PATH_SHORT_OPTION; + static const char * const sectigo_search_cert_short_options = BASE_SHORT_OPTIONS CA_PATH_SHORT_OPTION; + static const char * const sectigo_renew_cert_short_options = BASE_SHORT_OPTIONS CA_PATH_SHORT_OPTION; + static const char * const sectigo_revoke_cert_short_options = BASE_SHORT_OPTIONS CA_PATH_SHORT_OPTION; + static const char * const sectigo_ocsp_status_short_options = BASE_SHORT_OPTIONS CA_PATH_SHORT_OPTION; static const struct option get_cert_long_opts[] = { BASE_LONG_OPTIONS, GET_CRT_TOKEN_LONG_OPTIONS, GET_CERT_LONG_OPTIONS, VALIDITY_DAYS_LONG_OPTION, @@ -836,6 +1065,11 @@ static int process_command_line(CERTIFIER * easy) static const struct option renew_cert_long_opts[] = { BASE_LONG_OPTIONS, CA_PATH_LONG_OPTION, { NULL, 0, NULL, 0 } }; static const struct option print_cert_long_opts[] = { BASE_LONG_OPTIONS, { NULL, 0, NULL, 0 } }; static const struct option revoke_cert_long_opts[] = { BASE_LONG_OPTIONS, CA_PATH_LONG_OPTION, { NULL, 0, NULL, 0 } }; + static const struct option sectigo_get_cert_long_opts[] = {BASE_LONG_OPTIONS, SECTIGO_GET_CERT_LONG_OPTIONS, {NULL, 0, NULL, 0}}; + static const struct option sectigo_search_cert_long_opts[] = {BASE_LONG_OPTIONS, SECTIGO_SEARCH_CERT_LONG_OPTIONS, {NULL, 0, NULL, 0}}; + static const struct option sectigo_renew_cert_long_opts[] = {BASE_LONG_OPTIONS, SECTIGO_RENEW_CERT_LONG_OPTIONS, {NULL, 0, NULL, 0}}; + static const struct option sectigo_revoke_cert_long_opts[] = {BASE_LONG_OPTIONS, SECTIGO_REVOKE_CERT_LONG_OPTIONS, {NULL, 0, NULL, 0}}; + static const struct option sectigo_ocsp_status_long_opts[] = {BASE_LONG_OPTIONS, SECTIGO_OCSP_STATUS_LONG_OPTIONS, {NULL, 0, NULL, 0}}; static command_opt_lut_t command_opt_lut[] = { { CERTIFIER_MODE_REGISTER, get_cert_short_options, get_cert_long_opts }, @@ -844,6 +1078,11 @@ static int process_command_line(CERTIFIER * easy) { CERTIFIER_MODE_RENEW_CERT, renew_cert_short_options, renew_cert_long_opts }, { CERTIFIER_MODE_PRINT_CERT, print_cert_short_options, print_cert_long_opts }, { CERTIFIER_MODE_REVOKE_CERT, revoke_cert_short_options, revoke_cert_long_opts }, + { CERTIFIER_MODE_SECTIGO_GET_CERT, sectigo_get_cert_short_options, sectigo_get_cert_long_opts}, + { CERTIFIER_MODE_SECTIGO_SEARCH_CERT, sectigo_search_cert_short_options, sectigo_search_cert_long_opts}, + { CERTIFIER_MODE_SECTIGO_RENEW_CERT, sectigo_renew_cert_short_options, sectigo_renew_cert_long_opts}, + { CERTIFIER_MODE_SECTIGO_REVOKE_CERT, sectigo_revoke_cert_short_options, sectigo_revoke_cert_long_opts}, + { CERTIFIER_MODE_SECTIGO_OCSP_STATUS, sectigo_ocsp_status_short_options, sectigo_ocsp_status_long_opts} }; char * version_string = certifier_api_easy_get_version(easy); @@ -869,7 +1108,7 @@ static int process_command_line(CERTIFIER * easy) switch (opt) { case 'h': - XFPRINTF(stdout, get_command_opt_helper(easy->mode), easy->argv[0]); + XFPRINTF(stdout, get_xpki_command_opt_helper(easy->mode), easy->argv[0]); exit(1); case 'c': return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_CA_PATH, optarg); @@ -1067,6 +1306,116 @@ static int process_command_line(CERTIFIER * easy) case 'v': return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_LOG_LEVEL, (void *) (size_t) 0); break; + case 'C': // common-name + if (optarg) + { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_COMMON_NAME, optarg); + } + break; + case 'I': // id + if (optarg) + { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_ID, optarg); + } + break; + case 'r': // project-name + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_PROJECT_NAME, optarg); + } + break; + case 'b': // business-justification + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_BUSINESS_JUSTIFICATION, optarg); + } + break; + case 'A': // subject-alt-names + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_SUBJECT_ALT_NAMES, optarg); + } + break; + case 'K': // auth-token + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_AUTH_TOKEN, optarg); + } + break; + case 'u': // sectigo url + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_URL, optarg); + } + case 'G': // group-name + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_GROUP_NAME, optarg); + } + break; + case 'E': // group-email + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_GROUP_EMAIL, optarg); + } + break; + case 'O': // owner-first-name + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_OWNER_FIRST_NAME, optarg); + } + break; + case 'J': // owner-last-name + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_OWNER_LAST_NAME, optarg); + } + break; + case 'M': // owner-email + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_OWNER_EMAIL, optarg); + } + break; + case 'D': // DevHub ID + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_DEVHUB_ID, optarg); + } + break; + case 'V': // Validity Days for Sectigo cert + if (optarg) { + if (atoi(optarg) > 0) + { + return_code = + certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_VALIDITY_DAYS, (const void *) (size_t) atoi(optarg)); + } + else + { + log_error("Expected input to be of positive integer type"); + return_code = 1; + } + } + break; + case 'W': // Key Type + if (!is_valid_sectigo_key_type(optarg)) { + log_error("Invalid key type. Supported key types: [RSA-2048, RSA-3072, RSA-4096, RSA-8192, ECC-PRIME256V1, ECC-SECP384R1]"); + exit(0); + } + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_KEY_TYPE, optarg); + case 'R': // Revocation reason + if (optarg) { + if (optarg == NULL) { + log_error("Invalid revocation reason. Supported reasons: []"); + exit(0); + } + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_REVOCATION_REQUEST_REASON, optarg); + } + break; + case 's': // Revocation requestor email + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_REQUESTOR_EMAIL, optarg); + } + break; + case 'N': // Serial Number + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_SERIAL_NUMBER, optarg); + } + break; + case 'e': // Certificate ID + if (optarg) { + return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID, optarg); + } + break; case '?': /* Case when user enters the command as * $ ./libCertifier -p @@ -1343,10 +1692,42 @@ int certifier_api_easy_perform(CERTIFIER * easy) break; } + //For SECTIGO MODE +switch(easy -> mode){ + case CERTIFIER_MODE_NONE: + break; + + case CERTIFIER_MODE_SECTIGO_GET_CERT: + do_sectigo_get_cert(easy); + break; + + case CERTIFIER_MODE_SECTIGO_SEARCH_CERT: + do_sectigo_search_cert(easy); + break; + + case CERTIFIER_MODE_SECTIGO_RENEW_CERT: + do_sectigo_renew_cert(easy); + break; + + case CERTIFIER_MODE_SECTIGO_REVOKE_CERT: + do_sectigo_revoke_cert(easy); + break; + + case CERTIFIER_MODE_SECTIGO_OCSP_STATUS: + do_sectigo_ocsp_status(easy); + break; + + default: + finish_operation(easy, -1, "Invalid mode"); + break; +} + cleanup: return easy->last_info.error_code; } + + http_response * certifier_api_easy_http_post(const CERTIFIER * easy, const char * url, const char * http_headers[], const char * csr) { diff --git a/src/http.c b/src/http.c index 3db22fb..0113597 100644 --- a/src/http.c +++ b/src/http.c @@ -19,6 +19,7 @@ #include "certifier/http.h" #include "certifier/httpdebug.h" #include "certifier/log.h" +#include "certifier/parson.h" #include "certifier/property.h" #include "certifier/types.h" #include @@ -34,10 +35,10 @@ static void set_curl_options(CURL * curl, CertifierPropMap * prop_map) char * mtls_p12 = property_get(prop_map, CERTIFIER_OPT_MTLS_P12_PATH); char * mtls_password = property_get(prop_map, CERTIFIER_OPT_MTLS_P12_PASSWORD); - log_debug("[set_curl_options] - Host Validation=%i", host_validation); - log_debug("[set_curl_options] - Peer Validation=%i", peer_validation); - log_debug("[set_curl_options] - Debug HTTP Enabled=%i", is_debug_http_enabled); - log_debug("[set_curl_options] - Trace HTTP Enabled=%i", is_trace_http_enabled); + log_debug("[set_curl_options] - Host Validation=%i\n", host_validation); + log_debug("[set_curl_options] - Peer Validation=%i\n", peer_validation); + log_debug("[set_curl_options] - Debug HTTP Enabled=%i\n", is_debug_http_enabled); + log_debug("[set_curl_options] - Trace HTTP Enabled=%i\n", is_trace_http_enabled); // First set the URL that is about to receive our POST. http_set_curlopt(curl, CURLOPT_ACCEPT_ENCODING, ""); @@ -57,7 +58,6 @@ static void set_curl_options(CURL * curl, CertifierPropMap * prop_map) http_set_curlopt(curl, CURLOPT_SSL_VERIFYHOST, host_validation); http_set_curlopt(curl, CURLOPT_SSL_VERIFYPEER, peer_validation); - http_set_curlopt(curl, CURLOPT_FAILONERROR, 1L); http_set_curlopt(curl, CURLOPT_TIMEOUT, http_timeout); http_set_curlopt(curl, CURLOPT_CONNECTTIMEOUT, http_connect_timeout); @@ -119,11 +119,12 @@ static http_response * http_error_response(const char * msg, int error_code, int resp->error = error_code; resp->error_msg = XSTRDUP(msg); resp->http_code = http_code; + resp->payload_len = 0; } return resp; } -static http_response * http_success_response(const char * payload, int http_code, const char * errbuf, int err_code) +static http_response * http_success_response(const char * payload, size_t payload_len, int http_code, const char * errbuf, int err_code) { http_response * resp = XCALLOC(1, sizeof(http_response)); if (resp != NULL) @@ -132,11 +133,12 @@ static http_response * http_success_response(const char * payload, int http_code resp->error_msg = XSTRDUP(errbuf); resp->http_code = http_code; resp->payload = payload; // struct takes ownership of payload + resp->payload_len = payload_len; } return resp; } -static http_response * do_http(const CertifierPropMap * props, const char * url, const char * http_headers[], const char * body) +static http_response * do_http(const CertifierPropMap * props, const char * url, const char * http_headers[], const char * body, const char * http_method) { char errbuf[CURL_ERROR_SIZE] = { 0 }; @@ -177,6 +179,11 @@ static http_response * do_http(const CertifierPropMap * props, const char * url, http_set_curlopt(curl, CURLOPT_URL, url); + if (http_method != NULL) + { + http_set_curlopt(curl, CURLOPT_CUSTOMREQUEST, http_method); + } + // post if ((body != NULL) && (XSTRLEN(body) > 0)) { @@ -220,8 +227,23 @@ static http_response * do_http(const CertifierPropMap * props, const char * url, } else { - log_debug("do_http returned: %s", cf->payload); - return http_success_response(cf->payload, http_code, errbuf, res); + // Try to parse and pretty-print if it's JSON + JSON_Value *parsed_json = json_parse_string_with_comments(cf->payload); + if (parsed_json != NULL) { + char *pretty_json = json_serialize_to_string_pretty(parsed_json); + if (pretty_json) { + log_debug("do_http returned: %s", pretty_json); + json_free_serialized_string(pretty_json); + } else { + log_debug("do_http returned: %s", cf->payload); + } + json_value_free(parsed_json); + } else { + log_debug("do_http returned: %s", cf->payload); + } + return http_success_response(cf->payload, cf->size, http_code, errbuf, res); + // log_debug("do_http returned: %s", cf->payload); + // return http_success_response(cf->payload, http_code, errbuf, res); } } @@ -237,13 +259,18 @@ int http_destroy() http_response * http_get(const CertifierPropMap * props, const char * url, const char * http_headers[]) { - return do_http(props, url, http_headers, NULL); + return do_http(props, url, http_headers, NULL, "GET"); } http_response * http_post(const CertifierPropMap * props, const char * url, const char * http_headers[], const char * body) { - return do_http(props, url, http_headers, body); + return do_http(props, url, http_headers, body, "POST"); +} + +http_response * http_put(const CertifierPropMap * props, const char * url, const char * http_headers[], const char * body) +{ + return do_http(props, url, http_headers, body, "PUT"); } void http_free_response(http_response * resp) diff --git a/src/main.c b/src/main.c index 72a106f..039da52 100644 --- a/src/main.c +++ b/src/main.c @@ -20,13 +20,10 @@ #include "certifier/log.h" #include "certifier/xpki_client.h" #include "certifier/xpki_client_internal.h" - -XPKI_CLIENT_ERROR_CODE xpki_perform(int argc, char ** argv); - -int main(int argc, char ** argv) -{ - return xpki_perform(argc, argv); -} +#include "certifier/sectigo_client.h" +#include "certifier/certifier_api_easy.h" +#include "certifier/certifier_internal.h" +#include "certifier/certifier.h" typedef enum { @@ -40,6 +37,18 @@ typedef enum XPKI_MODE_REVOKE_CERT, } XPKI_MODE; +typedef enum +{ + SECTIGO_MODE_NONE, + SECTIGO_MODE_GET_CERT, + SECTIGO_MODE_SEARCH_CERT, + SECTIGO_MODE_RENEW_CERT, + SECTIGO_MODE_REVOKE_CERT, + SECTIGO_MODE_OCSP_STATUS, + SECTIGO_MODE_PRINT_HELP + +} SECTIGO_MODE; + typedef union { get_cert_param_t get_cert_param; @@ -47,6 +56,33 @@ typedef union renew_cert_param_t renew_cert_param; } xc_parameter_t; +typedef union +{ + sectigo_get_cert_param_t get_cert_param; + sectigo_search_cert_param_t search_cert_param; + sectigo_renew_cert_param_t renew_cert_param; + sectigo_revoke_cert_param_t revoke_cert_param; + sectigo_ocsp_status_param_t ocsp_status_param; +} sectigo_parameter_t; + + +XPKI_CLIENT_ERROR_CODE process(XPKI_MODE mode, xc_parameter_t * xc_parameter, int argc, char ** argv); +XPKI_CLIENT_ERROR_CODE xpki_perform(int argc, char ** argv); +SECTIGO_CLIENT_ERROR_CODE sectigo_perform(int argc, char ** argv); + +int main(int argc, char **argv) +{ + pthread_mutex_init(&lock, NULL); + // check for "sectigo-get-cert" as the first argument + if (argc > 1 && strncmp(argv[1], "sectigo", strlen("sectigo")) == 0) { + // Call Sectigo mode + return sectigo_perform(argc, argv); + } else { + // Default to XPKI mode + return xpki_perform(argc, argv); + } +} + XPKI_MODE xpki_get_mode(int argc, char ** argv) { if (argc <= 1 && argv[1] == NULL) @@ -64,7 +100,7 @@ XPKI_MODE xpki_get_mode(int argc, char ** argv) { "help", XPKI_MODE_PRINT_HELP }, { "version", XPKI_MODE_PRINT_VERSION }, { "get-cert", XPKI_MODE_GET_CERT }, { "get-cert-status", XPKI_MODE_GET_CERT_STATUS }, { "renew-cert", XPKI_MODE_RENEW_CERT }, { "print-cert", XPKI_MODE_PRINT_CERT }, - { "revoke", XPKI_CLIENT_CERT_REVOKED }, + { "revoke", XPKI_MODE_REVOKE_CERT }, }; for (int i = 0; i < sizeof(command_map) / sizeof(command_map_t); ++i) @@ -78,6 +114,31 @@ XPKI_MODE xpki_get_mode(int argc, char ** argv) return XPKI_MODE_NONE; } +SECTIGO_MODE sectigo_get_mode(int argc, char ** argv){ + typedef struct{ + char * name; + SECTIGO_MODE mode; + } command_map_t; + + command_map_t command_map[] = { + {"sectigo-get-cert", SECTIGO_MODE_GET_CERT}, + {"sectigo-search-cert", SECTIGO_MODE_SEARCH_CERT}, + {"sectigo-renew-cert", SECTIGO_MODE_RENEW_CERT}, + {"sectigo-revoke-cert", SECTIGO_MODE_REVOKE_CERT}, + {"sectigo-ocsp-status", SECTIGO_MODE_OCSP_STATUS} + }; + + for(int i = 0; i < sizeof(command_map) / sizeof(command_map_t); ++i){ + if (strcmp(argv[1], command_map[i].name) == 0){ + return command_map[i].mode; + } + + } + + + return SECTIGO_MODE_NONE; +} + XPKI_CLIENT_ERROR_CODE xpki_print_helper(XPKI_MODE mode) { if (mode == XPKI_MODE_PRINT_VERSION) @@ -105,12 +166,32 @@ XPKI_CLIENT_ERROR_CODE xpki_print_helper(XPKI_MODE mode) "get-cert-status\n" "renew-cert\n" "print-cert\n" - "revoke\n"); + "revoke\n" + "sectigo-get-cert\n" + "sectigo-search-cert\n" + "sectigo-renew-cert\n" + "sectigo-revoke-cert\n" + "sectigo-ocsp-status\n" + ); } return XPKI_CLIENT_SUCCESS; } +SECTIGO_CLIENT_ERROR_CODE sectigo_print_helper(SECTIGO_MODE mode) +{ + if (mode == SECTIGO_MODE_PRINT_HELP || mode == SECTIGO_MODE_NONE) + { + XFPRINTF(stdout, + "Usage: certifierUtil [COMMANDS] [OPTIONS]\n" + "Commands:\n" + "help\n" + "sectigo-get-cert\n"); + } + + return SECTIGO_CLIENT_SUCCESS; +} + #define BASE_SHORT_OPTIONS "hp:L:k:vm" #define GET_CRT_TOKEN_SHORT_OPTIONS "X:S:" #define GET_CERT_SHORT_OPTIONS "fT:P:o:i:n:F:a:w:" @@ -150,6 +231,24 @@ XPKI_CLIENT_ERROR_CODE xpki_print_helper(XPKI_MODE mode) "ca-path", required_argument, NULL, 'c' \ } +#define SECTIGO_GET_CERT_LONG_OPTIONS \ + { "common-name", required_argument, NULL, 'C' }, \ + { "id", required_argument, NULL, 'I' }, \ + { "project-name", required_argument, NULL, 'r' }, \ + { "business-justification", required_argument, NULL, 'b' }, \ + { "subject-alt-names", required_argument, NULL, 'A' }, \ + {"url", required_argument, NULL, 'u'}, \ + { "auth-token", required_argument, NULL, 'K' }, \ + { "group-name", required_argument, NULL, 'G' }, \ + { "group-email", required_argument, NULL, 'E' }, \ + { "owner-first-name", required_argument, NULL, 'O' }, \ + { "owner-last-name", required_argument, NULL, 'J' }, \ + { "owner-email", required_argument, NULL, 'Z' }, \ + { "config", required_argument, NULL, 'l' }, \ + { NULL, 0, NULL, 0 } \ + //make default arg '*' for san and ip + //only take in choices=['fte', 'contractor', 'associate'] + typedef struct { XPKI_MODE mode; @@ -157,6 +256,13 @@ typedef struct const struct option * long_opts; } command_opt_lut_t; +typedef struct +{ + SECTIGO_MODE mode; + const char * short_opts; + const struct option * long_opts; +} sectigo_command_opt_lut_t; + static size_t get_command_opt_index(command_opt_lut_t * command_opt_lut, size_t n_entries, XPKI_MODE mode) { for (size_t i = 0; i < n_entries; ++i) @@ -169,7 +275,92 @@ static size_t get_command_opt_index(command_opt_lut_t * command_opt_lut, size_t return -1; } -static const char * get_command_opt_helper(XPKI_MODE mode) +static const char * get_sectigo_command_opt_helper(SECTIGO_MODE mode) +{ + +#define SECTIGO_BASE_HELPER \ + "Usage: certifierUtil sectigo-get-cert [OPTIONS]\n" + +#define SECTIGO_GET_CERT_HELPER \ + "--common-name [value] (-C)\n" \ + "--id [value] (-I)\n" \ + "--project-name [value] (-r)\n" \ + "--business-justification [value] (-b)\n" \ + "--subject-alt-names [value] (-A)\n" \ + "--group-name [value] (-G)\n" \ + "--group-email [value] (-E)\n" \ + "--owner-first-name [value] (-O)\n" \ + "--owner-last-name [value] (-J)\n" \ + "--owner-email [value] (-Z)\n" \ + "--devhub-id [value] (-D)\n" \ + "--validity-days [value] (-V)\n" \ + "--key-type [value] (-W)\n" \ + "--auth-token [value] (-K)\n" \ + "--url [value] (-u)\n" \ + "--config [value] (-l)\n" \ + +#define SECTIGO_SEARCH_CERT_HELPER \ + "--auth-token [value] (-K)\n" \ + "--common-name [value] (-C)\n" \ + "--group-name [value] (-G)\n" \ + "--group-email [value] (-E)\n" \ + "--status [value] (-S)\n" \ + "--offset [value] (-o)\n" \ + "--limit [value] (-L)\n" \ + "--start-date [value] (-f)\n" \ + "--end-date [value] (-t)\n" \ + "--certificate-id [value] (-i)\n" \ + "--validity-start-date [value] (-p)\n" \ + "--validity-end-date [value] (-q)\n" \ + "--cert-order [value] (-c)\n" \ + "--is-cn-in-san [value] (-a)\n" \ + "--request-type [value] (-y)\n" \ + "--timestamp [value] (-m)\n" \ + "--devhub-id [value] (-D)\n" \ + "--key-type [value] (-W)\n" \ + "--config [value] (-l)\n" \ + +#define SECTIGO_RENEW_CERT_HELPER \ + "--auth-token [value] (-K)\n" \ + "--common-name [value] (-C)\n" \ + "--serial-number [value] (-N)\n" \ + "--certificate-id [value] (-i)\n" \ + "--requestor-email [value] (-s)\n" \ + "--config [value] (-l)\n" \ + +#define SECTIGO_REVOKE_CERT_HELPER \ + "--auth-token [value] (-K)\n" \ + "--common-name [value] (-C)\n" \ + "--serial-number [value] (-N)\n" \ + "--certificate-id [value] (-i)\n" \ + "--requestor-email [value] (-s)\n" \ + "--revocation-request-reason [value] (-R)\n" \ + "--config [value] (-l)\n" \ + +#define SECTIGO_OCSP_STATUS_HELPER \ + "--cert-path [value] (-j)\n" \ + "--config [value] (-l)\n" \ + + switch (mode) + { + case SECTIGO_MODE_GET_CERT: + return SECTIGO_BASE_HELPER SECTIGO_GET_CERT_HELPER; + case SECTIGO_MODE_SEARCH_CERT: + return SECTIGO_BASE_HELPER SECTIGO_SEARCH_CERT_HELPER; + case SECTIGO_MODE_RENEW_CERT: + return SECTIGO_BASE_HELPER SECTIGO_RENEW_CERT_HELPER; + case SECTIGO_MODE_REVOKE_CERT: + return SECTIGO_BASE_HELPER SECTIGO_REVOKE_CERT_HELPER; + case SECTIGO_MODE_OCSP_STATUS: + return SECTIGO_BASE_HELPER SECTIGO_OCSP_STATUS_HELPER; + case SECTIGO_MODE_PRINT_HELP: + return SECTIGO_BASE_HELPER; + default: + return ""; + } +} + +static const char * get_xpki_command_opt_helper(XPKI_MODE mode) { #define BASE_HELPER \ "Usage: certifierUtil %s [OPTIONS]\n" \ @@ -234,8 +425,7 @@ XPKI_CLIENT_ERROR_CODE process(XPKI_MODE mode, xc_parameter_t * xc_parameter, in break; default: return XPKI_CLIENT_NOT_IMPLEMENTED; - } - + } static const char * const get_cert_short_options = BASE_SHORT_OPTIONS GET_CRT_TOKEN_SHORT_OPTIONS GET_CERT_SHORT_OPTIONS VALIDITY_DAYS_SHORT_OPTION CA_PATH_SHORT_OPTION; static const char * const get_cert_status_short_options = BASE_SHORT_OPTIONS CA_PATH_SHORT_OPTION; @@ -270,9 +460,8 @@ XPKI_CLIENT_ERROR_CODE process(XPKI_MODE mode, xc_parameter_t * xc_parameter, in switch (opt) { case 'h': - XFPRINTF(stdout, get_command_opt_helper(mode), argv[0]); + XFPRINTF(stdout, get_xpki_command_opt_helper(mode), argv[0]); exit(0); - break; case 'c': // return_code = certifier_set_property(easy->certifier, CERTIFIER_OPT_CA_PATH, optarg); break; @@ -393,7 +582,7 @@ XPKI_CLIENT_ERROR_CODE process(XPKI_MODE mode, xc_parameter_t * xc_parameter, in } else if (optopt == 'D') { - log_info("Missing mandatory custom property option"); + log_info("Missing mandatory custom property option"); error_code = XPKI_CLIENT_INVALID_ARGUMENT; break; } @@ -451,6 +640,323 @@ XPKI_CLIENT_ERROR_CODE process(XPKI_MODE mode, xc_parameter_t * xc_parameter, in return error_code; } +// --- Sectigo Option Table --- +static const char * const sectigo_get_cert_short_options = "C:I:r:b:A:K:u:G:E:O:J:Z:W:V:D:l:h"; +static const char * const sectigo_search_cert_short_options = "K:C:G:E:S:o:L:f:t:i:p:q:c:a:y:m:D:W:l:h"; +static const char * const sectigo_renew_cert_short_options = "K:C:N:i:s:l:h"; +static const char * const sectigo_revoke_cert_short_options = "K:C:N:i:s:R:l:h"; +static const char * const sectigo_ocsp_status_short_options = "j:l:h"; + +static const struct option sectigo_get_cert_long_opts[] = { + { "common-name", required_argument, NULL, 'C' }, + { "id", required_argument, NULL, 'I' }, + { "project-name", required_argument, NULL, 'r' }, + { "business-justification", required_argument, NULL, 'b' }, + { "subject-alt-names", required_argument, NULL, 'A' }, + { "url", required_argument, NULL, 'u'}, + { "auth-token", required_argument, NULL, 'K' }, + { "group-name", required_argument, NULL, 'G' }, + { "group-email", required_argument, NULL, 'E' }, + { "owner-first-name", required_argument, NULL, 'O' }, + { "owner-last-name", required_argument, NULL, 'J' }, + { "owner-email", required_argument, NULL, 'Z' }, + { "devhub-id", required_argument, NULL, 'D' }, + { "validity-days", required_argument, NULL, 'V' }, + { "key-type", required_argument, NULL, 'W' }, + { "config", required_argument, NULL, 'l' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } +}; + +static const struct option sectigo_search_cert_long_opts[] = { + { "auth-token", required_argument, NULL, 'K' }, + { "common-name", required_argument, NULL, 'C' }, + { "group-name", required_argument, NULL, 'G' }, + { "group-email", required_argument, NULL, 'E' }, + { "status", required_argument, NULL, 'S' }, + { "offset", required_argument, NULL, 'o' }, + { "limit", required_argument, NULL, 'L' }, + { "start-date", required_argument, NULL, 'f' }, + { "end-date", required_argument, NULL, 't' }, + { "certificate-id", required_argument, NULL, 'i' }, + { "validity-start-date", required_argument, NULL, 'p' }, + { "validity-end-date", required_argument, NULL, 'q' }, + { "cert-order", required_argument, NULL, 'c' }, + { "is-cn-in-san", required_argument, NULL, 'a' }, + { "request-type", required_argument, NULL, 'y' }, + { "timestamp", required_argument, NULL, 'm' }, + { "devhub-id", required_argument, NULL, 'D' }, + { "key-type", required_argument, NULL, 'W' }, + { "config", required_argument, NULL, 'l' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } +}; + +static const struct option sectigo_renew_cert_long_opts[] = { + { "auth-token", required_argument, NULL, 'K' }, + { "common-name", required_argument, NULL, 'C' }, + { "serial-number", required_argument, NULL, 'N' }, + { "certificate-id", required_argument, NULL, 'i' }, + { "requestor-email", required_argument, NULL, 's' }, + { "config", required_argument, NULL, 'l' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } +}; + +static const struct option sectigo_revoke_cert_long_opts[] = { + { "auth-token", required_argument, NULL, 'K' }, + { "common-name", required_argument, NULL, 'C' }, + { "serial-number", required_argument, NULL, 'N' }, + { "certificate-id", required_argument, NULL, 'i' }, + { "requestor-email", required_argument, NULL, 's' }, + { "revocation-request-reason", required_argument, NULL, 'R' }, + { "config", required_argument, NULL, 'l' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } +}; + +static const struct option sectigo_ocsp_status_long_opts[] = { + { "cert-path", required_argument, NULL, 'j' }, + { "config", required_argument, NULL, 'l' }, + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } +}; + +static sectigo_command_opt_lut_t sectigo_command_opt_lut[] = { + { SECTIGO_MODE_GET_CERT, sectigo_get_cert_short_options, sectigo_get_cert_long_opts }, + { SECTIGO_MODE_SEARCH_CERT, sectigo_search_cert_short_options, sectigo_search_cert_long_opts }, + { SECTIGO_MODE_RENEW_CERT, sectigo_renew_cert_short_options, sectigo_renew_cert_long_opts }, + { SECTIGO_MODE_REVOKE_CERT, sectigo_revoke_cert_short_options, sectigo_revoke_cert_long_opts }, + { SECTIGO_MODE_OCSP_STATUS, sectigo_ocsp_status_short_options, sectigo_ocsp_status_long_opts }, +}; + +static size_t get_sectigo_command_opt_index(sectigo_command_opt_lut_t * lut, size_t n_entries, SECTIGO_MODE mode) +{ + for (size_t i = 0; i < n_entries; ++i) + { + if (lut[i].mode == mode) + { + return i; + } + } + return 0; // fallback +} + +// --- Sectigo Option Parsing --- +SECTIGO_CLIENT_ERROR_CODE sectigo_process(SECTIGO_MODE mode, sectigo_parameter_t * sectigo_parameter, int argc, char ** argv) +{ + VerifyOrReturnError(sectigo_parameter != NULL, SECTIGO_CLIENT_INVALID_ARGUMENT); + VerifyOrReturnError(argv != NULL, SECTIGO_CLIENT_INVALID_ARGUMENT); + + SECTIGO_CLIENT_ERROR_CODE error_code = SECTIGO_CLIENT_SUCCESS; + switch (mode) + { + case SECTIGO_MODE_GET_CERT: + ReturnErrorOnFailure(xc_sectigo_get_default_cert_param(§igo_parameter->get_cert_param)); + break; + case SECTIGO_MODE_SEARCH_CERT: + case SECTIGO_MODE_OCSP_STATUS: + // No default parameters to set for search cert or OCSP status, so just break + break; + case SECTIGO_MODE_RENEW_CERT: + ReturnErrorOnFailure(xc_sectigo_get_default_renew_cert_param(§igo_parameter->renew_cert_param)); + break; + case SECTIGO_MODE_REVOKE_CERT: + ReturnErrorOnFailure(xc_sectigo_get_default_revoke_cert_param(§igo_parameter->revoke_cert_param)); + break; + default: + return SECTIGO_CLIENT_NOT_IMPLEMENTED; + } + + for (;;) + { + int option_index = 0; + int sectigo_opt_index = get_sectigo_command_opt_index( + sectigo_command_opt_lut, + sizeof(sectigo_command_opt_lut) / sizeof(*sectigo_command_opt_lut), + mode + ); + + int opt = XGETOPT_LONG(argc, argv, + sectigo_command_opt_lut[sectigo_opt_index].short_opts, + sectigo_command_opt_lut[sectigo_opt_index].long_opts, + &option_index + ); + + if (opt == -1 || error_code != SECTIGO_CLIENT_SUCCESS) + { + break; + } + + switch (opt) + { + case 'h': + XFPRINTF(stdout, get_sectigo_command_opt_helper(mode), argv[0]); + exit(0); + case 'C': + sectigo_parameter->get_cert_param.common_name = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_COMMON_NAME, optarg); + break; + case 'I': + sectigo_parameter->get_cert_param.id = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_ID, optarg); + break; + case 'b': + sectigo_parameter->get_cert_param.business_justification = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_BUSINESS_JUSTIFICATION, optarg); + break; + case 'A': + sectigo_parameter->get_cert_param.subject_alt_names = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_SUBJECT_ALT_NAMES, optarg); + break; + case 'l': + // config file path, handled in sectigo_perform + break; + case 'G': + sectigo_parameter->get_cert_param.group_name = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_GROUP_NAME, optarg); + break; + case 'E': + sectigo_parameter->get_cert_param.group_email = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_GROUP_EMAIL, optarg); + break; + case 'O': + sectigo_parameter->get_cert_param.owner_first_name = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_OWNER_FIRST_NAME, optarg); + break; + case 'J': + sectigo_parameter->get_cert_param.owner_last_name = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_OWNER_LAST_NAME, optarg); + break; + case 'Z': + sectigo_parameter->get_cert_param.owner_email = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_OWNER_EMAIL, optarg); + break; + case 'K': + sectigo_parameter->get_cert_param.auth_token = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_AUTH_TOKEN, optarg); + break; + case 'u': + sectigo_parameter->get_cert_param.sectigo_url = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_URL, optarg); + break; + case 'D': + sectigo_parameter->get_cert_param.devhub_id = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_DEVHUB_ID, optarg); + break; + case 'V': + sectigo_parameter->get_cert_param.validity_days = atol(optarg); + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_VALIDITY_DAYS, (void *)(size_t)atol(optarg)); + break; + case 'W': + sectigo_parameter->get_cert_param.key_type = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_KEY_TYPE, optarg); + break; + case 'R': + sectigo_parameter->revoke_cert_param.revocation_request_reason = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_REVOCATION_REQUEST_REASON, optarg); + break; + case 'N': + sectigo_parameter->revoke_cert_param.serial_number = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_SERIAL_NUMBER, optarg); + break; + case 'i': + sectigo_parameter->revoke_cert_param.certificate_id = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID, optarg); + break; + case 's': + sectigo_parameter->revoke_cert_param.requestor_email = optarg; + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_REQUESTOR_EMAIL, optarg); + break; + case 'S': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_STATUS, optarg); + break; + case 'o': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_OFFSET, optarg); + break; + case 'L': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_LIMIT, (void *)(size_t)atol(optarg)); + break; + case 'f': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_START_DATE, optarg); + break; + case 't': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_END_DATE, optarg); + break; + case 'e': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID, optarg); + break; + case 'p': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_VALIDITY_START_DATE, optarg); + break; + case 'q': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_VALIDITY_END_DATE, optarg); + break; + case 'c': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_CERTIFICATE_ORDER, optarg); + break; + case 'a': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_IS_CN_IN_SAN, optarg); + break; + case 'y': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_REQUEST_TYPE, optarg); + break; + case 'm': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_TIMESTAMP, optarg); + break; + case 'j': + certifier_set_property(get_sectigo_certifier_instance(), CERTIFIER_OPT_SECTIGO_CERT_PATH, optarg); + break; + case '?': + log_info("Invalid or missing Sectigo option\n"); + error_code = SECTIGO_CLIENT_INVALID_ARGUMENT; + break; + default: + log_info("Unknown Sectigo option: %c\n", opt); + error_code = SECTIGO_CLIENT_INVALID_ARGUMENT; + break; + } + } + + return error_code; +} + +SECTIGO_CLIENT_ERROR_CODE sectigo_perform(int argc, char ** argv) +{ + SECTIGO_MODE mode = sectigo_get_mode(argc, argv); + + if (mode == SECTIGO_MODE_NONE || mode == SECTIGO_MODE_PRINT_HELP) + { + return sectigo_print_helper(mode); + } + + sectigo_parameter_t sectigo_parameter; + + ReturnErrorOnFailure(sectigo_process(mode, §igo_parameter, argc - 1, &argv[1])); + + switch (mode) + { + case SECTIGO_MODE_GET_CERT: + return xc_sectigo_get_cert(§igo_parameter.get_cert_param); + break; + case SECTIGO_MODE_SEARCH_CERT: + return xc_sectigo_search_cert(§igo_parameter.search_cert_param); + break; + case SECTIGO_MODE_RENEW_CERT: + return xc_sectigo_renew_cert(§igo_parameter.renew_cert_param); + break; + case SECTIGO_MODE_REVOKE_CERT: + return xc_sectigo_revoke_cert(§igo_parameter.revoke_cert_param); + break; + case SECTIGO_MODE_OCSP_STATUS: + return xc_sectigo_ocsp_status(§igo_parameter.ocsp_status_param); + break; + default: + break; + } + return SECTIGO_CLIENT_SUCCESS; +} + XPKI_CLIENT_ERROR_CODE xpki_perform(int argc, char ** argv) { XPKI_MODE mode = xpki_get_mode(argc, argv); @@ -472,7 +978,7 @@ XPKI_CLIENT_ERROR_CODE xpki_perform(int argc, char ** argv) case XPKI_MODE_GET_CERT_STATUS: { XPKI_CLIENT_CERT_STATUS status; ReturnErrorOnFailure(xc_get_cert_status(&xc_parameter.get_cert_status_param, &status)); - return status; + return (XPKI_CLIENT_ERROR_CODE)status; } break; case XPKI_MODE_RENEW_CERT: diff --git a/src/mbedtls.c b/src/mbedtls.c index 3589d37..cf2e0d9 100644 --- a/src/mbedtls.c +++ b/src/mbedtls.c @@ -2422,7 +2422,7 @@ static CertifierError load_certs_from_pkcs7(const char * pkcs7, X509_LIST ** out return result; } -CertifierError load_certs_from_certificate(const char * pem, X509_LIST ** out) +CertifierError load_certs_from_certificate(const char * pem, X509_LIST ** out, bool load_all_certs) { CertifierError result = CERTIFIER_ERROR_INITIALIZER; error_clear(&result); @@ -2433,6 +2433,10 @@ CertifierError load_certs_from_certificate(const char * pem, X509_LIST ** out) return result; } + // Note: mbedtls implementation currently only supports single certificate loading + // The load_all_certs parameter is accepted for API compatibility but not yet implemented + (void)load_all_certs; + X509_LIST * certs = NULL; mbedtls_pem_context pem_ctx; @@ -2488,7 +2492,7 @@ CertifierError load_certs_from_certificate(const char * pem, X509_LIST ** out) return result; } -CertifierError security_load_certs_from_pem(const char * pem, X509_LIST ** out) +CertifierError security_load_certs_from_pem(const char * pem, X509_LIST ** out, bool load_all_certs) { CertifierError result = CERTIFIER_ERROR_INITIALIZER; if (XSTRSTR(pem, "-----BEGIN PKCS7-----")) @@ -2497,7 +2501,7 @@ CertifierError security_load_certs_from_pem(const char * pem, X509_LIST ** out) } else if (XSTRSTR(pem, "-----BEGIN CERTIFICATE-----")) { - result = load_certs_from_certificate(pem, out); + result = load_certs_from_certificate(pem, out, load_all_certs); } else { diff --git a/src/openssl.c b/src/openssl.c index 0d2036c..1182830 100644 --- a/src/openssl.c +++ b/src/openssl.c @@ -1241,7 +1241,7 @@ CertifierError load_certs_from_pkcs7(const char * pkcs7, X509_LIST ** out) return result; } -CertifierError load_certs_from_certificate(const char * pem, X509_LIST ** out) +CertifierError load_certs_from_certificate(const char * pem, X509_LIST ** out, bool load_all_certs) { CertifierError result = CERTIFIER_ERROR_INITIALIZER; @@ -1259,20 +1259,38 @@ CertifierError load_certs_from_certificate(const char * pem, X509_LIST ** out) BIO * cert_bio = BIO_new(BIO_s_mem()); BIO_write(cert_bio, pem, cert_len); - cert = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL); - if (!cert) - { - result.application_error_msg = util_format_error(__func__, "Unable to parse certificate!", __FILE__, __LINE__); - goto cleanup; - } - + certs = sk_X509_new_null(); if (certs == NULL) { result.application_error_msg = util_format_error(__func__, "Unable to call sk_X509_new_null!", __FILE__, __LINE__); goto cleanup; } - sk_X509_push(certs, cert); + + if (load_all_certs) + { + // Read all certificates (i.e. for OCSP) + while ((cert = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL)) != NULL) + { + sk_X509_push(certs, cert); + } + } + else + { + // Legacy behavior loading only the first certificate from the chain + cert = PEM_read_bio_X509(cert_bio, NULL, NULL, NULL); + if (cert != NULL) + { + sk_X509_push(certs, cert); + } + } + + // Check if we read at least one certificate + if (sk_X509_num(certs) == 0) + { + result.application_error_msg = util_format_error(__func__, "Unable to parse any certificates!", __FILE__, __LINE__); + goto cleanup; + } cleanup: BIO_free(cert_bio); @@ -1286,7 +1304,7 @@ CertifierError load_certs_from_certificate(const char * pem, X509_LIST ** out) return result; } -CertifierError security_load_certs_from_pem(const char * pem, X509_LIST ** out) +CertifierError security_load_certs_from_pem(const char * pem, X509_LIST ** out, bool load_all_certs) { CertifierError result = CERTIFIER_ERROR_INITIALIZER; if (XSTRSTR(pem, "-----BEGIN PKCS7-----")) @@ -1295,7 +1313,7 @@ CertifierError security_load_certs_from_pem(const char * pem, X509_LIST ** out) } else if (XSTRSTR(pem, "-----BEGIN CERTIFICATE-----")) { - result = load_certs_from_certificate(pem, out); + result = load_certs_from_certificate(pem, out, load_all_certs); } else { diff --git a/src/property.c b/src/property.c index 8413ed6..8ce7b1d 100644 --- a/src/property.c +++ b/src/property.c @@ -36,6 +36,27 @@ #define DEFAULT_OUTPUT_P12_PATH "output.p12" #define DEFAULT_CFG_FILENAME "libcertifier.cfg" #define DEFAULT_USER_CFG_FILENAME "/usr/local/etc/certifier/libcertifier.cfg" + +// Helper function to validate Sectigo key type +int is_valid_sectigo_key_type(const char *key_type) +{ + if (!key_type) { + return 0; + } + + const char *valid_key_types[] = { + "RSA-2048", "RSA-3072", "RSA-4096", "RSA-8192", + "ECC-PRIME256V1", "ECC-SECP384R1" + }; + + for (int i = 0; i < sizeof(valid_key_types) / sizeof(valid_key_types[0]); i++) { + if (strcmp(key_type, valid_key_types[i]) == 0) { + return 1; + } + } + + return 0; +} #define DEFAULT_GLOBAL_CFG_FILENAME "/etc/certifier/libcertifier.cfg" #define DEFAULT_AUTH_TYPE "X509" #define DEFAULT_CA_INFO "libcertifier-cert.crt" @@ -53,6 +74,32 @@ #define DEFAULT_AUTORENEW_INTERVAL 86400 #define DEFAULT_AUTORENEW_CERTS_PATH "~/.libcertifier" +static char * simple_json_array_to_csv(const char *json_array_str) +{ + // Assumes input like ["a","b","c"] + if (!json_array_str || json_array_str[0] != '[') return XSTRDUP(""); + size_t len = strlen(json_array_str); + char *csv = XCALLOC(len + 1, sizeof(char)); + if (!csv) return NULL; + + size_t j = 0; + bool in_string = false; + for (size_t i = 0; i < len; ++i) { + char c = json_array_str[i]; + if (c == '"') { + in_string = !in_string; + continue; + } + if (in_string) { + csv[j++] = c; + } else if (c == ',' && j > 0) { + csv[j++] = ','; + } + } + csv[j] = '\0'; + return csv; +} + const char * get_default_cfg_filename() { static char cfg[] = DEFAULT_CFG_FILENAME; @@ -175,6 +222,37 @@ struct _PropMap X509_CERT * cert_x509_out; char * mtls_filename; char * mtls_p12_filename; + + //Sectigo properties (common properties like auth_token, validity days, source, etc. are above) + char * common_name; + char * group_name; + char * group_email; + char * id; + char * owner_first_name; + char * owner_last_name; + char * project_name; + char * business_justification; + char * subject_alt_names; + char * owner_email; + char * sectigo_url; + char * devhub_id; + char * key_type; + char * serial_number; + char * certificate_id; + char * revocation_request_reason; + char * requestor_email; + char * status; + char * offset; + int limit; + char * start_date; + char * end_date; + char * validity_start_date; + char * validity_end_date; + char * certificate_order; + char * is_cn_in_san; + char * request_type; + char * timestamp; + char * cert_path; }; static void free_prop_map_values(CertifierPropMap * prop_map); @@ -311,6 +389,118 @@ int property_set_int(CertifierPropMap * prop_map, CERTIFIER_OPT name, int value) return retval; } +int sectigo_property_set(CertifierPropMap * prop_map, int name, const void * value) +{ + int retval = 0; + switch (name) + { + case CERTIFIER_OPT_CFG_FILENAME: + prop_map->cfg_filename = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_LOG_LEVEL: + prop_map->log_level = (int)(size_t)value; + log_set_level(prop_map->log_level); + break; + case CERTIFIER_OPT_SECTIGO_AUTH_TOKEN: + prop_map->auth_token = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_COMMON_NAME: + prop_map->common_name = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_GROUP_NAME: + prop_map->group_name = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_GROUP_EMAIL: + prop_map->group_email = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_ID: + prop_map->id = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_OWNER_FIRST_NAME: + prop_map->owner_first_name = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_OWNER_LAST_NAME: + prop_map->owner_last_name = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_PROJECT_NAME: + prop_map->project_name = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_BUSINESS_JUSTIFICATION: + prop_map->business_justification = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_SUBJECT_ALT_NAMES: + prop_map->subject_alt_names = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_OWNER_EMAIL: + prop_map->owner_email = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_URL: + prop_map->sectigo_url = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_DEVHUB_ID: + prop_map->devhub_id = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_VALIDITY_DAYS: + prop_map->validity_days = (int)(size_t)value; + break; + case CERTIFIER_OPT_SECTIGO_KEY_TYPE: + prop_map->key_type = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_SERIAL_NUMBER: + prop_map->serial_number = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID: + prop_map->certificate_id = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_REQUESTOR_EMAIL: + prop_map->requestor_email = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_REVOCATION_REQUEST_REASON: + prop_map->revocation_request_reason = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_STATUS: + prop_map->status = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_OFFSET: + prop_map->offset = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_LIMIT: + prop_map->limit = (int)(size_t)value; + break; + case CERTIFIER_OPT_SECTIGO_START_DATE: + prop_map->start_date = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_END_DATE: + prop_map->end_date = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_VALIDITY_START_DATE: + prop_map->validity_start_date = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_VALIDITY_END_DATE: + prop_map->validity_end_date = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_CERTIFICATE_ORDER: + prop_map->certificate_order = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_IS_CN_IN_SAN: + prop_map->is_cn_in_san = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_REQUEST_TYPE: + prop_map->request_type = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_TIMESTAMP: + prop_map->timestamp = XSTRDUP((const char *)value); + break; + case CERTIFIER_OPT_SECTIGO_CERT_PATH: + prop_map->cert_path = XSTRDUP((const char *)value); + break; + default: + log_warn("sectigo_property_set: unrecognized property [%d]", name); + retval = CERTIFIER_ERR_PROPERTY_SET_10; + break; + } + return retval; +} int property_set(CertifierPropMap * prop_map, CERTIFIER_OPT name, const void * value) { int retval = 0; @@ -771,6 +961,99 @@ void * property_get(CertifierPropMap * prop_map, CERTIFIER_OPT name) break; } + case CERTIFIER_OPT_SECTIGO_AUTH_TOKEN: + retval = (void *) prop_map->auth_token; + break; + case CERTIFIER_OPT_SECTIGO_COMMON_NAME: + retval = (void *) prop_map->common_name; + break; + case CERTIFIER_OPT_SECTIGO_GROUP_NAME: + retval = (void *) prop_map->group_name; + break; + case CERTIFIER_OPT_SECTIGO_GROUP_EMAIL: + retval = (void *) prop_map->group_email; + break; + case CERTIFIER_OPT_SECTIGO_ID: + retval = (void *) prop_map->id; + break; + case CERTIFIER_OPT_SECTIGO_OWNER_FIRST_NAME: + retval = (void *) prop_map->owner_first_name; + break; + case CERTIFIER_OPT_SECTIGO_OWNER_LAST_NAME: + retval = (void *) prop_map->owner_last_name; + break; + case CERTIFIER_OPT_SECTIGO_PROJECT_NAME: + retval = (void *) prop_map->project_name; + break; + case CERTIFIER_OPT_SECTIGO_BUSINESS_JUSTIFICATION: + retval = (void *) prop_map->business_justification; + break; + case CERTIFIER_OPT_SECTIGO_SUBJECT_ALT_NAMES: + retval = (void *) prop_map->subject_alt_names; + break; + case CERTIFIER_OPT_SECTIGO_OWNER_EMAIL: + retval = (void *) prop_map->owner_email; + break; + case CERTIFIER_OPT_SECTIGO_URL: + retval = (void *) prop_map->sectigo_url; + break; + case CERTIFIER_OPT_SECTIGO_DEVHUB_ID: + retval = (void *) prop_map->devhub_id; + break; + case CERTIFIER_OPT_SECTIGO_VALIDITY_DAYS: + retval = (void *) (size_t) prop_map->validity_days; + break; + case CERTIFIER_OPT_SECTIGO_KEY_TYPE: + retval = (void *) prop_map->key_type; + break; + case CERTIFIER_OPT_SECTIGO_SERIAL_NUMBER: + retval = (void *) prop_map->serial_number; + break; + case CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID: + retval = (void *) prop_map->certificate_id; + break; + case CERTIFIER_OPT_SECTIGO_REQUESTOR_EMAIL: + retval = (void *) prop_map->requestor_email; + break; + case CERTIFIER_OPT_SECTIGO_REVOCATION_REQUEST_REASON: + retval = (void *) prop_map->revocation_request_reason; + break; + case CERTIFIER_OPT_SECTIGO_STATUS: + retval = (void *) prop_map->status; + break; + case CERTIFIER_OPT_SECTIGO_OFFSET: + retval = (void *) prop_map->offset; + break; + case CERTIFIER_OPT_SECTIGO_LIMIT: + retval = (void *) (size_t) prop_map->limit; + break; + case CERTIFIER_OPT_SECTIGO_START_DATE: + retval = (void *) prop_map->start_date; + break; + case CERTIFIER_OPT_SECTIGO_END_DATE: + retval = (void *) prop_map->end_date; + break; + case CERTIFIER_OPT_SECTIGO_VALIDITY_START_DATE: + retval = (void *) prop_map->validity_start_date; + break; + case CERTIFIER_OPT_SECTIGO_VALIDITY_END_DATE: + retval = (void *) prop_map->validity_end_date; + break; + case CERTIFIER_OPT_SECTIGO_CERTIFICATE_ORDER: + retval = (void *) prop_map->certificate_order; + break; + case CERTIFIER_OPT_SECTIGO_IS_CN_IN_SAN: + retval = (void *) prop_map->is_cn_in_san; + break; + case CERTIFIER_OPT_SECTIGO_REQUEST_TYPE: + retval = (void *) prop_map->request_type; + break; + case CERTIFIER_OPT_SECTIGO_TIMESTAMP: + retval = (void *) prop_map->timestamp; + break; + case CERTIFIER_OPT_SECTIGO_CERT_PATH: + retval = (void *) prop_map->cert_path; + break; default: log_warn("property_get: unrecognized property [%d]", name); retval = NULL; @@ -947,144 +1230,63 @@ int property_set_defaults(CertifierPropMap * prop_map) return return_code; } -int property_set_ext(CertifierPropMap * prop_map) +// Helper function to load XPKI-specific fields from JSON object +static int load_xpki_fields_from_json(CertifierPropMap *propMap, JSON_Object *root) { - JSON_Value * json; - const char * ext_key_usage_value = NULL; - int ret = 0; - - char * file_contents = NULL; - size_t file_contents_len = 0; - - const char * default_cfg_filename = get_default_cfg_filename(); - ret = util_slurp(default_cfg_filename, &file_contents, &file_contents_len); - if (ret != 0) - { - log_error("Received code: %i from util_slurp", ret); - if (file_contents != NULL) - { - XFREE(file_contents); - } - return 1; - } - else - { - file_contents[file_contents_len] = '\0'; - json = json_parse_string_with_comments(file_contents); - XFREE(file_contents); - if (json == NULL) - { - log_error("json_parse_string_with_comments returned a NULL value. Perhaps JSON malformed? Received error code: <%i>", - ret); - return 1; - } - } - - ext_key_usage_value = json_object_get_string(json_object(json), "libcertifier.ext.key.usage"); - if (ext_key_usage_value) - { - // log_info("Loaded Extended Key Usage Values: %s", ext_key_usage_value); - property_set(prop_map, CERTIFIER_OPT_EXT_KEY_USAGE, ext_key_usage_value); - } - - if (json) - { - json_value_free(json); - } - - return 0; -} - -int property_set_defaults_from_cfg_file(CertifierPropMap * propMap) -{ - - JSON_Value * json; - - const char * certifier_url_value = NULL; - const char * profile_name_value = NULL; - const char * auth_type_value = NULL; - const char * password_value = NULL; - const char * system_id_value = NULL; - const char * fabric_id_value = NULL; - const char * node_id_value = NULL; - const char * product_id_value = NULL; - const char * auth_tag_1_value = NULL; + const char *certifier_url_value = NULL; + const char *profile_name_value = NULL; + const char *auth_type_value = NULL; + const char *password_value = NULL; + const char *system_id_value = NULL; + const char *fabric_id_value = NULL; + const char *node_id_value = NULL; + const char *product_id_value = NULL; + const char *auth_tag_1_value = NULL; int http_timeout_value; int http_connect_timeout_value; int http_trace_value; - const char * input_p12_path_value = NULL; - const char * sat_token_value = NULL; - const char * ca_info_value = NULL; - const char * ca_path_value = NULL; - const char * ecc_curve_id_value = NULL; - const char * log_file_value = NULL; + const char *input_p12_path_value = NULL; + const char *sat_token_value = NULL; + const char *ca_info_value = NULL; + const char *ca_path_value = NULL; + const char *ecc_curve_id_value = NULL; + const char *log_file_value = NULL; int log_level_value; int log_max_size_value; int measure_performance_value; int autorenew_interval_value; int validity_days; - const char * source = NULL; + const char *source = NULL; int certificate_lite_value; int certificate_scopes_value; - const char * cn_prefix = NULL; - const char * ext_key_usage_value = NULL; - const char * autorenew_certs_path_list_value = NULL; - const char * mtls_p12_path_value = NULL; - const char * mtls_password_value = NULL; - - int ret = 0; + const char *cn_prefix = NULL; + const char *ext_key_usage_value = NULL; + const char *autorenew_certs_path_list_value = NULL; + const char *mtls_p12_path_value = NULL; + const char *mtls_password_value = NULL; - char * file_contents = NULL; - size_t file_contents_len = 0; - - log_info("Loading cfg file: %s", propMap->cfg_filename); - - log_debug("About to call: util_slurp with path: %s", propMap->cfg_filename); - ret = util_slurp(propMap->cfg_filename, &file_contents, &file_contents_len); - if (ret != 0) - { - log_error("Received code: %i from util_slurp", ret); - if (file_contents != NULL) - { - XFREE(file_contents); - } - return 1; - } - else - { - file_contents[file_contents_len] = '\0'; - json = json_parse_string_with_comments(file_contents); - XFREE(file_contents); - if (json == NULL) - { - log_error("json_parse_string_with_comments returned a NULL value. Perhaps JSON malformed? Received error code: <%i>", - ret); - return 1; - } - } - - certifier_url_value = json_object_get_string(json_object(json), "libcertifier.certifier.url"); + certifier_url_value = json_object_get_string(root, "libcertifier.certifier.url"); if (certifier_url_value) { log_info("Loaded certifier url: %s from config file.", certifier_url_value); property_set(propMap, CERTIFIER_OPT_CERTIFIER_URL, certifier_url_value); } - profile_name_value = json_object_get_string(json_object(json), "libcertifier.profile.name"); + profile_name_value = json_object_get_string(root, "libcertifier.profile.name"); if (profile_name_value) { log_info("Loaded profile name: %s from config file.", profile_name_value); property_set(propMap, CERTIFIER_OPT_PROFILE_NAME, profile_name_value); } - auth_type_value = json_object_get_string(json_object(json), "libcertifier.auth.type"); + auth_type_value = json_object_get_string(root, "libcertifier.auth.type"); if (auth_type_value) { log_info("Loaded crt.type: %s from config file.", auth_type_value); property_set(propMap, CERTIFIER_OPT_AUTH_TYPE, auth_type_value); } - password_value = json_object_get_string(json_object(json), "libcertifier.input.p12.password"); + password_value = json_object_get_string(root, "libcertifier.input.p12.password"); if (password_value) { print_warning("password"); @@ -1092,56 +1294,56 @@ int property_set_defaults_from_cfg_file(CertifierPropMap * propMap) property_set(propMap, CERTIFIER_OPT_INPUT_P12_PASSWORD, password_value); } - system_id_value = json_object_get_string(json_object(json), "libcertifier.system.id"); + system_id_value = json_object_get_string(root, "libcertifier.system.id"); if (system_id_value) { log_info("Loaded system_id_value: %s from config file.", system_id_value); property_set(propMap, CERTIFIER_OPT_SYSTEM_ID, system_id_value); } - fabric_id_value = json_object_get_string(json_object(json), "libcertifier.fabric.id"); + fabric_id_value = json_object_get_string(root, "libcertifier.fabric.id"); if (fabric_id_value) { log_info("Loaded fabric_id_value: %s from config file.", fabric_id_value); property_set(propMap, CERTIFIER_OPT_FABRIC_ID, fabric_id_value); } - node_id_value = json_object_get_string(json_object(json), "libcertifier.node.id"); + node_id_value = json_object_get_string(root, "libcertifier.node.id"); if (node_id_value) { log_info("Loaded node_id_value: %s from config file.", node_id_value); property_set(propMap, CERTIFIER_OPT_NODE_ID, node_id_value); } - product_id_value = json_object_get_string(json_object(json), "libcertifier.product.id"); + product_id_value = json_object_get_string(root, "libcertifier.product.id"); if (product_id_value) { log_info("Loaded product_id_value: %s from config file.", product_id_value); property_set(propMap, CERTIFIER_OPT_PRODUCT_ID, product_id_value); } - auth_tag_1_value = json_object_get_string(json_object(json), "libcertifier.authentication.tag.1"); + auth_tag_1_value = json_object_get_string(root, "libcertifier.authentication.tag.1"); if (auth_tag_1_value) { log_info("Loaded auth_tag_1_value: %s from config file.", auth_tag_1_value); property_set(propMap, CERTIFIER_OPT_AUTH_TAG_1, auth_tag_1_value); } - http_timeout_value = json_object_get_number(json_object(json), "libcertifier.http.timeout"); + http_timeout_value = json_object_get_number(root, "libcertifier.http.timeout"); if (http_timeout_value >= 0) { log_info("Loaded http_timeout_value: %i from cfg file.", http_timeout_value); property_set(propMap, CERTIFIER_OPT_HTTP_TIMEOUT, (void *) (size_t) http_timeout_value); } - http_connect_timeout_value = json_object_get_number(json_object(json), "libcertifier.http.connect.timeout"); + http_connect_timeout_value = json_object_get_number(root, "libcertifier.http.connect.timeout"); if (http_connect_timeout_value >= 0) { log_info("Loaded http_connect_timeout_value: %i from cfg file.", http_connect_timeout_value); property_set(propMap, CERTIFIER_OPT_HTTP_CONNECT_TIMEOUT, (void *) (size_t) http_connect_timeout_value); } - http_trace_value = json_object_get_number(json_object(json), "libcertifier.http.trace"); + http_trace_value = json_object_get_number(root, "libcertifier.http.trace"); if (http_trace_value == 1) { log_info("Loaded http_trace_value: %i from cfg file.", http_trace_value); @@ -1149,77 +1351,77 @@ int property_set_defaults_from_cfg_file(CertifierPropMap * propMap) propMap->options |= (CERTIFIER_OPTION_TRACE_HTTP | CERTIFIER_OPTION_DEBUG_HTTP); } - measure_performance_value = json_object_get_number(json_object(json), "libcertifier.measure.performance"); + measure_performance_value = json_object_get_number(root, "libcertifier.measure.performance"); if (measure_performance_value == 1) { log_info("Loaded measure.performance: %i from cfg file.", measure_performance_value); propMap->options |= CERTIFIER_OPTION_MEASURE_PERFORMANCE; } - autorenew_interval_value = json_object_get_number(json_object(json), "libcertifier.autorenew.interval"); + autorenew_interval_value = json_object_get_number(root, "libcertifier.autorenew.interval"); if (autorenew_interval_value == 1) { log_info("Loaded autorenew.interval: %i from cfg file.", autorenew_interval_value); property_set(propMap, CERTIFIER_OPT_AUTORENEW_INTERVAL, (void *) (size_t) autorenew_interval_value); } - input_p12_path_value = json_object_get_string(json_object(json), "libcertifier.input.p12.path"); + input_p12_path_value = json_object_get_string(root, "libcertifier.input.p12.path"); if (input_p12_path_value) { log_info("Loaded input_p12_path_value: %s from cfg file.", input_p12_path_value); property_set(propMap, CERTIFIER_OPT_INPUT_P12_PATH, input_p12_path_value); } - sat_token_value = json_object_get_string(json_object(json), "libcertifier.sat.token"); + sat_token_value = json_object_get_string(root, "libcertifier.sat.token"); if (sat_token_value) { log_info("Loaded sat_token_value: %s from cfg file.", sat_token_value); property_set(propMap, CERTIFIER_OPT_AUTH_TOKEN, sat_token_value); } - ca_info_value = json_object_get_string(json_object(json), "libcertifier.ca.info"); + ca_info_value = json_object_get_string(root, "libcertifier.ca.info"); if (ca_info_value) { log_info("Loaded ca_info_value: %s from cfg file.", ca_info_value); property_set(propMap, CERTIFIER_OPT_CA_INFO, ca_info_value); } - ca_path_value = json_object_get_string(json_object(json), "libcertifier.ca.path"); + ca_path_value = json_object_get_string(root, "libcertifier.ca.path"); if (ca_path_value) { log_info("Loaded ca_path_value: %s from cfg file.", ca_path_value); property_set(propMap, CERTIFIER_OPT_CA_PATH, ca_path_value); } - validity_days = json_object_get_number(json_object(json), "libcertifier.validity.days"); + validity_days = json_object_get_number(root, "libcertifier.validity.days"); if (validity_days) { log_info("Loaded validity_days: %d", validity_days); property_set(propMap, CERTIFIER_OPT_VALIDITY_DAYS, (void *) (size_t) validity_days); } - ecc_curve_id_value = json_object_get_string(json_object(json), "libcertifier.ecc.curve.id"); + ecc_curve_id_value = json_object_get_string(root, "libcertifier.ecc.curve.id"); if (ecc_curve_id_value) { log_info("Loaded ecc_curve_id_value: %s from cfg file.", ecc_curve_id_value); property_set(propMap, CERTIFIER_OPT_ECC_CURVE_ID, ecc_curve_id_value); } - log_file_value = json_object_get_string(json_object(json), "libcertifier.log.file"); + log_file_value = json_object_get_string(root, "libcertifier.log.file"); if (log_file_value) { log_info("Loaded Log File Value: %s from cfg file.", log_file_value); property_set(propMap, CERTIFIER_OPT_LOG_FILENAME, log_file_value); } - log_level_value = json_object_get_number(json_object(json), "libcertifier.log.level"); + log_level_value = json_object_get_number(root, "libcertifier.log.level"); if (log_level_value >= 0) { log_info("Loaded Log Level value: %i from cfg file.", log_level_value); property_set(propMap, CERTIFIER_OPT_LOG_LEVEL, (void *) (size_t) (log_level_value)); } - log_max_size_value = json_object_get_number(json_object(json), "libcertifier.log.max.size"); + log_max_size_value = json_object_get_number(root, "libcertifier.log.max.size"); if (log_max_size_value >= 0) { log_info("Loaded Log Max Size value: %i from cfg file.", log_max_size_value); @@ -1227,55 +1429,58 @@ int property_set_defaults_from_cfg_file(CertifierPropMap * propMap) } log_set_max_size(propMap->log_max_size); - source = json_object_get_string(json_object(json), "libcertifier.source.id"); + source = json_object_get_string(root, "libcertifier.source.id"); if (source) { log_info("Loaded source.id %s from cfg file.", source); property_set(propMap, CERTIFIER_OPT_SOURCE, source); } - certificate_lite_value = json_object_get_number(json_object(json), "libcertifier.certificate.lite"); + certificate_lite_value = json_object_get_number(root, "libcertifier.certificate.lite"); if (certificate_lite_value == 1) { log_info("Loaded certificate.lite: %i from cfg file.", certificate_lite_value); print_warning("certificate.lite"); propMap->options |= (CERTIFIER_OPTION_CERTIFICATE_LITE); } - certificate_scopes_value = json_object_get_number(json_object(json), "libcertifier.certificate.scopes"); + + certificate_scopes_value = json_object_get_number(root, "libcertifier.certificate.scopes"); if (certificate_scopes_value == 1) { log_info("Loaded certificate.scopes: %i from cfg file.", certificate_scopes_value); print_warning("certificate.scopes"); propMap->options |= (CERTIFIER_OPTION_USE_SCOPES); } - cn_prefix = json_object_get_string(json_object(json), "libcertifier.cn.name"); + + cn_prefix = json_object_get_string(root, "libcertifier.cn.name"); if (cn_prefix != NULL) { log_info("Loaded Common Name value: %s from cfg file.", cn_prefix); property_set(propMap, CERTIFIER_OPT_CN_PREFIX, cn_prefix); } - ext_key_usage_value = json_object_get_string(json_object(json), "libcertifier.ext.key.usage"); + + ext_key_usage_value = json_object_get_string(root, "libcertifier.ext.key.usage"); if (ext_key_usage_value) { log_info("Loaded Extended Key Usage Values: %s from cfg file.", ext_key_usage_value); property_set(propMap, CERTIFIER_OPT_EXT_KEY_USAGE, ext_key_usage_value); } - autorenew_certs_path_list_value = json_object_get_string(json_object(json), "libcertifier.autorenew.certs.path.list"); + autorenew_certs_path_list_value = json_object_get_string(root, "libcertifier.autorenew.certs.path.list"); if (autorenew_certs_path_list_value) { log_info("Loaded autorenew certs path: %s from config file.", autorenew_certs_path_list_value); property_set(propMap, CERTIFIER_OPT_AUTORENEW_CERTS_PATH_LIST, autorenew_certs_path_list_value); } - mtls_p12_path_value = json_object_get_string(json_object(json), "libcertifier.mtls.p12.path"); + mtls_p12_path_value = json_object_get_string(root, "libcertifier.mtls.p12.path"); if (mtls_p12_path_value) { log_info("Loaded mtls_p12_path_value: %s from cfg file.", mtls_p12_path_value); property_set(propMap, CERTIFIER_OPT_MTLS_P12_PATH, mtls_p12_path_value); } - mtls_password_value = json_object_get_string(json_object(json), "libcertifier.mtls.p12.password"); + mtls_password_value = json_object_get_string(root, "libcertifier.mtls.p12.password"); if (mtls_password_value) { print_warning("password"); @@ -1283,6 +1488,280 @@ int property_set_defaults_from_cfg_file(CertifierPropMap * propMap) property_set(propMap, CERTIFIER_OPT_MTLS_P12_PASSWORD, mtls_password_value); } + return 0; +} + +// Helper function to load Sectigo-specific fields from JSON object +static int load_sectigo_fields_from_json(CertifierPropMap *propMap, JSON_Object *root) +{ + // Iterate through all keys in the JSON object + size_t count = json_object_get_count(root); + for (size_t i = 0; i < count; i++) { + const char *key = json_object_get_name(root, i); + + // Only process keys that start with "libcertifier.sectigo." + if (strncmp(key, "libcertifier.sectigo.", 21) != 0) { + continue; + } + + // Special handling for array keys + if (strcmp(key, "libcertifier.sectigo.subject.alt.names") == 0) { + const char *array_str = json_object_get_string(root, key); + char *csv = NULL; + if (array_str) { + csv = simple_json_array_to_csv(array_str); + } + if (!csv) { + csv = XSTRDUP(""); // Always set to empty string if missing/empty + } + if (strlen(csv) > 0) { + log_info("Loaded sectigo subject alt names: %s from config file.", csv); + } + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_SUBJECT_ALT_NAMES, csv); + XFREE(csv); + continue; + } + + if (strcmp(key, "libcertifier.sectigo.validity.days") == 0) { + int validity_days = json_object_get_number(root, key); + log_info("Loaded sectigo validity days: %d from config file.", validity_days); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_VALIDITY_DAYS, (void *) (size_t) validity_days); + } + + if (strcmp(key, "libcertifier.sectigo.limit") == 0) { + int limit = json_object_get_number(root, key); + log_info("Loaded sectigo limit: %d from config file.", limit); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_LIMIT, (void *) (size_t) limit); + } + + const char *value_str = json_object_get_string(root, key); + if (value_str && strlen(value_str) > 0) { // Only process non-empty values + // Map config key to property enum + if (strcmp(key, "libcertifier.sectigo.auth.token") == 0) { + log_info("Loaded sectigo auth token from config file."); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_AUTH_TOKEN, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.common.name") == 0) { + log_info("Loaded sectigo common name: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_COMMON_NAME, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.group.name") == 0) { + log_info("Loaded sectigo group name: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_GROUP_NAME, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.group.email") == 0) { + log_info("Loaded sectigo group email: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_GROUP_EMAIL, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.id") == 0) { + log_info("Loaded sectigo id: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_ID, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.owner.first.name") == 0) { + log_info("Loaded sectigo owner first name: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_OWNER_FIRST_NAME, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.owner.last.name") == 0) { + log_info("Loaded sectigo owner last name: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_OWNER_LAST_NAME, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.project.name") == 0) { + log_info("Loaded sectigo project name: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_PROJECT_NAME, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.business.justification") == 0) { + log_info("Loaded sectigo business justification: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_BUSINESS_JUSTIFICATION, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.owner.email") == 0) { + log_info("Loaded sectigo owner email: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_OWNER_EMAIL, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.url") == 0) { + log_info("Loaded sectigo URL: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_URL, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.devhub.id") == 0) { + log_info("Loaded sectigo devhub id: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_DEVHUB_ID, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.key.type") == 0) { + if (!is_valid_sectigo_key_type(value_str)) { + log_error("Invalid key type '%s' in config file. Supported: [RSA-2048, RSA-3072, RSA-4096, RSA-8192, ECC-PRIME256V1, ECC-SECP384R1]", value_str); + exit(0); + } + log_info("Loaded sectigo key type: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_KEY_TYPE, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.serial.number") == 0) { + log_info("Loaded sectigo serial number: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_SERIAL_NUMBER, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.certificate.id") == 0) { + log_info("Loaded sectigo certificate id: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.requestor.email") == 0) { + log_info("Loaded sectigo requestor email: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_REQUESTOR_EMAIL, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.revocation.request.reason") == 0) { + log_info("Loaded sectigo revocation request reason: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_REVOCATION_REQUEST_REASON, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.status") == 0) { + log_info("Loaded sectigo status: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_STATUS, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.offset") == 0) { + int offset = json_object_get_number(root, key); + log_info("Loaded sectigo offset: %d from config file.", offset); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_OFFSET, (void *) (size_t) offset); + } + else if (strcmp(key, "libcertifier.sectigo.start.date") == 0) { + log_info("Loaded sectigo start date: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_START_DATE, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.end.date") == 0) { + log_info("Loaded sectigo end date: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_END_DATE, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.validity.start.date") == 0) { + log_info("Loaded sectigo validity start date: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_VALIDITY_START_DATE, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.validity.end.date") == 0) { + log_info("Loaded sectigo validity end date: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_VALIDITY_END_DATE, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.certificate.order") == 0) { + log_info("Loaded sectigo certificate order: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_CERTIFICATE_ORDER, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.is.cn.in.san") == 0) { + int is_cn_in_san = json_object_get_number(root, key); + log_info("Loaded sectigo is CN in SAN: %d from config file.", is_cn_in_san); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_IS_CN_IN_SAN, (void *) (size_t) is_cn_in_san); + } + else if (strcmp(key, "libcertifier.sectigo.request.type") == 0) { + log_info("Loaded sectigo request type: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_REQUEST_TYPE, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.timestamp") == 0) { + log_info("Loaded sectigo timestamp: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_TIMESTAMP, value_str); + } + else if (strcmp(key, "libcertifier.sectigo.cert.path") == 0) { + log_info("Loaded sectigo cert path: %s from config file.", value_str); + sectigo_property_set(propMap, CERTIFIER_OPT_SECTIGO_CERT_PATH, value_str); + } + } + } + + return 0; +} + +// Config file loader with mode-aware field loading +static int property_set_defaults_from_cfg_file_ex(CertifierPropMap *propMap, bool sectigo_mode) +{ + JSON_Value *json = NULL; + int ret = 0; + char *file_contents = NULL; + size_t file_contents_len = 0; + const char *cfg_filename = propMap->cfg_filename; + + if (!cfg_filename) { + log_warn("No config filename set, skipping defaults from cfg file"); + return 0; + } + + log_info("Loading %s cfg file: %s", sectigo_mode ? "Sectigo" : "XPKI", cfg_filename); + log_debug("property_set_defaults_from_cfg_file_ex called with sectigo_mode=%d", sectigo_mode); + + ret = util_slurp(cfg_filename, &file_contents, &file_contents_len); + if (ret != 0) { + log_error("Failed to read cfg file: %s (error code: %i)", cfg_filename, ret); + if (file_contents) { + XFREE(file_contents); + } + return 1; + } + + file_contents[file_contents_len] = '\0'; + json = json_parse_string_with_comments(file_contents); + XFREE(file_contents); + + if (!json) { + log_error("Failed to parse JSON from cfg file: %s", cfg_filename); + return 1; + } + + JSON_Object *root = json_object(json); + if (!root) { + log_error("JSON root object is NULL"); + json_value_free(json); + return 1; + } + + // Delegate to mode-specific field loader + if (sectigo_mode) { + log_debug("Delegating to load_sectigo_fields_from_json"); + ret = load_sectigo_fields_from_json(propMap, root); + } else { + log_debug("Delegating to load_xpki_fields_from_json"); + ret = load_xpki_fields_from_json(propMap, root); + } + + json_value_free(json); + return ret; +} + +int property_set_sectigo_defaults_from_cfg_file(CertifierPropMap * propMap) +{ + return property_set_defaults_from_cfg_file_ex(propMap, true); +} + + +int property_set_ext(CertifierPropMap * prop_map) +{ + JSON_Value * json; + const char * ext_key_usage_value = NULL; + int ret = 0; + + char * file_contents = NULL; + size_t file_contents_len = 0; + + const char * default_cfg_filename = get_default_cfg_filename(); + ret = util_slurp(default_cfg_filename, &file_contents, &file_contents_len); + if (ret != 0) + { + log_error("Received code: %i from util_slurp", ret); + if (file_contents != NULL) + { + XFREE(file_contents); + } + return 1; + } + else + { + file_contents[file_contents_len] = '\0'; + json = json_parse_string_with_comments(file_contents); + XFREE(file_contents); + if (json == NULL) + { + log_error("json_parse_string_with_comments returned a NULL value. Perhaps JSON malformed? Received error code: <%i>", + ret); + return 1; + } + } + + ext_key_usage_value = json_object_get_string(json_object(json), "libcertifier.ext.key.usage"); + if (ext_key_usage_value) + { + log_info("Loaded Extended Key Usage Values: %s", ext_key_usage_value); + property_set(prop_map, CERTIFIER_OPT_EXT_KEY_USAGE, ext_key_usage_value); + } + if (json) { json_value_free(json); @@ -1291,6 +1770,11 @@ int property_set_defaults_from_cfg_file(CertifierPropMap * propMap) return 0; } +int property_set_defaults_from_cfg_file(CertifierPropMap * propMap) +{ + return property_set_defaults_from_cfg_file_ex(propMap, false); +} + #define FV(field) \ if (field != NULL) \ { \ @@ -1339,4 +1823,40 @@ static void free_prop_map_values(CertifierPropMap * prop_map) security_free_cert(prop_map->cert_x509_out); FV(prop_map->mtls_filename); FV(prop_map->mtls_p12_filename); + FV(prop_map->auth_token); + FV(prop_map->common_name); + FV(prop_map->group_name); + FV(prop_map->group_email); + FV(prop_map->id); + FV(prop_map->owner_first_name); + FV(prop_map->owner_last_name); + FV(prop_map->project_name); + FV(prop_map->business_justification); + FV(prop_map->subject_alt_names); + FV(prop_map->owner_email); + FV(prop_map->sectigo_url); + FV(prop_map->devhub_id); + FV(prop_map->key_type); +} + +CertifierPropMap * property_new_sectigo(void) +{ + CertifierPropMap * prop_map = XCALLOC(1, sizeof(CertifierPropMap)); + if (prop_map == NULL) + { + log_error("Could not initialize CertifierPropMap."); + return NULL; + } + + char * trace_id = NULL; + + // generate tracking ID + trace_id = util_generate_random_value(16, ALLOWABLE_CHARACTERS); + if (trace_id) + { + property_set(prop_map, CERTIFIER_OPT_TRACKING_ID, trace_id); + XFREE(trace_id); + } + + return prop_map; } diff --git a/src/sectigo_client.c b/src/sectigo_client.c new file mode 100644 index 0000000..1da3eed --- /dev/null +++ b/src/sectigo_client.c @@ -0,0 +1,1163 @@ +/** + * Copyright 2022 Comcast Cable Communications Management, LLC + * + * 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. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include "certifier/sectigo_client.h" +#include +#include +#include "certifier/code_utils.h" +#include "certifier/types.h" +#include "certifier/certifierclient.h" +#include "certifier/certifier_internal.h" +#include "certifier/http.h" +#include "certifier/log.h" +#include "certifier/parson.h" +#include "certifier/util.h" +#include "certifier/error.h" +#include "certifier/property_internal.h" + + +#include +#include +#include +#include +#include + +#include + +#include +#include +#include +#include +#include +#include + +Certifier * get_sectigo_certifier_instance() +{ + static Certifier * certifier = NULL; + + if (certifier == NULL) + { + certifier = certifier_new(); + certifier->sectigo_mode = true; + certifier_set_property(certifier, CERTIFIER_OPT_LOG_LEVEL, (void *) (size_t) 0); + + // Load Sectigo config file if it exists + const char *cfg_filename = certifier_get_property(certifier, CERTIFIER_OPT_CFG_FILENAME); + if (cfg_filename && access(cfg_filename, F_OK) == 0) { + sectigo_load_cfg_file(certifier); + } + } + return certifier; +} + +static void append_query_param(char *url, size_t url_size, const char *key, const char *value, int *first_param) +{ + if (!value || strlen(value) == 0) { + return; + } + + size_t current_len = strlen(url); + snprintf(url + current_len, url_size - current_len, + "%s%s=%s", *first_param ? "?" : "&", key, value); + *first_param = 0; +} + +// For numeric parameters +static void append_query_param_num(char *url, size_t url_size, const char *key, size_t value, int *first_param) +{ + if (value == 0) { + return; + } + + size_t current_len = strlen(url); + snprintf(url + current_len, url_size - current_len, + "%s%s=%zu", *first_param ? "?" : "&", key, value); + *first_param = 0; +} + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_get_default_cert_param(sectigo_get_cert_param_t * params) +{ + Certifier * certifier = get_sectigo_certifier_instance(); + + memset(params, 0, sizeof(sectigo_get_cert_param_t)); + + void * param = NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_AUTH_TOKEN); + params->auth_token = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_COMMON_NAME); + params->common_name = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_GROUP_NAME); + params->group_name = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_GROUP_EMAIL); + params->group_email = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_ID); + params->id = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_OWNER_FIRST_NAME); + params->owner_first_name = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_OWNER_LAST_NAME); + params->owner_last_name = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_PROJECT_NAME); + params->project_name = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_BUSINESS_JUSTIFICATION); + params->business_justification = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_SUBJECT_ALT_NAMES); + params->subject_alt_names = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_OWNER_EMAIL); + params->owner_email = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_URL); + params->sectigo_url = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_DEVHUB_ID); + params->devhub_id = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_VALIDITY_DAYS); + params->validity_days = param ? (size_t) param : 365; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_KEY_TYPE); + params->key_type = param ? XSTRDUP((const char *)param) : NULL; + + return SECTIGO_CLIENT_SUCCESS; +} + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_get_default_renew_cert_param(sectigo_renew_cert_param_t * params) +{ + Certifier * certifier = get_sectigo_certifier_instance(); + + memset(params, 0, sizeof(sectigo_renew_cert_param_t)); + + void * param = NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_AUTH_TOKEN); + params->auth_token = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_COMMON_NAME); + params->common_name = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_SERIAL_NUMBER); + params->serial_number = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID); + params->certificate_id = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_REQUESTOR_EMAIL); + params->requestor_email = param ? XSTRDUP((const char *)param) : NULL; + + return SECTIGO_CLIENT_SUCCESS; +} + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_get_default_revoke_cert_param(sectigo_revoke_cert_param_t * params) +{ + Certifier * certifier = get_sectigo_certifier_instance(); + + memset(params, 0, sizeof(sectigo_revoke_cert_param_t)); + + void * param = NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_AUTH_TOKEN); + params->auth_token = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_COMMON_NAME); + params->common_name = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_SERIAL_NUMBER); + params->serial_number = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID); + params->certificate_id = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_REQUESTOR_EMAIL); + params->requestor_email = param ? XSTRDUP((const char *)param) : NULL; + + param = certifier_get_property(certifier, CERTIFIER_OPT_SECTIGO_REVOCATION_REQUEST_REASON); + params->revocation_request_reason = param ? XSTRDUP((const char *)param) : NULL; + + return SECTIGO_CLIENT_SUCCESS; +} + +CertifierError sectigo_client_request_certificate(CertifierPropMap * props, const unsigned char * csr, +const char * node_address, const char * certifier_id, char ** out_cert) +{ + Certifier *certifier = get_sectigo_certifier_instance(); + CertifierError rc = CERTIFIER_ERROR_INITIALIZER; + JSON_Value *root_value = NULL; + JSON_Object *root_obj = NULL; + char *json_body = NULL; + + if (out_cert == NULL) + { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("out cert cannot be null"); + return rc; + } + + char auth_header[VERY_LARGE_STRING_SIZE * 4] = ""; + char tracking_header[LARGE_STRING_SIZE] = ""; + char source_header[SMALL_STRING_SIZE] = ""; + JSON_Object * parsed_json_object_value = NULL; + JSON_Value * parsed_json_root_value = NULL; + char * serialized_string = NULL; + http_response * resp = NULL; + const char * tracking_id = property_get(props, CERTIFIER_OPT_TRACKING_ID); + const char * bearer_token = property_get(props, CERTIFIER_OPT_SECTIGO_AUTH_TOKEN); + const char * source = "libcertifier"; + const char * sectigo_base_url = property_get(props, CERTIFIER_OPT_SECTIGO_URL); + + if (!bearer_token) { + log_error("Missing CERTIFIER_OPT_SECTIGO_AUTH_TOKEN"); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Bearer token is missing"); + goto cleanup; + } + if (!sectigo_base_url) { + log_error("Missing CERTIFIER_OPT_SECTIGO_URL"); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Sectigo base URL is missing"); + goto cleanup; + } + + // Build full URL: base + endpoint + char sectigo_create_cert_url[256]; + char create_cert_endpoint[] = "/api/createCertificate"; + strncpy(sectigo_create_cert_url, sectigo_base_url, sizeof(sectigo_create_cert_url) - 1); + strncpy(sectigo_create_cert_url + strlen(sectigo_base_url), create_cert_endpoint, + sizeof(sectigo_create_cert_url) - 1 - strlen(sectigo_base_url)); + + log_debug("Tracking ID is: %s\n", tracking_id); + log_debug("Sectigo URL: %s\n", sectigo_create_cert_url); + + if (bearer_token != NULL) { + snprintf(auth_header, sizeof(auth_header), "Authorization: %s", bearer_token); + } + snprintf(tracking_header, sizeof(tracking_header), "x-xpki-request-id: %s", tracking_id); + snprintf(source_header, sizeof(source_header), "x-xpki-source: %s", source); + + const char *headers[] = { + "Accept: */*", + "Connection: keep-alive", + "cache-control: no-cache", + "Content-Type: application/json", + source_header, + tracking_header, + "x-xpki-partner-id: comcast", + auth_header, + NULL + }; + + CertifierError csr_rc = sectigo_generate_certificate_signing_request(certifier, &serialized_string); + + if (csr_rc.application_error_code != 0 || serialized_string == NULL) { + rc.application_error_code = csr_rc.application_error_code; + rc.application_error_msg = csr_rc.application_error_msg; + goto cleanup; + } + + // Take Mutex + if (pthread_mutex_lock(&lock) != 0) + { + rc.application_error_code = 17; + rc.application_error_msg = "sectigo_client_request_certificate: pthread_mutex_lock failed"; + goto cleanup; + } + // Give Mutex + if (pthread_mutex_unlock(&lock) != 0) + { + rc.application_error_code = 18; + rc.application_error_msg = "sectigo_client_request_certificate: pthread_mutex_unlock failed"; + goto cleanup; + } + + sectigo_get_cert_param_t params; + xc_sectigo_get_default_cert_param(¶ms); + + // Build JSON body + root_value = json_value_init_object(); + root_obj = json_value_get_object(root_value); + + json_object_set_string(root_obj, "certificateSigningRequest", serialized_string); + + json_object_set_string(root_obj, "commonName", params.common_name ? params.common_name : ""); + json_object_set_string(root_obj, "groupName", params.group_name ? params.group_name : ""); + json_object_set_string(root_obj, "groupEmailAddress", params.group_email ? params.group_email : ""); + json_object_set_string(root_obj, "id", params.id ? params.id : ""); + json_object_set_string(root_obj, "ownerFirstName", params.owner_first_name ? params.owner_first_name : ""); + json_object_set_string(root_obj, "ownerLastName", params.owner_last_name ? params.owner_last_name : ""); + json_object_set_string(root_obj, "projectName", params.project_name ? params.project_name : ""); + json_object_set_string(root_obj, "businessJustification", params.business_justification ? params.business_justification : ""); + json_object_set_string(root_obj, "certificateType", "comodo"); // Always "comodo" + json_object_set_string(root_obj, "ownerEmailAddress", params.owner_email ? params.owner_email : ""); + + // The following parameters are optional. Only include if set + if (params.devhub_id) { + json_object_set_string(root_obj, "devhubId", params.devhub_id); + } + + if (params.validity_days > 0) { + json_object_set_number(root_obj, "validityDays", (double)params.validity_days); + } + + if (params.key_type) { + json_object_set_string(root_obj, "keyType", params.key_type); + } + + // subjectAltNames as array + JSON_Value *san_array = json_value_init_array(); + JSON_Array *san_json_array = json_value_get_array(san_array); + if (params.subject_alt_names && strlen(params.subject_alt_names) > 0) { + char *san_copy = XSTRDUP(params.subject_alt_names); + char *token = strtok(san_copy, ","); + while (token) { + json_array_append_value(san_json_array, json_value_init_string(token)); + token = strtok(NULL, ","); + } + XFREE(san_copy); + } + json_object_set_value(root_obj, "subjectAltNames", san_array); + + json_body = json_serialize_to_string(root_value); + + resp = http_post(props, sectigo_create_cert_url, headers, json_body); + if (resp == NULL) + { + goto cleanup; + } + + rc.application_error_code = resp->error; + + // Check for errors + if (resp->error != 0) + { + rc.application_error_msg = util_format_curl_error("sectigo_client_request_certificate", resp->http_code, resp->error, + resp->error_msg, resp->payload, __FILE__, __LINE__); + goto cleanup; + } + + if (resp->payload == NULL) + { + log_error("ERROR: Failed to populate payload"); + goto cleanup; + } + + parsed_json_root_value = json_parse_string_with_comments(resp->payload); + if (json_value_get_type(parsed_json_root_value) != JSONObject) + { + rc.application_error_msg = + util_format_curl_error("sectigo_client_request_certificate", resp->http_code, resp->error, + "Could not parse JSON. Expected it to be an array.", resp->payload, __FILE__, __LINE__); + goto cleanup; + } + + parsed_json_object_value = json_value_get_object(parsed_json_root_value); + + if (parsed_json_object_value == NULL) + { + rc.application_error_msg = + util_format_curl_error("sectigo_client_request_certificate", resp->http_code, resp->error, + "Could not parse JSON. parsed_json_object_value is NULL!.", resp->payload, __FILE__, __LINE__); + goto cleanup; + } + +cleanup: + http_free_response(resp); + + if (parsed_json_root_value) json_value_free(parsed_json_root_value); + + XFREE(serialized_string); + + if (json_body) json_free_serialized_string(json_body); + if (root_value) json_value_free(root_value); + + return rc; +} + +CertifierError sectigo_client_search_certificates(CertifierPropMap * props) +{ + CertifierError rc = CERTIFIER_ERROR_INITIALIZER; + + char auth_header[VERY_LARGE_STRING_SIZE * 4] = ""; + char tracking_header[LARGE_STRING_SIZE] = ""; + char source_header[SMALL_STRING_SIZE] = ""; + http_response * resp = NULL; + const char * tracking_id = property_get(props, CERTIFIER_OPT_TRACKING_ID); + const char * bearer_token = property_get(props, CERTIFIER_OPT_SECTIGO_AUTH_TOKEN); + const char * source = "libcertifier"; + const char * sectigo_base_url = property_get(props, CERTIFIER_OPT_SECTIGO_URL); + + if (!bearer_token) { + log_error("Missing CERTIFIER_OPT_SECTIGO_AUTH_TOKEN"); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Bearer token is missing"); + goto cleanup; + } + if (!sectigo_base_url) { + log_error("Missing CERTIFIER_OPT_SECTIGO_URL"); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Sectigo base URL is missing"); + goto cleanup; + } + + // Build full URL: base + endpoint + char sectigo_search_cert_url[256]; + char search_cert_endpoint[] = "/api/getCertificates"; + strncpy(sectigo_search_cert_url, sectigo_base_url, sizeof(sectigo_search_cert_url) - 1); + strncpy(sectigo_search_cert_url + strlen(sectigo_base_url), search_cert_endpoint, + sizeof(sectigo_search_cert_url) - 1 - strlen(sectigo_base_url)); + log_debug("Tracking ID is: %s\n", tracking_id); + + if (bearer_token != NULL) { + snprintf(auth_header, sizeof(auth_header), "Authorization: %s", bearer_token); + } + snprintf(tracking_header, sizeof(tracking_header), "x-xpki-request-id: %s", tracking_id); + snprintf(source_header, sizeof(source_header), "x-xpki-source: %s", source); + + const char *headers[] = { + "Accept: */*", + "Connection: keep-alive", + "cache-control: no-cache", + "Content-Type: application/json", + source_header, + tracking_header, + "x-xpki-partner-id: comcast", + auth_header, + NULL + }; + + // Take Mutex + if (pthread_mutex_lock(&lock) != 0) + { + rc.application_error_code = 17; + rc.application_error_msg = "sectigo_client_search_certificates: pthread_mutex_lock failed"; + goto cleanup; + } + + int first_param = 1; // Used to determine whether to prepend '?' or '&' for query parameters + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "groupName", property_get(props, CERTIFIER_OPT_SECTIGO_GROUP_NAME), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "groupEmailAddress", property_get(props, CERTIFIER_OPT_SECTIGO_GROUP_EMAIL), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "status", property_get(props, CERTIFIER_OPT_SECTIGO_STATUS), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "commonName", property_get(props, CERTIFIER_OPT_SECTIGO_COMMON_NAME), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "offset", property_get(props, CERTIFIER_OPT_SECTIGO_OFFSET), &first_param); + append_query_param_num(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "limit", (size_t) property_get(props, CERTIFIER_OPT_SECTIGO_LIMIT), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "startDate", property_get(props, CERTIFIER_OPT_SECTIGO_START_DATE), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "endDate", property_get(props, CERTIFIER_OPT_SECTIGO_END_DATE), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "validityStartDate", property_get(props, CERTIFIER_OPT_SECTIGO_VALIDITY_START_DATE), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "validityEndDate", property_get(props, CERTIFIER_OPT_SECTIGO_VALIDITY_END_DATE), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "certOrder", property_get(props, CERTIFIER_OPT_SECTIGO_CERTIFICATE_ORDER), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "isCNinSAN", property_get(props, CERTIFIER_OPT_SECTIGO_IS_CN_IN_SAN), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "requestType", property_get(props, CERTIFIER_OPT_SECTIGO_REQUEST_TYPE), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "timestamp", property_get(props, CERTIFIER_OPT_SECTIGO_TIMESTAMP), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "devhubId", property_get(props, CERTIFIER_OPT_SECTIGO_DEVHUB_ID), &first_param); + append_query_param(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "keyType", property_get(props, CERTIFIER_OPT_SECTIGO_KEY_TYPE), &first_param); + + // certificateId is numeric for this endpoint, but a string for others, so handle separately + const char *cert_id_str = property_get(props, CERTIFIER_OPT_SECTIGO_CERTIFICATE_ID); + if (cert_id_str && strlen(cert_id_str) > 0) { + size_t cert_id_num = (size_t)atol(cert_id_str); + append_query_param_num(sectigo_search_cert_url, sizeof(sectigo_search_cert_url), "certificateId", cert_id_num, &first_param); + } + + log_debug("Sectigo URL: %s\n", sectigo_search_cert_url); + + resp = http_get(props, sectigo_search_cert_url, headers); + if (resp == NULL) + { + goto cleanup; + } + + // Give Mutex + if (pthread_mutex_unlock(&lock) != 0) + { + rc.application_error_code = 18; + rc.application_error_msg = "sectigo_client_search_certificates: pthread_mutex_unlock failed"; + goto cleanup; + } + + rc.application_error_code = resp->error; + + // Check for errors + if (resp->error != 0) + { + rc.application_error_msg = util_format_curl_error("sectigo_client_search_certificates", resp->http_code, resp->error, + resp->error_msg, resp->payload, __FILE__, __LINE__); + goto cleanup; + } + + if (resp->payload == NULL) + { + log_error("ERROR: Failed to populate payload"); + goto cleanup; + } + +// Cleanup +cleanup: + + http_free_response(resp); + + return rc; +} + +CertifierError sectigo_client_renew_certificate(CertifierPropMap * props) +{ + CertifierError rc = CERTIFIER_ERROR_INITIALIZER; + JSON_Value *root_value = NULL; + JSON_Object *root_obj = NULL; + char *json_body = NULL; + + char auth_header[VERY_LARGE_STRING_SIZE * 4] = ""; + char tracking_header[LARGE_STRING_SIZE] = ""; + char source_header[SMALL_STRING_SIZE] = ""; + http_response * resp = NULL; + const char * tracking_id = property_get(props, CERTIFIER_OPT_TRACKING_ID); + const char * bearer_token = property_get(props, CERTIFIER_OPT_SECTIGO_AUTH_TOKEN); + const char * source = "libcertifier"; + const char * sectigo_base_url = property_get(props, CERTIFIER_OPT_SECTIGO_URL); + + if (!bearer_token) { + log_error("Missing CERTIFIER_OPT_SECTIGO_AUTH_TOKEN"); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Bearer token is missing"); + goto cleanup; + } + if (!sectigo_base_url) { + log_error("Missing CERTIFIER_OPT_SECTIGO_URL"); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Sectigo base URL is missing"); + goto cleanup; + } + + // Build full URL: base + endpoint + char sectigo_renew_cert_url[256]; + char renew_cert_endpoint[] = "/api/renewCertificate"; + strncpy(sectigo_renew_cert_url, sectigo_base_url, sizeof(sectigo_renew_cert_url) - 1); + strncpy(sectigo_renew_cert_url + strlen(sectigo_base_url), renew_cert_endpoint, + sizeof(sectigo_renew_cert_url) - 1 - strlen(sectigo_base_url)); + log_debug("Tracking ID is: %s\n", tracking_id); + log_debug("Sectigo URL: %s\n", sectigo_renew_cert_url); + + if (bearer_token != NULL) { + snprintf(auth_header, sizeof(auth_header), "Authorization: %s", bearer_token); + } + snprintf(tracking_header, sizeof(tracking_header), "x-xpki-request-id: %s", tracking_id); + snprintf(source_header, sizeof(source_header), "x-xpki-source: %s", source); + + const char *headers[] = { + "Accept: */*", + "Connection: keep-alive", + "cache-control: no-cache", + "Content-Type: application/json", + source_header, + tracking_header, + "x-xpki-partner-id: comcast", + auth_header, + NULL + }; + + // Take Mutex + if (pthread_mutex_lock(&lock) != 0) + { + rc.application_error_code = 17; + rc.application_error_msg = "sectigo_client_renew_certificate: pthread_mutex_lock failed"; + goto cleanup; + } + + sectigo_renew_cert_param_t params; + xc_sectigo_get_default_renew_cert_param(¶ms); + + // Build JSON body + root_value = json_value_init_object(); + root_obj = json_value_get_object(root_value); + + json_object_set_string(root_obj, "commonName", params.common_name ? params.common_name : ""); + + json_object_set_string(root_obj, "requestorEmail", params.requestor_email); + + // The following parameters are optional (one required). Only include if set + if (params.serial_number) { + json_object_set_string(root_obj, "serialNumber", params.serial_number); + } + + if (params.certificate_id) { + json_object_set_string(root_obj, "certificateId", params.certificate_id); + } + + json_body = json_serialize_to_string(root_value); + if (!json_body) { + log_error("Failed to serialize JSON body"); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Failed to serialize JSON body"); + goto cleanup; + } + + resp = http_post(props, sectigo_renew_cert_url, headers, json_body); + if (resp == NULL) + { + goto cleanup; + } + + // Give Mutex + if (pthread_mutex_unlock(&lock) != 0) + { + rc.application_error_code = 18; + rc.application_error_msg = "sectigo_client_renew_certificate: pthread_mutex_unlock failed"; + goto cleanup; + } + + rc.application_error_code = resp->error; + + // Check for errors + if (resp->error != 0) + { + rc.application_error_msg = util_format_curl_error("sectigo_client_renew_certificate", resp->http_code, resp->error, + resp->error_msg, resp->payload, __FILE__, __LINE__); + goto cleanup; + } + + if (resp->payload == NULL) + { + log_error("ERROR: Failed to populate payload"); + goto cleanup; + } + +// Cleanup +cleanup: + + http_free_response(resp); + + if (json_body) { + json_free_serialized_string(json_body); + } + if (root_value) { + json_value_free(root_value); + } + + return rc; +} + +CertifierError sectigo_client_revoke_certificate(CertifierPropMap * props) +{ + CertifierError rc = CERTIFIER_ERROR_INITIALIZER; + JSON_Value *root_value = NULL; + JSON_Object *root_obj = NULL; + char *json_body = NULL; + + char auth_header[VERY_LARGE_STRING_SIZE * 4] = ""; + char tracking_header[LARGE_STRING_SIZE] = ""; + char source_header[SMALL_STRING_SIZE] = ""; + http_response * resp = NULL; + const char * tracking_id = property_get(props, CERTIFIER_OPT_TRACKING_ID); + const char * bearer_token = property_get(props, CERTIFIER_OPT_SECTIGO_AUTH_TOKEN); + const char * source = "libcertifier"; + const char * sectigo_base_url = property_get(props, CERTIFIER_OPT_SECTIGO_URL); + + if (!bearer_token) { + log_error("Missing CERTIFIER_OPT_SECTIGO_AUTH_TOKEN"); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Bearer token is missing"); + goto cleanup; + } + if (!sectigo_base_url) { + log_error("Missing CERTIFIER_OPT_SECTIGO_URL"); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Sectigo base URL is missing"); + goto cleanup; + } + + // Build full URL: base + endpoint + char sectigo_revoke_cert_url[256]; + char revoke_cert_endpoint[] = "/api/revokeCertificate"; + strncpy(sectigo_revoke_cert_url, sectigo_base_url, sizeof(sectigo_revoke_cert_url) - 1); + strncpy(sectigo_revoke_cert_url + strlen(sectigo_base_url), revoke_cert_endpoint, + sizeof(sectigo_revoke_cert_url) - 1 - strlen(sectigo_base_url)); + log_debug("Tracking ID is: %s\n", tracking_id); + log_debug("Sectigo URL: %s\n", sectigo_revoke_cert_url); + + if (bearer_token != NULL) { + snprintf(auth_header, sizeof(auth_header), "Authorization: %s", bearer_token); + } + snprintf(tracking_header, sizeof(tracking_header), "x-xpki-request-id: %s", tracking_id); + snprintf(source_header, sizeof(source_header), "x-xpki-source: %s", source); + + const char *headers[] = { + "Accept: */*", + "Connection: keep-alive", + "cache-control: no-cache", + "Content-Type: application/json", + source_header, + tracking_header, + "x-xpki-partner-id: comcast", + auth_header, + NULL + }; + + // Take Mutex + if (pthread_mutex_lock(&lock) != 0) + { + rc.application_error_code = 17; + rc.application_error_msg = "sectigo_client_revoke_certificate: pthread_mutex_lock failed"; + goto cleanup; + } + + sectigo_revoke_cert_param_t params; + xc_sectigo_get_default_revoke_cert_param(¶ms); + + // Build JSON body + root_value = json_value_init_object(); + root_obj = json_value_get_object(root_value); + + json_object_set_string(root_obj, "commonName", params.common_name ? params.common_name : ""); + + json_object_set_string(root_obj, "requestorEmail", params.requestor_email); + + json_object_set_string(root_obj, "revocationRequestReason", params.revocation_request_reason); + + // The following parameters are optional (one required). Only include if set + if (params.serial_number) { + json_object_set_string(root_obj, "serialNumber", params.serial_number); + } + + if (params.certificate_id) { + json_object_set_string(root_obj, "certificateId", params.certificate_id); + } + + json_body = json_serialize_to_string(root_value); + if (!json_body) { + log_error("Failed to serialize JSON body"); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Failed to serialize JSON body"); + goto cleanup; + } + + resp = http_put(props, sectigo_revoke_cert_url, headers, json_body); + if (resp == NULL) + { + goto cleanup; + } + + // Give Mutex + if (pthread_mutex_unlock(&lock) != 0) + { + rc.application_error_code = 18; + rc.application_error_msg = "sectigo_client_revoke_certificate: pthread_mutex_unlock failed"; + goto cleanup; + } + + rc.application_error_code = resp->error; + + // Check for errors + if (resp->error != 0) + { + rc.application_error_msg = util_format_curl_error("sectigo_client_revoke_certificate", resp->http_code, resp->error, + resp->error_msg, resp->payload, __FILE__, __LINE__); + goto cleanup; + } + + if (resp->payload == NULL) + { + log_error("ERROR: Failed to populate payload"); + goto cleanup; + } + +// Cleanup +cleanup: + + http_free_response(resp); + + if (json_body) { + json_free_serialized_string(json_body); + } + if (root_value) { + json_value_free(root_value); + } + + return rc; +} + +CertifierError sectigo_client_ocsp_status(CertifierPropMap * props) +{ + CertifierError rc = CERTIFIER_ERROR_INITIALIZER; + const char *pem_path = property_get(props, CERTIFIER_OPT_SECTIGO_CERT_PATH); + if (!pem_path) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Missing cert_path property"); + return rc; + } + + // Read PEM file into memory + FILE *fp = fopen(pem_path, "rb"); + if (!fp) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + char err_msg[512]; + snprintf(err_msg, sizeof(err_msg), "Unable to open PEM file: %s (errno=%d: %s)", + pem_path, errno, strerror(errno)); + rc.application_error_msg = util_format_error_here(err_msg); + log_error("%s", err_msg); + return rc; + } + log_info("PEM file opened successfully, reading content..."); + fseek(fp, 0, SEEK_END); + long fsize = ftell(fp); + fseek(fp, 0, SEEK_SET); + char *pem_buf = (char *)XMALLOC(fsize + 1); + if (!pem_buf) { + fclose(fp); + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Memory allocation failed"); + return rc; + } + fread(pem_buf, 1, fsize, fp); + pem_buf[fsize] = '\0'; + fclose(fp); + + // Load certs from PEM + log_info("Parsing PEM buffer..."); + X509_LIST *certs = NULL; + rc = security_load_certs_from_pem(pem_buf, &certs, true); + XFREE(pem_buf); + if (rc.application_error_code != 0 || !certs) { + if (!rc.application_error_msg) + rc.application_error_msg = util_format_error_here("Failed to parse PEM certs"); + log_error("Failed to load certs from PEM: error_code=%d", rc.application_error_code); + return rc; + } + + // Extract leaf and intermediate certs + log_info("Extracting leaf and intermediate certificates..."); + int cert_count = sk_X509_num(certs); + X509 *leaf = sk_X509_value(certs, cert_count - 1); + X509 *issuer = cert_count > 1 ? sk_X509_value(certs, cert_count - 2) : NULL; + if (!leaf || !issuer) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("PEM must contain at least leaf and intermediate cert"); + security_free_cert_list(certs); + return rc; + } + + // Extract AIA OCSP URL from leaf cert + log_info("Extracting OCSP URL from AIA extension..."); + STACK_OF(OPENSSL_STRING) *ocsp_urls = X509_get1_ocsp(leaf); + if (!ocsp_urls || sk_OPENSSL_STRING_num(ocsp_urls) == 0) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("No OCSP URL found in AIA extension"); + if (ocsp_urls) X509_email_free(ocsp_urls); // X509_email_free works for OCSP URLs too + security_free_cert_list(certs); + return rc; + } + const char *ocsp_url = sk_OPENSSL_STRING_value(ocsp_urls, 0); + log_info("OCSP URL extracted from AIA: %s", ocsp_url); + + // Build OCSP request + OCSP_CERTID *id = OCSP_cert_to_id(NULL, leaf, issuer); + if (!id) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Failed to create OCSP_CERTID"); + X509_email_free(ocsp_urls); + security_free_cert_list(certs); + return rc; + } + OCSP_REQUEST *req = OCSP_REQUEST_new(); + if (!req || !OCSP_request_add0_id(req, id)) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Failed to create OCSP_REQUEST"); + if (req) OCSP_REQUEST_free(req); + OCSP_CERTID_free(id); + X509_email_free(ocsp_urls); + security_free_cert_list(certs); + return rc; + } + + // Serialize OCSP request + unsigned char *req_der = NULL; + int req_der_len = i2d_OCSP_REQUEST(req, &req_der); + if (req_der_len <= 0) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Failed to encode OCSP request"); + OCSP_REQUEST_free(req); + X509_email_free(ocsp_urls); + security_free_cert_list(certs); + return rc; + } + + // Base64 encode the OCSP request + int base64_len = base64_encode_len(req_der_len); + char *base64_req = (char *)XMALLOC(base64_len + 1); + if (!base64_req) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Memory allocation failed for base64 encoding"); + OPENSSL_free(req_der); + OCSP_REQUEST_free(req); + X509_email_free(ocsp_urls); + security_free_cert_list(certs); + return rc; + } + base64_encode(base64_req, req_der, req_der_len); + base64_req[base64_len] = '\0'; + + // URL-encode the base64 string using curl_easy_escape + CURL *curl = curl_easy_init(); + if (!curl) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Failed to initialize CURL for URL encoding"); + XFREE(base64_req); + OPENSSL_free(req_der); + OCSP_REQUEST_free(req); + X509_email_free(ocsp_urls); + security_free_cert_list(certs); + return rc; + } + + char *url_encoded = curl_easy_escape(curl, base64_req, 0); + curl_easy_cleanup(curl); + + if (!url_encoded) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("URL encoding failed"); + XFREE(base64_req); + OPENSSL_free(req_der); + OCSP_REQUEST_free(req); + X509_email_free(ocsp_urls); + security_free_cert_list(certs); + return rc; + } + + // Build OCSP GET URL + char ocsp_get_url[4096]; + snprintf(ocsp_get_url, sizeof(ocsp_get_url), "%s/%s", ocsp_url, url_encoded); + + // Send OCSP GET request + http_response *resp = NULL; + resp = http_get(props, ocsp_get_url, NULL); + curl_free(url_encoded); // Free the curl-allocated string + XFREE(base64_req); + + if (!resp || !resp->payload) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Failed to get OCSP response"); + if (req_der) OPENSSL_free(req_der); + OCSP_REQUEST_free(req); + X509_email_free(ocsp_urls); + security_free_cert_list(certs); + return rc; + } + + // Parse OCSP response (response is binary DER) + const unsigned char *p = (const unsigned char *)resp->payload; + long resp_len = (long)resp->payload_len; + + if (resp->http_code != 200) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + char err_msg[512]; + snprintf(err_msg, sizeof(err_msg), "Invalid OCSP response: HTTP %d", resp->http_code); + rc.application_error_msg = util_format_error_here(err_msg); + if (req_der) OPENSSL_free(req_der); + OCSP_REQUEST_free(req); + X509_email_free(ocsp_urls); + security_free_cert_list(certs); + http_free_response(resp); + return rc; + } + + OCSP_RESPONSE *ocsp_resp = d2i_OCSP_RESPONSE(NULL, &p, resp_len); + if (!ocsp_resp) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Failed to parse OCSP response"); + if (req_der) OPENSSL_free(req_der); + OCSP_REQUEST_free(req); + X509_email_free(ocsp_urls); + security_free_cert_list(certs); + http_free_response(resp); + return rc; + } + + int status = OCSP_response_status(ocsp_resp); + if (status != OCSP_RESPONSE_STATUS_SUCCESSFUL) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("OCSP response not successful"); + } else { + OCSP_BASICRESP *basic = OCSP_response_get1_basic(ocsp_resp); + if (!basic) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + rc.application_error_msg = util_format_error_here("Failed to parse OCSP_BASICRESP"); + } else { + int cert_status, crl_reason; + ASN1_GENERALIZEDTIME *revtime, *thisupd, *nextupd; + cert_status = OCSP_single_get0_status( + OCSP_resp_get0(basic, 0), &crl_reason, &revtime, &thisupd, &nextupd); + + const char *status_str = OCSP_cert_status_str(cert_status); + char msg[512]; + + if (cert_status == V_OCSP_CERTSTATUS_GOOD) { + rc.application_error_code = 0; + snprintf(msg, sizeof(msg), "Certificate status: %s", status_str); + rc.application_error_msg = util_format_error_here(msg); + log_info("%s", msg); + } else if (cert_status == V_OCSP_CERTSTATUS_REVOKED) { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + snprintf(msg, sizeof(msg), "Certificate status: %s (reason=%d)", status_str, crl_reason); + rc.application_error_msg = util_format_error_here(msg); + log_error("%s", msg); + } else { + rc.application_error_code = CERTIFIER_ERR_EMPTY_OR_INVALID_PARAM_1; + snprintf(msg, sizeof(msg), "Certificate status: %s", status_str); + rc.application_error_msg = util_format_error_here(msg); + log_warn("%s", msg); + } + OCSP_BASICRESP_free(basic); + } + } + + OCSP_RESPONSE_free(ocsp_resp); + if (req_der) OPENSSL_free(req_der); + OCSP_REQUEST_free(req); + X509_email_free(ocsp_urls); + security_free_cert_list(certs); + http_free_response(resp); + return rc; +} + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_get_cert(sectigo_get_cert_param_t * params) +{ + Certifier *certifier = get_sectigo_certifier_instance(); + + // Build JSON body + JSON_Value *root_value = json_value_init_object(); + JSON_Object *root_obj = json_value_get_object(root_value); + + // Add all parameters to JSON body using passed-in params + if (params->common_name) + json_object_set_string(root_obj, "commonName", params->common_name); + if (params->group_name) + json_object_set_string(root_obj, "groupName", params->group_name); + if (params->group_email) + json_object_set_string(root_obj, "groupEmailAddress", params->group_email); + if (params->id) + json_object_set_string(root_obj, "id", params->id); + if (params->owner_first_name) + json_object_set_string(root_obj, "ownerFirstName", params->owner_first_name); + if (params->owner_last_name) + json_object_set_string(root_obj, "ownerLastName", params->owner_last_name); + if (params->project_name) + json_object_set_string(root_obj, "projectName", params->project_name); + if (params->business_justification) + json_object_set_string(root_obj, "businessJustification", params->business_justification); + + // subjectAltNames as array + JSON_Value *san_array = json_value_init_array(); + JSON_Array *san_json_array = json_value_get_array(san_array); + if (params->subject_alt_names && strlen(params->subject_alt_names) > 0) { + char *san_copy = XSTRDUP(params->subject_alt_names); + char *token = strtok(san_copy, ","); + while (token) { + json_array_append_value(san_json_array, json_value_init_string(token)); + token = strtok(NULL, ","); + } + XFREE(san_copy); + } + json_object_set_value(root_obj, "subjectAltNames", san_array); + + json_object_set_string(root_obj, "certificateType", "comodo"); // Always "comodo" + + if (params->owner_email) + json_object_set_string(root_obj, "ownerEmailAddress", params->owner_email); + + // Generate CSR and add to body + char *csr_pem = NULL; + CertifierError csr_rc = sectigo_generate_certificate_signing_request(certifier, &csr_pem); + if (csr_rc.application_error_code != 0 || csr_pem == NULL) { + log_error("Failed to generate CSR: %s\n", csr_rc.application_error_msg); + if (csr_pem) XFREE(csr_pem); + json_value_free(root_value); + return csr_rc.application_error_code; + } + json_object_set_string(root_obj, "certificateSigningRequest", csr_pem); + + // Serialize JSON body + char *json_body = json_serialize_to_string(root_value); + + // Call the request function + CertifierPropMap *props = certifier_get_prop_map(certifier); + char *cert_output = NULL; + CertifierError req_rc = sectigo_client_request_certificate( + props, + (unsigned char *)csr_pem, + NULL, // node_address + NULL, // certifier_id + &cert_output + ); + + // Cleanup + if (csr_pem) XFREE(csr_pem); + if (json_body) json_free_serialized_string(json_body); + if (root_value) json_value_free(root_value); + + return req_rc.application_error_code; +} + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_search_cert(sectigo_search_cert_param_t * params) +{ + Certifier *certifier = get_sectigo_certifier_instance(); + + // Call the request function + CertifierPropMap *props = certifier_get_prop_map(certifier); + CertifierError req_rc = sectigo_client_search_certificates(props); + + return req_rc.application_error_code; +} + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_renew_cert(sectigo_renew_cert_param_t * params) +{ + Certifier *certifier = get_sectigo_certifier_instance(); + + // Call the request function + CertifierPropMap *props = certifier_get_prop_map(certifier); + CertifierError req_rc = sectigo_client_renew_certificate(props); + + return req_rc.application_error_code; +} + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_revoke_cert(sectigo_revoke_cert_param_t * params) +{ + Certifier *certifier = get_sectigo_certifier_instance(); + + // Call the request function + CertifierPropMap *props = certifier_get_prop_map(certifier); + CertifierError req_rc = sectigo_client_revoke_certificate(props); + + return req_rc.application_error_code; +} + +SECTIGO_CLIENT_ERROR_CODE xc_sectigo_ocsp_status(sectigo_ocsp_status_param_t * params) +{ + Certifier *certifier = get_sectigo_certifier_instance(); + + // Call the request function + CertifierPropMap *props = certifier_get_prop_map(certifier); + CertifierError req_rc = sectigo_client_ocsp_status(props); + + return req_rc.application_error_code; +} diff --git a/src/xpki_client.c b/src/xpki_client.c index ac233e5..978eab0 100644 --- a/src/xpki_client.c +++ b/src/xpki_client.c @@ -51,6 +51,8 @@ XPKI_AUTH_TYPE map_to_xpki_auth_type(const char * str) { return XPKI_AUTH_SAT; } + + return XPKI_AUTH_X509; // Default to X509 } const char * xpki_auth_type_to_string(XPKI_AUTH_TYPE auth_type) diff --git a/tests/tests.c b/tests/tests.c index 5e86f55..929d5e7 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -54,7 +54,7 @@ static char * g_mock_http_expected_body = NULL; static http_response g_mock_http_response; // FIXME: use cmocka object mocking for this instead of assert -static http_response * do_http(const char * url, const char * http_headers[], const char * body) +static http_response * do_http(const char * url, const char * http_headers[], const char * body, const char * http_method) { if (g_mock_http_expected_url != NULL) assert_string_equal(g_mock_http_expected_url, url); @@ -72,13 +72,18 @@ static http_response * do_http(const char * url, const char * http_headers[], co http_response * http_get(const CertifierPropMap * props, const char * url, const char * http_headers[]) { - return do_http(url, http_headers, NULL); + return do_http(url, http_headers, NULL, "GET"); } http_response * http_post(const CertifierPropMap * props, const char * url, const char * http_headers[], const char * body) { - return do_http(url, http_headers, body); + return do_http(url, http_headers, body, "POST"); +} + +http_response * http_put(const CertifierPropMap * props, const char * url, const char * http_headers[], const char * body) +{ + return do_http(url, http_headers, body, "PUT"); } void http_free_response(http_response * resp) @@ -107,7 +112,8 @@ static void mock_http_set_response_success(const char * body, int status) g_mock_http_response.error = 0; } -static void mock_http_set_response_failure(const char * err, int status) +// Add unused attribute marker to suppress warning +__attribute__((unused)) static void mock_http_set_response_failure(const char * err, int status) { g_mock_http_response.payload = NULL; g_mock_http_response.http_code = status; @@ -115,7 +121,7 @@ static void mock_http_set_response_failure(const char * err, int status) g_mock_http_response.error = status; } -static void test_certifier_client_requests(void ** state) +static void test_certifier_client_requests(void) { const char * csr = "CSr"; @@ -163,9 +169,9 @@ static void test_certifier_client_requests(void ** state) certifier_set_property(certifier, CERTIFIER_OPT_PROFILE_NAME, profile_name); certifier_set_property(certifier, CERTIFIER_OPT_PRODUCT_ID, product_id); - int options = certifier_get_property(certifier, CERTIFIER_OPT_OPTIONS); - options |= CERTIFIER_OPT_CERTIFICATE_LITE; - certifier_set_property(certifier, CERTIFIER_OPT_OPTIONS, options); + int options = (int)(size_t)certifier_get_property(certifier, CERTIFIER_OPT_OPTIONS); + options |= CERTIFIER_OPTION_CERTIFICATE_LITE; + certifier_set_property(certifier, CERTIFIER_OPT_OPTIONS, (void *)(size_t)options); CertifierError rc = certifierclient_request_x509_certificate(_certifier_get_properties(certifier), (unsigned char *) csr, node_address, certifier_id, &ret); @@ -181,7 +187,7 @@ static void test_certifier_client_requests(void ** state) } } -static void test_certifier_client_requests1(void ** state) +static void test_certifier_client_requests1(void) { const char * csr = "CSr"; @@ -196,7 +202,7 @@ static void test_certifier_client_requests1(void ** state) const char * source = "test_libledger"; char * ret = NULL; char * cn_prefix = NULL; - int icount = 0, return_code = 0; + int return_code = 0; unsigned int num_days = 0; JSON_Value * root_value = json_value_init_object(); @@ -207,11 +213,11 @@ static void test_certifier_client_requests1(void ** state) return_code = certifier_set_property(certifier, CERTIFIER_OPT_CN_PREFIX, "xcal.tv"); assert_int_equal(0, return_code); - return_code = certifier_set_property(certifier, CERTIFIER_OPT_VALIDITY_DAYS, 730); + return_code = certifier_set_property(certifier, CERTIFIER_OPT_VALIDITY_DAYS, (void *)(size_t)730); assert_int_equal(0, return_code); cn_prefix = certifier_get_property(certifier, CERTIFIER_OPT_CN_PREFIX); - num_days = certifier_get_property(certifier, CERTIFIER_OPT_VALIDITY_DAYS); + num_days = (unsigned int)(size_t)certifier_get_property(certifier, CERTIFIER_OPT_VALIDITY_DAYS); assert_int_equal(730, num_days); if (cn_prefix) { @@ -253,9 +259,9 @@ static void test_certifier_client_requests1(void ** state) certifier_set_property(certifier, CERTIFIER_OPT_PROFILE_NAME, profile_name); certifier_set_property(certifier, CERTIFIER_OPT_PRODUCT_ID, product_id); - int options = certifier_get_property(certifier, CERTIFIER_OPT_OPTIONS); - options |= CERTIFIER_OPT_CERTIFICATE_LITE; - certifier_set_property(certifier, CERTIFIER_OPT_OPTIONS, options); + int options = (int)(size_t)certifier_get_property(certifier, CERTIFIER_OPT_OPTIONS); + options |= CERTIFIER_OPTION_CERTIFICATE_LITE; + certifier_set_property(certifier, CERTIFIER_OPT_OPTIONS, (void *)(size_t)options); CertifierError rc = certifierclient_request_x509_certificate(_certifier_get_properties(certifier), (unsigned char *) csr, cn_prefix, ledger_id, &ret); @@ -275,7 +281,7 @@ static void test_certifier_client_requests1(void ** state) } } -static void test_certifier_create_crt1(void ** state) +static void test_certifier_create_crt1(void) { int rc = 0; char * out_crt = NULL; @@ -294,7 +300,7 @@ static void test_certifier_create_crt1(void ** state) XFREE(out_crt); } -static void test_certifier_create_node_address(void ** state) +static void test_certifier_create_node_address(void) { int rc = 0; char * node_address = NULL; @@ -310,7 +316,7 @@ static void test_certifier_create_node_address(void ** state) XFREE(node_address); } -static void test_certifier_get_version(void ** state) +static void test_certifier_get_version(void) { char * out_version = certifier_get_version(certifier); @@ -373,7 +379,7 @@ static int tearDown(void ** state) #endif -static void test_base64(void ** state) +static void test_base64(void) { char buf[64]; @@ -415,7 +421,7 @@ static void test_base64(void ** state) assert_memory_equal("\x01\x02\xFF\x80\x81\x7F\x42\x42", buf, 8); } -static void test_base58(void ** state) +static void test_base58(void) { char b58[128]; uint8_t input[26]; @@ -449,7 +455,7 @@ static void test_base58(void ** state) assert_string_equal("1112", b58); } -static void test_file_utils(void ** state) +static void test_file_utils(void) { char temp_file[128]; XSTRCPY(temp_file, "/tmp/certifier_test.XXXXXXX"); @@ -472,7 +478,7 @@ static void test_file_utils(void ** state) assert_int_equal(-1, util_delete_file("/tmp/moved_temp")); } -void test_sha256_ripemd_b58(void ** state) +void test_sha256_ripemd_b58(void) { #define NUM_HASH_INPUTS 180 @@ -556,7 +562,7 @@ void test_sha256_ripemd_b58(void ** state) } } -static void test_set_curl_error(void ** state) +static void test_set_curl_error(void) { char errbuf[1024] = { "some error" }; int res = 0; @@ -566,7 +572,7 @@ static void test_set_curl_error(void ** state) XFREE(err); } -static void test_str_utils(void ** state) +static void test_str_utils(void) { char str[128] = { 0 }; @@ -629,7 +635,7 @@ static void test_str_utils(void ** state) assert_true(util_is_empty(line4)); } -static void test_random_val(void ** state) +static void test_random_val(void) { char * str = util_generate_random_value(5, ""); @@ -661,7 +667,7 @@ static void test_random_val(void ** state) assert_true(b_cnt > 32); } -static void test_ecc_key(void ** state) +static void test_ecc_key(void) { // verify free(NULL) is ok @@ -741,7 +747,7 @@ static void test_ecc_key(void ** state) XFREE(csr); } -static void test_verify_signature_1(void ** state) +static void test_verify_signature_1(void) { const char * pub_key_b64 = "MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEPGYYPEBOW/v/Kori+9rkwyDLijQ+OyOcXWN/" @@ -764,7 +770,7 @@ static void test_verify_signature_1(void ** state) security_free_eckey(deserialized_ecc_key); } -static void test_verify_signature_2(void ** state) +static void test_verify_signature_2(void) { int i = 0; @@ -871,7 +877,7 @@ static void test_verify_signature_2(void ** state) } } -void test_x509_cert(void ** state) +void test_x509_cert(void) { const char * digicert_pem_pkcs7_blob = "-----BEGIN PKCS7-----\n" @@ -1015,7 +1021,7 @@ void test_x509_cert(void ** state) char * cert3_ou = NULL; // try cert only format - CertifierError rc = security_load_certs_from_pem(ejbca_with_hsm_pem_cert_only_blob, &cert_list); + CertifierError rc = security_load_certs_from_pem(ejbca_with_hsm_pem_cert_only_blob, &cert_list, false); assert_int_equal(0, rc.application_error_code); assert_non_null(cert_list); cert1 = security_cert_list_get(cert_list, 0); @@ -1027,7 +1033,7 @@ void test_x509_cert(void ** state) cert_list = NULL; // try ejbca pkcs7 format - rc = security_load_certs_from_pem(ejbca_with_hsm_pem_pkcs7_blob, &cert_list); + rc = security_load_certs_from_pem(ejbca_with_hsm_pem_pkcs7_blob, &cert_list, false); assert_int_equal(0, rc.application_error_code); assert_non_null(cert_list); cert1 = security_cert_list_get(cert_list, 0); @@ -1039,7 +1045,7 @@ void test_x509_cert(void ** state) cert_list = NULL; // try digicert pkcs7 format (original tests) - rc = security_load_certs_from_pem(digicert_pem_pkcs7_blob, &cert_list); + rc = security_load_certs_from_pem(digicert_pem_pkcs7_blob, &cert_list, false); assert_int_equal(0, rc.application_error_code); assert_non_null(cert_list); @@ -1127,7 +1133,7 @@ void test_x509_cert(void ** state) security_free_cert_list(cert_list); } -static void test_pkcs12(void ** state) +static void test_pkcs12(void) { const char * pkcs12_blob_base64 = "MIII6AIBAzCCCK4GCSqGSIb3DQEHAaCCCJ8EggibMIIIlzCCB2oGCSqGSIb3DQEHAaCCB1sEggdXMIIHUzCCAz8GCyqGSIb3DQEMCgEDoIIC8DCCAuwGCiqGSI" @@ -1172,7 +1178,6 @@ static void test_pkcs12(void ** state) XFILE pkcs12_file = NULL; X509_LIST * certs = NULL; ECC_KEY * key = NULL; - ECC_KEY * dup_key = NULL; X509_CERT * cert = NULL; char * certifier_id = NULL; char * generated_crt = NULL; @@ -1181,12 +1186,8 @@ static void test_pkcs12(void ** state) unsigned char * der_key = NULL; int der_key_len = 0; int ret = 0; - - char * tmp_crt = NULL; - int rc = 0; - const char * expires = "0"; - const char * action = "allow"; + certifier_set_property(certifier, CERTIFIER_OPT_OUTPUT_NODE, "dummy output node"); blob_len = base64_decode(pkcs12_blob, pkcs12_blob_base64); @@ -1387,7 +1388,7 @@ static void test_pkcs12(void ** state) * The Java equivalent test took ~ 5189 ms to complete 1 million iterations. * This took ~ 4852 ms seconds in C (using -Os compiler flag). */ -static void test_sha256_ripemd_b58_performance(void ** state) +static void test_sha256_ripemd_b58_performance(void) { const char * value_prefix = "value12345678890000000000000000000000-0"; @@ -1438,7 +1439,7 @@ static void cleanup_logs(void) } // still a WIP - need more testing -static void test_logging(void ** state) +static void test_logging(void) { int i; @@ -1464,7 +1465,7 @@ static void test_logging(void ** state) cleanup_logs(); } -static void test_options(void ** state) +static void test_options(void) { CertifierPropMap * props = _certifier_get_properties(certifier); diff --git a/tests/xc_apis/xc_api_tests.c b/tests/xc_apis/xc_api_tests.c index 183c30e..738c36e 100644 --- a/tests/xc_apis/xc_api_tests.c +++ b/tests/xc_apis/xc_api_tests.c @@ -18,7 +18,7 @@ #include #include - +#include #include static const char * token = NULL; @@ -237,7 +237,33 @@ static void test_get_cert_validity() TEST_ASSERT_EQUAL_INT(XPKI_CLIENT_SUCCESS, error); TEST_ASSERT_EQUAL_INT(XPKI_CLIENT_CERT_ABOUT_TO_EXPIRE, status); } - +static void test_get_sectigo_cert() +{ + SECTIGO_CLIENT_ERROR_CODE error; + sectigo_get_cert_param_t params = { 0 }; + + // Fill parameters (simulate config or CLI) + params.auth_token = "token"; + params.common_name = "sectigotest.comcast.com"; + params.group_name = "GroupName"; + params.group_email = "example@comcast.com"; + params.id = "exid"; + params.owner_first_name = "First"; + params.owner_last_name = "Last"; + params.project_name = "Testing create with SAT"; + params.business_justification = "Testing create with SAT"; + params.subject_alt_names = "*"; + params.owner_email = "first_last@comcast.com"; + params.sectigo_url = "https://certs-dev.xpki.io/api/createCertificate"; + params.devhub_id = "12345"; + params.validity_days = 90; + params.key_type = "RSA-8192"; + + // Call the API + error = xc_sectigo_get_cert(¶ms); + + TEST_ASSERT_EQUAL_INT(SECTIGO_CLIENT_SUCCESS, error); +} int main(int argc, char ** argv) { UNITY_BEGIN(); @@ -254,6 +280,7 @@ int main(int argc, char ** argv) RUN_TEST(test_renew_cert); RUN_TEST(test_print_cert_validity); RUN_TEST(test_get_cert_validity); + RUN_TEST(test_get_sectigo_cert); return UNITY_END(); }