LCOV - code coverage report
Current view: top level - os_stub/spdm_device_secret_lib_sample - set_cert.c (source / functions) Coverage Total Hit
Test: coverage.info Lines: 66.7 % 24 16
Test Date: 2025-09-14 08:11:04 Functions: 100.0 % 2 2

            Line data    Source code
       1              : /**
       2              :  *  Copyright Notice:
       3              :  *  Copyright 2024 DMTF. All rights reserved.
       4              :  *  License: BSD 3-Clause License. For full text see link: https://github.com/DMTF/libspdm/blob/main/LICENSE.md
       5              :  **/
       6              : 
       7              : #include <stdarg.h>
       8              : #include <stddef.h>
       9              : #include <setjmp.h>
      10              : #include <stdint.h>
      11              : #include <stdlib.h>
      12              : #include <stdio.h>
      13              : #include <assert.h>
      14              : #include <string.h>
      15              : 
      16              : #include <base.h>
      17              : #if defined(_WIN32) || (defined(__clang__) && (defined (LIBSPDM_CPU_AARCH64) || \
      18              :     defined(LIBSPDM_CPU_ARM)))
      19              : #else
      20              :     #include <fcntl.h>
      21              :     #include <unistd.h>
      22              :     #include <sys/stat.h>
      23              : #endif
      24              : #include "library/memlib.h"
      25              : #include "spdm_device_secret_lib_internal.h"
      26              : #include "internal/libspdm_common_lib.h"
      27              : 
      28              : bool g_in_trusted_environment = false;
      29              : bool g_set_cert_is_busy = false;
      30              : 
      31              : #if LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP
      32           12 : bool libspdm_is_in_trusted_environment(void *spdm_context)
      33              : {
      34           12 :     return g_in_trusted_environment;
      35              : }
      36              : 
      37            9 : bool libspdm_write_certificate_to_nvm(
      38              :     void *spdm_context,
      39              :     uint8_t slot_id, const void * cert_chain,
      40              :     size_t cert_chain_size,
      41              :     uint32_t base_hash_algo, uint32_t base_asym_algo, uint32_t pqc_asym_algo,
      42              :     bool *need_reset, bool *is_busy)
      43              : {
      44            9 :     if (g_set_cert_is_busy) {
      45            1 :         *is_busy = true;
      46              : 
      47            1 :         return false;
      48              :     } else {
      49              :     #if defined(_WIN32) || (defined(__clang__) && (defined (LIBSPDM_CPU_AARCH64) || \
      50              :         defined(LIBSPDM_CPU_ARM)))
      51              :         FILE *fp_out;
      52              :     #else
      53              :         int64_t fp_out;
      54              :     #endif
      55              : 
      56            8 :         char file_name[] = "slot_id_0_cert_chain.der";
      57              :         /*change the file name, for example: slot_id_1_cert_chain.der*/
      58            8 :         file_name[8] = (char)(slot_id+'0');
      59              : 
      60              :         /*check the input parameter*/
      61            8 :         if ((cert_chain == NULL) ^ (cert_chain_size == 0) ) {
      62            0 :             return false;
      63              :         }
      64              : 
      65              :     #if defined(_WIN32) || (defined(__clang__) && (defined (LIBSPDM_CPU_AARCH64) || \
      66              :         defined(LIBSPDM_CPU_ARM)))
      67              :         if ((fp_out = fopen(file_name, "w+b")) == NULL) {
      68              :             printf("Unable to open file %s\n", file_name);
      69              :             return false;
      70              :         }
      71              : 
      72              :         if (cert_chain != NULL) {
      73              :             if ((fwrite(cert_chain, 1, cert_chain_size, fp_out)) != cert_chain_size) {
      74              :                 printf("Write output file error %s\n", file_name);
      75              :                 fclose(fp_out);
      76              :                 return false;
      77              :             }
      78              :         }
      79              : 
      80              :         fclose(fp_out);
      81              :     #else
      82            8 :         if (cert_chain != NULL) {
      83            7 :             if ((fp_out = open(file_name, O_WRONLY | O_CREAT, S_IRWXU)) == -1) {
      84            0 :                 printf("Unable to open file %s\n", file_name);
      85            0 :                 return false;
      86              :             }
      87              : 
      88            7 :             if ((write(fp_out, cert_chain, cert_chain_size)) != cert_chain_size) {
      89            0 :                 printf("Write output file error %s\n", file_name);
      90            0 :                 close(fp_out);
      91            0 :                 return false;
      92              :             }
      93              :         } else {
      94            1 :             if ((fp_out = open(file_name, O_WRONLY | O_TRUNC)) == -1) {
      95            0 :                 printf("Unable to open file %s\n", file_name);
      96            0 :                 return false;
      97              :             }
      98              : 
      99            1 :             close(fp_out);
     100              :         }
     101              : 
     102            8 :         close(fp_out);
     103              :     #endif
     104              : 
     105            8 :         return true;
     106              :     }
     107              : }
     108              : #endif /* LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP */
        

Generated by: LCOV version 2.0-1