Imported Upstream version 1.8.0
[platform/upstream/gpgme.git] / src / tofupolicy.c
1 /* tofupolicy.c - Tofu policy helpers.
2  * Copyright (C) 2016 g10 Code GmbH
3  *
4  * This file is part of GPGME.
5  *
6  * GPGME is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as
8  * published by the Free Software Foundation; either version 2.1 of
9  * the License, or (at your option) any later version.
10  *
11  * GPGME is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this program; if not, see <https://www.gnu.org/licenses/>.
18  */
19
20 #if HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23 #include <stdlib.h>
24
25 #include "gpgme.h"
26 #include "debug.h"
27 #include "context.h"
28 #include "ops.h"
29
30
31 typedef struct
32 {
33   /* The error code from a FAILURE status line or 0.  */
34   gpg_error_t failure_code;
35
36   /* The error code from an ERROR status line or 0.  */
37   gpg_error_t error_code;
38
39 } *op_data_t;
40
41
42 \f
43 /* Parse an error status line.  Return the error location and the
44    error code.  The function may modify ARGS. */
45 static char *
46 parse_error (char *args, gpg_error_t *r_err)
47 {
48   char *where = strchr (args, ' ');
49   char *which;
50
51   if (where)
52     {
53       *where = '\0';
54       which = where + 1;
55
56       where = strchr (which, ' ');
57       if (where)
58         *where = '\0';
59
60       where = args;
61     }
62   else
63     {
64       *r_err = trace_gpg_error (GPG_ERR_INV_ENGINE);
65       return NULL;
66     }
67
68   *r_err = atoi (which);
69
70   return where;
71 }
72
73
74 static gpgme_error_t
75 tofu_policy_status_handler (void *priv, gpgme_status_code_t code, char *args)
76 {
77   gpgme_ctx_t ctx = (gpgme_ctx_t) priv;
78   gpgme_error_t err;
79   void *hook;
80   op_data_t opd;
81   char *loc;
82
83   err = _gpgme_op_data_lookup (ctx, OPDATA_TOFU_POLICY, &hook, -1, NULL);
84   opd = hook;
85   if (err)
86     return err;
87
88   switch (code)
89     {
90     case GPGME_STATUS_ERROR:
91       loc = parse_error (args, &err);
92       if (!loc)
93         return err;
94       if (!opd->error_code)
95         opd->error_code = err;
96       break;
97
98     case GPGME_STATUS_FAILURE:
99       opd->failure_code = _gpgme_parse_failure (args);
100       break;
101
102     case GPGME_STATUS_EOF:
103       if (opd->error_code)
104         err = opd->error_code;
105       else if (opd->failure_code)
106         err = opd->failure_code;
107       break;
108
109     default:
110       break;
111     }
112
113   return err;
114 }
115
116
117 /* Set the TOFU policy for KEY to POLICY.  */
118 static gpgme_error_t
119 tofu_policy_start (gpgme_ctx_t ctx, int synchronous,
120                    gpgme_key_t key, gpgme_tofu_policy_t policy)
121 {
122   gpgme_error_t err;
123   void *hook;
124   op_data_t opd;
125
126   if (ctx->protocol != GPGME_PROTOCOL_OPENPGP)
127     return gpgme_error (GPG_ERR_UNSUPPORTED_PROTOCOL);
128
129   if (!key)
130     return gpg_error (GPG_ERR_INV_VALUE);
131
132   err = _gpgme_op_reset (ctx, synchronous);
133   if (err)
134     return err;
135
136   err = _gpgme_op_data_lookup (ctx, OPDATA_TOFU_POLICY, &hook,
137                                sizeof (*opd), NULL);
138   opd = hook;
139   if (err)
140     return err;
141
142   _gpgme_engine_set_status_handler (ctx->engine, tofu_policy_status_handler,
143                                     ctx);
144
145   return _gpgme_engine_op_tofu_policy (ctx->engine, key, policy);
146 }
147
148
149
150 /* Set the TOFU policy of KEY to POLCIY.  This is the asynchronous
151  * variant.  */
152 gpgme_error_t
153 gpgme_op_tofu_policy_start (gpgme_ctx_t ctx,
154                             gpgme_key_t key, gpgme_tofu_policy_t policy)
155 {
156   gpg_error_t err;
157   TRACE_BEG2 (DEBUG_CTX, "gpgme_op_tofu_policy_start", ctx,
158               "key=%p, policy=%u", key, (unsigned int)policy);
159
160   if (!ctx)
161     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
162
163   err = tofu_policy_start (ctx, 0, key, policy);
164   return TRACE_ERR (err);
165 }
166
167
168 /* This is the synchronous variant of gpgme_op_tofu_policy_start.  */
169 gpgme_error_t
170 gpgme_op_tofu_policy (gpgme_ctx_t ctx,
171                       gpgme_key_t key, gpgme_tofu_policy_t policy)
172 {
173   gpgme_error_t err;
174   TRACE_BEG2 (DEBUG_CTX, "gpgme_op_tofu_policy", ctx,
175               "key=%p, policy=%u", key, (unsigned int)policy);
176
177   if (!ctx)
178     return TRACE_ERR (gpg_error (GPG_ERR_INV_VALUE));
179
180   err = tofu_policy_start (ctx, 1, key, policy);
181   if (!err)
182     err = _gpgme_wait_one (ctx);
183   return TRACE_ERR (err);
184 }