aboutsummaryrefslogtreecommitdiffstats
path: root/src/core/function/result_analyse/GpgSignResultAnalyse.cpp
blob: 2c53d329ffc82be5de3bd60dcff7cbe45ed1708c (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
/**
 * 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 "GpgSignResultAnalyse.h"

#include "core/GpgModel.h"
#include "core/function/gpg/GpgKeyGetter.h"

namespace GpgFrontend {

GpgSignResultAnalyse::GpgSignResultAnalyse(GpgError error, GpgSignResult result)
    : error_(error), result_(std::move(result)) {}

void GpgSignResultAnalyse::doAnalyse() {
  auto *result = this->result_.GetRaw();

  GF_CORE_LOG_DEBUG("start sign result analyse");

  stream_ << "[#] " << _("Sign Operation") << " ";

  if (gpgme_err_code(error_) == GPG_ERR_NO_ERROR) {
    stream_ << "[" << _("Success") << "]" << std::endl;
  } else {
    stream_ << "[" << _("Failed") << "] " << gpgme_strerror(error_)
            << std::endl;
    setStatus(-1);
  }

  if (result != nullptr &&
      (result->signatures != nullptr || result->invalid_signers != nullptr)) {
    GF_CORE_LOG_DEBUG("sign result analyse getting result");
    stream_ << "------------>" << std::endl;
    auto *new_sign = result->signatures;

    while (new_sign != nullptr) {
      stream_ << "[>]" << _("New Signature") << ": " << std::endl;

      GF_CORE_LOG_DEBUG("signers fingerprint: ", new_sign->fpr);

      stream_ << "    " << _("Sign Mode") << ": ";
      if (new_sign->type == GPGME_SIG_MODE_NORMAL) {
        stream_ << _("Normal");
      } else if (new_sign->type == GPGME_SIG_MODE_CLEAR) {
        stream_ << _("Clear");
      } else if (new_sign->type == GPGME_SIG_MODE_DETACH) {
        stream_ << _("Detach");
      }

      stream_ << std::endl;

      auto singer_key = GpgKeyGetter::GetInstance().GetKey(new_sign->fpr);
      if (singer_key.IsGood()) {
        stream_ << "    " << _("Signer") << ": "
                << singer_key.GetUIDs()->front().GetUID() << std::endl;
      } else {
        stream_ << "    " << _("Signer") << ": "
                << "<unknown>" << std::endl;
      }
      stream_ << "    " << _("Public Key Algo") << ": "
              << gpgme_pubkey_algo_name(new_sign->pubkey_algo) << std::endl;
      stream_ << "    " << _("Hash Algo") << ": "
              << gpgme_hash_algo_name(new_sign->hash_algo) << std::endl;
      stream_ << "    " << _("Date") << "(" << _("UTC") << ")"
              << ": "
              << boost::posix_time::to_iso_extended_string(
                     boost::posix_time::from_time_t(new_sign->timestamp))
              << std::endl;

      stream_ << std::endl;

      new_sign = new_sign->next;
    }

    GF_CORE_LOG_DEBUG("sign result analyse getting invalid signer");

    auto *invalid_signer = result->invalid_signers;

    if (invalid_signer != nullptr) {
      stream_ << _("Invalid Signers") << ": " << std::endl;
    }

    while (invalid_signer != nullptr) {
      setStatus(0);
      stream_ << "[>] " << _("Signer") << ": " << std::endl;
      stream_ << "      " << _("Fingerprint") << ": " << invalid_signer->fpr
              << std::endl;
      stream_ << "      " << _("Reason") << ": "
              << gpgme_strerror(invalid_signer->reason) << std::endl;
      stream_ << std::endl;

      invalid_signer = invalid_signer->next;
    }
    stream_ << "<------------" << std::endl;
  }
}

}  // namespace GpgFrontend