14f8a354fSEtienne Carriere // SPDX-License-Identifier: BSD-2-Clause 24f8a354fSEtienne Carriere /* 34f8a354fSEtienne Carriere * Copyright (c) 2017-2020, Linaro Limited 44f8a354fSEtienne Carriere */ 54f8a354fSEtienne Carriere 64f8a354fSEtienne Carriere #include <pkcs11_ta.h> 74f8a354fSEtienne Carriere #include <stdbool.h> 84f8a354fSEtienne Carriere #include <stdint.h> 94f8a354fSEtienne Carriere #include <tee_internal_api.h> 104f8a354fSEtienne Carriere #include <trace.h> 114f8a354fSEtienne Carriere 12*3158faf6SEtienne Carriere #include "pkcs11_token.h" 134f8a354fSEtienne Carriere #include "serializer.h" 144f8a354fSEtienne Carriere 154f8a354fSEtienne Carriere /* 164f8a354fSEtienne Carriere * Util routines for serializes unformatted arguments in a client memref 174f8a354fSEtienne Carriere */ 184f8a354fSEtienne Carriere void serialargs_init(struct serialargs *args, void *in, size_t size) 194f8a354fSEtienne Carriere { 204f8a354fSEtienne Carriere args->start = in; 214f8a354fSEtienne Carriere args->next = in; 224f8a354fSEtienne Carriere args->size = size; 234f8a354fSEtienne Carriere } 244f8a354fSEtienne Carriere 254f8a354fSEtienne Carriere uint32_t serialargs_get(struct serialargs *args, void *out, size_t size) 264f8a354fSEtienne Carriere { 274f8a354fSEtienne Carriere if (args->next + size > args->start + args->size) { 284f8a354fSEtienne Carriere EMSG("arg too short: full %zd, remain %zd, expect %zd", 294f8a354fSEtienne Carriere args->size, args->size - (args->next - args->start), size); 304f8a354fSEtienne Carriere return PKCS11_CKR_ARGUMENTS_BAD; 314f8a354fSEtienne Carriere } 324f8a354fSEtienne Carriere 334f8a354fSEtienne Carriere TEE_MemMove(out, args->next, size); 344f8a354fSEtienne Carriere 354f8a354fSEtienne Carriere args->next += size; 364f8a354fSEtienne Carriere 374f8a354fSEtienne Carriere return PKCS11_CKR_OK; 384f8a354fSEtienne Carriere } 394f8a354fSEtienne Carriere 404f8a354fSEtienne Carriere uint32_t serialargs_alloc_and_get(struct serialargs *args, 414f8a354fSEtienne Carriere void **out, size_t size) 424f8a354fSEtienne Carriere { 434f8a354fSEtienne Carriere void *ptr = NULL; 444f8a354fSEtienne Carriere 454f8a354fSEtienne Carriere if (!size) { 464f8a354fSEtienne Carriere *out = NULL; 474f8a354fSEtienne Carriere return PKCS11_CKR_OK; 484f8a354fSEtienne Carriere } 494f8a354fSEtienne Carriere 504f8a354fSEtienne Carriere if (args->next + size > args->start + args->size) { 514f8a354fSEtienne Carriere EMSG("arg too short: full %zd, remain %zd, expect %zd", 524f8a354fSEtienne Carriere args->size, args->size - (args->next - args->start), size); 534f8a354fSEtienne Carriere return PKCS11_CKR_ARGUMENTS_BAD; 544f8a354fSEtienne Carriere } 554f8a354fSEtienne Carriere 564f8a354fSEtienne Carriere ptr = TEE_Malloc(size, TEE_MALLOC_FILL_ZERO); 574f8a354fSEtienne Carriere if (!ptr) 584f8a354fSEtienne Carriere return PKCS11_CKR_DEVICE_MEMORY; 594f8a354fSEtienne Carriere 604f8a354fSEtienne Carriere TEE_MemMove(ptr, args->next, size); 614f8a354fSEtienne Carriere 624f8a354fSEtienne Carriere args->next += size; 634f8a354fSEtienne Carriere *out = ptr; 644f8a354fSEtienne Carriere 654f8a354fSEtienne Carriere return PKCS11_CKR_OK; 664f8a354fSEtienne Carriere } 674f8a354fSEtienne Carriere 684f8a354fSEtienne Carriere uint32_t serialargs_get_ptr(struct serialargs *args, void **out, size_t size) 694f8a354fSEtienne Carriere { 704f8a354fSEtienne Carriere void *ptr = args->next; 714f8a354fSEtienne Carriere 724f8a354fSEtienne Carriere if (!size) { 734f8a354fSEtienne Carriere *out = NULL; 744f8a354fSEtienne Carriere return PKCS11_CKR_OK; 754f8a354fSEtienne Carriere } 764f8a354fSEtienne Carriere 774f8a354fSEtienne Carriere if (args->next + size > args->start + args->size) { 784f8a354fSEtienne Carriere EMSG("arg too short: full %zd, remain %zd, expect %zd", 794f8a354fSEtienne Carriere args->size, args->size - (args->next - args->start), size); 804f8a354fSEtienne Carriere return PKCS11_CKR_ARGUMENTS_BAD; 814f8a354fSEtienne Carriere } 824f8a354fSEtienne Carriere 834f8a354fSEtienne Carriere args->next += size; 844f8a354fSEtienne Carriere *out = ptr; 854f8a354fSEtienne Carriere 864f8a354fSEtienne Carriere return PKCS11_CKR_OK; 874f8a354fSEtienne Carriere } 884f8a354fSEtienne Carriere 894f8a354fSEtienne Carriere bool serialargs_remaining_bytes(struct serialargs *args) 904f8a354fSEtienne Carriere { 914f8a354fSEtienne Carriere return args->next < args->start + args->size; 924f8a354fSEtienne Carriere } 93*3158faf6SEtienne Carriere 94*3158faf6SEtienne Carriere enum pkcs11_rc serialargs_get_session_from_handle(struct serialargs *args, 95*3158faf6SEtienne Carriere struct pkcs11_client *client, 96*3158faf6SEtienne Carriere struct pkcs11_session **sess) 97*3158faf6SEtienne Carriere { 98*3158faf6SEtienne Carriere uint32_t rv = PKCS11_CKR_GENERAL_ERROR; 99*3158faf6SEtienne Carriere uint32_t session_handle = 0; 100*3158faf6SEtienne Carriere struct pkcs11_session *session = NULL; 101*3158faf6SEtienne Carriere 102*3158faf6SEtienne Carriere rv = serialargs_get(args, &session_handle, sizeof(uint32_t)); 103*3158faf6SEtienne Carriere if (rv) 104*3158faf6SEtienne Carriere return rv; 105*3158faf6SEtienne Carriere 106*3158faf6SEtienne Carriere session = pkcs11_handle2session(session_handle, client); 107*3158faf6SEtienne Carriere if (!session) 108*3158faf6SEtienne Carriere return PKCS11_CKR_SESSION_HANDLE_INVALID; 109*3158faf6SEtienne Carriere 110*3158faf6SEtienne Carriere *sess = session; 111*3158faf6SEtienne Carriere 112*3158faf6SEtienne Carriere return PKCS11_CKR_OK; 113*3158faf6SEtienne Carriere } 114