smartcard: store file data in spaceship tree

This commit is contained in:
Shiz 2020-04-13 04:54:26 +02:00
parent 863289b1a5
commit f7c6de4679
5 changed files with 41 additions and 18 deletions

View File

@ -18,6 +18,16 @@ static inline int is_authorized(struct nsl_smartcard_file *file, enum nsl_smartc
}
static inline char *file_data_offset(struct nsl_smartcard_file *file)
{
return (char *)file + sizeof(file);
}
static inline size_t file_data_capacity(struct nsl_smartcard_file *file)
{
return spaceship_get_size(&file->spaceship) - sizeof(file);
}
int nsl_smartcard_file_open(struct nsl_smartcard_file *file, struct nsl_smartcard_handle *handle)
{
if (!nsl_smartcard_is_file(file)) {
@ -68,7 +78,7 @@ int nsl_smartcard_handle_read(struct nsl_smartcard_handle *handle, uint8_t *data
size_t rlen = nsl_util_min(handle->file->contents.file.len - handle->offset, *len);
if (data) {
memcpy(data, handle->file->contents.file.data + handle->offset, rlen);
memcpy(data, file_data_offset(handle->file) + handle->offset, rlen);
}
*len = rlen;
return 0;
@ -80,9 +90,9 @@ int nsl_smartcard_handle_write(struct nsl_smartcard_handle *handle, const uint8_
return NSL_SMARTCARD_ERROR_NOT_FOUND;
}
size_t rlen = nsl_util_min(handle->file->contents.file.len - handle->offset, *len);
memcpy(handle->file->contents.file.data, data, rlen);
*len = rlen;
size_t rlen = nsl_util_min(file_data_capacity(handle->file) - handle->offset, *len);
memcpy(file_data_offset(handle->file) + handle->offset, data, rlen);
*len = handle->file->contents.file.len = rlen;
return 0;
}
@ -155,7 +165,6 @@ static int init_file(struct nsl_smartcard_file *file, nsl_smartcard_file_id_t i
return NSL_SMARTCARD_ERROR_WRONG_TYPE;
}
file->contents.file.data = NULL;
file->contents.file.len = 0;
return 0;
}
@ -174,16 +183,19 @@ int nsl_smartcard_create_root(void *mem, size_t size, struct nsl_smartcard_file
return 0;
}
int nsl_smartcard_dir_create(struct nsl_smartcard_file *file, nsl_smartcard_file_id_t id, struct nsl_smartcard_file **out)
int nsl_smartcard_dir_create(struct nsl_smartcard_file *file, size_t size, nsl_smartcard_file_id_t id, struct nsl_smartcard_file **out)
{
if (!nsl_smartcard_is_dir(file)) {
return NSL_SMARTCARD_ERROR_WRONG_TYPE;
}
if (size < sizeof(struct nsl_smartcard_file)) {
return NSL_SMARTCARD_ERROR_WRONG_VALUE;
}
if (!is_authorized(file, NSL_SMARTCARD_AUTH_DEV)) {
return NSL_SMARTCARD_ERROR_UNAUTHORIZED;
}
struct nsl_smartcard_file *dir = spaceship_alloc(&file->spaceship, 512);
struct nsl_smartcard_file *dir = spaceship_alloc(&file->spaceship, size);
if (!dir) {
return NSL_SMARTCARD_ERROR_NO_MEMORY;
}
@ -196,16 +208,19 @@ int nsl_smartcard_dir_create(struct nsl_smartcard_file *file, nsl_smartcard_file
return 0;
}
int nsl_smartcard_dir_create_file(struct nsl_smartcard_file *file, nsl_smartcard_file_id_t id, enum nsl_smartcard_file_type type, struct nsl_smartcard_file **out)
int nsl_smartcard_dir_create_file(struct nsl_smartcard_file *file, size_t size, nsl_smartcard_file_id_t id, enum nsl_smartcard_file_type type, struct nsl_smartcard_file **out)
{
if (!nsl_smartcard_is_dir(file)) {
return NSL_SMARTCARD_ERROR_WRONG_TYPE;
}
if (size < sizeof(struct nsl_smartcard_file)) {
return NSL_SMARTCARD_ERROR_WRONG_VALUE;
}
if (!is_authorized(file, NSL_SMARTCARD_AUTH_DEV)) {
return NSL_SMARTCARD_ERROR_UNAUTHORIZED;
}
struct nsl_smartcard_file *f = spaceship_alloc(&file->spaceship, sizeof(*f));
struct nsl_smartcard_file *f = spaceship_alloc(&file->spaceship, size);
if (!f) {
return NSL_SMARTCARD_ERROR_NO_MEMORY;
}

View File

@ -40,8 +40,7 @@ enum nsl_smartcard_file_flags
struct nsl_smartcard_file_contents
{
uint8_t *data;
size_t len;
size_t len;
};
struct nsl_smartcard_dir_contents
@ -113,8 +112,8 @@ int nsl_smartcard_handle_close(struct nsl_smartcard_handle *);
int nsl_smartcard_file_open(struct nsl_smartcard_file *, struct nsl_smartcard_handle *handle);
int nsl_smartcard_file_delete(struct nsl_smartcard_file *);
int nsl_smartcard_dir_create(struct nsl_smartcard_file *, nsl_smartcard_file_id_t id, struct nsl_smartcard_file **);
int nsl_smartcard_dir_create_file(struct nsl_smartcard_file *, nsl_smartcard_file_id_t id, enum nsl_smartcard_file_type type, struct nsl_smartcard_file **);
int nsl_smartcard_dir_create(struct nsl_smartcard_file *, size_t size, nsl_smartcard_file_id_t id, struct nsl_smartcard_file **);
int nsl_smartcard_dir_create_file(struct nsl_smartcard_file *, size_t size, nsl_smartcard_file_id_t id, enum nsl_smartcard_file_type type, struct nsl_smartcard_file **);
int nsl_smartcard_dir_delete(struct nsl_smartcard_file *);
int nsl_smartcard_dir_authorize(struct nsl_smartcard_file *, const uint8_t *pin, size_t len, enum nsl_smartcard_auth_level level);

View File

@ -18,7 +18,7 @@ int nsl_smartcard_init(struct nsl_smartcard *smartcard)
smartcard->curdir = NULL;
memset(smartcard->handles, 0, sizeof(smartcard->handles));
smartcard->last_handle = 0;
int err = nsl_smartcard_create_root(smartcard->file_system, sizeof(smartcard->file_system), &smartcard->rootdir);
int err = nsl_smartcard_create_root(smartcard->memory, sizeof(smartcard->memory), &smartcard->rootdir);
if (err) {
return err;
}
@ -54,7 +54,7 @@ int nsl_smartcard_change_pin(struct nsl_smartcard *smartcard, const uint8_t *pin
}
int nsl_smartcard_mkdir(struct nsl_smartcard *smartcard, nsl_smartcard_file_id_t id)
int nsl_smartcard_mkdir(struct nsl_smartcard *smartcard, nsl_smartcard_file_id_t id, size_t size)
{
if (!smartcard->curdir) {
return NSL_SMARTCARD_ERROR_NOT_FOUND;
@ -65,7 +65,7 @@ int nsl_smartcard_mkdir(struct nsl_smartcard *smartcard, nsl_smartcard_file_id_t
return NSL_SMARTCARD_ERROR_ALREADY_EXISTS;
}
return nsl_smartcard_dir_create(smartcard->curdir, id, NULL);
return nsl_smartcard_dir_create(smartcard->curdir, size, id, NULL);
}
int nsl_smartcard_chdir(struct nsl_smartcard *smartcard, nsl_smartcard_file_id_t id)

View File

@ -7,6 +7,7 @@
#define NSL_SMARTCARD_MAX_HANDLES 16
#define NSL_SMARTCARD_MEMORY_SIZE 8192
struct nsl_smartcard;
@ -22,7 +23,7 @@ struct nsl_smartcard {
nsl_smartcard_handler_t standard_handlers[0x100];
nsl_smartcard_handler_t proprietary_handlers[0x100];
uint8_t file_system[8192];
uint8_t memory[NSL_SMARTCARD_MEMORY_SIZE];
struct nsl_smartcard_handle handles[NSL_SMARTCARD_MAX_HANDLES];
uint8_t last_handle;
};
@ -32,7 +33,7 @@ int nsl_smartcard_reset(struct nsl_smartcard *smartcard);
int nsl_smartcard_authorize(struct nsl_smartcard *smartcard, const uint8_t *pin, size_t len, enum nsl_smartcard_auth_level level);
int nsl_smartcard_change_pin(struct nsl_smartcard *smartcard, const uint8_t *pin, size_t len, enum nsl_smartcard_auth_level level);
int nsl_smartcard_mkdir(struct nsl_smartcard *smartcard, nsl_smartcard_file_id_t id);
int nsl_smartcard_mkdir(struct nsl_smartcard *smartcard, nsl_smartcard_file_id_t id, size_t size);
int nsl_smartcard_chdir(struct nsl_smartcard *smartcard, nsl_smartcard_file_id_t id);
int nsl_smartcard_exec(struct nsl_smartcard *smartcard, nsl_smartcard_file_id_t id, const uint8_t *inbuf, size_t inlen, uint8_t *outbuf, size_t outlen);
int nsl_smartcard_open(struct nsl_smartcard *smartcard, nsl_smartcard_file_id_t id, struct nsl_smartcard_handle **handle);

View File

@ -43,6 +43,14 @@ void *spaceship_alloc(void *chunk, size_t size);
/* free previously-allocated bytes */
void spaceship_free(struct spaceship_chunk *chunk);
static inline size_t spaceship_get_size(struct spaceship_chunk *chunk)
{
if (!chunk || !(chunk->ssc_flags & SPACESHIP_FLAG_USED)) {
return 0;
}
return chunk->ssc_size;
}
/*
* tree features