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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
|
/**
* Copyright (C) 2021 Saturneric <[email protected]>
*
* This file is part of GpgFrontend.
*
* GpgFrontend 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, either version 3 of the License, or
* (at your option) any later version.
*
* GpgFrontend 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 GpgFrontend. If not, see <https://www.gnu.org/licenses/>.
*
* The initial version of the source code is inherited from
* the gpg4usb project, which is under GPL-3.0-or-later.
*
* All the source code of GpgFrontend was modified and released by
* Saturneric <[email protected]> starting on May 12, 2021.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*/
#include "GpgGenKeyInfo.h"
#include <algorithm>
#include <boost/date_time/gregorian/greg_date.hpp>
#include <boost/date_time/gregorian/greg_duration.hpp>
#include <boost/date_time/gregorian/gregorian_types.hpp>
#include <cassert>
void GpgFrontend::GenKeyInfo::SetAlgo(
const GpgFrontend::GenKeyInfo::KeyGenAlgo &m_algo) {
SPDLOG_DEBUG("set algo name: {}", m_algo.first);
// Check algo if supported
std::string algo_args = m_algo.second;
if (standalone_) {
if (!subkey_) {
auto support_algo = GetSupportedKeyAlgoStandalone();
auto it = std::find_if(
support_algo.begin(), support_algo.end(),
[=](const KeyGenAlgo &o) { return o.second == algo_args; });
// Algo Not Supported
if (it == support_algo.end()) return;
} else {
auto support_algo = GetSupportedSubkeyAlgoStandalone();
auto it = std::find_if(
support_algo.begin(), support_algo.end(),
[=](const KeyGenAlgo &o) { return o.second == algo_args; });
// Algo Not Supported
if (it == support_algo.end()) return;
}
} else {
if (!subkey_) {
auto support_algo = GetSupportedKeyAlgo();
auto it = std::find_if(
support_algo.begin(), support_algo.end(),
[=](const KeyGenAlgo &o) { return o.second == algo_args; });
// Algo Not Supported
if (it == support_algo.end()) return;
} else {
auto support_algo = GetSupportedSubkeyAlgo();
auto it = std::find_if(
support_algo.begin(), support_algo.end(),
[=](const KeyGenAlgo &o) { return o.second == algo_args; });
// Algo Not Supported
if (it == support_algo.end()) return;
}
}
// reset all options
reset_options();
if (!this->subkey_) {
this->SetAllowCertification(true);
} else {
this->SetAllowCertification(false);
}
this->allow_change_certification_ = false;
if (!standalone_) boost::algorithm::to_lower(algo_args);
if (algo_args == "rsa") {
/**
* RSA is the world’s premier asymmetric cryptographic algorithm,
* and is built on the difficulty of factoring extremely large composites.
* GnuPG supports RSA with key sizes of between 1024 and 4096 bits.
*/
suggest_min_key_size_ = 1024;
suggest_max_key_size_ = 4096;
suggest_size_addition_step_ = 1024;
SetKeyLength(2048);
} else if (algo_args == "dsa") {
/**
* Algorithm (DSA) as a government standard for digital signatures.
* Originally, it supported key lengths between 512 and 1024 bits.
* Recently, NIST has declared 512-bit keys obsolete:
* now, DSA is available in 1024, 2048 and 3072-bit lengths.
*/
SetAllowEncryption(false);
allow_change_encryption_ = false;
suggest_min_key_size_ = 1024;
suggest_max_key_size_ = 3072;
suggest_size_addition_step_ = 1024;
SetKeyLength(2048);
} else if (algo_args == "ed25519") {
/**
* GnuPG supports the Elgamal asymmetric encryption algorithm in key lengths
* ranging from 1024 to 4096 bits.
*/
SetAllowEncryption(false);
allow_change_encryption_ = false;
suggest_min_key_size_ = -1;
suggest_max_key_size_ = -1;
suggest_size_addition_step_ = -1;
SetKeyLength(-1);
} else if (algo_args == "cv25519") {
SetAllowAuthentication(false);
allow_change_authentication_ = false;
SetAllowSigning(false);
allow_change_signing_ = false;
SetAllowCertification(false);
allow_change_certification_ = false;
suggest_min_key_size_ = 1024;
suggest_max_key_size_ = 4096;
suggest_size_addition_step_ = 1024;
SetKeyLength(2048);
} else if (algo_args == "nistp256" || algo_args == "nistp384" ||
algo_args == "nistp521") {
SetAllowAuthentication(false);
allow_change_authentication_ = false;
SetAllowSigning(false);
allow_change_signing_ = false;
SetAllowCertification(false);
allow_change_certification_ = false;
suggest_min_key_size_ = -1;
suggest_max_key_size_ = -1;
suggest_size_addition_step_ = -1;
SetKeyLength(-1);
} else if (algo_args == "brainpoolp256r1") {
SetAllowAuthentication(false);
allow_change_authentication_ = false;
SetAllowSigning(false);
allow_change_signing_ = false;
SetAllowCertification(false);
allow_change_certification_ = false;
suggest_min_key_size_ = -1;
suggest_max_key_size_ = -1;
suggest_size_addition_step_ = -1;
SetKeyLength(-1);
}
this->algo_ = algo_args;
}
void GpgFrontend::GenKeyInfo::reset_options() {
allow_change_encryption_ = true;
SetAllowEncryption(true);
allow_change_certification_ = true;
SetAllowCertification(true);
allow_change_signing_ = true;
SetAllowSigning(true);
allow_change_authentication_ = true;
SetAllowAuthentication(true);
passphrase_.clear();
}
std::string GpgFrontend::GenKeyInfo::GetKeySizeStr() const {
if (key_size_ > 0) {
return std::to_string(key_size_);
} else {
return {};
}
}
void GpgFrontend::GenKeyInfo::SetKeyLength(int m_key_size) {
if (m_key_size < suggest_min_key_size_ ||
m_key_size > suggest_max_key_size_) {
return;
}
GenKeyInfo::key_size_ = m_key_size;
}
void GpgFrontend::GenKeyInfo::SetExpireTime(
const boost::posix_time::ptime &m_expired) {
using namespace boost::gregorian;
if (!IsNonExpired()) {
GenKeyInfo::expired_ = m_expired;
}
}
void GpgFrontend::GenKeyInfo::SetNonExpired(bool m_non_expired) {
using namespace boost::posix_time;
if (!m_non_expired) this->expired_ = from_time_t(0);
GenKeyInfo::non_expired_ = m_non_expired;
}
void GpgFrontend::GenKeyInfo::SetAllowEncryption(bool m_allow_encryption) {
if (allow_change_encryption_)
GenKeyInfo::allow_encryption_ = m_allow_encryption;
}
void GpgFrontend::GenKeyInfo::SetAllowCertification(
bool m_allow_certification) {
if (allow_change_certification_)
GenKeyInfo::allow_certification_ = m_allow_certification;
}
GpgFrontend::GenKeyInfo::GenKeyInfo(bool m_is_sub_key, bool m_standalone)
: standalone_(m_standalone), subkey_(m_is_sub_key) {
assert(GetSupportedKeyAlgo().size() > 0);
SetAlgo(GetSupportedKeyAlgo()[0]);
}
const std::vector<GpgFrontend::GenKeyInfo::KeyGenAlgo> &
GpgFrontend::GenKeyInfo::GetSupportedKeyAlgo() {
static const std::vector<GpgFrontend::GenKeyInfo::KeyGenAlgo>
support_key_algo = {
{"RSA", "RSA"},
{"DSA", "DSA"},
{"ECDSA", "ED25519"},
};
return support_key_algo;
}
const std::vector<GpgFrontend::GenKeyInfo::KeyGenAlgo> &
GpgFrontend::GenKeyInfo::GetSupportedSubkeyAlgo() {
static const std::vector<GpgFrontend::GenKeyInfo::KeyGenAlgo>
support_subkey_algo = {
{"RSA", "RSA"},
{"DSA", "DSA"},
{"ECDSA", "ED25519"},
{"ECDH NIST P-256", "NISTP256"},
{"ECDH NIST P-384", "NISTP384"},
{"ECDH NIST P-521", "NISTP521"},
// {"ECDH BrainPool P-256", "BRAINPOOlP256R1"}
};
return support_subkey_algo;
}
const std::vector<GpgFrontend::GenKeyInfo::KeyGenAlgo> &
GpgFrontend::GenKeyInfo::GetSupportedKeyAlgoStandalone() {
static const std::vector<GpgFrontend::GenKeyInfo::KeyGenAlgo>
support_subkey_algo_standalone = {
{"RSA", "RSA"},
{"DSA", "DSA"},
};
return support_subkey_algo_standalone;
}
const std::vector<GpgFrontend::GenKeyInfo::KeyGenAlgo> &
GpgFrontend::GenKeyInfo::GetSupportedSubkeyAlgoStandalone() {
static const std::vector<GpgFrontend::GenKeyInfo::KeyGenAlgo>
support_subkey_algo_standalone = {
{"RSA", "RSA"},
{"DSA", "DSA"},
};
return support_subkey_algo_standalone;
}
|