File: gpgaction.cpp

package info (click to toggle)
qca2 2.3.10-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,888 kB
  • sloc: cpp: 59,224; ansic: 814; perl: 133; sh: 89; makefile: 34
file content (848 lines) | stat: -rw-r--r-- 24,751 bytes parent folder | download | duplicates (3)
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
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/*
 * Copyright (C) 2003-2005  Justin Karneges <justin@affinix.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
 *
 */

// #define GPGOP_DEBUG

#include "gpgaction.h"

#ifdef GPGOP_DEBUG
#include "stdio.h"
#endif

namespace gpgQCAPlugin {

static QDateTime getTimestamp(const QString &s)
{
    if (s.isEmpty())
        return QDateTime();

    if (s.contains(QLatin1Char('T'))) {
        return QDateTime::fromString(s, Qt::ISODate);
    } else {
        return QDateTime::fromSecsSinceEpoch(s.toInt());
    }
}

static QByteArray getCString(const QByteArray &a)
{
    QByteArray out;

    // convert the "backslash" C-string syntax
    for (int n = 0; n < a.size(); ++n) {
        if (a[n] == '\\' && n + 1 < a.size()) {
            ++n;
            unsigned char c = (unsigned char)a[n];
            if (c == '\\') {
                out += '\\';
            } else if (c == 'x' && n + 2 < a.size()) {
                ++n;
                const QByteArray hex = a.mid(n, 2);
                ++n; // only skip one, loop will skip the next

                bool ok;
                uint val = hex.toInt(&ok, 16);
                if (ok) {
                    out += (unsigned char)val;
                } else {
                    out += "\\x";
                    out += hex;
                }
            }
        } else {
            out += a[n];
        }
    }

    return out;
}

static bool stringToKeyList(const QString &outstr, GpgOp::KeyList *_keylist, QString *_keyring)
{
    GpgOp::KeyList    keyList;
    const QStringList lines = outstr.split(QLatin1Char('\n'));

    if (lines.count() < 1)
        return false;

    QStringList::ConstIterator it = lines.constBegin();

    // first line is keyring file
    QString keyring = *(it++);

    // if the second line isn't a divider, we are dealing
    // with a new version of gnupg that doesn't give us
    // the keyring file on gpg --list-keys --with-colons
    if (it == lines.constEnd() || (*it).isEmpty() || (*it).at(0) != QLatin1Char('-')) {
        // first line wasn't the keyring name...
        keyring.clear();
        // ...so read the first line again
        it--;
    } else {
        // this was the divider line - skip it
        it++;
    }

    for (; it != lines.constEnd(); ++it) {
        const QStringList f = (*it).split(QLatin1Char(':'));
        if (f.count() < 1)
            continue;
        const QString &type = f[0];

        bool key     = false; // key or not
        bool primary = false; // primary key or sub key
        // bool sec = false; // private key or not

        if (type == QLatin1String("pub")) {
            key     = true;
            primary = true;
        } else if (type == QLatin1String("sec")) {
            key     = true;
            primary = true;
            // sec = true;
        } else if (type == QLatin1String("sub")) {
            key = true;
        } else if (type == QLatin1String("ssb")) {
            key = true;
            // sec = true;
        }

        if (key) {
            if (primary) {
                keyList += GpgOp::Key();

                const QString &trust = f[1];
                if (trust == QLatin1String("f") || trust == QLatin1String("u"))
                    keyList.last().isTrusted = true;
            }

            const int      key_type = f[3].toInt();
            const QString &caps     = f[11];

            GpgOp::KeyItem item;
            item.bits = f[2].toInt();
            if (key_type == 1)
                item.type = GpgOp::KeyItem::RSA;
            else if (key_type == 16)
                item.type = GpgOp::KeyItem::ElGamal;
            else if (key_type == 17)
                item.type = GpgOp::KeyItem::DSA;
            else
                item.type = GpgOp::KeyItem::Unknown;
            item.id             = f[4];
            item.creationDate   = getTimestamp(f[5]);
            item.expirationDate = getTimestamp(f[6]);
            if (caps.contains(QLatin1Char('e')))
                item.caps |= GpgOp::KeyItem::Encrypt;
            if (caps.contains(QLatin1Char('s')))
                item.caps |= GpgOp::KeyItem::Sign;
            if (caps.contains(QLatin1Char('c')))
                item.caps |= GpgOp::KeyItem::Certify;
            if (caps.contains(QLatin1Char('a')))
                item.caps |= GpgOp::KeyItem::Auth;

            keyList.last().keyItems += item;
        } else if (type == QLatin1String("uid")) {
            const QByteArray uid = getCString(f[9].toUtf8());
            keyList.last().userIds.append(QString::fromUtf8(uid));
        } else if (type == QLatin1String("fpr")) {
            const QString &s                           = f[9];
            keyList.last().keyItems.last().fingerprint = s;
        }
    }

    if (_keylist)
        *_keylist = keyList;
    if (_keyring)
        *_keyring = keyring;

    return true;
}

static bool findKeyringFilename(const QString &outstr, QString *_keyring)
{
    const QStringList lines = outstr.split(QLatin1Char('\n'));
    if (lines.count() < 1)
        return false;

    *_keyring = lines[0];
    return true;
}

GpgAction::GpgAction(QObject *parent)
    : QObject(parent)
    , proc(this)
    , dtextTimer(this)
    , utf8Output(false)
{
    dtextTimer.setSingleShot(true);

    connect(&proc, &GPGProc::error, this, &GpgAction::proc_error);
    connect(&proc, &GPGProc::finished, this, &GpgAction::proc_finished);
    connect(&proc, &GPGProc::readyReadStdout, this, &GpgAction::proc_readyReadStdout);
    connect(&proc, &GPGProc::readyReadStderr, this, &GpgAction::proc_readyReadStderr);
    connect(&proc, &GPGProc::readyReadStatusLines, this, &GpgAction::proc_readyReadStatusLines);
    connect(&proc, &GPGProc::bytesWrittenStdin, this, &GpgAction::proc_bytesWrittenStdin);
    connect(&proc, &GPGProc::bytesWrittenAux, this, &GpgAction::proc_bytesWrittenAux);
    connect(&proc, &GPGProc::bytesWrittenCommand, this, &GpgAction::proc_bytesWrittenCommand);
    connect(&proc, &GPGProc::debug, this, &GpgAction::proc_debug);
    connect(&dtextTimer, &QCA::SafeTimer::timeout, this, &GpgAction::t_dtext);

    reset();
}

GpgAction::~GpgAction()
{
    reset();
}

void GpgAction::reset()
{
    collectOutput = true;
    allowInput    = false;
    readConv.setup(LineConverter::Read);
    writeConv.setup(LineConverter::Write);
    readText              = false;
    writeText             = false;
    useAux                = false;
    passphraseKeyId       = QString();
    signing               = false;
    decryptGood           = false;
    signGood              = false;
    curError              = GpgOp::ErrorUnknown;
    badPassphrase         = false;
    need_submitPassphrase = false;
    need_cardOkay         = false;
    diagnosticText        = QString();
    dtextTimer.stop();

    output = Output();

    proc.reset();
}

void GpgAction::start()
{
    reset();

    QStringList args;
    bool        extra = false;

    if (input.opt_ascii)
        args += QStringLiteral("--armor");

    if (input.opt_noagent)
        args += QStringLiteral("--no-use-agent");

    if (input.opt_alwaystrust)
        args += QStringLiteral("--always-trust");

    if (!input.opt_pubfile.isEmpty() && !input.opt_secfile.isEmpty()) {
        args += QStringLiteral("--no-default-keyring");
        args += QStringLiteral("--keyring");
        args += input.opt_pubfile;
        args += QStringLiteral("--secret-keyring");
        args += input.opt_secfile;
    }

    switch (input.op) {
    case GpgOp::Check:
    {
        args += QStringLiteral("--version");
        readText = true;
        break;
    }
    case GpgOp::SecretKeyringFile:
    {
#ifndef Q_OS_WIN
        args += QStringLiteral("--display-charset=utf-8");
#endif
        args += QStringLiteral("--list-secret-keys");
        readText = true;
        break;
    }
    case GpgOp::PublicKeyringFile:
    {
#ifndef Q_OS_WIN
        args += QStringLiteral("--display-charset=utf-8");
#endif
        args += QStringLiteral("--list-public-keys");
        readText = true;
        break;
    }
    case GpgOp::SecretKeys:
    {
        args += QStringLiteral("--fixed-list-mode");
        args += QStringLiteral("--with-colons");
        args += QStringLiteral("--with-fingerprint");
        args += QStringLiteral("--with-fingerprint");
        args += QStringLiteral("--list-secret-keys");
        utf8Output = true;
        readText   = true;
        break;
    }
    case GpgOp::PublicKeys:
    {
        args += QStringLiteral("--fixed-list-mode");
        args += QStringLiteral("--with-colons");
        args += QStringLiteral("--with-fingerprint");
        args += QStringLiteral("--with-fingerprint");
        args += QStringLiteral("--list-public-keys");
        utf8Output = true;
        readText   = true;
        break;
    }
    case GpgOp::Encrypt:
    {
        args += QStringLiteral("--encrypt");

        // recipients
        for (QStringList::ConstIterator it = input.recip_ids.constBegin(); it != input.recip_ids.constEnd(); ++it) {
            args += QStringLiteral("--recipient");
            args += QStringLiteral("0x") + *it;
        }
        extra         = true;
        collectOutput = false;
        allowInput    = true;
        if (input.opt_ascii)
            readText = true;
        break;
    }
    case GpgOp::Decrypt:
    {
        args += QStringLiteral("--decrypt");
        extra         = true;
        collectOutput = false;
        allowInput    = true;
        if (input.opt_ascii)
            writeText = true;
        break;
    }
    case GpgOp::Sign:
    {
        args += QStringLiteral("--default-key");
        args += QStringLiteral("0x") + input.signer_id;
        args += QStringLiteral("--sign");
        extra         = true;
        collectOutput = false;
        allowInput    = true;
        if (input.opt_ascii)
            readText = true;
        signing = true;
        break;
    }
    case GpgOp::SignAndEncrypt:
    {
        args += QStringLiteral("--default-key");
        args += QStringLiteral("0x") + input.signer_id;
        args += QStringLiteral("--sign");
        args += QStringLiteral("--encrypt");

        // recipients
        for (QStringList::ConstIterator it = input.recip_ids.constBegin(); it != input.recip_ids.constEnd(); ++it) {
            args += QStringLiteral("--recipient");
            args += QStringLiteral("0x") + *it;
        }
        extra         = true;
        collectOutput = false;
        allowInput    = true;
        if (input.opt_ascii)
            readText = true;
        signing = true;
        break;
    }
    case GpgOp::SignClearsign:
    {
        args += QStringLiteral("--default-key");
        args += QStringLiteral("0x") + input.signer_id;
        args += QStringLiteral("--clearsign");
        extra         = true;
        collectOutput = false;
        allowInput    = true;
        if (input.opt_ascii)
            readText = true;
        signing = true;
        break;
    }
    case GpgOp::SignDetached:
    {
        args += QStringLiteral("--default-key");
        args += QStringLiteral("0x") + input.signer_id;
        args += QStringLiteral("--detach-sign");
        extra         = true;
        collectOutput = false;
        allowInput    = true;
        if (input.opt_ascii)
            readText = true;
        signing = true;
        break;
    }
    case GpgOp::Verify:
    {
        args += QStringLiteral("--verify");
        args += QStringLiteral("-"); // krazy:exclude=doublequote_chars
        extra      = true;
        allowInput = true;
        if (input.opt_ascii)
            writeText = true;
        break;
    }
    case GpgOp::VerifyDetached:
    {
        args += QStringLiteral("--verify");
        args += QStringLiteral("-"); // krazy:exclude=doublequote_chars
        args += QStringLiteral("-&?");
        extra      = true;
        allowInput = true;
        useAux     = true;
        break;
    }
    case GpgOp::Import:
    {
        args += QStringLiteral("--import");
        readText = true;
        if (input.opt_ascii)
            writeText = true;
        break;
    }
    case GpgOp::Export:
    {
        args += QStringLiteral("--export");
        args += QStringLiteral("0x") + input.export_key_id;
        collectOutput = false;
        if (input.opt_ascii)
            readText = true;
        break;
    }
    case GpgOp::DeleteKey:
    {
        args += QStringLiteral("--batch");
        args += QStringLiteral("--delete-key");
        args += QStringLiteral("0x") + input.delete_key_fingerprint;
        break;
    }
    }

#ifdef GPG_PROFILE
    timer.start();
    printf("<< launch >>\n");
#endif
    proc.start(input.bin, args, extra ? GPGProc::ExtendedMode : GPGProc::NormalMode);

    // detached sig
    if (input.op == GpgOp::VerifyDetached) {
        QByteArray a = input.sig;
        if (input.opt_ascii) {
            LineConverter conv;
            conv.setup(LineConverter::Write);
            a = conv.process(a);
        }
        proc.writeStdin(a);
        proc.closeStdin();
    }

    // import
    if (input.op == GpgOp::Import) {
        QByteArray a = input.inkey;
        if (writeText) {
            LineConverter conv;
            conv.setup(LineConverter::Write);
            a = conv.process(a);
        }
        proc.writeStdin(a);
        proc.closeStdin();
    }
}

#ifdef QPIPE_SECURE
void GpgAction::submitPassphrase(const QCA::SecureArray &a)
#else
void GpgAction::submitPassphrase(const QByteArray &a)
#endif
{
    if (!need_submitPassphrase)
        return;

    need_submitPassphrase = false;

#ifdef QPIPE_SECURE
    QCA::SecureArray b;
#else
    QByteArray b;
#endif
    // filter out newlines, since that's the delimiter used
    // to indicate a submitted passphrase
    b.resize(a.size());
    int at = 0;
    for (int n = 0; n < a.size(); ++n) {
        if (a[n] != '\n')
            b[at++] = a[n];
    }
    b.resize(at);

    // append newline
    b.resize(b.size() + 1);
    b[b.size() - 1] = '\n';
    proc.writeCommand(b);
}

QByteArray GpgAction::read()
{
    if (collectOutput)
        return QByteArray();

    QByteArray a = proc.readStdout();
    if (readText)
        a = readConv.update(a);
    if (!proc.isActive())
        a += readConv.final();
    return a;
}

void GpgAction::write(const QByteArray &in)
{
    if (!allowInput)
        return;

    QByteArray a = in;
    if (writeText)
        a = writeConv.update(in);

    if (useAux)
        proc.writeAux(a);
    else
        proc.writeStdin(a);
}

void GpgAction::endWrite()
{
    if (!allowInput)
        return;

    if (useAux)
        proc.closeAux();
    else
        proc.closeStdin();
}

void GpgAction::cardOkay()
{
    if (need_cardOkay) {
        need_cardOkay = false;
        submitCommand("\n");
    }
}

QString GpgAction::readDiagnosticText()
{
    QString s      = diagnosticText;
    diagnosticText = QString();
    return s;
}

void GpgAction::submitCommand(const QByteArray &a)
{
    proc.writeCommand(a);
}

// since str is taken as a value, it is ok to use the same variable for 'rest'
QString GpgAction::nextArg(QString str, QString *rest)
{
    int n = str.indexOf(QLatin1Char(' '));
    if (n == -1) {
        if (rest)
            *rest = QString();
        return str;
    } else {
        if (rest)
            *rest = str.mid(n + 1);
        return str.mid(0, n);
    }
}

void GpgAction::processStatusLine(const QString &line)
{
    appendDiagnosticText(QStringLiteral("{") + line + QStringLiteral("}"));
    ensureDTextEmit();

    if (!proc.isActive())
        return;

    QString s, rest;
    s = nextArg(line, &rest);

    if (s == QLatin1String("NODATA")) {
        // only set this if it'll make it better
        if (curError == GpgOp::ErrorUnknown)
            curError = GpgOp::ErrorFormat;
    } else if (s == QLatin1String("UNEXPECTED")) {
        if (curError == GpgOp::ErrorUnknown)
            curError = GpgOp::ErrorFormat;
    } else if (s == QLatin1String("EXPKEYSIG")) {
        curError = GpgOp::ErrorSignerExpired;
    } else if (s == QLatin1String("REVKEYSIG")) {
        curError = GpgOp::ErrorSignerRevoked;
    } else if (s == QLatin1String("EXPSIG")) {
        curError = GpgOp::ErrorSignatureExpired;
    } else if (s == QLatin1String("INV_RECP")) {
        const int r = nextArg(rest).toInt();

        if (curError == GpgOp::ErrorUnknown) {
            if (r == 10)
                curError = GpgOp::ErrorEncryptUntrusted;
            else if (r == 4)
                curError = GpgOp::ErrorEncryptRevoked;
            else if (r == 5)
                curError = GpgOp::ErrorEncryptExpired;
            else
                // due to GnuPG bug #1650
                // <https://bugs.g10code.com/gnupg/issue1650>
                // encrypting to expired and revoked keys will
                // not specify any reason for failing,
                // defaulting to this
                curError = GpgOp::ErrorEncryptInvalid;
        }
    } else if (s == QLatin1String("NO_SECKEY")) {
        output.encryptedToId = nextArg(rest);

        if (curError == GpgOp::ErrorUnknown)
            curError = GpgOp::ErrorDecryptNoKey;
    } else if (s == QLatin1String("DECRYPTION_OKAY")) {
        decryptGood = true;

        // message could be encrypted with several keys
        if (curError == GpgOp::ErrorDecryptNoKey)
            curError = GpgOp::ErrorUnknown;
    } else if (s == QLatin1String("SIG_CREATED")) {
        signGood = true;
    } else if (s == QLatin1String("USERID_HINT")) {
        passphraseKeyId = nextArg(rest);
    } else if (s == QLatin1String("GET_HIDDEN")) {
        QString arg = nextArg(rest);
        if (arg == QLatin1String("passphrase.enter") || arg == QLatin1String("passphrase.pin.ask")) {
            need_submitPassphrase = true;

            // for signal-safety, emit later
            QMetaObject::invokeMethod(this, "needPassphrase", Qt::QueuedConnection, Q_ARG(QString, passphraseKeyId));
        }
    } else if (s == QLatin1String("GET_LINE")) {
        QString arg = nextArg(rest);
        if (arg == QLatin1String("cardctrl.insert_card.okay")) {
            need_cardOkay = true;

            QMetaObject::invokeMethod(this, "needCard", Qt::QueuedConnection);
        }
    } else if (s == QLatin1String("GET_BOOL")) {
        QString arg = nextArg(rest);
        if (arg == QLatin1String("untrusted_key.override"))
            submitCommand("no\n");
    } else if (s == QLatin1String("GOOD_PASSPHRASE")) {
        badPassphrase = false;
    } else if (s == QLatin1String("BAD_PASSPHRASE")) {
        badPassphrase = true;
    } else if (s == QLatin1String("GOODSIG")) {
        output.wasSigned    = true;
        output.signerId     = nextArg(rest);
        output.verifyResult = GpgOp::VerifyGood;
    } else if (s == QLatin1String("BADSIG")) {
        output.wasSigned    = true;
        output.signerId     = nextArg(rest);
        output.verifyResult = GpgOp::VerifyBad;
    } else if (s == QLatin1String("ERRSIG")) {
        output.wasSigned       = true;
        const QStringList list = rest.split(QLatin1Char(' '), Qt::SkipEmptyParts);
        output.signerId        = list[0];
        output.timestamp       = getTimestamp(list[4]);
        output.verifyResult    = GpgOp::VerifyNoKey;
    } else if (s == QLatin1String("VALIDSIG")) {
        const QStringList list = rest.split(QLatin1Char(' '), Qt::SkipEmptyParts);
        output.timestamp       = getTimestamp(list[2]);
    }
}

void GpgAction::processResult(int code)
{
#ifdef GPG_PROFILE
    printf("<< launch: %d >>\n", timer.elapsed());
#endif

    // put stdout and stderr into QStrings

    QString outstr;
    QString errstr;

#ifdef Q_OS_WIN
    if (!utf8Output) {
        outstr = QString::fromLocal8Bit(buf_stdout);
        errstr = QString::fromLocal8Bit(buf_stderr);
    } else {
#endif
        outstr = QString::fromUtf8(buf_stdout);
        errstr = QString::fromUtf8(buf_stderr);
#ifdef Q_OS_WIN
    }
#endif

    if (collectOutput)
        appendDiagnosticText(QStringLiteral("stdout: [%1]").arg(outstr));
    appendDiagnosticText(QStringLiteral("stderr: [%1]").arg(errstr));
    ensureDTextEmit();

    if (badPassphrase) {
        output.errorCode = GpgOp::ErrorPassphrase;
    } else if (curError != GpgOp::ErrorUnknown) {
        output.errorCode = curError;
    } else if (code == 0) {
        if (input.op == GpgOp::Check) {
            const QStringList strList = outstr.split(QStringLiteral("\n"));
            foreach (const QString &str, strList) {
                if (!str.startsWith(QLatin1String("Home: ")))
                    continue;

                output.homeDir = str.section(QLatin1Char(' '), 1);
                break;
            }
            output.success = true;
        } else if (input.op == GpgOp::SecretKeyringFile || input.op == GpgOp::PublicKeyringFile) {
            if (findKeyringFilename(outstr, &output.keyringFile))
                output.success = true;
        } else if (input.op == GpgOp::SecretKeys || input.op == GpgOp::PublicKeys) {
            if (stringToKeyList(outstr, &output.keys, &output.keyringFile))
                output.success = true;
        } else
            output.success = true;
    } else {
        // decrypt and sign success based on status only.
        // this is mainly because gpg uses fatal return
        // values if there is trouble with gpg-agent, even
        // though the operation otherwise works.

        if (input.op == GpgOp::Decrypt && decryptGood)
            output.success = true;
        if (signing && signGood)
            output.success = true;

        // gpg will indicate failure for bad sigs, but we don't
        // consider this to be operation failure.

        bool signedMakesItGood = false;
        if (input.op == GpgOp::Verify || input.op == GpgOp::VerifyDetached)
            signedMakesItGood = true;

        if (signedMakesItGood && output.wasSigned)
            output.success = true;
    }

    emit finished();
}

void GpgAction::ensureDTextEmit()
{
    if (!dtextTimer.isActive())
        dtextTimer.start();
}

void GpgAction::t_dtext()
{
    emit readyReadDiagnosticText();
}

void GpgAction::proc_error(gpgQCAPlugin::GPGProc::Error e)
{
    QString str;
    if (e == GPGProc::FailedToStart)
        str = QStringLiteral("FailedToStart");
    else if (e == GPGProc::UnexpectedExit)
        str = QStringLiteral("UnexpectedExit");
    else if (e == GPGProc::ErrorWrite)
        str = QStringLiteral("ErrorWrite");

    appendDiagnosticText(QStringLiteral("GPG Process Error: %1").arg(str));
    ensureDTextEmit();

    output.errorCode = GpgOp::ErrorProcess;
    emit finished();
}

void GpgAction::proc_finished(int exitCode)
{
    appendDiagnosticText(QStringLiteral("GPG Process Finished: exitStatus=%1").arg(exitCode));
    ensureDTextEmit();

    processResult(exitCode);
}

void GpgAction::proc_readyReadStdout()
{
    if (collectOutput) {
        QByteArray a = proc.readStdout();
        if (readText)
            a = readConv.update(a);
        buf_stdout.append(a);
    } else
        emit readyRead();
}

void GpgAction::proc_readyReadStderr()
{
    buf_stderr.append(proc.readStderr());
}

void GpgAction::proc_readyReadStatusLines()
{
    const QStringList lines = proc.readStatusLines();
    for (int n = 0; n < lines.count(); ++n)
        processStatusLine(lines[n]);
}

void GpgAction::proc_bytesWrittenStdin(int bytes)
{
    if (!useAux) {
        int  actual = writeConv.writtenToActual(bytes);
        emit bytesWritten(actual);
    }
}

void GpgAction::proc_bytesWrittenAux(int bytes)
{
    if (useAux) {
        int  actual = writeConv.writtenToActual(bytes);
        emit bytesWritten(actual);
    }
}

void GpgAction::proc_bytesWrittenCommand(int)
{
    // don't care about this
}

void GpgAction::proc_debug(const QString &str)
{
    appendDiagnosticText(QStringLiteral("GPGProc: ") + str);
    ensureDTextEmit();
}

void GpgAction::appendDiagnosticText(const QString &line)
{
#ifdef GPGOP_DEBUG
    printf("%s\n", qPrintable(line));
#endif
    diagnosticText += line;
}

} // end namespace gpgQCAPlugin