diff --git a/AUTHORS b/AUTHORS index 31094467..d680e489 100644 --- a/AUTHORS +++ b/AUTHORS @@ -1,81 +1,81 @@ Package: gpgme Homepage: https://gnupg.org/software/gpgme/ Download: https://gnupg.org/ftp/gcrypt/gpgme/ Repository: git://git.gnupg.org/gpgme.git Maintainer: Werner Koch Bug reports: https://bugs.gnupg.org Security related bug reports: security@gnupg.org -License (software): LGPLv2.1+ -License (manual+tools): GPLv3+ +License (software): LGPL-2.1-or-later +License (manual+tools): GPL-3.0-or-later GPGME is free software. See the files COPYING for copying conditions. License copyright years may be listed using range notation, e.g., 2000-2013, indicating that every year in the range, inclusive, is a copyrightable year that would otherwise be listed individually. List of Copyright holders ========================= Copyright (C) 1991-2013 Free Software Foundation, Inc. Copyright (C) 2000-2001 Werner Koch Copyright (C) 2001-2018 g10 Code GmbH Copyright (C) 2002 Klarälvdalens Datakonsult AB Copyright (C) 2004-2008 Igor Belyi Copyright (C) 2002 John Goerzen Copyright (C) 2014, 2015 Martin Albrecht Copyright (C) 2015, 2018 Ben McGinnes Copyright (C) 2015, 2016, 2018 Bundesamt für Sicherheit in der Informationstechnik Copyright (C) 2016 Intevation GmbH Authors info ============ FSF - Code taken from GnuPG 1.0: src/w32-util.c. - Other from FSF projects: src/setenv.c, src/vasprintf.c, src/stpcpy.c, src/w32-ce.c. g10 Code GmbH - All stuff since mid march 2001. Werner Koch - Design and most stuff. Wojciech Polak - gpgme.spec Authors with a DCO ================== Daniel Kahn Gillmor 2014-09-24:878ul9w4j8.fsf@alice.fifthhorseman.net: Colin Watson 2017-09-16:20170916031428.uypfrdojquvjteor@riva.ucam.org: Tobias Mueller 2016-11-23:1479937342.11180.3.camel@cryptobitch.de: Ben McGinnes 2017-12-16:20171216002102.l6aejk5xdp6xhtfi@adversary.org: Jacob Adams 2018-06-03:ad5141df-b6cc-6c2a-59df-b2f18f7160fd@gmail.com: Guillaume LE VAILLANT 2018-10-11:20181011113825.76f9752a@yamatai: Copyright 2001, 2002, 2012, 2013 g10 Code GmbH This file is free software; as a special exception the author gives unlimited permission to copy and/or distribute it, with or without modifications, as long as this notice is preserved. This file is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY, to the extent permitted by law; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. diff --git a/Makefile.am b/Makefile.am index 52639b75..1772d69c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,128 +1,129 @@ # Makefile.am - Top level Makefile for GPGME. # Copyright (C) 2000 Werner Koch (dd9jn) # Copyright (C) 2001, 2002, 2004, 2005, 2008, 2009 g10 Code GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser General Public -# License along with this program; if not, see . +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later ## Process this file with automake to produce Makefile.in # Location of the released tarball archives. Note that this is an # internal archive and before uploading this to the public server, # manual tests should be run and the git release tat set and pushed. # Adjust as needed. RELEASE_ARCHIVE_DIR = wk@vigenere:tarballs/gpgme/ # The key used to sign the released sources. Adjust as needed. RELEASE_SIGNING_KEY = D8692123C4065DEA5E0F3AB5249B39D24F25E3B6 # Autoconf flags ACLOCAL_AMFLAGS = -I m4 DISTCHECK_CONFIGURE_FLAGS = EXTRA_DIST = autogen.sh autogen.rc gpgme.spec.in \ ChangeLog-2011 m4/ChangeLog-2011 \ conf/whatisthis VERSION if RUN_GPG_TESTS tests = tests else tests = endif SUBDIRS = src ${tests} doc lang # Fix the version of the spec file. dist-hook: gen-ChangeLog @set -e; \ sed -e 's/@pkg_version@/$(PACKAGE_VERSION)/g' \ $(top_srcdir)/gpgme.spec.in > $(distdir)/gpgme.spec distcheck-hook: set -e; ( \ pref="#+macro: gpgme_" ;\ reldate="$$(date -u +%Y-%m-%d)" ;\ echo "$${pref}ver $(PACKAGE_VERSION)" ;\ echo "$${pref}date $${reldate}" ;\ list='$(DIST_ARCHIVES)'; for i in $$list; do \ case "$$i" in *.tar.bz2) \ echo "$${pref}size $$(wc -c <$$i|awk '{print int($$1/1024)}')k" ;\ echo "$${pref}sha1 $$(sha1sum <$$i|cut -d' ' -f1)" ;\ echo "$${pref}sha2 $$(sha256sum <$$i|cut -d' ' -f1)" ;;\ esac;\ done ) | tee $(distdir).swdb .PHONY: gen-ChangeLog release sign-release gen_start_date = 2011-12-01T00:00:00 gen-ChangeLog: if test -d $(top_srcdir)/.git; then \ (cd $(top_srcdir) && \ $(GITLOG_TO_CHANGELOG) --append-dot --tear-off \ --amend=build-aux/git-log-fix \ --since=$(gen_start_date) ) > $(distdir)/cl-t; \ cat $(top_srcdir)/build-aux/git-log-footer >> $(distdir)/cl-t;\ rm -f $(distdir)/ChangeLog; \ mv $(distdir)/cl-t $(distdir)/ChangeLog; \ fi # Macro to help the release target. RELEASE_NAME = $(PACKAGE_TARNAME)-$(PACKAGE_VERSION) release: +(set -e;\ if [ "$(abs_top_builddir)" = "$(abs_top_srcdir)" ]; then \ echo "error: build directory must not be the source directory" >&2;\ exit 2;\ fi ;\ echo "/* Build started at $$(date -uIseconds) */" ;\ cd $(top_srcdir); \ ./autogen.sh --force; \ cd $(abs_top_builddir); \ rm -rf dist; mkdir dist ; cd dist ; \ $(abs_top_srcdir)/configure --enable-maintainer-mode; \ $(MAKE) distcheck TESTFLAGS=--parallel; \ echo "/* Build finished at $$(date -uIseconds) */" ;\ echo "/*" ;\ echo " * Please run the final step interactively:" ;\ echo " * make sign-release" ;\ echo " */" ;\ ) 2>&1 | tee "$(RELEASE_NAME).buildlog" sign-release: +(set -e; \ cd dist; \ files1="$(RELEASE_NAME).tar.bz2" ;\ files2="$(RELEASE_NAME).tar.bz2.sig \ $(RELEASE_NAME).swdb \ $(RELEASE_NAME).buildlog" ;\ echo "/* Signing the source tarball ..." ;\ gpg -sbu $(RELEASE_SIGNING_KEY) $(RELEASE_NAME).tar.bz2 ;\ cat $(RELEASE_NAME).swdb >swdb.snippet;\ echo >>swdb.snippet ;\ sha1sum $${files1} >>swdb.snippet ;\ cat "../$(RELEASE_NAME).buildlog" swdb.snippet \ | gzip >$(RELEASE_NAME).buildlog ;\ echo "Copying to local archive ..." ;\ scp -p $${files1} $${files2} $(RELEASE_ARCHIVE_DIR)/ || true;\ echo "Uploading documentation ..." ;\ $(MAKE) -C doc online; \ echo '/*' ;\ echo ' * All done; for checksums see dist/swdb.snippet' ;\ echo ' */' ;\ ) diff --git a/configure.ac b/configure.ac index 4976b5b2..b4223e9a 100644 --- a/configure.ac +++ b/configure.ac @@ -1,906 +1,907 @@ # configure.ac for GPGME # Copyright (C) 2000 Werner Koch (dd9jn) # Copyright (C) 2001-2018 g10 Code GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # -# You should have received a copy of the GNU General Public License -# along with this program; if not, see . +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later # (Process this file with autoconf to produce a configure script.) AC_PREREQ(2.59) min_automake_version="1.14" # To build a release you need to create a tag with the version number # (git tag -s gpgme-n.m.k) and run "./autogen.sh --force". Please # bump the version number immediately after the release and do another # commit and push so that the git magic is able to work. See below # for the LT versions. m4_define([mym4_package],[gpgme]) m4_define([mym4_major], [1]) m4_define([mym4_minor], [12]) m4_define([mym4_micro], [1]) # Below is m4 magic to extract and compute the git revision number, # the decimalized short revision number, a beta version string and a # flag indicating a development version (mym4_isbeta). Note that the # m4 processing is done by autoconf and not during the configure run. m4_define([mym4_verslist], m4_split(m4_esyscmd([./autogen.sh --find-version] \ mym4_package mym4_major mym4_minor mym4_micro),[:])) m4_define([mym4_isbeta], m4_argn(2, mym4_verslist)) m4_define([mym4_version], m4_argn(4, mym4_verslist)) m4_define([mym4_revision], m4_argn(7, mym4_verslist)) m4_define([mym4_revision_dec], m4_argn(8, mym4_verslist)) m4_esyscmd([echo ]mym4_version[>VERSION]) AC_INIT([mym4_package],[mym4_version], [https://bugs.gnupg.org]) # LT Version numbers, remember to change them just *before* a release. # (Code changed: REVISION++) # (Interfaces added/removed/changed: CURRENT++, REVISION=0) # (Interfaces added: AGE++) # (Interfaces removed: AGE=0) # LIBGPGME_LT_CURRENT=32 LIBGPGME_LT_AGE=21 LIBGPGME_LT_REVISION=0 # If there is an ABI break in gpgmepp or qgpgme also bump the # version in IMPORTED_LOCATION in the GpgmeppConfig-w32.cmake.in.in LIBGPGMEPP_LT_CURRENT=14 LIBGPGMEPP_LT_AGE=8 LIBGPGMEPP_LT_REVISION=0 LIBQGPGME_LT_CURRENT=10 LIBQGPGME_LT_AGE=3 LIBQGPGME_LT_REVISION=2 ################################################ AC_SUBST(LIBGPGME_LT_CURRENT) AC_SUBST(LIBGPGME_LT_AGE) AC_SUBST(LIBGPGME_LT_REVISION) AC_SUBST(LIBGPGMEPP_LT_CURRENT) AC_SUBST(LIBGPGMEPP_LT_AGE) AC_SUBST(LIBGPGMEPP_LT_REVISION) AC_SUBST(LIBQGPGME_LT_CURRENT) AC_SUBST(LIBQGPGME_LT_AGE) AC_SUBST(LIBQGPGME_LT_REVISION) # If the API is changed in an incompatible way: increment the next counter. GPGME_CONFIG_API_VERSION=1 ############################################## NEED_GPG_ERROR_VERSION=1.24 NEED_LIBASSUAN_API=2 NEED_LIBASSUAN_VERSION=2.4.2 VERSION_MAJOR=mym4_major VERSION_MINOR=mym4_minor VERSION_MICRO=mym4_micro AC_CONFIG_AUX_DIR([build-aux]) AC_CONFIG_MACRO_DIR([m4]) AC_CONFIG_SRCDIR(src/gpgme.h.in) AC_CONFIG_HEADER(conf/config.h) AM_INIT_AUTOMAKE([serial-tests dist-bzip2 no-dist-gzip]) AM_MAINTAINER_MODE AC_CANONICAL_HOST AM_SILENT_RULES AC_ARG_VAR(SYSROOT,[locate config scripts also below that directory]) # Enable GNU extensions on systems that have them. AC_GNU_SOURCE AH_VERBATIM([_REENTRANT], [/* To allow the use of GPGME in multithreaded programs we have to use special features from the library. IMPORTANT: gpgme is not yet fully reentrant and you should use it only from one thread. */ #ifndef _REENTRANT # define _REENTRANT 1 #endif]) AC_PROG_CC AC_PROG_CPP AC_PROG_CXX # Note: A suitable gitlog-to-changelog script can be found in GnuPG master. AC_CHECK_PROGS(GITLOG_TO_CHANGELOG, gitlog-to-changelog, [gitlog-to-changelog]) AC_SUBST(VERSION_MAJOR) AC_SUBST(VERSION_MINOR) AC_SUBST(VERSION_MICRO) VERSION_NUMBER=m4_esyscmd(printf "0x%02x%02x%02x" mym4_major \ mym4_minor mym4_micro) AC_SUBST(VERSION_NUMBER) # We need to compile and run a program on the build machine. A # comment in libgpg-error says that the AC_PROG_CC_FOR_BUILD macro in # the AC archive is broken for autoconf 2.57. Given that there is no # newer version of that macro, we assume that it is also broken for # autoconf 2.61 and thus we use a simple but usually sufficient # approach. AC_MSG_CHECKING(for cc for build) if test "$cross_compiling" = "yes"; then CC_FOR_BUILD="${CC_FOR_BUILD-cc}" else CC_FOR_BUILD="${CC_FOR_BUILD-$CC}" fi AC_MSG_RESULT($CC_FOR_BUILD) AC_ARG_VAR(CC_FOR_BUILD,[build system C compiler]) # Don't default to build static libs. LT_PREREQ([2.2.6]) LT_INIT([win32-dll disable-static]) LT_LANG([Windows Resource]) # For now we hardcode the use of version scripts. It would be better # to write a test for this or even implement this within libtool. have_ld_version_script=no case "${host}" in *-*-linux*) have_ld_version_script=yes ;; *-*-gnu*) have_ld_version_script=yes ;; *-apple-darwin*) AC_DEFINE(_DARWIN_C_SOURCE, 900000L, Expose all libc features (__DARWIN_C_FULL).) AC_DEFINE(_XOPEN_SOURCE, 500, Activate POSIX interface on MacOS X) ;; esac AM_CONDITIONAL(HAVE_LD_VERSION_SCRIPT, test "$have_ld_version_script" = "yes") GPG_DEFAULT=no GPGSM_DEFAULT=no GPGCONF_DEFAULT=no G13_DEFAULT=no component_system=None have_dosish_system=no have_android_system=no have_w32_system=no have_w64_system=no have_macos_system=no build_w32_glib=no build_w32_qt=no available_languages="cl cpp python qt" default_languages="cl cpp python qt" case "${host}" in x86_64-*mingw32*) have_w64_system=yes ;; *-linux-androideabi) have_android_system=yes ;; *-apple-darwin*) have_macos_system=yes ;; esac case "${host}" in *-mingw32*) have_dosish_system=yes have_w32_system=yes GPG_DEFAULT='c:\\gnupg\\gpg.exe' GPGSM_DEFAULT='c:\\gnupg\\gpgsm.exe' GPGCONF_DEFAULT='c:\\gnupg\\gpgconf.exe' G13_DEFAULT='c:\\gnupg\\g13.exe' #component_system='COM+' AM_PATH_GLIB_2_0 AC_ARG_ENABLE(w32-glib, AC_HELP_STRING([--enable-w32-glib], [build GPGME Glib for W32]), build_w32_glib=$enableval) ;; *) # XXX: Probably use exec-prefix here? # GPG_DEFAULT='/usr/bin/gpg' # GPGSM_DEFAULT='/usr/bin/gpgsm' # GPGCONF_DEFAULT='/usr/bin/gpgconf' # G13_DEFAULT='/usr/bin/g13' ;; 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) if test "$have_w32_system" = yes; then AC_DEFINE(HAVE_W32_SYSTEM,1, [Defined if we run on any kind of W32 API based system]) fi AM_CONDITIONAL(HAVE_W32_SYSTEM, test "$have_w32_system" = yes) if test "$have_w64_system" = yes; then AC_DEFINE(HAVE_W64_SYSTEM,1, [Defined if we run on a 64 bit W32 API based system]) fi AM_CONDITIONAL(HAVE_W64_SYSTEM, test "$have_w64_system" = yes) if test "$have_android_system" = yes; then AC_DEFINE(HAVE_ANDROID_SYSTEM,1, [Defined if we build for an Android system]) fi AM_CONDITIONAL(HAVE_ANDROID_SYSTEM, test "$have_android_system" = yes) if test "$have_macos_system" = yes; then AC_DEFINE(HAVE_MACOS_SYSTEM,1, [Defined if we build for an MacOS system]) fi AM_CONDITIONAL(HAVE_MACOS_SYSTEM, test "$have_macos_system" = yes) AM_CONDITIONAL(BUILD_W32_GLIB, test "$build_w32_glib" = yes) AC_ARG_ENABLE([fixed-path], AC_HELP_STRING([--enable-fixed-path=PATH], [locate binaries only via this PATH]), [fixed_search_path="$enableval"], [fixed_search_path=""]) if test x$fixed_search_path != x ; then AC_DEFINE_UNQUOTED(FIXED_SEARCH_PATH, "$fixed_search_path", [Locate binaries only via this PATH]) fi # Note: You need to declare all possible languages also in # lang/Makefile.am's DIST_SUBDIRS. AC_ARG_ENABLE([languages], AC_HELP_STRING([--enable-languages=languages], [enable only specific language bindings]), [enabled_languages=`echo $enableval | \ tr ',:' ' ' | tr '[A-Z]' '[a-z]' | \ sed 's/c++/cpp/'`], [enabled_languages="maybe"]) if test "x$enabled_languages" = "x" \ -o "$enabled_languages" = "no"; then enabled_languages= fi # If languages are explicitly set missing requirements # for the languages are treated as errors otherwise # there will be a warning. explicit_languages=1 if test "x$enabled_languages" = "xmaybe"; then explicit_languages=0 enabled_languages="$default_languages" fi for language in $enabled_languages; do LIST_MEMBER($language, $available_languages) if test "$found" = "0"; then AC_MSG_ERROR([unsupported language binding specified]) fi done # Enable C++ 11 if cpp language is requested LIST_MEMBER("cpp", $enabled_languages) if test "$found" = "1"; then AX_CXX_COMPILE_STDCXX(11, noext, optional) if test "$HAVE_CXX11" != "1"; then if test "$explicit_languages" = "1"; then AC_MSG_ERROR([[ *** *** A compiler with c++11 support is required for the c++ binding. ***]]) else enabled_languages=$(echo $enabled_languages | sed 's/cpp//') enabled_languages=$(echo $enabled_languages | sed 's/qt//') AC_MSG_WARN([[ *** *** No c++11 support detected. C++ and Qt bindings will be disabled. ***]]) fi fi fi # Check that if qt is enabled cpp also is enabled LIST_MEMBER("qt", $enabled_languages) if test "$found" = "1"; then # We need to ensure that in the language order qt comes after cpp # so we remove qt first and explicitly add it as last list member. enabled_languages=$(echo $enabled_languages | sed 's/qt//') LIST_MEMBER("cpp", $enabled_languages) if test "$found" = "0"; then AC_MSG_ERROR([[ *** *** Qt language binding depends on cpp binding. ***]]) fi FIND_QT if test "$have_qt5_libs" != "yes"; then if test "$explicit_languages" = "1"; then AC_MSG_ERROR([[ *** *** Qt5 (Qt5Core) is required for Qt binding. ***]]) else AC_MSG_WARN([[ *** *** Qt5 (Qt5Core) not found Qt Binding will be disabled. ***]]) fi else enabled_languages=`echo $enabled_languages qt` AC_CHECK_PROGS([DOXYGEN], [doxygen]) if test -z "$DOXYGEN"; # This is not highlighted because it's not really important. then AC_MSG_WARN([Doxygen not found - Qt binding doc will not be built.]) fi AC_CHECK_PROGS([GRAPHVIZ], [dot]) if test -z "$GRAPHVIZ"; then AC_MSG_WARN([Graphviz not found - Qt binding doc will not have diagrams.]) fi fi fi AM_CONDITIONAL([HAVE_DOXYGEN], [test -n "$DOXYGEN"]) if test -n "$GRAPHVIZ"; then HAVE_DOT="YES" else HAVE_DOT="NO" fi AC_SUBST(HAVE_DOT) # Python bindings. LIST_MEMBER("python", $enabled_languages) found_py=$found if test "$found_py" = "1"; then AX_PKG_SWIG if test -z "$SWIG"; then if test "$explicit_languages" = "1"; then AC_MSG_ERROR([[ *** *** You need SWIG to build the Python bindings. ***]]) else enabled_languages=$(echo $enabled_languages | sed 's/python//') fi else # Reset the version collecting vars. PYTHONS= PYTHON_VERSIONS= if test "$found_py" = "1" -o "$found_py3" = "1"; then # Reset everything, so that we can look for another Python. m4_foreach([mym4pythonver], [[2.7],[3.4],[3.5],[3.6],[3.7],[3.8],[all]], [unset PYTHON unset PYTHON_VERSION unset PYTHON_CPPFLAGS unset PYTHON_LDFLAGS unset PYTHON_SITE_PKG unset PYTHON_EXTRA_LIBS unset PYTHON_EXTRA_LDFLAGS unset ac_cv_path_PYTHON unset am_cv_pathless_PYTHON unset am_cv_python_version unset am_cv_python_platform unset am_cv_python_pythondir unset am_cv_python_pyexecdir AM_PATH_PYTHON(mym4pythonver, [ AX_PYTHON_DEVEL if test "$PYTHON_VERSION"; then PYTHONS="$(echo $PYTHONS $PYTHON)" PYTHON_VERSIONS="$(echo $PYTHON_VERSIONS $PYTHON_VERSION)" fi ], :, m4_if([mym4pythonver],[all],[],[python]mym4pythonver)) ]) fi # Recover some values lost in the second attempt to find Python. PYTHON="$(echo $PYTHONS | cut -d ' ' -f 1)" PYTHON_VERSION="$(echo $PYTHON_VERSIONS | cut -d ' ' -f 1)" # Remove duplicates. PYTHONS="$(echo $PYTHONS | tr '[[:space:]]' '\n' | sort | uniq | tr '\n' ' ' | sed -e 's/ $//')" PYTHON_VERSIONS="$(echo $PYTHON_VERSIONS | tr '[[:space:]]' '\n' | sort | uniq | tr '\n' ' ' | sed -e 's/ $//')" if test "$PYTHON_VERSIONS"; then enabled_languages_v=$(echo $enabled_languages | sed -Ee "s/python[[23]]?/python ($PYTHON_VERSIONS)/") enabled_languages=$(echo $enabled_languages | sed -Ee "s/python[[23]]?/python/") else if test "$explicit_languages" = "1"; then AC_MSG_ERROR([[ *** *** Please install the python development packages. ***]]) else enabled_languages=$(echo $enabled_languages | sed 's/python//') fi fi AC_SUBST(PYTHONS, $PYTHONS) fi fi AC_SUBST(ENABLED_LANGUAGES, $enabled_languages) # # Provide information about the build. # BUILD_REVISION="mym4_revision" AC_SUBST(BUILD_REVISION) AC_DEFINE_UNQUOTED(BUILD_REVISION, "$BUILD_REVISION", [GIT commit id revision used to build this package]) changequote(,)dnl BUILD_VERSION=`echo "$PACKAGE_VERSION" | sed 's/\([0-9.]*\).*/\1./'` changequote([,])dnl BUILD_VERSION="${BUILD_VERSION}mym4_revision_dec" BUILD_FILEVERSION=`echo "${BUILD_VERSION}" | tr . ,` AC_SUBST(BUILD_VERSION) AC_SUBST(BUILD_FILEVERSION) AC_ARG_ENABLE([build-timestamp], AC_HELP_STRING([--enable-build-timestamp], [set an explicit build timestamp for reproducibility. (default is the current time in ISO-8601 format)]), [if test "$enableval" = "yes"; then BUILD_TIMESTAMP=`date -u +%Y-%m-%dT%H:%M+0000 2>/dev/null || date` else BUILD_TIMESTAMP="$enableval" fi], [BUILD_TIMESTAMP=""]) AC_SUBST(BUILD_TIMESTAMP) AC_DEFINE_UNQUOTED(BUILD_TIMESTAMP, "$BUILD_TIMESTAMP", [The time this package was configured for a build]) # # Options to disable some regression tests # run_gpgconf_test="yes" AC_ARG_ENABLE(gpgconf-test, AC_HELP_STRING([--disable-gpgconf-test], [disable GPGCONF regression test]), run_gpgconf_test=$enableval) AM_CONDITIONAL(RUN_GPGCONF_TESTS, test "$run_gpgconf_test" = "yes") run_gpg_test="yes" AC_ARG_ENABLE(gpg-test, AC_HELP_STRING([--disable-gpg-test], [disable GPG regression test]), run_gpg_test=$enableval) AM_CONDITIONAL(RUN_GPG_TESTS, test "$run_gpg_test" = "yes") run_gpgsm_test="yes" AC_ARG_ENABLE(gpgsm-test, AC_HELP_STRING([--disable-gpgsm-test], [disable GPGSM regression test]), run_gpgsm_test=$enableval) AM_CONDITIONAL(RUN_GPGSM_TESTS, test "$run_gpgsm_test" = "yes") run_g13_test="yes" AC_ARG_ENABLE(g13-test, AC_HELP_STRING([--disable-g13-test], [disable G13 regression test]), run_g13_test=$enableval) AM_CONDITIONAL(RUN_G13_TESTS, test "$run_g13_test" = "yes") # Checks for header files. AC_CHECK_HEADERS_ONCE([locale.h sys/select.h sys/uio.h argp.h stdint.h unistd.h sys/time.h sys/types.h sys/stat.h]) # Type checks. AC_C_INLINE AC_CHECK_SIZEOF(unsigned int) AC_SYS_LARGEFILE AC_TYPE_OFF_T AC_TYPE_UINTPTR_T # We require uint64_t if test "$ac_cv_header_stdint_h" != yes; then AC_MSG_ERROR([[ *** *** No stdint.h and thus no uint64_t type. Can't build this library. ***]]) fi # A simple compile time check in gpgme.h for GNU/Linux systems that # prevents a file offset bits mismatch between gpgme and the application. NEED__FILE_OFFSET_BITS=0 if test "$have_w32_system" != yes; then case "$ac_cv_sys_file_offset_bits" in "" | no | unknown) ;; *) NEED__FILE_OFFSET_BITS=$ac_cv_sys_file_offset_bits ;; esac fi AC_SUBST(NEED__FILE_OFFSET_BITS) # Figure out platform dependent typedefs for gpgme.h if test "$have_w32_system" = yes; then INSERT__TYPEDEFS_FOR_GPGME_H=" #ifdef _WIN64 # include typedef int64_t gpgme_off_t; typedef int64_t gpgme_ssize_t; #else /* _WIN32 */ typedef long gpgme_off_t; typedef long gpgme_ssize_t; #endif /* _WIN32 */" API__OFF_T="gpgme_off_t" API__SSIZE_T="gpgme_ssize_t" else INSERT__TYPEDEFS_FOR_GPGME_H=" #include typedef off_t gpgme_off_t; typedef ssize_t gpgme_ssize_t;" API__OFF_T="off_t" API__SSIZE_T="ssize_t" fi AC_SUBST(INSERT__TYPEDEFS_FOR_GPGME_H) AM_SUBST_NOTMAKE(INSERT__TYPEDEFS_FOR_GPGME_H) AC_SUBST(API__OFF_T) AM_SUBST_NOTMAKE(API__OFF_T) AC_SUBST(API__SSIZE_T) AM_SUBST_NOTMAKE(API__SSIZE_T) # Checks for compiler features. if test "$GCC" = yes; then CFLAGS="$CFLAGS -Wall -Wcast-align -Wshadow -Wstrict-prototypes" if test "$USE_MAINTAINER_MODE" = "yes"; then CFLAGS="$CFLAGS -Wformat -Wno-format-y2k -Wformat-security" # If -Wno-missing-field-initializers is supported we can enable a # a bunch of really useful warnings. AC_MSG_CHECKING([if gcc supports -Wno-missing-field-initializers]) _gcc_cflags_save=$CFLAGS CFLAGS="-Wno-missing-field-initializers" AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[])],_gcc_wopt=yes,_gcc_wopt=no) AC_MSG_RESULT($_gcc_wopt) CFLAGS=$_gcc_cflags_save; if test x"$_gcc_wopt" = xyes ; then CFLAGS="$CFLAGS -W -Wextra -Wbad-function-cast" CFLAGS="$CFLAGS -Wwrite-strings" CFLAGS="$CFLAGS -Wdeclaration-after-statement" CFLAGS="$CFLAGS -Wno-missing-field-initializers" CFLAGS="$CFLAGS -Wno-sign-compare" fi CXXFLAGS="$CXXFLAGS -Wall -Wextra -Wno-shadow" AC_MSG_CHECKING([if gcc supports -Wpointer-arith]) _gcc_cflags_save=$CFLAGS CFLAGS="-Wpointer-arith" AC_COMPILE_IFELSE([AC_LANG_PROGRAM([],[])],_gcc_wopt=yes,_gcc_wopt=no) AC_MSG_RESULT($_gcc_wopt) CFLAGS=$_gcc_cflags_save; if test x"$_gcc_wopt" = xyes ; then CFLAGS="$CFLAGS -Wpointer-arith" fi fi if test "$have_w32_system" = yes; then CFLAGS="$CFLAGS -mms-bitfields" fi fi # Only used for debugging, so no serious test needed (for actual # functionality you have to test libc as well, this only tests the # compiler). AC_CACHE_CHECK([for __thread],[gpgme_cv_tls_works], AC_COMPILE_IFELSE([AC_LANG_PROGRAM([__thread int foo;])], gpgme_cv_tls_works=yes,gpgme_cv_tls_works=no)) if test "$gpgme_cv_tls_works" = yes; then AC_DEFINE(HAVE_TLS, [1], [Define if __thread is supported]) fi # Checks for library functions. AC_MSG_NOTICE([checking for libraries]) AC_FUNC_FSEEKO # Try to find a thread-safe version of ttyname(). gnupg_REPLACE_TTYNAME_R if test "$ac_cv_func_ttyname_r" != yes; then AC_MSG_WARN([ *** *** ttyname() is not thread-safe and ttyname_r() does not exist ***]) fi # Try to find a thread-safe version of getenv(). have_thread_safe_getenv=no jm_GLIBC21 if test $GLIBC21 = yes -o $have_w32_system = yes; then have_thread_safe_getenv=yes fi if test $have_thread_safe_getenv = yes; then AC_DEFINE(HAVE_THREAD_SAFE_GETENV, [1], [Define if getenv() is thread-safe]) fi have_getenv_r=no AC_CHECK_FUNCS(getenv_r, have_getenv_r=yes) if test $have_getenv_r = no && test $have_thread_safe_getenv = no; then AC_MSG_WARN([ *** *** getenv() is not thread-safe and getenv_r() does not exist ***]) fi # For converting time strings to seconds since Epoch, we need the timegm # function. AC_CHECK_FUNCS(timegm) if test "$ac_cv_func_timegm" != yes; then AC_MSG_WARN([ *** *** timegm() not available - a non-thread-safe kludge will be used *** and the TZ variable might be changed at runtime. ***]) fi AC_CHECK_FUNCS(setlocale) # Checking for libgpg-error. have_gpg_error=no AM_PATH_GPG_ERROR("$NEED_GPG_ERROR_VERSION", have_gpg_error=yes, have_gpg_error=no) AC_DEFINE(GPG_ERR_SOURCE_DEFAULT, GPG_ERR_SOURCE_GPGME, [The default error source for GPGME.]) # And for libassuan. have_libassuan=no AM_PATH_LIBASSUAN("$NEED_LIBASSUAN_API:$NEED_LIBASSUAN_VERSION", have_libassuan=yes, have_libassuan=no) if test "$have_libassuan" = "yes"; then AC_DEFINE_UNQUOTED(GPGME_LIBASSUAN_VERSION, "$libassuan_version", [version of the libassuan library]) fi # # Other checks # # Check for getgid etc AC_CHECK_FUNCS(getgid getegid closefrom) # Replacement functions. AC_REPLACE_FUNCS(stpcpy) AC_REPLACE_FUNCS(setenv) # Assuan check for descriptor passing. AC_CHECK_MEMBER(struct cmsghdr.cmsg_len, [supports_descriptor_passing=yes], [supports_descriptor_passing=no AC_MSG_WARN([ *** *** Data structure for sending ancillary data missing. *** Descriptor passing won't work. ***])],[ #include #include #include #include #include #include #if HAVE_SYS_UIO_H #include #endif #include ]) use_descriptor_passing=yes AC_ARG_ENABLE(fd-passing, AC_HELP_STRING([--disable-fd-passing], [do not use FD passing]), use_descriptor_passing=$enableval) if test "$supports_descriptor_passing" != "yes"; then use_descriptor_passing=no fi if test "$use_descriptor_passing" = "yes"; then AC_DEFINE(USE_DESCRIPTOR_PASSING,1, [Defined if descriptor passing is enabled and supported]) fi AM_CONDITIONAL(USE_DESCRIPTOR_PASSING, test "$use_descriptor_passing" = "yes") uiserver=no if test "$use_descriptor_passing" = "yes" && test "$have_libassuan" = "yes"; then uiserver=yes fi if test "$uiserver" != "no"; then AC_DEFINE(ENABLE_UISERVER, 1, [Defined if we are building with uiserver support.]) fi AM_CONDITIONAL(HAVE_UISERVER, test "$uiserver" != "no") # Option --disable-linux-getdents # # By default we use SYS_getdents on Linux to optimize fd closing # before an exec. This option allows to switch this optimization off. use_linux_getdents=yes AC_ARG_ENABLE(linux-getdents, AC_HELP_STRING([--disable-linux-getdents], [do not use SYS_getdents on Linux]), use_linux_getdents=$enableval) if test "$use_linux_getdents" = "yes"; then case "${host}" in *-*-linux*) AC_DEFINE(USE_LINUX_GETDENTS,1, [Defined if SYS_getdents can be used on Linux]) ;; esac fi # # Add a few constants to help porting to W32 # AH_VERBATIM([SEPCONSTANTS], [ /* Separators as used in $PATH and file name. */ #ifdef HAVE_DOSISH_SYSTEM #define PATHSEP_C ';' #define DIRSEP_C '\\' #define DIRSEP_S "\\" #else #define PATHSEP_C ':' #define DIRSEP_C '/' #define DIRSEP_S "/" #endif ]) AH_BOTTOM([ /* Definition of GCC specific attributes. */ #if __GNUC__ > 2 # define GPGME_GCC_A_PURE __attribute__ ((__pure__)) #else # define GPGME_GCC_A_PURE #endif /* Under WindowsCE we need gpg-error's strerror macro. */ #define GPG_ERR_ENABLE_ERRNO_MACROS 1 #define CRIGHTBLURB "Copyright (C) 2000 Werner Koch\n" \ "Copyright (C) 2001--2018 g10 Code GmbH\n" ]) # Substitution used for gpgme-config GPGME_CONFIG_LIBS="-lgpgme" GPGME_CONFIG_CFLAGS="" GPGME_CONFIG_HOST="$host" GPGME_CONFIG_AVAIL_LANG="$enabled_languages" AC_SUBST(GPGME_CONFIG_API_VERSION) AC_SUBST(GPGME_CONFIG_LIBS) AC_SUBST(GPGME_CONFIG_CFLAGS) AC_SUBST(GPGME_CONFIG_HOST) AC_SUBST(GPGME_CONFIG_AVAIL_LANG) # Frob'da Variables LTLIBOBJS=`echo "$LIB@&t@OBJS" | sed 's,\.[[^.]]* ,.lo ,g;s,\.[[^.]]*$,.lo,'` AC_SUBST(LTLIBOBJS) # Some checks for gpgme-tool # Done at top: AC_CHECK_HEADER([argp.h]) AC_CHECK_TYPES([error_t], [], [AC_DEFINE([error_t], [int], [Define to a type to use for `error_t' if it is not otherwise available.])], [#include ]) # A substitution to set generated files in a Emacs buffer to read-only. AC_SUBST(emacs_local_vars_begin, [['Local][ ][Variables:']]) AC_SUBST(emacs_local_vars_read_only, ['buffer-read-only: t']) AC_SUBST(emacs_local_vars_end, ['End:']) # Last check. die=no if test "$have_gpg_error" = "no"; then die=yes AC_MSG_NOTICE([[ *** *** You need libgpg-error to build this program. ** This library is for example available at *** https://www.gnupg.org/ftp/gcrypt/libgpg-error/ *** (at least version $NEED_GPG_ERROR_VERSION is required.) ***]]) fi if test "$have_libassuan" = "no"; then die=yes AC_MSG_NOTICE([[ *** *** You need libassuan to build this program. *** This library is for example available at *** https://www.gnupg.org/ftp/gcrypt/libassuan/ *** (at least version $NEED_LIBASSUAN_VERSION (API $NEED_LIBASSUAN_API) is required). ***]]) fi if test "$die" = "yes"; then AC_MSG_ERROR([[ *** *** Required libraries not found. Please consult the above messages *** and install them before running configure again. ***]]) fi # # Create config files AC_CONFIG_FILES(Makefile src/Makefile tests/Makefile tests/gpg/Makefile tests/gpgsm/Makefile tests/opassuan/Makefile tests/json/Makefile doc/Makefile src/versioninfo.rc src/gpgme.pc src/gpgme-glib.pc src/gpgme.h) AC_CONFIG_FILES(src/gpgme-config, chmod +x src/gpgme-config) AC_CONFIG_FILES(lang/cpp/Makefile lang/cpp/src/Makefile) AC_CONFIG_FILES(lang/cpp/tests/Makefile) AC_CONFIG_FILES(lang/cpp/src/GpgmeppConfig-w32.cmake.in) AC_CONFIG_FILES(lang/cpp/src/GpgmeppConfig.cmake.in) AC_CONFIG_FILES(lang/cpp/src/GpgmeppConfigVersion.cmake) AC_CONFIG_FILES(lang/cpp/src/gpgmepp_version.h) AC_CONFIG_FILES(lang/qt/Makefile lang/qt/src/Makefile) AC_CONFIG_FILES(lang/qt/src/QGpgmeConfig-w32.cmake.in) AC_CONFIG_FILES(lang/qt/src/QGpgmeConfig.cmake.in) AC_CONFIG_FILES(lang/qt/src/QGpgmeConfigVersion.cmake) AC_CONFIG_FILES(lang/qt/tests/Makefile) AC_CONFIG_FILES(lang/qt/src/qgpgme_version.h) AC_CONFIG_FILES([lang/Makefile lang/cl/Makefile lang/cl/gpgme.asd]) AM_COND_IF([HAVE_DOXYGEN], [AC_CONFIG_FILES([lang/qt/doc/Doxyfile])]) AC_CONFIG_FILES([lang/js/Makefile lang/js/src/Makefile lang/js/BrowserTestExtension/Makefile lang/js/DemoExtension/Makefile]) AC_CONFIG_FILES(lang/qt/doc/Makefile) AC_CONFIG_FILES([lang/python/Makefile lang/python/version.py lang/python/tests/Makefile]) AC_CONFIG_FILES([lang/python/setup.py], [chmod a+x lang/python/setup.py]) AC_OUTPUT echo " GPGME v${VERSION} has been configured as follows: Revision: mym4_revision (mym4_revision_dec) Platform: $host UI Server: $uiserver FD Passing: $use_descriptor_passing Language bindings: ${enabled_languages_v:-$enabled_languages} " if test "x${gpg_config_script_warn}" != x; then cat <. +# SPDX-License-Identifier: LGPL-2.1-or-later ## Process this file with automake to produce Makefile.in DISTCLEANFILES = gpgme.tmp CLEANFILES = mkdefsinc defs.inc EXTRA_DIST = module-overview.sk HACKING DCO ChangeLog-2011 \ mkdefsinc.c defsincdate \ examples/gpgme-mozilla.json examples/gpgme-chrome.json BUILT_SOURCES = defsincdate defs.inc info_TEXINFOS = gpgme.texi gpgme_TEXINFOS = uiserver.texi lesser.texi gpl.texi gpgme.texi : defs.inc mkdefsinc: mkdefsinc.c Makefile $(top_builddir)/conf/config.h $(CC_FOR_BUILD) -I. -I$(top_builddir)/conf -I$(srcdir) \ $(AM_CPPFLAGS) -o $@ $(srcdir)/mkdefsinc.c dist-hook: defsincdate defsincdate: $(gpgme_TEXINFOS) : >defsincdate ; \ if test -e $(top_srcdir)/.git; then \ (cd $(srcdir) && git log -1 --format='%ct' -- \ $(info_TEXINFOS) $(gpgme_TEXINFOS) 2>/dev/null) >>defsincdate; \ fi defs.inc: defsincdate Makefile mkdefsinc incd="`test -f defsincdate || echo '$(srcdir)/'`defsincdate"; \ ./mkdefsinc -C $(srcdir) --date "`cat $$incd 2>/dev/null`" \ $(info_TEXINFOS) $(gpgme_TEXINFOS) >$@ online: gpgme.html gpgme.pdf set -e; \ echo "Uploading current manuals to www.gnupg.org ..."; \ user=werner ; \ (cd gpgme.html && rsync -vr --exclude='.svn' . \ $${user}@ftp.gnupg.org:webspace/manuals/gpgme/ ); \ rsync -v gpgme.pdf $${user}@ftp.gnupg.org:webspace/manuals/ diff --git a/lang/Makefile.am b/lang/Makefile.am index 1bf73316..0cc36c27 100644 --- a/lang/Makefile.am +++ b/lang/Makefile.am @@ -1,23 +1,23 @@ # Makefile.am for gpgme/lang. # Copyright (C) 2003, 2006 g10 Code GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser 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 +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later SUBDIRS = $(ENABLED_LANGUAGES) DIST_SUBDIRS = cl cpp qt python js EXTRA_DIST = README diff --git a/lang/cl/Makefile.am b/lang/cl/Makefile.am index dee07119..a42c49cb 100644 --- a/lang/cl/Makefile.am +++ b/lang/cl/Makefile.am @@ -1,27 +1,26 @@ # Makefile.am for GPGME-CL. # Copyright (C) 2003, 2006 g10 Code GmbH # # This file is part of GPGME-CL. # # GPGME-CL 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. # # GPGME-CL 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 Lesser 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 +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later clfiles = gpgme.asd gpgme-package.lisp gpgme-grovel.lisp gpgme.lisp # FIXME: Should be configurable. clfilesdir = $(datadir)/common-lisp/source/gpgme dist_clfiles_DATA = $(clfiles) EXTRA_DIST = README ChangeLog-2011 diff --git a/lang/cpp/Makefile.am b/lang/cpp/Makefile.am index acfa0f9b..724da6ca 100644 --- a/lang/cpp/Makefile.am +++ b/lang/cpp/Makefile.am @@ -1,24 +1,23 @@ # Makefile.am for GPGMEPP. # Copyright (C) 2016 Bundesamt für Sicherheit in der Informationstechnik # Software engineering by Intevation GmbH # # This file is part of GPGMEPP. # # GPGME-CL 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. # # GPGME-CL 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 Lesser 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 +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later SUBDIRS = src tests EXTRA_DIST = README diff --git a/lang/js/Makefile.am b/lang/js/Makefile.am index 63cc41bd..c65eb4a7 100644 --- a/lang/js/Makefile.am +++ b/lang/js/Makefile.am @@ -1,32 +1,31 @@ # Makefile.am for gpgme.js. # Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik # # This file is part of gpgme.js. # # gpgme.js 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. # # gpgme.js 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 Lesser 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 +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later SUBDIRS = src BrowserTestExtension DemoExtension EXTRA_DIST = build_extensions.sh \ jsdoc.conf \ jsdoc_index.md \ .eslintrc.json \ package.json \ README \ unittest_inputvalues.js \ unittests.js \ webpack.conf.js \ webpack.conf_unittests.js diff --git a/lang/python/Makefile.am b/lang/python/Makefile.am index 1c7249a4..c888448f 100644 --- a/lang/python/Makefile.am +++ b/lang/python/Makefile.am @@ -1,104 +1,105 @@ # Makefile.am for the Python bindings. # Copyright (C) 2016 g10 Code GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser General Public -# License along with this program; if not, see . +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later EXTRA_DIST = \ README \ MANIFEST.in \ gpgme.i \ helpers.c helpers.h private.h \ examples \ doc \ src SUBDIRS = . tests .PHONY: prepare prepare: copystamp # For VPATH builds we need to copy some files because Python's # distutils are not VPATH-aware. copystamp: ln -sf "$(top_srcdir)/src/data.h" . ln -sf "$(top_builddir)/conf/config.h" . ln -sf "$(srcdir)/src" gpg touch $@ all-local: copystamp set -e ; for PYTHON in $(PYTHONS); do \ CPP="$(CPP)" \ CFLAGS="$(CFLAGS)" \ srcdir="$(srcdir)" \ top_builddir="$(top_builddir)" \ $$PYTHON setup.py build --verbose --build-base="$$(basename "$${PYTHON}")-gpg" ; \ done python$(PYTHON_VERSION)-gpg/dist/gpg-$(VERSION).tar.gz.asc: copystamp $(MKDIR_P) python$(PYTHON_VERSION)-gpg-dist CPP="$(CPP)" \ CFLAGS="$(CFLAGS)" \ srcdir="$(srcdir)" \ top_builddir="$(top_builddir)" \ $(PYTHON) setup.py sdist --verbose --dist-dir=python$(PYTHON_VERSION)-gpg-dist \ --manifest=python$(PYTHON_VERSION)-gpg-dist/MANIFEST gpgbin=gpgconf --list-components | grep OpenPGP | sed -e 's/gpg:OpenPGP://g' $(gpgbin) --detach-sign --armor python$(PYTHON_VERSION)-gpg-dist/gpg-$(VERSION).tar.gz .PHONY: sdist sdist: python$(PYTHON_VERSION)-gpg/dist/gpg-$(VERSION).tar.gz.asc .PHONY: upload upload: python$(PYTHON_VERSION)-gpg-dist/gpg-$(VERSION).tar.gz \ python$(PYTHON_VERSION)-gpg-dist/gpg-$(VERSION).tar.gz.asc twine upload $^ CLEANFILES = copystamp \ config.h \ data.h \ gpg # Remove the rest. # # 'make distclean' clears the write bit, breaking rm -rf. Fix the # permissions. clean-local: rm -rf -- build for PYTHON in $(PYTHONS); do \ find "$$(basename "$${PYTHON}")-gpg" -type d ! -perm -200 -exec chmod u+w {} ';' ; \ rm -rf -- "$$(basename "$${PYTHON}")-gpg" ; \ done install-exec-local: set -e ; for PYTHON in $(PYTHONS); do \ CPP="$(CPP)" \ CFLAGS="$(CFLAGS)" \ srcdir="$(srcdir)" \ top_builddir="$(top_builddir)" \ $$PYTHON setup.py \ build \ --build-base="$$(basename "$${PYTHON}")-gpg" \ install \ --prefix "$(DESTDIR)$(prefix)" \ --verbose ; \ done uninstall-local: set -x; GV=$$(echo $(VERSION) | tr - _); for PYTHON in $(PYTHONS); do \ PLATLIB="$(prefix)/$$("$${PYTHON}" -c 'import sysconfig, os; print(os.path.relpath(sysconfig.get_path("platlib", scheme="posix_prefix"), sysconfig.get_config_var("prefix")))')" ; \ rm -rf -- "$(DESTDIR)$${PLATLIB}/gpg" \ "$(DESTDIR)$${PLATLIB}"/gpg-$$GV-py*.egg-info ; \ done diff --git a/lang/qt/Makefile.am b/lang/qt/Makefile.am index a1b83e8d..a9e39769 100644 --- a/lang/qt/Makefile.am +++ b/lang/qt/Makefile.am @@ -1,30 +1,29 @@ # Makefile.am for GPGMEPP. # Copyright (C) 2016 Bundesamt für Sicherheit in der Informationstechnik # Software engineering by Intevation GmbH # # This file is part of GPGMEPP. # # GPGME-CL 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. # # GPGME-CL 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 Lesser 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 +# You should have received a copy of the GNU Lesser General Public +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later if RUN_GPG_TESTS tests = tests else tests = endif SUBDIRS = src ${tests} doc EXTRA_DIST = README diff --git a/src/Makefile.am b/src/Makefile.am index 435544ae..d85a85c9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,169 +1,170 @@ # Copyright (C) 2000 Werner Koch (dd9jn) # Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 g10 Code GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser General Public -# License along with this program; if not, see . +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later ## Process this file with automake to produce Makefile.in pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = gpgme.pc gpgme-glib.pc EXTRA_DIST = gpgme-config.in gpgme.m4 libgpgme.vers ChangeLog-2011 \ gpgme.h.in versioninfo.rc.in gpgme.def \ gpgme.pc.in gpgme-glib.pc.in bin_SCRIPTS = gpgme-config m4datadir = $(datadir)/aclocal m4data_DATA = gpgme.m4 nodist_include_HEADERS = gpgme.h bin_PROGRAMS = gpgme-tool gpgme-json if BUILD_W32_GLIB ltlib_gpgme_glib = libgpgme-glib.la else ltlib_gpgme_glib = endif lib_LTLIBRARIES = libgpgme.la $(ltlib_gpgme_glib) if HAVE_LD_VERSION_SCRIPT libgpgme_version_script_cmd = -Wl,--version-script=$(srcdir)/libgpgme.vers else libgpgme_version_script_cmd = endif if HAVE_DOSISH_SYSTEM system_components = w32-util.c system_components_not_extra = w32-io.c else system_components = ath.h posix-util.c posix-io.c system_components_not_extra = endif if HAVE_UISERVER uiserver_components = engine-uiserver.c else uiserver_components = endif # These are the source files common to all library versions. We used # to build a non-installed library for that, but that does not work # correctly on all platforms (in particular, one can not specify the # right linking order with libtool, as the non-installed version has # unresolved symbols to the thread module. main_sources = \ util.h conversion.c b64dec.c get-env.c context.h ops.h \ parsetlv.c parsetlv.h \ mbox-util.c mbox-util.h \ data.h data.c data-fd.c data-stream.c data-mem.c data-user.c \ data-estream.c \ data-compat.c data-identify.c \ signers.c sig-notation.c \ wait.c wait-global.c wait-private.c wait-user.c wait.h \ op-support.c \ encrypt.c encrypt-sign.c decrypt.c decrypt-verify.c verify.c \ sign.c passphrase.c progress.c \ key.c keylist.c keysign.c trust-item.c trustlist.c tofupolicy.c \ import.c export.c genkey.c delete.c edit.c getauditlog.c \ opassuan.c passwd.c spawn.c assuan-support.c \ engine.h engine-backend.h engine.c engine-gpg.c status-table.c \ engine-gpgsm.c engine-assuan.c engine-gpgconf.c \ $(uiserver_components) \ engine-g13.c vfs-mount.c vfs-create.c \ engine-spawn.c \ gpgconf.c queryswdb.c \ sema.h priv-io.h $(system_components) sys-util.h dirinfo.c \ debug.c debug.h gpgme.c version.c error.c \ ath.h ath.c libgpgme_la_SOURCES = $(main_sources) $(system_components_not_extra) if BUILD_W32_GLIB libgpgme_glib_la_SOURCES = $(main_sources) w32-glib-io.c endif # We use a global CFLAGS setting for all libraries # versions, because then every object file is only compiled once. AM_CFLAGS = @LIBASSUAN_CFLAGS@ @GLIB_CFLAGS@ gpgme_tool_SOURCES = gpgme-tool.c argparse.c argparse.h gpgme_tool_LDADD = libgpgme.la @LIBASSUAN_LIBS@ gpgme_json_SOURCES = gpgme-json.c cJSON.c cJSON.h gpgme_json_LDADD = -lm libgpgme.la $(GPG_ERROR_LIBS) if HAVE_W32_SYSTEM # Windows provides us with an endless stream of Tough Love. To spawn # processes with a controlled set of inherited handles, we need a # wrapper process. libexec_PROGRAMS = gpgme-w32spawn RCCOMPILE = $(RC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) LTRCCOMPILE = $(LIBTOOL) --mode=compile --tag=RC $(RCCOMPILE) SUFFIXES = .rc .lo .rc.lo: $(LTRCCOMPILE) -i "$<" -o "$@" gpgme_res = versioninfo.lo no_undefined = -no-undefined export_symbols = -export-symbols $(srcdir)/gpgme.def extra_ltoptions = -XCClinker -static-libgcc install-def-file: -$(INSTALL) -d $(DESTDIR)$(libdir) $(INSTALL) $(srcdir)/gpgme.def $(DESTDIR)$(libdir)/gpgme.def uninstall-def-file: -rm $(DESTDIR)$(libdir)/gpgme.def gpgme_deps = $(gpgme_res) gpgme.def else gpgme_res = no_undefined = export_symbols = extra_ltoptions = install-def-file: uninstall-def-file: gpgme_deps = endif libgpgme_la_LDFLAGS = $(no_undefined) $(export_symbols) $(extra_ltoptions) \ $(libgpgme_version_script_cmd) -version-info \ @LIBGPGME_LT_CURRENT@:@LIBGPGME_LT_REVISION@:@LIBGPGME_LT_AGE@ libgpgme_la_DEPENDENCIES = @LTLIBOBJS@ $(srcdir)/libgpgme.vers $(gpgme_deps) libgpgme_la_LIBADD = $(gpgme_res) @LIBASSUAN_LIBS@ @LTLIBOBJS@ \ @GPG_ERROR_LIBS@ if BUILD_W32_GLIB libgpgme_glib_la_LDFLAGS = \ $(no_undefined) $(export_symbols) $(extra_ltoptions) \ $(libgpgme_version_script_cmd) -version-info \ @LIBGPGME_LT_CURRENT@:@LIBGPGME_LT_REVISION@:@LIBGPGME_LT_AGE@ libgpgme_glib_la_DEPENDENCIES = @LTLIBOBJS@ \ $(srcdir)/libgpgme.vers $(gpgme_deps) libgpgme_glib_la_LIBADD = $(gpgme_res) @LIBASSUAN_LIBS@ @LTLIBOBJS@ \ @GPG_ERROR_LIBS@ @GLIB_LIBS@ endif install-data-local: install-def-file uninstall-local: uninstall-def-file diff --git a/src/assuan-support.c b/src/assuan-support.c index 705088e4..419adb41 100644 --- a/src/assuan-support.c +++ b/src/assuan-support.c @@ -1,294 +1,314 @@ +/* assuan-support.c - Assuan wrappers + * Copyright (C) 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ + + #if HAVE_CONFIG_H #include #endif #include #include #include #include "assuan.h" #include "gpgme.h" #include "ath.h" #include "priv-io.h" #include "debug.h" struct assuan_malloc_hooks _gpgme_assuan_malloc_hooks = { malloc, realloc, free }; int _gpgme_assuan_log_cb (assuan_context_t ctx, void *hook, unsigned int cat, const char *msg) { (void)ctx; (void)hook; (void)cat; if (msg == NULL) return 1; _gpgme_debug (DEBUG_ASSUAN, "%s", msg); return 0; } static void my_usleep (assuan_context_t ctx, unsigned int usec) { /* FIXME: Add to ath. */ __assuan_usleep (ctx, usec); } /* Create a pipe with an inheritable end. */ static int my_pipe (assuan_context_t ctx, assuan_fd_t fds[2], int inherit_idx) { int res; int gfds[2]; (void)ctx; res = _gpgme_io_pipe (gfds, inherit_idx); /* For now... */ fds[0] = (assuan_fd_t) gfds[0]; fds[1] = (assuan_fd_t) gfds[1]; return res; } /* Close the given file descriptor, created with _assuan_pipe or one of the socket functions. */ static int my_close (assuan_context_t ctx, assuan_fd_t fd) { (void)ctx; return _gpgme_io_close ((int) fd); } static gpgme_ssize_t my_read (assuan_context_t ctx, assuan_fd_t fd, void *buffer, size_t size) { (void)ctx; return _gpgme_io_read ((int) fd, buffer, size); } static gpgme_ssize_t my_write (assuan_context_t ctx, assuan_fd_t fd, const void *buffer, size_t size) { (void)ctx; return _gpgme_io_write ((int) fd, buffer, size); } static int my_recvmsg (assuan_context_t ctx, assuan_fd_t fd, assuan_msghdr_t msg, int flags) { (void)ctx; #ifdef HAVE_W32_SYSTEM (void)fd; (void)msg; (void)flags; gpg_err_set_errno (ENOSYS); return -1; #else return _gpgme_io_recvmsg ((int) fd, msg, flags); #endif } static int my_sendmsg (assuan_context_t ctx, assuan_fd_t fd, const assuan_msghdr_t msg, int flags) { (void)ctx; #ifdef HAVE_W32_SYSTEM (void)fd; (void)msg; (void)flags; gpg_err_set_errno (ENOSYS); return -1; #else return _gpgme_io_sendmsg ((int) fd, msg, flags); #endif } /* If NAME is NULL, don't exec, just fork. FD_CHILD_LIST is modified to reflect the value of the FD in the peer process (on Windows). */ static int my_spawn (assuan_context_t ctx, pid_t *r_pid, const char *name, const char **argv, assuan_fd_t fd_in, assuan_fd_t fd_out, assuan_fd_t *fd_child_list, void (*atfork) (void *opaque, int reserved), void *atforkvalue, unsigned int flags) { int err; struct spawn_fd_item_s *fd_items; int i; (void)ctx; (void)flags; assert (name); if (! name) { gpg_err_set_errno (ENOSYS); return -1; } i = 0; if (fd_child_list) { while (fd_child_list[i] != ASSUAN_INVALID_FD) i++; } /* fd_in, fd_out, terminator */ i += 3; fd_items = calloc (i, sizeof (struct spawn_fd_item_s)); if (! fd_items) return -1; i = 0; if (fd_child_list) { while (fd_child_list[i] != ASSUAN_INVALID_FD) { fd_items[i].fd = (int) fd_child_list[i]; fd_items[i].dup_to = -1; i++; } } if (fd_in != ASSUAN_INVALID_FD) { fd_items[i].fd = (int) fd_in; fd_items[i].dup_to = 0; i++; } if (fd_out != ASSUAN_INVALID_FD) { fd_items[i].fd = (int) fd_out; fd_items[i].dup_to = 1; i++; } fd_items[i].fd = -1; fd_items[i].dup_to = -1; err = _gpgme_io_spawn (name, (char*const*)argv, (IOSPAWN_FLAG_NOCLOSE | IOSPAWN_FLAG_DETACHED), fd_items, atfork, atforkvalue, r_pid); if (! err) { i = 0; if (fd_child_list) { while (fd_child_list[i] != ASSUAN_INVALID_FD) { fd_child_list[i] = (assuan_fd_t) fd_items[i].peer_name; i++; } } } free (fd_items); return err; } /* If action is 0, like waitpid. If action is 1, just release the PID? */ static pid_t my_waitpid (assuan_context_t ctx, pid_t pid, int nowait, int *status, int options) { (void)ctx; #ifdef HAVE_W32_SYSTEM (void)nowait; (void)status; (void)options; (void)pid; /* Just a number without a kernel object. */ #else /* We can't just release the PID, a waitpid is mandatory. But NOWAIT in POSIX systems just means the caller already did the waitpid for this child. */ if (! nowait) return _gpgme_ath_waitpid (pid, status, options); #endif return 0; } static int my_socketpair (assuan_context_t ctx, int namespace, int style, int protocol, assuan_fd_t filedes[2]) { #ifdef HAVE_W32_SYSTEM (void)ctx; (void)namespace; (void)style; (void)protocol; (void)filedes; gpg_err_set_errno (ENOSYS); return -1; #else /* FIXME: Debug output missing. */ return __assuan_socketpair (ctx, namespace, style, protocol, filedes); #endif } static int my_socket (assuan_context_t ctx, int namespace, int style, int protocol) { (void)ctx; return _gpgme_io_socket (namespace, style, protocol); } static int my_connect (assuan_context_t ctx, int sock, struct sockaddr *addr, socklen_t length) { (void)ctx; return _gpgme_io_connect (sock, addr, length); } /* Note for Windows: Ignore the incompatible pointer type warning for my_read and my_write. Mingw has been changed to use int for ssize_t on 32 bit systems while we use long. For 64 bit we use int64_t while mingw uses __int64_t. It doe not matter at all because under Windows long and int are both 32 bit even on 64 bit. */ struct assuan_system_hooks _gpgme_assuan_system_hooks = { ASSUAN_SYSTEM_HOOKS_VERSION, my_usleep, my_pipe, my_close, my_read, my_write, my_recvmsg, my_sendmsg, my_spawn, my_waitpid, my_socketpair, my_socket, my_connect }; - diff --git a/src/ath.c b/src/ath.c index 94d5eb1a..6cc84aa5 100644 --- a/src/ath.c +++ b/src/ath.c @@ -1,156 +1,156 @@ /* ath.c - Thread-safeness library. - Copyright (C) 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_SYS_SELECT_H # include #else # ifdef HAVE_SYS_TIME_H # include # endif #endif #ifdef HAVE_SYS_TYPES_H # include #endif #ifndef HAVE_W32_SYSTEM #include #endif #include "gpgme.h" #ifdef _MSC_VER typedef int pid_t; #endif #include "ath.h" #ifdef HAVE_W32_SYSTEM #include uintptr_t ath_self (void) { return (uintptr_t) GetCurrentThreadId (); } #else # ifdef __linux #include uintptr_t ath_self (void) { /* Just to catch users who don't use gpgme-pthread. */ return (uintptr_t) syscall (__NR_gettid); } # else uintptr_t ath_self (void) { return (uintptr_t) getpid (); } # endif #endif gpgme_ssize_t ath_read (int fd, void *buf, size_t nbytes) { return read (fd, buf, nbytes); } gpgme_ssize_t ath_write (int fd, const void *buf, size_t nbytes) { return write (fd, buf, nbytes); } gpgme_ssize_t ath_select (int nfd, fd_set *rset, fd_set *wset, fd_set *eset, struct timeval *timeout) { #ifdef HAVE_W32_SYSTEM return -1; /* Not supported. */ #else return select (nfd, rset, wset, eset, timeout); #endif } gpgme_ssize_t ath_waitpid (pid_t pid, int *status, int options) { #ifdef HAVE_W32_SYSTEM return -1; /* Not supported. */ #else return waitpid (pid, status, options); #endif } int ath_accept (int s, struct sockaddr *addr, socklen_t *length_ptr) { #ifdef HAVE_W32_SYSTEM return -1; /* Not supported. */ #else return accept (s, addr, length_ptr); #endif } int ath_connect (int s, const struct sockaddr *addr, socklen_t length) { #ifdef HAVE_W32_SYSTEM return -1; /* Not supported. */ #else return connect (s, addr, length); #endif } int ath_sendmsg (int s, const struct msghdr *msg, int flags) { #ifdef HAVE_W32_SYSTEM return -1; /* Not supported. */ #else return sendmsg (s, msg, flags); #endif } int ath_recvmsg (int s, struct msghdr *msg, int flags) { #ifdef HAVE_W32_SYSTEM return -1; /* Not supported. */ #else return recvmsg (s, msg, flags); #endif } diff --git a/src/b64dec.c b/src/b64dec.c index c824786c..1b845ee4 100644 --- a/src/b64dec.c +++ b/src/b64dec.c @@ -1,251 +1,252 @@ /* b64dec.c - Simple Base64 decoder. * Copyright (C) 2008, 2011 Free Software Foundation, Inc. * Copyright (C) 2008, 2011, 2016 g10 Code GmbH * * This file is part of GnuPG. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This file 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 Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, see . + * along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #include #include #include #include #include #include "gpgme.h" #include "util.h" /* The reverse base-64 list used for base-64 decoding. */ static unsigned char const asctobin[128] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x3e, 0xff, 0xff, 0xff, 0x3f, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b, 0x3c, 0x3d, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f, 0x30, 0x31, 0x32, 0x33, 0xff, 0xff, 0xff, 0xff, 0xff }; enum decoder_states { s_init, s_idle, s_lfseen, s_beginseen, s_waitheader, s_waitblank, s_begin, s_b64_0, s_b64_1, s_b64_2, s_b64_3, s_waitendtitle, s_waitend }; /* Initialize the context for the base64 decoder. If TITLE is NULL a plain base64 decoding is done. If it is the empty string the decoder will skip everything until a "-----BEGIN " line has been seen, decoding ends at a "----END " line. */ gpg_error_t _gpgme_b64dec_start (struct b64state *state, const char *title) { memset (state, 0, sizeof *state); if (title) { state->title = strdup (title); if (!state->title) state->lasterr = gpg_error_from_syserror (); else state->idx = s_init; } else state->idx = s_b64_0; return state->lasterr; } /* Do in-place decoding of base-64 data of LENGTH in BUFFER. Stores the new length of the buffer at R_NBYTES. */ gpg_error_t _gpgme_b64dec_proc (struct b64state *state, void *buffer, size_t length, size_t *r_nbytes) { enum decoder_states ds = state->idx; unsigned char val = state->radbuf[0]; int pos = state->quad_count; char *d, *s; if (state->lasterr) return state->lasterr; if (state->stop_seen) { *r_nbytes = 0; state->lasterr = gpg_error (GPG_ERR_EOF); free (state->title); state->title = NULL; return state->lasterr; } for (s=d=buffer; length && !state->stop_seen; length--, s++) { again: switch (ds) { case s_idle: if (*s == '\n') { ds = s_lfseen; pos = 0; } break; case s_init: ds = s_lfseen; case s_lfseen: if (*s != "-----BEGIN "[pos]) { ds = s_idle; goto again; } else if (pos == 10) { pos = 0; ds = s_beginseen; } else pos++; break; case s_beginseen: if (*s != "PGP "[pos]) ds = s_begin; /* Not a PGP armor. */ else if (pos == 3) ds = s_waitheader; else pos++; break; case s_waitheader: if (*s == '\n') ds = s_waitblank; break; case s_waitblank: if (*s == '\n') ds = s_b64_0; /* blank line found. */ else if (*s == ' ' || *s == '\r' || *s == '\t') ; /* Ignore spaces. */ else { /* Armor header line. Note that we don't care that our * FSM accepts a header prefixed with spaces. */ ds = s_waitheader; /* Wait for next header. */ } break; case s_begin: if (*s == '\n') ds = s_b64_0; break; case s_b64_0: case s_b64_1: case s_b64_2: case s_b64_3: { int c; if (*s == '-' && state->title) { /* Not a valid Base64 character: assume end header. */ ds = s_waitend; } else if (*s == '=') { /* Pad character: stop */ if (ds == s_b64_1) *d++ = val; ds = state->title? s_waitendtitle : s_waitend; } else if (*s == '\n' || *s == ' ' || *s == '\r' || *s == '\t') ; /* Skip white spaces. */ else if ( (*s & 0x80) || (c = asctobin[*(unsigned char *)s]) == 255) { /* Skip invalid encodings. */ state->invalid_encoding = 1; } else if (ds == s_b64_0) { val = c << 2; ds = s_b64_1; } else if (ds == s_b64_1) { val |= (c>>4)&3; *d++ = val; val = (c<<4)&0xf0; ds = s_b64_2; } else if (ds == s_b64_2) { val |= (c>>2)&15; *d++ = val; val = (c<<6)&0xc0; ds = s_b64_3; } else { val |= c&0x3f; *d++ = val; ds = s_b64_0; } } break; case s_waitendtitle: if (*s == '-') ds = s_waitend; break; case s_waitend: if ( *s == '\n') state->stop_seen = 1; break; default: assert (!"invalid state"); } } state->idx = ds; state->radbuf[0] = val; state->quad_count = pos; *r_nbytes = (d -(char*) buffer); return 0; } /* This function needs to be called before releasing the decoder state. It may return an error code in case an encoding error has been found during decoding. */ gpg_error_t _gpgme_b64dec_finish (struct b64state *state) { if (state->lasterr) return state->lasterr; free (state->title); state->title = NULL; return state->invalid_encoding? gpg_error(GPG_ERR_BAD_DATA): 0; } diff --git a/src/conversion.c b/src/conversion.c index 4bfd3d3e..1d28096d 100644 --- a/src/conversion.c +++ b/src/conversion.c @@ -1,623 +1,623 @@ /* conversion.c - String conversion helper functions. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #ifdef HAVE_SYS_TYPES_H /* Solaris 8 needs sys/types.h before time.h. */ # include #endif #include #include #include #include "gpgme.h" #include "util.h" #include "debug.h" #define atoi_1(p) (*(p) - '0' ) #define atoi_2(p) ((atoi_1(p) * 10) + atoi_1((p)+1)) #define atoi_4(p) ((atoi_2(p) * 100) + atoi_2((p)+2)) static char * do_strconcat (const char *s1, va_list arg_ptr) { const char *argv[16]; size_t argc; size_t needed; char *buffer, *p; argc = 0; argv[argc++] = s1; needed = strlen (s1); while (((argv[argc] = va_arg (arg_ptr, const char *)))) { needed += strlen (argv[argc]); if (argc >= DIM (argv)-1) { gpg_err_set_errno (EINVAL); return NULL; } argc++; } needed++; buffer = malloc (needed); if (buffer) { for (p = buffer, argc=0; argv[argc]; argc++) p = stpcpy (p, argv[argc]); } return buffer; } /* Concatenate the string S1 with all the following strings up to a * NULL. Returns a malloced buffer with the new string or NULL on a malloc error or if too many arguments are given. */ char * _gpgme_strconcat (const char *s1, ...) { va_list arg_ptr; char *result; if (!s1) result = strdup (""); else { va_start (arg_ptr, s1); result = do_strconcat (s1, arg_ptr); va_end (arg_ptr); } return result; } /* Convert two hexadecimal digits from STR to the value they represent. Returns -1 if one of the characters is not a hexadecimal digit. */ int _gpgme_hextobyte (const char *str) { int val = 0; int i; #define NROFHEXDIGITS 2 for (i = 0; i < NROFHEXDIGITS; i++) { if (*str >= '0' && *str <= '9') val += *str - '0'; else if (*str >= 'A' && *str <= 'F') val += 10 + *str - 'A'; else if (*str >= 'a' && *str <= 'f') val += 10 + *str - 'a'; else return -1; if (i < NROFHEXDIGITS - 1) val *= 16; str++; } return val; } /* Decode the C formatted string SRC and store the result in the buffer *DESTP which is LEN bytes long. If LEN is zero, then a large enough buffer is allocated with malloc and *DESTP is set to the result. Currently, LEN is only used to specify if allocation is desired or not, the caller is expected to make sure that *DESTP is large enough if LEN is not zero. */ gpgme_error_t _gpgme_decode_c_string (const char *src, char **destp, size_t len) { char *dest; /* Set up the destination buffer. */ if (len) { if (len < strlen (src) + 1) return gpg_error (GPG_ERR_INTERNAL); dest = *destp; } else { /* The converted string will never be larger than the original string. */ dest = malloc (strlen (src) + 1); if (!dest) return gpg_error_from_syserror (); *destp = dest; } /* Convert the string. */ while (*src) { if (*src != '\\') { *(dest++) = *(src++); continue; } switch (src[1]) { #define DECODE_ONE(match,result) \ case match: \ src += 2; \ *(dest++) = result; \ break; DECODE_ONE ('\'', '\''); DECODE_ONE ('\"', '\"'); DECODE_ONE ('\?', '\?'); DECODE_ONE ('\\', '\\'); DECODE_ONE ('a', '\a'); DECODE_ONE ('b', '\b'); DECODE_ONE ('f', '\f'); DECODE_ONE ('n', '\n'); DECODE_ONE ('r', '\r'); DECODE_ONE ('t', '\t'); DECODE_ONE ('v', '\v'); case 'x': { int val = _gpgme_hextobyte (&src[2]); if (val == -1) { /* Should not happen. */ *(dest++) = *(src++); *(dest++) = *(src++); if (*src) *(dest++) = *(src++); if (*src) *(dest++) = *(src++); } else { if (!val) { /* A binary zero is not representable in a C string. */ *(dest++) = '\\'; *(dest++) = '0'; } else *((unsigned char *) dest++) = val; src += 4; } } break; default: { /* Should not happen. */ *(dest++) = *(src++); *(dest++) = *(src++); } } } *(dest++) = 0; return 0; } /* Decode the percent escaped string SRC and store the result in the buffer *DESTP which is LEN bytes long. If LEN is zero, then a large enough buffer is allocated with malloc and *DESTP is set to the result. Currently, LEN is only used to specify if allocation is desired or not, the caller is expected to make sure that *DESTP is large enough if LEN is not zero. If BINARY is 1, then '\0' characters are allowed in the output. */ gpgme_error_t _gpgme_decode_percent_string (const char *src, char **destp, size_t len, int binary) { char *dest; /* Set up the destination buffer. */ if (len) { if (len < strlen (src) + 1) return gpg_error (GPG_ERR_INTERNAL); dest = *destp; } else { /* The converted string will never be larger than the original string. */ dest = malloc (strlen (src) + 1); if (!dest) return gpg_error_from_syserror (); *destp = dest; } /* Convert the string. */ while (*src) { if (*src != '%') { *(dest++) = *(src++); continue; } else { int val = _gpgme_hextobyte (&src[1]); if (val == -1) { /* Should not happen. */ *(dest++) = *(src++); if (*src) *(dest++) = *(src++); if (*src) *(dest++) = *(src++); } else { if (!val && !binary) { /* A binary zero is not representable in a C string. */ *(dest++) = '\\'; *(dest++) = '0'; } else *((unsigned char *) dest++) = val; src += 3; } } } *(dest++) = 0; return 0; } /* Encode the string SRC with percent escaping and store the result in the buffer *DESTP which is LEN bytes long. If LEN is zero, then a large enough buffer is allocated with malloc and *DESTP is set to the result. Currently, LEN is only used to specify if allocation is desired or not, the caller is expected to make sure that *DESTP is large enough if LEN is not zero. If BINARY is 1, then '\0' characters are allowed in the output. */ gpgme_error_t _gpgme_encode_percent_string (const char *src, char **destp, size_t len) { size_t destlen; char *dest; const char *str; destlen = 0; str = src; /* We percent-escape the + character because the user might need a "percent plus" escaped string (special gpg format). But we percent-escape the space character, which works with and without the special plus format. */ while (*str) { if (*str == '+' || *str == '\"' || *str == '%' || *(const unsigned char *)str <= 0x20) destlen += 3; else destlen++; str++; } /* Terminating nul byte. */ destlen++; /* Set up the destination buffer. */ if (len) { if (len < destlen) return gpg_error (GPG_ERR_INTERNAL); dest = *destp; } else { /* The converted string will never be larger than the original string. */ dest = malloc (destlen); if (!dest) return gpg_error_from_syserror (); *destp = dest; } /* Convert the string. */ while (*src) { if (*src == '+' || *src == '\"' || *src == '%' || *(const unsigned char *)src <= 0x20) { snprintf (dest, 4, "%%%02X", *(unsigned char *)src); dest += 3; } else *(dest++) = *src; src++; } *(dest++) = 0; return 0; } /* Split a string into space delimited fields and remove leading and * trailing spaces from each field. A pointer to each field is * stored in ARRAY. Stop splitting at ARRAYSIZE fields. The function * modifies STRING. The number of parsed fields is returned. */ int _gpgme_split_fields (char *string, char **array, int arraysize) { int n = 0; char *p, *pend; for (p = string; *p == ' '; p++) ; do { if (n == arraysize) break; array[n++] = p; pend = strchr (p, ' '); if (!pend) break; *pend++ = 0; for (p = pend; *p == ' '; p++) ; } while (*p); return n; } /* Convert the field STRING into an unsigned long value. Check for * trailing garbage. */ gpgme_error_t _gpgme_strtoul_field (const char *string, unsigned long *result) { char *endp; gpg_err_set_errno (0); *result = strtoul (string, &endp, 0); if (errno) return gpg_error_from_syserror (); if (endp == string || *endp) return gpg_error (GPG_ERR_INV_VALUE); return 0; } /* Convert STRING into an offset value. Note that this functions only * allows for a base-10 length. This function is similar to atoi() * and thus there is no error checking. */ gpgme_off_t _gpgme_string_to_off (const char *string) { gpgme_off_t value = 0; while (*string == ' ' || *string == '\t') string++; for (; *string >= '0' && *string <= '9'; string++) { value *= 10; value += atoi_1 (string); } return value; } #ifdef HAVE_W32_SYSTEM static time_t _gpgme_timegm (struct tm *tm) { /* This one is thread safe. */ SYSTEMTIME st; FILETIME ft; unsigned long long cnsecs; st.wYear = tm->tm_year + 1900; st.wMonth = tm->tm_mon + 1; st.wDay = tm->tm_mday; st.wHour = tm->tm_hour; st.wMinute = tm->tm_min; st.wSecond = tm->tm_sec; st.wMilliseconds = 0; /* Not available. */ st.wDayOfWeek = 0; /* Ignored. */ /* System time is UTC thus the conversion is pretty easy. */ if (!SystemTimeToFileTime (&st, &ft)) { gpg_err_set_errno (EINVAL); return (time_t)(-1); } cnsecs = (((unsigned long long)ft.dwHighDateTime << 32) | ft.dwLowDateTime); cnsecs -= 116444736000000000ULL; /* The filetime epoch is 1601-01-01. */ return (time_t)(cnsecs / 10000000ULL); } #endif /* Parse the string TIMESTAMP into a time_t. The string may either be seconds since Epoch or in the ISO 8601 format like "20390815T143012". Returns 0 for an empty string or seconds since Epoch. Leading spaces are skipped. If ENDP is not NULL, it will point to the next non-parsed character in TIMESTRING. */ time_t _gpgme_parse_timestamp (const char *timestamp, char **endp) { /* Need to skip leading spaces, because that is what strtoul does but not our ISO 8601 checking code. */ while (*timestamp && *timestamp== ' ') timestamp++; if (!*timestamp) return 0; if (strlen (timestamp) >= 15 && timestamp[8] == 'T') { struct tm buf; int year; year = atoi_4 (timestamp); if (year < 1900) return (time_t)(-1); if (endp) *endp = (char*)(timestamp + 15); /* Fixme: We would better use a configure test to see whether mktime can handle dates beyond 2038. */ if (sizeof (time_t) <= 4 && year >= 2038) return (time_t)2145914603; /* 2037-12-31 23:23:23 */ memset (&buf, 0, sizeof buf); buf.tm_year = year - 1900; buf.tm_mon = atoi_2 (timestamp+4) - 1; buf.tm_mday = atoi_2 (timestamp+6); buf.tm_hour = atoi_2 (timestamp+9); buf.tm_min = atoi_2 (timestamp+11); buf.tm_sec = atoi_2 (timestamp+13); #ifdef HAVE_W32_SYSTEM return _gpgme_timegm (&buf); #else #ifdef HAVE_TIMEGM return timegm (&buf); #else { time_t tim; putenv ("TZ=UTC"); tim = mktime (&buf); #ifdef __GNUC__ #warning fixme: we must somehow reset TZ here. It is not threadsafe anyway. #endif return tim; } #endif /* !HAVE_TIMEGM */ #endif /* !HAVE_W32_SYSTEM */ } else return (time_t)strtoul (timestamp, endp, 10); } /* This function is similar to _gpgme_parse_timestamp but returns an * unsigned long and 0 on error. */ unsigned long _gpgme_parse_timestamp_ul (const char *timestamp) { time_t tim; char *tail; if (!*timestamp) return 0; /* Shortcut empty strings. */ tim = _gpgme_parse_timestamp (timestamp, &tail); if (tim == -1 || timestamp == tail || (*tail && *tail != ' ')) tim = 0; /* No time given or invalid engine. */ return (unsigned long)tim; } /* The GPG backend uses OpenPGP algorithm numbers which we need to map to our algorithm numbers. This function MUST not change ERRNO. */ int _gpgme_map_pk_algo (int algo, gpgme_protocol_t protocol) { if (protocol == GPGME_PROTOCOL_OPENPGP) { switch (algo) { case 1: case 2: case 3: case 16: case 17: break; case 18: algo = GPGME_PK_ECDH; break; case 19: algo = GPGME_PK_ECDSA; break; case 20: break; case 22: algo = GPGME_PK_EDDSA; break; default: algo = 0; break; /* Unknown. */ } } return algo; } /* Return a string with a cipher algorithm. */ const char * _gpgme_cipher_algo_name (int algo, gpgme_protocol_t protocol) { if (protocol == GPGME_PROTOCOL_OPENPGP) { /* The algo is given according to OpenPGP specs. */ switch (algo) { case 1: return "IDEA"; case 2: return "3DES"; case 3: return "CAST5"; case 4: return "BLOWFISH"; case 7: return "AES"; case 8: return "AES192"; case 9: return "AES256"; case 10: return "TWOFISH"; case 11: return "CAMELLIA128"; case 12: return "CAMELLIA192"; case 13: return "CAMELLIA256"; } } return "Unknown"; } /* Return a string with the cipher mode. */ const char * _gpgme_cipher_mode_name (int algo, gpgme_protocol_t protocol) { if (protocol == GPGME_PROTOCOL_OPENPGP) { /* The algo is given according to OpenPGP specs. */ switch (algo) { case 0: return "CFB"; case 1: return "EAX"; case 2: return "OCB"; } } return "Unknown"; } diff --git a/src/data-compat.c b/src/data-compat.c index 1f04c462..a929f0b8 100644 --- a/src/data-compat.c +++ b/src/data-compat.c @@ -1,233 +1,233 @@ /* data-compat.c - Compatibility interfaces for data objects. - Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #ifdef HAVE_SYS_TIME_H # include #endif #ifdef HAVE_SYS_STAT_H # include #endif #include #include "data.h" #include "util.h" #include "debug.h" /* Create a new data buffer filled with LENGTH bytes starting from OFFSET within the file FNAME or stream STREAM (exactly one must be non-zero). */ gpgme_error_t gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname, FILE *stream, gpgme_off_t offset, size_t length) { gpgme_error_t err; char *buf = NULL; int res; TRACE_BEG4 (DEBUG_DATA, "gpgme_data_new_from_filepart", r_dh, "file_name=%s, stream=%p, offset=%lli, length=%u", fname, stream, offset, length); if (stream && fname) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (fname) stream = fopen (fname, "rb"); if (!stream) return TRACE_ERR (gpg_error_from_syserror ()); #ifdef HAVE_FSEEKO res = fseeko (stream, offset, SEEK_SET); #else /* FIXME: Check for overflow, or at least bail at compilation. */ res = fseek (stream, offset, SEEK_SET); #endif if (res) { int saved_err = gpg_error_from_syserror (); if (fname) fclose (stream); return TRACE_ERR (saved_err); } buf = malloc (length); if (!buf) { int saved_err = gpg_error_from_syserror (); if (fname) fclose (stream); return TRACE_ERR (saved_err); } while (fread (buf, length, 1, stream) < 1 && ferror (stream) && errno == EINTR); if (ferror (stream)) { int saved_err = gpg_error_from_syserror (); if (buf) free (buf); if (fname) fclose (stream); return TRACE_ERR (saved_err); } if (fname) fclose (stream); err = gpgme_data_new (r_dh); if (err) { if (buf) free (buf); return err; } (*r_dh)->data.mem.buffer = buf; (*r_dh)->data.mem.size = length; (*r_dh)->data.mem.length = length; return TRACE_SUC1 ("r_dh=%p", *r_dh); } /* Create a new data buffer filled with the content of file FNAME. COPY must be non-zero (delayed reads are not supported yet). */ gpgme_error_t gpgme_data_new_from_file (gpgme_data_t *r_dh, const char *fname, int copy) { gpgme_error_t err; struct stat statbuf; TRACE_BEG3 (DEBUG_DATA, "gpgme_data_new_from_file", r_dh, "file_name=%s, copy=%i (%s)", fname, copy, copy ? "yes" : "no"); if (!fname || !copy) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (stat (fname, &statbuf) < 0) return TRACE_ERR (gpg_error_from_syserror ()); err = gpgme_data_new_from_filepart (r_dh, fname, NULL, 0, statbuf.st_size); return TRACE_ERR (err); } static int gpgme_error_to_errno (gpgme_error_t err) { int res = gpg_err_code_to_errno (gpg_err_code (err)); if (!err) { switch (gpg_err_code (err)) { case GPG_ERR_EOF: res = 0; break; case GPG_ERR_INV_VALUE: res = EINVAL; break; case GPG_ERR_NOT_SUPPORTED: res = ENOSYS; break; default: /* FIXME: Yeah, well. */ res = EINVAL; break; } } TRACE3 (DEBUG_DATA, "gpgme:gpgme_error_to_errno", 0, "mapping %s <%s> to: %s", gpgme_strerror (err), gpgme_strsource (err), strerror (res)); gpg_err_set_errno (res); return res ? -1 : 0; } static gpgme_ssize_t old_user_read (gpgme_data_t dh, void *buffer, size_t size) { gpgme_error_t err; size_t amt; TRACE_BEG2 (DEBUG_DATA, "gpgme:old_user_read", dh, "buffer=%p, size=%u", buffer, size); err = (*dh->data.old_user.cb) (dh->data.old_user.handle, buffer, size, &amt); if (err) return TRACE_SYSRES (gpgme_error_to_errno (err)); return TRACE_SYSRES ((gpgme_ssize_t)amt); } static gpgme_off_t old_user_seek (gpgme_data_t dh, gpgme_off_t offset, int whence) { gpgme_error_t err; TRACE_BEG2 (DEBUG_DATA, "gpgme:old_user_seek", dh, "offset=%llu, whence=%i", offset, whence); if (whence != SEEK_SET || offset) { gpg_err_set_errno (EINVAL); return TRACE_SYSRES (-1); } err = (*dh->data.old_user.cb) (dh->data.old_user.handle, NULL, 0, NULL); if (err) return TRACE_SYSRES (gpgme_error_to_errno (err)); return TRACE_SYSRES (0); } static struct _gpgme_data_cbs old_user_cbs = { old_user_read, NULL, old_user_seek, NULL }; /* Create a new data buffer which retrieves the data from the callback function READ_CB. */ gpgme_error_t gpgme_data_new_with_read_cb (gpgme_data_t *r_dh, int (*read_cb) (void *, char *, size_t, size_t *), void *read_cb_value) { gpgme_error_t err; TRACE_BEG2 (DEBUG_DATA, "gpgme_data_new_with_read_cb", r_dh, "read_cb=%p/%p", read_cb, read_cb_value); err = _gpgme_data_new (r_dh, &old_user_cbs); if (err) return TRACE_ERR (err); (*r_dh)->data.old_user.cb = read_cb; (*r_dh)->data.old_user.handle = read_cb_value; return TRACE_ERR (0); } diff --git a/src/data-fd.c b/src/data-fd.c index dec52af7..b8739f2c 100644 --- a/src/data-fd.c +++ b/src/data-fd.c @@ -1,86 +1,86 @@ /* data-fd.c - A file descriptor based data object. - Copyright (C) 2002, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2002, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_SYS_TYPES_H # include #endif #include "debug.h" #include "data.h" static gpgme_ssize_t fd_read (gpgme_data_t dh, void *buffer, size_t size) { return read (dh->data.fd, buffer, size); } static gpgme_ssize_t fd_write (gpgme_data_t dh, const void *buffer, size_t size) { return write (dh->data.fd, buffer, size); } static gpgme_off_t fd_seek (gpgme_data_t dh, gpgme_off_t offset, int whence) { return lseek (dh->data.fd, offset, whence); } static int fd_get_fd (gpgme_data_t dh) { return (dh->data.fd); } static struct _gpgme_data_cbs fd_cbs = { fd_read, fd_write, fd_seek, NULL, fd_get_fd }; gpgme_error_t gpgme_data_new_from_fd (gpgme_data_t *r_dh, int fd) { gpgme_error_t err; TRACE_BEG1 (DEBUG_DATA, "gpgme_data_new_from_fd", r_dh, "fd=0x%x", fd); err = _gpgme_data_new (r_dh, &fd_cbs); if (err) return TRACE_ERR (err); (*r_dh)->data.fd = fd; return TRACE_SUC1 ("dh=%p", *r_dh); } diff --git a/src/data-identify.c b/src/data-identify.c index 826c427a..eb657861 100644 --- a/src/data-identify.c +++ b/src/data-identify.c @@ -1,516 +1,517 @@ /* data-identify.c - Try to identify the data - Copyright (C) 2013, 2016 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2013, 2016 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H # include #endif #include #include #include "gpgme.h" #include "data.h" #include "util.h" #include "parsetlv.h" /* The size of the sample data we take for detection. */ #define SAMPLE_SIZE 2048 /* OpenPGP packet types. */ enum { PKT_NONE = 0, PKT_PUBKEY_ENC = 1, /* Public key encrypted packet. */ PKT_SIGNATURE = 2, /* Secret key encrypted packet. */ PKT_SYMKEY_ENC = 3, /* Session key packet. */ PKT_ONEPASS_SIG = 4, /* One pass sig packet. */ PKT_SECRET_KEY = 5, /* Secret key. */ PKT_PUBLIC_KEY = 6, /* Public key. */ PKT_SECRET_SUBKEY = 7, /* Secret subkey. */ PKT_COMPRESSED = 8, /* Compressed data packet. */ PKT_ENCRYPTED = 9, /* Conventional encrypted data. */ PKT_MARKER = 10, /* Marker packet. */ PKT_PLAINTEXT = 11, /* Literal data packet. */ PKT_RING_TRUST = 12, /* Keyring trust packet. */ PKT_USER_ID = 13, /* User id packet. */ PKT_PUBLIC_SUBKEY = 14, /* Public subkey. */ PKT_OLD_COMMENT = 16, /* Comment packet from an OpenPGP draft. */ PKT_ATTRIBUTE = 17, /* PGP's attribute packet. */ PKT_ENCRYPTED_MDC = 18, /* Integrity protected encrypted data. */ PKT_MDC = 19, /* Manipulation detection code packet. */ }; static inline unsigned long buf32_to_ulong (const void *buffer) { const unsigned char *p = buffer; return (((unsigned long)p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3]); } /* Parse the next openpgp packet. This function assumes a valid * OpenPGP packet at the address pointed to by BUFPTR which has a * maximum length as stored at BUFLEN. Return the header information * of that packet and advance the pointer stored at BUFPTR to the next * packet; also adjust the length stored at BUFLEN to match the * remaining bytes. If there are no more packets, store NULL at * BUFPTR. Return an non-zero error code on failure or the following * data on success: * * R_PKTTYPE = The packet type. * R_NTOTAL = The total number of bytes of this packet * * If GPG_ERR_TRUNCATED is returned, a packet type is anyway stored at * R_PKTTYPE but R_NOTAL won't have a usable value, */ static gpg_error_t next_openpgp_packet (unsigned char const **bufptr, size_t *buflen, int *r_pkttype, size_t *r_ntotal) { const unsigned char *buf = *bufptr; size_t len = *buflen; int c, ctb, pkttype; unsigned long pktlen; if (!len) return gpg_error (GPG_ERR_NO_DATA); /* First some blacklisting. */ if (len >= 4 && !memcmp (buf, "\x89PNG", 4)) return gpg_error (GPG_ERR_INV_PACKET); /* This is a PNG file. */ /* Start parsing. */ ctb = *buf++; len--; if ( !(ctb & 0x80) ) return gpg_error (GPG_ERR_INV_PACKET); /* Invalid CTB. */ if ((ctb & 0x40)) /* New style (OpenPGP) CTB. */ { pkttype = (ctb & 0x3f); if (!len) return gpg_error (GPG_ERR_INV_PACKET); /* No 1st length byte. */ c = *buf++; len--; if ( c < 192 ) pktlen = c; else if ( c < 224 ) { pktlen = (c - 192) * 256; if (!len) return gpg_error (GPG_ERR_INV_PACKET); /* No 2nd length byte. */ c = *buf++; len--; pktlen += c + 192; } else if (c == 255) { if (len < 4) return gpg_error (GPG_ERR_INV_PACKET); /* No length bytes. */ pktlen = buf32_to_ulong (buf); buf += 4; len -= 4; } else /* Partial length encoding. */ { pktlen = 0; } } else /* Old style CTB. */ { int lenbytes; pktlen = 0; pkttype = (ctb>>2)&0xf; lenbytes = ((ctb&3)==3)? 0 : (1<<(ctb & 3)); if (len < lenbytes) return gpg_error (GPG_ERR_INV_PACKET); /* Not enough length bytes. */ for (; lenbytes; lenbytes--) { pktlen <<= 8; pktlen |= *buf++; len--; } } /* Do some basic sanity check. */ switch (pkttype) { case PKT_PUBKEY_ENC: case PKT_SIGNATURE: case PKT_SYMKEY_ENC: case PKT_ONEPASS_SIG: case PKT_SECRET_KEY: case PKT_PUBLIC_KEY: case PKT_SECRET_SUBKEY: case PKT_COMPRESSED: case PKT_ENCRYPTED: case PKT_MARKER: case PKT_PLAINTEXT: case PKT_RING_TRUST: case PKT_USER_ID: case PKT_PUBLIC_SUBKEY: case PKT_OLD_COMMENT: case PKT_ATTRIBUTE: case PKT_ENCRYPTED_MDC: case PKT_MDC: break; /* Okay these are allowed packets. */ default: return gpg_error (GPG_ERR_UNEXPECTED); } if (pktlen > len) { /* Packet length header too long. This is possible because we * may have only a truncated image. */ *r_pkttype = pkttype; *r_ntotal = 0; *bufptr = NULL; return gpg_error (GPG_ERR_TRUNCATED); } *r_pkttype = pkttype; *r_ntotal = (buf - *bufptr) + pktlen; *bufptr = buf + pktlen; *buflen = len - pktlen; if (!*buflen) *bufptr = NULL; return 0; } /* Detection of PGP binary data. This function parses an OpenPGP * message. This parser is robust enough to work on a truncated * version. Returns a GPGME_DATA_TYPE_. */ static gpgme_data_type_t pgp_binary_detection (const void *image_arg, size_t imagelen) { gpg_error_t err = 0; const unsigned char *image = image_arg; size_t n; int pkttype; int anypacket = 0; int allsignatures = 0; while (!err && image) { err = next_openpgp_packet (&image, &imagelen, &pkttype, &n); if (gpg_err_code (err) == GPG_ERR_TRUNCATED) ; else if (err) break; /* Skip all leading marker packets. */ if (!anypacket && pkttype == PKT_MARKER) continue; if (pkttype == PKT_SIGNATURE) { if (!anypacket) allsignatures = 1; } else allsignatures = 0; switch (pkttype) { case PKT_SIGNATURE: break; /* We decide later. */ case PKT_PLAINTEXT: /* Old style signature format: {sig}+,plaintext */ if (allsignatures) return GPGME_DATA_TYPE_PGP_SIGNED; break; case PKT_ONEPASS_SIG: return GPGME_DATA_TYPE_PGP_SIGNED; case PKT_SECRET_KEY: case PKT_PUBLIC_KEY: return GPGME_DATA_TYPE_PGP_KEY; case PKT_SECRET_SUBKEY: case PKT_PUBLIC_SUBKEY: return GPGME_DATA_TYPE_PGP_OTHER; case PKT_PUBKEY_ENC: case PKT_SYMKEY_ENC: return GPGME_DATA_TYPE_PGP_ENCRYPTED; case PKT_COMPRESSED: /* If this is the first packet we assume that that a signed * packet follows. We do not want to uncompress it here due * to the need of a lot of code and the potential DoS. */ if (!anypacket) return GPGME_DATA_TYPE_PGP_SIGNED; return GPGME_DATA_TYPE_PGP_OTHER; default: return GPGME_DATA_TYPE_PGP_OTHER; } anypacket = 1; } if (allsignatures) return GPGME_DATA_TYPE_PGP_SIGNATURE; return GPGME_DATA_TYPE_UNKNOWN; } /* This is probably an armored "PGP MESSAGE" which can encode * different PGP data types. STRING is modified after a call to this * function. */ static gpgme_data_type_t inspect_pgp_message (char *string) { struct b64state state; size_t nbytes; if (_gpgme_b64dec_start (&state, "")) return GPGME_DATA_TYPE_INVALID; /* oops */ if (_gpgme_b64dec_proc (&state, string, strlen (string), &nbytes)) { _gpgme_b64dec_finish (&state); return GPGME_DATA_TYPE_UNKNOWN; /* bad encoding etc. */ } _gpgme_b64dec_finish (&state); string[nbytes] = 0; /* Better append a Nul. */ return pgp_binary_detection (string, nbytes); } /* Note that DATA may be binary but a final nul is required so that string operations will find a terminator. Returns: GPGME_DATA_TYPE_xxxx */ static gpgme_data_type_t basic_detection (char *data, size_t datalen) { tlvinfo_t ti; const char *s; size_t n; int maybe_p12 = 0; if (datalen < 24) /* Object is probably too short for detection. */ return GPGME_DATA_TYPE_UNKNOWN; /* This is a common example of a CMS object - it is obvious that we only need to read a few bytes to get to the OID: 30 82 0B 59 06 09 2A 86 48 86 F7 0D 01 07 02 A0 82 0B 4A 30 82 0B 46 02 ----------- ++++++++++++++++++++++++++++++++ SEQUENCE OID (signedData) (2 byte len) A PKCS#12 message is: 30 82 08 59 02 01 03 30 82 08 1F 06 09 2A 86 48 86 F7 0D 01 07 01 A0 82 ----------- ++++++++ ----------- ++++++++++++++++++++++++++++++++ SEQUENCE INTEGER SEQUENCE OID (data) A X.509 certificate is: 30 82 05 B8 30 82 04 A0 A0 03 02 01 02 02 07 15 46 A0 BF 30 07 39 30 0D ----------- +++++++++++ ----- ++++++++ -------------------------- SEQUENCE SEQUENCE [0] INTEGER INTEGER SEQU (tbs) (version) (s/n) (Algo) Thus we need to read at least 22 bytes, we add 2 bytes to cope with length headers stored with 4 bytes. */ s = data; n = datalen; if (parse_tlv (&s, &n, &ti)) goto try_pgp; /* Not properly BER encoded. */ if (!(ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_SEQUENCE && ti.is_cons)) goto try_pgp; /* A CMS object always starts with a sequence. */ if (parse_tlv (&s, &n, &ti)) goto try_pgp; /* Not properly BER encoded. */ if (ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_SEQUENCE && ti.is_cons && n >= ti.length) { if (parse_tlv (&s, &n, &ti)) goto try_pgp; if (!(ti.cls == ASN1_CLASS_CONTEXT && ti.tag == 0 && ti.is_cons && ti.length == 3 && n >= ti.length)) goto try_pgp; if (parse_tlv (&s, &n, &ti)) goto try_pgp; if (!(ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_INTEGER && !ti.is_cons && ti.length == 1 && n && (*s == 1 || *s == 2))) goto try_pgp; s++; n--; if (!(ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_INTEGER && !ti.is_cons)) goto try_pgp; /* Because the now following S/N may be larger than the sample data we have, we stop parsing here and don't check for the algorithm ID. */ return GPGME_DATA_TYPE_X509_CERT; } if (ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_INTEGER && !ti.is_cons && ti.length == 1 && n && *s == 3) { maybe_p12 = 1; s++; n--; if (parse_tlv (&s, &n, &ti)) goto try_pgp; if (!(ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_SEQUENCE && ti.is_cons)) goto try_pgp; if (parse_tlv (&s, &n, &ti)) goto try_pgp; } if (ti.cls == ASN1_CLASS_UNIVERSAL && ti.tag == ASN1_TAG_OBJECT_ID && !ti.is_cons && ti.length && n >= ti.length) { if (ti.length == 9) { if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x07\x01", 9)) { /* Data. */ return (maybe_p12 ? GPGME_DATA_TYPE_PKCS12 /* */ : GPGME_DATA_TYPE_CMS_OTHER); } if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x07\x02", 9)) { /* Signed Data. */ return (maybe_p12 ? GPGME_DATA_TYPE_PKCS12 /* */ : GPGME_DATA_TYPE_CMS_SIGNED); } if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x07\x03", 9)) return GPGME_DATA_TYPE_CMS_ENCRYPTED; /* Enveloped Data. */ if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x07\x05", 9)) return GPGME_DATA_TYPE_CMS_OTHER; /* Digested Data. */ if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x07\x06", 9)) return GPGME_DATA_TYPE_CMS_OTHER; /* Encrypted Data. */ } else if (ti.length == 11) { if (!memcmp (s, "\x2A\x86\x48\x86\xF7\x0D\x01\x09\x10\x01\x02", 11)) return GPGME_DATA_TYPE_CMS_OTHER; /* Auth Data. */ } } try_pgp: /* Check whether this might be a non-armored PGP message. We need to do this before checking for armor lines, so that we don't get fooled by armored messages inside a signed binary PGP message. */ if ((data[0] & 0x80)) { /* That might be a binary PGP message. At least it is not plain ASCII. Of course this might be certain lead-in text of armored CMS messages. However, I am not sure whether this is at all defined and in any case it is uncommon. Thus we don't do any further plausibility checks but stupidly assume no CMS armored data will follow. */ return pgp_binary_detection (data, datalen); } /* Now check whether there are armor lines. */ for (s = data; s && *s; s = (*s=='\n')?(s+1):((s=strchr (s,'\n'))?(s+1):s)) { if (!strncmp (s, "-----BEGIN ", 11)) { if (!strncmp (s+11, "SIGNED ", 7)) return GPGME_DATA_TYPE_CMS_SIGNED; if (!strncmp (s+11, "ENCRYPTED ", 10)) return GPGME_DATA_TYPE_CMS_ENCRYPTED; if (!strncmp (s+11, "PGP ", 4)) { if (!strncmp (s+15, "SIGNATURE", 9)) return GPGME_DATA_TYPE_PGP_SIGNATURE; if (!strncmp (s+15, "SIGNED MESSAGE", 14)) return GPGME_DATA_TYPE_PGP_SIGNED; if (!strncmp (s+15, "PUBLIC KEY BLOCK", 16)) return GPGME_DATA_TYPE_PGP_KEY; if (!strncmp (s+15, "PRIVATE KEY BLOCK", 17)) return GPGME_DATA_TYPE_PGP_KEY; if (!strncmp (s+15, "SECRET KEY BLOCK", 16)) return GPGME_DATA_TYPE_PGP_KEY; if (!strncmp (s+15, "ARMORED FILE", 12)) return GPGME_DATA_TYPE_UNKNOWN; return inspect_pgp_message (data); } if (!strncmp (s+11, "CERTIFICATE", 11)) return GPGME_DATA_TYPE_X509_CERT; if (!strncmp (s+11, "PKCS12", 6)) return GPGME_DATA_TYPE_PKCS12; return GPGME_DATA_TYPE_CMS_OTHER; /* Not PGP, thus we assume CMS. */ } } return GPGME_DATA_TYPE_UNKNOWN; } /* Try to detect the type of the data. Note that this function works only on seekable data objects. The function tries to reset the file pointer but there is no guarantee that it will work. FIXME: We may want to add internal buffering so that this function can be implemented for almost all kind of data objects. */ gpgme_data_type_t gpgme_data_identify (gpgme_data_t dh, int reserved) { gpgme_data_type_t result; char *sample; int n; gpgme_off_t off; (void)reserved; /* Check whether we can seek the data object. */ off = gpgme_data_seek (dh, 0, SEEK_CUR); if (off == (gpgme_off_t)(-1)) return GPGME_DATA_TYPE_INVALID; /* Allocate a buffer and read the data. */ sample = malloc (SAMPLE_SIZE); if (!sample) return GPGME_DATA_TYPE_INVALID; /* Ooops. */ n = gpgme_data_read (dh, sample, SAMPLE_SIZE - 1); if (n < 0) { free (sample); return GPGME_DATA_TYPE_INVALID; /* Ooops. */ } sample[n] = 0; /* (Required for our string functions.) */ result = basic_detection (sample, n); free (sample); gpgme_data_seek (dh, off, SEEK_SET); return result; } diff --git a/src/data-mem.c b/src/data-mem.c index 7569f7df..2d19a99c 100644 --- a/src/data-mem.c +++ b/src/data-mem.c @@ -1,305 +1,305 @@ /* data-mem.c - A memory based data object. - Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #ifdef HAVE_UNISTD_H # include #endif #include #include #include "data.h" #include "util.h" #include "debug.h" static gpgme_ssize_t mem_read (gpgme_data_t dh, void *buffer, size_t size) { size_t amt = dh->data.mem.length - dh->data.mem.offset; const char *src; if (!amt) return 0; if (size < amt) amt = size; src = dh->data.mem.buffer ? dh->data.mem.buffer : dh->data.mem.orig_buffer; memcpy (buffer, src + dh->data.mem.offset, amt); dh->data.mem.offset += amt; return amt; } static gpgme_ssize_t mem_write (gpgme_data_t dh, const void *buffer, size_t size) { size_t unused; if (!dh->data.mem.buffer && dh->data.mem.orig_buffer) { size_t new_size = dh->data.mem.size; char *new_buffer; if (new_size < dh->data.mem.offset + size) new_size = dh->data.mem.offset + size; new_buffer = malloc (new_size); if (!new_buffer) return -1; memcpy (new_buffer, dh->data.mem.orig_buffer, dh->data.mem.length); dh->data.mem.buffer = new_buffer; dh->data.mem.size = new_size; } unused = dh->data.mem.size - dh->data.mem.offset; if (unused < size) { /* Allocate a large enough buffer with exponential backoff. */ #define INITIAL_ALLOC 512 size_t new_size = dh->data.mem.size ? (2 * dh->data.mem.size) : INITIAL_ALLOC; char *new_buffer; if (new_size < dh->data.mem.offset + size) new_size = dh->data.mem.offset + size; new_buffer = realloc (dh->data.mem.buffer, new_size); if (!new_buffer && new_size > dh->data.mem.offset + size) { /* Maybe we were too greedy, try again. */ new_size = dh->data.mem.offset + size; new_buffer = realloc (dh->data.mem.buffer, new_size); } if (!new_buffer) return -1; dh->data.mem.buffer = new_buffer; dh->data.mem.size = new_size; } memcpy (dh->data.mem.buffer + dh->data.mem.offset, buffer, size); dh->data.mem.offset += size; if (dh->data.mem.length < dh->data.mem.offset) dh->data.mem.length = dh->data.mem.offset; return size; } static gpgme_off_t mem_seek (gpgme_data_t dh, gpgme_off_t offset, int whence) { switch (whence) { case SEEK_SET: if (offset < 0 || offset > dh->data.mem.length) { gpg_err_set_errno (EINVAL); return -1; } dh->data.mem.offset = offset; break; case SEEK_CUR: if ((offset > 0 && dh->data.mem.length - dh->data.mem.offset < offset) || (offset < 0 && dh->data.mem.offset < -offset)) { gpg_err_set_errno (EINVAL); return -1; } dh->data.mem.offset += offset; break; case SEEK_END: if (offset > 0 || -offset > dh->data.mem.length) { gpg_err_set_errno (EINVAL); return -1; } dh->data.mem.offset = dh->data.mem.length + offset; break; default: gpg_err_set_errno (EINVAL); return -1; } return dh->data.mem.offset; } static void mem_release (gpgme_data_t dh) { if (dh->data.mem.buffer) free (dh->data.mem.buffer); } static struct _gpgme_data_cbs mem_cbs = { mem_read, mem_write, mem_seek, mem_release, NULL }; /* Create a new data buffer and return it in R_DH. */ gpgme_error_t gpgme_data_new (gpgme_data_t *r_dh) { gpgme_error_t err; TRACE_BEG (DEBUG_DATA, "gpgme_data_new", r_dh); err = _gpgme_data_new (r_dh, &mem_cbs); if (err) return TRACE_ERR (err); return TRACE_SUC1 ("dh=%p", *r_dh); } /* Create a new data buffer filled with SIZE bytes starting from BUFFER. If COPY is zero, copying is delayed until necessary, and the data is taken from the original location when needed. */ gpgme_error_t gpgme_data_new_from_mem (gpgme_data_t *r_dh, const char *buffer, size_t size, int copy) { gpgme_error_t err; TRACE_BEG4 (DEBUG_DATA, "gpgme_data_new_from_mem", r_dh, "buffer=%p, size=%u, copy=%i (%s)", buffer, size, copy, copy ? "yes" : "no"); err = _gpgme_data_new (r_dh, &mem_cbs); if (err) return TRACE_ERR (err); if (copy) { char *bufcpy = malloc (size); if (!bufcpy) { int saved_err = gpg_error_from_syserror (); _gpgme_data_release (*r_dh); return TRACE_ERR (saved_err); } memcpy (bufcpy, buffer, size); (*r_dh)->data.mem.buffer = bufcpy; } else (*r_dh)->data.mem.orig_buffer = buffer; (*r_dh)->data.mem.size = size; (*r_dh)->data.mem.length = size; return TRACE_SUC1 ("dh=%p", *r_dh); } /* Destroy the data buffer DH and return a pointer to its content. The memory has be to released with gpgme_free() by the user. It's size is returned in R_LEN. */ char * gpgme_data_release_and_get_mem (gpgme_data_t dh, size_t *r_len) { gpg_error_t err; char *str = NULL; size_t len; int blankout; TRACE_BEG1 (DEBUG_DATA, "gpgme_data_release_and_get_mem", dh, "r_len=%p", r_len); if (!dh || dh->cbs != &mem_cbs) { gpgme_data_release (dh); TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); return NULL; } err = _gpgme_data_get_prop (dh, 0, DATA_PROP_BLANKOUT, &blankout); if (err) { gpgme_data_release (dh); TRACE_ERR (err); return NULL; } str = dh->data.mem.buffer; len = dh->data.mem.length; if (blankout && len) len = 1; if (!str && dh->data.mem.orig_buffer) { str = malloc (len); if (!str) { int saved_err = gpg_error_from_syserror (); gpgme_data_release (dh); TRACE_ERR (saved_err); return NULL; } if (blankout) memset (str, 0, len); else memcpy (str, dh->data.mem.orig_buffer, len); } else { if (blankout && len) *str = 0; /* Prevent mem_release from releasing the buffer memory. We * must not fail from this point. */ dh->data.mem.buffer = NULL; } if (r_len) *r_len = len; gpgme_data_release (dh); if (r_len) { TRACE_SUC2 ("buffer=%p, len=%u", str, *r_len); } else { TRACE_SUC1 ("buffer=%p", str); } return str; } /* Release the memory returned by gpgme_data_release_and_get_mem() and some other functions. */ void gpgme_free (void *buffer) { TRACE (DEBUG_DATA, "gpgme_free", buffer); if (buffer) free (buffer); } diff --git a/src/data-stream.c b/src/data-stream.c index f358a0e0..8961d079 100644 --- a/src/data-stream.c +++ b/src/data-stream.c @@ -1,108 +1,108 @@ /* data-stream.c - A stream based data object. - Copyright (C) 2002, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2002, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #ifdef HAVE_SYS_TYPES_H # include #endif #include "debug.h" #include "data.h" static gpgme_ssize_t stream_read (gpgme_data_t dh, void *buffer, size_t size) { size_t amt = fread (buffer, 1, size, dh->data.stream); if (amt > 0) return amt; return ferror (dh->data.stream) ? -1 : 0; } static gpgme_ssize_t stream_write (gpgme_data_t dh, const void *buffer, size_t size) { size_t amt = fwrite (buffer, 1, size, dh->data.stream); if (amt > 0) return amt; return ferror (dh->data.stream) ? -1 : 0; } static gpgme_off_t stream_seek (gpgme_data_t dh, gpgme_off_t offset, int whence) { int err; #ifdef HAVE_FSEEKO err = fseeko (dh->data.stream, offset, whence); #else /* FIXME: Check for overflow, or at least bail at compilation. */ err = fseek (dh->data.stream, offset, whence); #endif if (err) return -1; #ifdef HAVE_FSEEKO return ftello (dh->data.stream); #else return ftell (dh->data.stream); #endif } static int stream_get_fd (gpgme_data_t dh) { fflush (dh->data.stream); return fileno (dh->data.stream); } static struct _gpgme_data_cbs stream_cbs = { stream_read, stream_write, stream_seek, NULL, stream_get_fd }; gpgme_error_t gpgme_data_new_from_stream (gpgme_data_t *r_dh, FILE *stream) { gpgme_error_t err; TRACE_BEG1 (DEBUG_DATA, "gpgme_data_new_from_stream", r_dh, "stream=%p", stream); err = _gpgme_data_new (r_dh, &stream_cbs); if (err) return TRACE_ERR (err); (*r_dh)->data.stream = stream; return TRACE_SUC1 ("dh=%p", *r_dh); } diff --git a/src/data-user.c b/src/data-user.c index 816ad7db..d1d85250 100644 --- a/src/data-user.c +++ b/src/data-user.c @@ -1,104 +1,104 @@ /* data-user.c - A user callback based data object. - Copyright (C) 2002, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2002, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #ifdef HAVE_SYS_TYPES_H # include #endif #include #include "debug.h" #include "data.h" static gpgme_ssize_t user_read (gpgme_data_t dh, void *buffer, size_t size) { if (!dh->data.user.cbs->read) { gpg_err_set_errno (EBADF); return -1; } return (*dh->data.user.cbs->read) (dh->data.user.handle, buffer, size); } static gpgme_ssize_t user_write (gpgme_data_t dh, const void *buffer, size_t size) { if (!dh->data.user.cbs->write) { gpg_err_set_errno (EBADF); return -1; } return (*dh->data.user.cbs->write) (dh->data.user.handle, buffer, size); } static gpgme_off_t user_seek (gpgme_data_t dh, gpgme_off_t offset, int whence) { if (!dh->data.user.cbs->seek) { gpg_err_set_errno (EBADF); return -1; } return (*dh->data.user.cbs->seek) (dh->data.user.handle, offset, whence); } static void user_release (gpgme_data_t dh) { if (dh->data.user.cbs->release) (*dh->data.user.cbs->release) (dh->data.user.handle); } static struct _gpgme_data_cbs user_cbs = { user_read, user_write, user_seek, user_release, NULL }; gpgme_error_t gpgme_data_new_from_cbs (gpgme_data_t *r_dh, gpgme_data_cbs_t cbs, void *handle) { gpgme_error_t err; TRACE_BEG1 (DEBUG_DATA, "gpgme_data_new_from_cbs", r_dh, "handle=%p", handle); err = _gpgme_data_new (r_dh, &user_cbs); if (err) return TRACE_ERR (err); (*r_dh)->data.user.cbs = cbs; (*r_dh)->data.user.handle = handle; return TRACE_SUC1 ("dh=%p", *r_dh); } diff --git a/src/data.c b/src/data.c index ba32477a..a786c7c7 100644 --- a/src/data.c +++ b/src/data.c @@ -1,663 +1,663 @@ /* data.c - An abstraction for data objects. - Copyright (C) 2002, 2003, 2004, 2005, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2002, 2003, 2004, 2005, 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #ifdef HAVE_UNISTD_H # include #endif #include #include #include #include "gpgme.h" #include "data.h" #include "util.h" #include "ops.h" #include "priv-io.h" #include "debug.h" /* The property table which has an entry for each active data object. * The data object itself uses an index into this table and the table * has a pointer back to the data object. All access to that table is * controlled by the property_table_lock. * * We use a separate table instead of linking all data objects * together for faster locating properties of the data object using * the data objects serial number. We use 64 bit for the serial * number which is good enough to create a new data object every * nanosecond for more than 500 years. Thus no wrap around will ever * happen. */ struct property_s { gpgme_data_t dh; /* The data objcet or NULL if the slot is not used. */ uint64_t dserial; /* The serial number of the data object. */ struct { unsigned int blankout : 1; /* Void the held data. */ } flags; }; typedef struct property_s *property_t; static property_t property_table; static unsigned int property_table_size; DEFINE_STATIC_LOCK (property_table_lock); #define PROPERTY_TABLE_ALLOCATION_CHUNK 32 /* Insert the newly created data object DH into the property table and * store the index of it at R_IDX. An error code is returned on error * and the table is not changed. */ static gpg_error_t insert_into_property_table (gpgme_data_t dh, unsigned int *r_idx) { static uint64_t last_dserial; gpg_error_t err; unsigned int idx; LOCK (property_table_lock); if (!property_table) { property_table_size = PROPERTY_TABLE_ALLOCATION_CHUNK; property_table = calloc (property_table_size, sizeof *property_table); if (!property_table) { err = gpg_error_from_syserror (); goto leave; } } /* Find an empty slot. */ for (idx = 0; idx < property_table_size; idx++) if (!property_table[idx].dh) break; if (!(idx < property_table_size)) { /* No empty slot found. Enlarge the table. */ property_t newtbl; unsigned int newsize; newsize = property_table_size + PROPERTY_TABLE_ALLOCATION_CHUNK;; if ((newsize * sizeof *property_table) < (property_table_size * sizeof *property_table)) { err = gpg_error (GPG_ERR_ENOMEM); goto leave; } newtbl = realloc (property_table, newsize * sizeof *property_table); if (!newtbl) { err = gpg_error_from_syserror (); goto leave; } property_table = newtbl; for (idx = property_table_size; idx < newsize; idx++) property_table[idx].dh = NULL; idx = property_table_size; property_table_size = newsize; } /* Slot found. */ property_table[idx].dh = dh; property_table[idx].dserial = ++last_dserial; memset (&property_table[idx].flags, 0, sizeof property_table[idx].flags); *r_idx = idx; err = 0; leave: UNLOCK (property_table_lock); return err; } /* Remove the data object at PROPIDX from the table. DH is only used * for cross checking. */ static void remove_from_property_table (gpgme_data_t dh, unsigned int propidx) { LOCK (property_table_lock); assert (property_table); assert (propidx < property_table_size); assert (property_table[propidx].dh == dh); property_table[propidx].dh = NULL; UNLOCK (property_table_lock); } /* Return the data object's serial number for handle DH. This is a * unique serial number for each created data object. */ uint64_t _gpgme_data_get_dserial (gpgme_data_t dh) { uint64_t dserial; unsigned int idx; if (!dh) return 0; idx = dh->propidx; LOCK (property_table_lock); assert (property_table); assert (idx < property_table_size); assert (property_table[idx].dh == dh); dserial = property_table[idx].dserial; UNLOCK (property_table_lock); return dserial; } /* Set an internal property of a data object. The data object may * either be identified by the usual DH or by using the data serial * number DSERIAL. */ gpg_error_t _gpgme_data_set_prop (gpgme_data_t dh, uint64_t dserial, data_prop_t name, int value) { gpg_error_t err = 0; int idx; TRACE_BEG3 (DEBUG_DATA, "gpgme_data_set_prop", dh, "dserial=%llu %lu=%d", (unsigned long long)dserial, (unsigned long)name, value); LOCK (property_table_lock); if ((!dh && !dserial) || (dh && dserial)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } if (dh) /* Lookup via handle. */ { idx = dh->propidx; assert (property_table); assert (idx < property_table_size); assert (property_table[idx].dh == dh); } else /* Lookup via DSERIAL. */ { if (!property_table) { err = gpg_error (GPG_ERR_NOT_FOUND); goto leave; } for (idx = 0; idx < property_table_size; idx++) if (property_table[idx].dh && property_table[idx].dserial == dserial) break; if (!(idx < property_table_size)) { err = gpg_error (GPG_ERR_NOT_FOUND); goto leave; } } switch (name) { case DATA_PROP_NONE: /* Nothing to to do. */ break; case DATA_PROP_BLANKOUT: property_table[idx].flags.blankout = !!value; break; default: err = gpg_error (GPG_ERR_UNKNOWN_NAME); break; } leave: UNLOCK (property_table_lock); return TRACE_ERR (err); } /* Get an internal property of a data object. This is the counter * part to _gpgme_data_set_property. The value of the property is * stored at R_VALUE. On error 0 is stored at R_VALUE. */ gpg_error_t _gpgme_data_get_prop (gpgme_data_t dh, uint64_t dserial, data_prop_t name, int *r_value) { gpg_error_t err = 0; int idx; TRACE_BEG2 (DEBUG_DATA, "gpgme_data_get_prop", dh, "dserial=%llu %lu", (unsigned long long)dserial, (unsigned long)name); *r_value = 0; LOCK (property_table_lock); if ((!dh && !dserial) || (dh && dserial)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } if (dh) /* Lookup via handle. */ { idx = dh->propidx; assert (property_table); assert (idx < property_table_size); assert (property_table[idx].dh == dh); } else /* Lookup via DSERIAL. */ { if (!property_table) { err = gpg_error (GPG_ERR_NOT_FOUND); goto leave; } for (idx = 0; idx < property_table_size; idx++) if (property_table[idx].dh && property_table[idx].dserial == dserial) break; if (!(idx < property_table_size)) { err = gpg_error (GPG_ERR_NOT_FOUND); goto leave; } } switch (name) { case DATA_PROP_NONE: /* Nothing to to do. */ break; case DATA_PROP_BLANKOUT: *r_value = property_table[idx].flags.blankout; break; default: err = gpg_error (GPG_ERR_UNKNOWN_NAME); break; } leave: UNLOCK (property_table_lock); return TRACE_ERR (err); } gpgme_error_t _gpgme_data_new (gpgme_data_t *r_dh, struct _gpgme_data_cbs *cbs) { gpgme_error_t err; gpgme_data_t dh; if (!r_dh) return gpg_error (GPG_ERR_INV_VALUE); *r_dh = NULL; if (_gpgme_selftest) return _gpgme_selftest; dh = calloc (1, sizeof (*dh)); if (!dh) return gpg_error_from_syserror (); dh->cbs = cbs; err = insert_into_property_table (dh, &dh->propidx); if (err) { free (dh); return err; } *r_dh = dh; return 0; } void _gpgme_data_release (gpgme_data_t dh) { if (!dh) return; remove_from_property_table (dh, dh->propidx); if (dh->file_name) free (dh->file_name); free (dh); } /* Read up to SIZE bytes into buffer BUFFER from the data object with the handle DH. Return the number of characters read, 0 on EOF and -1 on error. If an error occurs, errno is set. */ gpgme_ssize_t gpgme_data_read (gpgme_data_t dh, void *buffer, size_t size) { gpgme_ssize_t res; int blankout; TRACE_BEG2 (DEBUG_DATA, "gpgme_data_read", dh, "buffer=%p, size=%u", buffer, size); if (!dh) { gpg_err_set_errno (EINVAL); return TRACE_SYSRES (-1); } if (!dh->cbs->read) { gpg_err_set_errno (ENOSYS); return TRACE_SYSRES (-1); } if (_gpgme_data_get_prop (dh, 0, DATA_PROP_BLANKOUT, &blankout) || blankout) res = 0; else { do res = (*dh->cbs->read) (dh, buffer, size); while (res < 0 && errno == EINTR); } return TRACE_SYSRES (res); } /* Write up to SIZE bytes from buffer BUFFER to the data object with the handle DH. Return the number of characters written, or -1 on error. If an error occurs, errno is set. */ gpgme_ssize_t gpgme_data_write (gpgme_data_t dh, const void *buffer, size_t size) { gpgme_ssize_t res; TRACE_BEG2 (DEBUG_DATA, "gpgme_data_write", dh, "buffer=%p, size=%u", buffer, size); if (!dh) { gpg_err_set_errno (EINVAL); return TRACE_SYSRES (-1); } if (!dh->cbs->write) { gpg_err_set_errno (ENOSYS); return TRACE_SYSRES (-1); } do res = (*dh->cbs->write) (dh, buffer, size); while (res < 0 && errno == EINTR); return TRACE_SYSRES (res); } /* Set the current position from where the next read or write starts in the data object with the handle DH to OFFSET, relative to WHENCE. */ gpgme_off_t gpgme_data_seek (gpgme_data_t dh, gpgme_off_t offset, int whence) { TRACE_BEG2 (DEBUG_DATA, "gpgme_data_seek", dh, "offset=%lli, whence=%i", offset, whence); if (!dh) { gpg_err_set_errno (EINVAL); return TRACE_SYSRES (-1); } if (!dh->cbs->seek) { gpg_err_set_errno (ENOSYS); return TRACE_SYSRES (-1); } /* For relative movement, we must take into account the actual position of the read counter. */ if (whence == SEEK_CUR) offset -= dh->pending_len; offset = (*dh->cbs->seek) (dh, offset, whence); if (offset >= 0) dh->pending_len = 0; return TRACE_SYSRES (offset); } /* Convenience function to do a gpgme_data_seek (dh, 0, SEEK_SET). */ gpgme_error_t gpgme_data_rewind (gpgme_data_t dh) { gpgme_error_t err; TRACE_BEG (DEBUG_DATA, "gpgme_data_rewind", dh); err = ((gpgme_data_seek (dh, 0, SEEK_SET) == -1) ? gpg_error_from_syserror () : 0); return TRACE_ERR (err); } /* Release the data object with the handle DH. */ void gpgme_data_release (gpgme_data_t dh) { TRACE (DEBUG_DATA, "gpgme_data_release", dh); if (!dh) return; if (dh->cbs->release) (*dh->cbs->release) (dh); _gpgme_data_release (dh); } /* Get the current encoding meta information for the data object with handle DH. */ gpgme_data_encoding_t gpgme_data_get_encoding (gpgme_data_t dh) { TRACE1 (DEBUG_DATA, "gpgme_data_get_encoding", dh, "dh->encoding=%i", dh ? dh->encoding : GPGME_DATA_ENCODING_NONE); return dh ? dh->encoding : GPGME_DATA_ENCODING_NONE; } /* Set the encoding meta information for the data object with handle DH to ENC. */ gpgme_error_t gpgme_data_set_encoding (gpgme_data_t dh, gpgme_data_encoding_t enc) { TRACE_BEG1 (DEBUG_DATA, "gpgme_data_set_encoding", dh, "encoding=%i", enc); if (!dh) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (enc < 0 || enc > GPGME_DATA_ENCODING_MIME) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); dh->encoding = enc; return TRACE_ERR (0); } /* Set the file name associated with the data object with handle DH to FILE_NAME. */ gpgme_error_t gpgme_data_set_file_name (gpgme_data_t dh, const char *file_name) { TRACE_BEG1 (DEBUG_DATA, "gpgme_data_set_file_name", dh, "file_name=%s", file_name); if (!dh) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (dh->file_name) free (dh->file_name); if (file_name) { dh->file_name = strdup (file_name); if (!dh->file_name) return TRACE_ERR (gpg_error_from_syserror ()); } else dh->file_name = 0; return TRACE_ERR (0); } /* Get the file name associated with the data object with handle DH, or NULL if there is none. */ char * gpgme_data_get_file_name (gpgme_data_t dh) { if (!dh) { TRACE (DEBUG_DATA, "gpgme_data_get_file_name", dh); return NULL; } TRACE1 (DEBUG_DATA, "gpgme_data_get_file_name", dh, "dh->file_name=%s", dh->file_name); return dh->file_name; } /* Set a flag for the data object DH. See the manual for details. */ gpg_error_t gpgme_data_set_flag (gpgme_data_t dh, const char *name, const char *value) { TRACE_BEG2 (DEBUG_DATA, "gpgme_data_set_flag", dh, "%s=%s", name, value); if (!dh) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (!strcmp (name, "size-hint")) { dh->size_hint= value? _gpgme_string_to_off (value) : 0; } else return gpg_error (GPG_ERR_UNKNOWN_NAME); return 0; } /* Functions to support the wait interface. */ gpgme_error_t _gpgme_data_inbound_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; gpgme_data_t dh = (gpgme_data_t) data->handler_value; char buffer[BUFFER_SIZE]; char *bufp = buffer; gpgme_ssize_t buflen; TRACE_BEG1 (DEBUG_CTX, "_gpgme_data_inbound_handler", dh, "fd=0x%x", fd); buflen = _gpgme_io_read (fd, buffer, BUFFER_SIZE); if (buflen < 0) return gpg_error_from_syserror (); if (buflen == 0) { _gpgme_io_close (fd); return TRACE_ERR (0); } do { gpgme_ssize_t amt = gpgme_data_write (dh, bufp, buflen); if (amt == 0 || (amt < 0 && errno != EINTR)) return TRACE_ERR (gpg_error_from_syserror ()); bufp += amt; buflen -= amt; } while (buflen > 0); return TRACE_ERR (0); } gpgme_error_t _gpgme_data_outbound_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; gpgme_data_t dh = (gpgme_data_t) data->handler_value; gpgme_ssize_t nwritten; TRACE_BEG1 (DEBUG_CTX, "_gpgme_data_outbound_handler", dh, "fd=0x%x", fd); if (!dh->pending_len) { gpgme_ssize_t amt = gpgme_data_read (dh, dh->pending, BUFFER_SIZE); if (amt < 0) return TRACE_ERR (gpg_error_from_syserror ()); if (amt == 0) { _gpgme_io_close (fd); return TRACE_ERR (0); } dh->pending_len = amt; } nwritten = _gpgme_io_write (fd, dh->pending, dh->pending_len); if (nwritten == -1 && errno == EAGAIN) return TRACE_ERR (0); if (nwritten == -1 && errno == EPIPE) { /* Not much we can do. The other end closed the pipe, but we still have data. This should only ever happen if the other end is going to tell us what happened on some other channel. Silently close our end. */ _gpgme_io_close (fd); return TRACE_ERR (0); } if (nwritten <= 0) return TRACE_ERR (gpg_error_from_syserror ()); if (nwritten < dh->pending_len) memmove (dh->pending, dh->pending + nwritten, dh->pending_len - nwritten); dh->pending_len -= nwritten; return TRACE_ERR (0); } /* Get the file descriptor associated with DH, if possible. Otherwise return -1. */ int _gpgme_data_get_fd (gpgme_data_t dh) { if (!dh || !dh->cbs->get_fd) return -1; return (*dh->cbs->get_fd) (dh); } /* Get the size-hint value for DH or 0 if not available. */ gpgme_off_t _gpgme_data_get_size_hint (gpgme_data_t dh) { return dh ? dh->size_hint : 0; } diff --git a/src/debug.c b/src/debug.c index 84aa4084..364a5767 100644 --- a/src/debug.c +++ b/src/debug.c @@ -1,400 +1,400 @@ /* debug.c - helpful output in desperate situations - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, - MA 02110-1301, USA. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #ifdef HAVE_UNISTD_H # include #endif #include #include #include #ifndef HAVE_DOSISH_SYSTEM # ifdef HAVE_SYS_TYPES_H # include # endif # ifdef HAVE_SYS_STAT_H # include # endif # include #endif #include #include "util.h" #include "ath.h" #include "sema.h" #include "sys-util.h" #include "debug.h" /* Lock to serialize initialization of the debug output subsystem and output of actual debug messages. */ DEFINE_STATIC_LOCK (debug_lock); /* The amount of detail requested by the user, per environment variable GPGME_DEBUG. */ static int debug_level; /* The output stream for the debug messages. */ static FILE *errfp; /* If not NULL, this malloced string is used instead of the GPGME_DEBUG envvar. It must have been set before the debug subsystem has been initialized. Using it later may or may not have any effect. */ static char *envvar_override; #ifdef HAVE_TLS #define FRAME_NR static __thread int frame_nr = 0; #endif void _gpgme_debug_frame_begin (void) { #ifdef FRAME_NR frame_nr++; #endif } int _gpgme_debug_frame_end (void) { #ifdef FRAME_NR frame_nr--; #endif return 0; } /* Remove leading and trailing white spaces. */ static char * trim_spaces (char *str) { char *string, *p, *mark; string = str; /* Find first non space character. */ for (p = string; *p && isspace (*(unsigned char *) p); p++) ; /* Move characters. */ for (mark = NULL; (*string = *p); string++, p++) if (isspace (*(unsigned char *) p)) { if (!mark) mark = string; } else mark = NULL; if (mark) *mark = '\0'; /* Remove trailing spaces. */ return str; } /* This is an internal function to set debug info. The caller must assure that this function is called only by one thread at a time. The function may have no effect if called after the debug system has been initialized. Returns 0 on success. */ int _gpgme_debug_set_debug_envvar (const char *value) { free (envvar_override); envvar_override = strdup (value); return !envvar_override; } static void debug_init (void) { static int initialized; LOCK (debug_lock); if (!initialized) { gpgme_error_t err; char *e; const char *s1, *s2;; if (envvar_override) { e = strdup (envvar_override); free (envvar_override); envvar_override = NULL; } else { err = _gpgme_getenv ("GPGME_DEBUG", &e); if (err) { UNLOCK (debug_lock); return; } } initialized = 1; errfp = stderr; if (e) { debug_level = atoi (e); s1 = strchr (e, PATHSEP_C); if (s1) { #ifndef HAVE_DOSISH_SYSTEM if (getuid () == geteuid () #if defined(HAVE_GETGID) && defined(HAVE_GETEGID) && getgid () == getegid () #endif ) { #endif char *p; FILE *fp; s1++; if (!(s2 = strchr (s1, PATHSEP_C))) s2 = s1 + strlen (s1); p = malloc (s2 - s1 + 1); if (p) { memcpy (p, s1, s2 - s1); p[s2-s1] = 0; trim_spaces (p); fp = fopen (p,"a"); if (fp) { setvbuf (fp, NULL, _IOLBF, 0); errfp = fp; } free (p); } #ifndef HAVE_DOSISH_SYSTEM } #endif } free (e); } } UNLOCK (debug_lock); if (debug_level > 0) { _gpgme_debug (DEBUG_INIT, "gpgme_debug: level=%d\n", debug_level); #ifdef HAVE_W32_SYSTEM { const char *name = _gpgme_get_inst_dir (); _gpgme_debug (DEBUG_INIT, "gpgme_debug: gpgme='%s'\n", name? name: "?"); } #endif } } /* This should be called as soon as the locks are initialized. It is required so that the assuan logging gets conncted to the gpgme log stream as early as possible. */ void _gpgme_debug_subsystem_init (void) { debug_init (); } /* Log the formatted string FORMAT at debug level LEVEL or higher. * * Returns: 0 * * Note that we always return 0 because the old TRACE macro evaluated * to 0 which issues a warning with newer gcc version about an unused * values. By using a return value of this function this can be * avoided. Fixme: It might be useful to check whether the return * value from the TRACE macros are actually used somewhere. */ int _gpgme_debug (int level, const char *format, ...) { va_list arg_ptr; int saved_errno; saved_errno = errno; if (debug_level < level) return 0; va_start (arg_ptr, format); LOCK (debug_lock); { struct tm *tp; time_t atime = time (NULL); tp = localtime (&atime); fprintf (errfp, "GPGME %04d-%02d-%02d %02d:%02d:%02d <0x%04llx> ", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec, (unsigned long long) ath_self ()); } #ifdef FRAME_NR { int indent; indent = frame_nr > 0? (2 * (frame_nr - 1)):0; fprintf (errfp, "%*s", indent < 40? indent : 40, ""); } #endif vfprintf (errfp, format, arg_ptr); va_end (arg_ptr); if(format && *format && format[strlen (format) - 1] != '\n') putc ('\n', errfp); UNLOCK (debug_lock); fflush (errfp); gpg_err_set_errno (saved_errno); return 0; } /* Start a new debug line in *LINE, logged at level LEVEL or higher, and starting with the formatted string FORMAT. */ void _gpgme_debug_begin (void **line, int level, const char *format, ...) { va_list arg_ptr; int res; if (debug_level < level) { /* Disable logging of this line. */ *line = NULL; return; } va_start (arg_ptr, format); res = gpgrt_vasprintf ((char **) line, format, arg_ptr); va_end (arg_ptr); if (res < 0) *line = NULL; } /* Add the formatted string FORMAT to the debug line *LINE. */ void _gpgme_debug_add (void **line, const char *format, ...) { va_list arg_ptr; char *toadd; char *result; int res; if (!*line) return; va_start (arg_ptr, format); res = gpgrt_vasprintf (&toadd, format, arg_ptr); va_end (arg_ptr); if (res < 0) { gpgrt_free (*line); *line = NULL; } res = gpgrt_asprintf (&result, "%s%s", *(char **) line, toadd); gpgrt_free (toadd); gpgrt_free (*line); if (res < 0) *line = NULL; else *line = result; } /* Finish construction of *LINE and send it to the debug output stream. */ void _gpgme_debug_end (void **line) { if (!*line) return; /* The smallest possible level is 1, so force logging here by using that. */ _gpgme_debug (1, "%s", *line); gpgrt_free (*line); *line = NULL; } #define TOHEX(val) (((val) < 10) ? ((val) + '0') : ((val) - 10 + 'a')) void _gpgme_debug_buffer (int lvl, const char *const fmt, const char *const func, const char *const buffer, size_t len) { int idx = 0; int j; if (!_gpgme_debug_trace ()) return; if (!buffer) return; while (idx < len) { char str[51]; char *strp = str; char *strp2 = &str[34]; for (j = 0; j < 16; j++) { unsigned char val; if (idx < len) { val = buffer[idx++]; *(strp++) = TOHEX (val >> 4); *(strp++) = TOHEX (val % 16); *(strp2++) = isprint (val) ? val : '.'; } else { *(strp++) = ' '; *(strp++) = ' '; } if (j == 7) *(strp++) = ' '; } *(strp++) = ' '; *(strp2) = '\0'; _gpgme_debug (lvl, fmt, func, str); } } diff --git a/src/decrypt-verify.c b/src/decrypt-verify.c index 224edc10..b9c3103a 100644 --- a/src/decrypt-verify.c +++ b/src/decrypt-verify.c @@ -1,183 +1,183 @@ /* decrypt-verify.c - Decrypt and verify function. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include "debug.h" #include "gpgme.h" #include "ops.h" static gpgme_error_t decrypt_verify_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_decrypt_status_handler (priv, code, args); if (!err) err = _gpgme_verify_status_handler (priv, code, args); return err; } static gpgme_error_t decrypt_verify_start (gpgme_ctx_t ctx, int synchronous, gpgme_decrypt_flags_t flags, gpgme_data_t cipher, gpgme_data_t plain) { gpgme_error_t err; assert ((flags & GPGME_DECRYPT_VERIFY)); err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_decrypt_init_result (ctx, plain); if (err) return err; err = _gpgme_op_verify_init_result (ctx); if (err) return err; if (!cipher) return gpg_error (GPG_ERR_NO_DATA); if (!plain) return gpg_error (GPG_ERR_INV_VALUE); if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } _gpgme_engine_set_status_handler (ctx->engine, decrypt_verify_status_handler, ctx); return _gpgme_engine_op_decrypt (ctx->engine, flags, cipher, plain, ctx->export_session_keys, ctx->override_session_key, ctx->auto_key_retrieve); } /* Decrypt ciphertext CIPHER and make a signature verification within CTX and store the resulting plaintext in PLAIN. */ gpgme_error_t gpgme_op_decrypt_verify_start (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_decrypt_verify_start", ctx, "cipher=%p, plain=%p", cipher, plain); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = decrypt_verify_start (ctx, 0, GPGME_DECRYPT_VERIFY, cipher, plain); return TRACE_ERR (err); } /* Decrypt ciphertext CIPHER and make a signature verification within CTX and store the resulting plaintext in PLAIN. */ gpgme_error_t gpgme_op_decrypt_verify (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_decrypt_verify", ctx, "cipher=%p, plain=%p", cipher, plain); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = decrypt_verify_start (ctx, 1, GPGME_DECRYPT_VERIFY, cipher, plain); if (!err) err = _gpgme_wait_one (ctx); ctx->ignore_mdc_error = 0; /* Always reset. */ return TRACE_ERR (err); } /* Decrypt ciphertext CIPHER within CTX and store the resulting plaintext in PLAIN. */ gpgme_error_t gpgme_op_decrypt_ext_start (gpgme_ctx_t ctx, gpgme_decrypt_flags_t flags, gpgme_data_t cipher, gpgme_data_t plain) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_decrypt_ext_start", ctx, "cipher=%p, plain=%p", cipher, plain); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if ((flags & GPGME_DECRYPT_VERIFY)) err = decrypt_verify_start (ctx, 0, flags, cipher, plain); else err = _gpgme_decrypt_start (ctx, 0, flags, cipher, plain); return TRACE_ERR (err); } /* Decrypt ciphertext CIPHER within CTX and store the resulting plaintext in PLAIN. */ gpgme_error_t gpgme_op_decrypt_ext (gpgme_ctx_t ctx, gpgme_decrypt_flags_t flags, gpgme_data_t cipher, gpgme_data_t plain) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_decrypt_ext", ctx, "cipher=%p, plain=%p", cipher, plain); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if ((flags & GPGME_DECRYPT_VERIFY)) err = decrypt_verify_start (ctx, 1, flags, cipher, plain); else err = _gpgme_decrypt_start (ctx, 1, flags, cipher, plain); if (!err) err = _gpgme_wait_one (ctx); ctx->ignore_mdc_error = 0; /* Always reset. */ return TRACE_ERR (err); } diff --git a/src/decrypt.c b/src/decrypt.c index b51603a3..77c45627 100644 --- a/src/decrypt.c +++ b/src/decrypt.c @@ -1,619 +1,619 @@ /* decrypt.c - Decrypt function. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2017 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2017 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include "debug.h" #include "gpgme.h" #include "util.h" #include "context.h" #include "ops.h" #include "data.h" typedef struct { struct _gpgme_op_decrypt_result result; /* The error code from a FAILURE status line or 0. */ gpg_error_t failure_code; int okay; /* A flag telling that the a decryption failed and an optional error * code to further specify the failure. */ int failed; gpg_error_t pkdecrypt_failed; /* At least one secret key is not available. gpg issues NO_SECKEY * status lines for each key the message has been encrypted to but * that secret key is not available. This can't be done for hidden * recipients, though. We track it here to allow for a better error * message than the general DECRYPTION_FAILED. */ int any_no_seckey; /* If the engine emits a DECRYPTION_INFO status and that does not * indicate that an integrity protection mode is active, this flag * is set. */ int not_integrity_protected; /* The error code from the first ERROR line. This is in some cases * used to return a better matching error code to the caller. */ gpg_error_t first_status_error; /* A pointer to the next pointer of the last recipient in the list. This makes appending new invalid signers painless while preserving the order. */ gpgme_recipient_t *last_recipient_p; /* The data object serial number of the plaintext. */ uint64_t plaintext_dserial; } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; gpgme_recipient_t recipient = opd->result.recipients; free (opd->result.unsupported_algorithm); free (opd->result.file_name); free (opd->result.session_key); free (opd->result.symkey_algo); while (recipient) { gpgme_recipient_t next = recipient->next; free (recipient); recipient = next; } } gpgme_decrypt_result_t gpgme_op_decrypt_result (gpgme_ctx_t ctx) { void *hook; op_data_t opd; gpgme_error_t err; TRACE_BEG (DEBUG_CTX, "gpgme_op_decrypt_result", ctx); ctx->ignore_mdc_error = 0; /* Always reset this flag. */ err = _gpgme_op_data_lookup (ctx, OPDATA_DECRYPT, &hook, -1, NULL); opd = hook; if (err || !opd) { TRACE_SUC0 ("result=(null)"); return NULL; } /* Make sure that SYMKEY_ALGO has a value. */ if (!opd->result.symkey_algo) { opd->result.symkey_algo = strdup ("?.?"); if (!opd->result.symkey_algo) { TRACE_SUC0 ("result=(null)"); return NULL; } } if (_gpgme_debug_trace ()) { gpgme_recipient_t rcp; if (opd->result.unsupported_algorithm) { TRACE_LOG1 ("result: unsupported_algorithm: %s", opd->result.unsupported_algorithm); } if (opd->result.wrong_key_usage) { TRACE_LOG ("result: wrong key usage"); } rcp = opd->result.recipients; while (rcp) { TRACE_LOG3 ("result: recipient: keyid=%s, pubkey_algo=%i, " "status=%s", rcp->keyid, rcp->pubkey_algo, gpg_strerror (rcp->status)); rcp = rcp->next; } if (opd->result.file_name) { TRACE_LOG1 ("result: original file name: %s", opd->result.file_name); } } TRACE_SUC1 ("result=%p", &opd->result); return &opd->result; } /* Parse the ARGS of an error status line and record some error * conditions at OPD. Returns 0 on success. */ static gpgme_error_t parse_status_error (char *args, op_data_t opd) { gpgme_error_t err; char *field[3]; int nfields; char *args2; if (!args) return trace_gpg_error (GPG_ERR_INV_ENGINE); args2 = strdup (args); /* Split modifies the input string. */ nfields = _gpgme_split_fields (args2, field, DIM (field)); if (nfields < 1) { free (args2); return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Required arg missing. */ } err = nfields < 2 ? 0 : atoi (field[1]); if (!strcmp (field[0], "decrypt.algorithm")) { if (gpg_err_code (err) == GPG_ERR_UNSUPPORTED_ALGORITHM && nfields > 2 && strcmp (field[2], "?")) { opd->result.unsupported_algorithm = strdup (field[2]); if (!opd->result.unsupported_algorithm) { free (args2); return gpg_error_from_syserror (); } } } else if (!strcmp (field[0], "decrypt.keyusage")) { if (gpg_err_code (err) == GPG_ERR_WRONG_KEY_USAGE) opd->result.wrong_key_usage = 1; } else if (!strcmp (field[0], "pkdecrypt_failed")) { switch (gpg_err_code (err)) { case GPG_ERR_CANCELED: case GPG_ERR_FULLY_CANCELED: /* It is better to return with a cancel error code than the * general decryption failed error code. */ opd->pkdecrypt_failed = gpg_err_make (gpg_err_source (err), GPG_ERR_CANCELED); break; case GPG_ERR_BAD_PASSPHRASE: /* A bad passphrase is severe enough that we return this * error code. */ opd->pkdecrypt_failed = err; break; default: /* For now all other error codes are ignored and the * standard DECRYPT_FAILED is returned. */ break; } } else if (!strcmp (field[0], "nomdc_with_legacy_cipher")) { opd->result.legacy_cipher_nomdc = 1; opd->not_integrity_protected = 1; } /* Record the first error code. */ if (err && !opd->first_status_error) opd->first_status_error = err; free (args2); return 0; } static gpgme_error_t parse_enc_to (char *args, gpgme_recipient_t *recp, gpgme_protocol_t protocol) { gpgme_recipient_t rec; char *tail; int i; rec = malloc (sizeof (*rec)); if (!rec) return gpg_error_from_syserror (); rec->next = NULL; rec->keyid = rec->_keyid; rec->status = 0; for (i = 0; i < sizeof (rec->_keyid) - 1; i++) { if (args[i] == '\0' || args[i] == ' ') break; rec->_keyid[i] = args[i]; } rec->_keyid[i] = '\0'; args = &args[i]; if (*args != '\0' && *args != ' ') { free (rec); return trace_gpg_error (GPG_ERR_INV_ENGINE); } while (*args == ' ') args++; if (*args) { gpg_err_set_errno (0); rec->pubkey_algo = _gpgme_map_pk_algo (strtol (args, &tail, 0), protocol); if (errno || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ free (rec); return trace_gpg_error (GPG_ERR_INV_ENGINE); } } /* FIXME: The key length is always 0 right now, so no need to parse it. */ *recp = rec; return 0; } /* Parse the ARGS of a * DECRYPTION_INFO [] * status. Returns 0 on success and updates the OPD. */ static gpgme_error_t parse_decryption_info (char *args, op_data_t opd, gpgme_protocol_t protocol) { char *field[3]; int nfields; char *args2; int mdc, aead_algo; const char *algostr, *modestr; if (!args) return trace_gpg_error (GPG_ERR_INV_ENGINE); args2 = strdup (args); /* Split modifies the input string. */ nfields = _gpgme_split_fields (args2, field, DIM (field)); if (nfields < 2) { free (args2); return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Required arg missing. */ } mdc = atoi (field[0]); algostr = _gpgme_cipher_algo_name (atoi (field[1]), protocol); aead_algo = nfields < 3? 0 : atoi (field[2]); modestr = _gpgme_cipher_mode_name (aead_algo, protocol); free (args2); free (opd->result.symkey_algo); if (!aead_algo && mdc != 2) opd->result.symkey_algo = _gpgme_strconcat (algostr, ".PGPCFB", NULL); else opd->result.symkey_algo = _gpgme_strconcat (algostr, ".", modestr, NULL); if (!opd->result.symkey_algo) return gpg_error_from_syserror (); if (!mdc && !aead_algo) opd->not_integrity_protected = 1; return 0; } gpgme_error_t _gpgme_decrypt_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_passphrase_status_handler (priv, code, args); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_DECRYPT, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_FAILURE: opd->failure_code = _gpgme_parse_failure (args); break; case GPGME_STATUS_EOF: /* We force an encryption failure if we know that integrity * protection is missing. For modern version of gpg using * modern cipher algorithms this is not required because gpg * will issue a failure anyway. However older gpg versions emit * only a warning. * Fixme: These error values should probably be attributed to * the underlying crypto engine (as error source). */ if (opd->failed) { /* This comes from a specialized ERROR status line. */ if (opd->pkdecrypt_failed) return opd->pkdecrypt_failed; /* For an integrity failure return just DECRYPTION_FAILED; * the actual cause can be taken from an already set * decryption result flag. */ if ((opd->not_integrity_protected && !ctx->ignore_mdc_error)) return gpg_error (GPG_ERR_DECRYPT_FAILED); /* If we have any other ERROR code we prefer that over * NO_SECKEY because it is probably the better matching * code. For example a garbled message with multiple * plaintext will return BAD_DATA here but may also have * indicated a NO_SECKEY. */ if (opd->first_status_error) return opd->first_status_error; /* No secret key is pretty common reason. */ if (opd->any_no_seckey) return gpg_error (GPG_ERR_NO_SECKEY); /* Generic decryption failed error code. */ return gpg_error (GPG_ERR_DECRYPT_FAILED); } else if (!opd->okay) { /* No data was found. */ return gpg_error (GPG_ERR_NO_DATA); } else if (opd->failure_code) { /* The engine returned failure code at program exit. */ return opd->failure_code; } break; case GPGME_STATUS_DECRYPTION_INFO: err = parse_decryption_info (args, opd, ctx->protocol); if (err) return err; break; case GPGME_STATUS_DECRYPTION_OKAY: opd->okay = 1; break; case GPGME_STATUS_DECRYPTION_FAILED: opd->failed = 1; /* Tell the data object that it shall not return any data. We * use the serial number because the data object may be owned by * another thread. We also don't check for an error because it * is possible that the data object has already been destroyed * and we are then not interested in returning an error. */ if (!ctx->ignore_mdc_error) _gpgme_data_set_prop (NULL, opd->plaintext_dserial, DATA_PROP_BLANKOUT, 1); break; case GPGME_STATUS_ERROR: /* Note that this is an informational status code which should * not lead to an error return unless it is something not * related to the backend. However, it is used to return a * better matching final error code. */ err = parse_status_error (args, opd); if (err) return err; break; case GPGME_STATUS_ENC_TO: err = parse_enc_to (args, opd->last_recipient_p, ctx->protocol); if (err) return err; opd->last_recipient_p = &(*opd->last_recipient_p)->next; break; case GPGME_STATUS_SESSION_KEY: if (opd->result.session_key) free (opd->result.session_key); opd->result.session_key = strdup(args); break; case GPGME_STATUS_NO_SECKEY: { gpgme_recipient_t rec = opd->result.recipients; while (rec) { if (!strcmp (rec->keyid, args)) { rec->status = gpg_error (GPG_ERR_NO_SECKEY); break; } rec = rec->next; } /* FIXME: Is this ok? */ if (!rec) return trace_gpg_error (GPG_ERR_INV_ENGINE); opd->any_no_seckey = 1; } break; case GPGME_STATUS_PLAINTEXT: { int mime = 0; err = _gpgme_parse_plaintext (args, &opd->result.file_name, &mime); if (err) return err; opd->result.is_mime = !!mime; } break; case GPGME_STATUS_INQUIRE_MAXLEN: if (ctx->status_cb && !ctx->full_status) { err = ctx->status_cb (ctx->status_cb_value, "INQUIRE_MAXLEN", args); if (err) return err; } break; case GPGME_STATUS_DECRYPTION_COMPLIANCE_MODE: PARSE_COMPLIANCE_FLAGS (args, &opd->result); break; default: break; } return 0; } static gpgme_error_t decrypt_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_decrypt_status_handler (priv, code, args); return err; } gpgme_error_t _gpgme_op_decrypt_init_result (gpgme_ctx_t ctx, gpgme_data_t plaintext) { gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_DECRYPT, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; opd->last_recipient_p = &opd->result.recipients; opd->plaintext_dserial = _gpgme_data_get_dserial (plaintext); return 0; } gpgme_error_t _gpgme_decrypt_start (gpgme_ctx_t ctx, int synchronous, gpgme_decrypt_flags_t flags, gpgme_data_t cipher, gpgme_data_t plain) { gpgme_error_t err; assert (!(flags & GPGME_DECRYPT_VERIFY)); err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_decrypt_init_result (ctx, plain); if (err) return err; if (!cipher) return gpg_error (GPG_ERR_NO_DATA); if (!plain) return gpg_error (GPG_ERR_INV_VALUE); if (err) return err; if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } _gpgme_engine_set_status_handler (ctx->engine, decrypt_status_handler, ctx); return _gpgme_engine_op_decrypt (ctx->engine, flags, cipher, plain, ctx->export_session_keys, ctx->override_session_key, ctx->auto_key_retrieve); } gpgme_error_t gpgme_op_decrypt_start (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_decrypt_start", ctx, "cipher=%p, plain=%p", cipher, plain); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_decrypt_start (ctx, 0, 0, cipher, plain); return TRACE_ERR (err); } /* Decrypt ciphertext CIPHER within CTX and store the resulting plaintext in PLAIN. */ gpgme_error_t gpgme_op_decrypt (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_decrypt", ctx, "cipher=%p, plain=%p", cipher, plain); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_decrypt_start (ctx, 1, 0, cipher, plain); if (!err) err = _gpgme_wait_one (ctx); ctx->ignore_mdc_error = 0; /* Always reset. */ return TRACE_ERR (err); } diff --git a/src/dirinfo.c b/src/dirinfo.c index 73744124..10b7b5ee 100644 --- a/src/dirinfo.c +++ b/src/dirinfo.c @@ -1,471 +1,472 @@ /* dirinfo.c - Get directory information * Copyright (C) 2009, 2013 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #include "gpgme.h" #include "util.h" #include "priv-io.h" #include "debug.h" #include "sema.h" #include "sys-util.h" DEFINE_STATIC_LOCK (dirinfo_lock); /* Constants used internally to select the data. */ enum { WANT_HOMEDIR, WANT_SYSCONFDIR, WANT_BINDIR, WANT_LIBEXECDIR, WANT_LIBDIR, WANT_DATADIR, WANT_LOCALEDIR, WANT_AGENT_SOCKET, WANT_AGENT_SSH_SOCKET, WANT_DIRMNGR_SOCKET, WANT_UISRV_SOCKET, WANT_GPGCONF_NAME, WANT_GPG_NAME, WANT_GPGSM_NAME, WANT_G13_NAME, WANT_GPG_WKS_CLIENT_NAME, WANT_GPG_ONE_MODE }; /* Values retrieved via gpgconf and cached here. */ static struct { int valid; /* Cached information is valid. */ int disable_gpgconf; char *homedir; char *sysconfdir; char *bindir; char *libexecdir; char *libdir; char *datadir; char *localedir; char *agent_socket; char *agent_ssh_socket; char *dirmngr_socket; char *uisrv_socket; char *gpgconf_name; char *gpg_name; char *gpgsm_name; char *g13_name; char *gpg_wks_client_name; int gpg_one_mode; /* System is in gpg1 mode. */ } dirinfo; /* Helper function to be used only by gpgme_set_global_flag. */ void _gpgme_dirinfo_disable_gpgconf (void) { dirinfo.disable_gpgconf = 1; } /* Return the length of the directory part including the trailing * slash of NAME. */ static size_t dirname_len (const char *name) { return _gpgme_get_basename (name) - name; } /* Parse the output of "gpgconf --list-dirs". This function expects that DIRINFO_LOCK is held by the caller. If COMPONENTS is set, the output of --list-components is expected. */ static void parse_output (char *line, int components) { char *value, *p; size_t n; value = strchr (line, ':'); if (!value) return; *value++ = 0; if (components) { /* Skip the second field. */ value = strchr (value, ':'); if (!value) return; *value++ = 0; } p = strchr (value, ':'); if (p) *p = 0; if (_gpgme_decode_percent_string (value, &value, strlen (value)+1, 0)) return; if (!*value) return; if (components) { if (!strcmp (line, "gpg") && !dirinfo.gpg_name) dirinfo.gpg_name = strdup (value); else if (!strcmp (line, "gpgsm") && !dirinfo.gpgsm_name) dirinfo.gpgsm_name = strdup (value); else if (!strcmp (line, "g13") && !dirinfo.g13_name) dirinfo.g13_name = strdup (value); } else { if (!strcmp (line, "homedir") && !dirinfo.homedir) dirinfo.homedir = strdup (value); else if (!strcmp (line, "sysconfdir") && !dirinfo.sysconfdir) dirinfo.sysconfdir = strdup (value); else if (!strcmp (line, "bindir") && !dirinfo.bindir) dirinfo.bindir = strdup (value); else if (!strcmp (line, "libexecdir") && !dirinfo.libexecdir) dirinfo.libexecdir = strdup (value); else if (!strcmp (line, "libdir") && !dirinfo.libdir) dirinfo.libdir = strdup (value); else if (!strcmp (line, "datadir") && !dirinfo.datadir) dirinfo.datadir = strdup (value); else if (!strcmp (line, "localedir") && !dirinfo.localedir) dirinfo.localedir = strdup (value); else if (!strcmp (line, "agent-socket") && !dirinfo.agent_socket) { const char name[] = "S.uiserver"; char *buffer; dirinfo.agent_socket = strdup (value); if (dirinfo.agent_socket) { n = dirname_len (dirinfo.agent_socket); buffer = malloc (n + strlen (name) + 1); if (buffer) { strncpy (buffer, dirinfo.agent_socket, n); strcpy (buffer + n, name); dirinfo.uisrv_socket = buffer; } } } else if (!strcmp (line, "dirmngr-socket") && !dirinfo.dirmngr_socket) dirinfo.dirmngr_socket = strdup (value); else if (!strcmp (line, "agent-ssh-socket") && !dirinfo.agent_ssh_socket) dirinfo.agent_ssh_socket = strdup (value); } } /* Read the directory information from gpgconf. This function expects that DIRINFO_LOCK is held by the caller. PGNAME is the name of the gpgconf binary. If COMPONENTS is set, not the directories bit the name of the componeNts are read. */ static void read_gpgconf_dirs (const char *pgmname, int components) { char linebuf[1024] = {0}; int linelen = 0; char * argv[3]; int rp[2]; struct spawn_fd_item_s cfd[] = { {-1, 1 /* STDOUT_FILENO */, -1, 0}, {-1, -1} }; int status; int nread; char *mark = NULL; argv[0] = (char *)pgmname; argv[1] = (char*)(components? "--list-components" : "--list-dirs"); argv[2] = NULL; if (_gpgme_io_pipe (rp, 1) < 0) return; cfd[0].fd = rp[1]; status = _gpgme_io_spawn (pgmname, argv, IOSPAWN_FLAG_DETACHED, cfd, NULL, NULL, NULL); if (status < 0) { _gpgme_io_close (rp[0]); _gpgme_io_close (rp[1]); return; } do { nread = _gpgme_io_read (rp[0], linebuf + linelen, sizeof linebuf - linelen - 1); if (nread > 0) { char *line; const char *lastmark = NULL; size_t nused; linelen += nread; linebuf[linelen] = '\0'; for (line=linebuf; (mark = strchr (line, '\n')); line = mark+1 ) { lastmark = mark; if (mark > line && mark[-1] == '\r') mark[-1] = '\0'; else mark[0] = '\0'; parse_output (line, components); } nused = lastmark? (lastmark + 1 - linebuf) : 0; memmove (linebuf, linebuf + nused, linelen - nused); linelen -= nused; } } while (nread > 0 && linelen < sizeof linebuf - 1); _gpgme_io_close (rp[0]); } static const char * get_gpgconf_item (int what) { const char *result = NULL; LOCK (dirinfo_lock); if (!dirinfo.valid) { char *pgmname; pgmname = dirinfo.disable_gpgconf? NULL : _gpgme_get_gpgconf_path (); if (pgmname && access (pgmname, F_OK)) { _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: gpgconf='%s' [not installed]\n", pgmname); free (pgmname); pgmname = NULL; /* Not available. */ } else _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: gpgconf='%s'\n", pgmname? pgmname : "[null]"); if (!pgmname) { /* Probably gpgconf is not installed. Assume we are using GnuPG-1. */ dirinfo.gpg_one_mode = 1; pgmname = _gpgme_get_gpg_path (); if (pgmname) dirinfo.gpg_name = pgmname; } else { dirinfo.gpg_one_mode = 0; read_gpgconf_dirs (pgmname, 0); read_gpgconf_dirs (pgmname, 1); dirinfo.gpgconf_name = pgmname; } /* Even if the reading of the directories failed (e.g. due to an too old version gpgconf or no gpgconf at all), we need to mark the entries as valid so that we won't try over and over to read them. Note further that we are not able to change the read values later because they are practically statically allocated. */ dirinfo.valid = 1; if (dirinfo.gpg_name) _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: gpg='%s'\n", dirinfo.gpg_name); if (dirinfo.g13_name) _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: g13='%s'\n", dirinfo.g13_name); if (dirinfo.gpgsm_name) _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: gpgsm='%s'\n", dirinfo.gpgsm_name); if (dirinfo.homedir) _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: homedir='%s'\n", dirinfo.homedir); if (dirinfo.agent_socket) _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: agent='%s'\n", dirinfo.agent_socket); if (dirinfo.agent_ssh_socket) _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: ssh='%s'\n", dirinfo.agent_ssh_socket); if (dirinfo.dirmngr_socket) _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: dirmngr='%s'\n", dirinfo.dirmngr_socket); if (dirinfo.uisrv_socket) _gpgme_debug (DEBUG_INIT, "gpgme-dinfo: uisrv='%s'\n", dirinfo.uisrv_socket); } switch (what) { case WANT_HOMEDIR: result = dirinfo.homedir; break; case WANT_SYSCONFDIR: result = dirinfo.sysconfdir; break; case WANT_BINDIR: result = dirinfo.bindir; break; case WANT_LIBEXECDIR: result = dirinfo.libexecdir; break; case WANT_LIBDIR: result = dirinfo.libdir; break; case WANT_DATADIR: result = dirinfo.datadir; break; case WANT_LOCALEDIR: result = dirinfo.localedir; break; case WANT_AGENT_SOCKET: result = dirinfo.agent_socket; break; case WANT_AGENT_SSH_SOCKET: result = dirinfo.agent_ssh_socket; break; case WANT_DIRMNGR_SOCKET: result = dirinfo.dirmngr_socket; break; case WANT_GPGCONF_NAME: result = dirinfo.gpgconf_name; break; case WANT_GPG_NAME: result = dirinfo.gpg_name; break; case WANT_GPGSM_NAME: result = dirinfo.gpgsm_name; break; case WANT_G13_NAME: result = dirinfo.g13_name; break; case WANT_UISRV_SOCKET: result = dirinfo.uisrv_socket; break; case WANT_GPG_ONE_MODE: result = dirinfo.gpg_one_mode? "1":NULL; break; case WANT_GPG_WKS_CLIENT_NAME: if (!dirinfo.gpg_wks_client_name && dirinfo.libexecdir) dirinfo.gpg_wks_client_name = _gpgme_strconcat (dirinfo.libexecdir, "/", "gpg-wks-client", NULL); result = dirinfo.gpg_wks_client_name; break; } UNLOCK (dirinfo_lock); return result; } /* Return the default home directory. Returns NULL if not known. */ const char * _gpgme_get_default_homedir (void) { return get_gpgconf_item (WANT_HOMEDIR); } /* Return the default gpg-agent socket name. Returns NULL if not known. */ const char * _gpgme_get_default_agent_socket (void) { return get_gpgconf_item (WANT_AGENT_SOCKET); } /* Return the default gpg file name. Returns NULL if not known. */ const char * _gpgme_get_default_gpg_name (void) { return get_gpgconf_item (WANT_GPG_NAME); } /* Return the default gpgsm file name. Returns NULL if not known. */ const char * _gpgme_get_default_gpgsm_name (void) { return get_gpgconf_item (WANT_GPGSM_NAME); } /* Return the default g13 file name. Returns NULL if not known. */ const char * _gpgme_get_default_g13_name (void) { return get_gpgconf_item (WANT_G13_NAME); } /* Return the default gpgconf file name. Returns NULL if not known. */ const char * _gpgme_get_default_gpgconf_name (void) { return get_gpgconf_item (WANT_GPGCONF_NAME); } /* Return the default UI-server socket name. Returns NULL if not known. */ const char * _gpgme_get_default_uisrv_socket (void) { return get_gpgconf_item (WANT_UISRV_SOCKET); } /* Return true if we are in GnuPG-1 mode - ie. no gpgconf and agent being optional. */ int _gpgme_in_gpg_one_mode (void) { return !!get_gpgconf_item (WANT_GPG_ONE_MODE); } /* Helper function to return the basename of the passed filename. */ const char * _gpgme_get_basename (const char *name) { const char *s; if (!name || !*name) return name; for (s = name + strlen (name) -1; s >= name; s--) if (*s == '/' #ifdef HAVE_W32_SYSTEM || *s == '\\' || *s == ':' #endif ) return s+1; return name; } /* Return default values for various directories and file names. */ const char * gpgme_get_dirinfo (const char *what) { if (!what) return NULL; else if (!strcmp (what, "homedir")) return get_gpgconf_item (WANT_HOMEDIR); else if (!strcmp (what, "agent-socket")) return get_gpgconf_item (WANT_AGENT_SOCKET); else if (!strcmp (what, "uiserver-socket")) return get_gpgconf_item (WANT_UISRV_SOCKET); else if (!strcmp (what, "gpgconf-name")) return get_gpgconf_item (WANT_GPGCONF_NAME); else if (!strcmp (what, "gpg-name")) return get_gpgconf_item (WANT_GPG_NAME); else if (!strcmp (what, "gpgsm-name")) return get_gpgconf_item (WANT_GPGSM_NAME); else if (!strcmp (what, "g13-name")) return get_gpgconf_item (WANT_G13_NAME); else if (!strcmp (what, "gpg-wks-client-name")) return get_gpgconf_item (WANT_GPG_WKS_CLIENT_NAME); else if (!strcmp (what, "agent-ssh-socket")) return get_gpgconf_item (WANT_AGENT_SSH_SOCKET); else if (!strcmp (what, "dirmngr-socket")) return get_gpgconf_item (WANT_DIRMNGR_SOCKET); else if (!strcmp (what, "sysconfdir")) return get_gpgconf_item (WANT_SYSCONFDIR); else if (!strcmp (what, "bindir")) return get_gpgconf_item (WANT_BINDIR); else if (!strcmp (what, "libexecdir")) return get_gpgconf_item (WANT_LIBEXECDIR); else if (!strcmp (what, "libdir")) return get_gpgconf_item (WANT_LIBDIR); else if (!strcmp (what, "datadir")) return get_gpgconf_item (WANT_DATADIR); else if (!strcmp (what, "localedir")) return get_gpgconf_item (WANT_LOCALEDIR); else return NULL; } diff --git a/src/edit.c b/src/edit.c index 2867efb5..feb7cf66 100644 --- a/src/edit.c +++ b/src/edit.c @@ -1,314 +1,314 @@ /* edit.c - Key edit function. - Copyright (C) 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" #include "util.h" typedef struct { /* The user callback function and its hook value. */ gpgme_interact_cb_t fnc; gpgme_edit_cb_t fnc_old; void *fnc_value; } *op_data_t; static gpgme_error_t edit_status_handler (void *priv, gpgme_status_code_t status, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_passphrase_status_handler (priv, status, args); if (err) return err; err = _gpgme_progress_status_handler (priv, status, args); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_EDIT, &hook, -1, NULL); opd = hook; if (err) return err; if (opd->fnc_old) return (*opd->fnc_old) (opd->fnc_value, status, args, -1); return (*opd->fnc) (opd->fnc_value, _gpgme_status_to_string (status), args, -1); } static gpgme_error_t command_handler (void *priv, gpgme_status_code_t status, const char *args, int fd, int *processed_r) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; int processed = 0; if (ctx->passphrase_cb) { err = _gpgme_passphrase_command_handler (ctx, status, args, fd, &processed); if (err) return err; } else err = 0; if (!processed) { void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_EDIT, &hook, -1, NULL); opd = hook; if (err) return err; if (opd->fnc_old) err = (*opd->fnc_old) (opd->fnc_value, status, args, fd); else err = (*opd->fnc) (opd->fnc_value, _gpgme_status_to_string (status), args, fd); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; else processed = 1; } *processed_r = processed; return err; } static gpgme_error_t interact_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t key, unsigned int flags, gpgme_interact_cb_t fnc, void *fnc_value, gpgme_data_t out) { gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; if (!fnc || !out) return gpg_error (GPG_ERR_INV_VALUE); err = _gpgme_op_data_lookup (ctx, OPDATA_EDIT, &hook, sizeof (*opd), NULL); opd = hook; if (err) return err; opd->fnc = fnc; opd->fnc_old = NULL; opd->fnc_value = fnc_value; err = _gpgme_engine_set_command_handler (ctx->engine, command_handler, ctx); if (err) return err; _gpgme_engine_set_status_handler (ctx->engine, edit_status_handler, ctx); return _gpgme_engine_op_edit (ctx->engine, (flags & GPGME_INTERACT_CARD)? 1: 0, key, out, ctx); } gpgme_error_t gpgme_op_interact_start (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags, gpgme_interact_cb_t fnc, void *fnc_value, gpgme_data_t out) { gpgme_error_t err; TRACE_BEG5 (DEBUG_CTX, "gpgme_op_interact_start", ctx, "key=%p flags=0x%x fnc=%p fnc_value=%p, out=%p", key, flags,fnc, fnc_value, out); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = interact_start (ctx, 0, key, flags, fnc, fnc_value, out); return err; } gpgme_error_t gpgme_op_interact (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags, gpgme_interact_cb_t fnc, void *fnc_value, gpgme_data_t out) { gpgme_error_t err; TRACE_BEG5 (DEBUG_CTX, "gpgme_op_interact", ctx, "key=%p flags=0x%x fnc=%p fnc_value=%p, out=%p", key, flags,fnc, fnc_value, out); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = interact_start (ctx, 1, key, flags, fnc, fnc_value, out); if (!err) err = _gpgme_wait_one (ctx); return err; } /* The deprecated interface. */ static gpgme_error_t edit_start (gpgme_ctx_t ctx, int synchronous, int type, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) { gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; if (!fnc || !out) return gpg_error (GPG_ERR_INV_VALUE); err = _gpgme_op_data_lookup (ctx, OPDATA_EDIT, &hook, sizeof (*opd), NULL); opd = hook; if (err) return err; opd->fnc = NULL; opd->fnc_old = fnc; opd->fnc_value = fnc_value; err = _gpgme_engine_set_command_handler (ctx->engine, command_handler, ctx); if (err) return err; _gpgme_engine_set_status_handler (ctx->engine, edit_status_handler, ctx); return _gpgme_engine_op_edit (ctx->engine, type, key, out, ctx); } gpgme_error_t gpgme_op_edit_start (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) { gpgme_error_t err; TRACE_BEG5 (DEBUG_CTX, "gpgme_op_edit_start", ctx, "key=%p (%s), fnc=%p fnc_value=%p, out=%p", key, (key && key->subkeys && key->subkeys->fpr) ? key->subkeys->fpr : "invalid", fnc, fnc_value, out); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = edit_start (ctx, 0, 0, key, fnc, fnc_value, out); return err; } /* Edit the key KEY. Send status and command requests to FNC and output of edit commands to OUT. */ gpgme_error_t gpgme_op_edit (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) { gpgme_error_t err; TRACE_BEG5 (DEBUG_CTX, "gpgme_op_edit", ctx, "key=%p (%s), fnc=%p fnc_value=%p, out=%p", key, (key && key->subkeys && key->subkeys->fpr) ? key->subkeys->fpr : "invalid", fnc, fnc_value, out); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = edit_start (ctx, 1, 0, key, fnc, fnc_value, out); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } gpgme_error_t gpgme_op_card_edit_start (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) { gpgme_error_t err; TRACE_BEG5 (DEBUG_CTX, "gpgme_op_card_edit_start", ctx, "key=%p (%s), fnc=%p fnc_value=%p, out=%p", key, (key && key->subkeys && key->subkeys->fpr) ? key->subkeys->fpr : "invalid", fnc, fnc_value, out); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = edit_start (ctx, 0, 1, key, fnc, fnc_value, out); return err; } /* Edit the card for the key KEY. Send status and command requests to FNC and output of edit commands to OUT. */ gpgme_error_t gpgme_op_card_edit (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) { gpgme_error_t err; TRACE_BEG5 (DEBUG_CTX, "gpgme_op_card_edit", ctx, "key=%p (%s), fnc=%p fnc_value=%p, out=%p", key, (key && key->subkeys && key->subkeys->fpr) ? key->subkeys->fpr : "invalid", fnc, fnc_value, out); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = edit_start (ctx, 1, 1, key, fnc, fnc_value, out); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } diff --git a/src/encrypt-sign.c b/src/encrypt-sign.c index cc34fbd5..acc1a05d 100644 --- a/src/encrypt-sign.c +++ b/src/encrypt-sign.c @@ -1,218 +1,218 @@ /* encrypt-sign.c - encrypt and verify functions - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" static gpgme_error_t encrypt_sign_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_encrypt_status_handler (priv, code, args); if (!err) err = _gpgme_sign_status_handler (priv, code, args); return err; } static gpgme_error_t encrypt_sym_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_sign_status_handler (priv, code, args); if (!err) err = _gpgme_passphrase_status_handler (priv, code, args); return err; } static gpgme_error_t encrypt_sign_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; int symmetric; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; symmetric = (!recp && !recpstring) || (flags & GPGME_ENCRYPT_SYMMETRIC); if (!plain) return gpg_error (GPG_ERR_NO_DATA); if (!cipher) return gpg_error (GPG_ERR_INV_VALUE); if (recp && !*recp) return gpg_error (GPG_ERR_INV_VALUE); err = _gpgme_op_encrypt_init_result (ctx); if (err) return err; err = _gpgme_op_sign_init_result (ctx); if (err) return err; if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } _gpgme_engine_set_status_handler (ctx->engine, symmetric ? encrypt_sym_status_handler : encrypt_sign_status_handler, ctx); return _gpgme_engine_op_encrypt_sign (ctx->engine, recp, recpstring, flags, plain, cipher, ctx->use_armor, ctx /* FIXME */); } /* Old version of gpgme_op_encrypt_sign_ext_start w/o RECPSTRING. */ gpgme_error_t gpgme_op_encrypt_sign_start (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { return gpgme_op_encrypt_sign_ext_start (ctx, recp, NULL, flags, plain, cipher); } /* Old version of gpgme_op_encrypt_sign_ext w/o RECPSTRING. */ gpgme_error_t gpgme_op_encrypt_sign (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { return gpgme_op_encrypt_sign_ext (ctx, recp, NULL, flags, plain, cipher); } /* Encrypt plaintext PLAIN within CTX for the recipients RECP and * store the resulting ciphertext in CIPHER. Also sign the ciphertext * with the signers in CTX. */ gpgme_error_t gpgme_op_encrypt_sign_ext (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign", ctx, "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && (recp || recpstring)) { if (recp) { int i = 0; while (recp[i]) { TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], (recp[i]->subkeys && recp[i]->subkeys->fpr) ? recp[i]->subkeys->fpr : "invalid"); i++; } } else { TRACE_LOG1 ("recipients = '%s'", recpstring); } } err = encrypt_sign_start (ctx, 1, recp, recpstring, flags, plain, cipher); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } /* Encrypt plaintext PLAIN within CTX for the recipients RECP and store the resulting ciphertext in CIPHER. Also sign the ciphertext with the signers in CTX. */ gpgme_error_t gpgme_op_encrypt_sign_ext_start (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_sign_start", ctx, "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && (recp || recpstring)) { if (recp) { int i = 0; while (recp[i]) { TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], (recp[i]->subkeys && recp[i]->subkeys->fpr) ? recp[i]->subkeys->fpr : "invalid"); i++; } } else { TRACE_LOG1 ("recipients = '%s'", recpstring); } } err = encrypt_sign_start (ctx, 0, recp, recpstring, flags, plain, cipher); return err; } diff --git a/src/encrypt.c b/src/encrypt.c index a27a53ac..b23d20d6 100644 --- a/src/encrypt.c +++ b/src/encrypt.c @@ -1,363 +1,363 @@ /* encrypt.c - Encrypt function. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" typedef struct { struct _gpgme_op_encrypt_result result; /* The error code from a FAILURE status line or 0. */ gpg_error_t failure_code; /* The fingerprint from the last KEY_CONSIDERED status line. */ char *kc_fpr; /* The flags from the last KEY_CONSIDERED status line. */ unsigned int kc_flags; /* A pointer to the next pointer of the last invalid recipient in the list. This makes appending new invalid recipients painless while preserving the order. */ gpgme_invalid_key_t *lastp; } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; gpgme_invalid_key_t invalid_recipient = opd->result.invalid_recipients; while (invalid_recipient) { gpgme_invalid_key_t next = invalid_recipient->next; if (invalid_recipient->fpr) free (invalid_recipient->fpr); free (invalid_recipient); invalid_recipient = next; } free (opd->kc_fpr); } gpgme_encrypt_result_t gpgme_op_encrypt_result (gpgme_ctx_t ctx) { void *hook; op_data_t opd; gpgme_error_t err; TRACE_BEG (DEBUG_CTX, "gpgme_op_encrypt_result", ctx); err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, &hook, -1, NULL); opd = hook; if (err || !opd) { TRACE_SUC0 ("result=(null)"); return NULL; } if (_gpgme_debug_trace ()) { gpgme_invalid_key_t invkeys = opd->result.invalid_recipients; int i = 0; while (invkeys) { TRACE_LOG3 ("invalid_recipients[%i] = %s (%s)", i, invkeys->fpr ? invkeys->fpr : "(null)", gpg_strerror (invkeys->reason)); invkeys = invkeys->next; i++; } } TRACE_SUC1 ("result=%p", &opd->result); return &opd->result; } gpgme_error_t _gpgme_encrypt_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_FAILURE: opd->failure_code = _gpgme_parse_failure (args); break; case GPGME_STATUS_EOF: if (opd->result.invalid_recipients) return gpg_error (GPG_ERR_UNUSABLE_PUBKEY); if (opd->failure_code) return opd->failure_code; break; case GPGME_STATUS_KEY_CONSIDERED: /* This is emitted during gpg's key lookup to give information * about the lookup results. We store the last one so it can be * used in connection with INV_RECP. */ free (opd->kc_fpr); opd->kc_fpr = NULL; err = _gpgme_parse_key_considered (args, &opd->kc_fpr, &opd->kc_flags); if (err) return err; break; case GPGME_STATUS_INV_RECP: err = _gpgme_parse_inv_recp (args, 0, opd->kc_fpr, opd->kc_flags, opd->lastp); if (err) return err; opd->lastp = &(*opd->lastp)->next; free (opd->kc_fpr); opd->kc_fpr = NULL; break; case GPGME_STATUS_NO_RECP: /* Should not happen, because we require at least one recipient. */ return gpg_error (GPG_ERR_GENERAL); default: break; } return 0; } static gpgme_error_t encrypt_sym_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_passphrase_status_handler (priv, code, args); return err; } static gpgme_error_t encrypt_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_encrypt_status_handler (priv, code, args); return err; } gpgme_error_t _gpgme_op_encrypt_init_result (gpgme_ctx_t ctx) { gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_ENCRYPT, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; opd->lastp = &opd->result.invalid_recipients; return 0; } static gpgme_error_t encrypt_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; int symmetric = 0; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_encrypt_init_result (ctx); if (err) return err; symmetric = (!recp && !recpstring) || (flags & GPGME_ENCRYPT_SYMMETRIC); if (!plain) return gpg_error (GPG_ERR_NO_DATA); if (!cipher) return gpg_error (GPG_ERR_INV_VALUE); if (recp && !*recp) return gpg_error (GPG_ERR_INV_VALUE); if (symmetric && ctx->passphrase_cb) { /* Symmetric encryption requires a passphrase. */ err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } _gpgme_engine_set_status_handler (ctx->engine, symmetric ? encrypt_sym_status_handler : encrypt_status_handler, ctx); return _gpgme_engine_op_encrypt (ctx->engine, recp, recpstring, flags, plain, cipher, ctx->use_armor); } /* Old version of gpgme_op_encrypt_ext without RECPSTRING. */ gpgme_error_t gpgme_op_encrypt (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { return gpgme_op_encrypt_ext (ctx, recp, NULL, flags, plain, cipher); } /* Old version of gpgme_op_encrypt_ext_start without RECPSTRING. */ gpgme_error_t gpgme_op_encrypt_start (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { return gpgme_op_encrypt_ext_start (ctx, recp, NULL, flags, plain, cipher); } /* Encrypt plaintext PLAIN within CTX for the recipients RECP and * store the resulting ciphertext in CIPHER. RECPSTRING can be used * instead of the RECP array to directly specify recipients as LF * delimited strings; these may be any kind of recipient specification * patterns as supported by the backend. */ gpgme_error_t gpgme_op_encrypt_ext (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt", ctx, "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && (recp || recpstring)) { if (recp) { int i = 0; while (recp[i]) { TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], (recp[i]->subkeys && recp[i]->subkeys->fpr) ? recp[i]->subkeys->fpr : "invalid"); i++; } } else { TRACE_LOG1 ("recipients = '%s'", recpstring); } } err = encrypt_start (ctx, 1, recp, recpstring, flags, plain, cipher); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } gpgme_error_t gpgme_op_encrypt_ext_start (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_encrypt_start", ctx, "flags=0x%x, plain=%p, cipher=%p", flags, plain, cipher); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && (recp || recpstring)) { if (recp) { int i = 0; while (recp[i]) { TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], (recp[i]->subkeys && recp[i]->subkeys->fpr) ? recp[i]->subkeys->fpr : "invalid"); i++; } } else { TRACE_LOG1 ("recipients = '%s'", recpstring); } } err = encrypt_start (ctx, 0, recp, recpstring, flags, plain, cipher); return TRACE_ERR (err); } diff --git a/src/engine-assuan.c b/src/engine-assuan.c index 6e603d9d..83c0ca9f 100644 --- a/src/engine-assuan.c +++ b/src/engine-assuan.c @@ -1,843 +1,844 @@ /* engine-assuan.c - Low-level Assuan protocol engine * Copyright (C) 2009 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ /* Note: This engine requires a modern Assuan server which uses gpg-error codes. In particular there is no backward compatible mapping of old Assuan error codes implemented. */ #if HAVE_CONFIG_H #include #endif #include #include #ifdef HAVE_SYS_TYPES_H # include #endif #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_LOCALE_H #include #endif #include #include "gpgme.h" #include "util.h" #include "ops.h" #include "wait.h" #include "priv-io.h" #include "sema.h" #include "assuan.h" #include "debug.h" #include "engine-backend.h" typedef struct { int fd; /* FD we talk about. */ int server_fd;/* Server FD for this connection. */ int dir; /* Inbound/Outbound, maybe given implicit? */ void *data; /* Handler-specific data. */ void *tag; /* ID from the user for gpgme_remove_io_callback. */ } iocb_data_t; /* Engine instance data. */ struct engine_llass { assuan_context_t assuan_ctx; int lc_ctype_set; int lc_messages_set; iocb_data_t status_cb; struct gpgme_io_cbs io_cbs; /* Hack for old opassuan.c interface, see there the result struct. */ gpg_error_t last_op_err; /* User provided callbacks. */ struct { gpgme_assuan_data_cb_t data_cb; void *data_cb_value; gpgme_assuan_inquire_cb_t inq_cb; void *inq_cb_value; gpgme_assuan_status_cb_t status_cb; void *status_cb_value; } user; /* Option flags. */ struct { int gpg_agent:1; /* Assume this is a gpg-agent connection. */ } opt; char request_origin[10]; /* Copy from the CTX. */ }; typedef struct engine_llass *engine_llass_t; gpg_error_t _gpgme_engine_assuan_last_op_err (void *engine) { engine_llass_t llass = engine; return llass->last_op_err; } /* Prototypes. */ static void llass_io_event (void *engine, gpgme_event_io_t type, void *type_data); /* return the default home directory. */ static const char * llass_get_home_dir (void) { /* For this engine the home directory is not a filename but a string used to convey options. The exclamation mark is a marker to show that this is not a directory name. Options are strings delimited by a space. The only option defined for now is GPG_AGENT to enable GPG_AGENT specific commands to send to the server at connection startup. */ return "!GPG_AGENT"; } static char * llass_get_version (const char *file_name) { (void)file_name; return NULL; } static const char * llass_get_req_version (void) { return NULL; } static void close_notify_handler (int fd, void *opaque) { engine_llass_t llass = opaque; assert (fd != -1); if (llass->status_cb.fd == fd) { if (llass->status_cb.tag) llass->io_cbs.remove (llass->status_cb.tag); llass->status_cb.fd = -1; llass->status_cb.tag = NULL; } } static gpgme_error_t llass_cancel (void *engine) { engine_llass_t llass = engine; if (!llass) return gpg_error (GPG_ERR_INV_VALUE); if (llass->status_cb.fd != -1) _gpgme_io_close (llass->status_cb.fd); if (llass->assuan_ctx) { assuan_release (llass->assuan_ctx); llass->assuan_ctx = NULL; } return 0; } static gpgme_error_t llass_cancel_op (void *engine) { engine_llass_t llass = engine; if (!llass) return gpg_error (GPG_ERR_INV_VALUE); if (llass->status_cb.fd != -1) _gpgme_io_close (llass->status_cb.fd); return 0; } static void llass_release (void *engine) { engine_llass_t llass = engine; if (!llass) return; llass_cancel (engine); free (llass); } /* Create a new instance. If HOME_DIR is NULL standard options for use with gpg-agent are issued. */ static gpgme_error_t llass_new (void **engine, const char *file_name, const char *home_dir, const char *version) { gpgme_error_t err = 0; engine_llass_t llass; char *optstr; char *env_tty = NULL; (void)version; /* Not yet used. */ llass = calloc (1, sizeof *llass); if (!llass) return gpg_error_from_syserror (); llass->status_cb.fd = -1; llass->status_cb.dir = 1; llass->status_cb.tag = 0; llass->status_cb.data = llass; /* Parse_options. */ if (home_dir && *home_dir == '!') { home_dir++; /* Very simple parser only working for the one option we support. */ /* Note that wk promised to write a regression test if this parser will be extended. */ if (!strncmp (home_dir, "GPG_AGENT", 9) && (!home_dir[9] || home_dir[9] == ' ')) llass->opt.gpg_agent = 1; } err = assuan_new_ext (&llass->assuan_ctx, GPG_ERR_SOURCE_GPGME, &_gpgme_assuan_malloc_hooks, _gpgme_assuan_log_cb, NULL); if (err) goto leave; assuan_ctx_set_system_hooks (llass->assuan_ctx, &_gpgme_assuan_system_hooks); assuan_set_flag (llass->assuan_ctx, ASSUAN_CONVEY_COMMENTS, 1); err = assuan_socket_connect (llass->assuan_ctx, file_name, 0, 0); if (err) goto leave; if (llass->opt.gpg_agent) { char *dft_display = NULL; err = _gpgme_getenv ("DISPLAY", &dft_display); if (err) goto leave; if (dft_display) { if (gpgrt_asprintf (&optstr, "OPTION display=%s", dft_display) < 0) { err = gpg_error_from_syserror (); free (dft_display); goto leave; } free (dft_display); err = assuan_transact (llass->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } } if (llass->opt.gpg_agent) err = _gpgme_getenv ("GPG_TTY", &env_tty); if (llass->opt.gpg_agent && (isatty (1) || env_tty || err)) { int rc = 0; char dft_ttyname[64]; char *dft_ttytype = NULL; if (err) goto leave; else if (env_tty) { snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); free (env_tty); } else rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ if (!rc) { if (gpgrt_asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) { err = gpg_error_from_syserror (); goto leave; } err = assuan_transact (llass->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; err = _gpgme_getenv ("TERM", &dft_ttytype); if (err) goto leave; if (dft_ttytype) { if (gpgrt_asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype)< 0) { err = gpg_error_from_syserror (); free (dft_ttytype); goto leave; } free (dft_ttytype); err = assuan_transact (llass->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } } } #ifdef HAVE_W32_SYSTEM /* Under Windows we need to use AllowSetForegroundWindow. Tell llass to tell us when it needs it. */ if (!err && llass->opt.gpg_agent) { err = assuan_transact (llass->assuan_ctx, "OPTION allow-pinentry-notify", NULL, NULL, NULL, NULL, NULL, NULL); if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION) err = 0; /* This work only with recent gpg-agents. */ } #endif /*HAVE_W32_SYSTEM*/ leave: /* Close the server ends of the pipes (because of this, we must use the stored server_fd_str in the function start). Our ends are closed in llass_release(). */ if (err) llass_release (llass); else *engine = llass; return err; } /* Copy flags from CTX into the engine object. */ static void llass_set_engine_flags (void *engine, const gpgme_ctx_t ctx) { engine_llass_t llass = engine; if (ctx->request_origin) { if (strlen (ctx->request_origin) + 1 > sizeof llass->request_origin) strcpy (llass->request_origin, "xxx"); /* Too long - force error */ else strcpy (llass->request_origin, ctx->request_origin); } else *llass->request_origin = 0; } static gpgme_error_t llass_set_locale (void *engine, int category, const char *value) { gpgme_error_t err; engine_llass_t llass = engine; char *optstr; const char *catstr; if (!llass->opt.gpg_agent) return 0; /* FIXME: If value is NULL, we need to reset the option to default. But we can't do this. So we error out here. gpg-agent needs support for this. */ if (0) ; #ifdef LC_CTYPE else if (category == LC_CTYPE) { catstr = "lc-ctype"; if (!value && llass->lc_ctype_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) llass->lc_ctype_set = 1; } #endif #ifdef LC_MESSAGES else if (category == LC_MESSAGES) { catstr = "lc-messages"; if (!value && llass->lc_messages_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) llass->lc_messages_set = 1; } #endif /* LC_MESSAGES */ else return gpg_error (GPG_ERR_INV_VALUE); /* FIXME: Reset value to default. */ if (!value) return 0; if (gpgrt_asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) err = gpg_error_from_syserror (); else { err = assuan_transact (llass->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); } return err; } /* This is the inquiry callback. It handles stuff which ee need to handle here and passes everything on to the user callback. */ static gpgme_error_t inquire_cb (engine_llass_t llass, const char *keyword, const char *args) { gpg_error_t err; if (llass->opt.gpg_agent && !strcmp (keyword, "PINENTRY_LAUNCHED")) { _gpgme_allow_set_foreground_window ((pid_t)strtoul (args, NULL, 10)); } if (llass->user.inq_cb) { gpgme_data_t data = NULL; err = llass->user.inq_cb (llass->user.inq_cb_value, keyword, args, &data); if (!err && data) { /* FIXME: Returning data is not yet implemented. However we need to allow the caller to cleanup his data object. Thus we run the callback in finish mode immediately. */ err = llass->user.inq_cb (llass->user.inq_cb_value, NULL, NULL, &data); } } else err = 0; return err; } static gpgme_error_t llass_status_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_llass_t llass = (engine_llass_t) data->handler_value; gpgme_error_t err = 0; char *line; size_t linelen; do { err = assuan_read_line (llass->assuan_ctx, &line, &linelen); if (err) { /* Reading a full line may not be possible when communicating over a socket in nonblocking mode. In this case, we are done for now. */ if (gpg_err_code (err) == GPG_ERR_EAGAIN) { TRACE1 (DEBUG_CTX, "gpgme:llass_status_handler", llass, "fd 0x%x: EAGAIN reading assuan line (ignored)", fd); err = 0; continue; } TRACE2 (DEBUG_CTX, "gpgme:llass_status_handler", llass, "fd 0x%x: error reading assuan line: %s", fd, gpg_strerror (err)); } else if (linelen >= 2 && line[0] == 'D' && line[1] == ' ') { char *src = line + 2; char *end = line + linelen; char *dst = src; linelen = 0; while (src < end) { if (*src == '%' && src + 2 < end) { /* Handle escaped characters. */ ++src; *dst++ = _gpgme_hextobyte (src); src += 2; } else *dst++ = *src++; linelen++; } src = line + 2; if (linelen && llass->user.data_cb) err = llass->user.data_cb (llass->user.data_cb_value, src, linelen); TRACE2 (DEBUG_CTX, "gpgme:llass_status_handler", llass, "fd 0x%x: D inlinedata; status from cb: %s", fd, (llass->user.data_cb ? (err? gpg_strerror (err):"ok"):"no callback")); } else if (linelen >= 3 && line[0] == 'E' && line[1] == 'N' && line[2] == 'D' && (line[3] == '\0' || line[3] == ' ')) { /* END received. Tell the data callback. */ if (llass->user.data_cb) err = llass->user.data_cb (llass->user.data_cb_value, NULL, 0); TRACE2 (DEBUG_CTX, "gpgme:llass_status_handler", llass, "fd 0x%x: END line; status from cb: %s", fd, (llass->user.data_cb ? (err? gpg_strerror (err):"ok"):"no callback")); } else if (linelen > 2 && line[0] == 'S' && line[1] == ' ') { char *args; char *src; for (src=line+2; *src == ' '; src++) ; args = strchr (src, ' '); if (!args) args = line + linelen; /* Let it point to an empty string. */ else *(args++) = 0; while (*args == ' ') args++; if (llass->user.status_cb) err = llass->user.status_cb (llass->user.status_cb_value, src, args); TRACE3 (DEBUG_CTX, "gpgme:llass_status_handler", llass, "fd 0x%x: S line (%s) - status from cb: %s", fd, line+2, (llass->user.status_cb ? (err? gpg_strerror (err):"ok"):"no callback")); } else if (linelen >= 7 && line[0] == 'I' && line[1] == 'N' && line[2] == 'Q' && line[3] == 'U' && line[4] == 'I' && line[5] == 'R' && line[6] == 'E' && (line[7] == '\0' || line[7] == ' ')) { char *src; char *args; for (src=line+7; *src == ' '; src++) ; args = strchr (src, ' '); if (!args) args = line + linelen; /* Let it point to an empty string. */ else *(args++) = 0; while (*args == ' ') args++; err = inquire_cb (llass, src, args); if (!err) { /* Flush and send END. */ err = assuan_send_data (llass->assuan_ctx, NULL, 0); } else if (gpg_err_code (err) == GPG_ERR_ASS_CANCELED) { /* Flush and send CANcel. */ err = assuan_send_data (llass->assuan_ctx, NULL, 1); } } else if (linelen >= 3 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && (line[3] == '\0' || line[3] == ' ')) { if (line[3] == ' ') err = atoi (line+4); else err = gpg_error (GPG_ERR_GENERAL); TRACE2 (DEBUG_CTX, "gpgme:llass_status_handler", llass, "fd 0x%x: ERR line: %s", fd, err ? gpg_strerror (err) : "ok"); /* Command execution errors are not fatal, as we use a session based protocol. */ data->op_err = err; llass->last_op_err = err; /* The caller will do the rest (namely, call cancel_op, which closes status_fd). */ return 0; } else if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) { TRACE1 (DEBUG_CTX, "gpgme:llass_status_handler", llass, "fd 0x%x: OK line", fd); llass->last_op_err = 0; _gpgme_io_close (llass->status_cb.fd); return 0; } else { /* Comment line or invalid line. */ } } while (!err && assuan_pending_line (llass->assuan_ctx)); return err; } static gpgme_error_t add_io_cb (engine_llass_t llass, iocb_data_t *iocbd, gpgme_io_cb_t handler) { gpgme_error_t err; TRACE_BEG2 (DEBUG_ENGINE, "engine-assuan:add_io_cb", llass, "fd %d, dir %d", iocbd->fd, iocbd->dir); err = (*llass->io_cbs.add) (llass->io_cbs.add_priv, iocbd->fd, iocbd->dir, handler, iocbd->data, &iocbd->tag); if (err) return TRACE_ERR (err); if (!iocbd->dir) /* FIXME Kludge around poll() problem. */ err = _gpgme_io_set_nonblocking (iocbd->fd); return TRACE_ERR (err); } static gpgme_error_t start (engine_llass_t llass, const char *command) { gpgme_error_t err; assuan_fd_t afdlist[5]; int fdlist[5]; int nfds; int i; if (*llass->request_origin && llass->opt.gpg_agent) { char *cmd; cmd = _gpgme_strconcat ("OPTION pretend-request-origin=", llass->request_origin, NULL); if (!cmd) return gpg_error_from_syserror (); err = assuan_transact (llass->assuan_ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL); free (cmd); if (err && gpg_err_code (err) != GPG_ERR_UNKNOWN_OPTION) return err; } /* We need to know the fd used by assuan for reads. We do this by using the assumption that the first returned fd from assuan_get_active_fds() is always this one. */ nfds = assuan_get_active_fds (llass->assuan_ctx, 0 /* read fds */, afdlist, DIM (afdlist)); if (nfds < 1) return gpg_error (GPG_ERR_GENERAL); /* FIXME */ /* For now... */ for (i = 0; i < nfds; i++) fdlist[i] = (int) afdlist[i]; /* We "duplicate" the file descriptor, so we can close it here (we can't close fdlist[0], as that is closed by libassuan, and closing it here might cause libassuan to close some unrelated FD later). Alternatively, we could special case status_fd and register/unregister it manually as needed, but this increases code duplication and is more complicated as we can not use the close notifications etc. A third alternative would be to let Assuan know that we closed the FD, but that complicates the Assuan interface. */ llass->status_cb.fd = _gpgme_io_dup (fdlist[0]); if (llass->status_cb.fd < 0) return gpg_error_from_syserror (); if (_gpgme_io_set_close_notify (llass->status_cb.fd, close_notify_handler, llass)) { _gpgme_io_close (llass->status_cb.fd); llass->status_cb.fd = -1; return gpg_error (GPG_ERR_GENERAL); } err = add_io_cb (llass, &llass->status_cb, llass_status_handler); if (!err) err = assuan_write_line (llass->assuan_ctx, command); /* FIXME: If *command == '#' no answer is expected. */ if (!err) llass_io_event (llass, GPGME_EVENT_START, NULL); return err; } static gpgme_error_t llass_transact (void *engine, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { engine_llass_t llass = engine; gpgme_error_t err; if (!llass || !command || !*command) return gpg_error (GPG_ERR_INV_VALUE); llass->user.data_cb = data_cb; llass->user.data_cb_value = data_cb_value; llass->user.inq_cb = inq_cb; llass->user.inq_cb_value = inq_cb_value; llass->user.status_cb = status_cb; llass->user.status_cb_value = status_cb_value; err = start (llass, command); return err; } static void llass_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs) { engine_llass_t llass = engine; llass->io_cbs = *io_cbs; } static void llass_io_event (void *engine, gpgme_event_io_t type, void *type_data) { engine_llass_t llass = engine; TRACE3 (DEBUG_ENGINE, "gpgme:llass_io_event", llass, "event %p, type %d, type_data %p", llass->io_cbs.event, type, type_data); if (llass->io_cbs.event) (*llass->io_cbs.event) (llass->io_cbs.event_priv, type, type_data); } struct engine_ops _gpgme_engine_ops_assuan = { /* Static functions. */ _gpgme_get_default_agent_socket, llass_get_home_dir, llass_get_version, llass_get_req_version, llass_new, /* Member functions. */ llass_release, NULL, /* reset */ NULL, /* set_status_cb */ NULL, /* set_status_handler */ NULL, /* set_command_handler */ NULL, /* set_colon_line_handler */ llass_set_locale, NULL, /* set_protocol */ llass_set_engine_flags, NULL, /* decrypt */ NULL, /* delete */ NULL, /* edit */ NULL, /* encrypt */ NULL, /* encrypt_sign */ NULL, /* export */ NULL, /* export_ext */ NULL, /* genkey */ NULL, /* import */ NULL, /* keylist */ NULL, /* keylist_ext */ NULL, /* keylist_data */ NULL, /* keysign */ NULL, /* tofu_policy */ NULL, /* sign */ NULL, /* trustlist */ NULL, /* verify */ NULL, /* getauditlog */ llass_transact, /* opassuan_transact */ NULL, /* conf_load */ NULL, /* conf_save */ NULL, /* conf_dir */ NULL, /* query_swdb */ llass_set_io_cbs, llass_io_event, llass_cancel, llass_cancel_op, NULL, /* passwd */ NULL, /* set_pinentry_mode */ NULL /* opspawn */ }; diff --git a/src/engine-g13.c b/src/engine-g13.c index ec2f7af4..90f8e7ea 100644 --- a/src/engine-g13.c +++ b/src/engine-g13.c @@ -1,824 +1,824 @@ /* engine-g13.c - G13 engine. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #ifdef HAVE_SYS_TYPES_H # include #endif #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_LOCALE_H #include #endif #include /* FIXME */ #include #include "gpgme.h" #include "util.h" #include "ops.h" #include "wait.h" #include "priv-io.h" #include "sema.h" #include "assuan.h" #include "debug.h" #include "engine-backend.h" typedef struct { int fd; /* FD we talk about. */ int server_fd;/* Server FD for this connection. */ int dir; /* Inbound/Outbound, maybe given implicit? */ void *data; /* Handler-specific data. */ void *tag; /* ID from the user for gpgme_remove_io_callback. */ char server_fd_str[15]; /* Same as SERVER_FD but as a string. We need this because _gpgme_io_fd2str can't be used on a closed descriptor. */ } iocb_data_t; struct engine_g13 { assuan_context_t assuan_ctx; int lc_ctype_set; int lc_messages_set; iocb_data_t status_cb; struct gpgme_io_cbs io_cbs; /* User provided callbacks. */ struct { gpgme_assuan_data_cb_t data_cb; void *data_cb_value; gpgme_assuan_inquire_cb_t inq_cb; void *inq_cb_value; gpgme_assuan_status_cb_t status_cb; void *status_cb_value; } user; }; typedef struct engine_g13 *engine_g13_t; static void g13_io_event (void *engine, gpgme_event_io_t type, void *type_data); static char * g13_get_version (const char *file_name) { return _gpgme_get_program_version (file_name ? file_name : _gpgme_get_default_g13_name ()); } static const char * g13_get_req_version (void) { return "2.1.0"; } static void close_notify_handler (int fd, void *opaque) { engine_g13_t g13 = opaque; assert (fd != -1); if (g13->status_cb.fd == fd) { if (g13->status_cb.tag) (*g13->io_cbs.remove) (g13->status_cb.tag); g13->status_cb.fd = -1; g13->status_cb.tag = NULL; } } /* This is the default inquiry callback. We use it to handle the Pinentry notifications. */ static gpgme_error_t default_inq_cb (engine_g13_t g13, const char *keyword, const char *args) { gpg_error_t err; if (!strcmp (keyword, "PINENTRY_LAUNCHED")) { _gpgme_allow_set_foreground_window ((pid_t)strtoul (args, NULL, 10)); } if (g13->user.inq_cb) { gpgme_data_t data = NULL; err = g13->user.inq_cb (g13->user.inq_cb_value, keyword, args, &data); if (!err && data) { /* FIXME: Returning data is not yet implemented. However we need to allow the caller to cleanup his data object. Thus we run the callback in finish mode immediately. */ err = g13->user.inq_cb (g13->user.inq_cb_value, NULL, NULL, &data); } } else err = 0; return err; } static gpgme_error_t g13_cancel (void *engine) { engine_g13_t g13 = engine; if (!g13) return gpg_error (GPG_ERR_INV_VALUE); if (g13->status_cb.fd != -1) _gpgme_io_close (g13->status_cb.fd); if (g13->assuan_ctx) { assuan_release (g13->assuan_ctx); g13->assuan_ctx = NULL; } return 0; } static gpgme_error_t g13_cancel_op (void *engine) { engine_g13_t g13 = engine; if (!g13) return gpg_error (GPG_ERR_INV_VALUE); if (g13->status_cb.fd != -1) _gpgme_io_close (g13->status_cb.fd); return 0; } static void g13_release (void *engine) { engine_g13_t g13 = engine; if (!g13) return; g13_cancel (engine); free (g13); } static gpgme_error_t g13_new (void **engine, const char *file_name, const char *home_dir, const char *version) { gpgme_error_t err = 0; engine_g13_t g13; const char *pgmname; int argc; const char *argv[5]; char *dft_display = NULL; char dft_ttyname[64]; char *env_tty = NULL; char *dft_ttytype = NULL; char *optstr; (void)version; /* Not yet used. */ g13 = calloc (1, sizeof *g13); if (!g13) return gpg_error_from_syserror (); g13->status_cb.fd = -1; g13->status_cb.dir = 1; g13->status_cb.tag = 0; g13->status_cb.data = g13; pgmname = file_name ? file_name : _gpgme_get_default_g13_name (); argc = 0; argv[argc++] = _gpgme_get_basename (pgmname); if (home_dir) { argv[argc++] = "--homedir"; argv[argc++] = home_dir; } argv[argc++] = "--server"; argv[argc++] = NULL; err = assuan_new_ext (&g13->assuan_ctx, GPG_ERR_SOURCE_GPGME, &_gpgme_assuan_malloc_hooks, _gpgme_assuan_log_cb, NULL); if (err) goto leave; assuan_ctx_set_system_hooks (g13->assuan_ctx, &_gpgme_assuan_system_hooks); #if USE_DESCRIPTOR_PASSING err = assuan_pipe_connect (g13->assuan_ctx, pgmname, argv, NULL, NULL, NULL, ASSUAN_PIPE_CONNECT_FDPASSING); #else err = assuan_pipe_connect (g13->assuan_ctx, pgmname, argv, NULL, NULL, NULL, 0); #endif if (err) goto leave; err = _gpgme_getenv ("DISPLAY", &dft_display); if (err) goto leave; if (dft_display) { if (gpgrt_asprintf (&optstr, "OPTION display=%s", dft_display) < 0) { free (dft_display); err = gpg_error_from_syserror (); goto leave; } free (dft_display); err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } err = _gpgme_getenv ("GPG_TTY", &env_tty); if (isatty (1) || env_tty || err) { int rc = 0; if (err) goto leave; else if (env_tty) { snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); free (env_tty); } else rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ if (!rc) { if (gpgrt_asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) { err = gpg_error_from_syserror (); goto leave; } err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; err = _gpgme_getenv ("TERM", &dft_ttytype); if (err) goto leave; if (dft_ttytype) { if (gpgrt_asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype)< 0) { free (dft_ttytype); err = gpg_error_from_syserror (); goto leave; } free (dft_ttytype); err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } } } #ifdef HAVE_W32_SYSTEM /* Under Windows we need to use AllowSetForegroundWindow. Tell g13 to tell us when it needs it. */ if (!err) { err = assuan_transact (g13->assuan_ctx, "OPTION allow-pinentry-notify", NULL, NULL, NULL, NULL, NULL, NULL); if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION) err = 0; /* This is a new feature of g13. */ } #endif /*HAVE_W32_SYSTEM*/ leave: if (err) g13_release (g13); else *engine = g13; return err; } static gpgme_error_t g13_set_locale (void *engine, int category, const char *value) { engine_g13_t g13 = engine; gpgme_error_t err; char *optstr; const char *catstr; /* FIXME: If value is NULL, we need to reset the option to default. But we can't do this. So we error out here. G13 needs support for this. */ if (0) ; #ifdef LC_CTYPE else if (category == LC_CTYPE) { catstr = "lc-ctype"; if (!value && g13->lc_ctype_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) g13->lc_ctype_set = 1; } #endif #ifdef LC_MESSAGES else if (category == LC_MESSAGES) { catstr = "lc-messages"; if (!value && g13->lc_messages_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) g13->lc_messages_set = 1; } #endif /* LC_MESSAGES */ else return gpg_error (GPG_ERR_INV_VALUE); /* FIXME: Reset value to default. */ if (!value) return 0; if (gpgrt_asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) err = gpg_error_from_syserror (); else { err = assuan_transact (g13->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); } return err; } #if USE_DESCRIPTOR_PASSING static gpgme_error_t g13_assuan_simple_command (assuan_context_t ctx, const char *cmd, engine_status_handler_t status_fnc, void *status_fnc_value) { gpg_error_t err; char *line; size_t linelen; (void)status_fnc; (void)status_fnc_value; err = assuan_write_line (ctx, cmd); if (err) return err; do { err = assuan_read_line (ctx, &line, &linelen); if (err) return err; if (*line == '#' || !linelen) continue; if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) return 0; else if (linelen >= 4 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && line[3] == ' ') err = atoi (&line[4]); else if (linelen >= 2 && line[0] == 'S' && line[1] == ' ') { char *rest; rest = strchr (line + 2, ' '); if (!rest) rest = line + linelen; /* set to an empty string */ else *(rest++) = 0; /* Nothing to do with status lines. */ } else err = gpg_error (GPG_ERR_GENERAL); } while (!err); return err; } #endif static gpgme_error_t status_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_g13_t g13 = (engine_g13_t) data->handler_value; gpgme_error_t err = 0; char *line; size_t linelen; do { err = assuan_read_line (g13->assuan_ctx, &line, &linelen); if (err) { /* Try our best to terminate the connection friendly. */ /* assuan_write_line (g13->assuan_ctx, "BYE"); */ TRACE2 (DEBUG_CTX, "gpgme:status_handler", g13, "fd 0x%x: error reading assuan line: %s", fd, gpg_strerror (err)); } else if (linelen >= 3 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && (line[3] == '\0' || line[3] == ' ')) { if (line[3] == ' ') err = atoi (&line[4]); if (! err) err = gpg_error (GPG_ERR_GENERAL); TRACE2 (DEBUG_CTX, "gpgme:status_handler", g13, "fd 0x%x: ERR line: %s", fd, err ? gpg_strerror (err) : "ok"); /* Command execution errors are not fatal, as we use a session based protocol. */ data->op_err = err; /* The caller will do the rest (namely, call cancel_op, which closes status_fd). */ return 0; } else if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) { TRACE1 (DEBUG_CTX, "gpgme:status_handler", g13, "fd 0x%x: OK line", fd); _gpgme_io_close (g13->status_cb.fd); return 0; } else if (linelen > 2 && line[0] == 'D' && line[1] == ' ') { /* We are using the colon handler even for plain inline data - strange name for that function but for historic reasons we keep it. */ /* FIXME We can't use this for binary data because we assume this is a string. For the current usage of colon output it is correct. */ char *src = line + 2; char *end = line + linelen; char *dst = src; linelen = 0; while (src < end) { if (*src == '%' && src + 2 < end) { /* Handle escaped characters. */ ++src; *dst++ = _gpgme_hextobyte (src); src += 2; } else *dst++ = *src++; linelen++; } src = line + 2; if (linelen && g13->user.data_cb) err = g13->user.data_cb (g13->user.data_cb_value, src, linelen); else err = 0; TRACE2 (DEBUG_CTX, "gpgme:g13_status_handler", g13, "fd 0x%x: D inlinedata; status from cb: %s", fd, (g13->user.data_cb ? (err? gpg_strerror (err):"ok"):"no callback")); } else if (linelen > 2 && line[0] == 'S' && line[1] == ' ') { char *src; char *args; src = line + 2; while (*src == ' ') src++; args = strchr (line + 2, ' '); if (!args) args = line + linelen; /* set to an empty string */ else *(args++) = 0; while (*args == ' ') args++; if (g13->user.status_cb) err = g13->user.status_cb (g13->user.status_cb_value, src, args); else err = 0; TRACE3 (DEBUG_CTX, "gpgme:g13_status_handler", g13, "fd 0x%x: S line (%s) - status from cb: %s", fd, line+2, (g13->user.status_cb ? (err? gpg_strerror (err):"ok"):"no callback")); } else if (linelen >= 7 && line[0] == 'I' && line[1] == 'N' && line[2] == 'Q' && line[3] == 'U' && line[4] == 'I' && line[5] == 'R' && line[6] == 'E' && (line[7] == '\0' || line[7] == ' ')) { char *src; char *args; for (src=line+7; *src == ' '; src++) ; args = strchr (src, ' '); if (!args) args = line + linelen; /* Let it point to an empty string. */ else *(args++) = 0; while (*args == ' ') args++; err = default_inq_cb (g13, src, args); if (!err) { /* Flush and send END. */ err = assuan_send_data (g13->assuan_ctx, NULL, 0); } else if (gpg_err_code (err) == GPG_ERR_ASS_CANCELED) { /* Flush and send CANcel. */ err = assuan_send_data (g13->assuan_ctx, NULL, 1); } assuan_write_line (g13->assuan_ctx, "END"); } } while (!err && assuan_pending_line (g13->assuan_ctx)); return err; } static gpgme_error_t add_io_cb (engine_g13_t g13, iocb_data_t *iocbd, gpgme_io_cb_t handler) { gpgme_error_t err; TRACE_BEG2 (DEBUG_ENGINE, "engine-g13:add_io_cb", g13, "fd %d, dir %d", iocbd->fd, iocbd->dir); err = (*g13->io_cbs.add) (g13->io_cbs.add_priv, iocbd->fd, iocbd->dir, handler, iocbd->data, &iocbd->tag); if (err) return TRACE_ERR (err); if (!iocbd->dir) /* FIXME Kludge around poll() problem. */ err = _gpgme_io_set_nonblocking (iocbd->fd); return TRACE_ERR (err); } static gpgme_error_t start (engine_g13_t g13, const char *command) { gpgme_error_t err; assuan_fd_t afdlist[5]; int fdlist[5]; int nfds; int i; /* We need to know the fd used by assuan for reads. We do this by using the assumption that the first returned fd from assuan_get_active_fds() is always this one. */ nfds = assuan_get_active_fds (g13->assuan_ctx, 0 /* read fds */, afdlist, DIM (afdlist)); if (nfds < 1) return gpg_error (GPG_ERR_GENERAL); /* FIXME */ /* For now... */ for (i = 0; i < nfds; i++) fdlist[i] = (int) afdlist[i]; /* We "duplicate" the file descriptor, so we can close it here (we can't close fdlist[0], as that is closed by libassuan, and closing it here might cause libassuan to close some unrelated FD later). Alternatively, we could special case status_fd and register/unregister it manually as needed, but this increases code duplication and is more complicated as we can not use the close notifications etc. A third alternative would be to let Assuan know that we closed the FD, but that complicates the Assuan interface. */ g13->status_cb.fd = _gpgme_io_dup (fdlist[0]); if (g13->status_cb.fd < 0) return gpg_error_from_syserror (); if (_gpgme_io_set_close_notify (g13->status_cb.fd, close_notify_handler, g13)) { _gpgme_io_close (g13->status_cb.fd); g13->status_cb.fd = -1; return gpg_error (GPG_ERR_GENERAL); } err = add_io_cb (g13, &g13->status_cb, status_handler); if (!err) err = assuan_write_line (g13->assuan_ctx, command); if (!err) g13_io_event (g13, GPGME_EVENT_START, NULL); return err; } #if USE_DESCRIPTOR_PASSING static gpgme_error_t g13_reset (void *engine) { engine_g13_t g13 = engine; /* We must send a reset because we need to reset the list of signers. Note that RESET does not reset OPTION commands. */ return g13_assuan_simple_command (g13->assuan_ctx, "RESET", NULL, NULL); } #endif static gpgme_error_t g13_transact (void *engine, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { engine_g13_t g13 = engine; gpgme_error_t err; if (!g13 || !command || !*command) return gpg_error (GPG_ERR_INV_VALUE); g13->user.data_cb = data_cb; g13->user.data_cb_value = data_cb_value; g13->user.inq_cb = inq_cb; g13->user.inq_cb_value = inq_cb_value; g13->user.status_cb = status_cb; g13->user.status_cb_value = status_cb_value; err = start (g13, command); return err; } static void g13_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs) { engine_g13_t g13 = engine; g13->io_cbs = *io_cbs; } static void g13_io_event (void *engine, gpgme_event_io_t type, void *type_data) { engine_g13_t g13 = engine; TRACE3 (DEBUG_ENGINE, "gpgme:g13_io_event", g13, "event %p, type %d, type_data %p", g13->io_cbs.event, type, type_data); if (g13->io_cbs.event) (*g13->io_cbs.event) (g13->io_cbs.event_priv, type, type_data); } struct engine_ops _gpgme_engine_ops_g13 = { /* Static functions. */ _gpgme_get_default_g13_name, NULL, g13_get_version, g13_get_req_version, g13_new, /* Member functions. */ g13_release, #if USE_DESCRIPTOR_PASSING g13_reset, #else NULL, /* reset */ #endif NULL, /* set_status_cb */ NULL, /* set_status_handler */ NULL, /* set_command_handler */ NULL, /* set_colon_line_handler */ g13_set_locale, NULL, /* set_protocol */ NULL, /* set_engine_flags */ NULL, /* decrypt */ NULL, /* delete */ NULL, /* edit */ NULL, /* encrypt */ NULL, /* encrypt_sign */ NULL, /* export */ NULL, /* export_ext */ NULL, /* genkey */ NULL, /* import */ NULL, /* keylist */ NULL, /* keylist_ext */ NULL, /* keylist_data */ NULL, /* keysign */ NULL, /* tofu_policy */ NULL, /* sign */ NULL, /* trustlist */ NULL, /* verify */ NULL, /* getauditlog */ g13_transact, NULL, /* conf_load */ NULL, /* conf_save */ NULL, /* conf_dir */ NULL, /* query_swdb */ g13_set_io_cbs, g13_io_event, g13_cancel, g13_cancel_op, NULL, /* passwd */ NULL, /* set_pinentry_mode */ NULL /* opspawn */ }; diff --git a/src/engine-gpg.c b/src/engine-gpg.c index 2785bf20..c88e970a 100644 --- a/src/engine-gpg.c +++ b/src/engine-gpg.c @@ -1,3418 +1,3419 @@ /* engine-gpg.c - Gpg Engine. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, - 2009, 2010, 2012, 2013 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, + * 2009, 2010, 2012, 2013 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_LOCALE_H #include #endif #include "gpgme.h" #include "util.h" #include "ops.h" #include "wait.h" #include "context.h" /*temp hack until we have GpmeData methods to do I/O */ #include "priv-io.h" #include "sema.h" #include "debug.h" #include "data.h" #include "mbox-util.h" #include "engine-backend.h" /* This type is used to build a list of gpg arguments and data sources/sinks. */ struct arg_and_data_s { struct arg_and_data_s *next; gpgme_data_t data; /* If this is not NULL, use arg below. */ int inbound; /* True if this is used for reading from gpg. */ int dup_to; int print_fd; /* Print the fd number and not the special form of it. */ int *arg_locp; /* Write back the argv idx of this argument when building command line to this location. */ char arg[1]; /* Used if data above is not used. */ }; struct fd_data_map_s { gpgme_data_t data; int inbound; /* true if this is used for reading from gpg */ int dup_to; int fd; /* the fd to use */ int peer_fd; /* the other side of the pipe */ int arg_loc; /* The index into the argv for translation purposes. */ void *tag; }; /* NB.: R_LINE is allocated an gpgrt function and thus gpgrt_free * shall be used to release it. This takes care of custom memory * allocators and avoids problems on Windows with different runtimes * used for libgpg-error/gpgrt and gpgme. */ typedef gpgme_error_t (*colon_preprocessor_t) (char *line, char **rline); struct engine_gpg { char *file_name; char *version; char *lc_messages; char *lc_ctype; struct arg_and_data_s *arglist; struct arg_and_data_s **argtail; struct { int fd[2]; int arg_loc; size_t bufsize; char *buffer; size_t readpos; int eof; engine_status_handler_t fnc; void *fnc_value; gpgme_status_cb_t mon_cb; void *mon_cb_value; void *tag; } status; /* This is a kludge - see the comment at colon_line_handler. */ struct { int fd[2]; int arg_loc; size_t bufsize; char *buffer; size_t readpos; int eof; engine_colon_line_handler_t fnc; /* this indicate use of this structrue */ void *fnc_value; void *tag; colon_preprocessor_t preprocess_fnc; } colon; char **argv; struct fd_data_map_s *fd_data_map; /* stuff needed for interactive (command) mode */ struct { int used; int fd; void *cb_data; int idx; /* Index in fd_data_map */ gpgme_status_code_t code; /* last code */ char *keyword; /* what has been requested (malloced) */ engine_command_handler_t fnc; void *fnc_value; } cmd; struct gpgme_io_cbs io_cbs; gpgme_pinentry_mode_t pinentry_mode; char request_origin[10]; char *auto_key_locate; char *trust_model; struct { unsigned int no_symkey_cache : 1; unsigned int offline : 1; unsigned int ignore_mdc_error : 1; } flags; /* NULL or the data object fed to --override_session_key-fd. */ gpgme_data_t override_session_key; /* Memory data containing diagnostics (--logger-fd) of gpg */ gpgme_data_t diagnostics; }; typedef struct engine_gpg *engine_gpg_t; static void gpg_io_event (void *engine, gpgme_event_io_t type, void *type_data) { engine_gpg_t gpg = engine; TRACE3 (DEBUG_ENGINE, "gpgme:gpg_io_event", gpg, "event %p, type %d, type_data %p", gpg->io_cbs.event, type, type_data); if (gpg->io_cbs.event) (*gpg->io_cbs.event) (gpg->io_cbs.event_priv, type, type_data); } static void close_notify_handler (int fd, void *opaque) { engine_gpg_t gpg = opaque; assert (fd != -1); if (gpg->status.fd[0] == fd) { if (gpg->status.tag) (*gpg->io_cbs.remove) (gpg->status.tag); gpg->status.fd[0] = -1; } else if (gpg->status.fd[1] == fd) gpg->status.fd[1] = -1; else if (gpg->colon.fd[0] == fd) { if (gpg->colon.tag) (*gpg->io_cbs.remove) (gpg->colon.tag); gpg->colon.fd[0] = -1; } else if (gpg->colon.fd[1] == fd) gpg->colon.fd[1] = -1; else if (gpg->cmd.fd == fd) gpg->cmd.fd = -1; else if (gpg->fd_data_map) { int i; for (i = 0; gpg->fd_data_map[i].data; i++) { if (gpg->fd_data_map[i].fd == fd) { if (gpg->fd_data_map[i].tag) (*gpg->io_cbs.remove) (gpg->fd_data_map[i].tag); gpg->fd_data_map[i].fd = -1; break; } if (gpg->fd_data_map[i].peer_fd == fd) { gpg->fd_data_map[i].peer_fd = -1; break; } } } } /* If FRONT is true, push at the front of the list. Use this for options added late in the process. */ static gpgme_error_t _add_arg (engine_gpg_t gpg, const char *prefix, const char *arg, size_t arglen, int front, int *arg_locp) { struct arg_and_data_s *a; size_t prefixlen = prefix? strlen (prefix) : 0; assert (gpg); assert (arg); a = malloc (sizeof *a + prefixlen + arglen); if (!a) return gpg_error_from_syserror (); a->data = NULL; a->dup_to = -1; a->arg_locp = arg_locp; if (prefixlen) memcpy (a->arg, prefix, prefixlen); memcpy (a->arg + prefixlen, arg, arglen); a->arg[prefixlen + arglen] = 0; if (front) { a->next = gpg->arglist; if (!gpg->arglist) { /* If this is the first argument, we need to update the tail pointer. */ gpg->argtail = &a->next; } gpg->arglist = a; } else { a->next = NULL; *gpg->argtail = a; gpg->argtail = &a->next; } return 0; } static gpgme_error_t add_arg_ext (engine_gpg_t gpg, const char *arg, int front) { return _add_arg (gpg, NULL, arg, strlen (arg), front, NULL); } static gpgme_error_t add_arg_with_locp (engine_gpg_t gpg, const char *arg, int *locp) { return _add_arg (gpg, NULL, arg, strlen (arg), 0, locp); } static gpgme_error_t add_arg (engine_gpg_t gpg, const char *arg) { return _add_arg (gpg, NULL, arg, strlen (arg), 0, NULL); } static gpgme_error_t add_arg_pfx (engine_gpg_t gpg, const char *prefix, const char *arg) { return _add_arg (gpg, prefix, arg, strlen (arg), 0, NULL); } static gpgme_error_t add_arg_len (engine_gpg_t gpg, const char *prefix, const char *arg, size_t arglen) { return _add_arg (gpg, prefix, arg, arglen, 0, NULL); } static gpgme_error_t add_data (engine_gpg_t gpg, gpgme_data_t data, int dup_to, int inbound) { struct arg_and_data_s *a; assert (gpg); assert (data); a = malloc (sizeof *a - 1); if (!a) return gpg_error_from_syserror (); a->next = NULL; a->data = data; a->inbound = inbound; a->arg_locp = NULL; if (dup_to == -2) { a->print_fd = 1; a->dup_to = -1; } else { a->print_fd = 0; a->dup_to = dup_to; } *gpg->argtail = a; gpg->argtail = &a->next; return 0; } /* Return true if the engine's version is at least VERSION. */ static int have_gpg_version (engine_gpg_t gpg, const char *version) { return _gpgme_compare_versions (gpg->version, version); } static char * gpg_get_version (const char *file_name) { return _gpgme_get_program_version (file_name ? file_name : _gpgme_get_default_gpg_name ()); } static const char * gpg_get_req_version (void) { return "1.4.0"; } static void free_argv (char **argv) { int i; for (i = 0; argv[i]; i++) free (argv[i]); free (argv); } static void free_fd_data_map (struct fd_data_map_s *fd_data_map) { int i; if (!fd_data_map) return; for (i = 0; fd_data_map[i].data; i++) { if (fd_data_map[i].fd != -1) _gpgme_io_close (fd_data_map[i].fd); if (fd_data_map[i].peer_fd != -1) _gpgme_io_close (fd_data_map[i].peer_fd); /* Don't release data because this is only a reference. */ } free (fd_data_map); } static gpgme_error_t gpg_cancel (void *engine) { engine_gpg_t gpg = engine; if (!gpg) return gpg_error (GPG_ERR_INV_VALUE); /* If gpg may be waiting for a cmd, close the cmd fd first. On Windows, close operations block on the reader/writer thread. */ if (gpg->cmd.used) { if (gpg->cmd.fd != -1) _gpgme_io_close (gpg->cmd.fd); else if (gpg->fd_data_map && gpg->fd_data_map[gpg->cmd.idx].fd != -1) _gpgme_io_close (gpg->fd_data_map[gpg->cmd.idx].fd); } if (gpg->status.fd[0] != -1) _gpgme_io_close (gpg->status.fd[0]); if (gpg->status.fd[1] != -1) _gpgme_io_close (gpg->status.fd[1]); if (gpg->colon.fd[0] != -1) _gpgme_io_close (gpg->colon.fd[0]); if (gpg->colon.fd[1] != -1) _gpgme_io_close (gpg->colon.fd[1]); if (gpg->fd_data_map) { free_fd_data_map (gpg->fd_data_map); gpg->fd_data_map = NULL; } return 0; } static void gpg_release (void *engine) { engine_gpg_t gpg = engine; if (!gpg) return; gpg_cancel (engine); if (gpg->file_name) free (gpg->file_name); if (gpg->version) free (gpg->version); if (gpg->lc_messages) free (gpg->lc_messages); if (gpg->lc_ctype) free (gpg->lc_ctype); while (gpg->arglist) { struct arg_and_data_s *next = gpg->arglist->next; free (gpg->arglist); gpg->arglist = next; } if (gpg->status.buffer) free (gpg->status.buffer); if (gpg->colon.buffer) free (gpg->colon.buffer); if (gpg->argv) free_argv (gpg->argv); if (gpg->cmd.keyword) free (gpg->cmd.keyword); free (gpg->auto_key_locate); free (gpg->trust_model); gpgme_data_release (gpg->override_session_key); gpgme_data_release (gpg->diagnostics); free (gpg); } static gpgme_error_t gpg_new (void **engine, const char *file_name, const char *home_dir, const char *version) { engine_gpg_t gpg; gpgme_error_t rc = 0; char *dft_display = NULL; char dft_ttyname[64]; char *dft_ttytype = NULL; char *env_tty = NULL; gpg = calloc (1, sizeof *gpg); if (!gpg) return gpg_error_from_syserror (); if (file_name) { gpg->file_name = strdup (file_name); if (!gpg->file_name) { rc = gpg_error_from_syserror (); goto leave; } } if (version) { gpg->version = strdup (version); if (!gpg->version) { rc = gpg_error_from_syserror (); goto leave; } } gpg->argtail = &gpg->arglist; gpg->status.fd[0] = -1; gpg->status.fd[1] = -1; gpg->colon.fd[0] = -1; gpg->colon.fd[1] = -1; gpg->cmd.fd = -1; gpg->cmd.idx = -1; /* Allocate the read buffer for the status pipe. */ gpg->status.bufsize = 1024; gpg->status.readpos = 0; gpg->status.buffer = malloc (gpg->status.bufsize); if (!gpg->status.buffer) { rc = gpg_error_from_syserror (); goto leave; } /* In any case we need a status pipe - create it right here and don't handle it with our generic gpgme_data_t mechanism. */ if (_gpgme_io_pipe (gpg->status.fd, 1) == -1) { rc = gpg_error_from_syserror (); goto leave; } if (_gpgme_io_set_close_notify (gpg->status.fd[0], close_notify_handler, gpg) || _gpgme_io_set_close_notify (gpg->status.fd[1], close_notify_handler, gpg)) { rc = gpg_error (GPG_ERR_GENERAL); goto leave; } gpg->status.eof = 0; if (home_dir) { rc = add_arg (gpg, "--homedir"); if (!rc) rc = add_arg (gpg, home_dir); if (rc) goto leave; } rc = add_arg (gpg, "--status-fd"); if (rc) goto leave; { char buf[25]; _gpgme_io_fd2str (buf, sizeof (buf), gpg->status.fd[1]); rc = add_arg_with_locp (gpg, buf, &gpg->status.arg_loc); if (rc) goto leave; } rc = add_arg (gpg, "--no-tty"); if (!rc) rc = add_arg (gpg, "--charset"); if (!rc) rc = add_arg (gpg, "utf8"); if (!rc) rc = add_arg (gpg, "--enable-progress-filter"); if (!rc && have_gpg_version (gpg, "2.1.11")) rc = add_arg (gpg, "--exit-on-status-write-error"); if (rc) goto leave; rc = _gpgme_getenv ("DISPLAY", &dft_display); if (rc) goto leave; if (dft_display) { rc = add_arg (gpg, "--display"); if (!rc) rc = add_arg (gpg, dft_display); free (dft_display); if (rc) goto leave; } rc = _gpgme_getenv ("GPG_TTY", &env_tty); if (isatty (1) || env_tty || rc) { int err = 0; if (rc) goto leave; else if (env_tty) { snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); free (env_tty); } else err = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ if (!err) { if (*dft_ttyname) { rc = add_arg (gpg, "--ttyname"); if (!rc) rc = add_arg (gpg, dft_ttyname); } else rc = 0; if (!rc) { rc = _gpgme_getenv ("TERM", &dft_ttytype); if (rc) goto leave; if (dft_ttytype) { rc = add_arg (gpg, "--ttytype"); if (!rc) rc = add_arg (gpg, dft_ttytype); } free (dft_ttytype); } if (rc) goto leave; } } rc = gpgme_data_new (&gpg->diagnostics); if (rc) goto leave; rc = add_arg (gpg, "--logger-fd"); if (rc) goto leave; rc = add_data (gpg, gpg->diagnostics, -2, 1); leave: if (rc) gpg_release (gpg); else *engine = gpg; return rc; } /* Copy flags from CTX into the engine object. */ static void gpg_set_engine_flags (void *engine, const gpgme_ctx_t ctx) { engine_gpg_t gpg = engine; if (ctx->request_origin && have_gpg_version (gpg, "2.2.6")) { if (strlen (ctx->request_origin) + 1 > sizeof gpg->request_origin) strcpy (gpg->request_origin, "xxx"); /* Too long - force error */ else strcpy (gpg->request_origin, ctx->request_origin); } else *gpg->request_origin = 0; if (ctx->auto_key_locate && have_gpg_version (gpg, "2.1.18")) { if (gpg->auto_key_locate) free (gpg->auto_key_locate); gpg->auto_key_locate = _gpgme_strconcat ("--auto-key-locate=", ctx->auto_key_locate, NULL); } if (ctx->trust_model && strlen (ctx->trust_model)) { if (gpg->trust_model) free (gpg->trust_model); gpg->trust_model = _gpgme_strconcat ("--trust-model=", ctx->trust_model, NULL); } gpg->flags.no_symkey_cache = (ctx->no_symkey_cache && have_gpg_version (gpg, "2.2.7")); gpg->flags.offline = (ctx->offline && have_gpg_version (gpg, "2.1.23")); gpg->flags.ignore_mdc_error = !!ctx->ignore_mdc_error; } static gpgme_error_t gpg_set_locale (void *engine, int category, const char *value) { engine_gpg_t gpg = engine; if (0) ; #ifdef LC_CTYPE else if (category == LC_CTYPE) { if (gpg->lc_ctype) { free (gpg->lc_ctype); gpg->lc_ctype = NULL; } if (value) { gpg->lc_ctype = strdup (value); if (!gpg->lc_ctype) return gpg_error_from_syserror (); } } #endif #ifdef LC_MESSAGES else if (category == LC_MESSAGES) { if (gpg->lc_messages) { free (gpg->lc_messages); gpg->lc_messages = NULL; } if (value) { gpg->lc_messages = strdup (value); if (!gpg->lc_messages) return gpg_error_from_syserror (); } } #endif /* LC_MESSAGES */ else return gpg_error (GPG_ERR_INV_VALUE); return 0; } /* This sets a status callback for monitoring status lines before they * are passed to a caller set handler. */ static void gpg_set_status_cb (void *engine, gpgme_status_cb_t cb, void *cb_value) { engine_gpg_t gpg = engine; gpg->status.mon_cb = cb; gpg->status.mon_cb_value = cb_value; } /* Note, that the status_handler is allowed to modify the args value. */ static void gpg_set_status_handler (void *engine, engine_status_handler_t fnc, void *fnc_value) { engine_gpg_t gpg = engine; gpg->status.fnc = fnc; gpg->status.fnc_value = fnc_value; } /* Kludge to process --with-colon output. */ static gpgme_error_t gpg_set_colon_line_handler (void *engine, engine_colon_line_handler_t fnc, void *fnc_value) { engine_gpg_t gpg = engine; gpg->colon.bufsize = 1024; gpg->colon.readpos = 0; gpg->colon.buffer = malloc (gpg->colon.bufsize); if (!gpg->colon.buffer) return gpg_error_from_syserror (); if (_gpgme_io_pipe (gpg->colon.fd, 1) == -1) { int saved_err = gpg_error_from_syserror (); free (gpg->colon.buffer); gpg->colon.buffer = NULL; return saved_err; } if (_gpgme_io_set_close_notify (gpg->colon.fd[0], close_notify_handler, gpg) || _gpgme_io_set_close_notify (gpg->colon.fd[1], close_notify_handler, gpg)) return gpg_error (GPG_ERR_GENERAL); gpg->colon.eof = 0; gpg->colon.fnc = fnc; gpg->colon.fnc_value = fnc_value; return 0; } static gpgme_error_t command_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_gpg_t gpg = (engine_gpg_t) data->handler_value; gpgme_error_t err; int processed = 0; assert (gpg->cmd.used); assert (gpg->cmd.code); assert (gpg->cmd.fnc); err = gpg->cmd.fnc (gpg->cmd.fnc_value, gpg->cmd.code, gpg->cmd.keyword, fd, &processed); gpg->cmd.code = 0; /* And sleep again until read_status will wake us up again. */ /* XXX We must check if there are any more fds active after removing this one. */ (*gpg->io_cbs.remove) (gpg->fd_data_map[gpg->cmd.idx].tag); gpg->cmd.fd = gpg->fd_data_map[gpg->cmd.idx].fd; gpg->fd_data_map[gpg->cmd.idx].fd = -1; if (err) return err; /* We always need to send at least a newline character. */ if (!processed) _gpgme_io_write (fd, "\n", 1); return 0; } /* The FNC will be called to get a value for one of the commands with * a key KEY. If the code passed to FNC is 0, the function may * release resources associated with the returned value from another * call. To match such a second call to a first call, the returned * value from the first call is passed as keyword. */ static gpgme_error_t gpg_set_command_handler (void *engine, engine_command_handler_t fnc, void *fnc_value) { engine_gpg_t gpg = engine; gpgme_error_t rc; rc = add_arg (gpg, "--command-fd"); if (rc) return rc; /* This is a hack. We don't have a real data object. The only thing that matters is that we use something unique, so we use the address of the cmd structure in the gpg object. */ rc = add_data (gpg, (void *) &gpg->cmd, -2, 0); if (rc) return rc; gpg->cmd.fnc = fnc; gpg->cmd.cb_data = (void *) &gpg->cmd; gpg->cmd.fnc_value = fnc_value; gpg->cmd.used = 1; return 0; } static gpgme_error_t build_argv (engine_gpg_t gpg, const char *pgmname) { gpgme_error_t err; struct arg_and_data_s *a; struct fd_data_map_s *fd_data_map; size_t datac=0, argc=0; char **argv; int need_special = 0; int use_agent = 0; char *p; if (_gpgme_in_gpg_one_mode ()) { /* In GnuPG-1 mode we don't want to use the agent with a malformed environment variable. This is only a very basic test but sufficient to make our life in the regression tests easier. With GnuPG-2 the agent is anyway required and on modern installations GPG_AGENT_INFO is optional. */ err = _gpgme_getenv ("GPG_AGENT_INFO", &p); if (err) return err; use_agent = (p && strchr (p, ':')); if (p) free (p); } if (gpg->argv) { free_argv (gpg->argv); gpg->argv = NULL; } if (gpg->fd_data_map) { free_fd_data_map (gpg->fd_data_map); gpg->fd_data_map = NULL; } argc++; /* For argv[0]. */ for (a = gpg->arglist; a; a = a->next) { argc++; if (a->data) { /*fprintf (stderr, "build_argv: data\n" );*/ datac++; if (a->dup_to == -1 && !a->print_fd) need_special = 1; } else { /* fprintf (stderr, "build_argv: arg=`%s'\n", a->arg );*/ } } if (need_special) argc++; if (use_agent) argc++; if (gpg->pinentry_mode) argc++; if (!gpg->cmd.used) argc++; /* --batch */ argc += 4; /* --no-sk-comments, --request-origin, --no-symkey-cache */ /* --disable-dirmngr */ argv = calloc (argc + 1, sizeof *argv); if (!argv) return gpg_error_from_syserror (); fd_data_map = calloc (datac + 1, sizeof *fd_data_map); if (!fd_data_map) { int saved_err = gpg_error_from_syserror (); free_argv (argv); return saved_err; } argc = datac = 0; argv[argc] = strdup (_gpgme_get_basename (pgmname)); /* argv[0] */ if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; if (need_special) { argv[argc] = strdup ("--enable-special-filenames"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (use_agent) { argv[argc] = strdup ("--use-agent"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (*gpg->request_origin) { argv[argc] = _gpgme_strconcat ("--request-origin=", gpg->request_origin, NULL); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (gpg->auto_key_locate) { argv[argc] = strdup (gpg->auto_key_locate); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (gpg->trust_model) { argv[argc] = strdup (gpg->trust_model); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (gpg->flags.no_symkey_cache) { argv[argc] = strdup ("--no-symkey-cache"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (gpg->flags.ignore_mdc_error) { argv[argc] = strdup ("--ignore-mdc-error"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (gpg->flags.offline) { argv[argc] = strdup ("--disable-dirmngr"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } if (gpg->pinentry_mode && have_gpg_version (gpg, "2.1.0")) { const char *s = NULL; switch (gpg->pinentry_mode) { case GPGME_PINENTRY_MODE_DEFAULT: break; case GPGME_PINENTRY_MODE_ASK: s = "--pinentry-mode=ask"; break; case GPGME_PINENTRY_MODE_CANCEL: s = "--pinentry-mode=cancel"; break; case GPGME_PINENTRY_MODE_ERROR: s = "--pinentry-mode=error"; break; case GPGME_PINENTRY_MODE_LOOPBACK:s = "--pinentry-mode=loopback"; break; } if (s) { argv[argc] = strdup (s); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } } if (!gpg->cmd.used) { argv[argc] = strdup ("--batch"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } argv[argc] = strdup ("--no-sk-comments"); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; for (a = gpg->arglist; a; a = a->next) { if (a->arg_locp) *(a->arg_locp) = argc; if (a->data) { /* Create a pipe to pass it down to gpg. */ fd_data_map[datac].inbound = a->inbound; /* Create a pipe. */ { int fds[2]; if (_gpgme_io_pipe (fds, fd_data_map[datac].inbound ? 1 : 0) == -1) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } if (_gpgme_io_set_close_notify (fds[0], close_notify_handler, gpg) || _gpgme_io_set_close_notify (fds[1], close_notify_handler, gpg)) { /* We leak fd_data_map and the fds. This is not easy to avoid and given that we reach this here only after a malloc failure for a small object, it is probably better not to do anything. */ return gpg_error (GPG_ERR_GENERAL); } /* If the data_type is FD, we have to do a dup2 here. */ if (fd_data_map[datac].inbound) { fd_data_map[datac].fd = fds[0]; fd_data_map[datac].peer_fd = fds[1]; } else { fd_data_map[datac].fd = fds[1]; fd_data_map[datac].peer_fd = fds[0]; } } /* Hack to get hands on the fd later. */ if (gpg->cmd.used) { if (gpg->cmd.cb_data == a->data) { assert (gpg->cmd.idx == -1); gpg->cmd.idx = datac; } } fd_data_map[datac].data = a->data; fd_data_map[datac].dup_to = a->dup_to; if (a->dup_to == -1) { char *ptr; int buflen = 25; argv[argc] = malloc (buflen); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } ptr = argv[argc]; if (!a->print_fd) { *(ptr++) = '-'; *(ptr++) = '&'; buflen -= 2; } _gpgme_io_fd2str (ptr, buflen, fd_data_map[datac].peer_fd); fd_data_map[datac].arg_loc = argc; argc++; } datac++; } else { argv[argc] = strdup (a->arg); if (!argv[argc]) { int saved_err = gpg_error_from_syserror (); free (fd_data_map); free_argv (argv); return saved_err; } argc++; } } gpg->argv = argv; gpg->fd_data_map = fd_data_map; return 0; } static gpgme_error_t add_io_cb (engine_gpg_t gpg, int fd, int dir, gpgme_io_cb_t handler, void *data, void **tag) { gpgme_error_t err; err = (*gpg->io_cbs.add) (gpg->io_cbs.add_priv, fd, dir, handler, data, tag); if (err) return err; if (!dir) /* FIXME Kludge around poll() problem. */ err = _gpgme_io_set_nonblocking (fd); return err; } /* Handle the status output of GnuPG. This function does read entire lines and passes them as C strings to the callback function (we can use C Strings because the status output is always UTF-8 encoded). Of course we have to buffer the lines to cope with long lines e.g. with a large user ID. Note: We can optimize this to only cope with status line code we know about and skip all other stuff without buffering (i.e. without extending the buffer). */ static gpgme_error_t read_status (engine_gpg_t gpg) { char *p; int nread; size_t bufsize = gpg->status.bufsize; char *buffer = gpg->status.buffer; size_t readpos = gpg->status.readpos; gpgme_error_t err; assert (buffer); if (bufsize - readpos < 256) { /* Need more room for the read. */ bufsize += 1024; buffer = realloc (buffer, bufsize); if (!buffer) return gpg_error_from_syserror (); } nread = _gpgme_io_read (gpg->status.fd[0], buffer + readpos, bufsize-readpos); if (nread == -1) return gpg_error_from_syserror (); if (!nread) { err = 0; gpg->status.eof = 1; if (gpg->status.mon_cb) err = gpg->status.mon_cb (gpg->status.mon_cb_value, "", ""); if (gpg->status.fnc) { char emptystring[1] = {0}; err = gpg->status.fnc (gpg->status.fnc_value, GPGME_STATUS_EOF, emptystring); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ } return err; } while (nread > 0) { for (p = buffer + readpos; nread; nread--, p++) { if (*p == '\n') { /* (we require that the last line is terminated by a LF) */ if (p > buffer && p[-1] == '\r') p[-1] = 0; *p = 0; if (!strncmp (buffer, "[GNUPG:] ", 9) && buffer[9] >= 'A' && buffer[9] <= 'Z') { char *rest; gpgme_status_code_t r; rest = strchr (buffer + 9, ' '); if (!rest) rest = p; /* Set to an empty string. */ else *rest++ = 0; r = _gpgme_parse_status (buffer + 9); if (gpg->status.mon_cb && r != GPGME_STATUS_PROGRESS) { /* Note that we call the monitor even if we do * not know the status code (r < 0). */ err = gpg->status.mon_cb (gpg->status.mon_cb_value, buffer + 9, rest); if (err) return err; } if (r >= 0) { if (gpg->cmd.used && (r == GPGME_STATUS_GET_BOOL || r == GPGME_STATUS_GET_LINE || r == GPGME_STATUS_GET_HIDDEN)) { gpg->cmd.code = r; if (gpg->cmd.keyword) free (gpg->cmd.keyword); gpg->cmd.keyword = strdup (rest); if (!gpg->cmd.keyword) return gpg_error_from_syserror (); /* This should be the last thing we have received and the next thing will be that the command handler does its action. */ if (nread > 1) TRACE0 (DEBUG_CTX, "gpgme:read_status", 0, "error: unexpected data"); add_io_cb (gpg, gpg->cmd.fd, 0, command_handler, gpg, &gpg->fd_data_map[gpg->cmd.idx].tag); gpg->fd_data_map[gpg->cmd.idx].fd = gpg->cmd.fd; gpg->cmd.fd = -1; } else if (gpg->status.fnc) { err = gpg->status.fnc (gpg->status.fnc_value, r, rest); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ if (err) return err; } } } /* To reuse the buffer for the next line we have to shift the remaining data to the buffer start and restart the loop Hmmm: We can optimize this function by looking forward in the buffer to see whether a second complete line is available and in this case avoid the memmove for this line. */ nread--; p++; if (nread) memmove (buffer, p, nread); readpos = 0; break; /* the for loop */ } else readpos++; } } /* Update the gpg object. */ gpg->status.bufsize = bufsize; gpg->status.buffer = buffer; gpg->status.readpos = readpos; return 0; } static gpgme_error_t status_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_gpg_t gpg = (engine_gpg_t) data->handler_value; int err; assert (fd == gpg->status.fd[0]); err = read_status (gpg); if (err) return err; if (gpg->status.eof) _gpgme_io_close (fd); return 0; } static gpgme_error_t read_colon_line (engine_gpg_t gpg) { char *p; int nread; size_t bufsize = gpg->colon.bufsize; char *buffer = gpg->colon.buffer; size_t readpos = gpg->colon.readpos; assert (buffer); if (bufsize - readpos < 256) { /* Need more room for the read. */ bufsize += 1024; buffer = realloc (buffer, bufsize); if (!buffer) return gpg_error_from_syserror (); } nread = _gpgme_io_read (gpg->colon.fd[0], buffer+readpos, bufsize-readpos); if (nread == -1) return gpg_error_from_syserror (); if (!nread) { gpg->colon.eof = 1; assert (gpg->colon.fnc); gpg->colon.fnc (gpg->colon.fnc_value, NULL); return 0; } while (nread > 0) { for (p = buffer + readpos; nread; nread--, p++) { if ( *p == '\n' ) { /* (we require that the last line is terminated by a LF) and we skip empty lines. Note: we use UTF8 encoding and escaping of special characters. We require at least one colon to cope with some other printed information. */ *p = 0; if (*buffer && strchr (buffer, ':')) { char *line = NULL; if (gpg->colon.preprocess_fnc) { gpgme_error_t err; err = gpg->colon.preprocess_fnc (buffer, &line); if (err) return err; } assert (gpg->colon.fnc); if (line) { char *linep = line; char *endp; do { endp = strchr (linep, '\n'); if (endp) *endp++ = 0; gpg->colon.fnc (gpg->colon.fnc_value, linep); linep = endp; } while (linep && *linep); gpgrt_free (line); } else gpg->colon.fnc (gpg->colon.fnc_value, buffer); } /* To reuse the buffer for the next line we have to shift the remaining data to the buffer start and restart the loop Hmmm: We can optimize this function by looking forward in the buffer to see whether a second complete line is available and in this case avoid the memmove for this line. */ nread--; p++; if (nread) memmove (buffer, p, nread); readpos = 0; break; /* The for loop. */ } else readpos++; } } /* Update the gpg object. */ gpg->colon.bufsize = bufsize; gpg->colon.buffer = buffer; gpg->colon.readpos = readpos; return 0; } /* This colonline handler thing is not the clean way to do it. It might be better to enhance the gpgme_data_t object to act as a wrapper for a callback. Same goes for the status thing. For now we use this thing here because it is easier to implement. */ static gpgme_error_t colon_line_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_gpg_t gpg = (engine_gpg_t) data->handler_value; gpgme_error_t rc = 0; assert (fd == gpg->colon.fd[0]); rc = read_colon_line (gpg); if (rc) return rc; if (gpg->colon.eof) _gpgme_io_close (fd); return 0; } static gpgme_error_t start (engine_gpg_t gpg) { gpgme_error_t rc; int i, n; int status; struct spawn_fd_item_s *fd_list; pid_t pid; const char *pgmname; if (!gpg) return gpg_error (GPG_ERR_INV_VALUE); if (!gpg->file_name && !_gpgme_get_default_gpg_name ()) return trace_gpg_error (GPG_ERR_INV_ENGINE); if (gpg->lc_ctype) { rc = add_arg_ext (gpg, gpg->lc_ctype, 1); if (!rc) rc = add_arg_ext (gpg, "--lc-ctype", 1); if (rc) return rc; } if (gpg->lc_messages) { rc = add_arg_ext (gpg, gpg->lc_messages, 1); if (!rc) rc = add_arg_ext (gpg, "--lc-messages", 1); if (rc) return rc; } pgmname = gpg->file_name ? gpg->file_name : _gpgme_get_default_gpg_name (); rc = build_argv (gpg, pgmname); if (rc) return rc; /* status_fd, colon_fd and end of list. */ n = 3; for (i = 0; gpg->fd_data_map[i].data; i++) n++; fd_list = calloc (n, sizeof *fd_list); if (! fd_list) return gpg_error_from_syserror (); /* Build the fd list for the child. */ n = 0; fd_list[n].fd = gpg->status.fd[1]; fd_list[n].dup_to = -1; fd_list[n].arg_loc = gpg->status.arg_loc; n++; if (gpg->colon.fnc) { fd_list[n].fd = gpg->colon.fd[1]; fd_list[n].dup_to = 1; n++; } for (i = 0; gpg->fd_data_map[i].data; i++) { fd_list[n].fd = gpg->fd_data_map[i].peer_fd; fd_list[n].dup_to = gpg->fd_data_map[i].dup_to; fd_list[n].arg_loc = gpg->fd_data_map[i].arg_loc; n++; } fd_list[n].fd = -1; fd_list[n].dup_to = -1; status = _gpgme_io_spawn (pgmname, gpg->argv, (IOSPAWN_FLAG_DETACHED |IOSPAWN_FLAG_ALLOW_SET_FG), fd_list, NULL, NULL, &pid); { int saved_err = gpg_error_from_syserror (); free (fd_list); if (status == -1) return saved_err; } /*_gpgme_register_term_handler ( closure, closure_value, pid );*/ rc = add_io_cb (gpg, gpg->status.fd[0], 1, status_handler, gpg, &gpg->status.tag); if (rc) /* FIXME: kill the child */ return rc; if (gpg->colon.fnc) { assert (gpg->colon.fd[0] != -1); rc = add_io_cb (gpg, gpg->colon.fd[0], 1, colon_line_handler, gpg, &gpg->colon.tag); if (rc) /* FIXME: kill the child */ return rc; } for (i = 0; gpg->fd_data_map[i].data; i++) { if (gpg->cmd.used && i == gpg->cmd.idx) { /* Park the cmd fd. */ gpg->cmd.fd = gpg->fd_data_map[i].fd; gpg->fd_data_map[i].fd = -1; } else { rc = add_io_cb (gpg, gpg->fd_data_map[i].fd, gpg->fd_data_map[i].inbound, gpg->fd_data_map[i].inbound ? _gpgme_data_inbound_handler : _gpgme_data_outbound_handler, gpg->fd_data_map[i].data, &gpg->fd_data_map[i].tag); if (rc) /* FIXME: kill the child */ return rc; } } gpg_io_event (gpg, GPGME_EVENT_START, NULL); /* fixme: check what data we can release here */ return 0; } /* Add the --input-size-hint option if requested. */ static gpgme_error_t add_input_size_hint (engine_gpg_t gpg, gpgme_data_t data) { gpgme_error_t err; gpgme_off_t value = _gpgme_data_get_size_hint (data); char numbuf[50]; /* Large enough for even 2^128 in base-10. */ char *p; if (!value || !have_gpg_version (gpg, "2.1.15")) return 0; err = add_arg (gpg, "--input-size-hint"); if (!err) { p = numbuf + sizeof numbuf; *--p = 0; do { *--p = '0' + (value % 10); value /= 10; } while (value); err = add_arg (gpg, p); } return err; } static gpgme_error_t gpg_decrypt (void *engine, gpgme_decrypt_flags_t flags, gpgme_data_t ciph, gpgme_data_t plain, int export_session_key, const char *override_session_key, int auto_key_retrieve) { engine_gpg_t gpg = engine; gpgme_error_t err; err = add_arg (gpg, "--decrypt"); if (!err && (flags & GPGME_DECRYPT_UNWRAP)) { if (!have_gpg_version (gpg, "2.1.12")) err = gpg_error (GPG_ERR_NOT_SUPPORTED); else err = add_arg (gpg, "--unwrap"); } if (!err && export_session_key) err = add_arg (gpg, "--show-session-key"); if (!err && auto_key_retrieve) err = add_arg (gpg, "--auto-key-retrieve"); if (!err && override_session_key && *override_session_key) { if (have_gpg_version (gpg, "2.1.16")) { gpgme_data_release (gpg->override_session_key); TRACE2 (DEBUG_ENGINE, "override", gpg, "seskey='%s' len=%zu\n", override_session_key, strlen (override_session_key)); err = gpgme_data_new_from_mem (&gpg->override_session_key, override_session_key, strlen (override_session_key), 1); if (!err) { /* We add --no-keyring because a keyring is not required * when we are overriding the session key. It would * work without that option but --no-keyring avoids that * gpg return a failure due to a missing key log_error() * diagnostic. --no-keyring is supported since 2.1.14. */ err = add_arg (gpg, "--no-keyring"); if (!err) err = add_arg (gpg, "--override-session-key-fd"); if (!err) err = add_data (gpg, gpg->override_session_key, -2, 0); } } else { /* Using that option may leak the session key via ps(1). */ err = add_arg (gpg, "--override-session-key"); if (!err) err = add_arg (gpg, override_session_key); } } /* Tell the gpg object about the data. */ if (!err) err = add_arg (gpg, "--output"); if (!err) err = add_arg (gpg, "-"); if (!err) err = add_data (gpg, plain, 1, 1); if (!err) err = add_input_size_hint (gpg, ciph); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, ciph, -1, 0); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_delete (void *engine, gpgme_key_t key, unsigned int flags) { engine_gpg_t gpg = engine; gpgme_error_t err = 0; int allow_secret = flags & GPGME_DELETE_ALLOW_SECRET; int force = flags & GPGME_DELETE_FORCE; if (force) err = add_arg (gpg, "--yes"); if (!err) err = add_arg (gpg, allow_secret ? "--delete-secret-and-public-key" : "--delete-key"); if (!err) err = add_arg (gpg, "--"); if (!err) { if (!key->subkeys || !key->subkeys->fpr) return gpg_error (GPG_ERR_INV_VALUE); else err = add_arg (gpg, key->subkeys->fpr); } if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_passwd (void *engine, gpgme_key_t key, unsigned int flags) { engine_gpg_t gpg = engine; gpgme_error_t err; (void)flags; if (!key || !key->subkeys || !key->subkeys->fpr) return gpg_error (GPG_ERR_INV_CERT_OBJ); err = add_arg (gpg, "--passwd"); if (!err) err = add_arg (gpg, key->subkeys->fpr); if (!err) err = start (gpg); return err; } static gpgme_error_t append_args_from_signers (engine_gpg_t gpg, gpgme_ctx_t ctx /* FIXME */) { gpgme_error_t err = 0; int i; gpgme_key_t key; for (i = 0; (key = gpgme_signers_enum (ctx, i)); i++) { const char *s = key->subkeys ? key->subkeys->keyid : NULL; if (s) { if (!err) err = add_arg (gpg, "-u"); if (!err) err = add_arg (gpg, s); } gpgme_key_unref (key); if (err) break; } return err; } static gpgme_error_t append_args_from_sender (engine_gpg_t gpg, gpgme_ctx_t ctx) { gpgme_error_t err; if (ctx->sender && have_gpg_version (gpg, "2.1.15")) { err = add_arg (gpg, "--sender"); if (!err) err = add_arg (gpg, ctx->sender); } else err = 0; return err; } static gpgme_error_t append_args_from_sig_notations (engine_gpg_t gpg, gpgme_ctx_t ctx /* FIXME */) { gpgme_error_t err = 0; gpgme_sig_notation_t notation; notation = gpgme_sig_notation_get (ctx); while (!err && notation) { if (notation->name && !(notation->flags & GPGME_SIG_NOTATION_HUMAN_READABLE)) err = gpg_error (GPG_ERR_INV_VALUE); else if (notation->name) { char *arg; /* Maximum space needed is one byte for the "critical" flag, the name, one byte for '=', the value, and a terminating '\0'. */ arg = malloc (1 + notation->name_len + 1 + notation->value_len + 1); if (!arg) err = gpg_error_from_syserror (); if (!err) { char *argp = arg; if (notation->critical) *(argp++) = '!'; memcpy (argp, notation->name, notation->name_len); argp += notation->name_len; *(argp++) = '='; /* We know that notation->name is '\0' terminated. */ strcpy (argp, notation->value); } if (!err) err = add_arg (gpg, "--sig-notation"); if (!err) err = add_arg (gpg, arg); if (arg) free (arg); } else { /* This is a policy URL. */ char *value; if (notation->critical) { value = malloc (1 + notation->value_len + 1); if (!value) err = gpg_error_from_syserror (); else { value[0] = '!'; /* We know that notation->value is '\0' terminated. */ strcpy (&value[1], notation->value); } } else value = notation->value; if (!err) err = add_arg (gpg, "--sig-policy-url"); if (!err) err = add_arg (gpg, value); if (value != notation->value) free (value); } notation = notation->next; } return err; } static gpgme_error_t gpg_edit (void *engine, int type, gpgme_key_t key, gpgme_data_t out, gpgme_ctx_t ctx /* FIXME */) { engine_gpg_t gpg = engine; gpgme_error_t err; err = add_arg (gpg, "--with-colons"); if (!err) err = append_args_from_signers (gpg, ctx); if (!err) err = add_arg (gpg, type == 0 ? "--edit-key" : "--card-edit"); if (!err) err = add_data (gpg, out, 1, 1); if (!err) err = add_arg (gpg, "--"); if (!err && type == 0) { const char *s = key->subkeys ? key->subkeys->fpr : NULL; if (!s) err = gpg_error (GPG_ERR_INV_VALUE); else err = add_arg (gpg, s); } if (!err) err = start (gpg); return err; } /* Add a single argument from a key to an -r option. */ static gpg_error_t add_arg_recipient (engine_gpg_t gpg, gpgme_encrypt_flags_t flags, gpgme_key_t key) { gpg_error_t err; if ((flags & GPGME_ENCRYPT_WANT_ADDRESS)) { /* We have no way to figure out which mail address was * requested. FIXME: It would be possible to figure this out by * consulting the SENDER property of the context. */ err = gpg_error (GPG_ERR_INV_USER_ID); } else err = add_arg (gpg, key->subkeys->fpr); return err; } /* Add a single argument from a USERID string to an -r option. */ static gpg_error_t add_arg_recipient_string (engine_gpg_t gpg, gpgme_encrypt_flags_t flags, const char *userid, int useridlen) { gpg_error_t err; if ((flags & GPGME_ENCRYPT_WANT_ADDRESS)) { char *tmpstr, *mbox; tmpstr = malloc (useridlen + 1); if (!tmpstr) err = gpg_error_from_syserror (); else { memcpy (tmpstr, userid, useridlen); tmpstr[useridlen] = 0; mbox = _gpgme_mailbox_from_userid (tmpstr); if (!mbox) { err = gpg_error_from_syserror (); if (gpg_err_code (err) == GPG_ERR_EINVAL) err = gpg_error (GPG_ERR_INV_USER_ID); } else err = add_arg (gpg, mbox); free (mbox); free (tmpstr); } } else err = add_arg_len (gpg, NULL, userid, useridlen); return err; } static gpgme_error_t append_args_from_recipients (engine_gpg_t gpg, gpgme_encrypt_flags_t flags, gpgme_key_t recp[]) { gpgme_error_t err = 0; int i = 0; while (recp[i]) { if (!recp[i]->subkeys || !recp[i]->subkeys->fpr) err = gpg_error (GPG_ERR_INV_VALUE); if (!err) err = add_arg (gpg, "-r"); if (!err) err = add_arg_recipient (gpg, flags, recp[i]); if (err) break; i++; } return err; } /* Take recipients from the LF delimited STRING and add -r args. */ static gpg_error_t append_args_from_recipients_string (engine_gpg_t gpg, gpgme_encrypt_flags_t flags, const char *string) { gpg_error_t err = 0; gpgme_encrypt_flags_t orig_flags = flags; int any = 0; int ignore = 0; int hidden = 0; int file = 0; const char *s; int n; do { /* Skip leading white space */ while (*string == ' ' || *string == '\t') string++; if (!*string) break; /* Look for the LF. */ s = strchr (string, '\n'); if (s) n = s - string; else n = strlen (string); while (n && (string[n-1] == ' ' || string[n-1] == '\t')) n--; if (!ignore && n == 2 && !memcmp (string, "--", 2)) ignore = 1; else if (!ignore && n == 8 && !memcmp (string, "--hidden", 8)) hidden = 1; else if (!ignore && n == 11 && !memcmp (string, "--no-hidden", 11)) hidden = 0; else if (!ignore && n == 6 && !memcmp (string, "--file", 6)) { file = 1; /* Because the key is used as is we need to ignore this flag: */ flags &= ~GPGME_ENCRYPT_WANT_ADDRESS; } else if (!ignore && n == 9 && !memcmp (string, "--no-file", 9)) { file = 0; flags = orig_flags; } else if (n) /* Not empty - use it. */ { err = add_arg (gpg, file? (hidden? "-F":"-f") : (hidden? "-R":"-r")); if (!err) err = add_arg_recipient_string (gpg, flags, string, n); if (!err) any = 1; } string += n + !!s; } while (!err); if (!err && !any) err = gpg_error (GPG_ERR_MISSING_KEY); return err; } static gpgme_error_t gpg_encrypt (void *engine, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor) { engine_gpg_t gpg = engine; gpgme_error_t err = 0; if (recp || recpstring) err = add_arg (gpg, "--encrypt"); if (!err && ((flags & GPGME_ENCRYPT_SYMMETRIC) || (!recp && !recpstring))) err = add_arg (gpg, "--symmetric"); if (!err && use_armor) err = add_arg (gpg, "--armor"); if (!err && (flags & GPGME_ENCRYPT_WRAP)) { /* gpg is current not able to detect already compressed * packets. Thus when using * gpg --unwrap -d | gpg --no-literal -e * the encryption would add an additional compression layer. * We better suppress that. */ flags |= GPGME_ENCRYPT_NO_COMPRESS; err = add_arg (gpg, "--no-literal"); } if (!err && (flags & GPGME_ENCRYPT_NO_COMPRESS)) err = add_arg (gpg, "--compress-algo=none"); if (!err && (flags & GPGME_ENCRYPT_THROW_KEYIDS)) err = add_arg (gpg, "--throw-keyids"); if (gpgme_data_get_encoding (plain) == GPGME_DATA_ENCODING_MIME && have_gpg_version (gpg, "2.1.14")) err = add_arg (gpg, "--mimemode"); if (recp || recpstring) { /* If we know that all recipients are valid (full or ultimate trust) we can suppress further checks. */ if (!err && (flags & GPGME_ENCRYPT_ALWAYS_TRUST)) err = add_arg (gpg, "--always-trust"); if (!err && (flags & GPGME_ENCRYPT_NO_ENCRYPT_TO)) err = add_arg (gpg, "--no-encrypt-to"); if (!err && !recp && recpstring) err = append_args_from_recipients_string (gpg, flags, recpstring); else if (!err) err = append_args_from_recipients (gpg, flags, recp); } /* Tell the gpg object about the data. */ if (!err) err = add_arg (gpg, "--output"); if (!err) err = add_arg (gpg, "-"); if (!err) err = add_data (gpg, ciph, 1, 1); if (gpgme_data_get_file_name (plain)) { if (!err) err = add_arg (gpg, "--set-filename"); if (!err) err = add_arg (gpg, gpgme_data_get_file_name (plain)); } if (!err) err = add_input_size_hint (gpg, plain); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, plain, -1, 0); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_encrypt_sign (void *engine, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor, gpgme_ctx_t ctx /* FIXME */) { engine_gpg_t gpg = engine; gpgme_error_t err = 0; if (recp || recpstring) err = add_arg (gpg, "--encrypt"); if (!err && ((flags & GPGME_ENCRYPT_SYMMETRIC) || (!recp && !recpstring))) err = add_arg (gpg, "--symmetric"); if (!err) err = add_arg (gpg, "--sign"); if (!err && use_armor) err = add_arg (gpg, "--armor"); if (!err && (flags & GPGME_ENCRYPT_NO_COMPRESS)) err = add_arg (gpg, "--compress-algo=none"); if (!err && (flags & GPGME_ENCRYPT_THROW_KEYIDS)) err = add_arg (gpg, "--throw-keyids"); if (gpgme_data_get_encoding (plain) == GPGME_DATA_ENCODING_MIME && have_gpg_version (gpg, "2.1.14")) err = add_arg (gpg, "--mimemode"); if (recp || recpstring) { /* If we know that all recipients are valid (full or ultimate trust) we can suppress further checks. */ if (!err && (flags & GPGME_ENCRYPT_ALWAYS_TRUST)) err = add_arg (gpg, "--always-trust"); if (!err && (flags & GPGME_ENCRYPT_NO_ENCRYPT_TO)) err = add_arg (gpg, "--no-encrypt-to"); if (!err && !recp && recpstring) err = append_args_from_recipients_string (gpg, flags, recpstring); else if (!err) err = append_args_from_recipients (gpg, flags, recp); } if (!err) err = append_args_from_signers (gpg, ctx); if (!err) err = append_args_from_sender (gpg, ctx); if (!err) err = append_args_from_sig_notations (gpg, ctx); /* Tell the gpg object about the data. */ if (!err) err = add_arg (gpg, "--output"); if (!err) err = add_arg (gpg, "-"); if (!err) err = add_data (gpg, ciph, 1, 1); if (gpgme_data_get_file_name (plain)) { if (!err) err = add_arg (gpg, "--set-filename"); if (!err) err = add_arg (gpg, gpgme_data_get_file_name (plain)); } if (!err) err = add_input_size_hint (gpg, plain); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, plain, -1, 0); if (!err) err = start (gpg); return err; } static gpgme_error_t export_common (engine_gpg_t gpg, gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { gpgme_error_t err = 0; if ((mode & ~(GPGME_EXPORT_MODE_EXTERN |GPGME_EXPORT_MODE_MINIMAL |GPGME_EXPORT_MODE_SECRET))) return gpg_error (GPG_ERR_NOT_SUPPORTED); if ((mode & GPGME_EXPORT_MODE_MINIMAL)) { if ((mode & GPGME_EXPORT_MODE_NOUID)) err = add_arg (gpg, "--export-options=export-minimal,export-drop-uids"); else err = add_arg (gpg, "--export-options=export-minimal"); } else if ((mode & GPGME_EXPORT_MODE_NOUID)) err = add_arg (gpg, "--export-options=export-drop-uids"); if (err) ; else if ((mode & GPGME_EXPORT_MODE_EXTERN)) { err = add_arg (gpg, "--send-keys"); if (!err && (mode & GPGME_EXPORT_MODE_NOUID)) err = add_arg (gpg, "--keyserver-options=export-drop-uids"); } else { if ((mode & GPGME_EXPORT_MODE_SECRET)) err = add_arg (gpg, "--export-secret-keys"); else err = add_arg (gpg, "--export"); if (!err && use_armor) err = add_arg (gpg, "--armor"); if (!err) err = add_data (gpg, keydata, 1, 1); } if (!err) err = add_arg (gpg, "--"); return err; } static gpgme_error_t gpg_export (void *engine, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { engine_gpg_t gpg = engine; gpgme_error_t err; err = export_common (gpg, mode, keydata, use_armor); if (!err && pattern && *pattern) err = add_arg (gpg, pattern); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_export_ext (void *engine, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { engine_gpg_t gpg = engine; gpgme_error_t err; err = export_common (gpg, mode, keydata, use_armor); if (pattern) { while (!err && *pattern && **pattern) err = add_arg (gpg, *(pattern++)); } if (!err) err = start (gpg); return err; } /* Helper to add algo, usage, and expire to the list of args. */ static gpgme_error_t gpg_add_algo_usage_expire (engine_gpg_t gpg, const char *algo, unsigned long expires, unsigned int flags) { gpg_error_t err; /* This condition is only required to allow the use of gpg < 2.1.16 */ if (algo || (flags & (GPGME_CREATE_SIGN | GPGME_CREATE_ENCR | GPGME_CREATE_CERT | GPGME_CREATE_AUTH | GPGME_CREATE_NOEXPIRE)) || expires) { err = add_arg (gpg, algo? algo : "default"); if (!err) { char tmpbuf[5*4+1]; snprintf (tmpbuf, sizeof tmpbuf, "%s%s%s%s", (flags & GPGME_CREATE_SIGN)? " sign":"", (flags & GPGME_CREATE_ENCR)? " encr":"", (flags & GPGME_CREATE_CERT)? " cert":"", (flags & GPGME_CREATE_AUTH)? " auth":""); err = add_arg (gpg, *tmpbuf? tmpbuf : "default"); } if (!err) { if ((flags & GPGME_CREATE_NOEXPIRE)) err = add_arg (gpg, "never"); else if (expires == 0) err = add_arg (gpg, "-"); else { char tmpbuf[8+20]; snprintf (tmpbuf, sizeof tmpbuf, "seconds=%lu", expires); err = add_arg (gpg, tmpbuf); } } } else err = 0; return err; } static gpgme_error_t gpg_createkey_from_param (engine_gpg_t gpg, gpgme_data_t help_data, unsigned int extraflags) { gpgme_error_t err; err = add_arg (gpg, "--gen-key"); if (!err && (extraflags & GENKEY_EXTRAFLAG_ARMOR)) err = add_arg (gpg, "--armor"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, help_data, -1, 0); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_createkey (engine_gpg_t gpg, const char *userid, const char *algo, unsigned long expires, unsigned int flags, unsigned int extraflags) { gpgme_error_t err; err = add_arg (gpg, "--quick-gen-key"); if (!err && (extraflags & GENKEY_EXTRAFLAG_ARMOR)) err = add_arg (gpg, "--armor"); if (!err && (flags & GPGME_CREATE_NOPASSWD)) { err = add_arg (gpg, "--passphrase"); if (!err) err = add_arg (gpg, ""); if (!err) err = add_arg (gpg, "--batch"); } if (!err && (flags & GPGME_CREATE_FORCE)) err = add_arg (gpg, "--yes"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, userid); if (!err) err = gpg_add_algo_usage_expire (gpg, algo, expires, flags); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_addkey (engine_gpg_t gpg, const char *algo, unsigned long expires, gpgme_key_t key, unsigned int flags, unsigned int extraflags) { gpgme_error_t err; if (!key || !key->fpr) return gpg_error (GPG_ERR_INV_ARG); err = add_arg (gpg, "--quick-addkey"); if (!err && (extraflags & GENKEY_EXTRAFLAG_ARMOR)) err = add_arg (gpg, "--armor"); if (!err && (flags & GPGME_CREATE_NOPASSWD)) { err = add_arg (gpg, "--passphrase"); if (!err) err = add_arg (gpg, ""); if (!err) err = add_arg (gpg, "--batch"); } if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, key->fpr); if (!err) err = gpg_add_algo_usage_expire (gpg, algo, expires, flags); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_adduid (engine_gpg_t gpg, gpgme_key_t key, const char *userid, unsigned int extraflags) { gpgme_error_t err; if (!key || !key->fpr || !userid) return gpg_error (GPG_ERR_INV_ARG); if ((extraflags & GENKEY_EXTRAFLAG_SETPRIMARY)) { if (!have_gpg_version (gpg, "2.1.20")) err = gpg_error (GPG_ERR_NOT_SUPPORTED); else err = add_arg (gpg, "--quick-set-primary-uid"); } else if ((extraflags & GENKEY_EXTRAFLAG_REVOKE)) err = add_arg (gpg, "--quick-revuid"); else err = add_arg (gpg, "--quick-adduid"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, key->fpr); if (!err) err = add_arg (gpg, userid); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_genkey (void *engine, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t key, unsigned int flags, gpgme_data_t help_data, unsigned int extraflags, gpgme_data_t pubkey, gpgme_data_t seckey) { engine_gpg_t gpg = engine; gpgme_error_t err; (void)reserved; if (!gpg) return gpg_error (GPG_ERR_INV_VALUE); /* If HELP_DATA is given the use of the old interface * (gpgme_op_genkey) has been requested. The other modes are: * * USERID && !KEY - Create a new keyblock. * !USERID && KEY - Add a new subkey to KEY (gpg >= 2.1.14) * USERID && KEY && !ALGO - Add a new user id to KEY (gpg >= 2.1.14). * or set a flag on a user id. */ if (help_data) { /* We need a special mechanism to get the fd of a pipe here, so that we can use this for the %pubring and %secring parameters. We don't have this yet, so we implement only the adding to the standard keyrings. */ if (pubkey || seckey) err = gpg_error (GPG_ERR_NOT_IMPLEMENTED); else err = gpg_createkey_from_param (gpg, help_data, extraflags); } else if (!have_gpg_version (gpg, "2.1.13")) err = gpg_error (GPG_ERR_NOT_SUPPORTED); else if (userid && !key) err = gpg_createkey (gpg, userid, algo, expires, flags, extraflags); else if (!userid && key) err = gpg_addkey (gpg, algo, expires, key, flags, extraflags); else if (userid && key && !algo) err = gpg_adduid (gpg, key, userid, extraflags); else err = gpg_error (GPG_ERR_INV_VALUE); return err; } /* Return the next DELIM delimited string from DATA as a C-string. The caller needs to provide the address of a pointer variable which he has to set to NULL before the first call. After the last call to this function, this function needs to be called once more with DATA set to NULL so that the function can release its internal state. After that the pointer variable is free for use again. Note that we use a delimiter and thus a trailing delimiter is not required. DELIM may not be changed after the first call. */ static const char * string_from_data (gpgme_data_t data, int delim, void **helpptr, gpgme_error_t *r_err) { #define MYBUFLEN 2000 /* Fixme: We don't support URLs longer than that. */ struct { int eof_seen; int nbytes; /* Length of the last returned string including the delimiter. */ int buflen; /* Valid length of BUF. */ char buf[MYBUFLEN+1]; /* Buffer with one byte extra space. */ } *self; char *p; int nread; *r_err = 0; if (!data) { if (*helpptr) { free (*helpptr); *helpptr = NULL; } return NULL; } if (*helpptr) self = *helpptr; else { self = malloc (sizeof *self); if (!self) { *r_err = gpg_error_from_syserror (); return NULL; } *helpptr = self; self->eof_seen = 0; self->nbytes = 0; self->buflen = 0; } if (self->eof_seen) return NULL; assert (self->nbytes <= self->buflen); memmove (self->buf, self->buf + self->nbytes, self->buflen - self->nbytes); self->buflen -= self->nbytes; self->nbytes = 0; do { /* Fixme: This is fairly infective scanning because we may scan the buffer several times. */ p = memchr (self->buf, delim, self->buflen); if (p) { *p = 0; self->nbytes = p - self->buf + 1; return self->buf; } if ( !(MYBUFLEN - self->buflen) ) { /* Not enough space - URL too long. */ *r_err = gpg_error (GPG_ERR_TOO_LARGE); return NULL; } nread = gpgme_data_read (data, self->buf + self->buflen, MYBUFLEN - self->buflen); if (nread < 0) { *r_err = gpg_error_from_syserror (); return NULL; } self->buflen += nread; } while (nread); /* EOF reached. If we have anything in the buffer, append a Nul and return it. */ self->eof_seen = 1; if (self->buflen) { self->buf[self->buflen] = 0; /* (we allocated one extra byte) */ return self->buf; } return NULL; #undef MYBUFLEN } static gpgme_error_t gpg_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray) { engine_gpg_t gpg = engine; gpgme_error_t err; int idx; gpgme_data_encoding_t dataenc; if (keydata && keyarray) return gpg_error (GPG_ERR_INV_VALUE); /* Only one is allowed. */ dataenc = gpgme_data_get_encoding (keydata); if (keyarray) { err = add_arg (gpg, "--recv-keys"); if (!err) err = add_arg (gpg, "--"); for (idx=0; !err && keyarray[idx]; idx++) { if (keyarray[idx]->protocol != GPGME_PROTOCOL_OpenPGP) ; else if (!keyarray[idx]->subkeys) ; else if (keyarray[idx]->subkeys->fpr && *keyarray[idx]->subkeys->fpr) err = add_arg (gpg, keyarray[idx]->subkeys->fpr); else if (*keyarray[idx]->subkeys->keyid) err = add_arg (gpg, keyarray[idx]->subkeys->keyid); } } else if (dataenc == GPGME_DATA_ENCODING_URL || dataenc == GPGME_DATA_ENCODING_URL0) { void *helpptr; const char *string; gpgme_error_t xerr; int delim = (dataenc == GPGME_DATA_ENCODING_URL)? '\n': 0; /* FIXME: --fetch-keys is probably not correct because it can't grok all kinds of URLs. On Unix it should just work but on Windows we will build the command line and that may fail for some embedded control characters. It is anyway limited to the maximum size of the command line. We need another command which can take its input from a file. Maybe we should use an option to gpg to modify such commands (ala --multifile). */ err = add_arg (gpg, "--fetch-keys"); if (!err) err = add_arg (gpg, "--"); helpptr = NULL; while (!err && (string = string_from_data (keydata, delim, &helpptr, &xerr))) err = add_arg (gpg, string); if (!err) err = xerr; string_from_data (NULL, delim, &helpptr, &xerr); } else if (dataenc == GPGME_DATA_ENCODING_URLESC) { /* Already escaped URLs are not yet supported. */ err = gpg_error (GPG_ERR_NOT_IMPLEMENTED); } else { err = add_arg (gpg, "--import"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, keydata, -1, 0); } if (!err) err = start (gpg); return err; } /* The output for external keylistings in GnuPG is different from all the other key listings. We catch this here with a special preprocessor that reformats the colon handler lines. */ static gpgme_error_t gpg_keylist_preprocess (char *line, char **r_line) { enum { RT_NONE, RT_INFO, RT_PUB, RT_UID } rectype = RT_NONE; #define NR_FIELDS 16 char *field[NR_FIELDS]; int fields = 0; size_t n; *r_line = NULL; while (line && fields < NR_FIELDS) { field[fields++] = line; line = strchr (line, ':'); if (line) *(line++) = '\0'; } if (!strcmp (field[0], "info")) rectype = RT_INFO; else if (!strcmp (field[0], "pub")) rectype = RT_PUB; else if (!strcmp (field[0], "uid")) rectype = RT_UID; else rectype = RT_NONE; switch (rectype) { case RT_INFO: /* FIXME: Eventually, check the version number at least. */ return 0; case RT_PUB: if (fields < 7) return 0; /* The format is: pub:::::: as defined in 5.2. Machine Readable Indexes of the OpenPGP HTTP Keyserver Protocol (draft). Modern versions of the SKS keyserver return the fingerprint instead of the keyid. We detect this here and use the v4 fingerprint format to convert it to a key id. We want: pub:o::::::::::::: */ n = strlen (field[1]); if (n > 16) { if (gpgrt_asprintf (r_line, "pub:o%s:%s:%s:%s:%s:%s::::::::\n" "fpr:::::::::%s:", field[6], field[3], field[2], field[1] + n - 16, field[4], field[5], field[1]) < 0) return gpg_error_from_syserror (); } else { if (gpgrt_asprintf (r_line, "pub:o%s:%s:%s:%s:%s:%s::::::::", field[6], field[3], field[2], field[1], field[4], field[5]) < 0) return gpg_error_from_syserror (); } return 0; case RT_UID: /* The format is: uid:::: as defined in 5.2. Machine Readable Indexes of the OpenPGP HTTP Keyserver Protocol (draft). For an ldap keyserver the format is: uid: We want: uid:o::::::::: */ { /* The user ID is percent escaped, but we want c-coded. Because we have to replace each '%HL' by '\xHL', we need at most 4/3 th the number of bytes. But because we also need to escape the backslashes we allocate twice as much. */ char *uid = malloc (2 * strlen (field[1]) + 1); char *src; char *dst; if (! uid) return gpg_error_from_syserror (); src = field[1]; dst = uid; while (*src) { if (*src == '%') { *(dst++) = '\\'; *(dst++) = 'x'; src++; /* Copy the next two bytes unconditionally. */ if (*src) *(dst++) = *(src++); if (*src) *(dst++) = *(src++); } else if (*src == '\\') { *dst++ = '\\'; *dst++ = '\\'; src++; } else *(dst++) = *(src++); } *dst = '\0'; if (fields < 4) { if (gpgrt_asprintf (r_line, "uid:o::::::::%s:", uid) < 0) return gpg_error_from_syserror (); } else { if (gpgrt_asprintf (r_line, "uid:o%s::::%s:%s:::%s:", field[4], field[2], field[3], uid) < 0) return gpg_error_from_syserror (); } } return 0; case RT_NONE: /* Unknown record. */ break; } return 0; } static gpg_error_t gpg_keylist_build_options (engine_gpg_t gpg, int secret_only, gpgme_keylist_mode_t mode) { gpg_error_t err; err = add_arg (gpg, "--with-colons"); /* Since gpg 2.1.15 fingerprints are always printed, thus there is * no more need to explicitly request them. */ if (!have_gpg_version (gpg, "2.1.15")) { if (!err) err = add_arg (gpg, "--fixed-list-mode"); if (!err) err = add_arg (gpg, "--with-fingerprint"); if (!err) err = add_arg (gpg, "--with-fingerprint"); } if (!err && (mode & GPGME_KEYLIST_MODE_WITH_TOFU) && have_gpg_version (gpg, "2.1.16")) err = add_arg (gpg, "--with-tofu-info"); if (!err && (mode & GPGME_KEYLIST_MODE_WITH_SECRET)) err = add_arg (gpg, "--with-secret"); if (!err && (mode & GPGME_KEYLIST_MODE_SIGS) && (mode & GPGME_KEYLIST_MODE_SIG_NOTATIONS)) { err = add_arg (gpg, "--list-options"); if (!err) err = add_arg (gpg, "show-sig-subpackets=\"20,26\""); } if (!err) { if ( (mode & GPGME_KEYLIST_MODE_EXTERN) ) { if (secret_only) err = gpg_error (GPG_ERR_NOT_SUPPORTED); else if ( (mode & GPGME_KEYLIST_MODE_LOCAL)) { /* The local+extern mode is special. It works only with gpg >= 2.0.10. FIXME: We should check that we have such a version to that we can return a proper error code. The problem is that we don't know the context here and thus can't access the cached version number for the engine info structure. */ err = add_arg (gpg, "--locate-keys"); if ((mode & GPGME_KEYLIST_MODE_SIGS)) err = add_arg (gpg, "--with-sig-check"); } else { err = add_arg (gpg, "--search-keys"); gpg->colon.preprocess_fnc = gpg_keylist_preprocess; } } else { err = add_arg (gpg, secret_only ? "--list-secret-keys" : ((mode & GPGME_KEYLIST_MODE_SIGS) ? "--check-sigs" : "--list-keys")); } } if (!err) err = add_arg (gpg, "--"); return err; } static gpgme_error_t gpg_keylist (void *engine, const char *pattern, int secret_only, gpgme_keylist_mode_t mode, int engine_flags) { engine_gpg_t gpg = engine; gpgme_error_t err; (void)engine_flags; err = gpg_keylist_build_options (gpg, secret_only, mode); if (!err && pattern && *pattern) err = add_arg (gpg, pattern); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_keylist_ext (void *engine, const char *pattern[], int secret_only, int reserved, gpgme_keylist_mode_t mode, int engine_flags) { engine_gpg_t gpg = engine; gpgme_error_t err; (void)engine_flags; if (reserved) return gpg_error (GPG_ERR_INV_VALUE); err = gpg_keylist_build_options (gpg, secret_only, mode); if (pattern) { while (!err && *pattern && **pattern) err = add_arg (gpg, *(pattern++)); } if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_keylist_data (void *engine, gpgme_data_t data) { engine_gpg_t gpg = engine; gpgme_error_t err; if (!have_gpg_version (gpg, "2.1.14")) return gpg_error (GPG_ERR_NOT_SUPPORTED); err = add_arg (gpg, "--with-colons"); if (!err) err = add_arg (gpg, "--with-fingerprint"); if (!err) err = add_arg (gpg, "--import-options"); if (!err) err = add_arg (gpg, "import-show"); if (!err) err = add_arg (gpg, "--dry-run"); if (!err) err = add_arg (gpg, "--import"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, data, -1, 0); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_keysign (void *engine, gpgme_key_t key, const char *userid, unsigned long expire, unsigned int flags, gpgme_ctx_t ctx) { engine_gpg_t gpg = engine; gpgme_error_t err; const char *s; if (!key || !key->fpr) return gpg_error (GPG_ERR_INV_ARG); if (!have_gpg_version (gpg, "2.1.12")) return gpg_error (GPG_ERR_NOT_SUPPORTED); if ((flags & GPGME_KEYSIGN_LOCAL)) err = add_arg (gpg, "--quick-lsign-key"); else err = add_arg (gpg, "--quick-sign-key"); if (!err) err = append_args_from_signers (gpg, ctx); /* If an expiration time has been given use that. If none has been * given the default from gpg.conf is used. To make sure not to set * an expiration time at all the flag GPGME_KEYSIGN_NOEXPIRE can be * used. */ if (!err && (expire || (flags & GPGME_KEYSIGN_NOEXPIRE))) { char tmpbuf[8+20]; if ((flags & GPGME_KEYSIGN_NOEXPIRE)) expire = 0; snprintf (tmpbuf, sizeof tmpbuf, "seconds=%lu", expire); err = add_arg (gpg, "--default-cert-expire"); if (!err) err = add_arg (gpg, tmpbuf); } if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, key->fpr); if (!err && userid) { if ((flags & GPGME_KEYSIGN_LFSEP)) { for (; !err && (s = strchr (userid, '\n')); userid = s + 1) if ((s - userid)) err = add_arg_len (gpg, "=", userid, s - userid); if (!err && *userid) err = add_arg_pfx (gpg, "=", userid); } else err = add_arg_pfx (gpg, "=", userid); } if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_tofu_policy (void *engine, gpgme_key_t key, gpgme_tofu_policy_t policy) { engine_gpg_t gpg = engine; gpgme_error_t err; const char *policystr = NULL; if (!key || !key->fpr) return gpg_error (GPG_ERR_INV_ARG); switch (policy) { case GPGME_TOFU_POLICY_NONE: break; case GPGME_TOFU_POLICY_AUTO: policystr = "auto"; break; case GPGME_TOFU_POLICY_GOOD: policystr = "good"; break; case GPGME_TOFU_POLICY_BAD: policystr = "bad"; break; case GPGME_TOFU_POLICY_ASK: policystr = "ask"; break; case GPGME_TOFU_POLICY_UNKNOWN: policystr = "unknown"; break; } if (!policystr) return gpg_error (GPG_ERR_INV_VALUE); if (!have_gpg_version (gpg, "2.1.10")) return gpg_error (GPG_ERR_NOT_SUPPORTED); err = add_arg (gpg, "--tofu-policy"); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, policystr); if (!err) err = add_arg (gpg, key->fpr); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_sign (void *engine, gpgme_data_t in, gpgme_data_t out, gpgme_sig_mode_t mode, int use_armor, int use_textmode, int include_certs, gpgme_ctx_t ctx /* FIXME */) { engine_gpg_t gpg = engine; gpgme_error_t err; (void)include_certs; if (mode == GPGME_SIG_MODE_CLEAR) err = add_arg (gpg, "--clearsign"); else { err = add_arg (gpg, "--sign"); if (!err && mode == GPGME_SIG_MODE_DETACH) err = add_arg (gpg, "--detach"); if (!err && use_armor) err = add_arg (gpg, "--armor"); if (!err) { if (gpgme_data_get_encoding (in) == GPGME_DATA_ENCODING_MIME && have_gpg_version (gpg, "2.1.14")) err = add_arg (gpg, "--mimemode"); else if (use_textmode) err = add_arg (gpg, "--textmode"); } } if (!err) err = append_args_from_signers (gpg, ctx); if (!err) err = append_args_from_sender (gpg, ctx); if (!err) err = append_args_from_sig_notations (gpg, ctx); if (gpgme_data_get_file_name (in)) { if (!err) err = add_arg (gpg, "--set-filename"); if (!err) err = add_arg (gpg, gpgme_data_get_file_name (in)); } /* Tell the gpg object about the data. */ if (!err) err = add_input_size_hint (gpg, in); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, in, -1, 0); if (!err) err = add_data (gpg, out, 1, 1); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_trustlist (void *engine, const char *pattern) { engine_gpg_t gpg = engine; gpgme_error_t err; err = add_arg (gpg, "--with-colons"); if (!err) err = add_arg (gpg, "--list-trust-path"); /* Tell the gpg object about the data. */ if (!err) err = add_arg (gpg, "--"); if (!err) err = add_arg (gpg, pattern); if (!err) err = start (gpg); return err; } static gpgme_error_t gpg_verify (void *engine, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext, gpgme_ctx_t ctx) { engine_gpg_t gpg = engine; gpgme_error_t err; err = append_args_from_sender (gpg, ctx); if (!err && ctx->auto_key_retrieve) err = add_arg (gpg, "--auto-key-retrieve"); if (err) ; else if (plaintext) { /* Normal or cleartext signature. */ err = add_arg (gpg, "--output"); if (!err) err = add_arg (gpg, "-"); if (!err) err = add_input_size_hint (gpg, sig); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, sig, -1, 0); if (!err) err = add_data (gpg, plaintext, 1, 1); } else { err = add_arg (gpg, "--verify"); if (!err) err = add_input_size_hint (gpg, signed_text); if (!err) err = add_arg (gpg, "--"); if (!err) err = add_data (gpg, sig, -1, 0); if (!err && signed_text) err = add_data (gpg, signed_text, -1, 0); } if (!err) err = start (gpg); return err; } static void gpg_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs) { engine_gpg_t gpg = engine; gpg->io_cbs = *io_cbs; } static gpgme_error_t gpg_set_pinentry_mode (void *engine, gpgme_pinentry_mode_t mode) { engine_gpg_t gpg = engine; gpg->pinentry_mode = mode; return 0; } static gpgme_error_t gpg_getauditlog (void *engine, gpgme_data_t output, unsigned int flags) { engine_gpg_t gpg = engine; #define MYBUFLEN 4096 char buf[MYBUFLEN]; int nread; int any_written = 0; if (!(flags & GPGME_AUDITLOG_DIAG)) { return gpg_error (GPG_ERR_NOT_IMPLEMENTED); } if (!gpg || !output) { return gpg_error (GPG_ERR_INV_VALUE); } if (!gpg->diagnostics) { return gpg_error (GPG_ERR_GENERAL); } gpgme_data_rewind (gpg->diagnostics); while ((nread = gpgme_data_read (gpg->diagnostics, buf, MYBUFLEN)) > 0) { any_written = 1; if (gpgme_data_write (output, buf, nread) == -1) return gpg_error_from_syserror (); } if (!any_written) { return gpg_error (GPG_ERR_NO_DATA); } if (nread == -1) return gpg_error_from_syserror (); gpgme_data_rewind (output); return 0; #undef MYBUFLEN } struct engine_ops _gpgme_engine_ops_gpg = { /* Static functions. */ _gpgme_get_default_gpg_name, NULL, gpg_get_version, gpg_get_req_version, gpg_new, /* Member functions. */ gpg_release, NULL, /* reset */ gpg_set_status_cb, gpg_set_status_handler, gpg_set_command_handler, gpg_set_colon_line_handler, gpg_set_locale, NULL, /* set_protocol */ gpg_set_engine_flags, /* set_engine_flags */ gpg_decrypt, gpg_delete, gpg_edit, gpg_encrypt, gpg_encrypt_sign, gpg_export, gpg_export_ext, gpg_genkey, gpg_import, gpg_keylist, gpg_keylist_ext, gpg_keylist_data, gpg_keysign, gpg_tofu_policy, /* tofu_policy */ gpg_sign, gpg_trustlist, gpg_verify, gpg_getauditlog, NULL, /* opassuan_transact */ NULL, /* conf_load */ NULL, /* conf_save */ NULL, /* conf_dir */ NULL, /* query_swdb */ gpg_set_io_cbs, gpg_io_event, gpg_cancel, NULL, /* cancel_op */ gpg_passwd, gpg_set_pinentry_mode, NULL /* opspawn */ }; diff --git a/src/engine-gpgconf.c b/src/engine-gpgconf.c index 33a1da01..303ba640 100644 --- a/src/engine-gpgconf.c +++ b/src/engine-gpgconf.c @@ -1,1321 +1,1322 @@ /* engine-gpgconf.c - gpg-conf engine. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, - 2013 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, + * 2013 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #ifdef HAVE_SYS_TYPES_H # include #endif #include #ifdef HAVE_UNISTD_H # include #endif #include /* FIXME */ #include #include "gpgme.h" #include "util.h" #include "ops.h" #include "wait.h" #include "priv-io.h" #include "sema.h" #include "assuan.h" #include "debug.h" #include "engine-backend.h" struct engine_gpgconf { char *file_name; char *home_dir; char *version; }; typedef struct engine_gpgconf *engine_gpgconf_t; /* Return true if the engine's version is at least VERSION. */ static int have_gpgconf_version (engine_gpgconf_t gpgconf, const char *version) { return _gpgme_compare_versions (gpgconf->version, version); } static char * gpgconf_get_version (const char *file_name) { return _gpgme_get_program_version (file_name ? file_name : _gpgme_get_default_gpgconf_name ()); } static const char * gpgconf_get_req_version (void) { return "2.0.4"; } static void gpgconf_release (void *engine) { engine_gpgconf_t gpgconf = engine; if (!gpgconf) return; if (gpgconf->file_name) free (gpgconf->file_name); if (gpgconf->home_dir) free (gpgconf->home_dir); if (gpgconf->version) free (gpgconf->version); free (gpgconf); } static gpgme_error_t gpgconf_new (void **engine, const char *file_name, const char *home_dir, const char *version) { gpgme_error_t err = 0; engine_gpgconf_t gpgconf; gpgconf = calloc (1, sizeof *gpgconf); if (!gpgconf) return gpg_error_from_syserror (); gpgconf->file_name = strdup (file_name ? file_name : _gpgme_get_default_gpgconf_name ()); if (!gpgconf->file_name) err = gpg_error_from_syserror (); if (!err && home_dir) { gpgconf->home_dir = strdup (home_dir); if (!gpgconf->home_dir) err = gpg_error_from_syserror (); } if (!err && version) { gpgconf->version = strdup (version); if (!gpgconf->version) err = gpg_error_from_syserror (); } if (err) gpgconf_release (gpgconf); else *engine = gpgconf; return err; } static void release_arg (gpgme_conf_arg_t arg, gpgme_conf_type_t alt_type) { while (arg) { gpgme_conf_arg_t next = arg->next; if (alt_type == GPGME_CONF_STRING) free (arg->value.string); free (arg); arg = next; } } static void release_opt (gpgme_conf_opt_t opt) { if (opt->name) free (opt->name); if (opt->description) free (opt->description); if (opt->argname) free (opt->argname); release_arg (opt->default_value, opt->alt_type); if (opt->default_description) free (opt->default_description); release_arg (opt->no_arg_value, opt->alt_type); release_arg (opt->value, opt->alt_type); release_arg (opt->new_value, opt->alt_type); free (opt); } static void release_comp (gpgme_conf_comp_t comp) { gpgme_conf_opt_t opt; if (comp->name) free (comp->name); if (comp->description) free (comp->description); if (comp->program_name) free (comp->program_name); opt = comp->options; while (opt) { gpgme_conf_opt_t next = opt->next; release_opt (opt); opt = next; } free (comp); } static void gpgconf_config_release (gpgme_conf_comp_t conf) { while (conf) { gpgme_conf_comp_t next = conf->next; release_comp (conf); conf = next; } } /* Read from gpgconf and pass line after line to the hook function. We put a limit of 64 k on the maximum size for a line. This should allow for quite a long "group" line, which is usually the longest line (mine is currently ~3k). */ static gpgme_error_t gpgconf_read (void *engine, const char *arg1, char *arg2, gpgme_error_t (*cb) (void *hook, char *line), void *hook) { struct engine_gpgconf *gpgconf = engine; gpgme_error_t err = 0; char *linebuf; size_t linebufsize; int linelen; char *argv[6]; int argc = 0; int rp[2]; struct spawn_fd_item_s cfd[] = { {-1, 1 /* STDOUT_FILENO */, -1, 0}, {-1, -1} }; int status; int nread; char *mark = NULL; /* _gpgme_engine_new guarantees that this is not NULL. */ argv[argc++] = gpgconf->file_name; if (gpgconf->home_dir && have_gpgconf_version (gpgconf, "2.1.13")) { argv[argc++] = (char*)"--homedir"; argv[argc++] = gpgconf->home_dir; } argv[argc++] = (char*)arg1; argv[argc++] = arg2; argv[argc] = NULL; assert (argc < DIM (argv)); if (_gpgme_io_pipe (rp, 1) < 0) return gpg_error_from_syserror (); cfd[0].fd = rp[1]; status = _gpgme_io_spawn (gpgconf->file_name, argv, IOSPAWN_FLAG_DETACHED, cfd, NULL, NULL, NULL); if (status < 0) { _gpgme_io_close (rp[0]); _gpgme_io_close (rp[1]); return gpg_error_from_syserror (); } linebufsize = 1024; /* Usually enough for conf lines. */ linebuf = malloc (linebufsize); if (!linebuf) { err = gpg_error_from_syserror (); goto leave; } linelen = 0; while ((nread = _gpgme_io_read (rp[0], linebuf + linelen, linebufsize - linelen - 1))) { char *line; const char *lastmark = NULL; size_t nused; if (nread < 0) { err = gpg_error_from_syserror (); goto leave; } linelen += nread; linebuf[linelen] = '\0'; for (line=linebuf; (mark = strchr (line, '\n')); line = mark+1 ) { lastmark = mark; if (mark > line && mark[-1] == '\r') mark[-1] = '\0'; else mark[0] = '\0'; /* Got a full line. Due to the CR removal code (which occurs only on Windows) we might be one-off and thus would see empty lines. Don't pass them to the callback. */ err = *line? (*cb) (hook, line) : 0; if (err) goto leave; } nused = lastmark? (lastmark + 1 - linebuf) : 0; memmove (linebuf, linebuf + nused, linelen - nused); linelen -= nused; if (!(linelen < linebufsize - 1)) { char *newlinebuf; if (linelen < 8 * 1024 - 1) linebufsize = 8 * 1024; else if (linelen < 64 * 1024 - 1) linebufsize = 64 * 1024; else { /* We reached our limit - give up. */ err = gpg_error (GPG_ERR_LINE_TOO_LONG); goto leave; } newlinebuf = realloc (linebuf, linebufsize); if (!newlinebuf) { err = gpg_error_from_syserror (); goto leave; } linebuf = newlinebuf; } } leave: free (linebuf); _gpgme_io_close (rp[0]); return err; } static gpgme_error_t gpgconf_config_load_cb (void *hook, char *line) { gpgme_conf_comp_t *comp_p = hook; gpgme_conf_comp_t comp = *comp_p; #define NR_FIELDS 16 char *field[NR_FIELDS]; int fields = 0; while (line && fields < NR_FIELDS) { field[fields++] = line; line = strchr (line, ':'); if (line) *(line++) = '\0'; } /* We require at least the first 3 fields. */ if (fields < 2) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Find the pointer to the new component in the list. */ while (comp && comp->next) comp = comp->next; if (comp) comp_p = &comp->next; comp = calloc (1, sizeof (*comp)); if (!comp) return gpg_error_from_syserror (); /* Prepare return value. */ comp->_last_opt_p = &comp->options; *comp_p = comp; comp->name = strdup (field[0]); if (!comp->name) return gpg_error_from_syserror (); comp->description = strdup (field[1]); if (!comp->description) return gpg_error_from_syserror (); if (fields >= 3) { comp->program_name = strdup (field[2]); if (!comp->program_name) return gpg_error_from_syserror (); } return 0; } static gpgme_error_t gpgconf_parse_option (gpgme_conf_opt_t opt, gpgme_conf_arg_t *arg_p, char *line) { gpgme_error_t err; char *mark = NULL; if (!line[0]) return 0; while (line) { gpgme_conf_arg_t arg; if (opt->type != GPGME_CONF_STRING) mark = strchr (line, ','); if (mark) *mark = '\0'; arg = calloc (1, sizeof (*arg)); if (!arg) return gpg_error_from_syserror (); *arg_p = arg; arg_p = &arg->next; if (*line == '\0') arg->no_arg = 1; else { switch (opt->alt_type) { /* arg->value.count is an alias for arg->value.uint32. */ case GPGME_CONF_NONE: case GPGME_CONF_UINT32: arg->value.uint32 = strtoul (line, NULL, 0); break; case GPGME_CONF_INT32: arg->value.uint32 = strtol (line, NULL, 0); break; case GPGME_CONF_STRING: /* The complex types below are only here to silent the compiler warning. */ case GPGME_CONF_FILENAME: case GPGME_CONF_LDAP_SERVER: case GPGME_CONF_KEY_FPR: case GPGME_CONF_PUB_KEY: case GPGME_CONF_SEC_KEY: case GPGME_CONF_ALIAS_LIST: /* Skip quote character. */ line++; err = _gpgme_decode_percent_string (line, &arg->value.string, 0, 0); if (err) return err; break; } } /* Find beginning of next value. */ if (mark++ && *mark) line = mark; else line = NULL; } return 0; } static gpgme_error_t gpgconf_config_load_cb2 (void *hook, char *line) { gpgme_error_t err; gpgme_conf_comp_t comp = hook; gpgme_conf_opt_t *opt_p = comp->_last_opt_p; gpgme_conf_opt_t opt; #define NR_FIELDS 16 char *field[NR_FIELDS]; int fields = 0; while (line && fields < NR_FIELDS) { field[fields++] = line; line = strchr (line, ':'); if (line) *(line++) = '\0'; } /* We require at least the first 10 fields. */ if (fields < 10) return trace_gpg_error (GPG_ERR_INV_ENGINE); opt = calloc (1, sizeof (*opt)); if (!opt) return gpg_error_from_syserror (); comp->_last_opt_p = &opt->next; *opt_p = opt; if (field[0][0]) { opt->name = strdup (field[0]); if (!opt->name) return gpg_error_from_syserror (); } opt->flags = strtoul (field[1], NULL, 0); opt->level = strtoul (field[2], NULL, 0); if (field[3][0]) { opt->description = strdup (field[3]); if (!opt->description) return gpg_error_from_syserror (); } opt->type = strtoul (field[4], NULL, 0); opt->alt_type = strtoul (field[5], NULL, 0); if (field[6][0]) { opt->argname = strdup (field[6]); if (!opt->argname) return gpg_error_from_syserror (); } if (opt->flags & GPGME_CONF_DEFAULT) { err = gpgconf_parse_option (opt, &opt->default_value, field[7]); if (err) return err; } else if ((opt->flags & GPGME_CONF_DEFAULT_DESC) && field[7][0]) { opt->default_description = strdup (field[7]); if (!opt->default_description) return gpg_error_from_syserror (); } if (opt->flags & GPGME_CONF_NO_ARG_DESC) { opt->no_arg_description = strdup (field[8]); if (!opt->no_arg_description) return gpg_error_from_syserror (); } else { err = gpgconf_parse_option (opt, &opt->no_arg_value, field[8]); if (err) return err; } err = gpgconf_parse_option (opt, &opt->value, field[9]); if (err) return err; return 0; } static gpgme_error_t gpgconf_conf_load (void *engine, gpgme_conf_comp_t *comp_p) { gpgme_error_t err; gpgme_conf_comp_t comp = NULL; gpgme_conf_comp_t cur_comp; *comp_p = NULL; err = gpgconf_read (engine, "--list-components", NULL, gpgconf_config_load_cb, &comp); if (err) { gpgconf_release (comp); return err; } cur_comp = comp; while (!err && cur_comp) { err = gpgconf_read (engine, "--list-options", cur_comp->name, gpgconf_config_load_cb2, cur_comp); cur_comp = cur_comp->next; } if (err) { gpgconf_release (comp); return err; } *comp_p = comp; return 0; } gpgme_error_t _gpgme_conf_arg_new (gpgme_conf_arg_t *arg_p, gpgme_conf_type_t type, const void *value) { gpgme_conf_arg_t arg; arg = calloc (1, sizeof (*arg)); if (!arg) return gpg_error_from_syserror (); if (!value) arg->no_arg = 1; else { /* We need to switch on type here because the alt-type is not yet known. */ switch (type) { case GPGME_CONF_NONE: case GPGME_CONF_UINT32: arg->value.uint32 = *((unsigned int *) value); break; case GPGME_CONF_INT32: arg->value.int32 = *((int *) value); break; case GPGME_CONF_STRING: case GPGME_CONF_FILENAME: case GPGME_CONF_LDAP_SERVER: case GPGME_CONF_KEY_FPR: case GPGME_CONF_PUB_KEY: case GPGME_CONF_SEC_KEY: case GPGME_CONF_ALIAS_LIST: arg->value.string = strdup (value); if (!arg->value.string) { free (arg); return gpg_error_from_syserror (); } break; default: free (arg); return gpg_error (GPG_ERR_INV_VALUE); } } *arg_p = arg; return 0; } void _gpgme_conf_arg_release (gpgme_conf_arg_t arg, gpgme_conf_type_t type) { /* Lacking the alt_type we need to switch on type here. */ switch (type) { case GPGME_CONF_NONE: case GPGME_CONF_UINT32: case GPGME_CONF_INT32: case GPGME_CONF_STRING: default: break; case GPGME_CONF_FILENAME: case GPGME_CONF_LDAP_SERVER: case GPGME_CONF_KEY_FPR: case GPGME_CONF_PUB_KEY: case GPGME_CONF_SEC_KEY: case GPGME_CONF_ALIAS_LIST: type = GPGME_CONF_STRING; break; } release_arg (arg, type); } gpgme_error_t _gpgme_conf_opt_change (gpgme_conf_opt_t opt, int reset, gpgme_conf_arg_t arg) { if (reset) { if (opt->new_value) release_arg (opt->new_value, opt->alt_type); opt->new_value = NULL; opt->change_value = 0; } else { /* Support self-assignment, for example for adding an item to an existing list. */ if (opt->new_value && arg != opt->new_value) release_arg (opt->new_value, opt->alt_type); opt->new_value = arg; opt->change_value = 1; } return 0; } /* FIXME: Major problem: We don't get errors from gpgconf. */ static gpgme_error_t gpgconf_write (void *engine, const char *arg1, char *arg2, gpgme_data_t conf) { struct engine_gpgconf *gpgconf = engine; gpgme_error_t err = 0; #define BUFLEN 1024 char buf[BUFLEN]; int buflen = 0; char *argv[7]; int argc = 0; int rp[2] = { -1, -1 }; int errp[2] = { -1, -1 }; struct spawn_fd_item_s cfd[] = { {-1, 0 /* STDIN_FILENO */}, {-1, 2 /* STDERR_FILENO */, -1}, {-1, -1} }; int status; int nwrite; /* _gpgme_engine_new guarantees that this is not NULL. */ argv[argc++] = gpgconf->file_name; if (gpgconf->home_dir && have_gpgconf_version (gpgconf, "2.1.13")) { argv[argc++] = (char*)"--homedir"; argv[argc++] = gpgconf->home_dir; } argv[argc++] = (char*)"--runtime"; argv[argc++] = (char*)arg1; argv[argc++] = arg2; argv[argc] = NULL; assert (argc < DIM (argv)); if (_gpgme_io_pipe (rp, 0) < 0) { err = gpg_error_from_syserror (); goto leave; } if (_gpgme_io_pipe (errp, 1) < 0) { err = gpg_error_from_syserror (); goto leave; } cfd[0].fd = rp[0]; cfd[1].fd = errp[1]; status = _gpgme_io_spawn (gpgconf->file_name, argv, IOSPAWN_FLAG_DETACHED, cfd, NULL, NULL, NULL); if (status < 0) { err = gpg_error_from_syserror (); goto leave; } rp[0] = -1; errp[1] = -1; for (;;) { if (buflen == 0) { do { buflen = gpgme_data_read (conf, buf, BUFLEN); } while (buflen < 0 && errno == EAGAIN); if (buflen < 0) { err = gpg_error_from_syserror (); goto leave; } else if (buflen == 0) { /* All is written. */ _gpgme_io_close (rp[1]); rp[1] = -1; for (;;) { do { buflen = _gpgme_io_read (errp[0], buf, BUFLEN); } while (buflen < 0 && errno == EAGAIN); if (buflen == 0) { err = 0; goto leave; } /* XXX: Do something useful with BUF. */ } } } do { nwrite = _gpgme_io_write (rp[1], buf, buflen); } while (nwrite < 0 && errno == EAGAIN); if (nwrite > 0) { buflen -= nwrite; if (buflen > 0) memmove (&buf[0], &buf[nwrite], buflen); } else if (nwrite < 0) { err = gpg_error_from_syserror (); goto leave; } } assert (! "reached"); leave: if (rp[0] != -1) _gpgme_io_close (rp[0]); if (rp[1] != -1) _gpgme_io_close (rp[1]); if (errp[0] != -1) _gpgme_io_close (errp[0]); if (errp[1] != -1) _gpgme_io_close (errp[1]); return err; } static gpgme_error_t arg_to_data (gpgme_data_t conf, gpgme_conf_opt_t option, gpgme_conf_arg_t arg) { gpgme_error_t err = 0; int amt = 0; char buf[16]; while (amt >= 0 && arg) { switch (option->alt_type) { case GPGME_CONF_NONE: case GPGME_CONF_UINT32: default: snprintf (buf, sizeof (buf), "%u", arg->value.uint32); buf[sizeof (buf) - 1] = '\0'; amt = gpgme_data_write (conf, buf, strlen (buf)); break; case GPGME_CONF_INT32: snprintf (buf, sizeof (buf), "%i", arg->value.uint32); buf[sizeof (buf) - 1] = '\0'; amt = gpgme_data_write (conf, buf, strlen (buf)); break; case GPGME_CONF_STRING: /* The complex types below are only here to silent the compiler warning. */ case GPGME_CONF_FILENAME: case GPGME_CONF_LDAP_SERVER: case GPGME_CONF_KEY_FPR: case GPGME_CONF_PUB_KEY: case GPGME_CONF_SEC_KEY: case GPGME_CONF_ALIAS_LIST: if (arg->value.string) { /* One quote character, and three times to allow for percent escaping. */ char *ptr = arg->value.string; amt = gpgme_data_write (conf, "\"", 1); if (amt < 0) break; while (!err && *ptr) { switch (*ptr) { case '%': amt = gpgme_data_write (conf, "%25", 3); break; case ':': amt = gpgme_data_write (conf, "%3a", 3); break; case ',': amt = gpgme_data_write (conf, "%2c", 3); break; default: amt = gpgme_data_write (conf, ptr, 1); } ptr++; } } break; } if (amt < 0) break; arg = arg->next; /* Comma separator. */ if (arg) amt = gpgme_data_write (conf, ",", 1); } if (amt < 0) return gpg_error_from_syserror (); return 0; } static gpgme_error_t gpgconf_conf_save (void *engine, gpgme_conf_comp_t comp) { gpgme_error_t err; int amt = 0; /* We use a data object to store the new configuration. */ gpgme_data_t conf; gpgme_conf_opt_t option; int something_changed = 0; err = gpgme_data_new (&conf); if (err) return err; option = comp->options; while (!err && amt >= 0 && option) { if (option->change_value) { unsigned int flags = 0; char buf[16]; something_changed = 1; amt = gpgme_data_write (conf, option->name, strlen (option->name)); if (amt >= 0) amt = gpgme_data_write (conf, ":", 1); if (amt < 0) break; if (!option->new_value) flags |= GPGME_CONF_DEFAULT; snprintf (buf, sizeof (buf), "%u", flags); buf[sizeof (buf) - 1] = '\0'; amt = gpgme_data_write (conf, buf, strlen (buf)); if (amt >= 0) amt = gpgme_data_write (conf, ":", 1); if (amt < 0) break; if (option->new_value) { err = arg_to_data (conf, option, option->new_value); if (err) break; } amt = gpgme_data_write (conf, "\n", 1); } option = option->next; } if (!err && amt < 0) err = gpg_error_from_syserror (); if (err || !something_changed) goto bail; err = gpgme_data_seek (conf, 0, SEEK_SET); if (err) goto bail; err = gpgconf_write (engine, "--change-options", comp->name, conf); bail: gpgme_data_release (conf); return err; } struct gpgconf_config_dir_s { const char *what; char *result; }; /* Called for each line in the gpgconf --list-dirs output. Searches for the desired line and returns the result, indicating success by a special error value GPG_ERR_USER_1 (which terminates the operation immediately). */ static gpgme_error_t gpgconf_config_dir_cb (void *hook, char *line) { /* This is an input- and output-parameter. */ struct gpgconf_config_dir_s *data = (struct gpgconf_config_dir_s *) hook; int len = strlen(data->what); if (!strncmp(line, data->what, len) && line[len] == ':') { char *result = strdup(&line[len + 1]); if (!result) return gpg_error_from_syserror (); data->result = result; return gpg_error(GPG_ERR_USER_1); } return 0; } /* Like gpgme_get_dirinfo, but uses the home directory of ENGINE and does not cache the result. */ static gpgme_error_t gpgconf_conf_dir (void *engine, const char *what, char **result) { gpgme_error_t err; struct gpgconf_config_dir_s data; data.what = what; data.result = NULL; err = gpgconf_read (engine, "--list-dirs", NULL, gpgconf_config_dir_cb, &data); if (gpg_err_code (err) == GPG_ERR_USER_1) { /* This signals to us that a result was found. */ *result = data.result; return 0; } if (!err) err = gpg_error(GPG_ERR_NOT_FOUND); return 0; } /* Parse a line received from gpgconf --query-swdb. This function may * modify LINE. The result is stored at RESULT. */ static gpg_error_t parse_swdb_line (char *line, gpgme_query_swdb_result_t result) { char *field[9]; int fields = 0; gpg_err_code_t ec; while (line && fields < DIM (field)) { field[fields++] = line; line = strchr (line, ':'); if (line) *line++ = 0; } /* We require that all fields exists - gpgme emits all these fields * even on error. They might be empty, though. */ if (fields < 9) return gpg_error (GPG_ERR_INV_ENGINE); free (result->name); result->name = strdup (field[0]); if (!result->name) return gpg_error_from_syserror (); free (result->iversion); result->iversion = strdup (field[1]); if (!result->iversion) return gpg_error_from_syserror (); result->urgent = (strtol (field[3], NULL, 10) > 0); ec = gpg_err_code (strtoul (field[4], NULL, 10)); result->created = _gpgme_parse_timestamp (field[5], NULL); result->retrieved= _gpgme_parse_timestamp (field[6], NULL); free (result->version); result->version = strdup (field[7]); if (!result->version) return gpg_error_from_syserror (); result->reldate = _gpgme_parse_timestamp (field[8], NULL); /* Set other flags. */ result->warning = !!ec; result->update = 0; result->noinfo = 0; result->unknown = 0; result->tooold = 0; result->error = 0; switch (*field[2]) { case '-': result->warning = 1; break; case '?': result->unknown = result->warning = 1; break; case 'u': result->update = 1; break; case 'c': break; case 'n': break; default: result->warning = 1; if (!ec) ec = GPG_ERR_INV_ENGINE; break; } if (ec == GPG_ERR_TOO_OLD) result->tooold = 1; else if (ec == GPG_ERR_ENOENT) result->noinfo = 1; else if (ec) result->error = 1; return 0; } static gpgme_error_t gpgconf_query_swdb (void *engine, const char *name, const char *iversion, gpgme_query_swdb_result_t result) { struct engine_gpgconf *gpgconf = engine; gpgme_error_t err = 0; char *linebuf; size_t linebufsize; int linelen; char *argv[7]; int argc = 0; int rp[2]; struct spawn_fd_item_s cfd[] = { {-1, 1 /* STDOUT_FILENO */, -1, 0}, {-1, -1} }; int status; int nread; char *mark = NULL; if (!have_gpgconf_version (gpgconf, "2.1.16")) return gpg_error (GPG_ERR_ENGINE_TOO_OLD); /* _gpgme_engine_new guarantees that this is not NULL. */ argv[argc++] = gpgconf->file_name; if (gpgconf->home_dir) { argv[argc++] = (char*)"--homedir"; argv[argc++] = gpgconf->home_dir; } argv[argc++] = (char*)"--query-swdb"; argv[argc++] = (char*)name; argv[argc++] = (char*)iversion; argv[argc] = NULL; assert (argc < DIM (argv)); if (_gpgme_io_pipe (rp, 1) < 0) return gpg_error_from_syserror (); cfd[0].fd = rp[1]; status = _gpgme_io_spawn (gpgconf->file_name, argv, IOSPAWN_FLAG_DETACHED, cfd, NULL, NULL, NULL); if (status < 0) { _gpgme_io_close (rp[0]); _gpgme_io_close (rp[1]); return gpg_error_from_syserror (); } linebufsize = 2048; /* Same as used by gpgconf. */ linebuf = malloc (linebufsize); if (!linebuf) { err = gpg_error_from_syserror (); goto leave; } linelen = 0; while ((nread = _gpgme_io_read (rp[0], linebuf + linelen, linebufsize - linelen - 1))) { char *line; const char *lastmark = NULL; size_t nused; if (nread < 0) { err = gpg_error_from_syserror (); goto leave; } linelen += nread; linebuf[linelen] = '\0'; for (line=linebuf; (mark = strchr (line, '\n')); line = mark+1 ) { lastmark = mark; if (mark > line && mark[-1] == '\r') mark[-1] = '\0'; else mark[0] = '\0'; /* Got a full line. Due to the CR removal code (which occurs only on Windows) we might be one-off and thus would see empty lines. */ if (*line) { err = parse_swdb_line (line, result); goto leave; /* Ready. */ } else /* empty line. */ err = 0; } nused = lastmark? (lastmark + 1 - linebuf) : 0; memmove (linebuf, linebuf + nused, linelen - nused); linelen -= nused; if (!(linelen < linebufsize - 1)) { char *newlinebuf; if (linelen < 8 * 1024 - 1) linebufsize = 8 * 1024; else if (linelen < 64 * 1024 - 1) linebufsize = 64 * 1024; else { /* We reached our limit - give up. */ err = gpg_error (GPG_ERR_LINE_TOO_LONG); goto leave; } newlinebuf = realloc (linebuf, linebufsize); if (!newlinebuf) { err = gpg_error_from_syserror (); goto leave; } linebuf = newlinebuf; } } leave: free (linebuf); _gpgme_io_close (rp[0]); return err; } static void gpgconf_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs) { (void)engine; (void)io_cbs; /* Nothing to do. */ } /* Currently, we do not use the engine interface for the various operations. */ void _gpgme_conf_release (gpgme_conf_comp_t conf) { gpgconf_config_release (conf); } struct engine_ops _gpgme_engine_ops_gpgconf = { /* Static functions. */ _gpgme_get_default_gpgconf_name, NULL, gpgconf_get_version, gpgconf_get_req_version, gpgconf_new, /* Member functions. */ gpgconf_release, NULL, /* reset */ NULL, /* set_status_cb */ NULL, /* set_status_handler */ NULL, /* set_command_handler */ NULL, /* set_colon_line_handler */ NULL, /* set_locale */ NULL, /* set_protocol */ NULL, /* set_engine_flags */ NULL, /* decrypt */ NULL, /* delete */ NULL, /* edit */ NULL, /* encrypt */ NULL, /* encrypt_sign */ NULL, /* export */ NULL, /* export_ext */ NULL, /* genkey */ NULL, /* import */ NULL, /* keylist */ NULL, /* keylist_ext */ NULL, /* keylist_data */ NULL, /* keysign */ NULL, /* tofu_policy */ NULL, /* sign */ NULL, /* trustlist */ NULL, /* verify */ NULL, /* getauditlog */ NULL, /* opassuan_transact */ gpgconf_conf_load, gpgconf_conf_save, gpgconf_conf_dir, gpgconf_query_swdb, gpgconf_set_io_cbs, NULL, /* io_event */ NULL, /* cancel */ NULL, /* cancel_op */ NULL, /* passwd */ NULL, /* set_pinentry_mode */ NULL /* opspawn */ }; diff --git a/src/engine-gpgsm.c b/src/engine-gpgsm.c index 5700990b..a13af7fb 100644 --- a/src/engine-gpgsm.c +++ b/src/engine-gpgsm.c @@ -1,2240 +1,2240 @@ /* engine-gpgsm.c - GpgSM engine. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009, - 2010 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009, + * 2010 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #ifdef HAVE_SYS_TYPES_H # include #endif #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_LOCALE_H #include #endif #include /* FIXME */ #include "gpgme.h" #include "util.h" #include "ops.h" #include "wait.h" #include "priv-io.h" #include "sema.h" #include "data.h" #include "assuan.h" #include "debug.h" #include "engine-backend.h" typedef struct { int fd; /* FD we talk about. */ int server_fd;/* Server FD for this connection. */ int dir; /* Inbound/Outbound, maybe given implicit? */ void *data; /* Handler-specific data. */ void *tag; /* ID from the user for gpgme_remove_io_callback. */ char server_fd_str[15]; /* Same as SERVER_FD but as a string. We need this because _gpgme_io_fd2str can't be used on a closed descriptor. */ } iocb_data_t; struct engine_gpgsm { assuan_context_t assuan_ctx; int lc_ctype_set; int lc_messages_set; iocb_data_t status_cb; /* Input, output etc are from the servers perspective. */ iocb_data_t input_cb; gpgme_data_t input_helper_data; /* Input helper data object. */ void *input_helper_memory; /* Input helper memory block. */ iocb_data_t output_cb; iocb_data_t message_cb; struct { engine_status_handler_t fnc; void *fnc_value; gpgme_status_cb_t mon_cb; void *mon_cb_value; } status; struct { engine_colon_line_handler_t fnc; void *fnc_value; struct { char *line; int linesize; int linelen; } attic; int any; /* any data line seen */ } colon; gpgme_data_t inline_data; /* Used to collect D lines. */ char request_origin[10]; struct gpgme_io_cbs io_cbs; }; typedef struct engine_gpgsm *engine_gpgsm_t; static void gpgsm_io_event (void *engine, gpgme_event_io_t type, void *type_data); static char * gpgsm_get_version (const char *file_name) { return _gpgme_get_program_version (file_name ? file_name : _gpgme_get_default_gpgsm_name ()); } static const char * gpgsm_get_req_version (void) { return "2.0.4"; } static void close_notify_handler (int fd, void *opaque) { engine_gpgsm_t gpgsm = opaque; assert (fd != -1); if (gpgsm->status_cb.fd == fd) { if (gpgsm->status_cb.tag) (*gpgsm->io_cbs.remove) (gpgsm->status_cb.tag); gpgsm->status_cb.fd = -1; gpgsm->status_cb.tag = NULL; } else if (gpgsm->input_cb.fd == fd) { if (gpgsm->input_cb.tag) (*gpgsm->io_cbs.remove) (gpgsm->input_cb.tag); gpgsm->input_cb.fd = -1; gpgsm->input_cb.tag = NULL; if (gpgsm->input_helper_data) { gpgme_data_release (gpgsm->input_helper_data); gpgsm->input_helper_data = NULL; } if (gpgsm->input_helper_memory) { free (gpgsm->input_helper_memory); gpgsm->input_helper_memory = NULL; } } else if (gpgsm->output_cb.fd == fd) { if (gpgsm->output_cb.tag) (*gpgsm->io_cbs.remove) (gpgsm->output_cb.tag); gpgsm->output_cb.fd = -1; gpgsm->output_cb.tag = NULL; } else if (gpgsm->message_cb.fd == fd) { if (gpgsm->message_cb.tag) (*gpgsm->io_cbs.remove) (gpgsm->message_cb.tag); gpgsm->message_cb.fd = -1; gpgsm->message_cb.tag = NULL; } } /* This is the default inquiry callback. We use it to handle the Pinentry notifications. */ static gpgme_error_t default_inq_cb (engine_gpgsm_t gpgsm, const char *line) { (void)gpgsm; if (!strncmp (line, "PINENTRY_LAUNCHED", 17) && (line[17]==' '||!line[17])) { _gpgme_allow_set_foreground_window ((pid_t)strtoul (line+17, NULL, 10)); } return 0; } static gpgme_error_t gpgsm_cancel (void *engine) { engine_gpgsm_t gpgsm = engine; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (gpgsm->status_cb.fd != -1) _gpgme_io_close (gpgsm->status_cb.fd); if (gpgsm->input_cb.fd != -1) _gpgme_io_close (gpgsm->input_cb.fd); if (gpgsm->output_cb.fd != -1) _gpgme_io_close (gpgsm->output_cb.fd); if (gpgsm->message_cb.fd != -1) _gpgme_io_close (gpgsm->message_cb.fd); if (gpgsm->assuan_ctx) { assuan_release (gpgsm->assuan_ctx); gpgsm->assuan_ctx = NULL; } return 0; } static void gpgsm_release (void *engine) { engine_gpgsm_t gpgsm = engine; if (!gpgsm) return; gpgsm_cancel (engine); free (gpgsm->colon.attic.line); free (gpgsm); } static gpgme_error_t gpgsm_new (void **engine, const char *file_name, const char *home_dir, const char *version) { gpgme_error_t err = 0; engine_gpgsm_t gpgsm; const char *pgmname; const char *argv[5]; int argc; #if !USE_DESCRIPTOR_PASSING int fds[2]; int child_fds[4]; #endif char *dft_display = NULL; char dft_ttyname[64]; char *env_tty = NULL; char *dft_ttytype = NULL; char *optstr; (void)version; /* Not yet used. */ gpgsm = calloc (1, sizeof *gpgsm); if (!gpgsm) return gpg_error_from_syserror (); gpgsm->status_cb.fd = -1; gpgsm->status_cb.dir = 1; gpgsm->status_cb.tag = 0; gpgsm->status_cb.data = gpgsm; gpgsm->input_cb.fd = -1; gpgsm->input_cb.dir = 0; gpgsm->input_cb.tag = 0; gpgsm->input_cb.server_fd = -1; *gpgsm->input_cb.server_fd_str = 0; gpgsm->output_cb.fd = -1; gpgsm->output_cb.dir = 1; gpgsm->output_cb.tag = 0; gpgsm->output_cb.server_fd = -1; *gpgsm->output_cb.server_fd_str = 0; gpgsm->message_cb.fd = -1; gpgsm->message_cb.dir = 0; gpgsm->message_cb.tag = 0; gpgsm->message_cb.server_fd = -1; *gpgsm->message_cb.server_fd_str = 0; gpgsm->status.fnc = 0; gpgsm->colon.fnc = 0; gpgsm->colon.attic.line = 0; gpgsm->colon.attic.linesize = 0; gpgsm->colon.attic.linelen = 0; gpgsm->colon.any = 0; gpgsm->inline_data = NULL; gpgsm->io_cbs.add = NULL; gpgsm->io_cbs.add_priv = NULL; gpgsm->io_cbs.remove = NULL; gpgsm->io_cbs.event = NULL; gpgsm->io_cbs.event_priv = NULL; #if !USE_DESCRIPTOR_PASSING if (_gpgme_io_pipe (fds, 0) < 0) { err = gpg_error_from_syserror (); goto leave; } gpgsm->input_cb.fd = fds[1]; gpgsm->input_cb.server_fd = fds[0]; if (_gpgme_io_pipe (fds, 1) < 0) { err = gpg_error_from_syserror (); goto leave; } gpgsm->output_cb.fd = fds[0]; gpgsm->output_cb.server_fd = fds[1]; if (_gpgme_io_pipe (fds, 0) < 0) { err = gpg_error_from_syserror (); goto leave; } gpgsm->message_cb.fd = fds[1]; gpgsm->message_cb.server_fd = fds[0]; child_fds[0] = gpgsm->input_cb.server_fd; child_fds[1] = gpgsm->output_cb.server_fd; child_fds[2] = gpgsm->message_cb.server_fd; child_fds[3] = -1; #endif pgmname = file_name ? file_name : _gpgme_get_default_gpgsm_name (); argc = 0; argv[argc++] = _gpgme_get_basename (pgmname); if (home_dir) { argv[argc++] = "--homedir"; argv[argc++] = home_dir; } argv[argc++] = "--server"; argv[argc++] = NULL; err = assuan_new_ext (&gpgsm->assuan_ctx, GPG_ERR_SOURCE_GPGME, &_gpgme_assuan_malloc_hooks, _gpgme_assuan_log_cb, NULL); if (err) goto leave; assuan_ctx_set_system_hooks (gpgsm->assuan_ctx, &_gpgme_assuan_system_hooks); #if USE_DESCRIPTOR_PASSING err = assuan_pipe_connect (gpgsm->assuan_ctx, pgmname, argv, NULL, NULL, NULL, ASSUAN_PIPE_CONNECT_FDPASSING); #else { assuan_fd_t achild_fds[4]; int i; /* For now... */ for (i = 0; i < 4; i++) achild_fds[i] = (assuan_fd_t) child_fds[i]; err = assuan_pipe_connect (gpgsm->assuan_ctx, pgmname, argv, achild_fds, NULL, NULL, 0); /* For now... */ for (i = 0; i < 4; i++) child_fds[i] = (int) achild_fds[i]; } /* On Windows, handles are inserted in the spawned process with DuplicateHandle, and child_fds contains the server-local names for the inserted handles when assuan_pipe_connect returns. */ if (!err) { /* Note: We don't use _gpgme_io_fd2str here. On W32 the returned handles are real W32 system handles, not whatever GPGME uses internally (which may be a system handle, a C library handle or a GLib/Qt channel. Confusing, yes, but remember these are server-local names, so they are not part of GPGME at all. */ snprintf (gpgsm->input_cb.server_fd_str, sizeof gpgsm->input_cb.server_fd_str, "%d", child_fds[0]); snprintf (gpgsm->output_cb.server_fd_str, sizeof gpgsm->output_cb.server_fd_str, "%d", child_fds[1]); snprintf (gpgsm->message_cb.server_fd_str, sizeof gpgsm->message_cb.server_fd_str, "%d", child_fds[2]); } #endif if (err) goto leave; err = _gpgme_getenv ("DISPLAY", &dft_display); if (err) goto leave; if (dft_display) { if (gpgrt_asprintf (&optstr, "OPTION display=%s", dft_display) < 0) { free (dft_display); err = gpg_error_from_syserror (); goto leave; } free (dft_display); err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } err = _gpgme_getenv ("GPG_TTY", &env_tty); if (isatty (1) || env_tty || err) { int rc = 0; if (err) goto leave; else if (env_tty) { snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); free (env_tty); } else rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ if (!rc) { if (gpgrt_asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) { err = gpg_error_from_syserror (); goto leave; } err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; err = _gpgme_getenv ("TERM", &dft_ttytype); if (err) goto leave; if (dft_ttytype) { if (gpgrt_asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype)< 0) { free (dft_ttytype); err = gpg_error_from_syserror (); goto leave; } free (dft_ttytype); err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } } } /* Ask gpgsm to enable the audit log support. */ if (!err) { err = assuan_transact (gpgsm->assuan_ctx, "OPTION enable-audit-log=1", NULL, NULL, NULL, NULL, NULL, NULL); if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION) err = 0; /* This is an optional feature of gpgsm. */ } #ifdef HAVE_W32_SYSTEM /* Under Windows we need to use AllowSetForegroundWindow. Tell gpgsm to tell us when it needs it. */ if (!err) { err = assuan_transact (gpgsm->assuan_ctx, "OPTION allow-pinentry-notify", NULL, NULL, NULL, NULL, NULL, NULL); if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION) err = 0; /* This is a new feature of gpgsm. */ } #endif /*HAVE_W32_SYSTEM*/ #if !USE_DESCRIPTOR_PASSING if (!err && (_gpgme_io_set_close_notify (gpgsm->input_cb.fd, close_notify_handler, gpgsm) || _gpgme_io_set_close_notify (gpgsm->output_cb.fd, close_notify_handler, gpgsm) || _gpgme_io_set_close_notify (gpgsm->message_cb.fd, close_notify_handler, gpgsm))) { err = gpg_error (GPG_ERR_GENERAL); goto leave; } #endif leave: /* Close the server ends of the pipes (because of this, we must use the stored server_fd_str in the function start). Our ends are closed in gpgsm_release(). */ #if !USE_DESCRIPTOR_PASSING if (gpgsm->input_cb.server_fd != -1) _gpgme_io_close (gpgsm->input_cb.server_fd); if (gpgsm->output_cb.server_fd != -1) _gpgme_io_close (gpgsm->output_cb.server_fd); if (gpgsm->message_cb.server_fd != -1) _gpgme_io_close (gpgsm->message_cb.server_fd); #endif if (err) gpgsm_release (gpgsm); else *engine = gpgsm; return err; } /* Copy flags from CTX into the engine object. */ static void gpgsm_set_engine_flags (void *engine, const gpgme_ctx_t ctx) { engine_gpgsm_t gpgsm = engine; if (ctx->request_origin) { if (strlen (ctx->request_origin) + 1 > sizeof gpgsm->request_origin) strcpy (gpgsm->request_origin, "xxx"); /* Too long - force error */ else strcpy (gpgsm->request_origin, ctx->request_origin); } else *gpgsm->request_origin = 0; } static gpgme_error_t gpgsm_set_locale (void *engine, int category, const char *value) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; char *optstr; const char *catstr; /* FIXME: If value is NULL, we need to reset the option to default. But we can't do this. So we error out here. GPGSM needs support for this. */ if (0) ; #ifdef LC_CTYPE else if (category == LC_CTYPE) { catstr = "lc-ctype"; if (!value && gpgsm->lc_ctype_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) gpgsm->lc_ctype_set = 1; } #endif #ifdef LC_MESSAGES else if (category == LC_MESSAGES) { catstr = "lc-messages"; if (!value && gpgsm->lc_messages_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) gpgsm->lc_messages_set = 1; } #endif /* LC_MESSAGES */ else return gpg_error (GPG_ERR_INV_VALUE); /* FIXME: Reset value to default. */ if (!value) return 0; if (gpgrt_asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) err = gpg_error_from_syserror (); else { err = assuan_transact (gpgsm->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); } return err; } static gpgme_error_t gpgsm_assuan_simple_command (engine_gpgsm_t gpgsm, const char *cmd, engine_status_handler_t status_fnc, void *status_fnc_value) { assuan_context_t ctx = gpgsm->assuan_ctx; gpg_error_t err, cb_err; char *line; size_t linelen; err = assuan_write_line (ctx, cmd); if (err) return err; cb_err = 0; do { err = assuan_read_line (ctx, &line, &linelen); if (err) break; if (*line == '#' || !linelen) continue; if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) break; else if (linelen >= 4 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && line[3] == ' ') { /* We prefer a callback generated error because that one is more related to gpgme and thus probably more important than the error returned by the engine. */ err = cb_err? cb_err : atoi (&line[4]); cb_err = 0; } else if (linelen >= 2 && line[0] == 'S' && line[1] == ' ') { /* After an error from a status callback we skip all further status lines. */ if (!cb_err) { char *rest; gpgme_status_code_t r; rest = strchr (line + 2, ' '); if (!rest) rest = line + linelen; /* set to an empty string */ else *(rest++) = 0; r = _gpgme_parse_status (line + 2); if (gpgsm->status.mon_cb && r != GPGME_STATUS_PROGRESS) { /* Note that we call the monitor even if we do * not know the status code (r < 0). */ cb_err = gpgsm->status.mon_cb (gpgsm->status.mon_cb_value, line + 2, rest); } if (r >= 0 && status_fnc && !cb_err) cb_err = status_fnc (status_fnc_value, r, rest); } } else { /* Invalid line or INQUIRY. We can't do anything else than to stop. As with ERR we prefer a status callback generated error code, though. */ err = cb_err ? cb_err : gpg_error (GPG_ERR_GENERAL); cb_err = 0; } } while (!err); /* We only want the first error from the status handler, thus we * take the one saved in CB_ERR. */ if (!err && cb_err) err = cb_err; return err; } typedef enum { INPUT_FD, OUTPUT_FD, MESSAGE_FD } fd_type_t; static void gpgsm_clear_fd (engine_gpgsm_t gpgsm, fd_type_t fd_type) { #if !USE_DESCRIPTOR_PASSING switch (fd_type) { case INPUT_FD: _gpgme_io_close (gpgsm->input_cb.fd); break; case OUTPUT_FD: _gpgme_io_close (gpgsm->output_cb.fd); break; case MESSAGE_FD: _gpgme_io_close (gpgsm->message_cb.fd); break; } #else (void)gpgsm; (void)fd_type; #endif } #define COMMANDLINELEN 40 static gpgme_error_t gpgsm_set_fd (engine_gpgsm_t gpgsm, fd_type_t fd_type, const char *opt) { gpg_error_t err = 0; char line[COMMANDLINELEN]; const char *which; iocb_data_t *iocb_data; #if USE_DESCRIPTOR_PASSING int dir; #endif switch (fd_type) { case INPUT_FD: which = "INPUT"; iocb_data = &gpgsm->input_cb; break; case OUTPUT_FD: which = "OUTPUT"; iocb_data = &gpgsm->output_cb; break; case MESSAGE_FD: which = "MESSAGE"; iocb_data = &gpgsm->message_cb; break; default: return gpg_error (GPG_ERR_INV_VALUE); } #if USE_DESCRIPTOR_PASSING dir = iocb_data->dir; /* We try to short-cut the communication by giving GPGSM direct access to the file descriptor, rather than using a pipe. */ iocb_data->server_fd = _gpgme_data_get_fd (iocb_data->data); if (iocb_data->server_fd < 0) { int fds[2]; if (_gpgme_io_pipe (fds, dir) < 0) return gpg_error_from_syserror (); iocb_data->fd = dir ? fds[0] : fds[1]; iocb_data->server_fd = dir ? fds[1] : fds[0]; if (_gpgme_io_set_close_notify (iocb_data->fd, close_notify_handler, gpgsm)) { err = gpg_error (GPG_ERR_GENERAL); goto leave_set_fd; } } err = assuan_sendfd (gpgsm->assuan_ctx, iocb_data->server_fd); if (err) goto leave_set_fd; _gpgme_io_close (iocb_data->server_fd); iocb_data->server_fd = -1; if (opt) snprintf (line, COMMANDLINELEN, "%s FD %s", which, opt); else snprintf (line, COMMANDLINELEN, "%s FD", which); #else if (opt) snprintf (line, COMMANDLINELEN, "%s FD=%s %s", which, iocb_data->server_fd_str, opt); else snprintf (line, COMMANDLINELEN, "%s FD=%s", which, iocb_data->server_fd_str); #endif err = gpgsm_assuan_simple_command (gpgsm, line, NULL, NULL); #if USE_DESCRIPTOR_PASSING leave_set_fd: if (err) { _gpgme_io_close (iocb_data->fd); iocb_data->fd = -1; if (iocb_data->server_fd != -1) { _gpgme_io_close (iocb_data->server_fd); iocb_data->server_fd = -1; } } #endif return err; } static const char * map_data_enc (gpgme_data_t d) { switch (gpgme_data_get_encoding (d)) { case GPGME_DATA_ENCODING_NONE: break; case GPGME_DATA_ENCODING_BINARY: return "--binary"; case GPGME_DATA_ENCODING_BASE64: return "--base64"; case GPGME_DATA_ENCODING_ARMOR: return "--armor"; default: break; } return NULL; } static gpgme_error_t status_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_gpgsm_t gpgsm = (engine_gpgsm_t) data->handler_value; gpgme_error_t err = 0; char *line; size_t linelen; do { err = assuan_read_line (gpgsm->assuan_ctx, &line, &linelen); if (err) { /* Try our best to terminate the connection friendly. */ /* assuan_write_line (gpgsm->assuan_ctx, "BYE"); */ TRACE3 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: error from assuan (%d) getting status line : %s", fd, err, gpg_strerror (err)); } else if (linelen >= 3 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && (line[3] == '\0' || line[3] == ' ')) { if (line[3] == ' ') err = atoi (&line[4]); if (! err) err = gpg_error (GPG_ERR_GENERAL); TRACE2 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: ERR line - mapped to: %s", fd, err ? gpg_strerror (err) : "ok"); /* Try our best to terminate the connection friendly. */ /* assuan_write_line (gpgsm->assuan_ctx, "BYE"); */ } else if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) { if (gpgsm->status.fnc) { char emptystring[1] = {0}; err = gpgsm->status.fnc (gpgsm->status.fnc_value, GPGME_STATUS_EOF, emptystring); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ } if (!err && gpgsm->colon.fnc && gpgsm->colon.any) { /* We must tell a colon function about the EOF. We do this only when we have seen any data lines. Note that this inlined use of colon data lines will eventually be changed into using a regular data channel. */ gpgsm->colon.any = 0; err = gpgsm->colon.fnc (gpgsm->colon.fnc_value, NULL); } TRACE2 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: OK line - final status: %s", fd, err ? gpg_strerror (err) : "ok"); _gpgme_io_close (gpgsm->status_cb.fd); return err; } else if (linelen > 2 && line[0] == 'D' && line[1] == ' ' && gpgsm->colon.fnc) { /* We are using the colon handler even for plain inline data - strange name for that function but for historic reasons we keep it. */ /* FIXME We can't use this for binary data because we assume this is a string. For the current usage of colon output it is correct. */ char *src = line + 2; char *end = line + linelen; char *dst; char **aline = &gpgsm->colon.attic.line; int *alinelen = &gpgsm->colon.attic.linelen; if (gpgsm->colon.attic.linesize < *alinelen + linelen + 1) { char *newline = realloc (*aline, *alinelen + linelen + 1); if (!newline) err = gpg_error_from_syserror (); else { *aline = newline; gpgsm->colon.attic.linesize = *alinelen + linelen + 1; } } if (!err) { dst = *aline + *alinelen; while (!err && src < end) { if (*src == '%' && src + 2 < end) { /* Handle escaped characters. */ ++src; *dst = _gpgme_hextobyte (src); (*alinelen)++; src += 2; } else { *dst = *src++; (*alinelen)++; } if (*dst == '\n') { /* Terminate the pending line, pass it to the colon handler and reset it. */ gpgsm->colon.any = 1; if (*alinelen > 1 && *(dst - 1) == '\r') dst--; *dst = '\0'; /* FIXME How should we handle the return code? */ err = gpgsm->colon.fnc (gpgsm->colon.fnc_value, *aline); if (!err) { dst = *aline; *alinelen = 0; } } else dst++; } } TRACE2 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: D line; final status: %s", fd, err? gpg_strerror (err):"ok"); } else if (linelen > 2 && line[0] == 'D' && line[1] == ' ' && gpgsm->inline_data) { char *src = line + 2; char *end = line + linelen; char *dst = src; gpgme_ssize_t nwritten; linelen = 0; while (src < end) { if (*src == '%' && src + 2 < end) { /* Handle escaped characters. */ ++src; *dst++ = _gpgme_hextobyte (src); src += 2; } else *dst++ = *src++; linelen++; } src = line + 2; while (linelen > 0) { nwritten = gpgme_data_write (gpgsm->inline_data, src, linelen); if (nwritten <= 0 || nwritten > linelen) { err = gpg_error_from_syserror (); break; } src += nwritten; linelen -= nwritten; } TRACE2 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: D inlinedata; final status: %s", fd, err? gpg_strerror (err):"ok"); } else if (linelen > 2 && line[0] == 'S' && line[1] == ' ') { char *rest; gpgme_status_code_t r; rest = strchr (line + 2, ' '); if (!rest) rest = line + linelen; /* set to an empty string */ else *(rest++) = 0; r = _gpgme_parse_status (line + 2); if (gpgsm->status.mon_cb && r != GPGME_STATUS_PROGRESS) { /* Note that we call the monitor even if we do * not know the status code (r < 0). */ err = gpgsm->status.mon_cb (gpgsm->status.mon_cb_value, line + 2, rest); } else err = 0; if (r >= 0 && !err) { if (gpgsm->status.fnc) { err = gpgsm->status.fnc (gpgsm->status.fnc_value, r, rest); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ } } else fprintf (stderr, "[UNKNOWN STATUS]%s %s", line + 2, rest); TRACE3 (DEBUG_CTX, "gpgme:status_handler", gpgsm, "fd 0x%x: S line (%s) - final status: %s", fd, line+2, err? gpg_strerror (err):"ok"); } else if (linelen >= 7 && line[0] == 'I' && line[1] == 'N' && line[2] == 'Q' && line[3] == 'U' && line[4] == 'I' && line[5] == 'R' && line[6] == 'E' && (line[7] == '\0' || line[7] == ' ')) { char *keyword = line+7; while (*keyword == ' ') keyword++;; default_inq_cb (gpgsm, keyword); assuan_write_line (gpgsm->assuan_ctx, "END"); } } while (!err && assuan_pending_line (gpgsm->assuan_ctx)); return err; } static gpgme_error_t add_io_cb (engine_gpgsm_t gpgsm, iocb_data_t *iocbd, gpgme_io_cb_t handler) { gpgme_error_t err; TRACE_BEG2 (DEBUG_ENGINE, "engine-gpgsm:add_io_cb", gpgsm, "fd %d, dir %d", iocbd->fd, iocbd->dir); err = (*gpgsm->io_cbs.add) (gpgsm->io_cbs.add_priv, iocbd->fd, iocbd->dir, handler, iocbd->data, &iocbd->tag); if (err) return TRACE_ERR (err); if (!iocbd->dir) /* FIXME Kludge around poll() problem. */ err = _gpgme_io_set_nonblocking (iocbd->fd); return TRACE_ERR (err); } static gpgme_error_t start (engine_gpgsm_t gpgsm, const char *command) { gpgme_error_t err; assuan_fd_t afdlist[5]; int fdlist[5]; int nfds; int i; if (*gpgsm->request_origin) { char *cmd; cmd = _gpgme_strconcat ("OPTION request-origin=", gpgsm->request_origin, NULL); if (!cmd) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm, cmd, NULL, NULL); free (cmd); if (err && gpg_err_code (err) != GPG_ERR_UNKNOWN_OPTION) return err; } /* We need to know the fd used by assuan for reads. We do this by using the assumption that the first returned fd from assuan_get_active_fds() is always this one. */ nfds = assuan_get_active_fds (gpgsm->assuan_ctx, 0 /* read fds */, afdlist, DIM (afdlist)); if (nfds < 1) return gpg_error (GPG_ERR_GENERAL); /* FIXME */ /* For now... */ for (i = 0; i < nfds; i++) fdlist[i] = (int) afdlist[i]; /* We "duplicate" the file descriptor, so we can close it here (we can't close fdlist[0], as that is closed by libassuan, and closing it here might cause libassuan to close some unrelated FD later). Alternatively, we could special case status_fd and register/unregister it manually as needed, but this increases code duplication and is more complicated as we can not use the close notifications etc. A third alternative would be to let Assuan know that we closed the FD, but that complicates the Assuan interface. */ gpgsm->status_cb.fd = _gpgme_io_dup (fdlist[0]); if (gpgsm->status_cb.fd < 0) return gpg_error_from_syserror (); if (_gpgme_io_set_close_notify (gpgsm->status_cb.fd, close_notify_handler, gpgsm)) { _gpgme_io_close (gpgsm->status_cb.fd); gpgsm->status_cb.fd = -1; return gpg_error (GPG_ERR_GENERAL); } err = add_io_cb (gpgsm, &gpgsm->status_cb, status_handler); if (!err && gpgsm->input_cb.fd != -1) err = add_io_cb (gpgsm, &gpgsm->input_cb, _gpgme_data_outbound_handler); if (!err && gpgsm->output_cb.fd != -1) err = add_io_cb (gpgsm, &gpgsm->output_cb, _gpgme_data_inbound_handler); if (!err && gpgsm->message_cb.fd != -1) err = add_io_cb (gpgsm, &gpgsm->message_cb, _gpgme_data_outbound_handler); if (!err) err = assuan_write_line (gpgsm->assuan_ctx, command); if (!err) gpgsm_io_event (gpgsm, GPGME_EVENT_START, NULL); return err; } #if USE_DESCRIPTOR_PASSING static gpgme_error_t gpgsm_reset (void *engine) { engine_gpgsm_t gpgsm = engine; /* IF we have an active connection we must send a reset because we need to reset the list of signers. Note that RESET does not reset OPTION commands. */ return (gpgsm->assuan_ctx ? gpgsm_assuan_simple_command (gpgsm, "RESET", NULL, NULL) : 0); } #endif static gpgme_error_t gpgsm_decrypt (void *engine, gpgme_decrypt_flags_t flags, gpgme_data_t ciph, gpgme_data_t plain, int export_session_key, const char *override_session_key, int auto_key_retrieve) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; (void)flags; /* gpgsm is not capable of exporting session keys right now, so we * will ignore this if requested. */ (void)export_session_key; (void)override_session_key; /* --auto-key-retrieve is also not supported. */ (void)auto_key_retrieve; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); gpgsm->input_cb.data = ciph; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return gpg_error (GPG_ERR_GENERAL); /* FIXME */ gpgsm->output_cb.data = plain; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, 0); if (err) return gpg_error (GPG_ERR_GENERAL); /* FIXME */ gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (engine, "DECRYPT"); return err; } static gpgme_error_t gpgsm_delete (void *engine, gpgme_key_t key, unsigned int flags) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; char *fpr = key->subkeys ? key->subkeys->fpr : NULL; char *linep = fpr; char *line; int length = 8; /* "DELKEYS " */ (void)flags; if (!fpr) return gpg_error (GPG_ERR_INV_VALUE); while (*linep) { length++; if (*linep == '%' || *linep == ' ' || *linep == '+') length += 2; linep++; } length++; line = malloc (length); if (!line) return gpg_error_from_syserror (); strcpy (line, "DELKEYS "); linep = &line[8]; while (*fpr) { switch (*fpr) { case '%': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '5'; break; case ' ': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '0'; break; case '+': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = 'B'; break; default: *(linep++) = *fpr; break; } fpr++; } *linep = '\0'; gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, line); free (line); return err; } static gpgme_error_t set_recipients (engine_gpgsm_t gpgsm, gpgme_key_t recp[]) { gpgme_error_t err = 0; char *line; int linelen; int invalid_recipients = 0; int i; linelen = 10 + 40 + 1; /* "RECIPIENT " + guess + '\0'. */ line = malloc (10 + 40 + 1); if (!line) return gpg_error_from_syserror (); strcpy (line, "RECIPIENT "); for (i =0; !err && recp[i]; i++) { char *fpr; int newlen; if (!recp[i]->subkeys || !recp[i]->subkeys->fpr) { invalid_recipients++; continue; } fpr = recp[i]->subkeys->fpr; newlen = 11 + strlen (fpr); if (linelen < newlen) { char *newline = realloc (line, newlen); if (! newline) { int saved_err = gpg_error_from_syserror (); free (line); return saved_err; } line = newline; linelen = newlen; } strcpy (&line[10], fpr); err = gpgsm_assuan_simple_command (gpgsm, line, gpgsm->status.fnc, gpgsm->status.fnc_value); /* FIXME: This requires more work. */ if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY) invalid_recipients++; else if (err) { free (line); return err; } } free (line); return gpg_error (invalid_recipients ? GPG_ERR_UNUSABLE_PUBKEY : GPG_ERR_NO_ERROR); } /* Take recipients from the LF delimited STRING and send RECIPIENT * commands to gpgsm. */ static gpgme_error_t set_recipients_from_string (engine_gpgsm_t gpgsm, const char *string) { gpg_error_t err = 0; char *line = NULL; int no_pubkey = 0; const char *s; int n; for (;;) { while (*string == ' ' || *string == '\t') string++; if (!*string) break; s = strchr (string, '\n'); if (s) n = s - string; else n = strlen (string); while (n && (string[n-1] == ' ' || string[n-1] == '\t')) n--; gpgrt_free (line); if (gpgrt_asprintf (&line, "RECIPIENT %.*s", n, string) < 0) { err = gpg_error_from_syserror (); break; } string += n + !!s; err = gpgsm_assuan_simple_command (gpgsm, line, gpgsm->status.fnc, gpgsm->status.fnc_value); /* Fixme: Improve error reporting. */ if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY) no_pubkey++; else if (err) break; } gpgrt_free (line); return err? err : no_pubkey? gpg_error (GPG_ERR_NO_PUBKEY) : 0; } static gpgme_error_t gpgsm_encrypt (void *engine, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (!recp) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); if ((flags & GPGME_ENCRYPT_NO_ENCRYPT_TO)) { err = gpgsm_assuan_simple_command (gpgsm, "OPTION no-encrypt-to", NULL, NULL); if (err) return err; } gpgsm->input_cb.data = plain; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return err; gpgsm->output_cb.data = ciph; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (gpgsm->output_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; if (!recp && recpstring) err = set_recipients_from_string (gpgsm, recpstring); else err = set_recipients (gpgsm, recp); if (!err) err = start (gpgsm, "ENCRYPT"); return err; } static gpgme_error_t gpgsm_export (void *engine, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err = 0; char *cmd; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (!pattern) pattern = ""; cmd = malloc (7 + 9 + 9 + strlen (pattern) + 1); if (!cmd) return gpg_error_from_syserror (); strcpy (cmd, "EXPORT "); if ((mode & GPGME_EXPORT_MODE_SECRET)) { strcat (cmd, "--secret "); if ((mode & GPGME_EXPORT_MODE_RAW)) strcat (cmd, "--raw "); else if ((mode & GPGME_EXPORT_MODE_PKCS12)) strcat (cmd, "--pkcs12 "); } strcat (cmd, pattern); gpgsm->output_cb.data = keydata; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (gpgsm->output_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, cmd); free (cmd); return err; } static gpgme_error_t gpgsm_export_ext (void *engine, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err = 0; char *line; /* Length is "EXPORT " + "--secret " + "--pkcs12 " + p + '\0'. */ int length = 7 + 9 + 9 + 1; char *linep; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (pattern && *pattern) { const char **pat = pattern; while (*pat) { const char *patlet = *pat; while (*patlet) { length++; if (*patlet == '%' || *patlet == ' ' || *patlet == '+') length += 2; patlet++; } pat++; length++; } } line = malloc (length); if (!line) return gpg_error_from_syserror (); strcpy (line, "EXPORT "); if ((mode & GPGME_EXPORT_MODE_SECRET)) { strcat (line, "--secret "); if ((mode & GPGME_EXPORT_MODE_RAW)) strcat (line, "--raw "); else if ((mode & GPGME_EXPORT_MODE_PKCS12)) strcat (line, "--pkcs12 "); } linep = &line[strlen (line)]; if (pattern && *pattern) { while (*pattern) { const char *patlet = *pattern; while (*patlet) { switch (*patlet) { case '%': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '5'; break; case ' ': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '0'; break; case '+': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = 'B'; break; default: *(linep++) = *patlet; break; } patlet++; } pattern++; if (*pattern) *linep++ = ' '; } } *linep = '\0'; gpgsm->output_cb.data = keydata; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (gpgsm->output_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, line); free (line); return err; } static gpgme_error_t gpgsm_genkey (void *engine, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t key, unsigned int flags, gpgme_data_t help_data, unsigned int extraflags, gpgme_data_t pubkey, gpgme_data_t seckey) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; (void)reserved; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (help_data) { if (!pubkey || seckey) return gpg_error (GPG_ERR_INV_VALUE); gpgsm->input_cb.data = help_data; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return err; gpgsm->output_cb.data = pubkey; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, (extraflags & GENKEY_EXTRAFLAG_ARMOR)? "--armor" : map_data_enc (gpgsm->output_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, "GENKEY"); return err; } (void)userid; (void)algo; (void)expires; (void)key; (void)flags; /* The new interface has not yet been implemented, */ return gpg_error (GPG_ERR_NOT_IMPLEMENTED); } static gpgme_error_t gpgsm_import (void *engine, gpgme_data_t keydata, gpgme_key_t *keyarray) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; gpgme_data_encoding_t dataenc; int idx; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); if (keydata && keyarray) return gpg_error (GPG_ERR_INV_VALUE); /* Only one is allowed. */ dataenc = gpgme_data_get_encoding (keydata); if (keyarray) { size_t buflen; char *buffer, *p; /* Fist check whether the engine already features the --re-import option. */ err = gpgsm_assuan_simple_command (gpgsm, "GETINFO cmd_has_option IMPORT re-import", NULL, NULL); if (err) return gpg_error (GPG_ERR_NOT_SUPPORTED); /* Create an internal data object with a list of all fingerprints. The data object and its memory (to avoid an extra copy by gpgme_data_new_from_mem) are stored in two variables which are released by the close_notify_handler. */ for (idx=0, buflen=0; keyarray[idx]; idx++) { if (keyarray[idx]->protocol == GPGME_PROTOCOL_CMS && keyarray[idx]->subkeys && keyarray[idx]->subkeys->fpr && *keyarray[idx]->subkeys->fpr) buflen += strlen (keyarray[idx]->subkeys->fpr) + 1; } /* Allocate a buffer with extra space for the trailing Nul introduced by the use of stpcpy. */ buffer = malloc (buflen+1); if (!buffer) return gpg_error_from_syserror (); for (idx=0, p = buffer; keyarray[idx]; idx++) { if (keyarray[idx]->protocol == GPGME_PROTOCOL_CMS && keyarray[idx]->subkeys && keyarray[idx]->subkeys->fpr && *keyarray[idx]->subkeys->fpr) p = stpcpy (stpcpy (p, keyarray[idx]->subkeys->fpr), "\n"); } err = gpgme_data_new_from_mem (&gpgsm->input_helper_data, buffer, buflen, 0); if (err) { free (buffer); return err; } gpgsm->input_helper_memory = buffer; gpgsm->input_cb.data = gpgsm->input_helper_data; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) { gpgme_data_release (gpgsm->input_helper_data); gpgsm->input_helper_data = NULL; free (gpgsm->input_helper_memory); gpgsm->input_helper_memory = NULL; return err; } gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; return start (gpgsm, "IMPORT --re-import"); } else if (dataenc == GPGME_DATA_ENCODING_URL || dataenc == GPGME_DATA_ENCODING_URL0 || dataenc == GPGME_DATA_ENCODING_URLESC) { return gpg_error (GPG_ERR_NOT_IMPLEMENTED); } else { gpgsm->input_cb.data = keydata; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; return start (gpgsm, "IMPORT"); } } static gpgme_error_t gpgsm_keylist (void *engine, const char *pattern, int secret_only, gpgme_keylist_mode_t mode, int engine_flags) { engine_gpgsm_t gpgsm = engine; char *line; gpgme_error_t err; int list_mode = 0; if (mode & GPGME_KEYLIST_MODE_LOCAL) list_mode |= 1; if (mode & GPGME_KEYLIST_MODE_EXTERN) list_mode |= 2; if (!pattern) pattern = ""; /* Hack to make sure that the agent is started. Only if the agent has been started an application may connect to the agent via GPGME_PROTOCOL_ASSUAN - for example to look for smartcards. We do this only if a secret key listing has been requested. In general this is not needed because a secret key listing starts the agent. However on a fresh installation no public keys are available and thus there is no need for gpgsm to ask the agent whether a secret key exists for the public key. */ if (secret_only || (mode & GPGME_KEYLIST_MODE_WITH_SECRET)) gpgsm_assuan_simple_command (gpgsm, "GETINFO agent-check", NULL, NULL); /* Always send list-mode option because RESET does not reset it. */ if (gpgrt_asprintf (&line, "OPTION list-mode=%d", (list_mode & 3)) < 0) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm, line, NULL, NULL); gpgrt_free (line); if (err) return err; /* Always send key validation because RESET does not reset it. */ /* Use the validation mode if requested. We don't check for an error yet because this is a pretty fresh gpgsm features. */ gpgsm_assuan_simple_command (gpgsm, (mode & GPGME_KEYLIST_MODE_VALIDATE)? "OPTION with-validation=1": "OPTION with-validation=0" , NULL, NULL); /* Include the ephemeral keys if requested. We don't check for an error yet because this is a pretty fresh gpgsm features. */ gpgsm_assuan_simple_command (gpgsm, (mode & GPGME_KEYLIST_MODE_EPHEMERAL)? "OPTION with-ephemeral-keys=1": "OPTION with-ephemeral-keys=0" , NULL, NULL); gpgsm_assuan_simple_command (gpgsm, (mode & GPGME_KEYLIST_MODE_WITH_SECRET)? "OPTION with-secret=1": "OPTION with-secret=0" , NULL, NULL); gpgsm_assuan_simple_command (gpgsm, (engine_flags & GPGME_ENGINE_FLAG_OFFLINE)? "OPTION offline=1": "OPTION offline=0" , NULL, NULL); /* Length is "LISTSECRETKEYS " + p + '\0'. */ line = malloc (15 + strlen (pattern) + 1); if (!line) return gpg_error_from_syserror (); if (secret_only) { strcpy (line, "LISTSECRETKEYS "); strcpy (&line[15], pattern); } else { strcpy (line, "LISTKEYS "); strcpy (&line[9], pattern); } gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, line); free (line); return err; } static gpgme_error_t gpgsm_keylist_ext (void *engine, const char *pattern[], int secret_only, int reserved, gpgme_keylist_mode_t mode, int engine_flags) { engine_gpgsm_t gpgsm = engine; char *line; gpgme_error_t err; /* Length is "LISTSECRETKEYS " + p + '\0'. */ int length = 15 + 1; char *linep; int any_pattern = 0; int list_mode = 0; if (reserved) return gpg_error (GPG_ERR_INV_VALUE); if (mode & GPGME_KEYLIST_MODE_LOCAL) list_mode |= 1; if (mode & GPGME_KEYLIST_MODE_EXTERN) list_mode |= 2; /* Always send list-mode option because RESET does not reset it. */ if (gpgrt_asprintf (&line, "OPTION list-mode=%d", (list_mode & 3)) < 0) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm, line, NULL, NULL); gpgrt_free (line); if (err) return err; /* Always send key validation because RESET does not reset it. */ /* Use the validation mode if required. We don't check for an error yet because this is a pretty fresh gpgsm features. */ gpgsm_assuan_simple_command (gpgsm, (mode & GPGME_KEYLIST_MODE_VALIDATE)? "OPTION with-validation=1": "OPTION with-validation=0" , NULL, NULL); gpgsm_assuan_simple_command (gpgsm, (mode & GPGME_KEYLIST_MODE_WITH_SECRET)? "OPTION with-secret=1": "OPTION with-secret=0" , NULL, NULL); gpgsm_assuan_simple_command (gpgsm, (engine_flags & GPGME_ENGINE_FLAG_OFFLINE)? "OPTION offline=1": "OPTION offline=0" , NULL, NULL); if (pattern && *pattern) { const char **pat = pattern; while (*pat) { const char *patlet = *pat; while (*patlet) { length++; if (*patlet == '%' || *patlet == ' ' || *patlet == '+') length += 2; patlet++; } pat++; length++; } } line = malloc (length); if (!line) return gpg_error_from_syserror (); if (secret_only) { strcpy (line, "LISTSECRETKEYS "); linep = &line[15]; } else { strcpy (line, "LISTKEYS "); linep = &line[9]; } if (pattern && *pattern) { while (*pattern) { const char *patlet = *pattern; while (*patlet) { switch (*patlet) { case '%': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '5'; break; case ' ': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = '0'; break; case '+': *(linep++) = '%'; *(linep++) = '2'; *(linep++) = 'B'; break; default: *(linep++) = *patlet; break; } patlet++; } any_pattern = 1; *linep++ = ' '; pattern++; } } if (any_pattern) linep--; *linep = '\0'; gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, line); free (line); return err; } static gpgme_error_t gpgsm_sign (void *engine, gpgme_data_t in, gpgme_data_t out, gpgme_sig_mode_t mode, int use_armor, int use_textmode, int include_certs, gpgme_ctx_t ctx /* FIXME */) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; char *assuan_cmd; int i; gpgme_key_t key; (void)use_textmode; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); /* FIXME: This does not work as RESET does not reset it so we can't revert back to default. */ if (include_certs != GPGME_INCLUDE_CERTS_DEFAULT) { /* FIXME: Make sure that if we run multiple operations, that we can reset any previously set value in case the default is requested. */ if (gpgrt_asprintf (&assuan_cmd, "OPTION include-certs %i", include_certs) < 0) return gpg_error_from_syserror (); err = gpgsm_assuan_simple_command (gpgsm, assuan_cmd, NULL, NULL); gpgrt_free (assuan_cmd); if (err) return err; } for (i = 0; (key = gpgme_signers_enum (ctx, i)); i++) { const char *s = key->subkeys ? key->subkeys->fpr : NULL; if (s && strlen (s) < 80) { char buf[100]; strcpy (stpcpy (buf, "SIGNER "), s); err = gpgsm_assuan_simple_command (gpgsm, buf, gpgsm->status.fnc, gpgsm->status.fnc_value); } else err = gpg_error (GPG_ERR_INV_VALUE); gpgme_key_unref (key); if (err) return err; } gpgsm->input_cb.data = in; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return err; gpgsm->output_cb.data = out; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (gpgsm->output_cb.data)); if (err) return err; gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, mode == GPGME_SIG_MODE_DETACH ? "SIGN --detached" : "SIGN"); return err; } static gpgme_error_t gpgsm_verify (void *engine, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext, gpgme_ctx_t ctx) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; (void)ctx; if (!gpgsm) return gpg_error (GPG_ERR_INV_VALUE); gpgsm->input_cb.data = sig; err = gpgsm_set_fd (gpgsm, INPUT_FD, map_data_enc (gpgsm->input_cb.data)); if (err) return err; if (!signed_text) { /* Normal or cleartext signature. */ if (plaintext) { gpgsm->output_cb.data = plaintext; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, 0); } else { /* No output requested. */ gpgsm_clear_fd (gpgsm, OUTPUT_FD); } gpgsm_clear_fd (gpgsm, MESSAGE_FD); } else { /* Detached signature. */ gpgsm->message_cb.data = signed_text; err = gpgsm_set_fd (gpgsm, MESSAGE_FD, 0); gpgsm_clear_fd (gpgsm, OUTPUT_FD); } gpgsm->inline_data = NULL; if (!err) err = start (gpgsm, "VERIFY"); return err; } /* Send the GETAUDITLOG command. The result is saved to a gpgme data object. */ static gpgme_error_t gpgsm_getauditlog (void *engine, gpgme_data_t output, unsigned int flags) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err = 0; if (!gpgsm || !output) return gpg_error (GPG_ERR_INV_VALUE); if ((flags & GPGME_AUDITLOG_DIAG)) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); #if USE_DESCRIPTOR_PASSING gpgsm->output_cb.data = output; err = gpgsm_set_fd (gpgsm, OUTPUT_FD, 0); if (err) return err; gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; # define CMD "GETAUDITLOG" #else gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = output; # define CMD "GETAUDITLOG --data" #endif err = start (gpgsm, (flags & GPGME_AUDITLOG_HTML)? CMD " --html" : CMD); return err; } /* This sets a status callback for monitoring status lines before they * are passed to a caller set handler. */ static void gpgsm_set_status_cb (void *engine, gpgme_status_cb_t cb, void *cb_value) { engine_gpgsm_t gpgsm = engine; gpgsm->status.mon_cb = cb; gpgsm->status.mon_cb_value = cb_value; } static void gpgsm_set_status_handler (void *engine, engine_status_handler_t fnc, void *fnc_value) { engine_gpgsm_t gpgsm = engine; gpgsm->status.fnc = fnc; gpgsm->status.fnc_value = fnc_value; } static gpgme_error_t gpgsm_set_colon_line_handler (void *engine, engine_colon_line_handler_t fnc, void *fnc_value) { engine_gpgsm_t gpgsm = engine; gpgsm->colon.fnc = fnc; gpgsm->colon.fnc_value = fnc_value; gpgsm->colon.any = 0; return 0; } static void gpgsm_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs) { engine_gpgsm_t gpgsm = engine; gpgsm->io_cbs = *io_cbs; } static void gpgsm_io_event (void *engine, gpgme_event_io_t type, void *type_data) { engine_gpgsm_t gpgsm = engine; TRACE3 (DEBUG_ENGINE, "gpgme:gpgsm_io_event", gpgsm, "event %p, type %d, type_data %p", gpgsm->io_cbs.event, type, type_data); if (gpgsm->io_cbs.event) (*gpgsm->io_cbs.event) (gpgsm->io_cbs.event_priv, type, type_data); } static gpgme_error_t gpgsm_passwd (void *engine, gpgme_key_t key, unsigned int flags) { engine_gpgsm_t gpgsm = engine; gpgme_error_t err; char *line; (void)flags; if (!key || !key->subkeys || !key->subkeys->fpr) return gpg_error (GPG_ERR_INV_CERT_OBJ); if (gpgrt_asprintf (&line, "PASSWD -- %s", key->subkeys->fpr) < 0) return gpg_error_from_syserror (); gpgsm_clear_fd (gpgsm, OUTPUT_FD); gpgsm_clear_fd (gpgsm, INPUT_FD); gpgsm_clear_fd (gpgsm, MESSAGE_FD); gpgsm->inline_data = NULL; err = start (gpgsm, line); gpgrt_free (line); return err; } struct engine_ops _gpgme_engine_ops_gpgsm = { /* Static functions. */ _gpgme_get_default_gpgsm_name, NULL, gpgsm_get_version, gpgsm_get_req_version, gpgsm_new, /* Member functions. */ gpgsm_release, #if USE_DESCRIPTOR_PASSING gpgsm_reset, #else NULL, /* reset */ #endif gpgsm_set_status_cb, gpgsm_set_status_handler, NULL, /* set_command_handler */ gpgsm_set_colon_line_handler, gpgsm_set_locale, NULL, /* set_protocol */ gpgsm_set_engine_flags, gpgsm_decrypt, gpgsm_delete, /* decrypt_verify */ NULL, /* edit */ gpgsm_encrypt, NULL, /* encrypt_sign */ gpgsm_export, gpgsm_export_ext, gpgsm_genkey, gpgsm_import, gpgsm_keylist, gpgsm_keylist_ext, NULL, /* keylist_data */ NULL, /* keysign */ NULL, /* tofu_policy */ gpgsm_sign, NULL, /* trustlist */ gpgsm_verify, gpgsm_getauditlog, NULL, /* opassuan_transact */ NULL, /* conf_load */ NULL, /* conf_save */ NULL, /* conf_dir */ NULL, /* query_swdb */ gpgsm_set_io_cbs, gpgsm_io_event, gpgsm_cancel, NULL, /* cancel_op */ gpgsm_passwd, NULL, /* set_pinentry_mode */ NULL /* opspawn */ }; diff --git a/src/engine-spawn.c b/src/engine-spawn.c index 7b7a9cdf..6f57a7d3 100644 --- a/src/engine-spawn.c +++ b/src/engine-spawn.c @@ -1,483 +1,484 @@ /* engine-spawn.c - Run an arbitrary program - Copyright (C) 2014 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2014 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_LOCALE_H #include #endif #include "gpgme.h" #include "util.h" #include "ops.h" #include "wait.h" #include "context.h" /*temp hack until we have GpmeData methods to do I/O */ #include "priv-io.h" #include "sema.h" #include "debug.h" #include "engine-backend.h" /* This type is used to build a list of data sources/sinks. */ struct datalist_s { struct datalist_s *next; gpgme_data_t data; /* The data object. */ int inbound; /* True if this is used for reading from the peer. */ int dup_to; /* The fd used by the peer. */ }; struct fd_data_map_s { gpgme_data_t data; int inbound; /* True if this is used for reading from the peer. */ int dup_to; /* Dup the fd to that one. */ int fd; /* The fd to use. */ int peer_fd; /* The other side of the pipe. */ void *tag; /* Tag used by the I/O callback. */ }; struct engine_spawn { struct datalist_s *arglist; struct datalist_s **argtail; struct fd_data_map_s *fd_data_map; struct gpgme_io_cbs io_cbs; }; typedef struct engine_spawn *engine_spawn_t; static void engspawn_io_event (void *engine, gpgme_event_io_t type, void *type_data); static gpgme_error_t engspawn_cancel (void *engine); static void close_notify_handler (int fd, void *opaque) { engine_spawn_t esp = opaque; int i; assert (fd != -1); if (esp->fd_data_map) { for (i = 0; esp->fd_data_map[i].data; i++) { if (esp->fd_data_map[i].fd == fd) { if (esp->fd_data_map[i].tag) (*esp->io_cbs.remove) (esp->fd_data_map[i].tag); esp->fd_data_map[i].fd = -1; break; } if (esp->fd_data_map[i].peer_fd == fd) { esp->fd_data_map[i].peer_fd = -1; break; } } } } static gpgme_error_t add_data (engine_spawn_t esp, gpgme_data_t data, int dup_to, int inbound) { struct datalist_s *a; assert (esp); assert (data); a = malloc (sizeof *a); if (!a) return gpg_error_from_syserror (); a->next = NULL; a->data = data; a->inbound = inbound; a->dup_to = dup_to; *esp->argtail = a; esp->argtail = &a->next; return 0; } static void free_fd_data_map (struct fd_data_map_s *fd_data_map) { int i; if (!fd_data_map) return; for (i = 0; fd_data_map[i].data; i++) { if (fd_data_map[i].fd != -1) _gpgme_io_close (fd_data_map[i].fd); if (fd_data_map[i].peer_fd != -1) _gpgme_io_close (fd_data_map[i].peer_fd); /* Don't release data because this is only a reference. */ } free (fd_data_map); } static gpgme_error_t build_fd_data_map (engine_spawn_t esp) { struct datalist_s *a; size_t datac; int fds[2]; for (datac = 0, a = esp->arglist; a; a = a->next) if (a->data) datac++; free_fd_data_map (esp->fd_data_map); esp->fd_data_map = calloc (datac + 1, sizeof *esp->fd_data_map); if (!esp->fd_data_map) return gpg_error_from_syserror (); for (datac = 0, a = esp->arglist; a; a = a->next) { assert (a->data); if (_gpgme_io_pipe (fds, a->inbound ? 1 : 0) == -1) { free (esp->fd_data_map); esp->fd_data_map = NULL; return gpg_error_from_syserror (); } if (_gpgme_io_set_close_notify (fds[0], close_notify_handler, esp) || _gpgme_io_set_close_notify (fds[1], close_notify_handler, esp)) { /* FIXME: Need error cleanup. */ return gpg_error (GPG_ERR_GENERAL); } esp->fd_data_map[datac].inbound = a->inbound; if (a->inbound) { esp->fd_data_map[datac].fd = fds[0]; esp->fd_data_map[datac].peer_fd = fds[1]; } else { esp->fd_data_map[datac].fd = fds[1]; esp->fd_data_map[datac].peer_fd = fds[0]; } esp->fd_data_map[datac].data = a->data; esp->fd_data_map[datac].dup_to = a->dup_to; datac++; } return 0; } static gpgme_error_t add_io_cb (engine_spawn_t esp, int fd, int dir, gpgme_io_cb_t handler, void *data, void **tag) { gpgme_error_t err; err = (*esp->io_cbs.add) (esp->io_cbs.add_priv, fd, dir, handler, data, tag); if (err) return err; if (!dir) /* Fixme: Kludge around poll() problem. */ err = _gpgme_io_set_nonblocking (fd); return err; } static gpgme_error_t engspawn_start (engine_spawn_t esp, const char *file, const char *argv[], unsigned int flags) { gpgme_error_t err; int i, n; int status; struct spawn_fd_item_s *fd_list; pid_t pid; unsigned int spflags; const char *save_argv0 = NULL; if (!esp || !file || !argv || !argv[0]) return gpg_error (GPG_ERR_INV_VALUE); spflags = 0; if ((flags & GPGME_SPAWN_DETACHED)) spflags |= IOSPAWN_FLAG_DETACHED; if ((flags & GPGME_SPAWN_ALLOW_SET_FG)) spflags |= IOSPAWN_FLAG_ALLOW_SET_FG; if ((flags & GPGME_SPAWN_SHOW_WINDOW)) spflags |= IOSPAWN_FLAG_SHOW_WINDOW; err = build_fd_data_map (esp); if (err) return err; n = 0; for (i = 0; esp->fd_data_map[i].data; i++) n++; fd_list = calloc (n+1, sizeof *fd_list); if (!fd_list) return gpg_error_from_syserror (); /* Build the fd list for the child. */ n = 0; for (i = 0; esp->fd_data_map[i].data; i++) { fd_list[n].fd = esp->fd_data_map[i].peer_fd; fd_list[n].dup_to = esp->fd_data_map[i].dup_to; n++; } fd_list[n].fd = -1; fd_list[n].dup_to = -1; if (argv[0] && !*argv[0]) { save_argv0 = argv[0]; argv[0] = _gpgme_get_basename (file); } status = _gpgme_io_spawn (file, (char * const *)argv, spflags, fd_list, NULL, NULL, &pid); if (save_argv0) argv[0] = save_argv0; free (fd_list); if (status == -1) return gpg_error_from_syserror (); for (i = 0; esp->fd_data_map[i].data; i++) { err = add_io_cb (esp, esp->fd_data_map[i].fd, esp->fd_data_map[i].inbound, esp->fd_data_map[i].inbound ? _gpgme_data_inbound_handler : _gpgme_data_outbound_handler, esp->fd_data_map[i].data, &esp->fd_data_map[i].tag); if (err) return err; /* FIXME: kill the child */ } engspawn_io_event (esp, GPGME_EVENT_START, NULL); return 0; } /* Public functions */ static const char * engspawn_get_file_name (void) { return "/nonexistent"; } static char * engspawn_get_version (const char *file_name) { (void)file_name; return NULL; } static const char * engspawn_get_req_version (void) { return NULL; } static gpgme_error_t engspawn_new (void **engine, const char *file_name, const char *home_dir, const char *version) { engine_spawn_t esp; (void)file_name; (void)home_dir; (void)version; esp = calloc (1, sizeof *esp); if (!esp) return gpg_error_from_syserror (); esp->argtail = &esp->arglist; *engine = esp; return 0; } static void engspawn_release (void *engine) { engine_spawn_t esp = engine; if (!esp) return; engspawn_cancel (engine); while (esp->arglist) { struct datalist_s *next = esp->arglist->next; free (esp->arglist); esp->arglist = next; } free (esp); } static void engspawn_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs) { engine_spawn_t esp = engine; esp->io_cbs = *io_cbs; } static void engspawn_io_event (void *engine, gpgme_event_io_t type, void *type_data) { engine_spawn_t esp = engine; TRACE3 (DEBUG_ENGINE, "gpgme:engspawn_io_event", esp, "event %p, type %d, type_data %p", esp->io_cbs.event, type, type_data); if (esp->io_cbs.event) (*esp->io_cbs.event) (esp->io_cbs.event_priv, type, type_data); } static gpgme_error_t engspawn_cancel (void *engine) { engine_spawn_t esp = engine; if (!esp) return gpg_error (GPG_ERR_INV_VALUE); if (esp->fd_data_map) { free_fd_data_map (esp->fd_data_map); esp->fd_data_map = NULL; } return 0; } static gpgme_error_t engspawn_op_spawn (void *engine, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags) { engine_spawn_t esp = engine; gpgme_error_t err = 0; if (datain) err = add_data (esp, datain, 0, 0); if (!err && dataout) err = add_data (esp, dataout, 1, 1); if (!err && dataerr) err = add_data (esp, dataerr, 2, 1); if (!err) err = engspawn_start (esp, file, argv, flags); return err; } struct engine_ops _gpgme_engine_ops_spawn = { /* Static functions. */ engspawn_get_file_name, NULL, /* get_home_dir */ engspawn_get_version, engspawn_get_req_version, engspawn_new, /* Member functions. */ engspawn_release, NULL, /* reset */ NULL, /* set_status_cb */ NULL, /* set_status_handler */ NULL, /* set_command_handler */ NULL, /* set_colon_line_handler */ NULL, /* set_locale */ NULL, /* set_protocol */ NULL, /* set_engine_flags */ NULL, /* decrypt */ NULL, /* delete */ NULL, /* edit */ NULL, /* encrypt */ NULL, /* encrypt_sign */ NULL, /* export */ NULL, /* export_ext */ NULL, /* genkey */ NULL, /* import */ NULL, /* keylist */ NULL, /* keylist_ext */ NULL, /* keylist_data */ NULL, /* keysign */ NULL, /* tofu_policy */ NULL, /* sign */ NULL, /* trustlist */ NULL, /* verify */ NULL, /* getauditlog */ NULL, /* opassuan_transact */ NULL, /* conf_load */ NULL, /* conf_save */ NULL, /* conf_dir */ NULL, /* query_swdb */ engspawn_set_io_cbs, engspawn_io_event, /* io_event */ engspawn_cancel, /* cancel */ NULL, /* cancel_op */ NULL, /* passwd */ NULL, /* set_pinentry_mode */ engspawn_op_spawn /* opspawn */ }; diff --git a/src/engine-uiserver.c b/src/engine-uiserver.c index d8f4fce3..a79b3b13 100644 --- a/src/engine-uiserver.c +++ b/src/engine-uiserver.c @@ -1,1455 +1,1455 @@ /* engine-uiserver.c - Uiserver engine. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* Peculiar: Use special keys from email address for recipient and signer (==sender). Use no data objects with encryption for prep_encrypt. */ #if HAVE_CONFIG_H #include #endif #include #include #ifdef HAVE_SYS_TYPES_H # include #endif #include #ifdef HAVE_UNISTD_H # include #endif #include #include /* FIXME */ #include #include "gpgme.h" #include "util.h" #include "ops.h" #include "wait.h" #include "priv-io.h" #include "sema.h" #include "data.h" #include "assuan.h" #include "debug.h" #include "engine-backend.h" typedef struct { int fd; /* FD we talk about. */ int server_fd;/* Server FD for this connection. */ int dir; /* Inbound/Outbound, maybe given implicit? */ void *data; /* Handler-specific data. */ void *tag; /* ID from the user for gpgme_remove_io_callback. */ char server_fd_str[15]; /* Same as SERVER_FD but as a string. We need this because _gpgme_io_fd2str can't be used on a closed descriptor. */ } iocb_data_t; struct engine_uiserver { assuan_context_t assuan_ctx; int lc_ctype_set; int lc_messages_set; gpgme_protocol_t protocol; iocb_data_t status_cb; /* Input, output etc are from the servers perspective. */ iocb_data_t input_cb; gpgme_data_t input_helper_data; /* Input helper data object. */ void *input_helper_memory; /* Input helper memory block. */ iocb_data_t output_cb; iocb_data_t message_cb; struct { engine_status_handler_t fnc; void *fnc_value; gpgme_status_cb_t mon_cb; void *mon_cb_value; } status; struct { engine_colon_line_handler_t fnc; void *fnc_value; struct { char *line; int linesize; int linelen; } attic; int any; /* any data line seen */ } colon; gpgme_data_t inline_data; /* Used to collect D lines. */ struct gpgme_io_cbs io_cbs; }; typedef struct engine_uiserver *engine_uiserver_t; static void uiserver_io_event (void *engine, gpgme_event_io_t type, void *type_data); static char * uiserver_get_version (const char *file_name) { (void)file_name; return NULL; } static const char * uiserver_get_req_version (void) { return NULL; } static void close_notify_handler (int fd, void *opaque) { engine_uiserver_t uiserver = opaque; assert (fd != -1); if (uiserver->status_cb.fd == fd) { if (uiserver->status_cb.tag) (*uiserver->io_cbs.remove) (uiserver->status_cb.tag); uiserver->status_cb.fd = -1; uiserver->status_cb.tag = NULL; } else if (uiserver->input_cb.fd == fd) { if (uiserver->input_cb.tag) (*uiserver->io_cbs.remove) (uiserver->input_cb.tag); uiserver->input_cb.fd = -1; uiserver->input_cb.tag = NULL; if (uiserver->input_helper_data) { gpgme_data_release (uiserver->input_helper_data); uiserver->input_helper_data = NULL; } if (uiserver->input_helper_memory) { free (uiserver->input_helper_memory); uiserver->input_helper_memory = NULL; } } else if (uiserver->output_cb.fd == fd) { if (uiserver->output_cb.tag) (*uiserver->io_cbs.remove) (uiserver->output_cb.tag); uiserver->output_cb.fd = -1; uiserver->output_cb.tag = NULL; } else if (uiserver->message_cb.fd == fd) { if (uiserver->message_cb.tag) (*uiserver->io_cbs.remove) (uiserver->message_cb.tag); uiserver->message_cb.fd = -1; uiserver->message_cb.tag = NULL; } } /* This is the default inquiry callback. We use it to handle the Pinentry notifications. */ static gpgme_error_t default_inq_cb (engine_uiserver_t uiserver, const char *line) { (void)uiserver; if (!strncmp (line, "PINENTRY_LAUNCHED", 17) && (line[17]==' '||!line[17])) { _gpgme_allow_set_foreground_window ((pid_t)strtoul (line+17, NULL, 10)); } return 0; } static gpgme_error_t uiserver_cancel (void *engine) { engine_uiserver_t uiserver = engine; if (!uiserver) return gpg_error (GPG_ERR_INV_VALUE); if (uiserver->status_cb.fd != -1) _gpgme_io_close (uiserver->status_cb.fd); if (uiserver->input_cb.fd != -1) _gpgme_io_close (uiserver->input_cb.fd); if (uiserver->output_cb.fd != -1) _gpgme_io_close (uiserver->output_cb.fd); if (uiserver->message_cb.fd != -1) _gpgme_io_close (uiserver->message_cb.fd); if (uiserver->assuan_ctx) { assuan_release (uiserver->assuan_ctx); uiserver->assuan_ctx = NULL; } return 0; } static void uiserver_release (void *engine) { engine_uiserver_t uiserver = engine; if (!uiserver) return; uiserver_cancel (engine); free (uiserver->colon.attic.line); free (uiserver); } static gpgme_error_t uiserver_new (void **engine, const char *file_name, const char *home_dir, const char *version) { gpgme_error_t err = 0; engine_uiserver_t uiserver; char *dft_display = NULL; char dft_ttyname[64]; char *env_tty = NULL; char *dft_ttytype = NULL; char *optstr; (void)home_dir; (void)version; /* Not yet used. */ uiserver = calloc (1, sizeof *uiserver); if (!uiserver) return gpg_error_from_syserror (); uiserver->protocol = GPGME_PROTOCOL_DEFAULT; uiserver->status_cb.fd = -1; uiserver->status_cb.dir = 1; uiserver->status_cb.tag = 0; uiserver->status_cb.data = uiserver; uiserver->input_cb.fd = -1; uiserver->input_cb.dir = 0; uiserver->input_cb.tag = 0; uiserver->input_cb.server_fd = -1; *uiserver->input_cb.server_fd_str = 0; uiserver->output_cb.fd = -1; uiserver->output_cb.dir = 1; uiserver->output_cb.tag = 0; uiserver->output_cb.server_fd = -1; *uiserver->output_cb.server_fd_str = 0; uiserver->message_cb.fd = -1; uiserver->message_cb.dir = 0; uiserver->message_cb.tag = 0; uiserver->message_cb.server_fd = -1; *uiserver->message_cb.server_fd_str = 0; uiserver->status.fnc = 0; uiserver->colon.fnc = 0; uiserver->colon.attic.line = 0; uiserver->colon.attic.linesize = 0; uiserver->colon.attic.linelen = 0; uiserver->colon.any = 0; uiserver->inline_data = NULL; uiserver->io_cbs.add = NULL; uiserver->io_cbs.add_priv = NULL; uiserver->io_cbs.remove = NULL; uiserver->io_cbs.event = NULL; uiserver->io_cbs.event_priv = NULL; err = assuan_new_ext (&uiserver->assuan_ctx, GPG_ERR_SOURCE_GPGME, &_gpgme_assuan_malloc_hooks, _gpgme_assuan_log_cb, NULL); if (err) goto leave; assuan_ctx_set_system_hooks (uiserver->assuan_ctx, &_gpgme_assuan_system_hooks); err = assuan_socket_connect (uiserver->assuan_ctx, file_name ? file_name : _gpgme_get_default_uisrv_socket (), 0, ASSUAN_SOCKET_SERVER_FDPASSING); if (err) goto leave; err = _gpgme_getenv ("DISPLAY", &dft_display); if (err) goto leave; if (dft_display) { if (gpgrt_asprintf (&optstr, "OPTION display=%s", dft_display) < 0) { err = gpg_error_from_syserror (); free (dft_display); goto leave; } free (dft_display); err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } err = _gpgme_getenv ("GPG_TTY", &env_tty); if (isatty (1) || env_tty || err) { int rc = 0; if (err) goto leave; else if (env_tty) { snprintf (dft_ttyname, sizeof (dft_ttyname), "%s", env_tty); free (env_tty); } else rc = ttyname_r (1, dft_ttyname, sizeof (dft_ttyname)); /* Even though isatty() returns 1, ttyname_r() may fail in many ways, e.g., when /dev/pts is not accessible under chroot. */ if (!rc) { if (gpgrt_asprintf (&optstr, "OPTION ttyname=%s", dft_ttyname) < 0) { err = gpg_error_from_syserror (); goto leave; } err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; err = _gpgme_getenv ("TERM", &dft_ttytype); if (err) goto leave; if (dft_ttytype) { if (gpgrt_asprintf (&optstr, "OPTION ttytype=%s", dft_ttytype)< 0) { err = gpg_error_from_syserror (); free (dft_ttytype); goto leave; } free (dft_ttytype); err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); if (err) goto leave; } } } #ifdef HAVE_W32_SYSTEM /* Under Windows we need to use AllowSetForegroundWindow. Tell uiserver to tell us when it needs it. */ if (!err) { err = assuan_transact (uiserver->assuan_ctx, "OPTION allow-pinentry-notify", NULL, NULL, NULL, NULL, NULL, NULL); if (gpg_err_code (err) == GPG_ERR_UNKNOWN_OPTION) err = 0; /* This is a new feature of uiserver. */ } #endif /*HAVE_W32_SYSTEM*/ leave: if (err) uiserver_release (uiserver); else *engine = uiserver; return err; } static gpgme_error_t uiserver_set_locale (void *engine, int category, const char *value) { engine_uiserver_t uiserver = engine; gpgme_error_t err; char *optstr; const char *catstr; /* FIXME: If value is NULL, we need to reset the option to default. But we can't do this. So we error out here. UISERVER needs support for this. */ if (category == LC_CTYPE) { catstr = "lc-ctype"; if (!value && uiserver->lc_ctype_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) uiserver->lc_ctype_set = 1; } #ifdef LC_MESSAGES else if (category == LC_MESSAGES) { catstr = "lc-messages"; if (!value && uiserver->lc_messages_set) return gpg_error (GPG_ERR_INV_VALUE); if (value) uiserver->lc_messages_set = 1; } #endif /* LC_MESSAGES */ else return gpg_error (GPG_ERR_INV_VALUE); /* FIXME: Reset value to default. */ if (!value) return 0; if (gpgrt_asprintf (&optstr, "OPTION %s=%s", catstr, value) < 0) err = gpg_error_from_syserror (); else { err = assuan_transact (uiserver->assuan_ctx, optstr, NULL, NULL, NULL, NULL, NULL, NULL); gpgrt_free (optstr); } return err; } static gpgme_error_t uiserver_set_protocol (void *engine, gpgme_protocol_t protocol) { engine_uiserver_t uiserver = engine; if (protocol != GPGME_PROTOCOL_OpenPGP && protocol != GPGME_PROTOCOL_CMS && protocol != GPGME_PROTOCOL_DEFAULT) return gpg_error (GPG_ERR_INV_VALUE); uiserver->protocol = protocol; return 0; } static gpgme_error_t uiserver_assuan_simple_command (engine_uiserver_t uiserver, const char *cmd, engine_status_handler_t status_fnc, void *status_fnc_value) { assuan_context_t ctx = uiserver->assuan_ctx; gpg_error_t err; char *line; size_t linelen; err = assuan_write_line (ctx, cmd); if (err) return err; do { err = assuan_read_line (ctx, &line, &linelen); if (err) return err; if (*line == '#' || !linelen) continue; if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) return 0; else if (linelen >= 4 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && line[3] == ' ') err = atoi (&line[4]); else if (linelen >= 2 && line[0] == 'S' && line[1] == ' ') { char *rest; gpgme_status_code_t r; rest = strchr (line + 2, ' '); if (!rest) rest = line + linelen; /* set to an empty string */ else *(rest++) = 0; r = _gpgme_parse_status (line + 2); if (uiserver->status.mon_cb && r != GPGME_STATUS_PROGRESS) { /* Note that we call the monitor even if we do * not know the status code (r < 0). */ err = uiserver->status.mon_cb (uiserver->status.mon_cb_value, line + 2, rest); } if (err) ; else if (r >= 0 && status_fnc) err = status_fnc (status_fnc_value, r, rest); else err = gpg_error (GPG_ERR_GENERAL); } else err = gpg_error (GPG_ERR_GENERAL); } while (!err); return err; } typedef enum { INPUT_FD, OUTPUT_FD, MESSAGE_FD } fd_type_t; #define COMMANDLINELEN 40 static gpgme_error_t uiserver_set_fd (engine_uiserver_t uiserver, fd_type_t fd_type, const char *opt) { gpg_error_t err = 0; char line[COMMANDLINELEN]; const char *which; iocb_data_t *iocb_data; int dir; switch (fd_type) { case INPUT_FD: which = "INPUT"; iocb_data = &uiserver->input_cb; break; case OUTPUT_FD: which = "OUTPUT"; iocb_data = &uiserver->output_cb; break; case MESSAGE_FD: which = "MESSAGE"; iocb_data = &uiserver->message_cb; break; default: return gpg_error (GPG_ERR_INV_VALUE); } dir = iocb_data->dir; /* We try to short-cut the communication by giving UISERVER direct access to the file descriptor, rather than using a pipe. */ iocb_data->server_fd = _gpgme_data_get_fd (iocb_data->data); if (iocb_data->server_fd < 0) { int fds[2]; if (_gpgme_io_pipe (fds, 0) < 0) return gpg_error_from_syserror (); iocb_data->fd = dir ? fds[0] : fds[1]; iocb_data->server_fd = dir ? fds[1] : fds[0]; if (_gpgme_io_set_close_notify (iocb_data->fd, close_notify_handler, uiserver)) { err = gpg_error (GPG_ERR_GENERAL); goto leave_set_fd; } } err = assuan_sendfd (uiserver->assuan_ctx, iocb_data->server_fd); if (err) goto leave_set_fd; _gpgme_io_close (iocb_data->server_fd); iocb_data->server_fd = -1; if (opt) snprintf (line, COMMANDLINELEN, "%s FD %s", which, opt); else snprintf (line, COMMANDLINELEN, "%s FD", which); err = uiserver_assuan_simple_command (uiserver, line, NULL, NULL); leave_set_fd: if (err) { _gpgme_io_close (iocb_data->fd); iocb_data->fd = -1; if (iocb_data->server_fd != -1) { _gpgme_io_close (iocb_data->server_fd); iocb_data->server_fd = -1; } } return err; } static const char * map_data_enc (gpgme_data_t d) { switch (gpgme_data_get_encoding (d)) { case GPGME_DATA_ENCODING_NONE: break; case GPGME_DATA_ENCODING_BINARY: return "--binary"; case GPGME_DATA_ENCODING_BASE64: return "--base64"; case GPGME_DATA_ENCODING_ARMOR: return "--armor"; default: break; } return NULL; } static gpgme_error_t status_handler (void *opaque, int fd) { struct io_cb_data *data = (struct io_cb_data *) opaque; engine_uiserver_t uiserver = (engine_uiserver_t) data->handler_value; gpgme_error_t err = 0; char *line; size_t linelen; do { err = assuan_read_line (uiserver->assuan_ctx, &line, &linelen); if (err) { /* Try our best to terminate the connection friendly. */ /* assuan_write_line (uiserver->assuan_ctx, "BYE"); */ TRACE3 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: error from assuan (%d) getting status line : %s", fd, err, gpg_strerror (err)); } else if (linelen >= 3 && line[0] == 'E' && line[1] == 'R' && line[2] == 'R' && (line[3] == '\0' || line[3] == ' ')) { if (line[3] == ' ') err = atoi (&line[4]); if (! err) err = gpg_error (GPG_ERR_GENERAL); TRACE2 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: ERR line - mapped to: %s", fd, err ? gpg_strerror (err) : "ok"); /* Try our best to terminate the connection friendly. */ /* assuan_write_line (uiserver->assuan_ctx, "BYE"); */ } else if (linelen >= 2 && line[0] == 'O' && line[1] == 'K' && (line[2] == '\0' || line[2] == ' ')) { if (uiserver->status.fnc) { char emptystring[1] = {0}; err = uiserver->status.fnc (uiserver->status.fnc_value, GPGME_STATUS_EOF, emptystring); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ } if (!err && uiserver->colon.fnc && uiserver->colon.any) { /* We must tell a colon function about the EOF. We do this only when we have seen any data lines. Note that this inlined use of colon data lines will eventually be changed into using a regular data channel. */ uiserver->colon.any = 0; err = uiserver->colon.fnc (uiserver->colon.fnc_value, NULL); } TRACE2 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: OK line - final status: %s", fd, err ? gpg_strerror (err) : "ok"); _gpgme_io_close (uiserver->status_cb.fd); return err; } else if (linelen > 2 && line[0] == 'D' && line[1] == ' ' && uiserver->colon.fnc) { /* We are using the colon handler even for plain inline data - strange name for that function but for historic reasons we keep it. */ /* FIXME We can't use this for binary data because we assume this is a string. For the current usage of colon output it is correct. */ char *src = line + 2; char *end = line + linelen; char *dst; char **aline = &uiserver->colon.attic.line; int *alinelen = &uiserver->colon.attic.linelen; if (uiserver->colon.attic.linesize < *alinelen + linelen + 1) { char *newline = realloc (*aline, *alinelen + linelen + 1); if (!newline) err = gpg_error_from_syserror (); else { *aline = newline; uiserver->colon.attic.linesize = *alinelen + linelen + 1; } } if (!err) { dst = *aline + *alinelen; while (!err && src < end) { if (*src == '%' && src + 2 < end) { /* Handle escaped characters. */ ++src; *dst = _gpgme_hextobyte (src); (*alinelen)++; src += 2; } else { *dst = *src++; (*alinelen)++; } if (*dst == '\n') { /* Terminate the pending line, pass it to the colon handler and reset it. */ uiserver->colon.any = 1; if (*alinelen > 1 && *(dst - 1) == '\r') dst--; *dst = '\0'; /* FIXME How should we handle the return code? */ err = uiserver->colon.fnc (uiserver->colon.fnc_value, *aline); if (!err) { dst = *aline; *alinelen = 0; } } else dst++; } } TRACE2 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: D line; final status: %s", fd, err? gpg_strerror (err):"ok"); } else if (linelen > 2 && line[0] == 'D' && line[1] == ' ' && uiserver->inline_data) { char *src = line + 2; char *end = line + linelen; char *dst = src; gpgme_ssize_t nwritten; linelen = 0; while (src < end) { if (*src == '%' && src + 2 < end) { /* Handle escaped characters. */ ++src; *dst++ = _gpgme_hextobyte (src); src += 2; } else *dst++ = *src++; linelen++; } src = line + 2; while (linelen > 0) { nwritten = gpgme_data_write (uiserver->inline_data, src, linelen); if (!nwritten || (nwritten < 0 && errno != EINTR) || nwritten > linelen) { err = gpg_error_from_syserror (); break; } src += nwritten; linelen -= nwritten; } TRACE2 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: D inlinedata; final status: %s", fd, err? gpg_strerror (err):"ok"); } else if (linelen > 2 && line[0] == 'S' && line[1] == ' ') { char *rest; gpgme_status_code_t r; rest = strchr (line + 2, ' '); if (!rest) rest = line + linelen; /* set to an empty string */ else *(rest++) = 0; r = _gpgme_parse_status (line + 2); if (r >= 0) { if (uiserver->status.fnc) { err = uiserver->status.fnc (uiserver->status.fnc_value, r, rest); if (gpg_err_code (err) == GPG_ERR_FALSE) err = 0; /* Drop special error code. */ } } else fprintf (stderr, "[UNKNOWN STATUS]%s %s", line + 2, rest); TRACE3 (DEBUG_CTX, "gpgme:status_handler", uiserver, "fd 0x%x: S line (%s) - final status: %s", fd, line+2, err? gpg_strerror (err):"ok"); } else if (linelen >= 7 && line[0] == 'I' && line[1] == 'N' && line[2] == 'Q' && line[3] == 'U' && line[4] == 'I' && line[5] == 'R' && line[6] == 'E' && (line[7] == '\0' || line[7] == ' ')) { char *keyword = line+7; while (*keyword == ' ') keyword++;; default_inq_cb (uiserver, keyword); assuan_write_line (uiserver->assuan_ctx, "END"); } } while (!err && assuan_pending_line (uiserver->assuan_ctx)); return err; } static gpgme_error_t add_io_cb (engine_uiserver_t uiserver, iocb_data_t *iocbd, gpgme_io_cb_t handler) { gpgme_error_t err; TRACE_BEG2 (DEBUG_ENGINE, "engine-uiserver:add_io_cb", uiserver, "fd %d, dir %d", iocbd->fd, iocbd->dir); err = (*uiserver->io_cbs.add) (uiserver->io_cbs.add_priv, iocbd->fd, iocbd->dir, handler, iocbd->data, &iocbd->tag); if (err) return TRACE_ERR (err); if (!iocbd->dir) /* FIXME Kludge around poll() problem. */ err = _gpgme_io_set_nonblocking (iocbd->fd); return TRACE_ERR (err); } static gpgme_error_t start (engine_uiserver_t uiserver, const char *command) { gpgme_error_t err; int fdlist[5]; int nfds; /* We need to know the fd used by assuan for reads. We do this by using the assumption that the first returned fd from assuan_get_active_fds() is always this one. */ nfds = assuan_get_active_fds (uiserver->assuan_ctx, 0 /* read fds */, fdlist, DIM (fdlist)); if (nfds < 1) return gpg_error (GPG_ERR_GENERAL); /* FIXME */ /* We "duplicate" the file descriptor, so we can close it here (we can't close fdlist[0], as that is closed by libassuan, and closing it here might cause libassuan to close some unrelated FD later). Alternatively, we could special case status_fd and register/unregister it manually as needed, but this increases code duplication and is more complicated as we can not use the close notifications etc. A third alternative would be to let Assuan know that we closed the FD, but that complicates the Assuan interface. */ uiserver->status_cb.fd = _gpgme_io_dup (fdlist[0]); if (uiserver->status_cb.fd < 0) return gpg_error_from_syserror (); if (_gpgme_io_set_close_notify (uiserver->status_cb.fd, close_notify_handler, uiserver)) { _gpgme_io_close (uiserver->status_cb.fd); uiserver->status_cb.fd = -1; return gpg_error (GPG_ERR_GENERAL); } err = add_io_cb (uiserver, &uiserver->status_cb, status_handler); if (!err && uiserver->input_cb.fd != -1) err = add_io_cb (uiserver, &uiserver->input_cb, _gpgme_data_outbound_handler); if (!err && uiserver->output_cb.fd != -1) err = add_io_cb (uiserver, &uiserver->output_cb, _gpgme_data_inbound_handler); if (!err && uiserver->message_cb.fd != -1) err = add_io_cb (uiserver, &uiserver->message_cb, _gpgme_data_outbound_handler); if (!err) err = assuan_write_line (uiserver->assuan_ctx, command); if (!err) uiserver_io_event (uiserver, GPGME_EVENT_START, NULL); return err; } static gpgme_error_t uiserver_reset (void *engine) { engine_uiserver_t uiserver = engine; /* We must send a reset because we need to reset the list of signers. Note that RESET does not reset OPTION commands. */ return uiserver_assuan_simple_command (uiserver, "RESET", NULL, NULL); } static gpgme_error_t uiserver_decrypt (void *engine, gpgme_decrypt_flags_t flags, gpgme_data_t ciph, gpgme_data_t plain, int export_session_key, const char *override_session_key, int auto_key_retrieve) { engine_uiserver_t uiserver = engine; gpgme_error_t err; const char *protocol; char *cmd; int verify = !!(flags & GPGME_DECRYPT_VERIFY); (void)override_session_key; /* Fixme: We need to see now to add this * to the UI server protocol */ (void)auto_key_retrieve; /* Not yet supported. */ if (!uiserver) return gpg_error (GPG_ERR_INV_VALUE); if (uiserver->protocol == GPGME_PROTOCOL_DEFAULT) protocol = ""; else if (uiserver->protocol == GPGME_PROTOCOL_OpenPGP) protocol = " --protocol=OpenPGP"; else if (uiserver->protocol == GPGME_PROTOCOL_CMS) protocol = " --protocol=CMS"; else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); if (gpgrt_asprintf (&cmd, "DECRYPT%s%s%s", protocol, verify ? "" : " --no-verify", export_session_key ? " --export-session-key" : "") < 0) return gpg_error_from_syserror (); uiserver->input_cb.data = ciph; err = uiserver_set_fd (uiserver, INPUT_FD, map_data_enc (uiserver->input_cb.data)); if (err) { gpgrt_free (cmd); return gpg_error (GPG_ERR_GENERAL); /* FIXME */ } uiserver->output_cb.data = plain; err = uiserver_set_fd (uiserver, OUTPUT_FD, 0); if (err) { gpgrt_free (cmd); return gpg_error (GPG_ERR_GENERAL); /* FIXME */ } uiserver->inline_data = NULL; err = start (engine, cmd); gpgrt_free (cmd); return err; } static gpgme_error_t set_recipients (engine_uiserver_t uiserver, gpgme_key_t recp[]) { gpgme_error_t err = 0; char *line; int linelen; int invalid_recipients = 0; int i; linelen = 10 + 40 + 1; /* "RECIPIENT " + guess + '\0'. */ line = malloc (10 + 40 + 1); if (!line) return gpg_error_from_syserror (); strcpy (line, "RECIPIENT "); for (i=0; !err && recp[i]; i++) { char *uid; int newlen; /* We use only the first user ID of the key. */ if (!recp[i]->uids || !(uid=recp[i]->uids->uid) || !*uid) { invalid_recipients++; continue; } newlen = 11 + strlen (uid); if (linelen < newlen) { char *newline = realloc (line, newlen); if (! newline) { int saved_err = gpg_error_from_syserror (); free (line); return saved_err; } line = newline; linelen = newlen; } /* FIXME: need to do proper escaping */ strcpy (&line[10], uid); err = uiserver_assuan_simple_command (uiserver, line, uiserver->status.fnc, uiserver->status.fnc_value); /* FIXME: This might requires more work. */ if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY) invalid_recipients++; else if (err) { free (line); return err; } } free (line); return gpg_error (invalid_recipients ? GPG_ERR_UNUSABLE_PUBKEY : GPG_ERR_NO_ERROR); } /* Take recipients from the LF delimited STRING and send RECIPIENT * commands to gpgsm. */ static gpgme_error_t set_recipients_from_string (engine_uiserver_t uiserver, const char *string) { gpg_error_t err = 0; char *line = NULL; int no_pubkey = 0; const char *s; int n; for (;;) { while (*string == ' ' || *string == '\t') string++; if (!*string) break; s = strchr (string, '\n'); if (s) n = s - string; else n = strlen (string); while (n && (string[n-1] == ' ' || string[n-1] == '\t')) n--; gpgrt_free (line); if (gpgrt_asprintf (&line, "RECIPIENT %.*s", n, string) < 0) { err = gpg_error_from_syserror (); break; } string += n + !!s; err = uiserver_assuan_simple_command (uiserver, line, uiserver->status.fnc, uiserver->status.fnc_value); /* Fixme: Improve error reporting. */ if (gpg_err_code (err) == GPG_ERR_NO_PUBKEY) no_pubkey++; else if (err) break; } gpgrt_free (line); return err? err : no_pubkey? gpg_error (GPG_ERR_NO_PUBKEY) : 0; } static gpgme_error_t uiserver_encrypt (void *engine, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor) { engine_uiserver_t uiserver = engine; gpgme_error_t err; const char *protocol; char *cmd; if (!uiserver) return gpg_error (GPG_ERR_INV_VALUE); if (uiserver->protocol == GPGME_PROTOCOL_DEFAULT) protocol = ""; else if (uiserver->protocol == GPGME_PROTOCOL_OpenPGP) protocol = " --protocol=OpenPGP"; else if (uiserver->protocol == GPGME_PROTOCOL_CMS) protocol = " --protocol=CMS"; else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); if (flags & GPGME_ENCRYPT_PREPARE) { if (!recp || plain || ciph) return gpg_error (GPG_ERR_INV_VALUE); if (gpgrt_asprintf (&cmd, "PREP_ENCRYPT%s%s", protocol, (flags & GPGME_ENCRYPT_EXPECT_SIGN) ? " --expect-sign" : "") < 0) return gpg_error_from_syserror (); } else { if (!plain || !ciph) return gpg_error (GPG_ERR_INV_VALUE); if (gpgrt_asprintf (&cmd, "ENCRYPT%s", protocol) < 0) return gpg_error_from_syserror (); } if (plain) { uiserver->input_cb.data = plain; err = uiserver_set_fd (uiserver, INPUT_FD, map_data_enc (uiserver->input_cb.data)); if (err) { gpgrt_free (cmd); return err; } } if (ciph) { uiserver->output_cb.data = ciph; err = uiserver_set_fd (uiserver, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (uiserver->output_cb.data)); if (err) { gpgrt_free (cmd); return err; } } uiserver->inline_data = NULL; if (recp || recpstring) { if (recp) err = set_recipients (uiserver, recp); else err = set_recipients_from_string (uiserver, recpstring); if (err) { gpgrt_free (cmd); return err; } } err = start (uiserver, cmd); gpgrt_free (cmd); return err; } static gpgme_error_t uiserver_sign (void *engine, gpgme_data_t in, gpgme_data_t out, gpgme_sig_mode_t mode, int use_armor, int use_textmode, int include_certs, gpgme_ctx_t ctx /* FIXME */) { engine_uiserver_t uiserver = engine; gpgme_error_t err = 0; const char *protocol; char *cmd; gpgme_key_t key; (void)use_textmode; (void)include_certs; if (!uiserver || !in || !out) return gpg_error (GPG_ERR_INV_VALUE); if (uiserver->protocol == GPGME_PROTOCOL_DEFAULT) protocol = ""; else if (uiserver->protocol == GPGME_PROTOCOL_OpenPGP) protocol = " --protocol=OpenPGP"; else if (uiserver->protocol == GPGME_PROTOCOL_CMS) protocol = " --protocol=CMS"; else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); if (gpgrt_asprintf (&cmd, "SIGN%s%s", protocol, (mode == GPGME_SIG_MODE_DETACH) ? " --detached" : "") < 0) return gpg_error_from_syserror (); key = gpgme_signers_enum (ctx, 0); if (key) { const char *s = NULL; if (key && key->uids) s = key->uids->email; if (s && strlen (s) < 80) { char buf[100]; strcpy (stpcpy (buf, "SENDER --info "), s); err = uiserver_assuan_simple_command (uiserver, buf, uiserver->status.fnc, uiserver->status.fnc_value); } else err = gpg_error (GPG_ERR_INV_VALUE); gpgme_key_unref (key); if (err) { gpgrt_free (cmd); return err; } } uiserver->input_cb.data = in; err = uiserver_set_fd (uiserver, INPUT_FD, map_data_enc (uiserver->input_cb.data)); if (err) { gpgrt_free (cmd); return err; } uiserver->output_cb.data = out; err = uiserver_set_fd (uiserver, OUTPUT_FD, use_armor ? "--armor" : map_data_enc (uiserver->output_cb.data)); if (err) { gpgrt_free (cmd); return err; } uiserver->inline_data = NULL; err = start (uiserver, cmd); gpgrt_free (cmd); return err; } /* FIXME: Missing a way to specify --silent. */ static gpgme_error_t uiserver_verify (void *engine, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext, gpgme_ctx_t ctx) { engine_uiserver_t uiserver = engine; gpgme_error_t err; const char *protocol; char *cmd; (void)ctx; /* FIXME: We should to add a --sender option to the * UISever protocol. */ if (!uiserver) return gpg_error (GPG_ERR_INV_VALUE); if (uiserver->protocol == GPGME_PROTOCOL_DEFAULT) protocol = ""; else if (uiserver->protocol == GPGME_PROTOCOL_OpenPGP) protocol = " --protocol=OpenPGP"; else if (uiserver->protocol == GPGME_PROTOCOL_CMS) protocol = " --protocol=CMS"; else return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); if (gpgrt_asprintf (&cmd, "VERIFY%s", protocol) < 0) return gpg_error_from_syserror (); uiserver->input_cb.data = sig; err = uiserver_set_fd (uiserver, INPUT_FD, map_data_enc (uiserver->input_cb.data)); if (err) { gpgrt_free (cmd); return err; } if (plaintext) { /* Normal or cleartext signature. */ uiserver->output_cb.data = plaintext; err = uiserver_set_fd (uiserver, OUTPUT_FD, 0); } else { /* Detached signature. */ uiserver->message_cb.data = signed_text; err = uiserver_set_fd (uiserver, MESSAGE_FD, 0); } uiserver->inline_data = NULL; if (!err) err = start (uiserver, cmd); gpgrt_free (cmd); return err; } /* This sets a status callback for monitoring status lines before they * are passed to a caller set handler. */ static void uiserver_set_status_cb (void *engine, gpgme_status_cb_t cb, void *cb_value) { engine_uiserver_t uiserver = engine; uiserver->status.mon_cb = cb; uiserver->status.mon_cb_value = cb_value; } static void uiserver_set_status_handler (void *engine, engine_status_handler_t fnc, void *fnc_value) { engine_uiserver_t uiserver = engine; uiserver->status.fnc = fnc; uiserver->status.fnc_value = fnc_value; } static gpgme_error_t uiserver_set_colon_line_handler (void *engine, engine_colon_line_handler_t fnc, void *fnc_value) { engine_uiserver_t uiserver = engine; uiserver->colon.fnc = fnc; uiserver->colon.fnc_value = fnc_value; uiserver->colon.any = 0; return 0; } static void uiserver_set_io_cbs (void *engine, gpgme_io_cbs_t io_cbs) { engine_uiserver_t uiserver = engine; uiserver->io_cbs = *io_cbs; } static void uiserver_io_event (void *engine, gpgme_event_io_t type, void *type_data) { engine_uiserver_t uiserver = engine; TRACE3 (DEBUG_ENGINE, "gpgme:uiserver_io_event", uiserver, "event %p, type %d, type_data %p", uiserver->io_cbs.event, type, type_data); if (uiserver->io_cbs.event) (*uiserver->io_cbs.event) (uiserver->io_cbs.event_priv, type, type_data); } struct engine_ops _gpgme_engine_ops_uiserver = { /* Static functions. */ _gpgme_get_default_uisrv_socket, NULL, uiserver_get_version, uiserver_get_req_version, uiserver_new, /* Member functions. */ uiserver_release, uiserver_reset, uiserver_set_status_cb, uiserver_set_status_handler, NULL, /* set_command_handler */ uiserver_set_colon_line_handler, uiserver_set_locale, uiserver_set_protocol, NULL, /* set_engine_flags */ uiserver_decrypt, NULL, /* delete */ NULL, /* edit */ uiserver_encrypt, NULL, /* encrypt_sign */ NULL, /* export */ NULL, /* export_ext */ NULL, /* genkey */ NULL, /* import */ NULL, /* keylist */ NULL, /* keylist_ext */ NULL, /* keylist_data */ NULL, /* keysign */ NULL, /* tofu_policy */ uiserver_sign, NULL, /* trustlist */ uiserver_verify, NULL, /* getauditlog */ NULL, /* opassuan_transact */ NULL, /* conf_load */ NULL, /* conf_save */ NULL, /* conf_dir */ NULL, /* query_swdb */ uiserver_set_io_cbs, uiserver_io_event, uiserver_cancel, NULL, /* cancel_op */ NULL, /* passwd */ NULL, /* set_pinentry_mode */ NULL /* opspawn */ }; diff --git a/src/engine.c b/src/engine.c index b629bea7..b3df01aa 100644 --- a/src/engine.c +++ b/src/engine.c @@ -1,1129 +1,1130 @@ /* engine.c - GPGME engine support. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2006, 2009, 2010 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2006, 2009, 2010 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "gpgme.h" #include "util.h" #include "sema.h" #include "ops.h" #include "debug.h" #include "engine.h" #include "engine-backend.h" struct engine { struct engine_ops *ops; void *engine; }; static struct engine_ops *engine_ops[] = { &_gpgme_engine_ops_gpg, /* OpenPGP. */ &_gpgme_engine_ops_gpgsm, /* CMS. */ &_gpgme_engine_ops_gpgconf, /* gpg-conf. */ &_gpgme_engine_ops_assuan, /* Low-Level Assuan. */ &_gpgme_engine_ops_g13, /* Crypto VFS. */ #ifdef ENABLE_UISERVER &_gpgme_engine_ops_uiserver, /* UI-Server. */ #else NULL, #endif &_gpgme_engine_ops_spawn }; /* The engine info. */ static gpgme_engine_info_t engine_info; DEFINE_STATIC_LOCK (engine_info_lock); /* If non-NULL, the minimal version required for all engines. */ static char *engine_minimal_version; /* Get the file name of the engine for PROTOCOL. */ static const char * engine_get_file_name (gpgme_protocol_t proto) { if (proto > DIM (engine_ops)) return NULL; if (engine_ops[proto] && engine_ops[proto]->get_file_name) return (*engine_ops[proto]->get_file_name) (); else return NULL; } /* Get the standard home dir of the engine for PROTOCOL. */ static const char * engine_get_home_dir (gpgme_protocol_t proto) { if (proto > DIM (engine_ops)) return NULL; if (engine_ops[proto] && engine_ops[proto]->get_home_dir) return (*engine_ops[proto]->get_home_dir) (); else return NULL; } /* Get a malloced string containing the version number of the engine * for PROTOCOL. If this function returns NULL for a valid protocol, * it should be assumed that the engine is a pseudo engine. */ static char * engine_get_version (gpgme_protocol_t proto, const char *file_name) { if (proto > DIM (engine_ops)) return NULL; if (engine_ops[proto] && engine_ops[proto]->get_version) return (*engine_ops[proto]->get_version) (file_name); else return NULL; } /* Get the required version number of the engine for PROTOCOL. This * may be NULL. */ static const char * engine_get_req_version (gpgme_protocol_t proto) { if (proto > DIM (engine_ops)) return NULL; if (engine_ops[proto] && engine_ops[proto]->get_req_version) return (*engine_ops[proto]->get_req_version) (); else return NULL; } /* Verify the version requirement for the engine for PROTOCOL. */ gpgme_error_t gpgme_engine_check_version (gpgme_protocol_t proto) { gpgme_error_t err; gpgme_engine_info_t info; int result; LOCK (engine_info_lock); info = engine_info; if (!info) { /* Make sure it is initialized. */ UNLOCK (engine_info_lock); err = gpgme_get_engine_info (&info); if (err) return err; LOCK (engine_info_lock); } while (info && info->protocol != proto) info = info->next; if (!info) result = 0; else result = _gpgme_compare_versions (info->version, info->req_version); UNLOCK (engine_info_lock); return result ? 0 : trace_gpg_error (GPG_ERR_INV_ENGINE); } /* Release the engine info INFO. */ void _gpgme_engine_info_release (gpgme_engine_info_t info) { while (info) { gpgme_engine_info_t next_info = info->next; if (info->file_name) free (info->file_name); if (info->home_dir) free (info->home_dir); if (info->version) free (info->version); free (info); info = next_info; } } /* This is an internal function to set a mimimal required version. * This function must only be called by gpgme_set_global_flag. * Returns 0 on success. */ int _gpgme_set_engine_minimal_version (const char *value) { free (engine_minimal_version); if (value) { engine_minimal_version = strdup (value); return !engine_minimal_version; } else { engine_minimal_version = NULL; return 0; } } /* Get the information about the configured and installed engines. A pointer to the first engine in the statically allocated linked list is returned in *INFO. If an error occurs, it is returned. The returned data is valid until the next gpgme_set_engine_info. */ gpgme_error_t gpgme_get_engine_info (gpgme_engine_info_t *info) { gpgme_error_t err; LOCK (engine_info_lock); if (!engine_info) { gpgme_engine_info_t *lastp = &engine_info; gpgme_protocol_t proto_list[] = { GPGME_PROTOCOL_OpenPGP, GPGME_PROTOCOL_CMS, GPGME_PROTOCOL_GPGCONF, GPGME_PROTOCOL_ASSUAN, GPGME_PROTOCOL_G13, GPGME_PROTOCOL_UISERVER, GPGME_PROTOCOL_SPAWN }; unsigned int proto; err = 0; for (proto = 0; proto < DIM (proto_list); proto++) { const char *ofile_name = engine_get_file_name (proto_list[proto]); const char *ohome_dir = engine_get_home_dir (proto_list[proto]); char *version = engine_get_version (proto_list[proto], NULL); char *file_name; char *home_dir; if (!ofile_name) continue; file_name = strdup (ofile_name); if (!file_name) err = gpg_error_from_syserror (); if (ohome_dir) { home_dir = strdup (ohome_dir); if (!home_dir && !err) err = gpg_error_from_syserror (); } else home_dir = NULL; *lastp = calloc (1, sizeof (*engine_info)); if (!*lastp && !err) err = gpg_error_from_syserror (); /* Check against the optional minimal engine version. */ if (!err && version && engine_minimal_version && !_gpgme_compare_versions (version, engine_minimal_version)) { #if GPG_ERROR_VERSION_NUMBER < 0x011900 /* 1.25 */ err = gpg_error (GPG_ERR_NO_ENGINE); #else err = gpg_error (GPG_ERR_ENGINE_TOO_OLD); #endif } /* Now set the dummy version for pseudo engines. */ if (!err && !version) { version = strdup ("1.0.0"); if (!version) err = gpg_error_from_syserror (); } if (err) { _gpgme_engine_info_release (engine_info); engine_info = NULL; if (file_name) free (file_name); if (home_dir) free (home_dir); if (version) free (version); UNLOCK (engine_info_lock); return err; } (*lastp)->protocol = proto_list[proto]; (*lastp)->file_name = file_name; (*lastp)->home_dir = home_dir; (*lastp)->version = version; (*lastp)->req_version = engine_get_req_version (proto_list[proto]); if (!(*lastp)->req_version) (*lastp)->req_version = "1.0.0"; /* Dummy for pseudo engines. */ (*lastp)->next = NULL; lastp = &(*lastp)->next; } } *info = engine_info; UNLOCK (engine_info_lock); return 0; } /* Get a deep copy of the engine info and return it in INFO. */ gpgme_error_t _gpgme_engine_info_copy (gpgme_engine_info_t *r_info) { gpgme_error_t err = 0; gpgme_engine_info_t info; gpgme_engine_info_t new_info; gpgme_engine_info_t *lastp; LOCK (engine_info_lock); info = engine_info; if (!info) { /* Make sure it is initialized. */ UNLOCK (engine_info_lock); err = gpgme_get_engine_info (&info); if (err) return err; LOCK (engine_info_lock); } new_info = NULL; lastp = &new_info; while (info) { char *file_name; char *home_dir; char *version; assert (info->file_name); file_name = strdup (info->file_name); if (!file_name) err = gpg_error_from_syserror (); if (info->home_dir) { home_dir = strdup (info->home_dir); if (!home_dir && !err) err = gpg_error_from_syserror (); } else home_dir = NULL; if (info->version) { version = strdup (info->version); if (!version && !err) err = gpg_error_from_syserror (); } else version = NULL; *lastp = malloc (sizeof (*engine_info)); if (!*lastp && !err) err = gpg_error_from_syserror (); if (err) { _gpgme_engine_info_release (new_info); if (file_name) free (file_name); if (home_dir) free (home_dir); if (version) free (version); UNLOCK (engine_info_lock); return err; } (*lastp)->protocol = info->protocol; (*lastp)->file_name = file_name; (*lastp)->home_dir = home_dir; (*lastp)->version = version; (*lastp)->req_version = info->req_version; (*lastp)->next = NULL; lastp = &(*lastp)->next; info = info->next; } *r_info = new_info; UNLOCK (engine_info_lock); return 0; } /* Set the engine info for the info list INFO, protocol PROTO, to the file name FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t _gpgme_set_engine_info (gpgme_engine_info_t info, gpgme_protocol_t proto, const char *file_name, const char *home_dir) { char *new_file_name; char *new_home_dir; char *new_version; /* FIXME: Use some PROTO_MAX definition. */ if (proto > DIM (engine_ops)) return gpg_error (GPG_ERR_INV_VALUE); while (info && info->protocol != proto) info = info->next; if (!info) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Prepare new members. */ if (file_name) new_file_name = strdup (file_name); else { const char *ofile_name = engine_get_file_name (proto); assert (ofile_name); new_file_name = strdup (ofile_name); } if (!new_file_name) return gpg_error_from_syserror (); if (home_dir) { new_home_dir = strdup (home_dir); if (!new_home_dir) { free (new_file_name); return gpg_error_from_syserror (); } } else { const char *ohome_dir = engine_get_home_dir (proto); if (ohome_dir) { new_home_dir = strdup (ohome_dir); if (!new_home_dir) { free (new_file_name); return gpg_error_from_syserror (); } } else new_home_dir = NULL; } new_version = engine_get_version (proto, new_file_name); if (!new_version) { new_version = strdup ("1.0.0"); /* Fake one for dummy entries. */ if (!new_version) { free (new_file_name); free (new_home_dir); } } /* Remove the old members. */ assert (info->file_name); free (info->file_name); if (info->home_dir) free (info->home_dir); if (info->version) free (info->version); /* Install the new members. */ info->file_name = new_file_name; info->home_dir = new_home_dir; info->version = new_version; return 0; } /* Set the default engine info for the protocol PROTO to the file name FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t gpgme_set_engine_info (gpgme_protocol_t proto, const char *file_name, const char *home_dir) { gpgme_error_t err; gpgme_engine_info_t info; LOCK (engine_info_lock); info = engine_info; if (!info) { /* Make sure it is initialized. */ UNLOCK (engine_info_lock); err = gpgme_get_engine_info (&info); if (err) return err; LOCK (engine_info_lock); } err = _gpgme_set_engine_info (info, proto, file_name, home_dir); UNLOCK (engine_info_lock); return err; } gpgme_error_t _gpgme_engine_new (gpgme_engine_info_t info, engine_t *r_engine) { engine_t engine; if (!info->file_name || !info->version) return trace_gpg_error (GPG_ERR_INV_ENGINE); engine = calloc (1, sizeof *engine); if (!engine) return gpg_error_from_syserror (); engine->ops = engine_ops[info->protocol]; if (engine->ops->new) { gpgme_error_t err; err = (*engine->ops->new) (&engine->engine, info->file_name, info->home_dir, info->version); if (err) { free (engine); return err; } } else engine->engine = NULL; *r_engine = engine; return 0; } gpgme_error_t _gpgme_engine_reset (engine_t engine) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->reset) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->reset) (engine->engine); } void _gpgme_engine_release (engine_t engine) { if (!engine) return; if (engine->ops->release) (*engine->ops->release) (engine->engine); free (engine); } /* Set a status callback which is used to monitor the status values * before they are passed to a handler set with * _gpgme_engine_set_status_handler. */ void _gpgme_engine_set_status_cb (engine_t engine, gpgme_status_cb_t cb, void *cb_value) { if (!engine) return; if (engine->ops->set_status_cb) (*engine->ops->set_status_cb) (engine->engine, cb, cb_value); } void _gpgme_engine_set_status_handler (engine_t engine, engine_status_handler_t fnc, void *fnc_value) { if (!engine) return; if (engine->ops->set_status_handler) (*engine->ops->set_status_handler) (engine->engine, fnc, fnc_value); } gpgme_error_t _gpgme_engine_set_command_handler (engine_t engine, engine_command_handler_t fnc, void *fnc_value) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->set_command_handler) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->set_command_handler) (engine->engine, fnc, fnc_value); } gpgme_error_t _gpgme_engine_set_colon_line_handler (engine_t engine, engine_colon_line_handler_t fnc, void *fnc_value) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->set_colon_line_handler) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->set_colon_line_handler) (engine->engine, fnc, fnc_value); } gpgme_error_t _gpgme_engine_set_locale (engine_t engine, int category, const char *value) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->set_locale) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->set_locale) (engine->engine, category, value); } gpgme_error_t _gpgme_engine_set_protocol (engine_t engine, gpgme_protocol_t protocol) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->set_protocol) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->set_protocol) (engine->engine, protocol); } /* Pass information about the current context to the engine. The * engine may use this context to retrieve context specific flags. * Important: The engine is required to immediately copy the required * flags to its own context! * * This function will eventually be used to reduce the number of * explicit passed flags. */ void _gpgme_engine_set_engine_flags (engine_t engine, gpgme_ctx_t ctx) { if (!engine) return; if (!engine->ops->set_engine_flags) return; (*engine->ops->set_engine_flags) (engine->engine, ctx); } gpgme_error_t _gpgme_engine_op_decrypt (engine_t engine, gpgme_decrypt_flags_t flags, gpgme_data_t ciph, gpgme_data_t plain, int export_session_key, const char *override_session_key, int auto_key_retrieve) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->decrypt) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->decrypt) (engine->engine, flags, ciph, plain, export_session_key, override_session_key, auto_key_retrieve); } gpgme_error_t _gpgme_engine_op_delete (engine_t engine, gpgme_key_t key, unsigned int flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->delete) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->delete) (engine->engine, key, flags); } gpgme_error_t _gpgme_engine_op_edit (engine_t engine, int type, gpgme_key_t key, gpgme_data_t out, gpgme_ctx_t ctx /* FIXME */) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->edit) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->edit) (engine->engine, type, key, out, ctx); } gpgme_error_t _gpgme_engine_op_encrypt (engine_t engine, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->encrypt) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->encrypt) (engine->engine, recp, recpstring, flags, plain, ciph, use_armor); } gpgme_error_t _gpgme_engine_op_encrypt_sign (engine_t engine, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t ciph, int use_armor, gpgme_ctx_t ctx /* FIXME */) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->encrypt_sign) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->encrypt_sign) (engine->engine, recp, recpstring, flags, plain, ciph, use_armor, ctx); } gpgme_error_t _gpgme_engine_op_export (engine_t engine, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata, int use_armor) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->export) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->export) (engine->engine, pattern, mode, keydata, use_armor); } gpgme_error_t _gpgme_engine_op_export_ext (engine_t engine, const char *pattern[], unsigned int reserved, gpgme_data_t keydata, int use_armor) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->export_ext) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->export_ext) (engine->engine, pattern, reserved, keydata, use_armor); } gpgme_error_t _gpgme_engine_op_genkey (engine_t engine, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t key, unsigned int flags, gpgme_data_t help_data, unsigned int extraflags, gpgme_data_t pubkey, gpgme_data_t seckey) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->genkey) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->genkey) (engine->engine, userid, algo, reserved, expires, key, flags, help_data, extraflags, pubkey, seckey); } gpgme_error_t _gpgme_engine_op_keysign (engine_t engine, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags, gpgme_ctx_t ctx) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->keysign) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->keysign) (engine->engine, key, userid, expires, flags, ctx); } gpgme_error_t _gpgme_engine_op_tofu_policy (engine_t engine, gpgme_key_t key, gpgme_tofu_policy_t policy) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->tofu_policy) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->tofu_policy) (engine->engine, key, policy); } gpgme_error_t _gpgme_engine_op_import (engine_t engine, gpgme_data_t keydata, gpgme_key_t *keyarray) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->import) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->import) (engine->engine, keydata, keyarray); } gpgme_error_t _gpgme_engine_op_keylist (engine_t engine, const char *pattern, int secret_only, gpgme_keylist_mode_t mode, int engine_flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->keylist) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->keylist) (engine->engine, pattern, secret_only, mode, engine_flags); } gpgme_error_t _gpgme_engine_op_keylist_ext (engine_t engine, const char *pattern[], int secret_only, int reserved, gpgme_keylist_mode_t mode, int engine_flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->keylist_ext) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->keylist_ext) (engine->engine, pattern, secret_only, reserved, mode, engine_flags); } gpgme_error_t _gpgme_engine_op_keylist_data (engine_t engine, gpgme_data_t data) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->keylist_data) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->keylist_data) (engine->engine, data); } gpgme_error_t _gpgme_engine_op_sign (engine_t engine, gpgme_data_t in, gpgme_data_t out, gpgme_sig_mode_t mode, int use_armor, int use_textmode, int include_certs, gpgme_ctx_t ctx /* FIXME */) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->sign) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->sign) (engine->engine, in, out, mode, use_armor, use_textmode, include_certs, ctx); } gpgme_error_t _gpgme_engine_op_trustlist (engine_t engine, const char *pattern) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->trustlist) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->trustlist) (engine->engine, pattern); } gpgme_error_t _gpgme_engine_op_verify (engine_t engine, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext, gpgme_ctx_t ctx) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->verify) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->verify) (engine->engine, sig, signed_text, plaintext, ctx); } gpgme_error_t _gpgme_engine_op_getauditlog (engine_t engine, gpgme_data_t output, unsigned int flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->getauditlog) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->getauditlog) (engine->engine, output, flags); } gpgme_error_t _gpgme_engine_op_assuan_transact (engine_t engine, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->opassuan_transact) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->opassuan_transact) (engine->engine, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); } gpgme_error_t _gpgme_engine_op_conf_load (engine_t engine, gpgme_conf_comp_t *conf_p) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->conf_load) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->conf_load) (engine->engine, conf_p); } gpgme_error_t _gpgme_engine_op_conf_save (engine_t engine, gpgme_conf_comp_t conf) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->conf_save) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->conf_save) (engine->engine, conf); } gpgme_error_t _gpgme_engine_op_conf_dir (engine_t engine, const char *what, char **result) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->conf_dir) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->conf_dir) (engine->engine, what, result); } gpgme_error_t _gpgme_engine_op_query_swdb (engine_t engine, const char *name, const char *iversion, gpgme_query_swdb_result_t result) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->query_swdb) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->query_swdb) (engine->engine, name, iversion, result); } void _gpgme_engine_set_io_cbs (engine_t engine, gpgme_io_cbs_t io_cbs) { if (!engine) return; (*engine->ops->set_io_cbs) (engine->engine, io_cbs); } void _gpgme_engine_io_event (engine_t engine, gpgme_event_io_t type, void *type_data) { if (!engine) return; (*engine->ops->io_event) (engine->engine, type, type_data); } /* Cancel the session and the pending operation if any. */ gpgme_error_t _gpgme_engine_cancel (engine_t engine) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->cancel) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->cancel) (engine->engine); } /* Cancel the pending operation, but not the complete session. */ gpgme_error_t _gpgme_engine_cancel_op (engine_t engine) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->cancel_op) return 0; return (*engine->ops->cancel_op) (engine->engine); } /* Change the passphrase for KEY. */ gpgme_error_t _gpgme_engine_op_passwd (engine_t engine, gpgme_key_t key, unsigned int flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->passwd) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->passwd) (engine->engine, key, flags); } /* Set the pinentry mode for ENGINE to MODE. */ gpgme_error_t _gpgme_engine_set_pinentry_mode (engine_t engine, gpgme_pinentry_mode_t mode) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->set_pinentry_mode) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->set_pinentry_mode) (engine->engine, mode); } gpgme_error_t _gpgme_engine_op_spawn (engine_t engine, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags) { if (!engine) return gpg_error (GPG_ERR_INV_VALUE); if (!engine->ops->opspawn) return gpg_error (GPG_ERR_NOT_IMPLEMENTED); return (*engine->ops->opspawn) (engine->engine, file, argv, datain, dataout, dataerr, flags); } diff --git a/src/error.c b/src/error.c index a84b8673..8e0855f5 100644 --- a/src/error.c +++ b/src/error.c @@ -1,111 +1,111 @@ /* error.c - Error handling for GPGME. - Copyright (C) 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include /* Return a pointer to a string containing a description of the error code in the error value ERR. */ const char * gpgme_strerror (gpgme_error_t err) { return gpg_strerror (err); } /* Return the error string for ERR in the user-supplied buffer BUF of size BUFLEN. This function is, in contrast to gpg_strerror, thread-safe if a thread-safe strerror_r() function is provided by the system. If the function succeeds, 0 is returned and BUF contains the string describing the error. If the buffer was not large enough, ERANGE is returned and BUF contains as much of the beginning of the error string as fits into the buffer. */ int gpgme_strerror_r (gpg_error_t err, char *buf, size_t buflen) { return gpg_strerror_r (err, buf, buflen); } /* Return a pointer to a string containing a description of the error source in the error value ERR. */ const char * gpgme_strsource (gpgme_error_t err) { return gpg_strsource (err); } /* Retrieve the error code for the system error ERR. This returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report this). */ gpgme_err_code_t gpgme_err_code_from_errno (int err) { return gpg_err_code_from_errno (err); } /* Retrieve the system error for the error code CODE. This returns 0 if CODE is not a system error code. */ int gpgme_err_code_to_errno (gpgme_err_code_t code) { return gpg_err_code_to_errno (code); } /* Retrieve the error code directly from the ERRNO variable. This returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report this) and GPG_ERR_MISSING_ERRNO if ERRNO has the value 0. */ gpgme_err_code_t gpgme_err_code_from_syserror (void) { return gpg_err_code_from_syserror (); } /* Set the ERRNO variable. This function is the preferred way to set ERRNO due to peculiarities on WindowsCE. */ void gpgme_err_set_errno (int err) { gpg_err_set_errno (err); } /* Return an error value with the error source SOURCE and the system error ERR. */ gpgme_error_t gpgme_err_make_from_errno (gpg_err_source_t source, int err) { return gpg_err_make_from_errno (source, err); } /* Return an error value with the system error ERR. */ gpgme_error_t gpgme_error_from_errno (int err) { return gpgme_error (gpg_err_code_from_errno (err)); } diff --git a/src/export.c b/src/export.c index f460e853..0cc1099f 100644 --- a/src/export.c +++ b/src/export.c @@ -1,480 +1,481 @@ /* export.c - Export a key. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001-2004, 2010, 2014 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001-2004, 2010, 2014 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #include "gpgme.h" #include "util.h" #include "debug.h" #include "context.h" #include "ops.h" /* Local operation data. */ typedef struct { gpg_error_t err; /* Error encountered during the export. */ } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; (void)opd; /* Nothing to release here. */ } /* Parse an error status line. Return the error location and the error code. The function may modify ARGS. */ static char * parse_error (char *args, gpg_error_t *r_err) { char *where = strchr (args, ' '); char *which; if (where) { *where = '\0'; which = where + 1; where = strchr (which, ' '); if (where) *where = '\0'; where = args; } else { *r_err = trace_gpg_error (GPG_ERR_INV_ENGINE); return NULL; } *r_err = atoi (which); return where; } static gpgme_error_t export_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; const char *loc; err = _gpgme_op_data_lookup (ctx, OPDATA_EXPORT, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_ERROR: loc = parse_error (args, &err); if (!loc) return err; else if (opd->err) ; /* We only want to report the first error. */ else if (!strcmp (loc, "keyserver_send")) opd->err = err; break; default: break; } return 0; } static gpgme_error_t export_start (gpgme_ctx_t ctx, int synchronous, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata) { gpgme_error_t err; void *hook; op_data_t opd; if ((mode & ~(GPGME_EXPORT_MODE_EXTERN |GPGME_EXPORT_MODE_MINIMAL |GPGME_EXPORT_MODE_SECRET |GPGME_EXPORT_MODE_RAW |GPGME_EXPORT_MODE_NOUID |GPGME_EXPORT_MODE_PKCS12))) return gpg_error (GPG_ERR_INV_VALUE); /* Invalid flags in MODE. */ if ((mode & GPGME_EXPORT_MODE_SECRET)) { if ((mode & GPGME_EXPORT_MODE_EXTERN)) return gpg_error (GPG_ERR_INV_FLAG); /* Combination not allowed. */ if ((mode & GPGME_EXPORT_MODE_RAW) && (mode & GPGME_EXPORT_MODE_PKCS12)) return gpg_error (GPG_ERR_INV_FLAG); /* Combination not allowed. */ if (ctx->protocol != GPGME_PROTOCOL_CMS && (mode & (GPGME_EXPORT_MODE_RAW|GPGME_EXPORT_MODE_PKCS12))) return gpg_error (GPG_ERR_INV_FLAG); /* Only supported for X.509. */ } if ((mode & GPGME_EXPORT_MODE_EXTERN)) { if (keydata) return gpg_error (GPG_ERR_INV_VALUE); } else { if (!keydata) return gpg_error (GPG_ERR_INV_VALUE); } err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_EXPORT, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; _gpgme_engine_set_status_handler (ctx->engine, export_status_handler, ctx); return _gpgme_engine_op_export (ctx->engine, pattern, mode, keydata, ctx->use_armor); } /* Export the keys listed in PATTERN into KEYDATA. */ gpgme_error_t gpgme_op_export_start (gpgme_ctx_t ctx, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_export_start", ctx, "pattern=%s, mode=0x%x, keydata=%p", pattern, mode, keydata); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = export_start (ctx, 0, pattern, mode, keydata); return TRACE_ERR (err); } /* Export the keys listed in PATTERN into KEYDATA. */ gpgme_error_t gpgme_op_export (gpgme_ctx_t ctx, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_export", ctx, "pattern=%s, mode=0x%x, keydata=%p", pattern, mode, keydata); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = export_start (ctx, 1, pattern, mode, keydata); if (!err) err = _gpgme_wait_one (ctx); return err; } static gpgme_error_t export_ext_start (gpgme_ctx_t ctx, int synchronous, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata) { gpgme_error_t err; void *hook; op_data_t opd; if ((mode & ~(GPGME_EXPORT_MODE_EXTERN |GPGME_EXPORT_MODE_MINIMAL |GPGME_EXPORT_MODE_SECRET |GPGME_EXPORT_MODE_RAW |GPGME_EXPORT_MODE_PKCS12))) return gpg_error (GPG_ERR_INV_VALUE); /* Invalid flags in MODE. */ if ((mode & GPGME_EXPORT_MODE_SECRET)) { if ((mode & GPGME_EXPORT_MODE_EXTERN)) return gpg_error (GPG_ERR_INV_FLAG); /* Combination not allowed. */ if ((mode & GPGME_EXPORT_MODE_RAW) && (mode & GPGME_EXPORT_MODE_PKCS12)) return gpg_error (GPG_ERR_INV_FLAG); /* Combination not allowed. */ if (ctx->protocol != GPGME_PROTOCOL_CMS && (mode & (GPGME_EXPORT_MODE_RAW|GPGME_EXPORT_MODE_PKCS12))) return gpg_error (GPG_ERR_INV_FLAG); /* Only supported for X.509. */ } if ((mode & GPGME_EXPORT_MODE_EXTERN)) { if (keydata) return gpg_error (GPG_ERR_INV_VALUE); } else { if (!keydata) return gpg_error (GPG_ERR_INV_VALUE); } err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_EXPORT, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; _gpgme_engine_set_status_handler (ctx->engine, export_status_handler, ctx); return _gpgme_engine_op_export_ext (ctx->engine, pattern, mode, keydata, ctx->use_armor); } /* Export the keys listed in PATTERN into KEYDATA. */ gpgme_error_t gpgme_op_export_ext_start (gpgme_ctx_t ctx, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_ext_start", ctx, "mode=0x%x, keydata=%p", mode, keydata); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && pattern) { int i = 0; while (pattern[i]) { TRACE_LOG2 ("pattern[%i] = %s", i, pattern[i]); i++; } } err = export_ext_start (ctx, 0, pattern, mode, keydata); return TRACE_ERR (err); } /* Export the keys listed in PATTERN into KEYDATA. */ gpgme_error_t gpgme_op_export_ext (gpgme_ctx_t ctx, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_ext_start", ctx, "mode=0x%x, keydata=%p", mode, keydata); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && pattern) { int i = 0; while (pattern[i]) { TRACE_LOG2 ("pattern[%i] = %s", i, pattern[i]); i++; } } err = export_ext_start (ctx, 1, pattern, mode, keydata); if (!err) { err = _gpgme_wait_one (ctx); if (!err) { /* For this synchronous operation we check for operational errors and return them. For asynchronous operations there is currently no way to do this - we need to add a gpgme_op_export_result function to fix that. */ void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_EXPORT, &hook, -1, NULL); opd = hook; if (!err) err = opd->err; } } return TRACE_ERR (err); } static gpgme_error_t export_keys_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t keys[], gpgme_export_mode_t mode, gpgme_data_t keydata) { gpgme_error_t err; int nkeys, idx; char **pattern; if (!keys) return gpg_error (GPG_ERR_INV_VALUE); /* Create a list of pattern from the keys. */ for (idx=nkeys=0; keys[idx]; idx++) if (keys[idx]->protocol == ctx->protocol) nkeys++; if (!nkeys) return gpg_error (GPG_ERR_NO_DATA); pattern = calloc (nkeys+1, sizeof *pattern); if (!pattern) return gpg_error_from_syserror (); for (idx=nkeys=0; keys[idx]; idx++) if (keys[idx]->protocol == ctx->protocol && keys[idx]->subkeys && keys[idx]->subkeys->fpr && *keys[idx]->subkeys->fpr) { pattern[nkeys] = strdup (keys[idx]->subkeys->fpr); if (!pattern[nkeys]) { err = gpg_error_from_syserror (); goto leave; } nkeys++; } /* Pass on to the regular function. */ err = export_ext_start (ctx, synchronous, (const char**)pattern, mode, keydata); leave: for (idx=0; pattern[idx]; idx++) free (pattern[idx]); free (pattern); return err; } /* Export the keys from the array KEYS into KEYDATA. Only keys of the current protocol are exported and only those which have a fingerprint set; that is keys received with some external search methods are silently skipped. */ gpgme_error_t gpgme_op_export_keys_start (gpgme_ctx_t ctx, gpgme_key_t keys[], gpgme_export_mode_t mode, gpgme_data_t keydata) { gpg_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_keys_start", ctx, "mode=0x%x, keydata=%p", mode, keydata); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && keys) { int i = 0; while (keys[i]) { TRACE_LOG3 ("keys[%i] = %p (%s)", i, keys[i], (keys[i]->subkeys && keys[i]->subkeys->fpr) ? keys[i]->subkeys->fpr : "invalid"); i++; } } err = export_keys_start (ctx, 0, keys, mode, keydata); return TRACE_ERR (err); } gpgme_error_t gpgme_op_export_keys (gpgme_ctx_t ctx, gpgme_key_t keys[], gpgme_export_mode_t mode, gpgme_data_t keydata) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_export_keys", ctx, "mode=0x%x, keydata=%p", mode, keydata); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && keys) { int i = 0; while (keys[i]) { TRACE_LOG3 ("keys[%i] = %p (%s)", i, keys[i], (keys[i]->subkeys && keys[i]->subkeys->fpr) ? keys[i]->subkeys->fpr : "invalid"); i++; } } err = export_keys_start (ctx, 1, keys, mode, keydata); if (!err) { err = _gpgme_wait_one (ctx); if (!err) { /* For this synchronous operation we check for operational errors and return them. For asynchronous operations there is currently no way to do this - we need to add a gpgme_op_export_result function to fix that. */ void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_EXPORT, &hook, -1, NULL); opd = hook; if (!err) err = opd->err; } } return TRACE_ERR (err); } diff --git a/src/genkey.c b/src/genkey.c index e00d0475..f76b7d88 100644 --- a/src/genkey.c +++ b/src/genkey.c @@ -1,664 +1,665 @@ /* genkey.c - Key generation. * Copyright (C) 2000 Werner Koch (dd9jn) * Copyright (C) 2001, 2002, 2003, 2004, 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" #include "util.h" typedef struct { struct _gpgme_op_genkey_result result; /* The error code from a FAILURE status line or 0. */ gpg_error_t failure_code; /* The error code from certain ERROR status lines or 0. */ gpg_error_t error_code; /* Flag to indicate that a UID is to be added. */ gpg_error_t uidmode; /* The key parameters passed to the crypto engine. */ gpgme_data_t key_parameter; } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; if (opd->result.fpr) free (opd->result.fpr); if (opd->key_parameter) gpgme_data_release (opd->key_parameter); } gpgme_genkey_result_t gpgme_op_genkey_result (gpgme_ctx_t ctx) { void *hook; op_data_t opd; gpgme_error_t err; TRACE_BEG (DEBUG_CTX, "gpgme_op_genkey_result", ctx); err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, -1, NULL); opd = hook; if (err || !opd) { TRACE_SUC0 ("result=(null)"); return NULL; } TRACE_LOG3 ("fpr = %s, %s, %s", opd->result.fpr, opd->result.primary ? "primary" : "no primary", opd->result.sub ? "sub" : "no sub"); TRACE_SUC1 ("result=%p", &opd->result); return &opd->result; } /* Parse an error status line. Return the error location and the error code. The function may modify ARGS. */ static char * parse_error (char *args, gpg_error_t *r_err) { char *where = strchr (args, ' '); char *which; if (where) { *where = '\0'; which = where + 1; where = strchr (which, ' '); if (where) *where = '\0'; where = args; } else { *r_err = trace_gpg_error (GPG_ERR_INV_ENGINE); return NULL; } *r_err = atoi (which); return where; } static gpgme_error_t genkey_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; char *loc; /* Pipe the status code through the progress status handler. */ err = _gpgme_progress_status_handler (ctx, code, args); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_KEY_CREATED: if (args && *args) { if (*args == 'B' || *args == 'P') { opd->result.primary = 1; opd->result.uid = 1; } if (*args == 'B' || *args == 'S') opd->result.sub = 1; if (args[1] == ' ') { if (opd->result.fpr) free (opd->result.fpr); opd->result.fpr = strdup (&args[2]); if (!opd->result.fpr) return gpg_error_from_syserror (); } } break; case GPGME_STATUS_ERROR: loc = parse_error (args, &err); if (!loc) return err; if (!opd->error_code) opd->error_code = err; break; case GPGME_STATUS_FAILURE: opd->failure_code = _gpgme_parse_failure (args); break; case GPGME_STATUS_EOF: if (opd->error_code) return opd->error_code; else if (!opd->uidmode && !opd->result.primary && !opd->result.sub) return gpg_error (GPG_ERR_GENERAL); else if (opd->failure_code) return opd->failure_code; else if (opd->uidmode == 1) opd->result.uid = 1; /* We have no status line, thus this hack. */ break; case GPGME_STATUS_INQUIRE_MAXLEN: if (ctx->status_cb && !ctx->full_status) { err = ctx->status_cb (ctx->status_cb_value, "INQUIRE_MAXLEN", args); if (err) return err; } break; default: break; } return 0; } static gpgme_error_t get_key_parameter (const char *parms, gpgme_data_t *key_parameter) { const char *content; const char *attrib; const char *endtag; /* Extract the key parameter from the XML structure. */ parms = strstr (parms, "'); if (!content) return gpg_error (GPG_ERR_INV_VALUE); content++; attrib = strstr (parms, "format=\"internal\""); if (!attrib || attrib >= content) return gpg_error (GPG_ERR_INV_VALUE); endtag = strstr (content, ""); if (!endtag) endtag = content + strlen (content); /* FIXME: Check that there are no control statements inside. */ while (content < endtag && (content[0] == '\n' || (content[0] == '\r' && content[1] == '\n'))) content++; return gpgme_data_new_from_mem (key_parameter, content, endtag - content, 1); } static gpgme_error_t genkey_start (gpgme_ctx_t ctx, int synchronous, const char *parms, gpgme_data_t pubkey, gpgme_data_t seckey) { gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; err = get_key_parameter (parms, &opd->key_parameter); if (err) return err; _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx); if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } return _gpgme_engine_op_genkey (ctx->engine, NULL, NULL, 0, 0, NULL, 0, opd->key_parameter, ctx->use_armor? GENKEY_EXTRAFLAG_ARMOR:0, pubkey, seckey); } /* Generate a new keypair and add it to the keyring. PUBKEY and SECKEY should be null for now. PARMS specifies what keys should be generated. */ gpgme_error_t gpgme_op_genkey_start (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey, gpgme_data_t seckey) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_genkey_start", ctx, "pubkey=%p, seckey=%p", pubkey, seckey); TRACE_LOGBUF (parms, parms? strlen (parms):0); if (!ctx || parms) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = genkey_start (ctx, 0, parms, pubkey, seckey); return TRACE_ERR (err); } /* Generate a new keypair and add it to the keyring. PUBKEY and SECKEY should be null for now. PARMS specifies what keys should be generated. */ gpgme_error_t gpgme_op_genkey (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey, gpgme_data_t seckey) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_genkey", ctx, "pubkey=%p, seckey=%p", pubkey, seckey); TRACE_LOGBUF (parms, parms? strlen (parms):0); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = genkey_start (ctx, 1, parms, pubkey, seckey); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } static gpgme_error_t createkey_start (gpgme_ctx_t ctx, int synchronous, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t anchorkey, unsigned int flags) { gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; if (reserved || anchorkey || !userid) return gpg_error (GPG_ERR_INV_ARG); err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx); if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } return _gpgme_engine_op_genkey (ctx->engine, userid, algo, reserved, expires, anchorkey, flags, NULL, ctx->use_armor? GENKEY_EXTRAFLAG_ARMOR:0, NULL, NULL); } gpgme_error_t gpgme_op_createkey_start (gpgme_ctx_t ctx, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t anchorkey, unsigned int flags) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createkey_start", ctx, "userid='%s', algo='%s' flags=0x%x", userid, algo, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = createkey_start (ctx, 0, userid, algo, reserved, expires, anchorkey, flags); return TRACE_ERR (err); } gpgme_error_t gpgme_op_createkey (gpgme_ctx_t ctx, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t anchorkey, unsigned int flags) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createkey", ctx, "userid='%s', algo='%s' flags=0x%x", userid, algo, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = createkey_start (ctx, 1, userid, algo, reserved, expires, anchorkey, flags); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } static gpgme_error_t createsubkey_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t key, const char *algo, unsigned long reserved, unsigned long expires, unsigned int flags) { gpgme_error_t err; void *hook; op_data_t opd; if (ctx->protocol != GPGME_PROTOCOL_OPENPGP) return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); err = _gpgme_op_reset (ctx, synchronous); if (err) return err; if (reserved || !key) return gpg_error (GPG_ERR_INV_ARG); err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx); if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } return _gpgme_engine_op_genkey (ctx->engine, NULL, algo, reserved, expires, key, flags, NULL, ctx->use_armor? GENKEY_EXTRAFLAG_ARMOR:0, NULL, NULL); } /* Add a subkey to an existing KEY. */ gpgme_error_t gpgme_op_createsubkey_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo, unsigned long reserved, unsigned long expires, unsigned int flags) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createsubkey_start", ctx, "key=%p, algo='%s' flags=0x%x", key, algo, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = createsubkey_start (ctx, 0, key, algo, reserved, expires, flags); return TRACE_ERR (err); } gpgme_error_t gpgme_op_createsubkey (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo, unsigned long reserved, unsigned long expires, unsigned int flags) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_createsubkey", ctx, "key=%p, algo='%s' flags=0x%x", key, algo, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = createsubkey_start (ctx, 1, key, algo, reserved, expires, flags); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } static gpgme_error_t addrevuid_start (gpgme_ctx_t ctx, int synchronous, int extraflags, gpgme_key_t key, const char *userid, unsigned int flags) { gpgme_error_t err; void *hook; op_data_t opd; if (ctx->protocol != GPGME_PROTOCOL_OPENPGP) return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); if (!key || !userid) return gpg_error (GPG_ERR_INV_ARG); err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_GENKEY, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; opd->uidmode = extraflags? 2 : 1; _gpgme_engine_set_status_handler (ctx->engine, genkey_status_handler, ctx); if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } return _gpgme_engine_op_genkey (ctx->engine, userid, NULL, 0, 0, key, flags, NULL, extraflags, NULL, NULL); } /* Add USERID to an existing KEY. */ gpgme_error_t gpgme_op_adduid_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int flags) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_adduid_start", ctx, "uid='%s' flags=0x%x", userid, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = addrevuid_start (ctx, 0, 0, key, userid, flags); return TRACE_ERR (err); } gpgme_error_t gpgme_op_adduid (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int flags) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_adduid", ctx, "uid='%s' flags=0x%x", userid, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = addrevuid_start (ctx, 1, 0, key, userid, flags); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } /* Revoke USERID from KEY. */ gpgme_error_t gpgme_op_revuid_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int flags) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_revuid_start", ctx, "uid='%s' flags=0x%x", userid, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = addrevuid_start (ctx, 0, GENKEY_EXTRAFLAG_REVOKE, key, userid, flags); return TRACE_ERR (err); } gpgme_error_t gpgme_op_revuid (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int flags) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_revuid", ctx, "uid='%s' flags=0x%x", userid, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = addrevuid_start (ctx, 1, GENKEY_EXTRAFLAG_REVOKE, key, userid, flags); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } /* Set a flag on the USERID of KEY. The only supported flag right now * is "primary" to mark the primary key. */ static gpg_error_t set_uid_flag (gpgme_ctx_t ctx, int synchronous, gpgme_key_t key, const char *userid, const char *name, const char *value) { gpgme_error_t err; TRACE_BEG4 (DEBUG_CTX, "gpgme_op_set_uid_flag", ctx, "%d uid='%s' '%s'='%s'", synchronous, userid, name, value); if (!ctx || !name || !key || !userid) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); if (!strcmp (name, "primary")) { if (value) err = gpg_error (GPG_ERR_INV_ARG); else err = addrevuid_start (ctx, synchronous, GENKEY_EXTRAFLAG_SETPRIMARY, key, userid, 0); } else return err = gpg_error (GPG_ERR_UNKNOWN_NAME); if (synchronous && !err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } /* See set_uid_flag. */ gpgme_error_t gpgme_op_set_uid_flag_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, const char *name, const char *value) { return set_uid_flag (ctx, 0, key, userid, name, value); } /* See set_uid_flag. This is the synchronous variant. */ gpgme_error_t gpgme_op_set_uid_flag (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, const char *name, const char *value) { return set_uid_flag (ctx, 1, key, userid, name, value); } diff --git a/src/get-env.c b/src/get-env.c index b13706f9..a4c336bc 100644 --- a/src/get-env.c +++ b/src/get-env.c @@ -1,113 +1,113 @@ /* get_env.c - A getenv() replacement. - Copyright (C) 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include "util.h" /* Retrieve the environment variable NAME and return a copy of it in a malloc()'ed buffer in *VALUE. If the environment variable is not set, return NULL in *VALUE. */ #ifdef HAVE_GETENV_R #define INITIAL_GETENV_SIZE 32 gpgme_error_t _gpgme_getenv (const char *name, char **value) { size_t len = INITIAL_GETENV_SIZE; char *env_value; env_value = malloc (len); while (1) { *value = env_value; if (!env_value) return gpg_error_from_syserror (); if (getenv_r (name, env_value, len) == 0) break; if (errno == ERANGE) { len *= 2; env_value = realloc (env_value, len); } else { int saved = errno; free (env_value); *value = NULL; if (errno == ENOENT) return 0; else return gpg_error_from_errno (saved); } } return 0; } #else #ifndef HAVE_THREAD_SAFE_GETENV GPGRT_LOCK_DEFINE (environ_lock); #endif gpgme_error_t _gpgme_getenv (const char *name, char **value) { char *env_value; gpgme_error_t err = 0; #ifndef HAVE_THREAD_SAFE_GETENV gpg_err_code_t rc; rc= gpgrt_lock_lock (&environ_lock); if (rc) { err = gpg_error (rc); goto leave; } #endif env_value = getenv (name); if (!env_value) *value = NULL; else { *value = strdup (env_value); if (!*value) err = gpg_error_from_syserror (); } #ifndef HAVE_THREAD_SAFE_GETENV rc = gpgrt_lock_unlock (&environ_lock); if (rc) err = gpg_error (rc); leave: #endif return err; } #endif diff --git a/src/getauditlog.c b/src/getauditlog.c index d70e66fd..2d3f7f99 100644 --- a/src/getauditlog.c +++ b/src/getauditlog.c @@ -1,105 +1,105 @@ - /* getauditlog.c - Retrieve the audit log. - Copyright (C) 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" static gpgme_error_t getauditlog_status_handler (void *priv, gpgme_status_code_t code, char *args) { (void)priv; (void)code; (void)args; return 0; } static gpgme_error_t getauditlog_start (gpgme_ctx_t ctx, int synchronous, gpgme_data_t output, unsigned int flags) { gpgme_error_t err; if (!output) return gpg_error (GPG_ERR_INV_VALUE); if (!(flags & GPGME_AUDITLOG_DIAG)) { err = _gpgme_op_reset (ctx, ((synchronous&255) | 256) ); if (err) return err; } _gpgme_engine_set_status_handler (ctx->engine, getauditlog_status_handler, ctx); return _gpgme_engine_op_getauditlog (ctx->engine, output, flags); } /* Return the auditlog for the current session. This may be called after a successful or failed operation. If no audit log is available GPG_ERR_NO_DATA is returned. This is the asynchronous variant. */ gpgme_error_t gpgme_op_getauditlog_start (gpgme_ctx_t ctx, gpgme_data_t output, unsigned int flags) { gpg_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_getauditlog_start", ctx, "output=%p, flags=0x%x", output, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = getauditlog_start (ctx, 0, output, flags); return TRACE_ERR (err); } /* Return the auditlog for the current session. This may be called after a successful or failed operation. If no audit log is available GPG_ERR_NO_DATA is returned. This is the synchronous variant. */ gpgme_error_t gpgme_op_getauditlog (gpgme_ctx_t ctx, gpgme_data_t output, unsigned int flags) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_getauditlog", ctx, "output=%p, flags=0x%x", output, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = getauditlog_start (ctx, 1, output, flags); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } diff --git a/src/gpgconf.c b/src/gpgconf.c index ce6ace41..4f7e3280 100644 --- a/src/gpgconf.c +++ b/src/gpgconf.c @@ -1,131 +1,131 @@ /* gpgconf.c - GnuPG Made Easy. - Copyright (C) 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include "gpgme.h" #include "ops.h" #include "engine.h" #include "debug.h" #include "engine-backend.h" /* Allocate a new gpgme_conf_arg_t. */ gpgme_error_t gpgme_conf_arg_new (gpgme_conf_arg_t *arg_p, gpgme_conf_type_t type, const void *value) { return _gpgme_conf_arg_new (arg_p, type, value); } /* This also releases all chained argument structures! */ void gpgme_conf_arg_release (gpgme_conf_arg_t arg, gpgme_conf_type_t type) { _gpgme_conf_arg_release (arg, type); } /* Register a change for the value of OPT to ARG. */ gpgme_error_t gpgme_conf_opt_change (gpgme_conf_opt_t opt, int reset, gpgme_conf_arg_t arg) { return _gpgme_conf_opt_change (opt, reset, arg); } /* Public function to release a gpgme_conf_comp list. */ void gpgme_conf_release (gpgme_conf_comp_t conf) { _gpgme_conf_release (conf); } /* Public function to load a configuration list. No asynchronous interface for now. */ gpgme_error_t gpgme_op_conf_load (gpgme_ctx_t ctx, gpgme_conf_comp_t *conf_p) { gpgme_error_t err; gpgme_protocol_t proto; if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); proto = ctx->protocol; ctx->protocol = GPGME_PROTOCOL_GPGCONF; err = _gpgme_op_reset (ctx, 1); if (err) return err; err = _gpgme_engine_op_conf_load (ctx->engine, conf_p); ctx->protocol = proto; return err; } /* This function does not follow chained components! */ gpgme_error_t gpgme_op_conf_save (gpgme_ctx_t ctx, gpgme_conf_comp_t comp) { gpgme_error_t err; gpgme_protocol_t proto; if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); proto = ctx->protocol; ctx->protocol = GPGME_PROTOCOL_GPGCONF; err = _gpgme_op_reset (ctx, 1); if (err) return err; err = _gpgme_engine_op_conf_save (ctx->engine, comp); ctx->protocol = proto; return err; } gpgme_error_t gpgme_op_conf_dir (gpgme_ctx_t ctx, const char *what, char **result) { gpgme_error_t err; gpgme_protocol_t proto; if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); proto = ctx->protocol; ctx->protocol = GPGME_PROTOCOL_GPGCONF; err = _gpgme_op_reset (ctx, 1); if (err) return err; err = _gpgme_engine_op_conf_dir (ctx->engine, what, result); ctx->protocol = proto; return err; } diff --git a/src/gpgme-json.c b/src/gpgme-json.c index ba80f081..a7e3d5f2 100644 --- a/src/gpgme-json.c +++ b/src/gpgme-json.c @@ -1,3942 +1,3942 @@ /* gpgme-json.c - JSON based interface to gpgme (server) * Copyright (C) 2018 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . - * SPDX-License-Identifier: LGPL-2.1+ + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ -/* This is tool implements the Native Messaging protocol of web +/* This tool implements the Native Messaging protocol of web * browsers and provides the server part of it. A Javascript based * client can be found in lang/javascript. */ #include #include #include #include #include #ifdef HAVE_LOCALE_H #include #endif #include #include #define GPGRT_ENABLE_ES_MACROS 1 #define GPGRT_ENABLE_LOG_MACROS 1 #define GPGRT_ENABLE_ARGPARSE_MACROS 1 #include "gpgme.h" #include "cJSON.h" #if GPGRT_VERSION_NUMBER < 0x011c00 /* 1.28 */ int main (void){fputs ("Build with Libgpg-error >= 1.28!\n", stderr);return 1;} #else /* libgpg-error >= 1.28 */ /* We don't allow a request with more than 64 MiB. */ #define MAX_REQUEST_SIZE (64 * 1024 * 1024) /* Minimal chunk size for returned data.*/ #define MIN_REPLY_CHUNK_SIZE 30 /* If no chunksize is provided we print everything. Changing * this to a positive value will result in all messages being * chunked. */ #define DEF_REPLY_CHUNK_SIZE 0 #define MAX_REPLY_CHUNK_SIZE (10 * 1024 * 1024) static void xoutofcore (const char *type) GPGRT_ATTR_NORETURN; static cjson_t error_object_v (cjson_t json, const char *message, va_list arg_ptr, gpg_error_t err) GPGRT_ATTR_PRINTF(2,0); static cjson_t error_object (cjson_t json, const char *message, ...) GPGRT_ATTR_PRINTF(2,3); static char *error_object_string (const char *message, ...) GPGRT_ATTR_PRINTF(1,2); static char *process_request (const char *request); /* True if interactive mode is active. */ static int opt_interactive; /* True is debug mode is active. */ static int opt_debug; /* Pending data to be returned by a getmore command. */ static struct { char *buffer; /* Malloced data or NULL if not used. */ size_t length; /* Length of that data. */ size_t written; /* # of already written bytes from BUFFER. */ } pending_data; /* * Helper functions and macros */ #define xtrystrdup(a) gpgrt_strdup ((a)) #define xcalloc(a,b) ({ \ void *_r = gpgrt_calloc ((a), (b)); \ if (!_r) \ xoutofcore ("calloc"); \ _r; }) #define xstrdup(a) ({ \ char *_r = gpgrt_strdup ((a)); \ if (!_r) \ xoutofcore ("strdup"); \ _r; }) #define xstrconcat(a, ...) ({ \ char *_r = gpgrt_strconcat ((a), __VA_ARGS__); \ if (!_r) \ xoutofcore ("strconcat"); \ _r; }) #define xfree(a) gpgrt_free ((a)) /* Only use calloc. */ #define CALLOC_ONLY 1 #if CALLOC_ONLY #define xtrymalloc(a) gpgrt_calloc (1, (a)) #define xmalloc(a) xcalloc(1, (a)) #else #define xtrymalloc(a) gpgrt_malloc ((a)) #define xmalloc(a) ({ \ void *_r = gpgrt_malloc ((a)); \ if (!_r) \ xoutofcore ("malloc"); \ _r; }) #endif #define spacep(p) (*(p) == ' ' || *(p) == '\t') #ifndef HAVE_STPCPY static GPGRT_INLINE char * _my_stpcpy (char *a, const char *b) { while (*b) *a++ = *b++; *a = 0; return a; } #define stpcpy(a,b) _my_stpcpy ((a), (b)) #endif /*!HAVE_STPCPY*/ /* Free a NULL terminated array */ static void xfree_array (char **array) { if (array) { int idx; for (idx = 0; array[idx]; idx++) xfree (array[idx]); xfree (array); } } static void xoutofcore (const char *type) { gpg_error_t err = gpg_error_from_syserror (); log_error ("%s failed: %s\n", type, gpg_strerror (err)); exit (2); } /* Call cJSON_CreateObject but terminate in case of an error. */ static cjson_t xjson_CreateObject (void) { cjson_t json = cJSON_CreateObject (); if (!json) xoutofcore ("cJSON_CreateObject"); return json; } /* Call cJSON_CreateArray but terminate in case of an error. */ static cjson_t xjson_CreateArray (void) { cjson_t json = cJSON_CreateArray (); if (!json) xoutofcore ("cJSON_CreateArray"); return json; } /* Wrapper around cJSON_AddStringToObject which returns an gpg-error * code instead of the NULL or the new object. */ static gpg_error_t cjson_AddStringToObject (cjson_t object, const char *name, const char *string) { if (!cJSON_AddStringToObject (object, name, string)) return gpg_error_from_syserror (); return 0; } /* Same as cjson_AddStringToObject but prints an error message and * terminates the process. */ static void xjson_AddStringToObject (cjson_t object, const char *name, const char *string) { if (!cJSON_AddStringToObject (object, name, string)) xoutofcore ("cJSON_AddStringToObject"); } /* Same as xjson_AddStringToObject but ignores NULL strings */ static void xjson_AddStringToObject0 (cjson_t object, const char *name, const char *string) { if (!string) return; xjson_AddStringToObject (object, name, string); } /* Wrapper around cJSON_AddBoolToObject which terminates the process * in case of an error. */ static void xjson_AddBoolToObject (cjson_t object, const char *name, int abool) { if (!cJSON_AddBoolToObject (object, name, abool)) xoutofcore ("cJSON_AddStringToObject"); return ; } /* Wrapper around cJSON_AddNumberToObject which terminates the process * in case of an error. */ static void xjson_AddNumberToObject (cjson_t object, const char *name, double dbl) { if (!cJSON_AddNumberToObject (object, name, dbl)) xoutofcore ("cJSON_AddNumberToObject"); return ; } /* Wrapper around cJSON_AddItemToObject which terminates the process * in case of an error. */ static void xjson_AddItemToObject (cjson_t object, const char *name, cjson_t item) { if (!cJSON_AddItemToObject (object, name, item)) xoutofcore ("cJSON_AddItemToObject"); return ; } /* This is similar to cJSON_AddStringToObject but takes (DATA, * DATALEN) and adds it under NAME as a base 64 encoded string to * OBJECT. */ static gpg_error_t add_base64_to_object (cjson_t object, const char *name, const void *data, size_t datalen) { #if GPGRT_VERSION_NUMBER < 0x011d00 /* 1.29 */ return gpg_error (GPG_ERR_NOT_SUPPORTED); #else gpg_err_code_t err; estream_t fp = NULL; gpgrt_b64state_t state = NULL; cjson_t j_str = NULL; void *buffer = NULL; fp = es_fopenmem (0, "rwb"); if (!fp) { err = gpg_err_code_from_syserror (); goto leave; } state = gpgrt_b64enc_start (fp, ""); if (!state) { err = gpg_err_code_from_syserror (); goto leave; } err = gpgrt_b64enc_write (state, data, datalen); if (err) goto leave; err = gpgrt_b64enc_finish (state); state = NULL; if (err) return err; es_fputc (0, fp); if (es_fclose_snatch (fp, &buffer, NULL)) { fp = NULL; err = gpg_error_from_syserror (); goto leave; } fp = NULL; j_str = cJSON_CreateStringConvey (buffer); if (!j_str) { err = gpg_error_from_syserror (); goto leave; } buffer = NULL; if (!cJSON_AddItemToObject (object, name, j_str)) { err = gpg_error_from_syserror (); cJSON_Delete (j_str); j_str = NULL; goto leave; } j_str = NULL; leave: xfree (buffer); cJSON_Delete (j_str); gpgrt_b64enc_finish (state); es_fclose (fp); return err; #endif } /* Create a JSON error object. If JSON is not NULL the error message * is appended to that object. An existing "type" item will be replaced. */ static cjson_t error_object_v (cjson_t json, const char *message, va_list arg_ptr, gpg_error_t err) { cjson_t response, j_tmp; char *msg; msg = gpgrt_vbsprintf (message, arg_ptr); if (!msg) xoutofcore ("error_object"); response = json? json : xjson_CreateObject (); if (!(j_tmp = cJSON_GetObjectItem (response, "type"))) xjson_AddStringToObject (response, "type", "error"); else /* Replace existing "type". */ { j_tmp = cJSON_CreateString ("error"); if (!j_tmp) xoutofcore ("cJSON_CreateString"); cJSON_ReplaceItemInObject (response, "type", j_tmp); } xjson_AddStringToObject (response, "msg", msg); xfree (msg); xjson_AddNumberToObject (response, "code", err); return response; } /* Call cJSON_Print but terminate in case of an error. */ static char * xjson_Print (cjson_t object) { char *buf; buf = cJSON_Print (object); if (!buf) xoutofcore ("cJSON_Print"); return buf; } static cjson_t error_object (cjson_t json, const char *message, ...) { cjson_t response; va_list arg_ptr; va_start (arg_ptr, message); response = error_object_v (json, message, arg_ptr, 0); va_end (arg_ptr); return response; } static cjson_t gpg_error_object (cjson_t json, gpg_error_t err, const char *message, ...) { cjson_t response; va_list arg_ptr; va_start (arg_ptr, message); response = error_object_v (json, message, arg_ptr, err); va_end (arg_ptr); return response; } static char * error_object_string (const char *message, ...) { cjson_t response; va_list arg_ptr; char *msg; va_start (arg_ptr, message); response = error_object_v (NULL, message, arg_ptr, 0); va_end (arg_ptr); msg = xjson_Print (response); cJSON_Delete (response); return msg; } /* Get the boolean property NAME from the JSON object and store true * or valse at R_VALUE. If the name is unknown the value of DEF_VALUE * is returned. If the type of the value is not boolean, * GPG_ERR_INV_VALUE is returned and R_VALUE set to DEF_VALUE. */ static gpg_error_t get_boolean_flag (cjson_t json, const char *name, int def_value, int *r_value) { cjson_t j_item; j_item = cJSON_GetObjectItem (json, name); if (!j_item) *r_value = def_value; else if (cjson_is_true (j_item)) *r_value = 1; else if (cjson_is_false (j_item)) *r_value = 0; else { *r_value = def_value; return gpg_error (GPG_ERR_INV_VALUE); } return 0; } /* Get the boolean property PROTOCOL from the JSON object and store * its value at R_PROTOCOL. The default is OpenPGP. */ static gpg_error_t get_protocol (cjson_t json, gpgme_protocol_t *r_protocol) { cjson_t j_item; *r_protocol = GPGME_PROTOCOL_OpenPGP; j_item = cJSON_GetObjectItem (json, "protocol"); if (!j_item) ; else if (!cjson_is_string (j_item)) return gpg_error (GPG_ERR_INV_VALUE); else if (!strcmp(j_item->valuestring, "openpgp")) ; else if (!strcmp(j_item->valuestring, "cms")) *r_protocol = GPGME_PROTOCOL_CMS; else return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL); return 0; } /* Get the chunksize from JSON and store it at R_CHUNKSIZE. */ static gpg_error_t get_chunksize (cjson_t json, size_t *r_chunksize) { cjson_t j_item; *r_chunksize = DEF_REPLY_CHUNK_SIZE; j_item = cJSON_GetObjectItem (json, "chunksize"); if (!j_item) ; else if (!cjson_is_number (j_item)) return gpg_error (GPG_ERR_INV_VALUE); else if ((size_t)j_item->valueint < MIN_REPLY_CHUNK_SIZE) *r_chunksize = MIN_REPLY_CHUNK_SIZE; else if ((size_t)j_item->valueint > MAX_REPLY_CHUNK_SIZE) *r_chunksize = MAX_REPLY_CHUNK_SIZE; else *r_chunksize = (size_t)j_item->valueint; return 0; } /* Extract the keys from the array or string with the name "name" * in the JSON object. On success a string with the keys identifiers * is stored at R_KEYS. * The keys in that string are LF delimited. On failure an error code * is returned. */ static gpg_error_t get_keys (cjson_t json, const char *name, char **r_keystring) { cjson_t j_keys, j_item; int i, nkeys; char *p; size_t length; *r_keystring = NULL; j_keys = cJSON_GetObjectItem (json, name); if (!j_keys) return gpg_error (GPG_ERR_NO_KEY); if (!cjson_is_array (j_keys) && !cjson_is_string (j_keys)) return gpg_error (GPG_ERR_INV_VALUE); /* Fixme: We should better use a membuf like thing. */ length = 1; /* For the EOS. */ if (cjson_is_string (j_keys)) { nkeys = 1; length += strlen (j_keys->valuestring); if (strchr (j_keys->valuestring, '\n')) return gpg_error (GPG_ERR_INV_USER_ID); } else { nkeys = cJSON_GetArraySize (j_keys); if (!nkeys) return gpg_error (GPG_ERR_NO_KEY); for (i=0; i < nkeys; i++) { j_item = cJSON_GetArrayItem (j_keys, i); if (!j_item || !cjson_is_string (j_item)) return gpg_error (GPG_ERR_INV_VALUE); if (i) length++; /* Space for delimiter. */ length += strlen (j_item->valuestring); if (strchr (j_item->valuestring, '\n')) return gpg_error (GPG_ERR_INV_USER_ID); } } p = *r_keystring = xtrymalloc (length); if (!p) return gpg_error_from_syserror (); if (cjson_is_string (j_keys)) { strcpy (p, j_keys->valuestring); } else { for (i=0; i < nkeys; i++) { j_item = cJSON_GetArrayItem (j_keys, i); if (i) *p++ = '\n'; /* Add delimiter. */ p = stpcpy (p, j_item->valuestring); } } return 0; } /* * GPGME support functions. */ /* Helper for get_context. */ static gpgme_ctx_t _create_new_context (gpgme_protocol_t proto) { gpg_error_t err; gpgme_ctx_t ctx; err = gpgme_new (&ctx); if (err) log_fatal ("error creating GPGME context: %s\n", gpg_strerror (err)); gpgme_set_protocol (ctx, proto); gpgme_set_ctx_flag (ctx, "request-origin", "browser"); return ctx; } /* Return a context object for protocol PROTO. This is currently a * statically allocated context initialized for PROTO. Terminates * process on failure. */ static gpgme_ctx_t get_context (gpgme_protocol_t proto) { static gpgme_ctx_t ctx_openpgp, ctx_cms, ctx_conf; if (proto == GPGME_PROTOCOL_OpenPGP) { if (!ctx_openpgp) ctx_openpgp = _create_new_context (proto); return ctx_openpgp; } else if (proto == GPGME_PROTOCOL_CMS) { if (!ctx_cms) ctx_cms = _create_new_context (proto); return ctx_cms; } else if (proto == GPGME_PROTOCOL_GPGCONF) { if (!ctx_conf) ctx_conf = _create_new_context (proto); return ctx_conf; } else log_bug ("invalid protocol %d requested\n", proto); } /* Free context object retrieved by get_context. */ static void release_context (gpgme_ctx_t ctx) { /* Nothing to do right now. */ (void)ctx; } /* Create an addition context for short operations. */ static gpgme_ctx_t create_onetime_context (gpgme_protocol_t proto) { return _create_new_context (proto); } /* Release a one-time context. */ static void release_onetime_context (gpgme_ctx_t ctx) { return gpgme_release (ctx); } /* Given a Base-64 encoded string object in JSON return a gpgme data * object at R_DATA. */ static gpg_error_t data_from_base64_string (gpgme_data_t *r_data, cjson_t json) { #if GPGRT_VERSION_NUMBER < 0x011d00 /* 1.29 */ *r_data = NULL; return gpg_error (GPG_ERR_NOT_SUPPORTED); #else gpg_error_t err; size_t len; char *buf = NULL; gpgrt_b64state_t state = NULL; gpgme_data_t data = NULL; *r_data = NULL; /* A quick check on the JSON. */ if (!cjson_is_string (json)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } state = gpgrt_b64dec_start (NULL); if (!state) { err = gpg_err_code_from_syserror (); goto leave; } /* Fixme: Data duplication - we should see how to snatch the memory * from the json object. */ len = strlen (json->valuestring); buf = xtrystrdup (json->valuestring); if (!buf) { err = gpg_error_from_syserror (); goto leave; } err = gpgrt_b64dec_proc (state, buf, len, &len); if (err) goto leave; err = gpgrt_b64dec_finish (state); state = NULL; if (err) goto leave; err = gpgme_data_new_from_mem (&data, buf, len, 1); if (err) goto leave; *r_data = data; data = NULL; leave: xfree (data); xfree (buf); gpgrt_b64dec_finish (state); return err; #endif } /* Create a keylist pattern array from a json keys object * in the request. Returns either a malloced NULL terminated * string array which can be used as patterns for * op_keylist_ext or NULL. */ static char ** create_keylist_patterns (cjson_t request, const char *name) { char *keystring; char *p; char *tmp; char **ret; int cnt = 2; /* Last NULL and one is not newline delimited */ int i = 0; if (get_keys (request, name, &keystring)) return NULL; for (p = keystring; *p; p++) if (*p == '\n') cnt++; ret = xcalloc (cnt, sizeof *ret); for (p = keystring, tmp = keystring; *p; p++) { if (*p != '\n') continue; *p = '\0'; ret[i++] = xstrdup (tmp); tmp = p + 1; } /* The last key is not newline delimited. */ ret[i] = *tmp ? xstrdup (tmp) : NULL; xfree (keystring); return ret; } /* Do a secret keylisting for protocol proto and add the fingerprints of the secret keys for patterns to the result as "sec-fprs" array. */ static gpg_error_t add_secret_fprs (const char **patterns, gpgme_protocol_t protocol, cjson_t result) { gpgme_ctx_t ctx; gpg_error_t err; gpgme_key_t key = NULL; cjson_t j_fprs = xjson_CreateArray (); ctx = create_onetime_context (protocol); gpgme_set_keylist_mode (ctx, GPGME_KEYLIST_MODE_LOCAL | GPGME_KEYLIST_MODE_WITH_SECRET); err = gpgme_op_keylist_ext_start (ctx, patterns, 1, 0); if (err) { gpg_error_object (result, err, "Error listing keys: %s", gpg_strerror (err)); goto leave; } while (!(err = gpgme_op_keylist_next (ctx, &key))) { if (!key || !key->fpr) continue; cJSON_AddItemToArray (j_fprs, cJSON_CreateString (key->fpr)); gpgme_key_unref (key); key = NULL; } err = 0; release_onetime_context (ctx); ctx = NULL; xjson_AddItemToObject (result, "sec-fprs", j_fprs); leave: release_onetime_context (ctx); gpgme_key_unref (key); return err; } /* Create sigsum json array */ static cjson_t sigsum_to_json (gpgme_sigsum_t summary) { cjson_t result = xjson_CreateObject (); cjson_t sigsum_array = xjson_CreateArray (); if ( (summary & GPGME_SIGSUM_VALID )) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("valid")); if ( (summary & GPGME_SIGSUM_GREEN )) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("green")); if ( (summary & GPGME_SIGSUM_RED )) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("red")); if ( (summary & GPGME_SIGSUM_KEY_REVOKED)) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("revoked")); if ( (summary & GPGME_SIGSUM_KEY_EXPIRED)) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("key-expired")); if ( (summary & GPGME_SIGSUM_SIG_EXPIRED)) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("sig-expired")); if ( (summary & GPGME_SIGSUM_KEY_MISSING)) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("key-missing")); if ( (summary & GPGME_SIGSUM_CRL_MISSING)) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("crl-missing")); if ( (summary & GPGME_SIGSUM_CRL_TOO_OLD)) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("crl-too-old")); if ( (summary & GPGME_SIGSUM_BAD_POLICY )) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("bad-policy")); if ( (summary & GPGME_SIGSUM_SYS_ERROR )) cJSON_AddItemToArray (sigsum_array, cJSON_CreateString ("sys-error")); /* The signature summary as string array. */ xjson_AddItemToObject (result, "sigsum", sigsum_array); /* Bools for the same. */ xjson_AddBoolToObject (result, "valid", (summary & GPGME_SIGSUM_VALID )); xjson_AddBoolToObject (result, "green", (summary & GPGME_SIGSUM_GREEN )); xjson_AddBoolToObject (result, "red", (summary & GPGME_SIGSUM_RED )); xjson_AddBoolToObject (result, "revoked", (summary & GPGME_SIGSUM_KEY_REVOKED)); xjson_AddBoolToObject (result, "key-expired", (summary & GPGME_SIGSUM_KEY_EXPIRED)); xjson_AddBoolToObject (result, "sig-expired", (summary & GPGME_SIGSUM_SIG_EXPIRED)); xjson_AddBoolToObject (result, "key-missing", (summary & GPGME_SIGSUM_KEY_MISSING)); xjson_AddBoolToObject (result, "crl-missing", (summary & GPGME_SIGSUM_CRL_MISSING)); xjson_AddBoolToObject (result, "crl-too-old", (summary & GPGME_SIGSUM_CRL_TOO_OLD)); xjson_AddBoolToObject (result, "bad-policy", (summary & GPGME_SIGSUM_BAD_POLICY )); xjson_AddBoolToObject (result, "sys-error", (summary & GPGME_SIGSUM_SYS_ERROR )); return result; } /* Helper for summary formatting */ static const char * validity_to_string (gpgme_validity_t val) { switch (val) { case GPGME_VALIDITY_UNDEFINED:return "undefined"; case GPGME_VALIDITY_NEVER: return "never"; case GPGME_VALIDITY_MARGINAL: return "marginal"; case GPGME_VALIDITY_FULL: return "full"; case GPGME_VALIDITY_ULTIMATE: return "ultimate"; case GPGME_VALIDITY_UNKNOWN: default: return "unknown"; } } static const char * protocol_to_string (gpgme_protocol_t proto) { switch (proto) { case GPGME_PROTOCOL_OpenPGP: return "OpenPGP"; case GPGME_PROTOCOL_CMS: return "CMS"; case GPGME_PROTOCOL_GPGCONF: return "gpgconf"; case GPGME_PROTOCOL_ASSUAN: return "assuan"; case GPGME_PROTOCOL_G13: return "g13"; case GPGME_PROTOCOL_UISERVER:return "uiserver"; case GPGME_PROTOCOL_SPAWN: return "spawn"; default: return "unknown"; } } /* Create a sig_notation json object */ static cjson_t sig_notation_to_json (gpgme_sig_notation_t not) { cjson_t result = xjson_CreateObject (); xjson_AddBoolToObject (result, "human_readable", not->human_readable); xjson_AddBoolToObject (result, "critical", not->critical); xjson_AddStringToObject0 (result, "name", not->name); xjson_AddStringToObject0 (result, "value", not->value); xjson_AddNumberToObject (result, "flags", not->flags); return result; } /* Create a key_sig json object */ static cjson_t key_sig_to_json (gpgme_key_sig_t sig) { cjson_t result = xjson_CreateObject (); xjson_AddBoolToObject (result, "revoked", sig->revoked); xjson_AddBoolToObject (result, "expired", sig->expired); xjson_AddBoolToObject (result, "invalid", sig->invalid); xjson_AddBoolToObject (result, "exportable", sig->exportable); xjson_AddStringToObject0 (result, "pubkey_algo_name", gpgme_pubkey_algo_name (sig->pubkey_algo)); xjson_AddStringToObject0 (result, "keyid", sig->keyid); xjson_AddStringToObject0 (result, "status", gpgme_strerror (sig->status)); xjson_AddStringToObject0 (result, "name", sig->name); xjson_AddStringToObject0 (result, "email", sig->email); xjson_AddStringToObject0 (result, "comment", sig->comment); xjson_AddNumberToObject (result, "pubkey_algo", sig->pubkey_algo); xjson_AddNumberToObject (result, "timestamp", sig->timestamp); xjson_AddNumberToObject (result, "expires", sig->expires); xjson_AddNumberToObject (result, "status_code", sig->status); xjson_AddNumberToObject (result, "sig_class", sig->sig_class); if (sig->notations) { gpgme_sig_notation_t not; cjson_t array = xjson_CreateArray (); for (not = sig->notations; not; not = not->next) cJSON_AddItemToArray (array, sig_notation_to_json (not)); xjson_AddItemToObject (result, "notations", array); } return result; } /* Create a tofu info object */ static cjson_t tofu_to_json (gpgme_tofu_info_t tofu) { cjson_t result = xjson_CreateObject (); xjson_AddStringToObject0 (result, "description", tofu->description); xjson_AddNumberToObject (result, "validity", tofu->validity); xjson_AddNumberToObject (result, "policy", tofu->policy); xjson_AddNumberToObject (result, "signcount", tofu->signcount); xjson_AddNumberToObject (result, "encrcount", tofu->encrcount); xjson_AddNumberToObject (result, "signfirst", tofu->signfirst); xjson_AddNumberToObject (result, "signlast", tofu->signlast); xjson_AddNumberToObject (result, "encrfirst", tofu->encrfirst); xjson_AddNumberToObject (result, "encrlast", tofu->encrlast); return result; } /* Create a userid json object */ static cjson_t uid_to_json (gpgme_user_id_t uid) { cjson_t result = xjson_CreateObject (); xjson_AddBoolToObject (result, "revoked", uid->revoked); xjson_AddBoolToObject (result, "invalid", uid->invalid); xjson_AddStringToObject0 (result, "validity", validity_to_string (uid->validity)); xjson_AddStringToObject0 (result, "uid", uid->uid); xjson_AddStringToObject0 (result, "name", uid->name); xjson_AddStringToObject0 (result, "email", uid->email); xjson_AddStringToObject0 (result, "comment", uid->comment); xjson_AddStringToObject0 (result, "address", uid->address); xjson_AddNumberToObject (result, "origin", uid->origin); xjson_AddNumberToObject (result, "last_update", uid->last_update); /* Key sigs */ if (uid->signatures) { cjson_t sig_array = xjson_CreateArray (); gpgme_key_sig_t sig; for (sig = uid->signatures; sig; sig = sig->next) cJSON_AddItemToArray (sig_array, key_sig_to_json (sig)); xjson_AddItemToObject (result, "signatures", sig_array); } /* TOFU info */ if (uid->tofu) { gpgme_tofu_info_t tofu; cjson_t array = xjson_CreateArray (); for (tofu = uid->tofu; tofu; tofu = tofu->next) cJSON_AddItemToArray (array, tofu_to_json (tofu)); xjson_AddItemToObject (result, "tofu", array); } return result; } /* Create a subkey json object */ static cjson_t subkey_to_json (gpgme_subkey_t sub) { cjson_t result = xjson_CreateObject (); xjson_AddBoolToObject (result, "revoked", sub->revoked); xjson_AddBoolToObject (result, "expired", sub->expired); xjson_AddBoolToObject (result, "disabled", sub->disabled); xjson_AddBoolToObject (result, "invalid", sub->invalid); xjson_AddBoolToObject (result, "can_encrypt", sub->can_encrypt); xjson_AddBoolToObject (result, "can_sign", sub->can_sign); xjson_AddBoolToObject (result, "can_certify", sub->can_certify); xjson_AddBoolToObject (result, "can_authenticate", sub->can_authenticate); xjson_AddBoolToObject (result, "secret", sub->secret); xjson_AddBoolToObject (result, "is_qualified", sub->is_qualified); xjson_AddBoolToObject (result, "is_cardkey", sub->is_cardkey); xjson_AddBoolToObject (result, "is_de_vs", sub->is_de_vs); xjson_AddStringToObject0 (result, "pubkey_algo_name", gpgme_pubkey_algo_name (sub->pubkey_algo)); xjson_AddStringToObject0 (result, "pubkey_algo_string", gpgme_pubkey_algo_string (sub)); xjson_AddStringToObject0 (result, "keyid", sub->keyid); xjson_AddStringToObject0 (result, "card_number", sub->card_number); xjson_AddStringToObject0 (result, "curve", sub->curve); xjson_AddStringToObject0 (result, "keygrip", sub->keygrip); xjson_AddNumberToObject (result, "pubkey_algo", sub->pubkey_algo); xjson_AddNumberToObject (result, "length", sub->length); xjson_AddNumberToObject (result, "timestamp", sub->timestamp); xjson_AddNumberToObject (result, "expires", sub->expires); return result; } /* Create a key json object */ static cjson_t key_to_json (gpgme_key_t key) { cjson_t result = xjson_CreateObject (); xjson_AddBoolToObject (result, "revoked", key->revoked); xjson_AddBoolToObject (result, "expired", key->expired); xjson_AddBoolToObject (result, "disabled", key->disabled); xjson_AddBoolToObject (result, "invalid", key->invalid); xjson_AddBoolToObject (result, "can_encrypt", key->can_encrypt); xjson_AddBoolToObject (result, "can_sign", key->can_sign); xjson_AddBoolToObject (result, "can_certify", key->can_certify); xjson_AddBoolToObject (result, "can_authenticate", key->can_authenticate); xjson_AddBoolToObject (result, "secret", key->secret); xjson_AddBoolToObject (result, "is_qualified", key->is_qualified); xjson_AddStringToObject0 (result, "protocol", protocol_to_string (key->protocol)); xjson_AddStringToObject0 (result, "issuer_serial", key->issuer_serial); xjson_AddStringToObject0 (result, "issuer_name", key->issuer_name); xjson_AddStringToObject0 (result, "fingerprint", key->fpr); xjson_AddStringToObject0 (result, "chain_id", key->chain_id); xjson_AddStringToObject0 (result, "owner_trust", validity_to_string (key->owner_trust)); xjson_AddNumberToObject (result, "origin", key->origin); xjson_AddNumberToObject (result, "last_update", key->last_update); /* Add subkeys */ if (key->subkeys) { cjson_t subkey_array = xjson_CreateArray (); gpgme_subkey_t sub; for (sub = key->subkeys; sub; sub = sub->next) cJSON_AddItemToArray (subkey_array, subkey_to_json (sub)); xjson_AddItemToObject (result, "subkeys", subkey_array); } /* User Ids */ if (key->uids) { cjson_t uid_array = xjson_CreateArray (); gpgme_user_id_t uid; for (uid = key->uids; uid; uid = uid->next) cJSON_AddItemToArray (uid_array, uid_to_json (uid)); xjson_AddItemToObject (result, "userids", uid_array); } return result; } /* Create a signature json object */ static cjson_t signature_to_json (gpgme_signature_t sig) { cjson_t result = xjson_CreateObject (); xjson_AddItemToObject (result, "summary", sigsum_to_json (sig->summary)); xjson_AddBoolToObject (result, "wrong_key_usage", sig->wrong_key_usage); xjson_AddBoolToObject (result, "chain_model", sig->chain_model); xjson_AddBoolToObject (result, "is_de_vs", sig->is_de_vs); xjson_AddStringToObject0 (result, "status_string", gpgme_strerror (sig->status)); xjson_AddStringToObject0 (result, "fingerprint", sig->fpr); xjson_AddStringToObject0 (result, "validity_string", validity_to_string (sig->validity)); xjson_AddStringToObject0 (result, "pubkey_algo_name", gpgme_pubkey_algo_name (sig->pubkey_algo)); xjson_AddStringToObject0 (result, "hash_algo_name", gpgme_hash_algo_name (sig->hash_algo)); xjson_AddStringToObject0 (result, "pka_address", sig->pka_address); xjson_AddNumberToObject (result, "status_code", sig->status); xjson_AddNumberToObject (result, "timestamp", sig->timestamp); xjson_AddNumberToObject (result, "exp_timestamp", sig->exp_timestamp); xjson_AddNumberToObject (result, "pka_trust", sig->pka_trust); xjson_AddNumberToObject (result, "validity", sig->validity); xjson_AddNumberToObject (result, "validity_reason", sig->validity_reason); if (sig->notations) { gpgme_sig_notation_t not; cjson_t array = xjson_CreateArray (); for (not = sig->notations; not; not = not->next) cJSON_AddItemToArray (array, sig_notation_to_json (not)); xjson_AddItemToObject (result, "notations", array); } return result; } /* Create a JSON object from a gpgme_verify result */ static cjson_t verify_result_to_json (gpgme_verify_result_t verify_result) { cjson_t result = xjson_CreateObject (); xjson_AddBoolToObject (result, "is_mime", verify_result->is_mime); if (verify_result->signatures) { cjson_t array = xjson_CreateArray (); gpgme_signature_t sig; for (sig = verify_result->signatures; sig; sig = sig->next) cJSON_AddItemToArray (array, signature_to_json (sig)); xjson_AddItemToObject (result, "signatures", array); } return result; } /* Create a recipient json object */ static cjson_t recipient_to_json (gpgme_recipient_t recp) { cjson_t result = xjson_CreateObject (); xjson_AddStringToObject0 (result, "keyid", recp->keyid); xjson_AddStringToObject0 (result, "pubkey_algo_name", gpgme_pubkey_algo_name (recp->pubkey_algo)); xjson_AddStringToObject0 (result, "status_string", gpgme_strerror (recp->status)); xjson_AddNumberToObject (result, "status_code", recp->status); return result; } /* Create a JSON object from a gpgme_decrypt result */ static cjson_t decrypt_result_to_json (gpgme_decrypt_result_t decrypt_result) { cjson_t result = xjson_CreateObject (); xjson_AddStringToObject0 (result, "file_name", decrypt_result->file_name); xjson_AddStringToObject0 (result, "symkey_algo", decrypt_result->symkey_algo); xjson_AddBoolToObject (result, "wrong_key_usage", decrypt_result->wrong_key_usage); xjson_AddBoolToObject (result, "is_de_vs", decrypt_result->is_de_vs); xjson_AddBoolToObject (result, "is_mime", decrypt_result->is_mime); xjson_AddBoolToObject (result, "legacy_cipher_nomdc", decrypt_result->legacy_cipher_nomdc); if (decrypt_result->recipients) { cjson_t array = xjson_CreateArray (); gpgme_recipient_t recp; for (recp = decrypt_result->recipients; recp; recp = recp->next) cJSON_AddItemToArray (array, recipient_to_json (recp)); xjson_AddItemToObject (result, "recipients", array); } return result; } /* Create a JSON object from an engine_info */ static cjson_t engine_info_to_json (gpgme_engine_info_t info) { cjson_t result = xjson_CreateObject (); xjson_AddStringToObject0 (result, "protocol", protocol_to_string (info->protocol)); xjson_AddStringToObject0 (result, "fname", info->file_name); xjson_AddStringToObject0 (result, "version", info->version); xjson_AddStringToObject0 (result, "req_version", info->req_version); xjson_AddStringToObject0 (result, "homedir", info->home_dir ? info->home_dir : "default"); return result; } /* Create a JSON object from an import_status */ static cjson_t import_status_to_json (gpgme_import_status_t sts) { cjson_t result = xjson_CreateObject (); xjson_AddStringToObject0 (result, "fingerprint", sts->fpr); xjson_AddStringToObject0 (result, "error_string", gpgme_strerror (sts->result)); xjson_AddNumberToObject (result, "status", sts->status); return result; } /* Create a JSON object from an import result */ static cjson_t import_result_to_json (gpgme_import_result_t imp) { cjson_t result = xjson_CreateObject (); xjson_AddNumberToObject (result, "considered", imp->considered); xjson_AddNumberToObject (result, "no_user_id", imp->no_user_id); xjson_AddNumberToObject (result, "imported", imp->imported); xjson_AddNumberToObject (result, "imported_rsa", imp->imported_rsa); xjson_AddNumberToObject (result, "unchanged", imp->unchanged); xjson_AddNumberToObject (result, "new_user_ids", imp->new_user_ids); xjson_AddNumberToObject (result, "new_sub_keys", imp->new_sub_keys); xjson_AddNumberToObject (result, "new_signatures", imp->new_signatures); xjson_AddNumberToObject (result, "new_revocations", imp->new_revocations); xjson_AddNumberToObject (result, "secret_read", imp->secret_read); xjson_AddNumberToObject (result, "secret_imported", imp->secret_imported); xjson_AddNumberToObject (result, "secret_unchanged", imp->secret_unchanged); xjson_AddNumberToObject (result, "skipped_new_keys", imp->skipped_new_keys); xjson_AddNumberToObject (result, "not_imported", imp->not_imported); xjson_AddNumberToObject (result, "skipped_v3_keys", imp->skipped_v3_keys); if (imp->imports) { cjson_t array = xjson_CreateArray (); gpgme_import_status_t status; for (status = imp->imports; status; status = status->next) cJSON_AddItemToArray (array, import_status_to_json (status)); xjson_AddItemToObject (result, "imports", array); } return result; } /* Create a JSON object from a gpgconf arg */ static cjson_t conf_arg_to_json (gpgme_conf_arg_t arg, gpgme_conf_type_t type) { cjson_t result = xjson_CreateObject (); int is_none = 0; switch (type) { case GPGME_CONF_STRING: case GPGME_CONF_PATHNAME: case GPGME_CONF_LDAP_SERVER: case GPGME_CONF_KEY_FPR: case GPGME_CONF_PUB_KEY: case GPGME_CONF_SEC_KEY: case GPGME_CONF_ALIAS_LIST: xjson_AddStringToObject0 (result, "string", arg->value.string); break; case GPGME_CONF_UINT32: xjson_AddNumberToObject (result, "number", arg->value.uint32); break; case GPGME_CONF_INT32: xjson_AddNumberToObject (result, "number", arg->value.int32); break; case GPGME_CONF_NONE: default: is_none = 1; break; } xjson_AddBoolToObject (result, "is_none", is_none); return result; } /* Create a JSON object from a gpgconf option */ static cjson_t conf_opt_to_json (gpgme_conf_opt_t opt) { cjson_t result = xjson_CreateObject (); xjson_AddStringToObject0 (result, "name", opt->name); xjson_AddStringToObject0 (result, "description", opt->description); xjson_AddStringToObject0 (result, "argname", opt->argname); xjson_AddStringToObject0 (result, "default_description", opt->default_description); xjson_AddStringToObject0 (result, "no_arg_description", opt->no_arg_description); xjson_AddNumberToObject (result, "flags", opt->flags); xjson_AddNumberToObject (result, "level", opt->level); xjson_AddNumberToObject (result, "type", opt->type); xjson_AddNumberToObject (result, "alt_type", opt->alt_type); if (opt->default_value) { cjson_t array = xjson_CreateArray (); gpgme_conf_arg_t arg; for (arg = opt->default_value; arg; arg = arg->next) cJSON_AddItemToArray (array, conf_arg_to_json (arg, opt->alt_type)); xjson_AddItemToObject (result, "default_value", array); } if (opt->no_arg_value) { cjson_t array = xjson_CreateArray (); gpgme_conf_arg_t arg; for (arg = opt->no_arg_value; arg; arg = arg->next) cJSON_AddItemToArray (array, conf_arg_to_json (arg, opt->alt_type)); xjson_AddItemToObject (result, "no_arg_value", array); } if (opt->value) { cjson_t array = xjson_CreateArray (); gpgme_conf_arg_t arg; for (arg = opt->value; arg; arg = arg->next) cJSON_AddItemToArray (array, conf_arg_to_json (arg, opt->alt_type)); xjson_AddItemToObject (result, "value", array); } return result; } /* Create a JSON object from a gpgconf component*/ static cjson_t conf_comp_to_json (gpgme_conf_comp_t cmp) { cjson_t result = xjson_CreateObject (); xjson_AddStringToObject0 (result, "name", cmp->name); xjson_AddStringToObject0 (result, "description", cmp->description); xjson_AddStringToObject0 (result, "program_name", cmp->program_name); if (cmp->options) { cjson_t array = xjson_CreateArray (); gpgme_conf_opt_t opt; for (opt = cmp->options; opt; opt = opt->next) cJSON_AddItemToArray (array, conf_opt_to_json (opt)); xjson_AddItemToObject (result, "options", array); } return result; } /* Create a gpgme_data from json string data named "name" * in the request. Takes the base64 option into account. * * Adds an error to the "result" on error. */ static gpg_error_t get_string_data (cjson_t request, cjson_t result, const char *name, gpgme_data_t *r_data) { gpgme_error_t err; int opt_base64; cjson_t j_data; if ((err = get_boolean_flag (request, "base64", 0, &opt_base64))) return err; /* Get the data. Note that INPUT is a shallow data object with the * storage hold in REQUEST. */ j_data = cJSON_GetObjectItem (request, name); if (!j_data) { return gpg_error (GPG_ERR_NO_DATA); } if (!cjson_is_string (j_data)) { return gpg_error (GPG_ERR_INV_VALUE); } if (opt_base64) { err = data_from_base64_string (r_data, j_data); if (err) { gpg_error_object (result, err, "Error decoding Base-64 encoded '%s': %s", name, gpg_strerror (err)); return err; } } else { err = gpgme_data_new_from_mem (r_data, j_data->valuestring, strlen (j_data->valuestring), 0); if (err) { gpg_error_object (result, err, "Error getting '%s': %s", name, gpg_strerror (err)); return err; } } return 0; } /* Create a "data" object and the "type" and "base64" flags * from DATA and append them to RESULT. Ownership of DATA is * transferred to this function. TYPE must be a fixed string. * If BASE64 is -1 the need for base64 encoding is determined * by the content of DATA, all other values are taken as true * or false. */ static gpg_error_t make_data_object (cjson_t result, gpgme_data_t data, const char *type, int base64) { gpg_error_t err; char *buffer; const char *s; size_t buflen, n; if (!base64 || base64 == -1) /* Make sure that we really have a string. */ gpgme_data_write (data, "", 1); buffer = gpgme_data_release_and_get_mem (data, &buflen); data = NULL; if (!buffer) { err = gpg_error_from_syserror (); goto leave; } if (base64 == -1) { base64 = 0; if (!buflen) log_fatal ("Appended Nul byte got lost\n"); /* Figure out if there is any Nul octet in the buffer. In that * case we need to Base-64 the buffer. Due to problems with the * browser's Javascript we use Base-64 also in case an UTF-8 * character is in the buffer. This is because the chunking may * split an UTF-8 characters and JS can't handle this. */ for (s=buffer, n=0; n < buflen -1; s++, n++) if (!*s || (*s & 0x80)) { buflen--; /* Adjust for the extra nul byte. */ base64 = 1; break; } } xjson_AddStringToObject (result, "type", type); xjson_AddBoolToObject (result, "base64", base64); if (base64) err = add_base64_to_object (result, "data", buffer, buflen); else err = cjson_AddStringToObject (result, "data", buffer); leave: gpgme_free (buffer); return err; } /* Encode and chunk response. * * If necessary this base64 encodes and chunks the response * for getmore so that we always return valid json independent * of the chunksize. * * A chunked response contains the base64 encoded chunk * as a string and a boolean if there is still more data * available for getmore like: * { * chunk: "SGVsbG8gV29ybGQK" * more: true * } * * Chunking is only done if the response is larger then the * chunksize. * * caller has to xfree the return value. */ static char * encode_and_chunk (cjson_t request, cjson_t response) { char *data; gpg_error_t err = 0; size_t chunksize = 0; char *getmore_request = NULL; if (opt_interactive) data = cJSON_Print (response); else data = cJSON_PrintUnformatted (response); if (!data) { err = GPG_ERR_NO_DATA; goto leave; } if (!request) { goto leave; } if ((err = get_chunksize (request, &chunksize))) { err = GPG_ERR_INV_VALUE; goto leave; } if (!chunksize) goto leave; pending_data.buffer = data; /* Data should already be encoded so that it does not contain 0.*/ pending_data.length = strlen (data); pending_data.written = 0; if (gpgrt_asprintf (&getmore_request, "{ \"op\":\"getmore\", \"chunksize\": %i }", (int) chunksize) == -1) { err = gpg_error_from_syserror (); goto leave; } data = process_request (getmore_request); leave: xfree (getmore_request); if (!err && !data) { err = GPG_ERR_GENERAL; } if (err) { cjson_t err_obj = gpg_error_object (NULL, err, "Encode and chunk failed: %s", gpgme_strerror (err)); xfree (data); if (opt_interactive) data = cJSON_Print (err_obj); data = cJSON_PrintUnformatted (err_obj); cJSON_Delete (err_obj); } return data; } /* * Implementation of the commands. */ static const char hlp_encrypt[] = "op: \"encrypt\"\n" "keys: Array of strings with the fingerprints or user-ids\n" " of the keys to encrypt the data. For a single key\n" " a String may be used instead of an array.\n" "data: Input data. \n" "\n" "Optional parameters:\n" "protocol: Either \"openpgp\" (default) or \"cms\".\n" "signing_keys: Similar to the keys parameter for added signing.\n" " (openpgp only)" "file_name: The file name associated with the data.\n" "sender: Sender info to embed in a signature.\n" "\n" "Optional boolean flags (default is false):\n" "base64: Input data is base64 encoded.\n" "mime: Indicate that data is a MIME object.\n" "armor: Request output in armored format.\n" "always-trust: Request --always-trust option.\n" "no-encrypt-to: Do not use a default recipient.\n" "no-compress: Do not compress the plaintext first.\n" "throw-keyids: Request the --throw-keyids option.\n" "want-address: Require that the keys include a mail address.\n" "wrap: Assume the input is an OpenPGP message.\n" "\n" "Response on success:\n" "type: \"ciphertext\"\n" "data: Unless armor mode is used a Base64 encoded binary\n" " ciphertext. In armor mode a string with an armored\n" " OpenPGP or a PEM message.\n" "base64: Boolean indicating whether data is base64 encoded."; static gpg_error_t op_encrypt (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_protocol_t protocol; char **signing_patterns = NULL; int opt_mime; char *keystring = NULL; char *file_name = NULL; gpgme_data_t input = NULL; gpgme_data_t output = NULL; int abool; gpgme_encrypt_flags_t encrypt_flags = 0; gpgme_ctx_t keylist_ctx = NULL; gpgme_key_t key = NULL; cjson_t j_tmp = NULL; if ((err = get_protocol (request, &protocol))) goto leave; ctx = get_context (protocol); if ((err = get_boolean_flag (request, "mime", 0, &opt_mime))) goto leave; if ((err = get_boolean_flag (request, "armor", 0, &abool))) goto leave; gpgme_set_armor (ctx, abool); if ((err = get_boolean_flag (request, "always-trust", 0, &abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_ALWAYS_TRUST; if ((err = get_boolean_flag (request, "no-encrypt-to", 0,&abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_NO_ENCRYPT_TO; if ((err = get_boolean_flag (request, "no-compress", 0, &abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_NO_COMPRESS; if ((err = get_boolean_flag (request, "throw-keyids", 0, &abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_THROW_KEYIDS; if ((err = get_boolean_flag (request, "wrap", 0, &abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_WRAP; if ((err = get_boolean_flag (request, "want-address", 0, &abool))) goto leave; if (abool) encrypt_flags |= GPGME_ENCRYPT_WANT_ADDRESS; j_tmp = cJSON_GetObjectItem (request, "file_name"); if (j_tmp && cjson_is_string (j_tmp)) { file_name = j_tmp->valuestring; } j_tmp = cJSON_GetObjectItem (request, "sender"); if (j_tmp && cjson_is_string (j_tmp)) { gpgme_set_sender (ctx, j_tmp->valuestring); } /* Get the keys. */ err = get_keys (request, "keys", &keystring); if (err) { /* Provide a custom error response. */ gpg_error_object (result, err, "Error getting keys: %s", gpg_strerror (err)); goto leave; } /* Do we have signing keys ? */ signing_patterns = create_keylist_patterns (request, "signing_keys"); if (signing_patterns) { keylist_ctx = create_onetime_context (protocol); gpgme_set_keylist_mode (keylist_ctx, GPGME_KEYLIST_MODE_LOCAL); err = gpgme_op_keylist_ext_start (keylist_ctx, (const char **) signing_patterns, 1, 0); if (err) { gpg_error_object (result, err, "Error listing keys: %s", gpg_strerror (err)); goto leave; } while (!(err = gpgme_op_keylist_next (keylist_ctx, &key))) { if ((err = gpgme_signers_add (ctx, key))) { gpg_error_object (result, err, "Error adding signer: %s", gpg_strerror (err)); goto leave; } gpgme_key_unref (key); key = NULL; } release_onetime_context (keylist_ctx); keylist_ctx = NULL; } if ((err = get_string_data (request, result, "data", &input))) goto leave; if (opt_mime) gpgme_data_set_encoding (input, GPGME_DATA_ENCODING_MIME); if (file_name) { gpgme_data_set_file_name (input, file_name); } /* Create an output data object. */ err = gpgme_data_new (&output); if (err) { gpg_error_object (result, err, "Error creating output data object: %s", gpg_strerror (err)); goto leave; } /* Encrypt. */ if (!signing_patterns) { err = gpgme_op_encrypt_ext (ctx, NULL, keystring, encrypt_flags, input, output); } else { err = gpgme_op_encrypt_sign_ext (ctx, NULL, keystring, encrypt_flags, input, output); } /* encrypt_result = gpgme_op_encrypt_result (ctx); */ if (err) { gpg_error_object (result, err, "Encryption failed: %s", gpg_strerror (err)); goto leave; } gpgme_data_release (input); input = NULL; /* We need to base64 if armoring has not been requested. */ err = make_data_object (result, output, "ciphertext", !gpgme_get_armor (ctx)); output = NULL; leave: xfree_array (signing_patterns); xfree (keystring); release_onetime_context (keylist_ctx); /* Reset sender in case the context is reused */ gpgme_set_sender (ctx, NULL); gpgme_key_unref (key); gpgme_signers_clear (ctx); release_context (ctx); gpgme_data_release (input); gpgme_data_release (output); return err; } static const char hlp_decrypt[] = "op: \"decrypt\"\n" "data: The encrypted data.\n" "\n" "Optional parameters:\n" "protocol: Either \"openpgp\" (default) or \"cms\".\n" "\n" "Optional boolean flags (default is false):\n" "base64: Input data is base64 encoded.\n" "\n" "Response on success:\n" "type: \"plaintext\"\n" "data: The decrypted data. This may be base64 encoded.\n" "base64: Boolean indicating whether data is base64 encoded.\n" "mime: deprecated - use dec_info is_mime instead\n" "dec_info: An object with decryption information. (gpgme_decrypt_result_t)\n" " Boolean values:\n" " wrong_key_usage: Key should not have been used for encryption.\n" " is_de_vs: Message was encrypted in compliance to the de-vs\n" " mode.\n" " is_mime: Message claims that the content is a MIME Message.\n" " legacy_cipher_nomdc: The message was made by a legacy algorithm\n" " without integrity protection.\n" " String values:\n" " file_name: The filename contained in the decrypt result.\n" " symkey_algo: A string with the symmetric encryption algorithm and\n" " mode using the format \".\".\n" " Array values:\n" " recipients: The list of recipients (gpgme_recipient_t).\n" " String values:\n" " keyid: The keyid of the recipient.\n" " pubkey_algo_name: gpgme_pubkey_algo_name of used algo.\n" " status_string: The status code as localized gpg-error string\n" " Number values:\n" " status_code: The status as a number. (gpg_error_t)\n" "info: Optional an object with verification information.\n" " (gpgme_verify_result_t)\n" " file_name: The filename contained in the verify result.\n" " is_mime: The is_mime info contained in the verify result.\n" " signatures: Array of signatures\n" " summary: Object containing summary information.\n" " Boolean values: (Check gpgme_sigsum_t doc for meaning)\n" " valid\n" " green\n" " red\n" " revoked\n" " key-expired\n" " sig-expired\n" " key-missing\n" " crl-missing\n" " crl-too-old\n" " bad-policy\n" " sys-error\n" " sigsum: Array of strings representing the sigsum.\n" " Boolean values:\n" " wrong_key_usage: Key should not have been used for signing.\n" " chain_model: Validity has been verified using the chain model.\n" " is_de_vs: signature is in compliance to the de-vs mode.\n" " String values:\n" " status_string: The status code as localized gpg-error string\n" " fingerprint: The fingerprint of the signing key.\n" " validity_string: The validity as string.\n" " pubkey_algo_name: gpgme_pubkey_algo_name of used algo.\n" " hash_algo_name: gpgme_hash_algo_name of used hash algo\n" " pka_address: The mailbox from the PKA information.\n" " Number values:\n" " status_code: The status as a number. (gpg_error_t)\n" " timestamp: Signature creation time. (secs since epoch)\n" " exp_timestamp: Signature expiration or 0. (secs since epoch)\n" " pka_trust: PKA status: 0 = not available, 1 = bad, 2 = okay, 3 = RFU.\n" " validity: validity as number (gpgme_validity_t)\n" " validity_reason: (gpg_error_t)\n" " Array values:\n" " notations: Notation data and policy urls (gpgme_sig_notation_t)\n" " Boolean values:\n" " human_readable\n" " critical\n" " String values:\n" " name\n" " value\n" " Number values:\n" " flags\n"; static gpg_error_t op_decrypt (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_protocol_t protocol; gpgme_data_t input = NULL; gpgme_data_t output = NULL; gpgme_decrypt_result_t decrypt_result; gpgme_verify_result_t verify_result; if ((err = get_protocol (request, &protocol))) goto leave; ctx = get_context (protocol); if ((err = get_string_data (request, result, "data", &input))) goto leave; /* Create an output data object. */ err = gpgme_data_new (&output); if (err) { gpg_error_object (result, err, "Error creating output data object: %s", gpg_strerror (err)); goto leave; } /* Decrypt. */ err = gpgme_op_decrypt_ext (ctx, GPGME_DECRYPT_VERIFY, input, output); decrypt_result = gpgme_op_decrypt_result (ctx); if (err) { gpg_error_object (result, err, "Decryption failed: %s", gpg_strerror (err)); goto leave; } gpgme_data_release (input); input = NULL; if (decrypt_result->is_mime) xjson_AddBoolToObject (result, "mime", 1); xjson_AddItemToObject (result, "dec_info", decrypt_result_to_json (decrypt_result)); verify_result = gpgme_op_verify_result (ctx); if (verify_result && verify_result->signatures) { xjson_AddItemToObject (result, "info", verify_result_to_json (verify_result)); } err = make_data_object (result, output, "plaintext", -1); output = NULL; if (err) { gpg_error_object (result, err, "Plaintext output failed: %s", gpg_strerror (err)); goto leave; } leave: release_context (ctx); gpgme_data_release (input); gpgme_data_release (output); return err; } static const char hlp_sign[] = "op: \"sign\"\n" "keys: Array of strings with the fingerprints of the signing key.\n" " For a single key a String may be used instead of an array.\n" "data: Input data. \n" "\n" "Optional parameters:\n" "protocol: Either \"openpgp\" (default) or \"cms\".\n" "sender: The mail address of the sender.\n" "mode: A string with the signing mode can be:\n" " detached (default)\n" " opaque\n" " clearsign\n" "\n" "Optional boolean flags (default is false):\n" "base64: Input data is base64 encoded.\n" "armor: Request output in armored format.\n" "\n" "Response on success:\n" "type: \"signature\"\n" "data: Unless armor mode is used a Base64 encoded binary\n" " signature. In armor mode a string with an armored\n" " OpenPGP or a PEM message.\n" "base64: Boolean indicating whether data is base64 encoded.\n"; static gpg_error_t op_sign (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_protocol_t protocol; char **patterns = NULL; gpgme_data_t input = NULL; gpgme_data_t output = NULL; int abool; cjson_t j_tmp; gpgme_sig_mode_t mode = GPGME_SIG_MODE_DETACH; gpgme_ctx_t keylist_ctx = NULL; gpgme_key_t key = NULL; if ((err = get_protocol (request, &protocol))) goto leave; ctx = get_context (protocol); if ((err = get_boolean_flag (request, "armor", 0, &abool))) goto leave; gpgme_set_armor (ctx, abool); j_tmp = cJSON_GetObjectItem (request, "mode"); if (j_tmp && cjson_is_string (j_tmp)) { if (!strcmp (j_tmp->valuestring, "opaque")) { mode = GPGME_SIG_MODE_NORMAL; } else if (!strcmp (j_tmp->valuestring, "clearsign")) { mode = GPGME_SIG_MODE_CLEAR; } } j_tmp = cJSON_GetObjectItem (request, "sender"); if (j_tmp && cjson_is_string (j_tmp)) { gpgme_set_sender (ctx, j_tmp->valuestring); } patterns = create_keylist_patterns (request, "keys"); if (!patterns) { gpg_error_object (result, err, "Error getting keys: %s", gpg_strerror (gpg_error (GPG_ERR_NO_KEY))); goto leave; } /* Do a keylisting and add the keys */ keylist_ctx = create_onetime_context (protocol); gpgme_set_keylist_mode (keylist_ctx, GPGME_KEYLIST_MODE_LOCAL); err = gpgme_op_keylist_ext_start (keylist_ctx, (const char **) patterns, 1, 0); if (err) { gpg_error_object (result, err, "Error listing keys: %s", gpg_strerror (err)); goto leave; } while (!(err = gpgme_op_keylist_next (keylist_ctx, &key))) { if ((err = gpgme_signers_add (ctx, key))) { gpg_error_object (result, err, "Error adding signer: %s", gpg_strerror (err)); goto leave; } gpgme_key_unref (key); key = NULL; } if ((err = get_string_data (request, result, "data", &input))) goto leave; /* Create an output data object. */ err = gpgme_data_new (&output); if (err) { gpg_error_object (result, err, "Error creating output data object: %s", gpg_strerror (err)); goto leave; } /* Sign. */ err = gpgme_op_sign (ctx, input, output, mode); if (err) { gpg_error_object (result, err, "Signing failed: %s", gpg_strerror (err)); goto leave; } gpgme_data_release (input); input = NULL; /* We need to base64 if armoring has not been requested. */ err = make_data_object (result, output, "signature", !gpgme_get_armor (ctx)); output = NULL; leave: xfree_array (patterns); gpgme_signers_clear (ctx); gpgme_key_unref (key); release_onetime_context (keylist_ctx); release_context (ctx); gpgme_data_release (input); gpgme_data_release (output); return err; } static const char hlp_verify[] = "op: \"verify\"\n" "data: The data to verify.\n" "\n" "Optional parameters:\n" "protocol: Either \"openpgp\" (default) or \"cms\".\n" "signature: A detached signature. If missing opaque is assumed.\n" "\n" "Optional boolean flags (default is false):\n" "base64: Input data is base64 encoded.\n" "\n" "Response on success:\n" "type: \"plaintext\"\n" "data: The verified data. This may be base64 encoded.\n" "base64: Boolean indicating whether data is base64 encoded.\n" "info: An object with verification information (gpgme_verify_result_t).\n" " is_mime: Boolean that is true if the messages claims it is MIME.\n" " Note that this flag is not covered by the signature.)\n" " signatures: Array of signatures\n" " summary: Object containing summary information.\n" " Boolean values: (Check gpgme_sigsum_t doc for meaning)\n" " valid\n" " green\n" " red\n" " revoked\n" " key-expired\n" " sig-expired\n" " key-missing\n" " crl-missing\n" " crl-too-old\n" " bad-policy\n" " sys-error\n" " sigsum: Array of strings representing the sigsum.\n" " Boolean values:\n" " wrong_key_usage: Key should not have been used for signing.\n" " chain_model: Validity has been verified using the chain model.\n" " is_de_vs: signature is in compliance to the de-vs mode.\n" " String values:\n" " status_string: The status code as localized gpg-error string\n" " fingerprint: The fingerprint of the signing key.\n" " validity_string: The validity as string.\n" " pubkey_algo_name: gpgme_pubkey_algo_name of used algo.\n" " hash_algo_name: gpgme_hash_algo_name of used hash algo\n" " pka_address: The mailbox from the PKA information.\n" " Number values:\n" " status_code: The status as a number. (gpg_error_t)\n" " timestamp: Signature creation time. (secs since epoch)\n" " exp_timestamp: Signature expiration or 0. (secs since epoch)\n" " pka_trust: PKA status: 0 = not available, 1 = bad, 2 = okay, 3 = RFU.\n" " validity: validity as number (gpgme_validity_t)\n" " validity_reason: (gpg_error_t)\n" " Array values:\n" " notations: Notation data and policy urls (gpgme_sig_notation_t)\n" " Boolean values:\n" " human_readable\n" " critical\n" " String values:\n" " name\n" " value\n" " Number values:\n" " flags\n"; static gpg_error_t op_verify (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_protocol_t protocol; gpgme_data_t input = NULL; gpgme_data_t signature = NULL; gpgme_data_t output = NULL; gpgme_verify_result_t verify_result; if ((err = get_protocol (request, &protocol))) goto leave; ctx = get_context (protocol); if ((err = get_string_data (request, result, "data", &input))) goto leave; err = get_string_data (request, result, "signature", &signature); /* Signature data is optional otherwise we expect opaque or clearsigned. */ if (err && err != gpg_error (GPG_ERR_NO_DATA)) goto leave; if (!signature) { /* Verify opaque or clearsigned we need an output data object. */ err = gpgme_data_new (&output); if (err) { gpg_error_object (result, err, "Error creating output data object: %s", gpg_strerror (err)); goto leave; } err = gpgme_op_verify (ctx, input, 0, output); } else { err = gpgme_op_verify (ctx, signature, input, NULL); } if (err) { gpg_error_object (result, err, "Verify failed: %s", gpg_strerror (err)); goto leave; } gpgme_data_release (input); input = NULL; gpgme_data_release (signature); signature = NULL; verify_result = gpgme_op_verify_result (ctx); if (verify_result && verify_result->signatures) { xjson_AddItemToObject (result, "info", verify_result_to_json (verify_result)); } if (output) { err = make_data_object (result, output, "plaintext", -1); output = NULL; if (err) { gpg_error_object (result, err, "Plaintext output failed: %s", gpg_strerror (err)); goto leave; } } leave: release_context (ctx); gpgme_data_release (input); gpgme_data_release (output); gpgme_data_release (signature); return err; } static const char hlp_version[] = "op: \"version\"\n" "\n" "Response on success:\n" "gpgme: The GPGME Version.\n" "info: dump of engine info. containing:\n" " protocol: The protocol.\n" " fname: The file name.\n" " version: The version.\n" " req_ver: The required version.\n" " homedir: The homedir of the engine or \"default\".\n"; static gpg_error_t op_version (cjson_t request, cjson_t result) { gpg_error_t err = 0; gpgme_engine_info_t ei = NULL; cjson_t infos = xjson_CreateArray (); (void)request; if (!cJSON_AddStringToObject (result, "gpgme", gpgme_check_version (NULL))) { cJSON_Delete (infos); return gpg_error_from_syserror (); } if ((err = gpgme_get_engine_info (&ei))) { cJSON_Delete (infos); return err; } for (; ei; ei = ei->next) cJSON_AddItemToArray (infos, engine_info_to_json (ei)); if (!cJSON_AddItemToObject (result, "info", infos)) { err = gpg_error_from_syserror (); cJSON_Delete (infos); return err; } return 0; } static const char hlp_keylist[] = "op: \"keylist\"\n" "\n" "Optional parameters:\n" "keys: Array of strings or fingerprints to lookup\n" " For a single key a String may be used instead of an array.\n" " default lists all keys.\n" "protocol: Either \"openpgp\" (default) or \"cms\".\n" "\n" "Optional boolean flags (default is false):\n" "secret: List only secret keys.\n" "with-secret: Add KEYLIST_MODE_WITH_SECRET.\n" "extern: Add KEYLIST_MODE_EXTERN.\n" "local: Add KEYLIST_MODE_LOCAL. (default mode).\n" "sigs: Add KEYLIST_MODE_SIGS.\n" "notations: Add KEYLIST_MODE_SIG_NOTATIONS.\n" "tofu: Add KEYLIST_MODE_WITH_TOFU.\n" "ephemeral: Add KEYLIST_MODE_EPHEMERAL.\n" "validate: Add KEYLIST_MODE_VALIDATE.\n" "locate: Add KEYLIST_MODE_LOCATE.\n" "\n" "Response on success:\n" "keys: Array of keys.\n" " Boolean values:\n" " revoked\n" " expired\n" " disabled\n" " invalid\n" " can_encrypt\n" " can_sign\n" " can_certify\n" " can_authenticate\n" " secret\n" " is_qualified\n" " String values:\n" " protocol\n" " issuer_serial (CMS Only)\n" " issuer_name (CMS Only)\n" " chain_id (CMS Only)\n" " owner_trust (OpenPGP only)\n" " fingerprint\n" " Number values:\n" " last_update\n" " origin\n" " Array values:\n" " subkeys\n" " Boolean values:\n" " revoked\n" " expired\n" " disabled\n" " invalid\n" " can_encrypt\n" " can_sign\n" " can_certify\n" " can_authenticate\n" " secret\n" " is_qualified\n" " is_cardkey\n" " is_de_vs\n" " String values:\n" " pubkey_algo_name\n" " pubkey_algo_string\n" " keyid\n" " card_number\n" " curve\n" " keygrip\n" " Number values:\n" " pubkey_algo\n" " length\n" " timestamp\n" " expires\n" " userids\n" " Boolean values:\n" " revoked\n" " invalid\n" " String values:\n" " validity\n" " uid\n" " name\n" " email\n" " comment\n" " address\n" " Number values:\n" " origin\n" " last_update\n" " Array values:\n" " signatures\n" " Boolean values:\n" " revoked\n" " expired\n" " invalid\n" " exportable\n" " String values:\n" " pubkey_algo_name\n" " keyid\n" " status\n" " uid\n" " name\n" " email\n" " comment\n" " Number values:\n" " pubkey_algo\n" " timestamp\n" " expires\n" " status_code\n" " sig_class\n" " Array values:\n" " notations\n" " Boolean values:\n" " human_readable\n" " critical\n" " String values:\n" " name\n" " value\n" " Number values:\n" " flags\n" " tofu\n" " String values:\n" " description\n" " Number values:\n" " validity\n" " policy\n" " signcount\n" " encrcount\n" " signfirst\n" " signlast\n" " encrfirst\n" " encrlast\n"; static gpg_error_t op_keylist (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_protocol_t protocol; char **patterns = NULL; int abool; int secret_only = 0; gpgme_keylist_mode_t mode = 0; gpgme_key_t key = NULL; cjson_t keyarray = xjson_CreateArray (); if ((err = get_protocol (request, &protocol))) goto leave; ctx = get_context (protocol); /* Handle the various keylist mode bools. */ if ((err = get_boolean_flag (request, "secret", 0, &abool))) goto leave; if (abool) { mode |= GPGME_KEYLIST_MODE_WITH_SECRET; secret_only = 1; } if ((err = get_boolean_flag (request, "with-secret", 0, &abool))) goto leave; if (abool) mode |= GPGME_KEYLIST_MODE_WITH_SECRET; if ((err = get_boolean_flag (request, "extern", 0, &abool))) goto leave; if (abool) mode |= GPGME_KEYLIST_MODE_EXTERN; if ((err = get_boolean_flag (request, "local", 0, &abool))) goto leave; if (abool) mode |= GPGME_KEYLIST_MODE_LOCAL; if ((err = get_boolean_flag (request, "sigs", 0, &abool))) goto leave; if (abool) mode |= GPGME_KEYLIST_MODE_SIGS; if ((err = get_boolean_flag (request, "notations", 0, &abool))) goto leave; if (abool) mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS; if ((err = get_boolean_flag (request, "tofu", 0, &abool))) goto leave; if (abool) mode |= GPGME_KEYLIST_MODE_WITH_TOFU; if ((err = get_boolean_flag (request, "ephemeral", 0, &abool))) goto leave; if (abool) mode |= GPGME_KEYLIST_MODE_EPHEMERAL; if ((err = get_boolean_flag (request, "validate", 0, &abool))) goto leave; if (abool) mode |= GPGME_KEYLIST_MODE_VALIDATE; if ((err = get_boolean_flag (request, "locate", 0, &abool))) goto leave; if (abool) mode |= GPGME_KEYLIST_MODE_LOCATE; if (!mode) { /* default to local */ mode = GPGME_KEYLIST_MODE_LOCAL; } /* Get the keys. */ patterns = create_keylist_patterns (request, "keys"); /* Do a keylisting and add the keys */ gpgme_set_keylist_mode (ctx, mode); err = gpgme_op_keylist_ext_start (ctx, (const char **) patterns, secret_only, 0); if (err) { gpg_error_object (result, err, "Error listing keys: %s", gpg_strerror (err)); goto leave; } while (!(err = gpgme_op_keylist_next (ctx, &key))) { cJSON_AddItemToArray (keyarray, key_to_json (key)); gpgme_key_unref (key); } err = 0; if (!cJSON_AddItemToObject (result, "keys", keyarray)) { err = gpg_error_from_syserror (); goto leave; } leave: xfree_array (patterns); if (err) { cJSON_Delete (keyarray); } return err; } static const char hlp_import[] = "op: \"import\"\n" "data: The data to import.\n" "\n" "Optional parameters:\n" "protocol: Either \"openpgp\" (default) or \"cms\".\n" "\n" "Optional boolean flags (default is false):\n" "base64: Input data is base64 encoded.\n" "\n" "Response on success:\n" "result: The import result.\n" " Number values:\n" " considered\n" " no_user_id\n" " imported\n" " imported_rsa\n" " unchanged\n" " new_user_ids\n" " new_sub_keys\n" " new_signatures\n" " new_revocations\n" " secret_read\n" " secret_imported\n" " secret_unchanged\n" " skipped_new_keys\n" " not_imported\n" " skipped_v3_keys\n" " Array values:\n" " imports: List of keys for which an import was attempted\n" " String values:\n" " fingerprint\n" " error_string\n" " Number values:\n" " error_code\n" " status\n"; static gpg_error_t op_import (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_data_t input = NULL; gpgme_import_result_t import_result; gpgme_protocol_t protocol; if ((err = get_protocol (request, &protocol))) goto leave; ctx = get_context (protocol); if ((err = get_string_data (request, result, "data", &input))) goto leave; /* Import. */ err = gpgme_op_import (ctx, input); import_result = gpgme_op_import_result (ctx); if (err) { gpg_error_object (result, err, "Import failed: %s", gpg_strerror (err)); goto leave; } gpgme_data_release (input); input = NULL; xjson_AddItemToObject (result, "result", import_result_to_json (import_result)); leave: release_context (ctx); gpgme_data_release (input); return err; } static const char hlp_export[] = "op: \"export\"\n" "\n" "Optional parameters:\n" "keys: Array of strings or fingerprints to lookup\n" " For a single key a String may be used instead of an array.\n" " default exports all keys.\n" "protocol: Either \"openpgp\" (default) or \"cms\".\n" "\n" "Optional boolean flags (default is false):\n" "armor: Request output in armored format.\n" "extern: Add EXPORT_MODE_EXTERN.\n" "minimal: Add EXPORT_MODE_MINIMAL.\n" "raw: Add EXPORT_MODE_RAW.\n" "pkcs12: Add EXPORT_MODE_PKCS12.\n" "with-sec-fprs: Add the sec-fprs array to the result.\n" "\n" "Response on success:\n" "type: \"keys\"\n" "data: Unless armor mode is used a Base64 encoded binary.\n" " In armor mode a string with an armored\n" " OpenPGP or a PEM / PKCS12 key.\n" "base64: Boolean indicating whether data is base64 encoded.\n" "sec-fprs: Optional, only if with-secret is set. An array containing\n" " the fingerprints of the keys in the export for which a secret\n" " key is available"; static gpg_error_t op_export (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_protocol_t protocol; char **patterns = NULL; int abool; int with_secret = 0; gpgme_export_mode_t mode = 0; gpgme_data_t output = NULL; if ((err = get_protocol (request, &protocol))) goto leave; ctx = get_context (protocol); if ((err = get_boolean_flag (request, "armor", 0, &abool))) goto leave; gpgme_set_armor (ctx, abool); /* Handle the various export mode bools. */ if ((err = get_boolean_flag (request, "secret", 0, &abool))) goto leave; if (abool) { err = gpg_error (GPG_ERR_FORBIDDEN); goto leave; } if ((err = get_boolean_flag (request, "extern", 0, &abool))) goto leave; if (abool) mode |= GPGME_EXPORT_MODE_EXTERN; if ((err = get_boolean_flag (request, "minimal", 0, &abool))) goto leave; if (abool) mode |= GPGME_EXPORT_MODE_MINIMAL; if ((err = get_boolean_flag (request, "raw", 0, &abool))) goto leave; if (abool) mode |= GPGME_EXPORT_MODE_RAW; if ((err = get_boolean_flag (request, "pkcs12", 0, &abool))) goto leave; if (abool) mode |= GPGME_EXPORT_MODE_PKCS12; if ((err = get_boolean_flag (request, "with-sec-fprs", 0, &abool))) goto leave; if (abool) with_secret = 1; /* Get the export patterns. */ patterns = create_keylist_patterns (request, "keys"); /* Create an output data object. */ err = gpgme_data_new (&output); if (err) { gpg_error_object (result, err, "Error creating output data object: %s", gpg_strerror (err)); goto leave; } err = gpgme_op_export_ext (ctx, (const char **) patterns, mode, output); if (err) { gpg_error_object (result, err, "Error exporting keys: %s", gpg_strerror (err)); goto leave; } /* We need to base64 if armoring has not been requested. */ err = make_data_object (result, output, "keys", !gpgme_get_armor (ctx)); output = NULL; if (!err && with_secret) { err = add_secret_fprs ((const char **) patterns, protocol, result); } leave: xfree_array (patterns); release_context (ctx); gpgme_data_release (output); return err; } static const char hlp_delete[] = "op: \"delete\"\n" "key: Fingerprint of the key to delete.\n" "\n" "Optional parameters:\n" "protocol: Either \"openpgp\" (default) or \"cms\".\n" "\n" "Response on success:\n" "success: Boolean true.\n"; static gpg_error_t op_delete (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_ctx_t keylist_ctx = NULL; gpgme_protocol_t protocol; gpgme_key_t key = NULL; int secret = 0; cjson_t j_key = NULL; if ((err = get_protocol (request, &protocol))) goto leave; ctx = get_context (protocol); keylist_ctx = get_context (protocol); if ((err = get_boolean_flag (request, "secret", 0, &secret))) goto leave; if (secret) { err = gpg_error (GPG_ERR_FORBIDDEN); goto leave; } j_key = cJSON_GetObjectItem (request, "key"); if (!j_key) { err = gpg_error (GPG_ERR_NO_KEY); goto leave; } if (!cjson_is_string (j_key)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } /* Get the key */ if ((err = gpgme_get_key (keylist_ctx, j_key->valuestring, &key, 0))) { gpg_error_object (result, err, "Error fetching key for delete: %s", gpg_strerror (err)); goto leave; } err = gpgme_op_delete (ctx, key, 0); if (err) { gpg_error_object (result, err, "Error deleting key: %s", gpg_strerror (err)); goto leave; } xjson_AddBoolToObject (result, "success", 1); leave: gpgme_key_unref (key); release_context (ctx); release_context (keylist_ctx); return err; } static const char hlp_config_opt[] = "op: \"config_opt\"\n" "component: The component of the option.\n" "option: The name of the option.\n" "\n" "Response on success:\n" "\n" "option: Information about the option.\n" " String values:\n" " name: The name of the option\n" " description: Localized description of the opt.\n" " argname: Thhe argument name e.g. --verbose\n" " default_description\n" " no_arg_description\n" " Number values:\n" " flags: Flags for this option.\n" " level: the level of the description. See gpgme_conf_level_t.\n" " type: The type of the option. See gpgme_conf_type_t.\n" " alt_type: Alternate type of the option. See gpgme_conf_type_t\n" " Arg type values: (see desc. below)\n" " default_value: Array of the default value.\n" " no_arg_value: Array of the value if it is not set.\n" " value: Array for the current value if the option is set.\n" "\n" "If the response is empty the option was not found\n" ""; static gpg_error_t op_config_opt (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_conf_comp_t conf = NULL; gpgme_conf_comp_t comp = NULL; cjson_t j_tmp; char *comp_name = NULL; char *opt_name = NULL; ctx = get_context (GPGME_PROTOCOL_GPGCONF); j_tmp = cJSON_GetObjectItem (request, "component"); if (!j_tmp || !cjson_is_string (j_tmp)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } comp_name = j_tmp->valuestring; j_tmp = cJSON_GetObjectItem (request, "option"); if (!j_tmp || !cjson_is_string (j_tmp)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } opt_name = j_tmp->valuestring; /* Load the config */ err = gpgme_op_conf_load (ctx, &conf); if (err) { goto leave; } comp = conf; for (comp = conf; comp; comp = comp->next) { gpgme_conf_opt_t opt = NULL; int found = 0; if (!comp->name || strcmp (comp->name, comp_name)) { /* Skip components if a single one is specified */ continue; } for (opt = comp->options; opt; opt = opt->next) { if (!opt->name || strcmp (opt->name, opt_name)) { /* Skip components if a single one is specified */ continue; } xjson_AddItemToObject (result, "option", conf_opt_to_json (opt)); found = 1; break; } if (found) break; } leave: gpgme_conf_release (conf); release_context (ctx); return err; } static const char hlp_config[] = "op: \"config\"\n" "\n" "Optional parameters:\n" "component: Component of entries to list.\n" " Default: all\n" "\n" "Response on success:\n" " components: Array of the component program configs.\n" " name: The component name.\n" " description: Description of the component.\n" " program_name: The absolute path to the program.\n" " options: Array of config options\n" " String values:\n" " name: The name of the option\n" " description: Localized description of the opt.\n" " argname: Thhe argument name e.g. --verbose\n" " default_description\n" " no_arg_description\n" " Number values:\n" " flags: Flags for this option.\n" " level: the level of the description. See gpgme_conf_level_t.\n" " type: The type of the option. See gpgme_conf_type_t.\n" " alt_type: Alternate type of the option. See gpgme_conf_type_t\n" " Arg type values: (see desc. below)\n" " default_value: Array of the default value.\n" " no_arg_value: Array of the value if it is not set.\n" " value: Array for the current value if the option is set.\n" "\n" "Conf type values are an array of values that are either\n" "of type number named \"number\" or of type string,\n" "named \"string\".\n" "If the type is none the bool value is_none is true.\n" ""; static gpg_error_t op_config (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; gpgme_conf_comp_t conf = NULL; gpgme_conf_comp_t comp = NULL; cjson_t j_tmp; char *comp_name = NULL; cjson_t j_comps = xjson_CreateArray (); ctx = get_context (GPGME_PROTOCOL_GPGCONF); j_tmp = cJSON_GetObjectItem (request, "component"); if (j_tmp && cjson_is_string (j_tmp)) { comp_name = j_tmp->valuestring; } else if (j_tmp && !cjson_is_string (j_tmp)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } /* Load the config */ err = gpgme_op_conf_load (ctx, &conf); if (err) { goto leave; } comp = conf; for (comp = conf; comp; comp = comp->next) { if (comp_name && comp->name && strcmp (comp->name, comp_name)) { /* Skip components if a single one is specified */ continue; } cJSON_AddItemToArray (j_comps, conf_comp_to_json (comp)); } xjson_AddItemToObject (result, "components", j_comps); leave: gpgme_conf_release (conf); release_context (ctx); return err; } static const char hlp_createkey[] = "op: \"createkey\"\n" "userid: The user id. E.g. \"Foo Bar \"\n" "\n" "Optional parameters:\n" "algo: Algo of the key as string. See doc for gpg --quick-gen-key.\n" " Supported values are \"default\" and \"future-default\".\n" "expires: Seconds from now to expiry as Number. 0 means no expiry.\n" " The default is to use a standard expiration interval.\n" "\n" "Response on success:\n" "fingerprint: The fingerprint of the created key.\n" "\n" "Note: This interface does not allow key generation if the userid\n" "of the new key already exists in the keyring.\n"; static gpg_error_t op_createkey (cjson_t request, cjson_t result) { gpg_error_t err; gpgme_ctx_t ctx = NULL; unsigned int flags = GPGME_CREATE_FORCE; /* Always force as the GUI should handle checks, if required. */ unsigned long expires = 0; cjson_t j_tmp; const char *algo = "default"; const char *userid; gpgme_genkey_result_t res; #ifdef GPG_AGENT_ALLOWS_KEYGEN_THROUGH_BROWSER /* GnuPG forbids keygen through the browser socket so for this we create an unrestricted context. See GnuPG-Bug-Id: T4010 for more info */ ctx = get_context (GPGME_PROTOCOL_OpenPGP); #else err = gpgme_new (&ctx); if (err) log_fatal ("error creating GPGME context: %s\n", gpg_strerror (err)); gpgme_set_protocol (ctx, GPGME_PROTOCOL_OpenPGP); #endif j_tmp = cJSON_GetObjectItem (request, "algo"); if (j_tmp && cjson_is_string (j_tmp)) { algo = j_tmp->valuestring; } j_tmp = cJSON_GetObjectItem (request, "userid"); if (!j_tmp || !cjson_is_string (j_tmp)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } userid = j_tmp->valuestring; j_tmp = cJSON_GetObjectItem (request, "expires"); if (j_tmp) { if (!cjson_is_number (j_tmp)) { err = gpg_error (GPG_ERR_INV_VALUE); goto leave; } expires = j_tmp->valueint; if (!expires) flags |= GPGME_CREATE_NOEXPIRE; } if ((err = gpgme_op_createkey (ctx, userid, algo, 0, expires, NULL, flags))) goto leave; res = gpgme_op_genkey_result (ctx); if (!res) { err = gpg_error (GPG_ERR_GENERAL); goto leave; } xjson_AddStringToObject0 (result, "fingerprint", res->fpr); leave: #ifdef GPG_AGENT_ALLOWS_KEYGEN_THROUGH_BROWSER release_context (ctx); #else gpgme_release (ctx); #endif return err; } static const char hlp_getmore[] = "op: \"getmore\"\n" "\n" "Response on success:\n" "response: base64 encoded json response.\n" "more: Another getmore is required.\n" "base64: boolean if the response is base64 encoded.\n"; static gpg_error_t op_getmore (cjson_t request, cjson_t result) { gpg_error_t err; int c; size_t n; size_t chunksize; if ((err = get_chunksize (request, &chunksize))) goto leave; /* For the meta data we need 41 bytes: {"more":true,"base64":true,"response":""} */ chunksize -= 41; /* Adjust the chunksize for the base64 conversion. */ chunksize = (chunksize / 4) * 3; /* Do we have anything pending? */ if (!pending_data.buffer) { err = gpg_error (GPG_ERR_NO_DATA); gpg_error_object (result, err, "Operation not possible: %s", gpg_strerror (err)); goto leave; } /* We currently always use base64 encoding for simplicity. */ xjson_AddBoolToObject (result, "base64", 1); if (pending_data.written >= pending_data.length) { /* EOF reached. This should not happen but we return an empty * string once in case of client errors. */ gpgme_free (pending_data.buffer); pending_data.buffer = NULL; xjson_AddBoolToObject (result, "more", 0); err = cjson_AddStringToObject (result, "response", ""); } else { n = pending_data.length - pending_data.written; if (n > chunksize) { n = chunksize; xjson_AddBoolToObject (result, "more", 1); } else xjson_AddBoolToObject (result, "more", 0); c = pending_data.buffer[pending_data.written + n]; pending_data.buffer[pending_data.written + n] = 0; err = add_base64_to_object (result, "response", (pending_data.buffer + pending_data.written), n); pending_data.buffer[pending_data.written + n] = c; if (!err) { pending_data.written += n; if (pending_data.written >= pending_data.length) { xfree (pending_data.buffer); pending_data.buffer = NULL; } } } leave: return err; } static const char hlp_help[] = "The tool expects a JSON object with the request and responds with\n" "another JSON object. Even on error a JSON object is returned. The\n" "property \"op\" is mandatory and its string value selects the\n" "operation; if the property \"help\" with the value \"true\" exists, the\n" "operation is not performned but a string with the documentation\n" "returned. To list all operations it is allowed to leave out \"op\" in\n" "help mode. Supported values for \"op\" are:\n\n" " config Read configuration values.\n" " config_opt Read a single configuration value.\n" " decrypt Decrypt data.\n" " delete Delete a key.\n" " encrypt Encrypt data.\n" " export Export keys.\n" " createkey Generate a keypair (OpenPGP only).\n" " import Import data.\n" " keylist List keys.\n" " sign Sign data.\n" " verify Verify data.\n" " version Get engine information.\n" " getmore Retrieve remaining data if chunksize was used.\n" " help Help overview.\n" "\n" "If the data needs to be transferred in smaller chunks the\n" "property \"chunksize\" with an integer value can be added.\n" "When \"chunksize\" is set the response (including json) will\n" "not be larger then \"chunksize\" but might be smaller.\n" "The chunked result will be transferred in base64 encoded chunks\n" "using the \"getmore\" operation. See help getmore for more info."; static gpg_error_t op_help (cjson_t request, cjson_t result) { cjson_t j_tmp; char *buffer = NULL; const char *msg; j_tmp = cJSON_GetObjectItem (request, "interactive_help"); if (opt_interactive && j_tmp && cjson_is_string (j_tmp)) msg = buffer = xstrconcat (hlp_help, "\n", j_tmp->valuestring, NULL); else msg = hlp_help; xjson_AddStringToObject (result, "type", "help"); xjson_AddStringToObject (result, "msg", msg); xfree (buffer); return 0; } /* * Dispatcher */ /* Process a request and return the response. The response is a newly * allocated string or NULL in case of an error. */ static char * process_request (const char *request) { static struct { const char *op; gpg_error_t (*handler)(cjson_t request, cjson_t result); const char * const helpstr; } optbl[] = { { "config", op_config, hlp_config }, { "config_opt", op_config_opt, hlp_config_opt }, { "encrypt", op_encrypt, hlp_encrypt }, { "export", op_export, hlp_export }, { "decrypt", op_decrypt, hlp_decrypt }, { "delete", op_delete, hlp_delete }, { "createkey", op_createkey, hlp_createkey }, { "keylist", op_keylist, hlp_keylist }, { "import", op_import, hlp_import }, { "sign", op_sign, hlp_sign }, { "verify", op_verify, hlp_verify }, { "version", op_version, hlp_version }, { "getmore", op_getmore, hlp_getmore }, { "help", op_help, hlp_help }, { NULL } }; size_t erroff; cjson_t json; cjson_t j_tmp, j_op; cjson_t response; int helpmode; int is_getmore = 0; const char *op; char *res = NULL; int idx; response = xjson_CreateObject (); json = cJSON_Parse (request, &erroff); if (!json) { log_string (GPGRT_LOGLVL_INFO, request); log_info ("invalid JSON object at offset %zu\n", erroff); error_object (response, "invalid JSON object at offset %zu\n", erroff); goto leave; } j_tmp = cJSON_GetObjectItem (json, "help"); helpmode = (j_tmp && cjson_is_true (j_tmp)); j_op = cJSON_GetObjectItem (json, "op"); if (!j_op || !cjson_is_string (j_op)) { if (!helpmode) { error_object (response, "Property \"op\" missing"); goto leave; } op = "help"; /* Help summary. */ } else op = j_op->valuestring; for (idx=0; optbl[idx].op; idx++) if (!strcmp (op, optbl[idx].op)) break; if (optbl[idx].op) { if (helpmode && strcmp (op, "help")) { xjson_AddStringToObject (response, "type", "help"); xjson_AddStringToObject (response, "op", op); xjson_AddStringToObject (response, "msg", optbl[idx].helpstr); } else { gpg_error_t err; is_getmore = optbl[idx].handler == op_getmore; /* If this is not the "getmore" command and we have any * pending data release that data. */ if (pending_data.buffer && optbl[idx].handler != op_getmore) { gpgme_free (pending_data.buffer); pending_data.buffer = NULL; } err = optbl[idx].handler (json, response); if (err) { if (!(j_tmp = cJSON_GetObjectItem (response, "type")) || !cjson_is_string (j_tmp) || strcmp (j_tmp->valuestring, "error")) { /* No error type response - provide a generic one. */ gpg_error_object (response, err, "Operation failed: %s", gpg_strerror (err)); } xjson_AddStringToObject (response, "op", op); } } } else /* Operation not supported. */ { error_object (response, "Unknown operation '%s'", op); xjson_AddStringToObject (response, "op", op); } leave: if (is_getmore) { /* For getmore we bypass the encode_and_chunk. */ if (opt_interactive) res = cJSON_Print (response); else res = cJSON_PrintUnformatted (response); } else res = encode_and_chunk (json, response); if (!res) { cjson_t err_obj; log_error ("printing JSON data failed\n"); err_obj = error_object (NULL, "Printing JSON data failed"); if (opt_interactive) res = cJSON_Print (err_obj); res = cJSON_PrintUnformatted (err_obj); cJSON_Delete (err_obj); } cJSON_Delete (json); cJSON_Delete (response); if (!res) { /* Can't happen unless we created a broken error_object above */ return xtrystrdup ("Bug: Fatal error in process request\n"); } return res; } /* * Driver code */ static char * get_file (const char *fname) { gpg_error_t err; estream_t fp; struct stat st; char *buf; size_t buflen; fp = es_fopen (fname, "r"); if (!fp) { err = gpg_error_from_syserror (); log_error ("can't open '%s': %s\n", fname, gpg_strerror (err)); return NULL; } if (fstat (es_fileno(fp), &st)) { err = gpg_error_from_syserror (); log_error ("can't stat '%s': %s\n", fname, gpg_strerror (err)); es_fclose (fp); return NULL; } buflen = st.st_size; buf = xmalloc (buflen+1); if (es_fread (buf, buflen, 1, fp) != 1) { err = gpg_error_from_syserror (); log_error ("error reading '%s': %s\n", fname, gpg_strerror (err)); es_fclose (fp); xfree (buf); return NULL; } buf[buflen] = 0; es_fclose (fp); return buf; } /* Return a malloced line or NULL on EOF. Terminate on read * error. */ static char * get_line (void) { char *line = NULL; size_t linesize = 0; gpg_error_t err; size_t maxlength = 2048; int n; const char *s; char *p; again: n = es_read_line (es_stdin, &line, &linesize, &maxlength); if (n < 0) { err = gpg_error_from_syserror (); log_error ("error reading line: %s\n", gpg_strerror (err)); exit (1); } if (!n) { xfree (line); line = NULL; return NULL; /* EOF */ } if (!maxlength) { log_info ("line too long - skipped\n"); goto again; } if (memchr (line, 0, n)) log_info ("warning: line shortened due to embedded Nul character\n"); if (line[n-1] == '\n') line[n-1] = 0; /* Trim leading spaces. */ for (s=line; spacep (s); s++) ; if (s != line) { for (p=line; *s;) *p++ = *s++; *p = 0; n = p - line; } return line; } /* Process meta commands used with the standard REPL. */ static char * process_meta_commands (const char *request) { char *result = NULL; while (spacep (request)) request++; if (!strncmp (request, "help", 4) && (spacep (request+4) || !request[4])) { if (request[4]) { char *buf = xstrconcat ("{ \"help\":true, \"op\":\"", request+5, "\" }", NULL); result = process_request (buf); xfree (buf); } else result = process_request ("{ \"op\": \"help\"," " \"interactive_help\": " "\"\\nMeta commands:\\n" " ,read FNAME Process data from FILE\\n" " ,help CMD Print help for a command\\n" " ,quit Terminate process\"" "}"); } else if (!strncmp (request, "quit", 4) && (spacep (request+4) || !request[4])) exit (0); else if (!strncmp (request, "read", 4) && (spacep (request+4) || !request[4])) { if (!request[4]) log_info ("usage: ,read FILENAME\n"); else { char *buffer = get_file (request + 5); if (buffer) { result = process_request (buffer); xfree (buffer); } } } else log_info ("invalid meta command\n"); return result; } /* If STRING has a help response, return the MSG property in a human * readable format. */ static char * get_help_msg (const char *string) { cjson_t json, j_type, j_msg; const char *msg; char *buffer = NULL; char *p; json = cJSON_Parse (string, NULL); if (json) { j_type = cJSON_GetObjectItem (json, "type"); if (j_type && cjson_is_string (j_type) && !strcmp (j_type->valuestring, "help")) { j_msg = cJSON_GetObjectItem (json, "msg"); if (j_msg || cjson_is_string (j_msg)) { msg = j_msg->valuestring; buffer = malloc (strlen (msg)+1); if (buffer) { for (p=buffer; *msg; msg++) { if (*msg == '\\' && msg[1] == '\n') *p++ = '\n'; else *p++ = *msg; } *p = 0; } } } cJSON_Delete (json); } return buffer; } /* An interactive standard REPL. */ static void interactive_repl (void) { char *line = NULL; char *request = NULL; char *response = NULL; char *p; int first; es_setvbuf (es_stdin, NULL, _IONBF, 0); #if GPGRT_VERSION_NUMBER >= 0x011d00 /* 1.29 */ es_fprintf (es_stderr, "%s %s ready (enter \",help\" for help)\n", gpgrt_strusage (11), gpgrt_strusage (13)); #endif do { es_fputs ("> ", es_stderr); es_fflush (es_stderr); es_fflush (es_stdout); xfree (line); line = get_line (); es_fflush (es_stderr); es_fflush (es_stdout); first = !request; if (line && *line) { if (!request) request = xstrdup (line); else request = xstrconcat (request, "\n", line, NULL); } if (!line) es_fputs ("\n", es_stderr); if (!line || !*line || (first && *request == ',')) { /* Process the input. */ xfree (response); response = NULL; if (request && *request == ',') { response = process_meta_commands (request+1); } else if (request) { response = process_request (request); } xfree (request); request = NULL; if (response) { if (opt_interactive) { char *msg = get_help_msg (response); if (msg) { xfree (response); response = msg; } } es_fputs ("===> ", es_stderr); es_fflush (es_stderr); for (p=response; *p; p++) { if (*p == '\n') { es_fflush (es_stdout); es_fputs ("\n===> ", es_stderr); es_fflush (es_stderr); } else es_putc (*p, es_stdout); } es_fflush (es_stdout); es_fputs ("\n", es_stderr); } } } while (line); xfree (request); xfree (response); xfree (line); } /* Read and process a single request. */ static void read_and_process_single_request (void) { char *line = NULL; char *request = NULL; char *response = NULL; size_t n; for (;;) { xfree (line); line = get_line (); if (line && *line) request = (request? xstrconcat (request, "\n", line, NULL) /**/ : xstrdup (line)); if (!line) { if (request) { xfree (response); response = process_request (request); if (response) { es_fputs (response, es_stdout); if ((n = strlen (response)) && response[n-1] != '\n') es_fputc ('\n', es_stdout); } es_fflush (es_stdout); } break; } } xfree (response); xfree (request); xfree (line); } /* The Native Messaging processing loop. */ static void native_messaging_repl (void) { gpg_error_t err; uint32_t nrequest, nresponse; char *request = NULL; char *response = NULL; size_t n; /* Due to the length octets we need to switch the I/O stream into * binary mode. */ es_set_binary (es_stdin); es_set_binary (es_stdout); es_setbuf (es_stdin, NULL); /* stdin needs to be unbuffered! */ for (;;) { /* Read length. Note that the protocol uses native endianness. * Is it allowed to call such a thing a well thought out * protocol? */ if (es_read (es_stdin, &nrequest, sizeof nrequest, &n)) { err = gpg_error_from_syserror (); log_error ("error reading request header: %s\n", gpg_strerror (err)); break; } if (!n) break; /* EOF */ if (n != sizeof nrequest) { log_error ("error reading request header: short read\n"); break; } if (nrequest > MAX_REQUEST_SIZE) { log_error ("error reading request: request too long (%zu MiB)\n", (size_t)nrequest / (1024*1024)); /* Fixme: Shall we read the request to the bit bucket and * return an error response or just return an error response * and terminate? Needs some testing. */ break; } /* Read request. */ request = xtrymalloc (nrequest + 1); if (!request) { err = gpg_error_from_syserror (); log_error ("error reading request: Not enough memory for %zu MiB)\n", (size_t)nrequest / (1024*1024)); /* FIXME: See comment above. */ break; } if (es_read (es_stdin, request, nrequest, &n)) { err = gpg_error_from_syserror (); log_error ("error reading request: %s\n", gpg_strerror (err)); break; } if (n != nrequest) { /* That is a protocol violation. */ xfree (response); response = error_object_string ("Invalid request:" " short read (%zu of %zu bytes)\n", n, (size_t)nrequest); } else /* Process request */ { request[n] = '\0'; /* Ensure that request has an end */ if (opt_debug) log_debug ("request='%s'\n", request); xfree (response); response = process_request (request); if (opt_debug) log_debug ("response='%s'\n", response); } nresponse = strlen (response); /* Write response */ if (es_write (es_stdout, &nresponse, sizeof nresponse, &n)) { err = gpg_error_from_syserror (); log_error ("error writing request header: %s\n", gpg_strerror (err)); break; } if (n != sizeof nrequest) { log_error ("error writing request header: short write\n"); break; } if (es_write (es_stdout, response, nresponse, &n)) { err = gpg_error_from_syserror (); log_error ("error writing request: %s\n", gpg_strerror (err)); break; } if (n != nresponse) { log_error ("error writing request: short write\n"); break; } if (es_fflush (es_stdout) || es_ferror (es_stdout)) { err = gpg_error_from_syserror (); log_error ("error writing request: %s\n", gpg_strerror (err)); break; } xfree (response); response = NULL; xfree (request); request = NULL; } xfree (response); xfree (request); } static const char * my_strusage( int level ) { const char *p; switch (level) { case 9: p = "LGPL-2.1-or-later"; break; case 11: p = "gpgme-json"; break; case 13: p = PACKAGE_VERSION; break; case 14: p = "Copyright (C) 2018 g10 Code GmbH"; break; case 19: p = "Please report bugs to <" PACKAGE_BUGREPORT ">.\n"; break; case 1: case 40: p = "Usage: gpgme-json [OPTIONS]"; break; case 41: p = "Native messaging based GPGME operations.\n"; break; case 42: p = "1"; /* Flag print 40 as part of 41. */ break; default: p = NULL; break; } return p; } int main (int argc, char *argv[]) { #if GPGRT_VERSION_NUMBER < 0x011d00 /* 1.29 */ fprintf (stderr, "WARNING: Old libgpg-error - using limited mode\n"); native_messaging_repl (); #else /* This is a modern libgp-error. */ enum { CMD_DEFAULT = 0, CMD_INTERACTIVE = 'i', CMD_SINGLE = 's', CMD_LIBVERSION = 501, } cmd = CMD_DEFAULT; enum { OPT_DEBUG = 600 }; static gpgrt_opt_t opts[] = { ARGPARSE_c (CMD_INTERACTIVE, "interactive", "Interactive REPL"), ARGPARSE_c (CMD_SINGLE, "single", "Single request mode"), ARGPARSE_c (CMD_LIBVERSION, "lib-version", "Show library version"), ARGPARSE_s_n(OPT_DEBUG, "debug", "Flyswatter"), ARGPARSE_end() }; gpgrt_argparse_t pargs = { &argc, &argv}; int log_file_set = 0; gpgrt_set_strusage (my_strusage); #ifdef HAVE_SETLOCALE setlocale (LC_ALL, ""); #endif gpgme_check_version (NULL); #ifdef LC_CTYPE gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); #endif #ifdef LC_MESSAGES gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); #endif while (gpgrt_argparse (NULL, &pargs, opts)) { switch (pargs.r_opt) { case CMD_INTERACTIVE: opt_interactive = 1; /* Fall trough. */ case CMD_SINGLE: case CMD_LIBVERSION: cmd = pargs.r_opt; break; case OPT_DEBUG: opt_debug = 1; break; default: pargs.err = ARGPARSE_PRINT_WARNING; break; } } gpgrt_argparse (NULL, &pargs, NULL); if (!opt_debug) { /* Handling is similar to GPGME_DEBUG */ const char *s = getenv ("GPGME_JSON_DEBUG"); const char *s1; if (s && atoi (s) > 0) { opt_debug = 1; s1 = strchr (s, PATHSEP_C); if (s1 && strlen (s1) > 2) { s1++; log_set_file (s1); log_file_set = 1; } } } if (opt_debug && !log_file_set) { const char *home = getenv ("HOME"); char *file = xstrconcat ("socket://", home? home:"/tmp", "/.gnupg/S.gpgme-json.log", NULL); log_set_file (file); xfree (file); } if (opt_debug) { int i; for (i=0; argv[i]; i++) log_debug ("argv[%d]='%s'\n", i, argv[i]); } switch (cmd) { case CMD_DEFAULT: native_messaging_repl (); break; case CMD_SINGLE: read_and_process_single_request (); break; case CMD_INTERACTIVE: interactive_repl (); break; case CMD_LIBVERSION: printf ("Version from header: %s (0x%06x)\n", GPGME_VERSION, GPGME_VERSION_NUMBER); printf ("Version from binary: %s\n", gpgme_check_version (NULL)); printf ("Copyright blurb ...:%s\n", gpgme_check_version ("\x01\x01")); break; } if (opt_debug) log_debug ("ready"); #endif /* This is a modern libgp-error. */ return 0; } #endif /* libgpg-error >= 1.28 */ diff --git a/src/gpgme-tool.c b/src/gpgme-tool.c index 5c34b0b1..7a0bfcb3 100644 --- a/src/gpgme-tool.c +++ b/src/gpgme-tool.c @@ -1,3439 +1,3440 @@ /* gpgme-tool.c - Assuan server exposing GnuPG Made Easy operations. - Copyright (C) 2009, 2010, 2012, 2013 g10 Code GmbH - Copyright (C) 2001, 2003, 2009, 2011 Free Software Foundation, Inc. - - This file is part of GPGME. - - GPGME 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 3 of the License, or - (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2009, 2010, 2012, 2013 g10 Code GmbH + * Copyright (C) 2001, 2003, 2009, 2011 Free Software Foundation, Inc. + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute this file and/or modify it + * under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + * SPDX-License-Identifier: GPL-3.0-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #ifdef HAVE_LOCALE_H #include #endif #include #include "argparse.h" #include "gpgme.h" /* GCC attributes. */ #if __GNUC__ >= 4 # define GT_GCC_A_SENTINEL(a) __attribute__ ((sentinel(a))) #else # define GT_GCC_A_SENTINEL(a) #endif #if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 5 ) # define GT_GCC_A_PRINTF(f, a) __attribute__ ((format (printf,f,a))) #else # define GT_GCC_A_PRINTF(f, a) #endif #define DIM(v) (sizeof(v)/sizeof((v)[0])) #define xtoi_1(p) (*(p) <= '9'? (*(p)- '0'): \ *(p) <= 'F'? (*(p)-'A'+10):(*(p)-'a'+10)) #define xtoi_2(p) ((xtoi_1(p) * 16) + xtoi_1((p)+1)) /* MEMBUF */ /* A simple implementation of a dynamic buffer. Use init_membuf() to create a buffer, put_membuf to append bytes and get_membuf to release and return the buffer. Allocation errors are detected but only returned at the final get_membuf(), this helps not to clutter the code with out-of-core checks. */ /* The definition of the structure is private, we only need it here, so it can be allocated on the stack. */ struct private_membuf_s { size_t len; size_t size; char *buf; int out_of_core; }; typedef struct private_membuf_s membuf_t; /* Return the current length of the membuf. */ #define get_membuf_len(a) ((a)->len) #define is_membuf_ready(a) ((a)->buf || (a)->out_of_core) #define MEMBUF_ZERO { 0, 0, NULL, 0} static void init_membuf (membuf_t *mb, int initiallen) { mb->len = 0; mb->size = initiallen; mb->out_of_core = 0; mb->buf = malloc (initiallen); if (!mb->buf) mb->out_of_core = errno; } /* Shift the the content of the membuf MB by AMOUNT bytes. The next operation will then behave as if AMOUNT bytes had not been put into the buffer. If AMOUNT is greater than the actual accumulated bytes, the membuf is basically reset to its initial state. */ #if 0 /* Not yet used. */ static void clear_membuf (membuf_t *mb, size_t amount) { /* No need to clear if we are already out of core. */ if (mb->out_of_core) return; if (amount >= mb->len) mb->len = 0; else { mb->len -= amount; memmove (mb->buf, mb->buf+amount, mb->len); } } #endif /* unused */ static void put_membuf (membuf_t *mb, const void *buf, size_t len) { if (mb->out_of_core || !len) return; if (mb->len + len >= mb->size) { char *p; mb->size += len + 1024; p = realloc (mb->buf, mb->size); if (!p) { mb->out_of_core = errno ? errno : ENOMEM; return; } mb->buf = p; } memcpy (mb->buf + mb->len, buf, len); mb->len += len; } #if 0 /* Not yet used. */ static void put_membuf_str (membuf_t *mb, const char *string) { put_membuf (mb, string, strlen (string)); } #endif /* unused */ static void * get_membuf (membuf_t *mb, size_t *len) { char *p; if (mb->out_of_core) { if (mb->buf) { free (mb->buf); mb->buf = NULL; } gpg_err_set_errno (mb->out_of_core); return NULL; } p = mb->buf; if (len) *len = mb->len; mb->buf = NULL; mb->out_of_core = ENOMEM; /* hack to make sure it won't get reused. */ return p; } /* Peek at the membuf MB. On success a pointer to the buffer is returned which is valid until the next operation on MB. If LEN is not NULL the current LEN of the buffer is stored there. On error NULL is returned and ERRNO is set. */ #if 0 /* Not yet used. */ static const void * peek_membuf (membuf_t *mb, size_t *len) { const char *p; if (mb->out_of_core) { gpg_err_set_errno (mb->out_of_core); return NULL; } p = mb->buf; if (len) *len = mb->len; return p; } #endif /* unused */ /* SUPPORT. */ FILE *log_stream; char program_name[] = "gpgme-tool"; #define spacep(p) (*(p) == ' ' || *(p) == '\t') void log_error (int status, gpg_error_t errnum, const char *fmt, ...) GT_GCC_A_PRINTF(3,4); void log_init (void) { log_stream = stderr; } void log_error (int status, gpg_error_t errnum, const char *fmt, ...) { va_list ap; fprintf (log_stream, "%s: ", program_name); va_start (ap, fmt); vfprintf (log_stream, fmt, ap); va_end (ap); if (errnum) { fprintf (log_stream, ": %s", gpg_strerror (errnum)); if (gpg_err_source (errnum) != GPG_ERR_SOURCE_GPGME) fprintf (log_stream, " <%s>", gpg_strsource (errnum)); } fprintf (log_stream, "\n"); if (status) exit (status); } /* Note that it is sufficient to allocate the target string D as long as the source string S, i.e.: strlen(s)+1;. D == S is allowed. */ static void strcpy_escaped_plus (char *d, const char *s) { while (*s) { if (*s == '%' && s[1] && s[2]) { s++; *d++ = xtoi_2 (s); s += 2; } else if (*s == '+') *d++ = ' ', s++; else *d++ = *s++; } *d = 0; } /* Check whether the option NAME appears in LINE. */ static int has_option (const char *line, const char *name) { const char *s; int n = strlen (name); s = strstr (line, name); return (s && (s == line || spacep (s-1)) && (!s[n] || spacep (s+n))); } /* Skip over options. It is assumed that leading spaces have been removed (this is the case for lines passed to a handler from assuan). Blanks after the options are also removed. */ static char * skip_options (char *line) { while ( *line == '-' && line[1] == '-' ) { while (*line && !spacep (line)) line++; while (spacep (line)) line++; } return line; } typedef gpg_error_t (*result_xml_write_cb_t) (void *hook, const void *buf, size_t len); static char xml_preamble1[] = "\n"; static const char xml_preamble2[] = "\n"; static const char xml_end[] = "\n"; struct result_xml_state { int indent; result_xml_write_cb_t cb; void *hook; #define MAX_TAGS 20 int next_tag; const char *tag[MAX_TAGS]; int had_data[MAX_TAGS]; }; void result_init (struct result_xml_state *state, int indent, result_xml_write_cb_t cb, void *hook) { memset (state, '\0', sizeof (*state)); state->indent = indent; state->cb = cb; state->hook = hook; } gpg_error_t result_xml_indent (struct result_xml_state *state) { char spaces[state->indent + 1]; int i; for (i = 0; i < state->indent; i++) spaces[i] = ' '; spaces[i] = '\0'; return (*state->cb) (state->hook, spaces, i); } gpg_error_t result_xml_tag_start (struct result_xml_state *state, const char *name, ...) { result_xml_write_cb_t cb = state->cb; void *hook = state->hook; va_list ap; char *attr; char *attr_val; char string_null[] = "(null)"; va_start (ap, name); if (state->next_tag > 0) { if (! state->had_data[state->next_tag - 1]) { (*cb) (hook, ">\n", 2); (*cb) (hook, NULL, 0); } state->had_data[state->next_tag - 1] = 1; } result_xml_indent (state); (*cb) (hook, "<", 1); (*cb) (hook, name, strlen (name)); state->tag[state->next_tag] = name; state->had_data[state->next_tag] = 0; state->indent += 2; state->next_tag++; while (1) { attr = va_arg (ap, char *); if (attr == NULL) break; attr_val = va_arg (ap, char *); if (attr_val == NULL) attr_val = string_null; (*cb) (hook, " ", 1); (*cb) (hook, attr, strlen (attr)); (*cb) (hook, "=\"", 2); (*cb) (hook, attr_val, strlen (attr_val)); (*cb) (hook, "\"", 1); } va_end (ap); return 0; } /* Return a constant string with an XML entity for C. */ static const char * result_xml_escape_replacement(char c) { switch (c) { case '<': return "<"; case '>': return ">"; case '&': return "&"; default: return NULL; } } /* Escape DATA by replacing certain characters with their XML entities. The result is stored in a newly allocated buffer which address will be stored at BUF. Returns 0 on success. */ static gpg_error_t result_xml_escape (const char *data, char **buf) { int data_len, i; const char *r; membuf_t mb; init_membuf (&mb, 128); if (data) { data_len = strlen (data); for (i = 0; i < data_len; i++) { r = result_xml_escape_replacement (data[i]); if (r) put_membuf (&mb, r, strlen (r)); else put_membuf (&mb, data+i, 1); } } put_membuf (&mb, "", 1); *buf = get_membuf (&mb, NULL); return *buf? 0 : gpg_error_from_syserror (); } gpg_error_t result_xml_tag_data (struct result_xml_state *state, const char *data) { gpg_error_t err; result_xml_write_cb_t cb = state->cb; void *hook = state->hook; char *buf = NULL; if (state->had_data[state->next_tag - 1]) { (*cb) (hook, "\n", 2); (*cb) (hook, NULL, 0); result_xml_indent (state); } else (*cb) (hook, ">", 1); state->had_data[state->next_tag - 1] = 2; err = result_xml_escape (data, &buf); if (err) return err; (*cb) (hook, buf, strlen (buf)); free (buf); return 0; } gpg_error_t result_xml_tag_end (struct result_xml_state *state) { result_xml_write_cb_t cb = state->cb; void *hook = state->hook; state->next_tag--; state->indent -= 2; if (state->had_data[state->next_tag]) { if (state->had_data[state->next_tag] == 1) result_xml_indent (state); (*cb) (hook, "tag[state->next_tag], strlen (state->tag[state->next_tag])); (*cb) (hook, ">\n", 2); (*cb) (hook, NULL, 0); } else { (*cb) (hook, " />\n", 4); (*cb) (hook, NULL, 0); } return 0; } gpg_error_t result_add_error (struct result_xml_state *state, const char *name, gpg_error_t err) { char code[20]; char msg[1024]; snprintf (code, sizeof (code) - 1, "0x%x", err); snprintf (msg, sizeof (msg) - 1, "%s <%s>", gpg_strerror (err), gpg_strsource (err)); result_xml_tag_start (state, name, "value", code, NULL); result_xml_tag_data (state, msg); result_xml_tag_end (state); return 0; } gpg_error_t result_add_pubkey_algo (struct result_xml_state *state, const char *name, gpgme_pubkey_algo_t algo) { char code[20]; char msg[80]; snprintf (code, sizeof (code) - 1, "0x%x", algo); snprintf (msg, sizeof (msg) - 1, "%s", gpgme_pubkey_algo_name (algo)); result_xml_tag_start (state, name, "value", code, NULL); result_xml_tag_data (state, msg); result_xml_tag_end (state); return 0; } gpg_error_t result_add_hash_algo (struct result_xml_state *state, const char *name, gpgme_hash_algo_t algo) { char code[20]; char msg[80]; snprintf (code, sizeof (code) - 1, "0x%x", algo); snprintf (msg, sizeof (msg) - 1, "%s", gpgme_hash_algo_name (algo)); result_xml_tag_start (state, name, "value", code, NULL); result_xml_tag_data (state, msg); result_xml_tag_end (state); return 0; } gpg_error_t result_add_keyid (struct result_xml_state *state, const char *name, const char *keyid) { result_xml_tag_start (state, name, NULL); result_xml_tag_data (state, keyid); result_xml_tag_end (state); return 0; } gpg_error_t result_add_fpr (struct result_xml_state *state, const char *name, const char *fpr) { result_xml_tag_start (state, name, NULL); result_xml_tag_data (state, fpr); result_xml_tag_end (state); return 0; } gpg_error_t result_add_timestamp (struct result_xml_state *state, const char *name, unsigned int timestamp) { char code[20]; snprintf (code, sizeof (code) - 1, "%ui", timestamp); result_xml_tag_start (state, name, "unix", code, NULL); result_xml_tag_end (state); return 0; } gpg_error_t result_add_sig_mode (struct result_xml_state *state, const char *name, gpgme_sig_mode_t sig_mode) { const char *mode; char code[20]; snprintf (code, sizeof (code) - 1, "%i", sig_mode); switch (sig_mode) { case GPGME_SIG_MODE_NORMAL: mode = "normal"; break; case GPGME_SIG_MODE_DETACH: mode = "detach"; break; case GPGME_SIG_MODE_CLEAR: mode = "clear"; break; default: mode = "unknown"; } result_xml_tag_start (state, name, "type", mode, "value", code, NULL); result_xml_tag_data (state, mode); result_xml_tag_end (state); return 0; } gpg_error_t result_add_protocol (struct result_xml_state *state, const char *name, gpgme_protocol_t protocol) { const char *str; char code[20]; snprintf (code, sizeof (code) - 1, "%i", protocol); str = gpgme_get_protocol_name(protocol); if (!str) str = "invalid"; result_xml_tag_start (state, name, "value", code, NULL); result_xml_tag_data (state, str); result_xml_tag_end (state); return 0; } gpg_error_t result_add_validity (struct result_xml_state *state, const char *name, gpgme_validity_t validity) { const char *str; char code[20]; snprintf (code, sizeof (code) - 1, "%i", validity); switch (validity) { case GPGME_VALIDITY_UNDEFINED: str ="undefined"; break; case GPGME_VALIDITY_NEVER: str ="never"; break; case GPGME_VALIDITY_MARGINAL: str ="marginal"; break; case GPGME_VALIDITY_FULL: str ="full"; break; case GPGME_VALIDITY_ULTIMATE: str ="ultimate"; break; default: str ="unknown"; } result_xml_tag_start (state, name, "value", code, NULL); result_xml_tag_data (state, str); result_xml_tag_end (state); return 0; } gpg_error_t result_add_value (struct result_xml_state *state, const char *name, unsigned int val) { char code[20]; snprintf (code, sizeof (code) - 1, "0x%x", val); result_xml_tag_start (state, name, "value", code, NULL); result_xml_tag_end (state); return 0; } gpg_error_t result_add_string (struct result_xml_state *state, const char *name, const char *str) { if (!str) str = ""; result_xml_tag_start (state, name, NULL); result_xml_tag_data (state, str); result_xml_tag_end (state); return 0; } gpg_error_t result_encrypt_to_xml (gpgme_ctx_t ctx, int indent, result_xml_write_cb_t cb, void *hook) { struct result_xml_state state; gpgme_encrypt_result_t res = gpgme_op_encrypt_result (ctx); gpgme_invalid_key_t inv_recp; if (! res) return 0; result_init (&state, indent, cb, hook); result_xml_tag_start (&state, "encrypt-result", NULL); inv_recp = res->invalid_recipients; if (inv_recp) { result_xml_tag_start (&state, "invalid-recipients", NULL); while (inv_recp) { result_xml_tag_start (&state, "invalid-key", NULL); if (inv_recp->fpr) result_add_fpr (&state, "fpr", inv_recp->fpr); result_add_error (&state, "reason", inv_recp->reason); result_xml_tag_end (&state); inv_recp = inv_recp->next; } result_xml_tag_end (&state); } result_xml_tag_end (&state); return 0; } gpg_error_t result_decrypt_to_xml (gpgme_ctx_t ctx, int indent, result_xml_write_cb_t cb, void *hook) { struct result_xml_state state; gpgme_decrypt_result_t res = gpgme_op_decrypt_result (ctx); gpgme_recipient_t recp; if (! res) return 0; result_init (&state, indent, cb, hook); result_xml_tag_start (&state, "decrypt-result", NULL); if (res->file_name) { result_xml_tag_start (&state, "file-name", NULL); result_xml_tag_data (&state, res->file_name); result_xml_tag_end (&state); } if (res->unsupported_algorithm) { result_xml_tag_start (&state, "unsupported-alogorithm", NULL); result_xml_tag_data (&state, res->unsupported_algorithm); result_xml_tag_end (&state); } if (res->wrong_key_usage) { result_xml_tag_start (&state, "wrong-key-usage", NULL); result_xml_tag_end (&state); } recp = res->recipients; if (recp) { result_xml_tag_start (&state, "recipients", NULL); while (recp) { result_xml_tag_start (&state, "recipient", NULL); result_add_keyid (&state, "keyid", recp->keyid); result_add_pubkey_algo (&state, "pubkey-algo", recp->pubkey_algo); result_add_error (&state, "status", recp->status); result_xml_tag_end (&state); recp = recp->next; } result_xml_tag_end (&state); } result_xml_tag_end (&state); return 0; } gpg_error_t result_sign_to_xml (gpgme_ctx_t ctx, int indent, result_xml_write_cb_t cb, void *hook) { struct result_xml_state state; gpgme_sign_result_t res = gpgme_op_sign_result (ctx); gpgme_invalid_key_t inv_key; gpgme_new_signature_t new_sig; if (! res) return 0; result_init (&state, indent, cb, hook); result_xml_tag_start (&state, "sign-result", NULL); inv_key = res->invalid_signers; if (inv_key) { result_xml_tag_start (&state, "invalid-signers", NULL); while (inv_key) { result_xml_tag_start (&state, "invalid-key", NULL); if (inv_key->fpr) result_add_fpr (&state, "fpr", inv_key->fpr); result_add_error (&state, "reason", inv_key->reason); result_xml_tag_end (&state); inv_key = inv_key->next; } result_xml_tag_end (&state); } new_sig = res->signatures; if (new_sig) { result_xml_tag_start (&state, "signatures", NULL); while (new_sig) { result_xml_tag_start (&state, "new-signature", NULL); result_add_sig_mode (&state, "type", new_sig->type); result_add_pubkey_algo (&state, "pubkey-algo", new_sig->pubkey_algo); result_add_hash_algo (&state, "hash-algo", new_sig->hash_algo); result_add_timestamp (&state, "timestamp", new_sig->timestamp); if (new_sig->fpr) result_add_fpr (&state, "fpr", new_sig->fpr); result_add_value (&state, "sig-class", new_sig->sig_class); result_xml_tag_end (&state); new_sig = new_sig->next; } result_xml_tag_end (&state); } result_xml_tag_end (&state); return 0; } gpg_error_t result_verify_to_xml (gpgme_ctx_t ctx, int indent, result_xml_write_cb_t cb, void *hook) { struct result_xml_state state; gpgme_verify_result_t res = gpgme_op_verify_result (ctx); gpgme_signature_t sig; if (! res) return 0; result_init (&state, indent, cb, hook); result_xml_tag_start (&state, "verify-result", NULL); if (res->file_name) { result_xml_tag_start (&state, "file-name", NULL); result_xml_tag_data (&state, res->file_name); result_xml_tag_end (&state); } sig = res->signatures; if (sig) { result_xml_tag_start (&state, "signatures", NULL); while (sig) { result_xml_tag_start (&state, "signature", NULL); /* FIXME: Could be done better. */ result_add_value (&state, "summary", sig->summary); if (sig->fpr) result_add_fpr (&state, "fpr", sig->fpr); result_add_error (&state, "status", sig->status); /* FIXME: notations */ result_add_timestamp (&state, "timestamp", sig->timestamp); result_add_timestamp (&state, "exp-timestamp", sig->exp_timestamp); result_add_value (&state, "wrong-key-usage", sig->wrong_key_usage); result_add_value (&state, "pka-trust", sig->pka_trust); result_add_value (&state, "chain-model", sig->chain_model); result_add_value (&state, "validity", sig->validity); result_add_error (&state, "validity-reason", sig->validity_reason); result_add_pubkey_algo (&state, "pubkey-algo", sig->pubkey_algo); result_add_hash_algo (&state, "hash-algo", sig->hash_algo); if (sig->pka_address) result_add_string (&state, "pka_address", sig->pka_address); result_xml_tag_end (&state); sig = sig->next; } result_xml_tag_end (&state); } result_xml_tag_end (&state); return 0; } gpg_error_t result_import_to_xml (gpgme_ctx_t ctx, int indent, result_xml_write_cb_t cb, void *hook) { struct result_xml_state state; gpgme_import_result_t res = gpgme_op_import_result (ctx); gpgme_import_status_t stat; if (! res) return 0; result_init (&state, indent, cb, hook); result_xml_tag_start (&state, "import-result", NULL); result_add_value (&state, "considered", res->considered); result_add_value (&state, "no-user-id", res->no_user_id); result_add_value (&state, "imported", res->imported); result_add_value (&state, "imported-rsa", res->imported_rsa); result_add_value (&state, "unchanged", res->unchanged); result_add_value (&state, "new-user-ids", res->new_user_ids); result_add_value (&state, "new-sub-keys", res->new_sub_keys); result_add_value (&state, "new-signatures", res->new_signatures); result_add_value (&state, "new-revocations", res->new_revocations); result_add_value (&state, "secret-read", res->secret_read); result_add_value (&state, "secret-imported", res->secret_imported); result_add_value (&state, "secret-unchanged", res->secret_unchanged); result_add_value (&state, "skipped-new-keys", res->skipped_new_keys); result_add_value (&state, "not-imported", res->not_imported); stat = res->imports; if (stat) { result_xml_tag_start (&state, "imports", NULL); while (stat) { result_xml_tag_start (&state, "import-status", NULL); if (stat->fpr) result_add_fpr (&state, "fpr", stat->fpr); result_add_error (&state, "result", stat->result); /* FIXME: Could be done better. */ result_add_value (&state, "status", stat->status); result_xml_tag_end (&state); stat = stat->next; } result_xml_tag_end (&state); } result_xml_tag_end (&state); return 0; } gpg_error_t result_genkey_to_xml (gpgme_ctx_t ctx, int indent, result_xml_write_cb_t cb, void *hook) { struct result_xml_state state; gpgme_genkey_result_t res = gpgme_op_genkey_result (ctx); if (! res) return 0; result_init (&state, indent, cb, hook); result_xml_tag_start (&state, "genkey-result", NULL); result_add_value (&state, "primary", res->primary); result_add_value (&state, "sub", res->sub); if (res->fpr) result_add_fpr (&state, "fpr", res->fpr); result_xml_tag_end (&state); return 0; } gpg_error_t result_keylist_to_xml (gpgme_ctx_t ctx, int indent, result_xml_write_cb_t cb, void *hook) { struct result_xml_state state; gpgme_keylist_result_t res = gpgme_op_keylist_result (ctx); if (! res) return 0; result_init (&state, indent, cb, hook); result_xml_tag_start (&state, "keylist-result", NULL); result_add_value (&state, "truncated", res->truncated); result_xml_tag_end (&state); return 0; } gpg_error_t result_vfs_mount_to_xml (gpgme_ctx_t ctx, int indent, result_xml_write_cb_t cb, void *hook) { struct result_xml_state state; gpgme_vfs_mount_result_t res = gpgme_op_vfs_mount_result (ctx); if (! res) return 0; result_init (&state, indent, cb, hook); result_xml_tag_start (&state, "vfs-mount-result", NULL); result_add_string (&state, "mount-dir", res->mount_dir); result_xml_tag_end (&state); return 0; } typedef enum status { STATUS_PROTOCOL, STATUS_PROGRESS, STATUS_ENGINE, STATUS_ARMOR, STATUS_TEXTMODE, STATUS_INCLUDE_CERTS, STATUS_KEYLIST_MODE, STATUS_RECIPIENT, STATUS_ENCRYPT_RESULT, STATUS_IDENTIFY_RESULT } status_t; const char *status_string[] = { "PROTOCOL", "PROGRESS", "ENGINE", "ARMOR", "TEXTMODE", "INCLUDE_CERTS", "KEYLIST_MODE", "RECIPIENT", "ENCRYPT_RESULT", "IDENTIFY_RESULT" }; struct gpgme_tool { gpgme_ctx_t ctx; #define MAX_RECIPIENTS 10 gpgme_key_t recipients[MAX_RECIPIENTS + 1]; int recipients_nr; gpg_error_t (*write_status) (void *hook, const char *status, const char *msg); void *write_status_hook; gpg_error_t (*write_data) (void *hook, const void *buf, size_t len); void *write_data_hook; }; typedef struct gpgme_tool *gpgme_tool_t; /* Forward declaration. */ void gt_write_status (gpgme_tool_t gt, status_t status, ...) GT_GCC_A_SENTINEL(0); static gpg_error_t server_passphrase_cb (void *opaque, const char *uid_hint, const char *info, int was_bad, int fd); void _gt_progress_cb (void *opaque, const char *what, int type, int current, int total) { gpgme_tool_t gt = opaque; char buf[100]; snprintf (buf, sizeof (buf), "0x%02x %i %i", type, current, total); gt_write_status (gt, STATUS_PROGRESS, what, buf, NULL); } gpg_error_t _gt_gpgme_new (gpgme_tool_t gt, gpgme_ctx_t *ctx) { gpg_error_t err; err = gpgme_new (ctx); if (err) return err; gpgme_set_progress_cb (*ctx, _gt_progress_cb, gt); return 0; } void gt_init (gpgme_tool_t gt) { gpg_error_t err; memset (gt, '\0', sizeof (*gt)); err = _gt_gpgme_new (gt, >->ctx); if (err) log_error (1, err, "can't create gpgme context"); } gpg_error_t gt_signers_add (gpgme_tool_t gt, const char *fpr) { gpg_error_t err; gpgme_key_t key; err = gpgme_get_key (gt->ctx, fpr, &key, 0); if (err) return err; return gpgme_signers_add (gt->ctx, key); } gpg_error_t gt_signers_clear (gpgme_tool_t gt) { gpgme_signers_clear (gt->ctx); return 0; } gpg_error_t gt_get_key (gpgme_tool_t gt, const char *pattern, gpgme_key_t *r_key) { gpgme_ctx_t ctx; gpgme_ctx_t listctx; gpgme_error_t err; gpgme_key_t key; if (!gt || !r_key || !pattern) return gpg_error (GPG_ERR_INV_VALUE); ctx = gt->ctx; err = gpgme_new (&listctx); if (err) return err; { gpgme_protocol_t proto; gpgme_engine_info_t info; /* Clone the relevant state. */ proto = gpgme_get_protocol (ctx); /* The g13 protocol does not allow keylisting, we need to choose something else. */ if (proto == GPGME_PROTOCOL_G13) proto = GPGME_PROTOCOL_OpenPGP; gpgme_set_protocol (listctx, proto); gpgme_set_keylist_mode (listctx, gpgme_get_keylist_mode (ctx)); info = gpgme_ctx_get_engine_info (ctx); while (info && info->protocol != proto) info = info->next; if (info) gpgme_ctx_set_engine_info (listctx, proto, info->file_name, info->home_dir); } err = gpgme_op_keylist_start (listctx, pattern, 0); if (!err) err = gpgme_op_keylist_next (listctx, r_key); if (!err) { try_next_key: err = gpgme_op_keylist_next (listctx, &key); if (gpgme_err_code (err) == GPG_ERR_EOF) err = 0; else { if (!err && *r_key && (*r_key)->subkeys && (*r_key)->subkeys->fpr && key && key->subkeys && key->subkeys->fpr && !strcmp ((*r_key)->subkeys->fpr, key->subkeys->fpr)) { /* The fingerprint is identical. We assume that this is the same key and don't mark it as an ambiguous. This problem may occur with corrupted keyrings and has been noticed often with gpgsm. In fact gpgsm uses a similar hack to sort out such duplicates but it can't do that while listing keys. */ gpgme_key_unref (key); goto try_next_key; } if (!err) { gpgme_key_unref (key); err = gpg_error (GPG_ERR_AMBIGUOUS_NAME); } gpgme_key_unref (*r_key); } } gpgme_release (listctx); if (! err) gt_write_status (gt, STATUS_RECIPIENT, ((*r_key)->subkeys && (*r_key)->subkeys->fpr) ? (*r_key)->subkeys->fpr : "invalid", NULL); return err; } gpg_error_t gt_recipients_add (gpgme_tool_t gt, const char *pattern) { gpg_error_t err; gpgme_key_t key; if (gt->recipients_nr >= MAX_RECIPIENTS) return gpg_error (GPG_ERR_ENOMEM); if (gpgme_get_protocol (gt->ctx) == GPGME_PROTOCOL_UISERVER) err = gpgme_key_from_uid (&key, pattern); else err = gt_get_key (gt, pattern, &key); if (err) return err; gt->recipients[gt->recipients_nr++] = key; return 0; } void gt_recipients_clear (gpgme_tool_t gt) { int idx; for (idx = 0; idx < gt->recipients_nr; idx++) gpgme_key_unref (gt->recipients[idx]); memset (gt->recipients, '\0', gt->recipients_nr * sizeof (gpgme_key_t)); gt->recipients_nr = 0; } gpg_error_t gt_reset (gpgme_tool_t gt) { gpg_error_t err; gpgme_ctx_t ctx; err = _gt_gpgme_new (gt, &ctx); if (err) return err; gpgme_release (gt->ctx); gt->ctx = ctx; gt_recipients_clear (gt); return 0; } void gt_write_status (gpgme_tool_t gt, status_t status, ...) { va_list ap; const char *text; char buf[950]; char *p; size_t n; gpg_error_t err; va_start (ap, status); p = buf; n = 0; while ((text = va_arg (ap, const char *))) { if (n) { *p++ = ' '; n++; } while (*text && n < sizeof (buf) - 2) { *p++ = *text++; n++; } } *p = 0; va_end (ap); err = gt->write_status (gt->write_status_hook, status_string[status], buf); if (err) log_error (1, err, "can't write status line"); } gpg_error_t gt_write_data (gpgme_tool_t gt, const void *buf, size_t len) { return gt->write_data (gt->write_data_hook, buf, len); } gpg_error_t gt_get_engine_info (gpgme_tool_t gt, gpgme_protocol_t proto) { gpgme_engine_info_t info; info = gpgme_ctx_get_engine_info (gt->ctx); while (info) { if (proto == GPGME_PROTOCOL_UNKNOWN || proto == info->protocol) gt_write_status (gt, STATUS_ENGINE, gpgme_get_protocol_name (info->protocol), info->file_name, info->version, info->req_version, info->home_dir, NULL); info = info->next; } return 0; } gpgme_protocol_t gt_protocol_from_name (const char *name) { if (! strcasecmp (name, gpgme_get_protocol_name (GPGME_PROTOCOL_OpenPGP))) return GPGME_PROTOCOL_OpenPGP; if (! strcasecmp (name, gpgme_get_protocol_name (GPGME_PROTOCOL_CMS))) return GPGME_PROTOCOL_CMS; if (! strcasecmp (name,gpgme_get_protocol_name (GPGME_PROTOCOL_GPGCONF))) return GPGME_PROTOCOL_GPGCONF; if (! strcasecmp (name, gpgme_get_protocol_name (GPGME_PROTOCOL_ASSUAN))) return GPGME_PROTOCOL_ASSUAN; if (! strcasecmp (name, gpgme_get_protocol_name (GPGME_PROTOCOL_G13))) return GPGME_PROTOCOL_G13; if (! strcasecmp (name, gpgme_get_protocol_name (GPGME_PROTOCOL_UISERVER))) return GPGME_PROTOCOL_UISERVER; if (! strcasecmp (name, gpgme_get_protocol_name (GPGME_PROTOCOL_SPAWN))) return GPGME_PROTOCOL_SPAWN; if (! strcasecmp (name, gpgme_get_protocol_name (GPGME_PROTOCOL_DEFAULT))) return GPGME_PROTOCOL_DEFAULT; return GPGME_PROTOCOL_UNKNOWN; } gpg_error_t gt_set_protocol (gpgme_tool_t gt, gpgme_protocol_t proto) { return gpgme_set_protocol (gt->ctx, proto); } gpg_error_t gt_get_protocol (gpgme_tool_t gt) { gpgme_protocol_t proto = gpgme_get_protocol (gt->ctx); gt_write_status (gt, STATUS_PROTOCOL, gpgme_get_protocol_name (proto), NULL); return 0; } gpg_error_t gt_set_sub_protocol (gpgme_tool_t gt, gpgme_protocol_t proto) { return gpgme_set_sub_protocol (gt->ctx, proto); } gpg_error_t gt_get_sub_protocol (gpgme_tool_t gt) { gpgme_protocol_t proto = gpgme_get_sub_protocol (gt->ctx); gt_write_status (gt, STATUS_PROTOCOL, gpgme_get_protocol_name (proto), NULL); return 0; } gpg_error_t gt_set_pinentry_mode (gpgme_tool_t gt, gpgme_pinentry_mode_t mode, void *opaque) { gpg_error_t err; gpgme_set_passphrase_cb (gt->ctx, NULL, NULL); err = gpgme_set_pinentry_mode (gt->ctx, mode); if (!err && mode == GPGME_PINENTRY_MODE_LOOPBACK) gpgme_set_passphrase_cb (gt->ctx, server_passphrase_cb, opaque); return err; } gpg_error_t gt_set_armor (gpgme_tool_t gt, int armor) { gpgme_set_armor (gt->ctx, armor); return 0; } gpg_error_t gt_get_armor (gpgme_tool_t gt) { gt_write_status (gt, STATUS_ARMOR, gpgme_get_armor (gt->ctx) ? "true" : "false", NULL); return 0; } gpg_error_t gt_set_textmode (gpgme_tool_t gt, int textmode) { gpgme_set_textmode (gt->ctx, textmode); return 0; } gpg_error_t gt_get_textmode (gpgme_tool_t gt) { gt_write_status (gt, STATUS_TEXTMODE, gpgme_get_textmode (gt->ctx) ? "true" : "false", NULL); return 0; } gpg_error_t gt_set_keylist_mode (gpgme_tool_t gt, gpgme_keylist_mode_t keylist_mode) { gpgme_set_keylist_mode (gt->ctx, keylist_mode); return 0; } gpg_error_t gt_get_keylist_mode (gpgme_tool_t gt) { #define NR_KEYLIST_MODES 6 const char *modes[NR_KEYLIST_MODES + 1]; int idx = 0; gpgme_keylist_mode_t mode = gpgme_get_keylist_mode (gt->ctx); if (mode & GPGME_KEYLIST_MODE_LOCAL) modes[idx++] = "local"; if (mode & GPGME_KEYLIST_MODE_EXTERN) modes[idx++] = "extern"; if (mode & GPGME_KEYLIST_MODE_SIGS) modes[idx++] = "sigs"; if (mode & GPGME_KEYLIST_MODE_SIG_NOTATIONS) modes[idx++] = "sig_notations"; if (mode & GPGME_KEYLIST_MODE_WITH_SECRET) modes[idx++] = "with_secret"; if (mode & GPGME_KEYLIST_MODE_EPHEMERAL) modes[idx++] = "ephemeral"; if (mode & GPGME_KEYLIST_MODE_VALIDATE) modes[idx++] = "validate"; modes[idx++] = NULL; gt_write_status (gt, STATUS_KEYLIST_MODE, modes[0], modes[1], modes[2], modes[3], modes[4], modes[5], modes[6], NULL); return 0; } gpg_error_t gt_set_include_certs (gpgme_tool_t gt, int include_certs) { gpgme_set_include_certs (gt->ctx, include_certs); return 0; } gpg_error_t gt_get_include_certs (gpgme_tool_t gt) { int include_certs = gpgme_get_include_certs (gt->ctx); char buf[100]; if (include_certs == GPGME_INCLUDE_CERTS_DEFAULT) strcpy (buf, "default"); else snprintf (buf, sizeof (buf), "%i", include_certs); gt_write_status (gt, STATUS_INCLUDE_CERTS, buf, NULL); return 0; } gpg_error_t gt_decrypt_verify (gpgme_tool_t gt, gpgme_data_t cipher, gpgme_data_t plain, int verify) { if (verify) return gpgme_op_decrypt_verify (gt->ctx, cipher, plain); else return gpgme_op_decrypt (gt->ctx, cipher, plain); } gpg_error_t gt_sign_encrypt (gpgme_tool_t gt, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher, int sign) { gpg_error_t err; gpgme_key_t *recp; recp = gt->recipients_nr? gt->recipients : NULL; if (sign) err = gpgme_op_encrypt_sign (gt->ctx, recp, flags, plain, cipher); else err = gpgme_op_encrypt (gt->ctx, recp, flags, plain, cipher); gt_recipients_clear (gt); return err; } gpg_error_t gt_sign (gpgme_tool_t gt, gpgme_data_t plain, gpgme_data_t sig, gpgme_sig_mode_t mode) { return gpgme_op_sign (gt->ctx, plain, sig, mode); } gpg_error_t gt_verify (gpgme_tool_t gt, gpgme_data_t sig, gpgme_data_t sig_text, gpgme_data_t plain) { return gpgme_op_verify (gt->ctx, sig, sig_text, plain); } gpg_error_t gt_import (gpgme_tool_t gt, gpgme_data_t data) { return gpgme_op_import (gt->ctx, data); } gpg_error_t gt_export (gpgme_tool_t gt, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t data) { return gpgme_op_export_ext (gt->ctx, pattern, mode, data); } gpg_error_t gt_genkey (gpgme_tool_t gt, const char *parms, gpgme_data_t public, gpgme_data_t secret) { return gpgme_op_genkey (gt->ctx, parms, public, secret); } gpg_error_t gt_import_keys (gpgme_tool_t gt, char *fpr[]) { gpg_error_t err = 0; int cnt; int idx; gpgme_key_t *keys; cnt = 0; while (fpr[cnt]) cnt++; if (! cnt) return gpg_error (GPG_ERR_INV_VALUE); keys = malloc ((cnt + 1) * sizeof (gpgme_key_t)); if (! keys) return gpg_error_from_syserror (); for (idx = 0; idx < cnt; idx++) { err = gpgme_get_key (gt->ctx, fpr[idx], &keys[idx], 0); if (err) break; } if (! err) { keys[cnt] = NULL; err = gpgme_op_import_keys (gt->ctx, keys); } /* Rollback. */ while (--idx >= 0) gpgme_key_unref (keys[idx]); free (keys); return err; } gpg_error_t gt_delete (gpgme_tool_t gt, char *fpr, int allow_secret) { gpg_error_t err; gpgme_key_t key; err = gpgme_get_key (gt->ctx, fpr, &key, 0); if (err) return err; err = gpgme_op_delete (gt->ctx, key, allow_secret); gpgme_key_unref (key); return err; } gpg_error_t gt_keylist_start (gpgme_tool_t gt, const char *pattern[], int secret_only) { return gpgme_op_keylist_ext_start (gt->ctx, pattern, secret_only, 0); } gpg_error_t gt_keylist_next (gpgme_tool_t gt, gpgme_key_t *key) { return gpgme_op_keylist_next (gt->ctx, key); } gpg_error_t gt_getauditlog (gpgme_tool_t gt, gpgme_data_t output, unsigned int flags) { return gpgme_op_getauditlog (gt->ctx, output, flags); } gpg_error_t gt_vfs_mount (gpgme_tool_t gt, const char *container_file, const char *mount_dir, int flags) { gpg_error_t err; gpg_error_t op_err; err = gpgme_op_vfs_mount (gt->ctx, container_file, mount_dir, flags, &op_err); return err ? err : op_err; } gpg_error_t gt_vfs_create (gpgme_tool_t gt, const char *container_file, int flags) { gpg_error_t err; gpg_error_t op_err; err = gpgme_op_vfs_create (gt->ctx, gt->recipients, container_file, flags, &op_err); gt_recipients_clear (gt); return err ? err : op_err; } gpg_error_t gt_passwd (gpgme_tool_t gt, char *fpr) { gpg_error_t err; gpgme_key_t key; err = gpgme_get_key (gt->ctx, fpr, &key, 0); if (err) return gpg_err_code (err) == GPG_ERR_EOF? gpg_error (GPG_ERR_NO_PUBKEY):err; err = gpgme_op_passwd (gt->ctx, key, 0); gpgme_key_unref (key); return err; } gpg_error_t gt_identify (gpgme_tool_t gt, gpgme_data_t data) { const char *s = "?"; switch (gpgme_data_identify (data, 0)) { case GPGME_DATA_TYPE_INVALID: return gpg_error (GPG_ERR_GENERAL); case GPGME_DATA_TYPE_UNKNOWN : s = "unknown"; break; case GPGME_DATA_TYPE_PGP_SIGNED : s = "PGP-signed"; break; case GPGME_DATA_TYPE_PGP_SIGNATURE: s = "PGP-signature"; break; case GPGME_DATA_TYPE_PGP_ENCRYPTED: s = "PGP-encrypted"; break; case GPGME_DATA_TYPE_PGP_OTHER : s = "PGP"; break; case GPGME_DATA_TYPE_PGP_KEY : s = "PGP-key"; break; case GPGME_DATA_TYPE_CMS_SIGNED : s = "CMS-signed"; break; case GPGME_DATA_TYPE_CMS_ENCRYPTED: s = "CMS-encrypted"; break; case GPGME_DATA_TYPE_CMS_OTHER : s = "CMS"; break; case GPGME_DATA_TYPE_X509_CERT : s = "X.509"; break; case GPGME_DATA_TYPE_PKCS12 : s = "PKCS12"; break; } gt_write_status (gt, STATUS_IDENTIFY_RESULT, s, NULL); return 0; } gpg_error_t gt_spawn (gpgme_tool_t gt, const char *pgm, gpgme_data_t inp, gpgme_data_t outp) { gpg_error_t err; err = gpgme_op_spawn (gt->ctx, pgm, NULL, inp, outp, outp, 0); return err; } #define GT_RESULT_ENCRYPT 0x1 #define GT_RESULT_DECRYPT 0x2 #define GT_RESULT_SIGN 0x4 #define GT_RESULT_VERIFY 0x8 #define GT_RESULT_IMPORT 0x10 #define GT_RESULT_GENKEY 0x20 #define GT_RESULT_KEYLIST 0x40 #define GT_RESULT_VFS_MOUNT 0x80 #define GT_RESULT_ALL (~0U) gpg_error_t gt_result (gpgme_tool_t gt, unsigned int flags) { int indent = 2; gt_write_data (gt, xml_preamble1, strlen (xml_preamble1)); gt_write_data (gt, NULL, 0); gt_write_data (gt, xml_preamble2, strlen (xml_preamble2)); gt_write_data (gt, NULL, 0); if (flags & GT_RESULT_ENCRYPT) result_encrypt_to_xml (gt->ctx, indent, (result_xml_write_cb_t) gt_write_data, gt); if (flags & GT_RESULT_DECRYPT) result_decrypt_to_xml (gt->ctx, indent, (result_xml_write_cb_t) gt_write_data, gt); if (flags & GT_RESULT_SIGN) result_sign_to_xml (gt->ctx, indent, (result_xml_write_cb_t) gt_write_data, gt); if (flags & GT_RESULT_VERIFY) result_verify_to_xml (gt->ctx, indent, (result_xml_write_cb_t) gt_write_data, gt); if (flags & GT_RESULT_IMPORT) result_import_to_xml (gt->ctx, indent, (result_xml_write_cb_t) gt_write_data, gt); if (flags & GT_RESULT_GENKEY) result_genkey_to_xml (gt->ctx, indent, (result_xml_write_cb_t) gt_write_data, gt); if (flags & GT_RESULT_KEYLIST) result_keylist_to_xml (gt->ctx, indent, (result_xml_write_cb_t) gt_write_data, gt); if (flags & GT_RESULT_VFS_MOUNT) result_vfs_mount_to_xml (gt->ctx, indent, (result_xml_write_cb_t) gt_write_data, gt); gt_write_data (gt, xml_end, strlen (xml_end)); return 0; } /* GPGME SERVER. */ #include struct server { gpgme_tool_t gt; assuan_context_t assuan_ctx; gpgme_data_encoding_t input_enc; gpgme_data_encoding_t output_enc; assuan_fd_t input_fd; char *input_filename; FILE *input_stream; assuan_fd_t output_fd; char *output_filename; FILE *output_stream; assuan_fd_t message_fd; char *message_filename; FILE *message_stream; gpgme_data_encoding_t message_enc; }; gpg_error_t server_write_status (void *hook, const char *status, const char *msg) { struct server *server = hook; return assuan_write_status (server->assuan_ctx, status, msg); } gpg_error_t server_write_data (void *hook, const void *buf, size_t len) { struct server *server = hook; return assuan_send_data (server->assuan_ctx, buf, len); } static gpg_error_t server_passphrase_cb (void *opaque, const char *uid_hint, const char *info, int was_bad, int fd) { struct server *server = opaque; gpg_error_t err; unsigned char *buf = NULL; size_t buflen = 0; (void)was_bad; if (server && server->assuan_ctx) { if (uid_hint) assuan_write_status (server->assuan_ctx, "USERID_HINT", uid_hint); if (info) assuan_write_status (server->assuan_ctx, "NEED_PASSPHRASE", info); err = assuan_inquire (server->assuan_ctx, "PASSPHRASE", &buf, &buflen, 100); } else err = gpg_error (GPG_ERR_NO_PASSPHRASE); if (!err) { /* We take care to always send a LF. */ if (gpgme_io_writen (fd, buf, buflen)) err = gpg_error_from_syserror (); else if (!memchr (buf, '\n', buflen) && gpgme_io_writen (fd, "\n", 1)) err = gpg_error_from_syserror (); } free (buf); return err; } /* Wrapper around assuan_command_parse_fd to also handle a "file=FILENAME" argument. On success either a filename is returned at FILENAME or a file descriptor at RFD; the other one is set to NULL respective ASSUAN_INVALID_FD. */ static gpg_error_t server_parse_fd (assuan_context_t ctx, char *line, assuan_fd_t *rfd, char **filename) { *rfd = ASSUAN_INVALID_FD; *filename = NULL; if (! strncasecmp (line, "file=", 5)) { char *term; *filename = strdup (line + 5); if (!*filename) return gpg_error_from_syserror(); term = strchr (*filename, ' '); if (term) *term = '\0'; return 0; } else return assuan_command_parse_fd (ctx, line, rfd); } static gpgme_data_encoding_t server_data_encoding (const char *line) { if (strstr (line, "--binary")) return GPGME_DATA_ENCODING_BINARY; if (strstr (line, "--base64")) return GPGME_DATA_ENCODING_BASE64; if (strstr (line, "--armor")) return GPGME_DATA_ENCODING_ARMOR; if (strstr (line, "--url")) return GPGME_DATA_ENCODING_URL; if (strstr (line, "--urlesc")) return GPGME_DATA_ENCODING_URLESC; if (strstr (line, "--url0")) return GPGME_DATA_ENCODING_URL0; if (strstr (line, "--mime")) return GPGME_DATA_ENCODING_MIME; return GPGME_DATA_ENCODING_NONE; } static gpgme_error_t server_data_obj (assuan_fd_t fd, char *fn, int out, gpgme_data_encoding_t encoding, gpgme_data_t *data, FILE **fs) { gpgme_error_t err; *fs = NULL; if (fn) { *fs = fopen (fn, out ? "wb" : "rb"); if (!*fs) return gpg_error_from_syserror (); err = gpgme_data_new_from_stream (data, *fs); } else err = gpgme_data_new_from_fd (data, (int) fd); if (err) return err; return gpgme_data_set_encoding (*data, encoding); } void server_reset_fds (struct server *server) { /* assuan closes the input and output FDs for us when doing a RESET, but we use this same function after commands, so repeat it here. */ if (server->input_fd != ASSUAN_INVALID_FD) { #if HAVE_W32_SYSTEM CloseHandle (server->input_fd); #else close (server->input_fd); #endif server->input_fd = ASSUAN_INVALID_FD; } if (server->output_fd != ASSUAN_INVALID_FD) { #if HAVE_W32_SYSTEM CloseHandle (server->output_fd); #else close (server->output_fd); #endif server->output_fd = ASSUAN_INVALID_FD; } if (server->message_fd != ASSUAN_INVALID_FD) { /* FIXME: Assuan should provide a close function. */ #if HAVE_W32_SYSTEM CloseHandle (server->message_fd); #else close (server->message_fd); #endif server->message_fd = ASSUAN_INVALID_FD; } if (server->input_filename) { free (server->input_filename); server->input_filename = NULL; } if (server->output_filename) { free (server->output_filename); server->output_filename = NULL; } if (server->message_filename) { free (server->message_filename); server->message_filename = NULL; } if (server->input_stream) { fclose (server->input_stream); server->input_stream = NULL; } if (server->output_stream) { fclose (server->output_stream); server->output_stream = NULL; } if (server->message_stream) { fclose (server->message_stream); server->message_stream = NULL; } server->input_enc = GPGME_DATA_ENCODING_NONE; server->output_enc = GPGME_DATA_ENCODING_NONE; server->message_enc = GPGME_DATA_ENCODING_NONE; } static gpg_error_t reset_notify (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); (void)line; server_reset_fds (server); gt_reset (server->gt); return 0; } static const char hlp_version[] = "VERSION []\n" "\n" "Call the function gpgme_check_version."; static gpg_error_t cmd_version (assuan_context_t ctx, char *line) { if (line && *line) { const char *version = gpgme_check_version (line); return version ? 0 : gpg_error (GPG_ERR_SELFTEST_FAILED); } else { const char *version = gpgme_check_version (NULL); return assuan_send_data (ctx, version, strlen (version)); } } static const char hlp_engine[] = "ENGINE []\n" "\n" "Get information about a GPGME engine (a.k.a. protocol)."; static gpg_error_t cmd_engine (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); return gt_get_engine_info (server->gt, gt_protocol_from_name (line)); } static const char hlp_protocol[] = "PROTOCOL []\n" "\n" "With NAME, set the protocol. Without, return the current\n" "protocol."; static gpg_error_t cmd_protocol (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); if (line && *line) return gt_set_protocol (server->gt, gt_protocol_from_name (line)); else return gt_get_protocol (server->gt); } static const char hlp_sub_protocol[] = "SUB_PROTOCOL []\n" "\n" "With NAME, set the sub-protocol. Without, return the\n" "current sub-protocol."; static gpg_error_t cmd_sub_protocol (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); if (line && *line) return gt_set_sub_protocol (server->gt, gt_protocol_from_name (line)); else return gt_get_sub_protocol (server->gt); } static const char hlp_pinentry_mode[] = "PINENTRY_MODE \n" "\n" "Set the pinentry mode to NAME. Allowedvalues for NAME are:\n" " default - reset to the default of the engine,\n" " ask - force the use of the pinentry,\n" " cancel - emulate use of pinentry's cancel button,\n" " error - return a pinentry error,\n" " loopback - redirect pinentry queries to the caller.\n" "Note that only recent versions of GPG support changing the pinentry mode."; static gpg_error_t cmd_pinentry_mode (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpgme_pinentry_mode_t mode; if (!line || !*line || !strcmp (line, "default")) mode = GPGME_PINENTRY_MODE_DEFAULT; else if (!strcmp (line, "ask")) mode = GPGME_PINENTRY_MODE_ASK; else if (!strcmp (line, "cancel")) mode = GPGME_PINENTRY_MODE_CANCEL; else if (!strcmp (line, "error")) mode = GPGME_PINENTRY_MODE_ERROR; else if (!strcmp (line, "loopback")) mode = GPGME_PINENTRY_MODE_LOOPBACK; else return gpg_error (GPG_ERR_INV_VALUE); return gt_set_pinentry_mode (server->gt, mode, server); } static const char hlp_armor[] = "ARMOR [true|false]\n" "\n" "With 'true' or 'false', turn output ASCII armoring on or\n" "off. Without, return the current armoring status."; static gpg_error_t cmd_armor (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); if (line && *line) { int flag = 0; if (! strcasecmp (line, "true") || ! strcasecmp (line, "yes") || line[0] == '1') flag = 1; return gt_set_armor (server->gt, flag); } else return gt_get_armor (server->gt); } static const char hlp_textmode[] = "TEXTMODE [true|false]\n" "\n" "With 'true' or 'false', turn text mode on or off.\n" "Without, return the current text mode status."; static gpg_error_t cmd_textmode (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); if (line && *line) { int flag = 0; if (! strcasecmp (line, "true") || ! strcasecmp (line, "yes") || line[0] == '1') flag = 1; return gt_set_textmode (server->gt, flag); } else return gt_get_textmode (server->gt); } static const char hlp_include_certs[] = "INCLUDE_CERTS [default|]\n" "\n" "With DEFAULT or N, set how many certificates should be\n" "included in the next S/MIME signed message. See the\n" "GPGME documentation for details on the meaning of" "various N. Without either, return the current setting."; static gpg_error_t cmd_include_certs (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); if (line && *line) { int include_certs = 0; if (! strcasecmp (line, "default")) include_certs = GPGME_INCLUDE_CERTS_DEFAULT; else include_certs = atoi (line); return gt_set_include_certs (server->gt, include_certs); } else return gt_get_include_certs (server->gt); } static const char hlp_keylist_mode[] = "KEYLIST_MODE [local] [extern] [sigs] [sig_notations]\n" " [ephemeral] [validate]\n" "\n" "Set the mode for the next KEYLIST command."; static gpg_error_t cmd_keylist_mode (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); if (line && *line) { gpgme_keylist_mode_t mode = 0; if (strstr (line, "local")) mode |= GPGME_KEYLIST_MODE_LOCAL; if (strstr (line, "extern")) mode |= GPGME_KEYLIST_MODE_EXTERN; if (strstr (line, "sigs")) mode |= GPGME_KEYLIST_MODE_SIGS; if (strstr (line, "sig_notations")) mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS; if (strstr (line, "with_secret")) mode |= GPGME_KEYLIST_MODE_WITH_SECRET; if (strstr (line, "ephemeral")) mode |= GPGME_KEYLIST_MODE_EPHEMERAL; if (strstr (line, "validate")) mode |= GPGME_KEYLIST_MODE_VALIDATE; return gt_set_keylist_mode (server->gt, mode); } else return gt_get_keylist_mode (server->gt); } static const char hlp_input[] = "INPUT [|FILE=]\n" "\n" "Set the input for the next command. Use either the\n" "Assuan file descriptor FD or a filesystem PATH."; static gpg_error_t cmd_input (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t sysfd; char *filename; err = server_parse_fd (ctx, line, &sysfd, &filename); if (err) return err; server->input_fd = sysfd; server->input_filename = filename; server->input_enc = server_data_encoding (line); return 0; } static const char hlp_output[] = "OUTPUT [|FILE=]\n" "\n" "Set the output for the next command. Use either the\n" "Assuan file descriptor FD or a filesystem PATH."; static gpg_error_t cmd_output (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t sysfd; char *filename; err = server_parse_fd (ctx, line, &sysfd, &filename); if (err) return err; server->output_fd = sysfd; server->output_filename = filename; server->output_enc = server_data_encoding (line); return 0; } static const char hlp_message[] = "MESSAGE [|FILE=]\n" "\n" "Set the plaintext message for the next VERIFY command\n" "with a detached signature. Use either the Assuan file\n" "descriptor FD or a filesystem PATH."; static gpg_error_t cmd_message (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t sysfd; char *filename; err = server_parse_fd (ctx, line, &sysfd, &filename); if (err) return err; server->message_fd = sysfd; server->message_filename = filename; server->message_enc = server_data_encoding (line); return 0; } static const char hlp_recipient[] = "RECIPIENT \n" "\n" "Add the key matching PATTERN to the list of recipients\n" "for the next encryption command."; static gpg_error_t cmd_recipient (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); return gt_recipients_add (server->gt, line); } static const char hlp_signer[] = "SIGNER \n" "\n" "Add the key with FINGERPRINT to the list of signers to\n" "be used for the next signing command."; static gpg_error_t cmd_signer (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); return gt_signers_add (server->gt, line); } static const char hlp_signers_clear[] = "SIGNERS_CLEAR\n" "\n" "Clear the list of signers specified by previous SIGNER\n" "commands."; static gpg_error_t cmd_signers_clear (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); (void)line; return gt_signers_clear (server->gt); } static gpg_error_t _cmd_decrypt_verify (assuan_context_t ctx, char *line, int verify) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t inp_fd; char *inp_fn; assuan_fd_t out_fd; char *out_fn; gpgme_data_t inp_data; gpgme_data_t out_data; (void)line; inp_fd = server->input_fd; inp_fn = server->input_filename; if (inp_fd == ASSUAN_INVALID_FD && !inp_fn) return GPG_ERR_ASS_NO_INPUT; out_fd = server->output_fd; out_fn = server->output_filename; if (out_fd == ASSUAN_INVALID_FD && !out_fn) return GPG_ERR_ASS_NO_OUTPUT; err = server_data_obj (inp_fd, inp_fn, 0, server->input_enc, &inp_data, &server->input_stream); if (err) return err; err = server_data_obj (out_fd, out_fn, 1, server->output_enc, &out_data, &server->output_stream); if (err) { gpgme_data_release (inp_data); return err; } err = gt_decrypt_verify (server->gt, inp_data, out_data, verify); gpgme_data_release (inp_data); gpgme_data_release (out_data); server_reset_fds (server); return err; } static const char hlp_decrypt[] = "DECRYPT\n" "\n" "Decrypt the object set by the last INPUT command and\n" "write the decrypted message to the object set by the\n" "last OUTPUT command."; static gpg_error_t cmd_decrypt (assuan_context_t ctx, char *line) { return _cmd_decrypt_verify (ctx, line, 0); } static const char hlp_decrypt_verify[] = "DECRYPT_VERIFY\n" "\n" "Decrypt the object set by the last INPUT command and\n" "verify any embedded signatures. Write the decrypted\n" "message to the object set by the last OUTPUT command."; static gpg_error_t cmd_decrypt_verify (assuan_context_t ctx, char *line) { return _cmd_decrypt_verify (ctx, line, 1); } static gpg_error_t _cmd_sign_encrypt (assuan_context_t ctx, char *line, int sign) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t inp_fd; char *inp_fn; assuan_fd_t out_fd; char *out_fn; gpgme_data_t inp_data = NULL; gpgme_data_t out_data = NULL; gpgme_encrypt_flags_t flags = 0; if (strstr (line, "--always-trust")) flags |= GPGME_ENCRYPT_ALWAYS_TRUST; if (strstr (line, "--no-encrypt-to")) flags |= GPGME_ENCRYPT_NO_ENCRYPT_TO; if (strstr (line, "--prepare")) flags |= GPGME_ENCRYPT_PREPARE; if (strstr (line, "--expect-sign")) flags |= GPGME_ENCRYPT_EXPECT_SIGN; if (strstr (line, "--no-compress")) flags |= GPGME_ENCRYPT_NO_COMPRESS; inp_fd = server->input_fd; inp_fn = server->input_filename; out_fd = server->output_fd; out_fn = server->output_filename; if (inp_fd != ASSUAN_INVALID_FD || inp_fn) { err = server_data_obj (inp_fd, inp_fn, 0, server->input_enc, &inp_data, &server->input_stream); if (err) return err; } if (out_fd != ASSUAN_INVALID_FD || out_fn) { err = server_data_obj (out_fd, out_fn, 1, server->output_enc, &out_data, &server->output_stream); if (err) { gpgme_data_release (inp_data); return err; } } err = gt_sign_encrypt (server->gt, flags, inp_data, out_data, sign); gpgme_data_release (inp_data); gpgme_data_release (out_data); server_reset_fds (server); return err; } static const char hlp_encrypt[] = "ENCRYPT [--always-trust] [--no-encrypt-to]\n" " [--no-compress] [--prepare] [--expect-sign]\n" "\n" "Encrypt the object set by the last INPUT command to\n" "the keys specified by previous RECIPIENT commands. \n" "Write the signed and encrypted message to the object\n" "set by the last OUTPUT command."; static gpg_error_t cmd_encrypt (assuan_context_t ctx, char *line) { return _cmd_sign_encrypt (ctx, line, 0); } static const char hlp_sign_encrypt[] = "SIGN_ENCRYPT [--always-trust] [--no-encrypt-to]\n" " [--no-compress] [--prepare] [--expect-sign]\n" "\n" "Sign the object set by the last INPUT command with the\n" "keys specified by previous SIGNER commands and encrypt\n" "it to the keys specified by previous RECIPIENT\n" "commands. Write the signed and encrypted message to\n" "the object set by the last OUTPUT command."; static gpg_error_t cmd_sign_encrypt (assuan_context_t ctx, char *line) { return _cmd_sign_encrypt (ctx, line, 1); } static const char hlp_sign[] = "SIGN [--clear|--detach]\n" "\n" "Sign the object set by the last INPUT command with the\n" "keys specified by previous SIGNER commands. Write the\n" "signed message to the object set by the last OUTPUT\n" "command. With `--clear`, generate a clear text\n" "signature. With `--detach`, generate a detached\n" "signature."; static gpg_error_t cmd_sign (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t inp_fd; char *inp_fn; assuan_fd_t out_fd; char *out_fn; gpgme_data_t inp_data; gpgme_data_t out_data; gpgme_sig_mode_t mode = GPGME_SIG_MODE_NORMAL; if (strstr (line, "--clear")) mode = GPGME_SIG_MODE_CLEAR; if (strstr (line, "--detach")) mode = GPGME_SIG_MODE_DETACH; inp_fd = server->input_fd; inp_fn = server->input_filename; if (inp_fd == ASSUAN_INVALID_FD && !inp_fn) return GPG_ERR_ASS_NO_INPUT; out_fd = server->output_fd; out_fn = server->output_filename; if (out_fd == ASSUAN_INVALID_FD && !out_fn) return GPG_ERR_ASS_NO_OUTPUT; err = server_data_obj (inp_fd, inp_fn, 0, server->input_enc, &inp_data, &server->input_stream); if (err) return err; err = server_data_obj (out_fd, out_fn, 1, server->output_enc, &out_data, &server->output_stream); if (err) { gpgme_data_release (inp_data); return err; } err = gt_sign (server->gt, inp_data, out_data, mode); gpgme_data_release (inp_data); gpgme_data_release (out_data); server_reset_fds (server); return err; } static const char hlp_verify[] = "VERIFY\n" "\n" "Verify signatures on the object set by the last INPUT\n" "and MESSAGE commands. If the message was encrypted,\n" "write the plaintext to the object set by the last\n" "OUTPUT command."; static gpg_error_t cmd_verify (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t inp_fd; assuan_fd_t msg_fd; assuan_fd_t out_fd; char *inp_fn; char *msg_fn; char *out_fn; gpgme_data_t inp_data; gpgme_data_t msg_data = NULL; gpgme_data_t out_data = NULL; (void)line; inp_fd = server->input_fd; inp_fn = server->input_filename; if (inp_fd == ASSUAN_INVALID_FD && !inp_fn) return GPG_ERR_ASS_NO_INPUT; msg_fd = server->message_fd; msg_fn = server->message_filename; out_fd = server->output_fd; out_fn = server->output_filename; err = server_data_obj (inp_fd, inp_fn, 0, server->input_enc, &inp_data, &server->input_stream); if (err) return err; if (msg_fd != ASSUAN_INVALID_FD || msg_fn) { err = server_data_obj (msg_fd, msg_fn, 0, server->message_enc, &msg_data, &server->message_stream); if (err) { gpgme_data_release (inp_data); return err; } } if (out_fd != ASSUAN_INVALID_FD || out_fn) { err = server_data_obj (out_fd, out_fn, 1, server->output_enc, &out_data, &server->output_stream); if (err) { gpgme_data_release (inp_data); gpgme_data_release (msg_data); return err; } } err = gt_verify (server->gt, inp_data, msg_data, out_data); gpgme_data_release (inp_data); if (msg_data) gpgme_data_release (msg_data); if (out_data) gpgme_data_release (out_data); server_reset_fds (server); return err; } static const char hlp_import[] = "IMPORT []\n" "\n" "With PATTERN, import the keys described by PATTERN.\n" "Without, read a key (or keys) from the object set by the\n" "last INPUT command."; static gpg_error_t cmd_import (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); if (line && *line) { char *fprs[2] = { line, NULL }; return gt_import_keys (server->gt, fprs); } else { gpg_error_t err; assuan_fd_t inp_fd; char *inp_fn; gpgme_data_t inp_data; inp_fd = server->input_fd; inp_fn = server->input_filename; if (inp_fd == ASSUAN_INVALID_FD && !inp_fn) return GPG_ERR_ASS_NO_INPUT; err = server_data_obj (inp_fd, inp_fn, 0, server->input_enc, &inp_data, &server->input_stream); if (err) return err; err = gt_import (server->gt, inp_data); gpgme_data_release (inp_data); server_reset_fds (server); return err; } } static const char hlp_export[] = "EXPORT [--extern] [--minimal] [--secret [--pkcs12] [--raw]] []\n" "\n" "Export the keys described by PATTERN. Write the\n" "the output to the object set by the last OUTPUT command."; static gpg_error_t cmd_export (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t out_fd; char *out_fn; gpgme_data_t out_data; gpgme_export_mode_t mode = 0; const char *pattern[2]; out_fd = server->output_fd; out_fn = server->output_filename; if (out_fd == ASSUAN_INVALID_FD && !out_fn) return GPG_ERR_ASS_NO_OUTPUT; err = server_data_obj (out_fd, out_fn, 1, server->output_enc, &out_data, &server->output_stream); if (err) return err; if (has_option (line, "--extern")) mode |= GPGME_EXPORT_MODE_EXTERN; if (has_option (line, "--minimal")) mode |= GPGME_EXPORT_MODE_MINIMAL; if (has_option (line, "--secret")) mode |= GPGME_EXPORT_MODE_SECRET; if (has_option (line, "--raw")) mode |= GPGME_EXPORT_MODE_RAW; if (has_option (line, "--pkcs12")) mode |= GPGME_EXPORT_MODE_PKCS12; line = skip_options (line); pattern[0] = line; pattern[1] = NULL; err = gt_export (server->gt, pattern, mode, out_data); gpgme_data_release (out_data); server_reset_fds (server); return err; } static gpg_error_t _cmd_genkey_write (gpgme_data_t data, const void *buf, size_t size) { while (size > 0) { gpgme_ssize_t writen = gpgme_data_write (data, buf, size); if (writen < 0 && errno != EAGAIN) return gpg_error_from_syserror (); else if (writen > 0) { buf = (void *) (((char *) buf) + writen); size -= writen; } } return 0; } static gpg_error_t cmd_genkey (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t inp_fd; char *inp_fn; assuan_fd_t out_fd; char *out_fn; gpgme_data_t inp_data; gpgme_data_t out_data = NULL; gpgme_data_t parms_data = NULL; const char *parms; (void)line; inp_fd = server->input_fd; inp_fn = server->input_filename; if (inp_fd == ASSUAN_INVALID_FD && !inp_fn) return GPG_ERR_ASS_NO_INPUT; out_fd = server->output_fd; out_fn = server->output_filename; err = server_data_obj (inp_fd, inp_fn, 0, server->input_enc, &inp_data, &server->input_stream); if (err) return err; if (out_fd != ASSUAN_INVALID_FD || out_fn) { err = server_data_obj (out_fd, out_fn, 1, server->output_enc, &out_data, &server->output_stream); if (err) { gpgme_data_release (inp_data); return err; } } /* Convert input data. */ err = gpgme_data_new (&parms_data); if (err) goto out; do { char buf[512]; gpgme_ssize_t readn = gpgme_data_read (inp_data, buf, sizeof (buf)); if (readn < 0) { err = gpg_error_from_syserror (); goto out; } else if (readn == 0) break; err = _cmd_genkey_write (parms_data, buf, readn); if (err) goto out; } while (1); err = _cmd_genkey_write (parms_data, "", 1); if (err) goto out; parms = gpgme_data_release_and_get_mem (parms_data, NULL); parms_data = NULL; if (! parms) { err = gpg_error (GPG_ERR_GENERAL); goto out; } err = gt_genkey (server->gt, parms, out_data, NULL); server_reset_fds (server); out: gpgme_data_release (inp_data); if (out_data) gpgme_data_release (out_data); if (parms_data) gpgme_data_release (parms_data); return err; } static gpg_error_t cmd_delete (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); int allow_secret = 0; const char optstr[] = "--allow-secret"; if (!strncasecmp (line, optstr, strlen (optstr))) { allow_secret = 1; line += strlen (optstr); while (*line && !spacep (line)) line++; } return gt_delete (server->gt, line, allow_secret); } static const char hlp_keylist[] = "KEYLIST [--secret-only] []\n" "\n" "List all certificates or only those specified by PATTERNS. Each\n" "pattern shall be a percent-plus escaped certificate specification."; static gpg_error_t cmd_keylist (assuan_context_t ctx, char *line) { #define MAX_CMD_KEYLIST_PATTERN 20 struct server *server = assuan_get_pointer (ctx); gpgme_tool_t gt = server->gt; struct result_xml_state state; gpg_error_t err; int secret_only = 0; int idx, indent=2; const char *pattern[MAX_CMD_KEYLIST_PATTERN+1]; const char optstr[] = "--secret-only"; char *p; if (!strncasecmp (line, optstr, strlen (optstr))) { secret_only = 1; line += strlen (optstr); while (*line && !spacep (line)) line++; } idx = 0; for (p=line; *p; line = p) { while (*p && *p != ' ') p++; if (*p) *p++ = 0; if (*line) { if (idx+1 == DIM (pattern)) return gpg_error (GPG_ERR_TOO_MANY); strcpy_escaped_plus (line, line); pattern[idx++] = line; } } pattern[idx] = NULL; gt_write_data (gt, xml_preamble1, strlen (xml_preamble1)); gt_write_data (gt, NULL, 0); gt_write_data (gt, xml_preamble2, strlen (xml_preamble2)); gt_write_data (gt, NULL, 0); result_init (&state, indent, (result_xml_write_cb_t) gt_write_data, gt); result_xml_tag_start (&state, "keylist", NULL); err = gt_keylist_start (server->gt, pattern, secret_only); while (! err) { gpgme_key_t key; gpgme_subkey_t subkey; gpgme_user_id_t uid; err = gt_keylist_next (server->gt, &key); if (gpg_err_code (err) == GPG_ERR_EOF) { err = 0; break; } else if (! err) { result_xml_tag_start (&state, "key", NULL); result_add_value (&state, "revoked", key->revoked); result_add_value (&state, "expired", key->expired); result_add_value (&state, "disabled", key->disabled); result_add_value (&state, "invalid", key->invalid); result_add_value (&state, "can-encrypt", key->can_encrypt); result_add_value (&state, "can-sign", key->can_sign); result_add_value (&state, "can-certify", key->can_certify); result_add_value (&state, "can-authenticate", key->can_authenticate); result_add_value (&state, "is-qualified", key->is_qualified); result_add_value (&state, "secret", key->secret); result_add_protocol (&state, "protocol", key->protocol); result_xml_tag_start (&state, "issuer", NULL); result_add_string (&state, "serial", key->issuer_serial); result_add_string (&state, "name", key->issuer_name); result_xml_tag_end (&state); /* issuer */ result_add_string (&state, "chain-id", key->chain_id); result_add_validity (&state, "owner-trust", key->owner_trust); result_xml_tag_start (&state, "subkeys", NULL); subkey = key->subkeys; while (subkey) { result_xml_tag_start (&state, "subkey", NULL); /* FIXME: more data */ result_add_keyid (&state, "keyid", subkey->keyid); if (subkey->fpr) result_add_fpr (&state, "fpr", subkey->fpr); result_add_value (&state, "secret", subkey->secret); result_add_value (&state, "is_cardkey", subkey->is_cardkey); if (subkey->card_number) result_add_string (&state, "card_number", subkey->card_number); if (subkey->curve) result_add_string (&state, "curve", subkey->curve); result_xml_tag_end (&state); /* subkey */ subkey = subkey->next; } result_xml_tag_end (&state); /* subkeys */ result_xml_tag_start (&state, "uids", NULL); uid = key->uids; while (uid) { result_xml_tag_start (&state, "uid", NULL); /* FIXME: more data */ result_add_string (&state, "uid", uid->uid); result_add_string (&state, "name", uid->name); result_add_string (&state, "email", uid->email); result_add_string (&state, "comment", uid->comment); result_xml_tag_end (&state); /* uid */ uid = uid->next; } result_xml_tag_end (&state); /* uids */ result_xml_tag_end (&state); /* key */ gpgme_key_unref (key); } } result_xml_tag_end (&state); /* keylist */ gt_write_data (gt, xml_end, strlen (xml_end)); server_reset_fds (server); return err; } static const char hlp_getauditlog[] = "GETAUDITLOG [--html] [--with-help]\n" "\n" "Call the function gpgme_op_getauditlog with the given flags. Write\n" "the output to the object set by the last OUTPUT command."; static gpg_error_t cmd_getauditlog (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t out_fd; char *out_fn; gpgme_data_t out_data; unsigned int flags = 0; out_fd = server->output_fd; out_fn = server->output_filename; if (out_fd == ASSUAN_INVALID_FD && !out_fn) return GPG_ERR_ASS_NO_OUTPUT; err = server_data_obj (out_fd, out_fn, 1, server->output_enc, &out_data, &server->output_stream); if (err) return err; if (strstr (line, "--html")) flags |= GPGME_AUDITLOG_HTML; if (strstr (line, "--with-help")) flags |= GPGME_AUDITLOG_WITH_HELP; err = gt_getauditlog (server->gt, out_data, flags); gpgme_data_release (out_data); server_reset_fds (server); return err; } static gpg_error_t cmd_vfs_mount (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); char *mount_dir; gpg_error_t err; mount_dir = strchr (line, ' '); if (mount_dir) { *(mount_dir++) = '\0'; while (*mount_dir == ' ') mount_dir++; } err = gt_vfs_mount (server->gt, line, mount_dir, 0); return err; } static gpg_error_t cmd_vfs_create (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; char *end; end = strchr (line, ' '); if (end) { *(end++) = '\0'; while (*end == ' ') end++; } err = gt_vfs_create (server->gt, line, 0); return err; } static const char hlp_passwd[] = "PASSWD \n" "\n" "Ask the backend to change the passphrase for the key\n" "specified by USER-ID."; static gpg_error_t cmd_passwd (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); return gt_passwd (server->gt, line); } static gpg_error_t cmd_result (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); (void)line; return gt_result (server->gt, GT_RESULT_ALL); } /* STRERROR */ static gpg_error_t cmd_strerror (assuan_context_t ctx, char *line) { gpg_error_t err; char buf[100]; err = atoi (line); snprintf (buf, sizeof (buf), "%s <%s>", gpgme_strerror (err), gpgme_strsource (err)); return assuan_send_data (ctx, buf, strlen (buf)); } static gpg_error_t cmd_pubkey_algo_name (assuan_context_t ctx, char *line) { gpgme_pubkey_algo_t algo; char buf[100]; algo = atoi (line); snprintf (buf, sizeof (buf), "%s", gpgme_pubkey_algo_name (algo)); return assuan_send_data (ctx, buf, strlen (buf)); } static gpg_error_t cmd_hash_algo_name (assuan_context_t ctx, char *line) { gpgme_hash_algo_t algo; char buf[100]; algo = atoi (line); snprintf (buf, sizeof (buf), "%s", gpgme_hash_algo_name (algo)); return assuan_send_data (ctx, buf, strlen (buf)); } static const char hlp_identify[] = "IDENTIFY\n" "\n" "Identify the type of data set with the INPUT command."; static gpg_error_t cmd_identify (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t inp_fd; char *inp_fn; gpgme_data_t inp_data; (void)line; inp_fd = server->input_fd; inp_fn = server->input_filename; if (inp_fd == ASSUAN_INVALID_FD && !inp_fn) return GPG_ERR_ASS_NO_INPUT; err = server_data_obj (inp_fd, inp_fn, 0, server->input_enc, &inp_data, &server->input_stream); if (err) return err; err = gt_identify (server->gt, inp_data); gpgme_data_release (inp_data); server_reset_fds (server); return err; } static const char hlp_spawn[] = "SPAWN PGM [args]\n" "\n" "Run program PGM with stdin connected to the INPUT source;\n" "stdout and stderr to the OUTPUT source."; static gpg_error_t cmd_spawn (assuan_context_t ctx, char *line) { struct server *server = assuan_get_pointer (ctx); gpg_error_t err; assuan_fd_t inp_fd; char *inp_fn; assuan_fd_t out_fd; char *out_fn; gpgme_data_t inp_data = NULL; gpgme_data_t out_data = NULL; inp_fd = server->input_fd; inp_fn = server->input_filename; out_fd = server->output_fd; out_fn = server->output_filename; if (inp_fd != ASSUAN_INVALID_FD || inp_fn) { err = server_data_obj (inp_fd, inp_fn, 0, server->input_enc, &inp_data, &server->input_stream); if (err) return err; } if (out_fd != ASSUAN_INVALID_FD || out_fn) { err = server_data_obj (out_fd, out_fn, 1, server->output_enc, &out_data, &server->output_stream); if (err) { gpgme_data_release (inp_data); return err; } } err = gt_spawn (server->gt, line, inp_data, out_data); gpgme_data_release (inp_data); gpgme_data_release (out_data); server_reset_fds (server); return err; } /* Tell the assuan library about our commands. */ static gpg_error_t register_commands (assuan_context_t ctx) { gpg_error_t err; static struct { const char *name; assuan_handler_t handler; const char * const help; } table[] = { /* RESET, BYE are implicit. */ { "VERSION", cmd_version, hlp_version }, /* TODO: Set engine info. */ { "ENGINE", cmd_engine, hlp_engine }, { "PROTOCOL", cmd_protocol, hlp_protocol }, { "SUB_PROTOCOL", cmd_sub_protocol, hlp_sub_protocol }, { "PINENTRY_MODE", cmd_pinentry_mode, hlp_pinentry_mode }, { "ARMOR", cmd_armor, hlp_armor }, { "TEXTMODE", cmd_textmode, hlp_textmode }, { "INCLUDE_CERTS", cmd_include_certs, hlp_include_certs }, { "KEYLIST_MODE", cmd_keylist_mode, hlp_keylist_mode }, { "INPUT", cmd_input, hlp_input }, { "OUTPUT", cmd_output, hlp_output }, { "MESSAGE", cmd_message, hlp_message }, { "RECIPIENT", cmd_recipient, hlp_recipient }, { "SIGNER", cmd_signer, hlp_signer }, { "SIGNERS_CLEAR", cmd_signers_clear, hlp_signers_clear }, /* TODO: SIGNOTATION missing. */ /* TODO: Could add wait interface if we allow more than one context */ /* and add _START variants. */ /* TODO: Could add data interfaces if we allow multiple data objects. */ { "DECRYPT", cmd_decrypt, hlp_decrypt }, { "DECRYPT_VERIFY", cmd_decrypt_verify, hlp_decrypt_verify }, { "ENCRYPT", cmd_encrypt, hlp_encrypt }, { "ENCRYPT_SIGN", cmd_sign_encrypt, hlp_sign_encrypt }, { "SIGN_ENCRYPT", cmd_sign_encrypt, hlp_sign_encrypt }, { "SIGN", cmd_sign, hlp_sign }, { "VERIFY", cmd_verify, hlp_verify }, { "IMPORT", cmd_import, hlp_import }, { "EXPORT", cmd_export, hlp_export }, { "GENKEY", cmd_genkey }, { "DELETE", cmd_delete }, /* TODO: EDIT, CARD_EDIT (with INQUIRE) */ { "KEYLIST", cmd_keylist, hlp_keylist }, { "LISTKEYS", cmd_keylist, hlp_keylist }, /* TODO: TRUSTLIST, TRUSTLIST_EXT */ { "GETAUDITLOG", cmd_getauditlog, hlp_getauditlog }, /* TODO: ASSUAN */ { "VFS_MOUNT", cmd_vfs_mount }, { "MOUNT", cmd_vfs_mount }, { "VFS_CREATE", cmd_vfs_create }, { "CREATE", cmd_vfs_create }, /* TODO: GPGCONF */ { "RESULT", cmd_result }, { "STRERROR", cmd_strerror }, { "PUBKEY_ALGO_NAME", cmd_pubkey_algo_name }, { "HASH_ALGO_NAME", cmd_hash_algo_name }, { "PASSWD", cmd_passwd, hlp_passwd }, { "IDENTIFY", cmd_identify, hlp_identify }, { "SPAWN", cmd_spawn, hlp_spawn }, { NULL } }; int idx; for (idx = 0; table[idx].name; idx++) { err = assuan_register_command (ctx, table[idx].name, table[idx].handler, table[idx].help); if (err) return err; } return 0; } void gpgme_server (gpgme_tool_t gt) { gpg_error_t err; assuan_fd_t filedes[2]; struct server server; static const char hello[] = ("GPGME-Tool " VERSION " ready"); memset (&server, 0, sizeof (server)); server.input_fd = ASSUAN_INVALID_FD; server.output_fd = ASSUAN_INVALID_FD; server.message_fd = ASSUAN_INVALID_FD; server.input_enc = GPGME_DATA_ENCODING_NONE; server.output_enc = GPGME_DATA_ENCODING_NONE; server.message_enc = GPGME_DATA_ENCODING_NONE; server.gt = gt; gt->write_status = server_write_status; gt->write_status_hook = &server; gt->write_data = server_write_data; gt->write_data_hook = &server; /* We use a pipe based server so that we can work from scripts. * assuan_init_pipe_server will automagically detect when we are * called with a socketpair and ignore FILEDES in this case. */ filedes[0] = assuan_fdopen (0); filedes[1] = assuan_fdopen (1); err = assuan_new (&server.assuan_ctx); if (err) log_error (1, err, "can't create assuan context"); assuan_set_pointer (server.assuan_ctx, &server); err = assuan_init_pipe_server (server.assuan_ctx, filedes); if (err) log_error (1, err, "can't initialize assuan server"); err = register_commands (server.assuan_ctx); if (err) log_error (1, err, "can't register assuan commands"); assuan_set_hello_line (server.assuan_ctx, hello); assuan_register_reset_notify (server.assuan_ctx, reset_notify); #define DBG_ASSUAN 0 if (DBG_ASSUAN) assuan_set_log_stream (server.assuan_ctx, log_stream); for (;;) { err = assuan_accept (server.assuan_ctx); if (err == -1) break; else if (err) { log_error (0, err, "assuan accept problem"); break; } err = assuan_process (server.assuan_ctx); if (err) log_error (0, err, "assuan processing failed"); } assuan_release (server.assuan_ctx); } static const char * my_strusage( int level ) { const char *p; switch (level) { case 11: p = "gpgme-tool"; break; case 13: p = PACKAGE_VERSION; break; case 14: p = "Copyright (C) 2015 g10 Code GmbH"; break; case 19: p = "Please report bugs to <" PACKAGE_BUGREPORT ">.\n"; break; case 1: case 40: p = "Usage: gpgme-tool [OPTIONS] [COMMANDS]"; break; case 41: p = "GPGME Tool -- Assuan server exposing GPGME operations\n"; break; case 42: p = "1"; /* Flag print 40 as part of 41. */ break; default: p = NULL; break; } return p; } int main (int argc, char *argv[]) { static ARGPARSE_OPTS opts[] = { ARGPARSE_c ('s', "server", "Server mode"), ARGPARSE_s_s(501, "gpg-binary", "|FILE|Use FILE for the GPG backend"), ARGPARSE_c (502, "lib-version", "Show library version"), ARGPARSE_end() }; ARGPARSE_ARGS pargs = { &argc, &argv, 0 }; enum { CMD_DEFAULT, CMD_SERVER, CMD_LIBVERSION } cmd = CMD_DEFAULT; const char *gpg_binary = NULL; struct gpgme_tool gt; gpg_error_t err; int needgt = 1; set_strusage (my_strusage); #ifdef HAVE_SETLOCALE setlocale (LC_ALL, ""); #endif gpgme_check_version (NULL); #ifdef LC_CTYPE gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); #endif #ifdef LC_MESSAGES gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); #endif log_init (); while (arg_parse (&pargs, opts)) { switch (pargs.r_opt) { case 's': cmd = CMD_SERVER; break; case 501: gpg_binary = pargs.r.ret_str; break; case 502: cmd = CMD_LIBVERSION; break; default: pargs.err = ARGPARSE_PRINT_WARNING; break; } } if (cmd == CMD_LIBVERSION) needgt = 0; if (needgt && gpg_binary) { if (access (gpg_binary, X_OK)) err = gpg_error_from_syserror (); else err = gpgme_set_engine_info (GPGME_PROTOCOL_OpenPGP, gpg_binary, NULL); if (err) log_error (1, err, "error witching OpenPGP engine to '%s'", gpg_binary); } if (needgt) gt_init (>); switch (cmd) { case CMD_DEFAULT: case CMD_SERVER: gpgme_server (>); break; case CMD_LIBVERSION: printf ("Version from header: %s (0x%06x)\n", GPGME_VERSION, GPGME_VERSION_NUMBER); printf ("Version from binary: %s\n", gpgme_check_version (NULL)); printf ("Copyright blurb ...:%s\n", gpgme_check_version ("\x01\x01")); break; } if (needgt) gpgme_release (gt.ctx); return 0; } diff --git a/src/gpgme-w32spawn.c b/src/gpgme-w32spawn.c index 868dbd57..64913b01 100644 --- a/src/gpgme-w32spawn.c +++ b/src/gpgme-w32spawn.c @@ -1,487 +1,488 @@ /* gpgme-w32spawn.c - Wrapper to spawn a process under Windows. - Copyright (C) 2008 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2008 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #include #include #ifdef HAVE_SYS_TYPES_H # include #endif #ifdef HAVE_SYS_STAT_H # include #endif #include #include #include "priv-io.h" /* Name of this program. */ #define PGM "gpgme-w32spawn" static char * build_commandline (char **argv) { int i; int n = 0; char *buf; char *p; /* We have to quote some things because under Windows the program parses the commandline and does some unquoting. We enclose the whole argument in double-quotes, and escape literal double-quotes as well as backslashes with a backslash. We end up with a trailing space at the end of the line, but that is harmless. */ for (i = 0; argv[i]; i++) { p = argv[i]; /* The leading double-quote. */ n++; while (*p) { /* An extra one for each literal that must be escaped. */ if (*p == '\\' || *p == '"') n++; n++; p++; } /* The trailing double-quote and the delimiter. */ n += 2; } /* And a trailing zero. */ n++; buf = p = malloc (n); if (!buf) return NULL; for (i = 0; argv[i]; i++) { char *argvp = argv[i]; *(p++) = '"'; while (*argvp) { if (*argvp == '\\' || *argvp == '"') *(p++) = '\\'; *(p++) = *(argvp++); } *(p++) = '"'; *(p++) = ' '; } *(p++) = 0; return buf; } int my_spawn (char **argv, struct spawn_fd_item_s *fd_list, unsigned int flags) { SECURITY_ATTRIBUTES sec_attr; PROCESS_INFORMATION pi = { NULL, /* returns process handle */ 0, /* returns primary thread handle */ 0, /* returns pid */ 0 /* returns tid */ }; STARTUPINFO si; char *envblock = NULL; int cr_flags = CREATE_DEFAULT_ERROR_MODE | GetPriorityClass (GetCurrentProcess ()); int i; char *arg_string; int duped_stdin = 0; int duped_stdout = 0; int duped_stderr = 0; HANDLE hnul = INVALID_HANDLE_VALUE; i = 0; while (argv[i]) { fprintf (stderr, PGM": argv[%2i] = %s\n", i, argv[i]); i++; } memset (&sec_attr, 0, sizeof sec_attr); sec_attr.nLength = sizeof sec_attr; sec_attr.bInheritHandle = FALSE; arg_string = build_commandline (argv); if (!arg_string) return -1; memset (&si, 0, sizeof si); si.cb = sizeof (si); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; si.wShowWindow = (flags & IOSPAWN_FLAG_SHOW_WINDOW) ? SW_SHOW : SW_HIDE; si.hStdInput = GetStdHandle (STD_INPUT_HANDLE); si.hStdOutput = GetStdHandle (STD_OUTPUT_HANDLE); si.hStdError = GetStdHandle (STD_ERROR_HANDLE); fprintf (stderr, PGM": spawning: %s\n", arg_string); for (i = 0; fd_list[i].fd != -1; i++) { /* The handle already is inheritable. */ if (fd_list[i].dup_to == 0) { si.hStdInput = (HANDLE) fd_list[i].peer_name; duped_stdin = 1; fprintf (stderr, PGM": dup 0x%x to stdin\n", fd_list[i].peer_name); } else if (fd_list[i].dup_to == 1) { si.hStdOutput = (HANDLE) fd_list[i].peer_name; duped_stdout = 1; fprintf (stderr, PGM": dup 0x%x to stdout\n", fd_list[i].peer_name); } else if (fd_list[i].dup_to == 2) { si.hStdError = (HANDLE) fd_list[i].peer_name; duped_stderr = 1; fprintf (stderr, PGM":dup 0x%x to stderr\n", fd_list[i].peer_name); } } if (!duped_stdin || !duped_stdout || !duped_stderr) { SECURITY_ATTRIBUTES sa; memset (&sa, 0, sizeof sa); sa.nLength = sizeof sa; sa.bInheritHandle = TRUE; hnul = CreateFile ("nul", GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE, &sa, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hnul == INVALID_HANDLE_VALUE) { free (arg_string); /* FIXME: Should translate the error code. */ errno = EIO; return -1; } /* Make sure that the process has a connected stdin. */ if (!duped_stdin) si.hStdInput = hnul; /* Make sure that the process has a connected stdout. */ if (!duped_stdout) si.hStdOutput = hnul; /* We normally don't want all the normal output. */ if (!duped_stderr) si.hStdError = hnul; } cr_flags |= CREATE_SUSPENDED; if (!CreateProcessA (argv[0], arg_string, &sec_attr, /* process security attributes */ &sec_attr, /* thread security attributes */ TRUE, /* inherit handles */ cr_flags, /* creation flags */ envblock, /* environment */ NULL, /* use current drive/directory */ &si, /* startup information */ &pi)) /* returns process information */ { free (arg_string); /* FIXME: Should translate the error code. */ errno = EIO; return -1; } free (arg_string); /* Close the /dev/nul handle if used. */ if (hnul != INVALID_HANDLE_VALUE) CloseHandle (hnul); for (i = 0; fd_list[i].fd != -1; i++) CloseHandle ((HANDLE) fd_list[i].fd); if (flags & IOSPAWN_FLAG_ALLOW_SET_FG) { static int initialized; static BOOL (WINAPI * func)(DWORD); void *handle; if (!initialized) { /* Available since W2000; thus we dynload it. */ initialized = 1; handle = LoadLibrary ("user32.dll"); if (handle) { func = GetProcAddress (handle, "AllowSetForegroundWindow"); if (!func) FreeLibrary (handle); } } if (func) { int rc = func (pi.dwProcessId); fprintf (stderr, PGM": AllowSetForegroundWindow(%d): rc=%d\n", (int)pi.dwProcessId, rc); } } ResumeThread (pi.hThread); CloseHandle (pi.hThread); CloseHandle (pi.hProcess); return 0; } #define MAX_TRANS 10 int translate_get_from_file (const char *trans_file, struct spawn_fd_item_s *fd_list, unsigned int *r_flags) { /* Hold roughly MAX_TRANS triplets of 64 bit numbers in hex notation: "0xFEDCBA9876543210". 10*19*4 - 1 = 759. This plans ahead for a time when a HANDLE is 64 bit. */ #define BUFFER_MAX 810 char line[BUFFER_MAX + 1]; char *linep; int idx; int res; int fd; *r_flags = 0; fd = open (trans_file, O_RDONLY); if (fd < 0) return -1; /* We always read one line from stdin. */ res = read (fd, line, BUFFER_MAX); close (fd); if (res < 0) return -1; line[BUFFER_MAX] = '\0'; linep = strchr (line, '\n'); if (linep) { if (linep > line && linep[-1] == '\r') linep--; *linep = '\0'; } linep = line; /* Now start to read mapping pairs. */ for (idx = 0; idx < MAX_TRANS; idx++) { unsigned long from; long dup_to; unsigned long to; unsigned long loc; char *tail; /* FIXME: Maybe could use scanf. */ while (isspace (*((unsigned char *)linep))) linep++; if (*linep == '\0') break; if (!idx && *linep == '~') { /* Spawn flags have been passed. */ linep++; *r_flags = strtoul (linep, &tail, 0); if (tail == NULL || ! (*tail == '\0' || isspace (*tail))) break; linep = tail; while (isspace (*((unsigned char *)linep))) linep++; if (*linep == '\0') break; } from = strtoul (linep, &tail, 0); if (tail == NULL || ! (*tail == '\0' || isspace (*tail))) break; linep = tail; while (isspace (*linep)) linep++; if (*linep == '\0') break; dup_to = strtol (linep, &tail, 0); if (tail == NULL || ! (*tail == '\0' || isspace (*tail))) break; linep = tail; while (isspace (*linep)) linep++; if (*linep == '\0') break; to = strtoul (linep, &tail, 0); if (tail == NULL || ! (*tail == '\0' || isspace (*tail))) break; linep = tail; while (isspace (*linep)) linep++; if (*linep == '\0') break; loc = strtoul (linep, &tail, 0); if (tail == NULL || ! (*tail == '\0' || isspace (*tail))) break; linep = tail; fd_list[idx].fd = from; fd_list[idx].dup_to = dup_to; fd_list[idx].peer_name = to; fd_list[idx].arg_loc = loc; } fd_list[idx].fd = -1; fd_list[idx].dup_to = -1; fd_list[idx].peer_name = -1; fd_list[idx].arg_loc = 0; return 0; } /* Read the translated handles from TRANS_FILE and do a substitution in ARGV. Returns the new argv and the list of substitutions in FD_LIST (which must be MAX_TRANS+1 large). */ char ** translate_handles (const char *trans_file, const char * const *argv, struct spawn_fd_item_s *fd_list, unsigned int *r_flags) { int res; int idx; int n_args; char **args; res = translate_get_from_file (trans_file, fd_list, r_flags); if (res < 0) return NULL; for (idx = 0; argv[idx]; idx++) ; args = malloc (sizeof (*args) * (idx + 1)); for (idx = 0; argv[idx]; idx++) { args[idx] = strdup (argv[idx]); if (!args[idx]) return NULL; } args[idx] = NULL; n_args = idx; for (idx = 0; fd_list[idx].fd != -1; idx++) { char buf[25]; int aidx; aidx = fd_list[idx].arg_loc; if (aidx == 0) continue; if (aidx >= n_args) { fprintf (stderr, PGM": translation file does not match args\n"); return NULL; } args[aidx] = malloc (sizeof (buf)); /* We currently disable translation for stdin/stdout/stderr. We assume that the spawned program handles 0/1/2 specially already. FIXME: Check if this is true. */ if (!args[idx] || fd_list[idx].dup_to != -1) return NULL; /* NOTE: Here is the part where application specific knowledge comes in. GPGME/GnuPG uses two forms of descriptor specification, a plain number and a "-&" form. */ if (argv[aidx][0] == '-' && argv[aidx][1] == '&') snprintf (args[aidx], sizeof (buf), "-&%d", fd_list[idx].peer_name); else snprintf (args[aidx], sizeof (buf), "%d", fd_list[idx].peer_name); } return args; } int main (int argc, const char * const *argv) { int rc = 0; char **argv_spawn; struct spawn_fd_item_s fd_list[MAX_TRANS + 1]; unsigned int flags; if (argc < 3) { rc = 2; goto leave; } argv_spawn = translate_handles (argv[1], &argv[2], fd_list, &flags); if (!argv_spawn) { rc = 2; goto leave; } /* Using execv does not replace the existing program image, but spawns a new one and daemonizes it, confusing the command line interpreter. So we have to use spawnv. */ rc = my_spawn (argv_spawn, fd_list, flags); if (rc < 0) { fprintf (stderr, PGM": executing `%s' failed: %s\n", argv[0], strerror (errno)); rc = 2; goto leave; } leave: if (rc) fprintf (stderr, PGM": internal error\n"); /* Always try to delete the temporary file. */ if (argc >= 2) { if (DeleteFile (argv[1]) == 0) fprintf (stderr, PGM": failed to delete %s: ec=%ld\n", argv[1], GetLastError ()); } return rc; } diff --git a/src/gpgme.c b/src/gpgme.c index 3d72f695..4dbec0c7 100644 --- a/src/gpgme.c +++ b/src/gpgme.c @@ -1,1293 +1,1294 @@ /* gpgme.c - GnuPG Made Easy. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2012, - 2014, 2015 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2012, + * 2014, 2015 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include #ifdef HAVE_LOCALE_H #include #endif #include "util.h" #include "context.h" #include "ops.h" #include "wait.h" #include "debug.h" #include "priv-io.h" #include "sys-util.h" #include "mbox-util.h" /* The default locale. */ DEFINE_STATIC_LOCK (def_lc_lock); static char *def_lc_ctype; static char *def_lc_messages; gpgme_error_t _gpgme_selftest = GPG_ERR_NOT_OPERATIONAL; /* Protects all reference counters in result structures. All other accesses to a result structure are read only. */ DEFINE_STATIC_LOCK (result_ref_lock); /* Set the global flag NAME to VALUE. Return 0 on success. Note that this function does not use gpgme_error and thus a non-zero return value merely means "error". Certain flags may be set before gpgme_check_version is called. See the manual for a description of supported flags. The caller must assure that this function is called only by one thread at a time. */ int gpgme_set_global_flag (const char *name, const char *value) { if (!name || !value) return -1; else if (!strcmp (name, "debug")) return _gpgme_debug_set_debug_envvar (value); else if (!strcmp (name, "disable-gpgconf")) { _gpgme_dirinfo_disable_gpgconf (); return 0; } else if (!strcmp (name, "require-gnupg")) return _gpgme_set_engine_minimal_version (value); else if (!strcmp (name, "gpgconf-name")) return _gpgme_set_default_gpgconf_name (value); else if (!strcmp (name, "gpg-name")) return _gpgme_set_default_gpg_name (value); else if (!strcmp (name, "w32-inst-dir")) return _gpgme_set_override_inst_dir (value); else return -1; } /* Create a new context as an environment for GPGME crypto operations. */ gpgme_error_t gpgme_new (gpgme_ctx_t *r_ctx) { gpgme_error_t err; gpgme_ctx_t ctx; TRACE_BEG (DEBUG_CTX, "gpgme_new", r_ctx); if (_gpgme_selftest) return TRACE_ERR (_gpgme_selftest); if (!r_ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); ctx = calloc (1, sizeof *ctx); if (!ctx) return TRACE_ERR (gpg_error_from_syserror ()); INIT_LOCK (ctx->lock); err = _gpgme_engine_info_copy (&ctx->engine_info); if (!err && !ctx->engine_info) err = gpg_error (GPG_ERR_NO_ENGINE); if (err) { free (ctx); return TRACE_ERR (err); } ctx->keylist_mode = GPGME_KEYLIST_MODE_LOCAL; ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT; ctx->protocol = GPGME_PROTOCOL_OpenPGP; ctx->sub_protocol = GPGME_PROTOCOL_DEFAULT; _gpgme_fd_table_init (&ctx->fdt); LOCK (def_lc_lock); if (def_lc_ctype) { ctx->lc_ctype = strdup (def_lc_ctype); if (!ctx->lc_ctype) { int saved_err = gpg_error_from_syserror (); UNLOCK (def_lc_lock); _gpgme_engine_info_release (ctx->engine_info); free (ctx); return TRACE_ERR (saved_err); } } else def_lc_ctype = NULL; if (def_lc_messages) { ctx->lc_messages = strdup (def_lc_messages); if (!ctx->lc_messages) { int saved_err = gpg_error_from_syserror (); UNLOCK (def_lc_lock); if (ctx->lc_ctype) free (ctx->lc_ctype); _gpgme_engine_info_release (ctx->engine_info); free (ctx); return TRACE_ERR (saved_err); } } else def_lc_messages = NULL; UNLOCK (def_lc_lock); *r_ctx = ctx; return TRACE_SUC1 ("ctx=%p", ctx); } gpgme_error_t _gpgme_cancel_with_err (gpgme_ctx_t ctx, gpg_error_t ctx_err, gpg_error_t op_err) { gpgme_error_t err; struct gpgme_io_event_done_data data; TRACE_BEG2 (DEBUG_CTX, "_gpgme_cancel_with_err", ctx, "ctx_err=%i, op_err=%i", ctx_err, op_err); if (ctx_err) { err = _gpgme_engine_cancel (ctx->engine); if (err) return TRACE_ERR (err); } else { err = _gpgme_engine_cancel_op (ctx->engine); if (err) return TRACE_ERR (err); } data.err = ctx_err; data.op_err = op_err; _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &data); return TRACE_ERR (0); } /* Cancel a pending asynchronous operation. */ gpgme_error_t gpgme_cancel (gpgme_ctx_t ctx) { gpg_error_t err; TRACE_BEG (DEBUG_CTX, "gpgme_cancel", ctx); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_cancel_with_err (ctx, gpg_error (GPG_ERR_CANCELED), 0); return TRACE_ERR (err); } /* Cancel a pending operation asynchronously. */ gpgme_error_t gpgme_cancel_async (gpgme_ctx_t ctx) { TRACE_BEG (DEBUG_CTX, "gpgme_cancel_async", ctx); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); LOCK (ctx->lock); ctx->canceled = 1; UNLOCK (ctx->lock); return TRACE_ERR (0); } /* Release all resources associated with the given context. */ void gpgme_release (gpgme_ctx_t ctx) { TRACE (DEBUG_CTX, "gpgme_release", ctx); if (!ctx) return; _gpgme_engine_release (ctx->engine); ctx->engine = NULL; _gpgme_fd_table_deinit (&ctx->fdt); _gpgme_release_result (ctx); _gpgme_signers_clear (ctx); _gpgme_sig_notation_clear (ctx); free (ctx->sender); free (ctx->signers); free (ctx->lc_ctype); free (ctx->lc_messages); free (ctx->override_session_key); free (ctx->request_origin); free (ctx->auto_key_locate); free (ctx->trust_model); _gpgme_engine_info_release (ctx->engine_info); ctx->engine_info = NULL; DESTROY_LOCK (ctx->lock); free (ctx); } void gpgme_result_ref (void *result) { struct ctx_op_data *data; if (! result) return; data = (void*)((char*)result - sizeof (struct ctx_op_data)); assert (data->magic == CTX_OP_DATA_MAGIC); LOCK (result_ref_lock); data->references++; UNLOCK (result_ref_lock); } void gpgme_result_unref (void *result) { struct ctx_op_data *data; if (! result) return; data = (void*)((char*)result - sizeof (struct ctx_op_data)); assert (data->magic == CTX_OP_DATA_MAGIC); LOCK (result_ref_lock); if (--data->references) { UNLOCK (result_ref_lock); return; } UNLOCK (result_ref_lock); if (data->cleanup) (*data->cleanup) (data->hook); free (data); } void _gpgme_release_result (gpgme_ctx_t ctx) { struct ctx_op_data *data = ctx->op_data; while (data) { struct ctx_op_data *next_data = data->next; data->next = NULL; gpgme_result_unref (data->hook); data = next_data; } ctx->op_data = NULL; } gpgme_error_t gpgme_set_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol) { TRACE_BEG2 (DEBUG_CTX, "gpgme_set_protocol", ctx, "protocol=%i (%s)", protocol, gpgme_get_protocol_name (protocol) ? gpgme_get_protocol_name (protocol) : "invalid"); if (protocol != GPGME_PROTOCOL_OpenPGP && protocol != GPGME_PROTOCOL_CMS && protocol != GPGME_PROTOCOL_GPGCONF && protocol != GPGME_PROTOCOL_ASSUAN && protocol != GPGME_PROTOCOL_G13 && protocol != GPGME_PROTOCOL_UISERVER && protocol != GPGME_PROTOCOL_SPAWN) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (ctx->protocol != protocol) { /* Shut down the engine when switching protocols. */ if (ctx->engine) { TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine); _gpgme_engine_release (ctx->engine); ctx->engine = NULL; } ctx->protocol = protocol; } return TRACE_ERR (0); } gpgme_protocol_t gpgme_get_protocol (gpgme_ctx_t ctx) { TRACE2 (DEBUG_CTX, "gpgme_get_protocol", ctx, "ctx->protocol=%i (%s)", ctx->protocol, gpgme_get_protocol_name (ctx->protocol) ? gpgme_get_protocol_name (ctx->protocol) : "invalid"); return ctx->protocol; } gpgme_error_t gpgme_set_sub_protocol (gpgme_ctx_t ctx, gpgme_protocol_t protocol) { TRACE2 (DEBUG_CTX, "gpgme_set_sub_protocol", ctx, "protocol=%i (%s)", protocol, gpgme_get_protocol_name (protocol) ? gpgme_get_protocol_name (protocol) : "invalid"); if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); ctx->sub_protocol = protocol; return 0; } gpgme_protocol_t gpgme_get_sub_protocol (gpgme_ctx_t ctx) { TRACE2 (DEBUG_CTX, "gpgme_get_sub_protocol", ctx, "ctx->sub_protocol=%i (%s)", ctx->sub_protocol, gpgme_get_protocol_name (ctx->sub_protocol) ? gpgme_get_protocol_name (ctx->sub_protocol) : "invalid"); return ctx->sub_protocol; } const char * gpgme_get_protocol_name (gpgme_protocol_t protocol) { switch (protocol) { case GPGME_PROTOCOL_OpenPGP: return "OpenPGP"; case GPGME_PROTOCOL_CMS: return "CMS"; case GPGME_PROTOCOL_GPGCONF: return "GPGCONF"; case GPGME_PROTOCOL_ASSUAN: return "Assuan"; case GPGME_PROTOCOL_G13: return "G13"; case GPGME_PROTOCOL_UISERVER: return "UIServer"; case GPGME_PROTOCOL_SPAWN: return "Spawn"; case GPGME_PROTOCOL_DEFAULT: return "default"; case GPGME_PROTOCOL_UNKNOWN: return "unknown"; default: return NULL; } } /* Store the sender's address in the context. ADDRESS is addr-spec of * mailbox but my also be a complete mailbox, in which case this * function extracts the addr-spec from it. Returns 0 on success or * an error code if no valid addr-spec could be extracted from * ADDRESS. */ gpgme_error_t gpgme_set_sender (gpgme_ctx_t ctx, const char *address) { char *p = NULL; TRACE_BEG1 (DEBUG_CTX, "gpgme_set_sender", ctx, "sender='%s'", address?address:"(null)"); if (!ctx || (address && !(p = _gpgme_mailbox_from_userid (address)))) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); free (ctx->sender); ctx->sender = p; return TRACE_ERR (0); } /* Return the sender's address (addr-spec part) from the context or * NULL if none was set. The returned value is valid as long as the * CTX is valid and gpgme_set_sender has not been used. */ const char * gpgme_get_sender (gpgme_ctx_t ctx) { TRACE1 (DEBUG_CTX, "gpgme_get_sender", ctx, "sender='%s'", ctx?ctx->sender:""); return ctx->sender; } /* Enable or disable the use of an ascii armor for all output. */ void gpgme_set_armor (gpgme_ctx_t ctx, int use_armor) { TRACE2 (DEBUG_CTX, "gpgme_set_armor", ctx, "use_armor=%i (%s)", use_armor, use_armor ? "yes" : "no"); if (!ctx) return; ctx->use_armor = !!use_armor; } /* Return the state of the armor flag. */ int gpgme_get_armor (gpgme_ctx_t ctx) { TRACE2 (DEBUG_CTX, "gpgme_get_armor", ctx, "ctx->use_armor=%i (%s)", ctx->use_armor, ctx->use_armor ? "yes" : "no"); return ctx->use_armor; } /* Set the flag NAME for CTX to VALUE. Please consult the manual for * a description of the flags. */ gpgme_error_t gpgme_set_ctx_flag (gpgme_ctx_t ctx, const char *name, const char *value) { gpgme_error_t err = 0; int abool; TRACE2 (DEBUG_CTX, "gpgme_set_ctx_flag", ctx, "name='%s' value='%s'", name? name:"(null)", value?value:"(null)"); abool = (value && *value)? !!atoi (value) : 0; if (!ctx || !name || !value) err = gpg_error (GPG_ERR_INV_VALUE); else if (!strcmp (name, "redraw")) { ctx->redraw_suggested = abool; } else if (!strcmp (name, "full-status")) { ctx->full_status = abool; } else if (!strcmp (name, "raw-description")) { ctx->raw_description = abool; } else if (!strcmp (name, "export-session-key")) { ctx->export_session_keys = abool; } else if (!strcmp (name, "override-session-key")) { free (ctx->override_session_key); ctx->override_session_key = strdup (value); if (!ctx->override_session_key) err = gpg_error_from_syserror (); } else if (!strcmp (name, "auto-key-retrieve")) { ctx->auto_key_retrieve = abool; } else if (!strcmp (name, "request-origin")) { free (ctx->request_origin); ctx->request_origin = strdup (value); if (!ctx->request_origin) err = gpg_error_from_syserror (); } else if (!strcmp (name, "no-symkey-cache")) { ctx->no_symkey_cache = abool; } else if (!strcmp (name, "ignore-mdc-error")) { ctx->ignore_mdc_error = abool; } else if (!strcmp (name, "auto-key-locate")) { free (ctx->auto_key_locate); ctx->auto_key_locate = strdup (value); if (!ctx->auto_key_locate) err = gpg_error_from_syserror (); } else if (!strcmp (name, "trust-model")) { free (ctx->trust_model); ctx->trust_model = strdup (value); if (!ctx->trust_model) err = gpg_error_from_syserror (); } else err = gpg_error (GPG_ERR_UNKNOWN_NAME); return err; } /* Get the context flag named NAME. See gpgme_set_ctx_flag for a list * of valid names. If the NAME is unknown NULL is returned. For a * boolean flag an empty string is returned for False and the string * "1" for True; thus either atoi or a simple string test can be * used. */ const char * gpgme_get_ctx_flag (gpgme_ctx_t ctx, const char *name) { if (!ctx || !name) return NULL; else if (!strcmp (name, "redraw")) { return ctx->redraw_suggested? "1":""; } else if (!strcmp (name, "full-status")) { return ctx->full_status? "1":""; } else if (!strcmp (name, "raw-description")) { return ctx->raw_description? "1":""; } else if (!strcmp (name, "export-session-key")) { return ctx->export_session_keys? "1":""; } else if (!strcmp (name, "override-session-key")) { return ctx->override_session_key? ctx->override_session_key : ""; } else if (!strcmp (name, "auto-key-retrieve")) { return ctx->auto_key_retrieve? "1":""; } else if (!strcmp (name, "request-origin")) { return ctx->request_origin? ctx->request_origin : ""; } else if (!strcmp (name, "no-symkey-cache")) { return ctx->no_symkey_cache? "1":""; } else if (!strcmp (name, "ignore-mdc-error")) { return ctx->ignore_mdc_error? "1":""; } else if (!strcmp (name, "auto-key-locate")) { return ctx->auto_key_locate? ctx->auto_key_locate : ""; } else return NULL; } /* Enable or disable the use of the special textmode. Textmode is for example used for the RFC2015 signatures; note that the updated RFC 3156 mandates that the MUA does some preparations so that textmode is not needed anymore. */ void gpgme_set_textmode (gpgme_ctx_t ctx, int use_textmode) { TRACE2 (DEBUG_CTX, "gpgme_set_textmode", ctx, "use_textmode=%i (%s)", use_textmode, use_textmode ? "yes" : "no"); if (!ctx) return; ctx->use_textmode = !!use_textmode; } /* Return the state of the textmode flag. */ int gpgme_get_textmode (gpgme_ctx_t ctx) { TRACE2 (DEBUG_CTX, "gpgme_get_textmode", ctx, "ctx->use_textmode=%i (%s)", ctx->use_textmode, ctx->use_textmode ? "yes" : "no"); return ctx->use_textmode; } /* Enable offline mode for this context. In offline mode dirmngr will be disabled. */ void gpgme_set_offline (gpgme_ctx_t ctx, int offline) { TRACE2 (DEBUG_CTX, "gpgme_set_offline", ctx, "offline=%i (%s)", offline, offline ? "yes" : "no"); if (!ctx) return; ctx->offline = !!offline; } /* Return the state of the offline flag. */ int gpgme_get_offline (gpgme_ctx_t ctx) { TRACE2 (DEBUG_CTX, "gpgme_get_offline", ctx, "ctx->offline=%i (%s)", ctx->offline, ctx->offline ? "yes" : "no"); return ctx->offline; } /* Set the number of certifications to include in an S/MIME message. The default is GPGME_INCLUDE_CERTS_DEFAULT. -1 means all certs, and -2 means all certs except the root cert. */ void gpgme_set_include_certs (gpgme_ctx_t ctx, int nr_of_certs) { if (!ctx) return; if (nr_of_certs == GPGME_INCLUDE_CERTS_DEFAULT) ctx->include_certs = GPGME_INCLUDE_CERTS_DEFAULT; else if (nr_of_certs < -2) ctx->include_certs = -2; else ctx->include_certs = nr_of_certs; TRACE2 (DEBUG_CTX, "gpgme_set_include_certs", ctx, "nr_of_certs=%i%s", nr_of_certs, nr_of_certs == ctx->include_certs ? "" : " (-2)"); } /* Get the number of certifications to include in an S/MIME message. */ int gpgme_get_include_certs (gpgme_ctx_t ctx) { TRACE1 (DEBUG_CTX, "gpgme_get_include_certs", ctx, "ctx->include_certs=%i", ctx->include_certs); return ctx->include_certs; } /* This function changes the default behaviour of the keylisting functions. MODE is a bitwise-OR of the GPGME_KEYLIST_* flags. The default mode is GPGME_KEYLIST_MODE_LOCAL. */ gpgme_error_t gpgme_set_keylist_mode (gpgme_ctx_t ctx, gpgme_keylist_mode_t mode) { TRACE1 (DEBUG_CTX, "gpgme_set_keylist_mode", ctx, "keylist_mode=0x%x", mode); if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); ctx->keylist_mode = mode; return 0; } /* This function returns the default behaviour of the keylisting functions. */ gpgme_keylist_mode_t gpgme_get_keylist_mode (gpgme_ctx_t ctx) { TRACE1 (DEBUG_CTX, "gpgme_get_keylist_mode", ctx, "ctx->keylist_mode=0x%x", ctx->keylist_mode); return ctx->keylist_mode; } /* Set the pinentry mode for CTX to MODE. */ gpgme_error_t gpgme_set_pinentry_mode (gpgme_ctx_t ctx, gpgme_pinentry_mode_t mode) { TRACE1 (DEBUG_CTX, "gpgme_set_pinentry_mode", ctx, "pinentry_mode=%u", (unsigned int)mode); if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); switch (mode) { case GPGME_PINENTRY_MODE_DEFAULT: case GPGME_PINENTRY_MODE_ASK: case GPGME_PINENTRY_MODE_CANCEL: case GPGME_PINENTRY_MODE_ERROR: case GPGME_PINENTRY_MODE_LOOPBACK: break; default: return gpg_error (GPG_ERR_INV_VALUE); } ctx->pinentry_mode = mode; return 0; } /* Get the pinentry mode of CTX. */ gpgme_pinentry_mode_t gpgme_get_pinentry_mode (gpgme_ctx_t ctx) { TRACE1 (DEBUG_CTX, "gpgme_get_pinentry_mode", ctx, "ctx->pinentry_mode=%u", (unsigned int)ctx->pinentry_mode); return ctx->pinentry_mode; } /* This function sets a callback function to be used to pass a passphrase to gpg. */ void gpgme_set_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t cb, void *cb_value) { TRACE2 (DEBUG_CTX, "gpgme_set_passphrase_cb", ctx, "passphrase_cb=%p/%p", cb, cb_value); if (!ctx) return; ctx->passphrase_cb = cb; ctx->passphrase_cb_value = cb_value; } /* This function returns the callback function to be used to pass a passphrase to the crypto engine. */ void gpgme_get_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t *r_cb, void **r_cb_value) { TRACE2 (DEBUG_CTX, "gpgme_get_passphrase_cb", ctx, "ctx->passphrase_cb=%p/%p", ctx->passphrase_cb, ctx->passphrase_cb_value); if (r_cb) *r_cb = ctx->passphrase_cb; if (r_cb_value) *r_cb_value = ctx->passphrase_cb_value; } /* This function sets a callback function to be used as a progress indicator. */ void gpgme_set_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t cb, void *cb_value) { TRACE2 (DEBUG_CTX, "gpgme_set_progress_cb", ctx, "progress_cb=%p/%p", cb, cb_value); if (!ctx) return; ctx->progress_cb = cb; ctx->progress_cb_value = cb_value; } /* This function returns the callback function to be used as a progress indicator. */ void gpgme_get_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t *r_cb, void **r_cb_value) { TRACE2 (DEBUG_CTX, "gpgme_get_progress_cb", ctx, "ctx->progress_cb=%p/%p", ctx->progress_cb, ctx->progress_cb_value); if (r_cb) *r_cb = ctx->progress_cb; if (r_cb_value) *r_cb_value = ctx->progress_cb_value; } /* This function sets a callback function to be used as a status message forwarder. */ void gpgme_set_status_cb (gpgme_ctx_t ctx, gpgme_status_cb_t cb, void *cb_value) { TRACE2 (DEBUG_CTX, "gpgme_set_status_cb", ctx, "status_cb=%p/%p", cb, cb_value); if (!ctx) return; ctx->status_cb = cb; ctx->status_cb_value = cb_value; } /* This function returns the callback function to be used as a status message forwarder. */ void gpgme_get_status_cb (gpgme_ctx_t ctx, gpgme_status_cb_t *r_cb, void **r_cb_value) { TRACE2 (DEBUG_CTX, "gpgme_get_status_cb", ctx, "ctx->status_cb=%p/%p", ctx ? ctx->status_cb : NULL, ctx ? ctx->status_cb_value : NULL); if (r_cb) *r_cb = NULL; if (r_cb_value) *r_cb_value = NULL; if (!ctx || !ctx->status_cb) return; if (r_cb) *r_cb = ctx->status_cb; if (r_cb_value) *r_cb_value = ctx->status_cb_value; } /* Set the I/O callback functions for CTX to IO_CBS. */ void gpgme_set_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs) { if (!ctx) return; if (io_cbs) { TRACE6 (DEBUG_CTX, "gpgme_set_io_cbs", ctx, "io_cbs=%p (add=%p/%p, remove=%p, event=%p/%p", io_cbs, io_cbs->add, io_cbs->add_priv, io_cbs->remove, io_cbs->event, io_cbs->event_priv); ctx->io_cbs = *io_cbs; } else { TRACE1 (DEBUG_CTX, "gpgme_set_io_cbs", ctx, "io_cbs=%p (default)", io_cbs); ctx->io_cbs.add = NULL; ctx->io_cbs.add_priv = NULL; ctx->io_cbs.remove = NULL; ctx->io_cbs.event = NULL; ctx->io_cbs.event_priv = NULL; } } /* This function provides access to the internal read function; it is normally not used. */ gpgme_ssize_t gpgme_io_read (int fd, void *buffer, size_t count) { int ret; TRACE_BEG2 (DEBUG_GLOBAL, "gpgme_io_read", fd, "buffer=%p, count=%u", buffer, count); ret = _gpgme_io_read (fd, buffer, count); return TRACE_SYSRES (ret); } /* This function provides access to the internal write function. It is to be used by user callbacks to return data to gpgme. See gpgme_passphrase_cb_t and gpgme_edit_cb_t. */ gpgme_ssize_t gpgme_io_write (int fd, const void *buffer, size_t count) { int ret; TRACE_BEG2 (DEBUG_GLOBAL, "gpgme_io_write", fd, "buffer=%p, count=%u", buffer, count); ret = _gpgme_io_write (fd, buffer, count); return TRACE_SYSRES (ret); } /* This function provides access to the internal write function. It is to be used by user callbacks to return data to gpgme. See gpgme_passphrase_cb_t and gpgme_edit_cb_t. Note that this is a variant of gpgme_io_write which guarantees that all COUNT bytes are written or an error is return. Returns: 0 on success or -1 on error and the sets errno. */ int gpgme_io_writen (int fd, const void *buffer_arg, size_t count) { const char *buffer = buffer_arg; int ret = 0; TRACE_BEG2 (DEBUG_GLOBAL, "gpgme_io_writen", fd, "buffer=%p, count=%u", buffer, count); while (count) { ret = _gpgme_io_write (fd, buffer, count); if (ret < 0) break; buffer += ret; count -= ret; ret = 0; } return TRACE_SYSRES (ret); } /* This function returns the callback function for I/O. */ void gpgme_get_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs) { TRACE6 (DEBUG_CTX, "gpgme_get_io_cbs", ctx, "io_cbs=%p, ctx->io_cbs.add=%p/%p, .remove=%p, .event=%p/%p", io_cbs, io_cbs->add, io_cbs->add_priv, io_cbs->remove, io_cbs->event, io_cbs->event_priv); *io_cbs = ctx->io_cbs; } /* This function sets the locale for the context CTX, or the default locale if CTX is a null pointer. */ gpgme_error_t gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value) { int failed = 0; char *new_lc_ctype = NULL; char *new_lc_messages = NULL; TRACE_BEG2 (DEBUG_CTX, "gpgme_set_locale", ctx, "category=%i, value=%s", category, value ? value : "(null)"); #define PREPARE_ONE_LOCALE(lcat, ucat) \ if (!failed && value \ && (category == LC_ALL || category == LC_ ## ucat)) \ { \ new_lc_ ## lcat = strdup (value); \ if (!new_lc_ ## lcat) \ failed = 1; \ } #ifdef LC_CTYPE PREPARE_ONE_LOCALE (ctype, CTYPE); #endif #ifdef LC_MESSAGES PREPARE_ONE_LOCALE (messages, MESSAGES); #endif if (failed) { int saved_err = gpg_error_from_syserror (); if (new_lc_ctype) free (new_lc_ctype); if (new_lc_messages) free (new_lc_messages); return TRACE_ERR (saved_err); } #define SET_ONE_LOCALE(lcat, ucat) \ if (category == LC_ALL || category == LC_ ## ucat) \ { \ if (ctx) \ { \ if (ctx->lc_ ## lcat) \ free (ctx->lc_ ## lcat); \ ctx->lc_ ## lcat = new_lc_ ## lcat; \ } \ else \ { \ if (def_lc_ ## lcat) \ free (def_lc_ ## lcat); \ def_lc_ ## lcat = new_lc_ ## lcat; \ } \ } if (!ctx) LOCK (def_lc_lock); #ifdef LC_CTYPE SET_ONE_LOCALE (ctype, CTYPE); #endif #ifdef LC_MESSAGES SET_ONE_LOCALE (messages, MESSAGES); #endif if (!ctx) UNLOCK (def_lc_lock); return TRACE_ERR (0); } /* Get the information about the configured engines. A pointer to the first engine in the statically allocated linked list is returned. The returned data is valid until the next gpgme_ctx_set_engine_info. */ gpgme_engine_info_t gpgme_ctx_get_engine_info (gpgme_ctx_t ctx) { TRACE1 (DEBUG_CTX, "gpgme_ctx_get_engine_info", ctx, "ctx->engine_info=%p", ctx->engine_info); return ctx->engine_info; } /* Set the engine info for the context CTX, protocol PROTO, to the file name FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t gpgme_ctx_set_engine_info (gpgme_ctx_t ctx, gpgme_protocol_t proto, const char *file_name, const char *home_dir) { gpgme_error_t err; TRACE_BEG4 (DEBUG_CTX, "gpgme_ctx_set_engine_info", ctx, "protocol=%i (%s), file_name=%s, home_dir=%s", proto, gpgme_get_protocol_name (proto) ? gpgme_get_protocol_name (proto) : "unknown", file_name ? file_name : "(default)", home_dir ? home_dir : "(default)"); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); /* Shut down the engine when changing engine info. */ if (ctx->engine) { TRACE_LOG1 ("releasing ctx->engine=%p", ctx->engine); _gpgme_engine_release (ctx->engine); ctx->engine = NULL; } err = _gpgme_set_engine_info (ctx->engine_info, proto, file_name, home_dir); return TRACE_ERR (err); } /* Clear all notation data from the context. */ void _gpgme_sig_notation_clear (gpgme_ctx_t ctx) { gpgme_sig_notation_t notation; if (!ctx) return; notation = ctx->sig_notations; while (notation) { gpgme_sig_notation_t next_notation = notation->next; _gpgme_sig_notation_free (notation); notation = next_notation; } ctx->sig_notations = NULL; } void gpgme_sig_notation_clear (gpgme_ctx_t ctx) { TRACE (DEBUG_CTX, "gpgme_sig_notation_clear", ctx); if (!ctx) return; _gpgme_sig_notation_clear (ctx); } /* Add the human-readable notation data with name NAME and value VALUE to the context CTX, using the flags FLAGS. If NAME is NULL, then VALUE should be a policy URL. The flag GPGME_SIG_NOTATION_HUMAN_READABLE is forced to be true for notation data, and false for policy URLs. */ gpgme_error_t gpgme_sig_notation_add (gpgme_ctx_t ctx, const char *name, const char *value, gpgme_sig_notation_flags_t flags) { gpgme_error_t err; gpgme_sig_notation_t notation; gpgme_sig_notation_t *lastp; TRACE_BEG3 (DEBUG_CTX, "gpgme_sig_notation_add", ctx, "name=%s, value=%s, flags=0x%x", name ? name : "(null)", value ? value : "(null)", flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (name) flags |= GPGME_SIG_NOTATION_HUMAN_READABLE; else flags &= ~GPGME_SIG_NOTATION_HUMAN_READABLE; err = _gpgme_sig_notation_create (¬ation, name, name ? strlen (name) : 0, value, value ? strlen (value) : 0, flags); if (err) return TRACE_ERR (err); lastp = &ctx->sig_notations; while (*lastp) lastp = &(*lastp)->next; *lastp = notation; return TRACE_ERR (0); } /* Get the sig notations for this context. */ gpgme_sig_notation_t gpgme_sig_notation_get (gpgme_ctx_t ctx) { if (!ctx) { TRACE (DEBUG_CTX, "gpgme_sig_notation_get", ctx); return NULL; } TRACE1 (DEBUG_CTX, "gpgme_sig_notation_get", ctx, "ctx->sig_notations=%p", ctx->sig_notations); return ctx->sig_notations; } /* Return a public key algorithm string made of the algorithm and size or the curve name. May return NULL on error. Caller must free the result using gpgme_free. */ char * gpgme_pubkey_algo_string (gpgme_subkey_t subkey) { const char *prefix = NULL; char *result; if (!subkey) { gpg_err_set_errno (EINVAL); return NULL; } switch (subkey->pubkey_algo) { case GPGME_PK_RSA: case GPGME_PK_RSA_E: case GPGME_PK_RSA_S: prefix = "rsa"; break; case GPGME_PK_ELG_E: prefix = "elg"; break; case GPGME_PK_DSA: prefix = "dsa"; break; case GPGME_PK_ELG: prefix = "xxx"; break; case GPGME_PK_ECC: case GPGME_PK_ECDH: case GPGME_PK_ECDSA: case GPGME_PK_EDDSA: prefix = ""; break; } if (prefix && *prefix) { char buffer[40]; snprintf (buffer, sizeof buffer, "%s%u", prefix, subkey->length); result = strdup (buffer); } else if (prefix && subkey->curve && *subkey->curve) result = strdup (subkey->curve); else if (prefix) result = strdup ("E_error"); else result = strdup ("unknown"); return result; } const char * gpgme_pubkey_algo_name (gpgme_pubkey_algo_t algo) { switch (algo) { case GPGME_PK_RSA: return "RSA"; case GPGME_PK_RSA_E: return "RSA-E"; case GPGME_PK_RSA_S: return "RSA-S"; case GPGME_PK_ELG_E: return "ELG-E"; case GPGME_PK_DSA: return "DSA"; case GPGME_PK_ECC: return "ECC"; case GPGME_PK_ELG: return "ELG"; case GPGME_PK_ECDSA: return "ECDSA"; case GPGME_PK_ECDH: return "ECDH"; case GPGME_PK_EDDSA: return "EdDSA"; default: return NULL; } } const char * gpgme_hash_algo_name (gpgme_hash_algo_t algo) { switch (algo) { case GPGME_MD_MD5: return "MD5"; case GPGME_MD_SHA1: return "SHA1"; case GPGME_MD_RMD160: return "RIPEMD160"; case GPGME_MD_MD2: return "MD2"; case GPGME_MD_TIGER: return "TIGER192"; case GPGME_MD_HAVAL: return "HAVAL"; case GPGME_MD_SHA256: return "SHA256"; case GPGME_MD_SHA384: return "SHA384"; case GPGME_MD_SHA512: return "SHA512"; case GPGME_MD_SHA224: return "SHA224"; case GPGME_MD_MD4: return "MD4"; case GPGME_MD_CRC32: return "CRC32"; case GPGME_MD_CRC32_RFC1510: return "CRC32RFC1510"; case GPGME_MD_CRC24_RFC2440: return "CRC24RFC2440"; default: return NULL; } } diff --git a/src/gpgme.h.in b/src/gpgme.h.in index 283ef188..722ce68b 100644 --- a/src/gpgme.h.in +++ b/src/gpgme.h.in @@ -1,2834 +1,2834 @@ /* gpgme.h - Public interface to GnuPG Made Easy. -*- c -*- * Copyright (C) 2000 Werner Koch (dd9jn) * Copyright (C) 2001-2018 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . - * SPDX-License-Identifier: LGPL-2.1+ + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later * * Generated from gpgme.h.in for @GPGME_CONFIG_HOST@. */ #ifndef GPGME_H #define GPGME_H /* Include stdio.h for the FILE type definition. */ #include #include #include #ifdef __cplusplus extern "C" { #if 0 /*(Make Emacsen's auto-indent happy.)*/ } #endif #endif /* __cplusplus */ /* The version of this header should match the one of the library. Do * not use this symbol in your application, use gpgme_check_version * instead. The purpose of this macro is to let autoconf (using the * AM_PATH_GPGME macro) check that this header matches the installed * library. */ #define GPGME_VERSION "@PACKAGE_VERSION@" /* The version number of this header. It may be used to handle minor * API incompatibilities. */ #define GPGME_VERSION_NUMBER @VERSION_NUMBER@ /* System specific typedefs. */ @INSERT__TYPEDEFS_FOR_GPGME_H@ /* * Check for compiler features. */ #ifdef GPGRT_INLINE # define _GPGME_INLINE GPGRT_INLINE #elif defined(__GNUC__) # define _GPGME_INLINE __inline__ #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L # define _GPGME_INLINE inline #else # define _GPGME_INLINE #endif /* The deprecated macro takes the version number of GPGME which * introduced the deprecation as parameter for documentation. */ #ifdef GPGRT_ATTR_DEPRECATED # define _GPGME_DEPRECATED(a,b) GPGRT_ATTR_DEPRECATED #elif defined(__GNUC__) # define _GPGME_GCC_VERSION (__GNUC__ * 10000 \ + __GNUC_MINOR__ * 100 \ + __GNUC_PATCHLEVEL__) # if _GPGME_GCC_VERSION > 30100 # define _GPGME_DEPRECATED(a,b) __attribute__ ((__deprecated__)) # else # define _GPGME_DEPRECATED(a,b) # endif #else # define _GPGME_DEPRECATED(a,b) #endif /* The macro _GPGME_DEPRECATED_OUTSIDE_GPGME suppresses warnings for * fields we must access in GPGME for ABI compatibility. */ #ifdef _GPGME_IN_GPGME #define _GPGME_DEPRECATED_OUTSIDE_GPGME(a,b) #else #define _GPGME_DEPRECATED_OUTSIDE_GPGME(a,b) _GPGME_DEPRECATED(a,b) #endif /* We used to use some symbols which clash with keywords in some * languages. This macro is used to obsolete them. */ #if defined(__cplusplus) || defined(SWIGPYTHON) # define _GPGME_OBSOLETE_SOME_SYMBOLS 1 #endif /* Check for a matching _FILE_OFFSET_BITS definition. */ #if @NEED__FILE_OFFSET_BITS@ #ifndef _FILE_OFFSET_BITS #error GPGME was compiled with _FILE_OFFSET_BITS = @NEED__FILE_OFFSET_BITS@, please see the section "Largefile support (LFS)" in the GPGME manual. #else #if (_FILE_OFFSET_BITS) != (@NEED__FILE_OFFSET_BITS@) #error GPGME was compiled with a different value for _FILE_OFFSET_BITS, namely @NEED__FILE_OFFSET_BITS@, please see the section "Largefile support (LFS)" in the GPGME manual. #endif #endif #endif /* * Some opaque data types used by GPGME. */ /* The context holds some global state and configuration options, as * well as the results of a crypto operation. */ struct gpgme_context; typedef struct gpgme_context *gpgme_ctx_t; /* The data object is used by GPGME to exchange arbitrary data. */ struct gpgme_data; typedef struct gpgme_data *gpgme_data_t; /* * Wrappers for the libgpg-error library. They are generally not * needed and the gpg-error versions may be used instead. */ typedef gpg_error_t gpgme_error_t; typedef gpg_err_code_t gpgme_err_code_t; typedef gpg_err_source_t gpgme_err_source_t; static _GPGME_INLINE gpgme_error_t gpgme_err_make (gpgme_err_source_t source, gpgme_err_code_t code) { return gpg_err_make (source, code); } /* The user can define GPGME_ERR_SOURCE_DEFAULT before including this * file to specify a default source for gpgme_error. */ #ifndef GPGME_ERR_SOURCE_DEFAULT #define GPGME_ERR_SOURCE_DEFAULT GPG_ERR_SOURCE_USER_1 #endif static _GPGME_INLINE gpgme_error_t gpgme_error (gpgme_err_code_t code) { return gpgme_err_make (GPGME_ERR_SOURCE_DEFAULT, code); } static _GPGME_INLINE gpgme_err_code_t gpgme_err_code (gpgme_error_t err) { return gpg_err_code (err); } static _GPGME_INLINE gpgme_err_source_t gpgme_err_source (gpgme_error_t err) { return gpg_err_source (err); } /* Return a pointer to a string containing a description of the error * code in the error value ERR. This function is not thread safe. */ const char *gpgme_strerror (gpgme_error_t err); /* Return the error string for ERR in the user-supplied buffer BUF of * size BUFLEN. This function is, in contrast to gpg_strerror, * thread-safe if a thread-safe strerror_r() function is provided by * the system. If the function succeeds, 0 is returned and BUF * contains the string describing the error. If the buffer was not * large enough, ERANGE is returned and BUF contains as much of the * beginning of the error string as fits into the buffer. */ int gpgme_strerror_r (gpg_error_t err, char *buf, size_t buflen); /* Return a pointer to a string containing a description of the error * source in the error value ERR. */ const char *gpgme_strsource (gpgme_error_t err); /* Retrieve the error code for the system error ERR. This returns * GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped (report * this). */ gpgme_err_code_t gpgme_err_code_from_errno (int err); /* Retrieve the system error for the error code CODE. This returns 0 * if CODE is not a system error code. */ int gpgme_err_code_to_errno (gpgme_err_code_t code); /* Retrieve the error code directly from the ERRNO variable. This * returns GPG_ERR_UNKNOWN_ERRNO if the system error is not mapped * (report this) and GPG_ERR_MISSING_ERRNO if ERRNO has the value 0. */ gpgme_err_code_t gpgme_err_code_from_syserror (void); /* Set the ERRNO variable. This function is the preferred way to set * ERRNO due to peculiarities on WindowsCE. */ void gpgme_err_set_errno (int err); /* Return an error value with the error source SOURCE and the system * error ERR. FIXME: Should be inline. */ gpgme_error_t gpgme_err_make_from_errno (gpgme_err_source_t source, int err); /* Return an error value with the system error ERR. * inline. */ gpgme_error_t gpgme_error_from_errno (int err); static _GPGME_INLINE gpgme_error_t gpgme_error_from_syserror (void) { return gpgme_error (gpgme_err_code_from_syserror ()); } /* * Various constants and types */ /* The possible encoding mode of gpgme_data_t objects. */ typedef enum { GPGME_DATA_ENCODING_NONE = 0, /* Not specified. */ GPGME_DATA_ENCODING_BINARY = 1, GPGME_DATA_ENCODING_BASE64 = 2, GPGME_DATA_ENCODING_ARMOR = 3, /* Either PEM or OpenPGP Armor. */ GPGME_DATA_ENCODING_URL = 4, /* LF delimited URL list. */ GPGME_DATA_ENCODING_URLESC = 5, /* Ditto, but percent escaped. */ GPGME_DATA_ENCODING_URL0 = 6, /* Nul delimited URL list. */ GPGME_DATA_ENCODING_MIME = 7 /* Data is a MIME part. */ } gpgme_data_encoding_t; /* Known data types. */ typedef enum { GPGME_DATA_TYPE_INVALID = 0, /* Not detected. */ GPGME_DATA_TYPE_UNKNOWN = 1, GPGME_DATA_TYPE_PGP_SIGNED = 0x10, GPGME_DATA_TYPE_PGP_ENCRYPTED= 0x11, GPGME_DATA_TYPE_PGP_OTHER = 0x12, GPGME_DATA_TYPE_PGP_KEY = 0x13, GPGME_DATA_TYPE_PGP_SIGNATURE= 0x18, /* Detached signature */ GPGME_DATA_TYPE_CMS_SIGNED = 0x20, GPGME_DATA_TYPE_CMS_ENCRYPTED= 0x21, GPGME_DATA_TYPE_CMS_OTHER = 0x22, GPGME_DATA_TYPE_X509_CERT = 0x23, GPGME_DATA_TYPE_PKCS12 = 0x24, } gpgme_data_type_t; /* Public key algorithms. */ typedef enum { GPGME_PK_RSA = 1, GPGME_PK_RSA_E = 2, GPGME_PK_RSA_S = 3, GPGME_PK_ELG_E = 16, GPGME_PK_DSA = 17, GPGME_PK_ECC = 18, GPGME_PK_ELG = 20, GPGME_PK_ECDSA = 301, GPGME_PK_ECDH = 302, GPGME_PK_EDDSA = 303 } gpgme_pubkey_algo_t; /* Hash algorithms (the values match those from libgcrypt). */ typedef enum { GPGME_MD_NONE = 0, GPGME_MD_MD5 = 1, GPGME_MD_SHA1 = 2, GPGME_MD_RMD160 = 3, GPGME_MD_MD2 = 5, GPGME_MD_TIGER = 6, /* TIGER/192. */ GPGME_MD_HAVAL = 7, /* HAVAL, 5 pass, 160 bit. */ GPGME_MD_SHA256 = 8, GPGME_MD_SHA384 = 9, GPGME_MD_SHA512 = 10, GPGME_MD_SHA224 = 11, GPGME_MD_MD4 = 301, GPGME_MD_CRC32 = 302, GPGME_MD_CRC32_RFC1510 = 303, GPGME_MD_CRC24_RFC2440 = 304 } gpgme_hash_algo_t; /* The available signature modes. */ typedef enum { GPGME_SIG_MODE_NORMAL = 0, GPGME_SIG_MODE_DETACH = 1, GPGME_SIG_MODE_CLEAR = 2 } gpgme_sig_mode_t; /* The available validities for a trust item or key. */ typedef enum { GPGME_VALIDITY_UNKNOWN = 0, GPGME_VALIDITY_UNDEFINED = 1, GPGME_VALIDITY_NEVER = 2, GPGME_VALIDITY_MARGINAL = 3, GPGME_VALIDITY_FULL = 4, GPGME_VALIDITY_ULTIMATE = 5 } gpgme_validity_t; /* The TOFU policies. */ typedef enum { GPGME_TOFU_POLICY_NONE = 0, GPGME_TOFU_POLICY_AUTO = 1, GPGME_TOFU_POLICY_GOOD = 2, GPGME_TOFU_POLICY_UNKNOWN = 3, GPGME_TOFU_POLICY_BAD = 4, GPGME_TOFU_POLICY_ASK = 5 } gpgme_tofu_policy_t; /* The key origin values. */ typedef enum { GPGME_KEYORG_UNKNOWN = 0, GPGME_KEYORG_KS = 1, GPGME_KEYORG_DANE = 3, GPGME_KEYORG_WKD = 4, GPGME_KEYORG_URL = 5, GPGME_KEYORG_FILE = 6, GPGME_KEYORG_SELF = 7, GPGME_KEYORG_OTHER = 31 } gpgme_keyorg_t; /* The available protocols. */ typedef enum { GPGME_PROTOCOL_OpenPGP = 0, /* The default mode. */ GPGME_PROTOCOL_CMS = 1, GPGME_PROTOCOL_GPGCONF = 2, /* Special code for gpgconf. */ GPGME_PROTOCOL_ASSUAN = 3, /* Low-level access to an Assuan server. */ GPGME_PROTOCOL_G13 = 4, GPGME_PROTOCOL_UISERVER= 5, GPGME_PROTOCOL_SPAWN = 6, /* Direct access to any program. */ GPGME_PROTOCOL_DEFAULT = 254, GPGME_PROTOCOL_UNKNOWN = 255 } gpgme_protocol_t; /* Convenience macro for the surprisingly mixed spelling. */ #define GPGME_PROTOCOL_OPENPGP GPGME_PROTOCOL_OpenPGP /* The available keylist mode flags. */ #define GPGME_KEYLIST_MODE_LOCAL 1 #define GPGME_KEYLIST_MODE_EXTERN 2 #define GPGME_KEYLIST_MODE_SIGS 4 #define GPGME_KEYLIST_MODE_SIG_NOTATIONS 8 #define GPGME_KEYLIST_MODE_WITH_SECRET 16 #define GPGME_KEYLIST_MODE_WITH_TOFU 32 #define GPGME_KEYLIST_MODE_EPHEMERAL 128 #define GPGME_KEYLIST_MODE_VALIDATE 256 #define GPGME_KEYLIST_MODE_LOCATE (1|2) typedef unsigned int gpgme_keylist_mode_t; /* The pinentry modes. */ typedef enum { GPGME_PINENTRY_MODE_DEFAULT = 0, GPGME_PINENTRY_MODE_ASK = 1, GPGME_PINENTRY_MODE_CANCEL = 2, GPGME_PINENTRY_MODE_ERROR = 3, GPGME_PINENTRY_MODE_LOOPBACK = 4 } gpgme_pinentry_mode_t; /* The available export mode flags. */ #define GPGME_EXPORT_MODE_EXTERN 2 #define GPGME_EXPORT_MODE_MINIMAL 4 #define GPGME_EXPORT_MODE_SECRET 16 #define GPGME_EXPORT_MODE_RAW 32 #define GPGME_EXPORT_MODE_PKCS12 64 #define GPGME_EXPORT_MODE_NOUID 128 /* Experimental(!)*/ typedef unsigned int gpgme_export_mode_t; /* Flags for the audit log functions. */ #define GPGME_AUDITLOG_DEFAULT 0 #define GPGME_AUDITLOG_HTML 1 #define GPGME_AUDITLOG_DIAG 2 #define GPGME_AUDITLOG_WITH_HELP 128 /* The available signature notation flags. */ #define GPGME_SIG_NOTATION_HUMAN_READABLE 1 #define GPGME_SIG_NOTATION_CRITICAL 2 typedef unsigned int gpgme_sig_notation_flags_t; /* An object to hold information about notation data. This structure * shall be considered read-only and an application must not allocate * such a structure on its own. */ struct _gpgme_sig_notation { struct _gpgme_sig_notation *next; /* If NAME is a null pointer, then VALUE contains a policy URL * rather than a notation. */ char *name; /* The value of the notation data. */ char *value; /* The length of the name of the notation data. */ int name_len; /* The length of the value of the notation data. */ int value_len; /* The accumulated flags. */ gpgme_sig_notation_flags_t flags; /* Notation data is human-readable. */ unsigned int human_readable : 1; /* Notation data is critical. */ unsigned int critical : 1; /* Internal to GPGME, do not use. */ int _unused : 30; }; typedef struct _gpgme_sig_notation *gpgme_sig_notation_t; /* * Public structures. */ /* The engine information structure. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_engine_info { struct _gpgme_engine_info *next; /* The protocol ID. */ gpgme_protocol_t protocol; /* The file name of the engine binary. */ char *file_name; /* The version string of the installed engine. */ char *version; /* The minimum version required for GPGME. */ const char *req_version; /* The home directory used, or NULL if default. */ char *home_dir; }; typedef struct _gpgme_engine_info *gpgme_engine_info_t; /* An object with TOFU information. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_tofu_info { struct _gpgme_tofu_info *next; /* The TOFU validity: * 0 := conflict * 1 := key without history * 2 := key with too little history * 3 := key with enough history for basic trust * 4 := key with a lot of history */ unsigned int validity : 3; /* The TOFU policy (gpgme_tofu_policy_t). */ unsigned int policy : 4; unsigned int _rfu : 25; /* Number of signatures seen for this binding. Capped at USHRT_MAX. */ unsigned short signcount; /* Number of encryptions done with this binding. Capped at USHRT_MAX. */ unsigned short encrcount; /* Number of seconds since Epoch when the first and the most * recently seen message were verified/decrypted. 0 means unknown. */ unsigned long signfirst; unsigned long signlast; unsigned long encrfirst; unsigned long encrlast; /* If non-NULL a human readable string summarizing the TOFU data. */ char *description; }; typedef struct _gpgme_tofu_info *gpgme_tofu_info_t; /* A subkey from a key. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_subkey { struct _gpgme_subkey *next; /* True if subkey is revoked. */ unsigned int revoked : 1; /* True if subkey is expired. */ unsigned int expired : 1; /* True if subkey is disabled. */ unsigned int disabled : 1; /* True if subkey is invalid. */ unsigned int invalid : 1; /* True if subkey can be used for encryption. */ unsigned int can_encrypt : 1; /* True if subkey can be used for signing. */ unsigned int can_sign : 1; /* True if subkey can be used for certification. */ unsigned int can_certify : 1; /* True if subkey is secret. */ unsigned int secret : 1; /* True if subkey can be used for authentication. */ unsigned int can_authenticate : 1; /* True if subkey is qualified for signatures according to German law. */ unsigned int is_qualified : 1; /* True if the secret key is stored on a smart card. */ unsigned int is_cardkey : 1; /* True if the key is compliant to the de-vs mode. */ unsigned int is_de_vs : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 20; /* Public key algorithm supported by this subkey. */ gpgme_pubkey_algo_t pubkey_algo; /* Length of the subkey. */ unsigned int length; /* The key ID of the subkey. */ char *keyid; /* Internal to GPGME, do not use. */ char _keyid[16 + 1]; /* The fingerprint of the subkey in hex digit form. */ char *fpr; /* The creation timestamp, -1 if invalid, 0 if not available. */ long int timestamp; /* The expiration timestamp, 0 if the subkey does not expire. */ long int expires; /* The serial number of a smart card holding this key or NULL. */ char *card_number; /* The name of the curve for ECC algorithms or NULL. */ char *curve; /* The keygrip of the subkey in hex digit form or NULL if not availabale. */ char *keygrip; }; typedef struct _gpgme_subkey *gpgme_subkey_t; /* A signature on a user ID. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_key_sig { struct _gpgme_key_sig *next; /* True if the signature is a revocation signature. */ unsigned int revoked : 1; /* True if the signature is expired. */ unsigned int expired : 1; /* True if the signature is invalid. */ unsigned int invalid : 1; /* True if the signature should be exported. */ unsigned int exportable : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 28; /* The public key algorithm used to create the signature. */ gpgme_pubkey_algo_t pubkey_algo; /* The key ID of key used to create the signature. */ char *keyid; /* Internal to GPGME, do not use. */ char _keyid[16 + 1]; /* The creation timestamp, -1 if invalid, 0 if not available. */ long int timestamp; /* The expiration timestamp, 0 if the subkey does not expire. */ long int expires; /* Same as in gpgme_signature_t. */ gpgme_error_t status; /* Deprecated; use SIG_CLASS instead. */ #ifdef _GPGME_OBSOLETE_SOME_SYMBOLS unsigned int _obsolete_class _GPGME_DEPRECATED(0,4); #else unsigned int class _GPGME_DEPRECATED_OUTSIDE_GPGME(0,4); #endif /* The user ID string. */ char *uid; /* The name part of the user ID. */ char *name; /* The email part of the user ID. */ char *email; /* The comment part of the user ID. */ char *comment; /* Crypto backend specific signature class. */ unsigned int sig_class; /* Notation data and policy URLs. */ gpgme_sig_notation_t notations; /* Internal to GPGME, do not use. */ gpgme_sig_notation_t _last_notation; }; typedef struct _gpgme_key_sig *gpgme_key_sig_t; /* An user ID from a key. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_user_id { struct _gpgme_user_id *next; /* True if the user ID is revoked. */ unsigned int revoked : 1; /* True if the user ID is invalid. */ unsigned int invalid : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 25; /* Origin of this user ID. */ unsigned int origin : 5; /* The validity of the user ID. */ gpgme_validity_t validity; /* The user ID string. */ char *uid; /* The name part of the user ID. */ char *name; /* The email part of the user ID. */ char *email; /* The comment part of the user ID. */ char *comment; /* The signatures of the user ID. */ gpgme_key_sig_t signatures; /* Internal to GPGME, do not use. */ gpgme_key_sig_t _last_keysig; /* The mail address (addr-spec from RFC5322) of the UID string. * This is general the same as the EMAIL part of this struct but * might be slightly different. If no mail address is available * NULL is stored. */ char *address; /* The malloced TOFU information or NULL. */ gpgme_tofu_info_t tofu; /* Time of the last refresh of this user id. 0 if unknown. */ unsigned long last_update; }; typedef struct _gpgme_user_id *gpgme_user_id_t; /* A key from the keyring. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_key { /* Internal to GPGME, do not use. */ unsigned int _refs; /* True if key is revoked. */ unsigned int revoked : 1; /* True if key is expired. */ unsigned int expired : 1; /* True if key is disabled. */ unsigned int disabled : 1; /* True if key is invalid. */ unsigned int invalid : 1; /* True if key can be used for encryption. */ unsigned int can_encrypt : 1; /* True if key can be used for signing. */ unsigned int can_sign : 1; /* True if key can be used for certification. */ unsigned int can_certify : 1; /* True if key is secret. */ unsigned int secret : 1; /* True if key can be used for authentication. */ unsigned int can_authenticate : 1; /* True if subkey is qualified for signatures according to German law. */ unsigned int is_qualified : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 17; /* Origin of this key. */ unsigned int origin : 5; /* This is the protocol supported by this key. */ gpgme_protocol_t protocol; /* If protocol is GPGME_PROTOCOL_CMS, this string contains the issuer serial. */ char *issuer_serial; /* If protocol is GPGME_PROTOCOL_CMS, this string contains the issuer name. */ char *issuer_name; /* If protocol is GPGME_PROTOCOL_CMS, this string contains the chain ID. */ char *chain_id; /* If protocol is GPGME_PROTOCOL_OpenPGP, this field contains the owner trust. */ gpgme_validity_t owner_trust; /* The subkeys of the key. */ gpgme_subkey_t subkeys; /* The user IDs of the key. */ gpgme_user_id_t uids; /* Internal to GPGME, do not use. */ gpgme_subkey_t _last_subkey; /* Internal to GPGME, do not use. */ gpgme_user_id_t _last_uid; /* The keylist mode that was active when listing the key. */ gpgme_keylist_mode_t keylist_mode; /* This field gives the fingerprint of the primary key. Note that * this is a copy of the FPR of the first subkey. We need it here * to allow for an incomplete key object. */ char *fpr; /* Time of the last refresh of the entire key. 0 if unknown. */ unsigned long last_update; }; typedef struct _gpgme_key *gpgme_key_t; /* An invalid key object. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_invalid_key { struct _gpgme_invalid_key *next; /* The string used to request the key. Despite the name this may * not be a fingerprint. */ char *fpr; /* The error code. */ gpgme_error_t reason; }; typedef struct _gpgme_invalid_key *gpgme_invalid_key_t; /* * Types for callback functions. */ /* Request a passphrase from the user. */ typedef gpgme_error_t (*gpgme_passphrase_cb_t) (void *hook, const char *uid_hint, const char *passphrase_info, int prev_was_bad, int fd); /* Inform the user about progress made. */ typedef void (*gpgme_progress_cb_t) (void *opaque, const char *what, int type, int current, int total); /* Status messages from gpg. */ typedef gpgme_error_t (*gpgme_status_cb_t) (void *opaque, const char *keyword, const char *args); /* Interact with the user about an edit operation. */ typedef gpgme_error_t (*gpgme_interact_cb_t) (void *opaque, const char *keyword, const char *args, int fd); /* * Context management functions. */ /* Create a new context and return it in CTX. */ gpgme_error_t gpgme_new (gpgme_ctx_t *ctx); /* Release the context CTX. */ void gpgme_release (gpgme_ctx_t ctx); /* Set the flag NAME for CTX to VALUE. */ gpgme_error_t gpgme_set_ctx_flag (gpgme_ctx_t ctx, const char *name, const char *value); /* Get the value of the flag NAME from CTX. */ const char *gpgme_get_ctx_flag (gpgme_ctx_t ctx, const char *name); /* Set the protocol to be used by CTX to PROTO. */ gpgme_error_t gpgme_set_protocol (gpgme_ctx_t ctx, gpgme_protocol_t proto); /* Get the protocol used with CTX */ gpgme_protocol_t gpgme_get_protocol (gpgme_ctx_t ctx); /* Set the crypto protocol to be used by CTX to PROTO. * gpgme_set_protocol actually sets the backend engine. This sets the * crypto protocol used in engines that support more than one crypto * prococol (for example, an UISERVER can support OpenPGP and CMS). * This is reset to the default with gpgme_set_protocol. */ gpgme_error_t gpgme_set_sub_protocol (gpgme_ctx_t ctx, gpgme_protocol_t proto); /* Get the sub protocol. */ gpgme_protocol_t gpgme_get_sub_protocol (gpgme_ctx_t ctx); /* Get the string describing protocol PROTO, or NULL if invalid. */ const char *gpgme_get_protocol_name (gpgme_protocol_t proto); /* If YES is non-zero, enable armor mode in CTX, disable it otherwise. */ void gpgme_set_armor (gpgme_ctx_t ctx, int yes); /* Return non-zero if armor mode is set in CTX. */ int gpgme_get_armor (gpgme_ctx_t ctx); /* If YES is non-zero, enable text mode in CTX, disable it otherwise. */ void gpgme_set_textmode (gpgme_ctx_t ctx, int yes); /* Return non-zero if text mode is set in CTX. */ int gpgme_get_textmode (gpgme_ctx_t ctx); /* If YES is non-zero, enable offline mode in CTX, disable it otherwise. */ void gpgme_set_offline (gpgme_ctx_t ctx, int yes); /* Return non-zero if offline mode is set in CTX. */ int gpgme_get_offline (gpgme_ctx_t ctx); /* Use whatever the default of the backend crypto engine is. */ #define GPGME_INCLUDE_CERTS_DEFAULT -256 /* Include up to NR_OF_CERTS certificates in an S/MIME message. */ void gpgme_set_include_certs (gpgme_ctx_t ctx, int nr_of_certs); /* Return the number of certs to include in an S/MIME message. */ int gpgme_get_include_certs (gpgme_ctx_t ctx); /* Set keylist mode in CTX to MODE. */ gpgme_error_t gpgme_set_keylist_mode (gpgme_ctx_t ctx, gpgme_keylist_mode_t mode); /* Get keylist mode in CTX. */ gpgme_keylist_mode_t gpgme_get_keylist_mode (gpgme_ctx_t ctx); /* Set the pinentry mode for CTX to MODE. */ gpgme_error_t gpgme_set_pinentry_mode (gpgme_ctx_t ctx, gpgme_pinentry_mode_t mode); /* Get the pinentry mode of CTX. */ gpgme_pinentry_mode_t gpgme_get_pinentry_mode (gpgme_ctx_t ctx); /* Set the passphrase callback function in CTX to CB. HOOK_VALUE is * passed as first argument to the passphrase callback function. */ void gpgme_set_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t cb, void *hook_value); /* Get the current passphrase callback function in *CB and the current * hook value in *HOOK_VALUE. */ void gpgme_get_passphrase_cb (gpgme_ctx_t ctx, gpgme_passphrase_cb_t *cb, void **hook_value); /* Set the progress callback function in CTX to CB. HOOK_VALUE is * passed as first argument to the progress callback function. */ void gpgme_set_progress_cb (gpgme_ctx_t c, gpgme_progress_cb_t cb, void *hook_value); /* Get the current progress callback function in *CB and the current * hook value in *HOOK_VALUE. */ void gpgme_get_progress_cb (gpgme_ctx_t ctx, gpgme_progress_cb_t *cb, void **hook_value); /* Set the status callback function in CTX to CB. HOOK_VALUE is * passed as first argument to the status callback function. */ void gpgme_set_status_cb (gpgme_ctx_t c, gpgme_status_cb_t cb, void *hook_value); /* Get the current status callback function in *CB and the current * hook value in *HOOK_VALUE. */ void gpgme_get_status_cb (gpgme_ctx_t ctx, gpgme_status_cb_t *cb, void **hook_value); /* This function sets the locale for the context CTX, or the default * locale if CTX is a null pointer. */ gpgme_error_t gpgme_set_locale (gpgme_ctx_t ctx, int category, const char *value); /* Get the information about the configured engines. A pointer to the * first engine in the statically allocated linked list is returned. * The returned data is valid until the next gpgme_ctx_set_engine_info. */ gpgme_engine_info_t gpgme_ctx_get_engine_info (gpgme_ctx_t ctx); /* Set the engine info for the context CTX, protocol PROTO, to the * file name FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t gpgme_ctx_set_engine_info (gpgme_ctx_t ctx, gpgme_protocol_t proto, const char *file_name, const char *home_dir); /* Delete all signers from CTX. */ void gpgme_signers_clear (gpgme_ctx_t ctx); /* Add KEY to list of signers in CTX. */ gpgme_error_t gpgme_signers_add (gpgme_ctx_t ctx, const gpgme_key_t key); /* Return the number of signers in CTX. */ unsigned int gpgme_signers_count (const gpgme_ctx_t ctx); /* Return the SEQth signer's key in CTX. */ gpgme_key_t gpgme_signers_enum (const gpgme_ctx_t ctx, int seq); /* Clear all notation data from the context. */ void gpgme_sig_notation_clear (gpgme_ctx_t ctx); /* Add the human-readable notation data with name NAME and value VALUE * to the context CTX, using the flags FLAGS. If NAME is NULL, then * VALUE should be a policy URL. The flag * GPGME_SIG_NOTATION_HUMAN_READABLE is forced to be true for notation * data, and false for policy URLs. */ gpgme_error_t gpgme_sig_notation_add (gpgme_ctx_t ctx, const char *name, const char *value, gpgme_sig_notation_flags_t flags); /* Get the sig notations for this context. */ gpgme_sig_notation_t gpgme_sig_notation_get (gpgme_ctx_t ctx); /* Store a sender address in the context. */ gpgme_error_t gpgme_set_sender (gpgme_ctx_t ctx, const char *address); /* Get the sender address from the context. */ const char *gpgme_get_sender (gpgme_ctx_t ctx); /* * Run control. */ /* The type of an I/O callback function. */ typedef gpgme_error_t (*gpgme_io_cb_t) (void *data, int fd); /* The type of a function that can register FNC as the I/O callback * function for the file descriptor FD with direction dir (0: for writing, * 1: for reading). FNC_DATA should be passed as DATA to FNC. The * function should return a TAG suitable for the corresponding * gpgme_remove_io_cb_t, and an error value. */ typedef gpgme_error_t (*gpgme_register_io_cb_t) (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **tag); /* The type of a function that can remove a previously registered I/O * callback function given TAG as returned by the register * function. */ typedef void (*gpgme_remove_io_cb_t) (void *tag); typedef enum { GPGME_EVENT_START, GPGME_EVENT_DONE, GPGME_EVENT_NEXT_KEY, GPGME_EVENT_NEXT_TRUSTITEM } gpgme_event_io_t; struct gpgme_io_event_done_data { /* A fatal IPC error or an operational error in state-less * protocols. */ gpgme_error_t err; /* An operational errors in session-based protocols. */ gpgme_error_t op_err; }; typedef struct gpgme_io_event_done_data *gpgme_io_event_done_data_t; /* The type of a function that is called when a context finished an * operation. */ typedef void (*gpgme_event_io_cb_t) (void *data, gpgme_event_io_t type, void *type_data); struct gpgme_io_cbs { gpgme_register_io_cb_t add; void *add_priv; gpgme_remove_io_cb_t remove; gpgme_event_io_cb_t event; void *event_priv; }; typedef struct gpgme_io_cbs *gpgme_io_cbs_t; /* Set the I/O callback functions in CTX to IO_CBS. */ void gpgme_set_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs); /* Get the current I/O callback functions. */ void gpgme_get_io_cbs (gpgme_ctx_t ctx, gpgme_io_cbs_t io_cbs); /* Wrappers around the internal I/O functions for use with * gpgme_passphrase_cb_t and gpgme_interact_cb_t. */ @API__SSIZE_T@ gpgme_io_read (int fd, void *buffer, size_t count); @API__SSIZE_T@ gpgme_io_write (int fd, const void *buffer, size_t count); int gpgme_io_writen (int fd, const void *buffer, size_t count); /* Process the pending operation and, if HANG is non-zero, wait for * the pending operation to finish. */ gpgme_ctx_t gpgme_wait (gpgme_ctx_t ctx, gpgme_error_t *status, int hang); gpgme_ctx_t gpgme_wait_ext (gpgme_ctx_t ctx, gpgme_error_t *status, gpgme_error_t *op_err, int hang); /* Cancel a pending asynchronous operation. */ gpgme_error_t gpgme_cancel (gpgme_ctx_t ctx); /* Cancel a pending operation asynchronously. */ gpgme_error_t gpgme_cancel_async (gpgme_ctx_t ctx); /* * Functions to handle data objects. */ /* Read up to SIZE bytes into buffer BUFFER from the data object with * the handle HANDLE. Return the number of characters read, 0 on EOF * and -1 on error. If an error occurs, errno is set. */ typedef @API__SSIZE_T@ (*gpgme_data_read_cb_t) (void *handle, void *buffer, size_t size); /* Write up to SIZE bytes from buffer BUFFER to the data object with * the handle HANDLE. Return the number of characters written, or -1 * on error. If an error occurs, errno is set. */ typedef @API__SSIZE_T@ (*gpgme_data_write_cb_t) (void *handle, const void *buffer, size_t size); /* Set the current position from where the next read or write starts * in the data object with the handle HANDLE to OFFSET, relativ to * WHENCE. Returns the new offset in bytes from the beginning of the * data object. */ typedef @API__OFF_T@ (*gpgme_data_seek_cb_t) (void *handle, @API__OFF_T@ offset, int whence); /* Close the data object with the handle HANDLE. */ typedef void (*gpgme_data_release_cb_t) (void *handle); struct gpgme_data_cbs { gpgme_data_read_cb_t read; gpgme_data_write_cb_t write; gpgme_data_seek_cb_t seek; gpgme_data_release_cb_t release; }; typedef struct gpgme_data_cbs *gpgme_data_cbs_t; /* Read up to SIZE bytes into buffer BUFFER from the data object with * the handle DH. Return the number of characters read, 0 on EOF and * -1 on error. If an error occurs, errno is set. */ @API__SSIZE_T@ gpgme_data_read (gpgme_data_t dh, void *buffer, size_t size); /* Write up to SIZE bytes from buffer BUFFER to the data object with * the handle DH. Return the number of characters written, or -1 on * error. If an error occurs, errno is set. */ @API__SSIZE_T@ gpgme_data_write (gpgme_data_t dh, const void *buffer, size_t size); /* Set the current position from where the next read or write starts * in the data object with the handle DH to OFFSET, relativ to WHENCE. * Returns the new offset in bytes from the beginning of the data * object. */ @API__OFF_T@ gpgme_data_seek (gpgme_data_t dh, @API__OFF_T@ offset, int whence); /* Create a new data buffer and return it in R_DH. */ gpgme_error_t gpgme_data_new (gpgme_data_t *r_dh); /* Destroy the data buffer DH. */ void gpgme_data_release (gpgme_data_t dh); /* Create a new data buffer filled with SIZE bytes starting from * BUFFER. If COPY is zero, copying is delayed until necessary, and * the data is taken from the original location when needed. */ gpgme_error_t gpgme_data_new_from_mem (gpgme_data_t *r_dh, const char *buffer, size_t size, int copy); /* Destroy the data buffer DH and return a pointer to its content. * The memory has be to released with gpgme_free() by the user. It's * size is returned in R_LEN. */ char *gpgme_data_release_and_get_mem (gpgme_data_t dh, size_t *r_len); /* Release the memory returned by gpgme_data_release_and_get_mem() and * some other functions. */ void gpgme_free (void *buffer); gpgme_error_t gpgme_data_new_from_cbs (gpgme_data_t *dh, gpgme_data_cbs_t cbs, void *handle); gpgme_error_t gpgme_data_new_from_fd (gpgme_data_t *dh, int fd); gpgme_error_t gpgme_data_new_from_stream (gpgme_data_t *dh, FILE *stream); gpgme_error_t gpgme_data_new_from_estream (gpgme_data_t *r_dh, gpgrt_stream_t stream); /* Return the encoding attribute of the data buffer DH */ gpgme_data_encoding_t gpgme_data_get_encoding (gpgme_data_t dh); /* Set the encoding attribute of data buffer DH to ENC */ gpgme_error_t gpgme_data_set_encoding (gpgme_data_t dh, gpgme_data_encoding_t enc); /* Get the file name associated with the data object with handle DH, or * NULL if there is none. */ char *gpgme_data_get_file_name (gpgme_data_t dh); /* Set the file name associated with the data object with handle DH to * FILE_NAME. */ gpgme_error_t gpgme_data_set_file_name (gpgme_data_t dh, const char *file_name); /* Set a flag for the data object DH. See the manual for details. */ gpg_error_t gpgme_data_set_flag (gpgme_data_t dh, const char *name, const char *value); /* Try to identify the type of the data in DH. */ gpgme_data_type_t gpgme_data_identify (gpgme_data_t dh, int reserved); /* Create a new data buffer filled with the content of file FNAME. * COPY must be non-zero. For delayed read, please use * gpgme_data_new_from_fd or gpgme_data_new_from_stream instead. */ gpgme_error_t gpgme_data_new_from_file (gpgme_data_t *r_dh, const char *fname, int copy); /* Create a new data buffer filled with LENGTH bytes starting from * OFFSET within the file FNAME or stream FP (exactly one must be * non-zero). */ gpgme_error_t gpgme_data_new_from_filepart (gpgme_data_t *r_dh, const char *fname, FILE *fp, @API__OFF_T@ offset, size_t length); /* Convenience function to do a gpgme_data_seek (dh, 0, SEEK_SET). */ gpgme_error_t gpgme_data_rewind (gpgme_data_t dh); /* * Key and trust functions. */ /* Get the key with the fingerprint FPR from the crypto backend. If * SECRET is true, get the secret key. */ gpgme_error_t gpgme_get_key (gpgme_ctx_t ctx, const char *fpr, gpgme_key_t *r_key, int secret); /* Create a dummy key to specify an email address. */ gpgme_error_t gpgme_key_from_uid (gpgme_key_t *key, const char *name); /* Acquire a reference to KEY. */ void gpgme_key_ref (gpgme_key_t key); /* Release a reference to KEY. If this was the last one the key is * destroyed. */ void gpgme_key_unref (gpgme_key_t key); void gpgme_key_release (gpgme_key_t key); /* * Encryption. */ /* An object to return results from an encryption operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_encrypt_result { /* The list of invalid recipients. */ gpgme_invalid_key_t invalid_recipients; }; typedef struct _gpgme_op_encrypt_result *gpgme_encrypt_result_t; /* Retrieve a pointer to the result of the encrypt operation. */ gpgme_encrypt_result_t gpgme_op_encrypt_result (gpgme_ctx_t ctx); /* The valid encryption flags. */ typedef enum { GPGME_ENCRYPT_ALWAYS_TRUST = 1, GPGME_ENCRYPT_NO_ENCRYPT_TO = 2, GPGME_ENCRYPT_PREPARE = 4, GPGME_ENCRYPT_EXPECT_SIGN = 8, GPGME_ENCRYPT_NO_COMPRESS = 16, GPGME_ENCRYPT_SYMMETRIC = 32, GPGME_ENCRYPT_THROW_KEYIDS = 64, GPGME_ENCRYPT_WRAP = 128, GPGME_ENCRYPT_WANT_ADDRESS = 256 } gpgme_encrypt_flags_t; /* Encrypt plaintext PLAIN within CTX for the recipients RECP and * store the resulting ciphertext in CIPHER. */ gpgme_error_t gpgme_op_encrypt_start (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher); gpgme_error_t gpgme_op_encrypt (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher); gpgme_error_t gpgme_op_encrypt_ext_start (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher); gpgme_error_t gpgme_op_encrypt_ext (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher); /* Encrypt plaintext PLAIN within CTX for the recipients RECP and * store the resulting ciphertext in CIPHER. Also sign the ciphertext * with the signers in CTX. */ gpgme_error_t gpgme_op_encrypt_sign_start (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher); gpgme_error_t gpgme_op_encrypt_sign (gpgme_ctx_t ctx, gpgme_key_t recp[], gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher); gpgme_error_t gpgme_op_encrypt_sign_ext_start (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher); gpgme_error_t gpgme_op_encrypt_sign_ext (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *recpstring, gpgme_encrypt_flags_t flags, gpgme_data_t plain, gpgme_data_t cipher); /* * Decryption. */ /* An object to hold information about a recipient. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_recipient { struct _gpgme_recipient *next; /* The key ID of key for which the text was encrypted. */ char *keyid; /* Internal to GPGME, do not use. */ char _keyid[16 + 1]; /* The public key algorithm of the recipient key. */ gpgme_pubkey_algo_t pubkey_algo; /* The status of the recipient. */ gpgme_error_t status; }; typedef struct _gpgme_recipient *gpgme_recipient_t; /* An object to return results from a decryption operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_decrypt_result { char *unsupported_algorithm; /* Key should not have been used for encryption. */ unsigned int wrong_key_usage : 1; /* True if the message was encrypted in compliance to the de-vs * mode. */ unsigned int is_de_vs : 1; /* The message claims that the content is a MIME object. */ unsigned int is_mime : 1; /* The message was made by a legacy algorithm without any integrity * protection. This might be an old but legitimate message. */ unsigned int legacy_cipher_nomdc : 1; /* Internal to GPGME, do not use. */ int _unused : 28; gpgme_recipient_t recipients; /* The original file name of the plaintext message, if * available. */ char *file_name; /* A textual representation of the session key used to decrypt the * message, if available */ char *session_key; /* A string with the symmetric encryption algorithm and mode using * the format ".". */ char *symkey_algo; }; typedef struct _gpgme_op_decrypt_result *gpgme_decrypt_result_t; /* Retrieve a pointer to the result of the decrypt operation. */ gpgme_decrypt_result_t gpgme_op_decrypt_result (gpgme_ctx_t ctx); /* The valid decryption flags. */ typedef enum { GPGME_DECRYPT_VERIFY = 1, GPGME_DECRYPT_UNWRAP = 128 } gpgme_decrypt_flags_t; /* Decrypt ciphertext CIPHER within CTX and store the resulting * plaintext in PLAIN. */ gpgme_error_t gpgme_op_decrypt_start (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain); gpgme_error_t gpgme_op_decrypt (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain); /* Decrypt ciphertext CIPHER and make a signature verification within * CTX and store the resulting plaintext in PLAIN. */ gpgme_error_t gpgme_op_decrypt_verify_start (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain); gpgme_error_t gpgme_op_decrypt_verify (gpgme_ctx_t ctx, gpgme_data_t cipher, gpgme_data_t plain); /* Decrypt ciphertext CIPHER within CTX and store the resulting * plaintext in PLAIN. With the flag GPGME_DECRYPT_VERIFY also do a * signature verification pn the plaintext. */ gpgme_error_t gpgme_op_decrypt_ext_start (gpgme_ctx_t ctx, gpgme_decrypt_flags_t flags, gpgme_data_t cipher, gpgme_data_t plain); gpgme_error_t gpgme_op_decrypt_ext (gpgme_ctx_t ctx, gpgme_decrypt_flags_t flags, gpgme_data_t cipher, gpgme_data_t plain); /* * Signing. */ /* An object with signatures data. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_new_signature { struct _gpgme_new_signature *next; /* The type of the signature. */ gpgme_sig_mode_t type; /* The public key algorithm used to create the signature. */ gpgme_pubkey_algo_t pubkey_algo; /* The hash algorithm used to create the signature. */ gpgme_hash_algo_t hash_algo; /* Internal to GPGME, do not use. Must be set to the same value as * CLASS below. */ unsigned long _obsolete_class; /* Signature creation time. */ long int timestamp; /* The fingerprint of the signature. */ char *fpr; /* Deprecated; use SIG_CLASS instead. */ #ifdef _GPGME_OBSOLETE_SOME_SYMBOLS unsigned int _obsolete_class_2; #else unsigned int class _GPGME_DEPRECATED_OUTSIDE_GPGME(0,4); #endif /* Crypto backend specific signature class. */ unsigned int sig_class; }; typedef struct _gpgme_new_signature *gpgme_new_signature_t; /* An object to return results from a signing operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_sign_result { /* The list of invalid signers. */ gpgme_invalid_key_t invalid_signers; gpgme_new_signature_t signatures; }; typedef struct _gpgme_op_sign_result *gpgme_sign_result_t; /* Retrieve a pointer to the result of the signing operation. */ gpgme_sign_result_t gpgme_op_sign_result (gpgme_ctx_t ctx); /* Sign the plaintext PLAIN and store the signature in SIG. */ gpgme_error_t gpgme_op_sign_start (gpgme_ctx_t ctx, gpgme_data_t plain, gpgme_data_t sig, gpgme_sig_mode_t mode); gpgme_error_t gpgme_op_sign (gpgme_ctx_t ctx, gpgme_data_t plain, gpgme_data_t sig, gpgme_sig_mode_t mode); /* * Verify. */ /* Flags used for the SUMMARY field in a gpgme_signature_t. */ typedef enum { GPGME_SIGSUM_VALID = 0x0001, /* The signature is fully valid. */ GPGME_SIGSUM_GREEN = 0x0002, /* The signature is good. */ GPGME_SIGSUM_RED = 0x0004, /* The signature is bad. */ GPGME_SIGSUM_KEY_REVOKED = 0x0010, /* One key has been revoked. */ GPGME_SIGSUM_KEY_EXPIRED = 0x0020, /* One key has expired. */ GPGME_SIGSUM_SIG_EXPIRED = 0x0040, /* The signature has expired. */ GPGME_SIGSUM_KEY_MISSING = 0x0080, /* Can't verify: key missing. */ GPGME_SIGSUM_CRL_MISSING = 0x0100, /* CRL not available. */ GPGME_SIGSUM_CRL_TOO_OLD = 0x0200, /* Available CRL is too old. */ GPGME_SIGSUM_BAD_POLICY = 0x0400, /* A policy was not met. */ GPGME_SIGSUM_SYS_ERROR = 0x0800, /* A system error occurred. */ GPGME_SIGSUM_TOFU_CONFLICT=0x1000 /* Tofu conflict detected. */ } gpgme_sigsum_t; /* An object to hold the verification status of a signature. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_signature { struct _gpgme_signature *next; /* A summary of the signature status. */ gpgme_sigsum_t summary; /* The fingerprint of the signature. This can be a subkey. */ char *fpr; /* The status of the signature. */ gpgme_error_t status; /* Notation data and policy URLs. */ gpgme_sig_notation_t notations; /* Signature creation time. */ unsigned long timestamp; /* Signature expiration time or 0. */ unsigned long exp_timestamp; /* Key should not have been used for signing. */ unsigned int wrong_key_usage : 1; /* PKA status: 0 = not available, 1 = bad, 2 = okay, 3 = RFU. */ unsigned int pka_trust : 2; /* Validity has been verified using the chain model. */ unsigned int chain_model : 1; /* True if the signature is in compliance to the de-vs mode. */ unsigned int is_de_vs : 1; /* Internal to GPGME, do not use. */ int _unused : 27; gpgme_validity_t validity; gpgme_error_t validity_reason; /* The public key algorithm used to create the signature. */ gpgme_pubkey_algo_t pubkey_algo; /* The hash algorithm used to create the signature. */ gpgme_hash_algo_t hash_algo; /* The mailbox from the PKA information or NULL. */ char *pka_address; /* If non-NULL, a possible incomplete key object with the data * available for the signature. */ gpgme_key_t key; }; typedef struct _gpgme_signature *gpgme_signature_t; /* An object to return the results of a verify operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_verify_result { gpgme_signature_t signatures; /* The original file name of the plaintext message, if available. * Warning: This information is not covered by the signature. */ char *file_name; /* The message claims that the content is a MIME object. */ /* Warning: This flag is not covered by the signature. */ unsigned int is_mime : 1; /* Internal to GPGME; do not use. */ unsigned int _unused : 31; }; typedef struct _gpgme_op_verify_result *gpgme_verify_result_t; /* Retrieve a pointer to the result of the verify operation. */ gpgme_verify_result_t gpgme_op_verify_result (gpgme_ctx_t ctx); /* Verify within CTX that SIG is a valid signature for TEXT. */ gpgme_error_t gpgme_op_verify_start (gpgme_ctx_t ctx, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext); gpgme_error_t gpgme_op_verify (gpgme_ctx_t ctx, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext); /* * Import/Export */ #define GPGME_IMPORT_NEW 1 /* The key was new. */ #define GPGME_IMPORT_UID 2 /* The key contained new user IDs. */ #define GPGME_IMPORT_SIG 4 /* The key contained new signatures. */ #define GPGME_IMPORT_SUBKEY 8 /* The key contained new sub keys. */ #define GPGME_IMPORT_SECRET 16 /* The key contained a secret key. */ /* An object to hold results for one imported key. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_import_status { struct _gpgme_import_status *next; /* Fingerprint. */ char *fpr; /* If a problem occurred, the reason why the key could not be imported. Otherwise GPGME_No_Error. */ gpgme_error_t result; /* The result of the import, the GPGME_IMPORT_* values bit-wise ORed. 0 means the key was already known and no new components have been added. */ unsigned int status; }; typedef struct _gpgme_import_status *gpgme_import_status_t; /* Import result object. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_import_result { /* Number of considered keys. */ int considered; /* Keys without user ID. */ int no_user_id; /* Imported keys. */ int imported; /* Imported RSA keys. */ int imported_rsa; /* Unchanged keys. */ int unchanged; /* Number of new user ids. */ int new_user_ids; /* Number of new sub keys. */ int new_sub_keys; /* Number of new signatures. */ int new_signatures; /* Number of new revocations. */ int new_revocations; /* Number of secret keys read. */ int secret_read; /* Number of secret keys imported. */ int secret_imported; /* Number of secret keys unchanged. */ int secret_unchanged; /* Number of new keys skipped. */ int skipped_new_keys; /* Number of keys not imported. */ int not_imported; /* List of keys for which an import was attempted. */ gpgme_import_status_t imports; /* Number of v3 keys skipped. */ int skipped_v3_keys; }; typedef struct _gpgme_op_import_result *gpgme_import_result_t; /* Retrieve a pointer to the result of the import operation. */ gpgme_import_result_t gpgme_op_import_result (gpgme_ctx_t ctx); /* Import the key in KEYDATA into the keyring. */ gpgme_error_t gpgme_op_import_start (gpgme_ctx_t ctx, gpgme_data_t keydata); gpgme_error_t gpgme_op_import (gpgme_ctx_t ctx, gpgme_data_t keydata); /* Import the keys from the array KEYS into the keyring. */ gpgme_error_t gpgme_op_import_keys_start (gpgme_ctx_t ctx, gpgme_key_t keys[]); gpgme_error_t gpgme_op_import_keys (gpgme_ctx_t ctx, gpgme_key_t keys[]); /* Export the keys found by PATTERN into KEYDATA. */ gpgme_error_t gpgme_op_export_start (gpgme_ctx_t ctx, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata); gpgme_error_t gpgme_op_export (gpgme_ctx_t ctx, const char *pattern, gpgme_export_mode_t mode, gpgme_data_t keydata); gpgme_error_t gpgme_op_export_ext_start (gpgme_ctx_t ctx, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata); gpgme_error_t gpgme_op_export_ext (gpgme_ctx_t ctx, const char *pattern[], gpgme_export_mode_t mode, gpgme_data_t keydata); /* Export the keys from the array KEYS into KEYDATA. */ gpgme_error_t gpgme_op_export_keys_start (gpgme_ctx_t ctx, gpgme_key_t keys[], gpgme_export_mode_t mode, gpgme_data_t keydata); gpgme_error_t gpgme_op_export_keys (gpgme_ctx_t ctx, gpgme_key_t keys[], gpgme_export_mode_t mode, gpgme_data_t keydata); /* * Key generation. */ /* Flags for the key creation functions. */ #define GPGME_CREATE_SIGN (1 << 0) /* Allow usage: signing. */ #define GPGME_CREATE_ENCR (1 << 1) /* Allow usage: encryption. */ #define GPGME_CREATE_CERT (1 << 2) /* Allow usage: certification. */ #define GPGME_CREATE_AUTH (1 << 3) /* Allow usage: authentication. */ #define GPGME_CREATE_NOPASSWD (1 << 7) /* Create w/o passphrase. */ #define GPGME_CREATE_SELFSIGNED (1 << 8) /* Create self-signed cert. */ #define GPGME_CREATE_NOSTORE (1 << 9) /* Do not store the key. */ #define GPGME_CREATE_WANTPUB (1 << 10) /* Return the public key. */ #define GPGME_CREATE_WANTSEC (1 << 11) /* Return the secret key. */ #define GPGME_CREATE_FORCE (1 << 12) /* Force creation. */ #define GPGME_CREATE_NOEXPIRE (1 << 13) /* Create w/o expiration. */ /* An object to return result from a key generation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_genkey_result { /* A primary key was generated. */ unsigned int primary : 1; /* A sub key was generated. */ unsigned int sub : 1; /* A user id was generated. */ unsigned int uid : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 29; /* The fingerprint of the generated key. */ char *fpr; /* A memory data object with the created public key. Only set when * GPGME_CREATE_WANTPUB has been used. */ gpgme_data_t pubkey; /* A memory data object with the created secret key. Only set when * GPGME_CREATE_WANTSEC has been used. */ gpgme_data_t seckey; }; typedef struct _gpgme_op_genkey_result *gpgme_genkey_result_t; /* Generate a new keypair and add it to the keyring. PUBKEY and * SECKEY should be null for now. PARMS specifies what keys should be * generated. */ gpgme_error_t gpgme_op_genkey_start (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey, gpgme_data_t seckey); gpgme_error_t gpgme_op_genkey (gpgme_ctx_t ctx, const char *parms, gpgme_data_t pubkey, gpgme_data_t seckey); /* Generate a key pair using the modern interface. */ gpgme_error_t gpgme_op_createkey_start (gpgme_ctx_t ctx, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t certkey, unsigned int flags); gpgme_error_t gpgme_op_createkey (gpgme_ctx_t ctx, const char *userid, const char *algo, unsigned long reserved, unsigned long expires, gpgme_key_t certkey, unsigned int flags); /* Add a new subkey to KEY. */ gpgme_error_t gpgme_op_createsubkey_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo, unsigned long reserved, unsigned long expires, unsigned int flags); gpgme_error_t gpgme_op_createsubkey (gpgme_ctx_t ctx, gpgme_key_t key, const char *algo, unsigned long reserved, unsigned long expires, unsigned int flags); /* Add USERID to an existing KEY. */ gpgme_error_t gpgme_op_adduid_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int reserved); gpgme_error_t gpgme_op_adduid (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int reserved); /* Revoke a USERID from a KEY. */ gpgme_error_t gpgme_op_revuid_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int reserved); gpgme_error_t gpgme_op_revuid (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned int reserved); /* Set a flag on the USERID of KEY. See the manual for supported flags. */ gpgme_error_t gpgme_op_set_uid_flag_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, const char *name, const char *value); gpgme_error_t gpgme_op_set_uid_flag (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, const char *name, const char *value); /* Retrieve a pointer to the result of a genkey, createkey, or * createsubkey operation. */ gpgme_genkey_result_t gpgme_op_genkey_result (gpgme_ctx_t ctx); /* Delete KEY from the keyring. If ALLOW_SECRET is non-zero, secret * keys are also deleted. */ gpgme_error_t gpgme_op_delete_start (gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret); gpgme_error_t gpgme_op_delete (gpgme_ctx_t ctx, const gpgme_key_t key, int allow_secret); /* Flags for the key delete functions. */ #define GPGME_DELETE_ALLOW_SECRET (1 << 0) /* Also delete secret key. */ #define GPGME_DELETE_FORCE (1 << 1) /* Do not ask user to confirm. */ gpgme_error_t gpgme_op_delete_ext_start (gpgme_ctx_t ctx, const gpgme_key_t key, unsigned int flags); gpgme_error_t gpgme_op_delete_ext (gpgme_ctx_t ctx, const gpgme_key_t key, unsigned int flags); /* * Key signing interface */ /* Flags for the key signing functions. */ #define GPGME_KEYSIGN_LOCAL (1 << 7) /* Create a local signature. */ #define GPGME_KEYSIGN_LFSEP (1 << 8) /* Indicate LF separated user ids. */ #define GPGME_KEYSIGN_NOEXPIRE (1 << 9) /* Force no expiration. */ /* Sign the USERID of KEY using the current set of signers. */ gpgme_error_t gpgme_op_keysign_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags); gpgme_error_t gpgme_op_keysign (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags); /* * Key edit interface */ /* Flags to select the mode of the interact. */ #define GPGME_INTERACT_CARD (1 << 0) /* Use --card-edit mode. */ /* Edit the KEY. Send status and command requests to FNC and output of edit commands to OUT. */ gpgme_error_t gpgme_op_interact_start (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags, gpgme_interact_cb_t fnc, void *fnc_value, gpgme_data_t out); gpgme_error_t gpgme_op_interact (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags, gpgme_interact_cb_t fnc, void *fnc_value, gpgme_data_t out); /* Set the Tofu policy of KEY to POLCIY. */ gpgme_error_t gpgme_op_tofu_policy_start (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_tofu_policy_t policy); gpgme_error_t gpgme_op_tofu_policy (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_tofu_policy_t policy); /* * Key listing */ /* An object to return results from a key listing operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_keylist_result { unsigned int truncated : 1; /* Internal to GPGME, do not use. */ unsigned int _unused : 31; }; typedef struct _gpgme_op_keylist_result *gpgme_keylist_result_t; /* Retrieve a pointer to the result of the key listing operation. */ gpgme_keylist_result_t gpgme_op_keylist_result (gpgme_ctx_t ctx); /* Start a keylist operation within CTX, searching for keys which * match PATTERN. If SECRET_ONLY is true, only secret keys are * returned. */ gpgme_error_t gpgme_op_keylist_start (gpgme_ctx_t ctx, const char *pattern, int secret_only); gpgme_error_t gpgme_op_keylist_ext_start (gpgme_ctx_t ctx, const char *pattern[], int secret_only, int reserved); /* List the keys contained in DATA. */ gpgme_error_t gpgme_op_keylist_from_data_start (gpgme_ctx_t ctx, gpgme_data_t data, int reserved); /* Return the next key from the keylist in R_KEY. */ gpgme_error_t gpgme_op_keylist_next (gpgme_ctx_t ctx, gpgme_key_t *r_key); /* Terminate a pending keylist operation within CTX. */ gpgme_error_t gpgme_op_keylist_end (gpgme_ctx_t ctx); /* * Protecting keys */ /* Change the passphrase for KEY. FLAGS is reserved for future use * and must be passed as 0. */ gpgme_error_t gpgme_op_passwd_start (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags); gpgme_error_t gpgme_op_passwd (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags); /* * Trust items and operations. */ /* An object to hold data of a trust item. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_trust_item { /* Internal to GPGME, do not use. */ unsigned int _refs; /* The key ID to which the trust item belongs. */ char *keyid; /* Internal to GPGME, do not use. */ char _keyid[16 + 1]; /* The type of the trust item, 1 refers to a key, 2 to a user ID. */ int type; /* The trust level. */ int level; /* The owner trust if TYPE is 1. */ char *owner_trust; /* Internal to GPGME, do not use. */ char _owner_trust[2]; /* The calculated validity. */ char *validity; /* Internal to GPGME, do not use. */ char _validity[2]; /* The user name if TYPE is 2. */ char *name; }; typedef struct _gpgme_trust_item *gpgme_trust_item_t; /* Start a trustlist operation within CTX, searching for trust items which match PATTERN. */ gpgme_error_t gpgme_op_trustlist_start (gpgme_ctx_t ctx, const char *pattern, int max_level); /* Return the next trust item from the trustlist in R_ITEM. */ gpgme_error_t gpgme_op_trustlist_next (gpgme_ctx_t ctx, gpgme_trust_item_t *r_item); /* Terminate a pending trustlist operation within CTX. */ gpgme_error_t gpgme_op_trustlist_end (gpgme_ctx_t ctx); /* Acquire a reference to ITEM. */ void gpgme_trust_item_ref (gpgme_trust_item_t item); /* Release a reference to ITEM. If this was the last one the trust * item is destroyed. */ void gpgme_trust_item_unref (gpgme_trust_item_t item); /* * Audit log */ /* Return the auditlog for the current session. This may be called after a successful or failed operation. If no audit log is available GPG_ERR_NO_DATA is returned. */ gpgme_error_t gpgme_op_getauditlog_start (gpgme_ctx_t ctx, gpgme_data_t output, unsigned int flags); gpgme_error_t gpgme_op_getauditlog (gpgme_ctx_t ctx, gpgme_data_t output, unsigned int flags); /* * Spawn interface */ /* Flags for the spawn operations. */ #define GPGME_SPAWN_DETACHED 1 #define GPGME_SPAWN_ALLOW_SET_FG 2 #define GPGME_SPAWN_SHOW_WINDOW 4 /* Run the command FILE with the arguments in ARGV. Connect stdin to * DATAIN, stdout to DATAOUT, and STDERR to DATAERR. If one the data * streams is NULL, connect to /dev/null instead. */ gpgme_error_t gpgme_op_spawn_start (gpgme_ctx_t ctx, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags); gpgme_error_t gpgme_op_spawn (gpgme_ctx_t ctx, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags); /* * Low-level Assuan protocol access. */ typedef gpgme_error_t (*gpgme_assuan_data_cb_t) (void *opaque, const void *data, size_t datalen); typedef gpgme_error_t (*gpgme_assuan_inquire_cb_t) (void *opaque, const char *name, const char *args, gpgme_data_t *r_data); typedef gpgme_error_t (*gpgme_assuan_status_cb_t) (void *opaque, const char *status, const char *args); /* Send the Assuan COMMAND and return results via the callbacks. * Asynchronous variant. */ gpgme_error_t gpgme_op_assuan_transact_start (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t stat_cb, void *stat_cb_value); /* Send the Assuan COMMAND and return results via the callbacks. * Synchronous variant. */ gpgme_error_t gpgme_op_assuan_transact_ext (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t stat_cb, void *stat_cb_value, gpgme_error_t *op_err); /* * Crypto container support. */ /* An object to return results from a VFS mount operation. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_vfs_mount_result { char *mount_dir; }; typedef struct _gpgme_op_vfs_mount_result *gpgme_vfs_mount_result_t; gpgme_vfs_mount_result_t gpgme_op_vfs_mount_result (gpgme_ctx_t ctx); /* The container is automatically unmounted when the context is reset * or destroyed. Transmission errors are returned directly, * operational errors are returned in OP_ERR. */ gpgme_error_t gpgme_op_vfs_mount (gpgme_ctx_t ctx, const char *container_file, const char *mount_dir, unsigned int flags, gpgme_error_t *op_err); gpgme_error_t gpgme_op_vfs_create (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *container_file, unsigned int flags, gpgme_error_t *op_err); /* * Interface to gpgconf(1). */ /* The expert level at which a configuration option or group of * options should be displayed. See the gpgconf(1) documentation for * more details. */ typedef enum { GPGME_CONF_BASIC = 0, GPGME_CONF_ADVANCED = 1, GPGME_CONF_EXPERT = 2, GPGME_CONF_INVISIBLE = 3, GPGME_CONF_INTERNAL = 4 } gpgme_conf_level_t; /* The data type of a configuration option argument. See the gpgconf(1) * documentation for more details. */ typedef enum { /* Basic types. */ GPGME_CONF_NONE = 0, GPGME_CONF_STRING = 1, GPGME_CONF_INT32 = 2, GPGME_CONF_UINT32 = 3, /* Complex types. */ GPGME_CONF_FILENAME = 32, GPGME_CONF_LDAP_SERVER = 33, GPGME_CONF_KEY_FPR = 34, GPGME_CONF_PUB_KEY = 35, GPGME_CONF_SEC_KEY = 36, GPGME_CONF_ALIAS_LIST = 37 } gpgme_conf_type_t; /* For now, compatibility. */ #define GPGME_CONF_PATHNAME GPGME_CONF_FILENAME /* This represents a single argument for a configuration option. * Which of the members of value is used depends on the ALT_TYPE. */ typedef struct gpgme_conf_arg { struct gpgme_conf_arg *next; /* True if the option appears without an (optional) argument. */ unsigned int no_arg; union { unsigned int count; unsigned int uint32; int int32; char *string; } value; } *gpgme_conf_arg_t; /* The flags of a configuration option. See the gpgconf * documentation for details. */ #define GPGME_CONF_GROUP (1 << 0) #define GPGME_CONF_OPTIONAL (1 << 1) #define GPGME_CONF_LIST (1 << 2) #define GPGME_CONF_RUNTIME (1 << 3) #define GPGME_CONF_DEFAULT (1 << 4) #define GPGME_CONF_DEFAULT_DESC (1 << 5) #define GPGME_CONF_NO_ARG_DESC (1 << 6) #define GPGME_CONF_NO_CHANGE (1 << 7) /* The representation of a single configuration option. See the * gpg-conf documentation for details. */ typedef struct gpgme_conf_opt { struct gpgme_conf_opt *next; /* The option name. */ char *name; /* The flags for this option. */ unsigned int flags; /* The level of this option. */ gpgme_conf_level_t level; /* The localized description of this option. */ char *description; /* The type and alternate type of this option. */ gpgme_conf_type_t type; gpgme_conf_type_t alt_type; /* The localized (short) name of the argument, if any. */ char *argname; /* The default value. */ gpgme_conf_arg_t default_value; char *default_description; /* The default value if the option is not set. */ gpgme_conf_arg_t no_arg_value; char *no_arg_description; /* The current value if the option is set. */ gpgme_conf_arg_t value; /* The new value, if any. NULL means reset to default. */ int change_value; gpgme_conf_arg_t new_value; /* Free for application use. */ void *user_data; } *gpgme_conf_opt_t; /* The representation of a component that can be configured. See the * gpg-conf documentation for details. */ typedef struct gpgme_conf_comp { struct gpgme_conf_comp *next; /* Internal to GPGME, do not use! */ gpgme_conf_opt_t *_last_opt_p; /* The component name. */ char *name; /* A human-readable description for the component. */ char *description; /* The program name (an absolute path to the program). */ char *program_name; /* A linked list of options for this component. */ struct gpgme_conf_opt *options; } *gpgme_conf_comp_t; /* Allocate a new gpgme_conf_arg_t. If VALUE is NULL, a "no arg * default" is prepared. If type is a string type, VALUE should point * to the string. Else, it should point to an unsigned or signed * integer respectively. */ gpgme_error_t gpgme_conf_arg_new (gpgme_conf_arg_t *arg_p, gpgme_conf_type_t type, const void *value); /* This also releases all chained argument structures! */ void gpgme_conf_arg_release (gpgme_conf_arg_t arg, gpgme_conf_type_t type); /* Register a change for the value of OPT to ARG. If RESET is 1 (do * not use any values but 0 or 1), ARG is ignored and the option is * not changed (reverting a previous change). Otherwise, if ARG is * NULL, the option is cleared or reset to its default. The change * is done with gpgconf's --runtime option to immediately take effect. */ gpgme_error_t gpgme_conf_opt_change (gpgme_conf_opt_t opt, int reset, gpgme_conf_arg_t arg); /* Release a set of configurations. */ void gpgme_conf_release (gpgme_conf_comp_t conf); /* Retrieve the current configurations. */ gpgme_error_t gpgme_op_conf_load (gpgme_ctx_t ctx, gpgme_conf_comp_t *conf_p); /* Save the configuration of component comp. This function does not follow chained components! */ gpgme_error_t gpgme_op_conf_save (gpgme_ctx_t ctx, gpgme_conf_comp_t comp); /* Retrieve the configured directory. */ gpgme_error_t gpgme_op_conf_dir(gpgme_ctx_t ctx, const char *what, char **result); /* Information about software versions. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ typedef struct _gpgme_op_query_swdb_result { /* RFU */ struct _gpgme_op_query_swdb_result *next; /* The name of the package (e.g. "gpgme", "gnupg") */ char *name; /* The version number of the installed version. */ char *iversion; /* The time the online info was created. */ unsigned long created; /* The time the online info was retrieved. */ unsigned long retrieved; /* This bit is set if an error occured or some of the information * in this structure may not be set. */ unsigned int warning : 1; /* An update is available. */ unsigned int update : 1; /* The update is important. */ unsigned int urgent : 1; /* No information at all available. */ unsigned int noinfo : 1; /* The package name is not known. */ unsigned int unknown : 1; /* The information here is too old. */ unsigned int tooold : 1; /* Other error. */ unsigned int error : 1; unsigned int _reserved : 25; /* The version number of the latest released version. */ char *version; /* The release date of that version. */ unsigned long reldate; } *gpgme_query_swdb_result_t; /* Run the gpgconf --query-swdb command. */ gpgme_error_t gpgme_op_query_swdb (gpgme_ctx_t ctx, const char *name, const char *iversion, unsigned int reserved); /* Return the result from the last query_swdb operation. */ gpgme_query_swdb_result_t gpgme_op_query_swdb_result (gpgme_ctx_t ctx); /* * Various functions. */ /* Set special global flags; consult the manual before use. */ int gpgme_set_global_flag (const char *name, const char *value); /* Check that the library fulfills the version requirement. Note: * This is here only for the case where a user takes a pointer from * the old version of this function. The new version and macro for * run-time checks are below. */ const char *gpgme_check_version (const char *req_version); /* Do not call this directly; use the macro below. */ const char *gpgme_check_version_internal (const char *req_version, size_t offset_sig_validity); /* Check that the library fulfills the version requirement and check * for struct layout mismatch involving bitfields. */ #define gpgme_check_version(req_version) \ gpgme_check_version_internal (req_version, \ offsetof (struct _gpgme_signature, validity)) /* Return the default values for various directories. */ const char *gpgme_get_dirinfo (const char *what); /* Get the information about the configured and installed engines. A * pointer to the first engine in the statically allocated linked list * is returned in *INFO. If an error occurs, it is returned. The * returned data is valid until the next gpgme_set_engine_info. */ gpgme_error_t gpgme_get_engine_info (gpgme_engine_info_t *engine_info); /* Set the default engine info for the protocol PROTO to the file name * FILE_NAME and the home directory HOME_DIR. */ gpgme_error_t gpgme_set_engine_info (gpgme_protocol_t proto, const char *file_name, const char *home_dir); /* Verify that the engine implementing PROTO is installed and * available. */ gpgme_error_t gpgme_engine_check_version (gpgme_protocol_t proto); /* Reference counting for result objects. */ void gpgme_result_ref (void *result); void gpgme_result_unref (void *result); /* Return a public key algorithm string (e.g. "rsa2048"). Caller must * free using gpgme_free. */ char *gpgme_pubkey_algo_string (gpgme_subkey_t subkey); /* Return a statically allocated string with the name of the public * key algorithm ALGO, or NULL if that name is not known. */ const char *gpgme_pubkey_algo_name (gpgme_pubkey_algo_t algo); /* Return a statically allocated string with the name of the hash * algorithm ALGO, or NULL if that name is not known. */ const char *gpgme_hash_algo_name (gpgme_hash_algo_t algo); /* Return the addr-spec from a user id. Caller must free the result * with gpgme_free. */ char *gpgme_addrspec_from_uid (const char *uid); /* * Deprecated types, constants and functions. */ /* The possible stati for gpgme_op_edit. The use of that function and * these status codes are deprecated in favor of gpgme_op_interact. */ typedef enum { GPGME_STATUS_EOF = 0, /* mkstatus processing starts here */ GPGME_STATUS_ENTER = 1, GPGME_STATUS_LEAVE = 2, GPGME_STATUS_ABORT = 3, GPGME_STATUS_GOODSIG = 4, GPGME_STATUS_BADSIG = 5, GPGME_STATUS_ERRSIG = 6, GPGME_STATUS_BADARMOR = 7, GPGME_STATUS_RSA_OR_IDEA = 8, /* (legacy) */ GPGME_STATUS_KEYEXPIRED = 9, GPGME_STATUS_KEYREVOKED = 10, GPGME_STATUS_TRUST_UNDEFINED = 11, GPGME_STATUS_TRUST_NEVER = 12, GPGME_STATUS_TRUST_MARGINAL = 13, GPGME_STATUS_TRUST_FULLY = 14, GPGME_STATUS_TRUST_ULTIMATE = 15, GPGME_STATUS_SHM_INFO = 16, /* (legacy) */ GPGME_STATUS_SHM_GET = 17, /* (legacy) */ GPGME_STATUS_SHM_GET_BOOL = 18, /* (legacy) */ GPGME_STATUS_SHM_GET_HIDDEN = 19, /* (legacy) */ GPGME_STATUS_NEED_PASSPHRASE = 20, GPGME_STATUS_VALIDSIG = 21, GPGME_STATUS_SIG_ID = 22, GPGME_STATUS_ENC_TO = 23, GPGME_STATUS_NODATA = 24, GPGME_STATUS_BAD_PASSPHRASE = 25, GPGME_STATUS_NO_PUBKEY = 26, GPGME_STATUS_NO_SECKEY = 27, GPGME_STATUS_NEED_PASSPHRASE_SYM = 28, GPGME_STATUS_DECRYPTION_FAILED = 29, GPGME_STATUS_DECRYPTION_OKAY = 30, GPGME_STATUS_MISSING_PASSPHRASE = 31, GPGME_STATUS_GOOD_PASSPHRASE = 32, GPGME_STATUS_GOODMDC = 33, GPGME_STATUS_BADMDC = 34, GPGME_STATUS_ERRMDC = 35, GPGME_STATUS_IMPORTED = 36, GPGME_STATUS_IMPORT_OK = 37, GPGME_STATUS_IMPORT_PROBLEM = 38, GPGME_STATUS_IMPORT_RES = 39, GPGME_STATUS_FILE_START = 40, GPGME_STATUS_FILE_DONE = 41, GPGME_STATUS_FILE_ERROR = 42, GPGME_STATUS_BEGIN_DECRYPTION = 43, GPGME_STATUS_END_DECRYPTION = 44, GPGME_STATUS_BEGIN_ENCRYPTION = 45, GPGME_STATUS_END_ENCRYPTION = 46, GPGME_STATUS_DELETE_PROBLEM = 47, GPGME_STATUS_GET_BOOL = 48, GPGME_STATUS_GET_LINE = 49, GPGME_STATUS_GET_HIDDEN = 50, GPGME_STATUS_GOT_IT = 51, GPGME_STATUS_PROGRESS = 52, GPGME_STATUS_SIG_CREATED = 53, GPGME_STATUS_SESSION_KEY = 54, GPGME_STATUS_NOTATION_NAME = 55, GPGME_STATUS_NOTATION_DATA = 56, GPGME_STATUS_POLICY_URL = 57, GPGME_STATUS_BEGIN_STREAM = 58, /* (legacy) */ GPGME_STATUS_END_STREAM = 59, /* (legacy) */ GPGME_STATUS_KEY_CREATED = 60, GPGME_STATUS_USERID_HINT = 61, GPGME_STATUS_UNEXPECTED = 62, GPGME_STATUS_INV_RECP = 63, GPGME_STATUS_NO_RECP = 64, GPGME_STATUS_ALREADY_SIGNED = 65, GPGME_STATUS_SIGEXPIRED = 66, /* (legacy) */ GPGME_STATUS_EXPSIG = 67, GPGME_STATUS_EXPKEYSIG = 68, GPGME_STATUS_TRUNCATED = 69, GPGME_STATUS_ERROR = 70, GPGME_STATUS_NEWSIG = 71, GPGME_STATUS_REVKEYSIG = 72, GPGME_STATUS_SIG_SUBPACKET = 73, GPGME_STATUS_NEED_PASSPHRASE_PIN = 74, GPGME_STATUS_SC_OP_FAILURE = 75, GPGME_STATUS_SC_OP_SUCCESS = 76, GPGME_STATUS_CARDCTRL = 77, GPGME_STATUS_BACKUP_KEY_CREATED = 78, GPGME_STATUS_PKA_TRUST_BAD = 79, GPGME_STATUS_PKA_TRUST_GOOD = 80, GPGME_STATUS_PLAINTEXT = 81, GPGME_STATUS_INV_SGNR = 82, GPGME_STATUS_NO_SGNR = 83, GPGME_STATUS_SUCCESS = 84, GPGME_STATUS_DECRYPTION_INFO = 85, GPGME_STATUS_PLAINTEXT_LENGTH = 86, GPGME_STATUS_MOUNTPOINT = 87, GPGME_STATUS_PINENTRY_LAUNCHED = 88, GPGME_STATUS_ATTRIBUTE = 89, GPGME_STATUS_BEGIN_SIGNING = 90, GPGME_STATUS_KEY_NOT_CREATED = 91, GPGME_STATUS_INQUIRE_MAXLEN = 92, GPGME_STATUS_FAILURE = 93, GPGME_STATUS_KEY_CONSIDERED = 94, GPGME_STATUS_TOFU_USER = 95, GPGME_STATUS_TOFU_STATS = 96, GPGME_STATUS_TOFU_STATS_LONG = 97, GPGME_STATUS_NOTATION_FLAGS = 98, GPGME_STATUS_DECRYPTION_COMPLIANCE_MODE = 99, GPGME_STATUS_VERIFICATION_COMPLIANCE_MODE = 100 } gpgme_status_code_t; /* The callback type used by the deprecated functions gpgme_op_edit * and gpgme_op_card_edit. */ typedef gpgme_error_t (*gpgme_edit_cb_t) (void *opaque, gpgme_status_code_t status, const char *args, int fd); gpgme_error_t gpgme_op_edit_start (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) _GPGME_DEPRECATED(1,7); gpgme_error_t gpgme_op_edit (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) _GPGME_DEPRECATED(1,7); gpgme_error_t gpgme_op_card_edit_start (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) _GPGME_DEPRECATED(1,7); gpgme_error_t gpgme_op_card_edit (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_edit_cb_t fnc, void *fnc_value, gpgme_data_t out) _GPGME_DEPRECATED(1,7); /* The possible signature stati. Deprecated, use error value in sig * status. */ typedef enum { GPGME_SIG_STAT_NONE = 0, GPGME_SIG_STAT_GOOD = 1, GPGME_SIG_STAT_BAD = 2, GPGME_SIG_STAT_NOKEY = 3, GPGME_SIG_STAT_NOSIG = 4, GPGME_SIG_STAT_ERROR = 5, GPGME_SIG_STAT_DIFF = 6, GPGME_SIG_STAT_GOOD_EXP = 7, GPGME_SIG_STAT_GOOD_EXPKEY = 8 } _gpgme_sig_stat_t; typedef _gpgme_sig_stat_t gpgme_sig_stat_t _GPGME_DEPRECATED(0,4); /* The available key and signature attributes. Deprecated, use the * individual result structures instead. */ typedef enum { GPGME_ATTR_KEYID = 1, GPGME_ATTR_FPR = 2, GPGME_ATTR_ALGO = 3, GPGME_ATTR_LEN = 4, GPGME_ATTR_CREATED = 5, GPGME_ATTR_EXPIRE = 6, GPGME_ATTR_OTRUST = 7, GPGME_ATTR_USERID = 8, GPGME_ATTR_NAME = 9, GPGME_ATTR_EMAIL = 10, GPGME_ATTR_COMMENT = 11, GPGME_ATTR_VALIDITY = 12, GPGME_ATTR_LEVEL = 13, GPGME_ATTR_TYPE = 14, GPGME_ATTR_IS_SECRET = 15, GPGME_ATTR_KEY_REVOKED = 16, GPGME_ATTR_KEY_INVALID = 17, GPGME_ATTR_UID_REVOKED = 18, GPGME_ATTR_UID_INVALID = 19, GPGME_ATTR_KEY_CAPS = 20, GPGME_ATTR_CAN_ENCRYPT = 21, GPGME_ATTR_CAN_SIGN = 22, GPGME_ATTR_CAN_CERTIFY = 23, GPGME_ATTR_KEY_EXPIRED = 24, GPGME_ATTR_KEY_DISABLED = 25, GPGME_ATTR_SERIAL = 26, GPGME_ATTR_ISSUER = 27, GPGME_ATTR_CHAINID = 28, GPGME_ATTR_SIG_STATUS = 29, GPGME_ATTR_ERRTOK = 30, GPGME_ATTR_SIG_SUMMARY = 31, GPGME_ATTR_SIG_CLASS = 32 } _gpgme_attr_t; typedef _gpgme_attr_t gpgme_attr_t _GPGME_DEPRECATED(0,4); /* Retrieve the signature status of signature IDX in CTX after a * successful verify operation in R_STAT (if non-null). The creation * time stamp of the signature is returned in R_CREATED (if non-null). * The function returns a string containing the fingerprint. * Deprecated, use verify result directly. */ const char *gpgme_get_sig_status (gpgme_ctx_t ctx, int idx, _gpgme_sig_stat_t *r_stat, time_t *r_created) _GPGME_DEPRECATED(0,4); /* Retrieve certain attributes of a signature. IDX is the index * number of the signature after a successful verify operation. WHAT * is an attribute where GPGME_ATTR_EXPIRE is probably the most useful * one. WHATIDX is to be passed as 0 for most attributes . */ unsigned long gpgme_get_sig_ulong_attr (gpgme_ctx_t c, int idx, _gpgme_attr_t what, int whatidx) _GPGME_DEPRECATED(0,4); const char *gpgme_get_sig_string_attr (gpgme_ctx_t c, int idx, _gpgme_attr_t what, int whatidx) _GPGME_DEPRECATED(0,4); /* Get the key used to create signature IDX in CTX and return it in * R_KEY. */ gpgme_error_t gpgme_get_sig_key (gpgme_ctx_t ctx, int idx, gpgme_key_t *r_key) _GPGME_DEPRECATED(0,4); /* Create a new data buffer which retrieves the data from the callback * function READ_CB. Deprecated, please use gpgme_data_new_from_cbs * instead. */ gpgme_error_t gpgme_data_new_with_read_cb (gpgme_data_t *r_dh, int (*read_cb) (void*,char *, size_t,size_t*), void *read_cb_value) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of KEY, which has to be * representable by a string. IDX specifies the sub key or user ID * for attributes related to sub keys or user IDs. Deprecated, use * key structure directly instead. */ const char *gpgme_key_get_string_attr (gpgme_key_t key, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of KEY, which has to be * representable by an unsigned integer. IDX specifies the sub key or * user ID for attributes related to sub keys or user IDs. * Deprecated, use key structure directly instead. */ unsigned long gpgme_key_get_ulong_attr (gpgme_key_t key, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of a signature on user ID * UID_IDX in KEY, which has to be representable by a string. IDX * specifies the signature. Deprecated, use key structure directly * instead. */ const char *gpgme_key_sig_get_string_attr (gpgme_key_t key, int uid_idx, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of a signature on user ID * UID_IDX in KEY, which has to be representable by an unsigned * integer string. IDX specifies the signature. Deprecated, use key * structure directly instead. */ unsigned long gpgme_key_sig_get_ulong_attr (gpgme_key_t key, int uid_idx, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); gpgme_error_t gpgme_op_import_ext (gpgme_ctx_t ctx, gpgme_data_t keydata, int *nr) _GPGME_DEPRECATED(0,4); /* Release the trust item ITEM. Deprecated, use * gpgme_trust_item_unref. */ void gpgme_trust_item_release (gpgme_trust_item_t item) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of ITEM, which has to be * representable by a string. Deprecated, use trust item structure * directly. */ const char *gpgme_trust_item_get_string_attr (gpgme_trust_item_t item, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); /* Return the value of the attribute WHAT of KEY, which has to be * representable by an integer. IDX specifies a running index if the * attribute appears more than once in the key. Deprecated, use trust * item structure directly. */ int gpgme_trust_item_get_int_attr (gpgme_trust_item_t item, _gpgme_attr_t what, const void *reserved, int idx) _GPGME_DEPRECATED(0,4); /* Compat. * This structure shall be considered read-only and an application * must not allocate such a structure on its own. */ struct _gpgme_op_assuan_result { /* Deprecated. Use the second value in a DONE event or the synchronous variant gpgme_op_assuan_transact_ext. */ gpgme_error_t err _GPGME_DEPRECATED_OUTSIDE_GPGME(1,2); }; typedef struct _gpgme_op_assuan_result *gpgme_assuan_result_t; /* Return the result of the last Assuan command. */ gpgme_assuan_result_t gpgme_op_assuan_result (gpgme_ctx_t ctx) _GPGME_DEPRECATED(1,2); gpgme_error_t gpgme_op_assuan_transact (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) _GPGME_DEPRECATED(1,2); typedef gpgme_ctx_t GpgmeCtx _GPGME_DEPRECATED(0,4); typedef gpgme_data_t GpgmeData _GPGME_DEPRECATED(0,4); typedef gpgme_error_t GpgmeError _GPGME_DEPRECATED(0,4); typedef gpgme_data_encoding_t GpgmeDataEncoding _GPGME_DEPRECATED(0,4); typedef gpgme_pubkey_algo_t GpgmePubKeyAlgo _GPGME_DEPRECATED(0,4); typedef gpgme_hash_algo_t GpgmeHashAlgo _GPGME_DEPRECATED(0,4); typedef gpgme_sig_stat_t GpgmeSigStat _GPGME_DEPRECATED(0,4); typedef gpgme_sig_mode_t GpgmeSigMode _GPGME_DEPRECATED(0,4); typedef gpgme_attr_t GpgmeAttr _GPGME_DEPRECATED(0,4); typedef gpgme_validity_t GpgmeValidity _GPGME_DEPRECATED(0,4); typedef gpgme_protocol_t GpgmeProtocol _GPGME_DEPRECATED(0,4); typedef gpgme_engine_info_t GpgmeEngineInfo _GPGME_DEPRECATED(0,4); typedef gpgme_subkey_t GpgmeSubkey _GPGME_DEPRECATED(0,4); typedef gpgme_key_sig_t GpgmeKeySig _GPGME_DEPRECATED(0,4); typedef gpgme_user_id_t GpgmeUserID _GPGME_DEPRECATED(0,4); typedef gpgme_key_t GpgmeKey _GPGME_DEPRECATED(0,4); typedef gpgme_passphrase_cb_t GpgmePassphraseCb _GPGME_DEPRECATED(0,4); typedef gpgme_progress_cb_t GpgmeProgressCb _GPGME_DEPRECATED(0,4); typedef gpgme_io_cb_t GpgmeIOCb _GPGME_DEPRECATED(0,4); typedef gpgme_register_io_cb_t GpgmeRegisterIOCb _GPGME_DEPRECATED(0,4); typedef gpgme_remove_io_cb_t GpgmeRemoveIOCb _GPGME_DEPRECATED(0,4); typedef gpgme_event_io_t GpgmeEventIO _GPGME_DEPRECATED(0,4); typedef gpgme_event_io_cb_t GpgmeEventIOCb _GPGME_DEPRECATED(0,4); #define GpgmeIOCbs gpgme_io_cbs typedef gpgme_data_read_cb_t GpgmeDataReadCb _GPGME_DEPRECATED(0,4); typedef gpgme_data_write_cb_t GpgmeDataWriteCb _GPGME_DEPRECATED(0,4); typedef gpgme_data_seek_cb_t GpgmeDataSeekCb _GPGME_DEPRECATED(0,4); typedef gpgme_data_release_cb_t GpgmeDataReleaseCb _GPGME_DEPRECATED(0,4); #define GpgmeDataCbs gpgme_data_cbs typedef gpgme_encrypt_result_t GpgmeEncryptResult _GPGME_DEPRECATED(0,4); typedef gpgme_sig_notation_t GpgmeSigNotation _GPGME_DEPRECATED(0,4); typedef gpgme_signature_t GpgmeSignature _GPGME_DEPRECATED(0,4); typedef gpgme_verify_result_t GpgmeVerifyResult _GPGME_DEPRECATED(0,4); typedef gpgme_import_status_t GpgmeImportStatus _GPGME_DEPRECATED(0,4); typedef gpgme_import_result_t GpgmeImportResult _GPGME_DEPRECATED(0,4); typedef gpgme_genkey_result_t GpgmeGenKeyResult _GPGME_DEPRECATED(0,4); typedef gpgme_trust_item_t GpgmeTrustItem _GPGME_DEPRECATED(0,4); typedef gpgme_status_code_t GpgmeStatusCode _GPGME_DEPRECATED(0,4); #ifdef __cplusplus } #endif #endif /* GPGME_H */ /* @emacs_local_vars_begin@ @emacs_local_vars_read_only@ @emacs_local_vars_end@ */ diff --git a/src/import.c b/src/import.c index f0d9d9fa..33779e96 100644 --- a/src/import.c +++ b/src/import.c @@ -1,451 +1,451 @@ /* import.c - Import a key. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" #include "util.h" typedef struct { struct _gpgme_op_import_result result; /* A pointer to the next pointer of the last import status in the list. This makes appending new imports painless while preserving the order. */ gpgme_import_status_t *lastp; } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; gpgme_import_status_t import = opd->result.imports; while (import) { gpgme_import_status_t next = import->next; free (import->fpr); free (import); import = next; } } gpgme_import_result_t gpgme_op_import_result (gpgme_ctx_t ctx) { void *hook; op_data_t opd; gpgme_error_t err; TRACE_BEG (DEBUG_CTX, "gpgme_op_import_result", ctx); err = _gpgme_op_data_lookup (ctx, OPDATA_IMPORT, &hook, -1, NULL); opd = hook; if (err || !opd) { TRACE_SUC0 ("result=(null)"); return NULL; } if (_gpgme_debug_trace ()) { gpgme_import_status_t impstat; int i; TRACE_LOG5 ("%i considered, %i no UID, %i imported, %i imported RSA, " "%i unchanged", opd->result.considered, opd->result.no_user_id, opd->result.imported, opd->result.imported_rsa, opd->result.unchanged); TRACE_LOG4 ("%i new UIDs, %i new sub keys, %i new signatures, " "%i new revocations", opd->result.new_user_ids, opd->result.new_sub_keys, opd->result.new_signatures, opd->result.new_revocations); TRACE_LOG3 ("%i secret keys, %i imported, %i unchanged", opd->result.secret_read, opd->result.secret_imported, opd->result.secret_unchanged); TRACE_LOG3 ("%i skipped new keys, %i not imported, %i v3 skipped", opd->result.skipped_new_keys, opd->result.not_imported, opd->result.skipped_v3_keys); impstat = opd->result.imports; i = 0; while (impstat) { TRACE_LOG4 ("import[%i] for %s = 0x%x (%s)", i, impstat->fpr, impstat->status, impstat->result); impstat = impstat->next; i++; } } TRACE_SUC1 ("result=%p", &opd->result); return &opd->result; } static gpgme_error_t parse_import (char *args, gpgme_import_status_t *import_status, int problem) { gpgme_import_status_t import; char *tail; long int nr; import = malloc (sizeof (*import)); if (!import) return gpg_error_from_syserror (); import->next = NULL; gpg_err_set_errno (0); nr = strtol (args, &tail, 0); if (errno || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ free (import); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args = tail; if (problem) { switch (nr) { case 0: case 4: default: import->result = gpg_error (GPG_ERR_GENERAL); break; case 1: import->result = gpg_error (GPG_ERR_BAD_CERT); break; case 2: import->result = gpg_error (GPG_ERR_MISSING_ISSUER_CERT); break; case 3: import->result = gpg_error (GPG_ERR_BAD_CERT_CHAIN); break; } import->status = 0; } else { import->result = gpg_error (GPG_ERR_NO_ERROR); import->status = nr; } while (*args == ' ') args++; tail = strchr (args, ' '); if (tail) *tail = '\0'; import->fpr = strdup (args); if (!import->fpr) { free (import); return gpg_error_from_syserror (); } *import_status = import; return 0; } gpgme_error_t parse_import_res (char *args, gpgme_import_result_t result) { char *tail; gpg_err_set_errno (0); #define PARSE_NEXT(x) \ (x) = strtol (args, &tail, 0); \ if (errno || args == tail || !(*tail == ' ' || !*tail)) \ /* The crypto backend does not behave. */ \ return trace_gpg_error (GPG_ERR_INV_ENGINE); \ args = tail; PARSE_NEXT (result->considered); PARSE_NEXT (result->no_user_id); PARSE_NEXT (result->imported); PARSE_NEXT (result->imported_rsa); PARSE_NEXT (result->unchanged); PARSE_NEXT (result->new_user_ids); PARSE_NEXT (result->new_sub_keys); PARSE_NEXT (result->new_signatures); PARSE_NEXT (result->new_revocations); PARSE_NEXT (result->secret_read); PARSE_NEXT (result->secret_imported); PARSE_NEXT (result->secret_unchanged); PARSE_NEXT (result->skipped_new_keys); PARSE_NEXT (result->not_imported); if (args && *args) { PARSE_NEXT (result->skipped_v3_keys); } return 0; } static gpgme_error_t import_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_IMPORT, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_IMPORT_OK: case GPGME_STATUS_IMPORT_PROBLEM: err = parse_import (args, opd->lastp, code == GPGME_STATUS_IMPORT_OK ? 0 : 1); if (err) return err; opd->lastp = &(*opd->lastp)->next; break; case GPGME_STATUS_IMPORT_RES: err = parse_import_res (args, &opd->result); break; default: break; } return err; } static gpgme_error_t _gpgme_op_import_start (gpgme_ctx_t ctx, int synchronous, gpgme_data_t keydata) { gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_IMPORT, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; opd->lastp = &opd->result.imports; if (!keydata) return gpg_error (GPG_ERR_NO_DATA); _gpgme_engine_set_status_handler (ctx->engine, import_status_handler, ctx); return _gpgme_engine_op_import (ctx->engine, keydata, NULL); } gpgme_error_t gpgme_op_import_start (gpgme_ctx_t ctx, gpgme_data_t keydata) { gpg_error_t err; TRACE_BEG1 (DEBUG_CTX, "gpgme_op_import_start", ctx, "keydata=%p", keydata); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_op_import_start (ctx, 0, keydata); return TRACE_ERR (err); } /* Import the key in KEYDATA into the keyring. */ gpgme_error_t gpgme_op_import (gpgme_ctx_t ctx, gpgme_data_t keydata) { gpgme_error_t err; TRACE_BEG1 (DEBUG_CTX, "gpgme_op_import", ctx, "keydata=%p", keydata); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_op_import_start (ctx, 1, keydata); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } static gpgme_error_t _gpgme_op_import_keys_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t *keys) { gpgme_error_t err; void *hook; op_data_t opd; int idx, firstidx, nkeys; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_IMPORT, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; opd->lastp = &opd->result.imports; if (!keys) return gpg_error (GPG_ERR_NO_DATA); for (idx=nkeys=0, firstidx=-1; keys[idx]; idx++) { /* We only consider keys of the current protocol. */ if (keys[idx]->protocol != ctx->protocol) continue; if (firstidx == -1) firstidx = idx; /* If a key has been found using a different key listing mode, we bail out. This makes the processing easier. Fixme: To allow a mix of keys we would need to sort them by key listing mode and start two import operations one after the other. */ if (keys[idx]->keylist_mode != keys[firstidx]->keylist_mode) return gpg_error (GPG_ERR_CONFLICT); nkeys++; } if (!nkeys) return gpg_error (GPG_ERR_NO_DATA); _gpgme_engine_set_status_handler (ctx->engine, import_status_handler, ctx); return _gpgme_engine_op_import (ctx->engine, NULL, keys); } /* Asynchronous version of gpgme_op_import_key. */ gpgme_error_t gpgme_op_import_keys_start (gpgme_ctx_t ctx, gpgme_key_t *keys) { gpg_error_t err; TRACE_BEG (DEBUG_CTX, "gpgme_op_import_keys_start", ctx); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && keys) { int i = 0; while (keys[i]) { TRACE_LOG3 ("keys[%i] = %p (%s)", i, keys[i], (keys[i]->subkeys && keys[i]->subkeys->fpr) ? keys[i]->subkeys->fpr : "invalid"); i++; } } err = _gpgme_op_import_keys_start (ctx, 0, keys); return TRACE_ERR (err); } /* Import the keys from the array KEYS into the keyring. In particular it is used to actually import keys retrieved from an external source (i.e. using GPGME_KEYLIST_MODE_EXTERN). It replaces the old workaround of exporting and then importing a key as used to make an X.509 key permanent. This function automagically does the right thing. KEYS is a NULL terminated array of gpgme key objects. The result is the usual import result structure. Only keys matching the current protocol are imported; other keys are ignored. */ gpgme_error_t gpgme_op_import_keys (gpgme_ctx_t ctx, gpgme_key_t *keys) { gpgme_error_t err; TRACE_BEG (DEBUG_CTX, "gpgme_op_import_keys", ctx); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && keys) { int i = 0; while (keys[i]) { TRACE_LOG3 ("keys[%i] = %p (%s)", i, keys[i], (keys[i]->subkeys && keys[i]->subkeys->fpr) ? keys[i]->subkeys->fpr : "invalid"); i++; } } err = _gpgme_op_import_keys_start (ctx, 1, keys); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } /* Deprecated interface. */ gpgme_error_t gpgme_op_import_ext (gpgme_ctx_t ctx, gpgme_data_t keydata, int *nr) { gpgme_error_t err = gpgme_op_import (ctx, keydata); if (!err && nr) { gpgme_import_result_t result = gpgme_op_import_result (ctx); *nr = result->considered; } return err; } diff --git a/src/isascii.c b/src/isascii.c index 924ced0c..867dd0ca 100644 --- a/src/isascii.c +++ b/src/isascii.c @@ -1,28 +1,28 @@ /* Copyright (C) 1991,92,93,95,96,97,98,99,2001,2002,2004 - Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ + * Free Software Foundation, Inc. + * This file is part of the GNU C Library. + * + * The GNU C Library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * The GNU C Library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif int isascii (int c) { return (((c) & ~0x7f) == 0); } diff --git a/src/key.c b/src/key.c index bb4d5fd6..e5dbdcb9 100644 --- a/src/key.c +++ b/src/key.c @@ -1,780 +1,780 @@ /* key.c - Key objects. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include "util.h" #include "ops.h" #include "sema.h" #include "debug.h" #include "mbox-util.h" /* Protects all reference counters in keys. All other accesses to a key are read only. */ DEFINE_STATIC_LOCK (key_ref_lock); /* Create a new key. */ gpgme_error_t _gpgme_key_new (gpgme_key_t *r_key) { gpgme_key_t key; key = calloc (1, sizeof *key); if (!key) return gpg_error_from_syserror (); key->_refs = 1; *r_key = key; return 0; } gpgme_error_t _gpgme_key_add_subkey (gpgme_key_t key, gpgme_subkey_t *r_subkey) { gpgme_subkey_t subkey; subkey = calloc (1, sizeof *subkey); if (!subkey) return gpg_error_from_syserror (); subkey->keyid = subkey->_keyid; subkey->_keyid[16] = '\0'; if (!key->subkeys) key->subkeys = subkey; if (key->_last_subkey) key->_last_subkey->next = subkey; key->_last_subkey = subkey; *r_subkey = subkey; return 0; } static char * set_user_id_part (char *tail, const char *buf, size_t len) { while (len && (buf[len - 1] == ' ' || buf[len - 1] == '\t')) len--; for (; len; len--) *tail++ = *buf++; *tail++ = 0; return tail; } static void parse_user_id (char *src, char **name, char **email, char **comment, char *tail) { const char *start = NULL; int in_name = 0; int in_email = 0; int in_comment = 0; while (*src) { if (in_email) { if (*src == '<') /* Not legal but anyway. */ in_email++; else if (*src == '>') { if (!--in_email && !*email) { *email = tail; tail = set_user_id_part (tail, start, src - start); } } } else if (in_comment) { if (*src == '(') in_comment++; else if (*src == ')') { if (!--in_comment && !*comment) { *comment = tail; tail = set_user_id_part (tail, start, src - start); } } } else if (*src == '<') { if (in_name) { if (!*name) { *name = tail; tail = set_user_id_part (tail, start, src - start); } in_name = 0; } in_email = 1; start = src + 1; } else if (*src == '(') { if (in_name) { if (!*name) { *name = tail; tail = set_user_id_part (tail, start, src - start); } in_name = 0; } in_comment = 1; start = src + 1; } else if (!in_name && *src != ' ' && *src != '\t') { in_name = 1; start = src; } src++; } if (in_name) { if (!*name) { *name = tail; tail = set_user_id_part (tail, start, src - start); } } /* Let unused parts point to an EOS. */ tail--; if (!*name) *name = tail; if (!*email) *email = tail; if (!*comment) *comment = tail; } static void parse_x509_user_id (char *src, char **name, char **email, char **comment, char *tail) { if (*src == '<' && src[strlen (src) - 1] == '>') *email = src; /* Let unused parts point to an EOS. */ tail--; if (!*name) *name = tail; if (!*email) *email = tail; if (!*comment) *comment = tail; } /* Take a name from the --with-colon listing, remove certain escape sequences sequences and put it into the list of UIDs. */ gpgme_error_t _gpgme_key_append_name (gpgme_key_t key, const char *src, int convert) { gpgme_user_id_t uid; char *dst; int src_len = strlen (src); assert (key); /* We can malloc a buffer of the same length, because the converted string will never be larger. Actually we allocate it twice the size, so that we are able to store the parsed stuff there too. */ uid = malloc (sizeof (*uid) + 2 * src_len + 3); if (!uid) return gpg_error_from_syserror (); memset (uid, 0, sizeof *uid); uid->uid = ((char *) uid) + sizeof (*uid); dst = uid->uid; if (convert) _gpgme_decode_c_string (src, &dst, src_len + 1); else memcpy (dst, src, src_len + 1); dst += strlen (dst) + 1; if (key->protocol == GPGME_PROTOCOL_CMS) parse_x509_user_id (uid->uid, &uid->name, &uid->email, &uid->comment, dst); else parse_user_id (uid->uid, &uid->name, &uid->email, &uid->comment, dst); uid->address = _gpgme_mailbox_from_userid (uid->uid); if ((!uid->email || !*uid->email) && uid->address && uid->name && !strcmp (uid->name, uid->address)) { /* Name and address are the same. This is a mailbox only key. Use address as email and remove name. */ *uid->name = '\0'; uid->email = uid->address; } if (!key->uids) key->uids = uid; if (key->_last_uid) key->_last_uid->next = uid; key->_last_uid = uid; return 0; } gpgme_key_sig_t _gpgme_key_add_sig (gpgme_key_t key, char *src) { int src_len = src ? strlen (src) : 0; gpgme_user_id_t uid; gpgme_key_sig_t sig; assert (key); /* XXX */ uid = key->_last_uid; assert (uid); /* XXX */ /* We can malloc a buffer of the same length, because the converted string will never be larger. Actually we allocate it twice the size, so that we are able to store the parsed stuff there too. */ sig = malloc (sizeof (*sig) + 2 * src_len + 3); if (!sig) return NULL; memset (sig, 0, sizeof *sig); sig->keyid = sig->_keyid; sig->_keyid[16] = '\0'; sig->uid = ((char *) sig) + sizeof (*sig); if (src) { char *dst = sig->uid; _gpgme_decode_c_string (src, &dst, src_len + 1); dst += strlen (dst) + 1; if (key->protocol == GPGME_PROTOCOL_CMS) parse_x509_user_id (sig->uid, &sig->name, &sig->email, &sig->comment, dst); else parse_user_id (sig->uid, &sig->name, &sig->email, &sig->comment, dst); } else sig->uid[0] = '\0'; if (!uid->signatures) uid->signatures = sig; if (uid->_last_keysig) uid->_last_keysig->next = sig; uid->_last_keysig = sig; return sig; } /* Acquire a reference to KEY. */ void gpgme_key_ref (gpgme_key_t key) { LOCK (key_ref_lock); key->_refs++; UNLOCK (key_ref_lock); } /* gpgme_key_unref releases the key object. Note, that this function may not do an actual release if there are other shallow copies of the objects. You have to call this function for every newly created key object as well as for every gpgme_key_ref() done on the key object. */ void gpgme_key_unref (gpgme_key_t key) { gpgme_user_id_t uid; gpgme_subkey_t subkey; if (!key) return; LOCK (key_ref_lock); assert (key->_refs > 0); if (--key->_refs) { UNLOCK (key_ref_lock); return; } UNLOCK (key_ref_lock); subkey = key->subkeys; while (subkey) { gpgme_subkey_t next = subkey->next; free (subkey->fpr); free (subkey->curve); free (subkey->keygrip); free (subkey->card_number); free (subkey); subkey = next; } uid = key->uids; while (uid) { gpgme_user_id_t next_uid = uid->next; gpgme_key_sig_t keysig = uid->signatures; gpgme_tofu_info_t tofu = uid->tofu; while (keysig) { gpgme_key_sig_t next_keysig = keysig->next; gpgme_sig_notation_t notation = keysig->notations; while (notation) { gpgme_sig_notation_t next_notation = notation->next; _gpgme_sig_notation_free (notation); notation = next_notation; } free (keysig); keysig = next_keysig; } while (tofu) { /* NB: The ->next is currently not used but we are prepared * for it. */ gpgme_tofu_info_t tofu_next = tofu->next; free (tofu->description); free (tofu); tofu = tofu_next; } free (uid->address); free (uid); uid = next_uid; } free (key->issuer_serial); free (key->issuer_name); free (key->chain_id); free (key->fpr); free (key); } /* Support functions. */ /* Create a dummy key to specify an email address. */ gpgme_error_t gpgme_key_from_uid (gpgme_key_t *r_key, const char *name) { gpgme_error_t err; gpgme_key_t key; *r_key = NULL; err = _gpgme_key_new (&key); if (err) return err; /* Note: protocol doesn't matter if only email is provided. */ err = _gpgme_key_append_name (key, name, 0); if (err) gpgme_key_unref (key); else *r_key = key; return err; } /* Compatibility interfaces. */ void gpgme_key_release (gpgme_key_t key) { gpgme_key_unref (key); } static const char * otrust_to_string (int otrust) { switch (otrust) { case GPGME_VALIDITY_NEVER: return "n"; case GPGME_VALIDITY_MARGINAL: return "m"; case GPGME_VALIDITY_FULL: return "f"; case GPGME_VALIDITY_ULTIMATE: return "u"; default: return "?"; } } static const char * validity_to_string (int validity) { switch (validity) { case GPGME_VALIDITY_UNDEFINED: return "q"; case GPGME_VALIDITY_NEVER: return "n"; case GPGME_VALIDITY_MARGINAL: return "m"; case GPGME_VALIDITY_FULL: return "f"; case GPGME_VALIDITY_ULTIMATE: return "u"; case GPGME_VALIDITY_UNKNOWN: default: return "?"; } } static const char * capabilities_to_string (gpgme_subkey_t subkey) { static const char *const strings[8] = { "", "c", "s", "sc", "e", "ec", "es", "esc" }; return strings[(!!subkey->can_encrypt << 2) | (!!subkey->can_sign << 1) | (!!subkey->can_certify)]; } /* Return the value of the attribute WHAT of ITEM, which has to be representable by a string. */ const char * gpgme_key_get_string_attr (gpgme_key_t key, _gpgme_attr_t what, const void *reserved, int idx) { gpgme_subkey_t subkey; gpgme_user_id_t uid; int i; if (!key || reserved || idx < 0) return NULL; /* Select IDXth subkey. */ subkey = key->subkeys; for (i = 0; i < idx; i++) { subkey = subkey->next; if (!subkey) break; } /* Select the IDXth user ID. */ uid = key->uids; for (i = 0; i < idx; i++) { uid = uid->next; if (!uid) break; } switch (what) { case GPGME_ATTR_KEYID: return subkey ? subkey->keyid : NULL; case GPGME_ATTR_FPR: return subkey ? subkey->fpr : NULL; case GPGME_ATTR_ALGO: return subkey ? gpgme_pubkey_algo_name (subkey->pubkey_algo) : NULL; case GPGME_ATTR_TYPE: return key->protocol == GPGME_PROTOCOL_CMS ? "X.509" : "PGP"; case GPGME_ATTR_OTRUST: return otrust_to_string (key->owner_trust); case GPGME_ATTR_USERID: return uid ? uid->uid : NULL; case GPGME_ATTR_NAME: return uid ? uid->name : NULL; case GPGME_ATTR_EMAIL: return uid ? uid->email : NULL; case GPGME_ATTR_COMMENT: return uid ? uid->comment : NULL; case GPGME_ATTR_VALIDITY: return uid ? validity_to_string (uid->validity) : NULL; case GPGME_ATTR_KEY_CAPS: return subkey ? capabilities_to_string (subkey) : NULL; case GPGME_ATTR_SERIAL: return key->issuer_serial; case GPGME_ATTR_ISSUER: return idx ? NULL : key->issuer_name; case GPGME_ATTR_CHAINID: return key->chain_id; default: return NULL; } } unsigned long gpgme_key_get_ulong_attr (gpgme_key_t key, _gpgme_attr_t what, const void *reserved, int idx) { gpgme_subkey_t subkey; gpgme_user_id_t uid; int i; if (!key || reserved || idx < 0) return 0; /* Select IDXth subkey. */ subkey = key->subkeys; for (i = 0; i < idx; i++) { subkey = subkey->next; if (!subkey) break; } /* Select the IDXth user ID. */ uid = key->uids; for (i = 0; i < idx; i++) { uid = uid->next; if (!uid) break; } switch (what) { case GPGME_ATTR_ALGO: return subkey ? (unsigned long) subkey->pubkey_algo : 0; case GPGME_ATTR_LEN: return subkey ? (unsigned long) subkey->length : 0; case GPGME_ATTR_TYPE: return key->protocol == GPGME_PROTOCOL_CMS ? 1 : 0; case GPGME_ATTR_CREATED: return (subkey && subkey->timestamp >= 0) ? (unsigned long) subkey->timestamp : 0; case GPGME_ATTR_EXPIRE: return (subkey && subkey->expires >= 0) ? (unsigned long) subkey->expires : 0; case GPGME_ATTR_VALIDITY: return uid ? uid->validity : 0; case GPGME_ATTR_OTRUST: return key->owner_trust; case GPGME_ATTR_IS_SECRET: return !!key->secret; case GPGME_ATTR_KEY_REVOKED: return subkey ? subkey->revoked : 0; case GPGME_ATTR_KEY_INVALID: return subkey ? subkey->invalid : 0; case GPGME_ATTR_KEY_EXPIRED: return subkey ? subkey->expired : 0; case GPGME_ATTR_KEY_DISABLED: return subkey ? subkey->disabled : 0; case GPGME_ATTR_UID_REVOKED: return uid ? uid->revoked : 0; case GPGME_ATTR_UID_INVALID: return uid ? uid->invalid : 0; case GPGME_ATTR_CAN_ENCRYPT: return key->can_encrypt; case GPGME_ATTR_CAN_SIGN: return key->can_sign; case GPGME_ATTR_CAN_CERTIFY: return key->can_certify; default: return 0; } } static gpgme_key_sig_t get_keysig (gpgme_key_t key, int uid_idx, int idx) { gpgme_user_id_t uid; gpgme_key_sig_t sig; if (!key || uid_idx < 0 || idx < 0) return NULL; uid = key->uids; while (uid && uid_idx > 0) { uid = uid->next; uid_idx--; } if (!uid) return NULL; sig = uid->signatures; while (sig && idx > 0) { sig = sig->next; idx--; } return sig; } const char * gpgme_key_sig_get_string_attr (gpgme_key_t key, int uid_idx, _gpgme_attr_t what, const void *reserved, int idx) { gpgme_key_sig_t certsig = get_keysig (key, uid_idx, idx); if (!certsig || reserved) return NULL; switch (what) { case GPGME_ATTR_KEYID: return certsig->keyid; case GPGME_ATTR_ALGO: return gpgme_pubkey_algo_name (certsig->pubkey_algo); case GPGME_ATTR_USERID: return certsig->uid; case GPGME_ATTR_NAME: return certsig->name; case GPGME_ATTR_EMAIL: return certsig->email; case GPGME_ATTR_COMMENT: return certsig->comment; default: return NULL; } } unsigned long gpgme_key_sig_get_ulong_attr (gpgme_key_t key, int uid_idx, _gpgme_attr_t what, const void *reserved, int idx) { gpgme_key_sig_t certsig = get_keysig (key, uid_idx, idx); if (!certsig || reserved) return 0; switch (what) { case GPGME_ATTR_ALGO: return (unsigned long) certsig->pubkey_algo; case GPGME_ATTR_CREATED: return certsig->timestamp < 0 ? 0L : (unsigned long) certsig->timestamp; case GPGME_ATTR_EXPIRE: return certsig->expires < 0 ? 0L : (unsigned long) certsig->expires; case GPGME_ATTR_KEY_REVOKED: return certsig->revoked; case GPGME_ATTR_KEY_INVALID: return certsig->invalid; case GPGME_ATTR_KEY_EXPIRED: return certsig->expired; case GPGME_ATTR_SIG_CLASS: return certsig->sig_class; case GPGME_ATTR_SIG_STATUS: return certsig->status; default: return 0; } } diff --git a/src/keylist.c b/src/keylist.c index 6fe256ce..a0fa841b 100644 --- a/src/keylist.c +++ b/src/keylist.c @@ -1,1342 +1,1343 @@ /* keylist.c - Listing keys. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2006, 2007, - 2008, 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2006, 2007, + * 2008, 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #include #ifdef HAVE_SYS_TYPES_H /* Solaris 8 needs sys/types.h before time.h. */ # include #endif #include #include #include #include #include /* Suppress warning for accessing deprecated member "class". */ #define _GPGME_IN_GPGME #include "gpgme.h" #include "util.h" #include "context.h" #include "ops.h" #include "debug.h" struct key_queue_item_s { struct key_queue_item_s *next; gpgme_key_t key; }; typedef struct { struct _gpgme_op_keylist_result result; /* The error code from ERROR keydb_search. */ gpgme_error_t keydb_search_err; gpgme_key_t tmp_key; /* This points to the last uid in tmp_key. */ gpgme_user_id_t tmp_uid; /* This points to the last sig in tmp_uid. */ gpgme_key_sig_t tmp_keysig; /* Something new is available. */ int key_cond; struct key_queue_item_s *key_queue; } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; struct key_queue_item_s *key = opd->key_queue; if (opd->tmp_key) gpgme_key_unref (opd->tmp_key); /* opd->tmp_uid and opd->tmp_keysig are actually part of opd->tmp_key, so we do not need to release them here. */ while (key) { struct key_queue_item_s *next = key->next; gpgme_key_unref (key->key); key = next; } } gpgme_keylist_result_t gpgme_op_keylist_result (gpgme_ctx_t ctx) { void *hook; op_data_t opd; gpgme_error_t err; TRACE_BEG (DEBUG_CTX, "gpgme_op_keylist_result", ctx); err = _gpgme_op_data_lookup (ctx, OPDATA_KEYLIST, &hook, -1, NULL); opd = hook; if (err || !opd) { TRACE_SUC0 ("result=(null)"); return NULL; } TRACE_LOG1 ("truncated = %i", opd->result.truncated); TRACE_SUC1 ("result=%p", &opd->result); return &opd->result; } static gpgme_error_t keylist_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; (void)args; err = _gpgme_op_data_lookup (ctx, OPDATA_KEYLIST, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_TRUNCATED: opd->result.truncated = 1; break; case GPGME_STATUS_ERROR: err = _gpgme_parse_failure (args); if (!opd->keydb_search_err && !strcmp (args, "keydb_search")) opd->keydb_search_err = err; err = 0; break; default: break; } return err; } static void set_subkey_trust_info (gpgme_subkey_t subkey, const char *src) { while (*src && !isdigit (*src)) { switch (*src) { case 'e': subkey->expired = 1; break; case 'r': subkey->revoked = 1; break; case 'd': /* Note that gpg 1.3 won't print that anymore but only uses the capabilities field. */ subkey->disabled = 1; break; case 'i': subkey->invalid = 1; break; } src++; } } static void set_mainkey_trust_info (gpgme_key_t key, const char *src) { /* First set the trust info of the main key (the first subkey). */ set_subkey_trust_info (key->subkeys, src); /* Now set the summarized trust info. */ while (*src && !isdigit (*src)) { switch (*src) { case 'e': key->expired = 1; break; case 'r': key->revoked = 1; break; case 'd': /* Note that gpg 1.3 won't print that anymore but only uses the capabilities field. However, it is still used for external key listings. */ key->disabled = 1; break; case 'i': key->invalid = 1; break; } src++; } } static void set_userid_flags (gpgme_key_t key, const char *src) { gpgme_user_id_t uid = key->_last_uid; assert (uid); /* Look at letters and stop at the first digit. */ while (*src && !isdigit (*src)) { switch (*src) { case 'r': uid->revoked = 1; break; case 'i': uid->invalid = 1; break; case 'n': uid->validity = GPGME_VALIDITY_NEVER; break; case 'm': uid->validity = GPGME_VALIDITY_MARGINAL; break; case 'f': uid->validity = GPGME_VALIDITY_FULL; break; case 'u': uid->validity = GPGME_VALIDITY_ULTIMATE; break; } src++; } } static void set_subkey_capability (gpgme_subkey_t subkey, const char *src) { while (*src) { switch (*src) { case 'e': subkey->can_encrypt = 1; break; case 's': subkey->can_sign = 1; break; case 'c': subkey->can_certify = 1; break; case 'a': subkey->can_authenticate = 1; break; case 'q': subkey->is_qualified = 1; break; case 'd': subkey->disabled = 1; break; } src++; } } static void set_mainkey_capability (gpgme_key_t key, const char *src) { /* First set the capabilities of the main key (the first subkey). */ set_subkey_capability (key->subkeys, src); while (*src) { switch (*src) { case 'd': case 'D': /* Note, that this flag is also set using the key validity field for backward compatibility with gpg 1.2. We use d and D, so that a future gpg version will be able to disable certain subkeys. Currently it is expected that gpg sets this for the primary key. */ key->disabled = 1; break; case 'e': case 'E': key->can_encrypt = 1; break; case 's': case 'S': key->can_sign = 1; break; case 'c': case 'C': key->can_certify = 1; break; case 'a': case 'A': key->can_authenticate = 1; break; case 'q': case 'Q': key->is_qualified = 1; break; } src++; } } static void set_ownertrust (gpgme_key_t key, const char *src) { /* Look at letters and stop at the first digit. */ while (*src && !isdigit (*src)) { switch (*src) { case 'n': key->owner_trust = GPGME_VALIDITY_NEVER; break; case 'm': key->owner_trust = GPGME_VALIDITY_MARGINAL; break; case 'f': key->owner_trust = GPGME_VALIDITY_FULL; break; case 'u': key->owner_trust = GPGME_VALIDITY_ULTIMATE; break; default: key->owner_trust = GPGME_VALIDITY_UNKNOWN; break; } src++; } } static gpgme_keyorg_t parse_keyorg (const char *string) { switch (atoi (string)) { case 0: return GPGME_KEYORG_UNKNOWN; case 1: case 2: return GPGME_KEYORG_KS; case 3: return GPGME_KEYORG_DANE; case 4: return GPGME_KEYORG_WKD; case 5: return GPGME_KEYORG_URL; case 6: return GPGME_KEYORG_FILE; case 7: return GPGME_KEYORG_SELF; default: return GPGME_KEYORG_OTHER; } } /* Parse field 15 of a secret key or subkey. This fields holds a reference to smartcards. FIELD is the content of the field and we are allowed to modify it. */ static gpg_error_t parse_sec_field15 (gpgme_key_t key, gpgme_subkey_t subkey, char *field) { if (!*field) ; /* Empty. */ else if (*field == '#') { /* This is a stub for an offline key. We reset the SECRET flag of the subkey here. Note that the secret flag of the entire key will be true even then. We even explicitly set key->secret to make it works for GPGME_KEYLIST_MODE_WITH_SECRET. */ subkey->secret = 0; key->secret = 1; } else if (strchr ("01234567890ABCDEFabcdef", *field)) { /* Fields starts with a hex digit; thus it is a serial number. */ key->secret = 1; subkey->is_cardkey = 1; subkey->card_number = strdup (field); if (!subkey->card_number) return gpg_error_from_syserror (); } else if (*field == '+') { key->secret = 1; subkey->secret = 1; } else { /* RFU. */ } return 0; } /* Parse a tfs record. */ static gpg_error_t parse_tfs_record (gpgme_user_id_t uid, char **field, int nfield) { gpg_error_t err; gpgme_tofu_info_t ti; unsigned long uval; /* We add only the first TOFU record in case future versions emit * several. */ if (uid->tofu) return 0; /* Check that we have enough fields and that the version is supported. */ if (nfield < 8 || atoi(field[1]) != 1) return trace_gpg_error (GPG_ERR_INV_ENGINE); ti = calloc (1, sizeof *ti); if (!ti) return gpg_error_from_syserror (); /* Note that we allow a value of up to 7 which is what we can store * in the ti->validity. */ err = _gpgme_strtoul_field (field[2], &uval); if (err || uval > 7) goto inv_engine; ti->validity = uval; /* Parse the sign-count. */ err = _gpgme_strtoul_field (field[3], &uval); if (err) goto inv_engine; if (uval > USHRT_MAX) uval = USHRT_MAX; ti->signcount = uval; /* Parse the encr-count. */ err = _gpgme_strtoul_field (field[4], &uval); if (err) goto inv_engine; if (uval > USHRT_MAX) uval = USHRT_MAX; ti->encrcount = uval; /* Parse the policy. */ if (!strcmp (field[5], "none")) ti->policy = GPGME_TOFU_POLICY_NONE; else if (!strcmp (field[5], "auto")) ti->policy = GPGME_TOFU_POLICY_AUTO; else if (!strcmp (field[5], "good")) ti->policy = GPGME_TOFU_POLICY_GOOD; else if (!strcmp (field[5], "bad")) ti->policy = GPGME_TOFU_POLICY_BAD; else if (!strcmp (field[5], "ask")) ti->policy = GPGME_TOFU_POLICY_ASK; else /* "unknown" and invalid policy strings. */ ti->policy = GPGME_TOFU_POLICY_UNKNOWN; /* Parse first and last seen timestamps. */ err = _gpgme_strtoul_field (field[6], &uval); if (err) goto inv_engine; ti->signfirst = uval; err = _gpgme_strtoul_field (field[7], &uval); if (err) goto inv_engine; ti->signlast = uval; if (nfield > 9) { /* This condition is only to allow for gpg 2.1.15 - can * eventually be removed. */ err = _gpgme_strtoul_field (field[8], &uval); if (err) goto inv_engine; ti->encrfirst = uval; err = _gpgme_strtoul_field (field[9], &uval); if (err) goto inv_engine; ti->encrlast = uval; } /* Ready. */ uid->tofu = ti; return 0; inv_engine: free (ti); return trace_gpg_error (GPG_ERR_INV_ENGINE); } /* We have read an entire key into tmp_key and should now finish it. It is assumed that this releases tmp_key. */ static void finish_key (gpgme_ctx_t ctx, op_data_t opd) { gpgme_key_t key = opd->tmp_key; opd->tmp_key = NULL; opd->tmp_uid = NULL; opd->tmp_keysig = NULL; if (key) _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_NEXT_KEY, key); } /* Note: We are allowed to modify LINE. */ static gpgme_error_t keylist_colon_handler (void *priv, char *line) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; enum { RT_NONE, RT_SIG, RT_UID, RT_TFS, RT_SUB, RT_PUB, RT_FPR, RT_GRP, RT_SSB, RT_SEC, RT_CRT, RT_CRS, RT_REV, RT_SPK } rectype = RT_NONE; #define NR_FIELDS 20 char *field[NR_FIELDS]; int fields = 0; void *hook; op_data_t opd; gpgme_error_t err; gpgme_key_t key; gpgme_subkey_t subkey = NULL; gpgme_key_sig_t keysig = NULL; err = _gpgme_op_data_lookup (ctx, OPDATA_KEYLIST, &hook, -1, NULL); opd = hook; if (err) return err; key = opd->tmp_key; TRACE2 (DEBUG_CTX, "gpgme:keylist_colon_handler", ctx, "key = %p, line = %s", key, line ? line : "(null)"); if (!line) { /* End Of File. */ finish_key (ctx, opd); return 0; } while (line && fields < NR_FIELDS) { field[fields++] = line; line = strchr (line, ':'); if (line) *(line++) = '\0'; } if (!strcmp (field[0], "sig")) rectype = RT_SIG; else if (!strcmp (field[0], "rev")) rectype = RT_REV; else if (!strcmp (field[0], "pub")) rectype = RT_PUB; else if (!strcmp (field[0], "sec")) rectype = RT_SEC; else if (!strcmp (field[0], "crt")) rectype = RT_CRT; else if (!strcmp (field[0], "crs")) rectype = RT_CRS; else if (!strcmp (field[0], "fpr") && key) rectype = RT_FPR; else if (!strcmp (field[0], "grp") && key) rectype = RT_GRP; else if (!strcmp (field[0], "uid") && key) rectype = RT_UID; else if (!strcmp (field[0], "tfs") && key) rectype = RT_TFS; else if (!strcmp (field[0], "sub") && key) rectype = RT_SUB; else if (!strcmp (field[0], "ssb") && key) rectype = RT_SSB; else if (!strcmp (field[0], "spk") && key) rectype = RT_SPK; else rectype = RT_NONE; /* Only look at signature and trust info records immediately following a user ID. For this, clear the user ID pointer when encountering anything but a signature or trust record. */ if (rectype != RT_SIG && rectype != RT_REV && rectype != RT_TFS) opd->tmp_uid = NULL; /* Only look at subpackets immediately following a signature. For this, clear the signature pointer when encountering anything but a subpacket. */ if (rectype != RT_SPK) opd->tmp_keysig = NULL; switch (rectype) { case RT_PUB: case RT_SEC: case RT_CRT: case RT_CRS: /* Start a new keyblock. */ err = _gpgme_key_new (&key); if (err) return err; key->keylist_mode = ctx->keylist_mode; err = _gpgme_key_add_subkey (key, &subkey); if (err) { gpgme_key_unref (key); return err; } if (rectype == RT_SEC || rectype == RT_CRS) key->secret = subkey->secret = 1; if (rectype == RT_CRT || rectype == RT_CRS) key->protocol = GPGME_PROTOCOL_CMS; finish_key (ctx, opd); opd->tmp_key = key; /* Field 2 has the trust info. */ if (fields >= 2) set_mainkey_trust_info (key, field[1]); /* Field 3 has the key length. */ if (fields >= 3) { int i = atoi (field[2]); /* Ignore invalid values. */ if (i > 1) subkey->length = i; } /* Field 4 has the public key algorithm. */ if (fields >= 4) { int i = atoi (field[3]); if (i >= 1 && i < 128) subkey->pubkey_algo = _gpgme_map_pk_algo (i, ctx->protocol); } /* Field 5 has the long keyid. Allow short key IDs for the output of an external keyserver listing. */ if (fields >= 5 && strlen (field[4]) <= DIM(subkey->_keyid) - 1) strcpy (subkey->_keyid, field[4]); /* Field 6 has the timestamp (seconds). */ if (fields >= 6) subkey->timestamp = _gpgme_parse_timestamp (field[5], NULL); /* Field 7 has the expiration time (seconds). */ if (fields >= 7) subkey->expires = _gpgme_parse_timestamp (field[6], NULL); /* Field 8 has the X.509 serial number. */ if (fields >= 8 && (rectype == RT_CRT || rectype == RT_CRS)) { key->issuer_serial = strdup (field[7]); if (!key->issuer_serial) return gpg_error_from_syserror (); } /* Field 9 has the ownertrust. */ if (fields >= 9) set_ownertrust (key, field[8]); /* Field 10 is not used for gpg due to --fixed-list-mode option but GPGSM stores the issuer name. */ if (fields >= 10 && (rectype == RT_CRT || rectype == RT_CRS)) if (_gpgme_decode_c_string (field[9], &key->issuer_name, 0)) return gpg_error (GPG_ERR_ENOMEM); /* FIXME */ /* Field 11 has the signature class. */ /* Field 12 has the capabilities. */ if (fields >= 12) set_mainkey_capability (key, field[11]); /* Field 15 carries special flags of a secret key. */ if (fields >= 15 && (key->secret || (ctx->keylist_mode & GPGME_KEYLIST_MODE_WITH_SECRET))) { err = parse_sec_field15 (key, subkey, field[14]); if (err) return err; } /* Field 17 has the curve name for ECC. */ if (fields >= 17 && *field[16]) { subkey->curve = strdup (field[16]); if (!subkey->curve) return gpg_error_from_syserror (); } /* Field 18 has the compliance flags. */ if (fields >= 17 && *field[17]) PARSE_COMPLIANCE_FLAGS (field[17], subkey); if (fields >= 20) { key->last_update = _gpgme_parse_timestamp_ul (field[18]); key->origin = parse_keyorg (field[19]); } break; case RT_SUB: case RT_SSB: /* Start a new subkey. */ err = _gpgme_key_add_subkey (key, &subkey); if (err) return err; if (rectype == RT_SSB) subkey->secret = 1; /* Field 2 has the trust info. */ if (fields >= 2) set_subkey_trust_info (subkey, field[1]); /* Field 3 has the key length. */ if (fields >= 3) { int i = atoi (field[2]); /* Ignore invalid values. */ if (i > 1) subkey->length = i; } /* Field 4 has the public key algorithm. */ if (fields >= 4) { int i = atoi (field[3]); if (i >= 1 && i < 128) subkey->pubkey_algo = _gpgme_map_pk_algo (i, ctx->protocol); } /* Field 5 has the long keyid. */ if (fields >= 5 && strlen (field[4]) == DIM(subkey->_keyid) - 1) strcpy (subkey->_keyid, field[4]); /* Field 6 has the timestamp (seconds). */ if (fields >= 6) subkey->timestamp = _gpgme_parse_timestamp (field[5], NULL); /* Field 7 has the expiration time (seconds). */ if (fields >= 7) subkey->expires = _gpgme_parse_timestamp (field[6], NULL); /* Field 8 is reserved (LID). */ /* Field 9 has the ownertrust. */ /* Field 10, the user ID, is n/a for a subkey. */ /* Field 11 has the signature class. */ /* Field 12 has the capabilities. */ if (fields >= 12) set_subkey_capability (subkey, field[11]); /* Field 15 carries special flags of a secret key. */ if (fields >= 15 && (key->secret || (ctx->keylist_mode & GPGME_KEYLIST_MODE_WITH_SECRET))) { err = parse_sec_field15 (key, subkey, field[14]); if (err) return err; } /* Field 17 has the curve name for ECC. */ if (fields >= 17 && *field[16]) { subkey->curve = strdup (field[16]); if (!subkey->curve) return gpg_error_from_syserror (); } /* Field 18 has the compliance flags. */ if (fields >= 17 && *field[17]) PARSE_COMPLIANCE_FLAGS (field[17], subkey); break; case RT_UID: /* Field 2 has the trust info, and field 10 has the user ID. */ if (fields >= 10) { if (_gpgme_key_append_name (key, field[9], 1)) return gpg_error (GPG_ERR_ENOMEM); /* FIXME */ if (field[1]) set_userid_flags (key, field[1]); opd->tmp_uid = key->_last_uid; if (fields >= 20) { opd->tmp_uid->last_update = _gpgme_parse_timestamp_ul (field[18]); opd->tmp_uid->origin = parse_keyorg (field[19]); } } break; case RT_TFS: if (opd->tmp_uid) { err = parse_tfs_record (opd->tmp_uid, field, fields); if (err) return err; } break; case RT_FPR: /* Field 10 has the fingerprint (take only the first one). */ if (fields >= 10 && field[9] && *field[9]) { /* Need to apply it to the last subkey because all subkeys do have fingerprints. */ subkey = key->_last_subkey; if (!subkey->fpr) { subkey->fpr = strdup (field[9]); if (!subkey->fpr) return gpg_error_from_syserror (); } /* If this is the first subkey, store the fingerprint also in the KEY object. */ if (subkey == key->subkeys) { if (key->fpr && strcmp (key->fpr, subkey->fpr)) { /* FPR already set but mismatch: Should never happen. */ return trace_gpg_error (GPG_ERR_INTERNAL); } if (!key->fpr) { key->fpr = strdup (subkey->fpr); if (!key->fpr) return gpg_error_from_syserror (); } } } /* Field 13 has the gpgsm chain ID (take only the first one). */ if (fields >= 13 && !key->chain_id && *field[12]) { key->chain_id = strdup (field[12]); if (!key->chain_id) return gpg_error_from_syserror (); } break; case RT_GRP: /* Field 10 has the keygrip. */ if (fields >= 10 && field[9] && *field[9]) { /* Need to apply it to the last subkey because all subkeys have a keygrip. */ subkey = key->_last_subkey; if (!subkey->keygrip) { subkey->keygrip = strdup (field[9]); if (!subkey->keygrip) return gpg_error_from_syserror (); } } break; case RT_SIG: case RT_REV: if (!opd->tmp_uid) return 0; /* Start a new (revoked) signature. */ assert (opd->tmp_uid == key->_last_uid); keysig = _gpgme_key_add_sig (key, (fields >= 10) ? field[9] : NULL); if (!keysig) return gpg_error (GPG_ERR_ENOMEM); /* FIXME */ /* Field 2 has the calculated trust ('!', '-', '?', '%'). */ if (fields >= 2) switch (field[1][0]) { case '!': keysig->status = gpg_error (GPG_ERR_NO_ERROR); break; case '-': keysig->status = gpg_error (GPG_ERR_BAD_SIGNATURE); break; case '?': keysig->status = gpg_error (GPG_ERR_NO_PUBKEY); break; case '%': keysig->status = gpg_error (GPG_ERR_GENERAL); break; default: keysig->status = gpg_error (GPG_ERR_NO_ERROR); break; } /* Field 4 has the public key algorithm. */ if (fields >= 4) { int i = atoi (field[3]); if (i >= 1 && i < 128) keysig->pubkey_algo = _gpgme_map_pk_algo (i, ctx->protocol); } /* Field 5 has the long keyid. */ if (fields >= 5 && strlen (field[4]) == DIM(keysig->_keyid) - 1) strcpy (keysig->_keyid, field[4]); /* Field 6 has the timestamp (seconds). */ if (fields >= 6) keysig->timestamp = _gpgme_parse_timestamp (field[5], NULL); /* Field 7 has the expiration time (seconds). */ if (fields >= 7) keysig->expires = _gpgme_parse_timestamp (field[6], NULL); /* Field 11 has the signature class (eg, 0x30 means revoked). */ if (fields >= 11) if (field[10][0] && field[10][1]) { int sig_class = _gpgme_hextobyte (field[10]); if (sig_class >= 0) { keysig->sig_class = sig_class; keysig->class = keysig->sig_class; if (sig_class == 0x30) keysig->revoked = 1; } if (field[10][2] == 'x') keysig->exportable = 1; } opd->tmp_keysig = keysig; break; case RT_SPK: if (!opd->tmp_keysig) return 0; assert (opd->tmp_keysig == key->_last_uid->_last_keysig); if (fields >= 4) { /* Field 2 has the subpacket type. */ int type = atoi (field[1]); /* Field 3 has the flags. */ int flags = atoi (field[2]); /* Field 4 has the length. */ int len = atoi (field[3]); /* Field 5 has the data. */ char *data = field[4]; /* Type 20: Notation data. */ /* Type 26: Policy URL. */ if (type == 20 || type == 26) { gpgme_sig_notation_t notation; keysig = opd->tmp_keysig; /* At this time, any error is serious. */ err = _gpgme_parse_notation (¬ation, type, flags, len, data); if (err) return err; /* Add a new notation. FIXME: Could be factored out. */ if (!keysig->notations) keysig->notations = notation; if (keysig->_last_notation) keysig->_last_notation->next = notation; keysig->_last_notation = notation; } } case RT_NONE: /* Unknown record. */ break; } return 0; } void _gpgme_op_keylist_event_cb (void *data, gpgme_event_io_t type, void *type_data) { gpgme_error_t err; gpgme_ctx_t ctx = (gpgme_ctx_t) data; gpgme_key_t key = (gpgme_key_t) type_data; void *hook; op_data_t opd; struct key_queue_item_s *q, *q2; assert (type == GPGME_EVENT_NEXT_KEY); err = _gpgme_op_data_lookup (ctx, OPDATA_KEYLIST, &hook, -1, NULL); opd = hook; if (err) return; q = malloc (sizeof *q); if (!q) { gpgme_key_unref (key); /* FIXME return GPGME_Out_Of_Core; */ return; } q->key = key; q->next = NULL; /* FIXME: Use a tail pointer? */ if (!(q2 = opd->key_queue)) opd->key_queue = q; else { for (; q2->next; q2 = q2->next) ; q2->next = q; } opd->key_cond = 1; } /* Start a keylist operation within CTX, searching for keys which match PATTERN. If SECRET_ONLY is true, only secret keys are returned. */ gpgme_error_t gpgme_op_keylist_start (gpgme_ctx_t ctx, const char *pattern, int secret_only) { gpgme_error_t err; void *hook; op_data_t opd; int flags = 0; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_keylist_start", ctx, "pattern=%s, secret_only=%i", pattern, secret_only); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_op_reset (ctx, 2); if (err) return TRACE_ERR (err); err = _gpgme_op_data_lookup (ctx, OPDATA_KEYLIST, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return TRACE_ERR (err); _gpgme_engine_set_status_handler (ctx->engine, keylist_status_handler, ctx); err = _gpgme_engine_set_colon_line_handler (ctx->engine, keylist_colon_handler, ctx); if (err) return TRACE_ERR (err); if (ctx->offline) flags |= GPGME_ENGINE_FLAG_OFFLINE; err = _gpgme_engine_op_keylist (ctx->engine, pattern, secret_only, ctx->keylist_mode, flags); return TRACE_ERR (err); } /* Start a keylist operation within CTX, searching for keys which match PATTERN. If SECRET_ONLY is true, only secret keys are returned. */ gpgme_error_t gpgme_op_keylist_ext_start (gpgme_ctx_t ctx, const char *pattern[], int secret_only, int reserved) { gpgme_error_t err; void *hook; op_data_t opd; int flags = 0; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_keylist_ext_start", ctx, "secret_only=%i, reserved=0x%x", secret_only, reserved); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_op_reset (ctx, 2); if (err) return TRACE_ERR (err); err = _gpgme_op_data_lookup (ctx, OPDATA_KEYLIST, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return TRACE_ERR (err); _gpgme_engine_set_status_handler (ctx->engine, keylist_status_handler, ctx); err = _gpgme_engine_set_colon_line_handler (ctx->engine, keylist_colon_handler, ctx); if (err) return TRACE_ERR (err); if (ctx->offline) flags |= GPGME_ENGINE_FLAG_OFFLINE; err = _gpgme_engine_op_keylist_ext (ctx->engine, pattern, secret_only, reserved, ctx->keylist_mode, flags); return TRACE_ERR (err); } /* Start a keylist operation within CTX to show keys contained * in DATA. */ gpgme_error_t gpgme_op_keylist_from_data_start (gpgme_ctx_t ctx, gpgme_data_t data, int reserved) { gpgme_error_t err; void *hook; op_data_t opd; TRACE_BEG (DEBUG_CTX, "gpgme_op_keylist_from_data_start", ctx); if (!ctx || !data || reserved) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_op_reset (ctx, 2); if (err) return TRACE_ERR (err); err = _gpgme_op_data_lookup (ctx, OPDATA_KEYLIST, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return TRACE_ERR (err); _gpgme_engine_set_status_handler (ctx->engine, keylist_status_handler, ctx); err = _gpgme_engine_set_colon_line_handler (ctx->engine, keylist_colon_handler, ctx); if (err) return TRACE_ERR (err); err = _gpgme_engine_op_keylist_data (ctx->engine, data); return TRACE_ERR (err); } /* Return the next key from the keylist in R_KEY. */ gpgme_error_t gpgme_op_keylist_next (gpgme_ctx_t ctx, gpgme_key_t *r_key) { gpgme_error_t err; struct key_queue_item_s *queue_item; void *hook; op_data_t opd; TRACE_BEG (DEBUG_CTX, "gpgme_op_keylist_next", ctx); if (!ctx || !r_key) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); *r_key = NULL; if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_op_data_lookup (ctx, OPDATA_KEYLIST, &hook, -1, NULL); opd = hook; if (err) return TRACE_ERR (err); if (opd == NULL) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (!opd->key_queue) { err = _gpgme_wait_on_condition (ctx, &opd->key_cond, NULL); if (err) return TRACE_ERR (err); if (!opd->key_cond) return TRACE_ERR (opd->keydb_search_err? opd->keydb_search_err /**/ : gpg_error (GPG_ERR_EOF)); opd->key_cond = 0; assert (opd->key_queue); } queue_item = opd->key_queue; opd->key_queue = queue_item->next; if (!opd->key_queue) opd->key_cond = 0; *r_key = queue_item->key; free (queue_item); return TRACE_SUC2 ("key=%p (%s)", *r_key, ((*r_key)->subkeys && (*r_key)->subkeys->fpr) ? (*r_key)->subkeys->fpr : "invalid"); } /* Terminate a pending keylist operation within CTX. */ gpgme_error_t gpgme_op_keylist_end (gpgme_ctx_t ctx) { TRACE (DEBUG_CTX, "gpgme_op_keylist_end", ctx); if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); return 0; } /* Get the key with the fingerprint FPR from the crypto backend. If SECRET is true, get the secret key. */ gpgme_error_t gpgme_get_key (gpgme_ctx_t ctx, const char *fpr, gpgme_key_t *r_key, int secret) { gpgme_ctx_t listctx; gpgme_error_t err; gpgme_key_t result, key; TRACE_BEG2 (DEBUG_CTX, "gpgme_get_key", ctx, "fpr=%s, secret=%i", fpr, secret); if (r_key) *r_key = NULL; if (!ctx || !r_key || !fpr) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (strlen (fpr) < 8) /* We have at least a key ID. */ return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); /* FIXME: We use our own context because we have to avoid the user's I/O callback handlers. */ err = gpgme_new (&listctx); if (err) return TRACE_ERR (err); { gpgme_protocol_t proto; gpgme_engine_info_t info; /* Clone the relevant state. */ proto = gpgme_get_protocol (ctx); gpgme_set_protocol (listctx, proto); gpgme_set_keylist_mode (listctx, gpgme_get_keylist_mode (ctx)); info = gpgme_ctx_get_engine_info (ctx); while (info && info->protocol != proto) info = info->next; if (info) gpgme_ctx_set_engine_info (listctx, proto, info->file_name, info->home_dir); } err = gpgme_op_keylist_start (listctx, fpr, secret); if (!err) err = gpgme_op_keylist_next (listctx, &result); if (!err) { try_next_key: err = gpgme_op_keylist_next (listctx, &key); if (gpgme_err_code (err) == GPG_ERR_EOF) err = 0; else { if (!err && result && result->subkeys && result->subkeys->fpr && key && key->subkeys && key->subkeys->fpr && !strcmp (result->subkeys->fpr, key->subkeys->fpr)) { /* The fingerprint is identical. We assume that this is the same key and don't mark it as an ambiguous. This problem may occur with corrupted keyrings and has been noticed often with gpgsm. In fact gpgsm uses a similar hack to sort out such duplicates but it can't do that while listing keys. */ gpgme_key_unref (key); goto try_next_key; } if (!err) { gpgme_key_unref (key); err = gpg_error (GPG_ERR_AMBIGUOUS_NAME); } gpgme_key_unref (result); result = NULL; } } gpgme_release (listctx); if (! err) { *r_key = result; TRACE_LOG2 ("key=%p (%s)", *r_key, ((*r_key)->subkeys && (*r_key)->subkeys->fpr) ? (*r_key)->subkeys->fpr : "invalid"); } return TRACE_ERR (err); } diff --git a/src/keysign.c b/src/keysign.c index 5e497935..5cb47881 100644 --- a/src/keysign.c +++ b/src/keysign.c @@ -1,218 +1,219 @@ /* keysign.c - OpenPGP key signing * Copyright (C) 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" #include "util.h" typedef struct { /* The error code from a FAILURE status line or 0. */ gpg_error_t failure_code; /* The error code from certain ERROR status lines or 0. */ gpg_error_t error_code; } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; (void)opd; } /* Parse an error status line. Return the error location and the error code. The function may modify ARGS. */ static char * parse_error (char *args, gpg_error_t *r_err) { char *where = strchr (args, ' '); char *which; if (where) { *where = '\0'; which = where + 1; where = strchr (which, ' '); if (where) *where = '\0'; where = args; } else { *r_err = trace_gpg_error (GPG_ERR_INV_ENGINE); return NULL; } *r_err = atoi (which); return where; } static gpgme_error_t keysign_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; char *loc; /* Pipe the status code through the progress status handler. */ err = _gpgme_progress_status_handler (ctx, code, args); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_KEYSIGN, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_ERROR: loc = parse_error (args, &err); if (!loc) return err; if (!opd->error_code) opd->error_code = err; break; case GPGME_STATUS_FAILURE: opd->failure_code = _gpgme_parse_failure (args); break; case GPGME_STATUS_EOF: if (opd->error_code) return opd->error_code; else if (opd->failure_code) return opd->failure_code; break; case GPGME_STATUS_INQUIRE_MAXLEN: if (ctx->status_cb && !ctx->full_status) { err = ctx->status_cb (ctx->status_cb_value, "INQUIRE_MAXLEN", args); if (err) return err; } break; default: break; } return 0; } /* Sign the USERID of KEY using the current set of signers. If USERID * is NULL, sign all user ids. To put several user ids into USERID, * separate them by LF and set the flag GPGME_KEYSIGN_LFSEP. */ static gpgme_error_t keysign_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags) { gpgme_error_t err; void *hook; op_data_t opd; if (ctx->protocol != GPGME_PROTOCOL_OPENPGP) return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); err = _gpgme_op_reset (ctx, synchronous); if (err) return err; if (!key) return gpg_error (GPG_ERR_INV_ARG); err = _gpgme_op_data_lookup (ctx, OPDATA_KEYSIGN, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; _gpgme_engine_set_status_handler (ctx->engine, keysign_status_handler, ctx); if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } return _gpgme_engine_op_keysign (ctx->engine, key, userid, expires, flags, ctx); } /* Sign the USERID of KEY using the current set of signers. */ gpgme_error_t gpgme_op_keysign_start (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_keysign_start", ctx, "key=%p, uid='%s' flags=0x%x", key, userid, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = keysign_start (ctx, 0, key, userid, expires, flags); return TRACE_ERR (err); } gpgme_error_t gpgme_op_keysign (gpgme_ctx_t ctx, gpgme_key_t key, const char *userid, unsigned long expires, unsigned int flags) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_keysign", ctx, "key=%p, uid='%s' flags=0x%x", key, userid, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_ARG)); err = keysign_start (ctx, 1, key, userid, expires, flags); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } diff --git a/src/mbox-util.c b/src/mbox-util.c index 0dafc2a2..22816d64 100644 --- a/src/mbox-util.c +++ b/src/mbox-util.c @@ -1,275 +1,276 @@ /* mbox-util.c - Mail address helper functions * Copyright (C) 1998-2010 Free Software Foundation, Inc. * Copyright (C) 1998-2015 Werner Koch * * This file is part of GnuPG. * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This file 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 Lesser General Public License for more details. * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, see . + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ /* NB: This code has been taken from GnuPG. Please keep it in sync * with GnuPG. */ #if HAVE_CONFIG_H # include #endif #include #include #include #include #include #include "mbox-util.h" /* Lowercase all ASCII characters in STRING. */ static char * ascii_strlwr (char *string) { char *p; for (p = string; *p; p++ ) if (!(*p & ~0x7f) && *p >= 'A' && *p <= 'Z') *p |= 0x20; return string; } static int string_count_chr (const char *string, int c) { int count; for (count=0; *string; string++ ) if ( *string == c ) count++; return count; } static int mem_count_chr (const void *buffer, int c, size_t length) { const char *s = buffer; int count; for (count=0; length; length--, s++) if (*s == c) count++; return count; } /* This is a case-sensitive version of our memistr. I wonder why no standard function memstr exists but I better do not use the name memstr to avoid future conflicts. */ static const char * my_memstr (const void *buffer, size_t buflen, const char *sub) { const unsigned char *buf = buffer; const unsigned char *t = (const unsigned char *)buf; const unsigned char *s = (const unsigned char *)sub; size_t n = buflen; for ( ; n ; t++, n-- ) { if (*t == *s) { for (buf = t++, buflen = n--, s++; n && *t ==*s; t++, s++, n--) ; if (!*s) return (const char*)buf; t = (const unsigned char *)buf; s = (const unsigned char *)sub ; n = buflen; } } return NULL; } static int string_has_ctrl_or_space (const char *string) { for (; *string; string++ ) if (!(*string & 0x80) && *string <= 0x20) return 1; return 0; } /* Return true if STRING has two consecutive '.' after an '@' sign. */ static int has_dotdot_after_at (const char *string) { string = strchr (string, '@'); if (!string) return 0; /* No at-sign. */ string++; return !!strstr (string, ".."); } /* Check whether BUFFER has characters not valid in an RFC-822 address. LENGTH gives the length of BUFFER. To cope with OpenPGP we ignore non-ascii characters so that for example umlauts are legal in an email address. An OpenPGP user ID must be utf-8 encoded but there is no strict requirement for RFC-822. Thus to avoid IDNA encoding we put the address verbatim as utf-8 into the user ID under the assumption that mail programs handle IDNA at a lower level and take OpenPGP user IDs as utf-8. Note that we can't do an utf-8 encoding checking here because in keygen.c this function is called with the native encoding and native to utf-8 encoding is only done later. */ static int has_invalid_email_chars (const void *buffer, size_t length) { const unsigned char *s = buffer; int at_seen=0; const char *valid_chars= "01234567890_-.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; for ( ; length && *s; length--, s++ ) { if ((*s & 0x80)) continue; /* We only care about ASCII. */ if (*s == '@') at_seen=1; else if (!at_seen && !(strchr (valid_chars, *s) || strchr ("!#$%&'*+/=?^`{|}~", *s))) return 1; else if (at_seen && !strchr (valid_chars, *s)) return 1; } return 0; } /* Same as is_valid_mailbox (see below) but operates on non-nul terminated buffer. */ static int is_valid_mailbox_mem (const void *name_arg, size_t namelen) { const char *name = name_arg; return !( !name || !namelen || has_invalid_email_chars (name, namelen) || mem_count_chr (name, '@', namelen) != 1 || *name == '@' || name[namelen-1] == '@' || name[namelen-1] == '.' || my_memstr (name, namelen, "..")); } /* Check whether NAME represents a valid mailbox according to RFC822. Returns true if so. */ int _gpgme_is_valid_mailbox (const char *name) { return name? is_valid_mailbox_mem (name, strlen (name)) : 0; } /* Return the mailbox (local-part@domain) form a standard user id. All plain ASCII characters in the result are converted to lowercase. Caller must free the result. Returns NULL if no valid mailbox was found (or we are out of memory). */ char * _gpgme_mailbox_from_userid (const char *userid) { const char *s, *s_end; size_t len; char *result = NULL; s = strchr (userid, '<'); if (s) { /* Seems to be a standard user id. */ s++; s_end = strchr (s, '>'); if (s_end && s_end > s) { len = s_end - s; result = malloc (len + 1); if (!result) return NULL; /* Ooops - out of core. */ strncpy (result, s, len); result[len] = 0; /* Apply some basic checks on the address. We do not use is_valid_mailbox because those checks are too strict. */ if (string_count_chr (result, '@') != 1 /* Need exactly one '@. */ || *result == '@' /* local-part missing. */ || result[len-1] == '@' /* domain missing. */ || result[len-1] == '.' /* ends with a dot. */ || string_has_ctrl_or_space (result) || has_dotdot_after_at (result)) { free (result); result = NULL; errno = EINVAL; } } else errno = EINVAL; } else if (_gpgme_is_valid_mailbox (userid)) { /* The entire user id is a mailbox. Return that one. Note that this fallback method has some restrictions on the valid syntax of the mailbox. However, those who want weird addresses should know about it and use the regular <...> syntax. */ result = strdup (userid); } else errno = EINVAL; return result? ascii_strlwr (result): NULL; } /* /\* Check whether UID is a valid standard user id of the form */ /* "Heinrich Heine " */ /* and return true if this is the case. *\/ */ /* int */ /* is_valid_user_id (const char *uid) */ /* { */ /* if (!uid || !*uid) */ /* return 0; */ /* return 1; */ /* } */ /* * Exported public API */ /* Return the mail address ("addr-spec" as per RFC-5322) from a string * which is assumed to be an user id ("address" in RFC-5322). All * plain ASCII characters (those with bit 7 cleared) in the result * are converted to lowercase. Caller must free the result using * gpgme_free. Returns NULL if no valid address was found (in which * case ERRNO is set to EINVAL) or for other errors. */ char * gpgme_addrspec_from_uid (const char *uid) { return _gpgme_mailbox_from_userid (uid); } diff --git a/src/op-support.c b/src/op-support.c index 9414e612..6affb5e4 100644 --- a/src/op-support.c +++ b/src/op-support.c @@ -1,432 +1,433 @@ /* op-support.c - Supporting functions. - Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2002, 2003, 2004, 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #include #ifdef HAVE_LOCALE_H #include #endif #include "gpgme.h" #include "context.h" #include "ops.h" #include "util.h" #include "debug.h" #if GPG_ERROR_VERSION_NUMBER < 0x011700 /* 1.23 */ # define GPG_ERR_SUBKEYS_EXP_OR_REV 217 #endif gpgme_error_t _gpgme_op_data_lookup (gpgme_ctx_t ctx, ctx_op_data_id_t type, void **hook, int size, void (*cleanup) (void *)) { struct ctx_op_data *data; if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); data = ctx->op_data; while (data && data->type != type) data = data->next; if (!data) { if (size < 0) { *hook = NULL; return 0; } data = calloc (1, sizeof (struct ctx_op_data) + size); if (!data) return gpg_error_from_syserror (); data->magic = CTX_OP_DATA_MAGIC; data->next = ctx->op_data; data->type = type; data->cleanup = cleanup; data->hook = (void *) (((char *) data) + sizeof (struct ctx_op_data)); data->references = 1; ctx->op_data = data; } *hook = data->hook; return 0; } /* type is: 0: asynchronous operation (use global or user event loop). 1: synchronous operation (always use private event loop). 2: asynchronous private operation (use private or user event loop). 256: Modification flag to suppress the engine reset. */ gpgme_error_t _gpgme_op_reset (gpgme_ctx_t ctx, int type) { gpgme_error_t err = 0; struct gpgme_io_cbs io_cbs; int no_reset = (type & 256); int reuse_engine = 0; type &= 255; _gpgme_release_result (ctx); LOCK (ctx->lock); ctx->canceled = 0; ctx->redraw_suggested = 0; UNLOCK (ctx->lock); if (ctx->engine && no_reset) reuse_engine = 1; else if (ctx->engine) { /* Attempt to reset an existing engine. */ err = _gpgme_engine_reset (ctx->engine); if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED) { _gpgme_engine_release (ctx->engine); ctx->engine = NULL; } } if (!ctx->engine) { gpgme_engine_info_t info; info = ctx->engine_info; while (info && info->protocol != ctx->protocol) info = info->next; if (!info) return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL); /* Create an engine object. */ err = _gpgme_engine_new (info, &ctx->engine); if (err) return err; } if (!reuse_engine) { err = 0; #ifdef LC_CTYPE err = _gpgme_engine_set_locale (ctx->engine, LC_CTYPE, ctx->lc_ctype); #endif #ifdef LC_MESSAGES if (!err) err = _gpgme_engine_set_locale (ctx->engine, LC_MESSAGES, ctx->lc_messages); #endif if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED) err = 0; _gpgme_engine_set_engine_flags (ctx->engine, ctx); if (!err) { err = _gpgme_engine_set_pinentry_mode (ctx->engine, ctx->pinentry_mode); if (gpg_err_code (err) == GPG_ERR_NOT_IMPLEMENTED) err = 0; } if (!err && ctx->status_cb && ctx->full_status) { _gpgme_engine_set_status_cb (ctx->engine, ctx->status_cb, ctx->status_cb_value); } if (err) { _gpgme_engine_release (ctx->engine); ctx->engine = NULL; return err; } } if (ctx->sub_protocol != GPGME_PROTOCOL_DEFAULT) { err = _gpgme_engine_set_protocol (ctx->engine, ctx->sub_protocol); if (err) return err; } if (type == 1 || (type == 2 && !ctx->io_cbs.add)) { /* Use private event loop. */ io_cbs.add = _gpgme_add_io_cb; io_cbs.add_priv = ctx; io_cbs.remove = _gpgme_remove_io_cb; io_cbs.event = _gpgme_wait_private_event_cb; io_cbs.event_priv = ctx; } else if (! ctx->io_cbs.add) { /* Use global event loop. */ io_cbs.add = _gpgme_add_io_cb; io_cbs.add_priv = ctx; io_cbs.remove = _gpgme_remove_io_cb; io_cbs.event = _gpgme_wait_global_event_cb; io_cbs.event_priv = ctx; } else { /* Use user event loop. */ io_cbs.add = _gpgme_wait_user_add_io_cb; io_cbs.add_priv = ctx; io_cbs.remove = _gpgme_wait_user_remove_io_cb; io_cbs.event = _gpgme_wait_user_event_cb; io_cbs.event_priv = ctx; } _gpgme_engine_set_io_cbs (ctx->engine, &io_cbs); return err; } /* Parse the INV_RECP or INV_SNDR status line in ARGS and return the result in KEY. If KC_FPR (from the KEY_CONSIDERED status line) is not NULL take the KC_FLAGS in account. */ gpgme_error_t _gpgme_parse_inv_recp (char *args, int for_signing, const char *kc_fpr, unsigned int kc_flags, gpgme_invalid_key_t *key) { gpgme_invalid_key_t inv_key; char *tail; long int reason; (void)for_signing; inv_key = calloc (1, sizeof (*inv_key)); if (!inv_key) return gpg_error_from_syserror (); inv_key->next = NULL; gpg_err_set_errno (0); reason = strtol (args, &tail, 0); if (errno || args == tail || (*tail && *tail != ' ')) { /* The crypto backend does not behave. */ free (inv_key); return trace_gpg_error (GPG_ERR_INV_ENGINE); } switch (reason) { case 0: if (kc_fpr && (kc_flags & 2)) inv_key->reason = gpg_error (GPG_ERR_SUBKEYS_EXP_OR_REV); else inv_key->reason = gpg_error (GPG_ERR_GENERAL); break; case 1: inv_key->reason = gpg_error (GPG_ERR_NO_PUBKEY); break; case 2: inv_key->reason = gpg_error (GPG_ERR_AMBIGUOUS_NAME); break; case 3: inv_key->reason = gpg_error (GPG_ERR_WRONG_KEY_USAGE); break; case 4: inv_key->reason = gpg_error (GPG_ERR_CERT_REVOKED); break; case 5: inv_key->reason = gpg_error (GPG_ERR_CERT_EXPIRED); break; case 6: inv_key->reason = gpg_error (GPG_ERR_NO_CRL_KNOWN); break; case 7: inv_key->reason = gpg_error (GPG_ERR_CRL_TOO_OLD); break; case 8: inv_key->reason = gpg_error (GPG_ERR_NO_POLICY_MATCH); break; case 9: inv_key->reason = gpg_error (GPG_ERR_NO_SECKEY); break; case 10: inv_key->reason = gpg_error (GPG_ERR_PUBKEY_NOT_TRUSTED); break; case 11: inv_key->reason = gpg_error (GPG_ERR_MISSING_CERT); break; case 12: inv_key->reason = gpg_error (GPG_ERR_MISSING_ISSUER_CERT); break; case 13: inv_key->reason = gpg_error (252); /*GPG_ERR_KEY_DISABLED*/ break; case 14: inv_key->reason = gpg_error (GPG_ERR_INV_USER_ID); break; default: inv_key->reason = gpg_error (GPG_ERR_GENERAL); break; } while (*tail && *tail == ' ') tail++; if (*tail) { inv_key->fpr = strdup (tail); if (!inv_key->fpr) { free (inv_key); return gpg_error_from_syserror (); } } *key = inv_key; return 0; } /* Parse a KEY_CONSIDERED status line in ARGS and store the * fingerprint and the flags at R_FPR and R_FLAGS. The caller must * free the value at R_FPR on success. */ gpgme_error_t _gpgme_parse_key_considered (const char *args, char **r_fpr, unsigned int *r_flags) { char *pend; size_t n; *r_fpr = NULL; pend = strchr (args, ' '); if (!pend || pend == args) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Bogus status line. */ n = pend - args; *r_fpr = malloc (n + 1); if (!*r_fpr) return gpg_error_from_syserror (); memcpy (*r_fpr, args, n); (*r_fpr)[n] = 0; args = pend + 1; gpg_err_set_errno (0); *r_flags = strtoul (args, &pend, 0); if (errno || args == pend || (*pend && *pend != ' ')) { free (*r_fpr); *r_fpr = NULL; return trace_gpg_error (GPG_ERR_INV_ENGINE); } return 0; } /* Parse the PLAINTEXT status line in ARGS and return the result in FILENAMEP. */ gpgme_error_t _gpgme_parse_plaintext (char *args, char **filenamep, int *r_mime) { char *tail; while (*args == ' ') args++; if (*args == '\0') return 0; /* First argument is file type (a one byte uppercase hex value). */ if (args[0] == '6' && args[1] == 'D') *r_mime = 1; while (*args != ' ' && *args != '\0') args++; while (*args == ' ') args++; if (*args == '\0') return 0; /* Second argument is the timestamp. */ while (*args != ' ' && *args != '\0') args++; while (*args == ' ') args++; if (*args == '\0') return 0; tail = args; while (*tail != ' ' && *tail != '\0') tail++; *tail = '\0'; if (filenamep && *args != '\0') { char *filename = strdup (args); if (!filename) return gpg_error_from_syserror (); *filenamep = filename; } return 0; } /* Parse a FAILURE status line and return the error code. ARGS is * modified to contain the location part. Note that for now we ignore * failure codes with a location of gpg-exit; they are too trouble * some. Instead we should eventually record that error in the * context and provide a function to return a fuller error * description; this could then also show the location of the error * (e.g. "option- parser") to make it easier for the user to detect * the actual error. */ gpgme_error_t _gpgme_parse_failure (char *args) { char *where, *which; if (!strncmp (args, "gpg-exit", 8)) return 0; where = strchr (args, ' '); if (!where) return trace_gpg_error (GPG_ERR_INV_ENGINE); *where = '\0'; which = where + 1; where = strchr (which, ' '); if (where) *where = '\0'; return atoi (which); } diff --git a/src/opassuan.c b/src/opassuan.c index 2bbaacd8..c50ae557 100644 --- a/src/opassuan.c +++ b/src/opassuan.c @@ -1,230 +1,231 @@ /* opassuan.c - Low-level Assuan operations. - Copyright (C) 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif /* Suppress warning for accessing deprecated member "err". */ #define _GPGME_IN_GPGME 1 #include "gpgme.h" #include "context.h" #include "ops.h" #include "util.h" #include "debug.h" /* LEGACY: Remove this when removing the deprecated result structure. */ typedef struct { struct _gpgme_op_assuan_result result; } *op_data_t; static gpgme_error_t opassuan_start (gpgme_ctx_t ctx, int synchronous, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { gpgme_error_t err; if (!command || !*command) return gpg_error (GPG_ERR_INV_VALUE); /* The flag value 256 is used to suppress an engine reset. This is required to keep the connection running. */ err = _gpgme_op_reset (ctx, ((synchronous&255) | 256)); if (err) return err; { /* LEGACY: Remove this when removing the deprecated result structure. */ void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_ASSUAN, &hook, sizeof (*opd), NULL); if (err) return err; } return _gpgme_engine_op_assuan_transact (ctx->engine, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); } /* XXXX. This is the asynchronous variant. */ gpgme_error_t gpgme_op_assuan_transact_start (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { gpg_error_t err; TRACE_BEG7 (DEBUG_CTX, "gpgme_op_assuan_transact_start", ctx, "command=%s, data_cb=%p/%p, inq_cb=%p/%p, status_cb=%p/%p", command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = opassuan_start (ctx, 0, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); return TRACE_ERR (err); } /* XXXX. This is the synchronous variant. */ gpgme_error_t gpgme_op_assuan_transact_ext (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value, gpgme_error_t *op_err_p) { gpgme_error_t err; gpgme_error_t op_err; TRACE_BEG8 (DEBUG_CTX, "gpgme_op_assuan_transact", ctx, "command=%s, data_cb=%p/%p, inq_cb=%p/%p, status_cb=%p/%p, " "op_err=%p", command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value, op_err_p); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = opassuan_start (ctx, 1, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); if (err) goto out; err = _gpgme_wait_one_ext (ctx, &op_err); if (op_err) { TRACE_LOG2 ("op_err = %s <%s>", gpgme_strerror (op_err), gpgme_strsource (op_err)); if (! op_err_p) { TRACE_LOG ("warning: operational error ignored by user"); } } if (op_err_p) *op_err_p = op_err; out: return TRACE_ERR (err); } /* Compatibility code for old interface. */ /* Evil hack breaking abstractions for the purpose of localizing our other hack. This is copied from engine.c. */ struct engine { struct engine_ops *ops; void *engine; }; gpg_error_t _gpgme_engine_assuan_last_op_err (void *engine); gpgme_assuan_result_t gpgme_op_assuan_result (gpgme_ctx_t ctx) { gpgme_error_t err; void *hook; op_data_t opd; TRACE_BEG (DEBUG_CTX, "gpgme_op_assuan_result", ctx); err = _gpgme_op_data_lookup (ctx, OPDATA_ASSUAN, &hook, -1, NULL); opd = hook; /* Check in case this function is used without having run a command before. */ if (err || !opd) { TRACE_SUC0 ("result=(null)"); return NULL; } /* All of this is a hack for the old style interface. The new style interface returns op errors directly. */ opd->result.err = _gpgme_engine_assuan_last_op_err (ctx->engine->engine); if (opd->result.err) { TRACE_LOG1 ("err = %s", gpg_strerror (0)); } else { TRACE_LOG2 ("err = %s <%s>", gpg_strerror (opd->result.err), gpg_strsource (opd->result.err)); } TRACE_SUC1 ("result=%p", &opd->result); return &opd->result; } gpgme_error_t gpgme_op_assuan_transact (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { gpgme_error_t err; TRACE (DEBUG_CTX, "gpgme_op_assuan_transact", ctx); if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); /* Users of the old-style session based interfaces need to look at the result structure. */ err = gpgme_op_assuan_transact_ext (ctx, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value, NULL); return err; } diff --git a/src/parsetlv.c b/src/parsetlv.c index b311a73f..69f48eb4 100644 --- a/src/parsetlv.c +++ b/src/parsetlv.c @@ -1,103 +1,104 @@ /* parsetlv.c - ASN.1 TLV functions * Copyright (C) 2005, 2007, 2008, 2012 g10 Code GmbH * * This file is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * This file 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 Lesser General Public License for more details. * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, see . + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include "parsetlv.h" /* Simple but pretty complete ASN.1 BER parser. Parse the data at the address of BUFFER with a length given at the address of SIZE. On success return 0 and update BUFFER and SIZE to point to the value. Do not update them on error. The information about the object are stored in the caller allocated TI structure. */ int _gpgme_parse_tlv (char const **buffer, size_t *size, tlvinfo_t *ti) { int c; unsigned long tag; const unsigned char *buf = (const unsigned char *)(*buffer); size_t length = *size; ti->cls = 0; ti->tag = 0; ti->is_cons = 0; ti->is_ndef = 0; ti->length = 0; ti->nhdr = 0; if (!length) return -1; c = *buf++; length--; ++ti->nhdr; ti->cls = (c & 0xc0) >> 6; ti->is_cons = !!(c & 0x20); tag = c & 0x1f; if (tag == 0x1f) { tag = 0; do { tag <<= 7; if (!length) return -1; c = *buf++; length--; ++ti->nhdr; tag |= c & 0x7f; } while (c & 0x80); } ti->tag = tag; if (!length) return -1; c = *buf++; length--; ++ti->nhdr; if ( !(c & 0x80) ) ti->length = c; else if (c == 0x80) ti->is_ndef = 1; else if (c == 0xff) return -1; else { unsigned long len = 0; int count = (c & 0x7f); if (count > sizeof (len) || count > sizeof (size_t)) return -1; for (; count; count--) { len <<= 8; if (!length) return -1; c = *buf++; length--; ++ti->nhdr; len |= c & 0xff; } ti->length = len; } *buffer = (void*)buf; *size = length; return 0; } diff --git a/src/passphrase.c b/src/passphrase.c index 74d235cb..2a7daea1 100644 --- a/src/passphrase.c +++ b/src/passphrase.c @@ -1,191 +1,191 @@ /* passphrase.c - Passphrase callback. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "gpgme.h" #include "context.h" #include "ops.h" #include "util.h" #include "debug.h" typedef struct { int no_passphrase; char *uid_hint; char *passphrase_info; int bad_passphrase; char *maxlen; } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; if (opd->passphrase_info) free (opd->passphrase_info); if (opd->uid_hint) free (opd->uid_hint); free (opd->maxlen); } gpgme_error_t _gpgme_passphrase_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_INQUIRE_MAXLEN: free (opd->maxlen); if (!(opd->maxlen = strdup (args))) return gpg_error_from_syserror (); break; case GPGME_STATUS_USERID_HINT: if (opd->uid_hint) free (opd->uid_hint); if (!(opd->uid_hint = strdup (args))) return gpg_error_from_syserror (); break; case GPGME_STATUS_BAD_PASSPHRASE: opd->bad_passphrase++; opd->no_passphrase = 0; break; case GPGME_STATUS_GOOD_PASSPHRASE: opd->bad_passphrase = 0; opd->no_passphrase = 0; break; case GPGME_STATUS_NEED_PASSPHRASE: case GPGME_STATUS_NEED_PASSPHRASE_SYM: case GPGME_STATUS_NEED_PASSPHRASE_PIN: if (opd->passphrase_info) free (opd->passphrase_info); opd->passphrase_info = strdup (args); if (!opd->passphrase_info) return gpg_error_from_syserror (); break; case GPGME_STATUS_MISSING_PASSPHRASE: opd->no_passphrase = 1; break; case GPGME_STATUS_EOF: if (opd->no_passphrase || opd->bad_passphrase) return gpg_error (GPG_ERR_BAD_PASSPHRASE); break; case GPGME_STATUS_ERROR: /* We abuse this status handler to forward ERROR status codes to the caller. */ if (ctx->status_cb && !ctx->full_status) { err = ctx->status_cb (ctx->status_cb_value, "ERROR", args); if (err) return err; } break; case GPGME_STATUS_FAILURE: /* We abuse this status handler to forward FAILURE status codes to the caller. */ if (ctx->status_cb && !ctx->full_status) { err = ctx->status_cb (ctx->status_cb_value, "FAILURE", args); if (err) return err; } break; default: /* Ignore all other codes. */ break; } return 0; } gpgme_error_t _gpgme_passphrase_command_handler (void *priv, gpgme_status_code_t code, const char *key, int fd, int *processed) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; assert (ctx->passphrase_cb); err = _gpgme_op_data_lookup (ctx, OPDATA_PASSPHRASE, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; if (code == GPGME_STATUS_GET_HIDDEN && (!strcmp (key, "passphrase.enter") || !strcmp (key, "passphrase.pin.ask"))) { if (processed) *processed = 1; /* Fake a status line to to convey the MAXLEN info. */ if (ctx->status_cb && opd->maxlen) err = ctx->status_cb (ctx->status_cb_value, "INQUIRE_MAXLEN", opd->maxlen); if (!err) err = ctx->passphrase_cb (ctx->passphrase_cb_value, opd->uid_hint, opd->passphrase_info, opd->bad_passphrase, fd); /* Reset bad passphrase flag, in case it is correct now. */ opd->bad_passphrase = 0; return err; } return 0; } diff --git a/src/passwd.c b/src/passwd.c index 6c03002b..154ae287 100644 --- a/src/passwd.c +++ b/src/passwd.c @@ -1,204 +1,205 @@ /* passwd.c - Passphrase changing function - Copyright (C) 2010 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2010 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" typedef struct { /* The error code from a FAILURE status line or 0. */ gpg_error_t failure_code; int success_seen; int error_seen; } *op_data_t; /* Parse an error status line and return the error code. */ static gpgme_error_t parse_error (char *args) { gpgme_error_t err; char *where = strchr (args, ' '); char *which; if (where) { *where = '\0'; which = where + 1; where = strchr (which, ' '); if (where) *where = '\0'; where = args; } else return trace_gpg_error (GPG_ERR_INV_ENGINE); err = atoi (which); if (!strcmp (where, "keyedit.passwd")) return err; return 0; } static gpgme_error_t passwd_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_PASSWD, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_ERROR: err = parse_error (args); if (err) opd->error_seen = 1; break; case GPGME_STATUS_SUCCESS: opd->success_seen = 1; break; case GPGME_STATUS_FAILURE: opd->failure_code = _gpgme_parse_failure (args); break; case GPGME_STATUS_EOF: /* In case the OpenPGP engine does not properly implement the passwd command we won't get a success status back and thus we conclude that this operation is not supported. This is for example the case for GnuPG < 2.0.16. Note that this test is obsolete for assuan based engines because they will properly return an error for an unknown command. */ if (ctx->protocol == GPGME_PROTOCOL_OpenPGP && !opd->error_seen && !opd->success_seen) err = gpg_error (GPG_ERR_NOT_SUPPORTED); else if (opd->failure_code) err = opd->failure_code; break; default: break; } return err; } static gpgme_error_t passwd_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t key, unsigned int flags) { gpgme_error_t err; void *hook; op_data_t opd; if (!key) return gpg_error (GPG_ERR_INV_VALUE); if (flags) return gpg_error (GPG_ERR_INV_FLAG); err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_PASSWD, &hook, sizeof (*opd), NULL); opd = hook; if (err) return err; opd->success_seen = 0; opd->error_seen = 0; _gpgme_engine_set_status_handler (ctx->engine, passwd_status_handler, ctx); if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } return _gpgme_engine_op_passwd (ctx->engine, key, flags); } /* Change the passphrase for KEY. FLAGS is reserved for future use and must be passed as 0. The engine is expected to present a user interface to enter the old and the new passphrase. This is the asynchronous variant. Note that if ever the need arises to supply a passphrase we can do this with a flag value and the passphrase callback feature. */ gpgme_error_t gpgme_op_passwd_start (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags) { gpg_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_passwd_start", ctx, "key=%p, flags=0x%x", key, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = passwd_start (ctx, 0, key, flags); return TRACE_ERR (err); } /* Change the passphrase for KEY. FLAGS is reserved for future use and must be passed as 0. This is the synchronous variant. */ gpgme_error_t gpgme_op_passwd (gpgme_ctx_t ctx, gpgme_key_t key, unsigned int flags) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_passwd", ctx, "key=%p, flags=0x%x", key, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = passwd_start (ctx, 1, key, flags); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } diff --git a/src/posix-io.c b/src/posix-io.c index 322c41a8..67c6e392 100644 --- a/src/posix-io.c +++ b/src/posix-io.c @@ -1,875 +1,876 @@ /* posix-io.c - Posix I/O functions - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2004, 2005, 2007, 2010 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2004, 2005, 2007, 2010 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #ifdef HAVE_CONFIG_H #include #endif #include #include #ifdef HAVE_STDINT_H # include #endif #include #include #include #include #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_SYS_TIME_H # include #endif #ifdef HAVE_SYS_TYPES_H # include #endif #include #ifdef HAVE_SYS_UIO_H # include #endif #include #include #ifdef USE_LINUX_GETDENTS # include # include # include #endif /*USE_LINUX_GETDENTS*/ #include "util.h" #include "priv-io.h" #include "sema.h" #include "ath.h" #include "debug.h" void _gpgme_io_subsystem_init (void) { struct sigaction act; sigaction (SIGPIPE, NULL, &act); if (act.sa_handler == SIG_DFL) { act.sa_handler = SIG_IGN; sigemptyset (&act.sa_mask); act.sa_flags = 0; sigaction (SIGPIPE, &act, NULL); } } /* Write the printable version of FD to the buffer BUF of length BUFLEN. The printable version is the representation on the command line that the child process expects. */ int _gpgme_io_fd2str (char *buf, int buflen, int fd) { return snprintf (buf, buflen, "%d", fd); } /* The table to hold notification handlers. We use a linear search and extend the table as needed. */ struct notify_table_item_s { int fd; /* -1 indicates an unused entry. */ _gpgme_close_notify_handler_t handler; void *value; }; typedef struct notify_table_item_s *notify_table_item_t; static notify_table_item_t notify_table; static size_t notify_table_size; DEFINE_STATIC_LOCK (notify_table_lock); int _gpgme_io_read (int fd, void *buffer, size_t count) { int nread; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_read", fd, "buffer=%p, count=%u", buffer, count); do { nread = _gpgme_ath_read (fd, buffer, count); } while (nread == -1 && errno == EINTR); TRACE_LOGBUFX (buffer, nread); return TRACE_SYSRES (nread); } int _gpgme_io_write (int fd, const void *buffer, size_t count) { int nwritten; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_write", fd, "buffer=%p, count=%u", buffer, count); TRACE_LOGBUFX (buffer, count); do { nwritten = _gpgme_ath_write (fd, buffer, count); } while (nwritten == -1 && errno == EINTR); return TRACE_SYSRES (nwritten); } int _gpgme_io_pipe (int filedes[2], int inherit_idx) { int saved_errno; int err; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_pipe", filedes, "inherit_idx=%i (GPGME uses it for %s)", inherit_idx, inherit_idx ? "reading" : "writing"); err = pipe (filedes); if (err < 0) return TRACE_SYSRES (err); /* FIXME: Should get the old flags first. */ err = fcntl (filedes[1 - inherit_idx], F_SETFD, FD_CLOEXEC); saved_errno = errno; if (err < 0) { close (filedes[0]); close (filedes[1]); } errno = saved_errno; if (err) return TRACE_SYSRES (err); return TRACE_SUC2 ("read=0x%x, write=0x%x", filedes[0], filedes[1]); } int _gpgme_io_close (int fd) { int res; _gpgme_close_notify_handler_t handler = NULL; void *handler_value; int idx; TRACE_BEG (DEBUG_SYSIO, "_gpgme_io_close", fd); if (fd == -1) { errno = EINVAL; return TRACE_SYSRES (-1); } /* First call the notify handler. */ LOCK (notify_table_lock); for (idx=0; idx < notify_table_size; idx++) { if (notify_table[idx].fd == fd) { handler = notify_table[idx].handler; handler_value = notify_table[idx].value; notify_table[idx].handler = NULL; notify_table[idx].value = NULL; notify_table[idx].fd = -1; /* Mark slot as free. */ break; } } UNLOCK (notify_table_lock); if (handler) { TRACE_LOG2 ("invoking close handler %p/%p", handler, handler_value); handler (fd, handler_value); } /* Then do the close. */ res = close (fd); return TRACE_SYSRES (res); } int _gpgme_io_set_close_notify (int fd, _gpgme_close_notify_handler_t handler, void *value) { int res = 0; int idx; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_set_close_notify", fd, "close_handler=%p/%p", handler, value); assert (fd != -1); LOCK (notify_table_lock); for (idx=0; idx < notify_table_size; idx++) if (notify_table[idx].fd == -1) break; if (idx == notify_table_size) { /* We need to increase the size of the table. The approach we take is straightforward to minimize the risk of bugs. */ notify_table_item_t newtbl; size_t newsize = notify_table_size + 64; newtbl = calloc (newsize, sizeof *newtbl); if (!newtbl) { res = -1; goto leave; } for (idx=0; idx < notify_table_size; idx++) newtbl[idx] = notify_table[idx]; for (; idx < newsize; idx++) { newtbl[idx].fd = -1; newtbl[idx].handler = NULL; newtbl[idx].value = NULL; } free (notify_table); notify_table = newtbl; idx = notify_table_size; notify_table_size = newsize; } notify_table[idx].fd = fd; notify_table[idx].handler = handler; notify_table[idx].value = value; leave: UNLOCK (notify_table_lock); return TRACE_SYSRES (res); } int _gpgme_io_set_nonblocking (int fd) { int flags; int res; TRACE_BEG (DEBUG_SYSIO, "_gpgme_io_set_nonblocking", fd); flags = fcntl (fd, F_GETFL, 0); if (flags == -1) return TRACE_SYSRES (-1); flags |= O_NONBLOCK; res = fcntl (fd, F_SETFL, flags); return TRACE_SYSRES (res); } #ifdef USE_LINUX_GETDENTS /* This is not declared in public headers; getdents64(2) says that we must * define it ourselves. */ struct linux_dirent64 { ino64_t d_ino; off64_t d_off; unsigned short d_reclen; unsigned char d_type; char d_name[]; }; # define DIR_BUF_SIZE 1024 #endif /*USE_LINUX_GETDENTS*/ static long int get_max_fds (void) { const char *source = NULL; long int fds = -1; int rc; /* Under Linux we can figure out the highest used file descriptor by * reading /proc/self/fd. This is in the common cases much faster * than for example doing 4096 close calls where almost all of them * will fail. * * We can't use the normal opendir/readdir/closedir interface between * fork and exec in a multi-threaded process because opendir uses * malloc and thus a mutex which may deadlock with a malloc in another * thread. However, the underlying getdents system call is safe. */ #ifdef USE_LINUX_GETDENTS { int dir_fd; char dir_buf[DIR_BUF_SIZE]; struct linux_dirent64 *dir_entry; int r, pos; const char *s; int x; dir_fd = open ("/proc/self/fd", O_RDONLY | O_DIRECTORY); if (dir_fd != -1) { for (;;) { r = syscall(SYS_getdents64, dir_fd, dir_buf, DIR_BUF_SIZE); if (r == -1) { /* Fall back to other methods. */ fds = -1; break; } if (r == 0) break; for (pos = 0; pos < r; pos += dir_entry->d_reclen) { dir_entry = (struct linux_dirent64 *) (dir_buf + pos); s = dir_entry->d_name; if (*s < '0' || *s > '9') continue; /* atoi is not guaranteed to be async-signal-safe. */ for (x = 0; *s >= '0' && *s <= '9'; s++) x = x * 10 + (*s - '0'); if (!*s && x > fds && x != dir_fd) fds = x; } } close (dir_fd); } if (fds != -1) { fds++; source = "/proc"; } } #endif /*USE_LINUX_GETDENTS*/ #ifdef RLIMIT_NOFILE if (fds == -1) { struct rlimit rl; rc = getrlimit (RLIMIT_NOFILE, &rl); if (rc == 0) { source = "RLIMIT_NOFILE"; fds = rl.rlim_max; } } #endif #ifdef RLIMIT_OFILE if (fds == -1) { struct rlimit rl; rc = getrlimit (RLIMIT_OFILE, &rl); if (rc == 0) { source = "RLIMIT_OFILE"; fds = rl.rlim_max; } } #endif #ifdef _SC_OPEN_MAX if (fds == -1) { long int scres; scres = sysconf (_SC_OPEN_MAX); if (scres >= 0) { source = "_SC_OPEN_MAX"; return scres; } } #endif #ifdef OPEN_MAX if (fds == -1) { source = "OPEN_MAX"; fds = OPEN_MAX; } #endif #if !defined(RLIMIT_NOFILE) && !defined(RLIMIT_OFILE) \ && !defined(_SC_OPEN_MAX) && !defined(OPEN_MAX) #warning "No known way to get the maximum number of file descriptors." #endif if (fds == -1) { source = "arbitrary"; /* Arbitrary limit. */ fds = 1024; } /* AIX returns INT32_MAX instead of a proper value. We assume that * this is always an error and use a more reasonable limit. */ #ifdef INT32_MAX if (fds == INT32_MAX) { source = "aix-fix"; fds = 1024; } #endif TRACE2 (DEBUG_SYSIO, "gpgme:max_fds", 0, "max fds=%i (%s)", fds, source); return fds; } int _gpgme_io_waitpid (int pid, int hang, int *r_status, int *r_signal) { int status; pid_t ret; *r_status = 0; *r_signal = 0; do ret = _gpgme_ath_waitpid (pid, &status, hang? 0 : WNOHANG); while (ret == (pid_t)(-1) && errno == EINTR); if (ret == pid) { if (WIFSIGNALED (status)) { *r_status = 4; /* Need some value here. */ *r_signal = WTERMSIG (status); } else if (WIFEXITED (status)) *r_status = WEXITSTATUS (status); else *r_status = 4; /* Oops. */ return 1; } return 0; } /* Returns 0 on success, -1 on error. */ int _gpgme_io_spawn (const char *path, char *const argv[], unsigned int flags, struct spawn_fd_item_s *fd_list, void (*atfork) (void *opaque, int reserved), void *atforkvalue, pid_t *r_pid) { pid_t pid; int i; int status; int signo; TRACE_BEG1 (DEBUG_SYSIO, "_gpgme_io_spawn", path, "path=%s", path); i = 0; while (argv[i]) { TRACE_LOG2 ("argv[%2i] = %s", i, argv[i]); i++; } for (i = 0; fd_list[i].fd != -1; i++) if (fd_list[i].dup_to == -1) TRACE_LOG2 ("fd[%i] = 0x%x", i, fd_list[i].fd); else TRACE_LOG3 ("fd[%i] = 0x%x -> 0x%x", i, fd_list[i].fd, fd_list[i].dup_to); pid = fork (); if (pid == -1) return TRACE_SYSRES (-1); if (!pid) { /* Intermediate child to prevent zombie processes. */ if ((pid = fork ()) == 0) { /* Child. */ int max_fds = -1; int fd; int seen_stdin = 0; int seen_stdout = 0; int seen_stderr = 0; if (atfork) atfork (atforkvalue, 0); /* First close all fds which will not be inherited. If we * have closefrom(2) we first figure out the highest fd we * do not want to close, then call closefrom, and on success * use the regular code to close all fds up to the start * point of closefrom. Note that Solaris' and FreeBSD's closefrom do * not return errors. */ #ifdef HAVE_CLOSEFROM { fd = -1; for (i = 0; fd_list[i].fd != -1; i++) if (fd_list[i].fd > fd) fd = fd_list[i].fd; fd++; #if defined(__sun) || defined(__FreeBSD__) closefrom (fd); max_fds = fd; #else /*!__sun */ while ((i = closefrom (fd)) && errno == EINTR) ; if (!i || errno == EBADF) max_fds = fd; #endif /*!__sun*/ } #endif /*HAVE_CLOSEFROM*/ if (max_fds == -1) max_fds = get_max_fds (); for (fd = 0; fd < max_fds; fd++) { for (i = 0; fd_list[i].fd != -1; i++) if (fd_list[i].fd == fd) break; if (fd_list[i].fd == -1) close (fd); } /* And now dup and close those to be duplicated. */ for (i = 0; fd_list[i].fd != -1; i++) { int child_fd; int res; if (fd_list[i].dup_to != -1) child_fd = fd_list[i].dup_to; else child_fd = fd_list[i].fd; if (child_fd == 0) seen_stdin = 1; else if (child_fd == 1) seen_stdout = 1; else if (child_fd == 2) seen_stderr = 1; if (fd_list[i].dup_to == -1) continue; res = dup2 (fd_list[i].fd, fd_list[i].dup_to); if (res < 0) { #if 0 /* FIXME: The debug file descriptor is not dup'ed anyway, so we can't see this. */ TRACE_LOG1 ("dup2 failed in child: %s\n", strerror (errno)); #endif _exit (8); } close (fd_list[i].fd); } if (! seen_stdin || ! seen_stdout || !seen_stderr) { fd = open ("/dev/null", O_RDWR); if (fd == -1) { /* The debug file descriptor is not dup'ed, so we can't do a trace output. */ _exit (8); } /* Make sure that the process has connected stdin. */ if (! seen_stdin && fd != 0) { if (dup2 (fd, 0) == -1) _exit (8); } if (! seen_stdout && fd != 1) { if (dup2 (fd, 1) == -1) _exit (8); } if (! seen_stderr && fd != 2) { if (dup2 (fd, 2) == -1) _exit (8); } if (fd != 0 && fd != 1 && fd != 2) close (fd); } execv (path, (char *const *) argv); /* Hmm: in that case we could write a special status code to the status-pipe. */ _exit (8); /* End child. */ } if (pid == -1) _exit (1); else _exit (0); } TRACE_LOG1 ("waiting for child process pid=%i", pid); _gpgme_io_waitpid (pid, 1, &status, &signo); if (status) return TRACE_SYSRES (-1); for (i = 0; fd_list[i].fd != -1; i++) { if (! (flags & IOSPAWN_FLAG_NOCLOSE)) _gpgme_io_close (fd_list[i].fd); /* No handle translation. */ fd_list[i].peer_name = fd_list[i].fd; } if (r_pid) *r_pid = pid; return TRACE_SYSRES (0); } /* Select on the list of fds. Returns: -1 = error, 0 = timeout or nothing to select, > 0 = number of signaled fds. */ int _gpgme_io_select (struct io_select_fd_s *fds, size_t nfds, int nonblock) { fd_set readfds; fd_set writefds; unsigned int i; int any; int max_fd; int n; int count; /* Use a 1s timeout. */ struct timeval timeout = { 1, 0 }; void *dbg_help = NULL; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_select", fds, "nfds=%u, nonblock=%u", nfds, nonblock); FD_ZERO (&readfds); FD_ZERO (&writefds); max_fd = 0; if (nonblock) timeout.tv_sec = 0; TRACE_SEQ (dbg_help, "select on [ "); any = 0; for (i = 0; i < nfds; i++) { if (fds[i].fd == -1) continue; if (fds[i].for_read) { if (fds[i].fd >= FD_SETSIZE) { TRACE_END (dbg_help, " -BAD- ]"); gpg_err_set_errno (EMFILE); return TRACE_SYSRES (-1); } assert (!FD_ISSET (fds[i].fd, &readfds)); FD_SET (fds[i].fd, &readfds); if (fds[i].fd > max_fd) max_fd = fds[i].fd; TRACE_ADD1 (dbg_help, "r0x%x ", fds[i].fd); any = 1; } else if (fds[i].for_write) { if (fds[i].fd >= FD_SETSIZE) { TRACE_END (dbg_help, " -BAD- ]"); gpg_err_set_errno (EMFILE); return TRACE_SYSRES (-1); } assert (!FD_ISSET (fds[i].fd, &writefds)); FD_SET (fds[i].fd, &writefds); if (fds[i].fd > max_fd) max_fd = fds[i].fd; TRACE_ADD1 (dbg_help, "w0x%x ", fds[i].fd); any = 1; } fds[i].signaled = 0; } TRACE_END (dbg_help, "]"); if (!any) return TRACE_SYSRES (0); do { count = _gpgme_ath_select (max_fd + 1, &readfds, &writefds, NULL, &timeout); } while (count < 0 && errno == EINTR); if (count < 0) return TRACE_SYSRES (-1); TRACE_SEQ (dbg_help, "select OK [ "); if (TRACE_ENABLED (dbg_help)) { for (i = 0; i <= max_fd; i++) { if (FD_ISSET (i, &readfds)) TRACE_ADD1 (dbg_help, "r0x%x ", i); if (FD_ISSET (i, &writefds)) TRACE_ADD1 (dbg_help, "w0x%x ", i); } TRACE_END (dbg_help, "]"); } /* The variable N is used to optimize it a little bit. */ for (n = count, i = 0; i < nfds && n; i++) { if (fds[i].fd == -1) ; else if (fds[i].for_read) { if (FD_ISSET (fds[i].fd, &readfds)) { fds[i].signaled = 1; n--; } } else if (fds[i].for_write) { if (FD_ISSET (fds[i].fd, &writefds)) { fds[i].signaled = 1; n--; } } } return TRACE_SYSRES (count); } int _gpgme_io_recvmsg (int fd, struct msghdr *msg, int flags) { int nread; int saved_errno; struct iovec *iov; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_recvmsg", fd, "msg=%p, flags=%i", msg, flags); nread = 0; iov = msg->msg_iov; while (iov < msg->msg_iov + msg->msg_iovlen) { nread += iov->iov_len; iov++; } TRACE_LOG1 ("about to receive %d bytes", nread); do { nread = _gpgme_ath_recvmsg (fd, msg, flags); } while (nread == -1 && errno == EINTR); saved_errno = errno; if (nread > 0) { int nr = nread; iov = msg->msg_iov; while (nr > 0) { int len = nr > iov->iov_len ? iov->iov_len : nr; TRACE_LOGBUFX (msg->msg_iov->iov_base, len); iov++; nr -= len; } } errno = saved_errno; return TRACE_SYSRES (nread); } int _gpgme_io_sendmsg (int fd, const struct msghdr *msg, int flags) { int nwritten; struct iovec *iov; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_sendmsg", fd, "msg=%p, flags=%i", msg, flags); nwritten = 0; iov = msg->msg_iov; while (iov < msg->msg_iov + msg->msg_iovlen) { nwritten += iov->iov_len; iov++; } TRACE_LOG1 ("about to receive %d bytes", nwritten); iov = msg->msg_iov; while (nwritten > 0) { int len = nwritten > iov->iov_len ? iov->iov_len : nwritten; TRACE_LOGBUFX (msg->msg_iov->iov_base, len); iov++; nwritten -= len; } do { nwritten = _gpgme_ath_sendmsg (fd, msg, flags); } while (nwritten == -1 && errno == EINTR); return TRACE_SYSRES (nwritten); } int _gpgme_io_dup (int fd) { int new_fd; do new_fd = dup (fd); while (new_fd == -1 && errno == EINTR); TRACE1 (DEBUG_SYSIO, "_gpgme_io_dup", fd, "new fd==%i", new_fd); return new_fd; } int _gpgme_io_socket (int domain, int type, int proto) { int res; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_socket", domain, "type=%i, proto=%i", type, proto); res = socket (domain, type, proto); return TRACE_SYSRES (res); } int _gpgme_io_connect (int fd, struct sockaddr *addr, int addrlen) { int res; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_connect", fd, "addr=%p, addrlen=%i", addr, addrlen); do res = ath_connect (fd, addr, addrlen); while (res == -1 && errno == EINTR); return TRACE_SYSRES (res); } diff --git a/src/posix-util.c b/src/posix-util.c index 889c6aa5..d060c896 100644 --- a/src/posix-util.c +++ b/src/posix-util.c @@ -1,158 +1,158 @@ /* posix-util.c - Utility functions for Posix - Copyright (C) 2001 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2001 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "util.h" #include "sys-util.h" #include "debug.h" /* These variables store the malloced name of alternative default binaries. The are set only once by gpgme_set_global_flag. */ static char *default_gpg_name; static char *default_gpgconf_name; /* Set the default name for the gpg binary. This function may only be called by gpgme_set_global_flag. Returns 0 on success. Leading directories are removed from NAME. */ int _gpgme_set_default_gpg_name (const char *name) { const char *s; s = strrchr (name, '/'); if (s) name = s + 1; if (!default_gpg_name) default_gpg_name = strdup (name); return !default_gpg_name; } /* Set the default name for the gpgconf binary. This function may only be called by gpgme_set_global_flag. Returns 0 on success. Leading directories are removed from NAME. */ int _gpgme_set_default_gpgconf_name (const char *name) { const char *s; s = strrchr (name, '/'); if (s) name = s + 1; if (!default_gpgconf_name) default_gpgconf_name = strdup (name); return !default_gpgconf_name; } /* Dummy function - see w32-util.c for the actual code. */ int _gpgme_set_override_inst_dir (const char *dir) { (void)dir; return 0; } /* Find an executable program PGM along the envvar PATH. */ static char * walk_path (const char *pgm) { const char *orig_path, *path, *s; char *fname, *p; #ifdef FIXED_SEARCH_PATH orig_path = FIXED_SEARCH_PATH; #else orig_path = getenv ("PATH"); if (!orig_path) orig_path = "/bin:/usr/bin"; #endif fname = malloc (strlen (orig_path) + 1 + strlen (pgm) + 1); if (!fname) return NULL; path = orig_path; for (;;) { for (s=path, p=fname; *s && *s != ':'; s++, p++) *p = *s; if (p != fname && p[-1] != '/') *p++ = '/'; strcpy (p, pgm); if (!access (fname, X_OK)) return fname; if (!*s) break; path = s + 1; } _gpgme_debug (DEBUG_ENGINE, "gpgme-walk_path: '%s' not found in '%s'", pgm, orig_path); free (fname); return NULL; } /* Return the full file name of the GPG binary. This function is used if gpgconf was not found and thus it can be assumed that gpg2 is not installed. This function is only called by get_gpgconf_item and may not be called concurrently. */ char * _gpgme_get_gpg_path (void) { return walk_path (default_gpg_name? default_gpg_name : "gpg"); } /* This function is only called by get_gpgconf_item and may not be called concurrently. */ char * _gpgme_get_gpgconf_path (void) { return walk_path (default_gpgconf_name? default_gpgconf_name : "gpgconf"); } /* See w32-util.c */ int _gpgme_get_conf_int (const char *key, int *value) { (void)key; (void)value; return 0; } void _gpgme_allow_set_foreground_window (pid_t pid) { (void)pid; /* Not needed. */ } diff --git a/src/progress.c b/src/progress.c index 066a7f5d..e554caf1 100644 --- a/src/progress.c +++ b/src/progress.c @@ -1,90 +1,90 @@ /* progress.c - status handler for progress status - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include "util.h" #include "context.h" #include "debug.h" /* The status handler for progress status lines which also monitors * the PINENTRY_LAUNCHED status. */ gpgme_error_t _gpgme_progress_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; char *p; char *args_cpy; int type = 0; int current = 0; int total = 0; if (code == GPGME_STATUS_PINENTRY_LAUNCHED) { ctx->redraw_suggested = 1; return 0; } if (code != GPGME_STATUS_PROGRESS || !*args || !ctx->progress_cb) return 0; args_cpy = strdup (args); if (!args_cpy) return gpg_error_from_syserror (); p = strchr (args_cpy, ' '); if (p) { *p++ = 0; if (*p) { type = *(unsigned char *)p; p = strchr (p+1, ' '); if (p) { *p++ = 0; if (*p) { current = atoi (p); p = strchr (p+1, ' '); if (p) { *p++ = 0; total = atoi (p); } } } } } if (type != 'X') ctx->progress_cb (ctx->progress_cb_value, args_cpy, type, current, total); free (args_cpy); return 0; } diff --git a/src/putc_unlocked.c b/src/putc_unlocked.c index 6f046207..a0b90017 100644 --- a/src/putc_unlocked.c +++ b/src/putc_unlocked.c @@ -1,31 +1,31 @@ /* putc_unlocked.c - Replacement for putc_unlocked. - Copyright (C) 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include int putc_unlocked (int c, FILE *stream) { return putc (c, stream); } diff --git a/src/queryswdb.c b/src/queryswdb.c index 698a419b..537de39d 100644 --- a/src/queryswdb.c +++ b/src/queryswdb.c @@ -1,121 +1,122 @@ /* queryswdb.c - Access to the SWDB file * Copyright (C) 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" typedef struct { struct _gpgme_op_query_swdb_result result; } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; gpgme_query_swdb_result_t result = &opd->result; assert (!result->next); free (result->name); free (result->iversion); free (result->version); } gpgme_query_swdb_result_t gpgme_op_query_swdb_result (gpgme_ctx_t ctx) { void *hook; op_data_t opd; gpgme_error_t err; TRACE_BEG (DEBUG_CTX, "gpgme_op_query_swdb_result", ctx); err = _gpgme_op_data_lookup (ctx, OPDATA_QUERY_SWDB, &hook, -1, NULL); opd = hook; if (err || !opd) { TRACE_SUC0 ("result=(null)"); return NULL; } TRACE_SUC1 ("result=%p", &opd->result); return &opd->result; } /* Query the swdb for software package NAME and check against the * installed version given by IVERSION. If IVERSION is NULL a check * is only done if GPGME can figure out the version by itself * (e.g. for "gpgme" or "gnupg"). RESERVED should be 0. * * Note that we only implemented the synchronous variant of this * function but the API is prepared for an asynchronous variant. */ gpgme_error_t gpgme_op_query_swdb (gpgme_ctx_t ctx, const char *name, const char *iversion, unsigned int reserved) { gpgme_error_t err; void *hook; op_data_t opd; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_query_swdb", ctx, "name=%s, iversion=%a", name, iversion); if (!ctx || reserved) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (ctx->protocol != GPGME_PROTOCOL_GPGCONF) return TRACE_ERR (gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL)); if (!name) name = "gpgme"; if (!iversion && !strcmp (name, "gpgme")) iversion = VERSION; err = _gpgme_op_reset (ctx, 1); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_QUERY_SWDB, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return TRACE_ERR (err); err = _gpgme_engine_op_query_swdb (ctx->engine, name, iversion, &opd->result); return TRACE_ERR (err); } diff --git a/src/setenv.c b/src/setenv.c index d85bec9c..bfabc870 100644 --- a/src/setenv.c +++ b/src/setenv.c @@ -1,354 +1,354 @@ /* Copyright (C) 1992,1995-2001,2004 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02110-1301 USA. */ + * This file is part of the GNU C Library. + * + * The GNU C Library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * The GNU C Library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H # include #endif #include #include #define __set_errno(ev) (gpg_err_set_errno (ev)) #if HAVE_ASSUAN_H /* Fixme: Why do we need to include the assuan header and why the internal ones? */ #include "assuan-defs.h" #endif /*HAVE_ASSUAN_H*/ #define __builtin_expect(cond,val) (cond) #include #if _LIBC || HAVE_STDLIB_H # include #endif #if _LIBC || HAVE_STRING_H # include #endif #if _LIBC || HAVE_UNISTD_H # include #endif #if !_LIBC # define __environ environ # ifndef HAVE_ENVIRON_DECL extern char **environ; # endif #endif #if _LIBC /* This lock protects against simultaneous modifications of `environ'. */ # include __libc_lock_define_initialized (static, envlock) # define LOCK __libc_lock_lock (envlock) # define UNLOCK __libc_lock_unlock (envlock) #else # define LOCK # define UNLOCK #endif /* In the GNU C library we must keep the namespace clean. */ #ifdef _LIBC # define setenv __setenv # define unsetenv __unsetenv # define clearenv __clearenv # define tfind __tfind # define tsearch __tsearch #endif /* In the GNU C library implementation we try to be more clever and allow arbitrarily many changes of the environment given that the used values are from a small set. Outside glibc this will eat up all memory after a while. */ #if defined _LIBC || (defined HAVE_SEARCH_H && defined HAVE_TSEARCH \ && defined __GNUC__) # define USE_TSEARCH 1 # include /* This is a pointer to the root of the search tree with the known values. */ static void *known_values; # define KNOWN_VALUE(Str) \ ({ \ void *value = tfind (Str, &known_values, (__compar_fn_t) strcmp); \ value != NULL ? *(char **) value : NULL; \ }) # define STORE_VALUE(Str) \ tsearch (Str, &known_values, (__compar_fn_t) strcmp) #else # undef USE_TSEARCH # define KNOWN_VALUE(Str) NULL # define STORE_VALUE(Str) do { } while (0) #endif /* If this variable is not a null pointer we allocated the current environment. */ static char **last_environ; /* This function is used by `setenv' and `putenv'. The difference between the two functions is that for the former must create a new string which is then placed in the environment, while the argument of `putenv' must be used directly. This is all complicated by the fact that we try to reuse values once generated for a `setenv' call since we can never free the strings. */ static int __add_to_environ (const char *name, const char *value, const char *combined, int replace) { register char **ep; register size_t size; const size_t namelen = strlen (name); const size_t vallen = value != NULL ? strlen (value) + 1 : 0; LOCK; /* We have to get the pointer now that we have the lock and not earlier since another thread might have created a new environment. */ ep = __environ; size = 0; if (ep != NULL) { for (; *ep != NULL; ++ep) if (!strncmp (*ep, name, namelen) && (*ep)[namelen] == '=') break; else ++size; } if (ep == NULL || __builtin_expect (*ep == NULL, 1)) { char **new_environ; /* We allocated this space; we can extend it. */ new_environ = (char **) realloc (last_environ, (size + 2) * sizeof (char *)); if (new_environ == NULL) { UNLOCK; return -1; } /* If the whole entry is given add it. */ if (combined != NULL) /* We must not add the string to the search tree since it belongs to the user. */ new_environ[size] = (char *) combined; else { /* See whether the value is already known. */ #ifdef USE_TSEARCH # ifdef __GNUC__ char new_value[namelen + 1 + vallen]; # else char *new_value = (char *) alloca (namelen + 1 + vallen); # endif # ifdef _LIBC __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1), value, vallen); # else memcpy (new_value, name, namelen); new_value[namelen] = '='; memcpy (&new_value[namelen + 1], value, vallen); # endif new_environ[size] = KNOWN_VALUE (new_value); if (__builtin_expect (new_environ[size] == NULL, 1)) #endif { new_environ[size] = (char *) malloc (namelen + 1 + vallen); if (__builtin_expect (new_environ[size] == NULL, 0)) { __set_errno (ENOMEM); UNLOCK; return -1; } #ifdef USE_TSEARCH memcpy (new_environ[size], new_value, namelen + 1 + vallen); #else memcpy (new_environ[size], name, namelen); new_environ[size][namelen] = '='; memcpy (&new_environ[size][namelen + 1], value, vallen); #endif /* And save the value now. We cannot do this when we remove the string since then we cannot decide whether it is a user string or not. */ STORE_VALUE (new_environ[size]); } } if (__environ != last_environ) memcpy ((char *) new_environ, (char *) __environ, size * sizeof (char *)); new_environ[size + 1] = NULL; last_environ = __environ = new_environ; } else if (replace) { char *np; /* Use the user string if given. */ if (combined != NULL) np = (char *) combined; else { #ifdef USE_TSEARCH # ifdef __GNUC__ char new_value[namelen + 1 + vallen]; # else char *new_value = (char *) alloca (namelen + 1 + vallen); # endif # ifdef _LIBC __mempcpy (__mempcpy (__mempcpy (new_value, name, namelen), "=", 1), value, vallen); # else memcpy (new_value, name, namelen); new_value[namelen] = '='; memcpy (&new_value[namelen + 1], value, vallen); # endif np = KNOWN_VALUE (new_value); if (__builtin_expect (np == NULL, 1)) #endif { np = malloc (namelen + 1 + vallen); if (__builtin_expect (np == NULL, 0)) { UNLOCK; return -1; } #ifdef USE_TSEARCH memcpy (np, new_value, namelen + 1 + vallen); #else memcpy (np, name, namelen); np[namelen] = '='; memcpy (&np[namelen + 1], value, vallen); #endif /* And remember the value. */ STORE_VALUE (np); } } *ep = np; } UNLOCK; return 0; } int setenv (const char *name, const char *value, int replace) { if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) { __set_errno (EINVAL); return -1; } return __add_to_environ (name, value, NULL, replace); } int unsetenv (const char *name) { size_t len; char **ep; if (name == NULL || *name == '\0' || strchr (name, '=') != NULL) { __set_errno (EINVAL); return -1; } len = strlen (name); LOCK; ep = __environ; while (*ep != NULL) if (!strncmp (*ep, name, len) && (*ep)[len] == '=') { /* Found it. Remove this pointer by moving later ones back. */ char **dp = ep; do dp[0] = dp[1]; while (*dp++); /* Continue the loop in case NAME appears again. */ } else ++ep; UNLOCK; return 0; } /* The `clearenv' was planned to be added to POSIX.1 but probably never made it. Nevertheless the POSIX.9 standard (POSIX bindings for Fortran 77) requires this function. */ int clearenv (void) { LOCK; if (__environ == last_environ && __environ != NULL) { /* We allocated this environment so we can free it. */ free (__environ); last_environ = NULL; } /* Clear the environment pointer removes the whole environment. */ __environ = NULL; UNLOCK; return 0; } #ifdef _LIBC libc_freeres_fn (free_mem) { /* Remove all traces. */ clearenv (); /* Now remove the search tree. */ __tdestroy (known_values, free); known_values = NULL; } # undef setenv # undef unsetenv # undef clearenv weak_alias (__setenv, setenv) weak_alias (__unsetenv, unsetenv) weak_alias (__clearenv, clearenv) #endif diff --git a/src/sig-notation.c b/src/sig-notation.c index c747ad62..bebf426e 100644 --- a/src/sig-notation.c +++ b/src/sig-notation.c @@ -1,261 +1,261 @@ /* sig-notation.c - Signature notation data support. - Copyright (C) 2005 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2005 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include "gpgme.h" #include "util.h" #include "context.h" #include "ops.h" #include "debug.h" /* Free the signature notation object and all associated resources. The object must already be removed from any linked list as the next pointer is ignored. */ void _gpgme_sig_notation_free (gpgme_sig_notation_t notation) { if (notation->name) free (notation->name); if (notation->value) free (notation->value); free (notation); } /* Set the flags of NOTATION to FLAGS. */ static void sig_notation_set_flags (gpgme_sig_notation_t notation, gpgme_sig_notation_flags_t flags) { /* We copy the flags into individual bits to make them easier accessible individually for the user. */ notation->human_readable = flags & GPGME_SIG_NOTATION_HUMAN_READABLE ? 1 : 0; notation->critical = flags & GPGME_SIG_NOTATION_CRITICAL ? 1 : 0; notation->flags = flags; } /* Create a new, empty signature notation data object. */ gpgme_error_t _gpgme_sig_notation_create (gpgme_sig_notation_t *notationp, const char *name, int name_len, const char *value, int value_len, gpgme_sig_notation_flags_t flags) { gpgme_error_t err = 0; gpgme_sig_notation_t notation; /* Currently, we require all notations to be human-readable. */ if (name && !(flags & GPGME_SIG_NOTATION_HUMAN_READABLE)) return gpg_error (GPG_ERR_INV_VALUE); notation = calloc (1, sizeof (*notation)); if (!notation) return gpg_error_from_syserror (); /* This is critical. We want to reliably identify policy URLs by using a NULL pointer for NAME. So all notations must have a NAME string, even if it is empty. */ if (name) { /* We add a trailing '\0' for stringification in the good case. */ notation->name = malloc (name_len + 1); if (!notation->name) { err = gpg_error_from_syserror (); goto err; } memcpy (notation->name, name, name_len); notation->name[name_len] = '\0'; notation->name_len = name_len; } if (value) { /* We add a trailing '\0' for stringification in the good case. */ notation->value = malloc (value_len + 1); if (!notation->value) { err = gpg_error_from_syserror (); goto err; } memcpy (notation->value, value, value_len); notation->value[value_len] = '\0'; notation->value_len = value_len; } sig_notation_set_flags (notation, flags); *notationp = notation; return 0; err: _gpgme_sig_notation_free (notation); return err; } /* GnuPG subpacket flags. */ /* This subpacket data is part of the hashed data. */ #define GNUPG_SPK_HASHED 0x01 /* This subpacket is marked critical. */ #define GNUPG_SPK_CRITICAL 0x02 /* Parse a notation or policy URL subpacket. If the packet type is not known, return no error but NULL in NOTATION. */ gpgme_error_t _gpgme_parse_notation (gpgme_sig_notation_t *notationp, int type, int pkflags, int len, char *data) { gpgme_error_t err; char *name = NULL; int name_len = 0; char *value = NULL; int value_len = 0; gpgme_sig_notation_flags_t flags = 0; char *decoded_data; unsigned char *bdata; /* Type 20: Notation data. */ /* Type 26: Policy URL. */ if (type != 20 && type != 26) { *notationp = NULL; return 0; } /* A few simple sanity checks. */ if (len > strlen (data)) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* See below for the format of a notation subpacket. It has at least four octets of flags and two times two octets of length information. */ if (type == 20 && len < 4 + 2 + 2) return trace_gpg_error (GPG_ERR_INV_ENGINE); err = _gpgme_decode_percent_string (data, &decoded_data, 0, 1); if (err) return err; bdata = (unsigned char *) decoded_data; /* Flags common to notation data and policy URL. */ if (pkflags & GNUPG_SPK_CRITICAL) flags |= GPGME_SIG_NOTATION_CRITICAL; /* This information is relevant in parsing multi-octet numbers below: 3.1. Scalar numbers Scalar numbers are unsigned, and are always stored in big-endian format. Using n[k] to refer to the kth octet being interpreted, the value of a two-octet scalar is ((n[0] << 8) + n[1]). The value of a four-octet scalar is ((n[0] << 24) + (n[1] << 16) + (n[2] << 8) + n[3]). From RFC2440: OpenPGP Message Format. Copyright (C) The Internet Society (1998). All Rights Reserved. */ #define RFC2440_GET_WORD(chr) ((((int)((unsigned char *)(chr))[0]) << 8) \ + ((int)((unsigned char *)(chr))[1])) if (type == 20) { /* 5.2.3.15. Notation Data (4 octets of flags, 2 octets of name length (M), 2 octets of value length (N), M octets of name data, N octets of value data) [...] The "flags" field holds four octets of flags. All undefined flags MUST be zero. Defined flags are: First octet: 0x80 = human-readable. [...] Other octets: none. From RFC2440: OpenPGP Message Format. Copyright (C) The Internet Society (1998). All Rights Reserved. */ int chr; /* First octet of flags. */ #define RFC2440_SPK20_FLAG1_HUMAN_READABLE 0x80 chr = *bdata; bdata++; if (chr & RFC2440_SPK20_FLAG1_HUMAN_READABLE) flags |= GPGME_SIG_NOTATION_HUMAN_READABLE; /* The second, third and four octet of flags are unused. */ bdata++; bdata++; bdata++; name_len = RFC2440_GET_WORD (bdata); bdata += 2; value_len = RFC2440_GET_WORD (bdata); bdata += 2; /* Small sanity check. */ if (4 + 2 + 2 + name_len + value_len > len) { free (decoded_data); return trace_gpg_error (GPG_ERR_INV_ENGINE); } name = (char *) bdata; bdata += name_len; value = (char *) bdata; } else { /* Type is 26. */ /* NAME is NULL, name_len is 0. */ value = (char *) bdata; value_len = strlen (value); } err = _gpgme_sig_notation_create (notationp, name, name_len, value, value_len, flags); free (decoded_data); return err; } diff --git a/src/sign.c b/src/sign.c index 73a38039..49eac44c 100644 --- a/src/sign.c +++ b/src/sign.c @@ -1,500 +1,500 @@ /* sign.c - Signing function. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include /* Suppress warning for accessing deprecated member "class". */ #define _GPGME_IN_GPGME 1 #include "gpgme.h" #include "context.h" #include "ops.h" #include "util.h" #include "debug.h" typedef struct { struct _gpgme_op_sign_result result; /* The error code from a FAILURE status line or 0. */ gpg_error_t failure_code; /* The fingerprint from the last KEY_CONSIDERED status line. */ char *kc_fpr; /* The flags from the last KEY_CONSIDERED status line. */ unsigned int kc_flags; /* A pointer to the next pointer of the last invalid signer in the list. This makes appending new invalid signers painless while preserving the order. */ gpgme_invalid_key_t *last_signer_p; /* Likewise for signature information. */ gpgme_new_signature_t *last_sig_p; /* Flags used while processing the status lines. */ unsigned int ignore_inv_recp:1; unsigned int inv_sgnr_seen:1; unsigned int sig_created_seen:1; } *op_data_t; static void release_signatures (gpgme_new_signature_t sig) { while (sig) { gpgme_new_signature_t next = sig->next; free (sig->fpr); free (sig); sig = next; } } static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; gpgme_invalid_key_t invalid_signer = opd->result.invalid_signers; while (invalid_signer) { gpgme_invalid_key_t next = invalid_signer->next; if (invalid_signer->fpr) free (invalid_signer->fpr); free (invalid_signer); invalid_signer = next; } release_signatures (opd->result.signatures); free (opd->kc_fpr); } gpgme_sign_result_t gpgme_op_sign_result (gpgme_ctx_t ctx) { void *hook; op_data_t opd; gpgme_error_t err; gpgme_invalid_key_t inv_key, key; gpgme_new_signature_t sig; unsigned int inv_signers = 0; unsigned int signatures = 0; TRACE_BEG (DEBUG_CTX, "gpgme_op_sign_result", ctx); err = _gpgme_op_data_lookup (ctx, OPDATA_SIGN, &hook, -1, NULL); opd = hook; if (err || !opd) { TRACE_SUC0 ("result=(null)"); return NULL; } for (inv_key = opd->result.invalid_signers; inv_key; inv_key = inv_key->next) inv_signers++; for (sig = opd->result.signatures; sig; sig = sig->next) signatures++; if (gpgme_signers_count (ctx) && signatures + inv_signers != gpgme_signers_count (ctx)) { /* In this case at least one signatures was not created perhaps due to a bad passphrase etc. Thus the entire message is broken and should not be used. We add the already created signatures to the invalid signers list and thus this case can be detected. */ TRACE_LOG3 ("result: invalid signers: %u, signatures: %u, count: %u", inv_signers, signatures, gpgme_signers_count (ctx)); for (sig = opd->result.signatures; sig; sig = sig->next) { key = calloc (1, sizeof *key); if (!key) { TRACE_SUC0 ("out of core; result=(null)"); return NULL; } if (sig->fpr) { key->fpr = strdup (sig->fpr); if (!key->fpr) { free (key); TRACE_SUC0 ("out of core; result=(null)"); return NULL; } } key->reason = GPG_ERR_GENERAL; inv_key = opd->result.invalid_signers; if (inv_key) { for (; inv_key->next; inv_key = inv_key->next) ; inv_key->next = key; } else opd->result.invalid_signers = key; } release_signatures (opd->result.signatures); opd->result.signatures = NULL; } if (_gpgme_debug_trace()) { TRACE_LOG2 ("result: invalid signers: %i, signatures: %i", inv_signers, signatures); for (inv_key=opd->result.invalid_signers; inv_key; inv_key=inv_key->next) { TRACE_LOG3 ("result: invalid signer: fpr=%s, reason=%s <%s>", inv_key->fpr, gpgme_strerror (inv_key->reason), gpgme_strsource (inv_key->reason)); } for (sig = opd->result.signatures; sig; sig = sig->next) { TRACE_LOG6 ("result: signature: type=%i, pubkey_algo=%i, " "hash_algo=%i, timestamp=%li, fpr=%s, sig_class=%i", sig->type, sig->pubkey_algo, sig->hash_algo, sig->timestamp, sig->fpr, sig->sig_class); } } TRACE_SUC1 ("result=%p", &opd->result); return &opd->result; } static gpgme_error_t parse_sig_created (char *args, gpgme_new_signature_t *sigp, gpgme_protocol_t protocol) { gpgme_new_signature_t sig; char *tail; sig = malloc (sizeof (*sig)); if (!sig) return gpg_error_from_syserror (); sig->next = NULL; switch (*args) { case 'S': sig->type = GPGME_SIG_MODE_NORMAL; break; case 'D': sig->type = GPGME_SIG_MODE_DETACH; break; case 'C': sig->type = GPGME_SIG_MODE_CLEAR; break; default: /* The backend engine is not behaving. */ free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args++; if (*args != ' ') { free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } gpg_err_set_errno (0); sig->pubkey_algo = _gpgme_map_pk_algo (strtol (args, &tail, 0), protocol); if (errno || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args = tail; sig->hash_algo = strtol (args, &tail, 0); if (errno || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args = tail; sig->sig_class = strtol (args, &tail, 0); sig->class = sig->sig_class; sig->_obsolete_class = sig->sig_class; if (errno || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args = tail; sig->timestamp = _gpgme_parse_timestamp (args, &tail); if (sig->timestamp == -1 || args == tail || *tail != ' ') { /* The crypto backend does not behave. */ free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } args = tail; while (*args == ' ') args++; if (!*args) { /* The crypto backend does not behave. */ free (sig); return trace_gpg_error (GPG_ERR_INV_ENGINE); } tail = strchr (args, ' '); if (tail) *tail = '\0'; sig->fpr = strdup (args); if (!sig->fpr) { free (sig); return gpg_error_from_syserror (); } *sigp = sig; return 0; } gpgme_error_t _gpgme_sign_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_passphrase_status_handler (priv, code, args); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_SIGN, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_SIG_CREATED: opd->sig_created_seen = 1; err = parse_sig_created (args, opd->last_sig_p, ctx->protocol); if (err) return err; opd->last_sig_p = &(*opd->last_sig_p)->next; break; case GPGME_STATUS_KEY_CONSIDERED: /* This is emitted during gpg's key lookup to give information * about the lookup results. We store the last one so it can be * used in connection with INV_RECP. */ free (opd->kc_fpr); opd->kc_fpr = NULL; err = _gpgme_parse_key_considered (args, &opd->kc_fpr, &opd->kc_flags); if (err) return err; break; case GPGME_STATUS_INV_RECP: if (opd->inv_sgnr_seen && opd->ignore_inv_recp) break; /* FALLTHROUGH */ case GPGME_STATUS_INV_SGNR: if (code == GPGME_STATUS_INV_SGNR) opd->inv_sgnr_seen = 1; free (opd->kc_fpr); opd->kc_fpr = NULL; err = _gpgme_parse_inv_recp (args, 1, opd->kc_fpr, opd->kc_flags, opd->last_signer_p); if (err) return err; opd->last_signer_p = &(*opd->last_signer_p)->next; free (opd->kc_fpr); opd->kc_fpr = NULL; break; case GPGME_STATUS_FAILURE: opd->failure_code = _gpgme_parse_failure (args); break; case GPGME_STATUS_EOF: /* The UI server does not send information about the created signature. This is irrelevant for this protocol and thus we should not check for that. */ if (opd->result.invalid_signers) err = gpg_error (GPG_ERR_UNUSABLE_SECKEY); else if (!opd->sig_created_seen && ctx->protocol != GPGME_PROTOCOL_UISERVER) err = opd->failure_code? opd->failure_code:gpg_error (GPG_ERR_GENERAL); break; case GPGME_STATUS_INQUIRE_MAXLEN: if (ctx->status_cb && !ctx->full_status) err = ctx->status_cb (ctx->status_cb_value, "INQUIRE_MAXLEN", args); break; default: break; } return err; } static gpgme_error_t sign_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_sign_status_handler (priv, code, args); return err; } static gpgme_error_t sign_init_result (gpgme_ctx_t ctx, int ignore_inv_recp) { gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_SIGN, &hook, sizeof (*opd), release_op_data); opd = hook; if (err) return err; opd->failure_code = 0; opd->last_signer_p = &opd->result.invalid_signers; opd->last_sig_p = &opd->result.signatures; opd->ignore_inv_recp = !!ignore_inv_recp; opd->inv_sgnr_seen = 0; opd->sig_created_seen = 0; return 0; } gpgme_error_t _gpgme_op_sign_init_result (gpgme_ctx_t ctx) { return sign_init_result (ctx, 0); } static gpgme_error_t sign_start (gpgme_ctx_t ctx, int synchronous, gpgme_data_t plain, gpgme_data_t sig, gpgme_sig_mode_t mode) { gpgme_error_t err; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; /* If we are using the CMS protocol, we ignore the INV_RECP status code if a newer GPGSM is in use. GPGMS does not support combined sign+encrypt and thus this can't harm. */ err = sign_init_result (ctx, (ctx->protocol == GPGME_PROTOCOL_CMS)); if (err) return err; if (mode != GPGME_SIG_MODE_NORMAL && mode != GPGME_SIG_MODE_DETACH && mode != GPGME_SIG_MODE_CLEAR) return gpg_error (GPG_ERR_INV_VALUE); if (!plain) return gpg_error (GPG_ERR_NO_DATA); if (!sig) return gpg_error (GPG_ERR_INV_VALUE); if (ctx->passphrase_cb) { err = _gpgme_engine_set_command_handler (ctx->engine, _gpgme_passphrase_command_handler, ctx); if (err) return err; } _gpgme_engine_set_status_handler (ctx->engine, sign_status_handler, ctx); return _gpgme_engine_op_sign (ctx->engine, plain, sig, mode, ctx->use_armor, ctx->use_textmode, ctx->include_certs, ctx /* FIXME */); } /* Sign the plaintext PLAIN and store the signature in SIG. */ gpgme_error_t gpgme_op_sign_start (gpgme_ctx_t ctx, gpgme_data_t plain, gpgme_data_t sig, gpgme_sig_mode_t mode) { gpg_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_sign_start", ctx, "plain=%p, sig=%p, mode=%i", plain, sig, mode); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = sign_start (ctx, 0, plain, sig, mode); return TRACE_ERR (err); } /* Sign the plaintext PLAIN and store the signature in SIG. */ gpgme_error_t gpgme_op_sign (gpgme_ctx_t ctx, gpgme_data_t plain, gpgme_data_t sig, gpgme_sig_mode_t mode) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_sign", ctx, "plain=%p, sig=%p, mode=%i", plain, sig, mode); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = sign_start (ctx, 1, plain, sig, mode); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } diff --git a/src/signers.c b/src/signers.c index f540d700..fe2d6786 100644 --- a/src/signers.c +++ b/src/signers.c @@ -1,118 +1,118 @@ /* signers.c - Maintain signer sets. - Copyright (C) 2001 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2001 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include "gpgme.h" #include "util.h" #include "context.h" #include "debug.h" /* Delete all signers from CTX. */ void _gpgme_signers_clear (gpgme_ctx_t ctx) { unsigned int i; if (!ctx || !ctx->signers) return; for (i = 0; i < ctx->signers_len; i++) { assert (ctx->signers[i]); gpgme_key_unref (ctx->signers[i]); ctx->signers[i] = NULL; } ctx->signers_len = 0; } void gpgme_signers_clear (gpgme_ctx_t ctx) { TRACE (DEBUG_CTX, "gpgme_signers_clear", ctx); _gpgme_signers_clear (ctx); } /* Add KEY to list of signers in CTX. */ gpgme_error_t gpgme_signers_add (gpgme_ctx_t ctx, const gpgme_key_t key) { TRACE_BEG2 (DEBUG_CTX, "gpgme_signers_add", ctx, "key=%p (%s)", key, (key && key->subkeys && key->subkeys->fpr) ? key->subkeys->fpr : "invalid"); if (!ctx || !key) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (ctx->signers_len == ctx->signers_size) { gpgme_key_t *newarr; int n = ctx->signers_size + 5; int j; newarr = realloc (ctx->signers, n * sizeof (*newarr)); if (!newarr) return TRACE_ERR (gpg_error_from_syserror ()); for (j = ctx->signers_size; j < n; j++) newarr[j] = NULL; ctx->signers = newarr; ctx->signers_size = n; } gpgme_key_ref (key); ctx->signers[ctx->signers_len++] = key; return TRACE_SUC (); } /* Return the number of signers in CTX. */ unsigned int gpgme_signers_count (const gpgme_ctx_t ctx) { return ctx? ctx->signers_len : 0; } /* Return the SEQth signer's key in CTX with one reference. */ gpgme_key_t gpgme_signers_enum (const gpgme_ctx_t ctx, int seq) { unsigned int seqno; if (!ctx || seq < 0) return NULL; seqno = (unsigned int) seq; if (seqno >= ctx->signers_len) return NULL; gpgme_key_ref (ctx->signers[seqno]); return ctx->signers[seqno]; } diff --git a/src/spawn.c b/src/spawn.c index 7b3b4476..a4d5e8de 100644 --- a/src/spawn.c +++ b/src/spawn.c @@ -1,106 +1,106 @@ /* spawn.c - Run an arbitrary command with callbacks. - Copyright (C) 2014 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2014 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "util.h" #include "ops.h" static gpgme_error_t spawn_start (gpgme_ctx_t ctx, int synchronous, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags) { gpgme_error_t err; const char *tmp_argv[2]; if (ctx->protocol != GPGME_PROTOCOL_SPAWN) return gpg_error (GPG_ERR_UNSUPPORTED_PROTOCOL); err = _gpgme_op_reset (ctx, synchronous); if (err) return err; if (!argv) { tmp_argv[0] = _gpgme_get_basename (file); tmp_argv[1] = NULL; argv = tmp_argv; } return _gpgme_engine_op_spawn (ctx->engine, file, argv, datain, dataout, dataerr, flags); } /* Run the command FILE with the arguments in ARGV. Connect stdin to DATAIN, stdout to DATAOUT, and STDERR to DATAERR. If one the data streams is NULL, connect to /dev/null instead. */ gpgme_error_t gpgme_op_spawn_start (gpgme_ctx_t ctx, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_spawn_start", ctx, "file=(%s) flaggs=%x", file, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = spawn_start (ctx, 0, file, argv, datain, dataout, dataerr, flags); return err; } /* Run the command FILE with the arguments in ARGV. Connect stdin to DATAIN, stdout to DATAOUT, and STDERR to DATAERR. If one the data streams is NULL, connect to /dev/null instead. Synchronous variant. */ gpgme_error_t gpgme_op_spawn (gpgme_ctx_t ctx, const char *file, const char *argv[], gpgme_data_t datain, gpgme_data_t dataout, gpgme_data_t dataerr, unsigned int flags) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_spawn", ctx, "file=(%s) flags=%x", file, flags); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = spawn_start (ctx, 1, file, argv, datain, dataout, dataerr, flags); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } diff --git a/src/status-table.c b/src/status-table.c index afc7eab8..eeafb896 100644 --- a/src/status-table.c +++ b/src/status-table.c @@ -1,185 +1,185 @@ -/* gpgme.c - GnuPG Made Easy. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2012 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ +/* status-table.c - Status codes from gnupg. + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2012 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include "util.h" struct status_table_s { const char *name; gpgme_status_code_t code; }; /* Lexicographically sorted ('_' comes after any letter). You can use the Emacs command M-x sort-lines. But don't sweat it, the table is sorted at start up, too. */ static struct status_table_s status_table[] = { { "ABORT", GPGME_STATUS_ABORT }, { "ALREADY_SIGNED", GPGME_STATUS_ALREADY_SIGNED }, { "ATTRIBUTE", GPGME_STATUS_ATTRIBUTE }, { "BACKUP_KEY_CREATED", GPGME_STATUS_BACKUP_KEY_CREATED }, { "BADARMOR", GPGME_STATUS_BADARMOR }, { "BADMDC", GPGME_STATUS_BADMDC }, { "BADSIG", GPGME_STATUS_BADSIG }, { "BAD_PASSPHRASE", GPGME_STATUS_BAD_PASSPHRASE }, { "BEGIN_DECRYPTION", GPGME_STATUS_BEGIN_DECRYPTION }, { "BEGIN_ENCRYPTION", GPGME_STATUS_BEGIN_ENCRYPTION }, { "BEGIN_SIGNING", GPGME_STATUS_BEGIN_SIGNING }, { "BEGIN_STREAM", GPGME_STATUS_BEGIN_STREAM }, { "CARDCTRL", GPGME_STATUS_CARDCTRL }, { "DECRYPTION_FAILED", GPGME_STATUS_DECRYPTION_FAILED }, { "DECRYPTION_INFO", GPGME_STATUS_DECRYPTION_INFO }, { "DECRYPTION_OKAY", GPGME_STATUS_DECRYPTION_OKAY }, { "DELETE_PROBLEM", GPGME_STATUS_DELETE_PROBLEM }, { "DECRYPTION_COMPLIANCE_MODE", GPGME_STATUS_DECRYPTION_COMPLIANCE_MODE }, { "ENC_TO", GPGME_STATUS_ENC_TO }, { "END_DECRYPTION", GPGME_STATUS_END_DECRYPTION }, { "END_ENCRYPTION", GPGME_STATUS_END_ENCRYPTION }, { "END_STREAM", GPGME_STATUS_END_STREAM }, { "ENTER", GPGME_STATUS_ENTER }, { "ERRMDC", GPGME_STATUS_ERRMDC }, { "ERROR", GPGME_STATUS_ERROR }, { "ERRSIG", GPGME_STATUS_ERRSIG }, { "EXPKEYSIG", GPGME_STATUS_EXPKEYSIG }, { "EXPSIG", GPGME_STATUS_EXPSIG }, { "FAILURE", GPGME_STATUS_FAILURE }, { "FILE_DONE", GPGME_STATUS_FILE_DONE }, { "FILE_ERROR", GPGME_STATUS_FILE_ERROR }, { "FILE_START", GPGME_STATUS_FILE_START }, { "GET_BOOL", GPGME_STATUS_GET_BOOL }, { "GET_HIDDEN", GPGME_STATUS_GET_HIDDEN }, { "GET_LINE", GPGME_STATUS_GET_LINE }, { "GOODMDC", GPGME_STATUS_GOODMDC }, { "GOODSIG", GPGME_STATUS_GOODSIG }, { "GOOD_PASSPHRASE", GPGME_STATUS_GOOD_PASSPHRASE }, { "GOT_IT", GPGME_STATUS_GOT_IT }, { "IMPORTED", GPGME_STATUS_IMPORTED }, { "IMPORT_OK", GPGME_STATUS_IMPORT_OK }, { "IMPORT_PROBLEM", GPGME_STATUS_IMPORT_PROBLEM }, { "IMPORT_RES", GPGME_STATUS_IMPORT_RES }, { "INQUIRE_MAXLEN", GPGME_STATUS_INQUIRE_MAXLEN }, { "INV_RECP", GPGME_STATUS_INV_RECP }, { "INV_SGNR", GPGME_STATUS_INV_SGNR }, { "KEYEXPIRED", GPGME_STATUS_KEYEXPIRED }, { "KEYREVOKED", GPGME_STATUS_KEYREVOKED }, { "KEY_CONSIDERED", GPGME_STATUS_KEY_CONSIDERED }, { "KEY_CREATED", GPGME_STATUS_KEY_CREATED }, { "KEY_NOT_CREATED", GPGME_STATUS_KEY_NOT_CREATED }, { "LEAVE", GPGME_STATUS_LEAVE }, { "MISSING_PASSPHRASE", GPGME_STATUS_MISSING_PASSPHRASE }, { "MOUNTPOINT", GPGME_STATUS_MOUNTPOINT }, { "NEED_PASSPHRASE", GPGME_STATUS_NEED_PASSPHRASE }, { "NEED_PASSPHRASE_PIN", GPGME_STATUS_NEED_PASSPHRASE_PIN }, { "NEED_PASSPHRASE_SYM", GPGME_STATUS_NEED_PASSPHRASE_SYM }, { "NEWSIG", GPGME_STATUS_NEWSIG }, { "NODATA", GPGME_STATUS_NODATA }, { "NOTATION_DATA", GPGME_STATUS_NOTATION_DATA }, { "NOTATION_FLAGS", GPGME_STATUS_NOTATION_FLAGS }, { "NOTATION_NAME", GPGME_STATUS_NOTATION_NAME }, { "NO_PUBKEY", GPGME_STATUS_NO_PUBKEY }, { "NO_RECP", GPGME_STATUS_NO_RECP }, { "NO_SECKEY", GPGME_STATUS_NO_SECKEY }, { "NO_SGNR", GPGME_STATUS_NO_SGNR }, { "PINENTRY_LAUNCHED", GPGME_STATUS_PINENTRY_LAUNCHED}, { "PKA_TRUST_BAD", GPGME_STATUS_PKA_TRUST_BAD }, { "PKA_TRUST_GOOD", GPGME_STATUS_PKA_TRUST_GOOD }, { "PLAINTEXT", GPGME_STATUS_PLAINTEXT }, { "PLAINTEXT_LENGTH", GPGME_STATUS_PLAINTEXT_LENGTH }, { "POLICY_URL", GPGME_STATUS_POLICY_URL }, { "PROGRESS", GPGME_STATUS_PROGRESS }, { "REVKEYSIG", GPGME_STATUS_REVKEYSIG }, { "RSA_OR_IDEA", GPGME_STATUS_RSA_OR_IDEA }, { "SC_OP_FAILURE", GPGME_STATUS_SC_OP_FAILURE }, { "SC_OP_SUCCESS", GPGME_STATUS_SC_OP_SUCCESS }, { "SESSION_KEY", GPGME_STATUS_SESSION_KEY }, { "SHM_GET", GPGME_STATUS_SHM_GET }, { "SHM_GET_BOOL", GPGME_STATUS_SHM_GET_BOOL }, { "SHM_GET_HIDDEN", GPGME_STATUS_SHM_GET_HIDDEN }, { "SHM_INFO", GPGME_STATUS_SHM_INFO }, { "SIGEXPIRED", GPGME_STATUS_SIGEXPIRED }, { "SIG_CREATED", GPGME_STATUS_SIG_CREATED }, { "SIG_ID", GPGME_STATUS_SIG_ID }, { "SIG_SUBPACKET", GPGME_STATUS_SIG_SUBPACKET }, { "SUCCESS", GPGME_STATUS_SUCCESS }, { "TOFU_STATS", GPGME_STATUS_TOFU_STATS }, { "TOFU_STATS_LONG", GPGME_STATUS_TOFU_STATS_LONG }, { "TOFU_USER", GPGME_STATUS_TOFU_USER }, { "TRUNCATED", GPGME_STATUS_TRUNCATED }, { "TRUST_FULLY", GPGME_STATUS_TRUST_FULLY }, { "TRUST_MARGINAL", GPGME_STATUS_TRUST_MARGINAL }, { "TRUST_NEVER", GPGME_STATUS_TRUST_NEVER }, { "TRUST_ULTIMATE", GPGME_STATUS_TRUST_ULTIMATE }, { "TRUST_UNDEFINED", GPGME_STATUS_TRUST_UNDEFINED }, { "UNEXPECTED", GPGME_STATUS_UNEXPECTED }, { "USERID_HINT", GPGME_STATUS_USERID_HINT }, { "VALIDSIG", GPGME_STATUS_VALIDSIG }, { "VERIFICATION_COMPLIANCE_MODE", GPGME_STATUS_VERIFICATION_COMPLIANCE_MODE }, {NULL, 0} }; static int status_cmp (const void *ap, const void *bp) { const struct status_table_s *a = ap; const struct status_table_s *b = bp; return strcmp (a->name, b->name); } void _gpgme_status_init (void) { qsort (status_table, DIM(status_table) - 1, sizeof (status_table[0]), status_cmp); } gpgme_status_code_t _gpgme_parse_status (const char *name) { struct status_table_s t, *r; t.name = name; r = bsearch (&t, status_table, DIM(status_table) - 1, sizeof t, status_cmp); return r ? r->code : -1; } const char * _gpgme_status_to_string (gpgme_status_code_t code) { int i; for (i=0; i < DIM(status_table); i++) if (status_table[i].code == code) return status_table[i].name? status_table[i].name : ""; return "status_code_lost"; } diff --git a/src/stpcpy.c b/src/stpcpy.c index 6e42911f..060df118 100644 --- a/src/stpcpy.c +++ b/src/stpcpy.c @@ -1,55 +1,55 @@ /* Copyright (C) 1992, 1995, 1997, 2002, 2004 Free Software Foundation, Inc. - This file is part of the GNU C Library. - - The GNU C Library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - The GNU C Library 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with the GNU C Library; if not, write to the Free - Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA - 02111-1307 USA. */ + * This file is part of the GNU C Library. + * + * The GNU C Library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * The GNU C Library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H # include #endif #include #undef __stpcpy #undef stpcpy #ifndef weak_alias # define __stpcpy stpcpy #endif /* Copy SRC to DEST, returning the address of the terminating '\0' in DEST. */ char * __stpcpy (dest, src) char *dest; const char *src; { register char *d = dest; register const char *s = src; do *d++ = *s; while (*s++ != '\0'); return d - 1; } #ifdef libc_hidden_def libc_hidden_def (__stpcpy) #endif #ifdef weak_alias weak_alias (__stpcpy, stpcpy) #endif #ifdef libc_hidden_builtin_def libc_hidden_builtin_def (stpcpy) #endif diff --git a/src/tofupolicy.c b/src/tofupolicy.c index 460e3ba8..3ac9b8a6 100644 --- a/src/tofupolicy.c +++ b/src/tofupolicy.c @@ -1,184 +1,185 @@ /* tofupolicy.c - Tofu policy helpers. * Copyright (C) 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" typedef struct { /* The error code from a FAILURE status line or 0. */ gpg_error_t failure_code; /* The error code from an ERROR status line or 0. */ gpg_error_t error_code; } *op_data_t; /* Parse an error status line. Return the error location and the error code. The function may modify ARGS. */ static char * parse_error (char *args, gpg_error_t *r_err) { char *where = strchr (args, ' '); char *which; if (where) { *where = '\0'; which = where + 1; where = strchr (which, ' '); if (where) *where = '\0'; where = args; } else { *r_err = trace_gpg_error (GPG_ERR_INV_ENGINE); return NULL; } *r_err = atoi (which); return where; } static gpgme_error_t tofu_policy_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; char *loc; err = _gpgme_op_data_lookup (ctx, OPDATA_TOFU_POLICY, &hook, -1, NULL); opd = hook; if (err) return err; switch (code) { case GPGME_STATUS_ERROR: loc = parse_error (args, &err); if (!loc) return err; if (!opd->error_code) opd->error_code = err; break; case GPGME_STATUS_FAILURE: opd->failure_code = _gpgme_parse_failure (args); break; case GPGME_STATUS_EOF: if (opd->error_code) err = opd->error_code; else if (opd->failure_code) err = opd->failure_code; break; default: break; } return err; } /* Set the TOFU policy for KEY to POLICY. */ static gpgme_error_t tofu_policy_start (gpgme_ctx_t ctx, int synchronous, gpgme_key_t key, gpgme_tofu_policy_t policy) { gpgme_error_t err; void *hook; op_data_t opd; if (ctx->protocol != GPGME_PROTOCOL_OPENPGP) return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL); if (!key) return gpg_error (GPG_ERR_INV_VALUE); err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_TOFU_POLICY, &hook, sizeof (*opd), NULL); opd = hook; if (err) return err; _gpgme_engine_set_status_handler (ctx->engine, tofu_policy_status_handler, ctx); return _gpgme_engine_op_tofu_policy (ctx->engine, key, policy); } /* Set the TOFU policy of KEY to POLCIY. This is the asynchronous * variant. */ gpgme_error_t gpgme_op_tofu_policy_start (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_tofu_policy_t policy) { gpg_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_tofu_policy_start", ctx, "key=%p, policy=%u", key, (unsigned int)policy); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = tofu_policy_start (ctx, 0, key, policy); return TRACE_ERR (err); } /* This is the synchronous variant of gpgme_op_tofu_policy_start. */ gpgme_error_t gpgme_op_tofu_policy (gpgme_ctx_t ctx, gpgme_key_t key, gpgme_tofu_policy_t policy) { gpgme_error_t err; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_tofu_policy", ctx, "key=%p, policy=%u", key, (unsigned int)policy); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = tofu_policy_start (ctx, 1, key, policy); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } diff --git a/src/trust-item.c b/src/trust-item.c index f9378c68..8f4de73b 100644 --- a/src/trust-item.c +++ b/src/trust-item.c @@ -1,172 +1,172 @@ /* trust-item.c - Trust item objects. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include "util.h" #include "ops.h" #include "sema.h" #include "debug.h" /* Protects all reference counters in trust items. All other accesses to a trust item are either read only or happen before the trust item is available to the user. */ DEFINE_STATIC_LOCK (trust_item_ref_lock); /* Create a new trust item. */ gpgme_error_t _gpgme_trust_item_new (gpgme_trust_item_t *r_item) { gpgme_trust_item_t item; item = calloc (1, sizeof *item); if (!item) return gpg_error_from_syserror (); item->_refs = 1; item->keyid = item->_keyid; item->_keyid[16] = '\0'; item->owner_trust = item->_owner_trust; item->_owner_trust[1] = '\0'; item->validity = item->_validity; item->_validity[1] = '\0'; *r_item = item; return 0; } /* Acquire a reference to ITEM. */ void gpgme_trust_item_ref (gpgme_trust_item_t item) { LOCK (trust_item_ref_lock); item->_refs++; UNLOCK (trust_item_ref_lock); } /* gpgme_trust_item_unref releases the trust item object. Note that this function may not do an actual release if there are other shallow copies of the object. You have to call this function for every newly created trust item object as well as for every gpgme_trust_item_ref() done on the trust item object. */ void gpgme_trust_item_unref (gpgme_trust_item_t item) { LOCK (trust_item_ref_lock); assert (item->_refs > 0); if (--item->_refs) { UNLOCK (trust_item_ref_lock); return; } UNLOCK (trust_item_ref_lock); if (item->name) free (item->name); free (item); } /* Compatibility interfaces. */ void gpgme_trust_item_release (gpgme_trust_item_t item) { gpgme_trust_item_unref (item); } /* Return the value of the attribute WHAT of ITEM, which has to be representable by a string. */ const char *gpgme_trust_item_get_string_attr (gpgme_trust_item_t item, _gpgme_attr_t what, const void *reserved, int idx) { const char *val = NULL; if (!item) return NULL; if (reserved) return NULL; if (idx) return NULL; switch (what) { case GPGME_ATTR_KEYID: val = item->keyid; break; case GPGME_ATTR_OTRUST: val = item->owner_trust; break; case GPGME_ATTR_VALIDITY: val = item->validity; break; case GPGME_ATTR_USERID: val = item->name; break; default: break; } return val; } /* Return the value of the attribute WHAT of KEY, which has to be representable by an integer. IDX specifies a running index if the attribute appears more than once in the key. */ int gpgme_trust_item_get_int_attr (gpgme_trust_item_t item, _gpgme_attr_t what, const void *reserved, int idx) { int val = 0; if (!item) return 0; if (reserved) return 0; if (idx) return 0; switch (what) { case GPGME_ATTR_LEVEL: val = item->level; break; case GPGME_ATTR_TYPE: val = item->type; break; default: break; } return val; } diff --git a/src/trustlist.c b/src/trustlist.c index ee09e6e0..69f565a0 100644 --- a/src/trustlist.c +++ b/src/trustlist.c @@ -1,279 +1,279 @@ /* trustlist.c - Trust item listing. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include "gpgme.h" #include "debug.h" #include "util.h" #include "context.h" #include "ops.h" struct trust_queue_item_s { struct trust_queue_item_s *next; gpgme_trust_item_t item; }; typedef struct { /* Something new is available. */ int trust_cond; struct trust_queue_item_s *trust_queue; } *op_data_t; static gpgme_error_t trustlist_status_handler (void *priv, gpgme_status_code_t code, char *args) { (void)priv; (void)code; (void)args; return 0; } /* This handler is used to parse the output of --list-trust-path: Format: level:keyid:type:recno:ot:val:mc:cc:name: With TYPE = U for a user ID K for a key The RECNO is either the one of the dir record or the one of the uid record. OT is the the usual trust letter and only available on K lines. VAL is the calculated validity MC is the marginal trust counter and only available on U lines CC is the same for the complete count NAME ist the username and only printed on U lines. */ static gpgme_error_t trustlist_colon_handler (void *priv, char *line) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; char *p, *pend; int field = 0; gpgme_trust_item_t item = NULL; if (!line) return 0; /* EOF */ for (p = line; p; p = pend) { field++; pend = strchr (p, ':'); if (pend) *pend++ = 0; switch (field) { case 1: /* level */ err = _gpgme_trust_item_new (&item); if (err) return err; item->level = atoi (p); break; case 2: /* long keyid */ if (strlen (p) == DIM(item->keyid) - 1) strcpy (item->keyid, p); break; case 3: /* type */ item->type = *p == 'K'? 1 : *p == 'U'? 2 : 0; break; case 5: /* owner trust */ item->_owner_trust[0] = *p; break; case 6: /* validity */ item->_validity[0] = *p; break; case 9: /* user ID */ item->name = strdup (p); if (!item->name) { int saved_err = gpg_error_from_syserror (); gpgme_trust_item_unref (item); return saved_err; } break; } } if (item) _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_NEXT_TRUSTITEM, item); return 0; } void _gpgme_op_trustlist_event_cb (void *data, gpgme_event_io_t type, void *type_data) { gpgme_ctx_t ctx = (gpgme_ctx_t) data; gpgme_error_t err; void *hook; op_data_t opd; gpgme_trust_item_t item = (gpgme_trust_item_t) type_data; struct trust_queue_item_s *q, *q2; assert (type == GPGME_EVENT_NEXT_TRUSTITEM); err = _gpgme_op_data_lookup (ctx, OPDATA_TRUSTLIST, &hook, -1, NULL); opd = hook; if (err) return; q = malloc (sizeof *q); if (!q) { gpgme_trust_item_unref (item); /* FIXME: GPGME_Out_Of_Core; */ return; } q->item = item; q->next = NULL; /* FIXME: Use a tail pointer */ q2 = opd->trust_queue; if (!q2) opd->trust_queue = q; else { while (q2->next) q2 = q2->next; q2->next = q; } /* FIXME: unlock queue */ opd->trust_cond = 1; } gpgme_error_t gpgme_op_trustlist_start (gpgme_ctx_t ctx, const char *pattern, int max_level) { gpgme_error_t err = 0; void *hook; op_data_t opd; TRACE_BEG2 (DEBUG_CTX, "gpgme_op_trustlist_start", ctx, "pattern=%s, max_level=%i", pattern, max_level); if (!ctx || !pattern || !*pattern) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_op_reset (ctx, 2); if (err) return TRACE_ERR (err); err = _gpgme_op_data_lookup (ctx, OPDATA_TRUSTLIST, &hook, sizeof (*opd), NULL); opd = hook; if (err) return TRACE_ERR (err); _gpgme_engine_set_status_handler (ctx->engine, trustlist_status_handler, ctx); err = _gpgme_engine_set_colon_line_handler (ctx->engine, trustlist_colon_handler, ctx); if (err) return TRACE_ERR (err); err = _gpgme_engine_op_trustlist (ctx->engine, pattern); return TRACE_ERR (err); } gpgme_error_t gpgme_op_trustlist_next (gpgme_ctx_t ctx, gpgme_trust_item_t *r_item) { gpgme_error_t err; void *hook; op_data_t opd; struct trust_queue_item_s *q; TRACE_BEG (DEBUG_CTX, "gpgme_op_trustlist_next", ctx); if (!ctx || !r_item) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); *r_item = NULL; if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_op_data_lookup (ctx, OPDATA_TRUSTLIST, &hook, -1, NULL); opd = hook; if (err) return TRACE_ERR (err); if (opd == NULL) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (!opd->trust_queue) { err = _gpgme_wait_on_condition (ctx, &opd->trust_cond, NULL); if (err) return TRACE_ERR (err); if (!opd->trust_cond) return TRACE_ERR (gpg_error (GPG_ERR_EOF)); opd->trust_cond = 0; assert (opd->trust_queue); } q = opd->trust_queue; opd->trust_queue = q->next; *r_item = q->item; free (q); if ((*r_item)->type == 1) { TRACE_SUC5 ("trust_item=%p: %s: owner trust %s with level %i " "and validity 0x%x", *r_item, (*r_item)->keyid, (*r_item)->owner_trust, (*r_item)->level, (*r_item)->validity); } else if ((*r_item)->type == 2) { TRACE_SUC5 ("trust_item=%p: %s: UID %s with level %i " "and validity 0x%x", *r_item, (*r_item)->keyid, (*r_item)->name, (*r_item)->level, (*r_item)->validity); } else { TRACE_SUC5 ("trust_item=%p: %s: unknown type %i with level %i " "and validity 0x%x", *r_item, (*r_item)->keyid, (*r_item)->type, (*r_item)->level, (*r_item)->validity); } return 0; } /* Terminate a pending trustlist operation within CTX. */ gpgme_error_t gpgme_op_trustlist_end (gpgme_ctx_t ctx) { TRACE (DEBUG_CTX, "gpgme_op_trustlist_end", ctx); if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); return 0; } diff --git a/src/ttyname_r.c b/src/ttyname_r.c index 7aed79e8..a497e47f 100644 --- a/src/ttyname_r.c +++ b/src/ttyname_r.c @@ -1,135 +1,136 @@ /* ttyname_r.c - A ttyname_r() replacement. - Copyright (C) 2003, 2004, 2012 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2003, 2004, 2012 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #if HAVE_CONFIG_H #include #endif #include #include #include #ifdef HAVE_UNISTD_H # include #endif #if !HAVE_TTYNAME_R && defined(__GNUC__) # warning ttyname is not thread-safe, and ttyname_r is missing #endif /* For Android we force the use of our replacement code. */ #if HAVE_ANDROID_SYSTEM # undef HAVE_TTYNAME_R #endif int _gpgme_ttyname_r (int fd, char *buf, size_t buflen) { #if HAVE_TTYNAME_R # if HAVE_BROKEN_TTYNAME_R /* Solaris fails if BUFLEN is less than 128. OSF/1 5.1 completely ignores BUFLEN. We use a large buffer to woraround this. */ { char largebuf[512]; size_t namelen; int rc; # if HAVE_POSIXDECL_TTYNAME_R if (buflen < sizeof (largebuf)) { rc = ttyname_r (fd, largebuf, (int)sizeof (largebuf)); if (!rc) { namelen = strlen (largebuf) + 1; if (namelen > buflen) rc = ERANGE; else memcpy (buf, largebuf, namelen); } } else rc = ttyname_r (fd, buf, (int)buflen); # else /*!HAVE_POSIXDECL_TTYNAME_R*/ char *name; if (buflen < sizeof (largebuf)) name = ttyname_r (fd, largebuf, (int)sizeof (largebuf)); else name = ttyname_r (fd, buf, (int)buflen); rc = name? 0 : (errno? errno : -1); if (!rc && buf != name) { namelen = strlen (name) + 1; if (namelen > buflen) rc = ERANGE; else memmove (buf, name, namelen); } # endif return rc; } # else /*!HAVE_BROKEN_TTYNAME_R*/ { int rc; # if HAVE_POSIXDECL_TTYNAME_R rc = ttyname_r (fd, buf, buflen); # else /*!HAVE_POSIXDECL_TTYNAME_R*/ char *name; size_t namelen; name = ttyname_r (fd, buf, (int)buflen); rc = name? 0 : (errno? errno : -1); if (!rc && buf != name) { namelen = strlen (name) + 1; if (namelen > buflen) rc = ERANGE; else memmove (buf, name, namelen); } # endif return rc; } # endif /*!HAVE_BROKEN_TTYNAME_R*/ #else /*!HAVE_TTYNAME_R*/ char *tty; # if HAVE_W32_SYSTEM || HAVE_ANDROID_SYSTEM /* We use this default one for now. AFAICS we only need it to be passed to gpg and in turn to pinentry. Providing a replacement is needed because elsewhere we bail out on error or Android provided ttyname_r prints an error message if used. */ tty = "/dev/tty"; # else tty = ttyname (fd); if (!tty) return errno? errno : -1; # endif strncpy (buf, tty, buflen); buf[buflen - 1] = '\0'; return (strlen (tty) >= buflen) ? ERANGE : 0; #endif /*!HAVE_TTYNAME_R*/ } diff --git a/src/verify.c b/src/verify.c index bd35346f..a80563a0 100644 --- a/src/verify.c +++ b/src/verify.c @@ -1,1394 +1,1394 @@ /* verify.c - Signature verification. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "gpgme.h" #include "debug.h" #include "util.h" #include "context.h" #include "ops.h" typedef struct { struct _gpgme_op_verify_result result; /* The error code from a FAILURE status line or 0. */ gpg_error_t failure_code; gpgme_signature_t current_sig; int did_prepare_new_sig; int only_newsig_seen; int plaintext_seen; int conflict_user_seen; } *op_data_t; static void release_op_data (void *hook) { op_data_t opd = (op_data_t) hook; gpgme_signature_t sig = opd->result.signatures; while (sig) { gpgme_signature_t next = sig->next; gpgme_sig_notation_t notation = sig->notations; while (notation) { gpgme_sig_notation_t next_nota = notation->next; _gpgme_sig_notation_free (notation); notation = next_nota; } if (sig->fpr) free (sig->fpr); if (sig->pka_address) free (sig->pka_address); if (sig->key) gpgme_key_unref (sig->key); free (sig); sig = next; } if (opd->result.file_name) free (opd->result.file_name); } gpgme_verify_result_t gpgme_op_verify_result (gpgme_ctx_t ctx) { void *hook; op_data_t opd; gpgme_error_t err; gpgme_signature_t sig; TRACE_BEG (DEBUG_CTX, "gpgme_op_verify_result", ctx); err = _gpgme_op_data_lookup (ctx, OPDATA_VERIFY, &hook, -1, NULL); opd = hook; if (err || !opd) { TRACE_SUC0 ("result=(null)"); return NULL; } /* It is possible that we saw a new signature only followed by an ERROR line for that. In particular a missing X.509 key triggers this. In this case it is surprising that the summary field has not been updated. We fix it here by explicitly looking for this case. The real fix would be to have GPGME emit ERRSIG. */ for (sig = opd->result.signatures; sig; sig = sig->next) { if (!sig->summary) { switch (gpg_err_code (sig->status)) { case GPG_ERR_KEY_EXPIRED: sig->summary |= GPGME_SIGSUM_KEY_EXPIRED; break; case GPG_ERR_NO_PUBKEY: sig->summary |= GPGME_SIGSUM_KEY_MISSING; break; default: break; } } } /* Now for some tracing stuff. */ if (_gpgme_debug_trace ()) { int i; for (sig = opd->result.signatures, i = 0; sig; sig = sig->next, i++) { TRACE_LOG4 ("sig[%i] = fpr %s, summary 0x%x, status %s", i, sig->fpr, sig->summary, gpg_strerror (sig->status)); TRACE_LOG6 ("sig[%i] = timestamps 0x%x/0x%x flags:%s%s%s", i, sig->timestamp, sig->exp_timestamp, sig->wrong_key_usage ? "wrong key usage" : "", sig->pka_trust == 1 ? "pka bad" : (sig->pka_trust == 2 ? "pka_okay" : "pka RFU"), sig->chain_model ? "chain model" : ""); TRACE_LOG5 ("sig[%i] = validity 0x%x (%s), algos %s/%s", i, sig->validity, gpg_strerror (sig->validity_reason), gpgme_pubkey_algo_name (sig->pubkey_algo), gpgme_hash_algo_name (sig->hash_algo)); if (sig->pka_address) { TRACE_LOG2 ("sig[%i] = PKA address %s", i, sig->pka_address); } if (sig->notations) { TRACE_LOG1 ("sig[%i] = has notations (not shown)", i); } } } TRACE_SUC1 ("result=%p", &opd->result); return &opd->result; } /* Build a summary vector from RESULT. */ static void calc_sig_summary (gpgme_signature_t sig) { unsigned long sum = 0; /* Calculate the red/green flag. */ if (sig->validity == GPGME_VALIDITY_FULL || sig->validity == GPGME_VALIDITY_ULTIMATE) { if (gpg_err_code (sig->status) == GPG_ERR_NO_ERROR || gpg_err_code (sig->status) == GPG_ERR_SIG_EXPIRED || gpg_err_code (sig->status) == GPG_ERR_KEY_EXPIRED) sum |= GPGME_SIGSUM_GREEN; } else if (sig->validity == GPGME_VALIDITY_NEVER) { if (gpg_err_code (sig->status) == GPG_ERR_NO_ERROR || gpg_err_code (sig->status) == GPG_ERR_SIG_EXPIRED || gpg_err_code (sig->status) == GPG_ERR_KEY_EXPIRED) sum |= GPGME_SIGSUM_RED; } else if (gpg_err_code (sig->status) == GPG_ERR_BAD_SIGNATURE) sum |= GPGME_SIGSUM_RED; /* FIXME: handle the case when key and message are expired. */ switch (gpg_err_code (sig->status)) { case GPG_ERR_SIG_EXPIRED: sum |= GPGME_SIGSUM_SIG_EXPIRED; break; case GPG_ERR_KEY_EXPIRED: sum |= GPGME_SIGSUM_KEY_EXPIRED; break; case GPG_ERR_NO_PUBKEY: sum |= GPGME_SIGSUM_KEY_MISSING; break; case GPG_ERR_CERT_REVOKED: sum |= GPGME_SIGSUM_KEY_REVOKED; break; case GPG_ERR_BAD_SIGNATURE: case GPG_ERR_NO_ERROR: break; default: sum |= GPGME_SIGSUM_SYS_ERROR; break; } /* Now look at the certain reason codes. */ switch (gpg_err_code (sig->validity_reason)) { case GPG_ERR_CRL_TOO_OLD: if (sig->validity == GPGME_VALIDITY_UNKNOWN) sum |= GPGME_SIGSUM_CRL_TOO_OLD; break; case GPG_ERR_CERT_REVOKED: /* Note that this is a second way to set this flag. It may also have been set due to a sig->status of STATUS_REVKEYSIG from parse_new_sig. */ sum |= GPGME_SIGSUM_KEY_REVOKED; break; default: break; } /* Check other flags. */ if (sig->wrong_key_usage) sum |= GPGME_SIGSUM_BAD_POLICY; /* Set the valid flag when the signature is unquestionable valid. (The test is identical to if(sum == GPGME_SIGSUM_GREEN)). */ if ((sum & GPGME_SIGSUM_GREEN) && !(sum & ~GPGME_SIGSUM_GREEN)) sum |= GPGME_SIGSUM_VALID; sig->summary = sum; } static gpgme_error_t prepare_new_sig (op_data_t opd) { gpgme_signature_t sig; if (opd->only_newsig_seen && opd->current_sig) { /* We have only seen the NEWSIG status and nothing else - we better skip this signature therefore and reuse it for the next possible signature. */ sig = opd->current_sig; memset (sig, 0, sizeof *sig); assert (opd->result.signatures == sig); } else { sig = calloc (1, sizeof (*sig)); if (!sig) return gpg_error_from_syserror (); if (!opd->result.signatures) opd->result.signatures = sig; if (opd->current_sig) opd->current_sig->next = sig; opd->current_sig = sig; } opd->did_prepare_new_sig = 1; opd->only_newsig_seen = 0; return 0; } static gpgme_error_t parse_new_sig (op_data_t opd, gpgme_status_code_t code, char *args, gpgme_protocol_t protocol) { gpgme_signature_t sig; char *end = strchr (args, ' '); char *tail; int got_fpr = 0; if (end) { *end = '\0'; end++; } if (!opd->did_prepare_new_sig) { gpg_error_t err; err = prepare_new_sig (opd); if (err) return err; } assert (opd->did_prepare_new_sig); opd->did_prepare_new_sig = 0; assert (opd->current_sig); sig = opd->current_sig; /* FIXME: We should set the source of the state. */ switch (code) { case GPGME_STATUS_GOODSIG: sig->status = gpg_error (GPG_ERR_NO_ERROR); break; case GPGME_STATUS_EXPSIG: sig->status = gpg_error (GPG_ERR_SIG_EXPIRED); break; case GPGME_STATUS_EXPKEYSIG: sig->status = gpg_error (GPG_ERR_KEY_EXPIRED); break; case GPGME_STATUS_BADSIG: sig->status = gpg_error (GPG_ERR_BAD_SIGNATURE); break; case GPGME_STATUS_REVKEYSIG: sig->status = gpg_error (GPG_ERR_CERT_REVOKED); break; case GPGME_STATUS_ERRSIG: /* Parse the pubkey algo. */ if (!end) goto parse_err_sig_fail; gpg_err_set_errno (0); sig->pubkey_algo = _gpgme_map_pk_algo (strtol (end, &tail, 0), protocol); if (errno || end == tail || *tail != ' ') goto parse_err_sig_fail; end = tail; while (*end == ' ') end++; /* Parse the hash algo. */ if (!*end) goto parse_err_sig_fail; gpg_err_set_errno (0); sig->hash_algo = strtol (end, &tail, 0); if (errno || end == tail || *tail != ' ') goto parse_err_sig_fail; end = tail; while (*end == ' ') end++; /* Skip the sig class. */ end = strchr (end, ' '); if (!end) goto parse_err_sig_fail; while (*end == ' ') end++; /* Parse the timestamp. */ sig->timestamp = _gpgme_parse_timestamp (end, &tail); if (sig->timestamp == -1 || end == tail || (*tail && *tail != ' ')) return trace_gpg_error (GPG_ERR_INV_ENGINE); end = tail; while (*end == ' ') end++; /* Parse the return code. */ if (!*end) goto parse_err_sig_fail; gpg_err_set_errno (0); sig->status = strtoul (end, &tail, 10); if (errno || end == tail || (*tail && *tail != ' ')) goto parse_err_sig_fail; if (!*tail) goto parse_err_sig_ok; end = tail; while (*end == ' ') end++; /* Parse the new fingerprint (from the ISSUER_FPR subpacket). */ if (!*end || (*end == '-' && (end[1] == ' ' || !end[1]))) goto parse_err_sig_ok; /* Okay (just trailing spaces). */ sig->fpr = strdup (end); if (!sig->fpr) return gpg_error_from_syserror (); got_fpr = 1; goto parse_err_sig_ok; parse_err_sig_fail: sig->status = gpg_error (GPG_ERR_GENERAL); parse_err_sig_ok: break; default: return gpg_error (GPG_ERR_GENERAL); } if (*args && !got_fpr) { sig->fpr = strdup (args); if (!sig->fpr) return gpg_error_from_syserror (); } return 0; } static gpgme_error_t parse_valid_sig (gpgme_signature_t sig, char *args, gpgme_protocol_t protocol) { char *end = strchr (args, ' '); if (end) { *end = '\0'; end++; } if (!*args) /* We require at least the fingerprint. */ return gpg_error (GPG_ERR_GENERAL); if (sig->fpr) free (sig->fpr); sig->fpr = strdup (args); if (!sig->fpr) return gpg_error_from_syserror (); /* Skip the creation date. */ end = strchr (end, ' '); if (end) { char *tail; sig->timestamp = _gpgme_parse_timestamp (end, &tail); if (sig->timestamp == -1 || end == tail || (*tail && *tail != ' ')) return trace_gpg_error (GPG_ERR_INV_ENGINE); end = tail; sig->exp_timestamp = _gpgme_parse_timestamp (end, &tail); if (sig->exp_timestamp == -1 || end == tail || (*tail && *tail != ' ')) return trace_gpg_error (GPG_ERR_INV_ENGINE); end = tail; while (*end == ' ') end++; /* Skip the signature version. */ end = strchr (end, ' '); if (end) { while (*end == ' ') end++; /* Skip the reserved field. */ end = strchr (end, ' '); if (end) { /* Parse the pubkey algo. */ gpg_err_set_errno (0); sig->pubkey_algo = _gpgme_map_pk_algo (strtol (end, &tail, 0), protocol); if (errno || end == tail || *tail != ' ') return trace_gpg_error (GPG_ERR_INV_ENGINE); end = tail; while (*end == ' ') end++; if (*end) { /* Parse the hash algo. */ gpg_err_set_errno (0); sig->hash_algo = strtol (end, &tail, 0); if (errno || end == tail || *tail != ' ') return trace_gpg_error (GPG_ERR_INV_ENGINE); end = tail; } } } } return 0; } static gpgme_error_t parse_notation (gpgme_signature_t sig, gpgme_status_code_t code, char *args) { gpgme_error_t err; gpgme_sig_notation_t *lastp = &sig->notations; gpgme_sig_notation_t notation = sig->notations; char *p; if (code == GPGME_STATUS_NOTATION_NAME || code == GPGME_STATUS_POLICY_URL) { p = strchr (args, ' '); if (p) *p = '\0'; /* FIXME: We could keep a pointer to the last notation in the list. */ while (notation && notation->value) { lastp = ¬ation->next; notation = notation->next; } if (notation) /* There is another notation name without data for the previous one. The crypto backend misbehaves. */ return trace_gpg_error (GPG_ERR_INV_ENGINE); err = _gpgme_sig_notation_create (¬ation, NULL, 0, NULL, 0, 0); if (err) return err; if (code == GPGME_STATUS_NOTATION_NAME) { err = _gpgme_decode_percent_string (args, ¬ation->name, 0, 0); if (err) { _gpgme_sig_notation_free (notation); return err; } notation->name_len = strlen (notation->name); /* Set default flags for use with older gpg versions which * do not emit a NOTATIONS_FLAG line. */ notation->flags = GPGME_SIG_NOTATION_HUMAN_READABLE; notation->human_readable = 1; } else { /* This is a policy URL. */ err = _gpgme_decode_percent_string (args, ¬ation->value, 0, 0); if (err) { _gpgme_sig_notation_free (notation); return err; } notation->value_len = strlen (notation->value); } *lastp = notation; } else if (code == GPGME_STATUS_NOTATION_FLAGS) { char *field[2]; while (notation && notation->next) { lastp = ¬ation->next; notation = notation->next; } if (!notation || !notation->name) { /* There are notation flags without a previous notation name. * The crypto backend misbehaves. */ return trace_gpg_error (GPG_ERR_INV_ENGINE); } if (_gpgme_split_fields (args, field, DIM (field)) < 2) { /* Required args missing. */ return trace_gpg_error (GPG_ERR_INV_ENGINE); } notation->flags = 0; if (atoi (field[0])) { notation->flags |= GPGME_SIG_NOTATION_CRITICAL; notation->critical = 1; } if (atoi (field[1])) { notation->flags |= GPGME_SIG_NOTATION_HUMAN_READABLE; notation->human_readable = 1; } } else if (code == GPGME_STATUS_NOTATION_DATA) { int len = strlen (args) + 1; char *dest; /* FIXME: We could keep a pointer to the last notation in the list. */ while (notation && notation->next) { lastp = ¬ation->next; notation = notation->next; } if (!notation || !notation->name) /* There is notation data without a previous notation name. The crypto backend misbehaves. */ return trace_gpg_error (GPG_ERR_INV_ENGINE); if (!notation->value) { dest = notation->value = malloc (len); if (!dest) return gpg_error_from_syserror (); } else { int cur_len = strlen (notation->value); dest = realloc (notation->value, len + strlen (notation->value)); if (!dest) return gpg_error_from_syserror (); notation->value = dest; dest += cur_len; } err = _gpgme_decode_percent_string (args, &dest, len, 0); if (err) return err; notation->value_len += strlen (dest); } else return trace_gpg_error (GPG_ERR_INV_ENGINE); return 0; } static gpgme_error_t parse_trust (gpgme_signature_t sig, gpgme_status_code_t code, char *args) { char *end = strchr (args, ' '); if (end) *end = '\0'; switch (code) { case GPGME_STATUS_TRUST_UNDEFINED: default: sig->validity = GPGME_VALIDITY_UNKNOWN; break; case GPGME_STATUS_TRUST_NEVER: sig->validity = GPGME_VALIDITY_NEVER; break; case GPGME_STATUS_TRUST_MARGINAL: sig->validity = GPGME_VALIDITY_MARGINAL; break; case GPGME_STATUS_TRUST_FULLY: case GPGME_STATUS_TRUST_ULTIMATE: sig->validity = GPGME_VALIDITY_FULL; break; } sig->validity_reason = 0; sig->chain_model = 0; if (*args) { sig->validity_reason = atoi (args); while (*args && *args != ' ') args++; if (*args) { while (*args == ' ') args++; if (!strncmp (args, "chain", 2) && (args[2] == ' ' || !args[2])) sig->chain_model = 1; } } return 0; } /* Parse a TOFU_USER line and put the info into SIG. */ static gpgme_error_t parse_tofu_user (gpgme_signature_t sig, char *args, gpgme_protocol_t protocol) { gpg_error_t err; char *tail; gpgme_user_id_t uid; gpgme_tofu_info_t ti; char *fpr = NULL; char *address = NULL; tail = strchr (args, ' '); if (!tail || tail == args) { err = trace_gpg_error (GPG_ERR_INV_ENGINE); /* No fingerprint. */ goto leave; } *tail++ = 0; fpr = strdup (args); if (!fpr) { err = gpg_error_from_syserror (); goto leave; } if (sig->key && sig->key->fpr && strcmp (sig->key->fpr, fpr)) { /* GnuPG since 2.1.17 emits multiple TOFU_USER lines with different fingerprints in case of conflicts for a signature. */ err = gpg_error (GPG_ERR_DUP_VALUE); goto leave; } args = tail; tail = strchr (args, ' '); if (tail == args) { err = trace_gpg_error (GPG_ERR_INV_ENGINE); /* No addr-spec. */ goto leave; } if (tail) *tail = 0; err = _gpgme_decode_percent_string (args, &address, 0, 0); if (err) goto leave; if (!sig->key) { err = _gpgme_key_new (&sig->key); if (err) goto leave; sig->key->fpr = fpr; sig->key->protocol = protocol; fpr = NULL; } else if (!sig->key->fpr) { err = trace_gpg_error (GPG_ERR_INTERNAL); goto leave; } err = _gpgme_key_append_name (sig->key, address, 0); if (err) goto leave; uid = sig->key->_last_uid; assert (uid); ti = calloc (1, sizeof *ti); if (!ti) { err = gpg_error_from_syserror (); goto leave; } uid->tofu = ti; leave: free (fpr); free (address); return err; } /* Parse a TOFU_STATS line and store it in the last tofu info of SIG. * * TOFU_STATS \ * [ [ ]] */ static gpgme_error_t parse_tofu_stats (gpgme_signature_t sig, char *args) { gpgme_error_t err; gpgme_tofu_info_t ti; char *field[8]; int nfields; unsigned long uval; if (!sig->key || !sig->key->_last_uid || !(ti = sig->key->_last_uid->tofu)) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* No TOFU_USER seen. */ if (ti->signfirst || ti->signcount || ti->validity || ti->policy) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Already set. */ nfields = _gpgme_split_fields (args, field, DIM (field)); if (nfields < 3) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Required args missing. */ /* Note that we allow a value of up to 7 which is what we can store * in the ti->validity. */ err = _gpgme_strtoul_field (field[0], &uval); if (err || uval > 7) return trace_gpg_error (GPG_ERR_INV_ENGINE); ti->validity = uval; /* Parse the sign-count. */ err = _gpgme_strtoul_field (field[1], &uval); if (err) return trace_gpg_error (GPG_ERR_INV_ENGINE); if (uval > USHRT_MAX) uval = USHRT_MAX; ti->signcount = uval; /* Parse the encr-count. */ err = _gpgme_strtoul_field (field[2], &uval); if (err) return trace_gpg_error (GPG_ERR_INV_ENGINE); if (uval > USHRT_MAX) uval = USHRT_MAX; ti->encrcount = uval; if (nfields == 3) return 0; /* All mandatory fields parsed. */ /* Parse the policy. */ if (!strcmp (field[3], "none")) ti->policy = GPGME_TOFU_POLICY_NONE; else if (!strcmp (field[3], "auto")) ti->policy = GPGME_TOFU_POLICY_AUTO; else if (!strcmp (field[3], "good")) ti->policy = GPGME_TOFU_POLICY_GOOD; else if (!strcmp (field[3], "bad")) ti->policy = GPGME_TOFU_POLICY_BAD; else if (!strcmp (field[3], "ask")) ti->policy = GPGME_TOFU_POLICY_ASK; else /* "unknown" and invalid policy strings. */ ti->policy = GPGME_TOFU_POLICY_UNKNOWN; if (nfields == 4) return 0; /* No more optional fields. */ /* Parse first and last seen timestamps (none or both are required). */ if (nfields < 6) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* "tm2" missing. */ err = _gpgme_strtoul_field (field[4], &uval); if (err) return trace_gpg_error (GPG_ERR_INV_ENGINE); ti->signfirst = uval; err = _gpgme_strtoul_field (field[5], &uval); if (err) return trace_gpg_error (GPG_ERR_INV_ENGINE); ti->signlast = uval; if (nfields > 7) { /* This condition is only to allow for gpg 2.1.15 - can * eventually be removed. */ err = _gpgme_strtoul_field (field[6], &uval); if (err) return trace_gpg_error (GPG_ERR_INV_ENGINE); ti->encrfirst = uval; err = _gpgme_strtoul_field (field[7], &uval); if (err) return trace_gpg_error (GPG_ERR_INV_ENGINE); ti->encrlast = uval; } return 0; } /* Parse a TOFU_STATS_LONG line and store it in the last tofu info of SIG. */ static gpgme_error_t parse_tofu_stats_long (gpgme_signature_t sig, char *args, int raw) { gpgme_error_t err; gpgme_tofu_info_t ti; char *p; if (!sig->key || !sig->key->_last_uid || !(ti = sig->key->_last_uid->tofu)) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* No TOFU_USER seen. */ if (ti->description) return trace_gpg_error (GPG_ERR_INV_ENGINE); /* Already set. */ err = _gpgme_decode_percent_string (args, &ti->description, 0, 0); if (err) return err; /* Remove the non-breaking spaces. */ if (!raw) { for (p = ti->description; *p; p++) if (*p == '~') *p = ' '; } return 0; } /* Parse an error status line and if SET_STATUS is true update the result status as appropriate. With SET_STATUS being false, only check for an error. */ static gpgme_error_t parse_error (gpgme_signature_t sig, char *args, int set_status) { gpgme_error_t err; char *where = strchr (args, ' '); char *which; if (where) { *where = '\0'; which = where + 1; where = strchr (which, ' '); if (where) *where = '\0'; where = args; } else return trace_gpg_error (GPG_ERR_INV_ENGINE); err = atoi (which); if (!strcmp (where, "proc_pkt.plaintext") && gpg_err_code (err) == GPG_ERR_BAD_DATA) { /* This indicates a double plaintext. The only solid way to handle this is by failing the oepration. */ return gpg_error (GPG_ERR_BAD_DATA); } else if (!set_status) ; else if (!strcmp (where, "verify.findkey")) sig->status = err; else if (!strcmp (where, "verify.keyusage") && gpg_err_code (err) == GPG_ERR_WRONG_KEY_USAGE) sig->wrong_key_usage = 1; return 0; } gpgme_error_t _gpgme_verify_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; gpgme_signature_t sig; char *end; err = _gpgme_op_data_lookup (ctx, OPDATA_VERIFY, &hook, -1, NULL); opd = hook; if (err) return err; sig = opd->current_sig; switch (code) { case GPGME_STATUS_NEWSIG: if (sig) calc_sig_summary (sig); err = prepare_new_sig (opd); opd->only_newsig_seen = 1; opd->conflict_user_seen = 0; return err; case GPGME_STATUS_GOODSIG: case GPGME_STATUS_EXPSIG: case GPGME_STATUS_EXPKEYSIG: case GPGME_STATUS_BADSIG: case GPGME_STATUS_ERRSIG: case GPGME_STATUS_REVKEYSIG: if (sig && !opd->did_prepare_new_sig) calc_sig_summary (sig); opd->only_newsig_seen = 0; return parse_new_sig (opd, code, args, ctx->protocol); case GPGME_STATUS_VALIDSIG: opd->only_newsig_seen = 0; return sig ? parse_valid_sig (sig, args, ctx->protocol) : trace_gpg_error (GPG_ERR_INV_ENGINE); case GPGME_STATUS_NODATA: opd->only_newsig_seen = 0; if (!sig) return gpg_error (GPG_ERR_NO_DATA); sig->status = gpg_error (GPG_ERR_NO_DATA); break; case GPGME_STATUS_UNEXPECTED: opd->only_newsig_seen = 0; if (!sig) return gpg_error (GPG_ERR_GENERAL); sig->status = gpg_error (GPG_ERR_NO_DATA); break; case GPGME_STATUS_NOTATION_NAME: case GPGME_STATUS_NOTATION_FLAGS: case GPGME_STATUS_NOTATION_DATA: case GPGME_STATUS_POLICY_URL: opd->only_newsig_seen = 0; return sig ? parse_notation (sig, code, args) : trace_gpg_error (GPG_ERR_INV_ENGINE); case GPGME_STATUS_TRUST_UNDEFINED: case GPGME_STATUS_TRUST_NEVER: case GPGME_STATUS_TRUST_MARGINAL: case GPGME_STATUS_TRUST_FULLY: case GPGME_STATUS_TRUST_ULTIMATE: opd->only_newsig_seen = 0; return sig ? parse_trust (sig, code, args) : trace_gpg_error (GPG_ERR_INV_ENGINE); case GPGME_STATUS_PKA_TRUST_BAD: case GPGME_STATUS_PKA_TRUST_GOOD: opd->only_newsig_seen = 0; /* Check that we only get one of these status codes per signature; if not the crypto backend misbehaves. */ if (!sig || sig->pka_trust || sig->pka_address) return trace_gpg_error (GPG_ERR_INV_ENGINE); sig->pka_trust = code == GPGME_STATUS_PKA_TRUST_GOOD? 2 : 1; end = strchr (args, ' '); if (end) *end = 0; sig->pka_address = strdup (args); break; case GPGME_STATUS_TOFU_USER: opd->only_newsig_seen = 0; if (!sig) return trace_gpg_error (GPG_ERR_INV_ENGINE); err = parse_tofu_user (sig, args, ctx->protocol); /* gpg emits TOFU User lines for each conflicting key. * GPGME does not expose this to have a clean API and * a GPGME user can do a keylisting with the address * normalisation. * So when a duplicated TOFU_USER line is encountered * we ignore the conflicting tofu stats emitted afterwards. */ if (gpg_err_code (err) == GPG_ERR_DUP_VALUE) { opd->conflict_user_seen = 1; break; } opd->conflict_user_seen = 0; return trace_gpg_error (err); case GPGME_STATUS_TOFU_STATS: opd->only_newsig_seen = 0; if (opd->conflict_user_seen) break; return sig ? parse_tofu_stats (sig, args) /* */ : trace_gpg_error (GPG_ERR_INV_ENGINE); case GPGME_STATUS_TOFU_STATS_LONG: opd->only_newsig_seen = 0; if (opd->conflict_user_seen) break; return sig ? parse_tofu_stats_long (sig, args, ctx->raw_description) /* */ : trace_gpg_error (GPG_ERR_INV_ENGINE); case GPGME_STATUS_ERROR: opd->only_newsig_seen = 0; /* Some error stati are informational, so we don't return an error code if we are not ready to process this status. */ return parse_error (sig, args, !!sig ); case GPGME_STATUS_FAILURE: opd->failure_code = _gpgme_parse_failure (args); break; case GPGME_STATUS_EOF: if (sig && !opd->did_prepare_new_sig) calc_sig_summary (sig); if (opd->only_newsig_seen && sig) { gpgme_signature_t sig2; /* The last signature has no valid information - remove it from the list. */ assert (!sig->next); if (sig == opd->result.signatures) opd->result.signatures = NULL; else { for (sig2 = opd->result.signatures; sig2; sig2 = sig2->next) if (sig2->next == sig) { sig2->next = NULL; break; } } /* Note that there is no need to release the members of SIG because we won't be here if they have been set. */ free (sig); opd->current_sig = NULL; } opd->only_newsig_seen = 0; if (opd->failure_code) return opd->failure_code; break; case GPGME_STATUS_PLAINTEXT: if (++opd->plaintext_seen > 1) return gpg_error (GPG_ERR_BAD_DATA); { int mime = 0; err = _gpgme_parse_plaintext (args, &opd->result.file_name, &mime); if (err) return err; opd->result.is_mime = !!mime; } break; case GPGME_STATUS_VERIFICATION_COMPLIANCE_MODE: PARSE_COMPLIANCE_FLAGS (args, opd->current_sig); break; default: break; } return 0; } static gpgme_error_t verify_status_handler (void *priv, gpgme_status_code_t code, char *args) { gpgme_error_t err; err = _gpgme_progress_status_handler (priv, code, args); if (!err) err = _gpgme_verify_status_handler (priv, code, args); return err; } gpgme_error_t _gpgme_op_verify_init_result (gpgme_ctx_t ctx) { void *hook; op_data_t opd; return _gpgme_op_data_lookup (ctx, OPDATA_VERIFY, &hook, sizeof (*opd), release_op_data); } static gpgme_error_t verify_start (gpgme_ctx_t ctx, int synchronous, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext) { gpgme_error_t err; err = _gpgme_op_reset (ctx, synchronous); if (err) return err; err = _gpgme_op_verify_init_result (ctx); if (err) return err; _gpgme_engine_set_status_handler (ctx->engine, verify_status_handler, ctx); if (!sig) return gpg_error (GPG_ERR_NO_DATA); return _gpgme_engine_op_verify (ctx->engine, sig, signed_text, plaintext, ctx); } /* Decrypt ciphertext CIPHER and make a signature verification within CTX and store the resulting plaintext in PLAIN. */ gpgme_error_t gpgme_op_verify_start (gpgme_ctx_t ctx, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext) { gpg_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_verify_start", ctx, "sig=%p, signed_text=%p, plaintext=%p", sig, signed_text, plaintext); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = verify_start (ctx, 0, sig, signed_text, plaintext); return TRACE_ERR (err); } /* Decrypt ciphertext CIPHER and make a signature verification within CTX and store the resulting plaintext in PLAIN. */ gpgme_error_t gpgme_op_verify (gpgme_ctx_t ctx, gpgme_data_t sig, gpgme_data_t signed_text, gpgme_data_t plaintext) { gpgme_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_verify", ctx, "sig=%p, signed_text=%p, plaintext=%p", sig, signed_text, plaintext); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = verify_start (ctx, 1, sig, signed_text, plaintext); if (!err) err = _gpgme_wait_one (ctx); return TRACE_ERR (err); } /* Compatibility interfaces. */ /* Get the key used to create signature IDX in CTX and return it in R_KEY. */ gpgme_error_t gpgme_get_sig_key (gpgme_ctx_t ctx, int idx, gpgme_key_t *r_key) { gpgme_verify_result_t result; gpgme_signature_t sig; if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); result = gpgme_op_verify_result (ctx); sig = result->signatures; while (sig && idx) { sig = sig->next; idx--; } if (!sig || idx) return gpg_error (GPG_ERR_EOF); return gpgme_get_key (ctx, sig->fpr, r_key, 0); } /* Retrieve the signature status of signature IDX in CTX after a successful verify operation in R_STAT (if non-null). The creation time stamp of the signature is returned in R_CREATED (if non-null). The function returns a string containing the fingerprint. */ const char * gpgme_get_sig_status (gpgme_ctx_t ctx, int idx, _gpgme_sig_stat_t *r_stat, time_t *r_created) { gpgme_verify_result_t result; gpgme_signature_t sig; result = gpgme_op_verify_result (ctx); sig = result->signatures; while (sig && idx) { sig = sig->next; idx--; } if (!sig || idx) return NULL; if (r_stat) { switch (gpg_err_code (sig->status)) { case GPG_ERR_NO_ERROR: *r_stat = GPGME_SIG_STAT_GOOD; break; case GPG_ERR_BAD_SIGNATURE: *r_stat = GPGME_SIG_STAT_BAD; break; case GPG_ERR_NO_PUBKEY: *r_stat = GPGME_SIG_STAT_NOKEY; break; case GPG_ERR_NO_DATA: *r_stat = GPGME_SIG_STAT_NOSIG; break; case GPG_ERR_SIG_EXPIRED: *r_stat = GPGME_SIG_STAT_GOOD_EXP; break; case GPG_ERR_KEY_EXPIRED: *r_stat = GPGME_SIG_STAT_GOOD_EXPKEY; break; default: *r_stat = GPGME_SIG_STAT_ERROR; break; } } if (r_created) *r_created = sig->timestamp; return sig->fpr; } /* Retrieve certain attributes of a signature. IDX is the index number of the signature after a successful verify operation. WHAT is an attribute where GPGME_ATTR_EXPIRE is probably the most useful one. WHATIDX is to be passed as 0 for most attributes . */ unsigned long gpgme_get_sig_ulong_attr (gpgme_ctx_t ctx, int idx, _gpgme_attr_t what, int whatidx) { gpgme_verify_result_t result; gpgme_signature_t sig; (void)whatidx; result = gpgme_op_verify_result (ctx); sig = result->signatures; while (sig && idx) { sig = sig->next; idx--; } if (!sig || idx) return 0; switch (what) { case GPGME_ATTR_CREATED: return sig->timestamp; case GPGME_ATTR_EXPIRE: return sig->exp_timestamp; case GPGME_ATTR_VALIDITY: return (unsigned long) sig->validity; case GPGME_ATTR_SIG_STATUS: switch (gpg_err_code (sig->status)) { case GPG_ERR_NO_ERROR: return GPGME_SIG_STAT_GOOD; case GPG_ERR_BAD_SIGNATURE: return GPGME_SIG_STAT_BAD; case GPG_ERR_NO_PUBKEY: return GPGME_SIG_STAT_NOKEY; case GPG_ERR_NO_DATA: return GPGME_SIG_STAT_NOSIG; case GPG_ERR_SIG_EXPIRED: return GPGME_SIG_STAT_GOOD_EXP; case GPG_ERR_KEY_EXPIRED: return GPGME_SIG_STAT_GOOD_EXPKEY; default: return GPGME_SIG_STAT_ERROR; } case GPGME_ATTR_SIG_SUMMARY: return sig->summary; default: break; } return 0; } const char * gpgme_get_sig_string_attr (gpgme_ctx_t ctx, int idx, _gpgme_attr_t what, int whatidx) { gpgme_verify_result_t result; gpgme_signature_t sig; result = gpgme_op_verify_result (ctx); sig = result->signatures; while (sig && idx) { sig = sig->next; idx--; } if (!sig || idx) return NULL; switch (what) { case GPGME_ATTR_FPR: return sig->fpr; case GPGME_ATTR_ERRTOK: if (whatidx == 1) return sig->wrong_key_usage ? "Wrong_Key_Usage" : ""; else return ""; default: break; } return NULL; } diff --git a/src/version.c b/src/version.c index bd1d965a..e1d8e592 100644 --- a/src/version.c +++ b/src/version.c @@ -1,370 +1,370 @@ /* version.c - Version check routines. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #ifdef HAVE_W32_SYSTEM #include #endif #include "gpgme.h" #include "priv-io.h" #include "debug.h" #include "context.h" /* For _gpgme_sema_subsystem_init and _gpgme_status_init. */ #include "sema.h" #include "util.h" #ifdef HAVE_ASSUAN_H #include "assuan.h" #endif #ifdef HAVE_W32_SYSTEM #include "windows.h" #endif /* We implement this function, so we have to disable the overriding macro. */ #undef gpgme_check_version /* Bootstrap the subsystems needed for concurrent operation. This must be done once at startup. We can not guarantee this using a lock, though, because the semaphore subsystem needs to be initialized itself before it can be used. So we expect that the user performs the necessary synchronization. */ static void do_subsystem_inits (void) { static int done = 0; if (done) return; #ifdef HAVE_W32_SYSTEM /* We need to make sure that the sockets are initialized. */ { WSADATA wsadat; WSAStartup (0x202, &wsadat); } #endif _gpgme_debug_subsystem_init (); _gpgme_io_subsystem_init (); _gpgme_status_init (); done = 1; } /* Put vesion information into the binary. */ static const char * cright_blurb (void) { static const char blurb[] = "\n\n" "This is GPGME " PACKAGE_VERSION " - The GnuPG Made Easy library\n" CRIGHTBLURB "\n" "(" BUILD_REVISION " " BUILD_TIMESTAMP ")\n" "\n\n"; return blurb; } /* Read the next number in the version string STR and return it in *NUMBER. Return a pointer to the tail of STR after parsing, or *NULL if the version string was invalid. */ static const char * parse_version_number (const char *str, int *number) { #define MAXVAL ((INT_MAX - 10) / 10) int val = 0; /* Leading zeros are not allowed. */ if (*str == '0' && isdigit(str[1])) return NULL; while (isdigit (*str) && val <= MAXVAL) { val *= 10; val += *(str++) - '0'; } *number = val; return val > MAXVAL ? NULL : str; } /* Parse the version string STR in the format MAJOR.MINOR.MICRO (for example, 9.3.2) and return the components in MAJOR, MINOR and MICRO as integers. The function returns the tail of the string that follows the version number. This might be the empty string if there is nothing following the version number, or a patchlevel. The function returns NULL if the version string is not valid. */ static const char * parse_version_string (const char *str, int *major, int *minor, int *micro) { str = parse_version_number (str, major); if (!str || *str != '.') return NULL; str++; str = parse_version_number (str, minor); if (!str || *str != '.') return NULL; str++; str = parse_version_number (str, micro); if (!str) return NULL; /* A patchlevel might follow. */ return str; } /* Return true if MY_VERSION is at least REQ_VERSION, and false otherwise. */ int _gpgme_compare_versions (const char *my_version, const char *rq_version) { int my_major, my_minor, my_micro; int rq_major, rq_minor, rq_micro; const char *my_plvl, *rq_plvl; if (!rq_version) return 1; if (!my_version) return 0; my_plvl = parse_version_string (my_version, &my_major, &my_minor, &my_micro); if (!my_plvl) return 0; rq_plvl = parse_version_string (rq_version, &rq_major, &rq_minor, &rq_micro); if (!rq_plvl) return 0; if (my_major > rq_major || (my_major == rq_major && my_minor > rq_minor) || (my_major == rq_major && my_minor == rq_minor && my_micro > rq_micro) || (my_major == rq_major && my_minor == rq_minor && my_micro == rq_micro && strcmp (my_plvl, rq_plvl) >= 0)) return 1; return 0; } /* Check that the the version of the library is at minimum the requested one and return the version string; return NULL if the condition is not met. If a NULL is passed to this function, no check is done and the version string is simply returned. This function must be run once at startup, as it also initializes some subsystems. Its invocation must be synchronized against calling any of the other functions in a multi-threaded environments. */ const char * gpgme_check_version (const char *req_version) { const char *result; do_subsystem_inits (); /* Catch-22: We need to get at least the debug subsystem ready before using the trace facility. If we won't the trace would automagically initialize the debug system with out the locks being initialized and missing the assuan log level setting. */ TRACE2 (DEBUG_INIT, "gpgme_check_version", 0, "req_version=%s, VERSION=%s", req_version? req_version:"(null)", VERSION); result = _gpgme_compare_versions (VERSION, req_version) ? VERSION : NULL; if (result != NULL) _gpgme_selftest = 0; return result; } /* Check the version and also at runtime if the struct layout of the library matches the one of the user. This is particular useful for Windows targets (-mms-bitfields). */ const char * gpgme_check_version_internal (const char *req_version, size_t offset_sig_validity) { const char *result; if (req_version && req_version[0] == 1 && req_version[1] == 1) return cright_blurb (); result = gpgme_check_version (req_version); if (result == NULL) return result; /* Catch-22, see above. */ TRACE2 (DEBUG_INIT, "gpgme_check_version_internal", 0, "req_version=%s, offset_sig_validity=%i", req_version ? req_version : "(null)", offset_sig_validity); if (offset_sig_validity != offsetof (struct _gpgme_signature, validity)) { TRACE1 (DEBUG_INIT, "gpgme_check_version_internal", 0, "offset_sig_validity mismatch: expected %i", offsetof (struct _gpgme_signature, validity)); _gpgme_selftest = GPG_ERR_SELFTEST_FAILED; } return result; } #define LINELENGTH 80 /* Extract the version string of a program from STRING. The version number is expected to be in GNU style format: foo 1.2.3 foo (bar system) 1.2.3 foo 1.2.3 cruft foo (bar system) 1.2.3 cruft. Spaces and tabs are skipped and used as delimiters, a term in (nested) parenthesis before the version string is skipped, the version string may consist of any non-space and non-tab characters but needs to bstart with a digit. */ static const char * extract_version_string (const char *string, size_t *r_len) { const char *s; int count, len; for (s=string; *s; s++) if (*s == ' ' || *s == '\t') break; while (*s == ' ' || *s == '\t') s++; if (*s == '(') { for (count=1, s++; count && *s; s++) if (*s == '(') count++; else if (*s == ')') count--; } /* For robustness we look for a digit. */ while ( *s && !(*s >= '0' && *s <= '9') ) s++; if (*s >= '0' && *s <= '9') { for (len=0; s[len]; len++) if (s[len] == ' ' || s[len] == '\t') break; } else len = 0; *r_len = len; return s; } /* Retrieve the version number from the --version output of the program FILE_NAME. */ char * _gpgme_get_program_version (const char *const file_name) { char line[LINELENGTH] = ""; int linelen = 0; char *mark = NULL; int rp[2]; int nread; char *argv[] = {NULL /* file_name */, (char*)"--version", 0}; struct spawn_fd_item_s cfd[] = { {-1, 1 /* STDOUT_FILENO */, -1, 0}, {-1, -1} }; int status; if (!file_name) return NULL; argv[0] = (char *) file_name; if (_gpgme_io_pipe (rp, 1) < 0) return NULL; cfd[0].fd = rp[1]; status = _gpgme_io_spawn (file_name, argv, IOSPAWN_FLAG_DETACHED, cfd, NULL, NULL, NULL); if (status < 0) { _gpgme_io_close (rp[0]); _gpgme_io_close (rp[1]); return NULL; } do { nread = _gpgme_io_read (rp[0], &line[linelen], LINELENGTH - linelen - 1); if (nread > 0) { line[linelen + nread] = '\0'; mark = strchr (&line[linelen], '\n'); if (mark) { if (mark > &line[0] && mark[-1] == '\r') mark--; *mark = '\0'; break; } linelen += nread; } } while (nread > 0 && linelen < LINELENGTH - 1); _gpgme_io_close (rp[0]); if (mark) { size_t len; const char *s; s = extract_version_string (line, &len); if (!len) return NULL; mark = malloc (len + 1); if (!mark) return NULL; memcpy (mark, s, len); mark[len] = 0; return mark; } return NULL; } diff --git a/src/vfs-create.c b/src/vfs-create.c index a3bec197..1e23106a 100644 --- a/src/vfs-create.c +++ b/src/vfs-create.c @@ -1,205 +1,205 @@ /* vfs-create.c - vfs create support in GPGME - Copyright (C) 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" #include "util.h" static gpgme_error_t vfs_start (gpgme_ctx_t ctx, int synchronous, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { gpgme_error_t err; if (!command || !*command) return gpg_error (GPG_ERR_INV_VALUE); /* The flag value 256 is used to suppress an engine reset. This is required to keep the connection running. */ err = _gpgme_op_reset (ctx, ((synchronous & 255) | 256)); if (err) return err; return _gpgme_engine_op_assuan_transact (ctx->engine, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); } #if 0 /* XXXX. This is the asynchronous variant. */ static gpgme_error_t gpgme_op_vfs_transact_start (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { return vfs_start (ctx, 0, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); } #endif /* XXXX. This is the synchronous variant. */ static gpgme_error_t gpgme_op_vfs_transact (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value, gpgme_error_t *op_err) { gpgme_error_t err; if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); err = vfs_start (ctx, 1, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); if (!err) err = _gpgme_wait_one_ext (ctx, op_err); return err; } /* The actual exported interface follows. */ /* The container is automatically uncreateed when the context is reset or destroyed. This is a synchronous convenience interface, which automatically returns an operation error if there is no transmission error. */ static gpgme_error_t _gpgme_op_vfs_create (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *container_file, unsigned int flags, gpgme_error_t *op_err) { gpg_error_t err; char *cmd; char *container_file_esc = NULL; int i; (void)flags; /* We want to encourage people to check error values, so not getting them is discouraged here. Also makes our code easier. */ if (! op_err) return gpg_error (GPG_ERR_INV_VALUE); err = _gpgme_encode_percent_string (container_file, &container_file_esc, 0); if (err) return err; i = 0; while (!err && recp[i]) { if (!recp[i]->subkeys || !recp[i]->subkeys->fpr) { free (container_file_esc); return gpg_error (GPG_ERR_UNUSABLE_PUBKEY); } if (gpgrt_asprintf (&cmd, "RECIPIENT %s", recp[i]->subkeys->fpr) < 0) { err = gpg_error_from_syserror (); free (container_file_esc); return err; } err = gpgme_op_vfs_transact (ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL, op_err); gpgrt_free (cmd); if (err || *op_err) { free (container_file_esc); return err; } recp++; } if (gpgrt_asprintf (&cmd, "CREATE -- %s", container_file_esc) < 0) { err = gpg_error_from_syserror (); free (container_file_esc); return err; } free (container_file_esc); err = gpgme_op_vfs_transact (ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL, op_err); gpgrt_free (cmd); return err; } gpgme_error_t gpgme_op_vfs_create (gpgme_ctx_t ctx, gpgme_key_t recp[], const char *container_file, unsigned int flags, gpgme_error_t *op_err) { gpg_error_t err; TRACE_BEG3 (DEBUG_CTX, "gpgme_op_vfs_create", ctx, "container_file=%s, flags=0x%x, op_err=%p", container_file, flags, op_err); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); if (_gpgme_debug_trace () && recp) { int i = 0; while (recp[i]) { TRACE_LOG3 ("recipient[%i] = %p (%s)", i, recp[i], (recp[i]->subkeys && recp[i]->subkeys->fpr) ? recp[i]->subkeys->fpr : "invalid"); i++; } } err = _gpgme_op_vfs_create (ctx, recp, container_file, flags, op_err); return TRACE_ERR (err); } diff --git a/src/vfs-mount.c b/src/vfs-mount.c index 68a8efe9..bb5f056c 100644 --- a/src/vfs-mount.c +++ b/src/vfs-mount.c @@ -1,247 +1,247 @@ /* vfs-mount.c - vfs mount support in GPGME - Copyright (C) 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include "gpgme.h" #include "debug.h" #include "context.h" #include "ops.h" #include "util.h" typedef struct { struct _gpgme_op_vfs_mount_result result; } *op_data_t; gpgme_vfs_mount_result_t gpgme_op_vfs_mount_result (gpgme_ctx_t ctx) { gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_VFS_MOUNT, &hook, -1, NULL); opd = hook; /* Check in case this function is used without having run a command before. */ if (err || !opd) return NULL; return &opd->result; } static gpgme_error_t _gpgme_vfs_mount_status_handler (void *priv, const char *code, const char *args) { gpgme_ctx_t ctx = (gpgme_ctx_t) priv; gpgme_error_t err; void *hook; op_data_t opd; err = _gpgme_op_data_lookup (ctx, OPDATA_VFS_MOUNT, &hook, -1, NULL); opd = hook; if (err) return err; if (! strcasecmp ("MOUNTPOINT", code)) { if (opd->result.mount_dir) free (opd->result.mount_dir); opd->result.mount_dir = strdup (args); } return 0; } static gpgme_error_t vfs_start (gpgme_ctx_t ctx, int synchronous, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { gpgme_error_t err; void *hook; op_data_t opd; if (!command || !*command) return gpg_error (GPG_ERR_INV_VALUE); /* The flag value 256 is used to suppress an engine reset. This is required to keep the connection running. */ err = _gpgme_op_reset (ctx, ((synchronous & 255) | 256)); if (err) return err; err = _gpgme_op_data_lookup (ctx, OPDATA_VFS_MOUNT, &hook, sizeof (*opd), NULL); opd = hook; if (err) return err; return _gpgme_engine_op_assuan_transact (ctx->engine, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); } #if 0 /* XXXX. This is the asynchronous variant. */ static gpgme_error_t gpgme_op_vfs_transact_start (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value) { return vfs_start (ctx, 0, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); } #endif /* XXXX. This is the synchronous variant. */ static gpgme_error_t gpgme_op_vfs_transact (gpgme_ctx_t ctx, const char *command, gpgme_assuan_data_cb_t data_cb, void *data_cb_value, gpgme_assuan_inquire_cb_t inq_cb, void *inq_cb_value, gpgme_assuan_status_cb_t status_cb, void *status_cb_value, gpgme_error_t *op_err) { gpgme_error_t err; if (!ctx) return gpg_error (GPG_ERR_INV_VALUE); err = vfs_start (ctx, 1, command, data_cb, data_cb_value, inq_cb, inq_cb_value, status_cb, status_cb_value); if (!err) err = _gpgme_wait_one_ext (ctx, op_err); return err; } /* The actual exported interface follows. */ /* The container is automatically unmounted when the context is reset or destroyed. This is a synchronous convenience interface, which automatically returns an operation error if there is no transmission error. */ static gpgme_error_t _gpgme_op_vfs_mount (gpgme_ctx_t ctx, const char *container_file, const char *mount_dir, int flags, gpgme_error_t *op_err) { gpg_error_t err; char *cmd; char *container_file_esc = NULL; (void)flags; /* We want to encourage people to check error values, so not getting them is discouraged here. Also makes our code easier. */ if (! op_err) return gpg_error (GPG_ERR_INV_VALUE); err = _gpgme_encode_percent_string (container_file, &container_file_esc, 0); if (err) return err; if (gpgrt_asprintf (&cmd, "OPEN -- %s", container_file_esc) < 0) { err = gpg_error_from_syserror (); free (container_file_esc); return err; } free (container_file_esc); err = gpgme_op_vfs_transact (ctx, cmd, NULL, NULL, NULL, NULL, NULL, NULL, op_err); gpgrt_free (cmd); if (err || *op_err) return err; if (mount_dir) { char *mount_dir_esc = NULL; err = _gpgme_encode_percent_string (mount_dir, &mount_dir_esc, 0); if (err) return err; if (gpgrt_asprintf (&cmd, "MOUNT -- %s", mount_dir_esc) < 0) { err = gpg_error_from_syserror (); free (mount_dir_esc); return err; } free (mount_dir_esc); } else { if (gpgrt_asprintf (&cmd, "MOUNT") < 0) return gpg_error_from_syserror (); } err = gpgme_op_vfs_transact (ctx, cmd, NULL, NULL, NULL, NULL, _gpgme_vfs_mount_status_handler, ctx, op_err); gpgrt_free (cmd); return err; } gpgme_error_t gpgme_op_vfs_mount (gpgme_ctx_t ctx, const char *container_file, const char *mount_dir, unsigned int flags, gpgme_error_t *op_err) { gpg_error_t err; TRACE_BEG4 (DEBUG_CTX, "gpgme_op_vfs_mount", ctx, "container=%s, mount_dir=%s, flags=0x%x, op_err=%p", container_file, mount_dir, flags, op_err); if (!ctx) return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE)); err = _gpgme_op_vfs_mount (ctx, container_file, mount_dir, flags, op_err); return TRACE_ERR (err); } diff --git a/src/w32-glib-io.c b/src/w32-glib-io.c index 967c64e8..abd8045e 100644 --- a/src/w32-glib-io.c +++ b/src/w32-glib-io.c @@ -1,1085 +1,1085 @@ /* w32-glib-io.c - W32 Glib I/O functions - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2004, 2005 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2004, 2005 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #ifdef HAVE_UNISTD_H # include #endif #ifdef HAVE_SYS_TIME_H # include #endif #ifdef HAVE_SYS_TYPES_H # include #endif #include #include #include #include "util.h" #include "priv-io.h" #include "sema.h" #include "debug.h" #ifndef O_BINARY #ifdef _O_BINARY #define O_BINARY _O_BINARY #else #define O_BINARY 0 #endif #endif /* This file is an ugly hack to get GPGME working with glib on Windows targets. On Windows, you can not select() on file descriptors. The only way to check if there is something to read is to read something. This means that GPGME can not let glib check for data without letting glib also handle the data on Windows targets. The ugly consequence is that we need to work on GIOChannels in GPGME, creating a glib dependency. Also, we need to export an interface for the application to get at GPGME's GIOChannel. There is no good way to abstract all this with callbacks, because the whole thing is also interconnected with the creation of pipes and child processes. The following rule applies only to this I/O backend: * ALL operations must use the user defined event loop. GPGME can not anymore provide its own event loop. This is mostly a sanity requirement: Although we have in theory all information we need to make the GPGME W32 code for select still work, it would be a big complication and require changes throughout GPGME. Eventually, we probably have to bite the bullet and make some really nice callback interfaces to let the user control all this at a per-context level. */ #define MAX_SLAFD 256 static struct { int used; /* If this is not -1, then it's a libc file descriptor. */ int fd; /* If fd is -1, this is the Windows socket handle. */ int socket; GIOChannel *chan; /* The boolean PRIMARY is true if this file descriptor caused the allocation of CHAN. Only then should CHAN be destroyed when this FD is closed. This, together with the fact that dup'ed file descriptors are closed before the file descriptors from which they are dup'ed are closed, ensures that CHAN is always valid, and shared among all file descriptors referring to the same underlying object. The logic behind this is that there is only one reason for us to dup file descriptors anyway: to allow simpler book-keeping of file descriptors shared between GPGME and libassuan, which both want to close something. Using the same channel for these duplicates works just fine (and in fact, using different channels does not work because the W32 backend in glib does not support that: One would end up with several competing reader/writer threads. */ int primary; } giochannel_table[MAX_SLAFD]; static GIOChannel * find_channel (int fd) { if (fd < 0 || fd >= MAX_SLAFD || !giochannel_table[fd].used) return NULL; return giochannel_table[fd].chan; } /* Returns the FD or -1 on resource limit. */ int new_dummy_channel_from_fd (int cfd) { int idx; for (idx = 0; idx < MAX_SLAFD; idx++) if (! giochannel_table[idx].used) break; if (idx == MAX_SLAFD) { errno = EIO; return -1; } giochannel_table[idx].used = 1; giochannel_table[idx].chan = NULL; giochannel_table[idx].fd = cfd; giochannel_table[idx].socket = INVALID_SOCKET; giochannel_table[idx].primary = 1; return idx; } /* Returns the FD or -1 on resource limit. */ int new_channel_from_fd (int cfd) { int idx; for (idx = 0; idx < MAX_SLAFD; idx++) if (! giochannel_table[idx].used) break; if (idx == MAX_SLAFD) { errno = EIO; return -1; } giochannel_table[idx].used = 1; giochannel_table[idx].chan = g_io_channel_win32_new_fd (cfd); giochannel_table[idx].fd = cfd; giochannel_table[idx].socket = INVALID_SOCKET; giochannel_table[idx].primary = 1; g_io_channel_set_encoding (giochannel_table[idx].chan, NULL, NULL); g_io_channel_set_buffered (giochannel_table[idx].chan, FALSE); return idx; } /* Returns the FD or -1 on resource limit. */ int new_channel_from_socket (int sock) { int idx; for (idx = 0; idx < MAX_SLAFD; idx++) if (! giochannel_table[idx].used) break; if (idx == MAX_SLAFD) { errno = EIO; return -1; } giochannel_table[idx].used = 1; giochannel_table[idx].chan = g_io_channel_win32_new_socket (sock); giochannel_table[idx].fd = -1; giochannel_table[idx].socket = sock; giochannel_table[idx].primary = 1; g_io_channel_set_encoding (giochannel_table[idx].chan, NULL, NULL); g_io_channel_set_buffered (giochannel_table[idx].chan, FALSE); return idx; } /* Compatibility interface. Obsolete. */ void * gpgme_get_giochannel (int fd) { return find_channel (fd); } /* Look up the giochannel for "file descriptor" FD. */ void * gpgme_get_fdptr (int fd) { return find_channel (fd); } /* Write the printable version of FD to the buffer BUF of length BUFLEN. The printable version is the representation on the command line that the child process expects. */ int _gpgme_io_fd2str (char *buf, int buflen, int fd) { HANDLE hndl; TRACE_BEG1 (DEBUG_SYSIO, "_gpgme_io_fd2str", fd, "fd=%d", fd); if (giochannel_table[fd].fd != -1) hndl = (HANDLE) _get_osfhandle (giochannel_table[fd].fd); else hndl = (HANDLE) giochannel_table[fd].socket; TRACE_SUC1 ("syshd=%p", hndl); return snprintf (buf, buflen, "%d", (int) hndl); } void _gpgme_io_subsystem_init (void) { } static struct { _gpgme_close_notify_handler_t handler; void *value; } notify_table[MAX_SLAFD]; int _gpgme_io_read (int fd, void *buffer, size_t count) { int saved_errno = 0; gsize nread; GIOChannel *chan; GIOStatus status; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_read", fd, "buffer=%p, count=%u", buffer, count); chan = find_channel (fd); if (!chan) { TRACE_LOG ("no channel registered"); errno = EINVAL; return TRACE_SYSRES (-1); } TRACE_LOG1 ("channel %p", chan); { GError *err = NULL; status = g_io_channel_read_chars (chan, (gchar *) buffer, count, &nread, &err); if (err) { TRACE_LOG2 ("status %i, err %s", status, err->message); g_error_free (err); } } if (status == G_IO_STATUS_EOF) nread = 0; else if (status == G_IO_STATUS_AGAIN) { nread = -1; saved_errno = EAGAIN; } else if (status != G_IO_STATUS_NORMAL) { TRACE_LOG1 ("status %d", status); nread = -1; saved_errno = EIO; } if (nread != 0 && nread != -1) TRACE_LOGBUFX (buffer, nread); errno = saved_errno; return TRACE_SYSRES (nread); } int _gpgme_io_write (int fd, const void *buffer, size_t count) { int saved_errno = 0; gsize nwritten; GIOChannel *chan; GIOStatus status; GError *err = NULL; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_write", fd, "buffer=%p, count=%u", buffer, count); TRACE_LOGBUFX (buffer, count); chan = find_channel (fd); if (!chan) { TRACE_LOG ("fd %d: no channel registered"); errno = EINVAL; return -1; } status = g_io_channel_write_chars (chan, (gchar *) buffer, count, &nwritten, &err); if (err) { TRACE_LOG1 ("write error: %s", err->message); g_error_free (err); } if (status == G_IO_STATUS_AGAIN) { nwritten = -1; saved_errno = EAGAIN; } else if (status != G_IO_STATUS_NORMAL) { nwritten = -1; saved_errno = EIO; } errno = saved_errno; return TRACE_SYSRES (nwritten); } int _gpgme_io_pipe (int filedes[2], int inherit_idx) { int fds[2]; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_pipe", filedes, "inherit_idx=%i (GPGME uses it for %s)", inherit_idx, inherit_idx ? "reading" : "writing"); #define PIPEBUF_SIZE 4096 if (_pipe (fds, PIPEBUF_SIZE, O_NOINHERIT | O_BINARY) == -1) return TRACE_SYSRES (-1); /* Make one end inheritable. */ if (inherit_idx == 0) { int new_read; new_read = _dup (fds[0]); _close (fds[0]); fds[0] = new_read; if (new_read < 0) { _close (fds[1]); return TRACE_SYSRES (-1); } } else if (inherit_idx == 1) { int new_write; new_write = _dup (fds[1]); _close (fds[1]); fds[1] = new_write; if (new_write < 0) { _close (fds[0]); return TRACE_SYSRES (-1); } } /* For _gpgme_io_close. */ filedes[inherit_idx] = new_dummy_channel_from_fd (fds[inherit_idx]); if (filedes[inherit_idx] < 0) { int saved_errno = errno; _close (fds[0]); _close (fds[1]); errno = saved_errno; return TRACE_SYSRES (-1); } /* Now we have a pipe with the correct end inheritable. The other end should have a giochannel. */ filedes[1 - inherit_idx] = new_channel_from_fd (fds[1 - inherit_idx]); if (filedes[1 - inherit_idx] < 0) { int saved_errno = errno; _gpgme_io_close (fds[inherit_idx]); _close (fds[1 - inherit_idx]); errno = saved_errno; return TRACE_SYSRES (-1); } return TRACE_SUC5 ("read=0x%x/%p, write=0x%x/%p, channel=%p", filedes[0], (HANDLE) _get_osfhandle (giochannel_table[filedes[0]].fd), filedes[1], (HANDLE) _get_osfhandle (giochannel_table[filedes[1]].fd), giochannel_table[1 - inherit_idx].chan); } int _gpgme_io_close (int fd) { TRACE_BEG (DEBUG_SYSIO, "_gpgme_io_close", fd); if (fd < 0 || fd >= MAX_SLAFD) { errno = EBADF; return TRACE_SYSRES (-1); } assert (giochannel_table[fd].used); /* First call the notify handler. */ if (notify_table[fd].handler) { notify_table[fd].handler (fd, notify_table[fd].value); notify_table[fd].handler = NULL; notify_table[fd].value = NULL; } /* Then do the close. */ if (giochannel_table[fd].chan) { if (giochannel_table[fd].primary) g_io_channel_shutdown (giochannel_table[fd].chan, 1, NULL); g_io_channel_unref (giochannel_table[fd].chan); } else { /* Dummy entry, just close. */ assert (giochannel_table[fd].fd != -1); _close (giochannel_table[fd].fd); } giochannel_table[fd].used = 0; giochannel_table[fd].fd = -1; giochannel_table[fd].socket = INVALID_SOCKET; giochannel_table[fd].chan = NULL; giochannel_table[fd].primary = 0; TRACE_SUC (); return 0; } int _gpgme_io_set_close_notify (int fd, _gpgme_close_notify_handler_t handler, void *value) { TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_set_close_notify", fd, "close_handler=%p/%p", handler, value); assert (fd != -1); if (fd < 0 || fd >= (int) DIM (notify_table)) { errno = EINVAL; return TRACE_SYSRES (-1); } notify_table[fd].handler = handler; notify_table[fd].value = value; return TRACE_SYSRES (0); } int _gpgme_io_set_nonblocking (int fd) { GIOChannel *chan; GIOStatus status; TRACE_BEG (DEBUG_SYSIO, "_gpgme_io_set_nonblocking", fd); chan = find_channel (fd); if (!chan) { errno = EIO; return TRACE_SYSRES (-1); } status = g_io_channel_set_flags (chan, g_io_channel_get_flags (chan) | G_IO_FLAG_NONBLOCK, NULL); if (status != G_IO_STATUS_NORMAL) { #if 0 /* glib 1.9.2 does not implement set_flags and returns an error. */ errno = EIO; return TRACE_SYSRES (-1); #else TRACE_LOG1 ("g_io_channel_set_flags failed: status=%d (ignored)", status); #endif } return TRACE_SYSRES (0); } static char * build_commandline (char **argv) { int i; int n = 0; char *buf; char *p; /* We have to quote some things because under Windows the program parses the commandline and does some unquoting. We enclose the whole argument in double-quotes, and escape literal double-quotes as well as backslashes with a backslash. We end up with a trailing space at the end of the line, but that is harmless. */ for (i = 0; argv[i]; i++) { p = argv[i]; /* The leading double-quote. */ n++; while (*p) { /* An extra one for each literal that must be escaped. */ if (*p == '\\' || *p == '"') n++; n++; p++; } /* The trailing double-quote and the delimiter. */ n += 2; } /* And a trailing zero. */ n++; buf = p = malloc (n); if (!buf) return NULL; for (i = 0; argv[i]; i++) { char *argvp = argv[i]; *(p++) = '"'; while (*argvp) { if (*argvp == '\\' || *argvp == '"') *(p++) = '\\'; *(p++) = *(argvp++); } *(p++) = '"'; *(p++) = ' '; } *(p++) = 0; return buf; } int _gpgme_io_spawn (const char *path, char * const argv[], unsigned int flags, struct spawn_fd_item_s *fd_list, void (*atfork) (void *opaque, int reserved), void *atforkvalue, pid_t *r_pid) { SECURITY_ATTRIBUTES sec_attr; PROCESS_INFORMATION pi = { NULL, /* returns process handle */ 0, /* returns primary thread handle */ 0, /* returns pid */ 0 /* returns tid */ }; STARTUPINFO si; int cr_flags = (CREATE_DEFAULT_ERROR_MODE | GetPriorityClass (GetCurrentProcess ())); int i; char **args; char *arg_string; /* FIXME. */ int debug_me = 0; int tmp_fd; char *tmp_name; TRACE_BEG1 (DEBUG_SYSIO, "_gpgme_io_spawn", path, "path=%s", path); i = 0; while (argv[i]) { TRACE_LOG2 ("argv[%2i] = %s", i, argv[i]); i++; } /* We do not inherit any handles by default, and just insert those handles we want the child to have afterwards. But some handle values occur on the command line, and we need to move stdin/out/err to the right location. So we use a wrapper program which gets the information from a temporary file. */ if (_gpgme_mkstemp (&tmp_fd, &tmp_name) < 0) { TRACE_LOG1 ("_gpgme_mkstemp failed: %s", strerror (errno)); return TRACE_SYSRES (-1); } TRACE_LOG1 ("tmp_name = %s", tmp_name); args = calloc (2 + i + 1, sizeof (*args)); args[0] = (char *) _gpgme_get_w32spawn_path (); args[1] = tmp_name; args[2] = path; memcpy (&args[3], &argv[1], i * sizeof (*args)); memset (&sec_attr, 0, sizeof sec_attr); sec_attr.nLength = sizeof sec_attr; sec_attr.bInheritHandle = FALSE; arg_string = build_commandline (args); free (args); if (!arg_string) { close (tmp_fd); DeleteFile (tmp_name); return TRACE_SYSRES (-1); } memset (&si, 0, sizeof si); si.cb = sizeof (si); si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; si.wShowWindow = debug_me ? SW_SHOW : SW_HIDE; si.hStdInput = INVALID_HANDLE_VALUE; si.hStdOutput = INVALID_HANDLE_VALUE; si.hStdError = INVALID_HANDLE_VALUE; cr_flags |= CREATE_SUSPENDED; if ((flags & IOSPAWN_FLAG_DETACHED)) cr_flags |= DETACHED_PROCESS; if (!CreateProcessA (_gpgme_get_w32spawn_path (), arg_string, &sec_attr, /* process security attributes */ &sec_attr, /* thread security attributes */ FALSE, /* inherit handles */ cr_flags, /* creation flags */ NULL, /* environment */ NULL, /* use current drive/directory */ &si, /* startup information */ &pi)) /* returns process information */ { TRACE_LOG1 ("CreateProcess failed: ec=%d", (int) GetLastError ()); free (arg_string); close (tmp_fd); DeleteFile (tmp_name); /* FIXME: Should translate the error code. */ errno = EIO; return TRACE_SYSRES (-1); } free (arg_string); if (flags & IOSPAWN_FLAG_ALLOW_SET_FG) _gpgme_allow_set_foreground_window ((pid_t)pi.dwProcessId); /* Insert the inherited handles. */ for (i = 0; fd_list[i].fd != -1; i++) { HANDLE hd; /* Make it inheritable for the wrapper process. */ if (!DuplicateHandle (GetCurrentProcess(), _get_osfhandle (giochannel_table[fd_list[i].fd].fd), pi.hProcess, &hd, 0, TRUE, DUPLICATE_SAME_ACCESS)) { TRACE_LOG1 ("DuplicateHandle failed: ec=%d", (int) GetLastError ()); TerminateProcess (pi.hProcess, 0); /* Just in case TerminateProcess didn't work, let the process fail on its own. */ ResumeThread (pi.hThread); CloseHandle (pi.hThread); CloseHandle (pi.hProcess); close (tmp_fd); DeleteFile (tmp_name); /* FIXME: Should translate the error code. */ errno = EIO; return TRACE_SYSRES (-1); } /* Return the child name of this handle. */ fd_list[i].peer_name = (int) hd; } /* Write the handle translation information to the temporary file. */ { /* Hold roughly MAX_TRANS quadruplets of 64 bit numbers in hex notation: "0xFEDCBA9876543210" with an extra white space after every quadruplet. 10*(19*4 + 1) - 1 = 769. This plans ahead for a time when a HANDLE is 64 bit. */ #define BUFFER_MAX 800 char line[BUFFER_MAX + 1]; int res; int written; size_t len; if ((flags & IOSPAWN_FLAG_ALLOW_SET_FG)) strcpy (line, "~1 \n"); else strcpy (line, "\n"); for (i = 0; fd_list[i].fd != -1; i++) { /* Strip the newline. */ len = strlen (line) - 1; /* Format is: Local name, stdin/stdout/stderr, peer name, argv idx. */ snprintf (&line[len], BUFFER_MAX - len, "0x%x %d 0x%x %d \n", fd_list[i].fd, fd_list[i].dup_to, fd_list[i].peer_name, fd_list[i].arg_loc); /* Rather safe than sorry. */ line[BUFFER_MAX - 1] = '\n'; line[BUFFER_MAX] = '\0'; } len = strlen (line); written = 0; do { res = write (tmp_fd, &line[written], len - written); if (res > 0) written += res; } while (res > 0 || (res < 0 && errno == EAGAIN)); } close (tmp_fd); /* The temporary file is deleted by the gpgme-w32spawn process (hopefully). */ TRACE_LOG4 ("CreateProcess ready: hProcess=%p, hThread=%p, " "dwProcessID=%d, dwThreadId=%d", pi.hProcess, pi.hThread, (int) pi.dwProcessId, (int) pi.dwThreadId); if (r_pid) *r_pid = (pid_t)pi.dwProcessId; if (ResumeThread (pi.hThread) < 0) TRACE_LOG1 ("ResumeThread failed: ec=%d", (int) GetLastError ()); if (!CloseHandle (pi.hThread)) TRACE_LOG1 ("CloseHandle of thread failed: ec=%d", (int) GetLastError ()); TRACE_LOG1 ("process=%p", pi.hProcess); /* We don't need to wait for the process. */ if (!CloseHandle (pi.hProcess)) TRACE_LOG1 ("CloseHandle of process failed: ec=%d", (int) GetLastError ()); if (! (flags & IOSPAWN_FLAG_NOCLOSE)) { for (i = 0; fd_list[i].fd != -1; i++) _gpgme_io_close (fd_list[i].fd); } for (i = 0; fd_list[i].fd != -1; i++) if (fd_list[i].dup_to == -1) TRACE_LOG3 ("fd[%i] = 0x%x -> 0x%x", i, fd_list[i].fd, fd_list[i].peer_name); else TRACE_LOG4 ("fd[%i] = 0x%x -> 0x%x (std%s)", i, fd_list[i].fd, fd_list[i].peer_name, (fd_list[i].dup_to == 0) ? "in" : ((fd_list[i].dup_to == 1) ? "out" : "err")); return TRACE_SYSRES (0); } /* Select on the list of fds. Returns: -1 = error, 0 = timeout or nothing to select, > 0 = number of signaled fds. */ int _gpgme_io_select (struct io_select_fd_s *fds, size_t nfds, int nonblock) { int npollfds; GPollFD *pollfds; int *pollfds_map; int i; int j; int any; int n; int count; /* Use a 1s timeout. */ int timeout = 1000; void *dbg_help = NULL; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_select", fds, "nfds=%u, nonblock=%u", nfds, nonblock); if (nonblock) timeout = 0; pollfds = calloc (nfds, sizeof *pollfds); if (!pollfds) return -1; pollfds_map = calloc (nfds, sizeof *pollfds_map); if (!pollfds_map) { free (pollfds); return -1; } npollfds = 0; TRACE_SEQ (dbg_help, "select on [ "); any = 0; for (i = 0; i < nfds; i++) { GIOChannel *chan = NULL; if (fds[i].fd == -1) continue; if ((fds[i].for_read || fds[i].for_write) && !(chan = find_channel (fds[i].fd))) { TRACE_ADD1 (dbg_help, "[BAD0x%x ", fds[i].fd); TRACE_END (dbg_help, "]"); assert (!"see log file"); } else if (fds[i].for_read ) { assert(chan); g_io_channel_win32_make_pollfd (chan, G_IO_IN, pollfds + npollfds); pollfds_map[npollfds] = i; TRACE_ADD2 (dbg_help, "r0x%x<%d> ", fds[i].fd, pollfds[npollfds].fd); npollfds++; any = 1; } else if (fds[i].for_write) { assert(chan); g_io_channel_win32_make_pollfd (chan, G_IO_OUT, pollfds + npollfds); pollfds_map[npollfds] = i; TRACE_ADD2 (dbg_help, "w0x%x<%d> ", fds[i].fd, pollfds[npollfds].fd); npollfds++; any = 1; } fds[i].signaled = 0; } TRACE_END (dbg_help, "]"); if (!any) { count = 0; goto leave; } count = g_io_channel_win32_poll (pollfds, npollfds, timeout); if (count < 0) { int saved_errno = errno; errno = saved_errno; goto leave; } TRACE_SEQ (dbg_help, "select OK [ "); if (TRACE_ENABLED (dbg_help)) { for (i = 0; i < npollfds; i++) { if ((pollfds[i].revents & G_IO_IN)) TRACE_ADD1 (dbg_help, "r0x%x ", fds[pollfds_map[i]].fd); if ((pollfds[i].revents & G_IO_OUT)) TRACE_ADD1 (dbg_help, "w0x%x ", fds[pollfds_map[i]].fd); } TRACE_END (dbg_help, "]"); } /* COUNT is used to stop the lop as soon as possible. */ for (n = count, i = 0; i < npollfds && n; i++) { j = pollfds_map[i]; assert (j >= 0 && j < nfds); if (fds[j].fd == -1) ; else if (fds[j].for_read) { if ((pollfds[i].revents & G_IO_IN)) { fds[j].signaled = 1; n--; } } else if (fds[j].for_write) { if ((pollfds[i].revents & G_IO_OUT)) { fds[j].signaled = 1; n--; } } } leave: free (pollfds); free (pollfds_map); return TRACE_SYSRES (count); } int _gpgme_io_dup (int fd) { int newfd; GIOChannel *chan; TRACE_BEG (DEBUG_SYSIO, "_gpgme_io_dup", fd); if (fd < 0 || fd >= MAX_SLAFD || !giochannel_table[fd].used) { errno = EINVAL; return TRACE_SYSRES (-1); } for (newfd = 0; newfd < MAX_SLAFD; newfd++) if (! giochannel_table[newfd].used) break; if (newfd == MAX_SLAFD) { errno = EIO; return TRACE_SYSRES (-1); } chan = giochannel_table[fd].chan; g_io_channel_ref (chan); giochannel_table[newfd].used = 1; giochannel_table[newfd].chan = chan; giochannel_table[newfd].fd = -1; giochannel_table[newfd].socket = INVALID_SOCKET; giochannel_table[newfd].primary = 0; return TRACE_SYSRES (newfd); } static int wsa2errno (int err) { switch (err) { case WSAENOTSOCK: return EINVAL; case WSAEWOULDBLOCK: return EAGAIN; case ERROR_BROKEN_PIPE: return EPIPE; case WSANOTINITIALISED: return ENOSYS; default: return EIO; } } int _gpgme_io_socket (int domain, int type, int proto) { int res; int fd; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_socket", domain, "type=%i, protp=%i", type, proto); res = socket (domain, type, proto); if (res == INVALID_SOCKET) { errno = wsa2errno (WSAGetLastError ()); return TRACE_SYSRES (-1); } fd = new_channel_from_socket (res); if (fd < 0) { int saved_errno = errno; closesocket (res); errno = saved_errno; return TRACE_SYSRES (-1); } TRACE_SUC2 ("fd=%i, socket=0x%x", fd, res); return fd; } int _gpgme_io_connect (int fd, struct sockaddr *addr, int addrlen) { GIOChannel *chan; int sockfd; int res; GIOFlags flags; GIOStatus status; GError *err = NULL; TRACE_BEG2 (DEBUG_SYSIO, "_gpgme_io_connect", fd, "addr=%p, addrlen=%i", addr, addrlen); chan = find_channel (fd); if (! chan) { errno = EINVAL; return TRACE_SYSRES (-1); } flags = g_io_channel_get_flags (chan); if (flags & G_IO_FLAG_NONBLOCK) { status = g_io_channel_set_flags (chan, flags & ~G_IO_FLAG_NONBLOCK, &err); if (err) { TRACE_LOG1 ("setting flags error: %s", err->message); g_error_free (err); err = NULL; } if (status != G_IO_STATUS_NORMAL) { errno = EIO; return TRACE_SYSRES (-1); } } sockfd = giochannel_table[fd].socket; if (sockfd == INVALID_SOCKET) { errno = EINVAL; return TRACE_SYSRES (-1); } TRACE_LOG1 ("connect sockfd=0x%x", sockfd); res = connect (sockfd, addr, addrlen); /* FIXME: Error ignored here. */ if (! (flags & G_IO_FLAG_NONBLOCK)) g_io_channel_set_flags (chan, flags, NULL); if (res) { TRACE_LOG2 ("connect failed: %i %i", res, WSAGetLastError ()); errno = wsa2errno (WSAGetLastError ()); return TRACE_SYSRES (-1); } return TRACE_SUC (); } diff --git a/src/w32-util.c b/src/w32-util.c index 798f42b3..7992b618 100644 --- a/src/w32-util.c +++ b/src/w32-util.c @@ -1,794 +1,795 @@ /* w32-util.c - Utility functions for the W32 API * Copyright (C) 1999 Free Software Foundation, Inc * Copyright (C) 2001 Werner Koch (dd9jn) * Copyright (C) 2001, 2002, 2003, 2004, 2007, 2013 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . - **/ + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #ifdef HAVE_SYS_TIME_H # include #endif #ifdef HAVE_SYS_TYPES_H # include #endif #ifdef HAVE_SYS_STAT_H # include #endif #ifdef HAVE_UNISTD_H # include #endif #include #include #if __MINGW64_VERSION_MAJOR >= 2 # define _WIN32_IE 0x0501 /* Required by mingw64 toolkit. */ #else # define _WIN32_IE 0x0400 /* Required for SHGetSpecialFolderPathA. */ #endif /* We need to include the windows stuff here prior to shlobj.h so that we get the right winsock version. This is usually done in util.h but that header also redefines some Windows functions which we need to avoid unless having included shlobj.h. */ #include #include #include #include #include "util.h" #include "ath.h" #include "sema.h" #include "debug.h" #include "sys-util.h" #define HAVE_ALLOW_SET_FOREGROUND_WINDOW 1 #ifndef F_OK # define F_OK 0 #endif /* The Registry key used by GNUPG. */ #ifdef _WIN64 # define GNUPG_REGKEY_2 "Software\\Wow6432Node\\GNU\\GnuPG" #else # define GNUPG_REGKEY_2 "Software\\GNU\\GnuPG" #endif #ifdef _WIN64 # define GNUPG_REGKEY_3 "Software\\Wow6432Node\\GnuPG" #else # define GNUPG_REGKEY_3 "Software\\GnuPG" #endif DEFINE_STATIC_LOCK (get_path_lock); /* The module handle of this DLL. If we are linked statically, dllmain does not exists and thus the value of my_hmodule will be NULL. The effect is that a GetModuleFileName always returns the file name of the DLL or executable which contains the gpgme code. */ static HMODULE my_hmodule; /* These variables store the malloced name of alternative default binaries. The are set only once by gpgme_set_global_flag. */ static char *default_gpg_name; static char *default_gpgconf_name; /* If this variable is not NULL the value is assumed to be the installation directory. The variable may only be set once by gpgme_set_global_flag and accessed by _gpgme_get_inst_dir. */ static char *override_inst_dir; #define RTLD_LAZY 0 static GPG_ERR_INLINE void * dlopen (const char * name, int flag) { void * hd = LoadLibrary (name); (void)flag; return hd; } static GPG_ERR_INLINE void * dlsym (void * hd, const char * sym) { if (hd && sym) { void * fnc = GetProcAddress (hd, sym); if (!fnc) return NULL; return fnc; } return NULL; } static GPG_ERR_INLINE int dlclose (void * hd) { if (hd) { FreeLibrary (hd); return 0; } return -1; } /* Return a malloced string encoded in UTF-8 from the wide char input string STRING. Caller must free this value. Returns NULL and sets ERRNO on failure. Calling this function with STRING set to NULL is not defined. */ static char * wchar_to_utf8 (const wchar_t *string) { int n; char *result; n = WideCharToMultiByte (CP_UTF8, 0, string, -1, NULL, 0, NULL, NULL); if (n < 0) { gpg_err_set_errno (EINVAL); return NULL; } result = malloc (n+1); if (!result) return NULL; n = WideCharToMultiByte (CP_UTF8, 0, string, -1, result, n, NULL, NULL); if (n < 0) { free (result); gpg_err_set_errno (EINVAL); result = NULL; } return result; } /* Replace all forward slashes by backslashes. */ static void replace_slashes (char *string) { for (; *string; string++) if (*string == '/') *string = '\\'; } /* Get the base name of NAME. Returns a pointer into NAME right after the last slash or backslash or to NAME if no slash or backslash exists. */ static const char * get_basename (const char *name) { const char *mark, *s; for (mark=NULL, s=name; *s; s++) if (*s == '/' || *s == '\\') mark = s; return mark? mark+1 : name; } void _gpgme_allow_set_foreground_window (pid_t pid) { #ifdef HAVE_ALLOW_SET_FOREGROUND_WINDOW static int initialized; static BOOL (WINAPI * func)(DWORD); void *handle; if (!initialized) { /* Available since W2000; thus we dynload it. */ initialized = 1; handle = dlopen ("user32.dll", RTLD_LAZY); if (handle) { func = dlsym (handle, "AllowSetForegroundWindow"); if (!func) { dlclose (handle); handle = NULL; } } } if (!pid || pid == (pid_t)(-1)) { TRACE1 (DEBUG_ENGINE, "gpgme:AllowSetForegroundWindow", 0, "no action for pid %d", (int)pid); } else if (func) { int rc = func (pid); TRACE2 (DEBUG_ENGINE, "gpgme:AllowSetForegroundWindow", 0, "called for pid %d; result=%d", (int)pid, rc); } else { TRACE0 (DEBUG_ENGINE, "gpgme:AllowSetForegroundWindow", 0, "function not available"); } #endif /* HAVE_ALLOW_SET_FOREGROUND_WINDOW */ } /* Wrapper around CancelSynchronousIo which is only available since * Vista. */ void _gpgme_w32_cancel_synchronous_io (HANDLE thread) { static int initialized; static BOOL (WINAPI * func)(DWORD); void *handle; if (!initialized) { /* Available since Vista; thus we dynload it. */ initialized = 1; handle = dlopen ("kernel32.dll", RTLD_LAZY); if (handle) { func = dlsym (handle, "CancelSynchronousIo"); if (!func) { dlclose (handle); handle = NULL; } } } if (func) { if (!func (thread) && GetLastError() != ERROR_NOT_FOUND) { TRACE2 (DEBUG_ENGINE, "gpgme:CancelSynchronousIo", 0, "called for thread %p: ec=%d", thread, GetLastError ()); } } else { TRACE0 (DEBUG_ENGINE, "gpgme:CancelSynchronousIo", 0, "function not available"); } } /* Return a string from the W32 Registry or NULL in case of error. Caller must release the return value. A NULL for root is an alias for HKEY_CURRENT_USER, HKEY_LOCAL_MACHINE in turn. */ static char * read_w32_registry_string (const char *root, const char *dir, const char *name) { HKEY root_key, key_handle; DWORD n1, nbytes, type; char *result = NULL; if (!root) root_key = HKEY_CURRENT_USER; else if (!strcmp( root, "HKEY_CLASSES_ROOT")) root_key = HKEY_CLASSES_ROOT; else if (!strcmp( root, "HKEY_CURRENT_USER")) root_key = HKEY_CURRENT_USER; else if (!strcmp( root, "HKEY_LOCAL_MACHINE")) root_key = HKEY_LOCAL_MACHINE; else if (!strcmp( root, "HKEY_USERS")) root_key = HKEY_USERS; else if (!strcmp( root, "HKEY_PERFORMANCE_DATA")) root_key = HKEY_PERFORMANCE_DATA; else if (!strcmp( root, "HKEY_CURRENT_CONFIG")) root_key = HKEY_CURRENT_CONFIG; else return NULL; if (RegOpenKeyExA (root_key, dir, 0, KEY_READ, &key_handle)) { if (root) return NULL; /* no need for a RegClose, so return direct */ /* It seems to be common practise to fall back to HKLM. */ if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle)) return NULL; /* still no need for a RegClose, so return direct */ } nbytes = 1; if (RegQueryValueExA (key_handle, name, 0, NULL, NULL, &nbytes)) { if (root) goto leave; /* Try to fallback to HKLM also vor a missing value. */ RegCloseKey (key_handle); if (RegOpenKeyExA (HKEY_LOCAL_MACHINE, dir, 0, KEY_READ, &key_handle)) return NULL; /* Nope. */ if (RegQueryValueExA (key_handle, name, 0, NULL, NULL, &nbytes)) goto leave; } n1 = nbytes + 1; result = malloc (n1); if (!result) goto leave; if (RegQueryValueExA (key_handle, name, 0, &type, (LPBYTE) result, &n1)) { free (result); result = NULL; goto leave; } result[nbytes] = 0; /* Make sure it is really a string. */ leave: RegCloseKey (key_handle); return result; } /* Return the name of the directory with the gpgme DLL or the EXE (if statically linked). May return NULL on severe errors. */ const char * _gpgme_get_inst_dir (void) { static char *inst_dir; if (override_inst_dir) return override_inst_dir; LOCK (get_path_lock); if (!inst_dir) { wchar_t *moddir; moddir = malloc ((MAX_PATH+5) * sizeof *moddir); if (moddir) { if (!GetModuleFileNameW (my_hmodule, moddir, MAX_PATH)) *moddir = 0; if (!*moddir) gpg_err_set_errno (ENOENT); else { inst_dir = wchar_to_utf8 (moddir); if (inst_dir) { char *p = strrchr (inst_dir, '\\'); if (p) *p = 0; } } free (moddir); } } UNLOCK (get_path_lock); return inst_dir; } static char * find_program_in_dir (const char *dir, const char *name) { char *result; result = _gpgme_strconcat (dir, "\\", name, NULL); if (!result) return NULL; if (access (result, F_OK)) { free (result); return NULL; } return result; } static char * find_program_at_standard_place (const char *name) { char path[MAX_PATH]; char *result = NULL; /* See https://wiki.tcl-lang.org/page/Getting+Windows+%22special+folders%22+with+Ffidl for details on compatibility. We First try the generic place and then fallback to the x86 (i.e. 32 bit) place. This will prefer a 64 bit of the program over a 32 bit version on 64 bit Windows if installed. */ if (SHGetSpecialFolderPathA (NULL, path, CSIDL_PROGRAM_FILES, 0)) { result = _gpgme_strconcat (path, "\\", name, NULL); if (result && access (result, F_OK)) { free (result); result = NULL; } } if (!result && SHGetSpecialFolderPathA (NULL, path, CSIDL_PROGRAM_FILESX86, 0)) { result = _gpgme_strconcat (path, "\\", name, NULL); if (result && access (result, F_OK)) { free (result); result = NULL; } } return result; } /* Set the default name for the gpg binary. This function may only be called by gpgme_set_global_flag. Returns 0 on success. */ int _gpgme_set_default_gpg_name (const char *name) { if (!default_gpg_name) { default_gpg_name = _gpgme_strconcat (name, ".exe", NULL); if (default_gpg_name) replace_slashes (default_gpg_name); } return !default_gpg_name; } /* Set the default name for the gpgconf binary. This function may only be called by gpgme_set_global_flag. Returns 0 on success. */ int _gpgme_set_default_gpgconf_name (const char *name) { if (!default_gpgconf_name) { default_gpgconf_name = _gpgme_strconcat (name, ".exe", NULL); if (default_gpgconf_name) replace_slashes (default_gpgconf_name); } return !default_gpgconf_name; } /* Set the override installation directory. This function may only be called by gpgme_set_global_flag. Returns 0 on success. */ int _gpgme_set_override_inst_dir (const char *dir) { if (!override_inst_dir) { override_inst_dir = strdup (dir); if (override_inst_dir) { replace_slashes (override_inst_dir); /* Remove a trailing slash. */ if (*override_inst_dir && override_inst_dir[strlen (override_inst_dir)-1] == '\\') override_inst_dir[strlen (override_inst_dir)-1] = 0; } } return !override_inst_dir; } /* Return the full file name of the GPG binary. This function is used iff gpgconf was not found and thus it can be assumed that gpg2 is not installed. This function is only called by get_gpgconf_item and may not be called concurrently. */ char * _gpgme_get_gpg_path (void) { char *gpg = NULL; const char *name, *inst_dir; name = default_gpg_name? get_basename (default_gpg_name) : "gpg.exe"; /* 1. Try to find gpg.exe in the installation directory of gpgme. */ inst_dir = _gpgme_get_inst_dir (); if (inst_dir) { gpg = find_program_in_dir (inst_dir, name); } /* 2. Try to find gpg.exe using that ancient registry key. */ if (!gpg) { char *dir; dir = read_w32_registry_string ("HKEY_LOCAL_MACHINE", GNUPG_REGKEY_2, "Install Directory"); if (dir) { gpg = find_program_in_dir (dir, name); free (dir); } } /* 3. Try to find gpg.exe below CSIDL_PROGRAM_FILES. */ if (!gpg) { name = default_gpg_name? default_gpg_name : "GNU\\GnuPG\\gpg.exe"; gpg = find_program_at_standard_place (name); } /* 4. Print a debug message if not found. */ if (!gpg) _gpgme_debug (DEBUG_ENGINE, "_gpgme_get_gpg_path: '%s' not found", name); return gpg; } /* This function is only called by get_gpgconf_item and may not be called concurrently. */ char * _gpgme_get_gpgconf_path (void) { char *gpgconf = NULL; const char *inst_dir, *name; name = default_gpgconf_name? get_basename(default_gpgconf_name):"gpgconf.exe"; /* 1. Try to find gpgconf.exe in the installation directory of gpgme. */ inst_dir = _gpgme_get_inst_dir (); if (inst_dir) { gpgconf = find_program_in_dir (inst_dir, name); } /* 2. Try to find gpgconf.exe from GnuPG >= 2.1 below CSIDL_PROGRAM_FILES. */ if (!gpgconf) { const char *name2 = (default_gpgconf_name ? default_gpgconf_name /**/ : "GnuPG\\bin\\gpgconf.exe"); gpgconf = find_program_at_standard_place (name2); } /* 3. Try to find gpgconf.exe using the Windows registry. */ if (!gpgconf) { char *dir; dir = read_w32_registry_string (NULL, GNUPG_REGKEY_2, "Install Directory"); if (!dir) { char *tmp = read_w32_registry_string (NULL, GNUPG_REGKEY_3, "Install Directory"); if (tmp) { dir = _gpgme_strconcat (tmp, "\\bin", NULL); free (tmp); if (!dir) return NULL; } } if (dir) { gpgconf = find_program_in_dir (dir, name); free (dir); } } /* 4. Try to find gpgconf.exe from Gpg4win below CSIDL_PROGRAM_FILES. */ if (!gpgconf) { gpgconf = find_program_at_standard_place ("GNU\\GnuPG\\gpgconf.exe"); } /* 5. Try to find gpgconf.exe relative to us. */ if (!gpgconf && inst_dir) { char *dir = _gpgme_strconcat (inst_dir, "\\..\\..\\GnuPG\\bin"); gpgconf = find_program_in_dir (dir, name); free (dir); } /* 5. Print a debug message if not found. */ if (!gpgconf) _gpgme_debug (DEBUG_ENGINE, "_gpgme_get_gpgconf_path: '%s' not found",name); return gpgconf; } const char * _gpgme_get_w32spawn_path (void) { static char *w32spawn_program; const char *inst_dir; inst_dir = _gpgme_get_inst_dir (); LOCK (get_path_lock); if (!w32spawn_program) w32spawn_program = find_program_in_dir (inst_dir, "gpgme-w32spawn.exe"); UNLOCK (get_path_lock); return w32spawn_program; } /* Return an integer value from gpgme specific configuration entries. VALUE receives that value; function returns true if a value has been configured and false if not. */ int _gpgme_get_conf_int (const char *key, int *value) { char *tmp = read_w32_registry_string (NULL, "Software\\GNU\\gpgme", key); if (!tmp) return 0; *value = atoi (tmp); free (tmp); return 1; } /* mkstemp extracted from libc/sysdeps/posix/tempname.c. Copyright (C) 1991-1999, 2000, 2001, 2006 Free Software Foundation, Inc. The GNU C Library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. */ static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; /* Generate a temporary file name based on TMPL. TMPL must match the rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed does not exist at the time of the call to mkstemp. TMPL is overwritten with the result. */ static int my_mkstemp (char *tmpl) { int len; char *XXXXXX; static uint64_t value; uint64_t random_time_bits; unsigned int count; int fd = -1; int save_errno = errno; /* A lower bound on the number of temporary files to attempt to generate. The maximum total number of temporary file names that can exist for a given template is 62**6. It should never be necessary to try all these combinations. Instead if a reasonable number of names is tried (we define reasonable as 62**3) fail to give the system administrator the chance to remove the problems. */ #define ATTEMPTS_MIN (62 * 62 * 62) /* The number of times to attempt to generate a temporary file. To conform to POSIX, this must be no smaller than TMP_MAX. */ #if ATTEMPTS_MIN < TMP_MAX unsigned int attempts = TMP_MAX; #else unsigned int attempts = ATTEMPTS_MIN; #endif len = strlen (tmpl); if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX")) { gpg_err_set_errno (EINVAL); return -1; } /* This is where the Xs start. */ XXXXXX = &tmpl[len - 6]; /* Get some more or less random data. */ { FILETIME ft; GetSystemTimeAsFileTime (&ft); random_time_bits = (((uint64_t)ft.dwHighDateTime << 32) | (uint64_t)ft.dwLowDateTime); } value += random_time_bits ^ ath_self (); for (count = 0; count < attempts; value += 7777, ++count) { uint64_t v = value; /* Fill in the random bits. */ XXXXXX[0] = letters[v % 62]; v /= 62; XXXXXX[1] = letters[v % 62]; v /= 62; XXXXXX[2] = letters[v % 62]; v /= 62; XXXXXX[3] = letters[v % 62]; v /= 62; XXXXXX[4] = letters[v % 62]; v /= 62; XXXXXX[5] = letters[v % 62]; fd = open (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); if (fd >= 0) { gpg_err_set_errno (save_errno); return fd; } else if (errno != EEXIST) return -1; } /* We got out of the loop because we ran out of combinations to try. */ gpg_err_set_errno (EEXIST); return -1; } int _gpgme_mkstemp (int *fd, char **name) { char tmp[MAX_PATH + 2]; char *tmpname; int err; *fd = -1; *name = NULL; err = GetTempPathA (MAX_PATH + 1, tmp); if (err == 0 || err > MAX_PATH + 1) strcpy (tmp,"c:\\windows\\temp"); else { int len = strlen(tmp); /* GetTempPath may return with \ on the end */ while(len > 0 && tmp[len - 1] == '\\') { tmp[len-1] = '\0'; len--; } } tmpname = _gpgme_strconcat (tmp, "\\gpgme-XXXXXX", NULL); if (!tmpname) return -1; *fd = my_mkstemp (tmpname); if (*fd < 0) { free (tmpname); return -1; } *name = tmpname; return 0; } /* Entry point called by the DLL loader. */ #ifdef DLL_EXPORT int WINAPI DllMain (HINSTANCE hinst, DWORD reason, LPVOID reserved) { (void)reserved; if (reason == DLL_PROCESS_ATTACH) my_hmodule = hinst; return TRUE; } #endif /*DLL_EXPORT*/ diff --git a/src/wait-global.c b/src/wait-global.c index 28f3921f..e88962e0 100644 --- a/src/wait-global.c +++ b/src/wait-global.c @@ -1,401 +1,401 @@ /* wait-global.c - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #include "gpgme.h" #include "sema.h" #include "util.h" #include "context.h" #include "wait.h" #include "priv-io.h" #include "ops.h" #include "debug.h" /* The global event loop is used for all asynchronous operations (except key listing) for which no user I/O callbacks are specified. A context sets up its initial I/O callbacks and then sends the GPGME_EVENT_START event. After that, it is added to the global list of active contexts. The gpgme_wait function contains a select() loop over all file descriptors in all active contexts. If an error occurs, it closes all fds in that context and moves the context to the global done list. Likewise, if a context has removed all I/O callbacks, it is moved to the global done list. All contexts in the global done list are eligible for being returned by gpgme_wait if requested by the caller. */ /* The ctx_list_lock protects the list of active and done contexts. Insertion into any of these lists is only allowed when the lock is held. This allows a muli-threaded program to loop over gpgme_wait and in parallel start asynchronous gpgme operations. However, the fd tables in the contexts are not protected by this lock. They are only allowed to change either before the context is added to the active list (ie, before the start event is signalled) or in a callback handler. */ DEFINE_STATIC_LOCK (ctx_list_lock); /* A ctx_list_item is an item in the global list of active or done contexts. */ struct ctx_list_item { /* Every ctx_list_item is an element in a doubly linked list. The list pointers are protected by the ctx_list_lock. */ struct ctx_list_item *next; struct ctx_list_item *prev; gpgme_ctx_t ctx; /* The status is set when the ctx is moved to the done list. */ gpgme_error_t status; gpgme_error_t op_err; }; /* The active list contains all contexts that are in the global event loop, have active I/O callbacks, and have already seen the start event. */ static struct ctx_list_item *ctx_active_list; /* The done list contains all contexts that have previously been active but now are not active any longer, either because they finished successfully or an I/O callback returned an error. The status field in the list item contains the error value (or 0 if successful). */ static struct ctx_list_item *ctx_done_list; /* Enter the context CTX into the active list. */ static gpgme_error_t ctx_active (gpgme_ctx_t ctx) { struct ctx_list_item *li = malloc (sizeof (struct ctx_list_item)); if (!li) return gpg_error_from_syserror (); li->ctx = ctx; LOCK (ctx_list_lock); /* Add LI to active list. */ li->next = ctx_active_list; li->prev = NULL; if (ctx_active_list) ctx_active_list->prev = li; ctx_active_list = li; UNLOCK (ctx_list_lock); return 0; } /* Enter the context CTX into the done list with status STATUS. */ static void ctx_done (gpgme_ctx_t ctx, gpgme_error_t status, gpgme_error_t op_err) { struct ctx_list_item *li; LOCK (ctx_list_lock); li = ctx_active_list; while (li && li->ctx != ctx) li = li->next; assert (li); /* Remove LI from active list. */ if (li->next) li->next->prev = li->prev; if (li->prev) li->prev->next = li->next; else ctx_active_list = li->next; li->status = status; li->op_err = op_err; /* Add LI to done list. */ li->next = ctx_done_list; li->prev = NULL; if (ctx_done_list) ctx_done_list->prev = li; ctx_done_list = li; UNLOCK (ctx_list_lock); } /* Find finished context CTX (or any context if CTX is NULL) and return its status in STATUS after removing it from the done list. If a matching context could be found, return it. Return NULL if no context could be found. */ static gpgme_ctx_t ctx_wait (gpgme_ctx_t ctx, gpgme_error_t *status, gpgme_error_t *op_err) { struct ctx_list_item *li; LOCK (ctx_list_lock); li = ctx_done_list; if (ctx) { /* A specific context is requested. */ while (li && li->ctx != ctx) li = li->next; } if (li) { ctx = li->ctx; if (status) *status = li->status; if (op_err) *op_err = li->op_err; /* Remove LI from done list. */ if (li->next) li->next->prev = li->prev; if (li->prev) li->prev->next = li->next; else ctx_done_list = li->next; free (li); } else ctx = NULL; UNLOCK (ctx_list_lock); return ctx; } /* Internal I/O callback functions. */ /* The add_io_cb and remove_io_cb handlers are shared with the private event loops. */ void _gpgme_wait_global_event_cb (void *data, gpgme_event_io_t type, void *type_data) { gpgme_ctx_t ctx = (gpgme_ctx_t) data; assert (ctx); switch (type) { case GPGME_EVENT_START: { gpgme_error_t err = ctx_active (ctx); if (err) /* An error occurred. Close all fds in this context, and send the error in a done event. */ _gpgme_cancel_with_err (ctx, err, 0); } break; case GPGME_EVENT_DONE: { gpgme_io_event_done_data_t done_data = (gpgme_io_event_done_data_t) type_data; ctx_done (ctx, done_data->err, done_data->op_err); } break; case GPGME_EVENT_NEXT_KEY: assert (!"Unexpected event GPGME_EVENT_NEXT_KEY"); break; case GPGME_EVENT_NEXT_TRUSTITEM: assert (!"Unexpected event GPGME_EVENT_NEXT_TRUSTITEM"); break; default: assert (!"Unexpected event"); break; } } /* Perform asynchronous operations in the global event loop (ie, any asynchronous operation except key listing and trustitem listing operations). If CTX is not a null pointer, the function will return if the asynchronous operation in the context CTX finished. Otherwise the function will return if any asynchronous operation finished. If HANG is zero, the function will not block for a long time. Otherwise the function does not return until an operation matching CTX finished. If a matching context finished, it is returned, and *STATUS is set to the error value of the operation in that context. Otherwise, if the timeout expires, NULL is returned and *STATUS is 0. If an error occurs, NULL is returned and *STATUS is set to the error value. */ gpgme_ctx_t gpgme_wait_ext (gpgme_ctx_t ctx, gpgme_error_t *status, gpgme_error_t *op_err, int hang) { do { unsigned int i = 0; struct ctx_list_item *li; struct fd_table fdt; int nr; /* Collect the active file descriptors. */ LOCK (ctx_list_lock); for (li = ctx_active_list; li; li = li->next) i += li->ctx->fdt.size; fdt.fds = malloc (i * sizeof (struct io_select_fd_s)); if (!fdt.fds) { int saved_err = gpg_error_from_syserror (); UNLOCK (ctx_list_lock); if (status) *status = saved_err; if (op_err) *op_err = 0; return NULL; } fdt.size = i; i = 0; for (li = ctx_active_list; li; li = li->next) { memcpy (&fdt.fds[i], li->ctx->fdt.fds, li->ctx->fdt.size * sizeof (struct io_select_fd_s)); i += li->ctx->fdt.size; } UNLOCK (ctx_list_lock); nr = _gpgme_io_select (fdt.fds, fdt.size, 0); if (nr < 0) { int saved_err = gpg_error_from_syserror (); free (fdt.fds); if (status) *status = saved_err; if (op_err) *op_err = 0; return NULL; } for (i = 0; i < fdt.size && nr; i++) { if (fdt.fds[i].fd != -1 && fdt.fds[i].signaled) { gpgme_ctx_t ictx; gpgme_error_t err = 0; gpgme_error_t local_op_err = 0; struct wait_item_s *item; assert (nr); nr--; item = (struct wait_item_s *) fdt.fds[i].opaque; assert (item); ictx = item->ctx; assert (ictx); LOCK (ctx->lock); if (ctx->canceled) err = gpg_error (GPG_ERR_CANCELED); UNLOCK (ctx->lock); if (!err) err = _gpgme_run_io_cb (&fdt.fds[i], 0, &local_op_err); if (err || local_op_err) { /* An error occurred. Close all fds in this context, and signal it. */ _gpgme_cancel_with_err (ictx, err, local_op_err); /* Break out of the loop, and retry the select() from scratch, because now all fds should be gone. */ break; } } } free (fdt.fds); /* Now some contexts might have finished successfully. */ LOCK (ctx_list_lock); retry: for (li = ctx_active_list; li; li = li->next) { gpgme_ctx_t actx = li->ctx; for (i = 0; i < actx->fdt.size; i++) if (actx->fdt.fds[i].fd != -1) break; if (i == actx->fdt.size) { struct gpgme_io_event_done_data data; data.err = 0; data.op_err = 0; /* FIXME: This does not perform too well. We have to release the lock because the I/O event handler acquires it to remove the context from the active list. Two alternative strategies are worth considering: Either implement the DONE event handler here in a lock-free manner, or save a list of all contexts to be released and call the DONE events afterwards. */ UNLOCK (ctx_list_lock); _gpgme_engine_io_event (actx->engine, GPGME_EVENT_DONE, &data); LOCK (ctx_list_lock); goto retry; } } UNLOCK (ctx_list_lock); { gpgme_ctx_t dctx = ctx_wait (ctx, status, op_err); if (dctx) { ctx = dctx; hang = 0; } else if (!hang) { ctx = NULL; if (status) *status = 0; if (op_err) *op_err = 0; } } } while (hang); return ctx; } gpgme_ctx_t gpgme_wait (gpgme_ctx_t ctx, gpgme_error_t *status, int hang) { return gpgme_wait_ext (ctx, status, NULL, hang); } diff --git a/src/wait-private.c b/src/wait-private.c index 12d31805..417cb2b3 100644 --- a/src/wait-private.c +++ b/src/wait-private.c @@ -1,180 +1,180 @@ /* wait-private.c - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include "gpgme.h" #include "context.h" #include "wait.h" #include "ops.h" #include "priv-io.h" #include "util.h" #include "debug.h" /* The private event loops are used for all blocking operations, and for the key and trust item listing operations. They are completely separated from each other. */ /* Internal I/O callback functions. */ /* The add_io_cb and remove_io_cb handlers are shared with the global event loops. */ void _gpgme_wait_private_event_cb (void *data, gpgme_event_io_t type, void *type_data) { switch (type) { case GPGME_EVENT_START: /* Nothing to do here, as the wait routine is called after the initialization is finished. */ break; case GPGME_EVENT_DONE: break; case GPGME_EVENT_NEXT_KEY: _gpgme_op_keylist_event_cb (data, type, type_data); break; case GPGME_EVENT_NEXT_TRUSTITEM: _gpgme_op_trustlist_event_cb (data, type, type_data); break; } } /* If COND is a null pointer, wait until the blocking operation in CTX finished and return its error value. Otherwise, wait until COND is satisfied or the operation finished. */ gpgme_error_t _gpgme_wait_on_condition (gpgme_ctx_t ctx, volatile int *cond, gpgme_error_t *op_err_p) { gpgme_error_t err = 0; int hang = 1; if (op_err_p) *op_err_p = 0; do { int nr = _gpgme_io_select (ctx->fdt.fds, ctx->fdt.size, 0); unsigned int i; if (nr < 0) { /* An error occurred. Close all fds in this context, and signal it. */ err = gpg_error_from_syserror (); _gpgme_cancel_with_err (ctx, err, 0); return err; } for (i = 0; i < ctx->fdt.size && nr; i++) { if (ctx->fdt.fds[i].fd != -1 && ctx->fdt.fds[i].signaled) { gpgme_error_t op_err = 0; ctx->fdt.fds[i].signaled = 0; assert (nr); nr--; LOCK (ctx->lock); if (ctx->canceled) err = gpg_error (GPG_ERR_CANCELED); UNLOCK (ctx->lock); if (!err) err = _gpgme_run_io_cb (&ctx->fdt.fds[i], 0, &op_err); if (err) { /* An error occurred. Close all fds in this context, and signal it. */ _gpgme_cancel_with_err (ctx, err, 0); return err; } else if (op_err) { /* An operational error occurred. Cancel the current operation but not the session, and signal it. */ _gpgme_cancel_with_err (ctx, 0, op_err); /* NOTE: This relies on the operational error being generated after the operation really has completed, for example after no further status line output is generated. Otherwise the following I/O will spill over into the next operation. */ if (op_err_p) *op_err_p = op_err; return 0; } } } for (i = 0; i < ctx->fdt.size; i++) if (ctx->fdt.fds[i].fd != -1) break; if (i == ctx->fdt.size) { struct gpgme_io_event_done_data data; data.err = 0; data.op_err = 0; _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &data); hang = 0; } if (cond && *cond) hang = 0; } while (hang); return 0; } /* Wait until the blocking operation in context CTX has finished and return the error value. This variant can not be used for session-based protocols. */ gpgme_error_t _gpgme_wait_one (gpgme_ctx_t ctx) { return _gpgme_wait_on_condition (ctx, NULL, NULL); } /* Wait until the blocking operation in context CTX has finished and return the error value. This is the right variant to use for sesion-based protocols. */ gpgme_error_t _gpgme_wait_one_ext (gpgme_ctx_t ctx, gpgme_error_t *op_err) { return _gpgme_wait_on_condition (ctx, NULL, op_err); } diff --git a/src/wait-user.c b/src/wait-user.c index c7bc80f8..2a42170c 100644 --- a/src/wait-user.c +++ b/src/wait-user.c @@ -1,133 +1,133 @@ /* wait-user.c - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include "gpgme.h" #include "context.h" #include "priv-io.h" #include "wait.h" #include "ops.h" #include "debug.h" /* The user event loops are used for all asynchronous operations for which a user callback is defined. */ /* Internal I/O Callbacks. */ gpgme_error_t _gpgme_user_io_cb_handler (void *data, int fd) { gpgme_error_t err = 0; gpgme_error_t op_err = 0; struct tag *tag = (struct tag *) data; gpgme_ctx_t ctx; (void)fd; assert (data); ctx = tag->ctx; assert (ctx); LOCK (ctx->lock); if (ctx->canceled) err = gpg_error (GPG_ERR_CANCELED); UNLOCK (ctx->lock); if (! err) err = _gpgme_run_io_cb (&ctx->fdt.fds[tag->idx], 0, &op_err); if (err || op_err) _gpgme_cancel_with_err (ctx, err, op_err); else { unsigned int i; for (i = 0; i < ctx->fdt.size; i++) if (ctx->fdt.fds[i].fd != -1) break; if (i == ctx->fdt.size) { struct gpgme_io_event_done_data done_data; done_data.err = 0; done_data.op_err = 0; _gpgme_engine_io_event (ctx->engine, GPGME_EVENT_DONE, &done_data); } } return 0; } /* Register the file descriptor FD with the handler FNC (which gets FNC_DATA as its first argument) for the direction DIR. DATA should be the context for which the fd is added. R_TAG will hold the tag that can be used to remove the fd. */ gpgme_error_t _gpgme_wait_user_add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **r_tag) { gpgme_ctx_t ctx = (gpgme_ctx_t) data; struct tag *tag; gpgme_error_t err; assert (ctx); err = _gpgme_add_io_cb (data, fd, dir, fnc, fnc_data, r_tag); if (err) return err; tag = *r_tag; assert (tag); err = (*ctx->io_cbs.add) (ctx->io_cbs.add_priv, fd, dir, _gpgme_user_io_cb_handler, *r_tag, &tag->user_tag); if (err) _gpgme_remove_io_cb (*r_tag); return err; } void _gpgme_wait_user_remove_io_cb (void *data) { struct tag *tag = (struct tag *) data; gpgme_ctx_t ctx; assert (tag); ctx = tag->ctx; (*ctx->io_cbs.remove) (tag->user_tag); _gpgme_remove_io_cb (data); } void _gpgme_wait_user_event_cb (void *data, gpgme_event_io_t type, void *type_data) { gpgme_ctx_t ctx = data; if (ctx->io_cbs.event) (*ctx->io_cbs.event) (ctx->io_cbs.event_priv, type, type_data); } diff --git a/src/wait.c b/src/wait.c index 20b23032..a011b123 100644 --- a/src/wait.c +++ b/src/wait.c @@ -1,222 +1,222 @@ /* wait.c - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #if HAVE_CONFIG_H #include #endif #include #include #include #include #ifdef HAVE_SYS_TYPES_H # include #endif #include "util.h" #include "context.h" #include "ops.h" #include "wait.h" #include "sema.h" #include "priv-io.h" #include "engine.h" #include "debug.h" void _gpgme_fd_table_init (fd_table_t fdt) { fdt->fds = NULL; fdt->size = 0; } void _gpgme_fd_table_deinit (fd_table_t fdt) { if (fdt->fds) free (fdt->fds); } /* XXX We should keep a marker and roll over for speed. */ static gpgme_error_t fd_table_put (fd_table_t fdt, int fd, int dir, void *opaque, int *idx) { unsigned int i, j; struct io_select_fd_s *new_fds; for (i = 0; i < fdt->size; i++) { if (fdt->fds[i].fd == -1) break; } if (i == fdt->size) { #define FDT_ALLOCSIZE 10 new_fds = realloc (fdt->fds, (fdt->size + FDT_ALLOCSIZE) * sizeof (*new_fds)); if (!new_fds) return gpg_error_from_syserror (); fdt->fds = new_fds; fdt->size += FDT_ALLOCSIZE; for (j = 0; j < FDT_ALLOCSIZE; j++) fdt->fds[i + j].fd = -1; } fdt->fds[i].fd = fd; fdt->fds[i].for_read = (dir == 1); fdt->fds[i].for_write = (dir == 0); fdt->fds[i].signaled = 0; fdt->fds[i].opaque = opaque; *idx = i; return 0; } /* Register the file descriptor FD with the handler FNC (which gets FNC_DATA as its first argument) for the direction DIR. DATA should be the context for which the fd is added. R_TAG will hold the tag that can be used to remove the fd. */ gpgme_error_t _gpgme_add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **r_tag) { gpgme_error_t err; gpgme_ctx_t ctx = (gpgme_ctx_t) data; fd_table_t fdt; struct wait_item_s *item; struct tag *tag; assert (fnc); assert (ctx); fdt = &ctx->fdt; assert (fdt); tag = malloc (sizeof *tag); if (!tag) return gpg_error_from_syserror (); tag->ctx = ctx; /* Allocate a structure to hold information about the handler. */ item = calloc (1, sizeof *item); if (!item) { free (tag); return gpg_error_from_syserror (); } item->ctx = ctx; item->dir = dir; item->handler = fnc; item->handler_value = fnc_data; err = fd_table_put (fdt, fd, dir, item, &tag->idx); if (err) { free (tag); free (item); return err; } TRACE3 (DEBUG_CTX, "_gpgme_add_io_cb", ctx, "fd %d, dir=%d -> tag=%p", fd, dir, tag); *r_tag = tag; return 0; } void _gpgme_remove_io_cb (void *data) { struct tag *tag = data; gpgme_ctx_t ctx; fd_table_t fdt; int idx; assert (tag); ctx = tag->ctx; assert (ctx); fdt = &ctx->fdt; assert (fdt); idx = tag->idx; TRACE2 (DEBUG_CTX, "_gpgme_remove_io_cb", data, "setting fd 0x%x (item=%p) done", fdt->fds[idx].fd, fdt->fds[idx].opaque); free (fdt->fds[idx].opaque); free (tag); /* Free the table entry. */ fdt->fds[idx].fd = -1; fdt->fds[idx].for_read = 0; fdt->fds[idx].for_write = 0; fdt->fds[idx].opaque = NULL; } /* This is slightly embarrassing. The problem is that running an I/O callback _may_ influence the status of other file descriptors. Our own event loops could compensate for that, but the external event loops cannot. FIXME: We may still want to optimize this a bit when we are called from our own event loops. So if CHECKED is 1, the check is skipped. */ gpgme_error_t _gpgme_run_io_cb (struct io_select_fd_s *an_fds, int checked, gpgme_error_t *op_err) { struct wait_item_s *item; struct io_cb_data iocb_data; gpgme_error_t err; item = (struct wait_item_s *) an_fds->opaque; assert (item); if (!checked) { int nr; struct io_select_fd_s fds; TRACE0 (DEBUG_CTX, "_gpgme_run_io_cb", item, "need to check"); fds = *an_fds; fds.signaled = 0; /* Just give it a quick poll. */ nr = _gpgme_io_select (&fds, 1, 1); assert (nr <= 1); if (nr < 0) return errno; else if (nr == 0) /* The status changed in the meantime, there is nothing left to do. */ return 0; } TRACE2 (DEBUG_CTX, "_gpgme_run_io_cb", item, "handler (%p, %d)", item->handler_value, an_fds->fd); iocb_data.handler_value = item->handler_value; iocb_data.op_err = 0; err = item->handler (&iocb_data, an_fds->fd); *op_err = iocb_data.op_err; return err; } diff --git a/tests/Makefile.am b/tests/Makefile.am index 1b990e6f..8faa05ad 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,53 +1,54 @@ # Makefile.am - Makefile for GPGME tests. # Copyright (C) 2000 Werner Koch (dd9jn) # Copyright (C) 2001, 2004 g10 Code GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser General Public -# License along with this program; if not, see . +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later ## Process this file with automake to produce Makefile.in GNUPGHOME=$(abs_builddir) TESTS_ENVIRONMENT = GNUPGHOME=$(GNUPGHOME) TESTS = t-version t-data t-engine-info EXTRA_DIST = start-stop-agent t-data-1.txt t-data-2.txt ChangeLog-2011 AM_CPPFLAGS = -I$(top_builddir)/src @GPG_ERROR_CFLAGS@ AM_LDFLAGS = -no-install LDADD = ../src/libgpgme.la @GPG_ERROR_LIBS@ noinst_HEADERS = run-support.h noinst_PROGRAMS = $(TESTS) run-keylist run-export run-import run-sign \ run-verify run-encrypt run-identify run-decrypt run-genkey \ run-keysign run-tofu run-swdb run-threaded run_threaded_LDADD = ../src/libgpgme.la -lpthread @GPG_ERROR_LIBS@ if RUN_GPG_TESTS gpgtests = gpg json else gpgtests = endif if RUN_GPGSM_TESTS gpgsmtests = gpgsm opassuan else gpgsmtests = endif SUBDIRS = ${gpgtests} ${gpgsmtests} diff --git a/tests/gpg/Makefile.am b/tests/gpg/Makefile.am index ba2d1f3f..b8d15165 100644 --- a/tests/gpg/Makefile.am +++ b/tests/gpg/Makefile.am @@ -1,115 +1,115 @@ # Copyright (C) 2000 Werner Koch (dd9jn) # Copyright (C) 2001, 2004, 2005, 2009 g10 Code GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser 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 +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later ## Process this file with automake to produce Makefile.in GPG = gpg GPG_AGENT = gpg-agent GNUPGHOME=$(abs_builddir) TESTS_ENVIRONMENT = GNUPGHOME=$(GNUPGHOME) LC_ALL=C GPG_AGENT_INFO= \ top_srcdir=$(top_srcdir) # The keylist tests must come after the import and the edit test. noinst_HEADERS = t-support.h if HAVE_W32_SYSTEM tests_unix = else tests_unix = t-eventloop t-thread1 t-thread-keylist t-thread-keylist-verify endif c_tests = \ t-encrypt t-encrypt-sym t-encrypt-sign t-sign t-signers \ t-decrypt t-verify t-decrypt-verify t-sig-notation t-export \ t-import t-trustlist t-edit t-keylist t-keylist-sig t-wait \ t-encrypt-large t-file-name t-gpgconf t-encrypt-mixed \ $(tests_unix) TESTS = initial.test $(c_tests) final.test CLEANFILES = secring.gpg pubring.gpg pubring.kbx trustdb.gpg dirmngr.conf \ gpg-agent.conf pubring.kbx~ S.gpg-agent gpg.conf pubring.gpg~ \ random_seed S.gpg-agent .gpg-v21-migrated pubring-stamp \ gpg-sample.stamp tofu.db *.conf.gpgconf.bak private_keys = \ 13CD0F3BDF24BE53FE192D62F18737256FF6E4FD \ 76F7E2B35832976B50A27A282D9B87E44577EB66 \ A0747D5F9425E6664F4FFBEED20FBCA79FDED2BD \ 13CBE3758AFE42B5E5E2AE4CED27AFA455E3F87F \ 7A030357C0F253A5BBCD282FFC4E521B37558F5C EXTRA_DIST = initial.test final.test \ pubdemo.asc secdemo.asc cipher-1.asc cipher-2.asc \ geheim.txt pubkey-1.asc seckey-1.asc pinentry $(private_keys) BUILT_SOURCES = gpg.conf gpg-agent.conf pubring-stamp \ gpg-sample.stamp AM_CPPFLAGS = -I$(top_builddir)/src @GPG_ERROR_CFLAGS@ AM_LDFLAGS = -no-install LDADD = ../../src/libgpgme.la t_thread1_LDADD = ../../src/libgpgme.la -lpthread t_thread_keylist_LDADD = ../../src/libgpgme.la -lpthread t_thread_keylist_verify_LDADD = ../../src/libgpgme.la -lpthread t_cancel_LDADD = ../../src/libgpgme.la -lpthread # We don't run t-genkey and t-cancel in the test suite, because it # takes too long tests_skipped = t-genkey if !HAVE_W32_SYSTEM tests_skipped += t-cancel endif noinst_PROGRAMS = $(c_tests) $(tests_skipped) clean-local: -$(TESTS_ENVIRONMENT) $(top_srcdir)/tests/start-stop-agent --stop -rm -fR private-keys-v1.d gpg-sample.stamp: $(srcdir)/$(private_keys) -$(TESTS_ENVIRONMENT) gpgconf --kill all $(MKDIR_P) ./private-keys-v1.d for k in $(private_keys); do \ cp $(srcdir)/$$k private-keys-v1.d/$$k.key; \ done echo x > ./gpg-sample.stamp pubring-stamp: $(srcdir)/pubdemo.asc gpg-sample.stamp $(TESTS_ENVIRONMENT) $(GPG) --batch --no-permission-warning \ --import $(srcdir)/pubdemo.asc -$(TESTS_ENVIRONMENT) $(GPG) --batch --no-permission-warning \ --import $(srcdir)/secdemo.asc echo x > ./pubring-stamp gpg.conf: # This is required for t-sig-notations. echo no-force-v3-sigs > ./gpg.conf gpg-agent.conf: # This is required for gpg2, which does not support command fd for the # passphrase. disable-scdaemon is required so that we don't try using # a key from a smartcard reader (error might be: Unusable secret key) echo pinentry-program $(abs_srcdir)/pinentry > ./gpg-agent.conf echo disable-scdaemon >> ./gpg-agent.conf # end-of-file diff --git a/tests/gpg/t-cancel.c b/tests/gpg/t-cancel.c index b5550d8a..48158fbd 100644 --- a/tests/gpg/t-cancel.c +++ b/tests/gpg/t-cancel.c @@ -1,272 +1,272 @@ /* t-thread-cancel.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #ifdef HAVE_SYS_TIME_H # include #endif #include #include #include #ifdef HAVE_SYS_SELECT_H # include #endif #include #include "t-support.h" struct op_result { int done; gpgme_error_t err; }; static struct op_result op_result; struct one_fd { int fd; int dir; gpgme_io_cb_t fnc; void *fnc_data; }; #define FDLIST_MAX 32 static struct one_fd fdlist[FDLIST_MAX]; static pthread_mutex_t lock; static gpgme_error_t add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **r_tag) { struct one_fd *fds = data; int i; pthread_mutex_lock (&lock); for (i = 0; i < FDLIST_MAX; i++) { if (fds[i].fd == -1) { fds[i].fd = fd; fds[i].dir = dir; fds[i].fnc = fnc; fds[i].fnc_data = fnc_data; break; } } pthread_mutex_unlock (&lock); if (i == FDLIST_MAX) return gpgme_err_make (GPG_ERR_SOURCE_USER_1, GPG_ERR_GENERAL); *r_tag = &fds[i]; return 0; } static void remove_io_cb (void *tag) { struct one_fd *fd = tag; pthread_mutex_lock (&lock); fd->fd = -1; pthread_mutex_unlock (&lock); } static void io_event (void *data, gpgme_event_io_t type, void *type_data) { struct op_result *result = data; if (type == GPGME_EVENT_DONE) { result->done = 1; result->err = * (gpgme_error_t *) type_data; } } static int do_select (void) { fd_set rfds; fd_set wfds; int i, n; int any = 0; struct timeval tv; pthread_mutex_lock (&lock); FD_ZERO (&rfds); FD_ZERO (&wfds); for (i = 0; i < FDLIST_MAX; i++) if (fdlist[i].fd != -1) FD_SET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds); pthread_mutex_unlock (&lock); tv.tv_sec = 0; tv.tv_usec = 1000; do { n = select (FD_SETSIZE, &rfds, &wfds, NULL, &tv); } while (n < 0 && errno == EINTR); if (n < 0) return n; /* Error or timeout. */ pthread_mutex_lock (&lock); for (i = 0; i < FDLIST_MAX && n; i++) { if (fdlist[i].fd != -1) { if (FD_ISSET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds)) { assert (n); n--; any = 1; (*fdlist[i].fnc) (fdlist[i].fnc_data, fdlist[i].fd); } } } pthread_mutex_unlock (&lock); return any; } static int my_wait (void) { int n; do { n = do_select (); } while (n >= 0 && !op_result.done); return 0; } static struct gpgme_io_cbs io_cbs = { add_io_cb, fdlist, remove_io_cb, io_event, &op_result }; static void * thread_cancel (void *data) { gpgme_ctx_t ctx = data; gpgme_error_t err; usleep (100000); err = gpgme_cancel (ctx); fail_if_err (err); return NULL; } int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_engine_info_t info; int i; pthread_mutexattr_t attr; pthread_t tcancel; const char *parms = "\n" "Key-Type: RSA\n" "Key-Length: 2048\n" "Subkey-Type: RSA\n" "Subkey-Length: 2048\n" "Name-Real: Joe Tester\n" "Name-Comment: (pp=abc)\n" "Name-Email: joe@foo.bar\n" "Expire-Date: 0\n" "Passphrase: abc\n" "\n"; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_get_engine_info (&info); fail_if_err (err); /* The mutex must be recursive, since remove_io_cb (which acquires a lock) can be called while holding a lock acquired in do_select. */ pthread_mutexattr_init (&attr); pthread_mutexattr_settype (&attr, PTHREAD_MUTEX_RECURSIVE); pthread_mutex_init (&lock, &attr); pthread_mutexattr_destroy (&attr); for (i = 0; i < FDLIST_MAX; i++) fdlist[i].fd = -1; err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_armor (ctx, 1); gpgme_set_io_cbs (ctx, &io_cbs); op_result.done = 0; pthread_create (&tcancel, NULL, thread_cancel, ctx); err = gpgme_op_genkey_start (ctx, parms, NULL, NULL); fail_if_err (err); my_wait (); pthread_join (tcancel, NULL); if (op_result.err) { if (gpgme_err_code (op_result.err) == GPG_ERR_CANCELED) fputs ("Successfully cancelled\n", stdout); else { fprintf (stderr, "%s:%i: Operation finished with unexpected error: %s\n", __FILE__, __LINE__, gpgme_strerror (op_result.err)); exit (1); } } else fputs ("Successfully finished before cancellation\n", stdout); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-decrypt-verify.c b/tests/gpg/t-decrypt-verify.c index 653d74e9..cbd6cc70 100644 --- a/tests/gpg/t-decrypt-verify.c +++ b/tests/gpg/t-decrypt-verify.c @@ -1,144 +1,145 @@ /* t-decrypt-verify.c - Regression test. * Copyright (C) 2000 Werner Koch (dd9jn) * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include "t-support.h" static void check_verify_result (gpgme_verify_result_t result, unsigned int summary, const char *fpr, gpgme_error_t status) { gpgme_signature_t sig; sig = result->signatures; if (!sig || sig->next) { fprintf (stderr, "%s:%i: Unexpected number of signatures\n", __FILE__, __LINE__); exit (1); } if (sig->summary != summary) { fprintf (stderr, "%s:%i: Unexpected signature summary: 0x%x\n", __FILE__, __LINE__, sig->summary); exit (1); } if (strcmp (sig->fpr, fpr)) { fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n", __FILE__, __LINE__, sig->fpr); exit (1); } if (gpgme_err_code (sig->status) != status) { fprintf (stderr, "%s:%i: Unexpected signature status: %s\n", __FILE__, __LINE__, gpgme_strerror (sig->status)); exit (1); } if (sig->notations) { fprintf (stderr, "%s:%i: Unexpected notation data\n", __FILE__, __LINE__); exit (1); } if (sig->wrong_key_usage) { fprintf (stderr, "%s:%i: Unexpectedly wrong key usage\n", __FILE__, __LINE__); exit (1); } if (sig->validity != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "%s:%i: Unexpected validity: %i\n", __FILE__, __LINE__, sig->validity); exit (1); } if (gpgme_err_code (sig->validity_reason) != GPG_ERR_NO_ERROR) { fprintf (stderr, "%s:%i: Unexpected validity reason: %s\n", __FILE__, __LINE__, gpgme_strerror (sig->validity_reason)); exit (1); } } int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_decrypt_result_t decrypt_result; gpgme_verify_result_t verify_result; char *cipher_2_asc = make_filename ("cipher-2.asc"); char *agent_info; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); agent_info = getenv("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); err = gpgme_data_new_from_file (&in, cipher_2_asc, 1); free (cipher_2_asc); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_decrypt_verify (ctx, in, out); fail_if_err (err); decrypt_result = gpgme_op_decrypt_result (ctx); if (decrypt_result->unsupported_algorithm) { fprintf (stderr, "%s:%i: unsupported algorithm: %s\n", __FILE__, __LINE__, decrypt_result->unsupported_algorithm); exit (1); } print_data (out); verify_result = gpgme_op_verify_result (ctx); check_verify_result (verify_result, 0, "A0FF4590BB6122EDEF6E3C542D727CC768697734", GPG_ERR_NO_ERROR); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-decrypt.c b/tests/gpg/t-decrypt.c index 92fadd68..408564d7 100644 --- a/tests/gpg/t-decrypt.c +++ b/tests/gpg/t-decrypt.c @@ -1,83 +1,83 @@ /* t-decrypt.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include "t-support.h" int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_decrypt_result_t result; char *cipher_1_asc = make_filename ("cipher-1.asc"); char *agent_info; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); agent_info = getenv("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); err = gpgme_data_new_from_file (&in, cipher_1_asc, 1); free (cipher_1_asc); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_decrypt (ctx, in, out); fail_if_err (err); result = gpgme_op_decrypt_result (ctx); if (result->unsupported_algorithm) { fprintf (stderr, "%s:%i: unsupported algorithm: %s\n", __FILE__, __LINE__, result->unsupported_algorithm); exit (1); } print_data (out); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-edit.c b/tests/gpg/t-edit.c index 7b444fa0..b34ee3f7 100644 --- a/tests/gpg/t-edit.c +++ b/tests/gpg/t-edit.c @@ -1,155 +1,155 @@ /* t-edit.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include "t-support.h" static void flush_data (gpgme_data_t dh) { char buf[100]; int ret; ret = gpgme_data_seek (dh, 0, SEEK_SET); if (ret) fail_if_err (gpgme_error_from_errno (errno)); while ((ret = gpgme_data_read (dh, buf, 100)) > 0) fwrite (buf, ret, 1, stdout); if (ret < 0) fail_if_err (gpgme_error_from_errno (errno)); } gpgme_error_t interact_fnc (void *opaque, const char *status, const char *args, int fd) { const char *result = NULL; gpgme_data_t out = (gpgme_data_t) opaque; fputs ("[-- Response --]\n", stdout); flush_data (out); fprintf (stdout, "[-- Code: %s, %s --]\n", status, args); if (fd >= 0) { if (!strcmp (args, "keyedit.prompt")) { static int step = 0; switch (step) { case 0: result = "fpr"; break; case 1: result = "expire"; break; /* This fixes the primary user ID so the keylisting tests will have predictable output. */ case 2: result = "1"; break; case 3: result = "primary"; break; default: result = "quit"; break; } step++; } else if (!strcmp (args, "keyedit.save.okay")) result = "Y"; else if (!strcmp (args, "keygen.valid")) result = "0"; } if (result) { gpgme_io_writen (fd, result, strlen (result)); gpgme_io_writen (fd, "\n", 1); } return 0; } int main (int argc, char **argv) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t out = NULL; gpgme_key_t key = NULL; const char *pattern = "Alpha"; char *agent_info; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); agent_info = getenv("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, 0); err = gpgme_op_keylist_start (ctx, pattern, 0); fail_if_err (err); err = gpgme_op_keylist_next (ctx, &key); fail_if_err (err); err = gpgme_op_keylist_end (ctx); fail_if_err (err); err = gpgme_op_interact (ctx, key, 0, interact_fnc, out, out); fail_if_err (err); fputs ("[-- Last response --]\n", stdout); flush_data (out); gpgme_data_release (out); gpgme_key_unref (key); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-encrypt-large.c b/tests/gpg/t-encrypt-large.c index 8d78a049..fd389de7 100644 --- a/tests/gpg/t-encrypt-large.c +++ b/tests/gpg/t-encrypt-large.c @@ -1,150 +1,150 @@ /* t-encrypt-large.c - Regression test for large amounts of data. - Copyright (C) 2005 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2005 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" struct cb_parms { size_t bytes_to_send; size_t bytes_received; }; /* The read callback used by GPGME to read data. */ static ssize_t read_cb (void *handle, void *buffer, size_t size) { struct cb_parms *parms = handle; char *p = buffer; for (; size && parms->bytes_to_send; size--, parms->bytes_to_send--) *p++ = rand (); return (p - (char*)buffer); } /* The write callback used by GPGME to write data. */ static ssize_t write_cb (void *handle, const void *buffer, size_t size) { struct cb_parms *parms = handle; (void)buffer; parms->bytes_received += size; return size; } static void progress_cb (void *opaque, const char *what, int type, int current, int total) { /* This is just a dummy. */ (void)opaque; (void)what; (void)type; (void)current; (void)total; } int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; struct gpgme_data_cbs cbs; gpgme_data_t in, out; gpgme_key_t key[3] = { NULL, NULL, NULL }; gpgme_encrypt_result_t result; size_t nbytes; struct cb_parms parms; if (argc > 1) nbytes = atoi (argv[1]); else nbytes = 100000; init_gpgme (GPGME_PROTOCOL_OpenPGP); memset (&cbs, 0, sizeof cbs); cbs.read = read_cb; cbs.write = write_cb; memset (&parms, 0, sizeof parms); parms.bytes_to_send = nbytes; err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_armor (ctx, 0); /* Install a progress handler to enforce a bit of more work to the gpgme i/o system. */ gpgme_set_progress_cb (ctx, progress_cb, NULL); err = gpgme_data_new_from_cbs (&in, &cbs, &parms); fail_if_err (err); err = gpgme_data_new_from_cbs (&out, &cbs, &parms); fail_if_err (err); err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734", &key[0], 0); fail_if_err (err); err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", &key[1], 0); fail_if_err (err); err = gpgme_op_encrypt (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); fail_if_err (err); result = gpgme_op_encrypt_result (ctx); if (result->invalid_recipients) { fprintf (stderr, "Invalid recipient encountered: %s\n", result->invalid_recipients->fpr); exit (1); } printf ("plaintext=%u bytes, ciphertext=%u bytes\n", (unsigned int)nbytes, (unsigned int)parms.bytes_received); gpgme_key_unref (key[0]); gpgme_key_unref (key[1]); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-encrypt-mixed.c b/tests/gpg/t-encrypt-mixed.c index 57fdc515..4cc9d6a6 100644 --- a/tests/gpg/t-encrypt-mixed.c +++ b/tests/gpg/t-encrypt-mixed.c @@ -1,130 +1,130 @@ /* t-encrypt-mixed.c - Regression test. - Copyright (C) 2016 by Bundesamt für Sicherheit in der Informationstechnik - Software engineering by Intevation GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2016 by Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" /* Tests mixed symmetric and asymmetric decryption. Verifies that an encrypted message can be decrypted without the secret key but that the recipient is also set correctly. */ int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_key_t key[2] = { NULL, NULL }; gpgme_encrypt_result_t result; gpgme_decrypt_result_t dec_result; gpgme_recipient_t recipient; const char *text = "Hallo Leute\n"; char *text2; size_t len; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_armor (ctx, 1); err = gpgme_data_new_from_mem (&in, text, strlen (text), 0); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); /* A recipient for which we don't have a secret key */ err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", &key[0], 0); fail_if_err (err); err = gpgme_op_encrypt (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST | GPGME_ENCRYPT_SYMMETRIC, in, out); fail_if_err (err); result = gpgme_op_encrypt_result (ctx); if (result->invalid_recipients) { fprintf (stderr, "Invalid recipient encountered: %s\n", result->invalid_recipients->fpr); exit (1); } print_data (out); /* Now try to decrypt */ gpgme_data_seek (out, 0, SEEK_SET); gpgme_data_release (in); err = gpgme_data_new (&in); fail_if_err (err); err = gpgme_op_decrypt (ctx, out, in); fail_if_err (err); fputs ("Begin Result Decryption:\n", stdout); print_data (in); fputs ("End Result.\n", stdout); dec_result = gpgme_op_decrypt_result (ctx); if (dec_result->unsupported_algorithm || dec_result->wrong_key_usage) { fprintf (stderr, "%s:%d: Decryption failed\n", __FILE__, __LINE__); exit (1); } text2 = gpgme_data_release_and_get_mem (in, &len); if (strncmp (text, text2, len)) { fprintf (stderr, "%s:%d: Wrong plaintext\n", __FILE__, __LINE__); exit (1); } recipient = dec_result->recipients; if (!recipient || recipient->next) { fprintf (stderr, "%s:%d: Invalid recipients \n", __FILE__, __LINE__); exit (1); } if (strncmp (recipient->keyid, "5381EA4EE29BA37F", 16)) { fprintf (stderr, "%s:%d: Not encrypted to recipient's subkey \n", __FILE__, __LINE__); exit (1); } gpgme_key_unref (key[0]); free (text2); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-encrypt-sign.c b/tests/gpg/t-encrypt-sign.c index 41d16a0a..dbdae0b6 100644 --- a/tests/gpg/t-encrypt-sign.c +++ b/tests/gpg/t-encrypt-sign.c @@ -1,161 +1,161 @@ /* t-encrypt-sign.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" static void check_result (gpgme_sign_result_t result, gpgme_sig_mode_t type) { if (result->invalid_signers) { fprintf (stderr, "Invalid signer found: %s\n", result->invalid_signers->fpr); exit (1); } if (!result->signatures || result->signatures->next) { fprintf (stderr, "Unexpected number of signatures created\n"); exit (1); } if (result->signatures->type != type) { fprintf (stderr, "Wrong type of signature created\n"); exit (1); } if (result->signatures->pubkey_algo != GPGME_PK_DSA) { fprintf (stderr, "Wrong pubkey algorithm reported: %i\n", result->signatures->pubkey_algo); exit (1); } if (result->signatures->hash_algo != GPGME_MD_SHA1 && result->signatures->hash_algo != GPGME_MD_RMD160) { fprintf (stderr, "Wrong hash algorithm reported: %i\n", result->signatures->hash_algo); exit (1); } if (result->signatures->sig_class != 0) { fprintf (stderr, "Wrong signature class reported: %u\n", result->signatures->sig_class); exit (1); } if (strcmp ("A0FF4590BB6122EDEF6E3C542D727CC768697734", result->signatures->fpr)) { fprintf (stderr, "Wrong fingerprint reported: %s\n", result->signatures->fpr); exit (1); } } int main (int argc, char **argv) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_key_t key[3] = { NULL, NULL, NULL }; gpgme_encrypt_result_t result; gpgme_sign_result_t sign_result; char *agent_info; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_textmode (ctx, 1); gpgme_set_armor (ctx, 1); agent_info = getenv("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734", &key[0], 0); fail_if_err (err); err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", &key[1], 0); fail_if_err (err); err = gpgme_op_encrypt_sign (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); fail_if_err (err); result = gpgme_op_encrypt_result (ctx); if (result->invalid_recipients) { fprintf (stderr, "Invalid recipient encountered: %s\n", result->invalid_recipients->fpr); exit (1); } sign_result = gpgme_op_sign_result (ctx); check_result (sign_result, GPGME_SIG_MODE_NORMAL); print_data (out); gpgme_key_unref (key[0]); gpgme_key_unref (key[1]); gpgme_data_release (in); gpgme_data_release (out); /* Now a second time using symmetric encryption. */ err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_encrypt_sign (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); fail_if_err (err); sign_result = gpgme_op_sign_result (ctx); check_result (sign_result, GPGME_SIG_MODE_NORMAL); print_data (out); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-encrypt-sym.c b/tests/gpg/t-encrypt-sym.c index 8e5e7ff4..127bac60 100644 --- a/tests/gpg/t-encrypt-sym.c +++ b/tests/gpg/t-encrypt-sym.c @@ -1,102 +1,99 @@ /* t-encrypt-sym.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include "t-support.h" int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t plain, cipher; const char *text = "Hallo Leute\n"; char *text2; char *p; size_t len; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_armor (ctx, 1); p = getenv("GPG_AGENT_INFO"); if (!(p && strchr (p, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); err = gpgme_data_new_from_mem (&plain, text, strlen (text), 0); fail_if_err (err); err = gpgme_data_new (&cipher); fail_if_err (err); err = gpgme_op_encrypt (ctx, 0, 0, plain, cipher); fail_if_err (err); fflush (NULL); fputs ("Begin Result Encryption:\n", stdout); print_data (cipher); fputs ("End Result.\n", stdout); gpgme_data_seek (cipher, 0, SEEK_SET); gpgme_data_release (plain); err = gpgme_data_new (&plain); fail_if_err (err); err = gpgme_op_decrypt (ctx, cipher, plain); fail_if_err (err); fputs ("Begin Result Decryption:\n", stdout); print_data (plain); fputs ("End Result.\n", stdout); text2 = gpgme_data_release_and_get_mem (plain, &len); if (strncmp (text, text2, len)) { fprintf (stderr, "%s:%d: Wrong plaintext\n", __FILE__, __LINE__); exit (1); } gpgme_data_release (cipher); free (text2); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-encrypt.c b/tests/gpg/t-encrypt.c index 6eafc00c..76d9e397 100644 --- a/tests/gpg/t-encrypt.c +++ b/tests/gpg/t-encrypt.c @@ -1,85 +1,85 @@ /* t-encrypt.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_key_t key[3] = { NULL, NULL, NULL }; gpgme_encrypt_result_t result; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_armor (ctx, 1); err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734", &key[0], 0); fail_if_err (err); err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", &key[1], 0); fail_if_err (err); err = gpgme_op_encrypt (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); fail_if_err (err); result = gpgme_op_encrypt_result (ctx); if (result->invalid_recipients) { fprintf (stderr, "Invalid recipient encountered: %s\n", result->invalid_recipients->fpr); exit (1); } print_data (out); gpgme_key_unref (key[0]); gpgme_key_unref (key[1]); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-eventloop.c b/tests/gpg/t-eventloop.c index 2d3df41a..b31764ea 100644 --- a/tests/gpg/t-eventloop.c +++ b/tests/gpg/t-eventloop.c @@ -1,229 +1,229 @@ /* t-eventloop.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include #include "t-support.h" /* Stripped down version of gpgme/wait.c. */ struct op_result { int done; gpgme_error_t err; }; struct op_result op_result; struct one_fd { int fd; int dir; gpgme_io_cb_t fnc; void *fnc_data; }; #define FDLIST_MAX 32 struct one_fd fdlist[FDLIST_MAX]; gpgme_error_t add_io_cb (void *data, int fd, int dir, gpgme_io_cb_t fnc, void *fnc_data, void **r_tag) { struct one_fd *fds = data; int i; for (i = 0; i < FDLIST_MAX; i++) { if (fds[i].fd == -1) { fds[i].fd = fd; fds[i].dir = dir; fds[i].fnc = fnc; fds[i].fnc_data = fnc_data; break; } } if (i == FDLIST_MAX) return gpgme_err_make (GPG_ERR_SOURCE_USER_1, GPG_ERR_GENERAL); *r_tag = &fds[i]; return 0; } void remove_io_cb (void *tag) { struct one_fd *fd = tag; fd->fd = -1; } void io_event (void *data, gpgme_event_io_t type, void *type_data) { struct op_result *result = data; if (type == GPGME_EVENT_DONE) { result->done = 1; result->err = * (gpgme_error_t *) type_data; } } int do_select (void) { fd_set rfds; fd_set wfds; int i, n; int any = 0; struct timeval tv; FD_ZERO (&rfds); FD_ZERO (&wfds); for (i = 0; i < FDLIST_MAX; i++) if (fdlist[i].fd != -1) FD_SET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds); tv.tv_sec = 0; tv.tv_usec = 1000; do { n = select (FD_SETSIZE, &rfds, &wfds, NULL, &tv); } while (n < 0 && errno == EINTR); if (n < 0) return n; /* Error or timeout. */ for (i = 0; i < FDLIST_MAX && n; i++) { if (fdlist[i].fd != -1) { if (FD_ISSET (fdlist[i].fd, fdlist[i].dir ? &rfds : &wfds)) { assert (n); n--; any = 1; (*fdlist[i].fnc) (fdlist[i].fnc_data, fdlist[i].fd); } } } return any; } int my_wait (void) { int n; do { n = do_select (); } while (n >= 0 && !op_result.done); return 0; } struct gpgme_io_cbs io_cbs = { add_io_cb, fdlist, remove_io_cb, io_event, &op_result }; int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_key_t key[3] = { NULL, NULL, NULL }; int i; init_gpgme (GPGME_PROTOCOL_OpenPGP); for (i = 0; i < FDLIST_MAX; i++) fdlist[i].fd = -1; err = gpgme_engine_check_version (GPGME_PROTOCOL_OpenPGP); fail_if_err (err); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_armor (ctx, 1); gpgme_set_io_cbs (ctx, &io_cbs); op_result.done = 0; err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734", &key[0], 0); fail_if_err (err); err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", &key[1], 0); fail_if_err (err); err = gpgme_op_encrypt_start (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); fail_if_err (err); my_wait (); fail_if_err (op_result.err); fail_if_err (err); fflush (NULL); fputs ("Begin Result:\n", stdout); print_data (out); fputs ("End Result.\n", stdout); gpgme_key_unref (key[0]); gpgme_key_unref (key[1]); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-export.c b/tests/gpg/t-export.c index eaed829a..900bca99 100644 --- a/tests/gpg/t-export.c +++ b/tests/gpg/t-export.c @@ -1,97 +1,97 @@ /* t-export.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" int main (int argc, char **argv) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t out; const char *pattern[] = { "Alpha", "Bob", NULL }; gpgme_key_t keyarray[3]; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); gpgme_set_armor (ctx, 1); err = gpgme_op_export_ext (ctx, pattern, 0, out); fail_if_err (err); fflush (NULL); fputs ("Begin Result:\n", stdout); print_data (out); fputs ("End Result.\n", stdout); gpgme_data_release (out); /* Again. Now using a key array. */ err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_get_key (ctx, "0x68697734" /* Alpha */, keyarray+0, 0); fail_if_err (err); err = gpgme_get_key (ctx, "0xA9E3B0B2" /* Bob */, keyarray+1, 0); fail_if_err (err); keyarray[2] = NULL; gpgme_set_armor (ctx, 1); err = gpgme_op_export_keys (ctx, keyarray, 0, out); fail_if_err (err); gpgme_key_unref (keyarray[0]); gpgme_key_unref (keyarray[1]); fflush (NULL); fputs ("Begin Result:\n", stdout); print_data (out); fputs ("End Result.\n", stdout); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-file-name.c b/tests/gpg/t-file-name.c index 3c2a796b..3b29e4f1 100644 --- a/tests/gpg/t-file-name.c +++ b/tests/gpg/t-file-name.c @@ -1,99 +1,99 @@ /* t-file-name.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" #define TESTNAME "abcde12345" int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_key_t key[2] = { NULL, NULL }; gpgme_decrypt_result_t result; char *agent_info; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_armor (ctx, 1); agent_info = getenv("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); err = gpgme_data_set_file_name (in, TESTNAME); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734", &key[0], 0); fail_if_err (err); err = gpgme_op_encrypt (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); fail_if_err (err); gpgme_data_release (in); err = gpgme_data_new (&in); fail_if_err (err); err = gpgme_data_seek (out, 0, SEEK_SET); fail_if_err (err); err = gpgme_op_decrypt (ctx, out, in); fail_if_err (err); result = gpgme_op_decrypt_result (ctx); if (strcmp (TESTNAME, result->file_name)) { fprintf (stderr, "%s:%i: Unexpected result file name: %s\n", __FILE__, __LINE__, result->file_name ? result->file_name : "(null)"); exit (1); } gpgme_key_unref (key[0]); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-genkey.c b/tests/gpg/t-genkey.c index f0127f78..4b977f19 100644 --- a/tests/gpg/t-genkey.c +++ b/tests/gpg/t-genkey.c @@ -1,127 +1,127 @@ /* t-genkey.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" /* True if progress function printed something on the screen. */ static int progress_called; static void progress (void *self, const char *what, int type, int current, int total) { (void)self; if (!strcmp (what, "primegen") && !current && !total && (type == '.' || type == '+' || type == '!' || type == '^' || type == '<' || type == '>')) { printf ("%c", type); fflush (stdout); progress_called = 1; } else { fprintf (stderr, "unknown progress `%s' %d %d %d\n", what, type, current, total); exit (1); } } int main (int argc, char **argv) { gpgme_ctx_t ctx; gpgme_error_t err; const char *parms = "\n" "Key-Type: DSA\n" "Key-Length: 1024\n" "Subkey-Type: ELG-E\n" "Subkey-Length: 1024\n" "Name-Real: Joe Tester\n" "Name-Comment: (pp=abc)\n" "Name-Email: joe@foo.bar\n" "Expire-Date: 0\n" "Passphrase: abc\n" "\n"; gpgme_genkey_result_t result; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_progress_cb (ctx, progress, NULL); err = gpgme_op_genkey (ctx, parms, NULL, NULL); fail_if_err (err); result = gpgme_op_genkey_result (ctx); if (!result) { fprintf (stderr, "%s:%d: gpgme_op_genkey_result returns NULL\n", __FILE__, __LINE__); exit (1); } if (progress_called) printf ("\n"); printf ("Generated key: %s (%s)\n", result->fpr ? result->fpr : "none", result->primary ? (result->sub ? "primary, sub" : "primary") : (result->sub ? "sub" : "none")); if (result->fpr && strlen (result->fpr) != 40) { fprintf (stderr, "%s:%d: generated key has unexpected fingerprint\n", __FILE__, __LINE__); exit (1); } if (!result->primary) { fprintf (stderr, "%s:%d: primary key was not generated\n", __FILE__, __LINE__); exit (1); } if (!result->sub) { fprintf (stderr, "%s:%d: sub key was not generated\n", __FILE__, __LINE__); exit (1); } gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-gpgconf.c b/tests/gpg/t-gpgconf.c index 5eccedeb..d7a5f1ee 100644 --- a/tests/gpg/t-gpgconf.c +++ b/tests/gpg/t-gpgconf.c @@ -1,389 +1,389 @@ /* t-gpgconf.c - Regression test. - Copyright (C) 2001, 2004, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2001, 2004, 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #ifdef HAVE_W32_SYSTEM #include #endif #include #include "t-support.h" static char * spaces (char *str, int extra) { static char buf[80]; int len = str ? strlen (str) : 0; int n; #define TABSTOP 30 n = TABSTOP - len - extra; memset (buf, ' ', sizeof (buf)); if (n < 1 || n > (sizeof (buf) - 1)) { buf[0] = '\n'; n = TABSTOP + 1; } buf[n] = '\0'; return buf; } void dump_arg (int type, gpgme_conf_arg_t arg) { if (!arg) { printf ("(none)"); return; } while (arg) { switch (type) { case GPGME_CONF_STRING: case GPGME_CONF_PATHNAME: case GPGME_CONF_LDAP_SERVER: case GPGME_CONF_KEY_FPR: case GPGME_CONF_PUB_KEY: case GPGME_CONF_SEC_KEY: case GPGME_CONF_ALIAS_LIST: printf ("`%s'", arg->value.string); break; case GPGME_CONF_UINT32: printf ("%u", arg->value.uint32); break; case GPGME_CONF_INT32: printf ("%i", arg->value.int32); break; case GPGME_CONF_NONE: printf ("%i (times)", arg->value.count); break; default: printf ("(unknown type)"); } arg = arg->next; if (arg) printf (" "); } } void dump_opt (gpgme_conf_opt_t opt) { char level; char runtime = (opt->flags & GPGME_CONF_RUNTIME) ? 'r' : ' '; switch (opt->level) { case GPGME_CONF_BASIC: level = 'b'; break; case GPGME_CONF_ADVANCED: level = 'a'; break; case GPGME_CONF_EXPERT: level = 'e'; break; case GPGME_CONF_INVISIBLE: level = 'i'; break; case GPGME_CONF_INTERNAL: level = '#'; break; default: level = '?'; } if (opt->flags & GPGME_CONF_GROUP) { printf ("\n"); printf ("%c%c [%s]%s%s\n", level, runtime, opt->name, spaces (opt->name, 5), opt->description ? opt->description : ""); } else { if (opt->argname) { const char *more = (opt->flags & GPGME_CONF_LIST) ? "..." : ""; if (opt->flags & GPGME_CONF_OPTIONAL) { printf ("%c%c --%s [%s%s] %s", level, runtime, opt->name, opt->argname, more, spaces (opt->name, 9 + strlen (opt->argname) + strlen (more))); } else { printf ("%c%c --%s %s%s %s", level, runtime, opt->name, opt->argname, more, spaces (opt->name, 7 + strlen (opt->argname) + strlen (more))); } } else printf ("%c%c --%s%s", level, runtime, opt->name, spaces (opt->name, 5)); if (opt->description) printf ("%s", opt->description); printf ("\n"); if (opt->flags & GPGME_CONF_DEFAULT) { printf ("%s%s = ", spaces (NULL, 0), opt->argname ? opt->argname : "(default)"); dump_arg (opt->type, opt->default_value); printf ("\n"); } else if (opt->flags & GPGME_CONF_DEFAULT_DESC) printf ("%s%s = %s\n", spaces (NULL, 0), opt->argname ? opt->argname : "(default)", opt->default_description); if (opt->no_arg_value) { printf ("%sNo Arg Def = ", spaces (NULL, 0)); dump_arg (opt->type, opt->no_arg_value); printf ("\n"); } if (opt->value) { printf ("%sCurrent = ", spaces (NULL, 0)); dump_arg (opt->type, opt->value); printf ("\n"); } } #if 0 arg = comp->options; while (opt) { dump_opt (opt); opt = opt->next; } #endif } void dump_comp (gpgme_conf_comp_t comp) { gpgme_conf_opt_t opt; printf ("COMPONENT\n"); printf ("=========\n"); printf (" Name: %s\n", comp->name); if (comp->description) printf (" Desc: %s\n", comp->description); if (comp->program_name) printf (" Path: %s\n", comp->program_name); printf ("\n"); opt = comp->options; while (opt) { dump_opt (opt); opt = opt->next; } } int lookup (gpgme_conf_comp_t conf, const char *component, const char *option, gpgme_conf_comp_t *comp, gpgme_conf_opt_t *opt) { *comp = conf; while (*comp && strcmp ((*comp)->name, component)) *comp = (*comp)->next; if (*comp) { *opt = (*comp)->options; while (*opt && strcmp ((*opt)->name, option)) *opt = (*opt)->next; /* Allow for the option not to be there. */ if (*opt) return 1; /* Found. */ } return 0; /* Not found. */ } #include int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_conf_comp_t conf; gpgme_conf_comp_t comp; int first; int i, N = 10; init_gpgme (GPGME_PROTOCOL_GPGCONF); err = gpgme_new (&ctx); fail_if_err (err); { /* Let's check getting the agent-socket directory for different homedirs. */ char *result1 = NULL; char *result2 = NULL; err = gpgme_ctx_set_engine_info (ctx, GPGME_PROTOCOL_GPGCONF, NULL, "/tmp/foo"); fail_if_err (err); err = gpgme_op_conf_dir (ctx, "agent-socket", &result1); fail_if_err (err); err = gpgme_ctx_set_engine_info (ctx, GPGME_PROTOCOL_GPGCONF, NULL, NULL); fail_if_err (err); err = gpgme_op_conf_dir (ctx, "agent-socket", &result2); fail_if_err (err); /* They have to be different. */ test (strcmp(result1, result2)); gpgme_free (result1); gpgme_free (result2); } err = gpgme_op_conf_load (ctx, &conf); fail_if_err (err); comp = conf; first = 1; while (comp) { if (!first) printf ("\n"); else first = 0; dump_comp (comp); comp = comp->next; } /* Now change something. */ fprintf (stderr, " dirmngr.verbose "); for (i = 0; i < N; i++) { unsigned int count = i % 4 + 1; /* counts must not be zero */ gpgme_conf_arg_t arg; gpgme_conf_opt_t opt; err = gpgme_conf_arg_new (&arg, GPGME_CONF_NONE, &count); fail_if_err (err); if (lookup (conf, "dirmngr", "verbose", &comp, &opt)) { /* Found. */ err = gpgme_conf_opt_change (opt, 0, arg); fail_if_err (err); err = gpgme_op_conf_save (ctx, comp); fail_if_err (err); } else { fprintf (stderr, "Skipping test, option dirmngr.verbose not found.\n"); break; } /* Reload config and verify that the value was updated. */ gpgme_conf_release (conf); err = gpgme_op_conf_load (ctx, &conf); fail_if_err (err); if (lookup (conf, "dirmngr", "verbose", &comp, &opt)) { /* Found. */ test (opt->alt_type == GPGME_CONF_NONE); test (opt->value); test ((unsigned long) opt->value->value.count == count); } fprintf (stderr, "."); fflush (stderr); } /* Now change something else. */ fprintf (stderr, " gpg.keyserver "); for (i = 0; i < N; i++) { const char *values[2] = { "hkp://foo.bar", "hkps://bar.foo" }; gpgme_conf_arg_t arg; gpgme_conf_opt_t opt; err = gpgme_conf_arg_new (&arg, GPGME_CONF_STRING, values[i%2]); fail_if_err (err); if (lookup (conf, "gpg", "keyserver", &comp, &opt)) { /* Found. */ test (opt->alt_type == GPGME_CONF_STRING); err = gpgme_conf_opt_change (opt, 0, arg); fail_if_err (err); err = gpgme_op_conf_save (ctx, comp); fail_if_err (err); } else { fprintf (stderr, "Skipping test, option gpg.keyserver not found.\n"); break; } /* Reload config and verify that the value was updated. */ gpgme_conf_release (conf); err = gpgme_op_conf_load (ctx, &conf); fail_if_err (err); if (lookup (conf, "gpg", "keyserver", &comp, &opt)) { /* Found. */ test (opt->alt_type == GPGME_CONF_STRING); test (opt->value); test (opt->value->value.string); test (strcmp (opt->value->value.string, values[i%2]) == 0); } fprintf (stderr, "."); fflush (stderr); } fprintf (stderr, "\n"); gpgme_conf_release (conf); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-import.c b/tests/gpg/t-import.c index 89a58159..ae822630 100644 --- a/tests/gpg/t-import.c +++ b/tests/gpg/t-import.c @@ -1,250 +1,250 @@ /* t-import.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" void check_result (gpgme_import_result_t result, const char *fpr, int secret) { if (result->considered != 1 && (secret && result->considered != 3)) { fprintf (stderr, "Unexpected number of considered keys %i\n", result->considered); exit (1); } if (result->no_user_id != 0) { fprintf (stderr, "Unexpected number of user ids %i\n", result->no_user_id); exit (1); } if ((secret && result->imported != 0) || (!secret && (result->imported != 0 && result->imported != 1))) { fprintf (stderr, "Unexpected number of imported keys %i\n", result->imported); exit (1); } if (result->imported_rsa != 0) { fprintf (stderr, "Unexpected number of imported RSA keys %i\n", result->imported_rsa); exit (1); } if ((secret && (result->unchanged != 0 && result->unchanged != 1)) || (!secret && ((result->imported == 0 && result->unchanged != 1) || (result->imported == 1 && result->unchanged != 0)))) { fprintf (stderr, "Unexpected number of unchanged keys %i\n", result->unchanged); exit (1); } if (result->new_user_ids != 0) { fprintf (stderr, "Unexpected number of new user IDs %i\n", result->new_user_ids); exit (1); } if (result->new_sub_keys != 0) { fprintf (stderr, "Unexpected number of new sub keys %i\n", result->new_sub_keys); exit (1); } if ((secret && ((result->secret_imported == 0 && result->new_signatures != 0) || (result->secret_imported == 1 && result->new_signatures > 1))) || (!secret && result->new_signatures != 0)) { fprintf (stderr, "Unexpected number of new signatures %i\n", result->new_signatures); if (result->new_signatures == 2) fprintf (stderr, "### ignored due to gpg 1.3.4 problems\n"); else exit (1); } if (result->new_revocations != 0) { fprintf (stderr, "Unexpected number of new revocations %i\n", result->new_revocations); exit (1); } if ((secret && result->secret_read != 1 && result->secret_read != 3) || (!secret && result->secret_read != 0)) { fprintf (stderr, "Unexpected number of secret keys read %i\n", result->secret_read); exit (1); } if ((secret && result->secret_imported != 0 && result->secret_imported != 1 && result->secret_imported != 2) || (!secret && result->secret_imported != 0)) { fprintf (stderr, "Unexpected number of secret keys imported %i\n", result->secret_imported); exit (1); } if ((secret && ((result->secret_imported == 0 && result->secret_unchanged != 1 && result->secret_unchanged != 2) || (result->secret_imported == 1 && result->secret_unchanged != 0))) || (!secret && result->secret_unchanged != 0)) { fprintf (stderr, "Unexpected number of secret keys unchanged %i\n", result->secret_unchanged); exit (1); } if (result->not_imported != 0) { fprintf (stderr, "Unexpected number of secret keys not imported %i\n", result->not_imported); exit (1); } if (secret) { if (!result->imports || (result->imports->next && result->imports->next->next)) { fprintf (stderr, "Unexpected number of status reports\n"); exit (1); } } else { if (!result->imports || result->imports->next) { fprintf (stderr, "Unexpected number of status reports\n"); exit (1); } } if (strcmp (fpr, result->imports->fpr)) { fprintf (stderr, "Unexpected fingerprint %s\n", result->imports->fpr); exit (1); } if (result->imports->next && strcmp (fpr, result->imports->next->fpr)) { fprintf (stderr, "Unexpected fingerprint on second status %s\n", result->imports->next->fpr); exit (1); } if (result->imports->result != 0) { fprintf (stderr, "Unexpected status result %s\n", gpgme_strerror (result->imports->result)); exit (1); } #if 0 if (secret) { if (result->secret_imported == 0) { if (result->imports->status != GPGME_IMPORT_SECRET) { fprintf (stderr, "Unexpected status %i\n", result->imports->status); exit (1); } } else { if (result->imports->status != (GPGME_IMPORT_SECRET | GPGME_IMPORT_NEW) || (result->imports->next && result->imports->next->status != GPGME_IMPORT_SIG)) { fprintf (stderr, "Unexpected status %i\n", result->imports->status); exit (1); } } } else { if ((result->imported == 0 && result->imports->status != 0) || (result->imported == 1 && result->imports->status != GPGME_IMPORT_NEW)) { fprintf (stderr, "Unexpected status %i\n", result->imports->status); exit (1); } } #endif } int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in; gpgme_import_result_t result; char *pubkey_1_asc = make_filename ("pubkey-1.asc"); char *seckey_1_asc = make_filename ("seckey-1.asc"); (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_data_new_from_file (&in, pubkey_1_asc, 1); free (pubkey_1_asc); fail_if_err (err); err = gpgme_op_import (ctx, in); fail_if_err (err); result = gpgme_op_import_result (ctx); check_result (result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", 0); gpgme_data_release (in); err = gpgme_data_new_from_file (&in, seckey_1_asc, 1); free (seckey_1_asc); fail_if_err (err); err = gpgme_op_import (ctx, in); fail_if_err (err); result = gpgme_op_import_result (ctx); check_result (result, "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", 1); gpgme_data_release (in); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-keylist-sig.c b/tests/gpg/t-keylist-sig.c index fdec7ca2..390ff41a 100644 --- a/tests/gpg/t-keylist-sig.c +++ b/tests/gpg/t-keylist-sig.c @@ -1,628 +1,628 @@ /* t-keylist-sig.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" struct { const char *fpr; const char *sec_keyid; struct { const char *name; const char *comment; const char *email; struct { gpgme_pubkey_algo_t algo; const char *keyid; const char *name; const char *comment; const char *email; unsigned int sig_class; int exportable; } sig; } uid[3]; } keys[] = { { "A0FF4590BB6122EDEF6E3C542D727CC768697734", "6AE6D7EE46A871F8", { { "Alfa Test", "demo key", "alfa@example.net", { GPGME_PK_DSA, "2D727CC768697734", "Alfa Test", "demo key", "alfa@example.net", 19, 1 } }, { "Alpha Test", "demo key", "alpha@example.net", { GPGME_PK_DSA, "2D727CC768697734", "Alfa Test", "demo key", "alfa@example.net", 19, 1 } }, { "Alice", "demo key", NULL, { GPGME_PK_DSA, "2D727CC768697734", "Alfa Test", "demo key", "alfa@example.net", 19, 1 } } } }, { NULL } }; int main (void) { gpgme_error_t err; gpgme_ctx_t ctx; gpgme_key_t key; gpgme_keylist_result_t result; int mode; int i = 0; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); mode = gpgme_get_keylist_mode (ctx); mode |= GPGME_KEYLIST_MODE_SIGS; err = gpgme_set_keylist_mode (ctx, mode); fail_if_err (err); err = gpgme_op_keylist_start (ctx, "Alpha", 0); fail_if_err (err); while (!(err = gpgme_op_keylist_next (ctx, &key))) { if (!keys[i].fpr) { fprintf (stderr, "More keys returned than expected\n"); exit (1); } /* Global key flags. */ if (key->revoked) { fprintf (stderr, "Key unexpectedly revoked\n"); exit (1); } if (key->expired) { fprintf (stderr, "Key unexpectedly expired\n"); exit (1); } if (key->disabled) { fprintf (stderr, "Key unexpectedly disabled\n"); exit (1); } if (key->invalid) { fprintf (stderr, "Key unexpectedly invalid\n"); exit (1); } if (!key->can_encrypt) { fprintf (stderr, "Key unexpectedly unusable for encryption\n"); exit (1); } if (!key->can_sign) { fprintf (stderr, "Key unexpectedly unusable for signing\n"); exit (1); } if (!key->can_certify) { fprintf (stderr, "Key unexpectedly unusable for certifications\n"); exit (1); } if (key->secret) { fprintf (stderr, "Key unexpectedly secret\n"); exit (1); } if (key->protocol != GPGME_PROTOCOL_OpenPGP) { fprintf (stderr, "Key has unexpected protocol: %s\n", gpgme_get_protocol_name (key->protocol)); exit (1); } if (key->issuer_serial) { fprintf (stderr, "Key unexpectedly carries issuer serial: %s\n", key->issuer_serial); exit (1); } if (key->issuer_name) { fprintf (stderr, "Key unexpectedly carries issuer name: %s\n", key->issuer_name); exit (1); } if (key->chain_id) { fprintf (stderr, "Key unexpectedly carries chain ID: %s\n", key->chain_id); exit (1); } if (key->owner_trust != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "Key has unexpected owner trust: %i\n", key->owner_trust); exit (1); } if (!key->subkeys || !key->subkeys->next || key->subkeys->next->next) { fprintf (stderr, "Key has unexpected number of subkeys\n"); exit (1); } /* Primary key. */ if (key->subkeys->revoked) { fprintf (stderr, "Primary key unexpectedly revoked\n"); exit (1); } if (key->subkeys->expired) { fprintf (stderr, "Primary key unexpectedly expired\n"); exit (1); } if (key->subkeys->disabled) { fprintf (stderr, "Primary key unexpectedly disabled\n"); exit (1); } if (key->subkeys->invalid) { fprintf (stderr, "Primary key unexpectedly invalid\n"); exit (1); } if (key->subkeys->can_encrypt) { fprintf (stderr, "Primary key unexpectedly usable for encryption\n"); exit (1); } if (!key->subkeys->can_sign) { fprintf (stderr, "Primary key unexpectedly unusable for signing\n"); exit (1); } if (!key->subkeys->can_certify) { fprintf (stderr, "Primary key unexpectedly unusable for certifications\n"); exit (1); } if (key->subkeys->secret) { fprintf (stderr, "Primary key unexpectedly secret\n"); exit (1); } if (key->subkeys->pubkey_algo != GPGME_PK_DSA) { fprintf (stderr, "Primary key has unexpected public key algo: %s\n", gpgme_pubkey_algo_name (key->subkeys->pubkey_algo)); exit (1); } if (key->subkeys->length != 1024) { fprintf (stderr, "Primary key has unexpected length: %i\n", key->subkeys->length); exit (1); } if (strcmp (key->subkeys->keyid, &keys[i].fpr[40 - 16])) { fprintf (stderr, "Primary key has unexpected key ID: %s\n", key->subkeys->keyid); exit (1); } if (strcmp (key->subkeys->fpr, keys[i].fpr)) { fprintf (stderr, "Primary key has unexpected fingerprint: %s\n", key->subkeys->fpr); exit (1); } if (key->subkeys->expires) { fprintf (stderr, "Primary key unexpectedly expires: %lu\n", key->subkeys->expires); exit (1); } /* Secondary key. */ if (key->subkeys->next->revoked) { fprintf (stderr, "Secondary key unexpectedly revoked\n"); exit (1); } if (key->subkeys->next->expired) { fprintf (stderr, "Secondary key unexpectedly expired\n"); exit (1); } if (key->subkeys->next->disabled) { fprintf (stderr, "Secondary key unexpectedly disabled\n"); exit (1); } if (key->subkeys->next->invalid) { fprintf (stderr, "Secondary key unexpectedly invalid\n"); exit (1); } if (!key->subkeys->next->can_encrypt) { fprintf (stderr, "Secondary key unexpectedly unusable for encryption\n"); exit (1); } if (key->subkeys->next->can_sign) { fprintf (stderr, "Secondary key unexpectedly usable for signing\n"); exit (1); } if (key->subkeys->next->can_certify) { fprintf (stderr, "Secondary key unexpectedly usable for certifications\n"); exit (1); } if (key->subkeys->next->secret) { fprintf (stderr, "Secondary key unexpectedly secret\n"); exit (1); } if (key->subkeys->next->pubkey_algo != GPGME_PK_ELG_E) { fprintf (stderr, "Secondary key has unexpected public key algo: %s\n", gpgme_pubkey_algo_name (key->subkeys->next->pubkey_algo)); exit (1); } if (key->subkeys->next->length != 1024) { fprintf (stderr, "Secondary key has unexpected length: %i\n", key->subkeys->next->length); exit (1); } if (strcmp (key->subkeys->next->keyid, keys[i].sec_keyid)) { fprintf (stderr, "Secondary key has unexpected key ID: %s\n", key->subkeys->next->keyid); exit (1); } if (!key->subkeys->next->fpr) { fprintf (stderr, "Secondary key has unexpectedly no fingerprint\n"); exit (1); } if (key->subkeys->next->expires) { fprintf (stderr, "Secondary key unexpectedly expires: %lu\n", key->subkeys->next->expires); exit (1); } /* FIXME: The below test will crash if we want to check for a name, comment or email that doesn't exist in the key's user IDs. */ if (!((!keys[i].uid[0].name && !key->uids) || (keys[i].uid[0].name && !keys[i].uid[1].name && key->uids && !key->uids->next) || (keys[i].uid[0].name && keys[i].uid[1].name && !keys[i].uid[2].name && key->uids && key->uids->next && !key->uids->next->next) || (keys[i].uid[0].name && keys[i].uid[1].name && keys[i].uid[2].name && key->uids && key->uids->next && key->uids->next->next && !key->uids->next->next->next))) { fprintf (stderr, "Key has unexpected number of user IDs\n"); exit (1); } if (key->uids && key->uids->revoked) { fprintf (stderr, "First user ID unexpectedly revoked\n"); exit (1); } if (key->uids && key->uids->invalid) { fprintf (stderr, "First user ID unexpectedly invalid\n"); exit (1); } if (key->uids && key->uids->validity != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "First user ID has unexpectedly validity: %i\n", key->uids->validity); exit (1); } if (keys[i].uid[0].name && strcmp (keys[i].uid[0].name, key->uids->name)) { fprintf (stderr, "Unexpected name in first user ID: %s\n", key->uids->name); exit (1); } if (keys[i].uid[0].comment && strcmp (keys[i].uid[0].comment, key->uids->comment)) { fprintf (stderr, "Unexpected comment in first user ID: %s\n", key->uids->comment); exit (1); } if (keys[i].uid[0].email && strcmp (keys[i].uid[0].email, key->uids->email)) { fprintf (stderr, "Unexpected email in first user ID: %s\n", key->uids->email); exit (1); } if (key->uids && (!key->uids->signatures || key->uids->signatures->next)) { fprintf (stderr, "First user ID unexpected number of signatures\n"); exit (1); } if (keys[i].uid[0].sig.algo != key->uids->signatures->pubkey_algo) { fprintf (stderr, "Unexpected algorithm in first user ID sig: %s\n", gpgme_pubkey_algo_name (key->uids->signatures->pubkey_algo)); exit (1); } if (strcmp (keys[i].uid[0].sig.keyid, key->uids->signatures->keyid)) { fprintf (stderr, "Unexpected key ID in first user ID sig: %s\n", key->uids->signatures->keyid); exit (1); } if (strcmp (keys[i].uid[0].sig.name, key->uids->signatures->name)) { fprintf (stderr, "Unexpected name in first user ID sig: %s\n", key->uids->signatures->name); exit (1); } if (strcmp (keys[i].uid[0].sig.comment, key->uids->signatures->comment)) { fprintf (stderr, "Unexpected comment in first user ID sig: %s\n", key->uids->signatures->comment); exit (1); } if (strcmp (keys[i].uid[0].sig.email, key->uids->signatures->email)) { fprintf (stderr, "Unexpected email in first user ID sig: %s\n", key->uids->signatures->email); exit (1); } if (keys[i].uid[0].sig.sig_class != key->uids->signatures->sig_class) { fprintf (stderr, "Unexpected class in first user ID sig: %i\n", key->uids->signatures->sig_class); exit (1); } if (keys[i].uid[0].sig.exportable != key->uids->signatures->exportable) { fprintf (stderr, "Unexpected exportable stat in first user ID sig: %i\n", key->uids->signatures->exportable); exit (1); } if (key->uids && key->uids->next && key->uids->next->revoked) { fprintf (stderr, "Second user ID unexpectedly revoked\n"); exit (1); } if (key->uids && key->uids->next && key->uids->next->invalid) { fprintf (stderr, "Second user ID unexpectedly invalid\n"); exit (1); } if (key->uids && key->uids->next && key->uids->next->validity != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "Second user ID has unexpectedly validity: %i\n", key->uids->next->validity); exit (1); } if (keys[i].uid[1].name && strcmp (keys[i].uid[1].name, key->uids->next->name)) { fprintf (stderr, "Unexpected name in second user ID: %s\n", key->uids->next->name); exit (1); } if (keys[i].uid[1].comment && strcmp (keys[i].uid[1].comment, key->uids->next->comment)) { fprintf (stderr, "Unexpected comment in second user ID: %s\n", key->uids->next->comment); exit (1); } if (keys[i].uid[1].email && strcmp (keys[i].uid[1].email, key->uids->next->email)) { fprintf (stderr, "Unexpected email in second user ID: %s\n", key->uids->next->email); exit (1); } /* Note: There is a bug in gpg 1.3.4 which duplicates a signature after importing the secret key. Thus we disable the second part of the check. */ if (key->uids && (!key->uids->next->signatures /*|| key->uids->next->signatures->next*/)) { fprintf (stderr, "Second user ID unexpected number of signatures\n"); exit (1); } if (keys[i].uid[1].sig.algo != key->uids->next->signatures->pubkey_algo) { fprintf (stderr, "Unexpected algorithm in second user ID sig: %s\n", gpgme_pubkey_algo_name (key->uids->next->signatures->pubkey_algo)); exit (1); } if (strcmp (keys[i].uid[1].sig.keyid, key->uids->next->signatures->keyid)) { fprintf (stderr, "Unexpected key ID in second user ID sig: %s\n", key->uids->next->signatures->keyid); exit (1); } if (strcmp (keys[i].uid[1].sig.name, key->uids->next->signatures->name)) { fprintf (stderr, "Unexpected name in second user ID sig: %s\n", key->uids->next->signatures->name); exit (1); } if (strcmp (keys[i].uid[1].sig.comment, key->uids->next->signatures->comment)) { fprintf (stderr, "Unexpected comment in second user ID sig: %s\n", key->uids->next->signatures->comment); exit (1); } if (strcmp (keys[i].uid[1].sig.email, key->uids->next->signatures->email)) { fprintf (stderr, "Unexpected email in second user ID sig: %s\n", key->uids->next->signatures->email); exit (1); } if (keys[i].uid[1].sig.sig_class != key->uids->next->signatures->sig_class) { fprintf (stderr, "Unexpected class in second user ID sig: %i\n", key->uids->next->signatures->sig_class); exit (1); } if (keys[i].uid[1].sig.exportable != key->uids->next->signatures->exportable) { fprintf (stderr, "Unexpected exportable stat in second user ID sig: %i\n", key->uids->next->signatures->exportable); exit (1); } if (key->uids && key->uids->next && key->uids->next->next && key->uids->next->next->revoked) { fprintf (stderr, "Third user ID unexpectedly revoked\n"); exit (1); } if (key->uids && key->uids->next && key->uids->next->next && key->uids->next->next->invalid) { fprintf (stderr, "Third user ID unexpectedly invalid\n"); exit (1); } if (key->uids && key->uids->next && key->uids->next->next && key->uids->next->next->validity != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "Third user ID has unexpectedly validity: %i\n", key->uids->next->next->validity); exit (1); } if (keys[i].uid[2].name && strcmp (keys[i].uid[2].name, key->uids->next->next->name)) { fprintf (stderr, "Unexpected name in third user ID: %s\n", key->uids->next->next->name); exit (1); } if (keys[i].uid[2].comment && strcmp (keys[i].uid[2].comment, key->uids->next->next->comment)) { fprintf (stderr, "Unexpected comment in third user ID: %s\n", key->uids->next->next->comment); exit (1); } if (keys[i].uid[2].email && strcmp (keys[i].uid[2].email, key->uids->next->next->email)) { fprintf (stderr, "Unexpected email in third user ID: %s\n", key->uids->next->next->email); exit (1); } if (key->uids && (!key->uids->next->next->signatures || key->uids->next->next->signatures->next)) { fprintf (stderr, "Third user ID unexpected number of signatures\n"); exit (1); } if (keys[i].uid[2].sig.algo != key->uids->next->next->signatures->pubkey_algo) { fprintf (stderr, "Unexpected algorithm in third user ID sig: %s\n", gpgme_pubkey_algo_name (key->uids->next->next->signatures->pubkey_algo)); exit (1); } if (strcmp (keys[i].uid[2].sig.keyid, key->uids->next->next->signatures->keyid)) { fprintf (stderr, "Unexpected key ID in third user ID sig: %s\n", key->uids->next->next->signatures->keyid); exit (1); } if (strcmp (keys[i].uid[2].sig.name, key->uids->next->next->signatures->name)) { fprintf (stderr, "Unexpected name in third user ID sig: %s\n", key->uids->next->next->signatures->name); exit (1); } if (strcmp (keys[i].uid[2].sig.comment, key->uids->next->next->signatures->comment)) { fprintf (stderr, "Unexpected comment in third user ID sig: %s\n", key->uids->next->next->signatures->comment); exit (1); } if (strcmp (keys[i].uid[2].sig.email, key->uids->next->next->signatures->email)) { fprintf (stderr, "Unexpected email in third user ID sig: %s\n", key->uids->next->next->signatures->email); exit (1); } if (keys[i].uid[2].sig.sig_class != key->uids->next->next->signatures->sig_class) { fprintf (stderr, "Unexpected class in third user ID sig: %i\n", key->uids->next->next->signatures->sig_class); exit (1); } if (keys[i].uid[2].sig.exportable != key->uids->next->next->signatures->exportable) { fprintf (stderr, "Unexpected exportable stat in third user ID sig: %i\n", key->uids->next->next->signatures->exportable); exit (1); } gpgme_key_unref (key); i++; } if (gpgme_err_code (err) != GPG_ERR_EOF) fail_if_err (err); err = gpgme_op_keylist_end (ctx); fail_if_err (err); result = gpgme_op_keylist_result (ctx); if (result->truncated) { fprintf (stderr, "Key listing unexpectedly truncated\n"); exit (1); } if (keys[i].fpr) { fprintf (stderr, "Less keys returned than expected\n"); exit (1); } gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-keylist.c b/tests/gpg/t-keylist.c index 04170118..3b4fea18 100644 --- a/tests/gpg/t-keylist.c +++ b/tests/gpg/t-keylist.c @@ -1,606 +1,606 @@ /* t-keylist.c - regression test - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" struct key_info_s { const char *fpr; const char *sec_keyid; struct { const char *name; const char *comment; const char *email; } uid[3]; int n_subkeys; void (*misc_check)(struct key_info_s *keyinfo, gpgme_key_t key); }; static void check_whisky (struct key_info_s *keyinfo, gpgme_key_t key); struct key_info_s keys[] = { { "A0FF4590BB6122EDEF6E3C542D727CC768697734", "6AE6D7EE46A871F8", { { "Alfa Test", "demo key", "alfa@example.net" }, { "Alpha Test", "demo key", "alpha@example.net" }, { "Alice", "demo key", NULL } }, 1 }, { "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", "5381EA4EE29BA37F", { { "Bob", "demo key", NULL }, { "Bravo Test", "demo key", "bravo@example.net" } }, 1 }, { "61EE841A2A27EB983B3B3C26413F4AF31AFDAB6C", "E71E72ACBC43DA60", { { "Charlie Test", "demo key", "charlie@example.net" } }, 1 }, { "6560C59C43D031C54D7C588EEBA9F240EB9DC9E6", "06F22880B0C45424", { { "Delta Test", "demo key", "delta@example.net" } }, 1 }, { "3531152DE293E26A07F504BC318C1FAEFAEF6D1B", "B5C79E1A7272144D", { { "Echelon", "demo key", NULL }, { "Echo Test", "demo key", "echo@example.net" }, { "Eve", "demo key", NULL } }, 1 }, { "56D33268F7FE693FBB594762D4BF57F37372E243", "0A32EE79EE45198E", { { "Foxtrot Test", "demo key", "foxtrot@example.net" } }, 1 }, { "C9C07DCC6621B9FB8D071B1D168410A48FC282E6", "247491CC9DCAD354", { { "Golf Test", "demo key", "golf@example.net" } }, 1 }, { "9E91CBB11E4D4135583EF90513DB965534C6E3F1", "76E26537D622AD0A", { { "Hotel Test", "demo key", "hotel@example.net" } }, 1 }, { "CD538D6CC9FB3D745ECDA5201FE8FC6F04259677", "C1C8EFDE61F76C73", { { "India Test", "demo key", "india@example.net" } }, 1 }, { "F8F1EDC73995AB739AD54B380C820C71D2699313", "BD0B108735F8F136", { { "Juliet Test", "demo key", "juliet@example.net" } }, 1 }, { "3FD11083779196C2ECDD9594AD1B0FAD43C2D0C7", "86CBB34A9AF64D02", { { "Kilo Test", "demo key", "kilo@example.net" } }, 1 }, { "1DDD28CEF714F5B03B8C246937CAB51FB79103F8", "0363B449FE56350C", { { "Lima Test", "demo key", "lima@example.net" } }, 1 }, { "2686AA191A278013992C72EBBE794852BE5CF886", "5F600A834F31EAE8", { { "Mallory", "demo key", NULL }, { "Mike Test", "demo key", "mike@example.net" } }, 1 }, { "5AB9D6D7BAA1C95B3BAA3D9425B00FD430CEC684", "4C1D63308B70E472", { { "November Test", "demo key", "november@example.net" } }, 1 }, { "43929E89F8F79381678CAE515F6356BA6D9732AC", "FF0785712681619F", { { "Oscar Test", "demo key", "oscar@example.net" } }, 1 }, { "6FAA9C201E5E26DCBAEC39FD5D15E01D3FF13206", "2764E18263330D9C", { { "Papa test", "demo key", "papa@example.net" } }, 1 }, { "A7969DA1C3297AA96D49843F1C67EC133C661C84", "6CDCFC44A029ACF4", { { "Quebec Test", "demo key", "quebec@example.net" } }, 1 }, { "38FBE1E4BF6A5E1242C8F6A13BDBEDB1777FBED3", "9FAB805A11D102EA", { { "Romeo Test", "demo key", "romeo@example.net" } }, 1 }, { "045B2334ADD69FC221076841A5E67F7FA3AE3EA1", "93B88B0F0F1B50B4", { { "Sierra Test", "demo key", "sierra@example.net" } }, 1 }, { "ECAC774F4EEEB0620767044A58CB9A4C85A81F38", "97B60E01101C0402", { { "Tango Test", "demo key", "tango@example.net" } }, 1 }, { "0DBCAD3F08843B9557C6C4D4A94C0F75653244D6", "93079B915522BDB9", { { "Uniform Test", "demo key", "uniform@example.net" } }, 1 }, { "E8143C489C8D41124DC40D0B47AF4B6961F04784", "04071FB807287134", { { "Victor Test", "demo key", "victor@example.org" } }, 1 }, { "E8D6C90B683B0982BD557A99DEF0F7B8EC67DBDE", "D7FBB421FD6E27F6", { { "Whisky Test", "demo key", "whisky@example.net" } }, 3, check_whisky }, { "04C1DF62EFA0EBB00519B06A8979A6C5567FB34A", "5CC6F87F41E408BE", { { "XRay Test", "demo key", "xray@example.net" } }, 1 }, { "ED9B316F78644A58D042655A9EEF34CD4B11B25F", "5ADFD255F7B080AD", { { "Yankee Test", "demo key", "yankee@example.net" } }, 1 }, { "23FD347A419429BACCD5E72D6BC4778054ACD246", "EF9DC276A172C881", { { "Zulu Test", "demo key", "zulu@example.net" } }, 1 }, { "ADAB7FCC1F4DE2616ECFA402AF82244F9CD9FD55", "087DD7E0381701C4", { { "Joe Random Hacker", "test key with passphrase \"abc\"", "joe@example.com" } }, 1 }, { NULL } }; int main (int argc, char **argv) { gpgme_error_t err; gpgme_ctx_t ctx; gpgme_key_t key; gpgme_keylist_result_t result; int i = 0; int n; gpgme_subkey_t subkey; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_op_keylist_start (ctx, NULL, 0); fail_if_err (err); while (!(err = gpgme_op_keylist_next (ctx, &key))) { if (!keys[i].fpr) { fprintf (stderr, "More keys returned than expected\n"); exit (1); } /* Global key flags. */ if (key->revoked) { fprintf (stderr, "Key unexpectedly revoked\n"); exit (1); } if (key->expired) { fprintf (stderr, "Key unexpectedly expired\n"); exit (1); } if (key->disabled) { fprintf (stderr, "Key unexpectedly disabled\n"); exit (1); } if (key->invalid) { fprintf (stderr, "Key unexpectedly invalid\n"); exit (1); } #if 0 /* GnuPG 2.1+ have a different subkey for encryption. */ if (!key->can_encrypt) { fprintf (stderr, "Key unexpectedly unusable for encryption\n"); exit (1); } #endif if (!key->can_sign) { fprintf (stderr, "Key unexpectedly unusable for signing\n"); exit (1); } if (!key->can_certify) { fprintf (stderr, "Key unexpectedly unusable for certifications\n"); exit (1); } if (key->secret) { fprintf (stderr, "Key unexpectedly secret\n"); exit (1); } if (key->protocol != GPGME_PROTOCOL_OpenPGP) { fprintf (stderr, "Key has unexpected protocol: %s\n", gpgme_get_protocol_name (key->protocol)); exit (1); } if (key->issuer_serial) { fprintf (stderr, "Key unexpectedly carries issuer serial: %s\n", key->issuer_serial); exit (1); } if (key->issuer_name) { fprintf (stderr, "Key unexpectedly carries issuer name: %s\n", key->issuer_name); exit (1); } if (key->chain_id) { fprintf (stderr, "Key unexpectedly carries chain ID: %s\n", key->chain_id); exit (1); } if (key->owner_trust != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "Key has unexpected owner trust: %i\n", key->owner_trust); exit (1); } for (n=0, subkey = key->subkeys; subkey; subkey = subkey->next) n++; if (!n || n-1 != keys[i].n_subkeys) { fprintf (stderr, "Key `%s' has unexpected number of subkeys\n", keys[i].uid[0].name); exit (1); } /* Primary key. */ if (key->subkeys->revoked) { fprintf (stderr, "Primary key unexpectedly revoked\n"); exit (1); } if (key->subkeys->expired) { fprintf (stderr, "Primary key unexpectedly expired\n"); exit (1); } if (key->subkeys->disabled) { fprintf (stderr, "Primary key unexpectedly disabled\n"); exit (1); } if (key->subkeys->invalid) { fprintf (stderr, "Primary key unexpectedly invalid\n"); exit (1); } if (key->subkeys->can_encrypt) { fprintf (stderr, "Primary key unexpectedly usable for encryption\n"); exit (1); } if (!key->subkeys->can_sign) { fprintf (stderr, "Primary key unexpectedly unusable for signing\n"); exit (1); } if (!key->subkeys->can_certify) { fprintf (stderr, "Primary key unexpectedly unusable for certifications\n"); exit (1); } if (key->subkeys->secret) { fprintf (stderr, "Primary key unexpectedly secret\n"); exit (1); } if (key->subkeys->is_cardkey) { fprintf (stderr, "Public key marked as card key\n"); exit (1); } if (key->subkeys->card_number) { fprintf (stderr, "Public key with card number set\n"); exit (1); } if (key->subkeys->pubkey_algo != GPGME_PK_DSA) { fprintf (stderr, "Primary key has unexpected public key algo: %s\n", gpgme_pubkey_algo_name (key->subkeys->pubkey_algo)); exit (1); } if (key->subkeys->length != 1024) { fprintf (stderr, "Primary key has unexpected length: %i\n", key->subkeys->length); exit (1); } if (strcmp (key->subkeys->keyid, &keys[i].fpr[40 - 16])) { fprintf (stderr, "Primary key `%s' has unexpected key ID: %s\n", keys[i].uid[0].name, key->subkeys->keyid); exit (1); } if (strcmp (key->subkeys->fpr, keys[i].fpr)) { fprintf (stderr, "Primary key has unexpected fingerprint: %s\n", key->subkeys->fpr); exit (1); } if (key->subkeys->expires) { fprintf (stderr, "Primary key `%s' unexpectedly expires: %lu\n", keys[i].uid[0].name, key->subkeys->expires); exit (1); } /* Secondary key. */ if (key->subkeys->next->revoked) { fprintf (stderr, "Secondary key unexpectedly revoked\n"); exit (1); } if (key->subkeys->next->expired) { fprintf (stderr, "Secondary key unexpectedly expired\n"); exit (1); } if (key->subkeys->next->disabled) { fprintf (stderr, "Secondary key unexpectedly disabled\n"); exit (1); } if (key->subkeys->next->invalid) { fprintf (stderr, "Secondary key unexpectedly invalid\n"); exit (1); } if (!key->subkeys->next->can_encrypt) { fprintf (stderr, "Secondary key unexpectedly unusable for encryption\n"); exit (1); } if (key->subkeys->next->can_sign) { fprintf (stderr, "Secondary key unexpectedly usable for signing\n"); exit (1); } if (key->subkeys->next->can_certify) { fprintf (stderr, "Secondary key unexpectedly usable for certifications\n"); exit (1); } if (key->subkeys->next->secret) { fprintf (stderr, "Secondary key unexpectedly secret\n"); exit (1); } if (key->subkeys->next->is_cardkey) { fprintf (stderr, "Secondary public key marked as card key\n"); exit (1); } if (key->subkeys->next->card_number) { fprintf (stderr, "Secondary public key with card number set\n"); exit (1); } if (key->subkeys->next->pubkey_algo != GPGME_PK_ELG_E) { fprintf (stderr, "Secondary key has unexpected public key algo: %s\n", gpgme_pubkey_algo_name (key->subkeys->next->pubkey_algo)); exit (1); } if (key->subkeys->next->length != 1024) { fprintf (stderr, "Secondary key has unexpected length: %i\n", key->subkeys->next->length); exit (1); } if (strcmp (key->subkeys->next->keyid, keys[i].sec_keyid)) { fprintf (stderr, "Secondary key `%s' has unexpected key ID: %s/%s\n", keys[i].uid[0].name, key->subkeys->next->keyid, keys[i].sec_keyid ); exit (1); } if (!key->subkeys->next->fpr) { fprintf (stderr, "Secondary key has unexpectedly no fingerprint\n"); exit (1); } if (key->subkeys->next->expires) { fprintf (stderr, "Secondary key unexpectedly expires: %lu\n", key->subkeys->next->expires); exit (1); } /* FIXME: The below test will crash if we want to check for a name, comment or email that doesn't exist in the key's user IDs. */ if (!((!keys[i].uid[0].name && !key->uids) || (keys[i].uid[0].name && !keys[i].uid[1].name && key->uids && !key->uids->next) || (keys[i].uid[0].name && keys[i].uid[1].name && !keys[i].uid[2].name && key->uids && key->uids->next && !key->uids->next->next) || (keys[i].uid[0].name && keys[i].uid[1].name && keys[i].uid[2].name && key->uids && key->uids->next && key->uids->next->next && !key->uids->next->next->next))) { fprintf (stderr, "Key has unexpected number of user IDs\n"); exit (1); } if (key->uids && key->uids->revoked) { fprintf (stderr, "First user ID unexpectedly revoked\n"); exit (1); } if (key->uids && key->uids->invalid) { fprintf (stderr, "First user ID unexpectedly invalid\n"); exit (1); } if (key->uids && key->uids->validity != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "First user ID has unexpectedly validity: %i\n", key->uids->validity); exit (1); } if (key->uids && key->uids->signatures) { fprintf (stderr, "First user ID unexpectedly signed\n"); exit (1); } if (keys[i].uid[0].name && strcmp (keys[i].uid[0].name, key->uids->name)) { fprintf (stderr, "Unexpected name in first user ID: %s\n", key->uids->name); exit (1); } if (keys[i].uid[0].comment && strcmp (keys[i].uid[0].comment, key->uids->comment)) { fprintf (stderr, "Unexpected comment in first user ID: %s\n", key->uids->comment); exit (1); } if (keys[i].uid[0].email && strcmp (keys[i].uid[0].email, key->uids->email)) { fprintf (stderr, "Unexpected email in first user ID: %s\n", key->uids->email); exit (1); } if (key->uids && key->uids->next && key->uids->next->revoked) { fprintf (stderr, "Second user ID unexpectedly revoked\n"); exit (1); } if (key->uids && key->uids->next && key->uids->next->invalid) { fprintf (stderr, "Second user ID unexpectedly invalid\n"); exit (1); } if (key->uids && key->uids->next && key->uids->next->validity != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "Second user ID has unexpectedly validity: %i\n", key->uids->next->validity); exit (1); } if (key->uids && key->uids->next && key->uids->next->signatures) { fprintf (stderr, "Second user ID unexpectedly signed\n"); exit (1); } if (keys[i].uid[1].name && strcmp (keys[i].uid[1].name, key->uids->next->name)) { fprintf (stderr, "Unexpected name in second user ID: %s\n", key->uids->next->name); exit (1); } if (keys[i].uid[1].comment && strcmp (keys[i].uid[1].comment, key->uids->next->comment)) { fprintf (stderr, "Unexpected comment in second user ID: %s\n", key->uids->next->comment); exit (1); } if (keys[i].uid[1].email && strcmp (keys[i].uid[1].email, key->uids->next->email)) { fprintf (stderr, "Unexpected email in second user ID: %s\n", key->uids->next->email); exit (1); } if (key->uids && key->uids->next && key->uids->next->next && key->uids->next->next->revoked) { fprintf (stderr, "Third user ID unexpectedly revoked\n"); exit (1); } if (key->uids && key->uids->next && key->uids->next->next && key->uids->next->next->invalid) { fprintf (stderr, "Third user ID unexpectedly invalid\n"); exit (1); } if (key->uids && key->uids->next && key->uids->next->next && key->uids->next->next->validity != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "Third user ID has unexpectedly validity: %i\n", key->uids->next->next->validity); exit (1); } if (key->uids && key->uids->next && key->uids->next->next && key->uids->next->next->signatures) { fprintf (stderr, "Third user ID unexpectedly signed\n"); exit (1); } if (keys[i].uid[2].name && strcmp (keys[i].uid[2].name, key->uids->next->next->name)) { fprintf (stderr, "Unexpected name in third user ID: %s\n", key->uids->next->next->name); exit (1); } if (keys[i].uid[2].comment && strcmp (keys[i].uid[2].comment, key->uids->next->next->comment)) { fprintf (stderr, "Unexpected comment in third user ID: %s\n", key->uids->next->next->comment); exit (1); } if (keys[i].uid[2].email && strcmp (keys[i].uid[2].email, key->uids->next->next->email)) { fprintf (stderr, "Unexpected email in third user ID: %s\n", key->uids->next->next->email); exit (1); } if (keys[i].misc_check) keys[i].misc_check (keys+i, key); gpgme_key_unref (key); i++; } if (gpgme_err_code (err) != GPG_ERR_EOF) fail_if_err (err); err = gpgme_op_keylist_end (ctx); fail_if_err (err); result = gpgme_op_keylist_result (ctx); if (result->truncated) { fprintf (stderr, "Key listing unexpectedly truncated\n"); exit (1); } if (keys[i].fpr) { fprintf (stderr, "Less keys (%d) returned than expected (%d)\n", i, (int)(DIM (keys) - 1)); exit (1); } gpgme_release (ctx); return 0; } /* Check expration of keys. This test assumes three subkeys of which 2 are expired; it is used with the "Whisky" test key. It has already been checked that these 3 subkeys are available. */ static void check_whisky (struct key_info_s *keyinfo, gpgme_key_t key) { const char *name = keyinfo->uid[0].name; gpgme_subkey_t sub1, sub2; sub1 = key->subkeys->next->next; sub2 = sub1->next; if (!sub1->expired || !sub2->expired) { fprintf (stderr, "Subkey of `%s' not flagged as expired\n", name); exit (1); } if (sub1->expires != 1129636886 || sub2->expires != 1129636939) { fprintf (stderr, "Subkey of `%s' has wrong expiration date\n", name); exit (1); } } diff --git a/tests/gpg/t-sig-notation.c b/tests/gpg/t-sig-notation.c index 11443bc9..df45cfd5 100644 --- a/tests/gpg/t-sig-notation.c +++ b/tests/gpg/t-sig-notation.c @@ -1,198 +1,198 @@ /* t-sig-notation.c - Regression test. - Copyright (C) 2005 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2005 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" /* GnuPG prior to 2.1.13 did not report the critical flag correctly. */ int have_correct_sig_data; static struct { const char *name; const char *value; gpgme_sig_notation_flags_t flags; int seen; } expected_notations[] = { { "laughing@me", "Just Squeeze Me", GPGME_SIG_NOTATION_HUMAN_READABLE }, { "preferred-email-encoding@pgp.com", "pgpmime", GPGME_SIG_NOTATION_HUMAN_READABLE | GPGME_SIG_NOTATION_CRITICAL }, { NULL, "https://www.gnu.org/policy/", 0 } }; static void check_result (gpgme_verify_result_t result) { int i; gpgme_sig_notation_t r; gpgme_signature_t sig; sig = result->signatures; if (!sig || sig->next) { fprintf (stderr, "%s:%i: Unexpected number of signatures\n", __FILE__, __LINE__); exit (1); } for (i=0; i < DIM(expected_notations); i++ ) expected_notations[i].seen = 0; for (r = result->signatures->notations; r; r = r->next) { int any = 0; for (i=0; i < DIM(expected_notations); i++) { if ( ((r->name && expected_notations[i].name && !strcmp (r->name, expected_notations[i].name) && r->name_len == strlen (expected_notations[i].name)) || (!r->name && !expected_notations[i].name && r->name_len == 0)) && r->value && !strcmp (r->value, expected_notations[i].value) && r->value_len == strlen (expected_notations[i].value) && r->flags == (have_correct_sig_data ? expected_notations[i].flags : expected_notations[i].flags & ~GPGME_SIG_NOTATION_CRITICAL) && r->human_readable == !!(r->flags & GPGME_SIG_NOTATION_HUMAN_READABLE) && r->critical == (have_correct_sig_data ? !!(r->flags & GPGME_SIG_NOTATION_CRITICAL) : 0)) { expected_notations[i].seen++; any++; } } if (!any) { fprintf (stderr, "%s:%i: Unexpected notation data\n", __FILE__, __LINE__); exit (1); } } for (i=0; i < DIM(expected_notations); i++ ) { if (expected_notations[i].seen != 1) { fprintf (stderr, "%s:%i: Missing or duplicate notation data\n", __FILE__, __LINE__); exit (1); } } } int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_verify_result_t result; char *agent_info; int i; gpgme_engine_info_t engine_info; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_get_engine_info (&engine_info); fail_if_err (err); for (; engine_info; engine_info = engine_info->next) if (engine_info->protocol == GPGME_PROTOCOL_OpenPGP) break; assert (engine_info); /* GnuPG prior to 2.1.13 did not report the critical flag correctly. */ have_correct_sig_data = ! (strncmp ("1.", engine_info->version, 2) == 0 || strncmp ("2.0.", engine_info->version, 4) == 0 || (strncmp ("2.1.1", engine_info->version, 5) == 0 && (engine_info->version[5] == 0 || engine_info->version[5] < '3'))); err = gpgme_new (&ctx); fail_if_err (err); agent_info = getenv ("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); for (i = 0; i < sizeof (expected_notations) / sizeof (expected_notations[0]); i++) { err = gpgme_sig_notation_add (ctx, expected_notations[i].name, expected_notations[i].value, expected_notations[i].flags); fail_if_err (err); } err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL); fail_if_err (err); gpgme_data_release (in); err = gpgme_data_new (&in); fail_if_err (err); gpgme_data_seek (out, 0, SEEK_SET); err = gpgme_op_verify (ctx, out, NULL, in); fail_if_err (err); result = gpgme_op_verify_result (ctx); check_result (result); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-sign.c b/tests/gpg/t-sign.c index 66f54eaf..54138241 100644 --- a/tests/gpg/t-sign.c +++ b/tests/gpg/t-sign.c @@ -1,158 +1,158 @@ /* t-sign.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" static void check_result (gpgme_sign_result_t result, gpgme_sig_mode_t type) { if (result->invalid_signers) { fprintf (stderr, "Invalid signer found: %s\n", result->invalid_signers->fpr); exit (1); } if (!result->signatures || result->signatures->next) { fprintf (stderr, "Unexpected number of signatures created\n"); exit (1); } if (result->signatures->type != type) { fprintf (stderr, "Wrong type of signature created\n"); exit (1); } if (result->signatures->pubkey_algo != GPGME_PK_DSA) { fprintf (stderr, "Wrong pubkey algorithm reported: %i\n", result->signatures->pubkey_algo); exit (1); } if (result->signatures->hash_algo != GPGME_MD_SHA1) { fprintf (stderr, "Wrong hash algorithm reported: %i\n", result->signatures->hash_algo); exit (1); } if (result->signatures->sig_class != 1) { fprintf (stderr, "Wrong signature class reported: %u\n", result->signatures->sig_class); exit (1); } if (strcmp ("A0FF4590BB6122EDEF6E3C542D727CC768697734", result->signatures->fpr)) { fprintf (stderr, "Wrong fingerprint reported: %s\n", result->signatures->fpr); exit (1); } } int main (int argc, char **argv) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_sign_result_t result; char *agent_info; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); agent_info = getenv ("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); gpgme_set_textmode (ctx, 1); gpgme_set_armor (ctx, 1); #if 0 { gpgme_key_t akey; err = gpgme_get_key (ctx, "0x68697734", &akey, 0); fail_if_err (err); err = gpgme_signers_add (ctx, akey); fail_if_err (err); gpgme_key_unref (akey); } #endif err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); /* First a normal signature. */ err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL); fail_if_err (err); result = gpgme_op_sign_result (ctx); check_result (result, GPGME_SIG_MODE_NORMAL); print_data (out); gpgme_data_release (out); /* Now a detached signature. */ gpgme_data_seek (in, 0, SEEK_SET); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH); fail_if_err (err); result = gpgme_op_sign_result (ctx); check_result (result, GPGME_SIG_MODE_DETACH); print_data (out); gpgme_data_release (out); /* And finally a cleartext signature. */ gpgme_data_seek (in, 0, SEEK_SET); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_CLEAR); fail_if_err (err); result = gpgme_op_sign_result (ctx); check_result (result, GPGME_SIG_MODE_CLEAR); print_data (out); gpgme_data_release (out); gpgme_data_release (in); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-signers.c b/tests/gpg/t-signers.c index b1eb1913..cc8609a9 100644 --- a/tests/gpg/t-signers.c +++ b/tests/gpg/t-signers.c @@ -1,177 +1,177 @@ /* t-signers.c - Regression tests for the multiple signers interface. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" static void check_result (gpgme_sign_result_t result, gpgme_sig_mode_t type) { gpgme_new_signature_t signature; if (result->invalid_signers) { fprintf (stderr, "Invalid signer found: %s\n", result->invalid_signers->fpr); exit (1); } if (!result->signatures || !result->signatures->next || result->signatures->next->next) { fprintf (stderr, "Unexpected number of signatures created\n"); exit (1); } signature = result->signatures; while (signature) { if (signature->type != type) { fprintf (stderr, "Wrong type of signature created\n"); exit (1); } if (signature->pubkey_algo != GPGME_PK_DSA) { fprintf (stderr, "Wrong pubkey algorithm reported: %i\n", signature->pubkey_algo); exit (1); } if (signature->hash_algo != GPGME_MD_SHA1) { fprintf (stderr, "Wrong hash algorithm reported: %i\n", signature->hash_algo); exit (1); } if (signature->sig_class != 1) { fprintf (stderr, "Wrong signature class reported: %u\n", signature->sig_class); exit (1); } if (strcmp ("A0FF4590BB6122EDEF6E3C542D727CC768697734", signature->fpr) && strcmp ("23FD347A419429BACCD5E72D6BC4778054ACD246", signature->fpr)) { fprintf (stderr, "Wrong fingerprint reported: %s\n", signature->fpr); exit (1); } signature = signature->next; } } int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_key_t key[2]; gpgme_sign_result_t result; char *agent_info; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); agent_info = getenv("GPG_AGENT_INFO"); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); gpgme_set_textmode (ctx, 1); gpgme_set_armor (ctx, 1); err = gpgme_op_keylist_start (ctx, NULL, 1); fail_if_err (err); err = gpgme_op_keylist_next (ctx, &key[0]); fail_if_err (err); err = gpgme_op_keylist_next (ctx, &key[1]); fail_if_err (err); err = gpgme_op_keylist_end (ctx); fail_if_err (err); err = gpgme_signers_add (ctx, key[0]); fail_if_err (err); err = gpgme_signers_add (ctx, key[1]); fail_if_err (err); err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); /* First a normal signature. */ err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL); fail_if_err (err); result = gpgme_op_sign_result (ctx); check_result (result, GPGME_SIG_MODE_NORMAL); print_data (out); gpgme_data_release (out); /* Now a detached signature. */ gpgme_data_seek (in, 0, SEEK_SET); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH); fail_if_err (err); result = gpgme_op_sign_result (ctx); check_result (result, GPGME_SIG_MODE_DETACH); print_data (out); gpgme_data_release (out); /* And finally a cleartext signature. */ gpgme_data_seek (in, 0, SEEK_SET); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_CLEAR); fail_if_err (err); result = gpgme_op_sign_result (ctx); check_result (result, GPGME_SIG_MODE_CLEAR); print_data (out); gpgme_data_release (out); gpgme_data_seek (in, 0, SEEK_SET); gpgme_data_release (in); gpgme_release (ctx); gpgme_key_unref (key[0]); gpgme_key_unref (key[1]); return 0; } diff --git a/tests/gpg/t-support.h b/tests/gpg/t-support.h index b6ad515a..b3f54e57 100644 --- a/tests/gpg/t-support.h +++ b/tests/gpg/t-support.h @@ -1,218 +1,218 @@ /* t-support.h - Helper routines for regression tests. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #include #include #include #include #include #include #ifdef HAVE_W32_SYSTEM #include #endif #include #ifndef DIM #define DIM(v) (sizeof(v)/sizeof((v)[0])) #endif #define fail_if_err(err) \ do \ { \ if (err) \ { \ fprintf (stderr, "%s:%d: %s: %s\n", \ __FILE__, __LINE__, gpgme_strsource (err), \ gpgme_strerror (err)); \ exit (1); \ } \ } \ while (0) #ifdef GPGRT_HAVE_MACRO_FUNCTION void GPGRT_ATTR_NORETURN _test (const char *expr, const char *file, int line, const char *func) { fprintf (stderr, "Test \"%s\" in %s failed (%s:%d)\n", expr, func, file, line); exit (1); } # define test(expr) \ ((expr) \ ? (void) 0 \ : _test (#expr, __FILE__, __LINE__, __FUNCTION__)) #else /*!GPGRT_HAVE_MACRO_FUNCTION*/ void _test (const char *expr, const char *file, int line) { fprintf (stderr, "Test \"%s\" failed (%s:%d)\n", expr, file, line); exit (1); } # define test(expr) \ ((expr) \ ? (void) 0 \ : _test (#expr, __FILE__, __LINE__)) #endif /*!GPGRT_HAVE_MACRO_FUNCTION*/ static const char * nonnull (const char *s) { return s? s :"[none]"; } void print_data (gpgme_data_t dh) { #define BUF_SIZE 512 char buf[BUF_SIZE + 1]; int ret; ret = gpgme_data_seek (dh, 0, SEEK_SET); if (ret) fail_if_err (gpgme_err_code_from_errno (errno)); while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0) fwrite (buf, ret, 1, stdout); if (ret < 0) fail_if_err (gpgme_err_code_from_errno (errno)); } gpgme_error_t passphrase_cb (void *opaque, const char *uid_hint, const char *passphrase_info, int last_was_bad, int fd) { int res; char pass[] = "abc\n"; int passlen = strlen (pass); int off = 0; (void)opaque; (void)uid_hint; (void)passphrase_info; (void)last_was_bad; do { res = gpgme_io_write (fd, &pass[off], passlen - off); if (res > 0) off += res; } while (res > 0 && off != passlen); return off == passlen ? 0 : gpgme_error_from_errno (errno); } char * make_filename (const char *fname) { const char *srcdir = getenv ("srcdir"); char *buf; if (!srcdir) srcdir = "."; buf = malloc (strlen(srcdir) + strlen(fname) + 2); if (!buf) exit (8); strcpy (buf, srcdir); strcat (buf, "/"); strcat (buf, fname); return buf; } void init_gpgme (gpgme_protocol_t proto) { gpgme_error_t err; gpgme_check_version (NULL); setlocale (LC_ALL, ""); gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); #ifndef HAVE_W32_SYSTEM gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); #endif err = gpgme_engine_check_version (proto); fail_if_err (err); } void print_import_result (gpgme_import_result_t r) { gpgme_import_status_t st; for (st=r->imports; st; st = st->next) { printf (" fpr: %s err: %d (%s) status:", nonnull (st->fpr), st->result, gpgme_strerror (st->result)); if (st->status & GPGME_IMPORT_NEW) fputs (" new", stdout); if (st->status & GPGME_IMPORT_UID) fputs (" uid", stdout); if (st->status & GPGME_IMPORT_SIG) fputs (" sig", stdout); if (st->status & GPGME_IMPORT_SUBKEY) fputs (" subkey", stdout); if (st->status & GPGME_IMPORT_SECRET) fputs (" secret", stdout); putchar ('\n'); } printf ("key import summary:\n" " considered: %d\n" " no user id: %d\n" " imported: %d\n" " imported_rsa: %d\n" " unchanged: %d\n" " new user ids: %d\n" " new subkeys: %d\n" " new signatures: %d\n" " new revocations: %d\n" " secret read: %d\n" " secret imported: %d\n" " secret unchanged: %d\n" " skipped new keys: %d\n" " not imported: %d\n" " skipped v3 keys: %d\n", r->considered, r->no_user_id, r->imported, r->imported_rsa, r->unchanged, r->new_user_ids, r->new_sub_keys, r->new_signatures, r->new_revocations, r->secret_read, r->secret_imported, r->secret_unchanged, r->skipped_new_keys, r->not_imported, r->skipped_v3_keys); } diff --git a/tests/gpg/t-thread-keylist-verify.c b/tests/gpg/t-thread-keylist-verify.c index c011fbc0..3b31827f 100644 --- a/tests/gpg/t-thread-keylist-verify.c +++ b/tests/gpg/t-thread-keylist-verify.c @@ -1,143 +1,143 @@ /* t-thread-verify.c - Regression test. - Copyright (C) 2015 by Bundesamt für Sicherheit in der Informationstechnik - Software engineering by Intevation GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2015 by Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" #define THREAD_COUNT 10 static const char test_text1[] = "Just GNU it!\n"; static const char test_sig1[] = "-----BEGIN PGP SIGNATURE-----\n" "\n" "iN0EABECAJ0FAjoS+i9FFIAAAAAAAwA5YmFyw7bDpMO8w58gZGFzIHdhcmVuIFVt\n" "bGF1dGUgdW5kIGpldHp0IGVpbiBwcm96ZW50JS1aZWljaGVuNRSAAAAAAAgAJGZv\n" "b2Jhci4xdGhpcyBpcyBhIG5vdGF0aW9uIGRhdGEgd2l0aCAyIGxpbmVzGhpodHRw\n" "Oi8vd3d3Lmd1Lm9yZy9wb2xpY3kvAAoJEC1yfMdoaXc0JBIAoIiLlUsvpMDOyGEc\n" "dADGKXF/Hcb+AKCJWPphZCphduxSvrzH0hgzHdeQaA==\n" "=nts1\n" "-----END PGP SIGNATURE-----\n"; void * start_keylist (void *arg) { gpgme_error_t err; gpgme_ctx_t ctx; gpgme_key_t key; (void)arg; err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_op_keylist_start (ctx, NULL, 0); fail_if_err (err); while (!(err = gpgme_op_keylist_next (ctx, &key))) { gpgme_key_unref (key); } gpgme_release (ctx); return NULL; } void * start_verify (void *arg) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t sig, text; gpgme_verify_result_t result; gpgme_signature_t signature; (void)arg; err = gpgme_new (&ctx); fail_if_err (err); /* Checking a valid message. */ err = gpgme_data_new_from_mem (&text, test_text1, strlen (test_text1), 0); fail_if_err (err); err = gpgme_data_new_from_mem (&sig, test_sig1, strlen (test_sig1), 0); fail_if_err (err); err = gpgme_op_verify (ctx, sig, text, NULL); fail_if_err (err); result = gpgme_op_verify_result (ctx); signature = result->signatures; if (strcmp (signature->fpr, "A0FF4590BB6122EDEF6E3C542D727CC768697734")) { fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n", __FILE__, __LINE__, signature->fpr); exit (1); } if (gpgme_err_code (signature->status) != GPG_ERR_NO_ERROR) { fprintf (stderr, "%s:%i: Unexpected signature status: %s\n", __FILE__, __LINE__, gpgme_strerror (signature->status)); exit (1); } gpgme_free (text); gpgme_free (sig); gpgme_release (ctx); return NULL; } int main (int argc, char *argv[]) { int i; pthread_t verify_threads[THREAD_COUNT]; pthread_t keylist_threads[THREAD_COUNT]; init_gpgme (GPGME_PROTOCOL_OpenPGP); (void)argc; (void)argv; for (i = 0; i < THREAD_COUNT; i++) { if (pthread_create(&verify_threads[i], NULL, start_verify, NULL) || pthread_create(&keylist_threads[i], NULL, start_keylist, NULL)) { fprintf(stderr, "%s:%i: failed to create threads \n", __FILE__, __LINE__); exit(1); } } for (i = 0; i < THREAD_COUNT; i++) { pthread_join (verify_threads[i], NULL); pthread_join (keylist_threads[i], NULL); } return 0; } diff --git a/tests/gpg/t-thread-keylist.c b/tests/gpg/t-thread-keylist.c index d64a6d01..853bc3ad 100644 --- a/tests/gpg/t-thread-keylist.c +++ b/tests/gpg/t-thread-keylist.c @@ -1,86 +1,86 @@ /* t-thread-verify.c - Regression test. - Copyright (C) 2015 by Bundesamt für Sicherheit in der Informationstechnik - Software engineering by Intevation GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2015 by Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" #define THREAD_COUNT 10 void * start_keylist (void *arg) { gpgme_error_t err; gpgme_ctx_t ctx; gpgme_key_t key; (void) arg; err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_op_keylist_start (ctx, NULL, 0); fail_if_err (err); while (!(err = gpgme_op_keylist_next (ctx, &key))) { gpgme_key_unref (key); } gpgme_release (ctx); return NULL; } int main (int argc, char *argv[]) { int i; pthread_t keylist_threads[THREAD_COUNT]; init_gpgme (GPGME_PROTOCOL_OpenPGP); (void)argc; (void)argv; for (i = 0; i < THREAD_COUNT; i++) { if (pthread_create(&keylist_threads[i], NULL, start_keylist, NULL)) { fprintf(stderr, "%s:%i: failed to create threads \n", __FILE__, __LINE__); exit(1); } } for (i = 0; i < THREAD_COUNT; i++) { pthread_join (keylist_threads[i], NULL); } return 0; } diff --git a/tests/gpg/t-thread1.c b/tests/gpg/t-thread1.c index 2f9ee5dc..57d00502 100644 --- a/tests/gpg/t-thread1.c +++ b/tests/gpg/t-thread1.c @@ -1,159 +1,159 @@ /* t-thread1.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include #include #include "t-support.h" #define ROUNDS 20 void * thread_one (void *name) { int i; for (i = 0; i < ROUNDS; i++) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_key_t key[3] = { NULL, NULL, NULL }; gpgme_encrypt_result_t result; err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_armor (ctx, 1); err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734", &key[0], 0); fail_if_err (err); err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2", &key[1], 0); fail_if_err (err); err = gpgme_op_encrypt (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out); fail_if_err (err); result = gpgme_op_encrypt_result (ctx); if (result->invalid_recipients) { fprintf (stderr, "Invalid recipient encountered: %s\n", result->invalid_recipients->fpr); exit (1); } printf ("Encrypt %s %i\n", (char *) name, i); gpgme_key_unref (key[0]); gpgme_key_unref (key[1]); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); } return NULL; } void * thread_two (void *name) { int i; char *cipher_1_asc = make_filename ("cipher-1.asc"); char *agent_info; agent_info = getenv("GPG_AGENT_INFO"); for (i = 0; i < ROUNDS; i++) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_decrypt_result_t result; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); if (!(agent_info && strchr (agent_info, ':'))) gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); err = gpgme_data_new_from_file (&in, cipher_1_asc, 1); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_decrypt (ctx, in, out); fail_if_err (err); result = gpgme_op_decrypt_result (ctx); if (result->unsupported_algorithm) { fprintf (stderr, "%s:%i: unsupported algorithm: %s\n", __FILE__, __LINE__, result->unsupported_algorithm); exit (1); } printf ("Decrypt %s %i\n", (char *) name, i); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); } free (cipher_1_asc); return NULL; } int main (void) { pthread_t tone; pthread_t ttwo; char arg_A[] = "A"; char arg_B[] = "B"; init_gpgme (GPGME_PROTOCOL_OpenPGP); pthread_create (&tone, NULL, thread_one, arg_A); pthread_create (&ttwo, NULL, thread_two, arg_B); pthread_join (tone, NULL); pthread_join (ttwo, NULL); return 0; } diff --git a/tests/gpg/t-trustlist.c b/tests/gpg/t-trustlist.c index eeab85e9..54b2f70f 100644 --- a/tests/gpg/t-trustlist.c +++ b/tests/gpg/t-trustlist.c @@ -1,67 +1,67 @@ /* t-trustlist.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_trust_item_t item; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_op_trustlist_start (ctx, "alice", 0); fail_if_err (err); while (!(err = gpgme_op_trustlist_next (ctx, &item))) { printf ("l=%d k=%s t=%d o=%s v=%s u=%s\n", item->level, item->keyid, item->type, item->owner_trust, item->validity, item->name); gpgme_trust_item_unref (item); } if (gpgme_err_code (err) != GPG_ERR_EOF) fail_if_err (err); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-verify.c b/tests/gpg/t-verify.c index fa0164ac..695a994e 100644 --- a/tests/gpg/t-verify.c +++ b/tests/gpg/t-verify.c @@ -1,366 +1,366 @@ /* t-verify.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "t-verify" #include "t-support.h" static const char test_text1[] = "Just GNU it!\n"; static const char test_text1f[]= "Just GNU it?\n"; static const char test_sig1[] = "-----BEGIN PGP SIGNATURE-----\n" "\n" "iN0EABECAJ0FAjoS+i9FFIAAAAAAAwA5YmFyw7bDpMO8w58gZGFzIHdhcmVuIFVt\n" "bGF1dGUgdW5kIGpldHp0IGVpbiBwcm96ZW50JS1aZWljaGVuNRSAAAAAAAgAJGZv\n" "b2Jhci4xdGhpcyBpcyBhIG5vdGF0aW9uIGRhdGEgd2l0aCAyIGxpbmVzGhpodHRw\n" "Oi8vd3d3Lmd1Lm9yZy9wb2xpY3kvAAoJEC1yfMdoaXc0JBIAoIiLlUsvpMDOyGEc\n" "dADGKXF/Hcb+AKCJWPphZCphduxSvrzH0hgzHdeQaA==\n" "=nts1\n" "-----END PGP SIGNATURE-----\n"; /* The same as test_sig1 but with a second signature for which we do * not have the public key (deleted after signature creation). */ static const char test_sig1_plus_unknown_key[] = "-----BEGIN PGP SIGNATURE-----\n" "\n" "iN0EABECAJ0FAjoS+i9FFIAAAAAAAwA5YmFyw7bDpMO8w58gZGFzIHdhcmVuIFVt\n" "bGF1dGUgdW5kIGpldHp0IGVpbiBwcm96ZW50JS1aZWljaGVuNRSAAAAAAAgAJGZv\n" "b2Jhci4xdGhpcyBpcyBhIG5vdGF0aW9uIGRhdGEgd2l0aCAyIGxpbmVzGhpodHRw\n" "Oi8vd3d3Lmd1Lm9yZy9wb2xpY3kvAAoJEC1yfMdoaXc0JBIAoIiLlUsvpMDOyGEc\n" "dADGKXF/Hcb+AKCJWPphZCphduxSvrzH0hgzHdeQaIh1BAAWCAAdFiEENuwqcMZC\n" "brD85btN+RyY8EnUIEwFAlrPR4cACgkQ+RyY8EnUIEyiuAEAm41LJTGUFDzhavRm\n" "jNwqUZxGGOySduW+u/X1lEfV+MYA/2lJOo75rHtD1EG+tkFVWt4Ukj0rjhR132vZ\n" "IOtrYAcG\n" "=yYwZ\n" "-----END PGP SIGNATURE-----\n"; static const char test_sig2[] = "-----BEGIN PGP MESSAGE-----\n" "\n" "owGbwMvMwCSoW1RzPCOz3IRxjXQSR0lqcYleSUWJTZOvjVdpcYmCu1+oQmaJIleH\n" "GwuDIBMDGysTSIqBi1MApi+nlGGuwDeHao53HBr+FoVGP3xX+kvuu9fCMJvl6IOf\n" "y1kvP4y+8D5a11ang0udywsA\n" "=Crq6\n" "-----END PGP MESSAGE-----\n"; /* A message with a prepended but unsigned plaintext packet. */ static const char double_plaintext_sig[] = "-----BEGIN PGP MESSAGE-----\n" "\n" "rDRiCmZvb2Jhci50eHRF4pxNVGhpcyBpcyBteSBzbmVha3kgcGxhaW50ZXh0IG1l\n" "c3NhZ2UKowGbwMvMwCSoW1RzPCOz3IRxTWISa6JebnG666MFD1wzSzJSixQ81XMV\n" "UlITUxTyixRyKxXKE0uSMxQyEosVikvyCwpSU/S4FNCArq6Ce1F+aXJGvoJvYlGF\n" "erFCTmJxiUJ5flFKMVeHGwuDIBMDGysTyA4GLk4BmO036xgWzMgzt9V85jCtfDFn\n" "UqVooWlGXHwNw/xg/fVzt9VNbtjtJ/fhUqYo0/LyCGEA\n" "=6+AK\n" "-----END PGP MESSAGE-----\n"; /* NO_OF_SIGS is the expected number of signatures. SKIP_SKIPS is * which of these signatures to check (0 based). */ static void check_result (gpgme_verify_result_t result, int no_of_sigs, int skip_sigs, unsigned int summary, const char *fpr, gpgme_error_t status, int notation) { gpgme_signature_t sig; int n; sig = result->signatures; for (n=0; sig; sig = sig->next) n++; if (n != no_of_sigs) { fprintf (stderr, "%s:%i: Unexpected number of signatures" " (got %d expected %d)\n", PGM, __LINE__, n, no_of_sigs); exit (1); } if (skip_sigs >= n) { fprintf (stderr, "%s:%i: oops SKIPP_SIGS to high\n", PGM, __LINE__); exit (1); } for (n=0, sig = result->signatures; n < skip_sigs; sig = sig->next, n++) ; if (sig->summary != summary) { fprintf (stderr, "%s:%i:sig-%d: Unexpected signature summary: " "want=0x%x have=0x%x\n", PGM, __LINE__, skip_sigs, summary, sig->summary); exit (1); } if (strcmp (sig->fpr, fpr)) { if (strlen (sig->fpr) == 16 && strlen (fpr) == 40 && !strncmp (sig->fpr, fpr + 24, 16)) ; /* okay because gnupg < 2.2.6 only shows the keyid. */ else { fprintf (stderr, "%s:%i:sig-%d: Unexpected fingerprint: %s\n", PGM, __LINE__, skip_sigs, sig->fpr); exit (1); } } if (gpgme_err_code (sig->status) != status) { fprintf (stderr, "%s:%i:sig-%d: Unexpected signature status: %s\n", PGM, __LINE__, skip_sigs, gpgme_strerror (sig->status)); exit (1); } if (notation) { static struct { const char *name; const char *value; int seen; } expected_notations[] = { { "bar", "\xc3\xb6\xc3\xa4\xc3\xbc\xc3\x9f" " das waren Umlaute und jetzt ein prozent%-Zeichen" }, { "foobar.1", "this is a notation data with 2 lines" }, { NULL, "http://www.gu.org/policy/" } }; int i; gpgme_sig_notation_t r; for (i=0; i < DIM(expected_notations); i++ ) expected_notations[i].seen = 0; for (r = sig->notations; r; r = r->next) { int any = 0; for (i=0; i < DIM(expected_notations); i++) { if ( ((r->name && expected_notations[i].name && !strcmp (r->name, expected_notations[i].name) && r->name_len == strlen (expected_notations[i].name)) || (!r->name && !expected_notations[i].name && r->name_len == 0)) && r->value && !strcmp (r->value, expected_notations[i].value) && r->value_len == strlen (expected_notations[i].value)) { expected_notations[i].seen++; any++; } } if (!any) { fprintf (stderr, "%s:%i:sig-%d: Unexpected notation data\n", PGM, __LINE__, skip_sigs); exit (1); } } for (i=0; i < DIM(expected_notations); i++ ) { if (expected_notations[i].seen != 1) { fprintf (stderr, "%s:%i:sig-%d: " "Missing or duplicate notation data\n", PGM, __LINE__, skip_sigs); exit (1); } } } if (sig->wrong_key_usage) { fprintf (stderr, "%s:%i:sig-%d: Unexpectedly wrong key usage\n", PGM, __LINE__, skip_sigs); exit (1); } if (sig->validity != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "%s:%i:sig-%d: Unexpected validity: %i\n", PGM, __LINE__, skip_sigs, sig->validity); exit (1); } if (gpgme_err_code (sig->validity_reason) != GPG_ERR_NO_ERROR) { fprintf (stderr, "%s:%i:sig-%d: Unexpected validity reason: %s\n", PGM, __LINE__, skip_sigs, gpgme_strerror (sig->validity_reason)); exit (1); } } int main (int argc, char *argv[]) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t sig, text; gpgme_verify_result_t result; const char *s; (void)argc; (void)argv; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); /* Checking a valid message. */ err = gpgme_data_new_from_mem (&text, test_text1, strlen (test_text1), 0); fail_if_err (err); err = gpgme_data_new_from_mem (&sig, test_sig1, strlen (test_sig1), 0); fail_if_err (err); err = gpgme_op_verify (ctx, sig, text, NULL); fail_if_err (err); result = gpgme_op_verify_result (ctx); check_result (result, 1, 0, 0, "A0FF4590BB6122EDEF6E3C542D727CC768697734", GPG_ERR_NO_ERROR, 1); /* Checking a manipulated message. */ gpgme_data_release (text); err = gpgme_data_new_from_mem (&text, test_text1f, strlen (test_text1f), 0); fail_if_err (err); gpgme_data_seek (sig, 0, SEEK_SET); err = gpgme_op_verify (ctx, sig, text, NULL); fail_if_err (err); result = gpgme_op_verify_result (ctx); check_result (result, 1, 0, GPGME_SIGSUM_RED, "2D727CC768697734", GPG_ERR_BAD_SIGNATURE, 0); /* Checking a valid message. Bu that one has a second signature * made by an unknown key. */ gpgme_data_release (text); gpgme_data_release (sig); err = gpgme_data_new_from_mem (&text, test_text1, strlen (test_text1), 0); fail_if_err (err); err = gpgme_data_new_from_mem (&sig, test_sig1_plus_unknown_key, strlen (test_sig1_plus_unknown_key), 0); fail_if_err (err); err = gpgme_op_verify (ctx, sig, text, NULL); fail_if_err (err); result = gpgme_op_verify_result (ctx); check_result (result, 2, 0, 0, "A0FF4590BB6122EDEF6E3C542D727CC768697734", GPG_ERR_NO_ERROR, 1); check_result (result, 2, 1, GPGME_SIGSUM_KEY_MISSING, "36EC2A70C6426EB0FCE5BB4DF91C98F049D4204C", GPG_ERR_NO_PUBKEY, 0); /* Checking a normal signature. */ gpgme_data_release (sig); gpgme_data_release (text); err = gpgme_data_new_from_mem (&sig, test_sig2, strlen (test_sig2), 0); fail_if_err (err); err = gpgme_data_new (&text); fail_if_err (err); err = gpgme_op_verify (ctx, sig, NULL, text); fail_if_err (err); result = gpgme_op_verify_result (ctx); check_result (result, 1, 0, 0, "A0FF4590BB6122EDEF6E3C542D727CC768697734", GPG_ERR_NO_ERROR, 0); /* Checking an invalid message. */ gpgme_data_release (sig); gpgme_data_release (text); err = gpgme_data_new_from_mem (&sig, double_plaintext_sig, strlen (double_plaintext_sig), 0); fail_if_err (err); err = gpgme_data_new (&text); fail_if_err (err); err = gpgme_op_verify (ctx, sig, NULL, text); if (gpgme_err_code (err) != GPG_ERR_BAD_DATA) { fprintf (stderr, "%s:%i: Double plaintext message not detected\n", PGM, __LINE__); exit (1); } /* Checking that set/get_sernder works. */ err = gpgme_set_sender (ctx, "foo@example.org"); fail_if_err (err); s = gpgme_get_sender (ctx); if (!s || strcmp (s, "foo@example.org")) { fprintf (stderr, "%s:%i: gpgme_{set,get}_sender mismatch\n", PGM, __LINE__); exit (1); } err = gpgme_set_sender (ctx, ""); fail_if_err (err); s = gpgme_get_sender (ctx); if (!s || strcmp (s, "bar@example.org")) { fprintf (stderr, "%s:%i: gpgme_{set,get}_sender mismatch\n", PGM, __LINE__); exit (1); } err = gpgme_set_sender (ctx, "Foo bar (comment) "); fail_if_err (err); s = gpgme_get_sender (ctx); if (!s || strcmp (s, "foo@example.org")) { fprintf (stderr, "%s:%i: gpgme_{set,get}_sender mismatch\n", PGM, __LINE__); exit (1); } err = gpgme_set_sender (ctx, "foo"); if (gpgme_err_code (err) != GPG_ERR_INV_VALUE) { fprintf (stderr, "%s:%i: gpgme_set_sender didn't detect bogus address\n", PGM, __LINE__); exit (1); } /* (the former address should still be there.) */ s = gpgme_get_sender (ctx); if (!s || strcmp (s, "foo@example.org")) { fprintf (stderr, "%s:%i: gpgme_{set,get}_sender mismatch\n", PGM, __LINE__); exit (1); } gpgme_data_release (sig); gpgme_data_release (text); gpgme_release (ctx); return 0; } diff --git a/tests/gpg/t-wait.c b/tests/gpg/t-wait.c index 352b9bfb..c19502cd 100644 --- a/tests/gpg/t-wait.c +++ b/tests/gpg/t-wait.c @@ -1,76 +1,76 @@ /* t-wait.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #ifdef HAVE_W32_SYSTEM #define sleep _sleep #endif #include #include "t-support.h" int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t sig, text; init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); /* Checking a message without a signature. */ err = gpgme_data_new_from_mem (&sig, "foo\n", 4, 0); fail_if_err (err); err = gpgme_data_new (&text); fail_if_err (err); err = gpgme_op_verify_start (ctx, sig, NULL, text); fail_if_err (err); while (gpgme_wait (ctx, &err, 0) == NULL && err == 0) sleep(1); if (gpgme_err_code (err) != GPG_ERR_NO_DATA) { fprintf (stderr, "%s:%d: %s: %s\n", __FILE__, __LINE__, gpgme_strsource (err), gpgme_strerror (err)); exit (1); } gpgme_data_release (sig); gpgme_data_release (text); gpgme_release (ctx); return 0; } diff --git a/tests/gpgsm/Makefile.am b/tests/gpgsm/Makefile.am index c2599204..37068400 100644 --- a/tests/gpgsm/Makefile.am +++ b/tests/gpgsm/Makefile.am @@ -1,79 +1,79 @@ # Copyright (C) 2000 Werner Koch (dd9jn) # Copyright (C) 2001 g10 Code GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser 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 +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later ## Process this file with automake to produce Makefile.in GPGSM = gpgsm GPG_AGENT = gpg-agent GNUPGHOME=$(abs_builddir) TESTS_ENVIRONMENT = GNUPGHOME=$(GNUPGHOME) LC_ALL=C GPG_AGENT_INFO= \ top_srcdir=$(top_srcdir) noinst_HEADERS = t-support.h c_tests = t-import t-keylist t-encrypt t-verify t-decrypt t-sign t-export TESTS = initial.test $(c_tests) final.test EXTRA_DIST = cert_dfn_pca01.der cert_dfn_pca15.der cert_g10code_test1.der \ $(key_id) initial.test final.test AM_CPPFLAGS = -I$(top_builddir)/src @GPG_ERROR_CFLAGS@ AM_LDFLAGS = -no-install LDADD = ../../src/libgpgme.la # We don't run t-genkey in the test suite, because it takes too long # and needs a working pinentry. noinst_PROGRAMS = $(c_tests) t-genkey cms-keylist cms-decrypt key_id = 32100C27173EF6E9C4E9A25D3D69F86D37A4F939 CLEANFILES = pubring-stamp pubring.kbx pubring.kbx~ gpgsm.conf trustlist.txt \ random_seed S.gpg-agent gpg-sample.stamp clean-local: -$(TESTS_ENVIRONMENT) $(top_srcdir)/tests/start-stop-agent --stop -rm -fR private-keys-v1.d BUILT_SOURCES = gpgsm.conf trustlist.txt pubring-stamp \ gpg-sample.stamp pubring-stamp: $(srcdir)/cert_g10code_test1.der gpg-sample.stamp $(TESTS_ENVIRONMENT) $(GPGSM) --import $(srcdir)/cert_g10code_test1.der touch pubring-stamp gpgsm.conf: echo disable-crl-checks > ./gpgsm.conf echo faked-system-time 1008241200 >> ./gpgsm.conf echo "agent-program `which $(GPG_AGENT)`|--debug-quick-random" >> ./gpgsm.conf gpg-sample.stamp: $(srcdir)/$(key_id) -$(TESTS_ENVIRONMENT) gpgconf --kill all $(MKDIR_P) ./private-keys-v1.d cp $(srcdir)/$(key_id) private-keys-v1.d/$(key_id).key echo x > ./gpg-sample.stamp trustlist.txt: echo $(key_id) > ./trustlist.txt echo >> ./trustlist.txt echo "# CN=test cert 1,OU=Aegypten Project,O=g10 Code GmbH,L=Düsseldorf,C=DE" >> ./trustlist.txt echo "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E S" >> ./trustlist.txt diff --git a/tests/gpgsm/cms-decrypt.c b/tests/gpgsm/cms-decrypt.c index 794f0131..c3426bb2 100644 --- a/tests/gpgsm/cms-decrypt.c +++ b/tests/gpgsm/cms-decrypt.c @@ -1,108 +1,109 @@ /* cms-decrypt.c - Helper to debug the decrupt operation. - Copyright (C) 2008 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2008 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "cms-decrypt" #include "t-support.h" static const char * nonnull (const char *s) { return s? s :"[none]"; } -int +int main (int argc, char **argv) { gpgme_error_t err; gpgme_ctx_t ctx; gpgme_data_t in, out; gpgme_decrypt_result_t result; gpgme_recipient_t recp; if (argc) { argc--; argv++; } if (argc != 1) { fputs ("usage: " PGM " FILE\n", stderr); exit (1); } init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); err = gpgme_data_new_from_file (&in, *argv, 1); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_decrypt (ctx, in, out); printf ("gpgme_op_decrypt: %s <%s> (%u)\n", gpgme_strerror (err), gpgme_strsource (err), err); result = gpgme_op_decrypt_result (ctx); if (!result) { fputs (PGM ": error: decryption result missing\n", stderr); exit (1); } - - printf ("unsupported_algorithm: %s\n", + + printf ("unsupported_algorithm: %s\n", nonnull (result->unsupported_algorithm)); printf ("wrong_key_usage: %u\n", result->wrong_key_usage); printf ("file_name: %s\n", nonnull (result->file_name)); for (recp = result->recipients; recp; recp = recp->next) { printf ("recipient.status: %s <%s> (%u)\n", gpgme_strerror (recp->status), gpgme_strsource (recp->status), recp->status); printf ("recipient.pkalgo: %d\n", recp->pubkey_algo); printf ("recipient.keyid : %s\n", nonnull (recp->keyid)); } if (!err) { puts ("plaintext:"); print_data (out); gpgme_data_release (out); } gpgme_data_release (in); gpgme_release (ctx); return 0; } diff --git a/tests/gpgsm/cms-keylist.c b/tests/gpgsm/cms-keylist.c index ffe17e20..1995af30 100644 --- a/tests/gpgsm/cms-keylist.c +++ b/tests/gpgsm/cms-keylist.c @@ -1,125 +1,126 @@ /* cms-keylist.c - Helper to show a key listing. - Copyright (C) 2008 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2008 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "cms-keylist" #include "t-support.h" static const char * nonnull (const char *s) { return s? s :"[none]"; } -int +int main (int argc, char **argv) { gpgme_error_t err; gpgme_ctx_t ctx; gpgme_key_t key; gpgme_keylist_result_t result; if (argc) { argc--; argv++; } if (argc > 1) { fputs ("usage: " PGM " [USERID]\n", stderr); exit (1); } init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); gpgme_set_keylist_mode (ctx, (gpgme_get_keylist_mode (ctx) | GPGME_KEYLIST_MODE_VALIDATE)); err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, 0); fail_if_err (err); - + while (!(err = gpgme_op_keylist_next (ctx, &key))) { gpgme_user_id_t uid; int nuids; - + for (nuids=0, uid=key->uids; uid; uid = uid->next) nuids++; printf ("serial : %s\n", nonnull (key->issuer_serial)); printf ("issuer : %s\n", nonnull (key->issuer_name)); printf ("chain-id: %s\n", nonnull (key->chain_id)); printf ("caps : %s%s%s%s\n", key->can_encrypt? "e":"", key->can_sign? "s":"", key->can_certify? "c":"", key->can_authenticate? "a":""); printf ("flags :%s%s%s%s%s%s\n", key->secret? " secret":"", key->revoked? " revoked":"", key->expired? " expired":"", key->disabled? " disabled":"", key->invalid? " invalid":"", key->is_qualified? " qualifid":""); for (nuids=0, uid=key->uids; uid; uid = uid->next, nuids++) { printf ("userid %d: %s\n", nuids, nonnull(uid->uid)); - printf ("valid %d: %s\n", nuids, + printf ("valid %d: %s\n", nuids, uid->validity == GPGME_VALIDITY_UNKNOWN? "unknown": uid->validity == GPGME_VALIDITY_UNDEFINED? "undefined": uid->validity == GPGME_VALIDITY_NEVER? "never": uid->validity == GPGME_VALIDITY_MARGINAL? "marginal": uid->validity == GPGME_VALIDITY_FULL? "full": uid->validity == GPGME_VALIDITY_ULTIMATE? "ultimate": "[?]"); } putchar ('\n'); gpgme_key_unref (key); } if (gpgme_err_code (err) != GPG_ERR_EOF) fail_if_err (err); err = gpgme_op_keylist_end (ctx); fail_if_err (err); result = gpgme_op_keylist_result (ctx); if (result->truncated) { fprintf (stderr, PGM ": key listing unexpectedly truncated\n"); exit (1); } gpgme_release (ctx); return 0; } diff --git a/tests/gpgsm/t-decrypt.c b/tests/gpgsm/t-decrypt.c index 81803af4..84e33c58 100644 --- a/tests/gpgsm/t-decrypt.c +++ b/tests/gpgsm/t-decrypt.c @@ -1,96 +1,96 @@ /* t-encrypt.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" static const char test_text1[] = "Hallo Leute!\n"; static const char test_cip1[] = "-----BEGIN CMS OBJECT-----\n" "MIAGCSqGSIb3DQEHA6CAMIACAQAxggEJMIIBBQIBADBwMGsxCzAJBgNVBAYTAkRF\n" "MRMwEQYDVQQHFApE/HNzZWxkb3JmMRYwFAYDVQQKEw1nMTAgQ29kZSBHbWJIMRkw\n" "FwYDVQQLExBBZWd5cHRlbiBQcm9qZWN0MRQwEgYDVQQDEwt0ZXN0IGNlcnQgMQIB\n" "ADALBgkqhkiG9w0BAQEEgYBOFcOfUtAav+XjKGM1RJtF+8JLkbnu46S3T3709Iok\n" "u+Z9dwpOyfHwxXOmjzkSKQSBBxxi6ar+sKjU/KfPIvaMpARwT+NfIVSCZRWIJ27z\n" "wbSrav/kcRRDDA0wXV7dHVmSLPUJNCpiFMNZbkYtI+ai15g0PVeDw+szYd9zdsjJ\n" "2zCABgkqhkiG9w0BBwEwFAYIKoZIhvcNAwcECA8gPQY2NtJToIAECAeoY3MIcz9h\n" "BAiiytWtOSmqnwAA\n" "-----END CMS OBJECT-----\n"; int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; size_t len; char *test_text2; gpgme_decrypt_result_t result; init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); err = gpgme_data_new_from_mem (&in, test_cip1, strlen (test_cip1), 0); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_decrypt (ctx, in, out); fail_if_err (err); result = gpgme_op_decrypt_result (ctx); if (result->unsupported_algorithm) { fprintf (stderr, "%s:%i: unsupported algorithm: %s\n", __FILE__, __LINE__, result->unsupported_algorithm); exit (1); } test_text2 = gpgme_data_release_and_get_mem (out, &len); test_text2[len] = '\0'; if (strcmp (test_text1, test_text2)) { fprintf (stderr, "%s:%i: data mismatch: expected: \n\"%s\"\n" "got:\n\"%s\"", __FILE__, __LINE__, test_text1, test_text2); exit (1); } free (test_text2); gpgme_data_release (in); gpgme_release (ctx); return 0; } diff --git a/tests/gpgsm/t-encrypt.c b/tests/gpgsm/t-encrypt.c index 50c7a339..5e621eea 100644 --- a/tests/gpgsm/t-encrypt.c +++ b/tests/gpgsm/t-encrypt.c @@ -1,78 +1,78 @@ /* t-encrypt.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_key_t key[] = { NULL, NULL }; gpgme_encrypt_result_t result; init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); gpgme_set_armor (ctx, 1); err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0); fail_if_err (err); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_get_key (ctx, "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E", &key[0], 0); fail_if_err (err); err = gpgme_op_encrypt (ctx, key, 0, in, out); fail_if_err (err); result = gpgme_op_encrypt_result (ctx); if (result->invalid_recipients) { fprintf (stderr, "Invalid recipient encountered: %s\n", result->invalid_recipients->fpr); exit (1); } print_data (out); gpgme_key_unref (key[0]); gpgme_data_release (in); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpgsm/t-export.c b/tests/gpgsm/t-export.c index 120df6b5..352a3f1e 100644 --- a/tests/gpgsm/t-export.c +++ b/tests/gpgsm/t-export.c @@ -1,86 +1,86 @@ /* t-export.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t out; const char *pattern1[] = { "DFN Top Level Certification Authority", NULL }; const char *pattern2[] = { "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E", "DFN Server Certification Authority", NULL }; init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); gpgme_set_armor (ctx, 1); /* Check exporting of one certificate. */ err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_export_ext (ctx, pattern1, 0, out); fail_if_err (err); fflush (NULL); fputs ("Begin Result:\n", stdout); print_data (out); fputs ("End Result.\n", stdout); gpgme_data_release (out); /* Check exporting of 2 certificates. */ err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_export_ext (ctx, pattern2, 0, out); fail_if_err (err); fflush (NULL); fputs ("Begin Result:\n", stdout); print_data (out); fputs ("End Result.\n", stdout); gpgme_data_release (out); gpgme_release (ctx); return 0; } diff --git a/tests/gpgsm/t-genkey.c b/tests/gpgsm/t-genkey.c index 120ed688..ac7ef798 100644 --- a/tests/gpgsm/t-genkey.c +++ b/tests/gpgsm/t-genkey.c @@ -1,130 +1,130 @@ /* t-genkey.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" /* True if progress function printed something on the screen. */ static int progress_called; static void progress (void *self, const char *what, int type, int current, int total) { (void)self; if (!strcmp (what, "primegen") && !current && !total && (type == '.' || type == '+' || type == '!' || type == '^' || type == '<' || type == '>')) { printf ("%c", type); fflush (stdout); progress_called = 1; } else { fprintf (stderr, "unknown progress `%s' %d %d %d\n", what, type, current, total); exit (1); } } int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; const char *parms = "\n" "Key-Type: RSA\n" "Key-Length: 1024\n" "Name-DN: C=de,O=g10 code,OU=Testlab,CN=Joe 2 Tester\n" "Name-Email: joe@foo.bar\n" "\n"; gpgme_genkey_result_t result; gpgme_data_t certreq; init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_data_new (&certreq); fail_if_err (err); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); gpgme_set_armor (ctx, 1); gpgme_set_progress_cb (ctx, progress, NULL); err = gpgme_op_genkey (ctx, parms, certreq, NULL); fail_if_err (err); result = gpgme_op_genkey_result (ctx); if (!result) { fprintf (stderr, "%s:%d: gpgme_op_genkey_result returns NULL\n", __FILE__, __LINE__); exit (1); } if (progress_called) printf ("\n"); printf ("Generated key: %s (%s)\n", result->fpr ? result->fpr : "none", result->primary ? (result->sub ? "primary, sub" : "primary") : (result->sub ? "sub" : "none")); if (result->fpr) { fprintf (stderr, "%s:%d: generated key has (unexpectedly) a fingerprint\n", __FILE__, __LINE__); exit (1); } if (!result->primary) { fprintf (stderr, "%s:%d: primary key was not generated\n", __FILE__, __LINE__); exit (1); } if (result->sub) { fprintf (stderr, "%s:%d: sub key was (unexpectedly) generated\n", __FILE__, __LINE__); exit (1); } gpgme_release (ctx); print_data (certreq); gpgme_data_release (certreq); return 0; } diff --git a/tests/gpgsm/t-import.c b/tests/gpgsm/t-import.c index a8dfccaa..0766a280 100644 --- a/tests/gpgsm/t-import.c +++ b/tests/gpgsm/t-import.c @@ -1,179 +1,179 @@ /* t-import.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #include "t-support.h" void check_result (gpgme_import_result_t result, const char *fpr, int total, int total_stat) { (void)fpr; if (result->considered != total) { fprintf (stderr, "Unexpected number of considered keys %i\n", result->considered); exit (1); } if (result->no_user_id != 0) { fprintf (stderr, "Unexpected number of user ids %i\n", result->no_user_id); exit (1); } if (result->imported != 0 && result->imported != 1) { fprintf (stderr, "Unexpected number of imported keys %i\n", result->imported); exit (1); } if (result->imported_rsa != 0) { fprintf (stderr, "Unexpected number of imported RSA keys %i\n", result->imported_rsa); exit (1); } if ((result->imported == 0 && result->unchanged != total) || (result->imported == 1 && result->unchanged != total - 1)) { fprintf (stderr, "Unexpected number of unchanged keys %i\n", result->unchanged); exit (1); } if (result->new_user_ids != 0) { fprintf (stderr, "Unexpected number of new user IDs %i\n", result->new_user_ids); exit (1); } if (result->new_sub_keys != 0) { fprintf (stderr, "Unexpected number of new sub keys %i\n", result->new_sub_keys); exit (1); } if (result->new_signatures != 0) { fprintf (stderr, "Unexpected number of new signatures %i\n", result->new_signatures); exit (1); } if (result->new_revocations != 0) { fprintf (stderr, "Unexpected number of new revocations %i\n", result->new_revocations); exit (1); } if (result->secret_read != 0) { fprintf (stderr, "Unexpected number of secret keys read %i\n", result->secret_read); exit (1); } if (result->secret_imported != 0) { fprintf (stderr, "Unexpected number of secret keys imported %i\n", result->secret_imported); exit (1); } if (result->secret_unchanged != 0) { fprintf (stderr, "Unexpected number of secret keys unchanged %i\n", result->secret_unchanged); exit (1); } if (result->not_imported != 0) { fprintf (stderr, "Unexpected number of secret keys not imported %i\n", result->not_imported); exit (1); } { int n; gpgme_import_status_t r; for (n=0, r=result->imports; r; r=r->next) n++; if (n != total_stat) { fprintf (stderr, "Unexpected number of status reports\n"); exit (1); } } } int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in; gpgme_import_result_t result; char *cert_1 = make_filename ("cert_dfn_pca01.der"); char *cert_2 = make_filename ("cert_dfn_pca15.der"); init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); err = gpgme_data_new_from_file (&in, cert_1, 1); free (cert_1); fail_if_err (err); err = gpgme_op_import (ctx, in); fail_if_err (err); result = gpgme_op_import_result (ctx); check_result (result, "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD", 1, 1); gpgme_data_release (in); err = gpgme_data_new_from_file (&in, cert_2, 1); free (cert_2); fail_if_err (err); err = gpgme_op_import (ctx, in); fail_if_err (err); result = gpgme_op_import_result (ctx); check_result (result, "2C8F3C356AB761CB3674835B792CDA52937F9285", 1, 2); gpgme_data_release (in); gpgme_release (ctx); return 0; } diff --git a/tests/gpgsm/t-keylist.c b/tests/gpgsm/t-keylist.c index 314109c8..1523c7b5 100644 --- a/tests/gpgsm/t-keylist.c +++ b/tests/gpgsm/t-keylist.c @@ -1,390 +1,390 @@ /* t-keylist.c - regression test - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" struct { const char *fpr; int secret; long timestamp; long expires; const char *issuer_serial; const char *issuer_name; const char *chain_id; const char *uid; const char *email; gpgme_validity_t validity; unsigned int key_length; } keys[] = { { "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E", 1, 1007372198, 1038908198, "00", "CN=test cert 1,OU=Aegypten Project,O=g10 Code GmbH,L=D\xc3\xbcsseldorf,C=DE", "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E", "CN=test cert 1,OU=Aegypten Project,O=g10 Code GmbH,L=D\xc3\xbcsseldorf,C=DE", NULL, GPGME_VALIDITY_ULTIMATE, 1024 }, { "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD", 0, 909684190, 1009821790, "01", "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465," "CN=DFN Top Level Certification Authority,OU=DFN-PCA," "O=Deutsches Forschungsnetz,C=DE", "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD", "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465," "CN=DFN Top Level Certification Authority,OU=DFN-PCA," "O=Deutsches Forschungsnetz,C=DE", "", GPGME_VALIDITY_NEVER, 2048 }, { "2C8F3C356AB761CB3674835B792CDA52937F9285", 0, 973183644, 1009735200, "15", "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465," "CN=DFN Top Level Certification Authority,OU=DFN-PCA," "O=Deutsches Forschungsnetz,C=DE", "DFA56FB5FC41E3A8921F77AD1622EEFD9152A5AD", "1.2.840.113549.1.9.1=#63657274696679407063612E64666E2E6465," "CN=DFN Server Certification Authority,OU=DFN-PCA," "O=Deutsches Forschungsnetz,C=DE", "", GPGME_VALIDITY_UNKNOWN, 2048 }, { NULL } }; int main (void) { gpgme_error_t err; gpgme_ctx_t ctx; gpgme_key_t key; gpgme_keylist_result_t result; int i = 0; init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); err = gpgme_op_keylist_start (ctx, NULL, 0); fail_if_err (err); while (!(err = gpgme_op_keylist_next (ctx, &key))) { if (!keys[i].fpr) { fprintf (stderr, "More keys returned than expected\n"); exit (1); } if (strcmp (key->subkeys->fpr, keys[i].fpr)) { fprintf (stderr, "Warning: Skipping unknown key %s\n", key->subkeys->fpr); gpgme_key_unref (key); continue; } else printf ("Checking key %s\n", key->subkeys->fpr); /* Global key flags. */ if (key->revoked) { fprintf (stderr, "Key unexpectedly revoked\n"); exit (1); } if (key->expired) { fprintf (stderr, "Key unexpectedly expired\n"); exit (1); } if (key->disabled) { fprintf (stderr, "Key unexpectedly disabled\n"); exit (1); } if (key->invalid) { fprintf (stderr, "Key unexpectedly invalid\n"); exit (1); } if (key->can_encrypt != keys[i].secret) { fprintf (stderr, "Key unexpectedly%s usable for encryption\n", key->can_encrypt ? "" : " not"); exit (1); } if (key->can_sign != keys[i].secret) { fprintf (stderr, "Key unexpectedly%s usable for signing\n", key->can_sign ? "" : " not"); exit (1); } if (!key->can_certify) { fprintf (stderr, "Key unexpectedly unusable for certifications\n"); exit (1); } if (key->secret != keys[i].secret) { fprintf (stderr, "Key unexpectedly%s secret\n", key->secret ? "" : " not"); exit (1); } if (key->protocol != GPGME_PROTOCOL_CMS) { fprintf (stderr, "Key has unexpected protocol: %s\n", gpgme_get_protocol_name (key->protocol)); exit (1); } if (!key->issuer_serial) { fprintf (stderr, "Key unexpectedly misses issuer serial\n"); exit (1); } if (strcmp (key->issuer_serial, keys[i].issuer_serial)) { fprintf (stderr, "Key has unexpected issuer serial: %s\n", key->issuer_serial); exit (1); } if (!key->issuer_name) { fprintf (stderr, "Key unexpectedly misses issuer name\n"); exit (1); } if (strcmp (key->issuer_name, keys[i].issuer_name)) { fprintf (stderr, "Key has unexpected issuer name: %s\n", key->issuer_name); exit (1); } if (key->chain_id && !keys[i].chain_id) { fprintf (stderr, "Key unexpectedly carries chain ID: %s\n", key->chain_id); exit (1); } if (!key->chain_id && keys[i].chain_id) { fprintf (stderr, "Key unexpectedly carries no chain ID\n"); exit (1); } if (key->chain_id && strcmp (key->chain_id, keys[i].chain_id)) { fprintf (stderr, "Key carries unexpected chain ID: %s\n", key->chain_id); exit (1); } if (key->owner_trust != GPGME_VALIDITY_UNKNOWN) { fprintf (stderr, "Key has unexpected owner trust: %i\n", key->owner_trust); exit (1); } if (!key->subkeys || key->subkeys->next) { fprintf (stderr, "Key has unexpected number of subkeys\n"); exit (1); } /* Primary key. */ if (key->subkeys->revoked) { fprintf (stderr, "Primary key unexpectedly revoked\n"); exit (1); } if (key->subkeys->expired) { fprintf (stderr, "Primary key unexpectedly expired\n"); exit (1); } if (key->subkeys->disabled) { fprintf (stderr, "Primary key unexpectedly disabled\n"); exit (1); } if (key->subkeys->invalid) { fprintf (stderr, "Primary key unexpectedly invalid\n"); exit (1); } if (key->subkeys->can_encrypt != keys[i].secret) { fprintf (stderr, "Key unexpectedly%s usable for encryption\n", key->subkeys->can_encrypt ? "" : " not"); exit (1); } if (key->subkeys->can_sign != keys[i].secret) { fprintf (stderr, "Key unexpectedly%s usable for signing\n", key->subkeys->can_sign ? "" : " not"); exit (1); } if (!key->subkeys->can_certify) { fprintf (stderr, "Primary key unexpectedly unusable for certifications\n"); exit (1); } if (key->subkeys->secret != keys[i].secret) { fprintf (stderr, "Primary Key unexpectedly%s secret\n", key->secret ? "" : " not"); exit (1); } if (key->subkeys->pubkey_algo != GPGME_PK_RSA) { fprintf (stderr, "Primary key has unexpected public key algo: %s\n", gpgme_pubkey_algo_name (key->subkeys->pubkey_algo)); exit (1); } if (key->subkeys->length != keys[i].key_length) { fprintf (stderr, "Primary key has unexpected length: %i\n", key->subkeys->length); exit (1); } if (strcmp (key->subkeys->keyid, &keys[i].fpr[40 - 16])) { fprintf (stderr, "Primary key has unexpected key ID: %s\n", key->subkeys->keyid); exit (1); } if (strcmp (key->subkeys->fpr, keys[i].fpr)) { fprintf (stderr, "Primary key has unexpected fingerprint: %s\n", key->subkeys->fpr); exit (1); } if (key->subkeys->timestamp != keys[i].timestamp) { fprintf (stderr, "Primary key unexpected timestamp: %lu\n", key->subkeys->timestamp); exit (1); } if (key->subkeys->expires != keys[i].expires) { fprintf (stderr, "Primary key unexpectedly expires: %lu\n", key->subkeys->expires); exit (1); } /* Be tolerant against a missing email (ie, older gpgsm versions). */ if (!key->uids || (key->uids->next && !keys[i].email)) { fprintf (stderr, "Key has unexpected number of user IDs\n"); exit (1); } if (key->uids->revoked) { fprintf (stderr, "User ID unexpectedly revoked\n"); exit (1); } if (key->uids->invalid) { fprintf (stderr, "User ID unexpectedly invalid\n"); exit (1); } if (key->uids->validity != keys[i].validity) { fprintf (stderr, "User ID unexpectedly validity: %i\n", key->uids->validity); exit (1); } if (key->uids->signatures) { fprintf (stderr, "User ID unexpectedly signed\n"); exit (1); } if (!key->uids->name || key->uids->name[0]) { fprintf (stderr, "Unexpected name in user ID: %s\n", key->uids->name); exit (1); } if (!key->uids->comment || key->uids->comment[0]) { fprintf (stderr, "Unexpected comment in user ID: %s\n", key->uids->comment); exit (1); } if (!key->uids->email || key->uids->email[0]) { fprintf (stderr, "Unexpected email in user ID: %s\n", key->uids->email); exit (1); } if (!key->uids->uid || strcmp (key->uids->uid, keys[i].uid)) { fprintf (stderr, "Unexpected uid in user ID: %s\n", key->uids->uid); exit (1); } if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email)) { fprintf (stderr, "Unexpected email in user ID: %s\n", key->uids->next->uid); exit (1); } if (key->uids->next && strcmp (key->uids->next->uid, keys[i].email)) { fprintf (stderr, "Unexpected email in user ID: %s\n", key->uids->next->uid); exit (1); } gpgme_key_unref (key); i++; } if (gpgme_err_code (err) != GPG_ERR_EOF) fail_if_err (err); err = gpgme_op_keylist_end (ctx); fail_if_err (err); result = gpgme_op_keylist_result (ctx); if (result->truncated) { fprintf (stderr, "Key listing unexpectedly truncated\n"); exit (1); } if (keys[i].fpr) { fprintf (stderr, "Less keys returned than expected\n"); exit (1); } gpgme_release (ctx); return 0; } diff --git a/tests/gpgsm/t-sign.c b/tests/gpgsm/t-sign.c index d4e143dd..1dc7c0c1 100644 --- a/tests/gpgsm/t-sign.c +++ b/tests/gpgsm/t-sign.c @@ -1,127 +1,127 @@ /* t-sign.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" static void check_result (gpgme_sign_result_t result, gpgme_sig_mode_t type) { if (result->invalid_signers) { fprintf (stderr, "Invalid signer found: %s\n", result->invalid_signers->fpr); exit (1); } if (!result->signatures || result->signatures->next) { fprintf (stderr, "Unexpected number of signatures created\n"); exit (1); } if (result->signatures->type != type) { fprintf (stderr, "Wrong type of signature created\n"); exit (1); } if (result->signatures->pubkey_algo != GPGME_PK_RSA) { fprintf (stderr, "Wrong pubkey algorithm reported: %i\n", result->signatures->pubkey_algo); exit (1); } if (result->signatures->hash_algo != GPGME_MD_SHA1) { fprintf (stderr, "Wrong hash algorithm reported: %i\n", result->signatures->hash_algo); exit (1); } if (result->signatures->sig_class != 0) { fprintf (stderr, "Wrong signature class reported: %u\n", result->signatures->sig_class); exit (1); } if (strcmp ("3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E", result->signatures->fpr)) { fprintf (stderr, "Wrong fingerprint reported: %s\n", result->signatures->fpr); exit (1); } } int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t in, out; gpgme_sign_result_t result; init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); gpgme_set_textmode (ctx, 1); gpgme_set_armor (ctx, 1); err = gpgme_data_new_from_mem (&in, "Hallo Leute!\n", 13, 0); fail_if_err (err); /* First a normal signature. */ err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_NORMAL); fail_if_err (err); result = gpgme_op_sign_result (ctx); check_result (result, GPGME_SIG_MODE_NORMAL); print_data (out); gpgme_data_release (out); /* Now a detached signature. */ gpgme_data_seek (in, 0, SEEK_SET); err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, GPGME_SIG_MODE_DETACH); fail_if_err (err); result = gpgme_op_sign_result (ctx); check_result (result, GPGME_SIG_MODE_DETACH); print_data (out); gpgme_data_release (out); gpgme_data_release (in); gpgme_release (ctx); return 0; } diff --git a/tests/gpgsm/t-support.h b/tests/gpgsm/t-support.h index c3074db1..a0066599 100644 --- a/tests/gpgsm/t-support.h +++ b/tests/gpgsm/t-support.h @@ -1,119 +1,119 @@ /* t-support.h - Helper routines for regression tests. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #include #include #include #include #include #define fail_if_err(err) \ do \ { \ if (err) \ { \ fprintf (stderr, "%s:%d: %s: %s (%d.%d)\n", \ __FILE__, __LINE__, gpgme_strsource (err), \ gpgme_strerror (err), \ gpgme_err_source (err), gpgme_err_code (err)); \ exit (1); \ } \ } \ while (0) void print_data (gpgme_data_t dh) { #define BUF_SIZE 512 char buf[BUF_SIZE + 1]; int ret; ret = gpgme_data_seek (dh, 0, SEEK_SET); if (ret) fail_if_err (gpgme_error_from_errno (errno)); while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0) fwrite (buf, ret, 1, stdout); if (ret < 0) fail_if_err (gpgme_error_from_errno (errno)); } gpgme_error_t passphrase_cb (void *opaque, const char *uid_hint, const char *passphrase_info, int last_was_bad, int fd) { int res; char pass[] = "abc\n"; int passlen = strlen (pass); int off = 0; (void)opaque; (void)uid_hint; (void)passphrase_info; (void)last_was_bad; do { res = gpgme_io_write (fd, &pass[off], passlen - off); if (res > 0) off += res; } while (res > 0 && off != passlen); return off == passlen ? 0 : gpgme_error_from_errno (errno); } char * make_filename (const char *fname) { const char *srcdir = getenv ("srcdir"); char *buf; if (!srcdir) srcdir = "."; buf = malloc (strlen(srcdir) + strlen(fname) + 2); if (!buf) exit (8); strcpy (buf, srcdir); strcat (buf, "/"); strcat (buf, fname); return buf; } void init_gpgme (gpgme_protocol_t proto) { gpgme_error_t err; gpgme_check_version (NULL); #ifndef HAVE_W32_SYSTEM setlocale (LC_ALL, ""); gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); #endif err = gpgme_engine_check_version (proto); fail_if_err (err); } diff --git a/tests/gpgsm/t-verify.c b/tests/gpgsm/t-verify.c index 004a125e..3e0c6550 100644 --- a/tests/gpgsm/t-verify.c +++ b/tests/gpgsm/t-verify.c @@ -1,184 +1,184 @@ /* t-verify.c - Regression test. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include "t-support.h" static int got_errors; static const char test_text1[] = "Hallo Leute!\n"; static const char test_text1f[]= "Hallo Leute?\n"; static const char test_sig1[] = "-----BEGIN CMS OBJECT-----\n" "MIAGCSqGSIb3DQEHAqCAMIACAQExCzAJBgUrDgMCGgUAMIAGCSqGSIb3DQEHAQAA\n" "MYIBOTCCATUCAQEwcDBrMQswCQYDVQQGEwJERTETMBEGA1UEBxQKRPxzc2VsZG9y\n" "ZjEWMBQGA1UEChMNZzEwIENvZGUgR21iSDEZMBcGA1UECxMQQWVneXB0ZW4gUHJv\n" "amVjdDEUMBIGA1UEAxMLdGVzdCBjZXJ0IDECAQAwBwYFKw4DAhqgJTAjBgkqhkiG\n" "9w0BCQQxFgQU7FC/ibH3lC9GE24RJJxa8zqP7wEwCwYJKoZIhvcNAQEBBIGAA3oC\n" "DUmKERmD1eoJYFw38y/qnncS/6ZPjWINDIphZeK8mzAANpvpIaRPf3sNBznb89QF\n" "mRgCXIWcjlHT0DTRLBf192Ve22IyKH00L52CqFsSN3a2sajqRUlXH8RY2D+Al71e\n" "MYdRclgjObCcoilA8fZ13VR4DiMJVFCxJL4qVWI=\n" "-----END CMS OBJECT-----\n"; static void check_result (gpgme_verify_result_t result, int summary, const char *fpr, gpgme_error_t status, gpgme_validity_t validity) { gpgme_signature_t sig; sig = result->signatures; if (!sig || sig->next) { fprintf (stderr, "%s:%i: Unexpected number of signatures\n", __FILE__, __LINE__); got_errors = 1; if (!sig) return; } if (sig->summary != summary) { fprintf (stderr, "%s:%i: Unexpected signature summary: " "want=0x%x have=0x%x\n", __FILE__, __LINE__, summary, sig->summary); got_errors = 1; } if (sig->fpr && strcmp (sig->fpr, fpr)) { fprintf (stderr, "%s:%i: Unexpected fingerprint: %s\n", __FILE__, __LINE__, sig->fpr); got_errors = 1; } if (gpgme_err_code (sig->status) != status) { fprintf (stderr, "%s:%i: Unexpected signature status: %s\n", __FILE__, __LINE__, gpgme_strerror (sig->status)); got_errors = 1; } if (sig->notations) { fprintf (stderr, "%s:%i: Unexpected notation data\n", __FILE__, __LINE__); got_errors = 1; } if (sig->wrong_key_usage) { fprintf (stderr, "%s:%i: Unexpectedly wrong key usage\n", __FILE__, __LINE__); got_errors = 1; } if (sig->validity != validity) { fprintf (stderr, "%s:%i: Unexpected validity: %i\n", __FILE__, __LINE__, sig->validity); got_errors = 1; } if (gpgme_err_code (sig->validity_reason) != GPG_ERR_NO_ERROR) { fprintf (stderr, "%s:%i: Unexpected validity reason: %s\n", __FILE__, __LINE__, gpgme_strerror (sig->validity_reason)); got_errors = 1; } } static void show_auditlog (gpgme_ctx_t ctx) { gpgme_error_t err; gpgme_data_t data; err = gpgme_data_new (&data); fail_if_err (err); err = gpgme_op_getauditlog (ctx, data, 0); if (err) { fprintf (stderr, "%s:%i: Can't get audit log: %s\n", __FILE__, __LINE__, gpgme_strerror (err)); if (gpgme_err_code (err) != GPG_ERR_ASS_UNKNOWN_CMD) got_errors = 1; } print_data (data); gpgme_data_release (data); } int main (void) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t sig, text; gpgme_verify_result_t result; init_gpgme (GPGME_PROTOCOL_CMS); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_CMS); /* Checking a valid message. */ err = gpgme_data_new_from_mem (&text, test_text1, strlen (test_text1), 0); fail_if_err (err); err = gpgme_data_new_from_mem (&sig, test_sig1, strlen (test_sig1), 0); fail_if_err (err); err = gpgme_op_verify (ctx, sig, text, NULL); fail_if_err (err); result = gpgme_op_verify_result (ctx); check_result (result, GPGME_SIGSUM_VALID | GPGME_SIGSUM_GREEN, "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E", GPG_ERR_NO_ERROR, GPGME_VALIDITY_FULL); show_auditlog (ctx); /* Checking a manipulated message. */ gpgme_data_release (text); err = gpgme_data_new_from_mem (&text, test_text1f, strlen (test_text1f), 0); fail_if_err (err); gpgme_data_seek (sig, 0, SEEK_SET); err = gpgme_op_verify (ctx, sig, text, NULL); fail_if_err (err); result = gpgme_op_verify_result (ctx); check_result (result, GPGME_SIGSUM_RED, "3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E", GPG_ERR_BAD_SIGNATURE, GPGME_VALIDITY_UNKNOWN); show_auditlog (ctx); gpgme_data_release (text); gpgme_data_release (sig); gpgme_release (ctx); return got_errors? 1 : 0; } diff --git a/tests/json/Makefile.am b/tests/json/Makefile.am index 61c3e241..f4db8400 100644 --- a/tests/json/Makefile.am +++ b/tests/json/Makefile.am @@ -1,109 +1,109 @@ # Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik # Software engineering by Intevation GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser 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 +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later ## Process this file with automake to produce Makefile.in GPGME_JSON=$(abs_builddir)/../../src/gpgme-json GPG = gpg GNUPGHOME=$(abs_builddir) TESTS_ENVIRONMENT = GNUPGHOME=$(GNUPGHOME) LC_ALL=C GPG_AGENT_INFO= \ top_srcdir=$(top_srcdir) gpgme_json=$(GPGME_JSON) c_tests = t-json TESTS = initial.test $(c_tests) final.test CLEANFILES = secring.gpg pubring.gpg pubring.kbx trustdb.gpg dirmngr.conf \ gpg-agent.conf pubring.kbx~ S.gpg-agent gpg.conf pubring.gpg~ \ random_seed S.gpg-agent .gpg-v21-migrated pubring-stamp \ gpg-sample.stamp tofu.db *.conf.gpgconf.bak private_keys = \ $(top_srcdir)/tests/gpg/13CD0F3BDF24BE53FE192D62F18737256FF6E4FD \ $(top_srcdir)/tests/gpg/76F7E2B35832976B50A27A282D9B87E44577EB66 \ $(top_srcdir)/tests/gpg/A0747D5F9425E6664F4FFBEED20FBCA79FDED2BD \ $(top_srcdir)/tests/gpg/13CBE3758AFE42B5E5E2AE4CED27AFA455E3F87F \ $(top_srcdir)/tests/gpg/7A030357C0F253A5BBCD282FFC4E521B37558F5C EXTRA_DIST = initial.test final.test \ t-chunking.in.json t-chunking.out.json \ t-config.in.json t-config-opt.in.json \ t-config-opt.out.json t-config.out.json \ t-createkey.in.json t-createkey.out.json \ t-decrypt.in.json t-decrypt.out.json \ t-decrypt-verify.in.json t-decrypt-verify.out.json \ t-delete.in.json t-delete.out.json \ t-encrypt.in.json t-encrypt.out.json \ t-encrypt-sign.in.json t-encrypt-sign.out.json \ t-export.in.json t-export.out.json \ t-export-secret-info.in.json t-export-secret-info.out.json \ t-import.in.json t-import.out.json \ t-keylist.in.json t-keylist.out.json \ t-keylist-secret.in.json t-keylist-secret.out.json \ t-sign.in.json t-sign.out.json \ t-sig-notations.in.json t-sig-notations.out.json \ t-verify.in.json t-verify.out.json \ t-version.in.json t-version.out.json BUILT_SOURCES = gpg.conf gpg-agent.conf pubring-stamp \ gpg-sample.stamp t_json_SOURCES = t-json.c AM_LDFLAGS = -no-install LDADD = ../../src/libgpgme.la t_json_LDADD = ../../src/cJSON.o -lm ../../src/libgpgme.la @GPG_ERROR_LIBS@ AM_CPPFLAGS = -I$(top_builddir)/src @GPG_ERROR_CFLAGS@ noinst_PROGRAMS = $(c_tests) clean-local: -$(TESTS_ENVIRONMENT) $(top_srcdir)/tests/start-stop-agent --stop -rm -fR private-keys-v1.d -rm -fR openpgp-revocs.d gpg-sample.stamp: $(private_keys) -$(TESTS_ENVIRONMENT) gpgconf --kill all $(MKDIR_P) ./private-keys-v1.d for k in $(private_keys); do \ cp $$k private-keys-v1.d/$$(basename $$k).key; \ done echo x > ./gpg-sample.stamp pubring-stamp: $(top_srcdir)/tests/gpg/pubdemo.asc gpg-sample.stamp $(TESTS_ENVIRONMENT) $(GPG) --batch --no-permission-warning \ --import $(top_srcdir)/tests/gpg/pubdemo.asc -$(TESTS_ENVIRONMENT) $(GPG) --batch --no-permission-warning \ --import $(top_srcdir)/tests/gpg/secdemo.asc echo x > ./pubring-stamp gpg.conf: # This is required for t-sig-notations. echo no-force-v3-sigs > ./gpg.conf gpg-agent.conf: # This is required for gpg2, which does not support command fd for the # passphrase. disable-scdaemon is required so that we don't try using # a key from a smartcard reader (error might be: Unusable secret key) echo pinentry-program $(abs_srcdir)/../gpg/pinentry > ./gpg-agent.conf echo disable-scdaemon >> ./gpg-agent.conf # end-of-file diff --git a/tests/json/t-json.c b/tests/json/t-json.c index 77750ad0..f574f57e 100644 --- a/tests/json/t-json.c +++ b/tests/json/t-json.c @@ -1,499 +1,499 @@ /* t-json.c - Regression test. - Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik - Software engineering by Intevation GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2018 Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #include #include #include #include "../gpg/t-support.h" #include "../../src/cJSON.h" /* Register tests here */ static const char*tests[] = { "t-config", "t-version", "t-keylist", "t-keylist-secret", "t-decrypt", "t-config-opt", "t-encrypt", "t-encrypt-sign", "t-sign", "t-verify", "t-decrypt-verify", "t-export", "t-createkey", "t-export-secret-info", "t-chunking", "t-sig-notations", /* For these two the order is important * as t-import imports the deleted key from t-delete */ "t-delete", "t-import", NULL }; static int verbose = 0; /* Read the next number in the version string STR and return it in *NUMBER. Return a pointer to the tail of STR after parsing, or *NULL if the version string was invalid. */ static const char * parse_version_number (const char *str, int *number) { #define MAXVAL ((INT_MAX - 10) / 10) int val = 0; /* Leading zeros are not allowed. */ if (*str == '0' && isdigit(str[1])) return NULL; while (isdigit (*str) && val <= MAXVAL) { val *= 10; val += *(str++) - '0'; } *number = val; return val > MAXVAL ? NULL : str; } /* Parse the version string STR in the format MAJOR.MINOR.MICRO (for example, 9.3.2) and return the components in MAJOR, MINOR and MICRO as integers. The function returns the tail of the string that follows the version number. This might be the empty string if there is nothing following the version number, or a patchlevel. The function returns NULL if the version string is not valid. */ static const char * parse_version_string (const char *str, int *major, int *minor, int *micro) { str = parse_version_number (str, major); if (!str || *str != '.') return NULL; str++; str = parse_version_number (str, minor); if (!str || *str != '.') return NULL; str++; str = parse_version_number (str, micro); if (!str) return NULL; /* A patchlevel might follow. */ return str; } /* Return true if MY_VERSION is at least REQ_VERSION, and false otherwise. */ static int compare_versions (const char *my_version, const char *rq_version) { int my_major, my_minor, my_micro; int rq_major, rq_minor, rq_micro; const char *my_plvl, *rq_plvl; if (!rq_version) return 1; if (!my_version) return 0; my_plvl = parse_version_string (my_version, &my_major, &my_minor, &my_micro); if (!my_plvl) return 0; rq_plvl = parse_version_string (rq_version, &rq_major, &rq_minor, &rq_micro); if (!rq_plvl) return 0; if (my_major > rq_major || (my_major == rq_major && my_minor > rq_minor) || (my_major == rq_major && my_minor == rq_minor && my_micro > rq_micro) || (my_major == rq_major && my_minor == rq_minor && my_micro == rq_micro && strcmp (my_plvl, rq_plvl) >= 0)) return 1; return 0; } /* Return true if we have the required gpg version. This should probably go into gpgrt or gpgme proper. */ static int check_gpg_version (const char *req_version) { gpgme_engine_info_t engine_info; init_gpgme (GPGME_PROTOCOL_OpenPGP); fail_if_err (gpgme_get_engine_info (&engine_info)); for (; engine_info; engine_info = engine_info->next) if (engine_info->protocol == GPGME_PROTOCOL_OpenPGP) break; test (engine_info); return compare_versions (engine_info->version, req_version); } static char * get_file (const char *fname) { gpg_error_t err; gpgrt_stream_t fp; struct stat st; char *buf; size_t buflen; fp = gpgrt_fopen (fname, "r"); if (!fp) { err = gpg_error_from_syserror (); fprintf (stderr, "Error: can't open '%s': %s\n", fname, gpg_strerror (err)); return NULL; } if (fstat (gpgrt_fileno(fp), &st)) { err = gpg_error_from_syserror (); fprintf (stderr, "Error: can't stat '%s': %s\n", fname, gpg_strerror (err)); gpgrt_fclose (fp); return NULL; } buflen = st.st_size; buf = malloc (buflen+1); if (!buf) { fprintf (stderr, "Error: no mem\n"); gpgrt_fclose (fp); return NULL; } if (gpgrt_fread (buf, buflen, 1, fp) != 1) { err = gpg_error_from_syserror (); fprintf (stderr, "error reading '%s': %s\n", fname, gpg_strerror (err)); gpgrt_fclose (fp); free (buf); return NULL; } buf[buflen] = 0; gpgrt_fclose (fp); return buf; } /* Check that the element needle exists in hay. Returns 0 if the needle was found. */ int test_contains (cjson_t needle, cjson_t hay) { if (verbose == 2) fprintf (stderr, "%s: -------checking-------- " "\n%s\n -------against-------- \n%s\n", nonnull (needle->string), cJSON_Print (needle), cJSON_Print (hay)); /* Type check. This automatically checks bool vals and NULL */ if (needle->type != hay->type) { if (verbose) fprintf (stderr, "%s: type mismatch expected %i got %i\n", nonnull (needle->string), needle->type, hay->type); return 1; } /* First the simple types */ if (cjson_is_number (needle)) { if (needle->valueint != hay->valueint) { if (verbose) fprintf (stderr, "%s: value mismatch. Expected %i got %i\n", nonnull (needle->string), needle->valueint, hay->valueint); return 1; } } if (cjson_is_string (needle)) { if (strcmp (needle->valuestring, hay->valuestring) && /* Use * as a general don't care placeholder */ strcmp (needle->valuestring, "*")) { if (verbose) fprintf (stderr, "%s: string mismatch Expected '%s' got '%s'\n", needle->string, needle->valuestring, hay->valuestring); return 1; } } /* Now the complex types */ if (needle->child) { if (!hay->child) { fprintf (stderr, "Depth mismatch. Expected child for %s\n", nonnull (needle->string)); } if (test_contains (needle->child, hay->child)) { int found = 0; for (cjson_t hit = hay->child; hit; hit = hit->next) { found |= !test_contains (needle->child, hit); if (found) { break; } } if (!found) { return 1; } } } if (needle->prev) { return 0; } /* Walk elements of an array */ for (cjson_t it = needle->next; it; it = it->next) { int found = 0; if (!it->string && it->child) { /* Try out all other anonymous children on the same level */ cjson_t hit = hay; /* Return to the beginning */ while (hit->prev) { hit = hit->prev; } for (; hit && hit->child; hit = hit->next) { found |= !test_contains (it->child, hit->child); if (found) { break; } } if (!found) { return 1; } continue; } /* Try the children in the haystack */ for (cjson_t hit = hay; hit; hit = hit->next) { if (hit->string && it->string && !strcmp (hit->string, it->string)) { found = 1; if (test_contains (it, hit)) { return 1; } } } if (!found) { if (verbose) fprintf (stderr, "Failed to find '%s' in list\n", nonnull (it->string)); return 1; } } return 0; } int check_response (const char *response, const char *expected) { cjson_t hay; cjson_t needle; int rc; size_t erroff; hay = cJSON_Parse (response, &erroff); if (!hay) { fprintf (stderr, "Failed to parse json at %i:\n%s\n", (int) erroff, response); return 1; } needle = cJSON_Parse (expected, &erroff); if (!needle) { fprintf (stderr, "Failed to parse json at %i:\n%s\n", (int) erroff, expected); cJSON_Delete (hay); return 1; } rc = test_contains (needle, hay); cJSON_Delete (needle); cJSON_Delete (hay); return rc; } int run_test (const char *test, const char *gpgme_json) { gpgme_ctx_t ctx; gpgme_data_t json_stdin = NULL; gpgme_data_t json_stdout = NULL; gpgme_data_t json_stderr = NULL; char *test_in; char *test_out; const char *argv[3]; char *response; char *expected = NULL; size_t response_size; int rc = 0; const char *top_srcdir = getenv ("top_srcdir"); if (!top_srcdir) { fprintf (stderr, "Error top_srcdir environment variable not set\n"); exit(1); } gpgrt_asprintf (&test_in, "%s/tests/json/%s.in.json", top_srcdir, test); gpgrt_asprintf (&test_out, "%s/tests/json/%s.out.json", top_srcdir, test); printf ("Running %s...\n", test); fail_if_err (gpgme_new (&ctx)); gpgme_set_protocol (ctx, GPGME_PROTOCOL_SPAWN); fail_if_err (gpgme_data_new_from_file (&json_stdin, test_in, 1)); fail_if_err (gpgme_data_new (&json_stdout)); fail_if_err (gpgme_data_new (&json_stderr)); argv[0] = gpgme_json; argv[1] = "-s"; argv[2] = NULL; fail_if_err (gpgme_op_spawn (ctx, gpgme_json, argv, json_stdin, json_stdout, json_stderr, 0)); response = gpgme_data_release_and_get_mem (json_stdout, &response_size); if (response_size) { expected = get_file (test_out); test (expected); rc = check_response (response, expected); } else { rc = 1; } if (!rc) { printf (" success\n"); gpgme_data_release (json_stderr); } else { char *buf; size_t size; buf = gpgme_data_release_and_get_mem (json_stderr, &size); printf (" failed%s\n", response_size ? "" : ", no response from gpgme-json"); if (size) { printf ("gpgme-json stderr:\n%.*s\n", (int)size, buf); } free (buf); } free (test_out); free (test_in); free (response); free (expected); gpgme_data_release (json_stdin); gpgme_release (ctx); return rc; } int main (int argc, char *argv[]) { const char *gpgme_json = getenv ("gpgme_json"); int last_argc = -1; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--verbose")) { verbose++; argc--; argv++; } } if (!check_gpg_version ("2.2.0")) { /* Lets not break too much or have to test all combinations */ printf ("Testsuite skipped. Minimum GnuPG version (2.2.0) " "not found.\n"); exit(0); } init_gpgme (GPGME_PROTOCOL_SPAWN); for (const char **test = tests; *test; test++) { if (run_test (*test, gpgme_json)) { exit(1); } } return 0; } diff --git a/tests/opassuan/Makefile.am b/tests/opassuan/Makefile.am index 97fc8fb8..47686eb1 100644 --- a/tests/opassuan/Makefile.am +++ b/tests/opassuan/Makefile.am @@ -1,34 +1,35 @@ # Copyright (C) 2009 g10 Code GmbH # # This file is part of GPGME. # # GPGME is free software; you can redistribute it and/or modify it # under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # GPGME 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 Lesser General # Public License for more details. # # You should have received a copy of the GNU Lesser General Public -# License along with this program; if not, see . +# License along with this program; if not, see . +# SPDX-License-Identifier: LGPL-2.1-or-later ## Process this file with automake to produce Makefile.in GNUPGHOME=$(abs_builddir) TESTS_ENVIRONMENT = GNUPGHOME=$(GNUPGHOME) GPG_AGENT_INFO= noinst_HEADERS = TESTS = EXTRA_DIST = AM_CPPFLAGS = -I$(top_builddir)/src @GPG_ERROR_CFLAGS@ LDADD = ../../src/libgpgme.la noinst_PROGRAMS = $(TESTS) t-command DISTCLEANFILES = diff --git a/tests/opassuan/t-command.c b/tests/opassuan/t-command.c index fd0ea71c..b7e184c4 100644 --- a/tests/opassuan/t-command.c +++ b/tests/opassuan/t-command.c @@ -1,144 +1,145 @@ /* t-command.c - Regression test. - Copyright (C) 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . + * Copyright (C) 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #define fail_if_err(err) \ do \ { \ if (err) \ { \ fprintf (stderr, "%s:%d: %s: %s (%d.%d)\n", \ __FILE__, __LINE__, gpgme_strsource (err), \ gpgme_strerror (err), \ gpgme_err_source (err), gpgme_err_code (err)); \ exit (1); \ } \ } \ while (0) static gpgme_error_t data_cb (void *opaque, const void *data, size_t datalen) { (void)opaque; (void)data; printf ("DATA_CB: datalen=%d\n", (int)datalen); return 0; } static gpgme_error_t inq_cb (void *opaque, const char *name, const char *args, gpgme_data_t *r_data) { gpgme_data_t data; gpgme_error_t err; (void)opaque; if (name) { printf ("INQ_CB: name=`%s' args=`%s'\n", name, args); /* There shall be no data object. */ assert (!*r_data); err = gpgme_data_new (&data); fail_if_err (err); *r_data = data; printf (" sending data object %p\n", data); } else /* Finished using the formerly returned data object. */ { printf ("INQ_CB: data object %p finished\n", *r_data); /* There shall be a data object so that it can be cleaned up. */ assert (r_data); gpgme_data_release (*r_data); } /* Uncomment the next lines and send a "SCD LEARN" to test sending cancel from in inquiry. */ /* if (name && !strcmp (name, "KNOWNCARDP")) */ /* return gpgme_error (GPG_ERR_ASS_CANCELED); */ return 0; } static gpgme_error_t status_cb (void *opaque, const char *status, const char *args) { (void)opaque; printf ("STATUS_CB: status=`%s' args=`%s'\n", status, args); return 0; } int main (int argc, char **argv) { gpgme_error_t err; gpgme_error_t op_err; gpgme_ctx_t ctx; const char *command; gpgme_check_version (NULL); #ifndef HAVE_W32_SYSTEM setlocale (LC_ALL, ""); gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); #endif if (argc) { argc--; argv++; } command = argc? *argv : "NOP"; err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_set_protocol (ctx, GPGME_PROTOCOL_ASSUAN); fail_if_err (err); err = gpgme_op_assuan_transact_ext (ctx, command, data_cb, NULL, inq_cb, NULL, status_cb, NULL, &op_err); fail_if_err (err || op_err); gpgme_release (ctx); return 0; } diff --git a/tests/run-decrypt.c b/tests/run-decrypt.c index c9d9e72d..f1a9fcc0 100644 --- a/tests/run-decrypt.c +++ b/tests/run-decrypt.c @@ -1,338 +1,339 @@ /* run-decrypt.c - Helper to perform a verify operation - Copyright (C) 2009 g10 Code GmbH - 2016 by Bundesamt für Sicherheit in der Informationstechnik - Software engineering by Intevation GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2009 g10 Code GmbH + * 2016 by Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "run-decrypt" #include "run-support.h" static int verbose; static gpg_error_t status_cb (void *opaque, const char *keyword, const char *value) { (void)opaque; fprintf (stderr, "status_cb: %s %s\n", keyword, value); return 0; } static void print_result (gpgme_decrypt_result_t result) { gpgme_recipient_t recp; int count = 0; printf ("Original file name .: %s\n", nonnull(result->file_name)); printf ("Wrong key usage ....: %s\n", result->wrong_key_usage? "yes":"no"); printf ("Legacy w/o MDC ... .: %s\n", result->legacy_cipher_nomdc?"yes":"no"); printf ("Compliance de-vs ...: %s\n", result->is_de_vs? "yes":"no"); printf ("MIME flag ..........: %s\n", result->is_mime? "yes":"no"); printf ("Unsupported algo ...: %s\n", nonnull(result->unsupported_algorithm)); printf ("Session key ........: %s\n", nonnull (result->session_key)); printf ("Symmetric algorithm : %s\n", result->symkey_algo); for (recp = result->recipients; recp && recp->next; recp = recp->next) { printf ("Recipient ...: %d\n", count++); printf (" status ....: %s\n", gpgme_strerror (recp->status)); printf (" keyid .....: %s\n", nonnull (recp->keyid)); printf (" algo ......: %s\n", gpgme_pubkey_algo_name (recp->pubkey_algo)); } } static int show_usage (int ex) { fputs ("usage: " PGM " [options] FILE\n\n" "Options:\n" " --verbose run in verbose mode\n" " --status print status lines from the backend\n" " --openpgp use the OpenPGP protocol (default)\n" " --cms use the CMS protocol\n" " --export-session-key show the session key\n" " --override-session-key STRING use STRING as session key\n" " --request-origin STRING use STRING as request origin\n" " --no-symkey-cache disable the use of that cache\n" " --ignore-mdc-error allow decryption of legacy data\n" " --unwrap remove only the encryption layer\n" " --diagnostics print diagnostics\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; gpgme_decrypt_flags_t flags = 0; FILE *fp_in = NULL; gpgme_data_t in = NULL; gpgme_data_t out = NULL; gpgme_decrypt_result_t result; int print_status = 0; int export_session_key = 0; const char *override_session_key = NULL; const char *request_origin = NULL; int no_symkey_cache = 0; int ignore_mdc_error = 0; int raw_output = 0; int diagnostics = 0; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--status")) { print_status = 1; argc--; argv++; } else if (!strcmp (*argv, "--openpgp")) { protocol = GPGME_PROTOCOL_OpenPGP; argc--; argv++; } else if (!strcmp (*argv, "--cms")) { protocol = GPGME_PROTOCOL_CMS; argc--; argv++; } else if (!strcmp (*argv, "--export-session-key")) { export_session_key = 1; argc--; argv++; } else if (!strcmp (*argv, "--override-session-key")) { argc--; argv++; if (!argc) show_usage (1); override_session_key = *argv; argc--; argv++; } else if (!strcmp (*argv, "--request-origin")) { argc--; argv++; if (!argc) show_usage (1); request_origin = *argv; argc--; argv++; } else if (!strcmp (*argv, "--no-symkey-cache")) { no_symkey_cache = 1; argc--; argv++; } else if (!strcmp (*argv, "--ignore-mdc-error")) { ignore_mdc_error = 1; argc--; argv++; } else if (!strcmp (*argv, "--diagnostics")) { diagnostics = 1; argc--; argv++; } else if (!strcmp (*argv, "--unwrap")) { flags |= GPGME_DECRYPT_UNWRAP; raw_output = 1; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (argc < 1 || argc > 2) show_usage (1); fp_in = fopen (argv[0], "rb"); if (!fp_in) { err = gpgme_error_from_syserror (); fprintf (stderr, PGM ": can't open `%s': %s\n", argv[0], gpgme_strerror (err)); exit (1); } init_gpgme (protocol); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); if (print_status) { gpgme_set_status_cb (ctx, status_cb, NULL); gpgme_set_ctx_flag (ctx, "full-status", "1"); } if (export_session_key) { err = gpgme_set_ctx_flag (ctx, "export-session-key", "1"); if (err) { fprintf (stderr, PGM ": error requesting exported session key: %s\n", gpgme_strerror (err)); exit (1); } } if (override_session_key) { err = gpgme_set_ctx_flag (ctx, "override-session-key", override_session_key); if (err) { fprintf (stderr, PGM ": error setting overriding session key: %s\n", gpgme_strerror (err)); exit (1); } } if (request_origin) { err = gpgme_set_ctx_flag (ctx, "request-origin", request_origin); if (err) { fprintf (stderr, PGM ": error setting request_origin: %s\n", gpgme_strerror (err)); exit (1); } } if (no_symkey_cache) { err = gpgme_set_ctx_flag (ctx, "no-symkey-cache", "1"); if (err) { fprintf (stderr, PGM ": error setting no-symkey-cache: %s\n", gpgme_strerror (err)); exit (1); } } if (ignore_mdc_error) { err = gpgme_set_ctx_flag (ctx, "ignore-mdc-error", "1"); if (err) { fprintf (stderr, PGM ": error setting ignore-mdc-error: %s\n", gpgme_strerror (err)); exit (1); } } err = gpgme_data_new_from_stream (&in, fp_in); if (err) { fprintf (stderr, PGM ": error allocating data object: %s\n", gpgme_strerror (err)); exit (1); } err = gpgme_data_new (&out); if (err) { fprintf (stderr, PGM ": error allocating data object: %s\n", gpgme_strerror (err)); exit (1); } err = gpgme_op_decrypt_ext (ctx, flags, in, out); result = gpgme_op_decrypt_result (ctx); if (diagnostics) { gpgme_data_t diag; gpgme_error_t diag_err; gpgme_data_new (&diag); diag_err = gpgme_op_getauditlog (ctx, diag, GPGME_AUDITLOG_DIAG); if (diag_err) { fprintf (stderr, PGM ": getting diagnostics failed: %s\n", gpgme_strerror (diag_err)); } else { fputs ("Begin Diagnostics:\n", stdout); print_data (diag); fputs ("End Diagnostics.\n", stdout); } gpgme_data_release (diag); } if (err) { fprintf (stderr, PGM ": decrypt failed: %s\n", gpgme_strerror (err)); if (result) print_result (result); exit (1); } if (result) { if (!raw_output) print_result (result); if (!raw_output) fputs ("Begin Output:\n", stdout); print_data (out); if (!raw_output) fputs ("End Output.\n", stdout); } gpgme_data_release (out); gpgme_data_release (in); gpgme_release (ctx); return 0; } diff --git a/tests/run-encrypt.c b/tests/run-encrypt.c index 94084694..7b0e29a7 100644 --- a/tests/run-encrypt.c +++ b/tests/run-encrypt.c @@ -1,340 +1,341 @@ /* run-encrypt.c - Helper to perform an encrypt operation * Copyright (C) 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "run-encrypt" #include "run-support.h" static int verbose; static char * xstrdup (const char *string) { char *p = strdup (string); if (!p) { fprintf (stderr, "strdup failed\n"); exit (2); } return p; } static gpg_error_t status_cb (void *opaque, const char *keyword, const char *value) { (void)opaque; fprintf (stderr, "status_cb: %s %s\n", nonnull(keyword), nonnull(value)); return 0; } static void progress_cb (void *opaque, const char *what, int type, int current, int total) { (void)opaque; (void)type; if (total) fprintf (stderr, "progress for '%s' %u%% (%d of %d)\n", nonnull (what), (unsigned)(((double)current / total) * 100), current, total); else fprintf (stderr, "progress for '%s' %d\n", nonnull(what), current); fflush (stderr); } static void print_result (gpgme_encrypt_result_t result) { gpgme_invalid_key_t invkey; for (invkey = result->invalid_recipients; invkey; invkey = invkey->next) printf ("Encryption key `%s' not used: %s <%s>\n", nonnull (invkey->fpr), gpg_strerror (invkey->reason), gpg_strsource (invkey->reason)); } static int show_usage (int ex) { fputs ("usage: " PGM " [options] FILE\n\n" "Options:\n" " --verbose run in verbose mode\n" " --status print status lines from the backend\n" " --progress print progress info\n" " --openpgp use the OpenPGP protocol (default)\n" " --cms use the CMS protocol\n" " --uiserver use the UI server\n" " --loopback use a loopback pinentry\n" " --key NAME encrypt to key NAME\n" " --keystring NAMES encrypt to ';' delimited NAMES\n" " --throw-keyids use this option\n" " --no-symkey-cache disable the use of that cache\n" " --wrap assume input is valid OpenPGP message\n" " --symmetric encrypt symmetric (OpenPGP only)\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; gpgme_data_t in, out; gpgme_encrypt_result_t result; int print_status = 0; int print_progress = 0; int use_loopback = 0; char *keyargs[10]; gpgme_key_t keys[10+1]; int keycount = 0; char *keystring = NULL; int i; gpgme_encrypt_flags_t flags = GPGME_ENCRYPT_ALWAYS_TRUST; gpgme_off_t offset; int no_symkey_cache = 0; if (argc) { argc--; argv++; } if (DIM(keys) != DIM(keyargs)+1) abort (); while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--status")) { print_status = 1; argc--; argv++; } else if (!strcmp (*argv, "--progress")) { print_progress = 1; argc--; argv++; } else if (!strcmp (*argv, "--openpgp")) { protocol = GPGME_PROTOCOL_OpenPGP; argc--; argv++; } else if (!strcmp (*argv, "--cms")) { protocol = GPGME_PROTOCOL_CMS; argc--; argv++; } else if (!strcmp (*argv, "--uiserver")) { protocol = GPGME_PROTOCOL_UISERVER; argc--; argv++; } else if (!strcmp (*argv, "--key")) { argc--; argv++; if (!argc) show_usage (1); if (keycount == DIM (keyargs)) show_usage (1); keyargs[keycount++] = *argv; argc--; argv++; } else if (!strcmp (*argv, "--keystring")) { argc--; argv++; if (!argc) show_usage (1); keystring = xstrdup (*argv); for (i=0; keystring[i]; i++) if (keystring[i] == ';') keystring[i] = '\n'; argc--; argv++; } else if (!strcmp (*argv, "--throw-keyids")) { flags |= GPGME_ENCRYPT_THROW_KEYIDS; argc--; argv++; } else if (!strcmp (*argv, "--wrap")) { flags |= GPGME_ENCRYPT_WRAP; argc--; argv++; } else if (!strcmp (*argv, "--loopback")) { use_loopback = 1; argc--; argv++; } else if (!strcmp (*argv, "--symmetric")) { flags |= GPGME_ENCRYPT_SYMMETRIC; argc--; argv++; } else if (!strcmp (*argv, "--no-symkey-cache")) { no_symkey_cache = 1; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (argc != 1) show_usage (1); init_gpgme (protocol); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); gpgme_set_armor (ctx, 1); if (print_status) { gpgme_set_status_cb (ctx, status_cb, NULL); gpgme_set_ctx_flag (ctx, "full-status", "1"); } if (print_progress) gpgme_set_progress_cb (ctx, progress_cb, NULL); if (use_loopback) { gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK); gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); } if (no_symkey_cache) { err = gpgme_set_ctx_flag (ctx, "no-symkey-cache", "1"); if (err) { fprintf (stderr, PGM ": error setting no-symkey-cache: %s\n", gpgme_strerror (err)); exit (1); } } for (i=0; i < keycount; i++) { err = gpgme_get_key (ctx, keyargs[i], &keys[i], 0); fail_if_err (err); } keys[i] = NULL; err = gpgme_data_new_from_file (&in, *argv, 1); if (err) { fprintf (stderr, PGM ": error reading `%s': %s\n", *argv, gpg_strerror (err)); exit (1); } offset = gpgme_data_seek (in, 0, SEEK_END); if (offset == (gpgme_off_t)(-1)) { err = gpg_error_from_syserror (); fprintf (stderr, PGM ": error seeking `%s': %s\n", *argv, gpg_strerror (err)); exit (1); } if (gpgme_data_seek (in, 0, SEEK_SET) == (gpgme_off_t)(-1)) { err = gpg_error_from_syserror (); fprintf (stderr, PGM ": error seeking `%s': %s\n", *argv, gpg_strerror (err)); exit (1); } { char numbuf[50]; char *p; p = numbuf + sizeof numbuf; *--p = 0; do { *--p = '0' + (offset % 10); offset /= 10; } while (offset); err = gpgme_data_set_flag (in, "size-hint", p); if (err) { fprintf (stderr, PGM ": error setting size-hint for `%s': %s\n", *argv, gpg_strerror (err)); exit (1); } } err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_encrypt_ext (ctx, keycount ? keys : NULL, keystring, flags, in, out); result = gpgme_op_encrypt_result (ctx); if (result) print_result (result); if (err) { fprintf (stderr, PGM ": encrypting failed: %s\n", gpg_strerror (err)); exit (1); } fputs ("Begin Output:\n", stdout); print_data (out); fputs ("End Output.\n", stdout); gpgme_data_release (out); gpgme_data_release (in); for (i=0; i < keycount; i++) gpgme_key_unref (keys[i]); gpgme_release (ctx); free (keystring); return 0; } diff --git a/tests/run-export.c b/tests/run-export.c index 0bb54686..ecce80dc 100644 --- a/tests/run-export.c +++ b/tests/run-export.c @@ -1,194 +1,195 @@ /* pgp-export.c - Helper to run an export command - Copyright (C) 2008, 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2008, 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "run-export" #include "run-support.h" static int verbose; static int show_usage (int ex) { fputs ("usage: " PGM " [options] USERIDS\n\n" "Options:\n" " --verbose run in verbose mode\n" " --openpgp use OpenPGP protocol (default)\n" " --cms use X.509 protocol\n" " --extern send keys to the keyserver (TAKE CARE!)\n" " --secret export secret keys instead of public keys\n" " --raw use PKCS#1 as secret key format\n" " --pkcs12 use PKCS#12 as secret key format\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; gpgme_key_t key; gpgme_keylist_result_t result; gpgme_key_t keyarray[100]; int keyidx = 0; gpgme_data_t out; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; gpgme_export_mode_t mode = 0; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--openpgp")) { protocol = GPGME_PROTOCOL_OpenPGP; argc--; argv++; } else if (!strcmp (*argv, "--cms")) { protocol = GPGME_PROTOCOL_CMS; argc--; argv++; } else if (!strcmp (*argv, "--extern")) { mode |= GPGME_EXPORT_MODE_EXTERN; argc--; argv++; } else if (!strcmp (*argv, "--secret")) { mode |= GPGME_EXPORT_MODE_SECRET; argc--; argv++; } else if (!strcmp (*argv, "--raw")) { mode |= GPGME_EXPORT_MODE_RAW; argc--; argv++; } else if (!strcmp (*argv, "--pkcs12")) { mode |= GPGME_EXPORT_MODE_PKCS12; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (!argc) show_usage (1); init_gpgme (protocol); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); /* Lookup the keys. */ err = gpgme_op_keylist_ext_start (ctx, (const char**)argv, 0, 0); fail_if_err (err); while (!(err = gpgme_op_keylist_next (ctx, &key))) { printf ("keyid: %s (fpr: %s)\n", key->subkeys?nonnull (key->subkeys->keyid):"?", key->subkeys?nonnull (key->subkeys->fpr):"?"); if (keyidx < DIM (keyarray)-1) keyarray[keyidx++] = key; else { fprintf (stderr, PGM": too many keys" "- skipping this key\n"); gpgme_key_unref (key); } } if (gpgme_err_code (err) != GPG_ERR_EOF) fail_if_err (err); err = gpgme_op_keylist_end (ctx); fail_if_err (err); keyarray[keyidx] = NULL; result = gpgme_op_keylist_result (ctx); if (result->truncated) { fprintf (stderr, PGM ": key listing unexpectedly truncated\n"); exit (1); } /* Now for the actual export. */ if ((mode & GPGME_EXPORT_MODE_EXTERN)) printf ("sending keys to keyserver\n"); if ((mode & GPGME_EXPORT_MODE_SECRET)) printf ("exporting secret keys!\n"); err = gpgme_data_new (&out); fail_if_err (err); gpgme_set_armor (ctx, 1); err = gpgme_op_export_keys (ctx, keyarray, mode, (mode & GPGME_KEYLIST_MODE_EXTERN)? NULL:out); fail_if_err (err); fflush (NULL); if (!(mode & GPGME_KEYLIST_MODE_EXTERN)) { fputs ("Begin Result:\n", stdout); print_data (out); fputs ("End Result.\n", stdout); } /* Cleanup. */ gpgme_data_release (out); for (keyidx=0; keyarray[keyidx]; keyidx++) gpgme_key_unref (keyarray[keyidx]); gpgme_release (ctx); return 0; } diff --git a/tests/run-genkey.c b/tests/run-genkey.c index 91edb222..fd9b42a7 100644 --- a/tests/run-genkey.c +++ b/tests/run-genkey.c @@ -1,466 +1,467 @@ /* run-genkey.c - Test tool to perform key generation * Copyright (C) 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #define PGM "run-genkey" #include "run-support.h" static int verbose; /* Tokenize STRING using the set of delimiters in DELIM. Leading * spaces and tabs are removed from all tokens. The caller must free * the result. * * Returns: A malloced and NULL delimited array with the tokens. On * memory error NULL is returned and ERRNO is set. */ static char ** strtokenize (const char *string, const char *delim) { const char *s; size_t fields; size_t bytes, n; char *buffer; char *p, *px, *pend; char **result; /* Count the number of fields. */ for (fields = 1, s = strpbrk (string, delim); s; s = strpbrk (s + 1, delim)) fields++; fields++; /* Add one for the terminating NULL. */ /* Allocate an array for all fields, a terminating NULL, and space for a copy of the string. */ bytes = fields * sizeof *result; if (bytes / sizeof *result != fields) { gpg_err_set_errno (ENOMEM); return NULL; } n = strlen (string) + 1; bytes += n; if (bytes < n) { gpg_err_set_errno (ENOMEM); return NULL; } result = malloc (bytes); if (!result) return NULL; buffer = (char*)(result + fields); /* Copy and parse the string. */ strcpy (buffer, string); for (n = 0, p = buffer; (pend = strpbrk (p, delim)); p = pend + 1) { *pend = 0; while (*p == ' ' || *p == '\t') p++; for (px = pend - 1; px >= p && (*px == ' ' || *px == '\t'); px--) *px = 0; result[n++] = p; } while (*p == ' ' || *p == '\t') p++; for (px = p + strlen (p) - 1; px >= p && (*px == ' ' || *px == '\t'); px--) *px = 0; result[n++] = p; result[n] = NULL; assert ((char*)(result + n + 1) == buffer); return result; } static gpg_error_t status_cb (void *opaque, const char *keyword, const char *value) { (void)opaque; fprintf (stderr, "status_cb: %s %s\n", nonnull(keyword), nonnull(value)); return 0; } static void progress_cb (void *opaque, const char *what, int type, int current, int total) { (void)opaque; (void)type; if (total) fprintf (stderr, "progress for '%s' %u%% (%d of %d)\n", nonnull (what), (unsigned)(((double)current / total) * 100), current, total); else fprintf (stderr, "progress for '%s' %d\n", nonnull(what), current); fflush (stderr); } static unsigned long parse_expire_string (const char *string) { unsigned long seconds; if (!string || !*string || !strcmp (string, "none") || !strcmp (string, "never") || !strcmp (string, "-")) seconds = 0; else if (strspn (string, "01234567890") == strlen (string)) seconds = strtoul (string, NULL, 10); else { fprintf (stderr, PGM ": invalid value '%s'\n", string); exit (1); } return seconds; } /* Parse a usage string and return flags for gpgme_op_createkey. */ static unsigned int parse_usage_string (const char *string) { gpg_error_t err; char **tokens = NULL; const char *s; int i; unsigned int flags = 0; tokens = strtokenize (string, " \t,"); if (!tokens) { err = gpg_error_from_syserror (); fprintf (stderr, PGM": strtokenize failed: %s\n", gpg_strerror (err)); exit (1); } for (i=0; (s = tokens[i]); i++) { if (!*s) ; else if (!strcmp (s, "default")) ; else if (!strcmp (s, "sign")) flags |= GPGME_CREATE_SIGN; else if (!strcmp (s, "encr")) flags |= GPGME_CREATE_ENCR; else if (!strcmp (s, "cert")) flags |= GPGME_CREATE_CERT; else if (!strcmp (s, "auth")) flags |= GPGME_CREATE_AUTH; else { free (tokens); fprintf (stderr, PGM": invalid value '%s': %s\n", string, "bad usage"); exit (1); } } free (tokens); return flags; } static int show_usage (int ex) { fputs ("usage: " PGM " [options] ARGS\n" " args: USERID [ALGO [USAGE [EXPIRESECONDS]]]\n" " for addkey: FPR [ALGO [USAGE [EXPIRESECONDS]]]\n" " for adduid: FPR USERID\n" " for revuid: FPR USERID\n" " for set-primary: FPR USERID\n" "Options:\n" " --addkey add a subkey to the key with FPR\n" " --adduid add a user id to the key with FPR\n" " --revuid revoke a user id from the key with FPR\n" " --set-primary set the primary key flag on USERID\n" " --verbose run in verbose mode\n" " --status print status lines from the backend\n" " --progress print progress info\n" " --openpgp use the OpenPGP protocol (default)\n" " --cms use the CMS protocol\n" " --loopback use a loopback pinentry\n" " --unprotected do not use a passphrase\n" " --force do not check for a duplicated user id\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; int print_status = 0; int print_progress = 0; int use_loopback = 0; int addkey = 0; int adduid = 0; int revuid = 0; int setpri = 0; const char *userid; const char *algo = NULL; const char *newuserid = NULL; unsigned int flags = 0; unsigned long expire = 0; gpgme_genkey_result_t result; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--addkey")) { addkey = 1; adduid = 0; revuid = 0; setpri = 0; argc--; argv++; } else if (!strcmp (*argv, "--adduid")) { addkey = 0; adduid = 1; revuid = 0; setpri = 0; argc--; argv++; } else if (!strcmp (*argv, "--revuid")) { addkey = 0; adduid = 0; revuid = 1; setpri = 0; argc--; argv++; } else if (!strcmp (*argv, "--set-primary")) { addkey = 0; adduid = 0; revuid = 0; setpri = 1; argc--; argv++; } else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--status")) { print_status = 1; argc--; argv++; } else if (!strcmp (*argv, "--progress")) { print_progress = 1; argc--; argv++; } else if (!strcmp (*argv, "--openpgp")) { protocol = GPGME_PROTOCOL_OpenPGP; argc--; argv++; } else if (!strcmp (*argv, "--cms")) { protocol = GPGME_PROTOCOL_CMS; argc--; argv++; } else if (!strcmp (*argv, "--loopback")) { use_loopback = 1; argc--; argv++; } else if (!strcmp (*argv, "--unprotected")) { flags |= GPGME_CREATE_NOPASSWD; argc--; argv++; } else if (!strcmp (*argv, "--force")) { flags |= GPGME_CREATE_FORCE; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (adduid || revuid || setpri) { if (argc != 2) show_usage (1); userid = argv[0]; newuserid = argv[1]; } else { if (!argc || argc > 4) show_usage (1); userid = argv[0]; if (argc > 1) algo = argv[1]; if (argc > 2) flags |= parse_usage_string (argv[2]); if (argc > 3) expire = parse_expire_string (argv[3]); } init_gpgme (protocol); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); gpgme_set_armor (ctx, 1); if (print_status) { gpgme_set_status_cb (ctx, status_cb, NULL); gpgme_set_ctx_flag (ctx, "full-status", "1"); } if (print_progress) gpgme_set_progress_cb (ctx, progress_cb, NULL); if (use_loopback) { gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK); gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); } if (addkey || adduid || revuid || setpri) { gpgme_key_t akey; err = gpgme_get_key (ctx, userid, &akey, 1); if (err) { fprintf (stderr, PGM ": error getting secret key for '%s': %s\n", userid, gpg_strerror (err)); exit (1); } if (addkey) { err = gpgme_op_createsubkey (ctx, akey, algo, 0, expire, flags); if (err) { fprintf (stderr, PGM ": gpgme_op_createsubkey failed: %s\n", gpg_strerror (err)); exit (1); } } else if (adduid) { err = gpgme_op_adduid (ctx, akey, newuserid, flags); if (err) { fprintf (stderr, PGM ": gpgme_op_adduid failed: %s\n", gpg_strerror (err)); exit (1); } } else if (revuid) { err = gpgme_op_revuid (ctx, akey, newuserid, flags); if (err) { fprintf (stderr, PGM ": gpgme_op_revuid failed: %s\n", gpg_strerror (err)); exit (1); } } else if (setpri) { err = gpgme_op_set_uid_flag (ctx, akey, newuserid, "primary", NULL); if (err) { fprintf (stderr, PGM ": gpgme_op_set_uid_flag failed: %s\n", gpg_strerror (err)); exit (1); } } gpgme_key_unref (akey); } else { err = gpgme_op_createkey (ctx, userid, algo, 0, expire, NULL, flags); if (err) { fprintf (stderr, PGM ": gpgme_op_createkey failed: %s\n", gpg_strerror (err)); exit (1); } } if (!setpri) { result = gpgme_op_genkey_result (ctx); if (!result) { fprintf (stderr, PGM": gpgme_op_genkey_result returned NULL\n"); exit (1); } printf ("Generated key: %s (%s)\n", result->fpr ? result->fpr : "none", result->primary ? (result->sub ? "primary, sub" : "primary") /**/ : (result->sub ? "sub" : "none")); if (result->fpr && strlen (result->fpr) < 40) fprintf (stderr, PGM": generated key has unexpected fingerprint\n"); if (!result->primary) fprintf (stderr, PGM": primary key was not generated\n"); if (!result->sub) fprintf (stderr, PGM": sub key was not generated\n"); if (!result->uid) fprintf (stderr, PGM": uid was not generated\n"); } gpgme_release (ctx); return 0; } diff --git a/tests/run-identify.c b/tests/run-identify.c index 9361fa2d..91228143 100644 --- a/tests/run-identify.c +++ b/tests/run-identify.c @@ -1,129 +1,130 @@ /* run-identify - Helper to run the identify command * Copyright (C) 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "run-identify" #include "run-support.h" static int verbose; static const char * data_type_to_string (gpgme_data_type_t dt) { const char *s = "[?]"; switch (dt) { case GPGME_DATA_TYPE_INVALID : s = "invalid"; break; case GPGME_DATA_TYPE_UNKNOWN : s = "unknown"; break; case GPGME_DATA_TYPE_PGP_SIGNED : s = "PGP-signed"; break; case GPGME_DATA_TYPE_PGP_SIGNATURE: s = "PGP-signature"; break; case GPGME_DATA_TYPE_PGP_ENCRYPTED: s = "PGP-encrypted"; break; case GPGME_DATA_TYPE_PGP_OTHER : s = "PGP"; break; case GPGME_DATA_TYPE_PGP_KEY : s = "PGP-key"; break; case GPGME_DATA_TYPE_CMS_SIGNED : s = "CMS-signed"; break; case GPGME_DATA_TYPE_CMS_ENCRYPTED: s = "CMS-encrypted"; break; case GPGME_DATA_TYPE_CMS_OTHER : s = "CMS"; break; case GPGME_DATA_TYPE_X509_CERT : s = "X.509"; break; case GPGME_DATA_TYPE_PKCS12 : s = "PKCS12"; break; } return s; } static int show_usage (int ex) { fputs ("usage: " PGM " [options] FILENAMEs\n\n" "Options:\n" " --verbose run in verbose mode\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; int anyerr = 0; gpgme_data_t data; gpgme_data_type_t dt; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } init_gpgme_basic (); for (; argc; argc--, argv++) { if (verbose) printf ("reading file `%s'\n", *argv); err = gpgme_data_new_from_file (&data, *argv, 1); if (err) { fprintf (stderr, PGM ": error reading '%s': %s\n", *argv, gpg_strerror (err)); anyerr = 1; } else { dt = gpgme_data_identify (data, 0); if (dt == GPGME_DATA_TYPE_INVALID) anyerr = 1; printf ("%s: %s\n", *argv, data_type_to_string (dt)); gpgme_data_release (data); } } return anyerr; } diff --git a/tests/run-import.c b/tests/run-import.c index 081c0fa3..a3c52ac0 100644 --- a/tests/run-import.c +++ b/tests/run-import.c @@ -1,129 +1,130 @@ /* pgp-import.c - Helper to run an import command - Copyright (C) 2008, 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2008, 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "run-import" #include "run-support.h" static int verbose; static int show_usage (int ex) { fputs ("usage: " PGM " [options] FILENAMEs\n\n" "Options:\n" " --verbose run in verbose mode\n" " --url import from given URLs\n" " -0 URLs are delimited by a nul\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; int url_mode = 0; int nul_mode = 0; gpgme_import_result_t impres; gpgme_data_t data; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--url")) { url_mode = 1; argc--; argv++; } else if (!strcmp (*argv, "-0")) { nul_mode = 1; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (!argc) show_usage (1); init_gpgme (GPGME_PROTOCOL_OpenPGP); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, GPGME_PROTOCOL_OpenPGP); for (; argc; argc--, argv++) { printf ("reading file `%s'\n", *argv); err = gpgme_data_new_from_file (&data, *argv, 1); fail_if_err (err); if (url_mode) gpgme_data_set_encoding (data, (nul_mode? GPGME_DATA_ENCODING_URL0 : GPGME_DATA_ENCODING_URL)); err = gpgme_op_import (ctx, data); fail_if_err (err); impres = gpgme_op_import_result (ctx); if (!impres) { fprintf (stderr, PGM ": no import result returned\n"); exit (1); } print_import_result (impres); gpgme_data_release (data); } gpgme_release (ctx); return 0; } diff --git a/tests/run-keylist.c b/tests/run-keylist.c index 08b671dd..d12e3703 100644 --- a/tests/run-keylist.c +++ b/tests/run-keylist.c @@ -1,424 +1,425 @@ /* run-keylist.c - Helper to show a key listing. - Copyright (C) 2008, 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2008, 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #define PGM "run-keylist" #include "run-support.h" static int verbose; static int show_usage (int ex) { fputs ("usage: " PGM " [options] [USERID_or_FILE]\n\n" "Options:\n" " --verbose run in verbose mode\n" " --openpgp use the OpenPGP protocol (default)\n" " --cms use the CMS protocol\n" " --secret list only secret keys\n" " --with-secret list pubkeys with secret info filled\n" " --local use GPGME_KEYLIST_MODE_LOCAL\n" " --extern use GPGME_KEYLIST_MODE_EXTERN\n" " --sigs use GPGME_KEYLIST_MODE_SIGS\n" " --tofu use GPGME_KEYLIST_MODE_TOFU\n" " --sig-notations use GPGME_KEYLIST_MODE_SIG_NOTATIONS\n" " --ephemeral use GPGME_KEYLIST_MODE_EPHEMERAL\n" " --validate use GPGME_KEYLIST_MODE_VALIDATE\n" " --import import all keys\n" " --offline use offline mode\n" " --from-file list all keys in the given file\n" " --from-wkd list key from a web key directory\n" " --require-gnupg required at least the given GnuPG version\n" " --trust-model use the specified trust-model\n" , stderr); exit (ex); } static const char * isotimestr (unsigned long value) { time_t t; static char buffer[25+5]; struct tm *tp; if (!value) return "none"; t = value; tp = gmtime (&t); snprintf (buffer, sizeof buffer, "%04d-%02d-%02d %02d:%02d:%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec); return buffer; } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; gpgme_keylist_mode_t mode = 0; gpgme_key_t key; gpgme_subkey_t subkey; gpgme_keylist_result_t result; int import = 0; gpgme_key_t keyarray[100]; int keyidx = 0; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; int only_secret = 0; int offline = 0; int from_file = 0; int from_wkd = 0; gpgme_data_t data = NULL; char *trust_model = NULL; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--openpgp")) { protocol = GPGME_PROTOCOL_OpenPGP; argc--; argv++; } else if (!strcmp (*argv, "--cms")) { protocol = GPGME_PROTOCOL_CMS; argc--; argv++; } else if (!strcmp (*argv, "--secret")) { only_secret = 1; argc--; argv++; } else if (!strcmp (*argv, "--local")) { mode |= GPGME_KEYLIST_MODE_LOCAL; argc--; argv++; } else if (!strcmp (*argv, "--extern")) { mode |= GPGME_KEYLIST_MODE_EXTERN; argc--; argv++; } else if (!strcmp (*argv, "--tofu")) { mode |= GPGME_KEYLIST_MODE_WITH_TOFU; argc--; argv++; } else if (!strcmp (*argv, "--sigs")) { mode |= GPGME_KEYLIST_MODE_SIGS; argc--; argv++; } else if (!strcmp (*argv, "--sig-notations")) { mode |= GPGME_KEYLIST_MODE_SIG_NOTATIONS; argc--; argv++; } else if (!strcmp (*argv, "--ephemeral")) { mode |= GPGME_KEYLIST_MODE_EPHEMERAL; argc--; argv++; } else if (!strcmp (*argv, "--validate")) { mode |= GPGME_KEYLIST_MODE_VALIDATE; argc--; argv++; } else if (!strcmp (*argv, "--with-secret")) { mode |= GPGME_KEYLIST_MODE_WITH_SECRET; argc--; argv++; } else if (!strcmp (*argv, "--import")) { import = 1; argc--; argv++; } else if (!strcmp (*argv, "--offline")) { offline = 1; argc--; argv++; } else if (!strcmp (*argv, "--from-file")) { from_file = 1; argc--; argv++; } else if (!strcmp (*argv, "--require-gnupg")) { argc--; argv++; if (!argc) show_usage (1); gpgme_set_global_flag ("require-gnupg", *argv); argc--; argv++; } else if (!strcmp (*argv, "--from-wkd")) { argc--; argv++; mode |= GPGME_KEYLIST_MODE_LOCATE; from_wkd = 1; } else if (!strcmp (*argv, "--trust-model")) { argc--; argv++; if (!argc) show_usage (1); trust_model = strdup (*argv); argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (argc > 1) show_usage (1); else if (from_file && !argc) show_usage (1); init_gpgme (protocol); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); gpgme_set_keylist_mode (ctx, mode); gpgme_set_offline (ctx, offline); if (trust_model) { err = gpgme_set_ctx_flag (ctx, "trust-model", trust_model); fail_if_err (err); } if (from_wkd) { err = gpgme_set_ctx_flag (ctx, "auto-key-locate", "clear,nodefault,wkd"); fail_if_err (err); } if (from_file) { err = gpgme_data_new_from_file (&data, *argv, 1); fail_if_err (err); err = gpgme_op_keylist_from_data_start (ctx, data, 0); } else err = gpgme_op_keylist_start (ctx, argc? argv[0]:NULL, only_secret); fail_if_err (err); while (!(err = gpgme_op_keylist_next (ctx, &key))) { gpgme_user_id_t uid; gpgme_tofu_info_t ti; gpgme_key_sig_t ks; int nuids; int nsub; int nsigs; printf ("keyid : %s\n", key->subkeys?nonnull (key->subkeys->keyid):"?"); printf ("fpr : %s\n", key->subkeys?nonnull (key->subkeys->fpr):"?"); if (key->subkeys && key->subkeys->keygrip) printf ("grip : %s\n", key->subkeys->keygrip); if (key->subkeys && key->subkeys->curve) printf ("curve : %s\n", key->subkeys->curve); printf ("caps : %s%s%s%s\n", key->can_encrypt? "e":"", key->can_sign? "s":"", key->can_certify? "c":"", key->can_authenticate? "a":""); printf ("flags :%s%s%s%s%s%s%s%s\n", key->secret? " secret":"", key->revoked? " revoked":"", key->expired? " expired":"", key->disabled? " disabled":"", key->invalid? " invalid":"", key->is_qualified? " qualified":"", key->subkeys && key->subkeys->is_de_vs? " de-vs":"", key->subkeys && key->subkeys->is_cardkey? " cardkey":""); printf ("upd : %lu (%u)\n", key->last_update, key->origin); subkey = key->subkeys; if (subkey) subkey = subkey->next; for (nsub=1; subkey; subkey = subkey->next, nsub++) { printf ("fpr %2d: %s\n", nsub, nonnull (subkey->fpr)); if (subkey->keygrip) printf ("grip %2d: %s\n", nsub, subkey->keygrip); if (subkey->curve) printf ("curve %2d: %s\n", nsub, subkey->curve); printf ("caps %2d: %s%s%s%s\n", nsub, subkey->can_encrypt? "e":"", subkey->can_sign? "s":"", subkey->can_certify? "c":"", subkey->can_authenticate? "a":""); printf ("flags %2d:%s%s%s%s%s%s%s%s\n", nsub, subkey->secret? " secret":"", subkey->revoked? " revoked":"", subkey->expired? " expired":"", subkey->disabled? " disabled":"", subkey->invalid? " invalid":"", subkey->is_qualified? " qualified":"", subkey->is_de_vs? " de-vs":"", subkey->is_cardkey? " cardkey":""); } for (nuids=0, uid=key->uids; uid; uid = uid->next, nuids++) { printf ("userid %d: %s\n", nuids, nonnull(uid->uid)); printf (" mbox: %s\n", nonnull(uid->address)); if (uid->email && uid->email != uid->address) printf (" email: %s\n", uid->email); if (uid->name) printf (" name: %s\n", uid->name); if (uid->comment) printf (" cmmnt: %s\n", uid->comment); printf (" upd: %lu (%u)\n", uid->last_update, uid->origin); printf (" valid: %s\n", uid->validity == GPGME_VALIDITY_UNKNOWN? "unknown": uid->validity == GPGME_VALIDITY_UNDEFINED? "undefined": uid->validity == GPGME_VALIDITY_NEVER? "never": uid->validity == GPGME_VALIDITY_MARGINAL? "marginal": uid->validity == GPGME_VALIDITY_FULL? "full": uid->validity == GPGME_VALIDITY_ULTIMATE? "ultimate": "[?]"); if ((ti = uid->tofu)) { printf (" tofu: %u (%s)\n", ti->validity, ti->validity == 0? "conflict" : ti->validity == 1? "no history" : ti->validity == 2? "little history" : ti->validity == 3? "enough history" : ti->validity == 4? "lot of history" : "?"); printf (" policy: %u (%s)\n", ti->policy, ti->policy == GPGME_TOFU_POLICY_NONE? "none" : ti->policy == GPGME_TOFU_POLICY_AUTO? "auto" : ti->policy == GPGME_TOFU_POLICY_GOOD? "good" : ti->policy == GPGME_TOFU_POLICY_UNKNOWN? "unknown" : ti->policy == GPGME_TOFU_POLICY_BAD? "bad" : ti->policy == GPGME_TOFU_POLICY_ASK? "ask" : "?"); printf (" nsigs: %hu\n", ti->signcount); printf (" first: %s\n", isotimestr (ti->signfirst)); printf (" last: %s\n", isotimestr (ti->signlast)); printf (" nencr: %hu\n", ti->encrcount); printf (" first: %s\n", isotimestr (ti->encrfirst)); printf (" last: %s\n", isotimestr (ti->encrlast)); } for (nsigs=0, ks=uid->signatures; ks; ks = ks->next, nsigs++) { printf ("signature %d: %s\n", nsigs, nonnull (ks->uid)); printf (" keyid: %s\n", nonnull (ks->keyid)); printf (" created: %s\n", isotimestr(ks->timestamp)); printf (" expires: %s\n", isotimestr(ks->expires)); printf (" class: %x\n", ks->sig_class); } } putchar ('\n'); if (import) { if (keyidx < DIM (keyarray)-1) keyarray[keyidx++] = key; else { fprintf (stderr, PGM": too many keys in import mode" "- skipping this key\n"); gpgme_key_unref (key); } } else gpgme_key_unref (key); } if (gpgme_err_code (err) != GPG_ERR_EOF) fail_if_err (err); err = gpgme_op_keylist_end (ctx); fail_if_err (err); keyarray[keyidx] = NULL; gpgme_data_release (data); result = gpgme_op_keylist_result (ctx); if (result->truncated) { fprintf (stderr, PGM ": key listing unexpectedly truncated\n"); exit (1); } if (import) { gpgme_import_result_t impres; err = gpgme_op_import_keys (ctx, keyarray); fail_if_err (err); impres = gpgme_op_import_result (ctx); if (!impres) { fprintf (stderr, PGM ": no import result returned\n"); exit (1); } print_import_result (impres); } for (keyidx=0; keyarray[keyidx]; keyidx++) gpgme_key_unref (keyarray[keyidx]); free (trust_model); gpgme_release (ctx); return 0; } diff --git a/tests/run-keysign.c b/tests/run-keysign.c index 5f6cfced..83130dd0 100644 --- a/tests/run-keysign.c +++ b/tests/run-keysign.c @@ -1,261 +1,262 @@ /* run-keysign.c - Test tool to sign a key * Copyright (C) 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #define PGM "run-keysign" #include "run-support.h" static int verbose; static gpg_error_t status_cb (void *opaque, const char *keyword, const char *value) { (void)opaque; fprintf (stderr, "status_cb: %s %s\n", nonnull(keyword), nonnull(value)); return 0; } static unsigned long parse_expire_string (const char *string) { unsigned long seconds; if (!string || !*string || !strcmp (string, "none") || !strcmp (string, "never") || !strcmp (string, "-")) seconds = 0; else if (strspn (string, "01234567890") == strlen (string)) seconds = strtoul (string, NULL, 10); else { fprintf (stderr, PGM ": invalid value '%s'\n", string); exit (1); } return seconds; } static int show_usage (int ex) { fputs ("usage: " PGM " [options] FPR USERIDS\n\n" "Options:\n" " --verbose run in verbose mode\n" " --status print status lines from the backend\n" " --loopback use a loopback pinentry\n" " --signer NAME use key NAME for signing\n" " --local create a local signature\n" " --noexpire force no expiration\n" " --expire EPOCH expire the signature at EPOCH\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; const char *signer_string = NULL; int print_status = 0; int use_loopback = 0; const char *userid; unsigned int flags = 0; unsigned long expire = 0; gpgme_key_t thekey; int i; size_t n; char *userid_buffer = NULL; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--status")) { print_status = 1; argc--; argv++; } else if (!strcmp (*argv, "--signer")) { argc--; argv++; if (!argc) show_usage (1); signer_string = *argv; argc--; argv++; } else if (!strcmp (*argv, "--loopback")) { use_loopback = 1; argc--; argv++; } else if (!strcmp (*argv, "--local")) { flags |= GPGME_KEYSIGN_LOCAL; argc--; argv++; } else if (!strcmp (*argv, "--noexpire")) { flags |= GPGME_KEYSIGN_NOEXPIRE; argc--; argv++; } else if (!strcmp (*argv, "--expire")) { argc--; argv++; if (!argc) show_usage (1); expire = parse_expire_string (*argv); argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (!argc) show_usage (1); userid = argv[0]; argc--; argv++; init_gpgme (protocol); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); gpgme_set_armor (ctx, 1); if (print_status) { gpgme_set_status_cb (ctx, status_cb, NULL); gpgme_set_ctx_flag (ctx, "full-status", "1"); } if (use_loopback) { gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK); gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL); } if (signer_string) { gpgme_key_t akey; err = gpgme_get_key (ctx, signer_string, &akey, 1); if (err) { fprintf (stderr, PGM ": error getting signer key '%s': %s\n", signer_string, gpg_strerror (err)); exit (1); } err = gpgme_signers_add (ctx, akey); if (err) { fprintf (stderr, PGM ": error adding signer key: %s\n", gpg_strerror (err)); exit (1); } gpgme_key_unref (akey); } err = gpgme_get_key (ctx, userid, &thekey, 0); if (err) { fprintf (stderr, PGM ": error getting key for '%s': %s\n", userid, gpg_strerror (err)); exit (1); } if (argc > 1) { /* Several user ids given */ for (i=0, n = 0; i < argc; i++) n += strlen (argv[1]) + 1; n++; userid_buffer = malloc (n); if (!userid_buffer) { fprintf (stderr, PGM ": malloc failed: %s\n", gpg_strerror (gpg_error_from_syserror ())); exit (1); } *userid_buffer = 0; for (i=0; i < argc; i++) { strcat (userid_buffer, argv[i]); strcat (userid_buffer, "\n"); } userid = userid_buffer; flags |= GPGME_KEYSIGN_LFSEP; } else if (argc) { /* One user id given */ userid = *argv; } else { /* No user id given. */ userid = NULL; } err = gpgme_op_keysign (ctx, thekey, userid, expire, flags); if (err) { fprintf (stderr, PGM ": gpgme_op_adduid failed: %s\n", gpg_strerror (err)); exit (1); } free (userid_buffer); gpgme_key_unref (thekey); gpgme_release (ctx); return 0; } diff --git a/tests/run-sign.c b/tests/run-sign.c index 1daf173c..5576b8f5 100644 --- a/tests/run-sign.c +++ b/tests/run-sign.c @@ -1,245 +1,246 @@ /* run-sign.c - Helper to perform a sign operation - Copyright (C) 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "run-sign" #include "run-support.h" static int verbose; static gpg_error_t status_cb (void *opaque, const char *keyword, const char *value) { (void)opaque; printf ("status_cb: %s %s\n", keyword, value); return 0; } static void print_result (gpgme_sign_result_t result, gpgme_sig_mode_t type) { gpgme_invalid_key_t invkey; gpgme_new_signature_t sig; (void)type; for (invkey = result->invalid_signers; invkey; invkey = invkey->next) printf ("Signing key `%s' not used: %s <%s>\n", nonnull (invkey->fpr), gpg_strerror (invkey->reason), gpg_strsource (invkey->reason)); for (sig = result->signatures; sig; sig = sig->next) { printf ("Key fingerprint: %s\n", nonnull (sig->fpr)); printf ("Signature type : %d\n", sig->type); printf ("Public key algo: %d\n", sig->pubkey_algo); printf ("Hash algo .....: %d\n", sig->hash_algo); printf ("Creation time .: %ld\n", sig->timestamp); printf ("Sig class .....: 0x%u\n", sig->sig_class); } } static int show_usage (int ex) { fputs ("usage: " PGM " [options] FILE\n\n" "Options:\n" " --verbose run in verbose mode\n" " --status print status lines from the backend\n" " --openpgp use the OpenPGP protocol (default)\n" " --cms use the CMS protocol\n" " --uiserver use the UI server\n" " --loopback use a loopback pinentry\n" " --key NAME use key NAME for signing\n" " --sender MBOX use MBOX as sender address\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; const char *key_string = NULL; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; gpgme_sig_mode_t sigmode = GPGME_SIG_MODE_NORMAL; gpgme_data_t in, out; gpgme_sign_result_t result; int print_status = 0; int use_loopback = 0; const char *sender = NULL; const char *s; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--status")) { print_status = 1; argc--; argv++; } else if (!strcmp (*argv, "--openpgp")) { protocol = GPGME_PROTOCOL_OpenPGP; argc--; argv++; } else if (!strcmp (*argv, "--cms")) { protocol = GPGME_PROTOCOL_CMS; argc--; argv++; } else if (!strcmp (*argv, "--uiserver")) { protocol = GPGME_PROTOCOL_UISERVER; argc--; argv++; } else if (!strcmp (*argv, "--key")) { argc--; argv++; if (!argc) show_usage (1); key_string = *argv; argc--; argv++; } else if (!strcmp (*argv, "--sender")) { argc--; argv++; if (!argc) show_usage (1); sender = *argv; argc--; argv++; } else if (!strcmp (*argv, "--loopback")) { use_loopback = 1; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (argc != 1) show_usage (1); if (key_string && protocol == GPGME_PROTOCOL_UISERVER) { fprintf (stderr, PGM ": ignoring --key in UI-server mode\n"); key_string = NULL; } init_gpgme (protocol); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); gpgme_set_armor (ctx, 1); if (print_status) gpgme_set_status_cb (ctx, status_cb, NULL); if (use_loopback) gpgme_set_pinentry_mode (ctx, GPGME_PINENTRY_MODE_LOOPBACK); if (key_string) { gpgme_key_t akey; err = gpgme_get_key (ctx, key_string, &akey, 1); if (err) { exit (1); } err = gpgme_signers_add (ctx, akey); fail_if_err (err); gpgme_key_unref (akey); } if (sender) { err = gpgme_set_sender (ctx, sender); fail_if_err (err); } err = gpgme_data_new_from_file (&in, *argv, 1); if (err) { fprintf (stderr, PGM ": error reading `%s': %s\n", *argv, gpg_strerror (err)); exit (1); } err = gpgme_data_new (&out); fail_if_err (err); err = gpgme_op_sign (ctx, in, out, sigmode); result = gpgme_op_sign_result (ctx); if (result) print_result (result, sigmode); if (err) { fprintf (stderr, PGM ": signing failed: %s\n", gpg_strerror (err)); exit (1); } if ((s = gpgme_get_ctx_flag (ctx, "redraw")) && *s) fputs ("Screen redraw suggested\n", stdout); fputs ("Begin Output:\n", stdout); print_data (out); fputs ("End Output.\n", stdout); gpgme_data_release (out); gpgme_data_release (in); gpgme_release (ctx); return 0; } diff --git a/tests/run-support.h b/tests/run-support.h index 6c713a9a..d7b2987b 100644 --- a/tests/run-support.h +++ b/tests/run-support.h @@ -1,198 +1,199 @@ /* run-support.h - Helper routines for run-* test programs. - Copyright (C) 2000 Werner Koch (dd9jn) - Copyright (C) 2001, 2002, 2003, 2004, 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2000 Werner Koch (dd9jn) + * Copyright (C) 2001, 2002, 2003, 2004, 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #include #include #include #include #ifdef HAVE_W32_SYSTEM #include #endif #include #ifndef DIM #define DIM(v) (sizeof(v)/sizeof((v)[0])) #endif #define fail_if_err(err) \ do \ { \ if (err) \ { \ fprintf (stderr, PGM": file %s line %d: <%s> %s\n", \ __FILE__, __LINE__, gpgme_strsource (err), \ gpgme_strerror (err)); \ exit (1); \ } \ } \ while (0) static const char * nonnull (const char *s) { return s? s :"[none]"; } void print_data (gpgme_data_t dh) { #define BUF_SIZE 512 char buf[BUF_SIZE + 1]; int ret; ret = gpgme_data_seek (dh, 0, SEEK_SET); if (ret) fail_if_err (gpgme_err_code_from_errno (errno)); while ((ret = gpgme_data_read (dh, buf, BUF_SIZE)) > 0) fwrite (buf, ret, 1, stdout); if (ret < 0) fail_if_err (gpgme_err_code_from_errno (errno)); } gpgme_error_t passphrase_cb (void *opaque, const char *uid_hint, const char *passphrase_info, int last_was_bad, int fd) { int res; char pass[] = "abc\n"; int passlen = strlen (pass); int off = 0; (void)opaque; (void)uid_hint; (void)passphrase_info; (void)last_was_bad; do { res = gpgme_io_write (fd, &pass[off], passlen - off); if (res > 0) off += res; } while (res > 0 && off != passlen); return off == passlen ? 0 : gpgme_error_from_errno (errno); } char * make_filename (const char *fname) { const char *srcdir = getenv ("srcdir"); char *buf; if (!srcdir) srcdir = "."; buf = malloc (strlen(srcdir) + strlen(fname) + 2); if (!buf) { fprintf (stderr, "%s:%d: could not allocate string: %s\n", __FILE__, __LINE__, strerror (errno)); exit (8); } strcpy (buf, srcdir); strcat (buf, "/"); strcat (buf, fname); return buf; } void init_gpgme_basic (void) { gpgme_check_version (NULL); setlocale (LC_ALL, ""); gpgme_set_locale (NULL, LC_CTYPE, setlocale (LC_CTYPE, NULL)); #ifndef HAVE_W32_SYSTEM gpgme_set_locale (NULL, LC_MESSAGES, setlocale (LC_MESSAGES, NULL)); #endif } void init_gpgme (gpgme_protocol_t proto) { gpg_error_t err; init_gpgme_basic (); err = gpgme_engine_check_version (proto); fail_if_err (err); } void print_import_result (gpgme_import_result_t r) { gpgme_import_status_t st; for (st=r->imports; st; st = st->next) { printf (" fpr: %s err: %d (%s) status:", nonnull (st->fpr), st->result, gpgme_strerror (st->result)); if (st->status & GPGME_IMPORT_NEW) fputs (" new", stdout); if (st->status & GPGME_IMPORT_UID) fputs (" uid", stdout); if (st->status & GPGME_IMPORT_SIG) fputs (" sig", stdout); if (st->status & GPGME_IMPORT_SUBKEY) fputs (" subkey", stdout); if (st->status & GPGME_IMPORT_SECRET) fputs (" secret", stdout); putchar ('\n'); } printf ("key import summary:\n" " considered: %d\n" " no user id: %d\n" " imported: %d\n" " imported_rsa: %d\n" " unchanged: %d\n" " new user ids: %d\n" " new subkeys: %d\n" " new signatures: %d\n" " new revocations: %d\n" " secret read: %d\n" " secret imported: %d\n" " secret unchanged: %d\n" " skipped new keys: %d\n" " not imported: %d\n" " skipped v3 keys: %d\n", r->considered, r->no_user_id, r->imported, r->imported_rsa, r->unchanged, r->new_user_ids, r->new_sub_keys, r->new_signatures, r->new_revocations, r->secret_read, r->secret_imported, r->secret_unchanged, r->skipped_new_keys, r->not_imported, r->skipped_v3_keys); } diff --git a/tests/run-swdb.c b/tests/run-swdb.c index a3732549..a8d55b7a 100644 --- a/tests/run-swdb.c +++ b/tests/run-swdb.c @@ -1,151 +1,152 @@ /* run-swdb.c - Test tool for SWDB function * Copyright (C) 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #define PGM "run-swdb" #include "run-support.h" static int verbose; static const char * isotimestr (unsigned long value) { time_t t; static char buffer[25+5]; struct tm *tp; if (!value) return "none"; t = value; tp = gmtime (&t); snprintf (buffer, sizeof buffer, "%04d-%02d-%02d %02d:%02d:%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec); return buffer; } static int show_usage (int ex) { fputs ("usage: " PGM " [options] NAME [VERSION]\n\n" "Options:\n" " --verbose run in verbose mode\n" " --status print status lines from the backend\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; gpgme_protocol_t protocol = GPGME_PROTOCOL_GPGCONF; const char *name; const char *iversion; gpgme_query_swdb_result_t result; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (argc < 1 || argc > 2) show_usage (1); name = argv[0]; iversion = argc > 1? argv[1] : NULL; init_gpgme (protocol); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); err = gpgme_op_query_swdb (ctx, name, iversion, 0); if (err) { fprintf (stderr, PGM ": error querying swdb: %s\n", gpg_strerror (err)); exit (1); } result = gpgme_op_query_swdb_result (ctx); if (!result) { fprintf (stderr, PGM ": error querying swdb: %s\n", "no result"); exit (1); } printf ("package ...: %s\n" "iversion ..: %s\n" "version ...: %s\n", nonnull (result->name), nonnull (result->iversion), nonnull (result->version)); printf ("reldate ...: %s\n", isotimestr (result->reldate)); printf ("created ...: %s\n", isotimestr (result->created)); printf ("retrieved .: %s\n", isotimestr (result->retrieved)); printf ("flags .....:%s%s%s%s%s%s%s\n", result->warning? " warning" : "", result->update? " update" : "", result->urgent? " urgent" : "", result->unknown? " unknown" : "", result->tooold? " tooold" : "", result->noinfo? " noinfo" : "", result->error? " error" : "" ); gpgme_release (ctx); return 0; } diff --git a/tests/run-threaded.c b/tests/run-threaded.c index 14a9cbec..93d15883 100644 --- a/tests/run-threaded.c +++ b/tests/run-threaded.c @@ -1,678 +1,679 @@ /* run-threaded.c - Helper to put GPGME under multithread load. - Copyright (C) 2018 by Bundesamt für Sicherheit in der Informationstechnik - Software engineering by Intevation GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2018 by Bundesamt für Sicherheit in der Informationstechnik + * Software engineering by Intevation GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* The idea of this test is not to be run as unit test but as part * of development to find threading issues and resource leaks. */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include #include #include #include #include #include #define PGM "run-threaded" #include "run-support.h" static volatile int stop; static volatile int thread_cnt; static volatile int running_threads; static int verbose; static int data_type; static int mem_only; #ifdef HAVE_W32_SYSTEM # include # define THREAD_RET DWORD CALLBACK static void create_thread (THREAD_RET (*func) (void *), void *arg) { running_threads++; if (CloseHandle (CreateThread (NULL, 0, func, arg, 0, NULL))) { fprintf (stderr, "Failed to create thread!\n"); exit (1); } } #else # include # define THREAD_RET void * static void create_thread (THREAD_RET (func) (void *), void *arg) { pthread_t handle; running_threads++; if (pthread_create (&handle, NULL, func, arg)) { fprintf (stderr, "Failed to create thread!\n"); exit (1); } } #endif #include GPGRT_LOCK_DEFINE (out_lock); #define out(format, ...) \ { \ gpgrt_lock_lock (&out_lock); \ printf (format "\n", ##__VA_ARGS__); \ gpgrt_lock_unlock (&out_lock); \ } #define errpoint \ { \ out ("Error on %i", __LINE__); \ exit (1); \ } #define log(format, ...) \ if (verbose) \ { \ gpgrt_lock_lock (&out_lock); \ printf (format "\n", ##__VA_ARGS__); \ gpgrt_lock_unlock (&out_lock); \ } /* Lazy mans signal */ GPGRT_LOCK_DEFINE (threads_lock); void del_thread (void) { if (--running_threads == 0) { gpgrt_lock_unlock (&threads_lock); } } static int show_usage (int ex) { fputs ("usage: " PGM " [options] [messages]\n\n" "Options:\n" " --verbose run in verbose mode\n" " --no-list do not do keylistings\n" " --data-type mem function to use one of:\n" " 1: fstream\n" " 2: posix fd\n" " 3: memory\n" " 4: gpgrt_stream\n" " default: random\n" /* " --mem-cache read data only once and then work on memory\n" " exlusive with data-type option\n" */ " --threads N use 4+N threads (4 are used for keylisting" " default 1)\n" " --repeat N do N repeats on the messages (default 1)\n\n" "Note: The test does keylistings of both S/MIME and OpenPGP\n" " in the background while running operations on the\n" " messages, depending on their type.\n" " (Currently decrypt / verify).\n\n" " Without messages only keylistings will be done.\n" , stderr); exit (ex); } struct msg_list_s { const char *file_name; struct msg_list_s *next; }; typedef struct msg_list_s *msg_list_t; struct data_s { int fd; FILE *file; gpgrt_stream_t stream; unsigned char *mem; gpgme_data_t dh; }; typedef struct data_s *data_t; struct keylist_args_s { gpgme_protocol_t proto; int secret; }; typedef struct keylist_args_s *keylist_args_t; static volatile int keylists; static THREAD_RET do_keylist (void *keylist_args) { gpgme_error_t err; gpgme_ctx_t ctx; gpgme_key_t key; keylist_args_t args = (keylist_args_t) keylist_args; log ("Keylist %i, Protocol: %s, Secret %i", keylists++, args->proto == GPGME_PROTOCOL_CMS ? "CMS" : "OpenPGP", args->secret); err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_set_protocol (ctx, args->proto); fail_if_err (err); err = gpgme_op_keylist_start (ctx, NULL, args->secret); fail_if_err (err); while (!(err = gpgme_op_keylist_next (ctx, &key))) { gpgme_key_unref (key); } if (gpgme_err_code (err) != GPG_ERR_EOF) { fail_if_err (err); } gpgme_release (ctx); if (!stop) { create_thread (do_keylist, keylist_args); } del_thread (); return 0; } static unsigned char * get_file (const char *fname, size_t *r_size) { gpg_error_t err; gpgrt_stream_t fp; struct stat st; unsigned char *buf; size_t buflen; fp = gpgrt_fopen (fname, "r"); if (!fp) { err = gpg_error_from_syserror (); fprintf (stderr, "Error: can't open '%s': %s\n", fname, gpg_strerror (err)); return NULL; } if (fstat (gpgrt_fileno(fp), &st)) { err = gpg_error_from_syserror (); fprintf (stderr, "Error: can't stat '%s': %s\n", fname, gpg_strerror (err)); gpgrt_fclose (fp); return NULL; } buflen = st.st_size; buf = malloc (buflen+1); if (!buf) { fprintf (stderr, "Error: no mem\n"); gpgrt_fclose (fp); return NULL; } if (gpgrt_fread (buf, buflen, 1, fp) != 1) { err = gpg_error_from_syserror (); fprintf (stderr, "error reading '%s': %s\n", fname, gpg_strerror (err)); gpgrt_fclose (fp); free (buf); return NULL; } buf[buflen] = 0; gpgrt_fclose (fp); if (r_size) { *r_size = buflen; } return buf; } /** Lets use random data. This should also introduce a bit of randomness into the system by changing the runtimes of various data functions. Esp. Mem against the file ops. */ data_t random_data_new (const char *fname) { data_t ret = calloc (1, sizeof (struct data_s)); int data_rand; if (data_type) { data_rand = data_type; } else { data_rand = rand () % 3; } if (data_rand == 0) /* stream */ { FILE *f_stream = fopen (fname, "rb"); if (!f_stream) { errpoint; } fail_if_err (gpgme_data_new_from_stream (&(ret->dh), f_stream)); ret->file = f_stream; return ret; } if (data_rand == 1) /* fd */ { int fd = open (fname, O_RDONLY); gpgme_data_t dh; if (fd == -1) { errpoint; } fail_if_err (gpgme_data_new_from_fd (&dh, fd)); ret->fd = fd; ret->dh = dh; return ret; } if (data_rand == 2) /* mem */ { unsigned char *mem; size_t size; mem = get_file (fname, &size); if (!mem) { errpoint; } fail_if_err (gpgme_data_new_from_mem (&(ret->dh), (const char *)mem, size, 0)); ret->mem = mem; return ret; } if (data_rand == 3) /* estream */ { gpgrt_stream_t stream = gpgrt_fopen (fname, "rb"); if (!stream) { errpoint; } fail_if_err (gpgme_data_new_from_estream (&(ret->dh), stream)); ret->stream = stream; return ret; } /* notreached */ return ret; } void random_data_close (data_t data) { if (data->dh) { gpgme_data_release (data->dh); } if (data->fd) { close (data->fd); } else if (data->file) { fclose (data->file); } else if (data->stream) { gpgrt_fclose (data->stream); } else if (data->mem) { free (data->mem); } free (data); } void verify (const char *fname, gpgme_protocol_t proto) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t out; char *msg; size_t msg_len; data_t data = random_data_new (fname); log ("Starting verify on: %s with protocol %s", fname, proto == GPGME_PROTOCOL_CMS ? "CMS" : "OpenPGP"); gpgme_data_new (&out); err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_set_protocol (ctx, proto); fail_if_err (err); err = gpgme_op_verify (ctx, data->dh, NULL, out); out ("Data: %p, %i %p %p %p", data->dh, data->fd, data->file, data->stream, data->mem); fail_if_err (err); msg = gpgme_data_release_and_get_mem (out, &msg_len); if (msg_len) { log ("Verify result \n'%.*s'", (int)msg_len, msg); } gpgme_release (ctx); random_data_close (data); } /* We randomize data access to put in a bit additional entropy in this test and also to check if maybe some data functions might not be properly thread safe. */ void decrypt (const char *fname, gpgme_protocol_t proto) { gpgme_ctx_t ctx; gpgme_error_t err; gpgme_data_t out; char *msg; size_t msg_len; data_t data = random_data_new (fname); log ("Starting decrypt on: %s", fname); gpgme_data_new (&out); err = gpgme_new (&ctx); fail_if_err (err); err = gpgme_set_protocol (ctx, proto); fail_if_err (err); err = gpgme_op_decrypt (ctx, data->dh, out); fail_if_err (err); gpgme_release (ctx); msg = gpgme_data_release_and_get_mem (out, &msg_len); if (msg_len) { log ("Decrypt result \n'%.*s'", (int)msg_len, msg); } random_data_close (data); } static THREAD_RET do_data_op (void *file_name) { gpgme_data_t data; const char *fname = (const char *) file_name; FILE *f = fopen (fname, "rb"); gpgme_data_type_t type; if (!f) { fprintf (stderr, "Failed to open '%s'\n", fname); exit (1); } fail_if_err (gpgme_data_new_from_stream (&data, f)); type = gpgme_data_identify (data, 0); gpgme_data_release (data); fclose (f); switch (type) { case GPGME_DATA_TYPE_INVALID: case GPGME_DATA_TYPE_UNKNOWN: { fprintf (stderr, "Failed to identify '%s'", fname); exit(1); } case GPGME_DATA_TYPE_PGP_SIGNED: { verify (fname, GPGME_PROTOCOL_OpenPGP); break; } case GPGME_DATA_TYPE_CMS_SIGNED: { verify (fname, GPGME_PROTOCOL_CMS); break; } case GPGME_DATA_TYPE_PGP_ENCRYPTED: { decrypt (fname, GPGME_PROTOCOL_OpenPGP); break; } case GPGME_DATA_TYPE_CMS_ENCRYPTED: { decrypt (fname, GPGME_PROTOCOL_CMS); break; } default: { fprintf (stderr, "Unhandled data type 0x%x for '%s'", type, fname); exit(1); } } del_thread (); return 0; } void start_keylistings (void) { static struct keylist_args_s args[4]; args[0].proto = GPGME_PROTOCOL_OpenPGP; args[0].secret = 0; args[1].proto = GPGME_PROTOCOL_OpenPGP; args[1].secret = 1; args[2].proto = GPGME_PROTOCOL_CMS; args[2].secret = 0; args[3].proto = GPGME_PROTOCOL_CMS; args[3].secret = 1; for (int i = 0; i < 4; i++) { thread_cnt--; create_thread (do_keylist, &args[i]); } } int main (int argc, char **argv) { int last_argc = -1; int repeats = 1; int threads = 0; int no_list = 0; msg_list_t msgs = NULL; msg_list_t msg_it = NULL; stop = 0; srand (1 /* Somewhat deterministic results */); if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--help")) { show_usage (0); } else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--no-list")) { no_list = 1; argc--; argv++; } else if (!strcmp (*argv, "--mem-only")) { if (data_type) { show_usage (1); } mem_only = 1; argc--; argv++; } else if (!strcmp (*argv, "--threads")) { argc--; argv++; if (!argc) { show_usage (1); } threads = atoi (*argv); if (!threads) { show_usage (1); } argc--; argv++; } else if (!strcmp (*argv, "--data-type")) { argc--; argv++; if (!argc || mem_only) { show_usage (1); } data_type = atoi (*argv); if (data_type > 4) { show_usage (1); } argc--; argv++; } else if (!strcmp (*argv, "--repeat")) { argc--; argv++; if (!argc) { show_usage (1); } repeats = atoi (*argv); if (!repeats) { show_usage (1); } argc--; argv++; } } init_gpgme_basic (); if (threads < argc) { /* Make sure we run once on each arg */ threads += argc; } while (argc) { if (!msgs) { msgs = calloc (1, sizeof *msgs); msg_it = msgs; } else { msg_it->next = calloc (1, sizeof *msgs); msg_it = msg_it->next; } msg_it->file_name = *argv; argc--; argv++; } gpgrt_lock_lock (&threads_lock); do { stop = 0; thread_cnt = threads + 4; out ("Repeats left: %i", repeats); if (!no_list) { start_keylistings (); } else { thread_cnt -= 4; } while (thread_cnt) { log ("Thread %i", thread_cnt); for (msg_it = msgs; msg_it && thread_cnt; msg_it = msg_it->next) { thread_cnt--; create_thread (do_data_op, (void *)msg_it->file_name); } } stop = 1; gpgrt_lock_lock (&threads_lock); } while (--repeats != 0); return 0; } diff --git a/tests/run-tofu.c b/tests/run-tofu.c index 1f11c2d6..424ec288 100644 --- a/tests/run-tofu.c +++ b/tests/run-tofu.c @@ -1,200 +1,201 @@ /* run-tofu.c - Test tool for Tofu functions * Copyright (C) 2016 g10 Code GmbH * * This file is part of GPGME. * * GPGME is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as * published by the Free Software Foundation; either version 2.1 of * the License, or (at your option) any later version. * * GPGME 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public - * License along with this program; if not, see . + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #define PGM "run-tofu" #include "run-support.h" static int verbose; static gpg_error_t status_cb (void *opaque, const char *keyword, const char *value) { (void)opaque; fprintf (stderr, "status_cb: %s %s\n", nonnull(keyword), nonnull(value)); return 0; } static gpgme_tofu_policy_t parse_policy_string (const char *string) { gpgme_tofu_policy_t policy; if (!strcmp (string, "auto")) policy = GPGME_TOFU_POLICY_AUTO; else if (!strcmp (string, "good")) policy = GPGME_TOFU_POLICY_GOOD; else if (!strcmp (string, "bad")) policy = GPGME_TOFU_POLICY_BAD; else if (!strcmp (string, "ask")) policy = GPGME_TOFU_POLICY_ASK; else if (!strcmp (string, "unknown")) policy = GPGME_TOFU_POLICY_UNKNOWN; else { fprintf (stderr, PGM ": invalid policy value '%s'\n", string); exit (1); } return policy; } static int show_usage (int ex) { fputs ("usage: " PGM " [options] FPR\n\n" "Options:\n" " --policy NAME Set tofu policy for key to NAME\n" " --verbose run in verbose mode\n" " --status print status lines from the backend\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; gpgme_error_t err; gpgme_ctx_t ctx; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; int print_status = 0; gpgme_key_t thekey; const char *fpr; const char *policystr = NULL; gpgme_tofu_policy_t policy; const char *s; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--status")) { print_status = 1; argc--; argv++; } else if (!strcmp (*argv, "--policy")) { argc--; argv++; if (!argc) show_usage (1); policystr = *argv; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (argc != 1) show_usage (1); fpr = argv[0]; init_gpgme (protocol); err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); gpgme_set_armor (ctx, 1); s = gpgme_get_ctx_flag (ctx, "no_such-flag"); if (s) { fprintf (stderr, PGM ": gpgme_get_ctx_flag failed " "(bad name not detected)\n"); exit (1); } s = gpgme_get_ctx_flag (ctx, "full-status"); if (!s || *s) { fprintf (stderr, PGM ": gpgme_get_ctx_flag failed (wrong false)\n"); exit (1); } if (print_status) { gpgme_set_status_cb (ctx, status_cb, NULL); gpgme_set_ctx_flag (ctx, "full-status", "1"); s = gpgme_get_ctx_flag (ctx, "full-status"); if (!s || strcmp (s, "1")) { fprintf (stderr, PGM ": gpgme_get_ctx_flag fauled (wrong true)\n"); exit (1); } } err = gpgme_get_key (ctx, fpr, &thekey, 0); if (err) { fprintf (stderr, PGM ": error getting key '%s': %s\n", fpr, gpg_strerror (err)); exit (1); } if (policystr) { policy = parse_policy_string (policystr); err = gpgme_op_tofu_policy (ctx, thekey, policy); if (err) { fprintf (stderr, PGM ": gpgme_op_tofu_polciy failed: %s\n", gpg_strerror (err)); exit (1); } } gpgme_key_unref (thekey); gpgme_release (ctx); return 0; } diff --git a/tests/run-verify.c b/tests/run-verify.c index b8dd0213..5e3d9cb4 100644 --- a/tests/run-verify.c +++ b/tests/run-verify.c @@ -1,418 +1,419 @@ /* run-verify.c - Helper to perform a verify operation - Copyright (C) 2009 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this program; if not, see . -*/ + * Copyright (C) 2009 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #define PGM "run-verify" #include "run-support.h" static int verbose; static const char * isotimestr (unsigned long value) { time_t t; static char buffer[25+5]; struct tm *tp; if (!value) return "none"; t = value; tp = gmtime (&t); snprintf (buffer, sizeof buffer, "%04d-%02d-%02d %02d:%02d:%02d", 1900+tp->tm_year, tp->tm_mon+1, tp->tm_mday, tp->tm_hour, tp->tm_min, tp->tm_sec); return buffer; } static gpg_error_t status_cb (void *opaque, const char *keyword, const char *value) { (void)opaque; fprintf (stderr, "status_cb: %s %s\n", keyword, value); return 0; } static void print_summary (gpgme_sigsum_t summary) { if ( (summary & GPGME_SIGSUM_VALID )) fputs (" valid", stdout); if ( (summary & GPGME_SIGSUM_GREEN )) fputs (" green", stdout); if ( (summary & GPGME_SIGSUM_RED )) fputs (" red", stdout); if ( (summary & GPGME_SIGSUM_KEY_REVOKED)) fputs (" revoked", stdout); if ( (summary & GPGME_SIGSUM_KEY_EXPIRED)) fputs (" key-expired", stdout); if ( (summary & GPGME_SIGSUM_SIG_EXPIRED)) fputs (" sig-expired", stdout); if ( (summary & GPGME_SIGSUM_KEY_MISSING)) fputs (" key-missing", stdout); if ( (summary & GPGME_SIGSUM_CRL_MISSING)) fputs (" crl-missing", stdout); if ( (summary & GPGME_SIGSUM_CRL_TOO_OLD)) fputs (" crl-too-old", stdout); if ( (summary & GPGME_SIGSUM_BAD_POLICY )) fputs (" bad-policy", stdout); if ( (summary & GPGME_SIGSUM_SYS_ERROR )) fputs (" sys-error", stdout); } static void print_validity (gpgme_validity_t val) { const char *s = NULL; switch (val) { case GPGME_VALIDITY_UNKNOWN: s = "unknown"; break; case GPGME_VALIDITY_UNDEFINED:s = "undefined"; break; case GPGME_VALIDITY_NEVER: s = "never"; break; case GPGME_VALIDITY_MARGINAL: s = "marginal"; break; case GPGME_VALIDITY_FULL: s = "full"; break; case GPGME_VALIDITY_ULTIMATE: s = "ultimate"; break; } if (s) fputs (s, stdout); else printf ("[bad validity value %u]", (unsigned int)val); } static void print_description (const char *text, int indent) { for (; *text; text++) { putchar (*text); if (*text == '\n') printf ("%*s", indent, ""); } putchar ('\n'); } static void print_result (gpgme_verify_result_t result) { gpgme_signature_t sig; gpgme_sig_notation_t nt; gpgme_user_id_t uid; gpgme_tofu_info_t ti; int count = 0; printf ("Original file name .: %s\n", nonnull(result->file_name)); printf ("MIME flag ..........: %s\n", result->is_mime? "yes":"no"); for (sig = result->signatures; sig; sig = sig->next) { printf ("Signature ...: %d\n", count++); printf (" status ....: %s\n", gpgme_strerror (sig->status)); printf (" summary ...:"); print_summary (sig->summary); putchar ('\n'); printf (" fingerprint: %s\n", nonnull (sig->fpr)); printf (" created ...: %lu\n", sig->timestamp); printf (" expires ...: %lu\n", sig->exp_timestamp); printf (" validity ..: "); print_validity (sig->validity); putchar ('\n'); printf (" val.reason : %s\n", gpgme_strerror (sig->status)); printf (" pubkey algo: %d (%s)\n", sig->pubkey_algo, nonnull(gpgme_pubkey_algo_name (sig->pubkey_algo))); printf (" digest algo: %d (%s)\n", sig->hash_algo, nonnull(gpgme_hash_algo_name (sig->hash_algo))); printf (" pka address: %s\n", nonnull (sig->pka_address)); printf (" pka trust .: %s\n", sig->pka_trust == 0? "n/a" : sig->pka_trust == 1? "bad" : sig->pka_trust == 2? "okay": "RFU"); printf (" other flags:%s%s\n", sig->wrong_key_usage? " wrong-key-usage":"", sig->chain_model? " chain-model":"" ); for (nt = sig->notations; nt; nt = nt->next) { if (nt->name) { printf (" notation ..: '%s'\n", nt->name); if (strlen (nt->name) != nt->name_len) printf (" warning .: name larger (%d)\n", nt->name_len); printf (" flags ...:%s%s (0x%02x)\n", nt->critical? " critical":"", nt->human_readable? " human":"", nt->flags); if (nt->value) printf (" value ...: '%s'\n", nt->value); } else { printf (" policy ....: '%s'\n", nt->value); } if ((nt->value?strlen (nt->value):0) != nt->value_len) printf (" warning .: value larger (%d)\n", nt->value_len); } if (sig->key) { printf (" primary fpr: %s\n", nonnull (sig->key->fpr)); for (uid = sig->key->uids; uid; uid = uid->next) { printf (" tofu addr .: %s\n", nonnull (uid->address)); ti = uid->tofu; if (!ti) continue; printf (" validity : %u (%s)\n", ti->validity, ti->validity == 0? "conflict" : ti->validity == 1? "no history" : ti->validity == 2? "little history" : ti->validity == 3? "enough history" : ti->validity == 4? "lot of history" : "?"); printf (" policy ..: %u (%s)\n", ti->policy, ti->policy == GPGME_TOFU_POLICY_NONE? "none" : ti->policy == GPGME_TOFU_POLICY_AUTO? "auto" : ti->policy == GPGME_TOFU_POLICY_GOOD? "good" : ti->policy == GPGME_TOFU_POLICY_UNKNOWN? "unknown" : ti->policy == GPGME_TOFU_POLICY_BAD? "bad" : ti->policy == GPGME_TOFU_POLICY_ASK? "ask" : "?"); printf (" signcount: %hu\n", ti->signcount); printf (" first..: %s\n", isotimestr (ti->signfirst)); printf (" last ..: %s\n", isotimestr (ti->signlast)); printf (" encrcount: %hu\n", ti->encrcount); printf (" first..: %s\n", isotimestr (ti->encrfirst)); printf (" last ..: %s\n", isotimestr (ti->encrlast)); printf (" desc ....: "); print_description (nonnull (ti->description), 15); } } } } static int show_usage (int ex) { fputs ("usage: " PGM " [options] [DETACHEDSIGFILE] FILE\n\n" "Options:\n" " --verbose run in verbose mode\n" " --status print status lines from the backend\n" " --openpgp use the OpenPGP protocol (default)\n" " --cms use the CMS protocol\n" " --sender MBOX use MBOX as sender address\n" " --repeat N repeat the operation N times\n" " --auto-key-retrieve\n" , stderr); exit (ex); } int main (int argc, char **argv) { int last_argc = -1; const char *s; gpgme_protocol_t protocol = GPGME_PROTOCOL_OpenPGP; int print_status = 0; const char *sender = NULL; int auto_key_retrieve = 0; int repeats = 1; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) show_usage (0); else if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--status")) { print_status = 1; argc--; argv++; } else if (!strcmp (*argv, "--openpgp")) { protocol = GPGME_PROTOCOL_OpenPGP; argc--; argv++; } else if (!strcmp (*argv, "--cms")) { protocol = GPGME_PROTOCOL_CMS; argc--; argv++; } else if (!strcmp (*argv, "--sender")) { argc--; argv++; if (!argc) show_usage (1); sender = *argv; argc--; argv++; } else if (!strcmp (*argv, "--repeat")) { argc--; argv++; if (!argc) show_usage (1); repeats = atoi (*argv); argc--; argv++; } else if (!strcmp (*argv, "--auto-key-retrieve")) { auto_key_retrieve = 1; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) show_usage (1); } if (argc < 1 || argc > 2) show_usage (1); init_gpgme (protocol); for (int i = 0; i < repeats; i++) { gpgme_error_t err; gpgme_ctx_t ctx; FILE *fp_sig = NULL; gpgme_data_t sig = NULL; FILE *fp_msg = NULL; gpgme_data_t msg = NULL; gpgme_verify_result_t result; if (repeats > 1) { printf ("Repeat: %i\n", i); } fp_sig = fopen (argv[0], "rb"); if (!fp_sig) { err = gpgme_error_from_syserror (); fprintf (stderr, PGM ": can't open `%s': %s\n", argv[0], gpgme_strerror (err)); exit (1); } if (argc > 1) { fp_msg = fopen (argv[1], "rb"); if (!fp_msg) { err = gpgme_error_from_syserror (); fprintf (stderr, PGM ": can't open `%s': %s\n", argv[1], gpgme_strerror (err)); exit (1); } } err = gpgme_new (&ctx); fail_if_err (err); gpgme_set_protocol (ctx, protocol); if (print_status) { gpgme_set_status_cb (ctx, status_cb, NULL); gpgme_set_ctx_flag (ctx, "full-status", "1"); } /* gpgme_set_ctx_flag (ctx, "raw-description", "1"); */ if (auto_key_retrieve) { gpgme_set_ctx_flag (ctx, "auto-key-retrieve", "1"); s = gpgme_get_ctx_flag (ctx, "auto-key-retrieve"); if (!s || strcmp (s, "1")) { fprintf (stderr, PGM ": gpgme_get_ctx_flag failed for '%s'\n", "auto-key-retrieve"); exit (1); } } if (sender) { err = gpgme_set_sender (ctx, sender); fail_if_err (err); } err = gpgme_data_new_from_stream (&sig, fp_sig); if (err) { fprintf (stderr, PGM ": error allocating data object: %s\n", gpgme_strerror (err)); exit (1); } if (fp_msg) { err = gpgme_data_new_from_stream (&msg, fp_msg); if (err) { fprintf (stderr, PGM ": error allocating data object: %s\n", gpgme_strerror (err)); exit (1); } } err = gpgme_op_verify (ctx, sig, msg, NULL); result = gpgme_op_verify_result (ctx); if (result) print_result (result); if (err) { fprintf (stderr, PGM ": verify failed: %s\n", gpgme_strerror (err)); exit (1); } gpgme_data_release (msg); gpgme_data_release (sig); gpgme_release (ctx); if (fp_msg) fclose (fp_msg); if (fp_sig) fclose (fp_sig); } return 0; } diff --git a/tests/t-data.c b/tests/t-data.c index c214de7b..ea70f043 100644 --- a/tests/t-data.c +++ b/tests/t-data.c @@ -1,274 +1,274 @@ /* t-data - Regression tests for the gpgme_data_t abstraction. - Copyright (C) 2001, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2001, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ /* We need to include config.h so that we know whether we are building with large file system (LFS) support. */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #include #define PGM "t-data" #include "run-support.h" #undef fail_if_err #define fail_if_err(a) do { if(a) { \ fprintf (stderr, "%s:%d: (%i) gpgme_error_t " \ "%s\n", __FILE__, __LINE__, round, \ gpgme_strerror(a)); \ exit (1); } \ } while(0) typedef enum { TEST_INITIALIZER, TEST_INVALID_ARGUMENT, TEST_INOUT_NONE, TEST_INOUT_MEM_NO_COPY, TEST_INOUT_MEM_COPY, TEST_INOUT_MEM_FROM_FILE_COPY, TEST_INOUT_MEM_FROM_INEXISTANT_FILE, TEST_INOUT_MEM_FROM_FILE_NO_COPY, TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME, TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART, TEST_INOUT_MEM_FROM_FILE_PART_BY_FP, TEST_END } round_t; const char *text = "Just GNU it!\n"; const char *text2 = "Just GNU it!\nJust GNU it!\n"; int read_cb (void *cb_value, char *buffer, size_t count, size_t *nread) { static int off = 0; unsigned int amount = strlen (text) - off; /* round_t round = *((round_t *) cb_value); */ (void)cb_value; if (!buffer && !count && !nread) { /* Rewind requested. */ off = 0; return 0; } if (! buffer || !nread) return -1; if (amount <= 0) { /* End of file. */ *nread = 0; return -1; } if (amount > count) amount = count; memcpy (buffer, text, amount); off += amount; *nread = amount; return 0; } void read_once_test (round_t round, gpgme_data_t data) { char buffer[1024]; size_t read; read = gpgme_data_read (data, buffer, sizeof (buffer)); if (read != strlen (text) || strncmp (buffer, text, strlen (text))) { fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n", __FILE__, __LINE__, round); exit (1); } read = gpgme_data_read (data, buffer, sizeof (buffer)); if (read) { fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n", __FILE__, __LINE__, round); exit (1); } } void read_test (round_t round, gpgme_data_t data) { char buffer[1024]; size_t read; if (round == TEST_INOUT_NONE) { read = gpgme_data_read (data, buffer, sizeof (buffer)); if (read > 0) { fprintf (stderr, "%s:%d: (%i) gpgme_data_read succeeded unexpectedly\n", __FILE__, __LINE__, round); exit (1); } return; } read_once_test (round, data); gpgme_data_seek (data, 0, SEEK_SET); read_once_test (round, data); } void write_test (round_t round, gpgme_data_t data) { char buffer[1024]; size_t amt; amt = gpgme_data_write (data, text, strlen (text)); if (amt != strlen (text)) fail_if_err (gpgme_error_from_errno (errno)); gpgme_data_seek (data, 0, SEEK_SET); if (round == TEST_INOUT_NONE) read_once_test (round, data); else { amt = gpgme_data_read (data, buffer, sizeof (buffer)); if (amt != strlen (text2) || strncmp (buffer, text2, strlen (text2))) { fprintf (stderr, "%s:%d: (%i) gpgme_data_read returned wrong data\n", __FILE__, __LINE__, round); exit (1); } amt = gpgme_data_read (data, buffer, sizeof (buffer)); if (amt) { fprintf (stderr, "%s:%d: (%i) gpgme_data_read did not signal EOF\n", __FILE__, __LINE__, round); exit (1); } } } int main (void) { round_t round = TEST_INITIALIZER; char *text_filename = make_filename ("t-data-1.txt"); char *longer_text_filename = make_filename ("t-data-2.txt"); const char *missing_filename = "this-file-surely-does-not-exist"; gpgme_error_t err = 0; gpgme_data_t data; init_gpgme_basic (); while (++round) { switch (round) { case TEST_INVALID_ARGUMENT: err = gpgme_data_new (NULL); if (!err) { fprintf (stderr, "%s:%d: gpgme_data_new on NULL pointer succeeded " "unexpectedly\n", __FILE__, __LINE__); exit (1); } continue; case TEST_INOUT_NONE: err = gpgme_data_new (&data); break; case TEST_INOUT_MEM_NO_COPY: err = gpgme_data_new_from_mem (&data, text, strlen (text), 0); break; case TEST_INOUT_MEM_COPY: err = gpgme_data_new_from_mem (&data, text, strlen (text), 1); break; case TEST_INOUT_MEM_FROM_FILE_COPY: err = gpgme_data_new_from_file (&data, text_filename, 1); break; case TEST_INOUT_MEM_FROM_INEXISTANT_FILE: err = gpgme_data_new_from_file (&data, missing_filename, 1); if (!err) { fprintf (stderr, "%s:%d: gpgme_data_new_from_file on inexistant " "file succeeded unexpectedly\n", __FILE__, __LINE__); exit (1); } continue; case TEST_INOUT_MEM_FROM_FILE_NO_COPY: err = gpgme_data_new_from_file (&data, text_filename, 0); /* This is not implemented yet. */ if (gpgme_err_code (err) == GPG_ERR_NOT_IMPLEMENTED || gpgme_err_code (err) == GPG_ERR_INV_VALUE) continue; break; case TEST_INOUT_MEM_FROM_FILE_PART_BY_NAME: err = gpgme_data_new_from_filepart (&data, longer_text_filename, 0, strlen (text), strlen (text)); break; case TEST_INOUT_MEM_FROM_INEXISTANT_FILE_PART: err = gpgme_data_new_from_filepart (&data, missing_filename, 0, strlen (text), strlen (text)); if (!err) { fprintf (stderr, "%s:%d: gpgme_data_new_from_file on inexistant " "file succeeded unexpectedly\n", __FILE__, __LINE__); exit (1); } continue; case TEST_INOUT_MEM_FROM_FILE_PART_BY_FP: { FILE *fp = fopen (longer_text_filename, "rb"); if (! fp) { fprintf (stderr, "%s:%d: fopen: %s\n", __FILE__, __LINE__, strerror (errno)); exit (1); } err = gpgme_data_new_from_filepart (&data, 0, fp, strlen (text), strlen (text)); } break; case TEST_END: goto out; case TEST_INITIALIZER: /* Shouldn't happen. */ fprintf (stderr, "%s:%d: impossible condition\n", __FILE__, __LINE__); exit (1); } fail_if_err (err); read_test (round, data); write_test (round, data); gpgme_data_release (data); } out: free (text_filename); free (longer_text_filename); return 0; } diff --git a/tests/t-engine-info.c b/tests/t-engine-info.c index 43257eb0..db46bf86 100644 --- a/tests/t-engine-info.c +++ b/tests/t-engine-info.c @@ -1,148 +1,148 @@ /* t-engine-info.c - Regression test for gpgme_get_engine_info. - Copyright (C) 2003, 2004, 2007 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2003, 2004, 2007 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include #define PGM "t-engine-info" static int verbose; #define fail_if_err(err) \ do \ { \ if (err) \ { \ fprintf (stderr, "%s:%d: gpgme_error_t %s\n", \ __FILE__, __LINE__, gpgme_strerror (err)); \ exit (1); \ } \ } \ while (0) int main (int argc, char **argv ) { int last_argc = -1; gpgme_engine_info_t info; gpgme_error_t err; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--")) { argc--; argv++; break; } else if (!strcmp (*argv, "--help")) { fputs ("usage: " PGM " [options]\n" "Options:\n" " --set-global-flag KEY VALUE\n", stdout); exit (0); } else if (!strcmp (*argv, "--verbose")) { verbose++; argc--; argv++; } else if (!strcmp (*argv, "--set-global-flag")) { argc--; argv++; if (argc < 2) { fprintf (stderr, PGM ": not enough arguments for option\n"); exit (1); } if (gpgme_set_global_flag (argv[0], argv[1])) { fprintf (stderr, PGM ": gpgme_set_global_flag failed\n"); exit (1); } argc--; argv++; argc--; argv++; } else if (!strncmp (*argv, "--", 2)) { fprintf (stderr, PGM ": unknown option '%s'\n", *argv); exit (1); } } if (argc) { fprintf (stderr, PGM ": unexpected arguments\n"); exit (1); } gpgme_check_version (NULL); { const char *keys[] = {"homedir", "sysconfdir", "bindir", "libexecdir", "libdir", "datadir", "localedir", "agent-socket", "agent-ssh-socket", "dirmngr-socket", "uiserver-socket", "gpgconf-name", "gpg-name", "gpgsm-name", "g13-name", "gpg-wks-client-name", NULL }; const char *s; int i; for (i=0; keys[i]; i++) if ((s = gpgme_get_dirinfo (keys[i]))) fprintf (stderr, "dirinfo: %s='%s'\n", keys[i], s); } err = gpgme_get_engine_info (&info); fail_if_err (err); for (; info; info = info->next) fprintf (stdout, "protocol=%d engine='%s' v='%s' (min='%s') home='%s'\n", info->protocol, info->file_name, info->version, info->req_version, info->home_dir? info->home_dir : "[default]"); return 0; } diff --git a/tests/t-version.c b/tests/t-version.c index 2782ca39..1958343e 100644 --- a/tests/t-version.c +++ b/tests/t-version.c @@ -1,92 +1,92 @@ /* t-version.c - Regression test. - Copyright (C) 2001, 2004 g10 Code GmbH - - This file is part of GPGME. - - GPGME is free software; you can redistribute it and/or modify it - under the terms of the GNU Lesser General Public License as - published by the Free Software Foundation; either version 2.1 of - the License, or (at your option) any later version. - - GPGME 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 - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser 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. */ + * Copyright (C) 2001, 2004 g10 Code GmbH + * + * This file is part of GPGME. + * + * GPGME is free software; you can redistribute it and/or modify it + * under the terms of the GNU Lesser General Public License as + * published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * GPGME 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this program; if not, see . + * SPDX-License-Identifier: LGPL-2.1-or-later + */ #ifdef HAVE_CONFIG_H #include #endif #include #include #include #include static int verbose; static int debug; int main (int argc, char **argv) { int ret; const char *null_result; const char *current_result; const char *future_result; int last_argc = -1; if (argc) { argc--; argv++; } while (argc && last_argc != argc ) { last_argc = argc; if (!strcmp (*argv, "--help")) { puts ("usage: ./t-version [options]\n" "\n" "Options:\n" " --verbose Show what is going on\n" ); exit (0); } if (!strcmp (*argv, "--verbose")) { verbose = 1; argc--; argv++; } else if (!strcmp (*argv, "--debug")) { verbose = debug = 1; argc--; argv++; } } null_result = gpgme_check_version (NULL); current_result = gpgme_check_version (VERSION); future_result = gpgme_check_version (VERSION ".1"); ret = !(null_result && ! strcmp (null_result, VERSION) && current_result && ! strcmp (current_result, VERSION) && ! future_result); if (verbose || ret) { printf ("Version from header: %s (0x%06x)\n", GPGME_VERSION, GPGME_VERSION_NUMBER); printf ("Version from binary: %s\n", gpgme_check_version (NULL)); printf ("Copyright blurb ...:%s\n", gpgme_check_version ("\x01\x01")); } return ret; }