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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "util.h"
#include <fstream>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <prerror.h>
#if defined(__unix__) || defined(__APPLE__)
#include <termios.h>
#include <unistd.h>
#elif defined(WIN32) || defined(_WIN64)
#include <Windows.h>
#endif
static std::string GetPassword(const std::string &prompt) {
std::cout << prompt << std::endl;
#if defined(__unix__) || defined(__APPLE__)
termios oldt;
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
#elif defined(WIN32) || defined(_WIN64)
HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
DWORD mode = 0;
GetConsoleMode(hStdin, &mode);
SetConsoleMode(hStdin, mode & (~ENABLE_ECHO_INPUT));
#endif
std::string pw;
std::getline(std::cin, pw);
#if defined(__unix__) || defined(__APPLE__)
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
#elif defined(WIN32) || defined(_WIN64)
SetConsoleMode(hStdin, mode);
#endif
return pw;
}
static char *GetModulePassword(PK11SlotInfo *slot, int retry, void *arg) {
if (arg == nullptr) {
return nullptr;
}
PwData *pwData = reinterpret_cast<PwData *>(arg);
if (retry > 0) {
std::cerr << "Incorrect password/PIN entered." << std::endl;
return nullptr;
}
switch (pwData->source) {
case PW_NONE:
case PW_FROMFILE:
std::cerr << "Password input method not supported." << std::endl;
return nullptr;
case PW_PLAINTEXT:
return PL_strdup(pwData->data);
default:
break;
}
std::cerr << "Password check failed: No password found." << std::endl;
return nullptr;
}
static std::vector<uint8_t> ReadFromIstream(std::istream &is) {
std::vector<uint8_t> data;
while (is) {
char buf[1024];
is.read(buf, sizeof(buf));
data.insert(data.end(), buf, buf + is.gcount());
}
return data;
}
static std::string GetNewPasswordFromUser(void) {
std::string pw;
while (true) {
pw = GetPassword("Enter new password: ");
if (pw == GetPassword("Re-enter password: ")) {
break;
}
std::cerr << "Passwords do not match. Try again." << std::endl;
}
return pw;
}
bool InitSlotPassword(void) {
ScopedPK11SlotInfo slot(PK11_GetInternalKeySlot());
if (slot.get() == nullptr) {
std::cerr << "Error: Init PK11SlotInfo failed!" << std::endl;
return false;
}
std::cout << "Enter a password which will be used to encrypt your keys."
<< std::endl
<< std::endl;
std::string pw = GetNewPasswordFromUser();
SECStatus rv = PK11_InitPin(slot.get(), nullptr, pw.c_str());
if (rv != SECSuccess) {
std::cerr << "Init db password failed." << std::endl;
return false;
}
return true;
}
bool ChangeSlotPassword(void) {
ScopedPK11SlotInfo slot(PK11_GetInternalKeySlot());
if (slot.get() == nullptr) {
std::cerr << "Error: Init PK11SlotInfo failed!" << std::endl;
return false;
}
// get old password and authenticate to db
PK11_SetPasswordFunc(&GetModulePassword);
std::string oldPw = GetPassword("Enter your current password: ");
PwData pwData = {PW_PLAINTEXT, const_cast<char *>(oldPw.c_str())};
SECStatus rv = PK11_Authenticate(slot.get(), false /*loadCerts*/, &pwData);
if (rv != SECSuccess) {
std::cerr << "Password incorrect." << std::endl;
return false;
}
// get new password
std::string newPw = GetNewPasswordFromUser();
if (PK11_ChangePW(slot.get(), oldPw.c_str(), newPw.c_str()) != SECSuccess) {
std::cerr << "Failed to change password." << std::endl;
return false;
}
std::cout << "Password changed successfully." << std::endl;
return true;
}
bool DBLoginIfNeeded(const ScopedPK11SlotInfo &slot) {
if (!PK11_NeedLogin(slot.get())) {
return true;
}
PK11_SetPasswordFunc(&GetModulePassword);
std::string pw = GetPassword("Enter your password: ");
PwData pwData = {PW_PLAINTEXT, const_cast<char *>(pw.c_str())};
SECStatus rv = PK11_Authenticate(slot.get(), true /*loadCerts*/, &pwData);
if (rv != SECSuccess) {
std::cerr << "Could not authenticate to token "
<< PK11_GetTokenName(slot.get()) << ". Failed with error "
<< PR_ErrorToName(PR_GetError()) << std::endl;
return false;
}
std::cout << std::endl;
return true;
}
std::string StringToHex(const ScopedSECItem &input) {
std::stringstream ss;
ss << "0x";
for (size_t i = 0; i < input->len; i++) {
ss << std::hex << std::setfill('0') << std::setw(2)
<< static_cast<int>(input->data[i]);
}
return ss.str();
}
std::vector<uint8_t> ReadInputData(std::string dataPath) {
std::vector<uint8_t> data;
if (dataPath.empty()) {
std::cout << "No input file path given, using stdin." << std::endl;
data = ReadFromIstream(std::cin);
} else {
std::ifstream is(dataPath, std::ifstream::binary);
if (is.good()) {
data = ReadFromIstream(is);
} else {
std::cerr << "IO Error when opening " << dataPath << std::endl;
std::cerr << "Input file does not exist or you don't have permissions."
<< std::endl;
}
}
return data;
}
std::istream &GetStreamFromFileOrStdin(std::string &path, std::ifstream &ifs) {
if (path.empty()) {
return std::cin;
}
ifs.open(path, std::ifstream::binary);
if (!ifs.good()) {
std::cerr << "IO Error when opening " << path << std::endl;
std::cerr << "Input file does not exist or you don't have permissions."
<< std::endl;
}
return ifs;
}
|