1 /* t-encrypt-mixed.c - Regression test.
2  * Copyright (C) 2016 by Bundesamt für Sicherheit in der Informationstechnik
3  * Software engineering by Intevation 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 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 #include <string.h>
29 
30 #include <gpgme.h>
31 
32 #include "t-support.h"
33 
34 /* Tests mixed symmetric and asymmetric decryption. Verifies
35    that an encrypted message can be decrypted without the
36    secret key but that the recipient is also set correctly. */
37 int
main(void)38 main (void)
39 {
40   gpgme_ctx_t ctx;
41   gpgme_error_t err;
42   gpgme_data_t in, out;
43   gpgme_key_t key[2] = { NULL, NULL };
44   gpgme_encrypt_result_t result;
45   gpgme_decrypt_result_t dec_result;
46   gpgme_recipient_t recipient;
47   const char *text = "Hallo Leute\n";
48   char *text2;
49   size_t len;
50 
51   init_gpgme (GPGME_PROTOCOL_OpenPGP);
52 
53   err = gpgme_new (&ctx);
54   fail_if_err (err);
55   gpgme_set_armor (ctx, 1);
56 
57   err = gpgme_data_new_from_mem (&in, text, strlen (text), 0);
58   fail_if_err (err);
59 
60   err = gpgme_data_new (&out);
61   fail_if_err (err);
62 
63   gpgme_set_passphrase_cb (ctx, passphrase_cb, NULL);
64 
65   /* A recipient for which we don't have a secret key */
66   err = gpgme_get_key (ctx, "D695676BDCEDCC2CDD6152BCFE180B1DA9E3B0B2",
67                        &key[0], 0);
68   fail_if_err (err);
69 
70   err = gpgme_op_encrypt (ctx, key,
71                           GPGME_ENCRYPT_ALWAYS_TRUST | GPGME_ENCRYPT_SYMMETRIC,
72                           in, out);
73   fail_if_err (err);
74   result = gpgme_op_encrypt_result (ctx);
75   if (result->invalid_recipients)
76     {
77       fprintf (stderr, "Invalid recipient encountered: %s\n",
78                result->invalid_recipients->fpr);
79       exit (1);
80     }
81 
82   print_data (out);
83 
84   /* Now try to decrypt */
85   gpgme_data_seek (out, 0, SEEK_SET);
86 
87   gpgme_data_release (in);
88   err = gpgme_data_new (&in);
89   fail_if_err (err);
90 
91   err = gpgme_op_decrypt (ctx, out, in);
92   fail_if_err (err);
93 
94   fputs ("Begin Result Decryption:\n", stdout);
95   print_data (in);
96   fputs ("End Result.\n", stdout);
97 
98   dec_result = gpgme_op_decrypt_result (ctx);
99   if (dec_result->unsupported_algorithm || dec_result->wrong_key_usage)
100     {
101       fprintf (stderr, "%s:%d: Decryption failed\n", __FILE__, __LINE__);
102       exit (1);
103     }
104 
105   text2 = gpgme_data_release_and_get_mem (in, &len);
106   if (strncmp (text, text2, len))
107     {
108       fprintf (stderr, "%s:%d: Wrong plaintext\n", __FILE__, __LINE__);
109       exit (1);
110     }
111 
112   recipient = dec_result->recipients;
113   if (!recipient || recipient->next)
114     {
115       fprintf (stderr, "%s:%d: Invalid recipients \n", __FILE__, __LINE__);
116       exit (1);
117     }
118 
119   if (strncmp (recipient->keyid, "5381EA4EE29BA37F", 16))
120     {
121       fprintf (stderr, "%s:%d: Not encrypted to recipient's subkey \n", __FILE__, __LINE__);
122       exit (1);
123     }
124 
125   gpgme_key_unref (key[0]);
126   free (text2);
127   gpgme_data_release (out);
128   gpgme_release (ctx);
129   return 0;
130 }
131