1 /* tofuinfo.cpp - wraps gpgme tofu info
2   Copyright (C) 2016 by Bundesamt für Sicherheit in der Informationstechnik
3   Software engineering by Intevation GmbH
4 
5   This file is part of GPGME++.
6 
7   GPGME++ is free software; you can redistribute it and/or
8   modify it under the terms of the GNU Library General Public
9   License as published by the Free Software Foundation; either
10   version 2 of the License, or (at your option) any later version.
11 
12   GPGME++ is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU Library General Public License for more details.
16 
17   You should have received a copy of the GNU Library General Public License
18   along with GPGME++; see the file COPYING.LIB.  If not, write to the
19   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20   Boston, MA 02110-1301, USA.
21 */
22 
23 #ifdef HAVE_CONFIG_H
24  #include "config.h"
25 #endif
26 
27 #include "tofuinfo.h"
28 
29 #include <istream>
30 #include "util.h"
31 
32 class GpgME::TofuInfo::Private
33 {
34 public:
Private()35     Private() {}
Private(gpgme_tofu_info_t info)36     Private(gpgme_tofu_info_t info)
37         : mInfo(info ? new _gpgme_tofu_info(*info) : nullptr)
38     {
39         if (mInfo && mInfo->description) {
40             mInfo->description = strdup(mInfo->description);
41         }
42     }
43 
Private(const Private & other)44     Private(const Private &other)
45         : mInfo(other.mInfo)
46     {
47         if (mInfo && mInfo->description) {
48             mInfo->description = strdup(mInfo->description);
49         }
50     }
51 
~Private()52     ~Private()
53     {
54         if (mInfo) {
55             std::free(mInfo->description);
56             mInfo->description = nullptr;
57 
58             delete mInfo;
59         }
60     }
61 
62     gpgme_tofu_info_t mInfo;
63 };
64 
TofuInfo(gpgme_tofu_info_t info)65 GpgME::TofuInfo::TofuInfo(gpgme_tofu_info_t info)
66     : d(new Private(info))
67 {
68 }
69 
TofuInfo()70 GpgME::TofuInfo::TofuInfo() : d()
71 {
72 }
73 
isNull() const74 bool GpgME::TofuInfo::isNull() const
75 {
76     return !d || !d->mInfo;
77 }
78 
validity() const79 GpgME::TofuInfo::Validity GpgME::TofuInfo::validity() const
80 {
81     if (isNull()) {
82         return ValidityUnknown;
83     }
84     switch (d->mInfo->validity) {
85         case 0:
86             return Conflict;
87         case 1:
88             return NoHistory;
89         case 2:
90             return LittleHistory;
91         case 3:
92             return BasicHistory;
93         case 4:
94             return LargeHistory;
95         default:
96             return ValidityUnknown;
97     }
98 }
99 
policy() const100 GpgME::TofuInfo::Policy GpgME::TofuInfo::policy() const
101 {
102     if (isNull()) {
103         return PolicyUnknown;
104     }
105     switch (d->mInfo->policy) {
106         case GPGME_TOFU_POLICY_NONE:
107             return PolicyNone;
108         case GPGME_TOFU_POLICY_AUTO:
109             return PolicyAuto;
110         case GPGME_TOFU_POLICY_GOOD:
111             return PolicyGood;
112         case GPGME_TOFU_POLICY_BAD:
113             return PolicyBad;
114         case GPGME_TOFU_POLICY_ASK:
115             return PolicyAsk;
116         case GPGME_TOFU_POLICY_UNKNOWN:
117         default:
118             return PolicyUnknown;
119     }
120 }
121 
description() const122 const char *GpgME::TofuInfo::description() const
123 {
124     return isNull() ? nullptr : d->mInfo->description;
125 }
126 
signCount() const127 unsigned short GpgME::TofuInfo::signCount() const
128 {
129     return isNull() ? 0 : d->mInfo->signcount;
130 }
131 
encrCount() const132 unsigned short GpgME::TofuInfo::encrCount() const
133 {
134     return isNull() ? 0 : d->mInfo->encrcount;
135 }
136 
signFirst() const137 unsigned long GpgME::TofuInfo::signFirst() const
138 {
139     return isNull() ? 0 : d->mInfo->signfirst;
140 }
141 
signLast() const142 unsigned long GpgME::TofuInfo::signLast() const
143 {
144     return isNull() ? 0 : d->mInfo->signlast;
145 }
146 
encrFirst() const147 unsigned long GpgME::TofuInfo::encrFirst() const
148 {
149     return isNull() ? 0 : d->mInfo->encrfirst;
150 }
151 
encrLast() const152 unsigned long GpgME::TofuInfo::encrLast() const
153 {
154     return isNull() ? 0 : d->mInfo->encrlast;
155 }
156 
operator <<(std::ostream & os,const GpgME::TofuInfo & info)157 std::ostream &GpgME::operator<<(std::ostream &os, const GpgME::TofuInfo &info)
158 {
159     os << "GpgME::Signature::TofuInfo(";
160     if (!info.isNull()) {
161         os << "\n desc: "     << protect(info.description())
162            << "\n validity: " << info.validity()
163            << "\n policy: "   << info.policy()
164            << "\n signcount: "<< info.signCount()
165            << "\n signfirst: "<< info.signFirst()
166            << "\n signlast: " << info.signLast()
167            << "\n encrcount: "<< info.encrCount()
168            << "\n encrfirst: "<< info.encrFirst()
169            << "\n encrlast: " << info.encrLast()
170            << '\n';
171     }
172     return os << ")";
173 }
174