2001-11-07 17:44:22 +00:00
|
|
|
|
/* import.c - Import certificates
|
2003-06-03 19:55:50 +00:00
|
|
|
|
* Copyright (C) 2001, 2003 Free Software Foundation, Inc.
|
2001-11-07 17:44:22 +00: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
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (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
|
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
#include <errno.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <time.h>
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
2001-11-10 18:08:10 +00:00
|
|
|
|
#include <gcrypt.h>
|
2001-11-07 17:44:22 +00:00
|
|
|
|
#include <ksba.h>
|
|
|
|
|
|
|
|
|
|
#include "gpgsm.h"
|
2001-11-13 12:50:14 +00:00
|
|
|
|
#include "keydb.h"
|
|
|
|
|
#include "i18n.h"
|
2001-11-07 17:44:22 +00:00
|
|
|
|
|
2002-08-20 13:09:53 +00:00
|
|
|
|
struct stats_s {
|
|
|
|
|
unsigned long count;
|
|
|
|
|
unsigned long imported;
|
|
|
|
|
unsigned long unchanged;
|
2002-08-21 15:41:21 +00:00
|
|
|
|
unsigned long not_imported;
|
2002-08-20 13:09:53 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
2001-11-07 17:44:22 +00:00
|
|
|
|
|
2002-07-02 10:40:12 +00:00
|
|
|
|
static void
|
|
|
|
|
print_imported_status (CTRL ctrl, KsbaCert cert)
|
|
|
|
|
{
|
|
|
|
|
char *fpr;
|
|
|
|
|
|
|
|
|
|
fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
|
2002-07-03 16:03:22 +00:00
|
|
|
|
gpgsm_status2 (ctrl, STATUS_IMPORTED, fpr, "[X.509]", NULL);
|
2002-07-02 10:40:12 +00:00
|
|
|
|
xfree (fpr);
|
|
|
|
|
}
|
|
|
|
|
|
2002-08-20 13:09:53 +00:00
|
|
|
|
|
2002-08-21 15:41:21 +00:00
|
|
|
|
/* Print an IMPORT_PROBLEM status. REASON is one of:
|
|
|
|
|
0 := "No specific reason given".
|
|
|
|
|
1 := "Invalid Certificate".
|
|
|
|
|
2 := "Issuer Certificate missing".
|
|
|
|
|
3 := "Certificate Chain too long".
|
|
|
|
|
4 := "Error storing certificate".
|
|
|
|
|
*/
|
|
|
|
|
static void
|
|
|
|
|
print_import_problem (CTRL ctrl, KsbaCert cert, int reason)
|
|
|
|
|
{
|
|
|
|
|
char *fpr = NULL;
|
|
|
|
|
char buf[25];
|
|
|
|
|
int i;
|
|
|
|
|
|
|
|
|
|
sprintf (buf, "%d", reason);
|
|
|
|
|
if (cert)
|
|
|
|
|
{
|
|
|
|
|
fpr = gpgsm_get_fingerprint_hexstring (cert, GCRY_MD_SHA1);
|
|
|
|
|
/* detetect an error (all high) value */
|
|
|
|
|
for (i=0; fpr[i] == 'F'; i++)
|
|
|
|
|
;
|
|
|
|
|
if (!fpr[i])
|
|
|
|
|
{
|
|
|
|
|
xfree (fpr);
|
|
|
|
|
fpr = NULL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
gpgsm_status2 (ctrl, STATUS_IMPORT_PROBLEM, buf, fpr, NULL);
|
|
|
|
|
xfree (fpr);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2002-08-20 13:09:53 +00:00
|
|
|
|
void
|
|
|
|
|
print_imported_summary (CTRL ctrl, struct stats_s *stats)
|
|
|
|
|
{
|
2002-08-21 15:41:21 +00:00
|
|
|
|
char buf[14*25];
|
2002-08-20 13:09:53 +00:00
|
|
|
|
|
|
|
|
|
if (!opt.quiet)
|
|
|
|
|
{
|
|
|
|
|
log_info (_("total number processed: %lu\n"), stats->count);
|
|
|
|
|
if (stats->imported)
|
|
|
|
|
{
|
|
|
|
|
log_info (_(" imported: %lu"), stats->imported );
|
|
|
|
|
log_printf ("\n");
|
|
|
|
|
}
|
|
|
|
|
if (stats->unchanged)
|
|
|
|
|
log_info (_(" unchanged: %lu\n"), stats->unchanged);
|
2002-08-21 15:41:21 +00:00
|
|
|
|
if (stats->not_imported)
|
|
|
|
|
log_info (_(" not imported: %lu\n"), stats->not_imported);
|
2002-08-20 13:09:53 +00:00
|
|
|
|
}
|
|
|
|
|
|
2002-08-21 15:41:21 +00:00
|
|
|
|
sprintf (buf, "%lu 0 %lu 0 %lu 0 0 0 0 0 0 0 0 %lu",
|
2002-08-20 13:09:53 +00:00
|
|
|
|
stats->count,
|
|
|
|
|
stats->imported,
|
|
|
|
|
stats->unchanged,
|
2002-08-21 15:41:21 +00:00
|
|
|
|
stats->not_imported
|
2002-08-20 13:09:53 +00:00
|
|
|
|
);
|
|
|
|
|
gpgsm_status (ctrl, STATUS_IMPORT_RES, buf);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-07-02 10:40:12 +00:00
|
|
|
|
static void
|
2002-08-20 13:09:53 +00:00
|
|
|
|
check_and_store (CTRL ctrl, struct stats_s *stats, KsbaCert cert, int depth)
|
2002-07-02 10:40:12 +00:00
|
|
|
|
{
|
2002-08-21 15:41:21 +00:00
|
|
|
|
int rc;
|
|
|
|
|
|
2002-08-20 13:09:53 +00:00
|
|
|
|
stats->count++;
|
2002-08-21 15:41:21 +00:00
|
|
|
|
if ( depth >= 50 )
|
|
|
|
|
{
|
|
|
|
|
log_error (_("certificate chain too long\n"));
|
|
|
|
|
stats->not_imported++;
|
|
|
|
|
print_import_problem (ctrl, cert, 3);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = gpgsm_basic_cert_check (cert);
|
|
|
|
|
if (!rc)
|
2002-07-02 10:40:12 +00:00
|
|
|
|
{
|
|
|
|
|
int existed;
|
|
|
|
|
|
|
|
|
|
if (!keydb_store_cert (cert, 0, &existed))
|
|
|
|
|
{
|
|
|
|
|
KsbaCert next = NULL;
|
|
|
|
|
|
|
|
|
|
if (!existed)
|
2002-08-20 13:09:53 +00:00
|
|
|
|
{
|
|
|
|
|
print_imported_status (ctrl, cert);
|
|
|
|
|
stats->imported++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
stats->unchanged++;
|
|
|
|
|
|
2002-07-02 10:40:12 +00:00
|
|
|
|
if (opt.verbose > 1 && existed)
|
|
|
|
|
{
|
|
|
|
|
if (depth)
|
|
|
|
|
log_info ("issuer certificate already in DB\n");
|
|
|
|
|
else
|
|
|
|
|
log_info ("certificate already in DB\n");
|
|
|
|
|
}
|
|
|
|
|
else if (opt.verbose && !existed)
|
|
|
|
|
{
|
|
|
|
|
if (depth)
|
|
|
|
|
log_info ("issuer certificate imported\n");
|
|
|
|
|
else
|
|
|
|
|
log_info ("certificate imported\n");
|
|
|
|
|
}
|
|
|
|
|
/* Now lets walk up the chain and import all certificates up
|
|
|
|
|
the chain.*/
|
|
|
|
|
else if (!gpgsm_walk_cert_chain (cert, &next))
|
|
|
|
|
{
|
2002-08-20 13:09:53 +00:00
|
|
|
|
check_and_store (ctrl, stats, next, depth+1);
|
2002-07-02 10:40:12 +00:00
|
|
|
|
ksba_cert_release (next);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
2002-08-21 15:41:21 +00:00
|
|
|
|
{
|
|
|
|
|
log_error (_("error storing certificate\n"));
|
|
|
|
|
stats->not_imported++;
|
|
|
|
|
print_import_problem (ctrl, cert, 4);
|
|
|
|
|
}
|
2002-07-02 10:40:12 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
2002-08-21 15:41:21 +00:00
|
|
|
|
{
|
|
|
|
|
log_error (_("basic certificate checks failed - not imported\n"));
|
|
|
|
|
stats->not_imported++;
|
|
|
|
|
print_import_problem (ctrl, cert,
|
2003-06-03 19:55:50 +00:00
|
|
|
|
gpg_err_code (rc) == GPG_ERR_MISSING_CERT? 2 :
|
|
|
|
|
gpg_err_code (rc) == GPG_ERR_BAD_CERT? 1 : 0);
|
2002-08-21 15:41:21 +00:00
|
|
|
|
}
|
2002-07-02 10:40:12 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2001-11-07 17:44:22 +00:00
|
|
|
|
|
2002-08-20 13:09:53 +00:00
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
|
import_one (CTRL ctrl, struct stats_s *stats, int in_fd)
|
2001-11-07 17:44:22 +00:00
|
|
|
|
{
|
|
|
|
|
int rc;
|
2001-11-27 17:40:09 +00:00
|
|
|
|
Base64Context b64reader = NULL;
|
|
|
|
|
KsbaReader reader;
|
2001-11-07 17:44:22 +00:00
|
|
|
|
KsbaCert cert = NULL;
|
2002-05-21 08:29:17 +00:00
|
|
|
|
KsbaCMS cms = NULL;
|
2001-11-27 17:40:09 +00:00
|
|
|
|
FILE *fp = NULL;
|
2002-05-21 08:29:17 +00:00
|
|
|
|
KsbaContentType ct;
|
2001-11-07 17:44:22 +00:00
|
|
|
|
|
2001-11-27 17:40:09 +00:00
|
|
|
|
fp = fdopen ( dup (in_fd), "rb");
|
|
|
|
|
if (!fp)
|
2001-11-07 17:44:22 +00:00
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = gpg_error (gpg_err_code_from_errno (errno));
|
2001-11-07 17:44:22 +00:00
|
|
|
|
log_error ("fdopen() failed: %s\n", strerror (errno));
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-27 17:40:09 +00:00
|
|
|
|
rc = gpgsm_create_reader (&b64reader, ctrl, fp, &reader);
|
2001-11-07 17:44:22 +00:00
|
|
|
|
if (rc)
|
|
|
|
|
{
|
2003-06-05 07:14:21 +00:00
|
|
|
|
log_error ("can't create reader: %s\n", gpg_strerror (rc));
|
2001-11-07 17:44:22 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
2002-05-21 08:29:17 +00:00
|
|
|
|
ct = ksba_cms_identify (reader);
|
|
|
|
|
if (ct == KSBA_CT_SIGNED_DATA)
|
|
|
|
|
{ /* This is probably a signed-only message - import the certs */
|
|
|
|
|
KsbaStopReason stopreason;
|
|
|
|
|
int i;
|
2001-11-07 17:44:22 +00:00
|
|
|
|
|
2002-05-21 08:29:17 +00:00
|
|
|
|
cms = ksba_cms_new ();
|
|
|
|
|
if (!cms)
|
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = gpg_error (GPG_ERR_ENOMEM);
|
2002-05-21 08:29:17 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2001-11-07 17:44:22 +00:00
|
|
|
|
|
2002-05-21 08:29:17 +00:00
|
|
|
|
rc = ksba_cms_set_reader_writer (cms, reader, NULL);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("ksba_cms_set_reader_writer failed: %s\n",
|
|
|
|
|
ksba_strerror (rc));
|
|
|
|
|
rc = map_ksba_err (rc);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do
|
2002-03-12 13:36:29 +00:00
|
|
|
|
{
|
2002-05-21 08:29:17 +00:00
|
|
|
|
rc = ksba_cms_parse (cms, &stopreason);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
log_error ("ksba_cms_parse failed: %s\n", ksba_strerror (rc));
|
|
|
|
|
rc = map_ksba_err (rc);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (stopreason == KSBA_SR_BEGIN_DATA)
|
|
|
|
|
log_info ("not a certs-only message\n");
|
2002-03-12 13:36:29 +00:00
|
|
|
|
}
|
2002-05-21 08:29:17 +00:00
|
|
|
|
while (stopreason != KSBA_SR_READY);
|
|
|
|
|
|
|
|
|
|
for (i=0; (cert=ksba_cms_get_cert (cms, i)); i++)
|
|
|
|
|
{
|
2002-08-20 13:09:53 +00:00
|
|
|
|
check_and_store (ctrl, stats, cert, 0);
|
2002-05-21 08:29:17 +00:00
|
|
|
|
ksba_cert_release (cert);
|
|
|
|
|
cert = NULL;
|
|
|
|
|
}
|
2002-05-23 13:57:19 +00:00
|
|
|
|
if (!i)
|
|
|
|
|
log_error ("no certificate found\n");
|
2002-03-12 13:36:29 +00:00
|
|
|
|
}
|
2002-05-21 08:29:17 +00:00
|
|
|
|
else if (ct == KSBA_CT_NONE)
|
|
|
|
|
{ /* Failed to identify this message - assume a certificate */
|
|
|
|
|
|
|
|
|
|
cert = ksba_cert_new ();
|
|
|
|
|
if (!cert)
|
|
|
|
|
{
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = gpg_error (GPG_ERR_ENOMEM);
|
2002-05-21 08:29:17 +00:00
|
|
|
|
goto leave;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rc = ksba_cert_read_der (cert, reader);
|
|
|
|
|
if (rc)
|
|
|
|
|
{
|
|
|
|
|
rc = map_ksba_err (rc);
|
|
|
|
|
goto leave;
|
|
|
|
|
}
|
2002-07-02 10:40:12 +00:00
|
|
|
|
|
2002-08-20 13:09:53 +00:00
|
|
|
|
check_and_store (ctrl, stats, cert, 0);
|
2002-05-21 08:29:17 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
log_error ("can't extract certificates from input\n");
|
2003-06-03 19:55:50 +00:00
|
|
|
|
rc = gpg_error (GPG_ERR_NO_DATA);
|
2002-05-21 08:29:17 +00:00
|
|
|
|
}
|
|
|
|
|
|
2001-11-07 17:44:22 +00:00
|
|
|
|
leave:
|
2002-05-21 08:29:17 +00:00
|
|
|
|
ksba_cms_release (cms);
|
2001-11-07 17:44:22 +00:00
|
|
|
|
ksba_cert_release (cert);
|
2001-11-27 17:40:09 +00:00
|
|
|
|
gpgsm_destroy_reader (b64reader);
|
|
|
|
|
if (fp)
|
|
|
|
|
fclose (fp);
|
2002-08-20 13:09:53 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
gpgsm_import (CTRL ctrl, int in_fd)
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
struct stats_s stats;
|
|
|
|
|
|
|
|
|
|
memset (&stats, 0, sizeof stats);
|
|
|
|
|
rc = import_one (ctrl, &stats, in_fd);
|
|
|
|
|
print_imported_summary (ctrl, &stats);
|
2002-05-23 13:57:19 +00:00
|
|
|
|
/* If we never printed an error message do it now so that a command
|
|
|
|
|
line invocation will return with an error (log_error keeps a
|
|
|
|
|
global errorcount) */
|
|
|
|
|
if (rc && !log_get_errorcount (0))
|
2003-06-05 07:14:21 +00:00
|
|
|
|
log_error (_("error importing certificate: %s\n"), gpg_strerror (rc));
|
2001-11-07 17:44:22 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
|
|
|
|
|
2001-11-13 12:50:14 +00:00
|
|
|
|
|
2002-08-20 13:09:53 +00:00
|
|
|
|
int
|
|
|
|
|
gpgsm_import_files (CTRL ctrl, int nfiles, char **files,
|
|
|
|
|
int (*of)(const char *fname))
|
|
|
|
|
{
|
|
|
|
|
int rc = 0;
|
|
|
|
|
struct stats_s stats;
|
|
|
|
|
|
|
|
|
|
memset (&stats, 0, sizeof stats);
|
|
|
|
|
|
|
|
|
|
if (!nfiles)
|
|
|
|
|
rc = import_one (ctrl, &stats, 0);
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
for (; nfiles && !rc ; nfiles--, files++)
|
|
|
|
|
{
|
|
|
|
|
int fd = of (*files);
|
|
|
|
|
rc = import_one (ctrl, &stats, fd);
|
|
|
|
|
close (fd);
|
|
|
|
|
if (rc == -1)
|
|
|
|
|
rc = 0;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
print_imported_summary (ctrl, &stats);
|
|
|
|
|
/* If we never printed an error message do it now so that a command
|
|
|
|
|
line invocation will return with an error (log_error keeps a
|
|
|
|
|
global errorcount) */
|
|
|
|
|
if (rc && !log_get_errorcount (0))
|
2003-06-05 07:14:21 +00:00
|
|
|
|
log_error (_("error importing certificate: %s\n"), gpg_strerror (rc));
|
2002-08-20 13:09:53 +00:00
|
|
|
|
return rc;
|
|
|
|
|
}
|
2002-03-06 14:16:37 +00:00
|
|
|
|
|
|
|
|
|
|