SDK สำหรับ TESAIoT Dev Kit
คู่มืออ้างอิง API และ Tutorial (ModusToolbox)
Loading...
Searching...
No Matches
optiga_trust_helpers.h
Go to the documentation of this file.
1/******************************************************************************
2* File Name: optiga_trust_helpers.h
3*
4* Description: This file contains helping fucntions to read a certificate or
5* change some default parameter
6*
7* Related Document: See README.md
8*
9*
10*******************************************************************************
11* Copyright 2020-2025, Cypress Semiconductor Corporation (an Infineon company) or
12* an affiliate of Cypress Semiconductor Corporation. All rights reserved.
13*
14* This software, including source code, documentation and related
15* materials ("Software") is owned by Cypress Semiconductor Corporation
16* or one of its affiliates ("Cypress") and is protected by and subject to
17* worldwide patent protection (United States and foreign),
18* United States copyright laws and international treaty provisions.
19* Therefore, you may use this Software only as provided in the license
20* agreement accompanying the software package from which you
21* obtained this Software ("EULA").
22* If no EULA applies, Cypress hereby grants you a personal, non-exclusive,
23* non-transferable license to copy, modify, and compile the Software
24* source code solely for use in connection with Cypress's
25* integrated circuit products. Any reproduction, modification, translation,
26* compilation, or representation of this Software except as specified
27* above is prohibited without the express written permission of Cypress.
28*
29* Disclaimer: THIS SOFTWARE IS PROVIDED AS-IS, WITH NO WARRANTY OF ANY KIND,
30* EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, NONINFRINGEMENT, IMPLIED
31* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. Cypress
32* reserves the right to make changes to the Software without notice. Cypress
33* does not assume any liability arising out of the application or use of the
34* Software or any product or circuit described in the Software. Cypress does
35* not authorize its products for use in any products where a malfunction or
36* failure of the Cypress product may reasonably be expected to result in
37* significant property damage, injury or death ("High Risk Product"). By
38* including Cypress's product in a High Risk Product, the manufacturer
39* of such system or application assumes all risk of such use and in doing
40* so agrees to indemnify Cypress against all liability.
41*******************************************************************************/
42
43/******************************************************************************
44* TESAIoT Platform Extensions
45*******************************************************************************
46* This file has been extended through collaboration between:
47* - Infineon Technologies AG (OPTIGA Trust M expertise)
48* - TESAIoT Platform Developer Team (AIoT platform integration)
49*
50* Extensions added:
51* - Certificate validation and expiry checking functions
52* - CSR generation with OPTIGA Trust M integration
53* - Protected Update data set helpers
54* - TESAIoT Platform-specific certificate lifecycle management
55*
56* Original work Copyright 2020-2025 Cypress Semiconductor Corporation (Infineon)
57* Extensions developed jointly by Infineon Technologies AG and TESAIoT Team
58*
59* This file is part of the TESAIoT AIoT Foundation Platform, developed in
60* collaboration with Infineon Technologies AG for PSoC Edge E84 + OPTIGA Trust M.
61*
62* Contact: Wiroon Sriborrirux <sriborrirux@gmail.com>
63*******************************************************************************/
64
65#ifndef OPTIGA_TRUST_HELPERS_H_
66#define OPTIGA_TRUST_HELPERS_H_
67
68#include <stdlib.h>
69#include <stdio.h>
70#include <stdbool.h>
71#include "include/common/optiga_lib_common.h"
72#include "include/optiga_crypt.h"
73#include "include/optiga_util.h"
74#include "include/pal/pal_os_timer.h"
75
95
111
112
113
114void read_certificate_from_optiga(uint16_t optiga_oid, char * cert_pem, uint16_t * cert_pem_length);
115
116void read_trust_anchor_from_optiga(uint16_t oid, char * cert_pem, uint16_t * cert_pem_length);
117
118void write_data_object (uint16_t oid, const uint8_t * p_data, uint16_t length);
119
121
122/* Proves that the private key in key_oid belongs to the certificate in cert_oid,
123 * by signing a challenge in the chip and verifying it against the certificate's
124 * public key. 1 = match, 0 = mismatch, negative = could not run.
125 * Not used by the connect path — reachable as optiga.verify_pair(). */
126int optiga_verify_cert_key_pair(uint16_t cert_oid, uint16_t key_oid);
127
128/* Put a data object's change access condition back to "an ordinary write is
129 * accepted", clearing the Int(anchor) that Protected Update set. Reversible by
130 * design — see the definition. */
131bool optiga_clear_manifest_lock(uint16_t target_oid);
132
141typedef struct {
142 uint16_t oid;
143 bool valid;
146 uint16_t used_size;
147 uint8_t data_type;
149
150bool optiga_slot_info(uint16_t oid, optiga_slot_info_t *info);
151bool optiga_slot_info_raw(uint16_t oid, uint16_t *anchor, uint32_t *version,
152 uint16_t *used, uint8_t *type);
153uint16_t optiga_slot_manifest_anchor(uint16_t oid);
154uint32_t optiga_slot_payload_version(uint16_t oid);
155
156/* Opens the OPTIGA application, recovering from one left open by a previous
157 * host session. Returns false when the secure element is unusable. */
159
160/* Releases one reference taken above; the application closes only when the last
161 * owner lets go. */
163
164void optiga_util_callback(void *context, optiga_lib_status_t return_status);
165
166/* Global async status - can be polled by other modules for operation completion */
167extern volatile optiga_lib_status_t optiga_lib_status;
168
169void trustm_close(optiga_util_t *util);
170
171optiga_lib_status_t trustm_gen_ecc_keypair(uint16_t optiga_key_id, optiga_ecc_curve_t curve_id,uint8_t key_usage, bool export_private, uint8_t * pub, uint16_t *pub_len);
172
173/* Verify a store-signed Edge AI model blob, in the secure element.
174 *
175 * Returns 1 when the signature is good; negative otherwise, and the codes are
176 * distinct on purpose -- "no signature", "malformed", "the chip said no" and
177 * "could not ask the chip" call for different responses, and collapsing them
178 * would make a bus failure look like an attack. See ipc_model_stage_defs.h. */
179int optiga_verify_staged_model(const uint8_t *blob, uint32_t blob_len);
180
181optiga_lib_status_t trustm_ecdsa_sign(optiga_key_id_t oid, const uint8_t *digest, uint16_t digest_len, uint8_t *sig_raw, uint16_t *sig_raw_length);
182
183bool optiga_read_factory_uid(char *uid_hex, size_t uid_hex_len);
184
185bool optiga_read_factory_certificate(char *cert_pem, uint16_t *cert_pem_length);
186
187bool optiga_read_device_certificate(char *cert_pem, uint16_t *cert_pem_length);
188
190
192
193bool optiga_generate_device_keypair(uint16_t key_oid, uint8_t *public_key_der, uint16_t *public_key_der_len);
194
195/*******************************************************************************
196 * TESAIoT Platform Extensions - Types and Functions
197 *******************************************************************************
198 * REFACTORING NOTE (2026-01-18):
199 * The following types and functions (lines 139-217) are TESAIoT extensions
200 * added to this Infineon file. They are candidates for extraction to a
201 * separate file: tesaiot_cert_utils.h (planned for v0.9)
202 *
203 * TESAIoT-specific additions:
204 * - cert_validation_result_t struct
205 * - cert_source_t enum
206 * - optiga_check_certificate_validity()
207 * - optiga_get_cert_days_until_expiry()
208 * - optiga_generate_csr_pem()
209 *
210 * See: IMPROVEMENTS/2026.01/01_header_organization_analysis.md
211 ******************************************************************************/
212
219typedef struct {
220 bool is_valid;
224 time_t valid_from;
225 time_t valid_to;
226 char subject[256];
227 char issuer[256];
229
233typedef enum {
237
252
262
263bool optiga_generate_csr_pem(uint16_t key_oid, const uint8_t *public_key_der, uint16_t public_key_der_len, const char *subject, char *csr_pem, size_t csr_pem_len);
264
265/* Where write_device_certificate_and_verify() puts the certificate: Cert 2,
266 * the slot this project's pre-provisioning plan pairs with ECC Key 2 (0xE0F1).
267 * Defined here as well as in the .c so callers can verify the pair afterwards
268 * without hardcoding the number a second time. */
269#ifndef DEVICE_CERTIFICATE_OID
270#define DEVICE_CERTIFICATE_OID (0xE0E1U)
271#endif
272
274optiga_lib_status_t protected_update(uint16_t trust_anchor_oid, uint16_t target_key_oid, uint16_t confidentiality_oid);
275
298optiga_lib_status_t test_metadata_operations(void);
299
300#endif /* OPTIGA_TRUST_HELPERS_H_ */
optiga_lib_status_t write_device_certificate_and_verify()
void read_certificate_from_optiga(uint16_t optiga_oid, char *cert_pem, uint16_t *cert_pem_length)
bool optiga_read_device_certificate(char *cert_pem, uint16_t *cert_pem_length)
optiga_lib_status_t test_metadata_operations(void)
uint32_t optiga_get_cert_days_until_expiry(uint16_t oid)
Get days until certificate expiry.
void trustm_close(optiga_util_t *util)
bool trustm_use_device_certificate(void)
optiga_lib_status_t trustm_gen_ecc_keypair(uint16_t optiga_key_id, optiga_ecc_curve_t curve_id, uint8_t key_usage, bool export_private, uint8_t *pub, uint16_t *pub_len)
cert_source_t
Certificate source type for MQTT TLS connection.
Definition optiga_trust_helpers.h:233
@ CERT_SOURCE_FACTORY
Infineon Factory Certificate (fallback).
Definition optiga_trust_helpers.h:235
@ CERT_SOURCE_DEVICE
TESAIoT Platform Certificate (preferred).
Definition optiga_trust_helpers.h:234
struct optiga_protected_update_manifest_fragment_configuration optiga_protected_update_manifest_fragment_configuration_t
Specifies the structure for protected update manifest and fragment configuration.
bool optiga_slot_info(uint16_t oid, optiga_slot_info_t *info)
bool optiga_read_factory_certificate(char *cert_pem, uint16_t *cert_pem_length)
bool optiga_generate_device_keypair(uint16_t key_oid, uint8_t *public_key_der, uint16_t *public_key_der_len)
bool optiga_trust_open_application(void)
bool optiga_trust_init(void)
int optiga_verify_staged_model(const uint8_t *blob, uint32_t blob_len)
int optiga_verify_cert_key_pair(uint16_t cert_oid, uint16_t key_oid)
void optiga_util_callback(void *context, optiga_lib_status_t return_status)
void write_data_object(uint16_t oid, const uint8_t *p_data, uint16_t length)
uint16_t optiga_slot_manifest_anchor(uint16_t oid)
optiga_lib_status_t trustm_ecdsa_sign(optiga_key_id_t oid, const uint8_t *digest, uint16_t digest_len, uint8_t *sig_raw, uint16_t *sig_raw_length)
struct optiga_protected_update_data_configuration optiga_protected_update_data_configuration_t
Specifies the structure for protected update data configuration.
bool optiga_generate_csr_pem(uint16_t key_oid, const uint8_t *public_key_der, uint16_t public_key_der_len, const char *subject, char *csr_pem, size_t csr_pem_len)
bool trustm_use_factory_certificate(void)
void optiga_trust_close_application(void)
void read_trust_anchor_from_optiga(uint16_t oid, char *cert_pem, uint16_t *cert_pem_length)
bool optiga_read_factory_uid(char *uid_hex, size_t uid_hex_len)
bool optiga_check_certificate_validity(uint16_t oid, cert_validation_result_t *result)
Check certificate validity and extract information.
bool optiga_clear_manifest_lock(uint16_t target_oid)
optiga_lib_status_t protected_update(uint16_t trust_anchor_oid, uint16_t target_key_oid, uint16_t confidentiality_oid)
bool optiga_slot_info_raw(uint16_t oid, uint16_t *anchor, uint32_t *version, uint16_t *used, uint8_t *type)
uint32_t optiga_slot_payload_version(uint16_t oid)
volatile optiga_lib_status_t optiga_lib_status
Certificate validation result structure.
Definition optiga_trust_helpers.h:219
time_t valid_to
Certificate valid to timestamp.
Definition optiga_trust_helpers.h:225
char issuer[256]
Certificate issuer DN.
Definition optiga_trust_helpers.h:227
time_t valid_from
Certificate valid from timestamp.
Definition optiga_trust_helpers.h:224
uint32_t days_until_expiry
Days remaining until expiry (0 if expired).
Definition optiga_trust_helpers.h:223
bool is_expired
true if certificate has expired
Definition optiga_trust_helpers.h:221
bool is_valid
true if certificate is currently valid
Definition optiga_trust_helpers.h:220
bool cert_exists
true if certificate found in OID
Definition optiga_trust_helpers.h:222
char subject[256]
Certificate subject DN.
Definition optiga_trust_helpers.h:226
Specifies the structure for protected update data configuration.
Definition optiga_trust_helpers.h:99
const optiga_protected_update_manifest_fragment_configuration_t * data_config
Pointer to a buffer where continue fragment data is stored.
Definition optiga_trust_helpers.h:107
uint16_t target_oid_metadata_length
Target OID metadata length.
Definition optiga_trust_helpers.h:105
uint16_t target_oid
Target OID.
Definition optiga_trust_helpers.h:101
const char * set_prot_example_string
Pointer to a protected update example string.
Definition optiga_trust_helpers.h:109
const uint8_t * target_oid_metadata
Target OID metadata.
Definition optiga_trust_helpers.h:103
Specifies the structure for protected update manifest and fragment configuration.
Definition optiga_trust_helpers.h:79
uint16_t continue_fragment_length
Continue fragment length.
Definition optiga_trust_helpers.h:89
uint8_t manifest_version
Manifest version.
Definition optiga_trust_helpers.h:81
uint16_t final_fragment_length
Final fragment length.
Definition optiga_trust_helpers.h:93
uint16_t manifest_length
Manifest length.
Definition optiga_trust_helpers.h:85
const uint8_t * manifest_data
Pointer to a buffer where manifest data is stored.
Definition optiga_trust_helpers.h:83
const uint8_t * final_fragment_data
Pointer to a buffer where final fragment data is stored.
Definition optiga_trust_helpers.h:91
const uint8_t * continue_fragment_data
Pointer to a buffer where continue fragment data is stored.
Definition optiga_trust_helpers.h:87
Definition optiga_trust_helpers.h:141
uint16_t used_size
Definition optiga_trust_helpers.h:146
uint8_t data_type
Definition optiga_trust_helpers.h:147
uint32_t payload_version
Definition optiga_trust_helpers.h:145
uint16_t manifest_anchor_oid
Definition optiga_trust_helpers.h:144
bool valid
Definition optiga_trust_helpers.h:143
uint16_t oid
Definition optiga_trust_helpers.h:142