2007-08-23 19:41:22 +02:00
|
|
|
/* export.c - Export certificates and private keys.
|
2009-03-06 18:31:27 +01:00
|
|
|
* Copyright (C) 2002, 2003, 2004, 2007, 2009 Free Software Foundation, Inc.
|
2003-08-05 19:11:04 +02:00
|
|
|
*
|
|
|
|
* This file is part of GnuPG.
|
|
|
|
*
|
|
|
|
* GnuPG is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
2007-07-04 21:49:40 +02:00
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
2003-08-05 19:11:04 +02:00
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* GnuPG 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
|
2007-07-04 21:49:40 +02:00
|
|
|
* along with this program; if not, see <http://www.gnu.org/licenses/>.
|
2003-08-05 19:11:04 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include "gpgsm.h"
|
|
|
|
#include <gcrypt.h>
|
|
|
|
#include <ksba.h>
|
|
|
|
|
|
|
|
#include "keydb.h"
|
2004-12-06 19:28:56 +01:00
|
|
|
#include "exechelp.h"
|
2004-02-19 17:26:32 +01:00
|
|
|
#include "i18n.h"
|
2007-08-22 12:55:07 +02:00
|
|
|
#include "sysutils.h"
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2004-03-03 09:55:50 +01:00
|
|
|
/* A table to store a fingerprint as used in a duplicates table. We
|
2009-03-06 18:31:27 +01:00
|
|
|
don't need to hash here because a fingerprint is already a perfect
|
2004-03-02 09:02:47 +01:00
|
|
|
hash value. This we use the most significant bits to index the
|
|
|
|
table and then use a linked list for the overflow. Possible
|
2009-03-06 18:31:27 +01:00
|
|
|
enhancement for very large number of certificates: Add a second
|
|
|
|
level table and then resort to a linked list. */
|
2004-03-02 09:02:47 +01:00
|
|
|
struct duptable_s
|
|
|
|
{
|
|
|
|
struct duptable_s *next;
|
|
|
|
|
|
|
|
/* Note that we only need to store 19 bytes because the first byte
|
|
|
|
is implictly given by the table index (we require at least 8
|
|
|
|
bits). */
|
|
|
|
unsigned char fpr[19];
|
|
|
|
};
|
|
|
|
typedef struct duptable_s *duptable_t;
|
|
|
|
#define DUPTABLE_BITS 12
|
|
|
|
#define DUPTABLE_SIZE (1 << DUPTABLE_BITS)
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2007-03-19 16:44:59 +01:00
|
|
|
static void print_short_info (ksba_cert_t cert, FILE *fp, estream_t stream);
|
2004-09-29 15:50:31 +02:00
|
|
|
static gpg_error_t export_p12 (ctrl_t ctrl,
|
|
|
|
const unsigned char *certimg, size_t certimglen,
|
2004-02-19 17:26:32 +01:00
|
|
|
const char *prompt, const char *keygrip,
|
|
|
|
FILE **retfp);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
|
2004-03-02 09:02:47 +01:00
|
|
|
/* Create a table used to indetify duplicated certificates. */
|
|
|
|
static duptable_t *
|
|
|
|
create_duptable (void)
|
|
|
|
{
|
|
|
|
return xtrycalloc (DUPTABLE_SIZE, sizeof (duptable_t));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
destroy_duptable (duptable_t *table)
|
|
|
|
{
|
|
|
|
int idx;
|
|
|
|
duptable_t t, t2;
|
|
|
|
|
|
|
|
if (table)
|
|
|
|
{
|
|
|
|
for (idx=0; idx < DUPTABLE_SIZE; idx++)
|
|
|
|
for (t = table[idx]; t; t = t2)
|
|
|
|
{
|
|
|
|
t2 = t->next;
|
|
|
|
xfree (t);
|
|
|
|
}
|
|
|
|
xfree (table);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Insert the 20 byte fingerprint FPR into TABLE. Sets EXITS to true
|
|
|
|
if the fingerprint already exists in the table. */
|
|
|
|
static gpg_error_t
|
|
|
|
insert_duptable (duptable_t *table, unsigned char *fpr, int *exists)
|
|
|
|
{
|
|
|
|
size_t idx;
|
|
|
|
duptable_t t;
|
|
|
|
|
|
|
|
*exists = 0;
|
|
|
|
idx = fpr[0];
|
2004-03-03 09:55:50 +01:00
|
|
|
#if DUPTABLE_BITS > 16 || DUPTABLE_BITS < 8
|
|
|
|
#error cannot handle a table larger than 16 bits or smaller than 8 bits
|
2004-03-02 09:02:47 +01:00
|
|
|
#elif DUPTABLE_BITS > 8
|
|
|
|
idx <<= (DUPTABLE_BITS - 8);
|
|
|
|
idx |= (fpr[1] & ~(~0 << 4));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
for (t = table[idx]; t; t = t->next)
|
|
|
|
if (!memcmp (t->fpr, fpr+1, 19))
|
|
|
|
break;
|
|
|
|
if (t)
|
|
|
|
{
|
|
|
|
*exists = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Insert that fingerprint. */
|
|
|
|
t = xtrymalloc (sizeof *t);
|
|
|
|
if (!t)
|
2006-09-14 18:50:33 +02:00
|
|
|
return gpg_error_from_syserror ();
|
2004-03-02 09:02:47 +01:00
|
|
|
memcpy (t->fpr, fpr+1, 19);
|
|
|
|
t->next = table[idx];
|
|
|
|
table[idx] = t;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2007-03-19 16:44:59 +01:00
|
|
|
/* Export all certificates or just those given in NAMES. If STREAM is
|
|
|
|
not NULL the output is send to this extended stream. */
|
2003-08-05 19:11:04 +02:00
|
|
|
void
|
2007-03-19 16:44:59 +01:00
|
|
|
gpgsm_export (ctrl_t ctrl, strlist_t names, FILE *fp, estream_t stream)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2004-03-02 09:02:47 +01:00
|
|
|
KEYDB_HANDLE hd = NULL;
|
2003-08-05 19:11:04 +02:00
|
|
|
KEYDB_SEARCH_DESC *desc = NULL;
|
|
|
|
int ndesc;
|
|
|
|
Base64Context b64writer = NULL;
|
2003-12-17 13:28:24 +01:00
|
|
|
ksba_writer_t writer;
|
2006-10-02 13:54:35 +02:00
|
|
|
strlist_t sl;
|
2003-12-17 13:28:24 +01:00
|
|
|
ksba_cert_t cert = NULL;
|
2003-08-05 19:11:04 +02:00
|
|
|
int rc=0;
|
|
|
|
int count = 0;
|
|
|
|
int i;
|
2004-03-02 09:02:47 +01:00
|
|
|
duptable_t *dtable;
|
|
|
|
|
|
|
|
|
|
|
|
dtable = create_duptable ();
|
|
|
|
if (!dtable)
|
|
|
|
{
|
|
|
|
log_error ("creating duplicates table failed: %s\n", strerror (errno));
|
|
|
|
goto leave;
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
hd = keydb_new (0);
|
|
|
|
if (!hd)
|
|
|
|
{
|
|
|
|
log_error ("keydb_new failed\n");
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!names)
|
|
|
|
ndesc = 1;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (sl=names, ndesc=0; sl; sl = sl->next, ndesc++)
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc = xtrycalloc (ndesc, sizeof *desc);
|
|
|
|
if (!ndesc)
|
|
|
|
{
|
|
|
|
log_error ("allocating memory for export failed: %s\n",
|
2006-09-06 18:35:52 +02:00
|
|
|
gpg_strerror (out_of_core ()));
|
2003-08-05 19:11:04 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!names)
|
|
|
|
desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
for (ndesc=0, sl=names; sl; sl = sl->next)
|
|
|
|
{
|
|
|
|
rc = keydb_classify_name (sl->d, desc+ndesc);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("key `%s' not found: %s\n",
|
|
|
|
sl->d, gpg_strerror (rc));
|
|
|
|
rc = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ndesc++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-03-06 18:31:27 +01:00
|
|
|
/* If all specifications are done by fingerprint or keygrip, we
|
|
|
|
switch to ephemeral mode so that _all_ currently available and
|
|
|
|
matching certificates are exported. */
|
2003-08-05 19:11:04 +02:00
|
|
|
if (names && ndesc)
|
|
|
|
{
|
|
|
|
for (i=0; (i < ndesc
|
|
|
|
&& (desc[i].mode == KEYDB_SEARCH_MODE_FPR
|
|
|
|
|| desc[i].mode == KEYDB_SEARCH_MODE_FPR20
|
2009-03-06 18:31:27 +01:00
|
|
|
|| desc[i].mode == KEYDB_SEARCH_MODE_FPR16
|
|
|
|
|| desc[i].mode == KEYDB_SEARCH_MODE_KEYGRIP)); i++)
|
2003-08-05 19:11:04 +02:00
|
|
|
;
|
|
|
|
if (i == ndesc)
|
|
|
|
keydb_set_ephemeral (hd, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!(rc = keydb_search (hd, desc, ndesc)))
|
|
|
|
{
|
2004-03-02 09:02:47 +01:00
|
|
|
unsigned char fpr[20];
|
|
|
|
int exists;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
if (!names)
|
|
|
|
desc[0].mode = KEYDB_SEARCH_MODE_NEXT;
|
|
|
|
|
|
|
|
rc = keydb_get_cert (hd, &cert);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("keydb_get_cert failed: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2004-03-02 09:02:47 +01:00
|
|
|
gpgsm_get_fingerprint (cert, 0, fpr, NULL);
|
|
|
|
rc = insert_duptable (dtable, fpr, &exists);
|
|
|
|
if (rc)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2009-03-06 18:31:27 +01:00
|
|
|
log_error ("inserting into duplicates table failed: %s\n",
|
2004-03-02 09:02:47 +01:00
|
|
|
gpg_strerror (rc));
|
2003-08-05 19:11:04 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2004-03-03 09:55:50 +01:00
|
|
|
if (!exists && count && !ctrl->create_pem)
|
|
|
|
{
|
|
|
|
log_info ("exporting more than one certificate "
|
|
|
|
"is not possible in binary mode\n");
|
|
|
|
log_info ("ignoring other certificates\n");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2004-03-02 09:02:47 +01:00
|
|
|
if (!exists)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2004-03-02 09:02:47 +01:00
|
|
|
const unsigned char *image;
|
|
|
|
size_t imagelen;
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2004-03-02 09:02:47 +01:00
|
|
|
image = ksba_cert_get_image (cert, &imagelen);
|
|
|
|
if (!image)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2004-03-02 09:02:47 +01:00
|
|
|
log_error ("ksba_cert_get_image failed\n");
|
2003-08-05 19:11:04 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-03-02 09:02:47 +01:00
|
|
|
if (ctrl->create_pem)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
2004-03-02 09:02:47 +01:00
|
|
|
if (count)
|
2007-03-19 16:44:59 +01:00
|
|
|
{
|
|
|
|
if (stream)
|
|
|
|
es_putc ('\n', stream);
|
|
|
|
else
|
|
|
|
putc ('\n', fp);
|
|
|
|
}
|
|
|
|
print_short_info (cert, fp, stream);
|
|
|
|
if (stream)
|
|
|
|
es_putc ('\n', stream);
|
|
|
|
else
|
2004-03-02 09:02:47 +01:00
|
|
|
putc ('\n', fp);
|
|
|
|
}
|
|
|
|
count++;
|
|
|
|
|
|
|
|
if (!b64writer)
|
|
|
|
{
|
|
|
|
ctrl->pem_name = "CERTIFICATE";
|
2007-03-19 16:44:59 +01:00
|
|
|
rc = gpgsm_create_writer (&b64writer, ctrl, fp, stream, &writer);
|
2004-03-02 09:02:47 +01:00
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("can't create writer: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = ksba_writer_write (writer, image, imagelen);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("write error: %s\n", gpg_strerror (rc));
|
2003-08-05 19:11:04 +02:00
|
|
|
goto leave;
|
|
|
|
}
|
2004-03-02 09:02:47 +01:00
|
|
|
|
|
|
|
if (ctrl->create_pem)
|
|
|
|
{
|
|
|
|
/* We want one certificate per PEM block */
|
|
|
|
rc = gpgsm_finish_writer (b64writer);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("write failed: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
gpgsm_destroy_writer (b64writer);
|
|
|
|
b64writer = NULL;
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
2004-03-02 09:02:47 +01:00
|
|
|
|
2003-08-05 19:11:04 +02:00
|
|
|
ksba_cert_release (cert);
|
|
|
|
cert = NULL;
|
|
|
|
}
|
|
|
|
if (rc && rc != -1)
|
|
|
|
log_error ("keydb_search failed: %s\n", gpg_strerror (rc));
|
|
|
|
else if (b64writer)
|
|
|
|
{
|
|
|
|
rc = gpgsm_finish_writer (b64writer);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("write failed: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
leave:
|
|
|
|
gpgsm_destroy_writer (b64writer);
|
|
|
|
ksba_cert_release (cert);
|
|
|
|
xfree (desc);
|
|
|
|
keydb_release (hd);
|
2004-03-02 09:02:47 +01:00
|
|
|
destroy_duptable (dtable);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
/* Export a certificates and its private key. */
|
|
|
|
void
|
|
|
|
gpgsm_p12_export (ctrl_t ctrl, const char *name, FILE *fp)
|
|
|
|
{
|
|
|
|
KEYDB_HANDLE hd;
|
|
|
|
KEYDB_SEARCH_DESC *desc = NULL;
|
|
|
|
Base64Context b64writer = NULL;
|
|
|
|
ksba_writer_t writer;
|
|
|
|
ksba_cert_t cert = NULL;
|
|
|
|
int rc=0;
|
|
|
|
const unsigned char *image;
|
|
|
|
size_t imagelen;
|
|
|
|
char *keygrip = NULL;
|
|
|
|
char *prompt;
|
|
|
|
char buffer[1024];
|
|
|
|
int nread;
|
|
|
|
FILE *datafp = NULL;
|
|
|
|
|
2004-03-02 09:02:47 +01:00
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
hd = keydb_new (0);
|
|
|
|
if (!hd)
|
|
|
|
{
|
|
|
|
log_error ("keydb_new failed\n");
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
desc = xtrycalloc (1, sizeof *desc);
|
|
|
|
if (!desc)
|
|
|
|
{
|
|
|
|
log_error ("allocating memory for export failed: %s\n",
|
2006-09-06 18:35:52 +02:00
|
|
|
gpg_strerror (out_of_core ()));
|
2004-02-19 17:26:32 +01:00
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
rc = keydb_classify_name (name, desc);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("key `%s' not found: %s\n",
|
|
|
|
name, gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
2008-05-20 10:00:40 +02:00
|
|
|
/* Lookup the certificate and make sure that it is unique. */
|
2004-02-19 17:26:32 +01:00
|
|
|
rc = keydb_search (hd, desc, 1);
|
|
|
|
if (!rc)
|
|
|
|
{
|
|
|
|
rc = keydb_get_cert (hd, &cert);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("keydb_get_cert failed: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
2007-08-23 19:41:22 +02:00
|
|
|
|
|
|
|
next_ambiguous:
|
2004-02-19 17:26:32 +01:00
|
|
|
rc = keydb_search (hd, desc, 1);
|
|
|
|
if (!rc)
|
2007-08-23 19:41:22 +02:00
|
|
|
{
|
|
|
|
ksba_cert_t cert2 = NULL;
|
|
|
|
|
|
|
|
if (!keydb_get_cert (hd, &cert2))
|
|
|
|
{
|
|
|
|
if (gpgsm_certs_identical_p (cert, cert2))
|
|
|
|
{
|
|
|
|
ksba_cert_release (cert2);
|
|
|
|
goto next_ambiguous;
|
|
|
|
}
|
|
|
|
ksba_cert_release (cert2);
|
|
|
|
}
|
|
|
|
rc = gpg_error (GPG_ERR_AMBIGUOUS_NAME);
|
|
|
|
}
|
2004-02-19 17:26:32 +01:00
|
|
|
else if (rc == -1 || gpg_err_code (rc) == GPG_ERR_EOF)
|
|
|
|
rc = 0;
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("key `%s' not found: %s\n",
|
|
|
|
name, gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keygrip = gpgsm_get_keygrip_hexstring (cert);
|
2004-04-26 15:29:09 +02:00
|
|
|
if (!keygrip || gpgsm_agent_havekey (ctrl, keygrip))
|
2004-02-19 17:26:32 +01:00
|
|
|
{
|
|
|
|
/* Note, that the !keygrip case indicates a bad certificate. */
|
|
|
|
rc = gpg_error (GPG_ERR_NO_SECKEY);
|
|
|
|
log_error ("can't export key `%s': %s\n", name, gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
image = ksba_cert_get_image (cert, &imagelen);
|
|
|
|
if (!image)
|
|
|
|
{
|
|
|
|
log_error ("ksba_cert_get_image failed\n");
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctrl->create_pem)
|
|
|
|
{
|
2007-03-19 16:44:59 +01:00
|
|
|
print_short_info (cert, fp, NULL);
|
2004-02-19 17:26:32 +01:00
|
|
|
putc ('\n', fp);
|
|
|
|
}
|
|
|
|
|
2007-05-29 22:11:17 +02:00
|
|
|
if (opt.p12_charset && ctrl->create_pem)
|
2007-03-20 11:00:55 +01:00
|
|
|
{
|
|
|
|
fprintf (fp, "The passphrase is %s encoded.\n\n",
|
|
|
|
opt.p12_charset);
|
|
|
|
}
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
ctrl->pem_name = "PKCS12";
|
2007-03-19 16:44:59 +01:00
|
|
|
rc = gpgsm_create_writer (&b64writer, ctrl, fp, NULL, &writer);
|
2004-02-19 17:26:32 +01:00
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("can't create writer: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
prompt = gpgsm_format_keydesc (cert);
|
2004-09-29 15:50:31 +02:00
|
|
|
rc = export_p12 (ctrl, image, imagelen, prompt, keygrip, &datafp);
|
2004-02-19 17:26:32 +01:00
|
|
|
xfree (prompt);
|
|
|
|
if (rc)
|
|
|
|
goto leave;
|
|
|
|
rewind (datafp);
|
|
|
|
while ( (nread = fread (buffer, 1, sizeof buffer, datafp)) > 0 )
|
|
|
|
if ((rc = ksba_writer_write (writer, buffer, nread)))
|
|
|
|
{
|
|
|
|
log_error ("write failed: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
if (ferror (datafp))
|
|
|
|
{
|
|
|
|
rc = gpg_error_from_errno (rc);
|
|
|
|
log_error ("error reading temporary file: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ctrl->create_pem)
|
|
|
|
{
|
|
|
|
/* We want one certificate per PEM block */
|
|
|
|
rc = gpgsm_finish_writer (b64writer);
|
|
|
|
if (rc)
|
|
|
|
{
|
|
|
|
log_error ("write failed: %s\n", gpg_strerror (rc));
|
|
|
|
goto leave;
|
|
|
|
}
|
|
|
|
gpgsm_destroy_writer (b64writer);
|
|
|
|
b64writer = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
ksba_cert_release (cert);
|
|
|
|
cert = NULL;
|
|
|
|
|
|
|
|
leave:
|
|
|
|
if (datafp)
|
|
|
|
fclose (datafp);
|
|
|
|
gpgsm_destroy_writer (b64writer);
|
|
|
|
ksba_cert_release (cert);
|
|
|
|
xfree (desc);
|
|
|
|
keydb_release (hd);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-03-19 16:44:59 +01:00
|
|
|
/* Call either es_putc or the plain putc. */
|
|
|
|
static void
|
|
|
|
do_putc (int value, FILE *fp, estream_t stream)
|
|
|
|
{
|
|
|
|
if (stream)
|
|
|
|
es_putc (value, stream);
|
|
|
|
else
|
|
|
|
putc (value, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Call either es_fputs or the plain fputs. */
|
2003-08-05 19:11:04 +02:00
|
|
|
static void
|
2007-03-19 16:44:59 +01:00
|
|
|
do_fputs (const char *string, FILE *fp, estream_t stream)
|
|
|
|
{
|
|
|
|
if (stream)
|
|
|
|
es_fputs (string, stream);
|
|
|
|
else
|
|
|
|
fputs (string, fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Print some info about the certifciate CERT to FP or STREAM */
|
|
|
|
static void
|
|
|
|
print_short_info (ksba_cert_t cert, FILE *fp, estream_t stream)
|
2003-08-05 19:11:04 +02:00
|
|
|
{
|
|
|
|
char *p;
|
2003-12-17 13:28:24 +01:00
|
|
|
ksba_sexp_t sexp;
|
2003-08-05 19:11:04 +02:00
|
|
|
int idx;
|
|
|
|
|
|
|
|
for (idx=0; (p = ksba_cert_get_issuer (cert, idx)); idx++)
|
|
|
|
{
|
2007-03-19 16:44:59 +01:00
|
|
|
do_fputs ((!idx
|
|
|
|
? "Issuer ...: "
|
|
|
|
: "\n aka ...: "), fp, stream);
|
|
|
|
if (stream)
|
|
|
|
gpgsm_es_print_name (stream, p);
|
|
|
|
else
|
|
|
|
gpgsm_print_name (fp, p);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (p);
|
|
|
|
}
|
2007-03-19 16:44:59 +01:00
|
|
|
do_putc ('\n', fp, stream);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
2007-03-19 16:44:59 +01:00
|
|
|
do_fputs ("Serial ...: ", fp, stream);
|
2003-08-05 19:11:04 +02:00
|
|
|
sexp = ksba_cert_get_serial (cert);
|
|
|
|
if (sexp)
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
const unsigned char *s = sexp;
|
|
|
|
|
|
|
|
if (*s == '(')
|
|
|
|
{
|
|
|
|
s++;
|
|
|
|
for (len=0; *s && *s != ':' && digitp (s); s++)
|
|
|
|
len = len*10 + atoi_1 (s);
|
|
|
|
if (*s == ':')
|
2007-03-19 16:44:59 +01:00
|
|
|
{
|
|
|
|
if (stream)
|
|
|
|
es_write_hexstring (stream, s+1, len, 0, NULL);
|
|
|
|
else
|
|
|
|
print_hexstring (fp, s+1, len, 0);
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
xfree (sexp);
|
|
|
|
}
|
2007-03-19 16:44:59 +01:00
|
|
|
do_putc ('\n', fp, stream);
|
2003-08-05 19:11:04 +02:00
|
|
|
|
|
|
|
for (idx=0; (p = ksba_cert_get_subject (cert, idx)); idx++)
|
|
|
|
{
|
2007-03-19 16:44:59 +01:00
|
|
|
do_fputs ((!idx
|
|
|
|
? "Subject ..: "
|
|
|
|
: "\n aka ..: "), fp, stream);
|
|
|
|
if (stream)
|
|
|
|
gpgsm_es_print_name (stream, p);
|
|
|
|
else
|
|
|
|
gpgsm_print_name (fp, p);
|
2003-08-05 19:11:04 +02:00
|
|
|
xfree (p);
|
|
|
|
}
|
2007-03-19 16:44:59 +01:00
|
|
|
do_putc ('\n', fp, stream);
|
2003-08-05 19:11:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-19 17:26:32 +01:00
|
|
|
static gpg_error_t
|
2008-10-23 11:41:41 +02:00
|
|
|
popen_protect_tool (ctrl_t ctrl, const char *pgmname,
|
2004-02-19 17:26:32 +01:00
|
|
|
FILE *infile, FILE *outfile, FILE **statusfile,
|
|
|
|
const char *prompt, const char *keygrip,
|
|
|
|
pid_t *pid)
|
|
|
|
{
|
2009-04-01 12:51:53 +02:00
|
|
|
const char *argv[22];
|
2004-12-06 19:28:56 +01:00
|
|
|
int i=0;
|
|
|
|
|
2008-10-23 11:41:41 +02:00
|
|
|
/* Make sure that the agent is running so that the protect tool is
|
|
|
|
able to ask for a passphrase. This has only an effect under W32
|
|
|
|
where the agent is started on demand; sending a NOP does not harm
|
2009-04-01 12:51:53 +02:00
|
|
|
on other platforms. This is not really necessary anymore because
|
|
|
|
the protect tool does this now by itself; it does not harm either.*/
|
2008-10-23 11:41:41 +02:00
|
|
|
gpgsm_agent_send_nop (ctrl);
|
|
|
|
|
2004-12-06 19:28:56 +01:00
|
|
|
argv[i++] = "--homedir";
|
|
|
|
argv[i++] = opt.homedir;
|
|
|
|
argv[i++] = "--p12-export";
|
2005-09-09 13:18:08 +02:00
|
|
|
argv[i++] = "--have-cert";
|
2004-12-06 19:28:56 +01:00
|
|
|
argv[i++] = "--prompt";
|
|
|
|
argv[i++] = prompt?prompt:"";
|
|
|
|
argv[i++] = "--enable-status-msg";
|
2007-03-20 11:00:55 +01:00
|
|
|
if (opt.p12_charset)
|
|
|
|
{
|
|
|
|
argv[i++] = "--p12-charset";
|
|
|
|
argv[i++] = opt.p12_charset;
|
|
|
|
}
|
2009-04-01 12:51:53 +02:00
|
|
|
if (opt.agent_program)
|
|
|
|
{
|
|
|
|
argv[i++] = "--agent-program";
|
|
|
|
argv[i++] = opt.agent_program;
|
|
|
|
}
|
2004-12-06 19:28:56 +01:00
|
|
|
argv[i++] = "--",
|
|
|
|
argv[i++] = keygrip,
|
|
|
|
argv[i] = NULL;
|
|
|
|
assert (i < sizeof argv);
|
|
|
|
|
|
|
|
return gnupg_spawn_process (pgmname, argv, infile, outfile,
|
2009-04-01 12:51:53 +02:00
|
|
|
setup_pinentry_env, (128|64),
|
2004-12-06 19:28:56 +01:00
|
|
|
statusfile, pid);
|
2004-02-19 17:26:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static gpg_error_t
|
2004-09-29 15:50:31 +02:00
|
|
|
export_p12 (ctrl_t ctrl, const unsigned char *certimg, size_t certimglen,
|
2004-02-19 17:26:32 +01:00
|
|
|
const char *prompt, const char *keygrip,
|
|
|
|
FILE **retfp)
|
|
|
|
{
|
|
|
|
const char *pgmname;
|
|
|
|
gpg_error_t err = 0, child_err = 0;
|
2004-12-06 19:28:56 +01:00
|
|
|
int c, cont_line;
|
2004-02-19 17:26:32 +01:00
|
|
|
unsigned int pos;
|
|
|
|
FILE *infp = NULL, *outfp = NULL, *fp = NULL;
|
|
|
|
char buffer[1024];
|
|
|
|
pid_t pid = -1;
|
2004-09-29 15:50:31 +02:00
|
|
|
int bad_pass = 0;
|
2004-02-19 17:26:32 +01:00
|
|
|
|
|
|
|
if (!opt.protect_tool_program || !*opt.protect_tool_program)
|
2007-06-14 19:05:07 +02:00
|
|
|
pgmname = gnupg_module_name (GNUPG_MODULE_NAME_PROTECT_TOOL);
|
2004-02-19 17:26:32 +01:00
|
|
|
else
|
|
|
|
pgmname = opt.protect_tool_program;
|
|
|
|
|
2007-08-22 12:55:07 +02:00
|
|
|
infp = gnupg_tmpfile ();
|
2004-02-19 17:26:32 +01:00
|
|
|
if (!infp)
|
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
err = gpg_error_from_syserror ();
|
2004-02-19 17:26:32 +01:00
|
|
|
log_error (_("error creating temporary file: %s\n"), strerror (errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (fwrite (certimg, certimglen, 1, infp) != 1)
|
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
err = gpg_error_from_syserror ();
|
2004-02-19 17:26:32 +01:00
|
|
|
log_error (_("error writing to temporary file: %s\n"),
|
|
|
|
strerror (errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2007-08-22 12:55:07 +02:00
|
|
|
outfp = gnupg_tmpfile ();
|
2004-02-19 17:26:32 +01:00
|
|
|
if (!outfp)
|
|
|
|
{
|
2006-09-14 18:50:33 +02:00
|
|
|
err = gpg_error_from_syserror ();
|
2004-02-19 17:26:32 +01:00
|
|
|
log_error (_("error creating temporary file: %s\n"), strerror (errno));
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2008-10-23 11:41:41 +02:00
|
|
|
err = popen_protect_tool (ctrl,
|
|
|
|
pgmname, infp, outfp, &fp, prompt, keygrip, &pid);
|
2004-02-19 17:26:32 +01:00
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
pid = -1;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
fclose (infp);
|
|
|
|
infp = NULL;
|
|
|
|
|
|
|
|
/* Read stderr of the protect tool. */
|
|
|
|
pos = 0;
|
|
|
|
cont_line = 0;
|
|
|
|
while ((c=getc (fp)) != EOF)
|
|
|
|
{
|
|
|
|
/* fixme: We could here grep for status information of the
|
|
|
|
protect tool to figure out better error codes for
|
|
|
|
CHILD_ERR. */
|
|
|
|
buffer[pos++] = c;
|
2004-06-06 15:00:59 +02:00
|
|
|
if (pos >= sizeof buffer - 5 || c == '\n')
|
2004-02-19 17:26:32 +01:00
|
|
|
{
|
|
|
|
buffer[pos - (c == '\n')] = 0;
|
|
|
|
if (cont_line)
|
|
|
|
log_printf ("%s", buffer);
|
|
|
|
else
|
2004-09-29 15:50:31 +02:00
|
|
|
{
|
|
|
|
if (!strncmp (buffer, "gpg-protect-tool: [PROTECT-TOOL:] ",34))
|
|
|
|
{
|
|
|
|
char *p, *pend;
|
|
|
|
|
|
|
|
p = buffer + 34;
|
|
|
|
pend = strchr (p, ' ');
|
|
|
|
if (pend)
|
|
|
|
*pend = 0;
|
|
|
|
if ( !strcmp (p, "bad-passphrase"))
|
|
|
|
bad_pass++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
log_info ("%s", buffer);
|
|
|
|
}
|
2004-02-19 17:26:32 +01:00
|
|
|
pos = 0;
|
|
|
|
cont_line = (c != '\n');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pos)
|
|
|
|
{
|
|
|
|
buffer[pos] = 0;
|
|
|
|
if (cont_line)
|
|
|
|
log_printf ("%s\n", buffer);
|
|
|
|
else
|
|
|
|
log_info ("%s\n", buffer);
|
|
|
|
}
|
|
|
|
else if (cont_line)
|
|
|
|
log_printf ("\n");
|
|
|
|
|
|
|
|
/* If we found no error in the output of the child, setup a suitable
|
|
|
|
error code, which will later be reset if the exit status of the
|
|
|
|
child is 0. */
|
|
|
|
if (!child_err)
|
|
|
|
child_err = gpg_error (GPG_ERR_DECRYPT_FAILED);
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
if (infp)
|
|
|
|
fclose (infp);
|
|
|
|
if (fp)
|
|
|
|
fclose (fp);
|
|
|
|
if (pid != -1)
|
|
|
|
{
|
2007-08-29 11:51:37 +02:00
|
|
|
if (!gnupg_wait_process (pgmname, pid, NULL))
|
2004-02-19 17:26:32 +01:00
|
|
|
child_err = 0;
|
|
|
|
}
|
|
|
|
if (!err)
|
|
|
|
err = child_err;
|
|
|
|
if (err)
|
|
|
|
{
|
|
|
|
if (outfp)
|
|
|
|
fclose (outfp);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*retfp = outfp;
|
2004-09-29 15:50:31 +02:00
|
|
|
if (bad_pass)
|
|
|
|
{
|
|
|
|
/* During export this is the passphrase used to unprotect the
|
|
|
|
key and not the pkcs#12 thing as in export. Therefore we can
|
|
|
|
issue the regular passphrase status. FIXME: replace the all
|
|
|
|
zero keyid by a regular one. */
|
|
|
|
gpgsm_status (ctrl, STATUS_BAD_PASSPHRASE, "0000000000000000");
|
|
|
|
}
|
2004-02-19 17:26:32 +01:00
|
|
|
return err;
|
|
|
|
}
|
2003-08-05 19:11:04 +02:00
|
|
|
|