stub out secure as its own feature

This commit is contained in:
zvecr 2022-04-11 00:43:18 +01:00
parent 2c068d08dd
commit 89fab427c4
14 changed files with 204 additions and 5 deletions

View file

@ -562,6 +562,14 @@ void quantum_task(void) {
#ifdef AUTO_SHIFT_ENABLE
autoshift_matrix_scan();
#endif
#ifdef SECURE_ENABLE
secure_task();
#endif
#ifdef XAP_ENABLE
xap_event_task();
#endif
}
/** \brief Keyboard task: Do keyboard routine jobs

View file

@ -0,0 +1,18 @@
// Copyright 2022 QMK
// SPDX-License-Identifier: GPL-2.0-or-later
#include "secure.h"
#include "process_secure.h"
bool process_secure(uint16_t keycode, keyrecord_t *record) {
if (secure_is_unlocking()) {
if (!record->event.pressed) {
secure_keypress_event(record->event.key.row, record->event.key.col);
}
// Normal keypresses should be disabled until the sequence is completed
return false;
}
return true;
}

View file

@ -0,0 +1,9 @@
// Copyright 2022 QMK
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <stdbool.h>
#include "action.h"
bool process_secure(uint16_t keycode, keyrecord_t *record);

View file

@ -245,6 +245,9 @@ bool process_record_quantum(keyrecord_t *record) {
#endif
#if defined(VIA_ENABLE)
process_record_via(keycode, record) &&
#endif
#if defined(SECURE_ENABLE)
process_secure(keycode, record) &&
#endif
process_record_kb(keycode, record) &&
#if defined(SEQUENCER_ENABLE)

View file

@ -196,6 +196,11 @@ extern layer_state_t layer_state;
# include "process_dynamic_macro.h"
#endif
#ifdef SECURE_ENABLE
# include "secure.h"
# include "process_secure.h"
#endif
#ifdef DYNAMIC_KEYMAP_ENABLE
# include "dynamic_keymap.h"
#endif

66
quantum/secure.c Normal file
View file

@ -0,0 +1,66 @@
// Copyright 2022 QMK
// SPDX-License-Identifier: GPL-2.0-or-later
#include "secure.h"
#include "timer.h"
#ifndef SECURE_UNLOCK_TIMEOUT
# define SECURE_UNLOCK_TIMEOUT 5000
#endif
#ifndef SECURE_IDLE_TIMEOUT
# define SECURE_IDLE_TIMEOUT 60000
#endif
secure_status_t secure_status = SECURE_LOCKED;
static uint32_t unlock_time = 0;
static uint32_t idle_time = 0;
secure_status_t secure_get_status(void) {
return secure_status;
}
bool secure_is_unlocking(void) {
return secure_status == SECURE_PENDING;
}
void secure_lock(void) {
secure_status = SECURE_LOCKED;
}
void secure_unlock(void) {
secure_status = SECURE_UNLOCKED;
idle_time = timer_read32();
}
void secure_request_unlock(void) {
if (secure_status == SECURE_LOCKED) {
secure_status = SECURE_PENDING;
unlock_time = timer_read32();
}
}
void secure_keypress_event(uint8_t row, uint8_t col) {
// TODO: check keypress is actually part of unlock sequence
secure_unlock();
}
void secure_task(void) {
#if SECURE_UNLOCK_TIMEOUT != 0
// handle unlock timeout
if (secure_status == SECURE_PENDING) {
if (timer_elapsed32(unlock_time) >= SECURE_UNLOCK_TIMEOUT) {
secure_lock();
}
}
#endif
#if SECURE_IDLE_TIMEOUT != 0
// handle idle timeout
if (secure_status == SECURE_UNLOCKED) {
if (timer_elapsed32(idle_time) >= SECURE_IDLE_TIMEOUT) {
secure_lock();
}
}
#endif
}

27
quantum/secure.h Normal file
View file

@ -0,0 +1,27 @@
// Copyright 2022 QMK
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
#include <stdint.h>
#include <stdbool.h>
typedef enum {
SECURE_LOCKED,
SECURE_PENDING,
SECURE_UNLOCKED,
} secure_status_t;
secure_status_t secure_get_status(void);
bool secure_is_unlocking(void);
void secure_lock(void);
void secure_unlock(void);
void secure_request_unlock(void);
void secure_keypress_event(uint8_t row, uint8_t col);
void secure_task(void);

View file

@ -16,6 +16,7 @@
#include <quantum.h>
#include <xap.h>
#include "secure.h"
#include "info_json_gz.h"
bool get_info_json_chunk(uint16_t offset, uint8_t *data, uint8_t data_len) {
@ -27,8 +28,6 @@ bool get_info_json_chunk(uint16_t offset, uint8_t *data, uint8_t data_len) {
return true;
}
uint8_t secure_status = 2;
#define QSTR2(z) #z
#define QSTR(z) QSTR2(z)
@ -86,11 +85,18 @@ void xap_execute_route(xap_token_t token, const xap_route_t *routes, size_t max_
xap_route_t route;
memcpy_P(&route, &routes[id], sizeof(xap_route_t));
if (route.flags.is_secure && secure_status != 2) {
if (route.flags.is_secure && secure_get_status() != SECURE_UNLOCKED) {
xap_respond_failure(token, XAP_RESPONSE_FLAG_SECURE_FAILURE);
return;
}
// TODO: All other subsystems are disabled during unlock.
// how to flag status route as still allowed?
// if (!route.flags.is_secure && secure_get_status() == SECURE_PENDING) {
// xap_respond_failure(token, XAP_RESPONSE_FLAG_UNLOCK_IN_PROGRESS);
// return;
// }
switch (route.flags.type) {
case XAP_ROUTE:
if (route.child_routes != NULL && route.child_routes_len > 0 && data_len > 0) {
@ -134,3 +140,13 @@ void xap_execute_route(xap_token_t token, const xap_route_t *routes, size_t max_
void xap_receive(xap_token_t token, const uint8_t *data, size_t length) {
xap_execute_route(token, xap_route_table, sizeof(xap_route_table) / sizeof(xap_route_t), data, length);
}
void xap_event_task(void) {
static secure_status_t last_status = -1;
secure_status_t status = secure_get_status();
if (last_status != status) {
last_status = status;
xap_broadcast_secure_status(status);
}
}

View file

@ -36,3 +36,5 @@ bool xap_respond_data_P(xap_token_t token, const void *data, size_t length);
void xap_send(xap_token_t token, xap_response_flags_t response_flags, const void *data, size_t length);
void xap_broadcast(uint8_t type, const void *data, size_t length);
void xap_event_task(void);

View file

@ -16,6 +16,7 @@
#include <quantum.h>
#include <xap.h>
#include "secure.h"
void xap_respond_success(xap_token_t token) {
xap_send(token, XAP_RESPONSE_FLAG_SUCCESS, NULL, 0);
@ -59,6 +60,21 @@ bool xap_respond_get_info_json_chunk(xap_token_t token, const void *data, size_t
return xap_respond_data(token, &ret, sizeof(ret));
}
bool xap_respond_secure_status(xap_token_t token, const void *data, size_t length) {
uint8_t ret = secure_get_status();
return xap_respond_data(token, &ret, sizeof(ret));
}
bool xap_respond_secure_unlock(xap_token_t token, const void *data, size_t length) {
secure_request_unlock();
return xap_respond_data(token, NULL, 0);
}
bool xap_respond_secure_lock(xap_token_t token, const void *data, size_t length) {
secure_lock();
return xap_respond_data(token, NULL, 0);
}
// TODO: how to set this if "custom" is just an empty stub
#ifndef BOOTLOADER_JUMP_SUPPORTED
# define BOOTLOADER_JUMP_SUPPORTED