hc
2024-05-10 23fa18eaa71266feff7ba8d83022d9e1cc83c65a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
// SPDX-License-Identifier: BSD-2-Clause
/* Copyright (c) 2018, Linaro Limited */
 
#include <mbedtls_taf.h>
#include <mbedtls/aes.h>
#include <mbedtls/base64.h>
#include <mbedtls/bignum.h>
#include <mbedtls/des.h>
#include <mbedtls/md5.h>
#include <mbedtls/rsa.h>
#include <mbedtls/sha1.h>
#include <mbedtls/sha256.h>
#include <mbedtls/x509_crt.h>
#include <mbedtls/x509_csr.h>
#include <mbedtls/x509.h>
#include <stdio.h>
#include <string.h>
#include <tee_internal_api.h>
 
/* From ca_crt.c */
extern const uint8_t ca_crt[];
extern const size_t ca_crt_size;
/* From mid_crt.c */
extern const uint8_t mid_crt[];
extern const size_t mid_crt_size;
/* From mid_key.c */
extern const uint8_t mid_key[];
extern const size_t mid_key_size;
 
TEE_Result
ta_entry_mbedtls_self_tests(uint32_t param_type,
               TEE_Param params[TEE_NUM_PARAMS] __unused)
{
   const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_NONE,
                       TEE_PARAM_TYPE_NONE,
                       TEE_PARAM_TYPE_NONE,
                       TEE_PARAM_TYPE_NONE);
 
   if (param_type != exp_pt)
       return TEE_ERROR_BAD_PARAMETERS;
 
#ifdef CFG_TA_MBEDTLS_SELF_TEST
#define DO_MBEDTLS_SELF_TEST(x) do { \
       if (mbedtls_##x##_self_test(1)) { \
           EMSG("mbedtls_%s_self_test: failed", #x); \
           return TEE_ERROR_GENERIC; \
       } \
   } while (0)
 
   DO_MBEDTLS_SELF_TEST(aes);
   DO_MBEDTLS_SELF_TEST(des);
   DO_MBEDTLS_SELF_TEST(md5);
   DO_MBEDTLS_SELF_TEST(sha1);
   DO_MBEDTLS_SELF_TEST(sha256);
   DO_MBEDTLS_SELF_TEST(base64);
   DO_MBEDTLS_SELF_TEST(mpi);
   DO_MBEDTLS_SELF_TEST(rsa);
   DO_MBEDTLS_SELF_TEST(x509);
 
   return TEE_SUCCESS;
#else
   return TEE_ERROR_NOT_IMPLEMENTED;
#endif
}
 
TEE_Result ta_entry_mbedtls_check_cert(uint32_t param_type,
                   TEE_Param params[TEE_NUM_PARAMS])
{
   TEE_Result res = TEE_SUCCESS;
   const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
                       TEE_PARAM_TYPE_MEMREF_INPUT,
                       TEE_PARAM_TYPE_NONE,
                       TEE_PARAM_TYPE_NONE);
   int ret = 0;
   uint32_t flags = 0;
   mbedtls_x509_crt crt = { };
   mbedtls_x509_crt trust_crt = { };
 
   if (param_type != exp_pt)
       return TEE_ERROR_BAD_PARAMETERS;
 
   mbedtls_x509_crt_init(&crt);
   mbedtls_x509_crt_init(&trust_crt);
 
   ret = mbedtls_x509_crt_parse(&crt, params[0].memref.buffer,
                    params[0].memref.size);
   if (ret) {
       EMSG("mbedtls_x509_crt_parse: failed: %#x", ret);
       return TEE_ERROR_BAD_FORMAT;
   }
 
   ret = mbedtls_x509_crt_parse(&trust_crt, params[1].memref.buffer,
                    params[1].memref.size);
   if (ret) {
       EMSG("mbedtls_x509_crt_parse: failed: %#x", ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
 
   ret = mbedtls_x509_crt_verify(&crt, &trust_crt, NULL, NULL, &flags,
                     NULL, NULL);
   if (ret) {
       EMSG("mbedtls_x509_crt_verify: failed: %#x", ret);
       res = TEE_ERROR_BAD_FORMAT;
 
   }
 
out:
   mbedtls_x509_crt_free(&trust_crt);
   mbedtls_x509_crt_free(&crt);
 
   return res;
}
 
static int f_rng(void *rng __unused, unsigned char *output, size_t output_len)
{
   TEE_GenerateRandom(output, output_len);
   return 0;
}
 
static TEE_Result write_cert(mbedtls_x509write_cert *crt, void *buf,
                size_t *blen)
{
   int ret = 0;
   void *b = NULL;
   size_t bl = 1024;
 
   ret = mbedtls_x509write_crt_pem(crt, buf, *blen, f_rng, NULL);
   if (!ret)
       return TEE_SUCCESS;
 
   if (ret != MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) {
       EMSG("mbedtls_x509write_crt_pem: failed: %#x", ret);
       return TEE_ERROR_GENERIC;
   }
 
   /*
    * We were called with a too small buffer, let's find out how
    * large it has to be.
    */
   while (true) {
       b = TEE_Malloc(bl, TEE_MALLOC_FILL_ZERO);
       if (!b)
           return TEE_ERROR_OUT_OF_MEMORY;
       ret = mbedtls_x509write_crt_pem(crt, b, bl, f_rng, NULL);
       if (!ret) {
           *blen = strlen(b) + 1;
           TEE_Free(b);
           return TEE_ERROR_SHORT_BUFFER;
       }
       TEE_Free(b);
       if (ret != MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL) {
           EMSG("mbedtls_x509write_crt_pem: failed: %#x", ret);
           return TEE_ERROR_GENERIC;
       }
       bl *= 2;
   }
}
 
static TEE_Result parse_issuer_cert(mbedtls_x509_crt *crt)
{
   int ret = 0;
   unsigned char *buf = NULL;
 
   buf = TEE_Malloc(mid_crt_size + 1, TEE_MALLOC_FILL_ZERO);
   if (!buf)
       return TEE_ERROR_OUT_OF_MEMORY;
 
   memcpy(buf, mid_crt, mid_crt_size);
   ret = mbedtls_x509_crt_parse(crt, buf, mid_crt_size + 1);
   TEE_Free(buf);
   if (ret) {
       EMSG("mbedtls_x509_crt_parse: failed: %#x", ret);
       return TEE_ERROR_BAD_FORMAT;
   }
 
   return TEE_SUCCESS;
}
 
static TEE_Result parse_issuer_key(mbedtls_pk_context *pk)
{
   int ret = 0;
   unsigned char *buf = NULL;
 
   buf = TEE_Malloc(mid_key_size + 1, TEE_MALLOC_FILL_ZERO);
   if (!buf)
       return TEE_ERROR_OUT_OF_MEMORY;
 
   memcpy(buf, mid_key, mid_key_size);
   ret = mbedtls_pk_parse_key(pk, buf, mid_key_size + 1, NULL, 0);
   TEE_Free(buf);
   if (ret) {
       EMSG("mbedtls_pk_parse_key: failed: %#x", ret);
       return TEE_ERROR_BAD_FORMAT;
   }
 
   return TEE_SUCCESS;
}
 
TEE_Result ta_entry_mbedtls_sign_cert(uint32_t param_type,
                     TEE_Param params[TEE_NUM_PARAMS])
{
   TEE_Result res = TEE_SUCCESS;
   const uint32_t exp_pt = TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_INPUT,
                       TEE_PARAM_TYPE_MEMREF_OUTPUT,
                       TEE_PARAM_TYPE_MEMREF_OUTPUT,
                       TEE_PARAM_TYPE_NONE);
   char name[256] = { 0 };
   mbedtls_mpi serial = { };
   mbedtls_x509_crt issuer_crt = { };
   mbedtls_pk_context issuer_key = { };
   mbedtls_x509write_cert crt = { };
   mbedtls_x509_csr csr = { };
   int ret = 0;
   size_t sz = 0;
 
   if (param_type != exp_pt)
       return TEE_ERROR_BAD_PARAMETERS;
 
   mbedtls_mpi_init(&serial);
   mbedtls_x509_crt_init(&issuer_crt);
   mbedtls_pk_init(&issuer_key);
   mbedtls_x509write_crt_init(&crt);
   mbedtls_x509_csr_init(&csr);
 
   ret = parse_issuer_cert(&issuer_crt);
   if (ret)
       goto out;
   ret = parse_issuer_key(&issuer_key);
   if (ret)
       goto out;
 
   ret = mbedtls_mpi_lset(&serial, 1);
   if (ret) {
       EMSG("mbedtls_mpi_read_string: failed: %#x", ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
 
   ret = mbedtls_x509_csr_parse(&csr, params[0].memref.buffer,
                    params[0].memref.size);
   if (ret) {
       EMSG("mbedtls_x509_csr_parse: failed: %#x", ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
 
   /* Extract and set subject name */
   ret = mbedtls_x509_dn_gets(name, sizeof(name), &csr.subject);
   if (ret < 0) {
       EMSG("mbedtls_x509_dn_gets: failed: %#x", ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
   ret = mbedtls_x509write_crt_set_subject_name(&crt, name);
   if (ret) {
       EMSG("mbedtls_x509write_crt_set_subject_name: failed: %#x",
            ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
 
   /* Extract and set issuer name */
   ret = mbedtls_x509_dn_gets(name, sizeof(name), &issuer_crt.subject);
   if (ret < 0) {
       EMSG("mbedtls_x509_dn_gets: failed: %#x", ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
   ret = mbedtls_x509write_crt_set_issuer_name(&crt, name);
   if (ret) {
       EMSG("mbedtls_x509write_crt_set_issuer_name: failed: %#x",
            ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
 
   mbedtls_x509write_crt_set_md_alg(&crt, csr.sig_md);
   mbedtls_x509write_crt_set_subject_key(&crt, &csr.pk);
   mbedtls_x509write_crt_set_issuer_key(&crt, &issuer_key);
 
   ret = mbedtls_x509write_crt_set_serial(&crt, &serial);
   if (ret) {
       EMSG("mbedtls_x509write_crt_set_serial: failed: %#x", ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
 
   ret = mbedtls_x509write_crt_set_validity(&crt, "19700101000000",
                        "20301231235959");
   if (ret) {
       EMSG("mbedtls_x509write_crt_set_validity: failed: %#x", ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
 
   ret = mbedtls_x509write_crt_set_basic_constraints(&crt, 0, 0);
   if (ret) {
       EMSG("mbedtls_x509write_crt_set_validity: failed: %#x", ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
 
   ret = mbedtls_x509write_crt_set_subject_key_identifier(&crt);
   if (ret) {
       EMSG("mbedtls_x509write_crt_set_subject_key_identifier: failed: %#x",
            ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
 
   ret = mbedtls_x509write_crt_set_authority_key_identifier(&crt);
   if (ret) {
       EMSG("mbedtls_x509write_crt_set_authority_key_identifier: failed: %#x",
            ret);
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
 
   sz = params[1].memref.size;
   res = write_cert(&crt, params[1].memref.buffer, &sz);
   if (res && res != TEE_ERROR_SHORT_BUFFER)
       goto out;
   params[1].memref.size = sz;
 
   ret = snprintf(params[2].memref.buffer, params[2].memref.size,
              "%*s\n%*s", (int)mid_crt_size, (char *)mid_crt,
              (int)ca_crt_size, (char *)ca_crt);
   if (ret < 0) {
       res = TEE_ERROR_BAD_FORMAT;
       goto out;
   }
   if ((size_t)ret >= params[2].memref.size)
       res = TEE_ERROR_SHORT_BUFFER;
   params[2].memref.size = ret + 1;
 
out:
   mbedtls_mpi_free(&serial);
   mbedtls_x509_crt_free(&issuer_crt);
   mbedtls_pk_free(&issuer_key);
   mbedtls_x509write_crt_free(&crt);
   mbedtls_x509_csr_free(&csr);
   return res;
}