The example code below illustrates the use of GTA API to implement the scenario outlined in section 5.5.2, Figure 21. Note that functionality expected to be provided by the application is not elaborated but only addressed in comments. Error handling is omitted for the sake of clarity of the workflow.
The following pre-conditions apply:
- OPC UA application installed on device
- The OPC UA application has been successfully onboarded (cf. 5.4)
- The OPC UA security policy used in OpenSecureChannel Response is Aes256-Sha256-RsaPss
- The GTA API profile used in the example is org.opcfoundation.Aes256-Sha256-RsaPss
- The name of the attribute used to store the Application Instance Certificate is my_server
- The GTA API personality name used in the example is urn:manufacturer.com:2024-10:myproduct:myserver_appid?cg=DefaultApplicationGroup&ct=Rsa2048&ix=1
- gta_errinfo_t errinfo = 0;
- char personality_name[]
- = "urn:manufacturer.com:2024-10:myproduct:myserver_appid?"
- "cg=DefaultApplicationGroup&ct=Rsa2048&ix=1";
- char profile[]
- = "org.opcfoundation.Aes256-Sha256-RsaPss";
- char certificate_name = "my_server";
- char trustlist_personality_name[]
- = "urn:manufacturer.com:2024-10:myproduct:myserver_appid?"
- "cg=DefaultApplicationGroup;
- char trustlist_profile[]
- = "ch.iec.30168.basic.local_data_integrity_only";
- gta_context_handle_t h_ctx = GTA_HANDLE_INVALID;
- h_ctx = gta_context_open(
- h_inst,
- personality_name,
- profile,
- &errinfo);
- /* Get server certificate from personality attribute (optional) */
- char cert_buffer[CERT_SIZE];
- myio_obufstream_t ostream_cert = { 0 };
- gta_personality_get_attribute(
- h_ctx,
- certificate_name,
- (gtaio_ostream_t*)&ostream_cert,
- &errinfo);
- myio_close_obufstream(&ostream_cert, &errinfo);
- /* Prepare OpenSecureChannel Response */
- gta_context_handle_t h_ctx_seal = GTA_HANDLE_INVALID;
- h_ctx_seal = gta_context_open(
- h_inst,
- trustlist_personality_name,
- trustlist_profile,
- &errinfo);
- char trustlist_buffer[] = {…};
- size_t trustlist_len = …;
- myio_ibufstream_t istream_trustlist = { 0 };
- myio_open_ibufstream(
- &istream_trustlist,
- trustlist_buffer, trustlist_len,
- &errinfo);
- char seal_buffer[] = {…};
- size_t seal_len = …;
- myio_ibufstream_t istream_seal = { 0 };
- myio_open_ibufstream(
- &istream_seal,
- seal_buffer, seal_len,
- &errinfo);
- if (!gta_verify_data_detached(
- h_ctx_seal,
- (gtaio_istream_t*)&istream_trustlist,
- (gtaio_istream_t*)&istream_seal,
- &errinfo)) {
- /* fail – TrustList has been tampered with */
- }
- myio_close_ibufstream(&istream_trustlist, &errinfo);
- myio_close_ibufstream(&istream_seal, &errinfo);
- gta_context_close(h_ctx_seal, &errinfo);
- char encrypted_requestdata_buffer[] = {…};
- size_t encrypted_requestdata_len = …;
- myio_ibufstream_t istream_encrypted_requestdata = { 0 };
- myio_open_ibufstream(
- &istream_encrypted_requestdata,
- encrypted_requestdata_buffer, encrypted_requestdata_len,
- &errinfo);
- #define REQUESTDATA_SIZE
- char decrypted_requestdata_buffer[REQUESTDATA_SIZE];
- myio_obufstream_t ostream_decrypted_requestdata = { 0 };
- myio_open_obufstream(
- &ostream_decrypted_requestdata,
- decrypted_requestdata_buffer, sizeof(decrypted_requestdata_buffer),
- &errinfo);
- gta_unseal_data(
- h_ctx,
- (gtaio_istream_t*)&istream_encrypted_requestdata,
- (gtaio_ostream_t*)&ostream_decrypted_requestdata,
- &errinfo);
- myio_close_ibufstream(&istream_encrypted_requestdata, &errinfo);
- myio_close_obufstream(&ostream_decrypted_requestdata, &errinfo);
- char tbs_responsedata_buffer[] = {…};
- size_t tbs_responsedata_len = …;
- myio_ibufstream_t istream_tbs_responsedata = { 0 };
- myio_open_ibufstream(
- &istream_tbs_responsedata,
- tbs_responsedata_buffer, tbs_responsedata_len,
- &errinfo);
- #define SIGNATURE_SIZE
- char signature_buffer[SIGNATURE_SIZE];
- myio_obufstream_t ostream_signature = { 0 };
- myio_open_obufstream(
- &ostream_signature,
- signature_buffer, sizeof(signature_buffer),
- &errinfo);
- gta_authenticate_data_detached(
- h_ctx,
- (gtaio_istream_t*)&istream_tbs_responsedata,
- (gtaio_ostream_t*)&ostream_signature,
- &errinfo);
- myio_close_ibufstream(&istream_tbs_responsedata, &errinfo);
- myio_close_obufstream(&ostream_signature, &errinfo);
- gta_context_close(h_ctx, &errinfo);