summaryrefslogtreecommitdiffstats
path: root/certmanager/lib/backends/kpgp/kpgpkeylistjob.cpp
blob: 4c328adeef48e3c934a4c8aaaa31c9fd222b47f7 (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
/*
    kpgpkeylistjob.cpp

    This file is part of libkleopatra, the KDE keymanagement library
    Copyright (c) 2004 Ingo Kloecker <kloecker@kde.org>

    Libkleopatra 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 2 of the
    License, or (at your option) any later version.

    Libkleopatra 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 this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA

    In addition, as a special exception, the copyright holders give
    permission to link the code of this program with any edition of
    the Qt library by Trolltech AS, Norway (or with modified versions
    of Qt that use the same license as Qt), and distribute linked
    combinations including the two.  You must obey the GNU General
    Public License in all respects for all of the code used other than
    Qt.  If you modify this file, you may extend this exception to
    your version of the file, but you are not obligated to do so.  If
    you do not wish to do so, delete this exception statement from
    your version.
*/

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "kpgpkeylistjob.h"

#include <kpgpbase.h>
#include <kpgpkey.h>

#include <gpgmepp/key.h>
#include <gpgmepp/keylistresult.h>

#include <gpgme.h>

#include <qtimer.h>

#include <stdlib.h>
#include <string.h>
#include <assert.h>

Kleo::KpgpKeyListJob::KpgpKeyListJob( Kpgp::Base * pgpBase )
  : KeyListJob( 0, "Kleo::KpgpKeyListJob" ),
    mPgpBase( pgpBase )
{
}

Kleo::KpgpKeyListJob::~KpgpKeyListJob() {
}

// the following function is a verbatim copy from gpgme/key.c
static char *
set_user_id_part (char *tail, const char *buf, size_t len)
{
  while (len && (buf[len - 1] == ' ' || buf[len - 1] == '\t'))
    len--;
  for (; len; len--)
    *tail++ = *buf++;
  *tail++ = 0;
  return tail;
}

// the following function is a verbatim copy from gpgme/key.c
static void
parse_user_id (char *src, char **name, char **email,
	       char **comment, char *tail)
{
  const char *start = NULL;
  int in_name = 0;
  int in_email = 0;
  int in_comment = 0;

  while (*src)
    {
      if (in_email)
	{
	  if (*src == '<')
	    /* Not legal but anyway.  */
	    in_email++;
	  else if (*src == '>')
	    {
	      if (!--in_email && !*email)
		{
		  *email = tail;
		  tail = set_user_id_part (tail, start, src - start);
		}
	    }
	}
      else if (in_comment)
	{
	  if (*src == '(')
	    in_comment++;
	  else if (*src == ')')
	    {
	      if (!--in_comment && !*comment)
		{
		  *comment = tail;
		  tail = set_user_id_part (tail, start, src - start);
		}
	    }
	}
      else if (*src == '<')
	{
	  if (in_name)
	    {
	      if (!*name)
		{
		  *name = tail;
		  tail = set_user_id_part (tail, start, src - start);
		}
	      in_name = 0;
	    }
	  in_email = 1;
	  start = src + 1;
	}
      else if (*src == '(')
	{
	  if (in_name)
	    {
	      if (!*name)
		{
		  *name = tail;
		  tail = set_user_id_part (tail, start, src - start);
		}
	      in_name = 0;
	    }
	  in_comment = 1;
	  start = src + 1;
	}
      else if (!in_name && *src != ' ' && *src != '\t')
	{
	  in_name = 1;
	  start = src;
	}
      src++;
    }

  if (in_name)
    {
      if (!*name)
	{
	  *name = tail;
	  tail = set_user_id_part (tail, start, src - start);
	}
    }

  /* Let unused parts point to an EOS.  */
  tail--;
  if (!*name)
    *name = tail;
  if (!*email)
    *email = tail;
  if (!*comment)
    *comment = tail;
}

gpgme_user_id_t KpgpUserID2GPGMEUserID( const Kpgp::UserID * kUserId )
{
  // inspired by _gpgme_key_append_name

  const QCString text = kUserId->text().utf8();
  const int src_len = text.length();

  gpgme_user_id_t uid;
  /* Allocate enough memory for the _gpgme_user_id struct, for the actual user
     id (the text) and for the parsed version. */
  uid = (gpgme_user_id_t) malloc( sizeof( *uid ) + 2 * src_len + 3 );
  memset( uid, 0, sizeof *uid );
  uid->revoked = kUserId->revoked();
  uid->invalid = kUserId->invalid();
  uid->validity = (gpgme_validity_t) kUserId->validity();

  uid->uid = ((char *) uid) + sizeof (*uid);
  char *dst = uid->uid;
  memcpy( dst, text.data(), src_len + 1 );

  dst += src_len + 1;
  parse_user_id( uid->uid, &uid->name, &uid->email,
                 &uid->comment, dst );

  return uid;
}

gpgme_subkey_t KpgpSubkey2GPGMESubKey( const Kpgp::Subkey * kSubkey )
{
  gpgme_subkey_t subkey;

  const QCString fpr = kSubkey->fingerprint();
  const unsigned int fpr_len = fpr.length();
  const QCString keyId = kSubkey->longKeyID();

  subkey = (gpgme_subkey_t) calloc( 1, sizeof( *subkey ) + fpr_len + 1 );
  subkey->revoked = kSubkey->revoked();
  subkey->expired = kSubkey->expired();
  subkey->disabled = kSubkey->disabled();
  subkey->invalid = kSubkey->invalid();
  subkey->can_encrypt = kSubkey->canEncrypt();
  subkey->can_sign = kSubkey->canSign();
  subkey->can_certify = kSubkey->canCertify();
  subkey->secret = kSubkey->secret();
  subkey->pubkey_algo = (gpgme_pubkey_algo_t) kSubkey->keyAlgorithm();
  subkey->length = kSubkey->keyLength();
  subkey->keyid = subkey->_keyid;
  memcpy( subkey->_keyid, keyId.data(), keyId.length() + 1 );
  subkey->fpr = ((char *) subkey) + sizeof( *subkey );
  memcpy( subkey->fpr, fpr.data(), fpr_len + 1 );
  subkey->timestamp = kSubkey->creationDate();
  subkey->expires = kSubkey->expirationDate();

  return subkey;
}

gpgme_key_t KpgpKey2gpgme_key( const Kpgp::Key * kKey )
{
  gpgme_key_t key;

  key = (gpgme_key_t) calloc( 1, sizeof( *key ) );
  key->revoked = kKey->revoked();
  key->expired = kKey->expired();
  key->disabled = kKey->disabled();
  key->invalid = kKey->invalid();
  key->can_encrypt = kKey->canEncrypt();
  key->can_sign = kKey->canSign();
  key->can_certify = kKey->canCertify();
  key->secret = kKey->secret();
  key->protocol = GPGME_PROTOCOL_OpenPGP;
  key->owner_trust = GPGME_VALIDITY_UNKNOWN; // FIXME?

  Kpgp::UserIDList kUserIDs = kKey->userIDs();
  for ( Kpgp::UserIDListIterator it( kUserIDs ); it.current(); ++it ) {
    gpgme_user_id_t uid = KpgpUserID2GPGMEUserID( *it );
    if ( !key->uids )
      key->uids = uid;
    if ( key->_last_uid )
      key->_last_uid->next = uid;
    key->_last_uid = uid;
  }

  Kpgp::SubkeyList kSubkeys = kKey->subkeys();
  for ( Kpgp::SubkeyListIterator it( kSubkeys ); it.current(); ++it ) {
    gpgme_subkey_t subkey = KpgpSubkey2GPGMESubKey( *it );
    if (!key->subkeys)
      key->subkeys = subkey;
    if (key->_last_subkey)
      key->_last_subkey->next = subkey;
    key->_last_subkey = subkey;
  }

  return key;
}

GpgME::Error Kleo::KpgpKeyListJob::start( const QStringList & patterns,
                                          bool secretOnly ) {
  mPatterns = patterns;
  mSecretOnly = secretOnly;
  QTimer::singleShot( 0, this, SLOT( slotDoIt() ) );
  return GpgME::Error( 0 );
}

void Kleo::KpgpKeyListJob::slotDoIt() {
  std::vector<GpgME::Key> keys;
  GpgME::KeyListResult res = exec( mPatterns, mSecretOnly, keys );
  for ( std::vector<GpgME::Key>::const_iterator it = keys.begin();
        it != keys.end(); ++it )
    emit nextKey( *it );
  emit done();
  emit result( res );
  deleteLater();
}

GpgME::KeyListResult Kleo::KpgpKeyListJob::exec( const QStringList & patterns,
                                                 bool secretOnly,
                                                 std::vector<GpgME::Key> & keys ) {
  Kpgp::KeyList kKeys;
  if ( secretOnly )
    kKeys = mPgpBase->secretKeys( patterns );
  else
    kKeys = mPgpBase->publicKeys( patterns );

  keys.clear();
  for ( Kpgp::KeyListIterator it( kKeys ); it.current(); ++it ) {
    keys.push_back( GpgME::Key( KpgpKey2gpgme_key(*it), true ) );
  }

  _gpgme_op_keylist_result res;
  res.truncated = 0; // key list is not truncated
  res._unused = 0;

  return GpgME::KeyListResult( GpgME::Error( 0 ), res );
}

#include "kpgpkeylistjob.moc"