1 /*
2  * Copyright (c) 2017-2018 Nitrokey UG
3  *
4  * This file is part of Nitrokey App.
5  *
6  * Nitrokey App is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * any later version.
10  *
11  * Nitrokey App is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Nitrokey App. If not, see <http://www.gnu.org/licenses/>.
18  *
19  * SPDX-License-Identifier: GPL-3.0
20  */
21 
22 #ifndef NITROKEYAPP_LIBNITROKEY_ADAPTER_H
23 #define NITROKEYAPP_LIBNITROKEY_ADAPTER_H
24 
25 #include <memory>
26 #include <string>
27 #include "hotpslot.h"
28 #include <QString>
29 
30 #define HOTP_SLOT_COUNT_MAX 3
31 #define TOTP_SLOT_COUNT_MAX 15
32 #define HOTP_SLOT_COUNT 3
33 #define TOTP_SLOT_COUNT 15
34 
35 #define STICK10_PASSWORD_LEN 20
36 #define STICK20_PASSOWRD_LEN 20
37 #define CS20_MAX_UPDATE_PASSWORD_LEN 20
38 
39 #define PWS_SLOT_COUNT 16
40 #define PWS_SLOTNAME_LENGTH 11
41 #define PWS_PASSWORD_LENGTH 20
42 #define PWS_LOGINNAME_LENGTH 32
43 
44 #define DEBUG_STATUS_NO_DEBUGGING 0
45 #define DEBUG_STATUS_LOCAL_DEBUG 1
46 #define DEBUG_STATUS_DEBUG_ALL 2
47 
48 #define STICK20_CMD_START_VALUE 0x20
49 #define STICK20_CMD_FILL_SD_CARD_WITH_RANDOM_CHARS (STICK20_CMD_START_VALUE + 7)
50 
51 #define MAX_HIDDEN_VOLUME_PASSOWORD_SIZE 20
52 
53 #include <QObject>
54 #include <QCache>
55 
56 #include <libnitrokey/stick10_commands.h>
57 
58 #include <functional>
59 #include <QMutex>
60 
61 class NameCache {
62 private:
63   std::function<const char*(int)> getter;
64   QCache<int, std::string> cache;
65   QMutex mut;
66 public:
NameCache()67   NameCache(){};
68   NameCache(const std::function<const char *(int)> &getter);
69   ~NameCache();
70 
71   void setGetter(const std::function<const char *(int)> &getter);
72   std::string getName(const int i);
73   void remove(const int slot_no);
74   void clear();
75 };
76 
77 class libada : public QObject {
78     Q_OBJECT
79     Q_DISABLE_COPY(libada)
80   private:
81     static std::shared_ptr <libada> _instance;
82     NameCache cache_TOTP_name;
83     NameCache cache_HOTP_name;
84     NameCache cache_PWS_name;
85 
86     std::vector <uint8_t> status_PWS;
87     std::string cardSerial_cached;
88   static constexpr int invalid_value = 99;
89   std::atomic_int minor_firmware_version_cached {invalid_value} ;
90   std::atomic_int major_firmware_version_cached {invalid_value} ;
91   std::atomic_int secret320_supported_cached {invalid_value} ;
92 
93 
94 public slots:
95       void on_OTP_save(int slot_no, bool isHOTP);
96       void on_PWS_save(int slot_no);
97       void on_FactoryReset();
98       void on_DeviceDisconnect();
99 
100 signals:
101   void regenerateMenu();
102 
103 public:
104     explicit libada();
105     ~libada();
106     static std::shared_ptr<libada> i();
107 
108     int getMajorFirmwareVersion();
109     int getMinorFirmwareVersion();
110     int getAdminPasswordRetryCount();
111     int getUserPasswordRetryCount();
112     std::string getCardSerial();
113     std::string getTOTPSlotName(const int i);
114     std::string getHOTPSlotName(const int i);
115     std::string getTOTPCode(int slot_number, const char *user_temporary_password);
116     std::string getHOTPCode(int slot_number, const char *user_temporary_password);
117     int eraseHOTPSlot(const int i, const char *string);
118     int eraseTOTPSlot(const int i, const char *string);
119 
120     std::string getPWSSlotName(const int i);
121     bool getPWSSlotStatus(const int i);
122     void erasePWSSlot(const int i);
123 
124     uint8_t getStorageSDCardSizeGB();
125 
126     bool is_time_synchronized();
127     bool set_current_time();
128 
129     bool isDeviceConnected()  const throw();
130     bool isDeviceInitialized();
131     bool isStorageDeviceConnected() const throw();
132     bool isPasswordSafeAvailable();
133     bool isPasswordSafeUnlocked();
134     bool isTOTPSlotProgrammed(const int i);
135     bool isHOTPSlotProgrammed(const int i);
136     void writeToOTPSlot(const OTPSlot &otpconf, const char* tempPassword);
137 
138     bool is_nkpro_07_rtm1();
139     bool is_secret320_supported();
140 
141   std::string get_serial_number();
142 
143   nitrokey::proto::stick10::GetStatus::ResponsePayload get_status();
144   int get_status_no_except();
145 
146   bool have_communication_issues_occurred();
147 
148     void clearUserDataCache();
149 };
150 
151 
152 #endif //NITROKEYAPP_LIBNITROKEY_ADAPTER_H
153