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 | 950ea1c | 2018-04-30 15:39:22 +0200 | [diff] [blame] | 14 | #include <adbg.h> |
| 15 | |
| 16 | #ifdef OPENSSL_FOUND |
| 17 | #include <openssl/x509_vfy.h> |
| 18 | #include <openssl/pem.h> |
| 19 | #include <openssl/err.h> |
| 20 | #include <openssl/crypto.h> |
| 21 | #endif |
Jens Wiklander | 97d6e29 | 2018-04-23 13:00:31 +0200 | [diff] [blame] | 22 | |
| 23 | #include "regression_8100_ca_crt.h" |
| 24 | #include "regression_8100_mid_crt.h" |
| 25 | #include "regression_8100_my_crt.h" |
Jens Wiklander | 950ea1c | 2018-04-30 15:39:22 +0200 | [diff] [blame] | 26 | #include "regression_8100_my_csr.h" |
Jens Wiklander | 97d6e29 | 2018-04-23 13:00:31 +0200 | [diff] [blame] | 27 | |
| 28 | #ifdef CFG_TA_MBEDTLS |
Jens Wiklander | 2932647 | 2018-04-20 11:22:15 +0200 | [diff] [blame] | 29 | |
| 30 | static void test_8101(ADBG_Case_t *c __maybe_unused) |
| 31 | { |
| 32 | #ifdef CFG_TA_MBEDTLS_SELF_TEST |
| 33 | TEEC_Session session = { 0 }; |
| 34 | uint32_t ret_orig; |
| 35 | |
| 36 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session( |
| 37 | &session, &crypt_user_ta_uuid, |
| 38 | NULL, &ret_orig))) |
| 39 | return; |
| 40 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 41 | TEEC_InvokeCommand(&session, TA_CRYPT_CMD_MBEDTLS_SELF_TESTS, |
| 42 | NULL, &ret_orig)); |
| 43 | TEEC_CloseSession(&session); |
| 44 | #else |
| 45 | Do_ADBG_Log("CFG_TA_MBEDTLS_SELF_TEST not set, test skipped"); |
| 46 | #endif |
| 47 | } |
| 48 | ADBG_CASE_DEFINE(regression, 8101, test_8101, "TA mbedTLS self tests"); |
Jens Wiklander | 97d6e29 | 2018-04-23 13:00:31 +0200 | [diff] [blame] | 49 | |
| 50 | static int __printf(2, 3) myasprintf(char **strp, const char *fmt, ...) |
| 51 | { |
| 52 | char *str = NULL; |
| 53 | int rc = 0; |
| 54 | va_list ap; |
| 55 | |
| 56 | va_start(ap, fmt); |
| 57 | rc = vsnprintf(str, rc, fmt, ap); |
| 58 | if (rc <= 0) |
| 59 | goto out; |
| 60 | |
| 61 | str = malloc(rc); |
| 62 | if (!str) { |
| 63 | rc = -1; |
| 64 | goto out; |
| 65 | } |
| 66 | |
| 67 | rc = vsnprintf(str, rc, fmt, ap); |
| 68 | if (rc <= 0) |
| 69 | free(str); |
| 70 | else |
| 71 | *strp = str; |
| 72 | |
| 73 | out: |
| 74 | va_end(ap); |
| 75 | return rc; |
| 76 | } |
| 77 | |
| 78 | static void test_8102(ADBG_Case_t *c) |
| 79 | { |
| 80 | TEEC_Session session = { 0 }; |
| 81 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 82 | uint32_t ret_orig; |
| 83 | char *chain = NULL; |
| 84 | int clen = 0; |
| 85 | char *trust = NULL; |
| 86 | int tlen; |
| 87 | |
| 88 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session( |
| 89 | &session, &crypt_user_ta_uuid, |
| 90 | NULL, &ret_orig))) |
| 91 | return; |
| 92 | |
| 93 | clen = myasprintf(&chain, "%*s\n%*s", |
| 94 | (int)sizeof(regression_8100_my_crt), |
| 95 | regression_8100_my_crt, |
| 96 | (int)sizeof(regression_8100_mid_crt), |
| 97 | regression_8100_mid_crt); |
| 98 | if (!ADBG_EXPECT_COMPARE_SIGNED(c, clen, !=, -1)) |
| 99 | goto out; |
| 100 | tlen = myasprintf(&trust, "%*s", (int)sizeof(regression_8100_ca_crt), |
| 101 | regression_8100_ca_crt); |
| 102 | if (!ADBG_EXPECT_COMPARE_SIGNED(c, tlen, !=, -1)) |
| 103 | goto out; |
| 104 | |
| 105 | op.params[0].tmpref.buffer = chain; |
| 106 | op.params[0].tmpref.size = clen; |
| 107 | op.params[1].tmpref.buffer = trust; |
| 108 | op.params[1].tmpref.size = tlen; |
| 109 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 110 | TEEC_MEMREF_TEMP_INPUT, |
| 111 | TEEC_NONE, TEEC_NONE); |
| 112 | |
| 113 | ADBG_EXPECT_TEEC_SUCCESS(c, |
| 114 | TEEC_InvokeCommand(&session, TA_CRYPT_CMD_MBEDTLS_CHECK_CERT, |
| 115 | &op, &ret_orig)); |
| 116 | out: |
| 117 | free(chain); |
| 118 | free(trust); |
| 119 | TEEC_CloseSession(&session); |
| 120 | } |
| 121 | ADBG_CASE_DEFINE(regression, 8102, test_8102, "TA mbedTLS test cert chain"); |
Jens Wiklander | 950ea1c | 2018-04-30 15:39:22 +0200 | [diff] [blame] | 122 | |
| 123 | #ifdef OPENSSL_FOUND |
| 124 | static void osslerr(void) |
| 125 | { |
| 126 | while (true) { |
| 127 | unsigned long e; |
| 128 | char b[256]; |
| 129 | const char *f; |
| 130 | int l; |
| 131 | |
| 132 | e = ERR_get_error_line(&f, &l); |
| 133 | if (!e) |
| 134 | return; |
| 135 | ERR_error_string_n(e, b, sizeof(b)); |
| 136 | Do_ADBG_Log("%s:%d \"%s\"", f, l, b); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | static bool get_cert(ADBG_Case_t *c, const char *crt_str, X509 **crt) |
| 141 | { |
| 142 | bool ret = false; |
| 143 | size_t slen = strlen(crt_str) + 1; |
| 144 | BIO *buf = BIO_new(BIO_s_mem()); |
| 145 | size_t b; |
| 146 | |
| 147 | if (!ADBG_EXPECT_NOT_NULL(c, buf)) |
| 148 | goto out; |
| 149 | |
| 150 | b = BIO_write(buf, crt_str, slen); |
| 151 | if (!ADBG_EXPECT_COMPARE_UNSIGNED(c, b, ==, slen)) |
| 152 | goto out; |
| 153 | |
| 154 | if (!PEM_read_bio_X509(buf, crt, 0, NULL)) |
| 155 | goto out; |
| 156 | |
| 157 | ret = true; |
| 158 | out: |
| 159 | if (!ret) |
| 160 | osslerr(); |
| 161 | BIO_free(buf); |
| 162 | return ret; |
| 163 | } |
| 164 | |
| 165 | static bool push_cert(ADBG_Case_t *c, const char *crt_str, STACK_OF(X509) *cs) |
| 166 | { |
| 167 | X509 *crt = NULL; |
| 168 | int rc; |
| 169 | |
| 170 | if (!get_cert(c, crt_str, &crt)) |
| 171 | return false; |
| 172 | rc = sk_X509_push(cs, crt); |
| 173 | if (!ADBG_EXPECT_COMPARE_SIGNED(c, rc, >, 0)) { |
| 174 | osslerr(); |
| 175 | X509_free(crt); |
| 176 | return false; |
| 177 | } |
| 178 | |
| 179 | return true; |
| 180 | } |
| 181 | |
| 182 | static bool check(ADBG_Case_t *c, STACK_OF(X509) *trusted, |
| 183 | STACK_OF(X509) *untrusted, X509 *crt) |
| 184 | { |
| 185 | bool ret = false; |
| 186 | X509_STORE *store = NULL; |
| 187 | X509_STORE_CTX *csc = NULL; |
| 188 | X509_VERIFY_PARAM *pm = NULL; |
| 189 | int i; |
| 190 | time_t vfy_time; |
| 191 | |
| 192 | pm = X509_VERIFY_PARAM_new(); |
| 193 | vfy_time = 1526898005; /* Mon, 21 May 2018 10:20:05 +0000 */ |
| 194 | X509_VERIFY_PARAM_set_time(pm, vfy_time); |
| 195 | |
| 196 | store = X509_STORE_new(); |
| 197 | if (!ADBG_EXPECT_NOT_NULL(c, store)) |
| 198 | goto out; |
| 199 | X509_STORE_set_flags(store, 0); |
| 200 | if (!ADBG_EXPECT_TRUE(c, X509_STORE_set1_param(store, pm))) |
| 201 | goto out; |
| 202 | |
| 203 | csc = X509_STORE_CTX_new(); |
| 204 | if (!ADBG_EXPECT_NOT_NULL(c, csc)) |
| 205 | goto out; |
| 206 | |
| 207 | if (!ADBG_EXPECT_TRUE(c, X509_STORE_CTX_init(csc, store, crt, |
| 208 | untrusted))) |
| 209 | goto out; |
| 210 | X509_STORE_CTX_trusted_stack(csc, trusted); |
| 211 | |
| 212 | i = X509_verify_cert(csc); |
| 213 | if (!ADBG_EXPECT_COMPARE_SIGNED(c, i, >, 0)) |
| 214 | goto out; |
| 215 | i = X509_STORE_CTX_get_error(csc); |
| 216 | if (!ADBG_EXPECT_COMPARE_SIGNED(c, i, ==, X509_V_OK)) |
| 217 | goto out; |
| 218 | ret = true; |
| 219 | out: |
| 220 | if (!ret) |
| 221 | osslerr(); |
| 222 | X509_VERIFY_PARAM_free(pm); |
| 223 | X509_STORE_free(store); |
| 224 | X509_STORE_CTX_free(csc); |
| 225 | return ret; |
| 226 | } |
| 227 | |
| 228 | static bool verify_cert(ADBG_Case_t *c, const char *ca, const char *mid, |
| 229 | const char *cert) |
| 230 | { |
| 231 | bool ret = false; |
| 232 | STACK_OF(X509) *trusted = NULL; |
| 233 | STACK_OF(X509) *untrusted = NULL; |
| 234 | X509 *crt = NULL; |
| 235 | |
| 236 | trusted = sk_X509_new_null(); |
| 237 | if (!ADBG_EXPECT_NOT_NULL(c, trusted)) |
| 238 | goto out; |
| 239 | untrusted = sk_X509_new_null(); |
| 240 | if (!ADBG_EXPECT_NOT_NULL(c, untrusted)) |
| 241 | goto out; |
| 242 | |
| 243 | if (!ADBG_EXPECT_TRUE(c, get_cert(c, cert, &crt))) |
| 244 | goto out; |
| 245 | if (!ADBG_EXPECT_TRUE(c, push_cert(c, mid, untrusted))) |
| 246 | goto out; |
| 247 | if (!ADBG_EXPECT_TRUE(c, push_cert(c, ca, trusted))) |
| 248 | goto out; |
| 249 | |
| 250 | ret = ADBG_EXPECT_TRUE(c, check(c, trusted, untrusted, crt)); |
| 251 | out: |
| 252 | if (!ret) |
| 253 | osslerr(); |
| 254 | X509_free(crt); |
| 255 | sk_X509_pop_free(untrusted, X509_free); |
| 256 | sk_X509_pop_free(trusted, X509_free); |
| 257 | return ret; |
| 258 | } |
| 259 | #else /*!OPENSSL_FOUND*/ |
| 260 | static bool verify_cert(ADBG_Case_t *c __unused, const char *ca __unused, |
| 261 | const char *mid __unused, const char *cert __unused) |
| 262 | { |
| 263 | Do_ADBG_Log("OpenSSL not available, skipping certificate verification"); |
| 264 | return true; |
| 265 | } |
| 266 | #endif |
| 267 | |
| 268 | static void test_8103(ADBG_Case_t *c) |
| 269 | { |
| 270 | TEEC_Result res; |
| 271 | TEEC_Session session = { 0 }; |
| 272 | TEEC_Operation op = TEEC_OPERATION_INITIALIZER; |
| 273 | uint32_t ret_orig; |
| 274 | char *csr = NULL; |
| 275 | int clen = 0; |
| 276 | char cert[2048]; |
| 277 | char chain[4096]; |
| 278 | char *ca = NULL; |
| 279 | |
| 280 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, xtest_teec_open_session( |
| 281 | &session, &crypt_user_ta_uuid, |
| 282 | NULL, &ret_orig))) |
| 283 | return; |
| 284 | |
| 285 | clen = myasprintf(&csr, "%*s", (int)sizeof(regression_8100_my_csr), |
| 286 | regression_8100_my_csr); |
| 287 | if (!ADBG_EXPECT_COMPARE_SIGNED(c, clen, >=, 0)) |
| 288 | goto out; |
| 289 | op.params[0].tmpref.buffer = csr; |
| 290 | op.params[0].tmpref.size = clen; |
| 291 | op.params[1].tmpref.buffer = cert; |
| 292 | op.params[1].tmpref.size = sizeof(cert); |
| 293 | op.params[2].tmpref.buffer = chain; |
| 294 | op.params[2].tmpref.size = sizeof(chain); |
| 295 | op.paramTypes = TEEC_PARAM_TYPES(TEEC_MEMREF_TEMP_INPUT, |
| 296 | TEEC_MEMREF_TEMP_OUTPUT, |
| 297 | TEEC_MEMREF_TEMP_OUTPUT, TEEC_NONE); |
| 298 | res = TEEC_InvokeCommand(&session, TA_CRYPT_CMD_MBEDTLS_SIGN_CERT, &op, |
| 299 | &ret_orig); |
| 300 | if (!ADBG_EXPECT_TEEC_SUCCESS(c, res)) |
| 301 | goto out; |
| 302 | |
| 303 | myasprintf(&ca, "%*s", (int)sizeof(regression_8100_ca_crt), |
| 304 | regression_8100_ca_crt); |
| 305 | if (!ADBG_EXPECT_NOT_NULL(c, ca)) |
| 306 | goto out; |
| 307 | verify_cert(c, ca, op.params[2].tmpref.buffer, |
| 308 | op.params[1].tmpref.buffer); |
| 309 | out: |
| 310 | free(ca); |
| 311 | free(csr); |
| 312 | TEEC_CloseSession(&session); |
| 313 | } |
| 314 | ADBG_CASE_DEFINE(regression, 8103, test_8103, |
| 315 | "TA mbedTLS process certificate request"); |
Jens Wiklander | 97d6e29 | 2018-04-23 13:00:31 +0200 | [diff] [blame] | 316 | #endif /*CFG_TA_MBEDTLS*/ |