aboutsummaryrefslogtreecommitdiff
path: root/NitrokeyManagerStorage.cpp
blob: 6814e8b67917f5e1be13239c6b6ea83657577f00 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include "NitrokeyManagerStorage.h"

//using namespace nitrokey;
namespace nitrokey{

using nitrokey::misc::strcpyT;

//storage commands

void NitrokeyManager::send_startup(uint64_t seconds_from_epoch){
  auto p = get_payload<stick20::SendStartup>();
//      p.set_defaults(); //set current time
  p.localtime = seconds_from_epoch;
  stick20::SendStartup::CommandTransaction::run(device, p);
}

void NitrokeyManager::unlock_encrypted_volume(const char* user_pin){
  misc::execute_password_command<stick20::EnableEncryptedPartition>(device, user_pin);
}

void NitrokeyManager::unlock_hidden_volume(const char* hidden_volume_password) {
  misc::execute_password_command<stick20::EnableHiddenEncryptedPartition>(device, hidden_volume_password);
}

void NitrokeyManager::set_encrypted_volume_read_only(const char* admin_pin) {
  misc::execute_password_command<stick20::SetEncryptedVolumeReadOnly>(device, admin_pin);
}

void NitrokeyManager::set_encrypted_volume_read_write(const char* admin_pin) {
  misc::execute_password_command<stick20::SetEncryptedVolumeReadWrite>(device, admin_pin);
}

//TODO check is encrypted volume unlocked before execution
//if not return library exception
void NitrokeyManager::create_hidden_volume(uint8_t slot_nr, uint8_t start_percent, uint8_t end_percent,
                                           const char *hidden_volume_password) {
  auto p = get_payload<stick20::SetupHiddenVolume>();
  p.SlotNr_u8 = slot_nr;
  p.StartBlockPercent_u8 = start_percent;
  p.EndBlockPercent_u8 = end_percent;
  strcpyT(p.HiddenVolumePassword_au8, hidden_volume_password);
  stick20::SetupHiddenVolume::CommandTransaction::run(device, p);
}

void NitrokeyManager::set_unencrypted_read_only_admin(const char* admin_pin) {
  //from v0.49, v0.52+ it needs Admin PIN
  if (set_unencrypted_volume_rorw_pin_type_user()){
    LOG("set_unencrypted_read_only_admin is not supported for this version of Storage device. "
        "Please update firmware to v0.52+. Doing nothing.", nitrokey::log::Loglevel::WARNING);
    return;
  }
  misc::execute_password_command<stick20::SetUnencryptedVolumeReadOnlyAdmin>(device, admin_pin);
}

void NitrokeyManager::set_unencrypted_read_only(const char *user_pin) {
  //until v0.48 (incl. v0.50 and v0.51) User PIN was sufficient
  LOG("set_unencrypted_read_only is deprecated. Use set_unencrypted_read_only_admin instead.",
      nitrokey::log::Loglevel::WARNING);
  if (!set_unencrypted_volume_rorw_pin_type_user()){
    LOG("set_unencrypted_read_only is not supported for this version of Storage device. Doing nothing.",
        nitrokey::log::Loglevel::WARNING);
    return;
  }
  misc::execute_password_command<stick20::SendSetReadonlyToUncryptedVolume>(device, user_pin);
}

void NitrokeyManager::set_unencrypted_read_write_admin(const char* admin_pin) {
  //from v0.49, v0.52+ it needs Admin PIN
  if (set_unencrypted_volume_rorw_pin_type_user()){
    LOG("set_unencrypted_read_write_admin is not supported for this version of Storage device. "
        "Please update firmware to v0.52+. Doing nothing.", nitrokey::log::Loglevel::WARNING);
    return;
  }
  misc::execute_password_command<stick20::SetUnencryptedVolumeReadWriteAdmin>(device, admin_pin);
}

void NitrokeyManager::set_unencrypted_read_write(const char *user_pin) {
  //until v0.48 (incl. v0.50 and v0.51) User PIN was sufficient
  LOG("set_unencrypted_read_write is deprecated. Use set_unencrypted_read_write_admin instead.",
      nitrokey::log::Loglevel::WARNING);
  if (!set_unencrypted_volume_rorw_pin_type_user()){
    LOG("set_unencrypted_read_write is not supported for this version of Storage device. Doing nothing.",
        nitrokey::log::Loglevel::WARNING);
    return;
  }
  misc::execute_password_command<stick20::SendSetReadwriteToUncryptedVolume>(device, user_pin);
}

bool NitrokeyManager::set_unencrypted_volume_rorw_pin_type_user(){
  auto minor_firmware_version = get_minor_firmware_version();
  return minor_firmware_version <= 48 || minor_firmware_version == 50 || minor_firmware_version == 51;
}

void NitrokeyManager::export_firmware(const char* admin_pin) {
  misc::execute_password_command<stick20::ExportFirmware>(device, admin_pin);
}

void NitrokeyManager::enable_firmware_update(const char* firmware_pin) {
  misc::execute_password_command<stick20::EnableFirmwareUpdate>(device, firmware_pin);
}

void NitrokeyManager::clear_new_sd_card_warning(const char* admin_pin) {
  misc::execute_password_command<stick20::SendClearNewSdCardFound>(device, admin_pin);
}

void NitrokeyManager::fill_SD_card_with_random_data(const char* admin_pin) {
  auto p = get_payload<stick20::FillSDCardWithRandomChars>();
  p.set_defaults();
  strcpyT(p.admin_pin, admin_pin);
  stick20::FillSDCardWithRandomChars::CommandTransaction::run(device, p);
}

void NitrokeyManager::change_update_password(const char* current_update_password, const char* new_update_password) {
  auto p = get_payload<stick20::ChangeUpdatePassword>();
  strcpyT(p.current_update_password, current_update_password);
  strcpyT(p.new_update_password, new_update_password);
  stick20::ChangeUpdatePassword::CommandTransaction::run(device, p);
}

char * NitrokeyManager::get_status_storage_as_string(){
  auto p = stick20::GetDeviceStatus::CommandTransaction::run(device);
  return strndup(p.data().dissect().c_str(), max_string_field_length);
}

stick20::DeviceConfigurationResponsePacket::ResponsePayload NitrokeyManager::get_status_storage(){
  auto p = stick20::GetDeviceStatus::CommandTransaction::run(device);
  return p.data();
}

char * NitrokeyManager::get_SD_usage_data_as_string(){
  auto p = stick20::GetSDCardOccupancy::CommandTransaction::run(device);
  return strndup(p.data().dissect().c_str(), max_string_field_length);
}

std::pair<uint8_t,uint8_t> NitrokeyManager::get_SD_usage_data(){
  auto p = stick20::GetSDCardOccupancy::CommandTransaction::run(device);
  return std::make_pair(p.data().WriteLevelMin, p.data().WriteLevelMax);
}

int NitrokeyManager::get_progress_bar_value(){
  try{
    stick20::GetDeviceStatus::CommandTransaction::run(device);
    return -1;
  }
  catch (LongOperationInProgressException &e){
    return e.progress_bar_value;
  }
}


void NitrokeyManager::lock_encrypted_volume() {
  misc::execute_password_command<stick20::DisableEncryptedPartition>(device, "");
}

void NitrokeyManager::lock_hidden_volume() {
  misc::execute_password_command<stick20::DisableHiddenEncryptedPartition>(device, "");
}

uint8_t NitrokeyManager::get_SD_card_size() {
  auto data = stick20::ProductionTest::CommandTransaction::run(device);
  return data.data().SD_Card_Size_u8;
}


void NitrokeyManager::wink(){
  stick20::Wink::CommandTransaction::run(device);
};

stick20::ProductionTest::ResponsePayload NitrokeyManager::production_info(){
  auto data = stick20::ProductionTest::CommandTransaction::run(device);
  return data.data();
};

}