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 11 : bool libspdm_is_in_trusted_environment(
33 : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
34 : void *spdm_context
35 : #endif
36 : )
37 : {
38 11 : return g_in_trusted_environment;
39 : }
40 :
41 8 : bool libspdm_write_certificate_to_nvm(
42 : #if LIBSPDM_HAL_PASS_SPDM_CONTEXT
43 : void *spdm_context,
44 : #endif
45 : uint8_t slot_id, const void * cert_chain,
46 : size_t cert_chain_size,
47 : uint32_t base_hash_algo, uint32_t base_asym_algo
48 : #if LIBSPDM_SET_CERT_CSR_PARAMS
49 : , bool *need_reset, bool *is_busy
50 : #endif /* LIBSPDM_SET_CERT_CSR_PARAMS */
51 : )
52 : {
53 : #if LIBSPDM_SET_CERT_CSR_PARAMS
54 : if (g_set_cert_is_busy) {
55 : *is_busy = true;
56 :
57 : return false;
58 : } else
59 : #endif /* LIBSPDM_SET_CERT_CSR_PARAMS */
60 : {
61 : #if defined(_WIN32) || (defined(__clang__) && (defined (LIBSPDM_CPU_AARCH64) || \
62 : defined(LIBSPDM_CPU_ARM)))
63 : FILE *fp_out;
64 : #else
65 : int64_t fp_out;
66 : #endif
67 :
68 8 : char file_name[] = "slot_id_0_cert_chain.der";
69 : /*change the file name, for example: slot_id_1_cert_chain.der*/
70 8 : file_name[8] = (char)(slot_id+'0');
71 :
72 : /*check the input parameter*/
73 8 : if ((cert_chain == NULL) ^ (cert_chain_size == 0) ) {
74 0 : return false;
75 : }
76 :
77 : #if defined(_WIN32) || (defined(__clang__) && (defined (LIBSPDM_CPU_AARCH64) || \
78 : defined(LIBSPDM_CPU_ARM)))
79 : if ((fp_out = fopen(file_name, "w+b")) == NULL) {
80 : printf("Unable to open file %s\n", file_name);
81 : return false;
82 : }
83 :
84 : if (cert_chain != NULL) {
85 : if ((fwrite(cert_chain, 1, cert_chain_size, fp_out)) != cert_chain_size) {
86 : printf("Write output file error %s\n", file_name);
87 : fclose(fp_out);
88 : return false;
89 : }
90 : }
91 :
92 : fclose(fp_out);
93 : #else
94 8 : if (cert_chain != NULL) {
95 7 : if ((fp_out = open(file_name, O_WRONLY | O_CREAT, S_IRWXU)) == -1) {
96 0 : printf("Unable to open file %s\n", file_name);
97 0 : return false;
98 : }
99 :
100 7 : if ((write(fp_out, cert_chain, cert_chain_size)) != cert_chain_size) {
101 0 : printf("Write output file error %s\n", file_name);
102 0 : close(fp_out);
103 0 : return false;
104 : }
105 : } else {
106 1 : if ((fp_out = open(file_name, O_WRONLY | O_TRUNC)) == -1) {
107 0 : printf("Unable to open file %s\n", file_name);
108 0 : return false;
109 : }
110 :
111 1 : close(fp_out);
112 : }
113 :
114 8 : close(fp_out);
115 : #endif
116 :
117 8 : return true;
118 : }
119 : }
120 : #endif /* LIBSPDM_ENABLE_CAPABILITY_SET_CERT_CAP */
|