mirror of
https://github.com/polhenarejos/pico-hsm.git
synced 2026-04-17 13:48:27 +00:00
116 lines
3.6 KiB
C
116 lines
3.6 KiB
C
/*
|
|
* This file is part of the Pico HSM distribution (https://github.com/polhenarejos/pico-hsm).
|
|
* Copyright (c) 2022 Pol Henarejos.
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
* the Free Software Foundation, version 3.
|
|
*
|
|
* This program is distributed in the hope that it will be useful, but
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Affero General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "sc_hsm.h"
|
|
|
|
typedef int (*file_data_handler_t)(const file_t *f, int mode);
|
|
|
|
int cmd_read_binary(void) {
|
|
uint16_t offset = 0;
|
|
uint8_t ins = INS(apdu), p1 = P1(apdu), p2 = P2(apdu);
|
|
file_t *ef = NULL;
|
|
|
|
if ((ins & 0x1) == 0) {
|
|
if ((p1 & 0x80) != 0) {
|
|
if (!(ef = search_file(p1 & 0x1f))) {
|
|
return SW_FILE_NOT_FOUND();
|
|
}
|
|
offset = p2;
|
|
}
|
|
else {
|
|
offset = make_uint16_t_be(p1, p2) & 0x7fff;
|
|
ef = currentEF;
|
|
}
|
|
}
|
|
else {
|
|
if (p1 == 0 && (p2 & 0xE0) == 0 && (p2 & 0x1f) != 0 && (p2 & 0x1f) != 0x1f) {
|
|
if (!(ef = search_file(p2 & 0x1f))) {
|
|
return SW_FILE_NOT_FOUND();
|
|
}
|
|
}
|
|
else {
|
|
uint16_t file_id = make_uint16_t_be(p1, p2); // & 0x7fff;
|
|
if (file_id == 0x0) {
|
|
ef = currentEF;
|
|
}
|
|
else if (!(ef = search_file(file_id))) {
|
|
return SW_FILE_NOT_FOUND();
|
|
}
|
|
|
|
if (apdu.data[0] != 0x54) {
|
|
return SW_WRONG_DATA();
|
|
}
|
|
|
|
offset = 0;
|
|
for (int d = 0; d < apdu.data[1]; d++) {
|
|
offset |= apdu.data[2 + d] << (apdu.data[1] - 1 - d) * 8;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (ef == NULL) {
|
|
return SW_FILE_NOT_FOUND();
|
|
}
|
|
|
|
if (offset > 0x7fff) {
|
|
return SW_WRONG_P1P2();
|
|
}
|
|
|
|
if ((ef->fid >> 8) == PROT_DATA_PREFIX) {
|
|
memset(ef->acl, 0x90, sizeof(ef->acl)); //force PIN for protected data objects
|
|
}
|
|
|
|
if ((ef->fid >> 8) == KEY_PREFIX || !authenticate_action(ef, ACL_OP_READ_SEARCH)) {
|
|
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
|
}
|
|
if (ef->data) {
|
|
if ((ef->type & FILE_DATA_FUNC) == FILE_DATA_FUNC) {
|
|
union {
|
|
uint8_t *data;
|
|
file_data_handler_t handler;
|
|
} data_func = { .data = ef->data };
|
|
uint16_t data_len = (uint16_t)data_func.handler((const file_t *) ef, 1); //already copies content to res_APDU
|
|
if (offset > data_len) {
|
|
return SW_WRONG_P1P2();
|
|
}
|
|
uint16_t maxle = data_len - offset;
|
|
if (apdu.ne > maxle) {
|
|
apdu.ne = maxle;
|
|
}
|
|
if (offset) {
|
|
memmove(res_APDU, res_APDU + offset, res_APDU_size - offset);
|
|
//res_APDU += offset;
|
|
res_APDU_size -= offset;
|
|
}
|
|
}
|
|
else {
|
|
uint16_t data_len = file_get_size(ef);
|
|
if (offset > data_len) {
|
|
return SW_WARNING_EOF();
|
|
}
|
|
|
|
//uint16_t maxle = data_len - offset;
|
|
//if (apdu.ne > maxle) {
|
|
// apdu.ne = maxle;
|
|
//}
|
|
memcpy(res_APDU, file_get_data(ef) + offset, data_len - offset);
|
|
res_APDU_size = data_len - offset;
|
|
}
|
|
}
|
|
|
|
return SW_OK();
|
|
}
|