~ubuntu-branches/ubuntu/lucid/gpgme1.0/lucid

« back to all changes in this revision

Viewing changes to gpgme/genkey.c

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman, Bhavani Shankar, Scott Kitterman
  • Date: 2008-12-31 02:39:33 UTC
  • mfrom: (1.1.6 upstream) (3.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20081231023933-zi5r2w9vf7fdz0x9
Tags: 1.1.8-1ubuntu1
[ Bhavani Shankar ]
* Merge from debian unstable, remaining changes: LP: #311666
  - debian/rules: enable tests

[ Scott Kitterman ]
* Re-enable testsuite on armel since it's no longer a first build 

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* genkey.c - Key generation.
2
 
   Copyright (C) 2000 Werner Koch (dd9jn)
3
 
   Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
4
 
 
5
 
   This file is part of GPGME.
6
 
 
7
 
   GPGME is free software; you can redistribute it and/or modify it
8
 
   under the terms of the GNU Lesser General Public License as
9
 
   published by the Free Software Foundation; either version 2.1 of
10
 
   the License, or (at your option) any later version.
11
 
   
12
 
   GPGME is distributed in the hope that it will be useful, but
13
 
   WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 
   Lesser General Public License for more details.
16
 
   
17
 
   You should have received a copy of the GNU Lesser General Public
18
 
   License along with this program; if not, write to the Free Software
19
 
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20
 
   02111-1307, USA.  */
21
 
 
22
 
#if HAVE_CONFIG_H
23
 
#include <config.h>
24
 
#endif
25
 
#include <stdlib.h>
26
 
#include <string.h>
27
 
#include <errno.h>
28
 
 
29
 
#include "gpgme.h"
30
 
#include "context.h"
31
 
#include "ops.h"
32
 
 
33
 
 
34
 
typedef struct
35
 
{
36
 
  struct _gpgme_op_genkey_result result;
37
 
 
38
 
  /* The key parameters passed to the crypto engine.  */
39
 
  gpgme_data_t key_parameter;
40
 
} *op_data_t;
41
 
 
42
 
 
43
 
static void
44
 
release_op_data (void *hook)
45
 
{
46
 
  op_data_t opd = (op_data_t) hook;
47
 
  
48
 
  if (opd->result.fpr)
49
 
    free (opd->result.fpr);
50
 
  if (opd->key_parameter)
51
 
    gpgme_data_release (opd->key_parameter);
52
 
}
53
 
 
54
 
 
55
 
gpgme_genkey_result_t
56
 
gpgme_op_genkey_result (gpgme_ctx_t ctx)
57
 
{
58
 
  void *hook;
59
 
  op_data_t opd;
60
 
  gpgme_error_t err;
61
 
 
62
 
  err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, -1, NULL);
63
 
  opd = hook;
64
 
  if (err || !opd)
65
 
    return NULL;
66
 
 
67
 
  return &opd->result;
68
 
}
69
 
 
70
 
 
71
 
static gpgme_error_t
72
 
genkey_status_handler (void *priv, gpgme_status_code_t code, char *args)
73
 
{
74
 
  gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
75
 
  gpgme_error_t err;
76
 
  void *hook;
77
 
  op_data_t opd;
78
 
 
79
 
  /* Pipe the status code through the progress status handler.  */
80
 
  err = _gpgme_progress_status_handler (ctx, code, args);
81
 
  if (err)
82
 
    return err;
83
 
 
84
 
  err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, -1, NULL);
85
 
  opd = hook;
86
 
  if (err)
87
 
    return err;
88
 
 
89
 
  switch (code)
90
 
    {
91
 
    case GPGME_STATUS_KEY_CREATED:
92
 
      if (args && *args)
93
 
        {
94
 
          if (*args == 'B' || *args == 'P')
95
 
            opd->result.primary = 1;
96
 
          if (*args == 'B' || *args == 'S')
97
 
            opd->result.sub = 1;
98
 
          if (args[1] == ' ')
99
 
            {
100
 
              if (opd->result.fpr)
101
 
                free (opd->result.fpr);
102
 
              opd->result.fpr = strdup (&args[2]);
103
 
              if (!opd->result.fpr)
104
 
                return gpg_error_from_errno (errno);
105
 
            }
106
 
        }
107
 
      break;
108
 
 
109
 
    case GPGME_STATUS_EOF:
110
 
      /* FIXME: Should return some more useful error value.  */
111
 
      if (!opd->result.primary && !opd->result.sub)
112
 
        return gpg_error (GPG_ERR_GENERAL);
113
 
      break;
114
 
 
115
 
    default:
116
 
      break;
117
 
    }
118
 
  return 0;
119
 
}
120
 
 
121
 
 
122
 
static gpgme_error_t
123
 
get_key_parameter (const char *parms, gpgme_data_t *key_parameter)
124
 
{
125
 
  const char *content;
126
 
  const char *attrib;
127
 
  const char *endtag;
128
 
 
129
 
  /* Extract the key parameter from the XML structure.  */
130
 
  parms = strstr (parms, "<GnupgKeyParms ");
131
 
  if (!parms)
132
 
    return gpg_error (GPG_ERR_INV_VALUE);
133
 
 
134
 
  content = strchr (parms, '>');
135
 
  if (!content)
136
 
    return gpg_error (GPG_ERR_INV_VALUE);
137
 
  content++;
138
 
 
139
 
  attrib = strstr (parms, "format=\"internal\"");
140
 
  if (!attrib || attrib >= content)
141
 
    return gpg_error (GPG_ERR_INV_VALUE);
142
 
 
143
 
  endtag = strstr (content, "</GnupgKeyParms>");
144
 
  /* FIXME: Check that there are no control statements inside.  */
145
 
  while (content[0] == '\n'
146
 
         || (content[0] == '\r' && content[1] == '\n'))
147
 
    content++;
148
 
 
149
 
  return gpgme_data_new_from_mem (key_parameter, content,
150
 
                                  endtag - content, 1);
151
 
}
152
 
 
153
 
 
154
 
static gpgme_error_t
155
 
genkey_start (gpgme_ctx_t ctx, int synchronous, const char *parms,
156
 
              gpgme_data_t pubkey, gpgme_data_t seckey)
157
 
{
158
 
  gpgme_error_t err;
159
 
  void *hook;
160
 
  op_data_t opd;
161
 
  err = _gpgme_op_reset (ctx, synchronous);
162
 
  if (err)
163
 
    return err;
164
 
  
165
 
  err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook,
166
 
                               sizeof (*opd), release_op_data);
167
 
  opd = hook;
168
 
  if (err)
169
 
    return err;
170
 
 
171
 
  err = get_key_parameter (parms, &opd->key_parameter);
172
 
  if (err)
173
 
    return err;
174
 
 
175
 
  _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx);
176
 
 
177
 
  return _gpgme_engine_op_genkey (ctx->engine, opd->key_parameter,
178
 
                                  ctx->use_armor, pubkey, seckey);
179
 
}
180
 
 
181
 
 
182
 
/* Generate a new keypair and add it to the keyring.  PUBKEY and
183
 
   SECKEY should be null for now.  PARMS specifies what keys should be
184
 
   generated.  */
185
 
gpgme_error_t
186
 
gpgme_op_genkey_start (gpgme_ctx_t ctx, const char *parms,
187
 
                       gpgme_data_t pubkey, gpgme_data_t seckey)
188
 
{
189
 
  return genkey_start (ctx, 0, parms, pubkey, seckey);
190
 
}
191
 
 
192
 
 
193
 
/* Generate a new keypair and add it to the keyring.  PUBKEY and
194
 
   SECKEY should be null for now.  PARMS specifies what keys should be
195
 
   generated.  */
196
 
gpgme_error_t
197
 
gpgme_op_genkey (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey,
198
 
                 gpgme_data_t seckey)
199
 
{
200
 
  gpgme_error_t err;
201
 
 
202
 
  err = genkey_start (ctx, 1, parms, pubkey, seckey);
203
 
  if (!err)
204
 
    err = _gpgme_wait_one (ctx);
205
 
  return err;
206
 
}