File: mypgpkeycontext.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 (202 lines) | stat: -rw-r--r-- 5,494 bytes parent folder | download | duplicates (4)
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
#include "mypgpkeycontext.h"
#include "gpgop.h"
#include "utils.h"
#include <QDir>
#include <QTemporaryFile>

using namespace QCA;

namespace gpgQCAPlugin {

MyPGPKeyContext::MyPGPKeyContext(Provider *p)
    : PGPKeyContext(p)
{
    // zero out the props
    _props.isSecret  = false;
    _props.inKeyring = true;
    _props.isTrusted = false;
}

Provider::Context *MyPGPKeyContext::clone() const
{
    return new MyPGPKeyContext(*this);
}

const PGPKeyContextProps *MyPGPKeyContext::props() const
{
    return &_props;
}

QByteArray MyPGPKeyContext::toBinary() const
{
    if (_props.inKeyring) {
        GpgOp gpg(find_bin());
        gpg.setAsciiFormat(false);
        gpg.doExport(_props.keyId);
        gpg_waitForFinished(&gpg);
        gpg_keyStoreLog(gpg.readDiagnosticText());
        if (!gpg.success())
            return QByteArray();
        return gpg.read();
    } else
        return cacheExportBinary;
}

ConvertResult MyPGPKeyContext::fromBinary(const QByteArray &a)
{
    GpgOp::Key key;
    bool       sec = false;

    // temporary keyrings
    QString pubname, secname;

    QTemporaryFile pubtmp(QDir::tempPath() + QLatin1String("/qca_gnupg_tmp.XXXXXX.gpg"));
    if (!pubtmp.open())
        return ErrorDecode;

    QTemporaryFile sectmp(QDir::tempPath() + QLatin1String("/qca_gnupg_tmp.XXXXXX.gpg"));
    if (!sectmp.open())
        return ErrorDecode;

    pubname = pubtmp.fileName();
    secname = sectmp.fileName();

    // we turn off autoRemove so that we can close the files
    //   without them getting deleted
    pubtmp.setAutoRemove(false);
    sectmp.setAutoRemove(false);
    pubtmp.close();
    sectmp.close();

    // import key into temporary keyring
    GpgOp gpg(find_bin());
    gpg.setKeyrings(pubname, secname);
    gpg.doImport(a);
    gpg_waitForFinished(&gpg);
    gpg_keyStoreLog(gpg.readDiagnosticText());
    // comment this out.  apparently gpg will report failure for
    //   an import if there are trust issues, even though the
    //   key actually did get imported
    /*if(!gpg.success())
      {
      cleanup_temp_keyring(pubname);
      cleanup_temp_keyring(secname);
      return ErrorDecode;
      }*/

    // now extract the key from gpg like normal

    // is it a public key?
    gpg.doPublicKeys();
    gpg_waitForFinished(&gpg);
    gpg_keyStoreLog(gpg.readDiagnosticText());
    if (!gpg.success()) {
        cleanup_temp_keyring(pubname);
        cleanup_temp_keyring(secname);
        return ErrorDecode;
    }

    const GpgOp::KeyList pubkeys = gpg.keys();
    if (!pubkeys.isEmpty()) {
        key = pubkeys.first();
    } else {
        // is it a secret key?
        gpg.doSecretKeys();
        gpg_waitForFinished(&gpg);
        gpg_keyStoreLog(gpg.readDiagnosticText());
        if (!gpg.success()) {
            cleanup_temp_keyring(pubname);
            cleanup_temp_keyring(secname);
            return ErrorDecode;
        }

        const GpgOp::KeyList seckeys = gpg.keys();
        if (!seckeys.isEmpty()) {
            key = seckeys.first();
            sec = true;
        } else {
            // no keys found
            cleanup_temp_keyring(pubname);
            cleanup_temp_keyring(secname);
            return ErrorDecode;
        }
    }

    // export binary/ascii and cache

    gpg.setAsciiFormat(false);
    gpg.doExport(key.keyItems.first().id);
    gpg_waitForFinished(&gpg);
    gpg_keyStoreLog(gpg.readDiagnosticText());
    if (!gpg.success()) {
        cleanup_temp_keyring(pubname);
        cleanup_temp_keyring(secname);
        return ErrorDecode;
    }
    cacheExportBinary = gpg.read();

    gpg.setAsciiFormat(true);
    gpg.doExport(key.keyItems.first().id);
    gpg_waitForFinished(&gpg);
    gpg_keyStoreLog(gpg.readDiagnosticText());
    if (!gpg.success()) {
        cleanup_temp_keyring(pubname);
        cleanup_temp_keyring(secname);
        return ErrorDecode;
    }
    cacheExportAscii = QString::fromLocal8Bit(gpg.read());

    // all done

    cleanup_temp_keyring(pubname);
    cleanup_temp_keyring(secname);

    set(key, sec, false, false);
    return ConvertGood;
}

QString MyPGPKeyContext::toAscii() const
{
    if (_props.inKeyring) {
        GpgOp gpg(find_bin());
        gpg.setAsciiFormat(true);
        gpg.doExport(_props.keyId);
        gpg_waitForFinished(&gpg);
        gpg_keyStoreLog(gpg.readDiagnosticText());
        if (!gpg.success())
            return QString();
        return QString::fromLocal8Bit(gpg.read());
    } else {
        return cacheExportAscii;
    }
}

ConvertResult MyPGPKeyContext::fromAscii(const QString &s)
{
    // GnuPG does ascii/binary detection for imports, so for
    //   simplicity we consider an ascii import to just be a
    //   binary import that happens to be comprised of ascii
    return fromBinary(s.toLocal8Bit());
}

void MyPGPKeyContext::set(const GpgOp::Key &i, bool isSecret, bool inKeyring, bool isTrusted)
{
    const GpgOp::KeyItem &ki = i.keyItems.first();

    _props.keyId          = ki.id;
    _props.userIds        = i.userIds;
    _props.isSecret       = isSecret;
    _props.creationDate   = ki.creationDate;
    _props.expirationDate = ki.expirationDate;
    _props.fingerprint    = ki.fingerprint.toLower();
    _props.inKeyring      = inKeyring;
    _props.isTrusted      = isTrusted;
}

void MyPGPKeyContext::cleanup_temp_keyring(const QString &name)
{
    QFile::remove(name);
    QFile::remove(name + QLatin1Char('~')); // remove possible backup file
}

} // end namespace gpgQCAPlugin