1 /* t-encrypt-sign.c - Regression test.
2  * Copyright (C) 2000 Werner Koch (dd9jn)
3  * Copyright (C) 2001, 2002, 2003, 2004 g10 Code GmbH
4  *
5  * This file is part of GPGME.
6  *
7  * GPGME is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation; either version 2.1 of
10  * the License, or (at your option) any later version.
11  *
12  * GPGME is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this program; if not, see <https://gnu.org/licenses/>.
19  * SPDX-License-Identifier: LGPL-2.1-or-later
20  */
21 
22 /* We need to include config.h so that we know whether we are building
23    with large file system (LFS) support. */
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <string.h>
31 #include <unistd.h>
32 
33 #include <gpgme.h>
34 
35 #include "t-support.h"
36 
37 
38 static void
check_result(gpgme_sign_result_t result,gpgme_sig_mode_t type)39 check_result (gpgme_sign_result_t result, gpgme_sig_mode_t type)
40 {
41   if (result->invalid_signers)
42     {
43       fprintf (stderr, "Invalid signer found: %s\n",
44 	       result->invalid_signers->fpr);
45       exit (1);
46     }
47   if (!result->signatures || result->signatures->next)
48     {
49       fprintf (stderr, "Unexpected number of signatures created\n");
50       exit (1);
51     }
52   if (result->signatures->type != type)
53     {
54       fprintf (stderr, "Wrong type of signature created\n");
55       exit (1);
56     }
57   if (result->signatures->pubkey_algo != GPGME_PK_DSA)
58     {
59       fprintf (stderr, "Wrong pubkey algorithm reported: %i\n",
60 	       result->signatures->pubkey_algo);
61       exit (1);
62     }
63   if (result->signatures->hash_algo != GPGME_MD_SHA1
64       && result->signatures->hash_algo != GPGME_MD_RMD160)
65     {
66       fprintf (stderr, "Wrong hash algorithm reported: %i\n",
67 	       result->signatures->hash_algo);
68       exit (1);
69     }
70   if (result->signatures->sig_class != 0)
71     {
72       fprintf (stderr, "Wrong signature class reported: %u\n",
73 	       result->signatures->sig_class);
74       exit (1);
75     }
76   if (strcmp ("A0FF4590BB6122EDEF6E3C542D727CC768697734",
77 	      result->signatures->fpr))
78     {
79       fprintf (stderr, "Wrong fingerprint reported: %s\n",
80 	       result->signatures->fpr);
81       exit (1);
82     }
83 }
84 
85 
86 int
main(int argc,char ** argv)87 main (int argc, char **argv)
88 {
89   gpgme_ctx_t ctx;
90   gpgme_error_t err;
91   gpgme_data_t in, out;
92   gpgme_key_t key[3] = { NULL, NULL, NULL };
93   gpgme_encrypt_result_t result;
94   gpgme_sign_result_t sign_result;
95   char *agent_info;
96 
97   (void)argc;
98   (void)argv;
99 
100   init_gpgme (GPGME_PROTOCOL_OpenPGP);
101 
102   err = gpgme_new (&ctx);
103   fail_if_err (err);
104   gpgme_set_textmode (ctx, 1);
105   gpgme_set_armor (ctx, 1);
106 
107   agent_info = getenv("GPG_AGENT_INFO");
108   if (!(agent_info && strchr (agent_info, ':')))
109     gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
110 
111   err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0);
112   fail_if_err (err);
113 
114   err = gpgme_data_new (&out);
115   fail_if_err (err);
116 
117   err = gpgme_get_key (ctx, "A0FF4590BB6122EDEF6E3C542D727CC768697734",
118 		       &key[0], 0);
119   fail_if_err (err);
120   err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2",
121 		       &key[1], 0);
122   fail_if_err (err);
123 
124   err = gpgme_op_encrypt_sign (ctx, key, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
125   fail_if_err (err);
126   result = gpgme_op_encrypt_result (ctx);
127   if (result->invalid_recipients)
128     {
129       fprintf (stderr, "Invalid recipient encountered: %s\n",
130 	       result->invalid_recipients->fpr);
131       exit (1);
132     }
133   sign_result = gpgme_op_sign_result (ctx);
134   check_result (sign_result, GPGME_SIG_MODE_NORMAL);
135   print_data (out);
136 
137   gpgme_key_unref (key[0]);
138   gpgme_key_unref (key[1]);
139   gpgme_data_release (in);
140   gpgme_data_release (out);
141 
142   /* Now a second time using symmetric encryption.  */
143   err = gpgme_data_new_from_mem (&in, "Hallo Leute\n", 12, 0);
144   fail_if_err (err);
145 
146   err = gpgme_data_new (&out);
147   fail_if_err (err);
148 
149   err = gpgme_op_encrypt_sign (ctx, NULL, GPGME_ENCRYPT_ALWAYS_TRUST, in, out);
150   fail_if_err (err);
151   sign_result = gpgme_op_sign_result (ctx);
152   check_result (sign_result, GPGME_SIG_MODE_NORMAL);
153   print_data (out);
154 
155   gpgme_data_release (in);
156   gpgme_data_release (out);
157 
158 
159   gpgme_release (ctx);
160   return 0;
161 }
162