Jens Wiklander | 2932647 | 2018-04-20 11:22:15 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* Copyright (c) 2018, Linaro Limited */ |
| 3 | |
| 4 | #include "xtest_test.h" |
| 5 | #include "xtest_helpers.h" |
| 6 | |
Jens Wiklander | 97d6e29 | 2018-04-23 13:00:31 +0200 | [diff] [blame^] | 7 | #include <compiler.h> |
| 8 | #include <stdarg.h> |
| 9 | #include <stdio.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
Jens Wiklander | 2932647 | 2018-04-20 11:22:15 +0200 | [diff] [blame] | 12 | #include <ta_crypt.h> |
| 13 | #include <tee_api_types.h> |
Jens Wiklander | 97d6e29 | 2018-04-23 13:00:31 +0200 | [diff] [blame^] | 14 | |
| 15 | #include "regression_8100_ca_crt.h" |
| 16 | #include "regression_8100_mid_crt.h" |
| 17 | #include "regression_8100_my_crt.h" |
| 18 | |
| 19 | #ifdef CFG_TA_MBEDTLS |
Jens Wiklander | 2932647 | 2018-04-20 11:22:15 +0200 | [diff] [blame] | 20 | |
| 21 | static void test_8101(ADBG_Case_t *c __maybe_unused) |
| 22 | { |
| 23 | #ifdef CFG_TA_MBEDTLS_SELF_TEST |
| 24 | TEEC_Session session = { 0 }; |
| 25 | uint32_t ret_orig; |
| 26 | |
| 27 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session( |
| 28 | &session, &crypt_user_ta_uuid, |
| 29 | NULL, &ret_orig))) |
| 30 | return; |
| 31 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 32 | TEEC_InvokeCommand(&session, TA_CRYPT_CMD_MBEDTLS_SELF_TESTS, |
| 33 | NULL, &ret_orig)); |
| 34 | TEEC_CloseSession(&session); |
| 35 | #else |
| 36 | Do_ADBG_Log("CFG_TA_MBEDTLS_SELF_TEST not set, test skipped"); |
| 37 | #endif |
| 38 | } |
| 39 | ADBG_CASE_DEFINE(regression, 8101, test_8101, "TA mbedTLS self tests"); |
Jens Wiklander | 97d6e29 | 2018-04-23 13:00:31 +0200 | [diff] [blame^] | 40 | |
| 41 | static int __printf(2, 3) myasprintf(char **strp, const char *fmt, ...) |
| 42 | { |
| 43 | char *str = NULL; |
| 44 | int rc = 0; |
| 45 | va_list ap; |
| 46 | |
| 47 | va_start(ap, fmt); |
| 48 | rc = vsnprintf(str, rc, fmt, ap); |
| 49 | if (rc <= 0) |
| 50 | goto out; |
| 51 | |
| 52 | str = malloc(rc); |
| 53 | if (!str) { |
| 54 | rc = -1; |
| 55 | goto out; |
| 56 | } |
| 57 | |
| 58 | rc = vsnprintf(str, rc, fmt, ap); |
| 59 | if (rc <= 0) |
| 60 | free(str); |
| 61 | else |
| 62 | *strp = str; |
| 63 | |
| 64 | out: |
| 65 | va_end(ap); |
| 66 | return rc; |
| 67 | } |
| 68 | |
| 69 | static void test_8102(ADBG_Case_t *c) |
| 70 | { |
| 71 | TEEC_Session session = { 0 }; |
| 72 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 73 | uint32_t ret_orig; |
| 74 | char *chain = NULL; |
| 75 | int clen = 0; |
| 76 | char *trust = NULL; |
| 77 | int tlen; |
| 78 | |
| 79 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session( |
| 80 | &session, &crypt_user_ta_uuid, |
| 81 | NULL, &ret_orig))) |
| 82 | return; |
| 83 | |
| 84 | clen = myasprintf(&chain, "%*s\n%*s", |
| 85 | (int)sizeof(regression_8100_my_crt), |
| 86 | regression_8100_my_crt, |
| 87 | (int)sizeof(regression_8100_mid_crt), |
| 88 | regression_8100_mid_crt); |
| 89 | if (!ADBG_EXPECT_COMPARE_SIGNED(c, clen, !=, -1)) |
| 90 | goto out; |
| 91 | tlen = myasprintf(&trust, "%*s", (int)sizeof(regression_8100_ca_crt), |
| 92 | regression_8100_ca_crt); |
| 93 | if (!ADBG_EXPECT_COMPARE_SIGNED(c, tlen, !=, -1)) |
| 94 | goto out; |
| 95 | |
| 96 | op.params[0].tmpref.buffer = chain; |
| 97 | op.params[0].tmpref.size = clen; |
| 98 | op.params[1].tmpref.buffer = trust; |
| 99 | op.params[1].tmpref.size = tlen; |
| 100 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 101 | TEEC_MEMREF_TEMP_INPUT, |
| 102 | TEEC_NONE, TEEC_NONE); |
| 103 | |
| 104 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 105 | TEEC_InvokeCommand(&session, TA_CRYPT_CMD_MBEDTLS_CHECK_CERT, |
| 106 | &op, &ret_orig)); |
| 107 | out: |
| 108 | free(chain); |
| 109 | free(trust); |
| 110 | TEEC_CloseSession(&session); |
| 111 | } |
| 112 | ADBG_CASE_DEFINE(regression, 8102, test_8102, "TA mbedTLS test cert chain"); |
| 113 | #endif /*CFG_TA_MBEDTLS*/ |