mirror of
https://github.com/polhenarejos/pico-hsm.git
synced 2026-01-17 01:18:06 +00:00
102 lines
3.3 KiB
C
102 lines
3.3 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 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
|
|
* General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "sc_hsm.h"
|
|
#include "asn1.h"
|
|
|
|
extern void select_file(file_t *pe);
|
|
|
|
int cmd_update_ef() {
|
|
uint8_t p1 = P1(apdu), p2 = P2(apdu);
|
|
uint16_t fid = (p1 << 8) | p2;
|
|
uint8_t *data = NULL;
|
|
uint16_t offset = 0;
|
|
uint16_t data_len = 0;
|
|
file_t *ef = NULL;
|
|
if (!isUserAuthenticated) {
|
|
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
|
}
|
|
if (fid == 0x0) {
|
|
ef = currentEF;
|
|
}
|
|
/*
|
|
// This should not happen
|
|
else if (p1 != EE_CERTIFICATE_PREFIX && p1 != PRKD_PREFIX && p1 != CA_CERTIFICATE_PREFIX &&
|
|
p1 != CD_PREFIX && p1 != DATA_PREFIX && p1 != DCOD_PREFIX &&
|
|
p1 != PROT_DATA_PREFIX) {
|
|
return SW_INCORRECT_P1P2();
|
|
}
|
|
*/
|
|
|
|
if (ef && !authenticate_action(ef, ACL_OP_UPDATE_ERASE)) {
|
|
return SW_SECURITY_STATUS_NOT_SATISFIED();
|
|
}
|
|
|
|
uint16_t tag = 0x0;
|
|
uint8_t *tag_data = NULL, *p = NULL;
|
|
uint16_t tag_len = 0;
|
|
asn1_ctx_t ctxi;
|
|
asn1_ctx_init(apdu.data, (uint16_t)apdu.nc, &ctxi);
|
|
while (walk_tlv(&ctxi, &p, &tag, &tag_len, &tag_data)) {
|
|
if (tag == 0x54) { //ofset tag
|
|
for (size_t i = 1; i <= tag_len; i++) {
|
|
offset |= (*tag_data++ << (8 * (tag_len - i)));
|
|
}
|
|
}
|
|
else if (tag == 0x53) { //data
|
|
data_len = (uint16_t)tag_len;
|
|
data = tag_data;
|
|
}
|
|
}
|
|
if (data_len == 0 && offset == 0) { //new file
|
|
ef = file_new(fid);
|
|
//if ((fid & 0xff00) == (EE_CERTIFICATE_PREFIX << 8))
|
|
// add_file_to_chain(ef, &ef_pukdf);
|
|
select_file(ef);
|
|
}
|
|
else {
|
|
if (fid == 0x0 && !ef) {
|
|
return SW_FILE_NOT_FOUND();
|
|
}
|
|
else if (fid != 0x0 && !(ef = search_file(fid))) { //if does not exist, create it
|
|
//return SW_FILE_NOT_FOUND();
|
|
ef = file_new(fid);
|
|
}
|
|
if (offset == 0) {
|
|
int r = file_put_data(ef, data, data_len);
|
|
if (r != PICOKEY_OK) {
|
|
return SW_MEMORY_FAILURE();
|
|
}
|
|
}
|
|
else {
|
|
if (!file_has_data(ef)) {
|
|
return SW_DATA_INVALID();
|
|
}
|
|
|
|
uint8_t *data_merge = (uint8_t *) calloc(1, offset + data_len);
|
|
memcpy(data_merge, file_get_data(ef), offset);
|
|
memcpy(data_merge + offset, data, data_len);
|
|
int r = file_put_data(ef, data_merge, offset + data_len);
|
|
free(data_merge);
|
|
if (r != PICOKEY_OK) {
|
|
return SW_MEMORY_FAILURE();
|
|
}
|
|
}
|
|
low_flash_available();
|
|
}
|
|
return SW_OK();
|
|
}
|