Finished the bulk of changes for gnupg 1.9. This included switching

to libgcrypt functions, using shared error codes from libgpg-error,
replacing the old functions we used to have in ../util by those in
../jnlib and ../common, renaming the malloc functions and a couple of
types.  Note, that not all changes are listed below becuause they are
too similar and done at far too many places.  As of today the code
builds using the current libgcrypt from CVS but it is very unlikely
that it actually works.
This commit is contained in:
Werner Koch 2003-06-18 19:56:13 +00:00
parent b7b07d36e8
commit c0c2c58054
90 changed files with 5078 additions and 2925 deletions

View File

@ -1,3 +1,13 @@
2003-06-18 Werner Koch <wk@gnupg.org>
* configure.ac (GNUPG_DEFAULT_HOMEDIR): Changed temporary to
.gnupg2 to avoid accidential use with production keys.
2003-06-11 Werner Koch <wk@gnupg.org>
* configure.ac: Merged all stuff from current 1.3 version in.
* acinclude.m4: Merged required macros from current 1.2 version in.
2003-06-04 Werner Koch <wk@gnupg.org> 2003-06-04 Werner Koch <wk@gnupg.org>
* configure.ac, Makefile.am: Enable building of gpg. * configure.ac, Makefile.am: Enable building of gpg.

14
TODO
View File

@ -57,17 +57,3 @@ might want to have an agent context for each service request
* ALL * ALL
** Return IMPORT_OK status. ** Return IMPORT_OK status.

View File

@ -1,5 +1,5 @@
dnl macros to configure gnupg dnl macros to configure gnupg
dnl Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. dnl Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
dnl dnl
dnl This file is part of GnuPG. dnl This file is part of GnuPG.
dnl dnl
@ -36,6 +36,127 @@ AC_DEFUN(GNUPG_CHECK_TYPEDEF,
]) ])
dnl GNUPG_CHECK_GNUMAKE
dnl
AC_DEFUN(GNUPG_CHECK_GNUMAKE,
[
if ${MAKE-make} --version 2>/dev/null | grep '^GNU ' >/dev/null 2>&1; then
:
else
AC_MSG_WARN([[
***
*** It seems that you are not using GNU make. Some make tools have serious
*** flaws and you may not be able to build this software at all. Before you
*** complain, please try GNU make: GNU make is easy to build and available
*** at all GNU archives. It is always available from ftp.gnu.org:/gnu/make.
***]])
fi
])
dnl GNUPG_CHECK_FAQPROG
dnl
AC_DEFUN(GNUPG_CHECK_FAQPROG,
[ AC_MSG_CHECKING(for faqprog.pl)
if faqprog.pl -V 2>/dev/null | grep '^faqprog.pl ' >/dev/null 2>&1; then
working_faqprog=yes
FAQPROG="faqprog.pl"
else
working_faqprog=no
FAQPROG=": "
fi
AC_MSG_RESULT($working_faqprog)
AC_SUBST(FAQPROG)
AM_CONDITIONAL(WORKING_FAQPROG, test "$working_faqprog" = "yes" )
dnl if test $working_faqprog = no; then
dnl AC_MSG_WARN([[
dnl ***
dnl *** It seems that the faqprog.pl program is not installed;
dnl *** however it is only needed if you want to change the FAQ.
dnl *** (faqprog.pl should be available at:
dnl *** ftp://ftp.gnupg.org/gcrypt/contrib/faqprog.pl )
dnl *** No need to worry about this warning.
dnl ***]])
dnl fi
])
dnl GNUPG_CHECK_DOCBOOK_TO_TEXI
dnl
AC_DEFUN(GNUPG_CHECK_DOCBOOK_TO_TEXI,
[
AC_CHECK_PROG(DOCBOOK_TO_TEXI, docbook2texi, yes, no)
AC_MSG_CHECKING(for sgml to texi tools)
working_sgmltotexi=no
if test "$ac_cv_prog_DOCBOOK_TO_TEXI" = yes; then
if sgml2xml -v /dev/null 2>&1 | grep 'SP version' >/dev/null 2>&1 ; then
working_sgmltotexi=yes
fi
fi
AC_MSG_RESULT($working_sgmltotexi)
AM_CONDITIONAL(HAVE_DOCBOOK_TO_TEXI, test "$working_sgmltotexi" = "yes" )
])
dnl GNUPG_CHECK_ENDIAN
dnl define either LITTLE_ENDIAN_HOST or BIG_ENDIAN_HOST
dnl
define(GNUPG_CHECK_ENDIAN,
[
tmp_assumed_endian=big
if test "$cross_compiling" = yes; then
case "$host_cpu" in
i@<:@345678@:>@* )
tmp_assumed_endian=little
;;
*)
;;
esac
AC_MSG_WARN(cross compiling; assuming $tmp_assumed_endian endianess)
fi
AC_MSG_CHECKING(endianess)
AC_CACHE_VAL(gnupg_cv_c_endian,
[ gnupg_cv_c_endian=unknown
# See if sys/param.h defines the BYTE_ORDER macro.
AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/param.h>], [
#if !BYTE_ORDER || !BIG_ENDIAN || !LITTLE_ENDIAN
bogus endian macros
#endif], [# It does; now see whether it defined to BIG_ENDIAN or not.
AC_TRY_COMPILE([#include <sys/types.h>
#include <sys/param.h>], [
#if BYTE_ORDER != BIG_ENDIAN
not big endian
#endif], gnupg_cv_c_endian=big, gnupg_cv_c_endian=little)])
if test "$gnupg_cv_c_endian" = unknown; then
AC_TRY_RUN([main () {
/* Are we little or big endian? From Harbison&Steele. */
union
{
long l;
char c[sizeof (long)];
} u;
u.l = 1;
exit (u.c[sizeof (long) - 1] == 1);
}],
gnupg_cv_c_endian=little,
gnupg_cv_c_endian=big,
gnupg_cv_c_endian=$tmp_assumed_endian
)
fi
])
AC_MSG_RESULT([$gnupg_cv_c_endian])
if test "$gnupg_cv_c_endian" = little; then
AC_DEFINE(LITTLE_ENDIAN_HOST,1,
[Defined if the host has little endian byte ordering])
else
AC_DEFINE(BIG_ENDIAN_HOST,1,
[Defined if the host has big endian byte ordering])
fi
])
# Check for the getsockopt SO_PEERCRED # Check for the getsockopt SO_PEERCRED
AC_DEFUN(GNUPG_SYS_SO_PEERCRED, AC_DEFUN(GNUPG_SYS_SO_PEERCRED,
[ AC_MSG_CHECKING(for SO_PEERCRED) [ AC_MSG_CHECKING(for SO_PEERCRED)
@ -125,12 +246,11 @@ AC_DEFUN(GNUPG_PTH_VERSION_CHECK,
fi fi
]) ])
######################################################################
# Check whether mlock is broken (hpux 10.20 raises a SIGBUS if mlock # Check whether mlock is broken (hpux 10.20 raises a SIGBUS if mlock
# is not called from uid 0 (not tested whether uid 0 works) # is not called from uid 0 (not tested whether uid 0 works)
# For DECs Tru64 we have also to check whether mlock is in librt # For DECs Tru64 we have also to check whether mlock is in librt
# mlock is there a macro using memlk() # mlock is there a macro using memlk()
######################################################################
dnl GNUPG_CHECK_MLOCK dnl GNUPG_CHECK_MLOCK
dnl dnl
define(GNUPG_CHECK_MLOCK, define(GNUPG_CHECK_MLOCK,
@ -220,6 +340,32 @@ define(GNUPG_CHECK_MLOCK,
]) ])
dnl Stolen from gcc
dnl Define MKDIR_TAKES_ONE_ARG if mkdir accepts only one argument instead
dnl of the usual 2.
AC_DEFUN(GNUPG_FUNC_MKDIR_TAKES_ONE_ARG,
[AC_CHECK_HEADERS(sys/stat.h unistd.h direct.h)
AC_CACHE_CHECK([if mkdir takes one argument], gnupg_cv_mkdir_takes_one_arg,
[AC_TRY_COMPILE([
#include <sys/types.h>
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_DIRECT_H
# include <direct.h>
#endif], [mkdir ("foo", 0);],
gnupg_cv_mkdir_takes_one_arg=no, gnupg_cv_mkdir_takes_one_arg=yes)])
if test $gnupg_cv_mkdir_takes_one_arg = yes ; then
AC_DEFINE(MKDIR_TAKES_ONE_ARG,1,
[Defined if mkdir() does not take permission flags])
fi
])
dnl [copied from libgcrypt] dnl [copied from libgcrypt]
dnl AM_PATH_LIBGCRYPT([MINIMUM-VERSION, dnl AM_PATH_LIBGCRYPT([MINIMUM-VERSION,

View File

@ -1,5 +1,6 @@
# configure.ac - for GnuPG 1.9 # configure.ac - for GnuPG 1.9
# Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc, # Copyright (C) 1998, 1999, 2000, 2001, 2002,
# 2003 Free Software Foundation, Inc.
# #
# This file is part of GnuPG. # This file is part of GnuPG.
# #
@ -22,11 +23,16 @@ AC_PREREQ(2.52)
# Version number: Remember to change it immediately *after* a release. # Version number: Remember to change it immediately *after* a release.
# Add a "-cvs" prefix for non-released code. # Add a "-cvs" prefix for non-released code.
AC_INIT(gnupg, 1.9.0-cvs, gnupg-devel@gnupg.org) AC_INIT(gnupg, 1.9.0-cvs, gnupg-devel@gnupg.org)
# Set development_version to yes if the minor number is odd or you
# feel that the default check for a development version is not
# sufficient.
development_version=yes
NEED_LIBGCRYPT_VERSION=1.1.8 NEED_LIBGCRYPT_VERSION=1.1.8
NEED_LIBASSUAN_VERSION=0.0.1 NEED_LIBASSUAN_VERSION=0.0.1
NEED_KSBA_VERSION=0.4.6 NEED_KSBA_VERSION=0.4.6
NEED_OPENSC_VERSION=0.7.0 NEED_OPENSC_VERSION=0.7.0
PACKAGE=$PACKAGE_NAME PACKAGE=$PACKAGE_NAME
VERSION=$PACKAGE_VERSION VERSION=$PACKAGE_VERSION
@ -35,8 +41,10 @@ AM_GNU_GETTEXT_VERSION(0.11.5)
AC_CONFIG_AUX_DIR(scripts) AC_CONFIG_AUX_DIR(scripts)
AC_CONFIG_SRCDIR(sm/gpgsm.c) AC_CONFIG_SRCDIR(sm/gpgsm.c)
AM_CONFIG_HEADER(config.h) AM_CONFIG_HEADER(config.h)
AC_CANONICAL_TARGET()
AM_INIT_AUTOMAKE($PACKAGE, $VERSION) AM_INIT_AUTOMAKE($PACKAGE, $VERSION)
AM_MAINTAINER_MODE
AC_GNU_SOURCE
# Some status variables to give feedback at the end of a configure run # Some status variables to give feedback at the end of a configure run
habe_libassuan=no habe_libassuan=no
@ -50,24 +58,6 @@ GNUPG_BUILD_PROGRAM(agent, yes)
GNUPG_BUILD_PROGRAM(scdaemon, yes) GNUPG_BUILD_PROGRAM(scdaemon, yes)
AH_TOP([
/* We need this, because some autoconf tests rely on this (e.g. stpcpy)
and it should be used for new programs anyway. */
#define _GNU_SOURCE 1
])
AH_BOTTOM([
/* Some global constants. */
#ifdef HAVE_DRIVE_LETTERS
#define GNUPG_DEFAULT_HOMEDIR "c:/gnupg"
#else
#define GNUPG_DEFAULT_HOMEDIR "~/.gnupg"
#endif
#define GNUPG_PRIVATE_KEYS_DIR "private-keys-v1.d"
])
AC_SUBST(PACKAGE) AC_SUBST(PACKAGE)
AC_SUBST(VERSION) AC_SUBST(VERSION)
AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE", [Name of this package]) AC_DEFINE_UNQUOTED(PACKAGE, "$PACKAGE", [Name of this package])
@ -82,56 +72,6 @@ AC_DEFINE_UNQUOTED(NEED_KSBA_VERSION, "$NEED_KSBA_VERSION",
# Checks for programs.
missing_dir=`cd $ac_aux_dir && pwd`
AM_MISSING_PROG(ACLOCAL, aclocal, $missing_dir)
AM_MISSING_PROG(AUTOCONF, autoconf, $missing_dir)
AM_MISSING_PROG(AUTOMAKE, automake, $missing_dir)
AM_MISSING_PROG(AUTOHEADER, autoheader, $missing_dir)
AM_MISSING_PROG(MAKEINFO, makeinfo, $missing_dir)
AC_PROG_AWK
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB
#AC_ARG_PROGRAM
if test "$GCC" = yes; then
CFLAGS="$CFLAGS -Wall -Wcast-align -Wshadow -Wstrict-prototypes"
fi
case "${target}" in
*-*-mingw32*)
PRINTABLE_OS_NAME="MingW32"
AC_DEFINE(HAVE_DOSISH_SYSTEM,1,
[defined if we run on some of the PCDOS like systems
(DOS, Windoze. OS/2) with special properties like
no file modes])
;;
i?86-emx-os2 | i?86-*-os2*emx )
PRINTABLE_OS_NAME="OS/2"
;;
i?86-*-msdosdjgpp*)
PRINTABLE_OS_NAME="MSDOS/DJGPP"
try_dynload=no
;;
*-linux*)
PRINTABLE_OS_NAME="GNU/Linux"
;;
dnl let that after linux to avoid gnu-linux problems
*-gnu*)
PRINTABLE_OS_NAME="GNU/Hurd"
;;
*)
PRINTABLE_OS_NAME=`uname -s || echo "Unknown"`
;;
esac
AC_DEFINE_UNQUOTED(PRINTABLE_OS_NAME, "$PRINTABLE_OS_NAME",
[A human readable text with the name of the OS])
# I know that it is in general not a good idea to evaluate bindir in # I know that it is in general not a good idea to evaluate bindir in
# the configuration but we want to hard code the defaults into some of # the configuration but we want to hard code the defaults into some of
# the programs and doing this during a make install is not a good # the programs and doing this during a make install is not a good
@ -187,6 +127,221 @@ AC_DEFINE_UNQUOTED(GNUPG_PROTECT_TOOL, "$gnupg_protect_tool",
[Name of the protect tool program]) [Name of the protect tool program])
AC_MSG_CHECKING([whether to enable external program execution])
AC_ARG_ENABLE(exec,
AC_HELP_STRING([--disable-exec],[disable all external program execution]),
use_exec=$enableval)
AC_MSG_RESULT($use_exec)
if test "$use_exec" = no ; then
AC_DEFINE(NO_EXEC,1,[Define to disable all external program execution])
fi
if test "$use_exec" = yes ; then
AC_MSG_CHECKING([whether to enable photo ID viewing])
AC_ARG_ENABLE(photo-viewers,
[ --disable-photo-viewers disable photo ID viewers],
[if test "$enableval" = no ; then
AC_DEFINE(DISABLE_PHOTO_VIEWER,1,[define to disable photo viewing])
fi],enableval=yes)
gnupg_cv_enable_photo_viewers=$enableval
AC_MSG_RESULT($enableval)
if test "$gnupg_cv_enable_photo_viewers" = yes ; then
AC_MSG_CHECKING([whether to use a fixed photo ID viewer])
AC_ARG_WITH(photo-viewer,
[ --with-photo-viewer=FIXED_VIEWER set a fixed photo ID viewer],
[if test "$withval" = yes ; then
withval=no
elif test "$withval" != no ; then
AC_DEFINE_UNQUOTED(FIXED_PHOTO_VIEWER,"$withval",
[if set, restrict photo-viewer to this])
fi],withval=no)
AC_MSG_RESULT($withval)
fi
AC_MSG_CHECKING([whether to enable external keyserver helpers])
AC_ARG_ENABLE(keyserver-helpers,
[ --disable-keyserver-helpers disable all external keyserver support],
[if test "$enableval" = no ; then
AC_DEFINE(DISABLE_KEYSERVER_HELPERS,1,
[define to disable keyserver helpers])
fi],enableval=yes)
gnupg_cv_enable_keyserver_helpers=$enableval
AC_MSG_RESULT($enableval)
if test "$gnupg_cv_enable_keyserver_helpers" = yes ; then
AC_MSG_CHECKING([whether LDAP keyserver support is requested])
AC_ARG_ENABLE(ldap,
[ --disable-ldap disable LDAP keyserver interface],
try_ldap=$enableval, try_ldap=yes)
AC_MSG_RESULT($try_ldap)
AC_MSG_CHECKING([whether HKP keyserver support is requested])
AC_ARG_ENABLE(hkp,
[ --disable-hkp disable HKP keyserver interface],
try_hkp=$enableval, try_hkp=yes)
AC_MSG_RESULT($try_hkp)
if test "$try_hkp" = yes ; then
AC_SUBST(GPGKEYS_HKP,"gpgkeys_hkp$EXEEXT")
fi
AC_MSG_CHECKING([whether email keyserver support is requested])
AC_ARG_ENABLE(mailto,
[ --disable-mailto disable email keyserver interface],
try_mailto=$enableval, try_mailto=yes)
AC_MSG_RESULT($try_mailto)
fi
AC_MSG_CHECKING([whether keyserver exec-path is enabled])
AC_ARG_ENABLE(keyserver-path,
[ --disable-keyserver-path disable the exec-path option for keyserver helpers],
[if test "$enableval" = no ; then
AC_DEFINE(DISABLE_KEYSERVER_PATH,1,[define to disable exec-path for keyserver helpers])
fi],enableval=yes)
AC_MSG_RESULT($enableval)
fi
AC_MSG_CHECKING([whether the included zlib is requested])
AC_ARG_WITH(included-zlib,
[ --with-included-zlib use the zlib code included here],
[g10_force_zlib=yes], [g10_force_zlib=no] )
AC_MSG_RESULT($g10_force_zlib)
dnl
dnl Check whether we want to use Linux capabilities
dnl
AC_MSG_CHECKING([whether use of capabilities is requested])
AC_ARG_WITH(capabilities,
[ --with-capabilities use linux capabilities [default=no]],
[use_capabilities="$withval"],[use_capabilities=no])
AC_MSG_RESULT($use_capabilities)
AH_BOTTOM([
/* Some global constants. */
#ifdef HAVE_DRIVE_LETTERS
#define GNUPG_DEFAULT_HOMEDIR "c:/gnupg"
#else
#define GNUPG_DEFAULT_HOMEDIR "~/.gnupg2"
#endif
#define GNUPG_PRIVATE_KEYS_DIR "private-keys-v1.d"
#if !(defined(HAVE_FORK) && defined(HAVE_PIPE) && defined(HAVE_WAITPID))
#define EXEC_TEMPFILE_ONLY
#endif
/* Tell libgcrypt not to use its own libgpg-error implementation. */
#define USE_LIBGPG_ERROR 1
#include "g10defs.h"
])
AM_MAINTAINER_MODE
# Checks for programs.
AC_PROG_MAKE_SET
AM_SANITY_CHECK
missing_dir=`cd $ac_aux_dir && pwd`
AM_MISSING_PROG(ACLOCAL, aclocal, $missing_dir)
AM_MISSING_PROG(AUTOCONF, autoconf, $missing_dir)
AM_MISSING_PROG(AUTOMAKE, automake, $missing_dir)
AM_MISSING_PROG(AUTOHEADER, autoheader, $missing_dir)
AM_MISSING_PROG(MAKEINFO, makeinfo, $missing_dir)
AC_PROG_AWK
AC_PROG_CC
AC_PROG_CPP
AC_PROG_INSTALL
AC_PROG_LN_S
AC_PROG_MAKE_SET
AC_PROG_RANLIB
AC_CHECK_TOOL(AR, ar, :)
AC_PATH_PROG(PERL,"perl")
AC_ISC_POSIX
AC_SYS_LARGEFILE
AC_CHECK_PROG(DOCBOOK_TO_MAN, docbook-to-man, yes, no)
AM_CONDITIONAL(HAVE_DOCBOOK_TO_MAN, test "$ac_cv_prog_DOCBOOK_TO_MAN" = yes)
GNUPG_CHECK_FAQPROG
GNUPG_CHECK_DOCBOOK_TO_TEXI
try_gettext=yes
have_dosish_system=no
case "${target}" in
*-*-mingw32*)
# special stuff for Windoze NT
ac_cv_have_dev_random=no
AC_DEFINE(USE_ONLY_8DOT3,1,
[set this to limit filenames to the 8.3 format])
AC_DEFINE(HAVE_DRIVE_LETTERS,1,
[defined if we must run on a stupid file system])
AC_DEFINE(USE_SIMPLE_GETTEXT,1,
[because the Unix gettext has too much overhead on
MingW32 systems and these systems lack Posix functions,
we use a simplified version of gettext])
have_dosish_system=yes
try_gettext="no"
;;
i?86-emx-os2 | i?86-*-os2*emx )
# OS/2 with the EMX environment
ac_cv_have_dev_random=no
AC_DEFINE(HAVE_DRIVE_LETTERS)
have_dosish_system=yes
try_gettext="no"
;;
i?86-*-msdosdjgpp*)
# DOS with the DJGPP environment
ac_cv_have_dev_random=no
AC_DEFINE(HAVE_DRIVE_LETTERS)
have_dosish_system=yes
try_gettext="no"
;;
*-*-freebsd*)
# FreeBSD
CPPFLAGS="$CPPFLAGS -I/usr/local/include"
LDFLAGS="$LDFLAGS -L/usr/local/lib"
;;
*-*-hpux*)
if test -z "$GCC" ; then
CFLAGS="$CFLAGS -Ae -D_HPUX_SOURCE"
fi
;;
*-dec-osf4*)
if test -z "$GCC" ; then
# Suppress all warnings
# to get rid of the unsigned/signed char mismatch warnings.
CFLAGS="$CFLAGS -w"
fi
;;
*-dec-osf5*)
if test -z "$GCC" ; then
# Use the newer compiler `-msg_disable ptrmismatch' to
# get rid of the unsigned/signed char mismatch warnings.
# Using this may hide other pointer mismatch warnings, but
# it at least lets other warning classes through
CFLAGS="$CFLAGS -msg_disable ptrmismatch"
fi
;;
m68k-atari-mint)
;;
*)
;;
esac
if test "$have_dosish_system" = yes; then
AC_DEFINE(HAVE_DOSISH_SYSTEM,1,
[defined if we run on some of the PCDOS like systems
(DOS, Windoze. OS/2) with special properties like
no file modes])
fi
AM_CONDITIONAL(HAVE_DOSISH_SYSTEM, test "$have_dosish_system" = yes)
# #
# Checks for libraries. # Checks for libraries.
# #
@ -290,11 +445,199 @@ fi
AC_SUBST(PTH_CFLAGS) AC_SUBST(PTH_CFLAGS)
AC_SUBST(PTH_LIBS) AC_SUBST(PTH_LIBS)
AM_GNU_GETTEXT
dnl Must check for network library requirements before doing link tests
dnl for ldap, for example. If ldap libs are static (or dynamic and without
dnl ELF runtime link paths), then link will fail and LDAP support won't
dnl be detected.
AC_CHECK_FUNC(gethostbyname, , AC_CHECK_LIB(nsl, gethostbyname,
[NETLIBS="-lnsl $NETLIBS"]))
AC_CHECK_FUNC(setsockopt, , AC_CHECK_LIB(socket, setsockopt,
[NETLIBS="-lsocket $NETLIBS"]))
dnl Now try for the resolver functions so we can use DNS SRV
AC_ARG_ENABLE(dns-srv,
AC_HELP_STRING([--disable-dns-srv],[disable the use of DNS SRV in HKP]),
use_dns_srv=$enableval,use_dns_srv=yes)
if test x"$try_hkp" = xyes && test x"$use_dns_srv" = xyes ; then
_srv_save_libs=$LIBS
LIBS=""
# the double underscore thing is a glibc-ism?
AC_SEARCH_LIBS(res_query,resolv bind,,
AC_SEARCH_LIBS(__res_query,resolv bind,,use_dns_srv=no))
AC_SEARCH_LIBS(dn_expand,resolv bind,,
AC_SEARCH_LIBS(__dn_expand,resolv bind,,use_dns_srv=no))
AC_SEARCH_LIBS(dn_skipname,resolv bind,,
AC_SEARCH_LIBS(__dn_skipname,resolv bind,,use_dns_srv=no))
if test x"$use_dns_srv" = xyes ; then
AC_DEFINE(USE_DNS_SRV,1,[define to use DNS SRV])
SRVLIBS=$LIBS
else
AC_MSG_WARN([Resolver functions not found. Disabling DNS SRV.])
fi
LIBS=$_srv_save_libs
fi
AC_SUBST(SRVLIBS)
# Try and link a LDAP test program to weed out unusable LDAP
# libraries. -lldap [-llber [-lresolv]] is for OpenLDAP. OpenLDAP in
# general is terrible with creating weird dependencies. If all else
# fails, the user can play guess-the-dependency by using something
# like ./configure LDAPLIBS="-Lfoo -lbar"
if test "$try_ldap" = yes ; then
for MY_LDAPLIBS in ${LDAPLIBS+"$LDAPLIBS"} "-lldap" "-lldap -llber" "-lldap -llber -lresolv"; do
_ldap_save_libs=$LIBS
LIBS="$MY_LDAPLIBS $NETLIBS $LIBS"
AC_MSG_CHECKING([whether LDAP via \"$MY_LDAPLIBS\" is present and sane])
AC_TRY_LINK([#include <ldap.h>],[ldap_open("foobar",1234);],
[gnupg_cv_func_ldap_init=yes],[gnupg_cv_func_ldap_init=no])
AC_MSG_RESULT([$gnupg_cv_func_ldap_init])
if test $gnupg_cv_func_ldap_init = no; then
AC_MSG_CHECKING([whether I can make LDAP be sane with lber.h])
AC_TRY_LINK([#include <lber.h>
#include <ldap.h>],[ldap_open("foobar",1234);],
[gnupg_cv_func_ldaplber_init=yes],[gnupg_cv_func_ldaplber_init=no])
AC_MSG_RESULT([$gnupg_cv_func_ldaplber_init])
fi
if test "$gnupg_cv_func_ldaplber_init" = yes ; then
AC_DEFINE(NEED_LBER_H,1,[Define if the LDAP library requires including lber.h before ldap.h])
fi
if test "$gnupg_cv_func_ldap_init" = yes || \
test "$gnupg_cv_func_ldaplber_init" = yes ; then
LDAPLIBS=$MY_LDAPLIBS
GPGKEYS_LDAP="gpgkeys_ldap$EXEEXT"
AC_MSG_CHECKING([whether LDAP supports ldap_get_option])
if test "$gnupg_cv_func_ldap_init" = yes ; then
AC_TRY_LINK([#include <ldap.h>],
[ldap_get_option((void *)0,0,(void *)0);],
[gnupg_cv_func_ldap_get_option=yes],
[gnupg_cv_func_ldap_get_option=no])
else
AC_TRY_LINK([#include <lber.h>
#include <ldap.h>],[ldap_get_option((void *)0,0,(void *)0);],
[gnupg_cv_func_ldap_get_option=yes],
[gnupg_cv_func_ldap_get_option=no])
fi
AC_MSG_RESULT([$gnupg_cv_func_ldap_get_option])
if test "$gnupg_cv_func_ldap_get_option" = yes ; then
AC_DEFINE(HAVE_LDAP_GET_OPTION,1,[Define if the LDAP library has ldap_get_option])
else
AC_MSG_CHECKING([whether LDAP supports ld_errno])
if test "$gnupg_cv_func_ldap_init" = yes ; then
AC_TRY_COMPILE([#include <ldap.h>],
[LDAP *ldap; ldap->ld_errno;],
[gnupg_cv_func_ldap_ld_errno=yes],
[gnupg_cv_func_ldap_ld_errno=no])
else
AC_TRY_LINK([#include <lber.h>
#include <ldap.h>],[LDAP *ldap; ldap->ld_errno;],
[gnupg_cv_func_ldap_ld_errno=yes],
[gnupg_cv_func_ldap_ld_errno=no])
fi
AC_MSG_RESULT([$gnupg_cv_func_ldap_ld_errno])
if test "$gnupg_cv_func_ldap_ld_errno" = yes ; then
AC_DEFINE(HAVE_LDAP_LD_ERRNO,1,[Define if the LDAP library supports ld_errno])
fi
fi
fi
LIBS=$_ldap_save_libs
if test "$GPGKEYS_LDAP" != "" ; then break; fi
done
fi
AC_SUBST(GPGKEYS_LDAP)
AC_SUBST(LDAPLIBS)
dnl This isn't necessarily sendmail itself, but anything that gives a
dnl sendmail-ish interface to the outside world. That includes qmail,
dnl postfix, etc. Basically, anything that can handle "sendmail -t".
if test "$try_mailto" = yes ; then
AC_ARG_WITH(mailprog,[ --with-mailprog=NAME use "NAME -t" for mail transport],,with_mailprog=yes)
if test "$with_mailprog" = yes ; then
AC_PATH_PROG(SENDMAIL,sendmail,,$PATH:/usr/sbin:/usr/libexec:/usr/lib)
if test "$ac_cv_path_SENDMAIL" ; then
GPGKEYS_MAILTO="gpgkeys_mailto"
fi
elif test "$with_mailprog" != no ; then
AC_MSG_CHECKING([for a mail transport program])
AC_SUBST(SENDMAIL,$with_mailprog)
AC_MSG_RESULT($with_mailprog)
GPGKEYS_MAILTO="gpgkeys_mailto"
fi
fi
AC_SUBST(GPGKEYS_MAILTO)
case "${target}" in
*-*-mingw32*)
PRINTABLE_OS_NAME="MingW32"
;;
*-*-cygwin*)
PRINTABLE_OS_NAME="Cygwin"
;;
i?86-emx-os2 | i?86-*-os2*emx )
PRINTABLE_OS_NAME="OS/2"
;;
i?86-*-msdosdjgpp*)
PRINTABLE_OS_NAME="MSDOS/DJGPP"
try_dynload=no
;;
*-linux*)
PRINTABLE_OS_NAME="GNU/Linux"
;;
dnl let that after linux to avoid gnu-linux problems
*-gnu*)
PRINTABLE_OS_NAME="GNU/Hurd"
;;
*)
PRINTABLE_OS_NAME=`uname -s || echo "Unknown"`
;;
esac
AC_DEFINE_UNQUOTED(PRINTABLE_OS_NAME, "$PRINTABLE_OS_NAME",
[A human readable text with the name of the OS])
if test "$try_gettext" = yes; then
AM_GNU_GETTEXT(,[need-ngettext])
# gettext requires some extra checks. These really should be part of
# the basic AM_GNU_GETTEXT macro. TODO: move other gettext-specific
# function checks to here.
AC_CHECK_FUNCS(strchr)
else
USE_NLS=no
USE_INCLUDED_LIBINTL=no
BUILD_INCLUDED_LIBINTL=no
AC_SUBST(USE_NLS)
AC_SUBST(USE_INCLUDED_LIBINTL)
AC_SUBST(BUILD_INCLUDED_LIBINTL)
fi
# Checks for header files. # Checks for header files.
AC_HEADER_STDC AC_HEADER_STDC
AC_CHECK_HEADERS([string.h locale.h]) AC_CHECK_HEADERS(string.h unistd.h langinfo.h termio.h locale.h)
# Checks for typedefs, structures, and compiler characteristics. # Checks for typedefs, structures, and compiler characteristics.
AC_C_CONST AC_C_CONST
@ -303,34 +646,270 @@ AC_TYPE_SIZE_T
AC_TYPE_SIGNAL AC_TYPE_SIGNAL
AC_DECL_SYS_SIGLIST AC_DECL_SYS_SIGLIST
GNUPG_CHECK_ENDIAN
GNUPG_CHECK_TYPEDEF(byte, HAVE_BYTE_TYPEDEF) GNUPG_CHECK_TYPEDEF(byte, HAVE_BYTE_TYPEDEF)
GNUPG_CHECK_TYPEDEF(ushort, HAVE_USHORT_TYPEDEF) GNUPG_CHECK_TYPEDEF(ushort, HAVE_USHORT_TYPEDEF)
GNUPG_CHECK_TYPEDEF(ulong, HAVE_ULONG_TYPEDEF) GNUPG_CHECK_TYPEDEF(ulong, HAVE_ULONG_TYPEDEF)
GNUPG_CHECK_TYPEDEF(u16, HAVE_U16_TYPEDEF)
GNUPG_CHECK_TYPEDEF(u32, HAVE_U32_TYPEDEF)
AC_CHECK_SIZEOF(unsigned short)
AC_CHECK_SIZEOF(unsigned int)
AC_CHECK_SIZEOF(unsigned long)
AC_CHECK_SIZEOF(unsigned long long)
AC_CHECK_SIZEOF(uint64_t)
if test "$ac_cv_sizeof_unsigned_short" = "0" \
|| test "$ac_cv_sizeof_unsigned_int" = "0" \
|| test "$ac_cv_sizeof_unsigned_long" = "0"; then
AC_MSG_WARN([Hmmm, something is wrong with the sizes - using defaults]);
fi
dnl Do we have any 64-bit data types?
if test "$ac_cv_sizeof_unsigned_int" != "8" \
&& test "$ac_cv_sizeof_unsigned_long" != "8" \
&& test "$ac_cv_sizeof_unsigned_long_long" != "8" \
&& test "$ac_cv_sizeof_uint64_t" != "8"; then
AC_MSG_WARN([No 64-bit types. Disabling TIGER/192, SHA-384, and SHA-512])
else
if test x"$use_tiger192" = xyes ; then
AC_SUBST(TIGER_O,tiger.o)
AC_DEFINE(USE_TIGER192,1,[Define to include the TIGER/192 digest])
fi
if test "$use_old_tiger192" = yes ; then
AC_SUBST(TIGER_O,tiger.o)
AC_DEFINE(USE_TIGER192,1,[Define to include the TIGER/192 digest])
AC_DEFINE(USE_OLD_TIGER,1,[Define to use the old fake OID for TIGER/192 digest support])
fi
if test x"$use_sha512" = xyes ; then
AC_SUBST(SHA512_O,sha512.o)
AC_DEFINE(USE_SHA512,1,[Define to include the SHA-384 and SHA-512 digests])
fi
fi
GNUPG_SYS_SO_PEERCRED GNUPG_SYS_SO_PEERCRED
# Checks for library functions. # Checks for library functions.
AC_FUNC_FSEEKO
AC_FUNC_VPRINTF
AC_FUNC_FORK
AC_CHECK_FUNCS(strerror stpcpy strsep strlwr tcgetattr strtoul mmap)
AC_CHECK_FUNCS(strcasecmp strncasecmp ctermid times)
AC_CHECK_FUNCS(memmove gettimeofday getrusage setrlimit clock_gettime)
AC_CHECK_FUNCS(atexit raise getpagesize strftime nl_langinfo setlocale)
AC_CHECK_FUNCS(waitpid wait4 sigaction sigprocmask rand pipe stat)
# These are needed by libjnlib - fixme: we should have a macros for them # These are needed by libjnlib - fixme: we should have a macros for them
AC_CHECK_FUNCS(memicmp stpcpy strlwr strtoul memmove stricmp strtol) AC_CHECK_FUNCS(memicmp stpcpy strlwr strtoul memmove stricmp strtol)
AC_CHECK_FUNCS(getrusage setrlimit stat setlocale) AC_CHECK_FUNCS(getrusage setrlimit stat setlocale)
AC_CHECK_FUNCS(flockfile funlockfile) AC_CHECK_FUNCS(flockfile funlockfile)
AC_CHECK_FUNCS(sigaction sigprocmask)
AC_REPLACE_FUNCS(vasprintf) AC_REPLACE_FUNCS(vasprintf)
AC_REPLACE_FUNCS(fopencookie) AC_REPLACE_FUNCS(fopencookie)
# FIXME: Print a warning when fopencookie is not available.
AC_REPLACE_FUNCS(mkdtemp) AC_REPLACE_FUNCS(mkdtemp)
AC_REPLACE_FUNCS(fseeko ftello) AC_REPLACE_FUNCS(fseeko ftello)
AC_REPLACE_FUNCS(isascii) AC_REPLACE_FUNCS(isascii)
AC_REPLACE_FUNCS(putc_unlocked) AC_REPLACE_FUNCS(putc_unlocked)
#
# check for gethrtime and run a testprogram to see whether
# it is broken. It has been reported that some Solaris and HP UX systems
# raise an SIGILL
#
AC_CACHE_CHECK([for gethrtime],
[gnupg_cv_func_gethrtime],
[AC_TRY_LINK([#include <sys/times.h>],[
hrtime_t tv;
tv = gethrtime();
],
[gnupg_cv_func_gethrtime=yes],
[gnupg_cv_func_gethrtime=no])
])
if test $gnupg_cv_func_gethrtime = yes; then
AC_DEFINE([HAVE_GETHRTIME], 1,
[Define if you have the `gethrtime(2)' function.])
AC_CACHE_CHECK([whether gethrtime is broken],
[gnupg_cv_func_broken_gethrtime],
[AC_TRY_RUN([
#include <sys/times.h>
int main () {
hrtime_t tv;
tv = gethrtime();
}
],
[gnupg_cv_func_broken_gethrtime=no],
[gnupg_cv_func_broken_gethrtime=yes],
[gnupg_cv_func_broken_gethrtime=assume-no])
])
if test $gnupg_cv_func_broken_gethrtime = yes; then
AC_DEFINE([HAVE_BROKEN_GETHRTIME], 1,
[Define if `gethrtime(2)' does not work correctly i.e. issues a SIGILL.])
fi
fi
GNUPG_CHECK_MLOCK
GNUPG_FUNC_MKDIR_TAKES_ONE_ARG
dnl
dnl Check whether we can use Linux capabilities as requested
dnl
if test "$use_capabilities" = "yes" ; then
use_capabilities=no
AC_CHECK_HEADERS(sys/capability.h)
if test "$ac_cv_header_sys_capability_h" = "yes" ; then
AC_CHECK_LIB(cap, cap_init, ac_need_libcap=1)
if test "$ac_cv_lib_cap_cap_init" = "yes"; then
AC_DEFINE(USE_CAPABILITIES,1,
[define if capabilities should be used])
AC_SUBST(CAPLIBS,"-lcap")
use_capabilities=yes
fi
fi
if test "$use_capabilities" = "no" ; then
AC_MSG_WARN([[
***
*** The use of capabilities on this system is not possible.
*** You need a recent Linux kernel and some patches:
*** fcaps-2.2.9-990610.patch (kernel patch for 2.2.9)
*** fcap-module-990613.tar.gz (kernel module)
*** libcap-1.92.tar.gz (user mode library and utilities)
*** And you have to configure the kernel with CONFIG_VFS_CAP_PLUGIN
*** set (filesystems menu). Be warned: This code is *really* ALPHA.
***]])
fi
fi
# Sanity check regex. Tests adapted from mutt.
AC_MSG_CHECKING([whether regular expression support is requested])
AC_ARG_ENABLE(regex,
[ --disable-regex do not handle regular expressions in trust sigs],
use_regex=$enableval, use_regex=yes)
AC_MSG_RESULT($use_regex)
if test "$use_regex" = yes ; then
AC_MSG_CHECKING([whether the included regex lib is requested])
AC_ARG_WITH(included-regex,
[ --with-included-regex use the included GNU regex library],
[gnupg_cv_included_regex=yes],[gnupg_cv_included_regex=no])
AC_MSG_RESULT($gnupg_cv_included_regex)
if test $gnupg_cv_included_regex = no ; then
# Does the system have regex functions at all?
AC_CHECK_FUNC(regcomp,gnupg_cv_included_regex=no,
gnupg_cv_included_regex=yes)
fi
if test $gnupg_cv_included_regex = no ; then
AC_CACHE_CHECK([whether your system's regexp library is broken],
[gnupg_cv_regex_broken],
AC_TRY_RUN([
#include <unistd.h>
#include <regex.h>
main() { regex_t blah ; regmatch_t p; p.rm_eo = p.rm_eo; return regcomp(&blah, "foo.*bar", REG_NOSUB) || regexec (&blah, "foobar", 0, NULL, 0); }],
gnupg_cv_regex_broken=no, gnupg_cv_regex_broken=yes, gnupg_cv_regex_broken=yes))
if test $gnupg_cv_regex_broken = yes ; then
AC_MSG_WARN(your regex is broken - using the included GNU regex instead.)
gnupg_cv_included_regex=yes
fi
fi
if test $gnupg_cv_included_regex = yes; then
AC_DEFINE(USE_GNU_REGEX,1,[ Define if you want to use the included regex lib ])
AC_SUBST(REGEX_O,regex.o)
fi
else
AC_DEFINE(DISABLE_REGEX,1,[ Define to disable regular expression support ])
fi
dnl Do we have zlib? Must do it here because Solaris failed
dnl when compiling a conftest (due to the "-lz" from LIBS).
use_local_zlib=yes
if test "$g10_force_zlib" = "yes"; then
:
else
_cppflags="${CPPFLAGS}"
_ldflags="${LDFLAGS}"
AC_ARG_WITH(zlib,
[ --with-zlib=DIR use libz in DIR],[
if test -d "$withval"; then
CPPFLAGS="${CPPFLAGS} -I$withval/include"
LDFLAGS="${LDFLAGS} -L$withval/lib"
fi
])
AC_CHECK_HEADER(zlib.h,
AC_CHECK_LIB(z, deflateInit2_,
use_local_zlib=no
LIBS="$LIBS -lz",
CPPFLAGS=${_cppflags} LDFLAGS=${_ldflags}),
CPPFLAGS=${_cppflags} LDFLAGS=${_ldflags})
fi
if test "$use_local_zlib" = yes ; then
AM_CONDITIONAL(ENABLE_LOCAL_ZLIB, true)
AC_CONFIG_LINKS(zlib.h:zlib/zlib.h zconf.h:zlib/zconf.h )
ZLIBS="../zlib/libzlib.a"
else
AM_CONDITIONAL(ENABLE_LOCAL_ZLIB, false)
ZLIBS=
fi
AC_SUBST(ZLIBS)
# Allow users to append something to the version string without
# flagging it as development version. The user version parts is
# considered everything after a dash.
if test "$development_version" != yes; then
changequote(,)dnl
tmp_pat='[a-zA-Z]'
changequote([,])dnl
if echo "$VERSION" | sed 's/-.*//' | grep "$tmp_pat" >/dev/null ; then
development_version=yes
fi
fi
if test "$development_version" = yes; then
AC_DEFINE(IS_DEVELOPMENT_VERSION,1,
[Defined if this is not a regular release])
fi
AM_CONDITIONAL(CROSS_COMPILING, test x$cross_compiling = xyes)
GNUPG_CHECK_GNUMAKE
# add some extra libs here so that previous tests don't fail for
# mysterious reasons - the final link step should bail out.
case "${target}" in
*-*-mingw32*)
LIBS="$LIBS -lwsock32"
;;
*)
;;
esac
if test "$GCC" = yes; then
if test "$USE_MAINTAINER_MODE" = "yes"; then
CFLAGS="$CFLAGS -Wall -Wcast-align -Wshadow -Wstrict-prototypes"
else
CFLAGS="$CFLAGS -Wall"
fi
fi
AC_SUBST(NETLIBS)
# We use jnlib, so tell other modules about it # We use jnlib, so tell other modules about it
AC_DEFINE(HAVE_JNLIB_LOGGING, 1, AC_DEFINE(HAVE_JNLIB_LOGGING, 1,
[Defined if jnlib style logging fucntions are available]) [Defined if jnlib style logging fucntions are available])
# #
# Decide what to build # Decide what to build
# #
@ -369,6 +948,64 @@ AM_CONDITIONAL(BUILD_AGENT, test "$build_agent" = "yes")
AM_CONDITIONAL(BUILD_SCDAEMON, test "$build_scdaemon" = "yes") AM_CONDITIONAL(BUILD_SCDAEMON, test "$build_scdaemon" = "yes")
AC_CONFIG_COMMANDS(g10defs.h,[[
cat >g10defs.tmp <<G10EOF
/* Generated automatically by configure */
/* FIXME: Shouldn't we replace GNUPG_HOMEDIR by GNUPG_DEFAULT_HOMEDIR
and we propably can get rid of g10defs.h */
#ifdef HAVE_DRIVE_LETTERS
/*#define G10_LOCALEDIR "c:\\\\lib\\\\gnupg\\\\locale"*/
#define GNUPG_LIBDIR "c:\\\\lib\\\\gnupg"
#define GNUPG_LIBEXECDIR "c:\\\\lib\\\\gnupg"
#define GNUPG_DATADIR "c:\\\\lib\\\\gnupg"
#define GNUPG_HOMEDIR "c:\\\\gnupg"
#else
/*#define G10_LOCALEDIR "${datadir}/locale"*/
#define GNUPG_LIBDIR "${libdir}/gnupg"
#define GNUPG_DATADIR "${datadir}/gnupg"
#ifdef __VMS
#define GNUPG_HOMEDIR "/SYS\$LOGIN/gnupg"
#else
#define GNUPG_HOMEDIR "~/.gnupg2"
#endif
#endif
/* those are here to be redefined by handcrafted g10defs.h.
Please note that the string version must not contain more
than one character because the using code assumes strlen()==1 */
#ifdef HAVE_DOSISH_SYSTEM
#define DIRSEP_C '\\\\'
#define EXTSEP_C '.'
#define DIRSEP_S "\\\\"
#define EXTSEP_S "."
#else
#define DIRSEP_C '/'
#define EXTSEP_C '.'
#define DIRSEP_S "/"
#define EXTSEP_S "."
#endif
/* This is the same as VERSION, but should be overridden if the
platform cannot handle things like dots '.' in filenames. */
#define SAFE_VERSION VERSION
G10EOF
## Do we really need the following? It defines BYTES_PER-MPI_LIMB
## cat mpi/mpi-asm-defs.h >>g10defs.tmp
if cmp -s g10defs.h g10defs.tmp 2>/dev/null; then
echo "g10defs.h is unchanged"
rm -f g10defs.tmp
else
rm -f g10defs.h
mv g10defs.tmp g10defs.h
echo "g10defs.h created"
fi
]],[[
prefix=$prefix
exec_prefix=$exec_prefix
libdir=$libdir
libexecdir=$libexecdir
datadir=$datadir
DATADIRNAME=$DATADIRNAME
]])
AC_CONFIG_FILES([ m4/Makefile AC_CONFIG_FILES([ m4/Makefile
Makefile Makefile
po/Makefile.in po/Makefile.in
@ -389,6 +1026,8 @@ AC_OUTPUT
echo " echo "
GnuPG v${VERSION} has been configured as follows: GnuPG v${VERSION} has been configured as follows:
Platform: $PRINTABLE_OS_NAME ($target)
OpenPGP: $build_gpg OpenPGP: $build_gpg
S/MIME: $build_gpgsm S/MIME: $build_gpgsm
Agent: $build_agent $build_agent_threaded Agent: $build_agent $build_agent_threaded

View File

@ -1,4 +1,71 @@
2003-06-04 Werner Koch <wk@gnupg.org> 2003-06-18 Werner Koch <wk@gnupg.org>
Finished the bulk of changes for gnupg 1.9. This included
switching to libgcrypt functions, using shared error codes from
libgpg-error, replacing the old functions we used to have in
../util by those in ../jnlib and ../common, renaming the malloc
functions and a couple of types. Note, that not all changes are
listed below becuause they are too similar and done at far too
many places. As of today the code builds using the current
libgcrypt from CVS but it is very unlikely that it actually works.
* sig-check.c (cmp_help): Removed. Was never used.
* pkglue.c: New. Most stuff taken from gnupg 1.1.2.
* pkglue.h: New.
* misc.c (pull_in_libs): Removed.
* keygen.c (count_chr): New.
(ask_user_id): Removed faked RNG support.
* misc.c (openpgp_md_map_name,openpgp_cipher_map_name)
(openpgp_pk_map_name): New.
* skclist.c (build_sk_list): Removed faked RNG support.
(is_insecure): Removed.
* comment.c (make_mpi_comment_node): Use gcry MPI print function.
* keyid.c (v3_keyid): New.
* misc.c (mpi_write,mpi_write_opaque,mpi_read,mpi_read_opaque)
(mpi_print): New. Taken from gnupg 1.1.2.
(checksum_mpi): Replaced by implementation from 1.1.2.
* g10.c (my_strusage): Renamed from strusage and return NULL
instead calling a default function.
(add_to_strlist2): New. Taken from ../util/strgutil.c of gnupg 1.2.
* plaintext.c (handle_plaintext): New arg CREATE_FILE to cope with
the fact that gpg-error does not have this error code anymore.
* mainproc.c (symkey_decrypt_sesskey): Ditto.
* seskey.c (make_session_key): Adjusted for use with libgcrypt.
(encode_session_key): Ditto.
(do_encode_md): Ditto.
(encode_md_value): Ditto.
* keyring.c: Use libgpg-error instead of READ_ERROR etc.
* g10.c: Adjusted all algorithm name/id mapping functions.
(set_debug): Pass MPI and CRYPTO debug values to libgcrypt.
* Makefile.am (INCLUDES): Define LOCALEDIR and the default error
source.
* g10.c (i18n_init): s/G10_LOCALEDIR/LOCALEDIR/.
Renamed m_alloc et al to xmalloc et al.
s/g10_errstr/gpg_strerror/
s/MPI/gcry_mpi_t/
Adjusted all md_open calls to the libgcrypt API.
* build-packet.c (do_comment): Return error code from iobuf write
function.
(do_user_id): Ditto.
(do_public_key): Ditto.
* Makefile.am: Add new files, link gpg with libgpg-error. * Makefile.am: Add new files, link gpg with libgpg-error.
* g10.c, options.h: New option --agent-program. * g10.c, options.h: New option --agent-program.

View File

@ -19,16 +19,17 @@
## Process this file with automake to produce Makefile.in ## Process this file with automake to produce Makefile.in
INCLUDES = -I$(top_srcdir)/include -I$(top_srcdir)/intl localedir = $(datadir)/locale
INCLUDES = -I$(top_srcdir)/common -I$(top_srcdir)/include -I$(top_srcdir)/intl -DLOCALEDIR=\"$(localedir)\"
EXTRA_DIST = options.skel EXTRA_DIST = options.skel
# it seems that we can't use this with automake 1.5 # it seems that we can't use this with automake 1.5
#OMIT_DEPENDENCIES = zlib.h zconf.h #OMIT_DEPENDENCIES = zlib.h zconf.h
libexecdir = @libexecdir@/@PACKAGE@ libexecdir = @libexecdir@/@PACKAGE@
# FIXME: Windows support currently not enabled if ! HAVE_DOSISH_SYSTEM
#if ! HAVE_DOSISH_SYSTEM AM_CFLAGS = -DGNUPG_LIBEXECDIR="\"$(libexecdir)\""
#AM_CFLAGS = -DGNUPG_LIBEXECDIR="\"$(libexecdir)\"" endif
#endif needed_libs = ../common/libcommon.a ../jnlib/libjnlib.a
needed_libs = ../cipher/libcipher.a ../mpi/libmpi.a ../util/libutil.a
#noinst_PROGRAMS = gpgd #noinst_PROGRAMS = gpgd
bin_PROGRAMS = gpg gpgv bin_PROGRAMS = gpg gpgv
@ -62,6 +63,7 @@ common_source = \
plaintext.c \ plaintext.c \
sig-check.c \ sig-check.c \
keylist.c \ keylist.c \
pkglue.c pkglue.h \
signal.c signal.c
gpg_SOURCES = g10.c \ gpg_SOURCES = g10.c \
@ -108,8 +110,9 @@ gpgv_SOURCES = gpgv.c \
# ks-db.h \ # ks-db.h \
# $(common_source) # $(common_source)
LDADD = $(needed_libs) @INTLLIBS@ @CAPLIBS@ @ZLIBS@ LDADD = $(needed_libs) @INTLLIBS@ @CAPLIBS@ @ZLIBS@
gpg_LDADD = $(LDADD) @DLLIBS@ @EGDLIBS@ -lgpg-error gpg_LDADD = $(LIBGCRYPT_LIBS) $(LDADD) -lassuan -lgpg-error
gpgv_LDADD = $(LIBGCRYPT_LIBS) $(LDADD) -lassuan -lgpg-error
$(PROGRAMS): $(needed_libs) $(PROGRAMS): $(needed_libs)

View File

@ -27,6 +27,7 @@
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include "gpg.h"
#include "errors.h" #include "errors.h"
#include "iobuf.h" #include "iobuf.h"
#include "memory.h" #include "memory.h"
@ -192,7 +193,7 @@ is_armored( const byte *buf )
* filter to do further processing. * filter to do further processing.
*/ */
int int
use_armor_filter( IOBUF a ) use_armor_filter( iobuf_t a )
{ {
byte buf[1]; byte buf[1];
int n; int n;
@ -337,7 +338,7 @@ parse_header_line( armor_filter_context_t *afx, byte *line, unsigned int len )
int hashes=0; int hashes=0;
unsigned int len2; unsigned int len2;
len2 = check_trailing_ws( line, len ); len2 = length_sans_trailing_ws( line, len );
if( !len2 ) { if( !len2 ) {
afx->buffer_pos = len2; /* (it is not the fine way to do it here) */ afx->buffer_pos = len2; /* (it is not the fine way to do it here) */
return 0; /* WS only: same as empty line */ return 0; /* WS only: same as empty line */
@ -376,7 +377,7 @@ parse_header_line( armor_filter_context_t *afx, byte *line, unsigned int len )
/* figure out whether the data is armored or not */ /* figure out whether the data is armored or not */
static int static int
check_input( armor_filter_context_t *afx, IOBUF a ) check_input( armor_filter_context_t *afx, iobuf_t a )
{ {
int rc = 0; int rc = 0;
int i; int i;
@ -418,7 +419,7 @@ check_input( armor_filter_context_t *afx, IOBUF a )
if( hdr_line == BEGIN_SIGNED_MSG_IDX ) { if( hdr_line == BEGIN_SIGNED_MSG_IDX ) {
if( afx->in_cleartext ) { if( afx->in_cleartext ) {
log_error(_("nested clear text signatures\n")); log_error(_("nested clear text signatures\n"));
rc = G10ERR_INVALID_ARMOR; rc = GPG_ERR_INV_ARMOR;
} }
afx->in_cleartext = 1; afx->in_cleartext = 1;
} }
@ -448,7 +449,7 @@ check_input( armor_filter_context_t *afx, IOBUF a )
i = parse_header_line( afx, line, len ); i = parse_header_line( afx, line, len );
if( i <= 0 ) { if( i <= 0 ) {
if( i ) if( i )
rc = G10ERR_INVALID_ARMOR; rc = GPG_ERR_INV_ARMOR;
break; break;
} }
} }
@ -476,7 +477,7 @@ check_input( armor_filter_context_t *afx, IOBUF a )
* not implemented/checked. * not implemented/checked.
*/ */
static int static int
fake_packet( armor_filter_context_t *afx, IOBUF a, fake_packet( armor_filter_context_t *afx, iobuf_t a,
size_t *retn, byte *buf, size_t size ) size_t *retn, byte *buf, size_t size )
{ {
int rc = 0; int rc = 0;
@ -615,12 +616,12 @@ invalid_crc(void)
if ( opt.ignore_crc_error ) if ( opt.ignore_crc_error )
return 0; return 0;
log_inc_errorcount(); log_inc_errorcount();
return G10ERR_INVALID_ARMOR; return GPG_ERR_INV_ARMOR;
} }
static int static int
radix64_read( armor_filter_context_t *afx, IOBUF a, size_t *retn, radix64_read( armor_filter_context_t *afx, iobuf_t a, size_t *retn,
byte *buf, size_t size ) byte *buf, size_t size )
{ {
byte val; byte val;
@ -785,11 +786,11 @@ radix64_read( armor_filter_context_t *afx, IOBUF a, size_t *retn,
rc = 0; rc = 0;
else if( rc == 2 ) { else if( rc == 2 ) {
log_error(_("premature eof (in Trailer)\n")); log_error(_("premature eof (in Trailer)\n"));
rc = G10ERR_INVALID_ARMOR; rc = GPG_ERR_INV_ARMOR;
} }
else { else {
log_error(_("error in trailer line\n")); log_error(_("error in trailer line\n"));
rc = G10ERR_INVALID_ARMOR; rc = GPG_ERR_INV_ARMOR;
} }
#endif #endif
} }
@ -808,7 +809,7 @@ radix64_read( armor_filter_context_t *afx, IOBUF a, size_t *retn,
*/ */
int int
armor_filter( void *opaque, int control, armor_filter( void *opaque, int control,
IOBUF a, byte *buf, size_t *ret_len) iobuf_t a, byte *buf, size_t *ret_len)
{ {
size_t size = *ret_len; size_t size = *ret_len;
armor_filter_context_t *afx = opaque; armor_filter_context_t *afx = opaque;
@ -1081,7 +1082,7 @@ armor_filter( void *opaque, int control,
if( afx->qp_detected ) if( afx->qp_detected )
log_error(_("quoted printable character in armor - " log_error(_("quoted printable character in armor - "
"probably a buggy MTA has been used\n") ); "probably a buggy MTA has been used\n") );
m_free( afx->buffer ); xfree ( afx->buffer );
afx->buffer = NULL; afx->buffer = NULL;
} }
else if( control == IOBUFCTRL_DESC ) else if( control == IOBUFCTRL_DESC )
@ -1098,7 +1099,7 @@ make_radix64_string( const byte *data, size_t len )
{ {
char *buffer, *p; char *buffer, *p;
buffer = p = m_alloc( (len+2)/3*4 + 1 ); buffer = p = xmalloc ( (len+2)/3*4 + 1 );
for( ; len >= 3 ; len -= 3, data += 3 ) { for( ; len >= 3 ; len -= 3, data += 3 ) {
*p++ = bintoasc[(data[0] >> 2) & 077]; *p++ = bintoasc[(data[0] >> 2) & 077];
*p++ = bintoasc[(((data[0] <<4)&060)|((data[1] >> 4)&017))&077]; *p++ = bintoasc[(((data[0] <<4)&060)|((data[1] >> 4)&017))&077];
@ -1158,14 +1159,14 @@ unarmor_pump_new (void)
if( !is_initialized ) if( !is_initialized )
initialize(); initialize();
x = m_alloc_clear (sizeof *x); x = xcalloc (1,sizeof *x);
return x; return x;
} }
void void
unarmor_pump_release (UnarmorPump x) unarmor_pump_release (UnarmorPump x)
{ {
m_free (x); xfree (x);
} }
/* /*

View File

@ -25,6 +25,7 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "packet.h" #include "packet.h"
#include "errors.h" #include "errors.h"
#include "iobuf.h" #include "iobuf.h"
@ -35,28 +36,28 @@
#include "options.h" #include "options.h"
static int do_comment( IOBUF out, int ctb, PKT_comment *rem ); static int do_comment( iobuf_t out, int ctb, PKT_comment *rem );
static int do_user_id( IOBUF out, int ctb, PKT_user_id *uid ); static int do_user_id( iobuf_t out, int ctb, PKT_user_id *uid );
static int do_public_key( IOBUF out, int ctb, PKT_public_key *pk ); static int do_public_key( iobuf_t out, int ctb, PKT_public_key *pk );
static int do_secret_key( IOBUF out, int ctb, PKT_secret_key *pk ); static int do_secret_key( iobuf_t out, int ctb, PKT_secret_key *pk );
static int do_symkey_enc( IOBUF out, int ctb, PKT_symkey_enc *enc ); static int do_symkey_enc( iobuf_t out, int ctb, PKT_symkey_enc *enc );
static int do_pubkey_enc( IOBUF out, int ctb, PKT_pubkey_enc *enc ); static int do_pubkey_enc( iobuf_t out, int ctb, PKT_pubkey_enc *enc );
static u32 calc_plaintext( PKT_plaintext *pt ); static u32 calc_plaintext( PKT_plaintext *pt );
static int do_plaintext( IOBUF out, int ctb, PKT_plaintext *pt ); static int do_plaintext( iobuf_t out, int ctb, PKT_plaintext *pt );
static int do_encrypted( IOBUF out, int ctb, PKT_encrypted *ed ); static int do_encrypted( iobuf_t out, int ctb, PKT_encrypted *ed );
static int do_encrypted_mdc( IOBUF out, int ctb, PKT_encrypted *ed ); static int do_encrypted_mdc( iobuf_t out, int ctb, PKT_encrypted *ed );
static int do_compressed( IOBUF out, int ctb, PKT_compressed *cd ); static int do_compressed( iobuf_t out, int ctb, PKT_compressed *cd );
static int do_signature( IOBUF out, int ctb, PKT_signature *sig ); static int do_signature( iobuf_t out, int ctb, PKT_signature *sig );
static int do_onepass_sig( IOBUF out, int ctb, PKT_onepass_sig *ops ); static int do_onepass_sig( iobuf_t out, int ctb, PKT_onepass_sig *ops );
static int calc_header_length( u32 len, int new_ctb ); static int calc_header_length( u32 len, int new_ctb );
static int write_16(IOBUF inp, u16 a); static int write_16(iobuf_t inp, u16 a);
static int write_32(IOBUF inp, u32 a); static int write_32(iobuf_t inp, u32 a);
static int write_header( IOBUF out, int ctb, u32 len ); static int write_header( iobuf_t out, int ctb, u32 len );
static int write_sign_packet_header( IOBUF out, int ctb, u32 len ); static int write_sign_packet_header( iobuf_t out, int ctb, u32 len );
static int write_header2( IOBUF out, int ctb, u32 len, int hdrlen, int blkmode ); static int write_header2( iobuf_t out, int ctb, u32 len, int hdrlen, int blkmode );
static int write_new_header( IOBUF out, int ctb, u32 len, int hdrlen ); static int write_new_header( iobuf_t out, int ctb, u32 len, int hdrlen );
static int write_version( IOBUF out, int ctb ); static int write_version( iobuf_t out, int ctb );
/**************** /****************
* Build a packet and write it to INP * Build a packet and write it to INP
@ -65,7 +66,7 @@ static int write_version( IOBUF out, int ctb );
* Note: Caller must free the packet * Note: Caller must free the packet
*/ */
int int
build_packet( IOBUF out, PACKET *pkt ) build_packet( iobuf_t out, PACKET *pkt )
{ {
int new_ctb=0, rc=0, ctb; int new_ctb=0, rc=0, ctb;
int pkttype; int pkttype;
@ -179,51 +180,56 @@ calc_packet_length( PACKET *pkt )
} }
static void static void
write_fake_data( IOBUF out, MPI a ) write_fake_data( iobuf_t out, gcry_mpi_t a )
{ {
if( a ) { if( a ) {
int i; unsigned int n;
void *p; void *p;
p = mpi_get_opaque( a, &i ); assert( gcry_mpi_get_flag (a, GCRYMPI_FLAG_OPAQUE));
iobuf_write( out, p, i ); p = gcry_mpi_get_opaque (a, &n);
iobuf_write (out, p, (n+7)/8);
} }
} }
static int static int
do_comment( IOBUF out, int ctb, PKT_comment *rem ) do_comment (iobuf_t out, int ctb, PKT_comment *rem)
{ {
if( opt.sk_comments ) { int rc = 0;
write_header(out, ctb, rem->len);
if( iobuf_write( out, rem->data, rem->len ) ) if (opt.sk_comments)
return G10ERR_WRITE_FILE; {
write_header(out, ctb, rem->len);
rc = iobuf_write( out, rem->data, rem->len );
} }
return 0; return rc;
} }
static int static int
do_user_id( IOBUF out, int ctb, PKT_user_id *uid ) do_user_id( iobuf_t out, int ctb, PKT_user_id *uid )
{ {
if( uid->attrib_data ) { int rc;
write_header(out, ctb, uid->attrib_len);
if( iobuf_write( out, uid->attrib_data, uid->attrib_len ) ) if (uid->attrib_data)
return G10ERR_WRITE_FILE; {
write_header (out, ctb, uid->attrib_len);
rc = iobuf_write (out, uid->attrib_data, uid->attrib_len );
} }
else { else
write_header(out, ctb, uid->len); {
if( iobuf_write( out, uid->name, uid->len ) ) write_header (out, ctb, uid->len);
return G10ERR_WRITE_FILE; rc = iobuf_write (out, uid->name, uid->len );
} }
return 0; return rc;
} }
static int static int
do_public_key( IOBUF out, int ctb, PKT_public_key *pk ) do_public_key( iobuf_t out, int ctb, PKT_public_key *pk )
{ {
int rc = 0; int rc = 0;
int n, i; int n, i;
IOBUF a = iobuf_temp(); iobuf_t a = iobuf_temp();
if( !pk->version ) if( !pk->version )
iobuf_put( a, 3 ); iobuf_put( a, 3 );
@ -246,8 +252,7 @@ do_public_key( IOBUF out, int ctb, PKT_public_key *pk )
mpi_write(a, pk->pkey[i] ); mpi_write(a, pk->pkey[i] );
write_header2(out, ctb, iobuf_get_temp_length(a), pk->hdrbytes, 1 ); write_header2(out, ctb, iobuf_get_temp_length(a), pk->hdrbytes, 1 );
if( iobuf_write_temp( out, a ) ) rc = iobuf_write_temp (out, a);
rc = G10ERR_WRITE_FILE;
iobuf_close(a); iobuf_close(a);
return rc; return rc;
@ -265,7 +270,7 @@ hash_public_key( MD_HANDLE md, PKT_public_key *pk )
int ctb; int ctb;
ulong pktlen; ulong pktlen;
int c; int c;
IOBUF a = iobuf_temp(); iobuf_t a = iobuf_temp();
#if 0 #if 0
FILE *fp = fopen("dump.pk", "a"); FILE *fp = fopen("dump.pk", "a");
int i=0; int i=0;
@ -278,7 +283,7 @@ hash_public_key( MD_HANDLE md, PKT_public_key *pk )
pkt.pkttype = PKT_PUBLIC_KEY; pkt.pkttype = PKT_PUBLIC_KEY;
pkt.pkt.public_key = pk; pkt.pkt.public_key = pk;
if( (rc = build_packet( a, &pkt )) ) if( (rc = build_packet( a, &pkt )) )
log_fatal("build public_key for hashing failed: %s\n", g10_errstr(rc)); log_fatal("build public_key for hashing failed: %s\n", gpg_strerror (rc));
if( !(pk->version == 3 && pk->pubkey_algo == 16) ) { if( !(pk->version == 3 && pk->pubkey_algo == 16) ) {
/* skip the constructed header but don't do this for our very old /* skip the constructed header but don't do this for our very old
@ -309,10 +314,10 @@ hash_public_key( MD_HANDLE md, PKT_public_key *pk )
} }
} }
/* hash a header */ /* hash a header */
md_putc( md, 0x99 ); gcry_md_putc ( md, 0x99 );
pktlen &= 0xffff; /* can't handle longer packets */ pktlen &= 0xffff; /* can't handle longer packets */
md_putc( md, pktlen >> 8 ); gcry_md_putc ( md, pktlen >> 8 );
md_putc( md, pktlen & 0xff ); gcry_md_putc ( md, pktlen & 0xff );
} }
/* hash the packet body */ /* hash the packet body */
while( (c=iobuf_get(a)) != -1 ) { while( (c=iobuf_get(a)) != -1 ) {
@ -323,7 +328,7 @@ hash_public_key( MD_HANDLE md, PKT_public_key *pk )
i=0; i=0;
} }
#endif #endif
md_putc( md, c ); gcry_md_putc ( md, c );
} }
#if 0 #if 0
putc('\n', fp); putc('\n', fp);
@ -334,11 +339,11 @@ hash_public_key( MD_HANDLE md, PKT_public_key *pk )
static int static int
do_secret_key( IOBUF out, int ctb, PKT_secret_key *sk ) do_secret_key( iobuf_t out, int ctb, PKT_secret_key *sk )
{ {
int rc = 0; int rc = 0;
int i, nskey, npkey; int i, nskey, npkey;
IOBUF a = iobuf_temp(); /* build in a self-enlarging buffer */ iobuf_t a = iobuf_temp(); /* build in a self-enlarging buffer */
/* Write the version number - if none is specified, use 3 */ /* Write the version number - if none is specified, use 3 */
if( !sk->version ) if( !sk->version )
@ -366,7 +371,7 @@ do_secret_key( IOBUF out, int ctb, PKT_secret_key *sk )
/* If we don't have any public parameters - which is the case if /* If we don't have any public parameters - which is the case if
we don't know the algorithm used - the parameters are stored as we don't know the algorithm used - the parameters are stored as
one blob in a faked (opaque) MPI */ one blob in a faked (opaque) gcry_mpi_t */
if( !npkey ) { if( !npkey ) {
write_fake_data( a, sk->skey[0] ); write_fake_data( a, sk->skey[0] );
goto leave; goto leave;
@ -423,19 +428,19 @@ do_secret_key( IOBUF out, int ctb, PKT_secret_key *sk )
else if( sk->is_protected && sk->version >= 4 ) { else if( sk->is_protected && sk->version >= 4 ) {
/* The secret key is protected - write it out as it is */ /* The secret key is protected - write it out as it is */
byte *p; byte *p;
assert( mpi_is_opaque( sk->skey[npkey] ) ); assert( gcry_mpi_get_flag( sk->skey[npkey], GCRYMPI_FLAG_OPAQUE ) );
p = mpi_get_opaque( sk->skey[npkey], &i ); p = gcry_mpi_get_opaque( sk->skey[npkey], &i );
iobuf_write(a, p, i ); iobuf_write(a, p, (i+7)/8 );
} }
else if( sk->is_protected ) { else if( sk->is_protected ) {
/* The secret key is protected te old v4 way. */ /* The secret key is protected the old v4 way. */
for( ; i < nskey; i++ ) { for( ; i < nskey; i++ ) {
byte *p; byte *p;
int ndata; size_t n;
assert (mpi_is_opaque (sk->skey[i])); assert( gcry_mpi_get_flag (sk->skey[i], GCRYMPI_FLAG_OPAQUE));
p = mpi_get_opaque (sk->skey[i], &ndata); p = gcry_mpi_get_opaque( sk->skey[i], &n );
iobuf_write (a, p, ndata); iobuf_write (a, p, (n+7)/8);
} }
write_16(a, sk->csum ); write_16(a, sk->csum );
} }
@ -451,18 +456,17 @@ do_secret_key( IOBUF out, int ctb, PKT_secret_key *sk )
the other stuff, so that we know the length of the packet */ the other stuff, so that we know the length of the packet */
write_header2(out, ctb, iobuf_get_temp_length(a), sk->hdrbytes, 1 ); write_header2(out, ctb, iobuf_get_temp_length(a), sk->hdrbytes, 1 );
/* And finally write it out the real stream */ /* And finally write it out the real stream */
if( iobuf_write_temp( out, a ) ) rc = iobuf_write_temp (out, a );
rc = G10ERR_WRITE_FILE;
iobuf_close(a); /* close the remporary buffer */ iobuf_close(a); /* close the remporary buffer */
return rc; return rc;
} }
static int static int
do_symkey_enc( IOBUF out, int ctb, PKT_symkey_enc *enc ) do_symkey_enc( iobuf_t out, int ctb, PKT_symkey_enc *enc )
{ {
int rc = 0; int rc = 0;
IOBUF a = iobuf_temp(); iobuf_t a = iobuf_temp();
assert( enc->version == 4 ); assert( enc->version == 4 );
switch( enc->s2k.mode ) { switch( enc->s2k.mode ) {
@ -482,8 +486,7 @@ do_symkey_enc( IOBUF out, int ctb, PKT_symkey_enc *enc )
iobuf_write(a, enc->seskey, enc->seskeylen ); iobuf_write(a, enc->seskey, enc->seskeylen );
write_header(out, ctb, iobuf_get_temp_length(a) ); write_header(out, ctb, iobuf_get_temp_length(a) );
if( iobuf_write_temp( out, a ) ) rc = iobuf_write_temp (out, a);
rc = G10ERR_WRITE_FILE;
iobuf_close(a); iobuf_close(a);
return rc; return rc;
@ -493,11 +496,11 @@ do_symkey_enc( IOBUF out, int ctb, PKT_symkey_enc *enc )
static int static int
do_pubkey_enc( IOBUF out, int ctb, PKT_pubkey_enc *enc ) do_pubkey_enc( iobuf_t out, int ctb, PKT_pubkey_enc *enc )
{ {
int rc = 0; int rc = 0;
int n, i; int n, i;
IOBUF a = iobuf_temp(); iobuf_t a = iobuf_temp();
write_version( a, ctb ); write_version( a, ctb );
if( enc->throw_keyid ) { if( enc->throw_keyid ) {
@ -516,8 +519,7 @@ do_pubkey_enc( IOBUF out, int ctb, PKT_pubkey_enc *enc )
mpi_write(a, enc->data[i] ); mpi_write(a, enc->data[i] );
write_header(out, ctb, iobuf_get_temp_length(a) ); write_header(out, ctb, iobuf_get_temp_length(a) );
if( iobuf_write_temp( out, a ) ) rc = iobuf_write_temp (out, a);
rc = G10ERR_WRITE_FILE;
iobuf_close(a); iobuf_close(a);
return rc; return rc;
@ -533,7 +535,7 @@ calc_plaintext( PKT_plaintext *pt )
} }
static int static int
do_plaintext( IOBUF out, int ctb, PKT_plaintext *pt ) do_plaintext( iobuf_t out, int ctb, PKT_plaintext *pt )
{ {
int i, rc = 0; int i, rc = 0;
u32 n; u32 n;
@ -551,15 +553,13 @@ do_plaintext( IOBUF out, int ctb, PKT_plaintext *pt )
iobuf_put(out, pt->namelen ); iobuf_put(out, pt->namelen );
for(i=0; i < pt->namelen; i++ ) for(i=0; i < pt->namelen; i++ )
iobuf_put(out, pt->name[i] ); iobuf_put(out, pt->name[i] );
if( write_32(out, pt->timestamp ) ) rc = write_32 (out, pt->timestamp);
rc = G10ERR_WRITE_FILE;
n = 0; n = 0;
while( (nbytes=iobuf_read(pt->buf, buf, 1000)) != -1 ) { while( (nbytes=iobuf_read(pt->buf, buf, 1000)) != -1 ) {
if( iobuf_write(out, buf, nbytes) == -1 ) { rc = iobuf_write(out, buf, nbytes);
rc = G10ERR_WRITE_FILE; if (rc)
break; break;
}
n += nbytes; n += nbytes;
} }
wipememory(buf,1000); /* burn the buffer */ wipememory(buf,1000); /* burn the buffer */
@ -575,7 +575,7 @@ do_plaintext( IOBUF out, int ctb, PKT_plaintext *pt )
static int static int
do_encrypted( IOBUF out, int ctb, PKT_encrypted *ed ) do_encrypted( iobuf_t out, int ctb, PKT_encrypted *ed )
{ {
int rc = 0; int rc = 0;
u32 n; u32 n;
@ -589,7 +589,7 @@ do_encrypted( IOBUF out, int ctb, PKT_encrypted *ed )
} }
static int static int
do_encrypted_mdc( IOBUF out, int ctb, PKT_encrypted *ed ) do_encrypted_mdc( iobuf_t out, int ctb, PKT_encrypted *ed )
{ {
int rc = 0; int rc = 0;
u32 n; u32 n;
@ -608,7 +608,7 @@ do_encrypted_mdc( IOBUF out, int ctb, PKT_encrypted *ed )
static int static int
do_compressed( IOBUF out, int ctb, PKT_compressed *cd ) do_compressed( iobuf_t out, int ctb, PKT_compressed *cd )
{ {
int rc = 0; int rc = 0;
@ -816,12 +816,12 @@ build_sig_subpkt (PKT_signature *sig, sigsubpkttype_t type,
/*log_debug ("updating area for type %d\n", type );*/ /*log_debug ("updating area for type %d\n", type );*/
} }
else if (oldarea) { else if (oldarea) {
newarea = m_realloc (oldarea, sizeof (*newarea) + n - 1); newarea = xrealloc (oldarea, sizeof (*newarea) + n - 1);
newarea->size = n; newarea->size = n;
/*log_debug ("reallocating area for type %d\n", type );*/ /*log_debug ("reallocating area for type %d\n", type );*/
} }
else { else {
newarea = m_alloc (sizeof (*newarea) + n - 1); newarea = xmalloc (sizeof (*newarea) + n - 1);
newarea->size = n; newarea->size = n;
/*log_debug ("allocating area for type %d\n", type );*/ /*log_debug ("allocating area for type %d\n", type );*/
} }
@ -922,7 +922,7 @@ build_attribute_subpkt(PKT_user_id *uid,byte type,
/* realloc uid->attrib_data to the right size */ /* realloc uid->attrib_data to the right size */
uid->attrib_data=m_realloc(uid->attrib_data, uid->attrib_data=xrealloc(uid->attrib_data,
uid->attrib_len+idx+1+headerlen+buflen); uid->attrib_len+idx+1+headerlen+buflen);
attrib=&uid->attrib_data[uid->attrib_len]; attrib=&uid->attrib_data[uid->attrib_len];
@ -954,11 +954,11 @@ build_attribute_subpkt(PKT_user_id *uid,byte type,
} }
static int static int
do_signature( IOBUF out, int ctb, PKT_signature *sig ) do_signature( iobuf_t out, int ctb, PKT_signature *sig )
{ {
int rc = 0; int rc = 0;
int n, i; int n, i;
IOBUF a = iobuf_temp(); iobuf_t a = iobuf_temp();
if( !sig->version ) if( !sig->version )
iobuf_put( a, 3 ); iobuf_put( a, 3 );
@ -1000,8 +1000,7 @@ do_signature( IOBUF out, int ctb, PKT_signature *sig )
write_sign_packet_header(out, ctb, iobuf_get_temp_length(a) ); write_sign_packet_header(out, ctb, iobuf_get_temp_length(a) );
else else
write_header(out, ctb, iobuf_get_temp_length(a) ); write_header(out, ctb, iobuf_get_temp_length(a) );
if( iobuf_write_temp( out, a ) ) rc = iobuf_write_temp (out, a);
rc = G10ERR_WRITE_FILE;
iobuf_close(a); iobuf_close(a);
return rc; return rc;
@ -1009,10 +1008,10 @@ do_signature( IOBUF out, int ctb, PKT_signature *sig )
static int static int
do_onepass_sig( IOBUF out, int ctb, PKT_onepass_sig *ops ) do_onepass_sig( iobuf_t out, int ctb, PKT_onepass_sig *ops )
{ {
int rc = 0; int rc = 0;
IOBUF a = iobuf_temp(); iobuf_t a = iobuf_temp();
write_version( a, ctb ); write_version( a, ctb );
iobuf_put(a, ops->sig_class ); iobuf_put(a, ops->sig_class );
@ -1023,8 +1022,7 @@ do_onepass_sig( IOBUF out, int ctb, PKT_onepass_sig *ops )
iobuf_put(a, ops->last ); iobuf_put(a, ops->last );
write_header(out, ctb, iobuf_get_temp_length(a) ); write_header(out, ctb, iobuf_get_temp_length(a) );
if( iobuf_write_temp( out, a ) ) rc = iobuf_write_temp (out, a);
rc = G10ERR_WRITE_FILE;
iobuf_close(a); iobuf_close(a);
return rc; return rc;
@ -1032,23 +1030,19 @@ do_onepass_sig( IOBUF out, int ctb, PKT_onepass_sig *ops )
static int static int
write_16(IOBUF out, u16 a) write_16(iobuf_t out, u16 a)
{ {
iobuf_put(out, a>>8); iobuf_put(out, a>>8);
if( iobuf_put(out,a) ) return iobuf_put(out,a);
return -1;
return 0;
} }
static int static int
write_32(IOBUF out, u32 a) write_32(iobuf_t out, u32 a)
{ {
iobuf_put(out, a>> 24); iobuf_put(out, a>> 24);
iobuf_put(out, a>> 16); iobuf_put(out, a>> 16);
iobuf_put(out, a>> 8); iobuf_put(out, a>> 8);
if( iobuf_put(out, a) ) return iobuf_put (out, a);
return -1;
return 0;
} }
@ -1081,14 +1075,14 @@ calc_header_length( u32 len, int new_ctb )
* Write the CTB and the packet length * Write the CTB and the packet length
*/ */
static int static int
write_header( IOBUF out, int ctb, u32 len ) write_header( iobuf_t out, int ctb, u32 len )
{ {
return write_header2( out, ctb, len, 0, 1 ); return write_header2( out, ctb, len, 0, 1 );
} }
static int static int
write_sign_packet_header( IOBUF out, int ctb, u32 len ) write_sign_packet_header( iobuf_t out, int ctb, u32 len )
{ {
/* work around a bug in the pgp read function for signature packets, /* work around a bug in the pgp read function for signature packets,
* which are not correctly coded and silently assume at some * which are not correctly coded and silently assume at some
@ -1103,7 +1097,7 @@ write_sign_packet_header( IOBUF out, int ctb, u32 len )
* we need this, so that we can hash packets without reading them again. * we need this, so that we can hash packets without reading them again.
*/ */
static int static int
write_header2( IOBUF out, int ctb, u32 len, int hdrlen, int blkmode ) write_header2( iobuf_t out, int ctb, u32 len, int hdrlen, int blkmode )
{ {
if( ctb & 0x40 ) if( ctb & 0x40 )
return write_new_header( out, ctb, len, hdrlen ); return write_new_header( out, ctb, len, hdrlen );
@ -1149,7 +1143,7 @@ write_header2( IOBUF out, int ctb, u32 len, int hdrlen, int blkmode )
static int static int
write_new_header( IOBUF out, int ctb, u32 len, int hdrlen ) write_new_header( iobuf_t out, int ctb, u32 len, int hdrlen )
{ {
if( hdrlen ) if( hdrlen )
log_bug("can't cope with hdrlen yet\n"); log_bug("can't cope with hdrlen yet\n");
@ -1188,7 +1182,7 @@ write_new_header( IOBUF out, int ctb, u32 len, int hdrlen )
} }
static int static int
write_version( IOBUF out, int ctb ) write_version( iobuf_t out, int ctb )
{ {
if( iobuf_put( out, 3 ) ) if( iobuf_put( out, 3 ) )
return -1; return -1;

View File

@ -30,14 +30,21 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <assert.h> #include <assert.h>
#include <gcrypt.h>
#ifdef HAVE_LOCALE_H #ifdef HAVE_LOCALE_H
#include <locale.h> #include <locale.h>
#endif #endif
#include <assuan.h> #include <assuan.h>
#include "gpg.h" #include "gpg.h"
#include "util.h"
#include "membuf.h"
#include "options.h"
#include "i18n.h" #include "i18n.h"
#include "call-agent.h"
#ifndef DBG_ASSUAN
# define DBG_ASSUAN 1
#endif
static ASSUAN_CONTEXT agent_ctx = NULL; static ASSUAN_CONTEXT agent_ctx = NULL;
static int force_pipe_server = 0; static int force_pipe_server = 0;
@ -175,7 +182,7 @@ start_agent (void)
char *optstr; char *optstr;
if (asprintf (&optstr, "OPTION display=%s", if (asprintf (&optstr, "OPTION display=%s",
opt.display ? opt.display : dft_display) < 0) opt.display ? opt.display : dft_display) < 0)
return OUT_OF_CORE (errno); return gpg_error_from_errno (errno);
rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL, rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
NULL); NULL);
free (optstr); free (optstr);
@ -193,7 +200,7 @@ start_agent (void)
char *optstr; char *optstr;
if (asprintf (&optstr, "OPTION ttyname=%s", if (asprintf (&optstr, "OPTION ttyname=%s",
opt.ttyname ? opt.ttyname : dft_ttyname) < 0) opt.ttyname ? opt.ttyname : dft_ttyname) < 0)
return OUT_OF_CORE (errno); return gpg_error_from_errno (errno);
rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL, rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
NULL); NULL);
free (optstr); free (optstr);
@ -206,7 +213,7 @@ start_agent (void)
char *optstr; char *optstr;
if (asprintf (&optstr, "OPTION ttytype=%s", if (asprintf (&optstr, "OPTION ttytype=%s",
opt.ttyname ? opt.ttytype : dft_ttytype) < 0) opt.ttyname ? opt.ttytype : dft_ttytype) < 0)
return OUT_OF_CORE (errno); return gpg_error_from_errno (errno);
rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL, rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
NULL); NULL);
free (optstr); free (optstr);
@ -219,7 +226,8 @@ start_agent (void)
{ {
old_lc = strdup (old_lc); old_lc = strdup (old_lc);
if (!old_lc) if (!old_lc)
return OUT_OF_CORE (errno); return gpg_error_from_errno (errno);
} }
dft_lc = setlocale (LC_CTYPE, ""); dft_lc = setlocale (LC_CTYPE, "");
#endif #endif
@ -228,7 +236,7 @@ start_agent (void)
char *optstr; char *optstr;
if (asprintf (&optstr, "OPTION lc-ctype=%s", if (asprintf (&optstr, "OPTION lc-ctype=%s",
opt.lc_ctype ? opt.lc_ctype : dft_lc) < 0) opt.lc_ctype ? opt.lc_ctype : dft_lc) < 0)
rc = OUT_OF_CORE (errno); rc = gpg_error_from_errno (errno);
else else
{ {
rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL, rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL,
@ -253,7 +261,7 @@ start_agent (void)
{ {
old_lc = strdup (old_lc); old_lc = strdup (old_lc);
if (!old_lc) if (!old_lc)
return OUT_OF_CORE (errno); return gpg_error_from_errno (errno);
} }
dft_lc = setlocale (LC_MESSAGES, ""); dft_lc = setlocale (LC_MESSAGES, "");
#endif #endif
@ -262,7 +270,7 @@ start_agent (void)
char *optstr; char *optstr;
if (asprintf (&optstr, "OPTION lc-messages=%s", if (asprintf (&optstr, "OPTION lc-messages=%s",
opt.lc_messages ? opt.lc_messages : dft_lc) < 0) opt.lc_messages ? opt.lc_messages : dft_lc) < 0)
rc = OUT_OF_CORE (errno); rc = gpg_error_from_errno (errno);
else else
{ {
rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL, rc = assuan_transact (agent_ctx, optstr, NULL, NULL, NULL, NULL, NULL,

View File

@ -1,5 +1,5 @@
/* cipher.c - En-/De-ciphering filter /* cipher.c - En-/De-ciphering filter
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "errors.h" #include "errors.h"
#include "iobuf.h" #include "iobuf.h"
#include "memory.h" #include "memory.h"
@ -40,15 +41,16 @@
static void static void
write_header( cipher_filter_context_t *cfx, IOBUF a ) write_header( cipher_filter_context_t *cfx, iobuf_t a )
{ {
PACKET pkt; PACKET pkt;
PKT_encrypted ed; PKT_encrypted ed;
byte temp[18]; byte temp[18];
unsigned blocksize; unsigned int blocksize;
unsigned nprefix; unsigned int nprefix;
gpg_error_t rc;
blocksize = cipher_get_blocksize( cfx->dek->algo ); blocksize = gcry_cipher_get_algo_blklen ( cfx->dek->algo );
if( blocksize < 8 || blocksize > 16 ) if( blocksize < 8 || blocksize > 16 )
log_fatal("unsupported blocksize %u\n", blocksize ); log_fatal("unsupported blocksize %u\n", blocksize );
@ -58,9 +60,9 @@ write_header( cipher_filter_context_t *cfx, IOBUF a )
ed.new_ctb = !ed.len && !RFC1991; ed.new_ctb = !ed.len && !RFC1991;
if( cfx->dek->use_mdc ) { if( cfx->dek->use_mdc ) {
ed.mdc_method = DIGEST_ALGO_SHA1; ed.mdc_method = DIGEST_ALGO_SHA1;
cfx->mdc_hash = md_open( DIGEST_ALGO_SHA1, 0 ); gcry_md_open (&cfx->mdc_hash, GCRY_MD_SHA1, 0 );
if ( DBG_HASHING ) if ( DBG_HASHING )
md_start_debug( cfx->mdc_hash, "creatmdc" ); gcry_md_start_debug ( cfx->mdc_hash, "creatmdc" );
} }
{ {
@ -76,21 +78,28 @@ write_header( cipher_filter_context_t *cfx, IOBUF a )
if( build_packet( a, &pkt )) if( build_packet( a, &pkt ))
log_bug("build_packet(ENCR_DATA) failed\n"); log_bug("build_packet(ENCR_DATA) failed\n");
nprefix = blocksize; nprefix = blocksize;
randomize_buffer( temp, nprefix, 1 ); gcry_randomize ( temp, nprefix, GCRY_STRONG_RANDOM);
temp[nprefix] = temp[nprefix-2]; temp[nprefix] = temp[nprefix-2];
temp[nprefix+1] = temp[nprefix-1]; temp[nprefix+1] = temp[nprefix-1];
print_cipher_algo_note( cfx->dek->algo ); print_cipher_algo_note( cfx->dek->algo );
cfx->cipher_hd = cipher_open( cfx->dek->algo, rc = gcry_cipher_open (&cfx->cipher_hd, cfx->dek->algo,
cfx->dek->use_mdc? CIPHER_MODE_CFB GCRY_CIPHER_MODE_CFB,
: CIPHER_MODE_AUTO_CFB, 1 ); GCRY_CIPHER_SECURE
| ((cfx->dek->use_mdc || cfx->dek->algo >= 100) ?
0 : GCRY_CIPHER_ENABLE_SYNC));
if (rc) {
/* we should never get an error here cause we already checked, that
* the algorithm is available. */
BUG();
}
/* log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/ /* log_hexdump( "thekey", cfx->dek->key, cfx->dek->keylen );*/
cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen ); gcry_cipher_setkey( cfx->cipher_hd, cfx->dek->key, cfx->dek->keylen );
cipher_setiv( cfx->cipher_hd, NULL, 0 ); gcry_cipher_setiv( cfx->cipher_hd, NULL, 0 );
/* log_hexdump( "prefix", temp, nprefix+2 ); */ /* log_hexdump( "prefix", temp, nprefix+2 ); */
if( cfx->mdc_hash ) /* hash the "IV" */ if( cfx->mdc_hash ) /* hash the "IV" */
md_write( cfx->mdc_hash, temp, nprefix+2 ); gcry_md_write( cfx->mdc_hash, temp, nprefix+2 );
cipher_encrypt( cfx->cipher_hd, temp, temp, nprefix+2); gcry_cipher_encrypt( cfx->cipher_hd, temp, nprefix+2, NULL, 0);
cipher_sync( cfx->cipher_hd ); gcry_cipher_sync( cfx->cipher_hd );
iobuf_write(a, temp, nprefix+2); iobuf_write(a, temp, nprefix+2);
cfx->header=1; cfx->header=1;
} }
@ -102,7 +111,7 @@ write_header( cipher_filter_context_t *cfx, IOBUF a )
*/ */
int int
cipher_filter( void *opaque, int control, cipher_filter( void *opaque, int control,
IOBUF a, byte *buf, size_t *ret_len) iobuf_t a, byte *buf, size_t *ret_len)
{ {
size_t size = *ret_len; size_t size = *ret_len;
cipher_filter_context_t *cfx = opaque; cipher_filter_context_t *cfx = opaque;
@ -117,36 +126,40 @@ cipher_filter( void *opaque, int control,
write_header( cfx, a ); write_header( cfx, a );
} }
if( cfx->mdc_hash ) if( cfx->mdc_hash )
md_write( cfx->mdc_hash, buf, size ); gcry_md_write( cfx->mdc_hash, buf, size );
cipher_encrypt( cfx->cipher_hd, buf, buf, size); gcry_cipher_encrypt( cfx->cipher_hd, buf, size, NULL, 0);
if( iobuf_write( a, buf, size ) ) rc = iobuf_write( a, buf, size );
rc = G10ERR_WRITE_FILE;
} }
else if( control == IOBUFCTRL_FREE ) { else if( control == IOBUFCTRL_FREE ) {
if( cfx->mdc_hash ) { if( cfx->mdc_hash ) {
byte *hash; byte *hash;
int hashlen = md_digest_length( md_get_algo( cfx->mdc_hash ) ); int hashlen = gcry_md_get_algo_dlen (gcry_md_get_algo (
cfx->mdc_hash));
byte temp[22]; byte temp[22];
assert( hashlen == 20 ); assert( hashlen == 20 );
/* we must hash the prefix of the MDC packet here */ /* we must hash the prefix of the MDC packet here */
temp[0] = 0xd3; temp[0] = 0xd3;
temp[1] = 0x14; temp[1] = 0x14;
md_putc( cfx->mdc_hash, temp[0] ); gcry_md_putc ( cfx->mdc_hash, temp[0] );
md_putc( cfx->mdc_hash, temp[1] ); gcry_md_putc ( cfx->mdc_hash, temp[1] );
md_final( cfx->mdc_hash ); gcry_md_final ( cfx->mdc_hash );
hash = md_read( cfx->mdc_hash, 0 ); hash = gcry_md_read ( cfx->mdc_hash, 0 );
memcpy(temp+2, hash, 20); memcpy(temp+2, hash, 20);
cipher_encrypt( cfx->cipher_hd, temp, temp, 22 ); gcry_cipher_encrypt( cfx->cipher_hd, temp, 22, NULL, 0 );
md_close( cfx->mdc_hash ); cfx->mdc_hash = NULL; gcry_md_close ( cfx->mdc_hash ); cfx->mdc_hash = NULL;
if( iobuf_write( a, temp, 22 ) ) rc = iobuf_write( a, temp, 22 );
if (rc)
log_error("writing MDC packet failed\n" ); log_error("writing MDC packet failed\n" );
} }
cipher_close(cfx->cipher_hd); gcry_cipher_close (cfx->cipher_hd);
} }
else if( control == IOBUFCTRL_DESC ) { else if( control == IOBUFCTRL_DESC ) {
*(char**)buf = "cipher_filter"; *(char**)buf = "cipher_filter";
} }
return rc; return rc;
} }

View File

@ -1,5 +1,5 @@
/* comment.c - write comment stuff /* comment.c - write comment stuff
* Copyright (C) 1998 Free Software Foundation, Inc. * Copyright (C) 1998, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -37,7 +37,7 @@
int int
write_comment( IOBUF out, const char *s ) write_comment( iobuf_t out, const char *s )
{ {
PACKET pkt; PACKET pkt;
size_t n = strlen(s); size_t n = strlen(s);
@ -45,18 +45,18 @@ write_comment( IOBUF out, const char *s )
pkt.pkttype = PKT_COMMENT; pkt.pkttype = PKT_COMMENT;
if( *s != '#' ) { if( *s != '#' ) {
pkt.pkt.comment = m_alloc( sizeof *pkt.pkt.comment + n ); pkt.pkt.comment = xmalloc ( sizeof *pkt.pkt.comment + n );
pkt.pkt.comment->len = n+1; pkt.pkt.comment->len = n+1;
*pkt.pkt.comment->data = '#'; *pkt.pkt.comment->data = '#';
strcpy(pkt.pkt.comment->data+1, s); strcpy(pkt.pkt.comment->data+1, s);
} }
else { else {
pkt.pkt.comment = m_alloc( sizeof *pkt.pkt.comment + n - 1 ); pkt.pkt.comment = xmalloc ( sizeof *pkt.pkt.comment + n - 1 );
pkt.pkt.comment->len = n; pkt.pkt.comment->len = n;
strcpy(pkt.pkt.comment->data, s); strcpy(pkt.pkt.comment->data, s);
} }
if( (rc = build_packet( out, &pkt )) ) if( (rc = build_packet( out, &pkt )) )
log_error("build_packet(comment) failed: %s\n", g10_errstr(rc) ); log_error("build_packet(comment) failed: %s\n", gpg_strerror (rc) );
free_packet( &pkt ); free_packet( &pkt );
return rc; return rc;
} }
@ -68,9 +68,9 @@ make_comment_node( const char *s )
PACKET *pkt; PACKET *pkt;
size_t n = strlen(s); size_t n = strlen(s);
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_COMMENT; pkt->pkttype = PKT_COMMENT;
pkt->pkt.comment = m_alloc( sizeof *pkt->pkt.comment + n - 1 ); pkt->pkt.comment = xmalloc ( sizeof *pkt->pkt.comment + n - 1 );
pkt->pkt.comment->len = n; pkt->pkt.comment->len = n;
strcpy(pkt->pkt.comment->data, s); strcpy(pkt->pkt.comment->data, s);
return new_kbnode( pkt ); return new_kbnode( pkt );
@ -78,25 +78,29 @@ make_comment_node( const char *s )
KBNODE KBNODE
make_mpi_comment_node( const char *s, MPI a ) make_mpi_comment_node( const char *s, gcry_mpi_t a )
{ {
PACKET *pkt; PACKET *pkt;
byte *buf, *p, *pp; byte *buf, *pp;
unsigned n1, nb1; size_t n1, nb1;
size_t n = strlen(s); size_t n = strlen(s);
nb1 = mpi_get_nbits( a ); nb1 = mpi_get_nbits( a );
p = buf = mpi_get_buffer( a, &n1, NULL ); if (gcry_mpi_print (GCRYMPI_FMT_PGP, NULL, &n1, a))
pkt = m_alloc_clear( sizeof *pkt ); BUG ();
/* fixme: allocate it on the stack */
buf = xmalloc (n1);
if (gcry_mpi_print (GCRYMPI_FMT_PGP, buf, &n1, a))
BUG ();
pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_COMMENT; pkt->pkttype = PKT_COMMENT;
pkt->pkt.comment = m_alloc( sizeof *pkt->pkt.comment + n + 2 + n1 ); pkt->pkt.comment = xmalloc ( sizeof *pkt->pkt.comment + n + 2 + n1 );
pkt->pkt.comment->len = n+1+2+n1; pkt->pkt.comment->len = n+1+2+n1;
pp = pkt->pkt.comment->data; pp = pkt->pkt.comment->data;
memcpy(pp, s, n+1); memcpy(pp, s, n+1);
pp[n+1] = nb1 >> 8; memcpy(pp+n+1, buf, n1 );
pp[n+2] = nb1 ; xfree (buf);
memcpy(pp+n+3, p, n1 );
m_free(buf);
return new_kbnode( pkt ); return new_kbnode( pkt );
} }

View File

@ -1,5 +1,6 @@
/* compress.c - compress filter /* compress.c - compress filter
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2002,
* 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -30,6 +31,7 @@
# include "zlib-riscos.h" # include "zlib-riscos.h"
#endif #endif
#include "gpg.h"
#include "util.h" #include "util.h"
#include "memory.h" #include "memory.h"
#include "packet.h" #include "packet.h"
@ -73,12 +75,13 @@ init_compress( compress_filter_context_t *zfx, z_stream *zs )
} }
zfx->outbufsize = 8192; zfx->outbufsize = 8192;
zfx->outbuf = m_alloc( zfx->outbufsize ); zfx->outbuf = xmalloc ( zfx->outbufsize );
} }
static int static int
do_compress( compress_filter_context_t *zfx, z_stream *zs, int flush, IOBUF a ) do_compress( compress_filter_context_t *zfx, z_stream *zs, int flush, iobuf_t a )
{ {
gpg_error_t rc;
int zrc; int zrc;
unsigned n; unsigned n;
@ -108,10 +111,12 @@ do_compress( compress_filter_context_t *zfx, z_stream *zs, int flush, IOBUF a )
(unsigned)zs->avail_in, (unsigned)zs->avail_out, (unsigned)zs->avail_in, (unsigned)zs->avail_out,
(unsigned)n, zrc ); (unsigned)n, zrc );
if( iobuf_write( a, zfx->outbuf, n ) ) { rc = iobuf_write (a, zfx->outbuf, n);
if (rc)
{
log_debug("deflate: iobuf_write failed\n"); log_debug("deflate: iobuf_write failed\n");
return G10ERR_WRITE_FILE; return rc;
} }
} while( zs->avail_in || (flush == Z_FINISH && zrc != Z_STREAM_END) ); } while( zs->avail_in || (flush == Z_FINISH && zrc != Z_STREAM_END) );
return 0; return 0;
} }
@ -140,13 +145,13 @@ init_uncompress( compress_filter_context_t *zfx, z_stream *zs )
} }
zfx->inbufsize = 2048; zfx->inbufsize = 2048;
zfx->inbuf = m_alloc( zfx->inbufsize ); zfx->inbuf = xmalloc ( zfx->inbufsize );
zs->avail_in = 0; zs->avail_in = 0;
} }
static int static int
do_uncompress( compress_filter_context_t *zfx, z_stream *zs, do_uncompress( compress_filter_context_t *zfx, z_stream *zs,
IOBUF a, size_t *ret_len ) iobuf_t a, size_t *ret_len )
{ {
int zrc; int zrc;
int rc=0; int rc=0;
@ -210,7 +215,7 @@ do_uncompress( compress_filter_context_t *zfx, z_stream *zs,
int int
compress_filter( void *opaque, int control, compress_filter( void *opaque, int control,
IOBUF a, byte *buf, size_t *ret_len) iobuf_t a, byte *buf, size_t *ret_len)
{ {
size_t size = *ret_len; size_t size = *ret_len;
compress_filter_context_t *zfx = opaque; compress_filter_context_t *zfx = opaque;
@ -219,7 +224,7 @@ compress_filter( void *opaque, int control,
if( control == IOBUFCTRL_UNDERFLOW ) { if( control == IOBUFCTRL_UNDERFLOW ) {
if( !zfx->status ) { if( !zfx->status ) {
zs = zfx->opaque = m_alloc_clear( sizeof *zs ); zs = zfx->opaque = xcalloc (1, sizeof *zs );
init_uncompress( zfx, zs ); init_uncompress( zfx, zs );
zfx->status = 1; zfx->status = 1;
} }
@ -250,7 +255,7 @@ compress_filter( void *opaque, int control,
pkt.pkt.compressed = &cd; pkt.pkt.compressed = &cd;
if( build_packet( a, &pkt )) if( build_packet( a, &pkt ))
log_bug("build_packet(PKT_COMPRESSED) failed\n"); log_bug("build_packet(PKT_COMPRESSED) failed\n");
zs = zfx->opaque = m_alloc_clear( sizeof *zs ); zs = zfx->opaque = xcalloc (1, sizeof *zs );
init_compress( zfx, zs ); init_compress( zfx, zs );
zfx->status = 2; zfx->status = 2;
} }
@ -266,9 +271,9 @@ compress_filter( void *opaque, int control,
else if( control == IOBUFCTRL_FREE ) { else if( control == IOBUFCTRL_FREE ) {
if( zfx->status == 1 ) { if( zfx->status == 1 ) {
inflateEnd(zs); inflateEnd(zs);
m_free(zs); xfree (zs);
zfx->opaque = NULL; zfx->opaque = NULL;
m_free(zfx->outbuf); zfx->outbuf = NULL; xfree (zfx->outbuf); zfx->outbuf = NULL;
} }
else if( zfx->status == 2 ) { else if( zfx->status == 2 ) {
#ifndef __riscos__ #ifndef __riscos__
@ -279,9 +284,9 @@ compress_filter( void *opaque, int control,
zs->avail_in = 0; zs->avail_in = 0;
do_compress( zfx, zs, Z_FINISH, a ); do_compress( zfx, zs, Z_FINISH, a );
deflateEnd(zs); deflateEnd(zs);
m_free(zs); xfree (zs);
zfx->opaque = NULL; zfx->opaque = NULL;
m_free(zfx->outbuf); zfx->outbuf = NULL; xfree (zfx->outbuf); zfx->outbuf = NULL;
} }
if (zfx->release) if (zfx->release)
zfx->release (zfx); zfx->release (zfx);
@ -295,7 +300,7 @@ compress_filter( void *opaque, int control,
static void static void
release_context (compress_filter_context_t *ctx) release_context (compress_filter_context_t *ctx)
{ {
m_free (ctx); xfree (ctx);
} }
/**************** /****************
@ -303,14 +308,14 @@ release_context (compress_filter_context_t *ctx)
*/ */
int int
handle_compressed( void *procctx, PKT_compressed *cd, handle_compressed( void *procctx, PKT_compressed *cd,
int (*callback)(IOBUF, void *), void *passthru ) int (*callback)(iobuf_t, void *), void *passthru )
{ {
compress_filter_context_t *cfx; compress_filter_context_t *cfx;
int rc; int rc;
if( cd->algorithm < 1 || cd->algorithm > 2 ) if( cd->algorithm < 1 || cd->algorithm > 2 )
return G10ERR_COMPR_ALGO; return GPG_ERR_COMPR_ALGO;
cfx = m_alloc_clear (sizeof *cfx); cfx = xcalloc (1,sizeof *cfx);
cfx->algo = cd->algorithm; cfx->algo = cd->algorithm;
cfx->release = release_context; cfx->release = release_context;
iobuf_push_filter( cd->buf, compress_filter, cfx ); iobuf_push_filter( cd->buf, compress_filter, cfx );

View File

@ -1,5 +1,5 @@
/* dearmor.c - Armor utility /* dearmor.c - Armor utility
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "errors.h" #include "errors.h"
#include "iobuf.h" #include "iobuf.h"
#include "memory.h" #include "memory.h"
@ -42,7 +43,7 @@ int
dearmor_file( const char *fname ) dearmor_file( const char *fname )
{ {
armor_filter_context_t afx; armor_filter_context_t afx;
IOBUF inp = NULL, out = NULL; iobuf_t inp = NULL, out = NULL;
int rc = 0; int rc = 0;
int c; int c;
@ -50,9 +51,9 @@ dearmor_file( const char *fname )
/* prepare iobufs */ /* prepare iobufs */
if( !(inp = iobuf_open(fname)) ) { if( !(inp = iobuf_open(fname)) ) {
rc = gpg_error_from_errno (errno);
log_error("can't open %s: %s\n", fname? fname: "[stdin]", log_error("can't open %s: %s\n", fname? fname: "[stdin]",
strerror(errno) ); strerror(errno) );
rc = G10ERR_OPEN_FILE;
goto leave; goto leave;
} }
@ -84,7 +85,7 @@ int
enarmor_file( const char *fname ) enarmor_file( const char *fname )
{ {
armor_filter_context_t afx; armor_filter_context_t afx;
IOBUF inp = NULL, out = NULL; iobuf_t inp = NULL, out = NULL;
int rc = 0; int rc = 0;
int c; int c;
@ -92,9 +93,9 @@ enarmor_file( const char *fname )
/* prepare iobufs */ /* prepare iobufs */
if( !(inp = iobuf_open(fname)) ) { if( !(inp = iobuf_open(fname)) ) {
rc = gpg_error_from_errno (errno);
log_error("can't open %s: %s\n", fname? fname: "[stdin]", log_error("can't open %s: %s\n", fname? fname: "[stdin]",
strerror(errno) ); strerror(errno) );
rc = G10ERR_OPEN_FILE;
goto leave; goto leave;
} }

View File

@ -49,7 +49,7 @@
int int
decrypt_message( const char *filename ) decrypt_message( const char *filename )
{ {
IOBUF fp; iobuf_t fp;
armor_filter_context_t afx; armor_filter_context_t afx;
progress_filter_context_t pfx; progress_filter_context_t pfx;
int rc; int rc;
@ -58,8 +58,9 @@ decrypt_message( const char *filename )
/* open the message file */ /* open the message file */
fp = iobuf_open(filename); fp = iobuf_open(filename);
if( !fp ) { if( !fp ) {
rc = gpg_error_from_errno (errno);
log_error(_("can't open `%s'\n"), print_fname_stdin(filename)); log_error(_("can't open `%s'\n"), print_fname_stdin(filename));
return G10ERR_OPEN_FILE; return rc;
} }
handle_progress (&pfx, fp, filename); handle_progress (&pfx, fp, filename);
@ -85,7 +86,7 @@ decrypt_message( const char *filename )
void void
decrypt_messages(int nfiles, char **files) decrypt_messages(int nfiles, char **files)
{ {
IOBUF fp; iobuf_t fp;
armor_filter_context_t afx; armor_filter_context_t afx;
progress_filter_context_t pfx; progress_filter_context_t pfx;
char *p, *output = NULL; char *p, *output = NULL;
@ -125,15 +126,15 @@ decrypt_messages(int nfiles, char **files)
iobuf_close(fp); iobuf_close(fp);
if (rc) if (rc)
log_error("%s: decryption failed: %s\n", print_fname_stdin(*files), log_error("%s: decryption failed: %s\n", print_fname_stdin(*files),
g10_errstr(rc)); gpg_strerror (rc));
p = get_last_passphrase(); p = get_last_passphrase();
set_next_passphrase(p); set_next_passphrase(p);
m_free (p); xfree (p);
next_file: next_file:
/* Note that we emit file_done even after an error. */ /* Note that we emit file_done even after an error. */
write_status( STATUS_FILE_DONE ); write_status( STATUS_FILE_DONE );
m_free(output); xfree (output);
files++; files++;
} }
set_next_passphrase(NULL); set_next_passphrase(NULL);

View File

@ -68,9 +68,9 @@ do_delete_key( const char *username, int secret, int *r_sec_avail )
exactmatch = (desc.mode == KEYDB_SEARCH_MODE_FPR exactmatch = (desc.mode == KEYDB_SEARCH_MODE_FPR
|| desc.mode == KEYDB_SEARCH_MODE_FPR16 || desc.mode == KEYDB_SEARCH_MODE_FPR16
|| desc.mode == KEYDB_SEARCH_MODE_FPR20); || desc.mode == KEYDB_SEARCH_MODE_FPR20);
rc = desc.mode? keydb_search (hd, &desc, 1):G10ERR_INV_USER_ID; rc = desc.mode? keydb_search (hd, &desc, 1):GPG_ERR_INV_USER_ID;
if (rc) { if (rc) {
log_error (_("key `%s' not found: %s\n"), username, g10_errstr (rc)); log_error (_("key `%s' not found: %s\n"), username, gpg_strerror (rc));
write_status_text( STATUS_DELETE_PROBLEM, "1" ); write_status_text( STATUS_DELETE_PROBLEM, "1" );
goto leave; goto leave;
} }
@ -78,7 +78,7 @@ do_delete_key( const char *username, int secret, int *r_sec_avail )
/* read the keyblock */ /* read the keyblock */
rc = keydb_get_keyblock (hd, &keyblock ); rc = keydb_get_keyblock (hd, &keyblock );
if (rc) { if (rc) {
log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) ); log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc) );
goto leave; goto leave;
} }
@ -86,7 +86,7 @@ do_delete_key( const char *username, int secret, int *r_sec_avail )
node = find_kbnode( keyblock, secret? PKT_SECRET_KEY:PKT_PUBLIC_KEY ); node = find_kbnode( keyblock, secret? PKT_SECRET_KEY:PKT_PUBLIC_KEY );
if( !node ) { if( !node ) {
log_error("Oops; key not found anymore!\n"); log_error("Oops; key not found anymore!\n");
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
goto leave; goto leave;
} }
@ -103,8 +103,8 @@ do_delete_key( const char *username, int secret, int *r_sec_avail )
rc = -1; rc = -1;
goto leave; goto leave;
} }
else if( rc != G10ERR_NO_SECKEY ) { else if( rc != GPG_ERR_NO_SECKEY ) {
log_error("%s: get secret key: %s\n", username, g10_errstr(rc) ); log_error("%s: get secret key: %s\n", username, gpg_strerror (rc) );
} }
else else
rc = 0; rc = 0;
@ -153,7 +153,7 @@ do_delete_key( const char *username, int secret, int *r_sec_avail )
if( okay ) { if( okay ) {
rc = keydb_delete_keyblock (hd); rc = keydb_delete_keyblock (hd);
if (rc) { if (rc) {
log_error (_("deleting keyblock failed: %s\n"), g10_errstr(rc) ); log_error (_("deleting keyblock failed: %s\n"), gpg_strerror (rc) );
goto leave; goto leave;
} }
@ -200,7 +200,7 @@ delete_keys( STRLIST names, int secret, int allow_both )
} }
if(rc) { if(rc) {
log_error("%s: delete key failed: %s\n", names->d, g10_errstr(rc) ); log_error("%s: delete key failed: %s\n", names->d, gpg_strerror (rc) );
return rc; return rc;
} }
} }

View File

@ -26,6 +26,7 @@
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "options.h" #include "options.h"
#include "packet.h" #include "packet.h"
#include "errors.h" #include "errors.h"
@ -38,9 +39,11 @@
#include "trustdb.h" #include "trustdb.h"
#include "i18n.h" #include "i18n.h"
#include "status.h" #include "status.h"
#include "pkglue.h"
static int encode_simple( const char *filename, int mode, int compat ); static int encode_simple( const char *filename, int mode, int compat );
static int write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out ); static int write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, iobuf_t out );
@ -77,13 +80,14 @@ encode_store( const char *filename )
static void static void
encode_sesskey( DEK *dek, DEK **ret_dek, byte *enckey ) encode_sesskey( DEK *dek, DEK **ret_dek, byte *enckey )
{ {
#warning This functions needs a review.
CIPHER_HANDLE hd; CIPHER_HANDLE hd;
DEK *c; DEK *c;
byte buf[33]; byte buf[33];
assert ( dek->keylen < 32 ); assert ( dek->keylen < 32 );
c = m_alloc_clear( sizeof *c ); c = xcalloc (1, sizeof *c );
c->keylen = dek->keylen; c->keylen = dek->keylen;
c->algo = dek->algo; c->algo = dek->algo;
make_session_key( c ); make_session_key( c );
@ -92,11 +96,12 @@ encode_sesskey( DEK *dek, DEK **ret_dek, byte *enckey )
buf[0] = c->algo; buf[0] = c->algo;
memcpy( buf + 1, c->key, c->keylen ); memcpy( buf + 1, c->key, c->keylen );
hd = cipher_open( dek->algo, CIPHER_MODE_CFB, 1 );
cipher_setkey( hd, dek->key, dek->keylen ); gcry_cipher_open (&hd, dek->algo, GCRY_CIPHER_MODE_CFB, 1 );
cipher_setiv( hd, NULL, 0 ); gcry_cipher_setkey( hd, dek->key, dek->keylen );
cipher_encrypt( hd, buf, buf, c->keylen + 1 ); gcry_cipher_setiv( hd, NULL, 0 );
cipher_close( hd ); gcry_cipher_encrypt( hd, buf, c->keylen + 1, NULL, 0 );
gcry_cipher_close( hd );
memcpy( enckey, buf, c->keylen + 1 ); memcpy( enckey, buf, c->keylen + 1 );
wipememory( buf, sizeof buf ); /* burn key */ wipememory( buf, sizeof buf ); /* burn key */
@ -143,7 +148,7 @@ use_mdc(PK_LIST pk_list,int algo)
/* Last try. Use MDC for the modern ciphers. */ /* Last try. Use MDC for the modern ciphers. */
if(cipher_get_blocksize(algo)!=8) if( gcry_cipher_get_algo_blklen (algo) != 8)
return 1; return 1;
return 0; /* No MDC */ return 0; /* No MDC */
@ -152,7 +157,7 @@ use_mdc(PK_LIST pk_list,int algo)
static int static int
encode_simple( const char *filename, int mode, int compat ) encode_simple( const char *filename, int mode, int compat )
{ {
IOBUF inp, out; iobuf_t inp, out;
PACKET pkt; PACKET pkt;
DEK *dek = NULL; DEK *dek = NULL;
PKT_plaintext *pt = NULL; PKT_plaintext *pt = NULL;
@ -176,9 +181,10 @@ encode_simple( const char *filename, int mode, int compat )
/* prepare iobufs */ /* prepare iobufs */
if( !(inp = iobuf_open(filename)) ) { if( !(inp = iobuf_open(filename)) ) {
rc = gpg_error_from_errno (errno);
log_error(_("%s: can't open: %s\n"), filename? filename: "[stdin]", log_error(_("%s: can't open: %s\n"), filename? filename: "[stdin]",
strerror(errno) ); strerror(errno) );
return G10ERR_OPEN_FILE; return rc;
} }
handle_progress (&pfx, inp, filename); handle_progress (&pfx, inp, filename);
@ -194,18 +200,18 @@ encode_simple( const char *filename, int mode, int compat )
cfx.dek = NULL; cfx.dek = NULL;
if( mode ) { if( mode ) {
s2k = m_alloc_clear( sizeof *s2k ); s2k = xcalloc (1, sizeof *s2k );
s2k->mode = RFC1991? 0:opt.s2k_mode; s2k->mode = RFC1991? 0:opt.s2k_mode;
s2k->hash_algo = opt.s2k_digest_algo; s2k->hash_algo = opt.s2k_digest_algo;
cfx.dek = passphrase_to_dek( NULL, 0, cfx.dek = passphrase_to_dek( NULL, 0,
default_cipher_algo(), s2k, 2, default_cipher_algo(), s2k, 2,
NULL, NULL); NULL, NULL);
if( !cfx.dek || !cfx.dek->keylen ) { if( !cfx.dek || !cfx.dek->keylen ) {
rc = G10ERR_PASSPHRASE; rc = gpg_error (GPG_ERR_INV_PASSPHRASE);
m_free(cfx.dek); xfree (cfx.dek);
m_free(s2k); xfree (s2k);
iobuf_close(inp); iobuf_close(inp);
log_error(_("error creating passphrase: %s\n"), g10_errstr(rc) ); log_error(_("error creating passphrase: %s\n"), gpg_strerror (rc) );
return rc; return rc;
} }
if (!compat && s2k->mode != 1 && s2k->mode != 3) { if (!compat && s2k->mode != 1 && s2k->mode != 3) {
@ -215,9 +221,9 @@ encode_simple( const char *filename, int mode, int compat )
} }
if ( !compat ) { if ( !compat ) {
seskeylen = cipher_get_keylen( default_cipher_algo() ) / 8; seskeylen = gcry_cipher_get_algo_keylen (default_cipher_algo());
encode_sesskey( cfx.dek, &dek, enckey ); encode_sesskey( cfx.dek, &dek, enckey );
m_free( cfx.dek ); cfx.dek = dek; xfree (cfx.dek); cfx.dek = dek;
} }
cfx.dek->use_mdc=use_mdc(NULL,cfx.dek->algo); cfx.dek->use_mdc=use_mdc(NULL,cfx.dek->algo);
@ -233,8 +239,8 @@ encode_simple( const char *filename, int mode, int compat )
if( rc || (rc = open_outfile( filename, opt.armor? 1:0, &out )) ) { if( rc || (rc = open_outfile( filename, opt.armor? 1:0, &out )) ) {
iobuf_cancel(inp); iobuf_cancel(inp);
m_free(cfx.dek); xfree (cfx.dek);
m_free(s2k); xfree (s2k);
return rc; return rc;
} }
@ -249,7 +255,7 @@ encode_simple( const char *filename, int mode, int compat )
} }
#endif #endif
if( s2k && !RFC1991 ) { if( s2k && !RFC1991 ) {
PKT_symkey_enc *enc = m_alloc_clear( sizeof *enc + seskeylen + 1 ); PKT_symkey_enc *enc = xcalloc (1, sizeof *enc + seskeylen + 1 );
enc->version = 4; enc->version = 4;
enc->cipher_algo = cfx.dek->algo; enc->cipher_algo = cfx.dek->algo;
enc->s2k = *s2k; enc->s2k = *s2k;
@ -260,23 +266,25 @@ encode_simple( const char *filename, int mode, int compat )
pkt.pkttype = PKT_SYMKEY_ENC; pkt.pkttype = PKT_SYMKEY_ENC;
pkt.pkt.symkey_enc = enc; pkt.pkt.symkey_enc = enc;
if( (rc = build_packet( out, &pkt )) ) if( (rc = build_packet( out, &pkt )) )
log_error("build symkey packet failed: %s\n", g10_errstr(rc) ); log_error("build symkey packet failed: %s\n", gpg_strerror (rc) );
m_free(enc); xfree (enc);
} }
if (!opt.no_literal) { if (!opt.no_literal) {
/* setup the inner packet */ /* setup the inner packet */
if( filename || opt.set_filename ) { if( filename || opt.set_filename ) {
char *s = make_basename( opt.set_filename ? opt.set_filename char *s = make_basename ( opt.set_filename ? opt.set_filename
: filename, : filename
iobuf_get_real_fname( inp ) ); /* for riscos?
pt = m_alloc( sizeof *pt + strlen(s) - 1 ); .iobuf_get_real_fname( inp ) */
);
pt = xmalloc ( sizeof *pt + strlen(s) - 1 );
pt->namelen = strlen(s); pt->namelen = strlen(s);
memcpy(pt->name, s, pt->namelen ); memcpy(pt->name, s, pt->namelen );
m_free(s); xfree (s);
} }
else { /* no filename */ else { /* no filename */
pt = m_alloc( sizeof *pt - 1 ); pt = xmalloc ( sizeof *pt - 1 );
pt->namelen = 0; pt->namelen = 0;
} }
} }
@ -342,7 +350,7 @@ encode_simple( const char *filename, int mode, int compat )
/* do the work */ /* do the work */
if (!opt.no_literal) { if (!opt.no_literal) {
if( (rc = build_packet( out, &pkt )) ) if( (rc = build_packet( out, &pkt )) )
log_error("build_packet failed: %s\n", g10_errstr(rc) ); log_error("build_packet failed: %s\n", gpg_strerror (rc) );
} }
else { else {
/* user requested not to create a literal packet, /* user requested not to create a literal packet,
@ -350,9 +358,8 @@ encode_simple( const char *filename, int mode, int compat )
byte copy_buffer[4096]; byte copy_buffer[4096];
int bytes_copied; int bytes_copied;
while ((bytes_copied = iobuf_read(inp, copy_buffer, 4096)) != -1) while ((bytes_copied = iobuf_read(inp, copy_buffer, 4096)) != -1)
if (iobuf_write(out, copy_buffer, bytes_copied) == -1) { if ( (rc=iobuf_write(out, copy_buffer, bytes_copied))) {
rc = G10ERR_WRITE_FILE; log_error("copying input to output failed: %s\n", gpg_strerror (rc) );
log_error("copying input to output failed: %s\n", g10_errstr(rc) );
break; break;
} }
wipememory(copy_buffer, 4096); /* burn buffer */ wipememory(copy_buffer, 4096); /* burn buffer */
@ -370,8 +377,8 @@ encode_simple( const char *filename, int mode, int compat )
if (pt) if (pt)
pt->buf = NULL; pt->buf = NULL;
free_packet(&pkt); free_packet(&pkt);
m_free(cfx.dek); xfree (cfx.dek);
m_free(s2k); xfree (s2k);
return rc; return rc;
} }
@ -382,7 +389,7 @@ encode_simple( const char *filename, int mode, int compat )
int int
encode_crypt( const char *filename, STRLIST remusr ) encode_crypt( const char *filename, STRLIST remusr )
{ {
IOBUF inp = NULL, out = NULL; iobuf_t inp = NULL, out = NULL;
PACKET pkt; PACKET pkt;
PKT_plaintext *pt = NULL; PKT_plaintext *pt = NULL;
int rc = 0, rc2 = 0; int rc = 0, rc2 = 0;
@ -419,9 +426,9 @@ encode_crypt( const char *filename, STRLIST remusr )
/* prepare iobufs */ /* prepare iobufs */
if( !(inp = iobuf_open(filename)) ) { if( !(inp = iobuf_open(filename)) ) {
rc = gpg_error_from_errno (errno);
log_error(_("can't open %s: %s\n"), filename? filename: "[stdin]", log_error(_("can't open %s: %s\n"), filename? filename: "[stdin]",
strerror(errno) ); strerror(errno) );
rc = G10ERR_OPEN_FILE;
goto leave; goto leave;
} }
else if( opt.verbose ) else if( opt.verbose )
@ -447,7 +454,7 @@ encode_crypt( const char *filename, STRLIST remusr )
} }
#endif #endif
/* create a session key */ /* create a session key */
cfx.dek = m_alloc_secure_clear (sizeof *cfx.dek); cfx.dek = xcalloc_secure (1, sizeof *cfx.dek);
if( !opt.def_cipher_algo ) { /* try to get it from the prefs */ if( !opt.def_cipher_algo ) { /* try to get it from the prefs */
cfx.dek->algo = select_algo_from_prefs(pk_list,PREFTYPE_SYM,-1,NULL); cfx.dek->algo = select_algo_from_prefs(pk_list,PREFTYPE_SYM,-1,NULL);
/* The only way select_algo_from_prefs can fail here is when /* The only way select_algo_from_prefs can fail here is when
@ -473,7 +480,7 @@ encode_crypt( const char *filename, STRLIST remusr )
opt.def_cipher_algo,NULL)!=opt.def_cipher_algo) opt.def_cipher_algo,NULL)!=opt.def_cipher_algo)
log_info(_("forcing symmetric cipher %s (%d) " log_info(_("forcing symmetric cipher %s (%d) "
"violates recipient preferences\n"), "violates recipient preferences\n"),
cipher_algo_to_string(opt.def_cipher_algo), gcry_cipher_algo_name (opt.def_cipher_algo),
opt.def_cipher_algo); opt.def_cipher_algo);
cfx.dek->algo = opt.def_cipher_algo; cfx.dek->algo = opt.def_cipher_algo;
@ -501,7 +508,7 @@ encode_crypt( const char *filename, STRLIST remusr )
make_session_key( cfx.dek ); make_session_key( cfx.dek );
if( DBG_CIPHER ) if( DBG_CIPHER )
log_hexdump("DEK is: ", cfx.dek->key, cfx.dek->keylen ); log_printhex ("DEK is: ", cfx.dek->key, cfx.dek->keylen );
rc = write_pubkey_enc_from_list( pk_list, cfx.dek, out ); rc = write_pubkey_enc_from_list( pk_list, cfx.dek, out );
if( rc ) if( rc )
@ -511,15 +518,15 @@ encode_crypt( const char *filename, STRLIST remusr )
/* setup the inner packet */ /* setup the inner packet */
if( filename || opt.set_filename ) { if( filename || opt.set_filename ) {
char *s = make_basename( opt.set_filename ? opt.set_filename char *s = make_basename( opt.set_filename ? opt.set_filename
: filename, : filename
iobuf_get_real_fname( inp ) ); /* ,iobuf_get_real_fname( inp )*/ );
pt = m_alloc( sizeof *pt + strlen(s) - 1 ); pt = xmalloc ( sizeof *pt + strlen(s) - 1 );
pt->namelen = strlen(s); pt->namelen = strlen(s);
memcpy(pt->name, s, pt->namelen ); memcpy(pt->name, s, pt->namelen );
m_free(s); xfree (s);
} }
else { /* no filename */ else { /* no filename */
pt = m_alloc( sizeof *pt - 1 ); pt = xmalloc ( sizeof *pt - 1 );
pt->namelen = 0; pt->namelen = 0;
} }
} }
@ -590,7 +597,7 @@ encode_crypt( const char *filename, STRLIST remusr )
/* do the work */ /* do the work */
if (!opt.no_literal) { if (!opt.no_literal) {
if( (rc = build_packet( out, &pkt )) ) if( (rc = build_packet( out, &pkt )) )
log_error("build_packet failed: %s\n", g10_errstr(rc) ); log_error("build_packet failed: %s\n", gpg_strerror (rc) );
} }
else { else {
/* user requested not to create a literal packet, so we copy /* user requested not to create a literal packet, so we copy
@ -598,10 +605,9 @@ encode_crypt( const char *filename, STRLIST remusr )
byte copy_buffer[4096]; byte copy_buffer[4096];
int bytes_copied; int bytes_copied;
while ((bytes_copied = iobuf_read(inp, copy_buffer, 4096)) != -1) while ((bytes_copied = iobuf_read(inp, copy_buffer, 4096)) != -1)
if (iobuf_write(out, copy_buffer, bytes_copied) == -1) { if ((rc=iobuf_write(out, copy_buffer, bytes_copied))) {
rc = G10ERR_WRITE_FILE;
log_error("copying input to output failed: %s\n", log_error("copying input to output failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
break; break;
} }
wipememory(copy_buffer, 4096); /* burn buffer */ wipememory(copy_buffer, 4096); /* burn buffer */
@ -619,7 +625,7 @@ encode_crypt( const char *filename, STRLIST remusr )
if( pt ) if( pt )
pt->buf = NULL; pt->buf = NULL;
free_packet(&pkt); free_packet(&pkt);
m_free(cfx.dek); xfree (cfx.dek);
release_pk_list( pk_list ); release_pk_list( pk_list );
return rc; return rc;
} }
@ -632,7 +638,7 @@ encode_crypt( const char *filename, STRLIST remusr )
*/ */
int int
encrypt_filter( void *opaque, int control, encrypt_filter( void *opaque, int control,
IOBUF a, byte *buf, size_t *ret_len) iobuf_t a, byte *buf, size_t *ret_len)
{ {
size_t size = *ret_len; size_t size = *ret_len;
encrypt_filter_context_t *efx = opaque; encrypt_filter_context_t *efx = opaque;
@ -643,7 +649,7 @@ encrypt_filter( void *opaque, int control,
} }
else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */ else if( control == IOBUFCTRL_FLUSH ) { /* encrypt */
if( !efx->header_okay ) { if( !efx->header_okay ) {
efx->cfx.dek = m_alloc_secure_clear( sizeof *efx->cfx.dek ); efx->cfx.dek = xcalloc_secure (1, sizeof *efx->cfx.dek );
if( !opt.def_cipher_algo ) { /* try to get it from the prefs */ if( !opt.def_cipher_algo ) { /* try to get it from the prefs */
efx->cfx.dek->algo = efx->cfx.dek->algo =
@ -661,7 +667,7 @@ encrypt_filter( void *opaque, int control,
NULL)!=opt.def_cipher_algo) NULL)!=opt.def_cipher_algo)
log_info(_("forcing symmetric cipher %s (%d) " log_info(_("forcing symmetric cipher %s (%d) "
"violates recipient preferences\n"), "violates recipient preferences\n"),
cipher_algo_to_string(opt.def_cipher_algo), gcry_cipher_algo_name (opt.def_cipher_algo),
opt.def_cipher_algo); opt.def_cipher_algo);
efx->cfx.dek->algo = opt.def_cipher_algo; efx->cfx.dek->algo = opt.def_cipher_algo;
@ -671,8 +677,8 @@ encrypt_filter( void *opaque, int control,
make_session_key( efx->cfx.dek ); make_session_key( efx->cfx.dek );
if( DBG_CIPHER ) if( DBG_CIPHER )
log_hexdump("DEK is: ", log_printhex ("DEK is: ",
efx->cfx.dek->key, efx->cfx.dek->keylen ); efx->cfx.dek->key, efx->cfx.dek->keylen );
rc = write_pubkey_enc_from_list( efx->pk_list, efx->cfx.dek, a ); rc = write_pubkey_enc_from_list( efx->pk_list, efx->cfx.dek, a );
if( rc ) if( rc )
@ -698,7 +704,7 @@ encrypt_filter( void *opaque, int control,
* Write pubkey-enc packets from the list of PKs to OUT. * Write pubkey-enc packets from the list of PKs to OUT.
*/ */
static int static int
write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out ) write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, iobuf_t out )
{ {
PACKET pkt; PACKET pkt;
PKT_public_key *pk; PKT_public_key *pk;
@ -706,12 +712,12 @@ write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out )
int rc; int rc;
for( ; pk_list; pk_list = pk_list->next ) { for( ; pk_list; pk_list = pk_list->next ) {
MPI frame; gcry_mpi_t frame;
pk = pk_list->pk; pk = pk_list->pk;
print_pubkey_algo_note( pk->pubkey_algo ); print_pubkey_algo_note( pk->pubkey_algo );
enc = m_alloc_clear( sizeof *enc ); enc = xcalloc (1, sizeof *enc );
enc->pubkey_algo = pk->pubkey_algo; enc->pubkey_algo = pk->pubkey_algo;
keyid_from_pk( pk, enc->keyid ); keyid_from_pk( pk, enc->keyid );
enc->throw_keyid = (opt.throw_keyid || (pk_list->flags&1)); enc->throw_keyid = (opt.throw_keyid || (pk_list->flags&1));
@ -738,17 +744,17 @@ write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out )
*/ */
frame = encode_session_key( dek, pubkey_nbits( pk->pubkey_algo, frame = encode_session_key( dek, pubkey_nbits( pk->pubkey_algo,
pk->pkey ) ); pk->pkey ) );
rc = pubkey_encrypt( pk->pubkey_algo, enc->data, frame, pk->pkey ); rc = pk_encrypt( pk->pubkey_algo, enc->data, frame, pk->pkey );
mpi_free( frame ); gcry_mpi_release ( frame );
if( rc ) if( rc )
log_error("pubkey_encrypt failed: %s\n", g10_errstr(rc) ); log_error("pubkey_encrypt failed: %s\n", gpg_strerror (rc) );
else { else {
if( opt.verbose ) { if( opt.verbose ) {
char *ustr = get_user_id_string_printable (enc->keyid); char *ustr = get_user_id_string_printable (enc->keyid);
log_info(_("%s/%s encrypted for: \"%s\"\n"), log_info(_("%s/%s encrypted for: \"%s\"\n"),
pubkey_algo_to_string(enc->pubkey_algo), gcry_pk_algo_name (enc->pubkey_algo),
cipher_algo_to_string(dek->algo), ustr ); gcry_cipher_algo_name (dek->algo), ustr );
m_free(ustr); xfree (ustr);
} }
/* and write it */ /* and write it */
init_packet(&pkt); init_packet(&pkt);
@ -756,7 +762,7 @@ write_pubkey_enc_from_list( PK_LIST pk_list, DEK *dek, IOBUF out )
pkt.pkt.pubkey_enc = enc; pkt.pkt.pubkey_enc = enc;
rc = build_packet( out, &pkt ); rc = build_packet( out, &pkt );
if( rc ) if( rc )
log_error("build_packet(pubkey_enc) failed: %s\n", g10_errstr(rc)); log_error("build_packet(pubkey_enc) failed: %s\n", gpg_strerror (rc));
} }
free_pubkey_enc(enc); free_pubkey_enc(enc);
if( rc ) if( rc )
@ -792,7 +798,7 @@ encode_crypt_files(int nfiles, char **files, STRLIST remusr)
print_file_status(STATUS_FILE_START, line, 2); print_file_status(STATUS_FILE_START, line, 2);
if ( (rc = encode_crypt(line, remusr)) ) if ( (rc = encode_crypt(line, remusr)) )
log_error("%s: encryption failed: %s\n", log_error("%s: encryption failed: %s\n",
print_fname_stdin(line), g10_errstr(rc) ); print_fname_stdin(line), gpg_strerror (rc) );
write_status( STATUS_FILE_DONE ); write_status( STATUS_FILE_DONE );
} }
} }
@ -803,7 +809,7 @@ encode_crypt_files(int nfiles, char **files, STRLIST remusr)
print_file_status(STATUS_FILE_START, *files, 2); print_file_status(STATUS_FILE_START, *files, 2);
if ( (rc = encode_crypt(*files, remusr)) ) if ( (rc = encode_crypt(*files, remusr)) )
log_error("%s: encryption failed: %s\n", log_error("%s: encryption failed: %s\n",
print_fname_stdin(*files), g10_errstr(rc) ); print_fname_stdin(*files), gpg_strerror (rc) );
write_status( STATUS_FILE_DONE ); write_status( STATUS_FILE_DONE );
files++; files++;
} }

View File

@ -1,5 +1,5 @@
/* encr-data.c - process an encrypted data packet /* encr-data.c - process an encrypted data packet
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -23,6 +23,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "memory.h" #include "memory.h"
#include "packet.h" #include "packet.h"
@ -32,9 +34,9 @@
#include "i18n.h" #include "i18n.h"
static int mdc_decode_filter( void *opaque, int control, IOBUF a, static int mdc_decode_filter( void *opaque, int control, iobuf_t a,
byte *buf, size_t *ret_len); byte *buf, size_t *ret_len);
static int decode_filter( void *opaque, int control, IOBUF a, static int decode_filter( void *opaque, int control, iobuf_t a,
byte *buf, size_t *ret_len); byte *buf, size_t *ret_len);
typedef struct { typedef struct {
@ -61,16 +63,16 @@ decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
memset( &dfx, 0, sizeof dfx ); memset( &dfx, 0, sizeof dfx );
if( opt.verbose && !dek->algo_info_printed ) { if( opt.verbose && !dek->algo_info_printed ) {
const char *s = cipher_algo_to_string( dek->algo ); const char *s = gcry_cipher_algo_name (dek->algo);
if( s ) if (s && *s)
log_info(_("%s encrypted data\n"), s ); log_info(_("%s encrypted data\n"), s );
else else
log_info(_("encrypted with unknown algorithm %d\n"), dek->algo ); log_info(_("encrypted with unknown algorithm %d\n"), dek->algo );
dek->algo_info_printed = 1; dek->algo_info_printed = 1;
} }
if( (rc=check_cipher_algo(dek->algo)) ) if( (rc=openpgp_cipher_test_algo(dek->algo)) )
goto leave; goto leave;
blocksize = cipher_get_blocksize(dek->algo); blocksize = gcry_cipher_get_algo_blklen (dek->algo);
if( !blocksize || blocksize > 16 ) if( !blocksize || blocksize > 16 )
log_fatal("unsupported blocksize %u\n", blocksize ); log_fatal("unsupported blocksize %u\n", blocksize );
nprefix = blocksize; nprefix = blocksize;
@ -78,20 +80,29 @@ decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
BUG(); BUG();
if( ed->mdc_method ) { if( ed->mdc_method ) {
dfx.mdc_hash = md_open( ed->mdc_method, 0 ); gcry_md_open (&dfx.mdc_hash, ed->mdc_method, 0 );
if ( DBG_HASHING ) if ( DBG_HASHING )
md_start_debug(dfx.mdc_hash, "checkmdc"); gcry_md_start_debug (dfx.mdc_hash, "checkmdc");
} }
dfx.cipher_hd = cipher_open( dek->algo, rc = gcry_cipher_open (&dfx.cipher_hd, dek->algo,
ed->mdc_method? CIPHER_MODE_CFB GCRY_CIPHER_MODE_CFB,
: CIPHER_MODE_AUTO_CFB, 1 ); GCRY_CIPHER_SECURE
/* log_hexdump( "thekey", dek->key, dek->keylen );*/ | ((ed->mdc_method || dek->algo >= 100)?
rc = cipher_setkey( dfx.cipher_hd, dek->key, dek->keylen ); 0 : GCRY_CIPHER_ENABLE_SYNC) );
if( rc == G10ERR_WEAK_KEY ) if (rc)
{
/* we should never get an error here cause we already
* checked, that the algorithm is available. What about a
* flag to let the function die in this case? */
BUG();
}
/* log_hexdump( "thekey", dek->key, dek->keylen );*/
rc = gcry_cipher_setkey (dfx.cipher_hd, dek->key, dek->keylen);
if( gpg_err_code (rc) == GPG_ERR_WEAK_KEY )
log_info(_("WARNING: message was encrypted with " log_info(_("WARNING: message was encrypted with "
"a weak key in the symmetric cipher.\n")); "a weak key in the symmetric cipher.\n"));
else if( rc ) { else if( rc ) {
log_error("key setup failed: %s\n", g10_errstr(rc) ); log_error("key setup failed: %s\n", gpg_strerror (rc) );
goto leave; goto leave;
} }
if (!ed->buf) { if (!ed->buf) {
@ -99,9 +110,9 @@ decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
goto leave; goto leave;
} }
cipher_setiv( dfx.cipher_hd, NULL, 0 ); gcry_cipher_setiv (dfx.cipher_hd, NULL, 0);
if( ed->len ) { if (ed->len) {
for(i=0; i < (nprefix+2) && ed->len; i++, ed->len-- ) { for(i=0; i < (nprefix+2) && ed->len; i++, ed->len-- ) {
if( (c=iobuf_get(ed->buf)) == -1 ) if( (c=iobuf_get(ed->buf)) == -1 )
break; break;
@ -116,17 +127,17 @@ decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
else else
temp[i] = c; temp[i] = c;
} }
cipher_decrypt( dfx.cipher_hd, temp, temp, nprefix+2); gcry_cipher_decrypt( dfx.cipher_hd, temp, nprefix+2, NULL, 0);
cipher_sync( dfx.cipher_hd ); gcry_cipher_sync( dfx.cipher_hd );
p = temp; p = temp;
/* log_hexdump( "prefix", temp, nprefix+2 ); */ /* log_hexdump( "prefix", temp, nprefix+2 ); */
if( p[nprefix-2] != p[nprefix] || p[nprefix-1] != p[nprefix+1] ) { if( p[nprefix-2] != p[nprefix] || p[nprefix-1] != p[nprefix+1] ) {
rc = G10ERR_BAD_KEY; rc = GPG_ERR_BAD_KEY;
goto leave; goto leave;
} }
if( dfx.mdc_hash ) if( dfx.mdc_hash )
md_write( dfx.mdc_hash, temp, nprefix+2 ); gcry_md_write( dfx.mdc_hash, temp, nprefix+2 );
if( ed->mdc_method ) if( ed->mdc_method )
iobuf_push_filter( ed->buf, mdc_decode_filter, &dfx ); iobuf_push_filter( ed->buf, mdc_decode_filter, &dfx );
@ -136,23 +147,23 @@ decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
proc_packets( procctx, ed->buf ); proc_packets( procctx, ed->buf );
ed->buf = NULL; ed->buf = NULL;
if( ed->mdc_method && dfx.eof_seen == 2 ) if( ed->mdc_method && dfx.eof_seen == 2 )
rc = G10ERR_INVALID_PACKET; rc = gpg_error (GPG_ERR_INV_PACKET);
else if( ed->mdc_method ) { /* check the mdc */ else if( ed->mdc_method ) { /* check the mdc */
int datalen = md_digest_length( ed->mdc_method ); int datalen = gcry_md_get_algo_dlen (ed->mdc_method);
cipher_decrypt( dfx.cipher_hd, dfx.defer, dfx.defer, 20); gcry_cipher_decrypt (dfx.cipher_hd, dfx.defer, 20, NULL, 0);
md_final( dfx.mdc_hash ); gcry_md_final ( dfx.mdc_hash );
if( datalen != 20 if( datalen != 20
|| memcmp(md_read( dfx.mdc_hash, 0 ), dfx.defer, datalen) ) || memcmp(gcry_md_read ( dfx.mdc_hash, 0 ), dfx.defer, datalen) )
rc = G10ERR_BAD_SIGN; rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
/*log_hexdump("MDC calculated:", md_read( dfx.mdc_hash, 0), datalen);*/ /*log_hexdump("MDC calculated:", gcry_md_read ( dfx.mdc_hash, 0), datalen);*/
/*log_hexdump("MDC message :", dfx.defer, 20);*/ /*log_hexdump("MDC message :", dfx.defer, 20);*/
} }
leave: leave:
cipher_close(dfx.cipher_hd); gcry_cipher_close(dfx.cipher_hd);
md_close( dfx.mdc_hash ); gcry_md_close ( dfx.mdc_hash );
return rc; return rc;
} }
@ -160,7 +171,7 @@ decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
/* I think we should merge this with cipher_filter */ /* I think we should merge this with cipher_filter */
static int static int
mdc_decode_filter( void *opaque, int control, IOBUF a, mdc_decode_filter( void *opaque, int control, iobuf_t a,
byte *buf, size_t *ret_len) byte *buf, size_t *ret_len)
{ {
decode_filter_ctx_t *dfx = opaque; decode_filter_ctx_t *dfx = opaque;
@ -218,8 +229,8 @@ mdc_decode_filter( void *opaque, int control, IOBUF a,
} }
if( n ) { if( n ) {
cipher_decrypt( dfx->cipher_hd, buf, buf, n); gcry_cipher_decrypt( dfx->cipher_hd, buf, n, NULL, 0);
md_write( dfx->mdc_hash, buf, n ); gcry_md_write( dfx->mdc_hash, buf, n );
} }
else { else {
assert( dfx->eof_seen ); assert( dfx->eof_seen );
@ -234,7 +245,7 @@ mdc_decode_filter( void *opaque, int control, IOBUF a,
} }
static int static int
decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len) decode_filter( void *opaque, int control, iobuf_t a, byte *buf, size_t *ret_len)
{ {
decode_filter_ctx_t *fc = opaque; decode_filter_ctx_t *fc = opaque;
size_t n, size = *ret_len; size_t n, size = *ret_len;
@ -245,7 +256,7 @@ decode_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len)
n = iobuf_read( a, buf, size ); n = iobuf_read( a, buf, size );
if( n == -1 ) n = 0; if( n == -1 ) n = 0;
if( n ) if( n )
cipher_decrypt( fc->cipher_hd, buf, buf, n); gcry_cipher_decrypt( fc->cipher_hd, buf, n, NULL, 0);
else else
rc = -1; /* eof */ rc = -1; /* eof */
*ret_len = n; *ret_len = n;

View File

@ -46,12 +46,12 @@ int exec_write(struct exec_info **info,const char *program,
const char *args_in,const char *name,int writeonly,int binary) const char *args_in,const char *name,int writeonly,int binary)
{ {
log_error(_("no remote program execution supported\n")); log_error(_("no remote program execution supported\n"));
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
int exec_read(struct exec_info *info) { return G10ERR_GENERAL; } int exec_read(struct exec_info *info) { return GPG_ERR_GENERAL; }
int exec_finish(struct exec_info *info) { return G10ERR_GENERAL; } int exec_finish(struct exec_info *info) { return GPG_ERR_GENERAL; }
int set_exec_path(const char *path,int method) { return G10ERR_GENERAL; } int set_exec_path(const char *path,int method) { return GPG_ERR_GENERAL; }
#else /* ! NO_EXEC */ #else /* ! NO_EXEC */
@ -71,7 +71,7 @@ static int win_system(const char *command)
/* We must use a copy of the command as CreateProcess modifies this /* We must use a copy of the command as CreateProcess modifies this
argument. */ argument. */
string=m_strdup(command); string=xstrdup (command);
memset(&pi,0,sizeof(pi)); memset(&pi,0,sizeof(pi));
memset(&si,0,sizeof(si)); memset(&si,0,sizeof(si));
@ -85,7 +85,7 @@ static int win_system(const char *command)
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
m_free(string); xfree (string);
return 0; return 0;
} }
@ -101,7 +101,7 @@ int set_exec_path(const char *path,int method)
if(method==1 && (curpath=getenv("PATH"))) if(method==1 && (curpath=getenv("PATH")))
curlen=strlen(curpath)+1; curlen=strlen(curpath)+1;
p=m_alloc(5+curlen+strlen(path)+1); p=xmalloc (5+curlen+strlen(path)+1);
strcpy(p,"PATH="); strcpy(p,"PATH=");
if(curpath) if(curpath)
@ -120,7 +120,7 @@ int set_exec_path(const char *path,int method)
set_exec_path multiple times. */ set_exec_path multiple times. */
if(putenv(p)!=0) if(putenv(p)!=0)
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
else else
return 0; return 0;
} }
@ -140,7 +140,7 @@ static int make_tempdir(struct exec_info *info)
if(tmp==NULL) if(tmp==NULL)
{ {
#if defined (__MINGW32__) #if defined (__MINGW32__)
tmp=m_alloc(256); tmp=xmalloc (256);
if(GetTempPath(256,tmp)==0) if(GetTempPath(256,tmp)==0)
strcpy(tmp,"c:\\windows\\temp"); strcpy(tmp,"c:\\windows\\temp");
else else
@ -172,12 +172,12 @@ static int make_tempdir(struct exec_info *info)
#endif #endif
} }
info->tempdir=m_alloc(strlen(tmp)+strlen(DIRSEP_S)+10+1); info->tempdir=xmalloc (strlen(tmp)+strlen(DIRSEP_S)+10+1);
sprintf(info->tempdir,"%s" DIRSEP_S "gpg-XXXXXX",tmp); sprintf(info->tempdir,"%s" DIRSEP_S "gpg-XXXXXX",tmp);
#if defined (__MINGW32__) #if defined (__MINGW32__)
m_free(tmp); xfree (tmp);
#endif #endif
if(mkdtemp(info->tempdir)==NULL) if(mkdtemp(info->tempdir)==NULL)
@ -187,19 +187,19 @@ static int make_tempdir(struct exec_info *info)
{ {
info->madedir=1; info->madedir=1;
info->tempfile_in=m_alloc(strlen(info->tempdir)+ info->tempfile_in=xmalloc (strlen(info->tempdir)+
strlen(DIRSEP_S)+strlen(namein)+1); strlen(DIRSEP_S)+strlen(namein)+1);
sprintf(info->tempfile_in,"%s" DIRSEP_S "%s",info->tempdir,namein); sprintf(info->tempfile_in,"%s" DIRSEP_S "%s",info->tempdir,namein);
if(!info->writeonly) if(!info->writeonly)
{ {
info->tempfile_out=m_alloc(strlen(info->tempdir)+ info->tempfile_out=xmalloc (strlen(info->tempdir)+
strlen(DIRSEP_S)+strlen(nameout)+1); strlen(DIRSEP_S)+strlen(nameout)+1);
sprintf(info->tempfile_out,"%s" DIRSEP_S "%s",info->tempdir,nameout); sprintf(info->tempfile_out,"%s" DIRSEP_S "%s",info->tempdir,nameout);
} }
} }
return info->madedir?0:G10ERR_GENERAL; return info->madedir?0:GPG_ERR_GENERAL;
} }
/* Expands %i and %o in the args to the full temp files within the /* Expands %i and %o in the args to the full temp files within the
@ -216,7 +216,7 @@ static int expand_args(struct exec_info *info,const char *args_in)
log_debug("expanding string \"%s\"\n",args_in); log_debug("expanding string \"%s\"\n",args_in);
size=100; size=100;
info->command=m_alloc(size); info->command=xmalloc (size);
len=0; len=0;
info->command[0]='\0'; info->command[0]='\0';
@ -273,7 +273,7 @@ static int expand_args(struct exec_info *info,const char *args_in)
applen=100; applen=100;
size+=applen; size+=applen;
info->command=m_realloc(info->command,size); info->command=xrealloc(info->command,size);
} }
strcat(info->command,append); strcat(info->command,append);
@ -285,7 +285,7 @@ static int expand_args(struct exec_info *info,const char *args_in)
if(len==size-1) /* leave room for the \0 */ if(len==size-1) /* leave room for the \0 */
{ {
size+=100; size+=100;
info->command=m_realloc(info->command,size); info->command=xrealloc(info->command,size);
} }
info->command[len++]=*ch; info->command[len++]=*ch;
@ -303,10 +303,10 @@ static int expand_args(struct exec_info *info,const char *args_in)
fail: fail:
m_free(info->command); xfree (info->command);
info->command=NULL; info->command=NULL;
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
/* Either handles the tempfile creation, or the fork/exec. If it /* Either handles the tempfile creation, or the fork/exec. If it
@ -318,7 +318,7 @@ static int expand_args(struct exec_info *info,const char *args_in)
int exec_write(struct exec_info **info,const char *program, int exec_write(struct exec_info **info,const char *program,
const char *args_in,const char *name,int writeonly,int binary) const char *args_in,const char *name,int writeonly,int binary)
{ {
int ret=G10ERR_GENERAL; int ret=GPG_ERR_GENERAL;
if(opt.exec_disable && !opt.no_perm_warn) if(opt.exec_disable && !opt.no_perm_warn)
{ {
@ -338,10 +338,10 @@ int exec_write(struct exec_info **info,const char *program,
if(program==NULL && args_in==NULL) if(program==NULL && args_in==NULL)
BUG(); BUG();
*info=m_alloc_clear(sizeof(struct exec_info)); *info=xcalloc (1,sizeof(struct exec_info));
if(name) if(name)
(*info)->name=m_strdup(name); (*info)->name=xstrdup (name);
(*info)->binary=binary; (*info)->binary=binary;
(*info)->writeonly=writeonly; (*info)->writeonly=writeonly;
@ -449,8 +449,8 @@ int exec_write(struct exec_info **info,const char *program,
(*info)->tochild=fdopen(to[1],binary?"wb":"w"); (*info)->tochild=fdopen(to[1],binary?"wb":"w");
if((*info)->tochild==NULL) if((*info)->tochild==NULL)
{ {
ret = gpg_error_from_errno (errno);
close(to[1]); close(to[1]);
ret=G10ERR_WRITE_FILE;
goto fail; goto fail;
} }
@ -459,8 +459,8 @@ int exec_write(struct exec_info **info,const char *program,
(*info)->fromchild=iobuf_fdopen(from[0],"r"); (*info)->fromchild=iobuf_fdopen(from[0],"r");
if((*info)->fromchild==NULL) if((*info)->fromchild==NULL)
{ {
ret = gpg_error_from_errno (errno);
close(from[0]); close(from[0]);
ret=G10ERR_READ_FILE;
goto fail; goto fail;
} }
@ -478,9 +478,9 @@ int exec_write(struct exec_info **info,const char *program,
(*info)->tochild=fopen((*info)->tempfile_in,binary?"wb":"w"); (*info)->tochild=fopen((*info)->tempfile_in,binary?"wb":"w");
if((*info)->tochild==NULL) if((*info)->tochild==NULL)
{ {
ret = gpg_error_from_errno (errno);
log_error(_("can't create `%s': %s\n"), log_error(_("can't create `%s': %s\n"),
(*info)->tempfile_in,strerror(errno)); (*info)->tempfile_in,strerror(errno));
ret=G10ERR_WRITE_FILE;
goto fail; goto fail;
} }
@ -492,7 +492,7 @@ int exec_write(struct exec_info **info,const char *program,
int exec_read(struct exec_info *info) int exec_read(struct exec_info *info)
{ {
int ret=G10ERR_GENERAL; int ret=GPG_ERR_GENERAL;
fclose(info->tochild); fclose(info->tochild);
info->tochild=NULL; info->tochild=NULL;
@ -545,9 +545,9 @@ int exec_read(struct exec_info *info)
info->fromchild=iobuf_open(info->tempfile_out); info->fromchild=iobuf_open(info->tempfile_out);
if(info->fromchild==NULL) if(info->fromchild==NULL)
{ {
ret = gpg_error_from_errno (errno);
log_error(_("unable to read external program response: %s\n"), log_error(_("unable to read external program response: %s\n"),
strerror(errno)); strerror(errno));
ret=G10ERR_READ_FILE;
goto fail; goto fail;
} }
@ -607,12 +607,12 @@ int exec_finish(struct exec_info *info)
info->tempdir,strerror(errno)); info->tempdir,strerror(errno));
} }
m_free(info->command); xfree (info->command);
m_free(info->name); xfree (info->name);
m_free(info->tempdir); xfree (info->tempdir);
m_free(info->tempfile_in); xfree (info->tempfile_in);
m_free(info->tempfile_out); xfree (info->tempfile_out);
m_free(info); xfree (info);
return ret; return ret;
} }

View File

@ -1,5 +1,5 @@
/* exec.h /* exec.h
* Copyright (C) 2001, 2002 Free Software Foundation, Inc. * Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -23,14 +23,14 @@
#include <unistd.h> #include <unistd.h>
#include <stdio.h> #include <stdio.h>
#include "iobuf.h" #include "../common/iobuf.h"
struct exec_info struct exec_info
{ {
int progreturn,binary,writeonly,madedir,use_temp_files,keep_temp_files; int progreturn,binary,writeonly,madedir,use_temp_files,keep_temp_files;
pid_t child; pid_t child;
FILE *tochild; FILE *tochild;
IOBUF fromchild; iobuf_t fromchild;
char *command,*name,*tempdir,*tempfile_in,*tempfile_out; char *command,*name,*tempdir,*tempfile_in,*tempfile_out;
}; };

View File

@ -35,7 +35,7 @@
#include "i18n.h" #include "i18n.h"
static int do_export( STRLIST users, int secret, unsigned int options ); static int do_export( STRLIST users, int secret, unsigned int options );
static int do_export_stream( IOBUF out, STRLIST users, int secret, static int do_export_stream( iobuf_t out, STRLIST users, int secret,
KBNODE *keyblock_out, unsigned int options, KBNODE *keyblock_out, unsigned int options,
int *any ); int *any );
@ -71,7 +71,7 @@ export_pubkeys( STRLIST users, unsigned int options )
* been exported * been exported
*/ */
int int
export_pubkeys_stream( IOBUF out, STRLIST users, export_pubkeys_stream( iobuf_t out, STRLIST users,
KBNODE *keyblock_out, unsigned int options ) KBNODE *keyblock_out, unsigned int options )
{ {
int any, rc; int any, rc;
@ -97,7 +97,7 @@ export_secsubkeys( STRLIST users )
static int static int
do_export( STRLIST users, int secret, unsigned int options ) do_export( STRLIST users, int secret, unsigned int options )
{ {
IOBUF out = NULL; iobuf_t out = NULL;
int any, rc; int any, rc;
armor_filter_context_t afx; armor_filter_context_t afx;
compress_filter_context_t zfx; compress_filter_context_t zfx;
@ -129,7 +129,7 @@ do_export( STRLIST users, int secret, unsigned int options )
contains a pointer to the first keyblock found and exported. No contains a pointer to the first keyblock found and exported. No
other keyblocks are exported. The caller must free it. */ other keyblocks are exported. The caller must free it. */
static int static int
do_export_stream( IOBUF out, STRLIST users, int secret, do_export_stream( iobuf_t out, STRLIST users, int secret,
KBNODE *keyblock_out, unsigned int options, int *any ) KBNODE *keyblock_out, unsigned int options, int *any )
{ {
int rc = 0; int rc = 0;
@ -147,20 +147,20 @@ do_export_stream( IOBUF out, STRLIST users, int secret,
if (!users) { if (!users) {
ndesc = 1; ndesc = 1;
desc = m_alloc_clear ( ndesc * sizeof *desc); desc = xcalloc (1, ndesc * sizeof *desc);
desc[0].mode = KEYDB_SEARCH_MODE_FIRST; desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
} }
else { else {
for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++) for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++)
; ;
desc = m_alloc ( ndesc * sizeof *desc); desc = xmalloc ( ndesc * sizeof *desc);
for (ndesc=0, sl=users; sl; sl = sl->next) { for (ndesc=0, sl=users; sl; sl = sl->next) {
if (classify_user_id (sl->d, desc+ndesc)) if (classify_user_id (sl->d, desc+ndesc))
ndesc++; ndesc++;
else else
log_error (_("key `%s' not found: %s\n"), log_error (_("key `%s' not found: %s\n"),
sl->d, g10_errstr (G10ERR_INV_USER_ID)); sl->d, gpg_strerror (GPG_ERR_INV_USER_ID));
} }
/* it would be nice to see which of the given users did /* it would be nice to see which of the given users did
@ -181,7 +181,7 @@ do_export_stream( IOBUF out, STRLIST users, int secret,
/* read the keyblock */ /* read the keyblock */
rc = keydb_get_keyblock (kdbhd, &keyblock ); rc = keydb_get_keyblock (kdbhd, &keyblock );
if( rc ) { if( rc ) {
log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) ); log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc) );
goto leave; goto leave;
} }
@ -370,8 +370,7 @@ do_export_stream( IOBUF out, STRLIST users, int secret,
if( rc ) { if( rc ) {
log_error("build_packet(%d) failed: %s\n", log_error("build_packet(%d) failed: %s\n",
node->pkt->pkttype, g10_errstr(rc) ); node->pkt->pkttype, gpg_strerror (rc) );
rc = G10ERR_WRITE_FILE;
goto leave; goto leave;
} }
} }
@ -386,7 +385,7 @@ do_export_stream( IOBUF out, STRLIST users, int secret,
rc = 0; rc = 0;
leave: leave:
m_free(desc); xfree (desc);
keydb_release (kdbhd); keydb_release (kdbhd);
if(rc || keyblock_out==NULL) if(rc || keyblock_out==NULL)
release_kbnode( keyblock ); release_kbnode( keyblock );

View File

@ -22,6 +22,7 @@
#include "types.h" #include "types.h"
#include "cipher.h" #include "cipher.h"
#include "iobuf.h"
typedef struct { typedef struct {
MD_HANDLE md; /* catch all */ MD_HANDLE md; /* catch all */
@ -120,35 +121,35 @@ typedef struct {
/* encrypt_filter_context_t defined in main.h */ /* encrypt_filter_context_t defined in main.h */
/*-- mdfilter.c --*/ /*-- mdfilter.c --*/
int md_filter( void *opaque, int control, IOBUF a, byte *buf, size_t *ret_len); int md_filter( void *opaque, int control, iobuf_t a, byte *buf, size_t *ret_len);
void free_md_filter_context( md_filter_context_t *mfx ); void free_md_filter_context( md_filter_context_t *mfx );
/*-- armor.c --*/ /*-- armor.c --*/
int use_armor_filter( IOBUF a ); int use_armor_filter( iobuf_t a );
int armor_filter( void *opaque, int control, int armor_filter( void *opaque, int control,
IOBUF chain, byte *buf, size_t *ret_len); iobuf_t chain, byte *buf, size_t *ret_len);
UnarmorPump unarmor_pump_new (void); UnarmorPump unarmor_pump_new (void);
void unarmor_pump_release (UnarmorPump x); void unarmor_pump_release (UnarmorPump x);
int unarmor_pump (UnarmorPump x, int c); int unarmor_pump (UnarmorPump x, int c);
/*-- compress.c --*/ /*-- compress.c --*/
int compress_filter( void *opaque, int control, int compress_filter( void *opaque, int control,
IOBUF chain, byte *buf, size_t *ret_len); iobuf_t chain, byte *buf, size_t *ret_len);
/*-- cipher.c --*/ /*-- cipher.c --*/
int cipher_filter( void *opaque, int control, int cipher_filter( void *opaque, int control,
IOBUF chain, byte *buf, size_t *ret_len); iobuf_t chain, byte *buf, size_t *ret_len);
/*-- textfilter.c --*/ /*-- textfilter.c --*/
int text_filter( void *opaque, int control, int text_filter( void *opaque, int control,
IOBUF chain, byte *buf, size_t *ret_len); iobuf_t chain, byte *buf, size_t *ret_len);
int copy_clearsig_text( IOBUF out, IOBUF inp, MD_HANDLE md, int copy_clearsig_text( iobuf_t out, iobuf_t inp, MD_HANDLE md,
int escape_dash, int escape_from, int pgp2mode ); int escape_dash, int escape_from, int pgp2mode );
/*-- progress.c --*/ /*-- progress.c --*/
int progress_filter (void *opaque, int control, int progress_filter (void *opaque, int control,
IOBUF a, byte *buf, size_t *ret_len); iobuf_t a, byte *buf, size_t *ret_len);
void handle_progress (progress_filter_context_t *pfx, void handle_progress (progress_filter_context_t *pfx,
IOBUF inp, const char *name); iobuf_t inp, const char *name);
#endif /*G10_FILTER_H*/ #endif /*G10_FILTER_H*/

View File

@ -36,7 +36,7 @@
void void
free_symkey_enc( PKT_symkey_enc *enc ) free_symkey_enc( PKT_symkey_enc *enc )
{ {
m_free(enc); xfree (enc);
} }
void void
@ -45,10 +45,10 @@ free_pubkey_enc( PKT_pubkey_enc *enc )
int n, i; int n, i;
n = pubkey_get_nenc( enc->pubkey_algo ); n = pubkey_get_nenc( enc->pubkey_algo );
if( !n ) if( !n )
mpi_free(enc->data[0]); mpi_release (enc->data[0]);
for(i=0; i < n; i++ ) for(i=0; i < n; i++ )
mpi_free( enc->data[i] ); mpi_release ( enc->data[i] );
m_free(enc); xfree (enc);
} }
void void
@ -58,14 +58,14 @@ free_seckey_enc( PKT_signature *sig )
n = pubkey_get_nsig( sig->pubkey_algo ); n = pubkey_get_nsig( sig->pubkey_algo );
if( !n ) if( !n )
mpi_free(sig->data[0]); mpi_release (sig->data[0]);
for(i=0; i < n; i++ ) for(i=0; i < n; i++ )
mpi_free( sig->data[i] ); mpi_release ( sig->data[i] );
m_free(sig->revkey); xfree (sig->revkey);
m_free(sig->hashed); xfree (sig->hashed);
m_free(sig->unhashed); xfree (sig->unhashed);
m_free(sig); xfree (sig);
} }
@ -75,13 +75,13 @@ release_public_key_parts( PKT_public_key *pk )
int n, i; int n, i;
n = pubkey_get_npkey( pk->pubkey_algo ); n = pubkey_get_npkey( pk->pubkey_algo );
if( !n ) if( !n )
mpi_free(pk->pkey[0]); mpi_release (pk->pkey[0]);
for(i=0; i < n; i++ ) { for(i=0; i < n; i++ ) {
mpi_free( pk->pkey[i] ); mpi_release ( pk->pkey[i] );
pk->pkey[i] = NULL; pk->pkey[i] = NULL;
} }
if (pk->prefs) { if (pk->prefs) {
m_free (pk->prefs); xfree (pk->prefs);
pk->prefs = NULL; pk->prefs = NULL;
} }
if (pk->user_id) { if (pk->user_id) {
@ -89,7 +89,7 @@ release_public_key_parts( PKT_public_key *pk )
pk->user_id = NULL; pk->user_id = NULL;
} }
if (pk->revkey) { if (pk->revkey) {
m_free(pk->revkey); xfree (pk->revkey);
pk->revkey=NULL; pk->revkey=NULL;
pk->numrevkeys=0; pk->numrevkeys=0;
} }
@ -100,7 +100,7 @@ void
free_public_key( PKT_public_key *pk ) free_public_key( PKT_public_key *pk )
{ {
release_public_key_parts( pk ); release_public_key_parts( pk );
m_free(pk); xfree (pk);
} }
@ -111,7 +111,7 @@ cp_subpktarea (subpktarea_t *s )
if( !s ) if( !s )
return NULL; return NULL;
d = m_alloc (sizeof (*d) + s->size - 1 ); d = xmalloc (sizeof (*d) + s->size - 1 );
d->size = s->size; d->size = s->size;
d->len = s->len; d->len = s->len;
memcpy (d->data, s->data, s->len); memcpy (d->data, s->data, s->len);
@ -132,7 +132,7 @@ copy_prefs (const prefitem_t *prefs)
for (n=0; prefs[n].type; n++) for (n=0; prefs[n].type; n++)
; ;
new = m_alloc ( sizeof (*new) * (n+1)); new = xmalloc ( sizeof (*new) * (n+1));
for (n=0; prefs[n].type; n++) { for (n=0; prefs[n].type; n++) {
new[n].type = prefs[n].type; new[n].type = prefs[n].type;
new[n].value = prefs[n].value; new[n].value = prefs[n].value;
@ -150,7 +150,7 @@ copy_public_key ( PKT_public_key *d, PKT_public_key *s)
int n, i; int n, i;
if( !d ) if( !d )
d = m_alloc(sizeof *d); d = xmalloc (sizeof *d);
memcpy( d, s, sizeof *d ); memcpy( d, s, sizeof *d );
d->user_id = scopy_user_id (s->user_id); d->user_id = scopy_user_id (s->user_id);
d->prefs = copy_prefs (s->prefs); d->prefs = copy_prefs (s->prefs);
@ -164,7 +164,7 @@ copy_public_key ( PKT_public_key *d, PKT_public_key *s)
if( !s->revkey && s->numrevkeys ) if( !s->revkey && s->numrevkeys )
BUG(); BUG();
if( s->numrevkeys ) { if( s->numrevkeys ) {
d->revkey = m_alloc(sizeof(struct revocation_key)*s->numrevkeys); d->revkey = xmalloc (sizeof(struct revocation_key)*s->numrevkeys);
memcpy(d->revkey,s->revkey,sizeof(struct revocation_key)*s->numrevkeys); memcpy(d->revkey,s->revkey,sizeof(struct revocation_key)*s->numrevkeys);
} }
else else
@ -200,7 +200,7 @@ copy_signature( PKT_signature *d, PKT_signature *s )
int n, i; int n, i;
if( !d ) if( !d )
d = m_alloc(sizeof *d); d = xmalloc (sizeof *d);
memcpy( d, s, sizeof *d ); memcpy( d, s, sizeof *d );
n = pubkey_get_nsig( s->pubkey_algo ); n = pubkey_get_nsig( s->pubkey_algo );
if( !n ) if( !n )
@ -241,9 +241,9 @@ release_secret_key_parts( PKT_secret_key *sk )
n = pubkey_get_nskey( sk->pubkey_algo ); n = pubkey_get_nskey( sk->pubkey_algo );
if( !n ) if( !n )
mpi_free(sk->skey[0]); mpi_release (sk->skey[0]);
for(i=0; i < n; i++ ) { for(i=0; i < n; i++ ) {
mpi_free( sk->skey[i] ); mpi_release ( sk->skey[i] );
sk->skey[i] = NULL; sk->skey[i] = NULL;
} }
} }
@ -252,7 +252,7 @@ void
free_secret_key( PKT_secret_key *sk ) free_secret_key( PKT_secret_key *sk )
{ {
release_secret_key_parts( sk ); release_secret_key_parts( sk );
m_free(sk); xfree (sk);
} }
PKT_secret_key * PKT_secret_key *
@ -261,7 +261,7 @@ copy_secret_key( PKT_secret_key *d, PKT_secret_key *s )
int n, i; int n, i;
if( !d ) if( !d )
d = m_alloc(sizeof *d); d = xmalloc (sizeof *d);
memcpy( d, s, sizeof *d ); memcpy( d, s, sizeof *d );
n = pubkey_get_nskey( s->pubkey_algo ); n = pubkey_get_nskey( s->pubkey_algo );
if( !n ) if( !n )
@ -276,14 +276,14 @@ copy_secret_key( PKT_secret_key *d, PKT_secret_key *s )
void void
free_comment( PKT_comment *rem ) free_comment( PKT_comment *rem )
{ {
m_free(rem); xfree (rem);
} }
void void
free_attributes(PKT_user_id *uid) free_attributes(PKT_user_id *uid)
{ {
m_free(uid->attribs); xfree (uid->attribs);
m_free(uid->attrib_data); xfree (uid->attrib_data);
uid->attribs=NULL; uid->attribs=NULL;
uid->attrib_data=NULL; uid->attrib_data=NULL;
@ -298,9 +298,9 @@ free_user_id (PKT_user_id *uid)
return; return;
free_attributes(uid); free_attributes(uid);
m_free (uid->prefs); xfree (uid->prefs);
m_free (uid->namehash); xfree (uid->namehash);
m_free (uid); xfree (uid);
} }
void void
@ -312,7 +312,7 @@ free_compressed( PKT_compressed *zd )
while( iobuf_read( zd->buf, NULL, 1<<30 ) != -1 ) while( iobuf_read( zd->buf, NULL, 1<<30 ) != -1 )
; ;
} }
m_free(zd); xfree (zd);
} }
void void
@ -333,7 +333,7 @@ free_encrypted( PKT_encrypted *ed )
} }
} }
} }
m_free(ed); xfree (ed);
} }
@ -355,7 +355,7 @@ free_plaintext( PKT_plaintext *pt )
} }
} }
} }
m_free(pt); xfree (pt);
} }
/**************** /****************
@ -405,7 +405,7 @@ free_packet( PACKET *pkt )
free_plaintext( pkt->pkt.plaintext ); free_plaintext( pkt->pkt.plaintext );
break; break;
default: default:
m_free( pkt->pkt.generic ); xfree ( pkt->pkt.generic );
break; break;
} }
pkt->pkt.generic = NULL; pkt->pkt.generic = NULL;

441
g10/g10.c

File diff suppressed because it is too large Load Diff

View File

@ -25,6 +25,8 @@
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "packet.h" #include "packet.h"
#include "memory.h" #include "memory.h"
@ -152,7 +154,7 @@ cache_public_key( PKT_public_key *pk )
return; return;
} }
pk_cache_entries++; pk_cache_entries++;
ce = m_alloc( sizeof *ce ); ce = xmalloc ( sizeof *ce );
ce->next = pk_cache; ce->next = pk_cache;
pk_cache = ce; pk_cache = ce;
ce->pk = copy_public_key( NULL, pk ); ce->pk = copy_public_key( NULL, pk );
@ -195,7 +197,7 @@ release_keyid_list ( keyid_list_t k )
{ {
while ( k ) { while ( k ) {
keyid_list_t k2 = k->next; keyid_list_t k2 = k->next;
m_free (k); xfree (k);
k = k2; k = k2;
} }
} }
@ -216,7 +218,7 @@ cache_user_id( KBNODE keyblock )
for (k=keyblock; k; k = k->next ) { for (k=keyblock; k; k = k->next ) {
if ( k->pkt->pkttype == PKT_PUBLIC_KEY if ( k->pkt->pkttype == PKT_PUBLIC_KEY
|| k->pkt->pkttype == PKT_PUBLIC_SUBKEY ) { || k->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
keyid_list_t a = m_alloc_clear ( sizeof *a ); keyid_list_t a = xcalloc (1, sizeof *a );
/* Hmmm: For a long list of keyids it might be an advantage /* Hmmm: For a long list of keyids it might be an advantage
* to append the keys */ * to append the keys */
keyid_from_pk( k->pkt->pkt.public_key, a->keyid ); keyid_from_pk( k->pkt->pkt.public_key, a->keyid );
@ -229,7 +231,7 @@ cache_user_id( KBNODE keyblock )
if( DBG_CACHE ) if( DBG_CACHE )
log_debug("cache_user_id: already in cache\n"); log_debug("cache_user_id: already in cache\n");
release_keyid_list ( keyids ); release_keyid_list ( keyids );
m_free ( a ); xfree ( a );
return; return;
} }
} }
@ -250,10 +252,10 @@ cache_user_id( KBNODE keyblock )
r = user_id_db; r = user_id_db;
user_id_db = r->next; user_id_db = r->next;
release_keyid_list ( r->keyids ); release_keyid_list ( r->keyids );
m_free(r); xfree (r);
uid_cache_entries--; uid_cache_entries--;
} }
r = m_alloc( sizeof *r + uidlen-1 ); r = xmalloc ( sizeof *r + uidlen-1 );
r->keyids = keyids; r->keyids = keyids;
r->len = uidlen; r->len = uidlen;
memcpy(r->name, uid, r->len); memcpy(r->name, uid, r->len);
@ -273,7 +275,7 @@ getkey_disable_caches()
for( ce = pk_cache; ce; ce = ce2 ) { for( ce = pk_cache; ce; ce = ce2 ) {
ce2 = ce->next; ce2 = ce->next;
free_public_key( ce->pk ); free_public_key( ce->pk );
m_free( ce ); xfree ( ce );
} }
pk_cache_disabled=1; pk_cache_disabled=1;
pk_cache_entries = 0; pk_cache_entries = 0;
@ -333,7 +335,7 @@ get_pubkey( PKT_public_key *pk, u32 *keyid )
#endif #endif
/* more init stuff */ /* more init stuff */
if( !pk ) { if( !pk ) {
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
internal++; internal++;
} }
@ -361,7 +363,7 @@ get_pubkey( PKT_public_key *pk, u32 *keyid )
if( !rc ) if( !rc )
goto leave; goto leave;
rc = G10ERR_NO_PUBKEY; rc = GPG_ERR_NO_PUBKEY;
leave: leave:
if( !rc ) if( !rc )
@ -404,14 +406,14 @@ get_pubkey_fast (PKT_public_key *pk, u32 *keyid)
if (rc == -1) if (rc == -1)
{ {
keydb_release (hd); keydb_release (hd);
return G10ERR_NO_PUBKEY; return GPG_ERR_NO_PUBKEY;
} }
rc = keydb_get_keyblock (hd, &keyblock); rc = keydb_get_keyblock (hd, &keyblock);
keydb_release (hd); keydb_release (hd);
if (rc) if (rc)
{ {
log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc)); log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
return G10ERR_NO_PUBKEY; return GPG_ERR_NO_PUBKEY;
} }
assert ( keyblock->pkt->pkttype == PKT_PUBLIC_KEY assert ( keyblock->pkt->pkttype == PKT_PUBLIC_KEY
@ -494,7 +496,7 @@ get_seckey( PKT_secret_key *sk, u32 *keyid )
* check and does not tell us whether the secret key is valid. It * check and does not tell us whether the secret key is valid. It
* merely tells other whether there is some secret key. * merely tells other whether there is some secret key.
* Returns: 0 := key is available * Returns: 0 := key is available
* G10ERR_NO_SECKEY := not availabe * GPG_ERR_NO_SECKEY := not availabe
*/ */
int int
seckey_available( u32 *keyid ) seckey_available( u32 *keyid )
@ -504,7 +506,7 @@ seckey_available( u32 *keyid )
rc = keydb_search_kid (hd, keyid); rc = keydb_search_kid (hd, keyid);
if ( rc == -1 ) if ( rc == -1 )
rc = G10ERR_NO_SECKEY; rc = GPG_ERR_NO_SECKEY;
keydb_release (hd); keydb_release (hd);
return rc; return rc;
} }
@ -729,13 +731,13 @@ static int
skip_disabled(void *dummy,u32 *keyid) skip_disabled(void *dummy,u32 *keyid)
{ {
int rc,disabled=0; int rc,disabled=0;
PKT_public_key *pk=m_alloc_clear(sizeof(PKT_public_key)); PKT_public_key *pk=xcalloc (1,sizeof(PKT_public_key));
rc = get_pubkey(pk, keyid); rc = get_pubkey(pk, keyid);
if(rc) if(rc)
{ {
log_error("error checking disabled status of %08lX: %s\n", log_error("error checking disabled status of %08lX: %s\n",
(ulong)keyid[1],g10_errstr(rc)); (ulong)keyid[1],gpg_strerror (rc));
goto leave; goto leave;
} }
@ -778,7 +780,7 @@ key_byname( GETKEY_CTX *retctx, STRLIST namelist,
/* build the search context */ /* build the search context */
for(n=0, r=namelist; r; r = r->next ) for(n=0, r=namelist; r; r = r->next )
n++; n++;
ctx = m_alloc_clear (sizeof *ctx + (n-1)*sizeof ctx->items ); ctx = xcalloc (1,sizeof *ctx + (n-1)*sizeof ctx->items );
ctx->nitems = n; ctx->nitems = n;
for(n=0, r=namelist; r; r = r->next, n++ ) { for(n=0, r=namelist; r; r = r->next, n++ ) {
@ -787,8 +789,8 @@ key_byname( GETKEY_CTX *retctx, STRLIST namelist,
if (ctx->items[n].exact) if (ctx->items[n].exact)
ctx->exact = 1; ctx->exact = 1;
if (!ctx->items[n].mode) { if (!ctx->items[n].mode) {
m_free (ctx); xfree (ctx);
return G10ERR_INV_USER_ID; return GPG_ERR_INV_USER_ID;
} }
if(!include_disabled if(!include_disabled
&& ctx->items[n].mode!=KEYDB_SEARCH_MODE_SHORT_KID && ctx->items[n].mode!=KEYDB_SEARCH_MODE_SHORT_KID
@ -886,7 +888,7 @@ get_pubkey_end( GETKEY_CTX ctx )
memset (&ctx->kbpos, 0, sizeof ctx->kbpos); memset (&ctx->kbpos, 0, sizeof ctx->kbpos);
keydb_release (ctx->kr_handle); keydb_release (ctx->kr_handle);
if( !ctx->not_allocated ) if( !ctx->not_allocated )
m_free( ctx ); xfree ( ctx );
} }
} }
@ -924,7 +926,7 @@ get_pubkey_byfprint( PKT_public_key *pk,
get_pubkey_end( &ctx ); get_pubkey_end( &ctx );
} }
else else
rc = G10ERR_GENERAL; /* Oops */ rc = GPG_ERR_GENERAL; /* Oops */
return rc; return rc;
} }
@ -954,14 +956,14 @@ get_pubkey_byfprint_fast (PKT_public_key *pk,
if (rc == -1) if (rc == -1)
{ {
keydb_release (hd); keydb_release (hd);
return G10ERR_NO_PUBKEY; return GPG_ERR_NO_PUBKEY;
} }
rc = keydb_get_keyblock (hd, &keyblock); rc = keydb_get_keyblock (hd, &keyblock);
keydb_release (hd); keydb_release (hd);
if (rc) if (rc)
{ {
log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc)); log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
return G10ERR_NO_PUBKEY; return GPG_ERR_NO_PUBKEY;
} }
assert ( keyblock->pkt->pkttype == PKT_PUBLIC_KEY assert ( keyblock->pkt->pkttype == PKT_PUBLIC_KEY
@ -1000,7 +1002,7 @@ get_keyblock_byfprint( KBNODE *ret_keyblock, const byte *fprint,
get_pubkey_end( &ctx ); get_pubkey_end( &ctx );
} }
else else
rc = G10ERR_GENERAL; /* Oops */ rc = GPG_ERR_GENERAL; /* Oops */
return rc; return rc;
} }
@ -1118,7 +1120,7 @@ get_seckey_byfprint( PKT_secret_key *sk,
get_pubkey_end( &ctx ); get_pubkey_end( &ctx );
} }
else else
rc = G10ERR_GENERAL; /* Oops */ rc = GPG_ERR_GENERAL; /* Oops */
return rc; return rc;
} }
@ -1290,12 +1292,12 @@ fixup_uidnode ( KBNODE uidnode, KBNODE signode, u32 keycreated )
p = parse_sig_subpkt ( sig->hashed, SIGSUBPKT_PREF_COMPR, &n ); p = parse_sig_subpkt ( sig->hashed, SIGSUBPKT_PREF_COMPR, &n );
zip = p; nzip = p?n:0; zip = p; nzip = p?n:0;
if (uid->prefs) if (uid->prefs)
m_free (uid->prefs); xfree (uid->prefs);
n = nsym + nhash + nzip; n = nsym + nhash + nzip;
if (!n) if (!n)
uid->prefs = NULL; uid->prefs = NULL;
else { else {
uid->prefs = m_alloc (sizeof (*uid->prefs) * (n+1)); uid->prefs = xmalloc (sizeof (*uid->prefs) * (n+1));
n = 0; n = 0;
for (; nsym; nsym--, n++) { for (; nsym; nsym--, n++) {
uid->prefs[n].type = PREFTYPE_SYM; uid->prefs[n].type = PREFTYPE_SYM;
@ -1364,7 +1366,7 @@ merge_selfsigs_main( KBNODE keyblock, int *r_revoked )
*/ */
/* In case this key was already merged */ /* In case this key was already merged */
m_free(pk->revkey); xfree (pk->revkey);
pk->revkey=NULL; pk->revkey=NULL;
pk->numrevkeys=0; pk->numrevkeys=0;
@ -1400,7 +1402,7 @@ merge_selfsigs_main( KBNODE keyblock, int *r_revoked )
int i; int i;
pk->revkey= pk->revkey=
m_realloc(pk->revkey,sizeof(struct revocation_key)* xrealloc(pk->revkey,sizeof(struct revocation_key)*
(pk->numrevkeys+sig->numrevkeys)); (pk->numrevkeys+sig->numrevkeys));
for(i=0;i<sig->numrevkeys;i++) for(i=0;i<sig->numrevkeys;i++)
@ -1451,7 +1453,7 @@ merge_selfsigs_main( KBNODE keyblock, int *r_revoked )
} }
if(changed) if(changed)
pk->revkey=m_realloc(pk->revkey, pk->revkey=xrealloc(pk->revkey,
pk->numrevkeys*sizeof(struct revocation_key)); pk->numrevkeys*sizeof(struct revocation_key));
} }
@ -1594,7 +1596,7 @@ merge_selfsigs_main( KBNODE keyblock, int *r_revoked )
{ {
PKT_public_key *ultimate_pk; PKT_public_key *ultimate_pk;
ultimate_pk=m_alloc_clear(sizeof(*ultimate_pk)); ultimate_pk=xcalloc (1,sizeof(*ultimate_pk));
/* We don't want to use the full get_pubkey to /* We don't want to use the full get_pubkey to
avoid infinite recursion in certain cases. avoid infinite recursion in certain cases.
@ -1969,7 +1971,7 @@ merge_selfsigs( KBNODE keyblock )
|| k->pkt->pkttype == PKT_PUBLIC_SUBKEY ) { || k->pkt->pkttype == PKT_PUBLIC_SUBKEY ) {
PKT_public_key *pk = k->pkt->pkt.public_key; PKT_public_key *pk = k->pkt->pkt.public_key;
if (pk->prefs) if (pk->prefs)
m_free (pk->prefs); xfree (pk->prefs);
pk->prefs = copy_prefs (prefs); pk->prefs = copy_prefs (prefs);
pk->mdc_feature = mdc_feature; pk->mdc_feature = mdc_feature;
} }
@ -2326,7 +2328,7 @@ lookup( GETKEY_CTX ctx, KBNODE *ret_keyblock, int secmode )
rc = keydb_get_keyblock (ctx->kr_handle, &ctx->keyblock); rc = keydb_get_keyblock (ctx->kr_handle, &ctx->keyblock);
if (rc) { if (rc) {
log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc)); log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
rc = 0; rc = 0;
goto skip; goto skip;
} }
@ -2383,16 +2385,16 @@ lookup( GETKEY_CTX ctx, KBNODE *ret_keyblock, int secmode )
found: found:
if( rc && rc != -1 ) if( rc && rc != -1 )
log_error("keydb_search failed: %s\n", g10_errstr(rc)); log_error("keydb_search failed: %s\n", gpg_strerror (rc));
if( !rc ) { if( !rc ) {
*ret_keyblock = ctx->keyblock; /* return the keyblock */ *ret_keyblock = ctx->keyblock; /* return the keyblock */
ctx->keyblock = NULL; ctx->keyblock = NULL;
} }
else if (rc == -1 && no_suitable_key) else if (rc == -1 && no_suitable_key)
rc = secmode ? G10ERR_UNU_SECKEY : G10ERR_UNU_PUBKEY; rc = secmode ? GPG_ERR_UNUSABLE_SECKEY : GPG_ERR_UNUSABLE_PUBKEY;
else if( rc == -1 ) else if( rc == -1 )
rc = secmode ? G10ERR_NO_SECKEY : G10ERR_NO_PUBKEY; rc = secmode ? GPG_ERR_NO_SECKEY : GPG_ERR_NO_PUBKEY;
if ( secmode ) { if ( secmode ) {
release_kbnode( secblock ); release_kbnode( secblock );
@ -2442,7 +2444,7 @@ enum_secret_keys( void **context, PKT_secret_key *sk,
if( !c ) { /* make a new context */ if( !c ) { /* make a new context */
c = m_alloc_clear( sizeof *c ); c = xcalloc (1, sizeof *c );
*context = c; *context = c;
c->hd = keydb_new (1); c->hd = keydb_new (1);
c->first = 1; c->first = 1;
@ -2453,7 +2455,7 @@ enum_secret_keys( void **context, PKT_secret_key *sk,
if( !sk ) { /* free the context */ if( !sk ) { /* free the context */
keydb_release (c->hd); keydb_release (c->hd);
release_kbnode (c->keyblock); release_kbnode (c->keyblock);
m_free( c ); xfree ( c );
*context = NULL; *context = NULL;
return 0; return 0;
} }
@ -2514,7 +2516,7 @@ get_user_id_string( u32 *keyid )
keyid_list_t a; keyid_list_t a;
for (a=r->keyids; a; a= a->next ) { for (a=r->keyids; a; a= a->next ) {
if( a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1] ) { if( a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1] ) {
p = m_alloc( r->len + 10 ); p = xmalloc ( r->len + 10 );
sprintf(p, "%08lX %.*s", sprintf(p, "%08lX %.*s",
(ulong)keyid[1], r->len, r->name ); (ulong)keyid[1], r->len, r->name );
return p; return p;
@ -2522,7 +2524,7 @@ get_user_id_string( u32 *keyid )
} }
} }
} while( ++pass < 2 && !get_pubkey( NULL, keyid ) ); } while( ++pass < 2 && !get_pubkey( NULL, keyid ) );
p = m_alloc( 15 ); p = xmalloc ( 15 );
sprintf(p, "%08lX [?]", (ulong)keyid[1] ); sprintf(p, "%08lX [?]", (ulong)keyid[1] );
return p; return p;
} }
@ -2533,9 +2535,9 @@ get_user_id_string_printable ( u32 *keyid )
{ {
char *p = get_user_id_string( keyid ); char *p = get_user_id_string( keyid );
char *p2 = utf8_to_native( p, strlen(p), 0 ); char *p2 = utf8_to_native( p, strlen(p), 0 );
m_free(p); xfree (p);
p = make_printable_string (p2, strlen (p2), 0); p = make_printable_string (p2, strlen (p2), 0);
m_free (p2); xfree (p2);
return p; return p;
} }
@ -2552,7 +2554,7 @@ get_long_user_id_string( u32 *keyid )
keyid_list_t a; keyid_list_t a;
for (a=r->keyids; a; a= a->next ) { for (a=r->keyids; a; a= a->next ) {
if( a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1] ) { if( a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1] ) {
p = m_alloc( r->len + 20 ); p = xmalloc ( r->len + 20 );
sprintf(p, "%08lX%08lX %.*s", sprintf(p, "%08lX%08lX %.*s",
(ulong)keyid[0], (ulong)keyid[1], (ulong)keyid[0], (ulong)keyid[1],
r->len, r->name ); r->len, r->name );
@ -2561,7 +2563,7 @@ get_long_user_id_string( u32 *keyid )
} }
} }
} while( ++pass < 2 && !get_pubkey( NULL, keyid ) ); } while( ++pass < 2 && !get_pubkey( NULL, keyid ) );
p = m_alloc( 25 ); p = xmalloc ( 25 );
sprintf(p, "%08lX%08lX [?]", (ulong)keyid[0], (ulong)keyid[1] ); sprintf(p, "%08lX%08lX [?]", (ulong)keyid[0], (ulong)keyid[1] );
return p; return p;
} }
@ -2579,7 +2581,7 @@ get_user_id( u32 *keyid, size_t *rn )
keyid_list_t a; keyid_list_t a;
for (a=r->keyids; a; a= a->next ) { for (a=r->keyids; a; a= a->next ) {
if( a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1] ) { if( a->keyid[0] == keyid[0] && a->keyid[1] == keyid[1] ) {
p = m_alloc( r->len ); p = xmalloc ( r->len );
memcpy(p, r->name, r->len ); memcpy(p, r->name, r->len );
*rn = r->len; *rn = r->len;
return p; return p;
@ -2587,7 +2589,7 @@ get_user_id( u32 *keyid, size_t *rn )
} }
} }
} while( ++pass < 2 && !get_pubkey( NULL, keyid ) ); } while( ++pass < 2 && !get_pubkey( NULL, keyid ) );
p = m_strdup( _("[User id not found]") ); p = xstrdup ( _("[User id not found]") );
*rn = strlen(p); *rn = strlen(p);
return p; return p;
} }
@ -2598,9 +2600,9 @@ get_user_id_printable( u32 *keyid )
size_t rn; size_t rn;
char *p = get_user_id( keyid, &rn ); char *p = get_user_id( keyid, &rn );
char *p2 = utf8_to_native( p, rn, 0 ); char *p2 = utf8_to_native( p, rn, 0 );
m_free(p); xfree (p);
p = make_printable_string (p2, strlen (p2), 0); p = make_printable_string (p2, strlen (p2), 0);
m_free (p2); xfree (p2);
return p; return p;
} }

View File

@ -26,4 +26,6 @@
typedef struct kbnode_struct *KBNODE; typedef struct kbnode_struct *KBNODE;
typedef struct keydb_search_desc KEYDB_SEARCH_DESC; typedef struct keydb_search_desc KEYDB_SEARCH_DESC;
#include "gpg.h"
#endif /*GPG_GLOBAL_H*/ #endif /*GPG_GLOBAL_H*/

View File

@ -20,6 +20,11 @@
#ifndef GNUPG_G10_GPG_H #ifndef GNUPG_G10_GPG_H
#define GNUPG_G10_GPG_H #define GNUPG_G10_GPG_H
/* Note, that this file should be the first one after the system
header files. This is required to set the error source to the
correct value and may be of advantage if we ever have to do
special things. */
#ifdef GPG_ERR_SOURCE_DEFAULT #ifdef GPG_ERR_SOURCE_DEFAULT
#error GPG_ERR_SOURCE_DEFAULT already defined #error GPG_ERR_SOURCE_DEFAULT already defined
#endif #endif

View File

@ -30,6 +30,7 @@
#endif #endif
#define INCLUDED_BY_MAIN_MODULE 1 #define INCLUDED_BY_MAIN_MODULE 1
#include "gpg.h"
#include "packet.h" #include "packet.h"
#include "iobuf.h" #include "iobuf.h"
#include "memory.h" #include "memory.h"
@ -82,8 +83,8 @@ int g10_errors_seen = 0;
RISCOS_GLOBAL_STATICS("GnuPG (gpgv) Heap") RISCOS_GLOBAL_STATICS("GnuPG (gpgv) Heap")
#endif /* __riscos__ */ #endif /* __riscos__ */
const char * static const char *
strusage( int level ) my_strusage( int level )
{ {
const char *p; const char *p;
switch( level ) { switch( level ) {
@ -103,7 +104,7 @@ strusage( int level )
"Check signatures against known trusted keys\n"); "Check signatures against known trusted keys\n");
break; break;
default: p = default_strusage(level); default: p = NULL;
} }
return p; return p;
} }
@ -124,7 +125,7 @@ i18n_init(void)
#else #else
setlocale( LC_ALL, "" ); setlocale( LC_ALL, "" );
#endif #endif
bindtextdomain( PACKAGE, G10_LOCALEDIR ); bindtextdomain( PACKAGE, LOCALEDIR );
textdomain( PACKAGE ); textdomain( PACKAGE );
#endif #endif
#endif #endif
@ -144,7 +145,8 @@ main( int argc, char **argv )
riscos_global_defaults(); riscos_global_defaults();
#endif /* __riscos__ */ #endif /* __riscos__ */
log_set_name("gpgv"); set_strusage (my_strusage);
log_set_prefix ("gpgv", 1);
init_signals(); init_signals();
i18n_init(); i18n_init();
opt.command_fd = -1; /* no command fd */ opt.command_fd = -1; /* no command fd */
@ -177,7 +179,9 @@ main( int argc, char **argv )
opt.verbose++; opt.list_sigs=1; break; opt.verbose++; opt.list_sigs=1; break;
case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break; case oKeyring: append_to_strlist( &nrings, pargs.r.ret_str); break;
case oStatusFD: set_status_fd( pargs.r.ret_int ); break; case oStatusFD: set_status_fd( pargs.r.ret_int ); break;
case oLoggerFD: log_set_logfile( NULL, pargs.r.ret_int ); break; case oLoggerFD:
log_set_fd (iobuf_translate_file_handle (pargs.r.ret_int, 1));
break;
case oHomedir: opt.homedir = pargs.r.ret_str; break; case oHomedir: opt.homedir = pargs.r.ret_str; break;
case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break; case oIgnoreTimeConflict: opt.ignore_time_conflict = 1; break;
default : pargs.err = 2; break; default : pargs.err = 2; break;
@ -200,7 +204,7 @@ main( int argc, char **argv )
FREE_STRLIST(nrings); FREE_STRLIST(nrings);
if( (rc = verify_signatures( argc, argv ) )) if( (rc = verify_signatures( argc, argv ) ))
log_error("verify signatures failed: %s\n", g10_errstr(rc) ); log_error("verify signatures failed: %s\n", gpg_strerror (rc) );
/* cleanup */ /* cleanup */
g10_exit(0); g10_exit(0);
@ -287,19 +291,19 @@ keyserver_import_keyid( u32 *keyid, void *dummy )
int int
get_session_key( PKT_pubkey_enc *k, DEK *dek ) get_session_key( PKT_pubkey_enc *k, DEK *dek )
{ {
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
/* Stub: */ /* Stub: */
int int
get_override_session_key( DEK *dek, const char *string ) get_override_session_key( DEK *dek, const char *string )
{ {
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
/* Stub: */ /* Stub: */
int int
decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek ) decrypt_data( void *procctx, PKT_encrypted *ed, DEK *dek )
{ {
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
@ -317,7 +321,7 @@ display_online_help( const char *keyword )
int int
check_secret_key( PKT_secret_key *sk, int n ) check_secret_key( PKT_secret_key *sk, int n )
{ {
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
/* Stub: /* Stub:
@ -355,26 +359,6 @@ void cipher_decrypt( CIPHER_HANDLE c, byte *outbuf,
byte *inbuf, unsigned nbytes ) {} byte *inbuf, unsigned nbytes ) {}
void cipher_sync( CIPHER_HANDLE c ) {} void cipher_sync( CIPHER_HANDLE c ) {}
/* Stubs to avoid linking to ../cipher/random.c */
void random_dump_stats(void) {}
int quick_random_gen( int onoff ) { return -1;}
void randomize_buffer( byte *buffer, size_t length, int level ) {}
int random_is_faked() { return -1;}
byte *get_random_bits( size_t nbits, int level, int secure ) { return NULL;}
void set_random_seed_file( const char *name ) {}
void update_random_seed_file() {}
void fast_random_poll() {}
/* Stubs to avoid linking of ../cipher/primegen.c */
void register_primegen_progress ( void (*cb)( void *, int), void *cb_data ) {}
MPI generate_secret_prime( unsigned nbits ) { return NULL;}
MPI generate_public_prime( unsigned nbits ) { return NULL;}
MPI generate_elg_prime( int mode, unsigned pbits, unsigned qbits,
MPI g, MPI **ret_factors ) { return NULL;}
/* Do not link to ../cipher/rndlinux.c */
void rndlinux_constructor(void) {}
/* Stubs to avoid linking to ../util/ttyio.c */ /* Stubs to avoid linking to ../util/ttyio.c */
int tty_batchmode( int onoff ) { return 0; } int tty_batchmode( int onoff ) { return 0; }
@ -393,4 +377,4 @@ void disable_dotlock(void) {}
DOTLOCK create_dotlock( const char *file_to_lock ) { return NULL; } DOTLOCK create_dotlock( const char *file_to_lock ) { return NULL; }
int make_dotlock( DOTLOCK h, long timeout ) { return 0;} int make_dotlock( DOTLOCK h, long timeout ) { return 0;}
int release_dotlock( DOTLOCK h ) {return 0;} int release_dotlock( DOTLOCK h ) {return 0;}
void remove_lockfiles(void) {} void dotlock_remove_lockfiles(void) {}

View File

@ -22,6 +22,8 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "ttyio.h" #include "ttyio.h"
#include "main.h" #include "main.h"

View File

@ -56,9 +56,9 @@ struct stats_s {
}; };
static int import( IOBUF inp, const char* fname, static int import( iobuf_t inp, const char* fname,
struct stats_s *stats, unsigned int options ); struct stats_s *stats, unsigned int options );
static int read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root ); static int read_block( iobuf_t a, PACKET **pending_pkt, KBNODE *ret_root );
static void revocation_present(KBNODE keyblock); static void revocation_present(KBNODE keyblock);
static int import_one( const char *fname, KBNODE keyblock, static int import_one( const char *fname, KBNODE keyblock,
struct stats_s *stats, unsigned int options); struct stats_s *stats, unsigned int options);
@ -101,13 +101,13 @@ parse_import_options(char *str,unsigned int *options)
void * void *
import_new_stats_handle (void) import_new_stats_handle (void)
{ {
return m_alloc_clear ( sizeof (struct stats_s) ); return xcalloc (1, sizeof (struct stats_s) );
} }
void void
import_release_stats_handle (void *p) import_release_stats_handle (void *p)
{ {
m_free (p); xfree (p);
} }
/**************** /****************
@ -142,7 +142,7 @@ import_release_stats_handle (void *p)
* *
*/ */
static int static int
import_keys_internal( IOBUF inp, char **fnames, int nnames, import_keys_internal( iobuf_t inp, char **fnames, int nnames,
void *stats_handle, unsigned int options ) void *stats_handle, unsigned int options )
{ {
int i, rc = 0; int i, rc = 0;
@ -160,7 +160,7 @@ import_keys_internal( IOBUF inp, char **fnames, int nnames,
for(i=0; i < nnames; i++ ) { for(i=0; i < nnames; i++ ) {
const char *fname = fnames? fnames[i] : NULL; const char *fname = fnames? fnames[i] : NULL;
IOBUF inp2 = iobuf_open(fname); iobuf_t inp2 = iobuf_open(fname);
if( !fname ) if( !fname )
fname = "[stdin]"; fname = "[stdin]";
if( !inp2 ) if( !inp2 )
@ -170,7 +170,7 @@ import_keys_internal( IOBUF inp, char **fnames, int nnames,
iobuf_close(inp2); iobuf_close(inp2);
if( rc ) if( rc )
log_error("import from `%s' failed: %s\n", fname, log_error("import from `%s' failed: %s\n", fname,
g10_errstr(rc) ); gpg_strerror (rc) );
} }
if( !fname ) if( !fname )
break; break;
@ -204,13 +204,13 @@ import_keys( char **fnames, int nnames,
} }
int int
import_keys_stream( IOBUF inp, void *stats_handle, unsigned int options ) import_keys_stream( iobuf_t inp, void *stats_handle, unsigned int options )
{ {
return import_keys_internal( inp, NULL, 0, stats_handle, options); return import_keys_internal( inp, NULL, 0, stats_handle, options);
} }
static int static int
import( IOBUF inp, const char* fname, import( iobuf_t inp, const char* fname,
struct stats_s *stats, unsigned int options ) struct stats_s *stats, unsigned int options )
{ {
PACKET *pending_pkt = NULL; PACKET *pending_pkt = NULL;
@ -220,7 +220,7 @@ import( IOBUF inp, const char* fname,
getkey_disable_caches(); getkey_disable_caches();
if( !opt.no_armor ) { /* armored reading is not disabled */ if( !opt.no_armor ) { /* armored reading is not disabled */
armor_filter_context_t *afx = m_alloc_clear( sizeof *afx ); armor_filter_context_t *afx = xcalloc (1, sizeof *afx );
afx->only_keyblocks = 1; afx->only_keyblocks = 1;
iobuf_push_filter2( inp, armor_filter, afx, 1 ); iobuf_push_filter2( inp, armor_filter, afx, 1 );
} }
@ -247,8 +247,8 @@ import( IOBUF inp, const char* fname,
} }
if( rc == -1 ) if( rc == -1 )
rc = 0; rc = 0;
else if( rc && rc != G10ERR_INV_KEYRING ) else if( rc && rc != GPG_ERR_INV_KEYRING )
log_error( _("error reading `%s': %s\n"), fname, g10_errstr(rc)); log_error( _("error reading `%s': %s\n"), fname, gpg_strerror (rc));
return rc; return rc;
} }
@ -321,7 +321,7 @@ import_print_stats (void *hd)
* Retunr: 0 = okay, -1 no more blocks or another errorcode. * Retunr: 0 = okay, -1 no more blocks or another errorcode.
*/ */
static int static int
read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root ) read_block( iobuf_t a, PACKET **pending_pkt, KBNODE *ret_root )
{ {
int rc; int rc;
PACKET *pkt; PACKET *pkt;
@ -335,13 +335,13 @@ read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root )
} }
else else
in_cert = 0; in_cert = 0;
pkt = m_alloc( sizeof *pkt ); pkt = xmalloc ( sizeof *pkt );
init_packet(pkt); init_packet(pkt);
while( (rc=parse_packet(a, pkt)) != -1 ) { while( (rc=parse_packet(a, pkt)) != -1 ) {
if( rc ) { /* ignore errors */ if( rc ) { /* ignore errors */
if( rc != G10ERR_UNKNOWN_PACKET ) { if( rc != GPG_ERR_UNKNOWN_PACKET ) {
log_error("read_block: read error: %s\n", g10_errstr(rc) ); log_error("read_block: read error: %s\n", gpg_strerror (rc) );
rc = G10ERR_INV_KEYRING; rc = GPG_ERR_INV_KEYRING;
goto ready; goto ready;
} }
free_packet( pkt ); free_packet( pkt );
@ -363,11 +363,11 @@ read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root )
case PKT_COMPRESSED: case PKT_COMPRESSED:
if( pkt->pkt.compressed->algorithm < 1 if( pkt->pkt.compressed->algorithm < 1
|| pkt->pkt.compressed->algorithm > 2 ) { || pkt->pkt.compressed->algorithm > 2 ) {
rc = G10ERR_COMPR_ALGO; rc = GPG_ERR_COMPR_ALGO;
goto ready; goto ready;
} }
{ {
compress_filter_context_t *cfx = m_alloc_clear( sizeof *cfx ); compress_filter_context_t *cfx = xcalloc (1, sizeof *cfx );
cfx->algo = pkt->pkt.compressed->algorithm; cfx->algo = pkt->pkt.compressed->algorithm;
pkt->pkt.compressed->buf = NULL; pkt->pkt.compressed->buf = NULL;
iobuf_push_filter2( a, compress_filter, cfx, 1 ); iobuf_push_filter2( a, compress_filter, cfx, 1 );
@ -396,7 +396,7 @@ read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root )
root = new_kbnode( pkt ); root = new_kbnode( pkt );
else else
add_kbnode( root, new_kbnode( pkt ) ); add_kbnode( root, new_kbnode( pkt ) );
pkt = m_alloc( sizeof *pkt ); pkt = xmalloc ( sizeof *pkt );
} }
init_packet(pkt); init_packet(pkt);
break; break;
@ -411,7 +411,7 @@ read_block( IOBUF a, PACKET **pending_pkt, KBNODE *ret_root )
else else
*ret_root = root; *ret_root = root;
free_packet( pkt ); free_packet( pkt );
m_free( pkt ); xfree ( pkt );
return rc; return rc;
} }
@ -513,7 +513,7 @@ print_import_check (PKT_public_key * pk, PKT_user_id * id)
u32 keyid[2]; u32 keyid[2];
size_t i, pos = 0, n; size_t i, pos = 0, n;
buf = m_alloc (17+41+id->len+32); buf = xmalloc (17+41+id->len+32);
keyid_from_pk (pk, keyid); keyid_from_pk (pk, keyid);
sprintf (buf, "%08X%08X ", keyid[0], keyid[1]); sprintf (buf, "%08X%08X ", keyid[0], keyid[1]);
pos = 17; pos = 17;
@ -524,7 +524,7 @@ print_import_check (PKT_public_key * pk, PKT_user_id * id)
pos += 1; pos += 1;
strcat (buf, id->name); strcat (buf, id->name);
write_status_text (STATUS_IMPORT_CHECK, buf); write_status_text (STATUS_IMPORT_CHECK, buf);
m_free (buf); xfree (buf);
} }
/**************** /****************
@ -607,7 +607,7 @@ import_one( const char *fname, KBNODE keyblock,
node->flag |= 1; node->flag |= 1;
log_info( _("key %08lX: accepted non self-signed user ID '%s'\n"), log_info( _("key %08lX: accepted non self-signed user ID '%s'\n"),
(ulong)keyid[1],user); (ulong)keyid[1],user);
m_free(user); xfree (user);
} }
if( !delete_inv_parts( fname, keyblock, keyid, options ) ) { if( !delete_inv_parts( fname, keyblock, keyid, options ) ) {
@ -621,11 +621,12 @@ import_one( const char *fname, KBNODE keyblock,
} }
/* do we have this key already in one of our pubrings ? */ /* do we have this key already in one of our pubrings ? */
pk_orig = m_alloc_clear( sizeof *pk_orig ); pk_orig = xcalloc (1, sizeof *pk_orig );
rc = get_pubkey_fast ( pk_orig, keyid ); rc = get_pubkey_fast ( pk_orig, keyid );
if( rc && rc != G10ERR_NO_PUBKEY && rc != G10ERR_UNU_PUBKEY ) { if( rc && gpg_err_code (rc) != GPG_ERR_NO_PUBKEY
&& gpg_err_code (rc) != GPG_ERR_UNUSABLE_PUBKEY ) {
log_error( _("key %08lX: public key not found: %s\n"), log_error( _("key %08lX: public key not found: %s\n"),
(ulong)keyid[1], g10_errstr(rc)); (ulong)keyid[1], gpg_strerror (rc));
} }
else if ( rc && opt.merge_only ) { else if ( rc && opt.merge_only ) {
if( opt.verbose ) if( opt.verbose )
@ -638,16 +639,16 @@ import_one( const char *fname, KBNODE keyblock,
rc = keydb_locate_writable (hd, NULL); rc = keydb_locate_writable (hd, NULL);
if (rc) { if (rc) {
log_error (_("no writable keyring found: %s\n"), g10_errstr (rc)); log_error (_("no writable keyring found: %s\n"), gpg_strerror (rc));
keydb_release (hd); keydb_release (hd);
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
if( opt.verbose > 1 ) if( opt.verbose > 1 )
log_info (_("writing to `%s'\n"), keydb_get_resource_name (hd) ); log_info (_("writing to `%s'\n"), keydb_get_resource_name (hd) );
rc = keydb_insert_keyblock (hd, keyblock ); rc = keydb_insert_keyblock (hd, keyblock );
if (rc) if (rc)
log_error (_("error writing keyring `%s': %s\n"), log_error (_("error writing keyring `%s': %s\n"),
keydb_get_resource_name (hd), g10_errstr(rc)); keydb_get_resource_name (hd), gpg_strerror (rc));
else else
{ {
/* This should not be possible since we delete the /* This should not be possible since we delete the
@ -666,12 +667,12 @@ import_one( const char *fname, KBNODE keyblock,
char *p=get_user_id_printable (keyid); char *p=get_user_id_printable (keyid);
log_info( _("key %08lX: public key \"%s\" imported\n"), log_info( _("key %08lX: public key \"%s\" imported\n"),
(ulong)keyid[1],p); (ulong)keyid[1],p);
m_free(p); xfree (p);
} }
if( is_status_enabled() ) { if( is_status_enabled() ) {
char *us = get_long_user_id_string( keyid ); char *us = get_long_user_id_string( keyid );
write_status_text( STATUS_IMPORTED, us ); write_status_text( STATUS_IMPORTED, us );
m_free(us); xfree (us);
print_import_ok (pk,NULL, 1); print_import_ok (pk,NULL, 1);
} }
stats->imported++; stats->imported++;
@ -704,14 +705,14 @@ import_one( const char *fname, KBNODE keyblock,
} }
if( rc ) { if( rc ) {
log_error (_("key %08lX: can't locate original keyblock: %s\n"), log_error (_("key %08lX: can't locate original keyblock: %s\n"),
(ulong)keyid[1], g10_errstr(rc)); (ulong)keyid[1], gpg_strerror (rc));
keydb_release (hd); keydb_release (hd);
goto leave; goto leave;
} }
rc = keydb_get_keyblock (hd, &keyblock_orig ); rc = keydb_get_keyblock (hd, &keyblock_orig );
if (rc) { if (rc) {
log_error (_("key %08lX: can't read original keyblock: %s\n"), log_error (_("key %08lX: can't read original keyblock: %s\n"),
(ulong)keyid[1], g10_errstr(rc)); (ulong)keyid[1], gpg_strerror (rc));
keydb_release (hd); keydb_release (hd);
goto leave; goto leave;
} }
@ -733,7 +734,7 @@ import_one( const char *fname, KBNODE keyblock,
rc = keydb_update_keyblock (hd, keyblock_orig); rc = keydb_update_keyblock (hd, keyblock_orig);
if (rc) if (rc)
log_error (_("error writing keyring `%s': %s\n"), log_error (_("error writing keyring `%s': %s\n"),
keydb_get_resource_name (hd), g10_errstr(rc) ); keydb_get_resource_name (hd), gpg_strerror (rc) );
else if(non_self) else if(non_self)
revalidation_mark (); revalidation_mark ();
@ -758,7 +759,7 @@ import_one( const char *fname, KBNODE keyblock,
else if( n_subk ) else if( n_subk )
log_info( _("key %08lX: \"%s\" %d new subkeys\n"), log_info( _("key %08lX: \"%s\" %d new subkeys\n"),
(ulong)keyid[1], p, n_subk ); (ulong)keyid[1], p, n_subk );
m_free(p); xfree (p);
} }
stats->n_uids +=n_uids; stats->n_uids +=n_uids;
@ -777,7 +778,7 @@ import_one( const char *fname, KBNODE keyblock,
char *p=get_user_id_printable(keyid); char *p=get_user_id_printable(keyid);
log_info( _("key %08lX: \"%s\" not changed\n"), log_info( _("key %08lX: \"%s\" not changed\n"),
(ulong)keyid[1],p); (ulong)keyid[1],p);
m_free(p); xfree (p);
} }
stats->unchanged++; stats->unchanged++;
} }
@ -810,8 +811,8 @@ sec_to_pub_keyblock(KBNODE sec_keyblock)
write the keyblock out. */ write the keyblock out. */
PKT_secret_key *sk=secnode->pkt->pkt.secret_key; PKT_secret_key *sk=secnode->pkt->pkt.secret_key;
PACKET *pkt=m_alloc_clear(sizeof(PACKET)); PACKET *pkt=xcalloc (1,sizeof(PACKET));
PKT_public_key *pk=m_alloc_clear(sizeof(PKT_public_key)); PKT_public_key *pk=xcalloc (1,sizeof(PKT_public_key));
int n; int n;
if(secnode->pkt->pkttype==PKT_SECRET_KEY) if(secnode->pkt->pkttype==PKT_SECRET_KEY)
@ -905,20 +906,20 @@ import_secret_one( const char *fname, KBNODE keyblock,
/* do we have this key already in one of our secrings ? */ /* do we have this key already in one of our secrings ? */
rc = seckey_available( keyid ); rc = seckey_available( keyid );
if( rc == G10ERR_NO_SECKEY && !opt.merge_only ) { /* simply insert this key */ if( rc == GPG_ERR_NO_SECKEY && !opt.merge_only ) { /* simply insert this key */
KEYDB_HANDLE hd = keydb_new (1); KEYDB_HANDLE hd = keydb_new (1);
/* get default resource */ /* get default resource */
rc = keydb_locate_writable (hd, NULL); rc = keydb_locate_writable (hd, NULL);
if (rc) { if (rc) {
log_error (_("no default secret keyring: %s\n"), g10_errstr (rc)); log_error (_("no default secret keyring: %s\n"), gpg_strerror (rc));
keydb_release (hd); keydb_release (hd);
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
rc = keydb_insert_keyblock (hd, keyblock ); rc = keydb_insert_keyblock (hd, keyblock );
if (rc) if (rc)
log_error (_("error writing keyring `%s': %s\n"), log_error (_("error writing keyring `%s': %s\n"),
keydb_get_resource_name (hd), g10_errstr(rc) ); keydb_get_resource_name (hd), gpg_strerror (rc) );
keydb_release (hd); keydb_release (hd);
/* we are ready */ /* we are ready */
if( !opt.quiet ) if( !opt.quiet )
@ -949,7 +950,7 @@ import_secret_one( const char *fname, KBNODE keyblock,
} }
else else
log_error( _("key %08lX: secret key not found: %s\n"), log_error( _("key %08lX: secret key not found: %s\n"),
(ulong)keyid[1], g10_errstr(rc)); (ulong)keyid[1], gpg_strerror (rc));
return rc; return rc;
} }
@ -974,9 +975,9 @@ import_revoke_cert( const char *fname, KBNODE node, struct stats_s *stats )
keyid[0] = node->pkt->pkt.signature->keyid[0]; keyid[0] = node->pkt->pkt.signature->keyid[0];
keyid[1] = node->pkt->pkt.signature->keyid[1]; keyid[1] = node->pkt->pkt.signature->keyid[1];
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
rc = get_pubkey( pk, keyid ); rc = get_pubkey( pk, keyid );
if( rc == G10ERR_NO_PUBKEY ) { if( rc == GPG_ERR_NO_PUBKEY ) {
log_info( _("key %08lX: no public key - " log_info( _("key %08lX: no public key - "
"can't apply revocation certificate\n"), (ulong)keyid[1]); "can't apply revocation certificate\n"), (ulong)keyid[1]);
rc = 0; rc = 0;
@ -984,7 +985,7 @@ import_revoke_cert( const char *fname, KBNODE node, struct stats_s *stats )
} }
else if( rc ) { else if( rc ) {
log_error( _("key %08lX: public key not found: %s\n"), log_error( _("key %08lX: public key not found: %s\n"),
(ulong)keyid[1], g10_errstr(rc)); (ulong)keyid[1], gpg_strerror (rc));
goto leave; goto leave;
} }
@ -1001,13 +1002,13 @@ import_revoke_cert( const char *fname, KBNODE node, struct stats_s *stats )
} }
if (rc) { if (rc) {
log_error (_("key %08lX: can't locate original keyblock: %s\n"), log_error (_("key %08lX: can't locate original keyblock: %s\n"),
(ulong)keyid[1], g10_errstr(rc)); (ulong)keyid[1], gpg_strerror (rc));
goto leave; goto leave;
} }
rc = keydb_get_keyblock (hd, &keyblock ); rc = keydb_get_keyblock (hd, &keyblock );
if (rc) { if (rc) {
log_error (_("key %08lX: can't read original keyblock: %s\n"), log_error (_("key %08lX: can't read original keyblock: %s\n"),
(ulong)keyid[1], g10_errstr(rc)); (ulong)keyid[1], gpg_strerror (rc));
goto leave; goto leave;
} }
@ -1018,7 +1019,7 @@ import_revoke_cert( const char *fname, KBNODE node, struct stats_s *stats )
rc = check_key_signature( keyblock, node, NULL); rc = check_key_signature( keyblock, node, NULL);
if( rc ) { if( rc ) {
log_error( _("key %08lX: invalid revocation certificate" log_error( _("key %08lX: invalid revocation certificate"
": %s - rejected\n"), (ulong)keyid[1], g10_errstr(rc)); ": %s - rejected\n"), (ulong)keyid[1], gpg_strerror (rc));
goto leave; goto leave;
} }
@ -1044,14 +1045,14 @@ import_revoke_cert( const char *fname, KBNODE node, struct stats_s *stats )
rc = keydb_update_keyblock (hd, keyblock ); rc = keydb_update_keyblock (hd, keyblock );
if (rc) if (rc)
log_error (_("error writing keyring `%s': %s\n"), log_error (_("error writing keyring `%s': %s\n"),
keydb_get_resource_name (hd), g10_errstr(rc) ); keydb_get_resource_name (hd), gpg_strerror (rc) );
keydb_release (hd); hd = NULL; keydb_release (hd); hd = NULL;
/* we are ready */ /* we are ready */
if( !opt.quiet ) { if( !opt.quiet ) {
char *p=get_user_id_printable (keyid); char *p=get_user_id_printable (keyid);
log_info( _("key %08lX: \"%s\" revocation certificate imported\n"), log_info( _("key %08lX: \"%s\" revocation certificate imported\n"),
(ulong)keyid[1],p); (ulong)keyid[1],p);
m_free(p); xfree (p);
} }
stats->n_revoc++; stats->n_revoc++;
@ -1125,13 +1126,13 @@ chk_self_sigs( const char *fname, KBNODE keyblock,
{ {
char *p=utf8_to_native(unode->pkt->pkt.user_id->name, char *p=utf8_to_native(unode->pkt->pkt.user_id->name,
strlen(unode->pkt->pkt.user_id->name),0); strlen(unode->pkt->pkt.user_id->name),0);
log_info( rc == G10ERR_PUBKEY_ALGO ? log_info( rc == GPG_ERR_PUBKEY_ALGO ?
_("key %08lX: unsupported public key " _("key %08lX: unsupported public key "
"algorithm on user id \"%s\"\n"): "algorithm on user id \"%s\"\n"):
_("key %08lX: invalid self-signature " _("key %08lX: invalid self-signature "
"on user id \"%s\"\n"), "on user id \"%s\"\n"),
(ulong)keyid[1],p); (ulong)keyid[1],p);
m_free(p); xfree (p);
} }
else else
unode->flag |= 1; /* mark that signature checked */ unode->flag |= 1; /* mark that signature checked */
@ -1150,7 +1151,7 @@ chk_self_sigs( const char *fname, KBNODE keyblock,
else { else {
rc = check_key_signature( keyblock, n, NULL); rc = check_key_signature( keyblock, n, NULL);
if( rc ) { if( rc ) {
log_info( rc == G10ERR_PUBKEY_ALGO ? log_info( rc == GPG_ERR_PUBKEY_ALGO ?
_("key %08lX: unsupported public key algorithm\n"): _("key %08lX: unsupported public key algorithm\n"):
_("key %08lX: invalid subkey binding\n"), _("key %08lX: invalid subkey binding\n"),
(ulong)keyid[1]); (ulong)keyid[1]);
@ -1191,7 +1192,7 @@ chk_self_sigs( const char *fname, KBNODE keyblock,
else { else {
rc = check_key_signature( keyblock, n, NULL); rc = check_key_signature( keyblock, n, NULL);
if( rc ) { if( rc ) {
log_info( rc == G10ERR_PUBKEY_ALGO ? log_info( rc == GPG_ERR_PUBKEY_ALGO ?
_("key %08lX: unsupported public key algorithm\n"): _("key %08lX: unsupported public key algorithm\n"):
_("key %08lX: invalid subkey revocation\n"), _("key %08lX: invalid subkey revocation\n"),
(ulong)keyid[1]); (ulong)keyid[1]);
@ -1281,7 +1282,8 @@ delete_inv_parts( const char *fname, KBNODE keyblock,
subkey_seen = 1; subkey_seen = 1;
} }
else if( node->pkt->pkttype == PKT_SIGNATURE else if( node->pkt->pkttype == PKT_SIGNATURE
&& check_pubkey_algo( node->pkt->pkt.signature->pubkey_algo) && openpgp_pk_test_algo( node->pkt->pkt.signature
->pubkey_algo, 0)
&& node->pkt->pkt.signature->pubkey_algo != PUBKEY_ALGO_RSA ) && node->pkt->pkt.signature->pubkey_algo != PUBKEY_ALGO_RSA )
delete_kbnode( node ); /* build_packet() can't handle this */ delete_kbnode( node ); /* build_packet() can't handle this */
else if( node->pkt->pkttype == PKT_SIGNATURE && else if( node->pkt->pkttype == PKT_SIGNATURE &&
@ -1320,7 +1322,7 @@ delete_inv_parts( const char *fname, KBNODE keyblock,
{ {
log_error( _("key %08lX: invalid revocation " log_error( _("key %08lX: invalid revocation "
"certificate: %s - skipped\n"), "certificate: %s - skipped\n"),
(ulong)keyid[1], g10_errstr(rc)); (ulong)keyid[1], gpg_strerror (rc));
delete_kbnode( node ); delete_kbnode( node );
} }
} }
@ -1489,7 +1491,8 @@ revocation_present(KBNODE keyblock)
rc=get_pubkey_byfprint_fast (NULL,sig->revkey[idx]->fpr, rc=get_pubkey_byfprint_fast (NULL,sig->revkey[idx]->fpr,
MAX_FINGERPRINT_LEN); MAX_FINGERPRINT_LEN);
if(rc==G10ERR_NO_PUBKEY || rc==G10ERR_UNU_PUBKEY) if ( gpg_err_code (rc) == GPG_ERR_NO_PUBKEY
|| gpg_err_code (rc) == GPG_ERR_UNUSABLE_PUBKEY)
{ {
/* No, so try and get it */ /* No, so try and get it */
if(opt.keyserver_scheme && if(opt.keyserver_scheme &&
@ -1508,7 +1511,8 @@ revocation_present(KBNODE keyblock)
MAX_FINGERPRINT_LEN); MAX_FINGERPRINT_LEN);
} }
if(rc==G10ERR_NO_PUBKEY || rc==G10ERR_UNU_PUBKEY) if (gpg_err_code (rc) == GPG_ERR_NO_PUBKEY
|| gpg_err_code (rc) == GPG_ERR_UNUSABLE_PUBKEY)
log_info(_("WARNING: key %08lX may be revoked: " log_info(_("WARNING: key %08lX may be revoked: "
"revocation key %08lX not present.\n"), "revocation key %08lX not present.\n"),
(ulong)keyid_from_pk(pk,NULL), (ulong)keyid_from_pk(pk,NULL),
@ -1568,7 +1572,7 @@ merge_blocks( const char *fname, KBNODE keyblock_orig, KBNODE keyblock,
++*n_sigs; ++*n_sigs;
log_info(_("key %08lX: \"%s\" revocation certificate added\n"), log_info(_("key %08lX: \"%s\" revocation certificate added\n"),
(ulong)keyid[1],p); (ulong)keyid[1],p);
m_free(p); xfree (p);
} }
} }
} }

View File

@ -23,6 +23,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "memory.h" #include "memory.h"
#include "packet.h" #include "packet.h"
@ -41,7 +43,7 @@ alloc_node(void)
if( n ) if( n )
unused_nodes = n->next; unused_nodes = n->next;
else else
n = m_alloc( sizeof *n ); n = xmalloc ( sizeof *n );
n->next = NULL; n->next = NULL;
n->pkt = NULL; n->pkt = NULL;
n->flag = 0; n->flag = 0;
@ -58,7 +60,7 @@ free_node( KBNODE n )
n->next = unused_nodes; n->next = unused_nodes;
unused_nodes = n; unused_nodes = n;
#else #else
m_free( n ); xfree ( n );
#endif #endif
} }
} }
@ -94,7 +96,7 @@ release_kbnode( KBNODE n )
n2 = n->next; n2 = n->next;
if( !is_cloned_kbnode(n) ) { if( !is_cloned_kbnode(n) ) {
free_packet( n->pkt ); free_packet( n->pkt );
m_free( n->pkt ); xfree ( n->pkt );
} }
free_node( n ); free_node( n );
n = n2; n = n2;
@ -267,7 +269,7 @@ commit_kbnode( KBNODE *root )
nl->next = n->next; nl->next = n->next;
if( !is_cloned_kbnode(n) ) { if( !is_cloned_kbnode(n) ) {
free_packet( n->pkt ); free_packet( n->pkt );
m_free( n->pkt ); xfree ( n->pkt );
} }
free_node( n ); free_node( n );
changed = 1; changed = 1;
@ -291,7 +293,7 @@ remove_kbnode( KBNODE *root, KBNODE node )
nl->next = n->next; nl->next = n->next;
if( !is_cloned_kbnode(n) ) { if( !is_cloned_kbnode(n) ) {
free_packet( n->pkt ); free_packet( n->pkt );
m_free( n->pkt ); xfree ( n->pkt );
} }
free_node( n ); free_node( n );
} }

View File

@ -28,6 +28,7 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "options.h" #include "options.h"
#include "main.h" /*try_make_homedir ()*/ #include "main.h" /*try_make_homedir ()*/
@ -84,7 +85,7 @@ keydb_add_resource (const char *url, int flags, int secret)
{ {
static int any_secret, any_public; static int any_secret, any_public;
const char *resname = url; const char *resname = url;
IOBUF iobuf = NULL; iobuf_t iobuf = NULL;
char *filename = NULL; char *filename = NULL;
int force=(flags&1); int force=(flags&1);
int rc = 0; int rc = 0;
@ -103,7 +104,7 @@ keydb_add_resource (const char *url, int flags, int secret)
#if !defined(HAVE_DRIVE_LETTERS) && !defined(__riscos__) #if !defined(HAVE_DRIVE_LETTERS) && !defined(__riscos__)
else if (strchr (resname, ':')) { else if (strchr (resname, ':')) {
log_error ("invalid key resource URL `%s'\n", url ); log_error ("invalid key resource URL `%s'\n", url );
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
goto leave; goto leave;
} }
#endif /* !HAVE_DRIVE_LETTERS && !__riscos__ */ #endif /* !HAVE_DRIVE_LETTERS && !__riscos__ */
@ -116,7 +117,7 @@ keydb_add_resource (const char *url, int flags, int secret)
filename = make_filename (opt.homedir, resname, NULL); filename = make_filename (opt.homedir, resname, NULL);
} }
else else
filename = m_strdup (resname); filename = xstrdup (resname);
if (!force) if (!force)
force = secret? !any_secret : !any_public; force = secret? !any_secret : !any_public;
@ -145,7 +146,7 @@ keydb_add_resource (const char *url, int flags, int secret)
switch (rt) { switch (rt) {
case KEYDB_RESOURCE_TYPE_NONE: case KEYDB_RESOURCE_TYPE_NONE:
log_error ("unknown type of key resource `%s'\n", url ); log_error ("unknown type of key resource `%s'\n", url );
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
goto leave; goto leave;
case KEYDB_RESOURCE_TYPE_KEYRING: case KEYDB_RESOURCE_TYPE_KEYRING:
@ -156,7 +157,7 @@ keydb_add_resource (const char *url, int flags, int secret)
if (!force) if (!force)
{ {
rc = G10ERR_OPEN_FILE; rc = gpg_error_from_errno (errno);
goto leave; goto leave;
} }
@ -174,7 +175,7 @@ keydb_add_resource (const char *url, int flags, int secret)
} }
if (access (filename, F_OK)) if (access (filename, F_OK))
{ {
rc = G10ERR_OPEN_FILE; rc = gpg_error_from_errno (errno);
*last_slash_in_filename = DIRSEP_C; *last_slash_in_filename = DIRSEP_C;
goto leave; goto leave;
} }
@ -188,7 +189,7 @@ keydb_add_resource (const char *url, int flags, int secret)
{ {
log_error ( _("error creating keyring `%s': %s\n"), log_error ( _("error creating keyring `%s': %s\n"),
filename, strerror(errno)); filename, strerror(errno));
rc = G10ERR_OPEN_FILE; rc = gpg_error_from_errno (errno);
goto leave; goto leave;
} }
@ -203,7 +204,7 @@ keydb_add_resource (const char *url, int flags, int secret)
if(keyring_register_filename (filename, secret, &token)) if(keyring_register_filename (filename, secret, &token))
{ {
if (used_resources >= MAX_KEYDB_RESOURCES) if (used_resources >= MAX_KEYDB_RESOURCES)
rc = G10ERR_RESOURCE_LIMIT; rc = GPG_ERR_RESOURCE_LIMIT;
else else
{ {
if(flags&2) if(flags&2)
@ -227,7 +228,7 @@ keydb_add_resource (const char *url, int flags, int secret)
default: default:
log_error ("resource type of `%s' not supported\n", url); log_error ("resource type of `%s' not supported\n", url);
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
goto leave; goto leave;
} }
@ -235,12 +236,12 @@ keydb_add_resource (const char *url, int flags, int secret)
leave: leave:
if (rc) if (rc)
log_error ("keyblock resource `%s': %s\n", filename, g10_errstr(rc)); log_error ("keyblock resource `%s': %s\n", filename, gpg_strerror (rc));
else if (secret) else if (secret)
any_secret = 1; any_secret = 1;
else else
any_public = 1; any_public = 1;
m_free (filename); xfree (filename);
return rc; return rc;
} }
@ -253,7 +254,7 @@ keydb_new (int secret)
KEYDB_HANDLE hd; KEYDB_HANDLE hd;
int i, j; int i, j;
hd = m_alloc_clear (sizeof *hd); hd = xcalloc (1,sizeof *hd);
hd->found = -1; hd->found = -1;
assert (used_resources <= MAX_KEYDB_RESOURCES); assert (used_resources <= MAX_KEYDB_RESOURCES);
@ -271,7 +272,7 @@ keydb_new (int secret)
hd->active[j].secret = all_resources[i].secret; hd->active[j].secret = all_resources[i].secret;
hd->active[j].u.kr = keyring_new (all_resources[i].token, secret); hd->active[j].u.kr = keyring_new (all_resources[i].token, secret);
if (!hd->active[j].u.kr) { if (!hd->active[j].u.kr) {
m_free (hd); xfree (hd);
return NULL; /* fixme: release all previously allocated handles*/ return NULL; /* fixme: release all previously allocated handles*/
} }
j++; j++;
@ -305,7 +306,7 @@ keydb_release (KEYDB_HANDLE hd)
} }
} }
m_free (hd); xfree (hd);
} }
@ -413,14 +414,14 @@ keydb_get_keyblock (KEYDB_HANDLE hd, KBNODE *ret_kb)
int rc = 0; int rc = 0;
if (!hd) if (!hd)
return G10ERR_INV_ARG; return GPG_ERR_INV_ARG;
if ( hd->found < 0 || hd->found >= hd->used) if ( hd->found < 0 || hd->found >= hd->used)
return -1; /* nothing found */ return -1; /* nothing found */
switch (hd->active[hd->found].type) { switch (hd->active[hd->found].type) {
case KEYDB_RESOURCE_TYPE_NONE: case KEYDB_RESOURCE_TYPE_NONE:
rc = G10ERR_GENERAL; /* oops */ rc = GPG_ERR_GENERAL; /* oops */
break; break;
case KEYDB_RESOURCE_TYPE_KEYRING: case KEYDB_RESOURCE_TYPE_KEYRING:
rc = keyring_get_keyblock (hd->active[hd->found].u.kr, ret_kb); rc = keyring_get_keyblock (hd->active[hd->found].u.kr, ret_kb);
@ -439,7 +440,7 @@ keydb_update_keyblock (KEYDB_HANDLE hd, KBNODE kb)
int rc = 0; int rc = 0;
if (!hd) if (!hd)
return G10ERR_INV_ARG; return GPG_ERR_INV_ARG;
if ( hd->found < 0 || hd->found >= hd->used) if ( hd->found < 0 || hd->found >= hd->used)
return -1; /* nothing found */ return -1; /* nothing found */
@ -453,7 +454,7 @@ keydb_update_keyblock (KEYDB_HANDLE hd, KBNODE kb)
switch (hd->active[hd->found].type) { switch (hd->active[hd->found].type) {
case KEYDB_RESOURCE_TYPE_NONE: case KEYDB_RESOURCE_TYPE_NONE:
rc = G10ERR_GENERAL; /* oops */ rc = GPG_ERR_GENERAL; /* oops */
break; break;
case KEYDB_RESOURCE_TYPE_KEYRING: case KEYDB_RESOURCE_TYPE_KEYRING:
rc = keyring_update_keyblock (hd->active[hd->found].u.kr, kb); rc = keyring_update_keyblock (hd->active[hd->found].u.kr, kb);
@ -475,7 +476,7 @@ keydb_insert_keyblock (KEYDB_HANDLE hd, KBNODE kb)
int idx; int idx;
if (!hd) if (!hd)
return G10ERR_INV_ARG; return GPG_ERR_INV_ARG;
if( opt.dry_run ) if( opt.dry_run )
return 0; return 0;
@ -485,7 +486,7 @@ keydb_insert_keyblock (KEYDB_HANDLE hd, KBNODE kb)
else if ( hd->current >= 0 && hd->current < hd->used) else if ( hd->current >= 0 && hd->current < hd->used)
idx = hd->current; idx = hd->current;
else else
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
rc = lock_all (hd); rc = lock_all (hd);
if (rc) if (rc)
@ -493,7 +494,7 @@ keydb_insert_keyblock (KEYDB_HANDLE hd, KBNODE kb)
switch (hd->active[idx].type) { switch (hd->active[idx].type) {
case KEYDB_RESOURCE_TYPE_NONE: case KEYDB_RESOURCE_TYPE_NONE:
rc = G10ERR_GENERAL; /* oops */ rc = GPG_ERR_GENERAL; /* oops */
break; break;
case KEYDB_RESOURCE_TYPE_KEYRING: case KEYDB_RESOURCE_TYPE_KEYRING:
rc = keyring_insert_keyblock (hd->active[idx].u.kr, kb); rc = keyring_insert_keyblock (hd->active[idx].u.kr, kb);
@ -514,7 +515,7 @@ keydb_delete_keyblock (KEYDB_HANDLE hd)
int rc = -1; int rc = -1;
if (!hd) if (!hd)
return G10ERR_INV_ARG; return GPG_ERR_INV_ARG;
if ( hd->found < 0 || hd->found >= hd->used) if ( hd->found < 0 || hd->found >= hd->used)
return -1; /* nothing found */ return -1; /* nothing found */
@ -528,7 +529,7 @@ keydb_delete_keyblock (KEYDB_HANDLE hd)
switch (hd->active[hd->found].type) { switch (hd->active[hd->found].type) {
case KEYDB_RESOURCE_TYPE_NONE: case KEYDB_RESOURCE_TYPE_NONE:
rc = G10ERR_GENERAL; /* oops */ rc = GPG_ERR_GENERAL; /* oops */
break; break;
case KEYDB_RESOURCE_TYPE_KEYRING: case KEYDB_RESOURCE_TYPE_KEYRING:
rc = keyring_delete_keyblock (hd->active[hd->found].u.kr); rc = keyring_delete_keyblock (hd->active[hd->found].u.kr);
@ -551,7 +552,7 @@ keydb_locate_writable (KEYDB_HANDLE hd, const char *reserved)
int rc; int rc;
if (!hd) if (!hd)
return G10ERR_INV_ARG; return GPG_ERR_INV_ARG;
rc = keydb_search_reset (hd); /* this does reset hd->current */ rc = keydb_search_reset (hd); /* this does reset hd->current */
if (rc) if (rc)
@ -613,7 +614,7 @@ keydb_rebuild_caches (void)
rc = keyring_rebuild_cache (all_resources[i].token); rc = keyring_rebuild_cache (all_resources[i].token);
if (rc) if (rc)
log_error (_("failed to rebuild keyring cache: %s\n"), log_error (_("failed to rebuild keyring cache: %s\n"),
g10_errstr (rc)); gpg_strerror (rc));
break; break;
} }
} }
@ -630,7 +631,7 @@ keydb_search_reset (KEYDB_HANDLE hd)
int i, rc = 0; int i, rc = 0;
if (!hd) if (!hd)
return G10ERR_INV_ARG; return GPG_ERR_INV_ARG;
hd->current = 0; hd->current = 0;
hd->found = -1; hd->found = -1;
@ -659,7 +660,7 @@ keydb_search2 (KEYDB_HANDLE hd, KEYDB_SEARCH_DESC *desc,
int rc = -1; int rc = -1;
if (!hd) if (!hd)
return G10ERR_INV_ARG; return GPG_ERR_INV_ARG;
while (rc == -1 && hd->current >= 0 && hd->current < hd->used) { while (rc == -1 && hd->current >= 0 && hd->current < hd->used) {
switch (hd->active[hd->current].type) { switch (hd->active[hd->current].type) {

View File

@ -75,7 +75,7 @@ struct keyblock_pos_struct {
enum resource_type rt; enum resource_type rt;
off_t offset; /* position information */ off_t offset; /* position information */
unsigned count; /* length of the keyblock in packets */ unsigned count; /* length of the keyblock in packets */
IOBUF fp; /* used by enum_keyblocks */ iobuf_t fp; /* used by enum_keyblocks */
int secret; /* working on a secret keyring */ int secret; /* working on a secret keyring */
PACKET *pkt; /* ditto */ PACKET *pkt; /* ditto */
int valid; int valid;
@ -235,6 +235,7 @@ KEYDB_HANDLE get_ctx_handle(GETKEY_CTX ctx);
/*-- keyid.c --*/ /*-- keyid.c --*/
int pubkey_letter( int algo ); int pubkey_letter( int algo );
u32 v3_keyid (gcry_mpi_t a, u32 *ki);
u32 keyid_from_sk( PKT_secret_key *sk, u32 *keyid ); u32 keyid_from_sk( PKT_secret_key *sk, u32 *keyid );
u32 keyid_from_pk( PKT_public_key *pk, u32 *keyid ); u32 keyid_from_pk( PKT_public_key *pk, u32 *keyid );
u32 keyid_from_sig( PKT_signature *sig, u32 *keyid ); u32 keyid_from_sig( PKT_signature *sig, u32 *keyid );

View File

@ -27,6 +27,7 @@
#include <assert.h> #include <assert.h>
#include <ctype.h> #include <ctype.h>
#include "gpg.h"
#include "options.h" #include "options.h"
#include "packet.h" #include "packet.h"
#include "errors.h" #include "errors.h"
@ -107,19 +108,20 @@ print_and_check_one_sig( KBNODE keyblock, KBNODE node,
/* TODO: Make sure a cached sig record here still has the pk that /* TODO: Make sure a cached sig record here still has the pk that
issued it. See also keylist.c:list_keyblock_print */ issued it. See also keylist.c:list_keyblock_print */
switch( (rc = check_key_signature( keyblock, node, is_selfsig)) ) { rc = check_key_signature (keyblock, node, is_selfsig);
switch ( gpg_err_code (rc) ) {
case 0: case 0:
node->flag &= ~(NODFLG_BADSIG|NODFLG_NOKEY|NODFLG_SIGERR); node->flag &= ~(NODFLG_BADSIG|NODFLG_NOKEY|NODFLG_SIGERR);
sigrc = '!'; sigrc = '!';
break; break;
case G10ERR_BAD_SIGN: case GPG_ERR_BAD_SIGNATURE:
node->flag = NODFLG_BADSIG; node->flag = NODFLG_BADSIG;
sigrc = '-'; sigrc = '-';
if( inv_sigs ) if( inv_sigs )
++*inv_sigs; ++*inv_sigs;
break; break;
case G10ERR_NO_PUBKEY: case GPG_ERR_NO_PUBKEY:
case G10ERR_UNU_PUBKEY: case GPG_ERR_UNUSABLE_PUBKEY:
node->flag = NODFLG_NOKEY; node->flag = NODFLG_NOKEY;
sigrc = '?'; sigrc = '?';
if( no_key ) if( no_key )
@ -146,7 +148,7 @@ print_and_check_one_sig( KBNODE keyblock, KBNODE node,
(sig->trust_depth>0)?'0'+sig->trust_depth:' ', (sig->trust_depth>0)?'0'+sig->trust_depth:' ',
(ulong)sig->keyid[1], datestr_from_sig(sig)); (ulong)sig->keyid[1], datestr_from_sig(sig));
if( sigrc == '%' ) if( sigrc == '%' )
tty_printf("[%s] ", g10_errstr(rc) ); tty_printf("[%s] ", gpg_strerror (rc) );
else if( sigrc == '?' ) else if( sigrc == '?' )
; ;
else if( *is_selfsig ) { else if( *is_selfsig ) {
@ -157,7 +159,7 @@ print_and_check_one_sig( KBNODE keyblock, KBNODE node,
size_t n; size_t n;
char *p = get_user_id( sig->keyid, &n ); char *p = get_user_id( sig->keyid, &n );
tty_print_utf8_string2( p, n, 40 ); tty_print_utf8_string2( p, n, 40 );
m_free(p); xfree (p);
} }
tty_printf("\n"); tty_printf("\n");
@ -313,7 +315,7 @@ trustsig_prompt(byte *trust_value,byte *trust_depth,char **regexp)
*trust_value=60; *trust_value=60;
else if(p[0]=='2' && !p[1]) else if(p[0]=='2' && !p[1])
*trust_value=120; *trust_value=120;
m_free(p); xfree (p);
} }
tty_printf("\n"); tty_printf("\n");
@ -330,7 +332,7 @@ trustsig_prompt(byte *trust_value,byte *trust_depth,char **regexp)
trim_spaces(p); trim_spaces(p);
cpr_kill_prompt(); cpr_kill_prompt();
*trust_depth=atoi(p); *trust_depth=atoi(p);
m_free(p); xfree (p);
if(*trust_depth<1 || *trust_depth>255) if(*trust_depth<1 || *trust_depth>255)
*trust_depth=0; *trust_depth=0;
} }
@ -351,7 +353,7 @@ trustsig_prompt(byte *trust_value,byte *trust_depth,char **regexp)
char *q=p; char *q=p;
int regexplen=100,ind; int regexplen=100,ind;
*regexp=m_alloc(regexplen); *regexp=xmalloc (regexplen);
/* Now mangle the domain the user entered into a regexp. To do /* Now mangle the domain the user entered into a regexp. To do
this, \-escape everything that isn't alphanumeric, and attach this, \-escape everything that isn't alphanumeric, and attach
@ -371,7 +373,7 @@ trustsig_prompt(byte *trust_value,byte *trust_depth,char **regexp)
if((regexplen-ind)<3) if((regexplen-ind)<3)
{ {
regexplen+=100; regexplen+=100;
*regexp=m_realloc(*regexp,regexplen); *regexp=xrealloc(*regexp,regexplen);
} }
q++; q++;
@ -381,7 +383,7 @@ trustsig_prompt(byte *trust_value,byte *trust_depth,char **regexp)
strcat(*regexp,">$"); strcat(*regexp,">$");
} }
m_free(p); xfree (p);
tty_printf("\n"); tty_printf("\n");
} }
@ -504,7 +506,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
"self-signed.\n"),user); "self-signed.\n"),user);
} }
m_free(user); xfree (user);
} }
} }
else if( uidnode && node->pkt->pkttype == PKT_SIGNATURE else if( uidnode && node->pkt->pkttype == PKT_SIGNATURE
@ -534,7 +536,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
{ {
force_v4=1; force_v4=1;
node->flag|=NODFLG_DELSIG; node->flag|=NODFLG_DELSIG;
m_free(user); xfree (user);
continue; continue;
} }
} }
@ -558,7 +560,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
in place. */ in place. */
node->flag|=NODFLG_DELSIG; node->flag|=NODFLG_DELSIG;
m_free(user); xfree (user);
continue; continue;
} }
} }
@ -583,7 +585,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
in place. */ in place. */
node->flag|=NODFLG_DELSIG; node->flag|=NODFLG_DELSIG;
m_free(user); xfree (user);
continue; continue;
} }
} }
@ -606,7 +608,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
{ {
/* Don't delete the old sig here since this is /* Don't delete the old sig here since this is
an --expert thing. */ an --expert thing. */
m_free(user); xfree (user);
continue; continue;
} }
@ -615,7 +617,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
write_status_text (STATUS_ALREADY_SIGNED, buf); write_status_text (STATUS_ALREADY_SIGNED, buf);
uidnode->flag &= ~NODFLG_MARK_A; /* remove mark */ uidnode->flag &= ~NODFLG_MARK_A; /* remove mark */
m_free(user); xfree (user);
} }
} }
} }
@ -675,7 +677,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
} }
cpr_kill_prompt(); cpr_kill_prompt();
m_free(answer); xfree (answer);
} }
} }
@ -752,7 +754,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
else else
tty_printf(_("Invalid selection.\n")); tty_printf(_("Invalid selection.\n"));
m_free(answer); xfree (answer);
} }
} }
@ -764,7 +766,7 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
"with your key: \"")); "with your key: \""));
p = get_user_id( sk_keyid, &n ); p = get_user_id( sk_keyid, &n );
tty_print_utf8_string( p, n ); tty_print_utf8_string( p, n );
m_free(p); p = NULL; xfree (p); p = NULL;
tty_printf("\" (%08lX)\n",(ulong)sk_keyid[1]); tty_printf("\" (%08lX)\n",(ulong)sk_keyid[1]);
if(selfsig) if(selfsig)
@ -856,14 +858,14 @@ sign_uids( KBNODE keyblock, STRLIST locusr, int *ret_modified,
timestamp, duration, timestamp, duration,
sign_mk_attrib, &attrib ); sign_mk_attrib, &attrib );
if( rc ) { if( rc ) {
log_error(_("signing failed: %s\n"), g10_errstr(rc)); log_error(_("signing failed: %s\n"), gpg_strerror (rc));
goto leave; goto leave;
} }
*ret_modified = 1; /* we changed the keyblock */ *ret_modified = 1; /* we changed the keyblock */
update_trust = 1; update_trust = 1;
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = sig; pkt->pkt.signature = sig;
insert_kbnode( node, new_kbnode(pkt), PKT_SIGNATURE ); insert_kbnode( node, new_kbnode(pkt), PKT_SIGNATURE );
@ -909,7 +911,7 @@ change_passphrase( KBNODE keyblock )
switch( is_secret_key_protected( sk ) ) { switch( is_secret_key_protected( sk ) ) {
case -1: case -1:
rc = G10ERR_PUBKEY_ALGO; rc = GPG_ERR_PUBKEY_ALGO;
break; break;
case 0: case 0:
tty_printf(_("This key is not protected.\n")); tty_printf(_("This key is not protected.\n"));
@ -940,10 +942,10 @@ change_passphrase( KBNODE keyblock )
} }
if( rc ) if( rc )
tty_printf(_("Can't edit this key: %s\n"), g10_errstr(rc)); tty_printf(_("Can't edit this key: %s\n"), gpg_strerror (rc));
else { else {
DEK *dek = NULL; DEK *dek = NULL;
STRING2KEY *s2k = m_alloc_secure( sizeof *s2k ); STRING2KEY *s2k = xmalloc_secure ( sizeof *s2k );
const char *errtext = NULL; const char *errtext = NULL;
tty_printf(_("Enter the new passphrase for this secret key.\n\n") ); tty_printf(_("Enter the new passphrase for this secret key.\n\n") );
@ -983,18 +985,18 @@ change_passphrase( KBNODE keyblock )
} }
} }
if( rc ) if( rc )
log_error("protect_secret_key failed: %s\n", g10_errstr(rc) ); log_error("protect_secret_key failed: %s\n", gpg_strerror (rc) );
else else
changed++; changed++;
break; break;
} }
} }
m_free(s2k); xfree (s2k);
m_free(dek); xfree (dek);
} }
leave: leave:
m_free( passphrase ); xfree ( passphrase );
set_next_passphrase( NULL ); set_next_passphrase( NULL );
return changed && !rc; return changed && !rc;
} }
@ -1172,7 +1174,7 @@ keyedit_menu( const char *username, STRLIST locusr, STRLIST commands,
rc = keydb_get_keyblock (sec_kdbhd, &sec_keyblock); rc = keydb_get_keyblock (sec_kdbhd, &sec_keyblock);
if (rc) { if (rc) {
log_error (_("error reading secret keyblock `%s': %s\n"), log_error (_("error reading secret keyblock `%s': %s\n"),
username, g10_errstr(rc)); username, gpg_strerror (rc));
} }
else { else {
merge_keys_and_selfsig( sec_keyblock ); merge_keys_and_selfsig( sec_keyblock );
@ -1207,14 +1209,14 @@ keyedit_menu( const char *username, STRLIST locusr, STRLIST commands,
redisplay = 0; redisplay = 0;
} }
do { do {
m_free(answer); xfree (answer);
if( have_commands ) { if( have_commands ) {
if( commands ) { if( commands ) {
answer = m_strdup( commands->d ); answer = xstrdup ( commands->d );
commands = commands->next; commands = commands->next;
} }
else if( opt.batch ) { else if( opt.batch ) {
answer = m_strdup("quit"); answer = xstrdup ("quit");
} }
else else
have_commands = 0; have_commands = 0;
@ -1543,7 +1545,7 @@ keyedit_menu( const char *username, STRLIST locusr, STRLIST commands,
PKT_user_id *temp=keygen_get_std_prefs(); PKT_user_id *temp=keygen_get_std_prefs();
tty_printf(_("Current preference list:\n")); tty_printf(_("Current preference list:\n"));
show_prefs(temp,1); show_prefs(temp,1);
m_free(temp); xfree (temp);
} }
if (cpr_get_answer_is_yes ("keyedit.updpref.okay", if (cpr_get_answer_is_yes ("keyedit.updpref.okay",
count_selected_uids (keyblock)? count_selected_uids (keyblock)?
@ -1601,7 +1603,7 @@ keyedit_menu( const char *username, STRLIST locusr, STRLIST commands,
if( modified ) { if( modified ) {
rc = keydb_update_keyblock (kdbhd, keyblock); rc = keydb_update_keyblock (kdbhd, keyblock);
if( rc ) { if( rc ) {
log_error(_("update failed: %s\n"), g10_errstr(rc) ); log_error(_("update failed: %s\n"), gpg_strerror (rc) );
break; break;
} }
} }
@ -1609,7 +1611,7 @@ keyedit_menu( const char *username, STRLIST locusr, STRLIST commands,
rc = keydb_update_keyblock (sec_kdbhd, sec_keyblock ); rc = keydb_update_keyblock (sec_kdbhd, sec_keyblock );
if( rc ) { if( rc ) {
log_error( _("update secret failed: %s\n"), log_error( _("update secret failed: %s\n"),
g10_errstr(rc) ); gpg_strerror (rc) );
break; break;
} }
} }
@ -1636,7 +1638,7 @@ keyedit_menu( const char *username, STRLIST locusr, STRLIST commands,
release_kbnode( keyblock ); release_kbnode( keyblock );
release_kbnode( sec_keyblock ); release_kbnode( sec_keyblock );
keydb_release (kdbhd); keydb_release (kdbhd);
m_free(answer); xfree (answer);
} }
@ -1666,7 +1668,7 @@ show_prefs (PKT_user_id *uid, int verbose)
tty_printf (_("Cipher: ")); tty_printf (_("Cipher: "));
for(i=any=0; prefs[i].type; i++ ) { for(i=any=0; prefs[i].type; i++ ) {
if( prefs[i].type == PREFTYPE_SYM ) { if( prefs[i].type == PREFTYPE_SYM ) {
const char *s = cipher_algo_to_string (prefs[i].value); const char *s = gcry_cipher_algo_name (prefs[i].value);
if (any) if (any)
tty_printf (", "); tty_printf (", ");
@ -1683,13 +1685,13 @@ show_prefs (PKT_user_id *uid, int verbose)
if (!des_seen) { if (!des_seen) {
if (any) if (any)
tty_printf (", "); tty_printf (", ");
tty_printf ("%s",cipher_algo_to_string(CIPHER_ALGO_3DES)); tty_printf ("%s", gcry_cipher_algo_name (CIPHER_ALGO_3DES));
} }
tty_printf ("\n "); tty_printf ("\n ");
tty_printf (_("Digest: ")); tty_printf (_("Digest: "));
for(i=any=0; prefs[i].type; i++ ) { for(i=any=0; prefs[i].type; i++ ) {
if( prefs[i].type == PREFTYPE_HASH ) { if( prefs[i].type == PREFTYPE_HASH ) {
const char *s = digest_algo_to_string (prefs[i].value); const char *s = gcry_md_algo_name (prefs[i].value);
if (any) if (any)
tty_printf (", "); tty_printf (", ");
@ -1706,7 +1708,7 @@ show_prefs (PKT_user_id *uid, int verbose)
if (!sha1_seen) { if (!sha1_seen) {
if (any) if (any)
tty_printf (", "); tty_printf (", ");
tty_printf ("%s",digest_algo_to_string(DIGEST_ALGO_SHA1)); tty_printf ("%s", gcry_md_algo_name (DIGEST_ALGO_SHA1));
} }
tty_printf ("\n "); tty_printf ("\n ");
tty_printf (_("Compression: ")); tty_printf (_("Compression: "));
@ -1984,7 +1986,7 @@ show_key_with_all_names( KBNODE keyblock, int only_marked, int with_revoker,
u32 r_keyid[2]; u32 r_keyid[2];
char *user; char *user;
const char *algo= const char *algo=
pubkey_algo_to_string(pk->revkey[i].algid); gcry_pk_algo_name (pk->revkey[i].algid);
keyid_from_fingerprint(pk->revkey[i].fpr, keyid_from_fingerprint(pk->revkey[i].fpr,
MAX_FINGERPRINT_LEN,r_keyid); MAX_FINGERPRINT_LEN,r_keyid);
@ -1996,7 +1998,7 @@ show_key_with_all_names( KBNODE keyblock, int only_marked, int with_revoker,
if ((pk->revkey[i].class&0x40)) if ((pk->revkey[i].class&0x40))
tty_printf (_(" (sensitive)")); tty_printf (_(" (sensitive)"));
tty_printf ("\n"); tty_printf ("\n");
m_free(user); xfree (user);
} }
} }
@ -2052,11 +2054,11 @@ show_key_with_all_names( KBNODE keyblock, int only_marked, int with_revoker,
if( !rc ) if( !rc )
tty_printf( _("rev! subkey has been revoked: %s\n"), tty_printf( _("rev! subkey has been revoked: %s\n"),
datestr_from_sig( sig ) ); datestr_from_sig( sig ) );
else if( rc == G10ERR_BAD_SIGN ) else if( gpg_err_code (rc) == GPG_ERR_BAD_SIGNATURE )
tty_printf( _("rev- faked revocation found\n") ); tty_printf( _("rev- faked revocation found\n") );
else if( rc ) else if( rc )
tty_printf( _("rev? problem checking revocation: %s\n"), tty_printf( _("rev? problem checking revocation: %s\n"),
g10_errstr(rc) ); gpg_strerror (rc) );
} }
} }
/* the user ids */ /* the user ids */
@ -2311,13 +2313,13 @@ menu_adduid( KBNODE pub_keyblock, KBNODE sec_keyblock, int photo)
keygen_add_std_prefs, pk ); keygen_add_std_prefs, pk );
free_secret_key( sk ); free_secret_key( sk );
if( rc ) { if( rc ) {
log_error("signing failed: %s\n", g10_errstr(rc) ); log_error("signing failed: %s\n", gpg_strerror (rc) );
free_user_id(uid); free_user_id(uid);
return 0; return 0;
} }
/* insert/append to secret keyblock */ /* insert/append to secret keyblock */
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_USER_ID; pkt->pkttype = PKT_USER_ID;
pkt->pkt.user_id = scopy_user_id(uid); pkt->pkt.user_id = scopy_user_id(uid);
node = new_kbnode(pkt); node = new_kbnode(pkt);
@ -2325,7 +2327,7 @@ menu_adduid( KBNODE pub_keyblock, KBNODE sec_keyblock, int photo)
insert_kbnode( sec_where, node, 0 ); insert_kbnode( sec_where, node, 0 );
else else
add_kbnode( sec_keyblock, node ); add_kbnode( sec_keyblock, node );
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = copy_signature(NULL, sig); pkt->pkt.signature = copy_signature(NULL, sig);
if( sec_where ) if( sec_where )
@ -2333,7 +2335,7 @@ menu_adduid( KBNODE pub_keyblock, KBNODE sec_keyblock, int photo)
else else
add_kbnode( sec_keyblock, new_kbnode(pkt) ); add_kbnode( sec_keyblock, new_kbnode(pkt) );
/* insert/append to public keyblock */ /* insert/append to public keyblock */
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_USER_ID; pkt->pkttype = PKT_USER_ID;
pkt->pkt.user_id = uid; pkt->pkt.user_id = uid;
node = new_kbnode(pkt); node = new_kbnode(pkt);
@ -2341,7 +2343,7 @@ menu_adduid( KBNODE pub_keyblock, KBNODE sec_keyblock, int photo)
insert_kbnode( pub_where, node, 0 ); insert_kbnode( pub_where, node, 0 );
else else
add_kbnode( pub_keyblock, node ); add_kbnode( pub_keyblock, node );
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = copy_signature(NULL, sig); pkt->pkt.signature = copy_signature(NULL, sig);
if( pub_where ) if( pub_where )
@ -2586,7 +2588,7 @@ menu_addrevoker( KBNODE pub_keyblock, KBNODE sec_keyblock, int sensitive )
if(revoker_pk) if(revoker_pk)
free_public_key(revoker_pk); free_public_key(revoker_pk);
revoker_pk=m_alloc_clear(sizeof(*revoker_pk)); revoker_pk=xcalloc (1,sizeof(*revoker_pk));
tty_printf("\n"); tty_printf("\n");
@ -2599,7 +2601,7 @@ menu_addrevoker( KBNODE pub_keyblock, KBNODE sec_keyblock, int sensitive )
if(rc) if(rc)
{ {
log_error (_("key `%s' not found: %s\n"),answer,g10_errstr(rc)); log_error (_("key `%s' not found: %s\n"),answer,gpg_strerror (rc));
continue; continue;
} }
@ -2667,7 +2669,7 @@ menu_addrevoker( KBNODE pub_keyblock, KBNODE sec_keyblock, int sensitive )
p = get_user_id( keyid, &n ); p = get_user_id( keyid, &n );
tty_print_utf8_string( p, n ); tty_print_utf8_string( p, n );
m_free(p); xfree (p);
tty_printf("\n"); tty_printf("\n");
print_fingerprint(revoker_pk,NULL,2); print_fingerprint(revoker_pk,NULL,2);
tty_printf("\n"); tty_printf("\n");
@ -2693,7 +2695,7 @@ menu_addrevoker( KBNODE pub_keyblock, KBNODE sec_keyblock, int sensitive )
keygen_add_revkey,&revkey ); keygen_add_revkey,&revkey );
if( rc ) if( rc )
{ {
log_error("signing failed: %s\n", g10_errstr(rc) ); log_error("signing failed: %s\n", gpg_strerror (rc) );
goto fail; goto fail;
} }
@ -2701,13 +2703,13 @@ menu_addrevoker( KBNODE pub_keyblock, KBNODE sec_keyblock, int sensitive )
sk=NULL; sk=NULL;
/* insert into secret keyblock */ /* insert into secret keyblock */
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = copy_signature(NULL, sig); pkt->pkt.signature = copy_signature(NULL, sig);
insert_kbnode( sec_keyblock, new_kbnode(pkt), PKT_SIGNATURE ); insert_kbnode( sec_keyblock, new_kbnode(pkt), PKT_SIGNATURE );
/* insert into public keyblock */ /* insert into public keyblock */
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = sig; pkt->pkt.signature = sig;
insert_kbnode( pub_keyblock, new_kbnode(pkt), PKT_SIGNATURE ); insert_kbnode( pub_keyblock, new_kbnode(pkt), PKT_SIGNATURE );
@ -2821,23 +2823,23 @@ menu_expire( KBNODE pub_keyblock, KBNODE sec_keyblock )
sk, keygen_add_key_expire, sub_pk ); sk, keygen_add_key_expire, sub_pk );
if( rc ) { if( rc ) {
log_error("make_keysig_packet failed: %s\n", log_error("make_keysig_packet failed: %s\n",
g10_errstr(rc)); gpg_strerror (rc));
free_secret_key( sk ); free_secret_key( sk );
return 0; return 0;
} }
/* replace the packet */ /* replace the packet */
newpkt = m_alloc_clear( sizeof *newpkt ); newpkt = xcalloc (1, sizeof *newpkt );
newpkt->pkttype = PKT_SIGNATURE; newpkt->pkttype = PKT_SIGNATURE;
newpkt->pkt.signature = newsig; newpkt->pkt.signature = newsig;
free_packet( node->pkt ); free_packet( node->pkt );
m_free( node->pkt ); xfree ( node->pkt );
node->pkt = newpkt; node->pkt = newpkt;
if( sn ) { if( sn ) {
newpkt = m_alloc_clear( sizeof *newpkt ); newpkt = xcalloc (1, sizeof *newpkt );
newpkt->pkttype = PKT_SIGNATURE; newpkt->pkttype = PKT_SIGNATURE;
newpkt->pkt.signature = copy_signature( NULL, newsig ); newpkt->pkt.signature = copy_signature( NULL, newsig );
free_packet( sn->pkt ); free_packet( sn->pkt );
m_free( sn->pkt ); xfree ( sn->pkt );
sn->pkt = newpkt; sn->pkt = newpkt;
} }
sub_pk = NULL; sub_pk = NULL;
@ -2930,7 +2932,7 @@ menu_set_primary_uid ( KBNODE pub_keyblock, KBNODE sec_keyblock )
log_info(_("skipping v3 self-signature on user id \"%s\"\n"), log_info(_("skipping v3 self-signature on user id \"%s\"\n"),
user); user);
m_free(user); xfree (user);
} }
else { else {
/* This is a selfsignature which is to be replaced. /* This is a selfsignature which is to be replaced.
@ -2969,16 +2971,16 @@ menu_set_primary_uid ( KBNODE pub_keyblock, KBNODE sec_keyblock )
action > 0? "x":NULL ); action > 0? "x":NULL );
if( rc ) { if( rc ) {
log_error ("update_keysig_packet failed: %s\n", log_error ("update_keysig_packet failed: %s\n",
g10_errstr(rc)); gpg_strerror (rc));
free_secret_key( sk ); free_secret_key( sk );
return 0; return 0;
} }
/* replace the packet */ /* replace the packet */
newpkt = m_alloc_clear( sizeof *newpkt ); newpkt = xcalloc (1, sizeof *newpkt );
newpkt->pkttype = PKT_SIGNATURE; newpkt->pkttype = PKT_SIGNATURE;
newpkt->pkt.signature = newsig; newpkt->pkt.signature = newsig;
free_packet( node->pkt ); free_packet( node->pkt );
m_free( node->pkt ); xfree ( node->pkt );
node->pkt = newpkt; node->pkt = newpkt;
modified = 1; modified = 1;
} }
@ -3039,7 +3041,7 @@ menu_set_preferences (KBNODE pub_keyblock, KBNODE sec_keyblock )
log_info(_("skipping v3 self-signature on user id \"%s\"\n"), log_info(_("skipping v3 self-signature on user id \"%s\"\n"),
user); user);
m_free(user); xfree (user);
} }
else { else {
/* This is a selfsignature which is to be replaced /* This is a selfsignature which is to be replaced
@ -3056,16 +3058,16 @@ menu_set_preferences (KBNODE pub_keyblock, KBNODE sec_keyblock )
NULL ); NULL );
if( rc ) { if( rc ) {
log_error ("update_keysig_packet failed: %s\n", log_error ("update_keysig_packet failed: %s\n",
g10_errstr(rc)); gpg_strerror (rc));
free_secret_key( sk ); free_secret_key( sk );
return 0; return 0;
} }
/* replace the packet */ /* replace the packet */
newpkt = m_alloc_clear( sizeof *newpkt ); newpkt = xcalloc (1, sizeof *newpkt );
newpkt->pkttype = PKT_SIGNATURE; newpkt->pkttype = PKT_SIGNATURE;
newpkt->pkt.signature = newsig; newpkt->pkt.signature = newsig;
free_packet( node->pkt ); free_packet( node->pkt );
m_free( node->pkt ); xfree ( node->pkt );
node->pkt = newpkt; node->pkt = newpkt;
modified = 1; modified = 1;
} }
@ -3397,7 +3399,7 @@ menu_revsig( KBNODE keyblock )
attrib.non_exportable=!node->pkt->pkt.signature->flags.exportable; attrib.non_exportable=!node->pkt->pkt.signature->flags.exportable;
node->flag &= ~NODFLG_MARK_A; node->flag &= ~NODFLG_MARK_A;
sk = m_alloc_secure_clear( sizeof *sk ); sk = xcalloc_secure (1, sizeof *sk );
if( get_seckey( sk, node->pkt->pkt.signature->keyid ) ) { if( get_seckey( sk, node->pkt->pkt.signature->keyid ) ) {
log_info(_("no secret key\n")); log_info(_("no secret key\n"));
continue; continue;
@ -3411,7 +3413,7 @@ menu_revsig( KBNODE keyblock )
&attrib ); &attrib );
free_secret_key(sk); free_secret_key(sk);
if( rc ) { if( rc ) {
log_error(_("signing failed: %s\n"), g10_errstr(rc)); log_error(_("signing failed: %s\n"), gpg_strerror (rc));
release_revocation_reason_info( reason ); release_revocation_reason_info( reason );
return changed; return changed;
} }
@ -3421,7 +3423,7 @@ menu_revsig( KBNODE keyblock )
if(primary_pk->keyid[0]==sig->keyid[0] && if(primary_pk->keyid[0]==sig->keyid[0] &&
primary_pk->keyid[1]==sig->keyid[1]) primary_pk->keyid[1]==sig->keyid[1])
unode->pkt->pkt.user_id->is_revoked=1; unode->pkt->pkt.user_id->is_revoked=1;
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = sig; pkt->pkt.signature = sig;
insert_kbnode( unode, new_kbnode(pkt), 0 ); insert_kbnode( unode, new_kbnode(pkt), 0 );
@ -3470,7 +3472,7 @@ menu_revuid( KBNODE pub_keyblock, KBNODE sec_keyblock )
{ {
char *user=utf8_to_native(uid->name,uid->len,0); char *user=utf8_to_native(uid->name,uid->len,0);
log_info(_("user ID \"%s\" is already revoked\n"),user); log_info(_("user ID \"%s\" is already revoked\n"),user);
m_free(user); xfree (user);
} }
else else
{ {
@ -3502,12 +3504,12 @@ menu_revuid( KBNODE pub_keyblock, KBNODE sec_keyblock )
sign_mk_attrib, &attrib ); sign_mk_attrib, &attrib );
if( rc ) if( rc )
{ {
log_error(_("signing failed: %s\n"), g10_errstr(rc)); log_error(_("signing failed: %s\n"), gpg_strerror (rc));
goto leave; goto leave;
} }
else else
{ {
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = sig; pkt->pkt.signature = sig;
insert_kbnode( node, new_kbnode(pkt), 0 ); insert_kbnode( node, new_kbnode(pkt), 0 );
@ -3575,13 +3577,13 @@ menu_revkey( KBNODE pub_keyblock, KBNODE sec_keyblock )
sign_mk_attrib, &attrib ); sign_mk_attrib, &attrib );
free_secret_key(sk); free_secret_key(sk);
if( rc ) { if( rc ) {
log_error(_("signing failed: %s\n"), g10_errstr(rc)); log_error(_("signing failed: %s\n"), gpg_strerror (rc));
release_revocation_reason_info( reason ); release_revocation_reason_info( reason );
return changed; return changed;
} }
changed = 1; /* we changed the keyblock */ changed = 1; /* we changed the keyblock */
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = sig; pkt->pkt.signature = sig;
insert_kbnode( node, new_kbnode(pkt), 0 ); insert_kbnode( node, new_kbnode(pkt), 0 );

View File

@ -26,6 +26,8 @@
#include <ctype.h> #include <ctype.h>
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "main.h" #include "main.h"
#include "packet.h" #include "packet.h"
@ -81,13 +83,13 @@ struct output_control_s {
struct { struct {
char *fname; char *fname;
char *newfname; char *newfname;
IOBUF stream; iobuf_t stream;
armor_filter_context_t afx; armor_filter_context_t afx;
} pub; } pub;
struct { struct {
char *fname; char *fname;
char *newfname; char *newfname;
IOBUF stream; iobuf_t stream;
armor_filter_context_t afx; armor_filter_context_t afx;
} sec; } sec;
}; };
@ -110,17 +112,17 @@ static int mdc_available,ks_modify;
static void do_generate_keypair( struct para_data_s *para, static void do_generate_keypair( struct para_data_s *para,
struct output_control_s *outctrl ); struct output_control_s *outctrl );
static int write_keyblock( IOBUF out, KBNODE node ); static int write_keyblock( iobuf_t out, KBNODE node );
static void static void
write_uid( KBNODE root, const char *s ) write_uid( KBNODE root, const char *s )
{ {
PACKET *pkt = m_alloc_clear(sizeof *pkt ); PACKET *pkt = xcalloc (1,sizeof *pkt );
size_t n = strlen(s); size_t n = strlen(s);
pkt->pkttype = PKT_USER_ID; pkt->pkttype = PKT_USER_ID;
pkt->pkt.user_id = m_alloc_clear( sizeof *pkt->pkt.user_id + n - 1 ); pkt->pkt.user_id = xcalloc (1, sizeof *pkt->pkt.user_id + n - 1 );
pkt->pkt.user_id->len = n; pkt->pkt.user_id->len = n;
pkt->pkt.user_id->ref = 1; pkt->pkt.user_id->ref = 1;
strcpy(pkt->pkt.user_id->name, s); strcpy(pkt->pkt.user_id->name, s);
@ -241,7 +243,7 @@ keygen_set_std_prefs (const char *string,int personal)
if (!string || !ascii_strcasecmp (string, "default")) { if (!string || !ascii_strcasecmp (string, "default")) {
if (opt.def_preference_list) if (opt.def_preference_list)
string=opt.def_preference_list; string=opt.def_preference_list;
else if ( !check_cipher_algo(CIPHER_ALGO_IDEA) ) else if ( !openpgp_cipher_test_algo(CIPHER_ALGO_IDEA) )
string = AES CAST5 "S2 S1 H2 H3 Z2 Z1"; string = AES CAST5 "S2 S1 H2 H3 Z2 Z1";
else else
string = AES CAST5 "S2 H2 H3 Z2 Z1"; string = AES CAST5 "S2 H2 H3 Z2 Z1";
@ -261,16 +263,16 @@ keygen_set_std_prefs (const char *string,int personal)
{ {
char *tok,*prefstring; char *tok,*prefstring;
prefstring=m_strdup(string); /* need a writable string! */ prefstring=xstrdup (string); /* need a writable string! */
while((tok=strsep(&prefstring," ,"))) while((tok=strsep(&prefstring," ,")))
{ {
if((val=string_to_cipher_algo(tok))) if((val=openpgp_cipher_map_name(tok)))
{ {
if(set_one_pref(val,1,tok,sym,&nsym)) if(set_one_pref(val,1,tok,sym,&nsym))
rc=-1; rc=-1;
} }
else if((val=string_to_digest_algo(tok))) else if((val=openpgp_md_map_name(tok)))
{ {
if(set_one_pref(val,2,tok,hash,&nhash)) if(set_one_pref(val,2,tok,hash,&nhash))
rc=-1; rc=-1;
@ -301,7 +303,7 @@ keygen_set_std_prefs (const char *string,int personal)
} }
} }
m_free(prefstring); xfree (prefstring);
} }
if(!rc) if(!rc)
@ -310,7 +312,7 @@ keygen_set_std_prefs (const char *string,int personal)
{ {
if(personal==PREFTYPE_SYM) if(personal==PREFTYPE_SYM)
{ {
m_free(opt.personal_cipher_prefs); xfree (opt.personal_cipher_prefs);
if(nsym==0) if(nsym==0)
opt.personal_cipher_prefs=NULL; opt.personal_cipher_prefs=NULL;
@ -319,7 +321,7 @@ keygen_set_std_prefs (const char *string,int personal)
int i; int i;
opt.personal_cipher_prefs= opt.personal_cipher_prefs=
m_alloc(sizeof(prefitem_t *)*(nsym+1)); xmalloc (sizeof(prefitem_t *)*(nsym+1));
for (i=0; i<nsym; i++) for (i=0; i<nsym; i++)
{ {
@ -333,7 +335,7 @@ keygen_set_std_prefs (const char *string,int personal)
} }
else if(personal==PREFTYPE_HASH) else if(personal==PREFTYPE_HASH)
{ {
m_free(opt.personal_digest_prefs); xfree (opt.personal_digest_prefs);
if(nhash==0) if(nhash==0)
opt.personal_digest_prefs=NULL; opt.personal_digest_prefs=NULL;
@ -342,7 +344,7 @@ keygen_set_std_prefs (const char *string,int personal)
int i; int i;
opt.personal_digest_prefs= opt.personal_digest_prefs=
m_alloc(sizeof(prefitem_t *)*(nhash+1)); xmalloc (sizeof(prefitem_t *)*(nhash+1));
for (i=0; i<nhash; i++) for (i=0; i<nhash; i++)
{ {
@ -356,7 +358,7 @@ keygen_set_std_prefs (const char *string,int personal)
} }
else if(personal==PREFTYPE_ZIP) else if(personal==PREFTYPE_ZIP)
{ {
m_free(opt.personal_compress_prefs); xfree (opt.personal_compress_prefs);
if(nzip==0) if(nzip==0)
opt.personal_compress_prefs=NULL; opt.personal_compress_prefs=NULL;
@ -365,7 +367,7 @@ keygen_set_std_prefs (const char *string,int personal)
int i; int i;
opt.personal_compress_prefs= opt.personal_compress_prefs=
m_alloc(sizeof(prefitem_t *)*(nzip+1)); xmalloc (sizeof(prefitem_t *)*(nzip+1));
for (i=0; i<nzip; i++) for (i=0; i<nzip; i++)
{ {
@ -400,12 +402,12 @@ keygen_set_std_prefs (const char *string,int personal)
PKT_user_id *keygen_get_std_prefs(void) PKT_user_id *keygen_get_std_prefs(void)
{ {
int i,j=0; int i,j=0;
PKT_user_id *uid=m_alloc_clear(sizeof(PKT_user_id)); PKT_user_id *uid=xcalloc (1,sizeof(PKT_user_id));
if(!prefs_initialized) if(!prefs_initialized)
keygen_set_std_prefs(NULL,0); keygen_set_std_prefs(NULL,0);
uid->prefs=m_alloc((sizeof(prefitem_t *)* uid->prefs=xmalloc ((sizeof(prefitem_t *)*
(nsym_prefs+nhash_prefs+nzip_prefs+1))); (nsym_prefs+nhash_prefs+nzip_prefs+1)));
for(i=0;i<nsym_prefs;i++,j++) for(i=0;i<nsym_prefs;i++,j++)
@ -451,10 +453,10 @@ add_feature_mdc (PKT_signature *sig,int enabled)
if (!s || !n) { /* create a new one */ if (!s || !n) { /* create a new one */
n = 1; n = 1;
buf = m_alloc_clear (n); buf = xcalloc (1,n);
} }
else { else {
buf = m_alloc (n); buf = xmalloc (n);
memcpy (buf, s, n); memcpy (buf, s, n);
} }
@ -473,7 +475,7 @@ add_feature_mdc (PKT_signature *sig,int enabled)
else else
build_sig_subpkt (sig, SIGSUBPKT_FEATURES, buf, n); build_sig_subpkt (sig, SIGSUBPKT_FEATURES, buf, n);
m_free (buf); xfree (buf);
} }
static void static void
@ -495,10 +497,10 @@ add_keyserver_modify (PKT_signature *sig,int enabled)
if (!s || !n) { /* create a new one */ if (!s || !n) { /* create a new one */
n = 1; n = 1;
buf = m_alloc_clear (n); buf = xcalloc (1,n);
} }
else { else {
buf = m_alloc (n); buf = xmalloc (n);
memcpy (buf, s, n); memcpy (buf, s, n);
} }
@ -517,7 +519,7 @@ add_keyserver_modify (PKT_signature *sig,int enabled)
else else
build_sig_subpkt (sig, SIGSUBPKT_KS_FLAGS, buf, n); build_sig_subpkt (sig, SIGSUBPKT_KS_FLAGS, buf, n);
m_free (buf); xfree (buf);
} }
int int
@ -624,11 +626,11 @@ write_direct_sig( KBNODE root, KBNODE pub_root, PKT_secret_key *sk,
rc = make_keysig_packet(&sig,pk,NULL,NULL,sk,0x1F,0,0,0,0, rc = make_keysig_packet(&sig,pk,NULL,NULL,sk,0x1F,0,0,0,0,
keygen_add_revkey,revkey); keygen_add_revkey,revkey);
if( rc ) { if( rc ) {
log_error("make_keysig_packet failed: %s\n", g10_errstr(rc) ); log_error("make_keysig_packet failed: %s\n", gpg_strerror (rc) );
return rc; return rc;
} }
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = sig; pkt->pkt.signature = sig;
add_kbnode( root, new_kbnode( pkt ) ); add_kbnode( root, new_kbnode( pkt ) );
@ -668,11 +670,11 @@ write_selfsig( KBNODE root, KBNODE pub_root, PKT_secret_key *sk,
rc = make_keysig_packet( &sig, pk, uid, NULL, sk, 0x13, 0, 0, 0, 0, rc = make_keysig_packet( &sig, pk, uid, NULL, sk, 0x13, 0, 0, 0, 0,
keygen_add_std_prefs, pk ); keygen_add_std_prefs, pk );
if( rc ) { if( rc ) {
log_error("make_keysig_packet failed: %s\n", g10_errstr(rc) ); log_error("make_keysig_packet failed: %s\n", gpg_strerror (rc) );
return rc; return rc;
} }
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = sig; pkt->pkt.signature = sig;
add_kbnode( root, new_kbnode( pkt ) ); add_kbnode( root, new_kbnode( pkt ) );
@ -717,11 +719,11 @@ write_keybinding( KBNODE root, KBNODE pub_root, PKT_secret_key *sk,
rc = make_keysig_packet( &sig, pk, NULL, subpk, sk, 0x18, 0, 0, 0, 0, rc = make_keysig_packet( &sig, pk, NULL, subpk, sk, 0x18, 0, 0, 0, 0,
keygen_add_key_flags_and_expire, &oduap ); keygen_add_key_flags_and_expire, &oduap );
if( rc ) { if( rc ) {
log_error("make_keysig_packet failed: %s\n", g10_errstr(rc) ); log_error("make_keysig_packet failed: %s\n", gpg_strerror (rc) );
return rc; return rc;
} }
pkt = m_alloc_clear( sizeof *pkt ); pkt = xcalloc (1, sizeof *pkt );
pkt->pkttype = PKT_SIGNATURE; pkt->pkttype = PKT_SIGNATURE;
pkt->pkt.signature = sig; pkt->pkt.signature = sig;
add_kbnode( root, new_kbnode( pkt ) ); add_kbnode( root, new_kbnode( pkt ) );
@ -738,8 +740,8 @@ gen_elg(int algo, unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
PACKET *pkt; PACKET *pkt;
PKT_secret_key *sk; PKT_secret_key *sk;
PKT_public_key *pk; PKT_public_key *pk;
MPI skey[4]; gcry_mpi_t skey[4];
MPI *factors; gcry_mpi_t *factors;
assert( is_ELGAMAL(algo) ); assert( is_ELGAMAL(algo) );
@ -753,14 +755,15 @@ gen_elg(int algo, unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
log_info(_("keysize rounded up to %u bits\n"), nbits ); log_info(_("keysize rounded up to %u bits\n"), nbits );
} }
rc = pubkey_generate( algo, nbits, skey, &factors ); #warning need to implement this
rc = -1 /*pubkey_generate( algo, nbits, skey, &factors )*/;
if( rc ) { if( rc ) {
log_error("pubkey_generate failed: %s\n", g10_errstr(rc) ); log_error("pubkey_generate failed: %s\n", gpg_strerror (rc) );
return rc; return rc;
} }
sk = m_alloc_clear( sizeof *sk ); sk = xcalloc (1, sizeof *sk );
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
sk->timestamp = pk->timestamp = make_timestamp(); sk->timestamp = pk->timestamp = make_timestamp();
sk->version = pk->version = 4; sk->version = pk->version = 4;
if( expireval ) { if( expireval ) {
@ -786,21 +789,21 @@ gen_elg(int algo, unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
sk->protect.s2k = *s2k; sk->protect.s2k = *s2k;
rc = protect_secret_key( sk, dek ); rc = protect_secret_key( sk, dek );
if( rc ) { if( rc ) {
log_error("protect_secret_key failed: %s\n", g10_errstr(rc) ); log_error("protect_secret_key failed: %s\n", gpg_strerror (rc) );
free_public_key(pk); free_public_key(pk);
free_secret_key(sk); free_secret_key(sk);
return rc; return rc;
} }
} }
pkt = m_alloc_clear(sizeof *pkt); pkt = xcalloc (1,sizeof *pkt);
pkt->pkttype = ret_sk ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY; pkt->pkttype = ret_sk ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY;
pkt->pkt.public_key = pk; pkt->pkt.public_key = pk;
add_kbnode(pub_root, new_kbnode( pkt )); add_kbnode(pub_root, new_kbnode( pkt ));
/* don't know whether it makes sense to have the factors, so for now /* don't know whether it makes sense to have the factors, so for now
* we store them in the secret keyring (but they are not secret) */ * we store them in the secret keyring (but they are not secret) */
pkt = m_alloc_clear(sizeof *pkt); pkt = xcalloc (1,sizeof *pkt);
pkt->pkttype = ret_sk ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY; pkt->pkttype = ret_sk ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY;
pkt->pkt.secret_key = sk; pkt->pkt.secret_key = sk;
add_kbnode(sec_root, new_kbnode( pkt )); add_kbnode(sec_root, new_kbnode( pkt ));
@ -824,8 +827,8 @@ gen_dsa(unsigned int nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
PACKET *pkt; PACKET *pkt;
PKT_secret_key *sk; PKT_secret_key *sk;
PKT_public_key *pk; PKT_public_key *pk;
MPI skey[5]; gcry_mpi_t skey[5];
MPI *factors; gcry_mpi_t *factors;
if( nbits > 1024 || nbits < 512 ) { if( nbits > 1024 || nbits < 512 ) {
nbits = 1024; nbits = 1024;
@ -837,14 +840,15 @@ gen_dsa(unsigned int nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
log_info(_("keysize rounded up to %u bits\n"), nbits ); log_info(_("keysize rounded up to %u bits\n"), nbits );
} }
rc = pubkey_generate( PUBKEY_ALGO_DSA, nbits, skey, &factors ); #warning need to implement this
rc = -1 /*pubkey_generate( PUBKEY_ALGO_DSA, nbits, skey, &factors )*/;
if( rc ) { if( rc ) {
log_error("pubkey_generate failed: %s\n", g10_errstr(rc) ); log_error("pubkey_generate failed: %s\n", gpg_strerror (rc) );
return rc; return rc;
} }
sk = m_alloc_clear( sizeof *sk ); sk = xcalloc (1, sizeof *sk );
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
sk->timestamp = pk->timestamp = make_timestamp(); sk->timestamp = pk->timestamp = make_timestamp();
sk->version = pk->version = 4; sk->version = pk->version = 4;
if( expireval ) { if( expireval ) {
@ -872,14 +876,14 @@ gen_dsa(unsigned int nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
sk->protect.s2k = *s2k; sk->protect.s2k = *s2k;
rc = protect_secret_key( sk, dek ); rc = protect_secret_key( sk, dek );
if( rc ) { if( rc ) {
log_error("protect_secret_key failed: %s\n", g10_errstr(rc) ); log_error("protect_secret_key failed: %s\n", gpg_strerror (rc) );
free_public_key(pk); free_public_key(pk);
free_secret_key(sk); free_secret_key(sk);
return rc; return rc;
} }
} }
pkt = m_alloc_clear(sizeof *pkt); pkt = xcalloc (1,sizeof *pkt);
pkt->pkttype = ret_sk ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY; pkt->pkttype = ret_sk ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY;
pkt->pkt.public_key = pk; pkt->pkt.public_key = pk;
add_kbnode(pub_root, new_kbnode( pkt )); add_kbnode(pub_root, new_kbnode( pkt ));
@ -890,7 +894,7 @@ gen_dsa(unsigned int nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
* We store only f1 to f_n-1; fn can be calculated because p and q * We store only f1 to f_n-1; fn can be calculated because p and q
* are known. * are known.
*/ */
pkt = m_alloc_clear(sizeof *pkt); pkt = xcalloc (1,sizeof *pkt);
pkt->pkttype = ret_sk ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY; pkt->pkttype = ret_sk ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY;
pkt->pkt.secret_key = sk; pkt->pkt.secret_key = sk;
add_kbnode(sec_root, new_kbnode( pkt )); add_kbnode(sec_root, new_kbnode( pkt ));
@ -913,8 +917,8 @@ gen_rsa(int algo, unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
PACKET *pkt; PACKET *pkt;
PKT_secret_key *sk; PKT_secret_key *sk;
PKT_public_key *pk; PKT_public_key *pk;
MPI skey[6]; gcry_mpi_t skey[6];
MPI *factors; gcry_mpi_t *factors;
assert( is_RSA(algo) ); assert( is_RSA(algo) );
@ -928,14 +932,15 @@ gen_rsa(int algo, unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
log_info(_("keysize rounded up to %u bits\n"), nbits ); log_info(_("keysize rounded up to %u bits\n"), nbits );
} }
rc = pubkey_generate( algo, nbits, skey, &factors ); #warning need to implement this
rc = -1 /*pubkey_generate( algo, nbits, skey, &factors )*/;
if( rc ) { if( rc ) {
log_error("pubkey_generate failed: %s\n", g10_errstr(rc) ); log_error("pubkey_generate failed: %s\n", gpg_strerror (rc) );
return rc; return rc;
} }
sk = m_alloc_clear( sizeof *sk ); sk = xcalloc (1, sizeof *sk );
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
sk->timestamp = pk->timestamp = make_timestamp(); sk->timestamp = pk->timestamp = make_timestamp();
sk->version = pk->version = 4; sk->version = pk->version = 4;
if( expireval ) { if( expireval ) {
@ -965,19 +970,19 @@ gen_rsa(int algo, unsigned nbits, KBNODE pub_root, KBNODE sec_root, DEK *dek,
sk->protect.s2k = *s2k; sk->protect.s2k = *s2k;
rc = protect_secret_key( sk, dek ); rc = protect_secret_key( sk, dek );
if( rc ) { if( rc ) {
log_error("protect_secret_key failed: %s\n", g10_errstr(rc) ); log_error("protect_secret_key failed: %s\n", gpg_strerror (rc) );
free_public_key(pk); free_public_key(pk);
free_secret_key(sk); free_secret_key(sk);
return rc; return rc;
} }
} }
pkt = m_alloc_clear(sizeof *pkt); pkt = xcalloc (1,sizeof *pkt);
pkt->pkttype = ret_sk ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY; pkt->pkttype = ret_sk ? PKT_PUBLIC_KEY : PKT_PUBLIC_SUBKEY;
pkt->pkt.public_key = pk; pkt->pkt.public_key = pk;
add_kbnode(pub_root, new_kbnode( pkt )); add_kbnode(pub_root, new_kbnode( pkt ));
pkt = m_alloc_clear(sizeof *pkt); pkt = xcalloc (1,sizeof *pkt);
pkt->pkttype = ret_sk ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY; pkt->pkttype = ret_sk ? PKT_SECRET_KEY : PKT_SECRET_SUBKEY;
pkt->pkt.secret_key = sk; pkt->pkt.secret_key = sk;
add_kbnode(sec_root, new_kbnode( pkt )); add_kbnode(sec_root, new_kbnode( pkt ));
@ -1043,7 +1048,7 @@ ask_algo (int addmode, unsigned int *r_usage)
answer = cpr_get("keygen.algo",_("Your selection? ")); answer = cpr_get("keygen.algo",_("Your selection? "));
cpr_kill_prompt(); cpr_kill_prompt();
algo = *answer? atoi(answer): 1; algo = *answer? atoi(answer): 1;
m_free(answer); xfree (answer);
if( algo == 1 && !addmode ) { if( algo == 1 && !addmode ) {
algo = 0; /* create both keys */ algo = 0; /* create both keys */
break; break;
@ -1103,7 +1108,7 @@ ask_keysize( int algo )
" minimum keysize is 768 bits\n" " minimum keysize is 768 bits\n"
" default keysize is 1024 bits\n" " default keysize is 1024 bits\n"
" highest suggested keysize is 2048 bits\n"), " highest suggested keysize is 2048 bits\n"),
pubkey_algo_to_string(algo) ); gcry_pk_algo_name (algo) );
} }
for(;;) { for(;;) {
@ -1111,7 +1116,7 @@ ask_keysize( int algo )
_("What keysize do you want? (1024) ")); _("What keysize do you want? (1024) "));
cpr_kill_prompt(); cpr_kill_prompt();
nbits = *answer? atoi(answer): 1024; nbits = *answer? atoi(answer): 1024;
m_free(answer); xfree (answer);
if( algo == PUBKEY_ALGO_DSA && (nbits < 512 || nbits > 1024) ) if( algo == PUBKEY_ALGO_DSA && (nbits < 512 || nbits > 1024) )
tty_printf(_("DSA only allows keysizes from 512 to 1024\n")); tty_printf(_("DSA only allows keysizes from 512 to 1024\n"));
else if( algo == PUBKEY_ALGO_RSA && nbits < 1024 ) else if( algo == PUBKEY_ALGO_RSA && nbits < 1024 )
@ -1234,7 +1239,7 @@ ask_expire_interval(int object)
for(;;) { for(;;) {
u32 curtime=make_timestamp(); u32 curtime=make_timestamp();
m_free(answer); xfree (answer);
if(object==0) if(object==0)
answer = cpr_get("keygen.valid",_("Key is valid for? (0) ")); answer = cpr_get("keygen.valid",_("Key is valid for? (0) "));
else else
@ -1269,7 +1274,7 @@ ask_expire_interval(int object)
_("Is this correct (y/n)? ")) ) _("Is this correct (y/n)? ")) )
break; break;
} }
m_free(answer); xfree (answer);
return interval; return interval;
} }
@ -1280,6 +1285,19 @@ ask_expiredate()
return x? make_timestamp() + x : 0; return x? make_timestamp() + x : 0;
} }
static int
count_chr( const char *string, int c )
{
int count;
for (count=0; *string; string++ )
if ( *string == c )
count++;
return count;
}
static int static int
has_invalid_email_chars( const char *s ) has_invalid_email_chars( const char *s )
{ {
@ -1320,7 +1338,7 @@ ask_user_id( int mode )
if( !aname ) { if( !aname ) {
for(;;) { for(;;) {
m_free(aname); xfree (aname);
aname = cpr_get("keygen.name",_("Real name: ")); aname = cpr_get("keygen.name",_("Real name: "));
trim_spaces(aname); trim_spaces(aname);
cpr_kill_prompt(); cpr_kill_prompt();
@ -1340,14 +1358,14 @@ ask_user_id( int mode )
} }
if( !amail ) { if( !amail ) {
for(;;) { for(;;) {
m_free(amail); xfree (amail);
amail = cpr_get("keygen.email",_("Email address: ")); amail = cpr_get("keygen.email",_("Email address: "));
trim_spaces(amail); trim_spaces(amail);
cpr_kill_prompt(); cpr_kill_prompt();
if( !*amail ) if( !*amail )
break; /* no email address is okay */ break; /* no email address is okay */
else if( has_invalid_email_chars(amail) else if( has_invalid_email_chars(amail)
|| string_count_chr(amail,'@') != 1 || count_chr(amail,'@') != 1
|| *amail == '@' || *amail == '@'
|| amail[strlen(amail)-1] == '@' || amail[strlen(amail)-1] == '@'
|| amail[strlen(amail)-1] == '.' || amail[strlen(amail)-1] == '.'
@ -1359,7 +1377,7 @@ ask_user_id( int mode )
} }
if( !acomment ) { if( !acomment ) {
for(;;) { for(;;) {
m_free(acomment); xfree (acomment);
acomment = cpr_get("keygen.comment",_("Comment: ")); acomment = cpr_get("keygen.comment",_("Comment: "));
trim_spaces(acomment); trim_spaces(acomment);
cpr_kill_prompt(); cpr_kill_prompt();
@ -1373,19 +1391,14 @@ ask_user_id( int mode )
} }
m_free(uid); xfree (uid);
uid = p = m_alloc(strlen(aname)+strlen(amail)+strlen(acomment)+12+10); uid = p = xmalloc (strlen(aname)+strlen(amail)+strlen(acomment)+12+10);
p = stpcpy(p, aname ); p = stpcpy(p, aname );
if( *acomment ) if( *acomment )
p = stpcpy(stpcpy(stpcpy(p," ("), acomment),")"); p = stpcpy(stpcpy(stpcpy(p," ("), acomment),")");
if( *amail ) if( *amail )
p = stpcpy(stpcpy(stpcpy(p," <"), amail),">"); p = stpcpy(stpcpy(stpcpy(p," <"), amail),">");
/* append a warning if we do not have dev/random
* or it is switched into quick testmode */
if( quick_random_gen(-1) )
strcpy(p, " (INSECURE!)" );
/* print a note in case that UTF8 mapping has to be done */ /* print a note in case that UTF8 mapping has to be done */
for(p=uid; *p; p++ ) { for(p=uid; *p; p++ ) {
if( *p & 0x80 ) { if( *p & 0x80 ) {
@ -1409,7 +1422,7 @@ ask_user_id( int mode )
if( strlen(ansstr) != 10 ) if( strlen(ansstr) != 10 )
BUG(); BUG();
if( cpr_enabled() ) { if( cpr_enabled() ) {
answer = m_strdup(ansstr+6); answer = xstrdup (ansstr+6);
answer[1] = 0; answer[1] = 0;
} }
else { else {
@ -1421,15 +1434,15 @@ ask_user_id( int mode )
if( strlen(answer) > 1 ) if( strlen(answer) > 1 )
; ;
else if( *answer == ansstr[0] || *answer == ansstr[1] ) { else if( *answer == ansstr[0] || *answer == ansstr[1] ) {
m_free(aname); aname = NULL; xfree (aname); aname = NULL;
break; break;
} }
else if( *answer == ansstr[2] || *answer == ansstr[3] ) { else if( *answer == ansstr[2] || *answer == ansstr[3] ) {
m_free(acomment); acomment = NULL; xfree (acomment); acomment = NULL;
break; break;
} }
else if( *answer == ansstr[4] || *answer == ansstr[5] ) { else if( *answer == ansstr[4] || *answer == ansstr[5] ) {
m_free(amail); amail = NULL; xfree (amail); amail = NULL;
break; break;
} }
else if( *answer == ansstr[6] || *answer == ansstr[7] ) { else if( *answer == ansstr[6] || *answer == ansstr[7] ) {
@ -1437,29 +1450,29 @@ ask_user_id( int mode )
tty_printf(_("Please correct the error first\n")); tty_printf(_("Please correct the error first\n"));
} }
else { else {
m_free(aname); aname = NULL; xfree (aname); aname = NULL;
m_free(acomment); acomment = NULL; xfree (acomment); acomment = NULL;
m_free(amail); amail = NULL; xfree (amail); amail = NULL;
break; break;
} }
} }
else if( *answer == ansstr[8] || *answer == ansstr[9] ) { else if( *answer == ansstr[8] || *answer == ansstr[9] ) {
m_free(aname); aname = NULL; xfree (aname); aname = NULL;
m_free(acomment); acomment = NULL; xfree (acomment); acomment = NULL;
m_free(amail); amail = NULL; xfree (amail); amail = NULL;
m_free(uid); uid = NULL; xfree (uid); uid = NULL;
break; break;
} }
m_free(answer); xfree (answer);
} }
m_free(answer); xfree (answer);
if( !amail && !acomment && !amail ) if( !amail && !acomment && !amail )
break; break;
m_free(uid); uid = NULL; xfree (uid); uid = NULL;
} }
if( uid ) { if( uid ) {
char *p = native_to_utf8( uid ); char *p = native_to_utf8( uid );
m_free( uid ); xfree ( uid );
uid = p; uid = p;
} }
return uid; return uid;
@ -1475,7 +1488,7 @@ ask_passphrase( STRING2KEY **ret_s2k )
tty_printf(_("You need a Passphrase to protect your secret key.\n\n") ); tty_printf(_("You need a Passphrase to protect your secret key.\n\n") );
s2k = m_alloc_secure( sizeof *s2k ); s2k = xmalloc_secure ( sizeof *s2k );
for(;;) { for(;;) {
s2k->mode = opt.s2k_mode; s2k->mode = opt.s2k_mode;
s2k->hash_algo = opt.s2k_digest_algo; s2k->hash_algo = opt.s2k_digest_algo;
@ -1486,8 +1499,8 @@ ask_passphrase( STRING2KEY **ret_s2k )
tty_printf(_("%s.\n"), _(errtext)); tty_printf(_("%s.\n"), _(errtext));
} }
else if( !dek->keylen ) { else if( !dek->keylen ) {
m_free(dek); dek = NULL; xfree (dek); dek = NULL;
m_free(s2k); s2k = NULL; xfree (s2k); s2k = NULL;
tty_printf(_( tty_printf(_(
"You don't want a passphrase - this is probably a *bad* idea!\n" "You don't want a passphrase - this is probably a *bad* idea!\n"
"I will do it anyway. You can change your passphrase at any time,\n" "I will do it anyway. You can change your passphrase at any time,\n"
@ -1552,7 +1565,7 @@ generate_user_id()
if( !p ) if( !p )
return NULL; return NULL;
n = strlen(p); n = strlen(p);
uid = m_alloc_clear( sizeof *uid + n - 1 ); uid = xcalloc (1, sizeof *uid + n - 1 );
uid->len = n; uid->len = n;
strcpy(uid->name, p); strcpy(uid->name, p);
uid->ref = 1; uid->ref = 1;
@ -1568,11 +1581,11 @@ release_parameter_list( struct para_data_s *r )
for( ; r ; r = r2 ) { for( ; r ; r = r2 ) {
r2 = r->next; r2 = r->next;
if( r->key == pPASSPHRASE_DEK ) if( r->key == pPASSPHRASE_DEK )
m_free( r->u.dek ); xfree ( r->u.dek );
else if( r->key == pPASSPHRASE_S2K ) else if( r->key == pPASSPHRASE_S2K )
m_free( r->u.s2k ); xfree ( r->u.s2k );
m_free(r); xfree (r);
} }
} }
@ -1603,7 +1616,7 @@ get_parameter_algo( struct para_data_s *para, enum para_name key )
if( isdigit( *r->u.value ) ) if( isdigit( *r->u.value ) )
i = atoi( r->u.value ); i = atoi( r->u.value );
else else
i = string_to_pubkey_algo( r->u.value ); i = openpgp_pk_map_name ( r->u.value );
if (i == PUBKEY_ALGO_RSA_E || i == PUBKEY_ALGO_RSA_S) if (i == PUBKEY_ALGO_RSA_E || i == PUBKEY_ALGO_RSA_S)
i = 0; /* we don't want to allow generation of these algorithms */ i = 0; /* we don't want to allow generation of these algorithms */
return i; return i;
@ -1750,7 +1763,7 @@ proc_parameter_file( struct para_data_s *para, const char *fname,
/* check that we have all required parameters */ /* check that we have all required parameters */
assert( get_parameter( para, pKEYTYPE ) ); assert( get_parameter( para, pKEYTYPE ) );
i = get_parameter_algo( para, pKEYTYPE ); i = get_parameter_algo( para, pKEYTYPE );
if( i < 1 || check_pubkey_algo2( i, PUBKEY_USAGE_SIG ) ) { if( i < 1 || openpgp_pk_test_algo ( i, PUBKEY_USAGE_SIG ) ) {
r = get_parameter( para, pKEYTYPE ); r = get_parameter( para, pKEYTYPE );
log_error("%s:%d: invalid algorithm\n", fname, r->lnr ); log_error("%s:%d: invalid algorithm\n", fname, r->lnr );
return -1; return -1;
@ -1760,7 +1773,7 @@ proc_parameter_file( struct para_data_s *para, const char *fname,
return -1; return -1;
i = get_parameter_algo( para, pSUBKEYTYPE ); i = get_parameter_algo( para, pSUBKEYTYPE );
if( i > 0 && check_pubkey_algo( i ) ) { if( i > 0 && openpgp_pk_test_algo ( i, 0 ) ) {
r = get_parameter( para, pSUBKEYTYPE ); r = get_parameter( para, pSUBKEYTYPE );
log_error("%s:%d: invalid algorithm\n", fname, r->lnr ); log_error("%s:%d: invalid algorithm\n", fname, r->lnr );
return -1; return -1;
@ -1776,7 +1789,7 @@ proc_parameter_file( struct para_data_s *para, const char *fname,
s3 = get_parameter_value( para, pNAMEEMAIL ); s3 = get_parameter_value( para, pNAMEEMAIL );
if( s1 || s2 || s3 ) { if( s1 || s2 || s3 ) {
n = (s1?strlen(s1):0) + (s2?strlen(s2):0) + (s3?strlen(s3):0); n = (s1?strlen(s1):0) + (s2?strlen(s2):0) + (s3?strlen(s3):0);
r = m_alloc_clear( sizeof *r + n + 20 ); r = xcalloc (1, sizeof *r + n + 20 );
r->key = pUSERID; r->key = pUSERID;
p = r->u.value; p = r->u.value;
if( s1 ) if( s1 )
@ -1806,7 +1819,7 @@ proc_parameter_file( struct para_data_s *para, const char *fname,
STRING2KEY *s2k; STRING2KEY *s2k;
DEK *dek; DEK *dek;
s2k = m_alloc_secure( sizeof *s2k ); s2k = xmalloc_secure ( sizeof *s2k );
s2k->mode = opt.s2k_mode; s2k->mode = opt.s2k_mode;
s2k->hash_algo = opt.s2k_digest_algo; s2k->hash_algo = opt.s2k_digest_algo;
set_next_passphrase( r->u.value ); set_next_passphrase( r->u.value );
@ -1816,12 +1829,12 @@ proc_parameter_file( struct para_data_s *para, const char *fname,
assert( dek ); assert( dek );
memset( r->u.value, 0, strlen(r->u.value) ); memset( r->u.value, 0, strlen(r->u.value) );
r = m_alloc_clear( sizeof *r ); r = xcalloc (1, sizeof *r );
r->key = pPASSPHRASE_S2K; r->key = pPASSPHRASE_S2K;
r->u.s2k = s2k; r->u.s2k = s2k;
r->next = para; r->next = para;
para = r; para = r;
r = m_alloc_clear( sizeof *r ); r = xcalloc (1, sizeof *r );
r->key = pPASSPHRASE_DEK; r->key = pPASSPHRASE_DEK;
r->u.dek = dek; r->u.dek = dek;
r->next = para; r->next = para;
@ -1839,7 +1852,7 @@ proc_parameter_file( struct para_data_s *para, const char *fname,
r->u.expire = i * 86400L; r->u.expire = i * 86400L;
r->key = pKEYEXPIRE; /* change hat entry */ r->key = pKEYEXPIRE; /* change hat entry */
/* also set it for the subkey */ /* also set it for the subkey */
r = m_alloc_clear( sizeof *r + 20 ); r = xcalloc (1, sizeof *r + 20 );
r->key = pSUBKEYEXPIRE; r->key = pSUBKEYEXPIRE;
r->u.expire = i * 86400L; r->u.expire = i * 86400L;
r->next = para; r->next = para;
@ -1943,8 +1956,8 @@ read_parameter_file( const char *fname )
if( outctrl.pub.fname && !strcmp( outctrl.pub.fname, value ) ) if( outctrl.pub.fname && !strcmp( outctrl.pub.fname, value ) )
; /* still the same file - ignore it */ ; /* still the same file - ignore it */
else { else {
m_free( outctrl.pub.newfname ); xfree ( outctrl.pub.newfname );
outctrl.pub.newfname = m_strdup( value ); outctrl.pub.newfname = xstrdup ( value );
outctrl.use_files = 1; outctrl.use_files = 1;
} }
} }
@ -1952,8 +1965,8 @@ read_parameter_file( const char *fname )
if( outctrl.sec.fname && !strcmp( outctrl.sec.fname, value ) ) if( outctrl.sec.fname && !strcmp( outctrl.sec.fname, value ) )
; /* still the same file - ignore it */ ; /* still the same file - ignore it */
else { else {
m_free( outctrl.sec.newfname ); xfree ( outctrl.sec.newfname );
outctrl.sec.newfname = m_strdup( value ); outctrl.sec.newfname = xstrdup ( value );
outctrl.use_files = 1; outctrl.use_files = 1;
} }
} }
@ -2009,7 +2022,7 @@ read_parameter_file( const char *fname )
break; break;
} }
} }
r = m_alloc_clear( sizeof *r + strlen( value ) ); r = xcalloc (1, sizeof *r + strlen( value ) );
r->lnr = lnr; r->lnr = lnr;
r->key = keywords[i].key; r->key = keywords[i].key;
strcpy( r->u.value, value ); strcpy( r->u.value, value );
@ -2029,10 +2042,10 @@ read_parameter_file( const char *fname )
if( outctrl.use_files ) { /* close open streams */ if( outctrl.use_files ) { /* close open streams */
iobuf_close( outctrl.pub.stream ); iobuf_close( outctrl.pub.stream );
iobuf_close( outctrl.sec.stream ); iobuf_close( outctrl.sec.stream );
m_free( outctrl.pub.fname ); xfree ( outctrl.pub.fname );
m_free( outctrl.pub.newfname ); xfree ( outctrl.pub.newfname );
m_free( outctrl.sec.fname ); xfree ( outctrl.sec.fname );
m_free( outctrl.sec.newfname ); xfree ( outctrl.sec.newfname );
} }
release_parameter_list( para ); release_parameter_list( para );
@ -2070,34 +2083,34 @@ generate_keypair( const char *fname )
algo = ask_algo( 0, &use ); algo = ask_algo( 0, &use );
if( !algo ) { /* default: DSA with ElG subkey of the specified size */ if( !algo ) { /* default: DSA with ElG subkey of the specified size */
both = 1; both = 1;
r = m_alloc_clear( sizeof *r + 20 ); r = xcalloc (1, sizeof *r + 20 );
r->key = pKEYTYPE; r->key = pKEYTYPE;
sprintf( r->u.value, "%d", PUBKEY_ALGO_DSA ); sprintf( r->u.value, "%d", PUBKEY_ALGO_DSA );
r->next = para; r->next = para;
para = r; para = r;
tty_printf(_("DSA keypair will have 1024 bits.\n")); tty_printf(_("DSA keypair will have 1024 bits.\n"));
r = m_alloc_clear( sizeof *r + 20 ); r = xcalloc (1, sizeof *r + 20 );
r->key = pKEYLENGTH; r->key = pKEYLENGTH;
strcpy( r->u.value, "1024" ); strcpy( r->u.value, "1024" );
r->next = para; r->next = para;
para = r; para = r;
algo = PUBKEY_ALGO_ELGAMAL_E; algo = PUBKEY_ALGO_ELGAMAL_E;
r = m_alloc_clear( sizeof *r + 20 ); r = xcalloc (1, sizeof *r + 20 );
r->key = pSUBKEYTYPE; r->key = pSUBKEYTYPE;
sprintf( r->u.value, "%d", algo ); sprintf( r->u.value, "%d", algo );
r->next = para; r->next = para;
para = r; para = r;
} }
else { else {
r = m_alloc_clear( sizeof *r + 20 ); r = xcalloc (1, sizeof *r + 20 );
r->key = pKEYTYPE; r->key = pKEYTYPE;
sprintf( r->u.value, "%d", algo ); sprintf( r->u.value, "%d", algo );
r->next = para; r->next = para;
para = r; para = r;
if (use) { if (use) {
r = m_alloc_clear( sizeof *r + 20 ); r = xcalloc (1, sizeof *r + 20 );
r->key = pKEYUSAGE; r->key = pKEYUSAGE;
sprintf( r->u.value, "%s%s", sprintf( r->u.value, "%s%s",
(use & PUBKEY_USAGE_SIG)? "sign ":"", (use & PUBKEY_USAGE_SIG)? "sign ":"",
@ -2109,19 +2122,19 @@ generate_keypair( const char *fname )
} }
nbits = ask_keysize( algo ); nbits = ask_keysize( algo );
r = m_alloc_clear( sizeof *r + 20 ); r = xcalloc (1, sizeof *r + 20 );
r->key = both? pSUBKEYLENGTH : pKEYLENGTH; r->key = both? pSUBKEYLENGTH : pKEYLENGTH;
sprintf( r->u.value, "%u", nbits); sprintf( r->u.value, "%u", nbits);
r->next = para; r->next = para;
para = r; para = r;
expire = ask_expire_interval(0); expire = ask_expire_interval(0);
r = m_alloc_clear( sizeof *r + 20 ); r = xcalloc (1, sizeof *r + 20 );
r->key = pKEYEXPIRE; r->key = pKEYEXPIRE;
r->u.expire = expire; r->u.expire = expire;
r->next = para; r->next = para;
para = r; para = r;
r = m_alloc_clear( sizeof *r + 20 ); r = xcalloc (1, sizeof *r + 20 );
r->key = pSUBKEYEXPIRE; r->key = pSUBKEYEXPIRE;
r->u.expire = expire; r->u.expire = expire;
r->next = para; r->next = para;
@ -2133,7 +2146,7 @@ generate_keypair( const char *fname )
release_parameter_list( para ); release_parameter_list( para );
return; return;
} }
r = m_alloc_clear( sizeof *r + strlen(uid) ); r = xcalloc (1, sizeof *r + strlen(uid) );
r->key = pUSERID; r->key = pUSERID;
strcpy( r->u.value, uid ); strcpy( r->u.value, uid );
r->next = para; r->next = para;
@ -2141,12 +2154,12 @@ generate_keypair( const char *fname )
dek = ask_passphrase( &s2k ); dek = ask_passphrase( &s2k );
if( dek ) { if( dek ) {
r = m_alloc_clear( sizeof *r ); r = xcalloc (1, sizeof *r );
r->key = pPASSPHRASE_DEK; r->key = pPASSPHRASE_DEK;
r->u.dek = dek; r->u.dek = dek;
r->next = para; r->next = para;
para = r; para = r;
r = m_alloc_clear( sizeof *r ); r = xcalloc (1, sizeof *r );
r->key = pPASSPHRASE_S2K; r->key = pPASSPHRASE_S2K;
r->u.s2k = s2k; r->u.s2k = s2k;
r->next = para; r->next = para;
@ -2198,7 +2211,7 @@ do_generate_keypair( struct para_data_s *para,
if( outctrl->pub.newfname ) { if( outctrl->pub.newfname ) {
iobuf_close(outctrl->pub.stream); iobuf_close(outctrl->pub.stream);
outctrl->pub.stream = NULL; outctrl->pub.stream = NULL;
m_free( outctrl->pub.fname ); xfree ( outctrl->pub.fname );
outctrl->pub.fname = outctrl->pub.newfname; outctrl->pub.fname = outctrl->pub.newfname;
outctrl->pub.newfname = NULL; outctrl->pub.newfname = NULL;
@ -2217,7 +2230,7 @@ do_generate_keypair( struct para_data_s *para,
if( outctrl->sec.newfname ) { if( outctrl->sec.newfname ) {
iobuf_close(outctrl->sec.stream); iobuf_close(outctrl->sec.stream);
outctrl->sec.stream = NULL; outctrl->sec.stream = NULL;
m_free( outctrl->sec.fname ); xfree ( outctrl->sec.fname );
outctrl->sec.fname = outctrl->sec.newfname; outctrl->sec.fname = outctrl->sec.newfname;
outctrl->sec.newfname = NULL; outctrl->sec.newfname = NULL;
@ -2298,11 +2311,11 @@ do_generate_keypair( struct para_data_s *para,
if( !rc && outctrl->use_files ) { /* direct write to specified files */ if( !rc && outctrl->use_files ) { /* direct write to specified files */
rc = write_keyblock( outctrl->pub.stream, pub_root ); rc = write_keyblock( outctrl->pub.stream, pub_root );
if( rc ) if( rc )
log_error("can't write public key: %s\n", g10_errstr(rc) ); log_error("can't write public key: %s\n", gpg_strerror (rc) );
if( !rc ) { if( !rc ) {
rc = write_keyblock( outctrl->sec.stream, sec_root ); rc = write_keyblock( outctrl->sec.stream, sec_root );
if( rc ) if( rc )
log_error("can't write secret key: %s\n", g10_errstr(rc) ); log_error("can't write secret key: %s\n", gpg_strerror (rc) );
} }
} }
@ -2314,13 +2327,13 @@ do_generate_keypair( struct para_data_s *para,
rc = keydb_locate_writable (pub_hd, NULL); rc = keydb_locate_writable (pub_hd, NULL);
if (rc) if (rc)
log_error (_("no writable public keyring found: %s\n"), log_error (_("no writable public keyring found: %s\n"),
g10_errstr (rc)); gpg_strerror (rc));
if (!rc) { if (!rc) {
rc = keydb_locate_writable (sec_hd, NULL); rc = keydb_locate_writable (sec_hd, NULL);
if (rc) if (rc)
log_error (_("no writable secret keyring found: %s\n"), log_error (_("no writable secret keyring found: %s\n"),
g10_errstr (rc)); gpg_strerror (rc));
} }
if (!rc && opt.verbose) { if (!rc && opt.verbose) {
@ -2334,14 +2347,14 @@ do_generate_keypair( struct para_data_s *para,
rc = keydb_insert_keyblock (pub_hd, pub_root); rc = keydb_insert_keyblock (pub_hd, pub_root);
if (rc) if (rc)
log_error (_("error writing public keyring `%s': %s\n"), log_error (_("error writing public keyring `%s': %s\n"),
keydb_get_resource_name (pub_hd), g10_errstr(rc)); keydb_get_resource_name (pub_hd), gpg_strerror (rc));
} }
if (!rc) { if (!rc) {
rc = keydb_insert_keyblock (sec_hd, sec_root); rc = keydb_insert_keyblock (sec_hd, sec_root);
if (rc) if (rc)
log_error (_("error writing secret keyring `%s': %s\n"), log_error (_("error writing secret keyring `%s': %s\n"),
keydb_get_resource_name (pub_hd), g10_errstr(rc)); keydb_get_resource_name (pub_hd), gpg_strerror (rc));
} }
keydb_release (pub_hd); keydb_release (pub_hd);
@ -2382,9 +2395,9 @@ do_generate_keypair( struct para_data_s *para,
if( rc ) { if( rc ) {
if( opt.batch ) if( opt.batch )
log_error("key generation failed: %s\n", g10_errstr(rc) ); log_error("key generation failed: %s\n", gpg_strerror (rc) );
else else
tty_printf(_("Key generation failed: %s\n"), g10_errstr(rc) ); tty_printf(_("Key generation failed: %s\n"), gpg_strerror (rc) );
} }
else { else {
PKT_public_key *pk = find_kbnode (pub_root, PKT_public_key *pk = find_kbnode (pub_root,
@ -2435,7 +2448,7 @@ generate_subkeypair( KBNODE pub_keyblock, KBNODE sec_keyblock )
: _("key has been created %lu seconds " : _("key has been created %lu seconds "
"in future (time warp or clock problem)\n"), d ); "in future (time warp or clock problem)\n"), d );
if( !opt.ignore_time_conflict ) { if( !opt.ignore_time_conflict ) {
rc = G10ERR_TIME_CONFLICT; rc = GPG_ERR_TIME_CONFLICT;
goto leave; goto leave;
} }
} }
@ -2449,7 +2462,7 @@ generate_subkeypair( KBNODE pub_keyblock, KBNODE sec_keyblock )
/* unprotect to get the passphrase */ /* unprotect to get the passphrase */
switch( is_secret_key_protected( sk ) ) { switch( is_secret_key_protected( sk ) ) {
case -1: case -1:
rc = G10ERR_PUBKEY_ALGO; rc = GPG_ERR_PUBKEY_ALGO;
break; break;
case 0: case 0:
tty_printf("This key is not protected.\n"); tty_printf("This key is not protected.\n");
@ -2474,7 +2487,7 @@ generate_subkeypair( KBNODE pub_keyblock, KBNODE sec_keyblock )
goto leave; goto leave;
if( passphrase ) { if( passphrase ) {
s2k = m_alloc_secure( sizeof *s2k ); s2k = xmalloc_secure ( sizeof *s2k );
s2k->mode = opt.s2k_mode; s2k->mode = opt.s2k_mode;
s2k->hash_algo = opt.s2k_digest_algo; s2k->hash_algo = opt.s2k_digest_algo;
set_next_passphrase( passphrase ); set_next_passphrase( passphrase );
@ -2495,10 +2508,10 @@ generate_subkeypair( KBNODE pub_keyblock, KBNODE sec_keyblock )
leave: leave:
if( rc ) if( rc )
log_error(_("Key generation failed: %s\n"), g10_errstr(rc) ); log_error(_("Key generation failed: %s\n"), gpg_strerror (rc) );
m_free( passphrase ); xfree ( passphrase );
m_free( dek ); xfree ( dek );
m_free( s2k ); xfree ( s2k );
if( sk ) /* release the copy of the (now unprotected) secret key */ if( sk ) /* release the copy of the (now unprotected) secret key */
free_secret_key(sk); free_secret_key(sk);
set_next_passphrase( NULL ); set_next_passphrase( NULL );
@ -2509,14 +2522,14 @@ generate_subkeypair( KBNODE pub_keyblock, KBNODE sec_keyblock )
* Write a keyblock to an output stream * Write a keyblock to an output stream
*/ */
static int static int
write_keyblock( IOBUF out, KBNODE node ) write_keyblock( iobuf_t out, KBNODE node )
{ {
for( ; node ; node = node->next ) { for( ; node ; node = node->next ) {
int rc = build_packet( out, node->pkt ); int rc = build_packet( out, node->pkt );
if( rc ) { if( rc ) {
log_error("build_packet(%d) failed: %s\n", log_error("build_packet(%d) failed: %s\n",
node->pkt->pkttype, g10_errstr(rc) ); node->pkt->pkttype, gpg_strerror (rc) );
return G10ERR_WRITE_FILE; return rc;
} }
} }
return 0; return 0;

View File

@ -1,5 +1,5 @@
/* keyid.c - key ID and fingerprint handling /* keyid.c - key ID and fingerprint handling
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -25,6 +25,8 @@
#include <errno.h> #include <errno.h>
#include <time.h> #include <time.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "main.h" #include "main.h"
#include "packet.h" #include "packet.h"
@ -48,10 +50,10 @@ pubkey_letter( int algo )
} }
} }
static MD_HANDLE static gcry_md_hd_t
do_fingerprint_md( PKT_public_key *pk ) do_fingerprint_md( PKT_public_key *pk )
{ {
MD_HANDLE md; gcry_md_hd_t md;
unsigned n; unsigned n;
unsigned nb[PUBKEY_MAX_NPKEY]; unsigned nb[PUBKEY_MAX_NPKEY];
unsigned nn[PUBKEY_MAX_NPKEY]; unsigned nn[PUBKEY_MAX_NPKEY];
@ -59,27 +61,35 @@ do_fingerprint_md( PKT_public_key *pk )
int i; int i;
int npkey = pubkey_get_npkey( pk->pubkey_algo ); int npkey = pubkey_get_npkey( pk->pubkey_algo );
md = md_open( pk->version < 4 ? DIGEST_ALGO_RMD160 : DIGEST_ALGO_SHA1, 0); gcry_md_open (&md, pk->version < 4 ? DIGEST_ALGO_RMD160
: DIGEST_ALGO_SHA1, 0);
n = pk->version < 4 ? 8 : 6; n = pk->version < 4 ? 8 : 6;
for(i=0; i < npkey; i++ ) { for(i=0; i < npkey; i++ ) {
nb[i] = mpi_get_nbits(pk->pkey[i]); size_t nbytes;
pp[i] = mpi_get_buffer( pk->pkey[i], nn+i, NULL );
if (gcry_mpi_print( GCRYMPI_FMT_PGP, NULL, &nbytes, pk->pkey[i] ))
BUG ();
/* fixme: we should try to allocate a buffer on the stack */
pp[i] = xmalloc(nbytes);
if (gcry_mpi_print ( GCRYMPI_FMT_PGP, pp[i], &nbytes, pk->pkey[i] ))
BUG ();
nn[i] = nbytes;
n += 2 + nn[i]; n += 2 + nn[i];
} }
md_putc( md, 0x99 ); /* ctb */ gcry_md_putc ( md, 0x99 ); /* ctb */
md_putc( md, n >> 8 ); /* 2 byte length header */ gcry_md_putc ( md, n >> 8 ); /* 2 byte length header */
md_putc( md, n ); gcry_md_putc ( md, n );
if( pk->version < 4 ) if( pk->version < 4 )
md_putc( md, 3 ); gcry_md_putc ( md, 3 );
else else
md_putc( md, 4 ); gcry_md_putc ( md, 4 );
{ u32 a = pk->timestamp; { u32 a = pk->timestamp;
md_putc( md, a >> 24 ); gcry_md_putc ( md, a >> 24 );
md_putc( md, a >> 16 ); gcry_md_putc ( md, a >> 16 );
md_putc( md, a >> 8 ); gcry_md_putc ( md, a >> 8 );
md_putc( md, a ); gcry_md_putc ( md, a );
} }
if( pk->version < 4 ) { if( pk->version < 4 ) {
u16 a; u16 a;
@ -88,22 +98,22 @@ do_fingerprint_md( PKT_public_key *pk )
a = (u16)((pk->expiredate - pk->timestamp) / 86400L); a = (u16)((pk->expiredate - pk->timestamp) / 86400L);
else else
a = 0; a = 0;
md_putc( md, a >> 8 ); gcry_md_putc ( md, a >> 8 );
md_putc( md, a ); gcry_md_putc ( md, a );
} }
md_putc( md, pk->pubkey_algo ); gcry_md_putc ( md, pk->pubkey_algo );
for(i=0; i < npkey; i++ ) { for(i=0; i < npkey; i++ ) {
md_putc( md, nb[i]>>8); gcry_md_putc ( md, nb[i]>>8);
md_putc( md, nb[i] ); gcry_md_putc ( md, nb[i] );
md_write( md, pp[i], nn[i] ); gcry_md_write( md, pp[i], nn[i] );
m_free(pp[i]); xfree (pp[i]);
} }
md_final( md ); gcry_md_final ( md );
return md; return md;
} }
static MD_HANDLE static gcry_md_hd_t
do_fingerprint_md_sk( PKT_secret_key *sk ) do_fingerprint_md_sk( PKT_secret_key *sk )
{ {
PKT_public_key pk; PKT_public_key pk;
@ -121,6 +131,31 @@ do_fingerprint_md_sk( PKT_secret_key *sk )
} }
u32
v3_keyid (gcry_mpi_t a, u32 *ki)
{
byte *buffer;
size_t nbytes;
if (gcry_mpi_print (GCRYMPI_FMT_USG, NULL, &nbytes, a ))
BUG ();
/* fixme: allocate it on the stack */
buffer = xmalloc (nbytes);
if (gcry_mpi_print( GCRYMPI_FMT_USG, buffer, &nbytes, a ))
BUG ();
if (nbytes < 8) /* oops */
ki[0] = ki[1] = 0;
else
{
memcpy (ki+0, buffer+nbytes-8, 4);
memcpy (ki+1, buffer+nbytes-4, 4);
}
xfree (buffer);
return ki[1];
}
/**************** /****************
* Get the keyid from the secret key and put it into keyid * Get the keyid from the secret key and put it into keyid
* if this is not NULL. Return the 32 low bits of the keyid. * if this is not NULL. Return the 32 low bits of the keyid.
@ -135,18 +170,19 @@ keyid_from_sk( PKT_secret_key *sk, u32 *keyid )
keyid = dummy_keyid; keyid = dummy_keyid;
if( sk->version < 4 && is_RSA(sk->pubkey_algo) ) { if( sk->version < 4 && is_RSA(sk->pubkey_algo) ) {
keyid[0] = keyid[1] = 0;
lowbits = pubkey_get_npkey(sk->pubkey_algo) ? lowbits = pubkey_get_npkey(sk->pubkey_algo) ?
mpi_get_keyid( sk->skey[0], keyid ) : 0; /* take n */ v3_keyid (sk->skey[0], keyid) : 0;
} }
else { else {
const byte *dp; const byte *dp;
MD_HANDLE md; gcry_md_hd_t md;
md = do_fingerprint_md_sk(sk); md = do_fingerprint_md_sk(sk);
dp = md_read( md, 0 ); dp = gcry_md_read ( md, 0 );
keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ; keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ; keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
lowbits = keyid[1]; lowbits = keyid[1];
md_close(md); gcry_md_close (md);
} }
return lowbits; return lowbits;
@ -172,20 +208,21 @@ keyid_from_pk( PKT_public_key *pk, u32 *keyid )
lowbits = keyid[1]; lowbits = keyid[1];
} }
else if( pk->version < 4 && is_RSA(pk->pubkey_algo) ) { else if( pk->version < 4 && is_RSA(pk->pubkey_algo) ) {
keyid[0] = keyid[1] = 0;
lowbits = pubkey_get_npkey(pk->pubkey_algo) ? lowbits = pubkey_get_npkey(pk->pubkey_algo) ?
mpi_get_keyid( pk->pkey[0], keyid ) : 0 ; /* from n */ v3_keyid (pk->pkey[0], keyid) : 0 ;
pk->keyid[0] = keyid[0]; pk->keyid[0] = keyid[0];
pk->keyid[1] = keyid[1]; pk->keyid[1] = keyid[1];
} }
else { else {
const byte *dp; const byte *dp;
MD_HANDLE md; gcry_md_hd_t md;
md = do_fingerprint_md(pk); md = do_fingerprint_md(pk);
dp = md_read( md, 0 ); dp = gcry_md_read ( md, 0 );
keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ; keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ; keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
lowbits = keyid[1]; lowbits = keyid[1];
md_close(md); gcry_md_close (md);
pk->keyid[0] = keyid[0]; pk->keyid[0] = keyid[0];
pk->keyid[1] = keyid[1]; pk->keyid[1] = keyid[1];
} }
@ -246,12 +283,14 @@ namehash_from_uid(PKT_user_id *uid)
{ {
if(uid->namehash==NULL) if(uid->namehash==NULL)
{ {
uid->namehash=m_alloc(20); uid->namehash=xmalloc (20);
if(uid->attrib_data) if(uid->attrib_data)
rmd160_hash_buffer(uid->namehash,uid->attrib_data,uid->attrib_len); gcry_md_hash_buffer (GCRY_MD_RMD160, uid->namehash,
uid->attrib_data,uid->attrib_len);
else else
rmd160_hash_buffer(uid->namehash,uid->name,uid->len); gcry_md_hash_buffer (GCRY_MD_RMD160, uid->namehash,
uid->name,uid->len);
} }
return uid->namehash; return uid->namehash;
@ -427,43 +466,54 @@ colon_expirestr_from_sig (PKT_signature *sig)
byte * byte *
fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len ) fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len )
{ {
byte *p, *buf; byte *buf;
const byte *dp; const byte *dp;
size_t len; size_t len;
unsigned int n;
if( pk->version < 4 && is_RSA(pk->pubkey_algo) ) { if( pk->version < 4 && is_RSA(pk->pubkey_algo) ) {
/* RSA in version 3 packets is special */ /* RSA in version 3 packets is special */
MD_HANDLE md; gcry_md_hd_t md;
md = md_open( DIGEST_ALGO_MD5, 0); gcry_md_open (&md, DIGEST_ALGO_MD5, 0);
if( pubkey_get_npkey( pk->pubkey_algo ) > 1 ) { if( pubkey_get_npkey( pk->pubkey_algo ) > 1 ) {
p = buf = mpi_get_buffer( pk->pkey[0], &n, NULL ); size_t nbytes;
md_write( md, p, n );
m_free(buf); if (gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, pk->pkey[0]))
p = buf = mpi_get_buffer( pk->pkey[1], &n, NULL ); BUG ();
md_write( md, p, n ); /* fixme: allocate it on the stack */
m_free(buf); buf = xmalloc(nbytes);
if (gcry_mpi_print (GCRYMPI_FMT_USG, buf, &nbytes, pk->pkey[0]))
BUG ();
gcry_md_write (md, buf, nbytes);
xfree (buf);
if (gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, pk->pkey[1]))
BUG ();
/* fixme: allocate it on the stack */
buf = xmalloc(nbytes);
if (gcry_mpi_print( GCRYMPI_FMT_USG, buf, &nbytes, pk->pkey[1]))
BUG ();
gcry_md_write( md, buf, nbytes );
xfree(buf);
} }
md_final(md); gcry_md_final (md);
if( !array ) if( !array )
array = m_alloc( 16 ); array = xmalloc ( 16 );
len = 16; len = 16;
memcpy(array, md_read(md, DIGEST_ALGO_MD5), 16 ); memcpy(array, gcry_md_read (md, DIGEST_ALGO_MD5), 16 );
md_close(md); gcry_md_close (md);
} }
else { else {
MD_HANDLE md; gcry_md_hd_t md;
md = do_fingerprint_md(pk); md = do_fingerprint_md(pk);
dp = md_read( md, 0 ); dp = gcry_md_read ( md, 0 );
len = md_digest_length( md_get_algo( md ) ); len = gcry_md_get_algo_dlen (gcry_md_get_algo (md));
assert( len <= MAX_FINGERPRINT_LEN ); assert( len <= MAX_FINGERPRINT_LEN );
if( !array ) if( !array )
array = m_alloc( len ); array = xmalloc ( len );
memcpy(array, dp, len ); memcpy(array, dp, len );
pk->keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ; pk->keyid[0] = dp[12] << 24 | dp[13] << 16 | dp[14] << 8 | dp[15] ;
pk->keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ; pk->keyid[1] = dp[16] << 24 | dp[17] << 16 | dp[18] << 8 | dp[19] ;
md_close(md); gcry_md_close (md);
} }
*ret_len = len; *ret_len = len;
@ -473,41 +523,53 @@ fingerprint_from_pk( PKT_public_key *pk, byte *array, size_t *ret_len )
byte * byte *
fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len ) fingerprint_from_sk( PKT_secret_key *sk, byte *array, size_t *ret_len )
{ {
byte *p, *buf; byte *buf;
const char *dp; const char *dp;
size_t len; size_t len;
unsigned n;
if( sk->version < 4 && is_RSA(sk->pubkey_algo) ) { if( sk->version < 4 && is_RSA(sk->pubkey_algo) ) {
/* RSA in version 3 packets is special */ /* RSA in version 3 packets is special */
MD_HANDLE md; gcry_md_hd_t md;
md = md_open( DIGEST_ALGO_MD5, 0); gcry_md_open (&md, DIGEST_ALGO_MD5, 0);
if( pubkey_get_npkey( sk->pubkey_algo ) > 1 ) { if( pubkey_get_npkey( sk->pubkey_algo ) > 1 ) {
p = buf = mpi_get_buffer( sk->skey[0], &n, NULL ); size_t nbytes;
md_write( md, p, n );
m_free(buf); if (gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, sk->skey[0]))
p = buf = mpi_get_buffer( sk->skey[1], &n, NULL ); BUG ();
md_write( md, p, n ); /* fixme: allocate it on the stack */
m_free(buf); buf = xmalloc(nbytes);
if (gcry_mpi_print (GCRYMPI_FMT_USG, buf, &nbytes, sk->skey[0]))
BUG ();
gcry_md_write (md, buf, nbytes);
xfree (buf);
if (gcry_mpi_print( GCRYMPI_FMT_USG, NULL, &nbytes, sk->skey[1]))
BUG ();
/* fixme: allocate it on the stack */
buf = xmalloc(nbytes);
if (gcry_mpi_print( GCRYMPI_FMT_USG, buf, &nbytes, sk->skey[1]))
BUG ();
gcry_md_write( md, buf, nbytes );
xfree(buf);
} }
md_final(md); gcry_md_final (md);
if( !array ) if( !array )
array = m_alloc( 16 ); array = xmalloc ( 16 );
len = 16; len = 16;
memcpy(array, md_read(md, DIGEST_ALGO_MD5), 16 ); memcpy(array, gcry_md_read (md, DIGEST_ALGO_MD5), 16 );
md_close(md); gcry_md_close (md);
} }
else { else {
MD_HANDLE md; gcry_md_hd_t md;
md = do_fingerprint_md_sk(sk); md = do_fingerprint_md_sk(sk);
dp = md_read( md, 0 ); dp = gcry_md_read ( md, 0 );
len = md_digest_length( md_get_algo( md ) ); len = gcry_md_get_algo_dlen (gcry_md_get_algo (md));
assert( len <= MAX_FINGERPRINT_LEN ); assert( len <= MAX_FINGERPRINT_LEN );
if( !array ) if( !array )
array = m_alloc( len ); array = xmalloc ( len );
memcpy(array, dp, len ); memcpy(array, dp, len );
md_close(md); gcry_md_close (md);
} }
*ret_len = len; *ret_len = len;

View File

@ -88,7 +88,7 @@ print_seckey_info (PKT_secret_key *sk)
p = get_user_id (sk_keyid, &n); p = get_user_id (sk_keyid, &n);
tty_print_utf8_string (p, n); tty_print_utf8_string (p, n);
m_free (p); xfree (p);
tty_printf ("\n"); tty_printf ("\n");
} }
@ -109,7 +109,7 @@ print_pubkey_info (PKT_public_key *pk)
p = get_user_id (pk_keyid, &n); p = get_user_id (pk_keyid, &n);
tty_print_utf8_string (p, n); tty_print_utf8_string (p, n);
m_free (p); xfree (p);
tty_printf ("\n\n"); tty_printf ("\n\n");
} }
@ -126,7 +126,7 @@ show_policy_url(PKT_signature *sig,int indent,int mode)
const byte *p; const byte *p;
size_t len; size_t len;
int seq=0,crit; int seq=0,crit;
FILE *fp=mode?log_stream():stdout; FILE *fp=mode?log_get_stream():stdout;
while((p=enum_sig_subpkt(sig->hashed,SIGSUBPKT_POLICY,&len,&seq,&crit))) while((p=enum_sig_subpkt(sig->hashed,SIGSUBPKT_POLICY,&len,&seq,&crit)))
{ {
@ -168,7 +168,7 @@ show_notation(PKT_signature *sig,int indent,int mode)
const byte *p; const byte *p;
size_t len; size_t len;
int seq=0,crit; int seq=0,crit;
FILE *fp=mode?log_stream():stdout; FILE *fp=mode?log_get_stream():stdout;
/* There may be multiple notations in the same sig. */ /* There may be multiple notations in the same sig. */
@ -254,12 +254,12 @@ list_all( int secret )
hd = keydb_new (secret); hd = keydb_new (secret);
if (!hd) if (!hd)
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
else else
rc = keydb_search_first (hd); rc = keydb_search_first (hd);
if( rc ) { if( rc ) {
if( rc != -1 ) if( rc != -1 )
log_error("keydb_search_first failed: %s\n", g10_errstr(rc) ); log_error("keydb_search_first failed: %s\n", gpg_strerror (rc) );
goto leave; goto leave;
} }
@ -267,7 +267,7 @@ list_all( int secret )
do { do {
rc = keydb_get_keyblock (hd, &keyblock); rc = keydb_get_keyblock (hd, &keyblock);
if (rc) { if (rc) {
log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc)); log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
goto leave; goto leave;
} }
if(!opt.with_colons) if(!opt.with_colons)
@ -291,7 +291,7 @@ list_all( int secret )
keyblock = NULL; keyblock = NULL;
} while (!(rc = keydb_search_next (hd))); } while (!(rc = keydb_search_next (hd)));
if( rc && rc != -1 ) if( rc && rc != -1 )
log_error ("keydb_search_next failed: %s\n", g10_errstr(rc)); log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc));
if(opt.check_sigs && !opt.with_colons) if(opt.check_sigs && !opt.with_colons)
print_signature_stats(&stats); print_signature_stats(&stats);
@ -327,7 +327,7 @@ list_one( STRLIST names, int secret )
if( secret ) { if( secret ) {
rc = get_seckey_bynames( &ctx, NULL, names, &keyblock ); rc = get_seckey_bynames( &ctx, NULL, names, &keyblock );
if( rc ) { if( rc ) {
log_error("error reading key: %s\n", g10_errstr(rc) ); log_error("error reading key: %s\n", gpg_strerror (rc) );
get_seckey_end( ctx ); get_seckey_end( ctx );
return; return;
} }
@ -347,7 +347,7 @@ list_one( STRLIST names, int secret )
else { else {
rc = get_pubkey_bynames( &ctx, NULL, names, &keyblock ); rc = get_pubkey_bynames( &ctx, NULL, names, &keyblock );
if( rc ) { if( rc ) {
log_error("error reading key: %s\n", g10_errstr(rc) ); log_error("error reading key: %s\n", gpg_strerror (rc) );
get_pubkey_end( ctx ); get_pubkey_end( ctx );
return; return;
} }
@ -680,11 +680,11 @@ list_keyblock_print ( KBNODE keyblock, int secret, int fpr, void *opaque )
if( stats ) { if( stats ) {
/*fflush(stdout);*/ /*fflush(stdout);*/
rc = check_key_signature( keyblock, node, NULL ); rc = check_key_signature( keyblock, node, NULL );
switch( rc ) { switch( gpg_err_code (rc) ) {
case 0: sigrc = '!'; break; case 0: sigrc = '!'; break;
case G10ERR_BAD_SIGN: stats->inv_sigs++; sigrc = '-'; break; case GPG_ERR_BAD_SIGNATURE: stats->inv_sigs++; sigrc = '-'; break;
case G10ERR_NO_PUBKEY: case GPG_ERR_NO_PUBKEY:
case G10ERR_UNU_PUBKEY: stats->no_key++; continue; case GPG_ERR_UNUSABLE_PUBKEY: stats->no_key++; continue;
default: stats->oth_err++; sigrc = '%'; break; default: stats->oth_err++; sigrc = '%'; break;
} }
@ -748,14 +748,14 @@ list_keyblock_print ( KBNODE keyblock, int secret, int fpr, void *opaque )
printf("%08lX",(ulong)sig->keyid[1]); printf("%08lX",(ulong)sig->keyid[1]);
printf(" %s ", datestr_from_sig(sig)); printf(" %s ", datestr_from_sig(sig));
if( sigrc == '%' ) if( sigrc == '%' )
printf("[%s] ", g10_errstr(rc) ); printf("[%s] ", gpg_strerror (rc) );
else if( sigrc == '?' ) else if( sigrc == '?' )
; ;
else if ( !opt.fast_list_mode ) { else if ( !opt.fast_list_mode ) {
size_t n; size_t n;
char *p = get_user_id( sig->keyid, &n ); char *p = get_user_id( sig->keyid, &n );
print_utf8_string( stdout, p, n ); print_utf8_string( stdout, p, n );
m_free(p); xfree (p);
} }
putchar('\n'); putchar('\n');
@ -1024,11 +1024,11 @@ list_keyblock_colon( KBNODE keyblock, int secret, int fpr )
if( opt.check_sigs ) { if( opt.check_sigs ) {
fflush(stdout); fflush(stdout);
rc = check_key_signature( keyblock, node, NULL ); rc = check_key_signature( keyblock, node, NULL );
switch( rc ) { switch( gpg_err_code (rc) ) {
case 0: sigrc = '!'; break; case 0: sigrc = '!'; break;
case G10ERR_BAD_SIGN: sigrc = '-'; break; case GPG_ERR_BAD_SIGNATURE: sigrc = '-'; break;
case G10ERR_NO_PUBKEY: case GPG_ERR_NO_PUBKEY:
case G10ERR_UNU_PUBKEY: sigrc = '?'; break; case GPG_ERR_UNUSABLE_PUBKEY: sigrc = '?'; break;
default: sigrc = '%'; break; default: sigrc = '%'; break;
} }
} }
@ -1055,14 +1055,14 @@ list_keyblock_colon( KBNODE keyblock, int secret, int fpr )
printf(":"); printf(":");
if( sigrc == '%' ) if( sigrc == '%' )
printf("[%s] ", g10_errstr(rc) ); printf("[%s] ", gpg_strerror (rc) );
else if( sigrc == '?' ) else if( sigrc == '?' )
; ;
else if ( !opt.fast_list_mode ) { else if ( !opt.fast_list_mode ) {
size_t n; size_t n;
char *p = get_user_id( sig->keyid, &n ); char *p = get_user_id( sig->keyid, &n );
print_string( stdout, p, n, ':' ); print_string( stdout, p, n, ':' );
m_free(p); xfree (p);
} }
printf(":%02x%c:\n", sig->sig_class,sig->flags.exportable?'x':'l'); printf(":%02x%c:\n", sig->sig_class,sig->flags.exportable?'x':'l');
/* fixme: check or list other sigs here */ /* fixme: check or list other sigs here */
@ -1170,14 +1170,14 @@ print_fingerprint (PKT_public_key *pk, PKT_secret_key *sk, int mode )
{ {
if(sk) if(sk)
{ {
PKT_secret_key *primary_sk=m_alloc_clear(sizeof(*primary_sk)); PKT_secret_key *primary_sk=xcalloc (1,sizeof(*primary_sk));
get_seckey(primary_sk,sk->main_keyid); get_seckey(primary_sk,sk->main_keyid);
print_fingerprint(NULL,primary_sk,mode|0x80); print_fingerprint(NULL,primary_sk,mode|0x80);
free_secret_key(primary_sk); free_secret_key(primary_sk);
} }
else else
{ {
PKT_public_key *primary_pk=m_alloc_clear(sizeof(*primary_pk)); PKT_public_key *primary_pk=xcalloc (1,sizeof(*primary_pk));
get_pubkey(primary_pk,pk->main_keyid); get_pubkey(primary_pk,pk->main_keyid);
print_fingerprint(primary_pk,NULL,mode|0x80); print_fingerprint(primary_pk,NULL,mode|0x80);
free_public_key(primary_pk); free_public_key(primary_pk);
@ -1185,7 +1185,7 @@ print_fingerprint (PKT_public_key *pk, PKT_secret_key *sk, int mode )
} }
if (mode == 1) { if (mode == 1) {
fp = log_stream (); fp = log_get_stream ();
if(primary) if(primary)
text = _("Primary key fingerprint:"); text = _("Primary key fingerprint:");
else else

View File

@ -1,5 +1,5 @@
/* keyring.c - keyring file handling /* keyring.c - keyring file handling
* Copyright (C) 2001 Free Software Foundation, Inc. * Copyright (C) 2001, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -28,6 +28,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "keyring.h" #include "keyring.h"
#include "packet.h" #include "packet.h"
@ -72,7 +73,7 @@ struct keyring_handle {
int secret; /* this is for a secret keyring */ int secret; /* this is for a secret keyring */
struct { struct {
CONST_KR_NAME kr; CONST_KR_NAME kr;
IOBUF iobuf; iobuf_t iobuf;
int eof; int eof;
int error; int error;
} current; } current;
@ -101,7 +102,7 @@ new_offset_item (void)
{ {
struct off_item *k; struct off_item *k;
k = m_alloc_clear (sizeof *k); k = xcalloc (1,sizeof *k);
return k; return k;
} }
@ -114,7 +115,7 @@ release_offset_items (struct off_item *k)
for (; k; k = k2) for (; k; k = k2)
{ {
k2 = k->next; k2 = k->next;
m_free (k); xfree (k);
} }
} }
#endif #endif
@ -124,7 +125,7 @@ new_offset_hash_table (void)
{ {
struct off_item **tbl; struct off_item **tbl;
tbl = m_alloc_clear (2048 * sizeof *tbl); tbl = xcalloc (1,2048 * sizeof *tbl);
return tbl; return tbl;
} }
@ -138,7 +139,7 @@ release_offset_hash_table (OffsetHashTable tbl)
return; return;
for (i=0; i < 2048; i++) for (i=0; i < 2048; i++)
release_offset_items (tbl[i]); release_offset_items (tbl[i]);
m_free (tbl); xfree (tbl);
} }
#endif #endif
@ -213,7 +214,7 @@ keyring_register_filename (const char *fname, int secret, void **ptr)
} }
} }
kr = m_alloc (sizeof *kr + strlen (fname)); kr = xmalloc (sizeof *kr + strlen (fname));
strcpy (kr->fname, fname); strcpy (kr->fname, fname);
kr->secret = !!secret; kr->secret = !!secret;
kr->lockhd = NULL; kr->lockhd = NULL;
@ -254,7 +255,7 @@ keyring_new (void *token, int secret)
assert (resource && !resource->secret == !secret); assert (resource && !resource->secret == !secret);
hd = m_alloc_clear (sizeof *hd); hd = xcalloc (1,sizeof *hd);
hd->resource = resource; hd->resource = resource;
hd->secret = !!secret; hd->secret = !!secret;
active_handles++; active_handles++;
@ -268,10 +269,10 @@ keyring_release (KEYRING_HANDLE hd)
return; return;
assert (active_handles > 0); assert (active_handles > 0);
active_handles--; active_handles--;
m_free (hd->word_match.name); xfree (hd->word_match.name);
m_free (hd->word_match.pattern); xfree (hd->word_match.pattern);
iobuf_close (hd->current.iobuf); iobuf_close (hd->current.iobuf);
m_free (hd); xfree (hd);
} }
@ -303,7 +304,7 @@ keyring_lock (KEYRING_HANDLE hd, int yes)
kr->lockhd = create_dotlock( kr->fname ); kr->lockhd = create_dotlock( kr->fname );
if (!kr->lockhd) { if (!kr->lockhd) {
log_info ("can't allocate lock for `%s'\n", kr->fname ); log_info ("can't allocate lock for `%s'\n", kr->fname );
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
} }
} }
} }
@ -318,7 +319,7 @@ keyring_lock (KEYRING_HANDLE hd, int yes)
; ;
else if (make_dotlock (kr->lockhd, -1) ) { else if (make_dotlock (kr->lockhd, -1) ) {
log_info ("can't lock `%s'\n", kr->fname ); log_info ("can't lock `%s'\n", kr->fname );
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
} }
else else
kr->is_locked = 1; kr->is_locked = 1;
@ -355,7 +356,7 @@ keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
PACKET *pkt; PACKET *pkt;
int rc; int rc;
KBNODE keyblock = NULL, node, lastnode; KBNODE keyblock = NULL, node, lastnode;
IOBUF a; iobuf_t a;
int in_cert = 0; int in_cert = 0;
int pk_no = 0; int pk_no = 0;
int uid_no = 0; int uid_no = 0;
@ -370,31 +371,31 @@ keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
a = iobuf_open (hd->found.kr->fname); a = iobuf_open (hd->found.kr->fname);
if (!a) { if (!a) {
log_error ("can't open `%s'\n", hd->found.kr->fname); log_error ("can't open `%s'\n", hd->found.kr->fname);
return G10ERR_KEYRING_OPEN; return GPG_ERR_KEYRING_OPEN;
} }
if (iobuf_seek (a, hd->found.offset) ) { if (iobuf_seek (a, hd->found.offset) ) {
log_error ("can't seek `%s'\n", hd->found.kr->fname); log_error ("can't seek `%s'\n", hd->found.kr->fname);
iobuf_close(a); iobuf_close(a);
return G10ERR_KEYRING_OPEN; return GPG_ERR_KEYRING_OPEN;
} }
pkt = m_alloc (sizeof *pkt); pkt = xmalloc (sizeof *pkt);
init_packet (pkt); init_packet (pkt);
hd->found.n_packets = 0;; hd->found.n_packets = 0;;
lastnode = NULL; lastnode = NULL;
save_mode = set_packet_list_mode(0); save_mode = set_packet_list_mode(0);
while ((rc=parse_packet (a, pkt)) != -1) { while ((rc=parse_packet (a, pkt)) != -1) {
hd->found.n_packets++; hd->found.n_packets++;
if (rc == G10ERR_UNKNOWN_PACKET) { if (rc == GPG_ERR_UNKNOWN_PACKET) {
free_packet (pkt); free_packet (pkt);
init_packet (pkt); init_packet (pkt);
continue; continue;
} }
if (rc) { if (rc) {
log_error ("keyring_get_keyblock: read error: %s\n", log_error ("keyring_get_keyblock: read error: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
rc = G10ERR_INV_KEYRING; rc = GPG_ERR_INV_KEYRING;
break; break;
} }
if (pkt->pkttype == PKT_COMPRESSED) { if (pkt->pkttype == PKT_COMPRESSED) {
@ -448,7 +449,7 @@ keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
} }
} }
pkt = m_alloc (sizeof *pkt); pkt = xmalloc (sizeof *pkt);
init_packet(pkt); init_packet(pkt);
} }
set_packet_list_mode(save_mode); set_packet_list_mode(save_mode);
@ -471,13 +472,13 @@ keyring_get_keyblock (KEYRING_HANDLE hd, KBNODE *ret_kb)
*ret_kb = keyblock; *ret_kb = keyblock;
} }
free_packet (pkt); free_packet (pkt);
m_free (pkt); xfree (pkt);
iobuf_close(a); iobuf_close(a);
/* Make sure that future search operations fail immediately when /* Make sure that future search operations fail immediately when
* we know that we are working on a invalid keyring * we know that we are working on a invalid keyring
*/ */
if (rc == G10ERR_INV_KEYRING) if (rc == GPG_ERR_INV_KEYRING)
hd->current.error = rc; hd->current.error = rc;
return rc; return rc;
@ -495,7 +496,7 @@ keyring_update_keyblock (KEYRING_HANDLE hd, KBNODE kb)
/* need to know the number of packets - do a dummy get_keyblock*/ /* need to know the number of packets - do a dummy get_keyblock*/
rc = keyring_get_keyblock (hd, NULL); rc = keyring_get_keyblock (hd, NULL);
if (rc) { if (rc) {
log_error ("re-reading keyblock failed: %s\n", g10_errstr (rc)); log_error ("re-reading keyblock failed: %s\n", gpg_strerror (rc));
return rc; return rc;
} }
if (!hd->found.n_packets) if (!hd->found.n_packets)
@ -539,7 +540,7 @@ keyring_insert_keyblock (KEYRING_HANDLE hd, KBNODE kb)
fname = hd->resource? hd->resource->fname:NULL; fname = hd->resource? hd->resource->fname:NULL;
if (!fname) if (!fname)
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
/* close this one otherwise we will lose the position for /* close this one otherwise we will lose the position for
* a next search. Fixme: it would be better to adjust the position * a next search. Fixme: it would be better to adjust the position
@ -571,7 +572,7 @@ keyring_delete_keyblock (KEYRING_HANDLE hd)
/* need to know the number of packets - do a dummy get_keyblock*/ /* need to know the number of packets - do a dummy get_keyblock*/
rc = keyring_get_keyblock (hd, NULL); rc = keyring_get_keyblock (hd, NULL);
if (rc) { if (rc) {
log_error ("re-reading keyblock failed: %s\n", g10_errstr (rc)); log_error ("re-reading keyblock failed: %s\n", gpg_strerror (rc));
return rc; return rc;
} }
if (!hd->found.n_packets) if (!hd->found.n_packets)
@ -628,7 +629,7 @@ prepare_search (KEYRING_HANDLE hd)
if (hd->current.kr && !hd->current.eof) { if (hd->current.kr && !hd->current.eof) {
if ( !hd->current.iobuf ) if ( !hd->current.iobuf )
return G10ERR_GENERAL; /* position invalid after a modify */ return GPG_ERR_GENERAL; /* position invalid after a modify */
return 0; /* okay */ return 0; /* okay */
} }
@ -654,8 +655,9 @@ prepare_search (KEYRING_HANDLE hd)
hd->current.eof = 0; hd->current.eof = 0;
hd->current.iobuf = iobuf_open (hd->current.kr->fname); hd->current.iobuf = iobuf_open (hd->current.kr->fname);
if (!hd->current.iobuf) { if (!hd->current.iobuf) {
hd->current.error = gpg_error_from_errno (errno);
log_error ("can't open `%s'\n", hd->current.kr->fname ); log_error ("can't open `%s'\n", hd->current.kr->fname );
return (hd->current.error = G10ERR_OPEN_FILE); return hd->current.error;
} }
return 0; return 0;
@ -774,7 +776,7 @@ prepare_word_match (const byte *name)
int c; int c;
/* the original length is always enough for the pattern */ /* the original length is always enough for the pattern */
p = pattern = m_alloc(strlen(name)+1); p = pattern = xmalloc (strlen(name)+1);
do { do {
/* skip leading delimiters */ /* skip leading delimiters */
while( *name && !word_match_chars[*name] ) while( *name && !word_match_chars[*name] )
@ -951,9 +953,9 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
if ( !hd->word_match.name || strcmp (hd->word_match.name, name) ) if ( !hd->word_match.name || strcmp (hd->word_match.name, name) )
{ {
/* name changed */ /* name changed */
m_free (hd->word_match.name); xfree (hd->word_match.name);
m_free (hd->word_match.pattern); xfree (hd->word_match.pattern);
hd->word_match.name = m_strdup (name); hd->word_match.name = xstrdup (name);
hd->word_match.pattern = prepare_word_match (name); hd->word_match.pattern = prepare_word_match (name);
} }
name = hd->word_match.pattern; name = hd->word_match.pattern;
@ -1069,7 +1071,7 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
goto found; goto found;
break; break;
default: default:
rc = G10ERR_INV_ARG; rc = GPG_ERR_INV_ARG;
goto found; goto found;
} }
} }
@ -1139,7 +1141,7 @@ keyring_search (KEYRING_HANDLE hd, KEYDB_SEARCH_DESC *desc,
static int static int
create_tmp_file (const char *template, create_tmp_file (const char *template,
char **r_bakfname, char **r_tmpfname, IOBUF *r_fp) char **r_bakfname, char **r_tmpfname, iobuf_t *r_fp)
{ {
char *bakfname, *tmpfname; char *bakfname, *tmpfname;
mode_t oldmask; mode_t oldmask;
@ -1156,27 +1158,27 @@ create_tmp_file (const char *template,
if (strlen (template) > 4 if (strlen (template) > 4
&& !strcmp (template+strlen(template)-4, EXTSEP_S "gpg") ) && !strcmp (template+strlen(template)-4, EXTSEP_S "gpg") )
{ {
bakfname = m_alloc (strlen (template) + 1); bakfname = xmalloc (strlen (template) + 1);
strcpy (bakfname, template); strcpy (bakfname, template);
strcpy (bakfname+strlen(template)-4, EXTSEP_S "bak"); strcpy (bakfname+strlen(template)-4, EXTSEP_S "bak");
tmpfname = m_alloc (strlen( template ) + 1 ); tmpfname = xmalloc (strlen( template ) + 1 );
strcpy (tmpfname,template); strcpy (tmpfname,template);
strcpy (tmpfname+strlen(template)-4, EXTSEP_S "tmp"); strcpy (tmpfname+strlen(template)-4, EXTSEP_S "tmp");
} }
else else
{ /* file does not end with gpg; hmmm */ { /* file does not end with gpg; hmmm */
bakfname = m_alloc (strlen( template ) + 5); bakfname = xmalloc (strlen( template ) + 5);
strcpy (stpcpy(bakfname, template), EXTSEP_S "bak"); strcpy (stpcpy(bakfname, template), EXTSEP_S "bak");
tmpfname = m_alloc (strlen( template ) + 5); tmpfname = xmalloc (strlen( template ) + 5);
strcpy (stpcpy(tmpfname, template), EXTSEP_S "tmp"); strcpy (stpcpy(tmpfname, template), EXTSEP_S "tmp");
} }
# else /* Posix file names */ # else /* Posix file names */
bakfname = m_alloc (strlen( template ) + 2); bakfname = xmalloc (strlen( template ) + 2);
strcpy (stpcpy (bakfname,template),"~"); strcpy (stpcpy (bakfname,template),"~");
tmpfname = m_alloc (strlen( template ) + 5); tmpfname = xmalloc (strlen( template ) + 5);
strcpy (stpcpy(tmpfname,template), EXTSEP_S "tmp"); strcpy (stpcpy(tmpfname,template), EXTSEP_S "tmp");
# endif /* Posix filename */ # endif /* Posix filename */
@ -1185,10 +1187,11 @@ create_tmp_file (const char *template,
*r_fp = iobuf_create (tmpfname); *r_fp = iobuf_create (tmpfname);
umask(oldmask); umask(oldmask);
if (!*r_fp) { if (!*r_fp) {
int tmperr = gpg_error_from_errno (errno);
log_error ("can't create `%s': %s\n", tmpfname, strerror(errno) ); log_error ("can't create `%s': %s\n", tmpfname, strerror(errno) );
m_free (tmpfname); xfree (tmpfname);
m_free (bakfname); xfree (bakfname);
return G10ERR_OPEN_FILE; return tmperr;
} }
*r_bakfname = bakfname; *r_bakfname = bakfname;
@ -1216,9 +1219,10 @@ rename_tmp_file (const char *bakfname, const char *tmpfname,
#endif #endif
if (rename (fname, bakfname) ) if (rename (fname, bakfname) )
{ {
int tmperr = gpg_error_from_errno (errno);
log_error ("renaming `%s' to `%s' failed: %s\n", log_error ("renaming `%s' to `%s' failed: %s\n",
fname, bakfname, strerror(errno) ); fname, bakfname, strerror(errno) );
return G10ERR_RENAME_FILE; return tmperr;
} }
} }
@ -1228,9 +1232,9 @@ rename_tmp_file (const char *bakfname, const char *tmpfname,
#endif #endif
if (rename (tmpfname, fname) ) if (rename (tmpfname, fname) )
{ {
rc = gpg_error_from_errno (errno);
log_error ("renaming `%s' to `%s' failed: %s\n", log_error ("renaming `%s' to `%s' failed: %s\n",
tmpfname, fname, strerror(errno) ); tmpfname, fname, strerror(errno) );
rc = G10ERR_RENAME_FILE;
if (secret) if (secret)
{ {
log_info(_("WARNING: 2 files with confidential" log_info(_("WARNING: 2 files with confidential"
@ -1265,7 +1269,7 @@ rename_tmp_file (const char *bakfname, const char *tmpfname,
static int static int
write_keyblock (IOBUF fp, KBNODE keyblock) write_keyblock (iobuf_t fp, KBNODE keyblock)
{ {
KBNODE kbctx = NULL, node; KBNODE kbctx = NULL, node;
int rc; int rc;
@ -1278,7 +1282,7 @@ write_keyblock (IOBUF fp, KBNODE keyblock)
if ( (rc = build_packet (fp, node->pkt) )) if ( (rc = build_packet (fp, node->pkt) ))
{ {
log_error ("build_packet(%d) failed: %s\n", log_error ("build_packet(%d) failed: %s\n",
node->pkt->pkttype, g10_errstr(rc) ); node->pkt->pkttype, gpg_strerror (rc) );
return rc; return rc;
} }
if (node->pkt->pkttype == PKT_SIGNATURE) if (node->pkt->pkttype == PKT_SIGNATURE)
@ -1296,8 +1300,9 @@ write_keyblock (IOBUF fp, KBNODE keyblock)
iobuf_put (fp, 2); /* 2 bytes */ iobuf_put (fp, 2); /* 2 bytes */
iobuf_put (fp, 0); /* unused */ iobuf_put (fp, 0); /* unused */
if (iobuf_put (fp, cacheval)) { if (iobuf_put (fp, cacheval)) {
int tmperr = gpg_error_from_errno (errno);
log_error ("writing sigcache packet failed\n"); log_error ("writing sigcache packet failed\n");
return G10ERR_WRITE_FILE; return tmperr;
} }
} }
} }
@ -1316,7 +1321,7 @@ keyring_rebuild_cache (void *token)
KEYDB_SEARCH_DESC desc; KEYDB_SEARCH_DESC desc;
KBNODE keyblock = NULL, node; KBNODE keyblock = NULL, node;
const char *lastresname = NULL, *resname; const char *lastresname = NULL, *resname;
IOBUF tmpfp = NULL; iobuf_t tmpfp = NULL;
char *tmpfilename = NULL; char *tmpfilename = NULL;
char *bakfilename = NULL; char *bakfilename = NULL;
int rc; int rc;
@ -1340,9 +1345,9 @@ keyring_rebuild_cache (void *token)
{ {
if (iobuf_close (tmpfp)) if (iobuf_close (tmpfp))
{ {
rc = gpg_error_from_errno (errno);
log_error ("error closing `%s': %s\n", log_error ("error closing `%s': %s\n",
tmpfilename, strerror (errno)); tmpfilename, strerror (errno));
rc = G10ERR_CLOSE_FILE;
goto leave; goto leave;
} }
/* because we have switched resources, we can be sure that /* because we have switched resources, we can be sure that
@ -1351,8 +1356,8 @@ keyring_rebuild_cache (void *token)
} }
rc = lastresname? rename_tmp_file (bakfilename, tmpfilename, rc = lastresname? rename_tmp_file (bakfilename, tmpfilename,
lastresname, 0) : 0; lastresname, 0) : 0;
m_free (tmpfilename); tmpfilename = NULL; xfree (tmpfilename); tmpfilename = NULL;
m_free (bakfilename); bakfilename = NULL; xfree (bakfilename); bakfilename = NULL;
if (rc) if (rc)
goto leave; goto leave;
lastresname = resname; lastresname = resname;
@ -1367,7 +1372,7 @@ keyring_rebuild_cache (void *token)
rc = keyring_get_keyblock (hd, &keyblock); rc = keyring_get_keyblock (hd, &keyblock);
if (rc) if (rc)
{ {
log_error ("keyring_get_keyblock failed: %s\n", g10_errstr(rc)); log_error ("keyring_get_keyblock failed: %s\n", gpg_strerror (rc));
goto leave; goto leave;
} }
assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY); assert (keyblock->pkt->pkttype == PKT_PUBLIC_KEY);
@ -1396,7 +1401,7 @@ keyring_rebuild_cache (void *token)
rc = 0; rc = 0;
if (rc) if (rc)
{ {
log_error ("keyring_search failed: %s\n", g10_errstr(rc)); log_error ("keyring_search failed: %s\n", gpg_strerror (rc));
goto leave; goto leave;
} }
log_info(_("%lu keys checked (%lu signatures)\n"), count, sigcount ); log_info(_("%lu keys checked (%lu signatures)\n"), count, sigcount );
@ -1404,9 +1409,9 @@ keyring_rebuild_cache (void *token)
{ {
if (iobuf_close (tmpfp)) if (iobuf_close (tmpfp))
{ {
rc = gpg_error_from_errno (errno);
log_error ("error closing `%s': %s\n", log_error ("error closing `%s': %s\n",
tmpfilename, strerror (errno)); tmpfilename, strerror (errno));
rc = G10ERR_CLOSE_FILE;
goto leave; goto leave;
} }
/* because we have switched resources, we can be sure that /* because we have switched resources, we can be sure that
@ -1415,14 +1420,14 @@ keyring_rebuild_cache (void *token)
} }
rc = lastresname? rename_tmp_file (bakfilename, tmpfilename, rc = lastresname? rename_tmp_file (bakfilename, tmpfilename,
lastresname, 0) : 0; lastresname, 0) : 0;
m_free (tmpfilename); tmpfilename = NULL; xfree (tmpfilename); tmpfilename = NULL;
m_free (bakfilename); bakfilename = NULL; xfree (bakfilename); bakfilename = NULL;
leave: leave:
if (tmpfp) if (tmpfp)
iobuf_cancel (tmpfp); iobuf_cancel (tmpfp);
m_free (tmpfilename); xfree (tmpfilename);
m_free (bakfilename); xfree (bakfilename);
release_kbnode (keyblock); release_kbnode (keyblock);
keyring_lock (hd, 0); keyring_lock (hd, 0);
keyring_release (hd); keyring_release (hd);
@ -1440,7 +1445,7 @@ static int
do_copy (int mode, const char *fname, KBNODE root, int secret, do_copy (int mode, const char *fname, KBNODE root, int secret,
off_t start_offset, unsigned int n_packets ) off_t start_offset, unsigned int n_packets )
{ {
IOBUF fp, newfp; iobuf_t fp, newfp;
int rc=0; int rc=0;
char *bakfname = NULL; char *bakfname = NULL;
char *tmpfname = NULL; char *tmpfname = NULL;
@ -1448,7 +1453,8 @@ do_copy (int mode, const char *fname, KBNODE root, int secret,
/* Open the source file. Because we do a rname, we have to check the /* Open the source file. Because we do a rname, we have to check the
permissions of the file */ permissions of the file */
if (access (fname, W_OK)) if (access (fname, W_OK))
return G10ERR_WRITE_FILE; return gpg_error_from_errno (errno);
fp = iobuf_open (fname); fp = iobuf_open (fname);
if (mode == 1 && !fp && errno == ENOENT) { if (mode == 1 && !fp && errno == ENOENT) {
@ -1460,9 +1466,10 @@ do_copy (int mode, const char *fname, KBNODE root, int secret,
newfp = iobuf_create (fname); newfp = iobuf_create (fname);
umask(oldmask); umask(oldmask);
if( !newfp ) { if( !newfp ) {
int tmperr = gpg_error_from_errno (errno);
log_error (_("%s: can't create: %s\n"), log_error (_("%s: can't create: %s\n"),
fname, strerror(errno)); fname, strerror(errno));
return G10ERR_OPEN_FILE; return tmperr;
} }
if( !opt.quiet ) if( !opt.quiet )
log_info(_("%s: keyring created\n"), fname ); log_info(_("%s: keyring created\n"), fname );
@ -1471,21 +1478,22 @@ do_copy (int mode, const char *fname, KBNODE root, int secret,
while ( (node = walk_kbnode( root, &kbctx, 0 )) ) { while ( (node = walk_kbnode( root, &kbctx, 0 )) ) {
if( (rc = build_packet( newfp, node->pkt )) ) { if( (rc = build_packet( newfp, node->pkt )) ) {
log_error("build_packet(%d) failed: %s\n", log_error("build_packet(%d) failed: %s\n",
node->pkt->pkttype, g10_errstr(rc) ); node->pkt->pkttype, gpg_strerror (rc) );
iobuf_cancel(newfp); iobuf_cancel(newfp);
return G10ERR_WRITE_FILE; return rc;
} }
} }
if( iobuf_close(newfp) ) { if (iobuf_close(newfp)) {
int tmperr = gpg_error_from_errno (errno);
log_error ("%s: close failed: %s\n", fname, strerror(errno)); log_error ("%s: close failed: %s\n", fname, strerror(errno));
return G10ERR_CLOSE_FILE; return tmperr;
} }
return 0; /* ready */ return 0; /* ready */
} }
if( !fp ) { if( !fp ) {
rc = gpg_error_from_errno (errno);
log_error ("%s: can't open: %s\n", fname, strerror(errno) ); log_error ("%s: can't open: %s\n", fname, strerror(errno) );
rc = G10ERR_OPEN_FILE;
goto leave; goto leave;
} }
@ -1500,7 +1508,7 @@ do_copy (int mode, const char *fname, KBNODE root, int secret,
rc = copy_all_packets (fp, newfp); rc = copy_all_packets (fp, newfp);
if( rc != -1 ) { if( rc != -1 ) {
log_error("%s: copy to `%s' failed: %s\n", log_error("%s: copy to `%s' failed: %s\n",
fname, tmpfname, g10_errstr(rc) ); fname, tmpfname, gpg_strerror (rc) );
iobuf_close(fp); iobuf_close(fp);
iobuf_cancel(newfp); iobuf_cancel(newfp);
goto leave; goto leave;
@ -1513,7 +1521,7 @@ do_copy (int mode, const char *fname, KBNODE root, int secret,
rc = copy_some_packets( fp, newfp, start_offset ); rc = copy_some_packets( fp, newfp, start_offset );
if( rc ) { /* should never get EOF here */ if( rc ) { /* should never get EOF here */
log_error ("%s: copy to `%s' failed: %s\n", log_error ("%s: copy to `%s' failed: %s\n",
fname, tmpfname, g10_errstr(rc) ); fname, tmpfname, gpg_strerror (rc) );
iobuf_close(fp); iobuf_close(fp);
iobuf_cancel(newfp); iobuf_cancel(newfp);
goto leave; goto leave;
@ -1523,7 +1531,7 @@ do_copy (int mode, const char *fname, KBNODE root, int secret,
rc = skip_some_packets( fp, n_packets ); rc = skip_some_packets( fp, n_packets );
if( rc ) { if( rc ) {
log_error("%s: skipping %u packets failed: %s\n", log_error("%s: skipping %u packets failed: %s\n",
fname, n_packets, g10_errstr(rc)); fname, n_packets, gpg_strerror (rc));
iobuf_close(fp); iobuf_close(fp);
iobuf_cancel(newfp); iobuf_cancel(newfp);
goto leave; goto leave;
@ -1544,7 +1552,7 @@ do_copy (int mode, const char *fname, KBNODE root, int secret,
rc = copy_all_packets( fp, newfp ); rc = copy_all_packets( fp, newfp );
if( rc != -1 ) { if( rc != -1 ) {
log_error("%s: copy to `%s' failed: %s\n", log_error("%s: copy to `%s' failed: %s\n",
fname, tmpfname, g10_errstr(rc) ); fname, tmpfname, gpg_strerror (rc) );
iobuf_close(fp); iobuf_close(fp);
iobuf_cancel(newfp); iobuf_cancel(newfp);
goto leave; goto leave;
@ -1554,20 +1562,20 @@ do_copy (int mode, const char *fname, KBNODE root, int secret,
/* close both files */ /* close both files */
if( iobuf_close(fp) ) { if( iobuf_close(fp) ) {
rc = gpg_error_from_errno (errno);
log_error("%s: close failed: %s\n", fname, strerror(errno) ); log_error("%s: close failed: %s\n", fname, strerror(errno) );
rc = G10ERR_CLOSE_FILE;
goto leave; goto leave;
} }
if( iobuf_close(newfp) ) { if( iobuf_close(newfp) ) {
rc = gpg_error_from_errno (errno);
log_error("%s: close failed: %s\n", tmpfname, strerror(errno) ); log_error("%s: close failed: %s\n", tmpfname, strerror(errno) );
rc = G10ERR_CLOSE_FILE;
goto leave; goto leave;
} }
rc = rename_tmp_file (bakfname, tmpfname, fname, secret); rc = rename_tmp_file (bakfname, tmpfname, fname, secret);
leave: leave:
m_free(bakfname); xfree (bakfname);
m_free(tmpfname); xfree (tmpfname);
return rc; return rc;
} }

View File

@ -5,7 +5,7 @@
#include <time.h> #include <time.h>
#include "keyserver.h" #include "keyserver.h"
#include "iobuf.h" #include "../common/iobuf.h"
#include "types.h" #include "types.h"
void parse_keyserver_options(char *options); void parse_keyserver_options(char *options);

View File

@ -1,5 +1,5 @@
/* keyserver.c - generic keyserver code /* keyserver.c - generic keyserver code
* Copyright (C) 2001, 2002 Free Software Foundation, Inc. * Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -24,6 +24,8 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "filter.h" #include "filter.h"
#include "keydb.h" #include "keydb.h"
#include "status.h" #include "status.h"
@ -48,7 +50,7 @@ struct keyrec
time_t createtime,expiretime; time_t createtime,expiretime;
int size,flags; int size,flags;
byte type; byte type;
IOBUF uidbuf; iobuf_t uidbuf;
int lines; int lines;
}; };
@ -186,7 +188,7 @@ parse_keyserver_uri(char *uri,const char *configname,unsigned int configlineno)
/* Get the host */ /* Get the host */
opt.keyserver_host=strsep(&uri,":/"); opt.keyserver_host=strsep(&uri,":/");
if(opt.keyserver_host[0]=='\0') if(opt.keyserver_host[0]=='\0')
return G10ERR_BAD_URI; return GPG_ERR_BAD_URI;
if(uri==NULL || uri[0]=='\0') if(uri==NULL || uri[0]=='\0')
opt.keyserver_port=NULL; opt.keyserver_port=NULL;
@ -202,7 +204,7 @@ parse_keyserver_uri(char *uri,const char *configname,unsigned int configlineno)
while(*ch!='\0') while(*ch!='\0')
{ {
if(!isdigit(*ch)) if(!isdigit(*ch))
return G10ERR_BAD_URI; return GPG_ERR_BAD_URI;
ch++; ch++;
} }
@ -227,11 +229,11 @@ parse_keyserver_uri(char *uri,const char *configname,unsigned int configlineno)
{ {
/* One slash means absolute path. We don't need to support that /* One slash means absolute path. We don't need to support that
yet. */ yet. */
return G10ERR_BAD_URI; return GPG_ERR_BAD_URI;
} }
if(opt.keyserver_scheme[0]=='\0') if(opt.keyserver_scheme[0]=='\0')
return G10ERR_BAD_URI; return GPG_ERR_BAD_URI;
return 0; return 0;
} }
@ -250,7 +252,7 @@ print_keyrec(int number,struct keyrec *keyrec)
if(keyrec->type) if(keyrec->type)
{ {
const char *str=pubkey_algo_to_string(keyrec->type); const char *str = gcry_pk_algo_name (keyrec->type);
if(str) if(str)
printf("%s ",str); printf("%s ",str);
@ -319,7 +321,7 @@ parse_keyrec(char *keystring)
return NULL; return NULL;
else if(work->desc.mode==KEYDB_SEARCH_MODE_NONE) else if(work->desc.mode==KEYDB_SEARCH_MODE_NONE)
{ {
m_free(work); xfree (work);
return NULL; return NULL;
} }
else else
@ -332,7 +334,7 @@ parse_keyrec(char *keystring)
if(work==NULL) if(work==NULL)
{ {
work=m_alloc_clear(sizeof(struct keyrec)); work=xcalloc (1,sizeof(struct keyrec));
work->uidbuf=iobuf_temp(); work->uidbuf=iobuf_temp();
} }
@ -353,7 +355,7 @@ parse_keyrec(char *keystring)
if(work->desc.mode) if(work->desc.mode)
{ {
ret=work; ret=work;
work=m_alloc_clear(sizeof(struct keyrec)); work=xcalloc (1,sizeof(struct keyrec));
work->uidbuf=iobuf_temp(); work->uidbuf=iobuf_temp();
} }
@ -458,7 +460,7 @@ parse_keyrec(char *keystring)
decoded=utf8_to_native(userid,i,0); decoded=utf8_to_native(userid,i,0);
iobuf_writestr(work->uidbuf,decoded); iobuf_writestr(work->uidbuf,decoded);
m_free(decoded); xfree (decoded);
iobuf_writestr(work->uidbuf,"\n\t"); iobuf_writestr(work->uidbuf,"\n\t");
work->lines++; work->lines++;
} }
@ -496,7 +498,7 @@ show_prompt(KEYDB_SEARCH_DESC *desc,int numdesc,int count,const char *search)
if(answer[0]=='q' || answer[0]=='Q') if(answer[0]=='q' || answer[0]=='Q')
{ {
m_free(answer); xfree (answer);
return 1; return 1;
} }
else if(atoi(answer)>=1 && atoi(answer)<=numdesc) else if(atoi(answer)>=1 && atoi(answer)<=numdesc)
@ -507,7 +509,7 @@ show_prompt(KEYDB_SEARCH_DESC *desc,int numdesc,int count,const char *search)
if(atoi(num)>=1 && atoi(num)<=numdesc) if(atoi(num)>=1 && atoi(num)<=numdesc)
keyserver_work(GET,NULL,&desc[atoi(num)-1],1); keyserver_work(GET,NULL,&desc[atoi(num)-1],1);
m_free(answer); xfree (answer);
return 1; return 1;
} }
@ -518,7 +520,7 @@ show_prompt(KEYDB_SEARCH_DESC *desc,int numdesc,int count,const char *search)
small, it will grow safely. If negative it disables the "Key x-y small, it will grow safely. If negative it disables the "Key x-y
of z" messages. */ of z" messages. */
static void static void
keyserver_search_prompt(IOBUF buffer,const char *searchstr) keyserver_search_prompt(iobuf_t buffer,const char *searchstr)
{ {
int i=0,validcount=0,started=0,header=0,count=1; int i=0,validcount=0,started=0,header=0,count=1;
unsigned int maxlen,buflen; unsigned int maxlen,buflen;
@ -527,7 +529,7 @@ keyserver_search_prompt(IOBUF buffer,const char *searchstr)
/* TODO: Something other than 23? That's 24-1 (the prompt). */ /* TODO: Something other than 23? That's 24-1 (the prompt). */
int maxlines=23,numlines=0; int maxlines=23,numlines=0;
desc=m_alloc(count*sizeof(KEYDB_SEARCH_DESC)); desc=xmalloc (count*sizeof(KEYDB_SEARCH_DESC));
for(;;) for(;;)
{ {
@ -582,7 +584,7 @@ keyserver_search_prompt(IOBUF buffer,const char *searchstr)
else else
validcount=1; validcount=1;
desc=m_realloc(desc,count*sizeof(KEYDB_SEARCH_DESC)); desc=xrealloc(desc,count*sizeof(KEYDB_SEARCH_DESC));
} }
started=1; started=1;
@ -622,7 +624,7 @@ keyserver_search_prompt(IOBUF buffer,const char *searchstr)
/* keyserver helper sent more keys than they claimed in the /* keyserver helper sent more keys than they claimed in the
info: line. */ info: line. */
count+=10; count+=10;
desc=m_realloc(desc,count*sizeof(KEYDB_SEARCH_DESC)); desc=xrealloc(desc,count*sizeof(KEYDB_SEARCH_DESC));
validcount=0; validcount=0;
} }
@ -645,15 +647,15 @@ keyserver_search_prompt(IOBUF buffer,const char *searchstr)
numlines+=keyrec->lines; numlines+=keyrec->lines;
iobuf_close(keyrec->uidbuf); iobuf_close(keyrec->uidbuf);
m_free(keyrec); xfree (keyrec);
started=1; started=1;
i++; i++;
} }
} }
m_free(desc); xfree (desc);
m_free(line); xfree (line);
notfound: notfound:
if(count==0) if(count==0)
@ -694,7 +696,7 @@ keyserver_spawn(int action,STRLIST list,
#endif #endif
/* Build the filename for the helper to execute */ /* Build the filename for the helper to execute */
command=m_alloc(strlen("gpgkeys_")+strlen(opt.keyserver_scheme)+1); command=xmalloc (strlen("gpgkeys_")+strlen(opt.keyserver_scheme)+1);
strcpy(command,"gpgkeys_"); strcpy(command,"gpgkeys_");
strcat(command,opt.keyserver_scheme); strcat(command,opt.keyserver_scheme);
@ -702,13 +704,13 @@ keyserver_spawn(int action,STRLIST list,
{ {
if(opt.keyserver_options.keep_temp_files) if(opt.keyserver_options.keep_temp_files)
{ {
command=m_realloc(command,strlen(command)+ command=xrealloc(command,strlen(command)+
strlen(KEYSERVER_ARGS_KEEP)+1); strlen(KEYSERVER_ARGS_KEEP)+1);
strcat(command,KEYSERVER_ARGS_KEEP); strcat(command,KEYSERVER_ARGS_KEEP);
} }
else else
{ {
command=m_realloc(command,strlen(command)+ command=xrealloc(command,strlen(command)+
strlen(KEYSERVER_ARGS_NOKEEP)+1); strlen(KEYSERVER_ARGS_NOKEEP)+1);
strcat(command,KEYSERVER_ARGS_NOKEEP); strcat(command,KEYSERVER_ARGS_NOKEEP);
} }
@ -806,7 +808,7 @@ keyserver_spawn(int action,STRLIST list,
for(key=list;key!=NULL;key=key->next) for(key=list;key!=NULL;key=key->next)
{ {
armor_filter_context_t afx; armor_filter_context_t afx;
IOBUF buffer=iobuf_temp(); iobuf_t buffer=iobuf_temp();
KBNODE block; KBNODE block;
temp=NULL; temp=NULL;
@ -930,13 +932,13 @@ keyserver_spawn(int action,STRLIST list,
fprintf(spawn->tochild,"%s\n",key->d); fprintf(spawn->tochild,"%s\n",key->d);
if(key!=list) if(key!=list)
{ {
searchstr=m_realloc(searchstr, searchstr=xrealloc(searchstr,
strlen(searchstr)+strlen(key->d)+2); strlen(searchstr)+strlen(key->d)+2);
strcat(searchstr," "); strcat(searchstr," ");
} }
else else
{ {
searchstr=m_alloc(strlen(key->d)+1); searchstr=xmalloc (strlen(key->d)+1);
searchstr[0]='\0'; searchstr[0]='\0';
} }
@ -968,7 +970,7 @@ keyserver_spawn(int action,STRLIST list,
maxlen=1024; maxlen=1024;
if(iobuf_read_line(spawn->fromchild,&line,&buflen,&maxlen)==0) if(iobuf_read_line(spawn->fromchild,&line,&buflen,&maxlen)==0)
{ {
ret=G10ERR_READ_FILE; ret = iobuf_error (spawn->fromchild);
goto fail; /* i.e. EOF */ goto fail; /* i.e. EOF */
} }
@ -1052,7 +1054,7 @@ keyserver_spawn(int action,STRLIST list,
} }
fail: fail:
m_free(line); xfree (line);
*prog=exec_finish(spawn); *prog=exec_finish(spawn);
@ -1067,13 +1069,13 @@ keyserver_work(int action,STRLIST list,KEYDB_SEARCH_DESC *desc,int count)
if(opt.keyserver_scheme==NULL) if(opt.keyserver_scheme==NULL)
{ {
log_error(_("no keyserver known (use option --keyserver)\n")); log_error(_("no keyserver known (use option --keyserver)\n"));
return G10ERR_BAD_URI; return GPG_ERR_BAD_URI;
} }
#ifdef DISABLE_KEYSERVER_HELPERS #ifdef DISABLE_KEYSERVER_HELPERS
log_error(_("external keyserver calls are not supported in this build\n")); log_error(_("external keyserver calls are not supported in this build\n"));
return G10ERR_KEYSERVER; return GPG_ERR_KEYSERVER;
#else #else
/* Spawn a handler */ /* Spawn a handler */
@ -1107,12 +1109,12 @@ keyserver_work(int action,STRLIST list,KEYDB_SEARCH_DESC *desc,int count)
break; break;
} }
return G10ERR_KEYSERVER; return GPG_ERR_KEYSERVER;
} }
if(rc) if(rc)
{ {
log_error(_("keyserver communications error: %s\n"),g10_errstr(rc)); log_error(_("keyserver communications error: %s\n"),gpg_strerror (rc));
return rc; return rc;
} }
@ -1146,7 +1148,7 @@ keyserver_import(STRLIST users)
int rc=0; int rc=0;
/* Build a list of key ids */ /* Build a list of key ids */
desc=m_alloc(sizeof(KEYDB_SEARCH_DESC)*num); desc=xmalloc (sizeof(KEYDB_SEARCH_DESC)*num);
for(;users;users=users->next) for(;users;users=users->next)
{ {
@ -1164,14 +1166,14 @@ keyserver_import(STRLIST users)
if(count==num) if(count==num)
{ {
num+=100; num+=100;
desc=m_realloc(desc,sizeof(KEYDB_SEARCH_DESC)*num); desc=xrealloc(desc,sizeof(KEYDB_SEARCH_DESC)*num);
} }
} }
if(count>0) if(count>0)
rc=keyserver_work(GET,NULL,desc,count); rc=keyserver_work(GET,NULL,desc,count);
m_free(desc); xfree (desc);
return rc; return rc;
} }
@ -1221,21 +1223,21 @@ keyidlist(STRLIST users,KEYDB_SEARCH_DESC **klist,int *count,int fakev3)
*count=0; *count=0;
*klist=m_alloc(sizeof(KEYDB_SEARCH_DESC)*num); *klist=xmalloc (sizeof(KEYDB_SEARCH_DESC)*num);
kdbhd=keydb_new(0); kdbhd=keydb_new(0);
if(!users) if(!users)
{ {
ndesc = 1; ndesc = 1;
desc = m_alloc_clear ( ndesc * sizeof *desc); desc = xcalloc (1, ndesc * sizeof *desc);
desc[0].mode = KEYDB_SEARCH_MODE_FIRST; desc[0].mode = KEYDB_SEARCH_MODE_FIRST;
} }
else else
{ {
for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++) for (ndesc=0, sl=users; sl; sl = sl->next, ndesc++)
; ;
desc = m_alloc ( ndesc * sizeof *desc); desc = xmalloc ( ndesc * sizeof *desc);
for (ndesc=0, sl=users; sl; sl = sl->next) for (ndesc=0, sl=users; sl; sl = sl->next)
{ {
@ -1243,7 +1245,7 @@ keyidlist(STRLIST users,KEYDB_SEARCH_DESC **klist,int *count,int fakev3)
ndesc++; ndesc++;
else else
log_error (_("key `%s' not found: %s\n"), log_error (_("key `%s' not found: %s\n"),
sl->d, g10_errstr (G10ERR_INV_USER_ID)); sl->d, gpg_strerror (GPG_ERR_INV_USER_ID));
} }
} }
@ -1256,7 +1258,7 @@ keyidlist(STRLIST users,KEYDB_SEARCH_DESC **klist,int *count,int fakev3)
rc = keydb_get_keyblock (kdbhd, &keyblock ); rc = keydb_get_keyblock (kdbhd, &keyblock );
if( rc ) if( rc )
{ {
log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) ); log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc) );
goto leave; goto leave;
} }
@ -1273,14 +1275,14 @@ keyidlist(STRLIST users,KEYDB_SEARCH_DESC **klist,int *count,int fakev3)
node->pkt->pkt.public_key->version>=4) node->pkt->pkt.public_key->version>=4)
{ {
(*klist)[*count].mode=KEYDB_SEARCH_MODE_LONG_KID; (*klist)[*count].mode=KEYDB_SEARCH_MODE_LONG_KID;
mpi_get_keyid(node->pkt->pkt.public_key->pkey[0], v3_keyid (node->pkt->pkt.public_key->pkey[0],
(*klist)[*count].u.kid); (*klist)[*count].u.kid);
(*count)++; (*count)++;
if(*count==num) if(*count==num)
{ {
num+=100; num+=100;
*klist=m_realloc(*klist,sizeof(KEYDB_SEARCH_DESC)*num); *klist=xrealloc(*klist,sizeof(KEYDB_SEARCH_DESC)*num);
} }
} }
@ -1308,7 +1310,7 @@ keyidlist(STRLIST users,KEYDB_SEARCH_DESC **klist,int *count,int fakev3)
if(*count==num) if(*count==num)
{ {
num+=100; num+=100;
*klist=m_realloc(*klist,sizeof(KEYDB_SEARCH_DESC)*num); *klist=xrealloc(*klist,sizeof(KEYDB_SEARCH_DESC)*num);
} }
} }
} }
@ -1317,7 +1319,7 @@ keyidlist(STRLIST users,KEYDB_SEARCH_DESC **klist,int *count,int fakev3)
rc=0; rc=0;
leave: leave:
m_free(desc); xfree (desc);
keydb_release(kdbhd); keydb_release(kdbhd);
release_kbnode(keyblock); release_kbnode(keyblock);
@ -1363,7 +1365,7 @@ keyserver_refresh(STRLIST users)
rc=keyserver_work(GET,NULL,desc,count); rc=keyserver_work(GET,NULL,desc,count);
} }
m_free(desc); xfree (desc);
return rc; return rc;
} }

View File

@ -20,7 +20,8 @@
#ifndef G10_MAIN_H #ifndef G10_MAIN_H
#define G10_MAIN_H #define G10_MAIN_H
#include "types.h" #include "types.h"
#include "iobuf.h" #include "gpg.h"
#include "../common/iobuf.h"
#include "mpi.h" #include "mpi.h"
#include "cipher.h" #include "cipher.h"
#include "keydb.h" #include "keydb.h"
@ -65,13 +66,16 @@ void trap_unaligned(void);
int disable_core_dumps(void); int disable_core_dumps(void);
u16 checksum_u16( unsigned n ); u16 checksum_u16( unsigned n );
u16 checksum( byte *p, unsigned n ); u16 checksum( byte *p, unsigned n );
u16 checksum_mpi( MPI a ); u16 checksum_mpi( gcry_mpi_t a );
u32 buffer_to_u32( const byte *buffer ); u32 buffer_to_u32( const byte *buffer );
const byte *get_session_marker( size_t *rlen ); const byte *get_session_marker( size_t *rlen );
int openpgp_cipher_test_algo( int algo ); int openpgp_cipher_test_algo( int algo );
int openpgp_pk_test_algo( int algo, unsigned int usage_flags ); int openpgp_pk_test_algo( int algo, unsigned int usage_flags );
int openpgp_pk_algo_usage ( int algo ); int openpgp_pk_algo_usage ( int algo );
int openpgp_md_test_algo( int algo ); int openpgp_md_test_algo( int algo );
int openpgp_md_map_name (const char *string);
int openpgp_cipher_map_name (const char *string);
int openpgp_pk_map_name (const char *string);
#ifdef USE_IDEA #ifdef USE_IDEA
void idea_cipher_warn( int show ); void idea_cipher_warn( int show );
@ -106,6 +110,24 @@ struct parse_options
int parse_options(char *str,unsigned int *options,struct parse_options *opts); int parse_options(char *str,unsigned int *options,struct parse_options *opts);
/* Temporary helpers. */
int pubkey_get_npkey( int algo );
int pubkey_get_nskey( int algo );
int pubkey_get_nsig( int algo );
int pubkey_get_nenc( int algo );
unsigned int pubkey_nbits( int algo, gcry_mpi_t *pkey );
/* MPI helpers. */
int mpi_write( iobuf_t out, gcry_mpi_t a );
int mpi_write_opaque( iobuf_t out, gcry_mpi_t a );
gcry_mpi_t mpi_read(iobuf_t inp, unsigned int *ret_nread, int secure );
gcry_mpi_t mpi_read_opaque(iobuf_t inp, unsigned int *ret_nread );
int mpi_print( FILE *fp, gcry_mpi_t a, int mode );
/*-- helptext.c --*/ /*-- helptext.c --*/
void display_online_help( const char *keyword ); void display_online_help( const char *keyword );
@ -115,7 +137,7 @@ int encode_store( const char *filename );
int encode_crypt( const char *filename, STRLIST remusr ); int encode_crypt( const char *filename, STRLIST remusr );
void encode_crypt_files(int nfiles, char **files, STRLIST remusr); void encode_crypt_files(int nfiles, char **files, STRLIST remusr);
int encrypt_filter( void *opaque, int control, int encrypt_filter( void *opaque, int control,
IOBUF a, byte *buf, size_t *ret_len); iobuf_t a, byte *buf, size_t *ret_len);
/*-- sign.c --*/ /*-- sign.c --*/
@ -155,25 +177,25 @@ int generate_subkeypair( KBNODE pub_keyblock, KBNODE sec_keyblock );
int overwrite_filep( const char *fname ); int overwrite_filep( const char *fname );
char *make_outfile_name( const char *iname ); char *make_outfile_name( const char *iname );
char *ask_outfile_name( const char *name, size_t namelen ); char *ask_outfile_name( const char *name, size_t namelen );
int open_outfile( const char *iname, int mode, IOBUF *a ); int open_outfile( const char *iname, int mode, iobuf_t *a );
IOBUF open_sigfile( const char *iname, progress_filter_context_t *pfx ); iobuf_t open_sigfile( const char *iname, progress_filter_context_t *pfx );
void try_make_homedir( const char *fname ); void try_make_homedir( const char *fname );
/*-- seskey.c --*/ /*-- seskey.c --*/
void make_session_key( DEK *dek ); void make_session_key( DEK *dek );
MPI encode_session_key( DEK *dek, unsigned nbits ); gcry_mpi_t encode_session_key( DEK *dek, unsigned nbits );
MPI encode_md_value( int pubkey_algo, MD_HANDLE md, gcry_mpi_t encode_md_value( int pubkey_algo, MD_HANDLE md,
int hash_algo, unsigned nbits, int v3compathack ); int hash_algo, unsigned nbits, int v3compathack );
/*-- comment.c --*/ /*-- comment.c --*/
KBNODE make_comment_node( const char *s ); KBNODE make_comment_node( const char *s );
KBNODE make_mpi_comment_node( const char *s, MPI a ); KBNODE make_mpi_comment_node( const char *s, gcry_mpi_t a );
/*-- import.c --*/ /*-- import.c --*/
int parse_import_options(char *str,unsigned int *options); int parse_import_options(char *str,unsigned int *options);
void import_keys( char **fnames, int nnames, void import_keys( char **fnames, int nnames,
void *stats_hd, unsigned int options ); void *stats_hd, unsigned int options );
int import_keys_stream( IOBUF inp, int import_keys_stream( iobuf_t inp,
void *stats_hd, unsigned int options ); void *stats_hd, unsigned int options );
void *import_new_stats_handle (void); void *import_new_stats_handle (void);
void import_release_stats_handle (void *p); void import_release_stats_handle (void *p);
@ -184,7 +206,7 @@ int collapse_uids( KBNODE *keyblock );
/*-- export.c --*/ /*-- export.c --*/
int parse_export_options(char *str,unsigned int *options); int parse_export_options(char *str,unsigned int *options);
int export_pubkeys( STRLIST users, unsigned int options ); int export_pubkeys( STRLIST users, unsigned int options );
int export_pubkeys_stream( IOBUF out, STRLIST users, int export_pubkeys_stream( iobuf_t out, STRLIST users,
KBNODE *keyblock_out, unsigned int options ); KBNODE *keyblock_out, unsigned int options );
int export_seckeys( STRLIST users ); int export_seckeys( STRLIST users );
int export_secsubkeys( STRLIST users ); int export_secsubkeys( STRLIST users );

View File

@ -68,7 +68,7 @@ struct mainproc_context {
int last_was_session_key; int last_was_session_key;
KBNODE list; /* the current list of packets */ KBNODE list; /* the current list of packets */
int have_data; int have_data;
IOBUF iobuf; /* used to get the filename etc. */ iobuf_t iobuf; /* used to get the filename etc. */
int trustletter; /* temp usage in list_node */ int trustletter; /* temp usage in list_node */
ulong local_id; /* ditto */ ulong local_id; /* ditto */
struct kidlist_item *pkenc_list; /* list of encryption packets */ struct kidlist_item *pkenc_list; /* list of encryption packets */
@ -79,7 +79,7 @@ struct mainproc_context {
}; };
static int do_proc_packets( CTX c, IOBUF a ); static int do_proc_packets( CTX c, iobuf_t a );
static void list_node( CTX c, KBNODE node ); static void list_node( CTX c, KBNODE node );
static void proc_tree( CTX c, KBNODE node ); static void proc_tree( CTX c, KBNODE node );
@ -94,7 +94,7 @@ release_list( CTX c )
release_kbnode( c->list ); release_kbnode( c->list );
while( c->pkenc_list ) { while( c->pkenc_list ) {
struct kidlist_item *tmp = c->pkenc_list->next; struct kidlist_item *tmp = c->pkenc_list->next;
m_free( c->pkenc_list ); xfree ( c->pkenc_list );
c->pkenc_list = tmp; c->pkenc_list = tmp;
} }
c->pkenc_list = NULL; c->pkenc_list = NULL;
@ -103,7 +103,7 @@ release_list( CTX c )
c->last_was_session_key = 0; c->last_was_session_key = 0;
c->pipemode.op = 0; c->pipemode.op = 0;
c->pipemode.stop_now = 0; c->pipemode.stop_now = 0;
m_free(c->dek); c->dek = NULL; xfree (c->dek); c->dek = NULL;
} }
@ -249,25 +249,25 @@ symkey_decrypt_sesskey( DEK *dek, byte *sesskey, size_t slen )
(int)slen); (int)slen);
return; return;
} }
hd = cipher_open( dek->algo, CIPHER_MODE_CFB, 1 ); gcry_cipher_open ( &hd, dek->algo, GCRY_CIPHER_MODE_CFB, 1 );
cipher_setkey( hd, dek->key, dek->keylen ); gcry_cipher_setkey( hd, dek->key, dek->keylen );
cipher_setiv( hd, NULL, 0 ); gcry_cipher_setiv( hd, NULL, 0 );
cipher_decrypt( hd, sesskey, sesskey, slen ); gcry_cipher_decrypt( hd, sesskey, slen, NULL, 0);
cipher_close( hd ); gcry_cipher_close( hd );
/* check first byte (the cipher algo) */ /* check first byte (the cipher algo) */
if ( sesskey[0] > 10 ) { if ( sesskey[0] > 10 ) {
log_error ( _("invalid symkey algorithm detected (%d)\n"), log_error ( _("invalid symkey algorithm detected (%d)\n"),
sesskey[0] ); sesskey[0] );
return; return;
} }
n = cipher_get_keylen (sesskey[0]) / 8; n = gcry_cipher_get_algo_keylen (sesskey[0]);
if (n > DIM(dek->key)) if (n > DIM(dek->key))
BUG (); BUG ();
/* now we replace the dek components with the real session key /* now we replace the dek components with the real session key
to decrypt the contents of the sequencing packet. */ to decrypt the contents of the sequencing packet. */
dek->keylen = cipher_get_keylen( sesskey[0] ) / 8; dek->keylen = gcry_cipher_get_algo_keylen (sesskey[0]);
dek->algo = sesskey[0]; dek->algo = sesskey[0];
memcpy( dek->key, sesskey + 1, dek->keylen ); memcpy (dek->key, sesskey + 1, dek->keylen);
/*log_hexdump( "thekey", dek->key, dek->keylen );*/ /*log_hexdump( "thekey", dek->key, dek->keylen );*/
} }
@ -283,8 +283,8 @@ proc_symkey_enc( CTX c, PACKET *pkt )
int algo = enc->cipher_algo; int algo = enc->cipher_algo;
const char *s; const char *s;
s = cipher_algo_to_string (algo); s = gcry_cipher_algo_name (algo);
if( s ) if (s && *s)
log_info(_("%s encrypted data\n"), s ); log_info(_("%s encrypted data\n"), s );
else else
log_info(_("encrypted with unknown algorithm %d\n"), algo ); log_info(_("encrypted with unknown algorithm %d\n"), algo );
@ -328,10 +328,10 @@ proc_pubkey_enc( CTX c, PACKET *pkt )
/* It does not make much sense to store the session key in /* It does not make much sense to store the session key in
* secure memory because it has already been passed on the * secure memory because it has already been passed on the
* command line and the GCHQ knows about it */ * command line and the GCHQ knows about it */
c->dek = m_alloc_clear( sizeof *c->dek ); c->dek = xcalloc (1, sizeof *c->dek );
result = get_override_session_key ( c->dek, opt.override_session_key ); result = get_override_session_key ( c->dek, opt.override_session_key );
if ( result ) { if ( result ) {
m_free(c->dek); c->dek = NULL; xfree (c->dek); c->dek = NULL;
} }
} }
else if( is_ELGAMAL(enc->pubkey_algo) else if( is_ELGAMAL(enc->pubkey_algo)
@ -343,18 +343,18 @@ proc_pubkey_enc( CTX c, PACKET *pkt )
if( opt.list_only ) if( opt.list_only )
result = -1; result = -1;
else { else {
c->dek = m_alloc_secure_clear( sizeof *c->dek ); c->dek = xcalloc_secure (1, sizeof *c->dek);
if( (result = get_session_key( enc, c->dek )) ) { if( (result = get_session_key( enc, c->dek )) ) {
/* error: delete the DEK */ /* error: delete the DEK */
m_free(c->dek); c->dek = NULL; xfree (c->dek); c->dek = NULL;
} }
} }
} }
else else
result = G10ERR_NO_SECKEY; result = GPG_ERR_NO_SECKEY;
} }
else else
result = G10ERR_PUBKEY_ALGO; result = GPG_ERR_PUBKEY_ALGO;
if( result == -1 ) if( result == -1 )
; ;
@ -364,7 +364,7 @@ proc_pubkey_enc( CTX c, PACKET *pkt )
log_info( _("public key encrypted data: good DEK\n") ); log_info( _("public key encrypted data: good DEK\n") );
if ( opt.show_session_key ) { if ( opt.show_session_key ) {
int i; int i;
char *buf = m_alloc ( c->dek->keylen*2 + 20 ); char *buf = xmalloc ( c->dek->keylen*2 + 20 );
sprintf ( buf, "%d:", c->dek->algo ); sprintf ( buf, "%d:", c->dek->algo );
for(i=0; i < c->dek->keylen; i++ ) for(i=0; i < c->dek->keylen; i++ )
sprintf(buf+strlen(buf), "%02X", c->dek->key[i] ); sprintf(buf+strlen(buf), "%02X", c->dek->key[i] );
@ -374,7 +374,7 @@ proc_pubkey_enc( CTX c, PACKET *pkt )
} }
/* store it for later display */ /* store it for later display */
{ {
struct kidlist_item *x = m_alloc( sizeof *x ); struct kidlist_item *x = xmalloc ( sizeof *x );
x->kid[0] = enc->keyid[0]; x->kid[0] = enc->keyid[0];
x->kid[1] = enc->keyid[1]; x->kid[1] = enc->keyid[1];
x->pubkey_algo = enc->pubkey_algo; x->pubkey_algo = enc->pubkey_algo;
@ -404,11 +404,11 @@ print_pkenc_list( struct kidlist_item *list, int failed )
if ( !failed && list->reason ) if ( !failed && list->reason )
continue; continue;
algstr = pubkey_algo_to_string( list->pubkey_algo ); algstr = gcry_pk_algo_name (list->pubkey_algo);
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
if( !algstr ) if (!algstr || !*algstr)
algstr = "[?]"; algstr = "[?]";
pk->pubkey_algo = list->pubkey_algo; pk->pubkey_algo = list->pubkey_algo;
if( !get_pubkey( pk, list->kid ) ) { if( !get_pubkey( pk, list->kid ) ) {
size_t n; size_t n;
@ -416,11 +416,11 @@ print_pkenc_list( struct kidlist_item *list, int failed )
log_info( _("encrypted with %u-bit %s key, ID %08lX, created %s\n"), log_info( _("encrypted with %u-bit %s key, ID %08lX, created %s\n"),
nbits_from_pk( pk ), algstr, (ulong)list->kid[1], nbits_from_pk( pk ), algstr, (ulong)list->kid[1],
strtimestamp(pk->timestamp) ); strtimestamp(pk->timestamp) );
fputs(" \"", log_stream() ); fputs(" \"", log_get_stream() );
p = get_user_id( list->kid, &n ); p = get_user_id( list->kid, &n );
print_utf8_string2 ( log_stream(), p, n, '"' ); print_utf8_string2 ( log_get_stream(), p, n, '"' );
m_free(p); xfree (p);
fputs("\"\n", log_stream() ); fputs("\"\n", log_get_stream() );
} }
else { else {
log_info(_("encrypted with %s key, ID %08lX\n"), log_info(_("encrypted with %s key, ID %08lX\n"),
@ -428,7 +428,7 @@ print_pkenc_list( struct kidlist_item *list, int failed )
} }
free_public_key( pk ); free_public_key( pk );
if( list->reason == G10ERR_NO_SECKEY ) { if( list->reason == GPG_ERR_NO_SECKEY ) {
if( is_status_enabled() ) { if( is_status_enabled() ) {
char buf[20]; char buf[20];
sprintf(buf,"%08lX%08lX", (ulong)list->kid[0], sprintf(buf,"%08lX%08lX", (ulong)list->kid[0],
@ -438,7 +438,7 @@ print_pkenc_list( struct kidlist_item *list, int failed )
} }
else if (list->reason) else if (list->reason)
log_info(_("public key decryption failed: %s\n"), log_info(_("public key decryption failed: %s\n"),
g10_errstr(list->reason)); gpg_strerror (list->reason));
} }
} }
@ -465,22 +465,22 @@ proc_encrypted( CTX c, PACKET *pkt )
/* assume this is old style conventional encrypted data */ /* assume this is old style conventional encrypted data */
if ( (algo = opt.def_cipher_algo)) if ( (algo = opt.def_cipher_algo))
log_info (_("assuming %s encrypted data\n"), log_info (_("assuming %s encrypted data\n"),
cipher_algo_to_string(algo)); gcry_cipher_algo_name (algo));
else if ( check_cipher_algo(CIPHER_ALGO_IDEA) ) { else if ( gcry_cipher_test_algo(CIPHER_ALGO_IDEA) ) {
algo = opt.def_cipher_algo; algo = opt.def_cipher_algo;
if (!algo) if (!algo)
algo = opt.s2k_cipher_algo; algo = opt.s2k_cipher_algo;
idea_cipher_warn(1); idea_cipher_warn(1);
log_info (_("IDEA cipher unavailable, " log_info (_("IDEA cipher unavailable, "
"optimistically attempting to use %s instead\n"), "optimistically attempting to use %s instead\n"),
cipher_algo_to_string(algo)); gcry_cipher_algo_name (algo));
} }
else { else {
algo = CIPHER_ALGO_IDEA; algo = CIPHER_ALGO_IDEA;
if (!opt.s2k_digest_algo) { if (!opt.s2k_digest_algo) {
/* If no digest is given we assume MD5 */ /* If no digest is given we assume MD5 */
s2kbuf.mode = 0; s2kbuf.mode = 0;
s2kbuf.hash_algo = DIGEST_ALGO_MD5; s2kbuf.hash_algo = GCRY_MD_MD5;
s2k = &s2kbuf; s2k = &s2kbuf;
} }
log_info (_("assuming %s encrypted data\n"), "IDEA"); log_info (_("assuming %s encrypted data\n"), "IDEA");
@ -491,14 +491,15 @@ proc_encrypted( CTX c, PACKET *pkt )
c->dek->algo_info_printed = 1; c->dek->algo_info_printed = 1;
} }
else if( !c->dek ) else if( !c->dek )
result = G10ERR_NO_SECKEY; result = GPG_ERR_NO_SECKEY;
if( !result ) if( !result )
result = decrypt_data( c, pkt->pkt.encrypted, c->dek ); result = decrypt_data( c, pkt->pkt.encrypted, c->dek );
m_free(c->dek); c->dek = NULL; xfree (c->dek); c->dek = NULL;
if( result == -1 ) if( result == -1 )
; ;
else if( !result || (result==G10ERR_BAD_SIGN && opt.ignore_mdc_error)) { else if( !result || (gpg_err_code (result)==GPG_ERR_BAD_SIGNATURE
&& opt.ignore_mdc_error)) {
write_status( STATUS_DECRYPTION_OKAY ); write_status( STATUS_DECRYPTION_OKAY );
if( opt.verbose > 1 ) if( opt.verbose > 1 )
log_info(_("decryption okay\n")); log_info(_("decryption okay\n"));
@ -507,14 +508,14 @@ proc_encrypted( CTX c, PACKET *pkt )
else if(!opt.no_mdc_warn) else if(!opt.no_mdc_warn)
log_info (_("WARNING: message was not integrity protected\n")); log_info (_("WARNING: message was not integrity protected\n"));
} }
else if( result == G10ERR_BAD_SIGN ) { else if( gpg_err_code (result) == GPG_ERR_BAD_SIGNATURE ) {
log_error(_("WARNING: encrypted message has been manipulated!\n")); log_error(_("WARNING: encrypted message has been manipulated!\n"));
write_status( STATUS_BADMDC ); write_status( STATUS_BADMDC );
write_status( STATUS_DECRYPTION_FAILED ); write_status( STATUS_DECRYPTION_FAILED );
} }
else { else {
write_status( STATUS_DECRYPTION_FAILED ); write_status( STATUS_DECRYPTION_FAILED );
log_error(_("decryption failed: %s\n"), g10_errstr(result)); log_error(_("decryption failed: %s\n"), gpg_strerror (result));
/* Hmmm: does this work when we have encrypted using multiple /* Hmmm: does this work when we have encrypted using multiple
* ways to specify the session key (symmmetric and PK)*/ * ways to specify the session key (symmmetric and PK)*/
} }
@ -537,7 +538,7 @@ proc_plaintext( CTX c, PACKET *pkt )
else if( opt.verbose ) else if( opt.verbose )
log_info(_("original file name='%.*s'\n"), pt->namelen, pt->name); log_info(_("original file name='%.*s'\n"), pt->namelen, pt->name);
free_md_filter_context( &c->mfx ); free_md_filter_context( &c->mfx );
c->mfx.md = md_open( 0, 0); gcry_md_open (&c->mfx.md, 0, 0);
/* fixme: we may need to push the textfilter if we have sigclass 1 /* fixme: we may need to push the textfilter if we have sigclass 1
* and no armoring - Not yet tested * and no armoring - Not yet tested
* Hmmm, why don't we need it at all if we have sigclass 1 * Hmmm, why don't we need it at all if we have sigclass 1
@ -548,7 +549,7 @@ proc_plaintext( CTX c, PACKET *pkt )
for(n=c->list; n; n = n->next ) { for(n=c->list; n; n = n->next ) {
if( n->pkt->pkttype == PKT_ONEPASS_SIG ) { if( n->pkt->pkttype == PKT_ONEPASS_SIG ) {
if( n->pkt->pkt.onepass_sig->digest_algo ) { if( n->pkt->pkt.onepass_sig->digest_algo ) {
md_enable( c->mfx.md, n->pkt->pkt.onepass_sig->digest_algo ); gcry_md_enable ( c->mfx.md, n->pkt->pkt.onepass_sig->digest_algo );
if( !any && n->pkt->pkt.onepass_sig->digest_algo if( !any && n->pkt->pkt.onepass_sig->digest_algo
== DIGEST_ALGO_MD5 ) == DIGEST_ALGO_MD5 )
only_md5 = 1; only_md5 = 1;
@ -572,7 +573,7 @@ proc_plaintext( CTX c, PACKET *pkt )
* documents */ * documents */
clearsig = (*data == 0x01); clearsig = (*data == 0x01);
for( data++, datalen--; datalen; datalen--, data++ ) for( data++, datalen--; datalen; datalen--, data++ )
md_enable( c->mfx.md, *data ); gcry_md_enable ( c->mfx.md, *data );
any = 1; any = 1;
break; /* no pass signature pakets are expected */ break; /* no pass signature pakets are expected */
} }
@ -580,9 +581,9 @@ proc_plaintext( CTX c, PACKET *pkt )
if( !any && !opt.skip_verify ) { if( !any && !opt.skip_verify ) {
/* no onepass sig packet: enable all standard algos */ /* no onepass sig packet: enable all standard algos */
md_enable( c->mfx.md, DIGEST_ALGO_RMD160 ); gcry_md_enable ( c->mfx.md, DIGEST_ALGO_RMD160 );
md_enable( c->mfx.md, DIGEST_ALGO_SHA1 ); gcry_md_enable ( c->mfx.md, DIGEST_ALGO_SHA1 );
md_enable( c->mfx.md, DIGEST_ALGO_MD5 ); gcry_md_enable ( c->mfx.md, DIGEST_ALGO_MD5 );
} }
if( opt.pgp2_workarounds && only_md5 && !opt.skip_verify ) { if( opt.pgp2_workarounds && only_md5 && !opt.skip_verify ) {
/* This is a kludge to work around a bug in pgp2. It does only /* This is a kludge to work around a bug in pgp2. It does only
@ -590,25 +591,27 @@ proc_plaintext( CTX c, PACKET *pkt )
* pgp mails we could see whether there is the signature packet * pgp mails we could see whether there is the signature packet
* in front of the plaintext. If someone needs this, send me a patch. * in front of the plaintext. If someone needs this, send me a patch.
*/ */
c->mfx.md2 = md_open( DIGEST_ALGO_MD5, 0); gcry_md_open (&c->mfx.md2, DIGEST_ALGO_MD5, 0);
} }
if ( DBG_HASHING ) { if ( DBG_HASHING ) {
md_start_debug( c->mfx.md, "verify" ); gcry_md_start_debug ( c->mfx.md, "verify" );
if ( c->mfx.md2 ) if ( c->mfx.md2 )
md_start_debug( c->mfx.md2, "verify2" ); gcry_md_start_debug ( c->mfx.md2, "verify2" );
} }
if ( c->pipemode.op == 'B' ) if ( c->pipemode.op == 'B' )
rc = handle_plaintext( pt, &c->mfx, 1, 0 ); rc = handle_plaintext( pt, &c->mfx, 1, 0, NULL );
else { else {
rc = handle_plaintext( pt, &c->mfx, c->sigs_only, clearsig ); int failed;
if( rc == G10ERR_CREATE_FILE && !c->sigs_only) {
rc = handle_plaintext( pt, &c->mfx, c->sigs_only, clearsig, &failed);
if( rc && failed && !c->sigs_only) {
/* can't write output but we hash it anyway to /* can't write output but we hash it anyway to
* check the signature */ * check the signature */
rc = handle_plaintext( pt, &c->mfx, 1, clearsig ); rc = handle_plaintext( pt, &c->mfx, 1, clearsig, NULL );
} }
} }
if( rc ) if( rc )
log_error( "handle plaintext failed: %s\n", g10_errstr(rc)); log_error( "handle plaintext failed: %s\n", gpg_strerror (rc));
free_packet(pkt); free_packet(pkt);
c->last_was_session_key = 0; c->last_was_session_key = 0;
@ -624,14 +627,14 @@ proc_plaintext( CTX c, PACKET *pkt )
static int static int
proc_compressed_cb( IOBUF a, void *info ) proc_compressed_cb( iobuf_t a, void *info )
{ {
return proc_signature_packets( info, a, ((CTX)info)->signed_data, return proc_signature_packets( info, a, ((CTX)info)->signed_data,
((CTX)info)->sigfilename ); ((CTX)info)->sigfilename );
} }
static int static int
proc_encrypt_cb( IOBUF a, void *info ) proc_encrypt_cb( iobuf_t a, void *info )
{ {
return proc_encryption_packets( info, a ); return proc_encryption_packets( info, a );
} }
@ -650,7 +653,7 @@ proc_compressed( CTX c, PACKET *pkt )
else else
rc = handle_compressed( c, zd, NULL, NULL ); rc = handle_compressed( c, zd, NULL, NULL );
if( rc ) if( rc )
log_error("uncompressing failed: %s\n", g10_errstr(rc)); log_error("uncompressing failed: %s\n", gpg_strerror (rc));
free_packet(pkt); free_packet(pkt);
c->last_was_session_key = 0; c->last_was_session_key = 0;
} }
@ -676,27 +679,29 @@ do_check_sig( CTX c, KBNODE node, int *is_selfsig, int *is_expkey )
sig = node->pkt->pkt.signature; sig = node->pkt->pkt.signature;
algo = sig->digest_algo; algo = sig->digest_algo;
if( (rc=check_digest_algo(algo)) ) if( (rc = gcry_md_test_algo(algo)) )
return rc; return rc;
if( sig->sig_class == 0x00 ) { if( sig->sig_class == 0x00 ) {
if( c->mfx.md ) if( c->mfx.md )
md = md_copy( c->mfx.md ); gcry_md_copy (&md,c->mfx.md);
else /* detached signature */ else /* detached signature */
md = md_open( 0, 0 ); /* signature_check() will enable the md*/ gcry_md_open (&md, 0, 0 ); /* signature_check() will
enable the md*/
} }
else if( sig->sig_class == 0x01 ) { else if( sig->sig_class == 0x01 ) {
/* how do we know that we have to hash the (already hashed) text /* how do we know that we have to hash the (already hashed) text
* in canonical mode ??? (calculating both modes???) */ * in canonical mode ??? (calculating both modes???) */
if( c->mfx.md ) { if( c->mfx.md ) {
md = md_copy( c->mfx.md ); gcry_md_copy (&md, c->mfx.md);
if( c->mfx.md2 ) if (c->mfx.md2)
md2 = md_copy( c->mfx.md2 ); gcry_md_copy (&md2, c->mfx.md2);
} }
else { /* detached signature */ else { /* detached signature */
log_debug("Do we really need this here?"); log_debug("Do we really need this here?");
md = md_open( 0, 0 ); /* signature_check() will enable the md*/ gcry_md_open (&md, 0, 0 ); /* signature_check() will
md2 = md_open( 0, 0 ); enable the md*/
gcry_md_open (&md2, 0, 0 );
} }
} }
else if( (sig->sig_class&~3) == 0x10 else if( (sig->sig_class&~3) == 0x10
@ -712,21 +717,21 @@ do_check_sig( CTX c, KBNODE node, int *is_selfsig, int *is_expkey )
else if( sig->sig_class == 0x20 ) { else if( sig->sig_class == 0x20 ) {
log_info(_("standalone revocation - " log_info(_("standalone revocation - "
"use \"gpg --import\" to apply\n")); "use \"gpg --import\" to apply\n"));
return G10ERR_NOT_PROCESSED; return GPG_ERR_NOT_PROCESSED;
} }
else { else {
log_error("invalid root packet for sigclass %02x\n", log_error("invalid root packet for sigclass %02x\n",
sig->sig_class); sig->sig_class);
return G10ERR_SIG_CLASS; return GPG_ERR_SIG_CLASS;
} }
} }
else else
return G10ERR_SIG_CLASS; return GPG_ERR_SIG_CLASS;
rc = signature_check2( sig, md, &dummy, is_expkey ); rc = signature_check2( sig, md, &dummy, is_expkey );
if( rc == G10ERR_BAD_SIGN && md2 ) if( gpg_err_code (rc) == GPG_ERR_BAD_SIGNATURE && md2 )
rc = signature_check2( sig, md2, &dummy, is_expkey ); rc = signature_check2( sig, md2, &dummy, is_expkey );
md_close(md); gcry_md_close (md);
md_close(md2); gcry_md_close (md2);
return rc; return rc;
} }
@ -947,12 +952,13 @@ list_node( CTX c, KBNODE node )
fputs("sig", stdout); fputs("sig", stdout);
if( opt.check_sigs ) { if( opt.check_sigs ) {
fflush(stdout); fflush(stdout);
switch( (rc2=do_check_sig( c, node, &is_selfsig, NULL )) ) { switch( gpg_err_code (rc2=do_check_sig( c, node,
case 0: sigrc = '!'; break; &is_selfsig, NULL )) ) {
case G10ERR_BAD_SIGN: sigrc = '-'; break; case 0: sigrc = '!'; break;
case G10ERR_NO_PUBKEY: case GPG_ERR_BAD_SIGNATURE: sigrc = '-'; break;
case G10ERR_UNU_PUBKEY: sigrc = '?'; break; case GPG_ERR_NO_PUBKEY:
default: sigrc = '%'; break; case GPG_ERR_UNUSABLE_PUBKEY: sigrc = '?'; break;
default: sigrc = '%'; break;
} }
} }
else { /* check whether this is a self signature */ else { /* check whether this is a self signature */
@ -991,7 +997,7 @@ list_node( CTX c, KBNODE node )
printf("%c %08lX %s ", printf("%c %08lX %s ",
sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig)); sigrc, (ulong)sig->keyid[1], datestr_from_sig(sig));
if( sigrc == '%' ) if( sigrc == '%' )
printf("[%s] ", g10_errstr(rc2) ); printf("[%s] ", gpg_strerror (rc2) );
else if( sigrc == '?' ) else if( sigrc == '?' )
; ;
else if( is_selfsig ) { else if( is_selfsig ) {
@ -1004,7 +1010,7 @@ list_node( CTX c, KBNODE node )
else if( !opt.fast_list_mode ) { else if( !opt.fast_list_mode ) {
p = get_user_id( sig->keyid, &n ); p = get_user_id( sig->keyid, &n );
print_string( stdout, p, n, opt.with_colons ); print_string( stdout, p, n, opt.with_colons );
m_free(p); xfree (p);
} }
if( opt.with_colons ) if( opt.with_colons )
printf(":%02x%c:", sig->sig_class, sig->flags.exportable?'x':'l'); printf(":%02x%c:", sig->sig_class, sig->flags.exportable?'x':'l');
@ -1017,24 +1023,24 @@ list_node( CTX c, KBNODE node )
int int
proc_packets( void *anchor, IOBUF a ) proc_packets( void *anchor, iobuf_t a )
{ {
int rc; int rc;
CTX c = m_alloc_clear( sizeof *c ); CTX c = xcalloc (1, sizeof *c );
c->anchor = anchor; c->anchor = anchor;
rc = do_proc_packets( c, a ); rc = do_proc_packets( c, a );
m_free( c ); xfree ( c );
return rc; return rc;
} }
int int
proc_signature_packets( void *anchor, IOBUF a, proc_signature_packets( void *anchor, iobuf_t a,
STRLIST signedfiles, const char *sigfilename ) STRLIST signedfiles, const char *sigfilename )
{ {
CTX c = m_alloc_clear( sizeof *c ); CTX c = xcalloc (1, sizeof *c );
int rc; int rc;
c->anchor = anchor; c->anchor = anchor;
@ -1042,28 +1048,28 @@ proc_signature_packets( void *anchor, IOBUF a,
c->signed_data = signedfiles; c->signed_data = signedfiles;
c->sigfilename = sigfilename; c->sigfilename = sigfilename;
rc = do_proc_packets( c, a ); rc = do_proc_packets( c, a );
m_free( c ); xfree ( c );
return rc; return rc;
} }
int int
proc_encryption_packets( void *anchor, IOBUF a ) proc_encryption_packets( void *anchor, iobuf_t a )
{ {
CTX c = m_alloc_clear( sizeof *c ); CTX c = xcalloc (1, sizeof *c );
int rc; int rc;
c->anchor = anchor; c->anchor = anchor;
c->encrypt_only = 1; c->encrypt_only = 1;
rc = do_proc_packets( c, a ); rc = do_proc_packets( c, a );
m_free( c ); xfree ( c );
return rc; return rc;
} }
int int
do_proc_packets( CTX c, IOBUF a ) do_proc_packets( CTX c, iobuf_t a )
{ {
PACKET *pkt = m_alloc( sizeof *pkt ); PACKET *pkt = xmalloc ( sizeof *pkt );
int rc=0; int rc=0;
int any_data=0; int any_data=0;
int newpkt; int newpkt;
@ -1076,7 +1082,7 @@ do_proc_packets( CTX c, IOBUF a )
free_packet(pkt); free_packet(pkt);
/* stop processing when an invalid packet has been encountered /* stop processing when an invalid packet has been encountered
* but don't do so when we are doing a --list-packet. */ * but don't do so when we are doing a --list-packet. */
if( rc == G10ERR_INVALID_PACKET && opt.list_packets != 2 ) if( gpg_err_code (rc) == GPG_ERR_INV_PACKET && opt.list_packets != 2 )
break; break;
continue; continue;
} }
@ -1101,7 +1107,7 @@ do_proc_packets( CTX c, IOBUF a )
case PKT_ENCRYPTED: case PKT_ENCRYPTED:
case PKT_ENCRYPTED_MDC: case PKT_ENCRYPTED_MDC:
write_status_text( STATUS_UNEXPECTED, "0" ); write_status_text( STATUS_UNEXPECTED, "0" );
rc = G10ERR_UNEXPECTED; rc = GPG_ERR_UNEXPECTED;
goto leave; goto leave;
case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break; case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break;
case PKT_PLAINTEXT: proc_plaintext( c, pkt ); break; case PKT_PLAINTEXT: proc_plaintext( c, pkt ); break;
@ -1117,7 +1123,7 @@ do_proc_packets( CTX c, IOBUF a )
case PKT_SECRET_KEY: case PKT_SECRET_KEY:
case PKT_USER_ID: case PKT_USER_ID:
write_status_text( STATUS_UNEXPECTED, "0" ); write_status_text( STATUS_UNEXPECTED, "0" );
rc = G10ERR_UNEXPECTED; rc = GPG_ERR_UNEXPECTED;
goto leave; goto leave;
case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break; case PKT_SIGNATURE: newpkt = add_signature( c, pkt ); break;
case PKT_SYMKEY_ENC: proc_symkey_enc( c, pkt ); break; case PKT_SYMKEY_ENC: proc_symkey_enc( c, pkt ); break;
@ -1171,7 +1177,7 @@ do_proc_packets( CTX c, IOBUF a )
if( newpkt == -1 ) if( newpkt == -1 )
; ;
else if( newpkt ) { else if( newpkt ) {
pkt = m_alloc( sizeof *pkt ); pkt = xmalloc ( sizeof *pkt );
init_packet(pkt); init_packet(pkt);
} }
else else
@ -1183,7 +1189,7 @@ do_proc_packets( CTX c, IOBUF a )
break; break;
} }
} }
if( rc == G10ERR_INVALID_PACKET ) if( rc == GPG_ERR_INV_PACKET )
write_status_text( STATUS_NODATA, "3" ); write_status_text( STATUS_NODATA, "3" );
if( any_data ) if( any_data )
rc = 0; rc = 0;
@ -1193,9 +1199,9 @@ do_proc_packets( CTX c, IOBUF a )
leave: leave:
release_list( c ); release_list( c );
m_free(c->dek); xfree (c->dek);
free_packet( pkt ); free_packet( pkt );
m_free( pkt ); xfree ( pkt );
free_md_filter_context( &c->mfx ); free_md_filter_context( &c->mfx );
return rc; return rc;
} }
@ -1269,16 +1275,16 @@ check_sig_and_print( CTX c, KBNODE node )
} }
tstr = asctimestamp(sig->timestamp); tstr = asctimestamp(sig->timestamp);
astr = pubkey_algo_to_string( sig->pubkey_algo ); astr = gcry_pk_algo_name (sig->pubkey_algo);
log_info(_("Signature made %.*s using %s key ID %08lX\n"), log_info(_("Signature made %.*s using %s key ID %08lX\n"),
(int)strlen(tstr), tstr, astr? astr: "?", (ulong)sig->keyid[1] ); (int)strlen(tstr), tstr, astr? astr: "?", (ulong)sig->keyid[1] );
rc = do_check_sig(c, node, NULL, &is_expkey ); rc = do_check_sig(c, node, NULL, &is_expkey );
if( rc == G10ERR_NO_PUBKEY && opt.keyserver_scheme && opt.keyserver_options.auto_key_retrieve) { if( rc == GPG_ERR_NO_PUBKEY && opt.keyserver_scheme && opt.keyserver_options.auto_key_retrieve) {
if( keyserver_import_keyid ( sig->keyid )==0 ) if( keyserver_import_keyid ( sig->keyid )==0 )
rc = do_check_sig(c, node, NULL, &is_expkey ); rc = do_check_sig(c, node, NULL, &is_expkey );
} }
if( !rc || rc == G10ERR_BAD_SIGN ) { if( !rc || gpg_err_code (rc) == GPG_ERR_BAD_SIGNATURE ) {
KBNODE un, keyblock; KBNODE un, keyblock;
int count=0, statno; int count=0, statno;
char keyid_str[50]; char keyid_str[50];
@ -1322,9 +1328,9 @@ check_sig_and_print( CTX c, KBNODE node )
log_info(rc? _("BAD signature from \"") log_info(rc? _("BAD signature from \"")
: sig->flags.expired ? _("Expired signature from \"") : sig->flags.expired ? _("Expired signature from \"")
: _("Good signature from \"")); : _("Good signature from \""));
print_utf8_string( log_stream(), un->pkt->pkt.user_id->name, print_utf8_string( log_get_stream(), un->pkt->pkt.user_id->name,
un->pkt->pkt.user_id->len ); un->pkt->pkt.user_id->len );
fputs("\"\n", log_stream() ); fputs("\"\n", log_get_stream() );
count++; count++;
} }
if( !count ) { /* just in case that we have no valid textual if( !count ) { /* just in case that we have no valid textual
@ -1356,13 +1362,13 @@ check_sig_and_print( CTX c, KBNODE node )
: sig->flags.expired ? _("Expired signature from \"") : sig->flags.expired ? _("Expired signature from \"")
: _("Good signature from \"")); : _("Good signature from \""));
if (opt.trust_model!=TM_ALWAYS && un) { if (opt.trust_model!=TM_ALWAYS && un) {
fputs(_("[uncertain]"), log_stream() ); fputs(_("[uncertain]"), log_get_stream() );
putc(' ', log_stream() ); putc(' ', log_get_stream() );
} }
print_utf8_string( log_stream(), print_utf8_string( log_get_stream(),
un? un->pkt->pkt.user_id->name:"[?]", un? un->pkt->pkt.user_id->name:"[?]",
un? un->pkt->pkt.user_id->len:3 ); un? un->pkt->pkt.user_id->len:3 );
fputs("\"\n", log_stream() ); fputs("\"\n", log_get_stream() );
} }
/* If we have a good signature and already printed /* If we have a good signature and already printed
@ -1393,9 +1399,9 @@ check_sig_and_print( CTX c, KBNODE node )
} }
log_info( _(" aka \"")); log_info( _(" aka \""));
print_utf8_string( log_stream(), un->pkt->pkt.user_id->name, print_utf8_string( log_get_stream(), un->pkt->pkt.user_id->name,
un->pkt->pkt.user_id->len ); un->pkt->pkt.user_id->len );
fputs("\"\n", log_stream() ); fputs("\"\n", log_get_stream() );
} }
} }
release_kbnode( keyblock ); release_kbnode( keyblock );
@ -1408,7 +1414,7 @@ check_sig_and_print( CTX c, KBNODE node )
if( !rc && is_status_enabled() ) { if( !rc && is_status_enabled() ) {
/* print a status response with the fingerprint */ /* print a status response with the fingerprint */
PKT_public_key *pk = m_alloc_clear( sizeof *pk ); PKT_public_key *pk = xcalloc (1, sizeof *pk );
if( !get_pubkey( pk, sig->keyid ) ) { if( !get_pubkey( pk, sig->keyid ) ) {
byte array[MAX_FINGERPRINT_LEN], *p; byte array[MAX_FINGERPRINT_LEN], *p;
@ -1436,7 +1442,7 @@ check_sig_and_print( CTX c, KBNODE node )
akid[0] = pk->main_keyid[0]; akid[0] = pk->main_keyid[0];
akid[1] = pk->main_keyid[1]; akid[1] = pk->main_keyid[1];
free_public_key (pk); free_public_key (pk);
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
if (get_pubkey (pk, akid)) { if (get_pubkey (pk, akid)) {
/* impossible error, we simply return a zeroed out fpr */ /* impossible error, we simply return a zeroed out fpr */
n = MAX_FINGERPRINT_LEN < 20? MAX_FINGERPRINT_LEN : 20; n = MAX_FINGERPRINT_LEN < 20? MAX_FINGERPRINT_LEN : 20;
@ -1460,7 +1466,7 @@ check_sig_and_print( CTX c, KBNODE node )
{ {
log_info(_("Signature expired %s\n"), log_info(_("Signature expired %s\n"),
asctimestamp(sig->expiredate)); asctimestamp(sig->expiredate));
rc=G10ERR_GENERAL; /* need a better error here? */ rc=GPG_ERR_GENERAL; /* need a better error here? */
} }
else if(sig->expiredate) else if(sig->expiredate)
log_info(_("Signature expires %s\n"),asctimestamp(sig->expiredate)); log_info(_("Signature expires %s\n"),asctimestamp(sig->expiredate));
@ -1469,7 +1475,7 @@ check_sig_and_print( CTX c, KBNODE node )
log_info(_("%s signature, digest algorithm %s\n"), log_info(_("%s signature, digest algorithm %s\n"),
sig->sig_class==0x00?_("binary"): sig->sig_class==0x00?_("binary"):
sig->sig_class==0x01?_("textmode"):_("unknown"), sig->sig_class==0x01?_("textmode"):_("unknown"),
digest_algo_to_string(sig->digest_algo)); gcry_md_algo_name (sig->digest_algo));
if( rc ) if( rc )
g10_errors_seen = 1; g10_errors_seen = 1;
@ -1483,12 +1489,12 @@ check_sig_and_print( CTX c, KBNODE node )
sig->pubkey_algo, sig->digest_algo, sig->pubkey_algo, sig->digest_algo,
sig->sig_class, (ulong)sig->timestamp, rc ); sig->sig_class, (ulong)sig->timestamp, rc );
write_status_text( STATUS_ERRSIG, buf ); write_status_text( STATUS_ERRSIG, buf );
if( rc == G10ERR_NO_PUBKEY ) { if( rc == GPG_ERR_NO_PUBKEY ) {
buf[16] = 0; buf[16] = 0;
write_status_text( STATUS_NO_PUBKEY, buf ); write_status_text( STATUS_NO_PUBKEY, buf );
} }
if( rc != G10ERR_NOT_PROCESSED ) if( rc != GPG_ERR_NOT_PROCESSED )
log_error(_("Can't check signature: %s\n"), g10_errstr(rc) ); log_error(_("Can't check signature: %s\n"), gpg_strerror (rc) );
} }
return rc; return rc;
} }
@ -1534,11 +1540,11 @@ proc_tree( CTX c, KBNODE node )
if( !c->have_data ) { if( !c->have_data ) {
free_md_filter_context( &c->mfx ); free_md_filter_context( &c->mfx );
/* prepare to create all requested message digests */ /* prepare to create all requested message digests */
c->mfx.md = md_open(0, 0); gcry_md_open (&c->mfx.md, 0, 0);
/* fixme: why looking for the signature packet and not 1passpacket*/ /* fixme: why looking for the signature packet and not 1passpacket*/
for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); ) { for( n1 = node; (n1 = find_next_kbnode(n1, PKT_SIGNATURE )); ) {
md_enable( c->mfx.md, n1->pkt->pkt.signature->digest_algo); gcry_md_enable ( c->mfx.md, n1->pkt->pkt.signature->digest_algo);
} }
/* ask for file and hash it */ /* ask for file and hash it */
if( c->sigs_only ) { if( c->sigs_only ) {
@ -1552,7 +1558,7 @@ proc_tree( CTX c, KBNODE node )
n1? (n1->pkt->pkt.onepass_sig->sig_class == 0x01):0 ); n1? (n1->pkt->pkt.onepass_sig->sig_class == 0x01):0 );
} }
if( rc ) { if( rc ) {
log_error("can't hash datafile: %s\n", g10_errstr(rc)); log_error("can't hash datafile: %s\n", gpg_strerror (rc));
return; return;
} }
} }
@ -1613,20 +1619,20 @@ proc_tree( CTX c, KBNODE node )
else if( !c->have_data ) { else if( !c->have_data ) {
/* detached signature */ /* detached signature */
free_md_filter_context( &c->mfx ); free_md_filter_context( &c->mfx );
c->mfx.md = md_open(sig->digest_algo, 0); gcry_md_open (&c->mfx.md, sig->digest_algo, 0);
if( !opt.pgp2_workarounds ) if( !opt.pgp2_workarounds )
; ;
else if( sig->digest_algo == DIGEST_ALGO_MD5 else if( sig->digest_algo == DIGEST_ALGO_MD5
&& is_RSA( sig->pubkey_algo ) ) { && is_RSA( sig->pubkey_algo ) ) {
/* enable a workaround for a pgp2 bug */ /* enable a workaround for a pgp2 bug */
c->mfx.md2 = md_open( DIGEST_ALGO_MD5, 0 ); gcry_md_open (&c->mfx.md2, DIGEST_ALGO_MD5, 0 );
} }
else if( sig->digest_algo == DIGEST_ALGO_SHA1 else if( sig->digest_algo == DIGEST_ALGO_SHA1
&& sig->pubkey_algo == PUBKEY_ALGO_DSA && sig->pubkey_algo == PUBKEY_ALGO_DSA
&& sig->sig_class == 0x01 ) { && sig->sig_class == 0x01 ) {
/* enable the workaround also for pgp5 when the detached /* enable the workaround also for pgp5 when the detached
* signature has been created in textmode */ * signature has been created in textmode */
c->mfx.md2 = md_open( sig->digest_algo, 0 ); gcry_md_open (&c->mfx.md2, sig->digest_algo, 0 );
} }
#if 0 /* workaround disabled */ #if 0 /* workaround disabled */
/* Here we have another hack to work around a pgp 2 bug /* Here we have another hack to work around a pgp 2 bug
@ -1639,9 +1645,9 @@ proc_tree( CTX c, KBNODE node )
/* c->mfx.md2? 0 :(sig->sig_class == 0x01) */ /* c->mfx.md2? 0 :(sig->sig_class == 0x01) */
#endif #endif
if ( DBG_HASHING ) { if ( DBG_HASHING ) {
md_start_debug( c->mfx.md, "verify" ); gcry_md_start_debug ( c->mfx.md, "verify" );
if ( c->mfx.md2 ) if ( c->mfx.md2 )
md_start_debug( c->mfx.md2, "verify2" ); gcry_md_start_debug ( c->mfx.md2, "verify2" );
} }
if( c->sigs_only ) { if( c->sigs_only ) {
rc = hash_datafiles( c->mfx.md, c->mfx.md2, rc = hash_datafiles( c->mfx.md, c->mfx.md2,
@ -1654,7 +1660,7 @@ proc_tree( CTX c, KBNODE node )
(sig->sig_class == 0x01) ); (sig->sig_class == 0x01) );
} }
if( rc ) { if( rc ) {
log_error("can't hash datafile: %s\n", g10_errstr(rc)); log_error("can't hash datafile: %s\n", gpg_strerror (rc));
return; return;
} }
} }

View File

@ -38,7 +38,7 @@
*/ */
int int
md_filter( void *opaque, int control, md_filter( void *opaque, int control,
IOBUF a, byte *buf, size_t *ret_len) iobuf_t a, byte *buf, size_t *ret_len)
{ {
size_t size = *ret_len; size_t size = *ret_len;
md_filter_context_t *mfx = opaque; md_filter_context_t *mfx = opaque;
@ -50,9 +50,9 @@ md_filter( void *opaque, int control,
i = iobuf_read( a, buf, size ); i = iobuf_read( a, buf, size );
if( i == -1 ) i = 0; if( i == -1 ) i = 0;
if( i ) { if( i ) {
md_write(mfx->md, buf, i ); gcry_md_write(mfx->md, buf, i );
if( mfx->md2 ) if( mfx->md2 )
md_write(mfx->md2, buf, i ); gcry_md_write(mfx->md2, buf, i );
} }
else else
rc = -1; /* eof */ rc = -1; /* eof */
@ -67,8 +67,8 @@ md_filter( void *opaque, int control,
void void
free_md_filter_context( md_filter_context_t *mfx ) free_md_filter_context( md_filter_context_t *mfx )
{ {
md_close(mfx->md); gcry_md_close (mfx->md);
md_close(mfx->md2); gcry_md_close (mfx->md2);
mfx->md = NULL; mfx->md = NULL;
mfx->md2 = NULL; mfx->md2 = NULL;
mfx->maxbuf_size = 0; mfx->maxbuf_size = 0;

View File

@ -1,5 +1,6 @@
/* misc.c - miscellaneous functions /* misc.c - miscellaneous functions
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2002,
* 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -24,6 +25,7 @@
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <errno.h> #include <errno.h>
#include <assert.h>
#if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2 #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
#include <asm/sysinfo.h> #include <asm/sysinfo.h>
#include <asm/unistd.h> #include <asm/unistd.h>
@ -33,27 +35,15 @@
#include <sys/time.h> #include <sys/time.h>
#include <sys/resource.h> #include <sys/resource.h>
#endif #endif
#include "gpg.h"
#include "util.h" #include "util.h"
#include "main.h" #include "main.h"
#include "photoid.h" #include "photoid.h"
#include "options.h" #include "options.h"
#include "i18n.h" #include "i18n.h"
#define MAX_EXTERN_MPI_BITS 16384
const char *g10m_revision_string(int);
const char *g10c_revision_string(int);
const char *g10u_revision_string(int);
#ifdef __GNUC__
volatile
#endif
void
pull_in_libs(void)
{
g10m_revision_string(0);
g10c_revision_string(0);
g10u_revision_string(0);
}
#if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2 #if defined(__linux__) && defined(__alpha__) && __GLIBC__ < 2
@ -125,19 +115,26 @@ checksum( byte *p, unsigned n )
} }
u16 u16
checksum_mpi( MPI a ) checksum_mpi( gcry_mpi_t a )
{ {
u16 csum; int rc;
byte *buffer; u16 csum;
unsigned nbytes; byte *buffer;
unsigned nbits; size_t nbytes;
buffer = mpi_get_buffer( a, &nbytes, NULL ); rc = gcry_mpi_print( GCRYMPI_FMT_PGP, NULL, &nbytes, a );
nbits = mpi_get_nbits(a); if (rc)
csum = checksum_u16( nbits ); BUG ();
csum += checksum( buffer, nbytes ); /* fixme: for numbers not in secure memory we should use a stack
m_free( buffer ); * based buffer and only allocate a larger one if mpi_print return
return csum; * an error */
buffer = gcry_is_secure(a)? gcry_xmalloc_secure(nbytes) : gcry_xmalloc(nbytes);
rc = gcry_mpi_print( GCRYMPI_FMT_PGP, buffer, &nbytes, a );
if (rc)
BUG ();
csum = checksum (buffer, nbytes );
xfree (buffer );
return csum;
} }
u32 u32
@ -238,16 +235,18 @@ int
openpgp_cipher_test_algo( int algo ) openpgp_cipher_test_algo( int algo )
{ {
if( algo < 0 || algo > 110 ) if( algo < 0 || algo > 110 )
return G10ERR_CIPHER_ALGO; return GPG_ERR_CIPHER_ALGO;
return check_cipher_algo(algo); return gcry_cipher_test_algo (algo);
} }
int int
openpgp_pk_test_algo( int algo, unsigned int usage_flags ) openpgp_pk_test_algo( int algo, unsigned int usage_flags )
{ {
if( algo < 0 || algo > 110 ) size_t value = usage_flags;
return G10ERR_PUBKEY_ALGO;
return check_pubkey_algo2( algo, usage_flags ); if (algo < 0 || algo > 110)
return GPG_ERR_PUBKEY_ALGO;
return gcry_pk_algo_info (algo, GCRYCTL_TEST_ALGO, NULL, &value);
} }
int int
@ -285,8 +284,29 @@ int
openpgp_md_test_algo( int algo ) openpgp_md_test_algo( int algo )
{ {
if( algo < 0 || algo > 110 ) if( algo < 0 || algo > 110 )
return G10ERR_DIGEST_ALGO; return GPG_ERR_DIGEST_ALGO;
return check_digest_algo(algo); return gcry_md_test_algo (algo);
}
int
openpgp_md_map_name (const char *string)
{
int i = gcry_md_map_name (string);
return i < 0 || i > 110? 0 : i;
}
int
openpgp_cipher_map_name (const char *string)
{
int i = gcry_cipher_map_name (string);
return i < 0 || i > 110? 0 : i;
}
int
openpgp_pk_map_name (const char *string)
{
int i = gcry_pk_map_name (string);
return i < 0 || i > 110? 0 : i;
} }
#ifdef USE_IDEA #ifdef USE_IDEA
@ -336,7 +356,7 @@ pct_expando(const char *string,struct expando_args *args)
goto fail; goto fail;
maxlen+=1024; maxlen+=1024;
ret=m_realloc(ret,maxlen); ret= xrealloc(ret,maxlen);
} }
done=0; done=0;
@ -467,7 +487,7 @@ pct_expando(const char *string,struct expando_args *args)
return ret; return ret;
fail: fail:
m_free(ret); xfree (ret);
return NULL; return NULL;
} }
@ -565,7 +585,7 @@ check_compress_algo(int algo)
if(algo>=0 && algo<=2) if(algo>=0 && algo<=2)
return 0; return 0;
return G10ERR_COMPR_ALGO; return GPG_ERR_COMPR_ALGO;
} }
int int
@ -676,3 +696,233 @@ parse_options(char *str,unsigned int *options,struct parse_options *opts)
return 1; return 1;
} }
/* Temporary helper. */
int
pubkey_get_npkey( int algo )
{
int n = gcry_pk_algo_info( algo, GCRYCTL_GET_ALGO_NPKEY, NULL, 0 );
return n > 0? n : 0;
}
/* Temporary helper. */
int
pubkey_get_nskey( int algo )
{
int n = gcry_pk_algo_info( algo, GCRYCTL_GET_ALGO_NSKEY, NULL, 0 );
return n > 0? n : 0;
}
/* Temporary helper. */
int
pubkey_get_nsig( int algo )
{
int n = gcry_pk_algo_info( algo, GCRYCTL_GET_ALGO_NSIGN, NULL, 0 );
return n > 0? n : 0;
}
/* Temporary helper. */
int
pubkey_get_nenc( int algo )
{
int n = gcry_pk_algo_info( algo, GCRYCTL_GET_ALGO_NENCR, NULL, 0 );
return n > 0? n : 0;
}
/* Temporary helper. */
unsigned int
pubkey_nbits( int algo, gcry_mpi_t *key )
{
int rc, nbits;
gcry_sexp_t sexp;
if( algo == GCRY_PK_DSA ) {
rc = gcry_sexp_build ( &sexp, NULL,
"(public-key(dsa(p%m)(q%m)(g%m)(y%m)))",
key[0], key[1], key[2], key[3] );
}
else if( algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E ) {
rc = gcry_sexp_build ( &sexp, NULL,
"(public-key(elg(p%m)(g%m)(y%m)))",
key[0], key[1], key[2] );
}
else if( algo == GCRY_PK_RSA ) {
rc = gcry_sexp_build ( &sexp, NULL,
"(public-key(rsa(n%m)(e%m)))",
key[0], key[1] );
}
else
return 0;
if ( rc )
BUG ();
nbits = gcry_pk_get_nbits( sexp );
gcry_sexp_release( sexp );
return nbits;
}
/* MPI helper functions. */
/****************
* write an mpi to out.
*/
int
mpi_write( iobuf_t out, gcry_mpi_t a )
{
char buffer[(MAX_EXTERN_MPI_BITS+7)/8];
size_t nbytes;
int rc;
nbytes = (MAX_EXTERN_MPI_BITS+7)/8;
rc = gcry_mpi_print( GCRYMPI_FMT_PGP, buffer, &nbytes, a );
if( !rc )
rc = iobuf_write( out, buffer, nbytes );
return rc;
}
/****************
* Writye a MPI to out, but in this case it is an opaque one,
* s used vor v3 protected keys.
*/
int
mpi_write_opaque( iobuf_t out, gcry_mpi_t a )
{
size_t nbytes, nbits;
int rc;
char *p;
assert( gcry_mpi_get_flag( a, GCRYMPI_FLAG_OPAQUE ) );
p = gcry_mpi_get_opaque( a, &nbits );
nbytes = (nbits+7) / 8;
iobuf_put( out, nbits >> 8 );
iobuf_put( out, nbits );
rc = iobuf_write( out, p, nbytes );
return rc;
}
/****************
* Read an external representation of an mpi and return the MPI
* The external format is a 16 bit unsigned value stored in network byte order,
* giving the number of bits for the following integer. The integer is stored
* with MSB first (left padded with zeroes to align on a byte boundary).
*/
gcry_mpi_t
mpi_read(iobuf_t inp, unsigned int *ret_nread, int secure)
{
int c, c1, c2, i;
unsigned int nbits, nbytes, nread=0;
gcry_mpi_t a = NULL;
byte *buf = NULL;
byte *p;
if( (c = c1 = iobuf_get(inp)) == -1 )
goto leave;
nbits = c << 8;
if( (c = c2 = iobuf_get(inp)) == -1 )
goto leave;
nbits |= c;
if( nbits > MAX_EXTERN_MPI_BITS ) {
log_error("mpi too large (%u bits)\n", nbits);
goto leave;
}
nread = 2;
nbytes = (nbits+7) / 8;
buf = secure? gcry_xmalloc_secure( nbytes+2 ) : gcry_xmalloc( nbytes+2 );
p = buf;
p[0] = c1;
p[1] = c2;
for( i=0 ; i < nbytes; i++ ) {
p[i+2] = iobuf_get(inp) & 0xff;
nread++;
}
nread += nbytes;
if( gcry_mpi_scan( &a, GCRYMPI_FMT_PGP, buf, &nread ) )
a = NULL;
leave:
gcry_free(buf);
if( nread > *ret_nread )
log_bug("mpi larger than packet");
else
*ret_nread = nread;
return a;
}
/****************
* Same as mpi_read but the value is stored as an opaque MPI.
* This function is used to read encrypted MPI of v3 packets.
*/
gcry_mpi_t
mpi_read_opaque(iobuf_t inp, unsigned *ret_nread )
{
int c, c1, c2, i;
unsigned nbits, nbytes, nread=0;
gcry_mpi_t a = NULL;
byte *buf = NULL;
byte *p;
if( (c = c1 = iobuf_get(inp)) == -1 )
goto leave;
nbits = c << 8;
if( (c = c2 = iobuf_get(inp)) == -1 )
goto leave;
nbits |= c;
if( nbits > MAX_EXTERN_MPI_BITS ) {
log_error("mpi too large (%u bits)\n", nbits);
goto leave;
}
nread = 2;
nbytes = (nbits+7) / 8;
buf = gcry_xmalloc( nbytes );
p = buf;
for( i=0 ; i < nbytes; i++ ) {
p[i] = iobuf_get(inp) & 0xff;
}
nread += nbytes;
a = gcry_mpi_set_opaque(NULL, buf, nbits );
buf = NULL;
leave:
gcry_free(buf);
if( nread > *ret_nread )
log_bug("mpi larger than packet");
else
*ret_nread = nread;
return a;
}
int
mpi_print( FILE *fp, gcry_mpi_t a, int mode )
{
int n=0;
if( !a )
return fprintf(fp, "[MPI_NULL]");
if( !mode ) {
unsigned int n1;
n1 = gcry_mpi_get_nbits(a);
n += fprintf(fp, "[%u bits]", n1);
}
else {
int rc;
char *buffer;
rc = gcry_mpi_aprint( GCRYMPI_FMT_HEX, (void **)&buffer, NULL, a );
assert( !rc );
fputs( buffer, fp );
n += strlen(buffer);
gcry_free( buffer );
}
return n;
}

View File

@ -85,7 +85,7 @@ char *mkdtemp(char *template)
if(remaining>0) if(remaining>0)
sprintf(marker,"%X",randombits[idx]&0xF); sprintf(marker,"%X",randombits[idx]&0xF);
m_free(randombits); xfree (randombits);
if(mkdir(template,0700)==0) if(mkdir(template,0700)==0)
break; break;

View File

@ -28,6 +28,8 @@
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "memory.h" #include "memory.h"
#include "ttyio.h" #include "ttyio.h"
@ -99,19 +101,19 @@ make_outfile_name( const char *iname )
size_t n; size_t n;
if( (!iname || (*iname=='-' && !iname[1]) )) if( (!iname || (*iname=='-' && !iname[1]) ))
return m_strdup("-"); return xstrdup ("-");
n = strlen(iname); n = strlen(iname);
if( n > 4 && ( !CMP_FILENAME(iname+n-4, EXTSEP_S "gpg") if( n > 4 && ( !CMP_FILENAME(iname+n-4, EXTSEP_S "gpg")
|| !CMP_FILENAME(iname+n-4, EXTSEP_S "pgp") || !CMP_FILENAME(iname+n-4, EXTSEP_S "pgp")
|| !CMP_FILENAME(iname+n-4, EXTSEP_S "sig") || !CMP_FILENAME(iname+n-4, EXTSEP_S "sig")
|| !CMP_FILENAME(iname+n-4, EXTSEP_S "asc") ) ) { || !CMP_FILENAME(iname+n-4, EXTSEP_S "asc") ) ) {
char *buf = m_strdup( iname ); char *buf = xstrdup ( iname );
buf[n-4] = 0; buf[n-4] = 0;
return buf; return buf;
} }
else if( n > 5 && !CMP_FILENAME(iname+n-5, EXTSEP_S "sign") ) { else if( n > 5 && !CMP_FILENAME(iname+n-5, EXTSEP_S "sign") ) {
char *buf = m_strdup( iname ); char *buf = xstrdup ( iname );
buf[n-5] = 0; buf[n-5] = 0;
return buf; return buf;
} }
@ -142,19 +144,19 @@ ask_outfile_name( const char *name, size_t namelen )
n = strlen(s) + namelen + 10; n = strlen(s) + namelen + 10;
defname = name && namelen? make_printable_string( name, namelen, 0): NULL; defname = name && namelen? make_printable_string( name, namelen, 0): NULL;
prompt = m_alloc(n); prompt = xmalloc (n);
if( defname ) if( defname )
sprintf(prompt, "%s [%s]: ", s, defname ); sprintf(prompt, "%s [%s]: ", s, defname );
else else
sprintf(prompt, "%s: ", s ); sprintf(prompt, "%s: ", s );
fname = cpr_get("openfile.askoutname", prompt ); fname = cpr_get("openfile.askoutname", prompt );
cpr_kill_prompt(); cpr_kill_prompt();
m_free(prompt); xfree (prompt);
if( !*fname ) { if( !*fname ) {
m_free( fname ); fname = NULL; xfree ( fname ); fname = NULL;
fname = defname; defname = NULL; fname = defname; defname = NULL;
} }
m_free(defname); xfree (defname);
if (fname) if (fname)
trim_spaces (fname); trim_spaces (fname);
return fname; return fname;
@ -163,21 +165,21 @@ ask_outfile_name( const char *name, size_t namelen )
/**************** /****************
* Make an output filename for the inputfile INAME. * Make an output filename for the inputfile INAME.
* Returns an IOBUF and an errorcode * Returns an iobuf_t and an errorcode
* Mode 0 = use ".gpg" * Mode 0 = use ".gpg"
* 1 = use ".asc" * 1 = use ".asc"
* 2 = use ".sig" * 2 = use ".sig"
*/ */
int int
open_outfile( const char *iname, int mode, IOBUF *a ) open_outfile( const char *iname, int mode, iobuf_t *a )
{ {
int rc = 0; int rc = 0;
*a = NULL; *a = NULL;
if( (!iname || (*iname=='-' && !iname[1])) && !opt.outfile ) { if( (!iname || (*iname=='-' && !iname[1])) && !opt.outfile ) {
if( !(*a = iobuf_create(NULL)) ) { if( !(*a = iobuf_create(NULL)) ) {
rc = gpg_error_from_errno (errno);
log_error(_("%s: can't open: %s\n"), "[stdout]", strerror(errno) ); log_error(_("%s: can't open: %s\n"), "[stdout]", strerror(errno) );
rc = G10ERR_CREATE_FILE;
} }
else if( opt.verbose ) else if( opt.verbose )
log_info(_("writing to stdout\n")); log_info(_("writing to stdout\n"));
@ -205,7 +207,7 @@ open_outfile( const char *iname, int mode, IOBUF *a )
const char *newsfx = mode==1 ? ".asc" : const char *newsfx = mode==1 ? ".asc" :
mode==2 ? ".sig" : ".gpg"; mode==2 ? ".sig" : ".gpg";
buf = m_alloc(strlen(iname)+4+1); buf = xmalloc (strlen(iname)+4+1);
strcpy(buf,iname); strcpy(buf,iname);
dot = strchr(buf, '.' ); dot = strchr(buf, '.' );
if ( dot && dot > buf && dot[1] && strlen(dot) <= 4 if ( dot && dot > buf && dot[1] && strlen(dot) <= 4
@ -221,7 +223,7 @@ open_outfile( const char *iname, int mode, IOBUF *a )
if (!buf) if (!buf)
#endif /* USE_ONLY_8DOT3 */ #endif /* USE_ONLY_8DOT3 */
{ {
buf = m_alloc(strlen(iname)+4+1); buf = xmalloc (strlen(iname)+4+1);
strcpy(stpcpy(buf,iname), mode==1 ? EXTSEP_S "asc" : strcpy(stpcpy(buf,iname), mode==1 ? EXTSEP_S "asc" :
mode==2 ? EXTSEP_S "sig" : EXTSEP_S "gpg"); mode==2 ? EXTSEP_S "sig" : EXTSEP_S "gpg");
} }
@ -234,11 +236,11 @@ open_outfile( const char *iname, int mode, IOBUF *a )
char *tmp = ask_outfile_name (NULL, 0); char *tmp = ask_outfile_name (NULL, 0);
if ( !tmp || !*tmp ) if ( !tmp || !*tmp )
{ {
m_free (tmp); xfree (tmp);
rc = G10ERR_FILE_EXISTS; rc = GPG_ERR_EEXIST;
break; break;
} }
m_free (buf); xfree (buf);
name = buf = tmp; name = buf = tmp;
} }
@ -246,13 +248,13 @@ open_outfile( const char *iname, int mode, IOBUF *a )
{ {
if( !(*a = iobuf_create( name )) ) if( !(*a = iobuf_create( name )) )
{ {
rc = gpg_error_from_errno (errno);
log_error(_("%s: can't create: %s\n"), name, strerror(errno) ); log_error(_("%s: can't create: %s\n"), name, strerror(errno) );
rc = G10ERR_CREATE_FILE;
} }
else if( opt.verbose ) else if( opt.verbose )
log_info(_("writing to `%s'\n"), name ); log_info(_("writing to `%s'\n"), name );
} }
m_free(buf); xfree (buf);
} }
return rc; return rc;
@ -263,10 +265,10 @@ open_outfile( const char *iname, int mode, IOBUF *a )
* Try to open a file without the extension ".sig" or ".asc" * Try to open a file without the extension ".sig" or ".asc"
* Return NULL if such a file is not available. * Return NULL if such a file is not available.
*/ */
IOBUF iobuf_t
open_sigfile( const char *iname, progress_filter_context_t *pfx ) open_sigfile( const char *iname, progress_filter_context_t *pfx )
{ {
IOBUF a = NULL; iobuf_t a = NULL;
size_t len; size_t len;
if( iname && !(*iname == '-' && !iname[1]) ) { if( iname && !(*iname == '-' && !iname[1]) ) {
@ -275,14 +277,14 @@ open_sigfile( const char *iname, progress_filter_context_t *pfx )
|| ( len > 5 && !strcmp(iname + len - 5, EXTSEP_S "sign") ) || ( len > 5 && !strcmp(iname + len - 5, EXTSEP_S "sign") )
|| !strcmp(iname + len - 4, EXTSEP_S "asc")) ) { || !strcmp(iname + len - 4, EXTSEP_S "asc")) ) {
char *buf; char *buf;
buf = m_strdup(iname); buf = xstrdup (iname);
buf[len-(buf[len-1]=='n'?5:4)] = 0 ; buf[len-(buf[len-1]=='n'?5:4)] = 0 ;
a = iobuf_open( buf ); a = iobuf_open( buf );
if( a && opt.verbose ) if( a && opt.verbose )
log_info(_("assuming signed data in `%s'\n"), buf ); log_info(_("assuming signed data in `%s'\n"), buf );
if (a && pfx) if (a && pfx)
handle_progress (pfx, a, buf); handle_progress (pfx, a, buf);
m_free(buf); xfree (buf);
} }
} }
return a; return a;
@ -306,12 +308,12 @@ copy_options_file( const char *destdir )
if( opt.dry_run ) if( opt.dry_run )
return; return;
fname = m_alloc( strlen(datadir) + strlen(destdir) + 15 ); fname = xmalloc ( strlen(datadir) + strlen(destdir) + 15 );
strcpy(stpcpy(fname, datadir), DIRSEP_S "options" SKELEXT ); strcpy(stpcpy(fname, datadir), DIRSEP_S "options" SKELEXT );
src = fopen( fname, "r" ); src = fopen( fname, "r" );
if( !src ) { if( !src ) {
log_error(_("%s: can't open: %s\n"), fname, strerror(errno) ); log_error(_("%s: can't open: %s\n"), fname, strerror(errno) );
m_free(fname); xfree (fname);
return; return;
} }
strcpy(stpcpy(fname, destdir), DIRSEP_S "gpg" EXTSEP_S "conf" ); strcpy(stpcpy(fname, destdir), DIRSEP_S "gpg" EXTSEP_S "conf" );
@ -321,7 +323,7 @@ copy_options_file( const char *destdir )
if( !dst ) { if( !dst ) {
log_error(_("%s: can't create: %s\n"), fname, strerror(errno) ); log_error(_("%s: can't create: %s\n"), fname, strerror(errno) );
fclose( src ); fclose( src );
m_free(fname); xfree (fname);
return; return;
} }
@ -351,7 +353,7 @@ copy_options_file( const char *destdir )
log_info (_("WARNING: options in `%s'" log_info (_("WARNING: options in `%s'"
" are not yet active during this run\n"), " are not yet active during this run\n"),
fname); fname);
m_free(fname); xfree (fname);
} }

View File

@ -1,6 +1,6 @@
/* packet.h - packet definitions /* packet.h - packet definitions
* Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003 * Copyright (C) 1998, 1999, 2000, 2001, 2002,
* Free Software Foundation, Inc. * 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -22,9 +22,12 @@
#ifndef G10_PACKET_H #ifndef G10_PACKET_H
#define G10_PACKET_H #define G10_PACKET_H
#include "gpg.h"
#include <gcrypt.h>
#include "types.h" #include "types.h"
#include "iobuf.h" #include "../common/iobuf.h"
#include "mpi.h" #include "../jnlib/strlist.h"
#include "cipher.h" #include "cipher.h"
#include "filter.h" #include "filter.h"
#include "global.h" #include "global.h"
@ -96,7 +99,7 @@ typedef struct {
byte version; byte version;
byte pubkey_algo; /* algorithm used for public key scheme */ byte pubkey_algo; /* algorithm used for public key scheme */
byte throw_keyid; byte throw_keyid;
MPI data[PUBKEY_MAX_NENC]; gcry_mpi_t data[PUBKEY_MAX_NENC];
} PKT_pubkey_enc; } PKT_pubkey_enc;
@ -149,7 +152,7 @@ typedef struct {
subpktarea_t *hashed; /* all subpackets with hashed data (v4 only) */ subpktarea_t *hashed; /* all subpackets with hashed data (v4 only) */
subpktarea_t *unhashed; /* ditto for unhashed data */ subpktarea_t *unhashed; /* ditto for unhashed data */
byte digest_start[2]; /* first 2 bytes of the digest */ byte digest_start[2]; /* first 2 bytes of the digest */
MPI data[PUBKEY_MAX_NSIG]; gcry_mpi_t data[PUBKEY_MAX_NSIG];
} PKT_signature; } PKT_signature;
#define ATTRIB_IMAGE 1 #define ATTRIB_IMAGE 1
@ -221,7 +224,7 @@ typedef struct {
byte trust_depth; byte trust_depth;
byte trust_value; byte trust_value;
const byte *trust_regexp; const byte *trust_regexp;
MPI pkey[PUBKEY_MAX_NPKEY]; gcry_mpi_t pkey[PUBKEY_MAX_NPKEY];
} PKT_public_key; } PKT_public_key;
/* Evaluates as true if the pk is disabled, and false if it isn't. If /* Evaluates as true if the pk is disabled, and false if it isn't. If
@ -255,7 +258,7 @@ typedef struct {
byte ivlen; /* used length of the iv */ byte ivlen; /* used length of the iv */
byte iv[16]; /* initialization vector for CFB mode */ byte iv[16]; /* initialization vector for CFB mode */
} protect; } protect;
MPI skey[PUBKEY_MAX_NSKEY]; gcry_mpi_t skey[PUBKEY_MAX_NSKEY];
u16 csum; /* checksum */ u16 csum; /* checksum */
} PKT_secret_key; } PKT_secret_key;
@ -269,7 +272,7 @@ typedef struct {
u32 len; /* reserved */ u32 len; /* reserved */
byte new_ctb; byte new_ctb;
byte algorithm; byte algorithm;
IOBUF buf; /* IOBUF reference */ iobuf_t buf; /* iobuf_t reference */
} PKT_compressed; } PKT_compressed;
typedef struct { typedef struct {
@ -277,7 +280,7 @@ typedef struct {
int extralen; /* this is (blocksize+2) */ int extralen; /* this is (blocksize+2) */
byte new_ctb; /* uses a new CTB */ byte new_ctb; /* uses a new CTB */
byte mdc_method; /* > 0: integrity protected encrypted data packet */ byte mdc_method; /* > 0: integrity protected encrypted data packet */
IOBUF buf; /* IOBUF reference */ iobuf_t buf; /* iobuf_t reference */
} PKT_encrypted; } PKT_encrypted;
typedef struct { typedef struct {
@ -291,7 +294,7 @@ typedef struct {
typedef struct { typedef struct {
u32 len; /* length of encrypted data */ u32 len; /* length of encrypted data */
IOBUF buf; /* IOBUF reference */ iobuf_t buf; /* iobuf_t reference */
byte new_ctb; byte new_ctb;
byte is_partial; /* partial length encoded */ byte is_partial; /* partial length encoded */
int mode; int mode;
@ -365,25 +368,25 @@ typedef enum {
/*-- mainproc.c --*/ /*-- mainproc.c --*/
int proc_packets( void *ctx, IOBUF a ); int proc_packets( void *ctx, iobuf_t a );
int proc_signature_packets( void *ctx, IOBUF a, int proc_signature_packets( void *ctx, iobuf_t a,
STRLIST signedfiles, const char *sigfile ); STRLIST signedfiles, const char *sigfile );
int proc_encryption_packets( void *ctx, IOBUF a ); int proc_encryption_packets( void *ctx, iobuf_t a );
int list_packets( IOBUF a ); int list_packets( iobuf_t a );
/*-- parse-packet.c --*/ /*-- parse-packet.c --*/
int set_packet_list_mode( int mode ); int set_packet_list_mode( int mode );
#if DEBUG_PARSE_PACKET #if DEBUG_PARSE_PACKET
int dbg_search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid, int dbg_search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid,
const char* file, int lineno ); const char* file, int lineno );
int dbg_parse_packet( IOBUF inp, PACKET *ret_pkt, int dbg_parse_packet( iobuf_t inp, PACKET *ret_pkt,
const char* file, int lineno ); const char* file, int lineno );
int dbg_copy_all_packets( IOBUF inp, IOBUF out, int dbg_copy_all_packets( iobuf_t inp, iobuf_t out,
const char* file, int lineno ); const char* file, int lineno );
int dbg_copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff, int dbg_copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff,
const char* file, int lineno ); const char* file, int lineno );
int dbg_skip_some_packets( IOBUF inp, unsigned n, int dbg_skip_some_packets( iobuf_t inp, unsigned n,
const char* file, int lineno ); const char* file, int lineno );
#define search_packet( a,b,c,d ) \ #define search_packet( a,b,c,d ) \
dbg_search_packet( (a), (b), (c), (d), __FILE__, __LINE__ ) dbg_search_packet( (a), (b), (c), (d), __FILE__, __LINE__ )
@ -396,11 +399,11 @@ int dbg_skip_some_packets( IOBUF inp, unsigned n,
#define skip_some_packets( a,b ) \ #define skip_some_packets( a,b ) \
dbg_skip_some_packets((a),(b), __FILE__, __LINE__ ) dbg_skip_some_packets((a),(b), __FILE__, __LINE__ )
#else #else
int search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid ); int search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid );
int parse_packet( IOBUF inp, PACKET *ret_pkt); int parse_packet( iobuf_t inp, PACKET *ret_pkt);
int copy_all_packets( IOBUF inp, IOBUF out ); int copy_all_packets( iobuf_t inp, iobuf_t out );
int copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff ); int copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff );
int skip_some_packets( IOBUF inp, unsigned n ); int skip_some_packets( iobuf_t inp, unsigned n );
#endif #endif
const byte *enum_sig_subpkt ( const subpktarea_t *subpkts, const byte *enum_sig_subpkt ( const subpktarea_t *subpkts,
@ -421,7 +424,7 @@ PACKET *create_gpg_control ( ctrlpkttype_t type,
size_t datalen ); size_t datalen );
/*-- build-packet.c --*/ /*-- build-packet.c --*/
int build_packet( IOBUF inp, PACKET *pkt ); int build_packet( iobuf_t inp, PACKET *pkt );
u32 calc_packet_length( PACKET *pkt ); u32 calc_packet_length( PACKET *pkt );
void hash_public_key( MD_HANDLE md, PKT_public_key *pk ); void hash_public_key( MD_HANDLE md, PKT_public_key *pk );
void build_sig_subpkt( PKT_signature *sig, sigsubpkttype_t type, void build_sig_subpkt( PKT_signature *sig, sigsubpkttype_t type,
@ -474,19 +477,19 @@ int get_override_session_key( DEK *dek, const char *string );
/*-- compress.c --*/ /*-- compress.c --*/
int handle_compressed( void *ctx, PKT_compressed *cd, int handle_compressed( void *ctx, PKT_compressed *cd,
int (*callback)(IOBUF, void *), void *passthru ); int (*callback)(iobuf_t, void *), void *passthru );
/*-- encr-data.c --*/ /*-- encr-data.c --*/
int decrypt_data( void *ctx, PKT_encrypted *ed, DEK *dek ); int decrypt_data( void *ctx, PKT_encrypted *ed, DEK *dek );
/*-- plaintext.c --*/ /*-- plaintext.c --*/
int handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx, int handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
int nooutput, int clearsig ); int nooutput, int clearsig, int *create_failed );
int ask_for_detached_datafile( MD_HANDLE md, MD_HANDLE md2, int ask_for_detached_datafile( MD_HANDLE md, MD_HANDLE md2,
const char *inname, int textmode ); const char *inname, int textmode );
/*-- comment.c --*/ /*-- comment.c --*/
int write_comment( IOBUF out, const char *s ); int write_comment( iobuf_t out, const char *s );
/*-- sign.c --*/ /*-- sign.c --*/
int make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk, int make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk,

View File

@ -40,48 +40,48 @@
static int mpi_print_mode = 0; static int mpi_print_mode = 0;
static int list_mode = 0; static int list_mode = 0;
static int parse( IOBUF inp, PACKET *pkt, int onlykeypkts, static int parse( iobuf_t inp, PACKET *pkt, int onlykeypkts,
off_t *retpos, int *skip, IOBUF out, int do_skip off_t *retpos, int *skip, iobuf_t out, int do_skip
#ifdef DEBUG_PARSE_PACKET #ifdef DEBUG_PARSE_PACKET
,const char *dbg_w, const char *dbg_f, int dbg_l ,const char *dbg_w, const char *dbg_f, int dbg_l
#endif #endif
); );
static int copy_packet( IOBUF inp, IOBUF out, int pkttype, static int copy_packet( iobuf_t inp, iobuf_t out, int pkttype,
unsigned long pktlen ); unsigned long pktlen );
static void skip_packet( IOBUF inp, int pkttype, unsigned long pktlen ); static void skip_packet( iobuf_t inp, int pkttype, unsigned long pktlen );
static void skip_rest( IOBUF inp, unsigned long pktlen ); static void skip_rest( iobuf_t inp, unsigned long pktlen );
static void *read_rest( IOBUF inp, size_t pktlen ); static void *read_rest( iobuf_t inp, size_t pktlen );
static int parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_symkeyenc( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet ); PACKET *packet );
static int parse_pubkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_pubkeyenc( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet ); PACKET *packet );
static int parse_signature( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_signature( iobuf_t inp, int pkttype, unsigned long pktlen,
PKT_signature *sig ); PKT_signature *sig );
static int parse_onepass_sig( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_onepass_sig( iobuf_t inp, int pkttype, unsigned long pktlen,
PKT_onepass_sig *ops ); PKT_onepass_sig *ops );
static int parse_key( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_key( iobuf_t inp, int pkttype, unsigned long pktlen,
byte *hdr, int hdrlen, PACKET *packet ); byte *hdr, int hdrlen, PACKET *packet );
static int parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_user_id( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet ); PACKET *packet );
static int parse_attribute( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_attribute( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet ); PACKET *packet );
static int parse_comment( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_comment( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet ); PACKET *packet );
static void parse_trust( IOBUF inp, int pkttype, unsigned long pktlen, static void parse_trust( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet ); PACKET *packet );
static int parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_plaintext( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet, int new_ctb); PACKET *packet, int new_ctb);
static int parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_compressed( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet, int new_ctb ); PACKET *packet, int new_ctb );
static int parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_encrypted( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet, int new_ctb); PACKET *packet, int new_ctb);
static int parse_mdc( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_mdc( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet, int new_ctb); PACKET *packet, int new_ctb);
static int parse_gpg_control( IOBUF inp, int pkttype, unsigned long pktlen, static int parse_gpg_control( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *packet ); PACKET *packet );
static unsigned short static unsigned short
read_16(IOBUF inp) read_16(iobuf_t inp)
{ {
unsigned short a; unsigned short a;
a = iobuf_get_noeof(inp) << 8; a = iobuf_get_noeof(inp) << 8;
@ -90,7 +90,7 @@ read_16(IOBUF inp)
} }
static unsigned long static unsigned long
read_32(IOBUF inp) read_32(iobuf_t inp)
{ {
unsigned long a; unsigned long a;
a = iobuf_get_noeof(inp) << 24; a = iobuf_get_noeof(inp) << 24;
@ -106,7 +106,7 @@ set_packet_list_mode( int mode )
{ {
int old = list_mode; int old = list_mode;
list_mode = mode; list_mode = mode;
mpi_print_mode = DBG_MPI; /* FIXME(gcrypt) mpi_print_mode = DBG_MPI; */
return old; return old;
} }
@ -133,7 +133,7 @@ unknown_pubkey_warning( int algo )
*/ */
#ifdef DEBUG_PARSE_PACKET #ifdef DEBUG_PARSE_PACKET
int int
dbg_parse_packet( IOBUF inp, PACKET *pkt, const char *dbg_f, int dbg_l ) dbg_parse_packet( iobuf_t inp, PACKET *pkt, const char *dbg_f, int dbg_l )
{ {
int skip, rc; int skip, rc;
@ -144,7 +144,7 @@ dbg_parse_packet( IOBUF inp, PACKET *pkt, const char *dbg_f, int dbg_l )
} }
#else #else
int int
parse_packet( IOBUF inp, PACKET *pkt ) parse_packet( iobuf_t inp, PACKET *pkt )
{ {
int skip, rc; int skip, rc;
@ -160,7 +160,7 @@ parse_packet( IOBUF inp, PACKET *pkt )
*/ */
#ifdef DEBUG_PARSE_PACKET #ifdef DEBUG_PARSE_PACKET
int int
dbg_search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid, dbg_search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid,
const char *dbg_f, int dbg_l ) const char *dbg_f, int dbg_l )
{ {
int skip, rc; int skip, rc;
@ -172,7 +172,7 @@ dbg_search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid,
} }
#else #else
int int
search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid ) search_packet( iobuf_t inp, PACKET *pkt, off_t *retpos, int with_uid )
{ {
int skip, rc; int skip, rc;
@ -188,7 +188,7 @@ search_packet( IOBUF inp, PACKET *pkt, off_t *retpos, int with_uid )
*/ */
#ifdef DEBUG_PARSE_PACKET #ifdef DEBUG_PARSE_PACKET
int int
dbg_copy_all_packets( IOBUF inp, IOBUF out, dbg_copy_all_packets( iobuf_t inp, iobuf_t out,
const char *dbg_f, int dbg_l ) const char *dbg_f, int dbg_l )
{ {
PACKET pkt; PACKET pkt;
@ -200,7 +200,7 @@ dbg_copy_all_packets( IOBUF inp, IOBUF out,
} }
#else #else
int int
copy_all_packets( IOBUF inp, IOBUF out ) copy_all_packets( iobuf_t inp, iobuf_t out )
{ {
PACKET pkt; PACKET pkt;
int skip, rc=0; int skip, rc=0;
@ -217,7 +217,7 @@ copy_all_packets( IOBUF inp, IOBUF out )
*/ */
#ifdef DEBUG_PARSE_PACKET #ifdef DEBUG_PARSE_PACKET
int int
dbg_copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff, dbg_copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff,
const char *dbg_f, int dbg_l ) const char *dbg_f, int dbg_l )
{ {
PACKET pkt; PACKET pkt;
@ -232,7 +232,7 @@ dbg_copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff,
} }
#else #else
int int
copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff ) copy_some_packets( iobuf_t inp, iobuf_t out, off_t stopoff )
{ {
PACKET pkt; PACKET pkt;
int skip, rc=0; int skip, rc=0;
@ -250,7 +250,7 @@ copy_some_packets( IOBUF inp, IOBUF out, off_t stopoff )
*/ */
#ifdef DEBUG_PARSE_PACKET #ifdef DEBUG_PARSE_PACKET
int int
dbg_skip_some_packets( IOBUF inp, unsigned n, dbg_skip_some_packets( iobuf_t inp, unsigned n,
const char *dbg_f, int dbg_l ) const char *dbg_f, int dbg_l )
{ {
int skip, rc=0; int skip, rc=0;
@ -264,7 +264,7 @@ dbg_skip_some_packets( IOBUF inp, unsigned n,
} }
#else #else
int int
skip_some_packets( IOBUF inp, unsigned n ) skip_some_packets( iobuf_t inp, unsigned n )
{ {
int skip, rc=0; int skip, rc=0;
PACKET pkt; PACKET pkt;
@ -286,8 +286,8 @@ skip_some_packets( IOBUF inp, unsigned n )
* if OUT is not NULL, a special copymode is used. * if OUT is not NULL, a special copymode is used.
*/ */
static int static int
parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos, parse( iobuf_t inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
int *skip, IOBUF out, int do_skip int *skip, iobuf_t out, int do_skip
#ifdef DEBUG_PARSE_PACKET #ifdef DEBUG_PARSE_PACKET
,const char *dbg_w, const char *dbg_f, int dbg_l ,const char *dbg_w, const char *dbg_f, int dbg_l
#endif #endif
@ -313,7 +313,7 @@ parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
hdr[hdrlen++] = ctb; hdr[hdrlen++] = ctb;
if( !(ctb & 0x80) ) { if( !(ctb & 0x80) ) {
log_error("%s: invalid packet (ctb=%02x)\n", iobuf_where(inp), ctb ); log_error("%s: invalid packet (ctb=%02x)\n", iobuf_where(inp), ctb );
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
pktlen = 0; pktlen = 0;
@ -322,7 +322,7 @@ parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
pkttype = ctb & 0x3f; pkttype = ctb & 0x3f;
if( (c = iobuf_get(inp)) == -1 ) { if( (c = iobuf_get(inp)) == -1 ) {
log_error("%s: 1st length byte missing\n", iobuf_where(inp) ); log_error("%s: 1st length byte missing\n", iobuf_where(inp) );
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
if (pkttype == PKT_COMPRESSED) { if (pkttype == PKT_COMPRESSED) {
@ -338,7 +338,7 @@ parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
if( (c = iobuf_get(inp)) == -1 ) { if( (c = iobuf_get(inp)) == -1 ) {
log_error("%s: 2nd length byte missing\n", log_error("%s: 2nd length byte missing\n",
iobuf_where(inp) ); iobuf_where(inp) );
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
hdr[hdrlen++] = c; hdr[hdrlen++] = c;
@ -351,7 +351,7 @@ parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
if( (c = iobuf_get(inp)) == -1 ) { if( (c = iobuf_get(inp)) == -1 ) {
log_error("%s: 4 byte length invalid\n", log_error("%s: 4 byte length invalid\n",
iobuf_where(inp) ); iobuf_where(inp) );
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
pktlen |= (hdr[hdrlen++] = c ); pktlen |= (hdr[hdrlen++] = c );
@ -387,9 +387,8 @@ parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
} }
if( out && pkttype ) { if( out && pkttype ) {
if( iobuf_write( out, hdr, hdrlen ) == -1 ) rc = iobuf_write( out, hdr, hdrlen );
rc = G10ERR_WRITE_FILE; if (!rc)
else
rc = copy_packet(inp, out, pkttype, pktlen ); rc = copy_packet(inp, out, pkttype, pktlen );
goto leave; goto leave;
} }
@ -419,16 +418,16 @@ parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
#endif #endif
} }
pkt->pkttype = pkttype; pkt->pkttype = pkttype;
rc = G10ERR_UNKNOWN_PACKET; /* default error */ rc = GPG_ERR_UNKNOWN_PACKET; /* default error */
switch( pkttype ) { switch( pkttype ) {
case PKT_PUBLIC_KEY: case PKT_PUBLIC_KEY:
case PKT_PUBLIC_SUBKEY: case PKT_PUBLIC_SUBKEY:
pkt->pkt.public_key = m_alloc_clear(sizeof *pkt->pkt.public_key ); pkt->pkt.public_key = xcalloc (1,sizeof *pkt->pkt.public_key );
rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt ); rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt );
break; break;
case PKT_SECRET_KEY: case PKT_SECRET_KEY:
case PKT_SECRET_SUBKEY: case PKT_SECRET_SUBKEY:
pkt->pkt.secret_key = m_alloc_clear(sizeof *pkt->pkt.secret_key ); pkt->pkt.secret_key = xcalloc (1,sizeof *pkt->pkt.secret_key );
rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt ); rc = parse_key(inp, pkttype, pktlen, hdr, hdrlen, pkt );
break; break;
case PKT_SYMKEY_ENC: case PKT_SYMKEY_ENC:
@ -438,11 +437,11 @@ parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
rc = parse_pubkeyenc(inp, pkttype, pktlen, pkt ); rc = parse_pubkeyenc(inp, pkttype, pktlen, pkt );
break; break;
case PKT_SIGNATURE: case PKT_SIGNATURE:
pkt->pkt.signature = m_alloc_clear(sizeof *pkt->pkt.signature ); pkt->pkt.signature = xcalloc (1,sizeof *pkt->pkt.signature );
rc = parse_signature(inp, pkttype, pktlen, pkt->pkt.signature ); rc = parse_signature(inp, pkttype, pktlen, pkt->pkt.signature );
break; break;
case PKT_ONEPASS_SIG: case PKT_ONEPASS_SIG:
pkt->pkt.onepass_sig = m_alloc_clear(sizeof *pkt->pkt.onepass_sig ); pkt->pkt.onepass_sig = xcalloc (1,sizeof *pkt->pkt.onepass_sig );
rc = parse_onepass_sig(inp, pkttype, pktlen, pkt->pkt.onepass_sig ); rc = parse_onepass_sig(inp, pkttype, pktlen, pkt->pkt.onepass_sig );
break; break;
case PKT_USER_ID: case PKT_USER_ID:
@ -483,7 +482,7 @@ parse( IOBUF inp, PACKET *pkt, int onlykeypkts, off_t *retpos,
leave: leave:
if( !rc && iobuf_error(inp) ) if( !rc && iobuf_error(inp) )
rc = G10ERR_INV_KEYRING; rc = GPG_ERR_INV_KEYRING;
return rc; return rc;
} }
@ -505,31 +504,31 @@ dump_hex_line( int c, int *i )
static int static int
copy_packet( IOBUF inp, IOBUF out, int pkttype, unsigned long pktlen ) copy_packet( iobuf_t inp, iobuf_t out, int pkttype, unsigned long pktlen )
{ {
int n; int rc, n;
char buf[100]; char buf[100];
if( iobuf_in_block_mode(inp) ) { if( iobuf_in_block_mode(inp) ) {
while( (n = iobuf_read( inp, buf, 100 )) != -1 ) while( (n = iobuf_read( inp, buf, 100 )) != -1 )
if( iobuf_write(out, buf, n ) ) if( (rc = iobuf_write(out, buf, n )) )
return G10ERR_WRITE_FILE; /* write error */ return rc; /* write error */
} }
else if( !pktlen && pkttype == PKT_COMPRESSED ) { else if( !pktlen && pkttype == PKT_COMPRESSED ) {
log_debug("copy_packet: compressed!\n"); log_debug("copy_packet: compressed!\n");
/* compressed packet, copy till EOF */ /* compressed packet, copy till EOF */
while( (n = iobuf_read( inp, buf, 100 )) != -1 ) while( (n = iobuf_read( inp, buf, 100 )) != -1 )
if( iobuf_write(out, buf, n ) ) if( (rc = iobuf_write(out, buf, n )) )
return G10ERR_WRITE_FILE; /* write error */ return rc; /* write error */
} }
else { else {
for( ; pktlen; pktlen -= n ) { for( ; pktlen; pktlen -= n ) {
n = pktlen > 100 ? 100 : pktlen; n = pktlen > 100 ? 100 : pktlen;
n = iobuf_read( inp, buf, n ); n = iobuf_read( inp, buf, n );
if( n == -1 ) if( n == -1 )
return G10ERR_READ_FILE; return GPG_ERR_GENERAL; /* FIXME(gcrypt): read error*/;
if( iobuf_write(out, buf, n ) ) if( (rc = iobuf_write(out, buf, n )) )
return G10ERR_WRITE_FILE; /* write error */ return rc; /* write error */
} }
} }
return 0; return 0;
@ -537,7 +536,7 @@ copy_packet( IOBUF inp, IOBUF out, int pkttype, unsigned long pktlen )
static void static void
skip_packet( IOBUF inp, int pkttype, unsigned long pktlen ) skip_packet( iobuf_t inp, int pkttype, unsigned long pktlen )
{ {
if( list_mode ) { if( list_mode ) {
if( pkttype == PKT_MARKER ) if( pkttype == PKT_MARKER )
@ -564,7 +563,7 @@ skip_packet( IOBUF inp, int pkttype, unsigned long pktlen )
} }
static void static void
skip_rest( IOBUF inp, unsigned long pktlen ) skip_rest( iobuf_t inp, unsigned long pktlen )
{ {
if( iobuf_in_block_mode(inp) ) { if( iobuf_in_block_mode(inp) ) {
while( iobuf_get(inp) != -1 ) while( iobuf_get(inp) != -1 )
@ -579,7 +578,7 @@ skip_rest( IOBUF inp, unsigned long pktlen )
static void * static void *
read_rest( IOBUF inp, size_t pktlen ) read_rest( iobuf_t inp, size_t pktlen )
{ {
byte *p; byte *p;
int i; int i;
@ -589,7 +588,7 @@ read_rest( IOBUF inp, size_t pktlen )
p = NULL; p = NULL;
} }
else { else {
p = m_alloc( pktlen ); p = xmalloc ( pktlen );
for(i=0; pktlen; pktlen--, i++ ) for(i=0; pktlen; pktlen--, i++ )
p[i] = iobuf_get(inp); p[i] = iobuf_get(inp);
} }
@ -599,7 +598,7 @@ read_rest( IOBUF inp, size_t pktlen )
static int static int
parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet ) parse_symkeyenc( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
{ {
PKT_symkey_enc *k; PKT_symkey_enc *k;
int rc = 0; int rc = 0;
@ -607,18 +606,18 @@ parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
if( pktlen < 4 ) { if( pktlen < 4 ) {
log_error("packet(%d) too short\n", pkttype); log_error("packet(%d) too short\n", pkttype);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
version = iobuf_get_noeof(inp); pktlen--; version = iobuf_get_noeof(inp); pktlen--;
if( version != 4 ) { if( version != 4 ) {
log_error("packet(%d) with unknown version %d\n", pkttype, version); log_error("packet(%d) with unknown version %d\n", pkttype, version);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
if( pktlen > 200 ) { /* (we encode the seskeylen in a byte) */ if( pktlen > 200 ) { /* (we encode the seskeylen in a byte) */
log_error("packet(%d) too large\n", pkttype); log_error("packet(%d) too large\n", pkttype);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
cipher_algo = iobuf_get_noeof(inp); pktlen--; cipher_algo = iobuf_get_noeof(inp); pktlen--;
@ -640,11 +639,11 @@ parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
} }
if( minlen > pktlen ) { if( minlen > pktlen ) {
log_error("packet with S2K %d too short\n", s2kmode ); log_error("packet with S2K %d too short\n", s2kmode );
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
seskeylen = pktlen - minlen; seskeylen = pktlen - minlen;
k = packet->pkt.symkey_enc = m_alloc_clear( sizeof *packet->pkt.symkey_enc k = packet->pkt.symkey_enc = xcalloc (1, sizeof *packet->pkt.symkey_enc
+ seskeylen - 1 ); + seskeylen - 1 );
k->version = version; k->version = version;
k->cipher_algo = cipher_algo; k->cipher_algo = cipher_algo;
@ -681,23 +680,23 @@ parse_symkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
} }
static int static int
parse_pubkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet ) parse_pubkeyenc( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
{ {
unsigned int n; unsigned int n;
int rc = 0; int rc = 0;
int i, ndata; int i, ndata;
PKT_pubkey_enc *k; PKT_pubkey_enc *k;
k = packet->pkt.pubkey_enc = m_alloc_clear(sizeof *packet->pkt.pubkey_enc); k = packet->pkt.pubkey_enc = xcalloc (1,sizeof *packet->pkt.pubkey_enc);
if( pktlen < 12 ) { if( pktlen < 12 ) {
log_error("packet(%d) too short\n", pkttype); log_error("packet(%d) too short\n", pkttype);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
k->version = iobuf_get_noeof(inp); pktlen--; k->version = iobuf_get_noeof(inp); pktlen--;
if( k->version != 2 && k->version != 3 ) { if( k->version != 2 && k->version != 3 ) {
log_error("packet(%d) with unknown version %d\n", pkttype, k->version); log_error("packet(%d) with unknown version %d\n", pkttype, k->version);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
k->keyid[0] = read_32(inp); pktlen -= 4; k->keyid[0] = read_32(inp); pktlen -= 4;
@ -725,7 +724,7 @@ parse_pubkeyenc( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
putchar('\n'); putchar('\n');
} }
if (!k->data[i]) if (!k->data[i])
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
} }
} }
@ -1151,7 +1150,7 @@ void parse_revkeys(PKT_signature *sig)
if(len==sizeof(struct revocation_key) && if(len==sizeof(struct revocation_key) &&
(revkey->class&0x80)) /* 0x80 bit must be set */ (revkey->class&0x80)) /* 0x80 bit must be set */
{ {
sig->revkey=m_realloc(sig->revkey, sig->revkey=xrealloc(sig->revkey,
sizeof(struct revocation_key *)*(sig->numrevkeys+1)); sizeof(struct revocation_key *)*(sig->numrevkeys+1));
sig->revkey[sig->numrevkeys]=revkey; sig->revkey[sig->numrevkeys]=revkey;
sig->numrevkeys++; sig->numrevkeys++;
@ -1160,7 +1159,7 @@ void parse_revkeys(PKT_signature *sig)
} }
static int static int
parse_signature( IOBUF inp, int pkttype, unsigned long pktlen, parse_signature( iobuf_t inp, int pkttype, unsigned long pktlen,
PKT_signature *sig ) PKT_signature *sig )
{ {
int md5_len=0; int md5_len=0;
@ -1178,7 +1177,7 @@ parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
is_v4=1; is_v4=1;
else if( sig->version != 2 && sig->version != 3 ) { else if( sig->version != 2 && sig->version != 3 ) {
log_error("packet(%d) with unknown version %d\n", pkttype, sig->version); log_error("packet(%d) with unknown version %d\n", pkttype, sig->version);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
@ -1199,11 +1198,11 @@ parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
n = read_16(inp); pktlen -= 2; /* length of hashed data */ n = read_16(inp); pktlen -= 2; /* length of hashed data */
if( n > 10000 ) { if( n > 10000 ) {
log_error("signature packet: hashed data too long\n"); log_error("signature packet: hashed data too long\n");
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
if( n ) { if( n ) {
sig->hashed = m_alloc (sizeof (*sig->hashed) + n - 1 ); sig->hashed = xmalloc (sizeof (*sig->hashed) + n - 1 );
sig->hashed->size = n; sig->hashed->size = n;
sig->hashed->len = n; sig->hashed->len = n;
if( iobuf_read (inp, sig->hashed->data, n ) != n ) { if( iobuf_read (inp, sig->hashed->data, n ) != n ) {
@ -1217,14 +1216,14 @@ parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
n = read_16(inp); pktlen -= 2; /* length of unhashed data */ n = read_16(inp); pktlen -= 2; /* length of unhashed data */
if( n > 10000 ) { if( n > 10000 ) {
log_error("signature packet: unhashed data too long\n"); log_error("signature packet: unhashed data too long\n");
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
if( n ) { if( n ) {
/* we add 8 extra bytes so that we have space for the signature /* we add 8 extra bytes so that we have space for the signature
* status cache. Well we are wasting this if there is a cache * status cache. Well we are wasting this if there is a cache
* packet already, but in the other case it avoids an realloc */ * packet already, but in the other case it avoids an realloc */
sig->unhashed = m_alloc (sizeof(*sig->unhashed) + n + 8 - 1 ); sig->unhashed = xmalloc (sizeof(*sig->unhashed) + n + 8 - 1 );
sig->unhashed->size = n + 8; sig->unhashed->size = n + 8;
sig->unhashed->len = n; sig->unhashed->len = n;
if( iobuf_read(inp, sig->unhashed->data, n ) != n ) { if( iobuf_read(inp, sig->unhashed->data, n ) != n ) {
@ -1239,7 +1238,7 @@ parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
if( pktlen < 5 ) { /* sanity check */ if( pktlen < 5 ) { /* sanity check */
log_error("packet(%d) too short\n", pkttype); log_error("packet(%d) too short\n", pkttype);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
@ -1357,7 +1356,7 @@ parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
putchar('\n'); putchar('\n');
} }
if (!sig->data[i]) if (!sig->data[i])
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
} }
} }
@ -1368,7 +1367,7 @@ parse_signature( IOBUF inp, int pkttype, unsigned long pktlen,
static int static int
parse_onepass_sig( IOBUF inp, int pkttype, unsigned long pktlen, parse_onepass_sig( iobuf_t inp, int pkttype, unsigned long pktlen,
PKT_onepass_sig *ops ) PKT_onepass_sig *ops )
{ {
int version; int version;
@ -1376,13 +1375,13 @@ parse_onepass_sig( IOBUF inp, int pkttype, unsigned long pktlen,
if( pktlen < 13 ) { if( pktlen < 13 ) {
log_error("packet(%d) too short\n", pkttype); log_error("packet(%d) too short\n", pkttype);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
version = iobuf_get_noeof(inp); pktlen--; version = iobuf_get_noeof(inp); pktlen--;
if( version != 3 ) { if( version != 3 ) {
log_error("onepass_sig with unknown version %d\n", version); log_error("onepass_sig with unknown version %d\n", version);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
ops->sig_class = iobuf_get_noeof(inp); pktlen--; ops->sig_class = iobuf_get_noeof(inp); pktlen--;
@ -1405,13 +1404,13 @@ parse_onepass_sig( IOBUF inp, int pkttype, unsigned long pktlen,
} }
static MPI static gcry_mpi_t
read_protected_v3_mpi (IOBUF inp, unsigned long *length) read_protected_v3_mpi (iobuf_t inp, unsigned long *length)
{ {
int c; int c;
unsigned int nbits, nbytes; unsigned int nbits, nbytes;
unsigned char *buf, *p; unsigned char *buf, *p;
MPI val; gcry_mpi_t val;
if (*length < 2) if (*length < 2)
{ {
@ -1434,7 +1433,7 @@ read_protected_v3_mpi (IOBUF inp, unsigned long *length)
return NULL; return NULL;
} }
nbytes = (nbits+7) / 8; nbytes = (nbits+7) / 8;
buf = p = m_alloc (2 + nbytes); buf = p = xmalloc (2 + nbytes);
*p++ = nbits >> 8; *p++ = nbits >> 8;
*p++ = nbits; *p++ = nbits;
for (; nbytes && length; nbytes--, --*length) for (; nbytes && length; nbytes--, --*length)
@ -1442,18 +1441,18 @@ read_protected_v3_mpi (IOBUF inp, unsigned long *length)
if (nbytes) if (nbytes)
{ {
log_error ("packet shorter tham mpi\n"); log_error ("packet shorter tham mpi\n");
m_free (buf); xfree (buf);
return NULL; return NULL;
} }
/* convert buffer into an opaque MPI */ /* convert buffer into an opaque gcry_mpi_t */
val = mpi_set_opaque (NULL, buf, p-buf); val = mpi_set_opaque (NULL, buf, p-buf);
return val; return val;
} }
static int static int
parse_key( IOBUF inp, int pkttype, unsigned long pktlen, parse_key( iobuf_t inp, int pkttype, unsigned long pktlen,
byte *hdr, int hdrlen, PACKET *pkt ) byte *hdr, int hdrlen, PACKET *pkt )
{ {
int i, version, algorithm; int i, version, algorithm;
@ -1486,13 +1485,13 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
is_v4=1; is_v4=1;
else if( version != 2 && version != 3 ) { else if( version != 2 && version != 3 ) {
log_error("packet(%d) with unknown version %d\n", pkttype, version); log_error("packet(%d) with unknown version %d\n", pkttype, version);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
if( pktlen < 11 ) { if( pktlen < 11 ) {
log_error("packet(%d) too short\n", pkttype); log_error("packet(%d) too short\n", pkttype);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
@ -1579,7 +1578,7 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
putchar('\n'); putchar('\n');
} }
if (!sk->skey[i]) if (!sk->skey[i])
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
} }
if (rc) /* one of the MPIs were bad */ if (rc) /* one of the MPIs were bad */
goto leave; goto leave;
@ -1590,7 +1589,7 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
sk->protect.s2k.count = 0; sk->protect.s2k.count = 0;
if( sk->protect.algo == 254 || sk->protect.algo == 255 ) { if( sk->protect.algo == 254 || sk->protect.algo == 255 ) {
if( pktlen < 3 ) { if( pktlen < 3 ) {
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
sk->protect.sha1chk = (sk->protect.algo == 254); sk->protect.sha1chk = (sk->protect.algo == 254);
@ -1608,7 +1607,7 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
if( list_mode ) if( list_mode )
printf( "\tunknown S2K %d\n", printf( "\tunknown S2K %d\n",
sk->protect.s2k.mode ); sk->protect.s2k.mode );
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
/* here we know that it is a gnu extension /* here we know that it is a gnu extension
@ -1640,7 +1639,7 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
printf( "\tunknown %sS2K %d\n", printf( "\tunknown %sS2K %d\n",
sk->protect.s2k.mode < 1000? "":"GNU ", sk->protect.s2k.mode < 1000? "":"GNU ",
sk->protect.s2k.mode ); sk->protect.s2k.mode );
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
@ -1661,7 +1660,7 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
if( sk->protect.s2k.mode == 3 ) { if( sk->protect.s2k.mode == 3 ) {
if( pktlen < 1 ) { if( pktlen < 1 ) {
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
sk->protect.s2k.count = iobuf_get(inp); sk->protect.s2k.count = iobuf_get(inp);
@ -1701,7 +1700,7 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
sk->protect.ivlen = 0; sk->protect.ivlen = 0;
if( pktlen < sk->protect.ivlen ) { if( pktlen < sk->protect.ivlen ) {
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
for(i=0; i < sk->protect.ivlen && pktlen; i++, pktlen-- ) for(i=0; i < sk->protect.ivlen && pktlen; i++, pktlen-- )
@ -1722,7 +1721,7 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
* So we put the key into secure memory when we unprotect it. */ * So we put the key into secure memory when we unprotect it. */
if( sk->protect.s2k.mode == 1001 ) { if( sk->protect.s2k.mode == 1001 ) {
/* better set some dummy stuff here */ /* better set some dummy stuff here */
sk->skey[npkey] = mpi_set_opaque(NULL, m_strdup("dummydata"), 10); sk->skey[npkey] = mpi_set_opaque(NULL, xstrdup ("dummydata"), 10);
pktlen = 0; pktlen = 0;
} }
else if( is_v4 && sk->is_protected ) { else if( is_v4 && sk->is_protected ) {
@ -1755,7 +1754,7 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
} }
if (!sk->skey[i]) if (!sk->skey[i])
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
} }
if (rc) if (rc)
goto leave; goto leave;
@ -1784,7 +1783,7 @@ parse_key( IOBUF inp, int pkttype, unsigned long pktlen,
putchar('\n'); putchar('\n');
} }
if (!pk->pkey[i]) if (!pk->pkey[i])
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
} }
if (rc) if (rc)
goto leave; goto leave;
@ -1808,7 +1807,7 @@ parse_attribute_subpkts(PKT_user_id *uid)
int buflen=uid->attrib_len; int buflen=uid->attrib_len;
byte type; byte type;
m_free(uid->attribs); xfree (uid->attribs);
while(buflen) while(buflen)
{ {
@ -1831,7 +1830,7 @@ parse_attribute_subpkts(PKT_user_id *uid)
if( buflen < n ) if( buflen < n )
goto too_short; goto too_short;
attribs=m_realloc(attribs,(count+1)*sizeof(struct user_attribute)); attribs=xrealloc(attribs,(count+1)*sizeof(struct user_attribute));
memset(&attribs[count],0,sizeof(struct user_attribute)); memset(&attribs[count],0,sizeof(struct user_attribute));
type=*buffer; type=*buffer;
@ -1876,11 +1875,11 @@ static void setup_user_id(PACKET *packet)
} }
static int static int
parse_user_id( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet ) parse_user_id( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
{ {
byte *p; byte *p;
packet->pkt.user_id = m_alloc(sizeof *packet->pkt.user_id + pktlen); packet->pkt.user_id = xmalloc (sizeof *packet->pkt.user_id + pktlen);
packet->pkt.user_id->len = pktlen; packet->pkt.user_id->len = pktlen;
setup_user_id(packet); setup_user_id(packet);
@ -1939,17 +1938,17 @@ make_attribute_uidname(PKT_user_id *uid, size_t max_namelen)
} }
static int static int
parse_attribute( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet ) parse_attribute( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
{ {
byte *p; byte *p;
#define EXTRA_UID_NAME_SPACE 71 #define EXTRA_UID_NAME_SPACE 71
packet->pkt.user_id = m_alloc(sizeof *packet->pkt.user_id packet->pkt.user_id = xmalloc (sizeof *packet->pkt.user_id
+ EXTRA_UID_NAME_SPACE); + EXTRA_UID_NAME_SPACE);
setup_user_id(packet); setup_user_id(packet);
packet->pkt.user_id->attrib_data = m_alloc(pktlen); packet->pkt.user_id->attrib_data = xmalloc (pktlen);
packet->pkt.user_id->attrib_len = pktlen; packet->pkt.user_id->attrib_len = pktlen;
p = packet->pkt.user_id->attrib_data; p = packet->pkt.user_id->attrib_data;
for( ; pktlen; pktlen--, p++ ) for( ; pktlen; pktlen--, p++ )
@ -1970,11 +1969,11 @@ parse_attribute( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
static int static int
parse_comment( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet ) parse_comment( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *packet )
{ {
byte *p; byte *p;
packet->pkt.comment = m_alloc(sizeof *packet->pkt.comment + pktlen - 1); packet->pkt.comment = xmalloc (sizeof *packet->pkt.comment + pktlen - 1);
packet->pkt.comment->len = pktlen; packet->pkt.comment->len = pktlen;
p = packet->pkt.comment->data; p = packet->pkt.comment->data;
for( ; pktlen; pktlen--, p++ ) for( ; pktlen; pktlen--, p++ )
@ -1997,7 +1996,7 @@ parse_comment( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *packet )
static void static void
parse_trust( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *pkt ) parse_trust( iobuf_t inp, int pkttype, unsigned long pktlen, PACKET *pkt )
{ {
int c; int c;
@ -2005,7 +2004,7 @@ parse_trust( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *pkt )
{ {
c = iobuf_get_noeof(inp); c = iobuf_get_noeof(inp);
pktlen--; pktlen--;
pkt->pkt.ring_trust = m_alloc( sizeof *pkt->pkt.ring_trust ); pkt->pkt.ring_trust = xmalloc ( sizeof *pkt->pkt.ring_trust );
pkt->pkt.ring_trust->trustval = c; pkt->pkt.ring_trust->trustval = c;
pkt->pkt.ring_trust->sigcache = 0; pkt->pkt.ring_trust->sigcache = 0;
if (!c && pktlen==1) if (!c && pktlen==1)
@ -2031,7 +2030,7 @@ parse_trust( IOBUF inp, int pkttype, unsigned long pktlen, PACKET *pkt )
static int static int
parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen, parse_plaintext( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *pkt, int new_ctb ) PACKET *pkt, int new_ctb )
{ {
int rc = 0; int rc = 0;
@ -2042,7 +2041,7 @@ parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen,
if( pktlen && pktlen < 6 ) { if( pktlen && pktlen < 6 ) {
log_error("packet(%d) too short (%lu)\n", pkttype, (ulong)pktlen); log_error("packet(%d) too short (%lu)\n", pkttype, (ulong)pktlen);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
/* A packet length of zero indicates partial body length. A zero /* A packet length of zero indicates partial body length. A zero
@ -2052,7 +2051,7 @@ parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen,
partial=1; partial=1;
mode = iobuf_get_noeof(inp); if( pktlen ) pktlen--; mode = iobuf_get_noeof(inp); if( pktlen ) pktlen--;
namelen = iobuf_get_noeof(inp); if( pktlen ) pktlen--; namelen = iobuf_get_noeof(inp); if( pktlen ) pktlen--;
pt = pkt->pkt.plaintext = m_alloc(sizeof *pkt->pkt.plaintext + namelen -1); pt = pkt->pkt.plaintext = xmalloc (sizeof *pkt->pkt.plaintext + namelen -1);
pt->new_ctb = new_ctb; pt->new_ctb = new_ctb;
pt->mode = mode; pt->mode = mode;
pt->namelen = namelen; pt->namelen = namelen;
@ -2093,7 +2092,7 @@ parse_plaintext( IOBUF inp, int pkttype, unsigned long pktlen,
static int static int
parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen, parse_compressed( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *pkt, int new_ctb ) PACKET *pkt, int new_ctb )
{ {
PKT_compressed *zd; PKT_compressed *zd;
@ -2102,7 +2101,7 @@ parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen,
* (this should be the last object in a file or * (this should be the last object in a file or
* the compress algorithm should know the length) * the compress algorithm should know the length)
*/ */
zd = pkt->pkt.compressed = m_alloc(sizeof *pkt->pkt.compressed ); zd = pkt->pkt.compressed = xmalloc (sizeof *pkt->pkt.compressed );
zd->algorithm = iobuf_get_noeof(inp); zd->algorithm = iobuf_get_noeof(inp);
zd->len = 0; /* not used */ zd->len = 0; /* not used */
zd->new_ctb = new_ctb; zd->new_ctb = new_ctb;
@ -2114,14 +2113,14 @@ parse_compressed( IOBUF inp, int pkttype, unsigned long pktlen,
static int static int
parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen, parse_encrypted( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *pkt, int new_ctb ) PACKET *pkt, int new_ctb )
{ {
int rc = 0; int rc = 0;
PKT_encrypted *ed; PKT_encrypted *ed;
unsigned long orig_pktlen = pktlen; unsigned long orig_pktlen = pktlen;
ed = pkt->pkt.encrypted = m_alloc(sizeof *pkt->pkt.encrypted ); ed = pkt->pkt.encrypted = xmalloc (sizeof *pkt->pkt.encrypted );
ed->len = pktlen; ed->len = pktlen;
/* we don't know the extralen which is (cipher_blocksize+2) /* we don't know the extralen which is (cipher_blocksize+2)
because the algorithm ist not specified in this packet. because the algorithm ist not specified in this packet.
@ -2143,14 +2142,14 @@ parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen,
log_error("encrypted_mdc packet with unknown version %d\n", log_error("encrypted_mdc packet with unknown version %d\n",
version); version);
/*skip_rest(inp, pktlen); should we really do this? */ /*skip_rest(inp, pktlen); should we really do this? */
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
ed->mdc_method = DIGEST_ALGO_SHA1; ed->mdc_method = DIGEST_ALGO_SHA1;
} }
if( orig_pktlen && pktlen < 10 ) { /* actually this is blocksize+2 */ if( orig_pktlen && pktlen < 10 ) { /* actually this is blocksize+2 */
log_error("packet(%d) too short\n", pkttype); log_error("packet(%d) too short\n", pkttype);
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
skip_rest(inp, pktlen); skip_rest(inp, pktlen);
goto leave; goto leave;
} }
@ -2172,19 +2171,19 @@ parse_encrypted( IOBUF inp, int pkttype, unsigned long pktlen,
static int static int
parse_mdc( IOBUF inp, int pkttype, unsigned long pktlen, parse_mdc( iobuf_t inp, int pkttype, unsigned long pktlen,
PACKET *pkt, int new_ctb ) PACKET *pkt, int new_ctb )
{ {
int rc = 0; int rc = 0;
PKT_mdc *mdc; PKT_mdc *mdc;
byte *p; byte *p;
mdc = pkt->pkt.mdc= m_alloc(sizeof *pkt->pkt.mdc ); mdc = pkt->pkt.mdc= xmalloc (sizeof *pkt->pkt.mdc );
if( list_mode ) if( list_mode )
printf(":mdc packet: length=%lu\n", pktlen); printf(":mdc packet: length=%lu\n", pktlen);
if( !new_ctb || pktlen != 20 ) { if( !new_ctb || pktlen != 20 ) {
log_error("mdc_packet with invalid encoding\n"); log_error("mdc_packet with invalid encoding\n");
rc = G10ERR_INVALID_PACKET; rc = GPG_ERR_INV_PACKET;
goto leave; goto leave;
} }
p = mdc->hash; p = mdc->hash;
@ -2208,7 +2207,7 @@ parse_mdc( IOBUF inp, int pkttype, unsigned long pktlen,
*/ */
static int static int
parse_gpg_control( IOBUF inp, parse_gpg_control( iobuf_t inp,
int pkttype, unsigned long pktlen, PACKET *packet ) int pkttype, unsigned long pktlen, PACKET *packet )
{ {
byte *p; byte *p;
@ -2229,7 +2228,7 @@ parse_gpg_control( IOBUF inp,
if ( list_mode ) if ( list_mode )
puts ("- gpg control packet"); puts ("- gpg control packet");
packet->pkt.gpg_control = m_alloc(sizeof *packet->pkt.gpg_control packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control
+ pktlen - 1); + pktlen - 1);
packet->pkt.gpg_control->control = iobuf_get_noeof(inp); pktlen--; packet->pkt.gpg_control->control = iobuf_get_noeof(inp); pktlen--;
packet->pkt.gpg_control->datalen = pktlen; packet->pkt.gpg_control->datalen = pktlen;
@ -2256,7 +2255,7 @@ parse_gpg_control( IOBUF inp,
putchar('\n'); putchar('\n');
} }
skip_rest(inp,pktlen); skip_rest(inp,pktlen);
return G10ERR_INVALID_PACKET; return GPG_ERR_INV_PACKET;
} }
/* create a gpg control packet to be used internally as a placeholder */ /* create a gpg control packet to be used internally as a placeholder */
@ -2266,10 +2265,10 @@ create_gpg_control( ctrlpkttype_t type, const byte *data, size_t datalen )
PACKET *packet; PACKET *packet;
byte *p; byte *p;
packet = m_alloc( sizeof *packet ); packet = xmalloc ( sizeof *packet );
init_packet(packet); init_packet(packet);
packet->pkttype = PKT_GPG_CONTROL; packet->pkttype = PKT_GPG_CONTROL;
packet->pkt.gpg_control = m_alloc(sizeof *packet->pkt.gpg_control packet->pkt.gpg_control = xmalloc (sizeof *packet->pkt.gpg_control
+ datalen - 1); + datalen - 1);
packet->pkt.gpg_control->control = type; packet->pkt.gpg_control->control = type;
packet->pkt.gpg_control->datalen = datalen; packet->pkt.gpg_control->datalen = datalen;

View File

@ -40,6 +40,7 @@
#include <langinfo.h> #include <langinfo.h>
#endif #endif
#include "gpg.h"
#include "util.h" #include "util.h"
#include "memory.h" #include "memory.h"
#include "options.h" #include "options.h"
@ -122,10 +123,10 @@ have_static_passphrase()
void void
set_next_passphrase( const char *s ) set_next_passphrase( const char *s )
{ {
m_free(next_pw); xfree (next_pw);
next_pw = NULL; next_pw = NULL;
if( s ) { if( s ) {
next_pw = m_alloc_secure( strlen(s)+1 ); next_pw = gcry_xmalloc_secure ( strlen(s)+1 );
strcpy(next_pw, s ); strcpy(next_pw, s );
} }
} }
@ -170,7 +171,7 @@ read_passphrase_from_fd( int fd )
{ {
char *pw2 = pw; char *pw2 = pw;
len += 100; len += 100;
pw = m_alloc_secure( len ); pw = gcry_xmalloc_secure ( len );
if( pw2 ) if( pw2 )
memcpy(pw, pw2, i ); memcpy(pw, pw2, i );
else else
@ -183,7 +184,7 @@ read_passphrase_from_fd( int fd )
if (!opt.batch) if (!opt.batch)
tty_printf("\b\b\b \n" ); tty_printf("\b\b\b \n" );
m_free( fd_passwd ); xfree ( fd_passwd );
fd_passwd = pw; fd_passwd = pw;
} }
@ -337,11 +338,11 @@ agent_send_option (int fd, const char *name, const char *value)
char *line; char *line;
int i; int i;
line = m_alloc (7 + strlen (name) + 1 + strlen (value) + 2); line = xmalloc (7 + strlen (name) + 1 + strlen (value) + 2);
strcpy (stpcpy (stpcpy (stpcpy ( strcpy (stpcpy (stpcpy (stpcpy (
stpcpy (line, "OPTION "), name), "="), value), "\n"); stpcpy (line, "OPTION "), name), "="), value), "\n");
i = writen (fd, line, strlen (line)); i = writen (fd, line, strlen (line));
m_free (line); xfree (line);
if (i) if (i)
return -1; return -1;
@ -394,7 +395,7 @@ agent_send_all_options (int fd)
#if defined(HAVE_SETLOCALE) && defined(LC_CTYPE) #if defined(HAVE_SETLOCALE) && defined(LC_CTYPE)
old_lc = setlocale (LC_CTYPE, NULL); old_lc = setlocale (LC_CTYPE, NULL);
if (old_lc) if (old_lc)
old_lc = m_strdup (old_lc); old_lc = xstrdup (old_lc);
dft_lc = setlocale (LC_CTYPE, ""); dft_lc = setlocale (LC_CTYPE, "");
#endif #endif
if (opt.lc_ctype || (dft_ttyname && dft_lc)) if (opt.lc_ctype || (dft_ttyname && dft_lc))
@ -406,7 +407,7 @@ agent_send_all_options (int fd)
if (old_lc) if (old_lc)
{ {
setlocale (LC_CTYPE, old_lc); setlocale (LC_CTYPE, old_lc);
m_free (old_lc); xfree (old_lc);
} }
#endif #endif
if (rc) if (rc)
@ -415,7 +416,7 @@ agent_send_all_options (int fd)
#if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES) #if defined(HAVE_SETLOCALE) && defined(LC_MESSAGES)
old_lc = setlocale (LC_MESSAGES, NULL); old_lc = setlocale (LC_MESSAGES, NULL);
if (old_lc) if (old_lc)
old_lc = m_strdup (old_lc); old_lc = xstrdup (old_lc);
dft_lc = setlocale (LC_MESSAGES, ""); dft_lc = setlocale (LC_MESSAGES, "");
#endif #endif
if (opt.lc_messages || (dft_ttyname && dft_lc)) if (opt.lc_messages || (dft_ttyname && dft_lc))
@ -427,7 +428,7 @@ agent_send_all_options (int fd)
if (old_lc) if (old_lc)
{ {
setlocale (LC_MESSAGES, old_lc); setlocale (LC_MESSAGES, old_lc);
m_free (old_lc); xfree (old_lc);
} }
#endif #endif
return rc; return rc;
@ -495,7 +496,7 @@ agent_open (int *ret_prot)
int prot; int prot;
if (opt.gpg_agent_info) if (opt.gpg_agent_info)
infostr = m_strdup (opt.gpg_agent_info); infostr = xstrdup (opt.gpg_agent_info);
else else
{ {
infostr = getenv ( "GPG_AGENT_INFO" ); infostr = getenv ( "GPG_AGENT_INFO" );
@ -504,13 +505,13 @@ agent_open (int *ret_prot)
opt.use_agent = 0; opt.use_agent = 0;
return -1; return -1;
} }
infostr = m_strdup ( infostr ); infostr = xstrdup ( infostr );
} }
if ( !(p = strchr ( infostr, ':')) || p == infostr if ( !(p = strchr ( infostr, ':')) || p == infostr
|| (p-infostr)+1 >= sizeof client_addr.sun_path ) { || (p-infostr)+1 >= sizeof client_addr.sun_path ) {
log_error( _("malformed GPG_AGENT_INFO environment variable\n")); log_error( _("malformed GPG_AGENT_INFO environment variable\n"));
m_free (infostr ); xfree (infostr );
opt.use_agent = 0; opt.use_agent = 0;
return -1; return -1;
} }
@ -523,7 +524,7 @@ agent_open (int *ret_prot)
prot = *p? atoi (p+1) : 0; prot = *p? atoi (p+1) : 0;
if ( prot < 0 || prot > 1) { if ( prot < 0 || prot > 1) {
log_error (_("gpg-agent protocol version %d is not supported\n"),prot); log_error (_("gpg-agent protocol version %d is not supported\n"),prot);
m_free (infostr ); xfree (infostr );
opt.use_agent = 0; opt.use_agent = 0;
return -1; return -1;
} }
@ -531,7 +532,7 @@ agent_open (int *ret_prot)
if( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1 ) { if( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1 ) {
log_error ("can't create socket: %s\n", strerror(errno) ); log_error ("can't create socket: %s\n", strerror(errno) );
m_free (infostr ); xfree (infostr );
opt.use_agent = 0; opt.use_agent = 0;
return -1; return -1;
} }
@ -545,12 +546,12 @@ agent_open (int *ret_prot)
if( connect( fd, (struct sockaddr*)&client_addr, len ) == -1 ) { if( connect( fd, (struct sockaddr*)&client_addr, len ) == -1 ) {
log_error ( _("can't connect to `%s': %s\n"), log_error ( _("can't connect to `%s': %s\n"),
infostr, strerror (errno) ); infostr, strerror (errno) );
m_free (infostr ); xfree (infostr );
close (fd ); close (fd );
opt.use_agent = 0; opt.use_agent = 0;
return -1; return -1;
} }
m_free (infostr); xfree (infostr);
if (!prot) { if (!prot) {
if ( writen ( fd, "GPGA\0\0\0\x01", 8 ) ) { if ( writen ( fd, "GPGA\0\0\0\x01", 8 ) ) {
@ -623,7 +624,7 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
int nread; int nread;
u32 reply; u32 reply;
char *pw = NULL; char *pw = NULL;
PKT_public_key *pk = m_alloc_clear( sizeof *pk ); PKT_public_key *pk = xcalloc (1, sizeof *pk );
byte fpr[MAX_FINGERPRINT_LEN]; byte fpr[MAX_FINGERPRINT_LEN];
int have_fpr = 0; int have_fpr = 0;
int prot; int prot;
@ -652,7 +653,7 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
#endif #endif
if (orig_codeset) if (orig_codeset)
{ /* We only switch when we are able to restore the codeset later. */ { /* We only switch when we are able to restore the codeset later. */
orig_codeset = m_strdup (orig_codeset); orig_codeset = xstrdup (orig_codeset);
if (!bind_textdomain_codeset (PACKAGE, "utf-8")) if (!bind_textdomain_codeset (PACKAGE, "utf-8"))
orig_codeset = NULL; orig_codeset = NULL;
} }
@ -665,7 +666,7 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
{ {
char *uid; char *uid;
size_t uidlen; size_t uidlen;
const char *algo_name = pubkey_algo_to_string ( pk->pubkey_algo ); const char *algo_name = gcry_pk_algo_name ( pk->pubkey_algo );
const char *timestr; const char *timestr;
char *maink; char *maink;
const char *fmtstr; const char *fmtstr;
@ -674,7 +675,7 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
algo_name = "?"; algo_name = "?";
fmtstr = _(" (main key ID %08lX)"); fmtstr = _(" (main key ID %08lX)");
maink = m_alloc ( strlen (fmtstr) + 20 ); maink = xmalloc ( strlen (fmtstr) + 20 );
if( keyid[2] && keyid[3] && keyid[0] != keyid[2] if( keyid[2] && keyid[3] && keyid[0] != keyid[2]
&& keyid[1] != keyid[3] ) && keyid[1] != keyid[3] )
sprintf( maink, fmtstr, (ulong)keyid[3] ); sprintf( maink, fmtstr, (ulong)keyid[3] );
@ -687,15 +688,15 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
" secret key for user:\n" " secret key for user:\n"
"\"%.*s\"\n" "\"%.*s\"\n"
"%u-bit %s key, ID %08lX, created %s%s\n" ); "%u-bit %s key, ID %08lX, created %s%s\n" );
atext = m_alloc ( 100 + strlen (fmtstr) atext = xmalloc ( 100 + strlen (fmtstr)
+ uidlen + 15 + strlen(algo_name) + 8 + uidlen + 15 + strlen(algo_name) + 8
+ strlen (timestr) + strlen (maink) ); + strlen (timestr) + strlen (maink) );
sprintf (atext, fmtstr, sprintf (atext, fmtstr,
uidlen, uid, uidlen, uid,
nbits_from_pk (pk), algo_name, (ulong)keyid[1], timestr, nbits_from_pk (pk), algo_name, (ulong)keyid[1], timestr,
maink ); maink );
m_free (uid); xfree (uid);
m_free (maink); xfree (maink);
{ {
size_t dummy; size_t dummy;
@ -705,9 +706,9 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
} }
else if (mode == 2 ) else if (mode == 2 )
atext = m_strdup ( _("Repeat passphrase\n") ); atext = xstrdup ( _("Repeat passphrase\n") );
else else
atext = m_strdup ( _("Enter passphrase\n") ); atext = xstrdup ( _("Enter passphrase\n") );
if (!prot) if (!prot)
{ /* old style protocol */ { /* old style protocol */
@ -717,7 +718,7 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
memcpy (buf+8, fpr, 20 ); memcpy (buf+8, fpr, 20 );
if ( writen ( fd, buf, 28 ) || writen ( fd, atext, strlen (atext) ) ) if ( writen ( fd, buf, 28 ) || writen ( fd, atext, strlen (atext) ) )
goto failure; goto failure;
m_free (atext); atext = NULL; xfree (atext); atext = NULL;
/* get response */ /* get response */
if ( readn ( fd, buf, 12, &nread ) ) if ( readn ( fd, buf, 12, &nread ) )
@ -753,7 +754,7 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
* on how long the passhrase actually is - this wastes some bytes * on how long the passhrase actually is - this wastes some bytes
* but because we already have this padding we should not loosen * but because we already have this padding we should not loosen
* this by issuing 2 read calls */ * this by issuing 2 read calls */
pw = m_alloc_secure ( n+1 ); pw = xmalloc_secure ( n+1 );
if ( readn ( fd, pw, n, &nn ) ) if ( readn ( fd, pw, n, &nn ) )
goto failure; goto failure;
if ( n != nn ) if ( n != nn )
@ -768,7 +769,7 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
if (orig_codeset) if (orig_codeset)
bind_textdomain_codeset (PACKAGE, orig_codeset); bind_textdomain_codeset (PACKAGE, orig_codeset);
#endif #endif
m_free (orig_codeset); xfree (orig_codeset);
return pw; return pw;
} }
else if ( reply == GPGA_PROT_CANCELED ) else if ( reply == GPGA_PROT_CANCELED )
@ -794,7 +795,7 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
/* We allocate 2 time the needed space for atext so that there /* We allocate 2 time the needed space for atext so that there
is nenough space for escaping */ is nenough space for escaping */
line = m_alloc (15 + 46 line = xmalloc (15 + 46
+ 3*strlen (tryagain_text) + 3*strlen (atext) + 2); + 3*strlen (tryagain_text) + 3*strlen (atext) + 2);
strcpy (line, "GET_PASSPHRASE "); strcpy (line, "GET_PASSPHRASE ");
p = line+15; p = line+15;
@ -836,13 +837,13 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
} }
*p++ = '\n'; *p++ = '\n';
i = writen (fd, line, p - line); i = writen (fd, line, p - line);
m_free (line); xfree (line);
if (i) if (i)
goto failure; goto failure;
m_free (atext); atext = NULL; xfree (atext); atext = NULL;
/* get response */ /* get response */
pw = m_alloc_secure (500); pw = xmalloc_secure (500);
nread = readline (fd, pw, 499); nread = readline (fd, pw, 499);
if (nread < 3) if (nread < 3)
goto failure; goto failure;
@ -860,7 +861,7 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
if (orig_codeset) if (orig_codeset)
bind_textdomain_codeset (PACKAGE, orig_codeset); bind_textdomain_codeset (PACKAGE, orig_codeset);
#endif #endif
m_free (orig_codeset); xfree (orig_codeset);
return pw; return pw;
} }
else if (nread > 7 && !memcmp (pw, "ERR 111", 7) else if (nread > 7 && !memcmp (pw, "ERR 111", 7)
@ -883,10 +884,10 @@ agent_get_passphrase ( u32 *keyid, int mode, const char *tryagain_text,
if (orig_codeset) if (orig_codeset)
bind_textdomain_codeset (PACKAGE, orig_codeset); bind_textdomain_codeset (PACKAGE, orig_codeset);
#endif #endif
m_free (atext); xfree (atext);
if ( fd != -1 ) if ( fd != -1 )
agent_close (fd); agent_close (fd);
m_free (pw ); xfree (pw );
free_public_key( pk ); free_public_key( pk );
return NULL; return NULL;
@ -918,7 +919,7 @@ passphrase_clear_cache ( u32 *keyid, int algo )
if (!opt.use_agent) if (!opt.use_agent)
return; return;
pk = m_alloc_clear ( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
memset (fpr, 0, MAX_FINGERPRINT_LEN ); memset (fpr, 0, MAX_FINGERPRINT_LEN );
if( !keyid || get_pubkey( pk, keyid ) ) if( !keyid || get_pubkey( pk, keyid ) )
{ {
@ -964,14 +965,14 @@ passphrase_clear_cache ( u32 *keyid, int algo )
char *line, *p; char *line, *p;
int i; int i;
line = m_alloc (17 + 40 + 2); line = xmalloc (17 + 40 + 2);
strcpy (line, "CLEAR_PASSPHRASE "); strcpy (line, "CLEAR_PASSPHRASE ");
p = line+17; p = line+17;
for (i=0; i < 20; i++, p +=2 ) for (i=0; i < 20; i++, p +=2 )
sprintf (p, "%02X", fpr[i]); sprintf (p, "%02X", fpr[i]);
*p++ = '\n'; *p++ = '\n';
i = writen (fd, line, p - line); i = writen (fd, line, p - line);
m_free (line); xfree (line);
if (i) if (i)
goto failure; goto failure;
@ -1054,7 +1055,7 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo,
us = get_long_user_id_string( keyid ); us = get_long_user_id_string( keyid );
write_status_text( STATUS_USERID_HINT, us ); write_status_text( STATUS_USERID_HINT, us );
m_free(us); xfree (us);
sprintf( buf, "%08lX%08lX %08lX%08lX %d 0", sprintf( buf, "%08lX%08lX %08lX%08lX %d 0",
(ulong)keyid[0], (ulong)keyid[1], (ulong)keyid[0], (ulong)keyid[1],
@ -1070,7 +1071,7 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo,
} }
if( keyid && !opt.batch && !next_pw && mode!=1 ) { if( keyid && !opt.batch && !next_pw && mode!=1 ) {
PKT_public_key *pk = m_alloc_clear( sizeof *pk ); PKT_public_key *pk = xcalloc (1, sizeof *pk );
size_t n; size_t n;
char *p; char *p;
@ -1078,11 +1079,11 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo,
"user: \"") ); "user: \"") );
p = get_user_id( keyid, &n ); p = get_user_id( keyid, &n );
tty_print_utf8_string( p, n ); tty_print_utf8_string( p, n );
m_free(p); xfree (p);
tty_printf("\"\n"); tty_printf("\"\n");
if( !get_pubkey( pk, keyid ) ) { if( !get_pubkey( pk, keyid ) ) {
const char *s = pubkey_algo_to_string( pk->pubkey_algo ); const char *s = gcry_pk_algo_name ( pk->pubkey_algo );
tty_printf( _("%u-bit %s key, ID %08lX, created %s"), tty_printf( _("%u-bit %s key, ID %08lX, created %s"),
nbits_from_pk( pk ), s?s:"?", (ulong)keyid[1], nbits_from_pk( pk ), s?s:"?", (ulong)keyid[1],
strtimestamp(pk->timestamp) ); strtimestamp(pk->timestamp) );
@ -1108,7 +1109,7 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo,
{ {
if (!opt.use_agent) if (!opt.use_agent)
goto agent_died; goto agent_died;
pw = m_strdup (""); pw = xstrdup ("");
} }
if( *pw && mode == 2 ) { if( *pw && mode == 2 ) {
char *pw2 = agent_get_passphrase ( keyid, 2, NULL, canceled ); char *pw2 = agent_get_passphrase ( keyid, 2, NULL, canceled );
@ -1116,27 +1117,27 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo,
{ {
if (!opt.use_agent) if (!opt.use_agent)
{ {
m_free (pw); xfree (pw);
pw = NULL; pw = NULL;
goto agent_died; goto agent_died;
} }
pw2 = m_strdup (""); pw2 = xstrdup ("");
} }
if( strcmp(pw, pw2) ) { if( strcmp(pw, pw2) ) {
m_free(pw2); xfree (pw2);
m_free(pw); xfree (pw);
return NULL; return NULL;
} }
m_free(pw2); xfree (pw2);
} }
} }
else if( fd_passwd ) { else if( fd_passwd ) {
pw = m_alloc_secure( strlen(fd_passwd)+1 ); pw = xmalloc_secure ( strlen(fd_passwd)+1 );
strcpy( pw, fd_passwd ); strcpy( pw, fd_passwd );
} }
else if( opt.batch ) { else if( opt.batch ) {
log_error(_("can't query password in batchmode\n")); log_error(_("can't query password in batchmode\n"));
pw = m_strdup( "" ); /* return an empty passphrase */ pw = xstrdup ( "" ); /* return an empty passphrase */
} }
else { else {
pw = cpr_get_hidden("passphrase.enter", _("Enter passphrase: ") ); pw = cpr_get_hidden("passphrase.enter", _("Enter passphrase: ") );
@ -1146,24 +1147,24 @@ passphrase_to_dek( u32 *keyid, int pubkey_algo,
_("Repeat passphrase: ") ); _("Repeat passphrase: ") );
tty_kill_prompt(); tty_kill_prompt();
if( strcmp(pw, pw2) ) { if( strcmp(pw, pw2) ) {
m_free(pw2); xfree (pw2);
m_free(pw); xfree (pw);
return NULL; return NULL;
} }
m_free(pw2); xfree (pw2);
} }
} }
if( !pw || !*pw ) if( !pw || !*pw )
write_status( STATUS_MISSING_PASSPHRASE ); write_status( STATUS_MISSING_PASSPHRASE );
dek = m_alloc_secure_clear ( sizeof *dek ); dek = xcalloc_secure (1, sizeof *dek );
dek->algo = cipher_algo; dek->algo = cipher_algo;
if( !*pw && mode == 2 ) if( !*pw && mode == 2 )
dek->keylen = 0; dek->keylen = 0;
else else
hash_passphrase( dek, pw, s2k, mode==2 ); hash_passphrase( dek, pw, s2k, mode==2 );
m_free(last_pw); xfree (last_pw);
last_pw = pw; last_pw = pw;
return dek; return dek;
} }
@ -1183,16 +1184,16 @@ hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create )
int pwlen = strlen(pw); int pwlen = strlen(pw);
assert( s2k->hash_algo ); assert( s2k->hash_algo );
dek->keylen = cipher_get_keylen( dek->algo ) / 8; dek->keylen = gcry_cipher_get_algo_keylen (dek->algo);
if( !(dek->keylen > 0 && dek->keylen <= DIM(dek->key)) ) if( !(dek->keylen > 0 && dek->keylen <= DIM(dek->key)) )
BUG(); BUG();
md = md_open( s2k->hash_algo, 1); gcry_md_open (&md, s2k->hash_algo, 1);
for(pass=0; used < dek->keylen ; pass++ ) { for(pass=0; used < dek->keylen ; pass++ ) {
if( pass ) { if( pass ) {
md_reset(md); gcry_md_reset(md);
for(i=0; i < pass; i++ ) /* preset the hash context */ for(i=0; i < pass; i++ ) /* preset the hash context */
md_putc(md, 0 ); gcry_md_putc (md, 0 );
} }
if( s2k->mode == 1 || s2k->mode == 3 ) { if( s2k->mode == 1 || s2k->mode == 3 ) {
@ -1200,7 +1201,7 @@ hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create )
ulong count = len2; ulong count = len2;
if( create && !pass ) { if( create && !pass ) {
randomize_buffer(s2k->salt, 8, 1); gcry_randomize(s2k->salt, 8, GCRY_STRONG_RANDOM );
if( s2k->mode == 3 ) if( s2k->mode == 3 )
s2k->count = 96; /* 65536 iterations */ s2k->count = 96; /* 65536 iterations */
} }
@ -1212,27 +1213,27 @@ hash_passphrase( DEK *dek, char *pw, STRING2KEY *s2k, int create )
} }
/* a little bit complicated because we need a ulong for count */ /* a little bit complicated because we need a ulong for count */
while( count > len2 ) { /* maybe iterated+salted */ while( count > len2 ) { /* maybe iterated+salted */
md_write( md, s2k->salt, 8 ); gcry_md_write( md, s2k->salt, 8 );
md_write( md, pw, pwlen ); gcry_md_write( md, pw, pwlen );
count -= len2; count -= len2;
} }
if( count < 8 ) if( count < 8 )
md_write( md, s2k->salt, count ); gcry_md_write( md, s2k->salt, count );
else { else {
md_write( md, s2k->salt, 8 ); gcry_md_write( md, s2k->salt, 8 );
count -= 8; count -= 8;
md_write( md, pw, count ); gcry_md_write( md, pw, count );
} }
} }
else else
md_write( md, pw, pwlen ); gcry_md_write( md, pw, pwlen );
md_final( md ); gcry_md_final ( md );
i = md_digest_length( s2k->hash_algo ); i = gcry_md_get_algo_dlen (s2k->hash_algo);
if( i > dek->keylen - used ) if( i > dek->keylen - used )
i = dek->keylen - used; i = dek->keylen - used;
memcpy( dek->key+used, md_read(md, s2k->hash_algo), i ); memcpy( dek->key+used, gcry_md_read (md, s2k->hash_algo), i );
used += i; used += i;
} }
md_close(md); gcry_md_close (md);
} }

View File

@ -49,7 +49,7 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
char *filename=NULL; char *filename=NULL;
byte *photo=NULL; byte *photo=NULL;
byte header[16]; byte header[16];
IOBUF file; iobuf_t file;
header[0]=0x10; /* little side of photo header length */ header[0]=0x10; /* little side of photo header length */
header[1]=0; /* big side of photo header length */ header[1]=0; /* big side of photo header length */
@ -60,7 +60,7 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
header[i]=0; header[i]=0;
#define EXTRA_UID_NAME_SPACE 71 #define EXTRA_UID_NAME_SPACE 71
uid=m_alloc_clear(sizeof(*uid)+71); uid=xcalloc (1,sizeof(*uid)+71);
printf(_("\nPick an image to use for your photo ID. " printf(_("\nPick an image to use for your photo ID. "
"The image must be a JPEG file.\n" "The image must be a JPEG file.\n"
@ -73,7 +73,7 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
{ {
printf("\n"); printf("\n");
m_free(filename); xfree (filename);
filename=cpr_get("photoid.jpeg.add", filename=cpr_get("photoid.jpeg.add",
_("Enter JPEG filename for photo ID: ")); _("Enter JPEG filename for photo ID: "));
@ -101,7 +101,7 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
} }
} }
photo=m_alloc(len); photo=xmalloc (len);
iobuf_read(file,photo,len); iobuf_read(file,photo,len);
iobuf_close(file); iobuf_close(file);
@ -110,7 +110,7 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
photo[6]!='J' || photo[7]!='F' || photo[8]!='I' || photo[9]!='F') photo[6]!='J' || photo[7]!='F' || photo[8]!='I' || photo[9]!='F')
{ {
log_error(_("\"%s\" is not a JPEG file\n"),filename); log_error(_("\"%s\" is not a JPEG file\n"),filename);
m_free(photo); xfree (photo);
photo=NULL; photo=NULL;
continue; continue;
} }
@ -132,7 +132,7 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
goto scram; goto scram;
case 0: case 0:
free_attributes(uid); free_attributes(uid);
m_free(photo); xfree (photo);
photo=NULL; photo=NULL;
continue; continue;
} }
@ -143,13 +143,13 @@ PKT_user_id *generate_photo_id(PKT_public_key *pk)
uid->ref=1; uid->ref=1;
scram: scram:
m_free(filename); xfree (filename);
m_free(photo); xfree (photo);
if(error) if(error)
{ {
free_attributes(uid); free_attributes(uid);
m_free(uid); xfree (uid);
return NULL; return NULL;
} }
@ -283,7 +283,7 @@ void show_photos(const struct user_attribute *attrs,
if(!command) if(!command)
goto fail; goto fail;
name=m_alloc(16+strlen(EXTSEP_S)+ name=xmalloc (16+strlen(EXTSEP_S)+
strlen(image_type_to_string(args.imagetype,0))+1); strlen(image_type_to_string(args.imagetype,0))+1);
/* Make the filename. Notice we are not using the image /* Make the filename. Notice we are not using the image
@ -302,7 +302,7 @@ void show_photos(const struct user_attribute *attrs,
if(exec_write(&spawn,NULL,command,name,1,1)!=0) if(exec_write(&spawn,NULL,command,name,1,1)!=0)
{ {
m_free(name); xfree (name);
goto fail; goto fail;
} }
@ -311,7 +311,7 @@ void show_photos(const struct user_attribute *attrs,
image_type_to_string(args.imagetype,2)); image_type_to_string(args.imagetype,2));
#endif #endif
m_free(name); xfree (name);
fwrite(&attrs[i].data[offset],attrs[i].len-offset,1,spawn->tochild); fwrite(&attrs[i].data[offset],attrs[i].len-offset,1,spawn->tochild);

View File

@ -85,7 +85,7 @@ make_control ( byte *buf, int code, int operation )
static int static int
pipemode_filter( void *opaque, int control, pipemode_filter( void *opaque, int control,
IOBUF a, byte *buf, size_t *ret_len) iobuf_t a, byte *buf, size_t *ret_len)
{ {
size_t size = *ret_len; size_t size = *ret_len;
struct pipemode_context_s *stx = opaque; struct pipemode_context_s *stx = opaque;
@ -291,7 +291,7 @@ pipemode_filter( void *opaque, int control,
void void
run_in_pipemode(void) run_in_pipemode(void)
{ {
IOBUF fp; iobuf_t fp;
armor_filter_context_t afx; armor_filter_context_t afx;
struct pipemode_context_s stx; struct pipemode_context_s stx;
int rc; int rc;

View File

@ -74,10 +74,10 @@ do_show_revocation_reason( PKT_signature *sig )
log_info( _("reason for revocation: ") ); log_info( _("reason for revocation: ") );
if( text ) if( text )
fputs( text, log_stream() ); fputs( text, log_get_stream () );
else else
fprintf( log_stream(), "code=%02x", *p ); fprintf( log_get_stream (), "code=%02x", *p );
putc( '\n', log_stream() ); putc( '\n', log_get_stream () );
n--; p++; n--; p++;
pp = NULL; pp = NULL;
do { do {
@ -90,8 +90,8 @@ do_show_revocation_reason( PKT_signature *sig )
pp = memchr( p, '\n', n ); pp = memchr( p, '\n', n );
nn = pp? pp - p : n; nn = pp? pp - p : n;
log_info( _("revocation comment: ") ); log_info( _("revocation comment: ") );
print_string( log_stream(), p, nn, 0 ); print_string( log_get_stream(), p, nn, 0 );
putc( '\n', log_stream() ); putc( '\n', log_get_stream() );
p += nn; n -= nn; p += nn; n -= nn;
} }
} while( pp ); } while( pp );
@ -186,11 +186,11 @@ show_paths (const PKT_public_key *pk, int only_first )
return; return;
} }
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
rc = get_pubkey( pk, keyid ); rc = get_pubkey( pk, keyid );
if( rc ) { if( rc ) {
log_error("key %08lX: public key not found: %s\n", log_error("key %08lX: public key not found: %s\n",
(ulong)keyid[1], g10_errstr(rc) ); (ulong)keyid[1], gpg_strerror (rc) );
return; return;
} }
@ -214,7 +214,7 @@ show_paths (const PKT_public_key *pk, int only_first )
p = get_user_id( keyid, &n ); p = get_user_id( keyid, &n );
tty_print_utf8_string( p, n ), tty_print_utf8_string( p, n ),
m_free(p); xfree (p);
tty_printf("\"\n"); tty_printf("\"\n");
free_public_key( pk ); free_public_key( pk );
} }
@ -276,7 +276,7 @@ do_edit_ownertrust (PKT_public_key *pk, int mode,
(ulong)keyid[1], datestr_from_pk( pk ) ); (ulong)keyid[1], datestr_from_pk( pk ) );
p = get_user_id( keyid, &n ); p = get_user_id( keyid, &n );
tty_print_utf8_string( p, n ), tty_print_utf8_string( p, n ),
m_free(p); xfree (p);
tty_printf("\"\n"); tty_printf("\"\n");
keyblock = get_pubkeyblock (keyid); keyblock = get_pubkeyblock (keyid);
@ -395,9 +395,9 @@ do_edit_ownertrust (PKT_public_key *pk, int mode,
quit = 1; quit = 1;
break ; /* back to the menu */ break ; /* back to the menu */
} }
m_free(p); p = NULL; xfree (p); p = NULL;
} }
m_free(p); xfree (p);
return show? -2: quit? -1 : changed; return show? -2: quit? -1 : changed;
} }
@ -558,7 +558,7 @@ do_we_trust_pre( PKT_public_key *pk, unsigned int trustlevel )
size_t n; size_t n;
char *p = get_user_id( keyid, &n ); char *p = get_user_id( keyid, &n );
tty_print_utf8_string( p, n ); tty_print_utf8_string( p, n );
m_free(p); xfree (p);
} }
tty_printf("\"\n"); tty_printf("\"\n");
print_fingerprint (pk, NULL, 2); print_fingerprint (pk, NULL, 2);
@ -594,7 +594,7 @@ do_we_trust_pre( PKT_public_key *pk, unsigned int trustlevel )
int int
check_signatures_trust( PKT_signature *sig ) check_signatures_trust( PKT_signature *sig )
{ {
PKT_public_key *pk = m_alloc_clear( sizeof *pk ); PKT_public_key *pk = xcalloc (1, sizeof *pk );
unsigned int trustlevel; unsigned int trustlevel;
int rc=0; int rc=0;
@ -602,7 +602,7 @@ check_signatures_trust( PKT_signature *sig )
if (rc) if (rc)
{ /* this should not happen */ { /* this should not happen */
log_error("Ooops; the key vanished - can't check the trust\n"); log_error("Ooops; the key vanished - can't check the trust\n");
rc = G10ERR_NO_PUBKEY; rc = GPG_ERR_NO_PUBKEY;
goto leave; goto leave;
} }
@ -662,7 +662,7 @@ check_signatures_trust( PKT_signature *sig )
log_info(_(" The signature is probably a FORGERY.\n")); log_info(_(" The signature is probably a FORGERY.\n"));
if (opt.with_fingerprint) if (opt.with_fingerprint)
print_fingerprint (pk, NULL, 1); print_fingerprint (pk, NULL, 1);
rc = G10ERR_BAD_SIGN; rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
break; break;
case TRUST_MARGINAL: case TRUST_MARGINAL:
@ -701,7 +701,7 @@ release_pk_list( PK_LIST pk_list )
for( ; pk_list; pk_list = pk_rover ) { for( ; pk_list; pk_list = pk_rover ) {
pk_rover = pk_list->next; pk_rover = pk_list->next;
free_public_key( pk_list->pk ); free_public_key( pk_list->pk );
m_free( pk_list ); xfree ( pk_list );
} }
} }
@ -730,10 +730,10 @@ default_recipient(void)
int i; int i;
if( opt.def_recipient ) if( opt.def_recipient )
return m_strdup( opt.def_recipient ); return xstrdup ( opt.def_recipient );
if( !opt.def_recipient_self ) if( !opt.def_recipient_self )
return NULL; return NULL;
sk = m_alloc_clear( sizeof *sk ); sk = xcalloc (1, sizeof *sk );
i = get_seckey_byname( sk, NULL, 0 ); i = get_seckey_byname( sk, NULL, 0 );
if( i ) { if( i ) {
free_secret_key( sk ); free_secret_key( sk );
@ -742,7 +742,7 @@ default_recipient(void)
n = MAX_FINGERPRINT_LEN; n = MAX_FINGERPRINT_LEN;
fingerprint_from_sk( sk, fpr, &n ); fingerprint_from_sk( sk, fpr, &n );
free_secret_key( sk ); free_secret_key( sk );
p = m_alloc( 2*n+3 ); p = xmalloc ( 2*n+3 );
*p++ = '0'; *p++ = '0';
*p++ = 'x'; *p++ = 'x';
for(i=0; i < n; i++ ) for(i=0; i < n; i++ )
@ -829,17 +829,17 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
} }
} }
else if( (use & PUBKEY_USAGE_ENC) && !opt.no_encrypt_to ) { else if( (use & PUBKEY_USAGE_ENC) && !opt.no_encrypt_to ) {
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
pk->req_usage = use; pk->req_usage = use;
/* We can encrypt-to a disabled key */ /* We can encrypt-to a disabled key */
if( (rc = get_pubkey_byname( pk, rov->d, NULL, NULL, 1 )) ) { if( (rc = get_pubkey_byname( pk, rov->d, NULL, NULL, 1 )) ) {
free_public_key( pk ); pk = NULL; free_public_key( pk ); pk = NULL;
log_error(_("%s: skipped: %s\n"), rov->d, g10_errstr(rc) ); log_error(_("%s: skipped: %s\n"), rov->d, gpg_strerror (rc) );
write_status_text_and_buffer (STATUS_INV_RECP, "0 ", write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
rov->d, strlen (rov->d), -1); rov->d, strlen (rov->d), -1);
goto fail; goto fail;
} }
else if( !(rc=check_pubkey_algo2(pk->pubkey_algo, use )) ) { else if( !(rc=openpgp_pk_test_algo (pk->pubkey_algo, use )) ) {
/* Skip the actual key if the key is already present /* Skip the actual key if the key is already present
* in the list */ * in the list */
if (key_present_in_pk_list(pk_list, pk) == 0) { if (key_present_in_pk_list(pk_list, pk) == 0) {
@ -849,7 +849,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
} }
else { else {
PK_LIST r; PK_LIST r;
r = m_alloc( sizeof *r ); r = xmalloc ( sizeof *r );
r->pk = pk; pk = NULL; r->pk = pk; pk = NULL;
r->next = pk_list; r->next = pk_list;
r->flags = (rov->flags&2)?1:0; r->flags = (rov->flags&2)?1:0;
@ -867,7 +867,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
} }
else { else {
free_public_key( pk ); pk = NULL; free_public_key( pk ); pk = NULL;
log_error(_("%s: skipped: %s\n"), rov->d, g10_errstr(rc) ); log_error(_("%s: skipped: %s\n"), rov->d, gpg_strerror (rc) );
write_status_text_and_buffer (STATUS_INV_RECP, "0 ", write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
rov->d, strlen (rov->d), -1); rov->d, strlen (rov->d), -1);
goto fail; goto fail;
@ -887,13 +887,13 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
"You did not specify a user ID. (you may use \"-r\")\n")); "You did not specify a user ID. (you may use \"-r\")\n"));
for(;;) { for(;;) {
rc = 0; rc = 0;
m_free(answer); xfree (answer);
if( have_def_rec ) { if( have_def_rec ) {
answer = def_rec; answer = def_rec;
def_rec = NULL; def_rec = NULL;
} }
else if(backlog) { else if (backlog) {
answer=pop_strlist(&backlog); answer = strlist_pop (&backlog);
} }
else { else {
answer = cpr_get_utf8("pklist.user_id.enter", answer = cpr_get_utf8("pklist.user_id.enter",
@ -902,19 +902,19 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
cpr_kill_prompt(); cpr_kill_prompt();
} }
if( !answer || !*answer ) { if( !answer || !*answer ) {
m_free(answer); xfree (answer);
break; break;
} }
if(expand_id(answer,&backlog,0)) if(expand_id(answer,&backlog,0))
continue; continue;
if( pk ) if( pk )
free_public_key( pk ); free_public_key( pk );
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
pk->req_usage = use; pk->req_usage = use;
rc = get_pubkey_byname( pk, answer, NULL, NULL, 0 ); rc = get_pubkey_byname( pk, answer, NULL, NULL, 0 );
if( rc ) if( rc )
tty_printf(_("No such user ID.\n")); tty_printf(_("No such user ID.\n"));
else if( !(rc=check_pubkey_algo2(pk->pubkey_algo, use)) ) { else if( !(rc=openpgp_pk_test_algo (pk->pubkey_algo, use)) ) {
if( have_def_rec ) { if( have_def_rec ) {
if (key_present_in_pk_list(pk_list, pk) == 0) { if (key_present_in_pk_list(pk_list, pk) == 0) {
free_public_key(pk); pk = NULL; free_public_key(pk); pk = NULL;
@ -922,7 +922,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
"already set as default recipient\n") ); "already set as default recipient\n") );
} }
else { else {
PK_LIST r = m_alloc( sizeof *r ); PK_LIST r = xmalloc ( sizeof *r );
r->pk = pk; pk = NULL; r->pk = pk; pk = NULL;
r->next = pk_list; r->next = pk_list;
r->flags = 0; /* no throwing default ids */ r->flags = 0; /* no throwing default ids */
@ -963,11 +963,11 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
size_t n; size_t n;
char *p = get_user_id( keyid, &n ); char *p = get_user_id( keyid, &n );
tty_print_utf8_string( p, n ); tty_print_utf8_string( p, n );
m_free(p); xfree (p);
} }
tty_printf("\"\n"); tty_printf("\"\n");
r = m_alloc( sizeof *r ); r = xmalloc ( sizeof *r );
r->pk = pk; pk = NULL; r->pk = pk; pk = NULL;
r->next = pk_list; r->next = pk_list;
r->flags = 0; /* no throwing interactive ids */ r->flags = 0; /* no throwing interactive ids */
@ -978,7 +978,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
} }
} }
} }
m_free(def_rec); def_rec = NULL; xfree (def_rec); def_rec = NULL;
have_def_rec = 0; have_def_rec = 0;
} }
if( pk ) { if( pk ) {
@ -987,13 +987,13 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
} }
} }
else if( !any_recipients && (def_rec = default_recipient()) ) { else if( !any_recipients && (def_rec = default_recipient()) ) {
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
pk->req_usage = use; pk->req_usage = use;
/* The default recipient may be disabled */ /* The default recipient may be disabled */
rc = get_pubkey_byname( pk, def_rec, NULL, NULL, 1 ); rc = get_pubkey_byname( pk, def_rec, NULL, NULL, 1 );
if( rc ) if( rc )
log_error(_("unknown default recipient `%s'\n"), def_rec ); log_error(_("unknown default recipient `%s'\n"), def_rec );
else if( !(rc=check_pubkey_algo2(pk->pubkey_algo, use)) ) { else if( !(rc=openpgp_pk_test_algo (pk->pubkey_algo, use)) ) {
/* Mark any_recipients here since the default recipient /* Mark any_recipients here since the default recipient
would have been used if it wasn't already there. It would have been used if it wasn't already there. It
doesn't really matter if we got this key from the default doesn't really matter if we got this key from the default
@ -1002,7 +1002,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
if (key_present_in_pk_list(pk_list, pk) == 0) if (key_present_in_pk_list(pk_list, pk) == 0)
log_info(_("skipped: public key already set as default recipient\n")); log_info(_("skipped: public key already set as default recipient\n"));
else { else {
PK_LIST r = m_alloc( sizeof *r ); PK_LIST r = xmalloc ( sizeof *r );
r->pk = pk; pk = NULL; r->pk = pk; pk = NULL;
r->next = pk_list; r->next = pk_list;
r->flags = 0; /* no throwing default ids */ r->flags = 0; /* no throwing default ids */
@ -1013,7 +1013,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
free_public_key( pk ); free_public_key( pk );
pk = NULL; pk = NULL;
} }
m_free(def_rec); def_rec = NULL; xfree (def_rec); def_rec = NULL;
} }
else { else {
any_recipients = 0; any_recipients = 0;
@ -1021,17 +1021,17 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
if( (remusr->flags & 1) ) if( (remusr->flags & 1) )
continue; /* encrypt-to keys are already handled */ continue; /* encrypt-to keys are already handled */
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
pk->req_usage = use; pk->req_usage = use;
if( (rc = get_pubkey_byname( pk, remusr->d, NULL, NULL, 0 )) ) { if( (rc = get_pubkey_byname( pk, remusr->d, NULL, NULL, 0 )) ) {
free_public_key( pk ); pk = NULL; free_public_key( pk ); pk = NULL;
log_error(_("%s: skipped: %s\n"), remusr->d, g10_errstr(rc) ); log_error(_("%s: skipped: %s\n"), remusr->d, gpg_strerror (rc) );
write_status_text_and_buffer (STATUS_INV_RECP, "0 ", write_status_text_and_buffer (STATUS_INV_RECP, "0 ",
remusr->d, strlen (remusr->d), remusr->d, strlen (remusr->d),
-1); -1);
goto fail; goto fail;
} }
else if( !(rc=check_pubkey_algo2(pk->pubkey_algo, use )) ) { else if( !(rc=openpgp_pk_test_algo (pk->pubkey_algo, use )) ) {
int trustlevel; int trustlevel;
trustlevel = get_validity (pk, pk->user_id); trustlevel = get_validity (pk, pk->user_id);
@ -1043,7 +1043,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
remusr->d, remusr->d,
strlen (remusr->d), strlen (remusr->d),
-1); -1);
rc=G10ERR_UNU_PUBKEY; rc = gpg_error (GPG_ERR_UNUSABLE_PUBKEY);
goto fail; goto fail;
} }
else if( do_we_trust_pre( pk, trustlevel ) ) { else if( do_we_trust_pre( pk, trustlevel ) ) {
@ -1062,7 +1062,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
} }
else { else {
PK_LIST r; PK_LIST r;
r = m_alloc( sizeof *r ); r = xmalloc ( sizeof *r );
r->pk = pk; pk = NULL; r->pk = pk; pk = NULL;
r->next = pk_list; r->next = pk_list;
r->flags = (remusr->flags&2)?1:0; r->flags = (remusr->flags&2)?1:0;
@ -1075,7 +1075,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
remusr->d, remusr->d,
strlen (remusr->d), strlen (remusr->d),
-1); -1);
rc=G10ERR_UNU_PUBKEY; rc = gpg_error (GPG_ERR_UNUSABLE_PUBKEY);
goto fail; goto fail;
} }
} }
@ -1085,7 +1085,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
remusr->d, remusr->d,
strlen (remusr->d), strlen (remusr->d),
-1); -1);
log_error(_("%s: skipped: %s\n"), remusr->d, g10_errstr(rc) ); log_error(_("%s: skipped: %s\n"), remusr->d, gpg_strerror (rc) );
goto fail; goto fail;
} }
} }
@ -1094,7 +1094,7 @@ build_pk_list( STRLIST rcpts, PK_LIST *ret_pk_list, unsigned use )
if( !rc && !any_recipients ) { if( !rc && !any_recipients ) {
log_error(_("no valid addressees\n")); log_error(_("no valid addressees\n"));
write_status_text (STATUS_NO_RECP, "0"); write_status_text (STATUS_NO_RECP, "0");
rc = G10ERR_NO_USER_ID; rc = GPG_ERR_NO_USER_ID;
} }
fail: fail:
@ -1145,11 +1145,11 @@ algo_available( preftype_t preftype, int algo, void *hint )
&& algo != CIPHER_ALGO_TWOFISH)) && algo != CIPHER_ALGO_TWOFISH))
return 0; return 0;
return algo && !check_cipher_algo( algo ); return algo && !gcry_cipher_test_algo (algo);
} }
else if( preftype == PREFTYPE_HASH ) else if( preftype == PREFTYPE_HASH )
{ {
if(hint && ((*(int *)hint) != md_digest_length(algo))) if(hint && ((*(int *)hint) != gcry_md_get_algo_dlen (algo)))
return 0; return 0;
if((PGP6 || PGP7) && (algo != DIGEST_ALGO_MD5 if((PGP6 || PGP7) && (algo != DIGEST_ALGO_MD5
@ -1168,7 +1168,7 @@ algo_available( preftype_t preftype, int algo, void *hint )
if( RFC2440 && algo == DIGEST_ALGO_TIGER ) if( RFC2440 && algo == DIGEST_ALGO_TIGER )
return 0; return 0;
return algo && !check_digest_algo( algo ); return algo && !gcry_md_test_algo( algo );
} }
else if( preftype == PREFTYPE_ZIP ) else if( preftype == PREFTYPE_ZIP )
{ {

278
g10/pkglue.c Normal file
View File

@ -0,0 +1,278 @@
/* pkglue.c - public key operations glue code
* Copyright (C) 2000, 2003 Free Software Foundation, Inc.
*
* 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 <assert.h>
#include "gpg.h"
#include "util.h"
#include "pkglue.h"
/****************
* Emulate our old PK interface here - sometime in the future we might
* change the internal design to directly fit to libgcrypt.
*/
int
pk_sign (int algo, gcry_mpi_t * data, gcry_mpi_t hash, gcry_mpi_t * skey)
{
gcry_sexp_t s_sig, s_hash, s_skey, list;
int rc;
/* make a sexp from skey */
if (algo == GCRY_PK_DSA)
{
rc = gcry_sexp_build (&s_skey, NULL,
"(private-key(dsa(p%m)(q%m)(g%m)(y%m)(x%m)))",
skey[0], skey[1], skey[2], skey[3], skey[4]);
}
else if (algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E)
{
rc = gcry_sexp_build (&s_skey, NULL,
"(private-key(elg(p%m)(g%m)(y%m)(x%m)))",
skey[0], skey[1], skey[2], skey[3]);
}
else
return GPG_ERR_PUBKEY_ALGO;
if (rc)
BUG ();
/* put hash into a S-Exp s_hash */
if (gcry_sexp_build (&s_hash, NULL, "%m", hash))
BUG ();
rc = gcry_pk_sign (&s_sig, s_hash, s_skey);
gcry_sexp_release (s_hash);
gcry_sexp_release (s_skey);
if (rc)
;
else
{
list = gcry_sexp_find_token (s_sig, "r", 0);
assert (list);
data[0] = gcry_sexp_nth_mpi (list, 1, 0);
assert (data[0]);
gcry_sexp_release (list);
list = gcry_sexp_find_token (s_sig, "s", 0);
assert (list);
data[1] = gcry_sexp_nth_mpi (list, 1, 0);
assert (data[1]);
gcry_sexp_release (list);
}
gcry_sexp_release (s_sig);
return rc;
}
/****************
* Emulate our old PK interface here - sometime in the future we might
* change the internal design to directly fit to libgcrypt.
*/
int
pk_verify (int algo, gcry_mpi_t hash, gcry_mpi_t * data, gcry_mpi_t * pkey)
{
gcry_sexp_t s_sig, s_hash, s_pkey;
int rc;
/* make a sexp from pkey */
if (algo == GCRY_PK_DSA)
{
rc = gcry_sexp_build (&s_pkey, NULL,
"(public-key(dsa(p%m)(q%m)(g%m)(y%m)))",
pkey[0], pkey[1], pkey[2], pkey[3]);
}
else if (algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E)
{
rc = gcry_sexp_build (&s_pkey, NULL,
"(public-key(elg(p%m)(g%m)(y%m)))",
pkey[0], pkey[1], pkey[2]);
}
else if (algo == GCRY_PK_RSA)
{
rc = gcry_sexp_build (&s_pkey, NULL,
"(public-key(rsa(n%m)(e%m)))", pkey[0], pkey[1]);
}
else
return GPG_ERR_PUBKEY_ALGO;
if (rc)
BUG ();
/* put hash into a S-Exp s_hash */
if (gcry_sexp_build (&s_hash, NULL, "%m", hash))
BUG ();
/* put data into a S-Exp s_sig */
if (algo == GCRY_PK_DSA)
{
rc = gcry_sexp_build (&s_sig, NULL,
"(sig-val(dsa(r%m)(s%m)))", data[0], data[1]);
}
else if (algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E)
{
rc = gcry_sexp_build (&s_sig, NULL,
"(sig-val(elg(r%m)(s%m)))", data[0], data[1]);
}
else if (algo == GCRY_PK_RSA)
{
rc = gcry_sexp_build (&s_sig, NULL, "(sig-val(rsa(s%m)))", data[0]);
}
else
BUG ();
if (rc)
BUG ();
rc = gcry_pk_verify (s_sig, s_hash, s_pkey);
gcry_sexp_release (s_sig);
gcry_sexp_release (s_hash);
gcry_sexp_release (s_pkey);
return rc;
}
/****************
* Emulate our old PK interface here - sometime in the future we might
* change the internal design to directly fit to libgcrypt.
*/
int
pk_encrypt (int algo, gcry_mpi_t * resarr, gcry_mpi_t data, gcry_mpi_t * pkey)
{
gcry_sexp_t s_ciph, s_data, s_pkey;
int rc;
/* make a sexp from pkey */
if (algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E)
{
rc = gcry_sexp_build (&s_pkey, NULL,
"(public-key(elg(p%m)(g%m)(y%m)))",
pkey[0], pkey[1], pkey[2]);
}
else
return GPG_ERR_PUBKEY_ALGO;
if (rc)
BUG ();
/* put the data into a simple list */
if (gcry_sexp_build (&s_data, NULL, "%m", data))
BUG ();
/* pass it to libgcrypt */
rc = gcry_pk_encrypt (&s_ciph, s_data, s_pkey);
gcry_sexp_release (s_data);
gcry_sexp_release (s_pkey);
if (rc)
;
else
{ /* add better error handling or make gnupg use S-Exp directly */
gcry_sexp_t list = gcry_sexp_find_token (s_ciph, "a", 0);
assert (list);
resarr[0] = gcry_sexp_nth_mpi (list, 1, 0);
assert (resarr[0]);
gcry_sexp_release (list);
list = gcry_sexp_find_token (s_ciph, "b", 0);
assert (list);
resarr[1] = gcry_sexp_nth_mpi (list, 1, 0);
assert (resarr[1]);
gcry_sexp_release (list);
}
gcry_sexp_release (s_ciph);
return rc;
}
/****************
* Emulate our old PK interface here - sometime in the future we might
* change the internal design to directly fit to libgcrypt.
*/
int
pk_decrypt (int algo, gcry_mpi_t * result, gcry_mpi_t * data,
gcry_mpi_t * skey)
{
gcry_sexp_t s_skey, s_data, s_plain;
int rc;
*result = NULL;
/* make a sexp from skey */
if (algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E)
{
rc = gcry_sexp_build (&s_skey, NULL,
"(private-key(elg(p%m)(g%m)(y%m)(x%m)))",
skey[0], skey[1], skey[2], skey[3]);
}
else if (algo == GCRY_PK_RSA)
{
rc = gcry_sexp_build (&s_skey, NULL,
"(private-key(rsa(n%m)(e%m)(d%m)(p%m)(q%m)(u%m)))",
skey[0], skey[1], skey[2], skey[3], skey[4],
skey[5]);
}
else
return GPG_ERR_PUBKEY_ALGO;
if (rc)
BUG ();
/* put data into a S-Exp s_data */
if (algo == GCRY_PK_ELG || algo == GCRY_PK_ELG_E)
{
rc = gcry_sexp_build (&s_data, NULL,
"(enc-val(elg(a%m)(b%m)))", data[0], data[1]);
}
else if (algo == GCRY_PK_RSA)
{
rc = gcry_sexp_build (&s_data, NULL, "(enc-val(rsa(a%m)))", data[0]);
}
else
BUG ();
if (rc)
BUG ();
rc = gcry_pk_decrypt (&s_plain, s_data, s_skey);
gcry_sexp_release (s_skey);
gcry_sexp_release (s_data);
if (rc)
return rc;
*result = gcry_sexp_nth_mpi (s_plain, 0, 0);
gcry_sexp_release (s_plain);
if (!*result)
return -1; /* oops */
return 0;
}

34
g10/pkglue.h Normal file
View File

@ -0,0 +1,34 @@
/* pkglue.h - public key operations definitions
* Copyright (C) 2003 Free Software Foundation, Inc.
*
* 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
*/
#ifndef GNUPG_G10_PKGLUE_H
#define GNUPG_G10_PKGLUE_H
int pk_sign (int algo, gcry_mpi_t *data, gcry_mpi_t hash,
gcry_mpi_t *skey);
int pk_verify (int algo, gcry_mpi_t hash, gcry_mpi_t *data,
gcry_mpi_t *pkey);
int pk_encrypt (int algo, gcry_mpi_t *resarr, gcry_mpi_t data,
gcry_mpi_t *pkey);
int pk_decrypt (int algo, gcry_mpi_t *result, gcry_mpi_t *data,
gcry_mpi_t *skey);
#endif /*GNUPG_G10_PKGLUE_H*/

View File

@ -1,5 +1,6 @@
/* plaintext.c - process plaintext packets /* plaintext.c - process plaintext packets
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2002,
* 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -28,6 +29,7 @@
#include <fcntl.h> /* for setmode() */ #include <fcntl.h> /* for setmode() */
#endif #endif
#include "gpg.h"
#include "util.h" #include "util.h"
#include "memory.h" #include "memory.h"
#include "options.h" #include "options.h"
@ -48,7 +50,7 @@
*/ */
int int
handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx, handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
int nooutput, int clearsig ) int nooutput, int clearsig, int *create_failed )
{ {
char *fname = NULL; char *fname = NULL;
FILE *fp = NULL; FILE *fp = NULL;
@ -58,12 +60,17 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
#ifdef __riscos__ #ifdef __riscos__
int filetype = 0xfff; int filetype = 0xfff;
#endif #endif
int dummy_create_failed;
if (!create_failed)
create_failed = &dummy_create_failed;
*create_failed = 0;
/* create the filename as C string */ /* create the filename as C string */
if( nooutput ) if( nooutput )
; ;
else if( opt.outfile ) { else if( opt.outfile ) {
fname = m_alloc( strlen( opt.outfile ) + 1); fname = xmalloc ( strlen( opt.outfile ) + 1);
strcpy(fname, opt.outfile ); strcpy(fname, opt.outfile );
} }
else if( pt->namelen == 8 && !memcmp( pt->name, "_CONSOLE", 8 ) ) { else if( pt->namelen == 8 && !memcmp( pt->name, "_CONSOLE", 8 ) ) {
@ -75,7 +82,8 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
if( !fname ) if( !fname )
fname = ask_outfile_name( pt->name, pt->namelen ); fname = ask_outfile_name( pt->name, pt->namelen );
if( !fname ) { if( !fname ) {
rc = G10ERR_CREATE_FILE; *create_failed = 1;
rc = GPG_ERR_GENERAL;
goto leave; goto leave;
} }
} }
@ -96,11 +104,12 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
while( !overwrite_filep (fname) ) { while( !overwrite_filep (fname) ) {
char *tmp = ask_outfile_name (NULL, 0); char *tmp = ask_outfile_name (NULL, 0);
if ( !tmp || !*tmp ) { if ( !tmp || !*tmp ) {
m_free (tmp); xfree (tmp);
rc = G10ERR_CREATE_FILE; *create_failed = 1;
rc = GPG_ERR_GENERAL;
goto leave; goto leave;
} }
m_free (fname); xfree (fname);
fname = tmp; fname = tmp;
} }
} }
@ -109,8 +118,9 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
if( fp || nooutput ) if( fp || nooutput )
; ;
else if( !(fp = fopen(fname,"wb")) ) { else if( !(fp = fopen(fname,"wb")) ) {
rc = gpg_error_from_errno (errno);
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) ); log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
rc = G10ERR_CREATE_FILE; *create_failed = 1;
goto leave; goto leave;
} }
#else /* __riscos__ */ #else /* __riscos__ */
@ -124,8 +134,9 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
else { else {
fp = fopen(fname,"wb"); fp = fopen(fname,"wb");
if( !fp ) { if( !fp ) {
rc == gpg_error_from_errno (errno);
log_error(_("error creating `%s': %s\n"), fname, strerror(errno) ); log_error(_("error creating `%s': %s\n"), fname, strerror(errno) );
rc = G10ERR_CREATE_FILE; *create_failed = 1;
if (errno == 106) if (errno == 106)
log_info("Do output file and input file have the same name?\n"); log_info("Do output file and input file have the same name?\n");
goto leave; goto leave;
@ -150,76 +161,76 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
if( convert ) { /* text mode */ if( convert ) { /* text mode */
for( ; pt->len; pt->len-- ) { for( ; pt->len; pt->len-- ) {
if( (c = iobuf_get(pt->buf)) == -1 ) { if( (c = iobuf_get(pt->buf)) == -1 ) {
rc = gpg_error_from_errno (errno);
log_error("Problem reading source (%u bytes remaining)\n", log_error("Problem reading source (%u bytes remaining)\n",
(unsigned)pt->len); (unsigned)pt->len);
rc = G10ERR_READ_FILE;
goto leave; goto leave;
} }
if( mfx->md ) if( mfx->md )
md_putc(mfx->md, c ); gcry_md_putc (mfx->md, c );
#ifndef HAVE_DOSISH_SYSTEM #ifndef HAVE_DOSISH_SYSTEM
if( c == '\r' ) /* convert to native line ending */ if( c == '\r' ) /* convert to native line ending */
continue; /* fixme: this hack might be too simple */ continue; /* fixme: this hack might be too simple */
#endif #endif
if( fp ) { if( fp ) {
if( putc( c, fp ) == EOF ) { if( putc( c, fp ) == EOF ) {
rc = gpg_error_from_errno (errno);
log_error("Error writing to `%s': %s\n", log_error("Error writing to `%s': %s\n",
fname, strerror(errno) ); fname, strerror(errno) );
rc = G10ERR_WRITE_FILE;
goto leave; goto leave;
} }
} }
} }
} }
else { /* binary mode */ else { /* binary mode */
byte *buffer = m_alloc( 32768 ); byte *buffer = xmalloc ( 32768 );
while( pt->len ) { while( pt->len ) {
int len = pt->len > 32768 ? 32768 : pt->len; int len = pt->len > 32768 ? 32768 : pt->len;
len = iobuf_read( pt->buf, buffer, len ); len = iobuf_read( pt->buf, buffer, len );
if( len == -1 ) { if( len == -1 ) {
rc = gpg_error_from_errno (errno);
log_error("Problem reading source (%u bytes remaining)\n", log_error("Problem reading source (%u bytes remaining)\n",
(unsigned)pt->len); (unsigned)pt->len);
rc = G10ERR_READ_FILE; xfree ( buffer );
m_free( buffer );
goto leave; goto leave;
} }
if( mfx->md ) if( mfx->md )
md_write( mfx->md, buffer, len ); gcry_md_write( mfx->md, buffer, len );
if( fp ) { if( fp ) {
if( fwrite( buffer, 1, len, fp ) != len ) { if( fwrite( buffer, 1, len, fp ) != len ) {
rc = gpg_error_from_errno (errno);
log_error("Error writing to `%s': %s\n", log_error("Error writing to `%s': %s\n",
fname, strerror(errno) ); fname, strerror(errno) );
rc = G10ERR_WRITE_FILE; xfree ( buffer );
m_free( buffer );
goto leave; goto leave;
} }
} }
pt->len -= len; pt->len -= len;
} }
m_free( buffer ); xfree ( buffer );
} }
} }
else if( !clearsig ) { else if( !clearsig ) {
if( convert ) { /* text mode */ if( convert ) { /* text mode */
while( (c = iobuf_get(pt->buf)) != -1 ) { while( (c = iobuf_get(pt->buf)) != -1 ) {
if( mfx->md ) if( mfx->md )
md_putc(mfx->md, c ); gcry_md_putc (mfx->md, c );
#ifndef HAVE_DOSISH_SYSTEM #ifndef HAVE_DOSISH_SYSTEM
if( convert && c == '\r' ) if( convert && c == '\r' )
continue; /* fixme: this hack might be too simple */ continue; /* fixme: this hack might be too simple */
#endif #endif
if( fp ) { if( fp ) {
if( putc( c, fp ) == EOF ) { if( putc( c, fp ) == EOF ) {
rc = gpg_error_from_errno (errno);
log_error("Error writing to `%s': %s\n", log_error("Error writing to `%s': %s\n",
fname, strerror(errno) ); fname, strerror(errno) );
rc = G10ERR_WRITE_FILE;
goto leave; goto leave;
} }
} }
} }
} }
else { /* binary mode */ else { /* binary mode */
byte *buffer = m_alloc( 32768 ); byte *buffer = xmalloc ( 32768 );
int eof; int eof;
for( eof=0; !eof; ) { for( eof=0; !eof; ) {
/* Why do we check for len < 32768: /* Why do we check for len < 32768:
@ -234,18 +245,18 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
if( len < 32768 ) if( len < 32768 )
eof = 1; eof = 1;
if( mfx->md ) if( mfx->md )
md_write( mfx->md, buffer, len ); gcry_md_write( mfx->md, buffer, len );
if( fp ) { if( fp ) {
if( fwrite( buffer, 1, len, fp ) != len ) { if( fwrite( buffer, 1, len, fp ) != len ) {
rc = gpg_error_from_errno (errno);
log_error("Error writing to `%s': %s\n", log_error("Error writing to `%s': %s\n",
fname, strerror(errno) ); fname, strerror(errno) );
rc = G10ERR_WRITE_FILE; xfree ( buffer );
m_free( buffer );
goto leave; goto leave;
} }
} }
} }
m_free( buffer ); xfree ( buffer );
} }
pt->buf = NULL; pt->buf = NULL;
} }
@ -255,17 +266,17 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
while( (c = iobuf_get(pt->buf)) != -1 ) { while( (c = iobuf_get(pt->buf)) != -1 ) {
if( fp ) { if( fp ) {
if( putc( c, fp ) == EOF ) { if( putc( c, fp ) == EOF ) {
rc = gpg_error_from_errno (errno);
log_error("Error writing to `%s': %s\n", log_error("Error writing to `%s': %s\n",
fname, strerror(errno) ); fname, strerror(errno) );
rc = G10ERR_WRITE_FILE;
goto leave; goto leave;
} }
} }
if( !mfx->md ) if( !mfx->md )
continue; continue;
if( state == 2 ) { if( state == 2 ) {
md_putc(mfx->md, '\r' ); gcry_md_putc (mfx->md, '\r' );
md_putc(mfx->md, '\n' ); gcry_md_putc (mfx->md, '\n' );
state = 0; state = 0;
} }
if( !state ) { if( !state ) {
@ -274,18 +285,18 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
else if( c == '\n' ) else if( c == '\n' )
state = 2; state = 2;
else else
md_putc(mfx->md, c ); gcry_md_putc (mfx->md, c );
} }
else if( state == 1 ) { else if( state == 1 ) {
if( c == '\n' ) if( c == '\n' )
state = 2; state = 2;
else { else {
md_putc(mfx->md, '\r' ); gcry_md_putc (mfx->md, '\r' );
if( c == '\r' ) if( c == '\r' )
state = 1; state = 1;
else { else {
state = 0; state = 0;
md_putc(mfx->md, c ); gcry_md_putc (mfx->md, c );
} }
} }
} }
@ -294,9 +305,9 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
} }
if( fp && fp != stdout && fclose(fp) ) { if( fp && fp != stdout && fclose(fp) ) {
rc = gpg_error_from_errno (errno);
log_error("Error closing `%s': %s\n", fname, strerror(errno) ); log_error("Error closing `%s': %s\n", fname, strerror(errno) );
fp = NULL; fp = NULL;
rc = G10ERR_WRITE_FILE;
goto leave; goto leave;
} }
fp = NULL; fp = NULL;
@ -304,12 +315,12 @@ handle_plaintext( PKT_plaintext *pt, md_filter_context_t *mfx,
leave: leave:
if( fp && fp != stdout ) if( fp && fp != stdout )
fclose(fp); fclose(fp);
m_free(fname); xfree (fname);
return rc; return rc;
} }
static void static void
do_hash( MD_HANDLE md, MD_HANDLE md2, IOBUF fp, int textmode ) do_hash( MD_HANDLE md, MD_HANDLE md2, iobuf_t fp, int textmode )
{ {
text_filter_context_t tfx; text_filter_context_t tfx;
int c; int c;
@ -323,27 +334,27 @@ do_hash( MD_HANDLE md, MD_HANDLE md2, IOBUF fp, int textmode )
int lc = -1; int lc = -1;
while( (c = iobuf_get(fp)) != -1 ) { while( (c = iobuf_get(fp)) != -1 ) {
if( c == '\n' && lc == '\r' ) if( c == '\n' && lc == '\r' )
md_putc(md2, c); gcry_md_putc (md2, c);
else if( c == '\n' ) { else if( c == '\n' ) {
md_putc(md2, '\r'); gcry_md_putc (md2, '\r');
md_putc(md2, c); gcry_md_putc (md2, c);
} }
else if( c != '\n' && lc == '\r' ) { else if( c != '\n' && lc == '\r' ) {
md_putc(md2, '\n'); gcry_md_putc (md2, '\n');
md_putc(md2, c); gcry_md_putc (md2, c);
} }
else else
md_putc(md2, c); gcry_md_putc (md2, c);
if( md ) if( md )
md_putc(md, c ); gcry_md_putc (md, c );
lc = c; lc = c;
} }
} }
else { else {
while( (c = iobuf_get(fp)) != -1 ) { while( (c = iobuf_get(fp)) != -1 ) {
if( md ) if( md )
md_putc(md, c ); gcry_md_putc (md, c );
} }
} }
} }
@ -359,7 +370,7 @@ ask_for_detached_datafile( MD_HANDLE md, MD_HANDLE md2,
{ {
progress_filter_context_t pfx; progress_filter_context_t pfx;
char *answer = NULL; char *answer = NULL;
IOBUF fp; iobuf_t fp;
int rc = 0; int rc = 0;
fp = open_sigfile( inname, &pfx ); /* open default file */ fp = open_sigfile( inname, &pfx ); /* open default file */
@ -368,12 +379,12 @@ ask_for_detached_datafile( MD_HANDLE md, MD_HANDLE md2,
int any=0; int any=0;
tty_printf(_("Detached signature.\n")); tty_printf(_("Detached signature.\n"));
do { do {
m_free(answer); xfree (answer);
answer = cpr_get("detached_signature.filename", answer = cpr_get("detached_signature.filename",
_("Please enter name of data file: ")); _("Please enter name of data file: "));
cpr_kill_prompt(); cpr_kill_prompt();
if( any && !*answer ) { if( any && !*answer ) {
rc = G10ERR_READ_FILE; rc = GPG_ERR_GENERAL;
goto leave; goto leave;
} }
fp = iobuf_open(answer); fp = iobuf_open(answer);
@ -382,8 +393,8 @@ ask_for_detached_datafile( MD_HANDLE md, MD_HANDLE md2,
any++; any++;
} }
else if( !fp ) { else if( !fp ) {
rc = gpg_error_from_errno (errno);
log_error("can't open `%s': %s\n", answer, strerror(errno) ); log_error("can't open `%s': %s\n", answer, strerror(errno) );
rc = G10ERR_READ_FILE;
goto leave; goto leave;
} }
} while( !fp ); } while( !fp );
@ -399,7 +410,7 @@ ask_for_detached_datafile( MD_HANDLE md, MD_HANDLE md2,
iobuf_close(fp); iobuf_close(fp);
leave: leave:
m_free(answer); xfree (answer);
return rc; return rc;
} }
@ -414,7 +425,7 @@ hash_datafiles( MD_HANDLE md, MD_HANDLE md2, STRLIST files,
const char *sigfilename, int textmode ) const char *sigfilename, int textmode )
{ {
progress_filter_context_t pfx; progress_filter_context_t pfx;
IOBUF fp; iobuf_t fp;
STRLIST sl; STRLIST sl;
if( !files ) { if( !files ) {
@ -426,16 +437,17 @@ hash_datafiles( MD_HANDLE md, MD_HANDLE md2, STRLIST files,
return 0; return 0;
} }
log_error (_("no signed data\n")); log_error (_("no signed data\n"));
return G10ERR_OPEN_FILE; return GPG_ERR_NO_DATA;
} }
for (sl=files; sl; sl = sl->next ) { for (sl=files; sl; sl = sl->next ) {
fp = iobuf_open( sl->d ); fp = iobuf_open( sl->d );
if( !fp ) { if( !fp ) {
int tmperr = gpg_error_from_errno (errno);
log_error(_("can't open signed data `%s'\n"), log_error(_("can't open signed data `%s'\n"),
print_fname_stdin(sl->d)); print_fname_stdin(sl->d));
return G10ERR_OPEN_FILE; return tmperr;
} }
handle_progress (&pfx, fp, sl->d); handle_progress (&pfx, fp, sl->d);
do_hash( md, md2, fp, textmode ); do_hash( md, md2, fp, textmode );

View File

@ -21,6 +21,7 @@
#include <config.h> #include <config.h>
#include <stdio.h> #include <stdio.h>
#include "gpg.h"
#include "iobuf.h" #include "iobuf.h"
#include "filter.h" #include "filter.h"
#include "status.h" #include "status.h"
@ -32,7 +33,7 @@
*/ */
int int
progress_filter (void *opaque, int control, progress_filter (void *opaque, int control,
IOBUF a, byte *buf, size_t *ret_len) iobuf_t a, byte *buf, size_t *ret_len)
{ {
int rc = 0; int rc = 0;
progress_filter_context_t *pfx = opaque; progress_filter_context_t *pfx = opaque;
@ -86,7 +87,7 @@ progress_filter (void *opaque, int control,
/* Note, that we must always dealloc resources of a filter /* Note, that we must always dealloc resources of a filter
within the filter handler and not anywhere else. (We set it within the filter handler and not anywhere else. (We set it
to NULL and check all uses just in case.) */ to NULL and check all uses just in case.) */
m_free (pfx->what); xfree (pfx->what);
pfx->what = NULL; pfx->what = NULL;
} }
else if (control == IOBUFCTRL_DESC) else if (control == IOBUFCTRL_DESC)
@ -95,7 +96,7 @@ progress_filter (void *opaque, int control,
} }
void void
handle_progress (progress_filter_context_t *pfx, IOBUF inp, const char *name) handle_progress (progress_filter_context_t *pfx, iobuf_t inp, const char *name)
{ {
off_t filesize = 0; off_t filesize = 0;
@ -111,7 +112,7 @@ handle_progress (progress_filter_context_t *pfx, IOBUF inp, const char *name)
filesize = opt.set_filesize; filesize = opt.set_filesize;
/* register the progress filter */ /* register the progress filter */
pfx->what = m_strdup (name ? name : "stdin"); pfx->what = xstrdup (name ? name : "stdin");
pfx->total = filesize; pfx->total = filesize;
iobuf_push_filter (inp, progress_filter, pfx); iobuf_push_filter (inp, progress_filter, pfx);
} }

View File

@ -1,5 +1,6 @@
/* pubkey-enc.c - public key encoded packet handling /* pubkey-enc.c - public key encoded packet handling
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2002,
* 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -23,6 +24,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "memory.h" #include "memory.h"
#include "packet.h" #include "packet.h"
@ -34,6 +37,7 @@
#include "options.h" #include "options.h"
#include "main.h" #include "main.h"
#include "i18n.h" #include "i18n.h"
#include "pkglue.h"
static int get_it( PKT_pubkey_enc *k, static int get_it( PKT_pubkey_enc *k,
DEK *dek, PKT_secret_key *sk, u32 *keyid ); DEK *dek, PKT_secret_key *sk, u32 *keyid );
@ -72,12 +76,12 @@ get_session_key( PKT_pubkey_enc *k, DEK *dek )
PKT_secret_key *sk = NULL; PKT_secret_key *sk = NULL;
int rc; int rc;
rc = check_pubkey_algo2 (k->pubkey_algo, PUBKEY_USAGE_ENC); rc = openpgp_pk_test_algo (k->pubkey_algo, PUBKEY_USAGE_ENC);
if( rc ) if( rc )
goto leave; goto leave;
if( (k->keyid[0] || k->keyid[1]) && !opt.try_all_secrets ) { if( (k->keyid[0] || k->keyid[1]) && !opt.try_all_secrets ) {
sk = m_alloc_clear( sizeof *sk ); sk = xcalloc (1, sizeof *sk );
sk->pubkey_algo = k->pubkey_algo; /* we want a pubkey with this algo*/ sk->pubkey_algo = k->pubkey_algo; /* we want a pubkey with this algo*/
if( !(rc = get_seckey( sk, k->keyid )) ) if( !(rc = get_seckey( sk, k->keyid )) )
rc = get_it( k, dek, sk, k->keyid ); rc = get_it( k, dek, sk, k->keyid );
@ -90,10 +94,10 @@ get_session_key( PKT_pubkey_enc *k, DEK *dek )
for(;;) { for(;;) {
if( sk ) if( sk )
free_secret_key( sk ); free_secret_key( sk );
sk = m_alloc_clear( sizeof *sk ); sk = xcalloc (1, sizeof *sk );
rc=enum_secret_keys( &enum_context, sk, 1, 0); rc=enum_secret_keys( &enum_context, sk, 1, 0);
if( rc ) { if( rc ) {
rc = G10ERR_NO_SECKEY; rc = GPG_ERR_NO_SECKEY;
break; break;
} }
if( sk->pubkey_algo != k->pubkey_algo ) if( sk->pubkey_algo != k->pubkey_algo )
@ -106,7 +110,7 @@ get_session_key( PKT_pubkey_enc *k, DEK *dek )
{ {
p=get_last_passphrase(); p=get_last_passphrase();
set_next_passphrase(p); set_next_passphrase(p);
m_free(p); xfree (p);
} }
rc = check_secret_key( sk, opt.try_all_secrets?1:-1 ); /* ask rc = check_secret_key( sk, opt.try_all_secrets?1:-1 ); /* ask
@ -133,16 +137,17 @@ static int
get_it( PKT_pubkey_enc *enc, DEK *dek, PKT_secret_key *sk, u32 *keyid ) get_it( PKT_pubkey_enc *enc, DEK *dek, PKT_secret_key *sk, u32 *keyid )
{ {
int rc; int rc;
MPI plain_dek = NULL; gcry_mpi_t plain_dek = NULL;
byte *frame = NULL; byte *frame = NULL;
unsigned n, nframe; unsigned n, nframe;
u16 csum, csum2; u16 csum, csum2;
rc = pubkey_decrypt(sk->pubkey_algo, &plain_dek, enc->data, sk->skey ); rc = pk_decrypt (sk->pubkey_algo, &plain_dek, enc->data, sk->skey);
if( rc ) if( rc )
goto leave; goto leave;
frame = mpi_get_buffer( plain_dek, &nframe, NULL ); if ( gcry_mpi_aprint (GCRYMPI_FMT_USG, &frame, &nframe, plain_dek))
mpi_free( plain_dek ); plain_dek = NULL; BUG();
gcry_mpi_release (plain_dek); plain_dek = NULL;
/* Now get the DEK (data encryption key) from the frame /* Now get the DEK (data encryption key) from the frame
* *
@ -162,30 +167,30 @@ get_it( PKT_pubkey_enc *enc, DEK *dek, PKT_secret_key *sk, u32 *keyid )
* CSUM * CSUM
*/ */
if( DBG_CIPHER ) if( DBG_CIPHER )
log_hexdump("DEK frame:", frame, nframe ); log_printhex ("DEK frame:", frame, nframe );
n=0; n=0;
if( n + 7 > nframe ) if( n + 7 > nframe )
{ rc = G10ERR_WRONG_SECKEY; goto leave; } { rc = GPG_ERR_WRONG_SECKEY; goto leave; }
if( frame[n] == 1 && frame[nframe-1] == 2 ) { if( frame[n] == 1 && frame[nframe-1] == 2 ) {
log_info(_("old encoding of the DEK is not supported\n")); log_info(_("old encoding of the DEK is not supported\n"));
rc = G10ERR_CIPHER_ALGO; rc = GPG_ERR_CIPHER_ALGO;
goto leave; goto leave;
} }
if( frame[n] != 2 ) /* somethink is wrong */ if( frame[n] != 2 ) /* somethink is wrong */
{ rc = G10ERR_WRONG_SECKEY; goto leave; } { rc = GPG_ERR_WRONG_SECKEY; goto leave; }
for(n++; n < nframe && frame[n]; n++ ) /* skip the random bytes */ for(n++; n < nframe && frame[n]; n++ ) /* skip the random bytes */
; ;
n++; /* and the zero byte */ n++; /* and the zero byte */
if( n + 4 > nframe ) if( n + 4 > nframe )
{ rc = G10ERR_WRONG_SECKEY; goto leave; } { rc = GPG_ERR_WRONG_SECKEY; goto leave; }
dek->keylen = nframe - (n+1) - 2; dek->keylen = nframe - (n+1) - 2;
dek->algo = frame[n++]; dek->algo = frame[n++];
if( dek->algo == CIPHER_ALGO_IDEA ) if( dek->algo == CIPHER_ALGO_IDEA )
write_status(STATUS_RSA_OR_IDEA); write_status(STATUS_RSA_OR_IDEA);
rc = check_cipher_algo( dek->algo ); rc = openpgp_cipher_test_algo (dek->algo);
if( rc ) { if( rc ) {
if( !opt.quiet && rc == G10ERR_CIPHER_ALGO ) { if( !opt.quiet && rc == GPG_ERR_CIPHER_ALGO ) {
log_info(_("cipher algorithm %d%s is unknown or disabled\n"), log_info(_("cipher algorithm %d%s is unknown or disabled\n"),
dek->algo, dek->algo == CIPHER_ALGO_IDEA? " (IDEA)":""); dek->algo, dek->algo == CIPHER_ALGO_IDEA? " (IDEA)":"");
if(dek->algo==CIPHER_ALGO_IDEA) if(dek->algo==CIPHER_ALGO_IDEA)
@ -194,8 +199,8 @@ get_it( PKT_pubkey_enc *enc, DEK *dek, PKT_secret_key *sk, u32 *keyid )
dek->algo = 0; dek->algo = 0;
goto leave; goto leave;
} }
if( (dek->keylen*8) != cipher_get_keylen( dek->algo ) ) { if( dek->keylen != gcry_cipher_get_algo_keylen (dek->algo) ) {
rc = G10ERR_WRONG_SECKEY; rc = GPG_ERR_WRONG_SECKEY;
goto leave; goto leave;
} }
@ -206,11 +211,11 @@ get_it( PKT_pubkey_enc *enc, DEK *dek, PKT_secret_key *sk, u32 *keyid )
for( csum2=0, n=0; n < dek->keylen; n++ ) for( csum2=0, n=0; n < dek->keylen; n++ )
csum2 += dek->key[n]; csum2 += dek->key[n];
if( csum != csum2 ) { if( csum != csum2 ) {
rc = G10ERR_WRONG_SECKEY; rc = GPG_ERR_WRONG_SECKEY;
goto leave; goto leave;
} }
if( DBG_CIPHER ) if( DBG_CIPHER )
log_hexdump("DEK is:", dek->key, dek->keylen ); log_printhex ("DEK is:", dek->key, dek->keylen );
/* check that the algo is in the preferences and whether it has expired */ /* check that the algo is in the preferences and whether it has expired */
{ {
PKT_public_key *pk = NULL; PKT_public_key *pk = NULL;
@ -258,7 +263,7 @@ get_it( PKT_pubkey_enc *enc, DEK *dek, PKT_secret_key *sk, u32 *keyid )
if ( pk && pk->is_revoked ) { if ( pk && pk->is_revoked ) {
log_info( _("NOTE: key has been revoked") ); log_info( _("NOTE: key has been revoked") );
putc( '\n', log_stream() ); putc( '\n', log_get_stream() );
show_revocation_reason( pk, 1 ); show_revocation_reason( pk, 1 );
} }
@ -268,8 +273,8 @@ get_it( PKT_pubkey_enc *enc, DEK *dek, PKT_secret_key *sk, u32 *keyid )
leave: leave:
mpi_free(plain_dek); gcry_mpi_release (plain_dek);
m_free(frame); xfree (frame);
return rc; return rc;
} }
@ -286,21 +291,21 @@ get_override_session_key( DEK *dek, const char *string )
int i; int i;
if ( !string ) if ( !string )
return G10ERR_BAD_KEY; return GPG_ERR_BAD_KEY;
dek->algo = atoi(string); dek->algo = atoi(string);
if ( dek->algo < 1 ) if ( dek->algo < 1 )
return G10ERR_BAD_KEY; return GPG_ERR_BAD_KEY;
if ( !(s = strchr ( string, ':' )) ) if ( !(s = strchr ( string, ':' )) )
return G10ERR_BAD_KEY; return GPG_ERR_BAD_KEY;
s++; s++;
for(i=0; i < DIM(dek->key) && *s; i++, s +=2 ) { for(i=0; i < DIM(dek->key) && *s; i++, s +=2 ) {
int c = hextobyte ( s ); int c = hextobyte ( s );
if (c == -1) if (c == -1)
return G10ERR_BAD_KEY; return GPG_ERR_BAD_KEY;
dek->key[i] = c; dek->key[i] = c;
} }
if ( *s ) if ( *s )
return G10ERR_BAD_KEY; return GPG_ERR_BAD_KEY;
dek->keylen = i; dek->keylen = i;
return 0; return 0;
} }

View File

@ -59,15 +59,15 @@ revocation_reason_build_cb( PKT_signature *sig, void *opaque )
ud = native_to_utf8( reason->desc ); ud = native_to_utf8( reason->desc );
buflen += strlen(ud); buflen += strlen(ud);
} }
buffer = m_alloc( buflen ); buffer = xmalloc ( buflen );
*buffer = reason->code; *buffer = reason->code;
if( ud ) { if( ud ) {
memcpy(buffer+1, ud, strlen(ud) ); memcpy(buffer+1, ud, strlen(ud) );
m_free( ud ); xfree ( ud );
} }
build_sig_subpkt( sig, SIGSUBPKT_REVOC_REASON, buffer, buflen ); build_sig_subpkt( sig, SIGSUBPKT_REVOC_REASON, buffer, buflen );
m_free( buffer ); xfree ( buffer );
return 0; return 0;
} }
@ -76,7 +76,7 @@ revocation_reason_build_cb( PKT_signature *sig, void *opaque )
and pick a user ID that has a uid signature, and include it if and pick a user ID that has a uid signature, and include it if
possible. */ possible. */
static int static int
export_minimal_pk(IOBUF out,KBNODE keyblock, export_minimal_pk(iobuf_t out,KBNODE keyblock,
PKT_signature *revsig,PKT_signature *revkey) PKT_signature *revsig,PKT_signature *revkey)
{ {
KBNODE node; KBNODE node;
@ -90,7 +90,7 @@ export_minimal_pk(IOBUF out,KBNODE keyblock,
if(!node) if(!node)
{ {
log_error(_("key incomplete\n")); log_error(_("key incomplete\n"));
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
keyid_from_pk(node->pkt->pkt.public_key,keyid); keyid_from_pk(node->pkt->pkt.public_key,keyid);
@ -99,7 +99,7 @@ export_minimal_pk(IOBUF out,KBNODE keyblock,
rc=build_packet(out,&pkt); rc=build_packet(out,&pkt);
if(rc) if(rc)
{ {
log_error(_("build_packet failed: %s\n"), g10_errstr(rc) ); log_error(_("build_packet failed: %s\n"), gpg_strerror (rc) );
return rc; return rc;
} }
@ -113,7 +113,7 @@ export_minimal_pk(IOBUF out,KBNODE keyblock,
rc=build_packet(out,&pkt); rc=build_packet(out,&pkt);
if(rc) if(rc)
{ {
log_error(_("build_packet failed: %s\n"), g10_errstr(rc) ); log_error(_("build_packet failed: %s\n"), gpg_strerror (rc) );
return rc; return rc;
} }
} }
@ -125,7 +125,7 @@ export_minimal_pk(IOBUF out,KBNODE keyblock,
rc=build_packet(out,&pkt); rc=build_packet(out,&pkt);
if(rc) if(rc)
{ {
log_error(_("build_packet failed: %s\n"), g10_errstr(rc) ); log_error(_("build_packet failed: %s\n"), gpg_strerror (rc) );
return rc; return rc;
} }
} }
@ -143,7 +143,7 @@ export_minimal_pk(IOBUF out,KBNODE keyblock,
else else
{ {
log_error(_("key %08lX incomplete\n"),(ulong)keyid[1]); log_error(_("key %08lX incomplete\n"),(ulong)keyid[1]);
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
} }
@ -171,7 +171,7 @@ export_minimal_pk(IOBUF out,KBNODE keyblock,
rc=build_packet(out,&pkt); rc=build_packet(out,&pkt);
if(rc) if(rc)
{ {
log_error(_("build_packet failed: %s\n"), g10_errstr(rc) ); log_error(_("build_packet failed: %s\n"), gpg_strerror (rc) );
return rc; return rc;
} }
@ -183,7 +183,7 @@ export_minimal_pk(IOBUF out,KBNODE keyblock,
rc=build_packet(out,&pkt); rc=build_packet(out,&pkt);
if(rc) if(rc)
{ {
log_error(_("build_packet failed: %s\n"), g10_errstr(rc) ); log_error(_("build_packet failed: %s\n"), gpg_strerror (rc) );
return rc; return rc;
} }
} }
@ -202,7 +202,7 @@ gen_desig_revoke( const char *uname )
PKT_public_key *pk = NULL; PKT_public_key *pk = NULL;
PKT_secret_key *sk = NULL; PKT_secret_key *sk = NULL;
PKT_signature *sig = NULL; PKT_signature *sig = NULL;
IOBUF out = NULL; iobuf_t out = NULL;
struct revocation_reason_info *reason = NULL; struct revocation_reason_info *reason = NULL;
KEYDB_HANDLE kdbhd; KEYDB_HANDLE kdbhd;
KEYDB_SEARCH_DESC desc; KEYDB_SEARCH_DESC desc;
@ -212,22 +212,22 @@ gen_desig_revoke( const char *uname )
if( opt.batch ) { if( opt.batch ) {
log_error(_("sorry, can't do this in batch mode\n")); log_error(_("sorry, can't do this in batch mode\n"));
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
memset( &afx, 0, sizeof afx); memset( &afx, 0, sizeof afx);
kdbhd = keydb_new (0); kdbhd = keydb_new (0);
classify_user_id (uname, &desc); classify_user_id (uname, &desc);
rc = desc.mode? keydb_search (kdbhd, &desc, 1) : G10ERR_INV_USER_ID; rc = desc.mode? keydb_search (kdbhd, &desc, 1) : GPG_ERR_INV_USER_ID;
if (rc) { if (rc) {
log_error (_("key `%s' not found: %s\n"),uname, g10_errstr (rc)); log_error (_("key `%s' not found: %s\n"),uname, gpg_strerror (rc));
goto leave; goto leave;
} }
rc = keydb_get_keyblock (kdbhd, &keyblock ); rc = keydb_get_keyblock (kdbhd, &keyblock );
if( rc ) { if( rc ) {
log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) ); log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc) );
goto leave; goto leave;
} }
@ -253,7 +253,7 @@ gen_desig_revoke( const char *uname )
if(sk) if(sk)
free_secret_key(sk); free_secret_key(sk);
sk=m_alloc_clear(sizeof(*sk)); sk=xcalloc (1,sizeof(*sk));
rc=get_seckey_byfprint(sk,pk->revkey[i].fpr,MAX_FINGERPRINT_LEN); rc=get_seckey_byfprint(sk,pk->revkey[i].fpr,MAX_FINGERPRINT_LEN);
@ -302,7 +302,7 @@ gen_desig_revoke( const char *uname )
0, 0, 0, 0, 0, 0,
revocation_reason_build_cb, reason ); revocation_reason_build_cb, reason );
if( rc ) { if( rc ) {
log_error(_("make_keysig_packet failed: %s\n"), g10_errstr(rc)); log_error(_("make_keysig_packet failed: %s\n"), gpg_strerror (rc));
goto leave; goto leave;
} }
@ -403,7 +403,7 @@ gen_revoke( const char *uname )
PKT_public_key *pk = NULL; PKT_public_key *pk = NULL;
PKT_signature *sig = NULL; PKT_signature *sig = NULL;
u32 sk_keyid[2]; u32 sk_keyid[2];
IOBUF out = NULL; iobuf_t out = NULL;
KBNODE keyblock = NULL, pub_keyblock = NULL; KBNODE keyblock = NULL, pub_keyblock = NULL;
KBNODE node; KBNODE node;
KEYDB_HANDLE kdbhd; KEYDB_HANDLE kdbhd;
@ -412,7 +412,7 @@ gen_revoke( const char *uname )
if( opt.batch ) { if( opt.batch ) {
log_error(_("sorry, can't do this in batch mode\n")); log_error(_("sorry, can't do this in batch mode\n"));
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
memset( &afx, 0, sizeof afx); memset( &afx, 0, sizeof afx);
@ -423,16 +423,16 @@ gen_revoke( const char *uname )
*/ */
kdbhd = keydb_new (1); kdbhd = keydb_new (1);
classify_user_id (uname, &desc); classify_user_id (uname, &desc);
rc = desc.mode? keydb_search (kdbhd, &desc, 1) : G10ERR_INV_USER_ID; rc = desc.mode? keydb_search (kdbhd, &desc, 1) : GPG_ERR_INV_USER_ID;
if (rc) { if (rc) {
log_error (_("secret key `%s' not found: %s\n"), log_error (_("secret key `%s' not found: %s\n"),
uname, g10_errstr (rc)); uname, gpg_strerror (rc));
goto leave; goto leave;
} }
rc = keydb_get_keyblock (kdbhd, &keyblock ); rc = keydb_get_keyblock (kdbhd, &keyblock );
if( rc ) { if( rc ) {
log_error (_("error reading keyblock: %s\n"), g10_errstr(rc) ); log_error (_("error reading keyblock: %s\n"), gpg_strerror (rc) );
goto leave; goto leave;
} }
@ -447,14 +447,14 @@ gen_revoke( const char *uname )
keyid_from_sk( sk, sk_keyid ); keyid_from_sk( sk, sk_keyid );
print_seckey_info (sk); print_seckey_info (sk);
pk = m_alloc_clear( sizeof *pk ); pk = xcalloc (1, sizeof *pk );
/* FIXME: We should get the public key direct from the secret one */ /* FIXME: We should get the public key direct from the secret one */
pub_keyblock=get_pubkeyblock(sk_keyid); pub_keyblock=get_pubkeyblock(sk_keyid);
if(!pub_keyblock) if(!pub_keyblock)
{ {
log_error(_("no corresponding public key: %s\n"), g10_errstr(rc) ); log_error(_("no corresponding public key: %s\n"), gpg_strerror (rc) );
goto leave; goto leave;
} }
@ -466,7 +466,7 @@ gen_revoke( const char *uname )
if( cmp_public_secret_key( pk, sk ) ) { if( cmp_public_secret_key( pk, sk ) ) {
log_error(_("public key does not match secret key!\n") ); log_error(_("public key does not match secret key!\n") );
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
goto leave; goto leave;
} }
@ -489,7 +489,7 @@ gen_revoke( const char *uname )
switch( is_secret_key_protected( sk ) ) { switch( is_secret_key_protected( sk ) ) {
case -1: case -1:
log_error(_("unknown protection algorithm\n")); log_error(_("unknown protection algorithm\n"));
rc = G10ERR_PUBKEY_ALGO; rc = GPG_ERR_PUBKEY_ALGO;
break; break;
case 0: case 0:
tty_printf(_("NOTE: This key is not protected!\n")); tty_printf(_("NOTE: This key is not protected!\n"));
@ -517,7 +517,7 @@ gen_revoke( const char *uname )
opt.force_v4_certs?4:0, 0, 0, opt.force_v4_certs?4:0, 0, 0,
revocation_reason_build_cb, reason ); revocation_reason_build_cb, reason );
if( rc ) { if( rc ) {
log_error(_("make_keysig_packet failed: %s\n"), g10_errstr(rc)); log_error(_("make_keysig_packet failed: %s\n"), gpg_strerror (rc));
goto leave; goto leave;
} }
@ -537,7 +537,7 @@ gen_revoke( const char *uname )
rc = build_packet( out, &pkt ); rc = build_packet( out, &pkt );
if( rc ) { if( rc ) {
log_error(_("build_packet failed: %s\n"), g10_errstr(rc) ); log_error(_("build_packet failed: %s\n"), gpg_strerror (rc) );
goto leave; goto leave;
} }
} }
@ -581,7 +581,7 @@ ask_revocation_reason( int key_rev, int cert_rev, int hint )
do { do {
code=-1; code=-1;
m_free(description); xfree (description);
description = NULL; description = NULL;
tty_printf(_("Please select the reason for the revocation:\n")); tty_printf(_("Please select the reason for the revocation:\n"));
@ -612,7 +612,7 @@ ask_revocation_reason( int key_rev, int cert_rev, int hint )
n = -1; n = -1;
else else
n = atoi(answer); n = atoi(answer);
m_free(answer); xfree (answer);
if( n == 0 ) { if( n == 0 ) {
code = 0x00; /* no particular reason */ code = 0x00; /* no particular reason */
code_text = text_0; code_text = text_0;
@ -644,25 +644,25 @@ ask_revocation_reason( int key_rev, int cert_rev, int hint )
trim_trailing_ws( answer, strlen(answer) ); trim_trailing_ws( answer, strlen(answer) );
cpr_kill_prompt(); cpr_kill_prompt();
if( !*answer ) { if( !*answer ) {
m_free(answer); xfree (answer);
break; break;
} }
{ {
char *p = make_printable_string( answer, strlen(answer), 0 ); char *p = make_printable_string( answer, strlen(answer), 0 );
m_free(answer); xfree (answer);
answer = p; answer = p;
} }
if( !description ) if( !description )
description = m_strdup(answer); description = xstrdup (answer);
else { else {
char *p = m_alloc( strlen(description) + strlen(answer) + 2 ); char *p = xmalloc ( strlen(description) + strlen(answer) + 2 );
strcpy(stpcpy(stpcpy( p, description),"\n"),answer); strcpy(stpcpy(stpcpy( p, description),"\n"),answer);
m_free(description); xfree (description);
description = p; description = p;
} }
m_free(answer); xfree (answer);
} }
tty_printf(_("Reason for revocation: %s\n"), code_text ); tty_printf(_("Reason for revocation: %s\n"), code_text );
@ -674,7 +674,7 @@ ask_revocation_reason( int key_rev, int cert_rev, int hint )
} while( !cpr_get_answer_is_yes("ask_revocation_reason.okay", } while( !cpr_get_answer_is_yes("ask_revocation_reason.okay",
_("Is this okay? ")) ); _("Is this okay? ")) );
reason = m_alloc( sizeof *reason ); reason = xmalloc ( sizeof *reason );
reason->code = code; reason->code = code;
reason->desc = description; reason->desc = description;
return reason; return reason;
@ -684,7 +684,7 @@ void
release_revocation_reason_info( struct revocation_reason_info *reason ) release_revocation_reason_info( struct revocation_reason_info *reason )
{ {
if( reason ) { if( reason ) {
m_free( reason->desc ); xfree ( reason->desc );
m_free( reason ); xfree ( reason );
} }
} }

View File

@ -1,5 +1,6 @@
/* seckey-cert.c - secret key certificate packet handling /* seckey-cert.c - secret key certificate packet handling
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2002,
* 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -23,6 +24,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "memory.h" #include "memory.h"
#include "packet.h" #include "packet.h"
@ -33,7 +36,7 @@
#include "options.h" #include "options.h"
#include "i18n.h" #include "i18n.h"
#include "status.h" #include "status.h"
#include "pkglue.h"
static int static int
do_check( PKT_secret_key *sk, const char *tryagain_text, int mode, do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
@ -43,6 +46,7 @@ do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
u16 csum=0; u16 csum=0;
int i, res; int i, res;
unsigned nbytes; unsigned nbytes;
gpg_error_t rc;
if( sk->is_protected ) { /* remove the protection */ if( sk->is_protected ) { /* remove the protection */
DEK *dek = NULL; DEK *dek = NULL;
@ -52,11 +56,11 @@ do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
if( sk->protect.s2k.mode == 1001 ) { if( sk->protect.s2k.mode == 1001 ) {
log_info(_("secret key parts are not available\n")); log_info(_("secret key parts are not available\n"));
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
if( sk->protect.algo == CIPHER_ALGO_NONE ) if( sk->protect.algo == CIPHER_ALGO_NONE )
BUG(); BUG();
if( check_cipher_algo( sk->protect.algo ) ) { if( openpgp_cipher_test_algo( sk->protect.algo ) ) {
log_info(_("protection algorithm %d%s is not supported\n"), log_info(_("protection algorithm %d%s is not supported\n"),
sk->protect.algo,sk->protect.algo==1?" (IDEA)":"" ); sk->protect.algo,sk->protect.algo==1?" (IDEA)":"" );
if (sk->protect.algo==CIPHER_ALGO_IDEA) if (sk->protect.algo==CIPHER_ALGO_IDEA)
@ -64,7 +68,7 @@ do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
write_status (STATUS_RSA_OR_IDEA); write_status (STATUS_RSA_OR_IDEA);
idea_cipher_warn (0); idea_cipher_warn (0);
} }
return G10ERR_CIPHER_ALGO; return GPG_ERR_CIPHER_ALGO;
} }
keyid_from_sk( sk, keyid ); keyid_from_sk( sk, keyid );
keyid[2] = keyid[3] = 0; keyid[2] = keyid[3] = 0;
@ -76,28 +80,39 @@ do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
&sk->protect.s2k, mode, &sk->protect.s2k, mode,
tryagain_text, canceled ); tryagain_text, canceled );
if (!dek && canceled && *canceled) if (!dek && canceled && *canceled)
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
cipher_hd = cipher_open( sk->protect.algo, rc = gcry_cipher_open (&cipher_hd, sk->protect.algo,
CIPHER_MODE_AUTO_CFB, 1); GCRY_CIPHER_MODE_CFB,
cipher_setkey( cipher_hd, dek->key, dek->keylen ); GCRY_CIPHER_SECURE
m_free(dek); | (sk->protect.algo >= 100 ?
0 : GCRY_CIPHER_ENABLE_SYNC));
if (rc)
log_fatal ("cipher open failed: %s\n", gpg_strerror (rc) );
rc = gcry_cipher_setkey (cipher_hd, dek->key, dek->keylen);
if (rc)
log_fatal ("set key failed: %s\n", gpg_strerror (rc) );
xfree (dek);
save_sk = copy_secret_key( NULL, sk ); save_sk = copy_secret_key( NULL, sk );
cipher_setiv( cipher_hd, sk->protect.iv, sk->protect.ivlen ); gcry_cipher_setiv (cipher_hd, sk->protect.iv, sk->protect.ivlen);
csum = 0; csum = 0;
if( sk->version >= 4 ) { if( sk->version >= 4 ) {
int ndata; int ndata;
unsigned int ndatabits;
byte *p, *data; byte *p, *data;
u16 csumc = 0; u16 csumc = 0;
i = pubkey_get_npkey(sk->pubkey_algo); i = pubkey_get_npkey(sk->pubkey_algo);
assert( mpi_is_opaque( sk->skey[i] ) ); assert( gcry_mpi_get_flag (sk->skey[i], GCRYMPI_FLAG_OPAQUE ));
p = mpi_get_opaque( sk->skey[i], &ndata ); p = gcry_mpi_get_opaque( sk->skey[i], &ndatabits );
ndata = (ndatabits+7)/8;
if ( ndata > 1 ) if ( ndata > 1 )
csumc = p[ndata-2] << 8 | p[ndata-1]; csumc = p[ndata-2] << 8 | p[ndata-1];
data = m_alloc_secure( ndata ); data = gcry_xmalloc_secure ( ndata );
cipher_decrypt( cipher_hd, data, p, ndata ); gcry_cipher_decrypt( cipher_hd, data, ndata, p, ndata );
mpi_free( sk->skey[i] ); sk->skey[i] = NULL ; gcry_mpi_release ( sk->skey[i] ); sk->skey[i] = NULL ;
p = data; p = data;
if (sk->protect.sha1chk) { if (sk->protect.sha1chk) {
/* This is the new SHA1 checksum method to detect /* This is the new SHA1 checksum method to detect
@ -108,12 +123,13 @@ do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
if( ndata < 20 ) if( ndata < 20 )
log_error("not enough bytes for SHA-1 checksum\n"); log_error("not enough bytes for SHA-1 checksum\n");
else { else {
MD_HANDLE h = md_open (DIGEST_ALGO_SHA1, 1); gcry_md_hd_t h;
if (!h)
if ( gcry_md_open (&h, DIGEST_ALGO_SHA1, 1))
BUG(); /* algo not available */ BUG(); /* algo not available */
md_write (h, data, ndata - 20); gcry_md_write (h, data, ndata - 20);
md_final (h); gcry_md_final (h);
if (!memcmp (md_read (h, DIGEST_ALGO_SHA1), if (!memcmp (gcry_md_read (h, DIGEST_ALGO_SHA1),
data + ndata - 20, 20) ) { data + ndata - 20, 20) ) {
/* digest does match. We have to keep the old /* digest does match. We have to keep the old
style checksum in sk->csum, so that the style checksum in sk->csum, so that the
@ -122,7 +138,7 @@ do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
keys. */ keys. */
sk->csum = csum = checksum (data, ndata-20); sk->csum = csum = checksum (data, ndata-20);
} }
md_close (h); gcry_md_close (h);
} }
} }
else { else {
@ -146,56 +162,68 @@ do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
if( sk->csum == csum ) { if( sk->csum == csum ) {
for( ; i < pubkey_get_nskey(sk->pubkey_algo); i++ ) { for( ; i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
nbytes = ndata; nbytes = ndata;
sk->skey[i] = mpi_read_from_buffer(p, &nbytes, 1 ); assert( gcry_is_secure( p ) );
res = gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_PGP,
p, &nbytes);
if( res )
log_bug ("gcry_mpi_scan failed in do_check: %s\n",
gpg_strerror (res));
ndata -= nbytes; ndata -= nbytes;
p += nbytes; p += nbytes;
} }
/* Note: at this point ndata should be 2 for a simple /* Note: at this point ndata should be 2 for a simple
checksum or 20 for the sha1 digest */ checksum or 20 for the sha1 digest */
} }
m_free(data); xfree (data);
} }
else { else {
for(i=pubkey_get_npkey(sk->pubkey_algo); for(i=pubkey_get_npkey(sk->pubkey_algo);
i < pubkey_get_nskey(sk->pubkey_algo); i++ ) { i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
byte *p; byte *p;
int ndata; int ndata;
unsigned int dummy; unsigned int ndatabits;
assert (mpi_is_opaque (sk->skey[i])); assert( gcry_mpi_get_flag (sk->skey[i], GCRYMPI_FLAG_OPAQUE));
p = mpi_get_opaque (sk->skey[i], &ndata); p = gcry_mpi_get_opaque( sk->skey[i], &ndatabits );
ndata = (ndatabits+7)/8;
assert (ndata >= 2); assert (ndata >= 2);
assert (ndata == ((p[0] << 8 | p[1]) + 7)/8 + 2); assert (ndata == ((p[0] << 8 | p[1]) + 7)/8 + 2);
buffer = m_alloc_secure (ndata); buffer = gcry_xmalloc_secure (ndata);
cipher_sync (cipher_hd); gcry_cipher_sync (cipher_hd);
buffer[0] = p[0]; buffer[0] = p[0];
buffer[1] = p[1]; buffer[1] = p[1];
cipher_decrypt (cipher_hd, buffer+2, p+2, ndata-2); gcry_cipher_decrypt (cipher_hd, buffer+2, ndata-2,
p+2, ndata-2);
csum += checksum (buffer, ndata); csum += checksum (buffer, ndata);
mpi_free (sk->skey[i]); gcry_mpi_release (sk->skey[i]);
dummy = ndata; res = gcry_mpi_scan( &sk->skey[i], GCRYMPI_FMT_USG,
sk->skey[i] = mpi_read_from_buffer (buffer, &dummy, 1); buffer, &ndata );
if( res )
log_bug ("gcry_mpi_scan failed in do_check: %s\n",
gpg_strerror (res));
assert (sk->skey[i]); assert (sk->skey[i]);
m_free (buffer); xfree (buffer);
/* csum += checksum_mpi (sk->skey[i]); */ /* csum += checksum_mpi (sk->skey[i]); */
} }
} }
cipher_close( cipher_hd ); gcry_cipher_close (cipher_hd);
/* now let's see whether we have used the right passphrase */ /* now let's see whether we have used the right passphrase */
if( csum != sk->csum ) { if( csum != sk->csum ) {
copy_secret_key( sk, save_sk ); copy_secret_key( sk, save_sk );
passphrase_clear_cache ( keyid, sk->pubkey_algo ); passphrase_clear_cache ( keyid, sk->pubkey_algo );
free_secret_key( save_sk ); free_secret_key( save_sk );
return G10ERR_BAD_PASS; return gpg_error (GPG_ERR_BAD_PASSPHRASE);
} }
/* the checksum may fail, so we also check the key itself */ /* the checksum may fail, so we also check the key itself */
res = pubkey_check_secret_key( sk->pubkey_algo, sk->skey ); #warning fixme - we need to reenable this
if( res ) { /* res = pubkey_check_secret_key( sk->pubkey_algo, sk->skey ); */
copy_secret_key( sk, save_sk ); /* if( res ) { */
passphrase_clear_cache ( keyid, sk->pubkey_algo ); /* copy_secret_key( sk, save_sk ); */
free_secret_key( save_sk ); /* passphrase_clear_cache ( keyid, sk->pubkey_algo ); */
return G10ERR_BAD_PASS; /* free_secret_key( save_sk ); */
} /* return gpg_error (GPG_ERR_BAD_PASSPHRASE); */
/* } */
free_secret_key( save_sk ); free_secret_key( save_sk );
sk->is_protected = 0; sk->is_protected = 0;
} }
@ -206,7 +234,7 @@ do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
csum += checksum_mpi( sk->skey[i] ); csum += checksum_mpi( sk->skey[i] );
} }
if( csum != sk->csum ) if( csum != sk->csum )
return G10ERR_CHECKSUM; return GPG_ERR_CHECKSUM;
} }
return 0; return 0;
@ -222,7 +250,7 @@ do_check( PKT_secret_key *sk, const char *tryagain_text, int mode,
int int
check_secret_key( PKT_secret_key *sk, int n ) check_secret_key( PKT_secret_key *sk, int n )
{ {
int rc = G10ERR_BAD_PASS; int rc = gpg_error (GPG_ERR_BAD_PASSPHRASE);
int i,mode; int i,mode;
if(n<0) if(n<0)
@ -236,7 +264,7 @@ check_secret_key( PKT_secret_key *sk, int n )
if( n < 1 ) if( n < 1 )
n = (opt.batch && !opt.use_agent)? 1 : 3; /* use the default value */ n = (opt.batch && !opt.use_agent)? 1 : 3; /* use the default value */
for(i=0; i < n && rc == G10ERR_BAD_PASS; i++ ) { for(i=0; i < n && gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE; i++ ) {
int canceled = 0; int canceled = 0;
const char *tryagain = NULL; const char *tryagain = NULL;
if (i) { if (i) {
@ -244,7 +272,8 @@ check_secret_key( PKT_secret_key *sk, int n )
log_info (_("%s ...\n"), _(tryagain)); log_info (_("%s ...\n"), _(tryagain));
} }
rc = do_check( sk, tryagain, mode, &canceled ); rc = do_check( sk, tryagain, mode, &canceled );
if( rc == G10ERR_BAD_PASS && is_status_enabled() ) { if( gpg_err_code (rc) == GPG_ERR_BAD_PASSPHRASE
&& is_status_enabled() ) {
u32 kid[2]; u32 kid[2];
char buf[50]; char buf[50];
@ -291,21 +320,31 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek )
if( !sk->is_protected ) { /* okay, apply the protection */ if( !sk->is_protected ) { /* okay, apply the protection */
CIPHER_HANDLE cipher_hd=NULL; CIPHER_HANDLE cipher_hd=NULL;
if( check_cipher_algo( sk->protect.algo ) ) if( openpgp_cipher_test_algo( sk->protect.algo ) )
rc = G10ERR_CIPHER_ALGO; /* unsupport protection algorithm */ {
rc = gpg_error (GPG_ERR_CIPHER_ALGO); /* unsupport
protection
algorithm */
}
else { else {
print_cipher_algo_note( sk->protect.algo ); print_cipher_algo_note( sk->protect.algo );
cipher_hd = cipher_open( sk->protect.algo, rc = gcry_cipher_open (&cipher_hd, sk->protect.algo,
CIPHER_MODE_AUTO_CFB, 1 ); GCRY_CIPHER_MODE_CFB,
if( cipher_setkey( cipher_hd, dek->key, dek->keylen ) ) GCRY_CIPHER_SECURE
| (sk->protect.algo >= 100 ?
0 : GCRY_CIPHER_ENABLE_SYNC) );
if (rc)
BUG();
if( gcry_cipher_setkey( cipher_hd, dek->key, dek->keylen ) )
log_info(_("WARNING: Weak key detected" log_info(_("WARNING: Weak key detected"
" - please change passphrase again.\n")); " - please change passphrase again.\n"));
sk->protect.ivlen = cipher_get_blocksize( sk->protect.algo ); sk->protect.ivlen = gcry_cipher_get_algo_blklen(sk->protect.algo);
assert( sk->protect.ivlen <= DIM(sk->protect.iv) ); assert( sk->protect.ivlen <= DIM(sk->protect.iv) );
if( sk->protect.ivlen != 8 && sk->protect.ivlen != 16 ) if( sk->protect.ivlen != 8 && sk->protect.ivlen != 16 )
BUG(); /* yes, we are very careful */ BUG(); /* yes, we are very careful */
randomize_buffer(sk->protect.iv, sk->protect.ivlen, 1); gcry_randomize (sk->protect.iv, sk->protect.ivlen,
cipher_setiv( cipher_hd, sk->protect.iv, sk->protect.ivlen ); GCRY_STRONG_RANDOM);
gcry_cipher_setiv( cipher_hd, sk->protect.iv, sk->protect.ivlen );
if( sk->version >= 4 ) { if( sk->version >= 4 ) {
byte *bufarr[PUBKEY_MAX_NSKEY]; byte *bufarr[PUBKEY_MAX_NSKEY];
unsigned narr[PUBKEY_MAX_NSKEY]; unsigned narr[PUBKEY_MAX_NSKEY];
@ -315,16 +354,21 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek )
for(j=0, i = pubkey_get_npkey(sk->pubkey_algo); for(j=0, i = pubkey_get_npkey(sk->pubkey_algo);
i < pubkey_get_nskey(sk->pubkey_algo); i++, j++ ) { i < pubkey_get_nskey(sk->pubkey_algo); i++, j++ ) {
assert( !mpi_is_opaque( sk->skey[i] ) ); assert( !gcry_mpi_get_flag( sk->skey[i],
bufarr[j] = mpi_get_buffer( sk->skey[i], &narr[j], NULL ); GCRYMPI_FLAG_OPAQUE ));
nbits[j] = mpi_get_nbits( sk->skey[i] );
if( gcry_mpi_aprint( GCRYMPI_FMT_USG, (void**)bufarr+j,
narr+j, sk->skey[i]))
BUG();
nbits[j] = gcry_mpi_get_nbits( sk->skey[i] );
ndata += narr[j] + 2; ndata += narr[j] + 2;
} }
for( ; j < PUBKEY_MAX_NSKEY; j++ ) for( ; j < PUBKEY_MAX_NSKEY; j++ )
bufarr[j] = NULL; bufarr[j] = NULL;
ndata += opt.simple_sk_checksum? 2 : 20; /* for checksum */ ndata += opt.simple_sk_checksum? 2 : 20; /* for checksum */
data = m_alloc_secure( ndata ); data = xmalloc_secure ( ndata );
p = data; p = data;
for(j=0; j < PUBKEY_MAX_NSKEY && bufarr[j]; j++ ) { for(j=0; j < PUBKEY_MAX_NSKEY && bufarr[j]; j++ ) {
p[0] = nbits[j] >> 8 ; p[0] = nbits[j] >> 8 ;
@ -332,7 +376,7 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek )
p += 2; p += 2;
memcpy(p, bufarr[j], narr[j] ); memcpy(p, bufarr[j], narr[j] );
p += narr[j]; p += narr[j];
m_free(bufarr[j]); xfree (bufarr[j]);
} }
if (opt.simple_sk_checksum) { if (opt.simple_sk_checksum) {
@ -345,27 +389,28 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek )
sk->protect.sha1chk = 0; sk->protect.sha1chk = 0;
} }
else { else {
MD_HANDLE h = md_open (DIGEST_ALGO_SHA1, 1); gcry_md_hd_t h;
if (!h)
if (gcry_md_open (&h, GCRY_MD_SHA1, 1))
BUG(); /* algo not available */ BUG(); /* algo not available */
md_write (h, data, ndata - 20); gcry_md_write (h, data, ndata - 20);
md_final (h); gcry_md_final (h);
memcpy (p, md_read (h, DIGEST_ALGO_SHA1), 20); memcpy (p, gcry_md_read (h, GCRY_MD_SHA1), 20);
p += 20; p += 20;
md_close (h); gcry_md_close (h);
sk->csum = csum = 0; sk->csum = csum = 0;
sk->protect.sha1chk = 1; sk->protect.sha1chk = 1;
} }
assert( p == data+ndata ); assert( p == data+ndata );
cipher_encrypt( cipher_hd, data, data, ndata ); gcry_cipher_encrypt( cipher_hd, data, ndata, NULL, 0 );
for(i = pubkey_get_npkey(sk->pubkey_algo); for(i = pubkey_get_npkey(sk->pubkey_algo);
i < pubkey_get_nskey(sk->pubkey_algo); i++ ) { i < pubkey_get_nskey(sk->pubkey_algo); i++ ) {
mpi_free( sk->skey[i] ); gcry_mpi_release ( sk->skey[i] );
sk->skey[i] = NULL; sk->skey[i] = NULL;
} }
i = pubkey_get_npkey(sk->pubkey_algo); i = pubkey_get_npkey(sk->pubkey_algo);
sk->skey[i] = mpi_set_opaque(NULL, data, ndata ); sk->skey[i] = gcry_mpi_set_opaque(NULL, data, ndata*8);
} }
else { else {
csum = 0; csum = 0;
@ -375,26 +420,30 @@ protect_secret_key( PKT_secret_key *sk, DEK *dek )
unsigned int nbits; unsigned int nbits;
csum += checksum_mpi (sk->skey[i]); csum += checksum_mpi (sk->skey[i]);
buffer = mpi_get_buffer( sk->skey[i], &nbytes, NULL ); if( gcry_mpi_aprint( GCRYMPI_FMT_USG, &buffer,
cipher_sync (cipher_hd); &nbytes, sk->skey[i] ) )
assert ( !mpi_is_opaque (sk->skey[i]) ); BUG();
data = m_alloc (nbytes+2); gcry_cipher_sync (cipher_hd);
nbits = mpi_get_nbits (sk->skey[i]); assert (!gcry_mpi_get_flag( sk->skey[i],
GCRYMPI_FLAG_OPAQUE ));
data = xmalloc (nbytes+2);
nbits = gcry_mpi_get_nbits (sk->skey[i]);
assert (nbytes == (nbits + 7)/8); assert (nbytes == (nbits + 7)/8);
data[0] = nbits >> 8; data[0] = nbits >> 8;
data[1] = nbits; data[1] = nbits;
cipher_encrypt (cipher_hd, data+2, buffer, nbytes); gcry_cipher_encrypt (cipher_hd, data+2, nbytes,
m_free( buffer ); buffer, nbytes);
xfree ( buffer );
mpi_free (sk->skey[i]); gcry_mpi_release (sk->skey[i]);
sk->skey[i] = mpi_set_opaque (NULL, data, nbytes+2); sk->skey[i] = gcry_mpi_set_opaque (NULL, data,
(nbytes+2)*8);
} }
sk->csum = csum; sk->csum = csum;
} }
sk->is_protected = 1; sk->is_protected = 1;
cipher_close( cipher_hd ); gcry_cipher_close( cipher_hd );
} }
} }
return rc; return rc;
} }

View File

@ -1,5 +1,5 @@
/* seskey.c - make sesssion keys etc. /* seskey.c - make sesssion keys etc.
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -23,6 +23,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "cipher.h" #include "cipher.h"
#include "mpi.h" #include "mpi.h"
@ -36,26 +38,35 @@
void void
make_session_key( DEK *dek ) make_session_key( DEK *dek )
{ {
CIPHER_HANDLE chd; gcry_cipher_hd_t chd;
int i, rc; int i, rc;
dek->keylen = cipher_get_keylen( dek->algo ) / 8; dek->keylen = gcry_cipher_get_algo_keylen (dek->algo);
chd = cipher_open( dek->algo, CIPHER_MODE_AUTO_CFB, 1 ); if (gcry_cipher_open (&chd, dek->algo, GCRY_CIPHER_MODE_CFB,
randomize_buffer( dek->key, dek->keylen, 1 ); (GCRY_CIPHER_SECURE
for(i=0; i < 16; i++ ) { | (dek->algo >= 100 ?
rc = cipher_setkey( chd, dek->key, dek->keylen ); 0 : GCRY_CIPHER_ENABLE_SYNC))) )
if( !rc ) { BUG();
cipher_close( chd );
return; gcry_randomize (dek->key, dek->keylen, GCRY_STRONG_RANDOM );
} for (i=0; i < 16; i++ )
log_info(_("weak key created - retrying\n") ); {
/* Renew the session key until we get a non-weak key. */ rc = gcry_cipher_setkey (chd, dek->key, dek->keylen);
randomize_buffer( dek->key, dek->keylen, 1 ); if (!rc)
{
gcry_cipher_close (chd);
return;
}
if (gpg_err_code (rc) != GPG_ERR_WEAK_KEY)
BUG();
log_info (_("weak key created - retrying\n") );
/* Renew the session key until we get a non-weak key. */
gcry_randomize (dek->key, dek->keylen, GCRY_STRONG_RANDOM );
} }
log_fatal(_(
"cannot avoid weak key for symmetric cipher; tried %d times!\n"), log_fatal (_("cannot avoid weak key for symmetric cipher; "
i); "tried %d times!\n"), i);
} }
@ -64,15 +75,15 @@ make_session_key( DEK *dek )
* for packing the session key. * for packing the session key.
* returns: A mpi with the session key (caller must free) * returns: A mpi with the session key (caller must free)
*/ */
MPI gcry_mpi_t
encode_session_key( DEK *dek, unsigned nbits ) encode_session_key (DEK *dek, unsigned int nbits)
{ {
int nframe = (nbits+7) / 8; int nframe = (nbits+7) / 8;
byte *p; byte *p;
byte *frame; byte *frame;
int i,n; int i,n;
u16 csum; u16 csum;
MPI a; gcry_mpi_t a;
/* the current limitation is that we can only use a session key /* the current limitation is that we can only use a session key
* whose length is a multiple of BITS_PER_MPI_LIMB * whose length is a multiple of BITS_PER_MPI_LIMB
@ -99,13 +110,13 @@ encode_session_key( DEK *dek, unsigned nbits )
for( p = dek->key, i=0; i < dek->keylen; i++ ) for( p = dek->key, i=0; i < dek->keylen; i++ )
csum += *p++; csum += *p++;
frame = m_alloc_secure( nframe ); frame = gcry_xmalloc_secure ( nframe );
n = 0; n = 0;
frame[n++] = 0; frame[n++] = 0;
frame[n++] = 2; frame[n++] = 2;
i = nframe - 6 - dek->keylen; i = nframe - 6 - dek->keylen;
assert( i > 0 ); assert( i > 0 );
p = get_random_bits( i*8, 1, 1 ); p = gcry_random_bytes_secure (i, GCRY_STRONG_RANDOM);
/* replace zero bytes by new values */ /* replace zero bytes by new values */
for(;;) { for(;;) {
int j, k; int j, k;
@ -118,14 +129,14 @@ encode_session_key( DEK *dek, unsigned nbits )
if( !k ) if( !k )
break; /* okay: no zero bytes */ break; /* okay: no zero bytes */
k += k/128; /* better get some more */ k += k/128; /* better get some more */
pp = get_random_bits( k*8, 1, 1); pp = gcry_random_bytes_secure( k, GCRY_STRONG_RANDOM);
for(j=0; j < i && k ; j++ ) for(j=0; j < i && k ; j++ )
if( !p[j] ) if( !p[j] )
p[j] = pp[--k]; p[j] = pp[--k];
m_free(pp); xfree (pp);
} }
memcpy( frame+n, p, i ); memcpy( frame+n, p, i );
m_free(p); xfree (p);
n += i; n += i;
frame[n++] = 0; frame[n++] = 0;
frame[n++] = dek->algo; frame[n++] = dek->algo;
@ -133,21 +144,21 @@ encode_session_key( DEK *dek, unsigned nbits )
frame[n++] = csum >>8; frame[n++] = csum >>8;
frame[n++] = csum; frame[n++] = csum;
assert( n == nframe ); assert( n == nframe );
a = mpi_alloc_secure( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ); if (gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, &nframe))
mpi_set_buffer( a, frame, nframe, 0 ); BUG();
m_free(frame); xfree (frame);
return a; return a;
} }
static MPI static gcry_mpi_t
do_encode_md( MD_HANDLE md, int algo, size_t len, unsigned nbits, do_encode_md( gcry_md_hd_t md, int algo, size_t len, unsigned nbits,
const byte *asn, size_t asnlen, int v3compathack ) const byte *asn, size_t asnlen, int v3compathack )
{ {
int nframe = (nbits+7) / 8; int nframe = (nbits+7) / 8;
byte *frame; byte *frame;
int i,n; int i,n;
MPI a; gcry_mpi_t a;
if( len + asnlen + 4 > nframe ) if( len + asnlen + 4 > nframe )
log_bug("can't encode a %d bit MD into a %d bits frame\n", log_bug("can't encode a %d bit MD into a %d bits frame\n",
@ -159,7 +170,7 @@ do_encode_md( MD_HANDLE md, int algo, size_t len, unsigned nbits,
* *
* PAD consists of FF bytes. * PAD consists of FF bytes.
*/ */
frame = md_is_secure(md)? m_alloc_secure( nframe ) : m_alloc( nframe ); frame = gcry_md_is_secure (md)? xmalloc_secure (nframe): xmalloc (nframe);
n = 0; n = 0;
frame[n++] = 0; frame[n++] = 0;
frame[n++] = v3compathack? algo : 1; /* block type */ frame[n++] = v3compathack? algo : 1; /* block type */
@ -168,13 +179,11 @@ do_encode_md( MD_HANDLE md, int algo, size_t len, unsigned nbits,
memset( frame+n, 0xff, i ); n += i; memset( frame+n, 0xff, i ); n += i;
frame[n++] = 0; frame[n++] = 0;
memcpy( frame+n, asn, asnlen ); n += asnlen; memcpy( frame+n, asn, asnlen ); n += asnlen;
memcpy( frame+n, md_read(md, algo), len ); n += len; memcpy( frame+n, gcry_md_read (md, algo), len ); n += len;
assert( n == nframe ); assert( n == nframe );
a = md_is_secure(md)? if (gcry_mpi_scan( &a, GCRYMPI_FMT_USG, frame, &nframe ))
mpi_alloc_secure( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ) BUG();
: mpi_alloc( (nframe+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ); xfree (frame);
mpi_set_buffer( a, frame, nframe, 0 );
m_free(frame);
return a; return a;
} }
@ -185,33 +194,40 @@ do_encode_md( MD_HANDLE md, int algo, size_t len, unsigned nbits,
* which did put the algo identifier inseatd of the block type 1 into * which did put the algo identifier inseatd of the block type 1 into
* the encoded value. Setting this flag forces the old behaviour. * the encoded value. Setting this flag forces the old behaviour.
*/ */
MPI gcry_mpi_t
encode_md_value( int pubkey_algo, MD_HANDLE md, int hash_algo, encode_md_value (int pubkey_algo, gcry_md_hd_t md, int hash_algo,
unsigned nbits, int v3compathack ) unsigned int nbits, int v3compathack )
{ {
int algo = hash_algo? hash_algo : md_get_algo(md); int algo = hash_algo? hash_algo : gcry_md_get_algo (md);
const byte *asn; gcry_mpi_t frame;
size_t asnlen, mdlen;
MPI frame; if (pubkey_algo == GCRY_PK_DSA)
{
if( pubkey_algo == PUBKEY_ALGO_DSA ) { size_t n = gcry_md_get_algo_dlen(hash_algo);
mdlen = md_digest_length (hash_algo); if (n != 20)
if (mdlen != 20) { {
log_error (_("DSA requires the use of a 160 bit hash algorithm\n")); log_error (_("DSA requires the use of a 160 bit hash algorithm\n"));
return NULL; return NULL;
} }
if (gcry_mpi_scan( &frame, GCRYMPI_FMT_USG,
frame = md_is_secure(md)? mpi_alloc_secure((md_digest_length(hash_algo) gcry_md_read (md, hash_algo), &n ) )
+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB ) BUG();
: mpi_alloc((md_digest_length(hash_algo)
+BYTES_PER_MPI_LIMB-1) / BYTES_PER_MPI_LIMB );
mpi_set_buffer( frame, md_read(md, hash_algo),
md_digest_length(hash_algo), 0 );
} }
else { else
asn = md_asn_oid( algo, &asnlen, &mdlen ); {
frame = do_encode_md( md, algo, mdlen, nbits, asn, asnlen, v3compathack); byte *asn;
size_t asnlen;
if( gcry_md_algo_info( algo, GCRYCTL_GET_ASNOID, NULL, &asnlen ) )
log_fatal("can't get OID of algo %d: %s\n",
algo, gcry_strerror(-1));
asn = xmalloc (asnlen);
if( gcry_md_algo_info( algo, GCRYCTL_GET_ASNOID, asn, &asnlen ) )
BUG();
frame = do_encode_md( md, algo, gcry_md_get_algo_dlen( algo ),
nbits, asn, asnlen, v3compathack );
xfree (asn);
} }
return frame; return frame;
} }

View File

@ -1,5 +1,6 @@
/* sig-check.c - Check a signature /* sig-check.c - Check a signature
* Copyright (C) 1998, 1999, 2000, 2001, 2002 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2002,
* 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -23,6 +24,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "util.h" #include "util.h"
#include "packet.h" #include "packet.h"
#include "memory.h" #include "memory.h"
@ -33,6 +36,7 @@
#include "status.h" #include "status.h"
#include "i18n.h" #include "i18n.h"
#include "options.h" #include "options.h"
#include "pkglue.h"
struct cmp_help_context_s { struct cmp_help_context_s {
PKT_signature *sig; PKT_signature *sig;
@ -59,7 +63,7 @@ int
signature_check2( PKT_signature *sig, MD_HANDLE digest, signature_check2( PKT_signature *sig, MD_HANDLE digest,
u32 *r_expiredate, int *r_expired ) u32 *r_expiredate, int *r_expired )
{ {
PKT_public_key *pk = m_alloc_clear( sizeof *pk ); PKT_public_key *pk = xcalloc (1, sizeof *pk );
int rc=0; int rc=0;
*r_expiredate = 0; *r_expiredate = 0;
@ -69,14 +73,14 @@ signature_check2( PKT_signature *sig, MD_HANDLE digest,
not match the actual sig, and also if the clearsign "Hash:" not match the actual sig, and also if the clearsign "Hash:"
header is missing or does not match the actual sig. */ header is missing or does not match the actual sig. */
if(!md_algo_present(digest,sig->digest_algo)) { if(!gcry_md_is_enabled (digest,sig->digest_algo)) {
log_info(_("WARNING: signature digest conflict in message\n")); log_info(_("WARNING: signature digest conflict in message\n"));
rc=G10ERR_GENERAL; rc=GPG_ERR_GENERAL;
} }
else if( get_pubkey( pk, sig->keyid ) ) else if( get_pubkey( pk, sig->keyid ) )
rc = G10ERR_NO_PUBKEY; rc = GPG_ERR_NO_PUBKEY;
else if(!pk->is_valid && !pk->is_primary) else if(!pk->is_valid && !pk->is_primary)
rc=G10ERR_BAD_PUBKEY; /* you cannot have a good sig from an rc=GPG_ERR_BAD_PUBKEY; /* you cannot have a good sig from an
invalid subkey */ invalid subkey */
else { else {
*r_expiredate = pk->expiredate; *r_expiredate = pk->expiredate;
@ -93,116 +97,43 @@ signature_check2( PKT_signature *sig, MD_HANDLE digest,
* not possible to sign more than one identical document within * not possible to sign more than one identical document within
* one second. Some remote batch processing applications might * one second. Some remote batch processing applications might
* like this feature here */ * like this feature here */
MD_HANDLE md; gcry_md_hd_t md;
u32 a = sig->timestamp; u32 a = sig->timestamp;
int i, nsig = pubkey_get_nsig( sig->pubkey_algo ); int i, nsig = pubkey_get_nsig( sig->pubkey_algo );
byte *p, *buffer; byte *p, *buffer;
md = md_open( DIGEST_ALGO_RMD160, 0); gcry_md_open (&md, GCRY_MD_RMD160, 0);
md_putc( digest, sig->pubkey_algo ); gcry_md_putc( digest, sig->pubkey_algo );
md_putc( digest, sig->digest_algo ); gcry_md_putc( digest, sig->digest_algo );
md_putc( digest, (a >> 24) & 0xff ); gcry_md_putc( digest, (a >> 24) & 0xff );
md_putc( digest, (a >> 16) & 0xff ); gcry_md_putc( digest, (a >> 16) & 0xff );
md_putc( digest, (a >> 8) & 0xff ); gcry_md_putc( digest, (a >> 8) & 0xff );
md_putc( digest, a & 0xff ); gcry_md_putc( digest, a & 0xff );
for(i=0; i < nsig; i++ ) { for(i=0; i < nsig; i++ ) {
unsigned n = mpi_get_nbits( sig->data[i]); size_t n;
void *tmp;
md_putc( md, n>>8); if (gcry_mpi_aprint (GCRYMPI_FMT_USG, &tmp, &n, sig->data[i]))
md_putc( md, n ); BUG();
p = mpi_get_buffer( sig->data[i], &n, NULL );
md_write( md, p, n ); gcry_md_write (md, tmp, n);
m_free(p); xfree (tmp);
} }
md_final( md ); gcry_md_final( md );
p = make_radix64_string( md_read( md, 0 ), 20 ); p = make_radix64_string( gcry_md_read( md, 0 ), 20 );
buffer = m_alloc( strlen(p) + 60 ); buffer = xmalloc ( strlen(p) + 60 );
sprintf( buffer, "%s %s %lu", sprintf( buffer, "%s %s %lu",
p, strtimestamp( sig->timestamp ), (ulong)sig->timestamp ); p, strtimestamp( sig->timestamp ), (ulong)sig->timestamp );
write_status_text( STATUS_SIG_ID, buffer ); write_status_text( STATUS_SIG_ID, buffer );
m_free(buffer); xfree (buffer);
m_free(p); xfree (p);
md_close(md); gcry_md_close(md);
} }
return rc; return rc;
} }
/****************
* This function gets called by pubkey_verify() if the algorithm needs it.
*/
static int
cmp_help( void *opaque, MPI result )
{
#if 0 /* we do not use this anymore */
int rc=0, i, j, c, old_enc;
byte *dp;
const byte *asn;
size_t mdlen, asnlen;
struct cmp_help_context_s *ctx = opaque;
PKT_signature *sig = ctx->sig;
MD_HANDLE digest = ctx->md;
old_enc = 0;
for(i=j=0; (c=mpi_getbyte(result, i)) != -1; i++ ) {
if( !j ) {
if( !i && c != 1 )
break;
else if( i && c == 0xff )
; /* skip the padding */
else if( i && !c )
j++;
else
break;
}
else if( ++j == 18 && c != 1 )
break;
else if( j == 19 && c == 0 ) {
old_enc++;
break;
}
}
if( old_enc ) {
log_error("old encoding scheme is not supported\n");
return G10ERR_GENERAL;
}
if( (rc=check_digest_algo(sig->digest_algo)) )
return rc; /* unsupported algo */
asn = md_asn_oid( sig->digest_algo, &asnlen, &mdlen );
for(i=mdlen,j=asnlen-1; (c=mpi_getbyte(result, i)) != -1 && j >= 0;
i++, j-- )
if( asn[j] != c )
break;
if( j != -1 || mpi_getbyte(result, i) )
return G10ERR_BAD_PUBKEY; /* ASN is wrong */
for(i++; (c=mpi_getbyte(result, i)) != -1; i++ )
if( c != 0xff )
break;
i++;
if( c != sig->digest_algo || mpi_getbyte(result, i) ) {
/* Padding or leading bytes in signature is wrong */
return G10ERR_BAD_PUBKEY;
}
if( mpi_getbyte(result, mdlen-1) != sig->digest_start[0]
|| mpi_getbyte(result, mdlen-2) != sig->digest_start[1] ) {
/* Wrong key used to check the signature */
return G10ERR_BAD_PUBKEY;
}
dp = md_read( digest, sig->digest_algo );
for(i=mdlen-1; i >= 0; i--, dp++ ) {
if( mpi_getbyte( result, i ) != *dp )
return G10ERR_BAD_SIGN;
}
return 0;
#else
return -1;
#endif
}
static int static int
do_check_messages( PKT_public_key *pk, PKT_signature *sig, int *r_expired ) do_check_messages( PKT_public_key *pk, PKT_signature *sig, int *r_expired )
{ {
@ -213,7 +144,7 @@ do_check_messages( PKT_public_key *pk, PKT_signature *sig, int *r_expired )
log_info(_("key %08lX: this is a PGP generated " log_info(_("key %08lX: this is a PGP generated "
"ElGamal key which is NOT secure for signatures!\n"), "ElGamal key which is NOT secure for signatures!\n"),
(ulong)keyid_from_pk(pk,NULL)); (ulong)keyid_from_pk(pk,NULL));
return G10ERR_PUBKEY_ALGO; return GPG_ERR_PUBKEY_ALGO;
} }
if( pk->timestamp > sig->timestamp ) { if( pk->timestamp > sig->timestamp ) {
@ -223,7 +154,7 @@ do_check_messages( PKT_public_key *pk, PKT_signature *sig, int *r_expired )
: _("public key %08lX is %lu seconds newer than the signature\n"), : _("public key %08lX is %lu seconds newer than the signature\n"),
(ulong)keyid_from_pk(pk,NULL),d ); (ulong)keyid_from_pk(pk,NULL),d );
if( !opt.ignore_time_conflict ) if( !opt.ignore_time_conflict )
return G10ERR_TIME_CONFLICT; /* pubkey newer than signature */ return GPG_ERR_TIME_CONFLICT; /* pubkey newer than signature */
} }
cur_time = make_timestamp(); cur_time = make_timestamp();
@ -235,7 +166,7 @@ do_check_messages( PKT_public_key *pk, PKT_signature *sig, int *r_expired )
"in future (time warp or clock problem)\n"), "in future (time warp or clock problem)\n"),
(ulong)keyid_from_pk(pk,NULL),d ); (ulong)keyid_from_pk(pk,NULL),d );
if( !opt.ignore_time_conflict ) if( !opt.ignore_time_conflict )
return G10ERR_TIME_CONFLICT; return GPG_ERR_TIME_CONFLICT;
} }
if( pk->expiredate && pk->expiredate < cur_time ) { if( pk->expiredate && pk->expiredate < cur_time ) {
@ -262,48 +193,49 @@ static int
do_check( PKT_public_key *pk, PKT_signature *sig, MD_HANDLE digest, do_check( PKT_public_key *pk, PKT_signature *sig, MD_HANDLE digest,
int *r_expired ) int *r_expired )
{ {
MPI result = NULL; gcry_mpi_t result = NULL;
int rc=0; int rc=0;
struct cmp_help_context_s ctx; struct cmp_help_context_s ctx;
if( (rc=do_check_messages(pk,sig,r_expired)) ) if( (rc=do_check_messages(pk,sig,r_expired)) )
return rc; return rc;
if( (rc=check_digest_algo(sig->digest_algo)) ) if( (rc=gcry_md_test_algo(sig->digest_algo)) )
return rc; return rc;
if( (rc=check_pubkey_algo(sig->pubkey_algo)) ) if( (rc=gcry_pk_test_algo(sig->pubkey_algo)) )
return rc; return rc;
/* make sure the digest algo is enabled (in case of a detached signature)*/ /* make sure the digest algo is enabled (in case of a detached
md_enable( digest, sig->digest_algo ); signature)*/
gcry_md_enable( digest, sig->digest_algo );
/* complete the digest */ /* complete the digest */
if( sig->version >= 4 ) if( sig->version >= 4 )
md_putc( digest, sig->version ); gcry_md_putc( digest, sig->version );
md_putc( digest, sig->sig_class ); gcry_md_putc( digest, sig->sig_class );
if( sig->version < 4 ) { if( sig->version < 4 ) {
u32 a = sig->timestamp; u32 a = sig->timestamp;
md_putc( digest, (a >> 24) & 0xff ); gcry_md_putc( digest, (a >> 24) & 0xff );
md_putc( digest, (a >> 16) & 0xff ); gcry_md_putc( digest, (a >> 16) & 0xff );
md_putc( digest, (a >> 8) & 0xff ); gcry_md_putc( digest, (a >> 8) & 0xff );
md_putc( digest, a & 0xff ); gcry_md_putc( digest, a & 0xff );
} }
else { else {
byte buf[6]; byte buf[6];
size_t n; size_t n;
md_putc( digest, sig->pubkey_algo ); gcry_md_putc( digest, sig->pubkey_algo );
md_putc( digest, sig->digest_algo ); gcry_md_putc( digest, sig->digest_algo );
if( sig->hashed ) { if( sig->hashed ) {
n = sig->hashed->len; n = sig->hashed->len;
md_putc (digest, (n >> 8) ); gcry_md_putc (digest, (n >> 8) );
md_putc (digest, n ); gcry_md_putc (digest, n );
md_write (digest, sig->hashed->data, n); gcry_md_write (digest, sig->hashed->data, n);
n += 6; n += 6;
} }
else { else {
/* Two octets for the (empty) length of the hashed /* Two octets for the (empty) length of the hashed
section. */ section. */
md_putc (digest, 0); gcry_md_putc (digest, 0);
md_putc (digest, 0); gcry_md_putc (digest, 0);
n = 6; n = 6;
} }
/* add some magic */ /* add some magic */
@ -313,38 +245,39 @@ do_check( PKT_public_key *pk, PKT_signature *sig, MD_HANDLE digest,
buf[3] = n >> 16; buf[3] = n >> 16;
buf[4] = n >> 8; buf[4] = n >> 8;
buf[5] = n; buf[5] = n;
md_write( digest, buf, 6 ); gcry_md_write( digest, buf, 6 );
} }
md_final( digest ); gcry_md_final (digest);
result = encode_md_value( pk->pubkey_algo, digest, sig->digest_algo, result = encode_md_value( pk->pubkey_algo, digest, sig->digest_algo,
mpi_get_nbits(pk->pkey[0]), 0 ); mpi_get_nbits(pk->pkey[0]), 0 );
if (!result) if (!result)
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
ctx.sig = sig; ctx.sig = sig;
ctx.md = digest; ctx.md = digest;
rc = pubkey_verify( pk->pubkey_algo, result, sig->data, pk->pkey, rc = pk_verify ( pk->pubkey_algo, result, sig->data, pk->pkey);
cmp_help, &ctx ); gcry_mpi_release ( result );
mpi_free( result );
if( (opt.emulate_bugs & EMUBUG_MDENCODE) if( (opt.emulate_bugs & EMUBUG_MDENCODE)
&& rc == G10ERR_BAD_SIGN && is_ELGAMAL(pk->pubkey_algo) ) { && gpg_err_code (rc) == GPG_ERR_BAD_SIGNATURE
&& is_ELGAMAL(pk->pubkey_algo) ) {
/* In this case we try again because old GnuPG versions didn't encode /* In this case we try again because old GnuPG versions didn't encode
* the hash right. There is no problem with DSA however */ * the hash right. There is no problem with DSA however */
result = encode_md_value( pk->pubkey_algo, digest, sig->digest_algo, result = encode_md_value( pk->pubkey_algo, digest, sig->digest_algo,
mpi_get_nbits(pk->pkey[0]), (sig->version < 5) ); mpi_get_nbits(pk->pkey[0]), (sig->version < 5) );
if (!result) if (!result)
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
else { else {
ctx.sig = sig; ctx.sig = sig;
ctx.md = digest; ctx.md = digest;
rc = pubkey_verify( pk->pubkey_algo, result, sig->data, pk->pkey, rc = pk_verify (pk->pubkey_algo, result, sig->data, pk->pkey);
cmp_help, &ctx );
} }
} }
if( !rc && sig->flags.unknown_critical ) { if( !rc && sig->flags.unknown_critical ) {
log_info(_("assuming bad signature from key %08lX due to an unknown critical bit\n"),(ulong)keyid_from_pk(pk,NULL)); log_info(_("assuming bad signature from key %08lX "
rc = G10ERR_BAD_SIGN; "due to an unknown critical bit\n"),
(ulong)keyid_from_pk(pk,NULL));
rc = gpg_error (GPG_ERR_BAD_SIGNATURE);
} }
return rc; return rc;
@ -365,9 +298,9 @@ hash_uid_node( KBNODE unode, MD_HANDLE md, PKT_signature *sig )
buf[2] = uid->attrib_len >> 16; buf[2] = uid->attrib_len >> 16;
buf[3] = uid->attrib_len >> 8; buf[3] = uid->attrib_len >> 8;
buf[4] = uid->attrib_len; buf[4] = uid->attrib_len;
md_write( md, buf, 5 ); gcry_md_write( md, buf, 5 );
} }
md_write( md, uid->attrib_data, uid->attrib_len ); gcry_md_write( md, uid->attrib_data, uid->attrib_len );
} }
else { else {
if( sig->version >=4 ) { if( sig->version >=4 ) {
@ -377,9 +310,9 @@ hash_uid_node( KBNODE unode, MD_HANDLE md, PKT_signature *sig )
buf[2] = uid->len >> 16; buf[2] = uid->len >> 16;
buf[3] = uid->len >> 8; buf[3] = uid->len >> 8;
buf[4] = uid->len; buf[4] = uid->len;
md_write( md, buf, 5 ); gcry_md_write( md, buf, 5 );
} }
md_write( md, uid->name, uid->len ); gcry_md_write( md, uid->name, uid->len );
} }
} }
@ -390,7 +323,7 @@ cache_sig_result ( PKT_signature *sig, int result )
sig->flags.checked = 1; sig->flags.checked = 1;
sig->flags.valid = 1; sig->flags.valid = 1;
} }
else if ( result == G10ERR_BAD_SIGN ) { else if ( gpg_err_code (result) == GPG_ERR_BAD_SIGNATURE ) {
sig->flags.checked = 1; sig->flags.checked = 1;
sig->flags.valid = 0; sig->flags.valid = 0;
} }
@ -418,7 +351,7 @@ int
check_revocation_keys(PKT_public_key *pk,PKT_signature *sig) check_revocation_keys(PKT_public_key *pk,PKT_signature *sig)
{ {
static int busy=0; static int busy=0;
int i,rc=G10ERR_GENERAL; int i,rc=GPG_ERR_GENERAL;
assert(IS_KEY_REV(sig)); assert(IS_KEY_REV(sig));
assert((sig->keyid[0]!=pk->keyid[0]) || (sig->keyid[0]!=pk->keyid[1])); assert((sig->keyid[0]!=pk->keyid[0]) || (sig->keyid[0]!=pk->keyid[1]));
@ -450,9 +383,9 @@ check_revocation_keys(PKT_public_key *pk,PKT_signature *sig)
if(keyid[0]==sig->keyid[0] && keyid[1]==sig->keyid[1]) if(keyid[0]==sig->keyid[0] && keyid[1]==sig->keyid[1])
{ {
MD_HANDLE md; gcry_md_hd_t md;
md=md_open(sig->digest_algo,0); gcry_md_open (&md, sig->digest_algo,0);
hash_public_key(md,pk); hash_public_key(md,pk);
rc=signature_check(sig,md); rc=signature_check(sig,md);
cache_sig_result(sig,rc); cache_sig_result(sig,rc);
@ -513,12 +446,12 @@ check_key_signature2( KBNODE root, KBNODE node, PKT_public_key *check_pk,
} }
if((rc=do_check_messages(pk,sig,r_expired))) if((rc=do_check_messages(pk,sig,r_expired)))
return rc; return rc;
return sig->flags.valid? 0 : G10ERR_BAD_SIGN; return sig->flags.valid? 0 : gpg_error (GPG_ERR_BAD_SIGNATURE);
} }
} }
if( (rc=check_digest_algo(algo)) ) if( (rc=gcry_md_test_algo(algo)) )
return rc; return rc;
if( sig->sig_class == 0x20 ) { /* key revocation */ if( sig->sig_class == 0x20 ) { /* key revocation */
u32 keyid[2]; u32 keyid[2];
@ -529,30 +462,30 @@ check_key_signature2( KBNODE root, KBNODE node, PKT_public_key *check_pk,
rc=check_revocation_keys(pk,sig); rc=check_revocation_keys(pk,sig);
else else
{ {
md = md_open( algo, 0 ); gcry_md_open (&md, algo, 0 );
hash_public_key( md, pk ); hash_public_key( md, pk );
rc = do_check( pk, sig, md, r_expired ); rc = do_check( pk, sig, md, r_expired );
cache_sig_result ( sig, rc ); cache_sig_result ( sig, rc );
md_close(md); gcry_md_close(md);
} }
} }
else if( sig->sig_class == 0x28 ) { /* subkey revocation */ else if( sig->sig_class == 0x28 ) { /* subkey revocation */
KBNODE snode = find_prev_kbnode( root, node, PKT_PUBLIC_SUBKEY ); KBNODE snode = find_prev_kbnode( root, node, PKT_PUBLIC_SUBKEY );
if( snode ) { if( snode ) {
md = md_open( algo, 0 ); gcry_md_open (&md, algo, 0 );
hash_public_key( md, pk ); hash_public_key( md, pk );
hash_public_key( md, snode->pkt->pkt.public_key ); hash_public_key( md, snode->pkt->pkt.public_key );
rc = do_check( pk, sig, md, r_expired ); rc = do_check( pk, sig, md, r_expired );
cache_sig_result ( sig, rc ); cache_sig_result ( sig, rc );
md_close(md); gcry_md_close(md);
} }
else { else {
if (!opt.quiet) if (!opt.quiet)
log_info (_("key %08lX: no subkey for subkey " log_info (_("key %08lX: no subkey for subkey "
"revocation signature\n"), "revocation signature\n"),
(ulong)keyid_from_pk (pk, NULL)); (ulong)keyid_from_pk (pk, NULL));
rc = G10ERR_SIG_CLASS; rc = GPG_ERR_SIG_CLASS;
} }
} }
else if( sig->sig_class == 0x18 ) { /* key binding */ else if( sig->sig_class == 0x18 ) { /* key binding */
@ -566,27 +499,27 @@ check_key_signature2( KBNODE root, KBNODE node, PKT_public_key *check_pk,
if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] ) if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] )
*is_selfsig = 1; *is_selfsig = 1;
} }
md = md_open( algo, 0 ); gcry_md_open (&md, algo, 0 );
hash_public_key( md, pk ); hash_public_key( md, pk );
hash_public_key( md, snode->pkt->pkt.public_key ); hash_public_key( md, snode->pkt->pkt.public_key );
rc = do_check( pk, sig, md, r_expired ); rc = do_check( pk, sig, md, r_expired );
cache_sig_result ( sig, rc ); cache_sig_result ( sig, rc );
md_close(md); gcry_md_close(md);
} }
else { else {
if (opt.verbose) if (opt.verbose)
log_info(_("key %08lX: no subkey for subkey " log_info(_("key %08lX: no subkey for subkey "
"binding signature\n"), "binding signature\n"),
(ulong)keyid_from_pk (pk, NULL)); (ulong)keyid_from_pk (pk, NULL));
rc = G10ERR_SIG_CLASS; rc = GPG_ERR_SIG_CLASS;
} }
} }
else if( sig->sig_class == 0x1f ) { /* direct key signature */ else if( sig->sig_class == 0x1f ) { /* direct key signature */
md = md_open( algo, 0 ); gcry_md_open (&md, algo, 0 );
hash_public_key( md, pk ); hash_public_key( md, pk );
rc = do_check( pk, sig, md, r_expired ); rc = do_check( pk, sig, md, r_expired );
cache_sig_result ( sig, rc ); cache_sig_result ( sig, rc );
md_close(md); gcry_md_close(md);
} }
else { /* all other classes */ else { /* all other classes */
KBNODE unode = find_prev_kbnode( root, node, PKT_USER_ID ); KBNODE unode = find_prev_kbnode( root, node, PKT_USER_ID );
@ -595,7 +528,7 @@ check_key_signature2( KBNODE root, KBNODE node, PKT_public_key *check_pk,
u32 keyid[2]; u32 keyid[2];
keyid_from_pk( pk, keyid ); keyid_from_pk( pk, keyid );
md = md_open( algo, 0 ); gcry_md_open (&md, algo, 0 );
hash_public_key( md, pk ); hash_public_key( md, pk );
hash_uid_node( unode, md, sig ); hash_uid_node( unode, md, sig );
if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] ) if( keyid[0] == sig->keyid[0] && keyid[1] == sig->keyid[1] )
@ -610,14 +543,14 @@ check_key_signature2( KBNODE root, KBNODE node, PKT_public_key *check_pk,
rc = signature_check2( sig, md, r_expiredate, r_expired ); rc = signature_check2( sig, md, r_expiredate, r_expired );
cache_sig_result ( sig, rc ); cache_sig_result ( sig, rc );
md_close(md); gcry_md_close(md);
} }
else { else {
if (!opt.quiet) if (!opt.quiet)
log_info ("key %08lX: no user ID for key signature packet " log_info ("key %08lX: no user ID for key signature packet "
"of class %02x\n", "of class %02x\n",
(ulong)keyid_from_pk (pk, NULL), sig->sig_class ); (ulong)keyid_from_pk (pk, NULL), sig->sig_class );
rc = G10ERR_SIG_CLASS; rc = GPG_ERR_SIG_CLASS;
} }
} }

View File

@ -40,7 +40,7 @@
#include "trustdb.h" #include "trustdb.h"
#include "status.h" #include "status.h"
#include "i18n.h" #include "i18n.h"
#include "pkglue.h"
#ifdef HAVE_DOSISH_SYSTEM #ifdef HAVE_DOSISH_SYSTEM
#define LF "\r\n" #define LF "\r\n"
@ -103,11 +103,11 @@ mk_notation_and_policy( PKT_signature *sig,
{ {
log_error(_("WARNING: unable to %%-expand notation " log_error(_("WARNING: unable to %%-expand notation "
"(too large). Using unexpanded.\n")); "(too large). Using unexpanded.\n"));
expanded=m_strdup(s); expanded=xstrdup (s);
} }
n2 = strlen(expanded); n2 = strlen(expanded);
buf = m_alloc( 8 + n1 + n2 ); buf = xmalloc ( 8 + n1 + n2 );
buf[0] = 0x80; /* human readable */ buf[0] = 0x80; /* human readable */
buf[1] = buf[2] = buf[3] = 0; buf[1] = buf[2] = buf[3] = 0;
buf[4] = n1 >> 8; buf[4] = n1 >> 8;
@ -119,8 +119,8 @@ mk_notation_and_policy( PKT_signature *sig,
build_sig_subpkt( sig, SIGSUBPKT_NOTATION build_sig_subpkt( sig, SIGSUBPKT_NOTATION
| ((nd->flags & 1)? SIGSUBPKT_FLAG_CRITICAL:0), | ((nd->flags & 1)? SIGSUBPKT_FLAG_CRITICAL:0),
buf, 8+n1+n2 ); buf, 8+n1+n2 );
m_free(expanded); xfree (expanded);
m_free(buf); xfree (buf);
} }
if(opt.list_options&LIST_SHOW_NOTATION) if(opt.list_options&LIST_SHOW_NOTATION)
@ -151,14 +151,14 @@ mk_notation_and_policy( PKT_signature *sig,
{ {
log_error(_("WARNING: unable to %%-expand policy url " log_error(_("WARNING: unable to %%-expand policy url "
"(too large). Using unexpanded.\n")); "(too large). Using unexpanded.\n"));
s=m_strdup(string); s=xstrdup (string);
} }
build_sig_subpkt(sig,SIGSUBPKT_POLICY| build_sig_subpkt(sig,SIGSUBPKT_POLICY|
((pu->flags & 1)?SIGSUBPKT_FLAG_CRITICAL:0), ((pu->flags & 1)?SIGSUBPKT_FLAG_CRITICAL:0),
s,strlen(s)); s,strlen(s));
m_free(s); xfree (s);
} }
if(opt.list_options&LIST_SHOW_POLICY) if(opt.list_options&LIST_SHOW_POLICY)
@ -189,13 +189,13 @@ hash_uid (MD_HANDLE md, int sigversion, const PKT_user_id *uid)
buf[3] = uid->len >> 8; buf[3] = uid->len >> 8;
buf[4] = uid->len; buf[4] = uid->len;
} }
md_write( md, buf, 5 ); gcry_md_write( md, buf, 5 );
} }
if(uid->attrib_data) if(uid->attrib_data)
md_write (md, uid->attrib_data, uid->attrib_len ); gcry_md_write (md, uid->attrib_data, uid->attrib_len );
else else
md_write (md, uid->name, uid->len ); gcry_md_write (md, uid->name, uid->len );
} }
@ -206,31 +206,31 @@ static void
hash_sigversion_to_magic (MD_HANDLE md, const PKT_signature *sig) hash_sigversion_to_magic (MD_HANDLE md, const PKT_signature *sig)
{ {
if (sig->version >= 4) if (sig->version >= 4)
md_putc (md, sig->version); gcry_md_putc (md, sig->version);
md_putc (md, sig->sig_class); gcry_md_putc (md, sig->sig_class);
if (sig->version < 4) { if (sig->version < 4) {
u32 a = sig->timestamp; u32 a = sig->timestamp;
md_putc (md, (a >> 24) & 0xff ); gcry_md_putc (md, (a >> 24) & 0xff );
md_putc (md, (a >> 16) & 0xff ); gcry_md_putc (md, (a >> 16) & 0xff );
md_putc (md, (a >> 8) & 0xff ); gcry_md_putc (md, (a >> 8) & 0xff );
md_putc (md, a & 0xff ); gcry_md_putc (md, a & 0xff );
} }
else { else {
byte buf[6]; byte buf[6];
size_t n; size_t n;
md_putc (md, sig->pubkey_algo); gcry_md_putc (md, sig->pubkey_algo);
md_putc (md, sig->digest_algo); gcry_md_putc (md, sig->digest_algo);
if (sig->hashed) { if (sig->hashed) {
n = sig->hashed->len; n = sig->hashed->len;
md_putc (md, (n >> 8) ); gcry_md_putc (md, (n >> 8) );
md_putc (md, n ); gcry_md_putc (md, n );
md_write (md, sig->hashed->data, n ); gcry_md_write (md, sig->hashed->data, n );
n += 6; n += 6;
} }
else { else {
md_putc (md, 0); /* always hash the length of the subpacket*/ gcry_md_putc (md, 0); /* always hash the length of the subpacket*/
md_putc (md, 0); gcry_md_putc (md, 0);
n = 6; n = 6;
} }
/* add some magic */ /* add some magic */
@ -240,7 +240,7 @@ hash_sigversion_to_magic (MD_HANDLE md, const PKT_signature *sig)
buf[3] = n >> 16; buf[3] = n >> 16;
buf[4] = n >> 8; buf[4] = n >> 8;
buf[5] = n; buf[5] = n;
md_write (md, buf, 6); gcry_md_write (md, buf, 6);
} }
} }
@ -249,7 +249,7 @@ static int
do_sign( PKT_secret_key *sk, PKT_signature *sig, do_sign( PKT_secret_key *sk, PKT_signature *sig,
MD_HANDLE md, int digest_algo ) MD_HANDLE md, int digest_algo )
{ {
MPI frame; gcry_mpi_t frame;
byte *dp; byte *dp;
int rc; int rc;
@ -260,61 +260,60 @@ do_sign( PKT_secret_key *sk, PKT_signature *sig,
: _("key has been created %lu seconds " : _("key has been created %lu seconds "
"in future (time warp or clock problem)\n"), d ); "in future (time warp or clock problem)\n"), d );
if( !opt.ignore_time_conflict ) if( !opt.ignore_time_conflict )
return G10ERR_TIME_CONFLICT; return GPG_ERR_TIME_CONFLICT;
} }
print_pubkey_algo_note(sk->pubkey_algo); print_pubkey_algo_note(sk->pubkey_algo);
if( !digest_algo ) if( !digest_algo )
digest_algo = md_get_algo(md); digest_algo = gcry_md_get_algo(md);
print_digest_algo_note( digest_algo ); print_digest_algo_note( digest_algo );
dp = md_read( md, digest_algo ); dp = gcry_md_read ( md, digest_algo );
sig->digest_algo = digest_algo; sig->digest_algo = digest_algo;
sig->digest_start[0] = dp[0]; sig->digest_start[0] = dp[0];
sig->digest_start[1] = dp[1]; sig->digest_start[1] = dp[1];
frame = encode_md_value( sk->pubkey_algo, md, frame = encode_md_value( sk->pubkey_algo, md,
digest_algo, mpi_get_nbits(sk->skey[0]), 0 ); digest_algo, mpi_get_nbits(sk->skey[0]), 0 );
if (!frame) if (!frame)
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
rc = pubkey_sign( sk->pubkey_algo, sig->data, frame, sk->skey ); rc = pk_sign( sk->pubkey_algo, sig->data, frame, sk->skey );
mpi_free(frame); gcry_mpi_release (frame);
if (!rc && !opt.no_sig_create_check) { if (!rc && !opt.no_sig_create_check) {
/* check that the signature verification worked and nothing is /* check that the signature verification worked and nothing is
* fooling us e.g. by a bug in the signature create * fooling us e.g. by a bug in the signature create
* code or by deliberately introduced faults. */ * code or by deliberately introduced faults. */
PKT_public_key *pk = m_alloc_clear (sizeof *pk); PKT_public_key *pk = xcalloc (1,sizeof *pk);
if( get_pubkey( pk, sig->keyid ) ) if( get_pubkey( pk, sig->keyid ) )
rc = G10ERR_NO_PUBKEY; rc = GPG_ERR_NO_PUBKEY;
else { else {
frame = encode_md_value (pk->pubkey_algo, md, frame = encode_md_value (pk->pubkey_algo, md,
sig->digest_algo, sig->digest_algo,
mpi_get_nbits(pk->pkey[0]), 0); mpi_get_nbits(pk->pkey[0]), 0);
if (!frame) if (!frame)
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
else else
rc = pubkey_verify (pk->pubkey_algo, frame, rc = pk_verify (pk->pubkey_algo, frame,
sig->data, pk->pkey, sig->data, pk->pkey);
NULL, NULL ); gcry_mpi_release (frame);
mpi_free (frame);
} }
if (rc) if (rc)
log_error (_("checking created signature failed: %s\n"), log_error (_("checking created signature failed: %s\n"),
g10_errstr (rc)); gpg_strerror (rc));
free_public_key (pk); free_public_key (pk);
} }
if( rc ) if( rc )
log_error(_("signing failed: %s\n"), g10_errstr(rc) ); log_error(_("signing failed: %s\n"), gpg_strerror (rc) );
else { else {
if( opt.verbose ) { if( opt.verbose ) {
char *ustr = get_user_id_string_printable (sig->keyid); char *ustr = get_user_id_string_printable (sig->keyid);
log_info(_("%s/%s signature from: \"%s\"\n"), log_info(_("%s/%s signature from: \"%s\"\n"),
pubkey_algo_to_string(sk->pubkey_algo), gcry_pk_algo_name (sk->pubkey_algo),
digest_algo_to_string(sig->digest_algo), gcry_md_algo_name (sig->digest_algo),
ustr ); ustr );
m_free(ustr); xfree (ustr);
} }
} }
return rc; return rc;
@ -354,7 +353,7 @@ hash_for(int pubkey_algo, int packet_version )
prefitem_t *prefs; prefitem_t *prefs;
for(prefs=opt.personal_digest_prefs;prefs->type;prefs++) for(prefs=opt.personal_digest_prefs;prefs->type;prefs++)
if(md_digest_length(prefs->value)==20) if(gcry_md_get_algo_dlen (prefs->value) == 20)
return prefs->value; return prefs->value;
} }
@ -415,7 +414,7 @@ print_status_sig_created ( PKT_secret_key *sk, PKT_signature *sig, int what )
* packet here in reverse order * packet here in reverse order
*/ */
static int static int
write_onepass_sig_packets (SK_LIST sk_list, IOBUF out, int sigclass ) write_onepass_sig_packets (SK_LIST sk_list, iobuf_t out, int sigclass )
{ {
int skcount; int skcount;
SK_LIST sk_rover; SK_LIST sk_rover;
@ -435,7 +434,7 @@ write_onepass_sig_packets (SK_LIST sk_list, IOBUF out, int sigclass )
} }
sk = sk_rover->sk; sk = sk_rover->sk;
ops = m_alloc_clear (sizeof *ops); ops = xcalloc (1,sizeof *ops);
ops->sig_class = sigclass; ops->sig_class = sigclass;
ops->digest_algo = hash_for (sk->pubkey_algo, sk->version); ops->digest_algo = hash_for (sk->pubkey_algo, sk->version);
ops->pubkey_algo = sk->pubkey_algo; ops->pubkey_algo = sk->pubkey_algo;
@ -449,7 +448,7 @@ write_onepass_sig_packets (SK_LIST sk_list, IOBUF out, int sigclass )
free_packet (&pkt); free_packet (&pkt);
if (rc) { if (rc) {
log_error ("build onepass_sig packet failed: %s\n", log_error ("build onepass_sig packet failed: %s\n",
g10_errstr(rc)); gpg_strerror (rc));
return rc; return rc;
} }
} }
@ -461,7 +460,7 @@ write_onepass_sig_packets (SK_LIST sk_list, IOBUF out, int sigclass )
* Helper to write the plaintext (literal data) packet * Helper to write the plaintext (literal data) packet
*/ */
static int static int
write_plaintext_packet (IOBUF out, IOBUF inp, const char *fname, int ptmode) write_plaintext_packet (iobuf_t out, iobuf_t inp, const char *fname, int ptmode)
{ {
PKT_plaintext *pt = NULL; PKT_plaintext *pt = NULL;
u32 filesize; u32 filesize;
@ -470,15 +469,15 @@ write_plaintext_packet (IOBUF out, IOBUF inp, const char *fname, int ptmode)
if (!opt.no_literal) { if (!opt.no_literal) {
if (fname || opt.set_filename) { if (fname || opt.set_filename) {
char *s = make_basename (opt.set_filename? opt.set_filename char *s = make_basename (opt.set_filename? opt.set_filename
: fname, : fname
iobuf_get_real_fname(inp)); /*, iobuf_get_real_fname(inp)*/);
pt = m_alloc (sizeof *pt + strlen(s) - 1); pt = xmalloc (sizeof *pt + strlen(s) - 1);
pt->namelen = strlen (s); pt->namelen = strlen (s);
memcpy (pt->name, s, pt->namelen); memcpy (pt->name, s, pt->namelen);
m_free (s); xfree (s);
} }
else { /* no filename */ else { /* no filename */
pt = m_alloc (sizeof *pt - 1); pt = xmalloc (sizeof *pt - 1);
pt->namelen = 0; pt->namelen = 0;
} }
} }
@ -519,7 +518,7 @@ write_plaintext_packet (IOBUF out, IOBUF inp, const char *fname, int ptmode)
/*cfx.datalen = filesize? calc_packet_length( &pkt ) : 0;*/ /*cfx.datalen = filesize? calc_packet_length( &pkt ) : 0;*/
if( (rc = build_packet (out, &pkt)) ) if( (rc = build_packet (out, &pkt)) )
log_error ("build_packet(PLAINTEXT) failed: %s\n", log_error ("build_packet(PLAINTEXT) failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
pt->buf = NULL; pt->buf = NULL;
} }
else { else {
@ -527,10 +526,9 @@ write_plaintext_packet (IOBUF out, IOBUF inp, const char *fname, int ptmode)
int bytes_copied; int bytes_copied;
while ((bytes_copied = iobuf_read(inp, copy_buffer, 4096)) != -1) while ((bytes_copied = iobuf_read(inp, copy_buffer, 4096)) != -1)
if (iobuf_write(out, copy_buffer, bytes_copied) == -1) { if ( (rc=iobuf_write(out, copy_buffer, bytes_copied) )) {
rc = G10ERR_WRITE_FILE;
log_error ("copying input to output failed: %s\n", log_error ("copying input to output failed: %s\n",
g10_errstr(rc)); gpg_strerror (rc));
break; break;
} }
wipememory(copy_buffer,4096); /* burn buffer */ wipememory(copy_buffer,4096); /* burn buffer */
@ -545,7 +543,7 @@ write_plaintext_packet (IOBUF out, IOBUF inp, const char *fname, int ptmode)
* hash which will not be changes here. * hash which will not be changes here.
*/ */
static int static int
write_signature_packets (SK_LIST sk_list, IOBUF out, MD_HANDLE hash, write_signature_packets (SK_LIST sk_list, iobuf_t out, MD_HANDLE hash,
int sigclass, u32 timestamp, u32 duration, int sigclass, u32 timestamp, u32 duration,
int status_letter) int status_letter)
{ {
@ -561,7 +559,7 @@ write_signature_packets (SK_LIST sk_list, IOBUF out, MD_HANDLE hash,
sk = sk_rover->sk; sk = sk_rover->sk;
/* build the signature packet */ /* build the signature packet */
sig = m_alloc_clear (sizeof *sig); sig = xcalloc (1,sizeof *sig);
if(opt.force_v3_sigs || RFC1991) if(opt.force_v3_sigs || RFC1991)
sig->version=3; sig->version=3;
else if(duration || opt.sig_policy_url || opt.sig_notation_data) else if(duration || opt.sig_policy_url || opt.sig_notation_data)
@ -579,17 +577,17 @@ write_signature_packets (SK_LIST sk_list, IOBUF out, MD_HANDLE hash,
sig->expiredate = sig->timestamp+duration; sig->expiredate = sig->timestamp+duration;
sig->sig_class = sigclass; sig->sig_class = sigclass;
md = md_copy (hash); gcry_md_copy (&md, hash);
if (sig->version >= 4) if (sig->version >= 4)
build_sig_subpkt_from_sig (sig); build_sig_subpkt_from_sig (sig);
mk_notation_and_policy (sig, NULL, sk); mk_notation_and_policy (sig, NULL, sk);
hash_sigversion_to_magic (md, sig); hash_sigversion_to_magic (md, sig);
md_final (md); gcry_md_final (md);
rc = do_sign( sk, sig, md, hash_for (sig->pubkey_algo, sk->version) ); rc = do_sign( sk, sig, md, hash_for (sig->pubkey_algo, sk->version) );
md_close (md); gcry_md_close (md);
if( !rc ) { /* and write it */ if( !rc ) { /* and write it */
PACKET pkt; PACKET pkt;
@ -604,7 +602,7 @@ write_signature_packets (SK_LIST sk_list, IOBUF out, MD_HANDLE hash,
free_packet (&pkt); free_packet (&pkt);
if (rc) if (rc)
log_error ("build signature packet failed: %s\n", log_error ("build signature packet failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
} }
if( rc ) if( rc )
return rc;; return rc;;
@ -636,7 +634,7 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr,
text_filter_context_t tfx; text_filter_context_t tfx;
progress_filter_context_t pfx; progress_filter_context_t pfx;
encrypt_filter_context_t efx; encrypt_filter_context_t efx;
IOBUF inp = NULL, out = NULL; iobuf_t inp = NULL, out = NULL;
PACKET pkt; PACKET pkt;
int rc = 0; int rc = 0;
PK_LIST pk_list = NULL; PK_LIST pk_list = NULL;
@ -682,9 +680,9 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr,
inp = NULL; /* we do it later */ inp = NULL; /* we do it later */
else { else {
if( !(inp = iobuf_open(fname)) ) { if( !(inp = iobuf_open(fname)) ) {
rc = gpg_error_from_errno (errno);
log_error("can't open %s: %s\n", fname? fname: "[stdin]", log_error("can't open %s: %s\n", fname? fname: "[stdin]",
strerror(errno) ); strerror(errno) );
rc = G10ERR_OPEN_FILE;
goto leave; goto leave;
} }
@ -693,8 +691,8 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr,
if( outfile ) { if( outfile ) {
if( !(out = iobuf_create( outfile )) ) { if( !(out = iobuf_create( outfile )) ) {
rc = gpg_error_from_errno (errno);
log_error(_("can't create %s: %s\n"), outfile, strerror(errno) ); log_error(_("can't create %s: %s\n"), outfile, strerror(errno) );
rc = G10ERR_CREATE_FILE;
goto leave; goto leave;
} }
else if( opt.verbose ) else if( opt.verbose )
@ -710,7 +708,7 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr,
iobuf_push_filter( inp, text_filter, &tfx ); iobuf_push_filter( inp, text_filter, &tfx );
} }
mfx.md = md_open(0, 0); gcry_md_open (&mfx.md, 0, 0);
/* If we're encrypting and signing, it is reasonable to pick the /* If we're encrypting and signing, it is reasonable to pick the
hash algorithm to use out of the recepient key prefs. */ hash algorithm to use out of the recepient key prefs. */
@ -724,7 +722,7 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr,
NULL)!=opt.def_digest_algo) NULL)!=opt.def_digest_algo)
log_info(_("forcing digest algorithm %s (%d) " log_info(_("forcing digest algorithm %s (%d) "
"violates recipient preferences\n"), "violates recipient preferences\n"),
digest_algo_to_string(opt.def_digest_algo), gcry_md_algo_name (opt.def_digest_algo),
opt.def_digest_algo); opt.def_digest_algo);
} }
else else
@ -752,7 +750,7 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr,
for( sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next ) { for( sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next ) {
PKT_secret_key *sk = sk_rover->sk; PKT_secret_key *sk = sk_rover->sk;
md_enable(mfx.md, hash_for(sk->pubkey_algo, sk->version )); gcry_md_enable (mfx.md, hash_for(sk->pubkey_algo, sk->version ));
} }
if( !multifile ) if( !multifile )
@ -822,9 +820,9 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr,
for( sl = strlist_last(filenames); sl; for( sl = strlist_last(filenames); sl;
sl = strlist_prev( filenames, sl ) ) { sl = strlist_prev( filenames, sl ) ) {
if( !(inp = iobuf_open(sl->d)) ) { if( !(inp = iobuf_open(sl->d)) ) {
rc = gpg_error_from_errno (errno);
log_error(_("can't open %s: %s\n"), log_error(_("can't open %s: %s\n"),
sl->d, strerror(errno) ); sl->d, strerror(errno) );
rc = G10ERR_OPEN_FILE;
goto leave; goto leave;
} }
handle_progress (&pfx, inp, sl->d); handle_progress (&pfx, inp, sl->d);
@ -875,7 +873,7 @@ sign_file( STRLIST filenames, int detached, STRLIST locusr,
write_status( STATUS_END_ENCRYPTION ); write_status( STATUS_END_ENCRYPTION );
} }
iobuf_close(inp); iobuf_close(inp);
md_close( mfx.md ); gcry_md_close ( mfx.md );
release_sk_list( sk_list ); release_sk_list( sk_list );
release_pk_list( pk_list ); release_pk_list( pk_list );
recipient_digest_algo=0; recipient_digest_algo=0;
@ -893,7 +891,7 @@ clearsign_file( const char *fname, STRLIST locusr, const char *outfile )
armor_filter_context_t afx; armor_filter_context_t afx;
progress_filter_context_t pfx; progress_filter_context_t pfx;
MD_HANDLE textmd = NULL; MD_HANDLE textmd = NULL;
IOBUF inp = NULL, out = NULL; iobuf_t inp = NULL, out = NULL;
PACKET pkt; PACKET pkt;
int rc = 0; int rc = 0;
SK_LIST sk_list = NULL; SK_LIST sk_list = NULL;
@ -923,17 +921,17 @@ clearsign_file( const char *fname, STRLIST locusr, const char *outfile )
/* prepare iobufs */ /* prepare iobufs */
if( !(inp = iobuf_open(fname)) ) { if( !(inp = iobuf_open(fname)) ) {
rc = gpg_error_from_errno (errno);
log_error("can't open %s: %s\n", fname? fname: "[stdin]", log_error("can't open %s: %s\n", fname? fname: "[stdin]",
strerror(errno) ); strerror(errno) );
rc = G10ERR_OPEN_FILE;
goto leave; goto leave;
} }
handle_progress (&pfx, inp, fname); handle_progress (&pfx, inp, fname);
if( outfile ) { if( outfile ) {
if( !(out = iobuf_create( outfile )) ) { if( !(out = iobuf_create( outfile )) ) {
rc = gpg_error_from_errno (errno);
log_error(_("can't create %s: %s\n"), outfile, strerror(errno) ); log_error(_("can't create %s: %s\n"), outfile, strerror(errno) );
rc = G10ERR_CREATE_FILE;
goto leave; goto leave;
} }
else if( opt.verbose ) else if( opt.verbose )
@ -966,7 +964,7 @@ clearsign_file( const char *fname, STRLIST locusr, const char *outfile )
int i = hash_for(sk->pubkey_algo, sk->version); int i = hash_for(sk->pubkey_algo, sk->version);
if( !hashs_seen[ i & 0xff ] ) { if( !hashs_seen[ i & 0xff ] ) {
s = digest_algo_to_string( i ); s = gcry_md_algo_name (i);
if( s ) { if( s ) {
hashs_seen[ i & 0xff ] = 1; hashs_seen[ i & 0xff ] = 1;
if( any ) if( any )
@ -985,13 +983,13 @@ clearsign_file( const char *fname, STRLIST locusr, const char *outfile )
"NotDashEscaped: You need GnuPG to verify this message" LF ); "NotDashEscaped: You need GnuPG to verify this message" LF );
iobuf_writestr(out, LF ); iobuf_writestr(out, LF );
textmd = md_open(0, 0); gcry_md_open (&textmd, 0, 0);
for( sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next ) { for( sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next ) {
PKT_secret_key *sk = sk_rover->sk; PKT_secret_key *sk = sk_rover->sk;
md_enable(textmd, hash_for(sk->pubkey_algo, sk->version)); gcry_md_enable (textmd, hash_for(sk->pubkey_algo, sk->version));
} }
if ( DBG_HASHING ) if ( DBG_HASHING )
md_start_debug( textmd, "clearsign" ); gcry_md_start_debug ( textmd, "clearsign" );
copy_clearsig_text( out, inp, textmd, !opt.not_dash_escaped, copy_clearsig_text( out, inp, textmd, !opt.not_dash_escaped,
opt.escape_from, (old_style && only_md5) ); opt.escape_from, (old_style && only_md5) );
/* fixme: check for read errors */ /* fixme: check for read errors */
@ -1011,7 +1009,7 @@ clearsign_file( const char *fname, STRLIST locusr, const char *outfile )
else else
iobuf_close(out); iobuf_close(out);
iobuf_close(inp); iobuf_close(inp);
md_close( textmd ); gcry_md_close ( textmd );
release_sk_list( sk_list ); release_sk_list( sk_list );
return rc; return rc;
} }
@ -1029,7 +1027,7 @@ sign_symencrypt_file (const char *fname, STRLIST locusr)
md_filter_context_t mfx; md_filter_context_t mfx;
text_filter_context_t tfx; text_filter_context_t tfx;
cipher_filter_context_t cfx; cipher_filter_context_t cfx;
IOBUF inp = NULL, out = NULL; iobuf_t inp = NULL, out = NULL;
PACKET pkt; PACKET pkt;
STRING2KEY *s2k = NULL; STRING2KEY *s2k = NULL;
int rc = 0; int rc = 0;
@ -1055,27 +1053,27 @@ sign_symencrypt_file (const char *fname, STRLIST locusr)
/* prepare iobufs */ /* prepare iobufs */
inp = iobuf_open(fname); inp = iobuf_open(fname);
if( !inp ) { if( !inp ) {
rc = gpg_error_from_errno (errno);
log_error("can't open %s: %s\n", fname? fname: "[stdin]", log_error("can't open %s: %s\n", fname? fname: "[stdin]",
strerror(errno) ); strerror(errno) );
rc = G10ERR_OPEN_FILE;
goto leave; goto leave;
} }
handle_progress (&pfx, inp, fname); handle_progress (&pfx, inp, fname);
/* prepare key */ /* prepare key */
s2k = m_alloc_clear( sizeof *s2k ); s2k = xcalloc (1, sizeof *s2k );
s2k->mode = RFC1991? 0:opt.s2k_mode; s2k->mode = RFC1991? 0:opt.s2k_mode;
s2k->hash_algo = opt.s2k_digest_algo; s2k->hash_algo = opt.s2k_digest_algo;
algo = default_cipher_algo(); algo = default_cipher_algo();
if (!opt.quiet || !opt.batch) if (!opt.quiet || !opt.batch)
log_info (_("%s encryption will be used\n"), log_info (_("%s encryption will be used\n"),
cipher_algo_to_string(algo) ); gcry_cipher_algo_name (algo) );
cfx.dek = passphrase_to_dek( NULL, 0, algo, s2k, 2, NULL, NULL); cfx.dek = passphrase_to_dek( NULL, 0, algo, s2k, 2, NULL, NULL);
if (!cfx.dek || !cfx.dek->keylen) { if (!cfx.dek || !cfx.dek->keylen) {
rc = G10ERR_PASSPHRASE; rc = gpg_error (GPG_ERR_INV_PASSPHRASE);
log_error(_("error creating passphrase: %s\n"), g10_errstr(rc) ); log_error(_("error creating passphrase: %s\n"), gpg_strerror (rc) );
goto leave; goto leave;
} }
@ -1087,11 +1085,11 @@ sign_symencrypt_file (const char *fname, STRLIST locusr)
/* prepare to calculate the MD over the input */ /* prepare to calculate the MD over the input */
if (opt.textmode) if (opt.textmode)
iobuf_push_filter (inp, text_filter, &tfx); iobuf_push_filter (inp, text_filter, &tfx);
mfx.md = md_open(0, 0); gcry_md_open (&mfx.md, 0, 0);
for (sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next) { for (sk_rover = sk_list; sk_rover; sk_rover = sk_rover->next) {
PKT_secret_key *sk = sk_rover->sk; PKT_secret_key *sk = sk_rover->sk;
md_enable (mfx.md, hash_for (sk->pubkey_algo, sk->version )); gcry_md_enable (mfx.md, hash_for (sk->pubkey_algo, sk->version ));
} }
iobuf_push_filter (inp, md_filter, &mfx); iobuf_push_filter (inp, md_filter, &mfx);
@ -1103,15 +1101,15 @@ sign_symencrypt_file (const char *fname, STRLIST locusr)
/* Write the symmetric key packet */ /* Write the symmetric key packet */
/*(current filters: armor)*/ /*(current filters: armor)*/
if (!RFC1991) { if (!RFC1991) {
PKT_symkey_enc *enc = m_alloc_clear( sizeof *enc ); PKT_symkey_enc *enc = xcalloc (1, sizeof *enc );
enc->version = 4; enc->version = 4;
enc->cipher_algo = cfx.dek->algo; enc->cipher_algo = cfx.dek->algo;
enc->s2k = *s2k; enc->s2k = *s2k;
pkt.pkttype = PKT_SYMKEY_ENC; pkt.pkttype = PKT_SYMKEY_ENC;
pkt.pkt.symkey_enc = enc; pkt.pkt.symkey_enc = enc;
if( (rc = build_packet( out, &pkt )) ) if( (rc = build_packet( out, &pkt )) )
log_error("build symkey packet failed: %s\n", g10_errstr(rc) ); log_error("build symkey packet failed: %s\n", gpg_strerror (rc) );
m_free(enc); xfree (enc);
} }
/* Push the encryption filter */ /* Push the encryption filter */
@ -1157,9 +1155,9 @@ sign_symencrypt_file (const char *fname, STRLIST locusr)
} }
iobuf_close(inp); iobuf_close(inp);
release_sk_list( sk_list ); release_sk_list( sk_list );
md_close( mfx.md ); gcry_md_close ( mfx.md );
m_free(cfx.dek); xfree (cfx.dek);
m_free(s2k); xfree (s2k);
return rc; return rc;
} }
@ -1224,7 +1222,7 @@ make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk,
digest_algo = DIGEST_ALGO_SHA1; digest_algo = DIGEST_ALGO_SHA1;
} }
md = md_open( digest_algo, 0 ); gcry_md_open (&md, digest_algo, 0 );
/* hash the public key certificate and the user id */ /* hash the public key certificate and the user id */
hash_public_key( md, pk ); hash_public_key( md, pk );
@ -1235,7 +1233,7 @@ make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk,
hash_uid (md, sigversion, uid); hash_uid (md, sigversion, uid);
} }
/* and make the signature packet */ /* and make the signature packet */
sig = m_alloc_clear( sizeof *sig ); sig = xcalloc (1, sizeof *sig );
sig->version = sigversion; sig->version = sigversion;
sig->flags.exportable=1; sig->flags.exportable=1;
sig->flags.revocable=1; sig->flags.revocable=1;
@ -1261,12 +1259,12 @@ make_keysig_packet( PKT_signature **ret_sig, PKT_public_key *pk,
if( !rc ) { if( !rc ) {
hash_sigversion_to_magic (md, sig); hash_sigversion_to_magic (md, sig);
md_final(md); gcry_md_final (md);
rc = complete_sig( sig, sk, md ); rc = complete_sig( sig, sk, md );
} }
md_close( md ); gcry_md_close ( md );
if( rc ) if( rc )
free_seckey_enc( sig ); free_seckey_enc( sig );
else else
@ -1299,9 +1297,9 @@ update_keysig_packet( PKT_signature **ret_sig,
if ((!orig_sig || !pk || !sk) if ((!orig_sig || !pk || !sk)
|| (orig_sig->sig_class >= 0x10 && orig_sig->sig_class <= 0x13 && !uid) || (orig_sig->sig_class >= 0x10 && orig_sig->sig_class <= 0x13 && !uid)
|| (orig_sig->sig_class == 0x18 && !subpk)) || (orig_sig->sig_class == 0x18 && !subpk))
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
md = md_open( orig_sig->digest_algo, 0 ); gcry_md_open (&md, orig_sig->digest_algo, 0);
/* hash the public key certificate and the user id */ /* hash the public key certificate and the user id */
hash_public_key( md, pk ); hash_public_key( md, pk );
@ -1344,12 +1342,12 @@ update_keysig_packet( PKT_signature **ret_sig,
if (!rc) { if (!rc) {
hash_sigversion_to_magic (md, sig); hash_sigversion_to_magic (md, sig);
md_final(md); gcry_md_final (md);
rc = complete_sig( sig, sk, md ); rc = complete_sig( sig, sk, md );
} }
md_close (md); gcry_md_close (md);
if( rc ) if( rc )
free_seckey_enc (sig); free_seckey_enc (sig);
else else

View File

@ -88,17 +88,17 @@ got_fatal_signal( int sig )
raise( sig ); raise( sig );
caught_fatal_sig = 1; caught_fatal_sig = 1;
secmem_term(); gcry_control (GCRYCTL_TERM_SECMEM );
/* better don't transtale these messages */ /* better don't transtale these messages */
write(2, "\n", 1 ); write(2, "\n", 1 );
s = log_get_name(); if( s ) write(2, s, strlen(s) ); s = "?" /* FIXME: log_get_name()*/; if( s ) write(2, s, strlen(s) );
write(2, ": ", 2 ); write(2, ": ", 2 );
s = get_signal_name(sig); write(2, s, strlen(s) ); s = get_signal_name(sig); write(2, s, strlen(s) );
write(2, " caught ... exiting\n", 20 ); write(2, " caught ... exiting\n", 20 );
/* reset action to default action and raise signal again */ /* reset action to default action and raise signal again */
init_one_signal (sig, SIG_DFL, 0); init_one_signal (sig, SIG_DFL, 0);
remove_lockfiles (); dotlock_remove_lockfiles ();
#ifdef __riscos__ #ifdef __riscos__
riscos_close_fds (); riscos_close_fds ();
#endif /* __riscos__ */ #endif /* __riscos__ */

View File

@ -43,41 +43,11 @@ release_sk_list( SK_LIST sk_list )
for( ; sk_list; sk_list = sk_rover ) { for( ; sk_list; sk_list = sk_rover ) {
sk_rover = sk_list->next; sk_rover = sk_list->next;
free_secret_key( sk_list->sk ); free_secret_key( sk_list->sk );
m_free( sk_list ); xfree ( sk_list );
} }
} }
/* Check that we are only using keys which don't have
* the string "(insecure!)" or "not secure" or "do not use"
* in one of the user ids
*/
static int
is_insecure( PKT_secret_key *sk )
{
u32 keyid[2];
KBNODE node = NULL, u;
int insecure = 0;
keyid_from_sk( sk, keyid );
node = get_pubkeyblock( keyid );
for ( u = node; u; u = u->next ) {
if ( u->pkt->pkttype == PKT_USER_ID ) {
PKT_user_id *id = u->pkt->pkt.user_id;
if ( id->attrib_data )
continue; /* skip attribute packets */
if ( strstr( id->name, "(insecure!)" )
|| strstr( id->name, "not secure" )
|| strstr( id->name, "do not use" ) ) {
insecure = 1;
break;
}
}
}
release_kbnode( node );
return insecure;
}
static int static int
key_present_in_sk_list(SK_LIST sk_list, PKT_secret_key *sk) key_present_in_sk_list(SK_LIST sk_list, PKT_secret_key *sk)
@ -110,13 +80,13 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
if( !locusr ) { /* use the default one */ if( !locusr ) { /* use the default one */
PKT_secret_key *sk; PKT_secret_key *sk;
sk = m_alloc_clear( sizeof *sk ); sk = xcalloc (1, sizeof *sk );
sk->req_usage = use; sk->req_usage = use;
if( (rc = get_seckey_byname( sk, NULL, unlock )) ) { if( (rc = get_seckey_byname( sk, NULL, unlock )) ) {
free_secret_key( sk ); sk = NULL; free_secret_key( sk ); sk = NULL;
log_error("no default secret key: %s\n", g10_errstr(rc) ); log_error("no default secret key: %s\n", gpg_strerror (rc) );
} }
else if( !(rc=check_pubkey_algo2(sk->pubkey_algo, use)) ) { else if( !(rc=openpgp_pk_test_algo (sk->pubkey_algo, use)) ) {
SK_LIST r; SK_LIST r;
if( sk->version == 4 && (use & PUBKEY_USAGE_SIG) if( sk->version == 4 && (use & PUBKEY_USAGE_SIG)
@ -125,13 +95,8 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
"ElGamal key which is NOT secure for signatures!\n"); "ElGamal key which is NOT secure for signatures!\n");
free_secret_key( sk ); sk = NULL; free_secret_key( sk ); sk = NULL;
} }
else if( random_is_faked() && !is_insecure( sk ) ) {
log_info(_("key is not flagged as insecure - "
"can't use it with the faked RNG!\n"));
free_secret_key( sk ); sk = NULL;
}
else { else {
r = m_alloc( sizeof *r ); r = xmalloc ( sizeof *r );
r->sk = sk; sk = NULL; r->sk = sk; sk = NULL;
r->next = sk_list; r->next = sk_list;
r->mark = 0; r->mark = 0;
@ -140,7 +105,7 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
} }
else { else {
free_secret_key( sk ); sk = NULL; free_secret_key( sk ); sk = NULL;
log_error("invalid default secret key: %s\n", g10_errstr(rc) ); log_error("invalid default secret key: %s\n", gpg_strerror (rc) );
} }
} }
else { else {
@ -157,11 +122,11 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
log_error(_("skipped `%s': duplicated\n"), locusr->d ); log_error(_("skipped `%s': duplicated\n"), locusr->d );
continue; continue;
} }
sk = m_alloc_clear( sizeof *sk ); sk = xcalloc (1, sizeof *sk );
sk->req_usage = use; sk->req_usage = use;
if( (rc = get_seckey_byname( sk, locusr->d, 0 )) ) { if( (rc = get_seckey_byname( sk, locusr->d, 0 )) ) {
free_secret_key( sk ); sk = NULL; free_secret_key( sk ); sk = NULL;
log_error(_("skipped `%s': %s\n"), locusr->d, g10_errstr(rc) ); log_error(_("skipped `%s': %s\n"), locusr->d, gpg_strerror (rc) );
} }
else if ( key_present_in_sk_list(sk_list, sk) == 0) { else if ( key_present_in_sk_list(sk_list, sk) == 0) {
free_secret_key(sk); sk = NULL; free_secret_key(sk); sk = NULL;
@ -169,9 +134,9 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
} }
else if ( unlock && (rc = check_secret_key( sk, 0 )) ) { else if ( unlock && (rc = check_secret_key( sk, 0 )) ) {
free_secret_key( sk ); sk = NULL; free_secret_key( sk ); sk = NULL;
log_error(_("skipped `%s': %s\n"), locusr->d, g10_errstr(rc) ); log_error(_("skipped `%s': %s\n"), locusr->d, gpg_strerror (rc) );
} }
else if( !(rc=check_pubkey_algo2(sk->pubkey_algo, use)) ) { else if( !(rc=openpgp_pk_test_algo (sk->pubkey_algo, use)) ) {
SK_LIST r; SK_LIST r;
if( sk->version == 4 && (use & PUBKEY_USAGE_SIG) if( sk->version == 4 && (use & PUBKEY_USAGE_SIG)
@ -181,13 +146,8 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
locusr->d ); locusr->d );
free_secret_key( sk ); sk = NULL; free_secret_key( sk ); sk = NULL;
} }
else if( random_is_faked() && !is_insecure( sk ) ) {
log_info(_("key is not flagged as insecure - "
"can't use it with the faked RNG!\n"));
free_secret_key( sk ); sk = NULL;
}
else { else {
r = m_alloc( sizeof *r ); r = xmalloc ( sizeof *r );
r->sk = sk; sk = NULL; r->sk = sk; sk = NULL;
r->next = sk_list; r->next = sk_list;
r->mark = 0; r->mark = 0;
@ -196,7 +156,7 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
} }
else { else {
free_secret_key( sk ); sk = NULL; free_secret_key( sk ); sk = NULL;
log_error("skipped `%s': %s\n", locusr->d, g10_errstr(rc) ); log_error("skipped `%s': %s\n", locusr->d, gpg_strerror (rc) );
} }
} }
} }
@ -204,7 +164,7 @@ build_sk_list( STRLIST locusr, SK_LIST *ret_sk_list,
if( !rc && !sk_list ) { if( !rc && !sk_list ) {
log_error("no valid signators\n"); log_error("no valid signators\n");
rc = G10ERR_NO_USER_ID; rc = GPG_ERR_NO_USER_ID;
} }
if( rc ) if( rc )

View File

@ -40,6 +40,8 @@
#include <sys/mman.h> #include <sys/mman.h>
#endif #endif
#endif #endif
#include "gpg.h"
#include "util.h" #include "util.h"
#include "status.h" #include "status.h"
#include "ttyio.h" #include "ttyio.h"
@ -179,9 +181,12 @@ set_status_fd ( int fd )
fd, strerror(errno)); fd, strerror(errno));
} }
last_fd = fd; last_fd = fd;
#warning fixme: register progress CBs
#if 0
register_primegen_progress ( progress_cb, "primegen" ); register_primegen_progress ( progress_cb, "primegen" );
register_pk_dsa_progress ( progress_cb, "pk_dsa" ); register_pk_dsa_progress ( progress_cb, "pk_dsa" );
register_pk_elg_progress ( progress_cb, "pk_elg" ); register_pk_elg_progress ( progress_cb, "pk_elg" );
#endif
} }
int int
@ -453,7 +458,7 @@ do_shm_get( const char *keyword, int hidden, int bool )
if( bool ) if( bool )
return p[0]? "" : NULL; return p[0]? "" : NULL;
string = hidden? m_alloc_secure( n+1 ) : m_alloc( n+1 ); string = hidden? xmalloc_secure ( n+1 ) : xmalloc ( n+1 );
memcpy(string, p, n ); memcpy(string, p, n );
string[n] = 0; /* make sure it is a string */ string[n] = 0; /* make sure it is a string */
if( hidden ) /* invalidate the memory */ if( hidden ) /* invalidate the memory */
@ -508,7 +513,7 @@ do_get_from_fd( const char *keyword, int hidden, int bool )
if( i >= len-1 ) { if( i >= len-1 ) {
char *save = string; char *save = string;
len += 100; len += 100;
string = hidden? m_alloc_secure ( len ) : m_alloc ( len ); string = hidden? xmalloc_secure ( len ) : xmalloc ( len );
if( save ) if( save )
memcpy(string, save, i ); memcpy(string, save, i );
else else
@ -579,7 +584,7 @@ cpr_get( const char *keyword, const char *prompt )
for(;;) { for(;;) {
p = tty_get( prompt ); p = tty_get( prompt );
if( *p=='?' && !p[1] && !(keyword && !*keyword)) { if( *p=='?' && !p[1] && !(keyword && !*keyword)) {
m_free(p); xfree (p);
display_online_help( keyword ); display_online_help( keyword );
} }
else else
@ -595,7 +600,7 @@ cpr_get_utf8( const char *keyword, const char *prompt )
p = cpr_get( keyword, prompt ); p = cpr_get( keyword, prompt );
if( p ) { if( p ) {
char *utf8 = native_to_utf8( p ); char *utf8 = native_to_utf8( p );
m_free( p ); xfree ( p );
p = utf8; p = utf8;
} }
return p; return p;
@ -615,7 +620,7 @@ cpr_get_hidden( const char *keyword, const char *prompt )
for(;;) { for(;;) {
p = tty_get_hidden( prompt ); p = tty_get_hidden( prompt );
if( *p == '?' && !p[1] ) { if( *p == '?' && !p[1] ) {
m_free(p); xfree (p);
display_online_help( keyword ); display_online_help( keyword );
} }
else else
@ -652,13 +657,13 @@ cpr_get_answer_is_yes( const char *keyword, const char *prompt )
p = tty_get( prompt ); p = tty_get( prompt );
trim_spaces(p); /* it is okay to do this here */ trim_spaces(p); /* it is okay to do this here */
if( *p == '?' && !p[1] ) { if( *p == '?' && !p[1] ) {
m_free(p); xfree (p);
display_online_help( keyword ); display_online_help( keyword );
} }
else { else {
tty_kill_prompt(); tty_kill_prompt();
yes = answer_is_yes(p); yes = answer_is_yes(p);
m_free(p); xfree (p);
return yes; return yes;
} }
} }
@ -680,13 +685,13 @@ cpr_get_answer_yes_no_quit( const char *keyword, const char *prompt )
p = tty_get( prompt ); p = tty_get( prompt );
trim_spaces(p); /* it is okay to do this here */ trim_spaces(p); /* it is okay to do this here */
if( *p == '?' && !p[1] ) { if( *p == '?' && !p[1] ) {
m_free(p); xfree (p);
display_online_help( keyword ); display_online_help( keyword );
} }
else { else {
tty_kill_prompt(); tty_kill_prompt();
yes = answer_is_yes_no_quit(p); yes = answer_is_yes_no_quit(p);
m_free(p); xfree (p);
return yes; return yes;
} }
} }

View File

@ -1,5 +1,5 @@
/* tdbdump.c /* tdbdump.c
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -30,6 +30,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include "gpg.h"
#include "errors.h" #include "errors.h"
#include "iobuf.h" #include "iobuf.h"
#include "keydb.h" #include "keydb.h"
@ -57,7 +58,7 @@ write_record( TRUSTREC *rec )
if( !rc ) if( !rc )
return; return;
log_error(_("trust record %lu, type %d: write failed: %s\n"), log_error(_("trust record %lu, type %d: write failed: %s\n"),
rec->recnum, rec->rectype, g10_errstr(rc) ); rec->recnum, rec->rectype, gpg_strerror (rc) );
tdbio_invalid(); tdbio_invalid();
} }
@ -137,7 +138,7 @@ import_ownertrust( const char *fname )
is_stdin = 1; is_stdin = 1;
} }
else if( !(fp = fopen( fname, "r" )) ) { else if( !(fp = fopen( fname, "r" )) ) {
log_error_f(fname, _("can't open file: %s\n"), strerror(errno) ); log_error ( _("can't open `%s': %s\n"), fname, strerror(errno) );
return; return;
} }
@ -148,7 +149,7 @@ import_ownertrust( const char *fname )
continue; continue;
n = strlen(line); n = strlen(line);
if( line[n-1] != '\n' ) { if( line[n-1] != '\n' ) {
log_error_f(fname, _("line too long\n") ); log_error (_("\b%s: line too long\n"), fname );
/* ... or last line does not have a LF */ /* ... or last line does not have a LF */
break; /* can't continue */ break; /* can't continue */
} }
@ -156,16 +157,16 @@ import_ownertrust( const char *fname )
if( !isxdigit(*p) ) if( !isxdigit(*p) )
break; break;
if( *p != ':' ) { if( *p != ':' ) {
log_error_f(fname, _("error: missing colon\n") ); log_error (_("\b%s: error: missing colon\n"), fname );
continue; continue;
} }
fprlen = p - line; fprlen = p - line;
if( fprlen != 32 && fprlen != 40 ) { if( fprlen != 32 && fprlen != 40 ) {
log_error_f(fname, _("error: invalid fingerprint\n") ); log_error (_("\b%s: error: invalid fingerprint\n"), fname );
continue; continue;
} }
if( sscanf(p, ":%u:", &otrust ) != 1 ) { if( sscanf(p, ":%u:", &otrust ) != 1 ) {
log_error_f(fname, _("error: no ownertrust value\n") ); log_error (_("\b%s: error: no ownertrust value\n"), fname );
continue; continue;
} }
if( !otrust ) if( !otrust )
@ -201,11 +202,11 @@ import_ownertrust( const char *fname )
any = 1; any = 1;
} }
else /* error */ else /* error */
log_error_f(fname, _("error finding trust record: %s\n"), log_error (_("\b%s: error finding trust record: %s\n"),
g10_errstr(rc)); fname, gpg_strerror (rc));
} }
if( ferror(fp) ) if( ferror(fp) )
log_error_f(fname, _("read error: %s\n"), strerror(errno) ); log_error (_("\b%s: read error: %s\n"), fname, strerror(errno) );
if( !is_stdin ) if( !is_stdin )
fclose(fp); fclose(fp);
@ -214,7 +215,7 @@ import_ownertrust( const char *fname )
revalidation_mark (); revalidation_mark ();
rc = tdbio_sync (); rc = tdbio_sync ();
if (rc) if (rc)
log_error (_("trustdb: sync failed: %s\n"), g10_errstr(rc) ); log_error (_("trustdb: sync failed: %s\n"), gpg_strerror (rc) );
} }
} }

View File

@ -29,6 +29,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <unistd.h> #include <unistd.h>
#include "gpg.h"
#include "errors.h" #include "errors.h"
#include "iobuf.h" #include "iobuf.h"
#include "memory.h" #include "memory.h"
@ -123,17 +124,20 @@ static int
write_cache_item( CACHE_CTRL r ) write_cache_item( CACHE_CTRL r )
{ {
int n; int n;
gpg_error_t rc;
if( lseek( db_fd, r->recno * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) { if( lseek( db_fd, r->recno * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
rc = gpg_error_from_errno (errno);
log_error(_("trustdb rec %lu: lseek failed: %s\n"), log_error(_("trustdb rec %lu: lseek failed: %s\n"),
r->recno, strerror(errno) ); r->recno, strerror(errno) );
return G10ERR_WRITE_FILE; return rc;
} }
n = write( db_fd, r->data, TRUST_RECORD_LEN); n = write( db_fd, r->data, TRUST_RECORD_LEN);
if( n != TRUST_RECORD_LEN ) { if( n != TRUST_RECORD_LEN ) {
rc = gpg_error_from_errno (errno);
log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"), log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
r->recno, n, strerror(errno) ); r->recno, n, strerror(errno) );
return G10ERR_WRITE_FILE; return rc;
} }
r->flags.dirty = 0; r->flags.dirty = 0;
return 0; return 0;
@ -187,7 +191,7 @@ put_record_into_cache( ulong recno, const char *data )
} }
/* see whether we reached the limit */ /* see whether we reached the limit */
if( cache_entries < MAX_CACHE_ENTRIES_SOFT ) { /* no */ if( cache_entries < MAX_CACHE_ENTRIES_SOFT ) { /* no */
r = m_alloc( sizeof *r ); r = xmalloc ( sizeof *r );
r->flags.used = 1; r->flags.used = 1;
r->recno = recno; r->recno = recno;
memcpy( r->data, data, TRUST_RECORD_LEN ); memcpy( r->data, data, TRUST_RECORD_LEN );
@ -230,7 +234,7 @@ put_record_into_cache( ulong recno, const char *data )
if( cache_entries < MAX_CACHE_ENTRIES_HARD ) { /* no */ if( cache_entries < MAX_CACHE_ENTRIES_HARD ) { /* no */
if( opt.debug && !(cache_entries % 100) ) if( opt.debug && !(cache_entries % 100) )
log_debug("increasing tdbio cache size\n"); log_debug("increasing tdbio cache size\n");
r = m_alloc( sizeof *r ); r = xmalloc ( sizeof *r );
r->flags.used = 1; r->flags.used = 1;
r->recno = recno; r->recno = recno;
memcpy( r->data, data, TRUST_RECORD_LEN ); memcpy( r->data, data, TRUST_RECORD_LEN );
@ -242,7 +246,7 @@ put_record_into_cache( ulong recno, const char *data )
return 0; return 0;
} }
log_info(_("trustdb transaction too large\n")); log_info(_("trustdb transaction too large\n"));
return G10ERR_RESOURCE_LIMIT; return GPG_ERR_RESOURCE_LIMIT;
} }
if( dirty_count ) { if( dirty_count ) {
int n = dirty_count / 5; /* discard some dirty entries */ int n = dirty_count / 5; /* discard some dirty entries */
@ -488,13 +492,13 @@ tdbio_set_dbname( const char *new_dbname, int create )
fname = make_filename (opt.homedir, new_dbname, NULL); fname = make_filename (opt.homedir, new_dbname, NULL);
} }
else else
fname = m_strdup (new_dbname); fname = xstrdup (new_dbname);
if( access( fname, R_OK ) ) { if( access( fname, R_OK ) ) {
if( errno != ENOENT ) { if( errno != ENOENT ) {
log_error( _("%s: can't access: %s\n"), fname, strerror(errno) ); log_error( _("%s: can't access: %s\n"), fname, strerror(errno) );
m_free(fname); xfree (fname);
return G10ERR_TRUSTDB; return GPG_ERR_TRUSTDB;
} }
if( create ) { if( create ) {
FILE *fp; FILE *fp;
@ -511,7 +515,7 @@ tdbio_set_dbname( const char *new_dbname, int create )
} }
*p = DIRSEP_C; *p = DIRSEP_C;
m_free(db_name); xfree (db_name);
db_name = fname; db_name = fname;
#ifdef __riscos__ #ifdef __riscos__
if( !lockhandle ) if( !lockhandle )
@ -541,7 +545,7 @@ tdbio_set_dbname( const char *new_dbname, int create )
rc = create_version_record (); rc = create_version_record ();
if( rc ) if( rc )
log_fatal( _("%s: failed to create version record: %s"), log_fatal( _("%s: failed to create version record: %s"),
fname, g10_errstr(rc)); fname, gpg_strerror (rc));
/* and read again to check that we are okay */ /* and read again to check that we are okay */
if( tdbio_read_record( 0, &rec, RECTYPE_VER ) ) if( tdbio_read_record( 0, &rec, RECTYPE_VER ) )
log_fatal( _("%s: invalid trustdb created\n"), db_name ); log_fatal( _("%s: invalid trustdb created\n"), db_name );
@ -552,7 +556,7 @@ tdbio_set_dbname( const char *new_dbname, int create )
return 0; return 0;
} }
} }
m_free(db_name); xfree (db_name);
db_name = fname; db_name = fname;
return 0; return 0;
} }
@ -636,7 +640,7 @@ create_hashtable( TRUSTREC *vr, int type )
rc = tdbio_write_record( &rec ); rc = tdbio_write_record( &rec );
if( rc ) if( rc )
log_fatal( _("%s: failed to create hashtable: %s\n"), log_fatal( _("%s: failed to create hashtable: %s\n"),
db_name, g10_errstr(rc)); db_name, gpg_strerror (rc));
} }
/* update the version record */ /* update the version record */
rc = tdbio_write_record( vr ); rc = tdbio_write_record( vr );
@ -644,7 +648,7 @@ create_hashtable( TRUSTREC *vr, int type )
rc = tdbio_sync(); rc = tdbio_sync();
if( rc ) if( rc )
log_fatal( _("%s: error updating version record: %s\n"), log_fatal( _("%s: error updating version record: %s\n"),
db_name, g10_errstr(rc)); db_name, gpg_strerror (rc));
} }
@ -661,7 +665,7 @@ tdbio_db_matches_options()
rc = tdbio_read_record( 0, &vr, RECTYPE_VER ); rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
if( rc ) if( rc )
log_fatal( _("%s: error reading version record: %s\n"), log_fatal( _("%s: error reading version record: %s\n"),
db_name, g10_errstr(rc) ); db_name, gpg_strerror (rc) );
yes_no = vr.r.ver.marginals == opt.marginals_needed yes_no = vr.r.ver.marginals == opt.marginals_needed
&& vr.r.ver.completes == opt.completes_needed && vr.r.ver.completes == opt.completes_needed
@ -681,7 +685,7 @@ tdbio_read_model(void)
rc = tdbio_read_record( 0, &vr, RECTYPE_VER ); rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
if( rc ) if( rc )
log_fatal( _("%s: error reading version record: %s\n"), log_fatal( _("%s: error reading version record: %s\n"),
db_name, g10_errstr(rc) ); db_name, gpg_strerror (rc) );
return vr.r.ver.trust_model; return vr.r.ver.trust_model;
} }
@ -697,7 +701,7 @@ tdbio_read_nextcheck ()
rc = tdbio_read_record( 0, &vr, RECTYPE_VER ); rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
if( rc ) if( rc )
log_fatal( _("%s: error reading version record: %s\n"), log_fatal( _("%s: error reading version record: %s\n"),
db_name, g10_errstr(rc) ); db_name, gpg_strerror (rc) );
return vr.r.ver.nextcheck; return vr.r.ver.nextcheck;
} }
@ -711,7 +715,7 @@ tdbio_write_nextcheck (ulong stamp)
rc = tdbio_read_record( 0, &vr, RECTYPE_VER ); rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
if( rc ) if( rc )
log_fatal( _("%s: error reading version record: %s\n"), log_fatal( _("%s: error reading version record: %s\n"),
db_name, g10_errstr(rc) ); db_name, gpg_strerror (rc) );
if (vr.r.ver.nextcheck == stamp) if (vr.r.ver.nextcheck == stamp)
return 0; return 0;
@ -720,7 +724,7 @@ tdbio_write_nextcheck (ulong stamp)
rc = tdbio_write_record( &vr ); rc = tdbio_write_record( &vr );
if( rc ) if( rc )
log_fatal( _("%s: error writing version record: %s\n"), log_fatal( _("%s: error writing version record: %s\n"),
db_name, g10_errstr(rc) ); db_name, gpg_strerror (rc) );
return 1; return 1;
} }
@ -741,7 +745,7 @@ get_trusthashrec(void)
rc = tdbio_read_record( 0, &vr, RECTYPE_VER ); rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
if( rc ) if( rc )
log_fatal( _("%s: error reading version record: %s\n"), log_fatal( _("%s: error reading version record: %s\n"),
db_name, g10_errstr(rc) ); db_name, gpg_strerror (rc) );
if( !vr.r.ver.trusthashtbl ) if( !vr.r.ver.trusthashtbl )
create_hashtable( &vr, 0 ); create_hashtable( &vr, 0 );
@ -773,7 +777,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL ); rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
if( rc ) { if( rc ) {
log_error( db_name, "upd_hashtable: read failed: %s\n", log_error( db_name, "upd_hashtable: read failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
@ -783,7 +787,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
rc = tdbio_write_record( &rec ); rc = tdbio_write_record( &rec );
if( rc ) { if( rc ) {
log_error( db_name, "upd_hashtable: write htbl failed: %s\n", log_error( db_name, "upd_hashtable: write htbl failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
} }
@ -792,7 +796,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
rc = tdbio_read_record( item, &rec, 0 ); rc = tdbio_read_record( item, &rec, 0 );
if( rc ) { if( rc ) {
log_error( "upd_hashtable: read item failed: %s\n", log_error( "upd_hashtable: read item failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
@ -801,7 +805,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
level++; level++;
if( level >= keylen ) { if( level >= keylen ) {
log_error( "hashtable has invalid indirections.\n"); log_error( "hashtable has invalid indirections.\n");
return G10ERR_TRUSTDB; return GPG_ERR_TRUSTDB;
} }
goto next_level; goto next_level;
} }
@ -818,7 +822,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
&rec, RECTYPE_HLST); &rec, RECTYPE_HLST);
if( rc ) { if( rc ) {
log_error( "upd_hashtable: read hlst failed: %s\n", log_error( "upd_hashtable: read hlst failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
} }
@ -833,7 +837,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
rc = tdbio_write_record( &rec ); rc = tdbio_write_record( &rec );
if( rc ) if( rc )
log_error( "upd_hashtable: write hlst failed: %s\n", log_error( "upd_hashtable: write hlst failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; /* done */ return rc; /* done */
} }
} }
@ -842,7 +846,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
&rec, RECTYPE_HLST ); &rec, RECTYPE_HLST );
if( rc ) { if( rc ) {
log_error( "upd_hashtable: read hlst failed: %s\n", log_error( "upd_hashtable: read hlst failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
} }
@ -851,7 +855,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
rc = tdbio_write_record( &rec ); rc = tdbio_write_record( &rec );
if( rc ) { if( rc ) {
log_error( "upd_hashtable: write hlst failed: %s\n", log_error( "upd_hashtable: write hlst failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
memset( &rec, 0, sizeof rec ); memset( &rec, 0, sizeof rec );
@ -861,7 +865,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
rc = tdbio_write_record( &rec ); rc = tdbio_write_record( &rec );
if( rc ) if( rc )
log_error( "upd_hashtable: write ext hlst failed: %s\n", log_error( "upd_hashtable: write ext hlst failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; /* done */ return rc; /* done */
} }
} /* end loop over hlst slots */ } /* end loop over hlst slots */
@ -879,7 +883,7 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
rc = tdbio_write_record( &rec ); rc = tdbio_write_record( &rec );
if( rc ) { if( rc ) {
log_error( "upd_hashtable: write new hlst failed: %s\n", log_error( "upd_hashtable: write new hlst failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
/* update the hashtable record */ /* update the hashtable record */
@ -887,14 +891,14 @@ upd_hashtable( ulong table, byte *key, int keylen, ulong newrecnum )
rc = tdbio_write_record( &lastrec ); rc = tdbio_write_record( &lastrec );
if( rc ) if( rc )
log_error( "upd_hashtable: update htbl failed: %s\n", log_error( "upd_hashtable: update htbl failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; /* ready */ return rc; /* ready */
} }
else { else {
log_error( "hashtbl %lu: %lu/%d points to an invalid record %lu\n", log_error( "hashtbl %lu: %lu/%d points to an invalid record %lu\n",
table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item); table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
list_trustdb(NULL); list_trustdb(NULL);
return G10ERR_TRUSTDB; return GPG_ERR_TRUSTDB;
} }
} }
@ -922,7 +926,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL ); rc = tdbio_read_record( hashrec, &rec, RECTYPE_HTBL );
if( rc ) { if( rc ) {
log_error( db_name, "drop_from_hashtable: read failed: %s\n", log_error( db_name, "drop_from_hashtable: read failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
@ -935,14 +939,14 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
rc = tdbio_write_record( &rec ); rc = tdbio_write_record( &rec );
if( rc ) if( rc )
log_error( db_name, "drop_from_hashtable: write htbl failed: %s\n", log_error( db_name, "drop_from_hashtable: write htbl failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
rc = tdbio_read_record( item, &rec, 0 ); rc = tdbio_read_record( item, &rec, 0 );
if( rc ) { if( rc ) {
log_error( "drop_from_hashtable: read item failed: %s\n", log_error( "drop_from_hashtable: read item failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
@ -951,7 +955,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
level++; level++;
if( level >= keylen ) { if( level >= keylen ) {
log_error( "hashtable has invalid indirections.\n"); log_error( "hashtable has invalid indirections.\n");
return G10ERR_TRUSTDB; return GPG_ERR_TRUSTDB;
} }
goto next_level; goto next_level;
} }
@ -964,7 +968,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
rc = tdbio_write_record( &rec ); rc = tdbio_write_record( &rec );
if( rc ) if( rc )
log_error( db_name, "drop_from_hashtable: write htbl failed: %s\n", log_error( db_name, "drop_from_hashtable: write htbl failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
} }
@ -973,7 +977,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
&rec, RECTYPE_HLST); &rec, RECTYPE_HLST);
if( rc ) { if( rc ) {
log_error( "drop_from_hashtable: read hlst failed: %s\n", log_error( "drop_from_hashtable: read hlst failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
} }
@ -984,7 +988,7 @@ drop_from_hashtable( ulong table, byte *key, int keylen, ulong recnum )
log_error( "hashtbl %lu: %lu/%d points to wrong record %lu\n", log_error( "hashtbl %lu: %lu/%d points to wrong record %lu\n",
table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item); table, hashrec, (msb % ITEMS_PER_HTBL_RECORD), item);
return G10ERR_TRUSTDB; return GPG_ERR_TRUSTDB;
} }
@ -1010,7 +1014,7 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
hashrec += msb / ITEMS_PER_HTBL_RECORD; hashrec += msb / ITEMS_PER_HTBL_RECORD;
rc = tdbio_read_record( hashrec, rec, RECTYPE_HTBL ); rc = tdbio_read_record( hashrec, rec, RECTYPE_HTBL );
if( rc ) { if( rc ) {
log_error( db_name, "lookup_hashtable failed: %s\n", g10_errstr(rc) ); log_error( db_name, "lookup_hashtable failed: %s\n", gpg_strerror (rc) );
return rc; return rc;
} }
@ -1020,7 +1024,7 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
rc = tdbio_read_record( item, rec, 0 ); rc = tdbio_read_record( item, rec, 0 );
if( rc ) { if( rc ) {
log_error( db_name, "hashtable read failed: %s\n", g10_errstr(rc) ); log_error( db_name, "hashtable read failed: %s\n", gpg_strerror (rc) );
return rc; return rc;
} }
if( rec->rectype == RECTYPE_HTBL ) { if( rec->rectype == RECTYPE_HTBL ) {
@ -1028,7 +1032,7 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
level++; level++;
if( level >= keylen ) { if( level >= keylen ) {
log_error( db_name, "hashtable has invalid indirections\n"); log_error( db_name, "hashtable has invalid indirections\n");
return G10ERR_TRUSTDB; return GPG_ERR_TRUSTDB;
} }
goto next_level; goto next_level;
} }
@ -1043,7 +1047,7 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
rc = tdbio_read_record( rec->r.hlst.rnum[i], &tmp, 0 ); rc = tdbio_read_record( rec->r.hlst.rnum[i], &tmp, 0 );
if( rc ) { if( rc ) {
log_error( "lookup_hashtable: read item failed: %s\n", log_error( "lookup_hashtable: read item failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
if( (*cmpfnc)( cmpdata, &tmp ) ) { if( (*cmpfnc)( cmpdata, &tmp ) ) {
@ -1056,7 +1060,7 @@ lookup_hashtable( ulong table, const byte *key, size_t keylen,
rc = tdbio_read_record( rec->r.hlst.next, rec, RECTYPE_HLST ); rc = tdbio_read_record( rec->r.hlst.next, rec, RECTYPE_HLST );
if( rc ) { if( rc ) {
log_error( "lookup_hashtable: read hlst failed: %s\n", log_error( "lookup_hashtable: read hlst failed: %s\n",
g10_errstr(rc) ); gpg_strerror (rc) );
return rc; return rc;
} }
} }
@ -1159,17 +1163,19 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
buf = get_record_from_cache( recnum ); buf = get_record_from_cache( recnum );
if( !buf ) { if( !buf ) {
if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) { if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
rc = gpg_error_from_errno (errno);
log_error(_("trustdb: lseek failed: %s\n"), strerror(errno) ); log_error(_("trustdb: lseek failed: %s\n"), strerror(errno) );
return G10ERR_READ_FILE; return rc;
} }
n = read( db_fd, readbuf, TRUST_RECORD_LEN); n = read( db_fd, readbuf, TRUST_RECORD_LEN);
if( !n ) { if( !n ) {
return -1; /* eof */ return -1; /* eof */
} }
else if( n != TRUST_RECORD_LEN ) { else if( n != TRUST_RECORD_LEN ) {
rc = gpg_error_from_errno (errno);
log_error(_("trustdb: read failed (n=%d): %s\n"), n, log_error(_("trustdb: read failed (n=%d): %s\n"), n,
strerror(errno) ); strerror(errno) );
return G10ERR_READ_FILE; return rc;
} }
buf = readbuf; buf = readbuf;
} }
@ -1180,7 +1186,7 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
if( expected && rec->rectype != expected ) { if( expected && rec->rectype != expected ) {
log_error("%lu: read expected rec type %d, got %d\n", log_error("%lu: read expected rec type %d, got %d\n",
recnum, expected, rec->rectype ); recnum, expected, rec->rectype );
return G10ERR_TRUSTDB; return GPG_ERR_TRUSTDB;
} }
p++; /* skip reserved byte */ p++; /* skip reserved byte */
switch( rec->rectype ) { switch( rec->rectype ) {
@ -1189,7 +1195,7 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
case RECTYPE_VER: /* version record */ case RECTYPE_VER: /* version record */
if( memcmp(buf+1, "gpg", 3 ) ) { if( memcmp(buf+1, "gpg", 3 ) ) {
log_error( _("%s: not a trustdb file\n"), db_name ); log_error( _("%s: not a trustdb file\n"), db_name );
rc = G10ERR_TRUSTDB; rc = GPG_ERR_TRUSTDB;
} }
p += 2; /* skip "gpg" */ p += 2; /* skip "gpg" */
rec->r.ver.version = *p++; rec->r.ver.version = *p++;
@ -1208,12 +1214,12 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
if( recnum ) { if( recnum ) {
log_error( _("%s: version record with recnum %lu\n"), db_name, log_error( _("%s: version record with recnum %lu\n"), db_name,
(ulong)recnum ); (ulong)recnum );
rc = G10ERR_TRUSTDB; rc = GPG_ERR_TRUSTDB;
} }
else if( rec->r.ver.version != 3 ) { else if( rec->r.ver.version != 3 ) {
log_error( _("%s: invalid file version %d\n"), db_name, log_error( _("%s: invalid file version %d\n"), db_name,
rec->r.ver.version ); rec->r.ver.version );
rc = G10ERR_TRUSTDB; rc = GPG_ERR_TRUSTDB;
} }
break; break;
case RECTYPE_FREE: case RECTYPE_FREE:
@ -1248,7 +1254,7 @@ tdbio_read_record( ulong recnum, TRUSTREC *rec, int expected )
default: default:
log_error( "%s: invalid record type %d at recnum %lu\n", log_error( "%s: invalid record type %d at recnum %lu\n",
db_name, rec->rectype, (ulong)recnum ); db_name, rec->rectype, (ulong)recnum );
rc = G10ERR_TRUSTDB; rc = GPG_ERR_TRUSTDB;
break; break;
} }
@ -1364,7 +1370,7 @@ tdbio_delete_record( ulong recnum )
rc = tdbio_read_record( 0, &vr, RECTYPE_VER ); rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
if( rc ) if( rc )
log_fatal( _("%s: error reading version record: %s\n"), log_fatal( _("%s: error reading version record: %s\n"),
db_name, g10_errstr(rc) ); db_name, gpg_strerror (rc) );
rec.recnum = recnum; rec.recnum = recnum;
rec.rectype = RECTYPE_FREE; rec.rectype = RECTYPE_FREE;
@ -1391,13 +1397,13 @@ tdbio_new_recnum()
rc = tdbio_read_record( 0, &vr, RECTYPE_VER ); rc = tdbio_read_record( 0, &vr, RECTYPE_VER );
if( rc ) if( rc )
log_fatal( _("%s: error reading version record: %s\n"), log_fatal( _("%s: error reading version record: %s\n"),
db_name, g10_errstr(rc) ); db_name, gpg_strerror (rc) );
if( vr.r.ver.firstfree ) { if( vr.r.ver.firstfree ) {
recnum = vr.r.ver.firstfree; recnum = vr.r.ver.firstfree;
rc = tdbio_read_record( recnum, &rec, RECTYPE_FREE ); rc = tdbio_read_record( recnum, &rec, RECTYPE_FREE );
if( rc ) { if( rc ) {
log_error( _("%s: error reading free record: %s\n"), log_error( _("%s: error reading free record: %s\n"),
db_name, g10_errstr(rc) ); db_name, gpg_strerror (rc) );
return rc; return rc;
} }
/* update dir record */ /* update dir record */
@ -1405,7 +1411,7 @@ tdbio_new_recnum()
rc = tdbio_write_record( &vr ); rc = tdbio_write_record( &vr );
if( rc ) { if( rc ) {
log_error( _("%s: error writing dir record: %s\n"), log_error( _("%s: error writing dir record: %s\n"),
db_name, g10_errstr(rc) ); db_name, gpg_strerror (rc) );
return rc; return rc;
} }
/*zero out the new record */ /*zero out the new record */
@ -1415,7 +1421,7 @@ tdbio_new_recnum()
rc = tdbio_write_record( &rec ); rc = tdbio_write_record( &rec );
if( rc ) if( rc )
log_fatal(_("%s: failed to zero a record: %s\n"), log_fatal(_("%s: failed to zero a record: %s\n"),
db_name, g10_errstr(rc)); db_name, gpg_strerror (rc));
} }
else { /* not found, append a new record */ else { /* not found, append a new record */
offset = lseek( db_fd, 0, SEEK_END ); offset = lseek( db_fd, 0, SEEK_END );
@ -1430,22 +1436,22 @@ tdbio_new_recnum()
rec.recnum = recnum; rec.recnum = recnum;
rc = 0; rc = 0;
if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) { if( lseek( db_fd, recnum * TRUST_RECORD_LEN, SEEK_SET ) == -1 ) {
rc = gpg_error_from_errno (errno);
log_error(_("trustdb rec %lu: lseek failed: %s\n"), log_error(_("trustdb rec %lu: lseek failed: %s\n"),
recnum, strerror(errno) ); recnum, strerror(errno) );
rc = G10ERR_WRITE_FILE;
} }
else { else {
int n = write( db_fd, &rec, TRUST_RECORD_LEN); int n = write( db_fd, &rec, TRUST_RECORD_LEN);
if( n != TRUST_RECORD_LEN ) { if( n != TRUST_RECORD_LEN ) {
rc = gpg_error_from_errno (errno);
log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"), log_error(_("trustdb rec %lu: write failed (n=%d): %s\n"),
recnum, n, strerror(errno) ); recnum, n, strerror(errno) );
rc = G10ERR_WRITE_FILE;
} }
} }
if( rc ) if( rc )
log_fatal(_("%s: failed to append a record: %s\n"), log_fatal(_("%s: failed to append a record: %s\n"),
db_name, g10_errstr(rc)); db_name, gpg_strerror (rc));
} }
return recnum ; return recnum ;
} }
@ -1516,7 +1522,7 @@ migrate_from_v2 ()
int rc, count; int rc, count;
ottable_size = 5; ottable_size = 5;
ottable = m_alloc (ottable_size * sizeof *ottable); ottable = xmalloc (ottable_size * sizeof *ottable);
ottable_used = 0; ottable_used = 0;
/* We have some restrictions here. We can't use the version record /* We have some restrictions here. We can't use the version record
@ -1546,7 +1552,7 @@ migrate_from_v2 ()
if (ottable_used == ottable_size) if (ottable_used == ottable_size)
{ {
ottable_size += 1000; ottable_size += 1000;
ottable = m_realloc (ottable, ottable_size * sizeof *ottable); ottable = xrealloc (ottable, ottable_size * sizeof *ottable);
} }
ottable[ottable_used].keyrecno = buftoulong (oldbuf+6); ottable[ottable_used].keyrecno = buftoulong (oldbuf+6);
ottable[ottable_used].ot = oldbuf[18]; ottable[ottable_used].ot = oldbuf[18];
@ -1617,8 +1623,5 @@ migrate_from_v2 ()
if (rc) if (rc)
log_fatal ("failed to sync `%s'\n", db_name); log_fatal ("failed to sync `%s'\n", db_name);
log_info ("migrated %d version 2 ownertrusts\n", count); log_info ("migrated %d version 2 ownertrusts\n", count);
m_free (ottable); xfree (ottable);
} }

View File

@ -25,6 +25,7 @@
#include <errno.h> #include <errno.h>
#include <assert.h> #include <assert.h>
#include "gpg.h"
#include "errors.h" #include "errors.h"
#include "iobuf.h" #include "iobuf.h"
#include "memory.h" #include "memory.h"
@ -71,7 +72,7 @@ len_without_trailing_ws( byte *line, unsigned len )
static int static int
standard( text_filter_context_t *tfx, IOBUF a, standard( text_filter_context_t *tfx, iobuf_t a,
byte *buf, size_t size, size_t *ret_len) byte *buf, size_t size, size_t *ret_len)
{ {
int rc=0; int rc=0;
@ -120,7 +121,7 @@ standard( text_filter_context_t *tfx, IOBUF a,
*/ */
int int
text_filter( void *opaque, int control, text_filter( void *opaque, int control,
IOBUF a, byte *buf, size_t *ret_len) iobuf_t a, byte *buf, size_t *ret_len)
{ {
size_t size = *ret_len; size_t size = *ret_len;
text_filter_context_t *tfx = opaque; text_filter_context_t *tfx = opaque;
@ -133,7 +134,7 @@ text_filter( void *opaque, int control,
if( tfx->truncated ) if( tfx->truncated )
log_error(_("can't handle text lines longer than %d characters\n"), log_error(_("can't handle text lines longer than %d characters\n"),
MAX_LINELEN ); MAX_LINELEN );
m_free( tfx->buffer ); xfree ( tfx->buffer );
tfx->buffer = NULL; tfx->buffer = NULL;
} }
else if( control == IOBUFCTRL_DESC ) else if( control == IOBUFCTRL_DESC )
@ -147,7 +148,7 @@ text_filter( void *opaque, int control,
* md is updated as required by rfc2440 * md is updated as required by rfc2440
*/ */
int int
copy_clearsig_text( IOBUF out, IOBUF inp, MD_HANDLE md, copy_clearsig_text( iobuf_t out, iobuf_t inp, MD_HANDLE md,
int escape_dash, int escape_from, int pgp2mode ) int escape_dash, int escape_from, int pgp2mode )
{ {
unsigned maxlen; unsigned maxlen;
@ -175,15 +176,15 @@ copy_clearsig_text( IOBUF out, IOBUF inp, MD_HANDLE md,
/* update the message digest */ /* update the message digest */
if( escape_dash ) { if( escape_dash ) {
if( pending_lf ) { if( pending_lf ) {
md_putc( md, '\r' ); gcry_md_putc( md, '\r' );
md_putc( md, '\n' ); gcry_md_putc( md, '\n' );
} }
md_write( md, buffer, gcry_md_write( md, buffer,
len_without_trailing_chars( buffer, n, len_without_trailing_chars( buffer, n,
pgp2mode? " \r\n":" \t\r\n")); pgp2mode? " \r\n":" \t\r\n"));
} }
else else
md_write( md, buffer, n ); gcry_md_write( md, buffer, n );
pending_lf = buffer[n-1] == '\n'; pending_lf = buffer[n-1] == '\n';
/* write the output */ /* write the output */
@ -224,7 +225,7 @@ copy_clearsig_text( IOBUF out, IOBUF inp, MD_HANDLE md,
if( !pending_lf ) { /* make sure that the file ends with a LF */ if( !pending_lf ) { /* make sure that the file ends with a LF */
iobuf_writestr( out, LF ); iobuf_writestr( out, LF );
if( !escape_dash ) if( !escape_dash )
md_putc( md, '\n' ); gcry_md_putc( md, '\n' );
} }
if( truncated ) if( truncated )

View File

@ -34,6 +34,7 @@
#endif #endif
#endif /* !DISABLE_REGEX */ #endif /* !DISABLE_REGEX */
#include "gpg.h"
#include "errors.h" #include "errors.h"
#include "iobuf.h" #include "iobuf.h"
#include "keydb.h" #include "keydb.h"
@ -98,7 +99,7 @@ new_key_item (void)
{ {
struct key_item *k; struct key_item *k;
k = m_alloc_clear (sizeof *k); k = xcalloc (1,sizeof *k);
return k; return k;
} }
@ -110,8 +111,8 @@ release_key_items (struct key_item *k)
for (; k; k = k2) for (; k; k = k2)
{ {
k2 = k->next; k2 = k->next;
m_free (k->trust_regexp); xfree (k->trust_regexp);
m_free (k); xfree (k);
} }
} }
@ -128,7 +129,7 @@ new_key_hash_table (void)
{ {
struct key_item **tbl; struct key_item **tbl;
tbl = m_alloc_clear (1024 * sizeof *tbl); tbl = xcalloc (1,1024 * sizeof *tbl);
return tbl; return tbl;
} }
@ -141,7 +142,7 @@ release_key_hash_table (KeyHashTable tbl)
return; return;
for (i=0; i < 1024; i++) for (i=0; i < 1024; i++)
release_key_items (tbl[i]); release_key_items (tbl[i]);
m_free (tbl); xfree (tbl);
} }
/* /*
@ -188,7 +189,7 @@ release_key_array ( struct key_array *keys )
if (keys) { if (keys) {
for (k=keys; k->keyblock; k++) for (k=keys; k->keyblock; k++)
release_kbnode (k->keyblock); release_kbnode (k->keyblock);
m_free (keys); xfree (keys);
} }
} }
@ -335,7 +336,7 @@ read_record (ulong recno, TRUSTREC *rec, int rectype )
if (rc) if (rc)
{ {
log_error(_("trust record %lu, req type %d: read failed: %s\n"), log_error(_("trust record %lu, req type %d: read failed: %s\n"),
recno, rec->rectype, g10_errstr(rc) ); recno, rec->rectype, gpg_strerror (rc) );
tdbio_invalid(); tdbio_invalid();
} }
if (rectype != rec->rectype) if (rectype != rec->rectype)
@ -356,7 +357,7 @@ write_record (TRUSTREC *rec)
if (rc) if (rc)
{ {
log_error(_("trust record %lu, type %d: write failed: %s\n"), log_error(_("trust record %lu, type %d: write failed: %s\n"),
rec->recnum, rec->rectype, g10_errstr(rc) ); rec->recnum, rec->rectype, gpg_strerror (rc) );
tdbio_invalid(); tdbio_invalid();
} }
} }
@ -370,7 +371,7 @@ do_sync(void)
int rc = tdbio_sync (); int rc = tdbio_sync ();
if(rc) if(rc)
{ {
log_error (_("trustdb: sync failed: %s\n"), g10_errstr(rc) ); log_error (_("trustdb: sync failed: %s\n"), gpg_strerror (rc) );
g10_exit(2); g10_exit(2);
} }
} }
@ -399,7 +400,7 @@ setup_trustdb( int level, const char *dbname )
if( trustdb_args.init ) if( trustdb_args.init )
return 0; return 0;
trustdb_args.level = level; trustdb_args.level = level;
trustdb_args.dbname = dbname? m_strdup(dbname): NULL; trustdb_args.dbname = dbname? xstrdup (dbname): NULL;
return 0; return 0;
} }
@ -434,7 +435,7 @@ init_trustdb()
else else
BUG(); BUG();
if( rc ) if( rc )
log_fatal("can't init trustdb: %s\n", g10_errstr(rc) ); log_fatal("can't init trustdb: %s\n", gpg_strerror (rc) );
if(opt.trust_model==TM_AUTO) if(opt.trust_model==TM_AUTO)
{ {
@ -607,7 +608,7 @@ read_trust_record (PKT_public_key *pk, TRUSTREC *rec)
if (rc) if (rc)
{ {
log_error ("trustdb: searching trust record failed: %s\n", log_error ("trustdb: searching trust record failed: %s\n",
g10_errstr (rc)); gpg_strerror (rc));
return rc; return rc;
} }
@ -615,7 +616,7 @@ read_trust_record (PKT_public_key *pk, TRUSTREC *rec)
{ {
log_error ("trustdb: record %lu is not a trust record\n", log_error ("trustdb: record %lu is not a trust record\n",
rec->recnum); rec->recnum);
return G10ERR_TRUSTDB; return GPG_ERR_TRUSTDB;
} }
return 0; return 0;
@ -760,12 +761,12 @@ update_min_ownertrust (u32 *kid, unsigned int new_trust )
TRUSTREC rec; TRUSTREC rec;
int rc; int rc;
pk = m_alloc_clear (sizeof *pk); pk = xcalloc (1,sizeof *pk);
rc = get_pubkey (pk, kid); rc = get_pubkey (pk, kid);
if (rc) if (rc)
{ {
log_error (_("public key %08lX not found: %s\n"), log_error (_("public key %08lX not found: %s\n"),
(ulong)kid[1], g10_errstr(rc) ); (ulong)kid[1], gpg_strerror (rc) );
return; return;
} }
@ -1029,12 +1030,12 @@ get_validity (PKT_public_key *pk, PKT_user_id *uid)
keyid_from_pk (pk, kid); keyid_from_pk (pk, kid);
if (pk->main_keyid[0] != kid[0] || pk->main_keyid[1] != kid[1]) if (pk->main_keyid[0] != kid[0] || pk->main_keyid[1] != kid[1])
{ /* this is a subkey - get the mainkey */ { /* this is a subkey - get the mainkey */
main_pk = m_alloc_clear (sizeof *main_pk); main_pk = xcalloc (1,sizeof *main_pk);
rc = get_pubkey (main_pk, pk->main_keyid); rc = get_pubkey (main_pk, pk->main_keyid);
if (rc) if (rc)
{ {
log_error ("error getting main key %08lX of subkey %08lX: %s\n", log_error ("error getting main key %08lX of subkey %08lX: %s\n",
(ulong)pk->main_keyid[1], (ulong)kid[1], g10_errstr(rc)); (ulong)pk->main_keyid[1], (ulong)kid[1], gpg_strerror (rc));
validity = TRUST_UNKNOWN; validity = TRUST_UNKNOWN;
goto leave; goto leave;
} }
@ -1223,12 +1224,12 @@ ask_ownertrust (u32 *kid,int minimum)
int rc; int rc;
int ot; int ot;
pk = m_alloc_clear (sizeof *pk); pk = xcalloc (1,sizeof *pk);
rc = get_pubkey (pk, kid); rc = get_pubkey (pk, kid);
if (rc) if (rc)
{ {
log_error (_("public key %08lX not found: %s\n"), log_error (_("public key %08lX not found: %s\n"),
(ulong)kid[1], g10_errstr(rc) ); (ulong)kid[1], gpg_strerror (rc) );
return TRUST_UNKNOWN; return TRUST_UNKNOWN;
} }
@ -1712,14 +1713,14 @@ validate_key_list (KEYDB_HANDLE hd, KeyHashTable full_trust,
KEYDB_SEARCH_DESC desc; KEYDB_SEARCH_DESC desc;
maxkeys = 1000; maxkeys = 1000;
keys = m_alloc ((maxkeys+1) * sizeof *keys); keys = xmalloc ((maxkeys+1) * sizeof *keys);
nkeys = 0; nkeys = 0;
rc = keydb_search_reset (hd); rc = keydb_search_reset (hd);
if (rc) if (rc)
{ {
log_error ("keydb_search_reset failed: %s\n", g10_errstr(rc)); log_error ("keydb_search_reset failed: %s\n", gpg_strerror (rc));
m_free (keys); xfree (keys);
return NULL; return NULL;
} }
@ -1735,8 +1736,8 @@ validate_key_list (KEYDB_HANDLE hd, KeyHashTable full_trust,
} }
if (rc) if (rc)
{ {
log_error ("keydb_search_first failed: %s\n", g10_errstr(rc)); log_error ("keydb_search_first failed: %s\n", gpg_strerror (rc));
m_free (keys); xfree (keys);
return NULL; return NULL;
} }
@ -1748,8 +1749,8 @@ validate_key_list (KEYDB_HANDLE hd, KeyHashTable full_trust,
rc = keydb_get_keyblock (hd, &keyblock); rc = keydb_get_keyblock (hd, &keyblock);
if (rc) if (rc)
{ {
log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc)); log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
m_free (keys); xfree (keys);
return NULL; return NULL;
} }
@ -1781,7 +1782,7 @@ validate_key_list (KEYDB_HANDLE hd, KeyHashTable full_trust,
if (nkeys == maxkeys) { if (nkeys == maxkeys) {
maxkeys += 1000; maxkeys += 1000;
keys = m_realloc (keys, (maxkeys+1) * sizeof *keys); keys = xrealloc (keys, (maxkeys+1) * sizeof *keys);
} }
keys[nkeys++].keyblock = keyblock; keys[nkeys++].keyblock = keyblock;
@ -1804,8 +1805,8 @@ validate_key_list (KEYDB_HANDLE hd, KeyHashTable full_trust,
while ( !(rc = keydb_search (hd, &desc, 1)) ); while ( !(rc = keydb_search (hd, &desc, 1)) );
if (rc && rc != -1) if (rc && rc != -1)
{ {
log_error ("keydb_search_next failed: %s\n", g10_errstr(rc)); log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc));
m_free (keys); xfree (keys);
return NULL; return NULL;
} }
@ -1825,7 +1826,7 @@ reset_trust_records (KEYDB_HANDLE hd, KeyHashTable exclude)
rc = keydb_search_reset (hd); rc = keydb_search_reset (hd);
if (rc) if (rc)
{ {
log_error ("keydb_search_reset failed: %s\n", g10_errstr(rc)); log_error ("keydb_search_reset failed: %s\n", gpg_strerror (rc));
return; return;
} }
@ -1838,7 +1839,7 @@ reset_trust_records (KEYDB_HANDLE hd, KeyHashTable exclude)
} }
rc = keydb_search (hd, &desc, 1); rc = keydb_search (hd, &desc, 1);
if (rc && rc != -1 ) if (rc && rc != -1 )
log_error ("keydb_search_first failed: %s\n", g10_errstr(rc)); log_error ("keydb_search_first failed: %s\n", gpg_strerror (rc));
else if (!rc) else if (!rc)
{ {
desc.mode = KEYDB_SEARCH_MODE_NEXT; /* change mode */ desc.mode = KEYDB_SEARCH_MODE_NEXT; /* change mode */
@ -1847,7 +1848,7 @@ reset_trust_records (KEYDB_HANDLE hd, KeyHashTable exclude)
rc = keydb_get_keyblock (hd, &keyblock); rc = keydb_get_keyblock (hd, &keyblock);
if (rc) if (rc)
{ {
log_error ("keydb_get_keyblock failed: %s\n", g10_errstr(rc)); log_error ("keydb_get_keyblock failed: %s\n", gpg_strerror (rc));
break; break;
} }
count++; count++;
@ -1860,7 +1861,7 @@ reset_trust_records (KEYDB_HANDLE hd, KeyHashTable exclude)
} }
while ( !(rc = keydb_search (hd, &desc, 1)) ); while ( !(rc = keydb_search (hd, &desc, 1)) );
if (rc && rc != -1) if (rc && rc != -1)
log_error ("keydb_search_next failed: %s\n", g10_errstr(rc)); log_error ("keydb_search_next failed: %s\n", gpg_strerror (rc));
} }
if (opt.verbose) if (opt.verbose)
log_info (_("%d keys processed (%d validity counts cleared)\n"), log_info (_("%d keys processed (%d validity counts cleared)\n"),
@ -2027,7 +2028,7 @@ validate_keys (int interactive)
if (!keys) if (!keys)
{ {
log_error ("validate_key_list failed\n"); log_error ("validate_key_list failed\n");
rc = G10ERR_GENERAL; rc = GPG_ERR_GENERAL;
goto leave; goto leave;
} }
@ -2081,7 +2082,7 @@ validate_keys (int interactive)
kar->keyblock->pkt->pkt.public_key->trust_value; kar->keyblock->pkt->pkt.public_key->trust_value;
if(kar->keyblock->pkt->pkt.public_key->trust_regexp) if(kar->keyblock->pkt->pkt.public_key->trust_regexp)
k->trust_regexp= k->trust_regexp=
m_strdup(kar->keyblock->pkt-> xstrdup (kar->keyblock->pkt->
pkt.public_key->trust_regexp); pkt.public_key->trust_regexp);
k->next = klist; k->next = klist;
klist = k; klist = k;
@ -2117,7 +2118,7 @@ validate_keys (int interactive)
if(tdbio_update_version_record()!=0) if(tdbio_update_version_record()!=0)
{ {
log_error(_("unable to update trustdb version record: " log_error(_("unable to update trustdb version record: "
"write failed: %s\n"), g10_errstr(rc)); "write failed: %s\n"), gpg_strerror (rc));
tdbio_invalid(); tdbio_invalid();
} }

View File

@ -54,7 +54,7 @@
int int
verify_signatures( int nfiles, char **files ) verify_signatures( int nfiles, char **files )
{ {
IOBUF fp; iobuf_t fp;
armor_filter_context_t afx; armor_filter_context_t afx;
progress_filter_context_t pfx; progress_filter_context_t pfx;
const char *sigfile; const char *sigfile;
@ -92,8 +92,10 @@ verify_signatures( int nfiles, char **files )
/* open the signature file */ /* open the signature file */
fp = iobuf_open(sigfile); fp = iobuf_open(sigfile);
if( !fp ) { if( !fp ) {
log_error(_("can't open `%s'\n"), print_fname_stdin(sigfile)); rc = gpg_error_from_errno (errno);
return G10ERR_OPEN_FILE; log_error(_("can't open `%s': %s\n"),
print_fname_stdin(sigfile), strerror (errno));
return rc;
} }
handle_progress (&pfx, fp, sigfile); handle_progress (&pfx, fp, sigfile);
@ -120,17 +122,17 @@ verify_signatures( int nfiles, char **files )
void void
print_file_status( int status, const char *name, int what ) print_file_status( int status, const char *name, int what )
{ {
char *p = m_alloc(strlen(name)+10); char *p = xmalloc (strlen(name)+10);
sprintf(p, "%d %s", what, name ); sprintf(p, "%d %s", what, name );
write_status_text( status, p ); write_status_text( status, p );
m_free(p); xfree (p);
} }
static int static int
verify_one_file( const char *name ) verify_one_file( const char *name )
{ {
IOBUF fp; iobuf_t fp;
armor_filter_context_t afx; armor_filter_context_t afx;
progress_filter_context_t pfx; progress_filter_context_t pfx;
int rc; int rc;
@ -138,9 +140,11 @@ verify_one_file( const char *name )
print_file_status( STATUS_FILE_START, name, 1 ); print_file_status( STATUS_FILE_START, name, 1 );
fp = iobuf_open(name); fp = iobuf_open(name);
if( !fp ) { if( !fp ) {
rc = gpg_error_from_errno (errno);
log_error(_("can't open `%s': %s\n"),
print_fname_stdin(name), strerror (errno));
print_file_status( STATUS_FILE_ERROR, name, 1 ); print_file_status( STATUS_FILE_ERROR, name, 1 );
log_error(_("can't open `%s'\n"), print_fname_stdin(name)); return rc;
return G10ERR_OPEN_FILE;
} }
handle_progress (&pfx, fp, name); handle_progress (&pfx, fp, name);
@ -175,7 +179,7 @@ verify_files( int nfiles, char **files )
lno++; lno++;
if( !*line || line[strlen(line)-1] != '\n' ) { if( !*line || line[strlen(line)-1] != '\n' ) {
log_error(_("input line %u too long or missing LF\n"), lno ); log_error(_("input line %u too long or missing LF\n"), lno );
return G10ERR_GENERAL; return GPG_ERR_GENERAL;
} }
/* This code does not work on MSDOS but how cares there are /* This code does not work on MSDOS but how cares there are
* also no script languages available. We don't strip any * also no script languages available. We don't strip any

View File

@ -1,3 +1,15 @@
2003-06-11 Werner Koch <wk@gnupg.org>
* cipher.h: Include gcrypt.h and mapped cipher algo names to
gcrypt ones. Removed twofish_old and skipjack. Removed all
handle definitions and other raerely used stuff. This file will
eventually be entirely removed.
2003-06-10 Werner Koch <wk@gnupg.org>
* types.h (struct strlist): Removed.
2003-05-24 David Shaw <dshaw@jabberwocky.com> 2003-05-24 David Shaw <dshaw@jabberwocky.com>
* cipher.h, i18n.h, iobuf.h, memory.h, mpi.h, types.h, util.h: * cipher.h, i18n.h, iobuf.h, memory.h, mpi.h, types.h, util.h:

View File

@ -22,46 +22,42 @@
#define DBG_CIPHER g10c_debug_mode #define DBG_CIPHER g10c_debug_mode
#include "mpi.h" #include <gcrypt.h>
#include "../cipher/random.h"
#define CIPHER_ALGO_NONE GCRY_CIPHER_NONE
#define CIPHER_ALGO_NONE 0 #define CIPHER_ALGO_IDEA GCRY_CIPHER_IDEA
#define CIPHER_ALGO_IDEA 1 #define CIPHER_ALGO_3DES GCRY_CIPHER_3DES
#define CIPHER_ALGO_3DES 2 #define CIPHER_ALGO_CAST5 GCRY_CIPHER_CAST5
#define CIPHER_ALGO_CAST5 3 #define CIPHER_ALGO_BLOWFISH GCRY_CIPHER_BLOWFISH /* 128 bit */
#define CIPHER_ALGO_BLOWFISH 4 /* blowfish 128 bit key */ #define CIPHER_ALGO_SAFER_SK128 GCRY_CIPHER_SK128
#define CIPHER_ALGO_SAFER_SK128 5 #define CIPHER_ALGO_DES_SK GCRY_CIPHER_DES_SK
#define CIPHER_ALGO_DES_SK 6 #define CIPHER_ALGO_AES GCRY_CIPHER_AES
#define CIPHER_ALGO_AES 7 #define CIPHER_ALGO_AES192 GCRY_CIPHER_AES192
#define CIPHER_ALGO_AES192 8 #define CIPHER_ALGO_AES256 GCRY_CIPHER_AES256
#define CIPHER_ALGO_AES256 9
#define CIPHER_ALGO_RIJNDAEL CIPHER_ALGO_AES #define CIPHER_ALGO_RIJNDAEL CIPHER_ALGO_AES
#define CIPHER_ALGO_RIJNDAEL192 CIPHER_ALGO_AES192 #define CIPHER_ALGO_RIJNDAEL192 CIPHER_ALGO_AES192
#define CIPHER_ALGO_RIJNDAEL256 CIPHER_ALGO_AES256 #define CIPHER_ALGO_RIJNDAEL256 CIPHER_ALGO_AES256
#define CIPHER_ALGO_TWOFISH 10 /* twofish 256 bit */ #define CIPHER_ALGO_TWOFISH GCRY_CIPHER_TWOFISH /* 256 bit */
#define CIPHER_ALGO_SKIPJACK 101 /* experimental: skipjack */
#define CIPHER_ALGO_TWOFISH_OLD 102 /* experimental: twofish 128 bit */
#define CIPHER_ALGO_DUMMY 110 /* no encryption at all */ #define CIPHER_ALGO_DUMMY 110 /* no encryption at all */
#define PUBKEY_ALGO_RSA 1 #define PUBKEY_ALGO_RSA GCRY_PK_RSA
#define PUBKEY_ALGO_RSA_E 2 /* RSA encrypt only */ #define PUBKEY_ALGO_RSA_E GCRY_PK_RSA_E
#define PUBKEY_ALGO_RSA_S 3 /* RSA sign only */ #define PUBKEY_ALGO_RSA_S GCRY_PK_RSA_S
#define PUBKEY_ALGO_ELGAMAL_E 16 /* encrypt only ElGamal (but not for v3)*/ #define PUBKEY_ALGO_ELGAMAL_E GCRY_PK_ELG_E
#define PUBKEY_ALGO_DSA 17 #define PUBKEY_ALGO_DSA GCRY_PK_DSA
#define PUBKEY_ALGO_ELGAMAL 20 /* sign and encrypt elgamal */ #define PUBKEY_ALGO_ELGAMAL GCRY_PK_ELG
#define PUBKEY_USAGE_SIG 1 /* key is good for signatures */ #define PUBKEY_USAGE_SIG GCRY_PK_USAGE_SIGN
#define PUBKEY_USAGE_ENC 2 /* key is good for encryption */ #define PUBKEY_USAGE_ENC GCRY_PK_USAGE_ENCR
#define PUBKEY_USAGE_CERT 4 /* key is also good to certify other keys*/ #define PUBKEY_USAGE_CERT 4 /* key is also good to certify other keys*/
#define DIGEST_ALGO_MD5 1 #define DIGEST_ALGO_MD5 GCRY_MD_MD5
#define DIGEST_ALGO_SHA1 2 #define DIGEST_ALGO_SHA1 GCRY_MD_SHA1
#define DIGEST_ALGO_RMD160 3 #define DIGEST_ALGO_RMD160 GCRY_MD_RMD160
#define DIGEST_ALGO_TIGER 6 #define DIGEST_ALGO_TIGER GCRY_MD_TIGER
#define DIGEST_ALGO_SHA256 8 #define DIGEST_ALGO_SHA256 GCRY_MD_SHA256
#define DIGEST_ALGO_SHA384 9 #define DIGEST_ALGO_SHA384 GCRY_MD_SHA384
#define DIGEST_ALGO_SHA512 10 #define DIGEST_ALGO_SHA512 GCRY_MD_SHA512
#define COMPRESS_ALGO_NONE 0 #define COMPRESS_ALGO_NONE 0
#define COMPRESS_ALGO_ZIP 1 #define COMPRESS_ALGO_ZIP 1
@ -79,30 +75,6 @@ typedef struct {
byte key[32]; /* this is the largest used keylen (256 bit) */ byte key[32]; /* this is the largest used keylen (256 bit) */
} DEK; } DEK;
struct cipher_handle_s;
typedef struct cipher_handle_s *CIPHER_HANDLE;
#define CIPHER_MODE_ECB 1
#define CIPHER_MODE_CFB 2
#define CIPHER_MODE_PHILS_CFB 3
#define CIPHER_MODE_AUTO_CFB 4
#define CIPHER_MODE_DUMMY 5 /* used with algo DUMMY for no encryption */
#define CIPHER_MODE_CBC 6
struct md_digest_list_s;
struct gcry_md_context {
int secure;
FILE *debug;
int finalized;
struct md_digest_list_s *list;
int bufcount;
int bufsize;
byte buffer[1];
};
typedef struct gcry_md_context *MD_HANDLE;
#ifndef EXTERN_UNLESS_MAIN_MODULE #ifndef EXTERN_UNLESS_MAIN_MODULE
#if defined (__riscos__) && !defined (INCLUDED_BY_MAIN_MODULE) #if defined (__riscos__) && !defined (INCLUDED_BY_MAIN_MODULE)
@ -116,90 +88,13 @@ EXTERN_UNLESS_MAIN_MODULE int g10_opt_verbose;
EXTERN_UNLESS_MAIN_MODULE const char *g10_opt_homedir; EXTERN_UNLESS_MAIN_MODULE const char *g10_opt_homedir;
/*-- dynload.c --*/
void register_cipher_extension( const char *mainpgm, const char *fname );
/*-- md.c --*/
int string_to_digest_algo( const char *string );
const char * digest_algo_to_string( int algo );
int check_digest_algo( int algo );
MD_HANDLE md_open( int algo, int secure );
void md_enable( MD_HANDLE hd, int algo );
MD_HANDLE md_copy( MD_HANDLE a );
void md_reset( MD_HANDLE a );
void md_close(MD_HANDLE a);
void md_write( MD_HANDLE a, const byte *inbuf, size_t inlen);
void md_final(MD_HANDLE a);
byte *md_read( MD_HANDLE a, int algo );
int md_digest( MD_HANDLE a, int algo, byte *buffer, int buflen );
int md_get_algo( MD_HANDLE a );
int md_algo_present( MD_HANDLE a, int algo );
int md_digest_length( int algo );
const byte *md_asn_oid( int algo, size_t *asnlen, size_t *mdlen );
void md_start_debug( MD_HANDLE a, const char *suffix );
void md_stop_debug( MD_HANDLE a );
#define md_is_secure(a) ((a)->secure)
#define md_putc(h,c) \
do { \
if( (h)->bufcount == (h)->bufsize ) \
md_write( (h), NULL, 0 ); \
(h)->buffer[(h)->bufcount++] = (c) & 0xff; \
} while(0)
void rmd160_hash_buffer (char *outbuf, const char *buffer, size_t length);
/*-- cipher.c --*/
int string_to_cipher_algo( const char *string );
const char * cipher_algo_to_string( int algo );
void disable_cipher_algo( int algo );
int check_cipher_algo( int algo );
unsigned cipher_get_keylen( int algo );
unsigned cipher_get_blocksize( int algo );
CIPHER_HANDLE cipher_open( int algo, int mode, int secure );
void cipher_close( CIPHER_HANDLE c );
int cipher_setkey( CIPHER_HANDLE c, byte *key, unsigned keylen );
void cipher_setiv( CIPHER_HANDLE c, const byte *iv, unsigned ivlen );
void cipher_encrypt( CIPHER_HANDLE c, byte *out, byte *in, unsigned nbytes );
void cipher_decrypt( CIPHER_HANDLE c, byte *out, byte *in, unsigned nbytes );
void cipher_sync( CIPHER_HANDLE c );
/*-- pubkey.c --*/
#define PUBKEY_MAX_NPKEY 4 #define PUBKEY_MAX_NPKEY 4
#define PUBKEY_MAX_NSKEY 6 #define PUBKEY_MAX_NSKEY 6
#define PUBKEY_MAX_NSIG 2 #define PUBKEY_MAX_NSIG 2
#define PUBKEY_MAX_NENC 2 #define PUBKEY_MAX_NENC 2
int string_to_pubkey_algo( const char *string ); #define MD_HANDLE gcry_md_hd_t
const char * pubkey_algo_to_string( int algo ); #define CIPHER_HANDLE gcry_cipher_hd_t
void disable_pubkey_algo( int algo );
int check_pubkey_algo( int algo );
int check_pubkey_algo2( int algo, unsigned use );
int pubkey_get_npkey( int algo );
int pubkey_get_nskey( int algo );
int pubkey_get_nsig( int algo );
int pubkey_get_nenc( int algo );
unsigned pubkey_nbits( int algo, MPI *pkey );
int pubkey_generate( int algo, unsigned nbits, MPI *skey, MPI **retfactors );
int pubkey_check_secret_key( int algo, MPI *skey );
int pubkey_encrypt( int algo, MPI *resarr, MPI data, MPI *pkey );
int pubkey_decrypt( int algo, MPI *result, MPI *data, MPI *skey );
int pubkey_sign( int algo, MPI *resarr, MPI hash, MPI *skey );
int pubkey_verify( int algo, MPI hash, MPI *data, MPI *pkey,
int (*cmp)(void *, MPI), void *opaque );
/*-- smallprime.c --*/
extern ushort small_prime_numbers[];
/*-- primegen.c --*/
void register_primegen_progress ( void (*cb)( void *, int), void *cb_data );
MPI generate_secret_prime( unsigned nbits );
MPI generate_public_prime( unsigned nbits );
MPI generate_elg_prime( int mode, unsigned pbits, unsigned qbits,
MPI g, MPI **factors );
/*-- elsewhere --*/
void register_pk_dsa_progress ( void (*cb)( void *, int), void *cb_data );
void register_pk_elg_progress ( void (*cb)( void *, int), void *cb_data );
#endif /*G10_CIPHER_H*/ #endif /*G10_CIPHER_H*/

View File

@ -1,4 +1,4 @@
/* errors.h - erro code /* errors.h - error code
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
* *
* This file is part of GNUPG. * This file is part of GNUPG.
@ -25,6 +25,8 @@
#error from libgpg-error. The numerical values are identical, though. #error from libgpg-error. The numerical values are identical, though.
#endif #endif
#if 0 /* Not used anymore. */
#define G10ERR_GENERAL 1 #define G10ERR_GENERAL 1
#define G10ERR_UNKNOWN_PACKET 2 #define G10ERR_UNKNOWN_PACKET 2
#define G10ERR_UNKNOWN_VERSION 3 /* Unknown version (in packet) */ #define G10ERR_UNKNOWN_VERSION 3 /* Unknown version (in packet) */
@ -80,10 +82,20 @@
#define G10ERR_UNU_PUBKEY 53 #define G10ERR_UNU_PUBKEY 53
#define G10ERR_UNU_SECKEY 54 #define G10ERR_UNU_SECKEY 54
#define G10ERR_KEYSERVER 55 #define G10ERR_KEYSERVER 55
#endif
#ifndef HAVE_STRERROR #ifndef HAVE_STRERROR
char *strerror( int n ); char *strerror( int n );
#endif #endif
#endif /*GNUPG_INCLUDE_ERRORS_H*/ #endif /*GNUPG_INCLUDE_ERRORS_H*/

View File

@ -1,161 +0,0 @@
/* iobuf.h - I/O buffer
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
*
* 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
*/
#ifndef G10_IOBUF_H
#define G10_IOBUF_H
#include "types.h"
#define DBG_IOBUF iobuf_debug_mode
#define IOBUFCTRL_INIT 1
#define IOBUFCTRL_FREE 2
#define IOBUFCTRL_UNDERFLOW 3
#define IOBUFCTRL_FLUSH 4
#define IOBUFCTRL_DESC 5
#define IOBUFCTRL_CANCEL 6
#define IOBUFCTRL_USER 16
typedef struct iobuf_struct *IOBUF;
/* fixme: we should hide most of this stuff */
struct iobuf_struct {
int use; /* 1 input , 2 output, 3 temp */
off_t nlimit;
off_t nbytes; /* used together with nlimit */
off_t ntotal; /* total bytes read (position of stream) */
int nofast; /* used by the iobuf_get() */
void *directfp;
struct {
size_t size; /* allocated size */
size_t start; /* number of invalid bytes at the begin of the buffer */
size_t len; /* currently filled to this size */
byte *buf;
} d;
int filter_eof;
int error;
int (*filter)( void *opaque, int control,
IOBUF chain, byte *buf, size_t *len);
void *filter_ov; /* value for opaque */
int filter_ov_owner;
char *real_fname;
IOBUF chain; /* next iobuf used for i/o if any (passed to filter) */
int no, subno;
const char *desc;
void *opaque; /* can be used to hold any information */
/* this value is copied to all instances */
struct {
size_t size; /* allocated size */
size_t start; /* number of invalid bytes at the begin of the buffer */
size_t len; /* currently filled to this size */
byte *buf;
} unget;
};
#ifndef EXTERN_UNLESS_MAIN_MODULE
#if defined (__riscos__) && !defined (INCLUDED_BY_MAIN_MODULE)
#define EXTERN_UNLESS_MAIN_MODULE extern
#else
#define EXTERN_UNLESS_MAIN_MODULE
#endif
#endif
EXTERN_UNLESS_MAIN_MODULE int iobuf_debug_mode;
void iobuf_enable_special_filenames ( int yes );
IOBUF iobuf_alloc(int use, size_t bufsize);
IOBUF iobuf_temp(void);
IOBUF iobuf_temp_with_content( const char *buffer, size_t length );
IOBUF iobuf_open( const char *fname );
IOBUF iobuf_fdopen( int fd, const char *mode );
IOBUF iobuf_sockopen( int fd, const char *mode );
IOBUF iobuf_create( const char *fname );
IOBUF iobuf_append( const char *fname );
IOBUF iobuf_openrw( const char *fname );
int iobuf_ioctl ( IOBUF a, int cmd, int intval, void *ptrval );
int iobuf_close( IOBUF iobuf );
int iobuf_cancel( IOBUF iobuf );
int iobuf_push_filter( IOBUF a, int (*f)(void *opaque, int control,
IOBUF chain, byte *buf, size_t *len), void *ov );
int iobuf_push_filter2( IOBUF a,
int (*f)(void *opaque, int control,
IOBUF chain, byte *buf, size_t *len),
void *ov, int rel_ov );
int iobuf_flush(IOBUF a);
void iobuf_clear_eof(IOBUF a);
#define iobuf_set_error(a) do { (a)->error = 1; } while(0)
#define iobuf_error(a) ((a)->error)
void iobuf_set_limit( IOBUF a, off_t nlimit );
off_t iobuf_tell( IOBUF a );
int iobuf_seek( IOBUF a, off_t newpos );
int iobuf_readbyte(IOBUF a);
int iobuf_read(IOBUF a, byte *buf, unsigned buflen );
unsigned iobuf_read_line( IOBUF a, byte **addr_of_buffer,
unsigned *length_of_buffer, unsigned *max_length );
int iobuf_peek(IOBUF a, byte *buf, unsigned buflen );
int iobuf_writebyte(IOBUF a, unsigned c);
int iobuf_write(IOBUF a, byte *buf, unsigned buflen );
int iobuf_writestr(IOBUF a, const char *buf );
void iobuf_flush_temp( IOBUF temp );
int iobuf_write_temp( IOBUF a, IOBUF temp );
size_t iobuf_temp_to_buffer( IOBUF a, byte *buffer, size_t buflen );
void iobuf_unget_and_close_temp( IOBUF a, IOBUF temp );
off_t iobuf_get_filelength( IOBUF a );
#define IOBUF_FILELENGTH_LIMIT 0xffffffff
const char *iobuf_get_real_fname( IOBUF a );
const char *iobuf_get_fname( IOBUF a );
void iobuf_set_block_mode( IOBUF a, size_t n );
void iobuf_set_partial_block_mode( IOBUF a, size_t len );
int iobuf_in_block_mode( IOBUF a );
int iobuf_translate_file_handle ( int fd, int for_write );
/* get a byte form the iobuf; must check for eof prior to this function
* this function returns values in the range 0 .. 255 or -1 to indicate EOF
* iobuf_get_noeof() does not return -1 to indicate EOF, but masks the
* returned value to be in the range 0 ..255.
*/
#define iobuf_get(a) \
( ((a)->nofast || (a)->d.start >= (a)->d.len )? \
iobuf_readbyte((a)) : ( (a)->nbytes++, (a)->d.buf[(a)->d.start++] ) )
#define iobuf_get_noeof(a) (iobuf_get((a))&0xff)
/* write a byte to the iobuf and return true on write error
* This macro does only write the low order byte
*/
#define iobuf_put(a,c) iobuf_writebyte(a,c)
#define iobuf_where(a) "[don't know]"
#define iobuf_id(a) ((a)->no)
#define iobuf_get_temp_buffer(a) ( (a)->d.buf )
#define iobuf_get_temp_length(a) ( (a)->d.len )
#define iobuf_is_temp(a) ( (a)->use == 3 )
#endif /*G10_IOBUF_H*/

View File

@ -30,6 +30,11 @@
#ifndef G10_MPI_H #ifndef G10_MPI_H
#define G10_MPI_H #define G10_MPI_H
#include <gcrypt.h>
#if 0
#include <config.h> #include <config.h>
#include <stdio.h> #include <stdio.h>
#include "iobuf.h" #include "iobuf.h"
@ -192,5 +197,5 @@ void mpi_rshift( MPI x, MPI a, unsigned n );
/*-- mpi-inv.c --*/ /*-- mpi-inv.c --*/
void mpi_invm( MPI x, MPI u, MPI v ); void mpi_invm( MPI x, MPI u, MPI v );
#endif
#endif /*G10_MPI_H*/ #endif /*G10_MPI_H*/

View File

@ -1,40 +0,0 @@
/* ttyio.h
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc.
*
* 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
*/
#ifndef G10_TTYIO_H
#define G10_TTYIO_H
const char *tty_get_ttyname (void);
int tty_batchmode( int onoff );
#if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 )
void tty_printf (const char *fmt, ... ) __attribute__ ((format (printf,1,2)));
#else
void tty_printf (const char *fmt, ... );
#endif
void tty_print_string( byte *p, size_t n );
void tty_print_utf8_string( byte *p, size_t n );
void tty_print_utf8_string2( byte *p, size_t n, size_t max_n );
char *tty_get( const char *prompt );
char *tty_get_hidden( const char *prompt );
void tty_kill_prompt(void);
int tty_get_answer_is_yes( const char *prompt );
int tty_no_terminal(int onoff);
#endif /*G10_TTYIO_H*/

View File

@ -132,10 +132,4 @@ typedef union {
double g; double g;
} PROPERLY_ALIGNED_TYPE; } PROPERLY_ALIGNED_TYPE;
typedef struct string_list {
struct string_list *next;
unsigned int flags;
char d[1];
} *STRLIST;
#endif /*G10_TYPES_H*/ #endif /*G10_TYPES_H*/

View File

@ -20,6 +20,9 @@
#ifndef G10_UTIL_H #ifndef G10_UTIL_H
#define G10_UTIL_H #define G10_UTIL_H
#warning oops, using old util.h
#if 0 /* Dont use it anymore */
#if defined (__MINGW32__) || defined (__CYGWIN32__) #if defined (__MINGW32__) || defined (__CYGWIN32__)
#include <stdarg.h> #include <stdarg.h>
#endif #endif
@ -126,9 +129,6 @@ void g10_log_hexdump( const char *text, const char *buf, size_t len );
#define log_debug_f g10_log_debug_f #define log_debug_f g10_log_debug_f
/*-- errors.c --*/
const char * g10_errstr( int no );
/*-- argparse.c --*/ /*-- argparse.c --*/
int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts); int arg_parse( ARGPARSE_ARGS *arg, ARGPARSE_OPTS *opts);
int optfile_parse( FILE *fp, const char *filename, unsigned *lineno, int optfile_parse( FILE *fp, const char *filename, unsigned *lineno,
@ -177,15 +177,9 @@ int answer_is_yes( const char *s );
int answer_is_yes_no_quit( const char *s ); int answer_is_yes_no_quit( const char *s );
/*-- strgutil.c --*/ /*-- strgutil.c --*/
void free_strlist( STRLIST sl );
#define FREE_STRLIST(a) do { free_strlist((a)); (a) = NULL ; } while(0) #include "../jnlib/strlist.h"
STRLIST add_to_strlist( STRLIST *list, const char *string );
STRLIST add_to_strlist2( STRLIST *list, const char *string, int is_utf8 );
STRLIST append_to_strlist( STRLIST *list, const char *string );
STRLIST append_to_strlist2( STRLIST *list, const char *string, int is_utf8 );
STRLIST strlist_prev( STRLIST head, STRLIST node );
STRLIST strlist_last( STRLIST node );
char *pop_strlist( STRLIST *list );
const char *memistr( const char *buf, size_t buflen, const char *sub ); const char *memistr( const char *buf, size_t buflen, const char *sub );
const char *ascii_memistr( const char *buf, size_t buflen, const char *sub ); const char *ascii_memistr( const char *buf, size_t buflen, const char *sub );
char *mem2str( char *, const void *, size_t); char *mem2str( char *, const void *, size_t);
@ -301,4 +295,6 @@ void riscos_list_openfiles(void);
#endif /* !__RISCOS__C__ */ #endif /* !__RISCOS__C__ */
#endif /* __riscos__ */ #endif /* __riscos__ */
#endif
#endif /*G10_UTIL_H*/ #endif /*G10_UTIL_H*/

View File

@ -1,3 +1,36 @@
2003-06-18 Werner Koch <wk@gnupg.org>
* strlist.c (strlist_pop): New.
* dotlock.c (dotlock_remove_lockfiles): Prefixed with dotlock_ and
made global.
2003-06-17 Werner Koch <wk@gnupg.org>
* stringhelp.c (length_sans_trailing_chars)
(length_sans_trailing_ws): New.
* logging.c (log_inc_errorcount): New.
* stringhelp.c (print_sanitized_utf8_buffer): Implement utf8
conversion.
(sanitize_buffer): New. Based on gnupg 1.3.2 make_printable_string.
* dotlock.c: Updated to match the version from 1.3.2
* utf8conv.c: New. Code taken from strgutil.c of gnupg 1.3.2.
* utf8conv.h: New.
2003-06-16 Werner Koch <wk@gnupg.org>
* logging.c (do_logv): Hack to optionally suppress a leading space.
* stringhelp.c (ascii_strncasecmp): New. Taken from gnupg 1.3.
(ascii_memistr): New. Taken from gnupg 1.3
2003-06-13 Werner Koch <wk@gnupg.org>
* mischelp.h (wipememory2,wipememory): New. Taken from GnuPG 1.3.2.
2002-06-04 Werner Koch <wk@gnupg.org> 2002-06-04 Werner Koch <wk@gnupg.org>
* stringhelp.c (print_sanitized_utf8_string): New. No real * stringhelp.c (print_sanitized_utf8_string): New. No real
@ -113,7 +146,7 @@ Mon Jan 24 13:04:28 CET 2000 Werner Koch <wk@gnupg.de>
* You may find it source-copied in other packages. * * You may find it source-copied in other packages. *
*********************************************************** ***********************************************************
Copyright 2000, 2001, 2002 Free Software Foundation, Inc. Copyright 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
This file is free software; as a special exception the author gives This file is free software; as a special exception the author gives
unlimited permission to copy and/or distribute it, with or without unlimited permission to copy and/or distribute it, with or without

View File

@ -31,6 +31,7 @@ libjnlib_a_SOURCES = \
libjnlib-config.h \ libjnlib-config.h \
stringhelp.c stringhelp.h \ stringhelp.c stringhelp.h \
strlist.c strlist.h \ strlist.c strlist.h \
utf8conv.c utf8conv.h \
argparse.c argparse.h \ argparse.c argparse.h \
logging.c logging.h \ logging.c logging.h \
dotlock.c dotlock.h \ dotlock.c dotlock.h \

View File

@ -1,5 +1,5 @@
/* dotlock.c - dotfile locking /* dotlock.c - dotfile locking
* Copyright (C) 1998,2000,2001 Free Software Foundation, Inc. * Copyright (C) 1998,2000,2001,2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -42,13 +42,20 @@ struct dotlock_handle {
char *tname; /* name of lockfile template */ char *tname; /* name of lockfile template */
char *lockname; /* name of the real lockfile */ char *lockname; /* name of the real lockfile */
int locked; /* lock status */ int locked; /* lock status */
int disable; /* locking */
}; };
static DOTLOCK all_lockfiles; static volatile DOTLOCK all_lockfiles;
static int never_lock;
static int read_lockfile( const char *name ); static int read_lockfile( const char *name );
static void remove_lockfiles(void);
void
disable_dotlock(void)
{
never_lock = 1;
}
/**************** /****************
* Create a lockfile with the given name and return an object of * Create a lockfile with the given name and return an object of
@ -81,13 +88,23 @@ create_dotlock( const char *file_to_lock )
int dirpartlen; int dirpartlen;
if( !initialized ) { if( !initialized ) {
atexit( remove_lockfiles ); atexit( dotlock_remove_lockfiles );
initialized = 1; initialized = 1;
} }
if( !file_to_lock ) if( !file_to_lock )
return NULL; return NULL;
h = jnlib_xcalloc( 1, sizeof *h ); h = jnlib_xcalloc( 1, sizeof *h );
if( never_lock ) {
h->disable = 1;
#ifdef _REENTRANT
/* fixme: aquire mutex on all_lockfiles */
#endif
h->next = all_lockfiles;
all_lockfiles = h;
return h;
}
#ifndef HAVE_DOSISH_SYSTEM #ifndef HAVE_DOSISH_SYSTEM
sprintf( pidstr, "%10d\n", (int)getpid() ); sprintf( pidstr, "%10d\n", (int)getpid() );
/* fixme: add the hostname to the second line (FQDN or IP addr?) */ /* fixme: add the hostname to the second line (FQDN or IP addr?) */
@ -98,8 +115,17 @@ create_dotlock( const char *file_to_lock )
else else
nodename = utsbuf.nodename; nodename = utsbuf.nodename;
if( !(dirpart = strrchr( file_to_lock, '/' )) ) { #ifdef __riscos__
dirpart = "."; {
char *iter = (char *) nodename;
for (; iter[0]; iter++)
if (iter[0] == '.')
iter[0] = '/';
}
#endif /* __riscos__ */
if( !(dirpart = strrchr( file_to_lock, DIRSEP_C )) ) {
dirpart = EXTSEP_S;
dirpartlen = 1; dirpartlen = 1;
} }
else { else {
@ -114,8 +140,13 @@ create_dotlock( const char *file_to_lock )
all_lockfiles = h; all_lockfiles = h;
h->tname = jnlib_xmalloc( dirpartlen + 6+30+ strlen(nodename) + 11 ); h->tname = jnlib_xmalloc( dirpartlen + 6+30+ strlen(nodename) + 11 );
sprintf( h->tname, "%.*s/.#lk%p.%s.%d", #ifndef __riscos__
sprintf( h->tname, "%.*s/.#lk%p.%s.%d",
dirpartlen, dirpart, h, nodename, (int)getpid() ); dirpartlen, dirpart, h, nodename, (int)getpid() );
#else /* __riscos__ */
sprintf( h->tname, "%.*s.lk%p/%s/%d",
dirpartlen, dirpart, h, nodename, (int)getpid() );
#endif /* __riscos__ */
do { do {
errno = 0; errno = 0;
@ -147,7 +178,8 @@ create_dotlock( const char *file_to_lock )
#ifdef _REENTRANT #ifdef _REENTRANT
/* release mutex */ /* release mutex */
#endif #endif
log_error( "error closing `%s': %s\n", h->tname, strerror(errno)); log_fatal( "error writing to `%s': %s\n", h->tname, strerror(errno) );
close(fd);
unlink(h->tname); unlink(h->tname);
jnlib_free(h->tname); jnlib_free(h->tname);
jnlib_free(h); jnlib_free(h);
@ -159,7 +191,7 @@ create_dotlock( const char *file_to_lock )
#endif #endif
#endif /* !HAVE_DOSISH_SYSTEM */ #endif /* !HAVE_DOSISH_SYSTEM */
h->lockname = jnlib_xmalloc( strlen(file_to_lock) + 6 ); h->lockname = jnlib_xmalloc( strlen(file_to_lock) + 6 );
strcpy(stpcpy(h->lockname, file_to_lock), ".lock"); strcpy(stpcpy(h->lockname, file_to_lock), EXTSEP_S "lock");
return h; return h;
} }
@ -191,12 +223,19 @@ make_dotlock( DOTLOCK h, long timeout )
const char *maybe_dead=""; const char *maybe_dead="";
int backoff=0; int backoff=0;
if( h->disable ) {
return 0;
}
if( h->locked ) { if( h->locked ) {
#ifndef __riscos__
log_debug("oops, `%s' is already locked\n", h->lockname ); log_debug("oops, `%s' is already locked\n", h->lockname );
#endif /* !__riscos__ */
return 0; return 0;
} }
for(;;) { for(;;) {
#ifndef __riscos__
if( !link(h->tname, h->lockname) ) { if( !link(h->tname, h->lockname) ) {
/* fixme: better use stat to check the link count */ /* fixme: better use stat to check the link count */
h->locked = 1; h->locked = 1;
@ -206,6 +245,16 @@ make_dotlock( DOTLOCK h, long timeout )
log_error( "lock not made: link() failed: %s\n", strerror(errno) ); log_error( "lock not made: link() failed: %s\n", strerror(errno) );
return -1; return -1;
} }
#else /* __riscos__ */
if( !renamefile(h->tname, h->lockname) ) {
h->locked = 1;
return 0; /* okay */
}
if( errno != EEXIST ) {
log_error( "lock not made: rename() failed: %s\n", strerror(errno) );
return -1;
}
#endif /* __riscos__ */
if( (pid = read_lockfile(h->lockname)) == -1 ) { if( (pid = read_lockfile(h->lockname)) == -1 ) {
if( errno != ENOENT ) { if( errno != ENOENT ) {
log_info("cannot read lockfile\n"); log_info("cannot read lockfile\n");
@ -215,20 +264,27 @@ make_dotlock( DOTLOCK h, long timeout )
continue; continue;
} }
else if( pid == getpid() ) { else if( pid == getpid() ) {
log_info( "Oops: lock already hold by us\n"); log_info( "Oops: lock already held by us\n");
h->locked = 1; h->locked = 1;
return 0; /* okay */ return 0; /* okay */
} }
else if( kill(pid, 0) && errno == ESRCH ) { else if( kill(pid, 0) && errno == ESRCH ) {
#ifndef __riscos__
maybe_dead = " - probably dead"; maybe_dead = " - probably dead";
#if 0 /* we should not do this without checking the permissions */ #if 0 /* we should not do this without checking the permissions */
/* and the hostname */ /* and the hostname */
log_info( "removing stale lockfile (created by %d)", pid ); log_info( "removing stale lockfile (created by %d)", pid );
#endif #endif
#else /* __riscos__ */
/* we are *pretty* sure that the other task is dead and therefore
we remove the other lock file */
maybe_dead = " - probably dead - removing lock";
unlink(h->lockname);
#endif /* __riscos__ */
} }
if( timeout == -1 ) { if( timeout == -1 ) {
struct timeval tv; struct timeval tv;
log_info( "waiting for lock (hold by %d%s) %s...\n", log_info( "waiting for lock (held by %d%s) %s...\n",
pid, maybe_dead, maybe_deadlock(h)? "(deadlock?) ":""); pid, maybe_dead, maybe_deadlock(h)? "(deadlock?) ":"");
@ -259,6 +315,10 @@ release_dotlock( DOTLOCK h )
#else #else
int pid; int pid;
if( h->disable ) {
return 0;
}
if( !h->locked ) { if( !h->locked ) {
log_debug("oops, `%s' is not locked\n", h->lockname ); log_debug("oops, `%s' is not locked\n", h->lockname );
return 0; return 0;
@ -273,11 +333,19 @@ release_dotlock( DOTLOCK h )
log_error( "release_dotlock: not our lock (pid=%d)\n", pid); log_error( "release_dotlock: not our lock (pid=%d)\n", pid);
return -1; return -1;
} }
#ifndef __riscos__
if( unlink( h->lockname ) ) { if( unlink( h->lockname ) ) {
log_error( "release_dotlock: error removing lockfile `%s'", log_error( "release_dotlock: error removing lockfile `%s'",
h->lockname); h->lockname);
return -1; return -1;
} }
#else /* __riscos__ */
if( renamefile(h->lockname, h->tname) ) {
log_error( "release_dotlock: error renaming lockfile `%s' to `%s'",
h->lockname, h->tname);
return -1;
}
#endif /* __riscos__ */
/* fixme: check that the link count is now 1 */ /* fixme: check that the link count is now 1 */
h->locked = 0; h->locked = 0;
return 0; return 0;
@ -291,9 +359,9 @@ release_dotlock( DOTLOCK h )
static int static int
read_lockfile( const char *name ) read_lockfile( const char *name )
{ {
#ifdef HAVE_DOSISH_SYSTEM #ifdef HAVE_DOSISH_SYSTEM
return 0; return 0;
#else #else
int fd, pid; int fd, pid;
char pidstr[16]; char pidstr[16];
@ -312,20 +380,24 @@ read_lockfile( const char *name )
pidstr[10] = 0; /* terminate pid string */ pidstr[10] = 0; /* terminate pid string */
close(fd); close(fd);
pid = atoi(pidstr); pid = atoi(pidstr);
#ifndef __riscos__
if( !pid || pid == -1 ) { if( !pid || pid == -1 ) {
#else /* __riscos__ */
if( (!pid && riscos_getpid()) || pid == -1 ) {
#endif /* __riscos__ */
log_error("invalid pid %d in lockfile `%s'", pid, name ); log_error("invalid pid %d in lockfile `%s'", pid, name );
errno = 0; errno = 0;
return -1; return -1;
} }
return pid; return pid;
#endif #endif
} }
static void void
remove_lockfiles() dotlock_remove_lockfiles()
{ {
#ifndef HAVE_DOSISH_SYSTEM #ifndef HAVE_DOSISH_SYSTEM
DOTLOCK h, h2; DOTLOCK h, h2;
h = all_lockfiles; h = all_lockfiles;
@ -333,14 +405,16 @@ remove_lockfiles()
while( h ) { while( h ) {
h2 = h->next; h2 = h->next;
if( h->locked ) if (!h->disable ) {
if( h->locked )
unlink( h->lockname ); unlink( h->lockname );
unlink(h->tname); unlink(h->tname);
jnlib_free(h->tname); jnlib_free(h->tname);
jnlib_free(h->lockname); jnlib_free(h->lockname);
}
jnlib_free(h); jnlib_free(h);
h = h2; h = h2;
} }
#endif #endif
} }

View File

@ -24,9 +24,13 @@
struct dotlock_handle; struct dotlock_handle;
typedef struct dotlock_handle *DOTLOCK; typedef struct dotlock_handle *DOTLOCK;
DOTLOCK create_dotlock( const char *file_to_lock ); void disable_dotlock (void);
int make_dotlock( DOTLOCK h, long timeout ); DOTLOCK create_dotlock(const char *file_to_lock);
int release_dotlock( DOTLOCK h ); int make_dotlock (DOTLOCK h, long timeout);
int release_dotlock (DOTLOCK h);
void dotlock_remove_lockfiles (void);
#endif /*LIBJNLIB_DOTLOCK_H*/ #endif /*LIBJNLIB_DOTLOCK_H*/

View File

@ -1,5 +1,5 @@
/* logging.c - useful logging functions /* logging.c - useful logging functions
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -78,6 +78,12 @@ log_get_errorcount (int clear)
return n; return n;
} }
void
log_inc_errorcount (void)
{
errorcount++;
}
void void
log_set_file( const char *name ) log_set_file( const char *name )
{ {
@ -194,7 +200,12 @@ do_logv( int level, const char *fmt, va_list arg_ptr )
fprintf (logstream, "[%u]", (unsigned int)getpid ()); fprintf (logstream, "[%u]", (unsigned int)getpid ());
if (!with_time) if (!with_time)
putc (':', logstream); putc (':', logstream);
putc (' ', logstream); /* A leading backspace suppresses the extra space so that we can
correclty output, programname, filename and linenumber. */
if (fmt && *fmt == '\b')
fmt++;
else
putc (' ', logstream);
} }
switch (level) switch (level)
@ -272,7 +283,7 @@ log_fatal( const char *fmt, ... )
va_start( arg_ptr, fmt ) ; va_start( arg_ptr, fmt ) ;
do_logv( JNLIB_LOG_FATAL, fmt, arg_ptr ); do_logv( JNLIB_LOG_FATAL, fmt, arg_ptr );
va_end(arg_ptr); va_end(arg_ptr);
abort(); /* never called, bugs it makes the compiler happy */ abort(); /* never called, but it makes the compiler happy */
} }
void void

View File

@ -26,6 +26,7 @@
int log_get_errorcount (int clear); int log_get_errorcount (int clear);
void log_inc_errorcount (void);
void log_set_file( const char *name ); void log_set_file( const char *name );
void log_set_fd (int fd); void log_set_fd (int fd);
void log_set_prefix (const char *text, unsigned int flags); void log_set_prefix (const char *text, unsigned int flags);

View File

@ -1,5 +1,5 @@
/* mischelp.h /* mischelp.h
* Copyright (C) 1999, 2000, 2001 Free Software Foundation, Inc. * Copyright (C) 1999, 2000, 2001, 2002, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -39,5 +39,16 @@
#endif #endif
/* To avoid that a compiler optimizes certain memset calls away, these
macros may be used instead. */
#define wipememory2(_ptr,_set,_len) do { \
volatile char *_vptr=(volatile char *)(_ptr); \
size_t _vlen=(_len); \
while(_vlen) { *_vptr=(_set); _vptr++; _vlen--; } \
} while(0)
#define wipememory(_ptr,_len) wipememory2(_ptr,0,_len)
#endif /*LIBJNLIB_MISCHELP_H*/ #endif /*LIBJNLIB_MISCHELP_H*/

View File

@ -1,5 +1,5 @@
/* stringhelp.c - standard string helper functions /* stringhelp.c - standard string helper functions
* Copyright (C) 1998, 1999, 2000, 2001 Free Software Foundation, Inc. * Copyright (C) 1998, 1999, 2000, 2001, 2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -25,6 +25,7 @@
#include <ctype.h> #include <ctype.h>
#include "libjnlib-config.h" #include "libjnlib-config.h"
#include "utf8conv.h"
#include "stringhelp.h" #include "stringhelp.h"
@ -52,6 +53,25 @@ memistr( const char *buf, size_t buflen, const char *sub )
return NULL ; return NULL ;
} }
const char *
ascii_memistr( const char *buf, size_t buflen, const char *sub )
{
const byte *t, *s ;
size_t n;
for( t=buf, n=buflen, s=sub ; n ; t++, n-- )
if( ascii_toupper(*t) == ascii_toupper(*s) ) {
for( buf=t++, buflen = n--, s++;
n && ascii_toupper(*t) == ascii_toupper(*s); t++, s++, n-- )
;
if( !*s )
return buf;
t = buf; n = buflen; s = sub ;
}
return NULL ;
}
/**************** /****************
* Wie strncpy(), aber es werden maximal n-1 zeichen kopiert und ein * Wie strncpy(), aber es werden maximal n-1 zeichen kopiert und ein
* '\0' angehängt. Ist n = 0, so geschieht nichts, ist Destination * '\0' angehängt. Ist n = 0, so geschieht nichts, ist Destination
@ -127,7 +147,6 @@ trim_trailing_spaces( char *string )
} }
unsigned unsigned
trim_trailing_chars( byte *line, unsigned len, const char *trimchars ) trim_trailing_chars( byte *line, unsigned len, const char *trimchars )
{ {
@ -159,6 +178,39 @@ trim_trailing_ws( byte *line, unsigned len )
return trim_trailing_chars( line, len, " \t\r\n" ); return trim_trailing_chars( line, len, " \t\r\n" );
} }
size_t
length_sans_trailing_chars (const unsigned char *line, size_t len,
const char *trimchars )
{
const unsigned char *p, *mark;
size_t n;
for( mark=NULL, p=line, n=0; n < len; n++, p++ )
{
if (strchr (trimchars, *p ))
{
if( !mark )
mark = p;
}
else
mark = NULL;
}
if (mark)
return mark - line;
return len;
}
/****************
* remove trailing white spaces and return the length of the buffer
*/
size_t
length_sans_trailing_ws (const unsigned char *line, size_t len)
{
return length_sans_trailing_chars (line, len, " \t\r\n");
}
/*************** /***************
* Extract from a given path the filename component. * Extract from a given path the filename component.
@ -256,18 +308,19 @@ compare_filenames( const char *a, const char *b )
/* ? check whether this is an absolute filename and /* ? check whether this is an absolute filename and
* resolve symlinks? * resolve symlinks?
*/ */
#ifdef HAVE_DRIVE_LETTERS #ifdef HAVE_DRIVE_LETTERS
return stricmp(a,b); return stricmp(a,b);
#else #else
return strcmp(a,b); return strcmp(a,b);
#endif #endif
} }
/* Print a BUFFER to stream FP while replacing all control characters /* Print a BUFFER to stream FP while replacing all control characters
and the character DELIM with standard C eescape sequences. Returns and the character DELIM with standard C escape sequences. Returns
the number of characters printed. */ the number of characters printed. */
size_t size_t
print_sanitized_buffer (FILE *fp, const void *buffer, size_t length, int delim) print_sanitized_buffer (FILE *fp, const void *buffer, size_t length,
int delim)
{ {
const unsigned char *p = buffer; const unsigned char *p = buffer;
size_t count = 0; size_t count = 0;
@ -307,8 +360,26 @@ size_t
print_sanitized_utf8_buffer (FILE *fp, const void *buffer, print_sanitized_utf8_buffer (FILE *fp, const void *buffer,
size_t length, int delim) size_t length, int delim)
{ {
/* FIXME: convert to local characterset */ const char *p = buffer;
return print_sanitized_buffer (fp, buffer, length, delim); size_t i;
/* We can handle plain ascii simpler, so check for it first. */
for (i=0; i < length; i++ )
{
if ( (p[i] & 0x80) )
break;
}
if (i < length)
{
char *buf = utf8_to_native (p, length, delim);
/*(utf8 conversion already does the control character quoting)*/
i = strlen (buf);
fputs (buf, fp);
jnlib_free (buf);
return i;
}
else
return print_sanitized_buffer (fp, p, length, delim);
} }
@ -325,6 +396,63 @@ print_sanitized_utf8_string (FILE *fp, const char *string, int delim)
return print_sanitized_string (fp, string, delim); return print_sanitized_string (fp, string, delim);
} }
/* Create a string from the buffer P of length N which is suitable for
printing. Caller must release the created string using xfree. */
char *
sanitize_buffer (const unsigned char *p, size_t n, int delim)
{
size_t save_n, buflen;
const byte *save_p;
char *buffer, *d;
/* first count length */
for (save_n = n, save_p = p, buflen=1 ; n; n--, p++ )
{
if ( *p < 0x20 || (*p >= 0x7f && *p < 0xa0) || *p == delim
|| (delim && *p=='\\'))
{
if ( *p=='\n' || *p=='\r' || *p=='\f'
|| *p=='\v' || *p=='\b' || !*p )
buflen += 2;
else
buflen += 4;
}
else
buflen++;
}
p = save_p;
n = save_n;
/* and now make the string */
d = buffer = jnlib_xmalloc( buflen );
for ( ; n; n--, p++ )
{
if (*p < 0x20 || (*p >= 0x7f && *p < 0xa0) || *p == delim
||(delim && *p=='\\')) {
*d++ = '\\';
if( *p == '\n' )
*d++ = 'n';
else if( *p == '\r' )
*d++ = 'r';
else if( *p == '\f' )
*d++ = 'f';
else if( *p == '\v' )
*d++ = 'v';
else if( *p == '\b' )
*d++ = 'b';
else if( !*p )
*d++ = '0';
else {
sprintf(d, "x%02x", *p );
d += 2;
}
}
else
*d++ = *p;
}
*d = 0;
return buffer;
}
/**************************************************** /****************************************************
******** locale insensitive ctype functions ******** ******** locale insensitive ctype functions ********
****************************************************/ ****************************************************/
@ -371,6 +499,33 @@ ascii_strcasecmp( const char *a, const char *b )
return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b)); return *a == *b? 0 : (ascii_toupper (*a) - ascii_toupper (*b));
} }
int
ascii_strncasecmp (const char *a, const char *b, size_t n)
{
const unsigned char *p1 = (const unsigned char *)a;
const unsigned char *p2 = (const unsigned char *)b;
unsigned char c1, c2;
if (p1 == p2 || !n )
return 0;
do
{
c1 = ascii_tolower (*p1);
c2 = ascii_tolower (*p2);
if ( !--n || c1 == '\0')
break;
++p1;
++p2;
}
while (c1 == c2);
return c1 - c2;
}
int int
ascii_memcasecmp( const char *a, const char *b, size_t n ) ascii_memcasecmp( const char *a, const char *b, size_t n )
{ {

View File

@ -1,5 +1,5 @@
/* stringhelp.h /* stringhelp.h
* Copyright (C) 1998,1999,2000,2001 Free Software Foundation, Inc. * Copyright (C) 1998,1999,2000,2001,2003 Free Software Foundation, Inc.
* *
* This file is part of GnuPG. * This file is part of GnuPG.
* *
@ -30,6 +30,9 @@ char *trim_trailing_spaces( char *string );
unsigned int trim_trailing_chars( unsigned char *line, unsigned len, unsigned int trim_trailing_chars( unsigned char *line, unsigned len,
const char *trimchars); const char *trimchars);
unsigned int trim_trailing_ws( unsigned char *line, unsigned len ); unsigned int trim_trailing_ws( unsigned char *line, unsigned len );
size_t length_sans_trailing_chars (const unsigned char *line, size_t len,
const char *trimchars );
size_t length_sans_trailing_ws (const unsigned char *line, size_t len);
char *make_basename(const char *filepath); char *make_basename(const char *filepath);
@ -43,6 +46,7 @@ size_t print_sanitized_utf8_buffer (FILE *fp, const void *buffer,
size_t length, int delim); size_t length, int delim);
size_t print_sanitized_string (FILE *fp, const char *string, int delim); size_t print_sanitized_string (FILE *fp, const char *string, int delim);
size_t print_sanitized_utf8_string (FILE *fp, const char *string, int delim); size_t print_sanitized_utf8_string (FILE *fp, const char *string, int delim);
char *sanitize_buffer (const unsigned char *p, size_t n, int delim);
const char *ascii_memistr( const char *buf, size_t buflen, const char *sub ); const char *ascii_memistr( const char *buf, size_t buflen, const char *sub );
@ -51,7 +55,9 @@ int ascii_islower (int c);
int ascii_toupper (int c); int ascii_toupper (int c);
int ascii_tolower (int c); int ascii_tolower (int c);
int ascii_strcasecmp( const char *a, const char *b ); int ascii_strcasecmp( const char *a, const char *b );
int ascii_strncasecmp (const char *a, const char *b, size_t n);
int ascii_memcasecmp( const char *a, const char *b, size_t n ); int ascii_memcasecmp( const char *a, const char *b, size_t n );
const char *ascii_memistr ( const char *buf, size_t buflen, const char *sub);
void *ascii_memcasemem (const void *haystack, size_t nhaystack, void *ascii_memcasemem (const void *haystack, size_t nhaystack,
const void *needle, size_t nneedle); const void *needle, size_t nneedle);

View File

@ -56,7 +56,7 @@ add_to_strlist( STRLIST *list, const char *string )
#if 0 #if 0
/**************** /****************
* same as add_to_strlist() but if is_utf8 is *not* set a conversion * same as add_to_strlist() but if is_utf8 is *not* set a conversion
* to UTF8 is done * to UTF8 is done
*/ */
STRLIST STRLIST
add_to_strlist2( STRLIST *list, const char *string, int is_utf8 ) add_to_strlist2( STRLIST *list, const char *string, int is_utf8 )
@ -130,4 +130,22 @@ strlist_last( STRLIST node )
} }
char *
strlist_pop (STRLIST *list)
{
char *str=NULL;
STRLIST sl=*list;
if(sl)
{
str=jnlib_xmalloc(strlen(sl->d)+1);
strcpy(str,sl->d);
*list=sl->next;
jnlib_free(sl);
}
return str;
}

View File

@ -31,11 +31,12 @@ typedef struct string_list *STRLIST;
void free_strlist( STRLIST sl ); void free_strlist( STRLIST sl );
STRLIST add_to_strlist( STRLIST *list, const char *string ); STRLIST add_to_strlist( STRLIST *list, const char *string );
STRLIST add_to_strlist2( STRLIST *list, const char *string, int is_utf8 ); /*STRLIST add_to_strlist2( STRLIST *list, const char *string, int is_utf8 );*/
STRLIST append_to_strlist( STRLIST *list, const char *string ); STRLIST append_to_strlist( STRLIST *list, const char *string );
STRLIST append_to_strlist2( STRLIST *list, const char *string, int is_utf8 ); /*STRLIST append_to_strlist2( STRLIST *list, const char *string, int is_utf8 );*/
STRLIST strlist_prev( STRLIST head, STRLIST node ); STRLIST strlist_prev( STRLIST head, STRLIST node );
STRLIST strlist_last( STRLIST node ); STRLIST strlist_last( STRLIST node );
char * strlist_pop (STRLIST *list);
#define FREE_STRLIST(a) do { free_strlist((a)); (a) = NULL ; } while(0) #define FREE_STRLIST(a) do { free_strlist((a)); (a) = NULL ; } while(0)

448
jnlib/utf8conv.c Normal file
View File

@ -0,0 +1,448 @@
/* utf8conf.c - UTF8 character set conversion
* Copyright (C) 1994, 1998, 1999, 2000, 2001,
* 2003 Free Software Foundation, Inc.
*
* 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 <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <ctype.h>
#ifdef HAVE_LANGINFO_CODESET
#include <langinfo.h>
#endif
#include "libjnlib-config.h"
#include "stringhelp.h"
#include "utf8conv.h"
static ushort koi8_unicode[128] = {
0x2500, 0x2502, 0x250c, 0x2510, 0x2514, 0x2518, 0x251c, 0x2524,
0x252c, 0x2534, 0x253c, 0x2580, 0x2584, 0x2588, 0x258c, 0x2590,
0x2591, 0x2592, 0x2593, 0x2320, 0x25a0, 0x2219, 0x221a, 0x2248,
0x2264, 0x2265, 0x00a0, 0x2321, 0x00b0, 0x00b2, 0x00b7, 0x00f7,
0x2550, 0x2551, 0x2552, 0x0451, 0x2553, 0x2554, 0x2555, 0x2556,
0x2557, 0x2558, 0x2559, 0x255a, 0x255b, 0x255c, 0x255d, 0x255e,
0x255f, 0x2560, 0x2561, 0x0401, 0x2562, 0x2563, 0x2564, 0x2565,
0x2566, 0x2567, 0x2568, 0x2569, 0x256a, 0x256b, 0x256c, 0x00a9,
0x044e, 0x0430, 0x0431, 0x0446, 0x0434, 0x0435, 0x0444, 0x0433,
0x0445, 0x0438, 0x0439, 0x043a, 0x043b, 0x043c, 0x043d, 0x043e,
0x043f, 0x044f, 0x0440, 0x0441, 0x0442, 0x0443, 0x0436, 0x0432,
0x044c, 0x044b, 0x0437, 0x0448, 0x044d, 0x0449, 0x0447, 0x044a,
0x042e, 0x0410, 0x0411, 0x0426, 0x0414, 0x0415, 0x0424, 0x0413,
0x0425, 0x0418, 0x0419, 0x041a, 0x041b, 0x041c, 0x041d, 0x041e,
0x041f, 0x042f, 0x0420, 0x0421, 0x0422, 0x0423, 0x0416, 0x0412,
0x042c, 0x042b, 0x0417, 0x0428, 0x042d, 0x0429, 0x0427, 0x042a
};
static ushort latin2_unicode[128] = {
0x0080, 0x0081, 0x0082, 0x0083, 0x0084, 0x0085, 0x0086, 0x0087,
0x0088, 0x0089, 0x008A, 0x008B, 0x008C, 0x008D, 0x008E, 0x008F,
0x0090, 0x0091, 0x0092, 0x0093, 0x0094, 0x0095, 0x0096, 0x0097,
0x0098, 0x0099, 0x009A, 0x009B, 0x009C, 0x009D, 0x009E, 0x009F,
0x00A0, 0x0104, 0x02D8, 0x0141, 0x00A4, 0x013D, 0x015A, 0x00A7,
0x00A8, 0x0160, 0x015E, 0x0164, 0x0179, 0x00AD, 0x017D, 0x017B,
0x00B0, 0x0105, 0x02DB, 0x0142, 0x00B4, 0x013E, 0x015B, 0x02C7,
0x00B8, 0x0161, 0x015F, 0x0165, 0x017A, 0x02DD, 0x017E, 0x017C,
0x0154, 0x00C1, 0x00C2, 0x0102, 0x00C4, 0x0139, 0x0106, 0x00C7,
0x010C, 0x00C9, 0x0118, 0x00CB, 0x011A, 0x00CD, 0x00CE, 0x010E,
0x0110, 0x0143, 0x0147, 0x00D3, 0x00D4, 0x0150, 0x00D6, 0x00D7,
0x0158, 0x016E, 0x00DA, 0x0170, 0x00DC, 0x00DD, 0x0162, 0x00DF,
0x0155, 0x00E1, 0x00E2, 0x0103, 0x00E4, 0x013A, 0x0107, 0x00E7,
0x010D, 0x00E9, 0x0119, 0x00EB, 0x011B, 0x00ED, 0x00EE, 0x010F,
0x0111, 0x0144, 0x0148, 0x00F3, 0x00F4, 0x0151, 0x00F6, 0x00F7,
0x0159, 0x016F, 0x00FA, 0x0171, 0x00FC, 0x00FD, 0x0163, 0x02D9
};
static const char *active_charset_name = "iso-8859-1";
static ushort *active_charset = NULL;
static int no_translation = 0;
int
set_native_charset (const char *newset)
{
if (!newset)
#ifdef HAVE_LANGINFO_CODESET
newset = nl_langinfo (CODESET);
#else
newset = "8859-1";
#endif
if (strlen (newset) > 3 && !ascii_memcasecmp (newset, "iso", 3))
{
newset += 3;
if (*newset == '-' || *newset == '_')
newset++;
}
if (!*newset
|| !ascii_strcasecmp (newset, "8859-1")
|| !ascii_strcasecmp (newset, "8859-15"))
{
active_charset_name = "iso-8859-1";
no_translation = 0;
active_charset = NULL;
}
else if (!ascii_strcasecmp (newset, "8859-2"))
{
active_charset_name = "iso-8859-2";
no_translation = 0;
active_charset = latin2_unicode;
}
else if (!ascii_strcasecmp (newset, "koi8-r"))
{
active_charset_name = "koi8-r";
no_translation = 0;
active_charset = koi8_unicode;
}
else if (!ascii_strcasecmp (newset, "utf8")
|| !ascii_strcasecmp (newset, "utf-8"))
{
active_charset_name = "utf-8";
no_translation = 1;
active_charset = NULL;
}
else
return -1;
return 0;
}
const char *
get_native_charset ()
{
return active_charset_name;
}
/****************
* Convert string, which is in native encoding to UTF8 and return the
* new allocated UTF8 string.
*/
char *
native_to_utf8 (const char *string)
{
const byte *s;
char *buffer;
byte *p;
size_t length = 0;
if (no_translation)
{
buffer = jnlib_xstrdup (string);
}
else if (active_charset)
{
for (s = string; *s; s++)
{
length++;
if (*s & 0x80)
length += 2; /* we may need 3 bytes */
}
buffer = jnlib_xmalloc (length + 1);
for (p = buffer, s = string; *s; s++)
{
if ((*s & 0x80))
{
ushort val = active_charset[*s & 0x7f];
if (val < 0x0800)
{
*p++ = 0xc0 | ((val >> 6) & 0x1f);
*p++ = 0x80 | (val & 0x3f);
}
else
{
*p++ = 0xe0 | ((val >> 12) & 0x0f);
*p++ = 0x80 | ((val >> 6) & 0x3f);
*p++ = 0x80 | (val & 0x3f);
}
}
else
*p++ = *s;
}
*p = 0;
}
else
{
for (s = string; *s; s++)
{
length++;
if (*s & 0x80)
length++;
}
buffer = jnlib_xmalloc (length + 1);
for (p = buffer, s = string; *s; s++)
{
if (*s & 0x80)
{
*p++ = 0xc0 | ((*s >> 6) & 3);
*p++ = 0x80 | (*s & 0x3f);
}
else
*p++ = *s;
}
*p = 0;
}
return buffer;
}
/* Convert string, which is in UTF8 to native encoding. Replace
* illegal encodings by some "\xnn" and quote all control
* characters. A character with value DELIM will always be quoted, it
* must be a vanilla ASCII character. */
char *
utf8_to_native (const char *string, size_t length, int delim)
{
int nleft;
int i;
byte encbuf[8];
int encidx;
const byte *s;
size_t n;
byte *buffer = NULL, *p = NULL;
unsigned long val = 0;
size_t slen;
int resync = 0;
/* 1. pass (p==NULL): count the extended utf-8 characters */
/* 2. pass (p!=NULL): create string */
for (;;)
{
for (slen = length, nleft = encidx = 0, n = 0, s = string; slen;
s++, slen--)
{
if (resync)
{
if (!(*s < 128 || (*s >= 0xc0 && *s <= 0xfd)))
{
/* still invalid */
if (p)
{
sprintf (p, "\\x%02x", *s);
p += 4;
}
n += 4;
continue;
}
resync = 0;
}
if (!nleft)
{
if (!(*s & 0x80))
{ /* plain ascii */
if (*s < 0x20 || *s == 0x7f || *s == delim ||
(delim && *s == '\\'))
{
n++;
if (p)
*p++ = '\\';
switch (*s)
{
case '\n':
n++;
if (p)
*p++ = 'n';
break;
case '\r':
n++;
if (p)
*p++ = 'r';
break;
case '\f':
n++;
if (p)
*p++ = 'f';
break;
case '\v':
n++;
if (p)
*p++ = 'v';
break;
case '\b':
n++;
if (p)
*p++ = 'b';
break;
case 0:
n++;
if (p)
*p++ = '0';
break;
default:
n += 3;
if (p)
{
sprintf (p, "x%02x", *s);
p += 3;
}
break;
}
}
else
{
if (p)
*p++ = *s;
n++;
}
}
else if ((*s & 0xe0) == 0xc0)
{ /* 110x xxxx */
val = *s & 0x1f;
nleft = 1;
encidx = 0;
encbuf[encidx++] = *s;
}
else if ((*s & 0xf0) == 0xe0)
{ /* 1110 xxxx */
val = *s & 0x0f;
nleft = 2;
encidx = 0;
encbuf[encidx++] = *s;
}
else if ((*s & 0xf8) == 0xf0)
{ /* 1111 0xxx */
val = *s & 0x07;
nleft = 3;
encidx = 0;
encbuf[encidx++] = *s;
}
else if ((*s & 0xfc) == 0xf8)
{ /* 1111 10xx */
val = *s & 0x03;
nleft = 4;
encidx = 0;
encbuf[encidx++] = *s;
}
else if ((*s & 0xfe) == 0xfc)
{ /* 1111 110x */
val = *s & 0x01;
nleft = 5;
encidx = 0;
encbuf[encidx++] = *s;
}
else
{ /* invalid encoding: print as \xnn */
if (p)
{
sprintf (p, "\\x%02x", *s);
p += 4;
}
n += 4;
resync = 1;
}
}
else if (*s < 0x80 || *s >= 0xc0)
{ /* invalid */
if (p)
{
for (i = 0; i < encidx; i++)
{
sprintf (p, "\\x%02x", encbuf[i]);
p += 4;
}
sprintf (p, "\\x%02x", *s);
p += 4;
}
n += 4 + 4 * encidx;
nleft = 0;
encidx = 0;
resync = 1;
}
else
{
encbuf[encidx++] = *s;
val <<= 6;
val |= *s & 0x3f;
if (!--nleft)
{ /* ready */
if (no_translation)
{
if (p)
{
for (i = 0; i < encidx; i++)
*p++ = encbuf[i];
}
n += encidx;
encidx = 0;
}
else if (active_charset)
{ /* table lookup */
for (i = 0; i < 128; i++)
{
if (active_charset[i] == val)
break;
}
if (i < 128)
{ /* we can print this one */
if (p)
*p++ = i + 128;
n++;
}
else
{ /* we do not have a translation: print utf8 */
if (p)
{
for (i = 0; i < encidx; i++)
{
sprintf (p, "\\x%02x", encbuf[i]);
p += 4;
}
}
n += encidx * 4;
encidx = 0;
}
}
else
{ /* native set */
if (val >= 0x80 && val < 256)
{
n++; /* we can simply print this character */
if (p)
*p++ = val;
}
else
{ /* we do not have a translation: print utf8 */
if (p)
{
for (i = 0; i < encidx; i++)
{
sprintf (p, "\\x%02x", encbuf[i]);
p += 4;
}
}
n += encidx * 4;
encidx = 0;
}
}
}
}
}
if (!buffer)
{ /* allocate the buffer after the first pass */
buffer = p = jnlib_xmalloc (n + 1);
}
else
{
*p = 0; /* make a string */
return buffer;
}
}
}

31
jnlib/utf8conv.h Normal file
View File

@ -0,0 +1,31 @@
/* utf8conf.h
* Copyright (C) 2003 Free Software Foundation, Inc.
*
* 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
*/
#ifndef LIBJNLIB_UTF8CONF_H
#define LIBJNLIB_UTF8CONF_H
int set_native_charset (const char *newset);
const char *get_native_charset (void);
char *native_to_utf8 (const char *string);
char *utf8_to_native (const char *string, size_t length, int delim);
#endif /*LIBJNLIB_UTF8CONF_H*/