aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/function/gpg/GpgSmartCardManager.cpp
blob: 9937ef7a31ab91316812ac0d0db7e028cd4ba23d (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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
 * Copyright (C) 2021-2024 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 "GpgSmartCardManager.h"

#include "core/function/gpg/GpgAutomatonHandler.h"

namespace GpgFrontend {

GpgSmartCardManager::GpgSmartCardManager(int channel)
    : SingletonFunctionObject<GpgSmartCardManager>(channel) {}

auto GpgSmartCardManager::Fetch(const QString& serial_number) -> bool {
  GpgAutomatonHandler::AutomatonNextStateHandler next_state_handler =
      [=](AutomatonState state, QString status, QString args) {
        auto tokens = args.split(' ');

        switch (state) {
          case GpgAutomatonHandler::kAS_START:
            if (status == "GET_LINE" && args == "cardedit.prompt") {
              return GpgAutomatonHandler::kAS_COMMAND;
            }
            return GpgAutomatonHandler::kAS_ERROR;
          case GpgAutomatonHandler::kAS_COMMAND:
            if (status == "GET_LINE" && args == "cardedit.prompt") {
              return GpgAutomatonHandler::kAS_QUIT;
            }
            return GpgAutomatonHandler::kAS_ERROR;
          case GpgAutomatonHandler::kAS_QUIT:
          case GpgAutomatonHandler::kAS_ERROR:
            if (status == "GET_LINE" && args == "keyedit.prompt") {
              return GpgAutomatonHandler::kAS_QUIT;
            }
            return GpgAutomatonHandler::kAS_ERROR;
          default:
            return GpgAutomatonHandler::kAS_ERROR;
        };
      };

  AutomatonActionHandler action_handler = [](AutomatonHandelStruct& handler,
                                             AutomatonState state) {
    switch (state) {
      case GpgAutomatonHandler::kAS_COMMAND:
        return QString("fetch");
      case GpgAutomatonHandler::kAS_QUIT:
        return QString("quit");
      case GpgAutomatonHandler::kAS_START:
      case GpgAutomatonHandler::kAS_ERROR:
      default:
        return QString("");
    }
    return QString("");
  };

  return GpgAutomatonHandler::GetInstance(GetChannel())
      .DoCardInteract(serial_number, next_state_handler, action_handler);
}

auto GpgSmartCardManager::GetSerialNumbers() -> QStringList {
  auto [r, s] = assuan_.SendStatusCommand(GpgComponentType::kGPG_AGENT,
                                          "SCD SERIALNO --all");
  if (!r) {
    cached_scd_serialno_status_hash_.clear();
    cache_scd_card_serial_numbers_.clear();
    return {};
  }

  auto hash =
      QCryptographicHash::hash(s.join(' ').toUtf8(), QCryptographicHash::Sha1)
          .toHex();
  // check and skip
  if (cached_scd_serialno_status_hash_ == hash) {
    return cache_scd_card_serial_numbers_;
  }

  cached_scd_serialno_status_hash_.clear();
  cache_scd_card_serial_numbers_.clear();
  auto [ret, status] = assuan_.SendStatusCommand(GpgComponentType::kGPG_AGENT,
                                                 "SCD GETINFO all_active_apps");
  if (!ret || status.empty()) {
    LOG_D() << "command SCD GETINFO all_active_apps failed, resetting...";
    return {};
  }

  for (const auto& line : status) {
    auto tokens = line.split(' ');

    if (tokens.size() < 2 || tokens[0] != "SERIALNO") {
      LOG_E() << "invalid response of command GETINFO all_active_apps: "
              << line;
      continue;
    }

    auto serial_number = tokens[1];
    if (!line.contains("openpgp")) {
      LOG_W() << "smart card: " << serial_number << "doesn't support openpgp.";
      continue;
    }

    cache_scd_card_serial_numbers_.append(serial_number);
  }

  cached_scd_serialno_status_hash_ = hash;
  return cache_scd_card_serial_numbers_;
}

auto GpgSmartCardManager::SelectCardBySerialNumber(const QString& serial_number)
    -> std::tuple<bool, QString> {
  if (serial_number.isEmpty()) return {false, "Serial Number is empty."};

  auto [ret, status] = assuan_.SendStatusCommand(
      GpgComponentType::kGPG_AGENT,
      QString("SCD SERIALNO --demand=%1 openpgp").arg(serial_number));
  if (!ret || status.isEmpty()) {
    return {false, status.join(' ')};
  }

  auto line = status.front();
  auto token = line.split(' ');

  if (token.size() != 2) {
    LOG_E() << "invalid response of command SERIALNO: " << line;
    return {false, line};
  }

  LOG_D() << "selected smart card by serial number: " << serial_number;

  return {true, {}};
}

auto GpgSmartCardManager::FetchCardInfoBySerialNumber(
    const QString& serial_number) -> QSharedPointer<GpgOpenPGPCard> {
  if (serial_number.trimmed().isEmpty()) return nullptr;

  auto [ret, status] = assuan_.SendStatusCommand(
      GpgComponentType::kGPG_AGENT, "SCD LEARN --force " + serial_number);
  if (!ret || status.isEmpty()) {
    LOG_E() << "scd learn failed: " << status;
    return nullptr;
  }

  auto card_info = GpgOpenPGPCard(status);
  if (!card_info.good) {
    return nullptr;
  }

  return QSharedPointer<GpgOpenPGPCard>::create(card_info);
}

auto PercentDataEscape(const QByteArray& data, bool plus_escape = false,
                       const QString& prefix = QString()) -> QString {
  QString result;

  if (!prefix.isEmpty()) {
    for (QChar ch : prefix) {
      if (ch == '%' || ch.unicode() < 0x20) {
        result +=
            QString("%%%1")
                .arg(static_cast<int>(ch.unicode()), 2, 16, QLatin1Char('0'))
                .toUpper();
      } else {
        result += ch;
      }
    }
  }

  for (unsigned char ch : data) {
    if (ch == '\0') {
      result += "%00";
    } else if (ch == '%') {
      result += "%25";
    } else if (plus_escape && ch == ' ') {
      result += '+';
    } else if (plus_escape && (ch < 0x20 || ch == '+')) {
      result += QString("%%%1").arg(ch, 2, 16, QLatin1Char('0')).toUpper();
    } else {
      result += QLatin1Char(ch);
    }
  }

  return result;
}

auto GpgSmartCardManager::ModifyAttr(const QString& attr, const QString& value)
    -> std::tuple<bool, QString> {
  if (attr.trimmed().isEmpty() || value.trimmed().isEmpty()) {
    return {false, "ATTR or Value is empty"};
  }

  const auto command = QString("SCD SETATTR %1 ").arg(attr);
  const auto escaped_command =
      PercentDataEscape(value.trimmed().toUtf8(), true, command);

  auto [r, s] =
      assuan_.SendStatusCommand(GpgComponentType::kGPG_AGENT, escaped_command);

  if (!r) {
    LOG_E() << "SCD SETATTR command failed for attr" << attr;
    return {false, s.join(' ')};
  }

  return {true, {}};
}

auto GpgSmartCardManager::ModifyPin(const QString& pin_ref)
    -> std::tuple<bool, QString> {
  if (pin_ref.trimmed().isEmpty()) return {false, "PIN Reference is empty"};

  QString command;
  if (pin_ref == "OPENPGP.1") {
    command = "SCD PASSWD OPENPGP.1";
  } else if (pin_ref == "OPENPGP.3") {
    command = "SCD PASSWD OPENPGP.3";
  } else if (pin_ref == "OPENPGP.2") {
    command = "SCD PASSWD --reset OPENPGP.2";
  } else {
    command = QString("SCD PASSWD %1").arg(pin_ref);
  }

  auto [success, status] =
      assuan_.SendStatusCommand(GpgComponentType::kGPG_AGENT, command);

  if (!success) {
    LOG_E() << "modify pin of smart failed: " << status;
    return {false, status.join(' ')};
  }

  return {true, {}};
}

auto GpgSmartCardManager::GenerateKey(
    const QString& serial_number, const QString& name, const QString& email,
    const QString& comment, const QDateTime& expire,
    bool non_expire) -> std::tuple<bool, QString> {
  if (name.isEmpty() || email.isEmpty()) {
    return {false, "name or email is empty"};
  }

  qint64 days_before_expire = 0;
  if (!non_expire) {
    days_before_expire = QDateTime::currentDateTime().daysTo(expire);
  }

  GpgAutomatonHandler::AutomatonNextStateHandler next_state_handler =
      [=](AutomatonState state, const QString& status, const QString& args) {
        auto tokens = args.split(' ');

        switch (state) {
          case GpgAutomatonHandler::kAS_START:
            if (status == "GET_LINE" && args == "cardedit.prompt") {
              return GpgAutomatonHandler::kAS_ADMIN;
            }
            return GpgAutomatonHandler::kAS_ERROR;
          case GpgAutomatonHandler::kAS_ADMIN:
            if (status == "GET_LINE" && args == "cardedit.prompt") {
              return GpgAutomatonHandler::kAS_COMMAND;
            }
            return GpgAutomatonHandler::kAS_ERROR;
          case GpgAutomatonHandler::kAS_COMMAND:
            if (status == "GET_LINE" && args == "cardedit.genkeys.backup_enc") {
              return GpgAutomatonHandler::kAS_COMMAND;
            }
            if (status == "GET_BOOL" &&
                args == "cardedit.genkeys.replace_keys") {
              return GpgAutomatonHandler::kAS_COMMAND;
            }
            if (status == "GET_LINE" && args == "keygen.valid") {
              return GpgAutomatonHandler::kAS_COMMAND;
            }
            if (status == "GET_LINE" && args == "keygen.name") {
              return GpgAutomatonHandler::kAS_COMMAND;
            }
            if (status == "GET_LINE" && args == "keygen.email") {
              return GpgAutomatonHandler::kAS_COMMAND;
            }
            if (status == "GET_LINE" && args == "keygen.comment") {
              return GpgAutomatonHandler::kAS_COMMAND;
            }
            if (status == "PINENTRY_LAUNCHED" ||
                status == "BACKUP_KEY_CREATED" || status == "KEY_CONSIDERED" ||
                status == "KEY_CREATED") {
              return GpgAutomatonHandler::kAS_INFO;
            }
            return GpgAutomatonHandler::kAS_ERROR;
          case GpgAutomatonHandler::kAS_INFO:
            if (status == "PINENTRY_LAUNCHED" ||
                status == "BACKUP_KEY_CREATED" || status == "KEY_CONSIDERED" ||
                status == "KEY_CREATED") {
              return GpgAutomatonHandler::kAS_INFO;
            }
            if (status == "GET_LINE" && args == "cardedit.prompt") {
              return GpgAutomatonHandler::kAS_QUIT;
            }
            return GpgAutomatonHandler::kAS_ERROR;
          case GpgAutomatonHandler::kAS_QUIT:
          case GpgAutomatonHandler::kAS_ERROR:
            if (status == "GET_LINE" && args == "keyedit.prompt") {
              return GpgAutomatonHandler::kAS_QUIT;
            }
            return GpgAutomatonHandler::kAS_ERROR;
          default:
            return GpgAutomatonHandler::kAS_ERROR;
        };
      };

  AutomatonActionHandler action_handler = [=](AutomatonHandelStruct& handler,
                                              AutomatonState state) {
    switch (state) {
      case GpgAutomatonHandler::kAS_ADMIN:
        return QString("admin");
      case GpgAutomatonHandler::kAS_COMMAND: {
        auto [status, args] = handler.PromptStatus();
        if (args == "cardedit.prompt") {
          return QString("generate");
        }
        if (args == "cardedit.genkeys.backup_enc") {
          return QString("y");
        }
        if (args == "cardedit.genkeys.replace_keys") {
          return QString("y");
        }
        if (args == "keygen.valid") {
          return QString::number(days_before_expire);
        }
        if (args == "keygen.name") {
          return name;
        }
        if (args == "keygen.email") {
          return email;
        }
        if (args == "keygen.comment") {
          return comment;
        }
        return QString{};
      }
      case GpgAutomatonHandler::kAS_QUIT:
        return QString("quit");
      case GpgAutomatonHandler::kAS_INFO:
      case GpgAutomatonHandler::kAS_START:
      case GpgAutomatonHandler::kAS_ERROR:
      default:
        return QString{};
    }

    return QString{};
  };

  GpgAutomatonHandler::GetInstance(GetChannel())
      .DoCardInteract(serial_number, next_state_handler, action_handler);

  return {true, {}};
}
}  // namespace GpgFrontend