aboutsummaryrefslogtreecommitdiffstats
path: root/src/m_paper_key/PaperKeyModule.cpp
blob: c118bd9ac6abe4bb481de5fd54a7417e05abbb61 (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
/**
 * 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 "PaperKeyModule.h"

#include <QtCore>

#include "GFModuleDefine.h"
#include "extract.h"

GF_MODULE_API_DEFINE("com.bktus.gpgfrontend.module.paper_key", "PaperKey",
                     "1.0.0", "Integrated PaperKey Functions.", "Saturneric")

auto GFRegisterModule() -> int {
  LOG_DEBUG("paper key module registering");

  return 0;
}

auto GFActiveModule() -> int {
  LISTEN("REQUEST_TRANS_KEY_2_PAPER_KEY");
  LISTEN("REQUEST_TRANS_PAPER_KEY_2_KEY");
  return 0;
}

EXECUTE_MODULE() {
  FLOG_DEBUG("paper key module executing, event id: %1", event["event_id"]);

  if (event["event_id"] == "REQUEST_TRANS_KEY_2_PAPER_KEY") {
    if (event["secret_key"].isEmpty() || event["output_path"].isEmpty()) {
      CB_ERR(event, -1, "secret key or output path is empty");
    }

    QByteArray secret_key_data =
        QByteArray::fromBase64(event["secret_key"].toUtf8());

    QTemporaryFile secret_key_t_file;
    if (!secret_key_t_file.open()) {
      CB_ERR(event, -1, "unable to open temporary file");
    }

    secret_key_t_file.write(secret_key_data);
    secret_key_t_file.flush();
    secret_key_t_file.seek(0);

    FILE *file = fdopen(secret_key_t_file.handle(), "rb");
    if (file == nullptr) {
      CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
    }

    extract(file, event["output_path"].toUtf8(), AUTO);

    fclose(file);
  } else if (event["event_id"] == "REQUEST_TRANS_PAPER_KEY_2_KEY") {
    if (event["public_key"].isEmpty() || event["paper_key_secrets"].isEmpty()) {
      CB_ERR(event, -1, "public key or paper key secrets is empty");
    }

    QByteArray public_key_data =
        QByteArray::fromBase64(event["public_key"].toUtf8());

    QTemporaryFile public_key_t_file;
    if (!public_key_t_file.open()) {
      CB_ERR(event, -1, "unable to open temporary file");
    }

    public_key_t_file.write(public_key_data);
    public_key_t_file.flush();
    public_key_t_file.seek(0);

    FILE *pubring = fdopen(public_key_t_file.handle(), "rb");
    if (pubring == nullptr) {
      CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
    }

    QByteArray secrets_data =
        QByteArray::fromBase64(event["paper_key_secrets"].toUtf8());

    QTemporaryFile secrets_data_file;
    if (!secrets_data_file.open()) {
      CB_ERR(event, -1, "unable to open temporary file");
    }

    secrets_data_file.write(public_key_data);
    secrets_data_file.flush();
    secrets_data_file.seek(0);

    FILE *secrets = fdopen(secrets_data_file.handle(), "rb");
    if (secrets == nullptr) {
      CB_ERR(event, -1, "unable to convert QTemporaryFile to FILE*");
    }

    restore(pubring, secrets, AUTO, )
  }

  CB_SUCC(event);
}
END_EXECUTE_MODULE()

auto GFDeactivateModule() -> int { return 0; }

auto GFUnregisterModule() -> int {
  MLogDebug("paper key module unregistering");

  return 0;
}