summaryrefslogtreecommitdiff
path: root/lang/cpp/src/verificationresult.cpp
blob: 23c458e1a5ed0ee92d073d32dd51da65b3bb138d (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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
/*
  verificationresult.cpp - wraps a gpgme verify result
  Copyright (C) 2004 Klarälvdalens Datakonsult AB

  This file is part of GPGME++.

  GPGME++ is free software; you can redistribute it and/or
  modify it under the terms of the GNU Library General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  GPGME++ 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 Library General Public License for more details.

  You should have received a copy of the GNU Library General Public License
  along with GPGME++; see the file COPYING.LIB.  If not, write to the
  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
*/

#ifdef HAVE_CONFIG_H
 #include "config.h"
#endif

#include <verificationresult.h>
#include <notation.h>
#include "result_p.h"
#include "util.h"
#include "key.h"
#include "context.h"

#include <gpgme.h>

#include <istream>
#include <algorithm>
#include <iterator>
#include <string>
#include <cstring>
#include <cstdlib>

#include <string.h>

class GpgME::VerificationResult::Private
{
public:
    explicit Private(const gpgme_verify_result_t r)
    {
        if (!r) {
            return;
        }
        if (r->file_name) {
            file_name = r->file_name;
        }
        // copy recursively, using compiler-generated copy ctor.
        // We just need to handle the pointers in the structs:
        for (gpgme_signature_t is = r->signatures ; is ; is = is->next) {
            gpgme_signature_t scopy = new _gpgme_signature(*is);
            if (is->fpr) {
                scopy->fpr = strdup(is->fpr);
            }
// PENDING(marc) why does this crash on Windows in strdup()?
# ifndef _WIN32
            if (is->pka_address) {
                scopy->pka_address = strdup(is->pka_address);
            }
# else
            scopy->pka_address = 0;
# endif
            scopy->next = 0;
            sigs.push_back(scopy);
            // copy keys
            if (scopy->key) {
                keys.push_back(Key(scopy->key, true));
            } else {
                keys.push_back(Key());
            }
            // copy notations:
            nota.push_back(std::vector<Nota>());
            purls.push_back(0);
            for (gpgme_sig_notation_t in = is->notations ; in ; in = in->next) {
                if (!in->name) {
                    if (in->value) {
                        purls.back() = strdup(in->value);   // policy url
                    }
                    continue;
                }
                Nota n = { 0, 0, in->flags };
                n.name = strdup(in->name);
                if (in->value) {
                    n.value = strdup(in->value);
                }
                nota.back().push_back(n);
            }
        }
    }
    ~Private()
    {
        for (std::vector<gpgme_signature_t>::iterator it = sigs.begin() ; it != sigs.end() ; ++it) {
            std::free((*it)->fpr);
            std::free((*it)->pka_address);
            delete *it; *it = 0;
        }
        for (std::vector< std::vector<Nota> >::iterator it = nota.begin() ; it != nota.end() ; ++it) {
            for (std::vector<Nota>::iterator jt = it->begin() ; jt != it->end() ; ++jt) {
                std::free(jt->name);  jt->name = 0;
                std::free(jt->value); jt->value = 0;
            }
        }
        std::for_each(purls.begin(), purls.end(), &std::free);
    }

    struct Nota {
        char *name;
        char *value;
        gpgme_sig_notation_flags_t flags;
    };

    std::vector<gpgme_signature_t> sigs;
    std::vector< std::vector<Nota> > nota;
    std::vector<GpgME::Key> keys;
    std::vector<char *> purls;
    std::string file_name;
    Protocol proto;
};

GpgME::VerificationResult::VerificationResult(gpgme_ctx_t ctx, int error)
    : GpgME::Result(error), d()
{
    init(ctx);
}

GpgME::VerificationResult::VerificationResult(gpgme_ctx_t ctx, const Error &error)
    : GpgME::Result(error), d()
{
    init(ctx);
}

void GpgME::VerificationResult::init(gpgme_ctx_t ctx)
{
    if (!ctx) {
        return;
    }
    gpgme_verify_result_t res = gpgme_op_verify_result(ctx);
    if (!res) {
        return;
    }
    d.reset(new Private(res));
    gpgme_protocol_t proto = gpgme_get_protocol(ctx);
    d->proto = proto == GPGME_PROTOCOL_OpenPGP ? OpenPGP :
               proto == GPGME_PROTOCOL_CMS ? CMS :
               UnknownProtocol;
}

make_standard_stuff(VerificationResult)

const char *GpgME::VerificationResult::fileName() const
{
    return d ? d->file_name.c_str() : 0 ;
}

unsigned int GpgME::VerificationResult::numSignatures() const
{
    return d ? d->sigs.size() : 0 ;
}

GpgME::Signature GpgME::VerificationResult::signature(unsigned int idx) const
{
    return Signature(d, idx);
}

std::vector<GpgME::Signature> GpgME::VerificationResult::signatures() const
{
    if (!d) {
        return std::vector<Signature>();
    }
    std::vector<Signature> result;
    result.reserve(d->sigs.size());
    for (unsigned int i = 0 ; i < d->sigs.size() ; ++i) {
        result.push_back(Signature(d, i));
    }
    return result;
}

GpgME::Signature::Signature(const std::shared_ptr<VerificationResult::Private> &parent, unsigned int i)
    : d(parent), idx(i)
{
}

GpgME::Signature::Signature() : d(), idx(0) {}

bool GpgME::Signature::isNull() const
{
    return !d || idx >= d->sigs.size() ;
}

GpgME::Signature::Summary GpgME::Signature::summary() const
{
    if (isNull()) {
        return None;
    }
    gpgme_sigsum_t sigsum = d->sigs[idx]->summary;
    unsigned int result = 0;
    if (sigsum & GPGME_SIGSUM_VALID) {
        result |= Valid;
    }
    if (sigsum & GPGME_SIGSUM_GREEN) {
        result |= Green;
    }
    if (sigsum & GPGME_SIGSUM_RED) {
        result |= Red;
    }
    if (sigsum & GPGME_SIGSUM_KEY_REVOKED) {
        result |= KeyRevoked;
    }
    if (sigsum & GPGME_SIGSUM_KEY_EXPIRED) {
        result |= KeyExpired;
    }
    if (sigsum & GPGME_SIGSUM_SIG_EXPIRED) {
        result |= SigExpired;
    }
    if (sigsum & GPGME_SIGSUM_KEY_MISSING) {
        result |= KeyMissing;
    }
    if (sigsum & GPGME_SIGSUM_CRL_MISSING) {
        result |= CrlMissing;
    }
    if (sigsum & GPGME_SIGSUM_CRL_TOO_OLD) {
        result |= CrlTooOld;
    }
    if (sigsum & GPGME_SIGSUM_BAD_POLICY) {
        result |= BadPolicy;
    }
    if (sigsum & GPGME_SIGSUM_SYS_ERROR) {
        result |= SysError;
    }
    if (sigsum & GPGME_SIGSUM_TOFU_CONFLICT) {
        result |= TofuConflict;
    }
    return static_cast<Summary>(result);
}

const char *GpgME::Signature::fingerprint() const
{
    return isNull() ? 0 : d->sigs[idx]->fpr ;
}

GpgME::Error GpgME::Signature::status() const
{
    return Error(isNull() ? 0 : d->sigs[idx]->status);
}

time_t GpgME::Signature::creationTime() const
{
    return static_cast<time_t>(isNull() ? 0 : d->sigs[idx]->timestamp);
}

time_t GpgME::Signature::expirationTime() const
{
    return static_cast<time_t>(isNull() ? 0 : d->sigs[idx]->exp_timestamp);
}

bool GpgME::Signature::neverExpires() const
{
    return expirationTime() == (time_t)0;
}

bool GpgME::Signature::isWrongKeyUsage() const
{
    return !isNull() && d->sigs[idx]->wrong_key_usage;
}

bool GpgME::Signature::isVerifiedUsingChainModel() const
{
    return !isNull() && d->sigs[idx]->chain_model;
}

GpgME::Signature::PKAStatus GpgME::Signature::pkaStatus() const
{
    if (!isNull()) {
        return static_cast<PKAStatus>(d->sigs[idx]->pka_trust);
    }
    return UnknownPKAStatus;
}

const char *GpgME::Signature::pkaAddress() const
{
    if (!isNull()) {
        return d->sigs[idx]->pka_address;
    }
    return 0;
}

GpgME::Signature::Validity GpgME::Signature::validity() const
{
    if (isNull()) {
        return Unknown;
    }
    switch (d->sigs[idx]->validity) {
    default:
    case GPGME_VALIDITY_UNKNOWN:   return Unknown;
    case GPGME_VALIDITY_UNDEFINED: return Undefined;
    case GPGME_VALIDITY_NEVER:     return Never;
    case GPGME_VALIDITY_MARGINAL:  return Marginal;
    case GPGME_VALIDITY_FULL:      return Full;
    case GPGME_VALIDITY_ULTIMATE:  return Ultimate;
    }
}

char GpgME::Signature::validityAsString() const
{
    if (isNull()) {
        return '?';
    }
    switch (d->sigs[idx]->validity) {
    default:
    case GPGME_VALIDITY_UNKNOWN:   return '?';
    case GPGME_VALIDITY_UNDEFINED: return 'q';
    case GPGME_VALIDITY_NEVER:     return 'n';
    case GPGME_VALIDITY_MARGINAL:  return 'm';
    case GPGME_VALIDITY_FULL:      return 'f';
    case GPGME_VALIDITY_ULTIMATE:  return 'u';
    }
}

GpgME::Error GpgME::Signature::nonValidityReason() const
{
    return Error(isNull() ? 0 : d->sigs[idx]->validity_reason);
}

unsigned int GpgME::Signature::publicKeyAlgorithm() const
{
    if (!isNull()) {
        return d->sigs[idx]->pubkey_algo;
    }
    return 0;
}

const char *GpgME::Signature::publicKeyAlgorithmAsString() const
{
    if (!isNull()) {
        return gpgme_pubkey_algo_name(d->sigs[idx]->pubkey_algo);
    }
    return 0;
}

unsigned int GpgME::Signature::hashAlgorithm() const
{
    if (!isNull()) {
        return d->sigs[idx]->hash_algo;
    }
    return 0;
}

const char *GpgME::Signature::hashAlgorithmAsString() const
{
    if (!isNull()) {
        return gpgme_hash_algo_name(d->sigs[idx]->hash_algo);
    }
    return 0;
}

const char *GpgME::Signature::policyURL() const
{
    return isNull() ? 0 : d->purls[idx] ;
}

GpgME::Notation GpgME::Signature::notation(unsigned int nidx) const
{
    return GpgME::Notation(d, idx, nidx);
}

std::vector<GpgME::Notation> GpgME::Signature::notations() const
{
    if (isNull()) {
        return std::vector<GpgME::Notation>();
    }
    std::vector<GpgME::Notation> result;
    result.reserve(d->nota[idx].size());
    for (unsigned int i = 0 ; i < d->nota[idx].size() ; ++i) {
        result.push_back(GpgME::Notation(d, idx, i));
    }
    return result;
}

GpgME::Key GpgME::Signature::key() const
{
    if (isNull()) {
        return Key();
    }
    return d->keys[idx];
}

GpgME::Key GpgME::Signature::key(bool search, bool update) const
{
    if (isNull()) {
        return Key();
    }

    GpgME::Key ret = key();
    if (ret.isNull() && search) {
        auto ctx = Context::createForProtocol (d->proto);
        if (ctx) {
            ctx->setKeyListMode(KeyListMode::Local |
                        KeyListMode::Signatures |
                        KeyListMode::SignatureNotations |
                        KeyListMode::Validate |
                        KeyListMode::WithTofu);
            Error e;
            ret = d->keys[idx] = ctx->key(fingerprint(), e, false);
            delete ctx;
        }
    }
    if (update) {
        ret.update();
    }
    return ret;
}

class GpgME::Notation::Private
{
public:
    Private() : d(), sidx(0), nidx(0), nota(0) {}
    Private(const std::shared_ptr<VerificationResult::Private> &priv, unsigned int sindex, unsigned int nindex)
        : d(priv), sidx(sindex), nidx(nindex), nota(0)
    {

    }
    Private(gpgme_sig_notation_t n)
        : d(), sidx(0), nidx(0), nota(n ? new _gpgme_sig_notation(*n) : 0)
    {
        if (nota && nota->name) {
            nota->name = strdup(nota->name);
        }
        if (nota && nota->value) {
            nota->value = strdup(nota->value);
        }
    }
    Private(const Private &other)
        : d(other.d), sidx(other.sidx), nidx(other.nidx), nota(other.nota)
    {
        if (nota) {
            nota->name = strdup(nota->name);
            nota->value = strdup(nota->value);
        }
    }
    ~Private()
    {
        if (nota) {
            std::free(nota->name);  nota->name = 0;
            std::free(nota->value); nota->value = 0;
            delete nota;
        }
    }

    std::shared_ptr<VerificationResult::Private> d;
    unsigned int sidx, nidx;
    gpgme_sig_notation_t nota;
};

GpgME::Notation::Notation(const std::shared_ptr<VerificationResult::Private> &parent, unsigned int sindex, unsigned int nindex)
    : d(new Private(parent, sindex, nindex))
{

}

GpgME::Notation::Notation(gpgme_sig_notation_t nota)
    : d(new Private(nota))
{

}

GpgME::Notation::Notation() : d() {}

bool GpgME::Notation::isNull() const
{
    if (!d) {
        return true;
    }
    if (d->d) {
        return d->sidx >= d->d->nota.size() || d->nidx >= d->d->nota[d->sidx].size() ;
    }
    return !d->nota;
}

const char *GpgME::Notation::name() const
{
    return
        isNull() ? 0 :
        d->d ? d->d->nota[d->sidx][d->nidx].name :
        d->nota ? d->nota->name : 0 ;
}

const char *GpgME::Notation::value() const
{
    return
        isNull() ? 0 :
        d->d ? d->d->nota[d->sidx][d->nidx].value :
        d->nota ? d->nota->value : 0 ;
}

GpgME::Notation::Flags GpgME::Notation::flags() const
{
    return
        convert_from_gpgme_sig_notation_flags_t(
            isNull() ? 0 :
            d->d ? d->d->nota[d->sidx][d->nidx].flags :
            d->nota ? d->nota->flags : 0);
}

bool GpgME::Notation::isHumanReadable() const
{
    return flags() & HumanReadable;
}

bool GpgME::Notation::isCritical() const
{
    return flags() & Critical;
}

std::ostream &GpgME::operator<<(std::ostream &os, const VerificationResult &result)
{
    os << "GpgME::VerificationResult(";
    if (!result.isNull()) {
        os << "\n error:      " << result.error()
           << "\n fileName:   " << protect(result.fileName())
           << "\n signatures:\n";
        const std::vector<Signature> sigs = result.signatures();
        std::copy(sigs.begin(), sigs.end(),
                  std::ostream_iterator<Signature>(os, "\n"));
    }
    return os << ')';
}

std::ostream &GpgME::operator<<(std::ostream &os, Signature::PKAStatus pkaStatus)
{
#define OUTPUT( x ) if ( !(pkaStatus & (GpgME::Signature:: x)) ) {} else do { os << #x " "; } while(0)
    os << "GpgME::Signature::PKAStatus(";
    OUTPUT(UnknownPKAStatus);
    OUTPUT(PKAVerificationFailed);
    OUTPUT(PKAVerificationSucceeded);
#undef OUTPUT
    return os << ')';
}

std::ostream &GpgME::operator<<(std::ostream &os, Signature::Summary summary)
{
#define OUTPUT( x ) if ( !(summary & (GpgME::Signature:: x)) ) {} else do { os << #x " "; } while(0)
    os << "GpgME::Signature::Summary(";
    OUTPUT(Valid);
    OUTPUT(Green);
    OUTPUT(Red);
    OUTPUT(KeyRevoked);
    OUTPUT(KeyExpired);
    OUTPUT(SigExpired);
    OUTPUT(KeyMissing);
    OUTPUT(CrlMissing);
    OUTPUT(CrlTooOld);
    OUTPUT(BadPolicy);
    OUTPUT(SysError);
    OUTPUT(TofuConflict);
#undef OUTPUT
    return os << ')';
}

std::ostream &GpgME::operator<<(std::ostream &os, const Signature &sig)
{
    os << "GpgME::Signature(";
    if (!sig.isNull()) {
        os << "\n Summary:                   " << sig.summary()
           << "\n Fingerprint:               " << protect(sig.fingerprint())
           << "\n Status:                    " << sig.status()
           << "\n creationTime:              " << sig.creationTime()
           << "\n expirationTime:            " << sig.expirationTime()
           << "\n isWrongKeyUsage:           " << sig.isWrongKeyUsage()
           << "\n isVerifiedUsingChainModel: " << sig.isVerifiedUsingChainModel()
           << "\n pkaStatus:                 " << sig.pkaStatus()
           << "\n pkaAddress:                " << protect(sig.pkaAddress())
           << "\n validity:                  " << sig.validityAsString()
           << "\n nonValidityReason:         " << sig.nonValidityReason()
           << "\n publicKeyAlgorithm:        " << protect(sig.publicKeyAlgorithmAsString())
           << "\n hashAlgorithm:             " << protect(sig.hashAlgorithmAsString())
           << "\n policyURL:                 " << protect(sig.policyURL())
           << "\n notations:\n";
        const std::vector<Notation> nota = sig.notations();
        std::copy(nota.begin(), nota.end(),
                  std::ostream_iterator<Notation>(os, "\n"));
    }
    return os << ')';
}

std::ostream &GpgME::operator<<(std::ostream &os, Notation::Flags flags)
{
    os << "GpgME::Notation::Flags(";
#define OUTPUT( x ) if ( !(flags & (GpgME::Notation:: x)) ) {} else do { os << #x " "; } while(0)
    OUTPUT(HumanReadable);
    OUTPUT(Critical);
#undef OUTPUT
    return os << ')';
}

std::ostream &GpgME::operator<<(std::ostream &os, const Notation &nota)
{
    os << "GpgME::Signature::Notation(";
    if (!nota.isNull()) {
        os << "\n name:  " << protect(nota.name())
           << "\n value: " << protect(nota.value())
           << "\n flags: " << nota.flags()
           << '\n';
    }
    return os << ")";
}